live-bootstrap/sysa/helpers.sh

128 lines
3.1 KiB
Bash
Raw Normal View History

2021-01-29 01:31:56 +00:00
#!/bin/bash -e
# SPDX-FileCopyrightText: 2021 Andrius Štikonas <andrius@stikonas.eu>
# SPDX-License-Identifier: GPL-3.0-or-later
export PATH=/after/bin
# Common build steps
# Build function provides a few common stages with default implementation
# that can be overridden on per package basis in the build script.
# build takes two arguments:
# 1) name-version of the package
# 2) optionally specify build script. Default is name-version.sh
build () {
pkg=$1
script_name=${2:-${pkg}.sh}
cd "$pkg" || (echo "Cannot cd into ${pkg}!"; kill $$)
2021-01-28 18:53:44 +00:00
echo "${pkg}: beginning build using script ${script_name}"
2021-01-29 01:31:56 +00:00
base_dir="${PWD}"
patch_dir="${base_dir}/patches"
mk_dir="${base_dir}/mk"
2021-01-28 18:53:44 +00:00
files_dir="${base_dir}/files"
2021-01-29 01:31:56 +00:00
rm -rf "build"
mkdir -p "build"
cd "build"
build_script="${base_dir}/${script_name}"
if test -e "${build_script}"; then
# shellcheck source=/dev/null
. "${build_script}"
fi
2021-02-03 21:30:02 +00:00
echo "${pkg}: unpacking source."
call src_unpack
2021-01-29 01:31:56 +00:00
cd "${pkg}" || (echo "Cannot cd into build/${pkg}!"; kill $$)
echo "${pkg}: preparing source."
call src_prepare
echo "${pkg}: configuring source."
call src_configure
echo "${pkg}: compiling source."
call src_compile
echo "${pkg}: installing."
call src_install
cd ../..
echo "${pkg}: build successful"
cd ..
unset -f src_unpack src_prepare src_configure src_compile src_install
2021-01-29 01:31:56 +00:00
}
# Default unpacking function that unpacks a single source tarball.
default_src_unpack() {
src_dir="${base_dir}/src"
for suf in gz bz2 xz; do
source="${src_dir}/${pkg}.tar.${suf}"
if test -e "${source}"; then
case "${suf}" in
gz) xtr="z" ;;
bz2) xtr="j" ;;
xz) xtr="J" ;;
esac
tar "-${xtr}" -xf "${source}"
break
fi
done
}
# Default function to prepare source code.
# It applies all patches from patch_dir (at the moment only -p0 patches are supported).
2021-01-28 18:53:44 +00:00
# Then it copies our custom makefile and any other custom files from files directory.
2021-01-29 01:31:56 +00:00
default_src_prepare() {
if test -d "${patch_dir}"; then
for p in "${patch_dir}"/*.patch; do
patch -Np0 < "${p}"
done
fi
makefile="${mk_dir}/main.mk"
if test -e "${makefile}"; then
cp "${makefile}" Makefile
fi
2021-01-28 18:53:44 +00:00
if test -d "${files_dir}"; then
cp "${files_dir}"/* "${PWD}/"
fi
2021-01-29 01:31:56 +00:00
}
# Default function for configuring source.
default_src_configure() {
:
}
# Default function for compiling source. It simply runs make without any parameters.
default_src_compile() {
2021-02-07 15:39:42 +00:00
make -f Makefile
2021-01-29 01:31:56 +00:00
}
# Default installing function. PREFIX should be set by run.sh script.
# Note that upstream makefiles might ignore PREFIX and have to be configured in configure stage.
default_src_install() {
2021-02-07 15:39:42 +00:00
make -f Makefile install PREFIX="${PREFIX}"
2021-01-29 01:31:56 +00:00
}
# Check if bash function exists
fn_exists() {
test "$(type -t "$1")" == 'function'
}
# Call package specific function or default implementation.
call() {
if fn_exists "$1"; then
$1
else
default_"${1}"
fi
}