All public headers are exported
Sunappnio opened this issue · 18 comments
Thank you for your great work! However I have some trouble using cpp to wasm translation. I only want to export part of my functions to wasm, however tolc scan and generate ALL public functions. What's more, some references to int as parameter are not allowed in javascript so embind will complain.. Is there some way to exclude those functions so that they are only used inside cpp and not exported to js?
I have set DO_NOT_SEARCH_TARGET_INCLUDES, however that does not work because those headers are included recursively.
Hi!
It sounds like there might be an issue with the CMake code that handles DO_NOT_SEARCH_TARGET_INCLUDES
then. Could you provide me with the call to tolc_create_bindings
including the parameters?
Could you also elaborate on the functions embind are complaining about? What is the error that you recieve and how does the function look?
Btw, thank you for the report!
I clone the tolc-demo project, and did little modification. the call is :
tolc_create_bindings(
TARGET
MyCppLib
LANGUAGE
${language}
HEADERS
include/interface.h
DO_NOT_SEARCH_TARGET_INCLUDES
OUTPUT
${CMAKE_CURRENT_BINARY_DIR}/${language}-bindings)
# Get the built files
if(language STREQUAL python)
set(output_files $<TARGET_FILE:MyCppLib_python>)
elseif(language STREQUAL wasm)
set(output_files $<TARGET_FILE_DIR:MyCppLib_wasm>/MyCppLib.js
$<TARGET_FILE_DIR:MyCppLib_wasm>/MyCppLib.wasm)
elseif(language STREQUAL objc)
enable_language(OBJC)
add_executable(main objc/main.m)
target_link_libraries(main PRIVATE "-framework Foundation")
target_link_libraries(main PRIVATE MyCppLib_objc)
# The build lib and the corresponding header file
set(output_files $<TARGET_FILE:MyCppLib_objc>
${CMAKE_CURRENT_BINARY_DIR}/${language}-bindings/MyCppLib.h)
endif()
example code that embind complains:
bool Card::examplefunc(std::vector<std::vector<int> >& cardIds, int ¶m1, int ¶m2)
I would not consider it a bug, because there is no such thing as pass int by reference in javascipt.
Even though it is not a bug, maybe Frontend.wasm should print a warning. Thank you.
Is it just for wasm that it finds other header files as well? Or does the same thing happen with python/objc?
Actually, it's easy to reproduce my original problem.
After cloning the tolc-demo project, I create a file named "anotherheader.h" in the same directory of MyCppLib.hpp which has one simple enum defination in it. And in MyCppLib.hpp I include "anotherheader.h".
The demo still work but that enum(or any other public methods) will be exposed to javascript even I set "DO_NOT_SEARCH_TARGET_INCLUDES". I do not want to expose that. And if that function contains "int ¶m2" then the situation will be worse.
After some debugging, I found that this also happen with python. And the problem doesn't seem like comming from "DO_NOT_SEARCH_TARGET_INCLUDES". Because build-wasm/tolc/tolc_MyCppLib.hpp is correct.
I'm a javascript developer and have little knowledge about cmake. I tried to add things like "CXX_VISIBILITY_PRESET" or "VISIBILITY_INLINES_HIDDEN" but that's not working..
I see. Tolc just takes a list of headers and reads only those. It is CMakes job to provide which headers to read.
Hmm, I'm not able to reproduce your error. Here are the steps I took (from the tolc-demo
repo):
- Add a new file
include/should_not_be_included.hpp
with a functionf
. - Add
DO_NOT_SEARCH_TARGET_INCLUDES
andHEADERS include/MyCppLib.hpp
in the call to tolc. - Configure and build the project
- Check to see which functions are available from python with
import MyCppLib; dir(MyCppLib)
I did not see the f
function, and rerunning it with f
included in include/MyCppLib.hpp
makes it visible.
The list of headers that Tolc takes are stored in build-py/tolc/tolc_MyCppLib.hpp
And the actual pybind
that is generated is stored in build-py/python-bindings/MyCppLib_python.cpp
Could you check the equivalent files in your project? What do they contain?
should_not_be_included.hpp
#ifndef __demo_header
#define __demo_header
int f()
{
return 0;
}
#endif
build-py/tolc/tolc_MyCppLib.hpp
#include "/home/tom/github/tolc-demo/include/MyCppLib.hpp"
build-py/python-bindings/MyCppLib_python.cpp
#include </home/tom/github/tolc-demo/build-py/tolc/tolc_MyCppLib.hpp>
#include <pybind11/pybind11.h>
namespace py = pybind11;
PYBIND11_MODULE(MyCppLib, MyCppLib) {
MyCppLib.def("f", &f, "");
auto MyCppLib_Hello = MyCppLib.def_submodule("Hello", "");
MyCppLib_Hello.def("cppFunction", &Hello::cppFunction, "", py::arg("name"));
}
HEADERS include/MyCppLib.hpp
or
HEADERS include/MyCppLib.hpp include/should_not_be_included.h
same result here
Are you using windows? I'm using WSL2, ubuntu20.04 , cmake 3.25.2
maybe msvc hide these public method by default and linux gcc do not hide.
I'm using MacOS at the moment. It might be some weird msvc thing. I can try on Linux as well. This is pretty strange.
Do you have any other OS available to try on?
Yes, but it's a little bit late now, I'm going to try another OS tomorrow. Thanks for your help!
No worries. The only CI tests on Windows are using MSVC with Visual Studio so WSL is technically untested. If this works out, I'll add a test for it as well. Thank you as well!