Support branch selection and building subpackages.

This commit is contained in:
Péter Szilágyi 2015-05-03 13:11:00 +03:00
parent ccc86dbdce
commit 6bf2b5727f
2 changed files with 62 additions and 50 deletions

View File

@ -88,52 +88,60 @@ ENV GOPATH /go
# Create a small script to go get a package and cross compile it
ENV BUILD ./build.sh
RUN \
echo '#!/bin/bash' > $BUILD && \
echo 'set -e' >> $BUILD && \
echo >> $BUILD && \
echo 'echo Fetching $1...' >> $BUILD && \
echo 'go get $1' >> $BUILD && \
echo 'cd $GOPATH/src/$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 get' >> $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 get' >> $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 GOOS=linux GOARCH=arm CGO_ENABLED=1 GOARM=5 go get' >> $BUILD && \
echo 'CC=arm-linux-gnueabi-gcc GOOS=linux GOARCH=arm CGO_ENABLED=1 GOARM=5 go build $V -o $NAME-linux-arm' >> $BUILD && \
echo >> $BUILD && \
echo 'echo Compiling for windows/amd64...' >> $BUILD && \
echo 'CC=x86_64-w64-mingw32-gcc GOOS=windows GOARCH=amd64 CGO_ENABLED=1 go get' >> $BUILD && \
echo 'CC=x86_64-w64-mingw32-gcc 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 GOOS=windows GOARCH=386 CGO_ENABLED=1 go get' >> $BUILD && \
echo 'CC=i686-w64-mingw32-gcc 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 GOOS=darwin GOARCH=amd64 CGO_ENABLED=1 go get' >> $BUILD && \
echo 'CC=o64-clang 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 GOOS=darwin GOARCH=386 CGO_ENABLED=1 go get' >> $BUILD && \
echo 'CC=o32-clang 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 && \
echo '#!/bin/bash' > $BUILD && \
echo >> $BUILD && \
echo 'echo Fetching $1...' >> $BUILD && \
echo 'go get -d $1' >> $BUILD && \
echo 'cd $GOPATH/src/$1' >> $BUILD && \
echo 'export GOPATH=$GOPATH:`pwd`/Godeps/_workspace' >> $BUILD && \
echo >> $BUILD && \
echo 'if [ "$BRANCH" != "" ]; then' >> $BUILD && \
echo ' if [ -d ".git" ]; then' >> $BUILD && \
echo ' git checkout $BRANCH' >> $BUILD && \
echo ' elif [ -d ".hg" ]; then' >> $BUILD && \
echo ' hg checkout $BRANCH' >> $BUILD && \
echo ' fi' >> $BUILD && \
echo 'fi' >> $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 get -d ./$PACK' >> $BUILD && \
echo 'GOOS=linux GOARCH=amd64 CGO_ENABLED=1 go build $V $R -o $NAME-linux-amd64$R ./$PACK' >> $BUILD && \
echo >> $BUILD && \
echo 'echo Compiling for linux/386...' >> $BUILD && \
echo 'GOOS=linux GOARCH=386 CGO_ENABLED=1 go get -d ./$PACK' >> $BUILD && \
echo 'GOOS=linux GOARCH=386 CGO_ENABLED=1 go build $V -o $NAME-linux-386 ./$PACK' >> $BUILD && \
echo >> $BUILD && \
echo 'echo Compiling for linux/arm...' >> $BUILD && \
echo 'CC=arm-linux-gnueabi-gcc GOOS=linux GOARCH=arm CGO_ENABLED=1 GOARM=5 go get -d ./$PACK' >> $BUILD && \
echo 'CC=arm-linux-gnueabi-gcc GOOS=linux GOARCH=arm CGO_ENABLED=1 GOARM=5 go build $V -o $NAME-linux-arm ./$PACK' >> $BUILD && \
echo >> $BUILD && \
echo 'echo Compiling for windows/amd64...' >> $BUILD && \
echo 'CC=x86_64-w64-mingw32-gcc GOOS=windows GOARCH=amd64 CGO_ENABLED=1 go get -d ./$PACK' >> $BUILD && \
echo 'CC=x86_64-w64-mingw32-gcc GOOS=windows GOARCH=amd64 CGO_ENABLED=1 go build $V $R -o $NAME-windows-amd64$R.exe ./$PACK' >> $BUILD && \
echo >> $BUILD && \
echo 'echo Compiling for windows/386...' >> $BUILD && \
echo 'CC=i686-w64-mingw32-gcc GOOS=windows GOARCH=386 CGO_ENABLED=1 go get -d ./$PACK' >> $BUILD && \
echo 'CC=i686-w64-mingw32-gcc GOOS=windows GOARCH=386 CGO_ENABLED=1 go build $V -o $NAME-windows-386.exe ./$PACK' >> $BUILD && \
echo >> $BUILD && \
echo 'echo Compiling for darwin/amd64...' >> $BUILD && \
echo '`/osxcross/target/bin/osxcross-env`' >> $BUILD && \
echo 'CC=o64-clang GOOS=darwin GOARCH=amd64 CGO_ENABLED=1 go get -d ./$PACK' >> $BUILD && \
echo 'CC=o64-clang GOOS=darwin GOARCH=amd64 CGO_ENABLED=1 go build $V $R -o $NAME-darwin-amd64$R ./$PACK' >> $BUILD && \
echo >> $BUILD && \
echo 'echo Compiling for darwin/386...' >> $BUILD && \
echo 'CC=o32-clang GOOS=darwin GOARCH=386 CGO_ENABLED=1 go get -d ./$PACK' >> $BUILD && \
echo 'CC=o32-clang GOOS=darwin GOARCH=386 CGO_ENABLED=1 go build $V -o $NAME-darwin-386 ./$PACK' >> $BUILD && \
echo >> $BUILD && \
echo 'echo Moving binaries to host...' >> $BUILD && \
echo 'cp `ls -t | head -n 7` /build' >> $BUILD && \
chmod +x $BUILD
ENTRYPOINT ["./build.sh"]

12
xgo.go
View File

@ -21,7 +21,9 @@ 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 inPackage = flag.String("in", "", "Sub-package to build if not root import")
var outPrefix = flag.String("out", "", "Prefix to use for output naming (empty = package name)")
var srcBranch = flag.String("branch", "", "Version control branch to build")
// Command line arguments to pass to go build
var buildVerbose = flag.Bool("v", false, "Print the names of packages as they are compiled")
@ -52,7 +54,7 @@ func main() {
fmt.Println("found.")
}
// Cross compile the requested package into the local folder
if err := compile(flag.Args()[0], *outPrefix, *buildVerbose, *buildRace); err != nil {
if err := compile(flag.Args()[0], *srcBranch, *inPackage, *outPrefix, *buildVerbose, *buildRace); err != nil {
log.Fatalf("Failed to cross compile package: %v.", err)
}
}
@ -84,18 +86,20 @@ func pullDockerImage(image string) error {
}
// Cross compiles a requested package into the current working directory.
func compile(path string, prefix string, verbose bool, race bool) error {
func compile(repo string, branch string, pack 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)
fmt.Printf("Cross compiling %s...\n", repo)
return run(exec.Command("docker", "run",
"-v", folder+":/build",
"-e", "BRANCH="+branch,
"-e", "PACK="+pack,
"-e", "OUT="+prefix,
"-e", fmt.Sprintf("FLAG_V=%v", verbose),
"-e", fmt.Sprintf("FLAG_RACE=%v", race),
dockerDist+*goVersion, path))
dockerDist+*goVersion, repo))
}
// Executes a command synchronously, redirecting its output to stdout.