about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEelco Dolstra <eelco.dolstra@logicblox.com>2014-09-05T12·17+0200
committerEelco Dolstra <eelco.dolstra@logicblox.com>2014-09-05T12·17+0200
commit075cfe3b97bf0e90577bb7e842ba467524fad0ef (patch)
treed6675899455abe1472c260feb0e7584ab8f9804a
parentafbdd5533448724aaf32b434fda8478ad5d80a0f (diff)
Add support for using a build directory
-rw-r--r--functions.mk2
-rw-r--r--lib.mk10
-rw-r--r--libraries.mk4
-rw-r--r--patterns.mk9
-rw-r--r--programs.mk4
5 files changed, 21 insertions, 8 deletions
diff --git a/functions.mk b/functions.mk
index 45d917399391..c48775db8c3b 100644
--- a/functions.mk
+++ b/functions.mk
@@ -10,5 +10,5 @@ filename-to-dep = $(dir $1).$(notdir $1).dep
 # empty string if not found.
 find-program = $(shell for i in $$(IFS=: ; echo $$PATH); do p=$$i/$(strip $1); if [ -e $$p ]; then echo $$p; break; fi; done)
 
-# Remove trailing slash.
+# Ensure that the given string ends in a single slash.
 add-trailing-slash = $(patsubst %/,%,$(1))/
diff --git a/lib.mk b/lib.mk
index 8b109e2ce8da..aa92d8d94304 100644
--- a/lib.mk
+++ b/lib.mk
@@ -25,6 +25,16 @@ sysconfdir ?= $(prefix)/etc
 mandir ?= $(prefix)/share/man
 
 
+# Initialise support for build directories.
+builddir ?=
+
+ifdef builddir
+  buildprefix = $(builddir)/
+else
+  buildprefix =
+endif
+
+
 # Pass -fPIC if we're building dynamic libraries.
 BUILD_SHARED_LIBS ?= 1
 
diff --git a/libraries.mk b/libraries.mk
index 3b91c699e65a..206481979370 100644
--- a/libraries.mk
+++ b/libraries.mk
@@ -45,9 +45,9 @@ endif
 #   built, otherwise a static library.
 define build-library
   $(1)_NAME ?= $(1)
-  _d := $$(strip $$($(1)_DIR))
+  _d := $(buildprefix)$$(strip $$($(1)_DIR))
   _srcs := $$(sort $$(foreach src, $$($(1)_SOURCES), $$(src)))
-  $(1)_OBJS := $$(addsuffix .o, $$(basename $$(_srcs)))
+  $(1)_OBJS := $$(addprefix $(buildprefix), $$(addsuffix .o, $$(basename $$(_srcs))))
   _libs := $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_PATH))
 
   $(1)_INSTALL_DIR ?= $$(libdir)
diff --git a/patterns.mk b/patterns.mk
index 6b2cfd017050..3219d9629fbf 100644
--- a/patterns.mk
+++ b/patterns.mk
@@ -1,8 +1,11 @@
-%.o: %.cc
+$(buildprefix)%.o: %.cc
+	@mkdir -p "$(dir $@)"
 	$(trace-cxx) $(CXX) -o $@ -c $< $(GLOBAL_CXXFLAGS) $(GLOBAL_CXXFLAGS_PCH) $(CXXFLAGS) $($@_CXXFLAGS) -MMD -MF $(call filename-to-dep, $@) -MP
 
-%.o: %.cpp
+$(buildprefix)%.o: %.cpp
+	@mkdir -p "$(dir $@)"
 	$(trace-cxx) $(CXX) -o $@ -c $< $(GLOBAL_CXXFLAGS) $(GLOBAL_CXXFLAGS_PCH) $(CXXFLAGS) $($@_CXXFLAGS) -MMD -MF $(call filename-to-dep, $@) -MP
 
-%.o: %.c
+$(buildprefix)%.o: %.c
+	@mkdir -p "$(dir $@)"
 	$(trace-cc) $(CC) -o $@ -c $< $(GLOBAL_CFLAGS) $(CFLAGS) $($@_CFLAGS) -MMD -MF $(call filename-to-dep, $@) -MP
diff --git a/programs.mk b/programs.mk
index 72afdf95251b..3ac64494e3a5 100644
--- a/programs.mk
+++ b/programs.mk
@@ -23,9 +23,9 @@ programs-list :=
 # - $(1)_INSTALL_DIR: the directory where the program will be
 #   installed; defaults to $(bindir).
 define build-program
-  _d := $$($(1)_DIR)
+  _d := $(buildprefix)$$($(1)_DIR)
   _srcs := $$(sort $$(foreach src, $$($(1)_SOURCES), $$(src)))
-  $(1)_OBJS := $$(addsuffix .o, $$(basename $$(_srcs)))
+  $(1)_OBJS := $$(addprefix $(buildprefix), $$(addsuffix .o, $$(basename $$(_srcs))))
   _libs := $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_PATH))
   $(1)_PATH := $$(_d)/$(1)