1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-03-17 20:47:50 +02:00

feat: Add asmflags and gcflags fields (#648)

This commit is contained in:
Eli Young 2018-04-20 04:26:04 -07:00 committed by Carlos Alexandro Becker
parent 5a3e3e5ab8
commit 46aa41e27e
4 changed files with 114 additions and 18 deletions

View File

@ -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.

View File

@ -31,6 +31,36 @@ 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:
@ -38,6 +68,7 @@ builds:
# - 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

View File

@ -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
}

View File

@ -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)
})