From 3f69873c9969961a59cdd4a491487175a0b1f0b4 Mon Sep 17 00:00:00 2001 From: Nirbheek Chauhan Date: Thu, 13 Nov 2014 18:48:33 +0530 Subject: [PATCH] Makefile: Fix shared library building on various OSes Versioned for OS not in (Windows, Android), and create a .dll.a implib on Windows --- Makefile | 29 +++++++++++++++++++++++------ build/platform-android.mk | 4 ++++ build/platform-darwin.mk | 5 +++++ build/platform-freebsd.mk | 3 +++ build/platform-linux.mk | 3 +++ build/platform-mingw_nt.mk | 3 +++ 6 files changed, 41 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 2dd45cf1..cb919fe3 100644 --- a/Makefile +++ b/Makefile @@ -54,6 +54,7 @@ CFLAGS += -fsanitize=address LDFLAGS += -fsanitize=address endif +SHAREDLIBVERSION=0 include $(SRC_PATH)build/platform-$(OS).mk @@ -179,33 +180,41 @@ endif endif ifneq (ios, $(OS)) -libraries: $(LIBPREFIX)$(PROJECT_NAME).$(LIBSUFFIX) $(LIBPREFIX)$(PROJECT_NAME).$(SHAREDLIBSUFFIX) +libraries: $(LIBPREFIX)$(PROJECT_NAME).$(LIBSUFFIX) $(LIBPREFIX)$(PROJECT_NAME).$(SHAREDLIBSUFFIXVER) else libraries: $(LIBPREFIX)$(PROJECT_NAME).$(LIBSUFFIX) endif -LIBRARIES += $(LIBPREFIX)$(PROJECT_NAME).$(LIBSUFFIX) $(LIBPREFIX)$(PROJECT_NAME).$(SHAREDLIBSUFFIX) +LIBRARIES += $(LIBPREFIX)$(PROJECT_NAME).$(LIBSUFFIX) $(LIBPREFIX)$(PROJECT_NAME).$(SHAREDLIBSUFFIXVER) $(LIBPREFIX)$(PROJECT_NAME).$(LIBSUFFIX): $(ENCODER_OBJS) $(DECODER_OBJS) $(PROCESSING_OBJS) $(COMMON_OBJS) $(QUIET)rm -f $@ $(QUIET_AR)$(AR) $(AR_OPTS) $+ -$(LIBPREFIX)$(PROJECT_NAME).$(SHAREDLIBSUFFIX): $(ENCODER_OBJS) $(DECODER_OBJS) $(PROCESSING_OBJS) $(COMMON_OBJS) +$(LIBPREFIX)$(PROJECT_NAME).$(SHAREDLIBSUFFIXVER): $(ENCODER_OBJS) $(DECODER_OBJS) $(PROCESSING_OBJS) $(COMMON_OBJS) $(QUIET)rm -f $@ $(QUIET_CXX)$(CXX) $(SHARED) $(CXX_LINK_O) $+ $(LDFLAGS) $(SHLDFLAGS) + if [ "$(SHAREDLIBSUFFIXVER)" != "$(SHAREDLIBSUFFIX)" ]; then \ + ln -sfn $(LIBPREFIX)$(PROJECT_NAME).$(SHAREDLIBSUFFIXVER) \ + $(LIBPREFIX)$(PROJECT_NAME).$(SHAREDLIBSUFFIX); \ + fi ifeq ($(HAVE_GMP_API),Yes) -plugin: $(LIBPREFIX)$(MODULE_NAME).$(SHAREDLIBSUFFIX) -LIBRARIES += $(LIBPREFIX)$(MODULE_NAME).$(SHAREDLIBSUFFIX) +plugin: $(LIBPREFIX)$(MODULE_NAME).$(SHAREDLIBSUFFIXVER) +LIBRARIES += $(LIBPREFIX)$(MODULE_NAME).$(SHAREDLIBSUFFIXVER) else plugin: @echo "./gmp-api : No such file or directory." @echo "You do not have gmp-api. Run make gmp-bootstrap to get the gmp-api headers." endif -$(LIBPREFIX)$(MODULE_NAME).$(SHAREDLIBSUFFIX): $(MODULE_OBJS) $(ENCODER_OBJS) $(DECODER_OBJS) $(PROCESSING_OBJS) $(COMMON_OBJS) +$(LIBPREFIX)$(MODULE_NAME).$(SHAREDLIBSUFFIXVER): $(MODULE_OBJS) $(ENCODER_OBJS) $(DECODER_OBJS) $(PROCESSING_OBJS) $(COMMON_OBJS) $(QUIET)rm -f $@ $(QUIET_CXX)$(CXX) $(SHARED) $(CXX_LINK_O) $+ $(LDFLAGS) $(SHLDFLAGS) $(MODULE_LDFLAGS) + if [ "$(SHAREDLIBSUFFIXVER)" != "$(SHAREDLIBSUFFIX)" ]; then \ + ln -sfn $(LIBPREFIX)$(MODULE_NAME).$(SHAREDLIBSUFFIXVER) \ + $(LIBPREFIX)$(MODULE_NAME).$(SHAREDLIBSUFFIX); \ + fi $(PROJECT_NAME).pc: $(PROJECT_NAME).pc.in @sed -e 's;@prefix@;$(PREFIX);' -e 's;@VERSION@;$(VERSION);' < $(PROJECT_NAME).pc.in > $(PROJECT_NAME).pc @@ -220,7 +229,15 @@ install-static: $(LIBPREFIX)$(PROJECT_NAME).$(LIBSUFFIX) install-headers install-shared: $(LIBPREFIX)$(PROJECT_NAME).$(SHAREDLIBSUFFIX) install-headers $(PROJECT_NAME).pc mkdir -p $(PREFIX)/lib + mkdir -p $(PREFIX)/bin install -m 755 $(LIBPREFIX)$(PROJECT_NAME).$(SHAREDLIBSUFFIX) $(PREFIX)/lib + if [ -L $(LIBPREFIX)$(PROJECT_NAME).$(SHAREDLIBSUFFIX) ]; then \ + cp -a $(LIBPREFIX)$(PROJECT_NAME).$(SHAREDLIBSUFFIX) $(SHAREDLIB_DIR); \ + fi + if [ -f $(LIBPREFIX)$(PROJECT_NAME).$(SHAREDLIBSUFFIX).a ]; then \ + install -m 644 $(LIBPREFIX)$(PROJECT_NAME).$(SHAREDLIBSUFFIX).a \ + $(PREFIX)/lib; \ + fi mkdir -p $(PREFIX)/lib/pkgconfig install -m 444 $(PROJECT_NAME).pc $(PREFIX)/lib/pkgconfig ifneq ($(EXTRA_LIBRARY),) diff --git a/build/platform-android.mk b/build/platform-android.mk index 17981adb..a09386ad 100644 --- a/build/platform-android.mk +++ b/build/platform-android.mk @@ -1,6 +1,10 @@ ARCH = arm include $(SRC_PATH)build/arch.mk +SHAREDLIB_DIR = $(PREFIX)/lib SHAREDLIBSUFFIX = so +# Android APK/JARs expect libraries to be unversioned +SHAREDLIBSUFFIXVER=$(SHAREDLIBSUFFIX) +SHLDFLAGS = NDKLEVEL = 12 ifeq ($(ARCH), arm) ifneq ($(APP_ABI), armeabi) diff --git a/build/platform-darwin.mk b/build/platform-darwin.mk index d55731b9..226e3dc4 100644 --- a/build/platform-darwin.mk +++ b/build/platform-darwin.mk @@ -1,5 +1,10 @@ include $(SRC_PATH)build/arch.mk +SHAREDLIB_DIR = $(PREFIX)/lib SHAREDLIBSUFFIX = dylib +SHAREDLIBSUFFIXVER=$(SHAREDLIBVERSION).$(SHAREDLIBSUFFIX) +SHLDFLAGS = -dynamiclib -twolevel_namespace -undefined dynamic_lookup \ + -fno-common -headerpad_max_install_names -install_name \ + $(SHAREDLIB_DIR)/$(LIBPREFIX)$(PROJECT_NAME).$(SHAREDLIBSUFFIXVER) SHARED = -dynamiclib CFLAGS += -Wall -fPIC -MMD -MP LDFLAGS += -lpthread diff --git a/build/platform-freebsd.mk b/build/platform-freebsd.mk index 8a0021d3..3d0feaae 100644 --- a/build/platform-freebsd.mk +++ b/build/platform-freebsd.mk @@ -1,5 +1,8 @@ include $(SRC_PATH)build/arch.mk +SHAREDLIB_DIR = $(PREFIX)/lib SHAREDLIBSUFFIX = so +SHAREDLIBSUFFIXVER=$(SHAREDLIBSUFFIX).$(SHAREDLIBVERSION) +SHLDFLAGS = -Wl,-soname,$(LIBPREFIX)$(PROJECT_NAME).$(SHAREDLIBSUFFIXVER) CFLAGS += -fPIC LDFLAGS += -lpthread ifeq ($(ASM_ARCH), x86) diff --git a/build/platform-linux.mk b/build/platform-linux.mk index e7ff27b2..e98b972a 100644 --- a/build/platform-linux.mk +++ b/build/platform-linux.mk @@ -1,5 +1,8 @@ include $(SRC_PATH)build/arch.mk +SHAREDLIB_DIR = $(PREFIX)/lib SHAREDLIBSUFFIX = so +SHAREDLIBSUFFIXVER=$(SHAREDLIBSUFFIX).$(SHAREDLIBVERSION) +SHLDFLAGS = -Wl,-soname,$(LIBPREFIX)$(PROJECT_NAME).$(SHAREDLIBSUFFIXVER) CFLAGS += -Wall -fno-strict-aliasing -fPIC -MMD -MP LDFLAGS += -lpthread ifeq ($(ASM_ARCH), x86) diff --git a/build/platform-mingw_nt.mk b/build/platform-mingw_nt.mk index 8ea64c7a..96c3b246 100644 --- a/build/platform-mingw_nt.mk +++ b/build/platform-mingw_nt.mk @@ -1,5 +1,8 @@ include $(SRC_PATH)build/x86-common.mk +SHAREDLIB_DIR = $(PREFIX)/bin SHAREDLIBSUFFIX = dll +SHAREDLIBSUFFIXVER=$(SHAREDLIBSUFFIX) +SHLDFLAGS = -Wl,--out-implib,$(LIBPREFIX)$(PROJECT_NAME).dll.a CFLAGS += -MMD -MP LDFLAGS += ifeq ($(ENABLE64BIT), Yes)