Skip to content

Commit

Permalink
call userprovided setup.sh and build.sh script if existing (#97)
Browse files Browse the repository at this point in the history
* call userprovided setup.sh and build.sh script if existing

this allows to execute arbitrary command. first in a setup phase and
later in the loop over all targets.
the scripts are source to have access on all variables. XGOOS and
XGOARCH are set to the actual value for the execution time of the
script.

fixes: #96

* Adjust README.md

Signed-off-by: Andrew Thornton <art27@cantab.net>

* do not hardcode hooks directory but add option to xgo to mount a given directory into the container

setup.sh and build.sh are searched in that directory

Signed-off-by: Andrew Thornton <art27@cantab.net>
Co-authored-by: Andrew Thornton <art27@cantab.net>
  • Loading branch information
bearsh and zeripath committed Jan 17, 2023
1 parent 22d7aee commit 94aee17
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 18 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,24 @@ Some trivial arguments may be passed to the dependencies' configure script via

Note, that since xgo needs to cross compile the dependencies for each platform
and architecture separately, build time can increase significantly.

### Hooks

To give the user more power, xgo provides two hook scripts which will be sourced
during the build if they are available in a provided hooks directory (provided
by the `--hooksdir` argument, which mounts that directory into the container).
The first one is `setup.sh` which will be sourced after everything is set up.
This script can e.g. be used to install additional packages in the container.
The second is `build.sh` which will be sourced for each target (before the
actual build process).

All environment variables set up by xgo are available in the scripts.

Within `build.sh` there are several target specific environment variables:

* `XGOOS` and `XGOARCH` are expanded to the actual value defined by the [build targets](#limit-build-targets).
* `CC`: C cross compiler to use for the build
* `HOST`: Target platform to build
* `PREFIX`: File-system path where to install the built binaries.

For further reference, see [build.sh](docker/base/build.sh)
45 changes: 27 additions & 18 deletions docker/build/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ function extension {
fi
}

function do_build {
if [ -f "/hooksdir/build.sh" ]; then echo "source build.sh hook"; source "/hooksdir/build.sh"; fi
# call dedicated build script
$BUILD_DEPS /deps "${DEPS_ARGS[@]}"
}

GO_VERSION_MAJOR=$(go version | sed -e 's/.*go\([0-9]\+\)\..*/\1/')
GO_VERSION_MINOR=$(go version | sed -e 's/.*go[0-9]\+\.\([0-9]\+\)\..*/\1/')
GO111MODULE=$(go env GO111MODULE)
Expand Down Expand Up @@ -200,6 +206,9 @@ fi

if [ "${#LD[@]}" -gt 0 ]; then LDF=(--ldflags="$(printf "%s " "${LD[@]}")"); fi

# source setup.sh if existing
if [ -f "/hooksdir/setup.sh" ]; then echo "source setup.sh hook"; source "/hooksdir/setup.sh"; fi

# Build for each requested platform individually
for TARGET in $TARGETS; do
# Split the target into platform and architecture
Expand All @@ -210,7 +219,7 @@ for TARGET in $TARGETS; do
if { [ "$XGOOS" == "." ] || [ "$XGOOS" == "linux" ]; } && { [ "$XGOARCH" == "." ] || [ "$XGOARCH" == "amd64" ]; }; then
echo "Compiling for linux/amd64..."
mkdir -p /gocache/linux/amd64
GOCACHE=/gocache/linux/amd64 HOST=x86_64-linux PREFIX=/usr/local $BUILD_DEPS /deps "${DEPS_ARGS[@]}"
XGOOS="linux" XGOARCH="amd64" GOCACHE=/gocache/linux/amd64 HOST=x86_64-linux PREFIX=/usr/local do_build
if [[ "$USEMODULES" == false ]]; then
GOCACHE=/gocache/linux/amd64 GOOS=linux GOARCH=amd64 CGO_ENABLED=1 go get $V $X "${T[@]}" -d "$PACK_RELPATH"
fi
Expand All @@ -219,7 +228,7 @@ for TARGET in $TARGETS; do
if { [ "$XGOOS" == "." ] || [ "$XGOOS" == "linux" ]; } && { [ "$XGOARCH" == "." ] || [ "$XGOARCH" == "386" ]; }; then
echo "Compiling for linux/386..."
mkdir -p /gocache/linux/386
GOCACHE=/gocache/linux/386 CC="gcc -m32" CXX="g++ -m32" HOST=i686-linux PREFIX=/usr/local $BUILD_DEPS /deps "${DEPS_ARGS[@]}"
XGOOS="linux" XGOARCH="386" GOCACHE=/gocache/linux/386 CC="gcc -m32" CXX="g++ -m32" HOST=i686-linux PREFIX=/usr/local do_build
if [[ "$USEMODULES" == false ]]; then
GOCACHE=/gocache/linux/386 GOOS=linux GOARCH=386 CGO_ENABLED=1 go get $V $X "${T[@]}" -d "$PACK_RELPATH"
fi
Expand All @@ -231,7 +240,7 @@ for TARGET in $TARGETS; do
ln -s /usr/local/go/pkg/linux_arm-5 /usr/local/go/pkg/linux_arm
fi
echo "Compiling for linux/arm-5..."
GOCACHE=/gocache/linux/arm-5 CC=arm-linux-gnueabi-gcc-6 CXX=arm-linux-gnueabi-g++-6 HOST=arm-linux-gnueabi PREFIX=/usr/arm-linux-gnueabi CFLAGS="-march=armv5" CXXFLAGS="-march=armv5" $BUILD_DEPS /deps "${DEPS_ARGS[@]}"
XGOOS="linux" XGOARCH="arm-5" GOCACHE=/gocache/linux/arm-5 CC=arm-linux-gnueabi-gcc-6 CXX=arm-linux-gnueabi-g++-6 HOST=arm-linux-gnueabi PREFIX=/usr/arm-linux-gnueabi CFLAGS="-march=armv5" CXXFLAGS="-march=armv5" do_build
export PKG_CONFIG_PATH=/usr/arm-linux-gnueabi/lib/pkgconfig

if [[ "$USEMODULES" == false ]]; then
Expand All @@ -250,7 +259,7 @@ for TARGET in $TARGETS; do
ln -s /usr/local/go/pkg/linux_arm-6 /usr/local/go/pkg/linux_arm

echo "Compiling for linux/arm-6..."
GOCACHE=/gocache/linux/arm-6 CC=arm-linux-gnueabi-gcc-6 CXX=arm-linux-gnueabi-g++-6 HOST=arm-linux-gnueabi PREFIX=/usr/arm-linux-gnueabi CFLAGS="-march=armv6" CXXFLAGS="-march=armv6" $BUILD_DEPS /deps "${DEPS_ARGS[@]}"
XGOOS="linux" XGOARCH="arm-6" GOCACHE=/gocache/linux/arm-6 CC=arm-linux-gnueabi-gcc-6 CXX=arm-linux-gnueabi-g++-6 HOST=arm-linux-gnueabi PREFIX=/usr/arm-linux-gnueabi CFLAGS="-march=armv6" CXXFLAGS="-march=armv6" do_build
export PKG_CONFIG_PATH=/usr/arm-linux-gnueabi/lib/pkgconfig

if [[ "$USEMODULES" == false ]]; then
Expand All @@ -269,7 +278,7 @@ for TARGET in $TARGETS; do
ln -s /usr/local/go/pkg/linux_arm-7 /usr/local/go/pkg/linux_arm

echo "Compiling for linux/arm-7..."
GOCACHE=/gocache/linux/arm-7 CC=arm-linux-gnueabihf-gcc-6 CXX=arm-linux-gnueabihf-g++-6 HOST=arm-linux-gnueabihf PREFIX=/usr/arm-linux-gnueabihf CFLAGS="-march=armv7-a -fPIC" CXXFLAGS="-march=armv7-a -fPIC" $BUILD_DEPS /deps "${DEPS_ARGS[@]}"
XGOOS="linux" XGOARCH="arm-7" GOCACHE=/gocache/linux/arm-7 CC=arm-linux-gnueabihf-gcc-6 CXX=arm-linux-gnueabihf-g++-6 HOST=arm-linux-gnueabihf PREFIX=/usr/arm-linux-gnueabihf CFLAGS="-march=armv7-a -fPIC" CXXFLAGS="-march=armv7-a -fPIC" do_build
export PKG_CONFIG_PATH=/usr/arm-linux-gnueabihf/lib/pkgconfig

if [[ "$USEMODULES" == false ]]; then
Expand All @@ -286,7 +295,7 @@ for TARGET in $TARGETS; do
else
echo "Compiling for linux/arm64..."
mkdir -p /gocache/linux/arm64
GOCACHE=/gocache/linux/arm64 CC=aarch64-linux-gnu-gcc-6 CXX=aarch64-linux-gnu-g++-6 HOST=aarch64-linux-gnu PREFIX=/usr/aarch64-linux-gnu $BUILD_DEPS /deps "${DEPS_ARGS[@]}"
XGOOS="linux" XGOARCH="arm64" GOCACHE=/gocache/linux/arm64 CC=aarch64-linux-gnu-gcc-6 CXX=aarch64-linux-gnu-g++-6 HOST=aarch64-linux-gnu PREFIX=/usr/aarch64-linux-gnu do_build
export PKG_CONFIG_PATH=/usr/aarch64-linux-gnu/lib/pkgconfig

if [[ "$USEMODULES" == false ]]; then
Expand All @@ -301,7 +310,7 @@ for TARGET in $TARGETS; do
else
echo "Compiling for linux/mips64..."
mkdir -p /gocache/linux/mips64
GOCACHE=/gocache/linux/mips64 CC=mips64-linux-gnuabi64-gcc-6 CXX=mips64-linux-gnuabi64-g++-6 HOST=mips64-linux-gnuabi64 PREFIX=/usr/mips64-linux-gnuabi64 $BUILD_DEPS /deps "${DEPS_ARGS[@]}"
XGOOS="linux" XGOARCH="mips64" GOCACHE=/gocache/linux/mips64 CC=mips64-linux-gnuabi64-gcc-6 CXX=mips64-linux-gnuabi64-g++-6 HOST=mips64-linux-gnuabi64 PREFIX=/usr/mips64-linux-gnuabi64 do_build
export PKG_CONFIG_PATH=/usr/mips64-linux-gnuabi64/lib/pkgconfig

if [[ "$USEMODULES" == false ]]; then
Expand All @@ -316,7 +325,7 @@ for TARGET in $TARGETS; do
else
echo "Compiling for linux/mips64le..."
mkdir -p /gocache/linux/mips64le
GOCACHE=/gocache/linux/mips64le CC=mips64el-linux-gnuabi64-gcc-6 CXX=mips64el-linux-gnuabi64-g++-6 HOST=mips64el-linux-gnuabi64 PREFIX=/usr/mips64el-linux-gnuabi64 $BUILD_DEPS /deps "${DEPS_ARGS[@]}"
XGOOS="linux" XGOARCH="mips64le" GOCACHE=/gocache/linux/mips64le CC=mips64el-linux-gnuabi64-gcc-6 CXX=mips64el-linux-gnuabi64-g++-6 HOST=mips64el-linux-gnuabi64 PREFIX=/usr/mips64el-linux-gnuabi64 do_build
export PKG_CONFIG_PATH=/usr/mips64le-linux-gnuabi64/lib/pkgconfig

if [[ "$USEMODULES" == false ]]; then
Expand All @@ -331,7 +340,7 @@ for TARGET in $TARGETS; do
else
echo "Compiling for linux/mips..."
mkdir -p /gocache/linux/mips
GOCACHE=/gocache/linux/mips CC=mips-linux-gnu-gcc-6 CXX=mips-linux-gnu-g++-6 HOST=mips-linux-gnu PREFIX=/usr/mips-linux-gnu $BUILD_DEPS /deps "${DEPS_ARGS[@]}"
XGOOS="linux" XGOARCH="mips" GOCACHE=/gocache/linux/mips CC=mips-linux-gnu-gcc-6 CXX=mips-linux-gnu-g++-6 HOST=mips-linux-gnu PREFIX=/usr/mips-linux-gnu do_build
export PKG_CONFIG_PATH=/usr/mips-linux-gnu/lib/pkgconfig

if [[ "$USEMODULES" == false ]]; then
Expand All @@ -346,7 +355,7 @@ for TARGET in $TARGETS; do
else
echo "Compiling for linux/s390x..."
mkdir -p /gocache/linux/s390x
GOCACHE=/gocache/linux/s390x CC=s390x-linux-gnu-gcc-6 CXX=s390x-linux-gnu-g++-6 HOST=s390x-linux-gnu PREFIX=/usr/s390x-linux-gnu $BUILD_DEPS /deps "${DEPS_ARGS[@]}"
XGOOS="linux" XGOARCH="s390x" GOCACHE=/gocache/linux/s390x CC=s390x-linux-gnu-gcc-6 CXX=s390x-linux-gnu-g++-6 HOST=s390x-linux-gnu PREFIX=/usr/s390x-linux-gnu do_build
export PKG_CONFIG_PATH=/usr/s390x-linux-gnu/lib/pkgconfig

if [[ "$USEMODULES" == false ]]; then
Expand All @@ -361,7 +370,7 @@ for TARGET in $TARGETS; do
else
echo "Compiling for linux/riscv64..."
mkdir -p /gocache/linux/riscv64
GOCACHE=/gocache/linux/riscv64 CC=riscv64-linux-gnu-gcc-8 CXX=riscv64-linux-gnu-g++-8 HOST=riscv64-linux-gnu PREFIX=/usr/riscv64-linux-gnu $BUILD_DEPS /deps "${DEPS_ARGS[@]}"
XGOOS="linux" XGOARCH="riscv64" GOCACHE=/gocache/linux/riscv64 CC=riscv64-linux-gnu-gcc-8 CXX=riscv64-linux-gnu-g++-8 HOST=riscv64-linux-gnu PREFIX=/usr/riscv64-linux-gnu do_build
export PKG_CONFIG_PATH=/usr/riscv64-linux-gnu/lib/pkgconfig

if [[ "$USEMODULES" == false ]]; then
Expand All @@ -376,7 +385,7 @@ for TARGET in $TARGETS; do
else
echo "Compiling for linux/ppc64le..."
mkdir -p /gocache/linux/ppc64le
GOCACHE=/gocache/linux/ppc64le CC=powerpc64le-linux-gnu-gcc-6 CXX=powerpc64le-linux-gnu-g++-6 HOST=ppc64le-linux-gnu PREFIX=/usr/ppc64le-linux-gnu $BUILD_DEPS /deps "${DEPS_ARGS[@]}"
XGOOS="linux" XGOARCH="ppc64le" GOCACHE=/gocache/linux/ppc64le CC=powerpc64le-linux-gnu-gcc-6 CXX=powerpc64le-linux-gnu-g++-6 HOST=ppc64le-linux-gnu PREFIX=/usr/ppc64le-linux-gnu do_build
export PKG_CONFIG_PATH=/usr/ppc64le-linux-gnu/lib/pkgconfig

if [[ "$USEMODULES" == false ]]; then
Expand All @@ -391,7 +400,7 @@ for TARGET in $TARGETS; do
else
echo "Compiling for linux/mipsle..."
mkdir -p /gocache/linux/mipsle
GOCACHE=/gocache/linux/mipsle CC=mipsel-linux-gnu-gcc-6 CXX=mipsel-linux-gnu-g++-6 HOST=mipsel-linux-gnu PREFIX=/usr/mipsel-linux-gnu $BUILD_DEPS /deps "${DEPS_ARGS[@]}"
XGOOS="linux" XGOARCH="mipsle" GOCACHE=/gocache/linux/mipsle CC=mipsel-linux-gnu-gcc-6 CXX=mipsel-linux-gnu-g++-6 HOST=mipsel-linux-gnu PREFIX=/usr/mipsel-linux-gnu do_build
export PKG_CONFIG_PATH=/usr/mipsle-linux-gnu/lib/pkgconfig

if [[ "$USEMODULES" == false ]]; then
Expand All @@ -418,7 +427,7 @@ for TARGET in $TARGETS; do
if [ "$XGOARCH" == "." ] || [ "$XGOARCH" == "amd64" ]; then
echo "Compiling for windows-$PLATFORM/amd64..."
mkdir -p /gocache/windows-$PLATFORM/amd64
GOCACHE=/gocache/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[@]}"
XGOOS="windows-$PLATFORM" XGOARCH="amd64" GOCACHE=/gocache/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 do_build
export PKG_CONFIG_PATH=/usr/x86_64-w64-mingw32/lib/pkgconfig

if [[ "$USEMODULES" == false ]]; then
Expand All @@ -429,7 +438,7 @@ for TARGET in $TARGETS; do
if [ "$XGOARCH" == "." ] || [ "$XGOARCH" == "386" ]; then
echo "Compiling for windows-$PLATFORM/386..."
mkdir -p /gocache/windows-$PLATFORM/386
GOCACHE=/gocache/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[@]}"
XGOOS="windows-$PLATFORM" XGOARCH="386" GOCACHE=/gocache/windows-$PLATFORM/386 CC=i686-w64-mingw32-gcc-posix CXX=i686-w64-mingw32-g++-posix HOST=i686-w64-mingw32 PREFIX=/usr/i686-w64-mingw32 do_build
export PKG_CONFIG_PATH=/usr/i686-w64-mingw32/lib/pkgconfig

if [[ "$USEMODULES" == false ]]; then
Expand Down Expand Up @@ -459,7 +468,7 @@ for TARGET in $TARGETS; do
if [ "$XGOARCH" == "." ] || [ "$XGOARCH" == "amd64" ]; then
echo "Compiling for darwin-$PLATFORM/amd64..."
mkdir -p /gocache/darwin-$PLATFORM/amd64
GOCACHE=/gocache/darwin-$PLATFORM/amd64 CC=o64-clang CXX=o64-clang++ HOST=x86_64-apple-darwin15 PREFIX=/usr/local $BUILD_DEPS /deps "${DEPS_ARGS[@]}"
XGOOS="darwin-$PLATFORM" XGOARCH="amd64" GOCACHE=/gocache/darwin-$PLATFORM/amd64 CC=o64-clang CXX=o64-clang++ HOST=x86_64-apple-darwin15 PREFIX=/usr/local do_build
if [[ "$USEMODULES" == false ]]; then
GOCACHE=/gocache/darwin-$PLATFORM/amd64 CC=o64-clang CXX=o64-clang++ GOOS=darwin GOARCH=amd64 CGO_ENABLED=1 go get $V $X "${T[@]}" "${LDFS[@]}" "${GC[@]}" -d "$PACK_RELPATH"
fi
Expand All @@ -471,7 +480,7 @@ for TARGET in $TARGETS; do
else
echo "Compiling for darwin-$PLATFORM/arm64..."
mkdir -p /gocache/darwin-$PLATFORM/arm64
GOCACHE=/gocache/darwin-$PLATFORM/arm64 CC=o64-clang CXX=o64-clang++ HOST=arm64-apple-darwin15 PREFIX=/usr/local $BUILD_DEPS /deps "${DEPS_ARGS[@]}"
XGOOS="darwin-$PLATFORM" XGOARCH="arm64" GOCACHE=/gocache/darwin-$PLATFORM/arm64 CC=o64-clang CXX=o64-clang++ HOST=arm64-apple-darwin15 PREFIX=/usr/local do_build
if [[ "$USEMODULES" == false ]]; then
GOCACHE=/gocache/darwin-$PLATFORM/arm64 CC=o64-clang CXX=o64-clang++ GOOS=darwin GOARCH=arm64 CGO_ENABLED=1 go get $V $X "${T[@]}" "${LDFS[@]}" "${GC[@]}" -d "$PACK_RELPATH"
fi
Expand All @@ -486,7 +495,7 @@ for TARGET in $TARGETS; do
# Build the requested freebsd binaries
if [ "$XGOARCH" == "." ] || [ "$XGOARCH" == "amd64" ]; then
echo "Compiling for freebsd/amd64..."
CC=x86_64-pc-freebsd12-gcc HOST=x86_64-pc-freebsd12 PREFIX=/freebsdcross/x86_64-pc-freebsd12 $BUILD_DEPS /deps "${DEPS_ARGS[@]}"
XGOOS="freebsd" XGOARCH="amd64" CC=x86_64-pc-freebsd12-gcc HOST=x86_64-pc-freebsd12 PREFIX=/freebsdcross/x86_64-pc-freebsd12 do_build
export PKG_CONFIG_PATH=/freebsdcross/x86_64-pc-freebsd12/lib/pkgconfig

if [[ "$USEMODULES" == false ]]; then
Expand Down
13 changes: 13 additions & 0 deletions xgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ var (
dockerImage = flag.String("image", "", "Use custom docker image instead of official distribution")
dockerEnv = flag.String("env", "", "Comma separated custom environments added to docker run -e")
dockerArgs = flag.String("dockerargs", "", "Comma separated arguments added to docker run")
hooksDir = flag.String("hooksdir", "", "Directory with user hook scripts (setup.sh, build.sh)")
forwardSsh = flag.Bool("ssh", false, "Enable ssh agent forwarding")
)

Expand Down Expand Up @@ -208,6 +209,18 @@ func main() {
log.Fatalf("Failed to resolve destination path (%s): %v.", *outFolder, err)
}
}
if *hooksDir != "" {
dir, err := filepath.Abs(*hooksDir)
if err != nil {
log.Fatalf("Failed to resolve hooksdir path (%s): %v.", *hooksDir, err)
}
if i, err := os.Stat(dir); err != nil {
log.Fatalf("Failed to resolve hooksdir path (%s): %v.", *hooksDir, err)
} else if !i.IsDir() {
log.Fatalf("Given hooksdir (%s) is not a directory.", *hooksDir)
}
config.DockerArgs = append(config.DockerArgs, "--mount", fmt.Sprintf(`type=bind,source=%s,target=/hooksdir`, dir))
}
// Execute the cross compilation, either in a container or the current system
if !xgoInXgo {
err = compile(image, config, flags, folder)
Expand Down

0 comments on commit 94aee17

Please sign in to comment.