xref: /freebsd/share/mk/bsd.linker.mk (revision cdd89b9897bab7002b3fdbe3977357e01dc8546d)
1# $FreeBSD$
2
3# Setup variables for the linker.
4#
5# LINKER_TYPE is the major type of linker. Currently binutils and lld support
6# automatic detection.
7#
8# LINKER_VERSION is a numeric constant equal to:
9#     major * 10000 + minor * 100 + tiny
10# It too can be overridden on the command line.
11#
12# LINKER_FEATURES may contain one or more of the following, based on
13# linker support for that feature:
14#
15# - build-id : support for generating a Build-ID note
16#
17# These variables with an X_ prefix will also be provided if XLD is set.
18#
19# This file may be included multiple times, but only has effect the first time.
20#
21
22.if !target(__<bsd.linker.mk>__)
23__<bsd.linker.mk>__:
24
25.for ld X_ in LD $${_empty_var_} XLD X_
26.if ${ld} == "LD" || !empty(XLD)
27# Try to import LINKER_TYPE and LINKER_VERSION from parent make.
28# The value is only used/exported for the same environment that impacts
29# LD and LINKER_* settings here.
30_exported_vars=	${X_}LINKER_TYPE ${X_}LINKER_VERSION ${X_}LINKER_FEATURES
31${X_}_ld_hash=	${${ld}}${MACHINE}${PATH}
32${X_}_ld_hash:=	${${X_}_ld_hash:hash}
33# Only import if none of the vars are set somehow else.
34_can_export=	yes
35.for var in ${_exported_vars}
36.if defined(${var})
37_can_export=	no
38.endif
39.endfor
40.if ${_can_export} == yes
41.for var in ${_exported_vars}
42.if defined(${var}.${${X_}_ld_hash})
43${var}=	${${var}.${${X_}_ld_hash}}
44.endif
45.endfor
46.endif
47
48.if ${ld} == "LD" || (${ld} == "XLD" && ${XLD} != ${LD})
49.if !defined(${X_}LINKER_TYPE) || !defined(${X_}LINKER_VERSION)
50_ld_version!=	${${ld}} --version 2>/dev/null | head -n 1 || echo none
51.if ${_ld_version} == "none"
52.error Unable to determine linker type from ${ld}=${${ld}}
53.endif
54.if ${_ld_version:[1..2]} == "GNU ld"
55${X_}LINKER_TYPE=	bfd
56_v=	${_ld_version:M[1-9].[0-9]*:[1]}
57.elif ${_ld_version:[1]} == "LLD"
58${X_}LINKER_TYPE=	lld
59_v=	${_ld_version:[2]}
60.else
61.error Unknown linker from ${ld}=${${ld}}: ${_ld_version}
62.endif
63${X_}LINKER_VERSION!=	echo "${_v:M[1-9].[0-9]*}" | \
64			  awk -F. '{print $$1 * 10000 + $$2 * 100 + $$3;}'
65.undef _ld_version
66.undef _v
67${X_}LINKER_FEATURES=
68.if ${${X_}LINKER_TYPE} != "bfd" || ${${X_}LINKER_VERSION} > 21750
69${X_}LINKER_FEATURES+=	build-id
70.endif
71.endif
72.else
73# Use LD's values
74X_LINKER_TYPE=		${LINKER_TYPE}
75X_LINKER_VERSION=	${LINKER_VERSION}
76X_LINKER_FEATURES=	${LINKER_FEATURES}
77.endif	# ${ld} == "LD" || (${ld} == "XLD" && ${XLD} != ${LD})
78
79# Export the values so sub-makes don't have to look them up again, using the
80# hash key computed above.
81.for var in ${_exported_vars}
82${var}.${${X_}_ld_hash}:=	${${var}}
83.export-env ${var}.${${X_}_ld_hash}
84.undef ${var}.${${X_}_ld_hash}
85.endfor
86
87.endif	# ${ld} == "LD" || !empty(XLD)
88.endfor	# .for ld in LD XLD
89
90
91.endif	# !target(__<bsd.linker.mk>__)
92