blob: 49737ff24a049d84539651729c1edd8a7cf43b8e [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
Michal Vasko75441e72017-09-27 13:57:00 +020034if (LIBSSH_LIBRARY_DIR AND LIBSSH_INCLUDE_DIRS)
Radek Krejci4f2d40d2015-10-08 12:55:01 +020035 # in cache already
36 set(LIBSSH_FOUND TRUE)
Michal Vasko75441e72017-09-27 13:57:00 +020037else (LIBSSH_LIBRARY_DIR AND LIBSSH_INCLUDE_DIRS)
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
Radek Krejci4f2d40d2015-10-08 12:55:01 +020051 find_library(SSH_LIBRARY
52 NAMES
Michal Vasko75441e72017-09-27 13:57:00 +020053 ssh.so
54 libssh.so
Radek Krejci4f2d40d2015-10-08 12:55:01 +020055 PATHS
56 /usr/lib
57 /usr/local/lib
58 /opt/local/lib
59 /sw/lib
60 ${CMAKE_LIBRARY_PATH}
61 ${CMAKE_INSTALL_PREFIX}/lib
62 )
63
64 if (LIBSSH_INCLUDE_DIR AND SSH_LIBRARY)
65 set(SSH_FOUND TRUE)
66 endif (LIBSSH_INCLUDE_DIR AND SSH_LIBRARY)
67
68 set(LIBSSH_INCLUDE_DIRS
69 ${LIBSSH_INCLUDE_DIR}
70 )
71
72 if (SSH_FOUND)
Chaitanya Tataf2a28fc2019-04-23 13:46:40 +053073 string(REPLACE "libssh.so" ""
Michal Vasko75441e72017-09-27 13:57:00 +020074 LIBSSH_LIBRARY_DIR
Radek Krejci4f2d40d2015-10-08 12:55:01 +020075 ${SSH_LIBRARY}
76 )
Chaitanya Tataf2a28fc2019-04-23 13:46:40 +053077 string(REPLACE "ssh.so" ""
Michal Vasko75441e72017-09-27 13:57:00 +020078 LIBSSH_LIBRARY_DIR
79 ${LIBSSH_LIBRARY_DIR}
80 )
Radek Krejci4f2d40d2015-10-08 12:55:01 +020081
82 if (LibSSH_FIND_VERSION)
83 file(STRINGS ${LIBSSH_INCLUDE_DIR}/libssh/libssh.h LIBSSH_VERSION_MAJOR
84 REGEX "#define[ ]+LIBSSH_VERSION_MAJOR[ ]+[0-9]+")
85 # Older versions of libssh like libssh-0.2 have LIBSSH_VERSION but not LIBSSH_VERSION_MAJOR
86 if (LIBSSH_VERSION_MAJOR)
87 string(REGEX MATCH "[0-9]+" LIBSSH_VERSION_MAJOR ${LIBSSH_VERSION_MAJOR})
88 file(STRINGS ${LIBSSH_INCLUDE_DIR}/libssh/libssh.h LIBSSH_VERSION_MINOR
89 REGEX "#define[ ]+LIBSSH_VERSION_MINOR[ ]+[0-9]+")
90 string(REGEX MATCH "[0-9]+" LIBSSH_VERSION_MINOR ${LIBSSH_VERSION_MINOR})
91 file(STRINGS ${LIBSSH_INCLUDE_DIR}/libssh/libssh.h LIBSSH_VERSION_PATCH
92 REGEX "#define[ ]+LIBSSH_VERSION_MICRO[ ]+[0-9]+")
93 string(REGEX MATCH "[0-9]+" LIBSSH_VERSION_PATCH ${LIBSSH_VERSION_PATCH})
94
95 set(LibSSH_VERSION ${LIBSSH_VERSION_MAJOR}.${LIBSSH_VERSION_MINOR}.${LIBSSH_VERSION_PATCH})
96
97 include(FindPackageVersionCheck)
98 find_package_version_check(LibSSH DEFAULT_MSG)
99 else (LIBSSH_VERSION_MAJOR)
100 message(STATUS "LIBSSH_VERSION_MAJOR not found in ${LIBSSH_INCLUDE_DIR}/libssh/libssh.h, assuming libssh is too old")
101 set(LIBSSH_FOUND FALSE)
102 endif (LIBSSH_VERSION_MAJOR)
103 endif (LibSSH_FIND_VERSION)
104 endif (SSH_FOUND)
105
106 # If the version is too old, but libs and includes are set,
107 # find_package_handle_standard_args will set LIBSSH_FOUND to TRUE again,
108 # so we need this if() here.
109 if (LIBSSH_FOUND)
110 include(FindPackageHandleStandardArgs)
Michal Vasko75441e72017-09-27 13:57:00 +0200111 find_package_handle_standard_args(LibSSH DEFAULT_MSG LIBSSH_LIBRARY_DIR LIBSSH_INCLUDE_DIRS)
Radek Krejci4f2d40d2015-10-08 12:55:01 +0200112 endif (LIBSSH_FOUND)
113
Michal Vasko75441e72017-09-27 13:57:00 +0200114 # show the LIBSSH_INCLUDE_DIRS and LIBSSH_LIBRARY_DIR variables only in the advanced view
115 mark_as_advanced(LIBSSH_INCLUDE_DIRS LIBSSH_LIBRARY_DIR)
Radek Krejci4f2d40d2015-10-08 12:55:01 +0200116
Michal Vasko75441e72017-09-27 13:57:00 +0200117endif (LIBSSH_LIBRARY_DIR AND LIBSSH_INCLUDE_DIRS)
Radek Krejci4f2d40d2015-10-08 12:55:01 +0200118