Skip to content

Commit f40f715

Browse files
committed
Add wasi and emscripten minimal support
- wasi and emscripten cmake toolchains - emscripten CI for macOS
1 parent 6bab847 commit f40f715

File tree

6 files changed

+145
-14
lines changed

6 files changed

+145
-14
lines changed

.github/workflows/cmake.yml

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131

3232
- name: Setup MacOS
3333
if: startsWith(matrix.os, 'macOS')
34-
run: brew install automake autoconf ninja cmake
34+
run: brew upgrade && brew install automake autoconf ninja cmake emscripten wasi-libc wasi-runtimes llvm
3535

3636
- name: Setup Ubuntu
3737
if: startsWith(matrix.os, 'ubuntu')
@@ -41,15 +41,33 @@ jobs:
4141
uses: lukka/run-cmake@v10
4242
id: runcmake-ninja
4343
with:
44-
configurePreset: 'ninja'
45-
buildPreset: 'ninja-release'
46-
testPreset: 'ninja-release'
44+
configurePreset: "ninja"
45+
buildPreset: "ninja-release"
46+
testPreset: "ninja-release"
4747

4848
- name: Run CMake+MSVC+CTest to generate/build/test.
4949
uses: lukka/run-cmake@v10
5050
if: startsWith(matrix.os, 'win')
5151
id: runcmake-msvc
5252
with:
53-
configurePreset: 'msvc'
54-
buildPreset: 'msvc-release'
55-
testPreset: 'msvc-release'
53+
configurePreset: "msvc"
54+
buildPreset: "msvc-release"
55+
testPreset: "msvc-release"
56+
57+
- name: Build emscripten plugin template.
58+
uses: lukka/run-cmake@v10
59+
if: startsWith(matrix.os, 'macOS')
60+
id: runcmake-emscripten
61+
with:
62+
configurePreset: "emscripten"
63+
buildPreset: "emscripten-release"
64+
65+
# TODO: enable it, unfortunately it currently fails (only on github, not my on OS) with:
66+
# clang: error: unable to execute command: posix_spawn failed: No such file or directory
67+
# - name: Build wasi plugin template.
68+
# uses: lukka/run-cmake@v10
69+
# if: startsWith(matrix.os, 'macOS')
70+
# id: runcmake-wasi
71+
# with:
72+
# configurePreset: "wasi"
73+
# buildPreset: "wasi-release"

CMakeLists.txt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,22 @@ if (${CLAP_BUILD_TESTS})
7878
clap_compile_cpp(cpp11 cc 11 11)
7979
clap_compile_cpp(cpp14 cc 11 14)
8080
if(${CMAKE_VERSION} VERSION_LESS "3.21")
81-
message(STATUS "Skipping C17 tests due to older CMAKE_VERSION ${CMAKE_VERSION}")
81+
message(STATUS "Skipping C17 tests due to older CMAKE_VERSION ${CMAKE_VERSION}")
8282
else()
83-
clap_compile_cpp(c17 c 17 17)
83+
clap_compile_cpp(c17 c 17 17)
8484
endif()
8585
clap_compile_cpp(cpp17 cc 17 17)
8686
clap_compile_cpp(cpp20 cc 17 20)
8787

8888
check_include_file(threads.h CLAP_HAS_THREADS_H)
8989

90-
add_library(clap-plugin-template MODULE EXCLUDE_FROM_ALL src/plugin-template.c)
90+
if(CMAKE_SYSTEM_NAME STREQUAL "WASI")
91+
set(CLAP_LIBRARY_TYPE STATIC)
92+
else()
93+
set(CLAP_LIBRARY_TYPE MODULE)
94+
endif()
95+
96+
add_library(clap-plugin-template ${CLAP_LIBRARY_TYPE} EXCLUDE_FROM_ALL src/plugin-template.c)
9197
target_link_libraries(clap-plugin-template PRIVATE clap)
9298
set_target_properties(clap-plugin-template PROPERTIES C_STANDARD 11)
9399
add_dependencies(clap-tests clap-plugin-template)
@@ -114,5 +120,8 @@ if (${CLAP_BUILD_TESTS})
114120
)
115121
elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows")
116122
set_target_properties(clap-plugin-template PROPERTIES SUFFIX ".clap" PREFIX "")
123+
elseif(CMAKE_SYSTEM_NAME STREQUAL "WASI" OR CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
124+
target_link_options(clap-plugin-template PRIVATE --no-entry)
117125
endif()
126+
118127
endif()

CMakePresets.json

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,42 @@
3939
"value": true
4040
}
4141
}
42+
},
43+
{
44+
"name": "wasi",
45+
"displayName": "Ninja Clang WASM32/WASI",
46+
"description": "Configure and generate Ninja project files for all configurations, using clang and targetting wasm32 and wasi",
47+
"binaryDir": "${sourceDir}/builds/${presetName}",
48+
"generator": "Ninja Multi-Config",
49+
"cacheVariables": {
50+
"CMAKE_EXPORT_COMPILE_COMMANDS": {
51+
"type": "boolean",
52+
"value": true
53+
},
54+
"CLAP_BUILD_TESTS": {
55+
"type": "boolean",
56+
"value": true
57+
},
58+
"CMAKE_TOOLCHAIN_FILE": "${sourceDir}/cmake/toolchains/clang-wasm32-wasi.cmake"
59+
}
60+
},
61+
{
62+
"name": "emscripten",
63+
"displayName": "emscripten",
64+
"description": "Configure and generate Ninja project files for all configurations, using emscripten",
65+
"binaryDir": "${sourceDir}/builds/${presetName}",
66+
"generator": "Ninja Multi-Config",
67+
"cacheVariables": {
68+
"CMAKE_EXPORT_COMPILE_COMMANDS": {
69+
"type": "boolean",
70+
"value": true
71+
},
72+
"CLAP_BUILD_TESTS": {
73+
"type": "boolean",
74+
"value": true
75+
},
76+
"CMAKE_TOOLCHAIN_FILE": "${sourceDir}/cmake/toolchains/emscripten.cmake"
77+
}
4278
}
4379
],
4480
"buildPresets": [
@@ -48,15 +84,31 @@
4884
"displayName": "Build ninja-release",
4985
"description": "Build ninja Release configuration",
5086
"configuration": "RelWithDebInfo",
51-
"targets": ["clap-tests"]
87+
"targets": ["clap-tests", "clap-plugin-template"]
5288
},
5389
{
5490
"name": "msvc-release",
5591
"configurePreset": "msvc",
5692
"displayName": "Build msvc-release",
5793
"description": "Build msvc Release configuration",
5894
"configuration": "RelWithDebInfo",
59-
"targets": ["clap-tests"]
95+
"targets": ["clap-tests", "clap-plugin-template"]
96+
},
97+
{
98+
"name": "wasi-release",
99+
"configurePreset": "wasi",
100+
"displayName": "Build wasi-release",
101+
"description": "Build wasi Release configuration",
102+
"configuration": "RelWithDebInfo",
103+
"targets": ["clap-tests", "clap-plugin-template"]
104+
},
105+
{
106+
"name": "emscripten-release",
107+
"configurePreset": "emscripten",
108+
"displayName": "Build emscripten-release",
109+
"description": "Build emscripten Release configuration",
110+
"configuration": "RelWithDebInfo",
111+
"targets": ["clap-tests", "clap-plugin-template"]
60112
}
61113
],
62114
"testPresets": [
@@ -71,4 +123,4 @@
71123
"configuration": "RelWithDebInfo"
72124
}
73125
]
74-
}
126+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Simple toolchain example that targets wasm32 using wasi
2+
# On archlinux:
3+
# pacman -S clang wasi-compiler-rt wasi-libc wasi-libc++ wasi-libc++abi
4+
# On macOS:
5+
# homebrew install wasi-libc wasi-runtimes llvm
6+
#
7+
# For other OSes, make a copy of this file and do the necessary adjustments.
8+
9+
set(CMAKE_SYSTEM_NAME "WASI")
10+
set(CMAKE_CROSSCOMPILING TRUE)
11+
12+
set(CMAKE_C_COMPILER "clang")
13+
set(CMAKE_CXX_COMPILER "clang++")
14+
15+
if(CMAKE_HOST_SYSTEM_NAME STREQUAL Linux)
16+
set(CMAKE_SYSROOT "/usr/share/wasi-sysroot")
17+
elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL Darwin)
18+
set(CMAKE_SYSROOT "/opt/homebrew/share/wasi-sysroot")
19+
set(CMAKE_C_COMPILER "/opt/homebrew/opt/llvm/bin/clang")
20+
set(CMAKE_CXX_COMPILER "/opt/homebrew/opt/llvm/bin/clang++")
21+
set(CMAKE_RANLIB "/opt/homebrew/opt/llvm/bin/llvm-ranlib")
22+
endif()
23+
24+
# -nostdlib
25+
set(CMAKE_C_FLAGS_INIT "--target=wasm32-wasi")
26+
set(CMAKE_CXX_FLAGS_INIT "--target=wasm32-wasi")
27+
set(CMAKE_EXE_LINKER_FLAGS_INIT "-Wl,--export-all -Wl,--no-entry -lpthread")
28+
set(CMAKE_MODULE_LINKER_FLAGS_INIT "-Wl,--export-all -Wl,--no-entry -lpthread")
29+
set(CMAKE_SHARED_LINKER_FLAGS_INIT "-Wl,--export-all -Wl,--no-entry -lpthread")
30+
31+
set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)

cmake/toolchains/emscripten.cmake

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Simple toolchain example that targets wasm32 using emscripten
2+
# On archlinux:
3+
# pacman -S clang emscripten
4+
# On macOS:
5+
# brew install emscripten
6+
#
7+
# For other OSes, make a copy of this file and do the necessary adjustments.
8+
9+
set(CMAKE_SYSTEM_NAME "Emscripten")
10+
set(CMAKE_CROSSCOMPILING TRUE)
11+
12+
if(CMAKE_HOST_SYSTEM_NAME STREQUAL Linux)
13+
set(CMAKE_C_COMPILER "/usr/lib/emscripten/emcc")
14+
set(CMAKE_CXX_COMPILER "/usr/lib/emscripten/em++")
15+
elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL Darwin)
16+
set(CMAKE_C_COMPILER "/opt/homebrew/bin/emcc")
17+
set(CMAKE_CXX_COMPILER "/opt/homebrew/bin/em++")
18+
else()
19+
set(CMAKE_C_COMPILER "emcc")
20+
set(CMAKE_CXX_COMPILER "em++")
21+
endif()

src/plugin-template.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <stdio.h>
99
#include <assert.h>
1010

11-
#if __STDC_VERSION__ >= 201112L && !defined (__STDC_NO_THREADS__) && defined (CLAP_HAS_THREADS_H)
11+
#if __STDC_VERSION__ >= 201112L && !defined (__STDC_NO_THREADS__) && defined (CLAP_HAS_THREADS_H) && !defined (__wasi__)
1212
# define CLAP_HAS_THREAD
1313
# include <threads.h>
1414
#endif

0 commit comments

Comments
 (0)