Skip to content

Commit

Permalink
[libcxxabi] Support building hermetic static library
Browse files Browse the repository at this point in the history
This is useful when the static libc++abi library is being linked into
shared libraries that may be used in with other shared libraries that
use different C++ library. We want to avoid avoid exporting libc++abi
or libc++ symbols in those cases. This achieved by a new CMake option
which can be enabled by libc++abi vendors as needed.

Differential Revision: https://reviews.llvm.org/D56026

llvm-svn: 352017
  • Loading branch information
petrhosek committed Jan 24, 2019
1 parent 8c84e00 commit 8807db3
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 16 deletions.
3 changes: 3 additions & 0 deletions libcxxabi/CMakeLists.txt
Expand Up @@ -155,6 +155,9 @@ endif()
set(LIBCXXABI_LIBCXX_PATH "${LIBCXXABI_LIBCXX_PATH}" CACHE PATH
"Specify path to libc++ source." FORCE)

option(LIBCXXABI_HERMETIC_STATIC_LIBRARY
"Do not export any symbols from the static library." OFF)

#===============================================================================
# Configure System
#===============================================================================
Expand Down
23 changes: 23 additions & 0 deletions libcxxabi/cmake/Modules/HandleLibcxxabiFlags.cmake
Expand Up @@ -44,6 +44,29 @@ macro(check_flag_supported flag)
check_cxx_compiler_flag("${flag}" "LIBCXXABI_SUPPORTS_${flagname}_FLAG")
endmacro()

macro(append_flags DEST)
foreach(value ${ARGN})
list(APPEND ${DEST} ${value})
list(APPEND ${DEST} ${value})
endforeach()
endmacro()

# If the specified 'condition' is true then append the specified list of flags to DEST
macro(append_flags_if condition DEST)
if (${condition})
list(APPEND ${DEST} ${ARGN})
endif()
endmacro()

# Add each flag in the list specified by DEST if that flag is supported by the current compiler.
macro(append_flags_if_supported DEST)
foreach(flag ${ARGN})
mangle_name("${flag}" flagname)
check_cxx_compiler_flag("${flag}" "LIBCXXABI_SUPPORTS_${flagname}_FLAG")
append_flags_if(LIBCXXABI_SUPPORTS_${flagname}_FLAG ${DEST} ${flag})
endforeach()
endmacro()

# Add a macro definition if condition is true.
macro(define_if condition def)
if (${condition})
Expand Down
60 changes: 44 additions & 16 deletions libcxxabi/src/CMakeLists.txt
Expand Up @@ -139,24 +139,53 @@ if (LLVM_ENABLE_MODULES)
string(REPLACE "-Wl,-z,defs" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")
endif()

# Add a object library that contains the compiled source files.
add_library(cxxabi_objects OBJECT ${LIBCXXABI_SOURCES} ${LIBCXXABI_HEADERS})
set_target_properties(cxxabi_objects
PROPERTIES
CXX_EXTENSIONS
OFF
CXX_STANDARD
11
CXX_STANDARD_REQUIRED
ON
COMPILE_FLAGS
"${LIBCXXABI_COMPILE_FLAGS}"
POSITION_INDEPENDENT_CODE
ON)
macro(cxxabi_object_library name)
cmake_parse_arguments(ARGS "" "" "DEFINES;FLAGS" ${ARGN})

# Add a object library that contains the compiled source files.
add_library(${name} OBJECT ${LIBCXXABI_SOURCES} ${LIBCXXABI_HEADERS})
set_target_properties(${name}
PROPERTIES
CXX_EXTENSIONS
OFF
CXX_STANDARD
11
CXX_STANDARD_REQUIRED
ON
COMPILE_FLAGS
"${LIBCXXABI_COMPILE_FLAGS}"
POSITION_INDEPENDENT_CODE
ON)

if(ARGS_DEFINES)
target_compile_definitions(${name} PRIVATE ${ARGS_DEFINES})
endif()

if(ARGS_FLAGS)
target_compile_options(${name} PRIVATE ${ARGS_FLAGS})
endif()
endmacro()

if(LIBCXXABI_HERMETIC_STATIC_LIBRARY)
append_flags_if_supported(CXXABI_STATIC_OBJECTS_FLAGS -fvisibility=hidden)
append_flags_if_supported(CXXABI_STATIC_OBJECTS_FLAGS -fvisibility-global-new-delete-hidden)
cxxabi_object_library(cxxabi_static_objects
DEFINES
_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS
_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS
FLAGS ${CXXABI_STATIC_OBJECTS_FLAGS})
cxxabi_object_library(cxxabi_shared_objects)
set(cxxabi_static_sources $<TARGET_OBJECTS:cxxabi_static_objects>)
set(cxxabi_shared_sources $<TARGET_OBJECTS:cxxabi_shared_objects>)
else()
cxxabi_object_library(cxxabi_objects)
set(cxxabi_static_sources $<TARGET_OBJECTS:cxxabi_objects>)
set(cxxabi_shared_sources $<TARGET_OBJECTS:cxxabi_objects>)
endif()

# Build the shared library.
if (LIBCXXABI_ENABLE_SHARED)
add_library(cxxabi_shared SHARED $<TARGET_OBJECTS:cxxabi_objects>)
add_library(cxxabi_shared SHARED ${cxxabi_shared_sources})
if(COMMAND llvm_setup_rpath)
llvm_setup_rpath(cxxabi_shared)
endif()
Expand Down Expand Up @@ -187,7 +216,6 @@ endif()

# Build the static library.
if (LIBCXXABI_ENABLE_STATIC)
set(cxxabi_static_sources $<TARGET_OBJECTS:cxxabi_objects>)
if (LIBCXXABI_USE_LLVM_UNWINDER AND LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_STATIC_LIBRARY)
if (TARGET unwind_static OR HAVE_LIBUNWIND)
list(APPEND cxxabi_static_sources $<TARGET_OBJECTS:unwind_objects>)
Expand Down

0 comments on commit 8807db3

Please sign in to comment.