]> git.cworth.org Git - vogl/blob - src/libbacktrace/CMakeLists.txt
Initial vogl checkin
[vogl] / src / 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 if (CMAKE_BUILD_TYPE STREQUAL "Debug")
56     add_definitions("-D_DEBUG -DDEBUG")
57 endif()
58
59 # Adjust warnings
60 if (CMAKE_COMPILER_IS_GNUCC)
61     add_definitions ("-Wno-switch -Wno-enum-compare")
62 endif ()
63
64 # Add these compiler options to match how voglcore and voglcommon are compiled, and to get libbacktrace compiling with gcc.
65 # -fno-strict-aliasing is particularly important if voglcore is called, and -fvisibility=hidden must be used otherwide symbols from this lib could be made visible in libvogltrace.
66 add_definitions ("-g -fno-omit-frame-pointer -mtune=corei7 -fno-strict-aliasing -fno-math-errno -fvisibility=hidden")
67 SET(CMAKE_CXX_FLAGS_RELEASE "-O2 -g -DNDEBUG")
68
69 if ("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
70    if ( NOT BUILD_X64 )
71       # Fix startup crash in dlopen_notify_callback (called indirectly from our dlopen() function) when tracing glxspheres on my AMD dev box (x86 release only)
72           #add_definitions ("-mstack-alignment=8")
73    endif()
74 endif()
75
76 # clang doesn't print colored diagnostics when invoked from Ninja
77 if ("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
78   if (UNIX AND CMAKE_GENERATOR STREQUAL "Ninja")
79       add_definitions ("-fcolor-diagnostics")
80   endif()
81 endif()
82
83 check_c_source_compiles (
84     "int i;
85     int main() {
86     __sync_bool_compare_and_swap (&i, i, i);
87     __sync_lock_test_and_set (&i, 1);
88     __sync_lock_release (&i);}"
89     HAVE_SYNC_FUNCTIONS)
90
91 if (HAVE_SYNC_FUNCTIONS)
92     set (BACKTRACE_SUPPORTS_THREADS 1)
93 else ()
94     set (BACKTRACE_SUPPORTS_THREADS 0)
95 endif ()
96
97 if (CMAKE_EXECUTABLE_FORMAT STREQUAL "ELF")
98     set (FORMAT_FILE elf.c dwarf.c)
99     math (EXPR BACKTRACE_ELF_SIZE 8*${CMAKE_C_SIZEOF_DATA_PTR})
100 else ()
101     set (FORMAT_FILE unknown.c)
102     set (BACKTRACE_SUPPORTED 0)
103 endif ()
104
105 check_symbol_exists (mmap sys/mman.h HAVE_MMAP)
106
107 if (HAVE_MMAP)
108     set (VIEW_FILE mmapio.c)
109     check_symbol_exists (MAP_ANONYMOUS sys/mman.h HAVE_MMAP_ANONYMOUS)
110     check_symbol_exists (MAP_ANON sys/mman.h HAVE_MMAP_ANON)
111     if (HAVE_MMAP_ANONYMOUS AND HAVE_MMAP_ANON)
112         set (ALLOC_FILE mmap.c)
113     else ()
114         set (ALLOC_FILE alloc.c)
115     endif ()
116 else ()
117     set (VIEW_FILE read.c)
118     set (ALLOC_FILE alloc.c)
119 endif ()
120
121 if (ALLOC_FILE STREQUAL "alloc.c")
122     set (BACKTRACE_USES_MALLOC 1)
123 else ()
124     set (BACKTRACE_USES_MALLOC 0)
125 endif ()
126
127 if (CMAKE_BUILD_TYPE STREQUAL "Debug")
128     add_definitions("-D_DEBUG -DDEBUG")
129 endif()
130 add_definitions (-D_GNU_SOURCE)
131 set (CMAKE_REQUIRED_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS} -D_GNU_SOURCE)
132 check_symbol_exists (dl_iterate_phdr link.h HAVE_DL_ITERATE_PHDR)
133
134 include (CheckFunctionExists)
135 check_function_exists (fcntl HAVE_FCNTL)
136
137 check_function_exists (strnlen HAVE_DECL_STRNLEN)
138
139 check_function_exists (getexecname HAVE_GETEXECNAME)
140
141 include (CheckIncludeFile)
142 check_include_file (dwarf.h HAVE_DWARF_H)
143 if (NOT HAVE_DWARF_H)
144     set (FORMAT_FILE unknown.c)
145     set (BACKTRACE_SUPPORTED 0)
146 endif ()
147
148 configure_file (backtrace-supported.h.in backtrace-supported.h)
149
150 configure_file (config.h.in.cmake config.h)
151
152 include_directories (BEFORE
153     ${CMAKE_CURRENT_BINARY_DIR}
154 )
155 include_directories (
156     auxincl
157     ${SRC_DIR}/voglcore
158 )
159 add_library (backtrace STATIC EXCLUDE_FROM_ALL
160     ${BACKTRACE_FILE}
161     ${FORMAT_FILE}
162     ${VIEW_FILE}
163     ${ALLOC_FILE}
164     atomic.c
165     fileline.c
166     posix.c
167     print.c
168     state.c
169     # mikesart added
170     btrace.cpp
171     libelftc_dem_gnu3.c
172 )
173 target_link_libraries(backtrace voglcore)
174
175 find_library(LIBUNWIND_LIBRARY libunwind.a)
176 target_link_libraries(backtrace ${LIBUNWIND_LIBRARY})
177
178 set_target_properties (backtrace PROPERTIES
179     COMPILE_FLAGS "${CMAKE_SHARED_LIBRARY_C_FLAGS}"
180 )
181
182 set (ENABLE_LIBBACKTRACE_TEST false CACHE BOOL "Enable libbacktrace testing")
183 if (ENABLE_LIBBACKTRACE_TEST)
184     enable_testing ()
185     add_executable (btest btest.c)
186     set_property (SOURCE btest.c PROPERTY COMPILE_FLAGS "-g")
187     target_link_libraries (btest backtrace)
188     add_test (test-libbacktrace btest)
189 endif ()