Skip to content

Commit

Permalink
[open-telemetry#58] ld gc flags
Browse files Browse the repository at this point in the history
  • Loading branch information
Dainerx committed Dec 30, 2024
1 parent 080b765 commit f37ffc3
Show file tree
Hide file tree
Showing 5 changed files with 36 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 @@ -31,6 +31,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 @@ -68,7 +69,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 required for backwards-compatibility with builds older than x.xx.x
}

// 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 @@ -153,6 +153,8 @@ func TestNewDefaultConfig(t *testing.T) {
assert.NoError(t, cfg.SetGoPath())
require.NoError(t, cfg.Validate())
assert.False(t, cfg.Distribution.DebugCompilation)
assert.Empty(t, cfg.LDFlags)
assert.Empty(t, cfg.GCFlags)
assert.Empty(t, cfg.Distribution.BuildTags)
}

Expand Down
22 changes: 18 additions & 4 deletions cmd/builder/internal/builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,32 @@ func Compile(cfg Config) error {
return nil
}


cfg.Logger.Info("Compiling")
cfg.Logger.Info("distrubtion", zap.Any("distro", cfg.Distribution))

var 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 All @@ -130,6 +143,7 @@ func Compile(cfg Config) error {
}
cfg.Logger.Info("Compiled", zap.String("binary", fmt.Sprintf("%s/%s", cfg.Distribution.OutputPath, cfg.Distribution.Name)))

cfg.Logger.Info("arg", zap.Any("arguments", args))
return nil
}

Expand Down
12 changes: 12 additions & 0 deletions cmd/builder/internal/builder/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,18 @@ func TestGenerateAndCompile(t *testing.T) {
return cfg
},
},
{
name: "GCFlags Compilation",
cfgBuilder: func(t *testing.T) Config {
cfg := newTestConfig()
err := cfg.SetBackwardsCompatibility()
require.NoError(t, err)
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
2 changes: 2 additions & 0 deletions cmd/builder/internal/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (
skipGetModulesFlag = "skip-get-modules"
skipStrictVersioningFlag = "skip-strict-versioning"
ldflagsFlag = "ldflags"
gcflagsFlag = "gcflags"
distributionNameFlag = "name"
distributionDescriptionFlag = "description"
distributionVersionFlag = "version"
Expand Down Expand Up @@ -87,6 +88,7 @@ configuration is provided, ocb will generate a default Collector.
cmd.Flags().BoolVar(&cfg.SkipStrictVersioning, skipStrictVersioningFlag, true, "Whether builder should skip strictly checking the calculated versions following dependency resolution")
cmd.Flags().BoolVar(&cfg.Verbose, verboseFlag, false, "Whether builder should print verbose output (default false)")
cmd.Flags().StringVar(&cfg.LDFlags, ldflagsFlag, "", `ldflags to include in the "go build" command`)
cmd.Flags().StringVar(&cfg.GCFlags, gcflagsFlag, "", `gcflags to include in the "go build" command`)
cmd.Flags().StringVar(&cfg.Distribution.Name, distributionNameFlag, "otelcol-custom", "The executable name for the OpenTelemetry Collector distribution")
if err := cmd.Flags().MarkDeprecated(distributionNameFlag, "use config distribution::name"); err != nil {
return nil, err
Expand Down

0 comments on commit f37ffc3

Please sign in to comment.