help-make
[Top][All Lists]
Advanced

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

subdir recursion with generic target


From: thomas fossati
Subject: subdir recursion with generic target
Date: Mon, 5 May 2008 18:34:33 +0200

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.




reply via email to

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