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 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046
|
/* $NetBSD: main.c,v 1.226 2014/02/07 17:23:35 pooka 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.
*/
#ifndef MAKE_NATIVE
static char rcsid[] = "$NetBSD: main.c,v 1.226 2014/02/07 17:23:35 pooka Exp $";
#else
#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993\
The Regents of the University of California. All rights reserved.");
#endif /* not lint */
#ifndef lint
#if 0
static char sccsid[] = "@(#)main.c 8.3 (Berkeley) 3/19/94";
#else
__RCSID("$NetBSD: main.c,v 1.226 2014/02/07 17:23:35 pooka Exp $");
#endif
#endif /* not lint */
#endif
/*-
* main.c --
* The main file for this entire program. Exit routines etc
* reside here.
*
* Utility functions defined in this file:
* Main_ParseArgLine Takes a line of arguments, breaks them and
* treats them as if they were given when first
* invoked. Used by the parse module to implement
* the .MFLAGS target.
*
* Error Print a tagged error message. The global
* MAKE variable must have been defined. This
* takes a format string and two optional
* arguments for it.
*
* Fatal Print an error message and exit. Also takes
* a format string and two arguments.
*
* Punt Aborts all jobs and exits with a message. Also
* takes a format string and two arguments.
*
* Finish Finish things up by printing the number of
* errors which occurred, as passed to it, and
* exiting.
*/
#include <sys/types.h>
#include <sys/time.h>
#include <sys/param.h>
#include <sys/resource.h>
#include <sys/stat.h>
#if defined(MAKE_NATIVE) && defined(HAVE_SYSCTL)
#include <sys/sysctl.h>
#endif
#include <sys/utsname.h>
#include "wait.h"
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include "make.h"
#include "hash.h"
#include "dir.h"
#include "job.h"
#include "pathnames.h"
#include "trace.h"
#ifdef USE_IOVEC
#include <sys/uio.h>
#endif
#ifndef DEFMAXLOCAL
#define DEFMAXLOCAL DEFMAXJOBS
#endif /* DEFMAXLOCAL */
#ifndef __arraycount
# define __arraycount(__x) (sizeof(__x) / sizeof(__x[0]))
#endif
#ifdef TARGET_MACHINE
#undef MAKE_MACHINE
#define MAKE_MACHINE TARGET_MACHINE
#endif
#ifdef TARGET_MACHINE_ARCH
#undef MAKE_MACHINE_ARCH
#define MAKE_MACHINE_ARCH TARGET_MACHINE_ARCH
#endif
#ifdef TARGET_MACHINE_MULTIARCH
#undef MAKE_MACHINE_MULTIARCH
#define MAKE_MACHINE_MULTIARCH TARGET_MACHINE_MULTIARCH
#endif
Lst create; /* Targets to be made */
time_t now; /* Time at start of make */
GNode *DEFAULT; /* .DEFAULT node */
Boolean allPrecious; /* .PRECIOUS given on line by itself */
static Boolean noBuiltins; /* -r flag */
static Lst makefiles; /* ordered list of makefiles to read */
static Boolean printVars; /* print value of one or more vars */
static Lst variables; /* list of variables to print */
int maxJobs; /* -j argument */
static int maxJobTokens; /* -j argument */
Boolean compatMake; /* -B argument */
int debug; /* -d argument */
Boolean debugVflag; /* -dV */
Boolean noExecute; /* -n flag */
Boolean noRecursiveExecute; /* -N flag */
Boolean keepgoing; /* -k flag */
Boolean queryFlag; /* -q flag */
Boolean touchFlag; /* -t flag */
Boolean enterFlag; /* -w flag */
Boolean ignoreErrors; /* -i flag */
Boolean beSilent; /* -s flag */
Boolean oldVars; /* variable substitution style */
Boolean checkEnvFirst; /* -e flag */
Boolean parseWarnFatal; /* -W flag */
Boolean jobServer; /* -J flag */
static int jp_0 = -1, jp_1 = -1; /* ends of parent job pipe */
Boolean varNoExportEnv; /* -X flag */
Boolean doing_depend; /* Set while reading .depend */
static Boolean jobsRunning; /* TRUE if the jobs might be running */
static const char * tracefile;
static void MainParseArgs(int, char **);
static int ReadMakefile(const void *, const void *);
static void usage(void) MAKE_ATTR_DEAD;
static Boolean ignorePWD; /* if we use -C, PWD is meaningless */
static char objdir[MAXPATHLEN + 1]; /* where we chdir'ed to */
char curdir[MAXPATHLEN + 1]; /* Startup directory */
char *progname; /* the program name */
char *makeDependfile;
pid_t myPid;
int makelevel;
Boolean forceJobs = FALSE;
/*
* On some systems MACHINE is defined as something other than
* what we want.
*/
#ifdef FORCE_MACHINE
# undef MACHINE
# define MACHINE FORCE_MACHINE
#endif
extern Lst parseIncPath;
/*
* For compatibility with the POSIX version of MAKEFLAGS that includes
* all the options with out -, convert flags to -f -l -a -g -s.
*/
static char *
explode(const char *flags)
{
size_t len;
char *nf, *st;
const char *f;
if (flags == NULL)
return NULL;
for (f = flags; *f; f++)
if (!isalpha((unsigned char)*f))
break;
if (*f)
return bmake_strdup(flags);
len = strlen(flags);
st = nf = bmake_malloc(len * 3 + 1);
while (*flags) {
*nf++ = '-';
*nf++ = *flags++;
*nf++ = ' ';
}
*nf = '\0';
return st;
}
static void
parse_debug_options(const char *argvalue)
{
const char *modules;
const char *mode;
char *fname;
int len;
for (modules = argvalue; *modules; ++modules) {
switch (*modules) {
case 'A':
debug = ~0;
break;
case 'a':
debug |= DEBUG_ARCH;
break;
case 'C':
debug |= DEBUG_CWD;
break;
case 'c':
debug |= DEBUG_COND;
break;
case 'd':
debug |= DEBUG_DIR;
break;
case 'e':
debug |= DEBUG_ERROR;
break;
case 'f':
debug |= DEBUG_FOR;
break;
case 'g':
if (modules[1] == '1') {
debug |= DEBUG_GRAPH1;
++modules;
}
else if (modules[1] == '2') {
debug |= DEBUG_GRAPH2;
++modules;
}
else if (modules[1] == '3') {
debug |= DEBUG_GRAPH3;
++modules;
}
break;
case 'j':
debug |= DEBUG_JOB;
break;
case 'l':
debug |= DEBUG_LOUD;
break;
case 'M':
debug |= DEBUG_META;
break;
case 'm':
debug |= DEBUG_MAKE;
break;
case 'n':
debug |= DEBUG_SCRIPT;
break;
case 'p':
debug |= DEBUG_PARSE;
break;
case 's':
debug |= DEBUG_SUFF;
break;
case 't':
debug |= DEBUG_TARG;
break;
case 'V':
debugVflag = TRUE;
break;
case 'v':
debug |= DEBUG_VAR;
break;
case 'x':
debug |= DEBUG_SHELL;
break;
case 'F':
if (debug_file != stdout && debug_file != stderr)
fclose(debug_file);
if (*++modules == '+') {
modules++;
mode = "a";
} else
mode = "w";
if (strcmp(modules, "stdout") == 0) {
debug_file = stdout;
goto debug_setbuf;
}
if (strcmp(modules, "stderr") == 0) {
debug_file = stderr;
goto debug_setbuf;
}
len = strlen(modules);
fname = malloc(len + 20);
memcpy(fname, modules, len + 1);
/* Let the filename be modified by the pid */
if (strcmp(fname + len - 3, ".%d") == 0)
snprintf(fname + len - 2, 20, "%d", getpid());
debug_file = fopen(fname, mode);
if (!debug_file) {
fprintf(stderr, "Cannot open debug file %s\n",
fname);
usage();
}
free(fname);
goto debug_setbuf;
default:
(void)fprintf(stderr,
"%s: illegal argument to d option -- %c\n",
progname, *modules);
usage();
}
}
debug_setbuf:
/*
* Make the debug_file unbuffered, and make
* stdout line buffered (unless debugfile == stdout).
*/
setvbuf(debug_file, NULL, _IONBF, 0);
if (debug_file != stdout) {
setvbuf(stdout, NULL, _IOLBF, 0);
}
}
/*-
* MainParseArgs --
* Parse a given argument vector. Called from main() and from
* Main_ParseArgLine() when the .MAKEFLAGS target is used.
*
* XXX: Deal with command line overriding .MAKEFLAGS in makefile
*
* Results:
* None
*
* Side Effects:
* Various global and local flags will be set depending on the flags
* given
*/
static void
MainParseArgs(int argc, char **argv)
{
char *p;
int c = '?';
int arginc;
char *argvalue;
const char *getopt_def;
char *optscan;
Boolean inOption, dashDash = FALSE;
char found_path[MAXPATHLEN + 1]; /* for searching for sys.mk */
#define OPTFLAGS "BC:D:I:J:NST:V:WXd:ef:ij:km:nqrstw"
/* Can't actually use getopt(3) because rescanning is not portable */
getopt_def = OPTFLAGS;
rearg:
inOption = FALSE;
optscan = NULL;
while(argc > 1) {
char *getopt_spec;
if(!inOption)
optscan = argv[1];
c = *optscan++;
arginc = 0;
if(inOption) {
if(c == '\0') {
++argv;
--argc;
inOption = FALSE;
continue;
}
} else {
if (c != '-' || dashDash)
break;
inOption = TRUE;
c = *optscan++;
}
/* '-' found at some earlier point */
getopt_spec = strchr(getopt_def, c);
if(c != '\0' && getopt_spec != NULL && getopt_spec[1] == ':') {
/* -<something> found, and <something> should have an arg */
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 'B':
compatMake = TRUE;
Var_Append(MAKEFLAGS, "-B", VAR_GLOBAL);
Var_Set(MAKE_MODE, "compat", VAR_GLOBAL, 0);
break;
case 'C':
if (chdir(argvalue) == -1) {
(void)fprintf(stderr,
"%s: chdir %s: %s\n",
progname, argvalue,
strerror(errno));
exit(1);
}
if (getcwd(curdir, MAXPATHLEN) == NULL) {
(void)fprintf(stderr, "%s: %s.\n", progname, strerror(errno));
exit(2);
}
ignorePWD = TRUE;
break;
case 'D':
if (argvalue == NULL || argvalue[0] == 0) goto noarg;
Var_Set(argvalue, "1", VAR_GLOBAL, 0);
Var_Append(MAKEFLAGS, "-D", VAR_GLOBAL);
Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
break;
case 'I':
if (argvalue == NULL) goto noarg;
Parse_AddIncludeDir(argvalue);
Var_Append(MAKEFLAGS, "-I", VAR_GLOBAL);
Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
break;
case 'J':
if (argvalue == NULL) goto noarg;
if (sscanf(argvalue, "%d,%d", &jp_0, &jp_1) != 2) {
(void)fprintf(stderr,
"%s: internal error -- J option malformed (%s)\n",
progname, argvalue);
usage();
}
if ((fcntl(jp_0, F_GETFD, 0) < 0) ||
(fcntl(jp_1, F_GETFD, 0) < 0)) {
#if 0
(void)fprintf(stderr,
"%s: ###### warning -- J descriptors were closed!\n",
progname);
exit(2);
#endif
jp_0 = -1;
jp_1 = -1;
compatMake = TRUE;
} else {
Var_Append(MAKEFLAGS, "-J", VAR_GLOBAL);
Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
jobServer = TRUE;
}
break;
case 'N':
noExecute = TRUE;
noRecursiveExecute = TRUE;
Var_Append(MAKEFLAGS, "-N", VAR_GLOBAL);
break;
case 'S':
keepgoing = FALSE;
Var_Append(MAKEFLAGS, "-S", VAR_GLOBAL);
break;
case 'T':
if (argvalue == NULL) goto noarg;
tracefile = bmake_strdup(argvalue);
Var_Append(MAKEFLAGS, "-T", VAR_GLOBAL);
Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
break;
case 'V':
if (argvalue == NULL) goto noarg;
printVars = TRUE;
(void)Lst_AtEnd(variables, argvalue);
Var_Append(MAKEFLAGS, "-V", VAR_GLOBAL);
Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
break;
case 'W':
parseWarnFatal = TRUE;
break;
case 'X':
varNoExportEnv = TRUE;
Var_Append(MAKEFLAGS, "-X", VAR_GLOBAL);
break;
case 'd':
if (argvalue == NULL) goto noarg;
/* If '-d-opts' don't pass to children */
if (argvalue[0] == '-')
argvalue++;
else {
Var_Append(MAKEFLAGS, "-d", VAR_GLOBAL);
Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
}
parse_debug_options(argvalue);
break;
case 'e':
checkEnvFirst = TRUE;
Var_Append(MAKEFLAGS, "-e", VAR_GLOBAL);
break;
case 'f':
if (argvalue == NULL) goto noarg;
(void)Lst_AtEnd(makefiles, argvalue);
break;
case 'i':
ignoreErrors = TRUE;
Var_Append(MAKEFLAGS, "-i", VAR_GLOBAL);
break;
case 'j':
if (argvalue == NULL) goto noarg;
forceJobs = TRUE;
maxJobs = strtol(argvalue, &p, 0);
if (*p != '\0' || maxJobs < 1) {
(void)fprintf(stderr, "%s: illegal argument to -j -- must be positive integer!\n",
progname);
exit(1);
}
Var_Append(MAKEFLAGS, "-j", VAR_GLOBAL);
Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
Var_Set(".MAKE.JOBS", argvalue, VAR_GLOBAL, 0);
maxJobTokens = maxJobs;
break;
case 'k':
keepgoing = TRUE;
Var_Append(MAKEFLAGS, "-k", VAR_GLOBAL);
break;
case 'm':
if (argvalue == NULL) goto noarg;
/* look for magic parent directory search string */
if (strncmp(".../", argvalue, 4) == 0) {
if (!Dir_FindHereOrAbove(curdir, argvalue+4,
found_path, sizeof(found_path)))
break; /* nothing doing */
(void)Dir_AddDir(sysIncPath, found_path);
} else {
(void)Dir_AddDir(sysIncPath, argvalue);
}
Var_Append(MAKEFLAGS, "-m", VAR_GLOBAL);
Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
break;
case 'n':
noExecute = TRUE;
Var_Append(MAKEFLAGS, "-n", VAR_GLOBAL);
break;
case 'q':
queryFlag = TRUE;
/* Kind of nonsensical, wot? */
Var_Append(MAKEFLAGS, "-q", VAR_GLOBAL);
break;
case 'r':
noBuiltins = TRUE;
Var_Append(MAKEFLAGS, "-r", VAR_GLOBAL);
break;
case 's':
beSilent = TRUE;
Var_Append(MAKEFLAGS, "-s", VAR_GLOBAL);
break;
case 't':
touchFlag = TRUE;
Var_Append(MAKEFLAGS, "-t", VAR_GLOBAL);
break;
case 'w':
enterFlag = TRUE;
Var_Append(MAKEFLAGS, "-w", VAR_GLOBAL);
break;
case '-':
dashDash = TRUE;
break;
default:
case '?':
#ifndef MAKE_NATIVE
fprintf(stderr, "getopt(%s) -> %d (%c)\n",
OPTFLAGS, c, c);
#endif
usage();
}
argv += arginc;
argc -= arginc;
}
oldVars = TRUE;
/*
* 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_IsVar(argv[1])) {
Parse_DoVar(argv[1], VAR_CMD);
} else {
if (!*argv[1])
Punt("illegal (null) argument.");
if (*argv[1] == '-' && !dashDash)
goto rearg;
(void)Lst_AtEnd(create, bmake_strdup(argv[1]));
}
return;
noarg:
(void)fprintf(stderr, "%s: option requires an argument -- %c\n",
progname, c);
usage();
}
/*-
* Main_ParseArgLine --
* Used by the parse module when a .MFLAGS or .MAKEFLAGS target
* is encountered and by main() when reading the .MAKEFLAGS envariable.
* Takes a line of arguments and breaks it into its
* component words and passes those words and the number of them to the
* MainParseArgs function.
* The line should have all its leading whitespace removed.
*
* Input:
* line Line to fracture
*
* Results:
* None
*
* Side Effects:
* Only those that come from the various arguments.
*/
void
Main_ParseArgLine(const char *line)
{
char **argv; /* Manufactured argument vector */
int argc; /* Number of arguments in argv */
char *args; /* Space used by the args */
char *buf, *p1;
char *argv0 = Var_Value(".MAKE", VAR_GLOBAL, &p1);
size_t len;
if (line == NULL)
return;
for (; *line == ' '; ++line)
continue;
if (!*line)
return;
#ifndef POSIX
{
/*
* $MAKE may simply be naming the make(1) binary
*/
char *cp;
if (!(cp = strrchr(line, '/')))
cp = line;
if ((cp = strstr(cp, "make")) &&
strcmp(cp, "make") == 0)
return;
}
#endif
buf = bmake_malloc(len = strlen(line) + strlen(argv0) + 2);
(void)snprintf(buf, len, "%s %s", argv0, line);
if (p1)
free(p1);
argv = brk_string(buf, &argc, TRUE, &args);
if (argv == NULL) {
Error("Unterminated quoted string [%s]", buf);
free(buf);
return;
}
free(buf);
MainParseArgs(argc, argv);
free(args);
free(argv);
}
Boolean
Main_SetObjdir(const char *path)
{
struct stat sb;
char *p = NULL;
char buf[MAXPATHLEN + 1];
Boolean rc = FALSE;
/* expand variable substitutions */
if (strchr(path, '$') != 0) {
snprintf(buf, MAXPATHLEN, "%s", path);
path = p = Var_Subst(NULL, buf, VAR_GLOBAL, 0);
}
if (path[0] != '/') {
snprintf(buf, MAXPATHLEN, "%s/%s", curdir, path);
path = buf;
}
/* look for the directory and try to chdir there */
if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) {
if (chdir(path)) {
(void)fprintf(stderr, "make warning: %s: %s.\n",
path, strerror(errno));
} else {
strncpy(objdir, path, MAXPATHLEN);
Var_Set(".OBJDIR", objdir, VAR_GLOBAL, 0);
setenv("PWD", objdir, 1);
Dir_InitDot();
rc = TRUE;
}
}
if (p)
free(p);
return rc;
}
/*-
* ReadAllMakefiles --
* wrapper around ReadMakefile() to read all.
*
* Results:
* TRUE if ok, FALSE on error
*/
static int
ReadAllMakefiles(const void *p, const void *q)
{
return (ReadMakefile(p, q) == 0);
}
int
str2Lst_Append(Lst lp, char *str, const char *sep)
{
char *cp;
int n;
if (!sep)
sep = " \t";
for (n = 0, cp = strtok(str, sep); cp; cp = strtok(NULL, sep)) {
(void)Lst_AtEnd(lp, cp);
n++;
}
return (n);
}
#ifdef SIGINFO
/*ARGSUSED*/
static void
siginfo(int signo MAKE_ATTR_UNUSED)
{
char dir[MAXPATHLEN];
char str[2 * MAXPATHLEN];
int len;
if (getcwd(dir, sizeof(dir)) == NULL)
return;
len = snprintf(str, sizeof(str), "%s: Working in: %s\n", progname, dir);
if (len > 0)
(void)write(STDERR_FILENO, str, (size_t)len);
}
#endif
/*
* Allow makefiles some control over the mode we run in.
*/
void
MakeMode(const char *mode)
{
char *mp = NULL;
if (!mode)
mode = mp = Var_Subst(NULL, "${" MAKE_MODE ":tl}", VAR_GLOBAL, 0);
if (mode && *mode) {
if (strstr(mode, "compat")) {
compatMake = TRUE;
forceJobs = FALSE;
}
#if USE_META
if (strstr(mode, "meta"))
meta_mode_init(mode);
#endif
}
if (mp)
free(mp);
}
/*-
* main --
* The main function, for obvious reasons. Initializes variables
* and a few modules, then parses the arguments give it in the
* environment and on the command line. Reads the system makefile
* followed by either Makefile, makefile or the file given by the
* -f argument. Sets the .MAKEFLAGS PMake variable based on all the
* flags it has received by then uses either the Make or the Compat
* module to create the initial list of targets.
*
* Results:
* If -q was given, exits -1 if anything was out-of-date. Else it exits
* 0.
*
* Side Effects:
* The program exits when done. Targets are created. etc. etc. etc.
*/
int
main(int argc, char **argv)
{
Lst targs; /* target nodes to create -- passed to Make_Init */
Boolean outOfDate = FALSE; /* FALSE if all targets up to date */
struct stat sb, sa;
char *p1, *path;
char mdpath[MAXPATHLEN];
#ifdef FORCE_MACHINE
const char *machine = FORCE_MACHINE;
#else
const char *machine = getenv("MACHINE");
#endif
const char *machine_arch = getenv("MACHINE_ARCH");
const char *machine_multiarch = getenv("MACHINE_MULTIARCH");
char *syspath = getenv("MAKESYSPATH");
Lst sysMkPath; /* Path of sys.mk */
char *cp = NULL, *start;
/* avoid faults on read-only strings */
static char defsyspath[] = _PATH_DEFSYSPATH;
char found_path[MAXPATHLEN + 1]; /* for searching for sys.mk */
struct timeval rightnow; /* to initialize random seed */
struct utsname utsname;
/* default to writing debug to stderr */
debug_file = stderr;
#ifdef SIGINFO
(void)bmake_signal(SIGINFO, siginfo);
#endif
/*
* Set the seed to produce a different random sequence
* on each program execution.
*/
gettimeofday(&rightnow, NULL);
srandom(rightnow.tv_sec + rightnow.tv_usec);
if ((progname = strrchr(argv[0], '/')) != NULL)
progname++;
else
progname = argv[0];
#if defined(MAKE_NATIVE) || (defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE))
/*
* get rid of resource limit on file descriptors
*/
{
struct rlimit rl;
if (getrlimit(RLIMIT_NOFILE, &rl) != -1 &&
rl.rlim_cur != rl.rlim_max) {
rl.rlim_cur = rl.rlim_max;
(void)setrlimit(RLIMIT_NOFILE, &rl);
}
}
#endif
if (uname(&utsname) == -1) {
(void)fprintf(stderr, "%s: uname failed (%s).\n", progname,
strerror(errno));
exit(2);
}
/*
* Get the name of this type of MACHINE from utsname
* so we can share an executable for similar machines.
* (i.e. m68k: amiga hp300, mac68k, sun3, ...)
*
* Note that all of MACHINE, MACHINE_ARCH and MACHINE_MULTIARCH
* are decided at run-time.
*/
if (!machine) {
#ifdef MAKE_NATIVE
machine = utsname.machine;
#else
#ifdef MAKE_MACHINE
machine = MAKE_MACHINE;
#else
machine = "unknown";
#endif
#endif
}
if (!machine_arch) {
#if defined(MAKE_NATIVE) && defined(HAVE_SYSCTL) && defined(CTL_HW) && defined(HW_MACHINE_ARCH)
static char machine_arch_buf[sizeof(utsname.machine)];
int mib[2] = { CTL_HW, HW_MACHINE_ARCH };
size_t len = sizeof(machine_arch_buf);
if (sysctl(mib, __arraycount(mib), machine_arch_buf,
&len, NULL, 0) < 0) {
(void)fprintf(stderr, "%s: sysctl failed (%s).\n", progname,
strerror(errno));
exit(2);
}
machine_arch = machine_arch_buf;
#else
#ifndef MACHINE_ARCH
#ifdef MAKE_MACHINE_ARCH
machine_arch = MAKE_MACHINE_ARCH;
#else
machine_arch = "unknown";
#endif
#else
machine_arch = MACHINE_ARCH;
#endif
#endif
}
myPid = getpid(); /* remember this for vFork() */
if (!machine_multiarch) {
#ifndef MACHINE_MULTIARCH
#ifdef MAKE_MACHINE_MULTIARCH
machine_multiarch = MAKE_MACHINE_MULTIARCH;
#else
machine_multiarch = "unknown-unknown-unknown";
#endif
#else
machine_multiarch = MACHINE_MULTIARCH;
#endif
}
/*
* Just in case MAKEOBJDIR wants us to do something tricky.
*/
Var_Init(); /* Initialize the lists of variables for
* parsing arguments */
Var_Set(".MAKE.OS", utsname.sysname, VAR_GLOBAL, 0);
Var_Set("MACHINE", machine, VAR_GLOBAL, 0);
Var_Set("MACHINE_ARCH", machine_arch, VAR_GLOBAL, 0);
Var_Set("MACHINE_MULTIARCH", machine_multiarch, VAR_GLOBAL, 0);
#ifdef MAKE_VERSION
Var_Set("MAKE_VERSION", MAKE_VERSION, VAR_GLOBAL, 0);
#endif
Var_Set(".newline", "\n", VAR_GLOBAL, 0); /* handy for :@ loops */
/*
* This is the traditional preference for makefiles.
*/
#ifndef MAKEFILE_PREFERENCE_LIST
# define MAKEFILE_PREFERENCE_LIST "makefile Makefile"
#endif
Var_Set(MAKEFILE_PREFERENCE, MAKEFILE_PREFERENCE_LIST,
VAR_GLOBAL, 0);
Var_Set(MAKE_DEPENDFILE, ".depend", VAR_GLOBAL, 0);
create = Lst_Init(FALSE);
makefiles = Lst_Init(FALSE);
printVars = FALSE;
debugVflag = FALSE;
variables = Lst_Init(FALSE);
beSilent = FALSE; /* Print commands as executed */
ignoreErrors = FALSE; /* Pay attention to non-zero returns */
noExecute = FALSE; /* Execute all commands */
noRecursiveExecute = FALSE; /* Execute all .MAKE targets */
keepgoing = FALSE; /* Stop on error */
allPrecious = FALSE; /* Remove targets when interrupted */
queryFlag = FALSE; /* This is not just a check-run */
noBuiltins = FALSE; /* Read the built-in rules */
touchFlag = FALSE; /* Actually update targets */
debug = 0; /* No debug verbosity, please. */
jobsRunning = FALSE;
maxJobs = DEFMAXLOCAL; /* Set default local max concurrency */
maxJobTokens = maxJobs;
compatMake = FALSE; /* No compat mode */
ignorePWD = FALSE;
/*
* Initialize the parsing, directory and variable modules to prepare
* for the reading of inclusion paths and variable settings on the
* command line
*/
/*
* Initialize various variables.
* MAKE also gets this name, for compatibility
* .MAKEFLAGS gets set to the empty string just in case.
* MFLAGS also gets initialized empty, for compatibility.
*/
Parse_Init();
if (argv[0][0] == '/' || strchr(argv[0], '/') == NULL) {
/*
* Leave alone if it is an absolute path, or if it does
* not contain a '/' in which case we need to find it in
* the path, like execvp(3) and the shells do.
*/
p1 = argv[0];
} else {
/*
* A relative path, canonicalize it.
*/
p1 = realpath(argv[0], mdpath);
if (!p1 || *p1 != '/' || stat(p1, &sb) < 0) {
p1 = argv[0]; /* realpath failed */
}
}
Var_Set("MAKE", p1, VAR_GLOBAL, 0);
Var_Set(".MAKE", p1, VAR_GLOBAL, 0);
Var_Set(MAKEFLAGS, "", VAR_GLOBAL, 0);
Var_Set(MAKEOVERRIDES, "", VAR_GLOBAL, 0);
Var_Set("MFLAGS", "", VAR_GLOBAL, 0);
Var_Set(".ALLTARGETS", "", VAR_GLOBAL, 0);
/* some makefiles need to know this */
Var_Set(MAKE_LEVEL ".ENV", MAKE_LEVEL_ENV, VAR_CMD, 0);
/*
* Set some other useful macros
*/
{
char tmp[64], *ep;
makelevel = ((ep = getenv(MAKE_LEVEL_ENV)) && *ep) ? atoi(ep) : 0;
if (makelevel < 0)
makelevel = 0;
snprintf(tmp, sizeof(tmp), "%d", makelevel);
Var_Set(MAKE_LEVEL, tmp, VAR_GLOBAL, 0);
snprintf(tmp, sizeof(tmp), "%u", myPid);
Var_Set(".MAKE.PID", tmp, VAR_GLOBAL, 0);
snprintf(tmp, sizeof(tmp), "%u", getppid());
Var_Set(".MAKE.PPID", tmp, VAR_GLOBAL, 0);
}
if (makelevel > 0) {
char pn[1024];
snprintf(pn, sizeof(pn), "%s[%d]", progname, makelevel);
progname = bmake_strdup(pn);
}
#ifdef USE_META
meta_init();
#endif
/*
* First snag any flags out of the MAKE environment variable.
* (Note this is *not* MAKEFLAGS since /bin/make uses that and it's
* in a different format).
*/
#ifdef POSIX
p1 = explode(getenv("MAKEFLAGS"));
Main_ParseArgLine(p1);
free(p1);
#else
Main_ParseArgLine(getenv("MAKE"));
#endif
/*
* Find where we are (now).
* We take care of PWD for the automounter below...
*/
if (getcwd(curdir, MAXPATHLEN) == NULL) {
(void)fprintf(stderr, "%s: getcwd: %s.\n",
progname, strerror(errno));
exit(2);
}
MainParseArgs(argc, argv);
if (enterFlag)
printf("%s: Entering directory `%s'\n", progname, curdir);
/*
* Verify that cwd is sane.
*/
if (stat(curdir, &sa) == -1) {
(void)fprintf(stderr, "%s: %s: %s.\n",
progname, curdir, strerror(errno));
exit(2);
}
/*
* All this code is so that we know where we are when we start up
* on a different machine with pmake.
* Overriding getcwd() with $PWD totally breaks MAKEOBJDIRPREFIX
* since the value of curdir can vary depending on how we got
* here. Ie sitting at a shell prompt (shell that provides $PWD)
* or via subdir.mk in which case its likely a shell which does
* not provide it.
* So, to stop it breaking this case only, we ignore PWD if
* MAKEOBJDIRPREFIX is set or MAKEOBJDIR contains a transform.
*/
#ifndef NO_PWD_OVERRIDE
if (!ignorePWD) {
char *pwd, *ptmp1 = NULL, *ptmp2 = NULL;
if ((pwd = getenv("PWD")) != NULL &&
Var_Value("MAKEOBJDIRPREFIX", VAR_CMD, &ptmp1) == NULL) {
const char *makeobjdir = Var_Value("MAKEOBJDIR",
VAR_CMD, &ptmp2);
if (makeobjdir == NULL || !strchr(makeobjdir, '$')) {
if (stat(pwd, &sb) == 0 &&
sa.st_ino == sb.st_ino &&
sa.st_dev == sb.st_dev)
(void)strncpy(curdir, pwd, MAXPATHLEN);
}
}
free(ptmp1);
free(ptmp2);
}
#endif
Var_Set(".CURDIR", curdir, VAR_GLOBAL, 0);
/*
* 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, _PATH_OBJDIR, and
* finally _PATH_OBJDIRPREFIX`pwd`, in that order. If none
* of these paths exist, just use .CURDIR.
*/
Dir_Init(curdir);
(void)Main_SetObjdir(curdir);
if ((path = Var_Value("MAKEOBJDIRPREFIX", VAR_CMD, &p1)) != NULL) {
(void)snprintf(mdpath, MAXPATHLEN, "%s%s", path, curdir);
(void)Main_SetObjdir(mdpath);
free(p1);
} else if ((path = Var_Value("MAKEOBJDIR", VAR_CMD, &p1)) != NULL) {
(void)Main_SetObjdir(path);
free(p1);
} else {
(void)snprintf(mdpath, MAXPATHLEN, "%s.%s", _PATH_OBJDIR, machine);
if (!Main_SetObjdir(mdpath) && !Main_SetObjdir(_PATH_OBJDIR)) {
(void)snprintf(mdpath, MAXPATHLEN, "%s%s",
_PATH_OBJDIRPREFIX, curdir);
(void)Main_SetObjdir(mdpath);
}
}
/*
* Be compatible if user did not specify -j and did not explicitly
* turned compatibility on
*/
if (!compatMake && !forceJobs) {
compatMake = TRUE;
}
/*
* Initialize archive, target and suffix modules in preparation for
* parsing the makefile(s)
*/
Arch_Init();
Targ_Init();
Suff_Init();
Trace_Init(tracefile);
DEFAULT = NULL;
(void)time(&now);
Trace_Log(MAKESTART, NULL);
/*
* Set up the .TARGETS variable to contain the list of targets to be
* created. If none specified, make the variable empty -- the parser
* will fill the thing in with the default or .MAIN target.
*/
if (!Lst_IsEmpty(create)) {
LstNode ln;
for (ln = Lst_First(create); ln != NULL;
ln = Lst_Succ(ln)) {
char *name = (char *)Lst_Datum(ln);
Var_Append(".TARGETS", name, VAR_GLOBAL);
}
} else
Var_Set(".TARGETS", "", VAR_GLOBAL, 0);
/*
* If no user-supplied system path was given (through the -m option)
* add the directories from the DEFSYSPATH (more than one may be given
* as dir1:...:dirn) to the system include path.
*/
if (syspath == NULL || *syspath == '\0')
syspath = defsyspath;
else
syspath = bmake_strdup(syspath);
for (start = syspath; *start != '\0'; start = cp) {
for (cp = start; *cp != '\0' && *cp != ':'; cp++)
continue;
if (*cp == ':') {
*cp++ = '\0';
}
/* look for magic parent directory search string */
if (strncmp(".../", start, 4) != 0) {
(void)Dir_AddDir(defIncPath, start);
} else {
if (Dir_FindHereOrAbove(curdir, start+4,
found_path, sizeof(found_path))) {
(void)Dir_AddDir(defIncPath, found_path);
}
}
}
if (syspath != defsyspath)
free(syspath);
/*
* Read in the built-in rules first, followed by the specified
* makefile, if it was (makefile != NULL), or the default
* makefile and Makefile, in that order, if it wasn't.
*/
if (!noBuiltins) {
LstNode ln;
sysMkPath = Lst_Init(FALSE);
Dir_Expand(_PATH_DEFSYSMK,
Lst_IsEmpty(sysIncPath) ? defIncPath : sysIncPath,
sysMkPath);
if (Lst_IsEmpty(sysMkPath))
Fatal("%s: no system rules (%s).", progname,
_PATH_DEFSYSMK);
ln = Lst_Find(sysMkPath, NULL, ReadMakefile);
if (ln == NULL)
Fatal("%s: cannot open %s.", progname,
(char *)Lst_Datum(ln));
}
if (!Lst_IsEmpty(makefiles)) {
LstNode ln;
ln = Lst_Find(makefiles, NULL, ReadAllMakefiles);
if (ln != NULL)
Fatal("%s: cannot open %s.", progname,
(char *)Lst_Datum(ln));
} else {
p1 = Var_Subst(NULL, "${" MAKEFILE_PREFERENCE "}",
VAR_CMD, 0);
if (p1) {
(void)str2Lst_Append(makefiles, p1, NULL);
(void)Lst_Find(makefiles, NULL, ReadMakefile);
free(p1);
}
}
/* In particular suppress .depend for '-r -V .OBJDIR -f /dev/null' */
if (!noBuiltins || !printVars) {
makeDependfile = Var_Subst(NULL, "${.MAKE.DEPENDFILE:T}",
VAR_CMD, 0);
doing_depend = TRUE;
(void)ReadMakefile(makeDependfile, NULL);
doing_depend = FALSE;
}
MakeMode(NULL);
Var_Append("MFLAGS", Var_Value(MAKEFLAGS, VAR_GLOBAL, &p1), VAR_GLOBAL);
if (p1)
free(p1);
if (!compatMake)
Job_ServerStart(maxJobTokens, jp_0, jp_1);
if (DEBUG(JOB))
fprintf(debug_file, "job_pipe %d %d, maxjobs %d, tokens %d, compat %d\n",
jp_0, jp_1, maxJobs, maxJobTokens, compatMake);
Main_ExportMAKEFLAGS(TRUE); /* initial export */
/*
* For compatibility, look at the directories in the VPATH variable
* and add them to the search path, if the variable is defined. The
* variable's value is in the same format as the PATH envariable, i.e.
* <directory>:<directory>:<directory>...
*/
if (Var_Exists("VPATH", VAR_CMD)) {
char *vpath, savec;
/*
* GCC stores string constants in read-only memory, but
* Var_Subst will want to write this thing, so store it
* in an array
*/
static char VPATH[] = "${VPATH}";
vpath = Var_Subst(NULL, VPATH, VAR_CMD, FALSE);
path = vpath;
do {
/* skip to end of directory */
for (cp = path; *cp != ':' && *cp != '\0'; cp++)
continue;
/* Save terminator character so know when to stop */
savec = *cp;
*cp = '\0';
/* Add directory to search path */
(void)Dir_AddDir(dirSearchPath, path);
*cp = savec;
path = cp + 1;
} while (savec == ':');
free(vpath);
}
/*
* Now that all search paths have been read for suffixes et al, it's
* time to add the default search path to their lists...
*/
Suff_DoPaths();
/*
* Propagate attributes through :: dependency lists.
*/
Targ_Propagate();
/* print the initial graph, if the user requested it */
if (DEBUG(GRAPH1))
Targ_PrintGraph(1);
/* print the values of any variables requested by the user */
if (printVars) {
LstNode ln;
Boolean expandVars;
if (debugVflag)
expandVars = FALSE;
else
expandVars = getBoolean(".MAKE.EXPAND_VARIABLES", FALSE);
for (ln = Lst_First(variables); ln != NULL;
ln = Lst_Succ(ln)) {
char *var = (char *)Lst_Datum(ln);
char *value;
if (strchr(var, '$')) {
value = p1 = Var_Subst(NULL, var, VAR_GLOBAL, 0);
} else if (expandVars) {
char tmp[128];
if (snprintf(tmp, sizeof(tmp), "${%s}", var) >= (int)(sizeof(tmp)))
Fatal("%s: variable name too big: %s",
progname, var);
value = p1 = Var_Subst(NULL, tmp, VAR_GLOBAL, 0);
} else {
value = Var_Value(var, VAR_GLOBAL, &p1);
}
printf("%s\n", value ? value : "");
if (p1)
free(p1);
}
} else {
/*
* 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(create))
targs = Parse_MainName();
else
targs = Targ_FindList(create, TARG_CREATE);
if (!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 (!queryFlag) {
Job_Init();
jobsRunning = TRUE;
}
/* Traverse the graph, checking on all the targets */
outOfDate = Make_Run(targs);
} else {
/*
* Compat_Init will take care of creating all the
* targets as well as initializing the module.
*/
Compat_Run(targs);
}
}
#ifdef CLEANUP
Lst_Destroy(targs, NULL);
Lst_Destroy(variables, NULL);
Lst_Destroy(makefiles, NULL);
Lst_Destroy(create, (FreeProc *)free);
#endif
/* print the graph now it's been processed if the user requested it */
if (DEBUG(GRAPH2))
Targ_PrintGraph(2);
Trace_Log(MAKEEND, 0);
if (enterFlag)
printf("%s: Leaving directory `%s'\n", progname, curdir);
Suff_End();
Targ_End();
Arch_End();
Var_End();
Parse_End();
Dir_End();
Job_End();
Trace_End();
return outOfDate ? 1 : 0;
}
/*-
* ReadMakefile --
* Open and parse the given makefile.
*
* Results:
* 0 if ok. -1 if couldn't open file.
*
* Side Effects:
* lots
*/
static int
ReadMakefile(const void *p, const void *q MAKE_ATTR_UNUSED)
{
const char *fname = p; /* makefile to read */
int fd;
size_t len = MAXPATHLEN;
char *name, *path = bmake_malloc(len);
if (!strcmp(fname, "-")) {
Parse_File(NULL /*stdin*/, -1);
Var_Set("MAKEFILE", "", VAR_INTERNAL, 0);
} else {
/* if we've chdir'd, rebuild the path name */
if (strcmp(curdir, objdir) && *fname != '/') {
size_t plen = strlen(curdir) + strlen(fname) + 2;
if (len < plen)
path = bmake_realloc(path, len = 2 * plen);
(void)snprintf(path, len, "%s/%s", curdir, fname);
fd = open(path, O_RDONLY);
if (fd != -1) {
fname = path;
goto found;
}
/* If curdir failed, try objdir (ala .depend) */
plen = strlen(objdir) + strlen(fname) + 2;
if (len < plen)
path = bmake_realloc(path, len = 2 * plen);
(void)snprintf(path, len, "%s/%s", objdir, fname);
fd = open(path, O_RDONLY);
if (fd != -1) {
fname = path;
goto found;
}
} else {
fd = open(fname, O_RDONLY);
if (fd != -1)
goto found;
}
/* look in -I and system include directories. */
name = Dir_FindFile(fname, parseIncPath);
if (!name)
name = Dir_FindFile(fname,
Lst_IsEmpty(sysIncPath) ? defIncPath : sysIncPath);
if (!name || (fd = open(name, O_RDONLY)) == -1) {
if (name)
free(name);
free(path);
return(-1);
}
fname = name;
/*
* set the MAKEFILE variable desired by System V fans -- the
* placement of the setting here means it gets set to the last
* makefile specified, as it is set by SysV make.
*/
found:
if (!doing_depend)
Var_Set("MAKEFILE", fname, VAR_INTERNAL, 0);
Parse_File(fname, fd);
}
free(path);
return(0);
}
/*-
* Cmd_Exec --
* Execute the command in cmd, and return the output of that command
* in a string.
*
* Results:
* A string containing the output of the command, or the empty string
* If errnum is not NULL, it contains the reason for the command failure
*
* Side Effects:
* The string must be freed by the caller.
*/
char *
Cmd_Exec(const char *cmd, const char **errnum)
{
const char *args[4]; /* Args for invoking the shell */
int fds[2]; /* Pipe streams */
int cpid; /* Child PID */
int pid; /* PID from wait() */
char *res; /* result */
WAIT_T status; /* command exit status */
Buffer buf; /* buffer to store the result */
char *cp;
int cc;
*errnum = NULL;
if (!shellName)
Shell_Init();
/*
* Set up arguments for shell
*/
args[0] = shellName;
args[1] = "-c";
args[2] = cmd;
args[3] = NULL;
/*
* Open a pipe for fetching its output
*/
if (pipe(fds) == -1) {
*errnum = "Couldn't create pipe for \"%s\"";
goto bad;
}
/*
* Fork
*/
switch (cpid = vFork()) {
case 0:
/*
* Close input side of pipe
*/
(void)close(fds[0]);
/*
* Duplicate the output stream to the shell's output, then
* shut the extra thing down. Note we don't fetch the error
* stream...why not? Why?
*/
(void)dup2(fds[1], 1);
(void)close(fds[1]);
Var_ExportVars();
(void)execv(shellPath, UNCONST(args));
_exit(1);
/*NOTREACHED*/
case -1:
*errnum = "Couldn't exec \"%s\"";
goto bad;
default:
/*
* No need for the writing half
*/
(void)close(fds[1]);
Buf_Init(&buf, 0);
do {
char result[BUFSIZ];
cc = read(fds[0], result, sizeof(result));
if (cc > 0)
Buf_AddBytes(&buf, cc, result);
}
while (cc > 0 || (cc == -1 && errno == EINTR));
/*
* Close the input side of the pipe.
*/
(void)close(fds[0]);
/*
* Wait for the process to exit.
*/
while(((pid = waitpid(cpid, &status, 0)) != cpid) && (pid >= 0)) {
JobReapChild(pid, status, FALSE);
continue;
}
cc = Buf_Size(&buf);
res = Buf_Destroy(&buf, FALSE);
if (cc == 0)
*errnum = "Couldn't read shell's output for \"%s\"";
if (WIFSIGNALED(status))
*errnum = "\"%s\" exited on a signal";
else if (WEXITSTATUS(status) != 0)
*errnum = "\"%s\" returned non-zero status";
/*
* Null-terminate the result, convert newlines to spaces and
* install it in the variable.
*/
res[cc] = '\0';
cp = &res[cc];
if (cc > 0 && *--cp == '\n') {
/*
* A final newline is just stripped
*/
*cp-- = '\0';
}
while (cp >= res) {
if (*cp == '\n') {
*cp = ' ';
}
cp--;
}
break;
}
return res;
bad:
res = bmake_malloc(1);
*res = '\0';
return res;
}
/*-
* Error --
* Print an error message given its format.
*
* Results:
* None.
*
* Side Effects:
* The message is printed.
*/
/* VARARGS */
void
Error(const char *fmt, ...)
{
va_list ap;
FILE *err_file;
err_file = debug_file;
if (err_file == stdout)
err_file = stderr;
(void)fflush(stdout);
for (;;) {
va_start(ap, fmt);
fprintf(err_file, "%s: ", progname);
(void)vfprintf(err_file, fmt, ap);
va_end(ap);
(void)fprintf(err_file, "\n");
(void)fflush(err_file);
if (err_file == stderr)
break;
err_file = stderr;
}
}
/*-
* Fatal --
* Produce a Fatal error message. If jobs are running, waits for them
* to finish.
*
* Results:
* None
*
* Side Effects:
* The program exits
*/
/* VARARGS */
void
Fatal(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
if (jobsRunning)
Job_Wait();
(void)fflush(stdout);
(void)vfprintf(stderr, fmt, ap);
va_end(ap);
(void)fprintf(stderr, "\n");
(void)fflush(stderr);
PrintOnError(NULL, NULL);
if (DEBUG(GRAPH2) || DEBUG(GRAPH3))
Targ_PrintGraph(2);
Trace_Log(MAKEERROR, 0);
exit(2); /* Not 1 so -q can distinguish error */
}
/*
* Punt --
* Major exception once jobs are being created. Kills all jobs, prints
* a message and exits.
*
* Results:
* None
*
* Side Effects:
* All children are killed indiscriminately and the program Lib_Exits
*/
/* VARARGS */
void
Punt(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
(void)fflush(stdout);
(void)fprintf(stderr, "%s: ", progname);
(void)vfprintf(stderr, fmt, ap);
va_end(ap);
(void)fprintf(stderr, "\n");
(void)fflush(stderr);
PrintOnError(NULL, NULL);
DieHorribly();
}
/*-
* DieHorribly --
* Exit without giving a message.
*
* Results:
* None
*
* Side Effects:
* A big one...
*/
void
DieHorribly(void)
{
if (jobsRunning)
Job_AbortAll();
if (DEBUG(GRAPH2))
Targ_PrintGraph(2);
Trace_Log(MAKEERROR, 0);
exit(2); /* Not 1, so -q can distinguish error */
}
/*
* Finish --
* Called when aborting due to errors in child shell to signal
* abnormal exit.
*
* Results:
* None
*
* Side Effects:
* The program exits
*/
void
Finish(int errors)
/* number of errors encountered in Make_Make */
{
Fatal("%d error%s", errors, errors == 1 ? "" : "s");
}
/*
* eunlink --
* Remove a file carefully, avoiding directories.
*/
int
eunlink(const char *file)
{
struct stat st;
if (lstat(file, &st) == -1)
return -1;
if (S_ISDIR(st.st_mode)) {
errno = EISDIR;
return -1;
}
return unlink(file);
}
/*
* execError --
* Print why exec failed, avoiding stdio.
*/
void
execError(const char *af, const char *av)
{
#ifdef USE_IOVEC
int i = 0;
struct iovec iov[8];
#define IOADD(s) \
(void)(iov[i].iov_base = UNCONST(s), \
iov[i].iov_len = strlen(iov[i].iov_base), \
i++)
#else
#define IOADD(s) (void)write(2, s, strlen(s))
#endif
IOADD(progname);
IOADD(": ");
IOADD(af);
IOADD("(");
IOADD(av);
IOADD(") failed (");
IOADD(strerror(errno));
IOADD(")\n");
#ifdef USE_IOVEC
while (writev(2, iov, 8) == -1 && errno == EAGAIN)
continue;
#endif
}
/*
* usage --
* exit with usage message
*/
static void
usage(void)
{
char *p;
if ((p = strchr(progname, '[')) != NULL)
*p = '\0';
(void)fprintf(stderr,
"usage: %s [-BeikNnqrstWwX] \n\
[-C directory] [-D variable] [-d flags] [-f makefile]\n\
[-I directory] [-J private] [-j max_jobs] [-m directory] [-T file]\n\
[-V variable] [variable=value] [target ...]\n", progname);
exit(2);
}
int
PrintAddr(void *a, void *b)
{
printf("%lx ", (unsigned long) a);
return b ? 0 : 0;
}
void
PrintOnError(GNode *gn, const char *s)
{
static GNode *en = NULL;
char tmp[64];
char *cp;
if (s)
printf("%s", s);
printf("\n%s: stopped in %s\n", progname, curdir);
if (en)
return; /* we've been here! */
if (gn) {
/*
* We can print this even if there is no .ERROR target.
*/
Var_Set(".ERROR_TARGET", gn->name, VAR_GLOBAL, 0);
}
strncpy(tmp, "${MAKE_PRINT_VAR_ON_ERROR:@v@$v='${$v}'\n@}",
sizeof(tmp) - 1);
cp = Var_Subst(NULL, tmp, VAR_GLOBAL, 0);
if (cp) {
if (*cp)
printf("%s", cp);
free(cp);
}
/*
* Finally, see if there is a .ERROR target, and run it if so.
*/
en = Targ_FindNode(".ERROR", TARG_NOCREATE);
if (en) {
en->type |= OP_SPECIAL;
Compat_Make(en, en);
}
}
void
Main_ExportMAKEFLAGS(Boolean first)
{
static int once = 1;
char tmp[64];
char *s;
if (once != first)
return;
once = 0;
strncpy(tmp, "${.MAKEFLAGS} ${.MAKEOVERRIDES:O:u:@v@$v=${$v:Q}@}",
sizeof(tmp));
s = Var_Subst(NULL, tmp, VAR_CMD, 0);
if (s && *s) {
#ifdef POSIX
setenv("MAKEFLAGS", s, 1);
#else
setenv("MAKE", s, 1);
#endif
}
}
char *
getTmpdir(void)
{
static char *tmpdir = NULL;
if (!tmpdir) {
struct stat st;
/*
* Honor $TMPDIR but only if it is valid.
* Ensure it ends with /.
*/
tmpdir = Var_Subst(NULL, "${TMPDIR:tA:U" _PATH_TMP "}/", VAR_GLOBAL, 0);
if (stat(tmpdir, &st) < 0 || !S_ISDIR(st.st_mode)) {
free(tmpdir);
tmpdir = bmake_strdup(_PATH_TMP);
}
}
return tmpdir;
}
/*
* Create and open a temp file using "pattern".
* If "fnamep" is provided set it to a copy of the filename created.
* Otherwise unlink the file once open.
*/
int
mkTempFile(const char *pattern, char **fnamep)
{
static char *tmpdir = NULL;
char tfile[MAXPATHLEN];
int fd;
if (!pattern)
pattern = TMPPAT;
if (!tmpdir)
tmpdir = getTmpdir();
if (pattern[0] == '/') {
snprintf(tfile, sizeof(tfile), "%s", pattern);
} else {
snprintf(tfile, sizeof(tfile), "%s%s", tmpdir, pattern);
}
if ((fd = mkstemp(tfile)) < 0)
Punt("Could not create temporary file %s: %s", tfile, strerror(errno));
if (fnamep) {
*fnamep = bmake_strdup(tfile);
} else {
unlink(tfile); /* we just want the descriptor */
}
return fd;
}
/*
* Return a Boolean based on setting of a knob.
*
* If the knob is not set, the supplied default is the return value.
* If set, anything that looks or smells like "No", "False", "Off", "0" etc,
* is FALSE, otherwise TRUE.
*/
Boolean
getBoolean(const char *name, Boolean bf)
{
char tmp[64];
char *cp;
if (snprintf(tmp, sizeof(tmp), "${%s:tl}", name) < (int)(sizeof(tmp))) {
cp = Var_Subst(NULL, tmp, VAR_GLOBAL, 0);
if (cp) {
switch(*cp) {
case '\0': /* not set - the default wins */
break;
case '0':
case 'f':
case 'n':
bf = FALSE;
break;
case 'o':
switch (cp[1]) {
case 'f':
bf = FALSE;
break;
default:
bf = TRUE;
break;
}
break;
default:
bf = TRUE;
break;
}
free(cp);
}
}
return (bf);
}
|