Use case statements instead of abusing of elif

This commit is contained in:
augustin64 2022-09-29 22:34:03 +02:00
parent b12a03c1ba
commit 3fc43ef988

174
make.sh
View File

@ -16,96 +16,101 @@ compile_cuda () {
build () { build () {
mkdir -p "$OUT" mkdir -p "$OUT"
[[ $1 ]] || set "mnist-main" [[ $1 ]] || set "mnist-main"
if [[ $1 == "mnist-main" ]]; then case $1 in
echo "Compilation de src/mnist/main.c" "mnist-main")
$CC src/mnist/main.c -o "$OUT/mnist_main" $FLAGS echo "Compilation de src/mnist/main.c";
echo "Fait." $CC src/mnist/main.c -o "$OUT/mnist_main" $FLAGS;
return 0 echo "Fait.";
elif [[ $1 == "mnist-preview" ]]; then return 0;;
echo "Compilation de src/mnist/preview.c" "mnist-preview")
$CC src/mnist/preview.c -o "$OUT/mnist_preview" $FLAGS echo "Compilation de src/mnist/preview.c";
echo "Fait." $CC src/mnist/preview.c -o "$OUT/mnist_preview" $FLAGS;
return 0 echo "Fait.";
elif [[ $1 == "test" ]]; then return 0;;
rm "$OUT/test_"* || true "mnist-utils")
for i in "test/"*".c"; do echo "Compilation de src/mnist/utils.c";
echo "Compilation de $i" $CC "src/mnist/utils.c" -o "$OUT/mnist_utils" $FLAGS;
$CC "$i" -o "$OUT/test_$(echo $i | awk -F. '{print $1}' | awk -F/ '{print $NF}')" $FLAGS echo "Fait.";
echo "Fait." return 0;;
done "mnist")
if ! command -v nvcc &> /dev/null; then build mnist-main;
echo "Tests CUDA évités" build mnist-preview;
elif [[ $SKIP_CUDA == 1 ]]; then build mnist-utils;;
echo "Tests CUDA évités"
else "cnn-main")
for i in "test/"*".cu"; do echo "Compilation de src/cnn/main.c";
echo "Compilation de $i" $CC "src/cnn/main.c" -o "$OUT/cnn_main" $FLAGS;
nvcc "$i" -o "$OUT/test_$(echo $i | awk -F. '{print $1}' | awk -F/ '{print $NF}')" echo "Fait.";;
echo "Fait." "cnn")
done build cnn-main;;
fi
return 0 "test")
elif [[ $1 == "mnist-utils" ]]; then rm "$OUT/test_"* || true;
echo "Compilation de src/mnist/utils.c" for i in "test/"*".c"; do
$CC "src/mnist/utils.c" -o "$OUT/mnist_utils" $FLAGS echo "Compilation de $i";
echo "Fait." $CC "$i" -o "$OUT/test_$(echo $i | awk -F. '{print $1}' | awk -F/ '{print $NF}')" $FLAGS;
return 0 echo "Fait.";
elif [[ $1 == "mnist" ]]; then done;
build mnist-main if ! command -v nvcc &> /dev/null; then
build mnist-preview echo "Tests CUDA évités";
build mnist-utils elif [[ $SKIP_CUDA == 1 ]]; then
elif [[ $1 == "cnn-main" ]]; then echo "Tests CUDA évités";
echo "Compilation de src/cnn/main.c" else
$CC "src/cnn/main.c" -o "$OUT/cnn_main" $FLAGS for i in "test/"*".cu"; do
echo "Fait." echo "Compilation de $i";
elif [[ $1 == "cnn" ]]; then nvcc "$i" -o "$OUT/test_$(echo $i | awk -F. '{print $1}' | awk -F/ '{print $NF}')";
build cnn-main echo "Fait.";
else done;
echo -e "\033[1m\033[34m### Building mnist ###\033[0m" fi;
build mnist-main return 0;;
build mnist-preview *)
build mnist-utils echo -e "\033[1m\033[34m### Building mnist ###\033[0m";
echo -e "\033[1m\033[34m### Building cnn ###\033[0m" build mnist-main;
build cnn build mnist-preview;
echo -e "\033[1m\033[34m### Building tests ###\033[0m" build mnist-utils;
build test echo -e "\033[1m\033[34m### Building cnn ###\033[0m";
return 0 build cnn;
fi echo -e "\033[1m\033[34m### Building tests ###\033[0m";
build test;
return 0;;
esac
} }
preview () { preview () {
if [[ ! $1 ]]; then case $1 in
build preview "train")
return 0 [[ -f "$OUT/mnist_preview" ]] || $0 build mnist-preview;
elif [[ $1 == "train" ]]; then "$OUT/mnist_preview" data/mnist/train-images-idx3-ubyte data/mnist/train-labels-idx1-ubyte;
[[ -f "$OUT/mnist_preview" ]] || $0 build preview return 0;;
"$OUT/mnist_preview" data/mnist/train-images-idx3-ubyte data/mnist/train-labels-idx1-ubyte "t10k")
return 0 [[ -f "$OUT/mnist_preview" ]] || $0 build mnist-preview;
elif [[ $1 == "t10k" ]]; then "$OUT/mnist_preview" data/mnist/t10k-images-idx3-ubyte data/mnist/t10k-labels-idx1-ubyte;
[[ -f "$OUT/mnist_preview" ]] || $0 build preview return 0;;
"$OUT/mnist_preview" data/mnist/t10k-images-idx3-ubyte data/mnist/t10k-labels-idx1-ubyte *)
return 0 build mnist-preview;
fi return 0;;
esac
} }
test () { test () {
if [[ ! $1 ]]; then case $1 in
build test "run")
return 0 build test;
elif [[ $1 == "run" ]]; then mkdir -p .test-cache;
build test for i in "$OUT/test_"*; do
mkdir -p .test-cache echo "--- $i ---";
for i in "$OUT/test_"*; do $i;
echo "--- $i ---" done
$i for i in "test/"*".sh"; do
done echo "--- $i ---";
for i in "test/"*".sh"; do chmod +x "$i";
echo "--- $i ---" "$i" "$OUT" "$0";
chmod +x "$i" done;
"$i" "$OUT" "$0" return 0;;
done *)
return 0 build test;
fi return 0;;
esac
} }
train () { train () {
@ -197,6 +202,5 @@ if [[ $1 && $(type "$1") = *"is a"*"function"* || $(type "$1") == *"est une fonc
$1 ${*:2} # Call the function $1 ${*:2} # Call the function
else else
usage usage
echo $(type "$1")
exit 1 exit 1
fi; fi;