Skip to content

Commit

Permalink
Add LTO build of libc++
Browse files Browse the repository at this point in the history
  • Loading branch information
yamt committed Jul 12, 2024
1 parent 9255d10 commit 2b97025
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions cmake/wasi-sdk-sysroot.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,12 @@ endforeach()
# libcxx build logic
# =============================================================================

function(define_libcxx target)
execute_process(
COMMAND ${CMAKE_C_COMPILER} -dumpversion
OUTPUT_VARIABLE llvm_version
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 @@ -165,6 +170,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 @@ -175,7 +184,7 @@ function(define_libcxx target)
set(extra_cxxflags_list ${CMAKE_CXX_FLAGS} ${target_flags} --target=${target} ${dir_compile_opts} ${dir_link_opts})
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 @@ -213,8 +222,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 @@ -228,14 +237,19 @@ function(define_libcxx target)
USES_TERMINAL_BUILD ON
USES_TERMINAL_INSTALL ON
)
endfunction()

function(define_libcxx target)
define_libcxx_sub(${target} "" "" "")
define_libcxx_sub(${target} "-lto" "-flto=full" "/llvm-lto/${llvm_version}")

# 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 libcxx-${target}-lto-build libcxx-${target}-extra-dir)
endfunction()

foreach(target IN LISTS WASI_SDK_TARGETS)
Expand Down

0 comments on commit 2b97025

Please sign in to comment.