-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.c
679 lines (619 loc) · 15.4 KB
/
main.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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
/*
* make [--posix] [-C path] [-f makefile] [-j num] [-x pragma]
* [-ehiknpqrsSt] [macro[:[:[:]]]=val ...] [target ...]
*
* --posix Enforce POSIX mode (non-POSIX)
* -C Change directory to path (non-POSIX)
* -f Makefile name
* -j Number of jobs to run in parallel (not implemented)
* -x Pragma to make POSIX mode less strict (non-POSIX)
* -e Environment variables override macros in makefiles
* -h Display help information (non-POSIX)
* -i Ignore exit status
* -k Continue on error
* -n Pretend to make
* -p Print all macros & targets
* -q Question up-to-dateness of target. Return exit status 1 if not
* -r Don't use inbuilt rules
* -s Make silently
* -S Stop on error
* -t Touch files instead of making them
*/
#include "make.h"
uint32_t opts;
const char *myname;
const char *makefile;
struct file *makefiles;
#if ENABLE_FEATURE_MAKE_POSIX_2024
char *numjobs = NULL;
#endif
#if ENABLE_FEATURE_MAKE_EXTENSIONS
bool posix;
bool seen_first;
unsigned char pragma = 0;
unsigned char posix_level = DEFAULT_POSIX_LEVEL;
#endif
static void
usage(int exit_code)
{
FILE *fp = ENABLE_FEATURE_MAKE_EXTENSIONS && exit_code == 0 ?
stdout : stderr;
fprintf(fp,
"Usage: %s"
IF_FEATURE_MAKE_EXTENSIONS(" [--posix] [-C path]")
" [-f makefile]"
IF_FEATURE_MAKE_POSIX_2024(" [-j num]")
IF_FEATURE_MAKE_EXTENSIONS(" [-x pragma]")
IF_FEATURE_MAKE_EXTENSIONS("\n\t")
IF_NOT_FEATURE_MAKE_EXTENSIONS(" [-eiknpqrsSt] ")
IF_FEATURE_MAKE_EXTENSIONS(" [-ehiknpqrsSt] ")
IF_NOT_FEATURE_MAKE_POSIX_2024(
IF_FEATURE_MAKE_EXTENSIONS("[macro[:]=val ...]")
IF_NOT_FEATURE_MAKE_EXTENSIONS("[macro=val ...]")
)
IF_FEATURE_MAKE_POSIX_2024(
IF_FEATURE_MAKE_EXTENSIONS("[macro[:[:[:]]]=val ...]")
IF_NOT_FEATURE_MAKE_EXTENSIONS("[macro[::[:]]=val ...]")
)
" [target ...]\n", myname);
fprintf(fp, "\nThis build supports:"
IF_FEATURE_MAKE_EXTENSIONS(
" non-POSIX extensions,"
IF_FEATURE_MAKE_POSIX_2024(" POSIX 2024,")
" POSIX 2017\n"
)
IF_NOT_FEATURE_MAKE_EXTENSIONS(
IF_FEATURE_MAKE_POSIX_2024(" POSIX 2024")
IF_NOT_FEATURE_MAKE_POSIX_2024(" POSIX 2017")
)
);
#if ENABLE_FEATURE_MAKE_EXTENSIONS && ENABLE_FEATURE_MAKE_POSIX_2024
fprintf(fp,
"In strict POSIX mode the %s standard is enforced by default.\n",
DEFAULT_POSIX_LEVEL == STD_POSIX_2017 ? "2017" : "2024");
#endif
#if !ENABLE_FEATURE_MAKE_EXTENSIONS
# if ENABLE_FEATURE_MAKE_POSIX_2024
fprintf(fp, "\nFor details see:\n"
" https://pubs.opengroup.org/onlinepubs/9799919799.2024edition/utilities/make.html\n");
# else
fprintf(fp, "\nFor details see:\n"
" https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/make.html\n");
# endif
#endif
exit(exit_code);
}
/*
* Process options from an argv array. If from_env is non-zero we're
* handling options from MAKEFLAGS so skip '-C', '-f', '-p' and '-x'.
*/
static uint32_t
process_options(int argc, char **argv, int from_env)
{
int opt;
uint32_t flags = 0;
while ((opt = getopt(argc, argv, OPTSTR1 OPTSTR2 OPT_OFFSET)) != -1) {
switch(opt) {
#if ENABLE_FEATURE_MAKE_EXTENSIONS
case 'C':
if (!posix && !from_env) {
if (chdir(optarg) == -1) {
error("can't chdir to %s: %s", optarg, strerror(errno));
}
flags |= OPT_C;
break;
}
error("-C not allowed");
break;
#endif
case 'f': // Alternate file name
if (!from_env) {
makefiles = newfile(optarg, makefiles);
flags |= OPT_f;
}
break;
case 'e': // Prefer env vars to macros in makefiles
flags |= OPT_e;
break;
#if ENABLE_FEATURE_MAKE_EXTENSIONS
case 'h': // Print usage message and exit
if (posix)
error("-h not allowed");
usage(0);
break;
#endif
case 'i': // Ignore fault mode
flags |= OPT_i;
break;
#if ENABLE_FEATURE_MAKE_POSIX_2024
case 'j':
if (!POSIX_2017) {
const char *s;
for (s = optarg; *s; ++s) {
if (!isdigit(*s)) {
usage(2);
}
}
free(numjobs);
numjobs = xstrdup(optarg);
flags |= OPT_j;
break;
}
error("-j not allowed");
break;
#endif
case 'k': // Continue on error
flags |= OPT_k;
flags &= ~OPT_S;
break;
case 'n': // Pretend mode
flags |= OPT_n;
break;
case 'p':
if (!from_env)
flags |= OPT_p;
break;
case 'q':
flags |= OPT_q;
break;
case 'r':
flags |= OPT_r;
break;
case 't':
flags |= OPT_t;
break;
case 's': // Silent about commands
flags |= OPT_s;
break;
case 'S': // Stop on error
flags |= OPT_S;
flags &= ~OPT_k;
break;
#if ENABLE_FEATURE_MAKE_EXTENSIONS
case 'x': // Pragma
if (!from_env) {
set_pragma(optarg);
flags |= OPT_x;
}
break;
#endif
default:
if (from_env)
error("invalid MAKEFLAGS");
else
usage(2);
}
}
return flags;
}
/*
* Split the contents of MAKEFLAGS into an argv array. If the return
* value (call it fargv) isn't NULL the caller should free fargv[1] and
* fargv.
*/
static char **
expand_makeflags(int *fargc)
{
const char *m, *makeflags = getenv("MAKEFLAGS");
char *p, *argstr;
int argc;
char **argv;
if (makeflags == NULL)
return NULL;
while (isblank(*makeflags))
makeflags++;
if (*makeflags == '\0')
return NULL;
p = argstr = xmalloc(strlen(makeflags) + 2);
// If MAKEFLAGS doesn't start with a hyphen, doesn't look like
// a macro definition and only contains valid option characters,
// add a hyphen.
argc = 3;
if (makeflags[0] != '-' && strchr(makeflags, '=') == NULL) {
if (strspn(makeflags, OPTSTR1 + 1) != strlen(makeflags))
error("invalid MAKEFLAGS");
*p++ = '-';
} else {
// MAKEFLAGS may need to be split, estimate size of argv array.
for (m = makeflags; *m; ++m) {
if (isblank(*m))
argc++;
}
}
argv = xmalloc(argc * sizeof(char *));
argc = 0;
argv[argc++] = (char *)myname;
argv[argc++] = argstr;
// Copy MAKEFLAGS into argstr, splitting at non-escaped blanks.
m = makeflags;
do {
if (*m == '\\' && m[1] != '\0')
m++; // Skip backslash, copy next character unconditionally.
else if (isblank(*m)) {
// Terminate current argument and start a new one.
*p++ = '\0';
argv[argc++] = p;
do {
m++;
} while (isblank(*m));
continue;
}
*p++ = *m++;
} while (*m != '\0');
*p = '\0';
argv[argc] = NULL;
*fargc = argc;
return argv;
}
/*
* Instantiate all macros in an argv-style array of pointers. Stop
* processing at the first string that doesn't contain an equal sign.
* As an extension, target arguments on the command line (level 1)
* are skipped and will be processed later.
*/
static char **
process_macros(char **argv, int level)
{
char *equal;
for (; *argv; argv++) {
#if ENABLE_FEATURE_MAKE_EXTENSIONS || ENABLE_FEATURE_MAKE_POSIX_2024
char *colon = NULL;
int immediate = 0;
#endif
#if ENABLE_FEATURE_MAKE_POSIX_2024
int except_dollar = FALSE;
#endif
if (!(equal = strchr(*argv, '='))) {
#if ENABLE_FEATURE_MAKE_EXTENSIONS
// Skip targets on the command line
if (!posix && level == 1)
continue;
else
#endif
// Stop at first target
break;
}
#if ENABLE_FEATURE_MAKE_EXTENSIONS || ENABLE_FEATURE_MAKE_POSIX_2024
if (equal - 1 > *argv && equal[-1] == ':') {
# if ENABLE_FEATURE_MAKE_POSIX_2024
if (equal - 2 > *argv && equal[-2] == ':') {
if (POSIX_2017)
error("invalid macro assignment");
if (equal - 3 > *argv && equal[-3] == ':') {
// BSD-style ':='. Expand RHS, but not '$$',
// resulting macro is delayed expansion.
colon = equal - 3;
except_dollar = TRUE;
} else {
// GNU-style ':='. Expand RHS, including '$$',
// resulting macro is immediate expansion.
colon = equal - 2;
immediate = M_IMMEDIATE;
}
*colon = '\0';
} else
# endif
{
# if ENABLE_FEATURE_MAKE_EXTENSIONS
if (posix)
error("invalid macro assignment");
colon = equal - 1;
immediate = M_IMMEDIATE;
*colon = '\0';
# else
error("invalid macro assignment");
# endif
}
} else
#endif
*equal = '\0';
/* We want to process _most_ macro assignments.
* There are exceptions for particular values from the
* environment. */
if (!((level & M_ENVIRON) &&
(strcmp(*argv, "MAKEFLAGS") == 0
|| strcmp(*argv, "SHELL") == 0
#if ENABLE_FEATURE_MAKE_POSIX_2024
|| (strcmp(*argv, "CURDIR") == 0 && !useenv && !POSIX_2017)
#endif
))) {
#if ENABLE_FEATURE_MAKE_EXTENSIONS || ENABLE_FEATURE_MAKE_POSIX_2024
if (colon) {
char *exp = expand_macros(equal + 1, except_dollar);
setmacro(*argv, exp, level | immediate);
free(exp);
} else
#endif
setmacro(*argv, equal + 1, level);
}
#if ENABLE_FEATURE_MAKE_EXTENSIONS || ENABLE_FEATURE_MAKE_POSIX_2024
if (colon)
*colon = ':';
else
#endif
*equal = '=';
}
return argv;
}
/*
* Update the MAKEFLAGS macro and environment variable to include any
* command line options that don't have their default value (apart from
* -f, -p and -S). Also add any macros defined on the command line or
* by the MAKEFLAGS environment variable (apart from MAKEFLAGS itself).
* Add macros that were defined on the command line to the environment.
*/
static void
update_makeflags(void)
{
int i;
char optbuf[] = "-?";
char *makeflags = NULL;
char *macro, *s;
const char *t;
struct macro *mp;
t = OPTSTR1 + 1;
for (i = 0; *t; t++) {
#if ENABLE_FEATURE_MAKE_POSIX_2024
if (*t == ':')
continue;
#endif
if ((opts & OPT_MASK & (1 << i))) {
optbuf[1] = *t;
makeflags = xappendword(makeflags, optbuf);
#if ENABLE_FEATURE_MAKE_POSIX_2024
if (*t == 'j') {
makeflags = xappendword(makeflags, numjobs);
}
#endif
}
i++;
}
for (i = 0; i < HTABSIZE; ++i) {
for (mp = macrohead[i]; mp; mp = mp->m_next) {
if ((mp->m_level == 1 || mp->m_level == 2) &&
strcmp(mp->m_name, "MAKEFLAGS") != 0) {
macro = xmalloc(strlen(mp->m_name) + 2 * strlen(mp->m_val) + 1);
s = stpcpy(macro, mp->m_name);
*s++ = '=';
for (t = mp->m_val; *t; t++) {
if (*t == '\\' || isblank(*t))
*s++ = '\\';
*s++ = *t;
}
*s = '\0';
makeflags = xappendword(makeflags, macro);
free(macro);
// Add command line macro definitions to the environment
if (mp->m_level == 1 && strcmp(mp->m_name, "SHELL") != 0)
setenv(mp->m_name, mp->m_val, 1);
}
}
}
if (makeflags) {
setmacro("MAKEFLAGS", makeflags, 0);
setenv("MAKEFLAGS", makeflags, 1);
free(makeflags);
}
}
static void
make_handler(int sig)
{
signal(sig, SIG_DFL);
remove_target();
kill(getpid(), sig);
}
static void
init_signal(int sig)
{
struct sigaction sa, new_action;
sigemptyset(&new_action.sa_mask);
new_action.sa_flags = 0;
new_action.sa_handler = make_handler;
sigaction(sig, NULL, &sa);
if (sa.sa_handler != SIG_IGN)
sigaction(sig, &new_action, NULL);
}
/*
* If the global option flag associated with a special target hasn't
* been set mark all prerequisites of the target with a flag. If the
* target had no prerequisites set the global option flag.
*/
static void
mark_special(const char *special, uint32_t oflag, uint16_t nflag)
{
struct name *np;
struct rule *rp;
struct depend *dp;
int marked = FALSE;
if (!(opts & oflag) && (np = findname(special))) {
for (rp = np->n_rule; rp; rp = rp->r_next) {
for (dp = rp->r_dep; dp; dp = dp->d_next) {
dp->d_name->n_flag |= nflag;
marked = TRUE;
}
}
if (!marked)
opts |= oflag;
}
}
int
main(int argc, char **argv)
{
#if ENABLE_FEATURE_MAKE_POSIX_2024
const char *path, *newpath = NULL;
#else
const char *path = "make";
#endif
char **fargv, **fargv0;
int fargc, estat;
bool found_target;
FILE *ifd;
struct file *fp;
if (argc == 0) {
return EXIT_FAILURE;
}
myname = basename(*argv);
#if ENABLE_FEATURE_MAKE_EXTENSIONS
if (argv[1] && strcmp(argv[1], "--posix") == 0) {
argv[1] = argv[0];
++argv;
--argc;
setenv("PDPMAKE_POSIXLY_CORRECT", "", 1);
posix = TRUE;
} else {
posix = getenv("PDPMAKE_POSIXLY_CORRECT") != NULL;
}
pragmas_from_env();
#endif
#if ENABLE_FEATURE_MAKE_POSIX_2024
if (!POSIX_2017) {
path = argv[0];
if (argv[0][0] != '/' && strchr(argv[0], '/')) {
// Make relative path absolute
path = newpath = realpath(argv[0], NULL);
if (!path) {
error("can't resolve path for %s: %s", argv[0], strerror(errno));
}
}
} else {
path = "make";
}
#endif
// Process options from MAKEFLAGS
fargv = fargv0 = expand_makeflags(&fargc);
if (fargv0) {
opts = process_options(fargc, fargv, TRUE);
fargv = fargv0 + optind;
// Reset getopt(3) so we can call it again
GETOPT_RESET();
}
// Process options from the command line
opts |= process_options(argc, argv, FALSE);
argv += optind;
init_signal(SIGHUP);
init_signal(SIGTERM);
setmacro("$", "$", 0 | M_VALID);
#if ENABLE_FEATURE_MAKE_EXTENSIONS
pragmas_to_env();
// Process macro definitions from the command line
if (!posix)
process_macros(argv, 1);
else
#endif
// In POSIX mode macros must appear before targets.
// argv should now point to targets only.
argv = process_macros(argv, 1);
// Process macro definitions from MAKEFLAGS
if (fargv) {
process_macros(fargv, 2);
free(fargv0[1]);
free(fargv0);
}
// Process macro definitions from the environment
process_macros(environ, 3 | M_ENVIRON);
// Update MAKEFLAGS and environment
update_makeflags();
// Read built-in rules
input(NULL, 0);
setmacro("SHELL", "/bin/sh", 4);
setmacro("MAKE", path, 4);
#if ENABLE_FEATURE_MAKE_POSIX_2024
if (!POSIX_2017) {
char *cwd = NULL;
size_t len = 0;
do {
len += 256;
cwd = xrealloc(cwd, len);
if (getcwd(cwd, len)) {
if (!useenv) {
// Export cwd to environment, if necessary
char *envcwd = getenv("CURDIR");
if (envcwd && strcmp(cwd, envcwd) != 0)
setenv("CURDIR", cwd, 1);
}
setmacro("CURDIR", cwd, 4);
break;
}
} while (errno == ERANGE);
free(cwd);
}
free((void *)newpath);
#endif
fp = makefiles;
if (!fp) { // Look for a default Makefile
#if ENABLE_FEATURE_MAKE_EXTENSIONS
if (!posix && (ifd = fopen("PDPmakefile", "r")) != NULL)
makefile = "PDPmakefile";
else
#endif
if ((ifd = fopen("makefile", "r")) != NULL)
makefile = "makefile";
else if ((ifd = fopen("Makefile", "r")) != NULL)
makefile = "Makefile";
else
error("no makefile found");
goto read_makefile;
}
while (fp) {
if (strcmp(fp->f_name, "-") == 0) { // Can use stdin as makefile
ifd = stdin;
makefile = "stdin";
} else {
if ((ifd = fopen(fp->f_name, "r")) == NULL)
error("can't open %s: %s", fp->f_name, strerror(errno));
makefile = fp->f_name;
}
fp = fp->f_next;
read_makefile:
input(ifd, 0);
fclose(ifd);
makefile = NULL;
}
if (print)
print_details();
mark_special(".SILENT", OPT_s, N_SILENT);
mark_special(".IGNORE", OPT_i, N_IGNORE);
mark_special(".PRECIOUS", OPT_precious, N_PRECIOUS);
#if ENABLE_FEATURE_MAKE_POSIX_2024
if (!POSIX_2017)
mark_special(".PHONY", OPT_phony, N_PHONY);
#endif
#if ENABLE_FEATURE_MAKE_EXTENSIONS
if (posix)
#endif
{
// In POSIX mode only targets should now be in argv.
found_target = FALSE;
for (char **a = argv; *a; a++) {
if (!strchr(*a, '='))
found_target = TRUE;
else if (found_target)
error("macro assignments must precede targets");
}
}
estat = 0;
found_target = FALSE;
for (; *argv; argv++) {
#if ENABLE_FEATURE_MAKE_EXTENSIONS
// Skip macro assignments.
if (strchr(*argv, '='))
continue;
#endif
found_target = TRUE;
estat |= make(newname(*argv), 0);
}
if (!found_target) {
if (!firstname)
error("no targets defined");
estat = make(firstname, 0);
}
#if ENABLE_FEATURE_CLEAN_UP
# if ENABLE_FEATURE_MAKE_POSIX_2024
free((void *)numjobs);
# endif
freenames();
freemacros();
freefiles(makefiles);
#endif
return estat & MAKE_FAILURE;
}