aboutsummaryrefslogtreecommitdiffstats
path: root/dabuild.in
blob: e3c4326a2584fa7cc97e9ea488373cb62627337f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/bin/sh

# Copyright (C) 2019 Richard Mortier <mort@cantab.net>
# Licensed under the MIT License, https://opensource.org/licenses/MIT

set -e

die () {
  printf >&2 "%s\n" "$@"
  exit 1
}

## debug
if [ "$DABUILD_DEBUG" = "true" ]; then
  set -x
  PS4='$LINENO: '
fi

## check running from within an `aports` tree
if [ "${PWD%*/aports/*}" = "$PWD" ]; then
  die "Error: expecting to be run from within an aports tree!" \
      "Could not find '/aports/' in the current path: $PWD"
  exit 1
fi

## allow setting of arch by env variable
[ ! "$DABUILD_ARCH" ] && DABUILD_ARCH=$(uname -m)
case "$DABUILD_ARCH" in
  x86|x86_64|aarch64|armv6|armv7|armv7l|armv8 ) ;;
  * ) die "Unsupported arch \"$DABUILD_ARCH\" detected." ;;
esac

## use branch to figure out most appropriate alpine version
if [ "$DABUILD_VERSION" ]; then
  ABUILD_VERSION=$DABUILD_VERSION
else
  APORTS_BRANCH=$(git status | head -1)
  APORTS_BRANCH="${APORTS_BRANCH##*[ /]}"
  case $APORTS_BRANCH in
    [[:digit:]].[[:digit:]]-stable )
      ABUILD_VERSION=${APORTS_BRANCH%-stable}
      ;;

    * )
      ABUILD_VERSION=edge
      ;;
  esac
fi

## check $DABUILD_PACKAGES is a writable directory
ABUILD_PACKAGES=${DABUILD_PACKAGES:-${PWD%/aports/*}/packages/${ABUILD_VERSION}}

mkdir -p $ABUILD_PACKAGES
if [ ! \( -d "$ABUILD_PACKAGES" -a -w "$ABUILD_PACKAGES" \) ]; then
  die "Error: invalid or unwritable packages directory specified!" \
      "ABUILD_PACKAGES = '$ABUILD_PACKAGES'" \
      "DABUILD_PACKAGES = '$DABUILD_PACKAGES'"
fi

## setup volumes; use named volumes as cache if desired
ABUILD_VOLUMES="-v ${HOME}/.abuild:/home/builder/.abuild \
  -v ${PWD%/aports/*}/aports:/home/builder/aports \
  -v ${ABUILD_PACKAGES}:/home/builder/packages"

if [ -f "/etc/abuild.conf" ]; then
  ABUILD_VOLUMES="$ABUILD_VOLUMES -v /etc/abuild.conf:/etc/abuild.conf:ro"
fi

if [ -w "/var/cache/distfiles" ]; then
  ABUILD_VOLUMES="$ABUILD_VOLUMES -v /var/cache/distfiles:/var/cache/distfiles"
fi

if [ "$DABUILD_CACHE" = "true" ]; then
  for v in %%ABUILD_VOLUMES%% ; do
    vol=abuild-$ABUILD_VERSION-$DABUILD_ARCH-${v//\//_}
    if [ "$DABUILD_CLEAN" = "true" ]; then
      ## clean the cache if requested
      docker rm -f $(docker ps -qaf "volume=$vol") >/dev/null 2>&1 || true
      docker volume rm $vol >/dev/null
    fi

    if docker volume ls -f 'name=$v' | grep $v >/dev/null ; then
      docker volume create $vol >/dev/null
    fi
    ABUILD_VOLUMES="$ABUILD_VOLUMES -v $vol:/$v"
  done
fi

ABUILD_RM="--rm"
if [ "$DABUILD_RM" = "false" ]; then
  ABUILD_RM=""
fi

## go!
docker run --tty --interactive \
       $ABUILD_RM \
       $ABUILD_VOLUMES \
       -e DABUILD_DEBUG \
       $DABUILD_ARGS \
       --workdir /home/builder/aports/${PWD#*/aports/} \
       %%ABUILD_IMAGE%%:$ABUILD_VERSION-$DABUILD_ARCH "$@"