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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
|
/* Copyright (C) 1988, 1989, 1990, 1991 Free Software Foundation, Inc.
This file is part of GNU Make.
GNU Make is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Make is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Make; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "make.h"
#include "commands.h"
#include "dep.h"
#include "file.h"
#include "variable.h"
#include "job.h"
#include "getopt.h"
extern char *version_string;
extern void print_variable_data_base ();
extern void print_dir_data_base ();
extern void print_rule_data_base ();
extern void print_file_data_base ();
extern void print_vpath_data_base ();
#ifndef HAVE_UNISTD_H
extern int chdir ();
#endif
#ifndef STDC_HEADERS
#ifndef sun /* Sun has an incorrect decl in a header. */
extern void exit ();
#endif
extern double atof ();
#endif
extern char *mktemp ();
static void log_working_directory ();
static void print_data_base (), print_version ();
static void decode_switches (), decode_env_switches ();
static void define_makeflags ();
/* The structure that describes an accepted command switch. */
struct command_switch
{
char c; /* The switch character. */
enum /* Type of the value. */
{
flag, /* Turn int flag on. */
flag_off, /* Turn int flag off. */
string, /* One string per switch. */
positive_int, /* A positive integer. */
floating, /* A floating-point number (double). */
ignore /* Ignored. */
} type;
char *value_ptr; /* Pointer to the value-holding variable. */
unsigned int env:1; /* Can come from MAKEFLAGS. */
unsigned int toenv:1; /* Should be put in MAKEFLAGS. */
unsigned int no_makefile:1; /* Don't propagate when remaking makefiles. */
char *noarg_value; /* Pointer to value used if no argument is given. */
char *default_value;/* Pointer to default value. */
char *long_name; /* Long option name. */
char *argdesc; /* Descriptive word for argument. */
char *description; /* Description for usage message. */
};
/* The structure used to hold the list of strings given
in command switches of a type that takes string arguments. */
struct stringlist
{
char **list; /* Nil-terminated list of strings. */
unsigned int idx; /* Index into above. */
unsigned int max; /* Number of pointers allocated. */
};
/* The recognized command switches. */
/* Nonzero means do not print commands to be executed (-s). */
int silent_flag;
/* Nonzero means just touch the files
that would appear to need remaking (-t) */
int touch_flag;
/* Nonzero means just print what commands would need to be executed,
don't actually execute them (-n). */
int just_print_flag;
/* Print debugging trace info (-d). */
int debug_flag = 0;
/* Environment variables override makefile definitions. */
int env_overrides = 0;
/* Nonzero means ignore status codes returned by commands
executed to remake files. Just treat them all as successful (-i). */
int ignore_errors_flag = 0;
/* Nonzero means don't remake anything, just print the data base
that results from reading the makefile (-p). */
int print_data_base_flag = 0;
/* Nonzero means don't remake anything; just return a nonzero status
if the specified targets are not up to date (-q). */
int question_flag = 0;
/* Nonzero means do not use any of the builtin rules (-r). */
int no_builtin_rules_flag = 0;
/* Nonzero means keep going even if remaking some file fails (-k). */
int keep_going_flag;
int default_keep_going_flag = 0;
/* Nonzero means print directory before starting and when done (-w). */
int print_directory_flag = 0;
/* Nonzero means ignore print_directory_flag and never print the directory.
This is necessary because print_directory_flag is set implicitly. */
int inhibit_print_directory_flag = 0;
/* Nonzero means print version information. */
int print_version_flag = 0;
/* List of makefiles given with -f switches. */
static struct stringlist *makefiles = 0;
/* Number of job slots (commands that can be run at once). */
unsigned int job_slots = 1;
unsigned int default_job_slots = 1;
/* Value of job_slots that means no limit. */
static unsigned int inf_jobs = 0;
/* Maximum load average at which multiple jobs will be run.
Negative values mean unlimited, while zero means limit to
zero load (which could be useful to start infinite jobs remotely
but one at a time locally). */
double max_load_average = -1.0;
double default_load_average = -1.0;
/* List of directories given with -C switches. */
static struct stringlist *directories = 0;
/* List of include directories given with -I switches. */
static struct stringlist *include_directories = 0;
/* List of files given with -o switches. */
static struct stringlist *old_files = 0;
/* List of files given with -W switches. */
static struct stringlist *new_files = 0;
/* If nonzero, we should just print usage and exit. */
static int print_usage_flag = 0;
/* If nonzero, we should print a warning message
for each reference to an undefined variable. */
int warn_undefined_variables_flag;
/* The table of command switches. */
static const struct command_switch switches[] =
{
{ 'b', ignore, 0, 0, 0, 0, 0, 0,
0, 0,
"Ignored for compatibility" },
{ 'C', string, (char *) &directories, 0, 0, 0, 0, 0,
"directory", "DIRECTORY",
"Change to DIRECTORY before doing anything" },
{ 'd', flag, (char *) &debug_flag, 1, 1, 0, 0, 0,
"debug", 0,
"Print lots of debugging information" },
{ 'e', flag, (char *) &env_overrides, 1, 1, 0, 0, 0,
"environment-overrides", 0,
"Environment variables override makefiles" },
{ 'f', string, (char *) &makefiles, 0, 0, 0, 0, 0,
"file", "FILE",
"Read FILE as a makefile" },
{ 'h', flag, (char *) &print_usage_flag, 0, 0, 0, 0, 0,
"help", 0,
"Print this message and exit" },
{ 'i', flag, (char *) &ignore_errors_flag, 1, 1, 0, 0, 0,
"ignore-errors", 0,
"Ignore errors from commands" },
{ 'I', string, (char *) &include_directories, 1, 1, 0, 0, 0,
"include-dir", "DIRECTORY",
"Search DIRECTORY for included makefiles" },
{ 'j', positive_int, (char *) &job_slots, 1, 1, 0,
(char *) &inf_jobs, (char *) &default_job_slots,
"jobs", "N",
"Allow N jobs at once; infinite jobs with no arg" },
{ 'k', flag, (char *) &keep_going_flag, 1, 1, 0,
0, (char *) &default_keep_going_flag,
"keep-going", 0,
"Keep going when some targets can't be made" },
{ 'l', floating, (char *) &max_load_average, 1, 1, 0,
(char *) &default_load_average, (char *) &default_load_average,
"load-average", "N",
"Don't start multiple jobs unless load is below N" },
{ 'm', ignore, 0, 0, 0, 0, 0, 0,
0, 0,
"-b" },
{ 'n', flag, (char *) &just_print_flag, 1, 1, 1, 0, 0,
"just-print", 0,
"Don't actually run any commands; just print them" },
{ 'o', string, (char *) &old_files, 0, 0, 0, 0, 0,
"old-file", "FILE",
"Consider FILE to be very old and don't remake it" },
{ 'p', flag, (char *) &print_data_base_flag, 1, 1, 0, 0, 0,
"print-data-base", 0,
"Print make's internal database" },
{ 'q', flag, (char *) &question_flag, 1, 1, 1, 0, 0,
"question", 0,
"Run no commands; exit status says if up to date" },
{ 'r', flag, (char *) &no_builtin_rules_flag, 1, 1, 0, 0, 0,
"no-builtin-rules", 0,
"Disable the built-in implicit rules" },
{ 's', flag, (char *) &silent_flag, 1, 1, 0, 0, 0,
"silent", 0,
"Don't echo commands" },
{ 'S', flag_off, (char *) &keep_going_flag, 1, 1, 0,
0, (char *) &default_keep_going_flag,
"no-keep-going", 0,
"Turns off -k" },
{ 't', flag, (char *) &touch_flag, 1, 1, 1, 0, 0,
"touch", 0,
"Touch targets instead of remaking them" },
{ 'v', flag, (char *) &print_version_flag, 1, 1, 0, 0, 0,
"version", 0,
"Print the version number of make and exit" },
{ 'w', flag, (char *) &print_directory_flag, 1, 1, 0, 0, 0,
"print-directory", 0,
"Print the current directory" },
{ 1, flag, (char *) &inhibit_print_directory_flag, 1, 1, 0, 0, 0,
"no-print-directory", 0,
"Turn off -w, even if it was turned on implicitly" },
{ 'W', string, (char *) &new_files, 0, 0, 0, 0, 0,
"what-if", "FILE",
"Consider FILE to be infinitely new" },
{ 2, flag, (char *) &warn_undefined_variables_flag, 1, 1, 0, 0, 0,
"warn-undefined-variables", 0,
"Warn when an undefined variable is referenced" },
{ '\0', }
};
/* Secondary long names for options. */
static struct option long_option_aliases[] =
{
{ "quiet", no_argument, 0, 's' },
{ "stop", no_argument, 0, 'S' },
{ "new-file", required_argument, 0, 'W' },
{ "assume-new", required_argument, 0, 'W' },
{ "assume-old", required_argument, 0, 'o' },
{ "max-load", optional_argument, 0, 'l' },
{ "dry-run", no_argument, 0, 'n' },
{ "recon", no_argument, 0, 'n' },
{ "makefile", required_argument, 0, 'f' },
};
/* The usage message prints the descriptions of options starting in
this column. Make sure it leaves enough room for the longest
description to fit in less than 80 characters. */
#define DESCRIPTION_COLUMN 30
/* List of non-switch arguments. */
struct stringlist *other_args = 0;
/* The name we were invoked with. */
char *program;
/* Our current directory after processing all -C options. */
char *starting_directory;
/* Value of the MAKELEVEL variable at startup (or 0). */
unsigned int makelevel;
/* First file defined in the makefile whose name does not
start with `.'. This is the default to remake if the
command line does not specify. */
struct file *default_goal_file;
/* Pointer to structure for the file .DEFAULT
whose commands are used for any file that has none of its own.
This is zero if the makefiles do not define .DEFAULT. */
struct file *default_file;
/* Mask of signals that are being caught with fatal_error_signal. */
#ifdef POSIX
sigset_t fatal_signal_set;
#else
#ifdef HAVE_SIGSETMASK
int fatal_signal_mask;
#endif
#endif
static struct file *
enter_command_line_file (name)
char *name;
{
if (name[0] == '~')
{
char *expanded = tilde_expand (name);
if (expanded != 0)
name = expanded; /* Memory leak; I don't care. */
}
/* This is also done in parse_file_seq, so this is redundant
for names read from makefiles. It is here for names passed
on the command line. */
while (name[0] == '.' && name[1] == '/' && name[2] != '\0')
{
name += 2;
while (*name == '/')
/* Skip following slashes: ".//foo" is "foo", not "/foo". */
++name;
}
if (*name == '\0')
{
/* It was all slashes! Move back to the dot and truncate
it after the first slash, so it becomes just "./". */
do
--name;
while (name[0] != '.');
name[2] = '\0';
}
return enter_file (savestring (name, strlen (name)));
}
int
main (argc, argv, envp)
int argc;
char **argv;
char **envp;
{
extern void init_dir ();
extern RETSIGTYPE fatal_error_signal (), child_handler ();
register struct file *f;
register unsigned int i;
register char *cmd_defs;
register unsigned int cmd_defs_len, cmd_defs_idx;
char **p;
time_t now;
struct dep *goals = 0;
register struct dep *lastgoal;
struct dep *read_makefiles;
PATH_VAR (current_directory);
char *directory_before_chdir;
default_goal_file = 0;
reading_filename = 0;
reading_lineno_ptr = 0;
#ifndef HAVE_SYS_SIGLIST
signame_init ();
#endif
#ifdef POSIX
sigemptyset (&fatal_signal_set);
#define ADD_SIG(sig) sigaddset (&fatal_signal_set, sig)
#else
#ifdef HAVE_SIGSETMASK
fatal_signal_mask = 0;
#define ADD_SIG(sig) fatal_signal_mask |= sigmask (sig)
#else
#define ADD_SIG(sig)
#endif
#endif
#define FATAL_SIG(sig) \
if (signal ((sig), fatal_error_signal) == SIG_IGN) \
(void) signal ((sig), SIG_IGN); \
else \
ADD_SIG (sig);
FATAL_SIG (SIGHUP);
FATAL_SIG (SIGQUIT);
FATAL_SIG (SIGINT);
FATAL_SIG (SIGTERM);
#ifdef SIGDANGER
FATAL_SIG (SIGDANGER);
#endif
#ifdef SIGXCPU
FATAL_SIG (SIGXCPU);
#endif
#ifdef SIGXFSZ
FATAL_SIG (SIGXFSZ);
#endif
#undef FATAL_SIG
/* Make sure stdout is line-buffered. */
#ifdef HAVE_SETLINEBUF
setlinebuf (stdout);
#else
#ifndef SETVBUF_REVERSED
setvbuf (stdout, (char *) 0, _IOLBF, BUFSIZ);
#else /* setvbuf not reversed. */
/* Some buggy systems lose if we pass 0 instead of allocating ourselves. */
setvbuf (stdout, _IOLBF, xmalloc (BUFSIZ), BUFSIZ);
#endif /* setvbuf reversed. */
#endif /* setlinebuf missing. */
/* Initialize the directory hashing code. */
init_dir ();
/* Set up to access user data (files). */
user_access ();
/* Figure out where this program lives. */
if (argv[0] == 0)
argv[0] = "";
if (argv[0][0] == '\0')
program = "make";
else
{
program = rindex (argv[0], '/');
if (program == 0)
program = argv[0];
else
++program;
}
/* Figure out where we are. */
if (getcwd (current_directory, GET_PATH_MAX) == 0)
{
#ifdef HAVE_GETCWD
perror_with_name ("getcwd: ", "");
#else
error ("getwd: %s", current_directory);
#endif
current_directory[0] = '\0';
directory_before_chdir = 0;
}
else
directory_before_chdir = savestring (current_directory,
strlen (current_directory));
/* Read in variables from the environment. It is important that this be
done before `MAKE' and `MAKEOVERRIDES' are figured out so their
definitions will not be ones from the environment. */
for (i = 0; envp[i] != 0; ++i)
{
register char *ep = envp[i];
while (*ep++ != '=')
;
(void) define_variable (envp[i], ep - envp[i] - 1, ep, o_env, 1);
}
/* Decode the switches. */
decode_env_switches ("MAKEFLAGS", 9);
#if 0
/* People write things like:
MFLAGS="CC=gcc -pipe" "CFLAGS=-g"
and we set the -p, -i and -e switches. Doesn't seem quite right. */
decode_env_switches ("MFLAGS", 6);
#endif
decode_switches (argc, argv, 0);
/* Print version information. */
if (print_version_flag || print_data_base_flag || debug_flag)
print_version ();
/* `make --version' is supposed to just print the version and exit. */
if (print_version_flag)
die (0);
/* Search for command line arguments that define variables,
and do the definitions. Also save up the text of these
arguments in CMD_DEFS so we can put them into the values
of $(MAKEOVERRIDES) and $(MAKE). */
cmd_defs_len = 200;
cmd_defs = (char *) xmalloc (cmd_defs_len);
cmd_defs_idx = 0;
for (i = 1; other_args->list[i] != 0; ++i)
{
if (other_args->list[i][0] == '\0')
/* Ignore empty arguments, so they can't kill enter_file. */
continue;
/* Try a variable definition. */
if (try_variable_definition ((char *) 0, 0,
other_args->list[i], o_command))
{
/* It succeeded. The variable is already defined.
Backslash-quotify it and append it to CMD_DEFS, then clobber it
to 0 in the list so that it won't be taken for a goal target. */
register char *p = other_args->list[i];
unsigned int l = strlen (p);
if (cmd_defs_idx + (l * 2) + 1 > cmd_defs_len)
{
if (l * 2 > cmd_defs_len)
cmd_defs_len += l * 2;
else
cmd_defs_len *= 2;
cmd_defs = (char *) xrealloc (cmd_defs, cmd_defs_len);
}
while (*p != '\0')
{
if (index (";'\"*?[]$<>(){}|&~`\\ \t\r\n\f\v", *p) != 0)
cmd_defs[cmd_defs_idx++] = '\\';
cmd_defs[cmd_defs_idx++] = *p++;
}
cmd_defs[cmd_defs_idx++] = ' ';
}
else
{
/* It was not a variable definition, so it is a target to be made.
Enter it as a file and add it to the dep chain of goals. */
f = enter_command_line_file (other_args->list[i]);
f->cmd_target = 1;
if (goals == 0)
{
goals = (struct dep *) xmalloc (sizeof (struct dep));
lastgoal = goals;
}
else
{
lastgoal->next = (struct dep *) xmalloc (sizeof (struct dep));
lastgoal = lastgoal->next;
}
lastgoal->name = 0;
lastgoal->file = f;
}
}
if (cmd_defs_idx > 0)
{
cmd_defs[cmd_defs_idx - 1] = '\0';
(void) define_variable ("MAKEOVERRIDES", 13, cmd_defs, o_default, 0);
}
free (cmd_defs);
/* Set the "MAKE_COMMAND" variable to the name we were invoked with.
(If it is a relative pathname with a slash, prepend our directory name
so the result will run the same program regardless of the current dir.
If it is a name with no slash, we can only hope that PATH did not
find it in the current directory.) */
if (current_directory[0] != '\0'
&& argv[0] != 0 && argv[0][0] != '/' && index (argv[0], '/') != 0)
argv[0] = concat (current_directory, "/", argv[0]);
(void) define_variable ("MAKE_COMMAND", 12, argv[0], o_default, 0);
/* Append the command-line variable definitions gathered above
so sub-makes will get them as command-line definitions. */
(void) define_variable ("MAKE", 4,
"$(MAKE_COMMAND) $(MAKEOVERRIDES)", o_default, 1);
/* If there were -C flags, move ourselves about. */
if (directories != 0)
for (i = 0; directories->list[i] != 0; ++i)
{
char *dir = directories->list[i];
if (dir[0] == '~')
{
char *expanded = tilde_expand (dir);
if (expanded != 0)
dir = expanded;
}
if (chdir (dir) < 0)
pfatal_with_name (dir);
if (dir != directories->list[i])
free (dir);
}
/* Figure out the level of recursion. */
{
struct variable *v = lookup_variable ("MAKELEVEL", 9);
if (v != 0 && *v->value != '\0' && *v->value != '-')
makelevel = (unsigned int) atoi (v->value);
else
makelevel = 0;
}
/* Except under -s, always do -w in sub-makes and under -C. */
if (!silent_flag && (directories != 0 || makelevel > 0))
print_directory_flag = 1;
/* Let the user disable that with --no-print-directory. */
if (inhibit_print_directory_flag)
print_directory_flag = 0;
/* Construct the list of include directories to search. */
construct_include_path (include_directories == 0 ? (char **) 0
: include_directories->list);
/* Figure out where we are now, after chdir'ing. */
if (directories == 0)
/* We didn't move, so we're still in the same place. */
starting_directory = current_directory;
else
{
if (getcwd (current_directory, GET_PATH_MAX) == 0)
{
#ifdef HAVE_GETCWD
perror_with_name ("getcwd: ", "");
#else
error ("getwd: %s", current_directory);
#endif
starting_directory = 0;
}
else
starting_directory = current_directory;
}
/* Tell the user where he is. */
if (print_directory_flag)
log_working_directory (1);
/* Read any stdin makefiles into temporary files. */
if (makefiles != 0)
{
register unsigned int i;
for (i = 0; i < makefiles->idx; ++i)
if (makefiles->list[i][0] == '-' && makefiles->list[i][1] == '\0')
{
/* This makefile is standard input. Since we may re-exec
and thus re-read the makefiles, we read standard input
into a temporary file and read from that. */
static char name[] = "/tmp/GmXXXXXX";
FILE *outfile;
/* Make a unique filename. */
(void) mktemp (name);
outfile = fopen (name, "w");
if (outfile == 0)
pfatal_with_name ("fopen (temporary file)");
while (!feof (stdin))
{
char buf[2048];
int n = fread (buf, 1, sizeof(buf), stdin);
if (n > 0 && fwrite (buf, 1, n, outfile) != n)
pfatal_with_name ("fwrite (temporary file)");
}
/* Try to make sure we won't remake the temporary
file when we are re-exec'd. Kludge-o-matic! */
fprintf (outfile, "%s:;\n", name);
(void) fclose (outfile);
/* Replace the name that read_all_makefiles will
see with the name of the temporary file. */
{
char *temp;
/* SGI compiler requires alloca's result be assigned simply. */
temp = (char *) alloca (sizeof (name));
bcopy (name, temp, sizeof (name));
makefiles->list[i] = temp;
}
/* Make sure the temporary file will not be remade. */
f = enter_file (savestring (name, sizeof name - 1));
f->updated = 1;
f->update_status = 0;
f->command_state = cs_finished;
/* Let it be removed when we're done. */
f->intermediate = 1;
/* But don't mention it. */
f->dontcare = 1;
}
}
/* Set up to handle children dying. This must be done before
reading in the makefiles so that `shell' function calls will work. */
#ifdef SIGCHLD
(void) signal (SIGCHLD, child_handler);
#endif
#ifdef SIGCLD
(void) signal (SIGCLD, child_handler);
#endif
/* Define the initial list of suffixes for old-style rules. */
set_default_suffixes ();
/* Define some internal and special variables. */
define_automatic_variables ();
/* Set up the MAKEFLAGS and MFLAGS variables
so makefiles can look at them. */
define_makeflags (0, 0);
/* Define the default variables. */
define_default_variables ();
/* Read all the makefiles. */
default_file = enter_file (".DEFAULT");
read_makefiles
= read_all_makefiles (makefiles == 0 ? (char **) 0 : makefiles->list);
/* Decode switches again, in case the variables were set by the makefile. */
decode_env_switches ("MAKEFLAGS", 9);
decode_env_switches ("MFLAGS", 6);
/* Set up MAKEFLAGS and MFLAGS again, so they will be right. */
define_makeflags (1, 0);
ignore_errors_flag |= lookup_file (".IGNORE") != 0;
silent_flag |= lookup_file (".SILENT") != 0;
/* Make each `struct dep' point at the
`struct file' for the file depended on. */
snap_deps ();
/* Install the default implicit rules.
This used to be done before reading the makefiles.
But in that case, built-in pattern rules were in the chain
before user-defined ones, so they matched first. */
install_default_implicit_rules ();
/* Convert old-style suffix rules to pattern rules. */
convert_to_pattern ();
/* Compute implicit rule limits. */
count_implicit_rule_limits ();
/* Construct the listings of directories in VPATH lists. */
build_vpath_lists ();
/* Mark files given with -o flags as very old (00:00:01.00 Jan 1, 1970)
and as having been updated already, and files given with -W flags
as brand new (time-stamp of now). */
if (old_files != 0)
for (p = old_files->list; *p != 0; ++p)
{
f = enter_command_line_file (*p);
f->last_mtime = (time_t) 1;
f->updated = 1;
f->update_status = 0;
f->command_state = cs_finished;
}
if (new_files != 0)
{
now = time ((time_t *) 0);
for (p = new_files->list; *p != 0; ++p)
{
f = enter_command_line_file (*p);
f->last_mtime = now;
}
}
if (read_makefiles != 0)
{
/* Update any makefiles if necessary. */
time_t *makefile_mtimes = 0;
unsigned int mm_idx = 0;
if (debug_flag)
puts ("Updating makefiles....");
/* Remove any makefiles we don't want to try to update.
Also record the current modtimes so we can compare them later. */
{
register struct dep *d, *last;
last = 0;
d = read_makefiles;
while (d != 0)
{
register struct file *f = d->file;
if (f->double_colon)
do
{
if (f->deps == 0 && f->cmds != 0)
{
/* This makefile is a :: target with commands, but
no dependencies. So, it will always be remade.
This might well cause an infinite loop, so don't
try to remake it. (This will only happen if
your makefiles are written exceptionally
stupidly; but if you work for Athena, that's how
you write your makefiles.) */
if (debug_flag)
printf ("Makefile `%s' might loop; not remaking it.\n",
f->name);
if (last == 0)
read_makefiles = d->next;
else
last->next = d->next;
/* Free the storage. */
free ((char *) d);
d = last == 0 ? 0 : last->next;
break;
}
f = f->prev;
}
while (f != NULL);
if (f == NULL || !f->double_colon)
{
if (makefile_mtimes == 0)
makefile_mtimes = (time_t *) xmalloc (sizeof (time_t));
else
makefile_mtimes = (time_t *)
xrealloc ((char *) makefile_mtimes,
(mm_idx + 1) * sizeof (time_t));
makefile_mtimes[mm_idx++] = file_mtime_no_search (d->file);
last = d;
d = d->next;
}
}
}
/* Set up `MAKEFLAGS' specially while remaking makefiles. */
define_makeflags (1, 1);
switch (update_goal_chain (read_makefiles, 1))
{
default:
abort ();
case -1:
/* Did nothing. */
break;
case 1:
/* Failed to update. Figure out if we care. */
{
/* Nonzero if any makefile was successfully remade. */
int any_remade = 0;
/* Nonzero if any makefile we care about failed
in updating or could not be found at all. */
int any_failed = 0;
register unsigned int i;
for (i = 0; read_makefiles != 0; ++i)
{
struct dep *d = read_makefiles;
read_makefiles = d->next;
if (d->file->updated)
{
/* This makefile was updated. */
if (d->file->update_status == 0)
{
/* It was successfully updated. */
any_remade |= (file_mtime_no_search (d->file)
!= makefile_mtimes[i]);
}
else if (! (d->changed & RM_DONTCARE))
{
time_t mtime;
/* The update failed and this makefile was not
from the MAKEFILES variable, so we care. */
error ("Failed to remake makefile `%s'.",
d->file->name);
mtime = file_mtime_no_search (d->file);
any_remade |= (mtime != (time_t) -1
&& mtime != makefile_mtimes[i]);
}
}
else
/* This makefile was not found at all. */
if (! (d->changed & RM_DONTCARE))
{
/* This is a makefile we care about. See how much. */
if (d->changed & RM_INCLUDED)
/* An included makefile. We don't need
to die, but we do want to complain. */
error ("Included makefile `%s' was not found.",
dep_name (d));
else
{
/* A normal makefile. We must die later. */
error ("Makefile `%s' was not found", dep_name (d));
any_failed = 1;
}
}
free ((char *) d);
}
if (any_remade)
goto re_exec;
else if (any_failed)
die (1);
else
break;
}
case 0:
re_exec:
/* Updated successfully. Re-exec ourselves. */
remove_intermediates (0);
if (print_data_base_flag)
print_data_base ();
if (print_directory_flag)
log_working_directory (0);
if (makefiles != 0)
{
/* These names might have changed. */
register unsigned int i, j = 0;
for (i = 1; i < argc; ++i)
if (!strcmp (argv[i], "-f")) /* XXX */
{
char *p = &argv[i][2];
if (*p == '\0')
argv[++i] = makefiles->list[j];
else
argv[i] = concat ("-f", makefiles->list[j], "");
++j;
}
}
if (directories != 0 && directories->idx > 0)
{
char bad;
if (directory_before_chdir != 0)
{
if (chdir (directory_before_chdir) < 0)
{
perror_with_name ("chdir", "");
bad = 1;
}
else
bad = 0;
}
else
bad = 1;
if (bad)
fatal ("Couldn't change back to original directory.");
}
for (p = environ; *p != 0; ++p)
if (!strncmp (*p, "MAKELEVEL=", 10))
{
/* The SGI compiler apparently can't understand
the concept of storing the result of a function
in something other than a local variable. */
char *sgi_loses;
sgi_loses = (char *) alloca (40);
*p = sgi_loses;
sprintf (*p, "MAKELEVEL=%u", makelevel);
break;
}
if (debug_flag)
{
char **p;
fputs ("Re-executing:", stdout);
for (p = argv; *p != 0; ++p)
printf (" %s", *p);
puts ("");
}
fflush (stdout);
fflush (stderr);
exec_command (argv, environ);
/* NOTREACHED */
}
}
/* Set up `MAKEFLAGS' again for the normal targets. */
define_makeflags (1, 0);
{
int status;
/* If there were no command-line goals, use the default. */
if (goals == 0)
{
if (default_goal_file != 0)
{
goals = (struct dep *) xmalloc (sizeof (struct dep));
goals->next = 0;
goals->name = 0;
goals->file = default_goal_file;
}
}
else
lastgoal->next = 0;
if (goals != 0)
{
/* Update the goals. */
if (debug_flag)
puts ("Updating goal targets....");
switch (update_goal_chain (goals, 0))
{
case -1:
/* Nothing happened. */
case 0:
/* Updated successfully. */
status = 0;
break;
case 1:
/* Updating failed. */
status = 1;
break;
default:
abort ();
}
}
else
{
if (read_makefiles == 0)
fatal ("No targets specified and no makefile found");
else
fatal ("No targets");
}
/* Exit. */
die (status);
}
return 0;
}
/* Parsing of arguments, decoding of switches. */
static char options[sizeof (switches) / sizeof (switches[0]) * 3];
static struct option long_options[(sizeof (switches) / sizeof (switches[0])) +
(sizeof (long_option_aliases) /
sizeof (long_option_aliases[0]))];
/* Fill in the string and vector for getopt. */
static void
init_switches ()
{
register char *p;
register int c;
register unsigned int i;
if (options[0] != '\0')
/* Already done. */
return;
p = options;
for (i = 0; switches[i].c != '\0'; ++i)
{
long_options[i].name = (switches[i].long_name == 0 ? "" :
switches[i].long_name);
long_options[i].flag = 0;
long_options[i].val = switches[i].c;
if (isalnum (switches[i].c))
*p++ = switches[i].c;
switch (switches[i].type)
{
case flag:
case flag_off:
case ignore:
long_options[i].has_arg = no_argument;
break;
case string:
case positive_int:
case floating:
if (isalnum (switches[i].c))
*p++ = ':';
if (switches[i].noarg_value != 0)
{
if (isalnum (switches[i].c))
*p++ = ':';
long_options[i].has_arg = optional_argument;
}
else
long_options[i].has_arg = required_argument;
break;
}
}
*p = '\0';
for (c = 0; c < (sizeof (long_option_aliases) /
sizeof (long_option_aliases[0]));
++c)
long_options[i++] = long_option_aliases[c];
long_options[i].name = 0;
}
/* Decode switches from ARGC and ARGV.
They came from the environment if ENV is nonzero. */
static void
decode_switches (argc, argv, env)
int argc;
char **argv;
int env;
{
int bad = 0;
register const struct command_switch *cs;
register struct stringlist *sl;
register int c;
if (!env)
{
other_args = (struct stringlist *) xmalloc (sizeof (struct stringlist));
other_args->max = argc + 1;
other_args->list = (char **) xmalloc ((argc + 1) * sizeof (char *));
other_args->idx = 1;
other_args->list[0] = argv[0];
}
/* getopt does most of the parsing for us.
First, get its vectors set up. */
init_switches ();
/* Let getopt produce error messages for the command line,
but not for options from the environment. */
opterr = !env;
/* Reset getopt's state. */
optind = 0;
while ((c = getopt_long (argc, argv,
options, long_options, (int *) 0)) != EOF)
{
if (c == '?')
/* Bad option. We will print a usage message and die later.
But continue to parse the other options so the user can
see all he did wrong. */
bad = 1;
else
for (cs = switches; cs->c != '\0'; ++cs)
if (cs->c == c)
{
/* Whether or not we will actually do anything with
this switch. We test this individually inside the
switch below rather than just once outside it, so that
options which are to be ignored still consume args. */
int doit = !env || cs->env;
switch (cs->type)
{
default:
abort ();
case ignore:
break;
case flag:
case flag_off:
if (doit)
*(int *) cs->value_ptr = cs->type == flag;
break;
case string:
if (!doit)
break;
if (optarg == 0)
optarg = cs->noarg_value;
sl = *(struct stringlist **) cs->value_ptr;
if (sl == 0)
{
sl = (struct stringlist *)
xmalloc (sizeof (struct stringlist));
sl->max = 5;
sl->idx = 0;
sl->list = (char **) xmalloc (5 * sizeof (char *));
*(struct stringlist **) cs->value_ptr = sl;
}
else if (sl->idx == sl->max - 1)
{
sl->max += 5;
sl->list = (char **)
xrealloc ((char *) sl->list,
sl->max * sizeof (char *));
}
sl->list[sl->idx++] = optarg;
sl->list[sl->idx] = 0;
break;
case positive_int:
if (optarg == 0 && argc > optind
&& isdigit (argv[optind][0]))
optarg = argv[optind++];
if (!doit)
break;
if (optarg != 0)
{
int i = atoi (optarg);
if (i < 1)
{
if (doit)
error ("the `-%c' option requires a \
positive integral argument",
cs->c);
bad = 1;
}
else
*(unsigned int *) cs->value_ptr = i;
}
else
*(unsigned int *) cs->value_ptr
= *(unsigned int *) cs->noarg_value;
break;
case floating:
if (optarg == 0 && optind < argc
&& (isdigit (argv[optind][0]) || argv[optind][0] == '.'))
optarg = argv[optind++];
if (doit)
*(double *) cs->value_ptr
= (optarg != 0 ? atof (optarg)
: *(double *) cs->noarg_value);
break;
}
/* We've found the switch. Stop looking. */
break;
}
}
if (!env)
{
/* Collect the remaining args in the `other_args' string list. */
while (optind < argc)
{
char *arg = argv[optind++];
if (arg[0] != '-' || arg[1] != '\0')
other_args->list[other_args->idx++] = arg;
}
other_args->list[other_args->idx] = 0;
}
if (!env && (bad || print_usage_flag))
{
/* Print a nice usage message. */
if (print_version_flag)
print_version ();
fprintf (stderr, "Usage: %s [options] [target] ...\n", program);
fputs ("Options:\n", stderr);
for (cs = switches; cs->c != '\0'; ++cs)
{
char buf[1024], shortarg[50], longarg[50], *p;
if (cs->description[0] == '-')
continue;
switch (long_options[cs - switches].has_arg)
{
case no_argument:
shortarg[0] = longarg[0] = '\0';
break;
case required_argument:
sprintf (longarg, "=%s", cs->argdesc);
sprintf (shortarg, " %s", cs->argdesc);
break;
case optional_argument:
sprintf (longarg, "[=%s]", cs->argdesc);
sprintf (shortarg, " [%s]", cs->argdesc);
break;
}
p = buf;
if (isalnum (cs->c))
{
sprintf (buf, " -%c%s", cs->c, shortarg);
p += strlen (p);
}
if (cs->long_name != 0)
{
unsigned int i;
sprintf (p, "%s--%s%s",
!isalnum (cs->c) ? " " : ", ",
cs->long_name, longarg);
p += strlen (p);
for (i = 0; i < (sizeof (long_option_aliases) /
sizeof (long_option_aliases[0]));
++i)
if (long_option_aliases[i].val == cs->c)
{
sprintf (p, ", --%s%s",
long_option_aliases[i].name, longarg);
p += strlen (p);
}
}
{
const struct command_switch *ncs = cs;
while ((++ncs)->c != '\0')
if (ncs->description[0] == '-' &&
ncs->description[1] == cs->c)
{
/* This is another switch that does the same
one as the one we are processing. We want
to list them all together on one line. */
sprintf (p, ", -%c%s", ncs->c, shortarg);
p += strlen (p);
if (ncs->long_name != 0)
{
sprintf (p, ", --%s%s", ncs->long_name, longarg);
p += strlen (p);
}
}
}
if (p - buf > DESCRIPTION_COLUMN - 2)
/* The list of option names is too long to fit on the same
line with the description, leaving at least two spaces.
Print it on its own line instead. */
{
fprintf (stderr, "%s\n", buf);
buf[0] = '\0';
}
fprintf (stderr, "%*s%s.\n",
- DESCRIPTION_COLUMN,
buf, cs->description);
}
die (bad);
}
}
/* Decode switches from environment variable ENVAR (which is LEN chars long).
We do this by chopping the value into a vector of words, prepending a
dash to the first word if it lacks one, and passing the vector to
decode_switches. */
static void
decode_env_switches (envar, len)
char *envar;
unsigned int len;
{
char *varref = (char *) alloca (2 + len + 2);
char *value, *args;
int argc;
char **argv;
/* Get the variable's value. */
varref[0] = '$';
varref[1] = '(';
bcopy (envar, &varref[2], len);
varref[2 + len] = ')';
varref[2 + len + 1] = '\0';
value = variable_expand (varref);
/* Skip whitespace, and check for an empty value. */
value = next_token (value);
len = strlen (value);
if (len == 0)
return;
/* Make a copy of the value in ARGS, where we will munge it.
If it does not begin with a dash, prepend one.
We must allocate lasting storage for this (and we never free it) because
decode_switches may save pointers into it for string-valued switches. */
args = (char *) xmalloc (1 + len + 2);
if (value[0] != '-')
args[0] = '-';
bcopy (value, value[0] == '-' ? args : &args[1], len + 1);
/* Write an extra null terminator so our loop below will
never be in danger of looking past the end of the string. */
args[(value[0] == '-' ? 0 : 1) + len + 1] = '\0';
/* Allocate a vector that is definitely big enough. */
argv = (char **) alloca ((1 + len + 1) * sizeof (char *));
/* getopt will look at the arguments starting at ARGV[1].
Prepend a spacer word. */
argv[0] = 0;
argc = 1;
do
{
argv[argc++] = args;
args = end_of_token (args);
*args++ = '\0';
} while (*args != '\0');
argv[argc] = 0;
/* Parse those words. */
decode_switches (argc, argv, 1);
}
/* Define the MAKEFLAGS and MFLAGS variables to reflect the settings of the
command switches. Include options with args if ALL is nonzero.
Don't include options with the `no_makefile' flag set if MAKEFILE. */
static void
define_makeflags (all, makefile)
int all, makefile;
{
register const struct command_switch *cs;
char *flagstring;
/* We will construct a linked list of `struct flag's describing
all the flags which need to go in MAKEFLAGS. Then, once we
know how many there are and their lengths, we can put them all
together in a string. */
struct flag
{
struct flag *next;
const struct command_switch *cs;
char *arg;
unsigned int arglen;
};
struct flag *flags = 0;
unsigned int flagslen = 0;
#define ADD_FLAG(ARG, LEN) \
do { \
struct flag *new = (struct flag *) alloca (sizeof (struct flag)); \
new->cs = cs; \
new->arg = (ARG); \
new->arglen = (LEN); \
new->next = flags; \
flags = new; \
if (new->arg == 0) \
++flagslen; /* Just a single flag letter. */ \
else \
flagslen += 1 + 1 + 1 + 1 + new->arglen; /* " -x foo" */ \
if (!isalnum (cs->c)) \
/* This switch has no single-letter version, so we use the long. */ \
flagslen += 2 + strlen (cs->long_name); \
} while (0)
for (cs = switches; cs->c != '\0'; ++cs)
if (cs->toenv && (!makefile || !cs->no_makefile))
switch (cs->type)
{
default:
abort ();
case ignore:
break;
case flag:
case flag_off:
if (!*(int *) cs->value_ptr == (cs->type == flag_off)
&& (cs->default_value == 0
|| *(int *) cs->value_ptr != *(int *) cs->default_value))
ADD_FLAG (0, 0);
break;
case positive_int:
if (all)
{
if ((cs->default_value != 0
&& (*(unsigned int *) cs->value_ptr
== *(unsigned int *) cs->default_value)))
break;
else if (cs->noarg_value != 0
&& (*(unsigned int *) cs->value_ptr ==
*(unsigned int *) cs->noarg_value))
ADD_FLAG ("", 0); /* Optional value omitted; see below. */
else if (cs->c == 'j')
/* Special case for `-j'. */
ADD_FLAG ("1", 1);
else
{
char *buf = (char *) alloca (30);
sprintf (buf, "%u", *(unsigned int *) cs->value_ptr);
ADD_FLAG (buf, strlen (buf));
}
}
break;
case floating:
if (all)
{
if (cs->default_value != 0
&& (*(double *) cs->value_ptr
== *(double *) cs->default_value))
break;
else if (cs->noarg_value != 0
&& (*(double *) cs->value_ptr
== *(double *) cs->noarg_value))
ADD_FLAG ("", 0); /* Optional value omitted; see below. */
else
{
char *buf = (char *) alloca (100);
sprintf (buf, "%g", *(double *) cs->value_ptr);
ADD_FLAG (buf, strlen (buf));
}
}
break;
case string:
if (all)
{
struct stringlist *sl = *(struct stringlist **) cs->value_ptr;
if (sl != 0)
{
/* Add the elements in reverse order, because
all the flags get reversed below; and the order
matters for some switches (like -I). */
register unsigned int i = sl->idx;
while (i-- > 0)
ADD_FLAG (sl->list[i], strlen (sl->list[i]));
}
}
break;
}
#undef ADD_FLAG
if (flags == 0)
/* No flags. Use a string of two nulls so [1] works below. */
flagstring = "\0";
else
{
/* Construct the value in FLAGSTRING.
We allocate enough space for a preceding dash and trailing null. */
register char *p;
flagstring = (char *) alloca (1 + flagslen + 1);
p = flagstring;
*p++ = '-';
do
{
/* Add the flag letter or name to the string. */
if (!isalnum (flags->cs->c))
{
*p++ = '-';
strcpy (p, flags->cs->long_name);
p += strlen (p);
}
else
*p++ = flags->cs->c;
if (flags->arg != 0)
{
/* A flag that takes an optional argument which in this case
is omitted is specified by ARG being "" and ARGLEN being 0.
We must distinguish because a following flag appended without
an intervening " -" is considered the arg for the first. */
if (flags->arglen > 0)
{
/* Add its argument too. */
*p++ = !isalnum (flags->cs->c) ? '=' : ' ';
bcopy (flags->arg, p, flags->arglen);
p += flags->arglen;
}
/* Write a following space and dash, for the next flag. */
*p++ = ' ';
*p++ = '-';
}
else if (!isalnum (flags->cs->c))
{
/* Long options must each go in their own word,
so we write the following space and dash. */
*p++ = ' ';
*p++ = '-';
}
flags = flags->next;
} while (flags != 0);
if (p[-1] == '-')
/* Kill the final space and dash. */
p -= 2;
/* Terminate the string. */
*p = '\0';
}
(void) define_variable ("MAKEFLAGS", 9,
/* On Sun, the value of MFLAGS starts with a `-' but
the value of MAKEFLAGS lacks the `-'.
Be compatible with this unless FLAGSTRING starts
with a long option `--foo', since removing the
first dash would result in the bogus `-foo'. */
flagstring[1] == '-' ? flagstring : &flagstring[1],
o_env, 0);
(void) define_variable ("MFLAGS", 6, flagstring, o_env, 0);
}
/* Print version information. */
static void
print_version ()
{
static int printed_version = 0;
extern char *remote_description;
char *precede = print_data_base_flag ? "# " : "";
if (printed_version)
/* Do it only once. */
return;
printf ("%sGNU Make version %s", precede, version_string);
if (remote_description != 0 && *remote_description != '\0')
printf ("-%s", remote_description);
printf (", by Richard Stallman and Roland McGrath.\n\
%sCopyright (C) 1988, 89, 90, 91, 92, 93 Free Software Foundation, Inc.\n\
%sThis is free software; see the source for copying conditions.\n\
%sThere is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n\
%sPARTICULAR PURPOSE.\n\n", precede, precede, precede, precede);
printed_version = 1;
/* Flush stdout so the user doesn't have to wait to see the
version information while things are thought about. */
fflush (stdout);
}
/* Print a bunch of information about this and that. */
static void
print_data_base ()
{
extern char *ctime ();
time_t when;
when = time ((time_t *) 0);
printf ("\n# Make data base, printed on %s", ctime (&when));
print_variable_data_base ();
print_dir_data_base ();
print_rule_data_base ();
print_file_data_base ();
print_vpath_data_base ();
when = time ((time_t *) 0);
printf ("\n# Finished Make data base on %s\n", ctime (&when));
}
/* Exit with STATUS, cleaning up as necessary. */
void
die (status)
int status;
{
static char dying = 0;
if (!dying)
{
int err;
dying = 1;
if (print_version_flag)
print_version ();
/* Wait for children to die. */
for (err = status != 0; job_slots_used > 0; err = 0)
reap_children (1, err);
/* Remove the intermediate files. */
remove_intermediates (0);
if (print_data_base_flag)
print_data_base ();
if (print_directory_flag)
log_working_directory (0);
}
exit (status);
}
/* Write a message indicating that we've just entered or
left (according to ENTERING) the current directory. */
static void
log_working_directory (entering)
int entering;
{
static int entered = 0;
char *message = entering ? "Entering" : "Leaving";
if (entering)
entered = 1;
else if (!entered)
/* Don't print the leaving message if we
haven't printed the entering message. */
return;
if (print_data_base_flag)
fputs ("# ", stdout);
if (makelevel == 0)
printf ("%s: %s ", program, message);
else
printf ("%s[%u]: %s ", program, makelevel, message);
if (starting_directory == 0)
puts ("an unknown directory");
else
printf ("directory `%s'\n", starting_directory);
}
|