-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile
executable file
·304 lines (258 loc) · 5.73 KB
/
compile
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/bin/sh
set -e
parser_skip=0
start_time=""
end_time=""
action=""
lib_projectstruct=""
exec_projectstruct=""
_compile_obj(){
extra_flag=""
printf "Compiling $1 ($2)..."
basename=$(echo $(basename $1) | cut -d. -f1)
if [ -f obj/$basename-$2.o ]; then
echo "Already Built."
return 0
else
if [ "$2" = "shared" ]; then
extra_flag="-fPIC"
fi
$CC -c $extra_flag -Iinclude $1 -o obj/$basename-$2.o $CFLAGS
echo "Done."
fi
}
_parse_projectstruct(){
project_type=$(echo $1 | cut -d'|' -f1)
if [ "$project_type" = "lib" ]; then
libname=$(echo "$1" | cut -d'|' -f2)
libsrc=$(echo "$1" | cut -d'|' -f3 | sed 's/,/\ /g')
srcdir=$(echo "$1" | cut -d'|' -f4)
elif [ "$project_type" = "exec" ]; then
progname=$(echo "$1" | cut -d'|' -f2)
srcdir=$(echo "$1" | cut -d'|' -f3)
linkflags=$(echo "$1" | cut -d'|' -f4 | sed 's/,/\ /g')
prog_no_install=$(echo "$1" | cut -d'|' -f5)
fi
if [ "$linkflags" = "''" ]; then
linkflags=""
fi
if [ "$srcdir" = "" ]; then
srcdir="src"
fi
}
_runtime_calc(){
set +e
case $1 in
start)
start_time="$(date +%s)"
printf "Operation started at $(date --date=@$start_time)\n\n"
;;
stop)
end_time="$(date +%s)"
runtime="$(expr $end_time - $start_time)"
hours="$(expr $runtime / 3600)"
minutes=0
printf "Operation took "
if [ $hours -ne 0 ]; then
printf "$hours hours"
runtime="$(expr $runtime - $(expr $hours '*' 3600))"
if [ $runtime -gt 60 ]; then
printf ", "
else
printf " and "
fi
fi
minutes="$(expr $runtime / 60)"
if [ $minutes -ne 0 ]; then
printf "$minutes minutes and "
runtime="$(expr $runtime - $(expr $minutes '*' 60))"
fi
echo "$runtime seconds to complete"
;;
esac
set -e
}
compile_static_lib(){
mkdir obj -p
for i in $libsrc; do
_compile_obj $srcdir/$i.c "static"
if [ "$libobj" = "" ]; then
libobj="obj/$(basename $i)-static.o"
else
libobj="$libobj obj/$(basename $i)-static.o"
fi
done
printf "Creating static library $libname..."
$AR rc lib$libname.a $libobj 2>&1 >/dev/null
libobj=""
echo "Done."
}
compile_shared_lib(){
mkdir obj -p
for i in $libsrc; do
_compile_obj $srcdir/$i.c "shared"
if [ "$libobj" = "" ]; then
libobj="obj/$(basename $i)-shared.o"
else
libobj="$libobj obj/$(basename $i)-shared.o"
fi
done
printf "Creating shared library $libname..."
$CC -shared $libobj -o lib$libname.so $CFLAGS $LDFLAGS
libobj=""
echo "Done."
rm *.o -f
}
compile_prog(){
printf "Compiling program $srcdir/$progname..."
$CC $srcdir/$progname.c -Iinclude -L. $linkflags $CFLAGS -o $progname
echo "Done."
}
install_lib(){
printf "Installing library $libname..."
if [ ! -d "$includedir" ] || [ ! -d "$libdir" ]; then
mkdir -p "$includedir"
mkdir -p "$libdir"
fi
for i in $(ls include | grep ".h"); do
cp include/$i "$includedir"
done
mv lib$libname.* "$libdir"
echo "Done."
}
install_prog(){
if [ "$prog_no_install" != "" ]; then
echo "Skipping $progname"
return
fi
printf "Installing program $progname..."
if [ ! -d "$bindir" ]; then
mkdir -p "$bindir"
fi
mv $progname "$bindir"
echo "Done."
}
clean_lib(){
printf "Cleaning library $libname..."
rm -f lib$libname.*
echo "Done."
}
clean_prog(){
printf "Cleaning program $progname..."
rm -f $progname
echo "Done."
}
build_project(){
if [ "$1" = "" ]; then
internal_action="lib"
else
internal_action="$1"
fi
if [ $internal_action = "lib" ]; then
if [ "$2" = "" ]; then
internal_lib_action="shared-static"
else
internal_lib_action="$2"
fi
if [ $(echo $internal_lib_action | grep "static" -c) -ne 0 ]; then
compile_static_lib
fi
if [ $(echo $internal_lib_action | grep "shared" -c) -ne 0 ]; then
compile_shared_lib
fi
fi
if [ $internal_action = "prog" ]; then
compile_prog
fi
}
install_project(){
if [ "$1" = "" ]; then
internal_action="lib"
else
internal_action="$1"
fi
if [ $internal_action = "lib" ]; then
install_lib
fi
if [ $internal_action = "prog" ]; then
install_prog
fi
}
clean_project(){
if [ "$1" = "" ]; then
internal_action="lib"
else
internal_action="$1"
fi
if [ $internal_action = "lib" ]; then
clean_lib
fi
if [ $internal_action = "prog" ]; then
clean_prog
fi
}
printf "CinnamonWolfy's Generic C Build System v1.00\n\n"
if [ $# -lt 1 ]; then
echo "Error: Not enough arguments. Please run $0 help to see all of the valid options and try again"
exit 1
fi
case $1 in
clean)
action="clean_project"
;;
build)
action="build_project"
;;
install)
action="install_project"
;;
help)
echo "clean Removes any files generated during compilation"
echo "build [action] Compiles the library or the library testcase"
echo "install Installs the compiled library to dest"
exit 0
;;
*)
echo "Error: Unrecognized option. Please run $0 help to see all of the valid options and try again"
exit 1
esac
if [ ! -f ".config" ]; then
echo "Error: You haven't configured the system yet! Please run ./configure --help and try again"
exit 1
fi
. ./.config
echo "Compiler: $CC"
echo "Compiler Flags: $CFLAGS"
printf "Linker Flags: $LDFLAGS\n\n"
_runtime_calc start
for i in $main_project; do
project_type=$(echo $i | cut -d'|' -f1)
if [ "$project_type" = "lib" ]; then
if [ "$lib_projectstruct" = "" ]; then
lib_projectstruct="$i"
else
lib_projectstruct="$lib_projectstruct $i"
fi
elif [ "$project_type" = "exec" ]; then
if [ "$exec_projectstruct" = "" ]; then
exec_projectstruct="$i"
else
exec_projectstruct="$exec_projectstruct $i"
fi
fi
done
if [ "$action" = "clean_project" ]; then
rm -rf obj core*
if [ "$2" = "fully" ]; then
rm -f .config
fi
fi
for i in $lib_projectstruct; do
_parse_projectstruct $i
$action lib $3
done
for i in $exec_projectstruct; do
_parse_projectstruct $i
$action prog
done
_runtime_calc stop