[libcxxabi] Support building hermetic static library
authorPetr Hosek <phosek@chromium.org>
Thu, 24 Jan 2019 03:18:29 +0000 (03:18 +0000)
committerPetr Hosek <phosek@chromium.org>
Thu, 24 Jan 2019 03:18:29 +0000 (03:18 +0000)
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

libcxxabi/CMakeLists.txt
libcxxabi/cmake/Modules/HandleLibcxxabiFlags.cmake
libcxxabi/src/CMakeLists.txt

index 1973a52..664e1fe 100644 (file)
@@ -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
 #===============================================================================
index 3eddd7b..19d6e93 100644 (file)
@@ -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})
index 7e21fb2..77722e6 100644 (file)
@@ -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()
@@ -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>)