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

78 lines
1.7 KiB
Plaintext
Raw Normal View History

#!/bin/sh
######################################################################
2020-05-23 15:47:21 +02:00
# @author : swytch
# @file : maker
# @license : GPLv3
# @created : Wednesday May 20, 2020 18:18:53 CEST
#
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-06 03:20:19 +02:00
headers_list="$(ls | grep '.h$')"
sources_list="$(ls | grep '.cp*$')"
modules_list="$(echo "$sources_list" | sed 's/\.cp*//g' | tr '\n' ' ')"
targets_list="$(echo "$sources_list" | sed 's/\.cp*/\.o/g' | tr '\n' ' ')"
get_compiler(){
for source_ in "$sources_list"; do
case "$source_" in
*.cpp) echo "g++" && return;;
*.c) echo "gcc";;
2020-05-06 03:20:19 +02:00
esac
done
}
is_c_project(){
[ -n "$sources_list" ]
2020-05-06 03:20:19 +02:00
}
make_rule(){
if [ "$comp" = "gcc" ]; then
echo "$1.o : $1.c $1.h" >> makefile
2020-05-06 03:20:19 +02:00
echo -e "\t\$(CC) -c $1.c \$(FLAGS)" >> makefile
else
echo "$1.o : $1.cpp $1.h" >> makefile
2020-05-06 03:20:19 +02:00
echo -e "\t\$(CC) -c $1.cpp \$(FLAGS)" >> makefile
fi
echo "" >> makefile
}
if ! is_c_project; then
2020-05-06 03:20:19 +02:00
echo "No C/C++ project in directory. Exiting..."
exit 1
fi
comp=get_compiler
2020-05-06 03:20:19 +02:00
touch makefile
echo "BIN = main" > makefile
echo "TARGETS = $targets_list" >> makefile
echo "" >> makefile
echo "CC = $comp" >> makefile
echo "FLAGS = -Wall -g" >> makefile
echo "" >> makefile
echo "all : \$(BIN)" >> makefile
echo "" >> makefile
echo "\$(BIN) : \$(TARGETS)" >> makefile
echo -e "\t\$(CC) \$(TARGETS) -o \$(BIN) \$(FLAGS)" >> makefile
echo "" >> makefile
for module in $modules_list; do
make_rule "$module"
done
echo "clean :" >> makefile
echo -e "\trm \$(BIN) *.o vgcore.*" >> makefile
sed -i 's/\s$//' makefile
if type "nvim" &> /dev/null; then
nvim makefile
else
vim makefile
fi