2021-02-12 23:20:57 +01:00
|
|
|
#!/usr/bin/env sh
|
2020-05-20 18:34:34 +02:00
|
|
|
|
|
|
|
######################################################################
|
2021-02-14 19:07:45 +01:00
|
|
|
# @author : swytch
|
|
|
|
# @file : maker
|
2021-02-15 00:37:34 +01:00
|
|
|
# @license : GPLv3
|
2021-02-14 19:07:45 +01:00
|
|
|
# @created : Wednesday May 20, 2020 18:18:53 CEST
|
2020-05-20 18:34:34 +02:00
|
|
|
#
|
2021-02-14 19:07:45 +01:00
|
|
|
# @description : create a makefile for current project (if c/c++)
|
|
|
|
# open it with (neo)vim
|
2020-05-20 18:34:34 +02:00
|
|
|
######################################################################
|
|
|
|
|
2020-05-06 03:20:19 +02:00
|
|
|
|
2021-11-20 15:44:39 +01:00
|
|
|
headers_list="$(ls | grep '\.hp*$')"
|
|
|
|
sources_list="$(ls | grep '\.cp*$')"
|
2020-05-25 15:04:16 +02:00
|
|
|
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' ' ')"
|
2020-05-06 03:20:19 +02:00
|
|
|
|
|
|
|
get_compiler(){
|
|
|
|
for source_ in "$sources_list"; do
|
|
|
|
case "$source_" in
|
2021-11-20 15:44:39 +01:00
|
|
|
*.cpp) comp="g++";;
|
2020-09-29 00:39:22 +02:00
|
|
|
*.c) comp="gcc";;
|
2020-05-06 03:20:19 +02:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
is_c_project(){
|
2020-05-20 17:03:00 +02:00
|
|
|
[ -n "$sources_list" ]
|
2020-05-06 03:20:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
make_rule(){
|
|
|
|
if [ "$comp" = "gcc" ]; then
|
2020-09-29 00:39:22 +02:00
|
|
|
if [ "$1" = "main" ]; then
|
|
|
|
printf "\n$1.o : $1.c" >> makefile
|
|
|
|
else
|
|
|
|
printf "\n$1.o : $1.c $1.h" >> makefile
|
|
|
|
fi
|
|
|
|
printf "\n\t\$(CC) -c $1.c \$(FLAGS)" >> makefile
|
2020-05-06 03:20:19 +02:00
|
|
|
else
|
2020-09-29 00:39:22 +02:00
|
|
|
if [ "$1" = "main" ]; then
|
|
|
|
printf "\n$1.o : $1.cpp" >> makefile
|
|
|
|
else
|
|
|
|
printf "\n$1.o : $1.cpp $1.hpp" >> makefile
|
|
|
|
fi
|
|
|
|
printf "\n\t\$(CC) -c $1.cpp \$(FLAGS)" >> makefile
|
2020-05-06 03:20:19 +02:00
|
|
|
fi
|
2020-05-25 15:04:16 +02:00
|
|
|
printf "\n" >> makefile
|
2020-05-06 03:20:19 +02:00
|
|
|
}
|
|
|
|
|
2020-05-20 17:03:00 +02:00
|
|
|
if ! is_c_project; then
|
2020-05-25 15:04:16 +02:00
|
|
|
printf "\nNo C/C++ project in directory. Exiting..."
|
2020-05-06 03:20:19 +02:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2020-09-29 00:39:22 +02:00
|
|
|
get_compiler
|
2020-05-06 03:20:19 +02:00
|
|
|
|
|
|
|
touch makefile
|
|
|
|
|
2020-09-29 00:39:22 +02:00
|
|
|
printf "BIN = main" > makefile
|
2020-05-25 03:59:22 +02:00
|
|
|
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
|
2020-09-29 00:39:22 +02:00
|
|
|
printf "\n\t\$(CC) \$(TARGETS) -o \$(BIN) \$(FLAGS)" >> makefile
|
2020-05-25 03:59:22 +02:00
|
|
|
printf "\n" >> makefile
|
2020-05-06 03:20:19 +02:00
|
|
|
|
|
|
|
for module in $modules_list; do
|
|
|
|
make_rule "$module"
|
|
|
|
done
|
|
|
|
|
2020-05-25 03:59:22 +02:00
|
|
|
printf "\nclean :" >> makefile
|
2020-09-29 00:39:22 +02:00
|
|
|
printf "\n\trm \$(BIN) *.o vgcore.*" >> makefile
|
2020-05-06 03:20:19 +02:00
|
|
|
|
|
|
|
sed -i 's/\s$//' makefile
|
2020-05-20 17:03:00 +02:00
|
|
|
|
2020-05-25 03:59:22 +02:00
|
|
|
if command -v nvim > /dev/null 2>&1; then
|
2020-05-20 17:03:00 +02:00
|
|
|
nvim makefile
|
|
|
|
else
|
|
|
|
vim makefile
|
|
|
|
fi
|