Permalink
Browse files

Add a way to add extra -ldflags

  • Loading branch information...
frioux committed Jan 14, 2019
1 parent b61ac0e commit 6cf585e0ac075996c50636c2812d2a3c4f6f39ee
Showing with 136 additions and 24 deletions.
  1. +11 −1 README.md
  2. +45 −0 astilectron-bundler/ldflags.go
  3. +25 −0 astilectron-bundler/ldflags_test.go
  4. +8 −0 astilectron-bundler/main.go
  5. +10 −23 bundler.go
  6. +37 −0 ldflags.go
@@ -123,6 +123,16 @@ For each environment you specify in your configuration file, **astilectron-bundl
- `AppName`: filled with the configuration app name
- `BuiltAt`: filled with the date the build has been done at

If you need to add more flags yourself, like for a version number, add something
like this to your `astilectron-bundler` command: `-ldflags X:main.Version=xyzzy`.

If you need to add multiple flags you can pass `-ldflags` multiple times, with
multiple values split on commas, like this:

`-ldflags X:main.Version=xyzzy,main.CommitCount=100 -ldflags race`

That would set two variables and enable the race detection.

# Subcommands
## Only bind data: bd

@@ -134,4 +144,4 @@ Use this subcommand if you want to skip most of the bundling process and only bi

The **bundler** stores downloaded files in a cache to avoid downloading them over and over again. That cache may be corrupted. In that case, use this subcommand to clear the cache:

$ astilectron-bundler cc -v
$ astilectron-bundler cc -v
@@ -0,0 +1,45 @@
package main

import (
"fmt"
"sort"
"strings"
)

// LDFlags represents ldflags
type LDFlags map[string][]string

// String returns the ldflags as a string
func (l LDFlags) String() string {
var o []string
keys := make([]string, 0, len(l))
for k := range l {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
ss := l[k]
if len(ss) == 0 {
o = append(o, "-"+k)
continue
}
for _, s := range ss {
o = append(o, fmt.Sprintf(`-%s %s`, k, s))
}
}
return strings.Join(o, " ")
}

// Set allows setting the values for use by flag
func (l LDFlags) Set(s string) error {
segments := strings.SplitN(s, ":", 2)
flag := segments[0]

val := l[flag]
if len(segments) == 2 {
val = strings.Split(segments[1], ",")
}
l[flag] = append(l[flag], val...)

return nil
}
@@ -0,0 +1,25 @@
package main

import (
"testing"

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

func TestLDFlags(t *testing.T) {
l := LDFlags{}

assert.Equal(t, l.String(), "")

l.Set("X:main.foo=1")

assert.Equal(t, "-X main.foo=1", l.String())

l.Set("X:main.bar=1,main.baz=2")

assert.Equal(t, "-X main.foo=1 -X main.bar=1 -X main.baz=2", l.String())

l.Set("s")

assert.Equal(t, "-X main.foo=1 -X main.bar=1 -X main.baz=2 -s", l.String())
}
@@ -13,6 +13,8 @@ import (
"github.com/pkg/errors"
)

var ldflags = LDFlags{}

// Flags
var (
astilectronPath = flag.String("a", "", "the astilectron path")
@@ -23,6 +25,10 @@ var (
windows = flag.Bool("w", false, "if set, will add windows/amd64 to the environments")
)

func init() {
flag.Var(ldflags, "ldflags", "extra values to concatenate onto -ldflags, eg X:main.Version=1.0.7")
}

func main() {
// Init
var s = astiflag.Subcommand()
@@ -80,6 +86,8 @@ func main() {
c.Environments = []astibundler.ConfigurationEnvironment{{Arch: runtime.GOARCH, OS: runtime.GOOS}}
}

c.LDFlags = astibundler.LDFlags(ldflags)

// Build bundler
var b *astibundler.Bundler
if b, err = astibundler.New(c); err != nil {
@@ -76,6 +76,9 @@ type Configuration struct {

//!\\ DEBUG ONLY
AstilectronPath string `json:"astilectron_path"` // when making changes to astilectron

// LDFlags to pass through to go build
LDFlags LDFlags `json:"ldflags"`
}

type ConfigurationBind struct {
@@ -110,6 +113,7 @@ type Bundler struct {
ctx context.Context
darwinAgentApp bool
environments []ConfigurationEnvironment
ldflags LDFlags
pathAstilectron string
pathBindInput string
pathBindOutput string
@@ -153,6 +157,7 @@ func New(c *Configuration) (b *Bundler, err error) {
environments: c.Environments,
darwinAgentApp: c.DarwinAgentApp,
resourcesAdapters: c.ResourcesAdapters,
ldflags: c.LDFlags,
}

// Add context
@@ -305,24 +310,6 @@ func (b *Bundler) Bundle() (err error) {
return
}

// ldflags represents ldflags
type ldflags map[string][]string

// string returns the ldflags as a string
func (l ldflags) string() string {
var o []string
for k, ss := range l {
if len(ss) == 0 {
o = append(o, "-"+k)
continue
}
for _, s := range ss {
o = append(o, fmt.Sprintf(`-%s %s`, k, s))
}
}
return strings.Join(o, " ")
}

// bundle bundles an os
func (b *Bundler) bundle(e ConfigurationEnvironment) (err error) {
// Bind data
@@ -347,18 +334,18 @@ func (b *Bundler) bundle(e ConfigurationEnvironment) (err error) {
return
}

// Build ldflags
var l = ldflags{
"s": []string{},
std := LDFlags{
"X": []string{
`"main.AppName=` + b.appName + `"`,
`"main.BuiltAt=` + time.Now().String() + `"`,
},
}
if e.OS == "windows" {
l["H"] = []string{"windowsgui"}
std["H"] = []string{"windowsgui"}
}

b.ldflags.merge(std)

// Get gopath
gp := os.Getenv("GOPATH")
if len(gp) == 0 {
@@ -368,7 +355,7 @@ func (b *Bundler) bundle(e ConfigurationEnvironment) (err error) {
// Build cmd
astilog.Debugf("Building for os %s and arch %s", e.OS, e.Arch)
var binaryPath = filepath.Join(environmentPath, "binary")
var cmd = exec.Command(b.pathGoBinary, "build", "-ldflags", l.string(), "-o", binaryPath, b.pathBuild)
var cmd = exec.Command(b.pathGoBinary, "build", "-ldflags", b.ldflags.String(), "-o", binaryPath, b.pathBuild)
cmd.Env = []string{
"GOARCH=" + e.Arch,
"GOOS=" + e.OS,
@@ -0,0 +1,37 @@
package astibundler

import (
"fmt"
"sort"
"strings"
)

// LDFlags represents ldflags
type LDFlags map[string][]string

// String returns the ldflags as a string
func (l LDFlags) String() string {
var o []string
keys := make([]string, 0, len(l))
for k := range l {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
ss := l[k]
if len(ss) == 0 {
o = append(o, "-"+k)
continue
}
for _, s := range ss {
o = append(o, fmt.Sprintf(`-%s %s`, k, s))
}
}
return strings.Join(o, " ")
}

func (l LDFlags) merge(r LDFlags) {
for flag := range r {
l[flag] = append(l[flag], r[flag]...)
}
}

0 comments on commit 6cf585e

Please sign in to comment.