From 26734051f793ede517136d8c99d59c9395a98ed8 Mon Sep 17 00:00:00 2001 From: Chris Koch Date: Sat, 5 Aug 2023 18:20:54 -0700 Subject: [PATCH] Use simplified golang.Default Signed-off-by: Chris Koch --- pkg/uroot/builder/binary.go | 7 +++++-- pkg/uroot/builder/builder.go | 2 +- pkg/uroot/builder/gbb.go | 3 +++ pkg/uroot/uroot.go | 2 +- pkg/vmtest/gotest.go | 10 ++++------ pkg/vmtest/integration.go | 5 +---- u-root.go | 6 +++--- 7 files changed, 18 insertions(+), 17 deletions(-) diff --git a/pkg/uroot/builder/binary.go b/pkg/uroot/builder/binary.go index 68ced2425e..c30999117e 100644 --- a/pkg/uroot/builder/binary.go +++ b/pkg/uroot/builder/binary.go @@ -16,7 +16,7 @@ import ( "golang.org/x/tools/go/packages" ) -func lookupPkgs(env gbbgolang.Environ, dir string, patterns ...string) ([]*packages.Package, error) { +func lookupPkgs(env *gbbgolang.Environ, dir string, patterns ...string) ([]*packages.Package, error) { cfg := &packages.Config{ Mode: packages.NeedName | packages.NeedFiles, Env: append(os.Environ(), env.Env()...), @@ -25,7 +25,7 @@ func lookupPkgs(env gbbgolang.Environ, dir string, patterns ...string) ([]*packa return packages.Load(cfg, patterns...) } -func dirFor(env gbbgolang.Environ, pkg string) (string, error) { +func dirFor(env *gbbgolang.Environ, pkg string) (string, error) { pkgs, err := lookupPkgs(env, "", pkg) if err != nil { return "", fmt.Errorf("failed to look up package %q: %v", pkg, err) @@ -60,6 +60,9 @@ func (BinaryBuilder) DefaultBinaryDir() string { // Build implements Builder.Build. func (BinaryBuilder) Build(l ulog.Logger, af *initramfs.Files, opts Opts) error { + if opts.Env == nil { + return fmt.Errorf("must specify Go build environment") + } result := make(chan error, len(opts.Packages)) var wg sync.WaitGroup diff --git a/pkg/uroot/builder/builder.go b/pkg/uroot/builder/builder.go index f898e035c4..e4f5ea1388 100644 --- a/pkg/uroot/builder/builder.go +++ b/pkg/uroot/builder/builder.go @@ -20,7 +20,7 @@ var ( // Opts are options passed to the Builder.Build function. type Opts struct { // Env is the Go compiler environment. - Env gbbgolang.Environ + Env *gbbgolang.Environ // Build options for building go binaries. Ultimate this holds all the // args that end up being passed to `go build`. diff --git a/pkg/uroot/builder/gbb.go b/pkg/uroot/builder/gbb.go index 6e146dec13..a935a90dc7 100644 --- a/pkg/uroot/builder/gbb.go +++ b/pkg/uroot/builder/gbb.go @@ -57,6 +57,9 @@ func (b GBBBuilder) Build(l ulog.Logger, af *initramfs.Files, opts Opts) error { if len(opts.BinaryDir) == 0 { return fmt.Errorf("must specify binary directory") } + if opts.Env == nil { + return fmt.Errorf("must specify Go build environment") + } bopts := &bb.Opts{ Env: opts.Env, diff --git a/pkg/uroot/uroot.go b/pkg/uroot/uroot.go index bffc3afe95..ea2e839472 100644 --- a/pkg/uroot/uroot.go +++ b/pkg/uroot/uroot.go @@ -239,7 +239,7 @@ func CreateInitramfs(logger ulog.Logger, opts Opts) error { env := gbbgolang.Default() if opts.Env != nil { - env = *opts.Env + env = opts.Env } if opts.BuildOpts == nil { opts.BuildOpts = &gbbgolang.BuildOpts{} diff --git a/pkg/vmtest/gotest.go b/pkg/vmtest/gotest.go index 342c5a6c20..3a22b751a4 100644 --- a/pkg/vmtest/gotest.go +++ b/pkg/vmtest/gotest.go @@ -19,7 +19,7 @@ import ( "golang.org/x/tools/go/packages" ) -func lookupPkgs(env gbbgolang.Environ, dir string, patterns ...string) ([]*packages.Package, error) { +func lookupPkgs(env *gbbgolang.Environ, dir string, patterns ...string) ([]*packages.Package, error) { cfg := &packages.Config{ Mode: packages.NeedName | packages.NeedFiles, Env: append(os.Environ(), env.Env()...), @@ -64,10 +64,8 @@ func GolangTest(t *testing.T, pkgs []string, o *Options) { } // Set up u-root build options. - env := gbbgolang.Default() - env.CgoEnabled = false - env.GOARCH = TestArch() - o.BuildOpts.Env = &env + env := gbbgolang.Default(gbbgolang.DisableCGO(), gbbgolang.WithGOARCH(TestArch())) + o.BuildOpts.Env = env // Statically build tests and add them to the temporary directory. var tests []string @@ -113,7 +111,7 @@ func GolangTest(t *testing.T, pkgs []string, o *Options) { if _, err := os.Stat(testFile); !os.IsNotExist(err) { tests = append(tests, pkg) - pkgs, err := lookupPkgs(*o.BuildOpts.Env, "", pkg) + pkgs, err := lookupPkgs(o.BuildOpts.Env, "", pkg) if err != nil { t.Fatalf("Failed to look up package %q: %v", pkg, err) } diff --git a/pkg/vmtest/integration.go b/pkg/vmtest/integration.go index 5cb6c15a3b..031f584be2 100644 --- a/pkg/vmtest/integration.go +++ b/pkg/vmtest/integration.go @@ -330,10 +330,7 @@ func ChooseTestInitramfs(o uroot.Opts, uinit, outputFile string) error { // the initramfs file after use. func CreateTestInitramfs(o uroot.Opts, uinit, outputFile string) (string, error) { if o.Env == nil { - env := gbbgolang.Default() - env.CgoEnabled = false - env.GOARCH = TestArch() - o.Env = &env + o.Env = gbbgolang.Default(gbbgolang.DisableCGO(), gbbgolang.WithGOARCH(TestArch())) } if o.UrootSource == "" { diff --git a/u-root.go b/u-root.go index 2d18274e8a..4fdec58b59 100644 --- a/u-root.go +++ b/u-root.go @@ -139,7 +139,7 @@ func writeBuildStats(stats buildStats, path string) error { return nil } -func generateLabel(env gbbgolang.Environ) string { +func generateLabel(env *gbbgolang.Environ) string { var baseCmds []string if len(flag.Args()) > 0 { // Use the last component of the name to keep the label short @@ -264,7 +264,7 @@ func canFindSource(dir string) error { // Main is a separate function so defers are run on return, which they wouldn't // on exit. -func Main(l ulog.Logger, env gbbgolang.Environ, buildOpts *gbbgolang.BuildOpts) error { +func Main(l ulog.Logger, env *gbbgolang.Environ, buildOpts *gbbgolang.BuildOpts) error { v, err := env.Version() if err != nil { l.Printf("Could not get environment's Go version, using runtime's version: %v", err) @@ -371,7 +371,7 @@ func Main(l ulog.Logger, env gbbgolang.Environ, buildOpts *gbbgolang.BuildOpts) } opts := uroot.Opts{ - Env: &env, + Env: env, Commands: c, UrootSource: *urootSourceDir, TempDir: tempDir,