@@ -26,6 +26,15 @@ if (CMAKE_HOST_WIN32)
2626 set (CMAKE_INCLUDE_SYSTEM_FLAG_C "-isystem " )
2727endif ()
2828
29+ set (TARGET_TRIPLE "wasm32-wasip1" CACHE STRING "WASI target to test" )
30+ set (CMAKE_C_COMPILER_TARGET ${TARGET_TRIPLE} )
31+
32+ # We're building libc, and the compiler being used may not have a wasm sysroot
33+ # for libc, so skip this check. Otherwise the `project` call below attempts a
34+ # link to see if the compiler is working and if that fails it doesn't mean it
35+ # doesn't work. Let the build keep going to see what happens.
36+ set (CMAKE_C_COMPILER_WORKS 1)
37+
2938project (wasi-libc LANGUAGES C ASM )
3039
3140if (NOT CMAKE_C_COMPILER_ID MATCHES Clang)
@@ -36,7 +45,6 @@ message(STATUS "Found executable for `nm`: ${CMAKE_NM}")
3645message (STATUS "Found executable for `ar`: ${CMAKE_AR } " )
3746message (STATUS "Found executable for `ranlib`: ${CMAKE_RANLIB } " )
3847
39- set (TARGET_TRIPLE "wasm32-wasip1" CACHE STRING "WASI target to test" )
4048# Note: thin LTO here is just for experimentation. It has known issues:
4149# - https://github.com/llvm/llvm-project/issues/91700
4250# - https://github.com/llvm/llvm-project/issues/91711
@@ -51,6 +59,7 @@ option(BUILD_SHARED "Whether or not to build shared libraries" ON)
5159option (CHECK_SYMBOLS "Whether or not to check the exported symbols of libc.a" OFF )
5260set (WASI_SDK_VERSION "" CACHE STRING "Version information for wasi-sdk to embed in headers" )
5361option (ENABLE_WERROR "Whether to compile with `-Werror`" OFF )
62+ option (ENABLE_COOP_THREADS "Whether to compile with cooperative threads support" OFF )
5463
5564if (TARGET_TRIPLE MATCHES "-threads$" )
5665 set (WASI p1)
@@ -86,10 +95,10 @@ else()
8695endif ()
8796
8897if (TARGET_TRIPLE MATCHES "-threads$" )
89- set (THREADS ON )
9098 add_compile_options (-mthread-model posix -pthread -ftls-model=local-exec -matomics )
99+ elseif (ENABLE_COOP_THREADS)
100+ add_compile_options (-mthread-model posix -pthread -ftls-model=local-exec )
91101else ()
92- set (THREADS OFF )
93102 add_compile_options (-mthread-model single )
94103endif ()
95104
@@ -209,6 +218,28 @@ if (ENABLE_WERROR)
209218 add_compile_options (-Werror )
210219endif ()
211220
221+ # =============================================================================
222+ # Detection of a libcall thread context for wasip3
223+
224+ include (CheckSourceCompiles )
225+ set (CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
226+ check_source_compiles (C [[
227+ #ifndef __wasm_libcall_thread_context__
228+ #error "missing define"
229+ #endif
230+ ]] HAVE_WASM_LIBCALL_THREAD_CONTEXT )
231+
232+ if (ENABLE_COOP_THREADS)
233+ if (NOT WASI STREQUAL "p3" )
234+ message (FATAL_ERROR "Cooperative threads are only supported on WASIp3" )
235+ endif ()
236+ if (NOT HAVE_WASM_LIBCALL_THREAD_CONTEXT)
237+ message (FATAL_ERROR "Cooperative threads require clang 23 or later" )
238+ endif ()
239+ set (__wasi_cooperative_threads__ ON )
240+ else ()
241+ endif ()
242+
212243# =============================================================================
213244# Helper functions for adding libraries.
214245
@@ -259,7 +290,7 @@ function(add_internal_shared_library name)
259290 add_library (${name} SHARED EXCLUDE_FROM_ALL ${ARGN} )
260291 set_pic (${name} )
261292 target_link_libraries (${name} PUBLIC musl-top-half-interface )
262- add_dependencies (${name} sysroot-c builtins )
293+ add_dependencies (${name} sysroot-c builtins sysroot-empty-pthread )
263294 target_compile_options (${name} PRIVATE -fvisibility=default )
264295 set_target_properties (${name} PROPERTIES NO_SONAME 1)
265296 clang_format_target (${name} )
0 commit comments