blob: 7e5fa33d509ed827cc108585eb05e997ed6c00be [file] [log] [blame]
Radek Krejci4f2d40d2015-10-08 12:55:01 +02001# - Try to find LibSSH
2# Once done this will define
3#
4# LIBSSH_FOUND - system has LibSSH
5# LIBSSH_INCLUDE_DIRS - the LibSSH include directory
Michal Vasko75441e72017-09-27 13:57:00 +02006# LIBSSH_LIBRARY_DIR - the LibSSH library directory
Radek Krejci4f2d40d2015-10-08 12:55:01 +02007#
8# Copyright (c) 2009 Andreas Schneider <asn@cryptomilk.org>
9#
10# Redistribution and use in source and binary forms, with or without
11# modification, are permitted provided that the following conditions
12# are met:
13#
14# 1. Redistributions of source code must retain the copyright
15# notice, this list of conditions and the following disclaimer.
16# 2. Redistributions in binary form must reproduce the copyright
17# notice, this list of conditions and the following disclaimer in the
18# documentation and/or other materials provided with the distribution.
Michal Vasko75441e72017-09-27 13:57:00 +020019# 3. The name of the author may not be used to endorse or promote products
Radek Krejci4f2d40d2015-10-08 12:55:01 +020020# derived from this software without specific prior written permission.
21#
22# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32#
33
Claus Klein22091912020-01-20 13:45:47 +010034if(LIBSSH_LIBRARY_DIR AND LIBSSH_INCLUDE_DIRS)
Radek Krejci4f2d40d2015-10-08 12:55:01 +020035 # in cache already
36 set(LIBSSH_FOUND TRUE)
Claus Klein22091912020-01-20 13:45:47 +010037else()
Radek Krejci4f2d40d2015-10-08 12:55:01 +020038
39 find_path(LIBSSH_INCLUDE_DIR
40 NAMES
41 libssh/libssh.h
42 PATHS
43 /usr/include
44 /usr/local/include
45 /opt/local/include
46 /sw/include
47 ${CMAKE_INCLUDE_PATH}
48 ${CMAKE_INSTALL_PREFIX}/include
49 )
Michal Vasko75441e72017-09-27 13:57:00 +020050
Claus Klein22091912020-01-20 13:45:47 +010051 find_library(LIBSSH_LIBRARY
Radek Krejci4f2d40d2015-10-08 12:55:01 +020052 NAMES
Michal Vasko75441e72017-09-27 13:57:00 +020053 ssh.so
54 libssh.so
Claus Klein22091912020-01-20 13:45:47 +010055 libssh.dylib
Radek Krejci4f2d40d2015-10-08 12:55:01 +020056 PATHS
57 /usr/lib
58 /usr/local/lib
59 /opt/local/lib
60 /sw/lib
61 ${CMAKE_LIBRARY_PATH}
62 ${CMAKE_INSTALL_PREFIX}/lib
63 )
64
Claus Klein22091912020-01-20 13:45:47 +010065 if(LIBSSH_INCLUDE_DIR AND LIBSSH_LIBRARY)
66 set(LIBSSH_FOUND TRUE)
67 message(STATUS "LIBSSH Found: ${LIBSSH_LIBRARY}")
68 endif()
Radek Krejci4f2d40d2015-10-08 12:55:01 +020069
Claus Klein22091912020-01-20 13:45:47 +010070 set(LIBSSH_INCLUDE_DIRS ${LIBSSH_INCLUDE_DIR})
71 message(STATUS "LIBSSH_INCLUDE_DIR: ${LIBSSH_INCLUDE_DIR}")
Radek Krejci4f2d40d2015-10-08 12:55:01 +020072
Claus Klein22091912020-01-20 13:45:47 +010073 if(LIBSSH_FOUND)
Chaitanya Tataf2a28fc2019-04-23 13:46:40 +053074 string(REPLACE "libssh.so" ""
Michal Vasko75441e72017-09-27 13:57:00 +020075 LIBSSH_LIBRARY_DIR
Claus Klein22091912020-01-20 13:45:47 +010076 ${LIBSSH_LIBRARY}
77 )
78 string(REPLACE "libssh.dylib" ""
79 LIBSSH_LIBRARY_DIR
80 ${LIBSSH_LIBRARY_DIR}
Radek Krejci4f2d40d2015-10-08 12:55:01 +020081 )
Chaitanya Tataf2a28fc2019-04-23 13:46:40 +053082 string(REPLACE "ssh.so" ""
Michal Vasko75441e72017-09-27 13:57:00 +020083 LIBSSH_LIBRARY_DIR
84 ${LIBSSH_LIBRARY_DIR}
85 )
Claus Klein22091912020-01-20 13:45:47 +010086 message(STATUS "LIBSSH_LIBRARY_DIR: ${LIBSSH_LIBRARY_DIR}")
Radek Krejci4f2d40d2015-10-08 12:55:01 +020087
Claus Klein22091912020-01-20 13:45:47 +010088 if(LIBSSH_FIND_VERSION)
Radek Krejci4f2d40d2015-10-08 12:55:01 +020089 file(STRINGS ${LIBSSH_INCLUDE_DIR}/libssh/libssh.h LIBSSH_VERSION_MAJOR
90 REGEX "#define[ ]+LIBSSH_VERSION_MAJOR[ ]+[0-9]+")
91 # Older versions of libssh like libssh-0.2 have LIBSSH_VERSION but not LIBSSH_VERSION_MAJOR
Claus Klein22091912020-01-20 13:45:47 +010092 if(LIBSSH_VERSION_MAJOR)
Radek Krejci4f2d40d2015-10-08 12:55:01 +020093 string(REGEX MATCH "[0-9]+" LIBSSH_VERSION_MAJOR ${LIBSSH_VERSION_MAJOR})
Claus Klein22091912020-01-20 13:45:47 +010094 file(STRINGS ${LIBSSH_INCLUDE_DIR}/libssh/libssh.h LIBSSH_VERSION_MINOR
Radek Krejci4f2d40d2015-10-08 12:55:01 +020095 REGEX "#define[ ]+LIBSSH_VERSION_MINOR[ ]+[0-9]+")
Claus Klein22091912020-01-20 13:45:47 +010096 string(REGEX MATCH "[0-9]+" LIBSSH_VERSION_MINOR ${LIBSSH_VERSION_MINOR})
97 file(STRINGS ${LIBSSH_INCLUDE_DIR}/libssh/libssh.h LIBSSH_VERSION_PATCH
Radek Krejci4f2d40d2015-10-08 12:55:01 +020098 REGEX "#define[ ]+LIBSSH_VERSION_MICRO[ ]+[0-9]+")
Claus Klein22091912020-01-20 13:45:47 +010099 string(REGEX MATCH "[0-9]+" LIBSSH_VERSION_PATCH ${LIBSSH_VERSION_PATCH})
Radek Krejci4f2d40d2015-10-08 12:55:01 +0200100
Claus Klein22091912020-01-20 13:45:47 +0100101 set(LIBSSH_VERSION ${LIBSSH_VERSION_MAJOR}.${LIBSSH_VERSION_MINOR}.${LIBSSH_VERSION_PATCH})
Radek Krejci4f2d40d2015-10-08 12:55:01 +0200102
Claus Klein22091912020-01-20 13:45:47 +0100103 include(FindPackageVersionCheck)
104 find_package_version_check(LIBSSH DEFAULT_MSG)
105 else()
Radek Krejci4f2d40d2015-10-08 12:55:01 +0200106 message(STATUS "LIBSSH_VERSION_MAJOR not found in ${LIBSSH_INCLUDE_DIR}/libssh/libssh.h, assuming libssh is too old")
107 set(LIBSSH_FOUND FALSE)
Claus Klein22091912020-01-20 13:45:47 +0100108 endif()
109 endif()
110 endif()
Radek Krejci4f2d40d2015-10-08 12:55:01 +0200111
112 # If the version is too old, but libs and includes are set,
113 # find_package_handle_standard_args will set LIBSSH_FOUND to TRUE again,
114 # so we need this if() here.
Claus Klein22091912020-01-20 13:45:47 +0100115 if(LIBSSH_FOUND)
Radek Krejci4f2d40d2015-10-08 12:55:01 +0200116 include(FindPackageHandleStandardArgs)
Claus Klein22091912020-01-20 13:45:47 +0100117 find_package_handle_standard_args(LIBSSH DEFAULT_MSG LIBSSH_LIBRARY_DIR LIBSSH_INCLUDE_DIRS)
118 endif()
Radek Krejci4f2d40d2015-10-08 12:55:01 +0200119
Michal Vasko75441e72017-09-27 13:57:00 +0200120 # show the LIBSSH_INCLUDE_DIRS and LIBSSH_LIBRARY_DIR variables only in the advanced view
121 mark_as_advanced(LIBSSH_INCLUDE_DIRS LIBSSH_LIBRARY_DIR)
Radek Krejci4f2d40d2015-10-08 12:55:01 +0200122
Claus Klein22091912020-01-20 13:45:47 +0100123endif()
Radek Krejci4f2d40d2015-10-08 12:55:01 +0200124