xref: /freebsd/share/mk/bsd.linker.mk (revision 6f81bd02b3f6a3b48dbfc6416288346b97e87d7c)
1125902b6SBryan Drewery# $FreeBSD$
2125902b6SBryan Drewery
3125902b6SBryan Drewery# Setup variables for the linker.
4125902b6SBryan Drewery#
5125902b6SBryan Drewery# LINKER_TYPE is the major type of linker. Currently binutils and lld support
6125902b6SBryan Drewery# automatic detection.
7125902b6SBryan Drewery#
8125902b6SBryan Drewery# LINKER_VERSION is a numeric constant equal to:
9125902b6SBryan Drewery#     major * 10000 + minor * 100 + tiny
10125902b6SBryan Drewery# It too can be overridden on the command line.
11125902b6SBryan Drewery#
1208154765SBryan Drewery# These variables with an X_ prefix will also be provided if XLD is set.
1308154765SBryan Drewery#
14125902b6SBryan Drewery# This file may be included multiple times, but only has effect the first time.
15125902b6SBryan Drewery#
16125902b6SBryan Drewery
17125902b6SBryan Drewery.if !target(__<bsd.linker.mk>__)
18125902b6SBryan Drewery__<bsd.linker.mk>__:
19125902b6SBryan Drewery
2008154765SBryan Drewery.for ld X_ in LD $${_empty_var_} XLD X_
2108154765SBryan Drewery.if ${ld} == "LD" || !empty(XLD)
2219fad655SBryan Drewery# Try to import LINKER_TYPE and LINKER_VERSION from parent make.
2319fad655SBryan Drewery# The value is only used/exported for the same environment that impacts
2419fad655SBryan Drewery# LD and LINKER_* settings here.
2519fad655SBryan Drewery_exported_vars=	${X_}LINKER_TYPE ${X_}LINKER_VERSION
2619fad655SBryan Drewery${X_}_ld_hash=	${${ld}}${MACHINE}${PATH}
2719fad655SBryan Drewery${X_}_ld_hash:=	${${X_}_ld_hash:hash}
2819fad655SBryan Drewery# Only import if none of the vars are set somehow else.
2919fad655SBryan Drewery_can_export=	yes
3019fad655SBryan Drewery.for var in ${_exported_vars}
3119fad655SBryan Drewery.if defined(${var})
3219fad655SBryan Drewery_can_export=	no
3319fad655SBryan Drewery.endif
3419fad655SBryan Drewery.endfor
3519fad655SBryan Drewery.if ${_can_export} == yes
3619fad655SBryan Drewery.for var in ${_exported_vars}
3719fad655SBryan Drewery.if defined(${var}.${${X_}_ld_hash})
3819fad655SBryan Drewery${var}=	${${var}.${${X_}_ld_hash}}
3919fad655SBryan Drewery.endif
4019fad655SBryan Drewery.endfor
4119fad655SBryan Drewery.endif
4219fad655SBryan Drewery
4308154765SBryan Drewery.if ${ld} == "LD" || (${ld} == "XLD" && ${XLD} != ${LD})
44ec39013eSBryan Drewery.if !defined(${X_}LINKER_TYPE) || !defined(${X_}LINKER_VERSION)
4508154765SBryan Drewery_ld_version!=	${${ld}} --version 2>/dev/null | head -n 1 || echo none
46125902b6SBryan Drewery.if ${_ld_version} == "none"
4708154765SBryan Drewery.error Unable to determine linker type from ${ld}=${${ld}}
48125902b6SBryan Drewery.endif
49125902b6SBryan Drewery.if ${_ld_version:[1..2]} == "GNU ld"
5008154765SBryan Drewery${X_}LINKER_TYPE=	binutils
5189bf31fdSBryan Drewery_v=	${_ld_version:M[1-9].[0-9]*:[1]}
52125902b6SBryan Drewery.elif ${_ld_version:[1]} == "LLD"
5308154765SBryan Drewery${X_}LINKER_TYPE=	lld
54125902b6SBryan Drewery_v=	${_ld_version:[2]}
55125902b6SBryan Drewery.else
5608154765SBryan Drewery.error Unknown linker from ${ld}=${${ld}}: ${_ld_version}
57125902b6SBryan Drewery.endif
5808154765SBryan Drewery${X_}LINKER_VERSION!=	echo "${_v:M[1-9].[0-9]*}" | \
5908154765SBryan Drewery			  awk -F. '{print $$1 * 10000 + $$2 * 100 + $$3;}'
6008154765SBryan Drewery.undef _ld_version
6108154765SBryan Drewery.undef _v
62ec39013eSBryan Drewery.endif
63*6f81bd02SBryan Drewery.else
64*6f81bd02SBryan Drewery# Use LD's values
65*6f81bd02SBryan DreweryX_LINKER_TYPE=		${LINKER_TYPE}
66*6f81bd02SBryan DreweryX_LINKER_VERSION=	${LINKER_VERSION}
6708154765SBryan Drewery.endif	# ${ld} == "LD" || (${ld} == "XLD" && ${XLD} != ${LD})
6808154765SBryan Drewery
6919fad655SBryan Drewery# Export the values so sub-makes don't have to look them up again, using the
7019fad655SBryan Drewery# hash key computed above.
7119fad655SBryan Drewery.for var in ${_exported_vars}
7219fad655SBryan Drewery${var}.${${X_}_ld_hash}:=	${${var}}
7319fad655SBryan Drewery.export-env ${var}.${${X_}_ld_hash}
7419fad655SBryan Drewery.undef ${var}.${${X_}_ld_hash}
7519fad655SBryan Drewery.endfor
7619fad655SBryan Drewery
7708154765SBryan Drewery.endif	# ${ld} == "LD" || !empty(XLD)
7808154765SBryan Drewery.endfor	# .for ld in LD XLD
7908154765SBryan Drewery
80125902b6SBryan Drewery
81125902b6SBryan Drewery.endif	# !target(__<bsd.linker.mk>__)
82