2020-05-20 18:34:34 +02:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
######################################################################
|
2020-05-23 15:47:21 +02:00
|
|
|
# @author : swytch
|
|
|
|
# @file : maker
|
2020-06-25 18:19:40 +02:00
|
|
|
# @license : MIT
|
2020-05-23 15:47:21 +02:00
|
|
|
# @created : Wednesday May 20, 2020 18:18:53 CEST
|
2020-05-20 18:34:34 +02:00
|
|
|
#
|
2020-05-23 15:47:21 +02: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
|
|
|
|
|
|
|
headers_list="$(ls | grep '.h$')"
|
|
|
|
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
|
2020-09-29 00:39:22 +02:00
|
|
|
*.cpp) comp="g++" && return;;
|
|
|
|
*.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
|