bsd.compiler.mk revision 300351
1# $FreeBSD: head/share/mk/bsd.compiler.mk 300351 2016-05-21 01:32:13Z bdrewery $
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
99ccache-print-options: .PHONY
100	@${CCACHE_BIN} -p
101.endif	# exists(${CCACHE_BIN})
102.endif	# ${MK_CCACHE_BUILD} == "yes"
103
104.for cc X_ in CC $${_empty_var_} XCC X_
105.if ${cc} == "CC" || !empty(XCC)
106# Try to import COMPILER_TYPE and COMPILER_VERSION from parent make.
107# The value is only used/exported for the same environment that impacts
108# CC and COMPILER_* settings here.
109_exported_vars=	${X_}COMPILER_TYPE ${X_}COMPILER_VERSION \
110		${X_}COMPILER_FREEBSD_VERSION
111${X_}_cc_hash=	${${cc}}${MACHINE}${PATH}
112${X_}_cc_hash:=	${${X_}_cc_hash:hash}
113# Only import if none of the vars are set somehow else.
114_can_export=	yes
115.for var in ${_exported_vars}
116.if defined(${var})
117_can_export=	no
118.endif
119.endfor
120.if ${_can_export} == yes
121.for var in ${_exported_vars}
122.if defined(${var}.${${X_}_cc_hash})
123${var}=	${${var}.${${X_}_cc_hash}}
124.endif
125.endfor
126.endif
127
128.if ${cc} == "CC" || (${cc} == "XCC" && ${XCC} != ${CC})
129.if ${MACHINE} == "common"
130# common is a pseudo machine for architecture independent
131# generated files - thus there is no compiler.
132${X_}COMPILER_TYPE= none
133${X_}COMPILER_VERSION= 0
134${X_}COMPILER_FREEBSD_VERSION= 0
135.elif !defined(${X_}COMPILER_TYPE) || !defined(${X_}COMPILER_VERSION)
136_v!=	${${cc}} --version || echo 0.0.0
137
138.if !defined(${X_}COMPILER_TYPE)
139. if ${${cc}:T:M*gcc*}
140${X_}COMPILER_TYPE:=	gcc
141. elif ${${cc}:T:M*clang*}
142${X_}COMPILER_TYPE:=	clang
143. elif ${_v:Mgcc}
144${X_}COMPILER_TYPE:=	gcc
145. elif ${_v:M\(GCC\)}
146${X_}COMPILER_TYPE:=	gcc
147. elif ${_v:Mclang}
148${X_}COMPILER_TYPE:=	clang
149. else
150.error Unable to determine compiler type for ${cc}=${${cc}}.  Consider setting ${X_}COMPILER_TYPE.
151. endif
152.endif
153.if !defined(${X_}COMPILER_VERSION)
154${X_}COMPILER_VERSION!=echo "${_v:M[1-9].[0-9]*}" | awk -F. '{print $$1 * 10000 + $$2 * 100 + $$3;}'
155.endif
156.undef _v
157.endif
158.if !defined(${X_}COMPILER_FREEBSD_VERSION)
159${X_}COMPILER_FREEBSD_VERSION!=	{ echo "__FreeBSD_cc_version" | ${${cc}} -E - 2>/dev/null || echo __FreeBSD_cc_version; } | tail -n 1
160# If we get a literal "__FreeBSD_cc_version" back then the compiler
161# is a non-FreeBSD build that doesn't support it or some other error
162# occurred.
163.if ${${X_}COMPILER_FREEBSD_VERSION} == "__FreeBSD_cc_version"
164${X_}COMPILER_FREEBSD_VERSION=	unknown
165.endif
166.endif
167
168.if ${${X_}COMPILER_TYPE} == "clang" || \
169	(${${X_}COMPILER_TYPE} == "gcc" && ${${X_}COMPILER_VERSION} >= 40800)
170${X_}COMPILER_FEATURES=	c++11
171.else
172${X_}COMPILER_FEATURES=
173.endif
174
175.else
176# Use CC's values
177X_COMPILER_TYPE=	${COMPILER_TYPE}
178X_COMPILER_VERSION=	${COMPILER_VERSION}
179X_COMPILER_FREEBSD_VERSION=	${COMPILER_FREEBSD_VERSION}
180X_COMPILER_FEATURES=	${COMPILER_FEATURES}
181.endif	# ${cc} == "CC" || (${cc} == "XCC" && ${XCC} != ${CC})
182
183# Export the values so sub-makes don't have to look them up again, using the
184# hash key computed above.
185.for var in ${_exported_vars}
186${var}.${${X_}_cc_hash}:=	${${var}}
187.export-env ${var}.${${X_}_cc_hash}
188.undef ${var}.${${X_}_cc_hash}
189.endfor
190
191.endif	# ${cc} == "CC" || !empty(XCC)
192.endfor	# .for cc in CC XCC
193
194.endif	# !target(__<bsd.compiler.mk>__)
195