Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow linkage to .js files #4816

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
root = true

[*]
indent_style = space
indent_size = 2
trim_trailing_whitespace = false
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,4 @@ a license to everyone to use it as detailed in LICENSE.)
* Fumiya Chiba <fumiya.chiba@nifty.com>
* Ryan C. Gordon <icculus@icculus.org>
* Inseok Lee <dlunch@gmail.com>
* Thomas Heck <t@b128.net>
10 changes: 7 additions & 3 deletions emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
BITCODE_ENDINGS = ('.bc', '.o', '.obj', '.lo')
DYNAMICLIB_ENDINGS = ('.dylib', '.so') # Windows .dll suffix is not included in this list, since those are never linked to directly on the command line.
STATICLIB_ENDINGS = ('.a',)
JSLIB_ENDINGS = ('.emlib.js',)
ASSEMBLY_ENDINGS = ('.ll',)
HEADER_ENDINGS = ('.h', '.hxx', '.hpp', '.hh', '.H', '.HXX', '.HPP', '.HH')
WASM_ENDINGS = ('.wasm', '.wast')
Expand Down Expand Up @@ -965,13 +966,16 @@ def get_last_setting_change(setting):
logging.debug('looking for library "%s"', lib)
found = False
for prefix in LIB_PREFIXES:
for suff in STATICLIB_ENDINGS + DYNAMICLIB_ENDINGS:
for suff in STATICLIB_ENDINGS + DYNAMICLIB_ENDINGS + JSLIB_ENDINGS:
name = prefix + lib + suff
for lib_dir in lib_dirs:
path = os.path.join(lib_dir, name)
if os.path.exists(path):
logging.debug('found library "%s" at %s', lib, path)
input_files.append((i, path))
if suff in JSLIB_ENDINGS:
js_libraries.append(path)
else:
input_files.append((i, path))
found = True
break
if found: break
Expand All @@ -996,7 +1000,7 @@ def check(input_file):
input_files = [(i, input_file) for (i, input_file) in input_files if check(input_file)]

if len(input_files) == 0:
logging.error('no input files\nnote that input files without a known suffix are ignored, make sure your input files end with one of: ' + str(SOURCE_ENDINGS + BITCODE_ENDINGS + DYNAMICLIB_ENDINGS + STATICLIB_ENDINGS + ASSEMBLY_ENDINGS + HEADER_ENDINGS))
logging.error('no input files\nnote that input files without a known suffix are ignored, make sure your input files end with one of: ' + str(SOURCE_ENDINGS + BITCODE_ENDINGS + DYNAMICLIB_ENDINGS + STATICLIB_ENDINGS + JSLIB_ENDINGS + ASSEMBLY_ENDINGS + HEADER_ENDINGS))
exit(1)

newargs = CC_ADDITIONAL_ARGS + newargs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,10 @@ libraries for relevant external symbols.
By default, the implementation is added to **library.js** (and this is
where you'll find parts of Emscripten's *libc*). You can put
the JavaScript implementation in your own library file and add it using
the :ref:`emcc option <emcc-js-library>` ``--js-library``. See
`test_js_libraries`_ in **tests/test_other.py** for a complete working
example, including the syntax you should use inside the JavaScript library
file.
the :ref:`emcc option <emcc-js-library>` ``--js-library`` or linking to
it by using the ``-L`` and `-l`` arguments. See `test_js_libraries`_ in
**tests/test_other.py** for a complete working example, including the
syntax you should use inside the JavaScript library file.

As a simple example, consider the case where you have some C code like this:

Expand Down
1 change: 1 addition & 0 deletions site/source/docs/tools_reference/emcc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ Options that are modified or new in *emcc* are listed below:

``--js-library <lib>``
A JavaScript library to use in addition to those in Emscripten's core libraries (src/library_*).
This can be also achieved by using the linking arguments ``-L`` and ``-l``.

.. _emcc-verbose:

Expand Down
3 changes: 3 additions & 0 deletions tests/link_jslib/link_jslib.emlib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
LibraryManager.library.test_link_jslib = function test_link_jslib(a, b) {
return a + b;
};
11 changes: 11 additions & 0 deletions tests/link_jslib/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <stdio.h>

extern "C" {
extern int test_link_jslib(int a, int b);
}

int main() {
int temp = test_link_jslib(11, 31);
printf("res: %d\n", temp);
return 0;
}
4 changes: 4 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7293,6 +7293,10 @@ def test_mallinfo(self):

def test_wrap_malloc(self):
self.do_run(open(path_from_root('tests', 'wrap_malloc.cpp')).read(), 'OK.')

def test_link_jslib(self):
self.emcc_args += ['-L', path_from_root('tests', 'link_jslib'), '-l', 'link_jslib']
self.do_run(open(path_from_root('tests', 'link_jslib', 'main.c')).read(), 'res: 42')

# Generate tests for everything
def make_run(fullname, name=-1, compiler=-1, embetter=0, quantum_size=0,
Expand Down