From 46aa41e27e4d097a0314dce50cdce06fb294bd10 Mon Sep 17 00:00:00 2001 From: Eli Young Date: Fri, 20 Apr 2018 04:26:04 -0700 Subject: [PATCH] feat: Add asmflags and gcflags fields (#648) --- config/config.go | 26 +++++++------- docs/050-build.md | 31 ++++++++++++++++ internal/builders/golang/build.go | 25 ++++++++++--- internal/builders/golang/build_test.go | 50 ++++++++++++++++++++++++-- 4 files changed, 114 insertions(+), 18 deletions(-) diff --git a/config/config.go b/config/config.go index f69e8a78a..a5c70b7ef 100644 --- a/config/config.go +++ b/config/config.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. diff --git a/docs/050-build.md b/docs/050-build.md index bcc17ff57..83af70dda 100644 --- a/docs/050-build.md +++ b/docs/050-build.md @@ -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 diff --git a/internal/builders/golang/build.go b/internal/builders/golang/build.go index 2e371d621..19eb0d6ce 100644 --- a/internal/builders/golang/build.go +++ b/internal/builders/golang/build.go @@ -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 } diff --git a/internal/builders/golang/build_test.go b/internal/builders/golang/build_test.go index c9b216989..e7735c91c 100644 --- a/internal/builders/golang/build_test.go +++ b/internal/builders/golang/build_test.go @@ -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) })