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