#!/bin/bash # # Script to build under docker # # Copyright : GNU/AGPLv3 # # Created : 1 AUG 2017 # Created by : AleaJactaEst declare -i REMOVE=0 declare -i IMAGE=0 declare -i BUILD=1 declare -i DEBUG=0 declare JOBS="" declare CMAKEOPTS="" declare DOCKEROPTS="" declare DIRBUILD="" declare CLEANDOCKER=0 declare -i AUTODETEC=1 declare IMAGEDOCKER="builder_khanat_debian_jessie_i686" declare LOCALBUILDDIR="build/$IMAGEDOCKER" declare LOCALSRC="debian/jessie/i686" usage() { cat << EOF usage:$0 [options] script to build under docker Step: 1) clean old build directory 2) create image builder 3) launch build under docker (launch script build-under-docker.sh) 4) remove docker container with state exited options: -h, --help : Show this help -d, --debug : Show debug message -r, --remove : Remove old build repository -f, --force-create-image : force to create docker image (use to build khanat) - default autodetec. -j [N], --jobs[=N] Allow N jobs at once; infinite jobs with no arg. -a OPTS, --add-opts-cmake=OPTS : Adding options on cmake command (before build) -c, --clean-container : remove all container in state Exited -m OPTS, --add-opts-docker=OPTS : Adding options on docker command (when build) Example : cd [root Khanat directory] ./build.sh -c -r ./build.sh -c -r -j 4 -a '-DWITH_SYMBOLS=ON' -a '-DWITH_RYZOM_TOOLS=OFF' -a '-DWITH_NEL_TOOLS=OFF' -m '-m 20g' EOF } function msg_debug() { if [[ $DEBUG -ne 0 ]] then echo "$(date "+%Y/%m/%d %H:%M:%S") DEBUG - $*" fi } function msg_info() { echo "$(date "+%Y/%m/%d %H:%M:%S") INFO - $*" } function msg_error() { echo "$(date "+%Y/%m/%d %H:%M:%S") ERROR - $*" >&2 } # # MAIN # calldir="$(dirname $0)" basedir=$(cd $calldir; pwd) rootdir="$(dirname $(dirname $(dirname $(dirname $(dirname $(dirname ${basedir}))))))" while test $# -gt 0 do case "$1" in -h|--help) usage exit 1 ;; -d|--debug) DEBUG=1 shift ;; -c|--clean-container) CLEANDOCKER=1 shift ;; -r|--remove) REMOVE=1 shift ;; -f|--force-create-image) IMAGE=1 shift ;; -j) shift # search next argument is value or new argument if [[ ${1:0:1} == "-" ]] then JOBS="-j" else JOBS="-j $1" shift fi ;; --jobs*) if [[ ${1#*=} == "" ]] then JOBS="-j" else JOBS="-j ${1#*=}" fi shift ;; -a) shift CMAKEOPTS="$CMAKEOPTS $1" shift ;; --add-opts-cmake=*) CMAKEOPTS="$CMAKEOPTS ${1#*=}" shift ;; -m) shift DOCKEROPTS="$DOCKEROPTS $1" shift ;; --add-opts-docker=*) DOCKEROPTS="$DOCKEROPTS ${1#*=}" shift ;; *) msg_error "options '$1' not recoginze" usage exit 1 ;; esac done function chrashed() { msg_error "BUILD FAILED (code:$?)" exit 2 } trap chrashed EXIT msg_debug "prg: $0" docker -v 1>/dev/null if [[ $? -ne 0 ]] then echo "*** ERROR docker not installed" exit 2 fi if [[ $AUTODETEC -ne 0 ]] then if [[ $(docker images -f "reference=$IMAGEDOCKER" | wc -l) -lt 2 ]] then IMAGE=1 fi fi DIRBUILD="${rootdir}/code/${LOCALBUILDDIR}" msg_debug "calldir: $calldir" msg_debug "basedir: $basedir" msg_debug "rootdir: $rootdir" msg_debug "JOBS: '$JOBS'" msg_debug "CMAKEOPTS: '$CMAKEOPTS'" msg_debug "DOCKEROPTS: '$DOCKEROPTS'" mkdir -p "${DIRBUILD}" if [[ $REMOVE -ne 0 ]] then msg_info "REMOVE OLD BUILD" rm -rf ${DIRBUILD}/* || exit 2 fi touch ${DIRBUILD}/build.log cat << EOF > ${DIRBUILD}/envi.sh #!/bin/bash export MAKEOPTS="$JOBS" export CMAKEOPTS="$CMAKEOPTS" EOF if [[ $IMAGE -ne 0 ]] then msg_info "GENERATE DOCKER IMAGE" cd $rootdir; docker build . -t ${IMAGEDOCKER} \ --file "${basedir}/Dockerfile" || exit 2 fi if [[ $BUILD -ne 0 ]] then msg_info "BUILD" cd $rootdir; docker run -it \ --hostname=builder \ -u "$(id -u $USERNAME):$(id -g $USERNAME)" \ -v $rootdir/dist:/opt/dist \ -v $rootdir/code:/opt/code \ ${DOCKEROPTS} \ ${IMAGEDOCKER} \ /opt/dist/docker/builder/${LOCALSRC}/build-under-docker.sh "/opt/code/${LOCALBUILDDIR}" || exit 2 fi if [[ $CLEANDOCKER -ne 0 ]] then msg_info "CLEAN CONTAINER" docker rm --force `docker ps -qf 'status=exited' -f "ancestor=${IMAGEDOCKER}"` || exit 2 fi trap '' EXIT msg_info "END"