From f4df0bfcbf748a3954d2b8b7865bf27db8f8ee79 Mon Sep 17 00:00:00 2001 From: BEN GHORBEL Oussama Date: Mon, 30 Dec 2024 18:13:38 +0100 Subject: [PATCH] [#58] Ability to provide custom ld and gc flags --- cmd/builder/internal/builder/config.go | 3 ++- cmd/builder/internal/builder/config_test.go | 2 ++ cmd/builder/internal/builder/main.go | 19 +++++++++++++++---- cmd/builder/internal/builder/main_test.go | 10 ++++++++++ cmd/builder/internal/command.go | 4 ++++ 5 files changed, 33 insertions(+), 5 deletions(-) diff --git a/cmd/builder/internal/builder/config.go b/cmd/builder/internal/builder/config.go index cb83375316b..65317d42033 100644 --- a/cmd/builder/internal/builder/config.go +++ b/cmd/builder/internal/builder/config.go @@ -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"` @@ -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 diff --git a/cmd/builder/internal/builder/config_test.go b/cmd/builder/internal/builder/config_test.go index 14040dd50aa..014569ac659 100644 --- a/cmd/builder/internal/builder/config_test.go +++ b/cmd/builder/internal/builder/config_test.go @@ -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) { diff --git a/cmd/builder/internal/builder/main.go b/cmd/builder/internal/builder/main.go index 197d1038613..2f570fc7fc7 100644 --- a/cmd/builder/internal/builder/main.go +++ b/cmd/builder/internal/builder/main.go @@ -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) } diff --git a/cmd/builder/internal/builder/main_test.go b/cmd/builder/internal/builder/main_test.go index 8220e48df85..4b02a02b416 100644 --- a/cmd/builder/internal/builder/main_test.go +++ b/cmd/builder/internal/builder/main_test.go @@ -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 { diff --git a/cmd/builder/internal/command.go b/cmd/builder/internal/command.go index fa1fd235233..c64316fbd2f 100644 --- a/cmd/builder/internal/command.go +++ b/cmd/builder/internal/command.go @@ -27,6 +27,7 @@ const ( skipGetModulesFlag = "skip-get-modules" skipStrictVersioningFlag = "skip-strict-versioning" ldflagsFlag = "ldflags" + gcflagsFlag = "gcflags" distributionOutputPathFlag = "output-path" verboseFlag = "verbose" ) @@ -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") } @@ -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)