• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# See www/CMake.html for instructions on how to build libcxx with CMake.
2
3#===============================================================================
4# Setup Project
5#===============================================================================
6cmake_minimum_required(VERSION 3.4.3)
7
8if(POLICY CMP0042)
9  cmake_policy(SET CMP0042 NEW) # Set MACOSX_RPATH=YES by default
10endif()
11if(POLICY CMP0022)
12  cmake_policy(SET CMP0022 NEW) # Required when interacting with LLVM and Clang
13endif()
14
15# Add path for custom modules
16set(CMAKE_MODULE_PATH
17  "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
18  "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
19  ${CMAKE_MODULE_PATH}
20  )
21
22if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
23  project(libcxx CXX C)
24
25  set(PACKAGE_NAME libcxx)
26  set(PACKAGE_VERSION 7.0.0svn)
27  set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}")
28  set(PACKAGE_BUGREPORT "llvm-bugs@lists.llvm.org")
29
30  # Find the LLVM sources and simulate LLVM CMake options.
31  include(HandleOutOfTreeLLVM)
32endif()
33
34if (LIBCXX_STANDALONE_BUILD)
35  include(FindPythonInterp)
36  if( NOT PYTHONINTERP_FOUND )
37    message(WARNING "Failed to find python interpreter. "
38                    "The libc++ test suite will be disabled.")
39    set(LLVM_INCLUDE_TESTS OFF)
40  endif()
41endif()
42
43# Require out of source build.
44include(MacroEnsureOutOfSourceBuild)
45MACRO_ENSURE_OUT_OF_SOURCE_BUILD(
46 "${PROJECT_NAME} requires an out of source build. Please create a separate
47 build directory and run 'cmake /path/to/${PROJECT_NAME} [options]' there."
48 )
49
50if (MSVC)
51  set(LIBCXX_TARGETING_MSVC ON)
52else()
53  set(LIBCXX_TARGETING_MSVC OFF)
54endif()
55
56#===============================================================================
57# Setup CMake Options
58#===============================================================================
59include(CMakeDependentOption)
60include(HandleCompilerRT)
61
62# Basic options ---------------------------------------------------------------
63option(LIBCXX_ENABLE_ASSERTIONS "Enable assertions independent of build mode." OFF)
64option(LIBCXX_ENABLE_SHARED "Build libc++ as a shared library." ON)
65option(LIBCXX_ENABLE_STATIC "Build libc++ as a static library." ON)
66option(LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY "Build libc++experimental.a" ON)
67set(ENABLE_FILESYSTEM_DEFAULT ${LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY})
68if (WIN32)
69  set(ENABLE_FILESYSTEM_DEFAULT OFF)
70endif()
71option(LIBCXX_ENABLE_FILESYSTEM "Build filesystem as part of libc++experimental.a"
72    ${ENABLE_FILESYSTEM_DEFAULT})
73option(LIBCXX_INCLUDE_TESTS "Build the libc++ tests." ${LLVM_INCLUDE_TESTS})
74
75# Benchmark options -----------------------------------------------------------
76option(LIBCXX_INCLUDE_BENCHMARKS "Build the libc++ benchmarks and their dependancies" ON)
77set(LIBCXX_BENCHMARK_NATIVE_STDLIB "" CACHE STRING
78        "Build the benchmarks against the specified native STL.
79         The value must be one of libc++/libstdc++")
80set(LIBCXX_BENCHMARK_NATIVE_GCC_TOOLCHAIN "" CACHE STRING
81    "Use alternate GCC toolchain when building the native benchmarks")
82
83if (LIBCXX_BENCHMARK_NATIVE_STDLIB)
84  if (NOT (LIBCXX_BENCHMARK_NATIVE_STDLIB STREQUAL "libc++"
85        OR LIBCXX_BENCHMARK_NATIVE_STDLIB STREQUAL "libstdc++"))
86    message(FATAL_ERROR "Invalid value for LIBCXX_BENCHMARK_NATIVE_STDLIB: "
87            "'${LIBCXX_BENCHMARK_NATIVE_STDLIB}'")
88  endif()
89endif()
90
91option(LIBCXX_INCLUDE_DOCS "Build the libc++ documentation." ${LLVM_INCLUDE_DOCS})
92set(LIBCXX_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}" CACHE STRING
93    "Define suffix of library directory name (32/64)")
94option(LIBCXX_INSTALL_HEADERS "Install the libc++ headers." ON)
95option(LIBCXX_INSTALL_LIBRARY "Install the libc++ library." ON)
96option(LIBCXX_INSTALL_SUPPORT_HEADERS "Install libc++ support headers." ON)
97cmake_dependent_option(LIBCXX_INSTALL_EXPERIMENTAL_LIBRARY
98        "Install libc++experimental.a" ON
99        "LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY;LIBCXX_INSTALL_LIBRARY" OFF)
100set(LIBCXX_ABI_VERSION 1 CACHE STRING "ABI version of libc++.")
101option(LIBCXX_ABI_UNSTABLE "Unstable ABI of libc++." OFF)
102option(LIBCXX_ABI_FORCE_ITANIUM "Ignore auto-detection and force use of the Itanium ABI.")
103option(LIBCXX_ABI_FORCE_MICROSOFT "Ignore auto-detection and force use of the Microsoft ABI.")
104set(LIBCXX_ABI_DEFINES "" CACHE STRING "A semicolon separated list of ABI macros to define in the site config header.")
105option(LIBCXX_USE_COMPILER_RT "Use compiler-rt instead of libgcc" OFF)
106
107if (NOT LIBCXX_ENABLE_SHARED AND NOT LIBCXX_ENABLE_STATIC)
108  message(FATAL_ERROR "libc++ must be built as either a shared or static library.")
109endif()
110
111# ABI Library options ---------------------------------------------------------
112set(LIBCXX_CXX_ABI "default" CACHE STRING
113    "Specify C++ ABI library to use.")
114set(CXXABIS none default libcxxabi libcxxrt libstdc++ libsupc++ vcruntime)
115set_property(CACHE LIBCXX_CXX_ABI PROPERTY STRINGS ;${CXXABIS})
116
117# Setup the default options if LIBCXX_CXX_ABI is not specified.
118if (LIBCXX_CXX_ABI STREQUAL "default")
119  find_path(
120    LIBCXX_LIBCXXABI_INCLUDES_INTERNAL
121    cxxabi.h
122    PATHS ${LLVM_MAIN_SRC_DIR}/projects/libcxxabi/include
123          ${LLVM_MAIN_SRC_DIR}/runtimes/libcxxabi/include
124          ${LLVM_MAIN_SRC_DIR}/../libcxxabi/include
125    NO_DEFAULT_PATH
126    NO_CMAKE_FIND_ROOT_PATH
127  )
128  if (LIBCXX_TARGETING_MSVC)
129    # FIXME: Figure out how to configure the ABI library on Windows.
130    set(LIBCXX_CXX_ABI_LIBNAME "vcruntime")
131  elseif ((NOT LIBCXX_STANDALONE_BUILD OR HAVE_LIBCXXABI) AND
132          IS_DIRECTORY "${LIBCXX_LIBCXXABI_INCLUDES_INTERNAL}")
133    set(LIBCXX_CXX_ABI_LIBNAME "libcxxabi")
134    set(LIBCXX_CXX_ABI_INCLUDE_PATHS "${LIBCXX_LIBCXXABI_INCLUDES_INTERNAL}")
135    set(LIBCXX_CXX_ABI_INTREE 1)
136  elseif (APPLE)
137    set(LIBCXX_CXX_ABI_LIBNAME "libcxxabi")
138    set(LIBCXX_CXX_ABI_SYSTEM 1)
139  else()
140    set(LIBCXX_CXX_ABI_LIBNAME "default")
141  endif()
142else()
143  set(LIBCXX_CXX_ABI_LIBNAME "${LIBCXX_CXX_ABI}")
144endif()
145
146# Use a static copy of the ABI library when linking libc++. This option
147# cannot be used with LIBCXX_ENABLE_ABI_LINKER_SCRIPT.
148option(LIBCXX_ENABLE_STATIC_ABI_LIBRARY "Statically link the ABI library" OFF)
149
150# Generate and install a linker script inplace of libc++.so. The linker script
151# will link libc++ to the correct ABI library. This option is on by default
152# on UNIX platforms other than Apple unless 'LIBCXX_ENABLE_STATIC_ABI_LIBRARY'
153# is on. This option is also disabled when the ABI library is not specified
154# or is specified to be "none".
155set(ENABLE_LINKER_SCRIPT_DEFAULT_VALUE OFF)
156if (LLVM_HAVE_LINK_VERSION_SCRIPT AND NOT LIBCXX_ENABLE_STATIC_ABI_LIBRARY
157      AND NOT LIBCXX_CXX_ABI_LIBNAME STREQUAL "none"
158      AND NOT LIBCXX_CXX_ABI_LIBNAME STREQUAL "default"
159      AND PYTHONINTERP_FOUND
160      AND LIBCXX_ENABLE_SHARED)
161    set(ENABLE_LINKER_SCRIPT_DEFAULT_VALUE ON)
162endif()
163
164option(LIBCXX_ENABLE_ABI_LINKER_SCRIPT
165      "Use and install a linker script for the given ABI library"
166      ${ENABLE_LINKER_SCRIPT_DEFAULT_VALUE})
167
168set(ENABLE_NEW_DELETE_DEFAULT ON)
169if (LIBCXXABI_ENABLE_NEW_DELETE_DEFINITIONS)
170# FIXME: This option should default to off. Unfortunatly GCC 4.9 fails to link
171# programs due to undefined references to new/delete in libc++abi so to work
172# around this libc++abi currently defaults LIBCXXABI_ENABLE_NEW_DELETE_DEFINITIONS
173# to ON. Once the GCC bug has been worked around this option should be changed
174# back to OFF.
175  set(ENABLE_NEW_DELETE_DEFAULT ON)
176endif()
177
178option(LIBCXX_ENABLE_NEW_DELETE_DEFINITIONS
179    "Build libc++ with definitions for operator new/delete. This option can
180    be used to disable the definitions when libc++abi is expected to provide
181    them" ${ENABLE_NEW_DELETE_DEFAULT})
182
183# Build libc++abi with libunwind. We need this option to determine whether to
184# link with libunwind or libgcc_s while running the test cases.
185option(LIBCXXABI_USE_LLVM_UNWINDER "Build and use the LLVM unwinder." OFF)
186option(LIBCXXABI_ENABLE_STATIC_UNWINDER "Statically link the LLVM unwinder." OFF)
187
188# Target options --------------------------------------------------------------
189option(LIBCXX_BUILD_32_BITS "Build 32 bit libc++." ${LLVM_BUILD_32_BITS})
190set(LIBCXX_SYSROOT "" CACHE STRING "Use alternate sysroot.")
191set(LIBCXX_GCC_TOOLCHAIN "" CACHE STRING "Use alternate GCC toolchain.")
192
193# Feature options -------------------------------------------------------------
194option(LIBCXX_ENABLE_EXCEPTIONS "Use exceptions." ON)
195option(LIBCXX_ENABLE_RTTI "Use run time type information." ON)
196option(LIBCXX_ENABLE_GLOBAL_FILESYSTEM_NAMESPACE "Build libc++ with support for the global filesystem namespace." ON)
197option(LIBCXX_ENABLE_STDIN "Build libc++ with support for stdin/std::cin." ON)
198option(LIBCXX_ENABLE_STDOUT "Build libc++ with support for stdout/std::cout." ON)
199option(LIBCXX_ENABLE_THREADS "Build libc++ with support for threads." ON)
200option(LIBCXX_ENABLE_THREAD_UNSAFE_C_FUNCTIONS "Build libc++ with support for thread-unsafe C functions" ON)
201option(LIBCXX_ENABLE_MONOTONIC_CLOCK
202  "Build libc++ with support for a monotonic clock.
203   This option may only be set to OFF when LIBCXX_ENABLE_THREADS=OFF." ON)
204option(LIBCXX_HAS_MUSL_LIBC "Build libc++ with support for the Musl C library" OFF)
205option(LIBCXX_HAS_PTHREAD_API "Ignore auto-detection and force use of pthread API" OFF)
206option(LIBCXX_HAS_WIN32_THREAD_API "Ignore auto-detection and force use of win32 thread API" OFF)
207option(LIBCXX_HAS_EXTERNAL_THREAD_API
208  "Build libc++ with an externalized threading API.
209   This option may only be set to ON when LIBCXX_ENABLE_THREADS=ON." OFF)
210option(LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY
211    "Build libc++ with an externalized threading library.
212     This option may only be set to ON when LIBCXX_ENABLE_THREADS=ON" OFF)
213
214# Misc options ----------------------------------------------------------------
215# FIXME: Turn -pedantic back ON. It is currently off because it warns
216# about #include_next which is used everywhere.
217option(LIBCXX_ENABLE_PEDANTIC "Compile with pedantic enabled." OFF)
218option(LIBCXX_ENABLE_WERROR "Fail and stop if a warning is triggered." OFF)
219option(LIBCXX_DISABLE_MACRO_CONFLICT_WARNINGS "Disable #warnings about conflicting macros." OFF)
220
221option(LIBCXX_GENERATE_COVERAGE "Enable generating code coverage." OFF)
222set(LIBCXX_COVERAGE_LIBRARY "" CACHE STRING
223    "The Profile-rt library used to build with code coverage")
224
225# Don't allow a user to accidentally overwrite the system libc++ installation on Darwin.
226# If the user specifies -DCMAKE_INSTALL_PREFIX=/usr the install rules for libc++
227# will not be generated and a warning will be issued.
228option(LIBCXX_OVERRIDE_DARWIN_INSTALL "Enable overwriting darwins libc++ installation." OFF)
229mark_as_advanced(LIBCXX_OVERRIDE_DARWIN_INSTALL) # Don't show this option by default.
230
231if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin" AND NOT LIBCXX_OVERRIDE_DARWIN_INSTALL)
232  if ("${CMAKE_INSTALL_PREFIX}" STREQUAL "/usr")
233    message(WARNING "Disabling libc++ install rules because installation would "
234                    "overwrite the systems installation. Configure with "
235                    "-DLIBCXX_OVERRIDE_DARWIN_INSTALL=ON to suppress this behaviour.")
236    mark_as_advanced(CLEAR LIBCXX_OVERRIDE_DARWIN_INSTALL) # Show the override option.
237    set(LIBCXX_INSTALL_HEADERS OFF)
238    set(LIBCXX_INSTALL_LIBRARY OFF)
239  endif()
240endif()
241
242set(LIBCXX_CONFIGURE_IDE_DEFAULT OFF)
243if (XCODE OR MSVC_IDE)
244  set(LIBCXX_CONFIGURE_IDE_DEFAULT ON)
245endif()
246option(LIBCXX_CONFIGURE_IDE "Configure libcxx for use within an IDE"
247      ${LIBCXX_CONFIGURE_IDE_DEFAULT})
248
249#===============================================================================
250# Check option configurations
251#===============================================================================
252
253if (LIBCXX_ENABLE_FILESYSTEM AND NOT LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY)
254  message(FATAL_ERROR
255    "LIBCXX_ENABLE_FILESYSTEM cannot be turned on when LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY=OFF")
256endif()
257
258# Ensure LIBCXX_ENABLE_MONOTONIC_CLOCK is set to ON only when
259# LIBCXX_ENABLE_THREADS is on.
260if(LIBCXX_ENABLE_THREADS AND NOT LIBCXX_ENABLE_MONOTONIC_CLOCK)
261  message(FATAL_ERROR "LIBCXX_ENABLE_MONOTONIC_CLOCK can only be set to OFF"
262                      " when LIBCXX_ENABLE_THREADS is also set to OFF.")
263endif()
264
265if(NOT LIBCXX_ENABLE_THREADS)
266  if(LIBCXX_HAS_PTHREAD_API)
267    message(FATAL_ERROR "LIBCXX_HAS_PTHREAD_API can only be set to ON"
268                        " when LIBCXX_ENABLE_THREADS is also set to ON.")
269  endif()
270  if(LIBCXX_HAS_EXTERNAL_THREAD_API)
271    message(FATAL_ERROR "LIBCXX_HAS_EXTERNAL_THREAD_API can only be set to ON"
272                        " when LIBCXX_ENABLE_THREADS is also set to ON.")
273  endif()
274  if (LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY)
275    message(FATAL_ERROR "LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY can only be set "
276                        "to ON when LIBCXX_ENABLE_THREADS is also set to ON.")
277  endif()
278  if (LIBCXX_HAS_WIN32_THREAD_API)
279    message(FATAL_ERROR "LIBCXX_HAS_WIN32_THREAD_API can only be set to ON"
280                        " when LIBCXX_ENABLE_THREADS is also set to ON.")
281  endif()
282
283endif()
284
285if (LIBCXX_HAS_EXTERNAL_THREAD_API)
286  if (LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY)
287    message(FATAL_ERROR "The options LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY and "
288                        "LIBCXX_HAS_EXTERNAL_THREAD_API cannot both be ON at "
289                        "the same time")
290  endif()
291  if (LIBCXX_HAS_PTHREAD_API)
292    message(FATAL_ERROR "The options LIBCXX_HAS_EXTERNAL_THREAD_API"
293                        "and LIBCXX_HAS_PTHREAD_API cannot be both"
294                        "set to ON at the same time.")
295  endif()
296  if (LIBCXX_HAS_WIN32_THREAD_API)
297    message(FATAL_ERROR "The options LIBCXX_HAS_EXTERNAL_THREAD_API"
298                        "and LIBCXX_HAS_WIN32_THREAD_API cannot be both"
299                        "set to ON at the same time.")
300  endif()
301endif()
302
303if (LIBCXX_HAS_PTHREAD_API)
304  if (LIBCXX_HAS_WIN32_THREAD_API)
305    message(FATAL_ERROR "The options LIBCXX_HAS_PTHREAD_API"
306                        "and LIBCXX_HAS_WIN32_THREAD_API cannot be both"
307                        "set to ON at the same time.")
308  endif()
309endif()
310
311# Ensure LLVM_USE_SANITIZER is not specified when LIBCXX_GENERATE_COVERAGE
312# is ON.
313if (LLVM_USE_SANITIZER AND LIBCXX_GENERATE_COVERAGE)
314  message(FATAL_ERROR "LLVM_USE_SANITIZER cannot be used with LIBCXX_GENERATE_COVERAGE")
315endif()
316
317# Set LIBCXX_BUILD_32_BITS to (LIBCXX_BUILD_32_BITS OR LLVM_BUILD_32_BITS)
318# and check that we can build with 32 bits if requested.
319if (CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32)
320  if (LIBCXX_BUILD_32_BITS AND NOT LLVM_BUILD_32_BITS) # Don't duplicate the output from LLVM
321    message(STATUS "Building 32 bits executables and libraries.")
322  endif()
323elseif(LIBCXX_BUILD_32_BITS)
324  message(FATAL_ERROR "LIBCXX_BUILD_32_BITS=ON is not supported on this platform.")
325endif()
326
327# Check that this option is not enabled on Apple and emit a usage warning.
328if (LIBCXX_ENABLE_STATIC_ABI_LIBRARY)
329  if (APPLE)
330    message(FATAL_ERROR "LIBCXX_ENABLE_STATIC_ABI_LIBRARY is not supported on OS X")
331  else()
332    message(WARNING "LIBCXX_ENABLE_STATIC_ABI_LIBRARY is an experimental option")
333  endif()
334  if (LIBCXX_ENABLE_STATIC AND NOT PYTHONINTERP_FOUND)
335    message(FATAL_ERROR "LIBCXX_ENABLE_STATIC_ABI_LIBRARY requires python but it was not found.")
336  endif()
337endif()
338
339if (LIBCXX_ENABLE_ABI_LINKER_SCRIPT)
340    if (APPLE)
341      message(FATAL_ERROR "LIBCXX_ENABLE_ABI_LINKER_SCRIPT cannot be used on APPLE targets")
342    endif()
343    if (NOT PYTHONINTERP_FOUND)
344      message(FATAL_ERROR "LIBCXX_ENABLE_ABI_LINKER_SCRIPT requires python but it was not found.")
345    endif()
346    if (NOT LIBCXX_ENABLE_SHARED)
347      message(FATAL_ERROR "LIBCXX_ENABLE_ABI_LINKER_SCRIPT is only available for shared library builds.")
348    endif()
349endif()
350
351if (LIBCXX_ENABLE_STATIC_ABI_LIBRARY AND LIBCXX_ENABLE_ABI_LINKER_SCRIPT)
352    message(FATAL_ERROR "Conflicting options given.
353        LIBCXX_ENABLE_STATIC_ABI_LIBRARY cannot be specified with
354        LIBCXX_ENABLE_ABI_LINKER_SCRIPT")
355endif()
356
357if (LIBCXX_HAS_MUSL_LIBC AND NOT LIBCXX_INSTALL_SUPPORT_HEADERS)
358  message(FATAL_ERROR "LIBCXX_INSTALL_SUPPORT_HEADERS can not be turned off"
359                      "when building for Musl with LIBCXX_HAS_MUSL_LIBC.")
360endif()
361
362if (LIBCXX_ABI_FORCE_ITANIUM AND LIBCXX_ABI_FORCE_MICROSOFT)
363  message(FATAL_ERROR "Only one of LIBCXX_ABI_FORCE_ITANIUM and LIBCXX_ABI_FORCE_MICROSOFT can be specified.")
364endif ()
365
366#===============================================================================
367# Configure System
368#===============================================================================
369
370set(LIBCXX_COMPILER    ${CMAKE_CXX_COMPILER})
371set(LIBCXX_SOURCE_DIR  ${CMAKE_CURRENT_SOURCE_DIR})
372set(LIBCXX_BINARY_DIR  ${CMAKE_CURRENT_BINARY_DIR})
373set(LIBCXX_BINARY_INCLUDE_DIR "${LIBCXX_BINARY_DIR}/include/c++build")
374if (LLVM_LIBRARY_OUTPUT_INTDIR)
375  set(LIBCXX_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
376else()
377  set(LIBCXX_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LIBCXX_LIBDIR_SUFFIX})
378endif()
379file(MAKE_DIRECTORY "${LIBCXX_BINARY_INCLUDE_DIR}")
380
381set(LIBCXX_INSTALL_PREFIX "" CACHE STRING
382    "Define libc++ destination prefix.")
383
384if (NOT LIBCXX_INSTALL_PREFIX MATCHES "^$|.*/")
385  message(FATAL_ERROR "LIBCXX_INSTALL_PREFIX has to end with \"/\".")
386endif()
387
388set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${LIBCXX_LIBRARY_DIR})
389set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${LIBCXX_LIBRARY_DIR})
390set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LIBCXX_LIBRARY_DIR})
391
392# Declare libc++ configuration variables.
393# They are intended for use as follows:
394# LIBCXX_CXX_FLAGS: General flags for both the compiler and linker.
395# LIBCXX_COMPILE_FLAGS: Compile only flags.
396# LIBCXX_LINK_FLAGS: Linker only flags.
397# LIBCXX_LIBRARIES: libraries libc++ is linked to.
398# LIBCXX_INTERFACE_LIBRARIES: Libraries that must be linked when using libc++
399#                             These libraries are exposed in the linker script.
400set(LIBCXX_COMPILE_FLAGS "")
401set(LIBCXX_LINK_FLAGS "")
402set(LIBCXX_LIBRARIES "")
403set(LIBCXX_INTERFACE_LIBRARIES "")
404
405# Include macros for adding and removing libc++ flags.
406include(HandleLibcxxFlags)
407
408# Target flags ================================================================
409# These flags get added to CMAKE_CXX_FLAGS and CMAKE_C_FLAGS so that
410# 'config-ix' use them during feature checks. It also adds them to both
411# 'LIBCXX_COMPILE_FLAGS' and 'LIBCXX_LINK_FLAGS'
412add_target_flags_if(LIBCXX_BUILD_32_BITS "-m32")
413add_target_flags_if(LIBCXX_TARGET_TRIPLE "--target=${LIBCXX_TARGET_TRIPLE}")
414add_target_flags_if(LIBCXX_SYSROOT "--sysroot=${LIBCXX_SYSROOT}")
415add_target_flags_if(LIBCXX_GCC_TOOLCHAIN "--gcc-toolchain=${LIBCXX_GCC_TOOLCHAIN}")
416if (LIBCXX_TARGET_TRIPLE)
417  set(TARGET_TRIPLE "${LIBCXX_TARGET_TRIPLE}")
418endif()
419
420# Configure compiler.
421include(config-ix)
422
423if (LIBCXX_USE_COMPILER_RT)
424  list(APPEND LIBCXX_LINK_FLAGS "-rtlib=compiler-rt")
425endif()
426
427# Configure coverage options.
428if (LIBCXX_GENERATE_COVERAGE)
429  include(CodeCoverage)
430  set(CMAKE_BUILD_TYPE "COVERAGE" CACHE STRING "" FORCE)
431endif()
432
433string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
434if (uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
435  set(LIBCXX_DEBUG_BUILD ON)
436else()
437  set(LIBCXX_DEBUG_BUILD OFF)
438endif()
439
440#===============================================================================
441# Setup Compiler Flags
442#===============================================================================
443
444include(HandleLibCXXABI) # Setup the ABI library flags
445
446if (NOT LIBCXX_STANDALONE_BUILD)
447  # Remove flags that may have snuck in.
448  remove_flags(-DNDEBUG -UNDEBUG -D_DEBUG
449               -lc++abi)
450endif()
451remove_flags(-stdlib=libc++ -stdlib=libstdc++)
452
453# FIXME: Remove all debug flags and flags that change which Windows
454# default libraries are linked. Currently we only support linking the
455# non-debug DLLs
456remove_flags("/D_DEBUG" "/MTd" "/MDd" "/MT" "/Md")
457
458# FIXME(EricWF): See the FIXME on LIBCXX_ENABLE_PEDANTIC.
459# Remove the -pedantic flag and -Wno-pedantic and -pedantic-errors
460# so they don't get transformed into -Wno and -errors respectively.
461remove_flags(-Wno-pedantic -pedantic-errors -pedantic)
462
463# Required flags ==============================================================
464set(LIBCXX_STANDARD_VER c++11 CACHE INTERNAL "internal option to change build dialect")
465if (LIBCXX_HAS_MUSL_LIBC)
466  # musl's pthread implementations uses volatile types in their structs which is
467  # not a constexpr in C++11 but is in C++14, so we use C++14 with musl.
468  set(LIBCXX_STANDARD_VER c++14 CACHE INTERNAL "internal option to change build dialect")
469endif()
470add_compile_flags_if_supported(-std=${LIBCXX_STANDARD_VER})
471mangle_name("LIBCXX_SUPPORTS_STD_EQ_${LIBCXX_STANDARD_VER}_FLAG" SUPPORTS_DIALECT_NAME)
472if(NOT ${SUPPORTS_DIALECT_NAME})
473  if(NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC" AND NOT "${CMAKE_CXX_SIMULATE_ID}" STREQUAL "MSVC")
474    message(FATAL_ERROR "C++11 or greater is required but the compiler does not support ${LIBCXX_STANDARD_VER}")
475  endif()
476endif()
477
478# On all systems the system c++ standard library headers need to be excluded.
479# MSVC only has -X, which disables all default includes; including the crt.
480# Thus, we do nothing and hope we don't accidentally include any of the C++
481# headers
482add_compile_flags_if_supported(-nostdinc++)
483
484# Hide all inline function definitions which have not explicitly been marked
485# visible. This prevents new definitions for inline functions from appearing in
486# the dylib when get ODR used by another function.
487add_compile_flags_if_supported(-fvisibility-inlines-hidden)
488
489if (LIBCXX_CONFIGURE_IDE)
490  # This simply allows IDE to process <experimental/coroutine>
491  add_compile_flags_if_supported(-fcoroutines-ts)
492endif()
493
494# Let the library headers know they are currently being used to build the
495# library.
496add_definitions(-D_LIBCPP_BUILDING_LIBRARY)
497
498if (NOT LIBCXX_ENABLE_NEW_DELETE_DEFINITIONS)
499  add_definitions(-D_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS)
500endif()
501
502# Warning flags ===============================================================
503add_definitions(-D_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
504add_compile_flags_if_supported(
505    -Wall -Wextra -W -Wwrite-strings
506    -Wno-unused-parameter -Wno-long-long
507    -Werror=return-type)
508if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
509    add_compile_flags_if_supported(
510        -Wno-user-defined-literals
511        -Wno-covered-switch-default)
512elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
513    add_compile_flags_if_supported(
514        -Wno-literal-suffix
515        -Wno-c++14-compat
516        -Wno-noexcept-type)
517endif()
518if (LIBCXX_ENABLE_WERROR)
519  add_compile_flags_if_supported(-Werror)
520  add_compile_flags_if_supported(-WX)
521else()
522  # TODO(EricWF) Remove this. We shouldn't be suppressing errors when -Werror is
523  # added elsewhere.
524  add_compile_flags_if_supported(-Wno-error)
525endif()
526if (LIBCXX_ENABLE_PEDANTIC)
527  add_compile_flags_if_supported(-pedantic)
528endif()
529if (LIBCXX_DISABLE_MACRO_CONFLICT_WARNINGS)
530  add_definitions(-D_LIBCPP_DISABLE_MACRO_CONFLICT_WARNINGS)
531endif()
532
533# Exception flags =============================================================
534if (LIBCXX_ENABLE_EXCEPTIONS)
535  # Catches C++ exceptions only and tells the compiler to assume that extern C
536  # functions never throw a C++ exception.
537  add_compile_flags_if_supported(-EHsc)
538else()
539  add_definitions(-D_LIBCPP_NO_EXCEPTIONS)
540  add_compile_flags_if_supported(-EHs- -EHa-)
541  add_compile_flags_if_supported(-fno-exceptions)
542endif()
543
544# RTTI flags ==================================================================
545if (NOT LIBCXX_ENABLE_RTTI)
546  add_definitions(-D_LIBCPP_NO_RTTI)
547  add_compile_flags_if_supported(-GR-)
548  add_compile_flags_if_supported(-fno-rtti)
549endif()
550
551# Threading flags =============================================================
552if (LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY AND LIBCXX_ENABLE_SHARED)
553  # Need to allow unresolved symbols if this is to work with shared library builds
554  if (APPLE)
555    add_link_flags("-undefined dynamic_lookup")
556  else()
557    # Relax this restriction from HandleLLVMOptions
558    string(REPLACE "-Wl,-z,defs" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")
559  endif()
560endif()
561
562# Assertion flags =============================================================
563define_if(LIBCXX_ENABLE_ASSERTIONS -UNDEBUG)
564define_if_not(LIBCXX_ENABLE_ASSERTIONS -DNDEBUG)
565define_if(LIBCXX_ENABLE_ASSERTIONS -D_LIBCPP_DEBUG=0)
566define_if(LIBCXX_DEBUG_BUILD -D_DEBUG)
567if (LIBCXX_ENABLE_ASSERTIONS AND NOT LIBCXX_DEBUG_BUILD)
568  # MSVC doesn't like _DEBUG on release builds. See PR 4379.
569  define_if_not(LIBCXX_TARGETING_MSVC -D_DEBUG)
570endif()
571
572# Modules flags ===============================================================
573# FIXME The libc++ sources are fundamentally non-modular. They need special
574# versions of the headers in order to provide C++03 and legacy ABI definitions.
575# NOTE: The public headers can be used with modules in all other contexts.
576if (LLVM_ENABLE_MODULES)
577  # Ignore that the rest of the modules flags are now unused.
578  add_compile_flags_if_supported(-Wno-unused-command-line-argument)
579  add_compile_flags(-fno-modules)
580endif()
581
582# Sanitizer flags =============================================================
583
584# Configure for sanitizers. If LIBCXX_STANDALONE_BUILD then we have to do
585# the flag translation ourselves. Othewise LLVM's CMakeList.txt will handle it.
586if (LIBCXX_STANDALONE_BUILD)
587  set(LLVM_USE_SANITIZER "" CACHE STRING
588      "Define the sanitizer used to build the library and tests")
589  # NOTE: LLVM_USE_SANITIZER checks for a UNIX like system instead of MSVC.
590  # But we don't have LLVM_ON_UNIX so checking for MSVC is the best we can do.
591  if (LLVM_USE_SANITIZER AND NOT MSVC)
592    add_flags_if_supported("-fno-omit-frame-pointer")
593    add_flags_if_supported("-gline-tables-only")
594
595    if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" AND
596        NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINFO")
597        add_flags_if_supported("-gline-tables-only")
598    endif()
599    if (LLVM_USE_SANITIZER STREQUAL "Address")
600      add_flags("-fsanitize=address")
601    elseif (LLVM_USE_SANITIZER MATCHES "Memory(WithOrigins)?")
602      add_flags(-fsanitize=memory)
603      if (LLVM_USE_SANITIZER STREQUAL "MemoryWithOrigins")
604        add_flags("-fsanitize-memory-track-origins")
605      endif()
606    elseif (LLVM_USE_SANITIZER STREQUAL "Undefined")
607      add_flags("-fsanitize=undefined -fno-sanitize=vptr,function -fno-sanitize-recover=all")
608    elseif (LLVM_USE_SANITIZER STREQUAL "Thread")
609      add_flags(-fsanitize=thread)
610    else()
611      message(WARNING "Unsupported value of LLVM_USE_SANITIZER: ${LLVM_USE_SANITIZER}")
612    endif()
613  elseif(LLVM_USE_SANITIZER AND MSVC)
614    message(WARNING "LLVM_USE_SANITIZER is not supported on this platform.")
615  endif()
616endif()
617
618# Configuration file flags =====================================================
619if (NOT LIBCXX_ABI_VERSION EQUAL "1")
620  config_define(${LIBCXX_ABI_VERSION} _LIBCPP_ABI_VERSION)
621endif()
622config_define_if(LIBCXX_ABI_UNSTABLE _LIBCPP_ABI_UNSTABLE)
623config_define_if(LIBCXX_ABI_FORCE_ITANIUM _LIBCPP_ABI_FORCE_ITANIUM)
624config_define_if(LIBCXX_ABI_FORCE_MICROSOFT _LIBCPP_ABI_FORCE_MICROSOFT)
625
626config_define_if_not(LIBCXX_ENABLE_GLOBAL_FILESYSTEM_NAMESPACE _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE)
627config_define_if_not(LIBCXX_ENABLE_STDIN _LIBCPP_HAS_NO_STDIN)
628config_define_if_not(LIBCXX_ENABLE_STDOUT _LIBCPP_HAS_NO_STDOUT)
629config_define_if_not(LIBCXX_ENABLE_THREADS _LIBCPP_HAS_NO_THREADS)
630config_define_if_not(LIBCXX_ENABLE_MONOTONIC_CLOCK _LIBCPP_HAS_NO_MONOTONIC_CLOCK)
631config_define_if_not(LIBCXX_ENABLE_THREAD_UNSAFE_C_FUNCTIONS _LIBCPP_HAS_NO_THREAD_UNSAFE_C_FUNCTIONS)
632
633config_define_if(LIBCXX_HAS_PTHREAD_API _LIBCPP_HAS_THREAD_API_PTHREAD)
634config_define_if(LIBCXX_HAS_EXTERNAL_THREAD_API _LIBCPP_HAS_THREAD_API_EXTERNAL)
635config_define_if(LIBCXX_HAS_WIN32_THREAD_API _LIBCPP_HAS_THREAD_API_WIN32)
636config_define_if(LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY _LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL)
637config_define_if(LIBCXX_HAS_MUSL_LIBC _LIBCPP_HAS_MUSL_LIBC)
638config_define_if(LIBCXX_NO_VCRUNTIME _LIBCPP_NO_VCRUNTIME)
639
640if (LIBCXX_ABI_DEFINES)
641  set(abi_defines)
642  foreach (abi_define ${LIBCXX_ABI_DEFINES})
643    if (NOT abi_define MATCHES "^_LIBCPP_ABI_")
644      message(SEND_ERROR "Invalid ABI macro ${abi_define} in LIBCXX_ABI_DEFINES")
645    endif()
646    list(APPEND abi_defines "#define ${abi_define}")
647  endforeach()
648  string(REPLACE ";" "\n" abi_defines "${abi_defines}")
649  config_define(${abi_defines} _LIBCPP_ABI_DEFINES)
650endif()
651
652# By default libc++ on Windows expects to use a shared library, which requires
653# the headers to use DLL import/export semantics. However when building a
654# static library only we modify the headers to disable DLL import/export.
655if (DEFINED WIN32 AND LIBCXX_ENABLE_STATIC AND NOT LIBCXX_ENABLE_SHARED)
656  message(STATUS "Generating custom __config for non-DLL Windows build")
657  config_define(ON _LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS)
658endif()
659
660set(site_config_path "${LIBCXX_BINARY_DIR}/__config_site")
661if (LIBCXX_NEEDS_SITE_CONFIG)
662  configure_file("include/__config_site.in"
663                 "${site_config_path}"
664                 @ONLY)
665
666  # Provide the config definitions by included the generated __config_site
667  # file at compile time.
668  if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC" OR "${CMAKE_CXX_SIMULATE_ID}" STREQUAL "MSVC")
669    add_compile_flags("/FI\"${LIBCXX_BINARY_DIR}/__config_site\"")
670  else()
671    add_compile_flags("-include ${LIBCXX_BINARY_DIR}/__config_site")
672  endif()
673else()
674  if (EXISTS "${site_config_path}")
675    message(STATUS "Removing stale site configuration ${site_config_path}")
676    file(REMOVE "${site_config_path}")
677  endif()
678endif()
679
680#===============================================================================
681# Setup Source Code And Tests
682#===============================================================================
683include_directories(include)
684add_subdirectory(include)
685add_subdirectory(lib)
686
687
688if (LIBCXX_INCLUDE_BENCHMARKS)
689  add_subdirectory(benchmarks)
690endif()
691
692# Create the lit.site.cfg file even when LIBCXX_INCLUDE_TESTS is OFF or
693# LLVM_FOUND is OFF. This allows users to run the tests manually using
694# LIT without requiring a full LLVM checkout.
695#
696# However, since some submission systems strip test/ subdirectories, check for
697# it before adding it.
698
699if(IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/test")
700  add_subdirectory(test)
701endif()
702if (LIBCXX_INCLUDE_TESTS)
703  add_subdirectory(lib/abi)
704endif()
705
706if (LIBCXX_STANDALONE_BUILD AND EXISTS "${LLVM_MAIN_SRC_DIR}/utils/llvm-lit")
707  include(AddLLVM) # for get_llvm_lit_path
708  # Make sure the llvm-lit script is generated into the bin directory, and do
709  # it after adding all tests, since the generated script will only work
710  # correctly discovered tests against test locations from the source tree that
711  # have already been discovered.
712  add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/llvm-lit
713                   ${CMAKE_CURRENT_BINARY_DIR}/llvm-lit)
714endif()
715
716if (LIBCXX_INCLUDE_DOCS)
717  add_subdirectory(docs)
718endif()
719