Skip to content

Commit

Permalink
build: add wasm32-wasip1[-threads] targets and use LLVM 18 to build…
Browse files Browse the repository at this point in the history
… library (#119)
  • Loading branch information
toyobayashi committed May 15, 2024
1 parent b3f71d2 commit 1ccf7d2
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 7 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ on:
workflow_dispatch:

env:
WASI_VERSION: '21'
WASI_VERSION_FULL: '21.0'
WASI_SDK_PATH: './wasi-sdk-21.0'
EM_VERSION: '3.1.44'
WASI_VERSION: '22'
WASI_VERSION_FULL: '22.0'
WASI_SDK_PATH: './wasi-sdk-22.0'
EM_VERSION: '3.1.52'
EM_CACHE_FOLDER: 'emsdk-cache'

jobs:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ package-lock.json
/example/build
out
/script/emnapi.zip
/script/*.cmake
*.tgz
16 changes: 14 additions & 2 deletions packages/emnapi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ else()
set(IS_WASM32 OFF)
endif()

if((CMAKE_C_COMPILER_TARGET STREQUAL "wasm32-wasi") OR (CMAKE_C_COMPILER_TARGET STREQUAL "wasm32-wasip1"))
set(IS_WASM32_WASIP1 ON)
else()
set(IS_WASM32_WASIP1 OFF)
endif()

if((CMAKE_C_COMPILER_TARGET STREQUAL "wasm32-wasi-threads") OR (CMAKE_C_COMPILER_TARGET STREQUAL "wasm32-wasip1-threads"))
set(IS_WASM32_WASIP1_THREADS ON)
else()
set(IS_WASM32_WASIP1_THREADS OFF)
endif()

set(UV_SRC
"${CMAKE_CURRENT_SOURCE_DIR}/src/uv/uv-common.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/uv/threadpool.c"
Expand Down Expand Up @@ -133,7 +145,7 @@ if(IS_EMSCRIPTEN)
target_link_options(${EMNAPI_BASIC_TARGET_NAME} INTERFACE "--js-library=${EMNAPI_JS_LIB}")
endif()

if(IS_WASM32 OR (CMAKE_C_COMPILER_TARGET STREQUAL "wasm32-wasi") OR (CMAKE_C_COMPILER_TARGET STREQUAL "wasm32-wasi-threads"))
if(IS_WASM32 OR IS_WASM32_WASIP1 OR IS_WASM32_WASIP1_THREADS)
set(EMNAPI_BUILD_BASIC_MT ON)
else()
set(EMNAPI_BUILD_BASIC_MT OFF)
Expand All @@ -155,7 +167,7 @@ if(EMNAPI_BUILD_BASIC_MT)
endif()
endif()

if(IS_EMSCRIPTEN OR (CMAKE_C_COMPILER_TARGET STREQUAL "wasm32-wasi-threads"))
if(IS_EMSCRIPTEN OR IS_WASM32_WASIP1_THREADS)
set(EMNAPI_BUILD_MT ON)
else()
set(EMNAPI_BUILD_MT OFF)
Expand Down
66 changes: 65 additions & 1 deletion script/release.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@ async function main () {
sysroot
], cwd)

const wasiToolchainFile = `${WASI_SDK_PATH}/share/cmake/wasi-sdk.cmake`

await spawn('cmake', [
...generatorOptions,
`-DCMAKE_TOOLCHAIN_FILE=${WASI_SDK_PATH}/share/cmake/wasi-sdk.cmake`,
`-DCMAKE_TOOLCHAIN_FILE=${wasiToolchainFile}`,
`-DWASI_SDK_PREFIX=${WASI_SDK_PATH}`,
'-DCMAKE_BUILD_TYPE=Release',
'-DCMAKE_VERBOSE_MAKEFILE=1',
Expand All @@ -92,6 +94,36 @@ async function main () {
sysroot
], cwd)

const wasip1ToolchainFile = path.join(__dirname, 'wasip1.cmake')
fs.writeFileSync(
wasip1ToolchainFile,
fs.readFileSync(wasiToolchainFile, 'utf8').replace(/wasm32-wasi/g, 'wasm32-wasip1'),
'utf8'
)

await spawn('cmake', [
...generatorOptions,
`-DCMAKE_TOOLCHAIN_FILE=${wasip1ToolchainFile.replace(/\\/g, '/')}`,
`-DWASI_SDK_PREFIX=${WASI_SDK_PATH}`,
'-DCMAKE_BUILD_TYPE=Release',
'-DCMAKE_VERBOSE_MAKEFILE=1',
`-DNAPI_VERSION=${runtimeNapiVersion}`,
'-H.',
'-Bbuild/wasm32-wasip1'
], cwd)

await spawn('cmake', [
'--build',
'build/wasm32-wasip1'
], cwd)

await spawn('cmake', [
'--install',
'build/wasm32-wasip1',
'--prefix',
sysroot
], cwd)

let WASI_THREADS_CMAKE_TOOLCHAIN_FILE = ''
if (fs.existsSync(path.join(wasiSdkPath, 'share/cmake/wasi-sdk-pthread.cmake'))) {
WASI_THREADS_CMAKE_TOOLCHAIN_FILE = `${WASI_SDK_PATH}/share/cmake/wasi-sdk-pthread.cmake`
Expand Down Expand Up @@ -122,6 +154,36 @@ async function main () {
'--prefix',
sysroot
], cwd)

const wasip1ThreadsToolchainFile = path.join(__dirname, 'wasip1-threads.cmake')
fs.writeFileSync(
wasip1ThreadsToolchainFile,
fs.readFileSync(WASI_THREADS_CMAKE_TOOLCHAIN_FILE, 'utf8').replace(/wasm32-wasi-threads/g, 'wasm32-wasip1-threads'),
'utf8'
)

await spawn('cmake', [
...generatorOptions,
`-DCMAKE_TOOLCHAIN_FILE=${wasip1ThreadsToolchainFile.replace(/\\/g, '/')}`,
`-DWASI_SDK_PREFIX=${WASI_SDK_PATH}`,
'-DCMAKE_BUILD_TYPE=Release',
'-DCMAKE_VERBOSE_MAKEFILE=1',
`-DNAPI_VERSION=${runtimeNapiVersion}`,
'-H.',
'-Bbuild/wasm32-wasip1-threads'
], cwd)

await spawn('cmake', [
'--build',
'build/wasm32-wasip1-threads'
], cwd)

await spawn('cmake', [
'--install',
'build/wasm32-wasip1-threads',
'--prefix',
sysroot
], cwd)
}

await spawn(emcmake, [
Expand Down Expand Up @@ -211,9 +273,11 @@ async function main () {
fs.copySync(path.join(sysroot, 'lib/wasm32-emscripten'), path.join(__dirname, '../packages/emnapi/lib/wasm32-emscripten'))
fs.copySync(path.join(sysroot, 'lib/wasm64-emscripten'), path.join(__dirname, '../packages/emnapi/lib/wasm64-emscripten'))
fs.copySync(path.join(sysroot, 'lib/wasm32-wasi'), path.join(__dirname, '../packages/emnapi/lib/wasm32-wasi'))
fs.copySync(path.join(sysroot, 'lib/wasm32-wasip1'), path.join(__dirname, '../packages/emnapi/lib/wasm32-wasip1'))
fs.copySync(path.join(sysroot, 'lib/wasm32'), path.join(__dirname, '../packages/emnapi/lib/wasm32'))
if (WASI_THREADS_CMAKE_TOOLCHAIN_FILE) {
fs.copySync(path.join(sysroot, 'lib/wasm32-wasi-threads'), path.join(__dirname, '../packages/emnapi/lib/wasm32-wasi-threads'))
fs.copySync(path.join(sysroot, 'lib/wasm32-wasip1-threads'), path.join(__dirname, '../packages/emnapi/lib/wasm32-wasip1-threads'))
}

crossZip.zipSync(sysroot, path.join(__dirname, 'emnapi.zip'))
Expand Down

0 comments on commit 1ccf7d2

Please sign in to comment.