aboutsummaryrefslogtreecommitdiffstats
path: root/cmake/modules/FindNL.cmake
blob: d4f003e886204ef75b830cecc36c92d05724a66c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#
# Find the native netlink includes and library
#
# If they exist, differentiate between versions 1, 2 and 3.
# Version 1 does not have netlink/version.h
# Version 2 started separating libraries (libnl{,-genl,-route}).
# Version 3 (>= 3.2) started appending the major version number as suffix to
# library names (libnl-3)
#
#  NL_INCLUDE_DIRS - where to find libnl.h, etc.
#  NL_LIBRARIES    - List of libraries when using libnl.
#  NL_FOUND        - True if libnl found.

if(NL_LIBRARIES AND NL_INCLUDE_DIRS)
  # in cache already
  SET(NL_FOUND TRUE)
else()
  SET( SEARCHPATHS
      /opt/local
      /sw
      /usr
      /usr/local
  )

  find_package(PkgConfig)
  pkg_check_modules(NL3 libnl-3.0 libnl-genl-3.0 libnl-route-3.0)
  if(NOT NL3_FOUND)
    pkg_search_module(NL2 libnl-2.0)
  endif()

  # Try to find NL 2.0, 3.0 or 3.1 (/usr/include/netlink/version.h) or
  # NL >= 3.2 (/usr/include/libnl3/netlink/version.h)
  find_path(NL3_INCLUDE_DIR
    PATH_SUFFIXES
      include/libnl3
      include
    NAMES
      netlink/version.h
    HINTS
      "${NL3_libnl-3.0_INCLUDEDIR}"
      "${NL2_INCLUDEDIR}"
    PATHS
      $(SEARCHPATHS)
  )
  # NL version >= 2
  if(NL3_INCLUDE_DIR)
    find_library(NL3_LIBRARY
      NAMES
        nl-3 nl
      PATH_SUFFIXES
        lib64 lib
      HINTS
        "${NL3_libnl-3.0_LIBDIR}"
        "${NL2_LIBDIR}"
      PATHS
        $(SEARCHPATHS)
    )
    find_library(NLGENL_LIBRARY
      NAMES
        nl-genl-3 nl-genl
      PATH_SUFFIXES
        lib64 lib
      HINTS
        "${NL3_libnl-genl-3.0_LIBDIR}"
        "${NL2_LIBDIR}"
      PATHS
        $(SEARCHPATHS)
    )
    find_library(NLROUTE_LIBRARY
      NAMES
        nl-route-3 nl-route
      PATH_SUFFIXES
        lib64 lib
      HINTS
        "${NL3_libnl-route-3.0_LIBDIR}"
        "${NL2_LIBDIR}"
      PATHS
        $(SEARCHPATHS)
    )
    #
    # If we don't have all of those libraries, we can't use libnl.
    #
    if(NL3_LIBRARY AND NLGENL_LIBRARY AND NLROUTE_LIBRARY)
      set(NL_LIBRARY ${NL3_LIBRARY})
      if(NL3_INCLUDE_DIR)
        # NL2 and NL3 are similar and just affect how the version is reported in
        # the --version output. In cast of doubt, assume NL3 since a library
        # without version number could be any of 2.0, 3.0 or 3.1.
        if(NOT NL3_FOUND AND NL2_FOUND)
          set(HAVE_LIBNL2 1)
        else()
          set(HAVE_LIBNL3 1)
        endif()
      endif()
    endif()
    set(NL_INCLUDE_DIR ${NL3_INCLUDE_DIR})
  endif()

  # libnl-2 and libnl-3 not found, try NL version 1
  if(NOT (NL_LIBRARY AND NL_INCLUDE_DIR))
    pkg_search_module(NL1 libnl-1)
    find_path(NL1_INCLUDE_DIR
      NAMES
        netlink/netlink.h
      HINTS
        "${NL1_INCLUDEDIR}"
      PATHS
        $(SEARCHPATHS)
    )
    find_library(NL1_LIBRARY
      NAMES
        nl
      PATH_SUFFIXES
        lib64 lib
      HINTS
        "${NL1_LIBDIR}"
      PATHS
        $(SEARCHPATHS)
    )
    set(NL_LIBRARY ${NL1_LIBRARY})
    set(NL_INCLUDE_DIR ${NL1_INCLUDE_DIR})
    if(NL1_LIBRARY AND NL1_INCLUDE_DIR)
      set(HAVE_LIBNL1 1)
    endif()
  endif()
endif()
# MESSAGE(STATUS "LIB Found: ${NL_LIBRARY}, Suffix: ${NLSUFFIX}\n  1:${HAVE_LIBNL1}, 2:${HAVE_LIBNL2}, 3:${HAVE_LIBNL3}.")

# handle the QUIETLY and REQUIRED arguments and set NL_FOUND to TRUE if
# all listed variables are TRUE
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(NL DEFAULT_MSG NL_LIBRARY NL_INCLUDE_DIR)

IF(NL_FOUND)
  set(NL_LIBRARIES ${NLGENL_LIBRARY} ${NLROUTE_LIBRARY} ${NL_LIBRARY})
  set(NL_INCLUDE_DIRS ${NL_INCLUDE_DIR})
  set(HAVE_LIBNL 1)
else()
  set(NL_LIBRARIES )
  set(NL_INCLUDE_DIRS)
endif()

MARK_AS_ADVANCED( NL_LIBRARIES NL_INCLUDE_DIRS )