1 #===============================================================================
3 #===============================================================================
5 cmake_minimum_required(VERSION 3.20.0)
6 set(LLVM_SUBPROJECT_TITLE "LLVM libgcc")
8 set(LLVM_COMMON_CMAKE_UTILS "${CMAKE_CURRENT_SOURCE_DIR}/../cmake")
10 # Check if llvm-libgcc is built as a standalone project
11 if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR OR LLVM_LIBGCC_STANDALONE_BUILD)
12 project(llvm-libgcc LANGUAGES C CXX ASM)
13 set(COMPILER_RT_STANDALONE_BUILD ON)
14 set_property(GLOBAL PROPERTY USE_FOLDERS ON)
15 set(LLVM_LIBGCC_COMPILER_RT_BINARY_DIR "compiler-rt")
16 set(LLVM_LIBGCC_LIBUNWIND_BINARY_DIR "libunwind")
18 set(LLVM_LIBGCC_COMPILER_RT_BINARY_DIR "../compiler-rt")
19 set(LLVM_LIBGCC_LIBUNWIND_BINARY_DIR "../libunwind")
22 # Add path for custom modules
23 list(INSERT CMAKE_MODULE_PATH 0
24 "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
25 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
26 "${CMAKE_CURRENT_SOURCE_DIR}/../runtimes/cmake/Modules"
27 "${LLVM_COMMON_CMAKE_UTILS}"
28 "${LLVM_COMMON_CMAKE_UTILS}/Modules"
31 set(LLVM_LIBGCC_LIBUNWIND_PATH "${CMAKE_CURRENT_LIST_DIR}/../libunwind"
32 CACHE PATH "Specify path to libunwind source.")
33 set(LLVM_LIBGCC_COMPILER_RT_PATH "${CMAKE_CURRENT_LIST_DIR}/../compiler-rt"
34 CACHE PATH "Specify path to compiler-rt source.")
36 include(GNUInstallDirs)
38 if(NOT LLVM_LIBGCC_EXPLICIT_OPT_IN)
40 "llvm-libgcc is not for the casual LLVM user. It is intended to be used by distro "
41 "managers who want to replace libgcc with compiler-rt and libunwind, but cannot "
42 "fully abandon the libgcc family (e.g. because they are dependent on glibc). Such "
43 "managers must have worked out their compatibility requirements ahead of using "
44 "llvm-libgcc. If you want to build llvm-libgcc, please add -DLLVM_LIBGCC_EXPLICIT_OPT_IN=Yes "
45 "to your CMake invocation and try again.")
50 "Attempting to build both compiler-rt and llvm-libgcc will cause irreconcilable "
51 "target clashes. Please choose one or the other, but not both.")
56 "Attempting to build both libunwind and llvm-libgcc will cause irreconcilable "
57 "target clashes. Please choose one or the other, but not both.")
60 #===============================================================================
62 #===============================================================================
64 if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
65 set(TARGET_SUBDIR ${LLVM_DEFAULT_TARGET_TRIPLE})
66 if(LLVM_LIBGCC_LIBDIR_SUBDIR)
67 string(APPEND TARGET_SUBDIR /${LLVM_LIBGCC_LIBDIR_SUBDIR})
69 set(LLVM_LIBGCC_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}/${TARGET_SUBDIR})
70 set(LLVM_LIBGCC_INSTALL_LIBRARY_DIR lib${LLVM_LIBDIR_SUFFIX}/${TARGET_SUBDIR} CACHE PATH
71 "Path where built llvm-libgcc libraries should be installed.")
74 if(LLVM_LIBRARY_OUTPUT_INTDIR)
75 set(LLVM_LIBGCC_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
77 set(LLVM_LIBGCC_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LLVM_LIBGCC_LIBDIR_SUFFIX})
79 set(LLVM_LIBGCC_INSTALL_LIBRARY_DIR lib${LLVM_LIBGCC_LIBDIR_SUFFIX} CACHE PATH
80 "Path where built llvm-libgcc libraries should be installed.")
83 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${LLVM_LIBGCC_LIBRARY_DIR})
84 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${LLVM_LIBGCC_LIBRARY_DIR})
85 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_LIBGCC_LIBRARY_DIR})
87 #===============================================================================
89 #===============================================================================
91 set(COMPILER_RT_BUILD_BUILTINS ON)
92 set(COMPILER_RT_BUILTINS_HIDE_SYMBOLS OFF)
93 add_subdirectory(${LLVM_LIBGCC_COMPILER_RT_PATH} ${LLVM_LIBGCC_COMPILER_RT_BINARY_DIR})
95 set(LIBUNWIND_ENABLE_STATIC ON)
96 set(LIBUNWIND_ENABLE_SHARED ON)
97 set(LIBUNWIND_USE_COMPILER_RT OFF)
98 set(LIBUNWIND_HAS_GCC_LIB OFF)
99 set(LIBUNWIND_HAS_GCC_S_LIB OFF)
100 add_subdirectory(${LLVM_LIBGCC_LIBUNWIND_PATH} ${LLVM_LIBGCC_LIBUNWIND_BINARY_DIR})
102 add_custom_target(gcc_s.ver
103 SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/gcc_s.ver.in
104 COMMAND ${CMAKE_C_COMPILER} -E
105 -xc ${CMAKE_CURRENT_SOURCE_DIR}/gcc_s.ver.in
106 -o ${CMAKE_CURRENT_BINARY_DIR}/gcc_s.ver
109 add_dependencies(unwind_shared gcc_s.ver)
111 construct_compiler_rt_default_triple()
113 target_link_options(unwind_shared PUBLIC
114 -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/gcc_s.ver
117 target_link_libraries(unwind_shared PUBLIC
118 $<TARGET_OBJECTS:clang_rt.builtins-${COMPILER_RT_DEFAULT_TARGET_ARCH}>
122 #===============================================================================
124 #===============================================================================
126 get_compiler_rt_install_dir(${COMPILER_RT_DEFAULT_TARGET_ARCH} install_dir_builtins)
127 string(REGEX REPLACE "^lib/" "" install_dir_builtins "${install_dir_builtins}")
128 string(FIND "${install_dir_builtins}" "clang" install_path_contains_triple)
129 if(install_path_contains_triple EQUAL -1)
130 set(builtins_suffix "-${COMPILER_RT_DEFAULT_TARGET_ARCH}")
132 string(PREPEND install_dir_builtins "../")
134 set(LLVM_LIBGCC_COMPILER_RT ${install_dir_builtins}/libclang_rt.builtins${builtins_suffix}.a)
136 add_custom_target(llvm-libgcc ALL
137 DEPENDS unwind_shared unwind_static clang_rt.builtins-${COMPILER_RT_DEFAULT_TARGET_ARCH}
138 COMMAND ${CMAKE_COMMAND} -E create_symlink ${LLVM_LIBGCC_COMPILER_RT} libgcc.a
139 COMMAND ${CMAKE_COMMAND} -E create_symlink libunwind.a libgcc_eh.a
140 COMMAND ${CMAKE_COMMAND} -E create_symlink libunwind.so libgcc_s.so.1.0
141 COMMAND ${CMAKE_COMMAND} -E create_symlink libgcc_s.so.1.0 libgcc_s.so.1
142 COMMAND ${CMAKE_COMMAND} -E create_symlink libgcc_s.so.1 libgcc_s.so
145 install(TARGETS unwind_shared unwind_static
146 LIBRARY DESTINATION ${LLVM_LIBGCC_INSTALL_LIBRARY_DIR} COMPONENT llvm-libgcc
147 ARCHIVE DESTINATION ${LLVM_LIBGCC_INSTALL_LIBRARY_DIR} COMPONENT llvm-libgcc
148 RUNTIME DESTINATION ${LLVM_LIBGCC_INSTALL_RUNTIME_DIR} COMPONENT llvm-libgcc)
150 install(TARGETS clang_rt.builtins-${COMPILER_RT_DEFAULT_TARGET_ARCH}
151 LIBRARY DESTINATION ${LLVM_LIBGCC_INSTALL_LIBRARY_DIR}/${install_dir_builtins} COMPONENT llvm-libgcc
152 ARCHIVE DESTINATION ${LLVM_LIBGCC_INSTALL_LIBRARY_DIR}/${install_dir_builtins} COMPONENT llvm-libgcc
153 RUNTIME DESTINATION ${LLVM_LIBGCC_INSTALL_RUNTIME_DIR}/${install_dir_builtins} COMPONENT llvm-libgcc)
155 foreach(VAR libgcc.a libgcc_eh.a libgcc_s.so.1.0 libgcc_s.so.1 libgcc_s.so)
156 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${VAR}
157 DESTINATION ${LLVM_LIBGCC_INSTALL_LIBRARY_DIR}
158 COMPONENT llvm-libgcc)