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());
51 Options &toolOptions = dynamic_cast<CompileContext<Options> *>(context.value())->options();
52
53 // Process command-line options.
54 auto result = toolOptions.process(args);
55 if (result != EXIT_SUCCESS) {
56 return EXIT_FAILURE;
57 }
58
59 // Set up the compilation context.
60 // If not explicitly disabled, print basic information to standard output.
61 if (!toolOptions.disableInformationLogging) {
63 }
64
65 // Print the seed if it has been set.
66 if (toolOptions.seed) {
67 printInfo("============ Program seed %1% =============\n", *toolOptions.seed);
68 }
69
70 // Run the compiler to get an IR and invoke the tool.
71 const auto compilerResult = P4Tools::CompilerTarget::runCompiler(toolOptions, toolName);
72 if (!compilerResult.has_value()) {
73 return EXIT_FAILURE;
74 }
75 return mainImpl(compilerResult.value());
76 }
77};
78
79} // namespace P4::P4Tools
80
81#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:48
Definition compile_context.h:68