Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use libunwind-generic instead of specific libunwind-PLAT.
[simgrid.git] / tools / cmake / Modules / FindLibunwind.cmake
1 # Search for libunwind and components, both includes and libraries
2 #
3 # Copyright (C) 2003-2018 The SimGrid Team.
4 # This is distributed under the LGPL licence but please contact us for
5 # relicensing if you need. This is merely free software, no matter the licence.
6 #
7 #
8 # Input environment variables:
9 #    LIBUNWIND_HINT: path to libunwind installation (e.g., /usr)
10 #                    (only needed for non-standard installs)
11 #
12 # You can tune the needed components here.
13 # TODO: we should take this as a parameter if I knew how to do so.
14
15 # SimGrid needs unwind-ptrace on Linux and FreeBSD
16 if("${CMAKE_SYSTEM}" MATCHES "Linux|FreeBSD")
17   set(LIBUNWIND_COMPONENTS ${LIBUNWIND_COMPONENTS} unwind-ptrace unwind-generic)
18 endif()
19
20 #
21 #  Output variables:
22 #     HAVE_LIBUNWIND     : if all components were found was found
23 #     LIBUNWIND_LIBRARIES: List of all libraries to load (-lunwind -lunwind-x86_64 and such)
24 #
25 #  Other effects:
26 #    - Calls include_directories() on where libunwind.h lives
27 #    - Calls link_directories() on where the libs live
28
29 # Of course also need the core lib
30 set(LIBUNWIND_COMPONENTS ${LIBUNWIND_COMPONENTS} "unwind")
31
32 message(STATUS "Looking for libunwind:")
33 # Let's assume we have it, and invalidate if parts are missing
34 SET(HAVE_LIBUNWIND 1)
35
36 #
37 # Search for the header file
38 #
39
40 find_path(PATH_LIBUNWIND_H "libunwind.h"
41   HINTS
42     $ENV{LIBUNWIND_HINT}
43     $ENV{LD_LIBRARY_PATH}
44   PATH_SUFFIXES include/ GnuWin32/include
45   PATHS /opt /opt/local /opt/csw /sw /usr)
46 if(PATH_LIBUNWIND_H)
47   string(REGEX REPLACE "/libunwind.h"               "" PATH_LIBUNWIND_H   "${PATH_LIBUNWIND_H}")
48   message("   Found libunwind.h in ${PATH_LIBUNWIND_H}")
49   include_directories(${PATH_LIBUNWIND_H})
50 else()
51   message("   NOT FOUND libunwind.h")
52   SET(HAVE_LIBUNWIND 0)
53 endif()
54 mark_as_advanced(PATH_LIBUNWIND_H)
55
56 #
57 # Search for the library components
58 #
59
60 foreach(component ${LIBUNWIND_COMPONENTS})
61   find_library(PATH_LIBUNWIND_LIB_${component}
62     NAMES ${component}
63     HINTS
64       $ENV{LIBUNWIND_HINT}
65       $ENV{LD_LIBRARY_PATH}
66     PATH_SUFFIXES lib/ GnuWin32/lib lib/system
67     PATHS /opt /opt/local /opt/csw /sw /usr /usr/lib/)
68   if(PATH_LIBUNWIND_LIB_${component})
69     # message("     ${component}  ${PATH_LIBUNWIND_LIB_${component}}")
70     string(REGEX REPLACE "/lib${component}.*[.]${LIB_EXE}$" "" PATH_LIBUNWIND_LIB_${component} "${PATH_LIBUNWIND_LIB_${component}}")
71     message("   Found lib${component}.${LIB_EXE} in ${PATH_LIBUNWIND_LIB_${component}}")
72     link_directories(${PATH_LIBUNWIND_LIB_${component}})
73
74     if(${component} STREQUAL "unwind" AND APPLE)
75         # Apple forbids to link directly against its libunwind implementation
76         # So let's comply to that stupid restriction and link against the System framework
77         SET(LIBUNWIND_LIBRARIES "${LIBUNWIND_LIBRARIES} -lSystem")
78     else()
79         SET(LIBUNWIND_LIBRARIES "${LIBUNWIND_LIBRARIES} -l${component}")
80     endif()
81         
82   else()
83     message("   Looking for lib${component}.${LIB_EXE} - not found")
84     SET(HAVE_LIBUNWIND 0)
85   endif()
86   mark_as_advanced(PATH_LIBUNWIND_LIB_${component})
87 endforeach()
88 unset(component)
89 unset(LIBUNWIND_COMPONENTS)
90
91 #
92 # Conclude and cleanup
93 #
94 if(HAVE_LIBUNWIND)
95   message(STATUS "Dependencies induced by libunwind: ${LIBUNWIND_LIBRARIES}")
96 else()
97   message(STATUS "Some libunwind components are missing")
98   set(LIBUNWIND_LIBRARIES "")
99 endif()