bsd.compiler.mk 11.2 KB
Newer Older
1
2
# $FreeBSD$

3
4
# Setup variables for the compiler
#
5
6
7
8
# COMPILER_TYPE is the major type of compiler. Currently gcc and clang support
# automatic detection. Other compiler types can be shoe-horned in, but require
# explicit setting of the compiler type. The compiler type can also be set
# explicitly if, say, you install gcc as clang...
9
#
10
11
# COMPILER_VERSION is a numeric constant equal to:
#     major * 10000 + minor * 100 + tiny
Bryan Drewery's avatar
Bryan Drewery committed
12
# It too can be overridden on the command line. When testing it, be sure to
13
14
15
# make sure that you are limiting the test to a specific compiler. Testing
# against 30300 for gcc likely isn't  what you wanted (since versions of gcc
# prior to 4.2 likely have no prayer of working).
16
#
17
18
# COMPILER_FREEBSD_VERSION is the compiler's __FreeBSD_cc_version value.
#
19
20
21
# COMPILER_FEATURES will contain one or more of the following, based on
# compiler support for that feature:
#
22
23
# - c++17:     supports full (or nearly full) C++17 programming environment.
# - c++14:     supports full (or nearly full) C++14 programming environment.
24
25
26
# - c++11:     supports full (or nearly full) C++11 programming environment.
# - retpoline: supports the retpoline speculative execution vulnerability
#              mitigation.
27
# - init-all:  supports stack variable initialization.
28
# - aarch64-sha512: supports the AArch64 sha512 intrinsic functions.
29
#
30
31
32
33
34
35
# When bootstrapping on macOS, 'apple-clang' will be set in COMPILER_FEATURES
# to differentiate Apple's version of Clang. Apple Clang uses a different
# versioning scheme and may not support the same -W/-Wno warning flags. For a
# mapping of Apple Clang versions to upstream clang versions see
# 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
40
# This file may be included multiple times, but only has effect the first time.
#

41
42
43
.if !target(__<bsd.compiler.mk>__)
__<bsd.compiler.mk>__:

44
45
.include <bsd.opts.mk>

46
47
48
49
50
51
52
53
54
55
.if defined(_NO_INCLUDE_COMPILERMK)
# If _NO_INCLUDE_COMPILERMK is set we are doing a make obj/cleandir/cleanobj
# and might not have a valid compiler in $PATH yet. In this case just set the
# variables that are expected by the other .mk files and return
COMPILER_TYPE=none
X_COMPILER_TYPE=none
COMPILER_VERSION=0
X_COMPILER_VERSION=0
COMPILER_FEATURES=none
.else
56
57
58
# command = /usr/local/bin/ccache cc ...
# wrapper = /usr/local/libexec/ccache/cc ...
CCACHE_BUILD_TYPE?=	command
59
60
61
# Handle ccache after CC is determined, but not if CC/CXX are already
# overridden with a manual setup.
.if ${MK_CCACHE_BUILD:Uno} == "yes" && \
62
    !make(test-system-*) && !make(print-dir) && !make(showconfig) && \
63
64
65
66
67
    (${CC:M*ccache/world/*} == "" || ${CXX:M*ccache/world/*} == "")
# CC is always prepended with the ccache wrapper rather than modifying
# PATH since it is more clear that ccache is used and avoids wasting time
# for mkdep/linking/asm builds.
LOCALBASE?=		/usr/local
68
69
70
CCACHE_PKG_PREFIX?=	${LOCALBASE}
CCACHE_WRAPPER_PATH?=	${CCACHE_PKG_PREFIX}/libexec/ccache
CCACHE_BIN?=		${CCACHE_PKG_PREFIX}/bin/ccache
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
.if exists(${CCACHE_BIN})
# Export to ensure sub-makes can filter it out for mkdep/linking and
# to chain down into kernel build which won't include this file.
.export CCACHE_BIN
# Expand and export some variables so they may be based on make vars.
# This allows doing something like the following in the environment:
# CCACHE_BASEDIR='${SRCTOP:H}' MAKEOBJDIRPREFIX='${SRCTOP:H}/obj'
.for var in CCACHE_LOGFILE CCACHE_BASEDIR
.if defined(${var})
${var}:=	${${var}}
.export		${var}
.endif
.endfor
# Handle bootstrapped compiler changes properly by hashing their content
# rather than checking mtime.  For external compilers it should be safe
# to use the more optimal mtime check.
# XXX: CCACHE_COMPILERCHECK= string:<compiler_version, compiler_build_rev, compiler_patch_rev, compiler_default_target, compiler_default_sysroot>
.if ${CC:N${CCACHE_BIN}:[1]:M/*} == ""
CCACHE_COMPILERCHECK?=	content
.else
CCACHE_COMPILERCHECK?=	mtime
.endif
.export CCACHE_COMPILERCHECK
# Ensure no bogus CCACHE_PATH leaks in which might avoid the in-tree compiler.
95
.if !empty(CCACHE_PATH)
96
97
CCACHE_PATH=
.export CCACHE_PATH
98
.endif
99
100
101
.if ${CCACHE_BUILD_TYPE} == "command"
# Remove ccache from the PATH to prevent double calls and wasted CPP/LD time.
PATH:=	${PATH:C,:?${CCACHE_WRAPPER_PATH}(/world)?(:$)?,,g}
102
103
104
105
106
107
# Override various toolchain vars.
.for var in CC CXX HOST_CC HOST_CXX
.if defined(${var}) && ${${var}:M${CCACHE_BIN}} == ""
${var}:=	${CCACHE_BIN} ${${var}}
.endif
.endfor
108
109
110
.else
# Need to ensure CCACHE_WRAPPER_PATH is the first in ${PATH}
PATH:=	${PATH:C,:?${CCACHE_WRAPPER_PATH}(/world)?(:$)?,,g}
111
PATH:=	${CCACHE_WRAPPER_PATH}:${PATH}
112
CCACHE_WRAPPER_PATH_PFX=	${CCACHE_WRAPPER_PATH}:
113
.endif	# ${CCACHE_BUILD_TYPE} == "command"
114
115
116
117
118
119
120
121
122
# GCC does not need the CCACHE_CPP2 hack enabled by default in devel/ccache.
# The port enables it due to ccache passing preprocessed C to clang
# which fails with -Wparentheses-equality, -Wtautological-compare, and
# -Wself-assign on macro-expanded lines.
.if defined(COMPILER_TYPE) && ${COMPILER_TYPE} == "gcc"
CCACHE_NOCPP2=	1
.export CCACHE_NOCPP2
.endif
# Canonicalize CCACHE_DIR for meta mode usage.
123
124
125
126
127
.if !defined(CCACHE_DIR)
CCACHE_DIR!=	${CCACHE_BIN} -p | awk '$$2 == "cache_dir" {print $$4}'
.export CCACHE_DIR
.endif
.if !empty(CCACHE_DIR) && empty(.MAKE.META.IGNORE_PATHS:M${CCACHE_DIR})
128
129
130
131
CCACHE_DIR:=	${CCACHE_DIR:tA}
.MAKE.META.IGNORE_PATHS+= ${CCACHE_DIR}
.export CCACHE_DIR
.endif
132
133
134
# ccache doesn't affect build output so let it slide for meta mode
# comparisons.
.MAKE.META.IGNORE_PATHS+= ${CCACHE_BIN}
135
136
137
138
139
ccache-print-options: .PHONY
	@${CCACHE_BIN} -p
.endif	# exists(${CCACHE_BIN})
.endif	# ${MK_CCACHE_BUILD} == "yes"

140
141
142
143
144
145
146
147
148
149
_cc_vars=CC $${_empty_var_}
.if !empty(_WANT_TOOLCHAIN_CROSS_VARS)
# Only the toplevel makefile needs to compute the X_COMPILER_* variables.
# Skipping the computation of the unused X_COMPILER_* in the subdirectory
# makefiles can save a noticeable amount of time when walking the whole source
# tree (e.g. during make includes, etc.).
_cc_vars+=XCC X_
.endif

.for cc X_ in ${_cc_vars}
150
.if ${cc} == "CC" || !empty(XCC)
151
152
153
# Try to import COMPILER_TYPE and COMPILER_VERSION from parent make.
# The value is only used/exported for the same environment that impacts
# CC and COMPILER_* settings here.
154
_exported_vars=	${X_}COMPILER_TYPE ${X_}COMPILER_VERSION \
155
		${X_}COMPILER_FREEBSD_VERSION ${X_}COMPILER_RESOURCE_DIR
156
157
${X_}_cc_hash=	${${cc}}${MACHINE}${PATH}
${X_}_cc_hash:=	${${X_}_cc_hash:hash}
158
# Only import if none of the vars are set differently somehow else.
159
160
_can_export=	yes
.for var in ${_exported_vars}
161
162
163
164
.if defined(${var}) && (!defined(${var}__${${X_}_cc_hash}) || ${${var}__${${X_}_cc_hash}} != ${${var}})
.if defined(${var}__${${X_}_ld_hash})
.info "Cannot import ${X_}COMPILER variables since cached ${var} is different: ${${var}__${${X_}_cc_hash}} != ${${var}}"
.endif
165
166
167
168
_can_export=	no
.endif
.endfor
.if ${_can_export} == yes
169
.for var in ${_exported_vars}
170
171
.if defined(${var}__${${X_}_cc_hash})
${var}=	${${var}__${${X_}_cc_hash}}
172
173
.endif
.endfor
174
.endif
175

176
.if ${cc} == "CC" || (${cc} == "XCC" && ${XCC} != ${CC})
177
.if ${MACHINE} == "common"
178
179
# common is a pseudo machine for architecture independent
# generated files - thus there is no compiler.
180
181
${X_}COMPILER_TYPE= none
${X_}COMPILER_VERSION= 0
182
${X_}COMPILER_FREEBSD_VERSION= 0
183
.elif !defined(${X_}COMPILER_TYPE) || !defined(${X_}COMPILER_VERSION)
184
_v!=	${${cc}:N${CCACHE_BIN}} --version || echo 0.0.0
185

186
187
188
189
190
.if !defined(${X_}COMPILER_TYPE)
. if ${${cc}:T:M*gcc*}
${X_}COMPILER_TYPE:=	gcc
. elif ${${cc}:T:M*clang*}
${X_}COMPILER_TYPE:=	clang
191
. elif ${_v:Mgcc}
192
${X_}COMPILER_TYPE:=	gcc
193
. elif ${_v:M\(GCC\)} || ${_v:M*GNU}
194
${X_}COMPILER_TYPE:=	gcc
195
. elif ${_v:Mclang} || ${_v:M(clang-*.*.*)}
196
${X_}COMPILER_TYPE:=	clang
197
. else
198
199
200
201
202
203
204
# With GCC, cc --version prints "cc $VERSION ($PKGVERSION)", so if a
# distribution overrides the default GCC PKGVERSION it is not identified.
# However, its -v output always says "gcc version" in it, so fall back on that.
_gcc_version!=	${${cc}:N${CCACHE_BIN}} -v 2>&1 | grep "gcc version"
.  if !empty(_gcc_version)
${X_}COMPILER_TYPE:=	gcc
.  else
205
.error Unable to determine compiler type for ${cc}=${${cc}}.  Consider setting ${X_}COMPILER_TYPE.
206
207
.  endif
.undef _gcc_version
208
209
. endif
.endif
210
.if !defined(${X_}COMPILER_VERSION)
211
${X_}COMPILER_VERSION!=echo "${_v:M[1-9]*.[0-9]*}" | awk -F. '{print $$1 * 10000 + $$2 * 100 + $$3;}'
212
.endif
213
214
215
216
# Detect apple clang when bootstrapping to select appropriate warning flags.
.if !defined(${X_}COMPILER_FEATURES) && ${_v:[*]:M*Apple clang version*}
${X_}COMPILER_FEATURES=	apple-clang
.endif
217
.undef _v
218
.endif
219
.if !defined(${X_}COMPILER_FREEBSD_VERSION)
220
${X_}COMPILER_FREEBSD_VERSION!=	{ echo "__FreeBSD_cc_version" | ${${cc}:N${CCACHE_BIN}} -E - 2>/dev/null || echo __FreeBSD_cc_version; } | sed -n '$$p'
221
222
223
224
225
226
227
# If we get a literal "__FreeBSD_cc_version" back then the compiler
# is a non-FreeBSD build that doesn't support it or some other error
# occurred.
.if ${${X_}COMPILER_FREEBSD_VERSION} == "__FreeBSD_cc_version"
${X_}COMPILER_FREEBSD_VERSION=	unknown
.endif
.endif
228

229
230
231
232
.if !defined(${X_}COMPILER_RESOURCE_DIR)
${X_}COMPILER_RESOURCE_DIR!=	${${cc}:N${CCACHE_BIN}} -print-resource-dir 2>/dev/null || echo unknown
.endif

233
${X_}COMPILER_FEATURES+=		c++11 c++14
234
.if ${${X_}COMPILER_TYPE} == "clang" || \
235
236
237
	(${${X_}COMPILER_TYPE} == "gcc" && ${${X_}COMPILER_VERSION} >= 70000)
${X_}COMPILER_FEATURES+=	c++17
.endif
238
.if ${${X_}COMPILER_TYPE} == "clang"
239
240
241
242
${X_}COMPILER_FEATURES+=	retpoline init-all
# PR257638 lld fails with BE compressed debug.  Fixed in main but external tool
# chains will initially not have the fix.  For now limit the feature to LE
# targets.
243
244
245
246
247
# When compiling bootstrap tools on non-FreeBSD, the various MACHINE variables
# for the host can be missing or not match FreeBSD's naming (e.g. Linux/amd64
# reports as MACHINE=x86_64 MACHINE_ARCH=x86_64), causing TARGET_ENDIANNESS to
# be undefined; be conservative and default to off until we turn this on by
# default everywhere.
248
.include <bsd.endian.mk>
249
250
.if (${.MAKE.OS} == "FreeBSD" || defined(TARGET_ENDIANNESS)) && \
    ${TARGET_ENDIANNESS} == "1234"
251
252
${X_}COMPILER_FEATURES+=	compressed-debug
.endif
253
.endif
254
255
256
257
.if ${${X_}COMPILER_TYPE} == "clang" && ${${X_}COMPILER_VERSION} >= 100000 || \
	(${${X_}COMPILER_TYPE} == "gcc" && ${${X_}COMPILER_VERSION} >= 80100)
${X_}COMPILER_FEATURES+=	fileprefixmap
.endif
258

259
260
261
262
263
264
265
.if (${${X_}COMPILER_TYPE} == "clang" && ${${X_}COMPILER_VERSION} >= 130000) || \
	(${${X_}COMPILER_TYPE} == "gcc" && ${${X_}COMPILER_VERSION} >= 90000)
# AArch64 sha512 intrinsics are supported (and have been tested) in
# clang 13 and gcc 9.
${X_}COMPILER_FEATURES+=	aarch64-sha512
.endif

266
267
268
269
.else
# Use CC's values
X_COMPILER_TYPE=	${COMPILER_TYPE}
X_COMPILER_VERSION=	${COMPILER_VERSION}
270
X_COMPILER_FREEBSD_VERSION=	${COMPILER_FREEBSD_VERSION}
271
X_COMPILER_FEATURES=	${COMPILER_FEATURES}
272
X_COMPILER_RESOURCE_DIR=	${COMPILER_RESOURCE_DIR}
273
274
.endif	# ${cc} == "CC" || (${cc} == "XCC" && ${XCC} != ${CC})

275
276
277
# Export the values so sub-makes don't have to look them up again, using the
# hash key computed above.
.for var in ${_exported_vars}
278
279
280
${var}__${${X_}_cc_hash}:=	${${var}}
.export-env ${var}__${${X_}_cc_hash}
.undef ${var}__${${X_}_cc_hash}
281
282
.endfor

283
284
.endif	# ${cc} == "CC" || !empty(XCC)
.endfor	# .for cc in CC XCC
285

286
.if !defined(_NO_INCLUDE_LINKERMK)
287
.include <bsd.linker.mk>
288
.endif
289
.endif	# defined(_NO_INCLUDE_COMPILERMK)
290
.endif	# !target(__<bsd.compiler.mk>__)