Blob Blame History Raw
From bc32865a237f15cd4aaaa010b4f6a4f2fae71367 Mon Sep 17 00:00:00 2001
From: Yann Collet <cyan@fb.com>
Date: Thu, 14 Sep 2017 09:44:34 -0700
Subject: [PATCH] make : added lib target

make install now also installs static and dynamic libraries
---
 .gitignore |  6 ++++
 Makefile   | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++-----------
 2 files changed, 82 insertions(+), 16 deletions(-)

diff --git a/.gitignore b/.gitignore
index 6e41348..36639c6 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,9 @@
+# objects
+*.o
+
+# libraries
+libxxhash.*
+
 # Executables
 xxh32sum
 xxh64sum
diff --git a/Makefile b/Makefile
index f1b4238..98f6048 100644
--- a/Makefile
+++ b/Makefile
@@ -25,9 +25,12 @@
 # ################################################################
 
 # Version numbers
-LIBVER_MAJOR:=`sed -n '/define XXH_VERSION_MAJOR/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < xxhash.h`
-LIBVER_MINOR:=`sed -n '/define XXH_VERSION_MINOR/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < xxhash.h`
-LIBVER_PATCH:=`sed -n '/define XXH_VERSION_RELEASE/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < xxhash.h`
+LIBVER_MAJOR_SCRIPT:=`sed -n '/define XXH_VERSION_MAJOR/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < xxhash.h`
+LIBVER_MINOR_SCRIPT:=`sed -n '/define XXH_VERSION_MINOR/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < xxhash.h`
+LIBVER_PATCH_SCRIPT:=`sed -n '/define XXH_VERSION_RELEASE/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < xxhash.h`
+LIBVER_MAJOR := $(shell echo $(LIBVER_MAJOR_SCRIPT))
+LIBVER_MINOR := $(shell echo $(LIBVER_MINOR_SCRIPT))
+LIBVER_PATCH := $(shell echo $(LIBVER_PATCH_SCRIPT))
 LIBVER := $(LIBVER_MAJOR).$(LIBVER_MINOR).$(LIBVER_PATCH)
 
 CFLAGS ?= -O3
@@ -46,21 +49,61 @@ else
 EXT =
 endif
 
+# OS X linker doesn't support -soname, and use different extension
+# see : https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/DynamicLibraryDesignGuidelines.html
+ifeq ($(shell uname), Darwin)
+	SHARED_EXT = dylib
+	SHARED_EXT_MAJOR = $(LIBVER_MAJOR).$(SHARED_EXT)
+	SHARED_EXT_VER = $(LIBVER).$(SHARED_EXT)
+	SONAME_FLAGS = -install_name $(LIBDIR)/libxxhash.$(SHARED_EXT_MAJOR) -compatibility_version $(LIBVER_MAJOR) -current_version $(LIBVER)
+else
+	SONAME_FLAGS = -Wl,-soname=libxxhash.$(SHARED_EXT).$(LIBVER_MAJOR)
+	SHARED_EXT = so
+	SHARED_EXT_MAJOR = $(SHARED_EXT).$(LIBVER_MAJOR)
+	SHARED_EXT_VER = $(SHARED_EXT).$(LIBVER)
+endif
+
+LIBXXH = libxxhash.$(SHARED_EXT_VER)
+
+
 .PHONY: default
-default: xxhsum
+default: lib xxhsum
 
 .PHONY: all
-all: xxhsum xxhsum32 xxhsum_inlinedXXH
+all: lib xxhsum xxhsum32 xxhsum_inlinedXXH
 
 xxhsum32: CFLAGS += -m32
 xxhsum xxhsum32: xxhash.c xxhsum.c
-	$(CC)      $(FLAGS) $^ -o $@$(EXT)
+	$(CC) $(FLAGS) $^ -o $@$(EXT)
 	ln -sf $@ xxh32sum
 	ln -sf $@ xxh64sum
 
 xxhsum_inlinedXXH: xxhsum.c
 	$(CC) $(FLAGS) -DXXH_PRIVATE_API $^ -o $@$(EXT)
 
+
+# library
+
+libxxhash.a: ARFLAGS = rcs
+libxxhash.a: xxhash.o
+	@echo compiling static library
+	@$(AR) $(ARFLAGS) $@ $^
+
+$(LIBXXH): LDFLAGS += -shared -fPIC
+$(LIBXXH): xxhash.c
+	@echo compiling dynamic library $(LIBVER)
+	@$(CC) $(FLAGS) $^ $(LDFLAGS) $(SONAME_FLAGS) -o $@
+	@echo creating versioned links
+	@ln -sf $@ libxxhash.$(SHARED_EXT_MAJOR)
+	@ln -sf $@ libxxhash.$(SHARED_EXT)
+
+libxxhash : $(LIBXXH)
+
+lib: libxxhash.a libxxhash
+
+
+# tests
+
 .PHONY: test
 test: xxhsum
 	# stdin
@@ -154,7 +197,8 @@ test-all: clean all namespaceTest test test32 test-xxhsum-c clean-xxhsum-c \
 	armtest clangtest gpptest c90test test-mem usan staticAnalyze
 
 clean: clean-xxhsum-c
-	@$(RM) -f core *.o xxhsum$(EXT) xxhsum32$(EXT) xxhsum_inlinedXXH$(EXT) xxh32sum xxh64sum
+	@$(RM) core *.o libxxhash.*
+	@$(RM) xxhsum$(EXT) xxhsum32$(EXT) xxhsum_inlinedXXH$(EXT) xxh32sum xxh64sum
 	@echo cleaning completed
 
 
@@ -170,6 +214,10 @@ DESTDIR     ?=
 prefix      ?= /usr/local
 PREFIX      ?= $(prefix)
 exec_prefix ?= $(PREFIX)
+libdir      ?= $(exec_prefix)/lib
+LIBDIR      ?= $(libdir)
+includedir  ?= $(PREFIX)/include
+INCLUDEDIR  ?= $(includedir)
 bindir      ?= $(exec_prefix)/bin
 BINDIR      ?= $(bindir)
 datarootdir ?= $(PREFIX)/share
@@ -193,8 +241,15 @@ INSTALL_DATA    ?= $(INSTALL) -m 644
 
 
 .PHONY: install
-install: xxhsum
-	@echo Installing binaries
+install: lib xxhsum
+	@echo Installing libxxhash
+	@$(INSTALL_DATA) libxxhash.a $(DESTDIR)$(LIBDIR)
+	@$(INSTALL_PROGRAM) $(LIBXXH) $(DESTDIR)$(LIBDIR)
+	@ln -sf $(LIBXXH) $(DESTDIR)$(LIBDIR)/libxxhash.$(SHARED_EXT_MAJOR)
+	@ln -sf $(LIBXXH) $(DESTDIR)$(LIBDIR)/libxxhash.$(SHARED_EXT)
+	@$(INSTALL) -d -m 755 $(DESTDIR)$(INCLUDEDIR)   # includes
+	@$(INSTALL_DATA) xxhash.h $(DESTDIR)$(INCLUDEDIR)
+	@echo Installing xxhsum
 	@$(INSTALL) -d -m 755 $(DESTDIR)$(BINDIR)/ $(DESTDIR)$(MANDIR)/
 	@$(INSTALL_PROGRAM) xxhsum $(DESTDIR)$(BINDIR)/xxhsum
 	@ln -sf xxhsum $(DESTDIR)$(BINDIR)/xxh32sum
@@ -203,16 +258,21 @@ install: xxhsum
 	@$(INSTALL_DATA) xxhsum.1 $(DESTDIR)$(MANDIR)/xxhsum.1
 	@ln -sf xxhsum.1 $(DESTDIR)$(MANDIR)/xxh32sum.1
 	@ln -sf xxhsum.1 $(DESTDIR)$(MANDIR)/xxh64sum.1
-	@echo xxhsum installation completed
+	@echo xxhash installation completed
 
 .PHONY: uninstall
 uninstall:
-	$(RM) $(DESTDIR)$(BINDIR)/xxh32sum
-	$(RM) $(DESTDIR)$(BINDIR)/xxh64sum
-	$(RM) $(DESTDIR)$(BINDIR)/xxhsum
-	$(RM) $(DESTDIR)$(MANDIR)/xxh32sum.1
-	$(RM) $(DESTDIR)$(MANDIR)/xxh64sum.1
-	$(RM) $(DESTDIR)$(MANDIR)/xxhsum.1
+	@$(RM) $(DESTDIR)$(LIBDIR)/libxxhash.a
+	@$(RM) $(DESTDIR)$(LIBDIR)/libxxhash.$(SHARED_EXT)
+	@$(RM) $(DESTDIR)$(LIBDIR)/libxxhash.$(SHARED_EXT_MAJOR)
+	@$(RM) $(DESTDIR)$(LIBDIR)/$(LIBXXH)
+	@$(RM) $(DESTDIR)$(INCLUDEDIR)/xxhash.h
+	@$(RM) $(DESTDIR)$(BINDIR)/xxh32sum
+	@$(RM) $(DESTDIR)$(BINDIR)/xxh64sum
+	@$(RM) $(DESTDIR)$(BINDIR)/xxhsum
+	@$(RM) $(DESTDIR)$(MANDIR)/xxh32sum.1
+	@$(RM) $(DESTDIR)$(MANDIR)/xxh64sum.1
+	@$(RM) $(DESTDIR)$(MANDIR)/xxhsum.1
 	@echo xxhsum successfully uninstalled
 
 endif