Add support for output prefix and -v / -race flags

This commit is contained in:
Péter Szilágyi 2014-08-14 12:43:57 +03:00
parent 6d30460233
commit 5d94f51094
3 changed files with 89 additions and 53 deletions

View File

@ -72,7 +72,6 @@ to xgo and if the specific release was already integrated, it will automatically
be retrieved and installed.
$ xgo -go 1.3.0 github.com/project-iris/iris
...
Since xgo depends on not only the official releases, but also on Dave Cheney's
ARM packages, there will be a slight delay between official Go updates and the
@ -80,5 +79,29 @@ xgo updates.
Additionally, a few wildcard release strings are also supported:
- `-go latest` will use the latest Go release
- `-go 1.3.x` will use the latest point release of a specific Go version
- `latest` will use the latest Go release
- `1.3.x` will use the latest point release of a specific Go version
### Output prefixing
Xgo by default uses the name of the package being cross compiled as the output
file prefix. This can be overridden with the `-out` flag.
$ xgo -out iris-v0.3.0 github.com/project-iris/iris
...
$ ls -al
-rwxr-xr-x 1 root root 3090956 Aug 14 12:39 iris-v0.3.0-darwin-386
-rwxr-xr-x 1 root root 3941068 Aug 14 12:39 iris-v0.3.0-darwin-amd64
-rwxr-xr-x 1 root root 4185224 Aug 14 12:39 iris-v0.3.0-linux-386
-rwxr-xr-x 1 root root 5200960 Aug 14 12:39 iris-v0.3.0-linux-amd64
-rwxr-xr-x 1 root root 4155880 Aug 14 12:39 iris-v0.3.0-linux-arm
-rwxr-xr-x 1 root root 4230144 Aug 14 12:39 iris-v0.3.0-windows-386.exe
-rwxr-xr-x 1 root root 5245952 Aug 14 12:39 iris-v0.3.0-windows-amd64.exe
### Build flags
A handful of flags can be passed to `go build`. The currently supported ones are
- `-v`: prints the names of packages as they are compiled
- `-race`: enables data race detection (supported only on amd64, rest built without)

View File

@ -61,21 +61,20 @@ RUN \
echo '$FETCH $DIST_WIN_32 $DIST_WIN_32_SHA1' >> $BOOTSTRAP && \
echo >> $BOOTSTRAP && \
echo 'tar -C /usr/local -xzf `basename $DIST_LINUX_64`' >> $BOOTSTRAP && \
echo 'rm -rf /usr/local/go/pkg/linux_amd64_race' >> $BOOTSTRAP && \
echo >> $BOOTSTRAP && \
echo 'tar -C /usr/local --wildcards -xzf `basename $DIST_LINUX_32` go/pkg/linux_386' >> $BOOTSTRAP && \
echo 'tar -C /usr/local --wildcards -xzf `basename $DIST_LINUX_32` go/pkg/linux_386*' >> $BOOTSTRAP && \
echo 'GOOS=linux GOARCH=386 /usr/local/go/pkg/tool/linux_amd64/dist bootstrap' >> $BOOTSTRAP && \
echo 'tar -C /usr/local --wildcards -xzf `basename $DIST_LINUX_ARM` go/pkg/linux_arm' >> $BOOTSTRAP && \
echo 'tar -C /usr/local --wildcards -xzf `basename $DIST_LINUX_ARM` go/pkg/linux_arm*' >> $BOOTSTRAP && \
echo 'GOOS=linux GOARCH=arm /usr/local/go/pkg/tool/linux_amd64/dist bootstrap' >> $BOOTSTRAP && \
echo >> $BOOTSTRAP && \
echo 'tar -C /usr/local --wildcards -xzf `basename $DIST_OSX_64` go/pkg/darwin_amd64' >> $BOOTSTRAP && \
echo 'tar -C /usr/local --wildcards -xzf `basename $DIST_OSX_64` go/pkg/darwin_amd64*' >> $BOOTSTRAP && \
echo 'GOOS=darwin GOARCH=amd64 /usr/local/go/pkg/tool/linux_amd64/dist bootstrap' >> $BOOTSTRAP && \
echo 'tar -C /usr/local --wildcards -xzf `basename $DIST_OSX_32` go/pkg/darwin_386' >> $BOOTSTRAP && \
echo 'tar -C /usr/local --wildcards -xzf `basename $DIST_OSX_32` go/pkg/darwin_386*' >> $BOOTSTRAP && \
echo 'GOOS=darwin GOARCH=386 /usr/local/go/pkg/tool/linux_amd64/dist bootstrap' >> $BOOTSTRAP && \
echo >> $BOOTSTRAP && \
echo 'unzip -d /usr/local -q `basename $DIST_WIN_64` go/pkg/windows_amd64/*' >> $BOOTSTRAP && \
echo 'unzip -d /usr/local -q `basename $DIST_WIN_64` go/pkg/windows_amd64*' >> $BOOTSTRAP && \
echo 'GOOS=windows GOARCH=amd64 /usr/local/go/pkg/tool/linux_amd64/dist bootstrap' >> $BOOTSTRAP && \
echo 'unzip -d /usr/local -q `basename $DIST_WIN_32` go/pkg/windows_386/*' >> $BOOTSTRAP && \
echo 'unzip -d /usr/local -q `basename $DIST_WIN_32` go/pkg/windows_386*' >> $BOOTSTRAP && \
echo 'GOOS=windows GOARCH=386 /usr/local/go/pkg/tool/linux_amd64/dist bootstrap' >> $BOOTSTRAP && \
echo >> $BOOTSTRAP && \
echo 'rm -f `basename $DIST_LINUX_64` `basename $DIST_LINUX_32` `basename $DIST_LINUX_ARM` \\' >> $BOOTSTRAP && \
@ -95,34 +94,41 @@ RUN \
echo 'echo Fetching $1...' >> $BUILD && \
echo 'go get $1' >> $BUILD && \
echo 'cd $GOPATH/src/$1' >> $BUILD && \
echo 'pack=`basename $1`' >> $BUILD && \
echo >> $BUILD && \
echo 'NAME=`basename $1`' >> $BUILD && \
echo 'if [ "$OUT" != "" ]; then' >> $BUILD && \
echo ' NAME=$OUT' >> $BUILD && \
echo 'fi' >> $BUILD && \
echo >> $BUILD && \
echo 'if [ "$FLAG_V" == "true" ]; then V=-v; fi' >> $BUILD && \
echo 'if [ "$FLAG_RACE" == "true" ]; then R=-race; fi' >> $BUILD && \
echo >> $BUILD && \
echo 'echo Compiling for linux/amd64...' >> $BUILD && \
echo 'GOOS=linux GOARCH=amd64 CGO_ENABLED=1 go build -o $pack-linux-amd64' >> $BUILD && \
echo 'GOOS=linux GOARCH=amd64 CGO_ENABLED=1 go build $V $R -o $NAME-linux-amd64$R' >> $BUILD && \
echo >> $BUILD && \
echo 'echo Compiling for linux/386...' >> $BUILD && \
echo 'GOOS=linux GOARCH=386 CGO_ENABLED=1 go build -o $pack-linux-386' >> $BUILD && \
echo 'GOOS=linux GOARCH=386 CGO_ENABLED=1 go build $V -o $NAME-linux-386' >> $BUILD && \
echo >> $BUILD && \
echo 'echo Compiling for linux/arm...' >> $BUILD && \
echo 'CC=arm-linux-gnueabi-gcc \\' >> $BUILD && \
echo ' GOOS=linux GOARCH=arm CGO_ENABLED=1 go build -o $pack-linux-arm' >> $BUILD && \
echo ' GOOS=linux GOARCH=arm CGO_ENABLED=1 go build $V -o $NAME-linux-arm' >> $BUILD && \
echo >> $BUILD && \
echo 'echo Compiling for windows/amd64...' >> $BUILD && \
echo 'CC=x86_64-w64-mingw32-gcc \\' >> $BUILD && \
echo ' GOOS=windows GOARCH=amd64 CGO_ENABLED=1 go build -o $pack-windows-amd64.exe' >> $BUILD && \
echo ' GOOS=windows GOARCH=amd64 CGO_ENABLED=1 go build $V $R -o $NAME-windows-amd64$R.exe' >> $BUILD && \
echo >> $BUILD && \
echo 'echo Compiling for windows/386...' >> $BUILD && \
echo 'CC=i686-w64-mingw32-gcc \\' >> $BUILD && \
echo ' GOOS=windows GOARCH=386 CGO_ENABLED=1 go build -o $pack-windows-386.exe' >> $BUILD && \
echo ' GOOS=windows GOARCH=386 CGO_ENABLED=1 go build $V -o $NAME-windows-386.exe' >> $BUILD && \
echo >> $BUILD && \
echo 'echo Compiling for darwin/amd64...' >> $BUILD && \
echo '`/osxcross/target/bin/osxcross-env`' >> $BUILD && \
echo 'CC=o64-clang \\' >> $BUILD && \
echo ' GOOS=darwin GOARCH=amd64 CGO_ENABLED=1 go build -o $pack-darwin-amd64' >> $BUILD && \
echo ' GOOS=darwin GOARCH=amd64 CGO_ENABLED=1 go build $V $R -o $NAME-darwin-amd64$R' >> $BUILD && \
echo >> $BUILD && \
echo 'echo Compiling for darwin/386...' >> $BUILD && \
echo 'CC=o32-clang \\' >> $BUILD && \
echo ' GOOS=darwin GOARCH=386 CGO_ENABLED=1 go build -o $pack-darwin-386' >> $BUILD && \
echo ' GOOS=darwin GOARCH=386 CGO_ENABLED=1 go build $V -o $NAME-darwin-386' >> $BUILD && \
echo >> $BUILD && \
echo 'echo Moving binaries to host...' >> $BUILD && \
echo 'cp `ls -t | head -n 7` /build' >> $BUILD && \

19
xgo.go
View File

@ -22,6 +22,11 @@ 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")
var outPrefix = flag.String("out", "", "Prefix to use for output naming (empty = package name)")
// Command line arguments to pass to go build
var buildVerbose = flag.Bool("v", false, "Print the names of packages as they are compiled")
var buildRace = flag.Bool("race", false, "Enable data race detection (supported only on amd64)")
func main() {
flag.Parse()
@ -48,7 +53,7 @@ func main() {
fmt.Println("found.")
}
// Cross compile the requested package into the local folder
if err := compile(flag.Args()[0]); err != nil {
if err := compile(flag.Args()[0], *outPrefix, *buildVerbose, *buildRace); err != nil {
log.Fatalf("Failed to cross compile package: %v.", err)
}
}
@ -81,16 +86,18 @@ func pullDockerImage(image string) error {
}
// Cross compiles a requested package into the current working directory.
func compile(path string) error {
func compile(path string, prefix string, verbose bool, race bool) error {
folder, err := os.Getwd()
if err != nil {
log.Fatalf("Failed to retrieve the working directory: %v.", err)
}
fmt.Printf("Cross compiling %s...\n", path)
if err := run(exec.Command("docker", "run", "-v", folder+":/build", dockerDist+*goVersion, path)); err != nil {
return err
}
return nil
return run(exec.Command("docker", "run",
"-v", folder+":/build",
"-e", "OUT="+prefix,
"-e", fmt.Sprintf("FLAG_V=%v", verbose),
"-e", fmt.Sprintf("FLAG_RACE=%v", race),
dockerDist+*goVersion, path))
}
// Executes a command synchronously, redirecting its output to stdout.