blob: 6578aa99761629555e1e230bbe8b0d50a3c2b072 [file] [log] [blame]
Austin Schuh75263e32022-02-22 18:05:32 -08001# - 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
6# LIBSSH_LIBRARIES - link these to use LibSSH
7# LIBSSH_VERSION -
8#
9# Author Michal Vasko <mvasko@cesnet.cz>
10# Copyright (c) 2020 CESNET, z.s.p.o.
11#
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.
21# 3. The name of the author may not be used to endorse or promote products
22# 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#
35include(FindPackageHandleStandardArgs)
36
37if(LIBSSH_LIBRARIES AND LIBSSH_INCLUDE_DIRS)
38 # in cache already
39 set(LIBSSH_FOUND TRUE)
40else()
Maxwell Henderson80bec322024-01-09 15:48:44 -080041 find_path(
42 LIBSSH_INCLUDE_DIR
43 NAMES libssh/libssh.h
Austin Schuh75263e32022-02-22 18:05:32 -080044 PATHS
Maxwell Henderson80bec322024-01-09 15:48:44 -080045 /usr/include
46 /usr/local/include
47 /opt/local/include
48 /sw/include
49 ${CMAKE_INCLUDE_PATH}
50 ${CMAKE_INSTALL_PREFIX}/include
Austin Schuh75263e32022-02-22 18:05:32 -080051 )
52
Maxwell Henderson80bec322024-01-09 15:48:44 -080053 find_library(
54 LIBSSH_LIBRARY
55 NAMES ssh.so libssh.so libssh.dylib
Austin Schuh75263e32022-02-22 18:05:32 -080056 PATHS
Maxwell Henderson80bec322024-01-09 15:48:44 -080057 /usr/lib
58 /usr/local/lib
59 /opt/local/lib
60 /sw/lib
61 ${CMAKE_LIBRARY_PATH}
62 ${CMAKE_INSTALL_PREFIX}/lib
Austin Schuh75263e32022-02-22 18:05:32 -080063 )
64
65 if(LIBSSH_INCLUDE_DIR AND LIBSSH_LIBRARY)
66 # learn libssh version
67 if(EXISTS ${LIBSSH_INCLUDE_DIR}/libssh/libssh_version.h)
68 set(LIBSSH_HEADER_PATH ${LIBSSH_INCLUDE_DIR}/libssh/libssh_version.h)
69 else()
70 set(LIBSSH_HEADER_PATH ${LIBSSH_INCLUDE_DIR}/libssh/libssh.h)
71 endif()
Maxwell Henderson80bec322024-01-09 15:48:44 -080072 file(
73 STRINGS
74 ${LIBSSH_HEADER_PATH}
75 LIBSSH_VERSION_MAJOR
76 REGEX "#define[ ]+LIBSSH_VERSION_MAJOR[ ]+[0-9]+"
77 )
Austin Schuh75263e32022-02-22 18:05:32 -080078 if(NOT LIBSSH_VERSION_MAJOR)
Maxwell Henderson80bec322024-01-09 15:48:44 -080079 message(
80 STATUS
81 "LIBSSH_VERSION_MAJOR not found, assuming libssh is too old and cannot be used!"
82 )
Austin Schuh75263e32022-02-22 18:05:32 -080083 set(LIBSSH_INCLUDE_DIR "LIBSSH_INCLUDE_DIR-NOTFOUND")
84 set(LIBSSH_LIBRARY "LIBSSH_LIBRARY-NOTFOUND")
85 else()
86 string(REGEX MATCH "[0-9]+" LIBSSH_VERSION_MAJOR ${LIBSSH_VERSION_MAJOR})
Maxwell Henderson80bec322024-01-09 15:48:44 -080087 file(
88 STRINGS
89 ${LIBSSH_HEADER_PATH}
90 LIBSSH_VERSION_MINOR
91 REGEX "#define[ ]+LIBSSH_VERSION_MINOR[ ]+[0-9]+"
92 )
Austin Schuh75263e32022-02-22 18:05:32 -080093 string(REGEX MATCH "[0-9]+" LIBSSH_VERSION_MINOR ${LIBSSH_VERSION_MINOR})
Maxwell Henderson80bec322024-01-09 15:48:44 -080094 file(
95 STRINGS
96 ${LIBSSH_HEADER_PATH}
97 LIBSSH_VERSION_PATCH
98 REGEX "#define[ ]+LIBSSH_VERSION_MICRO[ ]+[0-9]+"
99 )
Austin Schuh75263e32022-02-22 18:05:32 -0800100 string(REGEX MATCH "[0-9]+" LIBSSH_VERSION_PATCH ${LIBSSH_VERSION_PATCH})
101
Maxwell Henderson80bec322024-01-09 15:48:44 -0800102 set(LIBSSH_VERSION
103 ${LIBSSH_VERSION_MAJOR}.${LIBSSH_VERSION_MINOR}.${LIBSSH_VERSION_PATCH}
104 )
Austin Schuh75263e32022-02-22 18:05:32 -0800105
106 if(LIBSSH_VERSION VERSION_LESS 0.8.0)
107 # libssh_threads also needs to be linked for these versions
Maxwell Henderson80bec322024-01-09 15:48:44 -0800108 string(
109 REPLACE
110 "libssh.so"
111 "libssh_threads.so"
Austin Schuh75263e32022-02-22 18:05:32 -0800112 LIBSSH_THREADS_LIBRARY
113 ${LIBSSH_LIBRARY}
114 )
Maxwell Henderson80bec322024-01-09 15:48:44 -0800115 string(
116 REPLACE
117 "libssh.dylib"
118 "libssh_threads.dylib"
Austin Schuh75263e32022-02-22 18:05:32 -0800119 LIBSSH_THREADS_LIBRARY
120 ${LIBSSH_THREADS_LIBRARY}
121 )
Maxwell Henderson80bec322024-01-09 15:48:44 -0800122 string(
123 REPLACE
124 "ssh.so"
125 "ssh_threads.so"
Austin Schuh75263e32022-02-22 18:05:32 -0800126 LIBSSH_THREADS_LIBRARY
127 ${LIBSSH_THREADS_LIBRARY}
128 )
129 endif()
130 endif()
131 endif()
132
133 set(LIBSSH_INCLUDE_DIRS ${LIBSSH_INCLUDE_DIR})
134 set(LIBSSH_LIBRARIES ${LIBSSH_LIBRARY} ${LIBSSH_THREADS_LIBRARY})
135 mark_as_advanced(LIBSSH_INCLUDE_DIRS LIBSSH_LIBRARIES)
136
Maxwell Henderson80bec322024-01-09 15:48:44 -0800137 find_package_handle_standard_args(
138 LIBSSH
139 FOUND_VAR LIBSSH_FOUND
Austin Schuh75263e32022-02-22 18:05:32 -0800140 REQUIRED_VARS LIBSSH_INCLUDE_DIRS LIBSSH_LIBRARIES
Maxwell Henderson80bec322024-01-09 15:48:44 -0800141 VERSION_VAR LIBSSH_VERSION
142 )
Austin Schuh75263e32022-02-22 18:05:32 -0800143endif()