File: Findlibssh.cmake

package info (click to toggle)
kio-extras 4%3A22.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 10,768 kB
  • sloc: cpp: 26,898; ansic: 4,443; perl: 1,058; xml: 97; sh: 69; python: 28; makefile: 9
file content (109 lines) | stat: -rw-r--r-- 3,944 bytes parent folder | download | duplicates (3)
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
# - Try to find libssh
# Once done this will define
#
#  LIBSSH_FOUND - system has libssh
#  LIBSSH_INCLUDE_DIR - the libssh include directory
#  LIBSSH_LIBRARIES - Link these to use libssh
#  LIBSSH_DEFINITIONS - Compiler switches required for using libssh

# SPDX-License-Identifier: BSD-3-Clause
# SPDX-FileCopyrightText: 2009-2014 Andreas Schneider <asn@cryptomilk.org>
# SPDX-FileCopyrightText: 2018 Harald Sitter <sitter@kde.org>

# Define an imported target to have compatibility with newer libssh and so
# we have a single target to use regardless of the code path taken in the
# finder and the actual libssh version defining the target.
macro(libssh_ensure_imported_target)
  if(NOT TARGET ssh)
      add_library(ssh SHARED IMPORTED)
      set_target_properties(ssh PROPERTIES
          IMPORTED_LOCATION "${LIBSSH_LIBRARIES}"
          INTERFACE_INCLUDE_DIRECTORIES "${LIBSSH_INCLUDE_DIR}"
      )
  endif()
endmacro()

# We prefer the config, but on Ubuntu 18.04 LTS (and to some extent later
# versions it seems) they've not packaged the config properly. So, go for the
# config by default and fall back to manual lookup iff the config was not found.
# https://bugs.kde.org/show_bug.cgi?id=400291
# https://bugs.launchpad.net/ubuntu/+source/libssh/+bug/1800135
find_package(libssh ${libssh_FIND_VERSION} NO_MODULE QUIET)
if(libssh_FOUND)
  # Certain versions with config may not have the target, so make sure it's
  # defined.
  libssh_ensure_imported_target()
  return()
endif()

find_path(LIBSSH_INCLUDE_DIR
  NAMES
    libssh/libssh.h
  PATHS
    /usr/include
    /usr/local/include
    /opt/local/include
    /sw/include
    ${CMAKE_INCLUDE_PATH}
    ${CMAKE_INSTALL_PREFIX}/include
)

find_library(SSH_LIBRARY
  NAMES
    ssh
    libssh
  PATHS
    /usr/lib
    /usr/local/lib
    /opt/local/lib
    /sw/lib
    ${CMAKE_LIBRARY_PATH}
    ${CMAKE_INSTALL_PREFIX}/lib
)

set(LIBSSH_LIBRARIES
    ${LIBSSH_LIBRARIES}
    ${SSH_LIBRARY}
)

message("LIB: ${LIBSSH_LIBRARIES}; INC: ${LIBSSH_INCLUDE_DIR};")

if (LIBSSH_INCLUDE_DIR AND libssh_FIND_VERSION)
  file(STRINGS ${LIBSSH_INCLUDE_DIR}/libssh/libssh.h LIBSSH_VERSION_MAJOR
    REGEX "#define[ ]+LIBSSH_VERSION_MAJOR[ ]+[0-9]+")

  # Older versions of libssh like libssh-0.2 have LIBSSH_VERSION but not LIBSSH_VERSION_MAJOR
  if (LIBSSH_VERSION_MAJOR)
    string(REGEX MATCH "[0-9]+" LIBSSH_VERSION_MAJOR ${LIBSSH_VERSION_MAJOR})
    file(STRINGS ${LIBSSH_INCLUDE_DIR}/libssh/libssh.h LIBSSH_VERSION_MINOR
      REGEX "#define[ ]+LIBSSH_VERSION_MINOR[ ]+[0-9]+")
    string(REGEX MATCH "[0-9]+" LIBSSH_VERSION_MINOR ${LIBSSH_VERSION_MINOR})
    file(STRINGS ${LIBSSH_INCLUDE_DIR}/libssh/libssh.h LIBSSH_VERSION_PATCH
      REGEX "#define[ ]+LIBSSH_VERSION_MICRO[ ]+[0-9]+")
    string(REGEX MATCH "[0-9]+" LIBSSH_VERSION_PATCH ${LIBSSH_VERSION_PATCH})

    set(LIBSSH_VERSION ${LIBSSH_VERSION_MAJOR}.${LIBSSH_VERSION_MINOR}.${LIBSSH_VERSION_PATCH})

  else (LIBSSH_VERSION_MAJOR)
    message(STATUS "LIBSSH_VERSION_MAJOR not found in ${LIBSSH_INCLUDE_DIR}/libssh/libssh.h, assuming libssh is too old")
    set(LIBSSH_FOUND FALSE)
  endif (LIBSSH_VERSION_MAJOR)
endif (LIBSSH_INCLUDE_DIR AND libssh_FIND_VERSION)

# If the version is too old, but libs and includes are set,
# find_package_handle_standard_args will set LIBSSH_FOUND to TRUE again,
# so we need this if() here.
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(libssh
                                  FOUND_VAR
                                    LIBSSH_FOUND
                                  REQUIRED_VARS
                                    LIBSSH_LIBRARIES
                                    LIBSSH_INCLUDE_DIR
                                  VERSION_VAR
                                    LIBSSH_VERSION)

libssh_ensure_imported_target()

# show the LIBSSH_INCLUDE_DIR and LIBSSH_LIBRARIES variables only in the advanced view
mark_as_advanced(LIBSSH_INCLUDE_DIR LIBSSH_LIBRARIES)