Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Rewrite how we search for libunwind (hopefully correctly this time)
[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)
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 # For some reason, some archs have an arch-specific while other do not.
33 if (CMAKE_SYSTEM_PROCESSOR MATCHES "^arm")
34     SET(LIBUNWIND_COMPONENTS ${LIBUNWIND_COMPONENTS} unwind-arm)
35 elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64" OR CMAKE_SYSTEM_PROCESSOR STREQUAL "amd64")
36     SET(LIBUNWIND_COMPONENTS ${LIBUNWIND_COMPONENTS} unwind-x86_64)
37 elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^i.86$")
38     SET(LIBUNWIND_COMPONENTS ${LIBUNWIND_COMPONENTS} unwind-x86)
39 endif()
40
41
42 message(STATUS "Looking for libunwind:")
43 # Let's assume we have it, and invalidate if parts are missing
44 SET(HAVE_LIBUNWIND 1)
45
46 #
47 # Search for the header file
48
49
50 find_path(PATH_LIBUNWIND_H "libunwind.h"
51   HINTS
52     $ENV{LIBUNWIND_HINT}
53     $ENV{LD_LIBRARY_PATH}
54   PATH_SUFFIXES include/ GnuWin32/include
55   PATHS /opt /opt/local /opt/csw /sw /usr)
56 if(PATH_LIBUNWIND_H)
57   string(REGEX REPLACE "/libunwind.h"               "" PATH_LIBUNWIND_H   "${PATH_LIBUNWIND_H}")
58   message("   Found libunwind.h in ${PATH_LIBUNWIND_H}")
59   include_directories(${PATH_LIBUNWIND_H})  
60 else()
61   message("   NOT FOUND libunwind.h")
62   SET(HAVE_LIBUNWIND 0)
63 endif()
64 mark_as_advanced(PATH_LIBUNWIND_H)
65
66 #
67 # Search for the library components
68 #
69
70 foreach(component ${LIBUNWIND_COMPONENTS})
71   find_library(PATH_LIBUNWIND_LIB_${component}
72     NAMES ${component}
73     HINTS
74       $ENV{LIBUNWIND_HINT}
75       $ENV{LD_LIBRARY_PATH}
76     PATH_SUFFIXES lib/ GnuWin32/lib lib/system
77     PATHS /opt /opt/local /opt/csw /sw /usr /usr/lib/)
78   if(PATH_LIBUNWIND_LIB_${component})
79     # message("     ${component}  ${PATH_LIBUNWIND_LIB_${component}}")
80     string(REGEX REPLACE "/lib${component}.*[.]${LIB_EXE}$" "" PATH_LIBUNWIND_LIB_${component} "${PATH_LIBUNWIND_LIB_${component}}")
81     message("   Found lib${component}.${LIB_EXE} in ${PATH_LIBUNWIND_LIB_${component}}")
82     link_directories(${PATH_LIBUNWIND_LIB_${component}})
83     
84     if(${component} STREQUAL "unwind" AND APPLE)
85         # Apple forbids to link directly against its libunwind implementation
86         # So let's comply to that stupid restriction and link against the System framework
87         SET(LIBUNWIND_LIBRARIES "${LIBUNWIND_LIBRARIES} -lSystem")
88     else()
89         SET(LIBUNWIND_LIBRARIES "${LIBUNWIND_LIBRARIES} -l${component}")
90     endif()
91         
92   else()
93     message("   Looking for lib${component}.${LIB_EXE} - not found")
94     SET(HAVE_LIBUNWIND 0)
95   endif()
96   mark_as_advanced(PATH_LIBUNWIND_LIB_${component})
97 endforeach()
98 unset(component)
99 unset(LIBUNWIND_COMPONENTS)
100
101 #
102 # Conclude and cleanup
103
104 if(HAVE_LIBUNWIND)
105   message(STATUS "Dependencies induced by libunwind: ${LIBUNWIND_LIBRARIES}")
106 else()
107   message(STATUS "Some libunwind components are missing")
108   set(LIBUNWIND_LIBRARIES "")
109 endif()