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 overriden 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_FEATURES will contain one or more of the following, based on
18 # compiler support for that feature:
20 # - c++11 : supports full (or nearly full) C++11 programming environment.
22 # This file may be included multiple times, but only has effect the first time.
25 .if !target(__<bsd.compiler.mk>__)
26 __<bsd.compiler.mk>__:
28 .include <bsd.opts.mk>
30 # Handle ccache after CC is determined, but not if CC/CXX are already
31 # overridden with a manual setup.
32 .if ${MK_CCACHE_BUILD:Uno} == "yes" && \
33 !make(showconfig) && \
34 (${CC:M*ccache/world/*} == "" || ${CXX:M*ccache/world/*} == "")
35 # CC is always prepended with the ccache wrapper rather than modifying
36 # PATH since it is more clear that ccache is used and avoids wasting time
37 # for mkdep/linking/asm builds.
38 LOCALBASE?= /usr/local
39 CCACHE_WRAPPER_PATH?= ${LOCALBASE}/libexec/ccache
40 CCACHE_BIN?= ${LOCALBASE}/bin/ccache
41 .if exists(${CCACHE_BIN})
42 # Export to ensure sub-makes can filter it out for mkdep/linking and
43 # to chain down into kernel build which won't include this file.
45 # Expand and export some variables so they may be based on make vars.
46 # This allows doing something like the following in the environment:
47 # CCACHE_BASEDIR='${SRCTOP:H}' MAKEOBJDIRPREFIX='${SRCTOP:H}/obj'
48 .for var in CCACHE_LOGFILE CCACHE_BASEDIR
54 # Handle bootstrapped compiler changes properly by hashing their content
55 # rather than checking mtime. For external compilers it should be safe
56 # to use the more optimal mtime check.
57 # XXX: CCACHE_COMPILERCHECK= string:<compiler_version, compiler_build_rev, compiler_patch_rev, compiler_default_target, compiler_default_sysroot>
58 .if ${CC:N${CCACHE_BIN}:[1]:M/*} == ""
59 CCACHE_COMPILERCHECK?= content
61 CCACHE_COMPILERCHECK?= mtime
63 .export CCACHE_COMPILERCHECK
64 # Remove ccache from the PATH to prevent double calls and wasted CPP/LD time.
65 PATH:= ${PATH:C,:?${CCACHE_WRAPPER_PATH}(/world)?(:$)?,,g}
66 # Ensure no bogus CCACHE_PATH leaks in which might avoid the in-tree compiler.
69 # Override various toolchain vars.
70 .for var in CC CXX HOST_CC HOST_CXX
71 .if defined(${var}) && ${${var}:M${CCACHE_BIN}} == ""
72 ${var}:= ${CCACHE_BIN} ${${var}}
75 # GCC does not need the CCACHE_CPP2 hack enabled by default in devel/ccache.
76 # The port enables it due to ccache passing preprocessed C to clang
77 # which fails with -Wparentheses-equality, -Wtautological-compare, and
78 # -Wself-assign on macro-expanded lines.
79 .if defined(COMPILER_TYPE) && ${COMPILER_TYPE} == "gcc"
83 # Canonicalize CCACHE_DIR for meta mode usage.
84 .if defined(CCACHE_DIR) && empty(.MAKE.META.IGNORE_PATHS:M${CCACHE_DIR})
85 CCACHE_DIR:= ${CCACHE_DIR:tA}
86 .MAKE.META.IGNORE_PATHS+= ${CCACHE_DIR}
89 ccache-print-options: .PHONY
91 .endif # exists(${CCACHE_BIN})
92 .endif # ${MK_CCACHE_BUILD} == "yes"
94 # Try to import COMPILER_TYPE and COMPILER_VERSION from parent make.
95 # The value is only used/exported for the same environment that impacts
96 # CC and COMPILER_* settings here.
97 _exported_vars= COMPILER_TYPE COMPILER_VERSION
98 _cc_hash= ${CC}${MACHINE}${PATH}
99 _cc_hash:= ${_cc_hash:hash}
100 # Only import if none of the vars are set somehow else.
102 .for var in ${_exported_vars}
107 .if ${_can_export} == yes
108 .for var in ${_exported_vars}
109 .if defined(${var}.${_cc_hash})
110 ${var}= ${${var}.${_cc_hash}}
115 .if ${MACHINE} == "common"
116 # common is a pseudo machine for architecture independent
117 # generated files - thus there is no compiler.
120 .elif !defined(COMPILER_TYPE) || !defined(COMPILER_VERSION)
121 _v!= ${CC} --version || echo 0.0.0
123 .if !defined(COMPILER_TYPE)
126 . elif ${CC:T:M*clang*}
127 COMPILER_TYPE:= clang
130 . elif ${_v:M\(GCC\)}
133 COMPILER_TYPE:= clang
135 .error Unable to determine compiler type for ${CC}. Consider setting COMPILER_TYPE.
138 .if !defined(COMPILER_VERSION)
139 COMPILER_VERSION!=echo ${_v:M[1-9].[0-9]*} | awk -F. '{print $$1 * 10000 + $$2 * 100 + $$3;}'
144 # Export the values so sub-makes don't have to look them up again, using the
145 # hash key computed above.
146 .for var in ${_exported_vars}
147 ${var}.${_cc_hash}:= ${${var}}
148 .export-env ${var}.${_cc_hash}
149 .undef ${var}.${_cc_hash}
152 .if ${COMPILER_TYPE} == "clang" || \
153 (${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 40800)
154 COMPILER_FEATURES= c++11
159 .endif # !target(__<bsd.compiler.mk>__)