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 // Process command-line options.
40 auto &toolOptions = Options::get();
41 auto compileContext = toolOptions.process(args);
42 if (!compileContext) {
43 return EXIT_FAILURE;
44 }
45
46 // Set up the compilation context.
47 AutoCompileContext autoContext(*compileContext);
48 // If not explicitly disabled, print basic information to standard output.
49 if (!toolOptions.disableInformationLogging) {
51 }
52
53 // Print the seed if it has been set.
54 if (toolOptions.seed) {
55 printInfo("============ Program seed %1% =============\n", *toolOptions.seed);
56 }
57
58 // Run the compiler to get an IR and invoke the tool.
59 const auto compilerResult = P4Tools::CompilerTarget::runCompiler(
60 CompileContext<CompilerOptions>::get().options(), toolName);
61 if (!compilerResult.has_value()) {
62 return EXIT_FAILURE;
63 }
64 return mainImpl(compilerResult.value());
65 }
66};
67
68} // namespace P4::P4Tools
69
70#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
Definition common/compiler/compiler_result.h:14
static CompilerResultOrError runCompiler(const CompilerOptions &options, std::string_view toolName)
Definition compiler_target.cpp:28
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 common/lib/logging.h:42
Definition compile_context.h:77