-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathlambda-cc.c
326 lines (281 loc) · 8.5 KB
/
lambda-cc.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdbool.h>
#include <sys/types.h>
#include <dirent.h>
#define PP_ARRAY_COUNT(ARRAY) \
(sizeof((ARRAY))/sizeof(*(ARRAY)))
#define PP_ARRAY_FOR(NAME, ARRAY) \
for (size_t NAME = 0; NAME < PP_ARRAY_COUNT(ARRAY); NAME++)
static void lcc_usage(const char *app) {
fprintf(stderr, "%s usage: [cc options]\n", app);
}
static void lcc_error(const char *message, ...) {
fprintf(stderr, "error: ");
va_list va;
va_start(va, message);
vfprintf(stderr, message, va);
fprintf(stderr, "\n");
va_end(va);
}
typedef struct {
const char *file;
size_t index;
bool cpp;
} lcc_source_t;
typedef struct {
const char *output;
size_t index;
bool aout;
} lcc_output_t;
typedef struct {
char *buffer;
size_t used;
size_t allocated;
} lcc_string_t;
static bool lcc_string_init(lcc_string_t *string) {
if (!(string->buffer = malloc(64)))
return false;
string->buffer[0] = '\0';
string->used = 1; /* always use the null byte */
string->allocated = 64;
return 1;
}
static bool lcc_string_resize(lcc_string_t *string) {
size_t request = string->allocated * 2;
void *attempt = realloc(string->buffer, request);
if (!attempt)
return false;
string->allocated = request;
string->buffer = attempt;
return true;
}
static bool lcc_string_appendf(lcc_string_t *string, const char *message, ...) {
va_list va1, va2;
va_start(va1, message);
va_copy(va2, va1); /* Copy the list */
size_t count = vsnprintf(NULL, 0, message, va1);
while (string->used + count >= string->allocated) {
if (!lcc_string_resize(string))
return false;
}
va_end(va1);
va_start(va2, message);
vsnprintf(string->buffer + string->used - 1, count + 1, message, va2);
va_end(va2);
string->used += count;
return true;
}
static void lcc_string_destroy(lcc_string_t *string) {
free(string->buffer);
}
static const char *lcc_lambdapp_find(void) {
char *search;
if ((search = getenv("LAMBDA_PP")))
return search;
static const char *bins[] = {
".", /* Try reliative to ourselfs as well */
"/bin",
"/usr/bin",
/* When lambdapp is included as a submodule in a project */
"lambdapp"
};
PP_ARRAY_FOR(bin, bins) {
DIR *dir = opendir(bins[bin]);
if (!dir)
continue;
struct dirent *entry;
while ((entry = readdir(dir))) {
if (entry->d_type != DT_REG && entry->d_type != DT_LNK)
continue;
if (!strcmp(entry->d_name, "lambda-pp")) {
closedir(dir);
return bins[bin];
}
}
closedir(dir);
}
return NULL;
}
static const char *lcc_compiler_find(void) {
/* Try enviroment variables first */
char *search;
if ((search = getenv("CC")))
return search;
if ((search = getenv("CXX")))
return search;
/* Start searching toolchain directories */
static const char *bins[] = {
"/bin",
"/usr/bin",
};
static const char *ccs[] = {
"cc", "gcc", "clang", "pathcc", "tcc"
};
PP_ARRAY_FOR(bin, bins) {
DIR *dir = opendir(bins[bin]);
if (!dir)
continue;
PP_ARRAY_FOR(cc, ccs) {
/* Scan the directory for one of the CCs */
struct dirent *entry;
while ((entry = readdir(dir))) {
/* Ignore things which are not files */
if (entry->d_type != DT_REG && entry->d_type != DT_LNK)
continue;
if (!strcmp(entry->d_name, ccs[cc])) {
closedir(dir);
return ccs[cc];
}
}
}
closedir(dir);
}
return NULL;
}
static bool lcc_source_find(int argc, char **argv, lcc_source_t *source) {
static const char *exts[] = {
/* C file extensions */
".c",
/* C++ file extensions */
".cc", ".cx", ".cxx", ".cpp",
};
for (int i = 0; i < argc; i++) {
PP_ARRAY_FOR(ext, exts) {
char *find = strstr(argv[i], exts[ext]);
if (!find)
continue;
/* It could be named stupidly like foo.c.c so we scan the whole filename
* until we reach the end (when strcmp will succeed).
*/
while (find) {
/* Make sure it's the end of the string */
if (!strcmp(find, exts[ext])) {
source->index = i;
source->file = argv[i];
source->cpp = (ext >= 1); /* See table of sources above for when this is valid. */
return true;
}
find = strstr(find + strlen(exts[ext]), exts[ext]);
}
}
}
return false;
}
static bool lcc_output_find(int argc, char **argv, lcc_output_t *output) {
for (int i = 0; i < argc; i++) {
if (strcmp(argv[i], "-o"))
continue;
if (i + 1 >= argc) /* expecting output */
return false;
output->index = i;
output->output = argv[i + 1];
return true;
}
return false;
}
int main(int argc, char **argv) {
argc--;
argv++;
if (!argc) {
lcc_usage(argv[-1]);
return 1;
}
const char *cc = lcc_compiler_find();
if (!cc) {
lcc_error("Couldn't find a compiler");
return 1;
}
const char *lambdapp = lcc_lambdapp_find();
if (!lambdapp) {
lcc_error("Couldn't find lambda-pp");
return 1;
}
/* Get the arguments */
lcc_string_t args_before; /* before -o */
lcc_string_t args_after; /* after -o */
if (!lcc_string_init(&args_before)) {
lcc_error("Out of memory");
return 1;
}
if (!lcc_string_init(&args_after)) {
lcc_error("Out of memory");
lcc_string_destroy(&args_before);
return 1;
}
/* Find the source file */
lcc_source_t source;
if (!lcc_source_find(argc, argv, &source)) {
/* If there isn't any source file on the command line it means
* the compiler is being used to invoke the linker.
*/
lcc_string_destroy(&args_after);
lcc_string_appendf(&args_before, "%s", cc);
for (int i = 0; i < argc; i++) {
if (!lcc_string_appendf(&args_before, " %s", argv[i]))
goto args_oom;
}
int attempt = system(args_before.buffer);
lcc_string_destroy(&args_before);
return attempt;
}
/* Find the output file */
lcc_output_t output = { .aout = false };
if (!lcc_output_find(argc, argv, &output)) {
/* not found? default to a.out */
output.output = "a.out";
output.aout = true;
}
/* Stop at the -o */
size_t stop = output.aout ? (size_t)argc : output.index;
for (size_t i = 0; i < stop; i++) {
/* Ignore the source file */
if (i == source.index)
continue;
if (!lcc_string_appendf(&args_before, "%s ", argv[i]))
goto args_oom;
}
/* Trim the trailing whitespace */
if (args_before.used >= 2)
args_before.buffer[args_before.used - 2] = '\0';
/* Handle anythng after the -o */
stop += 2; /* skip -o and <output> */
size_t count = argc;
if (stop != count) {
for (size_t i = stop; i < count; i++) {
if (!lcc_string_appendf(&args_after, "%s ", argv[i]))
goto args_oom;
}
}
/* Trim trailing whitespace */
if (args_after.used >= 2)
args_after.buffer[args_after.used - 2] = '\0';
/* Build the shell call */
lcc_string_t shell;
if (!lcc_string_init(&shell))
goto args_oom;
const char *lang = source.cpp ? "c++" : "c";
if (!lcc_string_appendf(&shell, "%s/lambda-pp %s | %s -x%s %s - -o %s %s",
lambdapp, source.file, cc, lang, args_before.buffer, output.output, args_after.buffer))
goto shell_oom;
int attempt = 0;
#ifndef _NDEBUG
/* Call the shell */
attempt = system(shell.buffer);
#else
printf("%s\n", shell.buffer);
#endif
lcc_string_destroy(&shell);
lcc_string_destroy(&args_before);
lcc_string_destroy(&args_after);
return attempt;
shell_oom:
lcc_string_destroy(&shell);
args_oom:
lcc_error("Out of memory");
lcc_string_destroy(&args_before);
lcc_string_destroy(&args_after);
return 1;
}