Skip to content

Commit

Permalink
WIP adding wasm target
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejurka committed Sep 6, 2023
1 parent eaf637a commit 5bb46de
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 3 deletions.
76 changes: 76 additions & 0 deletions quickjs_make_wasm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/bin/bash

EMSDK_PATH="emsdk"
DESIRED_VERSION="2.0.19-lto"
REPO_DIR="backend-quickjs"

install_emsdk() {
echo "Installing Emscripten SDK..."

# Fetch the latest version of the emsdk (Emscripten SDK) repository
git clone https://github.com/emscripten-core/emsdk.git $EMSDK_PATH

# Fetch the latest registry of available tools
$EMSDK_PATH/emsdk update-tags

# Install the desired version of the SDK
$EMSDK_PATH/emsdk install $DESIRED_VERSION

# Activate the installed SDK version
$EMSDK_PATH/emsdk activate $DESIRED_VERSION

# Source the Emscripten environment into the current shell session
source $EMSDK_PATH/emsdk_env.sh

echo "Emscripten SDK installed successfully!"
}

check_emsdk_version() {
if $EMSDK_PATH/emsdk list 2>&1 | grep -q "$DESIRED_VERSION.*INSTALLED"; then
echo "Emscripten version $DESIRED_VERSION is already installed."
return 0
else
return 1
fi
}

# Check for emsdk installation and correct version
if [ ! -d "$EMSDK_PATH" ]; then
install_emsdk
elif ! check_emsdk_version; then
read -p "Emscripten version $DESIRED_VERSION is not installed. Would you like to install it now? (y/n): " response
if [ "$response" == "y" ]; then
install_emsdk
else
echo "Exiting..."
exit 1
fi
fi

# Source the Emscripten environment
source $EMSDK_PATH/emsdk_env.sh

# Set up Emscripten compiler environment
export CC="emcc"
export CXX="em++"

BUILD_DIR="$REPO_DIR/build_wasm"

# Create build directory
mkdir -p $BUILD_DIR

# Configure cmake for WebAssembly using absolute paths
emcmake cmake $REPO_DIR -B$BUILD_DIR

# Build using cmake
cmake --build $BUILD_DIR --config Release

LIB_OUTPUT_DIR="$REPO_DIR/qjs/quickjs/Lib/WASM"

# Create library output directory
mkdir -p $LIB_OUTPUT_DIR

# Copy the generated .a file
cp $BUILD_DIR/libquickjs.a $LIB_OUTPUT_DIR

echo "Successfully built and copied the WebAssembly library!"
2 changes: 1 addition & 1 deletion unity/cli/cmd.mts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ program
.addOption(
new Option("--platform <platform>", "the target platform")
.default("")
.choices(["win", "osx", "linux", "android", "ios"])
.choices(["win", "osx", "linux", "android", "ios", "web"])
)
.addOption(
new Option("--arch <arch>", "the target architecture")
Expand Down
22 changes: 20 additions & 2 deletions unity/cli/make.mts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,31 @@ const glob = createRequire(fileURLToPath(import.meta.url))('glob');

interface BuildOptions {
config: 'Debug' | 'Release' | "RelWithDebInfo",
platform: 'osx' | 'win' | 'ios' | 'android' | 'linux',
arch: 'x64' | 'ia32' | 'armv7' | 'arm64' | 'auto',
platform: 'osx' | 'win' | 'ios' | 'android' | 'linux' | 'web',
arch: 'x64' | 'ia32' | 'armv7' | 'arm64' | 'auto' | 'wasm',
backend: string
}

//// 脚本 scripts
const platformCompileConfig = {
// ... (other platform configs)
'web': {
'wasm': {
outputPluginPath: 'web/wasm',
hook: function(CMAKE_BUILD_PATH: string, options: BuildOptions, cmakeAddedLibraryName: string, cmakeDArgs: string) {
// We are assuming you are using Emscripten's emcmake tool to use CMake for WebAssembly compilation
const EMSDK_PATH = process.env.EMSDK_PATH || "~/emsdk";
const EMSCRIPTEN_CMAKE_TOOLCHAIN_FILE = `${EMSDK_PATH}/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake`;

// Invoking emcmake with cmake for WebAssembly compilation
assert.equal(0, exec(`${EMSDK_PATH}/upstream/emscripten/emcmake cmake ${cmakeDArgs} -DCMAKE_TOOLCHAIN_FILE=${EMSCRIPTEN_CMAKE_TOOLCHAIN_FILE} -DJS_ENGINE=${options.backend} -H. -B${CMAKE_BUILD_PATH}`).code);
assert.equal(0, exec(`cmake --build ${CMAKE_BUILD_PATH} --config ${options.config}`).code);

// Assuming your output is in the format of .wasm
return `${CMAKE_BUILD_PATH}/${cmakeAddedLibraryName}.wasm`;
}
}
},
'android': {
'armv7': {
outputPluginPath: 'Android/libs/armeabi-v7a/',
Expand Down

0 comments on commit 5bb46de

Please sign in to comment.