ldionne created this revision.
Herald added a subscriber: arichardson.
ldionne requested review of this revision.
Herald added projects: clang, libc++.
Herald added subscribers: libcxx-commits, cfe-commits.
Herald added a reviewer: libc++.

This patch overhauls how we pick up the ABI library. Instead of setting
ad-hoc flags, it creates interface targets that can be linked against by
the rest of the build, which is easier to follow and extend to support
new ABI libraries. It also adds a new ABI library called "system-libcxxabi",
which represents linking against a LLVM libc++abi already installed on the
system, and solves existing issues when trying to build against a system
libc++abi outside of the now-unsupported standalone build.

This is intended to be a NFC change, however there are some additional
simplifications and improvements we can make in the future that would
require a slight behavior change.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D120727

Files:
  clang/cmake/caches/CrossWinToARMLinux.cmake
  libcxx/CMakeLists.txt
  libcxx/cmake/Modules/HandleLibCXXABI.cmake
  libcxx/docs/BuildingLibcxx.rst
  libcxx/include/CMakeLists.txt
  libcxx/lib/abi/CMakeLists.txt
  libcxx/src/CMakeLists.txt
  libcxx/test/CMakeLists.txt
  libcxx/test/configs/legacy.cfg.in
  libcxx/utils/libcxx/test/config.py

Index: libcxx/utils/libcxx/test/config.py
===================================================================
--- libcxx/utils/libcxx/test/config.py
+++ libcxx/utils/libcxx/test/config.py
@@ -390,7 +390,7 @@
             self.cxx.link_flags += ['-lstdc++']
         elif cxx_abi == 'libsupc++':
             self.cxx.link_flags += ['-lsupc++']
-        elif cxx_abi == 'libcxxabi':
+        elif cxx_abi == 'libcxxabi' or 'system-libcxxabi':
             # If the C++ library requires explicitly linking to libc++abi, or
             # if we're testing libc++abi itself (the test configs are shared),
             # then link it.
@@ -417,7 +417,7 @@
             # The compiler normally links in oldnames.lib too, but we've
             # specified -nostdlib above, so we need to specify it manually.
             self.cxx.link_flags += ['-loldnames']
-        elif cxx_abi == 'none' or cxx_abi == 'default':
+        elif cxx_abi == 'none':
             if self.target_info.is_windows():
                 debug_suffix = 'd' if self.debug_build else ''
                 self.cxx.link_flags += ['-lmsvcrt%s' % debug_suffix]
Index: libcxx/test/configs/legacy.cfg.in
===================================================================
--- libcxx/test/configs/legacy.cfg.in
+++ libcxx/test/configs/legacy.cfg.in
@@ -14,7 +14,7 @@
 config.cxx_library_root         = "@LIBCXX_LIBRARY_DIR@"
 config.abi_library_root         = "@LIBCXX_CXX_ABI_LIBRARY_PATH@"
 config.enable_shared            = @LIBCXX_LINK_TESTS_WITH_SHARED_LIBCXX@
-config.cxx_abi                  = "@LIBCXX_CXX_ABI_LIBNAME@"
+config.cxx_abi                  = "@LIBCXX_CXXABI_FOR_TESTS@"
 config.configuration_variant    = "@LIBCXX_LIT_VARIANT@"
 config.host_triple              = "@LLVM_HOST_TRIPLE@"
 config.sysroot                  = "@CMAKE_SYSROOT@"
Index: libcxx/test/CMakeLists.txt
===================================================================
--- libcxx/test/CMakeLists.txt
+++ libcxx/test/CMakeLists.txt
@@ -17,7 +17,9 @@
 # The tests shouldn't link to any ABI library when it has been linked into
 # libc++ statically or via a linker script.
 if (LIBCXX_ENABLE_STATIC_ABI_LIBRARY OR LIBCXX_ENABLE_ABI_LINKER_SCRIPT)
-  set(LIBCXX_CXX_ABI_LIBNAME "none")
+  set(LIBCXX_CXXABI_FOR_TESTS "none")
+else()
+  set(LIBCXX_CXXABI_FOR_TESTS "${LIBCXX_CXX_ABI}")
 endif()
 
 # The tests shouldn't link to libunwind if we have a linker script which
Index: libcxx/src/CMakeLists.txt
===================================================================
--- libcxx/src/CMakeLists.txt
+++ libcxx/src/CMakeLists.txt
@@ -155,11 +155,6 @@
   set(exclude_from_all EXCLUDE_FROM_ALL)
 endif()
 
-# If LIBCXX_CXX_ABI_LIBRARY_PATH is defined we want to add it to the search path.
-add_link_flags_if(LIBCXX_CXX_ABI_LIBRARY_PATH
-                  "${CMAKE_LIBRARY_PATH_FLAG}${LIBCXX_CXX_ABI_LIBRARY_PATH}")
-
-
 if (LIBCXX_GENERATE_COVERAGE AND NOT LIBCXX_COVERAGE_LIBRARY)
   find_compiler_rt_library(profile LIBCXX_COVERAGE_LIBRARY)
 endif()
@@ -196,10 +191,6 @@
 endif()
 
 function(cxx_set_common_defines name)
-  if(LIBCXX_CXX_ABI_HEADER_TARGET)
-    add_dependencies(${name} ${LIBCXX_CXX_ABI_HEADER_TARGET})
-  endif()
-
   if (LIBCXX_ENABLE_PARALLEL_ALGORITHMS)
     target_link_libraries(${name} PUBLIC pstl::ParallelSTL)
   endif()
@@ -240,19 +231,18 @@
   # Link against libc++abi
   if (LIBCXX_STATICALLY_LINK_ABI_IN_SHARED_LIBRARY)
     if (APPLE)
-      target_link_libraries(cxx_shared PRIVATE "-Wl,-force_load" "${LIBCXX_CXX_STATIC_ABI_LIBRARY}")
+      target_link_libraries(cxx_shared PRIVATE "-Wl,-force_load" "$<TARGET_LINKER_FILE:libcxx-abi-static>")
     else()
-      target_link_libraries(cxx_shared PRIVATE "-Wl,--whole-archive,-Bstatic" "${LIBCXX_CXX_STATIC_ABI_LIBRARY}" "-Wl,-Bdynamic,--no-whole-archive")
+      target_link_libraries(cxx_shared PRIVATE "-Wl,--whole-archive,-Bstatic" "$<TARGET_LINKER_FILE:libcxx-abi-static>" "-Wl,-Bdynamic,--no-whole-archive")
     endif()
   else()
-    target_link_libraries(cxx_shared PUBLIC "${LIBCXX_CXX_SHARED_ABI_LIBRARY}")
+    target_link_libraries(cxx_shared PUBLIC libcxx-abi-shared)
   endif()
 
   # Maybe re-export symbols from libc++abi
   # In particular, we don't re-export the symbols if libc++abi is merged statically
   # into libc++ because in that case there's no dylib to re-export from.
-  if (APPLE AND (LIBCXX_CXX_ABI_LIBNAME STREQUAL "libcxxabi" OR
-                 LIBCXX_CXX_ABI_LIBNAME STREQUAL "default")
+  if (APPLE AND LIBCXX_CXX_ABI STREQUAL "libcxxabi"
             AND NOT DEFINED LIBCXX_OSX_REEXPORT_LIBCXXABI_SYMBOLS
             AND NOT LIBCXX_STATICALLY_LINK_ABI_IN_SHARED_LIBRARY)
     set(LIBCXX_OSX_REEXPORT_LIBCXXABI_SYMBOLS ON)
@@ -291,7 +281,8 @@
   add_library(cxx_static STATIC ${exclude_from_all} ${LIBCXX_SOURCES} ${LIBCXX_HEADERS})
   target_include_directories(cxx_static PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
   target_link_libraries(cxx_static PUBLIC cxx-headers
-                                   PRIVATE ${LIBCXX_LIBRARIES})
+                                   PRIVATE ${LIBCXX_LIBRARIES}
+                                   PRIVATE libcxx-abi-static)
   set_target_properties(cxx_static
     PROPERTIES
       COMPILE_FLAGS "${LIBCXX_COMPILE_FLAGS}"
@@ -322,19 +313,6 @@
   list(APPEND LIBCXX_BUILD_TARGETS "cxx_static")
   # Attempt to merge the libc++.a archive and the ABI library archive into one.
   if (LIBCXX_STATICALLY_LINK_ABI_IN_STATIC_LIBRARY)
-    set(MERGE_ARCHIVES_SEARCH_PATHS "")
-    if (LIBCXX_CXX_ABI_LIBRARY_PATH)
-      set(MERGE_ARCHIVES_SEARCH_PATHS "-L${LIBCXX_CXX_ABI_LIBRARY_PATH}")
-    endif()
-    if (TARGET "${LIBCXX_CXX_STATIC_ABI_LIBRARY}" OR HAVE_LIBCXXABI)
-      set(MERGE_ARCHIVES_ABI_TARGET "$<TARGET_LINKER_FILE:${LIBCXX_CXX_STATIC_ABI_LIBRARY}>")
-    else()
-      set(MERGE_ARCHIVES_ABI_TARGET
-        "${CMAKE_STATIC_LIBRARY_PREFIX}${LIBCXX_CXX_STATIC_ABI_LIBRARY}${CMAKE_STATIC_LIBRARY_SUFFIX}")
-      if (LIBCXX_CXX_ABI_LIBRARY_PATH)
-        set(MERGE_ARCHIVES_ABI_TARGET "${LIBCXX_CXX_ABI_LIBRARY_PATH}/${MERGE_ARCHIVES_ABI_TARGET}")
-      endif ()
-    endif()
     if (APPLE)
       set(MERGE_ARCHIVES_LIBTOOL "--use-libtool" "--libtool" "${CMAKE_LIBTOOL}")
     endif()
@@ -346,8 +324,8 @@
       --ar "${CMAKE_AR}"
       ${MERGE_ARCHIVES_LIBTOOL}
       "$<TARGET_LINKER_FILE:cxx_static>"
-      "${MERGE_ARCHIVES_ABI_TARGET}"
-      "${MERGE_ARCHIVES_SEARCH_PATHS}"
+      "$<TARGET_LINKER_FILE:libcxx-abi-static>"
+      ""
     WORKING_DIRECTORY ${LIBCXX_BUILD_DIR}
     DEPENDS ${MERGE_ARCHIVES_ABI_TARGET}
     )
Index: libcxx/lib/abi/CMakeLists.txt
===================================================================
--- libcxx/lib/abi/CMakeLists.txt
+++ libcxx/lib/abi/CMakeLists.txt
@@ -57,7 +57,7 @@
 endif()
 cxx_abi_list_identifier(abi_list_identifier
   "${triple}"
-  "${LIBCXX_CXX_ABI_LIBNAME}"
+  "${LIBCXX_CXX_ABI}"
   "${LIBCXX_ABI_VERSION}"
   "${LIBCXX_ABI_UNSTABLE}"
   "${LIBCXX_ENABLE_EXCEPTIONS}"
Index: libcxx/include/CMakeLists.txt
===================================================================
--- libcxx/include/CMakeLists.txt
+++ libcxx/include/CMakeLists.txt
@@ -568,7 +568,8 @@
 add_custom_target(generate-cxx-headers ALL DEPENDS ${_all_includes})
 
 add_library(cxx-headers INTERFACE)
-add_dependencies(cxx-headers generate-cxx-headers ${LIBCXX_CXX_ABI_HEADER_TARGET})
+target_link_libraries(cxx-headers INTERFACE libcxx-abi-headers)
+add_dependencies(cxx-headers generate-cxx-headers)
 # TODO: Use target_include_directories once we figure out why that breaks the runtimes build
 if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC" OR "${CMAKE_CXX_SIMULATE_ID}" STREQUAL "MSVC")
   target_compile_options(cxx-headers INTERFACE /I${LIBCXX_GENERATED_INCLUDE_DIR}
Index: libcxx/docs/BuildingLibcxx.rst
===================================================================
--- libcxx/docs/BuildingLibcxx.rst
+++ libcxx/docs/BuildingLibcxx.rst
@@ -318,7 +318,7 @@
 
 .. option:: LIBCXX_CXX_ABI:STRING
 
-  **Values**: ``none``, ``libcxxabi``, ``libcxxrt``, ``libstdc++``, ``libsupc++``.
+  **Values**: ``none``, ``libcxxabi``, ``system-libcxxabi``, ``libcxxrt``, ``libstdc++``, ``libsupc++``, ``vcruntime``.
 
   Select the ABI library to build libc++ against.
 
@@ -328,7 +328,8 @@
 
 .. option:: LIBCXX_CXX_ABI_LIBRARY_PATH:PATH
 
-  Provide the path to the ABI library that libc++ should link against.
+  Provide the path to the ABI library that libc++ should link against. This is only
+  useful when linking against an out-of-tree ABI library.
 
 .. option:: LIBCXX_ENABLE_STATIC_ABI_LIBRARY:BOOL
 
Index: libcxx/cmake/Modules/HandleLibCXXABI.cmake
===================================================================
--- libcxx/cmake/Modules/HandleLibCXXABI.cmake
+++ libcxx/cmake/Modules/HandleLibCXXABI.cmake
@@ -1,143 +1,176 @@
 #===============================================================================
-# Add an ABI library if appropriate
+# Define targets for linking against the selected ABI library
+#
+# After including this file, the following targets are defined:
+# - libcxx-abi-headers: An interface target that allows getting access to the
+#                       headers of the selected ABI library.
+# - libcxx-abi-shared: A target representing the selected shared ABI library.
+# - libcxx-abi-static: A target representing the selected static ABI library.
 #===============================================================================
 
 include(GNUInstallDirs)
 
+# This function copies the provided headers to a private directory and adds that
+# path to the given INTERFACE target. That target can then be linked against to
+# get access to those headers (and only those).
 #
-# _setup_abi: Set up the build to use an ABI library
-#
-# Parameters:
-#   abidefines: A list of defines needed to compile libc++ with the ABI library
-#   abishared : The shared ABI library to link against.
-#   abistatic : The static ABI library to link against.
-#   abifiles  : A list of files (which may be relative paths) to copy into the
-#               libc++ build tree for the build.  These files will be copied
-#               twice: once into include/, so the libc++ build itself can find
-#               them, and once into include/c++/v1, so that a clang built into
-#               the same build area will find them.  These files will also be
-#               installed alongside the libc++ headers.
-#   abidirs   : A list of relative paths to create under an include directory
-#               in the libc++ build directory.
-#
-
-macro(setup_abi_lib abidefines abishared abistatic abifiles abidirs)
-  list(APPEND LIBCXX_COMPILE_FLAGS ${abidefines})
-  set(LIBCXX_CXX_ABI_INCLUDE_PATHS "${LIBCXX_CXX_ABI_INCLUDE_PATHS}"
-    CACHE PATH
-    "Paths to C++ ABI header directories separated by ';'." FORCE
-    )
-  set(LIBCXX_CXX_ABI_LIBRARY_PATH "${LIBCXX_CXX_ABI_LIBRARY_PATH}"
-    CACHE PATH
-    "Paths to C++ ABI library directory"
-    )
-  set(LIBCXX_CXX_SHARED_ABI_LIBRARY ${abishared})
-  set(LIBCXX_CXX_STATIC_ABI_LIBRARY ${abistatic})
-  set(LIBCXX_ABILIB_FILES ${abifiles})
-
-  foreach(fpath ${LIBCXX_ABILIB_FILES})
+# The problem this solves is that when building against a system-provided ABI library,
+# the ABI headers might live side-by-side with an actual C++ Standard Library
+# installation. For that reason, we can't just add `-I <path-to-ABI-headers>`,
+# since we would end up also adding the system-provided C++ Standard Library to
+# the search path. Instead, what we do is copy just the ABI library headers to
+# a private directory and add just that path when we build libc++.
+function(import_private_headers target include_dirs headers)
+  foreach(header ${headers})
     set(found FALSE)
-    foreach(incpath ${LIBCXX_CXX_ABI_INCLUDE_PATHS})
-      message(STATUS "Looking for ${fpath} in ${incpath}")
-      if (EXISTS "${incpath}/${fpath}")
+    foreach(incpath ${include_dirs})
+      message(STATUS "Looking for ${header} in ${incpath}")
+      if (EXISTS "${incpath}/${header}")
         set(found TRUE)
-        message(STATUS "Looking for ${fpath} in ${incpath} - found")
-        get_filename_component(dstdir ${fpath} PATH)
-        get_filename_component(ifile ${fpath} NAME)
-        set(src ${incpath}/${fpath})
-
-        set(dst ${LIBCXX_BINARY_INCLUDE_DIR}/${dstdir}/${ifile})
-        add_custom_command(OUTPUT ${dst}
-            DEPENDS ${src}
-            COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src} ${dst}
-            COMMENT "Copying C++ ABI header ${fpath}...")
-        list(APPEND abilib_headers "${dst}")
+        message(STATUS "Looking for ${header} in ${incpath} - found")
+        get_filename_component(dstdir ${header} PATH)
+        get_filename_component(header_file ${header} NAME)
+        set(src ${incpath}/${header})
+        set(dst "${LIBCXX_BINARY_DIR}/private-abi-headers/${dstdir}/${header_file}")
 
-        # TODO: libc++ shouldn't be responsible for copying the libc++abi
-        # headers into the right location.
-        set(dst "${LIBCXX_GENERATED_INCLUDE_DIR}/${dstdir}/${fpath}")
         add_custom_command(OUTPUT ${dst}
             DEPENDS ${src}
+            COMMAND ${CMAKE_COMMAND} -E make_directory "${LIBCXX_BINARY_DIR}/private-abi-headers/${dstdir}"
             COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src} ${dst}
-            COMMENT "Copying C++ ABI header ${fpath}...")
+            COMMENT "Copying C++ ABI header ${header}")
         list(APPEND abilib_headers "${dst}")
 
+        # TODO: libc++ should not be responsible for installing the headers of the ABI library.
         if (LIBCXX_INSTALL_HEADERS)
-          install(FILES "${LIBCXX_BINARY_INCLUDE_DIR}/${fpath}"
+          install(FILES "${dst}"
             DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/c++/v1/${dstdir}"
             COMPONENT cxx-headers
             PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ
             )
         endif()
       else()
-        message(STATUS "Looking for ${fpath} in ${incpath} - not found")
+        message(STATUS "Looking for ${header} in ${incpath} - not found")
       endif()
     endforeach()
     if (NOT found)
-      message(WARNING "Failed to find ${fpath} in ${LIBCXX_CXX_ABI_INCLUDE_PATHS}")
+      message(WARNING "Failed to find ${header} in ${include_dirs}")
     endif()
   endforeach()
 
-  include_directories("${LIBCXX_BINARY_INCLUDE_DIR}")
-  add_custom_target(cxx_abi_headers ALL DEPENDS ${abilib_headers})
-  set(LIBCXX_CXX_ABI_HEADER_TARGET "cxx_abi_headers")
-endmacro()
-
-
-# Configure based on the selected ABI library.
-if ("${LIBCXX_CXX_ABI_LIBNAME}" STREQUAL "libstdc++" OR
-    "${LIBCXX_CXX_ABI_LIBNAME}" STREQUAL "libsupc++")
-  set(_LIBSUPCXX_INCLUDE_FILES
-    cxxabi.h bits/c++config.h bits/os_defines.h bits/cpu_defines.h
-    bits/cxxabi_tweaks.h bits/cxxabi_forced.h
-    )
-  if ("${LIBCXX_CXX_ABI_LIBNAME}" STREQUAL "libstdc++")
-    set(_LIBSUPCXX_DEFINES "-DLIBSTDCXX")
-    set(_LIBSUPCXX_LIBNAME stdc++)
-  else()
-    set(_LIBSUPCXX_DEFINES "")
-    set(_LIBSUPCXX_LIBNAME supc++)
-  endif()
-  setup_abi_lib(
-    "-D__GLIBCXX__ ${_LIBSUPCXX_DEFINES}"
-    "${_LIBSUPCXX_LIBNAME}" "${_LIBSUPCXX_LIBNAME}" "${_LIBSUPCXX_INCLUDE_FILES}" "bits"
-    )
-elseif ("${LIBCXX_CXX_ABI_LIBNAME}" STREQUAL "libcxxabi")
-  if(NOT LIBCXX_CXX_ABI_INCLUDE_PATHS)
-    set(LIBCXX_CXX_ABI_INCLUDE_PATHS "${LIBCXX_SOURCE_DIR}/../libcxxabi/include")
+  target_sources(${target} INTERFACE ${abilib_headers})
+  target_include_directories(${target} INTERFACE "${LIBCXX_BINARY_DIR}/private-abi-headers")
+endfunction()
+
+function(import_shared_library target path name)
+  find_library(file
+    NAMES "${CMAKE_SHARED_LIBRARY_PREFIX}${name}${CMAKE_SHARED_LIBRARY_SUFFIX}"
+    PATHS "${path}")
+  set_target_properties(${target} PROPERTIES IMPORTED_LOCATION "${file}")
+endfunction()
+
+function(import_static_library target path name)
+  find_library(file
+    NAMES "${CMAKE_STATIC_LIBRARY_PREFIX}${name}${CMAKE_STATIC_LIBRARY_SUFFIX}"
+    PATHS "${path}")
+  set_target_properties(${target} PROPERTIES IMPORTED_LOCATION "${file}")
+endfunction()
+
+# Link against a system-provided libstdc++
+if ("${LIBCXX_CXX_ABI}" STREQUAL "libstdc++")
+  add_library(libcxx-abi-headers INTERFACE)
+  import_private_headers(libcxx-abi-headers "${LIBCXX_CXX_ABI_INCLUDE_PATHS}"
+    "cxxabi.h;bits/c++config.h;bits/os_defines.h;bits/cpu_defines.h;bits/cxxabi_tweaks.h;bits/cxxabi_forced.h")
+  target_compile_definitions(libcxx-abi-headers INTERFACE "-DLIBSTDCXX" "-D__GLIBCXX__")
+
+  add_library(libcxx-abi-shared SHARED IMPORTED GLOBAL)
+  target_link_libraries(libcxx-abi-shared INTERFACE libcxx-abi-headers)
+  import_shared_library(libcxx-abi-shared "${LIBCXX_CXX_ABI_LIBRARY_PATH}" stdc++)
+
+  add_library(libcxx-abi-static STATIC IMPORTED GLOBAL)
+  target_link_libraries(libcxx-abi-static INTERFACE libcxx-abi-headers)
+  import_shared_library(libcxx-abi-static "${LIBCXX_CXX_ABI_LIBRARY_PATH}" stdc++)
+
+# Link against a system-provided libsupc++
+elseif ("${LIBCXX_CXX_ABI}" STREQUAL "libsupc++")
+add_library(libcxx-abi-headers INTERFACE)
+  import_private_headers(libcxx-abi-headers "${LIBCXX_CXX_ABI_INCLUDE_PATHS}"
+    "cxxabi.h;bits/c++config.h;bits/os_defines.h;bits/cpu_defines.h;bits/cxxabi_tweaks.h;bits/cxxabi_forced.h")
+  target_compile_definitions(libcxx-abi-headers INTERFACE "-D__GLIBCXX__")
+
+  add_library(libcxx-abi-shared SHARED IMPORTED GLOBAL)
+  target_link_libraries(libcxx-abi-shared INTERFACE libcxx-abi-headers)
+  import_shared_library(libcxx-abi-shared "${LIBCXX_CXX_ABI_LIBRARY_PATH}" supc++)
+
+  add_library(libcxx-abi-static STATIC IMPORTED GLOBAL)
+  target_link_libraries(libcxx-abi-static INTERFACE libcxx-abi-headers)
+  import_shared_library(libcxx-abi-static "${LIBCXX_CXX_ABI_LIBRARY_PATH}" supc++)
+
+# Link against the in-tree libc++abi
+elseif ("${LIBCXX_CXX_ABI}" STREQUAL "libcxxabi")
+  add_library(libcxx-abi-headers INTERFACE)
+  target_link_libraries(libcxx-abi-headers INTERFACE cxxabi-headers)
+  target_compile_definitions(libcxx-abi-headers INTERFACE "-DLIBCXX_BUILDING_LIBCXXABI")
+
+  if (TARGET cxxabi_shared)
+    add_library(libcxx-abi-shared ALIAS cxxabi_shared)
   endif()
 
-  if(LIBCXX_STANDALONE_BUILD AND NOT (LIBCXX_CXX_ABI_INTREE OR HAVE_LIBCXXABI))
-    set(shared c++abi)
-    set(static c++abi)
-  else()
-    set(shared cxxabi_shared)
-    set(static cxxabi_static)
+  if (TARGET cxxabi_static)
+    add_library(libcxx-abi-static ALIAS cxxabi_static)
   endif()
 
-  setup_abi_lib(
-    "-DLIBCXX_BUILDING_LIBCXXABI"
-    "${shared}" "${static}" "cxxabi.h;__cxxabi_config.h" "")
-elseif ("${LIBCXX_CXX_ABI_LIBNAME}" STREQUAL "libcxxrt")
+# Link against a system-provided libc++abi
+elseif ("${LIBCXX_CXX_ABI}" STREQUAL "system-libcxxabi")
+  add_library(libcxx-abi-headers INTERFACE)
+  import_private_headers(libcxx-abi-headers "${LIBCXX_CXX_ABI_INCLUDE_PATHS}" "cxxabi.h;__cxxabi_config.h")
+  target_compile_definitions(libcxx-abi-headers INTERFACE "-DLIBCXX_BUILDING_LIBCXXABI")
+
+  add_library(libcxx-abi-shared SHARED IMPORTED GLOBAL)
+  target_link_libraries(libcxx-abi-shared INTERFACE libcxx-abi-headers)
+  import_shared_library(libcxx-abi-shared "${LIBCXX_CXX_ABI_LIBRARY_PATH}" c++abi)
+
+  add_library(libcxx-abi-static STATIC IMPORTED GLOBAL)
+  target_link_libraries(libcxx-abi-static INTERFACE libcxx-abi-headers)
+  import_shared_library(libcxx-abi-static "${LIBCXX_CXX_ABI_LIBRARY_PATH}" c++abi)
+
+# Link against a system-provided libcxxrt
+elseif ("${LIBCXX_CXX_ABI}" STREQUAL "libcxxrt")
+  # libcxxrt does not provide aligned new and delete operators
+  # TODO: We're keeping this for backwards compatibility, but this doesn't belong here.
+  set(LIBCXX_ENABLE_NEW_DELETE_DEFINITIONS ON)
+
   if(NOT LIBCXX_CXX_ABI_INCLUDE_PATHS)
+    message(STATUS "LIBCXX_CXX_ABI_INCLUDE_PATHS not set, using /usr/include/c++/v1")
     set(LIBCXX_CXX_ABI_INCLUDE_PATHS "/usr/include/c++/v1")
   endif()
-  # libcxxrt does not provide aligned new and delete operators
-  set(LIBCXX_ENABLE_NEW_DELETE_DEFINITIONS ON)
-  setup_abi_lib(
-    "-DLIBCXXRT"
-    "cxxrt" "cxxrt" "cxxabi.h;unwind.h;unwind-arm.h;unwind-itanium.h" ""
-    )
-elseif ("${LIBCXX_CXX_ABI_LIBNAME}" STREQUAL "vcruntime")
- # Nothing to do
-elseif ("${LIBCXX_CXX_ABI_LIBNAME}" STREQUAL "none")
-  list(APPEND LIBCXX_COMPILE_FLAGS "-D_LIBCPP_BUILDING_HAS_NO_ABI_LIBRARY")
-elseif ("${LIBCXX_CXX_ABI_LIBNAME}" STREQUAL "default")
-  # Nothing to do
-else()
-  message(FATAL_ERROR
-    "Unsupported c++ abi: '${LIBCXX_CXX_ABI_LIBNAME}'. \
-     Currently libstdc++, libsupc++, libcxxabi, libcxxrt, default and none are
-     supported for c++ abi."
-    )
-endif ()
+  add_library(libcxx-abi-headers INTERFACE)
+  import_private_headers(libcxx-abi-headers "${LIBCXX_CXX_ABI_INCLUDE_PATHS}"
+    "cxxabi.h;unwind.h;unwind-arm.h;unwind-itanium.h")
+  target_compile_definitions(libcxx-abi-headers INTERFACE "-DLIBCXXRT")
+
+  add_library(libcxx-abi-shared SHARED IMPORTED GLOBAL)
+  target_link_libraries(libcxx-abi-shared INTERFACE libcxx-abi-headers)
+  import_shared_library(libcxx-abi-shared "${LIBCXX_CXX_ABI_LIBRARY_PATH}" cxxrt)
+
+  add_library(libcxx-abi-static STATIC IMPORTED GLOBAL)
+  target_link_libraries(libcxx-abi-static INTERFACE libcxx-abi-headers)
+  import_shared_library(libcxx-abi-static "${LIBCXX_CXX_ABI_LIBRARY_PATH}" cxxrt)
+
+# Link against a system-provided vcruntime
+# FIXME: Figure out how to configure the ABI library on Windows.
+elseif ("${LIBCXX_CXX_ABI}" STREQUAL "vcruntime")
+  add_library(libcxx-abi-headers INTERFACE)
+  add_library(libcxx-abi-shared INTERFACE)
+  add_library(libcxx-abi-static INTERFACE)
+
+# Don't link against any ABI library
+elseif ("${LIBCXX_CXX_ABI}" STREQUAL "none")
+  add_library(libcxx-abi-headers INTERFACE)
+  target_compile_definitions(libcxx-abi-headers INTERFACE "-D_LIBCPP_BUILDING_HAS_NO_ABI_LIBRARY")
+
+  add_library(libcxx-abi-shared INTERFACE)
+  target_link_libraries(libcxx-abi-shared INTERFACE libcxx-abi-headers)
+
+  add_library(libcxx-abi-static INTERFACE)
+  target_link_libraries(libcxx-abi-static INTERFACE libcxx-abi-headers)
+endif()
Index: libcxx/CMakeLists.txt
===================================================================
--- libcxx/CMakeLists.txt
+++ libcxx/CMakeLists.txt
@@ -217,24 +217,30 @@
                                                Note that this is not related to the version of libc++'s ABI itself!")
 
 # ABI Library options ---------------------------------------------------------
-set(LIBCXX_CXX_ABI "default" CACHE STRING "Specify C++ ABI library to use.")
-set(CXXABIS none default libcxxabi libcxxrt libstdc++ libsupc++ vcruntime)
-set_property(CACHE LIBCXX_CXX_ABI PROPERTY STRINGS ;${CXXABIS})
+if (LIBCXX_TARGETING_MSVC)
+  set(LIBCXX_DEFAULT_ABI_LIBRARY "vcruntime")
+elseif (${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
+  set(LIBCXX_DEFAULT_ABI_LIBRARY "libcxxrt")
+else()
+  set(LIBCXX_DEFAULT_ABI_LIBRARY "libcxxabi")
+endif()
+
+set(LIBCXX_SUPPORTED_ABI_LIBRARIES none libcxxabi system-libcxxabi libcxxrt libstdc++ libsupc++ vcruntime)
+set(LIBCXX_CXX_ABI "${LIBCXX_DEFAULT_ABI_LIBRARY}" CACHE STRING "Specify C++ ABI library to use. Supported values are ${LIBCXX_SUPPORTED_ABI_LIBRARIES}.")
 
-# Setup the default options if LIBCXX_CXX_ABI is not specified.
+# Temporary to still accept existing CMake caches that contain "default" as the value
 if (LIBCXX_CXX_ABI STREQUAL "default")
   if (LIBCXX_TARGETING_MSVC)
-    # FIXME: Figure out how to configure the ABI library on Windows.
-    set(LIBCXX_CXX_ABI_LIBNAME "vcruntime")
+    set(LIBCXX_CXX_ABI "vcruntime")
   elseif (${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
-    set(LIBCXX_CXX_ABI_LIBNAME "libcxxrt")
-  elseif (NOT LIBCXX_STANDALONE_BUILD OR HAVE_LIBCXXABI)
-    set(LIBCXX_CXX_ABI_LIBNAME "libcxxabi")
+    set(LIBCXX_CXX_ABI "libcxxrt")
   else()
-    set(LIBCXX_CXX_ABI_LIBNAME "default")
+    set(LIBCXX_CXX_ABI "libcxxabi")
   endif()
-else()
-  set(LIBCXX_CXX_ABI_LIBNAME "${LIBCXX_CXX_ABI}")
+endif()
+
+if (NOT "${LIBCXX_CXX_ABI}" IN_LIST LIBCXX_SUPPORTED_ABI_LIBRARIES)
+  message(FATAL_ERROR "Unsupported C++ ABI library: '${LIBCXX_CXX_ABI}'. Supported values are ${LIBCXX_SUPPORTED_ABI_LIBRARIES}.")
 endif()
 
 option(LIBCXX_ENABLE_STATIC_ABI_LIBRARY
@@ -256,8 +262,7 @@
 # or is specified to be "none".
 set(ENABLE_LINKER_SCRIPT_DEFAULT_VALUE OFF)
 if (LLVM_HAVE_LINK_VERSION_SCRIPT AND NOT LIBCXX_STATICALLY_LINK_ABI_IN_SHARED_LIBRARY
-      AND NOT LIBCXX_CXX_ABI_LIBNAME STREQUAL "none"
-      AND NOT LIBCXX_CXX_ABI_LIBNAME STREQUAL "default"
+      AND NOT LIBCXX_CXX_ABI STREQUAL "none"
       AND Python3_EXECUTABLE
       AND LIBCXX_ENABLE_SHARED)
     set(ENABLE_LINKER_SCRIPT_DEFAULT_VALUE ON)
Index: clang/cmake/caches/CrossWinToARMLinux.cmake
===================================================================
--- clang/cmake/caches/CrossWinToARMLinux.cmake
+++ clang/cmake/caches/CrossWinToARMLinux.cmake
@@ -156,8 +156,6 @@
 set(RUNTIMES_${TARGET_TRIPLE}_LIBCXX_ENABLE_SHARED                      OFF CACHE BOOL "")
 set(RUNTIMES_${TARGET_TRIPLE}_LIBCXX_ABI_VERSION                        2 CACHE STRING "")
 set(RUNTIMES_${TARGET_TRIPLE}_LIBCXX_CXX_ABI                            "libcxxabi" CACHE STRING "")    #!!!
-set(RUNTIMES_${TARGET_TRIPLE}_LIBCXX_CXX_ABI_INCLUDE_PATHS              "${LLVM_PROJECT_DIR}/libcxxabi/include" CACHE PATH "")
-set(RUNTIMES_${TARGET_TRIPLE}_LIBCXX_CXX_ABI_LIBRARY_PATH               "${CMAKE_BINARY_DIR}/lib/${TARGET_TRIPLE}" CACHE PATH "")
 set(RUNTIMES_${TARGET_TRIPLE}_LIBCXX_ENABLE_NEW_DELETE_DEFINITIONS      ON CACHE BOOL "")
 
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to