Skip to content

Commit

Permalink
More CR.
Browse files Browse the repository at this point in the history
Signed-off-by: Roger Standridge <[email protected]>
  • Loading branch information
archie2x committed Sep 19, 2024
1 parent 81f0c36 commit be6e468
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/cmd/makebb/makebb.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ func main() {
l.Fatal(err)
}

l.Printf("Build environment: %s", env)
l.Printf("Compiler: %s", env.Compiler.VersionOutput)
l.Printf("Build environment: %s\n", env)
l.Printf("Compiler: %s\n", env.Compiler.VersionOutput)

tmpDir := *genDir
remove := false
Expand Down
34 changes: 16 additions & 18 deletions src/pkg/golang/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const (
CompilerUnkown
)

// cached information re: the compiler
// Cached information about the compiler used.
type Compiler struct {
Path string
Identifier string // e.g. 'tinygo' or 'go'
Expand All @@ -34,7 +34,7 @@ type Compiler struct {
IsInit bool // CompilerInit() succeeded
}

// map the compiler's identifier ("tinygo" or "go") to enum
// Map the compiler's identifier ("tinygo" or "go") to enum.
func CompilerTypeFromString(name string) CompilerType {
val, ok := map[string]CompilerType{
"go": CompilerGo,
Expand All @@ -46,15 +46,15 @@ func CompilerTypeFromString(name string) CompilerType {
return CompilerUnkown
}

// WithCompiler sets the compiler for Build() / BuildDir()
// Sets the compiler for Build() / BuildDir() functions.
func WithCompiler(p string) Opt {
return func(c *Environ) {
c.Compiler.Path = p
c.Compiler.IsInit = false
}
}

// compilerCmd returns a compiler command to be run in the environment.
// Returns a compiler command to be run in the environment.
func (c Environ) compilerCmd(gocmd string, args ...string) *exec.Cmd {
goBin := c.Compiler.Path
if "" == goBin {
Expand All @@ -70,8 +70,7 @@ func (c Environ) compilerCmd(gocmd string, args ...string) *exec.Cmd {
return cmd
}

// if go-compiler specified, resolve absolute-path from PATH,
// otherwise return 'nil'.
// If go-compiler specified, return its absolute-path, otherwise return 'nil'.
func (c *Environ) compilerAbs() error {
if c.Compiler.Path != "" {
fname, err := exec.LookPath(string(c.Compiler.Path))
Expand All @@ -86,29 +85,29 @@ func (c *Environ) compilerAbs() error {
return nil
}

// runs compilerCmd("version") and parse/caches output, to environ.Compiler
// Runs compilerCmd("version") and parse/caches output to c.Compiler.
func (c *Environ) CompilerInit() error {

if c.Compiler.IsInit {
return nil
}

c.compilerAbs()

cmd := c.compilerCmd("version")
v, err := cmd.CombinedOutput()
vb, err := cmd.CombinedOutput()
if err != nil {
return err
}
v := string(vb)

efmt := "go-compiler 'version' output unrecognized: %v"
s := strings.Fields(string(v))
s := strings.Fields(v)
if len(s) < 1 {
return fmt.Errorf(efmt, string(v))
return fmt.Errorf(efmt, v)
}

compiler := c.Compiler
compiler.VersionOutput = string(v)
compiler.VersionOutput = strings.TrimSpace(v)
compiler.Identifier = s[0]
compiler.Type = CompilerTypeFromString(compiler.Identifier)
compiler.IsInit = true
Expand All @@ -117,15 +116,15 @@ func (c *Environ) CompilerInit() error {

case CompilerGo:
if len(s) < 3 {
return fmt.Errorf(efmt, string(v))
return fmt.Errorf(efmt, v)
}
compiler.Version = s[2]
compiler.VersionGo = s[2]

case CompilerTinygo:
// e.g. "tinygo version 0.33.0 darwin/arm64 (using go version go1.22.2 and LLVM version 18.1.2)"
if len(s) < 8 {
return fmt.Errorf(efmt, string(v))
return fmt.Errorf(efmt, v)
}
compiler.Version = s[2]
compiler.VersionGo = s[7]
Expand Down Expand Up @@ -156,14 +155,14 @@ func (c *Environ) CompilerInit() error {
}

case CompilerUnkown:
return fmt.Errorf(efmt, string(v))
return fmt.Errorf(efmt, v)
}
c.Compiler = compiler
return nil
}

// Version returns the Go version string that runtime.Version would return for
// the Go compiler in this environ.
// Returns the Go version string that runtime.Version would return for the Go
// compiler in this environ.
func (c *Environ) Version() (string, error) {
if err := c.CompilerInit(); err != nil {
return "", err
Expand All @@ -172,7 +171,6 @@ func (c *Environ) Version() (string, error) {
}

func (c Environ) build(dirPath string, binaryPath string, pattern []string, opts *BuildOpts) error {

if err := c.CompilerInit(); err != nil {
return err
}
Expand Down

0 comments on commit be6e468

Please sign in to comment.