Makefile : Build in a separate directory tree
My project (an interpreted language) has a standard library composed by multiple files, each of them will be built into an .so dynamic library that the interpreter will load upon user request (with an import directive). Each source file is located into a subdirectory representing its "namespace", for instance :
The build process has to create a "build" directory, then when each file is compiling has to create its namespace directory inside the "build" one, for instance, when compiling
std/io/network/tcp.cc
he run an mkdir command with
mkdir -p build/std/io/network
The Makefile snippet is :
STDSRC=stdlib/std/hashing/md5.cc \
stdlib/std/hashing/crc32.cc \
stdlib/std/hashing/sha1.cc \
stdlib/std/hashing/sha2.cc \
stdlib/std/io/network/http.cc \
stdlib/std/io/network/tcp.cc \
stdlib/std/io/network/smtp.cc \
stdlib/std/io/file.cc \
stdlib/std/io/console.cc \
stdlib/std/io/xml.cc \
stdlib/std/type/reflection.cc \
stdlib/std/type/string.cc \
stdlib/std/type/matrix.cc \
stdlib/std/type/array.cc \
stdlib/std/type/map.cc \
stdlib/std/type/type.cc \
stdlib/std/type/binary.cc \
stdlib/std/encoding.cc \
stdlib/std/os/dll.cc \
stdlib/std/os/time.cc \
stdlib/std/os/threads.cc \
stdlib/std/os/process.cc \
stdlib/std/pcre.cc \
stdlib/std/math.cc
STDOBJ=$(STDSRC:.cc=.so)
all: stdlib
stdlib: $(STDOBJ)
.cc.so:
mkdir -p `dirname $< | sed -e 's/stdlib/stdlib\/build/'`
$(CXX) $< -o `dirname $< | sed -e 's/stdlib/stdlib\/build/'`/`basename $< .cc`.so $(CFLAGS) $(LDFLAGS)
I have two questions :
1 - The problem is that the make command, i really don't know why, doesn't check if a file was modified and launch the build process on ALL the files no matter what, so if i need to build only on开发者_开发百科e file, i have to build them all or use the command :
make path/to/single/file.so
Is there any way to solve this?
2 - Any way to do this in a "cleaner" way without have to distribute all the build directories with sources?
Thanks
For 1) the problem is that the target of your rule (stdlib/something.so) is not exactly what the rule makes (build/something.so), so Make always thinks it must make the target. This should fix it (I am using GNUMake):
STDOBJ=$(patsubst stdlib%.cc,build%.so, $(STDSRC))
all: stdlib
stdlib: $(STDOBJ)
build/%.so: stdlib/%.cc
mkdir -p $(dir $@)
$(CXX) $< -o $@ $(CFLAGS) $(LDFLAGS)
For 2) I'm not sure what you mean. If you want the build directory structure you describe, this will do it.
精彩评论