Skip to content
Permalink
Browse files

feat: Add asmflags and gcflags fields (#648)

  • Loading branch information...
elyscape authored and caarlos0 committed Apr 20, 2018
1 parent 5a3e3e5 commit 46aa41e27e4d097a0314dce50cdce06fb294bd10
Showing with 114 additions and 18 deletions.
  1. +14 −12 config/config.go
  2. +31 −0 docs/050-build.md
  3. +21 −4 internal/builders/golang/build.go
  4. +48 −2 internal/builders/golang/build_test.go
@@ -80,18 +80,20 @@ type IgnoredBuild struct {

// Build contains the build configuration section
type Build struct {
Goos []string `yaml:",omitempty"`
Goarch []string `yaml:",omitempty"`
Goarm []string `yaml:",omitempty"`
Targets []string `yaml:",omitempty"`
Ignore []IgnoredBuild `yaml:",omitempty"`
Main string `yaml:",omitempty"`
Ldflags string `yaml:",omitempty"`
Flags string `yaml:",omitempty"`
Binary string `yaml:",omitempty"`
Hooks Hooks `yaml:",omitempty"`
Env []string `yaml:",omitempty"`
Lang string `yaml:",omitempty"`
Goos []string `yaml:",omitempty"`
Goarch []string `yaml:",omitempty"`
Goarm []string `yaml:",omitempty"`
Targets []string `yaml:",omitempty"`
Ignore []IgnoredBuild `yaml:",omitempty"`
Main string `yaml:",omitempty"`
Ldflags string `yaml:",omitempty"`
Flags string `yaml:",omitempty"`
Binary string `yaml:",omitempty"`
Hooks Hooks `yaml:",omitempty"`
Env []string `yaml:",omitempty"`
Lang string `yaml:",omitempty"`
Asmflags string `yaml:",omitempty"`
Gcflags string `yaml:",omitempty"`
}

// FormatOverride is used to specify a custom format for a specific GOOS.
@@ -31,13 +31,44 @@ builds:
# Default is empty.
flags: -tags dev
# Custom asmflags template.
# This is parsed with the Go template engine and the following variables
# are available:
# - Date
# - Commit
# - Tag
# - Version (Git tag without `v` prefix)
# - Env (environment variables)
# Date format is `2006-01-02_15:04:05`.
# You can use the `time` function instead of `Date`, for example:
# `time "2006-01-02"` too if you need custom formats
#
# Default is empty.
asmflags: all=-trimpath={{.Env.GOPATH}}
# Custom gcflags template.
# This is parsed with the Go template engine and the following variables
# are available:
# - Date
# - Commit
# - Tag
# - Version (Git tag without `v` prefix)
# - Env (environment variables)
# Date format is `2006-01-02_15:04:05`.
# You can use the `time` function instead of `Date`, for example:
# `time "2006-01-02"` too if you need custom formats
#
# Default is empty.
gcflags: all=-trimpath={{.Env.GOPATH}}
# Custom ldflags template.
# This is parsed with the Go template engine and the following variables
# are available:
# - Date
# - Commit
# - Tag
# - Version (Git tag without `v` prefix)
# - Env (environment variables)
# Date format is `2006-01-02_15:04:05`.
# You can use the `time` function instead of `Date`, for example:
# `time "2006-01-02"` too if you need custom formats
@@ -62,7 +62,24 @@ func (*Builder) Build(ctx *context.Context, build config.Build, options api.Opti
if build.Flags != "" {
cmd = append(cmd, strings.Fields(build.Flags)...)
}
flags, err := ldflags(ctx, build)

if build.Asmflags != "" {
flags, err := processField(ctx, build.Asmflags, "asmflags")
if err != nil {
return err
}
cmd = append(cmd, "-asmflags="+flags)
}

if build.Gcflags != "" {
flags, err := processField(ctx, build.Gcflags, "gcflags")
if err != nil {
return err
}
cmd = append(cmd, "-gcflags="+flags)
}

flags, err := processField(ctx, build.Ldflags, "ldflags")
if err != nil {
return err
}
@@ -90,7 +107,7 @@ func (*Builder) Build(ctx *context.Context, build config.Build, options api.Opti
return nil
}

func ldflags(ctx *context.Context, build config.Build) (string, error) {
func processField(ctx *context.Context, field string, fieldName string) (string, error) {
var data = struct {
Commit string
Tag string
@@ -105,14 +122,14 @@ func ldflags(ctx *context.Context, build config.Build) (string, error) {
Env: ctx.Env,
}
var out bytes.Buffer
t, err := template.New("ldflags").
t, err := template.New(fieldName).
Funcs(template.FuncMap{
"time": func(s string) string {
return time.Now().UTC().Format(s)
},
}).
Option("missingkey=error").
Parse(build.Ldflags)
Parse(field)
if err != nil {
return "", err
}
@@ -84,6 +84,8 @@ func TestBuild(t *testing.T) {
"windows_amd64",
"linux_arm_6",
},
Asmflags: "all=",
Gcflags: "all=",
},
},
}
@@ -197,6 +199,50 @@ func TestBuildInvalidTarget(t *testing.T) {
assert.Len(t, ctx.Artifacts.List(), 0)
}

func TestRunInvalidAsmflags(t *testing.T) {
folder, back := testlib.Mktmp(t)
defer back()
writeGoodMain(t, folder)
var config = config.Project{
Builds: []config.Build{
{
Binary: "nametest",
Asmflags: "{{.Version}",
Targets: []string{
runtimeTarget,
},
},
},
}
var ctx = context.New(config)
var err = Default.Build(ctx, ctx.Config.Builds[0], api.Options{
Target: runtimeTarget,
})
assert.EqualError(t, err, `template: asmflags:1: unexpected "}" in operand`)
}

func TestRunInvalidGcflags(t *testing.T) {
folder, back := testlib.Mktmp(t)
defer back()
writeGoodMain(t, folder)
var config = config.Project{
Builds: []config.Build{
{
Binary: "nametest",
Gcflags: "{{.Version}",
Targets: []string{
runtimeTarget,
},
},
},
}
var ctx = context.New(config)
var err = Default.Build(ctx, ctx.Config.Builds[0], api.Options{
Target: runtimeTarget,
})
assert.EqualError(t, err, `template: gcflags:1: unexpected "}" in operand`)
}

func TestRunInvalidLdflags(t *testing.T) {
folder, back := testlib.Mktmp(t)
defer back()
@@ -319,7 +365,7 @@ func TestLdFlagsFullTemplate(t *testing.T) {
Config: config,
Env: map[string]string{"FOO": "123"},
}
flags, err := ldflags(ctx, ctx.Config.Builds[0])
flags, err := processField(ctx, ctx.Config.Builds[0].Ldflags, "ldflags")
assert.NoError(t, err)
assert.Contains(t, flags, "-s -w")
assert.Contains(t, flags, "-X main.version=1.2.3")
@@ -345,7 +391,7 @@ func TestInvalidTemplate(t *testing.T) {
var ctx = &context.Context{
Config: config,
}
flags, err := ldflags(ctx, ctx.Config.Builds[0])
flags, err := processField(ctx, template, "ldflags")
assert.EqualError(tt, err, eerr)
assert.Empty(tt, flags)
})

0 comments on commit 46aa41e

Please sign in to comment.
You can’t perform that action at this time.