]> git.cworth.org Git - apitrace/blob - thirdparty/libbacktrace/CMakeLists.txt
d6b4e10822dc986446928fbf25fdd8d6c59f84c2
[apitrace] / thirdparty / libbacktrace / CMakeLists.txt
1 #    CMakeLists.txt -- libbacktrace CMake build script
2 #    Contributed by Alexander Monakov, ISP RAS
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met:
7 #
8 #     (1) Redistributions of source code must retain the above copyright
9 #     notice, this list of conditions and the following disclaimer.
10 #
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
14 #     distribution.
15 #
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.
19 #
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.  */
31
32 cmake_minimum_required (VERSION 2.8)
33
34 project (libbacktrace)
35
36 set (BACKTRACE_SUPPORTED 1)
37
38 include (CheckSymbolExists)
39 check_symbol_exists (_Unwind_Backtrace unwind.h HAVE_BACKTRACE)
40 check_symbol_exists (_Unwind_GetIPInfo unwind.h HAVE_GETIPINFO)
41
42 if (HAVE_BACKTRACE)
43     set (BACKTRACE_FILE backtrace.c simple.c)
44 else ()
45     set (BACKTRACE_FILE nounwind.c)
46     set (BACKTRACE_SUPPORTED 0)
47 endif ()
48
49 include (CheckCCompilerFlag)
50 check_c_compiler_flag ("-funwind-tables" FLAG_UNWIND_TABLES)
51 if (FLAG_UNWIND_TABLES)
52     add_definitions ("-funwind-tables")
53 endif ()
54
55 # Adjust warnings
56 if (CMAKE_COMPILER_IS_GNUCC)
57     add_definitions ("-Wno-switch -Wno-enum-compare")
58 endif ()
59
60 check_c_source_compiles (
61     "int i;
62     int main() {
63     __sync_bool_compare_and_swap (&i, i, i);
64     __sync_lock_test_and_set (&i, 1);
65     __sync_lock_release (&i);}"
66     HAVE_SYNC_FUNCTIONS)
67
68 if (HAVE_SYNC_FUNCTIONS)
69     set (BACKTRACE_SUPPORTS_THREADS 1)
70 else ()
71     set (BACKTRACE_SUPPORTS_THREADS 0)
72 endif ()
73
74 if (CMAKE_EXECUTABLE_FORMAT STREQUAL "ELF")
75     set (FORMAT_FILE elf.c dwarf.c)
76     math (EXPR BACKTRACE_ELF_SIZE 8*${CMAKE_C_SIZEOF_DATA_PTR})
77 else ()
78     set (FORMAT_FILE unknown.c)
79     set (BACKTRACE_SUPPORTED 0)
80 endif ()
81
82 check_symbol_exists (mmap sys/mman.h HAVE_MMAP)
83
84 if (HAVE_MMAP)
85     set (VIEW_FILE mmapio.c)
86     check_symbol_exists (MAP_ANONYMOUS sys/mman.h HAVE_MMAP_ANONYMOUS)
87     check_symbol_exists (MAP_ANON sys/mman.h HAVE_MMAP_ANON)
88     if (HAVE_MMAP_ANONYMOUS AND HAVE_MMAP_ANON)
89         set (ALLOC_FILE mmap.c)
90     else ()
91         set (ALLOC_FILE alloc.c)
92     endif ()
93 else ()
94     set (VIEW_FILE read.c)
95     set (ALLOC_FILE alloc.c)
96 endif ()
97
98 if (ALLOC_FILE STREQUAL "alloc.c")
99     set (BACKTRACE_USES_MALLOC 1)
100 else ()
101     set (BACKTRACE_USES_MALLOC 0)
102 endif ()
103
104 add_definitions (-D_GNU_SOURCE)
105 set (CMAKE_REQUIRED_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS} -D_GNU_SOURCE)
106 check_symbol_exists (dl_iterate_phdr link.h HAVE_DL_ITERATE_PHDR)
107
108 include (CheckFunctionExists)
109 check_function_exists (fcntl HAVE_FCNTL)
110
111 check_function_exists (strnlen HAVE_DECL_STRNLEN)
112
113 check_function_exists (getexecname HAVE_GETEXECNAME)
114
115 include (CheckIncludeFile)
116 check_include_file (dwarf.h HAVE_DWARF_H)
117 if (NOT HAVE_DWARF_H)
118     set (FORMAT_FILE unknown.c)
119     set (BACKTRACE_SUPPORTED 0)
120 endif ()
121
122 configure_file (backtrace-supported.h.in backtrace-supported.h)
123
124 configure_file (config.h.in.cmake config.h)
125
126 include_directories (
127     ${CMAKE_CURRENT_BINARY_DIR}
128     auxincl
129 )
130 add_library (backtrace STATIC EXCLUDE_FROM_ALL
131     ${BACKTRACE_FILE}
132     ${FORMAT_FILE}
133     ${VIEW_FILE}
134     ${ALLOC_FILE}
135     fileline.c
136     posix.c
137     print.c
138     state.c
139 )
140 set_target_properties (backtrace PROPERTIES
141     COMPILE_FLAGS "${CMAKE_SHARED_LIBRARY_C_FLAGS}"
142 )
143
144 set (ENABLE_LIBBACKTRACE_TEST false CACHE BOOL "Enable libbacktrace testing")
145 if (ENABLE_LIBBACKTRACE_TEST)
146     enable_testing ()
147     add_executable (btest btest.c)
148     set_property (SOURCE btest.c PROPERTY COMPILE_FLAGS "-g")
149     target_link_libraries (btest backtrace)
150     add_test (test-libbacktrace btest)
151 endif ()