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

158
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")
echo "Compilation de src/mnist/utils.c";
$CC "src/mnist/utils.c" -o "$OUT/mnist_utils" $FLAGS;
echo "Fait.";
return 0;;
"mnist")
build mnist-main;
build mnist-preview;
build mnist-utils;;
"cnn-main")
echo "Compilation de src/cnn/main.c";
$CC "src/cnn/main.c" -o "$OUT/cnn_main" $FLAGS;
echo "Fait.";;
"cnn")
build cnn-main;;
"test")
rm "$OUT/test_"* || true;
for i in "test/"*".c"; do for i in "test/"*".c"; do
echo "Compilation de $i" echo "Compilation de $i";
$CC "$i" -o "$OUT/test_$(echo $i | awk -F. '{print $1}' | awk -F/ '{print $NF}')" $FLAGS $CC "$i" -o "$OUT/test_$(echo $i | awk -F. '{print $1}' | awk -F/ '{print $NF}')" $FLAGS;
echo "Fait." echo "Fait.";
done done;
if ! command -v nvcc &> /dev/null; then if ! command -v nvcc &> /dev/null; then
echo "Tests CUDA évités" echo "Tests CUDA évités";
elif [[ $SKIP_CUDA == 1 ]]; then elif [[ $SKIP_CUDA == 1 ]]; then
echo "Tests CUDA évités" echo "Tests CUDA évités";
else else
for i in "test/"*".cu"; do for i in "test/"*".cu"; do
echo "Compilation de $i" echo "Compilation de $i";
nvcc "$i" -o "$OUT/test_$(echo $i | awk -F. '{print $1}' | awk -F/ '{print $NF}')" nvcc "$i" -o "$OUT/test_$(echo $i | awk -F. '{print $1}' | awk -F/ '{print $NF}')";
echo "Fait." echo "Fait.";
done done;
fi fi;
return 0 return 0;;
elif [[ $1 == "mnist-utils" ]]; then *)
echo "Compilation de src/mnist/utils.c" echo -e "\033[1m\033[34m### Building mnist ###\033[0m";
$CC "src/mnist/utils.c" -o "$OUT/mnist_utils" $FLAGS build mnist-main;
echo "Fait." build mnist-preview;
return 0 build mnist-utils;
elif [[ $1 == "mnist" ]]; then 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;
elif [[ $1 == "cnn-main" ]]; then return 0;;
echo "Compilation de src/cnn/main.c" esac
$CC "src/cnn/main.c" -o "$OUT/cnn_main" $FLAGS
echo "Fait."
elif [[ $1 == "cnn" ]]; then
build cnn-main
else
echo -e "\033[1m\033[34m### Building mnist ###\033[0m"
build mnist-main
build mnist-preview
build mnist-utils
echo -e "\033[1m\033[34m### Building cnn ###\033[0m"
build cnn
echo -e "\033[1m\033[34m### Building tests ###\033[0m"
build test
return 0
fi
} }
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
mkdir -p .test-cache
for i in "$OUT/test_"*; do for i in "$OUT/test_"*; do
echo "--- $i ---" echo "--- $i ---";
$i $i;
done done
for i in "test/"*".sh"; do for i in "test/"*".sh"; do
echo "--- $i ---" echo "--- $i ---";
chmod +x "$i" chmod +x "$i";
"$i" "$OUT" "$0" "$i" "$OUT" "$0";
done done;
return 0 return 0;;
fi *)
build test;
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;