aboutsummaryrefslogtreecommitdiff
path: root/emcc
diff options
context:
space:
mode:
Diffstat (limited to 'emcc')
-rwxr-xr-xemcc64
1 files changed, 47 insertions, 17 deletions
diff --git a/emcc b/emcc
index 2a575559..6461dbb7 100755
--- a/emcc
+++ b/emcc
@@ -444,25 +444,17 @@ try:
extra_files_to_link = []
if not LEAVE_INPUTS_RAW:
- # Check if we need to include dlmalloc. Note that we assume a single symbol is enough to know if we have/do not have dlmalloc. If you
+ # Check if we need to include some libraries that we compile. (We implement libc ourselves in js, but
+ # compile a malloc implementation and stdlibc++.)
+ # Note that we assume a single symbol is enough to know if we have/do not have dlmalloc etc. If you
# include just a few symbols but want the rest, this will not work.
- need_dlmalloc = False
- has_dlmalloc = False
- for input_file in input_files:
- symbols = shared.Building.llvm_nm(in_temp(unsuffixed_basename(input_file) + '.o'))
- for malloc_def in ['malloc', 'free', 'calloc', 'memalign', 'realloc', 'valloc', 'pvalloc', 'mallinfo', 'mallopt', 'malloc_trim', 'malloc_stats', 'malloc_usable_size', 'malloc_footprint', 'malloc_max_footprint', 'independent_calloc', 'independent_comalloc', '_Znwj', '_Znaj', '_Znam', '_Znwm']:
- if malloc_def in symbols.undefs:
- need_dlmalloc = True
- if malloc_def in symbols.defs:
- has_dlmalloc = True
- if need_dlmalloc and not has_dlmalloc:
- # We need to build and link dlmalloc in
- if DEBUG: print >> sys.stderr, 'emcc: including dlmalloc'
- Popen([shared.EMCC, shared.path_from_root('src', 'dlmalloc.c'), '-g', '-o', in_temp('dlmalloc.o')], stdout=PIPE, stderr=PIPE).communicate()
- if llvm_opt_level > 0:
- shared.Building.llvm_opt(in_temp('dlmalloc.o'), LLVM_INTERNAL_OPT_LEVEL, safe=llvm_opt_level < 2)
- extra_files_to_link.append(in_temp('dlmalloc.o'))
+ # dlmalloc
+ def create_dlmalloc():
+ print >> sys.stderr, 'emcc: building dlmalloc for cache'
+ Popen([shared.EMCC, shared.path_from_root('src', 'dlmalloc.c'), '-g', '-o', in_temp('dlmalloc.o')], stdout=PIPE, stderr=PIPE).communicate()
+ return in_temp('dlmalloc.o')
+ def fix_dlmalloc():
# dlmalloc needs some sign correction. # If we are in mode 0, switch to 2. We will add our lines
try:
if shared.Settings.CORRECT_SIGNS == 0: raise Exception('we need to change to 2')
@@ -472,6 +464,38 @@ try:
shared.Settings.CORRECT_SIGNS_LINES = [shared.path_from_root('src', 'dlmalloc.c') + ':' + str(i+4) for i in [4816, 4191, 4246, 4199, 4205, 4235, 4227]]
# If we are in mode 1, we are correcting everything anyhow. If we are in mode 3, we will be corrected
# so all is well anyhow too.
+ dlmalloc_symbols = set(['malloc', 'free', 'calloc', 'memalign', 'realloc', 'valloc', 'pvalloc', 'mallinfo', 'mallopt', 'malloc_trim', 'malloc_stats', 'malloc_usable_size', 'malloc_footprint', 'malloc_max_footprint', 'independent_calloc', 'independent_comalloc', '_Znwj', '_Znaj', '_Znam', '_Znwm'])
+
+ # libcxx
+ def create_libcxx():
+ print >> sys.stderr, 'emcc: building libcxx for cache'
+ shared.Building.build_library('libcxx', shared.EMSCRIPTEN_TEMP_DIR, shared.EMSCRIPTEN_TEMP_DIR, ['libcxx.bc'], configure=None, copy_project=True, source_dir=shared.path_from_root('system', 'lib', 'libcxx'))
+ return os.path.join(shared.EMSCRIPTEN_TEMP_DIR, 'libcxx', 'libcxx.bc')
+ def fix_libcxx():
+ # libcxx probably needs sign correction. # If we are in mode 0, switch to 2. We will add our lines
+ shared.Settings.CORRECT_SIGNS = 1
+ print >> sys.stderr, 'emcc: warning: using libcxx turns on CORRECT_SIGNS'
+ libcxx_symbols = map(lambda line: line.strip().split(' ')[1], open(shared.path_from_root('system', 'lib', 'libcxx', 'symbols')).readlines())
+ libcxx_symbols = filter(lambda symbol: symbol not in dlmalloc_symbols, libcxx_symbols)
+ libcxx_symbols = set(libcxx_symbols)
+
+ for name, create, fix, library_symbols in [('dlmalloc', create_dlmalloc, fix_dlmalloc, dlmalloc_symbols),
+ ('libcxx', create_libcxx, fix_libcxx, libcxx_symbols)]:
+ need = False
+ has = False
+ for input_file in input_files:
+ symbols = shared.Building.llvm_nm(in_temp(unsuffixed_basename(input_file) + '.o'))
+ for library_symbol in library_symbols:
+ if library_symbol in symbols.undefs:
+ need = True
+ if library_symbol in symbols.defs:
+ has = True
+ if need and not has:
+ # We need to build and link the library in
+ if DEBUG: print >> sys.stderr, 'emcc: including %s' % name
+ extra_files_to_link.append(shared.Cache.get(name, create))
+ if fix:
+ fix()
# First, combine the bitcode files if there are several
if len(input_files) + len(extra_files_to_link) > 1:
@@ -488,6 +512,11 @@ try:
if llvm_opt_level > 0 and not LEAVE_INPUTS_RAW:
if DEBUG: print >> sys.stderr, 'emcc: LLVM opts'
shared.Building.llvm_opt(in_temp(target_basename + '.bc'), LLVM_INTERNAL_OPT_LEVEL, safe=llvm_opt_level < 2)
+ else:
+ # If possible, remove dead functions etc., this potentially saves a lot in the size of the generated code (and the time to compile it)
+ if not shared.Settings.BUILD_AS_SHARED_LIB:
+ if DEBUG: print >> sys.stderr, 'emcc: LLVM dead globals elimination'
+ shared.Building.llvm_opt(in_temp(target_basename + '.bc'), ['-internalize', '-globaldce'])
# Emscripten
try:
@@ -507,6 +536,7 @@ try:
if not LEAVE_INPUTS_RAW:
final = in_temp(target_basename + '.bc')
+ if DEBUG: save_intermediate('bc', 'bc')
final = shared.Building.llvm_dis(final, final + '.ll')
if DEBUG: save_intermediate('ll', 'll')
else: