Skip to content

Commit f6a86ff

Browse files
committed
Use gbbgolang everywhere in u-root
Signed-off-by: Chris Koch <[email protected]>
1 parent f5422cf commit f6a86ff

File tree

9 files changed

+80
-74
lines changed

9 files changed

+80
-74
lines changed

pkg/uroot/builder/builder.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package builder
66

77
import (
88
gbbgolang "github.com/u-root/gobusybox/src/pkg/golang"
9-
"github.com/u-root/u-root/pkg/golang"
109
"github.com/u-root/u-root/pkg/ulog"
1110
"github.com/u-root/u-root/pkg/uroot/initramfs"
1211
)
@@ -21,7 +20,7 @@ var (
2120
// Opts are options passed to the Builder.Build function.
2221
type Opts struct {
2322
// Env is the Go compiler environment.
24-
Env golang.Environ
23+
Env gbbgolang.Environ
2524

2625
// Build options for building go binaries. Ultimate this holds all the
2726
// args that end up being passed to `go build`.

pkg/uroot/builder/gbb.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ package builder
77
import (
88
"errors"
99
"fmt"
10-
"os"
1110
"path"
1211
"path/filepath"
1312

1413
"github.com/u-root/gobusybox/src/pkg/bb"
15-
"github.com/u-root/gobusybox/src/pkg/golang"
1614
"github.com/u-root/u-root/pkg/cpio"
1715
"github.com/u-root/u-root/pkg/ulog"
1816
"github.com/u-root/u-root/pkg/uroot/initramfs"
@@ -56,19 +54,12 @@ func (b GBBBuilder) Build(l ulog.Logger, af *initramfs.Files, opts Opts) error {
5654
}
5755
bbPath := filepath.Join(opts.TempDir, "bb")
5856

59-
// gobusybox has its own copy of the golang package, but Environ stayed
60-
// (mostly) the same.
61-
env := golang.Environ{
62-
Context: opts.Env.Context,
63-
GO111MODULE: os.Getenv("GO111MODULE"),
64-
}
65-
6657
if len(opts.BinaryDir) == 0 {
6758
return fmt.Errorf("must specify binary directory")
6859
}
6960

7061
bopts := &bb.Opts{
71-
Env: env,
62+
Env: opts.Env,
7263
GenSrcDir: opts.TempDir,
7364
CommandPaths: opts.Packages,
7465
BinaryPath: bbPath,

pkg/uroot/builder/gbb_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"testing"
99

1010
gbbgolang "github.com/u-root/gobusybox/src/pkg/golang"
11-
"github.com/u-root/u-root/pkg/golang"
1211
"github.com/u-root/u-root/pkg/ulog/ulogtest"
1312
"github.com/u-root/u-root/pkg/uroot/initramfs"
1413
)
@@ -17,7 +16,7 @@ func TestGBBBuild(t *testing.T) {
1716
dir := t.TempDir()
1817

1918
opts := Opts{
20-
Env: golang.Default(),
19+
Env: gbbgolang.Default(),
2120
Packages: []string{
2221
"../test/foo",
2322
"../../../cmds/core/elvish",
@@ -35,6 +34,7 @@ func TestGBBBuild(t *testing.T) {
3534
mustContain := []string{
3635
"bbin/elvish",
3736
"bbin/foo",
37+
"bbin/bb",
3838
}
3939
for _, name := range mustContain {
4040
if !af.Contains(name) {

pkg/uroot/uroot.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"github.com/u-root/gobusybox/src/pkg/bb/findpkg"
2020
gbbgolang "github.com/u-root/gobusybox/src/pkg/golang"
2121
"github.com/u-root/u-root/pkg/cpio"
22-
"github.com/u-root/u-root/pkg/golang"
2322
"github.com/u-root/u-root/pkg/ldd"
2423
"github.com/u-root/u-root/pkg/uflag"
2524
"github.com/u-root/u-root/pkg/ulog"
@@ -42,7 +41,7 @@ const (
4241
// If an OS is not known it will return a reasonable u-root specific
4342
// default.
4443
func DefaultRamfs() *cpio.Archive {
45-
switch golang.Default().GOOS {
44+
switch gbbgolang.Default().GOOS {
4645
case "linux":
4746
return cpio.ArchiveFromRecords([]cpio.Record{
4847
cpio.Directory("bin", 0o755),
@@ -118,7 +117,9 @@ func (c Commands) TargetDir() string {
118117
// build environment.
119118
type Opts struct {
120119
// Env is the Golang build environment (GOOS, GOARCH, etc).
121-
Env golang.Environ
120+
//
121+
// If nil, gbbgolang.Default is used.
122+
Env *gbbgolang.Environ
122123

123124
// Commands specify packages to build using a specific builder.
124125
//
@@ -236,6 +237,14 @@ func CreateInitramfs(logger ulog.Logger, opts Opts) error {
236237
return fmt.Errorf("must give output file")
237238
}
238239

240+
env := gbbgolang.Default()
241+
if opts.Env != nil {
242+
env = *opts.Env
243+
}
244+
if opts.BuildOpts == nil {
245+
opts.BuildOpts = &gbbgolang.BuildOpts{}
246+
}
247+
239248
files := initramfs.NewFiles()
240249

241250
lookupEnv := findpkg.DefaultEnv()
@@ -257,11 +266,6 @@ func CreateInitramfs(logger ulog.Logger, opts Opts) error {
257266
opts.Commands[index].Packages = paths
258267
}
259268

260-
// Make sure this isn't a nil pointer
261-
if opts.BuildOpts == nil {
262-
opts.BuildOpts = &gbbgolang.BuildOpts{}
263-
}
264-
265269
// Add each build mode's commands to the archive.
266270
for _, cmds := range opts.Commands {
267271
builderTmpDir, err := os.MkdirTemp(opts.TempDir, "builder")
@@ -271,7 +275,7 @@ func CreateInitramfs(logger ulog.Logger, opts Opts) error {
271275

272276
// Build packages.
273277
bOpts := builder.Opts{
274-
Env: opts.Env,
278+
Env: env,
275279
BuildOpts: opts.BuildOpts,
276280
Packages: cmds.Packages,
277281
TempDir: builderTmpDir,

pkg/uroot/uroot_test.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"testing"
1414

1515
"github.com/u-root/u-root/pkg/cpio"
16-
"github.com/u-root/u-root/pkg/golang"
16+
"github.com/u-root/u-root/pkg/ulog/ulogtest"
1717
"github.com/u-root/u-root/pkg/uroot/builder"
1818
itest "github.com/u-root/u-root/pkg/uroot/initramfs/test"
1919
)
@@ -51,7 +51,6 @@ func TestCreateInitramfs(t *testing.T) {
5151
{
5252
name: "BB archive with ls and init",
5353
opts: Opts{
54-
Env: golang.Default(),
5554
TempDir: dir,
5655
ExtraFiles: nil,
5756
UseExistingInit: false,
@@ -80,7 +79,6 @@ func TestCreateInitramfs(t *testing.T) {
8079
{
8180
name: "no temp dir",
8281
opts: Opts{
83-
Env: golang.Default(),
8482
InitCmd: "init",
8583
DefaultShell: "",
8684
},
@@ -92,7 +90,6 @@ func TestCreateInitramfs(t *testing.T) {
9290
{
9391
name: "no commands",
9492
opts: Opts{
95-
Env: golang.Default(),
9693
TempDir: dir,
9794
},
9895
want: "",
@@ -103,7 +100,6 @@ func TestCreateInitramfs(t *testing.T) {
103100
{
104101
name: "init specified, but not in commands",
105102
opts: Opts{
106-
Env: golang.Default(),
107103
TempDir: dir,
108104
DefaultShell: "zoocar",
109105
InitCmd: "foobar",
@@ -125,7 +121,6 @@ func TestCreateInitramfs(t *testing.T) {
125121
{
126122
name: "init symlinked to absolute path",
127123
opts: Opts{
128-
Env: golang.Default(),
129124
TempDir: dir,
130125
InitCmd: "/bin/systemd",
131126
},
@@ -137,7 +132,6 @@ func TestCreateInitramfs(t *testing.T) {
137132
{
138133
name: "multi-mode archive",
139134
opts: Opts{
140-
Env: golang.Default(),
141135
TempDir: dir,
142136
ExtraFiles: nil,
143137
UseExistingInit: false,

pkg/vmtest/gotest.go

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,23 @@ import (
1313
"path/filepath"
1414
"testing"
1515

16-
"github.com/u-root/u-root/pkg/golang"
16+
gbbgolang "github.com/u-root/gobusybox/src/pkg/golang"
1717
"github.com/u-root/u-root/pkg/uio"
1818
"github.com/u-root/u-root/pkg/uroot"
1919
"github.com/u-root/u-root/pkg/vmtest/internal/json2test"
20+
"golang.org/x/tools/go/packages"
2021
)
2122

23+
func lookupPkgs(env gbbgolang.Environ, dir string, patterns ...string) ([]*packages.Package, error) {
24+
cfg := &packages.Config{
25+
Mode: packages.NeedName | packages.NeedFiles,
26+
Env: append(os.Environ(), env.Env()...),
27+
Dir: dir,
28+
Tests: true,
29+
}
30+
return packages.Load(cfg, patterns...)
31+
}
32+
2233
// GolangTest compiles the unit tests found in pkgs and runs them in a QEMU VM.
2334
func GolangTest(t *testing.T, pkgs []string, o *Options) {
2435
SkipWithoutQEMU(t)
@@ -54,10 +65,10 @@ func GolangTest(t *testing.T, pkgs []string, o *Options) {
5465
}
5566

5667
// Set up u-root build options.
57-
env := golang.Default()
68+
env := gbbgolang.Default()
5869
env.CgoEnabled = false
5970
env.GOARCH = TestArch()
60-
o.BuildOpts.Env = env
71+
o.BuildOpts.Env = &env
6172

6273
// Statically build tests and add them to the temporary directory.
6374
var tests []string
@@ -103,13 +114,27 @@ func GolangTest(t *testing.T, pkgs []string, o *Options) {
103114
if _, err := os.Stat(testFile); !os.IsNotExist(err) {
104115
tests = append(tests, pkg)
105116

106-
p, err := o.BuildOpts.Env.Package(pkg)
117+
pkgs, err := lookupPkgs(*o.BuildOpts.Env, "", pkg)
107118
if err != nil {
108-
t.Fatal(err)
119+
t.Fatalf("Failed to look up package %q: %v", pkg, err)
109120
}
121+
122+
// One directory = one package in standard Go, so
123+
// finding the first file's parent directory should
124+
// find us the package directory.
125+
var dir string
126+
for _, p := range pkgs {
127+
if len(p.GoFiles) > 0 {
128+
dir = filepath.Dir(p.GoFiles[0])
129+
}
130+
}
131+
if dir == "" {
132+
t.Fatalf("Could not find package directory for %q", pkg)
133+
}
134+
110135
// Optimistically copy any files in the pkg's
111136
// directory, in case e.g. a testdata dir is there.
112-
if err := copyRelativeFiles(p.Dir, filepath.Join(testDir, pkg)); err != nil {
137+
if err := copyRelativeFiles(dir, filepath.Join(testDir, pkg)); err != nil {
113138
t.Fatal(err)
114139
}
115140
}

pkg/vmtest/integration.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import (
1414
"strings"
1515
"testing"
1616

17+
gbbgolang "github.com/u-root/gobusybox/src/pkg/golang"
1718
"github.com/u-root/u-root/pkg/cp"
18-
"github.com/u-root/u-root/pkg/golang"
1919
"github.com/u-root/u-root/pkg/qemu"
2020
"github.com/u-root/u-root/pkg/testutil"
2121
"github.com/u-root/u-root/pkg/uio"
@@ -40,11 +40,6 @@ type Options struct {
4040
// possible.
4141
QEMUOpts qemu.Options
4242

43-
// DontSetEnv doesn't set the BuildOpts.Env and uses the user-supplied one.
44-
//
45-
// TODO: make uroot.Opts.Env a pointer?
46-
DontSetEnv bool
47-
4843
// Name is the test's name.
4944
//
5045
// If name is left empty, the calling function's function name will be
@@ -270,7 +265,7 @@ func QEMU(o *Options) (*qemu.Options, error) {
270265
// Set the initramfs.
271266
if len(o.QEMUOpts.Initramfs) == 0 {
272267
o.QEMUOpts.Initramfs = filepath.Join(o.TmpDir, "initramfs.cpio")
273-
if err := ChooseTestInitramfs(o.DontSetEnv, o.BuildOpts, o.Uinit, o.QEMUOpts.Initramfs); err != nil {
268+
if err := ChooseTestInitramfs(o.BuildOpts, o.Uinit, o.QEMUOpts.Initramfs); err != nil {
274269
return nil, err
275270
}
276271
}
@@ -312,7 +307,7 @@ func QEMU(o *Options) (*qemu.Options, error) {
312307
// Default to the override initramfs if one is specified in the UROOT_INITRAMFS
313308
// environment variable. Else, build an initramfs with the given parameters.
314309
// If no uinit was provided, the generic one is used.
315-
func ChooseTestInitramfs(dontSetEnv bool, o uroot.Opts, uinit, outputFile string) error {
310+
func ChooseTestInitramfs(o uroot.Opts, uinit, outputFile string) error {
316311
override := os.Getenv("UROOT_INITRAMFS")
317312
if len(override) > 0 {
318313
log.Printf("Overriding with initramfs %q", override)
@@ -324,7 +319,7 @@ func ChooseTestInitramfs(dontSetEnv bool, o uroot.Opts, uinit, outputFile string
324319
uinit = "github.com/u-root/u-root/integration/testcmd/generic/uinit"
325320
}
326321

327-
_, err := CreateTestInitramfs(dontSetEnv, o, uinit, outputFile)
322+
_, err := CreateTestInitramfs(o, uinit, outputFile)
328323
return err
329324
}
330325

@@ -333,12 +328,12 @@ func ChooseTestInitramfs(dontSetEnv bool, o uroot.Opts, uinit, outputFile string
333328
// one will be created.
334329
// The output file name is returned. It is the caller's responsibility to remove
335330
// the initramfs file after use.
336-
func CreateTestInitramfs(dontSetEnv bool, o uroot.Opts, uinit, outputFile string) (string, error) {
337-
if !dontSetEnv {
338-
env := golang.Default()
331+
func CreateTestInitramfs(o uroot.Opts, uinit, outputFile string) (string, error) {
332+
if o.Env == nil {
333+
env := gbbgolang.Default()
339334
env.CgoEnabled = false
340335
env.GOARCH = TestArch()
341-
o.Env = env
336+
o.Env = &env
342337
}
343338

344339
if o.UrootSource == "" {

0 commit comments

Comments
 (0)