Subject: Re: bug#49317: dist: depends on $(BUILT_SOURCES), but has no reason to

From: Allison Karlitskaya <[email-masked]>
Date: Fri, 2 Jul 2021 14:56:19 +0200

[PATCH] dist: add new "pure-dist" automake option

Since v1.15.1-204-gac47c22e3, "make dist" has been depending on
$(BUILT_SOURCES) to avoid problems when building some GNU packages which
need to compile themselves as part of building their tarballs (for
example, to generate manpage content from --help output).  This default
behaviour might be "too much" for other projects, though, so add a new
option "pure-dist" to disable it.

* NEWS:
* bin/automake.in:
* doc/automake.texi: Add a new option "pure-dist" to disable the
dependency of distdir: on $(BUILT_SOURCES).
* t/pure-dist.sh:
* t/impure-dist.sh: Add a pair of tests for a similar scenario with and
  without the option.

Fixes automake bug https://debbugs.gnu.org/49317
---
 NEWS                    |  3 ++
 bin/automake.in         |  3 +-
 doc/automake.texi       |  7 +++++
 lib/Automake/Options.pm |  1 +
 lib/am/distdir.am       |  5 ++++
 t/impure-dist.sh        |  1 +
 t/pure-dist.sh          | 66 +++++++++++++++++++++++++++++++++++++++++
 7 files changed, 85 insertions(+), 1 deletion(-)
 create mode 120000 t/impure-dist.sh
 create mode 100644 t/pure-dist.sh
From 1f46cc63475b8af1a52e84ae329bb4c00ce291e3 Mon Sep 17 00:00:00 2001
From: Allison Karlitskaya <[email-masked]>
Date: Fri, 2 Jul 2021 14:37:06 +0200
Subject: [PATCH] dist: add new "pure-dist" automake option

Since v1.15.1-204-gac47c22e3, "make dist" has been depending on
$(BUILT_SOURCES) to avoid problems when building some GNU packages which
need to compile themselves as part of building their tarballs (for
example, to generate manpage content from --help output).  This default
behaviour might be "too much" for other projects, though, so add a new
option "pure-dist" to disable it.

* NEWS:
* bin/automake.in:
* doc/automake.texi: Add a new option "pure-dist" to disable the
dependency of distdir: on $(BUILT_SOURCES).
* t/pure-dist.sh:
* t/impure-dist.sh: Add a pair of tests for a similar scenario with and
  without the option.

Fixes automake bug https://debbugs.gnu.org/49317
---
 NEWS                    |  3 ++
 bin/automake.in         |  3 +-
 doc/automake.texi       |  7 +++++
 lib/Automake/Options.pm |  1 +
 lib/am/distdir.am       |  5 ++++
 t/impure-dist.sh        |  1 +
 t/pure-dist.sh          | 66 +++++++++++++++++++++++++++++++++++++++++
 7 files changed, 85 insertions(+), 1 deletion(-)
 create mode 120000 t/impure-dist.sh
 create mode 100644 t/pure-dist.sh

diff --git a/NEWS b/NEWS
index 5e92387e2..71602e768 100644
--- a/NEWS
+++ b/NEWS
@@ -14,6 +14,9 @@ New in ?.?.?:
   --with-python_prefix and --with-python_exec_prefix supported,
   to specify explicitly.
 
+  - the new option "pure-dist" skips generating $(BUILT_SOURCES) before
+  building the tarball as part of "make dist"
+
 * Bugs fixed
 
   - automake output reproducible.
diff --git a/bin/automake.in b/bin/automake.in
index 19ea9538f..bb18790a0 100644
--- a/bin/automake.in
+++ b/bin/automake.in
@@ -3900,7 +3900,8 @@ sub handle_dist ()
   $output_rules .= file_contents ('distdir',
 				  new Automake::Location,
 				  %transform,
-				  FILENAME_FILTER => $filename_filter);
+				  FILENAME_FILTER => $filename_filter,
+				  PURE_DIST => !! option 'pure-dist');
 }

diff --git a/doc/automake.texi b/doc/automake.texi
index 351b0a1bb..c043e864d 100644
--- a/doc/automake.texi
+++ b/doc/automake.texi
@@ -10403,6 +10403,13 @@ disable automatic dependency tracking.
 Don't emit any code related to @code{dist} target.  This is useful
 when a package has its own method for making distributions.
 
+@item @option{pure-dist}
+@cindex Option, @option{pure-dist}
+@opindex pure-dist
+Don't build @code{BUILT_SOURCES} as part of @code{dist}.  This option
+can be set if building the distribution only requires the source files,
+and doesn't compile anything as a side-effect.
+
 @item @option{no-dist-gzip}
 @cindex Option, @option{no-dist-gzip}
 @opindex no-dist-gzip
diff --git a/lib/Automake/Options.pm b/lib/Automake/Options.pm
index b846fee87..37e2bb14b 100644
--- a/lib/Automake/Options.pm
+++ b/lib/Automake/Options.pm
@@ -289,6 +289,7 @@ sub _is_valid_easy_option ($)
     no-installman
     no-texinfo.tex
     nostdinc
+    pure-dist
     readme-alpha
     serial-tests
     parallel-tests
diff --git a/lib/am/distdir.am b/lib/am/distdir.am
index 774d08b91..a18892d9d 100644
--- a/lib/am/distdir.am
+++ b/lib/am/distdir.am
@@ -75,8 +75,13 @@ if %?SUBDIRS%
 AM_RECURSIVE_TARGETS += distdir distdir-am
 endif %?SUBDIRS%
 
+if %?PURE_DIST%
+distdir:
+	$(MAKE) $(AM_MAKEFLAGS) distdir-am
+else !%?PURE_DIST%
 distdir: $(BUILT_SOURCES)
 	$(MAKE) $(AM_MAKEFLAGS) distdir-am
+endif !%?PURE_DIST%
 
 distdir-am: $(DISTFILES)
 ##
diff --git a/t/impure-dist.sh b/t/impure-dist.sh
new file mode 120000
index 000000000..0f8c349cd
--- /dev/null
+++ b/t/impure-dist.sh
@@ -0,0 +1 @@
+pure-dist.sh
\ No newline at end of file
diff --git a/t/pure-dist.sh b/t/pure-dist.sh
new file mode 100644
index 000000000..794f942c4
--- /dev/null
+++ b/t/pure-dist.sh
@@ -0,0 +1,66 @@
+#! /bin/sh
+# Copyright (C) 2001-2020 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/>.
+
+# Test to make sure -Werror and --add-missing work together.
+
+. test-init.sh
+
+PKG="$(basename -s.sh $0)"
+PKG_VER="${PKG}-1.0"
+
+if [ "${PKG}" = "pure-dist" ]; then
+    sed -ie 's/AM_INIT_AUTOMAKE/AM_INIT_AUTOMAKE([pure-dist])/' configure.ac
+fi
+
+cat >> configure.ac << 'END'
+AC_OUTPUT
+END
+
+cat > Makefile.am <<EOF
+BUILT_SOURCES = x.c
+EXTRA_DIST = y.c
+
+x.c:
+	touch \$@
+
+y.c:
+	cp x.c y.c # simulate 'undetectable' dependency on x.c
+EOF
+
+if [ "${PKG}" = "pure-dist" ]; then
+    touch y.c # pure dist needs to have all files already there
+else
+    : # impure will try to build y.c by the rule
+fi
+
+$ACLOCAL
+$AUTOMAKE
+$AUTOCONF
+./configure
+run_make dist
+
+# In any case, the tarball should contain y.c, but not x.c
+! tar tf "${PKG_VER}".tar.gz "${PKG_VER}"/x.c
+tar tf "${PKG_VER}".tar.gz "${PKG_VER}"/y.c
+
+# But x.c should only have been built for the "impure" version
+if [ "$(basename $0)" = "pure-dist.sh" ]; then
+    # pure build should not have generated this
+    ! test -e x.c
+else
+    # impure build should have
+    test -e x.c
+fi
-- 
2.31.1