From ee3515348b0034ce9c882c9b90bcff326aea8c2f Mon Sep 17 00:00:00 2001 From: BEN GHORBEL Oussama Date: Tue, 31 Dec 2024 17:17:16 +0100 Subject: [PATCH] [chore] detect the presence of ld/gc flags. --- cmd/builder/README.md | 4 ++-- cmd/builder/internal/builder/config.go | 2 ++ cmd/builder/internal/builder/config_test.go | 2 ++ cmd/builder/internal/builder/main.go | 4 ++-- cmd/builder/internal/command.go | 17 ++++++++++++----- 5 files changed, 20 insertions(+), 9 deletions(-) diff --git a/cmd/builder/README.md b/cmd/builder/README.md index 425f8590b62..9f63c66b8cf 100644 --- a/cmd/builder/README.md +++ b/cmd/builder/README.md @@ -115,7 +115,7 @@ Use `ocb --help` to learn about which flags are available. By default, the LDflags are set to -s -w, which strips debugging symbols to produce a smaller OpenTelemetry Collector binary. To retain debugging symbols and DWARF debugging data in the binary, override the LDflags as shown: ```console -ocb --ldflags=" " --config=builder-config.yaml. +ocb --ldflags="" --config=builder-config.yaml. ``` ### Debugging with Delve @@ -123,7 +123,7 @@ ocb --ldflags=" " --config=builder-config.yaml. To ensure the code being executed matches the written code exactly, debugging symbols must be preserved, and compiler inlining and optimizations disabled. You can achieve this in two ways: 1. Set the configuration property `debug_compilation` to true. -2. Manually override the ldflags and gcflags `ocb --ldflags=" " --gcflags="all=-N -l" --config=builder-config.yaml.` +2. Manually override the ldflags and gcflags `ocb --ldflags="" --gcflags="all=-N -l" --config=builder-config.yaml.` Then install `go-delve` and run OpenTelemetry Collector with `dlv` command as the following example: diff --git a/cmd/builder/internal/builder/config.go b/cmd/builder/internal/builder/config.go index 65317d42033..60dc4540b06 100644 --- a/cmd/builder/internal/builder/config.go +++ b/cmd/builder/internal/builder/config.go @@ -35,7 +35,9 @@ type Config struct { SkipGetModules bool `mapstructure:"-"` SkipStrictVersioning bool `mapstructure:"-"` LDFlags string `mapstructure:"-"` + LDSet bool `mapstructure:"-"` // only used to override LDFlags GCFlags string `mapstructure:"-"` + GCSet bool `mapstructure:"-"` // only used to override GCFlags Verbose bool `mapstructure:"-"` Distribution Distribution `mapstructure:"dist"` diff --git a/cmd/builder/internal/builder/config_test.go b/cmd/builder/internal/builder/config_test.go index 014569ac659..8cd02c0107c 100644 --- a/cmd/builder/internal/builder/config_test.go +++ b/cmd/builder/internal/builder/config_test.go @@ -179,7 +179,9 @@ func TestNewDefaultConfig(t *testing.T) { require.NoError(t, cfg.Validate()) assert.False(t, cfg.Distribution.DebugCompilation) assert.Empty(t, cfg.Distribution.BuildTags) + assert.False(t, cfg.LDSet) assert.Empty(t, cfg.LDFlags) + assert.False(t, cfg.GCSet) assert.Empty(t, cfg.GCFlags) } diff --git a/cmd/builder/internal/builder/main.go b/cmd/builder/internal/builder/main.go index 2f570fc7fc7..746211d37b5 100644 --- a/cmd/builder/internal/builder/main.go +++ b/cmd/builder/internal/builder/main.go @@ -118,11 +118,11 @@ func Compile(cfg *Config) error { ldflags = cfg.LDFlags gcflags = "all=-N -l" } else { - if len(cfg.LDFlags) > 0 { + if cfg.LDSet { cfg.Logger.Info("Using custom ldflags", zap.String("ldflags", cfg.LDFlags)) ldflags = cfg.LDFlags } - if len(cfg.GCFlags) > 0 { + if cfg.GCSet { cfg.Logger.Info("Using custom gcflags", zap.String("gcflags", cfg.GCFlags)) gcflags = cfg.GCFlags } diff --git a/cmd/builder/internal/command.go b/cmd/builder/internal/command.go index c64316fbd2f..aa90b7d66f2 100644 --- a/cmd/builder/internal/command.go +++ b/cmd/builder/internal/command.go @@ -27,7 +27,7 @@ const ( skipGetModulesFlag = "skip-get-modules" skipStrictVersioningFlag = "skip-strict-versioning" ldflagsFlag = "ldflags" - gcflagsFlag = "gcflags" + gcflagsFlag = "gcflags" distributionOutputPathFlag = "output-path" verboseFlag = "verbose" ) @@ -147,10 +147,17 @@ func applyFlags(flags *flag.FlagSet, cfg *builder.Config) error { cfg.SkipStrictVersioning, err = flags.GetBool(skipStrictVersioningFlag) errs = multierr.Append(errs, err) - cfg.LDFlags, err = flags.GetString(ldflagsFlag) - errs = multierr.Append(errs, err) - cfg.GCFlags, err = flags.GetString(gcflagsFlag) - errs = multierr.Append(errs, err) + if flags.Changed(ldflagsFlag) { + cfg.LDSet = true + cfg.LDFlags, err = flags.GetString(ldflagsFlag) + errs = multierr.Append(errs, err) + } + if flags.Changed(gcflagsFlag) { + cfg.GCSet = true + cfg.GCFlags, err = flags.GetString(gcflagsFlag) + errs = multierr.Append(errs, err) + } + cfg.Verbose, err = flags.GetBool(verboseFlag) errs = multierr.Append(errs, err)