Skip to content

Commit

Permalink
Add Emscripten (WebAssembly) support (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
srcejon authored May 21, 2024
1 parent c07bf58 commit 8f0b4a3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 8f0b4a3

Please sign in to comment.