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