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 := $(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 # Set the minimum required by gtest
41 PZSTD_CXX_STD := -std=c++14
43 PZSTD_CPPFLAGS = $(PZSTD_INC)
45 PZSTD_CFLAGS = $(PZSTD_CCXXFLAGS)
46 PZSTD_CXXFLAGS = $(PZSTD_CCXXFLAGS) $(PZSTD_CXX_STD)
49 ALL_CFLAGS = $(EXTRA_FLAGS) $(CPPFLAGS) $(PZSTD_CPPFLAGS) $(CFLAGS) $(PZSTD_CFLAGS)
50 ALL_CXXFLAGS = $(EXTRA_FLAGS) $(CPPFLAGS) $(PZSTD_CPPFLAGS) $(CXXFLAGS) $(PZSTD_CXXFLAGS)
51 ALL_LDFLAGS = $(EXTRA_FLAGS) $(CXXFLAGS) $(LDFLAGS) $(PZSTD_LDFLAGS)
54 # gtest libraries need to go before "-lpthread" because they depend on it.
55 GTEST_LIB = -L googletest/build/googlemock/gtest
58 # Compilation commands
59 LD_COMMAND = $(CXX) $^ $(ALL_LDFLAGS) $(LIBS) -pthread -o $@
60 CC_COMMAND = $(CC) $(DEPFLAGS) $(ALL_CFLAGS) -c $< -o $@
61 CXX_COMMAND = $(CXX) $(DEPFLAGS) $(ALL_CXXFLAGS) -c $< -o $@
63 # List all the pzstd source files so we can determine their dependencies
64 PZSTD_SRCS := $(wildcard *.cpp)
65 PZSTD_TESTS := $(wildcard test/*.cpp)
66 UTILS_TESTS := $(wildcard utils/test/*.cpp)
67 ALL_SRCS := $(PZSTD_SRCS) $(PZSTD_TESTS) $(UTILS_TESTS)
70 # Define *.exe as extension for Windows systems
71 ifneq (,$(filter Windows%,$(OS)))
82 test-pzstd: TESTFLAGS=--gtest_filter=-*ExtremelyLarge*
83 test-pzstd: clean googletest pzstd tests check
86 test-pzstd32: clean googletest32 all32 check
88 .PHONY: test-pzstd-tsan
89 test-pzstd-tsan: LDFLAGS=-fuse-ld=gold
90 test-pzstd-tsan: TESTFLAGS=--gtest_filter=-*ExtremelyLarge*
91 test-pzstd-tsan: clean googletest tsan check
93 .PHONY: test-pzstd-asan
94 test-pzstd-asan: LDFLAGS=-fuse-ld=gold
95 test-pzstd-asan: TESTFLAGS=--gtest_filter=-*ExtremelyLarge*
96 test-pzstd-asan: clean asan check
100 $(TESTPROG) ./utils/test/BufferTest$(EXT) $(TESTFLAGS)
101 $(TESTPROG) ./utils/test/RangeTest$(EXT) $(TESTFLAGS)
102 $(TESTPROG) ./utils/test/ResourcePoolTest$(EXT) $(TESTFLAGS)
103 $(TESTPROG) ./utils/test/ScopeGuardTest$(EXT) $(TESTFLAGS)
104 $(TESTPROG) ./utils/test/ThreadPoolTest$(EXT) $(TESTFLAGS)
105 $(TESTPROG) ./utils/test/WorkQueueTest$(EXT) $(TESTFLAGS)
106 $(TESTPROG) ./test/OptionsTest$(EXT) $(TESTFLAGS)
107 $(TESTPROG) ./test/PzstdTest$(EXT) $(TESTFLAGS)
110 install: PZSTD_CPPFLAGS += -DNDEBUG
112 install -d -m 755 $(DESTDIR)$(BINDIR)/
113 install -m 755 pzstd$(EXT) $(DESTDIR)$(BINDIR)/pzstd$(EXT)
117 $(RM) $(DESTDIR)$(BINDIR)/pzstd$(EXT)
119 # Targets for many different builds
121 all: PZSTD_CPPFLAGS += -DNDEBUG
125 debug: EXTRA_FLAGS += -g
126 debug: pzstd$(EXT) tests roundtrip
129 tsan: PZSTD_CCXXFLAGS += -fsanitize=thread -fPIC
130 tsan: PZSTD_LDFLAGS += -fsanitize=thread
134 asan: EXTRA_FLAGS += -fsanitize=address
138 ubsan: EXTRA_FLAGS += -fsanitize=undefined
142 all32: EXTRA_FLAGS += -m32
143 all32: all tests roundtrip
146 debug32: EXTRA_FLAGS += -m32
150 asan32: EXTRA_FLAGS += -m32
154 tsan32: EXTRA_FLAGS += -m32
158 ubsan32: EXTRA_FLAGS += -m32
161 # Run long round trip tests
162 .PHONY: roundtripcheck
163 roundtripcheck: roundtrip check
164 $(TESTPROG) ./test/RoundTripTest$(EXT) $(TESTFLAGS)
166 # Build the main binary
167 pzstd$(EXT): main.o $(PROGDIR)/util.o Options.o Pzstd.o SkippableFrame.o $(ZSTDDIR)/libzstd.a
170 # Target that depends on all the tests
172 tests: EXTRA_FLAGS += -Wno-deprecated-declarations
173 tests: $(patsubst %,%$(EXT),$(basename $(PZSTD_TESTS) $(UTILS_TESTS)))
175 # Build the round trip tests
177 roundtrip: EXTRA_FLAGS += -Wno-deprecated-declarations
178 roundtrip: test/RoundTripTest$(EXT)
180 # Use the static library that zstd builds for simplicity and
181 # so we get the compiler options correct
182 .PHONY: $(ZSTDDIR)/libzstd.a
183 $(ZSTDDIR)/libzstd.a:
184 CFLAGS="$(ALL_CFLAGS)" LDFLAGS="$(ALL_LDFLAGS)" $(MAKE) -C $(ZSTDDIR) libzstd.a
186 # Rules to build the tests
187 test/RoundTripTest$(EXT): test/RoundTripTest.o $(PROGDIR)/datagen.o \
188 $(PROGDIR)/util.o Options.o \
189 Pzstd.o SkippableFrame.o $(ZSTDDIR)/libzstd.a
192 test/%Test$(EXT): PZSTD_LDFLAGS += $(GTEST_LIB)
193 test/%Test$(EXT): LIBS += -lgtest -lgtest_main
194 test/%Test$(EXT): test/%Test.o $(PROGDIR)/datagen.o \
195 $(PROGDIR)/util.o Options.o Pzstd.o \
196 SkippableFrame.o $(ZSTDDIR)/libzstd.a
199 utils/test/%Test$(EXT): PZSTD_LDFLAGS += $(GTEST_LIB)
200 utils/test/%Test$(EXT): LIBS += -lgtest -lgtest_main
201 utils/test/%Test$(EXT): utils/test/%Test.o
209 googletest: PZSTD_CCXXFLAGS += -fPIC
211 @$(RM) -rf googletest
212 @git clone https://github.com/google/googletest
213 @mkdir -p googletest/build
214 @cd googletest/build && cmake $(GTEST_CMAKEFLAGS) -DCMAKE_CXX_FLAGS="$(ALL_CXXFLAGS)" .. && $(MAKE)
217 googletest32: PZSTD_CCXXFLAGS += -m32
218 googletest32: googletest
220 .PHONY: googletest-mingw64
221 googletest-mingw64: GTEST_CMAKEFLAGS += -G "MSYS Makefiles"
222 googletest-mingw64: googletest
226 $(RM) -f *.o pzstd$(EXT) *.Td *.d
227 $(RM) -f test/*.o test/*Test$(EXT) test/*.Td test/*.d
228 $(RM) -f utils/test/*.o utils/test/*Test$(EXT) utils/test/*.Td utils/test/*.d
229 $(RM) -f $(PROGDIR)/*.o $(PROGDIR)/*.Td $(PROGDIR)/*.d
230 $(MAKE) -C $(ZSTDDIR) clean
231 @echo Cleaning completed
234 # Cancel implicit rules
243 $(PROGDIR)/%.o: $(PROGDIR)/%.c
251 test/%.o: PZSTD_CPPFLAGS += $(GTEST_INC)
256 utils/test/%.o: PZSTD_CPPFLAGS += $(GTEST_INC)
257 utils/test/%.o: utils/test/%.cpp
261 # Dependency file stuff
262 .PRECIOUS: %.d test/%.d utils/test/%.d
264 # Include rules that specify header file dependencies
265 -include $(patsubst %,%.d,$(basename $(ALL_SRCS)))