help-make
[Top][All Lists]
Advanced

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

Re: Traversal Makefile?


From: Nick Patavalis
Subject: Re: Traversal Makefile?
Date: Sun, 5 Feb 2006 20:19:10 +0200
User-agent: Mutt/1.5.9i

On Sat, Feb 04, 2006 at 01:42:36PM -0800, Ken Klein wrote:
> Hi.  I'm new to makefiles.  I'm trying to write one
> to do this: 
> 
> 1.  Starting at a Top Directory, I want to traverse
> all subdirectories that are called UnitTest and
> execute the makefile in each one.  This will cause g++
> to run and create .o files in those subdirectories (I
> already have the makefiles to run from each
> directory). 

For this you'd need something along these lines: ::

  dirs := $(shell find ./ -type d -name "UnitTest")
  srcs := $(foreach d,$(dirs),$(wildcard $(d)/*.cpp))
  objs := $(srcs:.cpp=.o)

  .PHONY: traverse
  traverse: $(dirs)
          for d in $+; do \
              $(MAKE) -C "$$d"; \
          done

> 2. Do a link using all the .o files in the same
> traversal of subdirectories plus a file called
> testsute.o in the Top Directory (testsuite.o has the
> main( ) in it). 

This is now obvious: ::

  $(objs) : traverse

  testsuite : testsuite.o $(objs)
          g++ -o $@ $+

I have not tested the fragments above in a real makefile, so they
might contain bugs (hopefully trivial). If you have strange file names
(like file names with spaces in them and/or special characters) then
quoting must receive much more scrutiny. As given, the examples assume
that all names are "sane".

The assumption is made that every ".cpp" file inside every UnitTest
directory will be used to produce one, and only one, ".o" file, and
that the produced ".o" files will have names that differ from the
corresponding source files only in the suffix. These are not
unreasonable assumptions. They are, however, a quite liberal
interpretation of what you specified. If you really want what you said
(that is, link all ".o" files that happen to be present in the
UnitTest directories at the end of the traversal, without making
assumptions as to where and how they got there), then the Makefile
will get still uglier (and it is quite ugly to begin with...). For
this I see no other way but to make "testsuite" a phony target, and to
locate the ".o" files from inside the "testsuite"-rule command-set
using some scripting-fu.

Now that I told you how to do what you asked, I have to tell you *NOT
TO*. A far better approach would be to *avoid using Make
recursively*. This is always a good advice: If you can avoid using
make recursively, do so. If you think you can't, think again. Only
when everything else has failed, only then, resort to Makefiles
calling other Makefiles. Arguments about this have been expressed very
convincingly in [Miller]_. In your case, the toplevel makefile should
*include* the low-level makefiles, and get the lists of objects and
sources from them.

Hope this helps
/npat

References:

.. [miller] Peter Miller, `Recursive Make Considered Harmful`__

   .. __: http://www.pcug.org.au/
               ~millerp/rmch/recu-make-cons-harm.html

-- 
Structure is nothing if it is all you got. Skeletons spook people if
they try to walk around on their own. I really wonder why XML does
not.
  -- Erik Naggum




reply via email to

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