11# ################################################################
2- # Copyright (c) 2015-2021, Yann Collet, Facebook, Inc.
2+ # Copyright (c) Yann Collet, Facebook, Inc.
33# All rights reserved.
44#
55# This source code is licensed under both the BSD-style license (found in the
88# You may select, at your option, one of the above-listed licenses.
99# ################################################################
1010
11+ # Note: by default, the static library is built single-threaded and dynamic library is built
12+ # multi-threaded. It is possible to force multi or single threaded builds by appending
13+ # -mt or -nomt to the build target (like lib-mt for multi-threaded, lib-nomt for single-threaded).
1114.PHONY : default
1215default : lib-release
1316
@@ -68,6 +71,10 @@ DEBUGFLAGS= -Wall -Wextra -Wcast-qual -Wcast-align -Wshadow \
6871CFLAGS += $(DEBUGFLAGS ) $(MOREFLAGS )
6972FLAGS = $(CPPFLAGS ) $(CFLAGS )
7073
74+ CPPFLAGS_DYNLIB = -DZSTD_MULTITHREAD # dynamic library build defaults to multi-threaded
75+ LDFLAGS_DYNLIB = -pthread
76+ CPPFLAGS_STATLIB = # static library build defaults to single-threaded
77+
7178HAVE_COLORNEVER = $(shell echo a | grep --color=never a > /dev/null 2> /dev/null && echo 1 || echo 0)
7279GREP_OPTIONS ?=
7380ifeq ($HAVE_COLORNEVER, 1)
9198ZSTD_LIB_COMPRESSION ?= 1
9299ZSTD_LIB_DECOMPRESSION ?= 1
93100ZSTD_LIB_DICTBUILDER ?= 1
94- ZSTD_LIB_DEPRECATED ?= 1
101+ ZSTD_LIB_DEPRECATED ?= 0
95102
96103# Legacy support
97104ZSTD_LEGACY_SUPPORT ?= 5
@@ -176,7 +183,9 @@ UNAME := $(shell uname)
176183
177184ifndef BUILD_DIR
178185ifeq ($(UNAME ) , Darwin)
179- HASH ?= md5
186+ ifeq ($(shell md5 < /dev/null > /dev/null; echo $$?), 0)
187+ HASH ?= md5
188+ endif
180189else ifeq ($(UNAME), FreeBSD)
181190 HASH ?= gmd5sum
182191else ifeq ($(UNAME), NetBSD)
@@ -222,6 +231,7 @@ all: lib
222231
223232
224233.PHONY : libzstd.a # must be run every time
234+ libzstd.a : CPPFLAGS += $(CPPFLAGS_STATLIB )
225235
226236ifndef BUILD_DIR
227237# determine BUILD_DIR from compilation flags
@@ -238,7 +248,10 @@ ZSTD_STATLIB_OBJ := $(addprefix $(ZSTD_STATLIB_DIR)/,$(ZSTD_LOCAL_OBJ))
238248$(ZSTD_STATLIB ) : ARFLAGS = rcs
239249$(ZSTD_STATLIB ) : | $(ZSTD_STATLIB_DIR )
240250$(ZSTD_STATLIB ) : $(ZSTD_STATLIB_OBJ )
241- @echo compiling static library
251+ # Check for multithread flag at target execution time
252+ $(if $(filter -DZSTD_MULTITHREAD,$(CPPFLAGS)),\
253+ @echo compiling multi-threaded static library $(LIBVER),\
254+ @echo compiling single-threaded static library $(LIBVER))
242255 $(AR) $(ARFLAGS) $@ $^
243256
244257libzstd.a : $(ZSTD_STATLIB )
@@ -257,8 +270,9 @@ else # not Windows
257270
258271LIBZSTD = libzstd.$(SHARED_EXT_VER )
259272.PHONY : $(LIBZSTD ) # must be run every time
260- $(LIBZSTD ) : CFLAGS += -fPIC -fvisibility=hidden
261- $(LIBZSTD ) : LDFLAGS += -shared
273+ $(LIBZSTD ) : CPPFLAGS += $(CPPFLAGS_DYNLIB )
274+ $(LIBZSTD ) : CFLAGS += -fPIC -fvisibility=hidden
275+ $(LIBZSTD ) : LDFLAGS += -shared $(LDFLAGS_DYNLIB )
262276
263277ifndef BUILD_DIR
264278# determine BUILD_DIR from compilation flags
@@ -275,7 +289,10 @@ ZSTD_DYNLIB_OBJ := $(addprefix $(ZSTD_DYNLIB_DIR)/,$(ZSTD_LOCAL_OBJ))
275289
276290$(ZSTD_DYNLIB ) : | $(ZSTD_DYNLIB_DIR )
277291$(ZSTD_DYNLIB ) : $(ZSTD_DYNLIB_OBJ )
278- @echo compiling dynamic library $(LIBVER )
292+ # Check for multithread flag at target execution time
293+ $(if $(filter -DZSTD_MULTITHREAD,$(CPPFLAGS)),\
294+ @echo compiling multi-threaded dynamic library $(LIBVER),\
295+ @echo compiling single-threaded dynamic library $(LIBVER))
279296 $(CC) $(FLAGS) $^ $(LDFLAGS) $(SONAME_FLAGS) -o $@
280297 @echo creating versioned links
281298 ln -sf $@ libzstd.$(SHARED_EXT_MAJOR)
@@ -297,10 +314,17 @@ lib : libzstd.a libzstd
297314# note : do not define lib-mt or lib-release as .PHONY
298315# make does not consider implicit pattern rule for .PHONY target
299316
300- % -mt : CPPFLAGS += -DZSTD_MULTITHREAD
301- % -mt : LDFLAGS += -pthread
317+ % -mt : CPPFLAGS_DYNLIB := -DZSTD_MULTITHREAD
318+ % -mt : CPPFLAGS_STATLIB := -DZSTD_MULTITHREAD
319+ % -mt : LDFLAGS_DYNLIB := -pthread
302320% -mt : %
303- @echo multi-threading build completed
321+ @echo multi-threaded build completed
322+
323+ % -nomt : CPPFLAGS_DYNLIB :=
324+ % -nomt : LDFLAGS_DYNLIB :=
325+ % -nomt : CPPFLAGS_STATLIB :=
326+ % -nomt : %
327+ @echo single-threaded build completed
304328
305329% -release : DEBUGFLAGS :=
306330% -release : %
@@ -332,7 +356,8 @@ include $(wildcard $(DEPFILES))
332356# Special case : building library in single-thread mode _and_ without zstdmt_compress.c
333357ZSTDMT_FILES = compress/zstdmt_compress.c
334358ZSTD_NOMT_FILES = $(filter-out $(ZSTDMT_FILES ) ,$(ZSTD_FILES ) )
335- libzstd-nomt : LDFLAGS += -shared -fPIC -fvisibility=hidden
359+ libzstd-nomt : CFLAGS += -fPIC -fvisibility=hidden
360+ libzstd-nomt : LDFLAGS += -shared
336361libzstd-nomt : $(ZSTD_NOMT_FILES )
337362 @echo compiling single-thread dynamic library $(LIBVER )
338363 @echo files : $(ZSTD_NOMT_FILES )
@@ -411,17 +436,20 @@ libzstd.pc: libzstd.pc.in
411436install : install-pc install-static install-shared install-includes
412437 @echo zstd static and shared library installed
413438
439+ .PHONY : install-pc
414440install-pc : libzstd.pc
415441 [ -e $( DESTDIR) $( PKGCONFIGDIR) ] || $(INSTALL ) -d -m 755 $(DESTDIR )$(PKGCONFIGDIR ) /
416442 $(INSTALL_DATA ) libzstd.pc $(DESTDIR )$(PKGCONFIGDIR ) /
417443
444+ .PHONY : install-static
418445install-static :
419446 # only generate libzstd.a if it's not already present
420447 [ -e libzstd.a ] || $(MAKE ) libzstd.a-release
421448 [ -e $( DESTDIR) $( LIBDIR) ] || $(INSTALL ) -d -m 755 $(DESTDIR )$(LIBDIR ) /
422449 @echo Installing static library
423450 $(INSTALL_DATA ) libzstd.a $(DESTDIR )$(LIBDIR )
424451
452+ .PHONY : install-shared
425453install-shared :
426454 # only generate libzstd.so if it's not already present
427455 [ -e $( LIBZSTD) ] || $(MAKE ) libzstd-release
@@ -431,12 +459,13 @@ install-shared:
431459 ln -sf $(LIBZSTD ) $(DESTDIR )$(LIBDIR ) /libzstd.$(SHARED_EXT_MAJOR )
432460 ln -sf $(LIBZSTD ) $(DESTDIR )$(LIBDIR ) /libzstd.$(SHARED_EXT )
433461
462+ .PHONY : install-includes
434463install-includes :
435464 [ -e $( DESTDIR) $( INCLUDEDIR) ] || $(INSTALL ) -d -m 755 $(DESTDIR )$(INCLUDEDIR ) /
436465 @echo Installing includes
437466 $(INSTALL_DATA ) zstd.h $(DESTDIR )$(INCLUDEDIR )
438- $(INSTALL_DATA ) common/ zstd_errors.h $(DESTDIR )$(INCLUDEDIR )
439- $(INSTALL_DATA ) dictBuilder/ zdict.h $(DESTDIR )$(INCLUDEDIR )
467+ $(INSTALL_DATA ) zstd_errors.h $(DESTDIR )$(INCLUDEDIR )
468+ $(INSTALL_DATA ) zdict.h $(DESTDIR )$(INCLUDEDIR )
440469
441470.PHONY : uninstall
442471uninstall :
0 commit comments