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