Added cmake
[amule.git] / cmake / cryptopp.cmake
blob8611f179b3b2fe318a20770023853f97438c08c4
1 add_library (CRYPTOPP::CRYPTOPP
2         UNKNOWN
3         IMPORTED
6 set (CRYPTOPP_SEARCH_PREFIXES "cryptopp" "crypto++")
8 if (NOT CRYPTOPP_INCLUDE_PREFIX)
9         unset (CRYPT_SEARCH CACHE)
10         check_include_file_cxx (cryptlib.h CRYPT_SEARCH)
12         if (CRYPT_SEARCH)
13                 set (CRYPTOPP_INCLUDE_PREFIX "" CACHE STRING "cryptopp include prefix" FORCE)
14         else()
15                 foreach (PREFIX ${CRYPTOPP_SEARCH_PREFIXES})
16                         unset (CRYPT_SEARCH CACHE)
17                         check_include_file_cxx (${PREFIX}/cryptlib.h CRYPT_SEARCH)
19                         if (CRYPT_SEARCH)
20                                 message (STATUS "cryptopp prefix found: ${PREFIX}")
21                                 set (CRYPTOPP_INCLUDE_PREFIX ${PREFIX} CACHE STRING "cryptopp include prefix" FORCE)
22                                 break()
23                         endif()
24                 endforeach()
25         endif()
26 endif()
28 if (NOT CRYPTOPP_INCLUDE_PREFIX)
29         MESSAGE (FATAL_ERROR "cryptlib.h not found")
30 endif()
32 if (WIN32)
33         if (NOT CRYPTOPP_LIBRARY_DEBUG)
34                 unset (CRYPTOPP_LIBRARY_DEBUG CACHE)
36                 find_library (CRYPTOPP_LIBRARY_DEBUG
37                         NAMES crypto++d cryptlibd cryptoppd
38                         PATHS ${CRYPTOPP_LIB_SEARCH_PATH}
39                 )
41                 if (CRYPTOPP_LIBRARY_DEBUG)
42                         message (STATUS "Found debug-libcrypto++ in ${CRYPTOPP_LIBRARY_DEBUG}")
43                 endif()
44         endif()
46         if (CRYPTOPP_LIBRARY_DEBUG)
47                 set_property (TARGET CRYPTOPP::CRYPTOPP
48                         PROPERTY IMPORTED_LOCATION_DEBUG ${CRYPTOPP_LIBRARY_DEBUG}
49                 )
50         else()
51                 set (CRYPTO_COMPLETE FALSE)
52         endif()
54         if (NOT CRYPTOPP_LIBRARY_RELEASE)
55                 unset (CRYPTOPP_LIBRARY_RELEASE CACHE)
57                 find_library (CRYPTOPP_LIBRARY_RELEASE
58                         NAMES crypto++ cryptlib cryptopp
59                         PATHS ${CRYPTOPP_LIB_SEARCH_PATH}
60                 )
62                 if (CRYPTOPP_LIBRARY_RELEASE)
63                         message (STATUS "Found release-libcrypto++ in ${CRYPTOPP_LIBRARY_RELEASE}")
64                 endif()
65         endif (NOT CRYPTOPP_LIBRARY_RELEASE)
67         if (CRYPTOPP_LIBRARY_RELEASE)
68                 set_property (TARGET CRYPTOPP::CRYPTOPP
69                         PROPERTY IMPORTED_LOCATION_RELEASE ${CRYPTOPP_LIBRARY_RELEASE}
70                 )
71         else()
72                 set (CRYPTO_COMPLETE FALSE)
73         endif()
74 else()
75         if (NOT CRYPTOPP_LIBRARY)
76                 unset (CRYPTOPP_LIBRARY CACHE)
78                 find_library (CRYPTOPP_LIBRARY
79                         NAMES crypto++ cryptlib cryptopp
80                         PATHS ${CRYPTOPP_LIB_SEARCH_PATH}
81                 )
83                 if (CRYPTOPP_LIBRARY)
84                         message (STATUS "Found libcrypto++ in ${CRYPTOPP_LIBRARY}")
85                 endif()
86         endif()
88         if (CRYPTOPP_LIBRARY)
89                 set_property (TARGET CRYPTOPP::CRYPTOPP
90                         PROPERTY IMPORTED_LOCATION ${CRYPTOPP_LIBRARY}
91                 )
92         else()
93                 set (CRYPTO_COMPLETE FALSE)
94         endif()
95 endif()
97 if (NOT CRYPTOPP_CONFIG_SEARCH)
98         unset (CRYPTOPP_CONFIG_SEARCH CACHE)
100         check_include_file_cxx (${CRYPTOPP_INCLUDE_PREFIX}/config.h
101                 CRYPTOPP_CONFIG_SEARCH
102         )
103 endif()
105 if (NOT CRYPTOPP_CONFIG_FILE)
106         if (CRYPTOPP_CONFIG_SEARCH)
107                 if (CRYPTOPP_INCLUDE_DIR)
108                         set (CRYPTOPP_CONFIG_FILE ${CRYPTOPP_INCLUDE_DIR}/${CRYPTOPP_INCLUDE_PREFIX}/config.h
109                                 CACHE FILEPATH "cryptopp config.h" FORCE
110                         )
112                         set_target_properties (CRYPTOPP::CRYPTOPP PROPERTIES
113                                 INTERFACE_INCLUDE_DIRECTORIES "${CRYPTOPP_INCLUDE_DIR}"
114                         )
115                 else()
116                         set (CRYPTOPP_CONFIG_FILE ${CRYPTOPP_INCLUDE_PREFIX}/config.h
117                                 CACHE FILEPATH "cryptopp config.h" FORCE
118                         )
119                 endif()
120         else()
121                 unset(CRYPTOPP_CONFIG_SEARCH)
122         endif()
124         unset (CMAKE_REQUIRED_INCLUDES)
125 endif()
127 if (NOT CRYPTOPP_CONFIG_FILE)
128                 MESSAGE (FATAL_ERROR "crypto++ config.h not found")
129 endif()
131 if (NOT CRYPTOPP_VERSION)# AND CRYPTO_COMPLETE)
132         set (CMAKE_CONFIGURABLE_FILE_CONTENT
133                 "#include <${CRYPTOPP_CONFIG_FILE}>\n
134                 #include <stdio.h>\n
135                 int main(){\n
136                         printf (\"%d\", CRYPTOPP_VERSION);\n
137                 }\n"
138         )
140         configure_file ("${CMAKE_ROOT}/Modules/CMakeConfigurableFile.in"
141                 "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckCryptoppVersion.cxx" @ONLY IMMEDIATE
142         )
144         try_run (RUNRESULT
145                 COMPILERESULT
146                 ${CMAKE_BINARY_DIR}
147                 ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckCryptoppVersion.cxx
148                 RUN_OUTPUT_VARIABLE CRYPTOPP_VERSION
149         )
151         string (REGEX REPLACE "([0-9])([0-9])([0-9])" "\\1.\\2.\\3" CRYPTOPP_VERSION "${CRYPTOPP_VERSION}")
153         if (${CRYPTOPP_VERSION} VERSION_LESS ${MIN_CRYPTOPP_VERSION})
154                 message (FATAL_ERROR "crypto++ version ${CRYPTOPP_VERSION} is too old")
155         else()
156                 MESSAGE (STATUS "crypto++ version ${CRYPTOPP_VERSION} -- OK")
157                 set (CRYPTOPP_CONFIG_FILE ${CRYPTOPP_CONFIG_FILE} CACHE STRING "Path to config.h of crypto-lib" FORCE)
158                 set (CRYPTOPP_VERSION ${CRYPTOPP_VERSION} CACHE STRING "Version of cryptopp" FORCE)
159         endif()
160 endif()