1 # CMakeLists.txt -- libbacktrace CMake build script
2 # Contributed by Alexander Monakov, ISP RAS
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
8 # (1) Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
11 # (2) Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in
13 # the documentation and/or other materials provided with the
16 # (3) The name of the author may not be used to
17 # endorse or promote products derived from this software without
18 # specific prior written permission.
20 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 # DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24 # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 # IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 # POSSIBILITY OF SUCH DAMAGE. */
32 cmake_minimum_required (VERSION 2.8)
34 project (libbacktrace)
36 set (BACKTRACE_SUPPORTED 1)
38 include (CheckSymbolExists)
39 check_symbol_exists (_Unwind_Backtrace unwind.h HAVE_BACKTRACE)
40 check_symbol_exists (_Unwind_GetIPInfo unwind.h HAVE_GETIPINFO)
43 set (BACKTRACE_FILE backtrace.c simple.c)
45 set (BACKTRACE_FILE nounwind.c)
46 set (BACKTRACE_SUPPORTED 0)
49 include (CheckCCompilerFlag)
50 check_c_compiler_flag ("-funwind-tables" FLAG_UNWIND_TABLES)
51 if (FLAG_UNWIND_TABLES)
52 add_definitions ("-funwind-tables")
55 add_definitions ("-fPIC")
58 if (CMAKE_COMPILER_IS_GNUCC)
59 add_definitions ("-Wno-switch -Wno-enum-compare")
62 check_c_source_compiles (
65 __sync_bool_compare_and_swap (&i, i, i);
66 __sync_lock_test_and_set (&i, 1);
67 __sync_lock_release (&i);}"
70 if (HAVE_SYNC_FUNCTIONS)
71 set (BACKTRACE_SUPPORTS_THREADS 1)
73 set (BACKTRACE_SUPPORTS_THREADS 0)
76 if (CMAKE_EXECUTABLE_FORMAT STREQUAL "ELF")
77 set (FORMAT_FILE elf.c dwarf.c)
78 math (EXPR BACKTRACE_ELF_SIZE 8*${CMAKE_C_SIZEOF_DATA_PTR})
80 set (FORMAT_FILE unknown.c)
81 set (BACKTRACE_SUPPORTED 0)
84 check_symbol_exists (mmap sys/mman.h HAVE_MMAP)
87 set (VIEW_FILE mmapio.c)
88 check_symbol_exists (MAP_ANONYMOUS sys/mman.h HAVE_MMAP_ANONYMOUS)
89 check_symbol_exists (MAP_ANON sys/mman.h HAVE_MMAP_ANON)
90 if (HAVE_MMAP_ANONYMOUS AND HAVE_MMAP_ANON)
91 set (ALLOC_FILE mmap.c)
93 set (ALLOC_FILE alloc.c)
96 set (VIEW_FILE read.c)
97 set (ALLOC_FILE alloc.c)
100 if (ALLOC_FILE STREQUAL "alloc.c")
101 set (BACKTRACE_USES_MALLOC 1)
103 set (BACKTRACE_USES_MALLOC 0)
106 add_definitions ("-D_GNU_SOURCE")
107 set (CMAKE_REQUIRED_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS} -D_GNU_SOURCE)
108 check_symbol_exists (dl_iterate_phdr link.h HAVE_DL_ITERATE_PHDR)
110 include (CheckFunctionExists)
111 check_function_exists (fcntl HAVE_FCNTL)
113 check_function_exists (strnlen HAVE_DECL_STRNLEN)
115 check_function_exists (getexecname HAVE_GETEXECNAME)
117 include (CheckIncludeFile)
118 check_include_file (dwarf.h HAVE_DWARF_H)
119 if (NOT HAVE_DWARF_H)
120 set (FORMAT_FILE unknown.c)
121 set (BACKTRACE_SUPPORTED 0)
124 configure_file (backtrace-supported.h.in backtrace-supported.h)
126 configure_file (config.h.in.cmake config.h)
128 include_directories ("auxincl")
129 add_library (backtrace STATIC EXCLUDE_FROM_ALL
130 ${BACKTRACE_FILE} ${FORMAT_FILE} ${VIEW_FILE} ${ALLOC_FILE}
131 fileline.c posix.c print.c state.c)
133 set (ENABLE_LIBBACKTRACE_TEST false CACHE BOOL "Enable libbacktrace testing")
134 if (ENABLE_LIBBACKTRACE_TEST)
136 add_executable (btest btest.c)
137 set_property (SOURCE btest.c PROPERTY COMPILE_FLAGS "-g")
138 target_link_libraries (btest backtrace)
139 add_test (test-libbacktrace btest)