xref: /freebsd/share/mk/bsd.linker.mk (revision c685956956793831f724177309eace82c4b347b9)
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 || echo none) | head -n 1
51.if ${_ld_version} == "none"
52.warning 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.warning Unknown linker from ${ld}=${${ld}}: ${_ld_version}, defaulting to bfd
62${X_}LINKER_TYPE=	bfd
63_v=	2.17.50
64.endif
65${X_}LINKER_VERSION!=	echo "${_v:M[1-9].[0-9]*}" | \
66			  awk -F. '{print $$1 * 10000 + $$2 * 100 + $$3;}'
67.undef _ld_version
68.undef _v
69${X_}LINKER_FEATURES=
70.if ${${X_}LINKER_TYPE} != "bfd" || ${${X_}LINKER_VERSION} > 21750
71${X_}LINKER_FEATURES+=	build-id
72.endif
73.if ${${X_}LINKER_TYPE} != "lld" || ${${X_}LINKER_VERSION} >= 50000
74${X_}LINKER_FEATURES+=	filter
75.endif
76.endif
77.else
78# Use LD's values
79X_LINKER_TYPE=		${LINKER_TYPE}
80X_LINKER_VERSION=	${LINKER_VERSION}
81X_LINKER_FEATURES=	${LINKER_FEATURES}
82.endif	# ${ld} == "LD" || (${ld} == "XLD" && ${XLD} != ${LD})
83
84# Export the values so sub-makes don't have to look them up again, using the
85# hash key computed above.
86.for var in ${_exported_vars}
87${var}.${${X_}_ld_hash}:=	${${var}}
88.export-env ${var}.${${X_}_ld_hash}
89.undef ${var}.${${X_}_ld_hash}
90.endfor
91
92.endif	# ${ld} == "LD" || !empty(XLD)
93.endfor	# .for ld in LD XLD
94
95
96.endif	# !target(__<bsd.linker.mk>__)
97