ifeq ($(enable_stow),yes)
stow_pkg_dir := $(prefix)/pkgs
- DESTDIR ?= $(stow_pkg_dir)/$(project_name)-$(project_ver)
+ INSTALLDIR ?= $(DESTDIR)/$(stow_pkg_dir)/$(project_name)-$(project_ver)
else
- DESTDIR ?= $(prefix)
+ INSTALLDIR ?= $(DESTDIR)/$(prefix)
endif
-install_hdrs_dir := $(DESTDIR)/include/$(project_name)
-install_libs_dir := $(DESTDIR)/lib/$(project_name)
-install_exes_dir := $(DESTDIR)/bin
+install_hdrs_dir := $(INSTALLDIR)/include/$(project_name)
+install_libs_dir := $(INSTALLDIR)/lib
+install_exes_dir := $(INSTALLDIR)/bin
#-------------------------------------------------------------------------
# List of subprojects
CC := @CC@
CXX := @CXX@
-CFLAGS += @CFLAGS@
+CFLAGS += @CFLAGS@ -DPREFIX=\"$(prefix)\"
CPPFLAGS += @CPPFLAGS@
-CXXFLAGS += @CXXFLAGS@
-COMPILE := $(CXX) -MMD -MP $(CPPFLAGS) $(CFLAGS) $(CXXFLAGS) \
+CXXFLAGS += @CXXFLAGS@ -DPREFIX=\"$(prefix)\"
+COMPILE := $(CXX) -fPIC -MMD -MP $(CPPFLAGS) $(CXXFLAGS) \
$(sprojs_include)
-COMPILE_C := $(CC) -MMD -MP $(CPPFLAGS) $(CFLAGS) \
+COMPILE_C := $(CC) -fPIC -MMD -MP $(CPPFLAGS) $(CFLAGS) \
$(sprojs_include)
# Linker
# - LDFLAGS : Flags for the linker (eg. -L)
# - LIBS : Library flags (eg. -l)
+comma := ,
LD := $(CXX)
LDFLAGS := @LDFLAGS@
LIBS := @LIBS@
-LINK := $(LD) $(LDFLAGS)
+LINK := $(LD) -L. $(LDFLAGS) -Wl,-rpath,$(install_libs_dir) $(patsubst -L%,-Wl$(comma)-rpath$(comma)%,$(filter -L%,$(LDFLAGS)))
# Library creation
INSTALL_EXE := $(INSTALL) -m 555
STOW := @stow@
-#-------------------------------------------------------------------------
-# Range helper function. $(call range,3,6) generates the string 3 4 5
-#-------------------------------------------------------------------------
-
-range = $(shell _i=$(1); \
- while [[ $$_i -lt $(2) ]]; do \
- echo -n "$$_i "; \
- ((_i = _i + 1)); \
- done)
-
#-------------------------------------------------------------------------
# Include subproject makefile fragments
#-------------------------------------------------------------------------
# Build the object files for this subproject
+$(2)_pch := $$(patsubst %.h, %.h.gch, $$($(2)_precompiled_hdrs))
$(2)_objs := $$(patsubst %.cc, %.o, $$($(2)_srcs))
$(2)_c_objs := $$(patsubst %.c, %.o, $$($(2)_c_srcs))
$(2)_deps := $$(patsubst %.o, %.d, $$($(2)_objs))
-$(2)_c_deps := $$(patsubst %.o, %.d, $$($(2)_c_objs))
-$$($(2)_objs) : %.o : %.cc $(2)_gen_hdrs
+$(2)_deps += $$(patsubst %.o, %.d, $$($(2)_c_objs))
+$(2)_deps += $$(patsubst %.h, %.h.d, $$($(2)_precompiled_hdrs))
+$$($(2)_pch) : %.h.gch : %.h
+ $(COMPILE) $$< -o $$@
+# If using clang, don't depend (and thus don't build) precompiled headers
+$$($(2)_objs) : %.o : %.cc $$($(2)_gen_hdrs) $(if $(filter-out clang,$(CC)),$$($(2)_pch))
$(COMPILE) -c $$<
-$$($(2)_c_objs) : %.o : %.c
+$$($(2)_c_objs) : %.o : %.c $$($(2)_gen_hdrs)
$(COMPILE_C) -c $$<
-$(2)_junk += $$($(2)_objs) $$($(2)_c_objs) $$($(2)_deps) $$($(2)_c_deps)
-
-# Build a library for this subproject
-
-lib$(1).a : $$($(2)_objs) $$($(2)_c_objs)
- $(AR) rcv $$@ $$^
- $(RANLIB) $$@
-
-$(2)_junk += lib$(1).a
+$(2)_junk += $$($(2)_pch) $$($(2)_objs) $$($(2)_c_objs) $$($(2)_deps) \
+ $$($(2)_gen_hdrs)
# Reverse the dependency list so that a given subproject only depends on
# subprojects listed to its right. This is the correct order for linking
$(2)_reverse_deps := $$(call reverse_list,$$($(2)_subproject_deps))
+# Build a library for this subproject
+
+$(2)_lib_libs := $$($(2)_reverse_deps)
+$(2)_lib_libnames := $$(patsubst %, lib%.so, $$($(2)_lib_libs))
+$(2)_lib_libarg := $$(patsubst %, -l%, $$($(2)_lib_libs))
+
+lib$(1).so : $$($(2)_objs) $$($(2)_c_objs) $$($(2)_lib_libnames)
+ $(LINK) -shared -o $$@ $(if $(filter Darwin,$(shell uname -s)),-install_name $(install_libs_dir)/$$@) $$^ $$($(2)_lib_libarg) $(LIBS)
+
+$(2)_junk += lib$(1).so
+
# Build unit tests
$(2)_test_objs := $$(patsubst %.cc, %.o, $$($(2)_test_srcs))
$(2)_test_exes := $$(patsubst %.t.cc, %-utst, $$($(2)_test_srcs))
$(2)_test_outs := $$(patsubst %, %.out, $$($(2)_test_exes))
$(2)_test_libs := $(1) $$($(2)_reverse_deps) utst
-$(2)_test_libnames := $$(patsubst %, lib%.a, $$($(2)_test_libs))
-$(2)_test_libarg := -L. $$(patsubst %, -l%, $$($(2)_test_libs))
+$(2)_test_libnames := $$(patsubst %, lib%.so, $$($(2)_test_libs))
+$(2)_test_libarg := $$(patsubst %, -l%, $$($(2)_test_libs))
$$($(2)_test_objs) : %.o : %.cc
$(COMPILE) -c $$<
$(2)_prog_deps := $$(patsubst %.o, %.d, $$($(2)_prog_objs))
$(2)_prog_exes := $$(patsubst %.cc, %, $$($(2)_prog_srcs))
$(2)_prog_libs := $(1) $$($(2)_reverse_deps)
-$(2)_prog_libnames := $$(patsubst %, lib%.a, $$($(2)_prog_libs))
-$(2)_prog_libarg := -L. $$(patsubst %, -l%, $$($(2)_prog_libs))
+$(2)_prog_libnames := $$(patsubst %, lib%.so, $$($(2)_prog_libs))
+$(2)_prog_libarg := $$(patsubst %, -l%, $$($(2)_prog_libs))
$$($(2)_prog_objs) : %.o : %.cc
$(COMPILE) -c $$<
$(2)_install_prog_deps := $$(patsubst %.o, %.d, $$($(2)_install_prog_objs))
$(2)_install_prog_exes := $$(patsubst %.cc, %, $$($(2)_install_prog_srcs))
-$$($(2)_install_prog_objs) : %.o : %.cc
+$$($(2)_install_prog_objs) : %.o : %.cc $$($(2)_gen_hdrs)
$(COMPILE) -c $$<
$$($(2)_install_prog_exes) : % : %.o $$($(2)_prog_libnames)
# Subproject specific targets
-all-$(1) : lib$(1).a $$($(2)_install_prog_exes)
+all-$(1) : lib$(1).so $$($(2)_install_prog_exes)
check-$(1) : $$($(2)_test_outs)
echo; grep -h -e'Unit Tests' -e'FAILED' -e'Segementation' $$^; echo
# Update running variables
-libs += lib$(1).a
+libs += lib$(1).so
objs += $$($(2)_objs)
srcs += $$(addprefix $(src_dir)/$(1)/, $$($(2)_srcs))
-hdrs += $$(addprefix $(src_dir)/$(1)/, $$($(2)_hdrs) $$($(2)_gen_hdrs))
+hdrs += $$(addprefix $(src_dir)/$(1)/, $$($(2)_hdrs)) $$($(2)_gen_hdrs)
junk += $$($(2)_junk)
deps += $$($(2)_deps)
test_outs += $$($(2)_test_outs)
-install_hdrs += $$(addprefix $(src_dir)/$(1)/, $$($(2)_hdrs) $$($(2)_gen_hdrs))
-install_libs += lib$(1).a
+install_hdrs += $$(addprefix $(src_dir)/$(1)/, $$($(2)_hdrs)) $$($(2)_gen_hdrs)
+install_libs += lib$(1).so
install_exes += $$($(2)_install_prog_exes)
+install_pcs += riscv-$(1).pc
endef
# Installation
#-------------------------------------------------------------------------
-install-hdrs : $(install_hdrs)
+install-hdrs : $(install_hdrs) config.h
$(MKINSTALLDIRS) $(install_hdrs_dir)
for file in $^; \
do \
$(INSTALL_EXE) $$file $(install_exes_dir); \
done
-install : install-hdrs install-libs install-exes
+install-pc : $(install_pcs)
+ $(MKINSTALLDIRS) $(install_libs_dir)/pkgconfig/
+ for file in $^; \
+ do \
+ $(INSTALL_HDR) $$file $(install_libs_dir)/pkgconfig/; \
+ done
+
+install : install-hdrs install-libs install-exes install-pc
ifeq ($(enable_stow),yes)
$(MKINSTALLDIRS) $(stow_pkg_dir)
cd $(stow_pkg_dir) && \