ENH: Improved backtrace{,_symbols}() detection
[freefoam.git] / CMake / Modules / FindExecinfo.cmake
blob6d2bbef667af2fb764e3cb8f0c5a57c375efe3a7
1 #-------------------------------------------------------------------------------
2 #                ______             ______ ____          __  __
3 #               |  ____|           |  ____/ __ \   /\   |  \/  |
4 #               | |__ _ __ ___  ___| |__ | |  | | /  \  | \  / |
5 #               |  __| '__/ _ \/ _ \  __|| |  | |/ /\ \ | |\/| |
6 #               | |  | | |  __/  __/ |   | |__| / ____ \| |  | |
7 #               |_|  |_|  \___|\___|_|    \____/_/    \_\_|  |_|
9 #                   FreeFOAM: The Cross-Platform CFD Toolkit
11 # Copyright (C) 2009 Michael Wild <themiwi@users.sf.net>
12 #                    Gerber van der Graaf <gerber_graaf@users.sf.net>
13 #-------------------------------------------------------------------------------
14 # License
15 #   This file is part of FreeFOAM.
17 #   FreeFOAM is free software; you can redistribute it and/or modify it
18 #   under the terms of the GNU General Public License as published by the
19 #   Free Software Foundation; either version 2 of the License, or (at your
20 #   option) any later version.
22 #   FreeFOAM is distributed in the hope that it will be useful, but WITHOUT
23 #   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
24 #   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
25 #   for more details.
27 #   You should have received a copy of the GNU General Public License
28 #   along with FreeFOAM; if not, write to the Free Software Foundation,
29 #   Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #-------------------------------------------------------------------------------
32 # - Find execinfo (i.e. backtrace() and backtrace_symbols())
34 # This module looks for execinfo support and defines the following values
35 #  EXECINFO_FOUND          TRUE if execinfo has been found
36 #  EXECINFO_INCLUDE_DIRS   include path for execinfo
37 #  EXECINFO_LIBRARIES      libraries to link against (if required)
39 include( FFDetectChangedValue )
41 # take extra care on APPLE, the result depends on the sysroot and the deployment target
42 if( APPLE )
43   ff_detect_changed_value( __find_execinfo_CMAKE_OSX_SYSROOT_CHANGED CMAKE_OSX_SYSROOT
44     EXECINFO_INCLUDE_DIR )
45   ff_detect_changed_value( __find_execinfo_CMAKE_OSX_DEPLOYMENT_TARGET_CHANGED CMAKE_OSX_DEPLOYMENT_TARGET
46     EXECINFO_INCLUDE_DIR )
47   set( __find_execinfo_FIND_PATH_OPTS PATHS
48     ${CMAKE_OSX_SYSROOT}/usr/include
49     NO_DEFAULT_PATH )
50 else( APPLE )
51   set( __find_execinfo_FIND_PATH_OPTS )
52 endif( APPLE )
54 find_path( EXECINFO_INCLUDE_DIR
55   NAMES execinfo.h
56   ${__find_execinfo_FIND_PATH_OPTS}
57   )
58 mark_as_advanced( EXECINFO_INCLUDE_DIR )
60 set( EXECINFO_INCLUDE_DIRS ${EXECINFO_INCLUDE_DIR} )
62 # now check whether libexecinfo is required
63 set( __find_execinfo_LINK_LIBRARIES ${LINK_LIBRARIES} )
64 set( __find_execinfo_test_SRC
65   "#include <execinfo.h>
66   int main() {
67   void* callstack[128];
68   int frames = backtrace(callstack, 128);
69   char** strs = backtrace_symbols(callstack, frames);
70   free(strs);
71   return 0;
72   }\n"
73   )
74 set( __find_execinfo_COMPILES FALSE )
75 set( __find_execinfo_REQUIRED_INCLUDE_DIRS "-DINCLUDE_DIRECTORIES=${EXECINFO_INCLUDE_DIR}" )
76 foreach( __find_execinfo_with_lib FALSE TRUE )
77   if( NOT __find_execinfo_COMPILES )
78     if( __find_execinfo_with_lib )
79       find_library( EXECINFO_LIBRARY NAMES execinfo )
80       mark_as_advanced( EXECINFO_LIBRARY )
81       set( __find_execinfo_REQUIRED_LIBS "-DLINK_LIBRARIES=${EXECINFO_LIBRARY}" )
82     else( __find_execinfo_with_lib )
83       set( __find_execinfo_REQUIRED_LIBS )
84     endif( __find_execinfo_with_lib )
85     file( WRITE
86       ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/execinfo_test.c
87       "${__find_execinfo_test_SRC}"
88       )
89     try_compile(  __find_execinfo_COMPILES ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
90       ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/execinfo_test.c
91       CMAKE_FLAGS "${__find_execinfo_REQUIRED_LIBS}" "${__find_execinfo_REQUIRED_INCLUDE_DIRS}"
92       )
93   endif( NOT __find_execinfo_COMPILES )
94 endforeach( __find_execinfo_with_lib )
95 # if the test compiled and we found a library, add it...
96 if( __find_execinfo_COMPILES AND EXECINFO_LIBRARY )
97   set( EXECINFO_LIBRARIES ${EXECINFO_LIBRARY} )
98 endif( __find_execinfo_COMPILES AND EXECINFO_LIBRARY )
100 # handle standard stuff
101 include( FindPackageHandleStandardArgs )
102 find_package_handle_standard_args( execinfo
103   DEFAULT_MSG
104   EXECINFO_INCLUDE_DIR
105   __find_execinfo_COMPILES
106   )
108 # ------------------------- vim: set sw=2 sts=2 et: --------------- end-of-file