-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
2012 lines (1755 loc) · 46.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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $NetBSD: main.c,v 1.599 2023/09/10 21:52:36 rillig Exp $ */
/*
* Copyright (c) 1988, 1989, 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Adam de Boor.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Copyright (c) 1989 by Berkeley Softworks
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Adam de Boor.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* The main file for this entire program. Exit routines etc. reside here.
*
* Utility functions defined in this file:
*
* Main_ParseArgLine
* Parse and process command line arguments from a
* single string. Used to implement the special targets
* .MFLAGS and .MAKEFLAGS.
*
* Error Print a tagged error message.
*
* Fatal Print an error message and exit.
*
* Punt Abort all jobs and exit with a message.
*
* Finish Finish things up by printing the number of errors
* that occurred, and exit.
*/
#include <errno.h>
#include <time.h>
#include <process.h>
#include <sys/stat.h>
#include <sys/timeb.h>
#include <io.h>
#include <direct.h>
#include "make.h"
#include "dir.h"
#include "job.h"
#include "trace.h"
/* "@(#)main.c 8.3 (Berkeley) 3/19/94" */
CmdOpts opts;
time_t now; /* Time at start of make */
GNode *defaultNode; /* .DEFAULT node */
bool allPrecious; /* .PRECIOUS given on a line by itself */
bool deleteOnError; /* .DELETE_ON_ERROR: set */
static int maxJobTokens; /* -j argument */
static bool enterFlagObj; /* -w and objdir != srcdir */
static HANDLE jp_0 = NULL, jp_1 = NULL; /* ends of parent job pipe */
bool doing_depend; /* Set while reading .depend */
static bool jobsRunning; /* true if the jobs might be running */
static const char *tracefile;
static bool ReadMakefile(const char *);
static void purge_relative_cached_realpaths(void);
static char objdir[MAXPATHLEN + 1]; /* where we chdir'ed to */
char curdir[MAXPATHLEN + 1]; /* Startup directory */
const char *progname;
char *makeDependfile;
DWORD myPid;
int makelevel;
static DWORD numCpus;
bool forceJobs = false;
static int main_errors = 0;
static HashTable cached_realpaths;
/*
* For compatibility with the POSIX version of MAKEFLAGS that includes
* all the options without '-', convert 'flags' to '-f -l -a -g -s '.
*/
static char *
explode(const char *flags)
{
char *exploded, *ep;
const char *p;
if (flags == NULL)
return NULL;
for (p = flags; *p != '\0'; p++)
if (!ch_isalpha(*p))
return bmake_strdup(flags);
exploded = bmake_malloc((size_t)(p - flags) * 3 + 1);
for (p = flags, ep = exploded; *p != '\0'; p++) {
*ep++ = '-';
*ep++ = *p;
*ep++ = ' ';
}
*ep = '\0';
return exploded;
}
MAKE_ATTR_DEAD static void
usage(void)
{
size_t prognameLen = strcspn(progname, "[");
(void)fprintf(stderr,
"usage: %.*s [-BeikNnqrSstWwX]\n"
" [-C directory] [-D variable] [-d flags] [-f makefile]\n"
" [-I directory] [-J private] [-j max_jobs] [-m directory] [-T file]\n"
" [-V variable] [-v variable] [variable=value] [target ...]\n",
(int)prognameLen, progname);
exit(2);
}
static void
MainParseArgDebugFile(const char *arg)
{
const char *mode;
size_t len;
char *fname;
if (opts.debug_file != stdout && opts.debug_file != stderr)
fclose(opts.debug_file);
if (*arg == '+') {
arg++;
mode = "a";
} else
mode = "w";
if (strcmp(arg, "stdout") == 0) {
opts.debug_file = stdout;
return;
}
if (strcmp(arg, "stderr") == 0) {
opts.debug_file = stderr;
return;
}
len = strlen(arg);
fname = bmake_malloc(len + 20);
memcpy(fname, arg, len + 1);
/* Replace the trailing '%d' after '.%d' with the pid. */
if (len >= 3 && memcmp(fname + len - 3, ".%d", 3) == 0)
snprintf(fname + len - 2, 20, "%lu", myPid);
opts.debug_file = fopen(fname, mode);
if (opts.debug_file == NULL) {
fprintf(stderr, "Cannot open debug file \"%s\"\n", fname);
exit(2);
}
free(fname);
}
static void
MainParseArgDebug(const char *argvalue)
{
const char *modules;
DebugFlags debug = opts.debug;
for (modules = argvalue; *modules != '\0'; modules++) {
switch (*modules) {
case '0': /* undocumented, only intended for tests */
memset(&debug, 0, sizeof(debug));
break;
case 'A':
memset(&debug, ~0, sizeof(debug));
break;
case 'a':
debug.DEBUG_ARCH = true;
break;
case 'C':
debug.DEBUG_CWD = true;
break;
case 'c':
debug.DEBUG_COND = true;
break;
case 'd':
debug.DEBUG_DIR = true;
break;
case 'e':
debug.DEBUG_ERROR = true;
break;
case 'f':
debug.DEBUG_FOR = true;
break;
case 'g':
if (modules[1] == '1') {
debug.DEBUG_GRAPH1 = true;
modules++;
} else if (modules[1] == '2') {
debug.DEBUG_GRAPH2 = true;
modules++;
} else if (modules[1] == '3') {
debug.DEBUG_GRAPH3 = true;
modules++;
}
break;
case 'h':
debug.DEBUG_HASH = true;
break;
case 'j':
debug.DEBUG_JOB = true;
break;
case 'L':
opts.strict = true;
break;
case 'l':
debug.DEBUG_LOUD = true;
break;
case 'M':
debug.DEBUG_META = true;
break;
case 'm':
debug.DEBUG_MAKE = true;
break;
case 'n':
debug.DEBUG_SCRIPT = true;
break;
case 'p':
debug.DEBUG_PARSE = true;
break;
case 's':
debug.DEBUG_SUFF = true;
break;
case 't':
debug.DEBUG_TARG = true;
break;
case 'V':
opts.debugVflag = true;
break;
case 'v':
debug.DEBUG_VAR = true;
break;
case 'x':
debug.DEBUG_SHELL = true;
break;
case 'F':
MainParseArgDebugFile(modules + 1);
goto finish;
default:
(void)fprintf(stderr,
"%s: illegal argument to d option -- %c\n",
progname, *modules);
usage();
}
}
finish:
opts.debug = debug;
setvbuf(opts.debug_file, NULL, _IONBF, 0);
if (opts.debug_file != stdout)
setvbuf(stdout, NULL, _IONBF, 0);
}
/* Is path relative or does it contain any relative component "." or ".."? */
static bool
IsRelativePath(const char *path)
{
char *p, *s;
if (!isAbs(path))
return true;
/* Make life easier by replacing all backslashes. */
p = s = bmake_strdup(path);
replaceSlash(p);
for (; (p = strstr(p, "/.")) != NULL;) {
p += 2;
if (*p == '.')
p++;
if (*p == '/' || *p == '\0') {
free(s);
return true;
}
}
free(s);
return false;
}
static void
MainParseArgChdir(const char *argvalue)
{
struct stat sa, sb;
if (chdir(argvalue) == -1) {
(void)fprintf(stderr, "%s: chdir %s: %s\n",
progname, argvalue, strerror(errno));
exit(2); /* Not 1 so -q can distinguish error */
}
if (getcwd(curdir, MAXPATHLEN) == NULL) {
(void)fprintf(stderr, "%s: %s.\n", progname, strerror(errno));
exit(2);
}
if (!IsRelativePath(argvalue) &&
stat(argvalue, &sa) != -1 &&
stat(curdir, &sb) != -1 &&
sa.st_ino == sb.st_ino &&
sa.st_dev == sb.st_dev)
snprintf(curdir, MAXPATHLEN, "%s", argvalue);
}
static void
MainParseArgJobsInternal(const char *argvalue)
{
char end;
if (sscanf(argvalue, "%p,%p%c", &jp_0, &jp_1, &end) != 2) {
(void)fprintf(stderr,
"%s: internal error -- J option malformed (%s)\n",
progname, argvalue);
usage();
}
Global_Append(MAKEFLAGS, "-J");
Global_Append(MAKEFLAGS, argvalue);
}
static void
MainParseArgJobs(const char *arg)
{
const char *p;
char *end;
char v[12];
forceJobs = true;
opts.maxJobs = (int)strtol(arg, &end, 0);
p = end;
if (*p != '\0') {
double d;
if (*p == 'C')
d = (opts.maxJobs > 0) ? opts.maxJobs : 1;
else if (*p == '.') {
d = strtod(arg, &end);
p = end;
} else
d = 0.0;
if (d > 0.0) {
p = "";
opts.maxJobs = numCpus;
opts.maxJobs = (int)(d * (double)opts.maxJobs);
}
}
if (*p != '\0' || opts.maxJobs < 1) {
(void)fprintf(stderr,
"%s: argument '%s' to option '-j' "
"must be a positive number\n",
progname, arg);
exit(2); /* Not 1 so -q can distinguish error */
}
snprintf(v, sizeof(v), "%d", opts.maxJobs);
Global_Append(MAKEFLAGS, "-j");
Global_Append(MAKEFLAGS, v);
Global_Set(".MAKE.JOBS", v);
maxJobTokens = opts.maxJobs;
}
static void
MainParseArgSysInc(const char *argvalue)
{
if (strncmp(argvalue, ".../", 4) == 0) {
char *found_path = Dir_FindHereOrAbove(curdir, argvalue + 4);
if (found_path == NULL)
return;
(void)SearchPath_Add(sysIncPath, found_path);
free(found_path);
} else {
(void)SearchPath_Add(sysIncPath, argvalue);
}
Global_Append(MAKEFLAGS, "-m");
Global_Append(MAKEFLAGS, argvalue);
Dir_SetSYSPATH();
}
static bool
MainParseOption(char c, const char *argvalue)
{
switch (c) {
case '\0':
break;
case 'B':
opts.compatMake = true;
Global_Append(MAKEFLAGS, "-B");
Global_Set(".MAKE.MODE", "compat");
break;
case 'C':
MainParseArgChdir(argvalue);
break;
case 'D':
if (argvalue[0] == '\0')
return false;
Var_SetExpand(SCOPE_GLOBAL, argvalue, "1");
Global_Append(MAKEFLAGS, "-D");
Global_Append(MAKEFLAGS, argvalue);
break;
case 'I':
SearchPath_Add(parseIncPath, argvalue);
Global_Append(MAKEFLAGS, "-I");
Global_Append(MAKEFLAGS, argvalue);
break;
case 'J':
MainParseArgJobsInternal(argvalue);
break;
case 'N':
opts.noExecute = true;
opts.noRecursiveExecute = true;
Global_Append(MAKEFLAGS, "-N");
break;
case 'S':
opts.keepgoing = false;
Global_Append(MAKEFLAGS, "-S");
break;
case 'T':
tracefile = bmake_strdup(argvalue);
Global_Append(MAKEFLAGS, "-T");
Global_Append(MAKEFLAGS, argvalue);
break;
case 'V':
case 'v':
opts.printVars = c == 'v' ? PVM_EXPANDED : PVM_UNEXPANDED;
Lst_Append(&opts.variables, bmake_strdup(argvalue));
/* XXX: Why always -V? */
Global_Append(MAKEFLAGS, "-V");
Global_Append(MAKEFLAGS, argvalue);
break;
case 'W':
opts.parseWarnFatal = true;
/* XXX: why no Global_Append? */
break;
case 'X':
opts.varNoExportEnv = true;
Global_Append(MAKEFLAGS, "-X");
break;
case 'd':
/* If '-d-opts' don't pass to children */
if (argvalue[0] == '-')
argvalue++;
else {
Global_Append(MAKEFLAGS, "-d");
Global_Append(MAKEFLAGS, argvalue);
}
MainParseArgDebug(argvalue);
break;
case 'e':
opts.checkEnvFirst = true;
Global_Append(MAKEFLAGS, "-e");
break;
case 'f':
Lst_Append(&opts.makefiles, bmake_strdup(argvalue));
break;
case 'i':
opts.ignoreErrors = true;
Global_Append(MAKEFLAGS, "-i");
break;
case 'j':
MainParseArgJobs(argvalue);
break;
case 'k':
opts.keepgoing = true;
Global_Append(MAKEFLAGS, "-k");
break;
case 'm':
MainParseArgSysInc(argvalue);
/* XXX: why no Var_Append? */
break;
case 'n':
opts.noExecute = true;
Global_Append(MAKEFLAGS, "-n");
break;
case 'q':
opts.query = true;
/* Kind of nonsensical, wot? */
Global_Append(MAKEFLAGS, "-q");
break;
case 'r':
opts.noBuiltins = true;
Global_Append(MAKEFLAGS, "-r");
break;
case 's':
opts.silent = true;
Global_Append(MAKEFLAGS, "-s");
break;
case 't':
opts.touch = true;
Global_Append(MAKEFLAGS, "-t");
break;
case 'w':
opts.enterFlag = true;
Global_Append(MAKEFLAGS, "-w");
break;
default:
usage();
}
return true;
}
/*
* Parse the given arguments. Called from main() and from
* Main_ParseArgLine() when the .MAKEFLAGS target is used.
*
* The arguments must be treated as read-only and will be freed after the
* call.
*
* XXX: Deal with command line overriding .MAKEFLAGS in makefile
*/
static void
MainParseArgs(int argc, char **argv)
{
char c;
int arginc;
char *argvalue;
char *optscan;
bool inOption, dashDash = false;
const char *optspecs = "BC:D:I:J:NST:V:WXd:ef:ij:km:nqrstv:w";
/* Can't actually use getopt(3) because rescanning is not portable */
rearg:
inOption = false;
optscan = NULL;
while (argc > 1) {
const char *optspec;
if (!inOption)
optscan = argv[1];
c = *optscan++;
arginc = 0;
if (inOption) {
if (c == '\0') {
argv++;
argc--;
inOption = false;
continue;
}
} else {
if (c != '-' && c != '/' || dashDash)
break;
inOption = true;
c = *optscan++;
}
/* '-' or '/' found at some earlier point */
optspec = strchr(optspecs, c);
if (c != '\0' && optspec != NULL && optspec[1] == ':') {
/*
* -<something> found, and <something> should have an
* argument
*/
inOption = false;
arginc = 1;
argvalue = optscan;
if (*argvalue == '\0') {
if (argc < 3)
goto noarg;
argvalue = argv[2];
arginc = 2;
}
} else {
argvalue = NULL;
}
switch (c) {
case '\0':
arginc = 1;
inOption = false;
break;
case '-':
dashDash = true;
break;
default:
if (!MainParseOption(c, argvalue))
goto noarg;
}
argv += arginc;
argc -= arginc;
}
/*
* See if the rest of the arguments are variable assignments and
* perform them if so. Else take them to be targets and stuff them
* on the end of the "create" list.
*/
for (; argc > 1; argv++, argc--) {
if (!Parse_VarAssign(argv[1], false, SCOPE_CMDLINE)) {
if (argv[1][0] == '\0')
Punt("illegal (null) argument.");
if (argv[1][0] == '-' && !dashDash)
goto rearg;
Lst_Append(&opts.create, bmake_strdup(argv[1]));
}
}
return;
noarg:
(void)fprintf(stderr, "%s: option requires an argument -- %c\n",
progname, c);
usage();
}
/*
* Break a line of arguments into words and parse them.
*
* Used when a .MFLAGS or .MAKEFLAGS target is encountered during parsing and
* by main() when reading the MAKEFLAGS environment variable.
*/
void
Main_ParseArgLine(const char *line)
{
Words words;
char *buf;
const char *p;
if (line == NULL)
return;
for (p = line; *p == ' '; p++)
continue;
if (p[0] == '\0')
return;
{
FStr argv0 = Var_Value(SCOPE_GLOBAL, ".MAKE");
size_t len = strlen(argv0.str) + 1;
char *tmp = _alloca(len);
memcpy(tmp, argv0.str, len);
/*
* Str_Words treats backslashes as escape sequences.
* Replace all instances of '\' with '/'.
*/
replaceSlash(tmp);
buf = str_concat3(tmp, " ", p);
FStr_Done(&argv0);
}
words = Str_Words(buf, true);
if (words.words == NULL) {
Error("Unterminated quoted string [%s]", buf);
free(buf);
return;
}
free(buf);
MainParseArgs((int)words.len, words.words);
Words_Free(words);
}
bool
Main_SetObjdir(bool writable, MAKE_ATTR_PRINTFLIKE const char *fmt, ...)
{
struct stat sb;
char *path;
char buf[MAXPATHLEN + 1];
char buf2[MAXPATHLEN + 1];
va_list ap;
va_start(ap, fmt);
vsnprintf(path = buf, MAXPATHLEN, fmt, ap);
va_end(ap);
if (!isAbs(path)) {
if (snprintf(buf2, MAXPATHLEN, "%s\\%s", curdir, path) <= MAXPATHLEN)
path = buf2;
else
return false;
}
/* look for the directory and try to chdir there */
if (stat(path, &sb) != 0 || !S_ISDIR(sb.st_mode))
return false;
if ((writable && access(path, 2) != 0) || chdir(path) != 0) {
(void)fprintf(stderr, "%s: warning: %s: %s.\n",
progname, path, strerror(errno));
/* Allow debugging how we got here - not always obvious */
if (GetBooleanExpr("${MAKE_DEBUG_OBJDIR_CHECK_WRITABLE}",
false))
PrintOnError(NULL, "");
return false;
}
snprintf(objdir, sizeof objdir, "%s", path);
Global_Set(".OBJDIR", objdir);
Dir_InitDot();
purge_relative_cached_realpaths();
if (opts.enterFlag && strcmp(objdir, curdir) != 0)
enterFlagObj = true;
return true;
}
static bool
SetVarObjdir(bool writable, const char *var, const char *suffix)
{
FStr path = Var_Value(SCOPE_CMDLINE, var);
if (path.str == NULL || path.str[0] == '\0') {
FStr_Done(&path);
return false;
}
Var_Expand(&path, SCOPE_GLOBAL, VARE_EVAL);
(void)Main_SetObjdir(writable, "%s%s", path.str, suffix);
FStr_Done(&path);
return true;
}
/*
* Splits str into words (in-place, modifying it), adding them to the list.
* The string must be kept alive as long as the list.
*/
void
AppendWords(StringList *lp, char *str)
{
char *p;
const char *sep = " \t";
for (p = strtok(str, sep); p != NULL; p = strtok(NULL, sep))
Lst_Append(lp, p);
}
/* Allow makefiles some control over the mode we run in. */
static void
MakeMode(void)
{
char *mode = Var_Subst("${.MAKE.MODE:tl}", SCOPE_GLOBAL, VARE_EVAL);
/* TODO: handle errors */
if (mode[0] != '\0') {
if (strstr(mode, "compat") != NULL) {
opts.compatMake = true;
forceJobs = false;
}
#if USE_META
if (strstr(mode, "meta") != NULL)
meta_mode_init(mode);
#endif
if (strstr(mode, "randomize-targets") != NULL)
opts.randomizeTargets = true;
}
free(mode);
}
static void
PrintVar(const char *varname, bool expandVars)
{
if (strchr(varname, '$') != NULL) {
char *evalue = Var_Subst(varname, SCOPE_GLOBAL, VARE_EVAL);
/* TODO: handle errors */
printf("%s\n", evalue);
free(evalue);
} else if (expandVars) {
char *expr = str_concat3("${", varname, "}");
char *evalue = Var_Subst(expr, SCOPE_GLOBAL, VARE_EVAL);
/* TODO: handle errors */
free(expr);
printf("%s\n", evalue);
free(evalue);
} else {
FStr value = Var_Value(SCOPE_GLOBAL, varname);
printf("%s\n", value.str != NULL ? value.str : "");
FStr_Done(&value);
}
}
/*
* Return a bool based on a variable.
*
* If the knob is not set, return the fallback.
* If set, anything that looks or smells like "No", "False", "Off", "0", etc.
* is false, otherwise true.
*/
bool
GetBooleanExpr(const char *expr, bool fallback)
{
char *value;
bool res;
value = Var_Subst(expr, SCOPE_GLOBAL, VARE_EVAL);
/* TODO: handle errors */
res = ParseBoolean(value, fallback);
free(value);
return res;
}
static void
doPrintVars(void)
{
StringListNode *ln;
bool expandVars;
if (opts.printVars == PVM_EXPANDED)
expandVars = true;
else if (opts.debugVflag)
expandVars = false;
else
expandVars = GetBooleanExpr("${.MAKE.EXPAND_VARIABLES}",
false);
for (ln = opts.variables.first; ln != NULL; ln = ln->next) {
const char *varname = ln->datum;
PrintVar(varname, expandVars);
}
}
static bool
runTargets(void)
{
GNodeList targs = LST_INIT; /* target nodes to create */
bool outOfDate; /* false if all targets up to date */
/*
* Have now read the entire graph and need to make a list of
* targets to create. If none was given on the command line,
* we consult the parsing module to find the main target(s)
* to create.
*/
if (Lst_IsEmpty(&opts.create))
Parse_MainName(&targs);
else
Targ_FindList(&targs, &opts.create);
if (!opts.compatMake) {
/*
* Initialize job module before traversing the graph
* now that any .BEGIN and .END targets have been read.
* This is done only if the -q flag wasn't given
* (to prevent the .BEGIN from being executed should
* it exist).
*/
if (!opts.query) {
Job_Init();
jobsRunning = true;
}
/* Traverse the graph, checking on all the targets */
outOfDate = Make_Run(&targs);
} else {
Compat_MakeAll(&targs);
outOfDate = false;
}
Lst_Done(&targs); /* Don't free the targets themselves. */
return outOfDate;
}
/*
* Set up the .TARGETS variable to contain the list of targets to be created.
* If none specified, make the variable empty for now, the parser will fill
* in the default or .MAIN target later.
*/
static void
InitVarTargets(void)
{
StringListNode *ln;
if (Lst_IsEmpty(&opts.create)) {
Global_Set(".TARGETS", "");
return;
}
for (ln = opts.create.first; ln != NULL; ln = ln->next) {
const char *name = ln->datum;
Global_Append(".TARGETS", name);
}
}
static void
InitRandom(void)
{
struct _timeb t;
_ftime(&t);
srand((unsigned int)(t.time + t.millitm));
}
static const char*
InitVarMachine(void)
{
SYSTEM_INFO info;
GetSystemInfo(&info);
numCpus = info.dwNumberOfProcessors;
return MACHINE;
}
/*
* Find the .OBJDIR. If MAKEOBJDIRPREFIX, or failing that, MAKEOBJDIR is set
* in the environment, try only that value and fall back to .CURDIR if it
* does not exist.
*
* Otherwise, try _PATH_OBJDIR.MACHINE-MACHINE_ARCH, _PATH_OBJDIR.MACHINE,
* and finally _PATH_OBJDIRPREFIX`cwd`, in that order. If none of these
* paths exist, just use .CURDIR.
*/
static void
InitObjdir(const char *machine, const char *machine_arch)
{
bool writable;
Dir_InitCur(curdir);
writable = GetBooleanExpr("${MAKE_OBJDIR_CHECK_WRITABLE}", true);
(void)Main_SetObjdir(false, "%s", curdir);
if (!SetVarObjdir(writable, "MAKEOBJDIRPREFIX", curdir) &&
!SetVarObjdir(writable, "MAKEOBJDIR", "") &&
!Main_SetObjdir(writable, "%s.%s-%s", _PATH_OBJDIR, machine, machine_arch) &&
!Main_SetObjdir(writable, "%s.%s", _PATH_OBJDIR, machine))
(void)Main_SetObjdir(writable, "%s", _PATH_OBJDIR);
}
static void
CmdOpts_Init(void)
{
opts.compatMake = false;
memset(&opts.debug, 0, sizeof(opts.debug));
/* opts.debug_file has already been initialized earlier */
opts.strict = false;
opts.debugVflag = false;
opts.checkEnvFirst = false;
Lst_Init(&opts.makefiles);
opts.ignoreErrors = false; /* Pay attention to non-zero returns */
opts.maxJobs = 1;
opts.keepgoing = false; /* Stop on error */
opts.noRecursiveExecute = false; /* Execute all .MAKE targets */
opts.noExecute = false; /* Execute all commands */
opts.query = false;
opts.noBuiltins = false; /* Read the built-in rules */
opts.silent = false; /* Print commands as executed */
opts.touch = false;
opts.printVars = PVM_NONE;