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