diff --git a/flang-rt/CMakeLists.txt b/flang-rt/CMakeLists.txt index a0b998dc3abd9..a01b826686b3a 100644 --- a/flang-rt/CMakeLists.txt +++ b/flang-rt/CMakeLists.txt @@ -139,6 +139,17 @@ cmake_path(NORMAL_PATH FLANG_RT_INSTALL_RESOURCE_LIB_PATH) option(FLANG_RT_INCLUDE_TESTS "Generate build targets for the flang-rt unit and regression-tests." "${LLVM_INCLUDE_TESTS}") +# Provide an interface to link against the LLVM libc/libc++ projects directly. +set(FLANG_RT_SUPPORTED_PROVIDERS system llvm) +set(FLANG_RT_LIBC_PROVIDER "system" CACHE STRING "Specify C library to use. Supported values are ${FLANG_RT_SUPPORTED_PROVIDERS}.") +if (NOT "${FLANG_RT_LIBC_PROVIDER}" IN_LIST FLANG_RT_SUPPORTED_PROVIDERS) + message(FATAL_ERROR "Unsupported library: '${FLANG_RT_RUNTIME_PROVIDER}'. Supported values are ${FLANG_RT_SUPPORTED_PROVIDERS}.") +endif () + +set(FLANG_RT_LIBCXX_PROVIDER "system" CACHE STRING "Specify C++ library to use. Supported values are ${FLANG_RT_SUPPORTED_PROVIDERS}.") +if (NOT "${FLANG_RT_LIBCXX_PROVIDER}" IN_LIST FLANG_RT_SUPPORTED_PROVIDERS) + message(FATAL_ERROR "Unsupported library: '${FLANG_RT_LIBCXX_PROVIDER}'. Supported values are ${FLANG_RT_SUPPORTED_PROVIDERS}.") +endif () option(FLANG_RT_ENABLE_STATIC "Build Flang-RT as a static library." ON) if (WIN32) @@ -244,6 +255,15 @@ check_cxx_compiler_flag("-UTESTFLAG" FLANG_RT_SUPPORTS_UNDEFINE_FLAG) # Check whether -fno-lto is supported. check_cxx_compiler_flag(-fno-lto FLANG_RT_HAS_FNO_LTO_FLAG) +# Check whether -nostdlibinc is supported. +check_cxx_compiler_flag(-nostdlibinc FLANG_RT_HAS_NOSTDLIBINC_FLAG) + +# Check whether -nostdlib is supported. +check_cxx_compiler_flag(-nostdlib FLANG_RT_HAS_NOSTDLIB_FLAG) + +# Check whether -stdlib= is supported. +check_cxx_compiler_flag(-stdlib=platform FLANG_RT_HAS_STDLIB_FLAG) + # Check whether -Wl,--as-needed is supported. check_linker_flag(C "LINKER:--as-needed" LINKER_SUPPORTS_AS_NEEDED) if (LINKER_SUPPORTS_AS_NEEDED) @@ -303,6 +323,8 @@ endif () # Build Preparation # ##################### +include(HandleLibs) + if (FLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT AND FLANG_RT_INCLUDE_TESTS) # If Fortran runtime is built as CUDA library, the linking # of targets that link flang-rt must be done diff --git a/flang-rt/cmake/modules/AddFlangRT.cmake b/flang-rt/cmake/modules/AddFlangRT.cmake index 999128fcd55f0..39d0475b15326 100644 --- a/flang-rt/cmake/modules/AddFlangRT.cmake +++ b/flang-rt/cmake/modules/AddFlangRT.cmake @@ -139,9 +139,11 @@ function (add_flangrt_library name) endif () if (build_static) add_library("${name_static}" STATIC ${extra_args} ${ARG_ADDITIONAL_HEADERS} ${ARG_UNPARSED_ARGUMENTS}) + target_link_libraries("${name_static}" PRIVATE flang-rt-libcxx-headers flang-rt-libc-headers flang-rt-libc-static) endif () if (build_shared) add_library("${name_shared}" SHARED ${extra_args} ${ARG_ADDITIONAL_HEADERS} ${ARG_UNPARSED_ARGUMENTS}) + target_link_libraries("${name_static}" PRIVATE flang-rt-libcxx-headers flang-rt-libc-headers flang-rt-libc-shared) if (Threads_FOUND) target_link_libraries(${name_shared} PUBLIC Threads::Threads) endif () diff --git a/flang-rt/cmake/modules/HandleLibs.cmake b/flang-rt/cmake/modules/HandleLibs.cmake new file mode 100644 index 0000000000000..9987d6f668978 --- /dev/null +++ b/flang-rt/cmake/modules/HandleLibs.cmake @@ -0,0 +1,50 @@ +#===-- cmake/modules/HandleLibs.cmake --------------------------------------===# +# +# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +# See https://llvm.org/LICENSE.txt for license information. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# +#===------------------------------------------------------------------------===# + +# Select the C library to use for building flang-rt. +if (FLANG_RT_LIBC_PROVIDER STREQUAL "system") + add_library(flang-rt-libc-headers INTERFACE) + add_library(flang-rt-libc-static INTERFACE) + add_library(flang-rt-libc-shared INTERFACE) +elseif (FLANG_RT_LIBC_PROVIDER STREQUAL "llvm") + add_library(flang-rt-libc-headers INTERFACE) + target_link_libraries(flang-rt-libc-headers INTERFACE libc-headers) + if (FLANG_RT_HAS_NOSTDLIBINC_FLAG) + target_compile_options(flang-rt-libc-headers INTERFACE $<$:-nostdlibinc>) + endif () + + add_library(flang-rt-libc-static INTERFACE) + if (TARGET libc) + target_link_libraries(flang-rt-libc-static INTERFACE libc) + endif () + if (TARGET libm) + target_link_libraries(flang-rt-libc-static INTERFACE libm) + endif () + if (FLANG_RT_HAS_NOSTDLIB_FLAG) + target_compile_options(flang-rt-libc-headers INTERFACE $<$:-nostdlib>) + endif () + + # TODO: There's no support for building LLVM libc as a shared library yet. + add_library(flang-rt-libc-shared INTERFACE) +endif () + +# Select the C++ library to use for building flang-rt. +if (FLANG_RT_LIBCXX_PROVIDER STREQUAL "system") + add_library(flang-rt-libcxx-headers INTERFACE) +elseif (FLANG_RT_LIBCXX_PROVIDER STREQUAL "llvm") + add_library(flang-rt-libcxx-headers INTERFACE) + target_link_libraries(flang-rt-libcxx-headers INTERFACE cxx-headers) + + if (CXX_SUPPORTS_NOSTDINCXX_FLAG) + target_compile_options(flang-rt-libc-headers INTERFACE $<$:-nostdinc++>) + endif () + + if (FLANG_RT_HAS_STDLIB_FLAG) + target_compile_options(flang-rt-libc-headers INTERFACE $<$:-stdlib=libc++>) + endif () +endif ()