xref: /freebsd/share/mk/bsd.compiler.mk (revision 4f0a4502a1f33fef287ac558c98e5ef99a32216f)
1# $FreeBSD$
2
3# Setup variables for the compiler
4#
5# COMPILER_TYPE is the major type of compiler. Currently gcc and clang support
6# automatic detection. Other compiler types can be shoe-horned in, but require
7# explicit setting of the compiler type. The compiler type can also be set
8# explicitly if, say, you install gcc as clang...
9#
10# COMPILER_VERSION is a numeric constant equal to:
11#     major * 10000 + minor * 100 + tiny
12# It too can be overriden on the command line. When testing it, be sure to
13# make sure that you are limiting the test to a specific compiler. Testing
14# against 30300 for gcc likely isn't  what you wanted (since versions of gcc
15# prior to 4.2 likely have no prayer of working).
16#
17# COMPILER_FEATURES will contain one or more of the following, based on
18# compiler support for that feature:
19#
20# - c++11 : supports full (or nearly full) C++11 programming environment.
21#
22# This file may be included multiple times, but only has effect the first time.
23#
24
25.if !target(__<bsd.compiler.mk>__)
26__<bsd.compiler.mk>__:
27
28.include <bsd.opts.mk>
29
30# Handle ccache after CC is determined, but not if CC/CXX are already
31# overridden with a manual setup.
32.if ${MK_CCACHE_BUILD:Uno} == "yes" && \
33    !make(showconfig) && \
34    (${CC:M*ccache/world/*} == "" || ${CXX:M*ccache/world/*} == "")
35# CC is always prepended with the ccache wrapper rather than modifying
36# PATH since it is more clear that ccache is used and avoids wasting time
37# for mkdep/linking/asm builds.
38LOCALBASE?=		/usr/local
39CCACHE_WRAPPER_PATH?=	${LOCALBASE}/libexec/ccache
40CCACHE_BIN?=		${LOCALBASE}/bin/ccache
41.if exists(${CCACHE_BIN})
42# Export to ensure sub-makes can filter it out for mkdep/linking and
43# to chain down into kernel build which won't include this file.
44.export CCACHE_BIN
45# Expand and export some variables so they may be based on make vars.
46# This allows doing something like the following in the environment:
47# CCACHE_BASEDIR='${SRCTOP:H}' MAKEOBJDIRPREFIX='${SRCTOP:H}/obj'
48.for var in CCACHE_LOGFILE CCACHE_BASEDIR
49.if defined(${var})
50${var}:=	${${var}}
51.export		${var}
52.endif
53.endfor
54# Handle bootstrapped compiler changes properly by hashing their content
55# rather than checking mtime.  For external compilers it should be safe
56# to use the more optimal mtime check.
57# XXX: CCACHE_COMPILERCHECK= string:<compiler_version, compiler_build_rev, compiler_patch_rev, compiler_default_target, compiler_default_sysroot>
58.if ${CC:N${CCACHE_BIN}:[1]:M/*} == ""
59CCACHE_COMPILERCHECK?=	content
60.else
61CCACHE_COMPILERCHECK?=	mtime
62.endif
63.export CCACHE_COMPILERCHECK
64# Remove ccache from the PATH to prevent double calls and wasted CPP/LD time.
65PATH:=	${PATH:C,:?${CCACHE_WRAPPER_PATH}(/world)?(:$)?,,g}
66# Ensure no bogus CCACHE_PATH leaks in which might avoid the in-tree compiler.
67.if !empty(CCACHE_PATH)
68CCACHE_PATH=
69.export CCACHE_PATH
70.endif
71# Override various toolchain vars.
72.for var in CC CXX HOST_CC HOST_CXX
73.if defined(${var}) && ${${var}:M${CCACHE_BIN}} == ""
74${var}:=	${CCACHE_BIN} ${${var}}
75.endif
76.endfor
77# GCC does not need the CCACHE_CPP2 hack enabled by default in devel/ccache.
78# The port enables it due to ccache passing preprocessed C to clang
79# which fails with -Wparentheses-equality, -Wtautological-compare, and
80# -Wself-assign on macro-expanded lines.
81.if defined(COMPILER_TYPE) && ${COMPILER_TYPE} == "gcc"
82CCACHE_NOCPP2=	1
83.export CCACHE_NOCPP2
84.endif
85# Canonicalize CCACHE_DIR for meta mode usage.
86.if defined(CCACHE_DIR) && empty(.MAKE.META.IGNORE_PATHS:M${CCACHE_DIR})
87CCACHE_DIR:=	${CCACHE_DIR:tA}
88.MAKE.META.IGNORE_PATHS+= ${CCACHE_DIR}
89.export CCACHE_DIR
90.endif
91ccache-print-options: .PHONY
92	@${CCACHE_BIN} -p
93.endif	# exists(${CCACHE_BIN})
94.endif	# ${MK_CCACHE_BUILD} == "yes"
95
96# Try to import COMPILER_TYPE and COMPILER_VERSION from parent make.
97# The value is only used/exported for the same environment that impacts
98# CC and COMPILER_* settings here.
99_exported_vars=	COMPILER_TYPE COMPILER_VERSION
100_cc_hash=	${CC}${MACHINE}${PATH}
101_cc_hash:=	${_cc_hash:hash}
102# Only import if none of the vars are set somehow else.
103_can_export=	yes
104.for var in ${_exported_vars}
105.if defined(${var})
106_can_export=	no
107.endif
108.endfor
109.if ${_can_export} == yes
110.for var in ${_exported_vars}
111.if defined(${var}.${_cc_hash})
112${var}=	${${var}.${_cc_hash}}
113.endif
114.endfor
115.endif
116
117.if ${MACHINE} == "common"
118# common is a pseudo machine for architecture independent
119# generated files - thus there is no compiler.
120COMPILER_TYPE= none
121COMPILER_VERSION= 0
122.elif !defined(COMPILER_TYPE) || !defined(COMPILER_VERSION)
123_v!=	${CC} --version || echo 0.0.0
124
125.if !defined(COMPILER_TYPE)
126. if ${CC:T:M*gcc*}
127COMPILER_TYPE:=	gcc
128. elif ${CC:T:M*clang*}
129COMPILER_TYPE:=	clang
130. elif ${_v:Mgcc}
131COMPILER_TYPE:=	gcc
132. elif ${_v:M\(GCC\)}
133COMPILER_TYPE:=	gcc
134. elif ${_v:Mclang}
135COMPILER_TYPE:=	clang
136. else
137.error Unable to determine compiler type for ${CC}.  Consider setting COMPILER_TYPE.
138. endif
139.endif
140.if !defined(COMPILER_VERSION)
141COMPILER_VERSION!=echo "${_v:M[1-9].[0-9]*}" | awk -F. '{print $$1 * 10000 + $$2 * 100 + $$3;}'
142.endif
143.undef _v
144.endif
145
146# Export the values so sub-makes don't have to look them up again, using the
147# hash key computed above.
148.for var in ${_exported_vars}
149${var}.${_cc_hash}:=	${${var}}
150.export-env ${var}.${_cc_hash}
151.undef ${var}.${_cc_hash}
152.endfor
153
154.if ${COMPILER_TYPE} == "clang" || \
155	(${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 40800)
156COMPILER_FEATURES=	c++11
157.else
158COMPILER_FEATURES=
159.endif
160
161.endif	# !target(__<bsd.compiler.mk>__)
162