Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions artifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ type Artifacts struct {
Users []AddUserConfig `yaml:"users,omitempty" json:"users,omitempty"`
// Groups is a list of groups to add to the system when the package is installed.
Groups []AddGroupConfig `yaml:"groups,omitempty" json:"groups,omitempty"`

// DisableStrip is used to disable stripping of artifacts.
DisableStrip bool `yaml:"disable_strip,omitempty" json:"disable_strip,omitempty"`
}

type ArtifactSymlinkConfig struct {
Expand Down
6 changes: 6 additions & 0 deletions docs/spec.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@
],
"description": "DataDirs is a list of read-only architecture-independent data files, to be placed in /usr/share/"
},
"disable_strip": {
"type": [
"boolean"
],
"description": "DisableStrip is used to disable stripping of artifacts."
},
"docs": {
"additionalProperties": {
"$ref": "#/$defs/ArtifactConfig"
Expand Down
11 changes: 11 additions & 0 deletions packaging/linux/deb/template_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,14 @@ func (w *rulesWrapper) OverrideSystemd() (fmt.Stringer, error) {

return b, nil
}

func (w *rulesWrapper) OverrideStrip() fmt.Stringer {
artifacts := w.Spec.GetArtifacts(w.target)

buf := &strings.Builder{}

if artifacts.DisableStrip {
buf.WriteString("override_dh_strip:\n")
}
return buf
}
14 changes: 14 additions & 0 deletions packaging/linux/deb/template_rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,17 @@ Depends: ${misc:Depends},
actual = strings.TrimSpace(buf.String())
assert.Check(t, cmp.Equal(actual, strings.TrimSpace(expect)))
}

func TestRules_OverrideStrip(t *testing.T) {
w := &rulesWrapper{
Spec: &dalec.Spec{},
}

got := w.OverrideStrip()
assert.Equal(t, got.String(), "")

w.Spec.Artifacts.DisableStrip = true
got = w.OverrideStrip()
expect := "override_dh_strip:\n"
assert.Equal(t, got.String(), expect)
}
2 changes: 2 additions & 0 deletions packaging/linux/deb/templates/debian_rules.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ override_dh_dwz:

{{ .OverrideSystemd }}

{{ .OverrideStrip }}

%:
dh $@ -v

9 changes: 9 additions & 0 deletions packaging/linux/rpm/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const gomodsName = "__gomods"
const buildScriptName = "build.sh"

var specTmpl = template.Must(template.New("spec").Funcs(tmplFuncs).Parse(strings.TrimSpace(`
{{.DisableStrip}}
Name: {{.Name}}
Version: {{.Version}}
Release: {{.Release}}%{?dist}
Expand Down Expand Up @@ -776,6 +777,14 @@ func (w *specWrapper) Files() fmt.Stringer {
return b
}

func (w *specWrapper) DisableStrip() string {
artifacts := w.Spec.GetArtifacts(w.Target)
if artifacts.DisableStrip {
return "%global __strip /bin/true"
}
return ""
}

// WriteSpec generates an rpm spec from the provided [dalec.Spec] and distro target and writes it to the passed in writer
func WriteSpec(spec *dalec.Spec, target string, w io.Writer) error {
s := &specWrapper{spec, target}
Expand Down
17 changes: 17 additions & 0 deletions packaging/linux/rpm/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -838,5 +838,22 @@ OrderWithRequires(postun): systemd
want := "Requires(post): /usr/sbin/groupadd, /usr/bin/getent\n"
assert.Equal(t, got, want)
})
}

func TestTemplate_DisableStrip(t *testing.T) {
spec := &dalec.Spec{
Artifacts: dalec.Artifacts{
DisableStrip: true,
},
}

w := &specWrapper{Spec: spec}
want := `%global __strip /bin/true`
got := w.DisableStrip()
assert.Equal(t, got, want)

spec.Artifacts.DisableStrip = false
want = ""
got = w.DisableStrip()
assert.Equal(t, got, want)
}
1 change: 1 addition & 0 deletions targets/linux/rpm/almalinux/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

var (
builderPackages = []string{
"binutils",
"rpm-build",
"ca-certificates",
}
Expand Down
98 changes: 98 additions & 0 deletions test/linux_target_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package test

import (
_ "embed"

"context"
"encoding/json"
"errors"
Expand All @@ -16,9 +18,11 @@ import (
gwclient "github.com/moby/buildkit/frontend/gateway/client"
moby_buildkit_v1_frontend "github.com/moby/buildkit/frontend/gateway/pb"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
pkgerrors "github.com/pkg/errors"
"golang.org/x/exp/maps"
"gotest.tools/v3/assert"
"gotest.tools/v3/assert/cmp"
"gotest.tools/v3/skip"
)

type workerConfig struct {
Expand Down Expand Up @@ -77,6 +81,8 @@ type testLinuxConfig struct {
Libdir string
Worker workerConfig
Release OSRelease

SkipStripTest bool
}

type OSRelease struct {
Expand Down Expand Up @@ -1561,8 +1567,14 @@ Environment="KUBELET_KUBECONFIG_ARGS=--bootstrap-kubeconfig=/etc/kubernetes/boot
})

t.Run("inherited dependencies", func(t *testing.T) {
t.Parallel()
testMixGlobalTargetDependencies(ctx, t, testConfig)
})

t.Run("disable strip", func(t *testing.T) {
t.Parallel()
testDisableStrip(ctx, t, testConfig)
})
}

func testCustomLinuxWorker(ctx context.Context, t *testing.T, targetCfg targetConfig, workerCfg workerConfig) {
Expand Down Expand Up @@ -2342,3 +2354,89 @@ func testMixGlobalTargetDependencies(ctx context.Context, t *testing.T, cfg test
})
})
}

func testDisableStrip(ctx context.Context, t *testing.T, cfg testLinuxConfig) {
skip.If(t, cfg.SkipStripTest, "skipping test as it is not supported for this target: "+cfg.Target.Container)

newSpec := func() *dalec.Spec {
spec := newSimpleSpec()
spec.Args = map[string]string{
"TARGETARCH": "",
}

spec.Sources = map[string]dalec.Source{
"src": {
Generate: []*dalec.SourceGenerator{
{
Gomod: &dalec.GeneratorGomod{},
},
},
Inline: &dalec.SourceInline{
Dir: &dalec.SourceInlineDir{
Files: map[string]*dalec.SourceInlineFile{
"main.go": {Contents: gomodFixtureMain},
"go.mod": {Contents: gomodFixtureMod},
"go.sum": {Contents: gomodFixtureSum},
},
},
},
},
}

spec.Dependencies = &dalec.PackageDependencies{
Build: map[string]dalec.PackageConstraints{
cfg.GetPackage("golang"): {},
},
}
spec.Artifacts = dalec.Artifacts{
Binaries: map[string]dalec.ArtifactConfig{
"bad-executable": {},
},
Libs: map[string]dalec.ArtifactConfig{
"bad-executable": {},
},
}

spec.Build.Env = map[string]string{
"TARGETARCH": "$TARGETARCH",
}

spec.Build.Steps = []dalec.BuildStep{
// Build a binary for a different architecture
// This should make `strip` fail.
//
// Note: The test is specifically using ppc64le as GOARCH
// because it seems alma/rockylinux do not error ons trip except for ppc64le.
// Even this is a stretch as that does not even work as expected at verison < v9.
{
Command: `cd src; if [ "${TARGETARCH}" = "ppc64le" ]; then export GOARCH=amd64; else export GOARCH=ppc64le; fi; go build -o ../bad-executable main.go`,
},
}
return spec
}

t.Run("strip enabled", func(t *testing.T) {
// Make sure that we get a build failure when strip is enabled
t.Parallel()
ctx := startTestSpan(ctx, t)
testEnv.RunTest(ctx, t, func(ctx context.Context, client gwclient.Client) {
spec := newSpec()
req := newSolveRequest(withSpec(ctx, t, spec), withBuildTarget(cfg.Target.Container))

_, err := client.Solve(ctx, req)
assert.ErrorType(t, pkgerrors.Cause(err), &moby_buildkit_v1_frontend.ExitError{})
})
})

t.Run("strip disabled", func(t *testing.T) {
t.Parallel()
ctx := startTestSpan(ctx, t)
testEnv.RunTest(ctx, t, func(ctx context.Context, client gwclient.Client) {
spec := newSpec()
spec.Artifacts.DisableStrip = true

req := newSolveRequest(withSpec(ctx, t, spec), withBuildTarget(cfg.Target.Container))
solveT(ctx, t, client, req)
})
})
}
1 change: 1 addition & 0 deletions test/target_almalinux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,6 @@ func TestAlmalinux8(t *testing.T) {
ID: "almalinux",
VersionID: "8",
},
SkipStripTest: true,
})
}
1 change: 1 addition & 0 deletions test/target_rockylinux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,6 @@ func TestRockylinux8(t *testing.T) {
ID: "rocky",
VersionID: "8",
},
SkipStripTest: true,
})
}
9 changes: 9 additions & 0 deletions website/docs/artifacts.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,12 @@ artifacts:
groups:
- name: mygroup
```

## Automatic Stripping

Some builds may not work with binary stripping, in which case you can disable
automatic stripping by setting `disable_strip: true`
This is a global setting that applies to all artifacts only.

If you want some binaries stripped and others not, you will need to manually
strip them in the build phase.