-
Notifications
You must be signed in to change notification settings - Fork 0
/
automakefile
executable file
·249 lines (190 loc) · 5.13 KB
/
automakefile
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/bin/bash
##
## EPITECH PROJECT, 2020
## Automakefile
## File description:
## Make file creator script in bash
##
function get_config()
{
local searched_value="${1};"
local len=${#searched_value}
value=$(cat $config | grep ^$searched_value | cut -d ';' -f 2)
}
function check_value()
{
if [[ -z $1 ]]; then
value=$2
fi
}
function fwrite()
{
echo -e "$1\t" >> $2
}
function header()
{
fwrite "##" $1
fwrite "## EPITECH PROJECT, 2020" $1
fwrite "## Makefile" $1
fwrite "## File descritpion:" $1
fwrite "## Makefile generated by Prism's Automakefile" $1
fwrite "##\n" $1
}
###################################
##### Beginning of the script #####
###################################
## Argument check
if [[ $# != 1 ]]
then
echo -e "\e[91mBad argument, \e[94musage : ./automakefile [Config file]";
exit 84
fi
## Check config existence and get Makefile directory
config=$1;
dir=`dirname $1`
if [[ -f $config ]];
then
echo -e "\e[92mConfig file found !";
else
echo -e "\e[91mConfig file given doesn't exist, exiting..."; exit 84
fi
if [[ -f "$dir/Makefile" ]]
then
rm -f "$dir/Makefile"
fi
touch "$dir/Makefile"
## Get Makefile needed values ##
header "$dir/Makefile"
value=
get_config EXEC
check_value value a.out
if [[ -z $value ]]
then
value="a.out"
fi
fwrite "NAME = $value\n" "$dir/Makefile"
value=
get_config CC
if [[ -z $value ]]
then
value="gcc"
fi
fwrite "CC = $value\n" "$dir/Makefile"
value=
fwrite "RM = rm -f\n" "$dir/Makefile"
SOURCES_DIR=`grep '^SOURCES_DIR;' $1 | cut -d";" -f 2`
PROJECT_DIR=`grep '^PROJECT_DIR;' $1 | cut -d";" -f 2`
if [[ -z $PROJECT_DIR ]]; then
echo -e "\e[91mNo project directory found, exiting..."
rm -f "$dir/Makefile"
exit 84
elif [[ $PROJECT_DIR = "." ]]; then
echo -e "\e[91mNo project directory found, exiting..."
rm -f "$dir/Makefile"
exit 84
fi
echo -e "\e[92mProject directory found"
SOURCES=($(grep -E '\.c;' $1 | cut -d ";" -f1))
SOURCES_NBR=${#SOURCES[@]}
if [[ -z $SOURCES ]]; then
echo -e "\e[91mNo source file given, exiting..."
rm -f "$dir/Makefile"
exit 84
fi
echo -e -n "SRCS\t= " >> "$dir/Makefile"
echo -e "\e[92mSources found";
for (( i = 0; i != SOURCES_NBR; i++)); do
echo -e "\t${SOURCES[i]} \\" >> "$dir/Makefile"
done
echo -e "" >> "$dir/Makefile"
value=
echo -e -n "OBJS = $" >> "$dir/Makefile"
echo -e "(SRCS:.c=.o)\n" >> "$dir/Makefile"
fwrite "ZIPNAME = archive\n" "$dir/Makefile"
get_config ZIP
if [[ -z $value ]]
then
value="tar"
fi
fwrite "ZIP = $value\n" "$dir/Makefile"
value=
get_config ZIPFLAGS
if [[ -z $value ]]
then
value="-cvvf"
fi
fwrite "ZIPFLAGS = $value\n" "$dir/Makefile"
value=
get_config UNZIP
if [[ -z $value ]]
then
value="tar"
fi
fwrite "UNZIP = $value\n" "$dir/Makefile"
value=
get_config UNZIPFLAGS
if [[ -z $value ]]
then
value="-xvf"
fi
fwrite "UNZIPFLAGS = $value\n" "$dir/Makefile"
value=
fwrite "BCK_DIR = backup\n" "$dir/Makefile"
CFLAGS=`grep '^CFLAGS;' $1 | cut -d";" -f 2`
fwrite "CFLAGS += $CFLAGS\n" "$dir/Makefile"
## all flag
echo -e -n "all: $" >> "$dir/Makefile"
echo -e "(NAME)\n" >> "$dir/Makefile"
echo -e -n "$" >> "$dir/Makefile"
echo -e -n "(NAME): $" >> "$dir/Makefile"
echo -e -n "(OBJS)\n\t$" >> "$dir/Makefile"
echo -e -n "(CC) $" >> "$dir/Makefile"
echo -e -n "(OBJS) -o $" >> "$dir/Makefile"
echo -e -n "(NAME) $" >> "$dir/Makefile"
echo -e "(CFLAGS)" >> "$dir/Makefile"
## clean flag with temporary files and -O2 also being deleted
fwrite "clean:" "$dir/Makefile"
echo -e -n "\t$" >> "$dir/Makefile"
echo -e -n "(RM) $" >> "$dir/Makefile"
echo -e "(OBJS)" >> "$dir/Makefile"
echo -e "\tfind -name '*.gcno' -delete" >> "$dir/Makefile"
echo -e "\tfind -name '*.gcna' -delete" >> "$dir/Makefile"
echo -e "\tfind -name '*~' -delete" >> "$dir/Makefile"
echo -e "\tfind -name '#*#' -delete\n" >> "$dir/Makefile"
## fclean flag
fwrite "fclean: clean" "$dir/Makefile"
echo -e -n "\t$" >> "$dir/Makefile"
echo -e -n "(RM) $" >> "$dir/Makefile"
echo -e "(NAME)\n" >> "$dir/Makefile"
## re flag
fwrite "re: fclean all\n" "$dir/Makefile"
## .PHONY
fwrite ".PHONY: all clean fclean re archive revert delete\n" "$dir/Makefile"
## Archive flag
fwrite "archive:" "$dir/Makefile"
echo -e -n "\t$" >> "$dir/Makefile"
echo -e -n "(ZIP) $" >> "$dir/Makefile"
echo -e -n "(ZIPFLAGS) $" >> "$dir/Makefile"
echo -e -n "(ZIPNAME) $" >> "$dir/Makefile"
echo -e "(SRCS)" >> "$dir/Makefile"
echo -e -n "\tmv $" >> "$dir/Makefile"
echo -e -n "(ZIPNAME) $" >> "$dir/Makefile"
echo -e "(BCK_DIR)\n" >> "$dir/Makefile"
## revert flag
fwrite "revert:" "$dir/Makefile"
echo -e -n "\trm -f $" >> "$dir/Makefile"
echo -e "(SRCS)" >> "$dir/Makefile"
echo -e -n "\t$" >> "$dir/Makefile"
echo -e -n "(UNZIP) $" >> "$dir/Makefile"
echo -e -n "(UNZIPFLAGS) $" >> "$dir/Makefile"
echo -e -n "(BCK_DIR)/$" >> "$dir/Makefile"
echo -e "(ZIPNAME)\n" >> "$dir/Makefile"
## delete flag
fwrite "delete:" "$dir/Makefile"
echo -e -n "\t$" >> "$dir/Makefile"
echo -e -n "(RM) $" >> "$dir/Makefile"
echo -e "(ZIPNAME)" >> "$dir/Makefile"
echo -e "\e[92mFlags written"
echo -e "\e[92mVersion log created"
touch $PROJECT_DIR/.version
echo -e "\e[33m\e[1mMakefile successfully created !\e[0m";