1 /* Copyright (C) 1988, 1989, 1990, 1991 Free Software Foundation, Inc.
2 This file is part of GNU Make.
4 GNU Make is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 GNU Make is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with GNU Make; see the file COPYING. If not, write to
16 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
27 extern char *version_string
;
29 extern struct dep
*read_all_makefiles ();
31 extern void print_variable_data_base ();
32 extern void print_dir_data_base ();
33 extern void print_rule_data_base ();
34 extern void print_file_data_base ();
35 extern void print_vpath_data_base ();
41 #ifndef sun /* Sun has an incorrect decl in a header. */
44 extern double atof ();
46 extern char *mktemp ();
48 static void log_working_directory ();
49 static void print_data_base (), print_version ();
50 static void decode_switches (), decode_env_switches ();
51 static void define_makeflags ();
58 * -b ignored for compatibility with System V Make
67 * -m ignored for compatibility with something or other
69 * -o consider file old
76 * -v print version information
77 * -w log working directory
78 * -W consider file new (with -n, `what' if effect)
81 /* The structure that describes an accepted command switch. */
85 char c
; /* The switch character. */
87 enum /* Type of the value. */
89 flag
, /* Turn int flag on. */
90 flag_off
, /* Turn int flag off. */
91 string
, /* One string per switch. */
92 positive_int
, /* A positive integer. */
93 floating
, /* A floating-point number (double). */
94 ignore
, /* Ignored. */
95 usage_and_exit
/* Ignored; exit after processing switches. */
98 char *value_ptr
; /* Pointer to the value-holding variable. */
100 unsigned int env
:1; /* Can come from MAKEFLAGS. */
101 unsigned int toenv
:1; /* Should be put in MAKEFLAGS. */
102 unsigned int no_makefile
:1; /* Don't propagate when remaking makefiles. */
104 char *noarg_value
; /* Pointer to value used if no argument is given. */
105 char *default_value
;/* Pointer to default value. */
107 char *long_name
; /* Long option name. */
108 char *argdesc
; /* Descriptive word for argument. */
109 char *description
; /* Description for usage message. */
113 /* The structure used to hold the list of strings given
114 in command switches of a type that takes string arguments. */
118 char **list
; /* Nil-terminated list of strings. */
119 unsigned int idx
; /* Index into above. */
120 unsigned int max
; /* Number of pointers allocated. */
124 /* The recognized command switches. */
126 /* Nonzero means do not print commands to be executed (-s). */
130 /* Nonzero means just touch the files
131 that would appear to need remaking (-t) */
135 /* Nonzero means just print what commands would need to be executed,
136 don't actually execute them (-n). */
140 /* Print debugging trace info (-d). */
144 /* Environment variables override makefile definitions. */
146 int env_overrides
= 0;
148 /* Nonzero means ignore status codes returned by commands
149 executed to remake files. Just treat them all as successful (-i). */
151 int ignore_errors_flag
= 0;
153 /* Nonzero means don't remake anything, just print the data base
154 that results from reading the makefile (-p). */
156 int print_data_base_flag
= 0;
158 /* Nonzero means don't remake anything; just return a nonzero status
159 if the specified targets are not up to date (-q). */
161 int question_flag
= 0;
163 /* Nonzero means do not use any of the builtin rules (-r). */
165 int no_builtin_rules_flag
= 0;
167 /* Nonzero means keep going even if remaking some file fails (-k). */
170 int default_keep_going_flag
= 0;
172 /* Nonzero means print directory before starting and when done (-w). */
174 int print_directory_flag
= 0;
176 /* Nonzero means ignore print_directory_flag and never print the directory.
177 This is necessary because print_directory_flag is set implicitly. */
179 int inhibit_print_directory_flag
= 0;
181 /* Nonzero means print version information. */
183 int print_version_flag
= 0;
185 /* List of makefiles given with -f switches. */
187 static struct stringlist
*makefiles
= 0;
190 /* Number of job slots (commands that can be run at once). */
192 unsigned int job_slots
= 1;
193 unsigned int default_job_slots
= 1;
195 /* Value of job_slots that means no limit. */
197 static unsigned int inf_jobs
= 0;
199 /* Maximum load average at which multiple jobs will be run.
200 Negative values mean unlimited, while zero means limit to
201 zero load (which could be useful to start infinite jobs remotely
202 but one at a time locally). */
204 double max_load_average
= -1.0;
205 double default_load_average
= -1.0;
207 /* List of directories given with -c switches. */
209 static struct stringlist
*directories
= 0;
211 /* List of include directories given with -I switches. */
213 static struct stringlist
*include_directories
= 0;
215 /* List of files given with -o switches. */
217 static struct stringlist
*old_files
= 0;
219 /* List of files given with -W switches. */
221 static struct stringlist
*new_files
= 0;
223 /* The table of command switches. */
225 static const struct command_switch switches
[] =
227 { 'b', ignore
, 0, 0, 0, 0, 0, 0,
229 "Ignored for compatibility" },
230 { 'C', string
, (char *) &directories
, 0, 0, 0, 0, 0,
231 "directory", "DIRECTORY",
232 "Change to DIRECTORY before doing anything" },
233 { 'd', flag
, (char *) &debug_flag
, 1, 1, 0, 0, 0,
235 "Print lots of debugging information" },
236 { 'e', flag
, (char *) &env_overrides
, 1, 1, 0, 0, 0,
237 "environment-overrides", 0,
238 "Environment variables override makefiles" },
239 { 'f', string
, (char *) &makefiles
, 0, 0, 0, 0, 0,
241 "Read FILE as a makefile" },
242 { 'h', usage_and_exit
, 0, 0, 0, 0, 0, 0,
244 "Print this message and exit" },
245 { 'i', flag
, (char *) &ignore_errors_flag
, 1, 1, 0, 0, 0,
247 "Ignore errors from commands" },
248 { 'I', string
, (char *) &include_directories
, 1, 1, 0, 0, 0,
249 "include-dir", "DIRECTORY",
250 "Search DIRECTORY for included makefiles" },
251 { 'j', positive_int
, (char *) &job_slots
, 1, 1, 0,
252 (char *) &inf_jobs
, (char *) &default_job_slots
,
254 "Allow N jobs at once; infinite jobs with no arg" },
255 { 'k', flag
, (char *) &keep_going_flag
, 1, 1, 0,
256 0, (char *) &default_keep_going_flag
,
258 "Keep going when some targets can't be made" },
259 { 'l', floating
, (char *) &max_load_average
, 1, 1, 0,
260 (char *) &default_load_average
, (char *) &default_load_average
,
262 "Don't start multiple jobs unless load is below N" },
263 { 'm', ignore
, 0, 0, 0, 0, 0, 0,
266 { 'n', flag
, (char *) &just_print_flag
, 1, 1, 1, 0, 0,
268 "Don't actually run any commands; just print them" },
269 { 'o', string
, (char *) &old_files
, 0, 0, 0, 0, 0,
271 "Consider FILE to be very old and don't remake it" },
272 { 'p', flag
, (char *) &print_data_base_flag
, 1, 1, 0, 0, 0,
273 "print-data-base", 0,
274 "Print make's internal database" },
275 { 'q', flag
, (char *) &question_flag
, 1, 1, 1, 0, 0,
277 "Run no commands; exit status says if up to date" },
278 { 'r', flag
, (char *) &no_builtin_rules_flag
, 1, 1, 0, 0, 0,
279 "no-builtin-rules", 0,
280 "Disable the built-in implicit rules" },
281 { 's', flag
, (char *) &silent_flag
, 1, 1, 0, 0, 0,
283 "Don't echo commands" },
284 { 'S', flag_off
, (char *) &keep_going_flag
, 1, 1, 0,
285 0, (char *) &default_keep_going_flag
,
288 { 't', flag
, (char *) &touch_flag
, 1, 1, 1, 0, 0,
290 "Touch targets instead of remaking them" },
291 { 'v', flag
, (char *) &print_version_flag
, 1, 1, 0, 0, 0,
293 "Print the version number of make" },
294 { 'w', flag
, (char *) &print_directory_flag
, 1, 1, 0, 0, 0,
295 "print-directory", 0,
296 "Print the current directory" },
297 { 1, flag
, (char *) &inhibit_print_directory_flag
, 1, 1, 0, 0, 0,
298 "no-print-directory", 0,
299 "Turn off -w, even if it was turned on implicitly" },
300 { 'W', string
, (char *) &new_files
, 0, 0, 0, 0, 0,
302 "Consider FILE to be infinitely new" },
306 /* Secondary long names for options. */
308 static struct option long_option_aliases
[] =
310 { "quiet", no_argument
, 0, 's' },
311 { "stop", no_argument
, 0, 'S' },
312 { "new-file", required_argument
, 0, 'W' },
313 { "assume-new", required_argument
, 0, 'W' },
314 { "assume-old", required_argument
, 0, 'o' },
315 { "max-load", optional_argument
, 0, 'l' },
316 { "dry-run", no_argument
, 0, 'n' },
317 { "recon", no_argument
, 0, 'n' },
318 { "makefile", required_argument
, 0, 'f' },
321 /* The usage message prints the descriptions of options starting in
322 this column. Make sure it leaves enough room for the longest
323 description to fit in less than 80 characters. */
325 #define DESCRIPTION_COLUMN 30
327 /* List of non-switch arguments. */
329 struct stringlist
*other_args
= 0;
331 /* The name we were invoked with. */
335 /* Our current directory after processing all -C options. */
337 char *starting_directory
;
339 /* Value of the MAKELEVEL variable at startup (or 0). */
341 unsigned int makelevel
;
343 /* First file defined in the makefile whose name does not
344 start with `.'. This is the default to remake if the
345 command line does not specify. */
347 struct file
*default_goal_file
;
349 /* Pointer to structure for the file .DEFAULT
350 whose commands are used for any file that has none of its own.
351 This is zero if the makefiles do not define .DEFAULT. */
353 struct file
*default_file
;
355 /* Mask of signals that are being caught with fatal_error_signal. */
358 sigset_t fatal_signal_set
;
360 #ifdef HAVE_SIGSETMASK
361 int fatal_signal_mask
;
366 main (argc
, argv
, envp
)
371 extern void init_dir ();
372 extern RETSIGTYPE
fatal_error_signal (), child_handler ();
373 register struct file
*f
;
374 register unsigned int i
;
375 register char *cmd_defs
;
376 register unsigned int cmd_defs_len
, cmd_defs_idx
;
379 struct dep
*goals
= 0;
380 register struct dep
*lastgoal
;
381 struct dep
*read_makefiles
;
382 PATH_VAR (current_directory
);
383 char *directory_before_chdir
;
385 default_goal_file
= 0;
386 reading_filename
= 0;
387 reading_lineno_ptr
= 0;
389 #ifndef HAVE_SYS_SIGLIST
394 sigemptyset (&fatal_signal_set
);
395 #define ADD_SIG(sig) sigaddset (&fatal_signal_set, sig)
397 #ifdef HAVE_SIGSETMASK
398 fatal_signal_mask
= 0;
399 #define ADD_SIG(sig) fatal_signal_mask |= sigmask (sig)
405 #define FATAL_SIG(sig) \
406 if (signal ((sig), fatal_error_signal) == SIG_IGN) \
407 (void) signal ((sig), SIG_IGN); \
417 FATAL_SIG (SIGDANGER
);
428 /* Make sure stdout is line-buffered. */
430 #ifdef HAVE_SETLINEBUF
433 #ifndef SETVBUF_REVERSED
434 setvbuf (stdout
, (char *) 0, _IOLBF
, BUFSIZ
);
435 #else /* setvbuf not reversed. */
436 /* Some buggy systems lose if we pass 0 instead of allocating ourselves. */
437 setvbuf (stdout
, _IOLBF
, xmalloc (BUFSIZ
), BUFSIZ
);
438 #endif /* setvbuf reversed. */
439 #endif /* setlinebuf missing. */
441 /* Initialize the directory hashing code. */
444 /* Set up to access user data (files). */
447 /* Figure out where this program lives. */
451 if (argv
[0][0] == '\0')
455 program
= rindex (argv
[0], '/');
462 /* Figure out where we are. */
464 if (getcwd (current_directory
, GET_PATH_MAX
) == 0)
467 perror_with_name ("getcwd: ", "");
469 error ("getwd: %s", current_directory
);
471 current_directory
[0] = '\0';
472 directory_before_chdir
= 0;
475 directory_before_chdir
= savestring (current_directory
,
476 strlen (current_directory
));
478 /* Read in variables from the environment. It is important that this be
479 done before `MAKE' and `MAKEOVERRIDES' are figured out so their
480 definitions will not be ones from the environment. */
482 for (i
= 0; envp
[i
] != 0; ++i
)
484 register char *ep
= envp
[i
];
487 (void) define_variable (envp
[i
], ep
- envp
[i
] - 1, ep
, o_env
, 1);
490 /* Decode the switches. */
492 decode_env_switches ("MAKEFLAGS", 9);
493 decode_env_switches ("MFLAGS", 6);
494 decode_switches (argc
, argv
, 0);
496 /* Print version information. */
498 if (print_version_flag
|| print_data_base_flag
|| debug_flag
)
501 /* Search for command line arguments that define variables,
502 and do the definitions. Also save up the text of these
503 arguments in CMD_DEFS so we can put them into the values
504 of $(MAKEOVERRIDES) and $(MAKE). */
507 cmd_defs
= (char *) xmalloc (cmd_defs_len
);
510 for (i
= 1; other_args
->list
[i
] != 0; ++i
)
512 if (other_args
->list
[i
][0] == '\0')
513 /* Ignore empty arguments, so they can't kill enter_file. */
516 /* Try a variable definition. */
517 if (try_variable_definition ((char *) 0, 0,
518 other_args
->list
[i
], o_command
))
520 /* It succeeded. The variable is already defined.
521 Backslash-quotify it and append it to CMD_DEFS, then clobber it
522 to 0 in the list so that it won't be taken for a goal target. */
523 register char *p
= other_args
->list
[i
];
524 unsigned int l
= strlen (p
);
525 if (cmd_defs_idx
+ (l
* 2) + 1 > cmd_defs_len
)
527 if (l
* 2 > cmd_defs_len
)
528 cmd_defs_len
+= l
* 2;
531 cmd_defs
= (char *) xrealloc (cmd_defs
, cmd_defs_len
);
536 if (index (";'\"*?[]$<>(){}|&~`\\ \t\r\n\f\v", *p
) != 0)
537 cmd_defs
[cmd_defs_idx
++] = '\\';
538 cmd_defs
[cmd_defs_idx
++] = *p
++;
540 cmd_defs
[cmd_defs_idx
++] = ' ';
544 /* It was not a variable definition, so it is a target to be made.
545 Enter it as a file and add it to the dep chain of goals. */
546 f
= enter_file (other_args
->list
[i
]);
551 goals
= (struct dep
*) xmalloc (sizeof (struct dep
));
556 lastgoal
->next
= (struct dep
*) xmalloc (sizeof (struct dep
));
557 lastgoal
= lastgoal
->next
;
564 if (cmd_defs_idx
> 0)
566 cmd_defs
[cmd_defs_idx
- 1] = '\0';
567 (void) define_variable ("MAKEOVERRIDES", 13, cmd_defs
, o_default
, 0);
571 /* Set the "MAKE_COMMAND" variable to the name we were invoked with.
572 (If it is a relative pathname with a slash, prepend our directory name
573 so the result will run the same program regardless of the current dir.
574 If it is a name with no slash, we can only hope that PATH did not
575 find it in the current directory.) */
577 if (current_directory
[0] != '\0'
578 && argv
[0] != 0 && argv
[0][0] != '/' && index (argv
[0], '/') != 0)
579 argv
[0] = concat (current_directory
, "/", argv
[0]);
581 (void) define_variable ("MAKE_COMMAND", 12, argv
[0], o_default
, 0);
583 /* Append the command-line variable definitions gathered above
584 so sub-makes will get them as command-line definitions. */
586 (void) define_variable ("MAKE", 4,
587 "$(MAKE_COMMAND) $(MAKEOVERRIDES)", o_default
, 1);
589 /* If there were -C flags, move ourselves about. */
591 if (directories
!= 0)
592 for (i
= 0; directories
->list
[i
] != 0; ++i
)
594 char *dir
= directories
->list
[i
];
596 pfatal_with_name (dir
);
599 /* Figure out the level of recursion. */
601 struct variable
*v
= lookup_variable ("MAKELEVEL", 9);
602 if (v
!= 0 && *v
->value
!= '\0' && *v
->value
!= '-')
603 makelevel
= (unsigned int) atoi (v
->value
);
608 /* Except under -s, always do -w in sub-makes and under -C. */
609 if (!silent_flag
&& (directories
!= 0 || makelevel
> 0))
610 print_directory_flag
= 1;
612 /* Let the user disable that with --no-print-directory. */
613 if (inhibit_print_directory_flag
)
614 print_directory_flag
= 0;
616 /* Construct the list of include directories to search. */
618 construct_include_path (include_directories
== 0 ? (char **) 0
619 : include_directories
->list
);
621 /* Figure out where we are now, after chdir'ing. */
622 if (directories
== 0)
623 /* We didn't move, so we're still in the same place. */
624 starting_directory
= current_directory
;
627 if (getcwd (current_directory
, GET_PATH_MAX
) == 0)
630 perror_with_name ("getcwd: ", "");
632 error ("getwd: %s", current_directory
);
634 starting_directory
= 0;
637 starting_directory
= current_directory
;
640 /* Tell the user where he is. */
642 if (print_directory_flag
)
643 log_working_directory (1);
645 /* Read any stdin makefiles into temporary files. */
649 register unsigned int i
;
650 for (i
= 0; i
< makefiles
->idx
; ++i
)
651 if (makefiles
->list
[i
][0] == '-' && makefiles
->list
[i
][1] == '\0')
653 /* This makefile is standard input. Since we may re-exec
654 and thus re-read the makefiles, we read standard input
655 into a temporary file and read from that. */
656 static char name
[] = "/tmp/GmXXXXXX";
659 /* Free the storage allocated for "-". */
660 free (makefiles
->list
[i
]);
662 /* Make a unique filename. */
663 (void) mktemp (name
);
665 outfile
= fopen (name
, "w");
667 pfatal_with_name ("fopen (temporary file)");
668 while (!feof (stdin
))
671 int n
= fread (buf
, 1, sizeof(buf
), stdin
);
672 if (n
> 0 && fwrite (buf
, 1, n
, outfile
) != n
)
673 pfatal_with_name ("fwrite (temporary file)");
675 /* Try to make sure we won't remake the temporary
676 file when we are re-exec'd. Kludge-o-matic! */
677 fprintf (outfile
, "%s:;\n", name
);
678 (void) fclose (outfile
);
680 /* Replace the name that read_all_makefiles will
681 see with the name of the temporary file. */
682 makefiles
->list
[i
] = savestring (name
, sizeof name
- 1);
684 /* Make sure the temporary file will not be remade. */
685 f
= enter_file (savestring (name
, sizeof name
- 1));
687 f
->update_status
= 0;
688 f
->command_state
= cs_finished
;
689 /* Let it be removed when we're done. */
691 /* But don't mention it. */
696 /* Set up to handle children dying. This must be done before
697 reading in the makefiles so that `shell' function calls will work. */
700 (void) signal (SIGCHLD
, child_handler
);
703 (void) signal (SIGCLD
, child_handler
);
706 /* Define the initial list of suffixes for old-style rules. */
708 set_default_suffixes ();
710 /* Define some internal and special variables. */
712 define_automatic_variables ();
714 /* Set up the MAKEFLAGS and MFLAGS variables
715 so makefiles can look at them. */
717 define_makeflags (0, 0);
719 /* Define the default variables. */
720 define_default_variables ();
722 /* Read all the makefiles. */
724 default_file
= enter_file (".DEFAULT");
727 = read_all_makefiles (makefiles
== 0 ? (char **) 0 : makefiles
->list
);
729 /* Decode switches again, in case the variables were set by the makefile. */
730 decode_env_switches ("MAKEFLAGS", 9);
731 decode_env_switches ("MFLAGS", 6);
733 /* Set up MAKEFLAGS and MFLAGS again, so they will be right. */
735 define_makeflags (1, 0);
737 ignore_errors_flag
|= lookup_file (".IGNORE") != 0;
739 silent_flag
|= lookup_file (".SILENT") != 0;
741 /* Make each `struct dep' point at the
742 `struct file' for the file depended on. */
746 /* Install the default implicit rules.
747 This used to be done before reading the makefiles.
748 But in that case, built-in pattern rules were in the chain
749 before user-defined ones, so they matched first. */
751 install_default_implicit_rules ();
753 /* Convert old-style suffix rules to pattern rules. */
755 convert_to_pattern ();
757 /* Compute implicit rule limits. */
759 count_implicit_rule_limits ();
761 /* Construct the listings of directories in VPATH lists. */
763 build_vpath_lists ();
765 /* Mark files given with -o flags as very old (00:00:01.00 Jan 1, 1970)
766 and as having been updated already, and files given with -W flags
767 as brand new (time-stamp of now). */
770 for (p
= old_files
->list
; *p
!= 0; ++p
)
773 f
->last_mtime
= (time_t) 1;
775 f
->update_status
= 0;
776 f
->command_state
= cs_finished
;
781 now
= time ((time_t *) 0);
782 for (p
= new_files
->list
; *p
!= 0; ++p
)
789 if (read_makefiles
!= 0)
791 /* Update any makefiles if necessary. */
793 time_t *makefile_mtimes
= 0;
794 unsigned int mm_idx
= 0;
797 puts ("Updating makefiles....");
799 /* Remove any makefiles we don't want to try to update.
800 Also record the current modtimes so we can compare them later. */
802 register struct dep
*d
, *last
;
807 register struct file
*f
= d
->file
;
811 if (f
->deps
== 0 && f
->cmds
!= 0)
813 /* This makefile is a :: target with commands, but
814 no dependencies. So, it will always be remade.
815 This might well cause an infinite loop, so don't
816 try to remake it. (This will only happen if
817 your makefiles are written exceptionally
818 stupidly; but if you work for Athena, that's how
819 you write your makefiles.) */
822 printf ("Makefile `%s' might loop; not remaking it.\n",
826 read_makefiles
= d
->next
;
828 last
->next
= d
->next
;
830 /* Free the storage. */
833 d
= last
== 0 ? 0 : last
->next
;
840 if (f
== NULL
|| !f
->double_colon
)
842 if (makefile_mtimes
== 0)
843 makefile_mtimes
= (time_t *) xmalloc (sizeof (time_t));
845 makefile_mtimes
= (time_t *)
846 xrealloc ((char *) makefile_mtimes
,
847 (mm_idx
+ 1) * sizeof (time_t));
848 makefile_mtimes
[mm_idx
++] = file_mtime_no_search (d
->file
);
855 /* Set up `MAKEFLAGS' specially while remaking makefiles. */
856 define_makeflags (1, 1);
858 switch (update_goal_chain (read_makefiles
, 1))
868 /* Failed to update. Figure out if we care. */
870 /* Nonzero if any makefile was successfully remade. */
872 /* Nonzero if any makefile we care about failed
873 in updating or could not be found at all. */
875 register unsigned int i
;
877 for (i
= 0; read_makefiles
!= 0; ++i
)
879 struct dep
*d
= read_makefiles
;
880 read_makefiles
= d
->next
;
881 if (d
->file
->updated
)
883 /* This makefile was updated. */
884 if (d
->file
->update_status
== 0)
886 /* It was successfully updated. */
887 any_remade
|= (file_mtime_no_search (d
->file
)
888 != makefile_mtimes
[i
]);
890 else if (d
->changed
!= 1)
893 /* The update failed and this makefile was not
894 from the MAKEFILES variable, so we care. */
895 error ("Failed to remake makefile `%s'.",
897 mtime
= file_mtime_no_search (d
->file
);
898 any_remade
|= (mtime
!= (time_t) -1
899 && mtime
!= makefile_mtimes
[i
]);
903 /* This makefile was not found at all. */
907 /* A normal makefile. We must die later. */
908 error ("Makefile `%s' was not found", dep_name (d
));
912 /* A makefile from the MAKEFILES variable.
916 /* An included makefile. We don't need
917 to die, but we do want to complain. */
918 error ("Included makefile `%s' was not found.",
936 /* Updated successfully. Re-exec ourselves. */
937 if (print_directory_flag
)
938 log_working_directory (0);
940 puts ("Re-execing myself....");
943 /* These names might have changed. */
944 register unsigned int i
, j
= 0;
945 for (i
= 1; i
< argc
; ++i
)
946 if (!strcmp (argv
[i
], "-f"))
948 char *p
= &argv
[i
][2];
950 argv
[++i
] = makefiles
->list
[j
];
952 argv
[i
] = concat ("-f", makefiles
->list
[j
], "");
956 if (directories
!= 0 && directories
->idx
> 0)
959 if (directory_before_chdir
!= 0)
961 if (chdir (directory_before_chdir
) < 0)
963 perror_with_name ("chdir", "");
972 fatal ("Couldn't change back to original directory.");
976 for (p
= environ
; *p
!= 0; ++p
)
977 if (!strncmp (*p
, "MAKELEVEL=", 10))
979 /* The SGI compiler apparently can't understand
980 the concept of storing the result of a function
981 in something other than a local variable. */
983 sgi_loses
= (char *) alloca (40);
985 sprintf (*p
, "MAKELEVEL=%u", makelevel
);
988 exec_command (argv
, environ
);
993 /* Set up `MAKEFLAGS' again for the normal targets. */
994 define_makeflags (1, 0);
999 /* If there were no command-line goals, use the default. */
1002 if (default_goal_file
!= 0)
1004 goals
= (struct dep
*) xmalloc (sizeof (struct dep
));
1007 goals
->file
= default_goal_file
;
1015 /* Update the goals. */
1018 puts ("Updating goal targets....");
1020 switch (update_goal_chain (goals
, 0))
1023 /* Nothing happened. */
1025 /* Updated successfully. */
1029 /* Updating failed. */
1038 if (read_makefiles
== 0)
1039 fatal ("No targets specified and no makefile found");
1041 fatal ("No targets");
1051 /* Parsing of arguments, decoding of switches. */
1053 static char options
[sizeof (switches
) / sizeof (switches
[0]) * 3];
1054 static struct option long_options
[(sizeof (switches
) / sizeof (switches
[0])) +
1055 (sizeof (long_option_aliases
) /
1056 sizeof (long_option_aliases
[0]))];
1058 /* Fill in the string and vector for getopt. */
1064 register unsigned int i
;
1066 if (options
[0] != '\0')
1071 for (i
= 0; switches
[i
].c
!= '\0'; ++i
)
1073 long_options
[i
].name
= (switches
[i
].long_name
== 0 ? "" :
1074 switches
[i
].long_name
);
1075 long_options
[i
].flag
= 0;
1076 long_options
[i
].val
= switches
[i
].c
;
1077 if (isalnum (switches
[i
].c
))
1078 *p
++ = switches
[i
].c
;
1079 switch (switches
[i
].type
)
1084 case usage_and_exit
:
1085 long_options
[i
].has_arg
= no_argument
;
1091 if (isalnum (switches
[i
].c
))
1093 if (switches
[i
].noarg_value
!= 0)
1095 if (isalnum (switches
[i
].c
))
1097 long_options
[i
].has_arg
= optional_argument
;
1100 long_options
[i
].has_arg
= required_argument
;
1105 for (c
= 0; c
< (sizeof (long_option_aliases
) /
1106 sizeof (long_option_aliases
[0]));
1108 long_options
[i
++] = long_option_aliases
[c
];
1109 long_options
[i
].name
= 0;
1112 /* Decode switches from ARGC and ARGV.
1113 They came from the environment if ENV is nonzero. */
1116 decode_switches (argc
, argv
, env
)
1122 register const struct command_switch
*cs
;
1123 register struct stringlist
*sl
;
1128 other_args
= (struct stringlist
*) xmalloc (sizeof (struct stringlist
));
1129 other_args
->max
= argc
+ 1;
1130 other_args
->list
= (char **) xmalloc ((argc
+ 1) * sizeof (char *));
1131 other_args
->idx
= 1;
1132 other_args
->list
[0] = savestring (argv
[0], strlen (argv
[0]));
1135 /* getopt does most of the parsing for us.
1136 First, get its vectors set up. */
1140 /* Let getopt produce error messages for the command line,
1141 but not for options from the environment. */
1143 /* Reset getopt's state. */
1146 while ((c
= getopt_long (argc
, argv
,
1147 options
, long_options
, (int *) 0)) != EOF
)
1150 /* Bad option. We will print a usage message and die later.
1151 But continue to parse the other options so the user can
1152 see all he did wrong. */
1155 for (cs
= switches
; cs
->c
!= '\0'; ++cs
)
1158 /* Whether or not we will actually do anything with
1159 this switch. We test this individually inside the
1160 switch below rather than just once outside it, so that
1161 options which are to be ignored still consume args. */
1162 int doit
= !env
|| cs
->env
;
1172 case usage_and_exit
:
1179 *(int *) cs
->value_ptr
= cs
->type
== flag
;
1187 optarg
= cs
->noarg_value
;
1189 sl
= *(struct stringlist
**) cs
->value_ptr
;
1192 sl
= (struct stringlist
*)
1193 xmalloc (sizeof (struct stringlist
));
1196 sl
->list
= (char **) xmalloc (5 * sizeof (char *));
1197 *(struct stringlist
**) cs
->value_ptr
= sl
;
1199 else if (sl
->idx
== sl
->max
- 1)
1202 sl
->list
= (char **)
1203 xrealloc ((char *) sl
->list
,
1204 sl
->max
* sizeof (char *));
1206 sl
->list
[sl
->idx
++] = savestring (optarg
, strlen (optarg
));
1207 sl
->list
[sl
->idx
] = 0;
1211 if (optarg
== 0 && argc
> optind
1212 && isdigit (argv
[optind
][0]))
1213 optarg
= argv
[optind
++];
1220 int i
= atoi (optarg
);
1224 error ("the `-%c' option requires a \
1225 positive integral argument",
1230 *(unsigned int *) cs
->value_ptr
= i
;
1233 *(unsigned int *) cs
->value_ptr
1234 = *(unsigned int *) cs
->noarg_value
;
1238 if (optarg
== 0 && optind
< argc
1239 && (isdigit (argv
[optind
][0]) || argv
[optind
][0] == '.'))
1240 optarg
= argv
[optind
++];
1243 *(double *) cs
->value_ptr
1244 = (optarg
!= 0 ? atof (optarg
)
1245 : *(double *) cs
->noarg_value
);
1250 /* We've found the switch. Stop looking. */
1257 /* Collect the remaining args in the `other_args' string list. */
1259 while (optind
< argc
)
1261 char *arg
= argv
[optind
++];
1262 if (arg
[0] != '-' || arg
[1] != '\0')
1263 other_args
->list
[other_args
->idx
++] = arg
;
1265 other_args
->list
[other_args
->idx
] = 0;
1270 /* Print a nice usage message. */
1272 if (print_version_flag
)
1275 fprintf (stderr
, "Usage: %s [options] [target] ...\n", program
);
1277 fputs ("Options:\n", stderr
);
1278 for (cs
= switches
; cs
->c
!= '\0'; ++cs
)
1280 char buf
[1024], arg
[50], *p
;
1282 if (cs
->description
[0] == '-')
1285 switch (long_options
[cs
- switches
].has_arg
)
1290 case required_argument
:
1291 sprintf (arg
, " %s", cs
->argdesc
);
1293 case optional_argument
:
1294 sprintf (arg
, " [%s]", cs
->argdesc
);
1300 if (isalnum (cs
->c
))
1302 sprintf (buf
, " -%c%s", cs
->c
, arg
);
1305 if (cs
->long_name
!= 0)
1308 sprintf (p
, "%s--%s%s",
1309 !isalnum (cs
->c
) ? " " : ", ",
1310 cs
->long_name
, arg
);
1312 for (i
= 0; i
< (sizeof (long_option_aliases
) /
1313 sizeof (long_option_aliases
[0]));
1315 if (long_option_aliases
[i
].val
== cs
->c
)
1317 sprintf (p
, ", --%s%s", long_option_aliases
[i
].name
, arg
);
1322 const struct command_switch
*ncs
= cs
;
1323 while ((++ncs
)->c
!= '\0')
1324 if (ncs
->description
[0] == '-' &&
1325 ncs
->description
[1] == cs
->c
)
1327 /* This is another switch that does the same
1328 one as the one we are processing. We want
1329 to list them all together on one line. */
1330 sprintf (p
, ", -%c%s", ncs
->c
, arg
);
1332 if (ncs
->long_name
!= 0)
1334 sprintf (p
, ", --%s%s", ncs
->long_name
, arg
);
1340 if (p
- buf
> DESCRIPTION_COLUMN
- 2)
1341 /* The list of option names is too long to fit on the same
1342 line with the description, leaving at least two spaces.
1343 Print it on its own line instead. */
1345 fprintf (stderr
, "%s\n", buf
);
1349 fprintf (stderr
, "%*s%s.\n",
1350 - DESCRIPTION_COLUMN
,
1351 buf
, cs
->description
);
1358 /* Decode switches from environment variable ENVAR (which is LEN chars long).
1359 We do this by chopping the value into a vector of words, prepending a
1360 dash to the first word if it lacks one, and passing the vector to
1364 decode_env_switches (envar
, len
)
1368 char *varref
= (char *) alloca (2 + len
+ 2);
1373 /* Get the variable's value. */
1376 bcopy (envar
, &varref
[2], len
);
1377 varref
[2 + len
] = ')';
1378 varref
[2 + len
+ 1] = '\0';
1379 value
= variable_expand (varref
);
1381 /* Skip whitespace, and check for an empty value. */
1382 value
= next_token (value
);
1383 len
= strlen (value
);
1387 /* Make a copy of the value in ARGS, where we will munge it.
1388 If it does not begin with a dash, prepend one. */
1389 args
= (char *) alloca (1 + len
+ 2);
1390 if (value
[0] != '-')
1392 bcopy (value
, value
[0] == '-' ? args
: &args
[1], len
+ 1);
1393 /* Write an extra null terminator so our loop below will
1394 never be in danger of looking past the end of the string. */
1395 args
[(value
[0] == '-' ? 0 : 1) + len
+ 1] = '\0';
1397 /* Allocate a vector that is definitely big enough. */
1398 argv
= (char **) alloca ((1 + len
+ 1) * sizeof (char *));
1400 /* getopt will look at the arguments starting at ARGV[1].
1401 Prepend a spacer word. */
1406 argv
[argc
++] = args
;
1407 args
= end_of_token (args
);
1409 } while (*args
!= '\0');
1412 /* Parse those words. */
1413 decode_switches (argc
, argv
, 1);
1416 /* Define the MAKEFLAGS and MFLAGS variables to reflect the settings of the
1417 command switches. Include options with args if ALL is nonzero.
1418 Don't include options with the `no_makefile' flag set if MAKEFILE. */
1421 define_makeflags (all
, makefile
)
1424 register const struct command_switch
*cs
;
1427 /* We will construct a linked list of `struct flag's describing
1428 all the flags which need to go in MAKEFLAGS. Then, once we
1429 know how many there are and their lengths, we can put them all
1430 together in a string. */
1435 const struct command_switch
*cs
;
1437 unsigned int arglen
;
1439 struct flag
*flags
= 0;
1440 unsigned int flagslen
= 0;
1441 #define ADD_FLAG(ARG, LEN) \
1443 struct flag *new = (struct flag *) alloca (sizeof (struct flag)); \
1446 new->arglen = (LEN); \
1447 new->next = flags; \
1449 if (new->arg == 0) \
1450 ++flagslen; /* Just a single flag letter. */ \
1452 flagslen += 1 + 1 + 1 + 1 + new->arglen; /* " -x foo" */ \
1453 if (!isalnum (cs->c)) \
1454 /* This switch has no single-letter version, so we use the long. */ \
1455 flagslen += 2 + strlen (cs->long_name); \
1458 for (cs
= switches
; cs
->c
!= '\0'; ++cs
)
1459 if (cs
->toenv
&& (!makefile
|| !cs
->no_makefile
))
1470 if (!*(int *) cs
->value_ptr
== (cs
->type
== flag_off
)
1471 && (cs
->default_value
== 0
1472 || *(int *) cs
->value_ptr
!= *(int *) cs
->default_value
))
1479 if ((cs
->default_value
!= 0
1480 && (*(unsigned int *) cs
->value_ptr
1481 == *(unsigned int *) cs
->default_value
)))
1483 else if (cs
->noarg_value
!= 0
1484 && (*(unsigned int *) cs
->value_ptr
==
1485 *(unsigned int *) cs
->noarg_value
))
1486 ADD_FLAG ("", 0); /* Optional value omitted; see below. */
1487 else if (cs
->c
== 'j')
1488 /* Special case for `-j'. */
1492 char *buf
= (char *) alloca (30);
1493 sprintf (buf
, "%u", *(unsigned int *) cs
->value_ptr
);
1494 ADD_FLAG (buf
, strlen (buf
));
1502 if (cs
->default_value
!= 0
1503 && (*(double *) cs
->value_ptr
1504 == *(double *) cs
->default_value
))
1506 else if (cs
->noarg_value
!= 0
1507 && (*(double *) cs
->value_ptr
1508 == *(double *) cs
->noarg_value
))
1509 ADD_FLAG ("", 0); /* Optional value omitted; see below. */
1512 char *buf
= (char *) alloca (100);
1513 sprintf (buf
, "%g", *(double *) cs
->value_ptr
);
1514 ADD_FLAG (buf
, strlen (buf
));
1522 struct stringlist
*sl
= *(struct stringlist
**) cs
->value_ptr
;
1525 /* Add the elements in reverse order, because
1526 all the flags get reversed below; and the order
1527 matters for some switches (like -I). */
1528 register unsigned int i
= sl
->idx
;
1530 ADD_FLAG (sl
->list
[i
], strlen (sl
->list
[i
]));
1539 /* No flags. Use a string of two nulls so [1] works below. */
1543 /* Construct the value in FLAGSTRING.
1544 We allocate enough space for a preceding dash and trailing null. */
1546 flagstring
= (char *) alloca (1 + flagslen
+ 1);
1551 /* Add the flag letter or name to the string. */
1552 if (!isalnum (flags
->cs
->c
))
1555 strcpy (p
, flags
->cs
->long_name
);
1559 *p
++ = flags
->cs
->c
;
1560 if (flags
->arg
!= 0)
1562 /* A flag that takes an optional argument which in this case
1563 is omitted is specified by ARG being "" and ARGLEN being 0.
1564 We must distinguish because a following flag appended without
1565 an intervening " -" is considered the arg for the first. */
1566 if (flags
->arglen
> 0)
1568 /* Add its argument too. */
1569 *p
++ = !isalnum (flags
->cs
->c
) ? '=' : ' ';
1570 bcopy (flags
->arg
, p
, flags
->arglen
);
1573 /* Write a following space and dash, for the next flag. */
1577 else if (!isalnum (flags
->cs
->c
))
1579 /* Long options must each go in their own word,
1580 so we write the following space and dash. */
1584 flags
= flags
->next
;
1585 } while (flags
!= 0);
1588 /* Kill the final space and dash. */
1591 /* Terminate the string. */
1595 (void) define_variable ("MAKEFLAGS", 9,
1596 /* On Sun, the value of MFLAGS starts with a `-' but
1597 the value of MAKEFLAGS lacks the `-'.
1598 Be compatible with this unless FLAGSTRING starts
1599 with a long option `--foo', since removing the
1600 first dash would result in the bogus `-foo'. */
1601 flagstring
[1] == '-' ? flagstring
: &flagstring
[1],
1603 (void) define_variable ("MFLAGS", 6, flagstring
, o_env
, 0);
1606 /* Print version information. */
1611 static int printed_version
= 0;
1613 extern char *remote_description
;
1614 char *precede
= print_data_base_flag
? "# " : "";
1616 if (printed_version
)
1617 /* Do it only once. */
1620 printf ("%sGNU Make version %s", precede
, version_string
);
1621 if (remote_description
!= 0 && *remote_description
!= '\0')
1622 printf ("-%s", remote_description
);
1624 printf (", by Richard Stallman and Roland McGrath.\n\
1625 %sCopyright (C) 1988, 89, 90, 91, 92, 93 Free Software Foundation, Inc.\n\
1626 %sThis is free software; see the source for copying conditions.\n\
1627 %sThere is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n\
1628 %sPARTICULAR PURPOSE.\n\n", precede
, precede
, precede
, precede
);
1630 printed_version
= 1;
1632 /* Flush stdout so the user doesn't have to wait to see the
1633 version information while things are thought about. */
1637 /* Print a bunch of information about this and that. */
1642 extern char *ctime ();
1645 when
= time ((time_t *) 0);
1646 printf ("\n# Make data base, printed on %s", ctime (&when
));
1648 print_variable_data_base ();
1649 print_dir_data_base ();
1650 print_rule_data_base ();
1651 print_file_data_base ();
1652 print_vpath_data_base ();
1654 when
= time ((time_t *) 0);
1655 printf ("\n# Finished Make data base on %s\n", ctime (&when
));
1658 /* Exit with STATUS, cleaning up as necessary. */
1664 static char dying
= 0;
1672 if (print_version_flag
)
1675 /* Wait for children to die. */
1676 for (err
= status
!= 0; job_slots_used
> 0; err
= 0)
1677 reap_children (1, err
);
1679 /* Remove the intermediate files. */
1680 remove_intermediates (0);
1682 if (print_data_base_flag
)
1685 if (print_directory_flag
)
1686 log_working_directory (0);
1692 /* Write a message indicating that we've just entered or
1693 left (according to ENTERING) the current directory. */
1696 log_working_directory (entering
)
1699 static int entered
= 0;
1700 char *message
= entering
? "Entering" : "Leaving";
1705 /* Don't print the leaving message if we
1706 haven't printed the entering message. */
1709 if (print_data_base_flag
)
1710 fputs ("# ", stdout
);
1713 printf ("%s: %s ", program
, message
);
1715 printf ("%s[%u]: %s ", program
, makelevel
, message
);
1717 if (starting_directory
== 0)
1718 puts ("an unknown directory");
1720 printf ("directory `%s'\n", starting_directory
);