From f2f6a6e3966e59b3d13560053a3d3cb39efedf6c Mon Sep 17 00:00:00 2001 From: Eric Biggers Date: Sun, 25 Oct 2020 21:24:17 -0700 Subject: [PATCH] Makefile: support linking programs to shared library Most Linux distributions want dynamic linking rather than static linking, so support this via 'make USE_SHARED_LIB=1'. --- Makefile | 19 ++++++++++++++----- scripts/run_tests.sh | 13 +++++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 2036bfa..276d75d 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,9 @@ # # Define V=1 to enable "verbose" mode, showing all executed commands. # +# Define USE_SHARED_LIB to link the binaries to the shared library version of +# libdeflate rather than to the static library version. +# # Define DECOMPRESSION_ONLY to omit all compression code, building a # decompression-only library. If doing this, you must also build a specific # library target such as 'libdeflate.a', as the programs will no longer compile. @@ -138,6 +141,7 @@ endif # Rebuild if a user-specified setting that affects the build changed. .build-config: FORCE @flags=$$( \ + echo 'USE_SHARED_LIB=$(USE_SHARED_LIB)'; \ echo 'DECOMPRESSION_ONLY=$(DECOMPRESSION_ONLY)'; \ echo 'DISABLE_GZIP=$(DISABLE_GZIP)'; \ echo 'DISABLE_ZLIB=$(DISABLE_ZLIB)'; \ @@ -267,12 +271,17 @@ $(ALL_PROG_OBJ): %.o: %.c $(ALL_PROG_COMMON_HEADERS) $(COMMON_HEADERS) \ # Note: the test programs are not compiled by default. One reason is that the # test programs must be linked with zlib for doing comparisons. -$(NONTEST_PROGRAMS): %$(PROG_SUFFIX): programs/%.o $(PROG_COMMON_OBJ) \ - $(STATIC_LIB) +ifdef USE_SHARED_LIB +LIB := $(SHARED_LIB) +else +LIB := $(STATIC_LIB) +endif + +$(NONTEST_PROGRAMS): %$(PROG_SUFFIX): programs/%.o $(PROG_COMMON_OBJ) $(LIB) $(QUIET_CCLD) $(CC) -o $@ $(LDFLAGS) $(PROG_CFLAGS) $+ $(TEST_PROGRAMS): %$(PROG_SUFFIX): programs/%.o $(PROG_COMMON_OBJ) \ - $(TEST_PROG_COMMON_OBJ) $(STATIC_LIB) + $(TEST_PROG_COMMON_OBJ) $(LIB) $(QUIET_CCLD) $(CC) -o $@ $(LDFLAGS) $(PROG_CFLAGS) $+ -lz ifdef HARD_LINKS @@ -322,9 +331,9 @@ test_programs:$(TEST_PROGRAMS) # A minimal 'make check' target. This only runs some quick tests; # use scripts/run_tests.sh if you want to run the full tests. check:test_programs - ./benchmark$(PROG_SUFFIX) < ./benchmark$(PROG_SUFFIX) + LD_LIBRARY_PATH=. ./benchmark$(PROG_SUFFIX) < ./benchmark$(PROG_SUFFIX) for prog in test_*; do \ - ./$$prog || exit 1; \ + LD_LIBRARY_PATH=. ./$$prog || exit 1; \ done # Run the clang static analyzer. diff --git a/scripts/run_tests.sh b/scripts/run_tests.sh index 4dc3763..5b2d267 100755 --- a/scripts/run_tests.sh +++ b/scripts/run_tests.sh @@ -203,6 +203,18 @@ check_symbol_prefixes() { fi } +test_use_shared_lib() { + log "Testing USE_SHARED_LIB=1" + $MAKE gzip + if ldd gzip | grep -q 'libdeflate.so'; then + fail "Binary should be statically linked by default" + fi + $MAKE USE_SHARED_LIB=1 all check + if ! ldd gzip | grep -q 'libdeflate.so'; then + fail "Binary isn't dynamically linked" + fi +} + install_uninstall_tests() { local shell @@ -280,6 +292,7 @@ run_tests() { install_uninstall_tests check_symbol_prefixes + test_use_shared_lib } ###############################################################################