Changeset 287 for trunk/src/gmake/main.c
- Timestamp:
- May 17, 2005, 1:34:55 AM (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/gmake/main.c
r218 r287 1 1 /* Argument parsing and main program of GNU Make. 2 2 Copyright (C) 1988, 1989, 1990, 1991, 1994, 1995, 1996, 1997, 1998, 1999, 3 2002, 2003 Free Software Foundation, Inc.3 2002, 2003, 2005 Free Software Foundation, Inc. 4 4 This file is part of GNU Make. 5 5 … … 36 36 #ifdef WINDOWS32 37 37 #include <windows.h> 38 #include <io.h> 38 39 #include "pathstuff.h" 39 40 #endif … … 44 45 #ifdef HAVE_FCNTL_H 45 46 # include <fcntl.h> 47 #endif 48 49 #if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT) 50 # define SET_STACK_SIZE 51 #endif 52 53 #ifdef SET_STACK_SIZE 54 # include <sys/resource.h> 46 55 #endif 47 56 … … 186 195 int default_keep_going_flag = 0; 187 196 197 /* Nonzero means check symlink mtimes. */ 198 199 int check_symlink_flag = 0; 200 188 201 /* Nonzero means print directory before starting and when done (-w). */ 189 202 … … 207 220 unsigned int job_slots = 1; 208 221 unsigned int default_job_slots = 1; 222 static unsigned int master_job_slots = 0; 209 223 210 224 /* Value of job_slots that means no limit. */ … … 260 274 261 275 int always_make_flag = 0; 276 277 /* If nonzero, we're in the "try to rebuild makefiles" phase. */ 278 279 int rebuilding_makefiles = 0; 280 281 /* Remember the original value of the SHELL variable, from the environment. */ 282 283 struct variable shell_var; 284 262 285 263 286 … … 301 324 Don't start multiple jobs unless load is below N.\n"), 302 325 N_("\ 326 -L, --check-symlink-times Use the latest mtime between symlinks and target.\n"), 327 N_("\ 303 328 -n, --just-print, --dry-run, --recon\n\ 304 329 Don't actually run any commands; just print them.\n"), … … 348 373 #endif 349 374 { 'e', flag, (char *) &env_overrides, 1, 1, 0, 0, 0, 350 375 "environment-overrides", }, 351 376 { 'f', string, (char *) &makefiles, 0, 0, 0, 0, 0, "file" }, 352 377 { 'h', flag, (char *) &print_usage_flag, 0, 0, 0, 0, 0, "help" }, 353 378 { 'i', flag, (char *) &ignore_errors_flag, 1, 1, 0, 0, 0, 354 379 "ignore-errors" }, 355 380 { 'I', string, (char *) &include_directories, 1, 1, 0, 0, 0, 356 381 "include-dir" }, 357 382 { 'j', positive_int, (char *) &job_slots, 1, 1, 0, (char *) &inf_jobs, 358 383 (char *) &default_job_slots, "jobs" }, 359 384 { CHAR_MAX+2, string, (char *) &jobserver_fds, 1, 1, 0, 0, 0, 360 385 "jobserver-fds" }, 361 386 { 'k', flag, (char *) &keep_going_flag, 1, 1, 0, 0, 362 387 (char *) &default_keep_going_flag, "keep-going" }, 363 388 #ifndef NO_FLOAT 364 389 { 'l', floating, (char *) &max_load_average, 1, 1, 0, 365 366 390 (char *) &default_load_average, (char *) &default_load_average, 391 "load-average" }, 367 392 #else 368 393 { 'l', positive_int, (char *) &max_load_average, 1, 1, 0, 369 (char *) &default_load_average, (char *) &default_load_average, 370 "load-average" }, 371 #endif 394 (char *) &default_load_average, (char *) &default_load_average, 395 "load-average" }, 396 #endif 397 { 'L', flag, (char *) &check_symlink_flag, 1, 1, 0, 0, 0, 398 "check-symlink-times" }, 372 399 { 'm', ignore, 0, 0, 0, 0, 0, 0, 0 }, 373 400 { 'n', flag, (char *) &just_print_flag, 1, 1, 1, 0, 0, "just-print" }, 374 401 { 'o', string, (char *) &old_files, 0, 0, 0, 0, 0, "old-file" }, 375 402 { 'p', flag, (char *) &print_data_base_flag, 1, 1, 0, 0, 0, 376 403 "print-data-base" }, 377 404 { 'q', flag, (char *) &question_flag, 1, 1, 1, 0, 0, "question" }, 378 405 { 'r', flag, (char *) &no_builtin_rules_flag, 1, 1, 0, 0, 0, 379 406 "no-builtin-rules" }, 380 407 { 'R', flag, (char *) &no_builtin_variables_flag, 1, 1, 0, 0, 0, 381 408 "no-builtin-variables" }, 382 409 { 's', flag, (char *) &silent_flag, 1, 1, 0, 0, 0, "silent" }, 383 410 { 'S', flag_off, (char *) &keep_going_flag, 1, 1, 0, 0, … … 386 413 { 'v', flag, (char *) &print_version_flag, 1, 1, 0, 0, 0, "version" }, 387 414 { 'w', flag, (char *) &print_directory_flag, 1, 1, 0, 0, 0, 388 415 "print-directory" }, 389 416 { CHAR_MAX+3, flag, (char *) &inhibit_print_directory_flag, 1, 1, 0, 0, 0, 390 417 "no-print-directory" }, 391 418 { 'W', string, (char *) &new_files, 0, 0, 0, 0, 0, "what-if" }, 392 419 { CHAR_MAX+4, flag, (char *) &warn_undefined_variables_flag, 1, 1, 0, 0, 0, 393 420 "warn-undefined-variables" }, 394 421 { 0 } 395 422 }; … … 446 473 447 474 struct file *default_goal_file; 475 476 /* Pointer to the value of the .DEFAULT_GOAL special 477 variable. */ 478 char ** default_goal_name; 448 479 449 480 /* Pointer to structure for the file .DEFAULT … … 551 582 /* Toggle -d on receipt of SIGUSR1. */ 552 583 584 #ifdef SIGUSR1 553 585 static RETSIGTYPE 554 586 debug_signal_handler (int sig UNUSED) … … 556 588 db_level = db_level ? DB_NONE : DB_BASIC; 557 589 } 590 #endif 558 591 559 592 static void … … 701 734 { 702 735 int sh_found = 0; 703 char* search_token; 736 char *search_token; 737 char *tokend; 704 738 PATH_VAR(sh_path); 705 739 extern char *default_shell; … … 710 744 search_token = token; 711 745 712 if (!no_default_sh_exe && 713 (token == NULL || !strcmp(search_token, default_shell))) { 746 747 /* If the user explicitly requests the DOS cmd shell, obey that request. 748 However, make sure that's what they really want by requiring the value 749 of SHELL either equal, or have a final path element of, "cmd" or 750 "cmd.exe" case-insensitive. */ 751 tokend = search_token + strlen (search_token) - 3; 752 if (((tokend == search_token 753 || (tokend > search_token 754 && (tokend[-1] == '/' || tokend[-1] == '\\'))) 755 && !strcmpi (tokend, "cmd")) 756 || ((tokend - 4 == search_token 757 || (tokend - 4 > search_token 758 && (tokend[-5] == '/' || tokend[-5] == '\\'))) 759 && !strcmpi (tokend - 4, "cmd.exe"))) { 760 batch_mode_shell = 1; 761 unixy_shell = 0; 762 sh_found = 0; 763 } else if (!no_default_sh_exe && 764 (token == NULL || !strcmp (search_token, default_shell))) { 714 765 /* no new information, path already set or known */ 715 766 sh_found = 1; … … 723 774 } else { 724 775 char *p; 725 struct variable *v = lookup_variable ("Path", 4); 726 727 /* 728 * Search Path for shell 729 */ 776 struct variable *v = lookup_variable ("PATH", 4); 777 778 /* Search Path for shell */ 730 779 if (v && v->value) { 731 780 char *ep; … … 846 895 struct file *f; 847 896 int i; 897 int makefile_status = MAKE_SUCCESS; 848 898 char **p; 849 899 struct dep *read_makefiles; … … 858 908 unixy_shell = 0; 859 909 no_default_sh_exe = 1; 910 #endif 911 912 #ifdef SET_STACK_SIZE 913 /* Get rid of any avoidable limit on stack size. */ 914 { 915 struct rlimit rlim; 916 917 /* Set the stack limit huge so that alloca does not fail. */ 918 if (getrlimit (RLIMIT_STACK, &rlim) == 0) 919 { 920 rlim.rlim_cur = rlim.rlim_max; 921 setrlimit (RLIMIT_STACK, &rlim); 922 } 923 } 860 924 #endif 861 925 … … 983 1047 program = argv[0] + 1; 984 1048 #endif 1049 #ifdef WINDOWS32 1050 if (program == 0) 1051 { 1052 /* Extract program from full path */ 1053 int argv0_len; 1054 char *p = strrchr (argv[0], '\\'); 1055 if (!p) 1056 p = argv[0]; 1057 argv0_len = strlen(p); 1058 if (argv0_len > 4 1059 && streq (&p[argv0_len - 4], ".exe")) 1060 { 1061 /* Remove .exe extension */ 1062 p[argv0_len - 4] = '\0'; 1063 /* Increment past the initial '\' */ 1064 program = p + 1; 1065 } 1066 } 1067 #endif 985 1068 if (program == 0) 986 1069 program = argv[0]; … … 1003 1086 { 1004 1087 #ifdef HAVE_GETCWD 1005 perror_with_name ("getcwd :", "");1088 perror_with_name ("getcwd", ""); 1006 1089 #else 1007 1090 error (NILF, "getwd: %s", current_directory); … … 1019 1102 /* Initialize the special variables. */ 1020 1103 define_variable (".VARIABLES", 10, "", o_default, 0)->special = 1; 1021 /* define_variable (".TARGETS", 8, "", o_default, 0); */ 1104 /* define_variable (".TARGETS", 8, "", o_default, 0)->special = 1; */ 1105 1106 /* Set up .FEATURES */ 1107 define_variable (".FEATURES", 9, 1108 "target-specific order-only second-expansion", 1109 o_default, 0); 1110 #ifdef MAKE_JOBSERVER 1111 do_variable_definition (NILF, ".FEATURES", "jobserver", 1112 o_default, f_append, 0); 1113 #endif 1114 #ifdef MAKE_SYMLINKS 1115 do_variable_definition (NILF, ".FEATURES", "check-symlink", 1116 o_default, f_append, 0); 1117 #endif 1022 1118 1023 1119 /* Read in variables from the environment. It is important that this be … … 1028 1124 for (i = 0; envp[i] != 0; ++i) 1029 1125 { 1030 int do_not_define; 1031 register char *ep = envp[i]; 1032 1033 /* by default, everything gets defined and exported */ 1034 do_not_define = 0; 1035 1036 while (*ep != '=') 1126 int do_not_define = 0; 1127 char *ep = envp[i]; 1128 1129 while (*ep != '\0' && *ep != '=') 1037 1130 ++ep; 1038 1131 #ifdef WINDOWS32 1039 1132 if (!unix_path && strneq(envp[i], "PATH=", 5)) 1040 1133 unix_path = ep+1; 1041 else if (! windows32_path && !strnicmp(envp[i], "Path=", 5)) {1134 else if (!strnicmp(envp[i], "Path=", 5)) { 1042 1135 do_not_define = 1; /* it gets defined after loop exits */ 1043 windows32_path = ep+1; 1136 if (!windows32_path) 1137 windows32_path = ep+1; 1044 1138 } 1045 1139 #endif … … 1048 1142 the same. */ 1049 1143 if (!do_not_define) 1050 define_variable (envp[i], (unsigned int) (ep - envp[i]), 1051 ep + 1, o_env, 1) 1052 /* Force exportation of every variable culled from the environment. 1053 We used to rely on target_environment's v_default code to do this. 1054 But that does not work for the case where an environment variable 1055 is redefined in a makefile with `override'; it should then still 1056 be exported, because it was originally in the environment. */ 1057 ->export = v_export; 1144 { 1145 struct variable *v; 1146 1147 v = define_variable (envp[i], (unsigned int) (ep - envp[i]), 1148 ep + 1, o_env, 1); 1149 /* Force exportation of every variable culled from the environment. 1150 We used to rely on target_environment's v_default code to do this. 1151 But that does not work for the case where an environment variable 1152 is redefined in a makefile with `override'; it should then still 1153 be exported, because it was originally in the environment. */ 1154 v->export = v_export; 1155 1156 /* Another wrinkle is that POSIX says the value of SHELL set in the 1157 makefile should not change the value of SHELL given to 1158 subprocesses, which seems silly to me but... */ 1159 if (strncmp (envp[i], "SHELL=", 6) == 0) 1160 { 1161 #ifndef __MSDOS__ 1162 v->export = v_noexport; 1163 #endif 1164 shell_var.name = "SHELL"; 1165 shell_var.value = xstrdup (ep + 1); 1166 } 1167 } 1058 1168 } 1059 1169 #ifdef WINDOWS32 1060 /* 1061 * Make sure that this particular spelling of 'Path' is available1170 /* If we didn't find a correctly spelled PATH we define PATH as 1171 * either the first mispelled value or an empty string 1062 1172 */ 1063 if (windows32_path) 1064 define_variable("Path", 4, windows32_path, o_env, 1)->export = v_export; 1065 else if (unix_path) 1066 define_variable("Path", 4, unix_path, o_env, 1)->export = v_export; 1067 else 1068 define_variable("Path", 4, "", o_env, 1)->export = v_export; 1069 1070 /* 1071 * PATH defaults to Path iff PATH not found and Path is found. 1072 */ 1073 if (!unix_path && windows32_path) 1074 define_variable("PATH", 4, windows32_path, o_env, 1)->export = v_export; 1173 if (!unix_path) 1174 define_variable("PATH", 4, 1175 windows32_path ? windows32_path : "", 1176 o_env, 1)->export = v_export; 1075 1177 #endif 1076 1178 #else /* For Amiga, read the ENV: device, ignoring all dirs */ … … 1242 1344 { 1243 1345 char *dir = directories->list[i]; 1346 char *expanded = 0; 1244 1347 if (dir[0] == '~') 1245 1348 { 1246 char *expanded = tilde_expand (dir);1349 expanded = tilde_expand (dir); 1247 1350 if (expanded != 0) 1248 1351 dir = expanded; 1249 1352 } 1353 #ifdef WINDOWS32 1354 /* WINDOWS32 chdir() doesn't work if the directory has a trailing '/' 1355 But allow -C/ just in case someone wants that. */ 1356 { 1357 char *p = dir + strlen (dir) - 1; 1358 while (p > dir && (p[0] == '/' || p[0] == '\\')) 1359 --p; 1360 p[1] = '\0'; 1361 } 1362 #endif 1250 1363 if (chdir (dir) < 0) 1251 1364 pfatal_with_name (dir); 1252 if ( dir != directories->list[i])1253 free ( dir);1365 if (expanded) 1366 free (expanded); 1254 1367 } 1255 1368 … … 1306 1419 { 1307 1420 #ifdef HAVE_GETCWD 1308 perror_with_name ("getcwd :", "");1421 perror_with_name ("getcwd", ""); 1309 1422 #else 1310 1423 error (NILF, "getwd: %s", current_directory); … … 1373 1486 if (outfile == 0) 1374 1487 pfatal_with_name (_("fopen (temporary file)")); 1375 while (!feof (stdin) )1488 while (!feof (stdin) && ! ferror (stdin)) 1376 1489 { 1377 1490 char buf[2048]; … … 1453 1566 define_default_variables (); 1454 1567 1568 default_file = enter_file (".DEFAULT"); 1569 1570 { 1571 struct variable *v = define_variable (".DEFAULT_GOAL", 13, "", o_file, 0); 1572 default_goal_name = &v->value; 1573 } 1574 1455 1575 /* Read all the makefiles. */ 1456 1457 default_file = enter_file (".DEFAULT");1458 1576 1459 1577 read_makefiles … … 1585 1703 want job_slots to be 0 to indicate we're using the jobserver. */ 1586 1704 1705 master_job_slots = job_slots; 1706 1587 1707 while (--job_slots) 1588 1708 { … … 1604 1724 jobserver_fds->idx = 1; 1605 1725 jobserver_fds->max = 1; 1726 } 1727 #endif 1728 1729 #ifndef MAKE_SYMLINKS 1730 if (check_symlink_flag) 1731 { 1732 error (NILF, _("Symbolic links not supported: disabling -L.")); 1733 check_symlink_flag = 0; 1606 1734 } 1607 1735 #endif … … 1673 1801 int nargc = argc; 1674 1802 int orig_db_level = db_level; 1803 int status; 1675 1804 1676 1805 if (! ISDB (DB_MAKEFILES)) … … 1733 1862 define_makeflags (1, 1); 1734 1863 1735 switch (update_goal_chain (read_makefiles, 1)) 1864 rebuilding_makefiles = 1; 1865 status = update_goal_chain (read_makefiles); 1866 rebuilding_makefiles = 0; 1867 1868 switch (status) 1736 1869 { 1737 1870 case 1: … … 1782 1915 any_remade |= (mtime != NONEXISTENT_MTIME 1783 1916 && mtime != makefile_mtimes[i]); 1917 makefile_status = MAKE_FAILURE; 1784 1918 } 1785 1919 } … … 1871 2005 #ifndef _AMIGA 1872 2006 for (p = environ; *p != 0; ++p) 1873 if ( (*p)[MAKELEVEL_LENGTH] == '='1874 && strneq (*p, MAKELEVEL_NAME, MAKELEVEL_LENGTH))2007 if (strneq (*p, MAKELEVEL_NAME, MAKELEVEL_LENGTH) 2008 && (*p)[MAKELEVEL_LENGTH] == '=') 1875 2009 { 1876 2010 /* The SGI compiler apparently can't understand … … 1927 2061 int pid; 1928 2062 int status; 1929 pid = child_execute_job (0, 1, nargv, environ, NULL);2063 pid = child_execute_job (0, 1, nargv, environ, NULL); 1930 2064 1931 2065 /* is this loop really necessary? */ 1932 2066 do { 1933 pid = wait (&status);1934 } while (pid <= 0);2067 pid = wait (&status); 2068 } while (pid <= 0); 1935 2069 /* use the exit code of the child process */ 1936 exit (WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE);2070 exit (WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE); 1937 2071 } 1938 2072 #else … … 1948 2082 1949 2083 db_level = orig_db_level; 2084 2085 /* Free the makefile mtimes (if we allocated any). */ 2086 if (makefile_mtimes) 2087 free ((char *) makefile_mtimes); 1950 2088 } 1951 2089 … … 1964 2102 if (goals == 0) 1965 2103 { 1966 if (default_goal_file != 0) 1967 { 1968 goals = (struct dep *) xmalloc (sizeof (struct dep)); 1969 goals->next = 0; 1970 goals->name = 0; 2104 if (**default_goal_name != '\0') 2105 { 2106 if (default_goal_file == 0 || 2107 strcmp (*default_goal_name, default_goal_file->name) != 0) 2108 { 2109 default_goal_file = lookup_file (*default_goal_name); 2110 2111 /* In case user set .DEFAULT_GOAL to a non-existent target 2112 name let's just enter this name into the table and let 2113 the standard logic sort it out. */ 2114 if (default_goal_file == 0) 2115 { 2116 struct nameseq *ns; 2117 char *p = *default_goal_name; 2118 2119 ns = multi_glob ( 2120 parse_file_seq (&p, '\0', sizeof (struct nameseq), 1), 2121 sizeof (struct nameseq)); 2122 2123 /* .DEFAULT_GOAL should contain one target. */ 2124 if (ns->next != 0) 2125 fatal (NILF, _(".DEFAULT_GOAL contains more than one target")); 2126 2127 default_goal_file = enter_file (ns->name); 2128 2129 ns->name = 0; /* It was reused by enter_file(). */ 2130 free_ns_chain (ns); 2131 } 2132 } 2133 2134 goals = (struct dep *) xmalloc (sizeof (struct dep)); 2135 goals->next = 0; 2136 goals->name = 0; 1971 2137 goals->ignore_mtime = 0; 1972 goals->file = default_goal_file; 1973 } 2138 goals->need_2nd_expansion = 0; 2139 goals->file = default_goal_file; 2140 } 1974 2141 } 1975 2142 else 1976 2143 lastgoal->next = 0; 2144 1977 2145 1978 2146 if (!goals) … … 1988 2156 DB (DB_BASIC, (_("Updating goal targets....\n"))); 1989 2157 1990 switch (update_goal_chain (goals , 0))2158 switch (update_goal_chain (goals)) 1991 2159 { 1992 2160 case -1: … … 1994 2162 case 0: 1995 2163 /* Updated successfully. */ 1996 status = MAKE_SUCCESS;2164 status = makefile_status; 1997 2165 break; 1998 2166 case 1: … … 2098 2266 if (v != 0) 2099 2267 { 2100 /* It is indeed a variable definition. Record a pointer to 2101 the variable for later use in define_makeflags. */ 2102 struct command_variable *cv 2103 = (struct command_variable *) xmalloc (sizeof (*cv)); 2104 cv->variable = v; 2105 cv->next = command_variables; 2106 command_variables = cv; 2268 /* It is indeed a variable definition. If we don't already have this 2269 one, record a pointer to the variable for later use in 2270 define_makeflags. */ 2271 struct command_variable *cv; 2272 2273 for (cv = command_variables; cv != 0; cv = cv->next) 2274 if (cv->variable == v) 2275 break; 2276 2277 if (! cv) { 2278 cv = (struct command_variable *) xmalloc (sizeof (*cv)); 2279 cv->variable = v; 2280 cv->next = command_variables; 2281 command_variables = cv; 2282 } 2107 2283 } 2108 2284 else if (! env) … … 2127 2303 lastgoal->file = f; 2128 2304 lastgoal->ignore_mtime = 0; 2305 lastgoal->need_2nd_expansion = 0; 2129 2306 2130 2307 { … … 2775 2952 if (!dying) 2776 2953 { 2954 char token = '+'; 2777 2955 int err; 2778 2956 … … 2794 2972 if (print_data_base_flag) 2795 2973 print_data_base (); 2974 2975 /* Sanity: have we written all our jobserver tokens back? If our 2976 exit status is 2 that means some kind of syntax error; we might not 2977 have written all our tokens so do that now. If tokens are left 2978 after any other error code, that's bad. */ 2979 2980 if (job_fds[0] != -1 && jobserver_tokens) 2981 { 2982 if (status != 2) 2983 error (NILF, 2984 "INTERNAL: Exiting with %u jobserver tokens (should be 0)!", 2985 jobserver_tokens); 2986 else 2987 while (jobserver_tokens--) 2988 { 2989 int r; 2990 2991 EINTRLOOP (r, write (job_fds[1], &token, 1)); 2992 if (r != 1) 2993 perror_with_name ("write", ""); 2994 } 2995 } 2996 2997 2998 /* Sanity: If we're the master, were all the tokens written back? */ 2999 3000 if (master_job_slots) 3001 { 3002 /* We didn't write one for ourself, so start at 1. */ 3003 unsigned int tcnt = 1; 3004 3005 /* Close the write side, so the read() won't hang. */ 3006 close (job_fds[1]); 3007 3008 while ((err = read (job_fds[0], &token, 1)) == 1) 3009 ++tcnt; 3010 3011 if (tcnt != master_job_slots) 3012 error (NILF, 3013 "INTERNAL: Exiting with %u jobserver tokens available; should be %u!", 3014 tcnt, master_job_slots); 3015 } 2796 3016 2797 3017 /* Try to move back to the original directory. This is essential on … … 2859 3079 printf (_("%s[%u]: Leaving directory `%s'\n"), 2860 3080 program, makelevel, starting_directory); 3081 3082 /* Flush stdout to be sure this comes before any stderr output. */ 3083 fflush (stdout); 2861 3084 }
Note:
See TracChangeset
for help on using the changeset viewer.