1 # ################################################################
2 # Copyright (c) Meta Platforms, Inc. and affiliates.
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 # ################################################################
11 # This included Makefile provides the following variables :
12 # LIB_SRCDIR, LIB_BINDIR
14 # Ensure the file is not included twice
15 # Note : must be included after setting the default target
16 ifndef LIBZSTD_MK_INCLUDED
17 LIBZSTD_MK_INCLUDED := 1
19 ##################################################################
21 ##################################################################
23 # By default, library's directory is same as this included makefile
24 LIB_SRCDIR ?= $(dir $(realpath $(lastword $(MAKEFILE_LIST))))
25 LIB_BINDIR ?= $(LIBSRC_DIR)
27 # ZSTD_LIB_MINIFY is a helper variable that
28 # configures a bunch of other variables to space-optimized defaults.
32 ifneq ($(ZSTD_LIB_MINIFY), 0)
33 ZSTD_LEGACY_SUPPORT ?= 0
35 ZSTD_LEGACY_SUPPORT ?= 5
37 ZSTD_LEGACY_MULTITHREADED_API ?= 0
39 # Build size optimizations
40 ifneq ($(ZSTD_LIB_MINIFY), 0)
41 HUF_FORCE_DECOMPRESS_X1 ?= 1
42 HUF_FORCE_DECOMPRESS_X2 ?= 0
43 ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT ?= 1
44 ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG ?= 0
46 ZSTD_STRIP_ERROR_STRINGS ?= 1
48 HUF_FORCE_DECOMPRESS_X1 ?= 0
49 HUF_FORCE_DECOMPRESS_X2 ?= 0
50 ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT ?= 0
51 ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG ?= 0
53 ZSTD_STRIP_ERROR_STRINGS ?= 0
59 ZSTD_LIB_EXCLUDE_COMPRESSORS_DFAST_AND_UP ?= 0
60 ZSTD_LIB_EXCLUDE_COMPRESSORS_GREEDY_AND_UP ?= 0
62 ##################################################################
64 ##################################################################
68 # Make 4.3 doesn't support '\#' anymore (https://lwn.net/Articles/810071/)
71 # define silent mode as default (verbose mode with V=1 or VERBOSE=1)
72 # Note : must be defined _after_ the default target
73 $(V)$(VERBOSE).SILENT:
75 # When cross-compiling from linux to windows,
76 # one might need to specify TARGET_SYSTEM as "Windows."
77 # Building from Fedora fails without it.
78 # (but Ubuntu and Debian don't need to set anything)
79 TARGET_SYSTEM ?= $(OS)
82 LIBVER_SRC := $(LIB_SRCDIR)/zstd.h
83 LIBVER_MAJOR_SCRIPT:=`sed -n '/define ZSTD_VERSION_MAJOR/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < $(LIBVER_SRC)`
84 LIBVER_MINOR_SCRIPT:=`sed -n '/define ZSTD_VERSION_MINOR/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < $(LIBVER_SRC)`
85 LIBVER_PATCH_SCRIPT:=`sed -n '/define ZSTD_VERSION_RELEASE/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < $(LIBVER_SRC)`
86 LIBVER_SCRIPT:= $(LIBVER_MAJOR_SCRIPT).$(LIBVER_MINOR_SCRIPT).$(LIBVER_PATCH_SCRIPT)
87 LIBVER_MAJOR := $(shell echo $(LIBVER_MAJOR_SCRIPT))
88 LIBVER_MINOR := $(shell echo $(LIBVER_MINOR_SCRIPT))
89 LIBVER_PATCH := $(shell echo $(LIBVER_PATCH_SCRIPT))
90 LIBVER := $(shell echo $(LIBVER_SCRIPT))
91 CCVER := $(shell $(CC) --version)
92 ZSTD_VERSION?= $(LIBVER)
94 ifneq ($(ZSTD_LIB_MINIFY), 0)
95 HAVE_CC_OZ ?= $(shell echo "" | $(CC) -Oz -x c -c - -o /dev/null 2> /dev/null && echo 1 || echo 0)
96 ifneq ($(HAVE_CC_OZ), 0)
97 # Some compilers (clang) support an even more space-optimized setting.
102 CFLAGS += -fno-stack-protector -fomit-frame-pointer -fno-ident \
103 -DDYNAMIC_BMI2=0 -DNDEBUG
109 CPPFLAGS += -DXXH_NAMESPACE=ZSTD_ -DDEBUGLEVEL=$(DEBUGLEVEL)
110 ifeq ($(TARGET_SYSTEM),Windows_NT) # MinGW assumed
111 CPPFLAGS += -D__USE_MINGW_ANSI_STDIO # compatibility with %zu formatting
113 DEBUGFLAGS= -Wall -Wextra -Wcast-qual -Wcast-align -Wshadow \
114 -Wstrict-aliasing=1 -Wswitch-enum -Wdeclaration-after-statement \
115 -Wstrict-prototypes -Wundef -Wpointer-arith \
116 -Wvla -Wformat=2 -Winit-self -Wfloat-equal -Wwrite-strings \
117 -Wredundant-decls -Wmissing-prototypes -Wc++-compat
118 CFLAGS += $(DEBUGFLAGS) $(MOREFLAGS)
119 ASFLAGS += $(DEBUGFLAGS) $(MOREFLAGS) $(CFLAGS)
120 LDFLAGS += $(MOREFLAGS)
121 FLAGS = $(CPPFLAGS) $(CFLAGS) $(ASFLAGS) $(LDFLAGS)
123 ifndef ALREADY_APPENDED_NOEXECSTACK
124 export ALREADY_APPENDED_NOEXECSTACK := 1
125 ifeq ($(shell echo "int main(int argc, char* argv[]) { (void)argc; (void)argv; return 0; }" | $(CC) $(FLAGS) -z noexecstack -x c -Werror - -o $(VOID) 2>$(VOID) && echo 1 || echo 0),1)
126 LDFLAGS += -z noexecstack
128 ifeq ($(shell echo | $(CC) $(FLAGS) -Wa,--noexecstack -x assembler -Werror -c - -o $(VOID) 2>$(VOID) && echo 1 || echo 0),1)
129 CFLAGS += -Wa,--noexecstack
130 # CFLAGS are also added to ASFLAGS
131 else ifeq ($(shell echo | $(CC) $(FLAGS) -Qunused-arguments -Wa,--noexecstack -x assembler -Werror -c - -o $(VOID) 2>$(VOID) && echo 1 || echo 0),1)
132 # See e.g.: https://github.com/android/ndk/issues/171
133 CFLAGS += -Qunused-arguments -Wa,--noexecstack
134 # CFLAGS are also added to ASFLAGS
138 ifeq ($(shell echo "int main(int argc, char* argv[]) { (void)argc; (void)argv; return 0; }" | $(CC) $(FLAGS) -z cet-report=error -x c -Werror - -o $(VOID) 2>$(VOID) && echo 1 || echo 0),1)
139 LDFLAGS += -z cet-report=error
142 HAVE_COLORNEVER = $(shell echo a | grep --color=never a > /dev/null 2> /dev/null && echo 1 || echo 0)
144 ifeq ($(HAVE_COLORNEVER), 1)
145 GREP_OPTIONS += --color=never
147 GREP = grep $(GREP_OPTIONS)
149 ZSTD_COMMON_FILES := $(sort $(wildcard $(LIB_SRCDIR)/common/*.c))
150 ZSTD_COMPRESS_FILES := $(sort $(wildcard $(LIB_SRCDIR)/compress/*.c))
151 ZSTD_DECOMPRESS_FILES := $(sort $(wildcard $(LIB_SRCDIR)/decompress/*.c))
152 ZSTD_DICTBUILDER_FILES := $(sort $(wildcard $(LIB_SRCDIR)/dictBuilder/*.c))
153 ZSTD_DEPRECATED_FILES := $(sort $(wildcard $(LIB_SRCDIR)/deprecated/*.c))
156 ZSTD_DECOMPRESS_AMD64_ASM_FILES := $(sort $(wildcard $(LIB_SRCDIR)/decompress/*_amd64.S))
158 ifneq ($(ZSTD_NO_ASM), 0)
159 CPPFLAGS += -DZSTD_DISABLE_ASM
161 # Unconditionally add the ASM files they are disabled by
162 # macros in the .S file.
163 ZSTD_DECOMPRESS_FILES += $(ZSTD_DECOMPRESS_AMD64_ASM_FILES)
166 ifneq ($(HUF_FORCE_DECOMPRESS_X1), 0)
167 CFLAGS += -DHUF_FORCE_DECOMPRESS_X1
170 ifneq ($(HUF_FORCE_DECOMPRESS_X2), 0)
171 CFLAGS += -DHUF_FORCE_DECOMPRESS_X2
174 ifneq ($(ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT), 0)
175 CFLAGS += -DZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT
178 ifneq ($(ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG), 0)
179 CFLAGS += -DZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG
182 ifneq ($(ZSTD_NO_INLINE), 0)
183 CFLAGS += -DZSTD_NO_INLINE
186 ifneq ($(ZSTD_STRIP_ERROR_STRINGS), 0)
187 CFLAGS += -DZSTD_STRIP_ERROR_STRINGS
190 ifneq ($(ZSTD_LEGACY_MULTITHREADED_API), 0)
191 CFLAGS += -DZSTD_LEGACY_MULTITHREADED_API
194 ifneq ($(ZSTD_LIB_EXCLUDE_COMPRESSORS_DFAST_AND_UP), 0)
195 CFLAGS += -DZSTD_EXCLUDE_DFAST_BLOCK_COMPRESSOR -DZSTD_EXCLUDE_GREEDY_BLOCK_COMPRESSOR -DZSTD_EXCLUDE_LAZY2_BLOCK_COMPRESSOR -DZSTD_EXCLUDE_BTLAZY2_BLOCK_COMPRESSOR -DZSTD_EXCLUDE_BTOPT_BLOCK_COMPRESSOR -DZSTD_EXCLUDE_BTULTRA_BLOCK_COMPRESSOR
197 ifneq ($(ZSTD_LIB_EXCLUDE_COMPRESSORS_GREEDY_AND_UP), 0)
198 CFLAGS += -DZSTD_EXCLUDE_GREEDY_BLOCK_COMPRESSOR -DZSTD_EXCLUDE_LAZY2_BLOCK_COMPRESSOR -DZSTD_EXCLUDE_BTLAZY2_BLOCK_COMPRESSOR -DZSTD_EXCLUDE_BTOPT_BLOCK_COMPRESSOR -DZSTD_EXCLUDE_BTULTRA_BLOCK_COMPRESSOR
202 ifneq ($(ZSTD_LEGACY_SUPPORT), 0)
203 ifeq ($(shell test $(ZSTD_LEGACY_SUPPORT) -lt 8; echo $$?), 0)
204 ZSTD_LEGACY_FILES += $(shell ls $(LIB_SRCDIR)/legacy/*.c | $(GREP) 'v0[$(ZSTD_LEGACY_SUPPORT)-7]')
207 CPPFLAGS += -DZSTD_LEGACY_SUPPORT=$(ZSTD_LEGACY_SUPPORT)
209 UNAME := $(shell uname)
212 ifeq ($(UNAME), Darwin)
213 ifeq ($(shell md5 < /dev/null > /dev/null; echo $$?), 0)
216 else ifeq ($(UNAME), FreeBSD)
218 else ifeq ($(UNAME), NetBSD)
220 else ifeq ($(UNAME), OpenBSD)
225 HASH_DIR = conf_$(shell echo $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) $(ZSTD_FILES) | $(HASH) | cut -f 1 -d " " )
226 HAVE_HASH :=$(shell echo 1 | $(HASH) > /dev/null && echo 1 || echo 0)
227 ifeq ($(HAVE_HASH),0)
228 $(info warning : could not find HASH ($(HASH)), needed to differentiate builds using different flags)
229 BUILD_DIR := obj/generic_noconf
233 ZSTD_SUBDIR := $(LIB_SRCDIR)/common $(LIB_SRCDIR)/compress $(LIB_SRCDIR)/decompress $(LIB_SRCDIR)/dictBuilder $(LIB_SRCDIR)/legacy $(LIB_SRCDIR)/deprecated
234 vpath %.c $(ZSTD_SUBDIR)
235 vpath %.S $(ZSTD_SUBDIR)
237 endif # LIBZSTD_MK_INCLUDED