From 88eb66f0c481c4cfac0a71e1c0d9e14b4a408abf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Szil=C3=A1gyi?= Date: Thu, 7 Aug 2014 01:31:22 +0300 Subject: [PATCH] Add a simple wrapper around the docker command. --- xgo.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 xgo.go diff --git a/xgo.go b/xgo.go new file mode 100644 index 0000000..0d97f4b --- /dev/null +++ b/xgo.go @@ -0,0 +1,55 @@ +// 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 ( + "fmt" + "io" + "log" + "os" + "os/exec" +) + +// Docker container configured for cross compilation. +var container = "karalabe/xgo" + +func main() { + // Make sure docker is actually available on the system + fmt.Println("Checking docker installation...") + if err := run(exec.Command("docker", "version")); err != nil { + log.Fatalf("Failed to check docker installation: %v.", err) + } + // Fetch and configure the compilation settings + if len(os.Args) != 2 { + log.Fatalf("Usage: %s ", os.Args[0]) + } + path := os.Args[1] + pwd, err := os.Getwd() + if err != nil { + log.Fatalf("Failed to retrieve the working directory: %v.", err) + } + // Cross compile the requested package into the local folder + fmt.Println("Cross compiling", path) + if err := run(exec.Command("docker", "run", "-v", pwd+":/build", container, path)); err != nil { + log.Fatalf("Failed to cross compile package: %v.", err) + } +} + +// Executes a command synchronously, redirecting its output to stdout. +func run(cmd *exec.Cmd) error { + if out, err := cmd.StdoutPipe(); err != nil { + return err + } else { + go io.Copy(os.Stdout, out) + } + if out, err := cmd.StderrPipe(); err != nil { + return err + } else { + go io.Copy(os.Stderr, out) + } + return cmd.Run() +}