leaningtech / cheerp-meta

Cheerp - a C/C++ compiler for Web applications - compiles to WebAssembly and JavaScript
https://labs.leaningtech.com/cheerp
Other
1.03k stars 51 forks source link

Support compiling OpenCV #156

Closed iSuslov closed 2 months ago

iSuslov commented 2 months ago

Hey Cheerp team!

I've been tinkering with Cheerp and trying to compile OpenCV, but I'm stuck. After hitting some compilation errors, I feel like I'm back at square one. The current examples you've got are good for "make" OR "cmake", but OpenCV's a bit trickier - it uses cmake first and then make. I'm scratching my head on how to wedge Cheerp into this process. Any pointers?

Also, got a couple of questions buzzing around:

  1. What's your take on a Cheerp-compiled version of OpenCV vs the current Emscripten one? Do you think it could be in any way better?

  2. I stumbled across a Cheerp blog post from about a year and a half ago mentioning that version 3.1 with multithreading was coming "soon". Not to be a pain, but any updates on that front?

  3. Wild idea - what about Node.js addons? I'm no expert, but my gut says it might be better sometimes to keep the natural POSIX multithreading, especially when compiling for Node.js. Knowing that your approach is saving original code as much as possible that may lay down nicely. How hard would it be to whip up a special Cheerp mode just for compiling C/C++ Node addons?

DutChen18 commented 2 months ago

Hello Ivan!

Most libraries should compile with Cheerp with little to no modifications to the source code. You only have to define CMAKE_TOOLCHAIN_FILE so CMake knows to use the cheerp compiler.

For example, to build bullet physics:

# Clone the repository
git clone https://github.com/bulletphysics/bullet3.git
cd bullet3

# Configure CMake to use the Cheerp compiler
mkdir build
cmake -B build -DCMAKE_TOOLCHAIN_FILE=/opt/cheerp/share/cmake/Modules/CheerpWasmToolchain.cmake

# Build and run the HelloWorld example
make -C build -j App_HelloWorld
node build/examples/HelloWorld/App_HelloWorld.js

There is nothing tricky about first running cmake and then make. This is simply how CMake is intended to work. CMake does not compile the code itself, it only generates the required files for another build system (such as make). For more information, please read the cmake documentation and "Why CMake?".

Unfortunately, Cheerp cannot build OpenCV at the moment. Parts of OpenCV conditionally include code for specific compilers, and fail to compile when compiled with another compiler. I also encountered at least one cheerp crash while attempting to compile OpenCV. We do hope to be able to build OpenCV (possibly with some modifications) at some point in the future.

To answer your questions:

  1. I can't think of any significant differences that relate specifically to OpenCV. In general however, Cheerp does have some advantages over Emscripten that could be useful when writing web applications in C/C++ or using C/C++ libraries. For example, you can take a look at the jsexport attribute, or modifying the DOM, or read our blog post about TypeScript integration.

  2. Multithreading: we're working on it. You can join our discord for weekly development updates, including updates about multithreading.

  3. NodeJS addons contain native code compiled using a native compiler like Clang or GCC. Cheerp compiles C/C++ code to JavaScript and WebAssembly. Maybe I'm misunderstanding, but I don't see how/why you would want to use Cheerp to compile a native NodeJS addon.

iSuslov commented 2 months ago

Thank you for your insights!