1# ################################################################
2# Copyright (c) 2016-present, 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
10# Standard variables for installation
11DESTDIR ?=
12PREFIX  ?= /usr/local
13BINDIR  := $(DESTDIR)$(PREFIX)/bin
14
15ZSTDDIR = ../../lib
16PROGDIR = ../../programs
17
18# External program to use to run tests, e.g. qemu or valgrind
19TESTPROG  ?=
20# Flags to pass to the tests
21TESTFLAGS ?=
22
23# We use gcc/clang to generate the header dependencies of files
24DEPFLAGS = -MMD -MP -MF $*.Td
25POSTCOMPILE = mv -f $*.Td $*.d
26
27# CFLAGS, CXXFLAGS, CPPFLAGS, and LDFLAGS are for the users to override
28CFLAGS   ?= -O3 -Wall -Wextra
29CXXFLAGS ?= -O3 -Wall -Wextra -pedantic
30CPPFLAGS ?=
31LDFLAGS  ?=
32
33# PZstd uses legacy APIs
34CFLAGS   += -Wno-deprecated-declarations
35
36# Include flags
37PZSTD_INC  = -I$(ZSTDDIR) -I$(ZSTDDIR)/common -I$(PROGDIR) -I.
38GTEST_INC  = -isystem googletest/googletest/include
39
40PZSTD_CPPFLAGS  = $(PZSTD_INC)
41PZSTD_CCXXFLAGS =
42PZSTD_CFLAGS    = $(PZSTD_CCXXFLAGS)
43PZSTD_CXXFLAGS  = $(PZSTD_CCXXFLAGS) -std=c++11
44PZSTD_LDFLAGS   =
45EXTRA_FLAGS     =
46ALL_CFLAGS      = $(EXTRA_FLAGS) $(CPPFLAGS) $(PZSTD_CPPFLAGS) $(CFLAGS)   $(PZSTD_CFLAGS)
47ALL_CXXFLAGS    = $(EXTRA_FLAGS) $(CPPFLAGS) $(PZSTD_CPPFLAGS) $(CXXFLAGS) $(PZSTD_CXXFLAGS)
48ALL_LDFLAGS     = $(EXTRA_FLAGS) $(CXXFLAGS) $(LDFLAGS) $(PZSTD_LDFLAGS)
49
50
51# gtest libraries need to go before "-lpthread" because they depend on it.
52GTEST_LIB  = -L googletest/build/googlemock/gtest
53LIBS       =
54
55# Compilation commands
56LD_COMMAND  = $(CXX) $^          $(ALL_LDFLAGS) $(LIBS) -pthread -o $@
57CC_COMMAND  = $(CC)  $(DEPFLAGS) $(ALL_CFLAGS)   -c $<  -o $@
58CXX_COMMAND = $(CXX) $(DEPFLAGS) $(ALL_CXXFLAGS) -c $<  -o $@
59
60# Get a list of all zstd files so we rebuild the static library when we need to
61ZSTDCOMMON_FILES := $(wildcard $(ZSTDDIR)/common/*.c) \
62                    $(wildcard $(ZSTDDIR)/common/*.h)
63ZSTDCOMP_FILES   := $(wildcard $(ZSTDDIR)/compress/*.c) \
64                    $(wildcard $(ZSTDDIR)/compress/*.h)
65ZSTDDECOMP_FILES := $(wildcard $(ZSTDDIR)/decompress/*.c) \
66                    $(wildcard $(ZSTDDIR)/decompress/*.h)
67ZSTDPROG_FILES   := $(wildcard $(PROGDIR)/*.c) \
68                    $(wildcard $(PROGDIR)/*.h)
69ZSTD_FILES       := $(wildcard $(ZSTDDIR)/*.h) \
70                    $(ZSTDDECOMP_FILES) $(ZSTDCOMMON_FILES) $(ZSTDCOMP_FILES) \
71                    $(ZSTDPROG_FILES)
72
73# List all the pzstd source files so we can determine their dependencies
74PZSTD_SRCS  := $(wildcard *.cpp)
75PZSTD_TESTS := $(wildcard test/*.cpp)
76UTILS_TESTS := $(wildcard utils/test/*.cpp)
77ALL_SRCS    := $(PZSTD_SRCS) $(PZSTD_TESTS) $(UTILS_TESTS)
78
79
80# Define *.exe as extension for Windows systems
81ifneq (,$(filter Windows%,$(OS)))
82EXT =.exe
83else
84EXT =
85endif
86
87# Standard targets
88.PHONY: default
89default: all
90
91.PHONY: test-pzstd
92test-pzstd: TESTFLAGS=--gtest_filter=-*ExtremelyLarge*
93test-pzstd: clean googletest pzstd tests check
94
95.PHONY: test-pzstd32
96test-pzstd32: clean googletest32 all32 check
97
98.PHONY: test-pzstd-tsan
99test-pzstd-tsan: LDFLAGS=-fuse-ld=gold
100test-pzstd-tsan: TESTFLAGS=--gtest_filter=-*ExtremelyLarge*
101test-pzstd-tsan: clean googletest tsan check
102
103.PHONY: test-pzstd-asan
104test-pzstd-asan: LDFLAGS=-fuse-ld=gold
105test-pzstd-asan: TESTFLAGS=--gtest_filter=-*ExtremelyLarge*
106test-pzstd-asan: clean asan check
107
108.PHONY: check
109check:
110	$(TESTPROG) ./utils/test/BufferTest$(EXT) $(TESTFLAGS)
111	$(TESTPROG) ./utils/test/RangeTest$(EXT) $(TESTFLAGS)
112	$(TESTPROG) ./utils/test/ResourcePoolTest$(EXT) $(TESTFLAGS)
113	$(TESTPROG) ./utils/test/ScopeGuardTest$(EXT) $(TESTFLAGS)
114	$(TESTPROG) ./utils/test/ThreadPoolTest$(EXT) $(TESTFLAGS)
115	$(TESTPROG) ./utils/test/WorkQueueTest$(EXT) $(TESTFLAGS)
116	$(TESTPROG) ./test/OptionsTest$(EXT) $(TESTFLAGS)
117	$(TESTPROG) ./test/PzstdTest$(EXT) $(TESTFLAGS)
118
119.PHONY: install
120install: PZSTD_CPPFLAGS += -DNDEBUG
121install: pzstd$(EXT)
122	install -d -m 755 $(BINDIR)/
123	install -m 755 pzstd$(EXT) $(BINDIR)/pzstd$(EXT)
124
125.PHONY: uninstall
126uninstall:
127	$(RM) $(BINDIR)/pzstd$(EXT)
128
129# Targets for many different builds
130.PHONY: all
131all: PZSTD_CPPFLAGS += -DNDEBUG
132all: pzstd$(EXT)
133
134.PHONY: debug
135debug: EXTRA_FLAGS += -g
136debug: pzstd$(EXT) tests roundtrip
137
138.PHONY: tsan
139tsan: PZSTD_CCXXFLAGS += -fsanitize=thread -fPIC
140tsan: PZSTD_LDFLAGS   += -fsanitize=thread
141tsan: debug
142
143.PHONY: asan
144asan: EXTRA_FLAGS += -fsanitize=address
145asan: debug
146
147.PHONY: ubsan
148ubsan: EXTRA_FLAGS += -fsanitize=undefined
149ubsan: debug
150
151.PHONY: all32
152all32: EXTRA_FLAGS += -m32
153all32: all tests roundtrip
154
155.PHONY: debug32
156debug32: EXTRA_FLAGS += -m32
157debug32: debug
158
159.PHONY: asan32
160asan32: EXTRA_FLAGS += -m32
161asan32: asan
162
163.PHONY: tsan32
164tsan32: EXTRA_FLAGS += -m32
165tsan32: tsan
166
167.PHONY: ubsan32
168ubsan32: EXTRA_FLAGS += -m32
169ubsan32: ubsan
170
171# Run long round trip tests
172.PHONY: roundtripcheck
173roundtripcheck: roundtrip check
174	$(TESTPROG) ./test/RoundTripTest$(EXT) $(TESTFLAGS)
175
176# Build the main binary
177pzstd$(EXT): main.o $(PROGDIR)/util.o Options.o Pzstd.o SkippableFrame.o $(ZSTDDIR)/libzstd.a
178	$(LD_COMMAND)
179
180# Target that depends on all the tests
181.PHONY: tests
182tests: EXTRA_FLAGS += -Wno-deprecated-declarations
183tests: $(patsubst %,%$(EXT),$(basename $(PZSTD_TESTS) $(UTILS_TESTS)))
184
185# Build the round trip tests
186.PHONY: roundtrip
187roundtrip: EXTRA_FLAGS += -Wno-deprecated-declarations
188roundtrip: test/RoundTripTest$(EXT)
189
190# Use the static library that zstd builds for simplicity and
191# so we get the compiler options correct
192$(ZSTDDIR)/libzstd.a: $(ZSTD_FILES)
193	CFLAGS="$(ALL_CFLAGS)" LDFLAGS="$(ALL_LDFLAGS)" $(MAKE) -C $(ZSTDDIR) libzstd.a
194
195# Rules to build the tests
196test/RoundTripTest$(EXT): test/RoundTripTest.o $(PROGDIR)/datagen.o \
197                          $(PROGDIR)/util.o Options.o \
198                          Pzstd.o SkippableFrame.o $(ZSTDDIR)/libzstd.a
199	$(LD_COMMAND)
200
201test/%Test$(EXT): PZSTD_LDFLAGS += $(GTEST_LIB)
202test/%Test$(EXT): LIBS += -lgtest -lgtest_main
203test/%Test$(EXT): test/%Test.o $(PROGDIR)/datagen.o \
204                  $(PROGDIR)/util.o Options.o Pzstd.o \
205                  SkippableFrame.o $(ZSTDDIR)/libzstd.a
206	$(LD_COMMAND)
207
208utils/test/%Test$(EXT): PZSTD_LDFLAGS += $(GTEST_LIB)
209utils/test/%Test$(EXT): LIBS += -lgtest -lgtest_main
210utils/test/%Test$(EXT): utils/test/%Test.o
211	$(LD_COMMAND)
212
213
214GTEST_CMAKEFLAGS =
215
216# Install googletest
217.PHONY: googletest
218googletest: PZSTD_CCXXFLAGS += -fPIC
219googletest:
220	@$(RM) -rf googletest
221	@git clone https://github.com/google/googletest
222	@mkdir -p googletest/build
223	@cd googletest/build && cmake $(GTEST_CMAKEFLAGS) -DCMAKE_CXX_FLAGS="$(ALL_CXXFLAGS)" .. && $(MAKE)
224
225.PHONY: googletest32
226googletest32: PZSTD_CCXXFLAGS  += -m32
227googletest32: googletest
228
229.PHONY: googletest-mingw64
230googletest-mingw64: GTEST_CMAKEFLAGS += -G "MSYS Makefiles"
231googletest-mingw64: googletest
232
233.PHONY: clean
234clean:
235	$(RM) -f *.o pzstd$(EXT) *.Td *.d
236	$(RM) -f test/*.o test/*Test$(EXT) test/*.Td test/*.d
237	$(RM) -f utils/test/*.o utils/test/*Test$(EXT) utils/test/*.Td utils/test/*.d
238	$(RM) -f $(PROGDIR)/*.o $(PROGDIR)/*.Td $(PROGDIR)/*.d
239	$(MAKE) -C $(ZSTDDIR) clean
240	@echo Cleaning completed
241
242
243# Cancel implicit rules
244%.o: %.c
245%.o: %.cpp
246
247# Object file rules
248%.o: %.c
249	$(CC_COMMAND)
250	$(POSTCOMPILE)
251
252$(PROGDIR)/%.o: $(PROGDIR)/%.c
253	$(CC_COMMAND)
254	$(POSTCOMPILE)
255
256%.o: %.cpp
257	$(CXX_COMMAND)
258	$(POSTCOMPILE)
259
260test/%.o: PZSTD_CPPFLAGS += $(GTEST_INC)
261test/%.o: test/%.cpp
262	$(CXX_COMMAND)
263	$(POSTCOMPILE)
264
265utils/test/%.o: PZSTD_CPPFLAGS += $(GTEST_INC)
266utils/test/%.o: utils/test/%.cpp
267	$(CXX_COMMAND)
268	$(POSTCOMPILE)
269
270# Dependency file stuff
271.PRECIOUS: %.d test/%.d utils/test/%.d
272
273# Include rules that specify header file dependencies
274-include $(patsubst %,%.d,$(basename $(ALL_SRCS)))
275