[flang-rt] Add support for using LLVM in-tree libc/libc++ (#131695)

Summary:
This patch adds an interface that uses an in-tree build of LLVM's libc
and libc++.

This is done using the `-DFLANG_RT_LIBC_PROVIDER=llvm` and
`-DFLANG_RT_LIBCXX_PROVIDER=llvm` options. Using `libc` works in terms
of CMake, but the LLVM libc is not yet complete enough to compile all
the files.
This commit is contained in:
Joseph Huber
2025-03-24 06:05:24 -05:00
committed by GitHub
parent df086650e1
commit 038cdd236f
3 changed files with 74 additions and 0 deletions

View File

@@ -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

View File

@@ -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 ()

View File

@@ -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 $<$<COMPILE_LANGUAGE:CXX,C>:-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 $<$<COMPILE_LANGUAGE:CXX,C>:-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 $<$<COMPILE_LANGUAGE:CXX,C>:-nostdinc++>)
endif ()
if (FLANG_RT_HAS_STDLIB_FLAG)
target_compile_options(flang-rt-libc-headers INTERFACE $<$<COMPILE_LANGUAGE:CXX,C>:-stdlib=libc++>)
endif ()
endif ()