diff options
author | Brent Cook <busterb@gmail.com> | 2019-04-11 06:27:01 -0500 |
---|---|---|
committer | Brent Cook <busterb@gmail.com> | 2019-04-11 06:27:01 -0500 |
commit | 89e43070825b62d0e75927e744fb379171361b8a (patch) | |
tree | 004fce9213fb2adcf4d8c2366f27528d073ee221 | |
parent | 7dfda75497a2ccdc0ed91732ed471008040c2bf9 (diff) | |
parent | 3d989bc91ea2a4a2307c62592b4e725f62af6c05 (diff) | |
download | portable-89e43070825b62d0e75927e744fb379171361b8a.tar.gz portable-89e43070825b62d0e75927e744fb379171361b8a.tar.bz2 portable-89e43070825b62d0e75927e744fb379171361b8a.zip |
Land #503, add CMake module
-rw-r--r-- | FindLibreSSL.cmake | 225 | ||||
-rw-r--r-- | README.md | 51 |
2 files changed, 276 insertions, 0 deletions
diff --git a/FindLibreSSL.cmake b/FindLibreSSL.cmake new file mode 100644 index 0000000..d87b96e --- /dev/null +++ b/FindLibreSSL.cmake | |||
@@ -0,0 +1,225 @@ | |||
1 | #[=======================================================================[ | ||
2 | |||
3 | Copyright (c) 2019 John Norrbin <jlnorrbin@johnex.se> | ||
4 | |||
5 | Permission to use, copy, modify, and distribute this software for any | ||
6 | purpose with or without fee is hereby granted, provided that the above | ||
7 | copyright notice and this permission notice appear in all copies. | ||
8 | |||
9 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
10 | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
11 | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
12 | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
13 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
14 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
15 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
16 | |||
17 | FindLibreSSL | ||
18 | ------------ | ||
19 | |||
20 | Find the LibreSSL encryption library. | ||
21 | |||
22 | Optional Components | ||
23 | ^^^^^^^^^^^^^^^^^^^ | ||
24 | |||
25 | This module supports two optional components: SSL and TLS. Both | ||
26 | components have associated imported targets, as described below. | ||
27 | |||
28 | Imported Targets | ||
29 | ^^^^^^^^^^^^^^^^ | ||
30 | |||
31 | This module defines the following imported targets: | ||
32 | |||
33 | LibreSSL::Crypto | ||
34 | The LibreSSL crypto library, if found. | ||
35 | |||
36 | LibreSSL::SSL | ||
37 | The LibreSSL ssl library, if found. Requires and includes LibreSSL::Crypto automatically. | ||
38 | |||
39 | LibreSSL::TLS | ||
40 | The LibreSSL tls library, if found. Requires and includes LibreSSL::SSL and LibreSSL::Crypto automatically. | ||
41 | |||
42 | Result Variables | ||
43 | ^^^^^^^^^^^^^^^^ | ||
44 | |||
45 | This module will set the following variables in your project: | ||
46 | |||
47 | LIBRESSL_FOUND | ||
48 | System has the LibreSSL library. If no components are requested it only requires the crypto library. | ||
49 | LIBRESSL_INCLUDE_DIR | ||
50 | The LibreSSL include directory. | ||
51 | LIBRESSL_CRYPTO_LIBRARY | ||
52 | The LibreSSL crypto library. | ||
53 | LIBRESSL_SSL_LIBRARY | ||
54 | The LibreSSL SSL library. | ||
55 | LIBRESSL_TLS_LIBRARY | ||
56 | The LibreSSL TLS library. | ||
57 | LIBRESSL_LIBRARIES | ||
58 | All LibreSSL libraries. | ||
59 | LIBRESSL_VERSION | ||
60 | This is set to $major.$minor.$revision (e.g. 2.6.8). | ||
61 | |||
62 | Hints | ||
63 | ^^^^^ | ||
64 | |||
65 | Set LIBRESSL_ROOT_DIR to the root directory of an LibreSSL installation. | ||
66 | |||
67 | ]=======================================================================] | ||
68 | |||
69 | # Set Hints | ||
70 | set(_LIBRESSL_ROOT_HINTS | ||
71 | ${LIBRESSL_ROOT_DIR} | ||
72 | ENV LIBRESSL_ROOT_DIR | ||
73 | ) | ||
74 | |||
75 | # Set Paths | ||
76 | if (WIN32) | ||
77 | file(TO_CMAKE_PATH "$ENV{PROGRAMFILES}" _programfiles) | ||
78 | set(_LIBRESSL_ROOT_PATHS | ||
79 | "${_programfiles}/LibreSSL" | ||
80 | ) | ||
81 | unset(_programfiles) | ||
82 | else() | ||
83 | set(_LIBRESSL_ROOT_PATHS | ||
84 | "/usr/local/" | ||
85 | ) | ||
86 | endif() | ||
87 | |||
88 | # Combine | ||
89 | set(_LIBRESSL_ROOT_HINTS_AND_PATHS | ||
90 | HINTS ${_LIBRESSL_ROOT_HINTS} | ||
91 | PATHS ${_LIBRESSL_ROOT_PATHS} | ||
92 | ) | ||
93 | |||
94 | # Find Include Path | ||
95 | find_path(LIBRESSL_INCLUDE_DIR | ||
96 | NAMES | ||
97 | tls.h | ||
98 | ${_LIBRESSL_ROOT_HINTS_AND_PATHS} | ||
99 | PATH_SUFFIXES | ||
100 | include | ||
101 | ) | ||
102 | |||
103 | # Find Crypto Library | ||
104 | find_library(LIBRESSL_CRYPTO_LIBRARY | ||
105 | NAMES | ||
106 | libcrypto | ||
107 | crypto | ||
108 | NAMES_PER_DIR | ||
109 | ${_LIBRESSL_ROOT_HINTS_AND_PATHS} | ||
110 | PATH_SUFFIXES | ||
111 | lib | ||
112 | ) | ||
113 | |||
114 | # Find SSL Library | ||
115 | find_library(LIBRESSL_SSL_LIBRARY | ||
116 | NAMES | ||
117 | libssl | ||
118 | ssl | ||
119 | NAMES_PER_DIR | ||
120 | ${_LIBRESSL_ROOT_HINTS_AND_PATHS} | ||
121 | PATH_SUFFIXES | ||
122 | lib | ||
123 | ) | ||
124 | |||
125 | # Find TLS Library | ||
126 | find_library(LIBRESSL_TLS_LIBRARY | ||
127 | NAMES | ||
128 | libtls | ||
129 | tls | ||
130 | NAMES_PER_DIR | ||
131 | ${_LIBRESSL_ROOT_HINTS_AND_PATHS} | ||
132 | PATH_SUFFIXES | ||
133 | lib | ||
134 | ) | ||
135 | |||
136 | # Set Libraries | ||
137 | set(LIBRESSL_LIBRARIES ${LIBRESSL_CRYPTO_LIBRARY} ${LIBRESSL_SSL_LIBRARY} ${LIBRESSL_TLS_LIBRARY}) | ||
138 | |||
139 | # Mark Variables As Advanced | ||
140 | mark_as_advanced(LIBRESSL_INCLUDE_DIR LIBRESSL_LIBRARIES LIBRESSL_CRYPTO_LIBRARY LIBRESSL_SSL_LIBRARY LIBRESSL_TLS_LIBRARY) | ||
141 | |||
142 | # Find Version File | ||
143 | if(LIBRESSL_INCLUDE_DIR AND EXISTS "${LIBRESSL_INCLUDE_DIR}/openssl/opensslv.h") | ||
144 | |||
145 | # Get Version From File | ||
146 | file(STRINGS "${LIBRESSL_INCLUDE_DIR}/openssl/opensslv.h" OPENSSLV.H REGEX "#define LIBRESSL_VERSION_TEXT[ ]+\".*\"") | ||
147 | |||
148 | # Match Version String | ||
149 | string(REGEX REPLACE ".*\".*([0-9]+)\\.([0-9]+)\\.([0-9]+)\"" "\\1;\\2;\\3" LIBRESSL_VERSION_LIST "${OPENSSLV.H}") | ||
150 | |||
151 | # Split Parts | ||
152 | list(GET LIBRESSL_VERSION_LIST 0 LIBRESSL_VERSION_MAJOR) | ||
153 | list(GET LIBRESSL_VERSION_LIST 1 LIBRESSL_VERSION_MINOR) | ||
154 | list(GET LIBRESSL_VERSION_LIST 2 LIBRESSL_VERSION_REVISION) | ||
155 | |||
156 | # Set Version String | ||
157 | set(LIBRESSL_VERSION "${LIBRESSL_VERSION_MAJOR}.${LIBRESSL_VERSION_MINOR}.${LIBRESSL_VERSION_REVISION}") | ||
158 | |||
159 | endif() | ||
160 | |||
161 | # Set Find Package Arguments | ||
162 | find_package_handle_standard_args(LibreSSL | ||
163 | REQUIRED_VARS | ||
164 | LIBRESSL_CRYPTO_LIBRARY | ||
165 | LIBRESSL_INCLUDE_DIR | ||
166 | VERSION_VAR | ||
167 | LIBRESSL_VERSION | ||
168 | HANDLE_COMPONENTS | ||
169 | FAIL_MESSAGE | ||
170 | "Could NOT find LibreSSL, try setting the path to LibreSSL using the LIBRESSL_ROOT_DIR environment variable" | ||
171 | ) | ||
172 | |||
173 | # LibreSSL Found | ||
174 | if(LIBRESSL_FOUND) | ||
175 | |||
176 | # Set LibreSSL::Crypto | ||
177 | if(NOT TARGET LibreSSL::Crypto AND EXISTS "${LIBRESSL_CRYPTO_LIBRARY}") | ||
178 | |||
179 | # Add Library | ||
180 | add_library(LibreSSL::Crypto UNKNOWN IMPORTED) | ||
181 | |||
182 | # Set Properties | ||
183 | set_target_properties( | ||
184 | LibreSSL::Crypto | ||
185 | PROPERTIES | ||
186 | INTERFACE_INCLUDE_DIRECTORIES "${LIBRESSL_INCLUDE_DIR}" | ||
187 | IMPORTED_LINK_INTERFACE_LANGUAGES "C" | ||
188 | IMPORTED_LOCATION "${LIBRESSL_CRYPTO_LIBRARY}" | ||
189 | ) | ||
190 | |||
191 | endif() # LibreSSL::Crypto | ||
192 | |||
193 | # Set LibreSSL::SSL | ||
194 | if(NOT TARGET LibreSSL::SSL AND EXISTS "${LIBRESSL_SSL_LIBRARY}") | ||
195 | |||
196 | # Add Library | ||
197 | add_library(LibreSSL::SSL UNKNOWN IMPORTED) | ||
198 | |||
199 | # Set Properties | ||
200 | set_target_properties( | ||
201 | LibreSSL::SSL | ||
202 | PROPERTIES | ||
203 | INTERFACE_INCLUDE_DIRECTORIES "${LIBRESSL_INCLUDE_DIR}" | ||
204 | IMPORTED_LINK_INTERFACE_LANGUAGES "C" | ||
205 | IMPORTED_LOCATION "${LIBRESSL_SSL_LIBRARY}" | ||
206 | INTERFACE_LINK_LIBRARIES LibreSSL::Crypto | ||
207 | ) | ||
208 | |||
209 | endif() # LibreSSL::SSL | ||
210 | |||
211 | # Set LibreSSL::TLS | ||
212 | if(NOT TARGET LibreSSL::TLS AND EXISTS "${LIBRESSL_TLS_LIBRARY}") | ||
213 | add_library(LibreSSL::TLS UNKNOWN IMPORTED) | ||
214 | set_target_properties( | ||
215 | LibreSSL::TLS | ||
216 | PROPERTIES | ||
217 | INTERFACE_INCLUDE_DIRECTORIES "${LIBRESSL_INCLUDE_DIR}" | ||
218 | IMPORTED_LINK_INTERFACE_LANGUAGES "C" | ||
219 | IMPORTED_LOCATION "${LIBRESSL_TLS_LIBRARY}" | ||
220 | INTERFACE_LINK_LIBRARIES LibreSSL::SSL | ||
221 | ) | ||
222 | |||
223 | endif() # LibreSSL::TLS | ||
224 | |||
225 | endif(LIBRESSL_FOUND) | ||
@@ -151,3 +151,54 @@ into other projects or build by itself. | |||
151 | | ENABLE_NC | OFF | Enable installing TLS-enabled nc(1) | | 151 | | ENABLE_NC | OFF | Enable installing TLS-enabled nc(1) | |
152 | | OPENSSLDIR | Blank | Set the default openssl directory. Can be specified from command line using <br>```-DOPENSSLDIR=<dirname>``` | | 152 | | OPENSSLDIR | Blank | Set the default openssl directory. Can be specified from command line using <br>```-DOPENSSLDIR=<dirname>``` | |
153 | 153 | ||
154 | # Using LibreSSL # | ||
155 | |||
156 | ## CMake ## | ||
157 | |||
158 | Make a new folder in your project root (where your main CMakeLists.txt file is located) called CMake. Copy the FindLibreSSL.cmake file to that folder, and add the following line to your main CMakeLists.txt: | ||
159 | |||
160 | ```cmake | ||
161 | set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMake;${CMAKE_MODULE_PATH}") | ||
162 | ``` | ||
163 | |||
164 | After your **add_executable** or **add_library** line in your CMakeLists.txt file add the following: | ||
165 | |||
166 | ```cmake | ||
167 | find_package(LibreSSL REQUIRED) | ||
168 | ``` | ||
169 | |||
170 | It will tell CMake to find LibreSSL and if found will let you use the following 3 interfaces in your CMakeLists.txt file: | ||
171 | |||
172 | * LibreSSL::Crypto | ||
173 | * LibreSSL::SSL | ||
174 | * LibreSSL::TLS | ||
175 | |||
176 | If you for example want to use the LibreSSL TLS library in your test program, include it like so (SSL and Cryto are required by TLS and included automatically too): | ||
177 | |||
178 | ```cmake | ||
179 | target_link_libraries(test LibreSSL::TLS) | ||
180 | ``` | ||
181 | |||
182 | Full example: | ||
183 | |||
184 | ```cmake | ||
185 | cmake_minimum_required(VERSION 3.10.0) | ||
186 | |||
187 | set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMake;${CMAKE_MODULE_PATH}") | ||
188 | |||
189 | project(test) | ||
190 | |||
191 | add_executable(test Main.cpp) | ||
192 | |||
193 | find_package(LibreSSL REQUIRED) | ||
194 | |||
195 | target_link_libraries(test LibreSSL::TLS) | ||
196 | ``` | ||
197 | |||
198 | #### Linux #### | ||
199 | |||
200 | Following the guide in the sections above to compile LibreSSL using make and running "sudo make install" will install LibreSSL to the /usr/local/ folder, and will found automatically by find_package. If your system installs it to another location or you have placed them yourself in a different location, you can set the CMake variable LIBRESSL_ROOT_DIR to the correct path, to help CMake find the library. | ||
201 | |||
202 | #### Windows #### | ||
203 | |||
204 | Placing the library files in C:/Program Files/LibreSSL/lib and the include files in C:/Program Files/LibreSSL/include should let CMake find them automatically, but it is recommended that you use CMake-GUI to set the paths. It is more convenient as you can have the files in any folder you choose. | ||