2014-08-06 22:31:22 +00:00
|
|
|
// Go CGO cross compiler
|
|
|
|
// Copyright (c) 2014 Péter Szilágyi. All rights reserved.
|
|
|
|
//
|
|
|
|
// Released under the MIT license.
|
|
|
|
|
|
|
|
// Wrapper around the GCO cross compiler docker container.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-08-13 12:09:53 +00:00
|
|
|
"bytes"
|
|
|
|
"flag"
|
2014-08-06 22:31:22 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2015-09-14 15:02:27 +00:00
|
|
|
"strings"
|
2014-08-06 22:31:22 +00:00
|
|
|
)
|
|
|
|
|
2014-08-13 12:09:53 +00:00
|
|
|
// Cross compilation docker containers
|
|
|
|
var dockerBase = "karalabe/xgo-base"
|
|
|
|
var dockerDist = "karalabe/xgo-"
|
|
|
|
|
|
|
|
// Command line arguments to fine tune the compilation
|
|
|
|
var goVersion = flag.String("go", "latest", "Go release to use for cross compilation")
|
2015-05-03 14:56:31 +00:00
|
|
|
var inPackage = flag.String("pkg", "", "Sub-package to build if not root import")
|
2014-08-14 09:43:57 +00:00
|
|
|
var outPrefix = flag.String("out", "", "Prefix to use for output naming (empty = package name)")
|
2015-05-05 11:57:11 +00:00
|
|
|
var srcRemote = flag.String("remote", "", "Version control remote repository to build")
|
2015-05-03 10:11:00 +00:00
|
|
|
var srcBranch = flag.String("branch", "", "Version control branch to build")
|
2015-05-03 13:38:21 +00:00
|
|
|
var crossDeps = flag.String("deps", "", "CGO dependencies (configure/make based archives)")
|
2015-09-14 15:02:27 +00:00
|
|
|
var targets = flag.String("targets", "*/*", "Comma separated targets to build for")
|
2014-08-14 09:43:57 +00:00
|
|
|
|
|
|
|
// Command line arguments to pass to go build
|
|
|
|
var buildVerbose = flag.Bool("v", false, "Print the names of packages as they are compiled")
|
2015-09-14 15:02:27 +00:00
|
|
|
var buildSteps = flag.Bool("x", false, "Print the command as executing the builds")
|
2014-08-14 09:43:57 +00:00
|
|
|
var buildRace = flag.Bool("race", false, "Enable data race detection (supported only on amd64)")
|
2014-08-06 22:31:22 +00:00
|
|
|
|
|
|
|
func main() {
|
2014-08-13 12:09:53 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
// Ensure docker is available
|
|
|
|
if err := checkDocker(); err != nil {
|
|
|
|
log.Fatalf("Failed to check docker installation: %v.", err)
|
|
|
|
}
|
|
|
|
// Validate the command line arguments
|
|
|
|
if len(flag.Args()) != 1 {
|
|
|
|
log.Fatalf("Usage: %s [options] <go import path>", os.Args[0])
|
|
|
|
}
|
|
|
|
// Check that all required images are available
|
|
|
|
found, err := checkDockerImage(dockerDist + *goVersion)
|
|
|
|
switch {
|
|
|
|
case err != nil:
|
|
|
|
log.Fatalf("Failed to check docker image availability: %v.", err)
|
|
|
|
case !found:
|
|
|
|
fmt.Println("not found!")
|
|
|
|
if err := pullDockerImage(dockerDist + *goVersion); err != nil {
|
|
|
|
log.Fatalf("Failed to pull docker image from the registry: %v.", err)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
fmt.Println("found.")
|
|
|
|
}
|
|
|
|
// Cross compile the requested package into the local folder
|
2015-09-14 15:02:27 +00:00
|
|
|
if err := compile(flag.Args()[0], *srcRemote, *srcBranch, *inPackage, *crossDeps, *outPrefix, *buildVerbose, *buildSteps, *buildRace, strings.Split(*targets, ",")); err != nil {
|
2014-08-13 12:09:53 +00:00
|
|
|
log.Fatalf("Failed to cross compile package: %v.", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks whether a docker installation can be found and is functional.
|
|
|
|
func checkDocker() error {
|
2014-08-06 22:31:22 +00:00
|
|
|
fmt.Println("Checking docker installation...")
|
|
|
|
if err := run(exec.Command("docker", "version")); err != nil {
|
2014-08-13 12:09:53 +00:00
|
|
|
return err
|
2014-08-06 22:31:22 +00:00
|
|
|
}
|
2014-08-07 07:05:19 +00:00
|
|
|
fmt.Println()
|
2014-08-13 12:09:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
2014-08-07 07:05:19 +00:00
|
|
|
|
2014-08-13 12:09:53 +00:00
|
|
|
// Checks whether a required docker image is available locally.
|
|
|
|
func checkDockerImage(image string) (bool, error) {
|
|
|
|
fmt.Printf("Checking for required docker image %s... ", image)
|
|
|
|
out, err := exec.Command("docker", "images", "--no-trunc").Output()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
2014-08-06 22:31:22 +00:00
|
|
|
}
|
2014-08-13 12:09:53 +00:00
|
|
|
return bytes.Contains(out, []byte(image)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pulls an image from the docker registry.
|
|
|
|
func pullDockerImage(image string) error {
|
|
|
|
fmt.Printf("Pulling %s from docker registry...\n", image)
|
|
|
|
return run(exec.Command("docker", "pull", image))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cross compiles a requested package into the current working directory.
|
2015-09-14 15:02:27 +00:00
|
|
|
func compile(repo string, remote string, branch string, pack string, deps string, prefix string, verbose bool, steps bool, race bool, targets []string) error {
|
2014-08-13 12:09:53 +00:00
|
|
|
folder, err := os.Getwd()
|
2014-08-06 22:31:22 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to retrieve the working directory: %v.", err)
|
|
|
|
}
|
2015-05-03 10:11:00 +00:00
|
|
|
fmt.Printf("Cross compiling %s...\n", repo)
|
2014-08-14 09:43:57 +00:00
|
|
|
return run(exec.Command("docker", "run",
|
|
|
|
"-v", folder+":/build",
|
2015-05-05 11:57:11 +00:00
|
|
|
"-e", "REPO_REMOTE="+remote,
|
|
|
|
"-e", "REPO_BRANCH="+branch,
|
2015-05-03 10:11:00 +00:00
|
|
|
"-e", "PACK="+pack,
|
2015-05-03 13:38:21 +00:00
|
|
|
"-e", "DEPS="+deps,
|
2014-08-14 09:43:57 +00:00
|
|
|
"-e", "OUT="+prefix,
|
|
|
|
"-e", fmt.Sprintf("FLAG_V=%v", verbose),
|
2015-09-14 15:02:27 +00:00
|
|
|
"-e", fmt.Sprintf("FLAG_X=%v", steps),
|
2014-08-14 09:43:57 +00:00
|
|
|
"-e", fmt.Sprintf("FLAG_RACE=%v", race),
|
2015-09-14 15:02:27 +00:00
|
|
|
"-e", "TARGETS="+strings.Replace(strings.Join(targets, " "), "*", ".", -1),
|
2015-05-03 10:11:00 +00:00
|
|
|
dockerDist+*goVersion, repo))
|
2014-08-06 22:31:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Executes a command synchronously, redirecting its output to stdout.
|
|
|
|
func run(cmd *exec.Cmd) error {
|
2014-10-22 10:33:00 +00:00
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
2014-08-06 22:31:22 +00:00
|
|
|
return cmd.Run()
|
|
|
|
}
|