1 # ################################################################
2 # Copyright (c) Meta Platforms, Inc. and affiliates.
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
33 # PZstd uses legacy APIs
34 CFLAGS += -Wno-deprecated-declarations
37 PZSTD_INC = -I$(ZSTDDIR) -I$(ZSTDDIR)/common -I$(PROGDIR) -I.
38 GTEST_INC = -isystem googletest/googletest/include
40 # If default C++ version is older than C++11, explicitly set C++11, which is the
41 # minimum required by the code.
42 ifeq ($(shell echo "\043if __cplusplus < 201103L\n\043error\n\043endif" | $(CXX) -x c++ -Werror -c - -o /dev/null 2>/dev/null && echo 1 || echo 0),0)
43 PZSTD_CXX_STD := -std=c++11
46 PZSTD_CPPFLAGS = $(PZSTD_INC)
48 PZSTD_CFLAGS = $(PZSTD_CCXXFLAGS)
49 PZSTD_CXXFLAGS = $(PZSTD_CCXXFLAGS) $(PZSTD_CXX_STD)
52 ALL_CFLAGS = $(EXTRA_FLAGS) $(CPPFLAGS) $(PZSTD_CPPFLAGS) $(CFLAGS) $(PZSTD_CFLAGS)
53 ALL_CXXFLAGS = $(EXTRA_FLAGS) $(CPPFLAGS) $(PZSTD_CPPFLAGS) $(CXXFLAGS) $(PZSTD_CXXFLAGS)
54 ALL_LDFLAGS = $(EXTRA_FLAGS) $(CXXFLAGS) $(LDFLAGS) $(PZSTD_LDFLAGS)
57 # gtest libraries need to go before "-lpthread" because they depend on it.
58 GTEST_LIB = -L googletest/build/googlemock/gtest
61 # Compilation commands
62 LD_COMMAND = $(CXX) $^ $(ALL_LDFLAGS) $(LIBS) -pthread -o $@
63 CC_COMMAND = $(CC) $(DEPFLAGS) $(ALL_CFLAGS) -c $< -o $@
64 CXX_COMMAND = $(CXX) $(DEPFLAGS) $(ALL_CXXFLAGS) -c $< -o $@
66 # List all the pzstd source files so we can determine their dependencies
67 PZSTD_SRCS := $(wildcard *.cpp)
68 PZSTD_TESTS := $(wildcard test/*.cpp)
69 UTILS_TESTS := $(wildcard utils/test/*.cpp)
70 ALL_SRCS := $(PZSTD_SRCS) $(PZSTD_TESTS) $(UTILS_TESTS)
73 # Define *.exe as extension for Windows systems
74 ifneq (,$(filter Windows%,$(OS)))
85 test-pzstd: TESTFLAGS=--gtest_filter=-*ExtremelyLarge*
86 test-pzstd: clean googletest pzstd tests check
89 test-pzstd32: clean googletest32 all32 check
91 .PHONY: test-pzstd-tsan
92 test-pzstd-tsan: LDFLAGS=-fuse-ld=gold
93 test-pzstd-tsan: TESTFLAGS=--gtest_filter=-*ExtremelyLarge*
94 test-pzstd-tsan: clean googletest tsan check
96 .PHONY: test-pzstd-asan
97 test-pzstd-asan: LDFLAGS=-fuse-ld=gold
98 test-pzstd-asan: TESTFLAGS=--gtest_filter=-*ExtremelyLarge*
99 test-pzstd-asan: clean asan check
103 $(TESTPROG) ./utils/test/BufferTest$(EXT) $(TESTFLAGS)
104 $(TESTPROG) ./utils/test/RangeTest$(EXT) $(TESTFLAGS)
105 $(TESTPROG) ./utils/test/ResourcePoolTest$(EXT) $(TESTFLAGS)
106 $(TESTPROG) ./utils/test/ScopeGuardTest$(EXT) $(TESTFLAGS)
107 $(TESTPROG) ./utils/test/ThreadPoolTest$(EXT) $(TESTFLAGS)
108 $(TESTPROG) ./utils/test/WorkQueueTest$(EXT) $(TESTFLAGS)
109 $(TESTPROG) ./test/OptionsTest$(EXT) $(TESTFLAGS)
110 $(TESTPROG) ./test/PzstdTest$(EXT) $(TESTFLAGS)
113 install: PZSTD_CPPFLAGS += -DNDEBUG
115 install -d -m 755 $(BINDIR)/
116 install -m 755 pzstd$(EXT) $(BINDIR)/pzstd$(EXT)
120 $(RM) $(BINDIR)/pzstd$(EXT)
122 # Targets for many different builds
124 all: PZSTD_CPPFLAGS += -DNDEBUG
128 debug: EXTRA_FLAGS += -g
129 debug: pzstd$(EXT) tests roundtrip
132 tsan: PZSTD_CCXXFLAGS += -fsanitize=thread -fPIC
133 tsan: PZSTD_LDFLAGS += -fsanitize=thread
137 asan: EXTRA_FLAGS += -fsanitize=address
141 ubsan: EXTRA_FLAGS += -fsanitize=undefined
145 all32: EXTRA_FLAGS += -m32
146 all32: all tests roundtrip
149 debug32: EXTRA_FLAGS += -m32
153 asan32: EXTRA_FLAGS += -m32
157 tsan32: EXTRA_FLAGS += -m32
161 ubsan32: EXTRA_FLAGS += -m32
164 # Run long round trip tests
165 .PHONY: roundtripcheck
166 roundtripcheck: roundtrip check
167 $(TESTPROG) ./test/RoundTripTest$(EXT) $(TESTFLAGS)
169 # Build the main binary
170 pzstd$(EXT): main.o $(PROGDIR)/util.o Options.o Pzstd.o SkippableFrame.o $(ZSTDDIR)/libzstd.a
173 # Target that depends on all the tests
175 tests: EXTRA_FLAGS += -Wno-deprecated-declarations
176 tests: $(patsubst %,%$(EXT),$(basename $(PZSTD_TESTS) $(UTILS_TESTS)))
178 # Build the round trip tests
180 roundtrip: EXTRA_FLAGS += -Wno-deprecated-declarations
181 roundtrip: test/RoundTripTest$(EXT)
183 # Use the static library that zstd builds for simplicity and
184 # so we get the compiler options correct
185 .PHONY: $(ZSTDDIR)/libzstd.a
186 $(ZSTDDIR)/libzstd.a:
187 CFLAGS="$(ALL_CFLAGS)" LDFLAGS="$(ALL_LDFLAGS)" $(MAKE) -C $(ZSTDDIR) libzstd.a
189 # Rules to build the tests
190 test/RoundTripTest$(EXT): test/RoundTripTest.o $(PROGDIR)/datagen.o \
191 $(PROGDIR)/util.o Options.o \
192 Pzstd.o SkippableFrame.o $(ZSTDDIR)/libzstd.a
195 test/%Test$(EXT): PZSTD_LDFLAGS += $(GTEST_LIB)
196 test/%Test$(EXT): LIBS += -lgtest -lgtest_main
197 test/%Test$(EXT): test/%Test.o $(PROGDIR)/datagen.o \
198 $(PROGDIR)/util.o Options.o Pzstd.o \
199 SkippableFrame.o $(ZSTDDIR)/libzstd.a
202 utils/test/%Test$(EXT): PZSTD_LDFLAGS += $(GTEST_LIB)
203 utils/test/%Test$(EXT): LIBS += -lgtest -lgtest_main
204 utils/test/%Test$(EXT): utils/test/%Test.o
212 googletest: PZSTD_CCXXFLAGS += -fPIC
214 @$(RM) -rf googletest
215 @git clone https://github.com/google/googletest
216 @mkdir -p googletest/build
217 @cd googletest/build && cmake $(GTEST_CMAKEFLAGS) -DCMAKE_CXX_FLAGS="$(ALL_CXXFLAGS)" .. && $(MAKE)
220 googletest32: PZSTD_CCXXFLAGS += -m32
221 googletest32: googletest
223 .PHONY: googletest-mingw64
224 googletest-mingw64: GTEST_CMAKEFLAGS += -G "MSYS Makefiles"
225 googletest-mingw64: googletest
229 $(RM) -f *.o pzstd$(EXT) *.Td *.d
230 $(RM) -f test/*.o test/*Test$(EXT) test/*.Td test/*.d
231 $(RM) -f utils/test/*.o utils/test/*Test$(EXT) utils/test/*.Td utils/test/*.d
232 $(RM) -f $(PROGDIR)/*.o $(PROGDIR)/*.Td $(PROGDIR)/*.d
233 $(MAKE) -C $(ZSTDDIR) clean
234 @echo Cleaning completed
237 # Cancel implicit rules
246 $(PROGDIR)/%.o: $(PROGDIR)/%.c
254 test/%.o: PZSTD_CPPFLAGS += $(GTEST_INC)
259 utils/test/%.o: PZSTD_CPPFLAGS += $(GTEST_INC)
260 utils/test/%.o: utils/test/%.cpp
264 # Dependency file stuff
265 .PRECIOUS: %.d test/%.d utils/test/%.d
267 # Include rules that specify header file dependencies
268 -include $(patsubst %,%.d,$(basename $(ALL_SRCS)))