Skip to content

Commit 763032c

Browse files
committed
Add option to disable strip on binaries
This is required for some builds, namely building go. On debian targets there is a pretty easy work-around (without this change), but for rpm there is not. Signed-off-by: Brian Goff <[email protected]>
1 parent 38052bd commit 763032c

File tree

12 files changed

+172
-0
lines changed

12 files changed

+172
-0
lines changed

artifacts.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ type Artifacts struct {
4747
Users []AddUserConfig `yaml:"users,omitempty" json:"users,omitempty"`
4848
// Groups is a list of groups to add to the system when the package is installed.
4949
Groups []AddGroupConfig `yaml:"groups,omitempty" json:"groups,omitempty"`
50+
51+
// DisableStrip is used to disable stripping of artifacts.
52+
DisableStrip bool `yaml:"disable_strip,omitempty" json:"disable_strip,omitempty"`
5053
}
5154

5255
type ArtifactSymlinkConfig struct {

docs/spec.schema.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,12 @@
209209
],
210210
"description": "DataDirs is a list of read-only architecture-independent data files, to be placed in /usr/share/"
211211
},
212+
"disable_strip": {
213+
"type": [
214+
"boolean"
215+
],
216+
"description": "DisableStrip is used to disable stripping of artifacts."
217+
},
212218
"docs": {
213219
"additionalProperties": {
214220
"$ref": "#/$defs/ArtifactConfig"

packaging/linux/deb/template_rules.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,14 @@ func (w *rulesWrapper) OverrideSystemd() (fmt.Stringer, error) {
182182

183183
return b, nil
184184
}
185+
186+
func (w *rulesWrapper) OverrideStrip() fmt.Stringer {
187+
artifacts := w.Spec.GetArtifacts(w.target)
188+
189+
buf := &strings.Builder{}
190+
191+
if artifacts.DisableStrip {
192+
buf.WriteString("override_dh_strip:\n")
193+
}
194+
return buf
195+
}

packaging/linux/deb/template_rules_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,17 @@ Depends: ${misc:Depends},
190190
actual = strings.TrimSpace(buf.String())
191191
assert.Check(t, cmp.Equal(actual, strings.TrimSpace(expect)))
192192
}
193+
194+
func TestRules_OverrideStrip(t *testing.T) {
195+
w := &rulesWrapper{
196+
Spec: &dalec.Spec{},
197+
}
198+
199+
got := w.OverrideStrip()
200+
assert.Equal(t, got.String(), "")
201+
202+
w.Spec.Artifacts.DisableStrip = true
203+
got = w.OverrideStrip()
204+
expect := "override_dh_strip:\n"
205+
assert.Equal(t, got.String(), expect)
206+
}

packaging/linux/deb/templates/debian_rules.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ override_dh_dwz:
3434

3535
{{ .OverrideSystemd }}
3636

37+
{{ .OverrideStrip }}
38+
3739
%:
3840
dh $@ -v
3941

packaging/linux/rpm/template.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const gomodsName = "__gomods"
1818
const buildScriptName = "build.sh"
1919

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

780+
func (w *specWrapper) DisableStrip() string {
781+
artifacts := w.Spec.GetArtifacts(w.Target)
782+
if artifacts.DisableStrip {
783+
return "%global __strip /bin/true"
784+
}
785+
return ""
786+
}
787+
779788
// WriteSpec generates an rpm spec from the provided [dalec.Spec] and distro target and writes it to the passed in writer
780789
func WriteSpec(spec *dalec.Spec, target string, w io.Writer) error {
781790
s := &specWrapper{spec, target}

packaging/linux/rpm/template_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,5 +838,22 @@ OrderWithRequires(postun): systemd
838838
want := "Requires(post): /usr/sbin/groupadd, /usr/bin/getent\n"
839839
assert.Equal(t, got, want)
840840
})
841+
}
842+
843+
func TestTemplate_DisableStrip(t *testing.T) {
844+
spec := &dalec.Spec{
845+
Artifacts: dalec.Artifacts{
846+
DisableStrip: true,
847+
},
848+
}
841849

850+
w := &specWrapper{Spec: spec}
851+
want := `%global __strip /bin/true`
852+
got := w.DisableStrip()
853+
assert.Equal(t, got, want)
854+
855+
spec.Artifacts.DisableStrip = false
856+
want = ""
857+
got = w.DisableStrip()
858+
assert.Equal(t, got, want)
842859
}

targets/linux/rpm/almalinux/common.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
var (
1212
builderPackages = []string{
13+
"binutils",
1314
"rpm-build",
1415
"ca-certificates",
1516
}

test/linux_target_test.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package test
22

33
import (
4+
_ "embed"
5+
46
"context"
57
"encoding/json"
68
"errors"
@@ -16,9 +18,11 @@ import (
1618
gwclient "github.com/moby/buildkit/frontend/gateway/client"
1719
moby_buildkit_v1_frontend "github.com/moby/buildkit/frontend/gateway/pb"
1820
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
21+
pkgerrors "github.com/pkg/errors"
1922
"golang.org/x/exp/maps"
2023
"gotest.tools/v3/assert"
2124
"gotest.tools/v3/assert/cmp"
25+
"gotest.tools/v3/skip"
2226
)
2327

2428
type workerConfig struct {
@@ -77,6 +81,8 @@ type testLinuxConfig struct {
7781
Libdir string
7882
Worker workerConfig
7983
Release OSRelease
84+
85+
SkipStripTest bool
8086
}
8187

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

15631569
t.Run("inherited dependencies", func(t *testing.T) {
1570+
t.Parallel()
15641571
testMixGlobalTargetDependencies(ctx, t, testConfig)
15651572
})
1573+
1574+
t.Run("disable strip", func(t *testing.T) {
1575+
t.Parallel()
1576+
testDisableStrip(ctx, t, testConfig)
1577+
})
15661578
}
15671579

15681580
func testCustomLinuxWorker(ctx context.Context, t *testing.T, targetCfg targetConfig, workerCfg workerConfig) {
@@ -2342,3 +2354,89 @@ func testMixGlobalTargetDependencies(ctx context.Context, t *testing.T, cfg test
23422354
})
23432355
})
23442356
}
2357+
2358+
func testDisableStrip(ctx context.Context, t *testing.T, cfg testLinuxConfig) {
2359+
skip.If(t, cfg.SkipStripTest, "skipping test as it is not supported for this target: "+cfg.Target.Container)
2360+
2361+
newSpec := func() *dalec.Spec {
2362+
spec := newSimpleSpec()
2363+
spec.Args = map[string]string{
2364+
"TARGETARCH": "",
2365+
}
2366+
2367+
spec.Sources = map[string]dalec.Source{
2368+
"src": {
2369+
Generate: []*dalec.SourceGenerator{
2370+
{
2371+
Gomod: &dalec.GeneratorGomod{},
2372+
},
2373+
},
2374+
Inline: &dalec.SourceInline{
2375+
Dir: &dalec.SourceInlineDir{
2376+
Files: map[string]*dalec.SourceInlineFile{
2377+
"main.go": {Contents: gomodFixtureMain},
2378+
"go.mod": {Contents: gomodFixtureMod},
2379+
"go.sum": {Contents: gomodFixtureSum},
2380+
},
2381+
},
2382+
},
2383+
},
2384+
}
2385+
2386+
spec.Dependencies = &dalec.PackageDependencies{
2387+
Build: map[string]dalec.PackageConstraints{
2388+
cfg.GetPackage("golang"): {},
2389+
},
2390+
}
2391+
spec.Artifacts = dalec.Artifacts{
2392+
Binaries: map[string]dalec.ArtifactConfig{
2393+
"bad-executable": {},
2394+
},
2395+
Libs: map[string]dalec.ArtifactConfig{
2396+
"bad-executable": {},
2397+
},
2398+
}
2399+
2400+
spec.Build.Env = map[string]string{
2401+
"TARGETARCH": "$TARGETARCH",
2402+
}
2403+
2404+
spec.Build.Steps = []dalec.BuildStep{
2405+
// Build a binary for a different architecture
2406+
// This should make `strip` fail.
2407+
//
2408+
// Note: The test is specifically using ppc64le as GOARCH
2409+
// because it seems alma/rockylinux do not error ons trip except for ppc64le.
2410+
// Even this is a stretch as that does not even work as expected at verison < v9.
2411+
{
2412+
Command: `cd src; if [ "${TARGETARCH}" = "ppc64le" ]; then export GOARCH=amd64; else export GOARCH=ppc64le; fi; go build -o ../bad-executable main.go`,
2413+
},
2414+
}
2415+
return spec
2416+
}
2417+
2418+
t.Run("strip enabled", func(t *testing.T) {
2419+
// Make sure that we get a build failure when strip is enabled
2420+
t.Parallel()
2421+
ctx := startTestSpan(ctx, t)
2422+
testEnv.RunTest(ctx, t, func(ctx context.Context, client gwclient.Client) {
2423+
spec := newSpec()
2424+
req := newSolveRequest(withSpec(ctx, t, spec), withBuildTarget(cfg.Target.Container))
2425+
2426+
_, err := client.Solve(ctx, req)
2427+
assert.ErrorType(t, pkgerrors.Cause(err), &moby_buildkit_v1_frontend.ExitError{})
2428+
})
2429+
})
2430+
2431+
t.Run("strip disabled", func(t *testing.T) {
2432+
t.Parallel()
2433+
ctx := startTestSpan(ctx, t)
2434+
testEnv.RunTest(ctx, t, func(ctx context.Context, client gwclient.Client) {
2435+
spec := newSpec()
2436+
spec.Artifacts.DisableStrip = true
2437+
2438+
req := newSolveRequest(withSpec(ctx, t, spec), withBuildTarget(cfg.Target.Container))
2439+
solveT(ctx, t, client, req)
2440+
})
2441+
})
2442+
}

test/target_almalinux_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,6 @@ func TestAlmalinux8(t *testing.T) {
7777
ID: "almalinux",
7878
VersionID: "8",
7979
},
80+
SkipStripTest: true,
8081
})
8182
}

0 commit comments

Comments
 (0)