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
12
namespace
P4Tools
{
13
16
//
17
// Because of limitations of templates, method implementations must be inlined here.
18
template
<
class
Options,
19
typename
= std::enable_if_t<std::is_base_of_v<AbstractP4cToolOptions, Options>>>
20
class
AbstractP4cTool
{
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) {
48
enableInformationLogging
();
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_ */
P4Tools::AbstractP4cTool
Definition
p4ctool.h:20
P4Tools::AbstractP4cTool::main
int main(std::string_view toolName, const std::vector< const char * > &args)
Definition
p4ctool.h:33
P4Tools::AbstractP4cTool::mainImpl
virtual int mainImpl(const CompilerResult &compilerResult)=0
P4Tools::CompilerResult
Definition
common/compiler/compiler_result.h:14
P4Tools::CompilerTarget::runCompiler
static CompilerResultOrError runCompiler(std::string_view toolName)
Definition
compiler_target.cpp:30
P4Tools
Definition
common/compiler/compiler_result.cpp:3
P4Tools::printInfo
void printInfo(const std::string &fmt, Arguments &&...args)
Definition
common/lib/logging.h:42
P4Tools::enableInformationLogging
void enableInformationLogging()
Enable the printing of basic run information.
Definition
common/lib/logging.cpp:12
AutoCompileContext
Definition
compile_context.h:75
backends
p4tools
common
p4ctool.h
Generated by
1.11.0