Makefile 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. # ################################################################
  2. # Copyright (c) 2016-present, Facebook, Inc.
  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. # Standard variables for installation
  10. DESTDIR ?=
  11. PREFIX ?= /usr/local
  12. BINDIR := $(DESTDIR)$(PREFIX)/bin
  13. ZSTDDIR = ../../lib
  14. PROGDIR = ../../programs
  15. # External program to use to run tests, e.g. qemu or valgrind
  16. TESTPROG ?=
  17. # Flags to pass to the tests
  18. TESTFLAGS ?=
  19. # We use gcc/clang to generate the header dependencies of files
  20. DEPFLAGS = -MMD -MP -MF $*.Td
  21. POSTCOMPILE = mv -f $*.Td $*.d
  22. # CFLAGS, CXXFLAGS, CPPFLAGS, and LDFLAGS are for the users to override
  23. CFLAGS ?= -O3 -Wall -Wextra
  24. CXXFLAGS ?= -O3 -Wall -Wextra -pedantic
  25. CPPFLAGS ?=
  26. LDFLAGS ?=
  27. # PZstd uses legacy APIs
  28. CFLAGS += -Wno-deprecated-declarations
  29. # Include flags
  30. PZSTD_INC = -I$(ZSTDDIR) -I$(ZSTDDIR)/common -I$(PROGDIR) -I.
  31. GTEST_INC = -isystem googletest/googletest/include
  32. PZSTD_CPPFLAGS = $(PZSTD_INC)
  33. PZSTD_CCXXFLAGS =
  34. PZSTD_CFLAGS = $(PZSTD_CCXXFLAGS)
  35. PZSTD_CXXFLAGS = $(PZSTD_CCXXFLAGS) -std=c++11
  36. PZSTD_LDFLAGS =
  37. EXTRA_FLAGS =
  38. ALL_CFLAGS = $(EXTRA_FLAGS) $(CPPFLAGS) $(PZSTD_CPPFLAGS) $(CFLAGS) $(PZSTD_CFLAGS)
  39. ALL_CXXFLAGS = $(EXTRA_FLAGS) $(CPPFLAGS) $(PZSTD_CPPFLAGS) $(CXXFLAGS) $(PZSTD_CXXFLAGS)
  40. ALL_LDFLAGS = $(EXTRA_FLAGS) $(CXXFLAGS) $(LDFLAGS) $(PZSTD_LDFLAGS)
  41. # gtest libraries need to go before "-lpthread" because they depend on it.
  42. GTEST_LIB = -L googletest/build/googlemock/gtest
  43. LIBS =
  44. # Compilation commands
  45. LD_COMMAND = $(CXX) $^ $(ALL_LDFLAGS) $(LIBS) -pthread -o $@
  46. CC_COMMAND = $(CC) $(DEPFLAGS) $(ALL_CFLAGS) -c $< -o $@
  47. CXX_COMMAND = $(CXX) $(DEPFLAGS) $(ALL_CXXFLAGS) -c $< -o $@
  48. # List all the pzstd source files so we can determine their dependencies
  49. PZSTD_SRCS := $(wildcard *.cpp)
  50. PZSTD_TESTS := $(wildcard test/*.cpp)
  51. UTILS_TESTS := $(wildcard utils/test/*.cpp)
  52. ALL_SRCS := $(PZSTD_SRCS) $(PZSTD_TESTS) $(UTILS_TESTS)
  53. # Define *.exe as extension for Windows systems
  54. ifneq (,$(filter Windows%,$(OS)))
  55. EXT =.exe
  56. else
  57. EXT =
  58. endif
  59. # Standard targets
  60. .PHONY: default
  61. default: all
  62. .PHONY: test-pzstd
  63. test-pzstd: TESTFLAGS=--gtest_filter=-*ExtremelyLarge*
  64. test-pzstd: clean googletest pzstd tests check
  65. .PHONY: test-pzstd32
  66. test-pzstd32: clean googletest32 all32 check
  67. .PHONY: test-pzstd-tsan
  68. test-pzstd-tsan: LDFLAGS=-fuse-ld=gold
  69. test-pzstd-tsan: TESTFLAGS=--gtest_filter=-*ExtremelyLarge*
  70. test-pzstd-tsan: clean googletest tsan check
  71. .PHONY: test-pzstd-asan
  72. test-pzstd-asan: LDFLAGS=-fuse-ld=gold
  73. test-pzstd-asan: TESTFLAGS=--gtest_filter=-*ExtremelyLarge*
  74. test-pzstd-asan: clean asan check
  75. .PHONY: check
  76. check:
  77. $(TESTPROG) ./utils/test/BufferTest$(EXT) $(TESTFLAGS)
  78. $(TESTPROG) ./utils/test/RangeTest$(EXT) $(TESTFLAGS)
  79. $(TESTPROG) ./utils/test/ResourcePoolTest$(EXT) $(TESTFLAGS)
  80. $(TESTPROG) ./utils/test/ScopeGuardTest$(EXT) $(TESTFLAGS)
  81. $(TESTPROG) ./utils/test/ThreadPoolTest$(EXT) $(TESTFLAGS)
  82. $(TESTPROG) ./utils/test/WorkQueueTest$(EXT) $(TESTFLAGS)
  83. $(TESTPROG) ./test/OptionsTest$(EXT) $(TESTFLAGS)
  84. $(TESTPROG) ./test/PzstdTest$(EXT) $(TESTFLAGS)
  85. .PHONY: install
  86. install: PZSTD_CPPFLAGS += -DNDEBUG
  87. install: pzstd$(EXT)
  88. install -d -m 755 $(BINDIR)/
  89. install -m 755 pzstd$(EXT) $(BINDIR)/pzstd$(EXT)
  90. .PHONY: uninstall
  91. uninstall:
  92. $(RM) $(BINDIR)/pzstd$(EXT)
  93. # Targets for many different builds
  94. .PHONY: all
  95. all: PZSTD_CPPFLAGS += -DNDEBUG
  96. all: pzstd$(EXT)
  97. .PHONY: debug
  98. debug: EXTRA_FLAGS += -g
  99. debug: pzstd$(EXT) tests roundtrip
  100. .PHONY: tsan
  101. tsan: PZSTD_CCXXFLAGS += -fsanitize=thread -fPIC
  102. tsan: PZSTD_LDFLAGS += -fsanitize=thread
  103. tsan: debug
  104. .PHONY: asan
  105. asan: EXTRA_FLAGS += -fsanitize=address
  106. asan: debug
  107. .PHONY: ubsan
  108. ubsan: EXTRA_FLAGS += -fsanitize=undefined
  109. ubsan: debug
  110. .PHONY: all32
  111. all32: EXTRA_FLAGS += -m32
  112. all32: all tests roundtrip
  113. .PHONY: debug32
  114. debug32: EXTRA_FLAGS += -m32
  115. debug32: debug
  116. .PHONY: asan32
  117. asan32: EXTRA_FLAGS += -m32
  118. asan32: asan
  119. .PHONY: tsan32
  120. tsan32: EXTRA_FLAGS += -m32
  121. tsan32: tsan
  122. .PHONY: ubsan32
  123. ubsan32: EXTRA_FLAGS += -m32
  124. ubsan32: ubsan
  125. # Run long round trip tests
  126. .PHONY: roundtripcheck
  127. roundtripcheck: roundtrip check
  128. $(TESTPROG) ./test/RoundTripTest$(EXT) $(TESTFLAGS)
  129. # Build the main binary
  130. pzstd$(EXT): main.o $(PROGDIR)/util.o Options.o Pzstd.o SkippableFrame.o $(ZSTDDIR)/libzstd.a
  131. $(LD_COMMAND)
  132. # Target that depends on all the tests
  133. .PHONY: tests
  134. tests: EXTRA_FLAGS += -Wno-deprecated-declarations
  135. tests: $(patsubst %,%$(EXT),$(basename $(PZSTD_TESTS) $(UTILS_TESTS)))
  136. # Build the round trip tests
  137. .PHONY: roundtrip
  138. roundtrip: EXTRA_FLAGS += -Wno-deprecated-declarations
  139. roundtrip: test/RoundTripTest$(EXT)
  140. # Use the static library that zstd builds for simplicity and
  141. # so we get the compiler options correct
  142. .PHONY: $(ZSTDDIR)/libzstd.a
  143. $(ZSTDDIR)/libzstd.a:
  144. CFLAGS="$(ALL_CFLAGS)" LDFLAGS="$(ALL_LDFLAGS)" $(MAKE) -C $(ZSTDDIR) libzstd.a
  145. # Rules to build the tests
  146. test/RoundTripTest$(EXT): test/RoundTripTest.o $(PROGDIR)/datagen.o \
  147. $(PROGDIR)/util.o Options.o \
  148. Pzstd.o SkippableFrame.o $(ZSTDDIR)/libzstd.a
  149. $(LD_COMMAND)
  150. test/%Test$(EXT): PZSTD_LDFLAGS += $(GTEST_LIB)
  151. test/%Test$(EXT): LIBS += -lgtest -lgtest_main
  152. test/%Test$(EXT): test/%Test.o $(PROGDIR)/datagen.o \
  153. $(PROGDIR)/util.o Options.o Pzstd.o \
  154. SkippableFrame.o $(ZSTDDIR)/libzstd.a
  155. $(LD_COMMAND)
  156. utils/test/%Test$(EXT): PZSTD_LDFLAGS += $(GTEST_LIB)
  157. utils/test/%Test$(EXT): LIBS += -lgtest -lgtest_main
  158. utils/test/%Test$(EXT): utils/test/%Test.o
  159. $(LD_COMMAND)
  160. GTEST_CMAKEFLAGS =
  161. # Install googletest
  162. .PHONY: googletest
  163. googletest: PZSTD_CCXXFLAGS += -fPIC
  164. googletest:
  165. @$(RM) -rf googletest
  166. @git clone https://github.com/google/googletest
  167. @mkdir -p googletest/build
  168. @cd googletest/build && cmake $(GTEST_CMAKEFLAGS) -DCMAKE_CXX_FLAGS="$(ALL_CXXFLAGS)" .. && $(MAKE)
  169. .PHONY: googletest32
  170. googletest32: PZSTD_CCXXFLAGS += -m32
  171. googletest32: googletest
  172. .PHONY: googletest-mingw64
  173. googletest-mingw64: GTEST_CMAKEFLAGS += -G "MSYS Makefiles"
  174. googletest-mingw64: googletest
  175. .PHONY: clean
  176. clean:
  177. $(RM) -f *.o pzstd$(EXT) *.Td *.d
  178. $(RM) -f test/*.o test/*Test$(EXT) test/*.Td test/*.d
  179. $(RM) -f utils/test/*.o utils/test/*Test$(EXT) utils/test/*.Td utils/test/*.d
  180. $(RM) -f $(PROGDIR)/*.o $(PROGDIR)/*.Td $(PROGDIR)/*.d
  181. $(MAKE) -C $(ZSTDDIR) clean
  182. @echo Cleaning completed
  183. # Cancel implicit rules
  184. %.o: %.c
  185. %.o: %.cpp
  186. # Object file rules
  187. %.o: %.c
  188. $(CC_COMMAND)
  189. $(POSTCOMPILE)
  190. $(PROGDIR)/%.o: $(PROGDIR)/%.c
  191. $(CC_COMMAND)
  192. $(POSTCOMPILE)
  193. %.o: %.cpp
  194. $(CXX_COMMAND)
  195. $(POSTCOMPILE)
  196. test/%.o: PZSTD_CPPFLAGS += $(GTEST_INC)
  197. test/%.o: test/%.cpp
  198. $(CXX_COMMAND)
  199. $(POSTCOMPILE)
  200. utils/test/%.o: PZSTD_CPPFLAGS += $(GTEST_INC)
  201. utils/test/%.o: utils/test/%.cpp
  202. $(CXX_COMMAND)
  203. $(POSTCOMPILE)
  204. # Dependency file stuff
  205. .PRECIOUS: %.d test/%.d utils/test/%.d
  206. # Include rules that specify header file dependencies
  207. -include $(patsubst %,%.d,$(basename $(ALL_SRCS)))