aboutsummaryrefslogtreecommitdiffstats
path: root/dabuild.in
blob: 882684b9654a75549b1f8ef85e55abf2db358ad8 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/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
}

# source the configuration if available
if [ -f "/etc/dabuild.conf" ]; then
	. /etc/dabuild.conf
fi

## debug
if [ "$DABUILD_DEBUG" = "true" ]; then
  set -x
  PS4='$LINENO: '
  DABUILD_ENV="$DABUILD_ENV -e DABUILD_DEBUG=true"
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
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;;
  * ) die "Unsupported arch \"$DABUILD_ARCH\" detected." \
    "Expected one of: x86|x86_64|aarch64|armhf|armv7" \
    "You may force it setting DABUILD_ARCH=\"xxx\" in invocation";;
esac

## 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
_DOCKER=$DABUILD_DOCKER

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

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

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 ${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

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

setup_named_volume() {
	local name=$1 dest=$2 single=$3
	local volume="dabuild-$name-$ALPINE_RELEASE-$ALPINE_ARCH"
	[ "$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"
fi

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

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

## go!
$_DOCKER run --tty --interactive \
       $ABUILD_RM \
       $ABUILD_VOLUMES \
       $DABUILD_ENV \
	   $DABUILD_ARGS \
       --workdir /home/builder/aports/${PWD#*/aports/} \
       %%ABUILD_IMAGE%%:$ALPINE_RELEASE-$ALPINE_ARCH "$@"