1set(LIBCXX_LIB_CMAKEFILES_DIR "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}"  PARENT_SCOPE)
2
3# Get sources
4file(GLOB LIBCXX_SOURCES ../src/*.cpp)
5if(WIN32)
6  file(GLOB LIBCXX_WIN32_SOURCES ../src/support/win32/*.cpp)
7  list(APPEND LIBCXX_SOURCES ${LIBCXX_WIN32_SOURCES})
8elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "SunOS")
9  file(GLOB LIBCXX_SOLARIS_SOURCES ../src/support/solaris/*.cpp)
10  list(APPEND LIBCXX_SOURCES ${LIBCXX_SOLARIS_SOURCES})
11endif()
12
13# Add all the headers to the project for IDEs.
14if (LIBCXX_CONFIGURE_IDE)
15  file(GLOB_RECURSE LIBCXX_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/../include/*)
16  if(WIN32)
17    file( GLOB LIBCXX_WIN32_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/../include/support/win32/*.h)
18    list(APPEND LIBCXX_HEADERS ${LIBCXX_WIN32_HEADERS})
19  endif()
20  # Force them all into the headers dir on MSVC, otherwise they end up at
21  # project scope because they don't have extensions.
22  if (MSVC_IDE)
23    source_group("Header Files" FILES ${LIBCXX_HEADERS})
24  endif()
25endif()
26
27if(NOT LIBCXX_INSTALL_LIBRARY)
28  set(exclude_from_all EXCLUDE_FROM_ALL)
29endif()
30
31# If LIBCXX_CXX_ABI_LIBRARY_PATH is defined we want to add it to the search path.
32add_link_flags_if(LIBCXX_CXX_ABI_LIBRARY_PATH
33                  "${CMAKE_LIBRARY_PATH_FLAG}${LIBCXX_CXX_ABI_LIBRARY_PATH}")
34
35
36if (LIBCXX_GENERATE_COVERAGE AND NOT LIBCXX_COVERAGE_LIBRARY)
37  find_compiler_rt_library(profile LIBCXX_COVERAGE_LIBRARY)
38endif()
39add_library_flags_if(LIBCXX_COVERAGE_LIBRARY "${LIBCXX_COVERAGE_LIBRARY}")
40
41if (APPLE AND (LIBCXX_CXX_ABI_LIBNAME STREQUAL "libcxxabi" OR
42               LIBCXX_CXX_ABI_LIBNAME STREQUAL "default"))
43  set(LIBCXX_OSX_REEXPORT_SYSTEM_ABI_LIBRARY ON)
44endif()
45
46if (LIBCXX_ENABLE_STATIC_ABI_LIBRARY)
47  add_library_flags("-Wl,--whole-archive" "-Wl,-Bstatic")
48  add_library_flags("${LIBCXX_CXX_ABI_LIBRARY}")
49  add_library_flags("-Wl,-Bdynamic" "-Wl,--no-whole-archive")
50elseif (LIBCXX_OSX_REEXPORT_SYSTEM_ABI_LIBRARY)
51  add_library_flags("${LIBCXX_CXX_ABI_LIBRARY}")
52else ()
53 add_interface_library("${LIBCXX_CXX_ABI_LIBRARY}")
54endif()
55
56if (APPLE AND LLVM_USE_SANITIZER)
57  if (("${LLVM_USE_SANITIZER}" STREQUAL "Address") OR
58      ("${LLVM_USE_SANITIZER}" STREQUAL "Address;Undefined") OR
59      ("${LLVM_USE_SANITIZER}" STREQUAL "Undefined;Address"))
60    set(LIBFILE "libclang_rt.asan_osx_dynamic.dylib")
61  elseif("${LLVM_USE_SANITIZER}" STREQUAL "Undefined")
62    set(LIBFILE "libclang_rt.ubsan_osx_dynamic.dylib")
63  elseif("${LLVM_USE_SANITIZER}" STREQUAL "Thread")
64    set(LIBFILE "libclang_rt.tsan_osx_dynamic.dylib")
65  else()
66    message(WARNING "LLVM_USE_SANITIZER=${LLVM_USE_SANITIZER} is not supported on OS X")
67  endif()
68  if (LIBFILE)
69    find_compiler_rt_dir(LIBDIR)
70    if (NOT IS_DIRECTORY "${LIBDIR}")
71      message(FATAL_ERROR "Cannot find compiler-rt directory on OS X required for LLVM_USE_SANITIZER")
72    endif()
73    set(LIBCXX_SANITIZER_LIBRARY "${LIBDIR}/${LIBFILE}")
74    set(LIBCXX_SANITIZER_LIBRARY "${LIBCXX_SANITIZER_LIBRARY}" PARENT_SCOPE)
75    message(STATUS "Manually linking compiler-rt library: ${LIBCXX_SANITIZER_LIBRARY}")
76    add_library_flags("${LIBCXX_SANITIZER_LIBRARY}")
77    add_link_flags("-Wl,-rpath,${LIBDIR}")
78  endif()
79endif()
80
81# Generate private library list.
82add_library_flags_if(LIBCXX_HAS_PTHREAD_LIB pthread)
83add_library_flags_if(LIBCXX_HAS_C_LIB c)
84add_library_flags_if(LIBCXX_HAS_M_LIB m)
85add_library_flags_if(LIBCXX_HAS_RT_LIB rt)
86add_library_flags_if(LIBCXX_HAS_GCC_S_LIB gcc_s)
87add_library_flags_if(LIBCXX_HAVE_CXX_ATOMICS_WITH_LIB atomic)
88
89# Add the unwinder library.
90if (LIBCXXABI_USE_LLVM_UNWINDER)
91  if (NOT LIBCXXABI_ENABLE_STATIC_UNWINDER AND (TARGET unwind_shared OR HAVE_LIBUNWIND))
92    add_interface_library(unwind_shared)
93  elseif (LIBCXXABI_ENABLE_STATIC_UNWINDER AND (TARGET unwind_static OR HAVE_LIBUNWIND))
94    add_interface_library(unwind_static)
95  else()
96    add_interface_library(unwind)
97  endif()
98endif()
99
100# Setup flags.
101if (NOT WIN32)
102  add_flags_if_supported(-fPIC)
103endif()
104
105add_link_flags_if_supported(-nodefaultlibs)
106
107if (LIBCXX_TARGETING_MSVC)
108  if (LIBCXX_DEBUG_BUILD)
109    set(LIB_SUFFIX "d")
110  else()
111    set(LIB_SUFFIX "")
112  endif()
113  add_compile_flags(/Zl)
114  add_link_flags(/nodefaultlib)
115
116  add_library_flags(ucrt${LIB_SUFFIX}) # Universal C runtime
117  add_library_flags(vcruntime${LIB_SUFFIX}) # C++ runtime
118  add_library_flags(msvcrt${LIB_SUFFIX}) # C runtime startup files
119  # Required for standards-complaint wide character formatting functions
120  # (e.g. `printfw`/`scanfw`)
121  add_library_flags(iso_stdio_wide_specifiers)
122endif()
123
124if (LIBCXX_OSX_REEXPORT_SYSTEM_ABI_LIBRARY)
125  if (NOT DEFINED LIBCXX_LIBCPPABI_VERSION)
126    set(LIBCXX_LIBCPPABI_VERSION "2") # Default value
127    execute_process(
128      COMMAND xcrun --show-sdk-version
129      OUTPUT_VARIABLE sdk_ver
130      RESULT_VARIABLE res
131      OUTPUT_STRIP_TRAILING_WHITESPACE)
132    if (res EQUAL 0)
133      message(STATUS "Found SDK version ${sdk_ver}")
134      string(REPLACE "10." "" sdk_ver "${sdk_ver}")
135      if (sdk_ver LESS 9)
136        set(LIBCXX_LIBCPPABI_VERSION "")
137      else()
138        set(LIBCXX_LIBCPPABI_VERSION "2")
139      endif()
140    endif()
141  endif()
142
143  if ( CMAKE_OSX_DEPLOYMENT_TARGET STREQUAL "10.6" )
144    add_definitions(-D__STRICT_ANSI__)
145    add_link_flags(
146      "-compatibility_version 1"
147      "-current_version 1"
148      "-install_name /usr/lib/libc++.1.dylib"
149      "-Wl,-reexport_library,/usr/lib/libc++abi.dylib"
150      "-Wl,-unexported_symbols_list,${CMAKE_CURRENT_SOURCE_DIR}/libc++unexp.exp"
151      "/usr/lib/libSystem.B.dylib")
152  else()
153    if (DEFINED CMAKE_OSX_SYSROOT AND NOT CMAKE_OSX_SYSROOT STREQUAL "")
154      list(FIND CMAKE_OSX_ARCHITECTURES "armv7" OSX_HAS_ARMV7)
155      if (NOT OSX_HAS_ARMV7 EQUAL -1)
156        set(OSX_RE_EXPORT_LINE
157          "${CMAKE_OSX_SYSROOT}/usr/lib/libc++abi.dylib"
158          "-Wl,-reexported_symbols_list,${CMAKE_CURRENT_SOURCE_DIR}/libc++sjlj-abi.exp")
159      else()
160        set(OSX_RE_EXPORT_LINE
161          "-Wl,-reexport_library,${CMAKE_OSX_SYSROOT}/usr/lib/libc++abi.dylib")
162      endif()
163    else()
164      set(OSX_RE_EXPORT_LINE "/usr/lib/libc++abi.dylib -Wl,-reexported_symbols_list,${CMAKE_CURRENT_SOURCE_DIR}/libc++abi${LIBCXX_LIBCPPABI_VERSION}.exp")
165    endif()
166
167    add_link_flags(
168      "-compatibility_version 1"
169      "-install_name /usr/lib/libc++.1.dylib"
170      "-Wl,-unexported_symbols_list,${CMAKE_CURRENT_SOURCE_DIR}/libc++unexp.exp"
171      "${OSX_RE_EXPORT_LINE}"
172      "-Wl,-force_symbols_not_weak_list,${CMAKE_CURRENT_SOURCE_DIR}/notweak.exp"
173      "-Wl,-force_symbols_weak_list,${CMAKE_CURRENT_SOURCE_DIR}/weak.exp")
174  endif()
175endif()
176
177split_list(LIBCXX_COMPILE_FLAGS)
178split_list(LIBCXX_LINK_FLAGS)
179
180# Add a object library that contains the compiled source files.
181add_library(cxx_objects OBJECT ${exclude_from_all} ${LIBCXX_SOURCES} ${LIBCXX_HEADERS})
182if(WIN32 AND NOT MINGW)
183  target_compile_definitions(cxx_objects
184                             PRIVATE
185                               # Ignore the -MSC_VER mismatch, as we may build
186                               # with a different compatibility version.
187                               _ALLOW_MSC_VER_MISMATCH
188                               # Don't check the msvcprt iterator debug levels
189                               # as we will define the iterator types; libc++
190                               # uses a different macro to identify the debug
191                               # level.
192                               _ALLOW_ITERATOR_DEBUG_LEVEL_MISMATCH
193                               # We are building the c++ runtime, don't pull in
194                               # msvcprt.
195                               _CRTBLD
196                               # Don't warn on the use of "deprecated"
197                               # "insecure" functions which are standards
198                               # specified.
199                               _CRT_SECURE_NO_WARNINGS
200                               # Use the ISO conforming behaviour for conversion
201                               # in printf, scanf.
202                               _CRT_STDIO_ISO_WIDE_SPECIFIERS)
203endif()
204
205set_target_properties(cxx_objects
206  PROPERTIES
207    COMPILE_FLAGS "${LIBCXX_COMPILE_FLAGS}"
208)
209
210set(LIBCXX_TARGETS)
211
212# Build the shared library.
213if (LIBCXX_ENABLE_SHARED)
214  add_library(cxx_shared SHARED $<TARGET_OBJECTS:cxx_objects>)
215  target_link_libraries(cxx_shared ${LIBCXX_LIBRARIES})
216  set_target_properties(cxx_shared
217    PROPERTIES
218      LINK_FLAGS    "${LIBCXX_LINK_FLAGS}"
219      OUTPUT_NAME   "c++"
220      VERSION       "${LIBCXX_ABI_VERSION}.0"
221      SOVERSION     "${LIBCXX_ABI_VERSION}"
222  )
223  list(APPEND LIBCXX_TARGETS "cxx_shared")
224  if(WIN32 AND NOT MINGW AND NOT "${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Windows")
225    # Since we most likely do not have a mt.exe replacement, disable the
226    # manifest bundling.  This allows a normal cmake invocation to pass which
227    # will attempt to use the manifest tool to generate the bundled manifest
228    set_target_properties(cxx_shared PROPERTIES
229                          APPEND_STRING PROPERTY LINK_FLAGS " /MANIFEST:NO")
230  endif()
231endif()
232
233# Build the static library.
234if (LIBCXX_ENABLE_STATIC)
235  add_library(cxx_static STATIC $<TARGET_OBJECTS:cxx_objects>)
236  target_link_libraries(cxx_static ${LIBCXX_LIBRARIES})
237  set_target_properties(cxx_static
238    PROPERTIES
239      LINK_FLAGS    "${LIBCXX_LINK_FLAGS}"
240      OUTPUT_NAME   "c++"
241  )
242  list(APPEND LIBCXX_TARGETS "cxx_static")
243  # Attempt to merge the libc++.a archive and the ABI library archive into one.
244  if (LIBCXX_ENABLE_STATIC_ABI_LIBRARY)
245    set(MERGE_ARCHIVES_SEARCH_PATHS "")
246    if (LIBCXX_CXX_ABI_LIBRARY_PATH)
247      set(MERGE_ARCHIVES_SEARCH_PATHS "-L${LIBCXX_CXX_ABI_LIBRARY_PATH}")
248    endif()
249    if ((TARGET ${LIBCXX_CXX_ABI_LIBRARY}) OR
250        (${LIBCXX_CXX_ABI_LIBRARY} STREQUAL "cxxabi(_static|_shared)?" AND HAVE_LIBCXXABI))
251      set(MERGE_ARCHIVES_ABI_TARGET "$<TARGET_LINKER_FILE:${LIBCXX_CXX_ABI_LIBRARY}>")
252    else()
253      set(MERGE_ARCHIVES_ABI_TARGET
254          "${CMAKE_STATIC_LIBRARY_PREFIX}${LIBCXX_CXX_ABI_LIBRARY}${CMAKE_STATIC_LIBRARY_SUFFIX}")
255    endif()
256    add_custom_command(TARGET cxx_static POST_BUILD
257    COMMAND
258      ${PYTHON_EXECUTABLE} ${LIBCXX_SOURCE_DIR}/utils/merge_archives.py
259    ARGS
260      -o $<TARGET_LINKER_FILE:cxx_static>
261      "$<TARGET_LINKER_FILE:cxx_static>"
262      "${MERGE_ARCHIVES_ABI_TARGET}"
263      "${MERGE_ARCHIVES_SEARCH_PATHS}"
264    WORKING_DIRECTORY ${LIBCXX_BUILD_DIR}
265    )
266  endif()
267endif()
268
269# Add a meta-target for both libraries.
270add_custom_target(cxx DEPENDS ${LIBCXX_TARGETS})
271
272if (LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY)
273  file(GLOB LIBCXX_EXPERIMENTAL_SOURCES ../src/experimental/*.cpp)
274  if (LIBCXX_ENABLE_FILESYSTEM)
275    file(GLOB LIBCXX_FILESYSTEM_SOURCES ../src/experimental/filesystem/*.cpp)
276  endif()
277  add_library(cxx_experimental STATIC ${LIBCXX_EXPERIMENTAL_SOURCES} ${LIBCXX_FILESYSTEM_SOURCES})
278  if (LIBCXX_ENABLE_SHARED)
279    target_link_libraries(cxx_experimental cxx_shared)
280  else()
281    target_link_libraries(cxx_experimental cxx_static)
282  endif()
283
284  set(experimental_flags "${LIBCXX_COMPILE_FLAGS}")
285  check_flag_supported(-std=c++14)
286  if (NOT MSVC AND LIBCXX_SUPPORTS_STD_EQ_CXX14_FLAG)
287    string(REPLACE "-std=c++11" "-std=c++14" experimental_flags "${LIBCXX_COMPILE_FLAGS}")
288  endif()
289  set_target_properties(cxx_experimental
290    PROPERTIES
291      COMPILE_FLAGS "${experimental_flags}"
292      OUTPUT_NAME   "c++experimental"
293  )
294endif()
295
296if (LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY)
297  file(GLOB LIBCXX_EXTERNAL_THREADING_SUPPORT_SOURCES ../test/support/external_threads.cpp)
298
299  if (LIBCXX_ENABLE_SHARED)
300    add_library(cxx_external_threads SHARED ${LIBCXX_EXTERNAL_THREADING_SUPPORT_SOURCES})
301  else()
302    add_library(cxx_external_threads STATIC ${LIBCXX_EXTERNAL_THREADING_SUPPORT_SOURCES})
303  endif()
304
305  set_target_properties(cxx_external_threads
306    PROPERTIES
307      LINK_FLAGS    "${LIBCXX_LINK_FLAGS}"
308      COMPILE_FLAGS "${LIBCXX_COMPILE_FLAGS}"
309      OUTPUT_NAME   "c++external_threads"
310  )
311endif()
312
313# Generate a linker script inplace of a libc++.so symlink. Rerun this command
314# after cxx builds.
315if (LIBCXX_ENABLE_SHARED AND LIBCXX_ENABLE_ABI_LINKER_SCRIPT)
316  # Get the name of the ABI library and handle the case where CXXABI_LIBNAME
317  # is a target name and not a library. Ex cxxabi_shared.
318  set(LIBCXX_INTERFACE_LIBRARY_NAMES)
319  foreach(lib ${LIBCXX_INTERFACE_LIBRARIES})
320    # FIXME: Handle cxxabi_static and unwind_static.
321    if (TARGET ${lib} OR
322        (${lib} MATCHES "cxxabi(_static|_shared)?" AND HAVE_LIBCXXABI) OR
323        (${lib} MATCHES "unwind(_static|_shared)?" AND HAVE_LIBUNWIND))
324      list(APPEND LIBCXX_INTERFACE_LIBRARY_NAMES "$<TARGET_PROPERTY:${lib},OUTPUT_NAME>")
325    else()
326      list(APPEND LIBCXX_INTERFACE_LIBRARY_NAMES "${lib}")
327    endif()
328  endforeach()
329  #split_list(LIBCXX_INTERFACE_LIBRARY_NAMES)
330  # Generate a linker script inplace of a libc++.so symlink. Rerun this command
331  # after cxx builds.
332  add_custom_command(TARGET cxx_shared POST_BUILD
333    COMMAND
334      ${PYTHON_EXECUTABLE} ${LIBCXX_SOURCE_DIR}/utils/gen_link_script.py
335    ARGS
336      "$<TARGET_LINKER_FILE:cxx_shared>"
337      ${LIBCXX_INTERFACE_LIBRARY_NAMES}
338    WORKING_DIRECTORY ${LIBCXX_BUILD_DIR}
339  )
340endif()
341
342if (LIBCXX_INSTALL_LIBRARY)
343  if (LIBCXX_INSTALL_EXPERIMENTAL_LIBRARY)
344    set(experimental_lib cxx_experimental)
345  endif()
346  install(TARGETS ${LIBCXX_TARGETS} ${experimental_lib}
347    LIBRARY DESTINATION lib${LIBCXX_LIBDIR_SUFFIX} COMPONENT cxx
348    ARCHIVE DESTINATION lib${LIBCXX_LIBDIR_SUFFIX} COMPONENT cxx
349    )
350  # NOTE: This install command must go after the cxx install command otherwise
351  # it will not be executed after the library symlinks are installed.
352  if (LIBCXX_ENABLE_SHARED AND LIBCXX_ENABLE_ABI_LINKER_SCRIPT)
353    # Replace the libc++ filename with $<TARGET_LINKER_FILE:cxx>
354    # after we required CMake 3.0.
355    install(FILES "${LIBCXX_LIBRARY_DIR}/libc++${CMAKE_SHARED_LIBRARY_SUFFIX}"
356      DESTINATION lib${LIBCXX_LIBDIR_SUFFIX}
357      COMPONENT libcxx)
358  endif()
359endif()
360
361if (NOT CMAKE_CONFIGURATION_TYPES AND (LIBCXX_INSTALL_LIBRARY OR
362                                       LIBCXX_INSTALL_HEADERS))
363    if(LIBCXX_INSTALL_LIBRARY)
364      set(lib_install_target cxx)
365    endif()
366    if (LIBCXX_INSTALL_EXPERIMENTAL_LIBRARY)
367      set(experimental_lib_install_target cxx_experimental)
368    endif()
369    if(LIBCXX_INSTALL_HEADERS)
370      set(header_install_target install-cxx-headers)
371    endif()
372    add_custom_target(install-cxx
373                      DEPENDS ${lib_install_target}
374                              ${experimental_lib_install_target}
375                              ${header_install_target}
376                      COMMAND "${CMAKE_COMMAND}"
377                      -DCMAKE_INSTALL_COMPONENT=cxx
378                      -P "${LIBCXX_BINARY_DIR}/cmake_install.cmake")
379    add_custom_target(install-libcxx DEPENDS install-cxx)
380endif()
381