|
| 1 | +#!/bin/bash |
| 2 | +DEFAULT_DOCKCROSS_IMAGE=dockcross/windows-arm64 |
| 3 | + |
| 4 | +#------------------------------------------------------------------------------ |
| 5 | +# Helpers |
| 6 | +# |
| 7 | +err() { |
| 8 | + echo -e >&2 ERROR: $@\\n |
| 9 | +} |
| 10 | + |
| 11 | +die() { |
| 12 | + err $@ |
| 13 | + exit 1 |
| 14 | +} |
| 15 | + |
| 16 | +has() { |
| 17 | + # eg. has command update |
| 18 | + local kind=$1 |
| 19 | + local name=$2 |
| 20 | + |
| 21 | + type -t $kind:$name | grep -q function |
| 22 | +} |
| 23 | + |
| 24 | +#------------------------------------------------------------------------------ |
| 25 | +# Command handlers |
| 26 | +# |
| 27 | +command:update-image() { |
| 28 | + docker pull $FINAL_IMAGE |
| 29 | +} |
| 30 | + |
| 31 | +help:update-image() { |
| 32 | + echo Pull the latest $FINAL_IMAGE . |
| 33 | +} |
| 34 | + |
| 35 | +command:update-script() { |
| 36 | + if cmp -s <( docker run $FINAL_IMAGE ) $0; then |
| 37 | + echo $0 is up to date |
| 38 | + else |
| 39 | + echo -n Updating $0 '... ' |
| 40 | + docker run $FINAL_IMAGE > $0 && echo ok |
| 41 | + fi |
| 42 | +} |
| 43 | + |
| 44 | +help:update-image() { |
| 45 | + echo Update $0 from $FINAL_IMAGE . |
| 46 | +} |
| 47 | + |
| 48 | +command:update() { |
| 49 | + command:update-image |
| 50 | + command:update-script |
| 51 | +} |
| 52 | + |
| 53 | +help:update() { |
| 54 | + echo Pull the latest $FINAL_IMAGE, and then update $0 from that. |
| 55 | +} |
| 56 | + |
| 57 | +command:help() { |
| 58 | + if [[ $# != 0 ]]; then |
| 59 | + if ! has command $1; then |
| 60 | + err \"$1\" is not an dockcross command |
| 61 | + command:help |
| 62 | + elif ! has help $1; then |
| 63 | + err No help found for \"$1\" |
| 64 | + else |
| 65 | + help:$1 |
| 66 | + fi |
| 67 | + else |
| 68 | + cat >&2 <<ENDHELP |
| 69 | +Usage: dockcross [options] [--] command [args] |
| 70 | +
|
| 71 | +By default, run the given *command* in an dockcross Docker container. |
| 72 | +
|
| 73 | +The *options* can be one of: |
| 74 | +
|
| 75 | + --args|-a Extra args to the *docker run* command |
| 76 | + --image|-i Docker cross-compiler image to use |
| 77 | + --config|-c Bash script to source before running this script |
| 78 | +
|
| 79 | +
|
| 80 | +Additionally, there are special update commands: |
| 81 | +
|
| 82 | + update-image |
| 83 | + update-script |
| 84 | + update |
| 85 | +
|
| 86 | +For update command help use: $0 help <command> |
| 87 | +ENDHELP |
| 88 | + exit 1 |
| 89 | + fi |
| 90 | +} |
| 91 | + |
| 92 | +#------------------------------------------------------------------------------ |
| 93 | +# Option processing |
| 94 | +# |
| 95 | +special_update_command='' |
| 96 | +while [[ $# != 0 ]]; do |
| 97 | + case $1 in |
| 98 | + |
| 99 | + --) |
| 100 | + break |
| 101 | + ;; |
| 102 | + |
| 103 | + --args|-a) |
| 104 | + ARG_ARGS="$2" |
| 105 | + shift 2 |
| 106 | + ;; |
| 107 | + |
| 108 | + --config|-c) |
| 109 | + ARG_CONFIG="$2" |
| 110 | + shift 2 |
| 111 | + ;; |
| 112 | + |
| 113 | + --image|-i) |
| 114 | + ARG_IMAGE="$2" |
| 115 | + shift 2 |
| 116 | + ;; |
| 117 | + update|update-image|update-script) |
| 118 | + special_update_command=$1 |
| 119 | + break |
| 120 | + ;; |
| 121 | + -*) |
| 122 | + err Unknown option \"$1\" |
| 123 | + command:help |
| 124 | + exit |
| 125 | + ;; |
| 126 | + |
| 127 | + *) |
| 128 | + break |
| 129 | + ;; |
| 130 | + |
| 131 | + esac |
| 132 | +done |
| 133 | + |
| 134 | +# The precedence for options is: |
| 135 | +# 1. command-line arguments |
| 136 | +# 2. environment variables |
| 137 | +# 3. defaults |
| 138 | + |
| 139 | +# Source the config file if it exists |
| 140 | +DEFAULT_DOCKCROSS_CONFIG=~/.dockcross |
| 141 | +FINAL_CONFIG=${ARG_CONFIG-${DOCKCROSS_CONFIG-$DEFAULT_DOCKCROSS_CONFIG}} |
| 142 | + |
| 143 | +[[ -f "$FINAL_CONFIG" ]] && source "$FINAL_CONFIG" |
| 144 | + |
| 145 | +# Set the docker image |
| 146 | +FINAL_IMAGE=${ARG_IMAGE-${DOCKCROSS_IMAGE-$DEFAULT_DOCKCROSS_IMAGE}} |
| 147 | + |
| 148 | +# Handle special update command |
| 149 | +if [ "$special_update_command" != "" ]; then |
| 150 | + case $special_update_command in |
| 151 | + |
| 152 | + update) |
| 153 | + command:update |
| 154 | + exit $? |
| 155 | + ;; |
| 156 | + |
| 157 | + update-image) |
| 158 | + command:update-image |
| 159 | + exit $? |
| 160 | + ;; |
| 161 | + |
| 162 | + update-script) |
| 163 | + command:update-script |
| 164 | + exit $? |
| 165 | + ;; |
| 166 | + |
| 167 | + esac |
| 168 | +fi |
| 169 | + |
| 170 | +# Set the docker run extra args (if any) |
| 171 | +FINAL_ARGS=${ARG_ARGS-${DOCKCROSS_ARGS}} |
| 172 | + |
| 173 | +# If we are not running via boot2docker |
| 174 | +if [ -z $DOCKER_HOST ]; then |
| 175 | + USER_IDS="-e BUILDER_UID=$( id -u ) -e BUILDER_GID=$( id -g ) -e BUILDER_USER=$( id -un ) -e BUILDER_GROUP=$( id -gn )" |
| 176 | +fi |
| 177 | + |
| 178 | +#------------------------------------------------------------------------------ |
| 179 | +# Now, finally, run the command in a container |
| 180 | +# |
| 181 | +docker run --rm \ |
| 182 | + -v $PWD:/work \ |
| 183 | + $USER_IDS \ |
| 184 | + $FINAL_ARGS \ |
| 185 | + $FINAL_IMAGE "$@" |
| 186 | + |
| 187 | +################################################################################ |
| 188 | +# |
| 189 | +# This image is not intended to be run manually. |
| 190 | +# |
| 191 | +# To create a dockcross helper script for the |
| 192 | +# dockcross/linux-armv7 image, run: |
| 193 | +# |
| 194 | +# docker run --rm dockcross/linux-armv7 > dockcross-linux-armv7 |
| 195 | +# chmod +x dockcross-linux-armv7 |
| 196 | +# |
| 197 | +# You may then wish to move the dockcross script to your PATH. |
| 198 | +# |
| 199 | +################################################################################ |
0 commit comments