Skip to content

Commit

Permalink
seperate host &* cart for wasi-sdk cart compile
Browse files Browse the repository at this point in the history
  • Loading branch information
konsumer committed Jul 8, 2024
1 parent d7bbbae commit 0b3b59e
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
.DS_Store
build
build/
22 changes: 0 additions & 22 deletions CMakeLists.txt

This file was deleted.

5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ This should allow passing pointers back & forth between host & cart.
## build

```
emcmake cmake -B build -GNinja
cmake --build build
./build.sh
```

## host
Expand All @@ -16,5 +15,5 @@ This is a web-based host, compiled with emscripten. It has it's memory exported.

## cart

This is user-code, and must import it's memory (which comes from host.)
This is user-code, and must import it's memory (which comes from host.) It is built with wasi-sdk, for a very low-level no-frills wasm.

13 changes: 13 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh

mkdir -p build

cd host
emcmake cmake -B build -GNinja
cmake --build build
mv build/host.* ../build

cd ../cart
cmake -B build -GNinja
cmake --build build
mv build/cart.* ../build
6 changes: 0 additions & 6 deletions cart.c

This file was deleted.

1 change: 0 additions & 1 deletion cart.txt

This file was deleted.

39 changes: 39 additions & 0 deletions cart/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# build for carts

cmake_minimum_required (VERSION 3.18)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")

set(CMAKE_SYSTEM_NAME WASI)
set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_SYSTEM_PROCESSOR wasm32)
set(triple wasm32-wasi)

# When building from source, WASI_SDK_PREFIX represents the generated directory
if(NOT WASI_SDK_PREFIX)
set(WASI_SDK_PREFIX /opt/wasi-sdk)
endif()

set(CMAKE_C_COMPILER ${WASI_SDK_PREFIX}/bin/clang${WASI_HOST_EXE_SUFFIX})
set(CMAKE_CXX_COMPILER ${WASI_SDK_PREFIX}/bin/clang++${WASI_HOST_EXE_SUFFIX})
set(CMAKE_ASM_COMPILER ${WASI_SDK_PREFIX}/bin/clang${WASI_HOST_EXE_SUFFIX})
set(CMAKE_AR ${WASI_SDK_PREFIX}/bin/llvm-ar${WASI_HOST_EXE_SUFFIX})
set(CMAKE_RANLIB ${WASI_SDK_PREFIX}/bin/llvm-ranlib${WASI_HOST_EXE_SUFFIX})
set(CMAKE_C_COMPILER_TARGET ${triple})
set(CMAKE_CXX_COMPILER_TARGET ${triple})
set(CMAKE_ASM_COMPILER_TARGET ${triple})

# use only sysroot
set(HAVE_FLAG_SEARCH_PATHS_FIRST 0)
set(CMAKE_C_LINK_FLAGS "")
set(CMAKE_CXX_LINK_FLAGS "")
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

include_directories(${CMAKE_CURRENT_SOURCE_DIR})
set(CMAKE_EXECUTABLE_SUFFIX ".wasm")

add_executable(cart cart.c)
target_link_options(cart PRIVATE -Wl,--initial-memory=128057344 -Wl,--import-memory)
9 changes: 9 additions & 0 deletions cart/cart.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#define WASM_IMPORT(n) __attribute__((import_module("env"), import_name(n)))

WASM_IMPORT("logtest")
void logtest(char* message);

int main(int argc, char* argv[]) {
logtest("I am inside the cart!");
return 0;
}
2 changes: 0 additions & 2 deletions host.txt

This file was deleted.

17 changes: 17 additions & 0 deletions host/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
cmake_minimum_required(VERSION 3.18)

project(wasm_shared-host
DESCRIPTION "null0 engine experiment: shared memory"
HOMEPAGE_URL "https://github.com/notnullgames/wasm-shared"
VERSION 0.0.1
LANGUAGES C
)

if(NOT EMSCRIPTEN)
message(FATAL_ERROR "You must compile with emscripten.")
endif()

set(CMAKE_EXECUTABLE_SUFFIX ".mjs")

add_executable(host host.c)
target_link_options(host PRIVATE -sINITIAL_MEMORY=128MB -sEXPORTED_FUNCTIONS=_logtest -sEXPORTED_RUNTIME_METHODS=wasmMemory)
File renamed without changes.
13 changes: 10 additions & 3 deletions test.htm
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@
</body>

<script type="module">
import loadHost from './build/wasm_shared-host.mjs'
import loadHost from './build/host.mjs'


// this will take an emscripten host, and setup imports for plain-wasm cart
function getImports(host) {
const out = {
wasi_snapshot_preview1: {
args_get(){},
args_sizes_get() {
return 0
},
proc_exit(){}
},
env: {
memory: host.wasmMemory
}
Expand All @@ -27,9 +34,9 @@
}


const cart = (await WebAssembly.instantiateStreaming(fetch("./build/wasm_shared-cart.wasm"), getImports(await loadHost()))).instance.exports
const cart = (await WebAssembly.instantiateStreaming(fetch("./build/cart.wasm"), getImports(await loadHost()))).instance.exports

// call cart's test, which will call host's logtest
cart.test()
cart._start()

</script>

0 comments on commit 0b3b59e

Please sign in to comment.