Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/make Allow local variable assignments in dependency ...



details:   https://anonhg.NetBSD.org/src/rev/0797646aa942
branches:  trunk
changeset: 359841:0797646aa942
user:      sjg <sjg%NetBSD.org@localhost>
date:      Thu Jan 27 06:02:59 2022 +0000

description:
Allow local variable assignments in dependency lines

The variable is set in the context of the target.
This syntax has been supported by gmake for ~ever.
If necessary a makefile can set .MAKE.TARGET_LOCAL_VARIABLES=false
to disable this.

Expose GetBooleanExpr so parse.c can use it.

diffstat:

 usr.bin/make/main.c                      |   6 +-
 usr.bin/make/make.1                      |  34 +++++++++++--
 usr.bin/make/make.h                      |   3 +-
 usr.bin/make/meta.c                      |  12 +++-
 usr.bin/make/parse.c                     |  81 ++++++++++++++++++++++++-------
 usr.bin/make/unit-tests/meta-cmd-cmp.exp |   3 +
 usr.bin/make/unit-tests/meta-cmd-cmp.mk  |  24 ++++++--
 usr.bin/make/var.c                       |   7 +-
 8 files changed, 128 insertions(+), 42 deletions(-)

diffs (truncated from 394 to 300 lines):

diff -r fcf285e48078 -r 0797646aa942 usr.bin/make/main.c
--- a/usr.bin/make/main.c       Thu Jan 27 02:34:23 2022 +0000
+++ b/usr.bin/make/main.c       Thu Jan 27 06:02:59 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: main.c,v 1.575 2022/01/22 18:59:23 rillig Exp $        */
+/*     $NetBSD: main.c,v 1.576 2022/01/27 06:02:59 sjg Exp $   */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -111,7 +111,7 @@
 #include "trace.h"
 
 /*     "@(#)main.c     8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: main.c,v 1.575 2022/01/22 18:59:23 rillig Exp $");
+MAKE_RCSID("$NetBSD: main.c,v 1.576 2022/01/27 06:02:59 sjg Exp $");
 #if defined(MAKE_NATIVE) && !defined(lint)
 __COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993 "
            "The Regents of the University of California.  "
@@ -842,7 +842,7 @@
  * If set, anything that looks or smells like "No", "False", "Off", "0", etc.
  * is false, otherwise true.
  */
-static bool
+bool
 GetBooleanExpr(const char *expr, bool fallback)
 {
        char *value;
diff -r fcf285e48078 -r 0797646aa942 usr.bin/make/make.1
--- a/usr.bin/make/make.1       Thu Jan 27 02:34:23 2022 +0000
+++ b/usr.bin/make/make.1       Thu Jan 27 06:02:59 2022 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: make.1,v 1.301 2022/01/13 04:51:50 sjg Exp $
+.\"    $NetBSD: make.1,v 1.302 2022/01/27 06:02:59 sjg Exp $
 .\"
 .\" Copyright (c) 1990, 1993
 .\"    The Regents of the University of California.  All rights reserved.
@@ -29,7 +29,7 @@
 .\"
 .\"    from: @(#)make.1        8.4 (Berkeley) 3/19/94
 .\"
-.Dd January 12, 2022
+.Dd January 21, 2022
 .Dt MAKE 1
 .Os
 .Sh NAME
@@ -691,10 +691,27 @@
 Variables that are defined specific to a certain target.
 .El
 .Pp
-Local variables are all built in and their values vary magically from
-target to target.
-It is not currently possible to define new local variables.
-The seven local variables are as follows:
+Local variables can be set on a dependency line, if
+.Va .MAKE.TARGET_LOCAL_VARIABLES ,
+is not set to
+.Ql false .
+The rest of the line
+(which will already have had Global variables expanded),
+is the variable value.
+For example:
+.Bd -literal -offset indent
+COMPILER_WRAPPERS+= ccache distcc icecc
+
+${OBJS}: .MAKE.META.CMP_FILTER=${COMPILER_WRAPPERS:S,^,N,}
+.Ed
+.Pp
+Only the targets
+.Ql ${OBJS}
+will be impacted by that filter (in "meta" mode) and
+simply enabling/disabling any of the wrappers will not render all
+of those targets out-of-date.
+.Pp
+The seven built-in local variables are as follows:
 .Bl -tag -width ".ARCHIVE" -offset indent
 .It Va .ALLSRC
 The list of all sources for this target; also known as
@@ -846,6 +863,11 @@
 would produce tokens like
 .Ql ---make[1234] target ---
 making it easier to track the degree of parallelism being achieved.
+.It .MAKE.TARGET_LOCAL_VARIABLES
+If set to
+.Ql false ,
+apparent variable assignments in dependency lines are
+treated as normal sources.
 .It Ev MAKEFLAGS
 The environment variable
 .Ql Ev MAKEFLAGS
diff -r fcf285e48078 -r 0797646aa942 usr.bin/make/make.h
--- a/usr.bin/make/make.h       Thu Jan 27 02:34:23 2022 +0000
+++ b/usr.bin/make/make.h       Thu Jan 27 06:02:59 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: make.h,v 1.290 2022/01/23 22:12:16 rillig Exp $        */
+/*     $NetBSD: make.h,v 1.291 2022/01/27 06:02:59 sjg Exp $   */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -826,6 +826,7 @@
 char *getTmpdir(void) MAKE_ATTR_USE;
 bool ParseBoolean(const char *, bool) MAKE_ATTR_USE;
 const char *cached_realpath(const char *, char *);
+bool GetBooleanExpr(const char *, bool);
 
 /* parse.c */
 void Parse_Init(void);
diff -r fcf285e48078 -r 0797646aa942 usr.bin/make/meta.c
--- a/usr.bin/make/meta.c       Thu Jan 27 02:34:23 2022 +0000
+++ b/usr.bin/make/meta.c       Thu Jan 27 06:02:59 2022 +0000
@@ -1,4 +1,4 @@
-/*      $NetBSD: meta.c,v 1.194 2022/01/26 12:41:26 rillig Exp $ */
+/*      $NetBSD: meta.c,v 1.195 2022/01/27 06:02:59 sjg Exp $ */
 
 /*
  * Implement 'meta' mode.
@@ -1060,14 +1060,14 @@
 }
 
 static int
-meta_cmd_cmp(GNode *gn, char *a, char *b)
+meta_cmd_cmp(GNode *gn, char *a, char *b, bool filter)
 {
     static bool once = false;
     static Buffer buf;
     int rc;
 
     rc = strcmp(a, b);
-    if (rc == 0 || !metaCmpFilter)
+    if (rc == 0 || !filter)
        return rc;
     if (!once) {
        once = true;
@@ -1105,6 +1105,7 @@
     bool needOODATE = false;
     StringList missingFiles;
     bool have_filemon = false;
+    bool cmp_filter;
 
     if (oodate)
        return oodate;          /* we're done */
@@ -1164,6 +1165,9 @@
        /* we want to track all the .meta we read */
        Global_Append(".MAKE.META.FILES", fname);
 
+       cmp_filter = metaCmpFilter ? metaCmpFilter :
+           Var_Exists(gn, MAKE_META_CMP_FILTER);
+
        cmdNode = gn->commands.first;
        while (!oodate && (x = fgetLine(&buf, &bufsz, 0, fp)) > 0) {
            lineno++;
@@ -1556,7 +1560,7 @@
                    if (p != NULL &&
                        !hasOODATE &&
                        !(gn->type & OP_NOMETA_CMP) &&
-                       (meta_cmd_cmp(gn, p, cmd) != 0)) {
+                       (meta_cmd_cmp(gn, p, cmd, cmp_filter) != 0)) {
                        DEBUG4(META, "%s: %d: a build command has changed\n%s\nvs\n%s\n",
                               fname, lineno, p, cmd);
                        if (!metaIgnoreCMDs)
diff -r fcf285e48078 -r 0797646aa942 usr.bin/make/parse.c
--- a/usr.bin/make/parse.c      Thu Jan 27 02:34:23 2022 +0000
+++ b/usr.bin/make/parse.c      Thu Jan 27 06:02:59 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: parse.c,v 1.655 2022/01/22 18:59:23 rillig Exp $       */
+/*     $NetBSD: parse.c,v 1.656 2022/01/27 06:02:59 sjg Exp $  */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -106,7 +106,7 @@
 #include "pathnames.h"
 
 /*     "@(#)parse.c    8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: parse.c,v 1.655 2022/01/22 18:59:23 rillig Exp $");
+MAKE_RCSID("$NetBSD: parse.c,v 1.656 2022/01/27 06:02:59 sjg Exp $");
 
 /*
  * A file being read.
@@ -172,6 +172,24 @@
 typedef List SearchPathList;
 typedef ListNode SearchPathListNode;
 
+
+typedef enum VarAssignOp {
+       VAR_NORMAL,             /* = */
+       VAR_APPEND,             /* += */
+       VAR_DEFAULT,            /* ?= */
+       VAR_SUBST,              /* := */
+       VAR_SHELL               /* != or :sh= */
+} VarAssignOp;
+
+typedef struct VarAssign {
+       char *varname;          /* unexpanded */
+       VarAssignOp op;
+       const char *value;      /* unexpanded */
+} VarAssign;
+
+static bool Parse_IsVar(const char *, VarAssign *);
+static void Parse_Var_Keep(VarAssign *, GNode *);
+
 /*
  * The target to be made if no targets are specified in the command line.
  * This is the first target defined in any of the makefiles.
@@ -1263,12 +1281,48 @@
        }
 }
 
+static void
+LinkSourceVar(GNode *pgn, VarAssign *var)
+{
+       Parse_Var_Keep(var, pgn);
+}
+
+static void
+LinkVarToTargets(VarAssign *var)
+{
+       GNodeListNode *ln;
+
+       for (ln = targets->first; ln != NULL; ln = ln->next)
+               LinkSourceVar(ln->datum, var);
+       
+}
+
 static bool
 ParseDependencySourcesMundane(char *start,
                              ParseSpecial special, GNodeType targetAttr)
 {
        while (*start != '\0') {
                char *end = start;
+               VarAssign var;
+
+               /*
+                * Check for local variable assignment,
+                * rest of the line is the value.
+                */
+               if (Parse_IsVar(start, &var)) {
+                       /*
+                        * Check if this makefile has disabled
+                        * setting local variables.
+                        */
+                       bool target_vars = GetBooleanExpr("${.MAKE.TARGET_LOCAL_VARIABLES}", true);
+
+                       if (target_vars)
+                               LinkVarToTargets(&var);
+                       free(var.varname);
+                       if (target_vars)
+                               return true;
+               }
+                       
                /*
                 * The targets take real sources, so we must beware of archive
                 * specifications (i.e. things with left parentheses in them)
@@ -1421,20 +1475,6 @@
                Lst_Free(paths);
 }
 
-typedef enum VarAssignOp {
-       VAR_NORMAL,             /* = */
-       VAR_APPEND,             /* += */
-       VAR_DEFAULT,            /* ?= */
-       VAR_SUBST,              /* := */
-       VAR_SHELL               /* != or :sh= */
-} VarAssignOp;
-
-typedef struct VarAssign {
-       char *varname;          /* unexpanded */
-       VarAssignOp op;
-       const char *value;      /* unexpanded */
-} VarAssign;
-
 /*
  * Determine the assignment operator and adjust the end of the variable
  * name accordingly.
@@ -1685,7 +1725,7 @@
 
 /* Perform the variable assignment in the given scope. */
 static void
-Parse_Var(VarAssign *var, GNode *scope)
+Parse_Var_Keep(VarAssign *var, GNode *scope)
 {
        FStr avalue;            /* actual value (maybe expanded) */
 
@@ -1694,7 +1734,12 @@
                VarAssignSpecial(var->varname, avalue.str);
                FStr_Done(&avalue);
        }
-
+}
+              
+static void
+Parse_Var(VarAssign *var, GNode *scope)
+{
+       Parse_Var_Keep(var, scope);
        free(var->varname);
 }
 
diff -r fcf285e48078 -r 0797646aa942 usr.bin/make/unit-tests/meta-cmd-cmp.exp
--- a/usr.bin/make/unit-tests/meta-cmd-cmp.exp  Thu Jan 27 02:34:23 2022 +0000
+++ b/usr.bin/make/unit-tests/meta-cmd-cmp.exp  Thu Jan 27 06:02:59 2022 +0000
@@ -47,4 +47,7 @@
 filter2:



Home | Main Index | Thread Index | Old Index