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