1.PHONY: pretty clean ChangeLog.md release
2
3##########################################################################
4# configuration
5##########################################################################
6
7# directory to recent compiler binaries
8COMPILER_DIR=/usr/local/opt/llvm/bin
9
10# find GNU sed to use `-i` parameter
11SED:=$(shell command -v gsed || which sed)
12
13
14##########################################################################
15# source files
16##########################################################################
17
18# the list of sources in the include folder
19SRCS=$(shell find include -type f | sort)
20
21# the single header (amalgamated from the source files)
22AMALGAMATED_FILE=single_include/nlohmann/json.hpp
23
24
25##########################################################################
26# documentation of the Makefile's targets
27##########################################################################
28
29# main target
30all:
31	@echo "amalgamate - amalgamate file single_include/nlohmann/json.hpp from the include/nlohmann sources"
32	@echo "ChangeLog.md - generate ChangeLog file"
33	@echo "check-amalgamation - check whether sources have been amalgamated"
34	@echo "clean - remove built files"
35	@echo "coverage - create coverage information with lcov"
36	@echo "cppcheck - analyze code with cppcheck"
37	@echo "cpplint - analyze code with cpplint"
38	@echo "clang_tidy - analyze code with Clang-Tidy"
39	@echo "clang_analyze - analyze code with Clang-Analyzer"
40	@echo "doctest - compile example files and check their output"
41	@echo "fuzz_testing - prepare fuzz testing of the JSON parser"
42	@echo "fuzz_testing_bson - prepare fuzz testing of the BSON parser"
43	@echo "fuzz_testing_cbor - prepare fuzz testing of the CBOR parser"
44	@echo "fuzz_testing_msgpack - prepare fuzz testing of the MessagePack parser"
45	@echo "fuzz_testing_ubjson - prepare fuzz testing of the UBJSON parser"
46	@echo "pedantic_clang - run Clang with maximal warning flags"
47	@echo "pedantic_gcc - run GCC with maximal warning flags"
48	@echo "pretty - beautify code with Artistic Style"
49	@echo "run_benchmarks - build and run benchmarks"
50
51
52##########################################################################
53# coverage
54##########################################################################
55
56coverage:
57	rm -fr cmake-build-coverage
58	mkdir cmake-build-coverage
59	cd cmake-build-coverage ; cmake .. -GNinja -DCMAKE_BUILD_TYPE=Debug -DJSON_Coverage=ON -DJSON_MultipleHeaders=ON
60	cd cmake-build-coverage ; ninja
61	cd cmake-build-coverage ; ctest -j10
62	cd cmake-build-coverage ; ninja lcov_html
63	open cmake-build-coverage/test/html/index.html
64
65##########################################################################
66# documentation tests
67##########################################################################
68
69# compile example files and check output
70doctest:
71	$(MAKE) check_output -C doc
72
73
74##########################################################################
75# warning detector
76##########################################################################
77
78# calling Clang with all warnings, except:
79# -Wno-c++2a-compat: u8 literals will behave differently in C++20...
80# -Wno-deprecated-declarations: the library deprecated some functions
81# -Wno-documentation-unknown-command: code uses user-defined commands like @complexity
82# -Wno-exit-time-destructors: warning in json code triggered by NLOHMANN_JSON_SERIALIZE_ENUM
83# -Wno-float-equal: not all comparisons in the tests can be replaced by Approx
84# -Wno-keyword-macro: unit-tests use "#define private public"
85# -Wno-missing-prototypes: for NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE
86# -Wno-padded: padding is nothing to warn about
87# -Wno-range-loop-analysis: items tests "for(const auto i...)"
88# -Wno-switch-enum -Wno-covered-switch-default: pedantic/contradicting warnings about switches
89# -Wno-weak-vtables: exception class is defined inline, but has virtual method
90pedantic_clang:
91	rm -fr cmake-build-pedantic
92	CXXFLAGS=" \
93		-std=c++11 -Wno-c++98-compat -Wno-c++98-compat-pedantic \
94		-Werror \
95		-Weverything \
96		-Wno-c++2a-compat \
97		-Wno-deprecated-declarations \
98		-Wno-documentation-unknown-command \
99		-Wno-exit-time-destructors \
100		-Wno-float-equal \
101		-Wno-keyword-macro \
102		-Wno-missing-prototypes \
103		-Wno-padded \
104		-Wno-range-loop-analysis \
105		-Wno-switch-enum -Wno-covered-switch-default \
106		-Wno-weak-vtables" cmake -S . -B cmake-build-pedantic -GNinja -DCMAKE_BUILD_TYPE=Debug -DJSON_MultipleHeaders=ON -DJSON_BuildTests=On
107	cmake --build cmake-build-pedantic
108
109# calling GCC with most warnings
110pedantic_gcc:
111	rm -fr cmake-build-pedantic
112	CXXFLAGS=" \
113		-std=c++11 \
114		-pedantic \
115		-Werror \
116		--all-warnings                                    \
117		--extra-warnings                                  \
118		-W                                                \
119		-Wno-abi-tag                                         \
120		-Waddress                                         \
121		-Waddress-of-packed-member                        \
122		-Wno-aggregate-return                                \
123		-Waggressive-loop-optimizations                   \
124		-Waligned-new=all                                 \
125		-Wall                                             \
126		-Walloc-zero                                      \
127		-Walloca                                          \
128		-Wanalyzer-double-fclose                          \
129		-Wanalyzer-double-free                            \
130		-Wanalyzer-exposure-through-output-file           \
131		-Wanalyzer-file-leak                              \
132		-Wanalyzer-free-of-non-heap                       \
133		-Wanalyzer-malloc-leak                            \
134		-Wanalyzer-null-argument                          \
135		-Wanalyzer-null-dereference                       \
136		-Wanalyzer-possible-null-argument                 \
137		-Wanalyzer-possible-null-dereference              \
138		-Wanalyzer-stale-setjmp-buffer                    \
139		-Wanalyzer-tainted-array-index                    \
140		-Wanalyzer-too-complex                            \
141		-Wanalyzer-unsafe-call-within-signal-handler      \
142		-Wanalyzer-use-after-free                         \
143		-Wanalyzer-use-of-pointer-in-stale-stack-frame    \
144		-Warith-conversion                                \
145		-Warray-bounds                                    \
146		-Warray-bounds=2                                  \
147		-Wattribute-alias=2                               \
148		-Wattribute-warning                               \
149		-Wattributes                                      \
150		-Wbool-compare                                    \
151		-Wbool-operation                                  \
152		-Wbuiltin-declaration-mismatch                    \
153		-Wbuiltin-macro-redefined                         \
154		-Wc++0x-compat                                    \
155		-Wc++11-compat                                    \
156		-Wc++14-compat                                    \
157		-Wc++17-compat                                    \
158		-Wc++1z-compat                                    \
159		-Wc++20-compat                                    \
160		-Wc++2a-compat                                    \
161		-Wcannot-profile                                  \
162		-Wcast-align                                      \
163		-Wcast-align=strict                               \
164		-Wcast-function-type                              \
165		-Wcast-qual                                       \
166		-Wcatch-value=3                                   \
167		-Wchar-subscripts                                 \
168		-Wclass-conversion                                \
169		-Wclass-memaccess                                 \
170		-Wclobbered                                       \
171		-Wcomma-subscript                                 \
172		-Wcomment                                         \
173		-Wcomments                                        \
174		-Wconditionally-supported                         \
175		-Wconversion                                      \
176		-Wconversion-null                                 \
177		-Wcoverage-mismatch                               \
178		-Wcpp                                             \
179		-Wctor-dtor-privacy                               \
180		-Wdangling-else                                   \
181		-Wdate-time                                       \
182		-Wdelete-incomplete                               \
183		-Wdelete-non-virtual-dtor                         \
184		-Wdeprecated                                      \
185		-Wdeprecated-copy                                 \
186		-Wdeprecated-copy-dtor                            \
187		-Wdeprecated-declarations                         \
188		-Wdisabled-optimization                           \
189		-Wdiv-by-zero                                     \
190		-Wdouble-promotion                                \
191		-Wduplicated-branches                             \
192		-Wduplicated-cond                                 \
193		-Weffc++                                          \
194		-Wempty-body                                      \
195		-Wendif-labels                                    \
196		-Wenum-compare                                    \
197		-Wexpansion-to-defined                            \
198		-Wextra                                           \
199		-Wextra-semi                                      \
200		-Wfloat-conversion                                \
201		-Wfloat-equal                                     \
202		-Wformat -Wformat-contains-nul                    \
203		-Wformat -Wformat-extra-args                      \
204		-Wformat -Wformat-nonliteral                      \
205		-Wformat -Wformat-security                        \
206		-Wformat -Wformat-y2k                             \
207		-Wformat -Wformat-zero-length                     \
208		-Wformat-diag                                     \
209		-Wformat-overflow=2                               \
210		-Wformat-signedness                               \
211		-Wformat-truncation=2                             \
212		-Wformat=2                                        \
213		-Wframe-address                                   \
214		-Wfree-nonheap-object                             \
215		-Whsa                                             \
216		-Wif-not-aligned                                  \
217		-Wignored-attributes                              \
218		-Wignored-qualifiers                              \
219		-Wimplicit-fallthrough=5                          \
220		-Winaccessible-base                               \
221		-Winherited-variadic-ctor                         \
222		-Winit-list-lifetime                              \
223		-Winit-self                                       \
224		-Winline                                          \
225		-Wint-in-bool-context                             \
226		-Wint-to-pointer-cast                             \
227		-Winvalid-memory-model                            \
228		-Winvalid-offsetof                                \
229		-Winvalid-pch                                     \
230		-Wliteral-suffix                                  \
231		-Wlogical-not-parentheses                         \
232		-Wlogical-op                                      \
233		-Wno-long-long                                       \
234		-Wlto-type-mismatch                               \
235		-Wmain                                            \
236		-Wmaybe-uninitialized                             \
237		-Wmemset-elt-size                                 \
238		-Wmemset-transposed-args                          \
239		-Wmisleading-indentation                          \
240		-Wmismatched-tags                                 \
241		-Wmissing-attributes                              \
242		-Wmissing-braces                                  \
243		-Wno-missing-declarations                            \
244		-Wmissing-field-initializers                      \
245		-Wmissing-include-dirs                            \
246		-Wmissing-profile                                 \
247		-Wmultichar                                       \
248		-Wmultiple-inheritance                            \
249		-Wmultistatement-macros                           \
250		-Wno-namespaces                                      \
251		-Wnarrowing                                       \
252		-Wno-noexcept                                        \
253		-Wnoexcept-type                                   \
254		-Wnon-template-friend                             \
255		-Wnon-virtual-dtor                                \
256		-Wnonnull                                         \
257		-Wnonnull-compare                                 \
258		-Wnonportable-cfstrings                           \
259		-Wnormalized=nfkc                                 \
260		-Wnull-dereference                                \
261		-Wodr                                             \
262		-Wold-style-cast                                  \
263		-Wopenmp-simd                                     \
264		-Woverflow                                        \
265		-Woverlength-strings                              \
266		-Woverloaded-virtual                              \
267		-Wpacked                                          \
268		-Wpacked-bitfield-compat                          \
269		-Wpacked-not-aligned                              \
270		-Wno-padded                                          \
271		-Wparentheses                                     \
272		-Wpedantic                                        \
273		-Wpessimizing-move                                \
274		-Wplacement-new=2                                 \
275		-Wpmf-conversions                                 \
276		-Wpointer-arith                                   \
277		-Wpointer-compare                                 \
278		-Wpragmas                                         \
279		-Wprio-ctor-dtor                                  \
280		-Wpsabi                                           \
281		-Wredundant-decls                                 \
282		-Wredundant-move                                  \
283		-Wredundant-tags                                  \
284		-Wregister                                        \
285		-Wreorder                                         \
286		-Wrestrict                                        \
287		-Wreturn-local-addr                               \
288		-Wreturn-type                                     \
289		-Wscalar-storage-order                            \
290		-Wsequence-point                                  \
291		-Wshadow=compatible-local                         \
292		-Wshadow=global                                   \
293		-Wshadow=local                                    \
294		-Wshift-count-negative                            \
295		-Wshift-count-overflow                            \
296		-Wshift-negative-value                            \
297		-Wshift-overflow=2                                \
298		-Wsign-compare                                    \
299		-Wsign-conversion                                 \
300		-Wsign-promo                                      \
301		-Wsized-deallocation                              \
302		-Wsizeof-array-argument                           \
303		-Wsizeof-pointer-div                              \
304		-Wsizeof-pointer-memaccess                        \
305		-Wstack-protector                                 \
306		-Wstrict-aliasing                                 \
307		-Wstrict-aliasing=3                               \
308		-Wstrict-null-sentinel                            \
309		-Wstrict-overflow                                 \
310		-Wstrict-overflow=5                               \
311		-Wstring-compare                                  \
312		-Wstringop-overflow                               \
313		-Wstringop-overflow=4                             \
314		-Wstringop-truncation                             \
315		-Wsubobject-linkage                               \
316		-Wsuggest-attribute=cold                          \
317		-Wsuggest-attribute=const                         \
318		-Wsuggest-attribute=format                        \
319		-Wsuggest-attribute=malloc                        \
320		-Wsuggest-attribute=noreturn                      \
321		-Wsuggest-attribute=pure                          \
322		-Wsuggest-final-methods                           \
323		-Wsuggest-final-types                             \
324		-Wsuggest-override                                \
325		-Wswitch                                          \
326		-Wswitch-bool                                     \
327		-Wswitch-default                                  \
328		-Wno-switch-enum                                     \
329		-Wswitch-outside-range                            \
330		-Wswitch-unreachable                              \
331		-Wsync-nand                                       \
332		-Wsynth                                           \
333		-Wno-system-headers                                  \
334		-Wtautological-compare                            \
335		-Wno-templates                                       \
336		-Wterminate                                       \
337		-Wtrampolines                                     \
338		-Wtrigraphs                                       \
339		-Wtype-limits                                     \
340		-Wundef                                           \
341		-Wuninitialized                                   \
342		-Wunknown-pragmas                                 \
343		-Wunreachable-code                                \
344		-Wunsafe-loop-optimizations                       \
345		-Wunused                                          \
346		-Wunused-but-set-parameter                        \
347		-Wunused-but-set-variable                         \
348		-Wunused-const-variable=2                         \
349		-Wunused-function                                 \
350		-Wunused-label                                    \
351		-Wno-unused-local-typedefs                           \
352		-Wunused-macros                                   \
353		-Wunused-parameter                                \
354		-Wunused-result                                   \
355		-Wunused-value                                    \
356		-Wunused-variable                                 \
357		-Wuseless-cast                                    \
358		-Wvarargs                                         \
359		-Wvariadic-macros                                 \
360		-Wvector-operation-performance                    \
361		-Wvirtual-inheritance                             \
362		-Wvirtual-move-assign                             \
363		-Wvla                                             \
364		-Wvolatile                                        \
365		-Wvolatile-register-var                           \
366		-Wwrite-strings                                   \
367		-Wzero-as-null-pointer-constant                   \
368		-Wzero-length-bounds                              \
369		" cmake -S . -B cmake-build-pedantic -GNinja -DCMAKE_BUILD_TYPE=Debug -DJSON_MultipleHeaders=ON -DJSON_BuildTests=On
370	cmake --build cmake-build-pedantic
371
372##########################################################################
373# benchmarks
374##########################################################################
375
376run_benchmarks:
377	rm -fr cmake-build-benchmarks
378	mkdir cmake-build-benchmarks
379	cd cmake-build-benchmarks ; cmake ../benchmarks -GNinja -DCMAKE_BUILD_TYPE=Release -DJSON_BuildTests=On
380	cd cmake-build-benchmarks ; ninja
381	cd cmake-build-benchmarks ; ./json_benchmarks
382
383##########################################################################
384# fuzzing
385##########################################################################
386
387# the overall fuzz testing target
388fuzz_testing:
389	rm -fr fuzz-testing
390	mkdir -p fuzz-testing fuzz-testing/testcases fuzz-testing/out
391	$(MAKE) parse_afl_fuzzer -C test CXX=afl-clang++
392	mv test/parse_afl_fuzzer fuzz-testing/fuzzer
393	find test/data/json_tests -size -5k -name *json | xargs -I{} cp "{}" fuzz-testing/testcases
394	@echo "Execute: afl-fuzz -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer"
395
396fuzz_testing_bson:
397	rm -fr fuzz-testing
398	mkdir -p fuzz-testing fuzz-testing/testcases fuzz-testing/out
399	$(MAKE) parse_bson_fuzzer -C test CXX=afl-clang++
400	mv test/parse_bson_fuzzer fuzz-testing/fuzzer
401	find test/data -size -5k -name *.bson | xargs -I{} cp "{}" fuzz-testing/testcases
402	@echo "Execute: afl-fuzz -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer"
403
404fuzz_testing_cbor:
405	rm -fr fuzz-testing
406	mkdir -p fuzz-testing fuzz-testing/testcases fuzz-testing/out
407	$(MAKE) parse_cbor_fuzzer -C test CXX=afl-clang++
408	mv test/parse_cbor_fuzzer fuzz-testing/fuzzer
409	find test/data -size -5k -name *.cbor | xargs -I{} cp "{}" fuzz-testing/testcases
410	@echo "Execute: afl-fuzz -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer"
411
412fuzz_testing_msgpack:
413	rm -fr fuzz-testing
414	mkdir -p fuzz-testing fuzz-testing/testcases fuzz-testing/out
415	$(MAKE) parse_msgpack_fuzzer -C test CXX=afl-clang++
416	mv test/parse_msgpack_fuzzer fuzz-testing/fuzzer
417	find test/data -size -5k -name *.msgpack | xargs -I{} cp "{}" fuzz-testing/testcases
418	@echo "Execute: afl-fuzz -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer"
419
420fuzz_testing_ubjson:
421	rm -fr fuzz-testing
422	mkdir -p fuzz-testing fuzz-testing/testcases fuzz-testing/out
423	$(MAKE) parse_ubjson_fuzzer -C test CXX=afl-clang++
424	mv test/parse_ubjson_fuzzer fuzz-testing/fuzzer
425	find test/data -size -5k -name *.ubjson | xargs -I{} cp "{}" fuzz-testing/testcases
426	@echo "Execute: afl-fuzz -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer"
427
428fuzzing-start:
429	afl-fuzz -S fuzzer1 -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer > /dev/null &
430	afl-fuzz -S fuzzer2 -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer > /dev/null &
431	afl-fuzz -S fuzzer3 -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer > /dev/null &
432	afl-fuzz -S fuzzer4 -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer > /dev/null &
433	afl-fuzz -S fuzzer5 -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer > /dev/null &
434	afl-fuzz -S fuzzer6 -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer > /dev/null &
435	afl-fuzz -S fuzzer7 -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer > /dev/null &
436	afl-fuzz -M fuzzer0 -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer
437
438fuzzing-stop:
439	-killall fuzzer
440	-killall afl-fuzz
441
442
443##########################################################################
444# Static analysis
445##########################################################################
446
447# call cppcheck <http://cppcheck.sourceforge.net>
448# Note: this target is called by Travis
449cppcheck:
450	cppcheck --enable=warning --inline-suppr --inconclusive --force --std=c++11 $(AMALGAMATED_FILE) --error-exitcode=1
451
452# call Clang Static Analyzer <https://clang-analyzer.llvm.org>
453clang_analyze:
454	rm -fr cmake-build-clang-analyze
455	mkdir cmake-build-clang-analyze
456	cd cmake-build-clang-analyze ; CCC_CXX=$(COMPILER_DIR)/clang++ CXX=$(COMPILER_DIR)/clang++ $(COMPILER_DIR)/scan-build cmake .. -GNinja -DJSON_BuildTests=On
457	cd cmake-build-clang-analyze ; \
458		$(COMPILER_DIR)/scan-build \
459			-enable-checker alpha.core.BoolAssignment,alpha.core.CallAndMessageUnInitRefArg,alpha.core.CastSize,alpha.core.CastToStruct,alpha.core.Conversion,alpha.core.DynamicTypeChecker,alpha.core.FixedAddr,alpha.core.PointerArithm,alpha.core.PointerSub,alpha.core.SizeofPtr,alpha.core.StackAddressAsyncEscape,alpha.core.TestAfterDivZero,alpha.deadcode.UnreachableCode,core.builtin.BuiltinFunctions,core.builtin.NoReturnFunctions,core.CallAndMessage,core.DivideZero,core.DynamicTypePropagation,core.NonnilStringConstants,core.NonNullParamChecker,core.NullDereference,core.StackAddressEscape,core.UndefinedBinaryOperatorResult,core.uninitialized.ArraySubscript,core.uninitialized.Assign,core.uninitialized.Branch,core.uninitialized.CapturedBlockVariable,core.uninitialized.UndefReturn,core.VLASize,cplusplus.InnerPointer,cplusplus.Move,cplusplus.NewDelete,cplusplus.NewDeleteLeaks,cplusplus.SelfAssignment,deadcode.DeadStores,nullability.NullableDereferenced,nullability.NullablePassedToNonnull,nullability.NullableReturnedFromNonnull,nullability.NullPassedToNonnull,nullability.NullReturnedFromNonnull \
460			--use-c++=$(COMPILER_DIR)/clang++ -analyze-headers -o report ninja
461	open cmake-build-clang-analyze/report/*/index.html
462
463# call cpplint <https://github.com/cpplint/cpplint>
464# Note: some errors expected due to false positives
465cpplint:
466	third_party/cpplint/cpplint.py \
467		--filter=-whitespace,-legal,-readability/alt_tokens,-runtime/references,-runtime/explicit \
468		--quiet --recursive $(SRCS)
469
470# call Clang-Tidy <https://clang.llvm.org/extra/clang-tidy/>
471clang_tidy:
472	$(COMPILER_DIR)/clang-tidy $(SRCS) -- -Iinclude -std=c++11
473
474# call PVS-Studio Analyzer <https://www.viva64.com/en/pvs-studio/>
475pvs_studio:
476	rm -fr cmake-build-pvs-studio
477	mkdir cmake-build-pvs-studio
478	cd cmake-build-pvs-studio ; cmake .. -DCMAKE_EXPORT_COMPILE_COMMANDS=On -DJSON_MultipleHeaders=ON
479	cd cmake-build-pvs-studio ; pvs-studio-analyzer analyze -j 10
480	cd cmake-build-pvs-studio ; plog-converter -a'GA:1,2;64:1;CS' -t fullhtml PVS-Studio.log -o pvs
481	open cmake-build-pvs-studio/pvs/index.html
482
483# call Infer <https://fbinfer.com> static analyzer
484infer:
485	rm -fr cmake-build-infer
486	mkdir cmake-build-infer
487	cd cmake-build-infer ; infer compile -- cmake .. -DJSON_MultipleHeaders=ON ; infer run -- make -j 4
488
489# call OCLint <http://oclint.org> static analyzer
490oclint:
491	oclint $(SRCS) -report-type html -enable-global-analysis -o oclint_report.html -max-priority-1=10000 -max-priority-2=10000 -max-priority-3=10000 -- -std=c++11 -Iinclude
492	open oclint_report.html
493
494# execute the test suite with Clang sanitizers (address and undefined behavior)
495clang_sanitize:
496	rm -fr cmake-build-clang-sanitize
497	mkdir cmake-build-clang-sanitize
498	cd cmake-build-clang-sanitize ; CXX=$(COMPILER_DIR)/clang++ cmake .. -DJSON_Sanitizer=On -DJSON_MultipleHeaders=ON -DJSON_BuildTests=On -GNinja
499	cd cmake-build-clang-sanitize ; ninja
500	cd cmake-build-clang-sanitize ; ctest -j10
501
502
503##########################################################################
504# Code format and source amalgamation
505##########################################################################
506
507# call the Artistic Style pretty printer on all source files
508pretty:
509	astyle \
510		--style=allman \
511		--indent=spaces=4 \
512		--indent-modifiers \
513	    --indent-switches \
514	    --indent-preproc-block \
515	    --indent-preproc-define \
516	    --indent-col1-comments \
517	    --pad-oper \
518	    --pad-header \
519	    --align-pointer=type \
520	    --align-reference=type \
521	    --add-brackets \
522	    --convert-tabs \
523	    --close-templates \
524	    --lineend=linux \
525	    --preserve-date \
526	    --suffix=none \
527	    --formatted \
528	   $(SRCS) $(AMALGAMATED_FILE) test/src/*.cpp test/src/*.hpp benchmarks/src/benchmarks.cpp doc/examples/*.cpp
529
530# call the Clang-Format on all source files
531pretty_format:
532	for FILE in $(SRCS) $(AMALGAMATED_FILE) test/src/*.cpp test/src/*.hpp benchmarks/src/benchmarks.cpp doc/examples/*.cpp; do echo $$FILE; clang-format -i $$FILE; done
533
534# create single header file
535amalgamate: $(AMALGAMATED_FILE)
536
537# call the amalgamation tool and pretty print
538$(AMALGAMATED_FILE): $(SRCS)
539	third_party/amalgamate/amalgamate.py -c third_party/amalgamate/config.json -s . --verbose=yes
540	$(MAKE) pretty
541
542# check if file single_include/nlohmann/json.hpp has been amalgamated from the nlohmann sources
543# Note: this target is called by Travis
544check-amalgamation:
545	@mv $(AMALGAMATED_FILE) $(AMALGAMATED_FILE)~
546	@$(MAKE) amalgamate
547	@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)
548	@mv $(AMALGAMATED_FILE)~ $(AMALGAMATED_FILE)
549
550# check if every header in nlohmann includes sufficient headers to be compiled individually
551check-single-includes:
552	@for x in $(SRCS); do \
553	  echo "Checking self-sufficiency of $$x..." ; \
554	  echo "#include <$$x>\nint main() {}\n" | $(SED) 's|include/||' > single_include_test.cpp; \
555	  $(CXX) $(CXXFLAGS) -Iinclude -std=c++11 single_include_test.cpp -o single_include_test; \
556	  rm -f single_include_test.cpp single_include_test; \
557	done
558
559
560##########################################################################
561# CMake
562##########################################################################
563
564# grep "^option" CMakeLists.txt test/CMakeLists.txt | $(SED) 's/(/ /' | awk '{print $2}' | xargs
565
566# check if all flags of our CMake files work
567check_cmake_flags_do:
568	$(CMAKE_BINARY) --version
569	for flag in JSON_BuildTests JSON_Install JSON_MultipleHeaders JSON_Sanitizer JSON_Valgrind JSON_NoExceptions JSON_Coverage; do \
570		rm -fr cmake_build; \
571		mkdir cmake_build; \
572		echo "\n\n$(CMAKE_BINARY) .. -D$$flag=On\n" ; \
573		cd cmake_build ; \
574		$(CMAKE_BINARY) -Werror=dev .. -D$$flag=On -DCMAKE_CXX_COMPILE_FEATURES="cxx_std_11;cxx_range_for" -DCMAKE_CXX_FLAGS="-std=gnu++11" ; \
575		test -f Makefile || exit 1 ; \
576		cd .. ; \
577	done;
578
579# call target `check_cmake_flags_do` twice: once for minimal required CMake version 3.1.0 and once for the installed version
580check_cmake_flags:
581	wget https://github.com/Kitware/CMake/releases/download/v3.1.0/cmake-3.1.0-Darwin64.tar.gz
582	tar xfz cmake-3.1.0-Darwin64.tar.gz
583	CMAKE_BINARY=$(abspath cmake-3.1.0-Darwin64/CMake.app/Contents/bin/cmake) $(MAKE) check_cmake_flags_do
584	CMAKE_BINARY=$(shell which cmake) $(MAKE) check_cmake_flags_do
585
586
587##########################################################################
588# ChangeLog
589##########################################################################
590
591# Create a ChangeLog based on the git log using the GitHub Changelog Generator
592# (<https://github.com/github-changelog-generator/github-changelog-generator>).
593
594# variable to control the diffs between the last released version and the current repository state
595NEXT_VERSION ?= "unreleased"
596
597ChangeLog.md:
598	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)
599	$(SED) -i 's|https://github.com/nlohmann/json/releases/tag/HEAD|https://github.com/nlohmann/json/tree/HEAD|' ChangeLog.md
600	$(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
601
602
603##########################################################################
604# Release files
605##########################################################################
606
607# Create the files for a release and add signatures and hashes. We use `-X` to make the resulting ZIP file
608# reproducible, see <https://content.pivotal.io/blog/barriers-to-deterministic-reproducible-zip-files>.
609
610release:
611	rm -fr release_files
612	mkdir release_files
613	zip -9 --recurse-paths -X include.zip $(SRCS) $(AMALGAMATED_FILE) meson.build
614	gpg --armor --detach-sig include.zip
615	mv include.zip include.zip.asc release_files
616	gpg --armor --detach-sig $(AMALGAMATED_FILE)
617	cp $(AMALGAMATED_FILE) release_files
618	mv $(AMALGAMATED_FILE).asc release_files
619	cd release_files ; shasum -a 256 json.hpp > hashes.txt
620	cd release_files ; shasum -a 256 include.zip >> hashes.txt
621
622
623##########################################################################
624# Maintenance
625##########################################################################
626
627# clean up
628clean:
629	rm -fr json_unit json_benchmarks fuzz fuzz-testing *.dSYM test/*.dSYM oclint_report.html
630	rm -fr benchmarks/files/numbers/*.json
631	rm -fr cmake-3.1.0-Darwin64.tar.gz cmake-3.1.0-Darwin64
632	rm -fr cmake-build-coverage cmake-build-benchmarks fuzz-testing cmake-build-clang-analyze cmake-build-pvs-studio cmake-build-infer cmake-build-clang-sanitize cmake_build
633	$(MAKE) clean -Cdoc
634
635##########################################################################
636# Thirdparty code
637##########################################################################
638
639update_hedley:
640	rm -f include/nlohmann/thirdparty/hedley/hedley.hpp include/nlohmann/thirdparty/hedley/hedley_undef.hpp
641	curl https://raw.githubusercontent.com/nemequ/hedley/master/hedley.h -o include/nlohmann/thirdparty/hedley/hedley.hpp
642	$(SED) -i 's/HEDLEY_/JSON_HEDLEY_/g' include/nlohmann/thirdparty/hedley/hedley.hpp
643	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
644	$(MAKE) amalgamate
645