Skip to content

Commit 217dbc2

Browse files
committed
embind integration in emcc and work towards a test
1 parent 706cca7 commit 217dbc2

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

emcc

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,9 @@ Options that are modified or new in %s include:
289289
The main file resides in the base directory and
290290
has the suffix ".js".
291291
292+
--bind Compiles the source code using the "embind"
293+
bindings approach, which connects C/C++ and JS.
294+
292295
--ignore-dynamic-linking Normally emcc will treat dynamic linking like
293296
static linking, by linking in the code from
294297
the dynamic library. This fails if the same
@@ -413,6 +416,8 @@ STATICLIB_SUFFIXES = ('.a',)
413416
ASSEMBLY_SUFFIXES = ('.ll',)
414417
LIB_PREFIXES = ('', 'lib')
415418

419+
JS_CONTAINING_SUFFIXES = ('js', 'html')
420+
416421
seen_names = {}
417422
def uniquename(name):
418423
if name not in seen_names:
@@ -500,6 +505,7 @@ try:
500505
shell_path = shared.path_from_root('src', 'shell.html')
501506
js_libraries = []
502507
remove_duplicates = False
508+
bind = False
503509

504510
def check_bad_eq(arg):
505511
assert '=' not in arg, 'Invalid parameter (do not use "=" with "--" options)'
@@ -558,6 +564,9 @@ try:
558564
split_js_file = int(newargs[i+1])
559565
newargs[i] = ''
560566
newargs[i+1] = ''
567+
elif newargs[i] == '--bind':
568+
bind = True
569+
newargs[i] = '-std=c++11' # Force C++11 for embind code
561570
elif newargs[i].startswith('--embed-file'):
562571
check_bad_eq(newargs[i])
563572
embed_files.append(newargs[i+1])
@@ -730,6 +739,10 @@ try:
730739

731740
assert not (Compression.on and final_suffix != 'html'), 'Compression only works when generating HTML'
732741

742+
# If we are using embind and generating JS, now is the time to link in bind.cpp
743+
if bind and final_suffix in JS_CONTAINING_SUFFIXES:
744+
input_files.append(shared.path_from_root('system', 'lib', 'embind', 'bind.cpp'))
745+
733746
# Apply optimization level settings
734747
shared.Settings.apply_opt_level(opt_level, noisy=True)
735748

@@ -779,7 +792,7 @@ try:
779792
if not LEAVE_INPUTS_RAW: assert len(temp_files) == len(input_files)
780793

781794
# If we were just asked to generate bitcode, stop there
782-
if final_suffix not in ['js', 'html']:
795+
if final_suffix not in JS_CONTAINING_SUFFIXES:
783796
if llvm_opts > 0:
784797
print >> sys.stderr, 'emcc: warning: -Ox flags ignored, since not generating JavaScript'
785798
if not specified_target:
@@ -957,7 +970,7 @@ try:
957970
if Compression.on:
958971
file_args += ['--compress', Compression.encoder, Compression.decoder, Compression.js_name]
959972
code = execute(shared.ENV_PREFIX + ['python', shared.FILE_PACKAGER, unsuffixed(target) + '.data'] + file_args, stdout=PIPE)[0]
960-
src = open(final).read().replace('// {{PRE_RUN_ADDITIONS}}', code)
973+
src = open(final).read().replace('// {{PRE_RUN_ADDITIONS}}', '// {{PRE_RUN_ADDITIONS}}\n' + code)
961974
final += '.files.js'
962975
open(final, 'w').write(src)
963976
if DEBUG: save_intermediate('files')
@@ -970,6 +983,14 @@ try:
970983
open(final, 'w').write(pre_js + src + post_js)
971984
if DEBUG: save_intermediate('pre-post')
972985

986+
# Add bindings glue if used
987+
if bind:
988+
if DEBUG: print >> sys.stderr, 'emcc: adding embind glue'
989+
src = open(final).read().replace('// {{PRE_RUN_ADDITIONS}}', '// {{PRE_RUN_ADDITIONS}}\n' + open(shared.path_from_root('src', 'embind', 'embind.js')).read())
990+
final += '.bd.js'
991+
open(final, 'w').write(src)
992+
if DEBUG: save_intermediate('bind')
993+
973994
# Apply a source code transformation, if requested
974995
if js_transform:
975996
shutil.copyfile(final, final + '.tr.js')

src/embind/embind.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,3 +617,4 @@ function __embind_register_interface(
617617
},
618618
};
619619
}
620+

tests/runner.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7695,6 +7695,20 @@ def test_scons(self): # also incidentally tests c++11 integration in llvm 3.1
76957695
output = run_js('scons_integration.js')
76967696
assert 'If you see this - the world is all right!' in output
76977697

7698+
def zzztest_embind(self):
7699+
# TODO: test -O1 and -O2
7700+
for args, fail in [
7701+
([], True), # without --bind, we fail
7702+
(['--bind'], False)
7703+
]:
7704+
print args, fail
7705+
try_delete(self.in_dir('a.out.js'))
7706+
Popen(['python', EMCC, path_from_root('tests', 'embind', 'embind_test.cpp'), '--post-js', path_from_root('tests', 'embind', 'embind_test.js')] + args, stderr=PIPE if fail else None).communicate()
7707+
assert os.path.exists(self.in_dir('a.out.js')) == (not fail)
7708+
if not fail:
7709+
output = run_js(self.in_dir('a.out.js'))
7710+
assert 'If you see this - the world is all right!' in output
7711+
76987712
def test_llvm_nativizer(self):
76997713
# avoid impure_ptr problems etc.
77007714
shutil.copyfile(path_from_root('tests', 'files.cpp'), os.path.join(self.get_dir(), 'files.cpp'))

0 commit comments

Comments
 (0)