141e12eebapt# ################################################################
2648c686cem# Copyright (c) 2015-2020, Yann Collet, Facebook, Inc.
3b017827bapt# All rights reserved.
4b017827bapt#
541e12eebapt# This source code is licensed under both the BSD-style license (found in the
641e12eebapt# LICENSE file in the root directory of this source tree) and the GPLv2 (found
741e12eebapt# in the COPYING file in the root directory of this source tree).
8648c686cem# You may select, at your option, one of the above-listed licenses.
941e12eebapt# ################################################################
10b017827bapt
11648c686cemQ = $(if $(filter 1,$(V) $(VERBOSE)),,@)
12648c686cem
13648c686cem# When cross-compiling from linux to windows, you might
14648c686cem# need to specify this as "Windows." Fedora build fails
15648c686cem# without it.
16648c686cem#
17648c686cem# Note: mingw-w64 build from linux to windows does not
18648c686cem# fail on other tested distros (ubuntu, debian) even
19648c686cem# without manually specifying the TARGET_SYSTEM.
20648c686cemTARGET_SYSTEM ?= $(OS)
21648c686cem
22b017827bapt# Version numbers
23b017827baptLIBVER_MAJOR_SCRIPT:=`sed -n '/define ZSTD_VERSION_MAJOR/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < ./zstd.h`
24b017827baptLIBVER_MINOR_SCRIPT:=`sed -n '/define ZSTD_VERSION_MINOR/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < ./zstd.h`
25b017827baptLIBVER_PATCH_SCRIPT:=`sed -n '/define ZSTD_VERSION_RELEASE/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < ./zstd.h`
26b017827baptLIBVER_SCRIPT:= $(LIBVER_MAJOR_SCRIPT).$(LIBVER_MINOR_SCRIPT).$(LIBVER_PATCH_SCRIPT)
27b017827baptLIBVER_MAJOR := $(shell echo $(LIBVER_MAJOR_SCRIPT))
28b017827baptLIBVER_MINOR := $(shell echo $(LIBVER_MINOR_SCRIPT))
29b017827baptLIBVER_PATCH := $(shell echo $(LIBVER_PATCH_SCRIPT))
30b017827baptLIBVER := $(shell echo $(LIBVER_SCRIPT))
31b017827baptVERSION?= $(LIBVER)
32e580563cemCCVER := $(shell $(CC) --version)
33b017827bapt
34648c686cemCPPFLAGS+= -DXXH_NAMESPACE=ZSTD_
35648c686cemifeq ($(TARGET_SYSTEM),Windows_NT)   # MinGW assumed
367355e92cemCPPFLAGS   += -D__USE_MINGW_ANSI_STDIO   # compatibility with %zu formatting
377355e92cemendif
387355e92cemDEBUGFLAGS= -Wall -Wextra -Wcast-qual -Wcast-align -Wshadow \
39e7ab81dbapt            -Wstrict-aliasing=1 -Wswitch-enum -Wdeclaration-after-statement \
402d9b775cem            -Wstrict-prototypes -Wundef -Wpointer-arith \
41e7ab81dbapt            -Wvla -Wformat=2 -Winit-self -Wfloat-equal -Wwrite-strings \
42432d475cem            -Wredundant-decls -Wmissing-prototypes -Wc++-compat
43b017827baptCFLAGS  += $(DEBUGFLAGS) $(MOREFLAGS)
44b017827baptFLAGS    = $(CPPFLAGS) $(CFLAGS)
45b017827bapt
46432d475cemHAVE_COLORNEVER = $(shell echo a | grep --color=never a > /dev/null 2> /dev/null && echo 1 || echo 0)
47432d475cemGREP_OPTIONS ?=
48432d475cemifeq ($HAVE_COLORNEVER, 1)
49432d475cemGREP_OPTIONS += --color=never
50432d475cemendif
51432d475cemGREP = grep $(GREP_OPTIONS)
527355e92cem
537355e92cemZSTDCOMMON_FILES := $(sort $(wildcard common/*.c))
547355e92cemZSTDCOMP_FILES := $(sort $(wildcard compress/*.c))
557355e92cemZSTDDECOMP_FILES := $(sort $(wildcard decompress/*.c))
567355e92cemZDICT_FILES := $(sort $(wildcard dictBuilder/*.c))
577355e92cemZDEPR_FILES := $(sort $(wildcard deprecated/*.c))
587355e92cemZSTD_FILES := $(ZSTDCOMMON_FILES)
59b017827bapt
60e580563cemifeq ($(findstring GCC,$(CCVER)),GCC)
61e580563cemdecompress/zstd_decompress_block.o :	CFLAGS+=-fno-tree-vectorize
62e580563cemendif
63e580563cem
64648c686cem# This is a helper variable that configures a bunch of other variables to new,
65648c686cem# space-optimized defaults.
66648c686cemZSTD_LIB_MINIFY ?= 0
67648c686cemifneq ($(ZSTD_LIB_MINIFY), 0)
68648c686cem	HAVE_CC_OZ ?= $(shell echo "" | $(CC) -Oz -x c -c - -o /dev/null 2> /dev/null && echo 1 || echo 0)
69648c686cem	ZSTD_LEGACY_SUPPORT ?= 0
70648c686cem	ZSTD_LIB_DEPRECATED ?= 0
71648c686cem	HUF_FORCE_DECOMPRESS_X1 ?= 1
72648c686cem	ZSTD_FORCE_DECOMPRESS_SHORT ?= 1
73648c686cem	ZSTD_NO_INLINE ?= 1
74648c686cem	ZSTD_STRIP_ERROR_STRINGS ?= 1
75648c686cem	ifneq ($(HAVE_CC_OZ), 0)
76648c686cem		# Some compilers (clang) support an even more space-optimized setting.
77648c686cem		CFLAGS += -Oz
78648c686cem	else
79648c686cem		CFLAGS += -Os
80648c686cem	endif
81648c686cem	CFLAGS += -fno-stack-protector -fomit-frame-pointer -fno-ident \
82648c686cem	          -DDYNAMIC_BMI2=0 -DNDEBUG
83648c686cemelse
84648c686cem	CFLAGS += -O3
85648c686cemendif
86648c686cem
87648c686cem# Modules
887355e92cemZSTD_LIB_COMPRESSION ?= 1
897355e92cemZSTD_LIB_DECOMPRESSION ?= 1
907355e92cemZSTD_LIB_DICTBUILDER ?= 1
917355e92cemZSTD_LIB_DEPRECATED ?= 1
92648c686cem
93648c686cem# Legacy support
94648c686cemZSTD_LEGACY_SUPPORT ?= 5
95648c686cemZSTD_LEGACY_MULTITHREADED_API ?= 0
96648c686cem
97648c686cem# Build size optimizations
98432d475cemHUF_FORCE_DECOMPRESS_X1 ?= 0
99432d475cemHUF_FORCE_DECOMPRESS_X2 ?= 0
100432d475cemZSTD_FORCE_DECOMPRESS_SHORT ?= 0
101432d475cemZSTD_FORCE_DECOMPRESS_LONG ?= 0
102432d475cemZSTD_NO_INLINE ?= 0
103432d475cemZSTD_STRIP_ERROR_STRINGS ?= 0
104b017827bapt
1057355e92cemifeq ($(ZSTD_LIB_COMPRESSION), 0)
1067355e92cem	ZSTD_LIB_DICTBUILDER = 0
1077355e92cem	ZSTD_LIB_DEPRECATED = 0
1087355e92cemendif
1097355e92cem
1107355e92cemifeq ($(ZSTD_LIB_DECOMPRESSION), 0)
1117355e92cem	ZSTD_LEGACY_SUPPORT = 0
1127355e92cem	ZSTD_LIB_DEPRECATED = 0
1137355e92cemendif
1147355e92cem
1157355e92cemifneq ($(ZSTD_LIB_COMPRESSION), 0)
1167355e92cem	ZSTD_FILES += $(ZSTDCOMP_FILES)
1177355e92cemendif
1187355e92cem
1197355e92cemifneq ($(ZSTD_LIB_DECOMPRESSION), 0)
1207355e92cem	ZSTD_FILES += $(ZSTDDECOMP_FILES)
1217355e92cemendif
1227355e92cem
1237355e92cemifneq ($(ZSTD_LIB_DEPRECATED), 0)
1247355e92cem	ZSTD_FILES += $(ZDEPR_FILES)
1257355e92cemendif
1267355e92cem
1277355e92cemifneq ($(ZSTD_LIB_DICTBUILDER), 0)
1287355e92cem	ZSTD_FILES += $(ZDICT_FILES)
1297355e92cemendif
130b017827bapt
131432d475cemifneq ($(HUF_FORCE_DECOMPRESS_X1), 0)
132432d475cem	CFLAGS += -DHUF_FORCE_DECOMPRESS_X1
133432d475cemendif
134432d475cem
135432d475cemifneq ($(HUF_FORCE_DECOMPRESS_X2), 0)
136432d475cem	CFLAGS += -DHUF_FORCE_DECOMPRESS_X2
137432d475cemendif
138432d475cem
139432d475cemifneq ($(ZSTD_FORCE_DECOMPRESS_SHORT), 0)
140432d475cem	CFLAGS += -DZSTD_FORCE_DECOMPRESS_SHORT
141432d475cemendif
142432d475cem
143432d475cemifneq ($(ZSTD_FORCE_DECOMPRESS_LONG), 0)
144432d475cem	CFLAGS += -DZSTD_FORCE_DECOMPRESS_LONG
145432d475cemendif
146432d475cem
147432d475cemifneq ($(ZSTD_NO_INLINE), 0)
148432d475cem	CFLAGS += -DZSTD_NO_INLINE
149432d475cemendif
150432d475cem
151432d475cemifneq ($(ZSTD_STRIP_ERROR_STRINGS), 0)
152432d475cem	CFLAGS += -DZSTD_STRIP_ERROR_STRINGS
153432d475cemendif
154432d475cem
1552d9b775cemifneq ($(ZSTD_LEGACY_MULTITHREADED_API), 0)
1562d9b775cem	CFLAGS += -DZSTD_LEGACY_MULTITHREADED_API
1572d9b775cemendif
1582d9b775cem
159b017827baptifneq ($(ZSTD_LEGACY_SUPPORT), 0)
160b017827baptifeq ($(shell test $(ZSTD_LEGACY_SUPPORT) -lt 8; echo $$?), 0)
1617355e92cem	ZSTD_FILES += $(shell ls legacy/*.c | $(GREP) 'v0[$(ZSTD_LEGACY_SUPPORT)-7]')
162b017827baptendif
163b017827baptendif
164b017827baptCPPFLAGS  += -DZSTD_LEGACY_SUPPORT=$(ZSTD_LEGACY_SUPPORT)
165b017827bapt
166b017827baptZSTD_OBJ   := $(patsubst %.c,%.o,$(ZSTD_FILES))
167b017827bapt
1687355e92cem# macOS linker doesn't support -soname, and use different extension
169b017827bapt# see : https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/DynamicLibraryDesignGuidelines.html
170b017827baptifeq ($(shell uname), Darwin)
171b017827bapt	SHARED_EXT = dylib
172b017827bapt	SHARED_EXT_MAJOR = $(LIBVER_MAJOR).$(SHARED_EXT)
173b017827bapt	SHARED_EXT_VER = $(LIBVER).$(SHARED_EXT)
174b017827bapt	SONAME_FLAGS = -install_name $(LIBDIR)/libzstd.$(SHARED_EXT_MAJOR) -compatibility_version $(LIBVER_MAJOR) -current_version $(LIBVER)
175b017827baptelse
176b017827bapt	SONAME_FLAGS = -Wl,-soname=libzstd.$(SHARED_EXT).$(LIBVER_MAJOR)
177b017827bapt	SHARED_EXT = so
178b017827bapt	SHARED_EXT_MAJOR = $(SHARED_EXT).$(LIBVER_MAJOR)
179b017827bapt	SHARED_EXT_VER = $(SHARED_EXT).$(LIBVER)
180b017827baptendif
181b017827bapt
182b017827bapt
183648c686cem.PHONY: default lib-all all clean install uninstall
184b017827bapt
185b017827baptdefault: lib-release
186b017827bapt
187648c686cem# alias
188648c686cemlib-all: all
189648c686cem
190b017827baptall: lib
191b017827bapt
192b017827baptlibzstd.a: ARFLAGS = rcs
193b017827baptlibzstd.a: $(ZSTD_OBJ)
194b017827bapt	@echo compiling static library
195648c686cem	$(Q)$(AR) $(ARFLAGS) $@ $^
196803f70bbapt
197648c686cemifneq (,$(filter Windows%,$(TARGET_SYSTEM)))
1987355e92cem
1997355e92cemLIBZSTD = dll\libzstd.dll
200b017827bapt$(LIBZSTD): $(ZSTD_FILES)
201b017827bapt	@echo compiling dynamic library $(LIBVER)
202648c686cem	$(CC) $(FLAGS) -DZSTD_DLL_EXPORT=1 -Wl,--out-implib,dll\libzstd.dll.a -shared $^ -o $@
2037355e92cem
204b017827baptelse
2057355e92cem
2067355e92cemLIBZSTD = libzstd.$(SHARED_EXT_VER)
2077355e92cem$(LIBZSTD): LDFLAGS += -shared -fPIC -fvisibility=hidden
2087355e92cem$(LIBZSTD): $(ZSTD_FILES)
2097355e92cem	@echo compiling dynamic library $(LIBVER)
210648c686cem	$(Q)$(CC) $(FLAGS) $^ $(LDFLAGS) $(SONAME_FLAGS) -o $@
211b017827bapt	@echo creating versioned links
212648c686cem	$(Q)ln -sf $@ libzstd.$(SHARED_EXT_MAJOR)
213648c686cem	$(Q)ln -sf $@ libzstd.$(SHARED_EXT)
2147355e92cem
215b017827baptendif
216b017827bapt
217648c686cem.PHONY: libzstd
218b017827baptlibzstd : $(LIBZSTD)
219b017827bapt
220648c686cem.PHONY: lib
221648c686cemlib : libzstd.a libzstd
222803f70bbapt
223648c686cem.PHONY: lib-mt
224648c686cem%-mt : CPPFLAGS += -DZSTD_MULTITHREAD
225648c686cem%-mt : LDFLAGS  += -pthread
226648c686cem%-mt : %
227648c686cem	@echo multi-threading build completed
228b017827bapt
229648c686cem.PHONY: lib-release
230648c686cem%-release : DEBUGFLAGS :=
231648c686cem%-release : %
232648c686cem	@echo release build completed
233803f70bbapt
234b017827bapt
23541e12eebapt# Special case : building library in single-thread mode _and_ without zstdmt_compress.c
23641e12eebaptZSTDMT_FILES = compress/zstdmt_compress.c
23741e12eebaptZSTD_NOMT_FILES = $(filter-out $(ZSTDMT_FILES),$(ZSTD_FILES))
23841e12eebaptlibzstd-nomt: LDFLAGS += -shared -fPIC -fvisibility=hidden
23941e12eebaptlibzstd-nomt: $(ZSTD_NOMT_FILES)
24041e12eebapt	@echo compiling single-thread dynamic library $(LIBVER)
24141e12eebapt	@echo files : $(ZSTD_NOMT_FILES)
242648c686cem	$(Q)$(CC) $(FLAGS) $^ $(LDFLAGS) $(SONAME_FLAGS) -o $@
24341e12eebapt
244b017827baptclean:
245648c686cem	$(Q)$(RM) -r *.dSYM   # macOS-specific
246648c686cem	$(Q)$(RM) core *.o *.a *.gcda *.$(SHARED_EXT) *.$(SHARED_EXT).* libzstd.pc
247648c686cem	$(Q)$(RM) dll/libzstd.dll dll/libzstd.lib libzstd-nomt*
248648c686cem	$(Q)$(RM) common/*.o compress/*.o decompress/*.o dictBuilder/*.o legacy/*.o deprecated/*.o
249b017827bapt	@echo Cleaning library completed
250b017827bapt
251b017827bapt#-----------------------------------------------------------------------------
252648c686cem# make install is validated only for below listed environments
253b017827bapt#-----------------------------------------------------------------------------
2547355e92cemifneq (,$(filter $(shell uname),Linux Darwin GNU/kFreeBSD GNU OpenBSD FreeBSD NetBSD DragonFly SunOS Haiku))
255b017827bapt
256648c686cemall: libzstd.pc
257648c686cem
25841e12eebaptDESTDIR     ?=
25941e12eebapt# directory variables : GNU conventions prefer lowercase
26041e12eebapt# see https://www.gnu.org/prep/standards/html_node/Makefile-Conventions.html
26141e12eebapt# support both lower and uppercase (BSD), use uppercase in script
26241e12eebaptprefix      ?= /usr/local
26341e12eebaptPREFIX      ?= $(prefix)
26441e12eebaptexec_prefix ?= $(PREFIX)
265648c686cemEXEC_PREFIX ?= $(exec_prefix)
266648c686cemlibdir      ?= $(EXEC_PREFIX)/lib
26741e12eebaptLIBDIR      ?= $(libdir)
26841e12eebaptincludedir  ?= $(PREFIX)/include
26941e12eebaptINCLUDEDIR  ?= $(includedir)
270b017827bapt
271648c686cemPCLIBDIR ?= $(shell echo "$(LIBDIR)" | sed -n -E -e "s@^$(EXEC_PREFIX)(/|$$)@@p")
272648c686cemPCINCDIR ?= $(shell echo "$(INCLUDEDIR)" | sed -n -E -e "s@^$(PREFIX)(/|$$)@@p")
273648c686cem
274648c686cemifeq (,$(PCLIBDIR))
275648c686cem# Additional prefix check is required, since the empty string is technically a
276648c686cem# valid PCLIBDIR
277648c686cemifeq (,$(shell echo "$(LIBDIR)" | sed -n -E -e "\\@^$(EXEC_PREFIX)(/|$$)@ p"))
278648c686cem$(error configured libdir ($(LIBDIR)) is outside of prefix ($(PREFIX)), can't generate pkg-config file)
279648c686cemendif
280648c686cemendif
281648c686cem
282648c686cemifeq (,$(PCINCDIR))
283648c686cem# Additional prefix check is required, since the empty string is technically a
284648c686cem# valid PCINCDIR
285648c686cemifeq (,$(shell echo "$(INCLUDEDIR)" | sed -n -E -e "\\@^$(PREFIX)(/|$$)@ p"))
286648c686cem$(error configured includedir ($(INCLUDEDIR)) is outside of exec_prefix ($(EXEC_PREFIX)), can't generate pkg-config file)
287648c686cemendif
288648c686cemendif
289648c686cem
2907355e92cemifneq (,$(filter $(shell uname),FreeBSD NetBSD DragonFly))
291b017827baptPKGCONFIGDIR ?= $(PREFIX)/libdata/pkgconfig
292b017827baptelse
293b017827baptPKGCONFIGDIR ?= $(LIBDIR)/pkgconfig
294b017827baptendif
295b017827bapt
29641e12eebaptifneq (,$(filter $(shell uname),SunOS))
29741e12eebaptINSTALL ?= ginstall
29841e12eebaptelse
29941e12eebaptINSTALL ?= install
30041e12eebaptendif
30141e12eebapt
30241e12eebaptINSTALL_PROGRAM ?= $(INSTALL)
30341e12eebaptINSTALL_DATA    ?= $(INSTALL) -m 644
304b017827bapt
305b017827bapt
306b017827baptlibzstd.pc:
307b017827baptlibzstd.pc: libzstd.pc.in
308b017827bapt	@echo creating pkgconfig
309648c686cem	$(Q)@sed -E -e 's|@PREFIX@|$(PREFIX)|' \
310648c686cem          -e 's|@LIBDIR@|$(PCLIBDIR)|' \
311648c686cem          -e 's|@INCLUDEDIR@|$(PCINCDIR)|' \
312648c686cem          -e 's|@VERSION@|$(VERSION)|' \
313648c686cem          $< >$@
314b017827bapt
3157355e92ceminstall: install-pc install-static install-shared install-includes
3167355e92cem	@echo zstd static and shared library installed
3177355e92cem
3187355e92ceminstall-pc: libzstd.pc
319648c686cem	$(Q)$(INSTALL) -d -m 755 $(DESTDIR)$(PKGCONFIGDIR)/
320648c686cem	$(Q)$(INSTALL_DATA) libzstd.pc $(DESTDIR)$(PKGCONFIGDIR)/
3217355e92cem
3227355e92ceminstall-static: libzstd.a
3237355e92cem	@echo Installing static library
324648c686cem	$(Q)$(INSTALL) -d -m 755 $(DESTDIR)$(LIBDIR)/
325648c686cem	$(Q)$(INSTALL_DATA) libzstd.a $(DESTDIR)$(LIBDIR)
3267355e92cem
3277355e92ceminstall-shared: libzstd
3287355e92cem	@echo Installing shared library
329648c686cem	$(Q)$(INSTALL) -d -m 755 $(DESTDIR)$(LIBDIR)/
330648c686cem	$(Q)$(INSTALL_PROGRAM) $(LIBZSTD) $(DESTDIR)$(LIBDIR)
331648c686cem	$(Q)ln -sf $(LIBZSTD) $(DESTDIR)$(LIBDIR)/libzstd.$(SHARED_EXT_MAJOR)
332648c686cem	$(Q)ln -sf $(LIBZSTD) $(DESTDIR)$(LIBDIR)/libzstd.$(SHARED_EXT)
3337355e92cem
3347355e92ceminstall-includes:
335b017827bapt	@echo Installing includes
336648c686cem	$(Q)$(INSTALL) -d -m 755 $(DESTDIR)$(INCLUDEDIR)/
337648c686cem	$(Q)$(INSTALL_DATA) zstd.h $(DESTDIR)$(INCLUDEDIR)
338648c686cem	$(Q)$(INSTALL_DATA) common/zstd_errors.h $(DESTDIR)$(INCLUDEDIR)
339648c686cem	$(Q)$(INSTALL_DATA) deprecated/zbuff.h $(DESTDIR)$(INCLUDEDIR)     # prototypes generate deprecation warnings
340648c686cem	$(Q)$(INSTALL_DATA) dictBuilder/zdict.h $(DESTDIR)$(INCLUDEDIR)
341b017827bapt
342b017827baptuninstall:
343648c686cem	$(Q)$(RM) $(DESTDIR)$(LIBDIR)/libzstd.a
344648c686cem	$(Q)$(RM) $(DESTDIR)$(LIBDIR)/libzstd.$(SHARED_EXT)
345648c686cem	$(Q)$(RM) $(DESTDIR)$(LIBDIR)/libzstd.$(SHARED_EXT_MAJOR)
346648c686cem	$(Q)$(RM) $(DESTDIR)$(LIBDIR)/$(LIBZSTD)
347648c686cem	$(Q)$(RM) $(DESTDIR)$(PKGCONFIGDIR)/libzstd.pc
348648c686cem	$(Q)$(RM) $(DESTDIR)$(INCLUDEDIR)/zstd.h
349648c686cem	$(Q)$(RM) $(DESTDIR)$(INCLUDEDIR)/zstd_errors.h
350648c686cem	$(Q)$(RM) $(DESTDIR)$(INCLUDEDIR)/zbuff.h   # Deprecated streaming functions
351648c686cem	$(Q)$(RM) $(DESTDIR)$(INCLUDEDIR)/zdict.h
352b017827bapt	@echo zstd libraries successfully uninstalled
353b017827bapt
354b017827baptendif
355