Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to override build flags by goos/goarch/etc #2860

Merged
merged 10 commits into from Feb 2, 2022
Merged

Conversation

caarlos0
Copy link
Member

@caarlos0 caarlos0 commented Jan 30, 2022

refs #2829

allow to override ldflags et al based on the build target

TODO

@caarlos0 caarlos0 added the enhancement New feature or request label Jan 30, 2022
@caarlos0 caarlos0 self-assigned this Jan 30, 2022
@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jan 30, 2022
@vercel vercel bot temporarily deployed to Preview January 30, 2022 19:24 Inactive
@codecov
Copy link

codecov bot commented Jan 30, 2022

Codecov Report

Merging #2860 (27df8d2) into main (9d481d4) will increase coverage by 84.45%.
The diff coverage is 81.81%.

Impacted file tree graph

@@            Coverage Diff            @@
##           main    #2860       +/-   ##
=========================================
+ Coverage      0   84.45%   +84.45%     
=========================================
  Files         0      111      +111     
  Lines         0     8837     +8837     
=========================================
+ Hits          0     7463     +7463     
- Misses        0     1105     +1105     
- Partials      0      269      +269     
Impacted Files Coverage Δ
pkg/config/config.go 87.15% <ø> (ø)
internal/builders/golang/build.go 90.41% <81.81%> (ø)
internal/client/mock.go 87.27% <0.00%> (ø)
internal/pipe/teams/teams.go 52.00% <0.00%> (ø)
internal/pipe/docker/manifest.go 96.55% <0.00%> (ø)
pkg/archive/tar/tar.go 81.63% <0.00%> (ø)
internal/pipe/linkedin/linkedin.go 53.33% <0.00%> (ø)
internal/pipe/dist/dist.go 90.32% <0.00%> (ø)
internal/pipe/gomod/gomod.go 100.00% <0.00%> (ø)
pkg/archive/targz/targz.go 80.00% <0.00%> (ø)
... and 103 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9d481d4...27df8d2. Read the comment docs.

@bflad
Copy link
Contributor

bflad commented Jan 31, 2022

Hey @caarlos0 👋 This looks like its going in the right direction, although I'm having a little trouble verifying it after installing from this branch. It seems to be losing all the build configuration in the go build command for me or not including the override configuration.

Configuration:

builds:
  - flags:
      - -trimpath
    goos:
      - darwin
      - freebsd
      - linux
      - windows
    goarch:
      - '386'
      - amd64
      - arm
      - arm64
    ignore:
      - goarch: arm
        goos: windows
      - goarch: arm64
        goos: freebsd
      - goarch: arm64
        goos: windows
    ldflags:
      - -s -w -X main.Version={{.Version}}
    mod_timestamp: '{{ .CommitTimestamp }}'
    # overrides:
    #   - goos: darwin
    #     goarch: amd64
    #     tags:
    #       - netcgo

Environment:

go env GOOS          
darwingo env GOARCH
amd64

Command:

goreleaser build --snapshot --rm-dist --single-target --debug

v1.4.1 output:

      • running                   cmd=[go build -trimpath -ldflags=-s -w -X main.Version=v0.1.3-next -o /Users/bflad/src/github.com/bflad/terraform-provider-framework/dist/terraform-provider-framework_darwin_amd64/terraform-provider-framework .]

buildconf output (no overrides):

      • running                   cmd=[go build -trimpath -ldflags=-s -w -X main.Version=v0.1.3-next -o /Users/bflad/src/github.com/bflad/terraform-provider-framework/dist/terraform-provider-framework_darwin_amd64/terraform-provider-framework .]

buildconf output (with overrides uncommented, no goarm or gomips in override):

     • running                   cmd=[go build -ldflags= -o /Users/bflad/src/github.com/bflad/terraform-provider-framework/dist/terraform-provider-framework_darwin_amd64/terraform-provider-framework .]

buildconf output (with overrides uncommented and goarm: 6 and gomips: hardfloat in override):

      • running                   cmd=[go build -trimpath -ldflags=-s -w -X main.Version=v0.1.3-next -o /Users/bflad/src/github.com/bflad/terraform-provider-framework/dist/terraform-provider-framework_darwin_amd64/terraform-provider-framework .]

Not sure about the missing tags in the last one, but I'm wondering if

if len(build.Goarm) == 0 {
build.Goarm = []string{"6"}
}
if len(build.Gomips) == 0 {
build.Gomips = []string{"hardfloat"}
}
has anything to do with the target matching.

Signed-off-by: Carlos A Becker <caarlos0@gmail.com>
Signed-off-by: Carlos A Becker <caarlos0@gmail.com>
@vercel vercel bot temporarily deployed to Preview February 1, 2022 01:38 Inactive
@caarlos0
Copy link
Member Author

caarlos0 commented Feb 1, 2022

pushed some changes... it should now merge overrides into the build values, so it won't loose configs anymore

also, you can now template the goos/goarch/etc on overrides as well, in which you can for example use {{ .Runtime.Goos }}

Signed-off-by: Carlos A Becker <caarlos0@gmail.com>
@vercel vercel bot temporarily deployed to Preview February 1, 2022 02:57 Inactive
Copy link
Contributor

@bflad bflad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like its now selecting and merging correctly 🎉 although I think the build command logic might still need some updates to ensure its checking the details/overrides 😄

internal/builders/golang/build.go Outdated Show resolved Hide resolved
internal/builders/golang/build.go Outdated Show resolved Hide resolved
Co-authored-by: Brian Flad <bflad417@gmail.com>
@vercel vercel bot temporarily deployed to Preview February 1, 2022 16:26 Inactive
Co-authored-by: Brian Flad <bflad417@gmail.com>
@vercel vercel bot temporarily deployed to Preview February 1, 2022 16:27 Inactive
Copy link
Contributor

@bflad bflad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is working as I would anticipate! 🎉 Thanks so much.

Totally fine as something in the future: I'm also wondering if env should allow similar overrides since I don't believe it can be templated and setting CGO_ENABLED between platforms (or other go command environment variables) is a bit awkward. I understand that is more difficult to merge/manage in the code though and happy to submit a followup issue/pull request. 👍

@caarlos0
Copy link
Member Author

caarlos0 commented Feb 1, 2022

This is working as I would anticipate! 🎉 Thanks so much.

🚀🚀🚀🚀

Totally fine as something in the future: I'm also wondering if env should allow similar overrides since I don't believe it can be templated and setting CGO_ENABLED between platforms (or other go command environment variables) is a bit awkward. I understand that is more difficult to merge/manage in the code though and happy to submit a followup issue/pull request. 👍

I was thinking the same... and maybe also other options... but I think that yes, at least for env, we should do it.

for envs the endeavor is slightly bigger, as we need to care for build hooks as well https://goreleaser.com/customization/build/#build-hooks

but I think we can work on it on a different PR once this one has better coverage and is merged

Signed-off-by: Carlos A Becker <caarlos0@gmail.com>
@vercel vercel bot temporarily deployed to Preview February 2, 2022 02:50 Inactive
@caarlos0 caarlos0 merged commit 1bdfc9c into main Feb 2, 2022
@caarlos0 caarlos0 deleted the buildconf branch February 2, 2022 03:01
@github-actions github-actions bot added this to the v1.5.0 milestone Feb 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants