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 overridden 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_FREEBSD_VERSION is the compiler's __FreeBSD_cc_version value.
18#
19# COMPILER_FEATURES will contain one or more of the following, based on
20# compiler support for that feature:
21#
22# - c++17:     supports full (or nearly full) C++17 programming environment.
23# - c++14:     supports full (or nearly full) C++14 programming environment.
24# - c++11:     supports full (or nearly full) C++11 programming environment.
25# - retpoline: supports the retpoline speculative execution vulnerability
26#              mitigation.
27#
28# These variables with an X_ prefix will also be provided if XCC is set.
29#
30# This file may be included multiple times, but only has effect the first time.
31#
32
33.if !target(__<bsd.compiler.mk>__)
34__<bsd.compiler.mk>__:
35
36.include <bsd.opts.mk>
37
38# command = /usr/local/bin/ccache cc ...
39# wrapper = /usr/local/libexec/ccache/cc ...
40CCACHE_BUILD_TYPE?=	command
41# Handle ccache after CC is determined, but not if CC/CXX are already
42# overridden with a manual setup.
43.if ${MK_CCACHE_BUILD:Uno} == "yes" && \
44    !make(test-system-*) && !make(print-dir) && !make(showconfig) && \
45    (${CC:M*ccache/world/*} == "" || ${CXX:M*ccache/world/*} == "")
46# CC is always prepended with the ccache wrapper rather than modifying
47# PATH since it is more clear that ccache is used and avoids wasting time
48# for mkdep/linking/asm builds.
49LOCALBASE?=		/usr/local
50CCACHE_WRAPPER_PATH?=	${LOCALBASE}/libexec/ccache
51CCACHE_BIN?=		${LOCALBASE}/bin/ccache
52.if exists(${CCACHE_BIN})
53# Export to ensure sub-makes can filter it out for mkdep/linking and
54# to chain down into kernel build which won't include this file.
55.export CCACHE_BIN
56# Expand and export some variables so they may be based on make vars.
57# This allows doing something like the following in the environment:
58# CCACHE_BASEDIR='${SRCTOP:H}' MAKEOBJDIRPREFIX='${SRCTOP:H}/obj'
59.for var in CCACHE_LOGFILE CCACHE_BASEDIR
60.if defined(${var})
61${var}:=	${${var}}
62.export		${var}
63.endif
64.endfor
65# Handle bootstrapped compiler changes properly by hashing their content
66# rather than checking mtime.  For external compilers it should be safe
67# to use the more optimal mtime check.
68# XXX: CCACHE_COMPILERCHECK= string:<compiler_version, compiler_build_rev, compiler_patch_rev, compiler_default_target, compiler_default_sysroot>
69.if ${CC:N${CCACHE_BIN}:[1]:M/*} == ""
70CCACHE_COMPILERCHECK?=	content
71.else
72CCACHE_COMPILERCHECK?=	mtime
73.endif
74.export CCACHE_COMPILERCHECK
75# Ensure no bogus CCACHE_PATH leaks in which might avoid the in-tree compiler.
76.if !empty(CCACHE_PATH)
77CCACHE_PATH=
78.export CCACHE_PATH
79.endif
80.if ${CCACHE_BUILD_TYPE} == "command"
81# Remove ccache from the PATH to prevent double calls and wasted CPP/LD time.
82PATH:=	${PATH:C,:?${CCACHE_WRAPPER_PATH}(/world)?(:$)?,,g}
83# Override various toolchain vars.
84.for var in CC CXX HOST_CC HOST_CXX
85.if defined(${var}) && ${${var}:M${CCACHE_BIN}} == ""
86${var}:=	${CCACHE_BIN} ${${var}}
87.endif
88.endfor
89.else
90# Need to ensure CCACHE_WRAPPER_PATH is the first in ${PATH}
91PATH:=	${PATH:C,:?${CCACHE_WRAPPER_PATH}(/world)?(:$)?,,g}
92PATH:=	${CCACHE_WRAPPER_PATH}:${PATH}
93CCACHE_WRAPPER_PATH_PFX=	${CCACHE_WRAPPER_PATH}:
94.endif	# ${CCACHE_BUILD_TYPE} == "command"
95# GCC does not need the CCACHE_CPP2 hack enabled by default in devel/ccache.
96# The port enables it due to ccache passing preprocessed C to clang
97# which fails with -Wparentheses-equality, -Wtautological-compare, and
98# -Wself-assign on macro-expanded lines.
99.if defined(COMPILER_TYPE) && ${COMPILER_TYPE} == "gcc"
100CCACHE_NOCPP2=	1
101.export CCACHE_NOCPP2
102.endif
103# Canonicalize CCACHE_DIR for meta mode usage.
104.if !defined(CCACHE_DIR)
105CCACHE_DIR!=	${CCACHE_BIN} -p | awk '$$2 == "cache_dir" {print $$4}'
106.export CCACHE_DIR
107.endif
108.if !empty(CCACHE_DIR) && empty(.MAKE.META.IGNORE_PATHS:M${CCACHE_DIR})
109CCACHE_DIR:=	${CCACHE_DIR:tA}
110.MAKE.META.IGNORE_PATHS+= ${CCACHE_DIR}
111.export CCACHE_DIR
112.endif
113# ccache doesn't affect build output so let it slide for meta mode
114# comparisons.
115.MAKE.META.IGNORE_PATHS+= ${CCACHE_BIN}
116ccache-print-options: .PHONY
117	@${CCACHE_BIN} -p
118.endif	# exists(${CCACHE_BIN})
119.endif	# ${MK_CCACHE_BUILD} == "yes"
120
121.for cc X_ in CC $${_empty_var_} XCC X_
122.if ${cc} == "CC" || !empty(XCC)
123# Try to import COMPILER_TYPE and COMPILER_VERSION from parent make.
124# The value is only used/exported for the same environment that impacts
125# CC and COMPILER_* settings here.
126_exported_vars=	${X_}COMPILER_TYPE ${X_}COMPILER_VERSION \
127		${X_}COMPILER_FREEBSD_VERSION
128${X_}_cc_hash=	${${cc}}${MACHINE}${PATH}
129${X_}_cc_hash:=	${${X_}_cc_hash:hash}
130# Only import if none of the vars are set somehow else.
131_can_export=	yes
132.for var in ${_exported_vars}
133.if defined(${var})
134_can_export=	no
135.endif
136.endfor
137.if ${_can_export} == yes
138.for var in ${_exported_vars}
139.if defined(${var}.${${X_}_cc_hash})
140${var}=	${${var}.${${X_}_cc_hash}}
141.endif
142.endfor
143.endif
144
145.if ${cc} == "CC" || (${cc} == "XCC" && ${XCC} != ${CC})
146.if ${MACHINE} == "common"
147# common is a pseudo machine for architecture independent
148# generated files - thus there is no compiler.
149${X_}COMPILER_TYPE= none
150${X_}COMPILER_VERSION= 0
151${X_}COMPILER_FREEBSD_VERSION= 0
152.elif !defined(${X_}COMPILER_TYPE) || !defined(${X_}COMPILER_VERSION)
153_v!=	${${cc}:N${CCACHE_BIN}} --version || echo 0.0.0
154
155.if !defined(${X_}COMPILER_TYPE)
156. if ${${cc}:T:M*gcc*}
157${X_}COMPILER_TYPE:=	gcc
158. elif ${${cc}:T:M*clang*}
159${X_}COMPILER_TYPE:=	clang
160. elif ${_v:Mgcc}
161${X_}COMPILER_TYPE:=	gcc
162. elif ${_v:M\(GCC\)} || ${_v:M*GNU}
163${X_}COMPILER_TYPE:=	gcc
164. elif ${_v:Mclang} || ${_v:M(clang-*.*.*)}
165${X_}COMPILER_TYPE:=	clang
166. else
167.error Unable to determine compiler type for ${cc}=${${cc}}.  Consider setting ${X_}COMPILER_TYPE.
168. endif
169.endif
170.if !defined(${X_}COMPILER_VERSION)
171${X_}COMPILER_VERSION!=echo "${_v:M[1-9]*.[0-9]*}" | awk -F. '{print $$1 * 10000 + $$2 * 100 + $$3;}'
172.endif
173.undef _v
174.endif
175.if !defined(${X_}COMPILER_FREEBSD_VERSION)
176${X_}COMPILER_FREEBSD_VERSION!=	{ echo "__FreeBSD_cc_version" | ${${cc}:N${CCACHE_BIN}} -E - 2>/dev/null || echo __FreeBSD_cc_version; } | sed -n '$$p'
177# If we get a literal "__FreeBSD_cc_version" back then the compiler
178# is a non-FreeBSD build that doesn't support it or some other error
179# occurred.
180.if ${${X_}COMPILER_FREEBSD_VERSION} == "__FreeBSD_cc_version"
181${X_}COMPILER_FREEBSD_VERSION=	unknown
182.endif
183.endif
184
185${X_}COMPILER_FEATURES=
186.if (${${X_}COMPILER_TYPE} == "clang" && ${${X_}COMPILER_VERSION} >= 30300) || \
187	(${${X_}COMPILER_TYPE} == "gcc" && ${${X_}COMPILER_VERSION} >= 40800)
188${X_}COMPILER_FEATURES+=	c++11
189.endif
190.if (${${X_}COMPILER_TYPE} == "clang" && ${${X_}COMPILER_VERSION} >= 30400) || \
191	(${${X_}COMPILER_TYPE} == "gcc" && ${${X_}COMPILER_VERSION} >= 50000)
192${X_}COMPILER_FEATURES+=	c++14
193.endif
194.if (${${X_}COMPILER_TYPE} == "clang" && ${${X_}COMPILER_VERSION} >= 50000) || \
195	(${${X_}COMPILER_TYPE} == "gcc" && ${${X_}COMPILER_VERSION} >= 70000)
196${X_}COMPILER_FEATURES+=	c++17
197.endif
198.if ${${X_}COMPILER_TYPE} == "clang" && ${${X_}COMPILER_VERSION} >= 60000
199${X_}COMPILER_FEATURES+=	retpoline
200.endif
201
202.else
203# Use CC's values
204X_COMPILER_TYPE=	${COMPILER_TYPE}
205X_COMPILER_VERSION=	${COMPILER_VERSION}
206X_COMPILER_FREEBSD_VERSION=	${COMPILER_FREEBSD_VERSION}
207X_COMPILER_FEATURES=	${COMPILER_FEATURES}
208.endif	# ${cc} == "CC" || (${cc} == "XCC" && ${XCC} != ${CC})
209
210# Export the values so sub-makes don't have to look them up again, using the
211# hash key computed above.
212.for var in ${_exported_vars}
213${var}.${${X_}_cc_hash}:=	${${var}}
214.export-env ${var}.${${X_}_cc_hash}
215.undef ${var}.${${X_}_cc_hash}
216.endfor
217
218.endif	# ${cc} == "CC" || !empty(XCC)
219.endfor	# .for cc in CC XCC
220
221.if !defined(_NO_INCLUDE_LINKERMK)
222.include <bsd.linker.mk>
223.endif
224.endif	# !target(__<bsd.compiler.mk>__)
225