1# ################################################################ 2# Copyright (c) 2015-present, 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# ################################################################ 9 10# Version numbers 11LIBVER_MAJOR_SCRIPT:=`sed -n '/define ZSTD_VERSION_MAJOR/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < ./zstd.h` 12LIBVER_MINOR_SCRIPT:=`sed -n '/define ZSTD_VERSION_MINOR/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < ./zstd.h` 13LIBVER_PATCH_SCRIPT:=`sed -n '/define ZSTD_VERSION_RELEASE/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < ./zstd.h` 14LIBVER_SCRIPT:= $(LIBVER_MAJOR_SCRIPT).$(LIBVER_MINOR_SCRIPT).$(LIBVER_PATCH_SCRIPT) 15LIBVER_MAJOR := $(shell echo $(LIBVER_MAJOR_SCRIPT)) 16LIBVER_MINOR := $(shell echo $(LIBVER_MINOR_SCRIPT)) 17LIBVER_PATCH := $(shell echo $(LIBVER_PATCH_SCRIPT)) 18LIBVER := $(shell echo $(LIBVER_SCRIPT)) 19VERSION?= $(LIBVER) 20 21CPPFLAGS+= -I. -I./common -DXXH_NAMESPACE=ZSTD_ 22ifeq ($(OS),Windows_NT) # MinGW assumed 23CPPFLAGS += -D__USE_MINGW_ANSI_STDIO # compatibility with %zu formatting 24endif 25CFLAGS ?= -O3 26DEBUGFLAGS= -Wall -Wextra -Wcast-qual -Wcast-align -Wshadow \ 27 -Wstrict-aliasing=1 -Wswitch-enum -Wdeclaration-after-statement \ 28 -Wstrict-prototypes -Wundef -Wpointer-arith -Wformat-security \ 29 -Wvla -Wformat=2 -Winit-self -Wfloat-equal -Wwrite-strings \ 30 -Wredundant-decls -Wmissing-prototypes -Wc++-compat 31CFLAGS += $(DEBUGFLAGS) $(MOREFLAGS) 32FLAGS = $(CPPFLAGS) $(CFLAGS) 33 34HAVE_COLORNEVER = $(shell echo a | grep --color=never a > /dev/null 2> /dev/null && echo 1 || echo 0) 35GREP_OPTIONS ?= 36ifeq ($HAVE_COLORNEVER, 1) 37GREP_OPTIONS += --color=never 38endif 39GREP = grep $(GREP_OPTIONS) 40 41ZSTDCOMMON_FILES := $(sort $(wildcard common/*.c)) 42ZSTDCOMP_FILES := $(sort $(wildcard compress/*.c)) 43ZSTDDECOMP_FILES := $(sort $(wildcard decompress/*.c)) 44ZDICT_FILES := $(sort $(wildcard dictBuilder/*.c)) 45ZDEPR_FILES := $(sort $(wildcard deprecated/*.c)) 46ZSTD_FILES := $(ZSTDCOMMON_FILES) 47 48ZSTD_LEGACY_SUPPORT ?= 5 49ZSTD_LIB_COMPRESSION ?= 1 50ZSTD_LIB_DECOMPRESSION ?= 1 51ZSTD_LIB_DICTBUILDER ?= 1 52ZSTD_LIB_DEPRECATED ?= 1 53HUF_FORCE_DECOMPRESS_X1 ?= 0 54HUF_FORCE_DECOMPRESS_X2 ?= 0 55ZSTD_FORCE_DECOMPRESS_SHORT ?= 0 56ZSTD_FORCE_DECOMPRESS_LONG ?= 0 57ZSTD_NO_INLINE ?= 0 58ZSTD_STRIP_ERROR_STRINGS ?= 0 59 60ifeq ($(ZSTD_LIB_COMPRESSION), 0) 61 ZSTD_LIB_DICTBUILDER = 0 62 ZSTD_LIB_DEPRECATED = 0 63endif 64 65ifeq ($(ZSTD_LIB_DECOMPRESSION), 0) 66 ZSTD_LEGACY_SUPPORT = 0 67 ZSTD_LIB_DEPRECATED = 0 68endif 69 70ifneq ($(ZSTD_LIB_COMPRESSION), 0) 71 ZSTD_FILES += $(ZSTDCOMP_FILES) 72endif 73 74ifneq ($(ZSTD_LIB_DECOMPRESSION), 0) 75 ZSTD_FILES += $(ZSTDDECOMP_FILES) 76endif 77 78ifneq ($(ZSTD_LIB_DEPRECATED), 0) 79 ZSTD_FILES += $(ZDEPR_FILES) 80endif 81 82ifneq ($(ZSTD_LIB_DICTBUILDER), 0) 83 ZSTD_FILES += $(ZDICT_FILES) 84endif 85 86ifneq ($(HUF_FORCE_DECOMPRESS_X1), 0) 87 CFLAGS += -DHUF_FORCE_DECOMPRESS_X1 88endif 89 90ifneq ($(HUF_FORCE_DECOMPRESS_X2), 0) 91 CFLAGS += -DHUF_FORCE_DECOMPRESS_X2 92endif 93 94ifneq ($(ZSTD_FORCE_DECOMPRESS_SHORT), 0) 95 CFLAGS += -DZSTD_FORCE_DECOMPRESS_SHORT 96endif 97 98ifneq ($(ZSTD_FORCE_DECOMPRESS_LONG), 0) 99 CFLAGS += -DZSTD_FORCE_DECOMPRESS_LONG 100endif 101 102ifneq ($(ZSTD_NO_INLINE), 0) 103 CFLAGS += -DZSTD_NO_INLINE 104endif 105 106ifneq ($(ZSTD_STRIP_ERROR_STRINGS), 0) 107 CFLAGS += -DZSTD_STRIP_ERROR_STRINGS 108endif 109 110ifneq ($(ZSTD_LEGACY_SUPPORT), 0) 111ifeq ($(shell test $(ZSTD_LEGACY_SUPPORT) -lt 8; echo $$?), 0) 112 ZSTD_FILES += $(shell ls legacy/*.c | $(GREP) 'v0[$(ZSTD_LEGACY_SUPPORT)-7]') 113endif 114 CPPFLAGS += -I./legacy 115endif 116CPPFLAGS += -DZSTD_LEGACY_SUPPORT=$(ZSTD_LEGACY_SUPPORT) 117 118ZSTD_OBJ := $(patsubst %.c,%.o,$(ZSTD_FILES)) 119 120# macOS linker doesn't support -soname, and use different extension 121# see : https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/DynamicLibraryDesignGuidelines.html 122ifeq ($(shell uname), Darwin) 123 SHARED_EXT = dylib 124 SHARED_EXT_MAJOR = $(LIBVER_MAJOR).$(SHARED_EXT) 125 SHARED_EXT_VER = $(LIBVER).$(SHARED_EXT) 126 SONAME_FLAGS = -install_name $(LIBDIR)/libzstd.$(SHARED_EXT_MAJOR) -compatibility_version $(LIBVER_MAJOR) -current_version $(LIBVER) 127else 128 SONAME_FLAGS = -Wl,-soname=libzstd.$(SHARED_EXT).$(LIBVER_MAJOR) 129 SHARED_EXT = so 130 SHARED_EXT_MAJOR = $(SHARED_EXT).$(LIBVER_MAJOR) 131 SHARED_EXT_VER = $(SHARED_EXT).$(LIBVER) 132endif 133 134 135.PHONY: default all clean install uninstall 136 137default: lib-release 138 139all: lib 140 141libzstd.a: ARFLAGS = rcs 142libzstd.a: $(ZSTD_OBJ) 143 @echo compiling static library 144 @$(AR) $(ARFLAGS) $@ $^ 145 146libzstd.a-mt: CPPFLAGS += -DZSTD_MULTITHREAD 147libzstd.a-mt: libzstd.a 148 149ifneq (,$(filter Windows%,$(OS))) 150 151LIBZSTD = dll\libzstd.dll 152$(LIBZSTD): $(ZSTD_FILES) 153 @echo compiling dynamic library $(LIBVER) 154 @$(CC) $(FLAGS) -DZSTD_DLL_EXPORT=1 -shared $^ -o $@ 155 dlltool -D $@ -d dll\libzstd.def -l dll\libzstd.lib 156 157else 158 159LIBZSTD = libzstd.$(SHARED_EXT_VER) 160$(LIBZSTD): LDFLAGS += -shared -fPIC -fvisibility=hidden 161$(LIBZSTD): $(ZSTD_FILES) 162 @echo compiling dynamic library $(LIBVER) 163 @$(CC) $(FLAGS) $^ $(LDFLAGS) $(SONAME_FLAGS) -o $@ 164 @echo creating versioned links 165 @ln -sf $@ libzstd.$(SHARED_EXT_MAJOR) 166 @ln -sf $@ libzstd.$(SHARED_EXT) 167 168endif 169 170 171libzstd : $(LIBZSTD) 172 173libzstd-mt : CPPFLAGS += -DZSTD_MULTITHREAD 174libzstd-mt : libzstd 175 176lib: libzstd.a libzstd 177 178lib-mt: CPPFLAGS += -DZSTD_MULTITHREAD 179lib-mt: lib 180 181lib-release lib-release-mt: DEBUGFLAGS := 182lib-release: lib 183lib-release-mt: lib-mt 184 185# Special case : building library in single-thread mode _and_ without zstdmt_compress.c 186ZSTDMT_FILES = compress/zstdmt_compress.c 187ZSTD_NOMT_FILES = $(filter-out $(ZSTDMT_FILES),$(ZSTD_FILES)) 188libzstd-nomt: LDFLAGS += -shared -fPIC -fvisibility=hidden 189libzstd-nomt: $(ZSTD_NOMT_FILES) 190 @echo compiling single-thread dynamic library $(LIBVER) 191 @echo files : $(ZSTD_NOMT_FILES) 192 @$(CC) $(FLAGS) $^ $(LDFLAGS) $(SONAME_FLAGS) -o $@ 193 194clean: 195 @$(RM) -r *.dSYM # macOS-specific 196 @$(RM) core *.o *.a *.gcda *.$(SHARED_EXT) *.$(SHARED_EXT).* libzstd.pc 197 @$(RM) dll/libzstd.dll dll/libzstd.lib libzstd-nomt* 198 @$(RM) common/*.o compress/*.o decompress/*.o dictBuilder/*.o legacy/*.o deprecated/*.o 199 @echo Cleaning library completed 200 201#----------------------------------------------------------------------------- 202# make install is validated only for Linux, macOS, BSD, Hurd and Solaris targets 203#----------------------------------------------------------------------------- 204ifneq (,$(filter $(shell uname),Linux Darwin GNU/kFreeBSD GNU OpenBSD FreeBSD NetBSD DragonFly SunOS Haiku)) 205 206DESTDIR ?= 207# directory variables : GNU conventions prefer lowercase 208# see https://www.gnu.org/prep/standards/html_node/Makefile-Conventions.html 209# support both lower and uppercase (BSD), use uppercase in script 210prefix ?= /usr/local 211PREFIX ?= $(prefix) 212exec_prefix ?= $(PREFIX) 213libdir ?= $(exec_prefix)/lib 214LIBDIR ?= $(libdir) 215includedir ?= $(PREFIX)/include 216INCLUDEDIR ?= $(includedir) 217 218ifneq (,$(filter $(shell uname),FreeBSD NetBSD DragonFly)) 219PKGCONFIGDIR ?= $(PREFIX)/libdata/pkgconfig 220else 221PKGCONFIGDIR ?= $(LIBDIR)/pkgconfig 222endif 223 224ifneq (,$(filter $(shell uname),SunOS)) 225INSTALL ?= ginstall 226else 227INSTALL ?= install 228endif 229 230INSTALL_PROGRAM ?= $(INSTALL) 231INSTALL_DATA ?= $(INSTALL) -m 644 232 233 234libzstd.pc: 235libzstd.pc: libzstd.pc.in 236 @echo creating pkgconfig 237 @sed -e 's|@PREFIX@|$(PREFIX)|' \ 238 -e 's|@LIBDIR@|$(LIBDIR)|' \ 239 -e 's|@INCLUDEDIR@|$(INCLUDEDIR)|' \ 240 -e 's|@VERSION@|$(VERSION)|' \ 241 $< >$@ 242 243install: install-pc install-static install-shared install-includes 244 @echo zstd static and shared library installed 245 246install-pc: libzstd.pc 247 @$(INSTALL) -d -m 755 $(DESTDIR)$(PKGCONFIGDIR)/ 248 @$(INSTALL_DATA) libzstd.pc $(DESTDIR)$(PKGCONFIGDIR)/ 249 250install-static: libzstd.a 251 @echo Installing static library 252 @$(INSTALL) -d -m 755 $(DESTDIR)$(LIBDIR)/ 253 @$(INSTALL_DATA) libzstd.a $(DESTDIR)$(LIBDIR) 254 255install-shared: libzstd 256 @echo Installing shared library 257 @$(INSTALL) -d -m 755 $(DESTDIR)$(LIBDIR)/ 258 @$(INSTALL_PROGRAM) $(LIBZSTD) $(DESTDIR)$(LIBDIR) 259 @ln -sf $(LIBZSTD) $(DESTDIR)$(LIBDIR)/libzstd.$(SHARED_EXT_MAJOR) 260 @ln -sf $(LIBZSTD) $(DESTDIR)$(LIBDIR)/libzstd.$(SHARED_EXT) 261 262install-includes: 263 @echo Installing includes 264 @$(INSTALL) -d -m 755 $(DESTDIR)$(INCLUDEDIR)/ 265 @$(INSTALL_DATA) zstd.h $(DESTDIR)$(INCLUDEDIR) 266 @$(INSTALL_DATA) common/zstd_errors.h $(DESTDIR)$(INCLUDEDIR) 267 @$(INSTALL_DATA) deprecated/zbuff.h $(DESTDIR)$(INCLUDEDIR) # prototypes generate deprecation warnings 268 @$(INSTALL_DATA) dictBuilder/zdict.h $(DESTDIR)$(INCLUDEDIR) 269 270uninstall: 271 @$(RM) $(DESTDIR)$(LIBDIR)/libzstd.a 272 @$(RM) $(DESTDIR)$(LIBDIR)/libzstd.$(SHARED_EXT) 273 @$(RM) $(DESTDIR)$(LIBDIR)/libzstd.$(SHARED_EXT_MAJOR) 274 @$(RM) $(DESTDIR)$(LIBDIR)/$(LIBZSTD) 275 @$(RM) $(DESTDIR)$(PKGCONFIGDIR)/libzstd.pc 276 @$(RM) $(DESTDIR)$(INCLUDEDIR)/zstd.h 277 @$(RM) $(DESTDIR)$(INCLUDEDIR)/zstd_errors.h 278 @$(RM) $(DESTDIR)$(INCLUDEDIR)/zbuff.h # Deprecated streaming functions 279 @$(RM) $(DESTDIR)$(INCLUDEDIR)/zdict.h 280 @echo zstd libraries successfully uninstalled 281 282endif 283