xref: /freebsd/sys/contrib/zstd/Makefile (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
1# ################################################################
2# Copyright (c) 2015-2021, 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
11# verbose mode (print commands) on V=1 or VERBOSE=1
12Q = $(if $(filter 1,$(V) $(VERBOSE)),,@)
13
14PRGDIR   = programs
15ZSTDDIR  = lib
16BUILDIR  = build
17ZWRAPDIR = zlibWrapper
18TESTDIR  = tests
19FUZZDIR  = $(TESTDIR)/fuzz
20
21# Define nul output
22VOID = /dev/null
23
24# When cross-compiling from linux to windows, you might
25# need to specify this as "Windows." Fedora build fails
26# without it.
27#
28# Note: mingw-w64 build from linux to windows does not
29# fail on other tested distros (ubuntu, debian) even
30# without manually specifying the TARGET_SYSTEM.
31TARGET_SYSTEM ?= $(OS)
32CP ?= cp
33
34ifneq (,$(filter Windows%,$(TARGET_SYSTEM)))
35  EXT =.exe
36else
37  EXT =
38endif
39
40## default: Build lib-release and zstd-release
41.PHONY: default
42default: lib-release zstd-release
43
44.PHONY: all
45all: allmost examples manual contrib
46
47.PHONY: allmost
48allmost: allzstd zlibwrapper
49
50# skip zwrapper, can't build that on alternate architectures without the proper zlib installed
51.PHONY: allzstd
52allzstd: lib
53	$(Q)$(MAKE) -C $(PRGDIR) all
54	$(Q)$(MAKE) -C $(TESTDIR) all
55
56.PHONY: all32
57all32:
58	$(MAKE) -C $(PRGDIR) zstd32
59	$(MAKE) -C $(TESTDIR) all32
60
61.PHONY: lib lib-release lib-mt lib-nomt
62lib lib-release lib-mt lib-nomt:
63	$(Q)$(MAKE) -C $(ZSTDDIR) $@
64
65.PHONY: zstd zstd-release
66zstd zstd-release:
67	$(Q)$(MAKE) -C $(PRGDIR) $@
68	$(Q)ln -sf $(PRGDIR)/zstd$(EXT) zstd$(EXT)
69
70.PHONY: zstdmt
71zstdmt:
72	$(Q)$(MAKE) -C $(PRGDIR) $@
73	$(Q)$(CP) $(PRGDIR)/zstd$(EXT) ./zstdmt$(EXT)
74
75.PHONY: zlibwrapper
76zlibwrapper: lib
77	$(MAKE) -C $(ZWRAPDIR) all
78
79## test: run long-duration tests
80.PHONY: test
81DEBUGLEVEL ?= 1
82test: MOREFLAGS += -g -Werror
83test:
84	DEBUGLEVEL=$(DEBUGLEVEL) MOREFLAGS="$(MOREFLAGS)" $(MAKE) -j -C $(PRGDIR) allVariants
85	$(MAKE) -C $(TESTDIR) $@
86	ZSTD=../../programs/zstd $(MAKE) -C doc/educational_decoder $@
87
88## shortest: same as `make check`
89.PHONY: shortest
90shortest:
91	$(Q)$(MAKE) -C $(TESTDIR) $@
92
93## check: run basic tests for `zstd` cli
94.PHONY: check
95check: shortest
96
97.PHONY: automated_benchmarking
98automated_benchmarking:
99	$(MAKE) -C $(TESTDIR) $@
100
101.PHONY: benchmarking
102benchmarking: automated_benchmarking
103
104## examples: build all examples in `examples/` directory
105.PHONY: examples
106examples: lib
107	$(MAKE) -C examples all
108
109## manual: generate API documentation in html format
110.PHONY: manual
111manual:
112	$(MAKE) -C contrib/gen_html $@
113
114## man: generate man page
115.PHONY: man
116man:
117	$(MAKE) -C programs $@
118
119## contrib: build all supported projects in `/contrib` directory
120.PHONY: contrib
121contrib: lib
122	$(MAKE) -C contrib/pzstd all
123	$(MAKE) -C contrib/seekable_format/examples all
124	$(MAKE) -C contrib/seekable_format/tests test
125	$(MAKE) -C contrib/largeNbDicts all
126	cd build/single_file_libs/ ; ./build_decoder_test.sh
127	cd build/single_file_libs/ ; ./build_library_test.sh
128
129.PHONY: cleanTabs
130cleanTabs:
131	cd contrib; ./cleanTabs
132
133.PHONY: clean
134clean:
135	$(Q)$(MAKE) -C $(ZSTDDIR) $@ > $(VOID)
136	$(Q)$(MAKE) -C $(PRGDIR) $@ > $(VOID)
137	$(Q)$(MAKE) -C $(TESTDIR) $@ > $(VOID)
138	$(Q)$(MAKE) -C $(ZWRAPDIR) $@ > $(VOID)
139	$(Q)$(MAKE) -C examples/ $@ > $(VOID)
140	$(Q)$(MAKE) -C contrib/gen_html $@ > $(VOID)
141	$(Q)$(MAKE) -C contrib/pzstd $@ > $(VOID)
142	$(Q)$(MAKE) -C contrib/seekable_format/examples $@ > $(VOID)
143	$(Q)$(MAKE) -C contrib/seekable_format/tests $@ > $(VOID)
144	$(Q)$(MAKE) -C contrib/largeNbDicts $@ > $(VOID)
145	$(Q)$(RM) zstd$(EXT) zstdmt$(EXT) tmp*
146	$(Q)$(RM) -r lz4
147	@echo Cleaning completed
148
149#------------------------------------------------------------------------------
150# make install is validated only for Linux, macOS, Hurd and some BSD targets
151#------------------------------------------------------------------------------
152ifneq (,$(filter $(shell uname),Linux Darwin GNU/kFreeBSD GNU OpenBSD FreeBSD DragonFly NetBSD MSYS_NT Haiku AIX))
153
154HOST_OS = POSIX
155
156MKDIR ?= mkdir -p
157
158HAVE_COLORNEVER = $(shell echo a | egrep --color=never a > /dev/null 2> /dev/null && echo 1 || echo 0)
159EGREP_OPTIONS ?=
160ifeq ($HAVE_COLORNEVER, 1)
161EGREP_OPTIONS += --color=never
162endif
163EGREP = egrep $(EGREP_OPTIONS)
164
165# Print a two column output of targets and their description. To add a target description, put a
166# comment in the Makefile with the format "## <TARGET>: <DESCRIPTION>".  For example:
167#
168## list: Print all targets and their descriptions (if provided)
169.PHONY: list
170list:
171	$(Q)TARGETS=$$($(MAKE) -pRrq -f $(lastword $(MAKEFILE_LIST)) : 2>/dev/null \
172		| awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' \
173		| $(EGREP) -v  -e '^[^[:alnum:]]' | sort); \
174	{ \
175	    printf "Target Name\tDescription\n"; \
176	    printf "%0.s-" {1..16}; printf "\t"; printf "%0.s-" {1..40}; printf "\n"; \
177	    for target in $$TARGETS; do \
178	        line=$$($(EGREP) "^##[[:space:]]+$$target:" $(lastword $(MAKEFILE_LIST))); \
179	        description=$$(echo $$line | awk '{i=index($$0,":"); print substr($$0,i+1)}' | xargs); \
180	        printf "$$target\t$$description\n"; \
181	    done \
182	} | column -t -s $$'\t'
183
184.PHONY: install armtest usan asan uasan msan asan32
185install:
186	$(Q)$(MAKE) -C $(ZSTDDIR) $@
187	$(Q)$(MAKE) -C $(PRGDIR) $@
188
189.PHONY: uninstall
190uninstall:
191	$(Q)$(MAKE) -C $(ZSTDDIR) $@
192	$(Q)$(MAKE) -C $(PRGDIR) $@
193
194.PHONY: travis-install
195travis-install:
196	$(MAKE) install PREFIX=~/install_test_dir
197
198.PHONY: gcc5build gcc6build gcc7build clangbuild m32build armbuild aarch64build ppcbuild ppc64build
199gcc5build: clean
200	gcc-5 -v
201	CC=gcc-5 $(MAKE) all MOREFLAGS="-Werror"
202
203gcc6build: clean
204	gcc-6 -v
205	CC=gcc-6 $(MAKE) all MOREFLAGS="-Werror"
206
207gcc7build: clean
208	gcc-7 -v
209	CC=gcc-7 $(MAKE) all MOREFLAGS="-Werror"
210
211clangbuild: clean
212	clang -v
213	CXX=clang++ CC=clang CFLAGS="-Werror -Wconversion -Wno-sign-conversion -Wdocumentation" $(MAKE) all
214
215m32build: clean
216	gcc -v
217	$(MAKE) all32
218
219armbuild: clean
220	CC=arm-linux-gnueabi-gcc CFLAGS="-Werror" $(MAKE) allzstd
221
222aarch64build: clean
223	CC=aarch64-linux-gnu-gcc CFLAGS="-Werror -O0" $(MAKE) allzstd
224
225ppcbuild: clean
226	CC=powerpc-linux-gnu-gcc CFLAGS="-m32 -Wno-attributes -Werror" $(MAKE) -j allzstd
227
228ppc64build: clean
229	CC=powerpc-linux-gnu-gcc CFLAGS="-m64 -Werror" $(MAKE) -j allzstd
230
231.PHONY: armfuzz aarch64fuzz ppcfuzz ppc64fuzz
232armfuzz: clean
233	CC=arm-linux-gnueabi-gcc QEMU_SYS=qemu-arm-static MOREFLAGS="-static" FUZZER_FLAGS=--no-big-tests $(MAKE) -C $(TESTDIR) fuzztest
234
235aarch64fuzz: clean
236	ld -v
237	CC=aarch64-linux-gnu-gcc QEMU_SYS=qemu-aarch64-static MOREFLAGS="-static" FUZZER_FLAGS=--no-big-tests $(MAKE) -C $(TESTDIR) fuzztest
238
239ppcfuzz: clean
240	CC=powerpc-linux-gnu-gcc QEMU_SYS=qemu-ppc-static MOREFLAGS="-static" FUZZER_FLAGS=--no-big-tests $(MAKE) -C $(TESTDIR) fuzztest
241
242ppc64fuzz: clean
243	CC=powerpc-linux-gnu-gcc QEMU_SYS=qemu-ppc64-static MOREFLAGS="-m64 -static" FUZZER_FLAGS=--no-big-tests $(MAKE) -C $(TESTDIR) fuzztest
244
245.PHONY: cxxtest gcc5test gcc6test armtest aarch64test ppctest ppc64test
246cxxtest: CXXFLAGS += -Wall -Wextra -Wundef -Wshadow -Wcast-align -Werror
247cxxtest: clean
248	$(MAKE) -C $(PRGDIR) all CC="$(CXX) -Wno-deprecated" CFLAGS="$(CXXFLAGS)"   # adding -Wno-deprecated to avoid clang++ warning on dealing with C files directly
249
250gcc5test: clean
251	gcc-5 -v
252	$(MAKE) all CC=gcc-5 MOREFLAGS="-Werror"
253
254gcc6test: clean
255	gcc-6 -v
256	$(MAKE) all CC=gcc-6 MOREFLAGS="-Werror"
257
258armtest: clean
259	$(MAKE) -C $(TESTDIR) datagen   # use native, faster
260	$(MAKE) -C $(TESTDIR) test CC=arm-linux-gnueabi-gcc QEMU_SYS=qemu-arm-static ZSTDRTTEST= MOREFLAGS="-Werror -static" FUZZER_FLAGS=--no-big-tests
261
262aarch64test:
263	$(MAKE) -C $(TESTDIR) datagen   # use native, faster
264	$(MAKE) -C $(TESTDIR) test CC=aarch64-linux-gnu-gcc QEMU_SYS=qemu-aarch64-static ZSTDRTTEST= MOREFLAGS="-Werror -static" FUZZER_FLAGS=--no-big-tests
265
266ppctest: clean
267	$(MAKE) -C $(TESTDIR) datagen   # use native, faster
268	$(MAKE) -C $(TESTDIR) test CC=powerpc-linux-gnu-gcc QEMU_SYS=qemu-ppc-static ZSTDRTTEST= MOREFLAGS="-Werror -Wno-attributes -static" FUZZER_FLAGS=--no-big-tests
269
270ppc64test: clean
271	$(MAKE) -C $(TESTDIR) datagen   # use native, faster
272	$(MAKE) -C $(TESTDIR) test CC=powerpc-linux-gnu-gcc QEMU_SYS=qemu-ppc64-static ZSTDRTTEST= MOREFLAGS="-m64 -static" FUZZER_FLAGS=--no-big-tests
273
274.PHONY: arm-ppc-compilation
275arm-ppc-compilation:
276	$(MAKE) -C $(PRGDIR) clean zstd CC=arm-linux-gnueabi-gcc QEMU_SYS=qemu-arm-static ZSTDRTTEST= MOREFLAGS="-Werror -static"
277	$(MAKE) -C $(PRGDIR) clean zstd CC=aarch64-linux-gnu-gcc QEMU_SYS=qemu-aarch64-static ZSTDRTTEST= MOREFLAGS="-Werror -static"
278	$(MAKE) -C $(PRGDIR) clean zstd CC=powerpc-linux-gnu-gcc QEMU_SYS=qemu-ppc-static ZSTDRTTEST= MOREFLAGS="-Werror -Wno-attributes -static"
279	$(MAKE) -C $(PRGDIR) clean zstd CC=powerpc-linux-gnu-gcc QEMU_SYS=qemu-ppc64-static ZSTDRTTEST= MOREFLAGS="-m64 -static"
280
281regressiontest:
282	$(MAKE) -C $(FUZZDIR) regressiontest
283
284uasanregressiontest:
285	$(MAKE) -C $(FUZZDIR) regressiontest CC=clang CXX=clang++ CFLAGS="-O3 -fsanitize=address,undefined" CXXFLAGS="-O3 -fsanitize=address,undefined"
286
287msanregressiontest:
288	$(MAKE) -C $(FUZZDIR) regressiontest CC=clang CXX=clang++ CFLAGS="-O3 -fsanitize=memory" CXXFLAGS="-O3 -fsanitize=memory"
289
290update_regressionResults : REGRESS_RESULTS_DIR := /tmp/regress_results_dir/
291update_regressionResults:
292	$(MAKE) -C programs zstd
293	$(MAKE) -C tests/regression test
294	$(RM) -rf $(REGRESS_RESULTS_DIR)
295	$(MKDIR) $(REGRESS_RESULTS_DIR)
296	./tests/regression/test                         \
297        --cache  tests/regression/cache             \
298        --output $(REGRESS_RESULTS_DIR)/results.csv \
299        --zstd   programs/zstd
300	echo "Showing results differences"
301	! diff tests/regression/results.csv $(REGRESS_RESULTS_DIR)/results.csv
302	echo "Updating results.csv"
303	$(CP) $(REGRESS_RESULTS_DIR)/results.csv tests/regression/results.csv
304
305
306# run UBsan with -fsanitize-recover=pointer-overflow
307# this only works with recent compilers such as gcc 8+
308usan: clean
309	$(MAKE) test CC=clang MOREFLAGS="-g -fno-sanitize-recover=all -fsanitize-recover=pointer-overflow -fsanitize=undefined -Werror"
310
311asan: clean
312	$(MAKE) test CC=clang MOREFLAGS="-g -fsanitize=address -Werror"
313
314asan-%: clean
315	LDFLAGS=-fuse-ld=gold MOREFLAGS="-g -fno-sanitize-recover=all -fsanitize=address -Werror" $(MAKE) -C $(TESTDIR) $*
316
317msan: clean
318	$(MAKE) test CC=clang MOREFLAGS="-g -fsanitize=memory -fno-omit-frame-pointer -Werror" HAVE_LZMA=0   # datagen.c fails this test for no obvious reason
319
320msan-%: clean
321	LDFLAGS=-fuse-ld=gold MOREFLAGS="-g -fno-sanitize-recover=all -fsanitize=memory -fno-omit-frame-pointer -Werror" FUZZER_FLAGS=--no-big-tests $(MAKE) -C $(TESTDIR) HAVE_LZMA=0 $*
322
323asan32: clean
324	$(MAKE) -C $(TESTDIR) test32 CC=clang MOREFLAGS="-g -fsanitize=address"
325
326uasan: clean
327	$(MAKE) test CC=clang MOREFLAGS="-g -fno-sanitize-recover=all -fsanitize-recover=pointer-overflow -fsanitize=address,undefined -Werror"
328
329uasan-%: clean
330	LDFLAGS=-fuse-ld=gold MOREFLAGS="-g -fno-sanitize-recover=all -fsanitize-recover=pointer-overflow -fsanitize=address,undefined -Werror" $(MAKE) -C $(TESTDIR) $*
331
332tsan-%: clean
333	LDFLAGS=-fuse-ld=gold MOREFLAGS="-g -fno-sanitize-recover=all -fsanitize=thread -Werror" $(MAKE) -C $(TESTDIR) $* FUZZER_FLAGS=--no-big-tests
334
335.PHONY: apt-install
336apt-install:
337	sudo apt-get -yq --no-install-suggests --no-install-recommends --force-yes install $(APT_PACKAGES)
338
339.PHONY: apt-add-repo
340apt-add-repo:
341	sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
342	sudo apt-get update -y -qq
343
344.PHONY: ppcinstall arminstall valgrindinstall libc6install gcc6install gcc7install gcc8install gpp6install clang38install lz4install
345ppcinstall:
346	APT_PACKAGES="qemu-system-ppc qemu-user-static gcc-powerpc-linux-gnu" $(MAKE) apt-install
347
348arminstall:
349	APT_PACKAGES="qemu-system-arm qemu-user-static gcc-arm-linux-gnueabi libc6-dev-armel-cross gcc-aarch64-linux-gnu libc6-dev-arm64-cross" $(MAKE) apt-install
350
351valgrindinstall:
352	APT_PACKAGES="valgrind" $(MAKE) apt-install
353
354libc6install:
355	APT_PACKAGES="libc6-dev-i386 gcc-multilib" $(MAKE) apt-install
356
357gcc6install: apt-add-repo
358	APT_PACKAGES="libc6-dev-i386 gcc-multilib gcc-6 gcc-6-multilib" $(MAKE) apt-install
359
360gcc7install: apt-add-repo
361	APT_PACKAGES="libc6-dev-i386 gcc-multilib gcc-7 gcc-7-multilib" $(MAKE) apt-install
362
363gcc8install: apt-add-repo
364	APT_PACKAGES="libc6-dev-i386 gcc-multilib gcc-8 gcc-8-multilib" $(MAKE) apt-install
365
366gpp6install: apt-add-repo
367	APT_PACKAGES="libc6-dev-i386 g++-multilib gcc-6 g++-6 g++-6-multilib" $(MAKE) apt-install
368
369clang38install:
370	APT_PACKAGES="clang-3.8" $(MAKE) apt-install
371
372# Ubuntu 14.04 ships a too-old lz4
373lz4install:
374	[ -e lz4 ] || git clone https://github.com/lz4/lz4 && sudo $(MAKE) -C lz4 install
375
376endif
377
378
379CMAKE_PARAMS = -DZSTD_BUILD_CONTRIB:BOOL=ON -DZSTD_BUILD_STATIC:BOOL=ON -DZSTD_BUILD_TESTS:BOOL=ON -DZSTD_ZLIB_SUPPORT:BOOL=ON -DZSTD_LZMA_SUPPORT:BOOL=ON -DCMAKE_BUILD_TYPE=Release
380
381ifneq (,$(filter MSYS%,$(shell uname)))
382HOST_OS = MSYS
383CMAKE_PARAMS = -G"MSYS Makefiles" -DCMAKE_BUILD_TYPE=Debug -DZSTD_MULTITHREAD_SUPPORT:BOOL=OFF -DZSTD_BUILD_STATIC:BOOL=ON -DZSTD_BUILD_TESTS:BOOL=ON
384endif
385
386#------------------------------------------------------------------------
387# target specific tests
388#------------------------------------------------------------------------
389ifneq (,$(filter $(HOST_OS),MSYS POSIX))
390.PHONY: cmakebuild c89build gnu90build c99build gnu99build c11build bmix64build bmix32build bmi32build staticAnalyze
391cmakebuild:
392	cmake --version
393	$(RM) -r $(BUILDIR)/cmake/build
394	$(MKDIR) $(BUILDIR)/cmake/build
395	cd $(BUILDIR)/cmake/build; cmake -DCMAKE_INSTALL_PREFIX:PATH=~/install_test_dir $(CMAKE_PARAMS) ..
396	$(MAKE) -C $(BUILDIR)/cmake/build -j4;
397	$(MAKE) -C $(BUILDIR)/cmake/build install;
398	$(MAKE) -C $(BUILDIR)/cmake/build uninstall;
399	cd $(BUILDIR)/cmake/build; ctest -V -L Medium
400
401c89build: clean
402	$(CC) -v
403	CFLAGS="-std=c89 -Werror -O0" $(MAKE) allmost  # will fail, due to missing support for `long long`
404
405gnu90build: clean
406	$(CC) -v
407	CFLAGS="-std=gnu90 -Werror -O0" $(MAKE) allmost
408
409c99build: clean
410	$(CC) -v
411	CFLAGS="-std=c99 -Werror -O0" $(MAKE) allmost
412
413gnu99build: clean
414	$(CC) -v
415	CFLAGS="-std=gnu99 -Werror -O0" $(MAKE) allmost
416
417c11build: clean
418	$(CC) -v
419	CFLAGS="-std=c11 -Werror -O0" $(MAKE) allmost
420
421bmix64build: clean
422	$(CC) -v
423	CFLAGS="-O3 -mbmi -Werror" $(MAKE) -C $(TESTDIR) test
424
425bmix32build: clean
426	$(CC) -v
427	CFLAGS="-O3 -mbmi -mx32 -Werror" $(MAKE) -C $(TESTDIR) test
428
429bmi32build: clean
430	$(CC) -v
431	CFLAGS="-O3 -mbmi -m32 -Werror" $(MAKE) -C $(TESTDIR) test
432
433# static analyzer test uses clang's scan-build
434# does not analyze zlibWrapper, due to detected issues in zlib source code
435staticAnalyze: SCANBUILD ?= scan-build
436staticAnalyze:
437	$(CC) -v
438	CC=$(CC) CPPFLAGS=-g $(SCANBUILD) --status-bugs -v $(MAKE) zstd
439endif
440