>From d8662c978354043708dd82bd73ff477d602071d6 Mon Sep 17 00:00:00 2001 From: Peter Rosin Date: Fri, 25 Jan 2013 16:56:16 +0100 Subject: [PATCH] curdir: add support for relative names in included fragments The rationale for this change is that it is annoying to have to repeat the directory name when including a Makefile fragment. For deep directory structures these repeats can generate a lot of bloat. It also hinders reuse and easy directory restructuring if all Makefile fragments have to know exactly where they live. Suggested by Bob Friesenhahn, and later discussed in bug #13524. automake.in (read_am_file): Add third argument specifying the relative directory of this Makefile fragment compared to the main Makefile. Replace &{CURDIR}& and &{CANON_CURDIR}& in the fragment with this relative directory (with slashes etc, or canonicalized). (read_main_am_file): Adjust. t/curdir.sh: New test. t/list-of-tests.mk: Augment. --- automake.in | 26 +++++++++-- t/curdir.sh | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++++ t/list-of-tests.mk | 1 + 3 files changed, 137 insertions(+), 5 deletions(-) create mode 100755 t/curdir.sh diff --git a/automake.in b/automake.in index 0e3b882..6018489 100644 --- a/automake.in +++ b/automake.in @@ -6330,15 +6330,15 @@ sub check_trailing_slash ($\$) } -# &read_am_file ($AMFILE, $WHERE) -# ------------------------------- +# &read_am_file ($AMFILE, $WHERE, $CURDIR) +# ---------------------------------------- # Read Makefile.am and set up %contents. Simultaneously copy lines # from Makefile.am into $output_trailer, or define variables as # appropriate. NOTE we put rules in the trailer section. We want # user rules to come after our generated stuff. sub read_am_file ($$) { - my ($amfile, $where) = @_; + my ($amfile, $where, $curdir) = @_; my $am_file = new Automake::XFile ("< $amfile"); verb "reading $amfile"; @@ -6423,6 +6423,18 @@ sub read_am_file ($$) my $new_saw_bk = check_trailing_slash ($where, $_); + my $cur_dir = &canonicalize ($curdir); + if ($curdir eq '.') + { + # If present, eat the following '_' or '/', converting + # "&{CURDIR}&/foo" and "&{CANON_CURDIR}&_foo" into plain "foo" + # when $curdir is '.'. + $_ =~ s,&\{CURDIR\}&/,,g; + $_ =~ s,&\{CANON_CURDIR\}&_,,g; + } + $_ =~ s/&\{CURDIR\}&/${curdir}/g; + $_ =~ s/&\{CANON_CURDIR\}&/${cur_dir}/g; + if (/$IGNORE_PATTERN/o) { # Merely delete comments beginning with two hashes. @@ -6559,6 +6571,7 @@ sub read_am_file ($$) elsif (/$INCLUDE_PATTERN/o) { my $path = $1; + my $new_curdir; if ($path =~ s/^\$\(top_srcdir\)\///) { @@ -6571,6 +6584,7 @@ sub read_am_file ($$) # the dependency. # (subdircond2.test and subdircond3.test will fail.) push_dist_common ("\$\(top_srcdir\)/$path"); + $new_curdir = File::Spec->abs2rel ($path, $relative_dir); } else { @@ -6583,9 +6597,11 @@ sub read_am_file ($$) # (subdircond2.test and subdircond3.test will fail.) push_dist_common ("\$\(srcdir\)/$path"); $path = $relative_dir . "/" . $path if $relative_dir ne '.'; + $new_curdir = $path; } + $new_curdir = '.' if $new_curdir !~ s,/[^/]*$,,; $where->push_context ("'$path' included from here"); - &read_am_file ($path, $where); + &read_am_file ($path, $where, $new_curdir); $where->pop_context; } else @@ -6658,7 +6674,7 @@ sub read_main_am_file ($$) &define_standard_variables; # Read user file, which might override some of our values. - &read_am_file ($amfile, new Automake::Location); + &read_am_file ($amfile, new Automake::Location, '.'); } diff --git a/t/curdir.sh b/t/curdir.sh new file mode 100755 index 0000000..69b82b0 --- /dev/null +++ b/t/curdir.sh @@ -0,0 +1,115 @@ +#! /bin/sh +# Copyright (C) 2013 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 . + +# Test &{CURDIR}& and &{CANON_CURDIR}&. + +. test-init.sh + +cat >> configure.ac << 'END' +AC_PROG_CC +AM_PROG_CC_C_O +AC_CONFIG_FILES([zot/Makefile]) +AC_OUTPUT +END + +mkdir foo +mkdir foo/bar +mkdir foo/foobar +mkdir zot + +cat > Makefile.am << 'END' +AUTOMAKE_OPTIONS = subdir-objects +bin_PROGRAMS = +include $(top_srcdir)/foo/local.mk +include $(srcdir)/foo/foobar/local.mk +include local.mk +END + +cat > zot/Makefile.am << 'END' +AUTOMAKE_OPTIONS = subdir-objects +bin_PROGRAMS = +include $(top_srcdir)/zot/local.mk +include $(top_srcdir)/top.mk +END + +cat > local.mk << 'END' +&{CANON_CURDIR}&_whoami: + @echo "I am &{CURDIR}&/local.mk" + +bin_PROGRAMS += &{CURDIR}&/mumble +&{CANON_CURDIR}&_mumble_SOURCES = &{CURDIR}&/one.c +END + +cat > top.mk << 'END' +&{CANON_CURDIR}&_top_whoami: + @echo "I am &{CURDIR}&/top.mk" + +bin_PROGRAMS += &{CURDIR}&/scream +&{CANON_CURDIR}&_scream_SOURCES = &{CURDIR}&/two.c +END + +cat > one.c << 'END' +int main(void) { return 0; } +END + +cp local.mk foo +cp local.mk foo/bar +cp local.mk foo/foobar +cp local.mk zot +echo "include &{CURDIR}&/bar/local.mk" >> foo/local.mk + +cp one.c foo +cp one.c foo/bar +cp one.c foo/foobar +cp one.c zot +cp one.c two.c + +$ACLOCAL +$AUTOCONF +$AUTOMAKE -a +./configure + +$MAKE whoami >output 2>&1 || { cat output; exit 1; } +cat output +grep "I am local.mk" output +$MAKE foo_whoami >output 2>&1 || { cat output; exit 1; } +cat output +grep "I am foo/local.mk" output +$MAKE foo_bar_whoami >output 2>&1 || { cat output; exit 1; } +cat output +grep "I am foo/bar/local.mk" output +$MAKE foo_foobar_whoami >output 2>&1 || { cat output; exit 1; } +cat output +grep "I am foo/foobar/local.mk" output + +$MAKE +./mumble +foo/mumble +foo/bar/mumble +foo/foobar/mumble + +cd zot + +$MAKE ___top_whoami >output 2>&1 || { cat output; exit 1; } +cat output +grep "I am ../top.mk" output +$MAKE whoami >output 2>&1 || { cat output; exit 1; } +cat output +grep "I am local.mk" output + +$MAKE +./mumble +../scream diff --git a/t/list-of-tests.mk b/t/list-of-tests.mk index 44f598e..1121735 100644 --- a/t/list-of-tests.mk +++ b/t/list-of-tests.mk @@ -340,6 +340,7 @@ t/cscope.tap \ t/cscope2.sh \ t/cscope3.sh \ t/c-demo.sh \ +t/curdir.sh \ t/cxx.sh \ t/cxx2.sh \ t/cxxcpp.sh \ -- 1.7.9