Skip to content

Commit

Permalink
feat: opts builder
Browse files Browse the repository at this point in the history
  • Loading branch information
james-d-elliott committed May 3, 2023
1 parent 5f6a8ea commit 16cc89c
Show file tree
Hide file tree
Showing 10 changed files with 314 additions and 187 deletions.
4 changes: 4 additions & 0 deletions const.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package main

const (
gobin = "go"
)

const (
flagNameRace = "race"
flagNameMSAN = "msan"
Expand Down
97 changes: 97 additions & 0 deletions constraints.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package main

import "github.com/hashicorp/go-version"

var (
flagConstraints map[string]version.Constraints
platformConstraints []PlatformConstraint
experimentConstraints map[string]version.Constraints
)

// Parse all of the constraints as efficiently as possible.
func init() {
var (
ok bool
constraint version.Constraints
err error
)

parsed := map[string]version.Constraints{}

flagConstraints = map[string]version.Constraints{}

flagConstraintsInputs := []struct {
name string
constraint string
}{
{"C", ">= 1.20"},
{flagNameASAN, ">= 1.18"},
{flagNameCover, ">= 1.20"},
{flagNameCoverPKG, ">= 1.20"},
{flagNameBuildVCS, ">= 1.18"},
{flagNameModCacheRW, ">= 1.14"},
{flagNameMod, ">= 1.11"},
{flagNameModFile, ">= 1.14"},
{flagNameOverlay, ">= 1.16"},
{flagNameProfileGuidedOptimization, ">= 1.20"},
{flagNameTrimPath, ">= 1.13"},
}

for _, input := range flagConstraintsInputs {
if constraint, ok = parsed[input.constraint]; ok {
flagConstraints[input.name] = constraint

continue
}

if constraint, err = version.NewConstraint(input.constraint); err != nil {
panic(err)
}

flagConstraints[input.name] = constraint
parsed[input.constraint] = constraint
}

platformConstraintsInputs := []struct {
constraint string
platforms []Platform
}{
{"<= 1.0", Platforms_1_0},
{">= 1.1, < 1.3", Platforms_1_1},
{">= 1.3, < 1.4", Platforms_1_3},
{">= 1.4, < 1.5", Platforms_1_4},
{">= 1.5, < 1.6", Platforms_1_5},
{">= 1.6, < 1.7", Platforms_1_6},
{">= 1.7, < 1.8", Platforms_1_7},
{">= 1.8, < 1.9", Platforms_1_8},
{">= 1.9, < 1.10", Platforms_1_9},
{">= 1.10, < 1.11", Platforms_1_10},
{">= 1.11, < 1.12", Platforms_1_11},
{">= 1.12, < 1.13", Platforms_1_12},
{">= 1.13, < 1.14", Platforms_1_13},
{">= 1.14, < 1.15", Platforms_1_14},
{">= 1.15, < 1.16", Platforms_1_15},
{">= 1.16, < 1.17", Platforms_1_16},
{">= 1.17, < 1.18", Platforms_1_17},
{">= 1.18, < 1.19", Platforms_1_18},
{">= 1.19, < 1.20", Platforms_1_19},
{">= 1.20, < 1.21", Platforms_1_20},
}

platformConstraints = make([]PlatformConstraint, len(platformConstraintsInputs))

for i, input := range platformConstraintsInputs {
if constraint, ok = parsed[input.constraint]; ok {
platformConstraints[i] = PlatformConstraint{Constraints: constraint, Platforms: input.platforms}

continue
}

if constraint, err = version.NewConstraint(input.constraint); err != nil {
panic(err)
}

platformConstraints[i] = PlatformConstraint{Constraints: constraint, Platforms: input.platforms}
parsed[input.constraint] = constraint
}
}
86 changes: 4 additions & 82 deletions go.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,13 @@ type OutputTemplateData struct {

// GoCrossCompile
func GoCrossCompile(opts *CompileOpts) error {
env := append(os.Environ(),
"GOOS="+opts.Platform.OS,
"GOARCH="+opts.Platform.Arch)

if opts.Cc != "" {
env = append(env, "CC="+opts.Cc)
}
if opts.Cxx != "" {
env = append(env, "CXX="+opts.Cxx)
}

// If we're building for our own platform, then enable cgo always. We
// respect the CGO_ENABLED flag if that is explicitly set on the platform.
if !opts.Cgo && os.Getenv("CGO_ENABLED") != "0" {
opts.Cgo = runtime.GOOS == opts.Platform.OS &&
runtime.GOARCH == opts.Platform.Arch
}

// If cgo is enabled then set that env var
if opts.Cgo {
env = append(env, "CGO_ENABLED=1")
} else {
env = append(env, "CGO_ENABLED=0")
}

var outputPath bytes.Buffer
tpl, err := template.New("output").Parse(opts.OutputTpl)
if err != nil {
Expand Down Expand Up @@ -98,75 +80,15 @@ func GoCrossCompile(opts *CompileOpts) error {
opts.PackagePath = ""
}

args := []string{"build"}

if opts.Rebuild {
args = append(args, "-a")
}

if opts.TrimPath {
args = append(args, "-trimpath")
}

if opts.ModMode != "" {
args = append(args, fmtFlag("mod", opts.ModMode))
}

if opts.BuildMode != "" {
args = append(args, fmtFlag("buildmode", opts.BuildMode))
}

if opts.BuildVCS != "" {
args = append(args, fmtFlag("buildvcs", opts.BuildVCS))
}

if opts.Compiler != "" {
args = append(args, fmtFlag("-compiler", opts.Compiler))
}

if opts.Race {
args = append(args, "-race")
}

if opts.GcFlags != "" {
args = append(args, fmtFlagSpaceSeparated("gcflags", opts.GcFlags))
}

if opts.GcCGOFlags != "" {
args = append(args, fmtFlagSpaceSeparated("gccgoflags", opts.GcCGOFlags))
}

if opts.LDFlags != "" {
args = append(args, fmtFlagSpaceSeparated("ldflags", opts.LDFlags))
}

if opts.ASMFlags != "" {
args = append(args, fmtFlagSpaceSeparated("asmflags", opts.ASMFlags))
}

if opts.Tags != "" {
args = append(args, fmt.Sprintf("-tags=%s", opts.Tags))
}
args := append([]string{"build"}, opts.Arguments()...)

args = append(args, "-o", outputPathReal, opts.PackagePath)

_, err = execGo(opts.GoCmd, env, chdir, args...)
_, err = execGo(opts.GoCmd, opts.Env(), chdir, args...)

return err
}

func fmtFlag(name, value string) string {
return fmt.Sprintf(`-%s=%s`, name, value)
}

func fmtFlagSpaceSeparated(name, value string) string {
if strings.Contains(value, " ") && value[0] != '"' {
return fmt.Sprintf(`-%s="%s"`, name, value)
}

return fmt.Sprintf(`-%s=%s`, name, value)
}

// GoMainDirs returns the file paths to the packages that are "main"
// packages, from the list of packages given. The list of packages can
// include relative paths, the special "..." Go keyword, etc.
Expand Down Expand Up @@ -202,7 +124,7 @@ func GoMainDirs(packages []string, GoCmd string) ([]string, error) {

// GoRoot returns the GOROOT value for the compiled `go` binary.
func GoRoot() (string, error) {
output, err := execGo("go", nil, "", "env", "GOROOT")
output, err := execGo(gobin, nil, "", "env", "GOROOT")
if err != nil {
return "", err
}
Expand Down Expand Up @@ -231,7 +153,7 @@ func GoVersion() (string, error) {
}

// Execute and read the version, which will be the only thing on stdout.
version, err := execGo("go", nil, "", "run", sourcePath)
version, err := execGo(gobin, nil, "", "run", sourcePath)

fmt.Printf("Detected Go Version: %s\n", version)

Expand Down
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,11 @@ go 1.20
require (
github.com/hashicorp/go-version v1.6.0
github.com/mitchellh/iochan v1.0.0
github.com/stretchr/testify v1.8.2
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
17 changes: 17 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek=
github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/mitchellh/iochan v1.0.0 h1:C+X3KsSTLFVBr/tK1eYN/vs4rJcvsiLU338UhYPJWeY=
github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
19 changes: 3 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func realMain() int {
flags.StringVar(&flagOutput, "output", "{{.Dir}}_{{.OS}}_{{.Arch}}", "")
flags.IntVar(&flagParallel, "parallel", -1, "")
flags.BoolVar(&flagVerbose, "verbose", false, "")
flags.StringVar(&flagGoCmd, "gocmd", "go", "")
flags.StringVar(&flagGoCmd, "gocmd", gobin, "")

// Misc.
flags.Var(flagPlatform.ArchFlagValue(), "arch", "")
Expand Down Expand Up @@ -111,7 +111,7 @@ func realMain() int {
var v *version.Version

// Use latest if we get an unexpected version string
if strings.HasPrefix(versionStr, "go") {
if strings.HasPrefix(versionStr, gobin) {
if v, err = version.NewVersion(versionStr[2:]); err != nil {
log.Printf("Unable to parse current go version: %s\n%s", versionStr, err.Error())
}
Expand Down Expand Up @@ -154,19 +154,6 @@ func realMain() int {
return 1
}

// Assume -mod is supported when no version prefix is found
if flagMod != "" {
constraint, err := version.NewConstraint(">= 1.11")
if err != nil {
panic(err)
}

if !constraint.Check(v) {
fmt.Printf("Go compiler version %s does not support the -mod flag\n", versionStr)
flagMod = ""
}
}

// Build in parallel!
fmt.Printf("Number of parallel builds: %d\n\n", flagParallel)

Expand Down Expand Up @@ -211,7 +198,7 @@ func realMain() int {
InstallSuffix: flagInstallSuffix,
LDFlags: flagLDFlags,
LinkShared: flagLinkShared,
ModMode: flagMod,
Mod: flagMod,
ModCacheRW: flagModCacheRW,
ModFile: flagModFile,
Overlay: flagOverlay,
Expand Down
Loading

0 comments on commit 16cc89c

Please sign in to comment.