dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / make / bin / main.cc
blob496902c5022dcc5c63cff4605618136bd2d4adb7
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 * main.cc
29 * make program main routine plus some helper routines
33 * Included files
35 #include <bsd/bsd.h> /* bsd_signal() */
38 #include <locale.h> /* setlocale() */
39 #include <libgen.h>
40 #include <mk/defs.h>
41 #include <mksh/macro.h> /* getvar() */
42 #include <mksh/misc.h> /* getmem(), setup_char_semantics() */
44 #include <pwd.h> /* getpwnam() */
45 #include <setjmp.h>
46 #include <signal.h>
47 #include <stdlib.h>
48 #include <sys/errno.h> /* ENOENT */
49 #include <sys/stat.h> /* fstat() */
50 #include <fcntl.h> /* open() */
52 # include <sys/systeminfo.h> /* sysinfo() */
54 #include <sys/types.h> /* stat() */
55 #include <sys/wait.h> /* wait() */
56 #include <unistd.h> /* execv(), unlink(), access() */
57 #include <vroot/report.h> /* report_dependency(), get_report_file() */
59 // From read2.cc
60 extern Name normalize_name(register wchar_t *name_string, register int length);
62 extern void job_adjust_fini();
66 * Defined macros
68 #define LD_SUPPORT_ENV_VAR "SGS_SUPPORT_32"
69 #define LD_SUPPORT_ENV_VAR_32 "SGS_SUPPORT_32"
70 #define LD_SUPPORT_ENV_VAR_64 "SGS_SUPPORT_64"
71 #define LD_SUPPORT_MAKE_LIB "libmakestate.so.1"
72 #ifdef __i386
73 #define LD_SUPPORT_MAKE_ARCH "i386"
74 #elif __sparc
75 #define LD_SUPPORT_MAKE_ARCH "sparc"
76 #else
77 #error "Unsupported architecture"
78 #endif
81 * typedefs & structs
85 * Static variables
87 static char *argv_zero_string;
88 static Boolean build_failed_ever_seen;
89 static Boolean continue_after_error_ever_seen; /* `-k' */
90 static Boolean dmake_group_specified; /* `-g' */
91 static Boolean dmake_max_jobs_specified; /* `-j' */
92 static Boolean dmake_mode_specified; /* `-m' */
93 static Boolean dmake_add_mode_specified; /* `-x' */
94 static Boolean dmake_output_mode_specified; /* `-x DMAKE_OUTPUT_MODE=' */
95 static Boolean dmake_compat_mode_specified; /* `-x SUN_MAKE_COMPAT_MODE=' */
96 static Boolean dmake_odir_specified; /* `-o' */
97 static Boolean dmake_rcfile_specified; /* `-c' */
98 static Boolean env_wins; /* `-e' */
99 static Boolean ignore_default_mk; /* `-r' */
100 static Boolean list_all_targets; /* `-T' */
101 static int mf_argc;
102 static char **mf_argv;
103 static Dependency_rec not_auto_depen_struct;
104 static Dependency not_auto_depen = &not_auto_depen_struct;
105 static Boolean pmake_cap_r_specified; /* `-R' */
106 static Boolean pmake_machinesfile_specified; /* `-M' */
107 static Boolean stop_after_error_ever_seen; /* `-S' */
108 static Boolean trace_status; /* `-p' */
109 static char *working_dir; /* `-C' */
111 #ifdef DMAKE_STATISTICS
112 static Boolean getname_stat = false;
113 #endif
115 static time_t start_time;
116 static int g_argc;
117 static char **g_argv;
120 * File table of contents
122 extern "C" void cleanup_after_exit(void);
124 extern "C" {
125 extern void dmake_exit_callback(void);
126 extern void dmake_message_callback(char *);
129 extern Name normalize_name(register wchar_t *name_string, register int length);
131 extern int main(int, char * []);
133 static void append_makeflags_string(Name, String);
134 static void doalarm(int);
135 static void enter_argv_values(int , char **, ASCII_Dyn_Array *);
136 static void make_targets(int, char **, Boolean);
137 static int parse_command_option(char);
138 static void read_command_options(int, char **);
139 static void read_environment(Boolean);
140 static void read_files_and_state(int, char **);
141 static Boolean read_makefile(Name, Boolean, Boolean, Boolean);
142 static void report_recursion(Name);
143 static void set_sgs_support(void);
144 static void setup_for_projectdir(void);
145 static void setup_makeflags_argv(void);
146 static void report_dir_enter_leave(Boolean entering);
148 extern void expand_value(Name, register String , Boolean);
150 static const char verstring[] = "illumos make";
152 jmp_buf jmpbuffer;
155 * main(argc, argv)
157 * Parameters:
158 * argc You know what this is
159 * argv You know what this is
161 * Static variables used:
162 * list_all_targets make -T seen
163 * trace_status make -p seen
165 * Global variables used:
166 * debug_level Should we trace make actions?
167 * keep_state Set if .KEEP_STATE seen
168 * makeflags The Name "MAKEFLAGS", used to get macro
169 * remote_command_name Name of remote invocation cmd ("on")
170 * running_list List of parallel running processes
171 * stdout_stderr_same true if stdout and stderr are the same
172 * auto_dependencies The Name "SUNPRO_DEPENDENCIES"
173 * temp_file_directory Set to the dir where we create tmp file
174 * trace_reader Set to reflect tracing status
175 * working_on_targets Set when building user targets
178 main(int argc, char *argv[])
181 * cp is a -> to the value of the MAKEFLAGS env var,
182 * which has to be regular chars.
184 register char *cp;
185 char make_state_dir[MAXPATHLEN];
186 Boolean parallel_flag = false;
187 char *prognameptr;
188 char *slash_ptr;
189 mode_t um;
190 int i;
191 struct itimerval value;
192 char def_dmakerc_path[MAXPATHLEN];
193 Name dmake_name, dmake_name2;
194 Name dmake_value, dmake_value2;
195 Property prop, prop2;
196 struct stat statbuf;
197 int statval;
199 struct stat out_stat, err_stat;
200 hostid = gethostid();
201 bsd_signals();
203 (void) setlocale(LC_ALL, "");
206 #ifdef DMAKE_STATISTICS
207 if (getenv("DMAKE_STATISTICS")) {
208 getname_stat = true;
210 #endif
212 #ifndef TEXT_DOMAIN
213 #define TEXT_DOMAIN "SYS_TEST"
214 #endif
215 textdomain(TEXT_DOMAIN);
217 g_argc = argc;
218 g_argv = (char **) malloc((g_argc + 1) * sizeof(char *));
219 for (i = 0; i < argc; i++) {
220 g_argv[i] = argv[i];
222 g_argv[i] = NULL;
225 * Set argv_zero_string to some form of argv[0] for
226 * recursive MAKE builds.
229 if (*argv[0] == (int) slash_char) {
230 /* argv[0] starts with a slash */
231 argv_zero_string = strdup(argv[0]);
232 } else if (strchr(argv[0], (int) slash_char) == NULL) {
233 /* argv[0] contains no slashes */
234 argv_zero_string = strdup(argv[0]);
235 } else {
237 * argv[0] contains at least one slash,
238 * but doesn't start with a slash
240 char *tmp_current_path;
241 char *tmp_string;
243 tmp_current_path = get_current_path();
244 tmp_string = getmem(strlen(tmp_current_path) + 1 +
245 strlen(argv[0]) + 1);
246 (void) sprintf(tmp_string,
247 "%s/%s",
248 tmp_current_path,
249 argv[0]);
250 argv_zero_string = strdup(tmp_string);
251 retmem_mb(tmp_string);
255 * The following flags are reset if we don't have the
256 * (.nse_depinfo or .make.state) files locked and only set
257 * AFTER the file has been locked. This ensures that if the user
258 * interrupts the program while file_lock() is waiting to lock
259 * the file, the interrupt handler doesn't remove a lock
260 * that doesn't belong to us.
262 make_state_lockfile = NULL;
263 make_state_locked = false;
267 * look for last slash char in the path to look at the binary
268 * name. This is to resolve the hard link and invoke make
269 * in svr4 mode.
272 /* Sun OS make standart */
273 svr4 = false;
274 posix = false;
275 if(!strcmp(argv_zero_string, "/usr/bin/make")) {
276 svr4 = false;
277 posix = true;
278 } else {
279 prognameptr = strrchr(argv[0], '/');
280 if(prognameptr) {
281 prognameptr++;
282 } else {
283 prognameptr = argv[0];
285 if(!strcmp(prognameptr, "svr4.make")) {
286 svr4 = true;
287 posix = false;
290 if (getenv(USE_SVR4_MAKE) || getenv("USE_SVID")){
291 svr4 = true;
292 posix = false;
296 * Find the dmake_compat_mode: posix, sun, svr4, or gnu_style, .
298 char * dmake_compat_mode_var = getenv("SUN_MAKE_COMPAT_MODE");
299 if (dmake_compat_mode_var != NULL) {
300 if (0 == strcasecmp(dmake_compat_mode_var, "GNU")) {
301 gnu_style = true;
303 //svr4 = false;
304 //posix = false;
308 * Temporary directory set up.
310 char * tmpdir_var = getenv("TMPDIR");
311 if (tmpdir_var != NULL && *tmpdir_var == '/' && strlen(tmpdir_var) < MAXPATHLEN) {
312 strcpy(mbs_buffer, tmpdir_var);
313 for (tmpdir_var = mbs_buffer+strlen(mbs_buffer);
314 *(--tmpdir_var) == '/' && tmpdir_var > mbs_buffer;
315 *tmpdir_var = '\0');
316 if (strlen(mbs_buffer) + 32 < MAXPATHLEN) { /* 32 = strlen("/dmake.stdout.%d.%d.XXXXXX") */
317 sprintf(mbs_buffer2, "%s/dmake.tst.%d.XXXXXX",
318 mbs_buffer, getpid());
319 int fd = mkstemp(mbs_buffer2);
320 if (fd >= 0) {
321 close(fd);
322 unlink(mbs_buffer2);
323 tmpdir = strdup(mbs_buffer);
328 /* find out if stdout and stderr point to the same place */
329 if (fstat(1, &out_stat) < 0) {
330 fatal(gettext("fstat of standard out failed: %s"), errmsg(errno));
332 if (fstat(2, &err_stat) < 0) {
333 fatal(gettext("fstat of standard error failed: %s"), errmsg(errno));
335 if ((out_stat.st_dev == err_stat.st_dev) &&
336 (out_stat.st_ino == err_stat.st_ino)) {
337 stdout_stderr_same = true;
338 } else {
339 stdout_stderr_same = false;
341 /* Make the vroot package scan the path using shell semantics */
342 set_path_style(0);
344 setup_char_semantics();
346 setup_for_projectdir();
349 * If running with .KEEP_STATE, curdir will be set with
350 * the connected directory.
352 (void) atexit(cleanup_after_exit);
354 load_cached_names();
357 * Set command line flags
359 setup_makeflags_argv();
360 read_command_options(mf_argc, mf_argv);
361 read_command_options(argc, argv);
362 if (debug_level > 0) {
363 cp = getenv(makeflags->string_mb);
364 (void) printf(gettext("MAKEFLAGS value: %s\n"), cp == NULL ? "" : cp);
367 setup_interrupt(handle_interrupt);
369 if (working_dir) {
370 chdir(working_dir);
371 report_dir_enter_leave(true);
372 printf(gettext("%s: Entering directory `%s'\n"),
373 getprogname(), get_current_path());
376 read_files_and_state(argc, argv);
379 * Find the dmake_output_mode: TXT1, TXT2 or HTML1.
381 MBSTOWCS(wcs_buffer, "DMAKE_OUTPUT_MODE");
382 dmake_name2 = GETNAME(wcs_buffer, FIND_LENGTH);
383 prop2 = get_prop(dmake_name2->prop, macro_prop);
384 if (prop2 == NULL) {
385 /* DMAKE_OUTPUT_MODE not defined, default to TXT1 mode */
386 output_mode = txt1_mode;
387 } else {
388 dmake_value2 = prop2->body.macro.value;
389 if ((dmake_value2 == NULL) ||
390 (IS_EQUAL(dmake_value2->string_mb, "TXT1"))) {
391 output_mode = txt1_mode;
392 } else if (IS_EQUAL(dmake_value2->string_mb, "TXT2")) {
393 output_mode = txt2_mode;
394 } else if (IS_EQUAL(dmake_value2->string_mb, "HTML1")) {
395 output_mode = html1_mode;
396 } else {
397 warning(gettext("Unsupported value `%s' for DMAKE_OUTPUT_MODE after -x flag (ignored)"),
398 dmake_value2->string_mb);
402 * Find the dmake_mode: parallel, or serial.
404 if ((!pmake_cap_r_specified) &&
405 (!pmake_machinesfile_specified)) {
406 char *s, *b;
408 if ((s = strdup(argv[0])) == NULL)
409 fatal(gettext("Out of memory"));
411 b = basename(s);
413 MBSTOWCS(wcs_buffer, "DMAKE_MODE");
414 dmake_name2 = GETNAME(wcs_buffer, FIND_LENGTH);
415 prop2 = get_prop(dmake_name2->prop, macro_prop);
416 // If we're invoked as 'make' run serially, regardless of DMAKE_MODE
417 // If we're invoked as 'make' but passed -j, run parallel
418 // If we're invoked as 'dmake', without DMAKE_MODE, default parallel
419 // If we're invoked as 'dmake' and DMAKE_MODE is set, honour it.
420 if ((strcmp(b, "make") == 0) &&
421 !dmake_max_jobs_specified) {
422 dmake_mode_type = serial_mode;
423 no_parallel = true;
424 } else if (prop2 == NULL) {
425 /* DMAKE_MODE not defined, default based on our name */
426 if (strcmp(b, "dmake") == 0) {
427 dmake_mode_type = parallel_mode;
428 no_parallel = false;
430 } else {
431 dmake_value2 = prop2->body.macro.value;
432 if (IS_EQUAL(dmake_value2->string_mb, "parallel")) {
433 dmake_mode_type = parallel_mode;
434 no_parallel = false;
435 } else if (IS_EQUAL(dmake_value2->string_mb, "serial")) {
436 dmake_mode_type = serial_mode;
437 no_parallel = true;
438 } else {
439 fatal(gettext("Unknown dmake mode argument `%s' after -m flag"), dmake_value2->string_mb);
442 free(s);
445 parallel_flag = true;
446 putenv(strdup("DMAKE_CHILD=TRUE"));
449 // If dmake is running with -t option, set dmake_mode_type to serial.
450 // This is done because doname() calls touch_command() that runs serially.
451 // If we do not do that, maketool will have problems.
453 if(touch) {
454 dmake_mode_type = serial_mode;
455 no_parallel = true;
459 * Check whether stdout and stderr are physically same.
460 * This is in order to decide whether we need to redirect
461 * stderr separately from stdout.
462 * This check is performed only if __DMAKE_SEPARATE_STDERR
463 * is not set. This variable may be used in order to preserve
464 * the 'old' behaviour.
466 out_err_same = true;
467 char * dmake_sep_var = getenv("__DMAKE_SEPARATE_STDERR");
468 if (dmake_sep_var == NULL || (0 != strcasecmp(dmake_sep_var, "NO"))) {
469 struct stat stdout_stat;
470 struct stat stderr_stat;
471 if( (fstat(1, &stdout_stat) == 0)
472 && (fstat(2, &stderr_stat) == 0) )
474 if( (stdout_stat.st_dev != stderr_stat.st_dev)
475 || (stdout_stat.st_ino != stderr_stat.st_ino) )
477 out_err_same = false;
484 * Enable interrupt handler for alarms
486 (void) bsd_signal(SIGALRM, (SIG_PF)doalarm);
489 * Check if make should report
491 if (getenv(sunpro_dependencies->string_mb) != NULL) {
492 FILE *report_file;
494 report_dependency("");
495 report_file = get_report_file();
496 if ((report_file != NULL) && (report_file != (FILE*)-1)) {
497 (void) fprintf(report_file, "\n");
502 * Make sure SUNPRO_DEPENDENCIES is exported (or not) properly.
504 if (keep_state) {
505 maybe_append_prop(sunpro_dependencies, macro_prop)->
506 body.macro.exported = true;
507 } else {
508 maybe_append_prop(sunpro_dependencies, macro_prop)->
509 body.macro.exported = false;
512 working_on_targets = true;
513 if (trace_status) {
514 dump_make_state();
515 fclose(stdout);
516 fclose(stderr);
517 exit_status = 0;
518 exit(0);
520 if (list_all_targets) {
521 dump_target_list();
522 fclose(stdout);
523 fclose(stderr);
524 exit_status = 0;
525 exit(0);
527 trace_reader = false;
530 * Set temp_file_directory to the directory the .make.state
531 * file is written to.
533 if ((slash_ptr = strrchr(make_state->string_mb, (int) slash_char)) == NULL) {
534 temp_file_directory = strdup(get_current_path());
535 } else {
536 *slash_ptr = (int) nul_char;
537 (void) strcpy(make_state_dir, make_state->string_mb);
538 *slash_ptr = (int) slash_char;
539 /* when there is only one slash and it's the first
540 ** character, make_state_dir should point to '/'.
542 if(make_state_dir[0] == '\0') {
543 make_state_dir[0] = '/';
544 make_state_dir[1] = '\0';
546 if (make_state_dir[0] == (int) slash_char) {
547 temp_file_directory = strdup(make_state_dir);
548 } else {
549 char tmp_current_path2[MAXPATHLEN];
551 (void) sprintf(tmp_current_path2,
552 "%s/%s",
553 get_current_path(),
554 make_state_dir);
555 temp_file_directory = strdup(tmp_current_path2);
560 report_dir_enter_leave(true);
562 make_targets(argc, argv, parallel_flag);
564 report_dir_enter_leave(false);
566 if (build_failed_ever_seen) {
567 if (posix) {
568 exit_status = 1;
570 exit(1);
572 exit_status = 0;
573 exit(0);
574 /* NOTREACHED */
578 * cleanup_after_exit()
580 * Called from exit(), performs cleanup actions.
582 * Parameters:
583 * status The argument exit() was called with
584 * arg Address of an argument vector to
585 * cleanup_after_exit()
587 * Global variables used:
588 * command_changed Set if we think .make.state should be rewritten
589 * current_line Is set we set commands_changed
590 * do_not_exec_rule
591 * True if -n flag on
592 * done The Name ".DONE", rule we run
593 * keep_state Set if .KEEP_STATE seen
594 * parallel True if building in parallel
595 * quest If -q is on we do not run .DONE
596 * report_dependencies
597 * True if -P flag on
598 * running_list List of parallel running processes
599 * temp_file_name The temp file is removed, if any
601 extern "C" void
602 cleanup_after_exit(void)
604 Running rp;
606 extern long getname_bytes_count;
607 extern long getname_names_count;
608 extern long getname_struct_count;
609 extern long freename_bytes_count;
610 extern long freename_names_count;
611 extern long freename_struct_count;
612 extern long other_alloc;
614 extern long env_alloc_num;
615 extern long env_alloc_bytes;
618 #ifdef DMAKE_STATISTICS
619 if(getname_stat) {
620 printf(">>> Getname statistics:\n");
621 printf(" Allocated:\n");
622 printf(" Names: %ld\n", getname_names_count);
623 printf(" Strings: %ld Kb (%ld bytes)\n", getname_bytes_count/1000, getname_bytes_count);
624 printf(" Structs: %ld Kb (%ld bytes)\n", getname_struct_count/1000, getname_struct_count);
625 printf(" Total bytes: %ld Kb (%ld bytes)\n", getname_struct_count/1000 + getname_bytes_count/1000, getname_struct_count + getname_bytes_count);
627 printf("\n Unallocated: %ld\n", freename_names_count);
628 printf(" Names: %ld\n", freename_names_count);
629 printf(" Strings: %ld Kb (%ld bytes)\n", freename_bytes_count/1000, freename_bytes_count);
630 printf(" Structs: %ld Kb (%ld bytes)\n", freename_struct_count/1000, freename_struct_count);
631 printf(" Total bytes: %ld Kb (%ld bytes)\n", freename_struct_count/1000 + freename_bytes_count/1000, freename_struct_count + freename_bytes_count);
633 printf("\n Total used: %ld Kb (%ld bytes)\n", (getname_struct_count/1000 + getname_bytes_count/1000) - (freename_struct_count/1000 + freename_bytes_count/1000), (getname_struct_count + getname_bytes_count) - (freename_struct_count + freename_bytes_count));
635 printf("\n>>> Other:\n");
636 printf(
637 " Env (%ld): %ld Kb (%ld bytes)\n",
638 env_alloc_num,
639 env_alloc_bytes/1000,
640 env_alloc_bytes
644 #endif
646 parallel = false;
647 /* If we used the SVR4_MAKE, don't build .DONE or .FAILED */
648 if (!getenv(USE_SVR4_MAKE)){
649 /* Build the target .DONE or .FAILED if we caught an error */
650 if (!quest && !list_all_targets) {
651 Name failed_name;
653 MBSTOWCS(wcs_buffer, ".FAILED");
654 failed_name = GETNAME(wcs_buffer, FIND_LENGTH);
655 if ((exit_status != 0) && (failed_name->prop != NULL)) {
657 * [tolik] switch DMake to serial mode
659 dmake_mode_type = serial_mode;
660 no_parallel = true;
661 (void) doname(failed_name, false, true);
662 } else {
663 if (!trace_status) {
665 * Switch DMake to serial mode
667 dmake_mode_type = serial_mode;
668 no_parallel = true;
669 (void) doname(done, false, true);
675 * Remove the temp file utilities report dependencies thru if it
676 * is still around
678 if (temp_file_name != NULL) {
679 (void) unlink(temp_file_name->string_mb);
682 * Do not save the current command in .make.state if make
683 * was interrupted.
685 if (current_line != NULL) {
686 command_changed = true;
687 current_line->body.line.command_used = NULL;
690 * For each parallel build process running, remove the temp files
691 * and zap the command line so it won't be put in .make.state
693 for (rp = running_list; rp != NULL; rp = rp->next) {
694 if (rp->temp_file != NULL) {
695 (void) unlink(rp->temp_file->string_mb);
697 if (rp->stdout_file != NULL) {
698 (void) unlink(rp->stdout_file);
699 retmem_mb(rp->stdout_file);
700 rp->stdout_file = NULL;
702 if (rp->stderr_file != NULL) {
703 (void) unlink(rp->stderr_file);
704 retmem_mb(rp->stderr_file);
705 rp->stderr_file = NULL;
707 command_changed = true;
709 line = get_prop(rp->target->prop, line_prop);
710 if (line != NULL) {
711 line->body.line.command_used = NULL;
715 /* Remove the statefile lock file if the file has been locked */
716 if ((make_state_lockfile != NULL) && (make_state_locked)) {
717 (void) unlink(make_state_lockfile);
718 make_state_lockfile = NULL;
719 make_state_locked = false;
721 /* Write .make.state */
722 write_state_file(1, (Boolean) 1);
724 job_adjust_fini();
728 * handle_interrupt()
730 * This is where C-C traps are caught.
732 * Parameters:
734 * Global variables used (except DMake 1.0):
735 * current_target Sometimes the current target is removed
736 * do_not_exec_rule But not if -n is on
737 * quest or -q
738 * running_list List of parallel running processes
739 * touch Current target is not removed if -t on
741 void
742 handle_interrupt(int)
744 Property member;
745 Running rp;
747 (void) fflush(stdout);
748 if (childPid > 0) {
749 kill(childPid, SIGTERM);
750 childPid = -1;
752 for (rp = running_list; rp != NULL; rp = rp->next) {
753 if (rp->state != build_running) {
754 continue;
756 if (rp->pid > 0) {
757 kill(rp->pid, SIGTERM);
758 rp->pid = -1;
761 if (getpid() == getpgrp()) {
762 bsd_signal(SIGTERM, SIG_IGN);
763 kill (-getpid(), SIGTERM);
765 /* Clean up all parallel children already finished */
766 finish_children(false);
768 /* Make sure the processes running under us terminate first */
770 while (wait((int *) NULL) != -1);
771 /* Delete the current targets unless they are precious */
772 if ((current_target != NULL) &&
773 current_target->is_member &&
774 ((member = get_prop(current_target->prop, member_prop)) != NULL)) {
775 current_target = member->body.member.library;
777 if (!do_not_exec_rule &&
778 !touch &&
779 !quest &&
780 (current_target != NULL) &&
781 !(current_target->stat.is_precious || all_precious)) {
783 /* BID_1030811 */
784 /* azv 16 Oct 95 */
785 current_target->stat.time = file_no_time;
787 if (exists(current_target) != file_doesnt_exist) {
788 (void) fprintf(stderr,
789 "\n*** %s ",
790 current_target->string_mb);
791 if (current_target->stat.is_dir) {
792 (void) fprintf(stderr,
793 gettext("not removed.\n"),
794 current_target->string_mb);
795 } else if (unlink(current_target->string_mb) == 0) {
796 (void) fprintf(stderr,
797 gettext("removed.\n"),
798 current_target->string_mb);
799 } else {
800 (void) fprintf(stderr,
801 gettext("could not be removed: %s.\n"),
802 current_target->string_mb,
803 errmsg(errno));
807 for (rp = running_list; rp != NULL; rp = rp->next) {
808 if (rp->state != build_running) {
809 continue;
811 if (rp->target->is_member &&
812 ((member = get_prop(rp->target->prop, member_prop)) !=
813 NULL)) {
814 rp->target = member->body.member.library;
816 if (!do_not_exec_rule &&
817 !touch &&
818 !quest &&
819 !(rp->target->stat.is_precious || all_precious)) {
821 rp->target->stat.time = file_no_time;
822 if (exists(rp->target) != file_doesnt_exist) {
823 (void) fprintf(stderr,
824 "\n*** %s ",
825 rp->target->string_mb);
826 if (rp->target->stat.is_dir) {
827 (void) fprintf(stderr,
828 gettext("not removed.\n"),
829 rp->target->string_mb);
830 } else if (unlink(rp->target->string_mb) == 0) {
831 (void) fprintf(stderr,
832 gettext("removed.\n"),
833 rp->target->string_mb);
834 } else {
835 (void) fprintf(stderr,
836 gettext("could not be removed: %s.\n"),
837 rp->target->string_mb,
838 errmsg(errno));
845 /* Have we locked .make.state or .nse_depinfo? */
846 if ((make_state_lockfile != NULL) && (make_state_locked)) {
847 unlink(make_state_lockfile);
848 make_state_lockfile = NULL;
849 make_state_locked = false;
852 * Re-read .make.state file (it might be changed by recursive make)
854 check_state(NULL);
856 report_dir_enter_leave(false);
858 exit_status = 2;
859 exit(2);
863 * doalarm(sig, ...)
865 * Handle the alarm interrupt but do nothing. Side effect is to
866 * cause return from wait3.
868 * Parameters:
869 * sig
871 * Global variables used:
873 /*ARGSUSED*/
874 static void
875 doalarm(int)
877 return;
882 * read_command_options(argc, argv)
884 * Scan the cmd line options and process the ones that start with "-"
886 * Return value:
887 * -M argument, if any
889 * Parameters:
890 * argc You know what this is
891 * argv You know what this is
893 * Global variables used:
895 static void
896 read_command_options(register int argc, register char **argv)
898 register int ch;
899 int current_optind = 1;
900 int last_optind_with_double_hyphen = 0;
901 int last_optind;
902 int last_current_optind;
903 register int i;
904 register int j;
905 register int k;
906 register int makefile_next = 0; /*
907 * flag to note options:
908 * -c, f, g, j, m, o
910 const char *tptr;
911 const char *CMD_OPTS;
913 extern char *optarg;
914 extern int optind, opterr, optopt;
916 #define SUNPRO_CMD_OPTS "-~BbC:c:Ddef:g:ij:K:kM:m:NnO:o:PpqRrSsTtuVvwx:"
918 # define SVR4_CMD_OPTS "-C:c:ef:g:ij:km:nO:o:pqrsTtVv"
921 * Added V in SVR4_CMD_OPTS also, which is going to be a hidden
922 * option, just to make sure that the getopt doesn't fail when some
923 * users leave their USE_SVR4_MAKE set and try to use the makefiles
924 * that are designed to issue commands like $(MAKE) -V. Anyway it
925 * sets the same flag but ensures that getopt doesn't fail.
928 opterr = 0;
929 optind = 1;
930 while (1) {
931 last_optind=optind; /* Save optind and current_optind values */
932 last_current_optind=current_optind; /* in case we have to repeat this round. */
933 if (svr4) {
934 CMD_OPTS=SVR4_CMD_OPTS;
935 ch = getopt(argc, argv, SVR4_CMD_OPTS);
936 } else {
937 CMD_OPTS=SUNPRO_CMD_OPTS;
938 ch = getopt(argc, argv, SUNPRO_CMD_OPTS);
940 if (ch == EOF) {
941 if(optind < argc) {
943 * Fixing bug 4102537:
944 * Strange behaviour of command make using -- option.
945 * Not all argv have been processed
946 * Skip non-flag argv and continue processing.
948 optind++;
949 current_optind++;
950 continue;
951 } else {
952 break;
956 if (ch == '?') {
957 if (optopt == '-') {
958 /* Bug 5060758: getopt() changed behavior (s10_60),
959 * and now we have to deal with cases when options
960 * with double hyphen appear here, from -$(MAKEFLAGS)
962 i = current_optind;
963 if (argv[i][0] == '-') {
964 if (argv[i][1] == '-') {
965 if (argv[i][2] != '\0') {
966 /* Check if this option is allowed */
967 tptr = strchr(CMD_OPTS, argv[i][2]);
968 if (tptr) {
969 if (last_optind_with_double_hyphen != current_optind) {
970 /* This is first time we are trying to fix "--"
971 * problem with this option. If we come here second
972 * time, we will go to fatal error.
974 last_optind_with_double_hyphen = current_optind;
976 /* Eliminate first hyphen character */
977 for (j=0; argv[i][j] != '\0'; j++) {
978 argv[i][j] = argv[i][j+1];
981 /* Repeat the processing of this argument */
982 optind=last_optind;
983 current_optind=last_current_optind;
984 continue;
993 if (ch == '?') {
994 if (svr4) {
995 fprintf(stderr,
996 gettext("Usage : dmake [ -f makefile ][ -c dmake_rcfile ][ -g dmake_group ]\n"));
997 fprintf(stderr,
998 gettext(" [ -j dmake_max_jobs ][ -m dmake_mode ][ -o dmake_odir ]...\n"));
999 fprintf(stderr,
1000 gettext(" [ -e ][ -i ][ -k ][ -n ][ -p ][ -q ][ -r ][ -s ][ -t ][ -v ]\n"));
1001 tptr = strchr(SVR4_CMD_OPTS, optopt);
1002 } else {
1003 fprintf(stderr,
1004 gettext("Usage : dmake [ -f makefile ][ -c dmake_rcfile ][ -g dmake_group ]\n"));
1005 fprintf(stderr,
1006 gettext(" [ -j dmake_max_jobs ][ -K statefile ][ -m dmake_mode ][ -x MODE_NAME=VALUE ][ -o dmake_odir ]...\n"));
1007 fprintf(stderr,
1008 gettext(" [ -d ][ -dd ][ -D ][ -DD ][ -e ][ -i ][ -k ][ -n ][ -p ][ -P ][ -u ][ -w ]\n"));
1009 fprintf(stderr,
1010 gettext(" [ -q ][ -r ][ -s ][ -S ][ -t ][ -v ][ -V ][ target... ][ macro=value... ][ \"macro +=value\"... ]\n"));
1011 tptr = strchr(SUNPRO_CMD_OPTS, optopt);
1013 if (!tptr) {
1014 fatal(gettext("Unknown option `-%c'"), optopt);
1015 } else {
1016 fatal(gettext("Missing argument after `-%c'"), optopt);
1020 if (ch == 'C')
1021 working_dir = optarg;
1025 makefile_next |= parse_command_option(ch);
1027 * If we're done processing all of the options of
1028 * ONE argument string...
1030 if (current_optind < optind) {
1031 i = current_optind;
1032 k = 0;
1033 /* If there's an argument for an option... */
1034 if ((optind - current_optind) > 1) {
1035 k = i + 1;
1037 switch (makefile_next) {
1038 case 0:
1039 argv[i] = NULL;
1040 /* This shouldn't happen */
1041 if (k) {
1042 argv[k] = NULL;
1044 break;
1045 case 1: /* -f seen */
1046 argv[i] = (char *)"-f";
1047 break;
1048 case 2: /* -c seen */
1049 argv[i] = (char *)"-c";
1050 break;
1051 case 4: /* -g seen */
1052 argv[i] = (char *)"-g";
1053 break;
1054 case 8: /* -j seen */
1055 argv[i] = (char *)"-j";
1056 break;
1057 case 16: /* -M seen */
1058 argv[i] = (char *)"-M";
1059 break;
1060 case 32: /* -m seen */
1061 argv[i] = (char *)"-m";
1062 break;
1063 case 128: /* -O seen */
1064 argv[i] = (char *)"-O";
1065 break;
1066 case 256: /* -K seen */
1067 argv[i] = (char *)"-K";
1068 break;
1069 case 512: /* -o seen */
1070 argv[i] = (char *)"-o";
1071 break;
1072 case 1024: /* -x seen */
1073 argv[i] = (char *)"-x";
1074 break;
1075 default: /* > 1 of -c, f, g, j, K, M, m, O, o, x seen */
1076 fatal(gettext("Illegal command line. More than one option requiring\nan argument given in the same argument group"));
1079 makefile_next = 0;
1080 current_optind = optind;
1085 static void
1086 quote_str(char *str, char *qstr)
1088 char *to;
1089 char *from;
1091 to = qstr;
1092 for (from = str; *from; from++) {
1093 switch (*from) {
1094 case ';': /* End of command */
1095 case '(': /* Start group */
1096 case ')': /* End group */
1097 case '{': /* Start group */
1098 case '}': /* End group */
1099 case '[': /* Reg expr - any of a set of chars */
1100 case ']': /* End of set of chars */
1101 case '|': /* Pipe or logical-or */
1102 case '^': /* Old-fashioned pipe */
1103 case '&': /* Background or logical-and */
1104 case '<': /* Redirect stdin */
1105 case '>': /* Redirect stdout */
1106 case '*': /* Reg expr - any sequence of chars */
1107 case '?': /* Reg expr - any single char */
1108 case '$': /* Variable substitution */
1109 case '\'': /* Singe quote - turn off all magic */
1110 case '"': /* Double quote - span whitespace */
1111 case '`': /* Backquote - run a command */
1112 case '#': /* Comment */
1113 case ' ': /* Space (for MACRO=value1 value2 */
1114 case '\\': /* Escape char - turn off magic of next char */
1115 *to++ = '\\';
1116 break;
1118 default:
1119 break;
1121 *to++ = *from;
1123 *to = '\0';
1126 static void
1127 unquote_str(char *str, char *qstr)
1129 char *to;
1130 char *from;
1132 to = qstr;
1133 for (from = str; *from; from++) {
1134 if (*from == '\\') {
1135 from++;
1137 *to++ = *from;
1139 *to = '\0';
1143 * Convert the MAKEFLAGS string value into a vector of char *, similar
1144 * to argv.
1146 static void
1147 setup_makeflags_argv()
1149 char *cp;
1150 char *cp1;
1151 char *cp2;
1152 char *cp3;
1153 char *cp_orig;
1154 Boolean add_hyphen;
1155 int i;
1156 char tmp_char;
1158 mf_argc = 1;
1159 cp = getenv(makeflags->string_mb);
1160 cp_orig = cp;
1162 if (cp) {
1164 * If new MAKEFLAGS format, no need to add hyphen.
1165 * If old MAKEFLAGS format, add hyphen before flags.
1168 if ((strchr(cp, (int) hyphen_char) != NULL) ||
1169 (strchr(cp, (int) equal_char) != NULL)) {
1171 /* New MAKEFLAGS format */
1173 add_hyphen = false;
1175 /* Check if MAKEFLAGS value begins with multiple
1176 * hyphen characters, and remove all duplicates.
1177 * Usually it happens when the next command is
1178 * used: $(MAKE) -$(MAKEFLAGS)
1180 * This was a workaround for BugID 5060758, but
1181 * appears to have survived as a fix in make.
1183 while (*cp) {
1184 if (*cp != (int) hyphen_char) {
1185 break;
1187 cp++;
1188 if (*cp == (int) hyphen_char) {
1189 /* There are two hyphens. Skip one */
1190 cp_orig = cp;
1191 cp++;
1193 if (!(*cp)) {
1194 /* There are hyphens only. Skip all */
1195 cp_orig = cp;
1196 break;
1199 } else {
1201 /* Old MAKEFLAGS format */
1203 add_hyphen = true;
1207 /* Find the number of arguments in MAKEFLAGS */
1208 while (cp && *cp) {
1209 /* Skip white spaces */
1210 while (cp && *cp && isspace(*cp)) {
1211 cp++;
1213 if (cp && *cp) {
1214 /* Increment arg count */
1215 mf_argc++;
1216 /* Go to next white space */
1217 while (cp && *cp && !isspace(*cp)) {
1218 if(*cp == (int) backslash_char) {
1219 cp++;
1221 cp++;
1225 /* Allocate memory for the new MAKEFLAGS argv */
1226 mf_argv = (char **) malloc((mf_argc + 1) * sizeof(char *));
1227 mf_argv[0] = (char *)"MAKEFLAGS";
1229 * Convert the MAKEFLAGS string value into a vector of char *,
1230 * similar to argv.
1232 cp = cp_orig;
1233 for (i = 1; i < mf_argc; i++) {
1234 /* Skip white spaces */
1235 while (cp && *cp && isspace(*cp)) {
1236 cp++;
1238 if (cp && *cp) {
1239 cp_orig = cp;
1240 /* Go to next white space */
1241 while (cp && *cp && !isspace(*cp)) {
1242 if(*cp == (int) backslash_char) {
1243 cp++;
1245 cp++;
1247 tmp_char = *cp;
1248 *cp = (int) nul_char;
1249 if (add_hyphen) {
1250 mf_argv[i] = getmem(2 + strlen(cp_orig));
1251 mf_argv[i][0] = '\0';
1252 (void) strcat(mf_argv[i], "-");
1253 // (void) strcat(mf_argv[i], cp_orig);
1254 unquote_str(cp_orig, mf_argv[i]+1);
1255 } else {
1256 mf_argv[i] = getmem(2 + strlen(cp_orig));
1257 //mf_argv[i] = strdup(cp_orig);
1258 unquote_str(cp_orig, mf_argv[i]);
1260 *cp = tmp_char;
1263 mf_argv[i] = NULL;
1267 * parse_command_option(ch)
1269 * Parse make command line options.
1271 * Return value:
1272 * Indicates if any -f -c or -M were seen
1274 * Parameters:
1275 * ch The character to parse
1277 * Static variables used:
1278 * dmake_group_specified Set for make -g
1279 * dmake_max_jobs_specified Set for make -j
1280 * dmake_mode_specified Set for make -m
1281 * dmake_add_mode_specified Set for make -x
1282 * dmake_compat_mode_specified Set for make -x SUN_MAKE_COMPAT_MODE=
1283 * dmake_output_mode_specified Set for make -x DMAKE_OUTPUT_MODE=
1284 * dmake_odir_specified Set for make -o
1285 * dmake_rcfile_specified Set for make -c
1286 * env_wins Set for make -e
1287 * ignore_default_mk Set for make -r
1288 * trace_status Set for make -p
1290 * Global variables used:
1291 * .make.state path & name set for make -K
1292 * continue_after_error Set for make -k
1293 * debug_level Set for make -d
1294 * do_not_exec_rule Set for make -n
1295 * filter_stderr Set for make -X
1296 * ignore_errors_all Set for make -i
1297 * no_parallel Set for make -R
1298 * quest Set for make -q
1299 * read_trace_level Set for make -D
1300 * report_dependencies Set for make -P
1301 * silent_all Set for make -s
1302 * touch Set for make -t
1304 static int
1305 parse_command_option(register char ch)
1307 static int invert_next = 0;
1308 int invert_this = invert_next;
1310 invert_next = 0;
1311 switch (ch) {
1312 case '-': /* Ignore "--" */
1313 return 0;
1314 case '~': /* Invert next option */
1315 invert_next = 1;
1316 return 0;
1317 case 'B': /* Obsolete */
1318 return 0;
1319 case 'b': /* Obsolete */
1320 return 0;
1321 case 'C': /* Change directory */
1322 return 0;
1323 case 'c': /* Read alternative dmakerc file */
1324 if (invert_this) {
1325 dmake_rcfile_specified = false;
1326 } else {
1327 dmake_rcfile_specified = true;
1329 return 2;
1330 case 'D': /* Show lines read */
1331 if (invert_this) {
1332 read_trace_level--;
1333 } else {
1334 read_trace_level++;
1336 return 0;
1337 case 'd': /* Debug flag */
1338 if (invert_this) {
1339 debug_level--;
1340 } else {
1341 debug_level++;
1343 return 0;
1344 case 'e': /* Environment override flag */
1345 if (invert_this) {
1346 env_wins = false;
1347 } else {
1348 env_wins = true;
1350 return 0;
1351 case 'f': /* Read alternative makefile(s) */
1352 return 1;
1353 case 'g': /* Use alternative DMake group */
1354 if (invert_this) {
1355 dmake_group_specified = false;
1356 } else {
1357 dmake_group_specified = true;
1359 return 4;
1360 case 'i': /* Ignore errors */
1361 if (invert_this) {
1362 ignore_errors_all = false;
1363 } else {
1364 ignore_errors_all = true;
1366 return 0;
1367 case 'j': /* Use alternative DMake max jobs */
1368 if (invert_this) {
1369 dmake_max_jobs_specified = false;
1370 } else {
1371 dmake_mode_type = parallel_mode;
1372 no_parallel = false;
1373 dmake_max_jobs_specified = true;
1375 return 8;
1376 case 'K': /* Read alternative .make.state */
1377 return 256;
1378 case 'k': /* Keep making even after errors */
1379 if (invert_this) {
1380 continue_after_error = false;
1381 } else {
1382 continue_after_error = true;
1383 continue_after_error_ever_seen = true;
1385 return 0;
1386 case 'M': /* Read alternative make.machines file */
1387 if (invert_this) {
1388 pmake_machinesfile_specified = false;
1389 } else {
1390 pmake_machinesfile_specified = true;
1391 dmake_mode_type = parallel_mode;
1392 no_parallel = false;
1394 return 16;
1395 case 'm': /* Use alternative DMake build mode */
1396 if (invert_this) {
1397 dmake_mode_specified = false;
1398 } else {
1399 dmake_mode_specified = true;
1401 return 32;
1402 case 'x': /* Use alternative DMake mode */
1403 if (invert_this) {
1404 dmake_add_mode_specified = false;
1405 } else {
1406 dmake_add_mode_specified = true;
1408 return 1024;
1409 case 'N': /* Reverse -n */
1410 if (invert_this) {
1411 do_not_exec_rule = true;
1412 } else {
1413 do_not_exec_rule = false;
1415 return 0;
1416 case 'n': /* Print, not exec commands */
1417 if (invert_this) {
1418 do_not_exec_rule = false;
1419 } else {
1420 do_not_exec_rule = true;
1422 return 0;
1423 case 'O': /* Integrate with maketool, obsolete */
1424 return 0;
1425 case 'o': /* Use alternative dmake output dir */
1426 if (invert_this) {
1427 dmake_odir_specified = false;
1428 } else {
1429 dmake_odir_specified = true;
1431 return 512;
1432 case 'P': /* Print for selected targets */
1433 if (invert_this) {
1434 report_dependencies_level--;
1435 } else {
1436 report_dependencies_level++;
1438 return 0;
1439 case 'p': /* Print description */
1440 if (invert_this) {
1441 trace_status = false;
1442 do_not_exec_rule = false;
1443 } else {
1444 trace_status = true;
1445 do_not_exec_rule = true;
1447 return 0;
1448 case 'q': /* Question flag */
1449 if (invert_this) {
1450 quest = false;
1451 } else {
1452 quest = true;
1454 return 0;
1455 case 'R': /* Don't run in parallel */
1456 if (invert_this) {
1457 pmake_cap_r_specified = false;
1458 no_parallel = false;
1459 } else {
1460 pmake_cap_r_specified = true;
1461 dmake_mode_type = serial_mode;
1462 no_parallel = true;
1464 return 0;
1465 case 'r': /* Turn off internal rules */
1466 if (invert_this) {
1467 ignore_default_mk = false;
1468 } else {
1469 ignore_default_mk = true;
1471 return 0;
1472 case 'S': /* Reverse -k */
1473 if (invert_this) {
1474 continue_after_error = true;
1475 } else {
1476 continue_after_error = false;
1477 stop_after_error_ever_seen = true;
1479 return 0;
1480 case 's': /* Silent flag */
1481 if (invert_this) {
1482 silent_all = false;
1483 } else {
1484 silent_all = true;
1486 return 0;
1487 case 'T': /* Print target list */
1488 if (invert_this) {
1489 list_all_targets = false;
1490 do_not_exec_rule = false;
1491 } else {
1492 list_all_targets = true;
1493 do_not_exec_rule = true;
1495 return 0;
1496 case 't': /* Touch flag */
1497 if (invert_this) {
1498 touch = false;
1499 } else {
1500 touch = true;
1502 return 0;
1503 case 'u': /* Unconditional flag */
1504 if (invert_this) {
1505 build_unconditional = false;
1506 } else {
1507 build_unconditional = true;
1509 return 0;
1510 case 'V': /* SVR4 mode */
1511 svr4 = true;
1512 return 0;
1513 case 'v': /* Version flag */
1514 if (invert_this) {
1515 } else {
1516 fprintf(stdout, "%s: %s\n", getprogname(), verstring);
1517 exit_status = 0;
1518 exit(0);
1520 return 0;
1521 case 'w': /* Unconditional flag */
1522 if (invert_this) {
1523 report_cwd = false;
1524 } else {
1525 report_cwd = true;
1527 return 0;
1528 #if 0
1529 case 'X': /* Filter stdout */
1530 if (invert_this) {
1531 filter_stderr = false;
1532 } else {
1533 filter_stderr = true;
1535 return 0;
1536 #endif
1537 default:
1538 break;
1540 return 0;
1544 * setup_for_projectdir()
1546 * Read the PROJECTDIR variable, if defined, and set the sccs path
1548 * Parameters:
1550 * Global variables used:
1551 * sccs_dir_path Set to point to SCCS dir to use
1553 static void
1554 setup_for_projectdir(void)
1556 static char path[MAXPATHLEN];
1557 char cwdpath[MAXPATHLEN];
1558 uid_t uid;
1559 int done=0;
1561 /* Check if we should use PROJECTDIR when reading the SCCS dir. */
1562 sccs_dir_path = getenv("PROJECTDIR");
1563 if ((sccs_dir_path != NULL) &&
1564 (sccs_dir_path[0] != (int) slash_char)) {
1565 struct passwd *pwent;
1568 uid = getuid();
1569 pwent = getpwuid(uid);
1570 if (pwent == NULL) {
1571 fatal(gettext("Bogus USERID "));
1573 if ((pwent = getpwnam(sccs_dir_path)) == NULL) {
1574 /*empty block : it'll go & check cwd */
1576 else {
1577 (void) sprintf(path, "%s/src", pwent->pw_dir);
1578 if (access(path, F_OK) == 0) {
1579 sccs_dir_path = path;
1580 done = 1;
1581 } else {
1582 (void) sprintf(path, "%s/source", pwent->pw_dir);
1583 if (access(path, F_OK) == 0) {
1584 sccs_dir_path = path;
1585 done = 1;
1589 if (!done) {
1590 if (getcwd(cwdpath, MAXPATHLEN - 1 )) {
1592 (void) sprintf(path, "%s/%s", cwdpath,sccs_dir_path);
1593 if (access(path, F_OK) == 0) {
1594 sccs_dir_path = path;
1595 done = 1;
1596 } else {
1597 fatal(gettext("Bogus PROJECTDIR '%s'"), sccs_dir_path);
1605 char *
1606 make_install_prefix(void)
1608 int ret;
1609 char origin[PATH_MAX];
1610 char *dir;
1612 if ((ret = readlink("/proc/self/path/a.out", origin,
1613 PATH_MAX - 1)) < 0)
1614 fatal("failed to read origin from /proc\n");
1617 origin[ret] = '\0';
1618 return strdup(dirname(origin));
1621 static char *
1622 add_to_env(const char *var, const char *value, const char *fallback)
1624 const char *oldpath;
1625 char *newpath;
1627 oldpath = getenv(var);
1628 if (oldpath == NULL) {
1629 if (value != NULL) {
1630 asprintf(&newpath, "%s=%s",
1631 var, value);
1632 } else {
1633 asprintf(&newpath, "%s=%s",
1634 var, fallback);
1636 } else {
1637 if (value != NULL) {
1638 asprintf(&newpath, "%s=%s:%s",
1639 var, oldpath, value);
1640 } else {
1641 asprintf(&newpath, "%s=%s:%s",
1642 var, oldpath, fallback);
1646 return (newpath);
1650 * set_sgs_support()
1652 * Add the libmakestate.so.1 lib to the env var SGS_SUPPORT
1653 * if it's not already in there.
1654 * The SGS_SUPPORT env var and libmakestate.so.1 is used by
1655 * the linker ld to report .make.state info back to make.
1657 * In the new world we always will set the 32-bit and 64-bit versions of this
1658 * variable explicitly so that we can take into account the correct isa and our
1659 * prefix. So say that the prefix was /opt/local. Then we would want to search
1660 * /opt/local/lib/libmakestate.so.1:libmakestate.so.1. We still want to search
1661 * the original location just as a safety measure.
1663 static void
1664 set_sgs_support()
1666 int len;
1667 char *newpath, *newpath64;
1668 char *lib32, *lib64;
1669 static char *prev_path, *prev_path64;
1670 char *origin = make_install_prefix();
1671 struct stat st;
1673 asprintf(&lib32, "%s/%s/%s", origin, "../lib",
1674 LD_SUPPORT_MAKE_LIB);
1676 if (stat(lib32, &st) != 0) {
1677 free(lib32);
1678 // Try the tools path
1679 asprintf(&lib32, "%s/%s/%s/%s", origin, "../../lib/",
1680 LD_SUPPORT_MAKE_ARCH, LD_SUPPORT_MAKE_LIB);
1682 if (stat(lib32, &st) != 0) {
1683 free(lib32);
1684 lib32 = NULL;
1688 asprintf(&lib64, "%s/%s/64/%s", origin, "../lib",
1689 LD_SUPPORT_MAKE_LIB);
1691 if (stat(lib64, &st) != 0) {
1692 free(lib64);
1693 // Try the tools path
1694 asprintf(&lib64, "%s/%s/%s/64/%s", origin, "../../lib/",
1695 LD_SUPPORT_MAKE_ARCH, LD_SUPPORT_MAKE_LIB);
1697 if (stat(lib64, &st) != 0) {
1698 free(lib64);
1699 lib64 = NULL;
1703 newpath = add_to_env(LD_SUPPORT_ENV_VAR_32, lib32, LD_SUPPORT_MAKE_LIB);
1704 newpath64 = add_to_env(LD_SUPPORT_ENV_VAR_64, lib64, LD_SUPPORT_MAKE_LIB);
1706 putenv(newpath);
1707 if (prev_path) {
1708 free(prev_path);
1710 prev_path = newpath;
1712 putenv(newpath64);
1713 if (prev_path64) {
1714 free(prev_path64);
1716 prev_path64 = newpath64;
1717 free(lib32);
1718 free(lib64);
1719 free(origin);
1723 * read_files_and_state(argc, argv)
1725 * Read the makefiles we care about and the environment
1726 * Also read the = style command line options
1728 * Parameters:
1729 * argc You know what this is
1730 * argv You know what this is
1732 * Static variables used:
1733 * env_wins make -e, determines if env vars are RO
1734 * ignore_default_mk make -r, determines if make.rules is read
1735 * not_auto_depen dwight
1737 * Global variables used:
1738 * default_target_to_build Set to first proper target from file
1739 * do_not_exec_rule Set to false when makfile is made
1740 * dot The Name ".", used to read current dir
1741 * empty_name The Name "", use as macro value
1742 * keep_state Set if KEEP_STATE is in environment
1743 * make_state The Name ".make.state", used to read file
1744 * makefile_type Set to type of file being read
1745 * makeflags The Name "MAKEFLAGS", used to set macro value
1746 * not_auto dwight
1747 * read_trace_level Checked to se if the reader should trace
1748 * report_dependencies If -P is on we do not read .make.state
1749 * trace_reader Set if reader should trace
1750 * virtual_root The Name "VIRTUAL_ROOT", used to check value
1752 static void
1753 read_files_and_state(int argc, char **argv)
1755 wchar_t buffer[1000];
1756 wchar_t buffer_posix[1000];
1757 register char ch;
1758 register char *cp;
1759 Property def_make_macro = NULL;
1760 Name def_make_name;
1761 Name default_makefile;
1762 String_rec dest;
1763 wchar_t destbuffer[STRING_BUFFER_LENGTH];
1764 register int i;
1765 register int j;
1766 Name keep_state_name;
1767 int length;
1768 Name Makefile;
1769 register Property macro;
1770 struct stat make_state_stat;
1771 Name makefile_name;
1772 register int makefile_next = 0;
1773 register Boolean makefile_read = false;
1774 String_rec makeflags_string;
1775 String_rec makeflags_string_posix;
1776 String_rec * makeflags_string_current;
1777 Name makeflags_value_saved;
1778 register Name name;
1779 Name new_make_value;
1780 Boolean save_do_not_exec_rule;
1781 Name sdotMakefile;
1782 Name sdotmakefile_name;
1783 static wchar_t state_file_str;
1784 static char state_file_str_mb[MAXPATHLEN];
1785 static struct _Name state_filename;
1786 Boolean temp;
1787 char tmp_char;
1788 wchar_t *tmp_wcs_buffer;
1789 register Name value;
1790 ASCII_Dyn_Array makeflags_and_macro;
1791 Boolean is_xpg4;
1794 * Remember current mode. It may be changed after reading makefile
1795 * and we will have to correct MAKEFLAGS variable.
1797 is_xpg4 = posix;
1799 MBSTOWCS(wcs_buffer, "KEEP_STATE");
1800 keep_state_name = GETNAME(wcs_buffer, FIND_LENGTH);
1801 MBSTOWCS(wcs_buffer, "Makefile");
1802 Makefile = GETNAME(wcs_buffer, FIND_LENGTH);
1803 MBSTOWCS(wcs_buffer, "makefile");
1804 makefile_name = GETNAME(wcs_buffer, FIND_LENGTH);
1805 MBSTOWCS(wcs_buffer, "s.makefile");
1806 sdotmakefile_name = GETNAME(wcs_buffer, FIND_LENGTH);
1807 MBSTOWCS(wcs_buffer, "s.Makefile");
1808 sdotMakefile = GETNAME(wcs_buffer, FIND_LENGTH);
1811 * initialize global dependency entry for .NOT_AUTO
1813 not_auto_depen->next = NULL;
1814 not_auto_depen->name = not_auto;
1815 not_auto_depen->automatic = not_auto_depen->stale = false;
1818 * Read internal definitions and rules.
1820 if (read_trace_level > 1) {
1821 trace_reader = true;
1823 if (!ignore_default_mk) {
1824 if (svr4) {
1825 MBSTOWCS(wcs_buffer, "svr4.make.rules");
1826 default_makefile = GETNAME(wcs_buffer, FIND_LENGTH);
1827 } else {
1828 MBSTOWCS(wcs_buffer, "make.rules");
1829 default_makefile = GETNAME(wcs_buffer, FIND_LENGTH);
1831 default_makefile->stat.is_file = true;
1833 (void) read_makefile(default_makefile,
1834 true,
1835 false,
1836 true);
1840 * If the user did not redefine the MAKE macro in the
1841 * default makefile (make.rules), then we'd like to
1842 * change the macro value of MAKE to be some form
1843 * of argv[0] for recursive MAKE builds.
1845 MBSTOWCS(wcs_buffer, "MAKE");
1846 def_make_name = GETNAME(wcs_buffer, wcslen(wcs_buffer));
1847 def_make_macro = get_prop(def_make_name->prop, macro_prop);
1848 if ((def_make_macro != NULL) &&
1849 (IS_EQUAL(def_make_macro->body.macro.value->string_mb,
1850 "make"))) {
1851 MBSTOWCS(wcs_buffer, argv_zero_string);
1852 new_make_value = GETNAME(wcs_buffer, wcslen(wcs_buffer));
1853 (void) SETVAR(def_make_name,
1854 new_make_value,
1855 false);
1858 default_target_to_build = NULL;
1859 trace_reader = false;
1862 * Read environment args. Let file args which follow override unless
1863 * -e option seen. If -e option is not mentioned.
1865 read_environment(env_wins);
1866 if (getvar(virtual_root)->hash.length == 0) {
1867 maybe_append_prop(virtual_root, macro_prop)
1868 ->body.macro.exported = true;
1869 MBSTOWCS(wcs_buffer, "/");
1870 (void) SETVAR(virtual_root,
1871 GETNAME(wcs_buffer, FIND_LENGTH),
1872 false);
1876 * We now scan mf_argv and argv to see if we need to set
1877 * any of the DMake-added options/variables in MAKEFLAGS.
1880 makeflags_and_macro.start = 0;
1881 makeflags_and_macro.size = 0;
1882 enter_argv_values(mf_argc, mf_argv, &makeflags_and_macro);
1883 enter_argv_values(argc, argv, &makeflags_and_macro);
1886 * Set MFLAGS and MAKEFLAGS
1888 * Before reading makefile we do not know exactly which mode
1889 * (posix or not) is used. So prepare two MAKEFLAGS strings
1890 * for both posix and solaris modes because they are different.
1892 INIT_STRING_FROM_STACK(makeflags_string, buffer);
1893 INIT_STRING_FROM_STACK(makeflags_string_posix, buffer_posix);
1894 append_char((int) hyphen_char, &makeflags_string);
1895 append_char((int) hyphen_char, &makeflags_string_posix);
1897 switch (read_trace_level) {
1898 case 2:
1899 append_char('D', &makeflags_string);
1900 append_char('D', &makeflags_string_posix);
1901 case 1:
1902 append_char('D', &makeflags_string);
1903 append_char('D', &makeflags_string_posix);
1905 switch (debug_level) {
1906 case 2:
1907 append_char('d', &makeflags_string);
1908 append_char('d', &makeflags_string_posix);
1909 case 1:
1910 append_char('d', &makeflags_string);
1911 append_char('d', &makeflags_string_posix);
1913 if (env_wins) {
1914 append_char('e', &makeflags_string);
1915 append_char('e', &makeflags_string_posix);
1917 if (ignore_errors_all) {
1918 append_char('i', &makeflags_string);
1919 append_char('i', &makeflags_string_posix);
1921 if (continue_after_error) {
1922 if (stop_after_error_ever_seen) {
1923 append_char('S', &makeflags_string_posix);
1924 append_char((int) space_char, &makeflags_string_posix);
1925 append_char((int) hyphen_char, &makeflags_string_posix);
1927 append_char('k', &makeflags_string);
1928 append_char('k', &makeflags_string_posix);
1929 } else {
1930 if (stop_after_error_ever_seen
1931 && continue_after_error_ever_seen) {
1932 append_char('k', &makeflags_string_posix);
1933 append_char((int) space_char, &makeflags_string_posix);
1934 append_char((int) hyphen_char, &makeflags_string_posix);
1935 append_char('S', &makeflags_string_posix);
1938 if (do_not_exec_rule) {
1939 append_char('n', &makeflags_string);
1940 append_char('n', &makeflags_string_posix);
1942 switch (report_dependencies_level) {
1943 case 4:
1944 append_char('P', &makeflags_string);
1945 append_char('P', &makeflags_string_posix);
1946 case 3:
1947 append_char('P', &makeflags_string);
1948 append_char('P', &makeflags_string_posix);
1949 case 2:
1950 append_char('P', &makeflags_string);
1951 append_char('P', &makeflags_string_posix);
1952 case 1:
1953 append_char('P', &makeflags_string);
1954 append_char('P', &makeflags_string_posix);
1956 if (trace_status) {
1957 append_char('p', &makeflags_string);
1958 append_char('p', &makeflags_string_posix);
1960 if (quest) {
1961 append_char('q', &makeflags_string);
1962 append_char('q', &makeflags_string_posix);
1964 if (silent_all) {
1965 append_char('s', &makeflags_string);
1966 append_char('s', &makeflags_string_posix);
1968 if (touch) {
1969 append_char('t', &makeflags_string);
1970 append_char('t', &makeflags_string_posix);
1972 if (build_unconditional) {
1973 append_char('u', &makeflags_string);
1974 append_char('u', &makeflags_string_posix);
1976 if (report_cwd) {
1977 append_char('w', &makeflags_string);
1978 append_char('w', &makeflags_string_posix);
1980 /* -c dmake_rcfile */
1981 if (dmake_rcfile_specified) {
1982 MBSTOWCS(wcs_buffer, "DMAKE_RCFILE");
1983 dmake_rcfile = GETNAME(wcs_buffer, FIND_LENGTH);
1984 append_makeflags_string(dmake_rcfile, &makeflags_string);
1985 append_makeflags_string(dmake_rcfile, &makeflags_string_posix);
1987 /* -g dmake_group */
1988 if (dmake_group_specified) {
1989 MBSTOWCS(wcs_buffer, "DMAKE_GROUP");
1990 dmake_group = GETNAME(wcs_buffer, FIND_LENGTH);
1991 append_makeflags_string(dmake_group, &makeflags_string);
1992 append_makeflags_string(dmake_group, &makeflags_string_posix);
1994 /* -j dmake_max_jobs */
1995 if (dmake_max_jobs_specified) {
1996 MBSTOWCS(wcs_buffer, "DMAKE_MAX_JOBS");
1997 dmake_max_jobs = GETNAME(wcs_buffer, FIND_LENGTH);
1998 append_makeflags_string(dmake_max_jobs, &makeflags_string);
1999 append_makeflags_string(dmake_max_jobs, &makeflags_string_posix);
2001 /* -m dmake_mode */
2002 if (dmake_mode_specified) {
2003 MBSTOWCS(wcs_buffer, "DMAKE_MODE");
2004 dmake_mode = GETNAME(wcs_buffer, FIND_LENGTH);
2005 append_makeflags_string(dmake_mode, &makeflags_string);
2006 append_makeflags_string(dmake_mode, &makeflags_string_posix);
2008 /* -x dmake_compat_mode */
2009 // if (dmake_compat_mode_specified) {
2010 // MBSTOWCS(wcs_buffer, "SUN_MAKE_COMPAT_MODE");
2011 // dmake_compat_mode = GETNAME(wcs_buffer, FIND_LENGTH);
2012 // append_makeflags_string(dmake_compat_mode, &makeflags_string);
2013 // append_makeflags_string(dmake_compat_mode, &makeflags_string_posix);
2014 // }
2015 /* -x dmake_output_mode */
2016 if (dmake_output_mode_specified) {
2017 MBSTOWCS(wcs_buffer, "DMAKE_OUTPUT_MODE");
2018 dmake_output_mode = GETNAME(wcs_buffer, FIND_LENGTH);
2019 append_makeflags_string(dmake_output_mode, &makeflags_string);
2020 append_makeflags_string(dmake_output_mode, &makeflags_string_posix);
2022 /* -o dmake_odir */
2023 if (dmake_odir_specified) {
2024 MBSTOWCS(wcs_buffer, "DMAKE_ODIR");
2025 dmake_odir = GETNAME(wcs_buffer, FIND_LENGTH);
2026 append_makeflags_string(dmake_odir, &makeflags_string);
2027 append_makeflags_string(dmake_odir, &makeflags_string_posix);
2029 /* -M pmake_machinesfile */
2030 if (pmake_machinesfile_specified) {
2031 MBSTOWCS(wcs_buffer, "PMAKE_MACHINESFILE");
2032 pmake_machinesfile = GETNAME(wcs_buffer, FIND_LENGTH);
2033 append_makeflags_string(pmake_machinesfile, &makeflags_string);
2034 append_makeflags_string(pmake_machinesfile, &makeflags_string_posix);
2036 /* -R */
2037 if (pmake_cap_r_specified) {
2038 append_char((int) space_char, &makeflags_string);
2039 append_char((int) hyphen_char, &makeflags_string);
2040 append_char('R', &makeflags_string);
2041 append_char((int) space_char, &makeflags_string_posix);
2042 append_char((int) hyphen_char, &makeflags_string_posix);
2043 append_char('R', &makeflags_string_posix);
2047 * Make sure MAKEFLAGS is exported
2049 maybe_append_prop(makeflags, macro_prop)->
2050 body.macro.exported = true;
2052 if (makeflags_string.buffer.start[1] != (int) nul_char) {
2053 if (makeflags_string.buffer.start[1] != (int) space_char) {
2054 MBSTOWCS(wcs_buffer, "MFLAGS");
2055 (void) SETVAR(GETNAME(wcs_buffer, FIND_LENGTH),
2056 GETNAME(makeflags_string.buffer.start,
2057 FIND_LENGTH),
2058 false);
2059 } else {
2060 MBSTOWCS(wcs_buffer, "MFLAGS");
2061 (void) SETVAR(GETNAME(wcs_buffer, FIND_LENGTH),
2062 GETNAME(makeflags_string.buffer.start + 2,
2063 FIND_LENGTH),
2064 false);
2069 * Add command line macro to POSIX makeflags_string
2071 if (makeflags_and_macro.start) {
2072 tmp_char = (char) space_char;
2073 cp = makeflags_and_macro.start;
2074 do {
2075 append_char(tmp_char, &makeflags_string_posix);
2076 } while ( tmp_char = *cp++ );
2077 retmem_mb(makeflags_and_macro.start);
2081 * Now set the value of MAKEFLAGS macro in accordance
2082 * with current mode.
2084 macro = maybe_append_prop(makeflags, macro_prop);
2085 temp = (Boolean) macro->body.macro.read_only;
2086 macro->body.macro.read_only = false;
2087 if(posix || gnu_style) {
2088 makeflags_string_current = &makeflags_string_posix;
2089 } else {
2090 makeflags_string_current = &makeflags_string;
2092 if (makeflags_string_current->buffer.start[1] == (int) nul_char) {
2093 makeflags_value_saved =
2094 GETNAME( makeflags_string_current->buffer.start + 1
2095 , FIND_LENGTH
2097 } else {
2098 if (makeflags_string_current->buffer.start[1] != (int) space_char) {
2099 makeflags_value_saved =
2100 GETNAME( makeflags_string_current->buffer.start
2101 , FIND_LENGTH
2103 } else {
2104 makeflags_value_saved =
2105 GETNAME( makeflags_string_current->buffer.start + 2
2106 , FIND_LENGTH
2110 (void) SETVAR( makeflags
2111 , makeflags_value_saved
2112 , false
2114 macro->body.macro.read_only = temp;
2117 * Read command line "-f" arguments and ignore -c, g, j, K, M, m, O and o args.
2119 save_do_not_exec_rule = do_not_exec_rule;
2120 do_not_exec_rule = false;
2121 if (read_trace_level > 0) {
2122 trace_reader = true;
2125 for (i = 1; i < argc; i++) {
2126 if (argv[i] &&
2127 (argv[i][0] == (int) hyphen_char) &&
2128 (argv[i][1] == 'f') &&
2129 (argv[i][2] == (int) nul_char)) {
2130 argv[i] = NULL; /* Remove -f */
2131 if (i >= argc - 1) {
2132 fatal(gettext("No filename argument after -f flag"));
2134 MBSTOWCS(wcs_buffer, argv[++i]);
2135 primary_makefile = GETNAME(wcs_buffer, FIND_LENGTH);
2136 (void) read_makefile(primary_makefile, true, true, true);
2137 argv[i] = NULL; /* Remove filename */
2138 makefile_read = true;
2139 } else if (argv[i] &&
2140 (argv[i][0] == (int) hyphen_char) &&
2141 (argv[i][1] == 'c' ||
2142 argv[i][1] == 'g' ||
2143 argv[i][1] == 'j' ||
2144 argv[i][1] == 'K' ||
2145 argv[i][1] == 'M' ||
2146 argv[i][1] == 'm' ||
2147 argv[i][1] == 'O' ||
2148 argv[i][1] == 'o') &&
2149 (argv[i][2] == (int) nul_char)) {
2150 argv[i] = NULL;
2151 argv[++i] = NULL;
2156 * If no command line "-f" args then look for "makefile", and then for
2157 * "Makefile" if "makefile" isn't found.
2159 if (!makefile_read) {
2160 (void) read_dir(dot,
2161 (wchar_t *) NULL,
2162 (Property) NULL,
2163 (wchar_t *) NULL);
2164 if (!posix) {
2165 if (makefile_name->stat.is_file) {
2166 if (Makefile->stat.is_file) {
2167 warning(gettext("Both `makefile' and `Makefile' exist"));
2169 primary_makefile = makefile_name;
2170 makefile_read = read_makefile(makefile_name,
2171 false,
2172 false,
2173 true);
2175 if (!makefile_read &&
2176 Makefile->stat.is_file) {
2177 primary_makefile = Makefile;
2178 makefile_read = read_makefile(Makefile,
2179 false,
2180 false,
2181 true);
2183 } else {
2185 enum sccs_stat save_m_has_sccs = NO_SCCS;
2186 enum sccs_stat save_M_has_sccs = NO_SCCS;
2188 if (makefile_name->stat.is_file) {
2189 if (Makefile->stat.is_file) {
2190 warning(gettext("Both `makefile' and `Makefile' exist"));
2193 if (makefile_name->stat.is_file) {
2194 if (makefile_name->stat.has_sccs == NO_SCCS) {
2195 primary_makefile = makefile_name;
2196 makefile_read = read_makefile(makefile_name,
2197 false,
2198 false,
2199 true);
2200 } else {
2201 save_m_has_sccs = makefile_name->stat.has_sccs;
2202 makefile_name->stat.has_sccs = NO_SCCS;
2203 primary_makefile = makefile_name;
2204 makefile_read = read_makefile(makefile_name,
2205 false,
2206 false,
2207 true);
2210 if (!makefile_read &&
2211 Makefile->stat.is_file) {
2212 if (Makefile->stat.has_sccs == NO_SCCS) {
2213 primary_makefile = Makefile;
2214 makefile_read = read_makefile(Makefile,
2215 false,
2216 false,
2217 true);
2218 } else {
2219 save_M_has_sccs = Makefile->stat.has_sccs;
2220 Makefile->stat.has_sccs = NO_SCCS;
2221 primary_makefile = Makefile;
2222 makefile_read = read_makefile(Makefile,
2223 false,
2224 false,
2225 true);
2228 if (!makefile_read &&
2229 makefile_name->stat.is_file) {
2230 makefile_name->stat.has_sccs = save_m_has_sccs;
2231 primary_makefile = makefile_name;
2232 makefile_read = read_makefile(makefile_name,
2233 false,
2234 false,
2235 true);
2237 if (!makefile_read &&
2238 Makefile->stat.is_file) {
2239 Makefile->stat.has_sccs = save_M_has_sccs;
2240 primary_makefile = Makefile;
2241 makefile_read = read_makefile(Makefile,
2242 false,
2243 false,
2244 true);
2248 do_not_exec_rule = save_do_not_exec_rule;
2249 allrules_read = makefile_read;
2250 trace_reader = false;
2253 * Now get current value of MAKEFLAGS and compare it with
2254 * the saved value we set before reading makefile.
2255 * If they are different then MAKEFLAGS is subsequently set by
2256 * makefile, just leave it there. Otherwise, if make mode
2257 * is changed by using .POSIX target in makefile we need
2258 * to correct MAKEFLAGS value.
2260 Name mf_val = getvar(makeflags);
2261 if( (posix != is_xpg4)
2262 && (!strcmp(mf_val->string_mb, makeflags_value_saved->string_mb)))
2264 if (makeflags_string_posix.buffer.start[1] == (int) nul_char) {
2265 (void) SETVAR(makeflags,
2266 GETNAME(makeflags_string_posix.buffer.start + 1,
2267 FIND_LENGTH),
2268 false);
2269 } else {
2270 if (makeflags_string_posix.buffer.start[1] != (int) space_char) {
2271 (void) SETVAR(makeflags,
2272 GETNAME(makeflags_string_posix.buffer.start,
2273 FIND_LENGTH),
2274 false);
2275 } else {
2276 (void) SETVAR(makeflags,
2277 GETNAME(makeflags_string_posix.buffer.start + 2,
2278 FIND_LENGTH),
2279 false);
2284 if (makeflags_string.free_after_use) {
2285 retmem(makeflags_string.buffer.start);
2287 if (makeflags_string_posix.free_after_use) {
2288 retmem(makeflags_string_posix.buffer.start);
2290 makeflags_string.buffer.start = NULL;
2291 makeflags_string_posix.buffer.start = NULL;
2293 if (posix) {
2295 * If the user did not redefine the ARFLAGS macro in the
2296 * default makefile (make.rules), then we'd like to
2297 * change the macro value of ARFLAGS to be in accordance
2298 * with "POSIX" requirements.
2300 MBSTOWCS(wcs_buffer, "ARFLAGS");
2301 name = GETNAME(wcs_buffer, wcslen(wcs_buffer));
2302 macro = get_prop(name->prop, macro_prop);
2303 if ((macro != NULL) && /* Maybe (macro == NULL) || ? */
2304 (IS_EQUAL(macro->body.macro.value->string_mb,
2305 "rv"))) {
2306 MBSTOWCS(wcs_buffer, "-rv");
2307 value = GETNAME(wcs_buffer, wcslen(wcs_buffer));
2308 (void) SETVAR(name,
2309 value,
2310 false);
2314 if (!posix && !svr4) {
2315 set_sgs_support();
2320 * Make sure KEEP_STATE is in the environment if KEEP_STATE is on.
2322 macro = get_prop(keep_state_name->prop, macro_prop);
2323 if ((macro != NULL) &&
2324 macro->body.macro.exported) {
2325 keep_state = true;
2327 if (keep_state) {
2328 if (macro == NULL) {
2329 macro = maybe_append_prop(keep_state_name,
2330 macro_prop);
2332 macro->body.macro.exported = true;
2333 (void) SETVAR(keep_state_name,
2334 empty_name,
2335 false);
2338 * Read state file
2341 /* Before we read state, let's make sure we have
2342 ** right state file.
2344 /* just in case macro references are used in make_state file
2345 ** name, we better expand them at this stage using expand_value.
2347 INIT_STRING_FROM_STACK(dest, destbuffer);
2348 expand_value(make_state, &dest, false);
2350 make_state = GETNAME(dest.buffer.start, FIND_LENGTH);
2352 if(!stat(make_state->string_mb, &make_state_stat)) {
2353 if(!(make_state_stat.st_mode & S_IFREG) ) {
2354 /* copy the make_state structure to the other
2355 ** and then let make_state point to the new
2356 ** one.
2358 memcpy(&state_filename, make_state,sizeof(state_filename));
2359 state_filename.string_mb = state_file_str_mb;
2360 /* Just a kludge to avoid two slashes back to back */
2361 if((make_state->hash.length == 1)&&
2362 (make_state->string_mb[0] == '/')) {
2363 make_state->hash.length = 0;
2364 make_state->string_mb[0] = '\0';
2366 sprintf(state_file_str_mb,"%s%s",
2367 make_state->string_mb,"/.make.state");
2368 make_state = &state_filename;
2369 /* adjust the length to reflect the appended string */
2370 make_state->hash.length += 12;
2372 } else { /* the file doesn't exist or no permission */
2373 char tmp_path[MAXPATHLEN];
2374 char *slashp;
2376 if (slashp = strrchr(make_state->string_mb, '/')) {
2377 strncpy(tmp_path, make_state->string_mb,
2378 (slashp - make_state->string_mb));
2379 tmp_path[slashp - make_state->string_mb]=0;
2380 if(strlen(tmp_path)) {
2381 if(stat(tmp_path, &make_state_stat)) {
2382 warning(gettext("directory %s for .KEEP_STATE_FILE does not exist"),tmp_path);
2384 if (access(tmp_path, F_OK) != 0) {
2385 warning(gettext("can't access dir %s"),tmp_path);
2390 if (report_dependencies_level != 1) {
2391 Makefile_type makefile_type_temp = makefile_type;
2392 makefile_type = reading_statefile;
2393 if (read_trace_level > 1) {
2394 trace_reader = true;
2396 (void) read_simple_file(make_state,
2397 false,
2398 false,
2399 false,
2400 false,
2401 false,
2402 true);
2403 trace_reader = false;
2404 makefile_type = makefile_type_temp;
2410 * Scan the argv for options and "=" type args and make them readonly.
2412 static void
2413 enter_argv_values(int argc, char *argv[], ASCII_Dyn_Array *makeflags_and_macro)
2415 register char *cp;
2416 register int i;
2417 int length;
2418 register Name name;
2419 int opt_separator = argc;
2420 char tmp_char;
2421 wchar_t *tmp_wcs_buffer;
2422 register Name value;
2423 Boolean append = false;
2424 Property macro;
2425 struct stat statbuf;
2428 /* Read argv options and "=" type args and make them readonly. */
2429 makefile_type = reading_nothing;
2430 for (i = 1; i < argc; ++i) {
2431 append = false;
2432 if (argv[i] == NULL) {
2433 continue;
2434 } else if (((argv[i][0] == '-') && (argv[i][1] == '-')) ||
2435 ((argv[i][0] == (int) ' ') &&
2436 (argv[i][1] == (int) '-') &&
2437 (argv[i][2] == (int) ' ') &&
2438 (argv[i][3] == (int) '-'))) {
2439 argv[i] = NULL;
2440 opt_separator = i;
2441 continue;
2442 } else if ((i < opt_separator) && (argv[i][0] == (int) hyphen_char)) {
2443 switch (parse_command_option(argv[i][1])) {
2444 case 1: /* -f seen */
2445 ++i;
2446 continue;
2447 case 2: /* -c seen */
2448 if (argv[i+1] == NULL) {
2449 fatal(gettext("No dmake rcfile argument after -c flag"));
2451 MBSTOWCS(wcs_buffer, "DMAKE_RCFILE");
2452 name = GETNAME(wcs_buffer, FIND_LENGTH);
2453 break;
2454 case 4: /* -g seen */
2455 if (argv[i+1] == NULL) {
2456 fatal(gettext("No dmake group argument after -g flag"));
2458 MBSTOWCS(wcs_buffer, "DMAKE_GROUP");
2459 name = GETNAME(wcs_buffer, FIND_LENGTH);
2460 break;
2461 case 8: /* -j seen */
2462 if (argv[i+1] == NULL) {
2463 fatal(gettext("No dmake max jobs argument after -j flag"));
2465 MBSTOWCS(wcs_buffer, "DMAKE_MAX_JOBS");
2466 name = GETNAME(wcs_buffer, FIND_LENGTH);
2467 break;
2468 case 16: /* -M seen */
2469 if (argv[i+1] == NULL) {
2470 fatal(gettext("No pmake machinesfile argument after -M flag"));
2472 MBSTOWCS(wcs_buffer, "PMAKE_MACHINESFILE");
2473 name = GETNAME(wcs_buffer, FIND_LENGTH);
2474 break;
2475 case 32: /* -m seen */
2476 if (argv[i+1] == NULL) {
2477 fatal(gettext("No dmake mode argument after -m flag"));
2479 MBSTOWCS(wcs_buffer, "DMAKE_MODE");
2480 name = GETNAME(wcs_buffer, FIND_LENGTH);
2481 break;
2482 case 256: /* -K seen */
2483 if (argv[i+1] == NULL) {
2484 fatal(gettext("No makestate filename argument after -K flag"));
2486 MBSTOWCS(wcs_buffer, argv[i+1]);
2487 make_state = GETNAME(wcs_buffer, FIND_LENGTH);
2488 keep_state = true;
2489 argv[i] = NULL;
2490 argv[i+1] = NULL;
2491 continue;
2492 case 512: /* -o seen */
2493 if (argv[i+1] == NULL) {
2494 fatal(gettext("No dmake output dir argument after -o flag"));
2496 MBSTOWCS(wcs_buffer, "DMAKE_ODIR");
2497 name = GETNAME(wcs_buffer, FIND_LENGTH);
2498 break;
2499 case 1024: /* -x seen */
2500 if (argv[i+1] == NULL) {
2501 fatal(gettext("No argument after -x flag"));
2503 length = strlen( "SUN_MAKE_COMPAT_MODE=");
2504 if (strncmp(argv[i+1], "SUN_MAKE_COMPAT_MODE=", length) == 0) {
2505 argv[i+1] = &argv[i+1][length];
2506 MBSTOWCS(wcs_buffer, "SUN_MAKE_COMPAT_MODE");
2507 name = GETNAME(wcs_buffer, FIND_LENGTH);
2508 dmake_compat_mode_specified = dmake_add_mode_specified;
2509 break;
2511 length = strlen( "DMAKE_OUTPUT_MODE=");
2512 if (strncmp(argv[i+1], "DMAKE_OUTPUT_MODE=", length) == 0) {
2513 argv[i+1] = &argv[i+1][length];
2514 MBSTOWCS(wcs_buffer, "DMAKE_OUTPUT_MODE");
2515 name = GETNAME(wcs_buffer, FIND_LENGTH);
2516 dmake_output_mode_specified = dmake_add_mode_specified;
2517 } else {
2518 warning(gettext("Unknown argument `%s' after -x flag (ignored)"),
2519 argv[i+1]);
2520 argv[i] = argv[i + 1] = NULL;
2521 continue;
2523 break;
2524 default: /* Shouldn't reach here */
2525 argv[i] = NULL;
2526 continue;
2528 argv[i] = NULL;
2529 if (i == (argc - 1)) {
2530 break;
2532 if ((length = strlen(argv[i+1])) >= MAXPATHLEN) {
2533 tmp_wcs_buffer = ALLOC_WC(length + 1);
2534 (void) mbstowcs(tmp_wcs_buffer, argv[i+1], length + 1);
2535 value = GETNAME(tmp_wcs_buffer, FIND_LENGTH);
2536 retmem(tmp_wcs_buffer);
2537 } else {
2538 MBSTOWCS(wcs_buffer, argv[i+1]);
2539 value = GETNAME(wcs_buffer, FIND_LENGTH);
2541 argv[i+1] = NULL;
2542 } else if ((cp = strchr(argv[i], (int) equal_char)) != NULL) {
2544 * Combine all macro in dynamic array
2546 if(*(cp-1) == (int) plus_char)
2548 if(isspace(*(cp-2))) {
2549 append = true;
2550 cp--;
2553 if(!append)
2554 append_or_replace_macro_in_dyn_array(makeflags_and_macro, argv[i]);
2556 while (isspace(*(cp-1))) {
2557 cp--;
2559 tmp_char = *cp;
2560 *cp = (int) nul_char;
2561 MBSTOWCS(wcs_buffer, argv[i]);
2562 *cp = tmp_char;
2563 name = GETNAME(wcs_buffer, wcslen(wcs_buffer));
2564 while (*cp != (int) equal_char) {
2565 cp++;
2567 cp++;
2568 while (isspace(*cp) && (*cp != (int) nul_char)) {
2569 cp++;
2571 if ((length = strlen(cp)) >= MAXPATHLEN) {
2572 tmp_wcs_buffer = ALLOC_WC(length + 1);
2573 (void) mbstowcs(tmp_wcs_buffer, cp, length + 1);
2574 value = GETNAME(tmp_wcs_buffer, FIND_LENGTH);
2575 retmem(tmp_wcs_buffer);
2576 } else {
2577 MBSTOWCS(wcs_buffer, cp);
2578 value = GETNAME(wcs_buffer, FIND_LENGTH);
2580 argv[i] = NULL;
2581 } else {
2582 /* Illegal MAKEFLAGS argument */
2583 continue;
2585 if(append) {
2586 setvar_append(name, value);
2587 append = false;
2588 } else {
2589 macro = maybe_append_prop(name, macro_prop);
2590 macro->body.macro.exported = true;
2591 SETVAR(name, value, false)->body.macro.read_only = true;
2597 * Append the DMake option and value to the MAKEFLAGS string.
2599 static void
2600 append_makeflags_string(Name name, register String makeflags_string)
2602 const char *option;
2604 if (strcmp(name->string_mb, "DMAKE_GROUP") == 0) {
2605 option = " -g ";
2606 } else if (strcmp(name->string_mb, "DMAKE_MAX_JOBS") == 0) {
2607 option = " -j ";
2608 } else if (strcmp(name->string_mb, "DMAKE_MODE") == 0) {
2609 option = " -m ";
2610 } else if (strcmp(name->string_mb, "DMAKE_ODIR") == 0) {
2611 option = " -o ";
2612 } else if (strcmp(name->string_mb, "DMAKE_RCFILE") == 0) {
2613 option = " -c ";
2614 } else if (strcmp(name->string_mb, "PMAKE_MACHINESFILE") == 0) {
2615 option = " -M ";
2616 } else if (strcmp(name->string_mb, "DMAKE_OUTPUT_MODE") == 0) {
2617 option = " -x DMAKE_OUTPUT_MODE=";
2618 } else if (strcmp(name->string_mb, "SUN_MAKE_COMPAT_MODE") == 0) {
2619 option = " -x SUN_MAKE_COMPAT_MODE=";
2620 } else {
2621 fatal(gettext("Internal error: name not recognized in append_makeflags_string()"));
2623 Property prop = maybe_append_prop(name, macro_prop);
2624 if( prop == 0 || prop->body.macro.value == 0 ||
2625 prop->body.macro.value->string_mb == 0 ) {
2626 return;
2628 char mbs_value[MAXPATHLEN + 100];
2629 strcpy(mbs_value, option);
2630 strcat(mbs_value, prop->body.macro.value->string_mb);
2631 MBSTOWCS(wcs_buffer, mbs_value);
2632 append_string(wcs_buffer, makeflags_string, FIND_LENGTH);
2636 * read_environment(read_only)
2638 * This routine reads the process environment when make starts and enters
2639 * it as make macros. The environment variable SHELL is ignored.
2641 * Parameters:
2642 * read_only Should we make env vars read only?
2644 * Global variables used:
2645 * report_pwd Set if this make was started by other make
2647 static void
2648 read_environment(Boolean read_only)
2650 register char **environment;
2651 int length;
2652 wchar_t *tmp_wcs_buffer;
2653 Boolean alloced_tmp_wcs_buffer = false;
2654 register wchar_t *name;
2655 register wchar_t *value;
2656 register Name macro;
2657 Property val;
2658 Boolean read_only_saved;
2660 reading_environment = true;
2661 environment = environ;
2662 for (; *environment; environment++) {
2663 read_only_saved = read_only;
2664 if ((length = strlen(*environment)) >= MAXPATHLEN) {
2665 tmp_wcs_buffer = ALLOC_WC(length + 1);
2666 alloced_tmp_wcs_buffer = true;
2667 (void) mbstowcs(tmp_wcs_buffer, *environment, length + 1);
2668 name = tmp_wcs_buffer;
2669 } else {
2670 MBSTOWCS(wcs_buffer, *environment);
2671 name = wcs_buffer;
2673 value = (wchar_t *) wcschr(name, (int) equal_char);
2676 * Looks like there's a bug in the system, but sometimes
2677 * you can get blank lines in *environment.
2679 if (!value) {
2680 continue;
2682 MBSTOWCS(wcs_buffer2, "SHELL=");
2683 if (IS_WEQUALN(name, wcs_buffer2, wcslen(wcs_buffer2))) {
2684 continue;
2686 MBSTOWCS(wcs_buffer2, "MAKEFLAGS=");
2687 if (IS_WEQUALN(name, wcs_buffer2, wcslen(wcs_buffer2))) {
2688 report_pwd = true;
2690 * In POSIX mode we do not want MAKEFLAGS to be readonly.
2691 * If the MAKEFLAGS macro is subsequently set by the makefile,
2692 * it replaces the MAKEFLAGS variable currently found in the
2693 * environment.
2694 * See Assertion 50 in section 6.2.5.3 of standard P1003.3.2/D8.
2696 if(posix) {
2697 read_only_saved = false;
2702 * We ignore SUNPRO_DEPENDENCIES. This environment variable is
2703 * set by make and read by cpp which then writes info to
2704 * .make.dependency.xxx. When make is invoked by another make
2705 * (recursive make), we don't want to read this because then
2706 * the child make will end up writing to the parent
2707 * directory's .make.state and clobbering them.
2709 MBSTOWCS(wcs_buffer2, "SUNPRO_DEPENDENCIES");
2710 if (IS_WEQUALN(name, wcs_buffer2, wcslen(wcs_buffer2))) {
2711 continue;
2714 macro = GETNAME(name, value - name);
2715 maybe_append_prop(macro, macro_prop)->body.macro.exported =
2716 true;
2717 if ((value == NULL) || ((value + 1)[0] == (int) nul_char)) {
2718 val = setvar_daemon(macro,
2719 (Name) NULL,
2720 false, no_daemon, false, debug_level);
2721 } else {
2722 val = setvar_daemon(macro,
2723 GETNAME(value + 1, FIND_LENGTH),
2724 false, no_daemon, false, debug_level);
2726 val->body.macro.read_only = read_only_saved;
2727 if (alloced_tmp_wcs_buffer) {
2728 retmem(tmp_wcs_buffer);
2729 alloced_tmp_wcs_buffer = false;
2732 reading_environment = false;
2736 * read_makefile(makefile, complain, must_exist, report_file)
2738 * Read one makefile and check the result
2740 * Return value:
2741 * false is the read failed
2743 * Parameters:
2744 * makefile The file to read
2745 * complain Passed thru to read_simple_file()
2746 * must_exist Passed thru to read_simple_file()
2747 * report_file Passed thru to read_simple_file()
2749 * Global variables used:
2750 * makefile_type Set to indicate we are reading main file
2751 * recursion_level Initialized
2753 static Boolean
2754 read_makefile(register Name makefile, Boolean complain, Boolean must_exist, Boolean report_file)
2756 Boolean b;
2758 makefile_type = reading_makefile;
2759 recursion_level = 0;
2760 reading_dependencies = true;
2761 b = read_simple_file(makefile, true, true, complain,
2762 must_exist, report_file, false);
2763 reading_dependencies = false;
2764 return b;
2768 * make_targets(argc, argv, parallel_flag)
2770 * Call doname on the specified targets
2772 * Parameters:
2773 * argc You know what this is
2774 * argv You know what this is
2775 * parallel_flag True if building in parallel
2777 * Global variables used:
2778 * build_failed_seen Used to generated message after failed -k
2779 * commands_done Used to generate message "Up to date"
2780 * default_target_to_build First proper target in makefile
2781 * init The Name ".INIT", use to run command
2782 * parallel Global parallel building flag
2783 * quest make -q, suppresses messages
2784 * recursion_level Initialized, used for tracing
2785 * report_dependencies make -P, regroves whole process
2787 static void
2788 make_targets(int argc, char **argv, Boolean parallel_flag)
2790 int i;
2791 char *cp;
2792 Doname result;
2793 register Boolean target_to_make_found = false;
2795 (void) doname(init, true, true);
2796 recursion_level = 1;
2797 parallel = parallel_flag;
2799 * make remaining args
2802 if ((report_dependencies_level == 0) && parallel) {
2804 if (parallel) {
2806 * If building targets in parallel, start all of the
2807 * remaining args to build in parallel.
2809 for (i = 1; i < argc; i++) {
2810 if ((cp = argv[i]) != NULL) {
2811 commands_done = false;
2812 if ((cp[0] == (int) period_char) &&
2813 (cp[1] == (int) slash_char)) {
2814 cp += 2;
2816 if((cp[0] == (int) ' ') &&
2817 (cp[1] == (int) '-') &&
2818 (cp[2] == (int) ' ') &&
2819 (cp[3] == (int) '-')) {
2820 argv[i] = NULL;
2821 continue;
2823 MBSTOWCS(wcs_buffer, cp);
2824 //default_target_to_build = GETNAME(wcs_buffer,
2825 // FIND_LENGTH);
2826 default_target_to_build = normalize_name(wcs_buffer,
2827 wcslen(wcs_buffer));
2828 if (default_target_to_build == wait_name) {
2829 if (parallel_process_cnt > 0) {
2830 finish_running();
2832 continue;
2834 top_level_target = get_wstring(default_target_to_build->string_mb);
2836 * If we can't execute the current target in
2837 * parallel, hold off the target processing
2838 * to preserve the order of the targets as they appeared
2839 * in command line.
2841 if (!parallel_ok(default_target_to_build, false)
2842 && parallel_process_cnt > 0) {
2843 finish_running();
2845 result = doname_check(default_target_to_build,
2846 true,
2847 false,
2848 false);
2849 gather_recursive_deps();
2850 if (/* !commands_done && */
2851 (result == build_ok) &&
2852 !quest &&
2853 (report_dependencies_level == 0) /* &&
2854 (exists(default_target_to_build) > file_doesnt_exist) */) {
2855 if (posix) {
2856 if (!commands_done) {
2857 (void) printf(gettext("`%s' is updated.\n"),
2858 default_target_to_build->string_mb);
2859 } else {
2860 if (no_action_was_taken) {
2861 (void) printf(gettext("`%s': no action was taken.\n"),
2862 default_target_to_build->string_mb);
2865 } else {
2866 default_target_to_build->stat.time = file_no_time;
2867 if (!commands_done &&
2868 (exists(default_target_to_build) > file_doesnt_exist)) {
2869 (void) printf(gettext("`%s' is up to date.\n"),
2870 default_target_to_build->string_mb);
2876 /* Now wait for all of the targets to finish running */
2877 finish_running();
2878 // setjmp(jmpbuffer);
2881 for (i = 1; i < argc; i++) {
2882 if ((cp = argv[i]) != NULL) {
2883 target_to_make_found = true;
2884 if ((cp[0] == (int) period_char) &&
2885 (cp[1] == (int) slash_char)) {
2886 cp += 2;
2888 if((cp[0] == (int) ' ') &&
2889 (cp[1] == (int) '-') &&
2890 (cp[2] == (int) ' ') &&
2891 (cp[3] == (int) '-')) {
2892 argv[i] = NULL;
2893 continue;
2895 MBSTOWCS(wcs_buffer, cp);
2896 default_target_to_build = normalize_name(wcs_buffer, wcslen(wcs_buffer));
2897 top_level_target = get_wstring(default_target_to_build->string_mb);
2898 report_recursion(default_target_to_build);
2899 commands_done = false;
2900 if (parallel) {
2901 result = (Doname) default_target_to_build->state;
2902 } else {
2903 result = doname_check(default_target_to_build,
2904 true,
2905 false,
2906 false);
2908 gather_recursive_deps();
2909 if (build_failed_seen) {
2910 build_failed_ever_seen = true;
2911 warning(gettext("Target `%s' not remade because of errors"),
2912 default_target_to_build->string_mb);
2914 build_failed_seen = false;
2915 if (report_dependencies_level > 0) {
2916 print_dependencies(default_target_to_build,
2917 get_prop(default_target_to_build->prop,
2918 line_prop));
2920 default_target_to_build->stat.time =
2921 file_no_time;
2922 if (default_target_to_build->colon_splits > 0) {
2923 default_target_to_build->state =
2924 build_dont_know;
2926 if (!parallel &&
2927 /* !commands_done && */
2928 (result == build_ok) &&
2929 !quest &&
2930 (report_dependencies_level == 0) /* &&
2931 (exists(default_target_to_build) > file_doesnt_exist) */) {
2932 if (posix) {
2933 if (!commands_done) {
2934 (void) printf(gettext("`%s' is updated.\n"),
2935 default_target_to_build->string_mb);
2936 } else {
2937 if (no_action_was_taken) {
2938 (void) printf(gettext("`%s': no action was taken.\n"),
2939 default_target_to_build->string_mb);
2942 } else {
2943 if (!commands_done &&
2944 (exists(default_target_to_build) > file_doesnt_exist)) {
2945 (void) printf(gettext("`%s' is up to date.\n"),
2946 default_target_to_build->string_mb);
2954 * If no file arguments have been encountered,
2955 * make the first name encountered that doesnt start with a dot
2957 if (!target_to_make_found) {
2958 if (default_target_to_build == NULL) {
2959 fatal(gettext("No arguments to build"));
2961 commands_done = false;
2962 top_level_target = get_wstring(default_target_to_build->string_mb);
2963 report_recursion(default_target_to_build);
2966 if (getenv("SPRO_EXPAND_ERRORS")){
2967 (void) printf("::(%s)\n",
2968 default_target_to_build->string_mb);
2972 result = doname_parallel(default_target_to_build, true, false);
2973 gather_recursive_deps();
2974 if (build_failed_seen) {
2975 build_failed_ever_seen = true;
2976 warning(gettext("Target `%s' not remade because of errors"),
2977 default_target_to_build->string_mb);
2979 build_failed_seen = false;
2980 if (report_dependencies_level > 0) {
2981 print_dependencies(default_target_to_build,
2982 get_prop(default_target_to_build->
2983 prop,
2984 line_prop));
2986 default_target_to_build->stat.time = file_no_time;
2987 if (default_target_to_build->colon_splits > 0) {
2988 default_target_to_build->state = build_dont_know;
2990 if (/* !commands_done && */
2991 (result == build_ok) &&
2992 !quest &&
2993 (report_dependencies_level == 0) /* &&
2994 (exists(default_target_to_build) > file_doesnt_exist) */) {
2995 if (posix) {
2996 if (!commands_done) {
2997 (void) printf(gettext("`%s' is updated.\n"),
2998 default_target_to_build->string_mb);
2999 } else {
3000 if (no_action_was_taken) {
3001 (void) printf(gettext("`%s': no action was taken.\n"),
3002 default_target_to_build->string_mb);
3005 } else {
3006 if (!commands_done &&
3007 (exists(default_target_to_build) > file_doesnt_exist)) {
3008 (void) printf(gettext("`%s' is up to date.\n"),
3009 default_target_to_build->string_mb);
3017 * report_recursion(target)
3019 * If this is a recursive make and the parent make has KEEP_STATE on
3020 * this routine reports the dependency to the parent make
3022 * Parameters:
3023 * target Target to report
3025 * Global variables used:
3026 * makefiles_used List of makefiles read
3027 * recursive_name The Name ".RECURSIVE", printed
3028 * report_dependency dwight
3030 static void
3031 report_recursion(register Name target)
3033 register FILE *report_file = get_report_file();
3035 if ((report_file == NULL) || (report_file == (FILE*)-1)) {
3036 return;
3038 if (primary_makefile == NULL) {
3040 * This can happen when there is no makefile and
3041 * only implicit rules are being used.
3043 return;
3045 (void) fprintf(report_file,
3046 "%s: %s ",
3047 get_target_being_reported_for(),
3048 recursive_name->string_mb);
3049 report_dependency(get_current_path());
3050 report_dependency(target->string_mb);
3051 report_dependency(primary_makefile->string_mb);
3052 (void) fprintf(report_file, "\n");
3055 /* Next function "append_or_replace_macro_in_dyn_array" must be in "misc.cc". */
3056 /* NIKMOL */
3057 extern void
3058 append_or_replace_macro_in_dyn_array(ASCII_Dyn_Array *Ar, char *macro)
3060 register char *cp0; /* work pointer in macro */
3061 register char *cp1; /* work pointer in array */
3062 register char *cp2; /* work pointer in array */
3063 register char *cp3; /* work pointer in array */
3064 register char *name; /* macro name */
3065 register char *value; /* macro value */
3066 register int len_array;
3067 register int len_macro;
3069 char * esc_value = NULL;
3070 int esc_len;
3072 if (!(len_macro = strlen(macro))) return;
3073 name = macro;
3074 while (isspace(*(name))) {
3075 name++;
3077 if (!(value = strchr(name, (int) equal_char))) {
3078 /* no '=' in macro */
3079 goto ERROR_MACRO;
3081 cp0 = value;
3082 value++;
3083 while (isspace(*(value))) {
3084 value++;
3086 while (isspace(*(cp0-1))) {
3087 cp0--;
3089 if (cp0 <= name) goto ERROR_MACRO; /* no name */
3090 if (!(Ar->size)) goto ALLOC_ARRAY;
3091 cp1 = Ar->start;
3093 LOOK_FOR_NAME:
3094 if (!(cp1 = strchr(cp1, name[0]))) goto APPEND_MACRO;
3095 if (!(cp2 = strchr(cp1, (int) equal_char))) goto APPEND_MACRO;
3096 if (strncmp(cp1, name, (size_t)(cp0-name))) {
3097 /* another name */
3098 cp1++;
3099 goto LOOK_FOR_NAME;
3101 if (cp1 != Ar->start) {
3102 if (!isspace(*(cp1-1))) {
3103 /* another name */
3104 cp1++;
3105 goto LOOK_FOR_NAME;
3108 for (cp3 = cp1 + (cp0-name); cp3 < cp2; cp3++) {
3109 if (isspace(*cp3)) continue;
3110 /* else: another name */
3111 cp1++;
3112 goto LOOK_FOR_NAME;
3114 /* Look for the next macro name in array */
3115 cp3 = cp2+1;
3116 if (*cp3 != (int) doublequote_char) {
3117 /* internal error */
3118 goto ERROR_MACRO;
3120 if (!(cp3 = strchr(cp3+1, (int) doublequote_char))) {
3121 /* internal error */
3122 goto ERROR_MACRO;
3124 cp3++;
3125 while (isspace(*cp3)) {
3126 cp3++;
3129 cp2 = cp1; /* remove old macro */
3130 if ((*cp3) && (cp3 < Ar->start + Ar->size)) {
3131 for (; cp3 < Ar->start + Ar->size; cp3++) {
3132 *cp2++ = *cp3;
3135 for (; cp2 < Ar->start + Ar->size; cp2++) {
3136 *cp2 = 0;
3138 if (*cp1) {
3139 /* check next name */
3140 goto LOOK_FOR_NAME;
3142 goto APPEND_MACRO;
3144 ALLOC_ARRAY:
3145 if (Ar->size) {
3146 cp1 = Ar->start;
3147 } else {
3148 cp1 = 0;
3150 Ar->size += 128;
3151 Ar->start = getmem(Ar->size);
3152 for (len_array=0; len_array < Ar->size; len_array++) {
3153 Ar->start[len_array] = 0;
3155 if (cp1) {
3156 strcpy(Ar->start, cp1);
3157 retmem((wchar_t *) cp1);
3160 APPEND_MACRO:
3161 len_array = strlen(Ar->start);
3162 esc_value = (char*)malloc(strlen(value)*2 + 1);
3163 quote_str(value, esc_value);
3164 esc_len = strlen(esc_value) - strlen(value);
3165 if (len_array + len_macro + esc_len + 5 >= Ar->size) goto ALLOC_ARRAY;
3166 strcat(Ar->start, " ");
3167 strncat(Ar->start, name, cp0-name);
3168 strcat(Ar->start, "=");
3169 strncat(Ar->start, esc_value, strlen(esc_value));
3170 free(esc_value);
3171 return;
3172 ERROR_MACRO:
3173 /* Macro without '=' or with invalid left/right part */
3174 return;
3177 static void
3178 report_dir_enter_leave(Boolean entering)
3180 char rcwd[MAXPATHLEN];
3181 static char * mlev = NULL;
3182 char * make_level_str = NULL;
3183 int make_level_val = 0;
3185 make_level_str = getenv("MAKELEVEL");
3186 if(make_level_str) {
3187 make_level_val = atoi(make_level_str);
3189 if(mlev == NULL) {
3190 mlev = (char*) malloc(MAXPATHLEN);
3192 if(entering) {
3193 sprintf(mlev, "MAKELEVEL=%d", make_level_val + 1);
3194 } else {
3195 make_level_val--;
3196 sprintf(mlev, "MAKELEVEL=%d", make_level_val);
3198 putenv(mlev);
3200 if(report_cwd) {
3201 if(make_level_val <= 0) {
3202 if(entering) {
3203 sprintf(rcwd,
3204 gettext("%s: Entering directory `%s'\n"),
3205 getprogname(),
3206 get_current_path());
3207 } else {
3208 sprintf(rcwd,
3209 gettext("%s: Leaving directory `%s'\n"),
3210 getprogname(),
3211 get_current_path());
3213 } else {
3214 if(entering) {
3215 sprintf(rcwd,
3216 gettext("%s[%d]: Entering directory `%s'\n"),
3217 getprogname(),
3218 make_level_val, get_current_path());
3219 } else {
3220 sprintf(rcwd,
3221 gettext("%s[%d]: Leaving directory `%s'\n"),
3222 getprogname(),
3223 make_level_val, get_current_path());
3226 printf("%s", rcwd);