blob: 1345eeff2ae458579ba67212f460810acfd07aa4 [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)
43option(LIBCXX_ENABLE_CXX0X "Enable -std=c++0x and use of c++0x language features if the compiler supports it." ON)
44option(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)
Michael J. Spencer626916f2010-12-10 19:47:54 +000046
Peter Collingbourned0d308f2013-10-06 22:13:19 +000047set(CXXABIS none libcxxabi libcxxrt libstdc++ libsupc++)
Michael J. Spencerdb8a0302012-12-31 19:34:21 +000048if (NOT DEFINED LIBCXX_CXX_ABI)
Dan Albert0c6d1a82014-07-26 23:08:33 +000049 if (NOT DEFINED LIBCXX_BUILT_STANDALONE AND IS_DIRECTORY "${CMAKE_SOURCE_DIR}/projects/libcxxabi")
50 set(LIBCXX_CXX_ABI "libcxxabi")
51 set(LIBCXX_LIBCXXABI_INCLUDE_PATHS "${CMAKE_SOURCE_DIR}/projects/libcxxabi/include")
52 set(LIBCXX_CXX_ABI_INTREE 1)
53 else ()
54 set(LIBCXX_CXX_ABI "none")
55 endif ()
Michael J. Spencerdb8a0302012-12-31 19:34:21 +000056endif()
Michael J. Spencera358fbe2012-11-30 21:02:29 +000057set(LIBCXX_CXX_ABI "${LIBCXX_CXX_ABI}" CACHE STRING
58 "Specify C++ ABI library to use." FORCE)
Howard Hinnant9c07b142013-08-19 21:42:07 +000059set_property(CACHE LIBCXX_CXX_ABI PROPERTY STRINGS ;${CXXABIS})
Michael J. Spencera358fbe2012-11-30 21:02:29 +000060
Michael J. Spencer626916f2010-12-10 19:47:54 +000061#===============================================================================
62# Configure System
63#===============================================================================
64
65# Get triples.
66include(GetTriple)
67get_host_triple(LIBCXX_HOST_TRIPLE
68 LIBCXX_HOST_ARCH
69 LIBCXX_HOST_VENDOR
70 LIBCXX_HOST_OS
71 )
72set(LIBCXX_HOST_TRIPLE ${LIBCXX_HOST_TRIPLE} CACHE STRING "Host triple.")
73get_target_triple(LIBCXX_TARGET_TRIPLE
74 LIBCXX_TARGET_ARCH
75 LIBCXX_TARGET_VENDOR
76 LIBCXX_TARGET_OS
77 )
78set(LIBCXX_TARGET_TRIPLE ${LIBCXX_TARGET_TRIPLE} CACHE STRING "Target triple.")
79
Howard Hinnantb85dea32013-02-08 19:04:53 +000080#===============================================================================
81# Add an ABI library if appropriate
82#===============================================================================
83
84#
85# _setup_abi: Set up the build to use an ABI library
86#
87# Parameters:
88# abidefines: A list of defines needed to compile libc++ with the ABI library
89# abilibs : A list of libraries to link against
90# abifiles : A list of files (which may be relative paths) to copy into the
91# libc++ build tree for the build. These files will also be
92# installed alongside the libc++ headers.
93# abidirs : A list of relative paths to create under an include directory
94# in the libc++ build directory.
95#
96macro(setup_abi_lib abipathvar abidefines abilibs abifiles abidirs)
97 list(APPEND LIBCXX_CXX_FEATURE_FLAGS ${abidefines})
98 set(${abipathvar} "${${abipathvar}}"
David Fang44ead612014-06-24 20:32:11 +000099 CACHE PATH
100 "Paths to C++ ABI header directories separated by ';'." FORCE
Howard Hinnantb85dea32013-02-08 19:04:53 +0000101 )
102 set(LIBCXX_CXX_ABI_LIBRARIES ${abilibs})
103 set(LIBCXX_ABILIB_FILES ${abifiles})
David Fang44ead612014-06-24 20:32:11 +0000104
Michael J. Spencerdb8a0302012-12-31 19:34:21 +0000105 file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/include")
Howard Hinnantb85dea32013-02-08 19:04:53 +0000106 foreach(_d ${abidirs})
107 file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/include/${_d}")
108 endforeach()
David Fang44ead612014-06-24 20:32:11 +0000109
Howard Hinnantb85dea32013-02-08 19:04:53 +0000110 foreach(fpath ${LIBCXX_ABILIB_FILES})
Michael J. Spencera358fbe2012-11-30 21:02:29 +0000111 set(found FALSE)
Howard Hinnantb85dea32013-02-08 19:04:53 +0000112 foreach(incpath ${${abipathvar}})
113 if (EXISTS "${incpath}/${fpath}")
Michael J. Spencera358fbe2012-11-30 21:02:29 +0000114 set(found TRUE)
Howard Hinnantb85dea32013-02-08 19:04:53 +0000115 get_filename_component(dstdir ${fpath} PATH)
116 get_filename_component(ifile ${fpath} NAME)
David Fang44ead612014-06-24 20:32:11 +0000117 file(COPY "${incpath}/${fpath}"
118 DESTINATION "${CMAKE_BINARY_DIR}/include/${dstdir}"
Michael J. Spencera358fbe2012-11-30 21:02:29 +0000119 )
David Fang44ead612014-06-24 20:32:11 +0000120 list(APPEND abilib_headers "${CMAKE_BINARY_DIR}/include/${fpath}")
Michael J. Spencera358fbe2012-11-30 21:02:29 +0000121 endif()
122 endforeach()
123 if (NOT found)
Howard Hinnantb85dea32013-02-08 19:04:53 +0000124 message(FATAL_ERROR "Failed to find ${fpath}")
Michael J. Spencera358fbe2012-11-30 21:02:29 +0000125 endif()
126 endforeach()
David Fang44ead612014-06-24 20:32:11 +0000127
128 add_custom_target(LIBCXX_CXX_ABI_DEPS DEPENDS ${abilib_headers})
Michael J. Spencera358fbe2012-11-30 21:02:29 +0000129 include_directories("${CMAKE_BINARY_DIR}/include")
David Fang44ead612014-06-24 20:32:11 +0000130
131 install(FILES ${abilib_headers}
Michael J. Spencera358fbe2012-11-30 21:02:29 +0000132 DESTINATION include/c++/v1
David Fang44ead612014-06-24 20:32:11 +0000133 PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ
Michael J. Spencera358fbe2012-11-30 21:02:29 +0000134 )
Howard Hinnantb85dea32013-02-08 19:04:53 +0000135endmacro()
136
Peter Collingbourned0d308f2013-10-06 22:13:19 +0000137if ("${LIBCXX_CXX_ABI}" STREQUAL "libstdc++" OR
138 "${LIBCXX_CXX_ABI}" STREQUAL "libsupc++")
Howard Hinnantb85dea32013-02-08 19:04:53 +0000139 set(_LIBSUPCXX_INCLUDE_FILES
140 cxxabi.h bits/c++config.h bits/os_defines.h bits/cpu_defines.h
141 bits/cxxabi_tweaks.h bits/cxxabi_forced.h
142 )
Peter Collingbourned0d308f2013-10-06 22:13:19 +0000143 if ("${LIBCXX_CXX_ABI}" STREQUAL "libstdc++")
144 set(_LIBSUPCXX_DEFINES "-DLIBSTDCXX")
145 set(_LIBSUPCXX_LIBNAME stdc++)
146 else()
147 set(_LIBSUPCXX_DEFINES "")
148 set(_LIBSUPCXX_LIBNAME supc++)
149 endif()
150 setup_abi_lib("LIBCXX_LIBSUPCXX_INCLUDE_PATHS"
151 "-D__GLIBCXX__ ${_LIBSUPCXX_DEFINES}"
152 "${_LIBSUPCXX_LIBNAME}" "${_LIBSUPCXX_INCLUDE_FILES}" "bits"
Howard Hinnantb85dea32013-02-08 19:04:53 +0000153 )
Howard Hinnant46c49d12013-02-07 15:27:39 +0000154elseif ("${LIBCXX_CXX_ABI}" STREQUAL "libcxxabi")
Howard Hinnantb85dea32013-02-08 19:04:53 +0000155 setup_abi_lib("LIBCXX_LIBCXXABI_INCLUDE_PATHS" ""
Peter Collingbourne66a8fce2013-10-03 22:58:36 +0000156 "c++abi" "cxxabi.h" ""
Howard Hinnant46c49d12013-02-07 15:27:39 +0000157 )
Dan Albert0c6d1a82014-07-26 23:08:33 +0000158
159 if (LIBCXX_CXX_ABI_INTREE)
160 add_dependencies(LIBCXX_CXX_ABI_DEPS cxxabi)
161 endif ()
Howard Hinnant46c49d12013-02-07 15:27:39 +0000162elseif ("${LIBCXX_CXX_ABI}" STREQUAL "libcxxrt")
Howard Hinnantb85dea32013-02-08 19:04:53 +0000163 setup_abi_lib("LIBCXX_LIBCXXRT_INCLUDE_PATHS" "-DLIBCXXRT"
164 "cxxrt" "cxxabi.h;unwind.h;unwind-arm.h;unwind-itanium.h" ""
Howard Hinnant46c49d12013-02-07 15:27:39 +0000165 )
Michael J. Spencerdb8a0302012-12-31 19:34:21 +0000166elseif (NOT "${LIBCXX_CXX_ABI}" STREQUAL "none")
Michael J. Spencera358fbe2012-11-30 21:02:29 +0000167 message(FATAL_ERROR
Peter Collingbourned0d308f2013-10-06 22:13:19 +0000168 "Currently libstdc++, libsupc++, libcxxabi, libcxxrt and none are "
Howard Hinnantb85dea32013-02-08 19:04:53 +0000169 "supported for c++ abi."
170 )
Michael J. Spencera358fbe2012-11-30 21:02:29 +0000171endif ()
172
Michael J. Spencer626916f2010-12-10 19:47:54 +0000173# Configure compiler.
174include(config-ix)
175
176#===============================================================================
177# Setup Compiler Flags
178#===============================================================================
179
180# Get required flags.
181# On all systems the system c++ standard library headers need to be excluded.
182if (MSVC)
183 # MSVC only has -X, which disables all default includes; including the crt.
184 # Thus, we do nothing and hope we don't accidentally include any of the C++
185 # headers.
186else()
187 if (LIBCXX_HAS_NOSTDINCXX_FLAG)
Saleem Abdulrasooleb547812014-03-12 04:11:31 +0000188 list(APPEND LIBCXX_CXX_REQUIRED_FLAGS -nostdinc++)
David Fang44ead612014-06-24 20:32:11 +0000189 string(REPLACE "-stdlib=libc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
Michael J. Spencer626916f2010-12-10 19:47:54 +0000190 endif()
191 if (LIBCXX_ENABLE_CXX0X AND LIBCXX_HAS_STDCXX0X_FLAG)
192 list(APPEND LIBCXX_CXX_REQUIRED_FLAGS -std=c++0x)
193 endif()
194endif()
195
196macro(append_if list condition var)
197 if (${condition})
198 list(APPEND ${list} ${var})
199 endif()
200endmacro()
201
202# Get warning flags
Howard Hinnantadb73b12013-10-05 00:07:35 +0000203if (NOT MSVC)
204 append_if(LIBCXX_CXX_WARNING_FLAGS LIBCXX_HAS_WALL_FLAG -Wall)
Saleem Abdulrasooleb547812014-03-12 04:11:31 +0000205 list(APPEND LIBCXX_CXX_REQUIRED_FLAGS -Werror=return-type)
Howard Hinnantadb73b12013-10-05 00:07:35 +0000206endif()
Marshall Clow0f7afe72013-10-21 15:56:35 +0000207
Douglas Gregorf9b6e7e2012-06-07 22:26:00 +0000208append_if(LIBCXX_CXX_WARNING_FLAGS LIBCXX_HAS_W_FLAG -W)
209append_if(LIBCXX_CXX_WARNING_FLAGS LIBCXX_HAS_WNO_UNUSED_PARAMETER_FLAG -Wno-unused-parameter)
210append_if(LIBCXX_CXX_WARNING_FLAGS LIBCXX_HAS_WWRITE_STRINGS_FLAG -Wwrite-strings)
211append_if(LIBCXX_CXX_WARNING_FLAGS LIBCXX_HAS_WNO_LONG_LONG_FLAG -Wno-long-long)
Michael J. Spencer626916f2010-12-10 19:47:54 +0000212if (LIBCXX_ENABLE_WERROR)
Douglas Gregorf9b6e7e2012-06-07 22:26:00 +0000213 append_if(LIBCXX_CXX_WARNING_FLAGS LIBCXX_HAS_WERROR_FLAG -Werror)
214 append_if(LIBCXX_CXX_WARNING_FLAGS LIBCXX_HAS_WX_FLAG -WX)
Alexey Samsonovf7eb5732013-10-02 07:44:19 +0000215else()
216 append_if(LIBCXX_CXX_WARNING_FLAGS LIBCXX_HAS_WNO_ERROR_FLAG -Wno-error)
217 append_if(LIBCXX_CXX_WARNING_FLAGS LIBCXX_HAS_NO_WX_FLAG -WX-)
Michael J. Spencer626916f2010-12-10 19:47:54 +0000218endif()
219if (LIBCXX_ENABLE_PEDANTIC)
Douglas Gregorf9b6e7e2012-06-07 22:26:00 +0000220 append_if(LIBCXX_CXX_WARNING_FLAGS LIBCXX_HAS_PEDANTIC_FLAG -pedantic)
Michael J. Spencer626916f2010-12-10 19:47:54 +0000221endif()
222
223# Get feature flags.
224# Exceptions
225if (LIBCXX_ENABLE_EXCEPTIONS)
226 # Catches C++ exceptions only and tells the compiler to assume that extern C
227 # functions never throw a C++ exception.
228 append_if(LIBCXX_CXX_FEATURE_FLAGS LIBCXX_HAS_EHSC_FLAG -EHsc)
229else()
230 list(APPEND LIBCXX_CXX_FEATURE_FLAGS -D_LIBCPP_NO_EXCEPTIONS)
231 append_if(LIBCXX_CXX_FEATURE_FLAGS LIBCXX_HAS_NO_EHS_FLAG -EHs-)
232 append_if(LIBCXX_CXX_FEATURE_FLAGS LIBCXX_HAS_NO_EHA_FLAG -EHa-)
233 append_if(LIBCXX_CXX_FEATURE_FLAGS LIBCXX_HAS_FNO_EXCEPTIONS_FLAG -fno-exceptions)
234endif()
235# RTTI
236if (NOT LIBCXX_ENABLE_RTTI)
237 list(APPEND LIBCXX_CXX_FEATURE_FLAGS -D_LIBCPP_NO_RTTI)
238 append_if(LIBCXX_CXX_FEATURE_FLAGS LIBCXX_HAS_NO_GR_FLAG -GR-)
239 append_if(LIBCXX_CXX_FEATURE_FLAGS LIBCXX_HAS_FNO_RTTI_FLAG -fno-rtti)
240endif()
241# Assert
Howard Hinnante103a3d2012-08-05 17:37:39 +0000242string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
243if (LIBCXX_ENABLE_ASSERTIONS)
Michael J. Spencer626916f2010-12-10 19:47:54 +0000244 # MSVC doesn't like _DEBUG on release builds. See PR 4379.
245 if (NOT MSVC)
246 list(APPEND LIBCXX_CXX_FEATURE_FLAGS -D_DEBUG)
247 endif()
248 # On Release builds cmake automatically defines NDEBUG, so we
249 # explicitly undefine it:
250 if (uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE")
251 list(APPEND LIBCXX_CXX_FEATURE_FLAGS -UNDEBUG)
252 endif()
253else()
254 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE")
255 list(APPEND LIBCXX_CXX_FEATURE_FLAGS -DNDEBUG)
256 endif()
257endif()
Howard Hinnant4a0e74f2013-02-25 15:50:36 +0000258# Static library
259if (NOT LIBCXX_ENABLE_SHARED)
260 list(APPEND LIBCXX_CXX_FEATURE_FLAGS -D_LIBCPP_BUILD_STATIC)
261endif()
Michael J. Spencer626916f2010-12-10 19:47:54 +0000262
263# This is the _ONLY_ place where add_definitions is called.
Marshall Clow0f7afe72013-10-21 15:56:35 +0000264if (MSVC)
265 add_definitions(-D_CRT_SECURE_NO_WARNINGS)
266endif()
267
Saleem Abdulrasool89a52ff2014-03-12 04:11:28 +0000268string(REPLACE ";" " " LIBCXX_CXX_REQUIRED_FLAGS "${LIBCXX_CXX_REQUIRED_FLAGS}")
269set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${LIBCXX_CXX_REQUIRED_FLAGS}")
270
271string(REPLACE ";" " " LIBCXX_CXX_WARNING_FLAGS "${LIBCXX_CXX_WARNING_FLAGS}")
272set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${LIBCXX_CXX_WARNING_FLAGS}")
273
274string(REPLACE ";" " " LIBCXX_CXX_FEATURE_FLAGS "${LIBCXX_CXX_FEATURE_FLAGS}")
275set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${LIBCXX_CXX_FEATURE_FLAGS}")
Michael J. Spencer626916f2010-12-10 19:47:54 +0000276
277#===============================================================================
278# Setup Source Code
279#===============================================================================
280
281include_directories(include)
Howard Hinnantc4962b32013-11-15 17:18:57 +0000282add_subdirectory(include)
Michael J. Spencer626916f2010-12-10 19:47:54 +0000283
284# Add source code. This also contains all of the logic for deciding linker flags
285# soname, etc...
286add_subdirectory(lib)
287
288#===============================================================================
289# Setup Tests
290#===============================================================================
291
292add_subdirectory(test)