Module Name: src Committed By: rillig Date: Thu Dec 10 20:49:11 UTC 2020
Modified Files: src/usr.bin/make: compat.c job.c job.h meta.c meta.h trace.c src/usr.bin/make/unit-tests: opt-debug-jobs.exp Log Message: make(1): split JobFlags into separate fields Having all these flags in a single bitmask makes it harder to see where exactly they can possibly be used since their state could also be modified using the unsuspicious job->flags = 0. Using individual names just leaves the single memset, and that is only used during initialization. To generate a diff of this commit: cvs rdiff -u -r1.204 -r1.205 src/usr.bin/make/compat.c cvs rdiff -u -r1.353 -r1.354 src/usr.bin/make/job.c cvs rdiff -u -r1.64 -r1.65 src/usr.bin/make/job.h cvs rdiff -u -r1.157 -r1.158 src/usr.bin/make/meta.c cvs rdiff -u -r1.8 -r1.9 src/usr.bin/make/meta.h cvs rdiff -u -r1.21 -r1.22 src/usr.bin/make/trace.c cvs rdiff -u -r1.8 -r1.9 src/usr.bin/make/unit-tests/opt-debug-jobs.exp Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/usr.bin/make/compat.c diff -u src/usr.bin/make/compat.c:1.204 src/usr.bin/make/compat.c:1.205 --- src/usr.bin/make/compat.c:1.204 Mon Dec 7 01:35:33 2020 +++ src/usr.bin/make/compat.c Thu Dec 10 20:49:11 2020 @@ -1,4 +1,4 @@ -/* $NetBSD: compat.c,v 1.204 2020/12/07 01:35:33 rillig Exp $ */ +/* $NetBSD: compat.c,v 1.205 2020/12/10 20:49:11 rillig Exp $ */ /* * Copyright (c) 1988, 1989, 1990 The Regents of the University of California. @@ -96,7 +96,7 @@ #include "pathnames.h" /* "@(#)compat.c 8.2 (Berkeley) 3/19/94" */ -MAKE_RCSID("$NetBSD: compat.c,v 1.204 2020/12/07 01:35:33 rillig Exp $"); +MAKE_RCSID("$NetBSD: compat.c,v 1.205 2020/12/10 20:49:11 rillig Exp $"); static GNode *curTarg = NULL; static pid_t compatChild; @@ -409,7 +409,7 @@ Compat_RunCommand(const char *cmdp, GNod if (errCheck) { #ifdef USE_META if (useMeta) { - meta_job_error(NULL, gn, 0, status); + meta_job_error(NULL, gn, FALSE, status); } #endif gn->made = ERROR; Index: src/usr.bin/make/job.c diff -u src/usr.bin/make/job.c:1.353 src/usr.bin/make/job.c:1.354 --- src/usr.bin/make/job.c:1.353 Thu Dec 10 20:14:35 2020 +++ src/usr.bin/make/job.c Thu Dec 10 20:49:11 2020 @@ -1,4 +1,4 @@ -/* $NetBSD: job.c,v 1.353 2020/12/10 20:14:35 rillig Exp $ */ +/* $NetBSD: job.c,v 1.354 2020/12/10 20:49:11 rillig Exp $ */ /* * Copyright (c) 1988, 1989, 1990 The Regents of the University of California. @@ -143,7 +143,7 @@ #include "trace.h" /* "@(#)job.c 8.2 (Berkeley) 3/19/94" */ -MAKE_RCSID("$NetBSD: job.c,v 1.353 2020/12/10 20:14:35 rillig Exp $"); +MAKE_RCSID("$NetBSD: job.c,v 1.354 2020/12/10 20:49:11 rillig Exp $"); /* * A shell defines how the commands are run. All commands for a target are @@ -451,15 +451,27 @@ nfds_per_job(void) return 1; } +void +Job_FlagsToString(char *buf, size_t bufsize, const JobFlags *flags) +{ + snprintf(buf, bufsize, "%c%c%c%c", + flags->ignerr ? 'i' : '-', + flags->silent ? 's' : '-', + flags->special ? 'S' : '-', + flags->xtraced ? 'x' : '-'); +} + static void job_table_dump(const char *where) { Job *job; + char flags[5]; debug_printf("job table @ %s\n", where); for (job = job_table; job < job_table_end; job++) { - debug_printf("job %d, status %d, flags %d, pid %d\n", - (int)(job - job_table), job->status, job->flags, job->pid); + Job_FlagsToString(flags, sizeof flags, &job->flags); + debug_printf("job %d, status %d, flags %s, pid %d\n", + (int)(job - job_table), job->status, flags, job->pid); } } @@ -750,7 +762,7 @@ JobPrintln(Job *job, const char *line) static void JobPrintSpecialsErrCtl(Job *job, Boolean echo) { - if (!(job->flags & JOB_SILENT) && echo && commandShell->hasEchoCtl) { + if (!job->flags.silent && echo && commandShell->hasEchoCtl) { JobPrintln(job, commandShell->echoOff); JobPrintln(job, commandShell->errOffOrExecIgnore); JobPrintln(job, commandShell->echoOn); @@ -770,9 +782,9 @@ static void JobPrintSpecialsEchoCtl(Job *job, RunFlags *inout_runFlags, const char *escCmd, const char **inout_cmdTemplate) { - job->flags |= JOB_IGNERR; + job->flags.ignerr = TRUE; - if (!(job->flags & JOB_SILENT) && inout_runFlags->echo) { + if (!job->flags.silent && inout_runFlags->echo) { if (commandShell->hasEchoCtl) JobPrintln(job, commandShell->echoOff); JobPrintf(job, commandShell->errOnOrEcho, escCmd); @@ -871,8 +883,7 @@ JobPrintCommand(Job *job, char *cmd) escCmd = EscapeShellDblQuot(cmd); if (!runFlags.echo) { - if (!(job->flags & JOB_SILENT) && run && - commandShell->hasEchoCtl) { + if (!job->flags.silent && run && commandShell->hasEchoCtl) { JobPrintln(job, commandShell->echoOff); } else { if (commandShell->hasErrCtl) @@ -892,7 +903,7 @@ JobPrintCommand(Job *job, char *cmd) if (!commandShell->hasErrCtl && commandShell->errExit && commandShell->errExit[0] != '\0') { - if (!(job->flags & JOB_SILENT) && runFlags.echo) { + if (!job->flags.silent && runFlags.echo) { if (commandShell->hasEchoCtl) JobPrintln(job, commandShell->echoOff); JobPrintf(job, commandShell->errOnOrEcho, @@ -913,9 +924,9 @@ JobPrintCommand(Job *job, char *cmd) } if (DEBUG(SHELL) && strcmp(shellName, "sh") == 0 && - !(job->flags & JOB_TRACED)) { + !job->flags.xtraced) { JobPrintln(job, "set -x"); - job->flags |= JOB_TRACED; + job->flags.xtraced = TRUE; } JobPrintf(job, cmdTemplate, cmd); @@ -927,7 +938,7 @@ JobPrintCommand(Job *job, char *cmd) * echoOff command. Otherwise we issue it and pretend it was on * for the whole command... */ - if (runFlags.echo && !(job->flags & JOB_SILENT) && + if (runFlags.echo && !job->flags.silent && commandShell->hasEchoCtl) { JobPrintln(job, commandShell->echoOff); runFlags.echo = FALSE; @@ -1016,7 +1027,7 @@ JobFinish(Job *job, int status) job->pid, job->node->name, status); if ((WIFEXITED(status) && - ((WEXITSTATUS(status) != 0 && !(job->flags & JOB_IGNERR)))) || + ((WEXITSTATUS(status) != 0 && !job->flags.ignerr))) || WIFSIGNALED(status)) { /* * If it exited non-zero and either we're doing things our @@ -1056,7 +1067,8 @@ JobFinish(Job *job, int status) #ifdef USE_META if (useMeta) { meta_job_error(job, job->node, - job->flags, WEXITSTATUS(status)); + job->flags.ignerr, + WEXITSTATUS(status)); } #endif if (!shouldDieQuietly(job->node, -1)) @@ -1064,9 +1076,9 @@ JobFinish(Job *job, int status) "*** [%s] Error code %d%s\n", job->node->name, WEXITSTATUS(status), - (job->flags & JOB_IGNERR) + job->flags.ignerr ? " (ignored)" : ""); - if (job->flags & JOB_IGNERR) { + if (job->flags.ignerr) { status = 0; } else { if (deleteOnError) { @@ -1102,7 +1114,7 @@ JobFinish(Job *job, int status) return_job_token = FALSE; Trace_Log(JOBEND, job); - if (!(job->flags & JOB_SPECIAL)) { + if (!job->flags.special) { if (status != 0 || (aborting == ABORT_ERROR) || aborting == ABORT_INTERRUPT) return_job_token = TRUE; @@ -1117,7 +1129,7 @@ JobFinish(Job *job, int status) */ JobSaveCommands(job); job->node->made = MADE; - if (!(job->flags & JOB_SPECIAL)) + if (!job->flags.special) return_job_token = TRUE; Make_Update(job->node); job->status = JOB_ST_FREE; @@ -1296,7 +1308,7 @@ JobExec(Job *job, char **argv) int cpid; /* ID of new child */ sigset_t mask; - job->flags &= ~JOB_TRACED; + job->flags.xtraced = FALSE; if (DEBUG(JOB)) { int i; @@ -1315,7 +1327,7 @@ JobExec(Job *job, char **argv) * banner with their name in it never appears). This is an attempt to * provide that feedback, even if nothing follows it. */ - if (!(job->flags & JOB_SILENT)) + if (!job->flags.silent) SwitchOutputTo(job->node); /* No interruptions until this job is on the `jobs' list */ @@ -1469,9 +1481,9 @@ JobMakeArgv(Job *job, char **argv) * practically relevant. */ (void)snprintf(args, sizeof args, "-%s%s", - ((job->flags & JOB_IGNERR) ? "" : + (job->flags.ignerr ? "" : (commandShell->exit ? commandShell->exit : "")), - ((job->flags & JOB_SILENT) ? "" : + (job->flags.silent ? "" : (commandShell->echo ? commandShell->echo : ""))); if (args[1]) { @@ -1479,11 +1491,11 @@ JobMakeArgv(Job *job, char **argv) argc++; } } else { - if (!(job->flags & JOB_IGNERR) && commandShell->exit) { + if (!job->flags.ignerr && commandShell->exit) { argv[argc] = UNCONST(commandShell->exit); argc++; } - if (!(job->flags & JOB_SILENT) && commandShell->echo) { + if (!job->flags.silent && commandShell->echo) { argv[argc] = UNCONST(commandShell->echo); argc++; } @@ -1512,7 +1524,7 @@ JobMakeArgv(Job *job, char **argv) * NB: The return value is ignored by everyone. */ static JobStartResult -JobStart(GNode *gn, JobFlags flags) +JobStart(GNode *gn, Boolean special) { Job *job; /* new job descriptor */ char *argv[10]; /* Argument vector to shell */ @@ -1532,13 +1544,10 @@ JobStart(GNode *gn, JobFlags flags) job->tailCmds = NULL; job->status = JOB_ST_SET_UP; - if (gn->type & OP_SPECIAL) - flags |= JOB_SPECIAL; - if (Targ_Ignore(gn)) - flags |= JOB_IGNERR; - if (Targ_Silent(gn)) - flags |= JOB_SILENT; - job->flags = flags; + job->flags.special = special || (gn->type & OP_SPECIAL); + job->flags.ignerr = Targ_Ignore(gn); + job->flags.silent = Targ_Silent(gn); + job->flags.xtraced = FALSE; /* * Check the commands now so any attributes from .DEFAULT have a @@ -1592,7 +1601,7 @@ JobStart(GNode *gn, JobFlags flags) if (useMeta) { meta_job_start(job, gn); if (Targ_Silent(gn)) /* might have changed */ - job->flags |= JOB_SILENT; + job->flags.silent = TRUE; } #endif /* We can do all the commands at once. hooray for sanity */ @@ -1633,7 +1642,7 @@ JobStart(GNode *gn, JobFlags flags) * good -- it does no harm to keep working up the graph. */ job->cmdFILE = stdout; - Job_Touch(gn, (job->flags & JOB_SILENT) != 0); + Job_Touch(gn, job->flags.silent); run = FALSE; } /* Just in case it isn't already... */ @@ -1641,7 +1650,7 @@ JobStart(GNode *gn, JobFlags flags) /* If we're not supposed to execute a shell, don't. */ if (!run) { - if (!(job->flags & JOB_SPECIAL)) + if (!job->flags.special) Job_TokenReturn(); /* Unlink and close the command file if we opened one */ if (job->cmdFILE != NULL && job->cmdFILE != stdout) { @@ -2049,7 +2058,7 @@ Job_CatchOutput(void) void Job_Make(GNode *gn) { - (void)JobStart(gn, JOB_NONE); + (void)JobStart(gn, FALSE); } static void Index: src/usr.bin/make/job.h diff -u src/usr.bin/make/job.h:1.64 src/usr.bin/make/job.h:1.65 --- src/usr.bin/make/job.h:1.64 Sun Nov 29 09:27:40 2020 +++ src/usr.bin/make/job.h Thu Dec 10 20:49:11 2020 @@ -1,4 +1,4 @@ -/* $NetBSD: job.h,v 1.64 2020/11/29 09:27:40 rillig Exp $ */ +/* $NetBSD: job.h,v 1.65 2020/12/10 20:49:11 rillig Exp $ */ /* * Copyright (c) 1988, 1989, 1990 The Regents of the University of California. @@ -125,17 +125,16 @@ typedef enum JobStatus { JOB_ST_FINISHED = 4 /* Job is done (ie after SIGCHILD) */ } JobStatus; -typedef enum JobFlags { - JOB_NONE = 0, +typedef struct JobFlags { /* Ignore non-zero exits */ - JOB_IGNERR = 1 << 0, + Boolean ignerr; /* no output */ - JOB_SILENT = 1 << 1, + Boolean silent; /* Target is a special one. i.e. run it locally * if we can't export it and maxLocal is 0 */ - JOB_SPECIAL = 1 << 2, + Boolean special; /* we've sent 'set -x' */ - JOB_TRACED = 1 << 10 + Boolean xtraced; } JobFlags; /* A Job manages the shell commands that are run to create a single target. @@ -211,5 +210,6 @@ Boolean Job_TokenWithdraw(void); void Job_ServerStart(int, int, int); void Job_SetPrefix(void); Boolean Job_RunTarget(const char *, const char *); +void Job_FlagsToString(char *, size_t, const JobFlags *); #endif /* MAKE_JOB_H */ Index: src/usr.bin/make/meta.c diff -u src/usr.bin/make/meta.c:1.157 src/usr.bin/make/meta.c:1.158 --- src/usr.bin/make/meta.c:1.157 Sat Dec 5 17:46:41 2020 +++ src/usr.bin/make/meta.c Thu Dec 10 20:49:11 2020 @@ -1,4 +1,4 @@ -/* $NetBSD: meta.c,v 1.157 2020/12/05 17:46:41 rillig Exp $ */ +/* $NetBSD: meta.c,v 1.158 2020/12/10 20:49:11 rillig Exp $ */ /* * Implement 'meta' mode. @@ -772,7 +772,7 @@ meta_job_event(Job *job) } void -meta_job_error(Job *job, GNode *gn, int flags, int status) +meta_job_error(Job *job, GNode *gn, Boolean ignerr, int status) { char cwd[MAXPATHLEN]; BuildMon *pbm; @@ -786,9 +786,7 @@ meta_job_error(Job *job, GNode *gn, int } if (pbm->mfp != NULL) { fprintf(pbm->mfp, "\n*** Error code %d%s\n", - status, - (flags & JOB_IGNERR) ? - "(ignored)" : ""); + status, ignerr ? "(ignored)" : ""); } if (gn != NULL) { Var_Set(".ERROR_TARGET", GNode_Path(gn), VAR_GLOBAL); Index: src/usr.bin/make/meta.h diff -u src/usr.bin/make/meta.h:1.8 src/usr.bin/make/meta.h:1.9 --- src/usr.bin/make/meta.h:1.8 Mon Oct 19 23:43:55 2020 +++ src/usr.bin/make/meta.h Thu Dec 10 20:49:11 2020 @@ -1,4 +1,4 @@ -/* $NetBSD: meta.h,v 1.8 2020/10/19 23:43:55 rillig Exp $ */ +/* $NetBSD: meta.h,v 1.9 2020/12/10 20:49:11 rillig Exp $ */ /* * Things needed for 'meta' mode. @@ -48,7 +48,7 @@ void meta_job_child(struct Job *); void meta_job_parent(struct Job *, pid_t); int meta_job_fd(struct Job *); int meta_job_event(struct Job *); -void meta_job_error(struct Job *, GNode *, int, int); +void meta_job_error(struct Job *, GNode *, Boolean, int); void meta_job_output(struct Job *, char *, const char *); int meta_cmd_finish(void *); int meta_job_finish(struct Job *); Index: src/usr.bin/make/trace.c diff -u src/usr.bin/make/trace.c:1.21 src/usr.bin/make/trace.c:1.22 --- src/usr.bin/make/trace.c:1.21 Sat Oct 31 22:05:56 2020 +++ src/usr.bin/make/trace.c Thu Dec 10 20:49:11 2020 @@ -1,4 +1,4 @@ -/* $NetBSD: trace.c,v 1.21 2020/10/31 22:05:56 rillig Exp $ */ +/* $NetBSD: trace.c,v 1.22 2020/12/10 20:49:11 rillig Exp $ */ /*- * Copyright (c) 2000 The NetBSD Foundation, Inc. @@ -48,7 +48,7 @@ #include "job.h" #include "trace.h" -MAKE_RCSID("$NetBSD: trace.c,v 1.21 2020/10/31 22:05:56 rillig Exp $"); +MAKE_RCSID("$NetBSD: trace.c,v 1.22 2020/12/10 20:49:11 rillig Exp $"); static FILE *trfile; static pid_t trpid; @@ -92,8 +92,11 @@ Trace_Log(TrEvent event, Job *job) jobTokensRunning, evname[event], trpid, trwd); if (job != NULL) { - fprintf(trfile, " %s %d %x %x", job->node->name, - job->pid, job->flags, job->node->type); + char flags[5]; + + Job_FlagsToString(flags, sizeof flags, &job->flags); + fprintf(trfile, " %s %d %s %x", job->node->name, + job->pid, flags, job->node->type); } fputc('\n', trfile); fflush(trfile); Index: src/usr.bin/make/unit-tests/opt-debug-jobs.exp diff -u src/usr.bin/make/unit-tests/opt-debug-jobs.exp:1.8 src/usr.bin/make/unit-tests/opt-debug-jobs.exp:1.9 --- src/usr.bin/make/unit-tests/opt-debug-jobs.exp:1.8 Thu Nov 19 21:46:10 2020 +++ src/usr.bin/make/unit-tests/opt-debug-jobs.exp Thu Dec 10 20:49:11 2020 @@ -16,7 +16,7 @@ Running all Command: <shell> JobExec(all): pid <pid> added to jobs table job table @ job started -job 0, status 3, flags 0, pid <pid> +job 0, status 3, flags ----, pid <pid> : expanded expression : variable : 'single' and "double" quotes