Skip to content

Commit 038cdd2

Browse files
authored
[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.
1 parent df08665 commit 038cdd2

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

flang-rt/CMakeLists.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,17 @@ cmake_path(NORMAL_PATH FLANG_RT_INSTALL_RESOURCE_LIB_PATH)
139139

140140
option(FLANG_RT_INCLUDE_TESTS "Generate build targets for the flang-rt unit and regression-tests." "${LLVM_INCLUDE_TESTS}")
141141

142+
# Provide an interface to link against the LLVM libc/libc++ projects directly.
143+
set(FLANG_RT_SUPPORTED_PROVIDERS system llvm)
144+
set(FLANG_RT_LIBC_PROVIDER "system" CACHE STRING "Specify C library to use. Supported values are ${FLANG_RT_SUPPORTED_PROVIDERS}.")
145+
if (NOT "${FLANG_RT_LIBC_PROVIDER}" IN_LIST FLANG_RT_SUPPORTED_PROVIDERS)
146+
message(FATAL_ERROR "Unsupported library: '${FLANG_RT_RUNTIME_PROVIDER}'. Supported values are ${FLANG_RT_SUPPORTED_PROVIDERS}.")
147+
endif ()
148+
149+
set(FLANG_RT_LIBCXX_PROVIDER "system" CACHE STRING "Specify C++ library to use. Supported values are ${FLANG_RT_SUPPORTED_PROVIDERS}.")
150+
if (NOT "${FLANG_RT_LIBCXX_PROVIDER}" IN_LIST FLANG_RT_SUPPORTED_PROVIDERS)
151+
message(FATAL_ERROR "Unsupported library: '${FLANG_RT_LIBCXX_PROVIDER}'. Supported values are ${FLANG_RT_SUPPORTED_PROVIDERS}.")
152+
endif ()
142153

143154
option(FLANG_RT_ENABLE_STATIC "Build Flang-RT as a static library." ON)
144155
if (WIN32)
@@ -244,6 +255,15 @@ check_cxx_compiler_flag("-UTESTFLAG" FLANG_RT_SUPPORTS_UNDEFINE_FLAG)
244255
# Check whether -fno-lto is supported.
245256
check_cxx_compiler_flag(-fno-lto FLANG_RT_HAS_FNO_LTO_FLAG)
246257

258+
# Check whether -nostdlibinc is supported.
259+
check_cxx_compiler_flag(-nostdlibinc FLANG_RT_HAS_NOSTDLIBINC_FLAG)
260+
261+
# Check whether -nostdlib is supported.
262+
check_cxx_compiler_flag(-nostdlib FLANG_RT_HAS_NOSTDLIB_FLAG)
263+
264+
# Check whether -stdlib= is supported.
265+
check_cxx_compiler_flag(-stdlib=platform FLANG_RT_HAS_STDLIB_FLAG)
266+
247267
# Check whether -Wl,--as-needed is supported.
248268
check_linker_flag(C "LINKER:--as-needed" LINKER_SUPPORTS_AS_NEEDED)
249269
if (LINKER_SUPPORTS_AS_NEEDED)
@@ -303,6 +323,8 @@ endif ()
303323
# Build Preparation #
304324
#####################
305325

326+
include(HandleLibs)
327+
306328
if (FLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT AND FLANG_RT_INCLUDE_TESTS)
307329
# If Fortran runtime is built as CUDA library, the linking
308330
# of targets that link flang-rt must be done

flang-rt/cmake/modules/AddFlangRT.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,11 @@ function (add_flangrt_library name)
139139
endif ()
140140
if (build_static)
141141
add_library("${name_static}" STATIC ${extra_args} ${ARG_ADDITIONAL_HEADERS} ${ARG_UNPARSED_ARGUMENTS})
142+
target_link_libraries("${name_static}" PRIVATE flang-rt-libcxx-headers flang-rt-libc-headers flang-rt-libc-static)
142143
endif ()
143144
if (build_shared)
144145
add_library("${name_shared}" SHARED ${extra_args} ${ARG_ADDITIONAL_HEADERS} ${ARG_UNPARSED_ARGUMENTS})
146+
target_link_libraries("${name_static}" PRIVATE flang-rt-libcxx-headers flang-rt-libc-headers flang-rt-libc-shared)
145147
if (Threads_FOUND)
146148
target_link_libraries(${name_shared} PUBLIC Threads::Threads)
147149
endif ()
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#===-- cmake/modules/HandleLibs.cmake --------------------------------------===#
2+
#
3+
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
# See https://llvm.org/LICENSE.txt for license information.
5+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
#
7+
#===------------------------------------------------------------------------===#
8+
9+
# Select the C library to use for building flang-rt.
10+
if (FLANG_RT_LIBC_PROVIDER STREQUAL "system")
11+
add_library(flang-rt-libc-headers INTERFACE)
12+
add_library(flang-rt-libc-static INTERFACE)
13+
add_library(flang-rt-libc-shared INTERFACE)
14+
elseif (FLANG_RT_LIBC_PROVIDER STREQUAL "llvm")
15+
add_library(flang-rt-libc-headers INTERFACE)
16+
target_link_libraries(flang-rt-libc-headers INTERFACE libc-headers)
17+
if (FLANG_RT_HAS_NOSTDLIBINC_FLAG)
18+
target_compile_options(flang-rt-libc-headers INTERFACE $<$<COMPILE_LANGUAGE:CXX,C>:-nostdlibinc>)
19+
endif ()
20+
21+
add_library(flang-rt-libc-static INTERFACE)
22+
if (TARGET libc)
23+
target_link_libraries(flang-rt-libc-static INTERFACE libc)
24+
endif ()
25+
if (TARGET libm)
26+
target_link_libraries(flang-rt-libc-static INTERFACE libm)
27+
endif ()
28+
if (FLANG_RT_HAS_NOSTDLIB_FLAG)
29+
target_compile_options(flang-rt-libc-headers INTERFACE $<$<COMPILE_LANGUAGE:CXX,C>:-nostdlib>)
30+
endif ()
31+
32+
# TODO: There's no support for building LLVM libc as a shared library yet.
33+
add_library(flang-rt-libc-shared INTERFACE)
34+
endif ()
35+
36+
# Select the C++ library to use for building flang-rt.
37+
if (FLANG_RT_LIBCXX_PROVIDER STREQUAL "system")
38+
add_library(flang-rt-libcxx-headers INTERFACE)
39+
elseif (FLANG_RT_LIBCXX_PROVIDER STREQUAL "llvm")
40+
add_library(flang-rt-libcxx-headers INTERFACE)
41+
target_link_libraries(flang-rt-libcxx-headers INTERFACE cxx-headers)
42+
43+
if (CXX_SUPPORTS_NOSTDINCXX_FLAG)
44+
target_compile_options(flang-rt-libc-headers INTERFACE $<$<COMPILE_LANGUAGE:CXX,C>:-nostdinc++>)
45+
endif ()
46+
47+
if (FLANG_RT_HAS_STDLIB_FLAG)
48+
target_compile_options(flang-rt-libc-headers INTERFACE $<$<COMPILE_LANGUAGE:CXX,C>:-stdlib=libc++>)
49+
endif ()
50+
endif ()

0 commit comments

Comments
 (0)