From dcb5d1d91849bf3e22639b38657bdae6e50767d1 Mon Sep 17 00:00:00 2001 From: "Yann E. MORIN" Date: Wed, 5 Feb 2025 14:58:23 +0100 Subject: [PATCH] package/pkg-golang: allow packages to provide extldflags Currently, only the -static extldflags may be set in the golang-package infra. However, in some cases, it might be necessary to pass other flags, either because they are needed on a specific architecture, or a specific C library; packages may also have a need to pass arbitrary linker flags when they use CGO linking. For example, on AArch64, go forcibly uses ld.gold, but it is not available in all toolchains, and ld.bfd works nowadays (following patch); another case is musl, where PIE is not supported with go. Introduce FOO_EXTLDFLAGS, which we use to set those flags, and that packages can set to pass such flags. Migrate the current setting of -static to use that new variable. Signed-off-by: Yann E. MORIN Tested-by: Nathaniel Roach Signed-off-by: Fiona Klute (WIWA) Reviewed-by: Yann E. MORIN Reviewed-by: Christian Stewart Signed-off-by: Romain Naour --- docs/manual/adding-packages-golang.adoc | 6 ++++-- package/pkg-golang.mk | 6 +++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/docs/manual/adding-packages-golang.adoc b/docs/manual/adding-packages-golang.adoc index aa25426591..6de0916a83 100644 --- a/docs/manual/adding-packages-golang.adoc +++ b/docs/manual/adding-packages-golang.adoc @@ -84,8 +84,10 @@ therefore only use a few of them, or none. infrastructure will automatically generate a minimal +go.mod+ file in the package source tree if it doesn't exist. -* +FOO_LDFLAGS+ and +FOO_TAGS+ can be used to pass respectively the - +LDFLAGS+ or the +TAGS+ to the +go+ build command. +* +FOO_LDFLAGS+, +FOO_EXTLDFLAGS+, and +FOO_TAGS+ can be used to pass + respectively the go +LDFLAGS+ (via the `-ldflags` command line flag), + the external linker flags +EXTLDFLAGS+ (via the `-extldflags` + command line flag), or the +TAGS+ to the +go+ build command. * +FOO_BUILD_TARGETS+ can be used to pass the list of targets that should be built. If +FOO_BUILD_TARGETS+ is not specified, it diff --git a/package/pkg-golang.mk b/package/pkg-golang.mk index 33cf9bfb1a..a3421af288 100644 --- a/package/pkg-golang.mk +++ b/package/pkg-golang.mk @@ -114,10 +114,14 @@ ifndef $(2)_BUILD_CMDS ifeq ($(4),target) ifeq ($(BR2_STATIC_LIBS),y) -$(2)_LDFLAGS += -extldflags '-static' +$(2)_EXTLDFLAGS += -static $(2)_TAGS += osusergo netgo endif +ifneq ($$($(2)_EXTLDFLAGS),) +$(2)_LDFLAGS += -extldflags '$$($(2)_EXTLDFLAGS)' +endif + # Build package for target define $(2)_BUILD_CMDS $$(foreach d,$$($(2)_BUILD_TARGETS),\