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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
/*
* Copyright (c) 2015 Andrew Kelley
*
* This file is part of zig, which is MIT licensed.
* See http://opensource.org/licenses/MIT
*/
#include "config.h"
#include "buffer.hpp"
#include "codegen.hpp"
#include "os.hpp"
#include "error.hpp"
#include <stdio.h>
static int usage(const char *arg0) {
fprintf(stderr, "Usage: %s [command] [options]\n"
"Commands:\n"
" build create executable, object, or library from target\n"
" test create and run a test build\n"
" version print version number and exit\n"
" parseh convert a c header file to zig extern declarations\n"
"Options:\n"
" --release build with optimizations on and debug protection off\n"
" --static output will be statically linked\n"
" --strip exclude debug symbols\n"
" --export [exe|lib|obj] override output type\n"
" --name [name] override output name\n"
" --output [file] override destination path\n"
" --verbose turn on compiler debug output\n"
" --color [auto|off|on] enable or disable colored error messages\n"
" --libc-lib-dir [path] set the C compiler data path\n"
" --libc-include-dir [path] set the C compiler data path\n"
" -isystem [dir] add additional search path for other .h files\n"
" -dirafter [dir] same as -isystem but do it last\n"
" --library-path [dir] add a directory to the library search path\n"
, arg0);
return EXIT_FAILURE;
}
enum Cmd {
CmdInvalid,
CmdBuild,
CmdTest,
CmdVersion,
CmdParseH,
};
int main(int argc, char **argv) {
char *arg0 = argv[0];
Cmd cmd = CmdInvalid;
const char *in_file = nullptr;
const char *out_file = nullptr;
bool is_release_build = false;
bool strip = false;
bool is_static = false;
OutType out_type = OutTypeUnknown;
const char *out_name = nullptr;
bool verbose = false;
ErrColor color = ErrColorAuto;
const char *libc_lib_dir = nullptr;
const char *libc_include_dir = nullptr;
ZigList<const char *> clang_argv = {0};
ZigList<const char *> lib_dirs = {0};
int err;
for (int i = 1; i < argc; i += 1) {
char *arg = argv[i];
if (arg[0] == '-') {
if (strcmp(arg, "--release") == 0) {
is_release_build = true;
} else if (strcmp(arg, "--strip") == 0) {
strip = true;
} else if (strcmp(arg, "--static") == 0) {
is_static = true;
} else if (strcmp(arg, "--verbose") == 0) {
verbose = true;
} else if (i + 1 >= argc) {
return usage(arg0);
} else {
i += 1;
if (i >= argc) {
return usage(arg0);
} else if (strcmp(arg, "--output") == 0) {
out_file = argv[i];
} else if (strcmp(arg, "--export") == 0) {
if (strcmp(argv[i], "exe") == 0) {
out_type = OutTypeExe;
} else if (strcmp(argv[i], "lib") == 0) {
out_type = OutTypeLib;
} else if (strcmp(argv[i], "obj") == 0) {
out_type = OutTypeObj;
} else {
return usage(arg0);
}
} else if (strcmp(arg, "--color") == 0) {
if (strcmp(argv[i], "auto") == 0) {
color = ErrColorAuto;
} else if (strcmp(argv[i], "on") == 0) {
color = ErrColorOn;
} else if (strcmp(argv[i], "off") == 0) {
color = ErrColorOff;
} else {
return usage(arg0);
}
} else if (strcmp(arg, "--name") == 0) {
out_name = argv[i];
} else if (strcmp(arg, "--libc-lib-dir") == 0) {
libc_lib_dir = argv[i];
} else if (strcmp(arg, "--libc-include-dir") == 0) {
libc_include_dir = argv[i];
} else if (strcmp(arg, "-isystem") == 0) {
clang_argv.append("-isystem");
clang_argv.append(argv[i]);
} else if (strcmp(arg, "-dirafter") == 0) {
clang_argv.append("-dirafter");
clang_argv.append(argv[i]);
} else if (strcmp(arg, "--library-path") == 0) {
lib_dirs.append(argv[i]);
} else {
return usage(arg0);
}
}
} else if (cmd == CmdInvalid) {
if (strcmp(arg, "build") == 0) {
cmd = CmdBuild;
} else if (strcmp(arg, "version") == 0) {
cmd = CmdVersion;
} else if (strcmp(arg, "parseh") == 0) {
cmd = CmdParseH;
} else if (strcmp(arg, "test") == 0) {
cmd = CmdTest;
} else {
fprintf(stderr, "Unrecognized command: %s\n", arg);
return usage(arg0);
}
} else {
switch (cmd) {
case CmdBuild:
case CmdParseH:
case CmdTest:
if (!in_file) {
in_file = arg;
} else {
return usage(arg0);
}
break;
case CmdVersion:
return usage(arg0);
case CmdInvalid:
zig_unreachable();
}
}
}
switch (cmd) {
case CmdBuild:
case CmdParseH:
case CmdTest:
{
if (!in_file)
return usage(arg0);
Buf in_file_buf = BUF_INIT;
buf_init_from_str(&in_file_buf, in_file);
Buf root_source_dir = BUF_INIT;
Buf root_source_code = BUF_INIT;
Buf root_source_name = BUF_INIT;
if (buf_eql_str(&in_file_buf, "-")) {
os_get_cwd(&root_source_dir);
if ((err = os_fetch_file(stdin, &root_source_code))) {
fprintf(stderr, "unable to read stdin: %s\n", err_str(err));
return 1;
}
buf_init_from_str(&root_source_name, "");
} else {
os_path_split(&in_file_buf, &root_source_dir, &root_source_name);
if ((err = os_fetch_file_path(buf_create_from_str(in_file), &root_source_code))) {
fprintf(stderr, "unable to open '%s': %s\n", in_file, err_str(err));
return 1;
}
}
CodeGen *g = codegen_create(&root_source_dir);
codegen_set_is_release(g, is_release_build);
codegen_set_is_test(g, cmd == CmdTest);
codegen_set_clang_argv(g, clang_argv.items, clang_argv.length);
codegen_set_strip(g, strip);
codegen_set_is_static(g, is_static);
if (out_type != OutTypeUnknown) {
codegen_set_out_type(g, out_type);
} else if (cmd == CmdTest) {
codegen_set_out_type(g, OutTypeExe);
}
if (out_name) {
codegen_set_out_name(g, buf_create_from_str(out_name));
} else if (cmd == CmdTest) {
codegen_set_out_name(g, buf_create_from_str("test"));
}
if (libc_lib_dir)
codegen_set_libc_lib_dir(g, buf_create_from_str(libc_lib_dir));
if (libc_include_dir)
codegen_set_libc_include_dir(g, buf_create_from_str(libc_include_dir));
codegen_set_verbose(g, verbose);
codegen_set_errmsg_color(g, color);
for (int i = 0; i < lib_dirs.length; i += 1) {
codegen_add_lib_dir(g, lib_dirs.at(i));
}
if (cmd == CmdBuild) {
codegen_add_root_code(g, &root_source_dir, &root_source_name, &root_source_code);
codegen_link(g, out_file);
return EXIT_SUCCESS;
} else if (cmd == CmdParseH) {
codegen_parseh(g, &root_source_dir, &root_source_name, &root_source_code);
codegen_render_ast(g, stdout, 4);
return EXIT_SUCCESS;
} else if (cmd == CmdTest) {
codegen_add_root_code(g, &root_source_dir, &root_source_name, &root_source_code);
codegen_link(g, "./test");
ZigList<const char *> args = {0};
int return_code;
os_spawn_process("./test", args, &return_code);
if (return_code != 0) {
fprintf(stderr, "\nTests failed. Use the following command to reproduce the failure:\n");
fprintf(stderr, "./test\n");
}
return return_code;
} else {
zig_unreachable();
}
}
case CmdVersion:
printf("%s\n", ZIG_VERSION_STRING);
return EXIT_SUCCESS;
case CmdInvalid:
return usage(arg0);
}
}
|