libcxx-3.9-cmake-static-lib.patch 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. From b640da0b315ead39690d4d65c76938ab8aeb5449 Mon Sep 17 00:00:00 2001
  2. From: Petr Hosek <phosek@chromium.org>
  3. Date: Mon, 8 Aug 2016 22:57:25 +0000
  4. Subject: [PATCH] Allow building both shared and static library
  5. This change allows building both shared and static version of libc++
  6. in a single build, sharing object files between both versions.
  7. Differential Revision: https://reviews.llvm.org/D23232
  8. git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@278068 91177308-0d34-0410-b5e6-96231b3b80d8
  9. ---
  10. CMakeLists.txt | 5 ++++
  11. docs/BuildingLibcxx.rst | 11 ++++++--
  12. lib/CMakeLists.txt | 71 ++++++++++++++++++++++++++++++++++---------------
  13. 3 files changed, 63 insertions(+), 24 deletions(-)
  14. diff --git a/CMakeLists.txt b/CMakeLists.txt
  15. index 950070d..98886b0 100644
  16. --- a/CMakeLists.txt
  17. +++ b/CMakeLists.txt
  18. @@ -52,6 +52,7 @@ MACRO_ENSURE_OUT_OF_SOURCE_BUILD(
  19. # Basic options ---------------------------------------------------------------
  20. option(LIBCXX_ENABLE_ASSERTIONS "Enable assertions independent of build mode." ON)
  21. option(LIBCXX_ENABLE_SHARED "Build libc++ as a shared library." ON)
  22. +option(LIBCXX_ENABLE_STATIC "Build libc++ as a static library." ON)
  23. option(LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY "Build libc++experimental.a" ON)
  24. option(LIBCXX_ENABLE_FILESYSTEM
  25. "Build filesystem as part of libc++experimental.a" ${LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY})
  26. @@ -68,6 +69,10 @@ option(LIBCXX_INSTALL_EXPERIMENTAL_LIBRARY "Install libc++experimental.a" OFF)
  27. set(LIBCXX_ABI_VERSION 1 CACHE STRING "ABI version of libc++.")
  28. option(LIBCXX_ABI_UNSTABLE "Unstable ABI of libc++." OFF)
  29. +if (NOT LIBCXX_ENABLE_SHARED AND NOT LIBCXX_ENABLE_STATIC)
  30. + message(FATAL_ERROR "libc++ must be built as either a shared or static library.")
  31. +endif()
  32. +
  33. # ABI Library options ---------------------------------------------------------
  34. set(LIBCXX_CXX_ABI "${LIBCXX_CXX_ABI}" CACHE STRING
  35. "Specify C++ ABI library to use." FORCE)
  36. diff --git a/docs/BuildingLibcxx.rst b/docs/BuildingLibcxx.rst
  37. index 5dd174a..6709352 100644
  38. --- a/docs/BuildingLibcxx.rst
  39. +++ b/docs/BuildingLibcxx.rst
  40. @@ -150,8 +150,15 @@ libc++ specific options
  41. **Default**: ``ON``
  42. - Build libc++ as a shared library. If ``OFF`` is specified then libc++ is
  43. - built as a static library.
  44. + Build libc++ as a shared library. Either :option:`LIBCXX_ENABLE_SHARED` or
  45. + :option:`LIBCXX_ENABLE_STATIC` has to be enabled.
  46. +
  47. +.. option:: LIBCXX_ENABLE_STATIC:BOOL
  48. +
  49. + **Default**: ``ON``
  50. +
  51. + Build libc++ as a static library. Either :option:`LIBCXX_ENABLE_SHARED` or
  52. + :option:`LIBCXX_ENABLE_STATIC` has to be enabled.
  53. .. option:: LIBCXX_LIBDIR_SUFFIX:STRING
  54. diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt
  55. index afc388e..cabf2e6 100644
  56. --- a/lib/CMakeLists.txt
  57. +++ b/lib/CMakeLists.txt
  58. @@ -28,16 +28,6 @@ if(NOT LIBCXX_INSTALL_LIBRARY)
  59. set(exclude_from_all EXCLUDE_FROM_ALL)
  60. endif()
  61. -if (LIBCXX_ENABLE_SHARED)
  62. - add_library(cxx SHARED ${exclude_from_all} ${LIBCXX_SOURCES} ${LIBCXX_HEADERS})
  63. -else()
  64. - add_library(cxx STATIC ${exclude_from_all} ${LIBCXX_SOURCES} ${LIBCXX_HEADERS})
  65. -endif()
  66. -
  67. -if (DEFINED LIBCXX_CXX_ABI_DEPS)
  68. - add_dependencies(cxx LIBCXX_CXX_ABI_DEPS)
  69. -endif()
  70. -
  71. #if LIBCXX_CXX_ABI_LIBRARY_PATH is defined we want to add it to the search path.
  72. add_link_flags_if(LIBCXX_CXX_ABI_LIBRARY_PATH "-L${LIBCXX_CXX_ABI_LIBRARY_PATH}")
  73. @@ -139,18 +129,51 @@ if ( APPLE AND (LIBCXX_CXX_ABI_LIBNAME STREQUAL "libcxxabi" OR
  74. endif()
  75. endif()
  76. -target_link_libraries(cxx ${LIBCXX_LIBRARIES})
  77. split_list(LIBCXX_COMPILE_FLAGS)
  78. split_list(LIBCXX_LINK_FLAGS)
  79. -set_target_properties(cxx
  80. +# Add a object library that contains the compiled source files.
  81. +add_library(cxx_objects OBJECT ${exclude_from_all} ${LIBCXX_SOURCES} ${LIBCXX_HEADERS})
  82. +
  83. +set_target_properties(cxx_objects
  84. PROPERTIES
  85. COMPILE_FLAGS "${LIBCXX_COMPILE_FLAGS}"
  86. - LINK_FLAGS "${LIBCXX_LINK_FLAGS}"
  87. - OUTPUT_NAME "c++"
  88. - VERSION "${LIBCXX_ABI_VERSION}.0"
  89. - SOVERSION "${LIBCXX_ABI_VERSION}"
  90. +)
  91. +
  92. +set(LIBCXX_TARGETS)
  93. +
  94. +# Build the shared library.
  95. +if (LIBCXX_ENABLE_SHARED)
  96. + add_library(cxx_shared SHARED $<TARGET_OBJECTS:cxx_objects>)
  97. + target_link_libraries(cxx_shared ${LIBCXX_LIBRARIES})
  98. + set_target_properties(cxx_shared
  99. + PROPERTIES
  100. + LINK_FLAGS "${LIBCXX_LINK_FLAGS}"
  101. + OUTPUT_NAME "c++"
  102. + VERSION "${LIBCXX_ABI_VERSION}.0"
  103. + SOVERSION "${LIBCXX_ABI_VERSION}"
  104. )
  105. + list(APPEND LIBCXX_TARGETS "cxx_shared")
  106. +endif()
  107. +
  108. +# Build the static library.
  109. +if (LIBCXX_ENABLE_STATIC)
  110. + add_library(cxx_static STATIC $<TARGET_OBJECTS:cxx_objects>)
  111. + target_link_libraries(cxx_static ${LIBCXX_LIBRARIES})
  112. + set_target_properties(cxx_static
  113. + PROPERTIES
  114. + LINK_FLAGS "${LIBCXX_LINK_FLAGS}"
  115. + OUTPUT_NAME "c++"
  116. + )
  117. + list(APPEND LIBCXX_TARGETS "cxx_static")
  118. +endif()
  119. +
  120. +# Add a meta-target for both libraries.
  121. +add_custom_target(cxx DEPENDS ${LIBCXX_TARGETS})
  122. +
  123. +if (DEFINED LIBCXX_CXX_ABI_DEPS)
  124. + add_dependencies(cxx LIBCXX_CXX_ABI_DEPS)
  125. +endif()
  126. if (LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY)
  127. file(GLOB LIBCXX_EXPERIMENTAL_SOURCES ../src/experimental/*.cpp)
  128. @@ -158,7 +181,11 @@ if (LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY)
  129. file(GLOB LIBCXX_FILESYSTEM_SOURCES ../src/experimental/filesystem/*.cpp)
  130. endif()
  131. add_library(cxx_experimental STATIC ${LIBCXX_EXPERIMENTAL_SOURCES} ${LIBCXX_FILESYSTEM_SOURCES})
  132. - target_link_libraries(cxx_experimental cxx)
  133. + if (LIBCXX_ENABLE_SHARED)
  134. + target_link_libraries(cxx_experimental cxx_shared)
  135. + else()
  136. + target_link_libraries(cxx_experimental cxx_static)
  137. + endif()
  138. set(experimental_flags "${LIBCXX_COMPILE_FLAGS}")
  139. check_flag_supported(-std=c++14)
  140. @@ -174,7 +201,7 @@ endif()
  141. # Generate a linker script inplace of a libc++.so symlink. Rerun this command
  142. # after cxx builds.
  143. -if (LIBCXX_ENABLE_ABI_LINKER_SCRIPT)
  144. +if (LIBCXX_ENABLE_SHARED AND LIBCXX_ENABLE_ABI_LINKER_SCRIPT)
  145. # Get the name of the ABI library and handle the case where CXXABI_LIBNAME
  146. # is a target name and not a library. Ex cxxabi_shared.
  147. set(SCRIPT_ABI_LIBNAME "${LIBCXX_CXX_ABI_LIBRARY}")
  148. @@ -183,11 +210,11 @@ if (LIBCXX_ENABLE_ABI_LINKER_SCRIPT)
  149. endif()
  150. # Generate a linker script inplace of a libc++.so symlink. Rerun this command
  151. # after cxx builds.
  152. - add_custom_command(TARGET cxx POST_BUILD
  153. + add_custom_command(TARGET cxx_shared POST_BUILD
  154. COMMAND
  155. ${PYTHON_EXECUTABLE} ${LIBCXX_SOURCE_DIR}/utils/gen_link_script/gen_link_script.py
  156. ARGS
  157. - "$<TARGET_LINKER_FILE:cxx>"
  158. + "$<TARGET_LINKER_FILE:cxx_shared>"
  159. "${SCRIPT_ABI_LIBNAME}"
  160. WORKING_DIRECTORY ${LIBCXX_BUILD_DIR}
  161. )
  162. @@ -197,13 +224,13 @@ if (LIBCXX_INSTALL_LIBRARY)
  163. if (LIBCXX_INSTALL_EXPERIMENTAL_LIBRARY)
  164. set(experimental_lib cxx_experimental)
  165. endif()
  166. - install(TARGETS cxx ${experimental_lib}
  167. + install(TARGETS ${LIBCXX_TARGETS} ${experimental_lib}
  168. LIBRARY DESTINATION lib${LIBCXX_LIBDIR_SUFFIX} COMPONENT libcxx
  169. ARCHIVE DESTINATION lib${LIBCXX_LIBDIR_SUFFIX} COMPONENT libcxx
  170. )
  171. # NOTE: This install command must go after the cxx install command otherwise
  172. # it will not be executed after the library symlinks are installed.
  173. - if (LIBCXX_ENABLE_ABI_LINKER_SCRIPT)
  174. + if (LIBCXX_ENABLE_SHARED AND LIBCXX_ENABLE_ABI_LINKER_SCRIPT)
  175. # Replace the libc++ filename with $<TARGET_LINKER_FILE:cxx>
  176. # after we required CMake 3.0.
  177. install(FILES "${LIBCXX_LIBRARY_DIR}/libc++${CMAKE_SHARED_LIBRARY_SUFFIX}"
  178. --
  179. 2.4.10