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