This repository has been archived on 2023-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
dotfiles/.local/bin/maker
swytch a94e6be43c fix: change echo commands to printf commands
`printf` behaviour is more predictable then `echo` because bash is just
overdoing things
2020-06-20 11:39:37 +02:00

78 lines
1.8 KiB
Bash
Executable File

#!/bin/sh
######################################################################
# @author : swytch
# @file : maker
# @license : GPLv3
# @created : Wednesday May 20, 2020 18:18:53 CEST
#
# @description : create a makefile for current project (if c/c++)
# open it with (neo)vim
######################################################################
headers_list="$(ls | grep '.h$')"
sources_list="$(ls | grep '.cp*$')"
modules_list="$(printf "\n$sources_list" | sed 's/\.cp*//g' | tr '\n' ' ')"
targets_list="$(printf "\n$sources_list" | sed 's/\.cp*/\.o/g' | tr '\n' ' ')"
get_compiler(){
for source_ in "$sources_list"; do
case "$source_" in
*.cpp) printf "\ng++" && return;;
*.c) printf "\ngcc";;
esac
done
}
is_c_project(){
[ -n "$sources_list" ]
}
make_rule(){
if [ "$comp" = "gcc" ]; then
printf "\n$1.o : $1.c $1.h" >> makefile
echo -e "\t\$(CC) -c $1.c \$(FLAGS)" >> makefile
else
printf "\n$1.o : $1.cpp $1.h" >> makefile
echo -e "\t\$(CC) -c $1.cpp \$(FLAGS)" >> makefile
fi
printf "\n" >> makefile
}
if ! is_c_project; then
printf "\nNo C/C++ project in directory. Exiting..."
exit 1
fi
comp=get_compiler
touch makefile
printf "\nBIN = main" > makefile
printf "\nTARGETS = $targets_list" >> makefile
printf "\n" >> makefile
printf "\nCC = $comp" >> makefile
printf "\nFLAGS = -Wall -g" >> makefile
printf "\n" >> makefile
printf "\nall : \$(BIN)" >> makefile
printf "\n" >> makefile
printf "\n\$(BIN) : \$(TARGETS)" >> makefile
echo -e "\t\$(CC) \$(TARGETS) -o \$(BIN) \$(FLAGS)" >> makefile
printf "\n" >> makefile
for module in $modules_list; do
make_rule "$module"
done
printf "\nclean :" >> makefile
echo -e "\trm \$(BIN) *.o vgcore.*" >> makefile
sed -i 's/\s$//' makefile
if command -v nvim > /dev/null 2>&1; then
nvim makefile
else
vim makefile
fi