1# See www/CMake.html for instructions on how to build libcxx with CMake.
2
3#===============================================================================
4# Setup Project
5#===============================================================================
6
7project(libcxx CXX C)
8cmake_minimum_required(VERSION 2.8)
9
10if(POLICY CMP0042)
11  cmake_policy(SET CMP0042 NEW) # Set MACOSX_RPATH=YES by default
12endif()
13
14set(PACKAGE_NAME libcxx)
15set(PACKAGE_VERSION trunk-svn)
16set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}")
17set(PACKAGE_BUGREPORT "llvmbugs@cs.uiuc.edu")
18
19# Add path for custom modules
20set(CMAKE_MODULE_PATH
21  "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
22  "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
23  ${CMAKE_MODULE_PATH}
24  )
25
26# Require out of source build.
27include(MacroEnsureOutOfSourceBuild)
28MACRO_ENSURE_OUT_OF_SOURCE_BUILD(
29 "${PROJECT_NAME} requires an out of source build. Please create a separate
30 build directory and run 'cmake /path/to/${PROJECT_NAME} [options]' there."
31 )
32
33if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
34  set(LIBCXX_LIBDIR_SUFFIX "" CACHE STRING
35      "Define suffix of library directory name (32/64)")
36
37  set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LIBCXX_LIBDIR_SUFFIX})
38  set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LIBCXX_LIBDIR_SUFFIX})
39
40  set(LIBCXX_BUILT_STANDALONE 1)
41else()
42  set(LIBCXX_LIBDIR_SUFFIX ${LLVM_LIBDIR_SUFFIX})
43endif()
44
45#===============================================================================
46# Setup CMake Options
47#===============================================================================
48
49# Define options.
50option(LIBCXX_ENABLE_EXCEPTIONS "Use exceptions." ON)
51option(LIBCXX_ENABLE_RTTI "Use run time type information." ON)
52option(LIBCXX_ENABLE_ASSERTIONS "Enable assertions independent of build mode." ON)
53option(LIBCXX_ENABLE_PEDANTIC "Compile with pedantic enabled." ON)
54option(LIBCXX_ENABLE_WERROR "Fail and stop if a warning is triggered." OFF)
55option(LIBCXX_ENABLE_CXX1Y "Enable -std=c++1y and use of c++1y language features if the compiler supports it." OFF)
56option(LIBCXX_ENABLE_SHARED "Build libc++ as a shared library." ON)
57option(LIBCXX_ENABLE_THREADS "Build libc++ with support for threads." ON)
58option(LIBCXX_BUILD_32_BITS "Build 32 bit libc++" OFF)
59option(LIBCXX_ENABLE_MONOTONIC_CLOCK
60  "Build libc++ with support for a monotonic clock.
61   This option may only be used when LIBCXX_ENABLE_THREADS=OFF." ON)
62option(LIBCXX_INSTALL_HEADERS "Install the libc++ headers." ON)
63option(LIBCXX_INSTALL_SUPPORT_HEADERS "Install libc++ support headers." ON)
64option(LIBCXX_ENABLE_STATIC_ABI_LIBRARY "Statically link the ABI library" OFF)
65set(LIBCXX_SYSROOT "" CACHE STRING "Use alternate sysroot.")
66set(LIBCXX_GCC_TOOLCHAIN "" CACHE STRING "Use alternate GCC toolchain.")
67if (LIBCXX_BUILT_STANDALONE)
68  set(LLVM_USE_SANITIZER "" CACHE STRING
69      "Define the sanitizer used to build the library and tests")
70endif()
71
72if (LIBCXX_ENABLE_STATIC_ABI_LIBRARY)
73  if (APPLE)
74    message(FATAL_ERROR "LIBCXX_ENABLE_STATIC_ABI_LIBRARY is not supported on OS X")
75  else()
76    message(WARNING "LIBCXX_ENABLE_STATIC_ABI_LIBRARY is an experimental option")
77  endif()
78endif()
79
80set(CXXABIS none libcxxabi libcxxrt libstdc++ libsupc++)
81if (NOT LIBCXX_CXX_ABI)
82  if (NOT DEFINED LIBCXX_BUILT_STANDALONE AND
83      IS_DIRECTORY "${CMAKE_SOURCE_DIR}/projects/libcxxabi")
84    set(LIBCXX_CXX_ABI_LIBNAME "libcxxabi")
85    set(LIBCXX_LIBCXXABI_INCLUDE_PATHS "${CMAKE_SOURCE_DIR}/projects/libcxxabi/include")
86    set(LIBCXX_CXX_ABI_INTREE 1)
87  else ()
88    set(LIBCXX_CXX_ABI_LIBNAME "none")
89  endif ()
90else ()
91  set(LIBCXX_CXX_ABI_LIBNAME "${LIBCXX_CXX_ABI}")
92endif ()
93set(LIBCXX_CXX_ABI "${LIBCXX_CXX_ABI}" CACHE STRING
94    "Specify C++ ABI library to use." FORCE)
95set_property(CACHE LIBCXX_CXX_ABI PROPERTY STRINGS ;${CXXABIS})
96
97#===============================================================================
98# Configure System
99#===============================================================================
100
101set(LIBCXX_COMPILER    ${CMAKE_CXX_COMPILER})
102set(LIBCXX_SOURCE_DIR  ${CMAKE_CURRENT_SOURCE_DIR})
103set(LIBCXX_BINARY_DIR  ${CMAKE_CURRENT_BINARY_DIR})
104set(LIBCXX_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LIBCXX_LIBDIR_SUFFIX})
105
106# Declare libc++ configuration variables.
107# They are intended for use as follows:
108# LIBCXX_CXX_FLAGS: General flags for both the compiler and linker.
109# LIBCXX_COMPILE_FLAGS: Compile only flags.
110# LIBCXX_LINK_FLAGS: Linker only flags.
111set(LIBCXX_CXX_FLAGS "")
112set(LIBCXX_COMPILE_FLAGS "")
113set(LIBCXX_LINK_FLAGS "")
114
115# Configure compiler.
116include(config-ix)
117# Configure ABI library
118include(HandleLibCXXABI)
119
120#===============================================================================
121# Setup Compiler Flags
122#===============================================================================
123
124# Get required flags.
125# On all systems the system c++ standard library headers need to be excluded.
126if (MSVC)
127  # MSVC only has -X, which disables all default includes; including the crt.
128  # Thus, we do nothing and hope we don't accidentally include any of the C++
129  # headers.
130else()
131  if (LIBCXX_HAS_NOSTDINCXX_FLAG)
132    list(APPEND LIBCXX_COMPILE_FLAGS -nostdinc++)
133    string(REPLACE "-stdlib=libc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
134    string(REPLACE "-stdlib=libstdc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
135  endif()
136  # If c++1y has been enabled then attempt to use it. Fail if it is no supported
137  # by the compiler. Otherwise choose c++11 and ensure the compiler supports it.
138  if (LIBCXX_ENABLE_CXX1Y)
139    if (LIBCXX_HAS_STDCXX1Y_FLAG)
140      set(LIBCXX_STD_VERSION c++1y)
141    else()
142      message(FATAL_ERROR "c++1y was enabled but the compiler does not support it.")
143    endif()
144  else()
145    if (LIBCXX_HAS_STDCXX11_FLAG)
146      set(LIBCXX_STD_VERSION c++11)
147    else()
148      message(FATAL_ERROR "c++11 is required by libc++ but is not supported by the compiler")
149    endif()
150  endif()
151  # LIBCXX_STD_VERSION should always be set at this point.
152  list(APPEND LIBCXX_CXX_FLAGS "-std=${LIBCXX_STD_VERSION}")
153endif()
154
155macro(append_if list condition var)
156  if (${condition})
157    list(APPEND ${list} ${var})
158  endif()
159endmacro()
160
161# Get warning flags
162if (NOT MSVC)
163  append_if(LIBCXX_COMPILE_FLAGS LIBCXX_HAS_WALL_FLAG -Wall)
164  list(APPEND LIBCXX_COMPILE_FLAGS -Werror=return-type)
165endif()
166
167append_if(LIBCXX_COMPILE_FLAGS LIBCXX_HAS_W_FLAG -W)
168append_if(LIBCXX_COMPILE_FLAGS LIBCXX_HAS_WNO_UNUSED_PARAMETER_FLAG -Wno-unused-parameter)
169append_if(LIBCXX_COMPILE_FLAGS LIBCXX_HAS_WWRITE_STRINGS_FLAG -Wwrite-strings)
170append_if(LIBCXX_COMPILE_FLAGS LIBCXX_HAS_WNO_LONG_LONG_FLAG -Wno-long-long)
171if (LIBCXX_ENABLE_WERROR)
172  append_if(LIBCXX_COMPILE_FLAGS LIBCXX_HAS_WERROR_FLAG -Werror)
173  append_if(LIBCXX_COMPILE_FLAGS LIBCXX_HAS_WX_FLAG -WX)
174else()
175  append_if(LIBCXX_COMPILE_FLAGS LIBCXX_HAS_WNO_ERROR_FLAG -Wno-error)
176  append_if(LIBCXX_COMPILE_FLAGS LIBCXX_HAS_NO_WX_FLAG -WX-)
177endif()
178if (LIBCXX_ENABLE_PEDANTIC)
179  append_if(LIBCXX_COMPILE_FLAGS LIBCXX_HAS_PEDANTIC_FLAG -pedantic)
180endif()
181
182# Get feature flags.
183# Exceptions
184if (LIBCXX_ENABLE_EXCEPTIONS)
185  # Catches C++ exceptions only and tells the compiler to assume that extern C
186  # functions never throw a C++ exception.
187  append_if(LIBCXX_CXX_FLAGS LIBCXX_HAS_EHSC_FLAG -EHsc)
188else()
189  list(APPEND LIBCXX_CXX_FLAGS -D_LIBCPP_NO_EXCEPTIONS)
190  append_if(LIBCXX_CXX_FLAGS LIBCXX_HAS_NO_EHS_FLAG -EHs-)
191  append_if(LIBCXX_CXX_FLAGS LIBCXX_HAS_NO_EHA_FLAG -EHa-)
192  append_if(LIBCXX_CXX_FLAGS LIBCXX_HAS_FNO_EXCEPTIONS_FLAG -fno-exceptions)
193endif()
194# RTTI
195if (NOT LIBCXX_ENABLE_RTTI)
196  list(APPEND LIBCXX_CXX_FLAGS -D_LIBCPP_NO_RTTI)
197  append_if(LIBCXX_CXX_FLAGS LIBCXX_HAS_NO_GR_FLAG -GR-)
198  append_if(LIBCXX_CXX_FLAGS LIBCXX_HAS_FNO_RTTI_FLAG -fno-rtti)
199endif()
200# Assert
201string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
202if (LIBCXX_ENABLE_ASSERTIONS)
203  # MSVC doesn't like _DEBUG on release builds. See PR 4379.
204  if (NOT MSVC)
205    list(APPEND LIBCXX_COMPILE_FLAGS -D_DEBUG)
206  endif()
207  # On Release builds cmake automatically defines NDEBUG, so we
208  # explicitly undefine it:
209  if (uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE")
210    list(APPEND LIBCXX_COMPILE_FLAGS -UNDEBUG)
211  endif()
212else()
213  if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE")
214    list(APPEND LIBCXX_COMPILE_FLAGS -DNDEBUG)
215  endif()
216endif()
217# Static library
218if (NOT LIBCXX_ENABLE_SHARED)
219  list(APPEND LIBCXX_COMPILE_FLAGS -D_LIBCPP_BUILD_STATIC)
220endif()
221
222if (CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32)
223  if (LIBCXX_BUILD_32_BITS)
224    message(STATUS "Building 32 bits executables and libraries.")
225    list(APPEND LIBCXX_CXX_FLAGS "-m32")
226  endif()
227elseif(LIBCXX_BUILD_32_BITS)
228  message(FATAL_ERROR "LIBCXX_BUILD_32_BITS=ON is not supported on this platform.")
229endif()
230# This is the _ONLY_ place where add_definitions is called.
231if (MSVC)
232  add_definitions(-D_CRT_SECURE_NO_WARNINGS)
233endif()
234
235# LIBCXX_ENABLE_THREADS configuration
236if (NOT LIBCXX_ENABLE_THREADS)
237  add_definitions(-D_LIBCPP_HAS_NO_THREADS)
238  if (NOT LIBCXX_ENABLE_MONOTONIC_CLOCK)
239    add_definitions(-D_LIBCPP_HAS_NO_MONOTONIC_CLOCK)
240  endif()
241# Ensure LIBCXX_ENABLE_MONOTONIC_CLOCK is set to ON.
242elseif(NOT LIBCXX_ENABLE_MONOTONIC_CLOCK)
243  message(FATAL_ERROR "LIBCXX_ENABLE_MONOTONIC_CLOCK can only be set to OFF"
244                      " when LIBCXX_ENABLE_THREADS is also set to OFF.")
245endif()
246
247# Configure for sanitizers. If LIBCXX_BUILT_STANDALONE then we have to do
248# the flag translation ourselves. Othewise LLVM's CMakeList.txt will handle it.
249if (LIBCXX_BUILT_STANDALONE)
250  # NOTE: LLVM_USE_SANITIZER checks for a UNIX like system instead of MSVC.
251  # But we don't have LLVM_ON_UNIX so checking for MSVC is the best we can do.
252  if (LLVM_USE_SANITIZER AND NOT MSVC)
253    append_if(LIBCXX_CXX_FLAGS LIBCXX_HAS_FNO_OMIT_FRAME_POINTER_FLAG
254              "-fno-omit-frame-pointer")
255    if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" AND
256        NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINFO")
257      append_if(LIBCXX_CXX_FLAGS LIBCXX_HAS_GLINE_TABLES_ONLY_FLAG
258                "-gline-tables-only")
259    endif()
260    if (LLVM_USE_SANITIZER STREQUAL "Address")
261      list(APPEND LIBCXX_CXX_FLAGS "-fsanitize=address")
262    elseif (LLVM_USE_SANITIZER MATCHES "Memory(WithOrigins)?")
263      list(APPEND LIBCXX_CXX_FLAGS "-fsanitize=memory")
264      if (LLVM_USE_SANITIZER STREQUAL "MemoryWithOrigins")
265        list(APPEND LIBCXX_CXX_FLAGS "-fsanitize-memory-track-origins")
266      endif()
267    elseif (LLVM_USE_SANITIZER STREQUAL "Undefined")
268      list(APPEND LIBCXX_CXX_FLAGS
269          "-fsanitize=undefined -fno-sanitize=vptr,function -fno-sanitize-recover")
270    elseif (LLVM_USE_SANITIZER STREQUAL "Thread")
271      list(APPEND LIBCXX_CXX_FLAGS "-fsanitize=thread")
272    else()
273      message(WARNING "Unsupported value of LLVM_USE_SANITIZER: ${LLVM_USE_SANITIZER}")
274    endif()
275  elseif(MSVC)
276    message(WARNING "LLVM_USE_SANITIZER is not supported with MSVC")
277  endif()
278endif()
279
280append_if(LIBCXX_CXX_FLAGS LIBCXX_TARGET_TRIPLE
281          "-target ${LIBCXX_TARGET_TRIPLE}")
282append_if(LIBCXX_CXX_FLAGS LIBCXX_SYSROOT "--sysroot ${LIBCXX_SYSROOT}")
283append_if(LIBCXX_CXX_FLAGS LIBCXX_GCC_TOOLCHAIN
284          "-gcc-toolchain ${LIBCXX_GCC_TOOLCHAIN}")
285
286string(REPLACE ";" " " LIBCXX_CXX_FLAGS "${LIBCXX_CXX_FLAGS}")
287set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${LIBCXX_CXX_FLAGS}")
288
289#===============================================================================
290# Setup Source Code
291#===============================================================================
292
293include_directories(include)
294add_subdirectory(include)
295
296# Add source code. This also contains all of the logic for deciding linker flags
297# soname, etc...
298add_subdirectory(lib)
299
300#===============================================================================
301# Setup Tests
302#===============================================================================
303
304add_subdirectory(test)
305