10b27ccbbapt# ################################################################
2361ebd6cem# Copyright (c) 2015-2020, Yann Collet, Facebook, Inc.
3c4fc73ebapt# All rights reserved.
4c4fc73ebapt#
50b27ccbbapt# This source code is licensed under both the BSD-style license (found in the
60b27ccbbapt# LICENSE file in the root directory of this source tree) and the GPLv2 (found
70b27ccbbapt# in the COPYING file in the root directory of this source tree).
8361ebd6cem# You may select, at your option, one of the above-listed licenses.
9c4fc73ebapt# ##########################################################################
10c4fc73ebapt# zstd : Command Line Utility, supporting gzip-like arguments
11c4fc73ebapt# zstd32 : Same as zstd, but forced to compile in 32-bits mode
12c4fc73ebapt# zstd_nolegacy : zstd without support of decompression of legacy versions
13c4fc73ebapt# zstd-small : minimal zstd without dictionary builder and benchmark
14c4fc73ebapt# zstd-compress : compressor-only version of zstd
15c4fc73ebapt# zstd-decompress : decompressor-only version of zstd
16c4fc73ebapt# ##########################################################################
17c4fc73ebapt
18c4fc73ebaptZSTDDIR = ../lib
19c4fc73ebapt
20a490b59bapt# Version numbers
21a490b59baptLIBVER_SRC := $(ZSTDDIR)/zstd.h
22a490b59baptLIBVER_MAJOR_SCRIPT:=`sed -n '/define ZSTD_VERSION_MAJOR/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < $(LIBVER_SRC)`
23a490b59baptLIBVER_MINOR_SCRIPT:=`sed -n '/define ZSTD_VERSION_MINOR/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < $(LIBVER_SRC)`
24a490b59baptLIBVER_PATCH_SCRIPT:=`sed -n '/define ZSTD_VERSION_RELEASE/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < $(LIBVER_SRC)`
25a490b59baptLIBVER_SCRIPT:= $(LIBVER_MAJOR_SCRIPT).$(LIBVER_MINOR_SCRIPT).$(LIBVER_PATCH_SCRIPT)
26a490b59baptLIBVER_MAJOR := $(shell echo $(LIBVER_MAJOR_SCRIPT))
27a490b59baptLIBVER_MINOR := $(shell echo $(LIBVER_MINOR_SCRIPT))
28a490b59baptLIBVER_PATCH := $(shell echo $(LIBVER_PATCH_SCRIPT))
29a490b59baptLIBVER  := $(shell echo $(LIBVER_SCRIPT))
30a490b59bapt
311955f02cemZSTD_VERSION = $(LIBVER)
32a490b59bapt
3370d64c5cemHAVE_COLORNEVER = $(shell echo a | grep --color=never a > /dev/null 2> /dev/null && echo 1 || echo 0)
3470d64c5cemGREP_OPTIONS ?=
3570d64c5cemifeq ($HAVE_COLORNEVER, 1)
3670d64c5cemGREP_OPTIONS += --color=never
3770d64c5cemendif
3870d64c5cemGREP = grep $(GREP_OPTIONS)
391955f02cem
401955f02cemifeq ($(shell $(CC) -v 2>&1 | $(GREP) -c "gcc version "), 1)
41c4fc73ebaptALIGN_LOOP = -falign-loops=32
42c4fc73ebaptelse
43c4fc73ebaptALIGN_LOOP =
44c4fc73ebaptendif
45c4fc73ebapt
46361ebd6cemCPPFLAGS+= -DXXH_NAMESPACE=ZSTD_
471955f02cemifeq ($(OS),Windows_NT)   # MinGW assumed
481955f02cemCPPFLAGS   += -D__USE_MINGW_ANSI_STDIO   # compatibility with %zu formatting
491955f02cemendif
50c4fc73ebaptCFLAGS  ?= -O3
510b27ccbbaptDEBUGFLAGS+=-Wall -Wextra -Wcast-qual -Wcast-align -Wshadow \
526947bdbbapt            -Wstrict-aliasing=1 -Wswitch-enum -Wdeclaration-after-statement \
535df09a9cem            -Wstrict-prototypes -Wundef -Wpointer-arith \
546947bdbbapt            -Wvla -Wformat=2 -Winit-self -Wfloat-equal -Wwrite-strings \
5570d64c5cem            -Wredundant-decls -Wmissing-prototypes -Wc++-compat
56c4fc73ebaptCFLAGS  += $(DEBUGFLAGS) $(MOREFLAGS)
57c4fc73ebaptFLAGS    = $(CPPFLAGS) $(CFLAGS) $(LDFLAGS)
58c4fc73ebapt
59c4fc73ebapt
60c4fc73ebaptZSTDCOMMON_FILES := $(ZSTDDIR)/common/*.c
61c4fc73ebaptZSTDCOMP_FILES := $(ZSTDDIR)/compress/*.c
62c4fc73ebaptZSTDDECOMP_FILES := $(ZSTDDIR)/decompress/*.c
63c4fc73ebaptZSTD_FILES := $(ZSTDDECOMP_FILES) $(ZSTDCOMMON_FILES) $(ZSTDCOMP_FILES)
64c4fc73ebaptZDICT_FILES := $(ZSTDDIR)/dictBuilder/*.c
65c4fc73ebaptZSTDDECOMP_O = $(ZSTDDIR)/decompress/zstd_decompress.o
66c4fc73ebapt
671955f02cemZSTD_LEGACY_SUPPORT ?= 5
686947bdbbaptZSTDLEGACY_FILES :=
69c4fc73ebaptifneq ($(ZSTD_LEGACY_SUPPORT), 0)
70c4fc73ebaptifeq ($(shell test $(ZSTD_LEGACY_SUPPORT) -lt 8; echo $$?), 0)
711955f02cem	ZSTDLEGACY_FILES += $(shell ls $(ZSTDDIR)/legacy/*.c | $(GREP) 'v0[$(ZSTD_LEGACY_SUPPORT)-7]')
72c4fc73ebaptendif
73c4fc73ebaptelse
74c4fc73ebaptendif
75c4fc73ebapt
761f85a10cem# Sort files in alphabetical order for reproducible builds
770b27ccbbaptZSTDLIB_FILES := $(sort $(wildcard $(ZSTD_FILES)) $(wildcard $(ZSTDLEGACY_FILES)) $(wildcard $(ZDICT_FILES)))
78c4fc73ebapt
79361ebd6cemZSTD_CLI_FILES := $(wildcard *.c)
80361ebd6cemZSTD_CLI_OBJ := $(patsubst %.c,%.o,$(ZSTD_CLI_FILES))
81361ebd6cem
82c4fc73ebapt# Define *.exe as extension for Windows systems
83c4fc73ebaptifneq (,$(filter Windows%,$(OS)))
84c4fc73ebaptEXT =.exe
85c4fc73ebaptRES64_FILE = windres/zstd64.res
86c4fc73ebaptRES32_FILE = windres/zstd32.res
87c4fc73ebaptifneq (,$(filter x86_64%,$(shell $(CC) -dumpmachine)))
88c4fc73ebapt    RES_FILE = $(RES64_FILE)
89c4fc73ebaptelse
90c4fc73ebapt    RES_FILE = $(RES32_FILE)
91c4fc73ebaptendif
92c4fc73ebaptelse
93c4fc73ebaptEXT =
94c4fc73ebaptendif
95c4fc73ebapt
96a490b59baptVOID = /dev/null
97a490b59bapt
98361ebd6cem# Make 4.3 doesn't support '\#' anymore (https://lwn.net/Articles/810071/)
99361ebd6cemNUM_SYMBOL := \#
100361ebd6cem
101a490b59bapt# thread detection
102a490b59baptNO_THREAD_MSG := ==> no threads, building without multithreading support
103361ebd6cemHAVE_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)
104a490b59baptHAVE_THREAD := $(shell [ "$(HAVE_PTHREAD)" -eq "1" -o -n "$(filter Windows%,$(OS))" ] && echo 1 || echo 0)
105a490b59baptifeq ($(HAVE_THREAD), 1)
106a490b59baptTHREAD_MSG := ==> building with threading support
107a490b59baptTHREAD_CPP := -DZSTD_MULTITHREAD
108a490b59baptTHREAD_LD := -pthread
109a490b59baptelse
110a490b59baptTHREAD_MSG := $(NO_THREAD_MSG)
111a490b59baptendif
112a490b59bapt
113c4fc73ebapt# zlib detection
114c4fc73ebaptNO_ZLIB_MSG := ==> no zlib, building zstd without .gz support
115361ebd6cemHAVE_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)
116c4fc73ebaptifeq ($(HAVE_ZLIB), 1)
117c4fc73ebaptZLIB_MSG := ==> building zstd with .gz compression support
118c4fc73ebaptZLIBCPP = -DZSTD_GZCOMPRESS -DZSTD_GZDECOMPRESS
119c4fc73ebaptZLIBLD = -lz
120c4fc73ebaptelse
121c4fc73ebaptZLIB_MSG := $(NO_ZLIB_MSG)
122c4fc73ebaptendif
123a490b59bapt
124c4fc73ebapt# lzma detection
125c4fc73ebaptNO_LZMA_MSG := ==> no liblzma, building zstd without .xz/.lzma support
126361ebd6cemHAVE_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)
127c4fc73ebaptifeq ($(HAVE_LZMA), 1)
128c4fc73ebaptLZMA_MSG := ==> building zstd with .xz/.lzma compression support
129c4fc73ebaptLZMACPP = -DZSTD_LZMACOMPRESS -DZSTD_LZMADECOMPRESS
130c4fc73ebaptLZMALD = -llzma
131c4fc73ebaptelse
132c4fc73ebaptLZMA_MSG := $(NO_LZMA_MSG)
133c4fc73ebaptendif
134c4fc73ebapt
135a490b59bapt# lz4 detection
136a490b59baptNO_LZ4_MSG := ==> no liblz4, building zstd without .lz4 support
137361ebd6cemHAVE_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)
138a490b59baptifeq ($(HAVE_LZ4), 1)
139a490b59baptLZ4_MSG := ==> building zstd with .lz4 compression support
140a490b59baptLZ4CPP = -DZSTD_LZ4COMPRESS -DZSTD_LZ4DECOMPRESS
141a490b59baptLZ4LD = -llz4
142a490b59baptelse
143a490b59baptLZ4_MSG := $(NO_LZ4_MSG)
144a490b59baptendif
145c4fc73ebapt
1461955f02cem# explicit backtrace enable/disable for Linux & Darwin
1471955f02cemifeq ($(BACKTRACE), 0)
1481955f02cemDEBUGFLAGS += -DBACKTRACE_ENABLE=0
1491955f02cemendif
1501955f02cemifeq (,$(filter Windows%, $(OS)))
1511955f02cemifeq ($(BACKTRACE), 1)
1521955f02cemDEBUGFLAGS += -DBACKTRACE_ENABLE=1
1531955f02cemDEBUGFLAGS_LD += -rdynamic
1541955f02cemendif
1551955f02cemendif
1561955f02cem
1571955f02cem
1585d1206cbapt.PHONY: default
159c4fc73ebaptdefault: zstd-release
160c4fc73ebapt
1615d1206cbapt.PHONY: all
162c4fc73ebaptall: zstd
163c4fc73ebapt
1645d1206cbapt.PHONY: allVariants
165361ebd6cemallVariants: zstd zstd-compress zstd-decompress zstd-small zstd-nolegacy zstd-dictBuilder
1665d1206cbapt
167c4fc73ebapt$(ZSTDDECOMP_O): CFLAGS += $(ALIGN_LOOP)
168c4fc73ebapt
1690b27ccbbaptzstd : CPPFLAGS += $(THREAD_CPP) $(ZLIBCPP) $(LZMACPP) $(LZ4CPP)
1701955f02cemzstd : LDFLAGS += $(THREAD_LD) $(ZLIBLD) $(LZMALD) $(LZ4LD) $(DEBUGFLAGS_LD)
1710b27ccbbaptzstd : CPPFLAGS += -DZSTD_LEGACY_SUPPORT=$(ZSTD_LEGACY_SUPPORT)
172361ebd6cemifneq (,$(filter Windows%,$(OS)))
173361ebd6cemzstd : $(RES_FILE)
174361ebd6cemendif
175361ebd6cemzstd : $(ZSTDLIB_FILES) $(ZSTD_CLI_OBJ)
176a490b59bapt	@echo "$(THREAD_MSG)"
177c4fc73ebapt	@echo "$(ZLIB_MSG)"
178c4fc73ebapt	@echo "$(LZMA_MSG)"
179a490b59bapt	@echo "$(LZ4_MSG)"
180361ebd6cem	$(CC) $(FLAGS) $^ -o $@$(EXT) $(LDFLAGS)
181c4fc73ebapt
1825d1206cbapt.PHONY: zstd-release
1831955f02cemzstd-release: DEBUGFLAGS := -DBACKTRACE_ENABLE=0
1841955f02cemzstd-release: DEBUGFLAGS_LD :=
185c4fc73ebaptzstd-release: zstd
186c4fc73ebapt
18759481ebcemzstd32 : CPPFLAGS += $(THREAD_CPP)
1881955f02cemzstd32 : LDFLAGS  += $(THREAD_LD)
189c4fc73ebaptzstd32 : CPPFLAGS += -DZSTD_LEGACY_SUPPORT=$(ZSTD_LEGACY_SUPPORT)
190c4fc73ebaptifneq (,$(filter Windows%,$(OS)))
191361ebd6cemzstd32 : $(RES32_FILE)
192c4fc73ebaptendif
193361ebd6cemzstd32 : $(ZSTDLIB_FILES) $(ZSTD_CLI_FILES)
194361ebd6cem	$(CC) -m32 $(FLAGS) $^ -o $@$(EXT)
195c4fc73ebapt
196361ebd6cem## zstd-nolegacy: same scope as zstd, with just support of legacy formats removed
197361ebd6cemzstd-nolegacy : LDFLAGS += $(THREAD_LD) $(ZLIBLD) $(LZMALD) $(LZ4LD) $(DEBUGFLAGS_LD)
198361ebd6cemzstd-nolegacy : $(ZSTD_FILES) $(ZDICT_FILES) $(ZSTD_CLI_OBJ)
1995d1206cbapt	$(CC) $(FLAGS) $^ -o $@$(EXT) $(LDFLAGS)
200c4fc73ebapt
201a490b59baptzstd-nomt : THREAD_CPP :=
2021955f02cemzstd-nomt : THREAD_LD  :=
203a490b59baptzstd-nomt : THREAD_MSG := - multi-threading disabled
204a490b59baptzstd-nomt : zstd
205a490b59bapt
206a490b59baptzstd-nogz : ZLIBCPP :=
2071955f02cemzstd-nogz : ZLIBLD  :=
208a490b59baptzstd-nogz : ZLIB_MSG := - gzip support is disabled
209a490b59baptzstd-nogz : zstd
210a490b59bapt
2116947bdbbaptzstd-noxz : LZMACPP :=
2121955f02cemzstd-noxz : LZMALD  :=
2136947bdbbaptzstd-noxz : LZMA_MSG := - xz/lzma support is disabled
2146947bdbbaptzstd-noxz : zstd
2156947bdbbapt
216361ebd6cem## zstd-dll: zstd executable linked to dynamic library libzstd (must already exist)
217361ebd6cem# note : the following target doesn't link
218361ebd6cem#        because zstd uses non-public symbols from libzstd
219361ebd6cem#        such as XXH64 (for benchmark),
220361ebd6cem#        ZDICT_trainFromBuffer_unsafe_legacy (for dictionary builder)
221361ebd6cem#        and ZSTD_cycleLog (likely for --patch-from).
222361ebd6cem#        It's unclear at this stage if this is a scenario that must be supported
223361ebd6cem.PHONY: zstd-dll
224361ebd6cemzstd-dll : LDFLAGS+= -L$(ZSTDDIR) -lzstd
225361ebd6cemzstd-dll : ZSTDLIB_FILES =
226361ebd6cemzstd-dll : $(ZSTD_CLI_OBJ)
227361ebd6cem	$(CC) $(FLAGS) $^ -o $@$(EXT) $(LDFLAGS)
228361ebd6cem
229a490b59bapt
230361ebd6cem## zstd-pgo: zstd executable optimized with pgo. `gcc` only.
23170d64c5cemzstd-pgo :
23270d64c5cem	$(MAKE) clean
23370d64c5cem	$(MAKE) zstd MOREFLAGS=-fprofile-generate
234c4fc73ebapt	./zstd -b19i1 $(PROFILE_WITH)
235c4fc73ebapt	./zstd -b16i1 $(PROFILE_WITH)
236c4fc73ebapt	./zstd -b9i2 $(PROFILE_WITH)
237c4fc73ebapt	./zstd -b $(PROFILE_WITH)
238c4fc73ebapt	./zstd -b7i2 $(PROFILE_WITH)
239c4fc73ebapt	./zstd -b5 $(PROFILE_WITH)
24070d64c5cem	$(RM) zstd *.o $(ZSTDDECOMP_O) $(ZSTDDIR)/compress/*.o
241361ebd6cem	case $(CC) in *clang*) if ! [ -e default.profdata ]; then llvm-profdata merge -output=default.profdata default*.profraw; fi ;; esac
242c4fc73ebapt	$(MAKE) zstd MOREFLAGS=-fprofile-use
243c4fc73ebapt
244361ebd6cem## zstd-small: minimal target, supporting only zstd compression and decompression. no bench. no legacy. no other format.
2455d1206cbaptzstd-small: CFLAGS = -Os -s
2465df09a9cemzstd-frugal zstd-small: $(ZSTD_FILES) zstdcli.c util.c timefn.c fileio.c
2475d1206cbapt	$(CC) $(FLAGS) -DZSTD_NOBENCH -DZSTD_NODICT $^ -o $@$(EXT)
248c4fc73ebapt
2495df09a9cemzstd-decompress: $(ZSTDCOMMON_FILES) $(ZSTDDECOMP_FILES) zstdcli.c util.c timefn.c fileio.c
250c4fc73ebapt	$(CC) $(FLAGS) -DZSTD_NOBENCH -DZSTD_NODICT -DZSTD_NOCOMPRESS $^ -o $@$(EXT)
251c4fc73ebapt
2525df09a9cemzstd-compress: $(ZSTDCOMMON_FILES) $(ZSTDCOMP_FILES) zstdcli.c util.c timefn.c fileio.c
253c4fc73ebapt	$(CC) $(FLAGS) -DZSTD_NOBENCH -DZSTD_NODICT -DZSTD_NODECOMPRESS $^ -o $@$(EXT)
254c4fc73ebapt
255361ebd6cem## zstd-dictBuilder: executable supporting dictionary creation and compression (only)
256361ebd6cemzstd-dictBuilder: CPPFLAGS += -DZSTD_NOBENCH -DZSTD_NODECOMPRESS
257361ebd6cemzstd-dictBuilder: $(ZSTDCOMMON_FILES) $(ZSTDCOMP_FILES) $(ZDICT_FILES) zstdcli.c util.c timefn.c fileio.c dibio.c
258361ebd6cem	$(CC) $(FLAGS) $^ -o $@$(EXT)
259361ebd6cem
260c4fc73ebaptzstdmt: zstd
2610b27ccbbapt	ln -sf zstd zstdmt
262c4fc73ebapt
2635d1206cbapt.PHONY: generate_res
264361ebd6cemgenerate_res: $(RES64_FILE) $(RES32_FILE)
265361ebd6cem
266361ebd6cemifneq (,$(filter Windows%,$(OS)))
267361ebd6cemRC ?= windres
268361ebd6cem# http://stackoverflow.com/questions/708238/how-do-i-add-an-icon-to-a-mingw-gcc-compiled-executable
269361ebd6cem$(RES64_FILE): windres/zstd.rc
270361ebd6cem	$(RC) -o $@ -I ../lib -I windres -i $< -O coff -F pe-x86-64
271361ebd6cem$(RES32_FILE): windres/zstd.rc
272361ebd6cem	$(RC) -o $@ -I ../lib -I windres -i $< -O coff -F pe-i386
273361ebd6cemendif
274c4fc73ebapt
2755d1206cbapt.PHONY: clean
276c4fc73ebaptclean:
277c4fc73ebapt	$(MAKE) -C $(ZSTDDIR) clean
278c4fc73ebapt	@$(RM) $(ZSTDDIR)/decompress/*.o $(ZSTDDIR)/decompress/zstd_decompress.gcda
279c4fc73ebapt	@$(RM) core *.o tmp* result* *.gcda dictionary *.zst \
280c4fc73ebapt        zstd$(EXT) zstd32$(EXT) zstd-compress$(EXT) zstd-decompress$(EXT) \
2815d1206cbapt        zstd-small$(EXT) zstd-frugal$(EXT) zstd-nolegacy$(EXT) zstd4$(EXT) \
282361ebd6cem        zstd-dictBuilder$(EXT) *.gcda default*.profraw default.profdata have_zlib$(EXT)
283c4fc73ebapt	@echo Cleaning completed
284c4fc73ebapt
285a490b59baptMD2ROFF = ronn
286a490b59baptMD2ROFF_FLAGS = --roff --warnings --manual="User Commands" --organization="zstd $(ZSTD_VERSION)"
287a490b59bapt
2881f85a10cemzstd.1: zstd.1.md ../lib/zstd.h
2891f85a10cem	cat $< | $(MD2ROFF) $(MD2ROFF_FLAGS) | sed -n '/^\.\\\".*/!p' > $@
290a490b59bapt
2911955f02cemzstdgrep.1: zstdgrep.1.md ../lib/zstd.h
2921955f02cem	cat $< | $(MD2ROFF) $(MD2ROFF_FLAGS) | sed -n '/^\.\\\".*/!p' > $@
2931955f02cem
2941955f02cemzstdless.1: zstdless.1.md ../lib/zstd.h
2951955f02cem	cat $< | $(MD2ROFF) $(MD2ROFF_FLAGS) | sed -n '/^\.\\\".*/!p' > $@
2961955f02cem
2975d1206cbapt.PHONY: man
2981955f02cemman: zstd.1 zstdgrep.1 zstdless.1
299a490b59bapt
3005d1206cbapt.PHONY: clean-man
301a490b59baptclean-man:
3025df09a9cem	$(RM) zstd.1
3035df09a9cem	$(RM) zstdgrep.1
3045df09a9cem	$(RM) zstdless.1
305a490b59bapt
3065d1206cbapt.PHONY: preview-man
307a490b59baptpreview-man: clean-man man
308a490b59bapt	man ./zstd.1
3091955f02cem	man ./zstdgrep.1
3101955f02cem	man ./zstdless.1
311c4fc73ebapt
312c4fc73ebapt#-----------------------------------------------------------------------------
3131955f02cem# make install is validated only for Linux, macOS, BSD, Hurd and Solaris targets
314c4fc73ebapt#-----------------------------------------------------------------------------
3151955f02cemifneq (,$(filter $(shell uname),Linux Darwin GNU/kFreeBSD GNU OpenBSD FreeBSD NetBSD DragonFly SunOS Haiku))
316c4fc73ebapt
31770d64c5cemHAVE_COLORNEVER = $(shell echo a | egrep --color=never a > /dev/null 2> /dev/null && echo 1 || echo 0)
31870d64c5cemEGREP_OPTIONS ?=
31970d64c5cemifeq ($HAVE_COLORNEVER, 1)
32070d64c5cemEGREP_OPTIONS += --color=never
32170d64c5cemendif
32270d64c5cemEGREP = egrep $(EGREP_OPTIONS)
323361ebd6cemAWK = awk
3241955f02cem
3251955f02cem# Print a two column output of targets and their description. To add a target description, put a
3261955f02cem# comment in the Makefile with the format "## <TARGET>: <DESCRIPTION>".  For example:
3271955f02cem#
3281955f02cem## list: Print all targets and their descriptions (if provided)
3295d1206cbapt.PHONY: list
3305d1206cbaptlist:
3311955f02cem	@TARGETS=$$($(MAKE) -pRrq -f $(lastword $(MAKEFILE_LIST)) : 2>/dev/null \
332361ebd6cem		| $(AWK) -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' \
3331955f02cem		| $(EGREP) -v  -e '^[^[:alnum:]]' | sort); \
3341955f02cem	{ \
3351955f02cem	    printf "Target Name\tDescription\n"; \
3361955f02cem	    printf "%0.s-" {1..16}; printf "\t"; printf "%0.s-" {1..40}; printf "\n"; \
3371955f02cem	    for target in $$TARGETS; do \
3381955f02cem	        line=$$($(EGREP) "^##[[:space:]]+$$target:" $(lastword $(MAKEFILE_LIST))); \
339361ebd6cem	        description=$$(echo $$line | $(AWK) '{i=index($$0,":"); print substr($$0,i+1)}' | xargs); \
3401955f02cem	        printf "$$target\t$$description\n"; \
3411955f02cem	    done \
3421955f02cem	} | column -t -s $$'\t'
3431955f02cem
3445d1206cbapt
3450b27ccbbaptDESTDIR     ?=
3460b27ccbbapt# directory variables : GNU conventions prefer lowercase
3470b27ccbbapt# see https://www.gnu.org/prep/standards/html_node/Makefile-Conventions.html
3480b27ccbbapt# support both lower and uppercase (BSD), use uppercase in script
3490b27ccbbaptprefix      ?= /usr/local
3500b27ccbbaptPREFIX      ?= $(prefix)
3510b27ccbbaptexec_prefix ?= $(PREFIX)
3520b27ccbbaptbindir      ?= $(exec_prefix)/bin
3530b27ccbbaptBINDIR      ?= $(bindir)
3540b27ccbbaptdatarootdir ?= $(PREFIX)/share
3550b27ccbbaptmandir      ?= $(datarootdir)/man
3560b27ccbbaptman1dir     ?= $(mandir)/man1
357c4fc73ebapt
358c4fc73ebaptifneq (,$(filter $(shell uname),OpenBSD FreeBSD NetBSD DragonFly SunOS))
3591f85a10cemMANDIR  ?= $(PREFIX)/man
3601f85a10cemMAN1DIR ?= $(MANDIR)/man1
361c4fc73ebaptelse
3621f85a10cemMAN1DIR ?= $(man1dir)
3630b27ccbbaptendif
3640b27ccbbapt
3650b27ccbbaptifneq (,$(filter $(shell uname),SunOS))
3660b27ccbbaptINSTALL ?= ginstall
3670b27ccbbaptelse
3680b27ccbbaptINSTALL ?= install
369c4fc73ebaptendif
370c4fc73ebapt
3710b27ccbbaptINSTALL_PROGRAM ?= $(INSTALL)
3720b27ccbbaptINSTALL_SCRIPT  ?= $(INSTALL_PROGRAM)
3730b27ccbbaptINSTALL_DATA    ?= $(INSTALL) -m 644
3740b27ccbbaptINSTALL_MAN     ?= $(INSTALL_DATA)
375c4fc73ebapt
3765d1206cbapt.PHONY: install
377c4fc73ebaptinstall: zstd
378c4fc73ebapt	@echo Installing binaries
3791f85a10cem	@$(INSTALL) -d -m 755 $(DESTDIR)$(BINDIR)/ $(DESTDIR)$(MAN1DIR)/
380361ebd6cem	@$(INSTALL_PROGRAM) zstd$(EXT) $(DESTDIR)$(BINDIR)/zstd$(EXT)
381361ebd6cem	@ln -sf zstd$(EXT) $(DESTDIR)$(BINDIR)/zstdcat$(EXT)
382361ebd6cem	@ln -sf zstd$(EXT) $(DESTDIR)$(BINDIR)/unzstd$(EXT)
383361ebd6cem	@ln -sf zstd$(EXT) $(DESTDIR)$(BINDIR)/zstdmt$(EXT)
384c4fc73ebapt	@$(INSTALL_SCRIPT) zstdless $(DESTDIR)$(BINDIR)/zstdless
385c4fc73ebapt	@$(INSTALL_SCRIPT) zstdgrep $(DESTDIR)$(BINDIR)/zstdgrep
386c4fc73ebapt	@echo Installing man pages
3871f85a10cem	@$(INSTALL_MAN) zstd.1 $(DESTDIR)$(MAN1DIR)/zstd.1
3881f85a10cem	@ln -sf zstd.1 $(DESTDIR)$(MAN1DIR)/zstdcat.1
3891f85a10cem	@ln -sf zstd.1 $(DESTDIR)$(MAN1DIR)/unzstd.1
3901955f02cem	@$(INSTALL_MAN) zstdgrep.1 $(DESTDIR)$(MAN1DIR)/zstdgrep.1
3911955f02cem	@$(INSTALL_MAN) zstdless.1 $(DESTDIR)$(MAN1DIR)/zstdless.1
392c4fc73ebapt	@echo zstd installation completed
393c4fc73ebapt
3945d1206cbapt.PHONY: uninstall
395c4fc73ebaptuninstall:
396c4fc73ebapt	@$(RM) $(DESTDIR)$(BINDIR)/zstdgrep
397c4fc73ebapt	@$(RM) $(DESTDIR)$(BINDIR)/zstdless
398c4fc73ebapt	@$(RM) $(DESTDIR)$(BINDIR)/zstdcat
399c4fc73ebapt	@$(RM) $(DESTDIR)$(BINDIR)/unzstd
40070d64c5cem	@$(RM) $(DESTDIR)$(BINDIR)/zstdmt
401c4fc73ebapt	@$(RM) $(DESTDIR)$(BINDIR)/zstd
4021955f02cem	@$(RM) $(DESTDIR)$(MAN1DIR)/zstdless.1
4031955f02cem	@$(RM) $(DESTDIR)$(MAN1DIR)/zstdgrep.1
4041f85a10cem	@$(RM) $(DESTDIR)$(MAN1DIR)/zstdcat.1
4051f85a10cem	@$(RM) $(DESTDIR)$(MAN1DIR)/unzstd.1
4061f85a10cem	@$(RM) $(DESTDIR)$(MAN1DIR)/zstd.1
407c4fc73ebapt	@echo zstd programs successfully uninstalled
4085d1206cbapt
409c4fc73ebaptendif
410