• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#===============================================================================
2# Setup Project
3#===============================================================================
4
5cmake_minimum_required(VERSION 3.4.3)
6
7if (POLICY CMP0042)
8  cmake_policy(SET CMP0042 NEW) # Set MACOSX_RPATH=YES by default
9endif()
10
11# Add path for custom modules
12set(CMAKE_MODULE_PATH
13  "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
14  "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
15  ${CMAKE_MODULE_PATH}
16  )
17
18if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
19  project(libunwind)
20
21  # Rely on llvm-config.
22  set(CONFIG_OUTPUT)
23  if(NOT LLVM_CONFIG_PATH)
24    find_program(LLVM_CONFIG_PATH "llvm-config")
25  endif()
26  if (DEFINED LLVM_PATH)
27    set(LLVM_INCLUDE_DIR ${LLVM_INCLUDE_DIR} CACHE PATH "Path to llvm/include")
28    set(LLVM_PATH ${LLVM_PATH} CACHE PATH "Path to LLVM source tree")
29    set(LLVM_MAIN_SRC_DIR ${LLVM_PATH})
30    set(LLVM_CMAKE_PATH "${LLVM_PATH}/cmake/modules")
31  elseif(LLVM_CONFIG_PATH)
32    message(STATUS "Found LLVM_CONFIG_PATH as ${LLVM_CONFIG_PATH}")
33    set(CONFIG_COMMAND ${LLVM_CONFIG_PATH} "--includedir" "--prefix" "--src-root")
34    execute_process(COMMAND ${CONFIG_COMMAND}
35                    RESULT_VARIABLE HAD_ERROR
36                    OUTPUT_VARIABLE CONFIG_OUTPUT)
37    if (NOT HAD_ERROR)
38      string(REGEX REPLACE "[ \t]*[\r\n]+[ \t]*" ";"
39             CONFIG_OUTPUT ${CONFIG_OUTPUT})
40    else()
41      string(REPLACE ";" " " CONFIG_COMMAND_STR "${CONFIG_COMMAND}")
42      message(STATUS "${CONFIG_COMMAND_STR}")
43      message(FATAL_ERROR "llvm-config failed with status ${HAD_ERROR}")
44    endif()
45
46    list(GET CONFIG_OUTPUT 0 INCLUDE_DIR)
47    list(GET CONFIG_OUTPUT 1 LLVM_OBJ_ROOT)
48    list(GET CONFIG_OUTPUT 2 MAIN_SRC_DIR)
49
50    set(LLVM_INCLUDE_DIR ${INCLUDE_DIR} CACHE PATH "Path to llvm/include")
51    set(LLVM_BINARY_DIR ${LLVM_OBJ_ROOT} CACHE PATH "Path to LLVM build tree")
52    set(LLVM_MAIN_SRC_DIR ${MAIN_SRC_DIR} CACHE PATH "Path to LLVM source tree")
53    set(LLVM_LIT_PATH "${LLVM_PATH}/utils/lit/lit.py")
54
55    # --cmakedir is supported since llvm r291218 (4.0 release)
56    execute_process(
57      COMMAND ${LLVM_CONFIG_PATH} --cmakedir
58      RESULT_VARIABLE HAD_ERROR
59      OUTPUT_VARIABLE CONFIG_OUTPUT
60      ERROR_QUIET)
61    if(NOT HAD_ERROR)
62      string(STRIP "${CONFIG_OUTPUT}" LLVM_CMAKE_PATH_FROM_LLVM_CONFIG)
63      file(TO_CMAKE_PATH "${LLVM_CMAKE_PATH_FROM_LLVM_CONFIG}" LLVM_CMAKE_PATH)
64    else()
65      file(TO_CMAKE_PATH "${LLVM_BINARY_DIR}" LLVM_BINARY_DIR_CMAKE_STYLE)
66      set(LLVM_CMAKE_PATH "${LLVM_BINARY_DIR_CMAKE_STYLE}/lib${LLVM_LIBDIR_SUFFIX}/cmake/llvm")
67    endif()
68  else()
69    message(WARNING "UNSUPPORTED LIBUNWIND CONFIGURATION DETECTED: "
70                    "llvm-config not found and LLVM_MAIN_SRC_DIR not defined. "
71                    "Reconfigure with -DLLVM_CONFIG=path/to/llvm-config "
72                    "or -DLLVM_PATH=path/to/llvm-source-root.")
73  endif()
74
75  if (EXISTS ${LLVM_CMAKE_PATH})
76    # Enable warnings, otherwise -w gets added to the cflags by HandleLLVMOptions.
77    set(LLVM_ENABLE_WARNINGS ON)
78    list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_PATH}")
79    include("${LLVM_CMAKE_PATH}/AddLLVM.cmake")
80    include("${LLVM_CMAKE_PATH}/HandleLLVMOptions.cmake")
81  else()
82    message(WARNING "Not found: ${LLVM_CMAKE_PATH}")
83  endif()
84
85  set(PACKAGE_NAME libunwind)
86  set(PACKAGE_VERSION 9.0.0svn)
87  set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}")
88  set(PACKAGE_BUGREPORT "llvm-bugs@lists.llvm.org")
89
90  if (EXISTS ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
91    set(LLVM_LIT ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
92  else()
93    # Seek installed Lit.
94    find_program(LLVM_LIT "lit.py" ${LLVM_MAIN_SRC_DIR}/utils/lit
95                 DOC "Path to lit.py")
96  endif()
97
98  if (LLVM_LIT)
99    # Define the default arguments to use with 'lit', and an option for the user
100    # to override.
101    set(LIT_ARGS_DEFAULT "-sv")
102    if (MSVC OR XCODE)
103      set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
104    endif()
105    set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
106
107    # On Win32 hosts, provide an option to specify the path to the GnuWin32 tools.
108    if (WIN32 AND NOT CYGWIN)
109      set(LLVM_LIT_TOOLS_DIR "" CACHE PATH "Path to GnuWin32 tools")
110    endif()
111  else()
112    set(LLVM_INCLUDE_TESTS OFF)
113  endif()
114
115  set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX})
116  set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX})
117else()
118  set(LLVM_LIT "${CMAKE_SOURCE_DIR}/utils/lit/lit.py")
119endif()
120
121#===============================================================================
122# Setup CMake Options
123#===============================================================================
124include(CMakeDependentOption)
125include(HandleCompilerRT)
126
127# Define options.
128option(LIBUNWIND_BUILD_32_BITS "Build 32 bit libunwind" ${LLVM_BUILD_32_BITS})
129option(LIBUNWIND_ENABLE_ASSERTIONS "Enable assertions independent of build mode." ON)
130option(LIBUNWIND_ENABLE_PEDANTIC "Compile with pedantic enabled." ON)
131option(LIBUNWIND_ENABLE_WERROR "Fail and stop if a warning is triggered." OFF)
132option(LIBUNWIND_ENABLE_SHARED "Build libunwind as a shared library." ON)
133option(LIBUNWIND_ENABLE_STATIC "Build libunwind as a static library." ON)
134option(LIBUNWIND_ENABLE_CROSS_UNWINDING "Enable cross-platform unwinding support." OFF)
135option(LIBUNWIND_ENABLE_ARM_WMMX "Enable unwinding support for ARM WMMX registers." OFF)
136option(LIBUNWIND_ENABLE_THREADS "Build libunwind with threading support." ON)
137option(LIBUNWIND_USE_COMPILER_RT "Use compiler-rt instead of libgcc" OFF)
138option(LIBUNWIND_INCLUDE_DOCS "Build the libunwind documentation." ${LLVM_INCLUDE_DOCS})
139
140set(LIBUNWIND_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}" CACHE STRING
141    "Define suffix of library directory name (32/64)")
142option(LIBUNWIND_INSTALL_LIBRARY "Install the libunwind library." ON)
143cmake_dependent_option(LIBUNWIND_INSTALL_STATIC_LIBRARY
144  "Install the static libunwind library." ON
145  "LIBUNWIND_ENABLE_STATIC;LIBUNWIND_INSTALL_LIBRARY" OFF)
146cmake_dependent_option(LIBUNWIND_INSTALL_SHARED_LIBRARY
147  "Install the shared libunwind library." ON
148  "LIBUNWIND_ENABLE_SHARED;LIBUNWIND_INSTALL_LIBRARY" OFF)
149set(LIBUNWIND_TARGET_TRIPLE "" CACHE STRING "Target triple for cross compiling.")
150set(LIBUNWIND_GCC_TOOLCHAIN "" CACHE PATH "GCC toolchain for cross compiling.")
151set(LIBUNWIND_SYSROOT "" CACHE PATH "Sysroot for cross compiling.")
152set(LIBUNWIND_TEST_LINKER_FLAGS "" CACHE STRING
153    "Additional linker flags for test programs.")
154set(LIBUNWIND_TEST_COMPILER_FLAGS "" CACHE STRING
155    "Additional compiler flags for test programs.")
156
157if (NOT LIBUNWIND_ENABLE_SHARED AND NOT LIBUNWIND_ENABLE_STATIC)
158  message(FATAL_ERROR "libunwind must be built as either a shared or static library.")
159endif()
160
161# Check that we can build with 32 bits if requested.
162if (CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32)
163  if (LIBUNWIND_BUILD_32_BITS AND NOT LLVM_BUILD_32_BITS) # Don't duplicate the output from LLVM
164    message(STATUS "Building 32 bits executables and libraries.")
165  endif()
166elseif(LIBUNWIND_BUILD_32_BITS)
167  message(FATAL_ERROR "LIBUNWIND_BUILD_32_BITS=ON is not supported on this platform.")
168endif()
169
170option(LIBUNWIND_HERMETIC_STATIC_LIBRARY
171  "Do not export any symbols from the static library." OFF)
172
173#===============================================================================
174# Configure System
175#===============================================================================
176
177# Add path for custom modules
178set(CMAKE_MODULE_PATH
179    "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
180    ${CMAKE_MODULE_PATH})
181
182set(LIBUNWIND_COMPILER    ${CMAKE_CXX_COMPILER})
183set(LIBUNWIND_SOURCE_DIR  ${CMAKE_CURRENT_SOURCE_DIR})
184set(LIBUNWIND_BINARY_DIR  ${CMAKE_CURRENT_BINARY_DIR})
185
186string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
187       ${PACKAGE_VERSION})
188
189if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
190  set(DEFAULT_INSTALL_PREFIX lib${LLVM_LIBDIR_SUFFIX}/clang/${CLANG_VERSION}/${LLVM_DEFAULT_TARGET_TRIPLE}/)
191  set(LIBUNWIND_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}/clang/${CLANG_VERSION}/${LLVM_DEFAULT_TARGET_TRIPLE}/lib${LIBUNWIND_LIBDIR_SUFFIX})
192elseif(LLVM_LIBRARY_OUTPUT_INTDIR)
193  set(LIBUNWIND_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
194else()
195  set(LIBUNWIND_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LIBUNWIND_LIBDIR_SUFFIX})
196endif()
197
198set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${LIBUNWIND_LIBRARY_DIR})
199set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${LIBUNWIND_LIBRARY_DIR})
200set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LIBUNWIND_LIBRARY_DIR})
201
202set(LIBUNWIND_INSTALL_PREFIX ${DEFAULT_INSTALL_PREFIX} CACHE STRING
203    "Define libunwind destination prefix.")
204
205set(LIBUNWIND_C_FLAGS "")
206set(LIBUNWIND_CXX_FLAGS "")
207set(LIBUNWIND_COMPILE_FLAGS "")
208set(LIBUNWIND_LINK_FLAGS "")
209
210# Get required flags.
211macro(unwind_append_if list condition var)
212  if (${condition})
213    list(APPEND ${list} ${var})
214  endif()
215endmacro()
216
217macro(add_target_flags)
218  foreach(value ${ARGN})
219    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${value}")
220    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${value}")
221    list(APPEND LIBUNWIND_COMPILE_FLAGS ${value})
222    list(APPEND LIBUNWIND_LINK_FLAGS ${value})
223  endforeach()
224endmacro()
225
226macro(add_target_flags_if condition)
227  if (${condition})
228    add_target_flags(${ARGN})
229  endif()
230endmacro()
231
232add_target_flags_if(LIBUNWIND_BUILD_32_BITS "-m32")
233
234if(LIBUNWIND_TARGET_TRIPLE)
235  add_target_flags("--target=${LIBUNWIND_TARGET_TRIPLE}")
236elseif(CMAKE_CXX_COMPILER_TARGET)
237  set(LIBUNWIND_TARGET_TRIPLE "${CMAKE_CXX_COMPILER_TARGET}")
238endif()
239if(LIBUNWIND_GCC_TOOLCHAIN)
240  add_target_flags("--gcc-toolchain=${LIBUNWIND_GCC_TOOLCHAIN}")
241elseif(CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN)
242  set(LIBUNWIND_GCC_TOOLCHAIN "${CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN}")
243endif()
244if(LIBUNWIND_SYSROOT)
245  add_target_flags("--sysroot=${LIBUNWIND_SYSROOT}")
246elseif(CMAKE_SYSROOT)
247  set(LIBUNWIND_SYSROOT "${CMAKE_SYSROOT}")
248endif()
249
250if (LIBUNWIND_TARGET_TRIPLE)
251  set(TARGET_TRIPLE "${LIBUNWIND_TARGET_TRIPLE}")
252endif()
253
254# Configure compiler.
255include(config-ix)
256
257if (LIBUNWIND_USE_COMPILER_RT AND NOT LIBUNWIND_HAS_NODEFAULTLIBS_FLAG)
258  list(APPEND LIBUNWIND_LINK_FLAGS "-rtlib=compiler-rt")
259endif()
260
261#===============================================================================
262# Setup Compiler Flags
263#===============================================================================
264
265unwind_append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WERROR_FLAG -Werror=return-type)
266
267# Get warning flags
268unwind_append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_W_FLAG -W)
269unwind_append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WALL_FLAG -Wall)
270unwind_append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WCHAR_SUBSCRIPTS_FLAG -Wchar-subscripts)
271unwind_append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WCONVERSION_FLAG -Wconversion)
272unwind_append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WMISMATCHED_TAGS_FLAG -Wmismatched-tags)
273unwind_append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WMISSING_BRACES_FLAG -Wmissing-braces)
274unwind_append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WNEWLINE_EOF_FLAG -Wnewline-eof)
275unwind_append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WNO_UNUSED_FUNCTION_FLAG -Wno-unused-function)
276unwind_append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WSHADOW_FLAG -Wshadow)
277unwind_append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WSHORTEN_64_TO_32_FLAG -Wshorten-64-to-32)
278unwind_append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WSIGN_COMPARE_FLAG -Wsign-compare)
279unwind_append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WSIGN_CONVERSION_FLAG -Wsign-conversion)
280unwind_append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WSTRICT_ALIASING_FLAG -Wstrict-aliasing=2)
281unwind_append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WSTRICT_OVERFLOW_FLAG -Wstrict-overflow=4)
282unwind_append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WUNUSED_PARAMETER_FLAG -Wunused-parameter)
283unwind_append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WUNUSED_VARIABLE_FLAG -Wunused-variable)
284unwind_append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WWRITE_STRINGS_FLAG -Wwrite-strings)
285unwind_append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WUNDEF_FLAG -Wundef)
286
287if (LIBUNWIND_ENABLE_WERROR)
288  unwind_append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WERROR_FLAG -Werror)
289  unwind_append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WX_FLAG -WX)
290else()
291  unwind_append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WNO_ERROR_FLAG -Wno-error)
292  unwind_append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_NO_WX_FLAG -WX-)
293endif()
294
295if (LIBUNWIND_ENABLE_PEDANTIC)
296  unwind_append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_PEDANTIC_FLAG -pedantic)
297endif()
298
299# Get feature flags.
300# Exceptions
301# Catches C++ exceptions only and tells the compiler to assume that extern C
302# functions never throw a C++ exception.
303unwind_append_if(LIBUNWIND_CXX_FLAGS LIBUNWIND_HAS_FSTRICT_ALIASING_FLAG -fstrict-aliasing)
304unwind_append_if(LIBUNWIND_CXX_FLAGS LIBUNWIND_HAS_EHSC_FLAG -EHsc)
305
306unwind_append_if(LIBUNWIND_C_FLAGS LIBUNWIND_HAS_FUNWIND_TABLES -funwind-tables)
307
308# Ensure that we don't depend on C++ standard library.
309unwind_append_if(LIBUNWIND_CXX_FLAGS LIBUNWIND_HAS_NOSTDINCXX_FLAG -nostdinc++)
310
311# Assert
312string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
313if (LIBUNWIND_ENABLE_ASSERTIONS)
314  # MSVC doesn't like _DEBUG on release builds. See PR 4379.
315  if (NOT MSVC)
316    list(APPEND LIBUNWIND_COMPILE_FLAGS -D_DEBUG)
317  endif()
318
319  # On Release builds cmake automatically defines NDEBUG, so we
320  # explicitly undefine it:
321  if (uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE")
322    list(APPEND LIBUNWIND_COMPILE_FLAGS -UNDEBUG)
323  endif()
324else()
325  if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE")
326    list(APPEND LIBUNWIND_COMPILE_FLAGS -DNDEBUG)
327  endif()
328endif()
329
330# Cross-unwinding
331if (NOT LIBUNWIND_ENABLE_CROSS_UNWINDING)
332  list(APPEND LIBUNWIND_COMPILE_FLAGS -D_LIBUNWIND_IS_NATIVE_ONLY)
333endif()
334
335# Threading-support
336if (NOT LIBUNWIND_ENABLE_THREADS)
337  list(APPEND LIBUNWIND_COMPILE_FLAGS -D_LIBUNWIND_HAS_NO_THREADS)
338endif()
339
340# ARM WMMX register support
341if (LIBUNWIND_ENABLE_ARM_WMMX)
342  # __ARM_WMMX is a compiler pre-define (as per the ACLE 2.0). Clang does not
343  # define this macro for any supported target at present. Therefore, here we
344  # provide the option to explicitly enable support for WMMX registers in the
345  # unwinder.
346  list(APPEND LIBUNWIND_COMPILE_FLAGS -D__ARM_WMMX)
347endif()
348
349# This is the _ONLY_ place where add_definitions is called.
350if (MSVC)
351  add_definitions(-D_CRT_SECURE_NO_WARNINGS)
352endif()
353
354# Disable DLL annotations on Windows for static builds.
355if (WIN32 AND LIBUNWIND_ENABLE_STATIC AND NOT LIBUNWIND_ENABLE_SHARED)
356  add_definitions(-D_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS)
357endif()
358
359#===============================================================================
360# Setup Source Code
361#===============================================================================
362
363include_directories(include)
364
365add_subdirectory(src)
366
367if (LIBUNWIND_INCLUDE_DOCS)
368  add_subdirectory(docs)
369endif()
370
371if (EXISTS ${LLVM_CMAKE_PATH})
372  add_subdirectory(test)
373endif()
374