Makefile 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. # ################################################################
  2. # xxHash Makefile
  3. # Copyright (C) 2012-2021 Yann Collet
  4. #
  5. # GPL v2 License
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License along
  18. # with this program; if not, write to the Free Software Foundation, Inc.,
  19. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. #
  21. # You can contact the author at:
  22. # - xxHash homepage: https://www.xxhash.com
  23. # - xxHash source repository: https://github.com/Cyan4973/xxHash
  24. # ################################################################
  25. # xxhsum: provides 32/64 bits hash of one or multiple files, or stdin
  26. # ################################################################
  27. Q = $(if $(filter 1,$(V) $(VERBOSE)),,@)
  28. # Version numbers
  29. SED ?= sed
  30. SED_ERE_OPT ?= -E
  31. LIBVER_MAJOR_SCRIPT:=`$(SED) -n '/define XXH_VERSION_MAJOR/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < xxhash.h`
  32. LIBVER_MINOR_SCRIPT:=`$(SED) -n '/define XXH_VERSION_MINOR/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < xxhash.h`
  33. LIBVER_PATCH_SCRIPT:=`$(SED) -n '/define XXH_VERSION_RELEASE/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < xxhash.h`
  34. LIBVER_MAJOR := $(shell echo $(LIBVER_MAJOR_SCRIPT))
  35. LIBVER_MINOR := $(shell echo $(LIBVER_MINOR_SCRIPT))
  36. LIBVER_PATCH := $(shell echo $(LIBVER_PATCH_SCRIPT))
  37. LIBVER := $(LIBVER_MAJOR).$(LIBVER_MINOR).$(LIBVER_PATCH)
  38. CFLAGS ?= -O3
  39. DEBUGFLAGS+=-Wall -Wextra -Wconversion -Wcast-qual -Wcast-align -Wshadow \
  40. -Wstrict-aliasing=1 -Wswitch-enum -Wdeclaration-after-statement \
  41. -Wstrict-prototypes -Wundef -Wpointer-arith -Wformat-security \
  42. -Wvla -Wformat=2 -Winit-self -Wfloat-equal -Wwrite-strings \
  43. -Wredundant-decls -Wstrict-overflow=2
  44. CFLAGS += $(DEBUGFLAGS) $(MOREFLAGS)
  45. FLAGS = $(CFLAGS) $(CPPFLAGS)
  46. XXHSUM_VERSION = $(LIBVER)
  47. UNAME := $(shell uname)
  48. # Define *.exe as extension for Windows systems
  49. ifneq (,$(filter Windows%,$(OS)))
  50. EXT =.exe
  51. else
  52. EXT =
  53. endif
  54. ifeq ($(NODE_JS),1)
  55. # Link in unrestricted filesystem support
  56. LDFLAGS += -sNODERAWFS
  57. # Set flag to fix isatty() support
  58. CPPFLAGS += -DXSUM_NODE_JS=1
  59. endif
  60. # OS X linker doesn't support -soname, and use different extension
  61. # see: https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/DynamicLibraryDesignGuidelines.html
  62. ifeq ($(UNAME), Darwin)
  63. SHARED_EXT = dylib
  64. SHARED_EXT_MAJOR = $(LIBVER_MAJOR).$(SHARED_EXT)
  65. SHARED_EXT_VER = $(LIBVER).$(SHARED_EXT)
  66. SONAME_FLAGS = -install_name $(LIBDIR)/libxxhash.$(SHARED_EXT_MAJOR) -compatibility_version $(LIBVER_MAJOR) -current_version $(LIBVER)
  67. else
  68. SONAME_FLAGS = -Wl,-soname=libxxhash.$(SHARED_EXT).$(LIBVER_MAJOR)
  69. SHARED_EXT = so
  70. SHARED_EXT_MAJOR = $(SHARED_EXT).$(LIBVER_MAJOR)
  71. SHARED_EXT_VER = $(SHARED_EXT).$(LIBVER)
  72. endif
  73. LIBXXH = libxxhash.$(SHARED_EXT_VER)
  74. XXHSUM_SRC_DIR = cli
  75. XXHSUM_SPLIT_SRCS = $(XXHSUM_SRC_DIR)/xxhsum.c \
  76. $(XXHSUM_SRC_DIR)/xsum_os_specific.c \
  77. $(XXHSUM_SRC_DIR)/xsum_output.c \
  78. $(XXHSUM_SRC_DIR)/xsum_sanity_check.c \
  79. $(XXHSUM_SRC_DIR)/xsum_bench.c
  80. XXHSUM_SPLIT_OBJS = $(XXHSUM_SPLIT_SRCS:.c=.o)
  81. XXHSUM_HEADERS = $(XXHSUM_SRC_DIR)/xsum_config.h \
  82. $(XXHSUM_SRC_DIR)/xsum_arch.h \
  83. $(XXHSUM_SRC_DIR)/xsum_os_specific.h \
  84. $(XXHSUM_SRC_DIR)/xsum_output.h \
  85. $(XXHSUM_SRC_DIR)/xsum_sanity_check.h \
  86. $(XXHSUM_SRC_DIR)/xsum_bench.h
  87. ## generate CLI and libraries in release mode (default for `make`)
  88. .PHONY: default
  89. default: DEBUGFLAGS=
  90. default: lib xxhsum_and_links
  91. .PHONY: all
  92. all: lib xxhsum xxhsum_inlinedXXH
  93. ## xxhsum is the command line interface (CLI)
  94. ifeq ($(DISPATCH),1)
  95. xxhsum: CPPFLAGS += -DXXHSUM_DISPATCH=1
  96. xxhsum: xxh_x86dispatch.o
  97. endif
  98. xxhsum: xxhash.o $(XXHSUM_SPLIT_OBJS)
  99. $(CC) $(FLAGS) $^ $(LDFLAGS) -o $@$(EXT)
  100. xxhsum32: CFLAGS += -m32 ## generate CLI in 32-bits mode
  101. xxhsum32: xxhash.c $(XXHSUM_SPLIT_SRCS) ## do not generate object (avoid mixing different ABI)
  102. $(CC) $(FLAGS) $^ $(LDFLAGS) -o $@$(EXT)
  103. ## dispatch only works for x86/x64 systems
  104. dispatch: CPPFLAGS += -DXXHSUM_DISPATCH=1
  105. dispatch: xxhash.o xxh_x86dispatch.o $(XXHSUM_SPLIT_SRCS)
  106. $(CC) $(FLAGS) $^ $(LDFLAGS) -o $@$(EXT)
  107. xxhash.o: xxhash.c xxhash.h
  108. xxhsum.o: $(XXHSUM_SRC_DIR)/xxhsum.c $(XXHSUM_HEADERS) \
  109. xxhash.h xxh_x86dispatch.h
  110. xxh_x86dispatch.o: xxh_x86dispatch.c xxh_x86dispatch.h xxhash.h
  111. .PHONY: xxhsum_and_links
  112. xxhsum_and_links: xxhsum xxh32sum xxh64sum xxh128sum
  113. xxh32sum xxh64sum xxh128sum: xxhsum
  114. ln -sf $<$(EXT) $@$(EXT)
  115. xxhsum_inlinedXXH: CPPFLAGS += -DXXH_INLINE_ALL
  116. xxhsum_inlinedXXH: $(XXHSUM_SPLIT_SRCS)
  117. $(CC) $(FLAGS) $< -o $@$(EXT)
  118. # library
  119. libxxhash.a: ARFLAGS = rcs
  120. libxxhash.a: xxhash.o
  121. $(AR) $(ARFLAGS) $@ $^
  122. $(LIBXXH): LDFLAGS += -shared
  123. ifeq (,$(filter Windows%,$(OS)))
  124. $(LIBXXH): CFLAGS += -fPIC
  125. endif
  126. ifeq ($(DISPATCH),1)
  127. $(LIBXXH): xxh_x86dispatch.c
  128. endif
  129. $(LIBXXH): xxhash.c
  130. $(CC) $(FLAGS) $^ $(LDFLAGS) $(SONAME_FLAGS) -o $@
  131. ln -sf $@ libxxhash.$(SHARED_EXT_MAJOR)
  132. ln -sf $@ libxxhash.$(SHARED_EXT)
  133. .PHONY: libxxhash
  134. libxxhash: ## generate dynamic xxhash library
  135. libxxhash: $(LIBXXH)
  136. .PHONY: lib
  137. lib: ## generate static and dynamic xxhash libraries
  138. lib: libxxhash.a libxxhash
  139. # helper targets
  140. AWK = awk
  141. GREP = grep
  142. SORT = sort
  143. NM = nm
  144. .PHONY: list
  145. list: ## list all Makefile targets
  146. $(Q)$(MAKE) -pRrq -f $(lastword $(MAKEFILE_LIST)) : 2>/dev/null | $(AWK) -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | $(SORT) | egrep -v -e '^[^[:alnum:]]' -e '^$@$$' | xargs
  147. .PHONY: help
  148. help: ## list documented targets
  149. $(Q)$(GREP) -E '^[0-9a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \
  150. $(SORT) | \
  151. $(AWK) 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
  152. .PHONY: clean
  153. clean: ## remove all build artifacts
  154. $(Q)$(RM) -r *.dSYM # Mac OS-X specific
  155. $(Q)$(RM) core *.o *.obj *.$(SHARED_EXT) *.$(SHARED_EXT).* *.a libxxhash.pc
  156. $(Q)$(RM) xxhsum$(EXT) xxhsum32$(EXT) xxhsum_inlinedXXH$(EXT) dispatch$(EXT)
  157. $(Q)$(RM) xxhsum.wasm xxhsum.js xxhsum.html
  158. $(Q)$(RM) xxh32sum$(EXT) xxh64sum$(EXT) xxh128sum$(EXT)
  159. $(Q)$(RM) $(XXHSUM_SRC_DIR)/*.o $(XXHSUM_SRC_DIR)/*.obj
  160. $(MAKE) -C tests clean
  161. $(MAKE) -C tests/bench clean
  162. $(MAKE) -C tests/collisions clean
  163. @echo cleaning completed
  164. # =================================================
  165. # tests
  166. # =================================================
  167. # make check can be run with cross-compiled binaries on emulated environments (qemu user mode)
  168. # by setting $(RUN_ENV) to the target emulation environment
  169. .PHONY: check
  170. check: xxhsum test_sanity ## basic tests for xxhsum CLI, set RUN_ENV for emulated environments
  171. # stdin
  172. # If you get "Wrong parameters" on Emscripten+Node.js, recompile with `NODE_JS=1`
  173. $(RUN_ENV) ./xxhsum$(EXT) < xxhash.c
  174. # multiple files
  175. $(RUN_ENV) ./xxhsum$(EXT) xxhash.*
  176. # internal bench
  177. $(RUN_ENV) ./xxhsum$(EXT) -bi0
  178. # long bench command
  179. $(RUN_ENV) ./xxhsum$(EXT) --benchmark-all -i0
  180. # bench multiple variants
  181. $(RUN_ENV) ./xxhsum$(EXT) -b1,2,3 -i0
  182. # file bench
  183. $(RUN_ENV) ./xxhsum$(EXT) -bi0 xxhash.c
  184. # 32-bit
  185. $(RUN_ENV) ./xxhsum$(EXT) -H0 xxhash.c
  186. # 128-bit
  187. $(RUN_ENV) ./xxhsum$(EXT) -H2 xxhash.c
  188. # XXH3 (enforce BSD style)
  189. $(RUN_ENV) ./xxhsum$(EXT) -H3 xxhash.c | grep "XXH3"
  190. # request incorrect variant
  191. $(RUN_ENV) ./xxhsum$(EXT) -H9 xxhash.c ; test $$? -eq 1
  192. @printf "\n ....... checks completed successfully ....... \n"
  193. .PHONY: test-unicode
  194. test-unicode:
  195. $(MAKE) -C tests test_unicode
  196. .PHONY: test_sanity
  197. test_sanity:
  198. $(MAKE) -C tests test_sanity
  199. .PHONY: test-mem
  200. VALGRIND = valgrind --leak-check=yes --error-exitcode=1
  201. test-mem: RUN_ENV = $(VALGRIND)
  202. test-mem: xxhsum check
  203. .PHONY: test32
  204. test32: xxhsum32
  205. @echo ---- test 32-bit ----
  206. ./xxhsum32 -bi0 xxhash.c
  207. TEST_FILES = xxhsum$(EXT) xxhash.c xxhash.h
  208. .PHONY: test-xxhsum-c
  209. test-xxhsum-c: xxhsum
  210. # xxhsum to/from pipe
  211. ./xxhsum $(TEST_FILES) | ./xxhsum -c -
  212. ./xxhsum -H0 $(TEST_FILES) | ./xxhsum -c -
  213. # xxhsum -c is unable to verify checksum of file from STDIN (#470)
  214. ./xxhsum < README.md > .test.README.md.xxh
  215. ./xxhsum -c .test.README.md.xxh < README.md
  216. # xxhsum -q does not display "Loading" message into stderr (#251)
  217. ! ./xxhsum -q $(TEST_FILES) 2>&1 | grep Loading
  218. # xxhsum does not display "Loading" message into stderr either
  219. ! ./xxhsum $(TEST_FILES) 2>&1 | grep Loading
  220. # Check that xxhsum do display filename that it failed to open.
  221. LC_ALL=C ./xxhsum nonexistent 2>&1 | grep "Error: Could not open 'nonexistent'"
  222. # xxhsum to/from file, shell redirection
  223. ./xxhsum $(TEST_FILES) > .test.xxh64
  224. ./xxhsum --tag $(TEST_FILES) > .test.xxh64_tag
  225. ./xxhsum --little-endian $(TEST_FILES) > .test.le_xxh64
  226. ./xxhsum --tag --little-endian $(TEST_FILES) > .test.le_xxh64_tag
  227. ./xxhsum -H0 $(TEST_FILES) > .test.xxh32
  228. ./xxhsum -H0 --tag $(TEST_FILES) > .test.xxh32_tag
  229. ./xxhsum -H0 --little-endian $(TEST_FILES) > .test.le_xxh32
  230. ./xxhsum -H0 --tag --little-endian $(TEST_FILES) > .test.le_xxh32_tag
  231. ./xxhsum -H2 $(TEST_FILES) > .test.xxh128
  232. ./xxhsum -H2 --tag $(TEST_FILES) > .test.xxh128_tag
  233. ./xxhsum -H2 --little-endian $(TEST_FILES) > .test.le_xxh128
  234. ./xxhsum -H2 --tag --little-endian $(TEST_FILES) > .test.le_xxh128_tag
  235. ./xxhsum -H3 $(TEST_FILES) > .test.xxh3
  236. ./xxhsum -H3 --tag $(TEST_FILES) > .test.xxh3_tag
  237. ./xxhsum -H3 --little-endian $(TEST_FILES) > .test.le_xxh3
  238. ./xxhsum -H3 --tag --little-endian $(TEST_FILES) > .test.le_xxh3_tag
  239. ./xxhsum -c .test.xxh*
  240. ./xxhsum -c --little-endian .test.le_xxh*
  241. ./xxhsum -c .test.*_tag
  242. # read list of files from stdin
  243. ./xxhsum -c < .test.xxh32
  244. ./xxhsum -c < .test.xxh64
  245. ./xxhsum -c < .test.xxh128
  246. ./xxhsum -c < .test.xxh3
  247. cat .test.xxh* | ./xxhsum -c -
  248. # check variant with '*' marker as second separator
  249. $(SED) 's/ / \*/' .test.xxh32 | ./xxhsum -c
  250. # bsd-style output
  251. ./xxhsum --tag xxhsum* | $(GREP) XXH64
  252. ./xxhsum --tag -H0 xxhsum* | $(GREP) XXH32
  253. ./xxhsum --tag -H1 xxhsum* | $(GREP) XXH64
  254. ./xxhsum --tag -H2 xxhsum* | $(GREP) XXH128
  255. ./xxhsum --tag -H3 xxhsum* | $(GREP) XXH3
  256. ./xxhsum -H3 xxhsum* | $(GREP) XXH3 # --tag is implicit for H3
  257. ./xxhsum --tag -H32 xxhsum* | $(GREP) XXH32
  258. ./xxhsum --tag -H64 xxhsum* | $(GREP) XXH64
  259. ./xxhsum --tag -H128 xxhsum* | $(GREP) XXH128
  260. ./xxhsum --tag -H0 --little-endian xxhsum* | $(GREP) XXH32_LE
  261. ./xxhsum --tag -H1 --little-endian xxhsum* | $(GREP) XXH64_LE
  262. ./xxhsum --tag -H2 --little-endian xxhsum* | $(GREP) XXH128_LE
  263. ./xxhsum -H3 --little-endian xxhsum* | $(GREP) XXH3_LE
  264. ./xxhsum --tag -H32 --little-endian xxhsum* | $(GREP) XXH32_LE
  265. ./xxhsum --tag -H64 --little-endian xxhsum* | $(GREP) XXH64_LE
  266. ./xxhsum --tag -H128 --little-endian xxhsum* | $(GREP) XXH128_LE
  267. # check bsd-style
  268. ./xxhsum --tag xxhsum* | ./xxhsum -c
  269. ./xxhsum --tag -H32 --little-endian xxhsum* | ./xxhsum -c
  270. # xxhsum -c warns improperly format lines.
  271. echo '12345678 ' >>.test.xxh32
  272. ./xxhsum -c .test.xxh32 | $(GREP) improperly
  273. echo '123456789 file' >>.test.xxh64
  274. ./xxhsum -c .test.xxh64 | $(GREP) improperly
  275. # Expects "FAILED"
  276. echo "0000000000000000 LICENSE" | ./xxhsum -c -; test $$? -eq 1
  277. echo "00000000 LICENSE" | ./xxhsum -c -; test $$? -eq 1
  278. # Expects "FAILED open or read"
  279. echo "0000000000000000 test-expects-file-not-found" | ./xxhsum -c -; test $$? -eq 1
  280. echo "00000000 test-expects-file-not-found" | ./xxhsum -c -; test $$? -eq 1
  281. @$(RM) .test.*
  282. .PHONY: test-filename-escape
  283. test-filename-escape:
  284. $(MAKE) -C tests test_filename_escape
  285. .PHONY: test-cli-comment-line
  286. test-cli-comment-line:
  287. $(MAKE) -C tests test_cli_comment_line
  288. .PHONY: test-cli-ignore-missing
  289. test-cli-ignore-missing:
  290. $(MAKE) -C tests test_cli_ignore_missing
  291. .PHONY: armtest
  292. armtest: clean
  293. @echo ---- test ARM compilation ----
  294. CC=arm-linux-gnueabi-gcc MOREFLAGS="-Werror -static" $(MAKE) xxhsum
  295. .PHONY: clangtest
  296. clangtest: clean
  297. @echo ---- test clang compilation ----
  298. CC=clang MOREFLAGS="-Werror -Wconversion -Wno-sign-conversion" $(MAKE) all
  299. .PHONY: gcc-og-test
  300. gcc-og-test: clean
  301. @echo ---- test gcc -Og compilation ----
  302. CFLAGS="-Og -Wall -Wextra -Wundef -Wshadow -Wcast-align -Werror -fPIC" MOREFLAGS="-Werror" $(MAKE) all
  303. .PHONY: cxxtest
  304. cxxtest: clean
  305. @echo ---- test C++ compilation ----
  306. CC="$(CXX) -Wno-deprecated" $(MAKE) all CFLAGS="-O3 -Wall -Wextra -Wundef -Wshadow -Wcast-align -Werror -fPIC"
  307. .PHONY: c90test
  308. ifeq ($(NO_C90_TEST),true)
  309. c90test:
  310. @echo no c90 compatibility test
  311. else
  312. c90test: CPPFLAGS += -DXXH_NO_LONG_LONG
  313. c90test: CFLAGS += -std=c90 -Werror -pedantic
  314. c90test: xxhash.c
  315. @echo ---- test strict C90 compilation [xxh32 only] ----
  316. $(RM) xxhash.o
  317. $(CC) $(FLAGS) $^ -c
  318. $(NM) xxhash.o | $(GREP) XXH64 ; test $$? -eq 1
  319. $(RM) xxhash.o
  320. endif
  321. .PHONY: noxxh3test
  322. noxxh3test: CPPFLAGS += -DXXH_NO_XXH3
  323. noxxh3test: CFLAGS += -Werror -pedantic -Wno-long-long # XXH64 requires long long support
  324. noxxh3test: OFILE = xxh_noxxh3.o
  325. noxxh3test: xxhash.c
  326. @echo ---- test compilation without XXH3 ----
  327. $(CC) $(FLAGS) -c $^ -o $(OFILE)
  328. $(NM) $(OFILE) | $(GREP) XXH3_ ; test $$? -eq 1
  329. $(RM) $(OFILE)
  330. .PHONY: nostreamtest
  331. nostreamtest: CPPFLAGS += -DXXH_NO_STREAM
  332. nostreamtest: CFLAGS += -Werror -pedantic -Wno-long-long # XXH64 requires long long support
  333. nostreamtest: OFILE = xxh_nostream.o
  334. nostreamtest: xxhash.c
  335. @echo ---- test compilation without streaming ----
  336. $(CC) $(FLAGS) -c $^ -o $(OFILE)
  337. $(NM) $(OFILE) | $(GREP) update ; test $$? -eq 1
  338. $(RM) $(OFILE)
  339. .PHONY: nostdlibtest
  340. nostdlibtest: CPPFLAGS += -DXXH_NO_STDLIB
  341. nostdlibtest: CFLAGS += -Werror -pedantic -Wno-long-long # XXH64 requires long long support
  342. nostdlibtest: OFILE = xxh_nostdlib.o
  343. nostdlibtest: xxhash.c
  344. @echo ---- test compilation without \<stdlib.h\> ----
  345. $(CC) $(FLAGS) -c $^ -o $(OFILE)
  346. $(NM) $(OFILE) | $(GREP) "U _free\|U free" ; test $$? -eq 1
  347. $(RM) $(OFILE)
  348. .PHONY: usan
  349. usan: CC=clang
  350. usan: CXX=clang++
  351. usan: ## check CLI runtime for undefined behavior, using clang's sanitizer
  352. @echo ---- check undefined behavior - sanitize ----
  353. $(MAKE) clean
  354. $(MAKE) test CC=$(CC) CXX=$(CXX) MOREFLAGS="-g -fsanitize=undefined -fno-sanitize-recover=all"
  355. .PHONY: staticAnalyze
  356. SCANBUILD ?= scan-build
  357. staticAnalyze: clean ## check C source files using $(SCANBUILD) static analyzer
  358. @echo ---- static analyzer - $(SCANBUILD) ----
  359. CFLAGS="-g -Werror" $(SCANBUILD) --status-bugs -v $(MAKE) all
  360. CPPCHECK ?= cppcheck
  361. .PHONY: cppcheck
  362. cppcheck: ## check C source files using $(CPPCHECK) static analyzer
  363. @echo ---- static analyzer - $(CPPCHECK) ----
  364. $(CPPCHECK) . --force --enable=warning,portability,performance,style --error-exitcode=1 > /dev/null
  365. .PHONY: namespaceTest
  366. namespaceTest: ## ensure XXH_NAMESPACE redefines all public symbols
  367. $(CC) -c xxhash.c
  368. $(CC) -DXXH_NAMESPACE=TEST_ -c xxhash.c -o xxhash2.o
  369. $(CC) xxhash.o xxhash2.o $(XXHSUM_SPLIT_SRCS) -o xxhsum2 # will fail if one namespace missing (symbol collision)
  370. $(RM) *.o xxhsum2 # clean
  371. MAN = $(XXHSUM_SRC_DIR)/xxhsum.1
  372. MD2ROFF ?= ronn
  373. MD2ROFF_FLAGS ?= --roff --warnings --manual="User Commands" --organization="xxhsum $(XXHSUM_VERSION)"
  374. $(MAN): $(XXHSUM_SRC_DIR)/xxhsum.1.md xxhash.h
  375. cat $< | $(MD2ROFF) $(MD2ROFF_FLAGS) | $(SED) -n '/^\.\\\".*/!p' > $@
  376. .PHONY: man
  377. man: $(MAN) ## generate man page from markdown source
  378. .PHONY: clean-man
  379. clean-man:
  380. $(RM) xxhsum.1
  381. .PHONY: preview-man
  382. preview-man: man
  383. man ./xxhsum.1
  384. .PHONY: test
  385. test: DEBUGFLAGS += -DXXH_DEBUGLEVEL=1
  386. test: all namespaceTest check test-xxhsum-c c90test test-tools noxxh3test nostdlibtest
  387. .PHONY: test-inline
  388. test-inline:
  389. $(MAKE) -C tests test_multiInclude
  390. .PHONY: test-all
  391. test-all: CFLAGS += -Werror
  392. test-all: test test32 test-unicode clangtest gcc-og-test cxxtest usan test-inline listL120 trailingWhitespace test-xxh-nnn-sums
  393. .PHONY: test-tools
  394. test-tools:
  395. CFLAGS=-Werror $(MAKE) -C tests/bench
  396. CFLAGS=-Werror $(MAKE) -C tests/collisions
  397. .PHONY: test-xxh-nnn-sums
  398. test-xxh-nnn-sums: xxhsum_and_links
  399. ./xxhsum README.md > tmp.xxhsum.out # xxhsum outputs xxh64
  400. ./xxh32sum README.md > tmp.xxh32sum.out
  401. ./xxh64sum README.md > tmp.xxh64sum.out
  402. ./xxh128sum README.md > tmp.xxh128sum.out
  403. cat tmp.xxhsum.out
  404. cat tmp.xxh32sum.out
  405. cat tmp.xxh64sum.out
  406. cat tmp.xxh128sum.out
  407. ./xxhsum -c tmp.xxhsum.out
  408. ./xxhsum -c tmp.xxh32sum.out
  409. ./xxhsum -c tmp.xxh64sum.out
  410. ./xxhsum -c tmp.xxh128sum.out
  411. ./xxh32sum -c tmp.xxhsum.out ; test $$? -eq 1 # expects "no properly formatted"
  412. ./xxh32sum -c tmp.xxh32sum.out
  413. ./xxh32sum -c tmp.xxh64sum.out ; test $$? -eq 1 # expects "no properly formatted"
  414. ./xxh32sum -c tmp.xxh128sum.out ; test $$? -eq 1 # expects "no properly formatted"
  415. ./xxh64sum -c tmp.xxhsum.out
  416. ./xxh64sum -c tmp.xxh32sum.out ; test $$? -eq 1 # expects "no properly formatted"
  417. ./xxh64sum -c tmp.xxh64sum.out
  418. ./xxh64sum -c tmp.xxh128sum.out ; test $$? -eq 1 # expects "no properly formatted"
  419. ./xxh128sum -c tmp.xxhsum.out ; test $$? -eq 1 # expects "no properly formatted"
  420. ./xxh128sum -c tmp.xxh32sum.out ; test $$? -eq 1 # expects "no properly formatted"
  421. ./xxh128sum -c tmp.xxh64sum.out ; test $$? -eq 1 # expects "no properly formatted"
  422. ./xxh128sum -c tmp.xxh128sum.out
  423. .PHONY: listL120
  424. listL120: # extract lines >= 120 characters in *.{c,h}, by Takayuki Matsuoka (note: $$, for Makefile compatibility)
  425. find . -type f -name '*.c' -o -name '*.h' | while read -r filename; do awk 'length > 120 {print FILENAME "(" FNR "): " $$0}' $$filename; done
  426. .PHONY: trailingWhitespace
  427. trailingWhitespace:
  428. ! $(GREP) -E "`printf '[ \\t]$$'`" cli/*.c cli/*.h cli/*.1 *.c *.h LICENSE Makefile cmake_unofficial/CMakeLists.txt
  429. .PHONY: lint-unicode
  430. lint-unicode:
  431. ./tests/unicode_lint.sh
  432. # =========================================================
  433. # make install is validated only for the following targets
  434. # =========================================================
  435. ifneq (,$(filter Linux Darwin GNU/kFreeBSD GNU Haiku OpenBSD FreeBSD NetBSD DragonFly SunOS CYGWIN% , $(UNAME)))
  436. DESTDIR ?=
  437. # directory variables: GNU conventions prefer lowercase
  438. # see https://www.gnu.org/prep/standards/html_node/Makefile-Conventions.html
  439. # support both lower and uppercase (BSD), use uppercase in script
  440. prefix ?= /usr/local
  441. PREFIX ?= $(prefix)
  442. exec_prefix ?= $(PREFIX)
  443. EXEC_PREFIX ?= $(exec_prefix)
  444. libdir ?= $(EXEC_PREFIX)/lib
  445. LIBDIR ?= $(libdir)
  446. includedir ?= $(PREFIX)/include
  447. INCLUDEDIR ?= $(includedir)
  448. bindir ?= $(EXEC_PREFIX)/bin
  449. BINDIR ?= $(bindir)
  450. datarootdir ?= $(PREFIX)/share
  451. mandir ?= $(datarootdir)/man
  452. man1dir ?= $(mandir)/man1
  453. ifneq (,$(filter $(UNAME),FreeBSD NetBSD DragonFly))
  454. PKGCONFIGDIR ?= $(PREFIX)/libdata/pkgconfig
  455. else
  456. PKGCONFIGDIR ?= $(LIBDIR)/pkgconfig
  457. endif
  458. ifneq (,$(filter $(UNAME),OpenBSD FreeBSD NetBSD DragonFly SunOS))
  459. MANDIR ?= $(PREFIX)/man/man1
  460. else
  461. MANDIR ?= $(man1dir)
  462. endif
  463. ifneq (,$(filter $(UNAME),SunOS))
  464. INSTALL ?= ginstall
  465. else
  466. INSTALL ?= install
  467. endif
  468. INSTALL_PROGRAM ?= $(INSTALL)
  469. INSTALL_DATA ?= $(INSTALL) -m 644
  470. INSTALL_DIR ?= $(INSTALL) -d -m 755
  471. # Escape special symbols by putting each character into its separate class
  472. EXEC_PREFIX_REGEX ?= $(shell echo "$(EXEC_PREFIX)" | $(SED) $(SED_ERE_OPT) -e "s/([^^])/[\1]/g" -e "s/\\^/\\\\^/g")
  473. PREFIX_REGEX ?= $(shell echo "$(PREFIX)" | $(SED) $(SED_ERE_OPT) -e "s/([^^])/[\1]/g" -e "s/\\^/\\\\^/g")
  474. PCLIBDIR ?= $(shell echo "$(LIBDIR)" | $(SED) -n $(SED_ERE_OPT) -e "s@^$(EXEC_PREFIX_REGEX)(/|$$)@@p")
  475. PCINCDIR ?= $(shell echo "$(INCLUDEDIR)" | $(SED) -n $(SED_ERE_OPT) -e "s@^$(PREFIX_REGEX)(/|$$)@@p")
  476. PCEXECDIR?= $(if $(filter $(PREFIX),$(EXEC_PREFIX)),$$\{prefix\},$(EXEC_PREFIX))
  477. ifeq (,$(PCLIBDIR))
  478. # Additional prefix check is required, since the empty string is technically a
  479. # valid PCLIBDIR
  480. ifeq (,$(shell echo "$(LIBDIR)" | $(SED) -n $(SED_ERE_OPT) -e "\\@^$(EXEC_PREFIX_REGEX)(/|$$)@ p"))
  481. $(error configured libdir ($(LIBDIR)) is outside of exec_prefix ($(EXEC_PREFIX)), can't generate pkg-config file)
  482. endif
  483. endif
  484. ifeq (,$(PCINCDIR))
  485. # Additional prefix check is required, since the empty string is technically a
  486. # valid PCINCDIR
  487. ifeq (,$(shell echo "$(INCLUDEDIR)" | $(SED) -n $(SED_ERE_OPT) -e "\\@^$(PREFIX_REGEX)(/|$$)@ p"))
  488. $(error configured includedir ($(INCLUDEDIR)) is outside of prefix ($(PREFIX)), can't generate pkg-config file)
  489. endif
  490. endif
  491. libxxhash.pc: libxxhash.pc.in
  492. @echo creating pkgconfig
  493. $(Q)$(SED) $(SED_ERE_OPT) -e 's|@PREFIX@|$(PREFIX)|' \
  494. -e 's|@EXECPREFIX@|$(PCEXECDIR)|' \
  495. -e 's|@LIBDIR@|$$\{exec_prefix\}/$(PCLIBDIR)|' \
  496. -e 's|@INCLUDEDIR@|$$\{prefix\}/$(PCINCDIR)|' \
  497. -e 's|@VERSION@|$(LIBVER)|' \
  498. $< > $@
  499. install_libxxhash.a: libxxhash.a
  500. @echo Installing libxxhash.a
  501. $(Q)$(INSTALL_DIR) $(DESTDIR)$(LIBDIR)
  502. $(Q)$(INSTALL_DATA) libxxhash.a $(DESTDIR)$(LIBDIR)
  503. install_libxxhash: libxxhash
  504. @echo Installing libxxhash
  505. $(Q)$(INSTALL_DIR) $(DESTDIR)$(LIBDIR)
  506. $(Q)$(INSTALL_PROGRAM) $(LIBXXH) $(DESTDIR)$(LIBDIR)
  507. $(Q)ln -sf $(LIBXXH) $(DESTDIR)$(LIBDIR)/libxxhash.$(SHARED_EXT_MAJOR)
  508. $(Q)ln -sf $(LIBXXH) $(DESTDIR)$(LIBDIR)/libxxhash.$(SHARED_EXT)
  509. install_libxxhash.includes:
  510. $(Q)$(INSTALL) -d -m 755 $(DESTDIR)$(INCLUDEDIR) # includes
  511. $(Q)$(INSTALL_DATA) xxhash.h $(DESTDIR)$(INCLUDEDIR)
  512. $(Q)$(INSTALL_DATA) xxh3.h $(DESTDIR)$(INCLUDEDIR) # for compatibility, will be removed in v0.9.0
  513. ifeq ($(DISPATCH),1)
  514. $(Q)$(INSTALL_DATA) xxh_x86dispatch.h $(DESTDIR)$(INCLUDEDIR)
  515. endif
  516. install_libxxhash.pc: libxxhash.pc
  517. @echo Installing pkgconfig
  518. $(Q)$(INSTALL_DIR) $(DESTDIR)$(PKGCONFIGDIR)/
  519. $(Q)$(INSTALL_DATA) libxxhash.pc $(DESTDIR)$(PKGCONFIGDIR)/
  520. install_xxhsum: xxhsum
  521. @echo Installing xxhsum
  522. $(Q)$(INSTALL_DIR) $(DESTDIR)$(BINDIR)/
  523. $(Q)$(INSTALL_PROGRAM) xxhsum $(DESTDIR)$(BINDIR)/xxhsum
  524. $(Q)ln -sf xxhsum $(DESTDIR)$(BINDIR)/xxh32sum
  525. $(Q)ln -sf xxhsum $(DESTDIR)$(BINDIR)/xxh64sum
  526. $(Q)ln -sf xxhsum $(DESTDIR)$(BINDIR)/xxh128sum
  527. install_man:
  528. @echo Installing man pages
  529. $(Q)$(INSTALL_DIR) $(DESTDIR)$(MANDIR)/
  530. $(Q)$(INSTALL_DATA) $(MAN) $(DESTDIR)$(MANDIR)/xxhsum.1
  531. $(Q)ln -sf xxhsum.1 $(DESTDIR)$(MANDIR)/xxh32sum.1
  532. $(Q)ln -sf xxhsum.1 $(DESTDIR)$(MANDIR)/xxh64sum.1
  533. $(Q)ln -sf xxhsum.1 $(DESTDIR)$(MANDIR)/xxh128sum.1
  534. .PHONY: install
  535. install: install_libxxhash.a install_libxxhash install_libxxhash.includes install_libxxhash.pc install_xxhsum install_man ## install libraries, CLI, links and man page
  536. @echo xxhash installation completed
  537. .PHONY: uninstall
  538. uninstall: ## uninstall libraries, CLI, links and man page
  539. $(Q)$(RM) $(DESTDIR)$(LIBDIR)/libxxhash.a
  540. $(Q)$(RM) $(DESTDIR)$(LIBDIR)/libxxhash.$(SHARED_EXT)
  541. $(Q)$(RM) $(DESTDIR)$(LIBDIR)/libxxhash.$(SHARED_EXT_MAJOR)
  542. $(Q)$(RM) $(DESTDIR)$(LIBDIR)/$(LIBXXH)
  543. $(Q)$(RM) $(DESTDIR)$(INCLUDEDIR)/xxhash.h
  544. $(Q)$(RM) $(DESTDIR)$(INCLUDEDIR)/xxh3.h
  545. $(Q)$(RM) $(DESTDIR)$(INCLUDEDIR)/xxh_x86dispatch.h
  546. $(Q)$(RM) $(DESTDIR)$(PKGCONFIGDIR)/libxxhash.pc
  547. $(Q)$(RM) $(DESTDIR)$(BINDIR)/xxh32sum
  548. $(Q)$(RM) $(DESTDIR)$(BINDIR)/xxh64sum
  549. $(Q)$(RM) $(DESTDIR)$(BINDIR)/xxh128sum
  550. $(Q)$(RM) $(DESTDIR)$(BINDIR)/xxhsum
  551. $(Q)$(RM) $(DESTDIR)$(MANDIR)/xxh32sum.1
  552. $(Q)$(RM) $(DESTDIR)$(MANDIR)/xxh64sum.1
  553. $(Q)$(RM) $(DESTDIR)$(MANDIR)/xxh128sum.1
  554. $(Q)$(RM) $(DESTDIR)$(MANDIR)/xxhsum.1
  555. @echo xxhsum successfully uninstalled
  556. endif