-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp_args.h
More file actions
194 lines (187 loc) · 5.6 KB
/
Copy pathcpp_args.h
File metadata and controls
194 lines (187 loc) · 5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#ifndef CPP_ARGS_H
#define CPP_ARGS_H
//
// Copyright © 2026-2026, David Priver <david@davidpriver.com>
//
// For handling common cli args for the CPP
// This is not thread-safe, but it should basically only be used from main, so who cares.
//
#include "Drp/argument_parsing.h"
#include "C/cpp_preprocessor.h"
#include "C/cc_target.h"
#ifdef __clang__
#pragma clang assume_nonnull begin
#endif
static
int
ma_sv_appender(ArgToParse* ap, const void* arg){
CppPreprocessor* cpp = ap->dest.user_pointer->user_data;
Marray(StringView)* dst = ap->dest.pointer;
const StringView* sv = arg;
int err = ma_push(StringView)(dst, cpp->allocator, *sv);
return err;
}
static
ArgParseDestination
cpp_ma_sv_dest(const ArgParseUserDefinedType* t, Marray(StringView)*dst){
ArgParseDestination dest = {
.type = ARG_USER_DEFINED,
.pointer = dst,
.user_pointer = t,
};
return dest;
}
static
int
cli_macro(ArgToParse* ap, const void* arg){
MStringBuilder* sb = ap->dest.user_pointer->user_data;
const StringView* sv = arg;
const char* eq = memchr(sv->text, '=', sv->length);
StringView name = eq?(StringView){eq-sv->text, sv->text}:*sv;
if(!name.length)
return ARGPARSE_CONVERSION_ERROR;
msb_write_literal(sb, "#define ");
msb_write_str(sb, name.text, name.length);
if(eq){
msb_write_char(sb, ' ');
msb_write_str(sb, eq+1, sv->text+sv->length-eq-1);
msb_write_char(sb, '\n');
}
else
msb_write_literal(sb, " 1\n");
return sb->errored?ARGPARSE_INTERNAL_ERROR:0;
}
static
ArgParseDestination
cpp_macro_dest(MStringBuilder* sb){
static _Bool init;
static ArgParseUserDefinedType t;
if(!init){
t = (ArgParseUserDefinedType){
.type_name = SV("macro_def"),
.user_data = sb,
};
init = 1;
}
ArgParseDestination dest = {
.type = ARG_USER_DEFINED,
.pointer = sb,
.user_pointer = &t,
};
return dest;
}
static MStringBuilder cpp_cli_macros = {.allocator=MALLOCATORI};
static _Bool cpp_nostdinc = 0;
static CcTarget cc_target_arg = CC_TARGET_NATIVE;
static const ArgParseEnumType cc_target_argparse_enum = {
.enum_size = sizeof(CcTarget),
.enum_count = CC_TARGET_COUNT,
.enum_names = cc_target_names,
};
static
ArgParseKwParams*
cpp_kwargs(CppPreprocessor* cpp){
static ArgParseUserDefinedType t = {
.type_name = SVI("path"),
};
t.user_data = cpp;
static ArgToParse kw_args[] = {
[0] = {
.name = SVI("-I"),
.append_proc = ma_sv_appender,
.help = "Extra include paths.",
.max_num = 1000,
.one_at_a_time = 1,
.space_sep_is_optional = 1,
},
[1] = {
.name = SVI("-isystem"),
.append_proc = ma_sv_appender,
.help = "Extra system include paths.",
.max_num = 1000,
.one_at_a_time = 1,
.space_sep_is_optional = 1,
},
[2] = {
.name = SVI("-iquote"),
.append_proc = ma_sv_appender,
.help = "Extra quote include paths.",
.max_num = 1000,
.one_at_a_time = 1,
.space_sep_is_optional = 1,
},
[3] = {
.name = SVI("-idirafter"),
.append_proc = ma_sv_appender,
.help = "Extra dirafter include paths.",
.max_num = 1000,
.one_at_a_time = 1,
.space_sep_is_optional = 1,
},
[4] = {
.name = SVI("-F"),
.append_proc = ma_sv_appender,
.help = "Extra framework paths.",
.max_num = 1000,
.one_at_a_time = 1,
.space_sep_is_optional = 1,
#ifndef __APPLE__
.hidden = 1,
#endif
},
[5] = {
.name = SVI("-D"),
.append_proc = cli_macro,
.help = "Predefined macros.",
.max_num=1000,
.one_at_a_time=1,
.space_sep_is_optional=1,
},
[6] = {
.name = SVI("--target"),
.dest = {.type = ARG_ENUM, .pointer = &cc_target_arg, .enum_pointer = &cc_target_argparse_enum},
.help = "Target ABI.",
.min_num = 0, .max_num = 1,
},
[7] = {
.name = SVI("--nostdinc"),
.altname1 = SVI("-nostdinc"),
.dest = ARGDESTI(&cpp_nostdinc),
.help = "Do not search standard system include paths.",
},
};
kw_args[0].dest = cpp_ma_sv_dest(&t, &cpp->Ipaths);
kw_args[1].dest = cpp_ma_sv_dest(&t, &cpp->isystem_paths);
kw_args[2].dest = cpp_ma_sv_dest(&t, &cpp->iquote_paths);
kw_args[3].dest = cpp_ma_sv_dest(&t, &cpp->idirafter_paths);
kw_args[4].dest = cpp_ma_sv_dest(&t, &cpp->framework_paths);
kw_args[5].dest = cpp_macro_dest(&cpp_cli_macros);
static ArgParseKwParams kwargs = {
.args = kw_args,
.count = sizeof kw_args / sizeof kw_args[0],
};
return &kwargs;
}
static
int
cpp_cli_defines(CppPreprocessor* cpp){
int err = 0;
if(cpp_cli_macros.cursor){
fc_write_path(cpp->fc, "(command line)", sizeof "(command line)" -1);
err = fc_cache_file(cpp->fc, msb_borrow_sv(&cpp_cli_macros));
if(err) return err;
err = cpp_include_file_via_file_cache(cpp, SV("(command line)"));
if(err) return err;
CppToken tok;
for(;;){
err = cpp_next_pp_token(cpp, &tok);
if(err) return err;
if(tok.type == CPP_EOF) break;
}
}
return err;
}
#ifdef __clang__
#pragma clang assume_nonnull end
#endif
#endif