8446 uts: pci_check_bios() should check for BIOS
[unleashed.git] / bin / make / compat.c
blob0a0d9df04ac2ec298b72e375367aa0c2d6541381
1 /* $NetBSD: compat.c,v 1.106 2016/08/26 23:28:39 dholland Exp $ */
3 /*
4 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
5 * All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Adam de Boor.
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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
36 * Copyright (c) 1988, 1989 by Adam de Boor
37 * Copyright (c) 1989 by Berkeley Softworks
38 * All rights reserved.
40 * This code is derived from software contributed to Berkeley by
41 * Adam de Boor.
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
72 #ifndef MAKE_NATIVE
73 static char rcsid[] = "$NetBSD: compat.c,v 1.106 2016/08/26 23:28:39 dholland Exp $";
74 #else
75 #include <sys/cdefs.h>
76 #ifndef lint
77 #if 0
78 static char sccsid[] = "@(#)compat.c 8.2 (Berkeley) 3/19/94";
79 #else
80 __RCSID("$NetBSD: compat.c,v 1.106 2016/08/26 23:28:39 dholland Exp $");
81 #endif
82 #endif /* not lint */
83 #endif
85 /*-
86 * compat.c --
87 * The routines in this file implement the full-compatibility
88 * mode of PMake. Most of the special functionality of PMake
89 * is available in this mode. Things not supported:
90 * - different shells.
91 * - friendly variable substitution.
93 * Interface:
94 * Compat_Run Initialize things for this module and recreate
95 * thems as need creatin'
98 #ifdef HAVE_CONFIG_H
99 # include "config.h"
100 #endif
101 #include <sys/types.h>
102 #include <sys/stat.h>
103 #include "wait.h"
105 #include <ctype.h>
106 #include <errno.h>
107 #include <signal.h>
108 #include <stdio.h>
110 #include "make.h"
111 #include "hash.h"
112 #include "dir.h"
113 #include "job.h"
114 #include "metachar.h"
115 #include "pathnames.h"
118 static GNode *curTarg = NULL;
119 static GNode *ENDNode;
120 static void CompatInterrupt(int);
123 * CompatDeleteTarget -- delete a failed, interrupted, or otherwise
124 * duffed target if not inhibited by .PRECIOUS.
126 static void
127 CompatDeleteTarget(GNode *gn)
129 if ((gn != NULL) && !Targ_Precious (gn)) {
130 char *p1;
131 char *file = Var_Value(TARGET, gn, &p1);
133 if (!noExecute && eunlink(file) != -1) {
134 Error("*** %s removed", file);
137 free(p1);
142 *-----------------------------------------------------------------------
143 * CompatInterrupt --
144 * Interrupt the creation of the current target and remove it if
145 * it ain't precious.
147 * Results:
148 * None.
150 * Side Effects:
151 * The target is removed and the process exits. If .INTERRUPT exists,
152 * its commands are run first WITH INTERRUPTS IGNORED..
154 * XXX: is .PRECIOUS supposed to inhibit .INTERRUPT? I doubt it, but I've
155 * left the logic alone for now. - dholland 20160826
157 *-----------------------------------------------------------------------
159 static void
160 CompatInterrupt(int signo)
162 GNode *gn;
164 CompatDeleteTarget(curTarg);
166 if ((curTarg != NULL) && !Targ_Precious (curTarg)) {
168 * Run .INTERRUPT only if hit with interrupt signal
170 if (signo == SIGINT) {
171 gn = Targ_FindNode(".INTERRUPT", TARG_NOCREATE);
172 if (gn != NULL) {
173 Compat_Make(gn, gn);
177 if (signo == SIGQUIT)
178 _exit(signo);
179 bmake_signal(signo, SIG_DFL);
180 kill(myPid, signo);
184 *-----------------------------------------------------------------------
185 * CompatRunCommand --
186 * Execute the next command for a target. If the command returns an
187 * error, the node's made field is set to ERROR and creation stops.
189 * Input:
190 * cmdp Command to execute
191 * gnp Node from which the command came
193 * Results:
194 * 0 if the command succeeded, 1 if an error occurred.
196 * Side Effects:
197 * The node's 'made' field may be set to ERROR.
199 *-----------------------------------------------------------------------
202 CompatRunCommand(void *cmdp, void *gnp)
204 char *cmdStart; /* Start of expanded command */
205 char *cp, *bp;
206 Boolean silent, /* Don't print command */
207 doIt; /* Execute even if -n */
208 volatile Boolean errCheck; /* Check errors */
209 WAIT_T reason; /* Reason for child's death */
210 int status; /* Description of child's death */
211 pid_t cpid; /* Child actually found */
212 pid_t retstat; /* Result of wait */
213 LstNode cmdNode; /* Node where current command is located */
214 const char ** volatile av; /* Argument vector for thing to exec */
215 char ** volatile mav;/* Copy of the argument vector for freeing */
216 int argc; /* Number of arguments in av or 0 if not
217 * dynamically allocated */
218 Boolean local; /* TRUE if command should be executed
219 * locally */
220 Boolean useShell; /* TRUE if command should be executed
221 * using a shell */
222 char * volatile cmd = (char *)cmdp;
223 GNode *gn = (GNode *)gnp;
225 silent = gn->type & OP_SILENT;
226 errCheck = !(gn->type & OP_IGNORE);
227 doIt = FALSE;
229 cmdNode = Lst_Member(gn->commands, cmd);
230 cmdStart = Var_Subst(NULL, cmd, gn, VARF_WANTRES);
233 * brk_string will return an argv with a NULL in av[0], thus causing
234 * execvp to choke and die horribly. Besides, how can we execute a null
235 * command? In any case, we warn the user that the command expanded to
236 * nothing (is this the right thing to do?).
239 if (*cmdStart == '\0') {
240 free(cmdStart);
241 return(0);
243 cmd = cmdStart;
244 Lst_Replace(cmdNode, cmdStart);
246 if ((gn->type & OP_SAVE_CMDS) && (gn != ENDNode)) {
247 (void)Lst_AtEnd(ENDNode->commands, cmdStart);
248 return(0);
250 if (strcmp(cmdStart, "...") == 0) {
251 gn->type |= OP_SAVE_CMDS;
252 return(0);
255 while ((*cmd == '@') || (*cmd == '-') || (*cmd == '+')) {
256 switch (*cmd) {
257 case '@':
258 silent = DEBUG(LOUD) ? FALSE : TRUE;
259 break;
260 case '-':
261 errCheck = FALSE;
262 break;
263 case '+':
264 doIt = TRUE;
265 if (!shellName) /* we came here from jobs */
266 Shell_Init();
267 break;
269 cmd++;
272 while (isspace((unsigned char)*cmd))
273 cmd++;
276 * If we did not end up with a command, just skip it.
278 if (!*cmd)
279 return (0);
281 #if !defined(MAKE_NATIVE)
283 * In a non-native build, the host environment might be weird enough
284 * that it's necessary to go through a shell to get the correct
285 * behaviour. Or perhaps the shell has been replaced with something
286 * that does extra logging, and that should not be bypassed.
288 useShell = TRUE;
289 #else
291 * Search for meta characters in the command. If there are no meta
292 * characters, there's no need to execute a shell to execute the
293 * command.
295 * Additionally variable assignments and empty commands
296 * go to the shell. Therefore treat '=' and ':' like shell
297 * meta characters as documented in make(1).
300 useShell = needshell(cmd, FALSE);
301 #endif
304 * Print the command before echoing if we're not supposed to be quiet for
305 * this one. We also print the command if -n given.
307 if (!silent || NoExecute(gn)) {
308 printf("%s\n", cmd);
309 fflush(stdout);
313 * If we're not supposed to execute any commands, this is as far as
314 * we go...
316 if (!doIt && NoExecute(gn)) {
317 return (0);
319 if (DEBUG(JOB))
320 fprintf(debug_file, "Execute: '%s'\n", cmd);
322 again:
323 if (useShell) {
325 * We need to pass the command off to the shell, typically
326 * because the command contains a "meta" character.
328 static const char *shargv[5];
329 int shargc;
331 shargc = 0;
332 shargv[shargc++] = shellPath;
334 * The following work for any of the builtin shell specs.
336 if (errCheck && shellErrFlag) {
337 shargv[shargc++] = shellErrFlag;
339 if (DEBUG(SHELL))
340 shargv[shargc++] = "-xc";
341 else
342 shargv[shargc++] = "-c";
343 shargv[shargc++] = cmd;
344 shargv[shargc++] = NULL;
345 av = shargv;
346 argc = 0;
347 bp = NULL;
348 mav = NULL;
349 } else {
351 * No meta-characters, so no need to exec a shell. Break the command
352 * into words to form an argument vector we can execute.
354 mav = brk_string(cmd, &argc, TRUE, &bp);
355 if (mav == NULL) {
356 useShell = 1;
357 goto again;
359 av = (void *)mav;
362 local = TRUE;
364 #ifdef USE_META
365 if (useMeta) {
366 meta_compat_start();
368 #endif
371 * Fork and execute the single command. If the fork fails, we abort.
373 cpid = vFork();
374 if (cpid < 0) {
375 Fatal("Could not fork");
377 if (cpid == 0) {
378 Var_ExportVars();
379 #ifdef USE_META
380 if (useMeta) {
381 meta_compat_child();
383 #endif
384 if (local)
385 (void)execvp(av[0], (char *const *)UNCONST(av));
386 else
387 (void)execv(av[0], (char *const *)UNCONST(av));
388 execError("exec", av[0]);
389 _exit(1);
392 free(mav);
393 free(bp);
395 Lst_Replace(cmdNode, NULL);
397 #ifdef USE_META
398 if (useMeta) {
399 meta_compat_parent();
401 #endif
404 * The child is off and running. Now all we can do is wait...
406 while (1) {
408 while ((retstat = wait(&reason)) != cpid) {
409 if (retstat > 0)
410 JobReapChild(retstat, reason, FALSE); /* not ours? */
411 if (retstat == -1 && errno != EINTR) {
412 break;
416 if (retstat > -1) {
417 char *errfmt;
419 if (WIFSTOPPED(reason)) {
420 status = WSTOPSIG(reason); /* stopped */
421 errfmt = "*** Stopped %d%s\n";
422 } else if (WIFEXITED(reason)) {
423 status = WEXITSTATUS(reason); /* exited */
424 #if defined(USE_META) && defined(USE_FILEMON_ONCE)
425 if (useMeta) {
426 meta_cmd_finish(NULL);
428 #endif
429 if (status != 0) {
430 if (DEBUG(ERROR)) {
431 fprintf(debug_file, "\n*** Failed target: %s\n*** Failed command: ",
432 gn->name);
433 for (cp = cmd; *cp; ) {
434 if (isspace((unsigned char)*cp)) {
435 fprintf(debug_file, " ");
436 while (isspace((unsigned char)*cp))
437 cp++;
438 } else {
439 fprintf(debug_file, "%c", *cp);
440 cp++;
443 fprintf(debug_file, "\n");
445 errfmt = "*** Error code %d%s\n";
447 } else {
448 status = WTERMSIG(reason); /* signaled */
449 errfmt = "*** Signal %d%s\n";
453 if (!WIFEXITED(reason) || (status != 0)) {
454 if (errCheck) {
455 #ifdef USE_META
456 if (useMeta) {
457 meta_job_error(NULL, gn, 0, status);
459 #endif
460 gn->made = ERROR;
461 fprintf(stderr, errfmt, status, keepgoing ? " (continuing)" : "");
462 if (deleteOnError) {
463 CompatDeleteTarget(gn);
465 } else {
467 * Continue executing commands for this target.
468 * If we return 0, this will happen...
470 fprintf(stderr, errfmt, status, " (ignored)");
471 status = 0;
474 break;
475 } else {
476 Fatal("error in wait: %d: %s", retstat, strerror(errno));
477 /*NOTREACHED*/
480 free(cmdStart);
482 return (status);
486 *-----------------------------------------------------------------------
487 * Compat_Make --
488 * Make a target.
490 * Input:
491 * gnp The node to make
492 * pgnp Parent to abort if necessary
494 * Results:
497 * Side Effects:
498 * If an error is detected and not being ignored, the process exits.
500 *-----------------------------------------------------------------------
503 Compat_Make(void *gnp, void *pgnp)
505 GNode *gn = (GNode *)gnp;
506 GNode *pgn = (GNode *)pgnp;
508 if (!shellName) /* we came here from jobs */
509 Shell_Init();
510 if (gn->made == UNMADE && (gn == pgn || (pgn->type & OP_MADE) == 0)) {
512 * First mark ourselves to be made, then apply whatever transformations
513 * the suffix module thinks are necessary. Once that's done, we can
514 * descend and make all our children. If any of them has an error
515 * but the -k flag was given, our 'make' field will be set FALSE again.
516 * This is our signal to not attempt to do anything but abort our
517 * parent as well.
519 gn->flags |= REMAKE;
520 gn->made = BEINGMADE;
521 if ((gn->type & OP_MADE) == 0)
522 Suff_FindDeps(gn);
523 Lst_ForEach(gn->children, Compat_Make, gn);
524 if ((gn->flags & REMAKE) == 0) {
525 gn->made = ABORTED;
526 pgn->flags &= ~REMAKE;
527 goto cohorts;
530 if (Lst_Member(gn->iParents, pgn) != NULL) {
531 char *p1;
532 Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), pgn, 0);
533 free(p1);
537 * All the children were made ok. Now cmgn->mtime contains the
538 * modification time of the newest child, we need to find out if we
539 * exist and when we were modified last. The criteria for datedness
540 * are defined by the Make_OODate function.
542 if (DEBUG(MAKE)) {
543 fprintf(debug_file, "Examining %s...", gn->name);
545 if (! Make_OODate(gn)) {
546 gn->made = UPTODATE;
547 if (DEBUG(MAKE)) {
548 fprintf(debug_file, "up-to-date.\n");
550 goto cohorts;
551 } else if (DEBUG(MAKE)) {
552 fprintf(debug_file, "out-of-date.\n");
556 * If the user is just seeing if something is out-of-date, exit now
557 * to tell him/her "yes".
559 if (queryFlag) {
560 exit(1);
564 * We need to be re-made. We also have to make sure we've got a $?
565 * variable. To be nice, we also define the $> variable using
566 * Make_DoAllVar().
568 Make_DoAllVar(gn);
571 * Alter our type to tell if errors should be ignored or things
572 * should not be printed so CompatRunCommand knows what to do.
574 if (Targ_Ignore(gn)) {
575 gn->type |= OP_IGNORE;
577 if (Targ_Silent(gn)) {
578 gn->type |= OP_SILENT;
581 if (Job_CheckCommands(gn, Fatal)) {
583 * Our commands are ok, but we still have to worry about the -t
584 * flag...
586 if (!touchFlag || (gn->type & OP_MAKE)) {
587 curTarg = gn;
588 #ifdef USE_META
589 if (useMeta && !NoExecute(gn)) {
590 meta_job_start(NULL, gn);
592 #endif
593 Lst_ForEach(gn->commands, CompatRunCommand, gn);
594 curTarg = NULL;
595 } else {
596 Job_Touch(gn, gn->type & OP_SILENT);
598 } else {
599 gn->made = ERROR;
601 #ifdef USE_META
602 if (useMeta && !NoExecute(gn)) {
603 if (meta_job_finish(NULL) != 0)
604 gn->made = ERROR;
606 #endif
608 if (gn->made != ERROR) {
610 * If the node was made successfully, mark it so, update
611 * its modification time and timestamp all its parents. Note
612 * that for .ZEROTIME targets, the timestamping isn't done.
613 * This is to keep its state from affecting that of its parent.
615 gn->made = MADE;
616 pgn->flags |= Make_Recheck(gn) == 0 ? FORCE : 0;
617 if (!(gn->type & OP_EXEC)) {
618 pgn->flags |= CHILDMADE;
619 Make_TimeStamp(pgn, gn);
621 } else if (keepgoing) {
622 pgn->flags &= ~REMAKE;
623 } else {
624 PrintOnError(gn, "\nStop.");
625 exit(1);
627 } else if (gn->made == ERROR) {
629 * Already had an error when making this beastie. Tell the parent
630 * to abort.
632 pgn->flags &= ~REMAKE;
633 } else {
634 if (Lst_Member(gn->iParents, pgn) != NULL) {
635 char *p1;
636 Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), pgn, 0);
637 free(p1);
639 switch(gn->made) {
640 case BEINGMADE:
641 Error("Graph cycles through %s", gn->name);
642 gn->made = ERROR;
643 pgn->flags &= ~REMAKE;
644 break;
645 case MADE:
646 if ((gn->type & OP_EXEC) == 0) {
647 pgn->flags |= CHILDMADE;
648 Make_TimeStamp(pgn, gn);
650 break;
651 case UPTODATE:
652 if ((gn->type & OP_EXEC) == 0) {
653 Make_TimeStamp(pgn, gn);
655 break;
656 default:
657 break;
661 cohorts:
662 Lst_ForEach(gn->cohorts, Compat_Make, pgnp);
663 return (0);
667 *-----------------------------------------------------------------------
668 * Compat_Run --
669 * Initialize this mode and start making.
671 * Input:
672 * targs List of target nodes to re-create
674 * Results:
675 * None.
677 * Side Effects:
678 * Guess what?
680 *-----------------------------------------------------------------------
682 void
683 Compat_Run(Lst targs)
685 GNode *gn = NULL;/* Current root target */
686 int errors; /* Number of targets not remade due to errors */
688 if (!shellName)
689 Shell_Init();
691 if (bmake_signal(SIGINT, SIG_IGN) != SIG_IGN) {
692 bmake_signal(SIGINT, CompatInterrupt);
694 if (bmake_signal(SIGTERM, SIG_IGN) != SIG_IGN) {
695 bmake_signal(SIGTERM, CompatInterrupt);
697 if (bmake_signal(SIGHUP, SIG_IGN) != SIG_IGN) {
698 bmake_signal(SIGHUP, CompatInterrupt);
700 if (bmake_signal(SIGQUIT, SIG_IGN) != SIG_IGN) {
701 bmake_signal(SIGQUIT, CompatInterrupt);
704 ENDNode = Targ_FindNode(".END", TARG_CREATE);
705 ENDNode->type = OP_SPECIAL;
707 * If the user has defined a .BEGIN target, execute the commands attached
708 * to it.
710 if (!queryFlag) {
711 gn = Targ_FindNode(".BEGIN", TARG_NOCREATE);
712 if (gn != NULL) {
713 Compat_Make(gn, gn);
714 if (gn->made == ERROR) {
715 PrintOnError(gn, "\nStop.");
716 exit(1);
722 * Expand .USE nodes right now, because they can modify the structure
723 * of the tree.
725 Make_ExpandUse(targs);
728 * For each entry in the list of targets to create, call Compat_Make on
729 * it to create the thing. Compat_Make will leave the 'made' field of gn
730 * in one of several states:
731 * UPTODATE gn was already up-to-date
732 * MADE gn was recreated successfully
733 * ERROR An error occurred while gn was being created
734 * ABORTED gn was not remade because one of its inferiors
735 * could not be made due to errors.
737 errors = 0;
738 while (!Lst_IsEmpty (targs)) {
739 gn = (GNode *)Lst_DeQueue(targs);
740 Compat_Make(gn, gn);
742 if (gn->made == UPTODATE) {
743 printf("`%s' is up to date.\n", gn->name);
744 } else if (gn->made == ABORTED) {
745 printf("`%s' not remade because of errors.\n", gn->name);
746 errors += 1;
751 * If the user has defined a .END target, run its commands.
753 if (errors == 0) {
754 Compat_Make(ENDNode, ENDNode);
755 if (gn->made == ERROR) {
756 PrintOnError(gn, "\nStop.");
757 exit(1);