2017-02-17 22:54:32 +01:00
|
|
|
// +build ignore
|
|
|
|
|
|
|
|
// Cross compile rclone - in go because I hate bash ;-)
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2017-05-13 10:55:29 +02:00
|
|
|
"regexp"
|
2017-02-17 22:54:32 +01:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// Flags
|
|
|
|
debug = flag.Bool("d", false, "Print commands instead of running them.")
|
|
|
|
parallel = flag.Int("parallel", runtime.NumCPU(), "Number of commands to run in parallel.")
|
2017-02-20 17:54:33 +01:00
|
|
|
copyAs = flag.String("release", "", "Make copies of the releases with this name")
|
2017-02-20 18:08:07 +01:00
|
|
|
gitLog = flag.String("git-log", "", "git log to include as well")
|
2017-05-13 10:55:29 +02:00
|
|
|
include = flag.String("include", "^.*$", "os/arch regexp to include")
|
|
|
|
exclude = flag.String("exclude", "^$", "os/arch regexp to exclude")
|
|
|
|
cgo = flag.Bool("cgo", false, "Use cgo for the build")
|
2017-02-17 22:54:32 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// GOOS/GOARCH pairs we build for
|
|
|
|
var osarches = []string{
|
|
|
|
"windows/386",
|
|
|
|
"windows/amd64",
|
|
|
|
"darwin/386",
|
|
|
|
"darwin/amd64",
|
|
|
|
"linux/386",
|
|
|
|
"linux/amd64",
|
|
|
|
"linux/arm",
|
|
|
|
"linux/arm64",
|
|
|
|
"linux/mips",
|
|
|
|
"linux/mipsle",
|
|
|
|
"freebsd/386",
|
|
|
|
"freebsd/amd64",
|
|
|
|
"freebsd/arm",
|
|
|
|
"netbsd/386",
|
|
|
|
"netbsd/amd64",
|
|
|
|
"netbsd/arm",
|
|
|
|
"openbsd/386",
|
|
|
|
"openbsd/amd64",
|
|
|
|
"plan9/386",
|
|
|
|
"plan9/amd64",
|
|
|
|
"solaris/amd64",
|
|
|
|
}
|
|
|
|
|
2017-05-09 12:58:29 +02:00
|
|
|
// Special environment flags for a given arch
|
|
|
|
var archFlags = map[string][]string{
|
|
|
|
"386": {"GO386=387"},
|
|
|
|
}
|
|
|
|
|
2017-02-17 22:54:32 +01:00
|
|
|
// runEnv - run a shell command with env
|
|
|
|
func runEnv(args, env []string) {
|
|
|
|
if *debug {
|
|
|
|
args = append([]string{"echo"}, args...)
|
|
|
|
}
|
|
|
|
cmd := exec.Command(args[0], args[1:]...)
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
if env != nil {
|
|
|
|
cmd.Env = append(os.Environ(), env...)
|
|
|
|
}
|
2017-05-09 12:58:29 +02:00
|
|
|
if *debug {
|
|
|
|
log.Printf("args = %v, env = %v\n", args, cmd.Env)
|
|
|
|
}
|
2017-02-17 22:54:32 +01:00
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to run %v: %v", args, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// run a shell command
|
|
|
|
func run(args ...string) {
|
|
|
|
runEnv(args, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// build the binary in dir
|
|
|
|
func compileArch(version, goos, goarch, dir string) {
|
|
|
|
log.Printf("Compiling %s/%s", goos, goarch)
|
|
|
|
output := filepath.Join(dir, "rclone")
|
2017-02-20 17:36:54 +01:00
|
|
|
if goos == "windows" {
|
|
|
|
output += ".exe"
|
|
|
|
}
|
2017-02-17 22:54:32 +01:00
|
|
|
err := os.MkdirAll(dir, 0777)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to mkdir: %v", err)
|
|
|
|
}
|
|
|
|
args := []string{
|
|
|
|
"go", "build",
|
|
|
|
"--ldflags", "-s -X github.com/ncw/rclone/fs.Version=" + version,
|
|
|
|
"-i",
|
|
|
|
"-o", output,
|
|
|
|
"..",
|
|
|
|
}
|
|
|
|
env := []string{
|
|
|
|
"GOOS=" + goos,
|
|
|
|
"GOARCH=" + goarch,
|
2017-05-13 10:55:29 +02:00
|
|
|
}
|
|
|
|
if !*cgo {
|
|
|
|
env = append(env, "CGO_ENABLED=0")
|
|
|
|
} else {
|
|
|
|
env = append(env, "CGO_ENABLED=1")
|
2017-02-17 22:54:32 +01:00
|
|
|
}
|
2017-05-09 12:58:29 +02:00
|
|
|
if flags, ok := archFlags[goarch]; ok {
|
|
|
|
env = append(env, flags...)
|
|
|
|
}
|
2017-02-17 22:54:32 +01:00
|
|
|
runEnv(args, env)
|
|
|
|
// Now build the zip
|
|
|
|
run("cp", "-a", "../MANUAL.txt", filepath.Join(dir, "README.txt"))
|
|
|
|
run("cp", "-a", "../MANUAL.html", filepath.Join(dir, "README.html"))
|
|
|
|
run("cp", "-a", "../rclone.1", dir)
|
2017-02-20 18:08:07 +01:00
|
|
|
if *gitLog != "" {
|
|
|
|
run("cp", "-a", *gitLog, dir)
|
|
|
|
}
|
2017-02-17 22:54:32 +01:00
|
|
|
zip := dir + ".zip"
|
|
|
|
run("zip", "-r9", zip, dir)
|
2017-02-20 17:54:33 +01:00
|
|
|
if *copyAs != "" {
|
|
|
|
copyAsZip := strings.Replace(zip, "-"+version, "-"+*copyAs, 1)
|
|
|
|
run("ln", zip, copyAsZip)
|
|
|
|
}
|
2017-02-17 22:54:32 +01:00
|
|
|
run("rm", "-rf", dir)
|
|
|
|
log.Printf("Done compiling %s/%s", goos, goarch)
|
|
|
|
}
|
|
|
|
|
|
|
|
func compile(version string) {
|
|
|
|
start := time.Now()
|
|
|
|
wg := new(sync.WaitGroup)
|
|
|
|
run := make(chan func(), *parallel)
|
|
|
|
for i := 0; i < *parallel; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
for f := range run {
|
|
|
|
f()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2017-05-13 10:55:29 +02:00
|
|
|
includeRe, err := regexp.Compile(*include)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Bad -include regexp: %v", err)
|
|
|
|
}
|
|
|
|
excludeRe, err := regexp.Compile(*exclude)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Bad -exclude regexp: %v", err)
|
|
|
|
}
|
|
|
|
compiled := 0
|
2017-02-17 22:54:32 +01:00
|
|
|
for _, osarch := range osarches {
|
2017-05-13 10:55:29 +02:00
|
|
|
if excludeRe.MatchString(osarch) || !includeRe.MatchString(osarch) {
|
|
|
|
continue
|
|
|
|
}
|
2017-02-17 22:54:32 +01:00
|
|
|
parts := strings.Split(osarch, "/")
|
|
|
|
if len(parts) != 2 {
|
|
|
|
log.Fatalf("Bad osarch %q", osarch)
|
|
|
|
}
|
|
|
|
goos, goarch := parts[0], parts[1]
|
|
|
|
userGoos := goos
|
|
|
|
if goos == "darwin" {
|
|
|
|
userGoos = "osx"
|
|
|
|
}
|
|
|
|
dir := filepath.Join("rclone-" + version + "-" + userGoos + "-" + goarch)
|
|
|
|
run <- func() {
|
|
|
|
compileArch(version, goos, goarch, dir)
|
|
|
|
}
|
2017-05-13 10:55:29 +02:00
|
|
|
compiled++
|
2017-02-17 22:54:32 +01:00
|
|
|
}
|
|
|
|
close(run)
|
|
|
|
wg.Wait()
|
2017-05-13 10:55:29 +02:00
|
|
|
log.Printf("Compiled %d arches in %v", compiled, time.Since(start))
|
2017-02-17 22:54:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
args := flag.Args()
|
|
|
|
if len(args) != 1 {
|
|
|
|
log.Fatalf("Syntax: %s <version>", os.Args[0])
|
|
|
|
}
|
|
|
|
version := args[0]
|
|
|
|
run("rm", "-rf", "build")
|
|
|
|
run("mkdir", "build")
|
|
|
|
err := os.Chdir("build")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Couldn't cd into build dir: %v", err)
|
|
|
|
}
|
|
|
|
compile(version)
|
|
|
|
}
|