Skip to content

Commit 20d1f61

Browse files
committed
Backport from flang_runtime_shared
1 parent 746a9a2 commit 20d1f61

File tree

8 files changed

+64
-57
lines changed

8 files changed

+64
-57
lines changed

flang-rt/CMakeLists.txt

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ set(LLVM_TOOLS_DIR "${LLVM_BINARY_DIR}/bin")
8888
# Determine build and install paths.
8989
# The build path is absolute, but the install dir is relative, CMake's install
9090
# command has to apply CMAKE_INSTALL_PREFIX itself.
91-
get_toolchain_library_subdir(toolchain_lib_subdir)
9291
if (LLVM_TREE_AVAILABLE)
9392
# In a bootstrap build emit the libraries into a default search path in the
9493
# build directory of the just-built compiler. This allows using the
@@ -97,31 +96,28 @@ if (LLVM_TREE_AVAILABLE)
9796
# Despite Clang in the name, get_clang_resource_dir does not depend on Clang
9897
# being added to the build. Flang uses the same resource dir as clang.
9998
include(GetClangResourceDir)
100-
get_clang_resource_dir(FLANG_RT_OUTPUT_DIR PREFIX "${LLVM_LIBRARY_OUTPUT_INTDIR}/..")
101-
get_clang_resource_dir(FLANG_RT_INSTALL_PATH)
102-
103-
extend_path(FLANG_RT_BUILD_TOOLCHAIN_LIB_DIR "${FLANG_RT_OUTPUT_DIR}" "${toolchain_lib_subdir}")
99+
get_clang_resource_dir(FLANG_RT_OUTPUT_RESOURCE_DIR PREFIX "${LLVM_LIBRARY_OUTPUT_INTDIR}/..")
100+
get_clang_resource_dir(FLANG_RT_INSTALL_RESOURCE_PATH)
104101
else ()
105102
# In a standalone runtimes build, do not write into LLVM_BINARY_DIR. It may be
106103
# read-only and/or shared by multiple runtimes with different build
107104
# configurations (e.g. Debug/Release). Use the runtime's own lib dir like any
108105
# non-toolchain library.
109106
# For the install prefix, still use the resource dir assuming that Flang will
110107
# be installed there using the same prefix. This is to not have a difference
111-
# between bootstrap and standalone runtimes builds.
112-
set(FLANG_RT_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}")
113-
set(FLANG_RT_INSTALL_PATH "${CMAKE_INSTALL_LIBDIR}/clang/${LLVM_VERSION_MAJOR}")
114-
115-
extend_path(FLANG_RT_BUILD_TOOLCHAIN_LIB_DIR "${FLANG_RT_OUTPUT_DIR}" "${CMAKE_INSTALL_LIBDIR}")
108+
# between bootstrap and standalone runtimes builds.
109+
set(FLANG_RT_OUTPUT_RESOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}")
110+
set(FLANG_RT_INSTALL_RESOURCE_PATH "${CMAKE_INSTALL_LIBDIR}/clang/${LLVM_VERSION_MAJOR}")
116111
endif ()
112+
cmake_path(NORMAL_PATH FLANG_RT_OUTPUT_RESOURCE_DIR)
113+
cmake_path(NORMAL_PATH FLANG_RT_INSTALL_RESOURCE_PATH)
117114

118-
# Apply the arch-specific library dir with the resource dir.
119-
extend_path(FLANG_RT_INSTALL_TOOLCHAIN_LIB_DIR "${FLANG_RT_INSTALL_PATH}" "${toolchain_lib_subdir}")
120-
121-
cmake_path(NORMAL_PATH FLANG_RT_OUTPUT_DIR)
122-
cmake_path(NORMAL_PATH FLANG_RT_INSTALL_PATH)
123-
cmake_path(NORMAL_PATH FLANG_RT_BUILD_TOOLCHAIN_LIB_DIR)
124-
cmake_path(NORMAL_PATH FLANG_RT_INSTALL_TOOLCHAIN_LIB_DIR)
115+
# Determine subdirectories for build output and install destinations.
116+
get_toolchain_library_subdir(toolchain_lib_subdir)
117+
extend_path(FLANG_RT_OUTPUT_RESOURCE_LIB_DIR "${FLANG_RT_OUTPUT_RESOURCE_DIR}" "${toolchain_lib_subdir}")
118+
extend_path(FLANG_RT_INSTALL_RESOURCE_LIB_PATH "${FLANG_RT_INSTALL_RESOURCE_PATH}" "${toolchain_lib_subdir}")
119+
cmake_path(NORMAL_PATH FLANG_RT_OUTPUT_RESOURCE_LIB_DIR)
120+
cmake_path(NORMAL_PATH FLANG_RT_INSTALL_RESOURCE_LIB_PATH)
125121

126122

127123
#################

flang-rt/cmake/modules/AddFlangRT.cmake

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,6 @@ function (add_flangrt_library name)
4444
")
4545
endif ()
4646

47-
# Also add header files to IDEs to list as part of the library
48-
set_source_files_properties(${ARG_ADDITIONAL_HEADERS} PROPERTIES HEADER_FILE_ONLY ON)
49-
5047
# Forward libtype to add_library
5148
set(extra_args "")
5249
if (ARG_SHARED)
@@ -62,6 +59,9 @@ function (add_flangrt_library name)
6259
list(APPEND extra_args EXCLUDE_FROM_ALL)
6360
endif ()
6461

62+
# Also add header files to IDEs to list as part of the library.
63+
set_source_files_properties(${ARG_ADDITIONAL_HEADERS} PROPERTIES HEADER_FILE_ONLY ON)
64+
6565
add_library(${name} ${extra_args} ${ARG_ADDITIONAL_HEADERS} ${ARG_UNPARSED_ARGUMENTS})
6666

6767
if (ARG_INSTALL_WITH_TOOLCHAIN)
@@ -103,7 +103,7 @@ function (add_flangrt_library name)
103103
)
104104
endif ()
105105

106-
# Flang-rt's public headers
106+
# Flang-RT's public headers
107107
target_include_directories(${name} PRIVATE "${FLANG_RT_SOURCE_DIR}/include")
108108

109109
# For ISO_Fortran_binding.h to be found by the runtime itself (Accessed as #include "flang/ISO_Fortran_binding.h")
@@ -121,12 +121,24 @@ function (add_flangrt_library name)
121121
endif ()
122122

123123
# Flang/Clang (including clang-cl) -compiled programs targeting the MSVC ABI
124-
# should only depend on msv(u)crt. LLVM still emits libgcc/compiler-rt
124+
# should only depend on msvcrt/ucrt. LLVM still emits libgcc/compiler-rt
125125
# functions in some cases like 128-bit integer math (__udivti3, __modti3,
126126
# __fixsfti, __floattidf, ...) that msvc does not support. We are injecting a
127127
# dependency to Compiler-RT's builtin library where these are implemented.
128-
if (MSVC AND (CMAKE_CXX_COMPILER_ID MATCHES ".*Clang") AND FLANG_RT_BUILTINS_LIBRARY)
129-
target_compile_options(${name} PRIVATE "$<$<COMPILE_LANGUAGE:CXX,C>:-Xclang>$<$<COMPILE_LANGUAGE:Fortran>:-Xflang>" "--dependent-lib=${FLANG_RT_BUILTINS_LIBRARY}")
128+
if (MSVC AND CMAKE_CXX_COMPILER_ID MATCHES "Clang")
129+
if (FLANG_RT_BUILTINS_LIBRARY)
130+
target_compile_options(${name} PRIVATE "$<$<COMPILE_LANGUAGE:CXX,C>:-Xclang>" "--dependent-lib=${FLANG_RT_BUILTINS_LIBRARY}")
131+
endif ()
132+
endif ()
133+
if (MSVC AND CMAKE_Fortran_COMPILER_ID STREQUAL "LLVMFlang")
134+
if (FLANG_RT_BUILTINS_LIBRARY)
135+
target_compile_options(${name} PRIVATE "$<$<COMPILE_LANGUAGE:Fortran>:-Xflang>" "--dependent-lib=${FLANG_RT_BUILTINS_LIBRARY}")
136+
else ()
137+
message(WARNING "Did not find libclang_rt.builtins.lib.
138+
LLVM may emit builtins that are not implemented in msvcrt/ucrt and
139+
instead falls back to builtins from Compiler-RT. Linking with ${name}
140+
may result in a linker error.")
141+
endif ()
130142
endif ()
131143

132144
# Non-GTest unittests depend on LLVMSupport
@@ -146,19 +158,15 @@ function (add_flangrt_library name)
146158
if (ARG_INSTALL_WITH_TOOLCHAIN)
147159
set_target_properties(${name}
148160
PROPERTIES
149-
LIBRARY_OUTPUT_DIRECTORY "${FLANG_RT_BUILD_TOOLCHAIN_LIB_DIR}"
150-
ARCHIVE_OUTPUT_DIRECTORY "${FLANG_RT_BUILD_TOOLCHAIN_LIB_DIR}"
151-
RUNTIME_OUTPUT_DIRECTORY "${FLANG_RT_BUILD_TOOLCHAIN_LIB_DIR}"
161+
ARCHIVE_OUTPUT_DIRECTORY "${FLANG_RT_OUTPUT_RESOURCE_LIB_DIR}"
152162
)
153163

154164
install(TARGETS ${name}
155-
LIBRARY DESTINATION "${FLANG_RT_INSTALL_TOOLCHAIN_LIB_DIR}"
156-
ARCHIVE DESTINATION "${FLANG_RT_INSTALL_TOOLCHAIN_LIB_DIR}"
157-
RUNTIME DESTINATION "${FLANG_RT_INSTALL_TOOLCHAIN_LIB_DIR}"
165+
ARCHIVE DESTINATION "${FLANG_RT_INSTALL_RESOURCE_LIB_PATH}"
158166
)
159167
endif ()
160168

161-
# flang-rt should build all the flang-rt targets that are built in an
169+
# flang-rt should build all the Flang-RT targets that are built in an
162170
# 'all' build.
163171
if (NOT ARG_EXCLUDE_FROM_ALL)
164172
add_dependencies(flang-rt ${name})

flang-rt/lib/FortranFloat128Math/CMakeLists.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,16 @@ if (FLANG_RUNTIME_F128_MATH_LIB)
9696

9797
if (WIN32)
9898
# Do not create a FortranFloat128Math library under Windows, the Flang
99-
# driver never links it. Instead, add the sources to the flang_rt itself.
99+
# driver never links it. Instead, add the sources to flang_rt itself.
100100
target_sources(FortranFloat128MathILib INTERFACE ${sources})
101101
target_compile_definitions(FortranFloat128MathILib INTERFACE HAS_QUADMATHLIB)
102102
else ()
103103
add_flangrt_library(FortranFloat128Math STATIC INSTALL_WITH_TOOLCHAIN
104104
${sources})
105-
endif ()
106-
target_include_directories(FortranFloat128Math PRIVATE
107-
"${FLANG_RT_SOURCE_DIR}/lib/flang_rt"
108-
)
105+
target_include_directories(FortranFloat128Math PRIVATE
106+
"${FLANG_RT_SOURCE_DIR}/lib/flang_rt"
107+
)
108+
endif ()
109109
elseif (HAVE_LDBL_MANT_DIG_113)
110110
# We can use 'long double' versions from libc.
111111
check_library_exists(m sinl "" FOUND_LIBM)

flang-rt/lib/Testing/CMakeLists.txt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ file(GLOB_RECURSE public_headers
1212
)
1313

1414
add_flangrt_library(NonGTestTesting EXCLUDE_FROM_ALL LINK_TO_LLVM
15-
${FLANG_SOURCE_DIR}/lib/Testing/testing.cpp
16-
${FLANG_SOURCE_DIR}/lib/Testing/fp-testing.cpp
17-
ADDITIONAL_HEADERS
18-
${public_headers}
19-
)
15+
${FLANG_SOURCE_DIR}/lib/Testing/testing.cpp
16+
${FLANG_SOURCE_DIR}/lib/Testing/fp-testing.cpp
17+
18+
ADDITIONAL_HEADERS
19+
${public_headers}
20+
)

flang-rt/test/Runtime/no-cpp-dep.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ a C compiler.
66
UNSUPPORTED: system-windows
77
88
RUN: %if system-aix %{ export OBJECT_MODE=64 %}
9-
RUN: %cc -std=c99 %s -I%include %libruntime -lm %deplibs \
9+
RUN: %cc -std=c99 %s -I%include -L"%libdir" -lflang_rt -lm %deplibs \
1010
RUN: %if system-aix %{-lpthread %}
1111
*/
1212

flang-rt/test/lit.cfg.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,24 +80,24 @@ def shjoin(args, sep=" "):
8080
extra_args=isysroot_flag,
8181
unresolved="fatal",
8282
),
83-
ToolSubst("%cc", command=config.cc, extra_args=isysroot_flag, unresolved="fatal"),
83+
ToolSubst("%cc",
84+
command=config.cc,
85+
extra_args=isysroot_flag,
86+
unresolved="fatal"
87+
),
8488
]
8589
llvm_config.add_tool_substitutions(tools)
8690

8791
# Let tests find LLVM's standard tools (FileCheck, split-file, not, ...)
8892
llvm_config.with_environment("PATH", config.llvm_tools_dir, append_path=True)
8993

90-
# Library path of libflang_rt.a
91-
config.substitutions.append(("%libdir", config.flang_rt_build_lib_dir))
94+
# Include path for C headers that define Flang's Fortran ABI.
95+
config.substitutions.append(("%include", os.path.join(config.flang_source_dir, "include")))
9296

93-
# Define some variables to help us test that the flang runtime doesn't depend on
94-
# the C++ runtime libraries. For this we need a C compiler.
95-
libruntime = os.path.join(config.flang_rt_build_lib_dir, "libflang_rt.a")
96-
include = os.path.join(config.flang_source_dir, "include")
97-
config.substitutions.append(("%libruntime", libruntime))
98-
config.substitutions.append(("%include", include))
97+
# Library path of libflang_rt.a (for lib search path when using non-Flang driver for linking)
98+
config.substitutions.append(("%libdir", config.flang_rt_output_resource_lib_dir))
9999

100-
# Additional library depedendencies the that flang driver does not add itself.
100+
# Additional library depedendencies the that Flang driver does not add itself.
101101
deplibs = []
102102
if config.flang_rt_experimental_offload_support == "CUDA":
103103
deplibs.append("-lcudart")

flang-rt/test/lit.site.cfg.py.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ config.llvm_tools_dir = "@LLVM_TOOLS_DIR@"
66
config.flang_source_dir = "@FLANG_SOURCE_DIR@"
77
config.flang_rt_source_dir = "@FLANG_RT_SOURCE_DIR@"
88
config.flang_rt_binary_test_dir = os.path.dirname(__file__)
9-
config.flang_rt_build_lib_dir = "@FLANG_RT_BUILD_TOOLCHAIN_LIB_DIR@"
9+
config.flang_rt_output_resource_lib_dir = "@FLANG_RT_OUTPUT_RESOURCE_LIB_DIR@"
1010
config.flang_rt_experimental_offload_support = "@FLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT@"
1111
config.cc = "@CMAKE_C_COMPILER@"
1212
config.flang = "@CMAKE_Fortran_COMPILER@"

flang-rt/unittests/CMakeLists.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# output. Therefore it also needs to include files from LLVM. Unfortunately,
1111
# LLVM/GTest doesn't add the include search path itself. Limiting the scope
1212
# using target_include_directories does not work because with
13-
# LLVM_INSTALL_GTEST=ON, llvm_gtest is an IMPORT library.
13+
# LLVM_INSTALL_GTEST=ON, as llvm_gtest is an IMPORT library.
1414
include_directories("${LLVM_INCLUDE_DIR}" "${LLVM_MAIN_INCLUDE_DIR}")
1515

1616
# Add GTest if not already present.
@@ -91,11 +91,13 @@ function(add_flangrt_nongtest_unittest test_name)
9191
set(suffix .test)
9292
endif()
9393

94-
add_executable(${test_name}${suffix} ${ARG_UNPARSED_ARGUMENTS})
94+
add_executable(${test_name}${suffix} EXCLUDE_FROM_ALL ${ARG_UNPARSED_ARGUMENTS})
9595
set_target_properties(${test_name}${suffix} PROPERTIES FOLDER "Flang-RT/Tests/Unit")
9696

97-
target_include_directories(${test_name}${suffix} PRIVATE "${FLANG_RT_SOURCE_DIR}/include")
98-
target_include_directories(${test_name}${suffix} PRIVATE "${FLANG_SOURCE_DIR}/include")
97+
target_include_directories(${test_name}${suffix} PRIVATE
98+
"${FLANG_RT_SOURCE_DIR}/include"
99+
"${FLANG_SOURCE_DIR}/include"
100+
)
99101
target_link_libraries(${test_name}${suffix} PRIVATE NonGTestTesting ${ARG_LINK_LIBS})
100102

101103
if(NOT ARG_SLOW_TEST)

0 commit comments

Comments
 (0)