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

86 lines
2.0 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env sh
######################################################################
2021-02-14 19:07:45 +01:00
# @author : swytch
# @file : maker
# @license : GPLv3
2021-02-14 19:07:45 +01:00
# @created : Wednesday May 20, 2020 18:18:53 CEST
#
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-06 03:20:19 +02:00
headers_list="$(ls | grep '\.hp*$')"
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' ' ')"
2020-05-06 03:20:19 +02:00
get_compiler(){
for source_ in "$sources_list"; do
case "$source_" in
*.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(){
[ -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
printf "\n" >> makefile
2020-05-06 03:20:19 +02:00
}
if ! is_c_project; then
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
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
printf "\n" >> makefile
2020-05-06 03:20:19 +02:00
for module in $modules_list; do
make_rule "$module"
done
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
if command -v nvim > /dev/null 2>&1; then
nvim makefile
else
vim makefile
fi