1 #===============================================================================
3 #===============================================================================
5 cmake_minimum_required(VERSION 3.20.0)
7 set(LLVM_COMMON_CMAKE_UTILS "${CMAKE_CURRENT_SOURCE_DIR}/../cmake")
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")
17 set(LLVM_LIBGCC_COMPILER_RT_BINARY_DIR "../compiler-rt")
18 set(LLVM_LIBGCC_LIBUNWIND_BINARY_DIR "../libunwind")
21 # Add path for custom modules
22 list(INSERT CMAKE_MODULE_PATH 0
23 "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
24 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
25 "${CMAKE_CURRENT_SOURCE_DIR}/../runtimes/cmake/Modules"
26 "${LLVM_COMMON_CMAKE_UTILS}"
27 "${LLVM_COMMON_CMAKE_UTILS}/Modules"
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.")
35 include(GNUInstallDirs)
37 if(NOT LLVM_LIBGCC_EXPLICIT_OPT_IN)
39 "llvm-libgcc is not for the casual LLVM user. It is intended to be used by distro "
40 "managers who want to replace libgcc with compiler-rt and libunwind, but cannot "
41 "fully abandon the libgcc family (e.g. because they are dependent on glibc). Such "
42 "managers must have worked out their compatibility requirements ahead of using "
43 "llvm-libgcc. If you want to build llvm-libgcc, please add -DLLVM_LIBGCC_EXPLICIT_OPT_IN=Yes "
44 "to your CMake invocation and try again.")
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.")
55 "Attempting to build both libunwind and llvm-libgcc will cause irreconcilable "
56 "target clashes. Please choose one or the other, but not both.")
59 #===============================================================================
61 #===============================================================================
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})
72 if(LLVM_LIBRARY_OUTPUT_INTDIR)
73 set(LLVM_LIBGCC_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
75 set(LLVM_LIBGCC_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LLVM_LIBGCC_LIBDIR_SUFFIX})
77 set(LLVM_LIBGCC_INSTALL_LIBRARY_DIR lib${LLVM_LIBGCC_LIBDIR_SUFFIX} CACHE PATH
78 "Path where built llvm-libgcc libraries should be installed.")
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})
85 #===============================================================================
87 #===============================================================================
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})
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})
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
107 add_dependencies(unwind_shared gcc_s.ver)
109 construct_compiler_rt_default_triple()
111 target_link_options(unwind_shared PUBLIC
112 -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/gcc_s.ver
115 target_link_libraries(unwind_shared PUBLIC
116 $<TARGET_OBJECTS:clang_rt.builtins-${COMPILER_RT_DEFAULT_TARGET_ARCH}>
120 #===============================================================================
122 #===============================================================================
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}")
130 string(PREPEND install_dir_builtins "../")
132 set(LLVM_LIBGCC_COMPILER_RT ${install_dir_builtins}/libclang_rt.builtins${builtins_suffix}.a)
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
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)
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)
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)