xref: /freebsd/sys/contrib/zstd/lib/Makefile (revision 31d62a73c2e6ac0ff413a7a17700ffc7dce254ef)
1# ################################################################
2# Copyright (c) 2015-present, Yann Collet, Facebook, Inc.
3# All rights reserved.
4#
5# This source code is licensed under both the BSD-style license (found in the
6# LICENSE file in the root directory of this source tree) and the GPLv2 (found
7# in the COPYING file in the root directory of this source tree).
8# ################################################################
9
10# Version numbers
11LIBVER_MAJOR_SCRIPT:=`sed -n '/define ZSTD_VERSION_MAJOR/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < ./zstd.h`
12LIBVER_MINOR_SCRIPT:=`sed -n '/define ZSTD_VERSION_MINOR/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < ./zstd.h`
13LIBVER_PATCH_SCRIPT:=`sed -n '/define ZSTD_VERSION_RELEASE/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < ./zstd.h`
14LIBVER_SCRIPT:= $(LIBVER_MAJOR_SCRIPT).$(LIBVER_MINOR_SCRIPT).$(LIBVER_PATCH_SCRIPT)
15LIBVER_MAJOR := $(shell echo $(LIBVER_MAJOR_SCRIPT))
16LIBVER_MINOR := $(shell echo $(LIBVER_MINOR_SCRIPT))
17LIBVER_PATCH := $(shell echo $(LIBVER_PATCH_SCRIPT))
18LIBVER := $(shell echo $(LIBVER_SCRIPT))
19VERSION?= $(LIBVER)
20
21CPPFLAGS+= -I. -I./common -DXXH_NAMESPACE=ZSTD_
22ifeq ($(OS),Windows_NT)   # MinGW assumed
23CPPFLAGS   += -D__USE_MINGW_ANSI_STDIO   # compatibility with %zu formatting
24endif
25CFLAGS  ?= -O3
26DEBUGFLAGS= -Wall -Wextra -Wcast-qual -Wcast-align -Wshadow \
27            -Wstrict-aliasing=1 -Wswitch-enum -Wdeclaration-after-statement \
28            -Wstrict-prototypes -Wundef -Wpointer-arith -Wformat-security \
29            -Wvla -Wformat=2 -Winit-self -Wfloat-equal -Wwrite-strings \
30            -Wredundant-decls -Wmissing-prototypes
31CFLAGS  += $(DEBUGFLAGS) $(MOREFLAGS)
32FLAGS    = $(CPPFLAGS) $(CFLAGS)
33
34GREP = grep --color=never
35
36ZSTDCOMMON_FILES := $(sort $(wildcard common/*.c))
37ZSTDCOMP_FILES := $(sort $(wildcard compress/*.c))
38ZSTDDECOMP_FILES := $(sort $(wildcard decompress/*.c))
39ZDICT_FILES := $(sort $(wildcard dictBuilder/*.c))
40ZDEPR_FILES := $(sort $(wildcard deprecated/*.c))
41ZSTD_FILES := $(ZSTDCOMMON_FILES)
42
43ZSTD_LEGACY_SUPPORT ?= 5
44ZSTD_LIB_COMPRESSION ?= 1
45ZSTD_LIB_DECOMPRESSION ?= 1
46ZSTD_LIB_DICTBUILDER ?= 1
47ZSTD_LIB_DEPRECATED ?= 1
48
49ifeq ($(ZSTD_LIB_COMPRESSION), 0)
50	ZSTD_LIB_DICTBUILDER = 0
51	ZSTD_LIB_DEPRECATED = 0
52endif
53
54ifeq ($(ZSTD_LIB_DECOMPRESSION), 0)
55	ZSTD_LEGACY_SUPPORT = 0
56	ZSTD_LIB_DEPRECATED = 0
57endif
58
59ifneq ($(ZSTD_LIB_COMPRESSION), 0)
60	ZSTD_FILES += $(ZSTDCOMP_FILES)
61endif
62
63ifneq ($(ZSTD_LIB_DECOMPRESSION), 0)
64	ZSTD_FILES += $(ZSTDDECOMP_FILES)
65endif
66
67ifneq ($(ZSTD_LIB_DEPRECATED), 0)
68	ZSTD_FILES += $(ZDEPR_FILES)
69endif
70
71ifneq ($(ZSTD_LIB_DICTBUILDER), 0)
72	ZSTD_FILES += $(ZDICT_FILES)
73endif
74
75ifneq ($(ZSTD_LEGACY_SUPPORT), 0)
76ifeq ($(shell test $(ZSTD_LEGACY_SUPPORT) -lt 8; echo $$?), 0)
77	ZSTD_FILES += $(shell ls legacy/*.c | $(GREP) 'v0[$(ZSTD_LEGACY_SUPPORT)-7]')
78endif
79	CPPFLAGS += -I./legacy
80endif
81CPPFLAGS  += -DZSTD_LEGACY_SUPPORT=$(ZSTD_LEGACY_SUPPORT)
82
83ZSTD_OBJ   := $(patsubst %.c,%.o,$(ZSTD_FILES))
84
85# macOS linker doesn't support -soname, and use different extension
86# see : https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/DynamicLibraryDesignGuidelines.html
87ifeq ($(shell uname), Darwin)
88	SHARED_EXT = dylib
89	SHARED_EXT_MAJOR = $(LIBVER_MAJOR).$(SHARED_EXT)
90	SHARED_EXT_VER = $(LIBVER).$(SHARED_EXT)
91	SONAME_FLAGS = -install_name $(LIBDIR)/libzstd.$(SHARED_EXT_MAJOR) -compatibility_version $(LIBVER_MAJOR) -current_version $(LIBVER)
92else
93	SONAME_FLAGS = -Wl,-soname=libzstd.$(SHARED_EXT).$(LIBVER_MAJOR)
94	SHARED_EXT = so
95	SHARED_EXT_MAJOR = $(SHARED_EXT).$(LIBVER_MAJOR)
96	SHARED_EXT_VER = $(SHARED_EXT).$(LIBVER)
97endif
98
99
100.PHONY: default all clean install uninstall
101
102default: lib-release
103
104all: lib
105
106libzstd.a: ARFLAGS = rcs
107libzstd.a: $(ZSTD_OBJ)
108	@echo compiling static library
109	@$(AR) $(ARFLAGS) $@ $^
110
111libzstd.a-mt: CPPFLAGS += -DZSTD_MULTITHREAD
112libzstd.a-mt: libzstd.a
113
114ifneq (,$(filter Windows%,$(OS)))
115
116LIBZSTD = dll\libzstd.dll
117$(LIBZSTD): $(ZSTD_FILES)
118	@echo compiling dynamic library $(LIBVER)
119	@$(CC) $(FLAGS) -DZSTD_DLL_EXPORT=1 -shared $^ -o $@
120	dlltool -D $@ -d dll\libzstd.def -l dll\libzstd.lib
121
122else
123
124LIBZSTD = libzstd.$(SHARED_EXT_VER)
125$(LIBZSTD): LDFLAGS += -shared -fPIC -fvisibility=hidden
126$(LIBZSTD): $(ZSTD_FILES)
127	@echo compiling dynamic library $(LIBVER)
128	@$(CC) $(FLAGS) $^ $(LDFLAGS) $(SONAME_FLAGS) -o $@
129	@echo creating versioned links
130	@ln -sf $@ libzstd.$(SHARED_EXT_MAJOR)
131	@ln -sf $@ libzstd.$(SHARED_EXT)
132
133endif
134
135
136libzstd : $(LIBZSTD)
137
138libzstd-mt : CPPFLAGS += -DZSTD_MULTITHREAD
139libzstd-mt : libzstd
140
141lib: libzstd.a libzstd
142
143lib-mt: CPPFLAGS += -DZSTD_MULTITHREAD
144lib-mt: lib
145
146lib-release lib-release-mt: DEBUGFLAGS :=
147lib-release: lib
148lib-release-mt: lib-mt
149
150# Special case : building library in single-thread mode _and_ without zstdmt_compress.c
151ZSTDMT_FILES = compress/zstdmt_compress.c
152ZSTD_NOMT_FILES = $(filter-out $(ZSTDMT_FILES),$(ZSTD_FILES))
153libzstd-nomt: LDFLAGS += -shared -fPIC -fvisibility=hidden
154libzstd-nomt: $(ZSTD_NOMT_FILES)
155	@echo compiling single-thread dynamic library $(LIBVER)
156	@echo files : $(ZSTD_NOMT_FILES)
157	@$(CC) $(FLAGS) $^ $(LDFLAGS) $(SONAME_FLAGS) -o $@
158
159clean:
160	@$(RM) -r *.dSYM   # macOS-specific
161	@$(RM) core *.o *.a *.gcda *.$(SHARED_EXT) *.$(SHARED_EXT).* libzstd.pc
162	@$(RM) dll/libzstd.dll dll/libzstd.lib libzstd-nomt*
163	@$(RM) common/*.o compress/*.o decompress/*.o dictBuilder/*.o legacy/*.o deprecated/*.o
164	@echo Cleaning library completed
165
166#-----------------------------------------------------------------------------
167# make install is validated only for Linux, macOS, BSD, Hurd and Solaris targets
168#-----------------------------------------------------------------------------
169ifneq (,$(filter $(shell uname),Linux Darwin GNU/kFreeBSD GNU OpenBSD FreeBSD NetBSD DragonFly SunOS Haiku))
170
171DESTDIR     ?=
172# directory variables : GNU conventions prefer lowercase
173# see https://www.gnu.org/prep/standards/html_node/Makefile-Conventions.html
174# support both lower and uppercase (BSD), use uppercase in script
175prefix      ?= /usr/local
176PREFIX      ?= $(prefix)
177exec_prefix ?= $(PREFIX)
178libdir      ?= $(exec_prefix)/lib
179LIBDIR      ?= $(libdir)
180includedir  ?= $(PREFIX)/include
181INCLUDEDIR  ?= $(includedir)
182
183ifneq (,$(filter $(shell uname),FreeBSD NetBSD DragonFly))
184PKGCONFIGDIR ?= $(PREFIX)/libdata/pkgconfig
185else
186PKGCONFIGDIR ?= $(LIBDIR)/pkgconfig
187endif
188
189ifneq (,$(filter $(shell uname),SunOS))
190INSTALL ?= ginstall
191else
192INSTALL ?= install
193endif
194
195INSTALL_PROGRAM ?= $(INSTALL)
196INSTALL_DATA    ?= $(INSTALL) -m 644
197
198
199libzstd.pc:
200libzstd.pc: libzstd.pc.in
201	@echo creating pkgconfig
202	@sed -e 's|@PREFIX@|$(PREFIX)|' \
203             -e 's|@LIBDIR@|$(LIBDIR)|' \
204             -e 's|@INCLUDEDIR@|$(INCLUDEDIR)|' \
205             -e 's|@VERSION@|$(VERSION)|' \
206             $< >$@
207
208install: install-pc install-static install-shared install-includes
209	@echo zstd static and shared library installed
210
211install-pc: libzstd.pc
212	@$(INSTALL) -d -m 755 $(DESTDIR)$(PKGCONFIGDIR)/
213	@$(INSTALL_DATA) libzstd.pc $(DESTDIR)$(PKGCONFIGDIR)/
214
215install-static: libzstd.a
216	@echo Installing static library
217	@$(INSTALL) -d -m 755 $(DESTDIR)$(LIBDIR)/
218	@$(INSTALL_DATA) libzstd.a $(DESTDIR)$(LIBDIR)
219
220install-shared: libzstd
221	@echo Installing shared library
222	@$(INSTALL) -d -m 755 $(DESTDIR)$(LIBDIR)/
223	@$(INSTALL_PROGRAM) $(LIBZSTD) $(DESTDIR)$(LIBDIR)
224	@ln -sf $(LIBZSTD) $(DESTDIR)$(LIBDIR)/libzstd.$(SHARED_EXT_MAJOR)
225	@ln -sf $(LIBZSTD) $(DESTDIR)$(LIBDIR)/libzstd.$(SHARED_EXT)
226
227install-includes:
228	@echo Installing includes
229	@$(INSTALL) -d -m 755 $(DESTDIR)$(INCLUDEDIR)/
230	@$(INSTALL_DATA) zstd.h $(DESTDIR)$(INCLUDEDIR)
231	@$(INSTALL_DATA) common/zstd_errors.h $(DESTDIR)$(INCLUDEDIR)
232	@$(INSTALL_DATA) deprecated/zbuff.h $(DESTDIR)$(INCLUDEDIR)     # prototypes generate deprecation warnings
233	@$(INSTALL_DATA) dictBuilder/zdict.h $(DESTDIR)$(INCLUDEDIR)
234
235uninstall:
236	@$(RM) $(DESTDIR)$(LIBDIR)/libzstd.a
237	@$(RM) $(DESTDIR)$(LIBDIR)/libzstd.$(SHARED_EXT)
238	@$(RM) $(DESTDIR)$(LIBDIR)/libzstd.$(SHARED_EXT_MAJOR)
239	@$(RM) $(DESTDIR)$(LIBDIR)/$(LIBZSTD)
240	@$(RM) $(DESTDIR)$(PKGCONFIGDIR)/libzstd.pc
241	@$(RM) $(DESTDIR)$(INCLUDEDIR)/zstd.h
242	@$(RM) $(DESTDIR)$(INCLUDEDIR)/zstd_errors.h
243	@$(RM) $(DESTDIR)$(INCLUDEDIR)/zbuff.h   # Deprecated streaming functions
244	@$(RM) $(DESTDIR)$(INCLUDEDIR)/zdict.h
245	@echo zstd libraries successfully uninstalled
246
247endif
248