jscripts/build.sh
SileNce5k 3b899a2a30
Add build.sh
A script that builds each C and C++ file into their own executables
2023-12-03 02:03:35 +01:00

68 lines
1.8 KiB
Bash

#!/usr/bin/env bash
CXX=${CXX:-g++}
CC=${CC:-gcc}
CFLAGS=${CFLAGS:-}
CXXFLAGS=${CXXFLAGS:-}
compile_c_files() {
local strict=$1
for file in *.c; do
if [[ -f "$file" ]]; then
filename=$(basename "$file")
extension="${filename##*.}"
filename="${filename%.*}"
executable="c_$filename"
if [[ "$file" -nt "$executable" ]]; then
if $CC $CFLAGS $strict -o "$executable" "$file"; then
if [[ "$OSTYPE" == "msys" ]]; then
executable="$executable.exe"
fi
echo "Compiled $file to $executable"
else
echo -e "\e[31mCompilation failed for $file\e[0m"
fi
else
echo "$file is up to date"
fi
fi
done
}
compile_cpp_files() {
local strict=$1
for file in *.cpp; do
if [[ -f "$file" ]]; then
filename=$(basename "$file")
extension="${filename##*.}"
filename="${filename%.*}"
executable="cpp_$filename"
if [[ "$file" -nt "$executable" ]]; then
if $CXX $CXXFLAGS $strict -o "$executable" "$file"; then
if [[ "$OSTYPE" == "msys" ]]; then
executable="$executable.exe"
fi
echo "Compiled $file to $executable"
else
echo -e "\e[31mCompilation failed for $file\e[0m"
fi
else
echo "$file is up to date"
fi
fi
done
}
if [[ "$1" == "--strict" ]]; then
strict="-pedantic -Werror -Wall -Wextra"
else
strict=""
fi
compile_c_files "$strict"
compile_cpp_files "$strict"