blob: 90c65d2b62b18fe2955ffc2b3ead4d080c87c198 [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
6# LIBSSH_LIBRARIES - Link these to use LibSSH
7# LIBSSH_DEFINITIONS - Compiler switches required for using LibSSH
8#
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.
20# 3. The name of the author may not be used to endorse or promote products
21# 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
35if (LIBSSH_LIBRARIES AND LIBSSH_INCLUDE_DIRS)
36 # in cache already
37 set(LIBSSH_FOUND TRUE)
38else (LIBSSH_LIBRARIES AND LIBSSH_INCLUDE_DIRS)
39
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 )
51
52 find_library(SSH_LIBRARY
53 NAMES
54 ssh
55 libssh
56 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
65 if (LIBSSH_INCLUDE_DIR AND SSH_LIBRARY)
66 set(SSH_FOUND TRUE)
67 endif (LIBSSH_INCLUDE_DIR AND SSH_LIBRARY)
68
69 set(LIBSSH_INCLUDE_DIRS
70 ${LIBSSH_INCLUDE_DIR}
71 )
72
73 if (SSH_FOUND)
74 set(LIBSSH_LIBRARIES
75 ${LIBSSH_LIBRARIES}
76 ${SSH_LIBRARY}
77 )
78
79 if (LibSSH_FIND_VERSION)
80 file(STRINGS ${LIBSSH_INCLUDE_DIR}/libssh/libssh.h LIBSSH_VERSION_MAJOR
81 REGEX "#define[ ]+LIBSSH_VERSION_MAJOR[ ]+[0-9]+")
82 # Older versions of libssh like libssh-0.2 have LIBSSH_VERSION but not LIBSSH_VERSION_MAJOR
83 if (LIBSSH_VERSION_MAJOR)
84 string(REGEX MATCH "[0-9]+" LIBSSH_VERSION_MAJOR ${LIBSSH_VERSION_MAJOR})
85 file(STRINGS ${LIBSSH_INCLUDE_DIR}/libssh/libssh.h LIBSSH_VERSION_MINOR
86 REGEX "#define[ ]+LIBSSH_VERSION_MINOR[ ]+[0-9]+")
87 string(REGEX MATCH "[0-9]+" LIBSSH_VERSION_MINOR ${LIBSSH_VERSION_MINOR})
88 file(STRINGS ${LIBSSH_INCLUDE_DIR}/libssh/libssh.h LIBSSH_VERSION_PATCH
89 REGEX "#define[ ]+LIBSSH_VERSION_MICRO[ ]+[0-9]+")
90 string(REGEX MATCH "[0-9]+" LIBSSH_VERSION_PATCH ${LIBSSH_VERSION_PATCH})
91
92 set(LibSSH_VERSION ${LIBSSH_VERSION_MAJOR}.${LIBSSH_VERSION_MINOR}.${LIBSSH_VERSION_PATCH})
93
94 include(FindPackageVersionCheck)
95 find_package_version_check(LibSSH DEFAULT_MSG)
96 else (LIBSSH_VERSION_MAJOR)
97 message(STATUS "LIBSSH_VERSION_MAJOR not found in ${LIBSSH_INCLUDE_DIR}/libssh/libssh.h, assuming libssh is too old")
98 set(LIBSSH_FOUND FALSE)
99 endif (LIBSSH_VERSION_MAJOR)
100 endif (LibSSH_FIND_VERSION)
101 endif (SSH_FOUND)
102
103 # If the version is too old, but libs and includes are set,
104 # find_package_handle_standard_args will set LIBSSH_FOUND to TRUE again,
105 # so we need this if() here.
106 if (LIBSSH_FOUND)
107 include(FindPackageHandleStandardArgs)
108 find_package_handle_standard_args(LibSSH DEFAULT_MSG LIBSSH_LIBRARIES LIBSSH_INCLUDE_DIRS)
109 endif (LIBSSH_FOUND)
110
111 # show the LIBSSH_INCLUDE_DIRS and LIBSSH_LIBRARIES variables only in the advanced view
112 mark_as_advanced(LIBSSH_INCLUDE_DIRS LIBSSH_LIBRARIES)
113
114endif (LIBSSH_LIBRARIES AND LIBSSH_INCLUDE_DIRS)
115