blob: 4fde8992955e06231b9c325558570579c56fabe6 [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
Michal Vasko71514b32020-04-07 13:36:51 +02007# LIBSSH_LIBRARIES - link these to use LibSSH
Radek Krejci4f2d40d2015-10-08 12:55:01 +02008#
9# Copyright (c) 2009 Andreas Schneider <asn@cryptomilk.org>
10#
11# Redistribution and use in source and binary forms, with or without
12# modification, are permitted provided that the following conditions
13# are met:
14#
15# 1. Redistributions of source code must retain the copyright
16# notice, this list of conditions and the following disclaimer.
17# 2. Redistributions in binary form must reproduce the copyright
18# notice, this list of conditions and the following disclaimer in the
19# documentation and/or other materials provided with the distribution.
Michal Vasko75441e72017-09-27 13:57:00 +020020# 3. The name of the author may not be used to endorse or promote products
Radek Krejci4f2d40d2015-10-08 12:55:01 +020021# derived from this software without specific prior written permission.
22#
23# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33#
34
Michal Vasko71514b32020-04-07 13:36:51 +020035if(LIBSSH_LIBRARY_DIR AND LIBSSH_LIBRARIES AND LIBSSH_INCLUDE_DIRS)
Radek Krejci4f2d40d2015-10-08 12:55:01 +020036 # in cache already
37 set(LIBSSH_FOUND TRUE)
Claus Klein22091912020-01-20 13:45:47 +010038else()
Radek Krejci4f2d40d2015-10-08 12:55:01 +020039
40 find_path(LIBSSH_INCLUDE_DIR
41 NAMES
42 libssh/libssh.h
43 PATHS
44 /usr/include
45 /usr/local/include
46 /opt/local/include
47 /sw/include
48 ${CMAKE_INCLUDE_PATH}
49 ${CMAKE_INSTALL_PREFIX}/include
50 )
Michal Vasko75441e72017-09-27 13:57:00 +020051
Claus Klein22091912020-01-20 13:45:47 +010052 find_library(LIBSSH_LIBRARY
Radek Krejci4f2d40d2015-10-08 12:55:01 +020053 NAMES
Michal Vasko75441e72017-09-27 13:57:00 +020054 ssh.so
55 libssh.so
Claus Klein22091912020-01-20 13:45:47 +010056 libssh.dylib
Radek Krejci4f2d40d2015-10-08 12:55:01 +020057 PATHS
58 /usr/lib
59 /usr/local/lib
60 /opt/local/lib
61 /sw/lib
62 ${CMAKE_LIBRARY_PATH}
63 ${CMAKE_INSTALL_PREFIX}/lib
64 )
65
Claus Klein22091912020-01-20 13:45:47 +010066 if(LIBSSH_INCLUDE_DIR AND LIBSSH_LIBRARY)
Michal Vasko71514b32020-04-07 13:36:51 +020067 set(SSH_FOUND TRUE)
Claus Klein22091912020-01-20 13:45:47 +010068 endif()
Radek Krejci4f2d40d2015-10-08 12:55:01 +020069
Claus Klein22091912020-01-20 13:45:47 +010070 set(LIBSSH_INCLUDE_DIRS ${LIBSSH_INCLUDE_DIR})
Michal Vasko71514b32020-04-07 13:36:51 +020071 set(LIBSSH_LIBRARIES ${LIBSSH_LIBRARY})
Radek Krejci4f2d40d2015-10-08 12:55:01 +020072
Michal Vasko71514b32020-04-07 13:36:51 +020073 if (SSH_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 )
Radek Krejci4f2d40d2015-10-08 12:55:01 +020086
Michal Vasko71514b32020-04-07 13:36:51 +020087 if (LibSSH_FIND_VERSION)
Radek Krejci4f2d40d2015-10-08 12:55:01 +020088 file(STRINGS ${LIBSSH_INCLUDE_DIR}/libssh/libssh.h LIBSSH_VERSION_MAJOR
89 REGEX "#define[ ]+LIBSSH_VERSION_MAJOR[ ]+[0-9]+")
90 # Older versions of libssh like libssh-0.2 have LIBSSH_VERSION but not LIBSSH_VERSION_MAJOR
Michal Vasko71514b32020-04-07 13:36:51 +020091 if (LIBSSH_VERSION_MAJOR)
Radek Krejci4f2d40d2015-10-08 12:55:01 +020092 string(REGEX MATCH "[0-9]+" LIBSSH_VERSION_MAJOR ${LIBSSH_VERSION_MAJOR})
Claus Klein22091912020-01-20 13:45:47 +010093 file(STRINGS ${LIBSSH_INCLUDE_DIR}/libssh/libssh.h LIBSSH_VERSION_MINOR
Michal Vasko71514b32020-04-07 13:36:51 +020094 REGEX "#define[ ]+LIBSSH_VERSION_MINOR[ ]+[0-9]+")
Claus Klein22091912020-01-20 13:45:47 +010095 string(REGEX MATCH "[0-9]+" LIBSSH_VERSION_MINOR ${LIBSSH_VERSION_MINOR})
96 file(STRINGS ${LIBSSH_INCLUDE_DIR}/libssh/libssh.h LIBSSH_VERSION_PATCH
Michal Vasko71514b32020-04-07 13:36:51 +020097 REGEX "#define[ ]+LIBSSH_VERSION_MICRO[ ]+[0-9]+")
Claus Klein22091912020-01-20 13:45:47 +010098 string(REGEX MATCH "[0-9]+" LIBSSH_VERSION_PATCH ${LIBSSH_VERSION_PATCH})
Radek Krejci4f2d40d2015-10-08 12:55:01 +020099
Michal Vasko71514b32020-04-07 13:36:51 +0200100 set(LibSSH_VERSION ${LIBSSH_VERSION_MAJOR}.${LIBSSH_VERSION_MINOR}.${LIBSSH_VERSION_PATCH})
Radek Krejci4f2d40d2015-10-08 12:55:01 +0200101
Claus Klein22091912020-01-20 13:45:47 +0100102 include(FindPackageVersionCheck)
Michal Vasko71514b32020-04-07 13:36:51 +0200103 find_package_version_check(LibSSH DEFAULT_MSG)
Claus Klein22091912020-01-20 13:45:47 +0100104 else()
Radek Krejci4f2d40d2015-10-08 12:55:01 +0200105 message(STATUS "LIBSSH_VERSION_MAJOR not found in ${LIBSSH_INCLUDE_DIR}/libssh/libssh.h, assuming libssh is too old")
106 set(LIBSSH_FOUND FALSE)
Claus Klein22091912020-01-20 13:45:47 +0100107 endif()
108 endif()
109 endif()
Radek Krejci4f2d40d2015-10-08 12:55:01 +0200110
111 # If the version is too old, but libs and includes are set,
112 # find_package_handle_standard_args will set LIBSSH_FOUND to TRUE again,
113 # so we need this if() here.
Claus Klein22091912020-01-20 13:45:47 +0100114 if(LIBSSH_FOUND)
Radek Krejci4f2d40d2015-10-08 12:55:01 +0200115 include(FindPackageHandleStandardArgs)
Michal Vasko71514b32020-04-07 13:36:51 +0200116 find_package_handle_standard_args(LibSSH DEFAULT_MSG LIBSSH_LIBRARY_DIR LIBSSH_INCLUDE_DIRS)
Claus Klein22091912020-01-20 13:45:47 +0100117 endif()
Radek Krejci4f2d40d2015-10-08 12:55:01 +0200118
Michal Vasko71514b32020-04-07 13:36:51 +0200119 # show the LIBSSH_INCLUDE_DIRS, LIBSSH_LIBRARIES, and LIBSSH_LIBRARY_DIR variables only in the advanced view
120 mark_as_advanced(LIBSSH_INCLUDE_DIRS LIBSSH_LIBRARY_DIR LIBSSH_LIBRARIES)
Radek Krejci4f2d40d2015-10-08 12:55:01 +0200121
Claus Klein22091912020-01-20 13:45:47 +0100122endif()
Radek Krejci4f2d40d2015-10-08 12:55:01 +0200123