Skip to content

Commit a6d8bb0

Browse files
committed
[#58] Ability to provide custom ld and gc flags
1 parent ffcef93 commit a6d8bb0

File tree

5 files changed

+33
-5
lines changed

5 files changed

+33
-5
lines changed

cmd/builder/internal/builder/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type Config struct {
3535
SkipGetModules bool `mapstructure:"-"`
3636
SkipStrictVersioning bool `mapstructure:"-"`
3737
LDFlags string `mapstructure:"-"`
38+
GCFlags string `mapstructure:"-"`
3839
Verbose bool `mapstructure:"-"`
3940

4041
Distribution Distribution `mapstructure:"dist"`
@@ -71,7 +72,7 @@ type Distribution struct {
7172
OutputPath string `mapstructure:"output_path"`
7273
Version string `mapstructure:"version"`
7374
BuildTags string `mapstructure:"build_tags"`
74-
DebugCompilation bool `mapstructure:"debug_compilation"`
75+
DebugCompilation bool `mapstructure:"debug_compilation"` // TODO depercate?
7576
}
7677

7778
// Module represents a receiver, exporter, processor or extension for the distribution

cmd/builder/internal/builder/config_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ func TestNewDefaultConfig(t *testing.T) {
179179
require.NoError(t, cfg.Validate())
180180
assert.False(t, cfg.Distribution.DebugCompilation)
181181
assert.Empty(t, cfg.Distribution.BuildTags)
182+
assert.Empty(t, cfg.LDFlags)
183+
assert.Empty(t, cfg.GCFlags)
182184
}
183185

184186
func TestNewBuiltinConfig(t *testing.T) {

cmd/builder/internal/builder/main.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,28 @@ func Compile(cfg *Config) error {
109109

110110
cfg.Logger.Info("Compiling")
111111

112-
ldflags := "-s -w"
112+
var ldflags = "-s -w" // we strip the symbols by default for smaller binaries
113+
var gcflags = ""
113114

114115
args := []string{"build", "-trimpath", "-o", cfg.Distribution.Name}
115116
if cfg.Distribution.DebugCompilation {
116117
cfg.Logger.Info("Debug compilation is enabled, the debug symbols will be left on the resulting binary")
117118
ldflags = cfg.LDFlags
118-
args = append(args, "-gcflags=all=-N -l")
119-
} else if len(cfg.LDFlags) > 0 {
120-
ldflags += " " + cfg.LDFlags
119+
gcflags = "all=-N -l"
120+
} else {
121+
if len(cfg.LDFlags) > 0 {
122+
cfg.Logger.Info("Using custom ldflags", zap.String("ldflags", cfg.LDFlags))
123+
ldflags = cfg.LDFlags
124+
}
125+
if len(cfg.GCFlags) > 0 {
126+
cfg.Logger.Info("Using custom gcflags", zap.String("gcflags", cfg.GCFlags))
127+
gcflags = cfg.GCFlags
128+
}
121129
}
130+
122131
args = append(args, "-ldflags="+ldflags)
132+
args = append(args, "-gcflags="+gcflags)
133+
123134
if cfg.Distribution.BuildTags != "" {
124135
args = append(args, "-tags", cfg.Distribution.BuildTags)
125136
}

cmd/builder/internal/builder/main_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,16 @@ func TestGenerateAndCompile(t *testing.T) {
251251
return cfg
252252
},
253253
},
254+
{
255+
name: "GCFlags Compilation",
256+
cfgBuilder: func(t *testing.T) *Config {
257+
cfg := newTestConfig(t)
258+
cfg.Distribution.OutputPath = t.TempDir()
259+
cfg.Replaces = append(cfg.Replaces, replaces...)
260+
cfg.GCFlags = `all=-N -l`
261+
return cfg
262+
},
263+
},
254264
{
255265
name: "Build Tags Compilation",
256266
cfgBuilder: func(t *testing.T) *Config {

cmd/builder/internal/command.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const (
2727
skipGetModulesFlag = "skip-get-modules"
2828
skipStrictVersioningFlag = "skip-strict-versioning"
2929
ldflagsFlag = "ldflags"
30+
gcflagsFlag = "gcflags"
3031
distributionOutputPathFlag = "output-path"
3132
verboseFlag = "verbose"
3233
)
@@ -84,6 +85,7 @@ func initFlags(flags *flag.FlagSet) error {
8485
flags.Bool(skipStrictVersioningFlag, true, "Whether builder should skip strictly checking the calculated versions following dependency resolution")
8586
flags.Bool(verboseFlag, false, "Whether builder should print verbose output (default false)")
8687
flags.String(ldflagsFlag, "", `ldflags to include in the "go build" command`)
88+
flags.String(gcflagsFlag, "", `gcflags to include in the "go build" command`)
8789
flags.String(distributionOutputPathFlag, "", "Where to write the resulting files")
8890
return flags.MarkDeprecated(distributionOutputPathFlag, "use config distribution::output_path")
8991
}
@@ -147,6 +149,8 @@ func applyFlags(flags *flag.FlagSet, cfg *builder.Config) error {
147149

148150
cfg.LDFlags, err = flags.GetString(ldflagsFlag)
149151
errs = multierr.Append(errs, err)
152+
cfg.GCFlags, err = flags.GetString(gcflagsFlag)
153+
errs = multierr.Append(errs, err)
150154
cfg.Verbose, err = flags.GetBool(verboseFlag)
151155
errs = multierr.Append(errs, err)
152156

0 commit comments

Comments
 (0)