Skip to content
Open
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
39 changes: 2 additions & 37 deletions cmd/fyne/internal/commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"fyne.io/tools/cmd/fyne/internal/metadata"
"fyne.io/tools/cmd/fyne/internal/templates"
utils "fyne.io/tools/cmd/fyne/internal/util"
)

// Builder generate the executables.
Expand Down Expand Up @@ -170,7 +171,7 @@ func (b *Builder) build() error {
appendEnv(&env, "CGO_LDFLAGS", "-mmacosx-version-min=10.13")
}

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

Expand Down Expand Up @@ -372,42 +373,6 @@ func appendEnv(env *[]string, varName, value string) {
*env = append(*env, varName+"="+value)
}

func extractLdflagsFromGoFlags() string {
goFlags := os.Getenv("GOFLAGS")

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

return ldFlags
}

func extractLdFlags(goFlags string) (string, string) {
if goFlags == "" {
return "", ""
}

flags := strings.Fields(goFlags)
ldflags := ""
newGoFlags := ""

for _, flag := range flags {
if strings.HasPrefix(flag, "-ldflags=") {
ldflags += strings.TrimPrefix(flag, "-ldflags=") + " "
} else {
newGoFlags += flag + " "
}
}

ldflags = strings.TrimSpace(ldflags)
newGoFlags = strings.TrimSpace(newGoFlags)

return ldflags, newGoFlags
}

func normaliseVersion(str string) string {
if str == "master" {
return str
Expand Down
21 changes: 0 additions & 21 deletions cmd/fyne/internal/commands/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,27 +144,6 @@ func Test_AppendEnv(t *testing.T) {
}
}

type extractTest struct {
value string
wantLdFlags string
wantGoFlags string
}

func Test_ExtractLdFlags(t *testing.T) {
goFlagsTests := []extractTest{
{"-ldflags=-w", "-w", ""},
{"-ldflags=-s", "-s", ""},
{"-ldflags=-w -ldflags=-s", "-w -s", ""},
{"-mod=vendor", "", "-mod=vendor"},
}

for _, test := range goFlagsTests {
ldFlags, goFlags := extractLdFlags(test.value)
assert.Equal(t, test.wantLdFlags, ldFlags)
assert.Equal(t, test.wantGoFlags, goFlags)
}
}

func Test_NormaliseVersion(t *testing.T) {
assert.Equal(t, "master", normaliseVersion("master"))
assert.Equal(t, "2.3.0.0", normaliseVersion("v2.3"))
Expand Down
6 changes: 6 additions & 0 deletions cmd/fyne/internal/mobile/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ func envInit() (err error) {
// bitcodeEnabled = true
//}

// Set buildLdflags based on GOFLAGS env.
bldf, goflags := util.ExtractLdFlags(goEnv("GOFLAGS"))
buildLdflags = strings.Join([]string{buildLdflags, bldf}, " ")

// Setup the cross-compiler environments.
if ndkRoot, err := ndkRoot(); err == nil {
androidEnv = make(map[string][]string)
Expand Down Expand Up @@ -162,6 +166,7 @@ func envInit() (err error) {
androidEnv[arch] = []string{
"GOOS=android",
"GOARCH=" + arch,
"GOFLAGS=" + goflags,
"CC=" + clang,
"CXX=" + clangpp,
"CGO_ENABLED=1",
Expand Down Expand Up @@ -215,6 +220,7 @@ func envInit() (err error) {
env = append(env,
"GOOS="+os,
"GOARCH="+arch,
"GOFLAGS="+goflags,
"CC="+clang,
"CXX="+clang+"++",
"CGO_CFLAGS="+cflags+" -arch "+archClang(arch),
Expand Down
40 changes: 40 additions & 0 deletions cmd/fyne/internal/util/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package util

import "strings"

// ExtractLdflagsFromGoFlags returns the ldflags and environment with ldflags removed from GOFLAGS.
func ExtractLdflagsFromGoFlags(env []string) (string, []string) {
prefix := "GOFLAGS="
for i, v := range env {
if strings.HasPrefix(v, prefix) {
ldflags, goflags := ExtractLdFlags(strings.TrimPrefix(v, prefix))
env[i] = prefix + goflags
return ldflags, env
}
}
return "", env
}

// ExtractLdFlags extracts ldflags from the value of GOFLAGS environment variable, returns ldflags and new GOFLAGS.
func ExtractLdFlags(goFlags string) (string, string) {
if goFlags == "" {
return "", ""
}

flags := strings.Fields(goFlags)
ldflags := ""
newGoFlags := ""

for _, flag := range flags {
if strings.HasPrefix(flag, "-ldflags=") {
ldflags += strings.TrimPrefix(flag, "-ldflags=") + " "
} else {
newGoFlags += flag + " "
}
}

ldflags = strings.TrimSpace(ldflags)
newGoFlags = strings.TrimSpace(newGoFlags)

return ldflags, newGoFlags
}
28 changes: 28 additions & 0 deletions cmd/fyne/internal/util/env_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package util

import (
"testing"

"github.com/stretchr/testify/assert"
)

type extractTest struct {
value string
wantLdFlags string
wantGoFlags string
}

func Test_ExtractLdFlags(t *testing.T) {
goFlagsTests := []extractTest{
{"-ldflags=-w", "-w", ""},
{"-ldflags=-s", "-s", ""},
{"-ldflags=-w -ldflags=-s", "-w -s", ""},
{"-mod=vendor", "", "-mod=vendor"},
}

for _, test := range goFlagsTests {
ldFlags, goFlags := ExtractLdFlags(test.value)
assert.Equal(t, test.wantLdFlags, ldFlags)
assert.Equal(t, test.wantGoFlags, goFlags)
}
}