Open Brennanzuz opened 1 month ago
If you just want to use the SDK then I suggest you down the prebuilt binaries from https://github.com/WebAssembly/wasi-sdk/releases rather than trying to build it yourself from source.
Aight, I did just that:
WASI_OS=linux
WASI_ARCH=x86_64
WASI_VERSION=24
WASI_VERSION_FULL=${WASI_VERSION}.0
wget https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-${WASI_VERSION}/wasi-sdk-${WASI_VERSION_FULL}-${WASI_ARCH}-${WASI_OS}.tar.gz
tar xvf wasi-sdk-${WASI_VERSION_FULL}-${WASI_ARCH}-${WASI_OS}.tar.gz
and:
WASI_SDK_PATH=`pwd`/wasi-sdk-${WASI_VERSION_FULL}-${WASI_ARCH}-${WASI_OS}
cd build
cmake -DCMAKE_TOOLCHAIN_FILE=${WASI_SDK_PATH}/share/cmake/wasi-sdk.cmake -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules ../opencv
Same error:
``` -- The CXX compiler identification is Clang 18.1.2 -- The C compiler identification is Clang 18.1.2 System is unknown to cmake, create: Platform/WASI to use this system, please post your config file on discourse.cmake.org so it can be added to cmake Your CMakeCache.txt file was copied to CopyOfCMakeCache.txt. Please post that file on discourse.cmake.org. -- Detecting CXX compiler ABI info System is unknown to cmake, create: Platform/WASI to use this system, please post your config file on discourse.cmake.org so it can be added to cmake -- Detecting CXX compiler ABI info - failed -- Check for working CXX compiler: /home/neonu/wasi-sdk/build/install/share/cmake/../..//bin/clang++ System is unknown to cmake, create: Platform/WASI to use this system, please post your config file on discourse.cmake.org so it can be added to cmake -- Check for working CXX compiler: /home/neonu/wasi-sdk/build/install/share/cmake/../..//bin/clang++ - broken CMake Error at /home/neonu/.local/lib/python3.10/site-packages/cmake/data/share/cmake-3.27/Modules/CMakeTestCXXCompiler.cmake:60 (message): The C++ compiler "/home/neonu/wasi-sdk/build/install/share/cmake/../..//bin/clang++" is not able to compile a simple test program. It fails with the following output: Change Dir: '/home/neonu/build/CMakeFiles/CMakeScratch/TryCompile-8Qlo8E' Run Build Command(s): /home/neonu/.local/lib/python3.10/site-packages/cmake/data/bin/cmake -E env VERBOSE=1 /usr/bin/gmake -f Makefile cmTC_7f876/fast /usr/bin/gmake -f CMakeFiles/cmTC_7f876.dir/build.make CMakeFiles/cmTC_7f876.dir/build gmake[1]: Entering directory '/home/neonu/build/CMakeFiles/CMakeScratch/TryCompile-8Qlo8E' Building CXX object CMakeFiles/cmTC_7f876.dir/testCXXCompiler.cxx.obj /home/neonu/wasi-sdk/build/install/share/cmake/../..//bin/clang++ --target=wasm32-wasi-threads -pthread -fPIE -MD -MT CMakeFiles/cmTC_7f876.dir/testCXXCompiler.cxx.obj -MF CMakeFiles/cmTC_7f876.dir/testCXXCompiler.cxx.obj.d -o CMakeFiles/cmTC_7f876.dir/testCXXCompiler.cxx.obj -c /home/neonu/build/CMakeFiles/CMakeScratch/TryCompile-8Qlo8E/testCXXCompiler.cxx Linking CXX executable cmTC_7f876 /home/neonu/.local/lib/python3.10/site-packages/cmake/data/bin/cmake -E cmake_link_script CMakeFiles/cmTC_7f876.dir/link.txt --verbose=1 /home/neonu/wasi-sdk/build/install/share/cmake/../..//bin/clang++ --target=wasm32-wasi-threads -pthread -Wl,--import-memory -Wl,--export-memory CMakeFiles/cmTC_7f876.dir/testCXXCompiler.cxx.obj -o cmTC_7f876 wasm-ld: error: cannot open /home/neonu/wasi-sdk/build/install/lib/clang/18/lib/wasi/libclang_rt.builtins-wasm32.a: No such file or directory clang++: error: linker command failed with exit code 1 (use -v to see invocation) gmake[1]: *** [CMakeFiles/cmTC_7f876.dir/build.make:100: cmTC_7f876] Error 1 gmake[1]: Leaving directory '/home/neonu/build/CMakeFiles/CMakeScratch/TryCompile-8Qlo8E' gmake: *** [Makefile:127: cmTC_7f876/fast] Error 2 CMake will not be able to correctly generate this project. Call Stack (most recent call first): CMakeLists.txt:130 (project) ```
The clang that cmake is using is still the one you built yourself (/home/neonu/wasi-sdk/build/install/share/cmake/../..//bin/clang++
). Perhaps make is caching that? Try removing your build directory, or if you are doing an in-tree build when removing the cmake cache file.
Thanks, that fixed it, but I did a cmake --build .
(as per the OpenCV docs) and got this error:
In file included from /home/neonu/opencv/3rdparty/openjpeg/openjp2/opj_clock.c:38:
/home/neonu/wasi-sdk-24.0-x86_64-linux/bin/../share/wasi-sysroot/include/wasm32-wasi/sys/resource.h:2:2: error: WASI lacks process-associated clocks; to enable emulation of the `getrusage` function using the wall clock, which isn't sensitive to whether the program is running or suspended, compile with -D_WASI_EMULATED_PROCESS_CLOCKS and link with -lwasi-emulated-process-clocks
2 | #error WASI lacks process-associated clocks; to enable emulation of the `getrusage` function using \
| ^
In file included from /home/neonu/opencv/3rdparty/openjpeg/openjp2/opj_clock.c:39:
/home/neonu/wasi-sdk-24.0-x86_64-linux/bin/../share/wasi-sysroot/include/wasm32-wasi/sys/times.h:2:2: error: WASI lacks process-associated clocks; to enable emulation of the `times` function using the wall clock, which isn't sensitive to whether the program is running or suspended, compile with -D_WASI_EMULATED_PROCESS_CLOCKS and link with -lwasi-emulated-process-clocks
2 | #error WASI lacks process-associated clocks; to enable emulation of the `times` function using \
| ^
/home/neonu/opencv/3rdparty/openjpeg/openjp2/opj_clock.c:55:19: error: variable has incomplete type 'struct rusage'
55 | struct rusage t;
| ^
/home/neonu/opencv/3rdparty/openjpeg/openjp2/opj_clock.c:55:12: note: forward declaration of 'struct rusage'
55 | struct rusage t;
| ^
/home/neonu/opencv/3rdparty/openjpeg/openjp2/opj_clock.c:58:5: error: call to undeclared function 'getrusage'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
58 | getrusage(0, &t);
| ^
4 errors generated.
gmake[2]: *** [3rdparty/openjpeg/openjp2/CMakeFiles/libopenjp2.dir/build.make:258: 3rdparty/openjpeg/openjp2/CMakeFiles/libopenjp2.dir/opj_clock.c.obj] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:3678: 3rdparty/openjpeg/openjp2/CMakeFiles/libopenjp2.dir/all] Error 2
gmake: *** [Makefile:166: all] Error 2
I'm not at all familiar with CMake, but where would the suggested -D_WASI_EMULATED_PROCESS_CLOCKS
arguments and "link with -lwasi-emulated-process-clocks
" go? Or is there another way?
The -D
flag would go in compile flags (CFLAGS) and the -l
flag would go in your linker flags (LDFLAGS).
It seems like I can't put any flags when doing --build
because:
CMake Error: Unknown argument --build
CMake Error: Run 'cmake --help' for all supported options.
And even if I put the -D flag to recompile the thing with cmake -DCMAKE_TOOLCHAIN_FILE=${WASI_SDK_PATH}/share/cmake/wasi-sdk.cmake -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules -D_WASI_EMULATED_PROCESS_CLOCKS ../opencv
, it'll say that:
CMake Error: Parse error in command line argument: _WASI_EMULATED_PROCESS_CLOCKS
Should be: VAR:type=value
CMake Error: Run 'cmake --help' for all supported options.
And do the linker flags go in the same compile command?
cmake ... -DCMAKE_C_FLAGS=-D_WASI_EMULATED_PROCESS_CLOCKS
.
Thanks, so that's solved, but here's another error:
[ 5%] Linking C static library ../lib/libzlib.a
[ 5%] Built target zlib
[ 5%] Processing OpenCL kernels (core)
[ 5%] Building CXX object modules/core/CMakeFiles/opencv_core.dir/src/algorithm.cpp.obj
In file included from /home/neonu/opencv/modules/core/src/algorithm.cpp:43:
In file included from /home/neonu/opencv/modules/core/src/precomp.hpp:53:
/home/neonu/opencv/modules/core/include/opencv2/core/utility.hpp:730:14: error: no type named 'recursive_mutex' in namespace 'std'
730 | typedef std::recursive_mutex Mutex;
| ~~~~~^
/home/neonu/opencv/modules/core/include/opencv2/core/utility.hpp:731:14: error: no template named 'lock_guard' in namespace 'std'
731 | typedef std::lock_guard<cv::Mutex> AutoLock;
| ~~~~~^
2 errors generated.
gmake[2]: *** [modules/core/CMakeFiles/opencv_core.dir/build.make:103: modules/core/CMakeFiles/opencv_core.dir/src/algorithm.cpp.obj] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:3808: modules/core/CMakeFiles/opencv_core.dir/all] Error 2
gmake: *** [Makefile:166: all] Error 2
It couldn't have been an OpenCV error could it? It builds just fine otherwise.
@Brennanzuz The error you are having is beacause wasi-sdk.cmake
does not support threads you must use wasi-sdk-pthread.cmake
instead here is a bash script that succefully build openCV to wasi targert:
Important Note: I couldn't build the repository with -DBUILD_opencv_imgcodecs=ON
and -DWITH_OPENJPEG=OFF
. If someone find a way please contact
Important Note: you have to comment the following lines inside opencv-4.10.0/modules/core/src/parallel.cpp
#
WASI_SDK_PATH=/home/gnery/wasi-sdk-24.0-x86_64-linux
rm -rf ./build
mkdir -p build && cd build
cmake \
-DCMAKE_TOOLCHAIN_FILE=$WASI_SDK_PATH/share/cmake/wasi-sdk-pthread.cmake \
-DCMAKE_INSTALL_PREFIX=/home/gnery/Desktop/Projeto_pesquisa/opencv-wasm-rust/install \
-DOPENCV_EXTRA_MODULES_PATH=/home/gnery/Desktop/Projeto_pesquisa/opencv-wasm-rust/opencv_contrib-4.10.0/modules \
-DENABLE_PIC=FALSE \
-DCMAKE_BUILD_TYPE=Release \
-DCPU_BASELINE='' \
-DCPU_DISPATCH='' \
-DCV_TRACE=ON \
-DBUILD_SHARED_LIBS=OFF \
-DWITH_1394=ON \
-DWITH_ADE=ON \
-DWITH_VTK=OFF \
-DWITH_EIGEN=ON \
-DWITH_FFMPEG=ON \
-DWITH_GSTREAMER=ON \
-DWITH_GTK=OFF \
-DWITH_GTK_2_X=OFF \
-DWITH_IPP=OFF \
-DWITH_JASPER=ON \
-DWITH_JPEG=ON \
-DWITH_WEBP=OFF \
-DWITH_OPENEXR=ON \
-DWITH_OPENGL=ON \
-DWITH_OPENVX=ON \
-DWITH_OPENNI=ON \
-DWITH_OPENNI2=ON \
-DWITH_PNG=ON \
-DWITH_TBB=ON \
-DWITH_OPENMP=ON \
-DWITH_PTHREADS_PF=ON \
-DWITH_TIFF=ON \
-DWITH_V4L=OFF \
-DWITH_OPENCL=ON \
-DWITH_OPENCL_SVM=ON \
-DWITH_OPENCLAMDFFT=ON \
-DWITH_OPENCLAMDBLAS=ON \
-DWITH_GPHOTO2=OFF \
-DWITH_LAPACK=ON \
-DWITH_ITT=ON \
-DWITH_QUIRC=ON \
-DBUILD_ZLIB=ON \
-DBUILD_opencv_apps=OFF \
-DBUILD_opencv_calib3d=ON \
-DBUILD_opencv_dnn=ON \
-DBUILD_opencv_features2d=ON \
-DBUILD_opencv_flann=ON \
-DBUILD_opencv_gapi=ON \
-DBUILD_opencv_ml=ON \
-DBUILD_opencv_photo=ON \
-DBUILD_opencv_imgcodecs=OFF \
-DBUILD_opencv_shape=ON \
-DBUILD_opencv_videoio=ON \
-DBUILD_opencv_videostab=ON \
-DBUILD_opencv_highgui=ON \
-DBUILD_opencv_superres=ON \
-DBUILD_opencv_stitching=ON \
-DBUILD_opencv_java=OFF \
-DBUILD_opencv_js=OFF \
-DBUILD_opencv_python2=OFF \
-DBUILD_opencv_python3=OFF \
-DBUILD_EXAMPLES=OFF \
-DBUILD_PACKAGE=OFF \
-DBUILD_TESTS=OFF \
-DBUILD_PERF_TESTS=OFF \
-DWITH_OPENJPEG=OFF \
../opencv-4.10.0
make -j16
EDIT
i managed to build with -DBUILD_opencv_imgcodecs=ON
and -DWITH_OPENJPEG=OFF
, but i could't manage to build -DBUILD_opencv_apps=ON
. Here is the new script
WASI_SDK_PATH=/home/gnery/wasi-sdk-24.0-x86_64-linux
rm -rf ./build
mkdir -p build && cd build
cmake \
-DCMAKE_TOOLCHAIN_FILE=$WASI_SDK_PATH/share/cmake/wasi-sdk-pthread.cmake \
-DCMAKE_INSTALL_PREFIX=/home/gnery/Desktop/Projeto_pesquisa/opencv-wasm-rust/install \
-DOPENCV_EXTRA_MODULES_PATH=/home/gnery/Desktop/Projeto_pesquisa/opencv-wasm-rust/opencv_contrib-4.10.0/modules \
-DENABLE_PIC=FALSE \
-DCMAKE_BUILD_TYPE=Release \
-DCPU_BASELINE='' \
-DCPU_DISPATCH='' \
-DCV_TRACE=ON \
-DBUILD_SHARED_LIBS=OFF \
-DWITH_1394=ON \
-DWITH_ADE=ON \
-DWITH_VTK=OFF \
-DWITH_EIGEN=ON \
-DWITH_FFMPEG=ON \
-DWITH_GSTREAMER=ON \
-DWITH_GTK=OFF \
-DWITH_GTK_2_X=OFF \
-DWITH_IPP=OFF \
-DWITH_JASPER=ON \
-DWITH_JPEG=ON \
-DWITH_WEBP=OFF \
-DWITH_OPENEXR=ON \
-DWITH_OPENGL=ON \
-DWITH_OPENVX=ON \
-DWITH_OPENNI=ON \
-DWITH_OPENNI2=ON \
-DWITH_PNG=ON \
-DWITH_TBB=ON \
-DWITH_OPENMP=ON \
-DWITH_PTHREADS_PF=ON \
-DWITH_TIFF=ON \
-DWITH_V4L=OFF \
-DWITH_OPENCL=ON \
-DWITH_OPENCL_SVM=ON \
-DWITH_OPENCLAMDFFT=ON \
-DWITH_OPENCLAMDBLAS=ON \
-DWITH_GPHOTO2=OFF \
-DWITH_LAPACK=ON \
-DWITH_ITT=ON \
-DWITH_QUIRC=ON \
-DBUILD_ZLIB=ON \
-DBUILD_opencv_apps=OFF \
-DBUILD_opencv_calib3d=ON \
-DBUILD_opencv_dnn=ON \
-DBUILD_opencv_features2d=ON \
-DBUILD_opencv_flann=ON \
-DBUILD_opencv_gapi=ON \
-DBUILD_opencv_ml=ON \
-DBUILD_opencv_photo=ON \
-DBUILD_opencv_imgcodecs=ON \
-DBUILD_opencv_shape=ON \
-DBUILD_opencv_videoio=ON \
-DBUILD_opencv_videostab=ON \
-DBUILD_opencv_highgui=ON \
-DBUILD_opencv_superres=ON \
-DBUILD_opencv_stitching=ON \
-DBUILD_opencv_java=OFF \
-DBUILD_opencv_js=OFF \
-DBUILD_opencv_python2=OFF \
-DBUILD_opencv_python3=OFF \
-DBUILD_EXAMPLES=OFF \
-DBUILD_PACKAGE=OFF \
-DBUILD_TESTS=OFF \
-DBUILD_PERF_TESTS=OFF \
-DWITH_OPENJPEG=ON \
-DCMAKE_C_FLAGS="-D_WASI_EMULATED_PROCESS_CLOCKS -D_WASI_EMULATED_SIGNAL -mllvm -wasm-enable-sjlj" \
-DCMAKE_CXX_FLAGS="-D_WASI_EMULATED_PROCESS_CLOCKS -D_WASI_EMULATED_SIGNAL -mllvm -wasm-enable-sjlj" \
-DCMAKE_EXE_LINKER_FLAGS="-mllvm -wasm-enable-sjlj -lwasi-emulated-process-clocks -lwasi-emulated-signal" \
../opencv-4.10.0
make -j16
Thank you for checking it out. May I ask where did you get this Projeto_pesquisa
folder?
this is just a simple directory inside my Desktop folder
Oh yup, okay cool it builds, but what am I supposed to get to know that it worked?
It does not work. Because, wasi sdk does not support exceptions. So you will get an linking error saying that exceptions are not defined
@Brennanzuz I managed to build and run opencv with wasi target. As soon as possible I will share a repo showing how to do this
@ggnery I wonder what language you used to call opencv funcs ;-) My bet is Rust, but who knows?
Interesting enough, I have compiled opencv on Intel Mac following instructions from this topic. But have no idea how to call functions from my Rust app and compile it to wasm component compatible with Wasmcloud
Sry, I cant publish this repo due to some licesense problemns. But here is a Idea of how to run compiled code
1)Once you have all opencv static libraries made on previous step, make a CMakeLists.txt importing those like this:
cmake_minimum_required(VERSION 3.10)
set(PROJECT_NAME "test")
project(${PROJECT_NAME})
# Basic Config
set(CMAKE_BUILD_TYPE Release)
set(CMAKE_CXX_STANDARD 17)
#Some usefull flags
set(CMAKE_C_FLAGS "\
-Wl,--initial-memory=4194304000 \
-Wl,--shared-memory \
-mbulk-memory \
-mmutable-globals \
-msimd128 \
-matomics \
-Wl,--allow-undefined \
-Wl,--no-entry \
")
set(CMAKE_CXX_FLAGS "\
-Wl,--initial-memory=4194304000 \
-Wl,--shared-memory \
-mbulk-memory \
-mmutable-globals \
-msimd128 \
-matomics \
-Wl,--allow-undefined \
-Wl,--no-entry \
")
#Very importat because if not set the memory is turned into import by default
set(CMAKE_EXE_LINKER_FLAGS "")
# OPENCV config
set(OPENCV_INSTALL_FOLDER "../lib/install")
set(OPENCV_INCLUDE_FOLDER "${OPENCV_INSTALL_FOLDER}/include/opencv4/")
set(OPENCV_LIB_FOLDER "${OPENCV_INSTALL_FOLDER}/lib")
include_directories(${OPENCV_INCLUDE_FOLDER})
file(GLOB LIB_OPENCV "${OPENCV_LIB_FOLDER}/*.a" "${OPENCV_LIB_FOLDER}/opencv4/3rdparty/*.a")
# Project config
include_directories(src)
file(GLOB_RECURSE SRC_FILES src/*.cpp)
add_executable(${PROJECT_NAME} ${SRC_FILES})
# Link libraries
target_link_libraries(${PROJECT_NAME} ${LIB_OPENCV})
# Set target properties for WebAssembly
set_target_properties(${PROJECT_NAME} PROPERTIES SUFFIX ".wasm")
2) As wasi does not have support to exceptions you MUST define it somewhere in your code by extern "C"
. Here everytime I a exception Is thrown an abort occurs
#include <fstream>
#include <iostream>
extern "C" {
void __cxa_throw(size_t, size_t, size_t) {
abort();
}
void* __cxa_allocate_exception(size_t) {
abort();
}
}
3) Now you are ready to compile c++ code that uses opencv to wasm. For testing I compiled this example to wasm https://github.com/RsGoksel/Cpp-Object-Detection-Yolov5-OpenCV
Note, the performing of this demo inside wasm is terrible, but works. If someone find a way to optimize it, It will help a lot
Hi I am trying to compile the OpenCV source code (with the contrib modules) using WASI-SDK, as I was told by this issue that emscripten could not handle wasmtime (???).
So after following all the instructions of:
to install this SDK, and then going to my
~
folder where myopencv
andopencv_contrib
repos are at and doing:It gave me the following error:
I was hoping that integrating this SDK with a tried and true repository like OpenCV would be exemplary to see how this works.
I am on an Ubuntu 22.04 system. Ask me if you need any other information.
What should I do?