-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.sh
executable file
·330 lines (289 loc) · 7.29 KB
/
compile.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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/bin/bash
#
# Copyright (C) 2014 Michael Cummins
# License: GNUv2
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
shopt -s expand_aliases
alias gcc='gcc -w'
usage()
{
cat << EOF
usage: $0 options
This script is used when you execute a run executable
OPTIONS:
-h, --help Displays this help
-b Create a new application (compile with default commands)
-d Delete executables and libraries
-a [ <name> ] Compile main application
-e [ file1 [ file2 ... ] ] Compile executables (not main application)
-f [ file1 [ file2 ... ] ] Compile functions
-l [ file1 [ file2 ... ] ] Compile dynamic libraries
-r [ <name> ] Runs the specified program after setup
-w Show warnings from GCC
EOF
}
function contains()
{
local haystack=$1
local neddle=$2
for i in $haystack; do
if [ "$i" == "$neddle" ]; then
echo "1"
return 1
fi
done
echo "0"
return 0
}
FILE_EXTENSION=
LIB_FOLDER=
LIB_EXTENSION=
if [ "$OS" == "Windows_NT" ]; then
FILE_EXTENSION="exe"
LIB_FOLDER="lib"
LIB_EXTENSION="dll"
else
FILE_EXTENSION="out"
LIB_FOLDER="so"
LIB_EXTENSION="so"
fi
DEFAULT_FUNCTION_NAMES=("libfunctions")
DEFAULT_LIB_NAMES=("sendMessage" "alterStruct" "performAction" "callFunction")
DEFAULT_EXEC_NAMES=("execSQL" "getODBCDrivers" "getODBCDataSources");
while true; do
case $1 in
-h|--help)
usage
exit 1
;;
-a)
COMPILE_APP=1
COMPILE_APP_NAME=
shift
if [[ $1 =~ ^-. ]]; then
COMPILE_APP_NAME=""
elif [ "$1" == "" ]; then
COMPILE_APP_NAME=""
else
COMPILE_APP_NAME="$1"
shift
fi
;;
-b)
NEW_BUILD=1
shift
DELETE_FILES=1
COMPILE_FUNCTION=1
COMPILE_LIB=1
COMPILE_EXEC=1
COMPILE_APP=1
;;
-d)
DELETE_FILES=1
shift
;;
-e)
COMPILE_EXEC=1
COMPILE_EXEC_NAMES=()
while true; do
shift
if [[ $1 =~ ^-. ]]; then
break
elif [ "$1" == "" ]; then
break
else
COMPILE_EXEC_NAMES[${#COMPILE_EXEC_NAMES[@]}]="$1"
fi
done
;;
-f)
COMPILE_FUNCTION=1
COMPILE_FUNCTION_NAMES=()
while true; do
shift
if [[ $1 =~ ^-. ]]; then
break
elif [ "$1" == "" ]; then
break
else
COMPILE_FUNCTION_NAMES[${#COMPILE_FUNCTION_NAMES[@]}]="$1"
fi
done
;;
-l)
COMPILE_LIB=1
COMPILE_LIB_NAMES=()
while true; do
shift
if [[ $1 =~ ^-. ]]; then
break
elif [ "$1" == "" ]; then
break
else
COMPILE_LIB_NAMES[${#COMPILE_LIB_NAMES[@]}]="$1"
fi
done
;;
-r)
RUN_APP=1
RUN_APP_NAME=""
shift
if [[ $1 =~ ^-. ]]; then
RUN_APP_NAME=""
elif [ "$1" == "" ]; then
RUN_APP_NAME=""
else
RUN_APP_NAME="$1"
shift
fi
;;
-w)
SHOW_WARNINGS=1
shift
unalias gcc
;;
-*)
echo "$0: Invalid option $1" >&2
exit 2
;;
*)
break
;;
esac
done
#new build
if [ "$NEW_BUILD" == "1" ]; then
echo "Beginning full compile"
echo ""
fi
#delete files
if [ "$DELETE_FILES" == "1" ]; then
if [ "$NEW_BUILD" == "1" ]; then
echo "Removing old files"
else
echo "Deleting compiled libraries and executables"
fi
rm -rf ./$LIB_FOLDER/*.$LIB_EXTENSION
rm -rf bin/*.$FILE_EXTENSION
rm -f ./*.$FILE_EXTENSION
rm -rf objects/*.o
rm -rf ./tmp
fi
if [ "$COMPILE_FUNCTION" == "1" -a "${#COMPILE_FUNCTION_NAMES[@]}" == "0" ]; then
COMPILE_FUNCTION_NAMES=${DEFAULT_FUNCTION_NAMES[@]}
fi
if [ "$COMPILE_LIB" == "1" -a "${#COMPILE_LIB_NAMES[@]}" == "0" ]; then
COMPILE_LIB_NAMES=${DEFAULT_LIB_NAMES[@]}
fi
if [ "$COMPILE_EXEC" == "1" -a "${#COMPILE_EXEC_NAMES[@]}" == "0" ]; then
COMPILE_EXEC_NAMES=${DEFAULT_EXEC_NAMES[@]}
fi
#compile functions
if [ "$COMPILE_FUNCTION" == "1" ]; then
for i in ${COMPILE_FUNCTION_NAMES[@]}; do
if [ $(contains "`echo ${DEFAULT_LIB_NAMES[@]}`" "$i") == "1" ]; then
echo "Do not compile $i here. (Use ./compile.sh -l $i)"
elif [ $(contains "`echo ${DEFAULT_EXEC_NAMES[@]}`" "$i") == "1" ]; then
echo "Do not compile $i here. (Use ./compile.sh -e $i)"
else
echo "Generating $i.o"
gcc -c -fPIC -o objects/$i.o src/$i.c
fi
done
echo "Generating libfunctions.$LIB_EXTENSION"
gcc -shared -o $LIB_FOLDER/libfunctions.$LIB_EXTENSION objects/*.o
echo "Finished"
echo ""
fi
#compile libraries
if [ "$COMPILE_LIB" == "1" ]; then
echo "Creating tmp directory"
mkdir tmp
for i in ${COMPILE_LIB_NAMES[@]}; do
echo "Generating $i.o"
gcc -c -fPIC -o tmp/$i.o src/$i.c
if [ $(contains "`echo ${DEFAULT_FUNCTION_NAMES[@]}`" "$i") == "1" ]; then
echo "Do not compile $i here. (Use ./compile.sh -f $i)"
elif [ $(contains "`echo ${DEFAULT_EXEC_NAMES[@]}`" "$i") == "1" ]; then
echo "Do not compile $i here. (Use ./compile.sh -e $i)"
else
echo "Generating $i.$LIB_EXTENSION"
gcc -shared -o $LIB_FOLDER/lib$i.$LIB_EXTENSION tmp/$i.o $LIB_FOLDER/libfunctions.$LIB_EXTENSION
fi
done
echo "Cleaning up..."
rm -r tmp
echo "Finished"
echo ""
fi
#compile executables
if [ "$COMPILE_EXEC" == "1" ]; then
for i in ${COMPILE_EXEC_NAMES[@]}; do
if [ $(contains "`echo ${DEFAULT_FUNCTION_NAMES[@]}`" "$i") == "1" ]; then
echo "Do not compile $i here. (Use ./compile.sh -f $i)"
elif [ $(contains "`echo ${DEFAULT_LIB_NAMES[@]}`" "$i") == "1" ]; then
echo "Do not compile $i here. (Use ./compile.sh -l $i)"
else
echo "Generating $i.$FILE_EXTENSION"
if [[ "$i" == *(ODBC|SQL)* ]]; then
if [[ "`find /usr/include -iname 'sql.h'`" =~ "sql.h" ]]; then
INC_LIB="-lodbc32"
else
echo "Can not compile $i because you do not have the required includes (sql.h)"
continue
fi
else
INC_LIB=
fi
gcc -o bin/$i.$FILE_EXTENSION src/$i.c $INC_LIB
fi
done
fi
#compile main application
if [ "$COMPILE_APP" == "1" ]; then
if [ "$COMPILE_APP_NAME" == "" ]; then
echo "Creating websocket.$FILE_EXTENSION"
gcc -rdynamic -o bin/websocket.$FILE_EXTENSION src/websocket.c -L./$LIB_FOLDER -lfunctions -ldl -lpthread
else
echo "Creating $COMPILE_APP_NAME"
gcc -rdynamic -o bin/$COMPILE_APP_NAME src/websocket.c -L./$LIB_FOLDER -lfunctions -ldl -lpthread
fi
gcc -o run.$FILE_EXTENSION src/run.c
echo "App compiled successfully."
fi
#run app
if [ "$RUN_APP" == "1" ]; then
if [ "$OS" == "Windows_NT" ]; then
if [ "$LIBRARY_PATH" == "" ]; then
unset LIBRARY_PATH
export LIBRARY_PATH=$(pwd)/$LIB_FOLDER:$LIBRARY_PATH
fi
export PATH=$ORIGINAL_PATH:$(pwd)/$LIB_FOLDER
else
if [ "$LD_LIBRARY_PATH" == "" ]; then
unset LD_LIBRARY_PATH
export LD_LIBRARY_PATH=$(pwd)/$LIB_FOLDER:$LD_LIBRARY_PATH
fi
fi
if [ "$RUN_APP_NAME" == "" ]; then
RUN_APP_NAME="websocket.$FILE_EXTENSION"
fi
bin/$RUN_APP_NAME
fi
if [ "$SHOW_WARNINGS" == "" ]; then
unalias gcc
fi
complete -W "`ls bin | sed -e 's/\([^\.]*\)\.[A-Za-z0-9]*/\1/' | xargs echo -n`" run
#its all over now!
echo ""
echo "Script is now finished"