-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsunder-compile.c
198 lines (179 loc) · 4.99 KB
/
sunder-compile.c
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
// SPDX-License-Identifier: Apache-2.0
#define _XOPEN_SOURCE /* getopt */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> /* getopt */
#include "sunder.h"
// clang-format off
static char const* path = NULL;
static sbuf(char const*) a_paths = NULL;
static sbuf(char const*) c_paths = NULL;
static sbuf(char const*) o_paths = NULL;
static bool opt_c = false;
static bool opt_d = false;
static bool opt_g = false;
static bool opt_k = false;
static sbuf(char const*) opt_L = NULL;
static sbuf(char const*) opt_l = NULL;
static char const* opt_o = "a.out";
// clang-format on
// List of additional .a, .c, and .o files.
static sbuf(char const*) paths = NULL;
static void
env(void);
static void
usage(void);
static void
argparse(int argc, char** argv);
static void
fini(void);
int
main(int argc, char** argv)
{
atexit(fini);
context_init();
atexit(context_fini);
argparse(argc, argv);
load_module(path, canonical_path(path));
if (!opt_c) {
validate_main_is_defined_correctly();
}
codegen(opt_c, opt_d, opt_g, opt_k, opt_L, opt_l, opt_o, paths);
return EXIT_SUCCESS;
}
static void
env(void)
{
printf("SUNDER_HOME=%s\n", context()->env.SUNDER_HOME);
printf("SUNDER_ARCH=%s\n", context()->env.SUNDER_ARCH);
printf("SUNDER_HOST=%s\n", context()->env.SUNDER_HOST);
printf("SUNDER_SEARCH_PATH=%s\n", context()->env.SUNDER_SEARCH_PATH);
printf("SUNDER_CC=%s\n", context()->env.SUNDER_CC);
printf("SUNDER_CFLAGS=%s\n", context()->env.SUNDER_CFLAGS);
}
static void
usage(void)
{
// clang-format off
char const* const lines[] = {
"Usage: sunder-compile [OPTION...] FILE",
"",
"Options:",
" -c Compile and assemble, but do not link.",
" -d Do not invoke the C compiler to compile, assemble, or link.",
" -e Display the Sunder environment and exit.",
" -g Generate debug information in output files.",
" -k Keep intermediate files.",
" -L DIR Add DIR to the linker path.",
" -l OPT Pass OPT directly to the linker.",
" -o OUT Write output file to OUT (default a.out).",
" -h Display usage information and exit.",
};
// clang-format on
for (size_t i = 0; i < ARRAY_COUNT(lines); ++i) {
fprintf(stderr, "%s\n", lines[i]);
}
}
static void
argparse(int argc, char** argv)
{
int c = 0;
while ((c = getopt(argc, argv, "cdegkL:l:o:h")) != -1) {
switch (c) {
case 'c': {
opt_c = true;
break;
}
case 'd': {
opt_d = true;
break;
}
case 'e': {
env();
exit(EXIT_SUCCESS);
break;
}
case 'g': {
opt_g = true;
break;
}
case 'k': {
opt_k = true;
break;
}
case 'L': {
sbuf_push(opt_L, optarg);
break;
}
case 'l': {
sbuf_push(opt_l, optarg);
break;
}
case 'o': {
opt_o = optarg;
break;
}
case 'h': {
usage();
exit(EXIT_SUCCESS);
break;
}
case '?': {
exit(EXIT_FAILURE);
break;
}
}
}
for (int i = optind; i < argc; ++i) {
if (cstr_ends_with(argv[i], ".a")) {
sbuf_push(a_paths, argv[i]);
sbuf_push(paths, argv[i]);
continue;
}
if (cstr_ends_with(argv[i], ".c")) {
sbuf_push(c_paths, argv[i]);
sbuf_push(paths, argv[i]);
continue;
}
if (cstr_ends_with(argv[i], ".o")) {
sbuf_push(o_paths, argv[i]);
sbuf_push(paths, argv[i]);
continue;
}
if (path != NULL) {
fatal(
NO_LOCATION,
"multiple input files (`%s` and `%s` both specified)",
path,
argv[i]);
}
path = argv[i];
}
if (opt_c && opt_d) {
fatal(NO_LOCATION, "options -c and -d are mutually exclusive");
}
if (path == NULL) {
fatal(NO_LOCATION, "no input file");
}
size_t const have_a_paths = sbuf_count(a_paths) != 0;
size_t const have_c_paths = sbuf_count(c_paths) != 0;
size_t const have_o_paths = sbuf_count(o_paths) != 0;
bool const invalid_a_paths_with_opt_c = opt_c && have_a_paths;
bool const invalid_c_paths_with_opt_c = opt_c && have_c_paths;
bool const invalid_o_paths_with_opt_c = opt_c && have_o_paths;
if (invalid_a_paths_with_opt_c) {
fatal(NO_LOCATION, "cannot compile .a files with -c specified");
}
if (invalid_c_paths_with_opt_c) {
fatal(NO_LOCATION, "cannot compile .c files with -c specified");
}
if (invalid_o_paths_with_opt_c) {
fatal(NO_LOCATION, "cannot compile .o files with -c specified");
}
}
static void
fini(void)
{
sbuf_fini(opt_l);
}