[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / libcxx / cmake / Modules / HandleLibCXXABI.cmake
blob34e9a672a960f9c8a93a7ac9c167dab10dc73797
1 #===============================================================================
2 # Define targets for linking against the selected ABI library
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.
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 #===============================================================================
19 include(GNUInstallDirs)
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).
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++.
31 function(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}")
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()
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)
62   target_link_libraries(${target} INTERFACE ${target}-generate-private-headers)
63   target_include_directories(${target} INTERFACE "${LIBCXX_BINARY_DIR}/private-abi-headers")
64 endfunction()
66 # This function creates an imported static library named <target>.
67 # It imports a library named <name> searched at the given <path>.
68 function(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}")
75 endfunction()
77 # This function creates an imported shared (interface) library named <target>
78 # for the given library <name>.
79 function(import_shared_library target name)
80   add_library(${target} INTERFACE IMPORTED GLOBAL)
81   set_target_properties(${target} PROPERTIES IMPORTED_LIBNAME "${name}")
82 endfunction()
84 # Link against a system-provided libstdc++
85 if ("${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__")
91   import_shared_library(libcxx-abi-shared stdc++)
92   target_link_libraries(libcxx-abi-shared INTERFACE libcxx-abi-headers)
94   import_static_library(libcxx-abi-static "${LIBCXX_CXX_ABI_LIBRARY_PATH}" stdc++)
95   target_link_libraries(libcxx-abi-static INTERFACE libcxx-abi-headers)
97 # Link against a system-provided libsupc++
98 elseif ("${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__")
104   import_shared_library(libcxx-abi-shared supc++)
105   target_link_libraries(libcxx-abi-shared INTERFACE libcxx-abi-headers)
107   import_static_library(libcxx-abi-static "${LIBCXX_CXX_ABI_LIBRARY_PATH}" supc++)
108   target_link_libraries(libcxx-abi-static INTERFACE libcxx-abi-headers)
110 # Link against the in-tree libc++abi
111 elseif ("${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")
116   if (TARGET cxxabi_shared)
117     add_library(libcxx-abi-shared ALIAS cxxabi_shared)
118   endif()
120   if (TARGET cxxabi_static)
121     add_library(libcxx-abi-static ALIAS cxxabi_static)
122   endif()
124   if (TARGET cxxabi_shared_objects)
125     add_library(libcxx-abi-shared-objects ALIAS cxxabi_shared_objects)
126   endif()
128   if (TARGET cxxabi_static_objects)
129     add_library(libcxx-abi-static-objects ALIAS cxxabi_static_objects)
130   endif()
132 # Link against a system-provided libc++abi
133 elseif ("${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")
138   import_shared_library(libcxx-abi-shared c++abi)
139   target_link_libraries(libcxx-abi-shared INTERFACE libcxx-abi-headers)
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)
144 # Link against a system-provided libcxxrt
145 elseif ("${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")
155   import_shared_library(libcxx-abi-shared cxxrt)
156   target_link_libraries(libcxx-abi-shared INTERFACE libcxx-abi-headers)
158   import_static_library(libcxx-abi-static "${LIBCXX_CXX_ABI_LIBRARY_PATH}" cxxrt)
159   target_link_libraries(libcxx-abi-static INTERFACE libcxx-abi-headers)
161 # Link against a system-provided vcruntime
162 # FIXME: Figure out how to configure the ABI library on Windows.
163 elseif ("${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)
168 # Don't link against any ABI library
169 elseif ("${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")
173   add_library(libcxx-abi-shared INTERFACE)
174   target_link_libraries(libcxx-abi-shared INTERFACE libcxx-abi-headers)
176   add_library(libcxx-abi-static INTERFACE)
177   target_link_libraries(libcxx-abi-static INTERFACE libcxx-abi-headers)
178 endif()