Add C compiler options

This commit is contained in:
augustin64 2022-06-01 14:40:36 +02:00
parent 0c49729e0e
commit dffaf7a0cf

169
make.sh
View File

@ -1,65 +1,64 @@
#!/bin/bash #!/bin/bash
# C compiler can be defined with the $CC environment variable
FLAGS="-std=c99 -lm -lpthread"
OUT="out" OUT="out"
set -e set -e
if [[ $1 == "build" ]]; then build () {
mkdir -p "$OUT" mkdir -p "$OUT"
[[ $2 ]] || set "$1" "main" [[ $1 ]] || set "main"
if [[ $2 == "main" ]]; then if [[ $1 == "main" ]]; then
echo "Compilation de src/mnist/main.c" echo "Compilation de src/mnist/main.c"
gcc src/mnist/main.c -o "$OUT/main" $FLAGS $CC src/mnist/main.c -o "$OUT/main" $FLAGS
echo "Fait." echo "Fait."
exit 0 return 0
elif [[ $2 == "preview" ]]; then elif [[ $1 == "preview" ]]; then
echo "Compilation de src/mnist/preview.c" echo "Compilation de src/mnist/preview.c"
gcc src/mnist/preview.c -o "$OUT/preview_mnist" $FLAGS $CC src/mnist/preview.c -o "$OUT/preview_mnist" $FLAGS
echo "Fait." echo "Fait."
exit 0 return 0
elif [[ $2 == "test" ]]; then elif [[ $1 == "test" ]]; then
for i in "test/"*".c"; do for i in "test/"*".c"; do
echo "Compilation de $i" echo "Compilation de $i"
gcc "$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
exit 0 return 0
elif [[ $2 == "utils" ]]; then elif [[ $1 == "utils" ]]; then
echo "Compilation de src/mnist/utils.c" echo "Compilation de src/mnist/utils.c"
gcc "src/mnist/utils.c" -o "$OUT/utils" $FLAGS $CC "src/mnist/utils.c" -o "$OUT/utils" $FLAGS
echo "Fait." echo "Fait."
exit 0 return 0
else else
$0 build main build main
$0 build preview build preview
$0 build test build test
$0 build utils build utils
exit 0 return 0
fi
fi fi
}
if [[ $1 == "preview" ]]; then preview () {
if [[ ! $2 ]]; then if [[ ! $1 ]]; then
$0 build preview build preview
exit 0 return 0
elif [[ $2 == "train" ]]; then elif [[ $1 == "train" ]]; then
[[ -f "$OUT/preview_mnist" ]] || $0 build preview [[ -f "$OUT/preview_mnist" ]] || $0 build preview
"$OUT/preview_mnist" data/mnist/train-images-idx3-ubyte data/mnist/train-labels-idx1-ubyte "$OUT/preview_mnist" data/mnist/train-images-idx3-ubyte data/mnist/train-labels-idx1-ubyte
exit 0 return 0
elif [[ $2 == "t10k" ]]; then elif [[ $1 == "t10k" ]]; then
[[ -f "$OUT/preview_mnist" ]] || $0 build preview [[ -f "$OUT/preview_mnist" ]] || $0 build preview
"$OUT/preview_mnist" data/mnist/t10k-images-idx3-ubyte data/mnist/t10k-labels-idx1-ubyte "$OUT/preview_mnist" data/mnist/t10k-images-idx3-ubyte data/mnist/t10k-labels-idx1-ubyte
exit 0 return 0
fi
fi fi
}
if [[ $1 == "test" ]]; then test () {
if [[ ! $2 ]]; then if [[ ! $1 ]]; then
$0 build test build test
exit 0 return 0
elif [[ $2 == "run" ]]; then elif [[ $1 == "run" ]]; then
$0 build test build test
mkdir -p .test-cache mkdir -p .test-cache
for i in "$OUT/test_"*; do for i in "$OUT/test_"*; do
echo "--- $i ---" echo "--- $i ---"
@ -70,63 +69,64 @@ if [[ $1 == "test" ]]; then
chmod +x "$i" chmod +x "$i"
"$i" "$OUT" "$0" "$i" "$OUT" "$0"
done done
exit 0 return 0
fi
fi fi
}
if [[ $1 == "train" ]]; then train () {
[[ -f "$OUT/main" ]] || $0 build main [[ -f "$OUT/main" ]] || build main
[[ $2 ]] || set -- "$1" "train" [[ $1 ]] || set -- "train"
[[ $3 == "-r" || $3 == "--recover" ]] && RECOVER="-r .cache/reseau.bin" [[ $2 == "-r" || $2 == "--recover" ]] && RECOVER="-r .cache/reseau.bin"
mkdir -p .cache mkdir -p .cache
"$OUT/main" train \ "$OUT/main" train \
--images "data/mnist/$2-images-idx3-ubyte" \ --images "data/mnist/$1-images-idx3-ubyte" \
--labels "data/mnist/$2-labels-idx1-ubyte" \ --labels "data/mnist/$1-labels-idx1-ubyte" \
--out ".cache/reseau.bin" \ --out ".cache/reseau.bin" \
$RECOVER $RECOVER
exit 0 return 0
fi }
if [[ $1 == "test_reseau" ]]; then test_reseau () {
[[ -f "$OUT/main" ]] || $0 build main [[ -f "$OUT/main" ]] || build main
[[ $2 ]] || set -- "$1" "train" [[ $1 ]] || set -- "train"
[[ -f ".cache/reseau.bin" ]] || $0 train train [[ -f ".cache/reseau.bin" ]] || train train
"$OUT/main" test \ "$OUT/main" test \
--images "data/mnist/$2-images-idx3-ubyte" \ --images "data/mnist/$1-images-idx3-ubyte" \
--labels "data/mnist/$2-labels-idx1-ubyte" \ --labels "data/mnist/$1-labels-idx1-ubyte" \
--modele ".cache/reseau.bin" --modele ".cache/reseau.bin"
exit 0 return 0
fi }
if [[ $1 == "recognize" ]]; then recognize () {
if [[ $2 ]]; then if [[ $1 ]]; then
[[ $3 ]] || set -- "$1" "$2" "text" [[ $2 ]] || set -- "$2" "text"
[[ -f "$OUT/main" ]] || $0 build main [[ -f "$OUT/main" ]] || build main
[[ -f ".cache/reseau.bin" ]] || $0 train train [[ -f ".cache/reseau.bin" ]] || train train
"$OUT/main" recognize \ "$OUT/main" recognize \
--modele ".cache/reseau.bin" \ --modele ".cache/reseau.bin" \
--in "$2" \ --in "$1" \
--out "$3" --out "$2"
exit 0 return 0
else else
echo "Pas de fichier d'entrée spécifié. Abandon" echo "Pas de fichier d'entrée spécifié. Abandon"
exit 1 return 1
fi
fi fi
}
if [[ $1 == "utils" ]]; then utils () {
[[ -f "$OUT/utils" ]] || $0 build utils [[ -f "$OUT/utils" ]] || build utils
"$OUT/utils" ${*:2} "$OUT/utils" ${*:1}
exit 0 return 0
fi }
if [[ $1 == "webserver" ]]; then webserver () {
[[ -f "$OUT/main" ]] || $0 build main [[ -f "$OUT/main" ]] || build main
[[ -f ".cache/reseau.bin" ]] || $0 train train [[ -f ".cache/reseau.bin" ]] || train train
FLASK_APP="src/webserver/app.py" flask run FLASK_APP="src/webserver/app.py" flask run
exit 0 return 0
fi }
usage () {
echo "Usage:" echo "Usage:"
echo -e "\t$0 build ( main | preview | train | utils | all )\n" echo -e "\t$0 build ( main | preview | train | utils | all )\n"
echo -e "\t$0 train ( train | t10k ) ( -r | --recover )" echo -e "\t$0 train ( train | t10k ) ( -r | --recover )"
@ -139,3 +139,22 @@ echo -e "\t$0 webserver\n"
echo -e "Les fichiers de test sont recompilés à chaque exécution,\nles autres programmes sont compilés automatiquement si manquants\n" echo -e "Les fichiers de test sont recompilés à chaque exécution,\nles autres programmes sont compilés automatiquement si manquants\n"
echo -e "La plupart des options listées ici sont juste faites pour une utilisation plus rapide des commandes fréquentes," echo -e "La plupart des options listées ici sont juste faites pour une utilisation plus rapide des commandes fréquentes,"
echo -e "d'autres options sont uniquement disponibles via les fichiers binaires dans '$OUT'" echo -e "d'autres options sont uniquement disponibles via les fichiers binaires dans '$OUT'"
}
[[ $CC ]] || CC=gcc
if [[ "$CC" == "gcc" ]]; then
FLAGS="-std=c99 -lm -lpthread" # GCC flags
elif [[ "$CC" == "nvcc" ]]; then
FLAGS="" # NVCC flags
else
FLAGS=""
fi
if [[ $1 && $(type "$1") = *"is a shell function"* || $(type "$1") == *"est une fonction"* ]]; then
$1 ${*:2} # Call the function
else
usage
echo $(type "$1")
exit 1
fi;