From 5525bc7c0b5d33b81231ab484cd1b8635faa024c Mon Sep 17 00:00:00 2001 From: Michael Wild Date: Mon, 1 Jun 2009 09:58:33 +0200 Subject: [PATCH] ENH: Improved backtrace{,_symbols}() detection And moved the code into FindExecinfo.cmake. Signed-off-by: Michael Wild --- CMake/Modules/FindExecinfo.cmake | 108 +++++++++++++++++++++++++++++++++++++ CMakeLists.txt | 21 +------- src/OSspecific/Unix/CMakeLists.txt | 12 +++++ src/OSspecific/Unix/files.cmake | 6 +-- 4 files changed, 124 insertions(+), 23 deletions(-) create mode 100644 CMake/Modules/FindExecinfo.cmake diff --git a/CMake/Modules/FindExecinfo.cmake b/CMake/Modules/FindExecinfo.cmake new file mode 100644 index 000000000..6d2bbef66 --- /dev/null +++ b/CMake/Modules/FindExecinfo.cmake @@ -0,0 +1,108 @@ +#------------------------------------------------------------------------------- +# ______ ______ ____ __ __ +# | ____| | ____/ __ \ /\ | \/ | +# | |__ _ __ ___ ___| |__ | | | | / \ | \ / | +# | __| '__/ _ \/ _ \ __|| | | |/ /\ \ | |\/| | +# | | | | | __/ __/ | | |__| / ____ \| | | | +# |_| |_| \___|\___|_| \____/_/ \_\_| |_| +# +# FreeFOAM: The Cross-Platform CFD Toolkit +# +# Copyright (C) 2009 Michael Wild +# Gerber van der Graaf +#------------------------------------------------------------------------------- +# License +# This file is part of FreeFOAM. +# +# FreeFOAM is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the +# Free Software Foundation; either version 2 of the License, or (at your +# option) any later version. +# +# FreeFOAM is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +# for more details. +# +# You should have received a copy of the GNU General Public License +# along with FreeFOAM; if not, write to the Free Software Foundation, +# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +#------------------------------------------------------------------------------- + +# - Find execinfo (i.e. backtrace() and backtrace_symbols()) +# +# This module looks for execinfo support and defines the following values +# EXECINFO_FOUND TRUE if execinfo has been found +# EXECINFO_INCLUDE_DIRS include path for execinfo +# EXECINFO_LIBRARIES libraries to link against (if required) + +include( FFDetectChangedValue ) + +# take extra care on APPLE, the result depends on the sysroot and the deployment target +if( APPLE ) + ff_detect_changed_value( __find_execinfo_CMAKE_OSX_SYSROOT_CHANGED CMAKE_OSX_SYSROOT + EXECINFO_INCLUDE_DIR ) + ff_detect_changed_value( __find_execinfo_CMAKE_OSX_DEPLOYMENT_TARGET_CHANGED CMAKE_OSX_DEPLOYMENT_TARGET + EXECINFO_INCLUDE_DIR ) + set( __find_execinfo_FIND_PATH_OPTS PATHS + ${CMAKE_OSX_SYSROOT}/usr/include + NO_DEFAULT_PATH ) +else( APPLE ) + set( __find_execinfo_FIND_PATH_OPTS ) +endif( APPLE ) + +find_path( EXECINFO_INCLUDE_DIR + NAMES execinfo.h + ${__find_execinfo_FIND_PATH_OPTS} + ) +mark_as_advanced( EXECINFO_INCLUDE_DIR ) + +set( EXECINFO_INCLUDE_DIRS ${EXECINFO_INCLUDE_DIR} ) + +# now check whether libexecinfo is required +set( __find_execinfo_LINK_LIBRARIES ${LINK_LIBRARIES} ) +set( __find_execinfo_test_SRC + "#include + int main() { + void* callstack[128]; + int frames = backtrace(callstack, 128); + char** strs = backtrace_symbols(callstack, frames); + free(strs); + return 0; + }\n" + ) +set( __find_execinfo_COMPILES FALSE ) +set( __find_execinfo_REQUIRED_INCLUDE_DIRS "-DINCLUDE_DIRECTORIES=${EXECINFO_INCLUDE_DIR}" ) +foreach( __find_execinfo_with_lib FALSE TRUE ) + if( NOT __find_execinfo_COMPILES ) + if( __find_execinfo_with_lib ) + find_library( EXECINFO_LIBRARY NAMES execinfo ) + mark_as_advanced( EXECINFO_LIBRARY ) + set( __find_execinfo_REQUIRED_LIBS "-DLINK_LIBRARIES=${EXECINFO_LIBRARY}" ) + else( __find_execinfo_with_lib ) + set( __find_execinfo_REQUIRED_LIBS ) + endif( __find_execinfo_with_lib ) + file( WRITE + ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/execinfo_test.c + "${__find_execinfo_test_SRC}" + ) + try_compile( __find_execinfo_COMPILES ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp + ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/execinfo_test.c + CMAKE_FLAGS "${__find_execinfo_REQUIRED_LIBS}" "${__find_execinfo_REQUIRED_INCLUDE_DIRS}" + ) + endif( NOT __find_execinfo_COMPILES ) +endforeach( __find_execinfo_with_lib ) +# if the test compiled and we found a library, add it... +if( __find_execinfo_COMPILES AND EXECINFO_LIBRARY ) + set( EXECINFO_LIBRARIES ${EXECINFO_LIBRARY} ) +endif( __find_execinfo_COMPILES AND EXECINFO_LIBRARY ) + +# handle standard stuff +include( FindPackageHandleStandardArgs ) +find_package_handle_standard_args( execinfo + DEFAULT_MSG + EXECINFO_INCLUDE_DIR + __find_execinfo_COMPILES + ) + +# ------------------------- vim: set sw=2 sts=2 et: --------------- end-of-file diff --git a/CMakeLists.txt b/CMakeLists.txt index 82d8e548e..a67adade4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -302,26 +302,7 @@ endif( APPLE ) # execinfo.h and cxxabi.h only work using GNU gcc and Debug config # assume that if we have execinfo.h, cxxabi.h is also present. if( CMAKE_COMPILER_IS_GNUCXX AND CMAKE_BUILD_TYPE STREQUAL Debug ) - # find execinfo.h and cxxabi.h (take extra care on APPLE) - if( APPLE ) - ff_detect_changed_value( CMAKE_OSX_SYSROOT_CHANGED CMAKE_OSX_SYSROOT - FF_EXECINFO_H_PATH ) - ff_detect_changed_value( CMAKE_OSX_DEPLOYMENT_TARGET_CHANGED CMAKE_OSX_DEPLOYMENT_TARGET - FF_EXECINFO_H_PATH ) - set( FF_FIND_PATH_OPTS PATHS - ${CMAKE_OSX_SYSROOT}/usr/include - NO_DEFAULT_PATH ) - else( APPLE ) - set( FF_FIND_PATH_OPTS ) - endif( APPLE ) - - find_file( FF_EXECINFO_H_PATH execinfo.h ${FF_FIND_PATH_OPTS} ) - if( FF_EXECINFO_H_PATH ) - set( FF_HAVE_EXECINFO_H TRUE ) - else( FF_EXECINFO_H_PATH ) - set( FF_HAVE_EXECINFO_H FALSE ) - endif( FF_EXECINFO_H_PATH ) - mark_as_advanced( FF_EXECINFO_H_PATH ) + find_package( Execinfo ) endif( CMAKE_COMPILER_IS_GNUCXX AND CMAKE_BUILD_TYPE STREQUAL Debug ) # set up defines diff --git a/src/OSspecific/Unix/CMakeLists.txt b/src/OSspecific/Unix/CMakeLists.txt index 18f3825ce..f899beecd 100644 --- a/src/OSspecific/Unix/CMakeLists.txt +++ b/src/OSspecific/Unix/CMakeLists.txt @@ -35,8 +35,20 @@ create_include_wrappers( OSspecific ${HDRS} ) include_directories( signals cpuTime ) +if( CMAKE_COMPILER_IS_GNUCXX AND CMAKE_BUILD_TYPE STREQUAL Debug + AND EXECINFO_FOUND ) + include_directories( ${EXECINFO_INCLUDE_DIRS} ) +endif( CMAKE_COMPILER_IS_GNUCXX AND CMAKE_BUILD_TYPE STREQUAL Debug + AND EXECINFO_FOUND ) + add_library( OSspecific ${SRCS} ${HDRS} ) +if( CMAKE_COMPILER_IS_GNUCXX AND CMAKE_BUILD_TYPE STREQUAL Debug + AND EXECINFO_FOUND AND EXECINFO_LIBRARIES ) + target_link_libraries( OSspecific ${EXECINFO_LIBRARIES} ) +endif( CMAKE_COMPILER_IS_GNUCXX AND CMAKE_BUILD_TYPE STREQUAL Debug + AND EXECINFO_FOUND AND EXECINFO_LIBRARIES ) + set_target_properties( OSspecific PROPERTIES FRAMEWORK ${FF_BUILD_FRAMEWORK} FRAMEWORK_VERSION "${FF_VERSION_FULL}" diff --git a/src/OSspecific/Unix/files.cmake b/src/OSspecific/Unix/files.cmake index 463e2eda2..d533a2284 100644 --- a/src/OSspecific/Unix/files.cmake +++ b/src/OSspecific/Unix/files.cmake @@ -43,10 +43,10 @@ list( APPEND SRCS # only use printStack.C if we have a GCC compiler, are in Debug build type # and have execinfo.h and cxxabi.h -if( CMAKE_COMPILER_IS_GNUCXX AND CMAKE_BUILD_TYPE STREQUAL Debug AND FF_HAVE_EXECINFO_H ) +if( CMAKE_COMPILER_IS_GNUCXX AND CMAKE_BUILD_TYPE STREQUAL Debug AND EXECINFO_FOUND ) list( APPEND SRCS printStack.C ) -else( CMAKE_COMPILER_IS_GNUCXX AND CMAKE_BUILD_TYPE STREQUAL Debug AND FF_HAVE_EXECINFO_H ) +else( CMAKE_COMPILER_IS_GNUCXX AND CMAKE_BUILD_TYPE STREQUAL Debug AND EXECINFO_FOUND ) list( APPEND SRCS dummyPrintStack.C ) -endif( CMAKE_COMPILER_IS_GNUCXX AND CMAKE_BUILD_TYPE STREQUAL Debug AND FF_HAVE_EXECINFO_H ) +endif( CMAKE_COMPILER_IS_GNUCXX AND CMAKE_BUILD_TYPE STREQUAL Debug AND EXECINFO_FOUND ) # ------------------------- vim: set sw=2 sts=2 et: --------------- end-of-file -- 2.11.4.GIT