Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/make make(1): group the command line options and arg...



details:   https://anonhg.NetBSD.org/src/rev/40098a2d9752
branches:  trunk
changeset: 941681:40098a2d9752
user:      rillig <rillig%NetBSD.org@localhost>
date:      Mon Oct 26 21:34:10 2020 +0000

description:
make(1): group the command line options and arguments

By having a single struct that holds all command line options and
arguments, it is easy to see in the code when such a command line
argument is modified.  It also cleans up the namespace since the command
line options don't follow a common naming style.  Having them in a
struct also means that there is a single place for putting the
documentation, not two as before.

The struct also suggests to extract the initialization code out of main,
which is still too large, having more than 400 lines of code and
covering far too many topics.

diffstat:

 usr.bin/make/compat.c |   16 +-
 usr.bin/make/cond.c   |    6 +-
 usr.bin/make/job.c    |   34 +++---
 usr.bin/make/job.h    |    3 +-
 usr.bin/make/main.c   |  238 ++++++++++++++++++++++---------------------------
 usr.bin/make/make.c   |   22 ++--
 usr.bin/make/make.h   |  116 ++++++++++++++++-------
 usr.bin/make/parse.c  |   30 +++--
 usr.bin/make/targ.c   |   10 +-
 usr.bin/make/var.c    |   10 +-
 10 files changed, 254 insertions(+), 231 deletions(-)

diffs (truncated from 1296 to 300 lines):

diff -r be8f4ec8be75 -r 40098a2d9752 usr.bin/make/compat.c
--- a/usr.bin/make/compat.c     Mon Oct 26 20:18:33 2020 +0000
+++ b/usr.bin/make/compat.c     Mon Oct 26 21:34:10 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: compat.c,v 1.168 2020/10/24 04:40:45 rillig Exp $      */
+/*     $NetBSD: compat.c,v 1.169 2020/10/26 21:34:10 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.168 2020/10/24 04:40:45 rillig Exp $");
+MAKE_RCSID("$NetBSD: compat.c,v 1.169 2020/10/26 21:34:10 rillig Exp $");
 
 static GNode *curTarg = NULL;
 static pid_t compatChild;
@@ -113,7 +113,7 @@
        char *file_freeIt;
        const char *file = Var_Value(TARGET, gn, &file_freeIt);
 
-       if (!noExecute && eunlink(file) != -1) {
+       if (!opts.noExecute && eunlink(file) != -1) {
            Error("*** %s removed", file);
        }
 
@@ -420,7 +420,7 @@
            }
 #endif
            gn->made = ERROR;
-           if (keepgoing) {
+           if (opts.keepgoing) {
                /* Abort the current target, but let others continue. */
                printf(" (continuing)\n");
            } else {
@@ -526,7 +526,7 @@
         * If the user is just seeing if something is out-of-date, exit now
         * to tell him/her "yes".
         */
-       if (queryFlag) {
+       if (opts.queryFlag) {
            exit(1);
        }
 
@@ -551,7 +551,7 @@
             * Our commands are ok, but we still have to worry about the -t
             * flag...
             */
-           if (!touchFlag || (gn->type & OP_MAKE)) {
+           if (!opts.touchFlag || (gn->type & OP_MAKE)) {
                curTarg = gn;
 #ifdef USE_META
                if (useMeta && !NoExecute(gn)) {
@@ -586,7 +586,7 @@
                pgn->flags |= CHILDMADE;
                Make_TimeStamp(pgn, gn);
            }
-       } else if (keepgoing) {
+       } else if (opts.keepgoing) {
            pgn->flags &= ~(unsigned)REMAKE;
        } else {
            PrintOnError(gn, "\nStop.");
@@ -659,7 +659,7 @@
      * If the user has defined a .BEGIN target, execute the commands attached
      * to it.
      */
-    if (!queryFlag) {
+    if (!opts.queryFlag) {
        gn = Targ_FindNode(".BEGIN");
        if (gn != NULL) {
            Compat_Make(gn, gn);
diff -r be8f4ec8be75 -r 40098a2d9752 usr.bin/make/cond.c
--- a/usr.bin/make/cond.c       Mon Oct 26 20:18:33 2020 +0000
+++ b/usr.bin/make/cond.c       Mon Oct 26 21:34:10 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: cond.c,v 1.168 2020/10/24 04:51:19 rillig Exp $        */
+/*     $NetBSD: cond.c,v 1.169 2020/10/26 21:34:10 rillig Exp $        */
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -93,7 +93,7 @@
 #include "dir.h"
 
 /*     "@(#)cond.c     8.2 (Berkeley) 1/2/94"  */
-MAKE_RCSID("$NetBSD: cond.c,v 1.168 2020/10/24 04:51:19 rillig Exp $");
+MAKE_RCSID("$NetBSD: cond.c,v 1.169 2020/10/26 21:34:10 rillig Exp $");
 
 /*
  * The parsing of conditional expressions is based on this grammar:
@@ -294,7 +294,7 @@
 {
     StringListNode *ln;
 
-    for (ln = create->first; ln != NULL; ln = ln->next)
+    for (ln = opts.create->first; ln != NULL; ln = ln->next)
        if (Str_Match(ln->datum, arg))
            return TRUE;
     return FALSE;
diff -r be8f4ec8be75 -r 40098a2d9752 usr.bin/make/job.c
--- a/usr.bin/make/job.c        Mon Oct 26 20:18:33 2020 +0000
+++ b/usr.bin/make/job.c        Mon Oct 26 21:34:10 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: job.c,v 1.289 2020/10/26 20:11:02 rillig Exp $ */
+/*     $NetBSD: job.c,v 1.290 2020/10/26 21:34:10 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.289 2020/10/26 20:11:02 rillig Exp $");
+MAKE_RCSID("$NetBSD: job.c,v 1.290 2020/10/26 21:34:10 rillig Exp $");
 
 /* A shell defines how the commands are run.  All commands for a target are
  * written into a single file, which is then given to the shell to execute
@@ -402,7 +402,7 @@
 
 #define TARG_FMT  "%s %s ---\n" /* Default format */
 #define MESSAGE(fp, gn) \
-       if (maxJobs != 1 && targPrefix && *targPrefix) \
+       if (opts.maxJobs != 1 && targPrefix && *targPrefix) \
            (void)fprintf(fp, TARG_FMT, targPrefix, gn->name)
 
 static sigset_t caught_signals;        /* Set of signals we handle */
@@ -449,7 +449,7 @@
        return;
     if (Targ_Precious(gn))
        return;
-    if (noExecute)
+    if (opts.noExecute)
        return;
 
     file = GNode_Path(gn);
@@ -1083,7 +1083,7 @@
     /*
      * Set aborting if any error.
      */
-    if (errors && !keepgoing && (aborting != ABORT_INTERRUPT)) {
+    if (errors && !opts.keepgoing && (aborting != ABORT_INTERRUPT)) {
        /*
         * If we found any errors in this batch of children and the -k flag
         * wasn't given, we set the aborting flag so no more jobs get
@@ -1235,7 +1235,7 @@
        return TRUE;
     }
 
-    if (keepgoing) {
+    if (opts.keepgoing) {
        (void)fprintf(stdout, "%s: don't know how to make %s (%s)\n",
                      progname, gn->name, "continuing");
        (void)fflush(stdout);
@@ -1524,8 +1524,8 @@
      * need to reopen it to feed it to the shell. If the -n flag *was* given,
      * we just set the file to be stdout. Cute, huh?
      */
-    if (((gn->type & OP_MAKE) && !(noRecursiveExecute)) ||
-           (!noExecute && !touchFlag)) {
+    if (((gn->type & OP_MAKE) && !opts.noRecursiveExecute) ||
+           (!opts.noExecute && !opts.touchFlag)) {
        /*
         * tfile is the name of a file into which all shell commands are
         * put. It is removed before the child shell is executed, unless
@@ -1824,7 +1824,7 @@
             * our own free will.
             */
            if (*cp != '\0') {
-               if (!beSilent && job->node != lastNode) {
+               if (!opts.beSilent && job->node != lastNode) {
                    MESSAGE(stdout, job->node);
                    lastNode = job->node;
                }
@@ -2117,9 +2117,9 @@
 {
     Job_SetPrefix();
     /* Allocate space for all the job info */
-    job_table = bmake_malloc((size_t)maxJobs * sizeof *job_table);
-    memset(job_table, 0, (size_t)maxJobs * sizeof *job_table);
-    job_table_end = job_table + maxJobs;
+    job_table = bmake_malloc((size_t)opts.maxJobs * sizeof *job_table);
+    memset(job_table, 0, (size_t)opts.maxJobs * sizeof *job_table);
+    job_table_end = job_table + opts.maxJobs;
     wantToken =        0;
 
     aborting = 0;
@@ -2150,9 +2150,9 @@
 
     /* Preallocate enough for the maximum number of jobs.  */
     fds = bmake_malloc(sizeof(*fds) *
-       (npseudojobs + (size_t)maxJobs) * nfds_per_job());
+       (npseudojobs + (size_t)opts.maxJobs) * nfds_per_job());
     jobfds = bmake_malloc(sizeof(*jobfds) *
-       (npseudojobs + (size_t)maxJobs) * nfds_per_job());
+       (npseudojobs + (size_t)opts.maxJobs) * nfds_per_job());
 
     /* These are permanent entries and take slots 0 and 1 */
     watchfd(&tokenWaitJob);
@@ -2474,10 +2474,10 @@
 
     JobSigUnlock(&mask);
 
-    if (runINTERRUPT && !touchFlag) {
+    if (runINTERRUPT && !opts.touchFlag) {
        interrupt = Targ_FindNode(".INTERRUPT");
        if (interrupt != NULL) {
-           ignoreErrors = FALSE;
+           opts.ignoreErrors = FALSE;
            JobRun(interrupt);
        }
     }
@@ -2730,7 +2730,7 @@
     DEBUG3(JOB, "Job_TokenWithdraw(%d): aborting %d, running %d\n",
           getpid(), aborting, jobTokensRunning);
 
-    if (aborting || (jobTokensRunning >= maxJobs))
+    if (aborting || (jobTokensRunning >= opts.maxJobs))
        return FALSE;
 
     count = read(tokenWaitJob.inPipe, &tok, 1);
diff -r be8f4ec8be75 -r 40098a2d9752 usr.bin/make/job.h
--- a/usr.bin/make/job.h        Mon Oct 26 20:18:33 2020 +0000
+++ b/usr.bin/make/job.h        Mon Oct 26 21:34:10 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: job.h,v 1.57 2020/10/23 07:14:32 rillig Exp $  */
+/*     $NetBSD: job.h,v 1.58 2020/10/26 21:34:10 rillig Exp $  */
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -192,7 +192,6 @@
 extern char *shellErrFlag;
 
 extern int jobTokensRunning;   /* tokens currently "out" */
-extern int maxJobs;            /* Max jobs we can run */
 
 void Shell_Init(void);
 const char *Shell_GetNewline(void);
diff -r be8f4ec8be75 -r 40098a2d9752 usr.bin/make/main.c
--- a/usr.bin/make/main.c       Mon Oct 26 20:18:33 2020 +0000
+++ b/usr.bin/make/main.c       Mon Oct 26 21:34:10 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: main.c,v 1.390 2020/10/25 19:19:07 rillig Exp $        */
+/*     $NetBSD: main.c,v 1.391 2020/10/26 21:34:10 rillig Exp $        */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -118,7 +118,7 @@
 #include "trace.h"
 
 /*     "@(#)main.c     8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: main.c,v 1.390 2020/10/25 19:19:07 rillig Exp $");
+MAKE_RCSID("$NetBSD: main.c,v 1.391 2020/10/26 21:34:10 rillig Exp $");
 #if defined(MAKE_NATIVE) && !defined(lint)
 __COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993 "
            "The Regents of the University of California.  "
@@ -129,38 +129,18 @@
 #define        DEFMAXLOCAL DEFMAXJOBS
 #endif
 
-StringList *           create;         /* Targets to be made */
+CmdOpts opts;
 time_t                 now;            /* Time at start of make */
 GNode                  *DEFAULT;       /* .DEFAULT node */
 Boolean                        allPrecious;    /* .PRECIOUS given on line by itself */
 Boolean                        deleteOnError;  /* .DELETE_ON_ERROR: set */
 
-static Boolean         noBuiltins;     /* -r flag */
-static StringList *    makefiles;      /* ordered list of makefiles to read */
-static int             printVars;      /* -[vV] argument */
-#define COMPAT_VARS 1
-#define EXPAND_VARS 2
-static StringList *    variables;      /* list of variables to print
-                                        * (for -v and -V) */
-int                    maxJobs;        /* -j argument */
 static int             maxJobTokens;   /* -j argument */
-Boolean                        compatMake;     /* -B argument */
-DebugFlags             debug;          /* -d argument */
 Boolean                        debugVflag;     /* -dV */
-Boolean                        noExecute;      /* -n flag */
-Boolean                        noRecursiveExecute;     /* -N flag */
-Boolean                        keepgoing;      /* -k flag */
-Boolean                        queryFlag;      /* -q flag */
-Boolean                        touchFlag;      /* -t flag */
-Boolean                        enterFlag;      /* -w flag */
 Boolean                        enterFlagObj;   /* -w and objdir != srcdir */
-Boolean                        ignoreErrors;   /* -i flag */
-Boolean                        beSilent;       /* -s flag */
+
 Boolean                        oldVars;        /* variable substitution style */
-Boolean                        checkEnvFirst;  /* -e flag */
-Boolean                        parseWarnFatal; /* -W flag */
 static int jp_0 = -1, jp_1 = -1;       /* ends of parent job pipe */
-Boolean                        varNoExportEnv; /* -X flag */
 Boolean                        doing_depend;   /* Set while reading .depend */
 static Boolean         jobsRunning;    /* TRUE if the jobs might be running */
 static const char *    tracefile;
@@ -176,8 +156,6 @@
 pid_t myPid;
 int makelevel;
 



Home | Main Index | Thread Index | Old Index