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