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

Fix ldflags in GOFLAGS not being respected. #17

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
25 changes: 16 additions & 9 deletions cmd/fyne/internal/commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,14 @@ func (b *Builder) build() error {
}

args := []string{"build"}
env := os.Environ()

ldFlags, env := getEnvWithLDFlagsExtracted()

if goos == "darwin" {
appendEnv(&env, "CGO_CFLAGS", "-mmacosx-version-min=10.13")
appendEnv(&env, "CGO_LDFLAGS", "-mmacosx-version-min=10.13")
}

ldFlags := extractLdflagsFromGoFlags()
if !isWeb(goos) {
env = append(env, "CGO_ENABLED=1") // in case someone is trying to cross-compile...

Expand Down Expand Up @@ -399,17 +399,24 @@ func appendEnv(env *[]string, varName, value string) {
*env = append(*env, varName+"="+value)
}

func extractLdflagsFromGoFlags() string {
goFlags := os.Getenv("GOFLAGS")
// getEnvWithLDFlagsExtracted extracts the ldFlags from the GOFLAGS and returns the environment with those flags removed.
func getEnvWithLDFlagsExtracted() (string, []string) {
env := os.Environ()
var initialGoFlags string
for i, str := range env {
if strings.HasPrefix(str, "GOFLAGS=") {
initialGoFlags = strings.TrimPrefix(str, "GOFLAGS=")
env = append(env[:i], env[i+1:]...)
break
}
}

ldFlags, goFlags := extractLdFlags(goFlags)
ldFlags, goFlags := extractLdFlags(initialGoFlags)
if goFlags != "" {
os.Setenv("GOFLAGS", goFlags)
} else {
os.Unsetenv("GOFLAGS")
env = append(env, "GOFLAGS="+goFlags)
}

return ldFlags
return ldFlags, env
}

func extractLdFlags(goFlags string) (string, string) {
Expand Down