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