Cthulhu  0.2.10
Cthulhu compiler collection
cmd.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-3.0-only
2 
3 #include "cmd.h"
4 
5 #include "config/config.h"
6 
8 #include "format/notify.h"
9 #include "std/vector.h"
10 #include "std/str.h"
11 
12 #include "notify/notify.h"
13 
14 #include "argparse/argparse.h"
15 
16 static const cfg_info_t kConfigInfo = {
17  .name = "cli",
18  .brief = "Cthulhu CLI configuration options",
19 };
20 
21 static const cfg_arg_t kLangArgs[] = { CT_ARG_SHORT("l"), CT_ARG_LONG("lang") };
22 
23 static const cfg_info_t kLang = {
24  .name = "lang",
25  .brief = "Load a language driver",
26  .args = CT_ARGS(kLangArgs),
27 };
28 
29 static const cfg_arg_t kPluginArgs[] = { CT_ARG_SHORT("p"), CT_ARG_LONG("plugin") };
30 
31 static const cfg_info_t kPlugin = {
32  .name = "plugin",
33  .brief = "Load a plugin",
34  .args = CT_ARGS(kPluginArgs),
35 };
36 
37 static const cfg_arg_t kTargetArgs[] = { CT_ARG_SHORT("t"), CT_ARG_LONG("target") };
38 
39 static const cfg_info_t kTarget = {
40  .name = "target",
41  .brief = "Load a target",
42  .args = CT_ARGS(kTargetArgs),
43 };
44 
45 static const cfg_arg_t kIrArgs[] = { CT_ARG_SHORT("ir"), CT_ARG_LONG("emit-ssa") };
46 
47 static const cfg_info_t kEmitIr = {
48  .name = "emit-ssa",
49  .brief = "Emit SSA IR to the output directory",
50  .args = CT_ARGS(kIrArgs),
51 };
52 
53 static const cfg_arg_t kTreeArgs[] = { CT_ARG_SHORT("tree"), CT_ARG_LONG("emit-tree") };
54 
55 static const cfg_info_t kEmitTree = {
56  .name = "emit-tree",
57  .brief = "Emit the parse tree to the output directory",
58  .args = CT_ARGS(kTreeArgs),
59 };
60 
61 static const cfg_arg_t kFileLayoutArgs[] = { CT_ARG_LONG("file-layout") };
62 
63 static const cfg_info_t kFileLayout = {
64  .name = "file-layout",
65  .brief = "File layout to use",
66  .args = CT_ARGS(kFileLayoutArgs),
67 };
68 
69 static const cfg_choice_t kFileLayoutChoices[] = {
70  { "pair", eFileLayoutPair },
71  { "single", eFileLayoutSingle },
72  { "tree", eFileLayoutTree },
73  { "flat", eFileLayoutFlat },
74 };
75 
76 static const cfg_arg_t kTargetOutputArgs[] = { CT_ARG_LONG("target-output") };
77 
78 static const cfg_info_t kTargetOutput = {
79  .name = "target-output",
80  .brief = "Target to use for output generation",
81  .args = CT_ARGS(kTargetOutputArgs),
82 };
83 
84 static const cfg_arg_t kWarnAsErrorArgs[] = { CT_ARG_SHORT("Werror"), CT_ARG_SHORT("WX") };
85 
86 static const cfg_info_t kWarnAsError = {
87  .name = "warn-as-error",
88  .brief = "Treat warnings as errors",
89  .args = CT_ARGS(kWarnAsErrorArgs),
90 };
91 
92 static const cfg_arg_t kReportLimitArgs[] = { CT_ARG_SHORT("ferror-limit"), CT_ARG_SHORT("fmax-errors") };
93 
94 static const cfg_info_t kReportLimit = {
95  .name = "max-errors",
96  .brief = "Limit the number of reports",
97  .args = CT_ARGS(kReportLimitArgs),
98 };
99 
100 static const cfg_arg_t kOutputDirArgs[] = { CT_ARG_SHORT("o"), CT_ARG_LONG("output-dir") };
101 
102 static const cfg_info_t kOutputDir = {
103  .name = "output-dir",
104  .brief = "Output directory for generated files",
105  .args = CT_ARGS(kOutputDirArgs),
106 };
107 
108 static const cfg_arg_t kReportStyleArgs[] = { CT_ARG_SHORT("report"), CT_ARG_LONG("report-style") };
109 
110 static const cfg_info_t kReportStyle = {
111  .name = "report-style",
112  .brief = "Report style to use",
113  .args = CT_ARGS(kReportStyleArgs),
114 };
115 
116 static const cfg_choice_t kReportStyleChoices[] = {
117  { "simple", eTextSimple },
118  { "complex", eTextComplex },
119 };
120 
122 {
123  cfg_group_t *config = config_root(&kConfigInfo, arena);
124 
125  setup_options_t options = setup_options(version, config);
126 
127  cfg_field_t *add_language_field = config_vector(config, &kLang, NULL);
128 
129  cfg_field_t *add_plugin_field = config_vector(config, &kPlugin, NULL);
130  cfg_field_t *add_target_field = config_vector(config, &kTarget, NULL);
131 
132  cfg_field_t *emit_tree_field = config_bool(config, &kEmitTree, false);
133  cfg_field_t *emit_ir_field = config_bool(config, &kEmitIr, false);
134 
135  cfg_field_t *output_dir_field = config_string(config, &kOutputDir, "builddir");
136 
137  cfg_enum_t file_layout_options = {
138  .options = kFileLayoutChoices,
139  .count = (sizeof(kFileLayoutChoices) / sizeof(cfg_choice_t)),
140  .initial = eFileLayoutPair,
141  };
142  cfg_field_t *file_layout_field = config_enum(config, &kFileLayout, file_layout_options);
143 
144  cfg_field_t *output_target_field = config_string(config, &kTargetOutput, "auto");
145 
146  cfg_field_t *warn_as_error_field = config_bool(options.report.group, &kWarnAsError, false);
147 
148  cfg_int_t report_limit_options = {.initial = 20, .min = 0, .max = 1000};
149  cfg_field_t *report_limit_field = config_int(options.report.group, &kReportLimit, report_limit_options);
150 
151  cfg_enum_t report_style_options = {
152  .options = kReportStyleChoices,
153  .count = (sizeof(kReportStyleChoices) / sizeof(cfg_choice_t)),
154  .initial = eTextSimple,
155  };
156  cfg_field_t *report_style_field = config_enum(options.report.group, &kReportStyle, report_style_options);
157 
158  tool_t tool = {
159  .config = config,
160  .options = options,
161 
162  .add_language = add_language_field,
163  .add_plugin = add_plugin_field,
164  .add_target = add_target_field,
165 
166  .emit_tree = emit_tree_field,
167  .emit_ssa = emit_ir_field,
168  .output_dir = output_dir_field,
169  .output_layout = file_layout_field,
170  .output_target = output_target_field,
171 
172  .warn_as_error = warn_as_error_field,
173  .report_limit = report_limit_field,
174  .report_style = report_style_field,
175  };
176 
177  return tool;
178 }
tool_t make_tool(version_info_t version, arena_t *arena)
Definition: cmd.c:121
CT_CONFIG_API cfg_field_t * config_enum(cfg_group_t *group, const cfg_info_t *info, cfg_enum_t cfg)
add a new choice field to a configuration group
Definition: config.c:162
CT_CONFIG_API cfg_field_t * config_int(cfg_group_t *group, const cfg_info_t *info, cfg_int_t cfg)
add a new integer field to a configuration group
Definition: config.c:108
CT_CONFIG_API cfg_field_t * config_string(cfg_group_t *group, const cfg_info_t *info, const char *initial)
add a new string field to a configuration group
Definition: config.c:138
CT_CONFIG_API cfg_field_t * config_vector(cfg_group_t *group, const cfg_info_t *info, vector_t *initial)
add a new vector field to a configuration group
Definition: config.c:150
CT_CONFIG_API cfg_field_t * config_bool(cfg_group_t *group, const cfg_info_t *info, bool initial)
add a new yes/no field to a configuration group
Definition: config.c:126
#define CT_ARGS(it)
Definition: config.h:57
#define CT_ARG_LONG(name)
Definition: config.h:55
CT_CONFIG_API cfg_group_t * config_root(const cfg_info_t *info, arena_t *arena)
create a new configuration group
Definition: config.c:97
#define CT_ARG_SHORT(name)
Definition: config.h:54
CT_SETUP_API setup_options_t setup_options(version_info_t info, cfg_group_t *root)
setup default options
Definition: setup.c:215
an allocator object
Definition: arena.h:86
a choice in a set of options
Definition: config.h:97
a choice from a set of options
Definition: config.h:107
information about a configuration field
Definition: config.h:69
STA_FIELD_STRING const char * name
the name of this field
Definition: config.h:71
an integer field
Definition: config.h:82
default options shared by all tools
Definition: setup.h:31
struct setup_options_t::@170 report
diagnostic reporting options
cfg_group_t * group
Definition: setup.h:38
Definition: cmd.h:14
cfg_group_t * config
Definition: cmd.h:15
version information for a driver/interface/plugin
Definition: version_def.h:48
@ eTextSimple
simple text reporting
Definition: notify.h:63
@ eTextComplex
complex text reporting
Definition: notify.h:64