Skip to content

Commit 927b897

Browse files
committed
Simplify and fix dependencies for headers
1 parent 43c5fb7 commit 927b897

File tree

1 file changed

+36
-35
lines changed

1 file changed

+36
-35
lines changed

libcxx/cmake/Modules/HandleLibCXXABI.cmake

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ include(GNUInstallDirs)
2020
# since we would end up also adding the system-provided C++ Standard Library to
2121
# the search path. Instead, what we do is copy just the ABI library headers to
2222
# a private directory and add just that path when we build libc++.
23-
function(import_private_headers target include_dirs headers)
23+
function(_import_private_headers target include_dirs headers)
2424
if (NOT ${include_dirs})
2525
message(FATAL_ERROR "Missing include paths for the selected ABI library!")
2626
endif()
@@ -58,15 +58,14 @@ function(import_private_headers target include_dirs headers)
5858
target_include_directories(${target} INTERFACE "${LIBCXX_BINARY_DIR}/private-abi-headers/${target}")
5959
endfunction()
6060

61-
# This function creates an imported static library named <target>.
62-
# It imports a library named <name> searched at the given <path>.
63-
function(import_static_library target path name)
64-
add_library(${target} STATIC IMPORTED GLOBAL)
65-
find_library(file
66-
NAMES "${CMAKE_STATIC_LIBRARY_PREFIX}${name}${CMAKE_STATIC_LIBRARY_SUFFIX}"
67-
PATHS "${path}"
68-
NO_CACHE)
69-
set_target_properties(${target} PROPERTIES IMPORTED_LOCATION "${file}")
61+
# This function adds linker flags to a <target> appropriate for merging the object
62+
# files of another static <library> into whoever links against <target>.
63+
function(_merge_static_library target library)
64+
if (APPLE)
65+
target_link_options(${target} INTERFACE "-Wl,-force_load" "${library}")
66+
else()
67+
target_link_options(${target} INTERFACE "-Wl,--whole-archive" "-Wl,-Bstatic" "${library}" "-Wl,-Bdynamic" "-Wl,--no-whole-archive")
68+
endif()
7069
endfunction()
7170

7271
# This function creates a library target for linking against an external ABI library.
@@ -75,29 +74,31 @@ endfunction()
7574
# <name>: The name of the library file to search for (e.g. c++abi, stdc++, cxxrt, supc++)
7675
# <type>: Whether to set up a static or a shared library (e.g. SHARED or STATIC)
7776
# <merged>: Whether to include the ABI library's object files directly into libc++. Only makes sense for a static ABI library.
78-
function(import_external_abi_library target name type merged)
77+
function(_import_external_abi_library target name type merged)
7978
if (${merged} AND "${type}" STREQUAL "SHARED")
8079
message(FATAL_ERROR "Can't import an external ABI library for merging when requesting a shared ABI library.")
8180
endif()
8281

8382
if ("${type}" STREQUAL "SHARED")
8483
add_library(${target} INTERFACE IMPORTED GLOBAL)
8584
set_target_properties(${target} PROPERTIES IMPORTED_LIBNAME "${name}")
85+
8686
elseif ("${type}" STREQUAL "STATIC")
87+
add_library(${target}-imported STATIC IMPORTED GLOBAL)
88+
find_library(file
89+
NAMES "${CMAKE_STATIC_LIBRARY_PREFIX}${name}${CMAKE_STATIC_LIBRARY_SUFFIX}"
90+
PATHS "${LIBCXX_CXX_ABI_LIBRARY_PATH}"
91+
NO_CACHE)
92+
if (NOT "${file}")
93+
message(FATAL_ERROR "Failed to find static library ${name} at path ${LIBCXX_CXX_ABI_LIBRARY_PATH}")
94+
endif()
95+
set_target_properties(${target}-imported PROPERTIES IMPORTED_LOCATION "${file}")
96+
97+
add_library(${target} INTERFACE)
8798
if (${merged})
88-
import_static_library(${target}-impl "${LIBCXX_CXX_ABI_LIBRARY_PATH}" ${name})
89-
add_library(${target} INTERFACE)
90-
if (APPLE)
91-
target_link_options(${target} INTERFACE
92-
"-Wl,-force_load" "$<TARGET_PROPERTY:${target}-impl,IMPORTED_LOCATION>")
93-
else()
94-
target_link_options(${target} INTERFACE
95-
"-Wl,--whole-archive" "-Wl,-Bstatic"
96-
"$<TARGET_PROPERTY:${target}-impl,IMPORTED_LOCATION>"
97-
"-Wl,-Bdynamic" "-Wl,--no-whole-archive")
98-
endif()
99+
_merge_static_library(${target} "$<TARGET_PROPERTY:${target}-imported,IMPORTED_LOCATION>")
99100
else()
100-
import_static_library(${target} "${LIBCXX_CXX_ABI_LIBRARY_PATH}" ${name})
101+
target_link_libraries(${target} INTERFACE ${target}-imported)
101102
endif()
102103
endif()
103104
endfunction()
@@ -133,22 +134,22 @@ function(setup_abi_library abi_target linked_into input)
133134

134135
# Link against a system-provided libstdc++
135136
if ("${stdlib}" STREQUAL "libstdc++")
136-
import_external_abi_library(${abi_target} stdc++ ${search_type} ${merged})
137-
import_private_headers(${abi_target} "${LIBCXX_CXX_ABI_INCLUDE_PATHS}"
137+
_import_external_abi_library(${abi_target} stdc++ ${search_type} ${merged})
138+
_import_private_headers(${abi_target} "${LIBCXX_CXX_ABI_INCLUDE_PATHS}"
138139
"cxxabi.h;bits/c++config.h;bits/os_defines.h;bits/cpu_defines.h;bits/cxxabi_tweaks.h;bits/cxxabi_forced.h")
139140
target_compile_definitions(${abi_target} INTERFACE "-DLIBSTDCXX" "-D__GLIBCXX__")
140141

141142
# Link against a system-provided libsupc++
142143
elseif ("${stdlib}" STREQUAL "libsupc++")
143-
import_external_abi_library(${abi_target} supc++ ${search_type} ${merged})
144-
import_private_headers(${abi_target} "${LIBCXX_CXX_ABI_INCLUDE_PATHS}"
144+
_import_external_abi_library(${abi_target} supc++ ${search_type} ${merged})
145+
_import_private_headers(${abi_target} "${LIBCXX_CXX_ABI_INCLUDE_PATHS}"
145146
"cxxabi.h;bits/c++config.h;bits/os_defines.h;bits/cpu_defines.h;bits/cxxabi_tweaks.h;bits/cxxabi_forced.h")
146147
target_compile_definitions(${abi_target} INTERFACE "-D__GLIBCXX__")
147148

148149
# Link against a system-provided libcxxrt
149150
elseif ("${stdlib}" STREQUAL "libcxxrt")
150-
import_external_abi_library(${abi_target} cxxrt ${search_type} ${merged})
151-
import_private_headers(${abi_target} "${LIBCXX_CXX_ABI_INCLUDE_PATHS}"
151+
_import_external_abi_library(${abi_target} cxxrt ${search_type} ${merged})
152+
_import_private_headers(${abi_target} "${LIBCXX_CXX_ABI_INCLUDE_PATHS}"
152153
"cxxabi.h;unwind.h;unwind-arm.h;unwind-itanium.h")
153154
target_compile_definitions(${abi_target} INTERFACE "-DLIBCXXRT")
154155

@@ -159,18 +160,18 @@ function(setup_abi_library abi_target linked_into input)
159160

160161
# Link against a system-provided libc++abi
161162
elseif ("${stdlib}" STREQUAL "system-libcxxabi")
162-
import_external_abi_library(${abi_target} c++abi ${search_type} ${merged})
163-
import_private_headers(${abi_target} "${LIBCXX_CXX_ABI_INCLUDE_PATHS}"
163+
_import_external_abi_library(${abi_target} c++abi ${search_type} ${merged})
164+
_import_private_headers(${abi_target} "${LIBCXX_CXX_ABI_INCLUDE_PATHS}"
164165
"cxxabi.h;__cxxabi_config.h")
165166
target_compile_definitions(${abi_target} INTERFACE "-DLIBCXX_BUILDING_LIBCXXABI")
166167

167168
# Link against the in-tree libc++abi.
168169
elseif ("${stdlib}" STREQUAL "libcxxabi")
169170
if (${merged})
170-
get_target_property(_outdir cxxabi_static ARCHIVE_OUTPUT_DIRECTORY)
171-
get_target_property(_outname cxxabi_static OUTPUT_NAME)
172-
set(LIBCXX_CXX_ABI_LIBRARY_PATH "${_outdir}")
173-
import_external_abi_library(${abi_target} "${_outname}" STATIC TRUE)
171+
add_library(${abi_target} INTERFACE)
172+
_merge_static_library(${abi_target}
173+
"$<TARGET_PROPERTY:cxxabi_static,LIBRARY_OUTPUT_DIRECTORY>/${CMAKE_STATIC_LIBRARY_PREFIX}$<TARGET_PROPERTY:cxxabi_static,OUTPUT_NAME>${CMAKE_STATIC_LIBRARY_SUFFIX}")
174+
target_link_libraries(${abi_target} INTERFACE cxxabi-headers)
174175
else()
175176
string(TOLOWER "${search_type}" type)
176177
add_library(${abi_target} INTERFACE)

0 commit comments

Comments
 (0)