2018-06-13 20:12:52 +02:00
|
|
|
#!/usr/bin/env bash
|
2015-08-18 17:46:18 +02:00
|
|
|
|
2016-09-21 22:29:00 +02:00
|
|
|
DEFAULT_DOCKCROSS_IMAGE=dockcross/base # DO NOT MOVE THIS LINE (see entrypoint.sh)
|
2016-07-04 06:10:36 +02:00
|
|
|
|
2015-08-18 17:46:18 +02:00
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
# Helpers
|
|
|
|
#
|
|
|
|
err() {
|
2021-08-03 01:03:00 +02:00
|
|
|
echo -e >&2 "ERROR: $*\n"
|
2015-08-18 17:46:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
die() {
|
2021-08-03 01:03:00 +02:00
|
|
|
err "$*"
|
2015-08-18 17:46:18 +02:00
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
has() {
|
|
|
|
# eg. has command update
|
|
|
|
local kind=$1
|
|
|
|
local name=$2
|
|
|
|
|
|
|
|
type -t $kind:$name | grep -q function
|
|
|
|
}
|
|
|
|
|
2020-06-25 02:05:12 +02:00
|
|
|
# If OCI_EXE is not already set, search for a container executor (OCI stands for "Open Container Initiative")
|
|
|
|
if [ -z "$OCI_EXE" ]; then
|
2022-05-16 15:08:20 +02:00
|
|
|
if which podman >/dev/null 2>/dev/null; then
|
2020-06-25 02:05:12 +02:00
|
|
|
OCI_EXE=podman
|
2022-05-16 15:08:20 +02:00
|
|
|
elif which docker >/dev/null 2>/dev/null; then
|
|
|
|
OCI_EXE=docker
|
2020-06-25 02:05:12 +02:00
|
|
|
else
|
|
|
|
die "Cannot find a container executor. Search for docker and podman."
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2015-08-18 17:46:18 +02:00
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
# Command handlers
|
|
|
|
#
|
|
|
|
command:update-image() {
|
2020-06-25 02:05:12 +02:00
|
|
|
$OCI_EXE pull $FINAL_IMAGE
|
2015-08-18 17:46:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
help:update-image() {
|
2021-08-08 20:45:36 +02:00
|
|
|
echo "Pull the latest $FINAL_IMAGE ."
|
2015-08-18 17:46:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
command:update-script() {
|
2020-06-25 02:05:12 +02:00
|
|
|
if cmp -s <( $OCI_EXE run --rm $FINAL_IMAGE ) $0; then
|
2021-08-08 20:45:36 +02:00
|
|
|
echo "$0 is up to date"
|
2015-08-18 17:46:18 +02:00
|
|
|
else
|
2021-08-08 20:45:36 +02:00
|
|
|
echo -n "Updating $0 ... "
|
2020-06-25 02:05:12 +02:00
|
|
|
$OCI_EXE run --rm $FINAL_IMAGE > $0 && echo ok
|
2015-08-18 17:46:18 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2022-03-08 23:03:36 +01:00
|
|
|
help:update-script() {
|
2021-08-08 20:45:36 +02:00
|
|
|
echo "Update $0 from $FINAL_IMAGE ."
|
2015-08-18 17:46:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
command:update() {
|
|
|
|
command:update-image
|
|
|
|
command:update-script
|
|
|
|
}
|
|
|
|
|
|
|
|
help:update() {
|
2021-08-08 20:45:36 +02:00
|
|
|
echo "Pull the latest $FINAL_IMAGE, and then update $0 from that."
|
2015-08-18 17:46:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
command:help() {
|
|
|
|
if [[ $# != 0 ]]; then
|
|
|
|
if ! has command $1; then
|
|
|
|
err \"$1\" is not an dockcross command
|
|
|
|
command:help
|
|
|
|
elif ! has help $1; then
|
|
|
|
err No help found for \"$1\"
|
|
|
|
else
|
|
|
|
help:$1
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
cat >&2 <<ENDHELP
|
2016-07-05 04:27:04 +02:00
|
|
|
Usage: dockcross [options] [--] command [args]
|
2015-08-18 17:46:18 +02:00
|
|
|
|
2016-07-05 04:27:04 +02:00
|
|
|
By default, run the given *command* in an dockcross Docker container.
|
|
|
|
|
|
|
|
The *options* can be one of:
|
|
|
|
|
|
|
|
--args|-a Extra args to the *docker run* command
|
|
|
|
--image|-i Docker cross-compiler image to use
|
|
|
|
--config|-c Bash script to source before running this script
|
|
|
|
|
|
|
|
|
|
|
|
Additionally, there are special update commands:
|
2015-08-18 17:46:18 +02:00
|
|
|
|
|
|
|
update-image
|
|
|
|
update-script
|
|
|
|
update
|
|
|
|
|
2016-07-05 04:27:04 +02:00
|
|
|
For update command help use: $0 help <command>
|
2015-08-18 17:46:18 +02:00
|
|
|
ENDHELP
|
2016-07-05 04:27:04 +02:00
|
|
|
exit 1
|
2015-08-18 17:46:18 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
# Option processing
|
|
|
|
#
|
2016-09-21 22:31:09 +02:00
|
|
|
special_update_command=''
|
2015-08-18 17:46:18 +02:00
|
|
|
while [[ $# != 0 ]]; do
|
|
|
|
case $1 in
|
|
|
|
|
|
|
|
--)
|
2017-10-26 14:26:04 +02:00
|
|
|
shift
|
2015-08-18 17:46:18 +02:00
|
|
|
break
|
|
|
|
;;
|
|
|
|
|
2016-07-05 04:17:22 +02:00
|
|
|
--args|-a)
|
2015-08-18 17:46:18 +02:00
|
|
|
ARG_ARGS="$2"
|
|
|
|
shift 2
|
|
|
|
;;
|
|
|
|
|
2016-07-05 04:17:22 +02:00
|
|
|
--config|-c)
|
2015-08-18 17:46:18 +02:00
|
|
|
ARG_CONFIG="$2"
|
|
|
|
shift 2
|
|
|
|
;;
|
|
|
|
|
2016-07-05 04:17:22 +02:00
|
|
|
--image|-i)
|
2015-08-18 17:46:18 +02:00
|
|
|
ARG_IMAGE="$2"
|
|
|
|
shift 2
|
|
|
|
;;
|
2016-09-21 22:31:09 +02:00
|
|
|
update|update-image|update-script)
|
|
|
|
special_update_command=$1
|
|
|
|
break
|
|
|
|
;;
|
2015-08-18 17:46:18 +02:00
|
|
|
-*)
|
|
|
|
err Unknown option \"$1\"
|
|
|
|
command:help
|
|
|
|
exit
|
|
|
|
;;
|
|
|
|
|
|
|
|
*)
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
# The precedence for options is:
|
|
|
|
# 1. command-line arguments
|
|
|
|
# 2. environment variables
|
|
|
|
# 3. defaults
|
|
|
|
|
|
|
|
# Source the config file if it exists
|
2016-07-04 06:10:36 +02:00
|
|
|
DEFAULT_DOCKCROSS_CONFIG=~/.dockcross
|
|
|
|
FINAL_CONFIG=${ARG_CONFIG-${DOCKCROSS_CONFIG-$DEFAULT_DOCKCROSS_CONFIG}}
|
2015-08-18 17:46:18 +02:00
|
|
|
|
|
|
|
[[ -f "$FINAL_CONFIG" ]] && source "$FINAL_CONFIG"
|
|
|
|
|
|
|
|
# Set the docker image
|
2016-07-04 06:10:36 +02:00
|
|
|
FINAL_IMAGE=${ARG_IMAGE-${DOCKCROSS_IMAGE-$DEFAULT_DOCKCROSS_IMAGE}}
|
2015-08-18 17:46:18 +02:00
|
|
|
|
2016-09-22 02:25:11 +02:00
|
|
|
# Handle special update command
|
|
|
|
if [ "$special_update_command" != "" ]; then
|
|
|
|
case $special_update_command in
|
|
|
|
|
|
|
|
update)
|
|
|
|
command:update
|
|
|
|
exit $?
|
|
|
|
;;
|
|
|
|
|
|
|
|
update-image)
|
|
|
|
command:update-image
|
|
|
|
exit $?
|
|
|
|
;;
|
|
|
|
|
|
|
|
update-script)
|
|
|
|
command:update-script
|
|
|
|
exit $?
|
|
|
|
;;
|
|
|
|
|
|
|
|
esac
|
|
|
|
fi
|
2016-09-21 22:31:09 +02:00
|
|
|
|
2015-08-18 17:46:18 +02:00
|
|
|
# Set the docker run extra args (if any)
|
|
|
|
FINAL_ARGS=${ARG_ARGS-${DOCKCROSS_ARGS}}
|
|
|
|
|
2016-11-17 04:02:55 +01:00
|
|
|
# Bash on Ubuntu on Windows
|
2016-12-05 07:32:14 +01:00
|
|
|
UBUNTU_ON_WINDOWS=$([ -e /proc/version ] && grep -l Microsoft /proc/version || echo "")
|
2016-11-17 04:02:55 +01:00
|
|
|
# MSYS, Git Bash, etc.
|
2016-12-05 07:32:14 +01:00
|
|
|
MSYS=$([ -e /proc/version ] && grep -l MINGW /proc/version || echo "")
|
2021-07-19 14:18:59 +02:00
|
|
|
# CYGWIN
|
|
|
|
CYGWIN=$([ -e /proc/version ] && grep -l CYGWIN /proc/version || echo "")
|
2016-11-17 04:02:55 +01:00
|
|
|
|
2022-05-16 15:21:53 +02:00
|
|
|
if [ -z "$UBUNTU_ON_WINDOWS" -a -z "$MSYS" -a "$OCI_EXE" != "podman" ]; then
|
2018-09-27 18:45:48 +02:00
|
|
|
USER_IDS=(-e BUILDER_UID="$( id -u )" -e BUILDER_GID="$( id -g )" -e BUILDER_USER="$( id -un )" -e BUILDER_GROUP="$( id -gn )")
|
2015-08-18 17:46:18 +02:00
|
|
|
fi
|
|
|
|
|
2016-11-17 04:02:55 +01:00
|
|
|
# Change the PWD when working in Docker on Windows
|
|
|
|
if [ -n "$UBUNTU_ON_WINDOWS" ]; then
|
2018-05-27 10:36:59 +02:00
|
|
|
WSL_ROOT="/mnt/"
|
2018-05-23 16:59:03 +02:00
|
|
|
CFG_FILE=/etc/wsl.conf
|
2018-07-11 08:27:02 +02:00
|
|
|
if [ -f "$CFG_FILE" ]; then
|
2018-07-10 15:55:17 +02:00
|
|
|
CFG_CONTENT=$(cat $CFG_FILE | sed -r '/[^=]+=[^=]+/!d' | sed -r 's/\s+=\s/=/g')
|
|
|
|
eval "$CFG_CONTENT"
|
|
|
|
if [ -n "$root" ]; then
|
|
|
|
WSL_ROOT=$root
|
|
|
|
fi
|
|
|
|
fi
|
2018-01-29 17:20:10 +01:00
|
|
|
HOST_PWD=`pwd -P`
|
2018-05-23 16:59:03 +02:00
|
|
|
HOST_PWD=${HOST_PWD/$WSL_ROOT//}
|
2016-11-17 04:02:55 +01:00
|
|
|
elif [ -n "$MSYS" ]; then
|
|
|
|
HOST_PWD=$PWD
|
|
|
|
HOST_PWD=${HOST_PWD/\//}
|
|
|
|
HOST_PWD=${HOST_PWD/\//:\/}
|
2021-07-19 14:18:59 +02:00
|
|
|
elif [ -n "$CYGWIN" ]; then
|
|
|
|
for f in pwd readlink cygpath ; do
|
|
|
|
test -n "$(type "${f}" )" || { echo >&2 "Missing functionality (${f}) (in cygwin)." ; exit 1 ; } ;
|
|
|
|
done ;
|
|
|
|
HOST_PWD="$( cygpath -w "$( readlink -f "$( pwd ;)" ; )" ; )" ;
|
2016-11-17 04:02:55 +01:00
|
|
|
else
|
|
|
|
HOST_PWD=$PWD
|
2020-05-25 22:54:13 +02:00
|
|
|
[ -L $HOST_PWD ] && HOST_PWD=$(readlink $HOST_PWD)
|
2016-11-17 04:02:55 +01:00
|
|
|
fi
|
|
|
|
|
2017-05-10 05:04:33 +02:00
|
|
|
# Mount Additional Volumes
|
|
|
|
if [ -z "$SSH_DIR" ]; then
|
|
|
|
SSH_DIR="$HOME/.ssh"
|
|
|
|
fi
|
|
|
|
|
|
|
|
HOST_VOLUMES=
|
2019-03-07 17:57:23 +01:00
|
|
|
if [ -e "$SSH_DIR" -a -z "$MSYS" ]; then
|
2021-07-19 14:18:59 +02:00
|
|
|
if test -n "${CYGWIN}" ; then
|
|
|
|
HOST_VOLUMES+="-v $(cygpath -w ${SSH_DIR} ; ):/home/$(id -un)/.ssh" ;
|
|
|
|
else
|
|
|
|
HOST_VOLUMES+="-v $SSH_DIR:/home/$(id -un)/.ssh" ;
|
|
|
|
fi ;
|
2017-05-10 05:04:33 +02:00
|
|
|
fi
|
|
|
|
|
2015-08-18 17:46:18 +02:00
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
# Now, finally, run the command in a container
|
|
|
|
#
|
2019-03-05 23:45:09 +01:00
|
|
|
TTY_ARGS=
|
|
|
|
tty -s && [ -z "$MSYS" ] && TTY_ARGS=-ti
|
2016-10-31 04:46:01 +01:00
|
|
|
CONTAINER_NAME=dockcross_$RANDOM
|
2020-06-25 02:05:12 +02:00
|
|
|
$OCI_EXE run $TTY_ARGS --name $CONTAINER_NAME \
|
2016-11-18 06:19:54 +01:00
|
|
|
-v "$HOST_PWD":/work \
|
2017-05-10 05:04:33 +02:00
|
|
|
$HOST_VOLUMES \
|
2018-09-27 18:45:48 +02:00
|
|
|
"${USER_IDS[@]}" \
|
2015-08-18 17:46:18 +02:00
|
|
|
$FINAL_ARGS \
|
|
|
|
$FINAL_IMAGE "$@"
|
2016-10-27 17:32:22 +02:00
|
|
|
run_exit_code=$?
|
2015-08-18 17:46:18 +02:00
|
|
|
|
2016-11-29 01:50:42 +01:00
|
|
|
# Attempt to delete container
|
2020-06-25 02:05:12 +02:00
|
|
|
rm_output=$($OCI_EXE rm -f $CONTAINER_NAME 2>&1)
|
2016-11-29 01:50:42 +01:00
|
|
|
rm_exit_code=$?
|
|
|
|
if [[ $rm_exit_code != 0 ]]; then
|
|
|
|
if [[ "$CIRCLECI" == "true" ]] && [[ $rm_output == *"Driver btrfs failed to remove"* ]]; then
|
|
|
|
: # Ignore error because of https://circleci.com/docs/docker-btrfs-error/
|
|
|
|
else
|
|
|
|
echo "$rm_output"
|
|
|
|
exit $rm_exit_code
|
|
|
|
fi
|
|
|
|
fi
|
2016-10-27 05:24:25 +02:00
|
|
|
|
2016-10-27 17:32:22 +02:00
|
|
|
exit $run_exit_code
|
|
|
|
|
2015-08-18 17:46:18 +02:00
|
|
|
################################################################################
|
|
|
|
#
|
2016-07-06 03:50:06 +02:00
|
|
|
# This image is not intended to be run manually.
|
2015-08-18 17:46:18 +02:00
|
|
|
#
|
2016-07-06 03:50:06 +02:00
|
|
|
# To create a dockcross helper script for the
|
2016-07-16 05:46:08 +02:00
|
|
|
# dockcross/linux-armv7 image, run:
|
2015-08-18 17:46:18 +02:00
|
|
|
#
|
2016-07-16 05:46:08 +02:00
|
|
|
# docker run --rm dockcross/linux-armv7 > dockcross-linux-armv7
|
2016-07-06 03:50:06 +02:00
|
|
|
# chmod +x dockcross-linux-armv7
|
2015-08-18 17:46:18 +02:00
|
|
|
#
|
2016-07-06 03:50:06 +02:00
|
|
|
# You may then wish to move the dockcross script to your PATH.
|
2015-08-18 17:46:18 +02:00
|
|
|
#
|
|
|
|
################################################################################
|