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

Make embind work with -fvisibility=hidden #22095

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion system/include/emscripten/val.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <variant>
#endif

#define EMSCRIPTEN_EXPORT __attribute__((visibility("default")))

namespace emscripten {

Expand Down Expand Up @@ -284,7 +285,7 @@ struct WireTypePack {
static const char name##_symbol[] = #name; \
static const ::emscripten::internal::symbol_registrar<name##_symbol> name##_registrar

class val {
class EMSCRIPTEN_EXPORT val {
public:
// missing operators:
// * ~ - + ++ --
Expand Down
3 changes: 2 additions & 1 deletion system/include/emscripten/wire.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <string>

#define EMSCRIPTEN_ALWAYS_INLINE __attribute__((always_inline))
#define EMSCRIPTEN_EXPORT __attribute__((visibility("default")))

#ifndef EMSCRIPTEN_HAS_UNBOUND_TYPE_NAMES
#define EMSCRIPTEN_HAS_UNBOUND_TYPE_NAMES 1
Expand Down Expand Up @@ -430,7 +431,7 @@ constexpr bool typeSupportsMemoryView() {
} // namespace internal

template<typename ElementType>
struct memory_view {
struct EMSCRIPTEN_EXPORT memory_view {
memory_view() = delete;
explicit memory_view(size_t size, const ElementType* data)
: size(size)
Expand Down
59 changes: 59 additions & 0 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,17 @@ def decorated(self, *args, **kwargs):
return decorated


def requires_dylink(func):
assert callable(func)

@wraps(func)
def decorated(self, *args, **kwargs):
self.check_dylink()
return func(self, *args, **kwargs)

return decorated


def llvm_nm(file):
output = shared.run_process([LLVM_NM, file], stdout=PIPE).stdout

Expand Down Expand Up @@ -3346,6 +3357,54 @@ def test_embind_jsgen_method_pointer_stability(self):
# AOT JS generation still works correctly.
self.do_runf('other/embind_jsgen_method_pointer_stability.cpp', 'done')

def build_dylink_lib(self, filename, outfile='liblib.so', emcc_args=None):
self.clear_setting('MAIN_MODULE')
self.set_setting('SIDE_MODULE')
cmd = [compiler_for(filename), filename, '-o', outfile] + self.get_emcc_args()
if emcc_args:
cmd += emcc_args
self.run_process(cmd)

def prep_dylink_main(self):
self.clear_setting('SIDE_MODULE')
self.set_setting('MAIN_MODULE', 1)

@requires_dylink
def test_embind_dylink_visibility_hidden(self):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks similar to what test_dylink_rtti is doing in test_core.py, but with some embind specifics. As such, perhaps it belongs alongside that test?

Rather than duplicating the three helper functions from test_core.py why not just put this test in test_core.py itself?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It also might make more sense alongside test_embind_no_rtti

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, tst_core could make sense as well (I had it there first). Is it primarily a dylink or embind test? test_embind_no_rtti sounds related, I'll check what that does.

(Note I have vacation time is coming up, so I'll get back to this in a couple of week's time.)

# Check that embind is usable from a library built with "-fvisibility=hidden"
create_file('liba.cpp', r'''
#include <emscripten/val.h>

#define EXPORT __attribute__((visibility("default")))
using namespace emscripten;

EXPORT void liba_fun() {
unsigned char buffer[1];
val view(typed_memory_view(1, buffer));
}
''')

self.build_dylink_lib('liba.cpp', outfile='liba.so', emcc_args=['-std=c++11', '-fvisibility=hidden'])
self.prep_dylink_main()
self.emcc_args = ['liba.so', '-L.', '--bind']
create_file('main.cpp', r'''
#include <stdio.h>
#include <emscripten/val.h>

using namespace emscripten;

void liba_fun();

int main() {

liba_fun();

printf("done\n");
return 0;
}
''')
self.do_runf('main.cpp', 'done\n')

def test_emit_tsd(self):
self.run_process([EMCC, test_file('other/test_emit_tsd.c'),
'--emit-tsd', 'test_emit_tsd.d.ts', '-sEXPORT_ES6',
Expand Down
Loading