From 1ea89c8ebeabcfe4424a8c6f01e6a547aab66457 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Szil=C3=A1gyi?= Date: Tue, 24 Nov 2015 19:41:01 +0200 Subject: [PATCH] Add support for building archive files. --- README.md | 1 + docker/base/build.sh | 69 ++++++++++++++++++++++++++++++-------------- xgo.go | 4 +++ 3 files changed, 53 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 708057e..5c7958d 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,7 @@ A handful of flags can be passed to `go build`. The currently supported ones are - `-race`: enables data race detection (supported only on amd64, rest built without) - `-tags='tag list'`: list of build tags to consider satisfied during the build - `-ldflags='flag list'`: arguments to pass on each go tool link invocation + - `-buildmode=mode`: binary type to produce by the compiler ### Go releases diff --git a/docker/base/build.sh b/docker/base/build.sh index 26943ee..604381c 100644 --- a/docker/base/build.sh +++ b/docker/base/build.sh @@ -7,19 +7,46 @@ # Usage: build.sh # # Needed environment variables: -# REPO_REMOTE - Optional VCS remote if not the primary repository is needed -# REPO_BRANCH - Optional VCS branch to use, if not the master branch -# DEPS - Optional list of C dependency packages to build -# ARGS - Optional arguments to pass to C dependency configure scripts -# PACK - Optional sub-package, if not the import path is being built -# OUT - Optional output prefix to override the package name -# FLAG_V - Optional verbosity flag to set on the Go builder -# FLAG_X - Optional flag to print the build progress commands -# FLAG_RACE - Optional race flag to set on the Go builder -# TARGETS - Comma separated list of build targets to compile for -# GO_VERSION - Bootstrapped version of Go to disable uncupported targets -# EXT_GOPATH - GOPATH elements mounted from the host filesystem +# REPO_REMOTE - Optional VCS remote if not the primary repository is needed +# REPO_BRANCH - Optional VCS branch to use, if not the master branch +# DEPS - Optional list of C dependency packages to build +# ARGS - Optional arguments to pass to C dependency configure scripts +# PACK - Optional sub-package, if not the import path is being built +# OUT - Optional output prefix to override the package name +# FLAG_V - Optional verbosity flag to set on the Go builder +# FLAG_X - Optional flag to print the build progress commands +# FLAG_RACE - Optional race flag to set on the Go builder +# FLAG_TAGS - Optional tag flag to set on the Go builder +# FLAG_LDFLAGS - Optional ldflags flag to set on the Go builder +# FLAG_BUILDMODE - Optional buildmode flag to set on the Go builder +# TARGETS - Comma separated list of build targets to compile for +# GO_VERSION - Bootstrapped version of Go to disable uncupported targets +# EXT_GOPATH - GOPATH elements mounted from the host filesystem +# Define a function that figures out the binary extension +function extension { + if [ "$FLAG_BUILDMODE" == "archive" ] || [ "$FLAG_BUILDMODE" == "c-archive" ]; then + if [ "$1" == "windows" ]; then + echo ".lib" + else + echo ".a" + fi + elif [ "$FLAG_BUILDMODE" == "shared" ] || [ "$FLAG_BUILDMODE" == "c-shared" ]; then + if [ "$1" == "windows" ]; then + echo ".dll" + elif [ "$1" == "darwin" ] || [ "$1" == "ios" ]; then + echo ".dylib" + else + echo ".so" + fi + else + if [ "$1" == "windows" ]; then + echo ".exe" + fi + fi +} + +# Either set a local build environemnt, or pull any remote imports if [ "$EXT_GOPATH" != "" ]; then # If local builds are requested, inject the sources echo "Building locally $1..." @@ -145,7 +172,7 @@ for TARGET in $TARGETS; do echo "Compiling for android-$PLATFORM/arm..." CC=arm-linux-androideabi-gcc CXX=arm-linux-androideabi-g++ HOST=arm-linux-androideabi PREFIX=/usr/$ANDROID_CHAIN_ARM/arm-linux-androideabi $BUILD_DEPS /deps ${DEPS_ARGS[@]} CC=arm-linux-androideabi-gcc CXX=arm-linux-androideabi-g++ GOOS=android GOARCH=arm GOARM=7 CGO_ENABLED=1 CGO_CFLAGS="$CGO_CCPIE" CGO_CXXFLAGS="$CGO_CCPIE" CGO_LDFLAGS="$CGO_LDPIE" go get $V $X "${T[@]}" --ldflags="$V $LD" -d ./$PACK - CC=arm-linux-androideabi-gcc CXX=arm-linux-androideabi-g++ GOOS=android GOARCH=arm GOARM=7 CGO_ENABLED=1 CGO_CFLAGS="$CGO_CCPIE" CGO_CXXFLAGS="$CGO_CCPIE" CGO_LDFLAGS="$CGO_LDPIE" go build $V $X "${T[@]}" --ldflags="$V $EXT_LDPIE $LD" -o /build/$NAME-android-$PLATFORM-arm ./$PACK + CC=arm-linux-androideabi-gcc CXX=arm-linux-androideabi-g++ GOOS=android GOARCH=arm GOARM=7 CGO_ENABLED=1 CGO_CFLAGS="$CGO_CCPIE" CGO_CXXFLAGS="$CGO_CCPIE" CGO_LDFLAGS="$CGO_LDPIE" go build $V $X "${T[@]}" --ldflags="$V $EXT_LDPIE $LD" --buildmode=$FLAG_BUILDMODE -o "/build/$NAME-android-$PLATFORM-arm`extension android`" ./$PACK echo "Cleaning up Go runtime for android-$PLATFORM/arm..." rm -rf /usr/local/go/pkg/android_arm @@ -160,19 +187,19 @@ for TARGET in $TARGETS; do echo "Compiling for linux/amd64..." HOST=x86_64-linux PREFIX=/usr/local $BUILD_DEPS /deps ${DEPS_ARGS[@]} GOOS=linux GOARCH=amd64 CGO_ENABLED=1 go get $V $X "${T[@]}" --ldflags="$V $LD" -d ./$PACK - GOOS=linux GOARCH=amd64 CGO_ENABLED=1 go build $V $X "${T[@]}" --ldflags="$V $LD" $R -o /build/$NAME-linux-amd64$R ./$PACK + GOOS=linux GOARCH=amd64 CGO_ENABLED=1 go build $V $X "${T[@]}" --ldflags="$V $LD" $R --buildmode=$FLAG_BUILDMODE -o "/build/$NAME-linux-amd64$R`extension linux`" ./$PACK fi if ([ $XGOOS == "." ] || [ $XGOOS == "linux" ]) && ([ $XGOARCH == "." ] || [ $XGOARCH == "386" ]); then echo "Compiling for linux/386..." HOST=i686-linux PREFIX=/usr/local $BUILD_DEPS /deps ${DEPS_ARGS[@]} GOOS=linux GOARCH=386 CGO_ENABLED=1 go get $V $X "${T[@]}" --ldflags="$V $LD" -d ./$PACK - GOOS=linux GOARCH=386 CGO_ENABLED=1 go build $V $X "${T[@]}" --ldflags="$V $LD" -o /build/$NAME-linux-386 ./$PACK + GOOS=linux GOARCH=386 CGO_ENABLED=1 go build $V $X "${T[@]}" --ldflags="$V $LD" --buildmode=$FLAG_BUILDMODE -o "/build/$NAME-linux-386`extension linux`" ./$PACK fi if ([ $XGOOS == "." ] || [ $XGOOS == "linux" ]) && ([ $XGOARCH == "." ] || [ $XGOARCH == "arm" ]); then echo "Compiling for linux/arm..." CC=arm-linux-gnueabi-gcc-5 CXX=arm-linux-gnueabi-g++-5 HOST=arm-linux PREFIX=/usr/local/arm $BUILD_DEPS /deps ${DEPS_ARGS[@]} CC=arm-linux-gnueabi-gcc-5 CXX=arm-linux-gnueabi-g++-5 GOOS=linux GOARCH=arm CGO_ENABLED=1 GOARM=5 go get $V $X "${T[@]}" --ldflags="$V $LD" -d ./$PACK - CC=arm-linux-gnueabi-gcc-5 CXX=arm-linux-gnueabi-g++-5 GOOS=linux GOARCH=arm CGO_ENABLED=1 GOARM=5 go build $V $X "${T[@]}" --ldflags="$V $LD" -o /build/$NAME-linux-arm ./$PACK + CC=arm-linux-gnueabi-gcc-5 CXX=arm-linux-gnueabi-g++-5 GOOS=linux GOARCH=arm CGO_ENABLED=1 GOARM=5 go build $V $X "${T[@]}" --ldflags="$V $LD" --buildmode=$FLAG_BUILDMODE -o "/build/$NAME-linux-arm`extension linux`" ./$PACK fi # Check and build for Windows targets if [ $XGOOS == "." ] || [[ $XGOOS == windows* ]]; then @@ -193,13 +220,13 @@ for TARGET in $TARGETS; do echo "Compiling for windows-$PLATFORM/amd64..." CC=x86_64-w64-mingw32-gcc-posix CXX=x86_64-w64-mingw32-g++-posix HOST=x86_64-w64-mingw32 PREFIX=/usr/x86_64-w64-mingw32 $BUILD_DEPS /deps ${DEPS_ARGS[@]} CC=x86_64-w64-mingw32-gcc-posix CXX=x86_64-w64-mingw32-g++-posix GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CGO_CFLAGS="$CGO_NTDEF" CGO_CXXFLAGS="$CGO_NTDEF" go get $V $X "${T[@]}" --ldflags="$V $LD" -d ./$PACK - CC=x86_64-w64-mingw32-gcc-posix CXX=x86_64-w64-mingw32-g++-posix GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CGO_CFLAGS="$CGO_NTDEF" CGO_CXXFLAGS="$CGO_NTDEF" go build $V $X "${T[@]}" --ldflags="$V $LD" $R -o /build/$NAME-windows-$PLATFORM-amd64$R.exe ./$PACK + CC=x86_64-w64-mingw32-gcc-posix CXX=x86_64-w64-mingw32-g++-posix GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CGO_CFLAGS="$CGO_NTDEF" CGO_CXXFLAGS="$CGO_NTDEF" go build $V $X "${T[@]}" --ldflags="$V $LD" $R --buildmode=$FLAG_BUILDMODE -o "/build/$NAME-windows-$PLATFORM-amd64$R`extension windows`" ./$PACK fi if [ $XGOARCH == "." ] || [ $XGOARCH == "386" ]; then echo "Compiling for windows-$PLATFORM/386..." CC=i686-w64-mingw32-gcc-posix CXX=i686-w64-mingw32-g++-posix HOST=i686-w64-mingw32 PREFIX=/usr/i686-w64-mingw32 $BUILD_DEPS /deps ${DEPS_ARGS[@]} CC=i686-w64-mingw32-gcc-posix CXX=i686-w64-mingw32-g++-posix GOOS=windows GOARCH=386 CGO_ENABLED=1 CGO_CFLAGS="$CGO_NTDEF" CGO_CXXFLAGS="$CGO_NTDEF" go get $V $X "${T[@]}" --ldflags="$V $LD" -d ./$PACK - CC=i686-w64-mingw32-gcc-posix CXX=i686-w64-mingw32-g++-posix GOOS=windows GOARCH=386 CGO_ENABLED=1 CGO_CFLAGS="$CGO_NTDEF" CGO_CXXFLAGS="$CGO_NTDEF" go build $V $X "${T[@]}" --ldflags="$V $LD" -o /build/$NAME-windows-$PLATFORM-386.exe ./$PACK + CC=i686-w64-mingw32-gcc-posix CXX=i686-w64-mingw32-g++-posix GOOS=windows GOARCH=386 CGO_ENABLED=1 CGO_CFLAGS="$CGO_NTDEF" CGO_CXXFLAGS="$CGO_NTDEF" go build $V $X "${T[@]}" --ldflags="$V $LD" --buildmode=$FLAG_BUILDMODE -o "/build/$NAME-windows-$PLATFORM-386`extension windows`" ./$PACK fi fi # Check and build for OSX targets @@ -221,13 +248,13 @@ for TARGET in $TARGETS; do echo "Compiling for darwin-$PLATFORM/amd64..." CC=o64-clang CXX=o64-clang++ HOST=x86_64-apple-darwin13 PREFIX=/usr/local $BUILD_DEPS /deps ${DEPS_ARGS[@]} CC=o64-clang CXX=o64-clang++ GOOS=darwin GOARCH=amd64 CGO_ENABLED=1 go get $V $X "${T[@]}" --ldflags="$LDSTRIP $V $LD" -d ./$PACK - CC=o64-clang CXX=o64-clang++ GOOS=darwin GOARCH=amd64 CGO_ENABLED=1 go build $V $X "${T[@]}" --ldflags="$LDSTRIP $V $LD" $R -o /build/$NAME-darwin-$PLATFORM-amd64$R ./$PACK + CC=o64-clang CXX=o64-clang++ GOOS=darwin GOARCH=amd64 CGO_ENABLED=1 go build $V $X "${T[@]}" --ldflags="$LDSTRIP $V $LD" $R --buildmode=$FLAG_BUILDMODE -o "/build/$NAME-darwin-$PLATFORM-amd64$R`extension darwin`" ./$PACK fi if [ $XGOARCH == "." ] || [ $XGOARCH == "386" ]; then echo "Compiling for darwin-$PLATFORM/386..." CC=o32-clang CXX=o32-clang++ HOST=i386-apple-darwin13 PREFIX=/usr/local $BUILD_DEPS /deps ${DEPS_ARGS[@]} CC=o32-clang CXX=o32-clang++ GOOS=darwin GOARCH=386 CGO_ENABLED=1 go get $V $X "${T[@]}" --ldflags="$LDSTRIP $V $LD" -d ./$PACK - CC=o32-clang CXX=o32-clang++ GOOS=darwin GOARCH=386 CGO_ENABLED=1 go build $V $X "${T[@]}" --ldflags="$LDSTRIP $V $LD" -o /build/$NAME-darwin-$PLATFORM-386 ./$PACK + CC=o32-clang CXX=o32-clang++ GOOS=darwin GOARCH=386 CGO_ENABLED=1 go build $V $X "${T[@]}" --ldflags="$LDSTRIP $V $LD" --buildmode=$FLAG_BUILDMODE -o "/build/$NAME-darwin-$PLATFORM-386`extension darwin`" ./$PACK fi # Remove any automatically injected deployment target vars unset MACOSX_DEPLOYMENT_TARGET @@ -254,7 +281,7 @@ for TARGET in $TARGETS; do echo "Compiling for ios-$PLATFORM/arm..." CC=arm-apple-darwin11-clang CXX=arm-apple-darwin11-clang++ HOST=arm-apple-darwin11 PREFIX=/usr/local $BUILD_DEPS /deps ${DEPS_ARGS[@]} CC=arm-apple-darwin11-clang CXX=arm-apple-darwin11-clang++ GOOS=darwin GOARCH=arm GOARM=7 CGO_ENABLED=1 go get $V $X "${T[@]}" --ldflags="$LDSTRIP $V $LD" -d ./$PACK - CC=arm-apple-darwin11-clang CXX=arm-apple-darwin11-clang++ GOOS=darwin GOARCH=arm GOARM=7 CGO_ENABLED=1 go build $V $X "${T[@]}" --ldflags="$LDSTRIP $V $LD" -o /build/$NAME-ios-$PLATFORM-arm ./$PACK + CC=arm-apple-darwin11-clang CXX=arm-apple-darwin11-clang++ GOOS=darwin GOARCH=arm GOARM=7 CGO_ENABLED=1 go build $V $X "${T[@]}" --ldflags="$LDSTRIP $V $LD" --buildmode=$FLAG_BUILDMODE -o "/build/$NAME-ios-$PLATFORM-arm`extension ios`" ./$PACK echo "Cleaning up Go runtime for ios-$PLATFORM/arm..." rm -rf /usr/local/go/pkg/darwin_arm diff --git a/xgo.go b/xgo.go index 26bf11c..d263751 100644 --- a/xgo.go +++ b/xgo.go @@ -61,6 +61,7 @@ var ( buildRace = flag.Bool("race", false, "Enable data race detection (supported only on amd64)") buildTags = flag.String("tags", "", "List of build tags to consider satisfied during the build") buildLdFlags = flag.String("ldflags", "", "Arguments to pass on each go tool link invocation") + buildMode = flag.String("buildmode", "default", "Indicates which kind of object file to build") ) // BuildFlags is a simple collection of flags to fine tune a build. @@ -70,6 +71,7 @@ type BuildFlags struct { Race bool // Enable data race detection (supported only on amd64) Tags string // List of build tags to consider satisfied during the build LdFlags string // Arguments to pass on each go tool link invocation + Mode string // Indicates which kind of object file to build } func main() { @@ -153,6 +155,7 @@ func main() { Race: *buildRace, Tags: *buildTags, LdFlags: *buildLdFlags, + Mode: *buildMode, } if err := compile(image, config, flags, *outFolder); err != nil { log.Fatalf("Failed to cross compile package: %v.", err) @@ -275,6 +278,7 @@ func compile(image string, config *ConfigFlags, flags *BuildFlags, dest string) "-e", fmt.Sprintf("FLAG_RACE=%v", flags.Race), "-e", fmt.Sprintf("FLAG_TAGS=%s", flags.Tags), "-e", fmt.Sprintf("FLAG_LDFLAGS=%s", flags.LdFlags), + "-e", fmt.Sprintf("FLAG_BUILDMODE=%s", flags.Mode), "-e", "TARGETS=" + strings.Replace(strings.Join(config.Targets, " "), "*", ".", -1), } for i := 0; i < len(locals); i++ {