[llvm] [llvm-libgcc][CMake] Refactor llvm-libgcc (PR #65455)

via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 12 22:34:39 PDT 2023


https://github.com/ur4t updated https://github.com/llvm/llvm-project/pull/65455:

>From eb938ef706ed7994fa0b362dfc4706e529a33563 Mon Sep 17 00:00:00 2001
From: ur4t <46435411+ur4t at users.noreply.github.com>
Date: Thu, 17 Aug 2023 23:45:40 +0800
Subject: [PATCH 1/8] [llvm-libgcc][CMake] Fix broken libgcc_s.so

Commit c5a20b518203613497fa864867fc232648006068 ([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.
---
 llvm-libgcc/lib/CMakeLists.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm-libgcc/lib/CMakeLists.txt b/llvm-libgcc/lib/CMakeLists.txt
index d895a21554b03c9..43ef3403a28c3ef 100644
--- a/llvm-libgcc/lib/CMakeLists.txt
+++ b/llvm-libgcc/lib/CMakeLists.txt
@@ -29,7 +29,7 @@ set_target_properties(libgcc_s
     POSITION_INDEPENDENT_CODE ON)
 string(REGEX MATCH "[^-]+" LLVM_LIBGCC_TARGET_ARCH ${CMAKE_C_COMPILER_TARGET})
 target_link_libraries(libgcc_s PRIVATE
-  $<TARGET_OBJECTS:unwind_static>
+  $<TARGET_OBJECTS:unwind_static_objects>
   $<TARGET_OBJECTS:clang_rt.builtins-${LLVM_LIBGCC_TARGET_ARCH}>
 )
 target_link_options(libgcc_s PRIVATE

>From 1b8a9ba1168427c3cb3fff940dca63ba79668d33 Mon Sep 17 00:00:00 2001
From: ur4t <46435411+ur4t at users.noreply.github.com>
Date: Sat, 19 Aug 2023 18:59:05 +0800
Subject: [PATCH 2/8] [llvm-libgcc][CMake] Refactor llvm-libgcc CMakeLists

This commit removes the limitation that `llvm-libgcc` cannot co-exist with
`compiler-rt` and `libunwind` when configuring llvm builds.

There's still a limitation that `-DCOMPILER_RT_BUILTINS_HIDE_SYMBOLS=No` is
required when `compiler-rt` is listed in `LLVM_ENABLE_RUNTIMES`. Because
`llvm-libgcc` will be added after `compiler-rt` in `runtimes/CMakeLists.txt`,
thus flags set in `llvm-libgcc/CMakeLists.txt` will not take effect.

There might be further refactoring which removes `llvm-libgcc` subproject,
merges coresponding components into `compiler-rt` and `libunwind` and adds
options like `COMPILER_RT_ENABLE_LIBGCC`, `LIBUNWIND_ENABLE_LIBGCC_EH` and
`LIBUNWIND_ENABLE_LIBGCC_S`, with old `llvm-libgcc` in `LLVM_ENABLE_RUNTIMES`
and `LLVM_LIBGCC_EXPLICIT_OPT_IN` preserved to keep compatibility.
---
 llvm-libgcc/CMakeLists.txt                  | 127 +++++++++++++++++---
 llvm-libgcc/docs/LLVMLibgcc.rst             |  17 +--
 llvm-libgcc/{lib/gcc_s.ver => gcc_s.ver.in} |   0
 llvm-libgcc/lib/CMakeLists.txt              |  86 -------------
 llvm-libgcc/lib/blank.c                     |   0
 runtimes/CMakeLists.txt                     |  17 ---
 6 files changed, 118 insertions(+), 129 deletions(-)
 rename llvm-libgcc/{lib/gcc_s.ver => gcc_s.ver.in} (100%)
 delete mode 100644 llvm-libgcc/lib/CMakeLists.txt
 delete mode 100644 llvm-libgcc/lib/blank.c

diff --git a/llvm-libgcc/CMakeLists.txt b/llvm-libgcc/CMakeLists.txt
index de42d908c37119d..b1637922e3ce13a 100644
--- a/llvm-libgcc/CMakeLists.txt
+++ b/llvm-libgcc/CMakeLists.txt
@@ -1,19 +1,26 @@
-cmake_minimum_required(VERSION 3.20.0)
+#===============================================================================
+# Setup Project
+#===============================================================================
 
-if (NOT IS_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/../libunwind")
-  message(FATAL_ERROR "llvm-libgcc requires being built in a monorepo layout with libunwind available")
-endif()
+cmake_minimum_required(VERSION 3.20.0)
 
 set(LLVM_COMMON_CMAKE_UTILS "${CMAKE_CURRENT_SOURCE_DIR}/../cmake")
 
-list(APPEND CMAKE_MODULE_PATH
+# Add path for custom modules
+list(INSERT CMAKE_MODULE_PATH 0
   "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
   "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
+  "${CMAKE_CURRENT_SOURCE_DIR}/../runtimes/cmake/Modules"
   "${LLVM_COMMON_CMAKE_UTILS}"
   "${LLVM_COMMON_CMAKE_UTILS}/Modules"
   )
 
-project(llvm-libgcc LANGUAGES C CXX ASM)
+set(LLVM_LIBGCC_LIBUNWIND_PATH "${CMAKE_CURRENT_LIST_DIR}/../libunwind"
+  CACHE PATH "Specify path to libunwind source.")
+set(LLVM_LIBGCC_COMPILER_RT_PATH "${CMAKE_CURRENT_LIST_DIR}/../compiler-rt"
+  CACHE PATH "Specify path to compiler-rt source.")
+
+include(GNUInstallDirs)
 
 if(NOT LLVM_LIBGCC_EXPLICIT_OPT_IN)
   message(FATAL_ERROR
@@ -25,18 +32,102 @@ if(NOT LLVM_LIBGCC_EXPLICIT_OPT_IN)
     "to your CMake invocation and try again.")
 endif()
 
-set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib${LLVMLIB_DIR_SUFFIX}")
-set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib${LLVMLIB_DIR_SUFFIX}")
-set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin")
+if(COMPILER_RT_BUILTINS_HIDE_SYMBOLS AND HAVE_COMPILER_RT)
+  message(FATAL_ERROR
+    "llvm-libgcc requires compiler-rt builtins with default symbol visibility, please"
+    "add -DCOMPILER_RT_BUILTINS_HIDE_SYMBOLS=No to your CMake invocation and try again.")
+endif()
+
+#===============================================================================
+# Configure System
+#===============================================================================
+
+if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
+  set(LLVM_LIBGCC_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}/${LLVM_DEFAULT_TARGET_TRIPLE})
+  set(LLVM_LIBGCC_INSTALL_LIBRARY_DIR lib${LLVM_LIBDIR_SUFFIX}/${LLVM_DEFAULT_TARGET_TRIPLE} CACHE PATH
+      "Path where built libgcc libraries should be installed.")
+  if(LIBCXX_LIBDIR_SUBDIR)
+    string(APPEND LLVM_LIBGCC_LIBRARY_DIR /${LLVM_LIBGCC_LIBDIR_SUBDIR})
+    string(APPEND LLVM_LIBGCC_INSTALL_LIBRARY_DIR /${LLVM_LIBGCC_LIBDIR_SUBDIR})
+  endif()
+else()
+  if(LLVM_LIBRARY_OUTPUT_INTDIR)
+    set(LLVM_LIBGCC_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
+  else()
+    set(LLVM_LIBGCC_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LLVM_LIBGCC_LIBDIR_SUFFIX})
+  endif()
+  set(LLVM_LIBGCC_INSTALL_LIBRARY_DIR lib${LLVM_LIBGCC_LIBDIR_SUFFIX} CACHE PATH
+      "Path where built libgcc libraries should be installed.")
+endif()
+
+set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${LLVM_LIBGCC_LIBRARY_DIR})
+set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${LLVM_LIBGCC_LIBRARY_DIR})
+set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_LIBGCC_LIBRARY_DIR})
+
+#===============================================================================
+# Configure System
+#===============================================================================
+
+set(COMPILER_RT_BUILD_BUILTINS ON)
+set(COMPILER_RT_BUILTINS_HIDE_SYMBOLS OFF)
+
+if(NOT HAVE_COMPILER_RT)
+  add_subdirectory(${LLVM_LIBGCC_COMPILER_RT_PATH} "../compiler-rt")
+endif()
+
+set(LIBUNWIND_ENABLE_STATIC ON)
+set(LIBUNWIND_ENABLE_SHARED ON)
+set(LIBUNWIND_USE_COMPILER_RT ON)
+
+if(NOT HAVE_LIBUNWIND)
+    add_subdirectory(${LLVM_LIBGCC_LIBUNWIND_PATH} "../libunwind")
+endif()
+
+add_custom_target(gcc_s.ver
+  SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/gcc_s.ver.in
+  COMMAND ${CMAKE_C_COMPILER} -E
+    -xc ${CMAKE_CURRENT_SOURCE_DIR}/gcc_s.ver.in
+    -o ${CMAKE_CURRENT_BINARY_DIR}/gcc_s.ver
+)
 
-set(COMPILER_RT_BUILD_BUILTINS On)
-set(COMPILER_RT_BUILTINS_HIDE_SYMBOLS Off)
-add_subdirectory("../compiler-rt" "compiler-rt")
+add_dependencies(unwind_shared gcc_s.ver)
+
+construct_compiler_rt_default_triple()
+
+target_link_options(unwind_shared PUBLIC
+  -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/gcc_s.ver
+)
+
+target_link_libraries(unwind_shared PUBLIC
+  $<TARGET_OBJECTS:clang_rt.builtins-${COMPILER_RT_DEFAULT_TARGET_ARCH}>
+  m
+)
+
+#===============================================================================
+# Install Symlinks
+#===============================================================================
+
+get_compiler_rt_install_dir(${COMPILER_RT_DEFAULT_TARGET_ARCH} install_dir_builtins)
+string(REGEX REPLACE "^lib/" "" install_dir_builtins "${install_dir_builtins}")
+string(FIND "${install_dir_builtins}" "clang" install_path_contains_triple)
+if(install_path_contains_triple EQUAL -1)
+  set(builtins_suffix "-${COMPILER_RT_DEFAULT_TARGET_ARCH}")
+else()
+  string(PREPEND install_dir_builtins "../")
+endif()
+set(LLVM_LIBGCC_COMPILER_RT ${install_dir_builtins}/libclang_rt.builtins${builtins_suffix}.a)
 
-set(LIBUNWIND_ENABLE_STATIC On)
-set(LIBUNWIND_ENABLE_SHARED Off)
-set(LIBUNWIND_HAS_COMMENT_LIB_PRAGMA Off)
-set(LIBUNWIND_USE_COMPILER_RT On)
-add_subdirectory("../libunwind" "libunwind")
+add_custom_target(libgcc ALL
+  DEPENDS unwind_shared
+  COMMAND ${CMAKE_COMMAND} -E create_symlink ${LLVM_LIBGCC_COMPILER_RT} libgcc.a
+  COMMAND ${CMAKE_COMMAND} -E create_symlink libunwind.a libgcc_eh.a
+  COMMAND ${CMAKE_COMMAND} -E create_symlink libunwind.so libgcc_s.so.1.0
+  COMMAND ${CMAKE_COMMAND} -E create_symlink libgcc_s.so.1.0 libgcc_s.so.1
+  COMMAND ${CMAKE_COMMAND} -E create_symlink libgcc_s.so.1 libgcc_s.so
+)
 
-add_subdirectory(lib)
+foreach(VAR libgcc.a libgcc_eh.a libgcc_s.so.1.0 libgcc_s.so.1 libgcc_s.so)
+  install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${VAR}
+    DESTINATION ${LLVM_LIBGCC_INSTALL_LIBRARY_DIR}
+    COMPONENT libgcc)
+endforeach()
diff --git a/llvm-libgcc/docs/LLVMLibgcc.rst b/llvm-libgcc/docs/LLVMLibgcc.rst
index 22dae7afaa6cfc8..a9d6b19d1ff5ffb 100644
--- a/llvm-libgcc/docs/LLVMLibgcc.rst
+++ b/llvm-libgcc/docs/LLVMLibgcc.rst
@@ -93,14 +93,15 @@ build with these compiler-rt symbols exposed.
       -DLLVM_LIBGCC_EXPLICIT_OPT_IN=Yes
   $ ninja -C build-primary install
 
-It's very important to notice that neither ``compiler-rt``, nor ``libunwind``,
-are listed in ``LLVM_ENABLE_RUNTIMES``. llvm-libgcc makes these subprojects, and
-adding them to this list will cause you problems due to there being duplicate
-targets. As such, configuring the runtimes build will reject explicitly mentioning
-either project with ``llvm-libgcc``.
-
-To avoid issues when building with ``-DLLVM_ENABLE_RUNTIMES=all``, ``llvm-libgcc``
-is not included, and all runtimes targets must be manually listed.
+Enabling llvm-libgcc now implies ``compiler-rt`` and ``libunwind`` are enabled,
+but it's okay to explicitly enable ``compiler-rt`` and ``libunwind``. When
+``compiler-rt`` is explicitly enabled, ``-DCOMPILER_RT_BUILTINS_HIDE_SYMBOLS=No``
+is required to keep symbols in ``compiler-rt`` visible for linking together
+with ``libunwind``.
+
+As mentioned above, llvm-libgcc is not for the casual user, thus it is not
+included in runtimes with setting ``-DLLVM_ENABLE_RUNTIMES=all``, and
+``-DLLVM_LIBGCC_EXPLICIT_OPT_IN=Yes`` is required as double insurance.
 
 ## Verifying your results
 
diff --git a/llvm-libgcc/lib/gcc_s.ver b/llvm-libgcc/gcc_s.ver.in
similarity index 100%
rename from llvm-libgcc/lib/gcc_s.ver
rename to llvm-libgcc/gcc_s.ver.in
diff --git a/llvm-libgcc/lib/CMakeLists.txt b/llvm-libgcc/lib/CMakeLists.txt
deleted file mode 100644
index 43ef3403a28c3ef..000000000000000
--- a/llvm-libgcc/lib/CMakeLists.txt
+++ /dev/null
@@ -1,86 +0,0 @@
-include(CheckLibraryExists)
-include(GNUInstallDirs)
-include(ExtendPath)
-
-string(REPLACE "-Wl,-z,defs" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")
-
-add_custom_target(gcc_s_ver ALL DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/gcc_s.ver")
-set(LLVM_LIBGCC_GCC_S_VER "${CMAKE_CURRENT_BINARY_DIR}/gcc_s.ver")
-
-add_custom_target(gcc_s.ver ALL
-  DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/gcc_s.ver"
-  COMMAND
-    "${CMAKE_C_COMPILER}"
-    "-E"
-    "-xc" "${CMAKE_CURRENT_SOURCE_DIR}/gcc_s.ver"
-    "-o" "${CMAKE_CURRENT_BINARY_DIR}/gcc_s.ver"
-)
-set_target_properties(gcc_s.ver PROPERTIES
-  OUTPUT_PATH "${CMAKE_CURRENT_BINARY_DIR}/gcc_s.ver")
-
-add_library(libgcc_s SHARED blank.c)
-add_dependencies(libgcc_s gcc_s_ver)
-set_target_properties(libgcc_s
-  PROPERTIES
-    LINKER_LANGUAGE C
-    OUTPUT_NAME "unwind"
-    VERSION "1.0"
-    SOVERSION "1"
-    POSITION_INDEPENDENT_CODE ON)
-string(REGEX MATCH "[^-]+" LLVM_LIBGCC_TARGET_ARCH ${CMAKE_C_COMPILER_TARGET})
-target_link_libraries(libgcc_s PRIVATE
-  $<TARGET_OBJECTS:unwind_static_objects>
-  $<TARGET_OBJECTS:clang_rt.builtins-${LLVM_LIBGCC_TARGET_ARCH}>
-)
-target_link_options(libgcc_s PRIVATE
-  -nostdlib
-  -Wl,--version-script,$<TARGET_PROPERTY:gcc_s.ver,OUTPUT_PATH>)
-
-check_library_exists(m sin "" LLVM_LIBGCC_HAS_LIBM)
-target_link_libraries(libgcc_s PRIVATE
-  $<$<BOOL:LLVM_LIBGCC_HAS_LIBM>:m>
-  c
-)
-
-extend_path(LLVM_LIBGCC_LIBUNWIND_STATIC_ROOT "${CMAKE_INSTALL_PREFIX}" "${LIBUNWIND_INSTALL_LIBRARY_DIR}")
-#string(REPLACE "${CMAKE_INSTALL_FULL_LIBDIR}/" "" LLVM_LIBGCC_LIBUNWIND_STATIC_ROOT "${LLVM_LIBGCC_LIBUNWIND_STATIC_ROOT}")
-
-install(TARGETS libgcc_s
-        LIBRARY DESTINATION "${LLVM_LIBGCC_LIBUNWIND_STATIC_ROOT}" COMPONENT unwind
-        ARCHIVE DESTINATION "${LLVM_LIBGCC_LIBUNWIND_STATIC_ROOT}" COMPONENT unwind
-        RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT unwind)
-
-get_compiler_rt_install_dir(${LLVM_LIBGCC_TARGET_ARCH} install_dir_builtins)
-string(REGEX REPLACE "^lib/" "" install_dir_builtins "${install_dir_builtins}")
-string(FIND "${install_dir_builtins}" "clang" install_path_contains_triple)
-if(install_path_contains_triple EQUAL -1)
-  set(builtins_suffix "-${LLVM_LIBGCC_TARGET_ARCH}")
-else()
-  string(PREPEND install_dir_builtins "../")
-endif()
-install(CODE "execute_process(
-                COMMAND \"\${CMAKE_COMMAND}\" -E
-                create_symlink ${install_dir_builtins}/libclang_rt.builtins${builtins_suffix}.a libgcc.a
-                WORKING_DIRECTORY \"\$ENV{DESTDIR}${LLVM_LIBGCC_LIBUNWIND_STATIC_ROOT}\")"
-        COMPONENT unwind)
-
-install(CODE "execute_process(
-                COMMAND \"\${CMAKE_COMMAND}\" -E
-                create_symlink libunwind.a libgcc_eh.a
-                WORKING_DIRECTORY \"\$ENV{DESTDIR}${LLVM_LIBGCC_LIBUNWIND_STATIC_ROOT}\")"
-        COMPONENT unwind)
-install(CODE "execute_process(
-               COMMAND \"\${CMAKE_COMMAND}\" -E
-               create_symlink libunwind.so libgcc_s.so.1.0
-               WORKING_DIRECTORY \"\$ENV{DESTDIR}${LLVM_LIBGCC_LIBUNWIND_STATIC_ROOT}\")"
-        COMPONENT unwind)
-install(CODE "execute_process(
-                COMMAND \"\${CMAKE_COMMAND}\" -E
-                create_symlink libgcc_s.so.1.0 libgcc_s.so.1
-                WORKING_DIRECTORY \"\$ENV{DESTDIR}${LLVM_LIBGCC_LIBUNWIND_STATIC_ROOT}\")"
-        COMPONENT unwind)
-install(CODE "execute_process(
-                COMMAND \"\${CMAKE_COMMAND}\" -E
-                create_symlink libgcc_s.so.1 libgcc_s.so
-                WORKING_DIRECTORY \"\$ENV{DESTDIR}${LLVM_LIBGCC_LIBUNWIND_STATIC_ROOT}\")"
-        COMPONENT unwind)
diff --git a/llvm-libgcc/lib/blank.c b/llvm-libgcc/lib/blank.c
deleted file mode 100644
index e69de29bb2d1d64..000000000000000
diff --git a/runtimes/CMakeLists.txt b/runtimes/CMakeLists.txt
index 599529852688f25..06f51243827cdd7 100644
--- a/runtimes/CMakeLists.txt
+++ b/runtimes/CMakeLists.txt
@@ -212,23 +212,6 @@ if(LLVM_INCLUDE_TESTS)
   umbrella_lit_testsuite_begin(check-runtimes)
 endif()
 
-# llvm-libgcc incorporates both compiler-rt and libunwind as subprojects with very
-# specific flags, which causes clashes when they're independently built too.
-if("llvm-libgcc" IN_LIST runtimes)
-  if("compiler-rt" IN_LIST runtimes OR "compiler-rt" IN_LIST LLVM_ENABLE_PROJECTS)
-    message(FATAL_ERROR
-      "Attempting to build both compiler-rt and llvm-libgcc will cause irreconcilable "
-      "target clashes. Please choose one or the other, but not both.")
-  endif()
-
-  if("libunwind" IN_LIST runtimes)
-    message(
-      FATAL_ERROR
-      "Attempting to build both libunwind and llvm-libgcc will cause irreconcilable "
-      "target clashes. Please choose one or the other, but not both.")
-  endif()
-endif()
-
 # We do this in two loops so that HAVE_* is set for each runtime before the
 # other runtimes are added.
 foreach(entry ${runtimes})

>From c790fd05b3cd4a5c5cbaff1d9855a8c4d7a1e2cb Mon Sep 17 00:00:00 2001
From: ur4t <46435411+ur4t at users.noreply.github.com>
Date: Thu, 7 Sep 2023 21:04:23 +0800
Subject: [PATCH 3/8] [llvm-libgcc][CMake] Fix mismatched target names of
 llvm-libgcc

When configuring with `llvm/CMakeLists.txt`, target `llvm-libgcc` requires a
corresponding target in `llvm-libgcc/CMakeLists.txt`. This commit fixes the
mismatched target names.
---
 llvm-libgcc/CMakeLists.txt | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/llvm-libgcc/CMakeLists.txt b/llvm-libgcc/CMakeLists.txt
index b1637922e3ce13a..111de4080c4c0f3 100644
--- a/llvm-libgcc/CMakeLists.txt
+++ b/llvm-libgcc/CMakeLists.txt
@@ -45,7 +45,7 @@ endif()
 if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
   set(LLVM_LIBGCC_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}/${LLVM_DEFAULT_TARGET_TRIPLE})
   set(LLVM_LIBGCC_INSTALL_LIBRARY_DIR lib${LLVM_LIBDIR_SUFFIX}/${LLVM_DEFAULT_TARGET_TRIPLE} CACHE PATH
-      "Path where built libgcc libraries should be installed.")
+      "Path where built llvm-libgcc libraries should be installed.")
   if(LIBCXX_LIBDIR_SUBDIR)
     string(APPEND LLVM_LIBGCC_LIBRARY_DIR /${LLVM_LIBGCC_LIBDIR_SUBDIR})
     string(APPEND LLVM_LIBGCC_INSTALL_LIBRARY_DIR /${LLVM_LIBGCC_LIBDIR_SUBDIR})
@@ -57,7 +57,7 @@ else()
     set(LLVM_LIBGCC_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LLVM_LIBGCC_LIBDIR_SUFFIX})
   endif()
   set(LLVM_LIBGCC_INSTALL_LIBRARY_DIR lib${LLVM_LIBGCC_LIBDIR_SUFFIX} CACHE PATH
-      "Path where built libgcc libraries should be installed.")
+      "Path where built llvm-libgcc libraries should be installed.")
 endif()
 
 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${LLVM_LIBGCC_LIBRARY_DIR})
@@ -117,7 +117,7 @@ else()
 endif()
 set(LLVM_LIBGCC_COMPILER_RT ${install_dir_builtins}/libclang_rt.builtins${builtins_suffix}.a)
 
-add_custom_target(libgcc ALL
+add_custom_target(llvm-libgcc ALL
   DEPENDS unwind_shared
   COMMAND ${CMAKE_COMMAND} -E create_symlink ${LLVM_LIBGCC_COMPILER_RT} libgcc.a
   COMMAND ${CMAKE_COMMAND} -E create_symlink libunwind.a libgcc_eh.a
@@ -129,5 +129,5 @@ add_custom_target(libgcc ALL
 foreach(VAR libgcc.a libgcc_eh.a libgcc_s.so.1.0 libgcc_s.so.1 libgcc_s.so)
   install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${VAR}
     DESTINATION ${LLVM_LIBGCC_INSTALL_LIBRARY_DIR}
-    COMPONENT libgcc)
+    COMPONENT llvm-libgcc)
 endforeach()

>From 358ff004f5c1786cdb43bdf1167665eb4fb242b9 Mon Sep 17 00:00:00 2001
From: ur4t <46435411+ur4t at users.noreply.github.com>
Date: Thu, 7 Sep 2023 21:40:42 +0800
Subject: [PATCH 4/8] [llvm-libgcc][CMake] Refuse improper
 COMPILER_RT_BUILD_BUILTINS

---
 llvm-libgcc/CMakeLists.txt | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/llvm-libgcc/CMakeLists.txt b/llvm-libgcc/CMakeLists.txt
index 111de4080c4c0f3..bd811e087d16c15 100644
--- a/llvm-libgcc/CMakeLists.txt
+++ b/llvm-libgcc/CMakeLists.txt
@@ -32,10 +32,11 @@ if(NOT LLVM_LIBGCC_EXPLICIT_OPT_IN)
     "to your CMake invocation and try again.")
 endif()
 
-if(COMPILER_RT_BUILTINS_HIDE_SYMBOLS AND HAVE_COMPILER_RT)
+if(HAVE_COMPILER_RT AND (NOT COMPILER_RT_BUILD_BUILTINS OR COMPILER_RT_BUILTINS_HIDE_SYMBOLS))
   message(FATAL_ERROR
-    "llvm-libgcc requires compiler-rt builtins with default symbol visibility, please"
-    "add -DCOMPILER_RT_BUILTINS_HIDE_SYMBOLS=No to your CMake invocation and try again.")
+    "llvm-libgcc requires compiler-rt builtins with default symbol visibility, "
+    "add -DCOMPILER_RT_BUILD_BUILTINS=Yes -DCOMPILER_RT_BUILTINS_HIDE_SYMBOLS=No "
+    "to your CMake invocation and try again.")
 endif()
 
 #===============================================================================

>From 6f7088b13887494286ae2ed3adbb2635a2cbdccc Mon Sep 17 00:00:00 2001
From: ur4t <46435411+ur4t at users.noreply.github.com>
Date: Fri, 8 Sep 2023 17:28:48 +0800
Subject: [PATCH 5/8] [llvm-libgcc][CMake] Fix broken component llvm-libgcc

This commit adds targets of `llvm-libgcc` symlinks into the component, which
fixes broken `llvm-libgcc` when installing only the component `llvm-libgcc`.
---
 llvm-libgcc/CMakeLists.txt | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/llvm-libgcc/CMakeLists.txt b/llvm-libgcc/CMakeLists.txt
index bd811e087d16c15..d8d8e6ad1b63f2d 100644
--- a/llvm-libgcc/CMakeLists.txt
+++ b/llvm-libgcc/CMakeLists.txt
@@ -119,7 +119,7 @@ endif()
 set(LLVM_LIBGCC_COMPILER_RT ${install_dir_builtins}/libclang_rt.builtins${builtins_suffix}.a)
 
 add_custom_target(llvm-libgcc ALL
-  DEPENDS unwind_shared
+  DEPENDS unwind_shared unwind_static clang_rt.builtins-${COMPILER_RT_DEFAULT_TARGET_ARCH}
   COMMAND ${CMAKE_COMMAND} -E create_symlink ${LLVM_LIBGCC_COMPILER_RT} libgcc.a
   COMMAND ${CMAKE_COMMAND} -E create_symlink libunwind.a libgcc_eh.a
   COMMAND ${CMAKE_COMMAND} -E create_symlink libunwind.so libgcc_s.so.1.0
@@ -127,6 +127,16 @@ add_custom_target(llvm-libgcc ALL
   COMMAND ${CMAKE_COMMAND} -E create_symlink libgcc_s.so.1 libgcc_s.so
 )
 
+install(TARGETS unwind_shared unwind_static
+  LIBRARY DESTINATION ${LLVM_LIBGCC_INSTALL_LIBRARY_DIR} COMPONENT llvm-libgcc
+  ARCHIVE DESTINATION ${LLVM_LIBGCC_INSTALL_LIBRARY_DIR} COMPONENT llvm-libgcc
+  RUNTIME DESTINATION ${LLVM_LIBGCC_INSTALL_RUNTIME_DIR} COMPONENT llvm-libgcc)
+
+install(TARGETS clang_rt.builtins-${COMPILER_RT_DEFAULT_TARGET_ARCH}
+  LIBRARY DESTINATION ${LLVM_LIBGCC_INSTALL_LIBRARY_DIR}/${install_dir_builtins} COMPONENT llvm-libgcc
+  ARCHIVE DESTINATION ${LLVM_LIBGCC_INSTALL_LIBRARY_DIR}/${install_dir_builtins} COMPONENT llvm-libgcc
+  RUNTIME DESTINATION ${LLVM_LIBGCC_INSTALL_RUNTIME_DIR}/${install_dir_builtins} COMPONENT llvm-libgcc)
+
 foreach(VAR libgcc.a libgcc_eh.a libgcc_s.so.1.0 libgcc_s.so.1 libgcc_s.so)
   install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${VAR}
     DESTINATION ${LLVM_LIBGCC_INSTALL_LIBRARY_DIR}

>From b61aed75b1c5891a4f84dd053fad1c68cc60902d Mon Sep 17 00:00:00 2001
From: ur4t <46435411+ur4t at users.noreply.github.com>
Date: Fri, 8 Sep 2023 21:27:12 +0800
Subject: [PATCH 6/8] [llvm-libgcc][CMake] Restore to inhibit co-existence of
 llvm-libgcc and compiler-rt/libunwind

It is quite hard to set necessary options without further modifying the order
of runtime projects in `runtimes/CMakeLists.txt`.

This commit restores to inhibit co-existence of `llvm-libgcc` and
`compiler-rt`/`libunwind` in commit c5a20b518203613497fa864867fc232648006068
([llvm-libgcc] initial commit).
---
 llvm-libgcc/CMakeLists.txt      | 25 ++++++++++++-------------
 llvm-libgcc/docs/LLVMLibgcc.rst | 17 ++++++++---------
 2 files changed, 20 insertions(+), 22 deletions(-)

diff --git a/llvm-libgcc/CMakeLists.txt b/llvm-libgcc/CMakeLists.txt
index d8d8e6ad1b63f2d..2f3e4485a43e3af 100644
--- a/llvm-libgcc/CMakeLists.txt
+++ b/llvm-libgcc/CMakeLists.txt
@@ -32,11 +32,16 @@ if(NOT LLVM_LIBGCC_EXPLICIT_OPT_IN)
     "to your CMake invocation and try again.")
 endif()
 
-if(HAVE_COMPILER_RT AND (NOT COMPILER_RT_BUILD_BUILTINS OR COMPILER_RT_BUILTINS_HIDE_SYMBOLS))
+if(HAVE_COMPILER_RT)
   message(FATAL_ERROR
-    "llvm-libgcc requires compiler-rt builtins with default symbol visibility, "
-    "add -DCOMPILER_RT_BUILD_BUILTINS=Yes -DCOMPILER_RT_BUILTINS_HIDE_SYMBOLS=No "
-    "to your CMake invocation and try again.")
+    "Attempting to build both compiler-rt and llvm-libgcc will cause irreconcilable "
+    "target clashes. Please choose one or the other, but not both.")
+endif()
+
+if(HAVE_LIBUNWIND)
+  message(FATAL_ERROR
+    "Attempting to build both libunwind and llvm-libgcc will cause irreconcilable "
+    "target clashes. Please choose one or the other, but not both.")
 endif()
 
 #===============================================================================
@@ -66,23 +71,17 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${LLVM_LIBGCC_LIBRARY_DIR})
 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_LIBGCC_LIBRARY_DIR})
 
 #===============================================================================
-# Configure System
+# Build libraries
 #===============================================================================
 
 set(COMPILER_RT_BUILD_BUILTINS ON)
 set(COMPILER_RT_BUILTINS_HIDE_SYMBOLS OFF)
-
-if(NOT HAVE_COMPILER_RT)
-  add_subdirectory(${LLVM_LIBGCC_COMPILER_RT_PATH} "../compiler-rt")
-endif()
+add_subdirectory(${LLVM_LIBGCC_COMPILER_RT_PATH} "../compiler-rt")
 
 set(LIBUNWIND_ENABLE_STATIC ON)
 set(LIBUNWIND_ENABLE_SHARED ON)
 set(LIBUNWIND_USE_COMPILER_RT ON)
-
-if(NOT HAVE_LIBUNWIND)
-    add_subdirectory(${LLVM_LIBGCC_LIBUNWIND_PATH} "../libunwind")
-endif()
+add_subdirectory(${LLVM_LIBGCC_LIBUNWIND_PATH} "../libunwind")
 
 add_custom_target(gcc_s.ver
   SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/gcc_s.ver.in
diff --git a/llvm-libgcc/docs/LLVMLibgcc.rst b/llvm-libgcc/docs/LLVMLibgcc.rst
index a9d6b19d1ff5ffb..22dae7afaa6cfc8 100644
--- a/llvm-libgcc/docs/LLVMLibgcc.rst
+++ b/llvm-libgcc/docs/LLVMLibgcc.rst
@@ -93,15 +93,14 @@ build with these compiler-rt symbols exposed.
       -DLLVM_LIBGCC_EXPLICIT_OPT_IN=Yes
   $ ninja -C build-primary install
 
-Enabling llvm-libgcc now implies ``compiler-rt`` and ``libunwind`` are enabled,
-but it's okay to explicitly enable ``compiler-rt`` and ``libunwind``. When
-``compiler-rt`` is explicitly enabled, ``-DCOMPILER_RT_BUILTINS_HIDE_SYMBOLS=No``
-is required to keep symbols in ``compiler-rt`` visible for linking together
-with ``libunwind``.
-
-As mentioned above, llvm-libgcc is not for the casual user, thus it is not
-included in runtimes with setting ``-DLLVM_ENABLE_RUNTIMES=all``, and
-``-DLLVM_LIBGCC_EXPLICIT_OPT_IN=Yes`` is required as double insurance.
+It's very important to notice that neither ``compiler-rt``, nor ``libunwind``,
+are listed in ``LLVM_ENABLE_RUNTIMES``. llvm-libgcc makes these subprojects, and
+adding them to this list will cause you problems due to there being duplicate
+targets. As such, configuring the runtimes build will reject explicitly mentioning
+either project with ``llvm-libgcc``.
+
+To avoid issues when building with ``-DLLVM_ENABLE_RUNTIMES=all``, ``llvm-libgcc``
+is not included, and all runtimes targets must be manually listed.
 
 ## Verifying your results
 

>From 656d90077e8cb669a8a84813ce2fb0570ce97249 Mon Sep 17 00:00:00 2001
From: ur4t <46435411+ur4t at users.noreply.github.com>
Date: Fri, 8 Sep 2023 21:51:58 +0800
Subject: [PATCH 7/8] [llvm-libgcc][CMake] Remove uncessary dependency on
 compiler-rt or libgcc

`libunwind.so` in `llvm-libgcc` is linked with clang_rt.builtins objects,
thus dependency on compiler-rt or libgcc is uncessary and should be removed.
---
 llvm-libgcc/CMakeLists.txt | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/llvm-libgcc/CMakeLists.txt b/llvm-libgcc/CMakeLists.txt
index 2f3e4485a43e3af..e92d2c919812e91 100644
--- a/llvm-libgcc/CMakeLists.txt
+++ b/llvm-libgcc/CMakeLists.txt
@@ -80,7 +80,9 @@ add_subdirectory(${LLVM_LIBGCC_COMPILER_RT_PATH} "../compiler-rt")
 
 set(LIBUNWIND_ENABLE_STATIC ON)
 set(LIBUNWIND_ENABLE_SHARED ON)
-set(LIBUNWIND_USE_COMPILER_RT ON)
+set(LIBUNWIND_USE_COMPILER_RT OFF)
+set(LIBUNWIND_HAS_GCC_LIB OFF)
+set(LIBUNWIND_HAS_GCC_S_LIB OFF)
 add_subdirectory(${LLVM_LIBGCC_LIBUNWIND_PATH} "../libunwind")
 
 add_custom_target(gcc_s.ver

>From 60eccd0b5b012e3e13bfdd4145dbd381a955887b Mon Sep 17 00:00:00 2001
From: ur4t <46435411+ur4t at users.noreply.github.com>
Date: Wed, 13 Sep 2023 10:52:18 +0800
Subject: [PATCH 8/8] [llvm-libgcc][CMake] Improve standalone build for
 llvm-libgcc

---
 llvm-libgcc/CMakeLists.txt | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/llvm-libgcc/CMakeLists.txt b/llvm-libgcc/CMakeLists.txt
index e92d2c919812e91..013c9ca2e330766 100644
--- a/llvm-libgcc/CMakeLists.txt
+++ b/llvm-libgcc/CMakeLists.txt
@@ -6,6 +6,18 @@ cmake_minimum_required(VERSION 3.20.0)
 
 set(LLVM_COMMON_CMAKE_UTILS "${CMAKE_CURRENT_SOURCE_DIR}/../cmake")
 
+# Check if llvm-libgcc is built as a standalone project
+if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR OR LLVM_LIBGCC_STANDALONE_BUILD)
+  project(llvm-libgcc LANGUAGES C CXX ASM)
+  set(COMPILER_RT_STANDALONE_BUILD ON)
+  set_property(GLOBAL PROPERTY USE_FOLDERS ON)
+  set(LLVM_LIBGCC_COMPILER_RT_BINARY_DIR "compiler-rt")
+  set(LLVM_LIBGCC_LIBUNWIND_BINARY_DIR "libunwind")
+else()
+  set(LLVM_LIBGCC_COMPILER_RT_BINARY_DIR "../compiler-rt")
+  set(LLVM_LIBGCC_LIBUNWIND_BINARY_DIR "../libunwind")
+endif()
+
 # Add path for custom modules
 list(INSERT CMAKE_MODULE_PATH 0
   "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
@@ -13,7 +25,7 @@ list(INSERT CMAKE_MODULE_PATH 0
   "${CMAKE_CURRENT_SOURCE_DIR}/../runtimes/cmake/Modules"
   "${LLVM_COMMON_CMAKE_UTILS}"
   "${LLVM_COMMON_CMAKE_UTILS}/Modules"
-  )
+)
 
 set(LLVM_LIBGCC_LIBUNWIND_PATH "${CMAKE_CURRENT_LIST_DIR}/../libunwind"
   CACHE PATH "Specify path to libunwind source.")
@@ -76,14 +88,14 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_LIBGCC_LIBRARY_DIR})
 
 set(COMPILER_RT_BUILD_BUILTINS ON)
 set(COMPILER_RT_BUILTINS_HIDE_SYMBOLS OFF)
-add_subdirectory(${LLVM_LIBGCC_COMPILER_RT_PATH} "../compiler-rt")
+add_subdirectory(${LLVM_LIBGCC_COMPILER_RT_PATH} ${LLVM_LIBGCC_COMPILER_RT_BINARY_DIR})
 
 set(LIBUNWIND_ENABLE_STATIC ON)
 set(LIBUNWIND_ENABLE_SHARED ON)
 set(LIBUNWIND_USE_COMPILER_RT OFF)
 set(LIBUNWIND_HAS_GCC_LIB OFF)
 set(LIBUNWIND_HAS_GCC_S_LIB OFF)
-add_subdirectory(${LLVM_LIBGCC_LIBUNWIND_PATH} "../libunwind")
+add_subdirectory(${LLVM_LIBGCC_LIBUNWIND_PATH} ${LLVM_LIBGCC_LIBUNWIND_BINARY_DIR})
 
 add_custom_target(gcc_s.ver
   SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/gcc_s.ver.in



More information about the llvm-commits mailing list