Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 21 additions & 20 deletions cmake/wasi-sdk-sysroot.cmake
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went ahead and refactored this a bit to reduce the duplication between the various branches to also pass -fPIC unconditionally on wasip2

Original file line number Diff line number Diff line change
Expand Up @@ -139,31 +139,32 @@ add_custom_target(compiler-rt DEPENDS compiler-rt-build compiler-rt-post-build)
function(define_wasi_libc_sub target target_suffix lto)
set(build_dir ${CMAKE_CURRENT_BINARY_DIR}/wasi-libc-${target}${target_suffix})

if(${target} MATCHES threads)
if(lto)
set(extra_make_flags LTO=full THREAD_MODEL=posix)
else()
set(extra_make_flags THREAD_MODEL=posix)
endif()
elseif(${target} MATCHES p2)
if(lto)
set(extra_make_flags LTO=full WASI_SNAPSHOT=p2 default)
else()
set(extra_make_flags WASI_SNAPSHOT=p2 default libc_so)
endif()
else()
if(lto)
set(extra_make_flags LTO=full default)
else()
set(extra_make_flags default libc_so)
endif()
endif()

string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_UPPER)
get_property(directory_cflags DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY COMPILE_OPTIONS)
list(APPEND directory_cflags -resource-dir ${wasi_resource_dir})
set(extra_cflags_list
"${WASI_SDK_CPU_CFLAGS} ${CMAKE_C_FLAGS} ${directory_cflags} ${CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE_UPPER}}")

set(extra_make_flags default)

# If LTO is enabled then pass that on to wasi-libc, and otherwise make sure to
# build a `libc.so` dynamic library where possible (not compatible with
# threads though)
if(lto)
list(APPEND extra_make_flags LTO=full)
elseif(NOT ${target} MATCHES threads)
list(APPEND extra_make_flags libc_so)
endif()

if(${target} MATCHES threads)
list(APPEND extra_make_flags THREAD_MODEL=posix)
elseif(${target} MATCHES p2)
list(APPEND extra_make_flags WASI_SNAPSHOT=p2)
# Always enable `-fPIC` for the `wasm32-wasip2` target. This makes `libc.a`
# more flexible and usable in dynamic linking situations.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you want to make p1 and p2 different in this regard?
is there any p2-specific motivations to use pic?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're using dynamic libraries in p2 for various languages and it's much easier if everything is -fPIC by default than trying to align the right toolchain flags and binaries. The consequence of this is that there's slight overhead in what the linker generates if you don't end up using a dynamic library, which is also quite common. This slight overhead has led to pushback in the past, so we changed the Rust target, for example, to avoid altering anything about a preexisting target.

Short answer: changing only one target makes this I think less controversial. The p2-specific reason is intended use cases and usage scenarios, but it's not a strong argument.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

certain OSes (eg. netbsd) ship pic versions of static libraries for similar purposes. i feel it's a better approach.

-r--r--r--  1 root  wheel   4123868 Mar 28  2024 /usr/lib/libc.a
-r--r--r--  1 root  wheel   9657674 Jul 17  2018 /usr/lib/libc_pic.a

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you detail why that's a better approach? That still suffers from "align the correct matrix of flags and objects" I'm trying to avoid.

list(APPEND extra_cflags_list -fPIC)
endif()

list(JOIN extra_cflags_list " " extra_cflags)

if(${target} MATCHES threads)
Expand Down
2 changes: 1 addition & 1 deletion src/wasi-libc
Submodule wasi-libc updated 104 files
Loading