-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.c
504 lines (447 loc) · 10.6 KB
/
options.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
#include <assert.h>
#include <string.h>
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/param.h>
#include "common.h"
#include "library.h"
#define OPTION_ENTRY(x) MODE_##x,
enum mode {
#include "option_entries.h"
};
#undef OPTION_ENTRY
struct opstrings {
char *str;
enum mode mode;
};
#define OPTION_ENTRY(x) {"-:"#x, MODE_##x},
static struct opstrings opstrings[]={
#include "option_entries.h"
{NULL, 0}
};
#undef OPTION_ENTRY
/* need to maintain state for parsing -I<space>path */
static int include_space;
void die(char *error)
{
fprintf(stderr, "Error in command line: %s\n", error);
exit(1);
}
static char *add_slashes(char *in)
{
int incmode = 0;
int newlen = 0;
char *ptr, *out, *outptr;
if (strncasecmp(in, "-i", 2) == 0) {
incmode = 1;
}
ptr = in;
while (*ptr) {
if (*ptr == '"' || *ptr == ' ')
newlen++;
if (!incmode && (*ptr == '(' || *ptr == ')'))
newlen++;
newlen++; ptr++;
}
out = malloc(sizeof(char) * newlen + 1);
ptr = in;
outptr = out;
while (*ptr) {
if (*ptr == '"' || *ptr == ' ')
*(outptr++) = '\\';
if (!incmode && (*ptr == '(' || *ptr ==')'))
*(outptr++) = '\\';
if (!incmode && (*ptr == '<' || *ptr =='>'))
*(outptr++) = '\\';
*(outptr++) = *(ptr++);
}
*outptr = 0;
return out;
}
static void set_abs_top(struct project *p, char *str)
{
if (p->abs_top)
free(p->abs_top);
p->abs_top = str;
}
static void set_rel_top(struct project *p, char *str)
{
if (p->rel_top)
free(p->rel_top);
p->rel_top = str;
}
static struct project *new_project(char *name, enum script_type stype, enum build_type btype)
{
struct project *out = calloc(1, sizeof(struct project));
out->name = name;
out->stype = stype;
out->btype = btype;
return out;
}
static struct module *new_module(char *name, enum module_type mtype)
{
struct module *out = calloc(1, sizeof(struct module));
out->name = name;
out->mtype = mtype;
return out;
}
void add_tag(struct module *m, char *name)
{
enum tags tag = TAG_NONE;
if (strcmp("user", name) == 0)
tag = TAG_USER;
if (strcmp("eng", name) == 0)
tag = TAG_ENG;
if (strcmp("tests", name) == 0)
tag = TAG_TESTS;
if (strcmp("optional", name) == 0)
tag = TAG_OPTIONAL;
if (strcmp("debug", name) == 0)
tag = TAG_DEBUG;
m->tags |= tag;
free(name);
}
static char *path_subst(struct project *p, char *in)
{
char *ptr;
if (include_space) {
include_space = 0;
ptr = malloc(strlen(in) + 2);
ptr[0] = '-';
ptr[1] = 'I';
strcpy(ptr + 2, in);
free(in);
} else {
ptr = in;
}
if (p->abs_top && p->rel_top &&
(strlen(ptr) > 2) && (ptr[0] == '-') && (ptr[1] == 'I')) {
if (strncmp(ptr + 2, p->rel_top, strlen(p->rel_top)) == 0) {
int newlen = strlen(ptr) - strlen(p->rel_top)
+ strlen(p->abs_top) + 1;
char *newstr = malloc(newlen * sizeof(char));
newstr[0] = '-';
newstr[1] = 'I';
strcpy(newstr + 2, p->abs_top);
strcat(newstr, ptr + 2 + strlen(p->rel_top));
free(ptr);
return newstr;
}
}
return ptr;
}
static void add_cflag(struct project *p, struct module *m, char *flag)
{
int i;
if (strcmp("-I", flag) == 0) {
free(flag);
include_space = 1;
return;
}
if (strcmp("-Werror", flag) == 0) {
free(flag);
return;
}
if (strcmp("-pthread", flag) == 0) {
free(flag);
return;
}
flag = path_subst(p, flag);
for (i = 0; i < m->cflags; i++) {
if (strcmp(flag, m->cflag[i].flag) == 0) {
free(flag);
return;
}
}
m->cflags++;
m->cflag = realloc(m->cflag, m->cflags * sizeof(struct flag));
m->cflag[m->cflags - 1].flag = flag;
}
static void add_cppflag(struct project *p, struct module *m, char *flag)
{
if (strcmp("-Werror", flag) == 0) {
free(flag);
return;
}
flag = path_subst(p, flag);
m->cppflags++;
m->cppflag = realloc(m->cppflag, m->cppflags * sizeof(struct flag));
m->cppflag[m->cppflags - 1].flag = flag;
}
static int sources_filter(char *name)
{
int len = strlen(name);
if (len > 2) {
if ((strcmp(".h", name + len - 2) == 0)
|| (strcmp(".d", name + len - 2) == 0)) {
return 1;
}
}
if (len > 4) {
if ((strcmp(".asn", name + len - 4) == 0)
|| (strcmp(".map", name + len - 4) == 0)) {
return 1;
}
}
if (len > 5) {
if (strcmp(".list", name + len - 5) == 0)
return 1;
}
return 0;
}
static void add_source(struct module *m, char *name, struct generator *g)
{
if (sources_filter(name)) {
free(name);
return;
}
m->sources++;
m->source = realloc(m->source, m->sources * sizeof(struct source));
m->source[m->sources - 1].name = name;
m->source[m->sources - 1].gen = g;
}
static void add_header(struct module *m, char *name)
{
m->headers++;
m->header = realloc(m->header, m->headers * sizeof(struct header));
m->header[m->headers - 1].name = name;
}
static void add_passthrough(struct module *m, char *name)
{
m->passthroughs++;
m->passthrough = realloc(m->passthrough, m->passthroughs * sizeof(struct passthrough));
m->passthrough[m->passthroughs - 1].name = name;
}
static void add_libfilter(struct module *m, char *name, enum library_type ltype)
{
m->libfilters++;
m->libfilter = realloc(m->libfilter, m->libfilters * sizeof(struct library));
m->libfilter[m->libfilters - 1].name = name;
m->libfilter[m->libfilters - 1].ltype = ltype;
}
static void add_library(struct module *m, char *name, enum library_type ltype)
{
m->libraries++;
m->library = realloc(m->library, m->libraries * sizeof(struct library));
m->library[m->libraries - 1].name = name;
m->library[m->libraries - 1].ltype = ltype;
}
static int add_ldflag(struct module *m, char *flag, enum build_type btype)
{
enum library_type ltype;
int len = strlen(flag);
if (len < 2) {//this is probably a WTF condition...
free(flag);
return 0;
}
if (flag[0] == '-') {
if (flag[1] == 'L') {
free(flag);
return 0;
}
if (flag[1] == 'R') {
free(flag);
return 0;
}
if ((strcmp(flag, "-pthread") == 0) ||
(strcmp(flag, "-lpthread") == 0)) {
free(flag);
return 0;
}
if ((strcmp(flag, "-dlopen") == 0)) {
free(flag);
return 1;
}
if (flag[1] == 'l') {// actually figure out what libtype...
ltype = library_scope(flag + 2);
add_library(m, strdup(flag+2), ltype);
free(flag);
return 0;
}
add_library(m, flag, LIBRARY_FLAG);
} else {
char *dot = rindex(flag, '.');
if (dot && (strcmp(dot, ".lo") == 0)) {
free(flag);
return 0;
}
if (dot && (strcmp(dot, ".la") == 0)) {
char *slash = rindex(flag, '/');
char *temp, *lname;
if (slash)
lname = strstr(slash, "lib");
else
lname = strstr(flag, "lib");
*dot = 0;
if (lname) {
temp = flag;
flag = strdup(lname + 3);
free(temp);
add_library(m, flag, LIBRARY_EXTERNAL);
return 0;
}
free(flag);
return 0;
}
free(flag);
// add_library(m, flag, LIBRARY_FLAG);
}
return 0;
}
static void add_module(struct project *p, struct module *m)
{
p->modules++;
p->module = realloc(p->module, p->modules * sizeof(struct module));
p->module[p->modules - 1] = *m;
free(m);
}
static void add_subdir(struct project *p, char *name)
{
p->subdirs++;
p->subdir = realloc(p->subdir, p->subdirs * sizeof(struct subdir));
p->subdir[p->subdirs - 1].name = name;
}
static enum mode get_mode(char *arg)
{
int i;
int outval = MODE_UNDEFINED;
int len = strlen(arg);
if (len < 3)
return MODE_UNDEFINED;
if ((arg[0] != '-') || (arg[1] != ':'))
return MODE_UNDEFINED;
for (i = 0; opstrings[i].str != NULL; i++)
if (strcmp(opstrings[i].str, arg) == 0)
outval = opstrings[i].mode;
return outval;
}
struct project *options_parse(int argc, char **args)
{
enum mode mode = MODE_UNDEFINED;
char *arg;
int i, skip = 0;
enum build_type bt;
enum module_type mt;
struct project *p = NULL;
struct module *m = NULL;
if (getenv("ANDROGENIZER_NDK"))
bt = BUILD_NDK;
else bt = BUILD_EXTERNAL;
if (argc < 2) {
//print help!
return NULL;
}
for (i = 1; i < argc; i++) {
enum mode nm;
nm = get_mode(args[i]);
if (mode != MODE_PASSTHROUGH)
arg = add_slashes(args[i]);
else
arg = strdup(args[i]);
if (nm != MODE_UNDEFINED)
free(arg);
if (nm == MODE_UNDEFINED) {
switch (mode) {
case MODE_UNDEFINED:
assert(!!!"OH NOES!!!");
break;
case MODE_PROJECT:
p = new_project(arg, SCRIPT_SUBDIRECTORY, bt);
break;
case MODE_SUBDIR:
if (!p)
die("-:PROJECT must come before -:SUBDIR");
add_subdir(p, arg);
break;
case MODE_SHARED:
case MODE_STATIC:
case MODE_EXECUTABLE:
if (!p)
die("-:PROJECT must come before a module type");
if (m)
add_module(p, m);
if (mode == MODE_SHARED)
mt = MODULE_SHARED_LIBRARY;
if (mode == MODE_STATIC)
mt = MODULE_STATIC_LIBRARY;
if (mode == MODE_EXECUTABLE)
mt = MODULE_EXECUTABLE;
m = new_module(arg, mt);
break;
case MODE_SOURCES:
if (!m)
die("a module type must be declared before adding -:SOURCES");
add_source(m, arg, NULL);
break;
case MODE_LDFLAGS:
if (!m)
die("a module type must be declared before adding -:LDFLAGS");
if (!skip) {
skip = add_ldflag(m, arg, p->btype);
} else {
/* We were asked to skip this argument in the previous step */
skip = 0;
}
break;
case MODE_CFLAGS:
if (!p || !m)
die("a module type must be declared before adding -:CFLAGS");
add_cflag(p, m, arg);
break;
case MODE_CPPFLAGS:
if (!p || !m)
die("a module type must be declared before adding -:CPPFLAGS");
add_cppflag(p, m, arg);
break;
case MODE_TAGS:
if (!m)
die("a module type must be declared before setting -:TAGS");
add_tag(m, arg);
break;
case MODE_HEADER_TARGET:
if (!m)
die("a module type must be declared before setting a -:HEADER_TARGET");
if (m->header_target)
free(m->header_target);
m->header_target = arg;
break;
case MODE_HEADERS:
if (!m)
die("a module type must be declared before adding -:HEADERS");
add_header(m, arg);
break;
case MODE_PASSTHROUGH:
if (!m)
die("a module type must be declared before a -:PASSTHROUGH");
add_passthrough(m, arg);
break;
case MODE_REL_TOP:
if (!p)
die("a -:PROJECT must be declared before -:REL_TOP");
set_rel_top(p, arg);
break;
case MODE_ABS_TOP:
if (!p)
die("a -:PROJECT must be declared before -:ABS_TOP");
set_abs_top(p, arg);
break;
case MODE_LIBFILTER_STATIC:
if (!m)
die("a module type must be declared before adding libfilters");
add_libfilter(m, arg, LIBRARY_STATIC);
break;
case MODE_LIBFILTER_WHOLE:
if (!m)
die("a module type must be declared before adding libfilters");
add_libfilter(m, arg, LIBRARY_WHOLE_STATIC);
break;
case MODE_END:
break;
}
} else mode = nm;
}
if (p && m)
add_module(p, m);
return p;
}