Skip to content

Commit 94aee17

Browse files
bearshzeripath
andauthored
call userprovided setup.sh and build.sh script if existing (#97)
* 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 <[email protected]> * 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 <[email protected]> Co-authored-by: Andrew Thornton <[email protected]>
1 parent 22d7aee commit 94aee17

File tree

3 files changed

+61
-18
lines changed

3 files changed

+61
-18
lines changed

Diff for: README.md

+21
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,24 @@ Some trivial arguments may be passed to the dependencies' configure script via
256256

257257
Note, that since xgo needs to cross compile the dependencies for each platform
258258
and architecture separately, build time can increase significantly.
259+
260+
### Hooks
261+
262+
To give the user more power, xgo provides two hook scripts which will be sourced
263+
during the build if they are available in a provided hooks directory (provided
264+
by the `--hooksdir` argument, which mounts that directory into the container).
265+
The first one is `setup.sh` which will be sourced after everything is set up.
266+
This script can e.g. be used to install additional packages in the container.
267+
The second is `build.sh` which will be sourced for each target (before the
268+
actual build process).
269+
270+
All environment variables set up by xgo are available in the scripts.
271+
272+
Within `build.sh` there are several target specific environment variables:
273+
274+
* `XGOOS` and `XGOARCH` are expanded to the actual value defined by the [build targets](#limit-build-targets).
275+
* `CC`: C cross compiler to use for the build
276+
* `HOST`: Target platform to build
277+
* `PREFIX`: File-system path where to install the built binaries.
278+
279+
For further reference, see [build.sh](docker/base/build.sh)

Diff for: docker/build/build.sh

+27-18
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ function extension {
4747
fi
4848
}
4949

50+
function do_build {
51+
if [ -f "/hooksdir/build.sh" ]; then echo "source build.sh hook"; source "/hooksdir/build.sh"; fi
52+
# call dedicated build script
53+
$BUILD_DEPS /deps "${DEPS_ARGS[@]}"
54+
}
55+
5056
GO_VERSION_MAJOR=$(go version | sed -e 's/.*go\([0-9]\+\)\..*/\1/')
5157
GO_VERSION_MINOR=$(go version | sed -e 's/.*go[0-9]\+\.\([0-9]\+\)\..*/\1/')
5258
GO111MODULE=$(go env GO111MODULE)
@@ -200,6 +206,9 @@ fi
200206

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

209+
# source setup.sh if existing
210+
if [ -f "/hooksdir/setup.sh" ]; then echo "source setup.sh hook"; source "/hooksdir/setup.sh"; fi
211+
203212
# Build for each requested platform individually
204213
for TARGET in $TARGETS; do
205214
# Split the target into platform and architecture
@@ -210,7 +219,7 @@ for TARGET in $TARGETS; do
210219
if { [ "$XGOOS" == "." ] || [ "$XGOOS" == "linux" ]; } && { [ "$XGOARCH" == "." ] || [ "$XGOARCH" == "amd64" ]; }; then
211220
echo "Compiling for linux/amd64..."
212221
mkdir -p /gocache/linux/amd64
213-
GOCACHE=/gocache/linux/amd64 HOST=x86_64-linux PREFIX=/usr/local $BUILD_DEPS /deps "${DEPS_ARGS[@]}"
222+
XGOOS="linux" XGOARCH="amd64" GOCACHE=/gocache/linux/amd64 HOST=x86_64-linux PREFIX=/usr/local do_build
214223
if [[ "$USEMODULES" == false ]]; then
215224
GOCACHE=/gocache/linux/amd64 GOOS=linux GOARCH=amd64 CGO_ENABLED=1 go get $V $X "${T[@]}" -d "$PACK_RELPATH"
216225
fi
@@ -219,7 +228,7 @@ for TARGET in $TARGETS; do
219228
if { [ "$XGOOS" == "." ] || [ "$XGOOS" == "linux" ]; } && { [ "$XGOARCH" == "." ] || [ "$XGOARCH" == "386" ]; }; then
220229
echo "Compiling for linux/386..."
221230
mkdir -p /gocache/linux/386
222-
GOCACHE=/gocache/linux/386 CC="gcc -m32" CXX="g++ -m32" HOST=i686-linux PREFIX=/usr/local $BUILD_DEPS /deps "${DEPS_ARGS[@]}"
231+
XGOOS="linux" XGOARCH="386" GOCACHE=/gocache/linux/386 CC="gcc -m32" CXX="g++ -m32" HOST=i686-linux PREFIX=/usr/local do_build
223232
if [[ "$USEMODULES" == false ]]; then
224233
GOCACHE=/gocache/linux/386 GOOS=linux GOARCH=386 CGO_ENABLED=1 go get $V $X "${T[@]}" -d "$PACK_RELPATH"
225234
fi
@@ -231,7 +240,7 @@ for TARGET in $TARGETS; do
231240
ln -s /usr/local/go/pkg/linux_arm-5 /usr/local/go/pkg/linux_arm
232241
fi
233242
echo "Compiling for linux/arm-5..."
234-
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[@]}"
243+
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
235244
export PKG_CONFIG_PATH=/usr/arm-linux-gnueabi/lib/pkgconfig
236245

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

252261
echo "Compiling for linux/arm-6..."
253-
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[@]}"
262+
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
254263
export PKG_CONFIG_PATH=/usr/arm-linux-gnueabi/lib/pkgconfig
255264

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

271280
echo "Compiling for linux/arm-7..."
272-
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[@]}"
281+
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
273282
export PKG_CONFIG_PATH=/usr/arm-linux-gnueabihf/lib/pkgconfig
274283

275284
if [[ "$USEMODULES" == false ]]; then
@@ -286,7 +295,7 @@ for TARGET in $TARGETS; do
286295
else
287296
echo "Compiling for linux/arm64..."
288297
mkdir -p /gocache/linux/arm64
289-
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[@]}"
298+
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
290299
export PKG_CONFIG_PATH=/usr/aarch64-linux-gnu/lib/pkgconfig
291300

292301
if [[ "$USEMODULES" == false ]]; then
@@ -301,7 +310,7 @@ for TARGET in $TARGETS; do
301310
else
302311
echo "Compiling for linux/mips64..."
303312
mkdir -p /gocache/linux/mips64
304-
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[@]}"
313+
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
305314
export PKG_CONFIG_PATH=/usr/mips64-linux-gnuabi64/lib/pkgconfig
306315

307316
if [[ "$USEMODULES" == false ]]; then
@@ -316,7 +325,7 @@ for TARGET in $TARGETS; do
316325
else
317326
echo "Compiling for linux/mips64le..."
318327
mkdir -p /gocache/linux/mips64le
319-
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[@]}"
328+
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
320329
export PKG_CONFIG_PATH=/usr/mips64le-linux-gnuabi64/lib/pkgconfig
321330

322331
if [[ "$USEMODULES" == false ]]; then
@@ -331,7 +340,7 @@ for TARGET in $TARGETS; do
331340
else
332341
echo "Compiling for linux/mips..."
333342
mkdir -p /gocache/linux/mips
334-
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[@]}"
343+
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
335344
export PKG_CONFIG_PATH=/usr/mips-linux-gnu/lib/pkgconfig
336345

337346
if [[ "$USEMODULES" == false ]]; then
@@ -346,7 +355,7 @@ for TARGET in $TARGETS; do
346355
else
347356
echo "Compiling for linux/s390x..."
348357
mkdir -p /gocache/linux/s390x
349-
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[@]}"
358+
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
350359
export PKG_CONFIG_PATH=/usr/s390x-linux-gnu/lib/pkgconfig
351360

352361
if [[ "$USEMODULES" == false ]]; then
@@ -361,7 +370,7 @@ for TARGET in $TARGETS; do
361370
else
362371
echo "Compiling for linux/riscv64..."
363372
mkdir -p /gocache/linux/riscv64
364-
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[@]}"
373+
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
365374
export PKG_CONFIG_PATH=/usr/riscv64-linux-gnu/lib/pkgconfig
366375

367376
if [[ "$USEMODULES" == false ]]; then
@@ -376,7 +385,7 @@ for TARGET in $TARGETS; do
376385
else
377386
echo "Compiling for linux/ppc64le..."
378387
mkdir -p /gocache/linux/ppc64le
379-
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[@]}"
388+
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
380389
export PKG_CONFIG_PATH=/usr/ppc64le-linux-gnu/lib/pkgconfig
381390

382391
if [[ "$USEMODULES" == false ]]; then
@@ -391,7 +400,7 @@ for TARGET in $TARGETS; do
391400
else
392401
echo "Compiling for linux/mipsle..."
393402
mkdir -p /gocache/linux/mipsle
394-
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[@]}"
403+
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
395404
export PKG_CONFIG_PATH=/usr/mipsle-linux-gnu/lib/pkgconfig
396405

397406
if [[ "$USEMODULES" == false ]]; then
@@ -418,7 +427,7 @@ for TARGET in $TARGETS; do
418427
if [ "$XGOARCH" == "." ] || [ "$XGOARCH" == "amd64" ]; then
419428
echo "Compiling for windows-$PLATFORM/amd64..."
420429
mkdir -p /gocache/windows-$PLATFORM/amd64
421-
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[@]}"
430+
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
422431
export PKG_CONFIG_PATH=/usr/x86_64-w64-mingw32/lib/pkgconfig
423432

424433
if [[ "$USEMODULES" == false ]]; then
@@ -429,7 +438,7 @@ for TARGET in $TARGETS; do
429438
if [ "$XGOARCH" == "." ] || [ "$XGOARCH" == "386" ]; then
430439
echo "Compiling for windows-$PLATFORM/386..."
431440
mkdir -p /gocache/windows-$PLATFORM/386
432-
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[@]}"
441+
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
433442
export PKG_CONFIG_PATH=/usr/i686-w64-mingw32/lib/pkgconfig
434443

435444
if [[ "$USEMODULES" == false ]]; then
@@ -459,7 +468,7 @@ for TARGET in $TARGETS; do
459468
if [ "$XGOARCH" == "." ] || [ "$XGOARCH" == "amd64" ]; then
460469
echo "Compiling for darwin-$PLATFORM/amd64..."
461470
mkdir -p /gocache/darwin-$PLATFORM/amd64
462-
GOCACHE=/gocache/darwin-$PLATFORM/amd64 CC=o64-clang CXX=o64-clang++ HOST=x86_64-apple-darwin15 PREFIX=/usr/local $BUILD_DEPS /deps "${DEPS_ARGS[@]}"
471+
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
463472
if [[ "$USEMODULES" == false ]]; then
464473
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"
465474
fi
@@ -471,7 +480,7 @@ for TARGET in $TARGETS; do
471480
else
472481
echo "Compiling for darwin-$PLATFORM/arm64..."
473482
mkdir -p /gocache/darwin-$PLATFORM/arm64
474-
GOCACHE=/gocache/darwin-$PLATFORM/arm64 CC=o64-clang CXX=o64-clang++ HOST=arm64-apple-darwin15 PREFIX=/usr/local $BUILD_DEPS /deps "${DEPS_ARGS[@]}"
483+
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
475484
if [[ "$USEMODULES" == false ]]; then
476485
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"
477486
fi
@@ -486,7 +495,7 @@ for TARGET in $TARGETS; do
486495
# Build the requested freebsd binaries
487496
if [ "$XGOARCH" == "." ] || [ "$XGOARCH" == "amd64" ]; then
488497
echo "Compiling for freebsd/amd64..."
489-
CC=x86_64-pc-freebsd12-gcc HOST=x86_64-pc-freebsd12 PREFIX=/freebsdcross/x86_64-pc-freebsd12 $BUILD_DEPS /deps "${DEPS_ARGS[@]}"
498+
XGOOS="freebsd" XGOARCH="amd64" CC=x86_64-pc-freebsd12-gcc HOST=x86_64-pc-freebsd12 PREFIX=/freebsdcross/x86_64-pc-freebsd12 do_build
490499
export PKG_CONFIG_PATH=/freebsdcross/x86_64-pc-freebsd12/lib/pkgconfig
491500

492501
if [[ "$USEMODULES" == false ]]; then

Diff for: xgo.go

+13
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ var (
5858
dockerImage = flag.String("image", "", "Use custom docker image instead of official distribution")
5959
dockerEnv = flag.String("env", "", "Comma separated custom environments added to docker run -e")
6060
dockerArgs = flag.String("dockerargs", "", "Comma separated arguments added to docker run")
61+
hooksDir = flag.String("hooksdir", "", "Directory with user hook scripts (setup.sh, build.sh)")
6162
forwardSsh = flag.Bool("ssh", false, "Enable ssh agent forwarding")
6263
)
6364

@@ -208,6 +209,18 @@ func main() {
208209
log.Fatalf("Failed to resolve destination path (%s): %v.", *outFolder, err)
209210
}
210211
}
212+
if *hooksDir != "" {
213+
dir, err := filepath.Abs(*hooksDir)
214+
if err != nil {
215+
log.Fatalf("Failed to resolve hooksdir path (%s): %v.", *hooksDir, err)
216+
}
217+
if i, err := os.Stat(dir); err != nil {
218+
log.Fatalf("Failed to resolve hooksdir path (%s): %v.", *hooksDir, err)
219+
} else if !i.IsDir() {
220+
log.Fatalf("Given hooksdir (%s) is not a directory.", *hooksDir)
221+
}
222+
config.DockerArgs = append(config.DockerArgs, "--mount", fmt.Sprintf(`type=bind,source=%s,target=/hooksdir`, dir))
223+
}
211224
// Execute the cross compilation, either in a container or the current system
212225
if !xgoInXgo {
213226
err = compile(image, config, flags, folder)

0 commit comments

Comments
 (0)