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/lib/logging.h"
10#include "backends/p4tools/common/options.h"
11
12namespace P4Tools {
13
16//
17// Because of limitations of templates, method implementations must be inlined here.
18template <class Options,
19 typename = std::enable_if_t<std::is_base_of_v<AbstractP4cToolOptions, Options>>>
21 protected:
25 virtual int mainImpl(const CompilerResult &compilerResult) = 0;
26
27 virtual void registerTarget() = 0;
28
29 public:
33 int main(std::string_view toolName, const std::vector<const char *> &args) {
34 // Register supported compiler targets.
35 registerTarget();
36
37 // Process command-line options.
38 auto &toolOptions = Options::get();
39 auto compileContext = toolOptions.process(args);
40 if (!compileContext) {
41 return EXIT_FAILURE;
42 }
43
44 // Set up the compilation context.
45 AutoCompileContext autoContext(*compileContext);
46 // If not explicitly disabled, print basic information to standard output.
47 if (!toolOptions.disableInformationLogging) {
49 }
50
51 // Print the seed if it has been set.
52 if (toolOptions.seed) {
53 printInfo("============ Program seed %1% =============\n", *toolOptions.seed);
54 }
55
56 // Run the compiler to get an IR and invoke the tool.
57 const auto compilerResult = P4Tools::CompilerTarget::runCompiler(toolName);
58 if (!compilerResult.has_value()) {
59 return EXIT_FAILURE;
60 }
61 return mainImpl(compilerResult.value());
62 }
63};
64
65} // namespace P4Tools
66
67#endif /* BACKENDS_P4TOOLS_COMMON_P4CTOOL_H_ */
Definition p4ctool.h:20
int main(std::string_view toolName, const std::vector< const char * > &args)
Definition p4ctool.h:33
virtual int mainImpl(const CompilerResult &compilerResult)=0
Definition common/compiler/compiler_result.h:14
static CompilerResultOrError runCompiler(std::string_view toolName)
Definition compiler_target.cpp:30
Definition common/compiler/compiler_result.cpp:3
void printInfo(const std::string &fmt, Arguments &&...args)
Definition common/lib/logging.h:42
void enableInformationLogging()
Enable the printing of basic run information.
Definition common/lib/logging.cpp:12
Definition compile_context.h:75