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

Set GOAMD64 if variant is set #578

Merged
merged 1 commit into from
Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions pkg/build/gobuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,14 +335,20 @@ func buildEnv(platform v1.Platform, userEnv, configEnv []string) ([]string, erro
"GOOS=" + platform.OS,
"GOARCH=" + platform.Architecture,
}

if strings.HasPrefix(platform.Architecture, "arm") && platform.Variant != "" {
goarm, err := getGoarm(platform)
if err != nil {
return nil, fmt.Errorf("goarm failure: %w", err)
}
if goarm != "" {
env = append(env, "GOARM="+goarm)
if platform.Variant != "" {
switch platform.Architecture {
case "arm":
jonjohnsonjr marked this conversation as resolved.
Show resolved Hide resolved
// See: https://pkg.go.dev/cmd/go#hdr-Environment_variables
goarm, err := getGoarm(platform)
if err != nil {
return nil, fmt.Errorf("goarm failure: %w", err)
}
if goarm != "" {
env = append(env, "GOARM="+goarm)
}
case "amd64":
// See: https://tip.golang.org/doc/go1.18#amd64
env = append(env, "GOAMD64="+platform.Variant)
}
}

Expand Down
99 changes: 52 additions & 47 deletions pkg/build/gobuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,58 +221,63 @@ func TestBuildEnv(t *testing.T) {
userEnv []string
configEnv []string
expectedEnvs map[string]string
}{
{
description: "defaults",
platform: v1.Platform{
OS: "linux",
Architecture: "amd64",
},
expectedEnvs: map[string]string{
"GOOS": "linux",
"GOARCH": "amd64",
"CGO_ENABLED": "0",
},
}{{
description: "defaults",
platform: v1.Platform{
OS: "linux",
Architecture: "amd64",
},
{
description: "override a default value",
configEnv: []string{"CGO_ENABLED=1"},
expectedEnvs: map[string]string{
"CGO_ENABLED": "1",
},
expectedEnvs: map[string]string{
"GOOS": "linux",
"GOARCH": "amd64",
"CGO_ENABLED": "0",
},
{
description: "override an envvar and add an envvar",
userEnv: []string{"CGO_ENABLED=0"},
configEnv: []string{"CGO_ENABLED=1", "GOPRIVATE=git.internal.example.com,source.developers.google.com"},
expectedEnvs: map[string]string{
"CGO_ENABLED": "1",
"GOPRIVATE": "git.internal.example.com,source.developers.google.com",
},
}, {
description: "override a default value",
configEnv: []string{"CGO_ENABLED=1"},
expectedEnvs: map[string]string{
"CGO_ENABLED": "1",
},
{
description: "arm variant",
platform: v1.Platform{
Architecture: "arm",
Variant: "v7",
},
expectedEnvs: map[string]string{
"GOARCH": "arm",
"GOARM": "7",
},
}, {
description: "override an envvar and add an envvar",
userEnv: []string{"CGO_ENABLED=0"},
configEnv: []string{"CGO_ENABLED=1", "GOPRIVATE=git.internal.example.com,source.developers.google.com"},
expectedEnvs: map[string]string{
"CGO_ENABLED": "1",
"GOPRIVATE": "git.internal.example.com,source.developers.google.com",
},
{
description: "arm64 variant",
platform: v1.Platform{
Architecture: "arm64",
Variant: "v8",
},
expectedEnvs: map[string]string{
"GOARCH": "arm64",
"GOARM": "7",
},
}, {
description: "arm variant",
platform: v1.Platform{
Architecture: "arm",
Variant: "v7",
},
}
expectedEnvs: map[string]string{
"GOARCH": "arm",
"GOARM": "7",
},
}, {
// GOARM is ignored for arm64.
description: "arm64 variant",
platform: v1.Platform{
Architecture: "arm64",
Variant: "v8",
},
expectedEnvs: map[string]string{
"GOARCH": "arm64",
"GOARM": "",
},
}, {
description: "amd64 variant",
platform: v1.Platform{
Architecture: "amd64",
Variant: "v3",
},
expectedEnvs: map[string]string{
"GOARCH": "amd64",
"GOAMD64": "v3",
},
}}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
env, err := buildEnv(test.platform, test.userEnv, test.configEnv)
Expand Down