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