diff options
-rwxr-xr-x | emcc | 12 | ||||
-rwxr-xr-x | emscripten.py | 13 | ||||
-rw-r--r-- | src/compiler.js | 2 | ||||
-rw-r--r-- | src/modules.js | 5 | ||||
-rwxr-xr-x | tests/runner.py | 32 |
5 files changed, 58 insertions, 6 deletions
@@ -269,6 +269,9 @@ Options that are modified or new in %s include: target other than HTML is specified using the -o option. + --js-library <lib> A JavaScript library to use in addition to + those in Emscripten's src/library_* + The target file, if specified (-o <target>), defines what will be generated: @@ -437,6 +440,7 @@ try: compression = None ignore_dynamic_linking = False shell_path = shared.path_from_root('src', 'shell.html') + js_libraries = [] def check_bad_eq(arg): assert '=' not in arg, 'Invalid parameter (do not use "=" with "--" options)' @@ -514,6 +518,11 @@ try: shell_path = newargs[i+1] newargs[i] = '' newargs[i+1] = '' + elif newargs[i].startswith('--js-library'): + check_bad_eq(newargs[i]) + js_libraries.append(newargs[i+1]) + newargs[i] = '' + newargs[i+1] = '' newargs = [ arg for arg in newargs if arg is not '' ] if llvm_opts is None: llvm_opts = LLVM_OPT_LEVEL[opt_level] @@ -843,7 +852,8 @@ try: # Emscripten if DEBUG: print >> sys.stderr, 'emcc: LLVM => JS' - final = shared.Building.emscripten(final, append_ext=False) + extra_args = [] if not js_libraries else ['--libraries', ','.join(map(os.path.abspath, js_libraries))] + final = shared.Building.emscripten(final, append_ext=False, extra_args=extra_args) if DEBUG: save_intermediate('original') # Embed and preload files diff --git a/emscripten.py b/emscripten.py index 5674c33a..45c9f418 100755 --- a/emscripten.py +++ b/emscripten.py @@ -35,7 +35,7 @@ def path_from_root(*pathelems): temp_files = shared.TempFiles() -def emscript(infile, settings, outfile): +def emscript(infile, settings, outfile, libraries=[]): """Runs the emscripten LLVM-to-JS compiler. Args: @@ -49,7 +49,7 @@ def emscript(infile, settings, outfile): s.write(settings) s.close() compiler = path_from_root('src', 'compiler.js') - shared.run_js(compiler, shared.COMPILER_ENGINE, [settings_file, infile], stdout=outfile, cwd=path_from_root('src')) + shared.run_js(compiler, shared.COMPILER_ENGINE, [settings_file, infile] + libraries, stdout=outfile, cwd=path_from_root('src')) outfile.close() @@ -123,8 +123,11 @@ def main(args): #print >> sys.stderr, 'new defs:', str(defines).replace(',', ',\n '), '\n\n' settings.setdefault('C_DEFINES', {}).update(defines) + # libraries + libraries = args.libraries[0].split(',') if len(args.libraries) > 0 else [] + # Compile the assembly to Javascript. - emscript(args.infile, json.dumps(settings), args.outfile) + emscript(args.infile, json.dumps(settings), args.outfile, libraries) if __name__ == '__main__': parser = optparse.OptionParser( @@ -136,6 +139,10 @@ if __name__ == '__main__': default=[], action='append', help='System headers (comma separated) whose #defines should be exposed to the compiled code.') + parser.add_option('-L', '--libraries', + default=[], + action='append', + help='Library files (comma separated) to use in addition to those in emscripten src/library_*.') parser.add_option('-o', '--outfile', default=sys.stdout, help='Where to write the output; defaults to stdout.') diff --git a/src/compiler.js b/src/compiler.js index 29ae47dd..4442e38e 100644 --- a/src/compiler.js +++ b/src/compiler.js @@ -114,6 +114,7 @@ load('settings.js'); var settings_file = arguments_[0]; var ll_file = arguments_[1]; +additionalLibraries = Array.prototype.slice.call(arguments_, 2); if (settings_file) { var settings = JSON.parse(read(settings_file)); @@ -122,6 +123,7 @@ if (settings_file) { } } + if (CORRECT_SIGNS >= 2) { CORRECT_SIGNS_LINES = set(CORRECT_SIGNS_LINES); // for fast checking } diff --git a/src/modules.js b/src/modules.js index 2bed1756..a6aaa99a 100644 --- a/src/modules.js +++ b/src/modules.js @@ -259,8 +259,9 @@ var LibraryManager = { load: function() { assert(!this.library); - for (var suffix in set('', '_sdl', '_browser', '_gl', '_glut', '_xlib', '_egl')) { - eval(processMacros(preprocess(read('library' + suffix + '.js')))); + var libraries = ['library.js', 'library_browser.js', 'library_sdl.js', 'library_gl.js', 'library_glut.js', 'library_xlib.js', 'library_egl.js'].concat(additionalLibraries); + for (var i = 0; i < libraries.length; i++) { + eval(processMacros(preprocess(read(libraries[i])))); } }, diff --git a/tests/runner.py b/tests/runner.py index 7c24c799..a565b1d2 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -6396,6 +6396,38 @@ f.close() Popen(['python', EMCC, os.path.join(self.get_dir(), 'main.cpp'), os.path.join(self.get_dir(), 'subdir', 'libfile.so'), '-L.']).communicate() self.assertContained('hello from lib', run_js(os.path.join(self.get_dir(), 'a.out.js'))) + def test_js_libraries(self): + open(os.path.join(self.get_dir(), 'main.cpp'), 'w').write(''' + #include <stdio.h> + extern "C" { + extern void printey(); + extern int calcey(int x, int y); + } + int main() { + printey(); + printf("*%d*\\n", calcey(10, 22)); + return 0; + } + ''') + open(os.path.join(self.get_dir(), 'mylib1.js'), 'w').write(''' + mergeInto(LibraryManager.library, { + printey: function() { + Module.print('hello from lib!'); + } + }); + ''') + open(os.path.join(self.get_dir(), 'mylib2.js'), 'w').write(''' + mergeInto(LibraryManager.library, { + calcey: function(x, y) { + return x + y; + } + }); + ''') + + Popen(['python', EMCC, os.path.join(self.get_dir(), 'main.cpp'), '--js-library', os.path.join(self.get_dir(), 'mylib1.js'), + '--js-library', os.path.join(self.get_dir(), 'mylib2.js')]).communicate() + self.assertContained('hello from lib!\n*32*\n', run_js(os.path.join(self.get_dir(), 'a.out.js'))) + def test_identical_basenames(self): # Issue 287: files in different dirs but with the same basename get confused as the same, # causing multiply defined symbol errors |