-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.py
executable file
·81 lines (69 loc) · 2.51 KB
/
build.py
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
#!/usr/bin/env python2
# INFORMATION:
# This scripts compiles libelf to JavaScript
import os
import sys
import shutil
# Directories
LIBELF_DIR = os.path.abspath("libelf")
# Prepend strings at the beginning of the file
def prepend(path, code):
pathBak = path + ".bak"
if os.path.exists(pathBak):
return
shutil.move(path, pathBak)
fin = open(pathBak, 'r')
fout = open(path, 'w')
fout.write(code)
fout.write(fin.read())
fout.close()
fin.close()
def compileLibelf():
# Patching
prepend(os.path.join(LIBELF_DIR, "lib/version.c"),
"#include <stdlib.h>\n")
# Configuring libelf
os.chdir(LIBELF_DIR)
os.system('export "libelf_cv_int64=long long" && ' +
'export "libelf_cv_int32=int" && ' +
'export "libelf_cv_int16=short" && ' +
'emconfigure ./configure --enable-elf64')
# MinGW (Windows) or Make (Linux/Unix)
if os.name == 'nt':
os.system('mingw32-make')
if os.name == 'posix':
os.system('emmake make')
os.chdir('..')
# Get exported functions
with open(os.path.join(LIBELF_DIR, "lib/libelf.def"), 'r') as f:
key = 'EXPORTS'
contents = f.read()
exports = contents[contents.index(key) + len(key):]
exports = exports.replace('\r\n','\n')
exports = exports.strip().split('\n\t')
exports = map(lambda x: '_' + x, exports)
exports = list(exports)
exports.append("_free")
# Get exported runtime methods
methods = ['_free', '_malloc', 'ccall', 'writeArrayToMemory', 'getValue', 'Pointer_stringify']
# Compile static library to JavaScript
cmd = os.path.expandvars('emcc')
cmd += ' -Os --memory-init-file 0'
cmd += ' libelf/lib/libelf.a'
cmd += ' -s EXPORT_NAME="\'MLibelf\'"'
cmd += ' -s EXPORTED_FUNCTIONS=\"[\''+ '\', \''.join(exports) +'\']\"'
cmd += ' -s EXPORTED_RUNTIME_METHODS=\"[\''+ '\', \''.join(methods) +'\']\"'
cmd += ' -s RESERVED_FUNCTION_POINTERS=256'
cmd += ' -s ALLOW_MEMORY_GROWTH=1'
cmd += ' -s MODULARIZE=1'
cmd += ' -s WASM_ASYNC_COMPILATION=0'
cmd += ' -s ENVIRONMENT="web"'
cmd += ' -s WASM=0'
cmd += ' -o src/libelf.out.js'
os.system(cmd)
if __name__ == "__main__":
if os.name in ['nt', 'posix']:
compileLibelf()
else:
print("Your operating system is not supported by this script:")
print("Please, use Emscripten to compile libelf manually to src/libelf.out.js")