Skip to content

Commit 8e18401

Browse files
committed
WIP
1 parent 82aea2b commit 8e18401

File tree

7 files changed

+219
-179
lines changed

7 files changed

+219
-179
lines changed

flang-rt/CMakeLists.txt

Lines changed: 16 additions & 9 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
@@ -99,29 +98,37 @@ if (LLVM_TREE_AVAILABLE)
9998
include(GetClangResourceDir)
10099
get_clang_resource_dir(FLANG_RT_OUTPUT_DIR PREFIX "${LLVM_LIBRARY_OUTPUT_INTDIR}/..")
101100
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}")
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.
108+
# between bootstrap and standalone runtimes builds.
112109
set(FLANG_RT_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}")
113110
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}")
116111
endif ()
112+
cmake_path(NORMAL_PATH FLANG_RT_OUTPUT_DIR)
113+
cmake_path(NORMAL_PATH FLANG_RT_INSTALL_PATH)
117114

118-
# Apply the arch-specific library dir with the resource dir.
115+
# Determine subdirectories for build output and install destinations.
116+
get_toolchain_library_subdir(toolchain_lib_subdir)
117+
get_system_library_subdir(arch_lib_subdir)
118+
extend_path(FLANG_RT_BUILD_TOOLCHAIN_LIB_DIR "${FLANG_RT_OUTPUT_DIR}" "${toolchain_lib_subdir}")
119+
if (LLVM_TREE_AVAILABLE)
120+
extend_path(FLANG_RT_BUILD_LIB_DIR "${FLANG_RT_OUTPUT_DIR}" "${arch_lib_subdir}")
121+
else ()
122+
# Only building for a single target, so no need to write into a arch-specific subirectory.
123+
extend_path(FLANG_RT_BUILD_LIB_DIR "${FLANG_RT_OUTPUT_DIR}" "${CMAKE_INSTALL_LIBDIR}")
124+
endif ()
119125
extend_path(FLANG_RT_INSTALL_TOOLCHAIN_LIB_DIR "${FLANG_RT_INSTALL_PATH}" "${toolchain_lib_subdir}")
126+
extend_path(FLANG_RT_INSTALL_LIB_DIR "${FLANG_RT_INSTALL_PATH}" "${arch_lib_subdir}")
120127

121-
cmake_path(NORMAL_PATH FLANG_RT_OUTPUT_DIR)
122-
cmake_path(NORMAL_PATH FLANG_RT_INSTALL_PATH)
123128
cmake_path(NORMAL_PATH FLANG_RT_BUILD_TOOLCHAIN_LIB_DIR)
129+
cmake_path(NORMAL_PATH FLANG_RT_BUILD_LIB_DIR)
124130
cmake_path(NORMAL_PATH FLANG_RT_INSTALL_TOOLCHAIN_LIB_DIR)
131+
cmake_path(NORMAL_PATH FLANG_RT_INSTALL_LIB_DIR)
125132

126133

127134
#################

flang-rt/cmake/modules/AddFlangRT.cmake

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ function (add_flangrt_library name)
9292

9393
# srctargets: targets that contain source files
9494
# libtargets: static/shared if they are built
95-
# alltargets: static/shared/object if they are built
95+
# alltargets: any add_library target added by this function
9696
set(srctargets "")
9797
set(libtargets "")
9898
set(alltargets "")
@@ -121,81 +121,87 @@ function (add_flangrt_library name)
121121

122122
# Create selected library types.
123123
if (build_object)
124-
add_library(${name_object} OBJECT ${extra_args} ${ARG_ADDITIONAL_HEADERS} ${ARG_UNPARSED_ARGUMENTS})
124+
add_library("${name_object}" OBJECT ${extra_args} ${ARG_ADDITIONAL_HEADERS} ${ARG_UNPARSED_ARGUMENTS})
125125
set_target_properties(${name_object} PROPERTIES
126126
POSITION_INDEPENDENT_CODE ON
127127
FOLDER "Flang-RT/Object Libraries"
128128
)
129129

130130
# Replace arguments for the libraries we are going to create.
131131
set(ARG_ADDITIONAL_HEADERS "")
132-
set(ARG_UNPARSED_ARGUMENTS "$<TARGET_OBJECTS:${objectlib_name}>")
132+
set(ARG_UNPARSED_ARGUMENTS "$<TARGET_OBJECTS:${name_object}>")
133133
endif ()
134134
if (build_static)
135-
add_library(${name_static} STATIC ${extra_args} ${ARG_ADDITIONAL_HEADERS} ${ARG_UNPARSED_ARGUMENTS})
135+
add_library("${name_static}" STATIC ${extra_args} ${ARG_ADDITIONAL_HEADERS} ${ARG_UNPARSED_ARGUMENTS})
136136
endif ()
137137
if (build_shared)
138-
add_library(${name_shared} SHARED ${extra_args} ${ARG_ADDITIONAL_HEADERS} ${ARG_UNPARSED_ARGUMENTS})
138+
add_library("${name_shared}" SHARED ${extra_args} ${ARG_ADDITIONAL_HEADERS} ${ARG_UNPARSED_ARGUMENTS})
139139
endif ()
140140

141-
foreach (name IN LISTS libtargets)
141+
foreach (tgtname IN LISTS libtargets)
142+
if (NOT WIN32)
143+
# Use same stem name for .a and .so. Common in UNIX environments. Not
144+
# allowed with Windows.
145+
set_target_properties(${tgtname} PROPERTIES OUTPUT_NAME "${name}")
146+
endif ()
147+
142148
if (ARG_INSTALL_WITH_TOOLCHAIN)
143-
set_target_properties(${name} PROPERTIES FOLDER "Flang-RT/Toolchain Libraries")
149+
set_target_properties(${tgtname} PROPERTIES FOLDER "Flang-RT/Toolchain Libraries")
144150
else ()
145-
set_target_properties(${name} PROPERTIES FOLDER "Flang-RT/Libraries")
151+
set_target_properties(${tgtname} PROPERTIES FOLDER "Flang-RT/Libraries")
146152
endif ()
147153
endforeach ()
148154

149155
# Define how to compile and link the library.
150156
# Some conceptionally only apply to ${srctargets} or ${libtargets}, but we
151157
# apply them to ${alltargets}. In worst case, they are ignored by CMake.
152-
foreach (name IN LISTS alltargets)
158+
foreach (tgtname IN LISTS alltargets)
153159
# Minimum required C++ version for Flang-RT, even if CMAKE_CXX_STANDARD is defined to something else.
154-
target_compile_features(${name} PRIVATE cxx_std_17)
160+
target_compile_features(${tgtname} PRIVATE cxx_std_17)
155161

156162
# Use compiler-specific options to disable exceptions and RTTI.
157163
if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
158-
target_compile_options(${name} PRIVATE
164+
target_compile_options(${tgtname} PRIVATE
159165
$<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions -fno-rtti -fno-unwind-tables -fno-asynchronous-unwind-tables>
160166
)
161167
elseif (MSVC)
162-
target_compile_options(${name} PRIVATE
168+
target_compile_options(${tgtname} PRIVATE
163169
$<$<COMPILE_LANGUAGE:CXX>:/EHs-c- /GR->
164170
)
165171
elseif (CMAKE_CXX_COMPILER_ID MATCHES "XL")
166-
target_compile_options(${name} PRIVATE
172+
target_compile_options(${tgtname} PRIVATE
167173
$<$<COMPILE_LANGUAGE:CXX>:-qnoeh -qnortti>
168174
)
169175
endif ()
170176

171177
# Also for CUDA source when compiling with FLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT=CUDA
172178
if (CMAKE_CUDA_COMPILER_ID MATCHES "NVIDIA")
173179
# Assuming gcc as host compiler.
174-
target_compile_options(${name} PRIVATE
180+
target_compile_options(${tgtname} PRIVATE
175181
$<$<COMPILE_LANGUAGE:CUDA>:--no-exceptions -Xcompiler -fno-rtti -Xcompiler -fno-unwind-tables -Xcompiler -fno-asynchronous-unwind-tables>
176182
)
177183
else ()
178184
# Assuming a clang-compatible CUDA compiler.
179-
target_compile_options(${name} PRIVATE
185+
target_compile_options(${tgtname} PRIVATE
180186
$<$<COMPILE_LANGUAGE:CUDA>:-fno-exceptions -fno-rtti -fno-unwind-tables -fno-asynchronous-unwind-tables>
181187
)
182188
endif ()
183189

184190
# Flang-RT's public headers
185-
target_include_directories(${name} PRIVATE "${FLANG_RT_SOURCE_DIR}/include")
191+
target_include_directories(${tgtname} PRIVATE "${FLANG_RT_SOURCE_DIR}/include")
186192

187193
# For ISO_Fortran_binding.h to be found by the runtime itself (Accessed as #include "flang/ISO_Fortran_binding.h")
188194
# User applications can use #include <ISO_Fortran_binding.h>
189-
target_include_directories(${name} PRIVATE "${FLANG_SOURCE_DIR}/include")
195+
target_include_directories(${tgtname} PRIVATE "${FLANG_SOURCE_DIR}/include")
190196

191197
# For Flang-RT's configured config.h to be found
192-
target_include_directories(${name} PRIVATE "${FLANG_RT_BINARY_DIR}")
198+
target_include_directories(${tgtname} PRIVATE "${FLANG_RT_BINARY_DIR}")
193199

194200
# Disable libstdc++/libc++ assertions, even in an LLVM_ENABLE_ASSERTIONS
195201
# build, to avoid an unwanted dependency on libstdc++/libc++.so.
196202
if (FLANG_RT_SUPPORTS_UNDEFINE_FLAG)
197-
target_compile_options(${name} PUBLIC -U_GLIBCXX_ASSERTIONS)
198-
target_compile_options(${name} PUBLIC -U_LIBCPP_ENABLE_ASSERTIONS)
203+
target_compile_options(${tgtname} PUBLIC -U_GLIBCXX_ASSERTIONS)
204+
target_compile_options(${tgtname} PUBLIC -U_LIBCPP_ENABLE_ASSERTIONS)
199205
endif ()
200206

201207
# Flang/Clang (including clang-cl) -compiled programs targeting the MSVC ABI
@@ -205,16 +211,16 @@ function (add_flangrt_library name)
205211
# dependency to Compiler-RT's builtin library where these are implemented.
206212
if (MSVC AND CMAKE_CXX_COMPILER_ID MATCHES "Clang")
207213
if (FLANG_RT_BUILTINS_LIBRARY)
208-
target_compile_options(${name} PRIVATE "$<$<COMPILE_LANGUAGE:CXX,C>:-Xclang>" "--dependent-lib=${FLANG_RT_BUILTINS_LIBRARY}")
214+
target_compile_options(${tgtname} PRIVATE "$<$<COMPILE_LANGUAGE:CXX,C>:-Xclang>" "--dependent-lib=${FLANG_RT_BUILTINS_LIBRARY}")
209215
endif ()
210216
endif ()
211217
if (MSVC AND CMAKE_Fortran_COMPILER_ID STREQUAL "LLVMFlang")
212218
if (FLANG_RT_BUILTINS_LIBRARY)
213-
target_compile_options(${name} PRIVATE "$<$<COMPILE_LANGUAGE:Fortran>:-Xflang>" "--dependent-lib=${FLANG_RT_BUILTINS_LIBRARY}")
219+
target_compile_options(${tgtname} PRIVATE "$<$<COMPILE_LANGUAGE:Fortran>:-Xflang>" "--dependent-lib=${FLANG_RT_BUILTINS_LIBRARY}")
214220
else ()
215221
message(WARNING "Did not find libclang_rt.builtins.lib.
216222
LLVM may emit builtins that are not implemented in msvcrt/ucrt and
217-
instead falls back to builtins from Compiler-RT. Linking with ${name}
223+
instead falls back to builtins from Compiler-RT. Linking with ${tgtname}
218224
may result in a linker error.")
219225
endif ()
220226
endif ()
@@ -226,34 +232,32 @@ function (add_flangrt_library name)
226232
else()
227233
llvm_map_components_to_libnames(llvm_libs Support)
228234
endif()
229-
target_link_libraries(${name} PUBLIC ${llvm_libs})
230-
target_include_directories(${name} PUBLIC ${LLVM_INCLUDE_DIRS})
235+
target_link_libraries(${tgtname} PUBLIC ${llvm_libs})
236+
target_include_directories(${tgtname} PUBLIC ${LLVM_INCLUDE_DIRS})
231237
endif ()
232238
endforeach ()
233239

234-
foreach (name IN LISTS libtargets)
240+
foreach (tgtname IN LISTS libtargets)
235241
# If this is part of the toolchain, put it into the compiler's resource
236242
# directory. Otherwise it is part of testing and is not installed at all.
237243
# TODO: Consider multi-configuration builds (MSVC_IDE, "Ninja Multi-Config")
238244
if (ARG_INSTALL_WITH_TOOLCHAIN)
239-
set_target_properties(${name}
245+
set_target_properties(${tgtname}
240246
PROPERTIES
241-
LIBRARY_OUTPUT_DIRECTORY "${FLANG_RT_BUILD_TOOLCHAIN_LIB_DIR}"
242247
ARCHIVE_OUTPUT_DIRECTORY "${FLANG_RT_BUILD_TOOLCHAIN_LIB_DIR}"
243-
RUNTIME_OUTPUT_DIRECTORY "${FLANG_RT_BUILD_TOOLCHAIN_LIB_DIR}"
248+
LIBRARY_OUTPUT_DIRECTORY "${FLANG_RT_BUILD_LIB_DIR}"
244249
)
245250

246-
install(TARGETS ${name}
247-
LIBRARY DESTINATION "${FLANG_RT_INSTALL_TOOLCHAIN_LIB_DIR}"
251+
install(TARGETS ${tgtname}
248252
ARCHIVE DESTINATION "${FLANG_RT_INSTALL_TOOLCHAIN_LIB_DIR}"
249-
RUNTIME DESTINATION "${FLANG_RT_INSTALL_TOOLCHAIN_LIB_DIR}"
253+
LIBRARY DESTINATION "${FLANG_RT_INSTALL_LIB_DIR}"
250254
)
251255
endif ()
252256

253257
# flang-rt should build all the Flang-RT targets that are built in an
254258
# 'all' build.
255259
if (NOT ARG_EXCLUDE_FROM_ALL)
256-
add_dependencies(flang-rt ${name})
260+
add_dependencies(flang-rt ${tgtname})
257261
endif ()
258262
endforeach ()
259263
endfunction (add_flangrt_library)

0 commit comments

Comments
 (0)