-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sh
executable file
·285 lines (234 loc) · 7.56 KB
/
build.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
#!/bin/bash
# this is for linux, should work on macos and mingw also, though I haven't tested it
# in future i might add build scripts for other platforms
#HTML FILE
HTML_FILE="<!doctype html>
<html lang=\"EN-us\">
<head>
<meta charset=\"utf-8\">
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
<title>bruter web example</title>
</head>
<body>
<script src=\"./bruter.js\"></script>
<script>
async function bruter_start()
{
window.Bruter = await Bruter;
window.vm = Bruter.new();
console.log(\"bruter is ready\");
console.log(\"pretty simple right?\");
//alert(vm.eval(\"return 'if you see this message, then it works, this message was returned by the bruter vm we just created'\"));
}
bruter_start();
</script>
</body>
</html>"
# js file
JS_FILE="
// bruter.js
let bruter_loader = async function()
{
const BruterAPI = {};
let _module = await _Bruter();
BruterAPI.destroy = (vm_index) => {
_module._wasm_destroy_vm(vm_index);
};
BruterAPI.eval = (vm_index, code) => {
const lengthBytes = _module.lengthBytesUTF8(code) + 1;
const ptr = _module._malloc(lengthBytes);
_module.stringToUTF8(code, ptr, lengthBytes);
let result = _module._wasm_eval(vm_index, ptr);
_module._free(ptr);
result = _module.UTF8ToString(result);
if (result === 'null')
{
console.log('result is null');
return null;
}
else if(result[0] == '\x01') // number
{
result = parseFloat(result.substring(1));
}
else if(result[0] == '\x02') // string
{
result = result.substring(1);
}
else // integer/pointer/any
{
result = parseInt(result.substring(1));
}
return result;
};
BruterAPI.new = (index) => {
let _vm = { index: index || _module._wasm_new_vm() };
_vm.eval = (code) => BruterAPI.eval(_vm.index, code);
_vm.destroy = () => BruterAPI.destroy(_vm.index);
return _vm;
};
BruterAPI.module = _module;
return BruterAPI;
}
window.Bruter = bruter_loader();"
# usage function
usage() {
echo "usage: $0 [--outpath build] [--debug] [--debug-file] [--cc gcc] [--ino] [--web] [--emcc emcc] [--wasicc wasicc] [--exclude libfile] [--exec main.br]"
exit 1
}
# origin
ORIGIN=$(pwd)
# default values
OUTPATH="build"
DEBUG=0
CC="gcc -Wformat=0"
INO=0
EMCC=""
WASICC=""
EXCLUDE=""
MAIN="src/main.c"
EXEC=""
rm -rf build_tmp build
mkdir build_tmp
rsync -avq --exclude="build_tmp" "./" "build_tmp/"
# or cp -r * build_tmp/
cd build_tmp
# parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
--exec) EXEC="$2"; shift 2 ;;
--outpath) OUTPATH="$2"; shift 2 ;;
--debug) DEBUG=1; shift ;;
--debug-file) DEBUG=1; DEBUG_FILE="$2"; shift 2 ;;
--cc) CC="$2"; shift 2 ;;
--ino) INO=1; shift ;;
--emcc) EMCC="$2"; shift 2 ;;
--web) EMCC="emcc"; shift ;;
--wasicc) WASICC="$2"; shift 2 ;;
--exclude) EXCLUDE="$EXCLUDE $2"; shift 2 ;;
*) echo "unknown option: $1"; usage ;;
esac
done
rm -rf "$OUTPATH" build
mkdir -p "$OUTPATH/lib" "$OUTPATH/include" "$OUTPATH/example"
cp -r ./src "$OUTPATH/src"
cp -r ./lib/* "$OUTPATH/lib/"
cp -r ./include/* "$OUTPATH/include/"
cp -r ./example/* "$OUTPATH/example/"
# verify if a folder called packages exists, if so, copy the contents of each folder inside packages like: packages/name/lib/* to build/lib/, packages/name/include/* to build/include/, packages/name/src/* to build/src/
copy_packages() {
for folder in ./lib/*; do
if [[ -d "$folder" ]]; then
cp -r $folder/lib/* $OUTPATH/lib/
cp -r $folder/include/* $OUTPATH/include/
cp -r $folder/src/* $OUTPATH/src/
fi
done
}
copy_packages
cd $ORIGIN/build_tmp || exit 1
cd "$OUTPATH" || exit 1
rm -rf libbruter.a
if [[ $DEBUG -eq 1 ]]; then
DEBUGARGS='-g'
echo "debug mode enabled"
else
DEBUGARGS=""
fi
echo "compiler: $CC"
if [[ $INO -eq 1 ]]; then
echo "preparing arduino files..."
rm -rf arduino
mkdir -p arduino/bruter/src
cp include/bruter.h arduino/bruter/src/bruter.h
cp src/bruter.c arduino/bruter/src/bruter.c
cp lib/* arduino/bruter/src/
cp src/main.c arduino/bruter/bruter.ino
for file in ./lib/*.c; do
filename="${file##*/}"
filename="${filename%.*}"
sed -i "s/<libraries header>/<libraries header>\\nvoid init_$filename(VirtualMachine* vm);/g" arduino/bruter/src/bruter.h
sed -i "s/<libraries init>/<libraries init>\\ninit_$filename(vm);/g" arduino/bruter/bruter.ino
sed -i "s/<libraries init>/<libraries init>\\ninit_$filename(vm);/g" arduino/bruter/src/*.c
done
fi
if [[ -n $EXCLUDE ]]; then
cd lib || exit 1
rm -f $EXCLUDE
cd ..
fi
if [[ -n $EXEC ]]; then
MAIN="./src/main.c -DBRUTER_EXECUTABLE=1"
cp "../$EXEC" ./src/main.br
xxd -i src/main.br > include/entrypoint.h
fi
if [[ -z "$(ls -A lib)" ]]; then
echo "no libs to compile, building main..."
$CC $MAIN ./src/bruter.c -o bruter -O3 -lm -I./include $DEBUGARGS
else
for file in ./lib/*.c; do
filename="${file##*/}"
filename="${filename%.*}"
echo "compiling $filename"
sed -i "s/<libraries header>/<libraries header>\\nvoid init_$filename(VirtualMachine* vm);/g" include/bruter.h
sed -i "s/<libraries init>/<libraries init>\\ninit_$filename(vm);/g" src/*.c
sed -i "s/<libraries init>/<libraries init>\\ninit_$filename(vm);/g" lib/*.c
done
fi
if [[ -n $EMCC ]]; then
echo "compiling to webassembly using $EMCC..."
mkdir -p web
cd web || exit 1
# create index.html
echo "$HTML_FILE" > index.html
# compile to webassembly
$EMCC ../src/bruter.c \
../lib/*.c \
-o bruter.js \
-s EXPORT_NAME="'_Bruter'" \
-sEXPORTED_FUNCTIONS='["_wasm_new_vm", "_wasm_destroy_vm", "_wasm_eval", "_malloc", "_free"]' \
-sEXPORTED_RUNTIME_METHODS='["UTF8ToString", "stringToUTF8", "lengthBytesUTF8"]' \
-sNO_EXIT_RUNTIME=1 \
-sMODULARIZE=1 \
-O3 \
-lm \
-sALLOW_MEMORY_GROWTH=1 \
-L../lib \
-I../include $DEBUGARGS
echo "$JS_FILE" >> bruter.js
cd ..
fi
if [[ -n $WASICC ]]; then
echo "compile to wasi using $WASICC..."
$WASICC -o bruter.wasm $MAIN ./src/bruter.c ./lib/*.c -O3 -lm -I./include
echo 'wasmtime --dir=. bruter.wasm $@' > run_bruter.sh && chmod +x run_bruter.sh
fi
if [[ -n "$(ls -A lib)" ]]; then
$CC ./src/bruter.c -c -O3 -lm -I./include $DEBUGARGS
$CC ./lib/*.c -c -O3 -lm -I./include $DEBUGARGS
ar rcs lib/libbruter.a *.o
$CC $MAIN lib/*.a -o bruter -O3 -lm $DEBUGARGS
if [[ -n $EXEC ]]; then
mv bruter bruter_out
MAIN="./src/main.c"
$CC $MAIN lib/*.a -o bruter -O3 -lm $DEBUGARGS
fi
fi
if [ -n "$DEBUG_FILE" ]; then
cd ../../build_tmp
valgrind --tool=massif --stacks=yes --detailed-freq=1 --verbose $OUTPATH/bruter ../$DEBUG_FILE
ms_print massif.out.* > $OUTPATH/massif-out.txt
rm -rf massif.out.*
valgrind \
--leak-check=full \
--show-leak-kinds=all \
--track-origins=yes \
--log-file=$OUTPATH/valgrind-out.txt \
--verbose $OUTPATH/bruter ../$DEBUG_FILE
ls
cd $OUTPATH
fi
rm -rf *.o lib/*.o src
cd ../..
mv build_tmp/build ./build
rm -rf build_tmp
echo "done building."