[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: subdir recursion with generic target
From: |
thomas fossati |
Subject: |
Re: subdir recursion with generic target |
Date: |
Thu, 29 May 2008 15:43:36 +0200 |
Hello, still working on the same topic :)
I've another solution which is a little bit simpler, works with old
releases (3.79, 3.80) too, and is functionally equivalent to the
previous:
=-=-=[subdir.mk]-=-=-=
.DEFAULT_GOAL := all
MAKECMDGOALS ?= $(.DEFAULT_GOAL)
# all targets (plain and hooks)
G = $(foreach T,$(MAKECMDGOALS),$(T)-pre $(addsuffix .$(T),$(SUBDIR)) $
(T)-post)
# the cartesian product between MAKECMDGOALS and SUBDIR sets
G_PLAIN = $(filter-out %-pre %-post, $(G))
# hook'd MAKECMDGOALS
G_HOOK = $(filter %-pre %-post, $(G))
.PHONY: $(G)
$(MAKECMDGOALS): $(G)
# empty hooks (caller may override them)
$(G_HOOK):
# plain hooks (e.g. a_subdir.all, another_subdir.depend, ...)
$(G_PLAIN):
@$(MAKE) -C $(basename $@) $(patsubst .%,%,$(suffix $@))
=-=-=-=-=-=
bye, t.
On May 5, 2008, at 6:34 PM, thomas fossati wrote:
Hello all,
I've been working on a template to handle subdirectory recursion for
a generic set of targets:
=-=-=[subdir.mk]-=-=-=
.DEFAULT_GOAL = all
MAKECMDGOALS ?= $(.DEFAULT_GOAL)
.PHONY: subdirs
# the following implies each generated template
subdirs: $(MAKECMDGOALS)
# subdir_mk <$1> <$2>
# $1 = goal
# $2 = subdir list
define subdir_mk
$(1)_SUBGOAL = $(addsuffix .$(1),$(2))
$(1): $(1)-pre $$($(1)_SUBGOAL) $(1)-post
$(1)-pre $(1)-post:
$$($(1)_SUBGOAL) : %.$(1): ; @$(MAKE) -C $$* $(1)
.PHONY: $(1) $$($(1)_SUBGOAL) $(1)-pre $(1)-post
endef
# create rules using the subdir_mk template for each supplied target
$(foreach T,$(MAKECMDGOALS),$(eval $(call subdir_mk,$(T),$(SUBDIR))))
=-=-=-=-=-=
The idea is to set the SUBDIR variable in the "parent" Makefile to
the list of directories in which we need to recur, then include the
subdir.mk and let it do the job of correct goal dispatching.
Also, for each goal, a "free" -pre/-post targets pair is left to the
user to wrap the macro operation, e.g.:
=-=-=[Makefile]-=-=-=
SUBDIR = a b
include subdir.mk
install-pre: ; create_dest_dirs
uninstall-post: ; remove_dest_dirs
=-=-=-=-=-=
So, a "make clean depend build install" will result into the
following:
make -C a clean
make -C b clean
make -C a depend
make -C b depend
make -C a build
make -C b build
create_dest_dirs
make -C a install
make -C b install
while "make uninstall" gives:
make -C a uninstall
make -C b uninstall
remove_dest_dirs
The way to handle subdir recursion dependencies is goal specific and
can't be generalized. I.e. if directory 'b' is a precondition to
'a''s build, we have to specify the intended goal this way:
a.all: b.all
I've done some testing and everything seems fine to me, but the eye
of a guru would be much appreciated :)
Thanks, t.