1 | /*
|
---|
2 | * Copyright (c) 1988, 1989, 1990, 1993
|
---|
3 | * The Regents of the University of California. All rights reserved.
|
---|
4 | * Copyright (c) 1989 by Berkeley Softworks
|
---|
5 | * All rights reserved.
|
---|
6 | *
|
---|
7 | * This code is derived from software contributed to Berkeley by
|
---|
8 | * Adam de Boor.
|
---|
9 | *
|
---|
10 | * Redistribution and use in source and binary forms, with or without
|
---|
11 | * modification, are permitted provided that the following conditions
|
---|
12 | * are met:
|
---|
13 | * 1. Redistributions of source code must retain the above copyright
|
---|
14 | * notice, this list of conditions and the following disclaimer.
|
---|
15 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
16 | * notice, this list of conditions and the following disclaimer in the
|
---|
17 | * documentation and/or other materials provided with the distribution.
|
---|
18 | * 3. All advertising materials mentioning features or use of this software
|
---|
19 | * must display the following acknowledgement:
|
---|
20 | * This product includes software developed by the University of
|
---|
21 | * California, Berkeley and its contributors.
|
---|
22 | * 4. Neither the name of the University nor the names of its contributors
|
---|
23 | * may be used to endorse or promote products derived from this software
|
---|
24 | * without specific prior written permission.
|
---|
25 | *
|
---|
26 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
---|
27 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
28 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
29 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
---|
30 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
31 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
32 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
33 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
34 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
35 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
36 | * SUCH DAMAGE.
|
---|
37 | *
|
---|
38 | * @(#)main.c 8.3 (Berkeley) 3/19/94
|
---|
39 | */
|
---|
40 |
|
---|
41 | #ifndef lint
|
---|
42 | #if 0
|
---|
43 | static char copyright[] =
|
---|
44 | "@(#) Copyright (c) 1988, 1989, 1990, 1993\n\
|
---|
45 | The Regents of the University of California. All rights reserved.\n";
|
---|
46 | #endif
|
---|
47 | #endif /* not lint */
|
---|
48 | #include <sys/cdefs.h>
|
---|
49 | __FBSDID("$FreeBSD: src/usr.bin/make/main.c,v 1.79 2002/10/10 19:27:48 jmallett Exp $");
|
---|
50 |
|
---|
51 | /*-
|
---|
52 | * main.c --
|
---|
53 | * The main file for this entire program. Exit routines etc
|
---|
54 | * reside here.
|
---|
55 | *
|
---|
56 | * Utility functions defined in this file:
|
---|
57 | * Main_ParseArgLine Takes a line of arguments, breaks them and
|
---|
58 | * treats them as if they were given when first
|
---|
59 | * invoked. Used by the parse module to implement
|
---|
60 | * the .MFLAGS target.
|
---|
61 | */
|
---|
62 |
|
---|
63 | #include <sys/types.h>
|
---|
64 | #include <sys/time.h>
|
---|
65 | #include <sys/param.h>
|
---|
66 | #include <sys/resource.h>
|
---|
67 | #include <sys/signal.h>
|
---|
68 | #include <sys/stat.h>
|
---|
69 | #if defined(__i386__)
|
---|
70 | #include <sys/sysctl.h>
|
---|
71 | #endif
|
---|
72 | #ifndef MACHINE
|
---|
73 | #include <sys/utsname.h>
|
---|
74 | #endif
|
---|
75 | #include <sys/wait.h>
|
---|
76 | #include <err.h>
|
---|
77 | #include <stdlib.h>
|
---|
78 | #include <errno.h>
|
---|
79 | #include <fcntl.h>
|
---|
80 | #include <stdio.h>
|
---|
81 | #include <sysexits.h>
|
---|
82 | #include <stdarg.h>
|
---|
83 | #include <unistd.h>
|
---|
84 | #include "make.h"
|
---|
85 | #include "hash.h"
|
---|
86 | #include "dir.h"
|
---|
87 | #include "job.h"
|
---|
88 | #include "pathnames.h"
|
---|
89 |
|
---|
90 | #define WANT_ENV_MKLVL 1
|
---|
91 |
|
---|
92 | #ifndef DEFMAXLOCAL
|
---|
93 | #define DEFMAXLOCAL DEFMAXJOBS
|
---|
94 | #endif /* DEFMAXLOCAL */
|
---|
95 |
|
---|
96 | #define MAKEFLAGS ".MAKEFLAGS"
|
---|
97 |
|
---|
98 | Lst create; /* Targets to be made */
|
---|
99 | time_t now; /* Time at start of make */
|
---|
100 | GNode *DEFAULT; /* .DEFAULT node */
|
---|
101 | Boolean allPrecious; /* .PRECIOUS given on line by itself */
|
---|
102 |
|
---|
103 | static Boolean noBuiltins; /* -r flag */
|
---|
104 | static Lst makefiles; /* ordered list of makefiles to read */
|
---|
105 | static Boolean expandVars; /* fully expand printed variables */
|
---|
106 | static Lst variables; /* list of variables to print */
|
---|
107 | int maxJobs; /* -j argument */
|
---|
108 | static Boolean forceJobs; /* -j argument given */
|
---|
109 | static int maxLocal; /* -L argument */
|
---|
110 | Boolean compatMake; /* -B argument */
|
---|
111 | Boolean debug; /* -d flag */
|
---|
112 | Boolean noExecute; /* -n flag */
|
---|
113 | Boolean keepgoing; /* -k flag */
|
---|
114 | Boolean queryFlag; /* -q flag */
|
---|
115 | Boolean touchFlag; /* -t flag */
|
---|
116 | Boolean usePipes; /* !-P flag */
|
---|
117 | Boolean ignoreErrors; /* -i flag */
|
---|
118 | Boolean beSilent; /* -s flag */
|
---|
119 | Boolean beVerbose; /* -v flag */
|
---|
120 | Boolean oldVars; /* variable substitution style */
|
---|
121 | Boolean checkEnvFirst; /* -e flag */
|
---|
122 | Lst envFirstVars; /* (-E) vars to override from env */
|
---|
123 | Boolean jobsRunning; /* TRUE if the jobs might be running */
|
---|
124 |
|
---|
125 | static void MainParseArgs(int, char **);
|
---|
126 | char * chdir_verify_path(char *, char *);
|
---|
127 | static int ReadMakefile(void *, void *);
|
---|
128 | static void usage(void);
|
---|
129 |
|
---|
130 | static char *curdir; /* startup directory */
|
---|
131 | static char *objdir; /* where we chdir'ed to */
|
---|
132 |
|
---|
133 | /*-
|
---|
134 | * MainParseArgs --
|
---|
135 | * Parse a given argument vector. Called from main() and from
|
---|
136 | * Main_ParseArgLine() when the .MAKEFLAGS target is used.
|
---|
137 | *
|
---|
138 | * XXX: Deal with command line overriding .MAKEFLAGS in makefile
|
---|
139 | *
|
---|
140 | * Results:
|
---|
141 | * None
|
---|
142 | *
|
---|
143 | * Side Effects:
|
---|
144 | * Various global and local flags will be set depending on the flags
|
---|
145 | * given
|
---|
146 | */
|
---|
147 | static void
|
---|
148 | MainParseArgs(int argc, char **argv)
|
---|
149 | {
|
---|
150 | char *p;
|
---|
151 | int c;
|
---|
152 |
|
---|
153 | optind = 1; /* since we're called more than once */
|
---|
154 | #ifdef REMOTE
|
---|
155 | # define OPTFLAGS "BC:D:E:I:L:PSV:Xd:ef:ij:km:nqrstv"
|
---|
156 | #else
|
---|
157 | # define OPTFLAGS "BC:D:E:I:PSV:Xd:ef:ij:km:nqrstv"
|
---|
158 | #endif
|
---|
159 | rearg: while((c = getopt(argc, argv, OPTFLAGS)) != -1) {
|
---|
160 | switch(c) {
|
---|
161 | case 'C':
|
---|
162 | chdir(optarg);
|
---|
163 | break;
|
---|
164 | case 'D':
|
---|
165 | Var_Set(optarg, "1", VAR_GLOBAL);
|
---|
166 | Var_Append(MAKEFLAGS, "-D", VAR_GLOBAL);
|
---|
167 | Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
|
---|
168 | break;
|
---|
169 | case 'I':
|
---|
170 | Parse_AddIncludeDir(optarg);
|
---|
171 | Var_Append(MAKEFLAGS, "-I", VAR_GLOBAL);
|
---|
172 | Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
|
---|
173 | break;
|
---|
174 | case 'V':
|
---|
175 | (void)Lst_AtEnd(variables, (void *)optarg);
|
---|
176 | Var_Append(MAKEFLAGS, "-V", VAR_GLOBAL);
|
---|
177 | Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
|
---|
178 | break;
|
---|
179 | case 'X':
|
---|
180 | expandVars = FALSE;
|
---|
181 | break;
|
---|
182 | case 'B':
|
---|
183 | compatMake = TRUE;
|
---|
184 | Var_Append(MAKEFLAGS, "-B", VAR_GLOBAL);
|
---|
185 | break;
|
---|
186 | #ifdef REMOTE
|
---|
187 | case 'L': {
|
---|
188 | char *endptr;
|
---|
189 |
|
---|
190 | maxLocal = strtol(optarg, &endptr, 10);
|
---|
191 | if (maxLocal < 0 || *endptr != '\0') {
|
---|
192 | warnx("illegal number, -L argument -- %s",
|
---|
193 | optarg);
|
---|
194 | usage();
|
---|
195 | }
|
---|
196 | Var_Append(MAKEFLAGS, "-L", VAR_GLOBAL);
|
---|
197 | Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
|
---|
198 | break;
|
---|
199 | }
|
---|
200 | #endif
|
---|
201 | case 'P':
|
---|
202 | usePipes = FALSE;
|
---|
203 | Var_Append(MAKEFLAGS, "-P", VAR_GLOBAL);
|
---|
204 | break;
|
---|
205 | case 'S':
|
---|
206 | keepgoing = FALSE;
|
---|
207 | Var_Append(MAKEFLAGS, "-S", VAR_GLOBAL);
|
---|
208 | break;
|
---|
209 | case 'd': {
|
---|
210 | char *modules = optarg;
|
---|
211 |
|
---|
212 | for (; *modules; ++modules)
|
---|
213 | switch (*modules) {
|
---|
214 | case 'A':
|
---|
215 | debug = ~0;
|
---|
216 | break;
|
---|
217 | case 'a':
|
---|
218 | debug |= DEBUG_ARCH;
|
---|
219 | break;
|
---|
220 | case 'c':
|
---|
221 | debug |= DEBUG_COND;
|
---|
222 | break;
|
---|
223 | case 'd':
|
---|
224 | debug |= DEBUG_DIR;
|
---|
225 | break;
|
---|
226 | case 'f':
|
---|
227 | debug |= DEBUG_FOR;
|
---|
228 | break;
|
---|
229 | case 'g':
|
---|
230 | if (modules[1] == '1') {
|
---|
231 | debug |= DEBUG_GRAPH1;
|
---|
232 | ++modules;
|
---|
233 | }
|
---|
234 | else if (modules[1] == '2') {
|
---|
235 | debug |= DEBUG_GRAPH2;
|
---|
236 | ++modules;
|
---|
237 | }
|
---|
238 | break;
|
---|
239 | case 'j':
|
---|
240 | debug |= DEBUG_JOB;
|
---|
241 | break;
|
---|
242 | case 'l':
|
---|
243 | debug |= DEBUG_LOUD;
|
---|
244 | break;
|
---|
245 | case 'm':
|
---|
246 | debug |= DEBUG_MAKE;
|
---|
247 | break;
|
---|
248 | case 's':
|
---|
249 | debug |= DEBUG_SUFF;
|
---|
250 | break;
|
---|
251 | case 't':
|
---|
252 | debug |= DEBUG_TARG;
|
---|
253 | break;
|
---|
254 | case 'v':
|
---|
255 | debug |= DEBUG_VAR;
|
---|
256 | break;
|
---|
257 | default:
|
---|
258 | warnx("illegal argument to d option -- %c", *modules);
|
---|
259 | usage();
|
---|
260 | }
|
---|
261 | Var_Append(MAKEFLAGS, "-d", VAR_GLOBAL);
|
---|
262 | Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
|
---|
263 | break;
|
---|
264 | }
|
---|
265 | case 'E':
|
---|
266 | p = emalloc(strlen(optarg) + 1);
|
---|
267 | (void)strcpy(p, optarg);
|
---|
268 | (void)Lst_AtEnd(envFirstVars, (void *)p);
|
---|
269 | Var_Append(MAKEFLAGS, "-E", VAR_GLOBAL);
|
---|
270 | Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
|
---|
271 | break;
|
---|
272 | case 'e':
|
---|
273 | checkEnvFirst = TRUE;
|
---|
274 | Var_Append(MAKEFLAGS, "-e", VAR_GLOBAL);
|
---|
275 | break;
|
---|
276 | case 'f':
|
---|
277 | (void)Lst_AtEnd(makefiles, (void *)optarg);
|
---|
278 | break;
|
---|
279 | case 'i':
|
---|
280 | ignoreErrors = TRUE;
|
---|
281 | Var_Append(MAKEFLAGS, "-i", VAR_GLOBAL);
|
---|
282 | break;
|
---|
283 | case 'j': {
|
---|
284 | char *endptr;
|
---|
285 |
|
---|
286 | forceJobs = TRUE;
|
---|
287 | maxJobs = strtol(optarg, &endptr, 10);
|
---|
288 | if (maxJobs <= 0 || *endptr != '\0') {
|
---|
289 | warnx("illegal number, -j argument -- %s",
|
---|
290 | optarg);
|
---|
291 | usage();
|
---|
292 | }
|
---|
293 | #ifndef REMOTE
|
---|
294 | maxLocal = maxJobs;
|
---|
295 | #endif
|
---|
296 | Var_Append(MAKEFLAGS, "-j", VAR_GLOBAL);
|
---|
297 | Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
|
---|
298 | break;
|
---|
299 | }
|
---|
300 | case 'k':
|
---|
301 | keepgoing = TRUE;
|
---|
302 | Var_Append(MAKEFLAGS, "-k", VAR_GLOBAL);
|
---|
303 | break;
|
---|
304 | case 'm':
|
---|
305 | Dir_AddDir(sysIncPath, optarg);
|
---|
306 | Var_Append(MAKEFLAGS, "-m", VAR_GLOBAL);
|
---|
307 | Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
|
---|
308 | break;
|
---|
309 | case 'n':
|
---|
310 | noExecute = TRUE;
|
---|
311 | Var_Append(MAKEFLAGS, "-n", VAR_GLOBAL);
|
---|
312 | break;
|
---|
313 | case 'q':
|
---|
314 | queryFlag = TRUE;
|
---|
315 | /* Kind of nonsensical, wot? */
|
---|
316 | Var_Append(MAKEFLAGS, "-q", VAR_GLOBAL);
|
---|
317 | break;
|
---|
318 | case 'r':
|
---|
319 | noBuiltins = TRUE;
|
---|
320 | Var_Append(MAKEFLAGS, "-r", VAR_GLOBAL);
|
---|
321 | break;
|
---|
322 | case 's':
|
---|
323 | beSilent = TRUE;
|
---|
324 | Var_Append(MAKEFLAGS, "-s", VAR_GLOBAL);
|
---|
325 | break;
|
---|
326 | case 't':
|
---|
327 | touchFlag = TRUE;
|
---|
328 | Var_Append(MAKEFLAGS, "-t", VAR_GLOBAL);
|
---|
329 | break;
|
---|
330 | case 'v':
|
---|
331 | beVerbose = TRUE;
|
---|
332 | Var_Append(MAKEFLAGS, "-v", VAR_GLOBAL);
|
---|
333 | break;
|
---|
334 | default:
|
---|
335 | case '?':
|
---|
336 | usage();
|
---|
337 | }
|
---|
338 | }
|
---|
339 |
|
---|
340 | oldVars = TRUE;
|
---|
341 |
|
---|
342 | /*
|
---|
343 | * See if the rest of the arguments are variable assignments and
|
---|
344 | * perform them if so. Else take them to be targets and stuff them
|
---|
345 | * on the end of the "create" list.
|
---|
346 | */
|
---|
347 | for (argv += optind, argc -= optind; *argv; ++argv, --argc)
|
---|
348 | if (Parse_IsVar(*argv))
|
---|
349 | Parse_DoVar(*argv, VAR_CMD);
|
---|
350 | else {
|
---|
351 | if (!**argv)
|
---|
352 | Punt("illegal (null) argument.");
|
---|
353 | if (**argv == '-') {
|
---|
354 | if ((*argv)[1])
|
---|
355 | optind = 0; /* -flag... */
|
---|
356 | else
|
---|
357 | optind = 1; /* - */
|
---|
358 | goto rearg;
|
---|
359 | }
|
---|
360 | (void)Lst_AtEnd(create, (void *)estrdup(*argv));
|
---|
361 | }
|
---|
362 | }
|
---|
363 |
|
---|
364 | /*-
|
---|
365 | * Main_ParseArgLine --
|
---|
366 | * Used by the parse module when a .MFLAGS or .MAKEFLAGS target
|
---|
367 | * is encountered and by main() when reading the .MAKEFLAGS envariable.
|
---|
368 | * Takes a line of arguments and breaks it into its
|
---|
369 | * component words and passes those words and the number of them to the
|
---|
370 | * MainParseArgs function.
|
---|
371 | * The line should have all its leading whitespace removed.
|
---|
372 | *
|
---|
373 | * Results:
|
---|
374 | * None
|
---|
375 | *
|
---|
376 | * Side Effects:
|
---|
377 | * Only those that come from the various arguments.
|
---|
378 | */
|
---|
379 | void
|
---|
380 | Main_ParseArgLine(char *line)
|
---|
381 | {
|
---|
382 | char **argv; /* Manufactured argument vector */
|
---|
383 | int argc; /* Number of arguments in argv */
|
---|
384 |
|
---|
385 | if (line == NULL)
|
---|
386 | return;
|
---|
387 | for (; *line == ' '; ++line)
|
---|
388 | continue;
|
---|
389 | if (!*line)
|
---|
390 | return;
|
---|
391 |
|
---|
392 | argv = brk_string(line, &argc, TRUE);
|
---|
393 | MainParseArgs(argc, argv);
|
---|
394 | }
|
---|
395 |
|
---|
396 | char *
|
---|
397 | chdir_verify_path(char *path, char *obpath)
|
---|
398 | {
|
---|
399 | struct stat sb;
|
---|
400 |
|
---|
401 | if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) {
|
---|
402 | if (chdir(path) == -1 || getcwd(obpath, MAXPATHLEN) == NULL) {
|
---|
403 | warn("warning: %s", path);
|
---|
404 | return 0;
|
---|
405 | }
|
---|
406 | return obpath;
|
---|
407 | }
|
---|
408 |
|
---|
409 | return 0;
|
---|
410 | }
|
---|
411 |
|
---|
412 |
|
---|
413 | /*-
|
---|
414 | * main --
|
---|
415 | * The main function, for obvious reasons. Initializes variables
|
---|
416 | * and a few modules, then parses the arguments give it in the
|
---|
417 | * environment and on the command line. Reads the system makefile
|
---|
418 | * followed by either Makefile, makefile or the file given by the
|
---|
419 | * -f argument. Sets the .MAKEFLAGS PMake variable based on all the
|
---|
420 | * flags it has received by then uses either the Make or the Compat
|
---|
421 | * module to create the initial list of targets.
|
---|
422 | *
|
---|
423 | * Results:
|
---|
424 | * If -q was given, exits -1 if anything was out-of-date. Else it exits
|
---|
425 | * 0.
|
---|
426 | *
|
---|
427 | * Side Effects:
|
---|
428 | * The program exits when done. Targets are created. etc. etc. etc.
|
---|
429 | */
|
---|
430 | int
|
---|
431 | main(int argc, char **argv)
|
---|
432 | {
|
---|
433 | Lst targs; /* target nodes to create -- passed to Make_Init */
|
---|
434 | Boolean outOfDate = TRUE; /* FALSE if all targets up to date */
|
---|
435 | struct stat sa;
|
---|
436 | char *p, *p1, *path, *pathp;
|
---|
437 | #ifdef WANT_ENV_MKLVL
|
---|
438 | #define MKLVL_MAXVAL 500
|
---|
439 | #define MKLVL_ENVVAR "__MKLVL__"
|
---|
440 | int iMkLvl = 0;
|
---|
441 | char *szMkLvl = getenv(MKLVL_ENVVAR);
|
---|
442 | #endif /* WANT_ENV_MKLVL */
|
---|
443 | char mdpath[MAXPATHLEN];
|
---|
444 | char obpath[MAXPATHLEN];
|
---|
445 | char cdpath[MAXPATHLEN];
|
---|
446 | char *machine = getenv("MACHINE");
|
---|
447 | char *machine_arch = getenv("MACHINE_ARCH");
|
---|
448 | char *machine_cpu = getenv("MACHINE_CPU");
|
---|
449 | Lst sysMkPath; /* Path of sys.mk */
|
---|
450 | char *cp = NULL, *start;
|
---|
451 | /* avoid faults on read-only strings */
|
---|
452 | static char syspath[] = _PATH_DEFSYSPATH;
|
---|
453 |
|
---|
454 | #ifdef WANT_ENV_MKLVL
|
---|
455 | if ((iMkLvl = szMkLvl ? atoi(szMkLvl) : 0) < 0) {
|
---|
456 | iMkLvl = 0;
|
---|
457 | }
|
---|
458 | if (iMkLvl++ > MKLVL_MAXVAL) {
|
---|
459 | errc(2, EAGAIN,
|
---|
460 | "Max recursion level (%d) exceeded.", MKLVL_MAXVAL);
|
---|
461 | }
|
---|
462 | bzero(szMkLvl = emalloc(32), 32);
|
---|
463 | sprintf(szMkLvl, "%d", iMkLvl);
|
---|
464 | setenv(MKLVL_ENVVAR, szMkLvl, 1);
|
---|
465 | #endif /* WANT_ENV_MKLVL */
|
---|
466 |
|
---|
467 | #if DEFSHELL == 2
|
---|
468 | /*
|
---|
469 | * Turn off ENV to make ksh happier.
|
---|
470 | */
|
---|
471 | unsetenv("ENV");
|
---|
472 | #endif
|
---|
473 |
|
---|
474 | #ifdef RLIMIT_NOFILE
|
---|
475 | /*
|
---|
476 | * get rid of resource limit on file descriptors
|
---|
477 | */
|
---|
478 | {
|
---|
479 | struct rlimit rl;
|
---|
480 | if (getrlimit(RLIMIT_NOFILE, &rl) != -1 &&
|
---|
481 | rl.rlim_cur != rl.rlim_max) {
|
---|
482 | rl.rlim_cur = rl.rlim_max;
|
---|
483 | (void) setrlimit(RLIMIT_NOFILE, &rl);
|
---|
484 | }
|
---|
485 | }
|
---|
486 | #endif
|
---|
487 | /*
|
---|
488 | * Find where we are...
|
---|
489 | * All this code is so that we know where we are when we start up
|
---|
490 | * on a different machine with pmake.
|
---|
491 | */
|
---|
492 | curdir = cdpath;
|
---|
493 | if (getcwd(curdir, MAXPATHLEN) == NULL)
|
---|
494 | err(2, NULL);
|
---|
495 |
|
---|
496 | if (stat(curdir, &sa) == -1)
|
---|
497 | err(2, "%s", curdir);
|
---|
498 |
|
---|
499 | #if defined(__i386__) && defined(__FreeBSD_version) && \
|
---|
500 | __FreeBSD_version > 300003
|
---|
501 | /*
|
---|
502 | * PC-98 kernel sets the `i386' string to the utsname.machine and
|
---|
503 | * it cannot be distinguished from IBM-PC by uname(3). Therefore,
|
---|
504 | * we check machine.ispc98 and adjust the machine variable before
|
---|
505 | * using usname(3) below.
|
---|
506 | * NOTE: machdep.ispc98 was defined on 1998/8/31. At that time,
|
---|
507 | * __FreeBSD_version was defined as 300003. So, this check can
|
---|
508 | * safely be done with any kernel with version > 300003.
|
---|
509 | */
|
---|
510 | if (!machine) {
|
---|
511 | int ispc98;
|
---|
512 | size_t len;
|
---|
513 |
|
---|
514 | len = sizeof(ispc98);
|
---|
515 | if (!sysctlbyname("machdep.ispc98", &ispc98, &len, NULL, 0)) {
|
---|
516 | if (ispc98)
|
---|
517 | machine = "pc98";
|
---|
518 | }
|
---|
519 | }
|
---|
520 | #endif
|
---|
521 |
|
---|
522 | /*
|
---|
523 | * Get the name of this type of MACHINE from utsname
|
---|
524 | * so we can share an executable for similar machines.
|
---|
525 | * (i.e. m68k: amiga hp300, mac68k, sun3, ...)
|
---|
526 | *
|
---|
527 | * Note that while MACHINE is decided at run-time,
|
---|
528 | * MACHINE_ARCH is always known at compile time.
|
---|
529 | */
|
---|
530 | if (!machine) {
|
---|
531 | #ifndef MACHINE
|
---|
532 | struct utsname utsname;
|
---|
533 |
|
---|
534 | if (uname(&utsname) == -1)
|
---|
535 | err(2, "uname");
|
---|
536 | machine = utsname.machine;
|
---|
537 | #else
|
---|
538 | machine = MACHINE;
|
---|
539 | #endif
|
---|
540 | }
|
---|
541 |
|
---|
542 | if (!machine_arch) {
|
---|
543 | #ifndef MACHINE_ARCH
|
---|
544 | machine_arch = "unknown";
|
---|
545 | #else
|
---|
546 | machine_arch = MACHINE_ARCH;
|
---|
547 | #endif
|
---|
548 | }
|
---|
549 |
|
---|
550 | /*
|
---|
551 | * Set machine_cpu to the minumum supported CPU revision based
|
---|
552 | * on the target architecture, if not already set.
|
---|
553 | */
|
---|
554 | if (!machine_cpu) {
|
---|
555 | if (!strcmp(machine_arch, "i386"))
|
---|
556 | machine_cpu = "i386";
|
---|
557 | else if (!strcmp(machine_arch, "alpha"))
|
---|
558 | machine_cpu = "ev4";
|
---|
559 | else
|
---|
560 | machine_cpu = "unknown";
|
---|
561 | }
|
---|
562 |
|
---|
563 | /*
|
---|
564 | * The object directory location is determined using the
|
---|
565 | * following order of preference:
|
---|
566 | *
|
---|
567 | * 1. MAKEOBJDIRPREFIX`cwd`
|
---|
568 | * 2. MAKEOBJDIR
|
---|
569 | * 3. _PATH_OBJDIR.${MACHINE}
|
---|
570 | * 4. _PATH_OBJDIR
|
---|
571 | * 5. _PATH_OBJDIRPREFIX`cwd`
|
---|
572 | *
|
---|
573 | * If one of the first two fails, use the current directory.
|
---|
574 | * If the remaining three all fail, use the current directory.
|
---|
575 | *
|
---|
576 | * Once things are initted,
|
---|
577 | * have to add the original directory to the search path,
|
---|
578 | * and modify the paths for the Makefiles apropriately. The
|
---|
579 | * current directory is also placed as a variable for make scripts.
|
---|
580 | */
|
---|
581 | if (!(pathp = getenv("MAKEOBJDIRPREFIX"))) {
|
---|
582 | if (!(path = getenv("MAKEOBJDIR"))) {
|
---|
583 | path = _PATH_OBJDIR;
|
---|
584 | pathp = _PATH_OBJDIRPREFIX;
|
---|
585 | (void) snprintf(mdpath, MAXPATHLEN, "%s.%s",
|
---|
586 | path, machine);
|
---|
587 | if (!(objdir = chdir_verify_path(mdpath, obpath)))
|
---|
588 | if (!(objdir=chdir_verify_path(path, obpath))) {
|
---|
589 | (void) snprintf(mdpath, MAXPATHLEN,
|
---|
590 | "%s%s", pathp, curdir);
|
---|
591 | if (!(objdir=chdir_verify_path(mdpath,
|
---|
592 | obpath)))
|
---|
593 | objdir = curdir;
|
---|
594 | }
|
---|
595 | }
|
---|
596 | else if (!(objdir = chdir_verify_path(path, obpath)))
|
---|
597 | objdir = curdir;
|
---|
598 | }
|
---|
599 | else {
|
---|
600 | (void) snprintf(mdpath, MAXPATHLEN, "%s%s", pathp, curdir);
|
---|
601 | if (!(objdir = chdir_verify_path(mdpath, obpath)))
|
---|
602 | objdir = curdir;
|
---|
603 | }
|
---|
604 |
|
---|
605 | create = Lst_Init(FALSE);
|
---|
606 | makefiles = Lst_Init(FALSE);
|
---|
607 | envFirstVars = Lst_Init(FALSE);
|
---|
608 | expandVars = TRUE;
|
---|
609 | variables = Lst_Init(FALSE);
|
---|
610 | beSilent = FALSE; /* Print commands as executed */
|
---|
611 | ignoreErrors = FALSE; /* Pay attention to non-zero returns */
|
---|
612 | noExecute = FALSE; /* Execute all commands */
|
---|
613 | keepgoing = FALSE; /* Stop on error */
|
---|
614 | allPrecious = FALSE; /* Remove targets when interrupted */
|
---|
615 | queryFlag = FALSE; /* This is not just a check-run */
|
---|
616 | noBuiltins = FALSE; /* Read the built-in rules */
|
---|
617 | touchFlag = FALSE; /* Actually update targets */
|
---|
618 | usePipes = TRUE; /* Catch child output in pipes */
|
---|
619 | debug = 0; /* No debug verbosity, please. */
|
---|
620 | jobsRunning = FALSE;
|
---|
621 |
|
---|
622 | maxLocal = DEFMAXLOCAL; /* Set default local max concurrency */
|
---|
623 | #ifdef REMOTE
|
---|
624 | maxJobs = DEFMAXJOBS; /* Set default max concurrency */
|
---|
625 | #else
|
---|
626 | maxJobs = maxLocal;
|
---|
627 | #endif
|
---|
628 | forceJobs = FALSE; /* No -j flag */
|
---|
629 | compatMake = FALSE; /* No compat mode */
|
---|
630 |
|
---|
631 |
|
---|
632 | /*
|
---|
633 | * Initialize the parsing, directory and variable modules to prepare
|
---|
634 | * for the reading of inclusion paths and variable settings on the
|
---|
635 | * command line
|
---|
636 | */
|
---|
637 | Dir_Init(); /* Initialize directory structures so -I flags
|
---|
638 | * can be processed correctly */
|
---|
639 | Parse_Init(); /* Need to initialize the paths of #include
|
---|
640 | * directories */
|
---|
641 | Var_Init(); /* As well as the lists of variables for
|
---|
642 | * parsing arguments */
|
---|
643 | str_init();
|
---|
644 | if (objdir != curdir)
|
---|
645 | Dir_AddDir(dirSearchPath, curdir);
|
---|
646 | Var_Set(".CURDIR", curdir, VAR_GLOBAL);
|
---|
647 | Var_Set(".OBJDIR", objdir, VAR_GLOBAL);
|
---|
648 |
|
---|
649 | /*
|
---|
650 | * Initialize various variables.
|
---|
651 | * MAKE also gets this name, for compatibility
|
---|
652 | * .MAKEFLAGS gets set to the empty string just in case.
|
---|
653 | * MFLAGS also gets initialized empty, for compatibility.
|
---|
654 | */
|
---|
655 | Var_Set("MAKE", argv[0], VAR_GLOBAL);
|
---|
656 | Var_Set(MAKEFLAGS, "", VAR_GLOBAL);
|
---|
657 | Var_Set("MFLAGS", "", VAR_GLOBAL);
|
---|
658 | Var_Set("MACHINE", machine, VAR_GLOBAL);
|
---|
659 | Var_Set("MACHINE_ARCH", machine_arch, VAR_GLOBAL);
|
---|
660 | Var_Set("MACHINE_CPU", machine_cpu, VAR_GLOBAL);
|
---|
661 | #ifdef MAKE_VERSION
|
---|
662 | Var_Set("MAKE_VERSION", MAKE_VERSION, VAR_GLOBAL);
|
---|
663 | #endif
|
---|
664 |
|
---|
665 | /*
|
---|
666 | * First snag any flags out of the MAKE environment variable.
|
---|
667 | * (Note this is *not* MAKEFLAGS since /bin/make uses that and it's
|
---|
668 | * in a different format).
|
---|
669 | */
|
---|
670 | #ifdef POSIX
|
---|
671 | Main_ParseArgLine(getenv("MAKEFLAGS"));
|
---|
672 | #else
|
---|
673 | Main_ParseArgLine(getenv("MAKE"));
|
---|
674 | #endif
|
---|
675 |
|
---|
676 | MainParseArgs(argc, argv);
|
---|
677 |
|
---|
678 | /*
|
---|
679 | * Be compatible if user did not specify -j and did not explicitly
|
---|
680 | * turned compatibility on
|
---|
681 | */
|
---|
682 | if (!compatMake && !forceJobs)
|
---|
683 | compatMake = TRUE;
|
---|
684 |
|
---|
685 | /*
|
---|
686 | * Initialize archive, target and suffix modules in preparation for
|
---|
687 | * parsing the makefile(s)
|
---|
688 | */
|
---|
689 | Arch_Init();
|
---|
690 | Targ_Init();
|
---|
691 | Suff_Init();
|
---|
692 |
|
---|
693 | DEFAULT = NULL;
|
---|
694 | (void)time(&now);
|
---|
695 |
|
---|
696 | /*
|
---|
697 | * Set up the .TARGETS variable to contain the list of targets to be
|
---|
698 | * created. If none specified, make the variable empty -- the parser
|
---|
699 | * will fill the thing in with the default or .MAIN target.
|
---|
700 | */
|
---|
701 | if (!Lst_IsEmpty(create)) {
|
---|
702 | LstNode ln;
|
---|
703 |
|
---|
704 | for (ln = Lst_First(create); ln != NULL;
|
---|
705 | ln = Lst_Succ(ln)) {
|
---|
706 | char *name = (char *)Lst_Datum(ln);
|
---|
707 |
|
---|
708 | Var_Append(".TARGETS", name, VAR_GLOBAL);
|
---|
709 | }
|
---|
710 | } else
|
---|
711 | Var_Set(".TARGETS", "", VAR_GLOBAL);
|
---|
712 |
|
---|
713 |
|
---|
714 | /*
|
---|
715 | * If no user-supplied system path was given (through the -m option)
|
---|
716 | * add the directories from the DEFSYSPATH (more than one may be given
|
---|
717 | * as dir1:...:dirn) to the system include path.
|
---|
718 | */
|
---|
719 | if (Lst_IsEmpty(sysIncPath)) {
|
---|
720 | for (start = syspath; *start != '\0'; start = cp) {
|
---|
721 | for (cp = start; *cp != '\0' && *cp != ':'; cp++)
|
---|
722 | continue;
|
---|
723 | if (*cp == '\0') {
|
---|
724 | Dir_AddDir(sysIncPath, start);
|
---|
725 | } else {
|
---|
726 | *cp++ = '\0';
|
---|
727 | Dir_AddDir(sysIncPath, start);
|
---|
728 | }
|
---|
729 | }
|
---|
730 | }
|
---|
731 |
|
---|
732 | /*
|
---|
733 | * Read in the built-in rules first, followed by the specified
|
---|
734 | * makefile, if it was (makefile != (char *) NULL), or the default
|
---|
735 | * Makefile and makefile, in that order, if it wasn't.
|
---|
736 | */
|
---|
737 | if (!noBuiltins) {
|
---|
738 | LstNode ln;
|
---|
739 |
|
---|
740 | sysMkPath = Lst_Init (FALSE);
|
---|
741 | Dir_Expand (_PATH_DEFSYSMK, sysIncPath, sysMkPath);
|
---|
742 | if (Lst_IsEmpty(sysMkPath))
|
---|
743 | Fatal("make: no system rules (%s).", _PATH_DEFSYSMK);
|
---|
744 | ln = Lst_Find(sysMkPath, (void *)NULL, ReadMakefile);
|
---|
745 | if (ln != NULL)
|
---|
746 | Fatal("make: cannot open %s.", (char *)Lst_Datum(ln));
|
---|
747 | }
|
---|
748 |
|
---|
749 | if (!Lst_IsEmpty(makefiles)) {
|
---|
750 | LstNode ln;
|
---|
751 |
|
---|
752 | ln = Lst_Find(makefiles, (void *)NULL, ReadMakefile);
|
---|
753 | if (ln != NULL)
|
---|
754 | Fatal("make: cannot open %s.", (char *)Lst_Datum(ln));
|
---|
755 | } else if (!ReadMakefile("BSDmakefile", NULL))
|
---|
756 | if (!ReadMakefile("makefile", NULL))
|
---|
757 | (void)ReadMakefile("Makefile", NULL);
|
---|
758 |
|
---|
759 | (void)ReadMakefile(".depend", NULL);
|
---|
760 |
|
---|
761 | Var_Append("MFLAGS", Var_Value(MAKEFLAGS, VAR_GLOBAL, &p1), VAR_GLOBAL);
|
---|
762 | efree(p1);
|
---|
763 |
|
---|
764 | /* Install all the flags into the MAKE envariable. */
|
---|
765 | if (((p = Var_Value(MAKEFLAGS, VAR_GLOBAL, &p1)) != NULL) && *p)
|
---|
766 | #ifdef POSIX
|
---|
767 | setenv("MAKEFLAGS", p, 1);
|
---|
768 | #else
|
---|
769 | setenv("MAKE", p, 1);
|
---|
770 | #endif
|
---|
771 | efree(p1);
|
---|
772 |
|
---|
773 | /*
|
---|
774 | * For compatibility, look at the directories in the VPATH variable
|
---|
775 | * and add them to the search path, if the variable is defined. The
|
---|
776 | * variable's value is in the same format as the PATH envariable, i.e.
|
---|
777 | * <directory>:<directory>:<directory>...
|
---|
778 | */
|
---|
779 | if (Var_Exists("VPATH", VAR_CMD)) {
|
---|
780 | char *vpath, savec;
|
---|
781 | /*
|
---|
782 | * GCC stores string constants in read-only memory, but
|
---|
783 | * Var_Subst will want to write this thing, so store it
|
---|
784 | * in an array
|
---|
785 | */
|
---|
786 | static char VPATH[] = "${VPATH}";
|
---|
787 |
|
---|
788 | vpath = Var_Subst(NULL, VPATH, VAR_CMD, FALSE);
|
---|
789 | path = vpath;
|
---|
790 | do {
|
---|
791 | /* skip to end of directory */
|
---|
792 | for (cp = path; *cp != ':' && *cp != '\0'; cp++)
|
---|
793 | continue;
|
---|
794 | /* Save terminator character so know when to stop */
|
---|
795 | savec = *cp;
|
---|
796 | *cp = '\0';
|
---|
797 | /* Add directory to search path */
|
---|
798 | Dir_AddDir(dirSearchPath, path);
|
---|
799 | *cp = savec;
|
---|
800 | path = cp + 1;
|
---|
801 | } while (savec == ':');
|
---|
802 | (void)free(vpath);
|
---|
803 | }
|
---|
804 |
|
---|
805 | /*
|
---|
806 | * Now that all search paths have been read for suffixes et al, it's
|
---|
807 | * time to add the default search path to their lists...
|
---|
808 | */
|
---|
809 | Suff_DoPaths();
|
---|
810 |
|
---|
811 | /* print the initial graph, if the user requested it */
|
---|
812 | if (DEBUG(GRAPH1))
|
---|
813 | Targ_PrintGraph(1);
|
---|
814 |
|
---|
815 | /* print the values of any variables requested by the user */
|
---|
816 | if (!Lst_IsEmpty(variables)) {
|
---|
817 | LstNode ln;
|
---|
818 |
|
---|
819 | for (ln = Lst_First(variables); ln != NULL;
|
---|
820 | ln = Lst_Succ(ln)) {
|
---|
821 | char *value;
|
---|
822 | if (expandVars) {
|
---|
823 | p1 = emalloc(strlen((char *)Lst_Datum(ln)) + 1 + 3);
|
---|
824 | /* This sprintf is safe, because of the malloc above */
|
---|
825 | (void)sprintf(p1, "${%s}", (char *)Lst_Datum(ln));
|
---|
826 | value = Var_Subst(NULL, p1, VAR_GLOBAL, FALSE);
|
---|
827 | } else {
|
---|
828 | value = Var_Value((char *)Lst_Datum(ln),
|
---|
829 | VAR_GLOBAL, &p1);
|
---|
830 | }
|
---|
831 | printf("%s\n", value ? value : "");
|
---|
832 | if (p1)
|
---|
833 | free(p1);
|
---|
834 | }
|
---|
835 | } else {
|
---|
836 |
|
---|
837 | /*
|
---|
838 | * Have now read the entire graph and need to make a list of targets
|
---|
839 | * to create. If none was given on the command line, we consult the
|
---|
840 | * parsing module to find the main target(s) to create.
|
---|
841 | */
|
---|
842 | if (Lst_IsEmpty(create))
|
---|
843 | targs = Parse_MainName();
|
---|
844 | else
|
---|
845 | targs = Targ_FindList(create, TARG_CREATE);
|
---|
846 |
|
---|
847 | if (!compatMake) {
|
---|
848 | /*
|
---|
849 | * Initialize job module before traversing the graph, now that
|
---|
850 | * any .BEGIN and .END targets have been read. This is done
|
---|
851 | * only if the -q flag wasn't given (to prevent the .BEGIN from
|
---|
852 | * being executed should it exist).
|
---|
853 | */
|
---|
854 | if (!queryFlag) {
|
---|
855 | if (maxLocal == -1)
|
---|
856 | maxLocal = maxJobs;
|
---|
857 | Job_Init(maxJobs, maxLocal);
|
---|
858 | jobsRunning = TRUE;
|
---|
859 | }
|
---|
860 |
|
---|
861 | /* Traverse the graph, checking on all the targets */
|
---|
862 | outOfDate = Make_Run(targs);
|
---|
863 | } else {
|
---|
864 | /*
|
---|
865 | * Compat_Init will take care of creating all the targets as
|
---|
866 | * well as initializing the module.
|
---|
867 | */
|
---|
868 | Compat_Run(targs);
|
---|
869 | }
|
---|
870 | Lst_Destroy(targs, NOFREE);
|
---|
871 | }
|
---|
872 |
|
---|
873 | Lst_Destroy(variables, NOFREE);
|
---|
874 | Lst_Destroy(makefiles, NOFREE);
|
---|
875 | Lst_Destroy(create, (void (*)(void *)) free);
|
---|
876 |
|
---|
877 | /* print the graph now it's been processed if the user requested it */
|
---|
878 | if (DEBUG(GRAPH2))
|
---|
879 | Targ_PrintGraph(2);
|
---|
880 |
|
---|
881 | Suff_End();
|
---|
882 | Targ_End();
|
---|
883 | Arch_End();
|
---|
884 | str_end();
|
---|
885 | Var_End();
|
---|
886 | Parse_End();
|
---|
887 | Dir_End();
|
---|
888 |
|
---|
889 | if (queryFlag && outOfDate)
|
---|
890 | return(1);
|
---|
891 | else
|
---|
892 | return(0);
|
---|
893 | }
|
---|
894 |
|
---|
895 | /*-
|
---|
896 | * ReadMakefile --
|
---|
897 | * Open and parse the given makefile.
|
---|
898 | *
|
---|
899 | * Results:
|
---|
900 | * TRUE if ok. FALSE if couldn't open file.
|
---|
901 | *
|
---|
902 | * Side Effects:
|
---|
903 | * lots
|
---|
904 | */
|
---|
905 | static Boolean
|
---|
906 | ReadMakefile(void *p, void *q __unused)
|
---|
907 | {
|
---|
908 | char *fname; /* makefile to read */
|
---|
909 | FILE *stream;
|
---|
910 | char *name, path[MAXPATHLEN];
|
---|
911 | char *MAKEFILE;
|
---|
912 | int setMAKEFILE;
|
---|
913 |
|
---|
914 | fname = p;
|
---|
915 |
|
---|
916 | if (!strcmp(fname, "-")) {
|
---|
917 | Parse_File("(stdin)", stdin);
|
---|
918 | Var_Set("MAKEFILE", "", VAR_GLOBAL);
|
---|
919 | } else {
|
---|
920 | setMAKEFILE = strcmp(fname, ".depend");
|
---|
921 |
|
---|
922 | /* if we've chdir'd, rebuild the path name */
|
---|
923 | if (curdir != objdir && *fname != '/') {
|
---|
924 | (void)snprintf(path, MAXPATHLEN, "%s/%s", curdir, fname);
|
---|
925 | /*
|
---|
926 | * XXX The realpath stuff breaks relative includes
|
---|
927 | * XXX in some cases. The problem likely is in
|
---|
928 | * XXX parse.c where it does special things in
|
---|
929 | * XXX ParseDoInclude if the file is relateive
|
---|
930 | * XXX or absolute and not a system file. There
|
---|
931 | * XXX it assumes that if the current file that's
|
---|
932 | * XXX being included is absolute, that any files
|
---|
933 | * XXX that it includes shouldn't do the -I path
|
---|
934 | * XXX stuff, which is inconsistant with historical
|
---|
935 | * XXX behavior. However, I can't pentrate the mists
|
---|
936 | * XXX further, so I'm putting this workaround in
|
---|
937 | * XXX here until such time as the underlying bug
|
---|
938 | * XXX can be fixed.
|
---|
939 | */
|
---|
940 | #if THIS_BREAKS_THINGS
|
---|
941 | if (realpath(path, path) != NULL &&
|
---|
942 | (stream = fopen(path, "r")) != NULL) {
|
---|
943 | MAKEFILE = fname;
|
---|
944 | fname = path;
|
---|
945 | goto found;
|
---|
946 | }
|
---|
947 | } else if (realpath(fname, path) != NULL) {
|
---|
948 | MAKEFILE = fname;
|
---|
949 | fname = path;
|
---|
950 | if ((stream = fopen(fname, "r")) != NULL)
|
---|
951 | goto found;
|
---|
952 | }
|
---|
953 | #else
|
---|
954 | if ((stream = fopen(path, "r")) != NULL) {
|
---|
955 | MAKEFILE = fname;
|
---|
956 | fname = path;
|
---|
957 | goto found;
|
---|
958 | }
|
---|
959 | } else {
|
---|
960 | MAKEFILE = fname;
|
---|
961 | if ((stream = fopen(fname, "r")) != NULL)
|
---|
962 | goto found;
|
---|
963 | }
|
---|
964 | #endif
|
---|
965 | /* look in -I and system include directories. */
|
---|
966 | name = Dir_FindFile(fname, parseIncPath);
|
---|
967 | if (!name)
|
---|
968 | name = Dir_FindFile(fname, sysIncPath);
|
---|
969 | if (!name || !(stream = fopen(name, "r")))
|
---|
970 | return(FALSE);
|
---|
971 | MAKEFILE = fname = name;
|
---|
972 | /*
|
---|
973 | * set the MAKEFILE variable desired by System V fans -- the
|
---|
974 | * placement of the setting here means it gets set to the last
|
---|
975 | * makefile specified, as it is set by SysV make.
|
---|
976 | */
|
---|
977 | found:
|
---|
978 | if (setMAKEFILE)
|
---|
979 | Var_Set("MAKEFILE", MAKEFILE, VAR_GLOBAL);
|
---|
980 | Parse_File(fname, stream);
|
---|
981 | (void)fclose(stream);
|
---|
982 | }
|
---|
983 | return(TRUE);
|
---|
984 | }
|
---|
985 |
|
---|
986 | /*-
|
---|
987 | * Cmd_Exec --
|
---|
988 | * Execute the command in cmd, and return the output of that command
|
---|
989 | * in a string.
|
---|
990 | *
|
---|
991 | * Results:
|
---|
992 | * A string containing the output of the command, or the empty string
|
---|
993 | * If error is not NULL, it contains the reason for the command failure
|
---|
994 | *
|
---|
995 | * Side Effects:
|
---|
996 | * The string must be freed by the caller.
|
---|
997 | */
|
---|
998 | char *
|
---|
999 | Cmd_Exec(char *cmd, char **error)
|
---|
1000 | {
|
---|
1001 | char *args[4]; /* Args for invoking the shell */
|
---|
1002 | int fds[2]; /* Pipe streams */
|
---|
1003 | int cpid; /* Child PID */
|
---|
1004 | int pid; /* PID from wait() */
|
---|
1005 | char *res; /* result */
|
---|
1006 | int status; /* command exit status */
|
---|
1007 | Buffer buf; /* buffer to store the result */
|
---|
1008 | char *cp;
|
---|
1009 | int cc;
|
---|
1010 |
|
---|
1011 | *error = NULL;
|
---|
1012 |
|
---|
1013 | /*
|
---|
1014 | * Set up arguments for shell
|
---|
1015 | */
|
---|
1016 | args[0] = "sh";
|
---|
1017 | args[1] = "-c";
|
---|
1018 | args[2] = cmd;
|
---|
1019 | args[3] = NULL;
|
---|
1020 |
|
---|
1021 | /*
|
---|
1022 | * Open a pipe for fetching its output
|
---|
1023 | */
|
---|
1024 | if (pipe(fds) == -1) {
|
---|
1025 | *error = "Couldn't create pipe for \"%s\"";
|
---|
1026 | goto bad;
|
---|
1027 | }
|
---|
1028 |
|
---|
1029 | /*
|
---|
1030 | * Fork
|
---|
1031 | */
|
---|
1032 | switch (cpid = vfork()) {
|
---|
1033 | case 0:
|
---|
1034 | /*
|
---|
1035 | * Close input side of pipe
|
---|
1036 | */
|
---|
1037 | (void) close(fds[0]);
|
---|
1038 |
|
---|
1039 | /*
|
---|
1040 | * Duplicate the output stream to the shell's output, then
|
---|
1041 | * shut the extra thing down. Note we don't fetch the error
|
---|
1042 | * stream...why not? Why?
|
---|
1043 | */
|
---|
1044 | (void) dup2(fds[1], 1);
|
---|
1045 | (void) close(fds[1]);
|
---|
1046 |
|
---|
1047 | #if defined(DEFSHELL) && DEFSHELL == 0
|
---|
1048 | (void) execv("/bin/csh", args);
|
---|
1049 | #elif DEFSHELL == 1
|
---|
1050 | (void) execv("/bin/sh", args);
|
---|
1051 | #elif DEFSHELL == 2
|
---|
1052 | (void) execv("/bin/ksh", args);
|
---|
1053 | #else
|
---|
1054 | #error "DEFSHELL must be 1 or 2."
|
---|
1055 | #endif
|
---|
1056 | _exit(1);
|
---|
1057 | /*NOTREACHED*/
|
---|
1058 |
|
---|
1059 | case -1:
|
---|
1060 | *error = "Couldn't exec \"%s\"";
|
---|
1061 | goto bad;
|
---|
1062 |
|
---|
1063 | default:
|
---|
1064 | /*
|
---|
1065 | * No need for the writing half
|
---|
1066 | */
|
---|
1067 | (void) close(fds[1]);
|
---|
1068 |
|
---|
1069 | buf = Buf_Init (MAKE_BSIZE);
|
---|
1070 |
|
---|
1071 | do {
|
---|
1072 | char result[BUFSIZ];
|
---|
1073 | cc = read(fds[0], result, sizeof(result));
|
---|
1074 | if (cc > 0)
|
---|
1075 | Buf_AddBytes(buf, cc, (Byte *) result);
|
---|
1076 | }
|
---|
1077 | while (cc > 0 || (cc == -1 && errno == EINTR));
|
---|
1078 |
|
---|
1079 | /*
|
---|
1080 | * Close the input side of the pipe.
|
---|
1081 | */
|
---|
1082 | (void) close(fds[0]);
|
---|
1083 |
|
---|
1084 | /*
|
---|
1085 | * Wait for the process to exit.
|
---|
1086 | */
|
---|
1087 | while(((pid = wait(&status)) != cpid) && (pid >= 0))
|
---|
1088 | continue;
|
---|
1089 |
|
---|
1090 | if (cc == -1)
|
---|
1091 | *error = "Error reading shell's output for \"%s\"";
|
---|
1092 |
|
---|
1093 | res = (char *)Buf_GetAll (buf, &cc);
|
---|
1094 | Buf_Destroy (buf, FALSE);
|
---|
1095 |
|
---|
1096 | if (status)
|
---|
1097 | *error = "\"%s\" returned non-zero status";
|
---|
1098 |
|
---|
1099 | /*
|
---|
1100 | * Null-terminate the result, convert newlines to spaces and
|
---|
1101 | * install it in the variable.
|
---|
1102 | */
|
---|
1103 | res[cc] = '\0';
|
---|
1104 | cp = &res[cc] - 1;
|
---|
1105 |
|
---|
1106 | if (*cp == '\n') {
|
---|
1107 | /*
|
---|
1108 | * A final newline is just stripped
|
---|
1109 | */
|
---|
1110 | *cp-- = '\0';
|
---|
1111 | }
|
---|
1112 | while (cp >= res) {
|
---|
1113 | if (*cp == '\n') {
|
---|
1114 | *cp = ' ';
|
---|
1115 | }
|
---|
1116 | cp--;
|
---|
1117 | }
|
---|
1118 | break;
|
---|
1119 | }
|
---|
1120 | return res;
|
---|
1121 | bad:
|
---|
1122 | res = emalloc(1);
|
---|
1123 | *res = '\0';
|
---|
1124 | return res;
|
---|
1125 | }
|
---|
1126 |
|
---|
1127 | /*
|
---|
1128 | * usage --
|
---|
1129 | * exit with usage message
|
---|
1130 | */
|
---|
1131 | static void
|
---|
1132 | usage(void)
|
---|
1133 | {
|
---|
1134 | (void)fprintf(stderr, "%s\n%s\n%s\n",
|
---|
1135 | "usage: make [-Beiknqrstv] [-D variable] [-d flags] [-E variable] [-f makefile]",
|
---|
1136 | " [-I directory] [-j max_jobs] [-m directory] [-V variable]",
|
---|
1137 | " [variable=value] [target ...]");
|
---|
1138 | exit(2);
|
---|
1139 | }
|
---|