From c108be85bf27d40d9b1c45d4de14185dc902b763 Mon Sep 17 00:00:00 2001
From: Björn Persson <Bjorn@Rombobjörn.se>
Date: Sat, 9 Jan 2016 18:13:54 +0100
Subject: fixes to sub-Make situations
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

· Made preprocessing work even when build_GPRs is empty.
· Improved the quoting in delegating makefiles.
· Explained delegation_command better.
---
 comfignat.mk                            | 35 +++++++++++++++++++++++++--------
 testsuite/sources/empty/generated_files |  1 +
 2 files changed, 28 insertions(+), 8 deletions(-)

diff --git a/comfignat.mk b/comfignat.mk
index f9f2038..a9847a8 100644
--- a/comfignat.mk
+++ b/comfignat.mk
@@ -1,5 +1,5 @@
 # Comfignat makefile foundation for configuring and building GNAT projects
-# Copyright 2013 - 2015 B. Persson, Bjorn@Rombobeorn.se
+# Copyright 2013 - 2016 B. Persson, Bjorn@Rombobeorn.se
 #
 # This material is provided as is, with absolutely no warranty expressed
 # or implied. Any use is at your own risk.
@@ -212,6 +212,16 @@ srcdir := ${abspath ${dir ${lastword ${MAKEFILE_LIST}}}}
 # as the directory part of the last pathname in MAKEFILE_LIST – which is this
 # file since there is no include directive above this point.
 
+srcsubdir := ${abspath ${dir ${firstword ${MAKEFILE_LIST}}}}
+# Unlike other directory variables, srcsubdir varies between Make invocations
+# when there are several Comfignat-using makefiles in subdirectories in the
+# source tree. Its value is the directory of the makefile that the current Make
+# process was invoked with. This will often be the same as CURDIR, but not for
+# the main makefile when it's invoked from a delegating makefile in a separate
+# build directory, as CURDIR is then the build directory.
+# (dir must be called before abspath in case a parent directory name contains
+# spaces, because dir operates on a space-separated list.)
+
 stage_bindir         = ${stagedir}${bindir}
 stage_libexecdir     = ${stagedir}${libexecdir}
 stage_datadir        = ${stagedir}${datadir}
@@ -232,6 +242,7 @@ stage_miscdocdir     = ${stagedir}${miscdocdir}
 # written during the build.
 
 Make_srcdir         = ${call Make_pathname,srcdir}
+Make_srcsubdir      = ${call Make_pathname,srcsubdir}
 Make_builddir       = ${call Make_pathname,builddir}
 Make_objdir         = ${call Make_pathname,objdir}
 Make_stagedir       = ${call Make_pathname,stagedir}
@@ -350,7 +361,7 @@ endif
 ifneq (${origin preprocessed_files},file)
    preprocessed_files = \
       ${filter-out ${notdir ${usage_GPRs}}, \
-                   ${basename ${notdir ${wildcard ${Make_srcdir}/*.gp}}}}
+                   ${basename ${notdir ${wildcard ${Make_srcsubdir}/*.gp}}}}
 endif
 # preprocessed_files is a list of files to be produced in the preprocessing
 # step at the beginning of the build. Containing makefiles may override it or
@@ -574,12 +585,20 @@ options_building = ${addprefix -X,${option_values}}
 # Some other data that the rules below need:
 #
 
-main_makefile := ${firstword ${MAKEFILE_LIST}}
-delegation_command = @$${MAKE} --file=${abspath ${main_makefile}} \
-                     --include-dir=${abspath ${dir ${main_makefile}}}
+delegation_command = @\$${MAKE} \
+                     '--file=${abspath ${firstword ${MAKEFILE_LIST}}}' \
+                     '--include-dir=${srcsubdir}'
 # delegation_command is the Make command line that delegating makefiles in
 # separate build directories use to delegate commands to the main makefile. The
 # first pathname in MAKEFILE_LIST is the main makefile.
+# The --file parameter remembers the main makefile that was used to write the
+# delegating makefile.
+# The --include-dir parameter allows the main makefile to find comfignat.mk
+# relative to its own location.
+# The working directory is not changed, so that any relative pathnames passed
+# in will be resolved relative to the build directory. (Note that sub-Makes in
+# subdirectories can still change the working directory).
+
 
 build_targets = ${addsuffix .phony_target,${build_GPRs}}
 # A phony target is defined for each build project, and the job of determining
@@ -613,8 +632,8 @@ ${Make_gprdir}/:
 # How to initialize a build directory with a delegating makefile:
 ${Make_builddir}/Makefile: | ${Make_builddir}/
 	@echo 'Writing $@.'
-	@( echo 'Comfignat_default_goal: force ; ${delegation_command}'; \
-	   echo '%: force ; ${delegation_command} $$@'; \
+	@( echo "Comfignat_default_goal: force ; ${delegation_command}"; \
+	   echo "%: force ; ${delegation_command} "'$$@'; \
 	   echo 'force: ;'; \
 	   echo 'Makefile: ;' \
 	 ) > $@
@@ -698,7 +717,7 @@ preprocess: $${preprocessed_files_in_builddir}
 # done before any project is built.
 
 .PHONY: base
-base: $${build_targets}
+base: $${build_targets} preprocess
 # This builds the projects listed in build_GPRs, plus any additional
 # prerequisites that the containing makefile might add.
 
diff --git a/testsuite/sources/empty/generated_files b/testsuite/sources/empty/generated_files
index e69de29..ea63505 100644
--- a/testsuite/sources/empty/generated_files
+++ b/testsuite/sources/empty/generated_files
@@ -0,0 +1 @@
+comfignat.gpr
-- 
cgit v1.2.3