Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add toywasm support #749

Merged
merged 11 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ jobs:
wasm4: bin/wasm4
libretro: lib/wasm4_libretro.so

wasm_backend:
- wasm3

- toywasm

name: Build ${{ matrix.config.artifact }}
runs-on: ${{ matrix.config.os }}
defaults:
Expand All @@ -43,7 +48,7 @@ jobs:

- name: Build
run: |
cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake -B build -DCMAKE_BUILD_TYPE=Release -DWASM_BACKEND=${{ matrix.wasm_backend }}
cmake --build build --config Release --parallel

- name: Install
Expand All @@ -53,13 +58,13 @@ jobs:
uses: actions/upload-artifact@v4.4.0
with:
path: runtimes/native/instdir/${{ matrix.config.wasm4 }}
name: wasm4-${{ matrix.config.artifact }}
name: wasm4-${{ matrix.wasm_backend }}-${{ matrix.config.artifact }}

- name: Upload libretro artifact
uses: actions/upload-artifact@v4.4.0
with:
path: runtimes/native/instdir/${{ matrix.config.libretro }}
name: wasm4-libretro-${{ matrix.config.artifact }}
name: wasm4-libretro-${{ matrix.wasm_backend }}-${{ matrix.config.artifact }}

build-cli:
name: Build w4 CLI
Expand Down Expand Up @@ -95,9 +100,9 @@ jobs:
run: |
mkdir -p assets/natives
ls -R ../artifacts
mv ../artifacts/wasm4-windows/wasm4.exe assets/natives/wasm4-windows.exe
mv ../artifacts/wasm4-mac/wasm4 assets/natives/wasm4-mac
mv ../artifacts/wasm4-linux/wasm4 assets/natives/wasm4-linux
mv ../artifacts/wasm4-wasm3-windows/wasm4.exe assets/natives/wasm4-windows.exe
mv ../artifacts/wasm4-wasm3-mac/wasm4 assets/natives/wasm4-mac
mv ../artifacts/wasm4-wasm3-linux/wasm4 assets/natives/wasm4-linux
chmod +x assets/natives/wasm4-*

- name: Test CLI
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
[submodule "runtimes/native/vendor/cubeb"]
path = runtimes/native/vendor/cubeb
url = https://github.com/mozilla/cubeb
[submodule "runtimes/native/vendor/toywasm"]
path = runtimes/native/vendor/toywasm
url = https://github.com/yamt/toywasm
88 changes: 79 additions & 9 deletions runtimes/native/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
cmake_minimum_required(VERSION 3.7)
project(WASM4)

set(WASM_BACKEND "wasm3" CACHE STRING "webassembly runtime")

set (WASM3 OFF)
set (TOYWASM OFF)
if (WASM_BACKEND STREQUAL "wasm3")
set (WASM3 ON)
elseif (WASM_BACKEND STREQUAL "toywasm")
set (TOYWASM ON)
else ()
message (FATAL_ERROR "Unrecognized WASM_BACKEND value: ${WASM_BACKEND}")
endif ()

# Prevent BUILD_SHARED_LIBS and other options from being cleared by vendor CMakeLists
# https://stackoverflow.com/a/66342383
set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
Expand All @@ -25,6 +37,43 @@ set(BUILD_SHARED_LIBS OFF)
set(BUILD_TESTS OFF)
set(BUILD_TOOLS OFF)

# wasm3
if (WASM3)
file(GLOB M3_SOURCES RELATIVE "${CMAKE_SOURCE_DIR}" "vendor/wasm3/source/*.c")
set(WASM3_SOURCES
src/backend/wasm_wasm3.c
${M3_SOURCES}
)
endif ()

# toywasm
if (TOYWASM)
set(toywasm_tmp_install ${CMAKE_CURRENT_BINARY_DIR}/install)

include(ExternalProject)
ExternalProject_Add(toywasm
SOURCE_DIR "${CMAKE_SOURCE_DIR}/vendor/toywasm"
CMAKE_ARGS
-DCMAKE_INSTALL_PREFIX=${toywasm_tmp_install}
-DBUILD_TESTING=OFF
-DTOYWASM_BUILD_CLI=OFF
-DTOYWASM_BUILD_UNITTEST=OFF
-DTOYWASM_ENABLE_WASI=OFF
-DTOYWASM_ENABLE_WASM_SIMD=OFF
-DTOYWASM_USE_SHORT_ENUMS=OFF
-DTOYWASM_USE_SMALL_CELLS=OFF
BUILD_COMMAND
cmake --build .
INSTALL_COMMAND
cmake --build . --target install
)

set(TOYWASM_SOURCES
src/backend/wasm_toywasm.c
)
endif () # TOYWASM


# MiniFB options
set(MINIFB_BUILD_EXAMPLES OFF)
set(USE_OPENGL_API OFF)
Expand All @@ -51,7 +100,6 @@ add_subdirectory(vendor/cubeb)
endif ()

file(GLOB COMMON_SOURCES RELATIVE "${CMAKE_SOURCE_DIR}" "src/*.c")
file(GLOB M3_SOURCES RELATIVE "${CMAKE_SOURCE_DIR}" "vendor/wasm3/source/*.c")

# Include a strnlen polyfill for some platforms where it's missing (OSX PPC, maybe others)
include(CheckSymbolExists)
Expand All @@ -68,13 +116,24 @@ if (NOT LIBRETRO)

set(MINIFB_SOURCES
src/backend/main.c
src/backend/wasm_wasm3.c
src/backend/window_minifb.c
)
add_executable(wasm4 ${COMMON_SOURCES} ${MINIFB_SOURCES} ${M3_SOURCES})

target_include_directories(wasm4 PRIVATE "${CMAKE_SOURCE_DIR}/vendor/wasm3/source")
target_link_libraries(wasm4 minifb cubeb)
add_executable(wasm4 ${COMMON_SOURCES} ${MINIFB_SOURCES}
$<$<BOOL:${WASM3}>:${WASM3_SOURCES}>
$<$<BOOL:${TOYWASM}>:${TOYWASM_SOURCES}>)
if (TOYWASM)
add_dependencies(wasm4 toywasm)
endif ()

target_include_directories(wasm4 PRIVATE
$<$<BOOL:${WASM3}>:${CMAKE_SOURCE_DIR}/vendor/wasm3/source>
$<$<BOOL:${TOYWASM}>:${toywasm_tmp_install}/include>)
target_link_directories(wasm4 PRIVATE
$<$<BOOL:${TOYWASM}>:${toywasm_tmp_install}/lib>)

target_link_libraries(wasm4 minifb cubeb
$<$<BOOL:${TOYWASM}>:toywasm-core>)
set_target_properties(wasm4 PROPERTIES C_STANDARD 99)
install(TARGETS wasm4)
endif ()
Expand Down Expand Up @@ -117,15 +176,26 @@ endif ()
#
set(LIBRETRO_SOURCES
src/backend/main_libretro.c
src/backend/wasm_wasm3.c
)
if(LIBRETRO_STATIC)
add_library(wasm4_libretro STATIC ${COMMON_SOURCES} ${LIBRETRO_SOURCES} ${M3_SOURCES})
add_library(wasm4_libretro STATIC ${COMMON_SOURCES} ${LIBRETRO_SOURCES}
$<$<BOOL:${WASM3}>:${WASM3_SOURCES}>
$<$<BOOL:${TOYWASM}>:${TOYWASM_SOURCES}>)
else()
add_library(wasm4_libretro SHARED ${COMMON_SOURCES} ${LIBRETRO_SOURCES} ${M3_SOURCES})
add_library(wasm4_libretro SHARED ${COMMON_SOURCES} ${LIBRETRO_SOURCES}
$<$<BOOL:${WASM3}>:${WASM3_SOURCES}>
$<$<BOOL:${TOYWASM}>:${TOYWASM_SOURCES}>)
endif()
target_include_directories(wasm4_libretro PRIVATE "${CMAKE_SOURCE_DIR}/vendor/wasm3/source")
if (TOYWASM)
add_dependencies(wasm4_libretro toywasm)
endif ()
target_include_directories(wasm4_libretro PRIVATE
$<$<BOOL:${WASM3}>:${CMAKE_SOURCE_DIR}/vendor/wasm3/source>
$<$<BOOL:${TOYWASM}>:${toywasm_tmp_install}/include>)
target_link_directories(wasm4_libretro PRIVATE
$<$<BOOL:${TOYWASM}>:${toywasm_tmp_install}/lib>)
target_include_directories(wasm4_libretro PRIVATE "${CMAKE_SOURCE_DIR}/vendor/libretro/include")
target_link_libraries(wasm4_libretro $<$<BOOL:${TOYWASM}>:toywasm-core>)
set_target_properties(wasm4_libretro PROPERTIES C_STANDARD 99)
install(TARGETS wasm4_libretro
ARCHIVE DESTINATION lib
Expand Down
12 changes: 12 additions & 0 deletions runtimes/native/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ cmake -B build
cmake --build build
```

By default, it uses [wasm3] as a WebAssembly runtime.
Alternatively, you can use [toywasm] instead by setting
the `WASM_BACKEND` cmake option:

```shell
cmake -B build -DWASM_BACKEND=toywasm
cmake --build build
```

[wasm3]: https://github.com/wasm3/wasm3
[toywasm]: https://github.com/yamt/toywasm

Running:

```shell
Expand Down
Loading
Loading