-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmmf.sh
91 lines (72 loc) · 2.4 KB
/
mmf.sh
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
#!/bin/bash
# Author: Fabio J. Matos Nieves
# Date Created: 18/7/2021
# Last Modified: 19/7/2021
# Description
# This script creates a c/c++ MakeFile in the current working directory having the main located in a main.cpp file. Note: The the header and implementation files should all be in the same folder.
# Usage
# ./mmf.sh
echo "Hello, this script will now read all the .cpp and .h files within this directory and its subdirectories"
read -rp "Would you like to proceed? [y/n]: " yn
if [ "$yn" != y ]; then
echo "Script Closed"
exit 1
fi
echo "Script Proceeding"
echo ""
readarray -t cpps < <(find "$PWD" -type f -name '*.cpp' | rev | cut -d '/' -f 1 | rev)
readarray -t hs < <(find "$PWD" -type f -name '*.h' | rev | cut -d '/' -f 1 | rev)
echo "These are the .cpp files that will be compiled: "
echo "${cpps[@]}"
echo ""
echo "These are the .h files that will be compiled: "
echo "${hs[@]}"
echo ""
read -rp "Would you like proceed to generating the Makefile? [y/n]: " yn
echo ""
if [ "$yn" != y ]; then
echo "Script Closed"
exit 1
fi
if [ -f Makefile ]; then
rm Makefile
fi
if [ -f cpps.txt ]; then
rm cpps.txt
fi
if [ -f hs.txt ]; then
rm hs.txt
fi
if [ -f outputfile.txt ]; then
rm outputfile.txt
fi
echo "${cpps[@]}" | tr ' ' '\n' | cut -d '.' -f 1 > cpps.txt
echo "${hs[@]}" | tr ' ' '\n' | cut -d '.' -f 1 > hs.txt
while read -r outputfile; do
echo "$outputfile.o" >> outputfile.txt
done < cpps.txt
readarray -t outputarray < outputfile.txt
output=$(echo -en "output: " ; echo -en "${outputarray[@]}" ; echo -en "\n" ; echo -en "\tg++ " ; echo -en "${outputarray[@]}" ; echo -en " -o executable" ; echo "")
echo "$output " >> Makefile
while read -r line; do
if [ "$line" = "$(grep "$line" < hs.txt)" ]; then
output=$(echo -en "\n" ; echo -en "$line.o: $line.cpp $line.h" ; echo "" ; echo -en "\tg++ -c $line.cpp" ; echo -e "\n")
echo "$output" >> Makefile
else
output=$(echo -en "\n" ; echo -en "$line.o: $line.cpp" ; echo "" ; echo -en "\tg++ -c $line.cpp" ; echo "")
echo "$output" >> Makefile
fi
done < cpps.txt
output=$(echo "" ; echo "clean:" ; echo -e "\trm *.o executable")
echo "$output" >> Makefile
cat Makefile
rm cpps.txt outputfile.txt hs.txt
echo ""
echo "Makefile Generated"
read -rp "Would you like to compile the executable? [y/n]: " yn
if [ "$yn" != y ]; then
echo "Script Closed"
exit 0
else
make "-j$(nproc)"
fi