Skip to content

Instantly share code, notes, and snippets.

@momento-potsi
Created May 30, 2024 21:24
Show Gist options
  • Save momento-potsi/bd70f28c21975b274b895cff589152c1 to your computer and use it in GitHub Desktop.
Save momento-potsi/bd70f28c21975b274b895cff589152c1 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Author : Tosin Daudu
red=$(tput setaf 1)
bold=$(tput bold)
reset=$(tput sgr0)
green=$(tput setaf 2)
blue=$(tput setaf 6)
info1=$(date)
info2=$(pwd)
info3=$(uname -n)
echo "${bold}${blue}System Info [$info1] { $info3, $info2 }${reset}"
echo "[ Building Project with GNC Compiler]"
# Get all .c and .h files in the current directory
source_files=$(ls *.c *.h 2>/dev/null)
# Check if there are any source files
if [ -z "$source_files" ]; then
echo "No .c or .h files found in the current directory."
exit 1
fi
gcc $source_files -o exec -Wall -Wpedantic -Wextra -Werror -Wshadow -Wformat=2 -Wconversion -Wunused-parameter
exitcode=$?
backupBool=true
echo "${bold}${blue}[ Script Compilation Finished ]${reset}"
if [ "$1" = 'no-run' ]; then
echo "[ Done ]"
exit
elif [ "$1" = 'no-backup' ]; then
backupBool=false
fi
if [ -d backupsrc ]; then # build history temp storage to revert files
echo "${bold}${blue}[ Backing up files ] ${reset}"
cp main.c backupsrc/"main-$(date '+%Y-%m-%d').c";
else
mkdir backupsrc
echo "${bold}${blue}[ Backing up files ] ${reset}"
cp main.c backupsrc/"main-$(date '+%Y-%m-%d').c";
fi
echo $"${bold}${blue}[ Runnning Target Executable ]${reset} \n"
if [ "$exitcode" = 0 ]; then
time ./exec
exitcode=$?
if [ "$exitcode" = 0 ]; then
echo "${bold}${green}[ Build ran Successfully ]${reset}"
else
echo "${bold}${green}[ Program Runtime Error ]${reset}"
echo $'\a'
fi
else
echo "${bold}${red}[ Compilation Failed ]${reset}"
echo $'\a' # Alert Sound
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment