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