From cec1e49eec481a6333c218726ecaba8e4f447a07 Mon Sep 17 00:00:00 2001 From: srcejon <57259258+srcejon@users.noreply.github.com> Date: Tue, 21 May 2024 11:43:04 +0100 Subject: [PATCH] Add Emscripten (WebAssembly) support (#27) --- .github/workflows/builds.yml | 28 ++++++++++++++++++++++++++++ CMakeLists.txt | 9 +++++++++ 2 files changed, 37 insertions(+) diff --git a/.github/workflows/builds.yml b/.github/workflows/builds.yml index 18152c5..33191e3 100644 --- a/.github/workflows/builds.yml +++ b/.github/workflows/builds.yml @@ -264,6 +264,34 @@ jobs: fail: true # No test runs/results: cross-compiled Android binaries are not runnable on a host machine + emscripten-build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + path: src + - name: Setup emsdk + uses: mymindstorm/setup-emsdk@v14 + - name: Verify emsdk + run: emcc -v + - name: Install dependencies + run: | + sudo apt update + sudo apt install ninja-build + - name: Configure + run: | + rm -rf build install + echo "EMSDK:" $EMSDK + cmake -B build/static -S src -GNinja -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=install/static -DLIBUSB_BUILD_EXAMPLES=OFF -DLIBUSB_BUILD_TESTING=OFF -DLIBUSB_BUILD_SHARED_LIBS=OFF -DCMAKE_TOOLCHAIN_FILE=$EMSDK/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake + - name: Build Static + working-directory: build/static + run: ninja install + - name: Check artifacts + uses: andstor/file-existence-action@v2 + with: + files: "install/static/lib/libusb-1.0.a, \ + install/static/include/libusb-1.0/libusb.h" + fail: true publish-test-results: name: "Publish Tests Results" diff --git a/CMakeLists.txt b/CMakeLists.txt index 16fa95a..0fb6b07 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,10 @@ project(usb-1.0 VERSION ${LIBUSB_VERSION} LANGUAGES C ) +if(EMSCRIPTEN) + set(CMAKE_CXX_STANDARD 20) + enable_language(CXX) +endif() # This function generates all the local variables what end up getting written to config. # We use a function as any vars set in this context don't mess with the rest of the file. @@ -207,6 +211,11 @@ else() target_sources(usb-1.0 PRIVATE "${LIBUSB_ROOT}/os/openbsd_usb.c" ) + elseif(EMSCRIPTEN) + target_sources(usb-1.0 PRIVATE + "${LIBUSB_ROOT}/os/emscripten_webusb.cpp" + ) + target_compile_options(usb-1.0 PRIVATE -pthread) else() message(FATAL_ERROR "Unsupported target platform: ${CMAKE_SYSTEM_NAME}") endif()