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 "llvmbugs@cs.uiuc.edu")
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.")
121
122# Default to building a shared library so that the default options still test
123# the libc++abi that is being built. There are two problems with testing a
124# static libc++abi. In the case of a standalone build, the tests will link the
125# system's libc++, which might not have been built against our libc++abi. In the
126# case of an in tree build, libc++ will prefer a dynamic libc++abi from the
127# system over a static libc++abi from the output directory.
128option(LIBCXXABI_ENABLE_SHARED "Build libc++abi as a shared library." ON)
129option(LIBCXXABI_ENABLE_STATIC "Build libc++abi as a static library." ON)
130
131if (NOT LIBCXXABI_ENABLE_SHARED AND NOT LIBCXXABI_ENABLE_STATIC)
132  message(FATAL_ERROR "libc++abi must be built as either a shared or static library.")
133endif()
134
135find_path(
136  LIBCXXABI_LIBCXX_INCLUDES
137  vector
138  PATHS ${LIBCXXABI_LIBCXX_INCLUDES}
139        ${LIBCXXABI_LIBCXX_PATH}/include
140        ${CMAKE_BINARY_DIR}/${LIBCXXABI_LIBCXX_INCLUDES}
141        ${LLVM_MAIN_SRC_DIR}/projects/libcxx/include
142        ${LLVM_INCLUDE_DIR}/c++/v1
143  )
144
145set(LIBCXXABI_LIBCXX_INCLUDES "${LIBCXXABI_LIBCXX_INCLUDES}" CACHE PATH
146    "Specify path to libc++ includes." FORCE)
147
148find_path(
149  LIBCXXABI_LIBCXX_PATH
150  test/libcxx/__init__.py
151  PATHS ${LIBCXXABI_LIBCXX_PATH}
152        ${LIBCXXABI_LIBCXX_INCLUDES}/../
153        ${LLVM_MAIN_SRC_DIR}/projects/libcxx/
154  NO_DEFAULT_PATH
155  )
156
157if (LIBCXXABI_LIBCXX_PATH STREQUAL "LIBCXXABI_LIBCXX_PATH-NOTFOUND")
158  message(WARNING "LIBCXXABI_LIBCXX_PATH was not specified and couldn't be infered.")
159  set(LIBCXXABI_LIBCXX_PATH "")
160endif()
161
162set(LIBCXXABI_LIBCXX_PATH "${LIBCXXABI_LIBCXX_PATH}" CACHE PATH
163    "Specify path to libc++ source." FORCE)
164
165#===============================================================================
166# Configure System
167#===============================================================================
168
169# Add path for custom modules
170set(CMAKE_MODULE_PATH
171  "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
172  "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
173  ${CMAKE_MODULE_PATH}
174  )
175
176# Configure compiler.
177include(config-ix)
178
179set(LIBCXXABI_COMPILER    ${CMAKE_CXX_COMPILER})
180set(LIBCXXABI_SOURCE_DIR  ${CMAKE_CURRENT_SOURCE_DIR})
181set(LIBCXXABI_BINARY_DIR  ${CMAKE_CURRENT_BINARY_DIR})
182set(LIBCXXABI_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LIBCXXABI_LIBDIR_SUFFIX})
183
184#===============================================================================
185# Setup Compiler Flags
186#===============================================================================
187
188# Get required flags.
189macro(append_if list condition var)
190  if (${condition})
191    list(APPEND ${list} ${var})
192  endif()
193endmacro()
194
195set(LIBCXXABI_C_FLAGS "")
196set(LIBCXXABI_CXX_FLAGS "")
197set(LIBCXXABI_COMPILE_FLAGS "")
198set(LIBCXXABI_LINK_FLAGS "")
199
200
201if (LIBCXXABI_HAS_NOSTDINCXX_FLAG)
202  list(APPEND LIBCXXABI_COMPILE_FLAGS -nostdinc++)
203  # Remove -stdlib flags to prevent them from causing an unused flag warning.
204  string(REPLACE "-stdlib=libc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
205  string(REPLACE "-stdlib=libstdc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
206endif()
207
208
209append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WERROR_FLAG -Werror=return-type)
210
211# Get warning flags
212append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_W_FLAG -W)
213append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WALL_FLAG -Wall)
214append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WCHAR_SUBSCRIPTS_FLAG -Wchar-subscripts)
215append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WCONVERSION_FLAG -Wconversion)
216append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WMISMATCHED_TAGS_FLAG -Wmismatched-tags)
217append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WMISSING_BRACES_FLAG -Wmissing-braces)
218append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WNEWLINE_EOF_FLAG -Wnewline-eof)
219append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WNO_UNUSED_FUNCTION_FLAG -Wno-unused-function)
220append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WSHADOW_FLAG -Wshadow)
221append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WSHORTEN_64_TO_32_FLAG -Wshorten-64-to-32)
222append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WSIGN_COMPARE_FLAG -Wsign-compare)
223append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WSIGN_CONVERSION_FLAG -Wsign-conversion)
224append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WSTRICT_ALIASING_FLAG -Wstrict-aliasing=2)
225append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WSTRICT_OVERFLOW_FLAG -Wstrict-overflow=4)
226append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WUNUSED_PARAMETER_FLAG -Wunused-parameter)
227append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WUNUSED_VARIABLE_FLAG -Wunused-variable)
228append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WWRITE_STRINGS_FLAG -Wwrite-strings)
229append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WUNDEF_FLAG -Wundef)
230
231if (LIBCXXABI_ENABLE_WERROR)
232  append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WERROR_FLAG -Werror)
233  append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WX_FLAG -WX)
234else()
235  append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WNO_ERROR_FLAG -Wno-error)
236  append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_NO_WX_FLAG -WX-)
237endif()
238if (LIBCXXABI_ENABLE_PEDANTIC)
239  append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_PEDANTIC_FLAG -pedantic)
240endif()
241
242# Get feature flags.
243# Exceptions
244# Catches C++ exceptions only and tells the compiler to assume that extern C
245# functions never throw a C++ exception.
246append_if(LIBCXXABI_CXX_FLAGS LIBCXXABI_HAS_FSTRICT_ALIASING_FLAG -fstrict-aliasing)
247append_if(LIBCXXABI_CXX_FLAGS LIBCXXABI_HAS_EHSC_FLAG -EHsc)
248
249append_if(LIBCXXABI_C_FLAGS LIBCXXABI_HAS_FUNWIND_TABLES -funwind-tables)
250
251# Assert
252string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
253if (LIBCXXABI_ENABLE_ASSERTIONS)
254  # MSVC doesn't like _DEBUG on release builds. See PR 4379.
255  if (NOT MSVC)
256    list(APPEND LIBCXXABI_COMPILE_FLAGS -D_DEBUG)
257  endif()
258  # On Release builds cmake automatically defines NDEBUG, so we
259  # explicitly undefine it:
260  if (uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE")
261    list(APPEND LIBCXXABI_COMPILE_FLAGS -UNDEBUG)
262  endif()
263else()
264  if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE")
265    list(APPEND LIBCXXABI_COMPILE_FLAGS -DNDEBUG)
266  endif()
267endif()
268# Static library
269if (NOT LIBCXXABI_ENABLE_SHARED)
270  list(APPEND LIBCXXABI_COMPILE_FLAGS -D_LIBCPP_BUILD_STATIC)
271endif()
272
273if (NOT LIBCXXABI_ENABLE_THREADS)
274  add_definitions(-DLIBCXXABI_HAS_NO_THREADS=1)
275endif()
276
277# This is the _ONLY_ place where add_definitions is called.
278if (MSVC)
279  add_definitions(-D_CRT_SECURE_NO_WARNINGS)
280endif()
281
282# Define LIBCXXABI_USE_LLVM_UNWINDER for conditional compilation.
283if (LIBCXXABI_USE_LLVM_UNWINDER)
284  add_definitions(-DLIBCXXABI_USE_LLVM_UNWINDER=1)
285endif()
286
287append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_TARGET_TRIPLE
288          "-target ${LIBCXXABI_TARGET_TRIPLE}")
289append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_GCC_TOOLCHAIN
290         "-gcc-toolchain ${LIBCXXABI_GCC_TOOLCHAIN}")
291append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_SYSROOT
292          "--sysroot ${LIBCXXABI_SYSROOT}")
293string(REPLACE ";" " " LIBCXXABI_CXX_FLAGS "${LIBCXXABI_CXX_FLAGS}")
294set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${LIBCXXABI_CXX_FLAGS}")
295set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${LIBCXXABI_C_FLAGS}")
296
297#===============================================================================
298# Setup Source Code
299#===============================================================================
300
301include_directories(include)
302
303# Add source code. This also contains all of the logic for deciding linker flags
304# soname, etc...
305add_subdirectory(src)
306
307if (LIBCXXABI_USE_LLVM_UNWINDER)
308  add_subdirectory(src/Unwind)
309endif()
310
311if(NOT LIBCXXABI_ENABLE_SHARED)
312  # TODO: Fix the libc++ cmake files so that libc++abi can be statically linked.
313  # As it is now, libc++ will prefer linking against a dynamic libc++abi in the
314  # system library paths over a static libc++abi in the out directory. This
315  # would test the system library rather than the one we just built, which isn't
316  # very helpful.
317  message(WARNING "The libc++abi tests are currently only valid when "
318                  "LIBCXXABI_ENABLE_SHARED is on, no check target will be "
319                  "available!")
320else()
321  add_subdirectory(test)
322endif()
323
324install(DIRECTORY include/
325  DESTINATION include
326  FILES_MATCHING
327    PATTERN "*.h"
328    PATTERN ".svn" EXCLUDE
329)
330