@@ -68,6 +68,33 @@ target_compile_options(libc_test_support PRIVATE
6868 -Wno-sign-compare
6969)
7070
71+ function (add_wasilibc_shared_flags target )
72+ add_dependencies (${target} sysroot builtins )
73+ # Using an `INTERFACE` looks to get what we want here which is to rebuild this
74+ # if the library changes but not actually add any arguments to the link-line.
75+ target_link_libraries (${target} INTERFACE c )
76+ endfunction ()
77+
78+ if (NOT TARGET_TRIPLE MATCHES "-threads" )
79+ add_library (libc_test_support_shared SHARED
80+ "${LIBC_TEST} /src/common/path.c"
81+ "${LIBC_TEST} /src/common/print.c"
82+ "${LIBC_TEST} /src/common/rand.c"
83+ "${LIBC_TEST} /src/common/utf8.c"
84+ )
85+ target_include_directories (libc_test_support_shared PUBLIC "${LIBC_TEST} /src/common" )
86+ add_wasilibc_shared_flags (libc_test_support_shared )
87+ # Disable some warnings in this third-party code
88+ target_compile_options (libc_test_support_shared PRIVATE
89+ -fvisibility=default
90+ -fPIC
91+ -Wno-unused-variable
92+ -Wno-unused-parameter
93+ -Wno-unused-function
94+ -Wno-sign-compare
95+ )
96+ endif ()
97+
7198# Adds a new test executable to build
7299#
73100# * `executable_name` must be a unique name and valid cmake target name.
@@ -83,7 +110,7 @@ function(add_test_executable executable_name src)
83110 set (multiValueArgs LDFLAGS CFLAGS)
84111 cmake_parse_arguments (PARSE_ARGV 1 arg "${options} " "${oneValueArgs} " "${multiValueArgs} " )
85112
86- # Build the test exeutable itself and apply all custom options as applicable.
113+ # Build the test executable itself and apply all custom options as applicable.
87114 add_executable (${executable_name} ${src} )
88115 clang_format_target (${executable_name} )
89116 add_wasilibc_flags (${executable_name} )
@@ -352,6 +379,40 @@ if (TARGET_TRIPLE MATCHES "-threads")
352379 target_compile_options (pthread_mutex_busywait.wasm PRIVATE -Wno-unused-variable )
353380endif ()
354381
382+ # ========= shared library tests ================================
383+
384+ function (add_wasilibc_shared_test test_file )
385+ cmake_path (REPLACE_EXTENSION test_file so OUTPUT_VARIABLE test_library )
386+ cmake_path (REPLACE_EXTENSION test_file wasm OUTPUT_VARIABLE test_name )
387+ set (test_file "${CMAKE_CURRENT_SOURCE_DIR} /src/${test_file} " )
388+
389+ add_library (${test_library} SHARED ${test_file} )
390+ target_compile_options (${test_library} PRIVATE -fPIC )
391+ target_link_libraries (${test_library} PRIVATE libc_test_support_shared )
392+ # Here we embed the component type custom section into `${test_library}` using
393+ # `wasm-tools component embed`, then generate the `${test_name}` file by
394+ # linking `${test_library}`, `liblibc_test_support_shared.so`, and `libc.so`
395+ # together using `wasm-tools component link`.
396+ #
397+ # TODO: There must be a better way to express this in CMAKE.
398+ add_custom_command (
399+ TARGET ${test_library}
400+ POST_BUILD
401+ COMMAND ${wasm_tools} component embed --world wasi:cli/command "${CMAKE_CURRENT_SOURCE_DIR} /../wasi/p2/wit" $<TARGET_FILE :${test_library} > -o $<TARGET_FILE :${test_library} >
402+ COMMAND ${wasm_tools} component link $<TARGET_FILE :${test_library} > "${SYSROOT_LIB} /libc.so" $<TARGET_FILE :libc_test_support_shared > -o ${test_name}
403+ DEPENDS engine
404+ )
405+
406+ add_test (
407+ NAME "${test_name} "
408+ COMMAND ${ENGINE} ${test_name}
409+ )
410+ endfunction ()
411+
412+ if (WASI STREQUAL "p2" )
413+ add_wasilibc_shared_test (malloc-shared.c )
414+ endif ()
415+
355416# ========= sockets-related tests ===============================
356417
357418if (NOT (WASI STREQUAL "p1" ))
0 commit comments