From a35c6eb4a2209716fe1d40cebad2b3adb5d05e0f Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 11 Feb 2014 14:15:57 +0100 Subject: [PATCH 1/9] Support setting CFLAGS and CXXFLAGS for libraries/programs --- libraries.mk | 5 ++++- programs.mk | 7 ++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/libraries.mk b/libraries.mk index c41694794..35ce4983a 100644 --- a/libraries.mk +++ b/libraries.mk @@ -17,6 +17,8 @@ endif # # - $(1)_SOURCES: the source files of the library. # +# - $(1)_CFLAGS: additional C compiler flags. +# # - $(1)_CXXFLAGS: additional C++ compiler flags. # # - $(1)_LIBS: the symbolic names of other libraries on which this @@ -102,7 +104,8 @@ define build-library $(1)_LDFLAGS_USE += $$($(1)_LDFLAGS_PROPAGATED) $(1)_LDFLAGS_USE_INSTALLED += $$($(1)_LDFLAGS_PROPAGATED) - # Propagate CXXFLAGS to the individual object files. + # Propagate CFLAGS and CXXFLAGS to the individual object files. + $$(foreach obj, $$($(1)_OBJS), $$(eval $$(obj)_CFLAGS=$$($(1)_CFLAGS))) $$(foreach obj, $$($(1)_OBJS), $$(eval $$(obj)_CXXFLAGS=$$($(1)_CXXFLAGS))) # Make each object file depend on the common dependencies. diff --git a/programs.mk b/programs.mk index ab04ff93e..5e27a7d62 100644 --- a/programs.mk +++ b/programs.mk @@ -8,6 +8,10 @@ programs-list := # # - $(1)_SOURCES: the source files of the program. # +# - $(1)_CFLAGS: additional C compiler flags. +# +# - $(1)_CXXFLAGS: additional C++ compiler flags. +# # - $(1)_LIBS: the symbolic names of libraries on which this program # depends. # @@ -48,7 +52,8 @@ define build-program endif - # Propagate CXXFLAGS to the individual object files. + # Propagate CFLAGS and CXXFLAGS to the individual object files. + $$(foreach obj, $$($(1)_OBJS), $$(eval $$(obj)_CFLAGS=$$($(1)_CFLAGS))) $$(foreach obj, $$($(1)_OBJS), $$(eval $$(obj)_CXXFLAGS=$$($(1)_CXXFLAGS))) # Make each object file depend on the common dependencies. From 581a160c11dd3de66d9cd1a6e01c0641909546ef Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 14 Feb 2014 20:12:04 +0100 Subject: [PATCH 2/9] Add a function for looking up programs in $PATH --- functions.mk | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/functions.mk b/functions.mk index 51f645701..1287e4321 100644 --- a/functions.mk +++ b/functions.mk @@ -5,3 +5,7 @@ rwildcard=$(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2) $(filter $(subst # Given a file name, produce the corresponding dependency file # (e.g. ‘foo/bar.o’ becomes ‘foo/.bar.o.dep’). filename-to-dep = $(dir $1).$(notdir $1).dep + +# Return the full path to a program by looking it up in $PATH, or the +# 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) From 79f699edca26c035a8bcd68c7d5a13b4fbcbf3be Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 18 Feb 2014 12:57:32 +0100 Subject: [PATCH 3/9] More GNU Make 3.81 compatibility --- functions.mk | 3 +++ install.mk | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/functions.mk b/functions.mk index 1287e4321..c92cdbc54 100644 --- a/functions.mk +++ b/functions.mk @@ -9,3 +9,6 @@ filename-to-dep = $(dir $1).$(notdir $1).dep # Return the full path to a program by looking it up in $PATH, or the # 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. +remove-trailing-slash = $(patsubst %/,%,$(1)) diff --git a/install.mk b/install.mk index e4bc734e1..0fe4cf938 100644 --- a/install.mk +++ b/install.mk @@ -1,7 +1,7 @@ # Add a rule for creating $(1) as a directory. This template may be # called multiple times for the same directory. define create-dir - _i := $(DESTDIR)$$(strip $(1)) + _i := $$(call remove-trailing-slash, $(DESTDIR)$$(strip $(1))) ifndef $$(_i)_SEEN $$(_i)_SEEN = 1 $$(_i): From 7bef965d6f191efb9c671f49fd187f4214db6120 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 18 Feb 2014 13:35:35 +0100 Subject: [PATCH 4/9] Make it work on GNU Make > 3.81 again --- functions.mk | 2 +- install.mk | 2 +- libraries.mk | 6 +++--- programs.mk | 6 +++--- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/functions.mk b/functions.mk index c92cdbc54..45d917399 100644 --- a/functions.mk +++ b/functions.mk @@ -11,4 +11,4 @@ filename-to-dep = $(dir $1).$(notdir $1).dep 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. -remove-trailing-slash = $(patsubst %/,%,$(1)) +add-trailing-slash = $(patsubst %/,%,$(1))/ diff --git a/install.mk b/install.mk index 0fe4cf938..dad0fd853 100644 --- a/install.mk +++ b/install.mk @@ -1,7 +1,7 @@ # Add a rule for creating $(1) as a directory. This template may be # called multiple times for the same directory. define create-dir - _i := $$(call remove-trailing-slash, $(DESTDIR)$$(strip $(1))) + _i := $$(call add-trailing-slash, $(DESTDIR)$$(strip $(1))) ifndef $$(_i)_SEEN $$(_i)_SEEN = 1 $$(_i): diff --git a/libraries.mk b/libraries.mk index 35ce4983a..21718c496 100644 --- a/libraries.mk +++ b/libraries.mk @@ -68,7 +68,7 @@ define build-library $(1)_PATH := $$(_d)/$$($(1)_NAME).$(SO_EXT) - $$($(1)_PATH): $$($(1)_OBJS) $$(_libs) | $$(_d) + $$($(1)_PATH): $$($(1)_OBJS) $$(_libs) | $$(_d)/ $$(trace-ld) $(CXX) -o $$@ -shared $(GLOBAL_LDFLAGS) $$($(1)_OBJS) $$($(1)_LDFLAGS) $$($(1)_LDFLAGS_PROPAGATED) $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_LDFLAGS_USE)) $(1)_LDFLAGS_USE += -L$$(_d) -Wl,-rpath,$$(abspath $$(_d)) -l$$(patsubst lib%,%,$$(strip $$($(1)_NAME))) @@ -79,7 +79,7 @@ define build-library $$(eval $$(call create-dir, $$($(1)_INSTALL_DIR))) - $$($(1)_INSTALL_PATH): $$($(1)_OBJS) $$(_libs_final) | $(DESTDIR)$$($(1)_INSTALL_DIR) + $$($(1)_INSTALL_PATH): $$($(1)_OBJS) $$(_libs_final) | $(DESTDIR)$$($(1)_INSTALL_DIR)/ $$(trace-ld) $(CXX) -o $$@ -shared $(GLOBAL_LDFLAGS) $$($(1)_OBJS) $$($(1)_LDFLAGS) $$($(1)_LDFLAGS_PROPAGATED) $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_LDFLAGS_USE_INSTALLED)) $(1)_LDFLAGS_USE_INSTALLED += -L$$(DESTDIR)$$($(1)_INSTALL_DIR) -Wl,-rpath,$$($(1)_INSTALL_DIR) -l$$(patsubst lib%,%,$$(strip $$($(1)_NAME))) @@ -92,7 +92,7 @@ define build-library $(1)_PATH := $$(_d)/$$($(1)_NAME).a - $$($(1)_PATH): $$($(1)_OBJS) | $$(_d) + $$($(1)_PATH): $$($(1)_OBJS) | $$(_d)/ $(trace-ar) ar crs $$@ $$? $(1)_LDFLAGS_USE += $$($(1)_PATH) $$($(1)_LDFLAGS) diff --git a/programs.mk b/programs.mk index 5e27a7d62..04ba282b2 100644 --- a/programs.mk +++ b/programs.mk @@ -28,7 +28,7 @@ define build-program $$(eval $$(call create-dir, $$(_d))) - $$($(1)_PATH): $$($(1)_OBJS) $$(_libs) | $$(_d) + $$($(1)_PATH): $$($(1)_OBJS) $$(_libs) | $$(_d)/ $$(trace-ld) $(CXX) -o $$@ $(GLOBAL_LDFLAGS) $$($(1)_OBJS) $$($(1)_LDFLAGS) $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_LDFLAGS_USE)) $(1)_INSTALL_DIR ?= $$(bindir) @@ -42,12 +42,12 @@ define build-program _libs_final := $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_INSTALL_PATH)) - $(DESTDIR)$$($(1)_INSTALL_PATH): $$($(1)_OBJS) $$(_libs_final) | $(DESTDIR)$$($(1)_INSTALL_DIR) + $(DESTDIR)$$($(1)_INSTALL_PATH): $$($(1)_OBJS) $$(_libs_final) | $(DESTDIR)$$($(1)_INSTALL_DIR)/ $$(trace-ld) $(CXX) -o $$@ $(GLOBAL_LDFLAGS) $$($(1)_OBJS) $$($(1)_LDFLAGS) $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_LDFLAGS_USE_INSTALLED)) else - $(DESTDIR)$$($(1)_INSTALL_PATH): $$($(1)_PATH) | $(DESTDIR)$$($(1)_INSTALL_DIR) + $(DESTDIR)$$($(1)_INSTALL_PATH): $$($(1)_PATH) | $(DESTDIR)$$($(1)_INSTALL_DIR)/ install -t $$($(1)_INSTALL_DIR) $$< endif From 4e7e498ff95b553227a26fc20549bd69995a0b08 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 28 Feb 2014 12:01:42 +0100 Subject: [PATCH 5/9] Add variable GLOBAL_COMMON_DEPS This is a list of dependencies on which all C/C++ object files depend. Primarily useful for global precompiled headers. --- libraries.mk | 2 +- programs.mk | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries.mk b/libraries.mk index 21718c496..547f393e3 100644 --- a/libraries.mk +++ b/libraries.mk @@ -109,7 +109,7 @@ define build-library $$(foreach obj, $$($(1)_OBJS), $$(eval $$(obj)_CXXFLAGS=$$($(1)_CXXFLAGS))) # Make each object file depend on the common dependencies. - $$(foreach obj, $$($(1)_OBJS), $$(eval $$(obj): $$($(1)_COMMON_DEPS))) + $$(foreach obj, $$($(1)_OBJS), $$(eval $$(obj): $$($(1)_COMMON_DEPS) $$(GLOBAL_COMMON_DEPS))) # Include .dep files, if they exist. $(1)_DEPS := $$(foreach fn, $$($(1)_OBJS), $$(call filename-to-dep, $$(fn))) diff --git a/programs.mk b/programs.mk index 04ba282b2..d07c91448 100644 --- a/programs.mk +++ b/programs.mk @@ -57,7 +57,7 @@ define build-program $$(foreach obj, $$($(1)_OBJS), $$(eval $$(obj)_CXXFLAGS=$$($(1)_CXXFLAGS))) # Make each object file depend on the common dependencies. - $$(foreach obj, $$($(1)_OBJS), $$(eval $$(obj): $$($(1)_COMMON_DEPS))) + $$(foreach obj, $$($(1)_OBJS), $$(eval $$(obj): $$($(1)_COMMON_DEPS) $$(GLOBAL_COMMON_DEPS))) # Include .dep files, if they exist. $(1)_DEPS := $$(foreach fn, $$($(1)_OBJS), $$(call filename-to-dep, $$(fn))) From 4eac3b2471063a3726790368665df963e809fc4e Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 28 Feb 2014 12:13:20 +0100 Subject: [PATCH 6/9] Add a variable GLOBAL_CXXFLAGS_PCH for use by precompiled headers You don't want to use GLOBAL_CXXFLAGS for passing flags like "-include-pch" (clang), because that means you cannot use GLOBAL_CXXFLAGS when generating the PCH. --- patterns.mk | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/patterns.mk b/patterns.mk index f5674d404..6b2cfd017 100644 --- a/patterns.mk +++ b/patterns.mk @@ -1,8 +1,8 @@ %.o: %.cc - $(trace-cxx) $(CXX) -o $@ -c $< $(GLOBAL_CXXFLAGS) $(CXXFLAGS) $($@_CXXFLAGS) -MMD -MF $(call filename-to-dep, $@) -MP + $(trace-cxx) $(CXX) -o $@ -c $< $(GLOBAL_CXXFLAGS) $(GLOBAL_CXXFLAGS_PCH) $(CXXFLAGS) $($@_CXXFLAGS) -MMD -MF $(call filename-to-dep, $@) -MP %.o: %.cpp - $(trace-cxx) $(CXX) -o $@ -c $< $(GLOBAL_CXXFLAGS) $(CXXFLAGS) $($@_CXXFLAGS) -MMD -MF $(call filename-to-dep, $@) -MP + $(trace-cxx) $(CXX) -o $@ -c $< $(GLOBAL_CXXFLAGS) $(GLOBAL_CXXFLAGS_PCH) $(CXXFLAGS) $($@_CXXFLAGS) -MMD -MF $(call filename-to-dep, $@) -MP %.o: %.c $(trace-cc) $(CC) -o $@ -c $< $(GLOBAL_CFLAGS) $(CFLAGS) $($@_CFLAGS) -MMD -MF $(call filename-to-dep, $@) -MP From a3767628481eefc956eb9d9d70eda62930549205 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Mon, 3 Mar 2014 15:19:04 +0100 Subject: [PATCH 7/9] Add support for making relocatable packages using $ORIGIN --- lib.mk | 1 + libraries.mk | 9 ++++++--- programs.mk | 4 ++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib.mk b/lib.mk index 6bcd83a06..3fde7fe91 100644 --- a/lib.mk +++ b/lib.mk @@ -34,6 +34,7 @@ ifeq ($(BUILD_SHARED_LIBS), 1) ifneq ($(OS), Darwin) GLOBAL_LDFLAGS += -Wl,--no-copy-dt-needed-entries endif + SET_RPATH_TO_LIBS ?= 1 endif diff --git a/libraries.mk b/libraries.mk index 547f393e3..2da6c9b6a 100644 --- a/libraries.mk +++ b/libraries.mk @@ -69,7 +69,7 @@ define build-library $(1)_PATH := $$(_d)/$$($(1)_NAME).$(SO_EXT) $$($(1)_PATH): $$($(1)_OBJS) $$(_libs) | $$(_d)/ - $$(trace-ld) $(CXX) -o $$@ -shared $(GLOBAL_LDFLAGS) $$($(1)_OBJS) $$($(1)_LDFLAGS) $$($(1)_LDFLAGS_PROPAGATED) $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_LDFLAGS_USE)) + $$(trace-ld) $(CXX) -o $$@ -shared $$(GLOBAL_LDFLAGS) $$($(1)_OBJS) $$($(1)_LDFLAGS) $$($(1)_LDFLAGS_PROPAGATED) $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_LDFLAGS_USE)) $(1)_LDFLAGS_USE += -L$$(_d) -Wl,-rpath,$$(abspath $$(_d)) -l$$(patsubst lib%,%,$$(strip $$($(1)_NAME))) @@ -80,9 +80,12 @@ define build-library $$(eval $$(call create-dir, $$($(1)_INSTALL_DIR))) $$($(1)_INSTALL_PATH): $$($(1)_OBJS) $$(_libs_final) | $(DESTDIR)$$($(1)_INSTALL_DIR)/ - $$(trace-ld) $(CXX) -o $$@ -shared $(GLOBAL_LDFLAGS) $$($(1)_OBJS) $$($(1)_LDFLAGS) $$($(1)_LDFLAGS_PROPAGATED) $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_LDFLAGS_USE_INSTALLED)) + $$(trace-ld) $(CXX) -o $$@ -shared $$(GLOBAL_LDFLAGS) $$($(1)_OBJS) $$($(1)_LDFLAGS) $$($(1)_LDFLAGS_PROPAGATED) $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_LDFLAGS_USE_INSTALLED)) - $(1)_LDFLAGS_USE_INSTALLED += -L$$(DESTDIR)$$($(1)_INSTALL_DIR) -Wl,-rpath,$$($(1)_INSTALL_DIR) -l$$(patsubst lib%,%,$$(strip $$($(1)_NAME))) + $(1)_LDFLAGS_USE_INSTALLED += -L$$(DESTDIR)$$($(1)_INSTALL_DIR) -l$$(patsubst lib%,%,$$(strip $$($(1)_NAME))) + ifeq ($(SET_RPATH_TO_LIBS), 1) + $(1)_LDFLAGS_USE_INSTALLED += -Wl,-rpath,$$($(1)_INSTALL_DIR) + endif ifdef $(1)_FORCE_INSTALL install: $$($(1)_INSTALL_PATH) diff --git a/programs.mk b/programs.mk index d07c91448..b6de06894 100644 --- a/programs.mk +++ b/programs.mk @@ -29,7 +29,7 @@ define build-program $$(eval $$(call create-dir, $$(_d))) $$($(1)_PATH): $$($(1)_OBJS) $$(_libs) | $$(_d)/ - $$(trace-ld) $(CXX) -o $$@ $(GLOBAL_LDFLAGS) $$($(1)_OBJS) $$($(1)_LDFLAGS) $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_LDFLAGS_USE)) + $$(trace-ld) $(CXX) -o $$@ $$(GLOBAL_LDFLAGS) $$($(1)_OBJS) $$($(1)_LDFLAGS) $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_LDFLAGS_USE)) $(1)_INSTALL_DIR ?= $$(bindir) $(1)_INSTALL_PATH := $$($(1)_INSTALL_DIR)/$(1) @@ -43,7 +43,7 @@ define build-program _libs_final := $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_INSTALL_PATH)) $(DESTDIR)$$($(1)_INSTALL_PATH): $$($(1)_OBJS) $$(_libs_final) | $(DESTDIR)$$($(1)_INSTALL_DIR)/ - $$(trace-ld) $(CXX) -o $$@ $(GLOBAL_LDFLAGS) $$($(1)_OBJS) $$($(1)_LDFLAGS) $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_LDFLAGS_USE_INSTALLED)) + $$(trace-ld) $(CXX) -o $$@ $$(GLOBAL_LDFLAGS) $$($(1)_OBJS) $$($(1)_LDFLAGS) $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_LDFLAGS_USE_INSTALLED)) else From d6a45f6bdbf7c71eda9e4ab1ab78b373be5422b9 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Mon, 3 Mar 2014 15:29:58 +0100 Subject: [PATCH 8/9] Don't set an absolute soname --- libraries.mk | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libraries.mk b/libraries.mk index 2da6c9b6a..4b8292cc2 100644 --- a/libraries.mk +++ b/libraries.mk @@ -66,6 +66,10 @@ define build-library endif endif + ifneq ($(OS), Darwin) + $(1)_LDFLAGS += -Wl,-soname=$$($(1)_NAME).$(SO_EXT) + endif + $(1)_PATH := $$(_d)/$$($(1)_NAME).$(SO_EXT) $$($(1)_PATH): $$($(1)_OBJS) $$(_libs) | $$(_d)/ From 7191a7394a3091ed2856508674f84f3a87eda5a6 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 3 Apr 2014 17:35:16 +0200 Subject: [PATCH 9/9] Support Illumos From https://github.com/NixOS/nix/pull/236 --- lib.mk | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib.mk b/lib.mk index 3fde7fe91..56e162d50 100644 --- a/lib.mk +++ b/lib.mk @@ -32,7 +32,9 @@ ifeq ($(BUILD_SHARED_LIBS), 1) GLOBAL_CFLAGS += -fPIC GLOBAL_CXXFLAGS += -fPIC ifneq ($(OS), Darwin) + ifneq ($(OS), SunOS) GLOBAL_LDFLAGS += -Wl,--no-copy-dt-needed-entries + endif endif SET_RPATH_TO_LIBS ?= 1 endif