1 # ################################################################
2 # Copyright (c) 2016-present, Facebook, Inc.
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 # ################################################################
10 # Standard variables for installation
13 BINDIR := $(DESTDIR)$(PREFIX)/bin
16 PROGDIR = ../../programs
18 # External program to use to run tests, e.g. qemu or valgrind
20 # Flags to pass to the tests
23 # We use gcc/clang to generate the header dependencies of files
24 DEPFLAGS = -MMD -MP -MF $*.Td
25 POSTCOMPILE = mv -f $*.Td $*.d
27 # CFLAGS, CXXFLAGS, CPPFLAGS, and LDFLAGS are for the users to override
28 CFLAGS ?= -O3 -Wall -Wextra
29 CXXFLAGS ?= -O3 -Wall -Wextra -pedantic
34 PZSTD_INC = -I$(ZSTDDIR) -I$(ZSTDDIR)/common -I$(PROGDIR) -I.
35 GTEST_INC = -isystem googletest/googletest/include
37 PZSTD_CPPFLAGS = $(PZSTD_INC)
39 PZSTD_CFLAGS = $(PZSTD_CCXXFLAGS)
40 PZSTD_CXXFLAGS = $(PZSTD_CCXXFLAGS) -std=c++11
43 ALL_CFLAGS = $(EXTRA_FLAGS) $(CPPFLAGS) $(PZSTD_CPPFLAGS) $(CFLAGS) $(PZSTD_CFLAGS)
44 ALL_CXXFLAGS = $(EXTRA_FLAGS) $(CPPFLAGS) $(PZSTD_CPPFLAGS) $(CXXFLAGS) $(PZSTD_CXXFLAGS)
45 ALL_LDFLAGS = $(EXTRA_FLAGS) $(CXXFLAGS) $(LDFLAGS) $(PZSTD_LDFLAGS)
48 # gtest libraries need to go before "-lpthread" because they depend on it.
49 GTEST_LIB = -L googletest/build/googlemock/gtest
52 # Compilation commands
53 LD_COMMAND = $(CXX) $^ $(ALL_LDFLAGS) $(LIBS) -pthread -o $@
54 CC_COMMAND = $(CC) $(DEPFLAGS) $(ALL_CFLAGS) -c $< -o $@
55 CXX_COMMAND = $(CXX) $(DEPFLAGS) $(ALL_CXXFLAGS) -c $< -o $@
57 # Get a list of all zstd files so we rebuild the static library when we need to
58 ZSTDCOMMON_FILES := $(wildcard $(ZSTDDIR)/common/*.c) \
59 $(wildcard $(ZSTDDIR)/common/*.h)
60 ZSTDCOMP_FILES := $(wildcard $(ZSTDDIR)/compress/*.c) \
61 $(wildcard $(ZSTDDIR)/compress/*.h)
62 ZSTDDECOMP_FILES := $(wildcard $(ZSTDDIR)/decompress/*.c) \
63 $(wildcard $(ZSTDDIR)/decompress/*.h)
64 ZSTDPROG_FILES := $(wildcard $(PROGDIR)/*.c) \
65 $(wildcard $(PROGDIR)/*.h)
66 ZSTD_FILES := $(wildcard $(ZSTDDIR)/*.h) \
67 $(ZSTDDECOMP_FILES) $(ZSTDCOMMON_FILES) $(ZSTDCOMP_FILES) \
70 # List all the pzstd source files so we can determine their dependencies
71 PZSTD_SRCS := $(wildcard *.cpp)
72 PZSTD_TESTS := $(wildcard test/*.cpp)
73 UTILS_TESTS := $(wildcard utils/test/*.cpp)
74 ALL_SRCS := $(PZSTD_SRCS) $(PZSTD_TESTS) $(UTILS_TESTS)
77 # Define *.exe as extension for Windows systems
78 ifneq (,$(filter Windows%,$(OS)))
89 test-pzstd: TESTFLAGS=--gtest_filter=-*ExtremelyLarge*
90 test-pzstd: clean googletest pzstd tests check
93 test-pzstd32: clean googletest32 all32 check
95 .PHONY: test-pzstd-tsan
96 test-pzstd-tsan: LDFLAGS=-fuse-ld=gold
97 test-pzstd-tsan: TESTFLAGS=--gtest_filter=-*ExtremelyLarge*
98 test-pzstd-tsan: clean googletest tsan check
100 .PHONY: test-pzstd-asan
101 test-pzstd-asan: LDFLAGS=-fuse-ld=gold
102 test-pzstd-asan: TESTFLAGS=--gtest_filter=-*ExtremelyLarge*
103 test-pzstd-asan: clean asan check
107 $(TESTPROG) ./utils/test/BufferTest$(EXT) $(TESTFLAGS)
108 $(TESTPROG) ./utils/test/RangeTest$(EXT) $(TESTFLAGS)
109 $(TESTPROG) ./utils/test/ResourcePoolTest$(EXT) $(TESTFLAGS)
110 $(TESTPROG) ./utils/test/ScopeGuardTest$(EXT) $(TESTFLAGS)
111 $(TESTPROG) ./utils/test/ThreadPoolTest$(EXT) $(TESTFLAGS)
112 $(TESTPROG) ./utils/test/WorkQueueTest$(EXT) $(TESTFLAGS)
113 $(TESTPROG) ./test/OptionsTest$(EXT) $(TESTFLAGS)
114 $(TESTPROG) ./test/PzstdTest$(EXT) $(TESTFLAGS)
117 install: PZSTD_CPPFLAGS += -DNDEBUG
119 install -d -m 755 $(BINDIR)/
120 install -m 755 pzstd$(EXT) $(BINDIR)/pzstd$(EXT)
124 $(RM) $(BINDIR)/pzstd$(EXT)
126 # Targets for many different builds
128 all: PZSTD_CPPFLAGS += -DNDEBUG
132 debug: EXTRA_FLAGS += -g
133 debug: pzstd$(EXT) tests roundtrip
136 tsan: PZSTD_CCXXFLAGS += -fsanitize=thread -fPIC
137 tsan: PZSTD_LDFLAGS += -fsanitize=thread
141 asan: EXTRA_FLAGS += -fsanitize=address
145 ubsan: EXTRA_FLAGS += -fsanitize=undefined
149 all32: EXTRA_FLAGS += -m32
150 all32: all tests roundtrip
153 debug32: EXTRA_FLAGS += -m32
157 asan32: EXTRA_FLAGS += -m32
161 tsan32: EXTRA_FLAGS += -m32
165 ubsan32: EXTRA_FLAGS += -m32
168 # Run long round trip tests
169 .PHONY: roundtripcheck
170 roundtripcheck: roundtrip check
171 $(TESTPROG) ./test/RoundTripTest$(EXT) $(TESTFLAGS)
173 # Build the main binary
174 pzstd$(EXT): main.o $(PROGDIR)/util.o Options.o Pzstd.o SkippableFrame.o $(ZSTDDIR)/libzstd.a
177 # Target that depends on all the tests
179 tests: EXTRA_FLAGS += -Wno-deprecated-declarations
180 tests: $(patsubst %,%$(EXT),$(basename $(PZSTD_TESTS) $(UTILS_TESTS)))
182 # Build the round trip tests
184 roundtrip: EXTRA_FLAGS += -Wno-deprecated-declarations
185 roundtrip: test/RoundTripTest$(EXT)
187 # Use the static library that zstd builds for simplicity and
188 # so we get the compiler options correct
189 $(ZSTDDIR)/libzstd.a: $(ZSTD_FILES)
190 CFLAGS="$(ALL_CFLAGS)" LDFLAGS="$(ALL_LDFLAGS)" $(MAKE) -C $(ZSTDDIR) libzstd.a
192 # Rules to build the tests
193 test/RoundTripTest$(EXT): test/RoundTripTest.o $(PROGDIR)/datagen.o Options.o \
194 Pzstd.o SkippableFrame.o $(ZSTDDIR)/libzstd.a
197 test/%Test$(EXT): PZSTD_LDFLAGS += $(GTEST_LIB)
198 test/%Test$(EXT): LIBS += -lgtest -lgtest_main
199 test/%Test$(EXT): test/%Test.o $(PROGDIR)/datagen.o Options.o Pzstd.o \
200 SkippableFrame.o $(ZSTDDIR)/libzstd.a
203 utils/test/%Test$(EXT): PZSTD_LDFLAGS += $(GTEST_LIB)
204 utils/test/%Test$(EXT): LIBS += -lgtest -lgtest_main
205 utils/test/%Test$(EXT): utils/test/%Test.o
213 googletest: PZSTD_CCXXFLAGS += -fPIC
215 @$(RM) -rf googletest
216 @git clone https://github.com/google/googletest
217 @mkdir -p googletest/build
218 @cd googletest/build && cmake $(GTEST_CMAKEFLAGS) -DCMAKE_CXX_FLAGS="$(ALL_CXXFLAGS)" .. && $(MAKE)
221 googletest32: PZSTD_CCXXFLAGS += -m32
222 googletest32: googletest
224 .PHONY: googletest-mingw64
225 googletest-mingw64: GTEST_CMAKEFLAGS += -G "MSYS Makefiles"
226 googletest-mingw64: googletest
230 $(RM) -f *.o pzstd$(EXT) *.Td *.d
231 $(RM) -f test/*.o test/*Test$(EXT) test/*.Td test/*.d
232 $(RM) -f utils/test/*.o utils/test/*Test$(EXT) utils/test/*.Td utils/test/*.d
233 $(RM) -f $(PROGDIR)/*.o $(PROGDIR)/*.Td $(PROGDIR)/*.d
234 $(MAKE) -C $(ZSTDDIR) clean
235 @echo Cleaning completed
238 # Cancel implicit rules
247 $(PROGDIR)/%.o: $(PROGDIR)/%.c
255 test/%.o: PZSTD_CPPFLAGS += $(GTEST_INC)
260 utils/test/%.o: PZSTD_CPPFLAGS += $(GTEST_INC)
261 utils/test/%.o: utils/test/%.cpp
265 # Dependency file stuff
266 .PRECIOUS: %.d test/%.d utils/test/%.d
268 # Include rules that specify header file dependencies
269 -include $(patsubst %,%.d,$(basename $(ALL_SRCS)))