kernel/kernel/build
Arthur Beck 9881b01a33
Some checks are pending
Format code / Format the kernel (push) Waiting to run
Change formatting a bit and add formatting forgejo action.
2025-02-22 20:14:27 -06:00

156 lines
No EOL
5 KiB
Bash
Executable file

#!/bin/bash
(
flags=$-
set -o pipefail -o noclobber
HAVE_GETOPT="${HAVE_GETOPT:-true}"
getopt --test > /dev/null && true
if [[ $? -ne 4 && $HAVE_GETOPT ]]; then
if [[ -n "EXIT_WITHOUT_GETOPT" ]]; then
echo '[ERROR] `getopt --test` failed. Exiting.'
exit 1
else
echo '[WARN] `getopt --test` failed. Continuing and ignoring command line flags. (note that $1 will still be used for the target)'
echo '[WARN] (to exit instead of ignoring, set the environment variable `EXIT_WITHOUT_GETOPT` to a non-null value)'
HAVE_GETOPT=false
fi
fi
trap 'popd -0 2>&1 > /dev/null' EXIT
set -o errexit # put down here as getopt returns error code four if it's working, and it probably would work otherwise, but I'm not taking chances
check=false
format=false
if [[ "$HAVE_GETOPT" = "true" ]]; then
LONGOPTS=check,format
OPTIONS=cf
PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "$0" -- "$@") || (
echo '[ERROR] Failed to parse command line arguments. If your getopt is broken, set HAVE_GETOPT=true.'
exit 1
)
eval set -- "$PARSED"
while true; do
case "$1" in
-c|--check)
check=true
shift
;;
-f|--format)
format=true
shift
;;
--)
shift
break
;;
*)
echo "Unknown argument"
exit 1
;;
esac
done
if [[ $check == "true" && $format == "true" ]]; then
echo "[WARN] Both --check and --format were passed."
echo "[WARN] Interpretting as only --format, as format will also check that Aphrokern can compile."
check=false
fi
fi
TEMP_APHRODITE_DIR=$(readlink -e .)
APHRODITE_DIR="${APHRODITE_DIR:-$TEMP_APHRODITE_DIR}"
DIR="${BASH_SOURCE%/*}"
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
. "$DIR/functions"
# -f makes it so it won't error out if the file doesn't exist
rm -f config.aphro.tmp
envsubst < "config.aphro" > "config.aphro.tmp"
# see above about -f
rm -f targets.tmp
envsubst < "targets" > "targets.tmp"
# see above about -f
rm -f /tmp/aphrodite_dir
echo $APHRODITE_DIR > /tmp/aphrodite_dir # we really don't want this to be overwritten
export $(grep -Ev '^#' targets.tmp | xargs)
export $(grep -Ev '^#' config.aphro.tmp | xargs)
APHRODITE_DIR=$(cat /tmp/aphrodite_dir)
get_version
if [[ "$CFG_VERSION" != "$VERSION" ]]; then
echo -n "[WARN] Configuration version \"$CFG_VERSION\" is different then actual version \"$VERSION\""
if [[ "$CONT_WITH_DIFFERENT_VERSION" != "true" ]]; then
echo "; not continuing"
exit 1
fi
echo
fi
function compile_one {
pushd . 2>&1 > /dev/null
cd "$APHRODITE_DIR"
target=$1
real_target=${!target}
target_json=$(basename $real_target)
real_target="$APHRODITE_DIR/$target_json"
if [[ $check = "true" ]]; then
echo "[INFO] Checking target $target(with rust target of $real_target)"
cargo clippy --target "$real_target" --release -Zbuild-std=core,alloc --bin entrypoint_$target
else
echo "[INFO] Compiling target $target(with rust target of $real_target)"
echo "[INFO] Running clippy"
cargo clippy --target "$real_target" --release -Zbuild-std=core,alloc --bin entrypoint_$target
echo "[INFO] Building"
cargo build --target "$real_target" --release -Zbuild-std=core,alloc --bin entrypoint_$target 2>/dev/null
cp "target/$(echo $target_json | sed 's/\.json//')/release/entrypoint_$target" kernel-$target
if [[ "$CONFIG_BUILD_GRUB" = "true" ]]; then
if [[ "$target" = "x86" || "$target" = "mips64" || "$target" = "mipsel" || "$target" = "mipsle" ]]; then
rm -rf grub aphrodite-grub-$target.iso
cp -r ./grub_template ./grub
cp kernel-$target ./grub/boot/aphrodite.kernel
sed -i "s@%{VERSION}@$VERSION@g" ./grub/boot/grub/grub.cfg
grub-mkrescue -o aphrodite-grub-$target.iso grub
cp aphrodite-grub-$target.iso aphrodite-$target.iso
fi
fi
fi
}
if [[ $format = "true" ]]; then
echo "[INFO] Formatting"
pushd . 2>&1 > /dev/null
cd "$APHRODITE_DIR"
cargo fmt --all
exit 0
fi
if [[ $# -ge 1 ]]; then
echo "[INFO] Compiling only target $1"
compile_one $1
exit 0
fi
for target in $TARGETS; do
compile_one $target
done
reset_version_vars
set +$flags
)