648db22b |
1 | # ################################################################ |
2 | # Copyright (c) Meta Platforms, Inc. and affiliates. |
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 |
11 | DESTDIR ?= |
12 | PREFIX ?= /usr/local |
13 | BINDIR := $(DESTDIR)$(PREFIX)/bin |
14 | |
15 | ZSTDDIR = ../../lib |
16 | PROGDIR = ../../programs |
17 | |
18 | # External program to use to run tests, e.g. qemu or valgrind |
19 | TESTPROG ?= |
20 | # Flags to pass to the tests |
21 | TESTFLAGS ?= |
22 | |
23 | # We use gcc/clang to generate the header dependencies of files |
24 | DEPFLAGS = -MMD -MP -MF $*.Td |
25 | POSTCOMPILE = mv -f $*.Td $*.d |
26 | |
27 | # CFLAGS, CXXFLAGS, CPPFLAGS, and LDFLAGS are for the users to override |
28 | CFLAGS ?= -O3 -Wall -Wextra |
29 | CXXFLAGS ?= -O3 -Wall -Wextra -pedantic |
30 | CPPFLAGS ?= |
31 | LDFLAGS ?= |
32 | |
33 | # PZstd uses legacy APIs |
34 | CFLAGS += -Wno-deprecated-declarations |
35 | |
36 | # Include flags |
37 | PZSTD_INC = -I$(ZSTDDIR) -I$(ZSTDDIR)/common -I$(PROGDIR) -I. |
38 | GTEST_INC = -isystem googletest/googletest/include |
39 | |
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 |
44 | endif |
45 | |
46 | PZSTD_CPPFLAGS = $(PZSTD_INC) |
47 | PZSTD_CCXXFLAGS = |
48 | PZSTD_CFLAGS = $(PZSTD_CCXXFLAGS) |
49 | PZSTD_CXXFLAGS = $(PZSTD_CCXXFLAGS) $(PZSTD_CXX_STD) |
50 | PZSTD_LDFLAGS = |
51 | EXTRA_FLAGS = |
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) |
55 | |
56 | |
57 | # gtest libraries need to go before "-lpthread" because they depend on it. |
58 | GTEST_LIB = -L googletest/build/googlemock/gtest |
59 | LIBS = |
60 | |
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 $@ |
65 | |
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) |
71 | |
72 | |
73 | # Define *.exe as extension for Windows systems |
74 | ifneq (,$(filter Windows%,$(OS))) |
75 | EXT =.exe |
76 | else |
77 | EXT = |
78 | endif |
79 | |
80 | # Standard targets |
81 | .PHONY: default |
82 | default: all |
83 | |
84 | .PHONY: test-pzstd |
85 | test-pzstd: TESTFLAGS=--gtest_filter=-*ExtremelyLarge* |
86 | test-pzstd: clean googletest pzstd tests check |
87 | |
88 | .PHONY: test-pzstd32 |
89 | test-pzstd32: clean googletest32 all32 check |
90 | |
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 |
95 | |
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 |
100 | |
101 | .PHONY: check |
102 | 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) |
111 | |
112 | .PHONY: install |
113 | install: PZSTD_CPPFLAGS += -DNDEBUG |
114 | install: pzstd$(EXT) |
115 | install -d -m 755 $(BINDIR)/ |
116 | install -m 755 pzstd$(EXT) $(BINDIR)/pzstd$(EXT) |
117 | |
118 | .PHONY: uninstall |
119 | uninstall: |
120 | $(RM) $(BINDIR)/pzstd$(EXT) |
121 | |
122 | # Targets for many different builds |
123 | .PHONY: all |
124 | all: PZSTD_CPPFLAGS += -DNDEBUG |
125 | all: pzstd$(EXT) |
126 | |
127 | .PHONY: debug |
128 | debug: EXTRA_FLAGS += -g |
129 | debug: pzstd$(EXT) tests roundtrip |
130 | |
131 | .PHONY: tsan |
132 | tsan: PZSTD_CCXXFLAGS += -fsanitize=thread -fPIC |
133 | tsan: PZSTD_LDFLAGS += -fsanitize=thread |
134 | tsan: debug |
135 | |
136 | .PHONY: asan |
137 | asan: EXTRA_FLAGS += -fsanitize=address |
138 | asan: debug |
139 | |
140 | .PHONY: ubsan |
141 | ubsan: EXTRA_FLAGS += -fsanitize=undefined |
142 | ubsan: debug |
143 | |
144 | .PHONY: all32 |
145 | all32: EXTRA_FLAGS += -m32 |
146 | all32: all tests roundtrip |
147 | |
148 | .PHONY: debug32 |
149 | debug32: EXTRA_FLAGS += -m32 |
150 | debug32: debug |
151 | |
152 | .PHONY: asan32 |
153 | asan32: EXTRA_FLAGS += -m32 |
154 | asan32: asan |
155 | |
156 | .PHONY: tsan32 |
157 | tsan32: EXTRA_FLAGS += -m32 |
158 | tsan32: tsan |
159 | |
160 | .PHONY: ubsan32 |
161 | ubsan32: EXTRA_FLAGS += -m32 |
162 | ubsan32: ubsan |
163 | |
164 | # Run long round trip tests |
165 | .PHONY: roundtripcheck |
166 | roundtripcheck: roundtrip check |
167 | $(TESTPROG) ./test/RoundTripTest$(EXT) $(TESTFLAGS) |
168 | |
169 | # Build the main binary |
170 | pzstd$(EXT): main.o $(PROGDIR)/util.o Options.o Pzstd.o SkippableFrame.o $(ZSTDDIR)/libzstd.a |
171 | $(LD_COMMAND) |
172 | |
173 | # Target that depends on all the tests |
174 | .PHONY: tests |
175 | tests: EXTRA_FLAGS += -Wno-deprecated-declarations |
176 | tests: $(patsubst %,%$(EXT),$(basename $(PZSTD_TESTS) $(UTILS_TESTS))) |
177 | |
178 | # Build the round trip tests |
179 | .PHONY: roundtrip |
180 | roundtrip: EXTRA_FLAGS += -Wno-deprecated-declarations |
181 | roundtrip: test/RoundTripTest$(EXT) |
182 | |
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 |
188 | |
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 |
193 | $(LD_COMMAND) |
194 | |
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 |
200 | $(LD_COMMAND) |
201 | |
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 |
205 | $(LD_COMMAND) |
206 | |
207 | |
208 | GTEST_CMAKEFLAGS = |
209 | |
210 | # Install googletest |
211 | .PHONY: googletest |
212 | googletest: PZSTD_CCXXFLAGS += -fPIC |
213 | googletest: |
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) |
218 | |
219 | .PHONY: googletest32 |
220 | googletest32: PZSTD_CCXXFLAGS += -m32 |
221 | googletest32: googletest |
222 | |
223 | .PHONY: googletest-mingw64 |
224 | googletest-mingw64: GTEST_CMAKEFLAGS += -G "MSYS Makefiles" |
225 | googletest-mingw64: googletest |
226 | |
227 | .PHONY: clean |
228 | clean: |
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 |
235 | |
236 | |
237 | # Cancel implicit rules |
238 | %.o: %.c |
239 | %.o: %.cpp |
240 | |
241 | # Object file rules |
242 | %.o: %.c |
243 | $(CC_COMMAND) |
244 | $(POSTCOMPILE) |
245 | |
246 | $(PROGDIR)/%.o: $(PROGDIR)/%.c |
247 | $(CC_COMMAND) |
248 | $(POSTCOMPILE) |
249 | |
250 | %.o: %.cpp |
251 | $(CXX_COMMAND) |
252 | $(POSTCOMPILE) |
253 | |
254 | test/%.o: PZSTD_CPPFLAGS += $(GTEST_INC) |
255 | test/%.o: test/%.cpp |
256 | $(CXX_COMMAND) |
257 | $(POSTCOMPILE) |
258 | |
259 | utils/test/%.o: PZSTD_CPPFLAGS += $(GTEST_INC) |
260 | utils/test/%.o: utils/test/%.cpp |
261 | $(CXX_COMMAND) |
262 | $(POSTCOMPILE) |
263 | |
264 | # Dependency file stuff |
265 | .PRECIOUS: %.d test/%.d utils/test/%.d |
266 | |
267 | # Include rules that specify header file dependencies |
268 | -include $(patsubst %,%.d,$(basename $(ALL_SRCS))) |