blob: cdaae38ae498a2ad711ac465a2f533ea33d8ca98 [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 Vasko71514b32020-04-07 13:36:51 +02006# LIBSSH_LIBRARIES - link these to use LibSSH
Michal Vaskoed4a4522020-05-19 16:46:12 +02007# LIBSSH_VERSION -
Radek Krejci4f2d40d2015-10-08 12:55:01 +02008#
Michal Vaskoed4a4522020-05-19 16:46:12 +02009# Author Michal Vasko <mvasko@cesnet.cz>
10# Copyright (c) 2020 CESNET, z.s.p.o.
Radek Krejci4f2d40d2015-10-08 12:55:01 +020011#
12# Redistribution and use in source and binary forms, with or without
13# modification, are permitted provided that the following conditions
14# are met:
15#
16# 1. Redistributions of source code must retain the copyright
17# notice, this list of conditions and the following disclaimer.
18# 2. Redistributions in binary form must reproduce the copyright
19# notice, this list of conditions and the following disclaimer in the
20# documentation and/or other materials provided with the distribution.
Michal Vasko75441e72017-09-27 13:57:00 +020021# 3. The name of the author may not be used to endorse or promote products
Radek Krejci4f2d40d2015-10-08 12:55:01 +020022# derived from this software without specific prior written permission.
23#
24# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34#
Michal Vaskoed4a4522020-05-19 16:46:12 +020035include(FindPackageHandleStandardArgs)
Radek Krejci4f2d40d2015-10-08 12:55:01 +020036
Michal Vaskoed4a4522020-05-19 16:46:12 +020037if(LIBSSH_LIBRARIES AND LIBSSH_INCLUDE_DIRS)
38 # in cache already
39 set(LIBSSH_FOUND TRUE)
Claus Klein22091912020-01-20 13:45:47 +010040else()
Michal Vaskoed4a4522020-05-19 16:46:12 +020041 find_path(LIBSSH_INCLUDE_DIR
42 NAMES
43 libssh/libssh.h
44 PATHS
45 /usr/include
46 /usr/local/include
47 /opt/local/include
48 /sw/include
49 ${CMAKE_INCLUDE_PATH}
50 ${CMAKE_INSTALL_PREFIX}/include
Michal Vasko75441e72017-09-27 13:57:00 +020051 )
Radek Krejci4f2d40d2015-10-08 12:55:01 +020052
Michal Vaskoed4a4522020-05-19 16:46:12 +020053 find_library(LIBSSH_LIBRARY
54 NAMES
55 ssh.so
56 libssh.so
57 libssh.dylib
58 PATHS
59 /usr/lib
60 /usr/local/lib
61 /opt/local/lib
62 /sw/lib
63 ${CMAKE_LIBRARY_PATH}
64 ${CMAKE_INSTALL_PREFIX}/lib
65 )
Radek Krejci4f2d40d2015-10-08 12:55:01 +020066
Michal Vaskoed4a4522020-05-19 16:46:12 +020067 if(LIBSSH_INCLUDE_DIR AND LIBSSH_LIBRARY)
68 # learn libssh version
69 if(EXISTS ${LIBSSH_INCLUDE_DIR}/libssh/libssh_version.h)
70 set(LIBSSH_HEADER_PATH ${LIBSSH_INCLUDE_DIR}/libssh/libssh_version.h)
71 else()
72 set(LIBSSH_HEADER_PATH ${LIBSSH_INCLUDE_DIR}/libssh/libssh.h)
73 endif()
74 file(STRINGS ${LIBSSH_HEADER_PATH} LIBSSH_VERSION_MAJOR
75 REGEX "#define[ ]+LIBSSH_VERSION_MAJOR[ ]+[0-9]+")
76 if(NOT LIBSSH_VERSION_MAJOR)
77 message(STATUS "LIBSSH_VERSION_MAJOR not found, assuming libssh is too old and cannot be used!")
78 set(LIBSSH_INCLUDE_DIR "LIBSSH_INCLUDE_DIR-NOTFOUND")
79 set(LIBSSH_LIBRARY "LIBSSH_LIBRARY-NOTFOUND")
80 else()
81 string(REGEX MATCH "[0-9]+" LIBSSH_VERSION_MAJOR ${LIBSSH_VERSION_MAJOR})
82 file(STRINGS ${LIBSSH_HEADER_PATH} LIBSSH_VERSION_MINOR
83 REGEX "#define[ ]+LIBSSH_VERSION_MINOR[ ]+[0-9]+")
84 string(REGEX MATCH "[0-9]+" LIBSSH_VERSION_MINOR ${LIBSSH_VERSION_MINOR})
85 file(STRINGS ${LIBSSH_HEADER_PATH} LIBSSH_VERSION_PATCH
86 REGEX "#define[ ]+LIBSSH_VERSION_MICRO[ ]+[0-9]+")
87 string(REGEX MATCH "[0-9]+" LIBSSH_VERSION_PATCH ${LIBSSH_VERSION_PATCH})
Radek Krejci4f2d40d2015-10-08 12:55:01 +020088
Michal Vaskoed4a4522020-05-19 16:46:12 +020089 set(LIBSSH_VERSION ${LIBSSH_VERSION_MAJOR}.${LIBSSH_VERSION_MINOR}.${LIBSSH_VERSION_PATCH})
90
91 if(LIBSSH_VERSION VERSION_LESS 0.8.0)
92 # libssh_threads also needs to be linked for these versions
93 string(REPLACE "libssh.so" ""
94 LIBSSH_THREADS_LIBRARY
95 ${LIBSSH_LIBRARY}
96 )
97 string(REPLACE "libssh.dylib" ""
98 LIBSSH_THREADS_LIBRARY
99 ${LIBSSH_THREADS_LIBRARY}
100 )
101 string(REPLACE "ssh.so" ""
102 LIBSSH_THREADS_LIBRARY
103 ${LIBSSH_THREADS_LIBRARY}
104 )
105 endif()
106 endif()
Claus Klein22091912020-01-20 13:45:47 +0100107 endif()
Radek Krejci4f2d40d2015-10-08 12:55:01 +0200108
Michal Vaskoed4a4522020-05-19 16:46:12 +0200109 set(LIBSSH_INCLUDE_DIRS ${LIBSSH_INCLUDE_DIR})
110 set(LIBSSH_LIBRARIES ${LIBSSH_LIBRARY} ${LIBSSH_THREADS_LIBRARY})
111 mark_as_advanced(LIBSSH_INCLUDE_DIRS LIBSSH_LIBRARIES)
Radek Krejci4f2d40d2015-10-08 12:55:01 +0200112
Michal Vaskoed4a4522020-05-19 16:46:12 +0200113 find_package_handle_standard_args(LibSSH FOUND_VAR LIBSSH_FOUND
114 REQUIRED_VARS LIBSSH_INCLUDE_DIRS LIBSSH_LIBRARIES
115 VERSION_VAR LIBSSH_VERSION)
Claus Klein22091912020-01-20 13:45:47 +0100116endif()
Radek Krejci4f2d40d2015-10-08 12:55:01 +0200117