Skip to content

Commit

Permalink
Use native platform to fetch sources, generate buildroot, etc
Browse files Browse the repository at this point in the history
When building for a non-native platform (typically under qemu), use the
native platform for most operations that do not need platform specific
things, such as applying patches, tarring up sources, and fetching
go module deps.

Signed-off-by: Brian Goff <[email protected]>
  • Loading branch information
cpuguy83 committed Aug 28, 2024
1 parent a05e530 commit e103c78
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 17 deletions.
2 changes: 1 addition & 1 deletion frontend/azlinux/handle_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func handleContainer(w worker) gwclient.BuildFunc {

pg := dalec.ProgressGroup("Building " + targetKey + " container: " + spec.Name)

rpmDir, err := specToRpmLLB(ctx, w, client, spec, sOpt, targetKey, pg, dalec.WithPlatform(platform))
rpmDir, err := specToRpmLLB(ctx, w, client, spec, sOpt, targetKey, platform, pg)
if err != nil {
return nil, nil, fmt.Errorf("error creating rpm: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/azlinux/handle_depsonly.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func handleDepsOnly(w worker) gwclient.BuildFunc {
return nil, nil, err
}

st, err := specToContainerLLB(w, spec, targetKey, rpmDir, files, sOpt, pg)
st, err := specToContainerLLB(w, spec, targetKey, rpmDir, files, sOpt, pg, dalec.WithPlatform(platform))
if err != nil {
return nil, nil, err
}
Expand Down
29 changes: 19 additions & 10 deletions frontend/azlinux/handle_rpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func handleRPM(w worker) gwclient.BuildFunc {
return nil, nil, err
}

st, err := specToRpmLLB(ctx, w, client, spec, sOpt, targetKey, pg, dalec.WithPlatform(platform))
st, err := specToRpmLLB(ctx, w, client, spec, sOpt, targetKey, platform, pg)
if err != nil {
return nil, nil, err
}
Expand All @@ -53,7 +53,7 @@ func handleRPM(w worker) gwclient.BuildFunc {
}

// Creates and installs an rpm meta-package that requires the passed in deps as runtime-dependencies
func installBuildDepsPackage(target string, packageName string, w worker, deps map[string]dalec.PackageConstraints, installOpts ...installOpt) installFunc {
func installBuildDepsPackage(target string, packageName string, w worker, deps map[string]dalec.PackageConstraints, platform *ocispecs.Platform, installOpts ...installOpt) installFunc {
// depsOnly is a simple dalec spec that only includes build dependencies and their constraints
depsOnly := dalec.Spec{
Name: fmt.Sprintf("%s-build-dependencies", packageName),
Expand All @@ -70,7 +70,7 @@ func installBuildDepsPackage(target string, packageName string, w worker, deps m
pg := dalec.ProgressGroup("Building container for build dependencies")

// create an RPM with just the build dependencies, using our same base worker
rpmDir, err := specToRpmLLB(ctx, w, client, &depsOnly, sOpt, target, pg)
rpmDir, err := specToRpmLLB(ctx, w, client, &depsOnly, sOpt, target, platform, pg)
if err != nil {
return nil, err
}
Expand All @@ -91,7 +91,7 @@ func installBuildDepsPackage(target string, packageName string, w worker, deps m
}
}

func installBuildDeps(ctx context.Context, w worker, client gwclient.Client, spec *dalec.Spec, targetKey string, opts ...llb.ConstraintsOpt) (llb.StateOption, error) {
func installBuildDeps(ctx context.Context, w worker, client gwclient.Client, spec *dalec.Spec, targetKey string, platform *ocispecs.Platform, opts ...llb.ConstraintsOpt) (llb.StateOption, error) {
deps := spec.GetBuildDeps(targetKey)
if len(deps) == 0 {
return func(in llb.State) llb.State { return in }, nil
Expand All @@ -104,7 +104,7 @@ func installBuildDeps(ctx context.Context, w worker, client gwclient.Client, spe

opts = append(opts, dalec.ProgressGroup("Install build deps"))

installOpt, err := installBuildDepsPackage(targetKey, spec.Name, w, deps, installWithConstraints(opts))(ctx, client, sOpt)
installOpt, err := installBuildDepsPackage(targetKey, spec.Name, w, deps, platform, installWithConstraints(opts))(ctx, client, sOpt)
if err != nil {
return nil, err
}
Expand All @@ -114,23 +114,32 @@ func installBuildDeps(ctx context.Context, w worker, client gwclient.Client, spe
}, nil
}

func specToRpmLLB(ctx context.Context, w worker, client gwclient.Client, spec *dalec.Spec, sOpt dalec.SourceOpts, targetKey string, opts ...llb.ConstraintsOpt) (llb.State, error) {
base, err := w.Base(sOpt, opts...)
func specToRpmLLB(ctx context.Context, w worker, client gwclient.Client, spec *dalec.Spec, sOpt dalec.SourceOpts, targetKey string, platform *ocispecs.Platform, opts ...llb.ConstraintsOpt) (llb.State, error) {
// Generate the buildroot with the native platform
native, err := w.Base(sOpt, opts...)
if err != nil {
return llb.Scratch(), err
}

installOpt, err := installBuildDeps(ctx, w, client, spec, targetKey, opts...)
installDeps, err := installBuildDeps(ctx, w, client, spec, targetKey, platform, opts...)
if err != nil {
return llb.Scratch(), err
}
base = base.With(installOpt)

br, err := rpm.SpecToBuildrootLLB(base, spec, sOpt, targetKey, opts...)
native = native.With(installDeps)
br, err := rpm.SpecToBuildrootLLB(native, spec, sOpt, targetKey, opts...)
if err != nil {
return llb.Scratch(), err
}
specPath := filepath.Join("SPECS", spec.Name, spec.Name+".spec")

// Build the RPM with the target platform
opts = append(opts, dalec.WithPlatform(platform))
base, err := w.Base(sOpt, opts...)
if err != nil {
return llb.Scratch(), err
}
base = base.With(installDeps)
st := rpm.Build(br, base, specPath, opts...)

return frontend.MaybeSign(ctx, client, st, spec, targetKey, sOpt)
Expand Down
4 changes: 2 additions & 2 deletions frontend/azlinux/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func handleDebug(w worker) gwclient.BuildFunc {
}

func getSpecWorker(ctx context.Context, w worker, client gwclient.Client, sOpt dalec.SourceOpts) rpm.WorkerFunc {
return func(resolver llb.ImageMetaResolver, spec *dalec.Spec, targetKey string, opts ...llb.ConstraintsOpt) (llb.State, error) {
return func(resolver llb.ImageMetaResolver, spec *dalec.Spec, targetKey string, platform *ocispecs.Platform, opts ...llb.ConstraintsOpt) (llb.State, error) {
st, err := w.Base(sOpt, opts...)
if err != nil {
return llb.Scratch(), err
Expand All @@ -83,7 +83,7 @@ func getSpecWorker(ctx context.Context, w worker, client gwclient.Client, sOpt d
return llb.Scratch(), errors.New("spec contains go modules but does not have golang in build deps")
}

installOpt, err := installBuildDeps(ctx, w, client, spec, targetKey, opts...)
installOpt, err := installBuildDeps(ctx, w, client, spec, targetKey, platform, opts...)
if err != nil {
return llb.Scratch(), err
}
Expand Down
6 changes: 4 additions & 2 deletions frontend/rpm/handle_buildroot.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
)

type WorkerFunc func(resolver llb.ImageMetaResolver, spec *dalec.Spec, targetKey string, opts ...llb.ConstraintsOpt) (llb.State, error)
type WorkerFunc func(resolver llb.ImageMetaResolver, spec *dalec.Spec, targetKey string, platform *ocispecs.Platform, opts ...llb.ConstraintsOpt) (llb.State, error)

func HandleBuildroot(wf WorkerFunc) gwclient.BuildFunc {
return func(ctx context.Context, client gwclient.Client) (*gwclient.Result, error) {
Expand All @@ -21,7 +21,9 @@ func HandleBuildroot(wf WorkerFunc) gwclient.BuildFunc {
return nil, nil, err
}

worker, err := wf(sOpt.Resolver, spec, targetKey)
// Note, we are not passing platform down here because everything should
// be able to work regardless of platform, so prefer the native platform.
worker, err := wf(sOpt.Resolver, spec, targetKey, platform)
if err != nil {
return nil, nil, err
}
Expand Down
4 changes: 3 additions & 1 deletion frontend/rpm/handle_sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ func HandleSources(wf WorkerFunc) gwclient.BuildFunc {
return nil, nil, err
}

worker, err := wf(sOpt.Resolver, spec, targetKey)
worker, err := wf(sOpt.Resolver, spec, targetKey, platform)
if err != nil {
return nil, nil, err
}

// Note, we are not passing platform down here because everything should
// be able to work regardless of platform, so prefer the native platform.
sources, err := Dalec2SourcesLLB(worker, spec, sOpt)
if err != nil {
return nil, nil, err
Expand Down

0 comments on commit e103c78

Please sign in to comment.