bug-automake
[Top][All Lists]
Advanced

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

bug#7610: [PATCH] automake: support embedded \# in variable appends


From: Mike Frysinger
Subject: bug#7610: [PATCH] automake: support embedded \# in variable appends
Date: Sun, 20 Feb 2022 13:35:11 -0500

Fixes automake bug https://bugs.gnu.org/7610.

Use of \# is not portable.  POSIX does not provide any way of retaining
the # marker in variables.  There is wide spread support for \# though
in GNU & BSD Make implementations.

Today, with plain variable assignments, Automake leaves the line alone:
  foo = blah\#blah
This will leave it to the implementation to decide what to do.  But if
you try to append to it, Automake follows POSIX and strips it:
  foo = blah\#blah
  foo += what
  -> foo = blah\ what

Instead, let's issue a portability warning whenever \# is used, even if
it isn't being appended, and do not strip the \# when appending.  Now:
  foo = blah\#blah
  foo += what
  -> warning: escaping \# comment markers is not portable
  -> foo = blah\#blah what
---
 NEWS                         |  3 +++
 lib/Automake/VarDef.pm       |  2 +-
 lib/Automake/Variable.pm     |  3 +++
 t/comments-escaped-in-var.sh | 47 ++++++++++++++++++++++++++++++++++++
 t/list-of-tests.mk           |  1 +
 5 files changed, 55 insertions(+), 1 deletion(-)
 create mode 100644 t/comments-escaped-in-var.sh

diff --git a/NEWS b/NEWS
index fbb3f758ce52..6e04ee123ea0 100644
--- a/NEWS
+++ b/NEWS
@@ -26,6 +26,9 @@ New in 1.17:
     drop support for them has been reversed.  The ACCEPT_INFERIOR_RM_PROGRAM
     setting no longer exists.
 
+  - Variables using escaped \# will trigger portability warnings, but be
+    retained when appended.  GNU Make & BSD Makes are known to support it.
+
 * Obsolescent features:
 
   - py-compile no longer supports Python 0.x or 1.x versions.  Python 2.0,
diff --git a/lib/Automake/VarDef.pm b/lib/Automake/VarDef.pm
index 59af4036b35f..599b57fe85d6 100644
--- a/lib/Automake/VarDef.pm
+++ b/lib/Automake/VarDef.pm
@@ -188,7 +188,7 @@ sub append ($$$)
   #   VAR = foo # com bar
   # Furthermore keeping '#' would not be portable if the variable is
   # output on multiple lines.
-  $val =~ s/ ?#.*//;
+  $val =~ s/ ?[^\\]#.*//;
   # Insert a separator, if required.
   $val .= ' ' if $val;
   $self->{'value'} = $val . $value;
diff --git a/lib/Automake/Variable.pm b/lib/Automake/Variable.pm
index 4f6a88d61c5c..1269d5d9f40e 100644
--- a/lib/Automake/Variable.pm
+++ b/lib/Automake/Variable.pm
@@ -857,6 +857,9 @@ sub define ($$$$$$$$)
   msg ('portability', $where, "':='-style assignments are not portable")
     if $type eq ':';
 
+  msg ('portability', $where, "escaping \\# comment markers is not portable")
+    if index ($value, '\#') != -1;
+
   check_variable_expansions ($value, $where);
 
   # If there's a comment, make sure it is \n-terminated.
diff --git a/t/comments-escaped-in-var.sh b/t/comments-escaped-in-var.sh
new file mode 100644
index 000000000000..e06c3f359b68
--- /dev/null
+++ b/t/comments-escaped-in-var.sh
@@ -0,0 +1,47 @@
+#! /bin/sh
+# Copyright (C) 2022 Free Software Foundation, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2, or (at your option)
+# any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+# Make sure Automake preserves escaped comments in the output.
+
+. test-init.sh
+
+cat >> configure.ac <<'END'
+AC_OUTPUT
+END
+
+cat > Makefile.am << 'END'
+# This value should be preserved.
+var = -DVAL='"\#xxx\#"'   # this should be stripped
+var += -DX=1  # this should be kept
+END
+
+$ACLOCAL
+
+# This should fail due to -Werror.
+$AUTOMAKE && exit 1
+
+# This should pass though.
+$AUTOMAKE -Wno-portability
+
+# For debugging.
+grep ^var Makefile.in
+
+# The full flag should be retained.
+grep '^var.*\\#xxx\\#.*DX=1' Makefile.in
+
+# Only the 2nd comment should be retained.
+grep '^var.*stripped' Makefile.in && exit 1 || :
+grep '^var.*should be kept' Makefile.in
diff --git a/t/list-of-tests.mk b/t/list-of-tests.mk
index 1601154e031e..d8e50b080166 100644
--- a/t/list-of-tests.mk
+++ b/t/list-of-tests.mk
@@ -266,6 +266,7 @@ t/comment9.sh \
 t/commen10.sh \
 t/commen11.sh \
 t/comment-block.sh \
+t/comments-escaped-in-var.sh \
 t/comments-in-var-def.sh \
 t/compile.sh \
 t/compile2.sh \
-- 
2.34.1






reply via email to

[Prev in Thread] Current Thread [Next in Thread]