xref: /freebsd/share/mk/bsd.clang-analyze.mk (revision 8ef24a0d4b28fe230e20637f56869cc4148cd2ca)
1# $FreeBSD$
2#
3# Support Clang static analyzer on SRCS.
4#
5#
6# +++ variables +++
7#
8# CLANG_ANALYZE_CHECKERS	Which checkers to run for all sources.
9#
10# CLANG_ANALYZE_CXX_CHECKERS	Which checkers to run for C++ sources.
11#
12# CLANG_ANALYZE_OUTPUT		Output format for generated files.
13# 				text - don't generate extra files.
14# 				html - generate html in obj.plist/ directories.
15# 				plist - generate xml obj.plist files.
16# 				See also:
17# 				  contrib/llvm/tools/clang/include/clang/StaticAnalyzer/Core/Analyses.def
18#
19# CLANG_ANALYZE_OUTPUT_DIR	Sets which directory output set by
20# 				CLANG_ANALYZE_OUTPUT is placed into.
21#
22# +++ targets +++
23#
24#	analyze:
25#		Run the Clang static analyzer against all sources and present
26#		output on stdout.
27
28.if !target(__<bsd.clang-analyze.mk>__)
29__<bsd.clang-analyze.mk>__:
30
31.include <bsd.compiler.mk>
32
33.if ${COMPILER_TYPE} != "clang" && (make(analyze) || make(*.clang-analyzer))
34.error Clang static analyzer requires clang but found that compiler '${CC}' is ${COMPILER_TYPE}
35.endif
36
37CLANG_ANALYZE_OUTPUT?=	text
38CLANG_ANALYZE_OUTPUT_DIR?=	clang-analyze
39CLANG_ANALYZE_FLAGS+=	--analyze \
40			-Xanalyzer -analyzer-output=${CLANG_ANALYZE_OUTPUT} \
41			-o ${CLANG_ANALYZE_OUTPUT_DIR}
42
43CLANG_ANALYZE_CHECKERS+=	core deadcode security unix
44CLANG_ANALYZE_CXX_CHECKERS+=	cplusplus
45
46.for checker in ${CLANG_ANALYZE_CHECKERS}
47CLANG_ANALYZE_FLAGS+=	-Xanalyzer -analyzer-checker=${checker}
48.endfor
49CLANG_ANALYZE_CXX_FLAGS+=	${CLANG_ANALYZE_FLAGS}
50.for checker in ${CLANG_ANALYZE_CXX_CHECKERS}
51CLANG_ANALYZE_CXX_FLAGS+=	-Xanalyzer -analyzer-checker=${checker}
52.endfor
53
54.SUFFIXES: .c .cc .cpp .cxx .C .clang-analyzer
55
56CLANG_ANALYZE_CFLAGS=	${CFLAGS:N-Wa,--fatal-warnings}
57CLANG_ANALYZE_CXXFLAGS=	${CXXFLAGS:N-Wa,--fatal-warnings}
58
59.c.clang-analyzer:
60	${CC:N${CCACHE_BIN}} ${CLANG_ANALYZE_FLAGS} \
61	    ${CLANG_ANALYZE_CFLAGS} \
62	    ${.IMPSRC}
63.cc.clang-analyzer .cpp.clang-analyzer .cxx.clang-analyzer .C.clang-analyzer:
64	${CXX:N${CCACHE_BIN}} ${CLANG_ANALYZE_CXX_FLAGS} \
65	    ${CLANG_ANALYZE_CXXFLAGS} \
66	    ${.IMPSRC}
67
68CLANG_ANALYZE_SRCS= \
69	${SRCS:M*.[cC]} ${SRCS:M*.cc} \
70	${SRCS:M*.cpp} ${SRCS:M*.cxx} \
71	${DPSRCS:M*.[cC]} ${DPSRCS:M*.cc} \
72	${DPSRCS:M*.cpp} ${DPSRCS:M*.cxx}
73.if !empty(CLANG_ANALYZE_SRCS)
74CLANG_ANALYZE_OBJS=	${CLANG_ANALYZE_SRCS:O:u:R:S,$,.clang-analyzer,}
75.NOPATH:	${CLANG_ANALYZE_OBJS}
76.endif
77
78# .depend files aren't relevant here since they reference obj.o rather than
79# obj.clang-analyzer, so add in some guesses in case 'make depend' wasn't ran,
80# for when directly building 'obj.clang-analyzer'.
81.for __obj in ${CLANG_ANALYZE_OBJS}
82${__obj}: ${OBJS_DEPEND_GUESS}
83${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
84.endfor
85
86beforeanalyze: depend .PHONY
87.if !defined(_RECURSING_PROGS) && !empty(CLANG_ANALYZE_SRCS) && \
88    ${CLANG_ANALYZE_OUTPUT} != "text"
89	mkdir -p ${CLANG_ANALYZE_OUTPUT_DIR}
90.endif
91
92.if !target(analyze)
93analyze: beforeanalyze .WAIT ${CLANG_ANALYZE_OBJS}
94.endif
95
96.if exists(${CLANG_ANALYZE_OUTPUT_DIR})
97CLEANDIRS+=	${CLANG_ANALYZE_OUTPUT_DIR}
98.endif
99
100.endif	# !target(__<bsd.clang-analyze.mk>__)
101