Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
sanitize our way to find and load libunwind
[simgrid.git] / tools / cmake / Modules / FindLibunwind.cmake
1 # Find the libunwind library
2 #
3 #  LIBUNWIND_FOUND       - True if libunwind was found.
4 #  LIBUNWIND_LIBRARIES   - The libraries needed to use libunwind
5 #  LIBUNWIND_INCLUDE_DIR - Location of unwind.h and libunwind.h
6
7 # This file was downloaded from https://github.com/cmccabe/lksmith/blob/master/cmake_modules/FindLibunwind.cmake
8 # Copyright (c) 2011-2012, the Locksmith authors.
9 # All rights reserved.
10 #
11 # Redistribution and use in source and binary forms, with or without
12 # modification, are permitted provided that the following conditions
13 # are met:
14 #
15 # Redistributions of source code must retain the above copyright
16 # notice, this list of conditions and the following disclaimer.
17 #
18 # Redistributions in binary form must reproduce the above copyright
19 # notice, this list of conditions and the following disclaimer in the
20 # documentation and/or other materials provided with the distribution.
21 #
22 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34 # Modified by the SimGrid team for:
35 #  - Survive the fact that libunwind cannot be found (setting LIBUNWIND_FOUND correctly)
36 #  - Also search for the unwind-ptrace component (but survive when it's not here)
37
38 set(LIBUNWIND_FOUND "1")
39
40 FIND_PATH(LIBUNWIND_INCLUDE_DIR libunwind.h)
41 if(NOT LIBUNWIND_INCLUDE_DIR)
42   message(FATAL_ERROR "failed to find libunwind.h")
43   set(LIBUNWIND_FOUND "")
44 elif(NOT EXISTS "${LIBUNWIND_INCLUDE_DIR}/unwind.h")
45   message(FATAL_ERROR "libunwind.h was found, but unwind.h was not found in that directory.")
46   set(LIBUNWIND_FOUND "")
47   SET(LIBUNWIND_INCLUDE_DIR "")
48 endif()
49
50 FIND_LIBRARY(LIBUNWIND_GENERIC_LIBRARY "unwind")
51 if (LIBUNWIND_GENERIC_LIBRARY)
52   MESSAGE(STATUS "Found libunwind library: ${LIBUNWIND_GENERIC_LIBRARY}")
53 else()
54   set(LIBUNWIND_FOUND "")
55   MESSAGE(FATAL_ERROR "failed to find unwind generic library")
56 endif ()
57 SET(LIBUNWIND_LIBRARIES ${LIBUNWIND_GENERIC_LIBRARY})
58 unset(LIBUNWIND_GENERIC_LIBRARY)
59
60 FIND_LIBRARY(LIBUNWIND_PTRACE_LIBRARY "unwind-ptrace")
61 if (LIBUNWIND_PTRACE_LIBRARY)
62   MESSAGE(STATUS "Found libunwind-ptrace library: ${LIBUNWIND_PTRACE_LIBRARY}")
63   SET(LIBUNWIND_LIBRARIES ${LIBUNWIND_LIBRARIES} ${LIBUNWIND_PTRACE_LIBRARY})
64 else()
65   MESSAGE(WARNING "Failed to find unwind-ptrace library. Proceeding anyway.")
66 endif ()
67 unset(LIBUNWIND_PTRACE_LIBRARY)
68
69 # For some reason, we have to link to two libunwind shared object files:
70 # one arch-specific and one not.
71 if (CMAKE_SYSTEM_PROCESSOR MATCHES "^arm")
72     SET(LIBUNWIND_ARCH "arm")
73 elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64" OR CMAKE_SYSTEM_PROCESSOR STREQUAL "amd64")
74     SET(LIBUNWIND_ARCH "x86_64")
75 elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^i.86$")
76     SET(LIBUNWIND_ARCH "x86")
77 endif()
78
79 if (LIBUNWIND_FOUND AND LIBUNWIND_ARCH)
80     FIND_LIBRARY(LIBUNWIND_SPECIFIC_LIBRARY "unwind-${LIBUNWIND_ARCH}")
81     if (LIBUNWIND_SPECIFIC_LIBRARY)
82         MESSAGE(STATUS "Found libunwind-${LIBUNWIND_ARCH} library: ${LIBUNWIND_SPECIFIC_LIBRARY}")
83     else()
84         MESSAGE(FATAL_ERROR "failed to find unwind-${LIBUNWIND_ARCH}")
85         set(LIBUNWIND_FOUND "")
86     endif ()
87     SET(LIBUNWIND_LIBRARIES ${LIBUNWIND_LIBRARIES} ${LIBUNWIND_SPECIFIC_LIBRARY})
88     unset(LIBUNWIND_SPECIFIC_LIBRARY)
89 endif()
90
91 if (LIBUNWIND_FOUND)
92   MESSAGE(STATUS "Found all required libunwind components.")
93 endif()
94
95 MARK_AS_ADVANCED(LIBUNWIND_LIBRARIES LIBUNWIND_INCLUDE_DIR)