1
0
mirror of https://github.com/falk-werner/webfuse synced 2024-10-27 20:34:10 +00:00
falk-werner_webfuse/bin/dobuild

241 lines
5.5 KiB
Plaintext
Raw Normal View History

#!/bin/sh
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This file is part of dobuild.
# Copyright (c) 2019 Contributors as noted in the AUTHORS file.
#
# SPDX-License-Identifier: MPL-2.0
set -e
physical_pwd() {
pwd -P 2>/dev/null || pwd
}
try_canonicalize() {
readlink -f "$@" 2>/dev/null || realpath "$@"
}
canonicalize() {
if ! try_canonicalize "$1" 2>/dev/null; then
echo "$(cd "$(dirname "$1")" && physical_pwd)/$(basename "$1")"
fi
}
scriptdir() {
dirname "$(canonicalize "${BASH_SOURCE:-$1}")"
}
template_subst() {
sed \
-e "s!%MARCH%!${MARCH}!g" \
-e "s!%DISTRIB%!${DISTRIB}!g" \
-e "s!%REGISTRY_PREFIX%!${REGISTRY_PREFIX}!g"
}
mktemp_dockerfile() {
mkdir -p "$1"
echo "$1/dobuild_generated.dockerfile"
}
findonly_file() {
workingdir="$1"
name="$2"
filter="$3"
find "$workingdir" -maxdepth 1 -type f -name "$name" \
| grep "^${workingdir}/${filter}" \
| {
firstfound=
lastfound=
while IFS= read -r line; do
firstfound="${firstfound:-$line}"
lastfound="$line"
done
if [ "$firstfound" != "$lastfound" ]; then
printf 'error: more than one match found for %s in %s using filter %s\n' "$name" "$workingdir" "$filter" 1>&2
return 3
fi
echo "$firstfound"
}
}
export LANG=C
export LC_ALL=C
DOBUILDDIR="${DOBUILDDIR:-"$(dirname "$(scriptdir "$0")")"}"
MAKE="${MAKE:-$(command -v make)}"
PATH="$DOBUILDDIR/bin:$PATH"
set -- "$@" --
FILTER="$DOBUILD_FILTER"
WORKINGDIR=
MAKEFILE=
DISTRIB=
MARCH=
IMAGE=
while :; do
case $1 in
-f|--file|--makefile)
if [ "$2" != '--' ]; then
MAKEFILE="${MAKEFILE:-$2}"
set -- "$@" "$1" "$2"
shift
else
printf 'error: "%s" requires a non-empty option argument.\n' "$1" >&2
exit 3
fi
;;
--file=|--makefile=)
printf 'error: "%s" requires a non-empty option argument.\n' "$1" >&2
exit 3
;;
--file=?*|--makefile=?*)
MAKEFILE="${MAKEFILE:-${1#*=}}"
set -- "$@" "$1"
;;
-C|--directory)
if [ "$2" != '--' ]; then
WORKINGDIR="$2"
shift
else
printf 'error: "%s" requires a non-empty option argument.\n' "$1" >&2
exit 3
fi
;;
--directory=)
printf 'error: "%s" requires a non-empty option argument.\n' "$1" >&2
exit 3
;;
--directory=?*)
WORKINGDIR="${1#*=}"
;;
--dockerfile)
if [ "$2" != '--' ]; then
DOCKERFILE="$2"
shift
else
printf 'warning: "%s" ignored, requires a non-empty option argument.\n' "$1" >&2
fi
;;
--dockerfile=)
printf 'warning: "%s" ignored, requires a non-empty option argument.\n' "$1" >&2
;;
--dockerfile=?*|DOCKERFILE=?*)
DOCKERFILE="${1#*=}"
;;
--image)
if [ "$2" != '--' ]; then
IMAGE="$2"
shift
else
printf 'error: "%s" requires a non-empty option argument.\n' "$1" >&2
exit 3
fi
;;
--image=)
printf 'error: "%s" requires a non-empty option argument.\n' "$1" >&2
exit 3
;;
--image=?*|IMAGE=?*)
IMAGE="${1#*=}"
;;
--filter)
if [ "$2" != '--' ]; then
FILTER="$2"
set -- "$@" "FILTER=$FILTER"
shift
else
printf 'warning: "%s" ignored, requires a non-empty option argument.\n' "$1" >&2
fi
;;
--filter=)
printf 'warning: "%s" ignored, requires a non-empty option argument.\n' "$1" >&2
;;
--filter=?*|FILTER=?*)
FILTER="${1#*=}"
set -- "$@" "FILTER=$FILTER"
;;
MARCH=?*)
MARCH="${1#*=}"
;;
DISTRIB=?*)
DISTRIB="${1#*=}"
;;
PROJECTDIR=?*)
PROJECTDIR="${1#*=}"
;;
--)
shift
break
;;
*)
set -- "$@" "$1"
;;
esac
shift
done
if [ -n "$WORKINGDIR" ]; then
cd "$WORKINGDIR"
fi
PROJECTDIR="${DOBUILD_PROJECTDIR:-.}"
OUTDIR="${DOBUILD_OUTDIR:-$PROJECTDIR/.build}"
DISTRIB_SUFFIX="${DISTRIB:+-${DISTRIB}}"
MARCH_DISTRIB="${MARCH:-[^-]*}${DISTRIB_SUFFIX}"
FILTER="${FILTER:-${MARCH_DISTRIB:+${MARCH_DISTRIB}-}[^.]*}"
BUILD_DOCKERFILE="${DOCKERFILE:-$(findonly_file "$PROJECTDIR" '*.dockerfile' "$FILTER")}"
BUILD_DOCKERFILE="${BUILD_DOCKERFILE:-$(find "$PROJECTDIR" -maxdepth 1 -type f -name 'Dockerfile')}" || true
if [ -z "$MAKEFILE" ]; then
if [ -f "$PROJECTDIR/CMakeLists.txt" ]; then
set -- --file="$DOBUILDDIR/examples/cmake-gtest-example/Makefile" "$@"
elif [ -f "$PROJECTDIR/Makefile" ] || [ -f "$PROJECTDIR/makefile" ] || [ -f "$PROJECTDIR/GNUmakefile" ]; then
printf 'error: makefile project support not implemented.\n' >&2
exit 3
else
printf 'error: unknown project kind.\n' >&2
exit 3
fi
fi
set -- "$@" "PROJECTDIR=$PROJECTDIR" "DOBUILDDIR=$DOBUILDDIR" "OUTDIR=$OUTDIR"
if [ -n "$IMAGE" ]; then
BUILD_DOCKERFILE="$(mktemp_dockerfile "$OUTDIR")"
{ \
echo "FROM %REGISTRY_PREFIX%${IMAGE}"; \
echo; \
} | template_subst > "$BUILD_DOCKERFILE"
fi
if [ -n "$BUILD_DOCKERFILE" ]; then
if [ "$BUILD_DOCKERFILE" = '-' ]; then
BUILD_DOCKERFILE="$(mktemp_dockerfile "$OUTDIR")"
template_subst > "$BUILD_DOCKERFILE"
fi
set -- "$@" "DOCKERFILE=$BUILD_DOCKERFILE"
fi
if [ -n "$MARCH" ]; then
set -- "$@" "MARCH=$MARCH"
fi
if [ -n "$DISTRIB" ]; then
set -- "$@" "DISTRIB=$DISTRIB"
fi
exec "$MAKE" "$@"