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