diff --git a/Dockerfile b/Dockerfile index b57a83b..58d11fc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -51,3 +51,8 @@ RUN git clone https://github.com/martine/ninja.git && \ ./ninja && \ cp ./ninja /usr/bin/ && \ cd .. && rm -rf ninja + +WORKDIR /build +ENTRYPOINT ["/dockcross/entrypoint.sh"] + +COPY imagefiles/entrypoint.sh imagefiles/dockcross /dockcross/ diff --git a/imagefiles/dockcross b/imagefiles/dockcross new file mode 100755 index 0000000..dfccede --- /dev/null +++ b/imagefiles/dockcross @@ -0,0 +1,188 @@ +#!/bin/bash + +#------------------------------------------------------------------------------ +# Helpers +# +err() { + echo -e >&2 ERROR: $@\\n +} + +die() { + err $@ + exit 1 +} + +has() { + # eg. has command update + local kind=$1 + local name=$2 + + type -t $kind:$name | grep -q function +} + +#------------------------------------------------------------------------------ +# Command handlers +# +command:update-image() { + docker pull $FINAL_IMAGE +} + +help:update-image() { + echo Pull the latest $FINAL_IMAGE . +} + +command:update-script() { + if cmp -s <( docker run $FINAL_IMAGE ) $0; then + echo $0 is up to date + else + echo -n Updating $0 '... ' + docker run $FINAL_IMAGE > $0 && echo ok + fi +} + +help:update-image() { + echo Update $0 from $FINAL_IMAGE . +} + +command:update() { + command:update-image + command:update-script +} + +help:update() { + echo Pull the latest $FINAL_IMAGE, and then update $0 from that. +} + +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 + fi +} + +#------------------------------------------------------------------------------ +# Option processing +# +while [[ $# != 0 ]]; do + case $1 in + + --) + break + ;; + + --args) + ARG_ARGS="$2" + shift 2 + ;; + + --config) + ARG_CONFIG="$2" + shift 2 + ;; + + --image) + ARG_IMAGE="$2" + shift 2 + ;; + + -*) + 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 +DEFAULT_CONFIG=~/.dockcross +FINAL_CONFIG=${ARG_CONFIG-${DOCKCROSS_CONFIG-$DEFAULT_CONFIG}} + +[[ -f "$FINAL_CONFIG" ]] && source "$FINAL_CONFIG" + +# Set the docker image +DEFAULT_IMAGE=thewtex/cross-compiler-base +FINAL_IMAGE=${ARG_IMAGE-${DOCKCROSS_IMAGE-$DEFAULT_IMAGE}} + +# Set the docker run extra args (if any) +FINAL_ARGS=${ARG_ARGS-${DOCKCROSS_ARGS}} + +# If we are not running via boot2docker +if [ -z $DOCKER_HOST ]; then + USER_IDS="-e BUILDER_UID=$( id -u ) -e BUILDER_GID=$( id -g )" +fi + +#------------------------------------------------------------------------------ +# Command-line processing +# +if [[ $# == 0 ]]; then + command:help + exit +fi + +case $1 in + + --) + # Everything after this is the command-line to be executed + shift + ;; + + *) + # If this is a builtin command, execute it, otherwise fall through + if has command $1; then + command:$1 "${@:2}" # array slice skipping first element + exit $? + fi + ;; + +esac + +#------------------------------------------------------------------------------ +# Now, finally, run the command in a container +# +docker run -i -t --rm \ + -v $PWD:/build \ + $USER_IDS \ + $FINAL_ARGS \ + $FINAL_IMAGE "$@" + +################################################################################ +# +# This image is not intended to be run manually. +# +# To install the dockcross helper, run the following commands: +# +# docker run sdt4docker/raspberry-pi-cross-compiler > dockcross +# chmod +x dockcross +# +# You may then wish to move dockcross to somewhere in your path. +# +################################################################################ diff --git a/imagefiles/entrypoint.sh b/imagefiles/entrypoint.sh new file mode 100755 index 0000000..b278367 --- /dev/null +++ b/imagefiles/entrypoint.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +# This is the entrypoint script for the dockerfile. Executed in the +# container at runtime. + +if [[ $# == 0 ]]; then + # Presumably the image has been run directly, so help the user get started. + cat /dockcross/dockcross + exit 0 +fi + +# If we are running docker natively, we want to create a user in the container +# with the same UID and GID as the user on the host machine, so that any files +# created are owned by that user. Without this they are all owned by root. +# If we are running from boot2docker, this is not necessary. +# The dockcross script sets the BUILDER_UID and BUILDER_GID vars. +if [[ -n $BUILDER_UID ]] && [[ -n $BUILDER_GID ]]; then + + BUILDER_USER=dockcross-user + BUILDER_GROUP=dockcross-group + + groupadd -o -g $BUILDER_GID $BUILDER_GROUP 2> /dev/null + useradd -o -g $BUILDER_GID -u $BUILDER_UID $BUILDER_USER 2> /dev/null + + # Run the command as the specified user/group. + exec chpst -u :$BUILDER_UID:$BUILDER_GID "$@" +else + # Just run the command as root. + exec "$@" +fi