aboutsummaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2011-10-24 15:20:01 -0700
committerAlon Zakai <alonzakai@gmail.com>2011-10-24 15:20:01 -0700
commit160c0621603a3abc99d936d0216e84862583338a (patch)
treeaf5d5e724300e795d0f88da3b081d9e2892bbc99 /tools
parent46398a40e7fde539f6645d12772ff7493b0ba2b1 (diff)
move get_library into shared
Diffstat (limited to 'tools')
-rw-r--r--tools/shared.py60
1 files changed, 60 insertions, 0 deletions
diff --git a/tools/shared.py b/tools/shared.py
index c0cc6f21..9b2dee70 100644
--- a/tools/shared.py
+++ b/tools/shared.py
@@ -214,3 +214,63 @@ Settings = Dummy() # A global singleton. Not pretty, but nicer than passing |, s
Settings.save_dir = 0
Settings.save_JS = 0
+# Building
+
+COMPILER = CLANG
+LLVM_OPTS = False
+COMPILER_TEST_OPTS = []
+
+GlobalCache = {}
+
+def get_library(name, build_dir, output_dir, generated_libs, configure=['./configure'], configure_args=[], make=['make'], make_args=['-j', '2'], cache=True, build_subdir=None):
+ ''' Build a library into a .bc file. We build the .bc file once and cache it for all our tests. (We cache in
+ memory since the test directory is destroyed and recreated for each test. Note that we cache separately
+ for different compilers) '''
+
+ if type(generated_libs) is not list: generated_libs = [generated_libs]
+ if build_subdir and configure.startswith('./'):
+ configure = '.' + configure
+
+ if GlobalCache is not None:
+ cache_name = name + '|' + COMPILER
+ if cache and GlobalCache.get(cache_name):
+ print >> sys.stderr, '<load build from cache> ',
+ bc_file = os.path.join(output_dir, 'lib' + name + '.bc')
+ f = open(bc_file, 'wb')
+ f.write(GlobalCache[cache_name])
+ f.close()
+ return bc_file
+
+ temp_dir = build_dir
+ project_dir = os.path.join(temp_dir, name)
+ shutil.copytree(path_from_root('tests', name), project_dir) # Useful in debugging sometimes to comment this out
+ os.chdir(project_dir)
+ if build_subdir:
+ try:
+ os.mkdir('cbuild')
+ except:
+ pass
+ os.chdir(os.path.join(project_dir, 'cbuild'))
+ env = os.environ.copy()
+ env['RANLIB'] = env['AR'] = env['CXX'] = env['CC'] = env['LIBTOOL'] = EMMAKEN
+ env['EMMAKEN_COMPILER'] = COMPILER
+ env['EMSCRIPTEN_TOOLS'] = path_from_root('tools')
+ env['CFLAGS'] = env['EMMAKEN_CFLAGS'] = ' '.join(COMPILER_OPTS + COMPILER_TEST_OPTS) # Normal CFLAGS is ignored by some configure's.
+ if configure: # Useful in debugging sometimes to comment this out (and the lines below up to and including the |make| call)
+ env['EMMAKEN_JUST_CONFIGURE'] = '1'
+ Popen(configure + configure_args, stdout=open(os.path.join(output_dir, 'configure'), 'w'),
+ stderr=open(os.path.join(output_dir, 'configure_err'), 'w'), env=env).communicate()[0]
+ del env['EMMAKEN_JUST_CONFIGURE']
+ Popen(make + make_args, stdout=open(os.path.join(output_dir, 'make'), 'w'),
+ stderr=open(os.path.join(output_dir, 'make_err'), 'w'), env=env).communicate()[0]
+ bc_file = os.path.join(project_dir, 'bc.bc')
+ do_link(map(lambda lib: os.path.join(project_dir, 'cbuild', lib) if build_subdir else os.path.join(project_dir, lib), generated_libs), bc_file)
+ if cache and GlobalCache is not None:
+ print >> sys.stderr, '<save build into cache> ',
+ GlobalCache[cache_name] = open(bc_file, 'rb').read()
+ return bc_file
+
+def do_link(files, target):
+ output = Popen([LLVM_LINK] + files + ['-o', target], stdout=PIPE, stderr=STDOUT).communicate()[0]
+ assert output is None or 'Could not open input file' not in output, 'Linking error: ' + output
+