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