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.
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)
35 # These variables with an X_ prefix will also be provided if XCC is set.
37 # This file may be included multiple times, but only has effect the first time.
40 .if !target(__<bsd.compiler.mk>__)
41 __<bsd.compiler.mk>__:
43 .include <bsd.opts.mk>
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
53 COMPILER_FEATURES=none
55 # command = /usr/local/bin/ccache cc ...
56 # wrapper = /usr/local/libexec/ccache/cc ...
57 CCACHE_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.
66 LOCALBASE?= /usr/local
67 CCACHE_WRAPPER_PATH?= ${LOCALBASE}/libexec/ccache
68 CCACHE_BIN?= ${LOCALBASE}/bin/ccache
69 .if exists(${CCACHE_BIN})
70 # Export to ensure sub-makes can filter it out for mkdep/linking and
71 # to chain down into kernel build which won't include this file.
73 # Expand and export some variables so they may be based on make vars.
74 # This allows doing something like the following in the environment:
75 # CCACHE_BASEDIR='${SRCTOP:H}' MAKEOBJDIRPREFIX='${SRCTOP:H}/obj'
76 .for var in CCACHE_LOGFILE CCACHE_BASEDIR
82 # Handle bootstrapped compiler changes properly by hashing their content
83 # rather than checking mtime. For external compilers it should be safe
84 # to use the more optimal mtime check.
85 # XXX: CCACHE_COMPILERCHECK= string:<compiler_version, compiler_build_rev, compiler_patch_rev, compiler_default_target, compiler_default_sysroot>
86 .if ${CC:N${CCACHE_BIN}:[1]:M/*} == ""
87 CCACHE_COMPILERCHECK?= content
89 CCACHE_COMPILERCHECK?= mtime
91 .export CCACHE_COMPILERCHECK
92 # Ensure no bogus CCACHE_PATH leaks in which might avoid the in-tree compiler.
93 .if !empty(CCACHE_PATH)
97 .if ${CCACHE_BUILD_TYPE} == "command"
98 # Remove ccache from the PATH to prevent double calls and wasted CPP/LD time.
99 PATH:= ${PATH:C,:?${CCACHE_WRAPPER_PATH}(/world)?(:$)?,,g}
100 # Override various toolchain vars.
101 .for var in CC CXX HOST_CC HOST_CXX
102 .if defined(${var}) && ${${var}:M${CCACHE_BIN}} == ""
103 ${var}:= ${CCACHE_BIN} ${${var}}
107 # Need to ensure CCACHE_WRAPPER_PATH is the first in ${PATH}
108 PATH:= ${PATH:C,:?${CCACHE_WRAPPER_PATH}(/world)?(:$)?,,g}
109 PATH:= ${CCACHE_WRAPPER_PATH}:${PATH}
110 CCACHE_WRAPPER_PATH_PFX= ${CCACHE_WRAPPER_PATH}:
111 .endif # ${CCACHE_BUILD_TYPE} == "command"
112 # GCC does not need the CCACHE_CPP2 hack enabled by default in devel/ccache.
113 # The port enables it due to ccache passing preprocessed C to clang
114 # which fails with -Wparentheses-equality, -Wtautological-compare, and
115 # -Wself-assign on macro-expanded lines.
116 .if defined(COMPILER_TYPE) && ${COMPILER_TYPE} == "gcc"
118 .export CCACHE_NOCPP2
120 # Canonicalize CCACHE_DIR for meta mode usage.
121 .if !defined(CCACHE_DIR)
122 CCACHE_DIR!= ${CCACHE_BIN} -p | awk '$$2 == "cache_dir" {print $$4}'
125 .if !empty(CCACHE_DIR) && empty(.MAKE.META.IGNORE_PATHS:M${CCACHE_DIR})
126 CCACHE_DIR:= ${CCACHE_DIR:tA}
127 .MAKE.META.IGNORE_PATHS+= ${CCACHE_DIR}
130 # ccache doesn't affect build output so let it slide for meta mode
132 .MAKE.META.IGNORE_PATHS+= ${CCACHE_BIN}
133 ccache-print-options: .PHONY
135 .endif # exists(${CCACHE_BIN})
136 .endif # ${MK_CCACHE_BUILD} == "yes"
138 _cc_vars=CC $${_empty_var_}
139 .if !empty(_WANT_TOOLCHAIN_CROSS_VARS)
140 # Only the toplevel makefile needs to compute the X_COMPILER_* variables.
141 # Skipping the computation of the unused X_COMPILER_* in the subdirectory
142 # makefiles can save a noticeable amount of time when walking the whole source
143 # tree (e.g. during make includes, etc.).
147 .for cc X_ in ${_cc_vars}
148 .if ${cc} == "CC" || !empty(XCC)
149 # Try to import COMPILER_TYPE and COMPILER_VERSION from parent make.
150 # The value is only used/exported for the same environment that impacts
151 # CC and COMPILER_* settings here.
152 _exported_vars= ${X_}COMPILER_TYPE ${X_}COMPILER_VERSION \
153 ${X_}COMPILER_FREEBSD_VERSION ${X_}COMPILER_RESOURCE_DIR
154 ${X_}_cc_hash= ${${cc}}${MACHINE}${PATH}
155 ${X_}_cc_hash:= ${${X_}_cc_hash:hash}
156 # Only import if none of the vars are set differently somehow else.
158 .for var in ${_exported_vars}
159 .if defined(${var}) && (!defined(${var}__${${X_}_cc_hash}) || ${${var}__${${X_}_cc_hash}} != ${${var}})
160 .if defined(${var}__${${X_}_ld_hash})
161 .info "Cannot import ${X_}COMPILER variables since cached ${var} is different: ${${var}__${${X_}_cc_hash}} != ${${var}}"
166 .if ${_can_export} == yes
167 .for var in ${_exported_vars}
168 .if defined(${var}__${${X_}_cc_hash})
169 ${var}= ${${var}__${${X_}_cc_hash}}
174 .if ${cc} == "CC" || (${cc} == "XCC" && ${XCC} != ${CC})
175 .if ${MACHINE} == "common"
176 # common is a pseudo machine for architecture independent
177 # generated files - thus there is no compiler.
178 ${X_}COMPILER_TYPE= none
179 ${X_}COMPILER_VERSION= 0
180 ${X_}COMPILER_FREEBSD_VERSION= 0
181 .elif !defined(${X_}COMPILER_TYPE) || !defined(${X_}COMPILER_VERSION)
182 _v!= ${${cc}:N${CCACHE_BIN}} --version || echo 0.0.0
184 .if !defined(${X_}COMPILER_TYPE)
185 . if ${${cc}:T:M*gcc*}
186 ${X_}COMPILER_TYPE:= gcc
187 . elif ${${cc}:T:M*clang*}
188 ${X_}COMPILER_TYPE:= clang
190 ${X_}COMPILER_TYPE:= gcc
191 . elif ${_v:M\(GCC\)} || ${_v:M*GNU}
192 ${X_}COMPILER_TYPE:= gcc
193 . elif ${_v:Mclang} || ${_v:M(clang-*.*.*)}
194 ${X_}COMPILER_TYPE:= clang
196 # With GCC, cc --version prints "cc $VERSION ($PKGVERSION)", so if a
197 # distribution overrides the default GCC PKGVERSION it is not identified.
198 # However, its -v output always says "gcc version" in it, so fall back on that.
199 _gcc_version!= ${${cc}:N${CCACHE_BIN}} -v 2>&1 | grep "gcc version"
200 . if !empty(_gcc_version)
201 ${X_}COMPILER_TYPE:= gcc
203 .error Unable to determine compiler type for ${cc}=${${cc}}. Consider setting ${X_}COMPILER_TYPE.
208 .if !defined(${X_}COMPILER_VERSION)
209 ${X_}COMPILER_VERSION!=echo "${_v:M[1-9]*.[0-9]*}" | awk -F. '{print $$1 * 10000 + $$2 * 100 + $$3;}'
211 # Detect apple clang when bootstrapping to select appropriate warning flags.
212 .if !defined(${X_}COMPILER_FEATURES) && ${_v:[*]:M*Apple clang version*}
213 ${X_}COMPILER_FEATURES= apple-clang
217 .if !defined(${X_}COMPILER_FREEBSD_VERSION)
218 ${X_}COMPILER_FREEBSD_VERSION!= { echo "__FreeBSD_cc_version" | ${${cc}:N${CCACHE_BIN}} -E - 2>/dev/null || echo __FreeBSD_cc_version; } | sed -n '$$p'
219 # If we get a literal "__FreeBSD_cc_version" back then the compiler
220 # is a non-FreeBSD build that doesn't support it or some other error
222 .if ${${X_}COMPILER_FREEBSD_VERSION} == "__FreeBSD_cc_version"
223 ${X_}COMPILER_FREEBSD_VERSION= unknown
227 .if !defined(${X_}COMPILER_RESOURCE_DIR)
228 ${X_}COMPILER_RESOURCE_DIR!= ${${cc}:N${CCACHE_BIN}} -print-resource-dir 2>/dev/null || echo unknown
231 ${X_}COMPILER_FEATURES+= c++11 c++14
232 .if ${${X_}COMPILER_TYPE} == "clang" || \
233 (${${X_}COMPILER_TYPE} == "gcc" && ${${X_}COMPILER_VERSION} >= 70000)
234 ${X_}COMPILER_FEATURES+= c++17
236 .if ${${X_}COMPILER_TYPE} == "clang"
237 ${X_}COMPILER_FEATURES+= retpoline init-all
239 .if ${${X_}COMPILER_TYPE} == "clang" && ${${X_}COMPILER_VERSION} >= 100000 || \
240 (${${X_}COMPILER_TYPE} == "gcc" && ${${X_}COMPILER_VERSION} >= 80100)
241 ${X_}COMPILER_FEATURES+= fileprefixmap
246 X_COMPILER_TYPE= ${COMPILER_TYPE}
247 X_COMPILER_VERSION= ${COMPILER_VERSION}
248 X_COMPILER_FREEBSD_VERSION= ${COMPILER_FREEBSD_VERSION}
249 X_COMPILER_FEATURES= ${COMPILER_FEATURES}
250 X_COMPILER_RESOURCE_DIR= ${COMPILER_RESOURCE_DIR}
251 .endif # ${cc} == "CC" || (${cc} == "XCC" && ${XCC} != ${CC})
253 # Export the values so sub-makes don't have to look them up again, using the
254 # hash key computed above.
255 .for var in ${_exported_vars}
256 ${var}__${${X_}_cc_hash}:= ${${var}}
257 .export-env ${var}__${${X_}_cc_hash}
258 .undef ${var}__${${X_}_cc_hash}
261 .endif # ${cc} == "CC" || !empty(XCC)
262 .endfor # .for cc in CC XCC
264 .if !defined(_NO_INCLUDE_LINKERMK)
265 .include <bsd.linker.mk>
267 .endif # defined(_NO_INCLUDE_COMPILERMK)
268 .endif # !target(__<bsd.compiler.mk>__)