]> git.ipfire.org Git - thirdparty/make.git/commitdiff
* NEWS: Do not insert a space during '+=' if the value is empty.
authorPaul Smith <psmith@gnu.org>
Sun, 28 May 2017 00:07:30 +0000 (20:07 -0400)
committerPaul Smith <psmith@gnu.org>
Sun, 4 Jun 2017 22:37:21 +0000 (18:37 -0400)
* doc/make.texi (Appending): Document this behavior.
* variable.c (do_variable_definition): Only add a space if the variable
value is not empty.
* tests/scripts/variables/flavors: Test this behavior.

NEWS
doc/make.texi
tests/scripts/variables/flavors
variable.c

diff --git a/NEWS b/NEWS
index e60644a1d2b0500526bbf098786ee2bd4f4071b8..6e2c5c65bf682d809363eebcdcc12916b40edf1d 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -29,6 +29,12 @@ http://sv.gnu.org/bugs/index.php?group=make&report_id=111&fix_release_id=108&set
   This was claimed to be fixed in 3.81, but wasn't, for some reason.
   To detect this change search for 'nocomment' in the .FEATURES variable.
 
+* WARNING: Backward-incompatibility!
+  Previously appending using '+=' to an empty variable would result in a value
+  starting with a space.  Now the initial space is only added if the variable
+  already contains some value.  Similarly, appending an empty string does not
+  add a trailing space.
+
 * The previous limit of 63 jobs under -jN on MS-Windows is now
   increased to 4095.  That limit includes the subprocess started by
   the $(shell) function.
index c7c0f4f95c38ddde7c799b08da2697180b77be32..dfa44549272aa4f43620f2863a3c92fd845c6530 100644 (file)
@@ -5720,7 +5720,8 @@ objects += another.o
 
 @noindent
 This takes the value of the variable @code{objects}, and adds the text
-@samp{another.o} to it (preceded by a single space).  Thus:
+@samp{another.o} to it (preceded by a single space, if it has a value
+already).  Thus:
 
 @example
 objects = main.o foo.o bar.o utils.o
index ba133ea860d2fc204b3b75c54837da2e2dad328f..ed060dcb70c9274539f029a0049623767334ea8a 100644 (file)
@@ -93,4 +93,23 @@ all: ; @echo $(foo)
 ',
               '', "Goodbye\n");
 
+# TEST 8: Append to empty
+run_make_test(q!
+recur =
+recur += foo
+simple :=
+simple += bar
+recur_empty = foo
+recur_empty +=
+simple_empty := bar
+simple_empty +=
+empty_recur =
+empty_recur +=
+empty_simple :=
+empty_simple +=
+
+all: ; @: $(info recur=/$(recur)/ simple=/$(simple)/ recure=/$(recur_empty)/ simplee=/$(simple_empty)/ erecur=/$(empty_recur)/ esimple=/$(empty_simple)/)
+!,
+              '', "recur=/foo/ simple=/bar/ recure=/foo/ simplee=/bar/ erecur=// esimple=//\n");
+
 1;
index 36ab1f45f39683f5522fadc7e6d4c9b1f29ff67f..15096b0b75500d0741139ca57bf1671e19b3fd13 100644 (file)
@@ -1197,7 +1197,7 @@ do_variable_definition (const floc *flocp, const char *varname,
          The value is set IFF the variable is not defined yet. */
       v = lookup_variable (varname, strlen (varname));
       if (v)
-        return v->special ? set_special_var (v) : v;
+        goto done;
 
       conditional = 1;
       flavor = f_recursive;
@@ -1253,15 +1253,29 @@ do_variable_definition (const floc *flocp, const char *varname,
                  buffer if we're looking at a target-specific variable.  */
               val = tp = allocated_variable_expand (val);
 
-            oldlen = strlen (v->value);
+            /* If the new value is empty, nothing to do.  */
             vallen = strlen (val);
+            if (!vallen)
+              {
+                alloc_value = tp;
+                goto done;
+              }
+
+            oldlen = strlen (v->value);
             p = alloc_value = xmalloc (oldlen + 1 + vallen + 1);
-            memcpy (alloc_value, v->value, oldlen);
-            alloc_value[oldlen] = ' ';
-            memcpy (&alloc_value[oldlen + 1], val, vallen + 1);
+
+            if (oldlen)
+              {
+                memcpy (alloc_value, v->value, oldlen);
+                alloc_value[oldlen] = ' ';
+                ++oldlen;
+              }
+
+            memcpy (&alloc_value[oldlen], val, vallen + 1);
 
             free (tp);
           }
+        break;
       }
     }
 
@@ -1405,8 +1419,8 @@ do_variable_definition (const floc *flocp, const char *varname,
   v->append = append;
   v->conditional = conditional;
 
+ done:
   free (alloc_value);
-
   return v->special ? set_special_var (v) : v;
 }
 \f