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