xref: /freebsd/sys/contrib/zstd/lib/Makefile (revision 451bcf1b360146af0759ac18c9cc55f0a36cc386)
1# ################################################################
2# Copyright (c) 2015-2020, 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# You may select, at your option, one of the above-listed licenses.
9# ################################################################
10
11.PHONY: default
12default: lib-release
13
14# define silent mode as default (verbose mode with V=1 or VERBOSE=1)
15$(V)$(VERBOSE).SILENT:
16
17# When cross-compiling from linux to windows,
18# one might need to specify TARGET_SYSTEM as "Windows."
19# Building from Fedora fails without it.
20# (but Ubuntu and Debian don't need to set anything)
21TARGET_SYSTEM ?= $(OS)
22
23# Version numbers
24LIBVER_MAJOR_SCRIPT:=`sed -n '/define ZSTD_VERSION_MAJOR/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < ./zstd.h`
25LIBVER_MINOR_SCRIPT:=`sed -n '/define ZSTD_VERSION_MINOR/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < ./zstd.h`
26LIBVER_PATCH_SCRIPT:=`sed -n '/define ZSTD_VERSION_RELEASE/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < ./zstd.h`
27LIBVER_SCRIPT:= $(LIBVER_MAJOR_SCRIPT).$(LIBVER_MINOR_SCRIPT).$(LIBVER_PATCH_SCRIPT)
28LIBVER_MAJOR := $(shell echo $(LIBVER_MAJOR_SCRIPT))
29LIBVER_MINOR := $(shell echo $(LIBVER_MINOR_SCRIPT))
30LIBVER_PATCH := $(shell echo $(LIBVER_PATCH_SCRIPT))
31LIBVER := $(shell echo $(LIBVER_SCRIPT))
32VERSION?= $(LIBVER)
33CCVER := $(shell $(CC) --version)
34
35# ZSTD_LIB_MINIFY is a helper variable that
36# configures a bunch of other variables to space-optimized defaults.
37ZSTD_LIB_MINIFY ?= 0
38ifneq ($(ZSTD_LIB_MINIFY), 0)
39  HAVE_CC_OZ ?= $(shell echo "" | $(CC) -Oz -x c -c - -o /dev/null 2> /dev/null && echo 1 || echo 0)
40  ZSTD_LEGACY_SUPPORT ?= 0
41  ZSTD_LIB_DEPRECATED ?= 0
42  HUF_FORCE_DECOMPRESS_X1 ?= 1
43  ZSTD_FORCE_DECOMPRESS_SHORT ?= 1
44  ZSTD_NO_INLINE ?= 1
45  ZSTD_STRIP_ERROR_STRINGS ?= 1
46ifneq ($(HAVE_CC_OZ), 0)
47    # Some compilers (clang) support an even more space-optimized setting.
48    CFLAGS += -Oz
49else
50    CFLAGS += -Os
51endif
52  CFLAGS += -fno-stack-protector -fomit-frame-pointer -fno-ident \
53            -DDYNAMIC_BMI2=0 -DNDEBUG
54else
55  CFLAGS += -O3
56endif
57
58DEBUGLEVEL ?= 0
59CPPFLAGS += -DXXH_NAMESPACE=ZSTD_ -DDEBUGLEVEL=$(DEBUGLEVEL)
60ifeq ($(TARGET_SYSTEM),Windows_NT)   # MinGW assumed
61  CPPFLAGS += -D__USE_MINGW_ANSI_STDIO   # compatibility with %zu formatting
62endif
63DEBUGFLAGS= -Wall -Wextra -Wcast-qual -Wcast-align -Wshadow \
64            -Wstrict-aliasing=1 -Wswitch-enum -Wdeclaration-after-statement \
65            -Wstrict-prototypes -Wundef -Wpointer-arith \
66            -Wvla -Wformat=2 -Winit-self -Wfloat-equal -Wwrite-strings \
67            -Wredundant-decls -Wmissing-prototypes -Wc++-compat
68CFLAGS   += $(DEBUGFLAGS) $(MOREFLAGS)
69FLAGS     = $(CPPFLAGS) $(CFLAGS)
70
71HAVE_COLORNEVER = $(shell echo a | grep --color=never a > /dev/null 2> /dev/null && echo 1 || echo 0)
72GREP_OPTIONS ?=
73ifeq ($HAVE_COLORNEVER, 1)
74  GREP_OPTIONS += --color=never
75endif
76GREP = grep $(GREP_OPTIONS)
77SED_ERE_OPT ?= -E
78
79ZSTDCOMMON_FILES := $(sort $(wildcard common/*.c))
80ZSTDCOMP_FILES := $(sort $(wildcard compress/*.c))
81ZSTDDECOMP_FILES := $(sort $(wildcard decompress/*.c))
82ZDICT_FILES := $(sort $(wildcard dictBuilder/*.c))
83ZDEPR_FILES := $(sort $(wildcard deprecated/*.c))
84ZSTD_FILES := $(ZSTDCOMMON_FILES)
85
86ifeq ($(findstring GCC,$(CCVER)),GCC)
87decompress/zstd_decompress_block.o : CFLAGS+=-fno-tree-vectorize
88endif
89
90# Modules
91ZSTD_LIB_COMPRESSION ?= 1
92ZSTD_LIB_DECOMPRESSION ?= 1
93ZSTD_LIB_DICTBUILDER ?= 1
94ZSTD_LIB_DEPRECATED ?= 1
95
96# Legacy support
97ZSTD_LEGACY_SUPPORT ?= 5
98ZSTD_LEGACY_MULTITHREADED_API ?= 0
99
100# Build size optimizations
101HUF_FORCE_DECOMPRESS_X1 ?= 0
102HUF_FORCE_DECOMPRESS_X2 ?= 0
103ZSTD_FORCE_DECOMPRESS_SHORT ?= 0
104ZSTD_FORCE_DECOMPRESS_LONG ?= 0
105ZSTD_NO_INLINE ?= 0
106ZSTD_STRIP_ERROR_STRINGS ?= 0
107
108ifeq ($(ZSTD_LIB_COMPRESSION), 0)
109  ZSTD_LIB_DICTBUILDER = 0
110  ZSTD_LIB_DEPRECATED = 0
111endif
112
113ifeq ($(ZSTD_LIB_DECOMPRESSION), 0)
114  ZSTD_LEGACY_SUPPORT = 0
115  ZSTD_LIB_DEPRECATED = 0
116endif
117
118ifneq ($(ZSTD_LIB_COMPRESSION), 0)
119  ZSTD_FILES += $(ZSTDCOMP_FILES)
120endif
121
122ifneq ($(ZSTD_LIB_DECOMPRESSION), 0)
123  ZSTD_FILES += $(ZSTDDECOMP_FILES)
124endif
125
126ifneq ($(ZSTD_LIB_DEPRECATED), 0)
127  ZSTD_FILES += $(ZDEPR_FILES)
128endif
129
130ifneq ($(ZSTD_LIB_DICTBUILDER), 0)
131  ZSTD_FILES += $(ZDICT_FILES)
132endif
133
134ifneq ($(HUF_FORCE_DECOMPRESS_X1), 0)
135  CFLAGS += -DHUF_FORCE_DECOMPRESS_X1
136endif
137
138ifneq ($(HUF_FORCE_DECOMPRESS_X2), 0)
139  CFLAGS += -DHUF_FORCE_DECOMPRESS_X2
140endif
141
142ifneq ($(ZSTD_FORCE_DECOMPRESS_SHORT), 0)
143  CFLAGS += -DZSTD_FORCE_DECOMPRESS_SHORT
144endif
145
146ifneq ($(ZSTD_FORCE_DECOMPRESS_LONG), 0)
147  CFLAGS += -DZSTD_FORCE_DECOMPRESS_LONG
148endif
149
150ifneq ($(ZSTD_NO_INLINE), 0)
151  CFLAGS += -DZSTD_NO_INLINE
152endif
153
154ifneq ($(ZSTD_STRIP_ERROR_STRINGS), 0)
155  CFLAGS += -DZSTD_STRIP_ERROR_STRINGS
156endif
157
158ifneq ($(ZSTD_LEGACY_MULTITHREADED_API), 0)
159  CFLAGS += -DZSTD_LEGACY_MULTITHREADED_API
160endif
161
162ifneq ($(ZSTD_LEGACY_SUPPORT), 0)
163ifeq ($(shell test $(ZSTD_LEGACY_SUPPORT) -lt 8; echo $$?), 0)
164  ZSTD_FILES += $(shell ls legacy/*.c | $(GREP) 'v0[$(ZSTD_LEGACY_SUPPORT)-7]')
165endif
166endif
167CPPFLAGS  += -DZSTD_LEGACY_SUPPORT=$(ZSTD_LEGACY_SUPPORT)
168
169ZSTD_LOCAL_SRC := $(notdir $(ZSTD_FILES))
170ZSTD_LOCAL_OBJ := $(ZSTD_LOCAL_SRC:.c=.o)
171
172ZSTD_SUBDIR := common compress decompress dictBuilder legacy deprecated
173vpath %.c $(ZSTD_SUBDIR)
174
175UNAME := $(shell uname)
176
177ifndef BUILD_DIR
178ifeq ($(UNAME), Darwin)
179  HASH ?= md5
180else ifeq ($(UNAME), FreeBSD)
181  HASH ?= gmd5sum
182else ifeq ($(UNAME), OpenBSD)
183  HASH ?= md5
184endif
185HASH ?= md5sum
186
187HASH_DIR = conf_$(shell echo $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) $(ZSTD_FILES) | $(HASH) | cut -f 1 -d " " )
188HAVE_HASH :=$(shell echo 1 | $(HASH) > /dev/null && echo 1 || echo 0)
189ifeq ($(HAVE_HASH),0)
190  $(info warning : could not find HASH ($(HASH)), needed to differentiate builds using different flags)
191  BUILD_DIR := obj/generic_noconf
192endif
193endif # BUILD_DIR
194
195
196# macOS linker doesn't support -soname, and use different extension
197# see : https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/DynamicLibraryDesignGuidelines.html
198ifeq ($(UNAME), Darwin)
199  SHARED_EXT = dylib
200  SHARED_EXT_MAJOR = $(LIBVER_MAJOR).$(SHARED_EXT)
201  SHARED_EXT_VER = $(LIBVER).$(SHARED_EXT)
202  SONAME_FLAGS = -install_name $(LIBDIR)/libzstd.$(SHARED_EXT_MAJOR) -compatibility_version $(LIBVER_MAJOR) -current_version $(LIBVER)
203else
204  SONAME_FLAGS = -Wl,-soname=libzstd.$(SHARED_EXT).$(LIBVER_MAJOR)
205  SHARED_EXT = so
206  SHARED_EXT_MAJOR = $(SHARED_EXT).$(LIBVER_MAJOR)
207  SHARED_EXT_VER = $(SHARED_EXT).$(LIBVER)
208endif
209
210SET_CACHE_DIRECTORY = \
211	$(MAKE) --no-print-directory $@ \
212    BUILD_DIR=obj/$(HASH_DIR) \
213    CPPFLAGS="$(CPPFLAGS)" \
214    CFLAGS="$(CFLAGS)" \
215    LDFLAGS="$(LDFLAGS)"
216
217
218.PHONY: lib-all all clean install uninstall
219
220# alias
221lib-all: all
222
223all: lib
224
225.PHONY: libzstd.a  # must be run every time
226
227ifndef BUILD_DIR
228# determine BUILD_DIR from compilation flags
229
230libzstd.a:
231	$(SET_CACHE_DIRECTORY)
232
233else
234# BUILD_DIR is defined
235
236ZSTD_STATLIB_DIR := $(BUILD_DIR)/static
237ZSTD_STATLIB := $(ZSTD_STATLIB_DIR)/libzstd.a
238ZSTD_STATLIB_OBJ := $(addprefix $(ZSTD_STATLIB_DIR)/,$(ZSTD_LOCAL_OBJ))
239$(ZSTD_STATLIB): ARFLAGS = rcs
240$(ZSTD_STATLIB): | $(ZSTD_STATLIB_DIR)
241$(ZSTD_STATLIB): $(ZSTD_STATLIB_OBJ)
242	@echo compiling static library
243	$(AR) $(ARFLAGS) $@ $^
244
245libzstd.a: $(ZSTD_STATLIB)
246	cp -f $< $@
247
248endif
249
250ifneq (,$(filter Windows%,$(TARGET_SYSTEM)))
251
252LIBZSTD = dll/libzstd.dll
253$(LIBZSTD): $(ZSTD_FILES)
254	@echo compiling dynamic library $(LIBVER)
255	$(CC) $(FLAGS) -DZSTD_DLL_EXPORT=1 -Wl,--out-implib,dll/libzstd.dll.a -shared $^ -o $@
256
257else  # not Windows
258
259LIBZSTD = libzstd.$(SHARED_EXT_VER)
260.PHONY: $(LIBZSTD)  # must be run every time
261$(LIBZSTD): CFLAGS += -fPIC
262$(LIBZSTD): LDFLAGS += -shared -fvisibility=hidden
263
264ifndef BUILD_DIR
265# determine BUILD_DIR from compilation flags
266
267$(LIBZSTD):
268	$(SET_CACHE_DIRECTORY)
269
270else
271# BUILD_DIR is defined
272
273ZSTD_DYNLIB_DIR := $(BUILD_DIR)/dynamic
274ZSTD_DYNLIB := $(ZSTD_DYNLIB_DIR)/$(LIBZSTD)
275ZSTD_DYNLIB_OBJ := $(addprefix $(ZSTD_DYNLIB_DIR)/,$(ZSTD_LOCAL_OBJ))
276
277$(ZSTD_DYNLIB): | $(ZSTD_DYNLIB_DIR)
278$(ZSTD_DYNLIB): $(ZSTD_DYNLIB_OBJ)
279	@echo compiling dynamic library $(LIBVER)
280	$(CC) $(FLAGS) $^ $(LDFLAGS) $(SONAME_FLAGS) -o $@
281	@echo creating versioned links
282	ln -sf $@ libzstd.$(SHARED_EXT_MAJOR)
283	ln -sf $@ libzstd.$(SHARED_EXT)
284
285$(LIBZSTD): $(ZSTD_DYNLIB)
286	cp -f $< $@
287
288endif  # ifndef BUILD_DIR
289endif  # if windows
290
291.PHONY: libzstd
292libzstd : $(LIBZSTD)
293
294.PHONY: lib
295lib : libzstd.a libzstd
296
297
298# note : do not define lib-mt or lib-release as .PHONY
299# make does not consider implicit pattern rule for .PHONY target
300
301%-mt : CPPFLAGS += -DZSTD_MULTITHREAD
302%-mt : LDFLAGS  += -pthread
303%-mt : %
304	@echo multi-threading build completed
305
306%-release : DEBUGFLAGS :=
307%-release : %
308	@echo release build completed
309
310
311# Generate .h dependencies automatically
312
313DEPFLAGS = -MT $@ -MMD -MP -MF
314
315$(ZSTD_DYNLIB_DIR)/%.o : %.c $(ZSTD_DYNLIB_DIR)/%.d | $(ZSTD_DYNLIB_DIR)
316	@echo CC $@
317	$(COMPILE.c) $(DEPFLAGS) $(ZSTD_DYNLIB_DIR)/$*.d $(OUTPUT_OPTION) $<
318
319$(ZSTD_STATLIB_DIR)/%.o : %.c $(ZSTD_STATLIB_DIR)/%.d | $(ZSTD_STATLIB_DIR)
320	@echo CC $@
321	$(COMPILE.c) $(DEPFLAGS) $(ZSTD_STATLIB_DIR)/$*.d $(OUTPUT_OPTION) $<
322
323MKDIR ?= mkdir
324$(BUILD_DIR) $(ZSTD_DYNLIB_DIR) $(ZSTD_STATLIB_DIR):
325	$(MKDIR) -p $@
326
327DEPFILES := $(ZSTD_DYNLIB_OBJ:.o=.d) $(ZSTD_STATLIB_OBJ:.o=.d)
328$(DEPFILES):
329
330include $(wildcard $(DEPFILES))
331
332
333# Special case : building library in single-thread mode _and_ without zstdmt_compress.c
334ZSTDMT_FILES = compress/zstdmt_compress.c
335ZSTD_NOMT_FILES = $(filter-out $(ZSTDMT_FILES),$(ZSTD_FILES))
336libzstd-nomt: LDFLAGS += -shared -fPIC -fvisibility=hidden
337libzstd-nomt: $(ZSTD_NOMT_FILES)
338	@echo compiling single-thread dynamic library $(LIBVER)
339	@echo files : $(ZSTD_NOMT_FILES)
340	$(CC) $(FLAGS) $^ $(LDFLAGS) $(SONAME_FLAGS) -o $@
341
342clean:
343	$(RM) -r *.dSYM   # macOS-specific
344	$(RM) core *.o *.a *.gcda *.$(SHARED_EXT) *.$(SHARED_EXT).* libzstd.pc
345	$(RM) dll/libzstd.dll dll/libzstd.lib libzstd-nomt*
346	$(RM) -r obj/*
347	@echo Cleaning library completed
348
349#-----------------------------------------------------------------------------
350# make install is validated only for below listed environments
351#-----------------------------------------------------------------------------
352ifneq (,$(filter $(UNAME),Linux Darwin GNU/kFreeBSD GNU OpenBSD FreeBSD NetBSD DragonFly SunOS Haiku))
353
354all: libzstd.pc
355
356HAS_EXPLICIT_EXEC_PREFIX := $(if $(or $(EXEC_PREFIX),$(exec_prefix)),1,)
357
358DESTDIR     ?=
359# directory variables : GNU conventions prefer lowercase
360# see https://www.gnu.org/prep/standards/html_node/Makefile-Conventions.html
361# support both lower and uppercase (BSD), use uppercase in script
362prefix      ?= /usr/local
363PREFIX      ?= $(prefix)
364exec_prefix ?= $(PREFIX)
365EXEC_PREFIX ?= $(exec_prefix)
366libdir      ?= $(EXEC_PREFIX)/lib
367LIBDIR      ?= $(libdir)
368includedir  ?= $(PREFIX)/include
369INCLUDEDIR  ?= $(includedir)
370
371PCINCDIR := $(patsubst $(PREFIX)%,%,$(INCLUDEDIR))
372PCLIBDIR := $(patsubst $(EXEC_PREFIX)%,%,$(LIBDIR))
373
374# If we successfully stripped off a prefix, we'll add a reference to the
375# relevant pc variable.
376PCINCPREFIX := $(if $(findstring $(INCLUDEDIR),$(PCINCDIR)),,$${prefix})
377PCLIBPREFIX := $(if $(findstring $(LIBDIR),$(PCLIBDIR)),,$${exec_prefix})
378
379# If no explicit EXEC_PREFIX was set by the caller, write it out as a reference
380# to PREFIX, rather than as a resolved value.
381PCEXEC_PREFIX := $(if $(HAS_EXPLICIT_EXEC_PREFIX),$(EXEC_PREFIX),$${prefix})
382
383ifneq (,$(filter $(UNAME),FreeBSD NetBSD DragonFly))
384  PKGCONFIGDIR ?= $(PREFIX)/libdata/pkgconfig
385else
386  PKGCONFIGDIR ?= $(LIBDIR)/pkgconfig
387endif
388
389ifneq (,$(filter $(UNAME),SunOS))
390  INSTALL ?= ginstall
391else
392  INSTALL ?= install
393endif
394
395INSTALL_PROGRAM ?= $(INSTALL)
396INSTALL_DATA    ?= $(INSTALL) -m 644
397
398
399libzstd.pc:
400libzstd.pc: libzstd.pc.in
401	@echo creating pkgconfig
402	@sed $(SED_ERE_OPT) \
403	        -e 's|@PREFIX@|$(PREFIX)|' \
404	        -e 's|@EXEC_PREFIX@|$(PCEXEC_PREFIX)|' \
405          -e 's|@INCLUDEDIR@|$(PCINCPREFIX)$(PCINCDIR)|' \
406          -e 's|@LIBDIR@|$(PCLIBPREFIX)$(PCLIBDIR)|' \
407          -e 's|@VERSION@|$(VERSION)|' \
408          $< >$@
409
410install: install-pc install-static install-shared install-includes
411	@echo zstd static and shared library installed
412
413install-pc: libzstd.pc
414	[ -e $(DESTDIR)$(PKGCONFIGDIR) ] || $(INSTALL) -d -m 755 $(DESTDIR)$(PKGCONFIGDIR)/
415	$(INSTALL_DATA) libzstd.pc $(DESTDIR)$(PKGCONFIGDIR)/
416
417install-static:
418	# only generate libzstd.a if it's not already present
419	[ -e libzstd.a ] || $(MAKE) libzstd.a-release
420	[ -e $(DESTDIR)$(LIBDIR) ] || $(INSTALL) -d -m 755 $(DESTDIR)$(LIBDIR)/
421	@echo Installing static library
422	$(INSTALL_DATA) libzstd.a $(DESTDIR)$(LIBDIR)
423
424install-shared:
425	# only generate libzstd.so if it's not already present
426	[ -e $(LIBZSTD) ] || $(MAKE) libzstd-release
427	[ -e $(DESTDIR)$(LIBDIR) ] || $(INSTALL) -d -m 755 $(DESTDIR)$(LIBDIR)/
428	@echo Installing shared library
429	$(INSTALL_PROGRAM) $(LIBZSTD) $(DESTDIR)$(LIBDIR)
430	ln -sf $(LIBZSTD) $(DESTDIR)$(LIBDIR)/libzstd.$(SHARED_EXT_MAJOR)
431	ln -sf $(LIBZSTD) $(DESTDIR)$(LIBDIR)/libzstd.$(SHARED_EXT)
432
433install-includes:
434	[ -e $(DESTDIR)$(INCLUDEDIR) ] || $(INSTALL) -d -m 755 $(DESTDIR)$(INCLUDEDIR)/
435	@echo Installing includes
436	$(INSTALL_DATA) zstd.h $(DESTDIR)$(INCLUDEDIR)
437	$(INSTALL_DATA) common/zstd_errors.h $(DESTDIR)$(INCLUDEDIR)
438	$(INSTALL_DATA) dictBuilder/zdict.h $(DESTDIR)$(INCLUDEDIR)
439
440uninstall:
441	$(RM) $(DESTDIR)$(LIBDIR)/libzstd.a
442	$(RM) $(DESTDIR)$(LIBDIR)/libzstd.$(SHARED_EXT)
443	$(RM) $(DESTDIR)$(LIBDIR)/libzstd.$(SHARED_EXT_MAJOR)
444	$(RM) $(DESTDIR)$(LIBDIR)/$(LIBZSTD)
445	$(RM) $(DESTDIR)$(PKGCONFIGDIR)/libzstd.pc
446	$(RM) $(DESTDIR)$(INCLUDEDIR)/zstd.h
447	$(RM) $(DESTDIR)$(INCLUDEDIR)/zstd_errors.h
448	$(RM) $(DESTDIR)$(INCLUDEDIR)/zdict.h
449	@echo zstd libraries successfully uninstalled
450
451endif
452