1
2# Setup variables for the compiler
3#
4# COMPILER_TYPE is the major type of compiler. Currently gcc and clang support
5# automatic detection. Other compiler types can be shoe-horned in, but require
6# explicit setting of the compiler type. The compiler type can also be set
7# explicitly if, say, you install gcc as clang...
8#
9# COMPILER_VERSION is a numeric constant equal to:
10#     major * 10000 + minor * 100 + tiny
11# It too can be overridden on the command line. When testing it, be sure to
12# make sure that you are limiting the test to a specific compiler. Testing
13# against 30300 for gcc likely isn't  what you wanted (since versions of gcc
14# prior to 4.2 likely have no prayer of working).
15#
16# COMPILER_FREEBSD_VERSION is the compiler's __FreeBSD_cc_version value.
17#
18# COMPILER_FEATURES will contain one or more of the following, based on
19# compiler support for that feature:
20#
21# - c++17:     supports full (or nearly full) C++17 programming environment.
22# - c++14:     supports full (or nearly full) C++14 programming environment.
23# - c++11:     supports full (or nearly full) C++11 programming environment.
24# - retpoline: supports the retpoline speculative execution vulnerability
25#              mitigation.
26# - init-all:  supports stack variable initialization.
27# - aarch64-sha512: supports the AArch64 sha512 intrinsic functions.
28#
29# When bootstrapping on macOS, 'apple-clang' will be set in COMPILER_FEATURES
30# to differentiate Apple's version of Clang. Apple Clang uses a different
31# versioning scheme and may not support the same -W/-Wno warning flags. For a
32# mapping of Apple Clang versions to upstream clang versions see
33# https://en.wikipedia.org/wiki/Xcode#Xcode_7.0_-_12.x_(since_Free_On-Device_Development)
34#
35# These variables with an X_ prefix will also be provided if XCC is set.
36#
37# This file may be included multiple times, but only has effect the first time.
38#
39
40.if !target(__<bsd.compiler.mk>__)
41__<bsd.compiler.mk>__:
42
43.include <bsd.opts.mk>
44
45.if defined(_NO_INCLUDE_COMPILERMK)
46# If _NO_INCLUDE_COMPILERMK is set we are doing a make obj/cleandir/cleanobj
47# and might not have a valid compiler in $PATH yet. In this case just set the
48# variables that are expected by the other .mk files and return
49COMPILER_TYPE=none
50X_COMPILER_TYPE=none
51COMPILER_VERSION=0
52X_COMPILER_VERSION=0
53COMPILER_FEATURES=none
54.else
55# command = /usr/local/bin/ccache cc ...
56# wrapper = /usr/local/libexec/ccache/cc ...
57CCACHE_BUILD_TYPE?=	command
58# Handle ccache after CC is determined, but not if CC/CXX are already
59# overridden with a manual setup.
60.if ${MK_CCACHE_BUILD:Uno} == "yes" && \
61    !make(test-system-*) && !make(print-dir) && !make(showconfig) && \
62    (${CC:M*ccache/world/*} == "" || ${CXX:M*ccache/world/*} == "")
63# CC is always prepended with the ccache wrapper rather than modifying
64# PATH since it is more clear that ccache is used and avoids wasting time
65# for mkdep/linking/asm builds.
66LOCALBASE?=		/usr/local
67CCACHE_PKG_PREFIX?=	${LOCALBASE}
68CCACHE_WRAPPER_PATH?=	${CCACHE_PKG_PREFIX}/libexec/ccache
69CCACHE_BIN?=		${CCACHE_PKG_PREFIX}/bin/ccache
70.if exists(${CCACHE_BIN})
71# Export to ensure sub-makes can filter it out for mkdep/linking and
72# to chain down into kernel build which won't include this file.
73.export CCACHE_BIN
74# Expand and export some variables so they may be based on make vars.
75# This allows doing something like the following in the environment:
76# CCACHE_BASEDIR='${SRCTOP:H}' MAKEOBJDIRPREFIX='${SRCTOP:H}/obj'
77.for var in CCACHE_LOGFILE CCACHE_BASEDIR
78.if defined(${var})
79${var}:=	${${var}}
80.export		${var}
81.endif
82.endfor
83# Handle bootstrapped compiler changes properly by hashing their content
84# rather than checking mtime.  For external compilers it should be safe
85# to use the more optimal mtime check.
86# XXX: CCACHE_COMPILERCHECK= string:<compiler_version, compiler_build_rev, compiler_patch_rev, compiler_default_target, compiler_default_sysroot>
87.if ${CC:N${CCACHE_BIN}:[1]:M/*} == ""
88CCACHE_COMPILERCHECK?=	content
89.else
90CCACHE_COMPILERCHECK?=	mtime
91.endif
92.export CCACHE_COMPILERCHECK
93# Ensure no bogus CCACHE_PATH leaks in which might avoid the in-tree compiler.
94.if !empty(CCACHE_PATH)
95CCACHE_PATH=
96.export CCACHE_PATH
97.endif
98.if ${CCACHE_BUILD_TYPE} == "command"
99# Remove ccache from the PATH to prevent double calls and wasted CPP/LD time.
100PATH:=	${PATH:C,:?${CCACHE_WRAPPER_PATH}(/world)?(:$)?,,g}
101# Override various toolchain vars.
102.for var in CC CXX HOST_CC HOST_CXX
103.if defined(${var}) && ${${var}:M${CCACHE_BIN}} == ""
104${var}:=	${CCACHE_BIN} ${${var}}
105.endif
106.endfor
107.else
108# Need to ensure CCACHE_WRAPPER_PATH is the first in ${PATH}
109PATH:=	${PATH:C,:?${CCACHE_WRAPPER_PATH}(/world)?(:$)?,,g}
110PATH:=	${CCACHE_WRAPPER_PATH}:${PATH}
111CCACHE_WRAPPER_PATH_PFX=	${CCACHE_WRAPPER_PATH}:
112.endif	# ${CCACHE_BUILD_TYPE} == "command"
113# GCC does not need the CCACHE_CPP2 hack enabled by default in devel/ccache.
114# The port enables it due to ccache passing preprocessed C to clang
115# which fails with -Wparentheses-equality, -Wtautological-compare, and
116# -Wself-assign on macro-expanded lines.
117.if defined(COMPILER_TYPE) && ${COMPILER_TYPE} == "gcc"
118CCACHE_NOCPP2=	1
119.export CCACHE_NOCPP2
120.endif
121# Canonicalize CCACHE_DIR for meta mode usage.
122.if !defined(CCACHE_DIR)
123CCACHE_DIR!=	${CCACHE_BIN} -p | awk '$$2 == "cache_dir" {print $$4}'
124.export CCACHE_DIR
125.endif
126.if !empty(CCACHE_DIR) && empty(.MAKE.META.IGNORE_PATHS:M${CCACHE_DIR})
127CCACHE_DIR:=	${CCACHE_DIR:tA}
128.MAKE.META.IGNORE_PATHS+= ${CCACHE_DIR}
129.export CCACHE_DIR
130.endif
131# ccache doesn't affect build output so let it slide for meta mode
132# comparisons.
133.MAKE.META.IGNORE_PATHS+= ${CCACHE_BIN}
134ccache-print-options: .PHONY
135	@${CCACHE_BIN} -p
136.endif	# exists(${CCACHE_BIN})
137.endif	# ${MK_CCACHE_BUILD} == "yes"
138
139_cc_vars=CC $${_empty_var_}
140.if !empty(_WANT_TOOLCHAIN_CROSS_VARS)
141# Only the toplevel makefile needs to compute the X_COMPILER_* variables.
142# Skipping the computation of the unused X_COMPILER_* in the subdirectory
143# makefiles can save a noticeable amount of time when walking the whole source
144# tree (e.g. during make includes, etc.).
145_cc_vars+=XCC X_
146.endif
147
148.for cc X_ in ${_cc_vars}
149.if ${cc} == "CC" || !empty(XCC)
150# Try to import COMPILER_TYPE and COMPILER_VERSION from parent make.
151# The value is only used/exported for the same environment that impacts
152# CC and COMPILER_* settings here.
153_exported_vars=	${X_}COMPILER_TYPE ${X_}COMPILER_VERSION \
154		${X_}COMPILER_FREEBSD_VERSION ${X_}COMPILER_RESOURCE_DIR
155${X_}_cc_hash=	${${cc}}${MACHINE}${PATH}
156${X_}_cc_hash:=	${${X_}_cc_hash:hash}
157# Only import if none of the vars are set differently somehow else.
158_can_export=	yes
159.for var in ${_exported_vars}
160.if defined(${var}) && (!defined(${var}__${${X_}_cc_hash}) || ${${var}__${${X_}_cc_hash}} != ${${var}})
161.if defined(${var}__${${X_}_ld_hash})
162.info Cannot import ${X_}COMPILER variables since cached ${var} is different: ${${var}__${${X_}_cc_hash}} != ${${var}}
163.endif
164_can_export=	no
165.endif
166.endfor
167.if ${_can_export} == yes
168.for var in ${_exported_vars}
169.if defined(${var}__${${X_}_cc_hash})
170${var}=	${${var}__${${X_}_cc_hash}}
171.endif
172.endfor
173.endif
174
175.if ${cc} == "CC" || (${cc} == "XCC" && ${XCC} != ${CC})
176.if ${MACHINE} == "common"
177# common is a pseudo machine for architecture independent
178# generated files - thus there is no compiler.
179${X_}COMPILER_TYPE= none
180${X_}COMPILER_VERSION= 0
181${X_}COMPILER_FREEBSD_VERSION= 0
182.elif !defined(${X_}COMPILER_TYPE) || !defined(${X_}COMPILER_VERSION)
183_v!=	${${cc}:N${CCACHE_BIN}} --version || echo 0.0.0
184
185.if !defined(${X_}COMPILER_TYPE)
186. if ${${cc}:T:M*gcc*}
187${X_}COMPILER_TYPE:=	gcc
188. elif ${${cc}:T:M*clang*}
189${X_}COMPILER_TYPE:=	clang
190. elif ${_v:Mgcc}
191${X_}COMPILER_TYPE:=	gcc
192. elif ${_v:M\(GCC\)} || ${_v:M*GNU}
193${X_}COMPILER_TYPE:=	gcc
194. elif ${_v:Mclang} || ${_v:M(clang-*.*.*)}
195${X_}COMPILER_TYPE:=	clang
196. else
197# With GCC, cc --version prints "cc $VERSION ($PKGVERSION)", so if a
198# distribution overrides the default GCC PKGVERSION it is not identified.
199# However, its -v output always says "gcc version" in it, so fall back on that.
200_gcc_version!=	${${cc}:N${CCACHE_BIN}} -v 2>&1 | grep "gcc version"
201.  if !empty(_gcc_version)
202${X_}COMPILER_TYPE:=	gcc
203.  else
204.error Unable to determine compiler type for ${cc}=${${cc}}.  Consider setting ${X_}COMPILER_TYPE.
205.  endif
206.undef _gcc_version
207. endif
208.endif
209.if !defined(${X_}COMPILER_VERSION)
210${X_}COMPILER_VERSION!=echo "${_v:M[1-9]*.[0-9]*}" | awk -F. '{print $$1 * 10000 + $$2 * 100 + $$3;}'
211.endif
212# Detect apple clang when bootstrapping to select appropriate warning flags.
213.if !defined(${X_}COMPILER_FEATURES) && ${_v:[*]:M*Apple clang version*}
214${X_}COMPILER_FEATURES=	apple-clang
215.endif
216.undef _v
217.endif
218.if !defined(${X_}COMPILER_FREEBSD_VERSION)
219${X_}COMPILER_FREEBSD_VERSION!=	{ echo "__FreeBSD_cc_version" | ${${cc}:N${CCACHE_BIN}} -E - 2>/dev/null || echo __FreeBSD_cc_version; } | sed -n '$$p'
220# If we get a literal "__FreeBSD_cc_version" back then the compiler
221# is a non-FreeBSD build that doesn't support it or some other error
222# occurred.
223.if ${${X_}COMPILER_FREEBSD_VERSION} == "__FreeBSD_cc_version"
224${X_}COMPILER_FREEBSD_VERSION=	unknown
225.endif
226.endif
227
228.if !defined(${X_}COMPILER_RESOURCE_DIR)
229${X_}COMPILER_RESOURCE_DIR!=	${${cc}:N${CCACHE_BIN}} -print-resource-dir 2>/dev/null || echo unknown
230.endif
231
232${X_}COMPILER_FEATURES+=		c++11 c++14
233.if ${${X_}COMPILER_TYPE} == "clang" || \
234	(${${X_}COMPILER_TYPE} == "gcc" && ${${X_}COMPILER_VERSION} >= 70000)
235${X_}COMPILER_FEATURES+=	c++17
236.endif
237.if (${${X_}COMPILER_TYPE} == "clang" && ${${X_}COMPILER_VERSION} >= 100000) || \
238	(${${X_}COMPILER_TYPE} == "gcc" && ${${X_}COMPILER_VERSION} >= 100100)
239${X_}COMPILER_FEATURES+=	c++20
240.endif
241.if ${${X_}COMPILER_TYPE} == "clang" || \
242	(${${X_}COMPILER_TYPE} == "gcc" && ${${X_}COMPILER_VERSION} >= 120000)
243${X_}COMPILER_FEATURES+=	init-all
244.endif
245.if ${${X_}COMPILER_TYPE} == "clang"
246${X_}COMPILER_FEATURES+=	retpoline
247# PR257638 lld fails with BE compressed debug.  Fixed in main but external tool
248# chains will initially not have the fix.  For now limit the feature to LE
249# targets.
250# When compiling bootstrap tools on non-FreeBSD, the various MACHINE variables
251# for the host can be missing or not match FreeBSD's naming (e.g. Linux/amd64
252# reports as MACHINE=x86_64 MACHINE_ARCH=x86_64), causing TARGET_ENDIANNESS to
253# be undefined; be conservative and default to off until we turn this on by
254# default everywhere.
255.include <bsd.endian.mk>
256.if (${.MAKE.OS} == "FreeBSD" || defined(TARGET_ENDIANNESS)) && \
257    ${TARGET_ENDIANNESS} == "1234"
258${X_}COMPILER_FEATURES+=	compressed-debug
259.endif
260.endif
261.if ${${X_}COMPILER_TYPE} == "clang" && ${${X_}COMPILER_VERSION} >= 100000 || \
262	(${${X_}COMPILER_TYPE} == "gcc" && ${${X_}COMPILER_VERSION} >= 80100)
263${X_}COMPILER_FEATURES+=	fileprefixmap
264.endif
265
266.if (${${X_}COMPILER_TYPE} == "clang" && ${${X_}COMPILER_VERSION} >= 130000) || \
267	(${${X_}COMPILER_TYPE} == "gcc" && ${${X_}COMPILER_VERSION} >= 90000)
268# AArch64 sha512 intrinsics are supported (and have been tested) in
269# clang 13 and gcc 9.
270${X_}COMPILER_FEATURES+=	aarch64-sha512
271.endif
272
273.else
274# Use CC's values
275X_COMPILER_TYPE=	${COMPILER_TYPE}
276X_COMPILER_VERSION=	${COMPILER_VERSION}
277X_COMPILER_FREEBSD_VERSION=	${COMPILER_FREEBSD_VERSION}
278X_COMPILER_FEATURES=	${COMPILER_FEATURES}
279X_COMPILER_RESOURCE_DIR=	${COMPILER_RESOURCE_DIR}
280.endif	# ${cc} == "CC" || (${cc} == "XCC" && ${XCC} != ${CC})
281
282# Export the values so sub-makes don't have to look them up again, using the
283# hash key computed above.
284.for var in ${_exported_vars}
285${var}__${${X_}_cc_hash}:=	${${var}}
286.export-env ${var}__${${X_}_cc_hash}
287.undef ${var}__${${X_}_cc_hash}
288.endfor
289
290.endif	# ${cc} == "CC" || !empty(XCC)
291.endfor	# .for cc in CC XCC
292
293.if !defined(_NO_INCLUDE_LINKERMK)
294.include <bsd.linker.mk>
295.endif
296.endif	# defined(_NO_INCLUDE_COMPILERMK)
297.endif	# !target(__<bsd.compiler.mk>__)
298