Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
614
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.72 KB | None | 0 0
  1. # Copyright (c) 2014 Alexander Lamaison <alexander.lamaison@gmail.com>
  2. #
  3. # Redistribution and use in source and binary forms,
  4. # with or without modification, are permitted provided
  5. # that the following conditions are met:
  6. #
  7. # Redistributions of source code must retain the above
  8. # copyright notice, this list of conditions and the
  9. # following disclaimer.
  10. #
  11. # Redistributions in binary form must reproduce the above
  12. # copyright notice, this list of conditions and the following
  13. # disclaimer in the documentation and/or other materials
  14. # provided with the distribution.
  15. #
  16. # Neither the name of the copyright holder nor the names
  17. # of any other contributors may be used to endorse or
  18. # promote products derived from this software without
  19. # specific prior written permission.
  20. #
  21. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  22. # CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  23. # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  24. # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  26. # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  29. # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  31. # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  32. # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  33. # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  34. # OF SUCH DAMAGE.
  35.  
  36. include(CheckFunctionExists)
  37. include(CheckSymbolExists)
  38. include(CheckFunctionExistsMayNeedLibrary)
  39. include(CheckIncludeFiles)
  40. include(CheckTypeSize)
  41. include(CheckSymbolExists)
  42. include(CheckNonblockingSocketSupport)
  43. include(SocketLibraries)
  44.  
  45. ## Cryptography backend choice
  46.  
  47. set(CRYPTO_BACKEND
  48. ""
  49. CACHE
  50. STRING
  51. "The backend to use for cryptography: OpenSSL, Libgcrypt or WinCNG,
  52. or empty to try any available")
  53.  
  54. # If the crypto backend was given, rather than searching for the first
  55. # we are able to find, the find_package commands must abort configuration
  56. # and report to the user.
  57. if(CRYPTO_BACKEND)
  58. set(SPECIFIC_CRYPTO_REQUIREMENT REQUIRED)
  59. endif()
  60.  
  61. if(CRYPTO_BACKEND STREQUAL "OpenSSL" OR NOT CRYPTO_BACKEND)
  62.  
  63. find_package(OpenSSL ${SPECIFIC_CRYPTO_REQUIREMENT})
  64.  
  65. if(OPENSSL_FOUND)
  66. set(CRYPTO_BACKEND "OpenSSL")
  67. set(CRYPTO_SOURCES openssl.c openssl.h)
  68. list(APPEND PRIVATE_COMPILE_DEFINITIONS LIBSSH2_OPENSSL)
  69. list(APPEND PRIVATE_INCLUDE_DIRECTORIES ${OPENSSL_INCLUDE_DIR})
  70. list(APPEND LIBRARIES ${OPENSSL_LIBRARIES})
  71. list(APPEND PC_REQUIRES_PRIVATE libssl libcrypto)
  72.  
  73. if (WIN32)
  74. find_file(DLL_LIBEAY32
  75. NAMES libeay32.dll crypto.dll
  76. HINTS ${_OPENSSL_ROOT_HINTS} PATHS ${_OPENSSL_ROOT_PATHS}
  77. PATH_SUFFIXES bin)
  78. if (NOT DLL_LIBEAY32)
  79. message(WARNING
  80. "Unable to find OpenSSL libeay32 DLL, executables may not run")
  81. endif()
  82.  
  83. find_file(DLL_SSLEAY32
  84. NAMES ssleay32.dll ssl.dll
  85. HINTS ${_OPENSSL_ROOT_HINTS} PATHS ${_OPENSSL_ROOT_PATHS}
  86. PATH_SUFFIXES bin)
  87. if (NOT DLL_SSLEAY32)
  88. message(WARNING
  89. "Unable to find OpenSSL ssleay32 DLL, executables may not run")
  90. endif()
  91.  
  92. if(DLL_LIBEAY32 AND DLL_SSLEAY32)
  93. list(APPEND _RUNTIME_DEPENDENCIES ${DLL_LIBEAY32} ${DLL_SSLEAY32})
  94. endif()
  95. endif()
  96.  
  97. # Not all OpenSSL have AES-CTR functions.
  98. set(SAVE_CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  99. set(CMAKE_REQUIRED_LIBRARIES ${OPENSSL_LIBRARIES})
  100. check_function_exists(EVP_aes_128_ctr HAVE_EVP_AES_128_CTR)
  101. set(CMAKE_REQUIRED_LIBRARIES ${SAVE_CMAKE_REQUIRED_LIBRARIES})
  102. endif()
  103. endif()
  104.  
  105. if(CRYPTO_BACKEND STREQUAL "Libgcrypt" OR NOT CRYPTO_BACKEND)
  106.  
  107. find_package(Libgcrypt ${SPECIFIC_CRYPTO_REQUIREMENT})
  108.  
  109. if(LIBGCRYPT_FOUND)
  110. set(CRYPTO_BACKEND "Libgcrypt")
  111. set(CRYPTO_SOURCES libgcrypt.c libgcrypt.h)
  112. list(APPEND PRIVATE_COMPILE_DEFINITIONS LIBSSH2_LIBGCRYPT)
  113. list(APPEND PRIVATE_INCLUDE_DIRECTORIES ${LIBGCRYPT_INCLUDE_DIRS})
  114. list(APPEND LIBRARIES ${LIBGCRYPT_LIBRARIES})
  115. list(APPEND PC_LIBS -lgcrypt)
  116. endif()
  117. endif()
  118.  
  119. if(CRYPTO_BACKEND STREQUAL "WinCNG" OR NOT CRYPTO_BACKEND)
  120.  
  121. # The check actually compiles the header. This requires windows.h.
  122. check_include_files("windows.h;bcrypt.h" HAVE_BCRYPT_H)
  123.  
  124. if(HAVE_BCRYPT_H)
  125. set(CRYPTO_BACKEND "WinCNG")
  126. set(CRYPTO_SOURCES wincng.c wincng.h)
  127. list(APPEND PRIVATE_COMPILE_DEFINITIONS LIBSSH2_WINCNG)
  128.  
  129. set(HAVE_LIBCRYPT32 TRUE)
  130. list(APPEND LIBRARIES bcrypt)
  131. list(APPEND PC_LIBS -lbcrypt)
  132.  
  133. check_include_files(ntdef.h HAVE_NTDEF_H)
  134. check_include_files(ntstatus.h HAVE_NTSTATUS_H)
  135.  
  136. # Reading keys from files is optional and depends on Wincrypt
  137. check_include_files("windows.h;wincrypt.h" HAVE_WINCRYPT_H)
  138.  
  139. if(HAVE_WINCRYPT_H)
  140. list(APPEND LIBRARIES crypt32)
  141. list(APPEND PC_LIBS -lcrypt32)
  142. endif()
  143.  
  144. elseif(${SPECIFIC_CRYPTO_REQUIREMENT} STREQUAL ${REQUIRED})
  145. message(FATAL_ERROR "WinCNG not available")
  146. endif()
  147. endif()
  148.  
  149. if(NOT CRYPTO_BACKEND)
  150. message(FATAL_ERROR "No suitable cryptography backend found.")
  151. endif()
  152.  
  153. ## Library definition
  154.  
  155. include(GNUInstallDirs)
  156. set(SOURCES
  157. ${CRYPTO_SOURCES}
  158. agent.c
  159. channel.c
  160. channel.h
  161. comp.c
  162. comp.h
  163. crypt.c
  164. crypto.h
  165. global.c
  166. hostkey.c
  167. keepalive.c
  168. kex.c
  169. knownhost.c
  170. libssh2_priv.h
  171. mac.c
  172. mac.h
  173. misc.c
  174. misc.h
  175. packet.c
  176. packet.h
  177. pem.c
  178. publickey.c
  179. scp.c
  180. session.c
  181. session.h
  182. sftp.c
  183. sftp.h
  184. transport.c
  185. transport.h
  186. userauth.c
  187. userauth.h
  188. version.c)
  189.  
  190. if(WIN32)
  191. list(APPEND SOURCES ${PROJECT_SOURCE_DIR}/win32/libssh2.rc)
  192. endif()
  193.  
  194. add_library(libssh2 ${SOURCES})
  195. # we want it to be called libssh2 on all platforms
  196. set_target_properties(libssh2 PROPERTIES PREFIX "")
  197.  
  198. target_compile_definitions(libssh2 PRIVATE ${PRIVATE_COMPILE_DEFINITIONS})
  199. target_include_directories(libssh2
  200. PRIVATE ${PRIVATE_INCLUDE_DIRECTORIES}
  201. PUBLIC
  202. $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
  203. $<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>)
  204.  
  205. ## Options
  206.  
  207. add_feature_info("Shared library" BUILD_SHARED_LIBS
  208. "creating libssh2 as a shared library (.so/.dll)")
  209.  
  210. option(ENABLE_ZLIB_COMPRESSION "Use zlib for compression")
  211. add_feature_info(Compression ENABLE_ZLIB_COMPRESSION
  212. "using zlib for compression")
  213. if(ENABLE_ZLIB_COMPRESSION)
  214. find_package(ZLIB REQUIRED)
  215.  
  216. target_include_directories(libssh2 PRIVATE ${ZLIB_INCLUDE_DIRS})
  217. list(APPEND LIBRARIES ${ZLIB_LIBRARIES})
  218. list(APPEND PC_REQUIRES_PRIVATE zlib)
  219. if(ZLIB_FOUND)
  220. target_compile_definitions(libssh2 PRIVATE LIBSSH2_HAVE_ZLIB=1)
  221. endif()
  222. endif()
  223.  
  224. option(ENABLE_CRYPT_NONE "Permit \"none\" cipher -- NOT RECOMMENDED")
  225. add_feature_info("\"none\" cipher" ENABLE_CRYPT_NONE "")
  226. if(ENABLE_CRYPT_NONE)
  227. target_compile_definitions(libssh2 PRIVATE LIBSSH2_CRYPT_NONE=1)
  228. endif()
  229.  
  230. option(ENABLE_MAC_NONE "Permit \"none\" MAC -- NOT RECOMMMENDED")
  231. add_feature_info("\"none\" MAC" ENABLE_MAC_NONE "")
  232. if(ENABLE_MAC_NONE)
  233. target_compile_definitions(libssh2 PRIVATE LIBSSH2_MAC_NONE=1)
  234. endif()
  235.  
  236. option(ENABLE_GEX_NEW
  237. "Enable diffie-hellman-group-exchange-sha1 method" ON)
  238. add_feature_info("diffie-hellman-group-exchange-sha1" ENABLE_GEX_NEW
  239. "\"new\" diffie-hellman-group-exchange-sha1 method")
  240. if(ENABLE_GEX_NEW)
  241. target_compile_definitions(libssh2 PRIVATE LIBSSH2_DH_GEX_NEW=1)
  242. endif()
  243.  
  244. # Enable debugging logging by default if the user configured a debug build
  245. if(CMAKE_BUILD_TYPE STREQUAL "Debug")
  246. set(DEBUG_LOGGING_DEFAULT ON)
  247. else()
  248. set(DEBUG_LOGGING_DEFAULT OFF)
  249. endif()
  250. option(ENABLE_DEBUG_LOGGING "log execution with debug trace"
  251. ${DEBUG_LOGGING_DEFAULT})
  252. add_feature_info(Logging ENABLE_DEBUG_LOGGING
  253. "Logging of execution with debug trace")
  254. if(ENABLE_DEBUG_LOGGING)
  255. target_compile_definitions(libssh2 PRIVATE LIBSSH2DEBUG)
  256. endif()
  257.  
  258. ## Platform checks
  259. check_include_files(unistd.h HAVE_UNISTD_H)
  260. check_include_files(inttypes.h HAVE_INTTYPES_H)
  261. check_include_files(stdlib.h HAVE_STDLIB_H)
  262. check_include_files(sys/select.h HAVE_SYS_SELECT_H)
  263.  
  264. check_include_files(sys/uio.h HAVE_SYS_UIO_H)
  265. check_include_files(sys/socket.h HAVE_SYS_SOCKET_H)
  266. check_include_files(sys/ioctl.h HAVE_SYS_IOCTL_H)
  267. check_include_files(sys/time.h HAVE_SYS_TIME_H)
  268. check_include_files(sys/un.h HAVE_SYS_UN_H)
  269. check_include_files(windows.h HAVE_WINDOWS_H)
  270. check_include_files(ws2tcpip.h HAVE_WS2TCPIP_H)
  271. check_include_files(winsock2.h HAVE_WINSOCK2_H)
  272.  
  273. check_type_size("long long" LONGLONG)
  274.  
  275. if(HAVE_SYS_TIME_H)
  276. check_symbol_exists(gettimeofday sys/time.h HAVE_GETTIMEOFDAY)
  277. else()
  278. check_function_exists(gettimeofday HAVE_GETTIMEOFDAY)
  279. endif()
  280. if(HAVE_STDLIB_H)
  281. check_symbol_exists(strtoll stdlib.h HAVE_STRTOLL)
  282. else()
  283. check_function_exists(strtoll HAVE_STRTOLL)
  284. endif()
  285. if (NOT HAVE_STRTOLL)
  286. # Try _strtoi64 if strtoll isn't available
  287. check_symbol_exists(_strtoi64 stdlib.h HAVE_STRTOI64)
  288. endif()
  289. check_symbol_exists(snprintf stdio.h HAVE_SNPRINTF)
  290.  
  291. if(${CMAKE_SYSTEM_NAME} STREQUAL "Darwin" OR
  292. ${CMAKE_SYSTEM_NAME} STREQUAL "Interix")
  293. # poll() does not work on these platforms
  294. #
  295. # Interix: "does provide poll(), but the implementing developer must
  296. # have been in a bad mood, because poll() only works on the /proc
  297. # filesystem here"
  298. #
  299. # Mac OS X's poll has funny behaviors, like:
  300. # not being able to do poll on no fildescriptors (10.3?)
  301. # not being able to poll on some files (like anything in /dev)
  302. # not having reliable timeout support
  303. # inconsistent return of POLLHUP where other implementations give POLLIN
  304. message("poll use is disabled on this platform")
  305. else()
  306. check_function_exists(poll HAVE_POLL)
  307. endif()
  308.  
  309. append_needed_socket_libraries(LIBRARIES)
  310.  
  311. # Non-blocking socket support tests. Must be after after library tests to
  312. # link correctly
  313. set(SAVE_CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  314. set(CMAKE_REQUIRED_LIBRARIES ${LIBRARIES})
  315. check_nonblocking_socket_support()
  316. set(CMAKE_REQUIRED_LIBRARIES ${SAVE_CMAKE_REQUIRED_LIBRARIES})
  317.  
  318. configure_file(
  319. ${CMAKE_CURRENT_SOURCE_DIR}/libssh2_config_cmake.h.in
  320. ${CMAKE_CURRENT_BINARY_DIR}/libssh2_config.h)
  321. # to find generated header
  322. target_include_directories(libssh2 PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
  323.  
  324. # Check for the OS.
  325. # Daniel's note: this should not be necessary and we need to work to
  326. # get this removed.
  327. if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
  328. target_compile_definitions(libssh2 PRIVATE LIBSSH2_WIN32)
  329. elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
  330. target_compile_definitions(libssh2 PRIVATE LIBSSH2_DARWIN)
  331. endif()
  332.  
  333. if(CMAKE_VERSION VERSION_LESS "2.8.12")
  334. # Fall back to over-linking dependencies
  335. target_link_libraries(libssh2 ${LIBRARIES})
  336. else()
  337. target_link_libraries(libssh2 PRIVATE ${LIBRARIES})
  338. endif()
  339.  
  340. ## Installation
  341.  
  342. install(FILES
  343. ${PROJECT_SOURCE_DIR}/include/libssh2.h
  344. ${PROJECT_SOURCE_DIR}/include/libssh2_publickey.h
  345. ${PROJECT_SOURCE_DIR}/include/libssh2_sftp.h
  346. DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
  347.  
  348. install(TARGETS libssh2
  349. EXPORT Libssh2Config
  350. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  351. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  352. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
  353.  
  354. if(BUILD_SHARED_LIBS)
  355. list(APPEND _RUNTIME_DEPENDENCIES $<TARGET_FILE:libssh2>)
  356. endif()
  357.  
  358. set(RUNTIME_DEPENDENCIES ${_RUNTIME_DEPENDENCIES} CACHE INTERNAL
  359. "Files that must be in the same directory as the executables at runtime.")
  360.  
  361. # Package config
  362.  
  363. ## During package installation, install Libssh2Config.cmake
  364. install(EXPORT Libssh2Config
  365. NAMESPACE Libssh2::
  366. DESTINATION lib/cmake/libssh2)
  367.  
  368. ## During build, register directly from build tree
  369. # create Libssh2Config.cmake
  370. export(TARGETS libssh2 NAMESPACE Libssh2:: FILE Libssh2Config.cmake)
  371. export(PACKAGE Libssh2) # register it
  372.  
  373. ## Export a .pc file for client projects not using CMaek
  374. if(PC_REQUIRES_PRIVATE)
  375. string(REPLACE ";" "," PC_REQUIRES_PRIVATE "${PC_REQUIRES_PRIVATE}")
  376. endif()
  377. if(PC_LIBS)
  378. string(REPLACE ";" " " PC_LIBS "${PC_LIBS}")
  379. endif()
  380. configure_file(libssh2.pc.in libssh2.pc @ONLY)
  381. install(
  382. FILES ${CMAKE_CURRENT_BINARY_DIR}/libssh2.pc
  383. DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
  384.  
  385. ## Versioning
  386.  
  387. set_target_properties(libssh2 PROPERTIES
  388. SOVERSION 1
  389. VERSION 1.0.1)
  390.  
  391. include(CMakePackageConfigHelpers)
  392. write_basic_package_version_file(
  393. ${CMAKE_CURRENT_BINARY_DIR}/Libssh2ConfigVersion.cmake
  394. VERSION "${LIBSSH2_VERSION_MAJOR}.${LIBSSH2_VERSION_MINOR}.${LIBSSH2_VERSION_PATCH}"
  395. COMPATIBILITY SameMajorVersion)
  396. install(
  397. FILES ${CMAKE_CURRENT_BINARY_DIR}/Libssh2ConfigVersion.cmake
  398. DESTINATION lib/cmake/libssh2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement