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