Skip to content

Commit

Permalink
[chore] detect the presence of ld/gc flags.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dainerx committed Dec 31, 2024
1 parent e1a0d1f commit ee35153
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 9 deletions.
4 changes: 2 additions & 2 deletions cmd/builder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,15 @@ 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

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:

Expand Down
2 changes: 2 additions & 0 deletions cmd/builder/internal/builder/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
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,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)
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/builder/internal/builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Check warning on line 124 in cmd/builder/internal/builder/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/builder/internal/builder/main.go#L122-L124

Added lines #L122 - L124 were not covered by tests
if len(cfg.GCFlags) > 0 {
if cfg.GCSet {
cfg.Logger.Info("Using custom gcflags", zap.String("gcflags", cfg.GCFlags))
gcflags = cfg.GCFlags
}

Check warning on line 128 in cmd/builder/internal/builder/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/builder/internal/builder/main.go#L126-L128

Added lines #L126 - L128 were not covered by tests
Expand Down
17 changes: 12 additions & 5 deletions cmd/builder/internal/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (
skipGetModulesFlag = "skip-get-modules"
skipStrictVersioningFlag = "skip-strict-versioning"
ldflagsFlag = "ldflags"
gcflagsFlag = "gcflags"
gcflagsFlag = "gcflags"
distributionOutputPathFlag = "output-path"
verboseFlag = "verbose"
)
Expand Down Expand Up @@ -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)
}

Check warning on line 159 in cmd/builder/internal/command.go

View check run for this annotation

Codecov / codecov/patch

cmd/builder/internal/command.go#L156-L159

Added lines #L156 - L159 were not covered by tests

cfg.Verbose, err = flags.GetBool(verboseFlag)
errs = multierr.Append(errs, err)

Expand Down

0 comments on commit ee35153

Please sign in to comment.