about summary refs log tree commit diff
path: root/main.go
blob: 428946f5e8fd1a96b8d07c3c2262f84647d87f1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package main

import (
	"bytes"
	"context"
	"flag"
	"fmt"
	"log"
	"os"
	"os/exec"
	"path/filepath"
	"runtime"

	"golang.org/x/sync/semaphore"
)

type Arch string

const (
	ARCH_AMD64    Arch = "amd64"
	ARCH_386      Arch = "386"
	ARCH_ARM      Arch = "arm"
	ARCH_ARM64    Arch = "arm64"
	ARCH_PPC64    Arch = "ppc64"
	ARCH_PPC64LE  Arch = "ppc64le"
	ARCH_MIPS     Arch = "mips"
	ARCH_MIPSLE   Arch = "mipsle"
	ARCH_MIPS64   Arch = "mips64"
	ARCH_MIPS64LE Arch = "mips64le"
)

type OS string

const (
	OS_ANDROID   OS = "android"
	OS_DARWIN    OS = "darwin"
	OS_DRAGONFLY OS = "dragonfly"
	OS_FREEBSD   OS = "freebsd"
	OS_LINUX     OS = "linux"
	OS_NETBSD    OS = "netbsd"
	OS_OPENBSD   OS = "openbsd"
	OS_PLAN9     OS = "plan9"
	OS_SOLARIS   OS = "solaris"
	OS_WINDOWS   OS = "windows"
)

type Platform struct {
	OS   OS
	Arch Arch
}

type kvs map[string]string

func (o kvs) Strings() []string {
	str := []string{}
	for k, v := range o {
		str = append(str, k+"="+v)
	}

	return str
}

var cgo bool
var ldflags string

func main() {
	// TODO: enable ARM after supporting GOARM
	// TODO: probably should make this configurableā€¦
	platforms := []Platform{
		//{OS_ANDROID, ARCH_ARM},
		{OS_DARWIN, ARCH_386},
		{OS_DARWIN, ARCH_AMD64},
		//{OS_DARWIN, ARCH_ARM},
		//{OS_DARWIN, ARCH_ARM64},
		{OS_DRAGONFLY, ARCH_AMD64},
		{OS_FREEBSD, ARCH_386},
		{OS_FREEBSD, ARCH_AMD64},
		//{OS_FREEBSD, ARCH_ARM},
		{OS_LINUX, ARCH_386},
		{OS_LINUX, ARCH_AMD64},
		//{OS_LINUX, ARCH_ARM},
		//{OS_LINUX, ARCH_ARM64},
		{OS_LINUX, ARCH_PPC64},
		{OS_LINUX, ARCH_PPC64LE},
		{OS_LINUX, ARCH_MIPS},
		{OS_LINUX, ARCH_MIPSLE},
		{OS_LINUX, ARCH_MIPS64},
		{OS_LINUX, ARCH_MIPS64LE},
		{OS_NETBSD, ARCH_386},
		{OS_NETBSD, ARCH_AMD64},
		//{OS_NETBSD, ARCH_ARM},
		{OS_OPENBSD, ARCH_386},
		{OS_OPENBSD, ARCH_AMD64},
		//{OS_OPENBSD, ARCH_ARM},
		{OS_PLAN9, ARCH_386},
		{OS_PLAN9, ARCH_AMD64},
		{OS_SOLARIS, ARCH_AMD64},
		{OS_WINDOWS, ARCH_386},
		{OS_WINDOWS, ARCH_AMD64},
	}

	flags := flag.NewFlagSet("bakelite", flag.ExitOnError)
	flags.BoolVar(&cgo, "cgo", false, "enables cgo (may require your own toolchain).")
	flags.StringVar(&ldflags, "ldflags", "", "arguments to pass on each go tool compile invocation.")
	flags.Usage = func() {
		fmt.Println("usage: bakelite [build flags] [packages]")
		fmt.Println(`
Bakelite compiles the packages named by the import paths for multiple GOOS and
GOARCH combinations. It does not install their results.

When compiling a package, Bakelite writes the result to output files named
after the source directory in the form "$package_$goos_$goarch".

Multiple packages may be given to Bakelite, the result of each are saved as
described in the preceding paragraph.

The build flags recognized by Bakelite:

	-ldflags 'flag list'
		arguments to pass on each go tool compile invocation.

The Bakelite specific flags:

	-cgo
		passes CGO_ENABLED=1 to the build environment.
		May require a build toolchain for each GOOS and GOARCH
		combination.

For more about specifying packages, see 'go help packages'.
For more about calling between Go and C/C++, run 'go help c'.

See also: go build, go install, go clean.
			`)
	}
	if err := flags.Parse(os.Args[1:]); err != nil {
		flags.Usage()
		os.Exit(-1)
	}

	packages := flags.Args()
	if len(packages) == 0 {
		fmt.Println("fatal: Expected at least one package.")
		os.Exit(-1)
	}

	var (
		parallelJobs = runtime.NumCPU()
		sem          = semaphore.NewWeighted(int64(parallelJobs))
		ctx          = context.Background()
	)

	fmt.Printf("info: running bakelite with %d jobs\n", parallelJobs)

	for _, platform := range platforms {
		for _, pkg := range packages {
			if err := sem.Acquire(ctx, 1); err != nil {
				log.Printf("failed to acquire semaphore: %s", err)
				break
			}

			go func(platform Platform, pkg string) {
				defer sem.Release(1)
				build(ctx, platform, pkg)
			}(platform, pkg)
		}
	}

	if err := sem.Acquire(ctx, int64(parallelJobs)); err != nil {
		log.Printf("failed to acquire semaphore: %s", err)
	}
}

func build(ctx context.Context, platform Platform, pkg string) error {
	name := fmt.Sprintf("%s-%s-%s", filepath.Base(pkg), platform.OS, platform.Arch)

	env := kvs{
		"GOOS":   string(platform.OS),
		"GOARCH": string(platform.Arch),
		"GOROOT": os.Getenv("GOROOT"),
		"GOPATH": os.Getenv("GOPATH"),
	}

	if cgo {
		env["CGO_ENABLED"] = "1"
	} else {
		env["CGO_ENABLED"] = "0"
	}

	var stdout bytes.Buffer
	var stderr bytes.Buffer

	args := []string{
		"build",
		"-o",
		name,
	}

	if ldflags != "" {
		args = append(args, "-ldflags", ldflags)
	}

	args = append(args, pkg)

	cmd := exec.CommandContext(context.Background(), "go", args...)
	cmd.Env = env.Strings()
	cmd.Stdout = &stdout
	cmd.Stderr = &stderr

	fmt.Printf("info: Running build for %s @ %s/%sā€¦\n", pkg, platform.OS, platform.Arch)
	err := cmd.Run()

	if err != nil {
		log.Printf("fatal: There was an error! goos='%s' goarch='%s' err='%s' stdout='%s' stderr='%s'", platform.OS, platform.Arch, err, stdout.String(), stderr.String())
	}

	return err
}