dabuild.in 3.44 KB
Newer Older
Richard Mortier's avatar
Richard Mortier committed
1 2
#!/bin/sh

Richard Mortier's avatar
Richard Mortier committed
3 4 5
# Copyright (C) 2019 Richard Mortier <mort@cantab.net>
# Licensed under the MIT License, https://opensource.org/licenses/MIT

Richard Mortier's avatar
Richard Mortier committed
6 7
set -e

8
die () {
Richard Mortier's avatar
Richard Mortier committed
9
  printf >&2 "%s\n" "$@"
10 11 12
  exit 1
}

Carlo Landmeter's avatar
Carlo Landmeter committed
13 14 15 16 17
# source the configuration if available
if [ -f "/etc/dabuild.conf" ]; then
	. /etc/dabuild.conf
fi

Richard Mortier's avatar
Richard Mortier committed
18
## debug
19
if [ "$DABUILD_DEBUG" = "true" ]; then
Richard Mortier's avatar
Richard Mortier committed
20 21
  set -x
  PS4='$LINENO: '
22
  DABUILD_ENV="$DABUILD_ENV -e DABUILD_DEBUG=true"
Richard Mortier's avatar
Richard Mortier committed
23 24
fi

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

32
## allow setting of arch by env variable
33 34 35 36 37 38 39 40 41
if [ "$DABUILD_ARCH" ]; then
  ALPINE_ARCH=$DABUILD_ARCH
else
  ALPINE_ARCH=$(uname -m)
fi

case "$ALPINE_ARCH" in
  x86|x86_64|aarch64|armhf|armv7);;
  armv8l|arm) ALPINE_ARCH=armv7;;
42
  * ) die "Unsupported arch \"$DABUILD_ARCH\" detected." \
43 44
    "Expected one of: x86|x86_64|aarch64|armhf|armv7" \
    "You may force it setting DABUILD_ARCH=\"xxx\" in invocation";;
45 46
esac

47 48 49 50 51 52 53 54
## allow setting of `docker` command by env variable
[ ! "$DABUILD_DOCKER" ] && DABUILD_DOCKER=docker
case "$DABUILD_DOCKER" in
  podman|docker ) ;;
  * ) die "Unsupported docker CLI replacement \"$DABUILD_DOCKER\" detected." \
          "Expected one of: docker|podman"
      ;;
esac
55
_DOCKER=$DABUILD_DOCKER
56

57
## use branch to figure out most appropriate alpine version
58
if [ "$DABUILD_VERSION" ]; then
59
  ALPINE_RELEASE=$DABUILD_VERSION
60 61 62 63
else
  APORTS_BRANCH=$(git status | head -1)
  APORTS_BRANCH="${APORTS_BRANCH##*[ /]}"
  case $APORTS_BRANCH in
64 65 66
    [[:digit:]].[[:digit:]]-stable)
      ALPINE_RELEASE=${APORTS_BRANCH%-stable};;
    *) ALPINE_RELEASE=edge;;
67 68 69
  esac
fi

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

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

80
## setup volumes; use named volumes as cache if desired
81
ABUILD_VOLUMES="-v ${PWD%/aports/*}/aports:/home/builder/aports \
Carlo Landmeter's avatar
Carlo Landmeter committed
82 83 84
  -v ${ABUILD_PACKAGES}:/home/builder/packages"

if [ -f "/etc/abuild.conf" ]; then
85
  ABUILD_VOLUMES="$ABUILD_VOLUMES -v /etc/abuild.conf:/etc/abuild.conf:ro"
Carlo Landmeter's avatar
Carlo Landmeter committed
86 87 88 89
fi

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

92 93 94 95 96
# pass over gitconfig for abuild-keygen
if [ -f "$HOME/.gitconfig" ]; then
  ABUILD_VOLUMES="$ABUILD_VOLUMES -v $HOME/.gitconfig:/home/builder/.gitconfig"
fi

97 98
setup_named_volume() {
	local name=$1 dest=$2 single=$3
99
	local volume="dabuild-$name-$ALPINE_RELEASE-$ALPINE_ARCH"
100 101 102 103 104 105
	[ "$single" = "true" ] && volume="dabuild-$name"
	ABUILD_VOLUMES="$ABUILD_VOLUMES -v $volume:$dest"
}

if [ "$DABUILD_APK_CACHE" = "true" ]; then
	setup_named_volume apkcache "/etc/apk/cache"
106
fi
Richard Mortier's avatar
Richard Mortier committed
107

108 109 110 111 112 113 114
if [ "$DABUILD_CCACHE" = "true" ]; then
	setup_named_volume ccache "/home/builder/.ccache"
	DABUILD_ENV="$DABUILD_ENV -e DABUILD_CCACHE=true"
fi

setup_named_volume config "/home/builder/.abuild" true

115 116 117 118 119
ABUILD_RM="--rm"
if [ "$DABUILD_RM" = "false" ]; then
  ABUILD_RM=""
fi

120
## go!
121
$_DOCKER run --tty --interactive \
122 123
       $ABUILD_RM \
       $ABUILD_VOLUMES \
124 125
       $DABUILD_ENV \
	   $DABUILD_ARGS \
126
       --workdir /home/builder/aports/${PWD#*/aports/} \
127
       %%ABUILD_IMAGE%%:$ALPINE_RELEASE-$ALPINE_ARCH "$@"