diff options
Diffstat (limited to 'mk')
-rw-r--r-- | mk/README.md | 6 | ||||
-rw-r--r-- | mk/clean.mk | 11 | ||||
-rw-r--r-- | mk/dist.mk | 17 | ||||
-rw-r--r-- | mk/functions.mk | 7 | ||||
-rw-r--r-- | mk/install.mk | 62 | ||||
-rw-r--r-- | mk/jars.mk | 29 | ||||
-rw-r--r-- | mk/lib.mk | 128 | ||||
-rw-r--r-- | mk/libraries.mk | 118 | ||||
-rw-r--r-- | mk/patterns.mk | 8 | ||||
-rw-r--r-- | mk/programs.mk | 64 | ||||
-rw-r--r-- | mk/templates.mk | 12 | ||||
-rw-r--r-- | mk/tests.mk | 26 | ||||
-rw-r--r-- | mk/tracing.mk | 17 |
13 files changed, 505 insertions, 0 deletions
diff --git a/mk/README.md b/mk/README.md new file mode 100644 index 000000000000..e4cd742b4c7f --- /dev/null +++ b/mk/README.md @@ -0,0 +1,6 @@ +This is a set of helper Makefiles for doing non-recursive builds with +GNU Make. The canonical source can be found at +https://github.com/edolstra/make-rules. You should copy the files +into the `mk` subdirectory of your project. + +TODO: write more documentation. diff --git a/mk/clean.mk b/mk/clean.mk new file mode 100644 index 000000000000..ce9afb3b0db7 --- /dev/null +++ b/mk/clean.mk @@ -0,0 +1,11 @@ +clean-files := + +clean: + $(suppress) rm -fv -- $(clean-files) + +dryclean: + @for i in $(clean-files); do if [ -e $$i ]; then echo $$i; fi; done | sort + +print-top-help += \ + echo " clean: Delete generated files"; \ + echo " dryclean: Show what files would be deleted by 'make clean'"; diff --git a/mk/dist.mk b/mk/dist.mk new file mode 100644 index 000000000000..794b277713d4 --- /dev/null +++ b/mk/dist.mk @@ -0,0 +1,17 @@ +ifdef PACKAGE_NAME + +dist-name = $(PACKAGE_NAME)-$(PACKAGE_VERSION) + +dist: $(dist-name).tar.bz2 $(dist-name).tar.xz + +$(dist-name).tar.bz2: $(dist-files) + $(trace-gen) tar cfj $@ $(sort $(dist-files)) --transform 's,^,$(dist-name)/,' + +$(dist-name).tar.xz: $(dist-files) + $(trace-gen) tar cfJ $@ $(sort $(dist-files)) --transform 's,^,$(dist-name)/,' + +clean-files += $(dist-name).tar.bz2 $(dist-name).tar.xz + +print-top-help += echo " dist: Generate a source distribution"; + +endif diff --git a/mk/functions.mk b/mk/functions.mk new file mode 100644 index 000000000000..51f6457016e7 --- /dev/null +++ b/mk/functions.mk @@ -0,0 +1,7 @@ +# Utility function for recursively finding files, e.g. +# ‘$(call rwildcard, path/to/dir, *.c *.h)’. +rwildcard=$(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2) $(filter $(subst *,%,$2),$d)) + +# 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 diff --git a/mk/install.mk b/mk/install.mk new file mode 100644 index 000000000000..e4bc734e11fb --- /dev/null +++ b/mk/install.mk @@ -0,0 +1,62 @@ +# 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)) + ifndef $$(_i)_SEEN + $$(_i)_SEEN = 1 + $$(_i): + $$(trace-mkdir) install -d "$$@" + endif +endef + + +# Add a rule for installing file $(1) as file $(2) with mode $(3). +# The directory containing $(2) will be created automatically. +define install-file-as + + _i := $(DESTDIR)$$(strip $(2)) + + install: $$(_i) + + $$(_i): $(1) | $$(dir $$(_i)) + $$(trace-install) install -m $(3) $(1) "$$@" + + $$(eval $$(call create-dir, $$(dir $(2)))) + +endef + + +# Add a rule for installing file $(1) in directory $(2) with mode +# $(3). The directory will be created automatically. +define install-file-in + $$(eval $$(call install-file-as,$(1),$(2)/$$(notdir $(1)),$(3))) +endef + + +define install-program-in + $$(eval $$(call install-file-in,$(1),$(2),0755)) +endef + + +define install-data-in + $$(eval $$(call install-file-in,$(1),$(2),0644)) +endef + + +# Install a symlink from $(2) to $(1). Note that $(1) need not exist. +define install-symlink + + _i := $(DESTDIR)$$(strip $(2)) + + install: $$(_i) + + $$(_i): | $$(dir $$(_i)) + $$(trace-install) ln -sfn $(1) "$$@" + + $$(eval $$(call create-dir, $$(dir $(2)))) + +endef + + +print-top-help += \ + echo " install: Install into \$$(prefix) (currently set to '$(prefix)')"; diff --git a/mk/jars.mk b/mk/jars.mk new file mode 100644 index 000000000000..99470f37435b --- /dev/null +++ b/mk/jars.mk @@ -0,0 +1,29 @@ +define build-jar + $(1)_NAME ?= $(1) + + _d := $$(strip $$($(1)_DIR)) + + $(1)_PATH := $$(_d)/$$($(1)_NAME).jar + + $(1)_TMPDIR := $$(_d)/.$$($(1)_NAME).jar.tmp + + $$($(1)_PATH): $$($(1)_SOURCES) + @rm -rf $$($(1)_TMPDIR) + @mkdir -p $$($(1)_TMPDIR) + $$(trace-javac) javac $(GLOBAL_JAVACFLAGS) $$($(1)_JAVACFLAGS) -d $$($(1)_TMPDIR) $$($(1)_SOURCES) + $$(trace-jar) jar cf $$($(1)_PATH) -C $$($(1)_TMPDIR) . + @rm -rf $$($(1)_TMPDIR) + + $(1)_INSTALL_DIR ?= $$(libdir)/java + + $(1)_INSTALL_PATH := $$($(1)_INSTALL_DIR)/$$($(1)_NAME).jar + + $$(eval $$(call install-file-as, $$($(1)_PATH), $$($(1)_INSTALL_PATH), 0644)) + + install: $$($(1)_INSTALL_PATH) + + jars-list += $$($(1)_PATH) + + clean-files += $$($(1)_PATH) + +endef diff --git a/mk/lib.mk b/mk/lib.mk new file mode 100644 index 000000000000..6bcd83a06e5f --- /dev/null +++ b/mk/lib.mk @@ -0,0 +1,128 @@ +default: all + + +# Get rid of default suffixes. FIXME: is this a good idea? +.SUFFIXES: + + +# Initialise some variables. +bin-scripts := +noinst-scripts := +man-pages := +install-tests := +dist-files := +OS = $(shell uname -s) + + +# Default installation paths. +prefix ?= /usr/local +libdir ?= $(prefix)/lib +bindir ?= $(prefix)/bin +libexecdir ?= $(prefix)/libexec +datadir ?= $(prefix)/share +localstatedir ?= $(prefix)/var +sysconfdir ?= $(prefix)/etc +mandir ?= $(prefix)/share/man + + +# Pass -fPIC if we're building dynamic libraries. +BUILD_SHARED_LIBS ?= 1 + +ifeq ($(BUILD_SHARED_LIBS), 1) + GLOBAL_CFLAGS += -fPIC + GLOBAL_CXXFLAGS += -fPIC + ifneq ($(OS), Darwin) + GLOBAL_LDFLAGS += -Wl,--no-copy-dt-needed-entries + endif +endif + + +# Pass -g if we want debug info. +BUILD_DEBUG ?= 1 + +ifeq ($(BUILD_DEBUG), 1) + GLOBAL_CFLAGS += -g + GLOBAL_CXXFLAGS += -g + GLOBAL_JAVACFLAGS += -g +endif + + +include mk/functions.mk +include mk/tracing.mk +include mk/clean.mk +include mk/install.mk +include mk/libraries.mk +include mk/programs.mk +include mk/jars.mk +include mk/patterns.mk +include mk/templates.mk +include mk/tests.mk + + +# Include all sub-Makefiles. +define include-sub-makefile + d := $$(patsubst %/,%,$$(dir $(1))) + include $(1) +endef + +$(foreach mf, $(makefiles), $(eval $(call include-sub-makefile, $(mf)))) + + +# Instantiate stuff. +$(foreach lib, $(libraries), $(eval $(call build-library,$(lib)))) +$(foreach prog, $(programs), $(eval $(call build-program,$(prog)))) +$(foreach jar, $(jars), $(eval $(call build-jar,$(jar)))) +$(foreach script, $(bin-scripts), $(eval $(call install-program-in,$(script),$(bindir)))) +$(foreach script, $(bin-scripts), $(eval programs-list += $(script))) +$(foreach script, $(noinst-scripts), $(eval programs-list += $(script))) +$(foreach template, $(template-files), $(eval $(call instantiate-template,$(template)))) +$(foreach test, $(install-tests), $(eval $(call run-install-test,$(test)))) +$(foreach file, $(man-pages), $(eval $(call install-data-in, $(file), $(mandir)/man$(patsubst .%,%,$(suffix $(file)))))) + + +include mk/dist.mk + + +.PHONY: default all man help + +all: $(programs-list) $(libs-list) $(jars-list) $(man-pages) + +man: $(man-pages) + + +help: + @echo "The following targets are available:" + @echo "" + @echo " default: Build default targets" +ifdef man-pages + @echo " man: Generate manual pages" +endif + @$(print-top-help) +ifdef programs-list + @echo "" + @echo "The following programs can be built:" + @echo "" + @for i in $(programs-list); do echo " $$i"; done +endif +ifdef libs-list + @echo "" + @echo "The following libraries can be built:" + @echo "" + @for i in $(libs-list); do echo " $$i"; done +endif +ifdef jars-list + @echo "" + @echo "The following JARs can be built:" + @echo "" + @for i in $(jars-list); do echo " $$i"; done +endif + @echo "" + @echo "The following variables control the build:" + @echo "" + @echo " BUILD_SHARED_LIBS ($(BUILD_SHARED_LIBS)): Whether to build shared libraries" + @echo " BUILD_DEBUG ($(BUILD_DEBUG)): Whether to include debug symbols" + @echo " CC ($(CC)): C compiler to be used" + @echo " CFLAGS: Flags for the C compiler" + @echo " CXX ($(CXX)): C++ compiler to be used" + @echo " CXXFLAGS: Flags for the C++ compiler" + @$(print-var-help) diff --git a/mk/libraries.mk b/mk/libraries.mk new file mode 100644 index 000000000000..c41694794b60 --- /dev/null +++ b/mk/libraries.mk @@ -0,0 +1,118 @@ +libs-list := + +ifeq ($(OS), Darwin) + SO_EXT = dylib +else + SO_EXT = so +endif + +# Build a library with symbolic name $(1). The library is defined by +# various variables prefixed by ‘$(1)_’: +# +# - $(1)_NAME: the name of the library (e.g. ‘libfoo’); defaults to +# $(1). +# +# - $(1)_DIR: the directory where the (non-installed) library will be +# placed. +# +# - $(1)_SOURCES: the source files of the library. +# +# - $(1)_CXXFLAGS: additional C++ compiler flags. +# +# - $(1)_LIBS: the symbolic names of other libraries on which this +# library depends. +# +# - $(1)_ALLOW_UNDEFINED: if set, the library is allowed to have +# undefined symbols. Has no effect for static libraries. +# +# - $(1)_LDFLAGS: additional linker flags. +# +# - $(1)_LDFLAGS_PROPAGATED: additional linker flags, also propagated +# to the linking of programs/libraries that use this library. +# +# - $(1)_FORCE_INSTALL: if defined, the library will be installed even +# if it's not needed (i.e. dynamically linked) by a program. +# +# - $(1)_INSTALL_DIR: the directory where the library will be +# installed. Defaults to $(libdir). +# +# - BUILD_SHARED_LIBS: if equal to ‘1’, a dynamic library will be +# built, otherwise a static library. +define build-library + $(1)_NAME ?= $(1) + _d := $$(strip $$($(1)_DIR)) + _srcs := $$(sort $$(foreach src, $$($(1)_SOURCES), $$(src))) + $(1)_OBJS := $$(addsuffix .o, $$(basename $$(_srcs))) + _libs := $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_PATH)) + + $(1)_INSTALL_DIR ?= $$(libdir) + + $(1)_LDFLAGS_USE := + $(1)_LDFLAGS_USE_INSTALLED := + + $$(eval $$(call create-dir, $$(_d))) + + ifeq ($(BUILD_SHARED_LIBS), 1) + + ifdef $(1)_ALLOW_UNDEFINED + ifeq ($(OS), Darwin) + $(1)_LDFLAGS += -undefined suppress -flat_namespace + endif + else + ifneq ($(OS), Darwin) + $(1)_LDFLAGS += -Wl,-z,defs + endif + endif + + $(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)) + + $(1)_LDFLAGS_USE += -L$$(_d) -Wl,-rpath,$$(abspath $$(_d)) -l$$(patsubst lib%,%,$$(strip $$($(1)_NAME))) + + $(1)_INSTALL_PATH := $(DESTDIR)$$($(1)_INSTALL_DIR)/$$($(1)_NAME).$(SO_EXT) + + _libs_final := $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_INSTALL_PATH)) + + $$(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)) + + $(1)_LDFLAGS_USE_INSTALLED += -L$$(DESTDIR)$$($(1)_INSTALL_DIR) -Wl,-rpath,$$($(1)_INSTALL_DIR) -l$$(patsubst lib%,%,$$(strip $$($(1)_NAME))) + + ifdef $(1)_FORCE_INSTALL + install: $$($(1)_INSTALL_PATH) + endif + + else + + $(1)_PATH := $$(_d)/$$($(1)_NAME).a + + $$($(1)_PATH): $$($(1)_OBJS) | $$(_d) + $(trace-ar) ar crs $$@ $$? + + $(1)_LDFLAGS_USE += $$($(1)_PATH) $$($(1)_LDFLAGS) + + $(1)_INSTALL_PATH := $$(libdir)/$$($(1)_NAME).a + + endif + + $(1)_LDFLAGS_USE += $$($(1)_LDFLAGS_PROPAGATED) + $(1)_LDFLAGS_USE_INSTALLED += $$($(1)_LDFLAGS_PROPAGATED) + + # Propagate CXXFLAGS to the individual object files. + $$(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))) + + # Include .dep files, if they exist. + $(1)_DEPS := $$(foreach fn, $$($(1)_OBJS), $$(call filename-to-dep, $$(fn))) + -include $$($(1)_DEPS) + + libs-list += $$($(1)_PATH) + clean-files += $$(_d)/*.a $$(_d)/*.$(SO_EXT) $$(_d)/*.o $$(_d)/.*.dep $$($(1)_DEPS) $$($(1)_OBJS) + dist-files += $$(_srcs) +endef diff --git a/mk/patterns.mk b/mk/patterns.mk new file mode 100644 index 000000000000..f5674d40436c --- /dev/null +++ b/mk/patterns.mk @@ -0,0 +1,8 @@ +%.o: %.cc + $(trace-cxx) $(CXX) -o $@ -c $< $(GLOBAL_CXXFLAGS) $(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 + +%.o: %.c + $(trace-cc) $(CC) -o $@ -c $< $(GLOBAL_CFLAGS) $(CFLAGS) $($@_CFLAGS) -MMD -MF $(call filename-to-dep, $@) -MP diff --git a/mk/programs.mk b/mk/programs.mk new file mode 100644 index 000000000000..ab04ff93e910 --- /dev/null +++ b/mk/programs.mk @@ -0,0 +1,64 @@ +programs-list := + +# Build a program with symbolic name $(1). The program is defined by +# various variables prefixed by ‘$(1)_’: +# +# - $(1)_DIR: the directory where the (non-installed) program will be +# placed. +# +# - $(1)_SOURCES: the source files of the program. +# +# - $(1)_LIBS: the symbolic names of libraries on which this program +# depends. +# +# - $(1)_LDFLAGS: additional linker flags. +# +# - $(1)_INSTALL_DIR: the directory where the program will be +# installed; defaults to $(bindir). +define build-program + _d := $$($(1)_DIR) + _srcs := $$(sort $$(foreach src, $$($(1)_SOURCES), $$(src))) + $(1)_OBJS := $$(addsuffix .o, $$(basename $$(_srcs))) + _libs := $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_PATH)) + $(1)_PATH := $$(_d)/$(1) + + $$(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)) + + $(1)_INSTALL_DIR ?= $$(bindir) + $(1)_INSTALL_PATH := $$($(1)_INSTALL_DIR)/$(1) + + $$(eval $$(call create-dir, $$($(1)_INSTALL_DIR))) + + install: $(DESTDIR)$$($(1)_INSTALL_PATH) + + ifeq ($(BUILD_SHARED_LIBS), 1) + + _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)) + + else + + $(DESTDIR)$$($(1)_INSTALL_PATH): $$($(1)_PATH) | $(DESTDIR)$$($(1)_INSTALL_DIR) + install -t $$($(1)_INSTALL_DIR) $$< + + endif + + # Propagate CXXFLAGS to the individual object files. + $$(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))) + + # Include .dep files, if they exist. + $(1)_DEPS := $$(foreach fn, $$($(1)_OBJS), $$(call filename-to-dep, $$(fn))) + -include $$($(1)_DEPS) + + programs-list += $$($(1)_PATH) + clean-files += $$($(1)_PATH) $$(_d)/*.o $$(_d)/.*.dep $$($(1)_DEPS) $$($(1)_OBJS) + dist-files += $$(_srcs) +endef diff --git a/mk/templates.mk b/mk/templates.mk new file mode 100644 index 000000000000..8e8b28643f5c --- /dev/null +++ b/mk/templates.mk @@ -0,0 +1,12 @@ +template-files := + +# Create the file $(1) from $(1).in by running config.status (which +# substitutes all ‘@var@’ variables set by the configure script). +define instantiate-template + + clean-files += $(1) + +endef + +%: %.in + $(trace-gen) ./config.status --quiet --file $@ diff --git a/mk/tests.mk b/mk/tests.mk new file mode 100644 index 000000000000..004a48028616 --- /dev/null +++ b/mk/tests.mk @@ -0,0 +1,26 @@ +# Run program $1 as part of ‘make installcheck’. +define run-install-test + + installcheck: $1 + + _installcheck-list += $1 + +endef + +installcheck: + @total=0; failed=0; for i in $(_installcheck-list); do \ + total=$$((total + 1)); \ + echo "running test $$i"; \ + if (cd $$(dirname $$i) && $(tests-environment) $$(basename $$i)); then \ + echo "PASS: $$i"; \ + else \ + echo "FAIL: $$i"; \ + failed=$$((failed + 1)); \ + fi; \ + done; \ + if [ "$$failed" != 0 ]; then \ + echo "$$failed out of $$total tests failed "; \ + exit 1; \ + fi + +.PHONY: check installcheck diff --git a/mk/tracing.mk b/mk/tracing.mk new file mode 100644 index 000000000000..13912d3c7821 --- /dev/null +++ b/mk/tracing.mk @@ -0,0 +1,17 @@ +V ?= 0 + +ifeq ($(V), 0) + + trace-gen = @echo " GEN " $@; + trace-cc = @echo " CC " $@; + trace-cxx = @echo " CXX " $@; + trace-ld = @echo " LD " $@; + trace-ar = @echo " AR " $@; + trace-install = @echo " INST " $@; + trace-javac = @echo " JAVAC " $@; + trace-jar = @echo " JAR " $@; + trace-mkdir = @echo " MKDIR " $@; + + suppress = @ + +endif |