1*3117ece4Schristos# ################################################################ 2*3117ece4Schristos# Copyright (c) Meta Platforms, Inc. and affiliates. 3*3117ece4Schristos# All rights reserved. 4*3117ece4Schristos# 5*3117ece4Schristos# This source code is licensed under both the BSD-style license (found in the 6*3117ece4Schristos# LICENSE file in the root directory of this source tree) and the GPLv2 (found 7*3117ece4Schristos# in the COPYING file in the root directory of this source tree). 8*3117ece4Schristos# You may select, at your option, one of the above-listed licenses. 9*3117ece4Schristos# ################################################################ 10*3117ece4Schristos 11*3117ece4Schristos# This included Makefile provides the following variables : 12*3117ece4Schristos# LIB_SRCDIR, LIB_BINDIR 13*3117ece4Schristos 14*3117ece4Schristos# Ensure the file is not included twice 15*3117ece4Schristos# Note : must be included after setting the default target 16*3117ece4Schristosifndef LIBZSTD_MK_INCLUDED 17*3117ece4SchristosLIBZSTD_MK_INCLUDED := 1 18*3117ece4Schristos 19*3117ece4Schristos################################################################## 20*3117ece4Schristos# Input Variables 21*3117ece4Schristos################################################################## 22*3117ece4Schristos 23*3117ece4Schristos# By default, library's directory is same as this included makefile 24*3117ece4SchristosLIB_SRCDIR ?= $(dir $(realpath $(lastword $(MAKEFILE_LIST)))) 25*3117ece4SchristosLIB_BINDIR ?= $(LIBSRC_DIR) 26*3117ece4Schristos 27*3117ece4Schristos# ZSTD_LIB_MINIFY is a helper variable that 28*3117ece4Schristos# configures a bunch of other variables to space-optimized defaults. 29*3117ece4SchristosZSTD_LIB_MINIFY ?= 0 30*3117ece4Schristos 31*3117ece4Schristos# Legacy support 32*3117ece4Schristosifneq ($(ZSTD_LIB_MINIFY), 0) 33*3117ece4Schristos ZSTD_LEGACY_SUPPORT ?= 0 34*3117ece4Schristoselse 35*3117ece4Schristos ZSTD_LEGACY_SUPPORT ?= 5 36*3117ece4Schristosendif 37*3117ece4SchristosZSTD_LEGACY_MULTITHREADED_API ?= 0 38*3117ece4Schristos 39*3117ece4Schristos# Build size optimizations 40*3117ece4Schristosifneq ($(ZSTD_LIB_MINIFY), 0) 41*3117ece4Schristos HUF_FORCE_DECOMPRESS_X1 ?= 1 42*3117ece4Schristos HUF_FORCE_DECOMPRESS_X2 ?= 0 43*3117ece4Schristos ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT ?= 1 44*3117ece4Schristos ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG ?= 0 45*3117ece4Schristos ZSTD_NO_INLINE ?= 1 46*3117ece4Schristos ZSTD_STRIP_ERROR_STRINGS ?= 1 47*3117ece4Schristoselse 48*3117ece4Schristos HUF_FORCE_DECOMPRESS_X1 ?= 0 49*3117ece4Schristos HUF_FORCE_DECOMPRESS_X2 ?= 0 50*3117ece4Schristos ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT ?= 0 51*3117ece4Schristos ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG ?= 0 52*3117ece4Schristos ZSTD_NO_INLINE ?= 0 53*3117ece4Schristos ZSTD_STRIP_ERROR_STRINGS ?= 0 54*3117ece4Schristosendif 55*3117ece4Schristos 56*3117ece4Schristos# Assembly support 57*3117ece4SchristosZSTD_NO_ASM ?= 0 58*3117ece4Schristos 59*3117ece4SchristosZSTD_LIB_EXCLUDE_COMPRESSORS_DFAST_AND_UP ?= 0 60*3117ece4SchristosZSTD_LIB_EXCLUDE_COMPRESSORS_GREEDY_AND_UP ?= 0 61*3117ece4Schristos 62*3117ece4Schristos################################################################## 63*3117ece4Schristos# libzstd helpers 64*3117ece4Schristos################################################################## 65*3117ece4Schristos 66*3117ece4SchristosVOID ?= /dev/null 67*3117ece4Schristos 68*3117ece4Schristos# Make 4.3 doesn't support '\#' anymore (https://lwn.net/Articles/810071/) 69*3117ece4SchristosNUM_SYMBOL := \# 70*3117ece4Schristos 71*3117ece4Schristos# define silent mode as default (verbose mode with V=1 or VERBOSE=1) 72*3117ece4Schristos# Note : must be defined _after_ the default target 73*3117ece4Schristos$(V)$(VERBOSE).SILENT: 74*3117ece4Schristos 75*3117ece4Schristos# When cross-compiling from linux to windows, 76*3117ece4Schristos# one might need to specify TARGET_SYSTEM as "Windows." 77*3117ece4Schristos# Building from Fedora fails without it. 78*3117ece4Schristos# (but Ubuntu and Debian don't need to set anything) 79*3117ece4SchristosTARGET_SYSTEM ?= $(OS) 80*3117ece4Schristos 81*3117ece4Schristos# Version numbers 82*3117ece4SchristosLIBVER_SRC := $(LIB_SRCDIR)/zstd.h 83*3117ece4SchristosLIBVER_MAJOR_SCRIPT:=`sed -n '/define ZSTD_VERSION_MAJOR/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < $(LIBVER_SRC)` 84*3117ece4SchristosLIBVER_MINOR_SCRIPT:=`sed -n '/define ZSTD_VERSION_MINOR/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < $(LIBVER_SRC)` 85*3117ece4SchristosLIBVER_PATCH_SCRIPT:=`sed -n '/define ZSTD_VERSION_RELEASE/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < $(LIBVER_SRC)` 86*3117ece4SchristosLIBVER_SCRIPT:= $(LIBVER_MAJOR_SCRIPT).$(LIBVER_MINOR_SCRIPT).$(LIBVER_PATCH_SCRIPT) 87*3117ece4SchristosLIBVER_MAJOR := $(shell echo $(LIBVER_MAJOR_SCRIPT)) 88*3117ece4SchristosLIBVER_MINOR := $(shell echo $(LIBVER_MINOR_SCRIPT)) 89*3117ece4SchristosLIBVER_PATCH := $(shell echo $(LIBVER_PATCH_SCRIPT)) 90*3117ece4SchristosLIBVER := $(shell echo $(LIBVER_SCRIPT)) 91*3117ece4SchristosCCVER := $(shell $(CC) --version) 92*3117ece4SchristosZSTD_VERSION?= $(LIBVER) 93*3117ece4Schristos 94*3117ece4Schristosifneq ($(ZSTD_LIB_MINIFY), 0) 95*3117ece4Schristos HAVE_CC_OZ ?= $(shell echo "" | $(CC) -Oz -x c -c - -o /dev/null 2> /dev/null && echo 1 || echo 0) 96*3117ece4Schristosifneq ($(HAVE_CC_OZ), 0) 97*3117ece4Schristos # Some compilers (clang) support an even more space-optimized setting. 98*3117ece4Schristos CFLAGS += -Oz 99*3117ece4Schristoselse 100*3117ece4Schristos CFLAGS += -Os 101*3117ece4Schristosendif 102*3117ece4Schristos CFLAGS += -fno-stack-protector -fomit-frame-pointer -fno-ident \ 103*3117ece4Schristos -DDYNAMIC_BMI2=0 -DNDEBUG 104*3117ece4Schristoselse 105*3117ece4Schristos CFLAGS ?= -O3 106*3117ece4Schristosendif 107*3117ece4Schristos 108*3117ece4SchristosDEBUGLEVEL ?= 0 109*3117ece4SchristosCPPFLAGS += -DXXH_NAMESPACE=ZSTD_ -DDEBUGLEVEL=$(DEBUGLEVEL) 110*3117ece4Schristosifeq ($(TARGET_SYSTEM),Windows_NT) # MinGW assumed 111*3117ece4Schristos CPPFLAGS += -D__USE_MINGW_ANSI_STDIO # compatibility with %zu formatting 112*3117ece4Schristosendif 113*3117ece4SchristosDEBUGFLAGS= -Wall -Wextra -Wcast-qual -Wcast-align -Wshadow \ 114*3117ece4Schristos -Wstrict-aliasing=1 -Wswitch-enum -Wdeclaration-after-statement \ 115*3117ece4Schristos -Wstrict-prototypes -Wundef -Wpointer-arith \ 116*3117ece4Schristos -Wvla -Wformat=2 -Winit-self -Wfloat-equal -Wwrite-strings \ 117*3117ece4Schristos -Wredundant-decls -Wmissing-prototypes -Wc++-compat 118*3117ece4SchristosCFLAGS += $(DEBUGFLAGS) $(MOREFLAGS) 119*3117ece4SchristosASFLAGS += $(DEBUGFLAGS) $(MOREFLAGS) $(CFLAGS) 120*3117ece4SchristosLDFLAGS += $(MOREFLAGS) 121*3117ece4SchristosFLAGS = $(CPPFLAGS) $(CFLAGS) $(ASFLAGS) $(LDFLAGS) 122*3117ece4Schristos 123*3117ece4Schristosifndef ALREADY_APPENDED_NOEXECSTACK 124*3117ece4Schristosexport ALREADY_APPENDED_NOEXECSTACK := 1 125*3117ece4Schristosifeq ($(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*3117ece4SchristosLDFLAGS += -z noexecstack 127*3117ece4Schristosendif 128*3117ece4Schristosifeq ($(shell echo | $(CC) $(FLAGS) -Wa,--noexecstack -x assembler -Werror -c - -o $(VOID) 2>$(VOID) && echo 1 || echo 0),1) 129*3117ece4SchristosCFLAGS += -Wa,--noexecstack 130*3117ece4Schristos# CFLAGS are also added to ASFLAGS 131*3117ece4Schristoselse ifeq ($(shell echo | $(CC) $(FLAGS) -Qunused-arguments -Wa,--noexecstack -x assembler -Werror -c - -o $(VOID) 2>$(VOID) && echo 1 || echo 0),1) 132*3117ece4Schristos# See e.g.: https://github.com/android/ndk/issues/171 133*3117ece4SchristosCFLAGS += -Qunused-arguments -Wa,--noexecstack 134*3117ece4Schristos# CFLAGS are also added to ASFLAGS 135*3117ece4Schristosendif 136*3117ece4Schristosendif 137*3117ece4Schristos 138*3117ece4Schristosifeq ($(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*3117ece4SchristosLDFLAGS += -z cet-report=error 140*3117ece4Schristosendif 141*3117ece4Schristos 142*3117ece4SchristosHAVE_COLORNEVER = $(shell echo a | grep --color=never a > /dev/null 2> /dev/null && echo 1 || echo 0) 143*3117ece4SchristosGREP_OPTIONS ?= 144*3117ece4Schristosifeq ($(HAVE_COLORNEVER), 1) 145*3117ece4Schristos GREP_OPTIONS += --color=never 146*3117ece4Schristosendif 147*3117ece4SchristosGREP = grep $(GREP_OPTIONS) 148*3117ece4Schristos 149*3117ece4SchristosZSTD_COMMON_FILES := $(sort $(wildcard $(LIB_SRCDIR)/common/*.c)) 150*3117ece4SchristosZSTD_COMPRESS_FILES := $(sort $(wildcard $(LIB_SRCDIR)/compress/*.c)) 151*3117ece4SchristosZSTD_DECOMPRESS_FILES := $(sort $(wildcard $(LIB_SRCDIR)/decompress/*.c)) 152*3117ece4SchristosZSTD_DICTBUILDER_FILES := $(sort $(wildcard $(LIB_SRCDIR)/dictBuilder/*.c)) 153*3117ece4SchristosZSTD_DEPRECATED_FILES := $(sort $(wildcard $(LIB_SRCDIR)/deprecated/*.c)) 154*3117ece4SchristosZSTD_LEGACY_FILES := 155*3117ece4Schristos 156*3117ece4SchristosZSTD_DECOMPRESS_AMD64_ASM_FILES := $(sort $(wildcard $(LIB_SRCDIR)/decompress/*_amd64.S)) 157*3117ece4Schristos 158*3117ece4Schristosifneq ($(ZSTD_NO_ASM), 0) 159*3117ece4Schristos CPPFLAGS += -DZSTD_DISABLE_ASM 160*3117ece4Schristoselse 161*3117ece4Schristos # Unconditionally add the ASM files they are disabled by 162*3117ece4Schristos # macros in the .S file. 163*3117ece4Schristos ZSTD_DECOMPRESS_FILES += $(ZSTD_DECOMPRESS_AMD64_ASM_FILES) 164*3117ece4Schristosendif 165*3117ece4Schristos 166*3117ece4Schristosifneq ($(HUF_FORCE_DECOMPRESS_X1), 0) 167*3117ece4Schristos CFLAGS += -DHUF_FORCE_DECOMPRESS_X1 168*3117ece4Schristosendif 169*3117ece4Schristos 170*3117ece4Schristosifneq ($(HUF_FORCE_DECOMPRESS_X2), 0) 171*3117ece4Schristos CFLAGS += -DHUF_FORCE_DECOMPRESS_X2 172*3117ece4Schristosendif 173*3117ece4Schristos 174*3117ece4Schristosifneq ($(ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT), 0) 175*3117ece4Schristos CFLAGS += -DZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT 176*3117ece4Schristosendif 177*3117ece4Schristos 178*3117ece4Schristosifneq ($(ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG), 0) 179*3117ece4Schristos CFLAGS += -DZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG 180*3117ece4Schristosendif 181*3117ece4Schristos 182*3117ece4Schristosifneq ($(ZSTD_NO_INLINE), 0) 183*3117ece4Schristos CFLAGS += -DZSTD_NO_INLINE 184*3117ece4Schristosendif 185*3117ece4Schristos 186*3117ece4Schristosifneq ($(ZSTD_STRIP_ERROR_STRINGS), 0) 187*3117ece4Schristos CFLAGS += -DZSTD_STRIP_ERROR_STRINGS 188*3117ece4Schristosendif 189*3117ece4Schristos 190*3117ece4Schristosifneq ($(ZSTD_LEGACY_MULTITHREADED_API), 0) 191*3117ece4Schristos CFLAGS += -DZSTD_LEGACY_MULTITHREADED_API 192*3117ece4Schristosendif 193*3117ece4Schristos 194*3117ece4Schristosifneq ($(ZSTD_LIB_EXCLUDE_COMPRESSORS_DFAST_AND_UP), 0) 195*3117ece4Schristos 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 196*3117ece4Schristoselse 197*3117ece4Schristosifneq ($(ZSTD_LIB_EXCLUDE_COMPRESSORS_GREEDY_AND_UP), 0) 198*3117ece4Schristos 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 199*3117ece4Schristosendif 200*3117ece4Schristosendif 201*3117ece4Schristos 202*3117ece4Schristosifneq ($(ZSTD_LEGACY_SUPPORT), 0) 203*3117ece4Schristosifeq ($(shell test $(ZSTD_LEGACY_SUPPORT) -lt 8; echo $$?), 0) 204*3117ece4Schristos ZSTD_LEGACY_FILES += $(shell ls $(LIB_SRCDIR)/legacy/*.c | $(GREP) 'v0[$(ZSTD_LEGACY_SUPPORT)-7]') 205*3117ece4Schristosendif 206*3117ece4Schristosendif 207*3117ece4SchristosCPPFLAGS += -DZSTD_LEGACY_SUPPORT=$(ZSTD_LEGACY_SUPPORT) 208*3117ece4Schristos 209*3117ece4SchristosUNAME := $(shell uname) 210*3117ece4Schristos 211*3117ece4Schristosifndef BUILD_DIR 212*3117ece4Schristosifeq ($(UNAME), Darwin) 213*3117ece4Schristos ifeq ($(shell md5 < /dev/null > /dev/null; echo $$?), 0) 214*3117ece4Schristos HASH ?= md5 215*3117ece4Schristos endif 216*3117ece4Schristoselse ifeq ($(UNAME), FreeBSD) 217*3117ece4Schristos HASH ?= gmd5sum 218*3117ece4Schristoselse ifeq ($(UNAME), NetBSD) 219*3117ece4Schristos HASH ?= md5 -n 220*3117ece4Schristoselse ifeq ($(UNAME), OpenBSD) 221*3117ece4Schristos HASH ?= md5 222*3117ece4Schristosendif 223*3117ece4SchristosHASH ?= md5sum 224*3117ece4Schristos 225*3117ece4SchristosHASH_DIR = conf_$(shell echo $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) $(ZSTD_FILES) | $(HASH) | cut -f 1 -d " " ) 226*3117ece4SchristosHAVE_HASH :=$(shell echo 1 | $(HASH) > /dev/null && echo 1 || echo 0) 227*3117ece4Schristosifeq ($(HAVE_HASH),0) 228*3117ece4Schristos $(info warning : could not find HASH ($(HASH)), needed to differentiate builds using different flags) 229*3117ece4Schristos BUILD_DIR := obj/generic_noconf 230*3117ece4Schristosendif 231*3117ece4Schristosendif # BUILD_DIR 232*3117ece4Schristos 233*3117ece4SchristosZSTD_SUBDIR := $(LIB_SRCDIR)/common $(LIB_SRCDIR)/compress $(LIB_SRCDIR)/decompress $(LIB_SRCDIR)/dictBuilder $(LIB_SRCDIR)/legacy $(LIB_SRCDIR)/deprecated 234*3117ece4Schristosvpath %.c $(ZSTD_SUBDIR) 235*3117ece4Schristosvpath %.S $(ZSTD_SUBDIR) 236*3117ece4Schristos 237*3117ece4Schristosendif # LIBZSTD_MK_INCLUDED 238