P4C
The P4 Compiler
Loading...
Searching...
No Matches
p4ctool.h
1/*
2 * SPDX-FileCopyrightText: 2022 The P4 Language Consortium
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef BACKENDS_P4TOOLS_COMMON_P4CTOOL_H_
8#define BACKENDS_P4TOOLS_COMMON_P4CTOOL_H_
9
10#include <cstdlib>
11#include <type_traits>
12#include <vector>
13
14#include "backends/p4tools/common/compiler/compiler_target.h"
15#include "backends/p4tools/common/compiler/context.h"
16#include "backends/p4tools/common/lib/logging.h"
17#include "backends/p4tools/common/options.h"
18#include "frontends/common/options.h"
19
20namespace P4::P4Tools {
21
24//
25// Because of limitations of templates, method implementations must be inlined here.
26template <class Options,
27 typename = std::enable_if_t<std::is_base_of_v<AbstractP4cToolOptions, Options>>>
29 protected:
33 virtual int mainImpl(const CompilerResult &compilerResult) = 0;
34
35 virtual void registerTarget() = 0;
36
37 public:
41 int main(std::string_view toolName, const std::vector<const char *> &args) {
42 // Register supported compiler targets.
43 registerTarget();
44
45 // Initialize the target and the context.
46 auto context = Target::initializeTarget(toolName, args);
47 if (!context.has_value()) {
48 return EXIT_FAILURE;
49 }
50 AutoCompileContext autoContext(context.value().get());
51 Options &toolOptions =
52 dynamic_cast<CompileContext<Options> *>(context.value().get())->options();
53
54 // Process command-line options.
55 auto result = toolOptions.process(args);
56 if (result != EXIT_SUCCESS) {
57 return EXIT_FAILURE;
58 }
59
60 // Set up the compilation context.
61 // If not explicitly disabled, print basic information to standard output.
62 if (!toolOptions.disableInformationLogging) {
64 }
65
66 // Print the seed if it has been set.
67 if (toolOptions.seed) {
68 printInfo("============ Program seed %1% =============\n", *toolOptions.seed);
69 }
70
71 // Run the compiler to get an IR and invoke the tool.
72 const auto compilerResult = P4Tools::CompilerTarget::runCompiler(toolOptions, toolName);
73 if (!compilerResult.has_value()) {
74 return EXIT_FAILURE;
75 }
76 return mainImpl(compilerResult.value());
77 }
78};
79
80} // namespace P4::P4Tools
81
82#endif /* BACKENDS_P4TOOLS_COMMON_P4CTOOL_H_ */
Definition p4ctool.h:28
virtual int mainImpl(const CompilerResult &compilerResult)=0
int main(std::string_view toolName, const std::vector< const char * > &args)
Definition p4ctool.h:41
A compilation context for P4Tools that provides a custom compiler configuration.
Definition context.h:17
OptionsType & options() override
Definition context.h:30
Definition common/compiler/compiler_result.h:20
static CompilerResultOrError runCompiler(const CompilerOptions &options, std::string_view toolName)
Definition compiler_target.cpp:21
Definition common/compiler/compiler_result.cpp:7
void enableInformationLogging()
Enable the printing of basic run information.
Definition common/lib/logging.cpp:16
void printInfo(const std::string &fmt, Arguments &&...args)
Definition p4tools/common/lib/logging.h:63
Definition compile_context.h:69