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