1# $FreeBSD: stable/11/share/mk/bsd.compiler.mk 352638 2019-09-24 02:30:32Z mhorne $ 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++11: supports full (or nearly full) C++11 programming environment. 23# - retpoline: supports the retpoline speculative execution vulnerability 24# mitigation. 25# 26# These variables with an X_ prefix will also be provided if XCC is set. 27# 28# This file may be included multiple times, but only has effect the first time. 29# 30 31.if !target(__<bsd.compiler.mk>__) 32__<bsd.compiler.mk>__: 33 34.include <bsd.opts.mk> 35 36# Handle ccache after CC is determined, but not if CC/CXX are already 37# overridden with a manual setup. 38.if ${MK_CCACHE_BUILD:Uno} == "yes" && \ 39 !make(showconfig) && \ 40 (${CC:M*ccache/world/*} == "" || ${CXX:M*ccache/world/*} == "") 41# CC is always prepended with the ccache wrapper rather than modifying 42# PATH since it is more clear that ccache is used and avoids wasting time 43# for mkdep/linking/asm builds. 44LOCALBASE?= /usr/local 45CCACHE_WRAPPER_PATH?= ${LOCALBASE}/libexec/ccache 46CCACHE_BIN?= ${LOCALBASE}/bin/ccache 47.if exists(${CCACHE_BIN}) 48# Export to ensure sub-makes can filter it out for mkdep/linking and 49# to chain down into kernel build which won't include this file. 50.export CCACHE_BIN 51# Expand and export some variables so they may be based on make vars. 52# This allows doing something like the following in the environment: 53# CCACHE_BASEDIR='${SRCTOP:H}' MAKEOBJDIRPREFIX='${SRCTOP:H}/obj' 54.for var in CCACHE_LOGFILE CCACHE_BASEDIR 55.if defined(${var}) 56${var}:= ${${var}} 57.export ${var} 58.endif 59.endfor 60# Handle bootstrapped compiler changes properly by hashing their content 61# rather than checking mtime. For external compilers it should be safe 62# to use the more optimal mtime check. 63# XXX: CCACHE_COMPILERCHECK= string:<compiler_version, compiler_build_rev, compiler_patch_rev, compiler_default_target, compiler_default_sysroot> 64.if ${CC:N${CCACHE_BIN}:[1]:M/*} == "" 65CCACHE_COMPILERCHECK?= content 66.else 67CCACHE_COMPILERCHECK?= mtime 68.endif 69.export CCACHE_COMPILERCHECK 70# Remove ccache from the PATH to prevent double calls and wasted CPP/LD time. 71PATH:= ${PATH:C,:?${CCACHE_WRAPPER_PATH}(/world)?(:$)?,,g} 72# Ensure no bogus CCACHE_PATH leaks in which might avoid the in-tree compiler. 73.if !empty(CCACHE_PATH) 74CCACHE_PATH= 75.export CCACHE_PATH 76.endif 77# Override various toolchain vars. 78.for var in CC CXX HOST_CC HOST_CXX 79.if defined(${var}) && ${${var}:M${CCACHE_BIN}} == "" 80${var}:= ${CCACHE_BIN} ${${var}} 81.endif 82.endfor 83# GCC does not need the CCACHE_CPP2 hack enabled by default in devel/ccache. 84# The port enables it due to ccache passing preprocessed C to clang 85# which fails with -Wparentheses-equality, -Wtautological-compare, and 86# -Wself-assign on macro-expanded lines. 87.if defined(COMPILER_TYPE) && ${COMPILER_TYPE} == "gcc" 88CCACHE_NOCPP2= 1 89.export CCACHE_NOCPP2 90.endif 91# Canonicalize CCACHE_DIR for meta mode usage. 92.if !defined(CCACHE_DIR) 93CCACHE_DIR!= ${CCACHE_BIN} -p | awk '$$2 == "cache_dir" {print $$4}' 94.export CCACHE_DIR 95.endif 96.if !empty(CCACHE_DIR) && empty(.MAKE.META.IGNORE_PATHS:M${CCACHE_DIR}) 97CCACHE_DIR:= ${CCACHE_DIR:tA} 98.MAKE.META.IGNORE_PATHS+= ${CCACHE_DIR} 99.export CCACHE_DIR 100.endif 101# ccache doesn't affect build output so let it slide for meta mode 102# comparisons. 103.MAKE.META.IGNORE_PATHS+= ${CCACHE_BIN} 104ccache-print-options: .PHONY 105 @${CCACHE_BIN} -p 106.endif # exists(${CCACHE_BIN}) 107.endif # ${MK_CCACHE_BUILD} == "yes" 108 109.for cc X_ in CC $${_empty_var_} XCC X_ 110.if ${cc} == "CC" || !empty(XCC) 111# Try to import COMPILER_TYPE and COMPILER_VERSION from parent make. 112# The value is only used/exported for the same environment that impacts 113# CC and COMPILER_* settings here. 114_exported_vars= ${X_}COMPILER_TYPE ${X_}COMPILER_VERSION \ 115 ${X_}COMPILER_FREEBSD_VERSION 116${X_}_cc_hash= ${${cc}}${MACHINE}${PATH} 117${X_}_cc_hash:= ${${X_}_cc_hash:hash} 118# Only import if none of the vars are set somehow else. 119_can_export= yes 120.for var in ${_exported_vars} 121.if defined(${var}) 122_can_export= no 123.endif 124.endfor 125.if ${_can_export} == yes 126.for var in ${_exported_vars} 127.if defined(${var}.${${X_}_cc_hash}) 128${var}= ${${var}.${${X_}_cc_hash}} 129.endif 130.endfor 131.endif 132 133.if ${cc} == "CC" || (${cc} == "XCC" && ${XCC} != ${CC}) 134.if ${MACHINE} == "common" 135# common is a pseudo machine for architecture independent 136# generated files - thus there is no compiler. 137${X_}COMPILER_TYPE= none 138${X_}COMPILER_VERSION= 0 139${X_}COMPILER_FREEBSD_VERSION= 0 140.elif !defined(${X_}COMPILER_TYPE) || !defined(${X_}COMPILER_VERSION) 141_v!= ${${cc}:N${CCACHE_BIN}} --version || echo 0.0.0 142 143.if !defined(${X_}COMPILER_TYPE) 144. if ${${cc}:T:M*gcc*} 145${X_}COMPILER_TYPE:= gcc 146. elif ${${cc}:T:M*clang*} 147${X_}COMPILER_TYPE:= clang 148. elif ${_v:Mgcc} 149${X_}COMPILER_TYPE:= gcc 150. elif ${_v:M\(GCC\)} 151${X_}COMPILER_TYPE:= gcc 152. elif ${_v:Mclang} 153${X_}COMPILER_TYPE:= clang 154. else 155.error Unable to determine compiler type for ${cc}=${${cc}}. Consider setting ${X_}COMPILER_TYPE. 156. endif 157.endif 158.if !defined(${X_}COMPILER_VERSION) 159${X_}COMPILER_VERSION!=echo "${_v:M[1-9]*.[0-9]*}" | awk -F. '{print $$1 * 10000 + $$2 * 100 + $$3;}' 160.endif 161.undef _v 162.endif 163.if !defined(${X_}COMPILER_FREEBSD_VERSION) 164${X_}COMPILER_FREEBSD_VERSION!= { echo "__FreeBSD_cc_version" | ${${cc}:N${CCACHE_BIN}} -E - 2>/dev/null || echo __FreeBSD_cc_version; } | sed -n '$$p' 165# If we get a literal "__FreeBSD_cc_version" back then the compiler 166# is a non-FreeBSD build that doesn't support it or some other error 167# occurred. 168.if ${${X_}COMPILER_FREEBSD_VERSION} == "__FreeBSD_cc_version" 169${X_}COMPILER_FREEBSD_VERSION= unknown 170.endif 171.endif 172 173${X_}COMPILER_FEATURES= 174.if ${${X_}COMPILER_TYPE} == "clang" || \ 175 (${${X_}COMPILER_TYPE} == "gcc" && ${${X_}COMPILER_VERSION} >= 40800) 176${X_}COMPILER_FEATURES+= c++11 177.endif 178.if ${${X_}COMPILER_TYPE} == "clang" && ${${X_}COMPILER_VERSION} >= 60000 179${X_}COMPILER_FEATURES+= retpoline 180.endif 181 182.else 183# Use CC's values 184X_COMPILER_TYPE= ${COMPILER_TYPE} 185X_COMPILER_VERSION= ${COMPILER_VERSION} 186X_COMPILER_FREEBSD_VERSION= ${COMPILER_FREEBSD_VERSION} 187X_COMPILER_FEATURES= ${COMPILER_FEATURES} 188.endif # ${cc} == "CC" || (${cc} == "XCC" && ${XCC} != ${CC}) 189 190# Export the values so sub-makes don't have to look them up again, using the 191# hash key computed above. 192.for var in ${_exported_vars} 193${var}.${${X_}_cc_hash}:= ${${var}} 194.export-env ${var}.${${X_}_cc_hash} 195.undef ${var}.${${X_}_cc_hash} 196.endfor 197 198.endif # ${cc} == "CC" || !empty(XCC) 199.endfor # .for cc in CC XCC 200 201.include <bsd.linker.mk> 202.endif # !target(__<bsd.compiler.mk>__) 203