[libcxx-commits] [PATCH] D90021: [libcxx] [libcxxabi] Set flags for visibility when statically linking libcxxabi into libcxx for windows

Martin Storsjö via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Fri Oct 23 03:27:48 PDT 2020


mstorsjo created this revision.
mstorsjo added reviewers: libc++, bd1976llvm, smeenai, compnerd, rnk.
Herald added a subscriber: mgorny.
Herald added projects: libc++, libc++abi.
Herald added 1 blocking reviewer(s): libc++.
Herald added a reviewer: libc++abi.
mstorsjo requested review of this revision.

Visibility attributes on windows (dllexport, dllimport) don't only have an effect when compiling the definitions of symbols, but also affect how undefined references to symbols behave (expecting them to live in the same DLL or not).

If we are building libcxxabi statically, with the intent to bundle it into the built libcxx (which is the only form that really works on windows, due to their interdependencies), we should have it flag its symbols as if it was being built in the same way as libcxx.

I.e., if libcxx is being built shared and libcxxabi is built static (to be linked into libcxx), libcxxabi should include dllexport on its symbols and don't import symbols from libcxx with dllimport as they are going to reside in the same DLL. And if building a static libcxx and going to bundle libcxxabi into it, it needs to access cxxabi symbols without dllimport attributes. (libcxxabi doesn't add `#define _LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS` to any header like libcxx does, if configured for a static-only build.)


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D90021

Files:
  libcxx/CMakeLists.txt
  libcxxabi/CMakeLists.txt


Index: libcxxabi/CMakeLists.txt
===================================================================
--- libcxxabi/CMakeLists.txt
+++ libcxxabi/CMakeLists.txt
@@ -281,7 +281,18 @@
 
 # Disable DLL annotations on Windows for static builds.
 if (WIN32 AND LIBCXXABI_ENABLE_STATIC AND NOT LIBCXXABI_ENABLE_SHARED)
-  add_definitions(-D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS)
+  if (LIBCXX_ENABLE_SHARED AND LIBCXX_ENABLE_STATIC_ABI_LIBRARY)
+    # Building libcxxabi statically, but intending for it to be statically
+    # linked into a shared libcxx; keep dllexport enabled within libcxxabi,
+    # and treat libcxx imports in the same way as if we were building libcxx
+    # itself (make them dllexport instead of dllimport).
+    # Define to the empty string (if just defined as -DFOO, it defines it to
+    # the value 1) as we have a number of "#define _LIBCPP_BUILDING_LIBRARY"
+    # in certain source files.
+    add_definitions(-D_LIBCPP_BUILDING_LIBRARY=)
+  else()
+    add_definitions(-D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS)
+  endif()
 endif()
 
 add_compile_flags_if_supported(-Werror=return-type)
Index: libcxx/CMakeLists.txt
===================================================================
--- libcxx/CMakeLists.txt
+++ libcxx/CMakeLists.txt
@@ -866,6 +866,19 @@
   config_define(ON _LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS)
 endif()
 
+if (WIN32 AND LIBCXX_ENABLE_STATIC_ABI_LIBRARY)
+  if (LIBCXX_ENABLE_SHARED AND LIBCXX_STATICALLY_LINK_ABI_IN_SHARED_LIBRARY)
+    # If linking libcxxabi statically into a shared libcxx, change declarations
+    # as if we are part of the libcxxabi, to make declarations dllexport
+    # instead of dllimport.
+    add_definitions(-D_LIBCXXABI_BUILDING_LIBRARY)
+  elseif(LIBCXX_ENABLE_STATIC AND LIBCXX_STATICALLY_LINK_ABI_IN_STATIC_LIBRARY)
+    # If linking libcxxabi statically into a static libcxx, skip the dllimport
+    # attributes on symbols we refer to from libcxxabi.
+    add_definitions(-D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS)
+  endif()
+endif()
+
 # Setup all common build flags =================================================
 function(cxx_add_common_build_flags target)
   cxx_add_basic_build_flags(${target})


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D90021.300211.patch
Type: text/x-patch
Size: 2195 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20201023/2a5407fd/attachment.bin>


More information about the libcxx-commits mailing list