1#===============================================================================
2# Setup Project
3#===============================================================================
4
5cmake_minimum_required(VERSION 2.8.8)
6
7if(POLICY CMP0042)
8  cmake_policy(SET CMP0042 NEW) # Set MACOSX_RPATH=YES by default
9endif()
10
11if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
12  project(libcxxabi)
13
14  # Rely on llvm-config.
15  set(CONFIG_OUTPUT)
16  find_program(LLVM_CONFIG "llvm-config")
17  if(DEFINED LLVM_PATH)
18    set(LLVM_INCLUDE_DIR ${LLVM_INCLUDE_DIR} CACHE PATH "Path to llvm/include")
19    set(LLVM_PATH ${LLVM_PATH} CACHE PATH "Path to LLVM source tree")
20    set(LLVM_MAIN_SRC_DIR ${LLVM_PATH})
21    set(LLVM_CMAKE_PATH "${LLVM_PATH}/cmake/modules")
22  elseif(LLVM_CONFIG)
23    message(STATUS "Found LLVM_CONFIG as ${LLVM_CONFIG}")
24    set(CONFIG_COMMAND ${LLVM_CONFIG}
25      "--includedir"
26      "--prefix"
27      "--src-root")
28    execute_process(
29      COMMAND ${CONFIG_COMMAND}
30      RESULT_VARIABLE HAD_ERROR
31      OUTPUT_VARIABLE CONFIG_OUTPUT
32    )
33    if(NOT HAD_ERROR)
34      string(REGEX REPLACE
35        "[ \t]*[\r\n]+[ \t]*" ";"
36        CONFIG_OUTPUT ${CONFIG_OUTPUT})
37    else()
38      string(REPLACE ";" " " CONFIG_COMMAND_STR "${CONFIG_COMMAND}")
39      message(STATUS "${CONFIG_COMMAND_STR}")
40      message(FATAL_ERROR "llvm-config failed with status ${HAD_ERROR}")
41    endif()
42
43    list(GET CONFIG_OUTPUT 0 INCLUDE_DIR)
44    list(GET CONFIG_OUTPUT 1 LLVM_OBJ_ROOT)
45    list(GET CONFIG_OUTPUT 2 MAIN_SRC_DIR)
46
47    set(LLVM_INCLUDE_DIR ${INCLUDE_DIR} CACHE PATH "Path to llvm/include")
48    set(LLVM_BINARY_DIR ${LLVM_OBJ_ROOT} CACHE PATH "Path to LLVM build tree")
49    set(LLVM_MAIN_SRC_DIR ${MAIN_SRC_DIR} CACHE PATH "Path to LLVM source tree")
50    set(LLVM_CMAKE_PATH "${LLVM_BINARY_DIR}/share/llvm/cmake")
51    set(LLVM_LIT_PATH "${LLVM_PATH}/utils/lit/lit.py")
52  else()
53    message(FATAL_ERROR "llvm-config not found and LLVM_MAIN_SRC_DIR not defined. "
54                        "Reconfigure with -DLLVM_CONFIG=path/to/llvm-config "
55                        "or -DLLVM_PATH=path/to/llvm-source-root.")
56  endif()
57
58  if(EXISTS ${LLVM_CMAKE_PATH})
59    list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_PATH}")
60    include("${LLVM_CMAKE_PATH}/AddLLVM.cmake")
61    include("${LLVM_CMAKE_PATH}/HandleLLVMOptions.cmake")
62  else()
63    message(FATAL_ERROR "Not found: ${LLVM_CMAKE_PATH}")
64  endif()
65
66  set(PACKAGE_NAME libcxxabi)
67  set(PACKAGE_VERSION 3.7.0svn)
68  set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}")
69  set(PACKAGE_BUGREPORT "llvm-bugs@lists.llvm.org")
70
71  if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
72    set(LLVM_LIT ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
73  else()
74    # Seek installed Lit.
75    find_program(LLVM_LIT "lit.py" ${LLVM_MAIN_SRC_DIR}/utils/lit
76      DOC "Path to lit.py")
77  endif()
78
79  if(LLVM_LIT)
80    # Define the default arguments to use with 'lit', and an option for the user
81    # to override.
82    set(LIT_ARGS_DEFAULT "-sv")
83    if (MSVC OR XCODE)
84      set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
85    endif()
86    set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
87
88    # On Win32 hosts, provide an option to specify the path to the GnuWin32 tools.
89    if( WIN32 AND NOT CYGWIN )
90      set(LLVM_LIT_TOOLS_DIR "" CACHE PATH "Path to GnuWin32 tools")
91    endif()
92  else()
93    set(LLVM_INCLUDE_TESTS OFF)
94  endif()
95
96  set(LIBCXXABI_LIBDIR_SUFFIX "" CACHE STRING
97      "Define suffix of library directory name (32/64)")
98
99  set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LIBCXXABI_LIBDIR_SUFFIX})
100  set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LIBCXXABI_LIBDIR_SUFFIX})
101
102  set(LIBCXXABI_BUILT_STANDALONE 1)
103else()
104  set(LLVM_MAIN_SRC_DIR "${CMAKE_SOURCE_DIR}" CACHE PATH "Path to LLVM source tree")
105  set(LLVM_LIT "${CMAKE_SOURCE_DIR}/utils/lit/lit.py")
106  set(LIBCXXABI_LIBDIR_SUFFIX ${LLVM_LIBDIR_SUFFIX})
107endif()
108
109#===============================================================================
110# Setup CMake Options
111#===============================================================================
112
113# Define options.
114option(LIBCXXABI_ENABLE_ASSERTIONS "Enable assertions independent of build mode." ON)
115option(LIBCXXABI_ENABLE_PEDANTIC "Compile with pedantic enabled." ON)
116option(LIBCXXABI_ENABLE_WERROR "Fail and stop if a warning is triggered." OFF)
117option(LIBCXXABI_USE_LLVM_UNWINDER "Build and use the LLVM unwinder." OFF)
118option(LIBCXXABI_ENABLE_THREADS "Build with threads enabled" ON)
119set(LIBCXXABI_GCC_TOOLCHAIN "" CACHE STRING "GCC toolchain for cross compiling.")
120set(LIBCXXABI_SYSROOT "" CACHE STRING "Sysroot for cross compiling.")
121set(LIBCXXABI_LIBCXX_LIBRARY_PATH "" CACHE STRING "The path to libc++ library.")
122
123# Default to building a shared library so that the default options still test
124# the libc++abi that is being built. There are two problems with testing a
125# static libc++abi. In the case of a standalone build, the tests will link the
126# system's libc++, which might not have been built against our libc++abi. In the
127# case of an in tree build, libc++ will prefer a dynamic libc++abi from the
128# system over a static libc++abi from the output directory.
129option(LIBCXXABI_ENABLE_SHARED "Build libc++abi as a shared library." ON)
130option(LIBCXXABI_ENABLE_STATIC "Build libc++abi as a static library." ON)
131
132if (NOT LIBCXXABI_ENABLE_SHARED AND NOT LIBCXXABI_ENABLE_STATIC)
133  message(FATAL_ERROR "libc++abi must be built as either a shared or static library.")
134endif()
135
136find_path(
137  LIBCXXABI_LIBCXX_INCLUDES
138  vector
139  PATHS ${LIBCXXABI_LIBCXX_INCLUDES}
140        ${LIBCXXABI_LIBCXX_PATH}/include
141        ${CMAKE_BINARY_DIR}/${LIBCXXABI_LIBCXX_INCLUDES}
142        ${LLVM_MAIN_SRC_DIR}/projects/libcxx/include
143        ${LLVM_INCLUDE_DIR}/c++/v1
144  )
145
146set(LIBCXXABI_LIBCXX_INCLUDES "${LIBCXXABI_LIBCXX_INCLUDES}" CACHE PATH
147    "Specify path to libc++ includes." FORCE)
148
149find_path(
150  LIBCXXABI_LIBCXX_PATH
151  test/libcxx/__init__.py
152  PATHS ${LIBCXXABI_LIBCXX_PATH}
153        ${LIBCXXABI_LIBCXX_INCLUDES}/../
154        ${LLVM_MAIN_SRC_DIR}/projects/libcxx/
155  NO_DEFAULT_PATH
156  )
157
158if (LIBCXXABI_LIBCXX_PATH STREQUAL "LIBCXXABI_LIBCXX_PATH-NOTFOUND")
159  message(WARNING "LIBCXXABI_LIBCXX_PATH was not specified and couldn't be infered.")
160  set(LIBCXXABI_LIBCXX_PATH "")
161endif()
162
163set(LIBCXXABI_LIBCXX_PATH "${LIBCXXABI_LIBCXX_PATH}" CACHE PATH
164    "Specify path to libc++ source." FORCE)
165
166#===============================================================================
167# Configure System
168#===============================================================================
169
170# Add path for custom modules
171set(CMAKE_MODULE_PATH
172  "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
173  "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
174  ${CMAKE_MODULE_PATH}
175  )
176
177# Configure compiler.
178include(config-ix)
179
180set(LIBCXXABI_COMPILER    ${CMAKE_CXX_COMPILER})
181set(LIBCXXABI_SOURCE_DIR  ${CMAKE_CURRENT_SOURCE_DIR})
182set(LIBCXXABI_BINARY_DIR  ${CMAKE_CURRENT_BINARY_DIR})
183set(LIBCXXABI_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LIBCXXABI_LIBDIR_SUFFIX})
184
185#===============================================================================
186# Setup Compiler Flags
187#===============================================================================
188
189# Get required flags.
190macro(append_if list condition var)
191  if (${condition})
192    list(APPEND ${list} ${var})
193  endif()
194endmacro()
195
196set(LIBCXXABI_C_FLAGS "")
197set(LIBCXXABI_CXX_FLAGS "")
198set(LIBCXXABI_COMPILE_FLAGS "")
199set(LIBCXXABI_LINK_FLAGS "")
200
201
202if (LIBCXXABI_HAS_NOSTDINCXX_FLAG)
203  list(APPEND LIBCXXABI_COMPILE_FLAGS -nostdinc++)
204  # Remove -stdlib flags to prevent them from causing an unused flag warning.
205  string(REPLACE "-stdlib=libc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
206  string(REPLACE "-stdlib=libstdc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
207endif()
208
209
210append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WERROR_FLAG -Werror=return-type)
211
212# Get warning flags
213append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_W_FLAG -W)
214append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WALL_FLAG -Wall)
215append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WCHAR_SUBSCRIPTS_FLAG -Wchar-subscripts)
216append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WCONVERSION_FLAG -Wconversion)
217append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WMISMATCHED_TAGS_FLAG -Wmismatched-tags)
218append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WMISSING_BRACES_FLAG -Wmissing-braces)
219append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WNEWLINE_EOF_FLAG -Wnewline-eof)
220append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WNO_UNUSED_FUNCTION_FLAG -Wno-unused-function)
221append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WSHADOW_FLAG -Wshadow)
222append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WSHORTEN_64_TO_32_FLAG -Wshorten-64-to-32)
223append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WSIGN_COMPARE_FLAG -Wsign-compare)
224append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WSIGN_CONVERSION_FLAG -Wsign-conversion)
225append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WSTRICT_ALIASING_FLAG -Wstrict-aliasing=2)
226append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WSTRICT_OVERFLOW_FLAG -Wstrict-overflow=4)
227append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WUNUSED_PARAMETER_FLAG -Wunused-parameter)
228append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WUNUSED_VARIABLE_FLAG -Wunused-variable)
229append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WWRITE_STRINGS_FLAG -Wwrite-strings)
230append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WUNDEF_FLAG -Wundef)
231
232if (LIBCXXABI_ENABLE_WERROR)
233  append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WERROR_FLAG -Werror)
234  append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WX_FLAG -WX)
235else()
236  append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WNO_ERROR_FLAG -Wno-error)
237  append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_NO_WX_FLAG -WX-)
238endif()
239if (LIBCXXABI_ENABLE_PEDANTIC)
240  append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_PEDANTIC_FLAG -pedantic)
241endif()
242
243# Get feature flags.
244# Exceptions
245# Catches C++ exceptions only and tells the compiler to assume that extern C
246# functions never throw a C++ exception.
247append_if(LIBCXXABI_CXX_FLAGS LIBCXXABI_HAS_FSTRICT_ALIASING_FLAG -fstrict-aliasing)
248append_if(LIBCXXABI_CXX_FLAGS LIBCXXABI_HAS_EHSC_FLAG -EHsc)
249
250append_if(LIBCXXABI_C_FLAGS LIBCXXABI_HAS_FUNWIND_TABLES -funwind-tables)
251
252# Assert
253string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
254if (LIBCXXABI_ENABLE_ASSERTIONS)
255  # MSVC doesn't like _DEBUG on release builds. See PR 4379.
256  if (NOT MSVC)
257    list(APPEND LIBCXXABI_COMPILE_FLAGS -D_DEBUG)
258  endif()
259  # On Release builds cmake automatically defines NDEBUG, so we
260  # explicitly undefine it:
261  if (uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE")
262    list(APPEND LIBCXXABI_COMPILE_FLAGS -UNDEBUG)
263  endif()
264else()
265  if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE")
266    list(APPEND LIBCXXABI_COMPILE_FLAGS -DNDEBUG)
267  endif()
268endif()
269# Static library
270if (NOT LIBCXXABI_ENABLE_SHARED)
271  list(APPEND LIBCXXABI_COMPILE_FLAGS -D_LIBCPP_BUILD_STATIC)
272endif()
273
274if (NOT LIBCXXABI_ENABLE_THREADS)
275  add_definitions(-DLIBCXXABI_HAS_NO_THREADS=1)
276endif()
277
278# This is the _ONLY_ place where add_definitions is called.
279if (MSVC)
280  add_definitions(-D_CRT_SECURE_NO_WARNINGS)
281endif()
282
283# Define LIBCXXABI_USE_LLVM_UNWINDER for conditional compilation.
284if (LIBCXXABI_USE_LLVM_UNWINDER OR LLVM_NATIVE_ARCH MATCHES ARM)
285  add_definitions(-DLIBCXXABI_USE_LLVM_UNWINDER=1)
286endif()
287
288append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_TARGET_TRIPLE
289          "-target ${LIBCXXABI_TARGET_TRIPLE}")
290append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_GCC_TOOLCHAIN
291         "-gcc-toolchain ${LIBCXXABI_GCC_TOOLCHAIN}")
292append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_SYSROOT
293          "--sysroot ${LIBCXXABI_SYSROOT}")
294string(REPLACE ";" " " LIBCXXABI_CXX_FLAGS "${LIBCXXABI_CXX_FLAGS}")
295set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${LIBCXXABI_CXX_FLAGS}")
296set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${LIBCXXABI_C_FLAGS}")
297
298#===============================================================================
299# Setup Source Code
300#===============================================================================
301
302set(LIBCXXABI_LIBUNWIND_INCLUDES "${LIBCXXABI_LIBUNWIND_INCLUDES}" CACHE PATH
303    "Specify path to libunwind includes." FORCE)
304set(LIBCXXABI_LIBUNWIND_PATH "${LIBCXXABI_LIBUNWIND_PATH}" CACHE PATH
305    "Specify path to libunwind source." FORCE)
306
307include_directories(include)
308if (LIBCXXABI_USE_LLVM_UNWINDER OR LLVM_NATIVE_ARCH MATCHES ARM)
309  find_path(
310    LIBCXXABI_LIBUNWIND_INCLUDES_INTERNAL
311    libunwind.h
312    PATHS ${LIBCXXABI_LIBUNWIND_INCLUDES}
313          ${LIBCXXABI_LIBUNWIND_PATH}/include
314          ${CMAKE_BINARY_DIR}/${LIBCXXABI_LIBUNWIND_INCLUDES}
315          ${LLVM_MAIN_SRC_DIR}/projects/libunwind/include
316    NO_DEFAULT_PATH
317  )
318
319  find_path(
320    LIBCXXABI_LIBUNWIND_SOURCES
321    libunwind_ext.h
322    PATHS ${LIBCXXABI_LIBUNWIND_PATH}/src/
323          ${LIBCXXABI_LIBUNWIND_INCLUDES}/../src/
324          ${LLVM_MAIN_SRC_DIR}/projects/libunwind/src/
325    NO_DEFAULT_PATH
326  )
327
328  if (LIBCXXABI_LIBUNWIND_SOURCES STREQUAL "LIBCXXABI_LIBUNWIND_SOURCES-NOTFOUND")
329    message(WARNING "LIBCXXABI_LIBCXX_PATH was not specified and couldn't be infered.")
330    set(LIBCXXABI_LIBUNWIND_SOURCES "")
331  endif()
332
333  include_directories("${LIBCXXABI_LIBUNWIND_INCLUDES_INTERNAL}")
334  include_directories("${LIBCXXABI_LIBUNWIND_SOURCES}")
335endif ()
336
337# Add source code. This also contains all of the logic for deciding linker flags
338# soname, etc...
339add_subdirectory(src)
340
341if(NOT LIBCXXABI_ENABLE_SHARED)
342  # TODO: Fix the libc++ cmake files so that libc++abi can be statically linked.
343  # As it is now, libc++ will prefer linking against a dynamic libc++abi in the
344  # system library paths over a static libc++abi in the out directory. This
345  # would test the system library rather than the one we just built, which isn't
346  # very helpful.
347  message(WARNING "The libc++abi tests are currently only valid when "
348                  "LIBCXXABI_ENABLE_SHARED is on, no check target will be "
349                  "available!")
350else()
351  add_subdirectory(test)
352endif()
353