blob: f12c5257409d5e1d5e8cdb03c971e8e19c549c03 [file] [log] [blame]
Michael J. Spencer626916f2010-12-10 19:47:54 +00001# 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
10set(PACKAGE_NAME libcxx)
11set(PACKAGE_VERSION trunk-svn)
12set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}")
13set(PACKAGE_BUGREPORT "llvmbugs@cs.uiuc.edu")
14
15# Add path for custom modules
16set(CMAKE_MODULE_PATH
Michael J. Spencer626916f2010-12-10 19:47:54 +000017 "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
18 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
Alexey Samsonov179fa782013-09-30 09:10:01 +000019 ${CMAKE_MODULE_PATH}
Michael J. Spencer626916f2010-12-10 19:47:54 +000020 )
21
22# Require out of source build.
23include(MacroEnsureOutOfSourceBuild)
24MACRO_ENSURE_OUT_OF_SOURCE_BUILD(
25 "${PROJECT_NAME} requires an out of source build. Please create a separate
26 build directory and run 'cmake /path/to/${PROJECT_NAME} [options]' there."
27 )
28
Peter Collingbourne4c81b002013-10-03 21:58:25 +000029if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
30 set(LIBCXX_BUILT_STANDALONE 1)
31endif()
32
Michael J. Spencer626916f2010-12-10 19:47:54 +000033#===============================================================================
34# Setup CMake Options
35#===============================================================================
36
37# Define options.
38option(LIBCXX_ENABLE_EXCEPTIONS "Use exceptions." ON)
39option(LIBCXX_ENABLE_RTTI "Use run time type information." ON)
40option(LIBCXX_ENABLE_ASSERTIONS "Enable assertions independent of build mode." ON)
41option(LIBCXX_ENABLE_PEDANTIC "Compile with pedantic enabled." ON)
42option(LIBCXX_ENABLE_WERROR "Fail and stop if a warning is triggered." OFF)
Dan Albert90dc8dd2014-08-26 11:20:46 -070043option(LIBCXX_ENABLE_CXX1Y "Enable -std=c++1y and use of c++1y language features if the compiler supports it." OFF)
Michael J. Spencer626916f2010-12-10 19:47:54 +000044option(LIBCXX_ENABLE_SHARED "Build libc++ as a shared library." ON)
Michael Gottesmanc64c9802013-09-02 07:28:05 +000045option(LIBCXX_INSTALL_SUPPORT_HEADERS "Install libc++ support headers." ON)
Dan Albert90dc8dd2014-08-26 11:20:46 -070046if (LIBCXX_BUILT_STANDALONE)
47 set(LLVM_USE_SANITIZER "" CACHE STRING
48 "Define the sanitizer used to build the library and tests")
49endif()
Michael J. Spencer626916f2010-12-10 19:47:54 +000050
Peter Collingbourned0d308f2013-10-06 22:13:19 +000051set(CXXABIS none libcxxabi libcxxrt libstdc++ libsupc++)
Dan Albert90dc8dd2014-08-26 11:20:46 -070052if (NOT LIBCXX_CXX_ABI)
53 if (NOT DEFINED LIBCXX_BUILT_STANDALONE AND
54 IS_DIRECTORY "${CMAKE_SOURCE_DIR}/projects/libcxxabi")
55 set(LIBCXX_CXX_ABI_LIBNAME "libcxxabi")
56 set(LIBCXX_LIBCXXABI_INCLUDE_PATHS "${CMAKE_SOURCE_DIR}/projects/libcxxabi/include")
57 set(LIBCXX_CXX_ABI_INTREE 1)
58 else ()
59 set(LIBCXX_CXX_ABI_LIBNAME "none")
60 endif ()
61else ()
62 set(LIBCXX_CXX_ABI_LIBNAME "${LIBCXX_CXX_ABI}")
63endif ()
Michael J. Spencera358fbe2012-11-30 21:02:29 +000064set(LIBCXX_CXX_ABI "${LIBCXX_CXX_ABI}" CACHE STRING
65 "Specify C++ ABI library to use." FORCE)
Howard Hinnant9c07b142013-08-19 21:42:07 +000066set_property(CACHE LIBCXX_CXX_ABI PROPERTY STRINGS ;${CXXABIS})
Michael J. Spencera358fbe2012-11-30 21:02:29 +000067
Michael J. Spencer626916f2010-12-10 19:47:54 +000068#===============================================================================
69# Configure System
70#===============================================================================
71
72# Get triples.
73include(GetTriple)
74get_host_triple(LIBCXX_HOST_TRIPLE
75 LIBCXX_HOST_ARCH
76 LIBCXX_HOST_VENDOR
77 LIBCXX_HOST_OS
78 )
79set(LIBCXX_HOST_TRIPLE ${LIBCXX_HOST_TRIPLE} CACHE STRING "Host triple.")
80get_target_triple(LIBCXX_TARGET_TRIPLE
81 LIBCXX_TARGET_ARCH
82 LIBCXX_TARGET_VENDOR
83 LIBCXX_TARGET_OS
84 )
85set(LIBCXX_TARGET_TRIPLE ${LIBCXX_TARGET_TRIPLE} CACHE STRING "Target triple.")
86
Howard Hinnantb85dea32013-02-08 19:04:53 +000087
Dan Albert7112dae2014-11-20 15:55:17 -080088# Declare libc++ configuration variables.
89# They are intended for use as follows:
90# LIBCXX_CXX_FLAGS: General flags for both the compiler and linker.
91# LIBCXX_COMPILE_FLAGS: Compile only flags.
92# LIBCXX_LINK_FLAGS: Linker only flags.
93set(LIBCXX_CXX_FLAGS "")
94set(LIBCXX_COMPILE_FLAGS "")
95set(LIBCXX_LINK_FLAGS "")
Michael J. Spencera358fbe2012-11-30 21:02:29 +000096
Michael J. Spencer626916f2010-12-10 19:47:54 +000097# Configure compiler.
98include(config-ix)
Dan Albert7112dae2014-11-20 15:55:17 -080099# Configure ABI library
100include(HandleLibCXXABI)
Michael J. Spencer626916f2010-12-10 19:47:54 +0000101
102#===============================================================================
103# Setup Compiler Flags
104#===============================================================================
105
106# Get required flags.
107# On all systems the system c++ standard library headers need to be excluded.
108if (MSVC)
109 # MSVC only has -X, which disables all default includes; including the crt.
110 # Thus, we do nothing and hope we don't accidentally include any of the C++
111 # headers.
112else()
113 if (LIBCXX_HAS_NOSTDINCXX_FLAG)
Dan Albert7112dae2014-11-20 15:55:17 -0800114 list(APPEND LIBCXX_COMPILE_FLAGS -nostdinc++)
Dan Albert90dc8dd2014-08-26 11:20:46 -0700115 string(REPLACE "-stdlib=libc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
Dan Albert7112dae2014-11-20 15:55:17 -0800116 string(REPLACE "-stdlib=libstdc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
Michael J. Spencer626916f2010-12-10 19:47:54 +0000117 endif()
Dan Albert90dc8dd2014-08-26 11:20:46 -0700118 # If c++1y has been enabled then attempt to use it. Fail if it is no supported
119 # by the compiler. Otherwise choose c++11 and ensure the compiler supports it.
120 if (LIBCXX_ENABLE_CXX1Y)
121 if (LIBCXX_HAS_STDCXX1Y_FLAG)
122 set(LIBCXX_STD_VERSION c++1y)
123 else()
124 message(FATAL_ERROR "c++1y was enabled but the compiler does not support it.")
125 endif()
126 else()
127 if (LIBCXX_HAS_STDCXX11_FLAG)
128 set(LIBCXX_STD_VERSION c++11)
129 else()
130 message(FATAL_ERROR "c++11 is required by libc++ but is not supported by the compiler")
131 endif()
Michael J. Spencer626916f2010-12-10 19:47:54 +0000132 endif()
Dan Albert90dc8dd2014-08-26 11:20:46 -0700133 # LIBCXX_STD_VERSION should always be set at this point.
Dan Albert7112dae2014-11-20 15:55:17 -0800134 list(APPEND LIBCXX_CXX_FLAGS "-std=${LIBCXX_STD_VERSION}")
Michael J. Spencer626916f2010-12-10 19:47:54 +0000135endif()
136
137macro(append_if list condition var)
138 if (${condition})
139 list(APPEND ${list} ${var})
140 endif()
141endmacro()
142
143# Get warning flags
Howard Hinnantadb73b12013-10-05 00:07:35 +0000144if (NOT MSVC)
Dan Albert7112dae2014-11-20 15:55:17 -0800145 append_if(LIBCXX_COMPILE_FLAGS LIBCXX_HAS_WALL_FLAG -Wall)
146 list(APPEND LIBCXX_COMPILE_FLAGS -Werror=return-type)
Howard Hinnantadb73b12013-10-05 00:07:35 +0000147endif()
Marshall Clow0f7afe72013-10-21 15:56:35 +0000148
Dan Albert7112dae2014-11-20 15:55:17 -0800149append_if(LIBCXX_COMPILE_FLAGS LIBCXX_HAS_W_FLAG -W)
150append_if(LIBCXX_COMPILE_FLAGS LIBCXX_HAS_WNO_UNUSED_PARAMETER_FLAG -Wno-unused-parameter)
151append_if(LIBCXX_COMPILE_FLAGS LIBCXX_HAS_WWRITE_STRINGS_FLAG -Wwrite-strings)
152append_if(LIBCXX_COMPILE_FLAGS LIBCXX_HAS_WNO_LONG_LONG_FLAG -Wno-long-long)
Michael J. Spencer626916f2010-12-10 19:47:54 +0000153if (LIBCXX_ENABLE_WERROR)
Dan Albert7112dae2014-11-20 15:55:17 -0800154 append_if(LIBCXX_COMPILE_FLAGS LIBCXX_HAS_WERROR_FLAG -Werror)
155 append_if(LIBCXX_COMPILE_FLAGS LIBCXX_HAS_WX_FLAG -WX)
Alexey Samsonovf7eb5732013-10-02 07:44:19 +0000156else()
Dan Albert7112dae2014-11-20 15:55:17 -0800157 append_if(LIBCXX_COMPILE_FLAGS LIBCXX_HAS_WNO_ERROR_FLAG -Wno-error)
158 append_if(LIBCXX_COMPILE_FLAGS LIBCXX_HAS_NO_WX_FLAG -WX-)
Michael J. Spencer626916f2010-12-10 19:47:54 +0000159endif()
160if (LIBCXX_ENABLE_PEDANTIC)
Dan Albert7112dae2014-11-20 15:55:17 -0800161 append_if(LIBCXX_COMPILE_FLAGS LIBCXX_HAS_PEDANTIC_FLAG -pedantic)
Michael J. Spencer626916f2010-12-10 19:47:54 +0000162endif()
163
164# Get feature flags.
165# Exceptions
166if (LIBCXX_ENABLE_EXCEPTIONS)
167 # Catches C++ exceptions only and tells the compiler to assume that extern C
168 # functions never throw a C++ exception.
Dan Albert7112dae2014-11-20 15:55:17 -0800169 append_if(LIBCXX_CXX_FLAGS LIBCXX_HAS_EHSC_FLAG -EHsc)
Michael J. Spencer626916f2010-12-10 19:47:54 +0000170else()
Dan Albert7112dae2014-11-20 15:55:17 -0800171 list(APPEND LIBCXX_CXX_FLAGS -D_LIBCPP_NO_EXCEPTIONS)
172 append_if(LIBCXX_CXX_FLAGS LIBCXX_HAS_NO_EHS_FLAG -EHs-)
173 append_if(LIBCXX_CXX_FLAGS LIBCXX_HAS_NO_EHA_FLAG -EHa-)
174 append_if(LIBCXX_CXX_FLAGS LIBCXX_HAS_FNO_EXCEPTIONS_FLAG -fno-exceptions)
Michael J. Spencer626916f2010-12-10 19:47:54 +0000175endif()
176# RTTI
177if (NOT LIBCXX_ENABLE_RTTI)
Dan Albert7112dae2014-11-20 15:55:17 -0800178 list(APPEND LIBCXX_CXX_FLAGS -D_LIBCPP_NO_RTTI)
179 append_if(LIBCXX_CXX_FLAGS LIBCXX_HAS_NO_GR_FLAG -GR-)
180 append_if(LIBCXX_CXX_FLAGS LIBCXX_HAS_FNO_RTTI_FLAG -fno-rtti)
Michael J. Spencer626916f2010-12-10 19:47:54 +0000181endif()
182# Assert
Howard Hinnante103a3d2012-08-05 17:37:39 +0000183string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
184if (LIBCXX_ENABLE_ASSERTIONS)
Michael J. Spencer626916f2010-12-10 19:47:54 +0000185 # MSVC doesn't like _DEBUG on release builds. See PR 4379.
186 if (NOT MSVC)
Dan Albert7112dae2014-11-20 15:55:17 -0800187 list(APPEND LIBCXX_COMPILE_FLAGS -D_DEBUG)
Michael J. Spencer626916f2010-12-10 19:47:54 +0000188 endif()
189 # On Release builds cmake automatically defines NDEBUG, so we
190 # explicitly undefine it:
191 if (uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE")
Dan Albert7112dae2014-11-20 15:55:17 -0800192 list(APPEND LIBCXX_COMPILE_FLAGS -UNDEBUG)
Michael J. Spencer626916f2010-12-10 19:47:54 +0000193 endif()
194else()
195 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE")
Dan Albert7112dae2014-11-20 15:55:17 -0800196 list(APPEND LIBCXX_COMPILE_FLAGS -DNDEBUG)
Michael J. Spencer626916f2010-12-10 19:47:54 +0000197 endif()
198endif()
Howard Hinnant4a0e74f2013-02-25 15:50:36 +0000199# Static library
200if (NOT LIBCXX_ENABLE_SHARED)
Dan Albert7112dae2014-11-20 15:55:17 -0800201 list(APPEND LIBCXX_COMPILE_FLAGS -D_LIBCPP_BUILD_STATIC)
Howard Hinnant4a0e74f2013-02-25 15:50:36 +0000202endif()
Michael J. Spencer626916f2010-12-10 19:47:54 +0000203
204# This is the _ONLY_ place where add_definitions is called.
Marshall Clow0f7afe72013-10-21 15:56:35 +0000205if (MSVC)
206 add_definitions(-D_CRT_SECURE_NO_WARNINGS)
207endif()
208
Dan Albert90dc8dd2014-08-26 11:20:46 -0700209# Configure for sanitizers. If LIBCXX_BUILT_STANDALONE then we have to do
210# the flag translation ourselves. Othewise LLVM's CMakeList.txt will handle it.
211if (LIBCXX_BUILT_STANDALONE)
212 # NOTE: LLVM_USE_SANITIZER checks for a UNIX like system instead of MSVC.
213 # But we don't have LLVM_ON_UNIX so checking for MSVC is the best we can do.
214 if (LLVM_USE_SANITIZER AND NOT MSVC)
Dan Albert7112dae2014-11-20 15:55:17 -0800215 append_if(LIBCXX_CXX_FLAGS LIBCXX_HAS_FNO_OMIT_FRAME_POINTER_FLAG
216 "-fno-omit-frame-pointer")
217 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" AND
218 NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINFO")
219 append_if(LIBCXX_CXX_FLAGS LIBCXX_HAS_GLINE_TABLES_ONLY_FLAG
220 "-gline-tables-only")
221 endif()
Dan Albert90dc8dd2014-08-26 11:20:46 -0700222 if (LLVM_USE_SANITIZER STREQUAL "Address")
Dan Albert7112dae2014-11-20 15:55:17 -0800223 list(APPEND LIBCXX_CXX_FLAGS "-fsanitize=address")
Dan Albert90dc8dd2014-08-26 11:20:46 -0700224 elseif (LLVM_USE_SANITIZER MATCHES "Memory(WithOrigins)?")
Dan Albert7112dae2014-11-20 15:55:17 -0800225 list(APPEND LIBCXX_CXX_FLAGS "-fsanitize=memory")
Dan Albert90dc8dd2014-08-26 11:20:46 -0700226 if (LLVM_USE_SANITIZER STREQUAL "MemoryWithOrigins")
Dan Albert7112dae2014-11-20 15:55:17 -0800227 list(APPEND LIBCXX_CXX_FLAGS "-fsanitize-memory-track-origins")
Dan Albert90dc8dd2014-08-26 11:20:46 -0700228 endif()
Dan Albert7112dae2014-11-20 15:55:17 -0800229 elseif (LLVM_USE_SANITIZER STREQUAL "Undefined")
230 list(APPEND LIBCXX_CXX_FLAGS
231 "-fsanitize=undefined -fno-sanitize=vptr,function -fno-sanitize-recover")
232 elseif (LLVM_USE_SANITIZER STREQUAL "Thread")
233 list(APPEND LIBCXX_CXX_FLAGS "-fsanitize=thread")
Dan Albert90dc8dd2014-08-26 11:20:46 -0700234 else()
235 message(WARNING "Unsupported value of LLVM_USE_SANITIZER: ${LLVM_USE_SANITIZER}")
236 endif()
237 elseif(MSVC)
238 message(WARNING "LLVM_USE_SANITIZER is not supported with MSVC")
239 endif()
240endif()
241
Dan Albert7112dae2014-11-20 15:55:17 -0800242string(REPLACE ";" " " LIBCXX_CXX_FLAGS "${LIBCXX_CXX_FLAGS}")
243set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${LIBCXX_CXX_FLAGS}")
Michael J. Spencer626916f2010-12-10 19:47:54 +0000244
245#===============================================================================
246# Setup Source Code
247#===============================================================================
248
249include_directories(include)
Howard Hinnantc4962b32013-11-15 17:18:57 +0000250add_subdirectory(include)
Michael J. Spencer626916f2010-12-10 19:47:54 +0000251
252# Add source code. This also contains all of the logic for deciding linker flags
253# soname, etc...
254add_subdirectory(lib)
255
256#===============================================================================
257# Setup Tests
258#===============================================================================
259
260add_subdirectory(test)