-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_make_cpp.bash
executable file
·116 lines (91 loc) · 2.58 KB
/
generate_make_cpp.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/bin/bash
# Prompt the user for the project name, exercise number, and program name
read -p "Would you like to create a project directory? [y/n]: " project_answer
if [ "$project_answer" == "y" ]; then
read -p "Enter the project name: " project_name
mkdir -p $project_name
cd $project_name
fi
read -p "Would you like to create an exercise directory? [y/n]: " exercise_answer
if [ "$exercise_answer" == "y" ]; then
read -p "Enter the exercise: " exercise
if [ "$project_answer" == "n" ]; then
read -p "Would you like to create the exercise directory in current directory? [y/n]: " answer
if [ "$answer" == "n" ]; then
read -p "Enter the path to the exercise directory: " path
cd $path
fi
fi
mkdir -p $exercise
cd $exercise
fi
read -p "Enter the program name: " program_name
# Prompt the user for the source files
read -p "Enter the source files (space-separated, excluding \`main.cpp\`): " source_files
read -p "Would you like to create header-files associated with the source files? (y/n): " answer
read -p "Would you like to create a main.cpp file (with simple boiler-plate)? [y/n]: " main_answer
read -p "Would you like to include valgrind support in the Makefile? [y/n]: " valgrind_answer
INCLUDES=""
INCLUDE_DEP=""
if [ "$answer" == "y" ]; then
INCLUDES+="INCLUDES := "
for file in $source_files; do
header_file="${file%.cpp}.hpp"
touch "$header_file"
INCLUDES+="$header_file "
done
# INCLUDES+="INCLUDES := \$(SRC:.cpp=.hpp)"
INCLUDE_DEP+="\$(INCLUDES)"
fi
if [ "$main_answer" == "y" ]; then
source_files+=" main.cpp"
fi
# Create the source_files
touch $source_files
# Create the Makefile
cat <<EOF >Makefile
# Program
NAME := $program_name
# Necessities
CXX := c++
CXXFLAGS := -Wall -Wextra -Werror -std=c++98
#Colors:
GREEN = \e[92;5;118m
YELLOW = \e[93;5;226m
GRAY = \e[33;2;37m
RESET = \e[0m
CURSIVE = \e[33;3m
# Targets
SRC := $source_files
$INCLUDES
# Rules
all: \$(NAME)
\$(NAME): \$(SRC) $INCLUDE_DEP
\$(CXX) -o \$@ \$(CXXFLAGS) \$(SRC)
@printf "\$(GREEN)Compilation successful!\$(RESET)\n"
clean:
rm -rf \$(NAME)
@printf "\$(YELLOW)Executable removed.\$(RESET)\n"
fclean: clean
EOF
if [ "$valgrind_answer" == "y" ]; then
cat <<EOF >>Makefile
valgrind: | \$(NAME)
@printf "\$(CURSIVE)Running valgrind...\$(RESET)\n"
valgrind --leak-check=full ./\$(NAME)
EOF
fi
cat <<EOF >>Makefile
re: clean all
.PHONY: all clean fclean re
EOF
printf "Makefile generated successfully.\n"
if [ "$main_answer" == "y" ]; then
cat <<EOF >main.cpp
#include <iostream>
int main(void) {
std::cout << "Hello, world!" << std::endl;
return 0;
}
EOF
fi