xref: /freebsd/share/mk/bsd.linker.mk (revision 6f81bd02b3f6a3b48dbfc6416288346b97e87d7c)
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# These variables with an X_ prefix will also be provided if XLD is set.
13#
14# This file may be included multiple times, but only has effect the first time.
15#
16
17.if !target(__<bsd.linker.mk>__)
18__<bsd.linker.mk>__:
19
20.for ld X_ in LD $${_empty_var_} XLD X_
21.if ${ld} == "LD" || !empty(XLD)
22# Try to import LINKER_TYPE and LINKER_VERSION from parent make.
23# The value is only used/exported for the same environment that impacts
24# LD and LINKER_* settings here.
25_exported_vars=	${X_}LINKER_TYPE ${X_}LINKER_VERSION
26${X_}_ld_hash=	${${ld}}${MACHINE}${PATH}
27${X_}_ld_hash:=	${${X_}_ld_hash:hash}
28# Only import if none of the vars are set somehow else.
29_can_export=	yes
30.for var in ${_exported_vars}
31.if defined(${var})
32_can_export=	no
33.endif
34.endfor
35.if ${_can_export} == yes
36.for var in ${_exported_vars}
37.if defined(${var}.${${X_}_ld_hash})
38${var}=	${${var}.${${X_}_ld_hash}}
39.endif
40.endfor
41.endif
42
43.if ${ld} == "LD" || (${ld} == "XLD" && ${XLD} != ${LD})
44.if !defined(${X_}LINKER_TYPE) || !defined(${X_}LINKER_VERSION)
45_ld_version!=	${${ld}} --version 2>/dev/null | head -n 1 || echo none
46.if ${_ld_version} == "none"
47.error Unable to determine linker type from ${ld}=${${ld}}
48.endif
49.if ${_ld_version:[1..2]} == "GNU ld"
50${X_}LINKER_TYPE=	binutils
51_v=	${_ld_version:M[1-9].[0-9]*:[1]}
52.elif ${_ld_version:[1]} == "LLD"
53${X_}LINKER_TYPE=	lld
54_v=	${_ld_version:[2]}
55.else
56.error Unknown linker from ${ld}=${${ld}}: ${_ld_version}
57.endif
58${X_}LINKER_VERSION!=	echo "${_v:M[1-9].[0-9]*}" | \
59			  awk -F. '{print $$1 * 10000 + $$2 * 100 + $$3;}'
60.undef _ld_version
61.undef _v
62.endif
63.else
64# Use LD's values
65X_LINKER_TYPE=		${LINKER_TYPE}
66X_LINKER_VERSION=	${LINKER_VERSION}
67.endif	# ${ld} == "LD" || (${ld} == "XLD" && ${XLD} != ${LD})
68
69# Export the values so sub-makes don't have to look them up again, using the
70# hash key computed above.
71.for var in ${_exported_vars}
72${var}.${${X_}_ld_hash}:=	${${var}}
73.export-env ${var}.${${X_}_ld_hash}
74.undef ${var}.${${X_}_ld_hash}
75.endfor
76
77.endif	# ${ld} == "LD" || !empty(XLD)
78.endfor	# .for ld in LD XLD
79
80
81.endif	# !target(__<bsd.linker.mk>__)
82