Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

GOARM support #69

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
gox
gox*
8 changes: 7 additions & 1 deletion go.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type OutputTemplateData struct {
Dir string
OS string
Arch string
ARM string
}

type CompileOpts struct {
Expand Down Expand Up @@ -52,6 +53,10 @@ func GoCrossCompile(opts *CompileOpts) error {
env = append(env, "CGO_ENABLED=0")
}

if len(opts.Platform.ARM) > 0 {
env = append(env, "GOARM="+opts.Platform.ARM)
}

var outputPath bytes.Buffer
tpl, err := template.New("output").Parse(opts.OutputTpl)
if err != nil {
Expand All @@ -60,7 +65,8 @@ func GoCrossCompile(opts *CompileOpts) error {
tplData := OutputTemplateData{
Dir: filepath.Base(opts.PackagePath),
OS: opts.Platform.OS,
Arch: opts.Platform.Arch,
Arch: opts.Platform.GetArch(),
ARM: opts.Platform.GetARMVersion(),
}
if err := tpl.Execute(&outputPath, &tplData); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestGoVersion(t *testing.T) {

acceptable := []string{
"devel", "go1.0", "go1.1", "go1.2", "go1.3", "go1.4.2", "go1.5",
"go1.5.1",
"go1.5.1", "go1.6",
}
found := false
for _, expected := range acceptable {
Expand Down
8 changes: 8 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func realMain() int {
flags.Var(platformFlag.ArchFlagValue(), "arch", "arch to build for or skip")
flags.Var(platformFlag.OSArchFlagValue(), "osarch", "os/arch pairs to build for or skip")
flags.Var(platformFlag.OSFlagValue(), "os", "os to build for or skip")
flags.Var(platformFlag.ARMArchFlagValue(), "armarch", "os to build for or skip")
flags.StringVar(&ldflags, "ldflags", "", "linker flags")
flags.StringVar(&tags, "tags", "", "go build tags")
flags.StringVar(&outputTpl, "output", "{{.Dir}}_{{.OS}}_{{.Arch}}", "output path")
Expand Down Expand Up @@ -109,6 +110,12 @@ func realMain() int {
return 1
}

fmt.Println("Building for platforms:")
for _, platform := range platforms {
fmt.Printf("%s\n", platform.String())
}
fmt.Println("")

// Build in parallel!
fmt.Printf("Number of parallel builds: %d\n\n", parallel)
var errorLock sync.Mutex
Expand Down Expand Up @@ -184,6 +191,7 @@ Options:
-tags="" Additional '-tags' value to pass to go build
-os="" Space-separated list of operating systems to build for
-osarch="" Space-separated list of os/arch pairs to build for
-armarch="" Space-separated list of GOARM arch version to build for when arch is "arm"
-osarch-list List supported os/arch pairs for your Go version
-output="foo" Output path template. See below for more info
-parallel=-1 Amount of parallelism, defaults to number of CPUs
Expand Down
105 changes: 72 additions & 33 deletions platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,77 +16,114 @@ type Platform struct {
// is not a default because it is quite rare that you're cross-compiling
// something to Android AND something like Linux.
Default bool
ARM string
}

func PlatformFromString(os, arch string) Platform {
if strings.HasPrefix(arch, "armv") && len(arch) >= 5 {
return Platform{
OS: os,
Arch: "arm",
ARM: arch[4:],
}
}
return Platform{
OS: os,
Arch: arch,
}
}

func (p *Platform) String() string {
return fmt.Sprintf("%s/%s", p.OS, p.Arch)
return fmt.Sprintf("%s/%s", p.OS, p.GetArch())
}

func (p *Platform) GetArch() string {
return fmt.Sprintf("%s%s", p.Arch, p.GetARMVersion())
}

func (p *Platform) GetARMVersion() string {
if len(p.ARM) > 0 {
return "v" + p.ARM
}
return ""
}

var (
OsList = []string{
"darwin",
"dragonfly",
"freebsd",
"linux",
"android",
"solaris",
"freebsd",
"nacl",
"netbsd",
"openbsd",
"plan9",
"solaris",
"windows",
}

ArchList = []string{
"386",
"amd64",
"amd64p32",
"arm",
"arm64",
"mips64",
"mips64le",
"ppc64",
"ppc64le",
}

Platforms_1_0 = []Platform{
{"darwin", "386", true},
{"darwin", "amd64", true},
{"linux", "386", true},
{"linux", "amd64", true},
{"linux", "arm", true},
{"freebsd", "386", true},
{"freebsd", "amd64", true},
{"openbsd", "386", true},
{"openbsd", "amd64", true},
{"windows", "386", true},
{"windows", "amd64", true},
{OS: "darwin", Arch: "386", Default: true},
{OS: "darwin", Arch: "amd64", Default: true},
{OS: "linux", Arch: "386", Default: true},
{OS: "linux", Arch: "amd64", Default: true},
{OS: "linux", Arch: "arm", Default: true},
{OS: "freebsd", Arch: "386", Default: true},
{OS: "freebsd", Arch: "amd64", Default: true},
{OS: "openbsd", Arch: "386", Default: true},
{OS: "openbsd", Arch: "amd64", Default: true},
{OS: "windows", Arch: "386", Default: true},
{OS: "windows", Arch: "amd64", Default: true},
}

Platforms_1_1 = append(Platforms_1_0, []Platform{
{"freebsd", "arm", true},
{"netbsd", "386", true},
{"netbsd", "amd64", true},
{"netbsd", "arm", true},
{"plan9", "386", false},
{OS: "freebsd", Arch: "arm", Default: true},
{OS: "linux", Arch: "arm", Default: false, ARM: "5"},
{OS: "linux", Arch: "arm", Default: false, ARM: "6"},
{OS: "linux", Arch: "arm", Default: false, ARM: "7"},
{OS: "netbsd", Arch: "386", Default: true},
{OS: "netbsd", Arch: "amd64", Default: true},
{OS: "netbsd", Arch: "arm", Default: true},
{OS: "plan9", Arch: "386", Default: false},
}...)

Platforms_1_3 = append(Platforms_1_1, []Platform{
{"dragonfly", "386", false},
{"dragonfly", "amd64", false},
{"nacl", "amd64", false},
{"nacl", "amd64p32", false},
{"nacl", "arm", false},
{"solaris", "amd64", false},
{OS: "dragonfly", Arch: "386", Default: false},
{OS: "dragonfly", Arch: "amd64", Default: false},
{OS: "nacl", Arch: "amd64", Default: false},
{OS: "nacl", Arch: "amd64p32", Default: false},
{OS: "nacl", Arch: "arm", Default: false},
{OS: "solaris", Arch: "amd64", Default: false},
}...)

Platforms_1_4 = append(Platforms_1_3, []Platform{
{"android", "arm", false},
{"plan9", "amd64", false},
{OS: "android", Arch: "arm", Default: false},
{OS: "plan9", Arch: "amd64", Default: false},
}...)

Platforms_1_5 = append(Platforms_1_4, []Platform{
{"darwin", "arm", false},
{"darwin", "arm64", false},
{"linux", "arm64", false},
{"linux", "ppc64", false},
{"linux", "ppc64le", false},
{OS: "darwin", Arch: "arm", Default: false},
{OS: "darwin", Arch: "arm64", Default: false},
{OS: "linux", Arch: "arm64", Default: false},
{OS: "linux", Arch: "ppc64", Default: false},
{OS: "linux", Arch: "ppc64le", Default: false},
}...)

// Nothing changed from 1.5 to 1.6
Platforms_1_6 = Platforms_1_5
)

// SupportedPlatforms returns the full list of supported platforms for
Expand All @@ -102,8 +139,10 @@ func SupportedPlatforms(v string) []Platform {
return Platforms_1_4
} else if strings.HasPrefix(v, "go1.5") {
return Platforms_1_5
} else if strings.HasPrefix(v, "go1.6") {
return Platforms_1_6
}

// Assume latest
return Platforms_1_5
return Platforms_1_6
}
34 changes: 16 additions & 18 deletions platform_flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (
// PlatformFlag is a flag.Value (and flag.Getter) implementation that
// is used to track the os/arch flags on the command-line.
type PlatformFlag struct {
OS []string
Arch []string
OSArch []Platform
OS []string
Arch []string
OSArch []Platform
ARMArch []string
}

// Platforms returns the list of platforms that were set by this flag.
Expand Down Expand Up @@ -80,11 +81,7 @@ func (p *PlatformFlag) Platforms(supported []Platform) []Platform {
if _, ok := includeArch[arch]; !ok {
continue
}

prefilter = append(prefilter, Platform{
OS: os,
Arch: arch,
})
prefilter = append(prefilter, PlatformFromString(os, arch))
}
}
} else if len(includeOS) > 0 {
Expand All @@ -106,19 +103,14 @@ func (p *PlatformFlag) Platforms(supported []Platform) []Platform {
// Remove any that aren't supported
result := make([]Platform, 0, len(prefilter))
for _, pending := range prefilter {
found := false
for _, platform := range supported {
if pending.String() == platform.String() {
found = true
add := platform
add.Default = false
result = append(result, add)
break
}
}

if found {
add := pending
add.Default = false
result = append(result, add)
}
}

prefilter = result
Expand Down Expand Up @@ -155,7 +147,7 @@ func (p *PlatformFlag) Platforms(supported []Platform) []Platform {

if checkComponents {
if len(ignoreArch) > 0 {
if _, ok := ignoreArch[platform.Arch]; ok {
if _, ok := ignoreArch[platform.GetArch()]; ok {
continue
}
}
Expand All @@ -165,7 +157,7 @@ func (p *PlatformFlag) Platforms(supported []Platform) []Platform {
}
}
if len(includeArch) > 0 {
if _, ok := includeArch[platform.Arch]; !ok {
if _, ok := includeArch[platform.GetArch()]; !ok {
continue
}
}
Expand Down Expand Up @@ -200,6 +192,12 @@ func (p *PlatformFlag) OSArchFlagValue() flag.Value {
return (*appendPlatformValue)(&p.OSArch)
}

// ARMArchFlagValue returns a flag.Value that can be used with the flag
// package to collect the arm arches for the flag.
func (p *PlatformFlag) ARMArchFlagValue() flag.Value {
return (*appendStringValue)(&p.ARMArch)
}

// appendPlatformValue is a flag.Value that appends a full platform (os/arch)
// to a list where the values from space-separated lines. This is used to
// satisfy the -osarch flag.
Expand Down
Loading