[WebAssembly] Fix asan issue from https://reviews.llvm.org/D121349
[llvm-project.git] / libcxx / cmake / Modules / HandleLibCXXABI.cmake
blob9b5df6e015b35a7c0c26c2d131a29d547df0a664
1 #===============================================================================
2 # Add an ABI library if appropriate
3 #===============================================================================
5 include(GNUInstallDirs)
8 # _setup_abi: Set up the build to use an ABI library
10 # Parameters:
11 #   abidefines: A list of defines needed to compile libc++ with the ABI library
12 #   abishared : The shared ABI library to link against.
13 #   abistatic : The static ABI library to link against.
14 #   abifiles  : A list of files (which may be relative paths) to copy into the
15 #               libc++ build tree for the build.  These files will be copied
16 #               twice: once into include/, so the libc++ build itself can find
17 #               them, and once into include/c++/v1, so that a clang built into
18 #               the same build area will find them.  These files will also be
19 #               installed alongside the libc++ headers.
20 #   abidirs   : A list of relative paths to create under an include directory
21 #               in the libc++ build directory.
24 macro(setup_abi_lib abidefines abishared abistatic abifiles abidirs)
25   list(APPEND LIBCXX_COMPILE_FLAGS ${abidefines})
26   set(LIBCXX_CXX_ABI_INCLUDE_PATHS "${LIBCXX_CXX_ABI_INCLUDE_PATHS}"
27     CACHE PATH
28     "Paths to C++ ABI header directories separated by ';'." FORCE
29     )
30   set(LIBCXX_CXX_ABI_LIBRARY_PATH "${LIBCXX_CXX_ABI_LIBRARY_PATH}"
31     CACHE PATH
32     "Paths to C++ ABI library directory"
33     )
34   set(LIBCXX_CXX_SHARED_ABI_LIBRARY ${abishared})
35   set(LIBCXX_CXX_STATIC_ABI_LIBRARY ${abistatic})
36   set(LIBCXX_ABILIB_FILES ${abifiles})
38   foreach(fpath ${LIBCXX_ABILIB_FILES})
39     set(found FALSE)
40     foreach(incpath ${LIBCXX_CXX_ABI_INCLUDE_PATHS})
41       message(STATUS "Looking for ${fpath} in ${incpath}")
42       if (EXISTS "${incpath}/${fpath}")
43         set(found TRUE)
44         message(STATUS "Looking for ${fpath} in ${incpath} - found")
45         get_filename_component(dstdir ${fpath} PATH)
46         get_filename_component(ifile ${fpath} NAME)
47         set(src ${incpath}/${fpath})
49         set(dst ${LIBCXX_BINARY_INCLUDE_DIR}/${dstdir}/${ifile})
50         add_custom_command(OUTPUT ${dst}
51             DEPENDS ${src}
52             COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src} ${dst}
53             COMMENT "Copying C++ ABI header ${fpath}...")
54         list(APPEND abilib_headers "${dst}")
56         # TODO: libc++ shouldn't be responsible for copying the libc++abi
57         # headers into the right location.
58         set(dst "${LIBCXX_GENERATED_INCLUDE_DIR}/${dstdir}/${fpath}")
59         add_custom_command(OUTPUT ${dst}
60             DEPENDS ${src}
61             COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src} ${dst}
62             COMMENT "Copying C++ ABI header ${fpath}...")
63         list(APPEND abilib_headers "${dst}")
65         if (LIBCXX_INSTALL_HEADERS)
66           install(FILES "${LIBCXX_BINARY_INCLUDE_DIR}/${fpath}"
67             DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/c++/v1/${dstdir}"
68             COMPONENT cxx-headers
69             PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ
70             )
71         endif()
72       else()
73         message(STATUS "Looking for ${fpath} in ${incpath} - not found")
74       endif()
75     endforeach()
76     if (NOT found)
77       message(WARNING "Failed to find ${fpath} in ${LIBCXX_CXX_ABI_INCLUDE_PATHS}")
78     endif()
79   endforeach()
81   include_directories("${LIBCXX_BINARY_INCLUDE_DIR}")
82   add_custom_target(cxx_abi_headers ALL DEPENDS ${abilib_headers})
83   set(LIBCXX_CXX_ABI_HEADER_TARGET "cxx_abi_headers")
84 endmacro()
87 # Configure based on the selected ABI library.
88 if ("${LIBCXX_CXX_ABI_LIBNAME}" STREQUAL "libstdc++" OR
89     "${LIBCXX_CXX_ABI_LIBNAME}" STREQUAL "libsupc++")
90   set(_LIBSUPCXX_INCLUDE_FILES
91     cxxabi.h bits/c++config.h bits/os_defines.h bits/cpu_defines.h
92     bits/cxxabi_tweaks.h bits/cxxabi_forced.h
93     )
94   if ("${LIBCXX_CXX_ABI_LIBNAME}" STREQUAL "libstdc++")
95     set(_LIBSUPCXX_DEFINES "-DLIBSTDCXX")
96     set(_LIBSUPCXX_LIBNAME stdc++)
97   else()
98     set(_LIBSUPCXX_DEFINES "")
99     set(_LIBSUPCXX_LIBNAME supc++)
100   endif()
101   setup_abi_lib(
102     "-D__GLIBCXX__ ${_LIBSUPCXX_DEFINES}"
103     "${_LIBSUPCXX_LIBNAME}" "${_LIBSUPCXX_LIBNAME}" "${_LIBSUPCXX_INCLUDE_FILES}" "bits"
104     )
105 elseif ("${LIBCXX_CXX_ABI_LIBNAME}" STREQUAL "libcxxabi")
106   if(NOT LIBCXX_CXX_ABI_INCLUDE_PATHS)
107     set(LIBCXX_CXX_ABI_INCLUDE_PATHS "${LIBCXX_SOURCE_DIR}/../libcxxabi/include")
108   endif()
110   if(LIBCXX_STANDALONE_BUILD AND NOT (LIBCXX_CXX_ABI_INTREE OR HAVE_LIBCXXABI))
111     set(shared c++abi)
112     set(static c++abi)
113   else()
114     set(shared cxxabi_shared)
115     set(static cxxabi_static)
116   endif()
118   setup_abi_lib(
119     "-DLIBCXX_BUILDING_LIBCXXABI"
120     "${shared}" "${static}" "cxxabi.h;__cxxabi_config.h" "")
121 elseif ("${LIBCXX_CXX_ABI_LIBNAME}" STREQUAL "system-libcxxabi")
122   setup_abi_lib(
123     "-DLIBCXX_BUILDING_LIBCXXABI"
124     "c++abi" "c++abi" "cxxabi.h;__cxxabi_config.h" "")
125 elseif ("${LIBCXX_CXX_ABI_LIBNAME}" STREQUAL "libcxxrt")
126   if(NOT LIBCXX_CXX_ABI_INCLUDE_PATHS)
127     set(LIBCXX_CXX_ABI_INCLUDE_PATHS "/usr/include/c++/v1")
128   endif()
129   # libcxxrt does not provide aligned new and delete operators
130   set(LIBCXX_ENABLE_NEW_DELETE_DEFINITIONS ON)
131   setup_abi_lib(
132     "-DLIBCXXRT"
133     "cxxrt" "cxxrt" "cxxabi.h;unwind.h;unwind-arm.h;unwind-itanium.h" ""
134     )
135 elseif ("${LIBCXX_CXX_ABI_LIBNAME}" STREQUAL "vcruntime")
136  # Nothing to do
137 elseif ("${LIBCXX_CXX_ABI_LIBNAME}" STREQUAL "none")
138   list(APPEND LIBCXX_COMPILE_FLAGS "-D_LIBCPP_BUILDING_HAS_NO_ABI_LIBRARY")
139 elseif ("${LIBCXX_CXX_ABI_LIBNAME}" STREQUAL "default")
140   # Nothing to do
141 else()
142   message(FATAL_ERROR
143     "Unsupported c++ abi: '${LIBCXX_CXX_ABI_LIBNAME}'. \
144      Currently libstdc++, libsupc++, libcxxabi, libcxxrt, default and none are
145      supported for c++ abi."
146     )
147 endif ()