Skip to content

Commit

Permalink
[#58] Ability to provide custom ld and gc flags
Browse files Browse the repository at this point in the history
  • Loading branch information
Dainerx committed Dec 30, 2024
1 parent c05bab7 commit f4df0bf
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
3 changes: 2 additions & 1 deletion cmd/builder/internal/builder/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Config struct {
SkipGetModules bool `mapstructure:"-"`
SkipStrictVersioning bool `mapstructure:"-"`
LDFlags string `mapstructure:"-"`
GCFlags string `mapstructure:"-"`
Verbose bool `mapstructure:"-"`

Distribution Distribution `mapstructure:"dist"`
Expand Down Expand Up @@ -71,7 +72,7 @@ type Distribution struct {
OutputPath string `mapstructure:"output_path"`
Version string `mapstructure:"version"`
BuildTags string `mapstructure:"build_tags"`
DebugCompilation bool `mapstructure:"debug_compilation"`
DebugCompilation bool `mapstructure:"debug_compilation"` // TODO depercate?
}

// Module represents a receiver, exporter, processor or extension for the distribution
Expand Down
2 changes: 2 additions & 0 deletions cmd/builder/internal/builder/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ func TestNewDefaultConfig(t *testing.T) {
require.NoError(t, cfg.Validate())
assert.False(t, cfg.Distribution.DebugCompilation)
assert.Empty(t, cfg.Distribution.BuildTags)
assert.Empty(t, cfg.LDFlags)
assert.Empty(t, cfg.GCFlags)
}

func TestNewBuiltinConfig(t *testing.T) {
Expand Down
19 changes: 15 additions & 4 deletions cmd/builder/internal/builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,28 @@ func Compile(cfg *Config) error {

cfg.Logger.Info("Compiling")

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

args := []string{"build", "-trimpath", "-o", cfg.Distribution.Name}
if cfg.Distribution.DebugCompilation {
cfg.Logger.Info("Debug compilation is enabled, the debug symbols will be left on the resulting binary")
ldflags = cfg.LDFlags
args = append(args, "-gcflags=all=-N -l")
} else if len(cfg.LDFlags) > 0 {
ldflags += " " + cfg.LDFlags
gcflags = "all=-N -l"
} else {
if len(cfg.LDFlags) > 0 {
cfg.Logger.Info("Using custom ldflags", zap.String("ldflags", cfg.LDFlags))
ldflags = cfg.LDFlags
}
if len(cfg.GCFlags) > 0 {
cfg.Logger.Info("Using custom gcflags", zap.String("gcflags", cfg.GCFlags))
gcflags = cfg.GCFlags
}
}

args = append(args, "-ldflags="+ldflags)
args = append(args, "-gcflags="+gcflags)

if cfg.Distribution.BuildTags != "" {
args = append(args, "-tags", cfg.Distribution.BuildTags)
}
Expand Down
10 changes: 10 additions & 0 deletions cmd/builder/internal/builder/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,16 @@ func TestGenerateAndCompile(t *testing.T) {
return cfg
},
},
{
name: "GCFlags Compilation",
cfgBuilder: func(t *testing.T) *Config {
cfg := newTestConfig(t)
cfg.Distribution.OutputPath = t.TempDir()
cfg.Replaces = append(cfg.Replaces, replaces...)
cfg.GCFlags = `all=-N -l`
return cfg
},
},
{
name: "Build Tags Compilation",
cfgBuilder: func(t *testing.T) *Config {
Expand Down
4 changes: 4 additions & 0 deletions cmd/builder/internal/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
skipGetModulesFlag = "skip-get-modules"
skipStrictVersioningFlag = "skip-strict-versioning"
ldflagsFlag = "ldflags"
gcflagsFlag = "gcflags"
distributionOutputPathFlag = "output-path"
verboseFlag = "verbose"
)
Expand Down Expand Up @@ -84,6 +85,7 @@ func initFlags(flags *flag.FlagSet) error {
flags.Bool(skipStrictVersioningFlag, true, "Whether builder should skip strictly checking the calculated versions following dependency resolution")
flags.Bool(verboseFlag, false, "Whether builder should print verbose output (default false)")
flags.String(ldflagsFlag, "", `ldflags to include in the "go build" command`)
flags.String(gcflagsFlag, "", `gcflags to include in the "go build" command`)
flags.String(distributionOutputPathFlag, "", "Where to write the resulting files")
return flags.MarkDeprecated(distributionOutputPathFlag, "use config distribution::output_path")
}
Expand Down Expand Up @@ -147,6 +149,8 @@ func applyFlags(flags *flag.FlagSet, cfg *builder.Config) error {

cfg.LDFlags, err = flags.GetString(ldflagsFlag)
errs = multierr.Append(errs, err)
cfg.GCFlags, err = flags.GetString(gcflagsFlag)
errs = multierr.Append(errs, err)
cfg.Verbose, err = flags.GetBool(verboseFlag)
errs = multierr.Append(errs, err)

Expand Down

0 comments on commit f4df0bf

Please sign in to comment.