Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
GordonSmith committed Mar 25, 2024
1 parent 86262cd commit 9a96204
Show file tree
Hide file tree
Showing 22 changed files with 116 additions and 1,025 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ jobs:
run: |
cmake -G Ninja -S . -B build -DENABLE_TEST_COVERAGE=1 -DCMAKE_BUILD_TYPE=Debug
- name: build-wasm
run: ./test/wasm/build.sh

- name: build
run: cmake --build build --parallel

Expand Down
49 changes: 49 additions & 0 deletions test/wasm/.devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# See: https://github.com/devcontainers/images/blob/main/src/cpp/.devcontainer/Dockerfile
ARG VARIANT=ubuntu-22.04
FROM mcr.microsoft.com/devcontainers/base:${VARIANT}
USER root

# Install needed packages. Use a separate RUN statement to add your own dependencies.
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
&& apt-get -y install cmake curl git tar xz-utils ninja-build \
&& apt-get autoremove -y && apt-get clean -y && rm -rf /var/lib/apt/lists/*

ARG USERNAME=vscode

# Install wasi-sdk https://github.com/WebAssembly/wasi-sdk
ARG WASI_VERSION="21"
ENV WASI_VERSION_FULL="${WASI_VERSION}.0"
ENV WASI_LOCATION="${LOCATION:-"/usr/local"}/lib"
ENV WASI_FILE="wasi-sdk-${WASI_VERSION_FULL}-linux.tar.gz"
ENV WASI_SDK_PREFIX="/usr/local/lib/wasi-sdk-${WASI_VERSION_FULL}"
RUN curl -L https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-${WASI_VERSION}/${WASI_FILE} --output ${WASI_FILE}
RUN tar -C ${WASI_LOCATION} -xvf ${WASI_FILE}
RUN rm ${WASI_FILE}

# Install wit-bindgen https://github.com/bytecodealliance/wit-bindgen
ARG WIT_BINDGEN_VERSION="0.22.0"
RUN curl -L https://github.com/bytecodealliance/wit-bindgen/releases/download/v${WIT_BINDGEN_VERSION}/wit-bindgen-${WIT_BINDGEN_VERSION}-x86_64-linux.tar.gz --output wit-bindgen-${WIT_BINDGEN_VERSION}-x86_64-linux.tar.gz
RUN tar -xvzf wit-bindgen-${WIT_BINDGEN_VERSION}-x86_64-linux.tar.gz
RUN cp wit-bindgen-${WIT_BINDGEN_VERSION}-x86_64-linux/wit-bindgen ${LOCATION}/bin/wit-bindgen
RUN rm -r wit-bindgen-${WIT_BINDGEN_VERSION}-x86_64-linux*

# Install wasm-tools https://github.com/bytecodealliance/wasm-tools
ARG WASM_TOOLS_VERSION="1.201.0"
RUN curl -L https://github.com/bytecodealliance/wasm-tools/releases/download/v${WASM_TOOLS_VERSION}/wasm-tools-${WASM_TOOLS_VERSION}-x86_64-linux.tar.gz --output wasm-tools-${WASM_TOOLS_VERSION}-x86_64-linux.tar.gz
RUN tar -xvzf wasm-tools-${WASM_TOOLS_VERSION}-x86_64-linux.tar.gz
RUN cp wasm-tools-${WASM_TOOLS_VERSION}-x86_64-linux/wasm-tools ${LOCATION}/bin/wasm-tools
RUN rm -r wasm-tools-${WASM_TOOLS_VERSION}-x86_64-linux*

# Install wasi adapter https://github.com/bytecodealliance/wasmtime
ARG WASMTIME_VERSION="19.0.0"
RUN curl -L -O https://github.com/bytecodealliance/wasmtime/releases/download/v${WASMTIME_VERSION}/wasi_snapshot_preview1.reactor.wasm
RUN mv wasi_snapshot_preview1.reactor.wasm ${LOCATION}/lib/wasi_snapshot_preview1.reactor.wasm

# # Install wasmtime
# ENV WASMTIME_VERSION="${WASMTIME_VERSION:-"v9.0.4"}"
# ENV INSTALLER=./wasmtime-install.sh
# RUN curl https://wasmtime.dev/install.sh -L --output ${INSTALLER}
# RUN chmod a+x ${INSTALLER}
# RUN ${INSTALLER} --version ${WASMTIME_VERSION}
# RUN cp ${HOME}/.wasmtime/bin/wasmtime /usr/bin/wasmtime

29 changes: 29 additions & 0 deletions test/wasm/.devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "C/C++ WASM",
"build": {
"dockerfile": "./Dockerfile",
"context": "."
},
"features": {
"ghcr.io/devcontainers/features/common-utils:2": {
"installZsh": "true",
"username": "vscode",
"userUid": "1000",
"userGid": "1000",
"upgradePackages": "true"
}
},
"customizations": {
"vscode": {
"extensions": [
"ms-vscode.cpptools-extension-pack",
"dtsvet.vscode-wasm"
]
}
},
"remoteUser": "vscode",
"remoteEnv": {
"PATH": "${containerEnv:PATH}:${WASI_SDK_PREFIX}/bin"
},
"postCreateCommand": "echo 'Done'"
}
1 change: 1 addition & 0 deletions test/wasm/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
File renamed without changes.
File renamed without changes.
7 changes: 5 additions & 2 deletions wasm/CMakeLists.txt → test/wasm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ add_custom_command(
add_custom_target(wit-generate ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/tests.c)

set(CMAKE_EXECUTABLE_SUFFIX ".wasm")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -nostartfiles -fno-exceptions --sysroot=/${WASI_SDK_PREFIX}/share/wasi-sysroot -Wl,--no-entry")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions --sysroot=/${WASI_SDK_PREFIX}/share/wasi-sysroot")

include_directories(
${CMAKE_CURRENT_BINARY_DIR}
Expand All @@ -33,10 +33,13 @@ add_executable(wasmtests
${CMAKE_CURRENT_BINARY_DIR}/tests.h
)

target_link_options(wasmtests PRIVATE "LINKER:--no-entry")


add_custom_command(
TARGET wasmtests POST_BUILD
# OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/wasmtests-component.wasm
COMMAND wasm-tools component new ${CMAKE_CURRENT_BINARY_DIR}/wasmtests.wasm -o ${CMAKE_CURRENT_BINARY_DIR}/wasmtests-component.wasm --adapt wasi_snapshot_preview1=${CMAKE_CURRENT_SOURCE_DIR}/wasi_snapshot_preview1.reactor.wasm
COMMAND echo "wasm-tools component new ${CMAKE_CURRENT_BINARY_DIR}/wasmtests.wasm -o ${CMAKE_CURRENT_BINARY_DIR}/wasmtests-component.wasm --adapt wasi_snapshot_preview1=${WASI_SDK_PREFIX}/wasi_snapshot_preview1.reactor.wasm"
)

target_link_libraries(wasmtests
Expand Down
4 changes: 2 additions & 2 deletions wasm/CMakePresets.json → test/wasm/CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
{
"name": "wasi-sdk",
"displayName": "wasi-sdk",
"toolchainFile": "/usr/local/lib/wasi-sdk-20.0/share/cmake/wasi-sdk.cmake",
"toolchainFile": "$env{WASI_SDK_PREFIX}/share/cmake/wasi-sdk.cmake",
"binaryDir": "${sourceDir}/build",
"installDir": "${sourceDir}/build/stage",
"cacheVariables": {
"WASI_SDK_PREFIX": "/usr/local/lib/wasi-sdk-20.0"
"WASI_SDK_PREFIX": "$env{WASI_SDK_PREFIX}"
}
}
],
Expand Down
File renamed without changes.
24 changes: 24 additions & 0 deletions test/wasm/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]:-$0}"; )" &> /dev/null && pwd 2> /dev/null; )"
ROOT_DIR="$(pwd)"

echo "SCRIPT_DIR: ${SCRIPT_DIR}"
echo "ROOT_DIR: $ROOT_DIR"

function cleanup()
{
CONTAINER_ID=$(jq -r .containerId ${SCRIPT_DIR}/up.json)
docker rm -f "${CONTAINER_ID}"
rm ${SCRIPT_DIR}/up.json
}

trap cleanup EXIT

npx -y @devcontainers/cli up --workspace-folder ${SCRIPT_DIR} | jq . > "${SCRIPT_DIR}/up.json"

CMAKE_OPTIONS="-G Ninja -DCMAKE_BUILD_TYPE=MinSizeRel -DCMAKE_TOOLCHAIN_FILE=\${WASI_SDK_PREFIX}/share/cmake/wasi-sdk.cmake -DWASI_SDK_PREFIX=\${WASI_SDK_PREFIX}"

npx -y @devcontainers/cli exec --workspace-folder ${SCRIPT_DIR} mkdir -p ./build
npx -y @devcontainers/cli exec --workspace-folder ${SCRIPT_DIR} sh -c 'cmake -S . -B ./build -G Ninja -DCMAKE_BUILD_TYPE=MinSizeRel -DCMAKE_TOOLCHAIN_FILE=${WASI_SDK_PREFIX}/share/cmake/wasi-sdk.cmake -DWASI_SDK_PREFIX=${WASI_SDK_PREFIX}'
npx -y @devcontainers/cli exec --workspace-folder ${SCRIPT_DIR} cmake --build ./build --parallel
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions wasm/string.cpp → test/wasm/string.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "util.hpp"
#include <vector>
#include <cstdint>

static uint32_t tally = 0;

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion test/wasmtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ TEST_CASE("component-model-cpp")
wasmtime::Engine engine;

wasmtime::Store store(engine);
std::vector<uint8_t> _contents = readWasmBinaryToBuffer("wasm/build/wasmtests.wasm");
std::vector<uint8_t> _contents = readWasmBinaryToBuffer("test/wasm/build/wasmtests.wasm");
const wasmtime::Span<uint8_t> &contents = _contents;
auto module = wasmtime::Module::compile(engine, contents).unwrap();

Expand Down
21 changes: 0 additions & 21 deletions wasm/.devcontainer/devcontainer.json

This file was deleted.

24 changes: 0 additions & 24 deletions wasm/build.sh

This file was deleted.

Loading

0 comments on commit 9a96204

Please sign in to comment.