hamzasood created this revision.
Herald added a subscriber: mgorny.

This patch fixes a few problems with building libc++experimental on Windows.

Previously _LIBCPP_BUILDING_LIBRARY was defined for every source file, 
including files destined for the experimental library. As a result of this, 
non-experimental APIs were marked as dllexport when compiling the experimental 
library (which led to link errors whenever experimental code uses symbols from 
the regular libc++ library).
I've changed it so that _LIBCPP_BUILDING_LIBRARY is only defined for 
non-experimental source files, and _LIBCPPX_BUILDING_LIBRARY is defined for 
experimental source files (that macro isn't currently used for anything, it's 
mainly there for consistency).

I've also added the needed MSVCRT defines to the experimental code too, so that 
there won't be a bunch of warnings about using "deprecated" functions etc.


https://reviews.llvm.org/D37376

Files:
  CMakeLists.txt
  lib/CMakeLists.txt

Index: lib/CMakeLists.txt
===================================================================
--- lib/CMakeLists.txt
+++ lib/CMakeLists.txt
@@ -186,31 +186,37 @@
 split_list(LIBCXX_COMPILE_FLAGS)
 split_list(LIBCXX_LINK_FLAGS)
 
+macro(add_msvcrt_defs_if_needed target)
+  if(WIN32 AND NOT MINGW)
+    target_compile_definitions(${target}
+                               PRIVATE
+                                 # Ignore the -MSC_VER mismatch, as we may build
+                                 # with a different compatibility version.
+                                 _ALLOW_MSC_VER_MISMATCH
+                                 # Don't check the msvcprt iterator debug levels
+                                 # as we will define the iterator types; libc++
+                                 # uses a different macro to identify the debug
+                                 # level.
+                                 _ALLOW_ITERATOR_DEBUG_LEVEL_MISMATCH
+                                 # We are building the c++ runtime, don't pull in
+                                 # msvcprt.
+                                 _CRTBLD
+                                 # Don't warn on the use of "deprecated"
+                                 # "insecure" functions which are standards
+                                 # specified.
+                                 _CRT_SECURE_NO_WARNINGS
+                                 # Use the ISO conforming behaviour for conversion
+                                 # in printf, scanf.
+                                 _CRT_STDIO_ISO_WIDE_SPECIFIERS)
+  endif()
+endmacro()
+
 # Add an object library that contains the compiled source files.
 add_library(cxx_objects OBJECT ${exclude_from_all} ${LIBCXX_SOURCES} ${LIBCXX_HEADERS})
-if(WIN32 AND NOT MINGW)
-  target_compile_definitions(cxx_objects
-                             PRIVATE
-                               # Ignore the -MSC_VER mismatch, as we may build
-                               # with a different compatibility version.
-                               _ALLOW_MSC_VER_MISMATCH
-                               # Don't check the msvcprt iterator debug levels
-                               # as we will define the iterator types; libc++
-                               # uses a different macro to identify the debug
-                               # level.
-                               _ALLOW_ITERATOR_DEBUG_LEVEL_MISMATCH
-                               # We are building the c++ runtime, don't pull in
-                               # msvcprt.
-                               _CRTBLD
-                               # Don't warn on the use of "deprecated"
-                               # "insecure" functions which are standards
-                               # specified.
-                               _CRT_SECURE_NO_WARNINGS
-                               # Use the ISO conforming behaviour for conversion
-                               # in printf, scanf.
-                               _CRT_STDIO_ISO_WIDE_SPECIFIERS)
-endif()
-
+target_compile_definitions(cxx_objects
+                           PRIVATE
+                             _LIBCPP_BUILDING_LIBRARY)
+add_msvcrt_defs_if_needed(cxx_objects)
 set_target_properties(cxx_objects
   PROPERTIES
     COMPILE_FLAGS "${LIBCXX_COMPILE_FLAGS}"
@@ -285,7 +291,13 @@
   if (LIBCXX_ENABLE_FILESYSTEM)
     file(GLOB LIBCXX_FILESYSTEM_SOURCES ../src/experimental/filesystem/*.cpp)
   endif()
+
   add_library(cxx_experimental STATIC ${LIBCXX_EXPERIMENTAL_SOURCES} ${LIBCXX_FILESYSTEM_SOURCES})
+  target_compile_definitions(cxx_experimental
+                             PRIVATE
+                               _LIBCPPX_BUILDING_LIBRARY)
+  add_msvcrt_defs_if_needed(cxx_experimental)
+
   if (LIBCXX_ENABLE_SHARED)
     target_link_libraries(cxx_experimental cxx_shared)
   else()
@@ -313,6 +325,9 @@
     add_library(cxx_external_threads STATIC ${LIBCXX_EXTERNAL_THREADING_SUPPORT_SOURCES})
   endif()
 
+  target_compile_definitions(cxx_external_threads
+                             PRIVATE
+                               _LIBCPP_BUILDING_LIBRARY)
   set_target_properties(cxx_external_threads
     PROPERTIES
       LINK_FLAGS    "${LIBCXX_LINK_FLAGS}"
Index: CMakeLists.txt
===================================================================
--- CMakeLists.txt
+++ CMakeLists.txt
@@ -465,10 +465,6 @@
   add_compile_flags_if_supported(-fcoroutines-ts)
 endif()
 
-# Let the library headers know they are currently being used to build the
-# library.
-add_definitions(-D_LIBCPP_BUILDING_LIBRARY)
-
 if (NOT LIBCXX_ENABLE_NEW_DELETE_DEFINITIONS)
   add_definitions(-D_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS)
 endif()
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to