[Top][All Lists]

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

sources in a deep hierarchy, objects flat

From: Frink, Alexander
Subject: sources in a deep hierarchy, objects flat
Date: Fri, 29 Sep 2006 16:35:30 +0200
User-agent: Internet Messaging Program (IMP) 3.2.3


I have my sources in a deep hierarchy and want to compile them into a flat
directory. The module name is unique in the deep hierarchy, so there are no
object name clashes.
Assume I have the following sources


and want the objects to be


and do not want to create and maintain a Makefile in each source subdirectory,
so my only, single Makefile is ./Makefile

I cannot use pattern rules, but I can easily derive the object name from the
source name (replace .c with .o, strip the directory part and add a prefix
obj/flat/). Therefore I tried to use "foreach", "eval" and canned command
sequences to dynamically generate the dependencies and compile steps using
something like the following Makefile

------------ BEGIN MAKEFILE -----------------
SOURCES := $(shell find src -type f -name "*.c")


define compile_rule
SOURCE := $(1)
TARGET := $(addprefix obj/flat/, \
          $(notdir \
          $(patsubst %.c,%.o, \
all: $(TARGET)
        echo compiling $(SOURCE) to $(TARGET)
        touch $(TARGET)

$(info foreach-loop)

$(foreach source,$(SOURCES),$(eval $(call compile_rule,$(source))))
------------ END MAKEFILE -----------------
(In front of the echo and touch lines I use Tabs which get lost due to copy and

I know that the above Makefile is wrong and I surely need some escaping. In this
form it should be easiest to understand what I wanted to achieve. I tried
various combinations, replacing := by =, using $$ instead of $, but now I think
I got lost with deferred and immediate evaluation, double evaluation by eval

Using make 3.81, the current result is

SOURCES=src/some/deep/structure/a/1.c src/some/deep/structure/a/2.c
echo compiling src/some/deep/structure/a/2.c to obj/flat/1.o
compiling src/some/deep/structure/a/2.c to obj/flat/1.o
touch obj/flat/1.o

Only one file is compiled, and $(SOURCE) and $(TARGET) are out of sync
(2.c/1.o). Running it again gives "Nothing to be done for `all'."

Could anybody please try to explain why and where to escape $ or give a
completely different solution (my other solution would be to write a Perl
script which generates the Makefile, but I prefer to use Make syntax only).


Alexander Frink

Debeka Hauptverwaltung
Abteilung IS/Q
Ferdinand-Sauerbruch-Str. 18
56058 Koblenz

Telefon  (0261) 498-1455
Telefax  (0261) 498-1496

E-Mail   address@hidden

reply via email to

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