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