Skip to content

Commit

Permalink
Use simplified golang.Default
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Koch <[email protected]>
  • Loading branch information
hugelgupf authored and orangecms committed Aug 7, 2023
1 parent 7d1d7bf commit 2673405
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 17 deletions.
7 changes: 5 additions & 2 deletions pkg/uroot/builder/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()...),
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/uroot/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
3 changes: 3 additions & 0 deletions pkg/uroot/builder/gbb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion pkg/uroot/uroot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
10 changes: 4 additions & 6 deletions pkg/vmtest/gotest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()...),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
5 changes: 1 addition & 4 deletions pkg/vmtest/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down
6 changes: 3 additions & 3 deletions u-root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 2673405

Please sign in to comment.