-
-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
122 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package xcaddy | ||
|
||
import ( | ||
"encoding/json" | ||
"os/exec" | ||
) | ||
|
||
// Compile contains parameters for compilation. | ||
type Compile struct { | ||
Platform | ||
Cgo bool `json:"cgo,omitempty"` | ||
} | ||
|
||
// Platform represents a build target. | ||
type Platform struct { | ||
OS string `json:"os,omitempty"` | ||
Arch string `json:"arch,omitempty"` | ||
ARM string `json:"arm,omitempty"` | ||
} | ||
|
||
// SupportedPlatforms runs `go tool dist list` to make | ||
// a list of possible build targets. | ||
func SupportedPlatforms() ([]Compile, error) { | ||
out, err := exec.Command("go", "tool", "dist", "list", "-json").Output() | ||
if err != nil { | ||
return nil, err | ||
} | ||
var dists []dist | ||
err = json.Unmarshal(out, &dists) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// translate from the go command's output structure | ||
// to our own user-facing structure | ||
var compiles []Compile | ||
for _, d := range dists { | ||
comp := d.toCompile() | ||
if d.GOARCH == "arm" { | ||
if d.GOOS == "linux" { | ||
// only linux supports ARMv5; see https://github.com/golang/go/issues/18418 | ||
comp.ARM = "5" | ||
compiles = append(compiles, comp) | ||
} | ||
comp.ARM = "6" | ||
compiles = append(compiles, comp) | ||
comp.ARM = "7" | ||
compiles = append(compiles, comp) | ||
} else { | ||
compiles = append(compiles, comp) | ||
} | ||
} | ||
|
||
return compiles, nil | ||
} | ||
|
||
// dist is the structure that fits the output | ||
// of the `go tool dist list -json` command. | ||
type dist struct { | ||
GOOS string `json:"GOOS"` | ||
GOARCH string `json:"GOARCH"` | ||
CgoSupported bool `json:"CgoSupported"` | ||
} | ||
|
||
func (d dist) toCompile() Compile { | ||
return Compile{ | ||
Platform: Platform{ | ||
OS: d.GOOS, | ||
Arch: d.GOARCH, | ||
}, | ||
Cgo: d.CgoSupported, | ||
} | ||
} |