1# ################################################################
2# Copyright (c) 2015-2020, 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# You may select, at your option, one of the above-listed licenses.
9# ##########################################################################
10# zstd : Command Line Utility, supporting gzip-like arguments
11# zstd32 : Same as zstd, but forced to compile in 32-bits mode
12# zstd_nolegacy : zstd without support of decompression of legacy versions
13# zstd-small : minimal zstd without dictionary builder and benchmark
14# zstd-compress : compressor-only version of zstd
15# zstd-decompress : decompressor-only version of zstd
16# ##########################################################################
17
18.PHONY: default
19default: zstd-release
20
21# silent mode by default; verbose can be triggered by V=1 or VERBOSE=1
22$(V)$(VERBOSE).SILENT:
23
24
25ZSTDDIR := ../lib
26
27# Version numbers
28LIBVER_SRC := $(ZSTDDIR)/zstd.h
29LIBVER_MAJOR_SCRIPT:=`sed -n '/define ZSTD_VERSION_MAJOR/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < $(LIBVER_SRC)`
30LIBVER_MINOR_SCRIPT:=`sed -n '/define ZSTD_VERSION_MINOR/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < $(LIBVER_SRC)`
31LIBVER_PATCH_SCRIPT:=`sed -n '/define ZSTD_VERSION_RELEASE/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < $(LIBVER_SRC)`
32LIBVER_SCRIPT:= $(LIBVER_MAJOR_SCRIPT).$(LIBVER_MINOR_SCRIPT).$(LIBVER_PATCH_SCRIPT)
33LIBVER_MAJOR := $(shell echo $(LIBVER_MAJOR_SCRIPT))
34LIBVER_MINOR := $(shell echo $(LIBVER_MINOR_SCRIPT))
35LIBVER_PATCH := $(shell echo $(LIBVER_PATCH_SCRIPT))
36LIBVER  := $(shell echo $(LIBVER_SCRIPT))
37
38ZSTD_VERSION = $(LIBVER)
39
40HAVE_COLORNEVER = $(shell echo a | grep --color=never a > /dev/null 2> /dev/null && echo 1 || echo 0)
41GREP_OPTIONS ?=
42ifeq ($HAVE_COLORNEVER, 1)
43  GREP_OPTIONS += --color=never
44endif
45GREP = grep $(GREP_OPTIONS)
46
47ifeq ($(shell $(CC) -v 2>&1 | $(GREP) -c "gcc version "), 1)
48  ALIGN_LOOP = -falign-loops=32
49else
50  ALIGN_LOOP =
51endif
52
53DEBUGLEVEL ?= 0
54CPPFLAGS += -DXXH_NAMESPACE=ZSTD_ -DDEBUGLEVEL=$(DEBUGLEVEL)
55ifeq ($(OS),Windows_NT)   # MinGW assumed
56  CPPFLAGS += -D__USE_MINGW_ANSI_STDIO   # compatibility with %zu formatting
57endif
58CFLAGS   ?= -O3
59DEBUGFLAGS+=-Wall -Wextra -Wcast-qual -Wcast-align -Wshadow \
60            -Wstrict-aliasing=1 -Wswitch-enum -Wdeclaration-after-statement \
61            -Wstrict-prototypes -Wundef -Wpointer-arith \
62            -Wvla -Wformat=2 -Winit-self -Wfloat-equal -Wwrite-strings \
63            -Wredundant-decls -Wmissing-prototypes -Wc++-compat
64CFLAGS   += $(DEBUGFLAGS) $(MOREFLAGS)
65FLAGS     = $(CPPFLAGS) $(CFLAGS) $(LDFLAGS)
66
67ZSTDLIB_COMMON := $(ZSTDDIR)/common
68ZSTDLIB_COMPRESS := $(ZSTDDIR)/compress
69ZSTDLIB_DECOMPRESS := $(ZSTDDIR)/decompress
70ZDICT_DIR := $(ZSTDDIR)/dictBuilder
71ZSTDLEGACY_DIR := $(ZSTDDIR)/legacy
72
73vpath %.c $(ZSTDLIB_COMMON) $(ZSTDLIB_COMPRESS) $(ZSTDLIB_DECOMPRESS) $(ZDICT_DIR) $(ZSTDLEGACY_DIR)
74
75ZSTDLIB_COMMON_C := $(wildcard $(ZSTDLIB_COMMON)/*.c)
76ZSTDLIB_COMPRESS_C := $(wildcard $(ZSTDLIB_COMPRESS)/*.c)
77ZSTDLIB_DECOMPRESS_C := $(wildcard $(ZSTDLIB_DECOMPRESS)/*.c)
78ZSTDLIB_CORE_SRC := $(ZSTDLIB_DECOMPRESS_C) $(ZSTDLIB_COMMON_C) $(ZSTDLIB_COMPRESS_C)
79ZDICT_SRC := $(wildcard $(ZDICT_DIR)/*.c)
80
81ZSTD_LEGACY_SUPPORT ?= 5
82ZSTDLEGACY_SRC :=
83ifneq ($(ZSTD_LEGACY_SUPPORT), 0)
84ifeq ($(shell test $(ZSTD_LEGACY_SUPPORT) -lt 8; echo $$?), 0)
85  ZSTDLEGACY_SRC += $(shell ls $(ZSTDLEGACY_DIR)/*.c | $(GREP) 'v0[$(ZSTD_LEGACY_SUPPORT)-7]')
86endif
87endif
88
89# Sort files in alphabetical order for reproducible builds
90ZSTDLIB_FULL_SRC = $(sort $(ZSTDLIB_CORE_SRC) $(ZSTDLEGACY_SRC) $(ZDICT_SRC))
91ZSTDLIB_LOCAL_SRC := $(notdir $(ZSTDLIB_FULL_SRC))
92ZSTDLIB_LOCAL_OBJ := $(ZSTDLIB_LOCAL_SRC:.c=.o)
93
94ZSTD_CLI_SRC := $(wildcard *.c)
95ZSTD_CLI_OBJ := $(ZSTD_CLI_SRC:.c=.o)
96
97ZSTD_ALL_SRC := $(ZSTDLIB_LOCAL_SRC) $(ZSTD_CLI_SRC)
98ZSTD_ALL_OBJ := $(ZSTD_ALL_SRC:.c=.o)
99
100UNAME := $(shell uname)
101ifeq ($(UNAME), Darwin)
102  HASH ?= md5
103else ifeq ($(UNAME), FreeBSD)
104  HASH ?= gmd5sum
105else ifeq ($(UNAME), OpenBSD)
106  HASH ?= md5
107endif
108HASH ?= md5sum
109HAVE_HASH :=$(shell echo 1 | $(HASH) > /dev/null && echo 1 || echo 0)
110
111ifndef BUILD_DIR
112HASH_DIR = conf_$(shell echo $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) $(ZSTD_FILES) | $(HASH) | cut -f 1 -d " ")
113ifeq ($(HAVE_HASH),0)
114  $(info warning : could not find HASH ($(HASH)), needed to differentiate builds using different flags)
115  BUILD_DIR := obj/generic_noconf
116endif
117endif # BUILD_DIR
118
119# Define *.exe as extension for Windows systems
120ifneq (,$(filter Windows%,$(OS)))
121  EXT =.exe
122  RES64_FILE = windres/zstd64.res
123  RES32_FILE = windres/zstd32.res
124ifneq (,$(filter x86_64%,$(shell $(CC) -dumpmachine)))
125    RES_FILE = $(RES64_FILE)
126else
127    RES_FILE = $(RES32_FILE)
128endif
129else
130  EXT =
131endif
132
133VOID = /dev/null
134
135# Make 4.3 doesn't support '\#' anymore (https://lwn.net/Articles/810071/)
136NUM_SYMBOL := \#
137
138# thread detection
139NO_THREAD_MSG := ==> no threads, building without multithreading support
140HAVE_PTHREAD := $(shell printf '$(NUM_SYMBOL)include <pthread.h>\nint main(void) { return 0; }' > have_pthread.c && $(CC) $(FLAGS) -o have_pthread$(EXT) have_pthread.c -pthread 2> $(VOID) && rm have_pthread$(EXT) && echo 1 || echo 0; rm have_pthread.c)
141HAVE_THREAD := $(shell [ "$(HAVE_PTHREAD)" -eq "1" -o -n "$(filter Windows%,$(OS))" ] && echo 1 || echo 0)
142ifeq ($(HAVE_THREAD), 1)
143  THREAD_MSG := ==> building with threading support
144  THREAD_CPP := -DZSTD_MULTITHREAD
145  THREAD_LD := -pthread
146else
147  THREAD_MSG := $(NO_THREAD_MSG)
148endif
149
150# zlib detection
151NO_ZLIB_MSG := ==> no zlib, building zstd without .gz support
152HAVE_ZLIB := $(shell printf '$(NUM_SYMBOL)include <zlib.h>\nint main(void) { return 0; }' > have_zlib.c && $(CC) $(FLAGS) -o have_zlib$(EXT) have_zlib.c -lz 2> $(VOID) && rm have_zlib$(EXT) && echo 1 || echo 0; rm have_zlib.c)
153ifeq ($(HAVE_ZLIB), 1)
154  ZLIB_MSG := ==> building zstd with .gz compression support
155  ZLIBCPP = -DZSTD_GZCOMPRESS -DZSTD_GZDECOMPRESS
156  ZLIBLD = -lz
157else
158  ZLIB_MSG := $(NO_ZLIB_MSG)
159endif
160
161# lzma detection
162NO_LZMA_MSG := ==> no liblzma, building zstd without .xz/.lzma support
163HAVE_LZMA := $(shell printf '$(NUM_SYMBOL)include <lzma.h>\nint main(void) { return 0; }' > have_lzma.c && $(CC) $(FLAGS) -o have_lzma$(EXT) have_lzma.c -llzma 2> $(VOID) && rm have_lzma$(EXT) && echo 1 || echo 0; rm have_lzma.c)
164ifeq ($(HAVE_LZMA), 1)
165  LZMA_MSG := ==> building zstd with .xz/.lzma compression support
166  LZMACPP = -DZSTD_LZMACOMPRESS -DZSTD_LZMADECOMPRESS
167  LZMALD = -llzma
168else
169  LZMA_MSG := $(NO_LZMA_MSG)
170endif
171
172# lz4 detection
173NO_LZ4_MSG := ==> no liblz4, building zstd without .lz4 support
174HAVE_LZ4 := $(shell printf '$(NUM_SYMBOL)include <lz4frame.h>\n$(NUM_SYMBOL)include <lz4.h>\nint main(void) { return 0; }' > have_lz4.c && $(CC) $(FLAGS) -o have_lz4$(EXT) have_lz4.c -llz4 2> $(VOID) && rm have_lz4$(EXT) && echo 1 || echo 0; rm have_lz4.c)
175ifeq ($(HAVE_LZ4), 1)
176  LZ4_MSG := ==> building zstd with .lz4 compression support
177  LZ4CPP = -DZSTD_LZ4COMPRESS -DZSTD_LZ4DECOMPRESS
178  LZ4LD = -llz4
179else
180  LZ4_MSG := $(NO_LZ4_MSG)
181endif
182
183# explicit backtrace enable/disable for Linux & Darwin
184ifeq ($(BACKTRACE), 0)
185  DEBUGFLAGS += -DBACKTRACE_ENABLE=0
186endif
187ifeq (,$(filter Windows%, $(OS)))
188ifeq ($(BACKTRACE), 1)
189  DEBUGFLAGS += -DBACKTRACE_ENABLE=1
190  DEBUGFLAGS_LD += -rdynamic
191endif
192endif
193
194SET_CACHE_DIRECTORY = \
195	$(MAKE) --no-print-directory $@ \
196    BUILD_DIR=obj/$(HASH_DIR) \
197    CPPFLAGS="$(CPPFLAGS)" \
198    CFLAGS="$(CFLAGS)" \
199    LDFLAGS="$(LDFLAGS)"
200
201
202.PHONY: all
203all: zstd
204
205.PHONY: allVariants
206allVariants: zstd zstd-compress zstd-decompress zstd-small zstd-nolegacy zstd-dictBuilder
207
208.PHONY: zstd  # must always be run
209zstd : CPPFLAGS += $(THREAD_CPP) $(ZLIBCPP) $(LZMACPP) $(LZ4CPP)
210zstd : LDFLAGS += $(THREAD_LD) $(ZLIBLD) $(LZMALD) $(LZ4LD) $(DEBUGFLAGS_LD)
211zstd : CPPFLAGS += -DZSTD_LEGACY_SUPPORT=$(ZSTD_LEGACY_SUPPORT)
212ifneq (,$(filter Windows%,$(OS)))
213zstd : $(RES_FILE)
214endif
215
216ifndef BUILD_DIR
217# generate BUILD_DIR from flags
218
219zstd:
220	$(SET_CACHE_DIRECTORY)
221
222else
223# BUILD_DIR is defined
224
225ZSTD_OBJ := $(addprefix $(BUILD_DIR)/, $(ZSTD_ALL_OBJ))
226$(BUILD_DIR)/zstd : $(ZSTD_OBJ)
227	@echo "$(THREAD_MSG)"
228	@echo "$(ZLIB_MSG)"
229	@echo "$(LZMA_MSG)"
230	@echo "$(LZ4_MSG)"
231	@echo LINK $@
232	$(CC) $(FLAGS) $^ -o $@$(EXT) $(LDFLAGS)
233
234ifeq ($(HAVE_HASH),1)
235SRCBIN_HASH = $(shell cat $(BUILD_DIR)/zstd 2> $(VOID) | $(HASH) | cut -f 1 -d " ")
236DSTBIN_HASH = $(shell cat zstd 2> $(VOID) | $(HASH) | cut -f 1 -d " ")
237BIN_ISDIFFERENT = $(if $(filter $(SRCBIN_HASH),$(DSTBIN_HASH)),0,1)
238else
239BIN_ISDIFFERENT = 1
240endif
241
242zstd : $(BUILD_DIR)/zstd
243	if [ $(BIN_ISDIFFERENT) -eq 1 ]; then \
244		cp -f $< $@; \
245		echo zstd build completed; \
246	else \
247		echo zstd already built; \
248	fi
249
250endif  # BUILD_DIR
251
252
253.PHONY: zstd-release
254zstd-release: DEBUGFLAGS := -DBACKTRACE_ENABLE=0
255zstd-release: DEBUGFLAGS_LD :=
256zstd-release: zstd
257
258zstd32 : CPPFLAGS += $(THREAD_CPP)
259zstd32 : LDFLAGS  += $(THREAD_LD)
260zstd32 : CPPFLAGS += -DZSTD_LEGACY_SUPPORT=$(ZSTD_LEGACY_SUPPORT)
261ifneq (,$(filter Windows%,$(OS)))
262zstd32 : $(RES32_FILE)
263endif
264zstd32 : $(ZSTDLIB_FULL_SRC) $(ZSTD_CLI_SRC)
265	$(CC) -m32 $(FLAGS) $^ -o $@$(EXT)
266
267## zstd-nolegacy: same scope as zstd, with just support of legacy formats removed
268zstd-nolegacy : LDFLAGS += $(THREAD_LD) $(ZLIBLD) $(LZMALD) $(LZ4LD) $(DEBUGFLAGS_LD)
269zstd-nolegacy : $(ZSTDLIB_CORE_SRC) $(ZDICT_SRC) $(ZSTD_CLI_OBJ)
270	$(CC) $(FLAGS) $^ -o $@$(EXT) $(LDFLAGS)
271
272zstd-nomt : THREAD_CPP :=
273zstd-nomt : THREAD_LD  :=
274zstd-nomt : THREAD_MSG := - multi-threading disabled
275zstd-nomt : zstd
276
277zstd-nogz : ZLIBCPP :=
278zstd-nogz : ZLIBLD  :=
279zstd-nogz : ZLIB_MSG := - gzip support is disabled
280zstd-nogz : zstd
281
282zstd-noxz : LZMACPP :=
283zstd-noxz : LZMALD  :=
284zstd-noxz : LZMA_MSG := - xz/lzma support is disabled
285zstd-noxz : zstd
286
287## zstd-dll: zstd executable linked to dynamic library libzstd (must already exist)
288# note : the following target doesn't link
289#        because zstd uses non-public symbols from libzstd
290#        such as XXH64 (for benchmark),
291#        ZDICT_trainFromBuffer_unsafe_legacy (for dictionary builder)
292#        and ZSTD_cycleLog (likely for --patch-from).
293#        It's unclear at this stage if this is a scenario that must be supported
294.PHONY: zstd-dll
295zstd-dll : LDFLAGS+= -L$(ZSTDDIR) -lzstd
296zstd-dll : ZSTDLIB_FULL_SRC =
297zstd-dll : $(ZSTD_CLI_OBJ)
298	$(CC) $(FLAGS) $^ -o $@$(EXT) $(LDFLAGS)
299
300
301## zstd-pgo: zstd executable optimized with PGO.
302zstd-pgo :
303	$(MAKE) clean
304	$(MAKE) zstd MOREFLAGS=-fprofile-generate
305	./zstd -b19i1 $(PROFILE_WITH)
306	./zstd -b16i1 $(PROFILE_WITH)
307	./zstd -b9i2 $(PROFILE_WITH)
308	./zstd -b $(PROFILE_WITH)
309	./zstd -b7i2 $(PROFILE_WITH)
310	./zstd -b5 $(PROFILE_WITH)
311	$(RM) zstd *.o
312	case $(CC) in *clang*) if ! [ -e default.profdata ]; then llvm-profdata merge -output=default.profdata default*.profraw; fi ;; esac
313	$(MAKE) zstd MOREFLAGS=-fprofile-use
314
315## zstd-small: minimal target, supporting only zstd compression and decompression. no bench. no legacy. no other format.
316zstd-small: CFLAGS = -Os -s
317zstd-frugal zstd-small: $(ZSTDLIB_CORE_SRC) zstdcli.c util.c timefn.c fileio.c
318	$(CC) $(FLAGS) -DZSTD_NOBENCH -DZSTD_NODICT $^ -o $@$(EXT)
319
320zstd-decompress: $(ZSTDLIB_COMMON_C) $(ZSTDLIB_DECOMPRESS_C) zstdcli.c util.c timefn.c fileio.c
321	$(CC) $(FLAGS) -DZSTD_NOBENCH -DZSTD_NODICT -DZSTD_NOCOMPRESS $^ -o $@$(EXT)
322
323zstd-compress: $(ZSTDLIB_COMMON_C) $(ZSTDLIB_COMPRESS_C) zstdcli.c util.c timefn.c fileio.c
324	$(CC) $(FLAGS) -DZSTD_NOBENCH -DZSTD_NODICT -DZSTD_NODECOMPRESS $^ -o $@$(EXT)
325
326## zstd-dictBuilder: executable supporting dictionary creation and compression (only)
327zstd-dictBuilder: CPPFLAGS += -DZSTD_NOBENCH -DZSTD_NODECOMPRESS
328zstd-dictBuilder: $(ZSTDLIB_COMMON_C) $(ZSTDLIB_COMPRESS_C) $(ZDICT_SRC) zstdcli.c util.c timefn.c fileio.c dibio.c
329	$(CC) $(FLAGS) $^ -o $@$(EXT)
330
331zstdmt: zstd
332	ln -sf zstd zstdmt
333
334.PHONY: generate_res
335generate_res: $(RES64_FILE) $(RES32_FILE)
336
337ifneq (,$(filter Windows%,$(OS)))
338RC ?= windres
339# http://stackoverflow.com/questions/708238/how-do-i-add-an-icon-to-a-mingw-gcc-compiled-executable
340$(RES64_FILE): windres/zstd.rc
341	$(RC) -o $@ -I ../lib -I windres -i $< -O coff -F pe-x86-64
342$(RES32_FILE): windres/zstd.rc
343	$(RC) -o $@ -I ../lib -I windres -i $< -O coff -F pe-i386
344endif
345
346.PHONY: clean
347clean:
348	$(RM) core *.o tmp* result* *.gcda dictionary *.zst \
349        zstd$(EXT) zstd32$(EXT) zstd-compress$(EXT) zstd-decompress$(EXT) \
350        zstd-small$(EXT) zstd-frugal$(EXT) zstd-nolegacy$(EXT) zstd4$(EXT) \
351        zstd-dictBuilder$(EXT) *.gcda default*.profraw default.profdata have_zlib$(EXT)
352	$(RM) -r obj/*
353	@echo Cleaning completed
354
355MD2ROFF = ronn
356MD2ROFF_FLAGS = --roff --warnings --manual="User Commands" --organization="zstd $(ZSTD_VERSION)"
357
358zstd.1: zstd.1.md ../lib/zstd.h
359	cat $< | $(MD2ROFF) $(MD2ROFF_FLAGS) | sed -n '/^\.\\\".*/!p' > $@
360
361zstdgrep.1: zstdgrep.1.md ../lib/zstd.h
362	cat $< | $(MD2ROFF) $(MD2ROFF_FLAGS) | sed -n '/^\.\\\".*/!p' > $@
363
364zstdless.1: zstdless.1.md ../lib/zstd.h
365	cat $< | $(MD2ROFF) $(MD2ROFF_FLAGS) | sed -n '/^\.\\\".*/!p' > $@
366
367.PHONY: man
368man: zstd.1 zstdgrep.1 zstdless.1
369
370.PHONY: clean-man
371clean-man:
372	$(RM) zstd.1
373	$(RM) zstdgrep.1
374	$(RM) zstdless.1
375
376.PHONY: preview-man
377preview-man: clean-man man
378	man ./zstd.1
379	man ./zstdgrep.1
380	man ./zstdless.1
381
382
383# Generate .h dependencies automatically
384
385DEPFLAGS = -MT $@ -MMD -MP -MF
386
387$(BUILD_DIR)/%.o : %.c $(BUILD_DIR)/%.d | $(BUILD_DIR)
388	@echo CC $@
389	$(COMPILE.c) $(DEPFLAGS) $(BUILD_DIR)/$*.d $(OUTPUT_OPTION) $<
390
391MKDIR ?= mkdir
392$(BUILD_DIR): ; $(MKDIR) -p $@
393
394DEPFILES := $(ZSTD_OBJ:.o=.d)
395$(DEPFILES):
396
397include $(wildcard $(DEPFILES))
398
399
400
401#-----------------------------------------------------------------------------
402# make install is validated only for Linux, macOS, BSD, Hurd and Solaris targets
403#-----------------------------------------------------------------------------
404ifneq (,$(filter $(UNAME),Linux Darwin GNU/kFreeBSD GNU OpenBSD FreeBSD NetBSD DragonFly SunOS Haiku))
405
406HAVE_COLORNEVER = $(shell echo a | egrep --color=never a > /dev/null 2> /dev/null && echo 1 || echo 0)
407EGREP_OPTIONS ?=
408ifeq ($HAVE_COLORNEVER, 1)
409  EGREP_OPTIONS += --color=never
410endif
411EGREP = egrep $(EGREP_OPTIONS)
412AWK = awk
413
414# Print a two column output of targets and their description. To add a target description, put a
415# comment in the Makefile with the format "## <TARGET>: <DESCRIPTION>".  For example:
416#
417## list: Print all targets and their descriptions (if provided)
418.PHONY: list
419list:
420	TARGETS=$$($(MAKE) -pRrq -f $(lastword $(MAKEFILE_LIST)) : 2>/dev/null \
421		| $(AWK) -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' \
422		| $(EGREP) -v  -e '^[^[:alnum:]]' | sort); \
423	{ \
424	    printf "Target Name\tDescription\n"; \
425	    printf "%0.s-" {1..16}; printf "\t"; printf "%0.s-" {1..40}; printf "\n"; \
426	    for target in $$TARGETS; do \
427	        line=$$($(EGREP) "^##[[:space:]]+$$target:" $(lastword $(MAKEFILE_LIST))); \
428	        description=$$(echo $$line | $(AWK) '{i=index($$0,":"); print substr($$0,i+1)}' | xargs); \
429	        printf "$$target\t$$description\n"; \
430	    done \
431	} | column -t -s $$'\t'
432
433
434DESTDIR     ?=
435# directory variables : GNU conventions prefer lowercase
436# see https://www.gnu.org/prep/standards/html_node/Makefile-Conventions.html
437# support both lower and uppercase (BSD), use uppercase in script
438prefix      ?= /usr/local
439PREFIX      ?= $(prefix)
440exec_prefix ?= $(PREFIX)
441bindir      ?= $(exec_prefix)/bin
442BINDIR      ?= $(bindir)
443datarootdir ?= $(PREFIX)/share
444mandir      ?= $(datarootdir)/man
445man1dir     ?= $(mandir)/man1
446
447ifneq (,$(filter $(UNAME),OpenBSD FreeBSD NetBSD DragonFly SunOS))
448  MANDIR  ?= $(PREFIX)/man
449  MAN1DIR ?= $(MANDIR)/man1
450else
451  MAN1DIR ?= $(man1dir)
452endif
453
454ifneq (,$(filter $(UNAME),SunOS))
455  INSTALL ?= ginstall
456else
457  INSTALL ?= install
458endif
459
460INSTALL_PROGRAM ?= $(INSTALL)
461INSTALL_SCRIPT  ?= $(INSTALL_PROGRAM)
462INSTALL_DATA    ?= $(INSTALL) -m 644
463INSTALL_MAN     ?= $(INSTALL_DATA)
464
465.PHONY: install
466install:
467	# generate zstd only if not already present
468	[ -e zstd ] || $(MAKE) zstd-release
469	[ -e $(DESTDIR)$(BINDIR) ] || $(INSTALL) -d -m 755 $(DESTDIR)$(BINDIR)/
470	[ -e $(DESTDIR)$(MAN1DIR) ] || $(INSTALL) -d -m 755 $(DESTDIR)$(MAN1DIR)/
471	@echo Installing binaries
472	$(INSTALL_PROGRAM) zstd$(EXT) $(DESTDIR)$(BINDIR)/zstd$(EXT)
473	ln -sf zstd$(EXT) $(DESTDIR)$(BINDIR)/zstdcat$(EXT)
474	ln -sf zstd$(EXT) $(DESTDIR)$(BINDIR)/unzstd$(EXT)
475	ln -sf zstd$(EXT) $(DESTDIR)$(BINDIR)/zstdmt$(EXT)
476	$(INSTALL_SCRIPT) zstdless $(DESTDIR)$(BINDIR)/zstdless
477	$(INSTALL_SCRIPT) zstdgrep $(DESTDIR)$(BINDIR)/zstdgrep
478	@echo Installing man pages
479	$(INSTALL_MAN) zstd.1 $(DESTDIR)$(MAN1DIR)/zstd.1
480	ln -sf zstd.1 $(DESTDIR)$(MAN1DIR)/zstdcat.1
481	ln -sf zstd.1 $(DESTDIR)$(MAN1DIR)/unzstd.1
482	$(INSTALL_MAN) zstdgrep.1 $(DESTDIR)$(MAN1DIR)/zstdgrep.1
483	$(INSTALL_MAN) zstdless.1 $(DESTDIR)$(MAN1DIR)/zstdless.1
484	@echo zstd installation completed
485
486.PHONY: uninstall
487uninstall:
488	$(RM) $(DESTDIR)$(BINDIR)/zstdgrep
489	$(RM) $(DESTDIR)$(BINDIR)/zstdless
490	$(RM) $(DESTDIR)$(BINDIR)/zstdcat
491	$(RM) $(DESTDIR)$(BINDIR)/unzstd
492	$(RM) $(DESTDIR)$(BINDIR)/zstdmt
493	$(RM) $(DESTDIR)$(BINDIR)/zstd
494	$(RM) $(DESTDIR)$(MAN1DIR)/zstdless.1
495	$(RM) $(DESTDIR)$(MAN1DIR)/zstdgrep.1
496	$(RM) $(DESTDIR)$(MAN1DIR)/zstdcat.1
497	$(RM) $(DESTDIR)$(MAN1DIR)/unzstd.1
498	$(RM) $(DESTDIR)$(MAN1DIR)/zstd.1
499	@echo zstd programs successfully uninstalled
500
501endif
502