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# zstd : Command Line Utility, supporting gzip-like arguments 10# zstd32 : Same as zstd, but forced to compile in 32-bits mode 11# zstd_nolegacy : zstd without support of decompression of legacy versions 12# zstd-small : minimal zstd without dictionary builder and benchmark 13# zstd-compress : compressor-only version of zstd 14# zstd-decompress : decompressor-only version of zstd 15# ########################################################################## 16 17ZSTDDIR = ../lib 18 19# Version numbers 20LIBVER_SRC := $(ZSTDDIR)/zstd.h 21LIBVER_MAJOR_SCRIPT:=`sed -n '/define ZSTD_VERSION_MAJOR/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < $(LIBVER_SRC)` 22LIBVER_MINOR_SCRIPT:=`sed -n '/define ZSTD_VERSION_MINOR/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < $(LIBVER_SRC)` 23LIBVER_PATCH_SCRIPT:=`sed -n '/define ZSTD_VERSION_RELEASE/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < $(LIBVER_SRC)` 24LIBVER_SCRIPT:= $(LIBVER_MAJOR_SCRIPT).$(LIBVER_MINOR_SCRIPT).$(LIBVER_PATCH_SCRIPT) 25LIBVER_MAJOR := $(shell echo $(LIBVER_MAJOR_SCRIPT)) 26LIBVER_MINOR := $(shell echo $(LIBVER_MINOR_SCRIPT)) 27LIBVER_PATCH := $(shell echo $(LIBVER_PATCH_SCRIPT)) 28LIBVER := $(shell echo $(LIBVER_SCRIPT)) 29 30ZSTD_VERSION=$(LIBVER) 31 32ifeq ($(shell $(CC) -v 2>&1 | grep -c "gcc version "), 1) 33ALIGN_LOOP = -falign-loops=32 34else 35ALIGN_LOOP = 36endif 37 38CPPFLAGS+= -I$(ZSTDDIR) -I$(ZSTDDIR)/common -I$(ZSTDDIR)/compress \ 39 -I$(ZSTDDIR)/dictBuilder \ 40 -DXXH_NAMESPACE=ZSTD_ 41CFLAGS ?= -O3 42DEBUGFLAGS+=-Wall -Wextra -Wcast-qual -Wcast-align -Wshadow \ 43 -Wstrict-aliasing=1 -Wswitch-enum -Wdeclaration-after-statement \ 44 -Wstrict-prototypes -Wundef -Wpointer-arith -Wformat-security \ 45 -Wvla -Wformat=2 -Winit-self -Wfloat-equal -Wwrite-strings \ 46 -Wredundant-decls 47CFLAGS += $(DEBUGFLAGS) $(MOREFLAGS) 48FLAGS = $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) 49 50 51ZSTDCOMMON_FILES := $(ZSTDDIR)/common/*.c 52ZSTDCOMP_FILES := $(ZSTDDIR)/compress/*.c 53ZSTDDECOMP_FILES := $(ZSTDDIR)/decompress/*.c 54ZSTD_FILES := $(ZSTDDECOMP_FILES) $(ZSTDCOMMON_FILES) $(ZSTDCOMP_FILES) 55ZDICT_FILES := $(ZSTDDIR)/dictBuilder/*.c 56ZSTDDECOMP_O = $(ZSTDDIR)/decompress/zstd_decompress.o 57 58ZSTD_LEGACY_SUPPORT ?= 4 59ZSTDLEGACY_FILES := 60ifneq ($(ZSTD_LEGACY_SUPPORT), 0) 61ifeq ($(shell test $(ZSTD_LEGACY_SUPPORT) -lt 8; echo $$?), 0) 62 ZSTDLEGACY_FILES += $(shell ls $(ZSTDDIR)/legacy/*.c | grep 'v0[$(ZSTD_LEGACY_SUPPORT)-7]') 63endif 64 CPPFLAGS += -I$(ZSTDDIR)/legacy 65else 66endif 67 68# Sort files in alphabetical order for reproducible builds 69ZSTDLIB_FILES := $(sort $(wildcard $(ZSTD_FILES)) $(wildcard $(ZSTDLEGACY_FILES)) $(wildcard $(ZDICT_FILES))) 70 71# Define *.exe as extension for Windows systems 72ifneq (,$(filter Windows%,$(OS))) 73EXT =.exe 74RES64_FILE = windres/zstd64.res 75RES32_FILE = windres/zstd32.res 76ifneq (,$(filter x86_64%,$(shell $(CC) -dumpmachine))) 77 RES_FILE = $(RES64_FILE) 78else 79 RES_FILE = $(RES32_FILE) 80endif 81else 82EXT = 83endif 84 85VOID = /dev/null 86 87# thread detection 88NO_THREAD_MSG := ==> no threads, building without multithreading support 89HAVE_PTHREAD := $(shell printf '\#include <pthread.h>\nint main(void) { return 0; }' | $(CC) $(FLAGS) -o have_pthread$(EXT) -x c - -pthread 2> $(VOID) && rm have_pthread$(EXT) && echo 1 || echo 0) 90HAVE_THREAD := $(shell [ "$(HAVE_PTHREAD)" -eq "1" -o -n "$(filter Windows%,$(OS))" ] && echo 1 || echo 0) 91ifeq ($(HAVE_THREAD), 1) 92THREAD_MSG := ==> building with threading support 93THREAD_CPP := -DZSTD_MULTITHREAD 94THREAD_LD := -pthread 95else 96THREAD_MSG := $(NO_THREAD_MSG) 97endif 98 99# zlib detection 100NO_ZLIB_MSG := ==> no zlib, building zstd without .gz support 101HAVE_ZLIB := $(shell printf '\#include <zlib.h>\nint main(void) { return 0; }' | $(CC) $(FLAGS) -o have_zlib$(EXT) -x c - -lz 2> $(VOID) && rm have_zlib$(EXT) && echo 1 || echo 0) 102ifeq ($(HAVE_ZLIB), 1) 103ZLIB_MSG := ==> building zstd with .gz compression support 104ZLIBCPP = -DZSTD_GZCOMPRESS -DZSTD_GZDECOMPRESS 105ZLIBLD = -lz 106else 107ZLIB_MSG := $(NO_ZLIB_MSG) 108endif 109 110# lzma detection 111NO_LZMA_MSG := ==> no liblzma, building zstd without .xz/.lzma support 112HAVE_LZMA := $(shell printf '\#include <lzma.h>\nint main(void) { return 0; }' | $(CC) $(FLAGS) -o have_lzma$(EXT) -x c - -llzma 2> $(VOID) && rm have_lzma$(EXT) && echo 1 || echo 0) 113ifeq ($(HAVE_LZMA), 1) 114LZMA_MSG := ==> building zstd with .xz/.lzma compression support 115LZMACPP = -DZSTD_LZMACOMPRESS -DZSTD_LZMADECOMPRESS 116LZMALD = -llzma 117else 118LZMA_MSG := $(NO_LZMA_MSG) 119endif 120 121# lz4 detection 122NO_LZ4_MSG := ==> no liblz4, building zstd without .lz4 support 123HAVE_LZ4 := $(shell printf '\#include <lz4frame.h>\n\#include <lz4.h>\nint main(void) { return 0; }' | $(CC) $(FLAGS) -o have_lz4$(EXT) -x c - -llz4 2> $(VOID) && rm have_lz4$(EXT) && echo 1 || echo 0) 124ifeq ($(HAVE_LZ4), 1) 125LZ4_MSG := ==> building zstd with .lz4 compression support 126LZ4CPP = -DZSTD_LZ4COMPRESS -DZSTD_LZ4DECOMPRESS 127LZ4LD = -llz4 128else 129LZ4_MSG := $(NO_LZ4_MSG) 130endif 131 132.PHONY: default 133default: zstd-release 134 135.PHONY: all 136all: zstd 137 138.PHONY: allVariants 139allVariants: zstd zstd-compress zstd-decompress zstd-small zstd-nolegacy 140 141$(ZSTDDECOMP_O): CFLAGS += $(ALIGN_LOOP) 142 143zstd : CPPFLAGS += $(THREAD_CPP) $(ZLIBCPP) $(LZMACPP) $(LZ4CPP) 144zstd : LDFLAGS += $(THREAD_LD) $(ZLIBLD) $(LZMALD) $(LZ4LD) 145zstd : CPPFLAGS += -DZSTD_LEGACY_SUPPORT=$(ZSTD_LEGACY_SUPPORT) 146zstd : $(ZSTDLIB_FILES) zstdcli.o fileio.o bench.o datagen.o dibio.o 147 @echo "$(THREAD_MSG)" 148 @echo "$(ZLIB_MSG)" 149 @echo "$(LZMA_MSG)" 150 @echo "$(LZ4_MSG)" 151ifneq (,$(filter Windows%,$(OS))) 152 windres/generate_res.bat 153endif 154 $(CC) $(FLAGS) $^ $(RES_FILE) -o $@$(EXT) $(LDFLAGS) 155 156.PHONY: zstd-release 157zstd-release: DEBUGFLAGS := 158zstd-release: zstd 159 160zstd32 : CPPFLAGS += $(THREAD_CPP) 161zstd32 : LDFLAGS += $(THREAD_LD) 162zstd32 : CPPFLAGS += -DZSTD_LEGACY_SUPPORT=$(ZSTD_LEGACY_SUPPORT) 163zstd32 : $(ZSTDLIB_FILES) zstdcli.c fileio.c bench.c datagen.c dibio.c 164ifneq (,$(filter Windows%,$(OS))) 165 windres/generate_res.bat 166endif 167 $(CC) -m32 $(FLAGS) $^ $(RES32_FILE) -o $@$(EXT) 168 169zstd-nolegacy : $(ZSTD_FILES) $(ZDICT_FILES) zstdcli.o fileio.c bench.o datagen.o dibio.o 170 $(CC) $(FLAGS) $^ -o $@$(EXT) $(LDFLAGS) 171 172zstd-nomt : THREAD_CPP := 173zstd-nomt : THREAD_LD := 174zstd-nomt : THREAD_MSG := - multi-threading disabled 175zstd-nomt : zstd 176 177zstd-nogz : ZLIBCPP := 178zstd-nogz : ZLIBLD := 179zstd-nogz : ZLIB_MSG := - gzip support is disabled 180zstd-nogz : zstd 181 182zstd-noxz : LZMACPP := 183zstd-noxz : LZMALD := 184zstd-noxz : LZMA_MSG := - xz/lzma support is disabled 185zstd-noxz : zstd 186 187 188zstd-pgo : MOREFLAGS = -fprofile-generate 189zstd-pgo : clean zstd 190 ./zstd -b19i1 $(PROFILE_WITH) 191 ./zstd -b16i1 $(PROFILE_WITH) 192 ./zstd -b9i2 $(PROFILE_WITH) 193 ./zstd -b $(PROFILE_WITH) 194 ./zstd -b7i2 $(PROFILE_WITH) 195 ./zstd -b5 $(PROFILE_WITH) 196 $(RM) zstd 197 $(RM) $(ZSTDDECOMP_O) 198 $(MAKE) zstd MOREFLAGS=-fprofile-use 199 200# minimal target, with only zstd compression and decompression. no bench. no legacy. 201zstd-small: CFLAGS = -Os -s 202zstd-frugal zstd-small: $(ZSTD_FILES) zstdcli.c fileio.c 203 $(CC) $(FLAGS) -DZSTD_NOBENCH -DZSTD_NODICT $^ -o $@$(EXT) 204 205zstd-decompress: $(ZSTDCOMMON_FILES) $(ZSTDDECOMP_FILES) zstdcli.c fileio.c 206 $(CC) $(FLAGS) -DZSTD_NOBENCH -DZSTD_NODICT -DZSTD_NOCOMPRESS $^ -o $@$(EXT) 207 208zstd-compress: $(ZSTDCOMMON_FILES) $(ZSTDCOMP_FILES) zstdcli.c fileio.c 209 $(CC) $(FLAGS) -DZSTD_NOBENCH -DZSTD_NODICT -DZSTD_NODECOMPRESS $^ -o $@$(EXT) 210 211zstdmt: zstd 212 ln -sf zstd zstdmt 213 214.PHONY: generate_res 215generate_res: 216 windres/generate_res.bat 217 218.PHONY: clean 219clean: 220 $(MAKE) -C $(ZSTDDIR) clean 221 @$(RM) $(ZSTDDIR)/decompress/*.o $(ZSTDDIR)/decompress/zstd_decompress.gcda 222 @$(RM) core *.o tmp* result* *.gcda dictionary *.zst \ 223 zstd$(EXT) zstd32$(EXT) zstd-compress$(EXT) zstd-decompress$(EXT) \ 224 zstd-small$(EXT) zstd-frugal$(EXT) zstd-nolegacy$(EXT) zstd4$(EXT) \ 225 *.gcda default.profraw have_zlib$(EXT) 226 @echo Cleaning completed 227 228MD2ROFF = ronn 229MD2ROFF_FLAGS = --roff --warnings --manual="User Commands" --organization="zstd $(ZSTD_VERSION)" 230 231zstd.1: zstd.1.md ../lib/zstd.h 232 cat $< | $(MD2ROFF) $(MD2ROFF_FLAGS) | sed -n '/^\.\\\".*/!p' > $@ 233 234.PHONY: man 235man: zstd.1 236 237.PHONY: clean-man 238clean-man: 239 rm zstd.1 240 241.PHONY: preview-man 242preview-man: clean-man man 243 man ./zstd.1 244 245#----------------------------------------------------------------------------- 246# make install is validated only for Linux, OSX, BSD, Hurd and Solaris targets 247#----------------------------------------------------------------------------- 248ifneq (,$(filter $(shell uname),Linux Darwin GNU/kFreeBSD GNU OpenBSD FreeBSD NetBSD DragonFly SunOS)) 249 250.PHONY: list 251list: 252 @$(MAKE) -pRrq -f $(lastword $(MAKEFILE_LIST)) : 2>/dev/null | awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | sort | egrep -v -e '^[^[:alnum:]]' -e '^$@$$' | xargs 253 254DESTDIR ?= 255# directory variables : GNU conventions prefer lowercase 256# see https://www.gnu.org/prep/standards/html_node/Makefile-Conventions.html 257# support both lower and uppercase (BSD), use uppercase in script 258prefix ?= /usr/local 259PREFIX ?= $(prefix) 260exec_prefix ?= $(PREFIX) 261bindir ?= $(exec_prefix)/bin 262BINDIR ?= $(bindir) 263datarootdir ?= $(PREFIX)/share 264mandir ?= $(datarootdir)/man 265man1dir ?= $(mandir)/man1 266 267ifneq (,$(filter $(shell uname),OpenBSD FreeBSD NetBSD DragonFly SunOS)) 268MANDIR ?= $(PREFIX)/man 269MAN1DIR ?= $(MANDIR)/man1 270else 271MAN1DIR ?= $(man1dir) 272endif 273 274ifneq (,$(filter $(shell uname),SunOS)) 275INSTALL ?= ginstall 276else 277INSTALL ?= install 278endif 279 280INSTALL_PROGRAM ?= $(INSTALL) 281INSTALL_SCRIPT ?= $(INSTALL_PROGRAM) 282INSTALL_DATA ?= $(INSTALL) -m 644 283INSTALL_MAN ?= $(INSTALL_DATA) 284 285.PHONY: install 286install: zstd 287 @echo Installing binaries 288 @$(INSTALL) -d -m 755 $(DESTDIR)$(BINDIR)/ $(DESTDIR)$(MAN1DIR)/ 289 @$(INSTALL_PROGRAM) zstd $(DESTDIR)$(BINDIR)/zstd 290 @ln -sf zstd $(DESTDIR)$(BINDIR)/zstdcat 291 @ln -sf zstd $(DESTDIR)$(BINDIR)/unzstd 292 @ln -sf zstd $(DESTDIR)$(BINDIR)/zstdmt 293 @$(INSTALL_SCRIPT) zstdless $(DESTDIR)$(BINDIR)/zstdless 294 @$(INSTALL_SCRIPT) zstdgrep $(DESTDIR)$(BINDIR)/zstdgrep 295 @echo Installing man pages 296 @$(INSTALL_MAN) zstd.1 $(DESTDIR)$(MAN1DIR)/zstd.1 297 @ln -sf zstd.1 $(DESTDIR)$(MAN1DIR)/zstdcat.1 298 @ln -sf zstd.1 $(DESTDIR)$(MAN1DIR)/unzstd.1 299 @echo zstd installation completed 300 301.PHONY: uninstall 302uninstall: 303 @$(RM) $(DESTDIR)$(BINDIR)/zstdgrep 304 @$(RM) $(DESTDIR)$(BINDIR)/zstdless 305 @$(RM) $(DESTDIR)$(BINDIR)/zstdcat 306 @$(RM) $(DESTDIR)$(BINDIR)/unzstd 307 @$(RM) $(DESTDIR)$(BINDIR)/zstd 308 @$(RM) $(DESTDIR)$(MAN1DIR)/zstdcat.1 309 @$(RM) $(DESTDIR)$(MAN1DIR)/unzstd.1 310 @$(RM) $(DESTDIR)$(MAN1DIR)/zstd.1 311 @echo zstd programs successfully uninstalled 312 313endif 314