Split Makefile.lib into several *.mk files

This commit is contained in:
Eelco Dolstra 2013-11-25 09:50:35 +00:00
parent e9b6397d2f
commit f980755766
9 changed files with 243 additions and 203 deletions

View file

@ -1,201 +0,0 @@
default: all
# Include Autoconf variables.
Makefile.config: Makefile.config.in
./config.status --file $@
include Makefile.config
# Installing stuff.
define create-dir =
ifndef $(1)_SEEN
$(1)_SEEN = 1
$(1):
install -d $(1)
endif
endef
define install-file-in =
install:: $(1)/$(notdir $(2))
$$(eval $$(call create-dir,$(1)))
$(1)/$(notdir $(2)): $(2) | $(1)
install -t $(1) $(2)
endef
# Include all sub-Makefiles.
define include_sub_makefile =
d := $$(patsubst %/, %, $$(dir $(1)))
include $(1)
endef
$(foreach mf, $(SUBS), $(eval $(call include_sub_makefile, $(mf))))
# Initialise some variables.
clean_list :=
dist_files :=
QUIET = @
# Pass -fPIC if we're building dynamic libraries.
ifeq ($(BUILD_SHARED_LIBS), 1)
GLOBAL_CFLAGS += -fPIC
GLOBAL_CXXFLAGS += -fPIC
GLOBAL_LDFLAGS += -Wl,--no-copy-dt-needed-entries
endif
# Pass -g if we want debug info.
ifeq ($(BUILD_DEBUG), 1)
GLOBAL_CFLAGS += -g
GLOBAL_CXXFLAGS += -g
endif
# Pattern rules.
%.o: %.cc
$(QUIET) $(CXX) -o $@ -c $< $(GLOBAL_CXXFLAGS) $(CXXFLAGS) $($@_CXXFLAGS) -MMD -MF $(basename $@).dep -MP
%.o: %.c
$(QUIET) $(CC) -o $@ -c $< $(GLOBAL_CFLAGS) $(CFLAGS) $($@_CFLAGS) -MMD -MF $(basename $@).dep -MP
# Generate Make rules for libraries.
libs_list :=
define LIBS_template =
$(1)_NAME ?= $(1)
_d := $$(strip $$($(1)_DIR))
_srcs := $$(foreach src, $$($(1)_SOURCES), $$(_d)/$$(src))
$(1)_OBJS := $$(addsuffix .o, $$(basename $$(_srcs)))
_libs := $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_PATH))
$(1)_LDFLAGS_USE :=
$(1)_LDFLAGS_USE_INSTALLED :=
ifeq ($(BUILD_SHARED_LIBS), 1)
ifndef $(1)_ALLOW_UNDEFINED
$(1)_LDFLAGS += -z defs
endif
$(1)_PATH := $$(_d)/$$($(1)_NAME).so
$$($(1)_PATH): $$($(1)_OBJS) $$(_libs)
$(QUIET) $(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_DIR := $$(libdir)
$(1)_INSTALL_PATH := $$($(1)_INSTALL_DIR)/$$($(1)_NAME).so
_libs_final := $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_INSTALL_PATH))
$$($(1)_INSTALL_PATH): $$($(1)_OBJS) $$(_libs_final)
install -d $$($(1)_INSTALL_DIR)
$(QUIET) $(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$$($(1)_INSTALL_DIR) -Wl,-rpath,$$($(1)_INSTALL_DIR) -l$$(patsubst lib%,%,$$(strip $$($(1)_NAME)))
else
$(1)_PATH := $$(_d)/$$($(1)_NAME).a
$$($(1)_PATH): $$($(1)_OBJS)
$(QUIET) 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)))
include $$(wildcard $$(_d)/*.dep)
libs_list += $$($(1)_PATH)
clean_list += $$(_d)/*.a $$(_d)/*.so $$(_d)/*.o $$(_d)/*.dep
dist_files += $$(_srcs)
endef
$(foreach lib, $(LIBS), $(eval $(call LIBS_template,$(lib))))
# Generate Make rules for programs.
programs_list :=
define PROGRAMS_template =
_d := $$($(1)_DIR)
_srcs := $$(foreach src, $$($(1)_SOURCES), $$(_d)/$$(src))
$(1)_OBJS := $$(addsuffix .o, $$(basename $$(_srcs)))
_libs := $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_PATH))
$(1)_PATH := $$(_d)/$(1)
$$($(1)_PATH): $$($(1)_OBJS) $$(_libs)
$(QUIET) $(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:: $$($(1)_INSTALL_PATH)
ifeq ($(BUILD_SHARED_LIBS), 1)
_libs_final := $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_INSTALL_PATH))
$$($(1)_INSTALL_PATH): $$($(1)_OBJS) $$(_libs_final) | $$($(1)_INSTALL_DIR)
$(QUIET) $(CXX) -o $$@ $(GLOBAL_LDFLAGS) $$($(1)_OBJS) $$($(1)_LDFLAGS) $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_LDFLAGS_USE_INSTALLED))
else
$$($(1)_INSTALL_PATH): $$($(1)_PATH) | $$($(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)))
include $$(wildcard $$(_d)/*.dep)
programs_list += $$($(1)_PATH)
clean_list += $$($(1)_PATH) $$(_d)/*.o $$(_d)/*.dep
dist_files += $$(_srcs)
endef
$(foreach prog, $(PROGRAMS), $(eval $(call PROGRAMS_template,$(prog))))
# Distributing stuff.
dist_name = $(PACKAGE_NAME)-$(PACKAGE_VERSION)
dist:
@echo $(dist_files)
$(QUIET) tar cvfj $(dist_name).tar.bz2 $(dist_files) --transform 's,^,$(dist_name)/,'
# Cleaning stuff.
clean:
rm -fv $(clean_list)
dryclean:
@echo $(clean_list)
all: $(programs_list)

View file

@ -14,5 +14,4 @@ SUBS = \
GLOBAL_CXXFLAGS = -I . -I src -I src/libutil -I src/libstore -I src/libmain -I src/libexpr
include Makefile.lib
include mk/lib.mk

7
mk/clean.mk Normal file
View file

@ -0,0 +1,7 @@
clean_files :=
clean:
rm -fv $(clean_files)
dryclean:
@echo $(clean_files)

10
mk/dist.mk Normal file
View file

@ -0,0 +1,10 @@
dist_name = $(PACKAGE_NAME)-$(PACKAGE_VERSION)
dist_files :=
dist: $(dist_name).tar.bz2
$(dist_name).tar.bz2: $(dist_files)
$(QUIET) tar cvfj $@ $(dist_files) --transform 's,^,$(dist_name)/,'
clean_files += $(dist_name).tar.bz2

23
mk/install.mk Normal file
View file

@ -0,0 +1,23 @@
# Add a rule for creating $(1) as a directory. This template may be
# called multiple times for the same directory.
define create-dir =
ifndef $(1)_SEEN
$(1)_SEEN = 1
$(1):
install -d $(1)
endif
endef
# Add a rule for installing file $(2) in directory $(1). The
# directory will be created automatically.
define install-file-in =
install:: $(1)/$(notdir $(2))
$$(eval $$(call create-dir,$(1)))
$(1)/$(notdir $(2)): $(2) | $(1)
install -t $(1) $(2)
endef

52
mk/lib.mk Normal file
View file

@ -0,0 +1,52 @@
default: all
# Include Autoconf variables.
Makefile.config: Makefile.config.in
./config.status --file $@
include Makefile.config
# Initialise some variables.
QUIET = @
# Pass -fPIC if we're building dynamic libraries.
ifeq ($(BUILD_SHARED_LIBS), 1)
GLOBAL_CFLAGS += -fPIC
GLOBAL_CXXFLAGS += -fPIC
GLOBAL_LDFLAGS += -Wl,--no-copy-dt-needed-entries
endif
# Pass -g if we want debug info.
ifeq ($(BUILD_DEBUG), 1)
GLOBAL_CFLAGS += -g
GLOBAL_CXXFLAGS += -g
endif
include mk/clean.mk
include mk/dist.mk
include mk/install.mk
include mk/libraries.mk
include mk/programs.mk
include mk/patterns.mk
# Include all sub-Makefiles.
define include-sub-makefile =
d := $$(patsubst %/, %, $$(dir $(1)))
include $(1)
endef
$(foreach mf, $(SUBS), $(eval $(call include-sub-makefile, $(mf))))
# Instantiate libraries and programs.
$(foreach lib, $(LIBS), $(eval $(call build-library,$(lib))))
$(foreach prog, $(PROGRAMS), $(eval $(call build-program,$(prog))))
all: $(programs_list)

89
mk/libraries.mk Normal file
View file

@ -0,0 +1,89 @@
libs_list :=
# 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 containing the sources of the library, and
# where the (non-installed) library will be placed.
#
# - $(1)_SOURCES: the source files of the library.
#
# - $(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.
#
# - BUILD_SHARED_LIBS: if equal to 1, a dynamic library will be
# built, otherwise a static library.
#
# - libdir: the directory where the library will be installed (if
# required).
define build-library =
$(1)_NAME ?= $(1)
_d := $$(strip $$($(1)_DIR))
_srcs := $$(foreach src, $$($(1)_SOURCES), $$(_d)/$$(src))
$(1)_OBJS := $$(addsuffix .o, $$(basename $$(_srcs)))
_libs := $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_PATH))
$(1)_LDFLAGS_USE :=
$(1)_LDFLAGS_USE_INSTALLED :=
ifeq ($(BUILD_SHARED_LIBS), 1)
ifndef $(1)_ALLOW_UNDEFINED
$(1)_LDFLAGS += -z defs
endif
$(1)_PATH := $$(_d)/$$($(1)_NAME).so
$$($(1)_PATH): $$($(1)_OBJS) $$(_libs)
$(QUIET) $(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_DIR := $$(libdir)
$(1)_INSTALL_PATH := $$($(1)_INSTALL_DIR)/$$($(1)_NAME).so
_libs_final := $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_INSTALL_PATH))
$$($(1)_INSTALL_PATH): $$($(1)_OBJS) $$(_libs_final)
install -d $$($(1)_INSTALL_DIR)
$(QUIET) $(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$$($(1)_INSTALL_DIR) -Wl,-rpath,$$($(1)_INSTALL_DIR) -l$$(patsubst lib%,%,$$(strip $$($(1)_NAME)))
else
$(1)_PATH := $$(_d)/$$($(1)_NAME).a
$$($(1)_PATH): $$($(1)_OBJS)
$(QUIET) 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)))
include $$(wildcard $$(_d)/*.dep)
libs_list += $$($(1)_PATH)
clean_files += $$(_d)/*.a $$(_d)/*.so $$(_d)/*.o $$(_d)/*.dep
dist_files += $$(_srcs)
endef

5
mk/patterns.mk Normal file
View file

@ -0,0 +1,5 @@
%.o: %.cc
$(QUIET) $(CXX) -o $@ -c $< $(GLOBAL_CXXFLAGS) $(CXXFLAGS) $($@_CXXFLAGS) -MMD -MF $(basename $@).dep -MP
%.o: %.c
$(QUIET) $(CC) -o $@ -c $< $(GLOBAL_CFLAGS) $(CFLAGS) $($@_CFLAGS) -MMD -MF $(basename $@).dep -MP

56
mk/programs.mk Normal file
View file

@ -0,0 +1,56 @@
programs_list :=
# Build a program with symbolic name $(1). The program is defined by
# various variables prefixed by $(1)_:
#
# - $(1)_DIR: the directory containing the sources of the program, and
# 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.
#
# - bindir: the directory where the program will be installed.
define build-program =
_d := $$($(1)_DIR)
_srcs := $$(foreach src, $$($(1)_SOURCES), $$(_d)/$$(src))
$(1)_OBJS := $$(addsuffix .o, $$(basename $$(_srcs)))
_libs := $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_PATH))
$(1)_PATH := $$(_d)/$(1)
$$($(1)_PATH): $$($(1)_OBJS) $$(_libs)
$(QUIET) $(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:: $$($(1)_INSTALL_PATH)
ifeq ($(BUILD_SHARED_LIBS), 1)
_libs_final := $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_INSTALL_PATH))
$$($(1)_INSTALL_PATH): $$($(1)_OBJS) $$(_libs_final) | $$($(1)_INSTALL_DIR)
$(QUIET) $(CXX) -o $$@ $(GLOBAL_LDFLAGS) $$($(1)_OBJS) $$($(1)_LDFLAGS) $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_LDFLAGS_USE_INSTALLED))
else
$$($(1)_INSTALL_PATH): $$($(1)_PATH) | $$($(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)))
include $$(wildcard $$(_d)/*.dep)
programs_list += $$($(1)_PATH)
clean_files += $$($(1)_PATH) $$(_d)/*.o $$(_d)/*.dep
dist_files += $$(_srcs)
endef