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