From f1ce1c0a8cc1a365acccd5899b1a6b178b2bc59e Mon Sep 17 00:00:00 2001 From: Brian Goff Date: Thu, 18 Jul 2024 16:31:16 -0700 Subject: [PATCH] Use native platform to fetch sources, generate buildroot, etc 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 --- frontend/azlinux/handle_container.go | 2 +- frontend/azlinux/handle_depsonly.go | 2 +- frontend/azlinux/handle_rpm.go | 20 ++++++++++++++------ frontend/rpm/handle_buildroot.go | 2 ++ frontend/rpm/handle_sources.go | 2 ++ 5 files changed, 20 insertions(+), 8 deletions(-) diff --git a/frontend/azlinux/handle_container.go b/frontend/azlinux/handle_container.go index f0bd487df..f7fb5d01a 100644 --- a/frontend/azlinux/handle_container.go +++ b/frontend/azlinux/handle_container.go @@ -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) } diff --git a/frontend/azlinux/handle_depsonly.go b/frontend/azlinux/handle_depsonly.go index 93c2856d4..a7499f8b8 100644 --- a/frontend/azlinux/handle_depsonly.go +++ b/frontend/azlinux/handle_depsonly.go @@ -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 } diff --git a/frontend/azlinux/handle_rpm.go b/frontend/azlinux/handle_rpm.go index 5db2e4375..648c7505a 100644 --- a/frontend/azlinux/handle_rpm.go +++ b/frontend/azlinux/handle_rpm.go @@ -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 } @@ -64,18 +64,26 @@ func installBuildDeps(w worker, spec *dalec.Spec, targetKey string, opts ...llb. } } -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...) - base = base.With(installBuildDeps(w, spec, targetKey, 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 } - - br, err := rpm.SpecToBuildrootLLB(base, spec, sOpt, targetKey, opts...) + native = native.With(installBuildDeps(w, spec, targetKey, opts...)) + 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(installBuildDeps(w, spec, targetKey, opts...)) st := rpm.Build(br, base, specPath, opts...) return frontend.MaybeSign(ctx, client, st, spec, targetKey, sOpt) diff --git a/frontend/rpm/handle_buildroot.go b/frontend/rpm/handle_buildroot.go index 7438d8230..60722712f 100644 --- a/frontend/rpm/handle_buildroot.go +++ b/frontend/rpm/handle_buildroot.go @@ -21,6 +21,8 @@ func HandleBuildroot(wf WorkerFunc) gwclient.BuildFunc { 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. worker, err := wf(sOpt.Resolver, spec, targetKey) if err != nil { return nil, nil, err diff --git a/frontend/rpm/handle_sources.go b/frontend/rpm/handle_sources.go index 22b4fd64b..6449354a2 100644 --- a/frontend/rpm/handle_sources.go +++ b/frontend/rpm/handle_sources.go @@ -25,6 +25,8 @@ func HandleSources(wf WorkerFunc) gwclient.BuildFunc { 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