help-make
[Top][All Lists]
Advanced

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

Re: only run submake if that submake needs to be run?


From: Tzafrir Cohen
Subject: Re: only run submake if that submake needs to be run?
Date: Tue, 31 Aug 2004 16:25:40 +0300
User-agent: Mutt/1.5.6i

On Fri, Aug 20, 2004 at 02:33:21PM -0400, David Boyce wrote:
> At 01:53 PM 8/20/2004, David Wuertele wrote:
> >The reason I chose recursive in the first place was for a couple of
> >reasons:
> >
> >1.  I want to be able to run make in the sub-directory and have it
> >    work
> >
> >2.  I want to use the same variable namespace for all the makefiles.
> >    For example, I don't want one of my users to have to create
> >    variable names like:
> >
> >      my_program_OBJ := this.o that.o
> >
> >    I want them to be able to write:
> >
> >      OBJ := this.o that.o
> >
> >    This is an oversimplified example for the purposes of making the
> >    point.  I want them to be able to copy their neighbor's Makefile
> >    and not have to change all the variable names.
> >
> >Is there a way to write makefiles in a non-recursive fashion that will
> >enable me to acheive both goals?
> 
> I've had success with a circular-include design. E.g. there's a Makefile 
> per directory, just like in the recursive model. Each Makefile includes the 
> "base makefile", and the base makefile in turn includes all subtree 
> Makefiles (this takes a bit of macro/ifdef logic to prevent infinite 
> include loops). The net result is a single logical Makefile accessible via 
> a different physical "entry point" in each subdir.
> 
> This preserves two of the major benefits of the recursive model: (1) the 
> user can simply type "make" in any directory (as opposed to "make -f 
> ../../../Makefile") and (2) a developer adding a new source file need only 
> modify the Makefile in the same dir, which usually is just a list of object 
> files, without having to delve into the arcanities of the full build system.

Can you give an example?

Is it possible to avoid writing the relative subdir path in each
subdir's makefile?

I thought of something like:

Parent GNUmakefile:

  # initialize
  PACK_FILES:=
  reset:=0

  include include.mk
  include curdir.mk
  include include.mk

include.mk:

  ifeq(0,$(reset))
    # Used when first including include.mk
    SUBDIRS_local:=0
    C_SRC_local:=
    DOCS_local:=
    ...
    reset:=1
  else
    # used when including include.mk the second time: the common tasks
    PACK_FILES_local:=\
      $(DOCS_local) \
      $(C_SRC_local:%:%.c) $(C_SRC_local:%:%.h) \ 
      ...
    PACK_FILES+=$(PACK_FILES_local:%=$(curdir)/%)
    ...

    reset:=0
    # recursively include subdirs
    ifneq(,$(SUBDIRS_local)
      # e.g: if curdir=some/path and SUBDIRS_local=dir1 dir2, this will
      # run:
      # include include.mk some/path/dir1/curdir.mk include.mk \
      #         include.mk some/path/dir1/curdir.mk include.mk
      include \
        $(foreach file,$(SUBDIRS_local:%=$(curdir)/%/curdir.mk),include.mk 
$(file) include.mk)
    endif
  endif
  
curdir.mk (toplevel):
  subdir:=
  
  SUBDIRS_local:=dir1 dir2
  DOCS_local:=README ChangeLog

dir1/curdir.mk:
  subdir:=dir1

  C_SRC_local:=module1 mod2

But I could not get rid of the need to assing the relative path in each
curdir.mk

You can replace curdir.mk with a local GNUmakefile with minor changes, I
figure. Maybe by resetting all the globals beforehand and restarting an
include loop.

-- 
Tzafrir Cohen                       +---------------------------+
http://www.technion.ac.il/~tzafrir/ |vim is a mutt's best friend|
mailto:address@hidden       +---------------------------+




reply via email to

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