Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
feat(build): initial support for WASM via Emscripten
Browse files Browse the repository at this point in the history
  • Loading branch information
lmichaelis committed Oct 28, 2023
1 parent c2d8b01 commit 221cce3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
12 changes: 10 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ option(PXC_ENABLE_ASAN "phoenix-shared: Enable sanitizers in debug builds." ON)

add_subdirectory(vendor/phoenix)

set(PXC_SOURCES
list(APPEND PXC_SOURCES
src/Animation.cc
src/BspTree.cc
src/Buffer.cc
Expand All @@ -37,7 +37,15 @@ set(PXC_SOURCES

bs_select_cflags(${PXC_ENABLE_ASAN} PXC_COMPILE_FLAGS PXC_LINK_FLAGS)

add_library(phoenix-shared SHARED)
if (EMSCRIPTEN)
list(APPEND PXC_SOURCES src/Main.cc)
list(APPEND PXC_COMPILE_FLAGS -fexceptions)
list(APPEND PXC_LINK_FLAGS -sWASM=1 -sMODULARIZE -sEXPORTED_FUNCTIONS=_malloc,_free -sEXPORTED_RUNTIME_METHODS=ccall,cwrap,addFunction,AsciiToString,setValue,getValue -sSINGLE_FILE=1 -sALLOW_MEMORY_GROWTH=1 -sALLOW_TABLE_GROWTH=1 -fexceptions)
add_executable(phoenix-shared)
else ()
add_library(phoenix-shared SHARED)
endif ()

set_target_properties(phoenix-shared PROPERTIES CXX_VISIBILITY_PRESET hidden VISIBILITY_INLINES_HIDDEN 1)

target_sources(phoenix-shared PRIVATE ${PXC_SOURCES})
Expand Down
9 changes: 8 additions & 1 deletion include/phoenix/cffi/Api.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@
#pragma once
#include <stdint.h>

#ifdef __EMSCRIPTEN__
#include <emscripten/emscripten.h>
#define PXC_PRELUDE EMSCRIPTEN_KEEPALIVE
#else
#define PXC_PRELUDE
#endif

#ifdef __cplusplus
#define PXC_EXTERN extern "C"
#define PXC_EXTERN extern "C" PXC_PRELUDE
#else
#define PXC_EXTERN
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/Buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ uint64_t pxBufferSize(PxBuffer* buffer) {
}

uint8_t* pxBufferArray(PxBuffer* buffer) {
auto* data = new std::uint8_t[buffer->limit()];
auto* data = new std::uint8_t[(size_t) buffer->limit()];
buffer->get(data, buffer->limit());
return data;
}
7 changes: 7 additions & 0 deletions src/Main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright © 2023. GothicKit Contributors
// SPDX-License-Identifier: MIT

// Dummy main for Emscripten!
int main(int, char**) {
return 0;
}

0 comments on commit 221cce3

Please sign in to comment.