1# $FreeBSD$ 2 3# Setup variables for the compiler 4# 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... 9# 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). 16# 17# COMPILER_FREEBSD_VERSION is the compiler's __FreeBSD_cc_version value. 18# 19# COMPILER_FEATURES will contain one or more of the following, based on 20# compiler support for that feature: 21# 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 26# mitigation. 27# - init-all: supports stack variable initialization. 28# - aarch64-sha512: supports the AArch64 sha512 intrinsic functions. 29# 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) 35# 36# These variables with an X_ prefix will also be provided if XCC is set. 37# 38# This file may be included multiple times, but only has effect the first time. 39# 40 41.if !target(__<bsd.compiler.mk>__) 42__<bsd.compiler.mk>__: 43 44.include <bsd.opts.mk> 45 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 50COMPILER_TYPE=none 51X_COMPILER_TYPE=none 52COMPILER_VERSION=0 53X_COMPILER_VERSION=0 54COMPILER_FEATURES=none 55.else 56# command = /usr/local/bin/ccache cc ... 57# wrapper = /usr/local/libexec/ccache/cc ... 58CCACHE_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. 67LOCALBASE?= /usr/local 68CCACHE_PKG_PREFIX?= ${LOCALBASE} 69CCACHE_WRAPPER_PATH?= ${CCACHE_PKG_PREFIX}/libexec/ccache 70CCACHE_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. 74.export CCACHE_BIN 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 79.if defined(${var}) 80${var}:= ${${var}} 81.export ${var} 82.endif 83.endfor 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/*} == "" 89CCACHE_COMPILERCHECK?= content 90.else 91CCACHE_COMPILERCHECK?= mtime 92.endif 93.export CCACHE_COMPILERCHECK 94# Ensure no bogus CCACHE_PATH leaks in which might avoid the in-tree compiler. 95.if !empty(CCACHE_PATH) 96CCACHE_PATH= 97.export CCACHE_PATH 98.endif 99.if ${CCACHE_BUILD_TYPE} == "command" 100# Remove ccache from the PATH to prevent double calls and wasted CPP/LD time. 101PATH:= ${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}} 106.endif 107.endfor 108.else 109# Need to ensure CCACHE_WRAPPER_PATH is the first in ${PATH} 110PATH:= ${PATH:C,:?${CCACHE_WRAPPER_PATH}(/world)?(:$)?,,g} 111PATH:= ${CCACHE_WRAPPER_PATH}:${PATH} 112CCACHE_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" 119CCACHE_NOCPP2= 1 120.export CCACHE_NOCPP2 121.endif 122# Canonicalize CCACHE_DIR for meta mode usage. 123.if !defined(CCACHE_DIR) 124CCACHE_DIR!= ${CCACHE_BIN} -p | awk '$$2 == "cache_dir" {print $$4}' 125.export CCACHE_DIR 126.endif 127.if !empty(CCACHE_DIR) && empty(.MAKE.META.IGNORE_PATHS:M${CCACHE_DIR}) 128CCACHE_DIR:= ${CCACHE_DIR:tA} 129.MAKE.META.IGNORE_PATHS+= ${CCACHE_DIR} 130.export CCACHE_DIR 131.endif 132# ccache doesn't affect build output so let it slide for meta mode 133# comparisons. 134.MAKE.META.IGNORE_PATHS+= ${CCACHE_BIN} 135ccache-print-options: .PHONY 136 @${CCACHE_BIN} -p 137.endif # exists(${CCACHE_BIN}) 138.endif # ${MK_CCACHE_BUILD} == "yes" 139 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.). 146_cc_vars+=XCC X_ 147.endif 148 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. 159_can_export= yes 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}} 164.endif 165_can_export= no 166.endif 167.endfor 168.if ${_can_export} == yes 169.for var in ${_exported_vars} 170.if defined(${var}__${${X_}_cc_hash}) 171${var}= ${${var}__${${X_}_cc_hash}} 172.endif 173.endfor 174.endif 175 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 185 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 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# 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 204. else 205.error Unable to determine compiler type for ${cc}=${${cc}}. Consider setting ${X_}COMPILER_TYPE. 206. endif 207.undef _gcc_version 208. endif 209.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# 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 216.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# 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 223# occurred. 224.if ${${X_}COMPILER_FREEBSD_VERSION} == "__FreeBSD_cc_version" 225${X_}COMPILER_FREEBSD_VERSION= unknown 226.endif 227.endif 228 229.if !defined(${X_}COMPILER_RESOURCE_DIR) 230${X_}COMPILER_RESOURCE_DIR!= ${${cc}:N${CCACHE_BIN}} -print-resource-dir 2>/dev/null || echo unknown 231.endif 232 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 237.endif 238.if ${${X_}COMPILER_TYPE} == "clang" 239${X_}COMPILER_FEATURES+= retpoline init-all 240# PR257638 lld fails with BE compressed debug. Fixed in main but external tool 241# chains will initially not have the fix. For now limit the feature to LE 242# targets. 243# When compiling bootstrap tools on non-FreeBSD, the various MACHINE variables 244# for the host can be missing or not match FreeBSD's naming (e.g. Linux/amd64 245# reports as MACHINE=x86_64 MACHINE_ARCH=x86_64), causing TARGET_ENDIANNESS to 246# be undefined; be conservative and default to off until we turn this on by 247# default everywhere. 248.include <bsd.endian.mk> 249.if (${.MAKE.OS} == "FreeBSD" || defined(TARGET_ENDIANNESS)) && \ 250 ${TARGET_ENDIANNESS} == "1234" 251${X_}COMPILER_FEATURES+= compressed-debug 252.endif 253.endif 254.if ${${X_}COMPILER_TYPE} == "clang" && ${${X_}COMPILER_VERSION} >= 100000 || \ 255 (${${X_}COMPILER_TYPE} == "gcc" && ${${X_}COMPILER_VERSION} >= 80100) 256${X_}COMPILER_FEATURES+= fileprefixmap 257.endif 258 259.if (${${X_}COMPILER_TYPE} == "clang" && ${${X_}COMPILER_VERSION} >= 130000) || \ 260 (${${X_}COMPILER_TYPE} == "gcc" && ${${X_}COMPILER_VERSION} >= 90000) 261# AArch64 sha512 intrinsics are supported (and have been tested) in 262# clang 13 and gcc 9. 263${X_}COMPILER_FEATURES+= aarch64-sha512 264.endif 265 266.else 267# Use CC's values 268X_COMPILER_TYPE= ${COMPILER_TYPE} 269X_COMPILER_VERSION= ${COMPILER_VERSION} 270X_COMPILER_FREEBSD_VERSION= ${COMPILER_FREEBSD_VERSION} 271X_COMPILER_FEATURES= ${COMPILER_FEATURES} 272X_COMPILER_RESOURCE_DIR= ${COMPILER_RESOURCE_DIR} 273.endif # ${cc} == "CC" || (${cc} == "XCC" && ${XCC} != ${CC}) 274 275# Export the values so sub-makes don't have to look them up again, using the 276# hash key computed above. 277.for var in ${_exported_vars} 278${var}__${${X_}_cc_hash}:= ${${var}} 279.export-env ${var}__${${X_}_cc_hash} 280.undef ${var}__${${X_}_cc_hash} 281.endfor 282 283.endif # ${cc} == "CC" || !empty(XCC) 284.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>__) 291