Makefile 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. .PHONY: pretty clean ChangeLog.md release
  2. ##########################################################################
  3. # configuration
  4. ##########################################################################
  5. # find GNU sed to use `-i` parameter
  6. SED:=$(shell command -v gsed || which sed)
  7. ##########################################################################
  8. # source files
  9. ##########################################################################
  10. # the list of sources in the include folder
  11. SRCS=$(shell find include -type f | sort)
  12. # the list of sources in the tests folder
  13. TESTS_SRCS=$(shell find tests -type f \( -name '*.hpp' -o -name '*.cpp' -o -name '*.cu' \) -not -path 'tests/thirdparty/*' -not -path 'tests/abi/include/nlohmann/*' | sort)
  14. # the single headers (amalgamated from the source files)
  15. AMALGAMATED_FILE=single_include/nlohmann/json.hpp
  16. AMALGAMATED_FWD_FILE=single_include/nlohmann/json_fwd.hpp
  17. ##########################################################################
  18. # documentation of the Makefile's targets
  19. ##########################################################################
  20. # main target
  21. all:
  22. @echo "amalgamate - amalgamate files single_include/nlohmann/json{,_fwd}.hpp from the include/nlohmann sources"
  23. @echo "ChangeLog.md - generate ChangeLog file"
  24. @echo "check-amalgamation - check whether sources have been amalgamated"
  25. @echo "clean - remove built files"
  26. @echo "doctest - compile example files and check their output"
  27. @echo "fuzz_testing - prepare fuzz testing of the JSON parser"
  28. @echo "fuzz_testing_bson - prepare fuzz testing of the BSON parser"
  29. @echo "fuzz_testing_cbor - prepare fuzz testing of the CBOR parser"
  30. @echo "fuzz_testing_msgpack - prepare fuzz testing of the MessagePack parser"
  31. @echo "fuzz_testing_ubjson - prepare fuzz testing of the UBJSON parser"
  32. @echo "pretty - beautify code with Artistic Style"
  33. @echo "run_benchmarks - build and run benchmarks"
  34. ##########################################################################
  35. # documentation tests
  36. ##########################################################################
  37. # compile example files and check output
  38. doctest:
  39. $(MAKE) check_output -C docs
  40. ##########################################################################
  41. # benchmarks
  42. ##########################################################################
  43. run_benchmarks:
  44. rm -fr cmake-build-benchmarks
  45. mkdir cmake-build-benchmarks
  46. cd cmake-build-benchmarks ; cmake ../tests/benchmarks -GNinja -DCMAKE_BUILD_TYPE=Release
  47. cd cmake-build-benchmarks ; ninja
  48. cd cmake-build-benchmarks ; ./json_benchmarks
  49. ##########################################################################
  50. # fuzzing
  51. ##########################################################################
  52. # the overall fuzz testing target
  53. fuzz_testing:
  54. rm -fr fuzz-testing
  55. mkdir -p fuzz-testing fuzz-testing/testcases fuzz-testing/out
  56. $(MAKE) parse_afl_fuzzer -C tests CXX=afl-clang++
  57. mv tests/parse_afl_fuzzer fuzz-testing/fuzzer
  58. find tests/data/json_tests -size -5k -name *json | xargs -I{} cp "{}" fuzz-testing/testcases
  59. @echo "Execute: afl-fuzz -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer"
  60. fuzz_testing_bson:
  61. rm -fr fuzz-testing
  62. mkdir -p fuzz-testing fuzz-testing/testcases fuzz-testing/out
  63. $(MAKE) parse_bson_fuzzer -C tests CXX=afl-clang++
  64. mv tests/parse_bson_fuzzer fuzz-testing/fuzzer
  65. find tests/data -size -5k -name *.bson | xargs -I{} cp "{}" fuzz-testing/testcases
  66. @echo "Execute: afl-fuzz -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer"
  67. fuzz_testing_cbor:
  68. rm -fr fuzz-testing
  69. mkdir -p fuzz-testing fuzz-testing/testcases fuzz-testing/out
  70. $(MAKE) parse_cbor_fuzzer -C tests CXX=afl-clang++
  71. mv tests/parse_cbor_fuzzer fuzz-testing/fuzzer
  72. find tests/data -size -5k -name *.cbor | xargs -I{} cp "{}" fuzz-testing/testcases
  73. @echo "Execute: afl-fuzz -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer"
  74. fuzz_testing_msgpack:
  75. rm -fr fuzz-testing
  76. mkdir -p fuzz-testing fuzz-testing/testcases fuzz-testing/out
  77. $(MAKE) parse_msgpack_fuzzer -C tests CXX=afl-clang++
  78. mv tests/parse_msgpack_fuzzer fuzz-testing/fuzzer
  79. find tests/data -size -5k -name *.msgpack | xargs -I{} cp "{}" fuzz-testing/testcases
  80. @echo "Execute: afl-fuzz -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer"
  81. fuzz_testing_ubjson:
  82. rm -fr fuzz-testing
  83. mkdir -p fuzz-testing fuzz-testing/testcases fuzz-testing/out
  84. $(MAKE) parse_ubjson_fuzzer -C tests CXX=afl-clang++
  85. mv tests/parse_ubjson_fuzzer fuzz-testing/fuzzer
  86. find tests/data -size -5k -name *.ubjson | xargs -I{} cp "{}" fuzz-testing/testcases
  87. @echo "Execute: afl-fuzz -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer"
  88. fuzzing-start:
  89. afl-fuzz -S fuzzer1 -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer > /dev/null &
  90. afl-fuzz -S fuzzer2 -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer > /dev/null &
  91. afl-fuzz -S fuzzer3 -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer > /dev/null &
  92. afl-fuzz -S fuzzer4 -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer > /dev/null &
  93. afl-fuzz -S fuzzer5 -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer > /dev/null &
  94. afl-fuzz -S fuzzer6 -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer > /dev/null &
  95. afl-fuzz -S fuzzer7 -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer > /dev/null &
  96. afl-fuzz -M fuzzer0 -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer
  97. fuzzing-stop:
  98. -killall fuzzer
  99. -killall afl-fuzz
  100. ##########################################################################
  101. # Static analysis
  102. ##########################################################################
  103. # call PVS-Studio Analyzer <https://www.viva64.com/en/pvs-studio/>
  104. pvs_studio:
  105. rm -fr cmake-build-pvs-studio
  106. mkdir cmake-build-pvs-studio
  107. cd cmake-build-pvs-studio ; cmake .. -DCMAKE_EXPORT_COMPILE_COMMANDS=On -DJSON_MultipleHeaders=ON
  108. cd cmake-build-pvs-studio ; pvs-studio-analyzer analyze -j 10
  109. cd cmake-build-pvs-studio ; plog-converter -a'GA:1,2;64:1;CS' -t fullhtml PVS-Studio.log -o pvs
  110. open cmake-build-pvs-studio/pvs/index.html
  111. ##########################################################################
  112. # Code format and source amalgamation
  113. ##########################################################################
  114. # call the Artistic Style pretty printer on all source files
  115. pretty:
  116. astyle \
  117. --style=allman \
  118. --indent=spaces=4 \
  119. --indent-modifiers \
  120. --indent-switches \
  121. --indent-preproc-block \
  122. --indent-preproc-define \
  123. --indent-col1-comments \
  124. --pad-oper \
  125. --pad-header \
  126. --align-pointer=type \
  127. --align-reference=type \
  128. --add-brackets \
  129. --convert-tabs \
  130. --close-templates \
  131. --lineend=linux \
  132. --preserve-date \
  133. --suffix=none \
  134. --formatted \
  135. $(SRCS) $(TESTS_SRCS) $(AMALGAMATED_FILE) $(AMALGAMATED_FWD_FILE) docs/examples/*.cpp
  136. # call the Clang-Format on all source files
  137. pretty_format:
  138. for FILE in $(SRCS) $(TESTS_SRCS) $(AMALGAMATED_FILE) docs/examples/*.cpp; do echo $$FILE; clang-format -i $$FILE; done
  139. # create single header files and pretty print
  140. amalgamate: $(AMALGAMATED_FILE) $(AMALGAMATED_FWD_FILE)
  141. $(MAKE) pretty
  142. # call the amalgamation tool for json.hpp
  143. $(AMALGAMATED_FILE): $(SRCS)
  144. tools/amalgamate/amalgamate.py -c tools/amalgamate/config_json.json -s . --verbose=yes
  145. # call the amalgamation tool for json_fwd.hpp
  146. $(AMALGAMATED_FWD_FILE): $(SRCS)
  147. tools/amalgamate/amalgamate.py -c tools/amalgamate/config_json_fwd.json -s . --verbose=yes
  148. # check if file single_include/nlohmann/json.hpp has been amalgamated from the nlohmann sources
  149. # Note: this target is called by Travis
  150. check-amalgamation:
  151. @mv $(AMALGAMATED_FILE) $(AMALGAMATED_FILE)~
  152. @mv $(AMALGAMATED_FWD_FILE) $(AMALGAMATED_FWD_FILE)~
  153. @$(MAKE) amalgamate
  154. @diff $(AMALGAMATED_FILE) $(AMALGAMATED_FILE)~ || (echo "===================================================================\n Amalgamation required! Please read the contribution guidelines\n in file .github/CONTRIBUTING.md.\n===================================================================" ; mv $(AMALGAMATED_FILE)~ $(AMALGAMATED_FILE) ; false)
  155. @diff $(AMALGAMATED_FWD_FILE) $(AMALGAMATED_FWD_FILE)~ || (echo "===================================================================\n Amalgamation required! Please read the contribution guidelines\n in file .github/CONTRIBUTING.md.\n===================================================================" ; mv $(AMALGAMATED_FWD_FILE)~ $(AMALGAMATED_FWD_FILE) ; false)
  156. @mv $(AMALGAMATED_FILE)~ $(AMALGAMATED_FILE)
  157. @mv $(AMALGAMATED_FWD_FILE)~ $(AMALGAMATED_FWD_FILE)
  158. ##########################################################################
  159. # ChangeLog
  160. ##########################################################################
  161. # Create a ChangeLog based on the git log using the GitHub Changelog Generator
  162. # (<https://github.com/github-changelog-generator/github-changelog-generator>).
  163. # variable to control the diffs between the last released version and the current repository state
  164. NEXT_VERSION ?= "unreleased"
  165. ChangeLog.md:
  166. github_changelog_generator -o ChangeLog.md --user nlohmann --project json --simple-list --release-url https://github.com/nlohmann/json/releases/tag/%s --future-release $(NEXT_VERSION)
  167. $(SED) -i 's|https://github.com/nlohmann/json/releases/tag/HEAD|https://github.com/nlohmann/json/tree/HEAD|' ChangeLog.md
  168. $(SED) -i '2i All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/).' ChangeLog.md
  169. ##########################################################################
  170. # Release files
  171. ##########################################################################
  172. # Create a tar.gz archive that contains sufficient files to be used as CMake project (e.g., using FetchContent). The
  173. # archive is created according to the advices of <https://reproducible-builds.org/docs/archives/>.
  174. json.tar.xz:
  175. mkdir json
  176. rsync -R $(shell find LICENSE.MIT nlohmann_json.natvis CMakeLists.txt cmake/*.in include single_include -type f) json
  177. gtar --sort=name --mtime="@$(shell git log -1 --pretty=%ct)" --owner=0 --group=0 --numeric-owner --pax-option=exthdr.name=%d/PaxHeaders/%f,delete=atime,delete=ctime --create --file - json | xz --compress -9e --threads=2 - > json.tar.xz
  178. rm -fr json
  179. # We use `-X` to make the resulting ZIP file reproducible, see
  180. # <https://content.pivotal.io/blog/barriers-to-deterministic-reproducible-zip-files>.
  181. include.zip:
  182. zip -9 --recurse-paths -X include.zip $(SRCS) $(AMALGAMATED_FILE) meson.build LICENSE.MIT
  183. # Create the files for a release and add signatures and hashes.
  184. release: include.zip json.tar.xz
  185. rm -fr release_files
  186. mkdir release_files
  187. gpg --armor --detach-sig include.zip
  188. gpg --armor --detach-sig $(AMALGAMATED_FILE)
  189. gpg --armor --detach-sig $(AMALGAMATED_FWD_FILE)
  190. gpg --armor --detach-sig json.tar.xz
  191. cp $(AMALGAMATED_FILE) release_files
  192. cp $(AMALGAMATED_FWD_FILE) release_files
  193. mv $(AMALGAMATED_FILE).asc $(AMALGAMATED_FWD_FILE).asc json.tar.xz json.tar.xz.asc include.zip include.zip.asc release_files
  194. cd release_files ; shasum -a 256 json.hpp include.zip json.tar.xz > hashes.txt
  195. ##########################################################################
  196. # Maintenance
  197. ##########################################################################
  198. # clean up
  199. clean:
  200. rm -fr fuzz fuzz-testing *.dSYM tests/*.dSYM
  201. rm -fr benchmarks/files/numbers/*.json
  202. rm -fr cmake-build-benchmarks fuzz-testing cmake-build-pvs-studio release_files
  203. $(MAKE) clean -Cdocs
  204. ##########################################################################
  205. # Thirdparty code
  206. ##########################################################################
  207. update_hedley:
  208. rm -f include/nlohmann/thirdparty/hedley/hedley.hpp include/nlohmann/thirdparty/hedley/hedley_undef.hpp
  209. curl https://raw.githubusercontent.com/nemequ/hedley/master/hedley.h -o include/nlohmann/thirdparty/hedley/hedley.hpp
  210. $(SED) -i 's/HEDLEY_/JSON_HEDLEY_/g' include/nlohmann/thirdparty/hedley/hedley.hpp
  211. grep "[[:blank:]]*#[[:blank:]]*undef" include/nlohmann/thirdparty/hedley/hedley.hpp | grep -v "__" | sort | uniq | $(SED) 's/ //g' | $(SED) 's/undef/undef /g' > include/nlohmann/thirdparty/hedley/hedley_undef.hpp
  212. $(SED) -i '1s/^/#pragma once\n\n/' include/nlohmann/thirdparty/hedley/hedley.hpp
  213. $(SED) -i '1s/^/#pragma once\n\n/' include/nlohmann/thirdparty/hedley/hedley_undef.hpp
  214. $(MAKE) amalgamate
  215. ##########################################################################
  216. # serve_header.py
  217. ##########################################################################
  218. serve_header:
  219. ./tools/serve_header/serve_header.py --make $(MAKE)
  220. ##########################################################################
  221. # REUSE
  222. ##########################################################################
  223. reuse:
  224. pipx run reuse addheader --recursive single_include include -tjson --license MIT --copyright "Niels Lohmann <https://nlohmann.me>" --year "2013-2022"
  225. pipx run reuse addheader $(TESTS_SRCS) --style=c -tjson_support --license MIT --copyright "Niels Lohmann <https://nlohmann.me>" --year "2013-2022"
  226. pipx run reuse lint