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