Skip to content

Commit

Permalink
Add Emscripten backend for WebAssembly + WebUSB support
Browse files Browse the repository at this point in the history
Fixes libusb#501
Closes libusb#1057
  • Loading branch information
RReverser authored and tormodvolden committed Jun 26, 2022
1 parent 4689bd5 commit ff8fe93
Show file tree
Hide file tree
Showing 7 changed files with 705 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ Makefile.in
*.la
*.lo
*.o
*.js
*.wasm
*.html
libtool
ltmain.sh
missing
Expand Down
3 changes: 2 additions & 1 deletion README
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
[![Coverity Scan Build Status](https://scan.coverity.com/projects/2180/badge.svg)](https://scan.coverity.com/projects/libusb-libusb)

libusb is a library for USB device access from Linux, macOS,
Windows, OpenBSD/NetBSD, Haiku and Solaris userspace.
Windows, OpenBSD/NetBSD, Haiku, Solaris userspace, and WebAssembly
via WebUSB.
It is written in C (Haiku backend in C++) and licensed under the GNU
Lesser General Public License version 2.1 or, at your option, any later
version (see [COPYING](COPYING)).
Expand Down
18 changes: 17 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ case $host in
backend=haiku
platform=posix
;;
wasm32-**)
AC_MSG_RESULT([Emscripten])
backend=emscripten
platform=posix
;;
*-linux* | *-uclinux*)
dnl on Android Linux, some functions are in different places
case $host in
Expand Down Expand Up @@ -138,7 +143,11 @@ esac
if test "x$platform" = xposix; then
AC_DEFINE([PLATFORM_POSIX], [1], [Define to 1 if compiling for a POSIX platform.])
AC_CHECK_TYPES([nfds_t], [], [], [[#include <poll.h>]])
AC_CHECK_FUNCS([pipe2])
if test "x$backend" != xemscripten; then
# pipe2 is detected as present on Emscripten, but it isn't actually ported and always
# returns an error. https://github.com/emscripten-core/emscripten/issues/14824
AC_CHECK_FUNCS([pipe2])
fi
dnl Some compilers do not support the '-pthread' option so check for it here
saved_CFLAGS="${CFLAGS}"
CFLAGS="-Wall -Werror -pthread"
Expand Down Expand Up @@ -217,6 +226,11 @@ windows)
AC_DEFINE([_WIN32_WINNT], [_WIN32_WINNT_VISTA], [Define to the oldest supported Windows version.])
LT_LDFLAGS="${LT_LDFLAGS} -avoid-version -Wl,--add-stdcall-alias"
;;
emscripten)
AC_SUBST(EXEEXT, [.html])
# Note: LT_LDFLAGS is not enough here because we need link flags for executable.
AM_LDFLAGS="${AM_LDFLAGS} --bind -s ASYNCIFY -s ASSERTIONS -s ALLOW_MEMORY_GROWTH -s INVOKE_RUN=0 -s EXPORTED_RUNTIME_METHODS=['callMain']"
;;
*)
dnl no special handling required
;;
Expand Down Expand Up @@ -372,6 +386,7 @@ AM_CONDITIONAL([OS_NULL], [test "x$backend" = xnull])
AM_CONDITIONAL([OS_OPENBSD], [test "x$backend" = xopenbsd])
AM_CONDITIONAL([OS_SUNOS], [test "x$backend" = xsunos])
AM_CONDITIONAL([OS_WINDOWS], [test "x$backend" = xwindows])
AM_CONDITIONAL([OS_EMSCRIPTEN], [test "x$backend" = xemscripten])
AM_CONDITIONAL([PLATFORM_POSIX], [test "x$platform" = xposix])
AM_CONDITIONAL([PLATFORM_WINDOWS], [test "x$platform" = xwindows])
AM_CONDITIONAL([USE_UDEV], [test "x$use_udev" = xyes])
Expand Down Expand Up @@ -399,6 +414,7 @@ AM_CXXFLAGS="-std=${c_dialect}++11 ${EXTRA_CFLAGS} ${SHARED_CFLAGS} -Wmissing-de
AC_SUBST(AM_CXXFLAGS)

AC_SUBST(LT_LDFLAGS)
AC_SUBST(AM_LDFLAGS)

AC_SUBST([EXTRA_LDFLAGS])

Expand Down
7 changes: 7 additions & 0 deletions libusb/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ OS_DARWIN_SRC = os/darwin_usb.h os/darwin_usb.c
OS_HAIKU_SRC = os/haiku_usb.h os/haiku_usb_backend.cpp \
os/haiku_pollfs.cpp os/haiku_usb_raw.h os/haiku_usb_raw.cpp
OS_LINUX_SRC = os/linux_usbfs.h os/linux_usbfs.c
OS_EMSCRIPTEN_SRC = os/emscripten_webusb.cpp
OS_NETBSD_SRC = os/netbsd_usb.c
OS_NULL_SRC = os/null_usb.c
OS_OPENBSD_SRC = os/openbsd_usb.c
Expand Down Expand Up @@ -48,6 +49,12 @@ OS_SRC += os/linux_netlink.c
endif
endif

if OS_EMSCRIPTEN
noinst_LTLIBRARIES = libusb_emscripten.la
libusb_emscripten_la_SOURCES = $(OS_EMSCRIPTEN_SRC)
libusb_1_0_la_LIBADD = libusb_emscripten.la
endif

if OS_NETBSD
OS_SRC = $(OS_NETBSD_SRC)
endif
Expand Down
Loading

0 comments on commit ff8fe93

Please sign in to comment.