Skip to content

Commit 7ac1f60

Browse files
authored
[llvm-libgcc][CMake] Refactor llvm-libgcc (llvm#65455)
There are some issues in `llvm-libgcc` before this patch: Commit c5a20b5 ([llvm-libgcc] initial commit) uses `$<TARGET_OBJECTS:unwind_static>` to get libunwind objects, which is empty. The built library is actually a shared version of libclang_rt.builtins. When configuring with `llvm/CMakeLists.txt`, target `llvm-libgcc` requires a corresponding target in `llvm-libgcc/CMakeLists.txt`. Per target installation is not handled by `llvm-libgcc`, which is not consistent with `libunwind`. This patch fixes those issues by: Reusing target `unwind_shared` in `libunwind`, linking `compiler-rt.builtins` objects into it, and applying version script. Adding target `llvm-libgcc`, creating symlinks, and utilizing cmake's dependency and component mechanism to ensure link targets will be built and installed along with symlinks. Mimicking `libunwind` to handle per target installation. It is quite hard to set necessary options without further modifying the order of runtime projects in `runtimes/CMakeLists.txt`. So though this patch reveals the possibility of co-existence of `llvm-libgcc` and `compiler-rt`/`libunwind` in `LLVM_ENABLE_RUNTIMES`, we still inhibit it to minimize influence on other projects, considering that `llvm-libgcc` is only intended for toolchain vendors, and not for casual use.
1 parent 44532a9 commit 7ac1f60

File tree

5 files changed

+134
-122
lines changed

5 files changed

+134
-122
lines changed

llvm-libgcc/CMakeLists.txt

+134-19
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,38 @@
1-
cmake_minimum_required(VERSION 3.20.0)
1+
#===============================================================================
2+
# Setup Project
3+
#===============================================================================
24

3-
if (NOT IS_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/../libunwind")
4-
message(FATAL_ERROR "llvm-libgcc requires being built in a monorepo layout with libunwind available")
5-
endif()
5+
cmake_minimum_required(VERSION 3.20.0)
66

77
set(LLVM_COMMON_CMAKE_UTILS "${CMAKE_CURRENT_SOURCE_DIR}/../cmake")
88

9-
list(APPEND CMAKE_MODULE_PATH
9+
# Check if llvm-libgcc is built as a standalone project
10+
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR OR LLVM_LIBGCC_STANDALONE_BUILD)
11+
project(llvm-libgcc LANGUAGES C CXX ASM)
12+
set(COMPILER_RT_STANDALONE_BUILD ON)
13+
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
14+
set(LLVM_LIBGCC_COMPILER_RT_BINARY_DIR "compiler-rt")
15+
set(LLVM_LIBGCC_LIBUNWIND_BINARY_DIR "libunwind")
16+
else()
17+
set(LLVM_LIBGCC_COMPILER_RT_BINARY_DIR "../compiler-rt")
18+
set(LLVM_LIBGCC_LIBUNWIND_BINARY_DIR "../libunwind")
19+
endif()
20+
21+
# Add path for custom modules
22+
list(INSERT CMAKE_MODULE_PATH 0
1023
"${CMAKE_CURRENT_SOURCE_DIR}/cmake"
1124
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
25+
"${CMAKE_CURRENT_SOURCE_DIR}/../runtimes/cmake/Modules"
1226
"${LLVM_COMMON_CMAKE_UTILS}"
1327
"${LLVM_COMMON_CMAKE_UTILS}/Modules"
14-
)
28+
)
29+
30+
set(LLVM_LIBGCC_LIBUNWIND_PATH "${CMAKE_CURRENT_LIST_DIR}/../libunwind"
31+
CACHE PATH "Specify path to libunwind source.")
32+
set(LLVM_LIBGCC_COMPILER_RT_PATH "${CMAKE_CURRENT_LIST_DIR}/../compiler-rt"
33+
CACHE PATH "Specify path to compiler-rt source.")
1534

16-
project(llvm-libgcc LANGUAGES C CXX ASM)
35+
include(GNUInstallDirs)
1736

1837
if(NOT LLVM_LIBGCC_EXPLICIT_OPT_IN)
1938
message(FATAL_ERROR
@@ -25,18 +44,114 @@ if(NOT LLVM_LIBGCC_EXPLICIT_OPT_IN)
2544
"to your CMake invocation and try again.")
2645
endif()
2746

28-
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib${LLVMLIB_DIR_SUFFIX}")
29-
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib${LLVMLIB_DIR_SUFFIX}")
30-
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin")
47+
if(HAVE_COMPILER_RT)
48+
message(FATAL_ERROR
49+
"Attempting to build both compiler-rt and llvm-libgcc will cause irreconcilable "
50+
"target clashes. Please choose one or the other, but not both.")
51+
endif()
52+
53+
if(HAVE_LIBUNWIND)
54+
message(FATAL_ERROR
55+
"Attempting to build both libunwind and llvm-libgcc will cause irreconcilable "
56+
"target clashes. Please choose one or the other, but not both.")
57+
endif()
58+
59+
#===============================================================================
60+
# Configure System
61+
#===============================================================================
62+
63+
if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
64+
set(LLVM_LIBGCC_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}/${LLVM_DEFAULT_TARGET_TRIPLE})
65+
set(LLVM_LIBGCC_INSTALL_LIBRARY_DIR lib${LLVM_LIBDIR_SUFFIX}/${LLVM_DEFAULT_TARGET_TRIPLE} CACHE PATH
66+
"Path where built llvm-libgcc libraries should be installed.")
67+
if(LIBCXX_LIBDIR_SUBDIR)
68+
string(APPEND LLVM_LIBGCC_LIBRARY_DIR /${LLVM_LIBGCC_LIBDIR_SUBDIR})
69+
string(APPEND LLVM_LIBGCC_INSTALL_LIBRARY_DIR /${LLVM_LIBGCC_LIBDIR_SUBDIR})
70+
endif()
71+
else()
72+
if(LLVM_LIBRARY_OUTPUT_INTDIR)
73+
set(LLVM_LIBGCC_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
74+
else()
75+
set(LLVM_LIBGCC_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LLVM_LIBGCC_LIBDIR_SUFFIX})
76+
endif()
77+
set(LLVM_LIBGCC_INSTALL_LIBRARY_DIR lib${LLVM_LIBGCC_LIBDIR_SUFFIX} CACHE PATH
78+
"Path where built llvm-libgcc libraries should be installed.")
79+
endif()
80+
81+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${LLVM_LIBGCC_LIBRARY_DIR})
82+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${LLVM_LIBGCC_LIBRARY_DIR})
83+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_LIBGCC_LIBRARY_DIR})
84+
85+
#===============================================================================
86+
# Build libraries
87+
#===============================================================================
88+
89+
set(COMPILER_RT_BUILD_BUILTINS ON)
90+
set(COMPILER_RT_BUILTINS_HIDE_SYMBOLS OFF)
91+
add_subdirectory(${LLVM_LIBGCC_COMPILER_RT_PATH} ${LLVM_LIBGCC_COMPILER_RT_BINARY_DIR})
92+
93+
set(LIBUNWIND_ENABLE_STATIC ON)
94+
set(LIBUNWIND_ENABLE_SHARED ON)
95+
set(LIBUNWIND_USE_COMPILER_RT OFF)
96+
set(LIBUNWIND_HAS_GCC_LIB OFF)
97+
set(LIBUNWIND_HAS_GCC_S_LIB OFF)
98+
add_subdirectory(${LLVM_LIBGCC_LIBUNWIND_PATH} ${LLVM_LIBGCC_LIBUNWIND_BINARY_DIR})
99+
100+
add_custom_target(gcc_s.ver
101+
SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/gcc_s.ver.in
102+
COMMAND ${CMAKE_C_COMPILER} -E
103+
-xc ${CMAKE_CURRENT_SOURCE_DIR}/gcc_s.ver.in
104+
-o ${CMAKE_CURRENT_BINARY_DIR}/gcc_s.ver
105+
)
106+
107+
add_dependencies(unwind_shared gcc_s.ver)
108+
109+
construct_compiler_rt_default_triple()
110+
111+
target_link_options(unwind_shared PUBLIC
112+
-Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/gcc_s.ver
113+
)
114+
115+
target_link_libraries(unwind_shared PUBLIC
116+
$<TARGET_OBJECTS:clang_rt.builtins-${COMPILER_RT_DEFAULT_TARGET_ARCH}>
117+
m
118+
)
119+
120+
#===============================================================================
121+
# Install Symlinks
122+
#===============================================================================
123+
124+
get_compiler_rt_install_dir(${COMPILER_RT_DEFAULT_TARGET_ARCH} install_dir_builtins)
125+
string(REGEX REPLACE "^lib/" "" install_dir_builtins "${install_dir_builtins}")
126+
string(FIND "${install_dir_builtins}" "clang" install_path_contains_triple)
127+
if(install_path_contains_triple EQUAL -1)
128+
set(builtins_suffix "-${COMPILER_RT_DEFAULT_TARGET_ARCH}")
129+
else()
130+
string(PREPEND install_dir_builtins "../")
131+
endif()
132+
set(LLVM_LIBGCC_COMPILER_RT ${install_dir_builtins}/libclang_rt.builtins${builtins_suffix}.a)
133+
134+
add_custom_target(llvm-libgcc ALL
135+
DEPENDS unwind_shared unwind_static clang_rt.builtins-${COMPILER_RT_DEFAULT_TARGET_ARCH}
136+
COMMAND ${CMAKE_COMMAND} -E create_symlink ${LLVM_LIBGCC_COMPILER_RT} libgcc.a
137+
COMMAND ${CMAKE_COMMAND} -E create_symlink libunwind.a libgcc_eh.a
138+
COMMAND ${CMAKE_COMMAND} -E create_symlink libunwind.so libgcc_s.so.1.0
139+
COMMAND ${CMAKE_COMMAND} -E create_symlink libgcc_s.so.1.0 libgcc_s.so.1
140+
COMMAND ${CMAKE_COMMAND} -E create_symlink libgcc_s.so.1 libgcc_s.so
141+
)
31142

32-
set(COMPILER_RT_BUILD_BUILTINS On)
33-
set(COMPILER_RT_BUILTINS_HIDE_SYMBOLS Off)
34-
add_subdirectory("../compiler-rt" "compiler-rt")
143+
install(TARGETS unwind_shared unwind_static
144+
LIBRARY DESTINATION ${LLVM_LIBGCC_INSTALL_LIBRARY_DIR} COMPONENT llvm-libgcc
145+
ARCHIVE DESTINATION ${LLVM_LIBGCC_INSTALL_LIBRARY_DIR} COMPONENT llvm-libgcc
146+
RUNTIME DESTINATION ${LLVM_LIBGCC_INSTALL_RUNTIME_DIR} COMPONENT llvm-libgcc)
35147

36-
set(LIBUNWIND_ENABLE_STATIC On)
37-
set(LIBUNWIND_ENABLE_SHARED Off)
38-
set(LIBUNWIND_HAS_COMMENT_LIB_PRAGMA Off)
39-
set(LIBUNWIND_USE_COMPILER_RT On)
40-
add_subdirectory("../libunwind" "libunwind")
148+
install(TARGETS clang_rt.builtins-${COMPILER_RT_DEFAULT_TARGET_ARCH}
149+
LIBRARY DESTINATION ${LLVM_LIBGCC_INSTALL_LIBRARY_DIR}/${install_dir_builtins} COMPONENT llvm-libgcc
150+
ARCHIVE DESTINATION ${LLVM_LIBGCC_INSTALL_LIBRARY_DIR}/${install_dir_builtins} COMPONENT llvm-libgcc
151+
RUNTIME DESTINATION ${LLVM_LIBGCC_INSTALL_RUNTIME_DIR}/${install_dir_builtins} COMPONENT llvm-libgcc)
41152

42-
add_subdirectory(lib)
153+
foreach(VAR libgcc.a libgcc_eh.a libgcc_s.so.1.0 libgcc_s.so.1 libgcc_s.so)
154+
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${VAR}
155+
DESTINATION ${LLVM_LIBGCC_INSTALL_LIBRARY_DIR}
156+
COMPONENT llvm-libgcc)
157+
endforeach()
File renamed without changes.

llvm-libgcc/lib/CMakeLists.txt

-86
This file was deleted.

llvm-libgcc/lib/blank.c

Whitespace-only changes.

runtimes/CMakeLists.txt

-17
Original file line numberDiff line numberDiff line change
@@ -216,23 +216,6 @@ if(LLVM_INCLUDE_TESTS)
216216
umbrella_lit_testsuite_begin(check-runtimes)
217217
endif()
218218

219-
# llvm-libgcc incorporates both compiler-rt and libunwind as subprojects with very
220-
# specific flags, which causes clashes when they're independently built too.
221-
if("llvm-libgcc" IN_LIST runtimes)
222-
if("compiler-rt" IN_LIST runtimes OR "compiler-rt" IN_LIST LLVM_ENABLE_PROJECTS)
223-
message(FATAL_ERROR
224-
"Attempting to build both compiler-rt and llvm-libgcc will cause irreconcilable "
225-
"target clashes. Please choose one or the other, but not both.")
226-
endif()
227-
228-
if("libunwind" IN_LIST runtimes)
229-
message(
230-
FATAL_ERROR
231-
"Attempting to build both libunwind and llvm-libgcc will cause irreconcilable "
232-
"target clashes. Please choose one or the other, but not both.")
233-
endif()
234-
endif()
235-
236219
# We do this in two loops so that HAVE_* is set for each runtime before the
237220
# other runtimes are added.
238221
foreach(entry ${runtimes})

0 commit comments

Comments
 (0)