Bug 14554

Summary: Enhancement: go.bbclass: allow appending to -ldflags flag
Product: [Build System, Metadata & Runtime] OE-Core Reporter: Ahmed Hossam <Ahmed.Hossam>
Component: oe-core otherAssignee: Ahmed Hossam <Ahmed.Hossam>
Status: RESOLVED FIXED QA Contact:
Severity: enhancement    
Priority: Medium CC: randy.macleod, richard.purdie
Version: 3.4   
Target Milestone: 4.0   
Hardware: x86   
OS: Multiple   
Whiteboard:
OS type for building Yocto: --- Type of Regression: ---
Verified: Documentation change: Yes (doc changes required)

Description Ahmed Hossam 2021-09-17 08:06:10 UTC
Add a variable to allow adding extra parameters to -ldflags in the GO_LDFLAGS variable, the main usecase is setting the application version similar to https://www.digitalocean.com/community/tutorials/using-ldflags-to-set-version-information-for-go-applications

http://git.yoctoproject.org/cgit/cgit.cgi/poky/tree/meta/classes/go.bbclass?id=be665a79831c3dab7623e75d112bb35d72e39a82#n44
Current implementation:
GO_LDFLAGS ?= '-ldflags="${GO_RPATH} ${GO_LINKMODE} -extldflags '${GO_EXTLDFLAGS}'"'
Suggestion:
GO_LDFLAGS ?= '-ldflags="${GO_RPATH} ${GO_LINKMODE} ${GO_EXTRA_LDFLAGS} -extldflags '${GO_EXTLDFLAGS}'"'

Then in the recipe something like GO_EXTRA_LDFLAGS="-X main.Version=v1.0.0" or GO_EXTRA_LDFLAGS="-X main.Version=${PV}" can be used


Current workaround: append the flag to GO_LINKMODE
Comment 1 Richard Purdie 2021-09-23 13:38:20 UTC
Can't you use GO_LDFLAGS:append = "xxx" for this?
Comment 2 Randy MacLeod 2021-09-23 14:39:02 UTC
Ahmen, does Richard's suggestion work for you? If so please close this enhancement.
Comment 3 Ahmed Hossam 2021-09-23 14:49:57 UTC
Unfortunately it doesn't work.

The goal is to add parameters inside the '-ldflags' quote, I tried using your suggestion using:
GO_LDFLAGS:append = ' -ldflags="-X main.Version=v1.0.0"'
Which would result in
GO_LDFLAGS = '-ldflags="${GO_RPATH} ${GO_LINKMODE} -extldflags '${GO_EXTLDFLAGS}'" -ldflags="-X main.Version=v1.0.0"'
But that overrode the previous '-ldflags' content, breaking qa

You can try this by adding
GO_LDFLAGS:append = ' -ldflags="-X main.Version=v1.0.0"'
to the end of poky/meta/recipes-extended/go-examples/go-helloworld_0.1.bb and building "bitbake go-helloworld", it will result in qa failures (do_package_qa)

That is why I am suggesting to add a variable *inside* the '-ldflags' quote, i.e. to result in:
GO_LDFLAGS = '-ldflags="${GO_RPATH} ${GO_LINKMODE} -X main.Version=v1.0.0 -extldflags '${GO_EXTLDFLAGS}'"'
with one occurence of '-ldflags'
Maybe someone with better go linker understanding can share a better solution.
Comment 4 Randy MacLeod 2021-09-30 15:17:17 UTC
Ahmed, can you send the patch to the list?
Comment 6 Ahmed Hossam 2021-11-01 10:50:06 UTC
Hey Randy, do I need to assign the bug to someone for integration?
Comment 7 Randy MacLeod 2021-11-01 17:39:09 UTC
Hi Ahmen,

Thanks for sending the patch to the list. It can take a while for changes to make it through our QA process and when we are doing a release for 3.4 which was going on last week, it can take even longer. 

You patch is merged now though:

https://git.openembedded.org/openembedded-core/commit/?id=eaa7a61dab9a1d7bb039f16abdd9aacb44faa595

so I've marked this issue as resolved/fixed.

Thanks!