| 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 := $(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 |
# Set the minimum required by gtest |
| 41 |
PZSTD_CXX_STD := -std=c++14 |
| 42 |
|
| 43 |
PZSTD_CPPFLAGS = $(PZSTD_INC) |
| 44 |
PZSTD_CCXXFLAGS = |
| 45 |
PZSTD_CFLAGS = $(PZSTD_CCXXFLAGS) |
| 46 |
PZSTD_CXXFLAGS = $(PZSTD_CCXXFLAGS) $(PZSTD_CXX_STD) |
| 47 |
PZSTD_LDFLAGS = |
| 48 |
EXTRA_FLAGS = |
| 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) |
| 52 |
|
| 53 |
|
| 54 |
# gtest libraries need to go before "-lpthread" because they depend on it. |
| 55 |
GTEST_LIB = -L googletest/build/googlemock/gtest |
| 56 |
LIBS = |
| 57 |
|
| 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 $@ |
| 62 |
|
| 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) |
| 68 |
|
| 69 |
|
| 70 |
# Define *.exe as extension for Windows systems |
| 71 |
ifneq (,$(filter Windows%,$(OS))) |
| 72 |
EXT =.exe |
| 73 |
else |
| 74 |
EXT = |
| 75 |
endif |
| 76 |
|
| 77 |
# Standard targets |
| 78 |
.PHONY: default |
| 79 |
default: all |
| 80 |
|
| 81 |
.PHONY: test-pzstd |
| 82 |
test-pzstd: TESTFLAGS=--gtest_filter=-*ExtremelyLarge* |
| 83 |
test-pzstd: clean googletest pzstd tests check |
| 84 |
|
| 85 |
.PHONY: test-pzstd32 |
| 86 |
test-pzstd32: clean googletest32 all32 check |
| 87 |
|
| 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 |
| 92 |
|
| 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 |
| 97 |
|
| 98 |
.PHONY: check |
| 99 |
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) |
| 108 |
|
| 109 |
.PHONY: install |
| 110 |
install: PZSTD_CPPFLAGS += -DNDEBUG |
| 111 |
install: pzstd$(EXT) |
| 112 |
install -d -m 755 $(DESTDIR)$(BINDIR)/ |
| 113 |
install -m 755 pzstd$(EXT) $(DESTDIR)$(BINDIR)/pzstd$(EXT) |
| 114 |
|
| 115 |
.PHONY: uninstall |
| 116 |
uninstall: |
| 117 |
$(RM) $(DESTDIR)$(BINDIR)/pzstd$(EXT) |
| 118 |
|
| 119 |
# Targets for many different builds |
| 120 |
.PHONY: all |
| 121 |
all: PZSTD_CPPFLAGS += -DNDEBUG |
| 122 |
all: pzstd$(EXT) |
| 123 |
|
| 124 |
.PHONY: debug |
| 125 |
debug: EXTRA_FLAGS += -g |
| 126 |
debug: pzstd$(EXT) tests roundtrip |
| 127 |
|
| 128 |
.PHONY: tsan |
| 129 |
tsan: PZSTD_CCXXFLAGS += -fsanitize=thread -fPIC |
| 130 |
tsan: PZSTD_LDFLAGS += -fsanitize=thread |
| 131 |
tsan: debug |
| 132 |
|
| 133 |
.PHONY: asan |
| 134 |
asan: EXTRA_FLAGS += -fsanitize=address |
| 135 |
asan: debug |
| 136 |
|
| 137 |
.PHONY: ubsan |
| 138 |
ubsan: EXTRA_FLAGS += -fsanitize=undefined |
| 139 |
ubsan: debug |
| 140 |
|
| 141 |
.PHONY: all32 |
| 142 |
all32: EXTRA_FLAGS += -m32 |
| 143 |
all32: all tests roundtrip |
| 144 |
|
| 145 |
.PHONY: debug32 |
| 146 |
debug32: EXTRA_FLAGS += -m32 |
| 147 |
debug32: debug |
| 148 |
|
| 149 |
.PHONY: asan32 |
| 150 |
asan32: EXTRA_FLAGS += -m32 |
| 151 |
asan32: asan |
| 152 |
|
| 153 |
.PHONY: tsan32 |
| 154 |
tsan32: EXTRA_FLAGS += -m32 |
| 155 |
tsan32: tsan |
| 156 |
|
| 157 |
.PHONY: ubsan32 |
| 158 |
ubsan32: EXTRA_FLAGS += -m32 |
| 159 |
ubsan32: ubsan |
| 160 |
|
| 161 |
# Run long round trip tests |
| 162 |
.PHONY: roundtripcheck |
| 163 |
roundtripcheck: roundtrip check |
| 164 |
$(TESTPROG) ./test/RoundTripTest$(EXT) $(TESTFLAGS) |
| 165 |
|
| 166 |
# Build the main binary |
| 167 |
pzstd$(EXT): main.o $(PROGDIR)/util.o Options.o Pzstd.o SkippableFrame.o $(ZSTDDIR)/libzstd.a |
| 168 |
$(LD_COMMAND) |
| 169 |
|
| 170 |
# Target that depends on all the tests |
| 171 |
.PHONY: tests |
| 172 |
tests: EXTRA_FLAGS += -Wno-deprecated-declarations |
| 173 |
tests: $(patsubst %,%$(EXT),$(basename $(PZSTD_TESTS) $(UTILS_TESTS))) |
| 174 |
|
| 175 |
# Build the round trip tests |
| 176 |
.PHONY: roundtrip |
| 177 |
roundtrip: EXTRA_FLAGS += -Wno-deprecated-declarations |
| 178 |
roundtrip: test/RoundTripTest$(EXT) |
| 179 |
|
| 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 |
| 185 |
|
| 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 |
| 190 |
$(LD_COMMAND) |
| 191 |
|
| 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 |
| 197 |
$(LD_COMMAND) |
| 198 |
|
| 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 |
| 202 |
$(LD_COMMAND) |
| 203 |
|
| 204 |
|
| 205 |
GTEST_CMAKEFLAGS = |
| 206 |
|
| 207 |
# Install googletest |
| 208 |
.PHONY: googletest |
| 209 |
googletest: PZSTD_CCXXFLAGS += -fPIC |
| 210 |
googletest: |
| 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) |
| 215 |
|
| 216 |
.PHONY: googletest32 |
| 217 |
googletest32: PZSTD_CCXXFLAGS += -m32 |
| 218 |
googletest32: googletest |
| 219 |
|
| 220 |
.PHONY: googletest-mingw64 |
| 221 |
googletest-mingw64: GTEST_CMAKEFLAGS += -G "MSYS Makefiles" |
| 222 |
googletest-mingw64: googletest |
| 223 |
|
| 224 |
.PHONY: clean |
| 225 |
clean: |
| 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 |
| 232 |
|
| 233 |
|
| 234 |
# Cancel implicit rules |
| 235 |
%.o: %.c |
| 236 |
%.o: %.cpp |
| 237 |
|
| 238 |
# Object file rules |
| 239 |
%.o: %.c |
| 240 |
$(CC_COMMAND) |
| 241 |
$(POSTCOMPILE) |
| 242 |
|
| 243 |
$(PROGDIR)/%.o: $(PROGDIR)/%.c |
| 244 |
$(CC_COMMAND) |
| 245 |
$(POSTCOMPILE) |
| 246 |
|
| 247 |
%.o: %.cpp |
| 248 |
$(CXX_COMMAND) |
| 249 |
$(POSTCOMPILE) |
| 250 |
|
| 251 |
test/%.o: PZSTD_CPPFLAGS += $(GTEST_INC) |
| 252 |
test/%.o: test/%.cpp |
| 253 |
$(CXX_COMMAND) |
| 254 |
$(POSTCOMPILE) |
| 255 |
|
| 256 |
utils/test/%.o: PZSTD_CPPFLAGS += $(GTEST_INC) |
| 257 |
utils/test/%.o: utils/test/%.cpp |
| 258 |
$(CXX_COMMAND) |
| 259 |
$(POSTCOMPILE) |
| 260 |
|
| 261 |
# Dependency file stuff |
| 262 |
.PRECIOUS: %.d test/%.d utils/test/%.d |
| 263 |
|
| 264 |
# Include rules that specify header file dependencies |
| 265 |
-include $(patsubst %,%.d,$(basename $(ALL_SRCS))) |