Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add full LTO build of wasi-libc and libc++ #436

Merged
merged 6 commits into from
Jul 24, 2024
Merged
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
65 changes: 53 additions & 12 deletions cmake/wasi-sdk-sysroot.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ find_program(MAKE make REQUIRED)
option(WASI_SDK_DEBUG_PREFIX_MAP "Pass `-fdebug-prefix-map` for built artifacts" ON)
option(WASI_SDK_INCLUDE_TESTS "Whether or not to build tests by default" OFF)
option(WASI_SDK_INSTALL_TO_CLANG_RESOURCE_DIR "Whether or not to modify the compiler's resource directory" OFF)
option(WASI_SDK_LTO "Whether or not to build LTO assets" ON)

set(wasi_tmp_install ${CMAKE_CURRENT_BINARY_DIR}/install)
set(wasi_sysroot ${wasi_tmp_install}/share/wasi-sysroot)
Expand Down Expand Up @@ -97,15 +98,27 @@ add_custom_target(compiler-rt DEPENDS compiler-rt-build compiler-rt-post-build)
# wasi-libc build logic
# =============================================================================

function(define_wasi_libc target)
set(build_dir ${CMAKE_CURRENT_BINARY_DIR}/wasi-libc-${target})
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)
set(extra_make_flags THREAD_MODEL=posix)
if(lto)
set(extra_make_flags LTO=full THREAD_MODEL=posix)
else()
set(extra_make_flags THREAD_MODEL=posix)
endif()
elseif(${target} MATCHES p2)
set(extra_make_flags WASI_SNAPSHOT=p2 default libc_so)
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()
set(extra_make_flags default libc_so)
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)
Expand All @@ -114,7 +127,7 @@ function(define_wasi_libc target)
"${CMAKE_C_FLAGS} ${directory_cflags} ${CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE_UPPER}}")
list(JOIN extra_cflags_list " " extra_cflags)

ExternalProject_Add(wasi-libc-${target}
ExternalProject_Add(wasi-libc-${target}${target_suffix}-build
# Currently wasi-libc doesn't support out-of-tree builds so feigh a
# "download command" which copies the source tree to a different location
# so out-of-tree builds are supported.
Expand All @@ -129,7 +142,7 @@ function(define_wasi_libc target)
NM=${CMAKE_NM}
SYSROOT=${wasi_sysroot}
EXTRA_CFLAGS=${extra_cflags}
TARGET_TRIPLE=${target}
TARGET_TRIPLE=${target}
${extra_make_flags}
INSTALL_COMMAND ""
DEPENDS compiler-rt
Expand All @@ -140,6 +153,16 @@ function(define_wasi_libc target)
)
endfunction()

function(define_wasi_libc target)
define_wasi_libc_sub (${target} "" OFF)
if(WASI_SDK_LTO)
define_wasi_libc_sub (${target} "-lto" ON)
endif()

add_custom_target(wasi-libc-${target}
DEPENDS wasi-libc-${target}-build $<$<BOOL:${WASI_SDK_LTO}>:wasi-libc-${target}-lto-build>)
endfunction()

foreach(target IN LISTS WASI_SDK_TARGETS)
define_wasi_libc(${target})
endforeach()
Expand All @@ -148,7 +171,12 @@ endforeach()
# libcxx build logic
# =============================================================================

function(define_libcxx target)
execute_process(
COMMAND ${CMAKE_C_COMPILER} -dumpversion
OUTPUT_VARIABLE llvm_version
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can this also be called something like host_llvm_version or clang_llvm_version to better distinguish from clang_version? (either that or can that variable be renamed?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

my impression is clang_version should be retired. we can use -print-resource-dir instead.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you retire it in this PR then? Otherwise this is creating a confusing state where both are available and they refer to slightly different things.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i don't want to interfere with #445.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Now that #445 is merged could this be retired?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

see #451

OUTPUT_STRIP_TRAILING_WHITESPACE)

function(define_libcxx_sub target target_suffix extra_target_flags extra_libdir_suffix)
if(${target} MATCHES threads)
set(threads ON)
set(pic OFF)
Expand All @@ -158,6 +186,10 @@ function(define_libcxx target)
set(pic ON)
set(target_flags "")
endif()
if(${target_suffix} MATCHES lto)
set(pic OFF)
endif()
list(APPEND target_flags ${extra_target_flags})

set(runtimes "libcxx;libcxxabi")

Expand All @@ -176,7 +208,7 @@ function(define_libcxx target)
set(extra_cxxflags_list ${CMAKE_CXX_FLAGS} ${extra_flags})
list(JOIN extra_cxxflags_list " " extra_cxxflags)

ExternalProject_Add(libcxx-${target}-build
ExternalProject_Add(libcxx-${target}${target_suffix}-build
SOURCE_DIR ${llvm_proj_dir}/runtimes
CMAKE_ARGS
${default_cmake_args}
Expand Down Expand Up @@ -214,8 +246,8 @@ function(define_libcxx target)
-DUNIX:BOOL=ON
-DCMAKE_C_FLAGS=${extra_cflags}
-DCMAKE_CXX_FLAGS=${extra_cxxflags}
-DLIBCXX_LIBDIR_SUFFIX=/${target}
-DLIBCXXABI_LIBDIR_SUFFIX=/${target}
-DLIBCXX_LIBDIR_SUFFIX=/${target}${extra_libdir_suffix}
-DLIBCXXABI_LIBDIR_SUFFIX=/${target}${extra_libdir_suffix}

# See https://www.scivision.dev/cmake-externalproject-list-arguments/ for
# why this is in `CMAKE_CACHE_ARGS` instead of above
Expand All @@ -229,14 +261,23 @@ function(define_libcxx target)
USES_TERMINAL_BUILD ON
USES_TERMINAL_INSTALL ON
)
endfunction()

function(define_libcxx target)
define_libcxx_sub(${target} "" "" "")
if(WASI_SDK_LTO)
# Note: clang knows this /llvm-lto/${llvm_version} convention.
# https://github.com/llvm/llvm-project/blob/llvmorg-18.1.8/clang/lib/Driver/ToolChains/WebAssembly.cpp#L204-L210
define_libcxx_sub(${target} "-lto" "-flto=full" "/llvm-lto/${llvm_version}")
endif()

# As of this writing, `clang++` will ignore the target-specific include dirs
# unless this one also exists:
add_custom_target(libcxx-${target}-extra-dir
COMMAND ${CMAKE_COMMAND} -E make_directory ${wasi_sysroot}/include/c++/v1
COMMENT "creating libcxx-specific header file folder")
add_custom_target(libcxx-${target}
DEPENDS libcxx-${target}-build libcxx-${target}-extra-dir)
DEPENDS libcxx-${target}-build $<$<BOOL:${WASI_SDK_LTO}>:libcxx-${target}-lto-build> libcxx-${target}-extra-dir)
endfunction()

foreach(target IN LISTS WASI_SDK_TARGETS)
Expand Down
2 changes: 1 addition & 1 deletion src/wasi-libc
Submodule wasi-libc updated 1 files
+37 −0 Makefile
Loading