diff options
author | Alon Zakai <alonzakai@gmail.com> | 2011-10-26 20:16:22 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2011-10-26 20:16:22 -0700 |
commit | 96a9c24f8170d3657ea711c17689448e8886b938 (patch) | |
tree | 35db63064319b8756e0c2ff675c01bdc4dd8cbcb /tools | |
parent | d259c5e17e7727806f2e54c6ffcdce1eec752ade (diff) |
refactor library building/caching
Diffstat (limited to 'tools')
-rw-r--r-- | tools/shared.py | 96 |
1 files changed, 39 insertions, 57 deletions
diff --git a/tools/shared.py b/tools/shared.py index 5bd80c5d..9f59eeb0 100644 --- a/tools/shared.py +++ b/tools/shared.py @@ -214,61 +214,43 @@ Settings = Dummy() # A global singleton. Not pretty, but nicer than passing |, s # 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 +class Building: + COMPILER = CLANG + LLVM_OPTS = False + COMPILER_TEST_OPTS = [] + + @staticmethod + def build_library(name, build_dir, output_dir, generated_libs, configure=['./configure'], configure_args=[], make=['make'], make_args=['-j', '2'], cache=None, cache_name=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] + + 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) + 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') + Building.link(map(lambda lib: os.path.join(project_dir, lib), generated_libs), bc_file) + if cache is not None: + cache[cache_name] = open(bc_file, 'rb').read() + return bc_file + + @staticmethod + def 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 |