aboutsummaryrefslogtreecommitdiffstats
path: root/dabuild.in
blob: 3e389c55e543b5f3910953a6b25ff9cf0e61eeff (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
#!/bin/sh

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

set -eu

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

# set defaults
: "${DABUILD_DEBUG:=false}"
: "${DABUILD_ARCH:=$(uname -m)}"
: "${DABUILD_DOCKER:=docker}"
: "${DABUILD_VERSION=}"
: "${DABUILD_PACKAGES:=${PWD%/aports/*}/packages}}"
: "${DABUILD_APK_CACHE:=true}"
: "${DABUILD_CCACHE:=false}"
: "${DABUILD_DISTFILES:=false}"
: "${DABUILD_RM:=true}"
: "${DABUILD_ARGS=}"

# reset vars
DABUILD_ENV=

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

## 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"
fi

case "$DABUILD_ARCH" in
	x86|x86_64|aarch64|armhf|armv7);;
	armv8l|arm) DABUILD_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
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
	APORTS_BRANCH=$(git symbolic-ref --short -q HEAD)
	case $APORTS_BRANCH in
		*-stable) DABUILD_VERSION=${APORTS_BRANCH%-stable};;
		*) DABUILD_VERSION=edge;;
	esac
fi

# set packages dir based on alpine release
DABUILD_PACKAGES="$DABUILD_PACKAGES/$DABUILD_VERSION"

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

# 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:-false}"
	local volume="dabuild-$name-$DABUILD_VERSION-$DABUILD_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

# use a bind or named volume for distfiles.
case $DABUILD_DISTFILES in
	*/*) ABUILD_VOLUMES="$ABUILD_VOLUMES \
		-v $DABUILD_DISTFILES:/var/cache/distfiles";;
	true) setup_named_volume distfiles "/var/cache/distfiles" true;;
	*) ;;
esac

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

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

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