1#=============================================================================== 2# Define targets for linking against the selected ABI library 3# 4# After including this file, the following targets are defined: 5# - libcxx-abi-headers: An interface target that allows getting access to the 6# headers of the selected ABI library. 7# - libcxx-abi-shared: A target representing the selected shared ABI library. 8# - libcxx-abi-static: A target representing the selected static ABI library. 9# 10# Furthermore, some ABI libraries also define the following target: 11# - libcxx-abi-shared-objects: An object library representing a set of object files 12# constituting the ABI library, suitable for bundling 13# into a shared library. 14# - libcxx-abi-static-objects: An object library representing a set of object files 15# constituting the ABI library, suitable for bundling 16# into a static library. 17#=============================================================================== 18 19include(GNUInstallDirs) 20 21# This function copies the provided headers to a private directory and adds that 22# path to the given INTERFACE target. That target can then be linked against to 23# get access to those headers (and only those). 24# 25# The problem this solves is that when building against a system-provided ABI library, 26# the ABI headers might live side-by-side with an actual C++ Standard Library 27# installation. For that reason, we can't just add `-I <path-to-ABI-headers>`, 28# since we would end up also adding the system-provided C++ Standard Library to 29# the search path. Instead, what we do is copy just the ABI library headers to 30# a private directory and add just that path when we build libc++. 31function(import_private_headers target include_dirs headers) 32 foreach(header ${headers}) 33 set(found FALSE) 34 foreach(incpath ${include_dirs}) 35 if (EXISTS "${incpath}/${header}") 36 set(found TRUE) 37 message(STATUS "Looking for ${header} in ${incpath} - found") 38 get_filename_component(dstdir ${header} PATH) 39 get_filename_component(header_file ${header} NAME) 40 set(src ${incpath}/${header}) 41 set(dst "${LIBCXX_BINARY_DIR}/private-abi-headers/${dstdir}/${header_file}") 42 43 add_custom_command(OUTPUT ${dst} 44 DEPENDS ${src} 45 COMMAND ${CMAKE_COMMAND} -E make_directory "${LIBCXX_BINARY_DIR}/private-abi-headers/${dstdir}" 46 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src} ${dst} 47 COMMENT "Copying C++ ABI header ${header}") 48 list(APPEND abilib_headers "${dst}") 49 else() 50 message(STATUS "Looking for ${header} in ${incpath} - not found") 51 endif() 52 endforeach() 53 if (NOT found) 54 message(WARNING "Failed to find ${header} in ${include_dirs}") 55 endif() 56 endforeach() 57 58 # Work around https://gitlab.kitware.com/cmake/cmake/-/issues/18399 59 add_library(${target}-generate-private-headers OBJECT ${abilib_headers}) 60 set_target_properties(${target}-generate-private-headers PROPERTIES LINKER_LANGUAGE CXX) 61 62 target_link_libraries(${target} INTERFACE ${target}-generate-private-headers) 63 target_include_directories(${target} INTERFACE "${LIBCXX_BINARY_DIR}/private-abi-headers") 64endfunction() 65 66# This function creates an imported static library named <target>. 67# It imports a library named <name> searched at the given <path>. 68function(import_static_library target path name) 69 add_library(${target} STATIC IMPORTED GLOBAL) 70 find_library(file 71 NAMES "${CMAKE_STATIC_LIBRARY_PREFIX}${name}${CMAKE_STATIC_LIBRARY_SUFFIX}" 72 PATHS "${path}" 73 NO_CACHE) 74 set_target_properties(${target} PROPERTIES IMPORTED_LOCATION "${file}") 75endfunction() 76 77# This function creates an imported shared (interface) library named <target> 78# for the given library <name>. 79function(import_shared_library target name) 80 add_library(${target} INTERFACE IMPORTED GLOBAL) 81 set_target_properties(${target} PROPERTIES IMPORTED_LIBNAME "${name}") 82endfunction() 83 84# Link against a system-provided libstdc++ 85if ("${LIBCXX_CXX_ABI}" STREQUAL "libstdc++") 86 add_library(libcxx-abi-headers INTERFACE) 87 import_private_headers(libcxx-abi-headers "${LIBCXX_CXX_ABI_INCLUDE_PATHS}" 88 "cxxabi.h;bits/c++config.h;bits/os_defines.h;bits/cpu_defines.h;bits/cxxabi_tweaks.h;bits/cxxabi_forced.h") 89 target_compile_definitions(libcxx-abi-headers INTERFACE "-DLIBSTDCXX" "-D__GLIBCXX__") 90 91 import_shared_library(libcxx-abi-shared stdc++) 92 target_link_libraries(libcxx-abi-shared INTERFACE libcxx-abi-headers) 93 94 import_static_library(libcxx-abi-static "${LIBCXX_CXX_ABI_LIBRARY_PATH}" stdc++) 95 target_link_libraries(libcxx-abi-static INTERFACE libcxx-abi-headers) 96 97# Link against a system-provided libsupc++ 98elseif ("${LIBCXX_CXX_ABI}" STREQUAL "libsupc++") 99 add_library(libcxx-abi-headers INTERFACE) 100 import_private_headers(libcxx-abi-headers "${LIBCXX_CXX_ABI_INCLUDE_PATHS}" 101 "cxxabi.h;bits/c++config.h;bits/os_defines.h;bits/cpu_defines.h;bits/cxxabi_tweaks.h;bits/cxxabi_forced.h") 102 target_compile_definitions(libcxx-abi-headers INTERFACE "-D__GLIBCXX__") 103 104 import_shared_library(libcxx-abi-shared supc++) 105 target_link_libraries(libcxx-abi-shared INTERFACE libcxx-abi-headers) 106 107 import_static_library(libcxx-abi-static "${LIBCXX_CXX_ABI_LIBRARY_PATH}" supc++) 108 target_link_libraries(libcxx-abi-static INTERFACE libcxx-abi-headers) 109 110# Link against the in-tree libc++abi 111elseif ("${LIBCXX_CXX_ABI}" STREQUAL "libcxxabi") 112 add_library(libcxx-abi-headers INTERFACE) 113 target_link_libraries(libcxx-abi-headers INTERFACE cxxabi-headers) 114 target_compile_definitions(libcxx-abi-headers INTERFACE "-DLIBCXX_BUILDING_LIBCXXABI") 115 116 if (TARGET cxxabi_shared) 117 add_library(libcxx-abi-shared ALIAS cxxabi_shared) 118 endif() 119 120 if (TARGET cxxabi_static) 121 add_library(libcxx-abi-static ALIAS cxxabi_static) 122 endif() 123 124 if (TARGET cxxabi_shared_objects) 125 add_library(libcxx-abi-shared-objects ALIAS cxxabi_shared_objects) 126 endif() 127 128 if (TARGET cxxabi_static_objects) 129 add_library(libcxx-abi-static-objects ALIAS cxxabi_static_objects) 130 endif() 131 132# Link against a system-provided libc++abi 133elseif ("${LIBCXX_CXX_ABI}" STREQUAL "system-libcxxabi") 134 add_library(libcxx-abi-headers INTERFACE) 135 import_private_headers(libcxx-abi-headers "${LIBCXX_CXX_ABI_INCLUDE_PATHS}" "cxxabi.h;__cxxabi_config.h") 136 target_compile_definitions(libcxx-abi-headers INTERFACE "-DLIBCXX_BUILDING_LIBCXXABI") 137 138 import_shared_library(libcxx-abi-shared c++abi) 139 target_link_libraries(libcxx-abi-shared INTERFACE libcxx-abi-headers) 140 141 import_static_library(libcxx-abi-static "${LIBCXX_CXX_ABI_LIBRARY_PATH}" c++abi) 142 target_link_libraries(libcxx-abi-static INTERFACE libcxx-abi-headers) 143 144# Link against a system-provided libcxxrt 145elseif ("${LIBCXX_CXX_ABI}" STREQUAL "libcxxrt") 146 if(NOT LIBCXX_CXX_ABI_INCLUDE_PATHS) 147 message(STATUS "LIBCXX_CXX_ABI_INCLUDE_PATHS not set, using /usr/include/c++/v1") 148 set(LIBCXX_CXX_ABI_INCLUDE_PATHS "/usr/include/c++/v1") 149 endif() 150 add_library(libcxx-abi-headers INTERFACE) 151 import_private_headers(libcxx-abi-headers "${LIBCXX_CXX_ABI_INCLUDE_PATHS}" 152 "cxxabi.h;unwind.h;unwind-arm.h;unwind-itanium.h") 153 target_compile_definitions(libcxx-abi-headers INTERFACE "-DLIBCXXRT") 154 155 import_shared_library(libcxx-abi-shared cxxrt) 156 target_link_libraries(libcxx-abi-shared INTERFACE libcxx-abi-headers) 157 158 import_static_library(libcxx-abi-static "${LIBCXX_CXX_ABI_LIBRARY_PATH}" cxxrt) 159 target_link_libraries(libcxx-abi-static INTERFACE libcxx-abi-headers) 160 161# Link against a system-provided vcruntime 162# FIXME: Figure out how to configure the ABI library on Windows. 163elseif ("${LIBCXX_CXX_ABI}" STREQUAL "vcruntime") 164 add_library(libcxx-abi-headers INTERFACE) 165 add_library(libcxx-abi-shared INTERFACE) 166 add_library(libcxx-abi-static INTERFACE) 167 168# Don't link against any ABI library 169elseif ("${LIBCXX_CXX_ABI}" STREQUAL "none") 170 add_library(libcxx-abi-headers INTERFACE) 171 target_compile_definitions(libcxx-abi-headers INTERFACE "-D_LIBCPP_BUILDING_HAS_NO_ABI_LIBRARY") 172 173 add_library(libcxx-abi-shared INTERFACE) 174 target_link_libraries(libcxx-abi-shared INTERFACE libcxx-abi-headers) 175 176 add_library(libcxx-abi-static INTERFACE) 177 target_link_libraries(libcxx-abi-static INTERFACE libcxx-abi-headers) 178endif() 179