aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrent Cook <busterb@gmail.com>2019-04-11 06:27:01 -0500
committerBrent Cook <busterb@gmail.com>2019-04-11 06:27:01 -0500
commit89e43070825b62d0e75927e744fb379171361b8a (patch)
tree004fce9213fb2adcf4d8c2366f27528d073ee221
parent7dfda75497a2ccdc0ed91732ed471008040c2bf9 (diff)
parent3d989bc91ea2a4a2307c62592b4e725f62af6c05 (diff)
downloadportable-89e43070825b62d0e75927e744fb379171361b8a.tar.gz
portable-89e43070825b62d0e75927e744fb379171361b8a.tar.bz2
portable-89e43070825b62d0e75927e744fb379171361b8a.zip
Land #503, add CMake module
-rw-r--r--FindLibreSSL.cmake225
-rw-r--r--README.md51
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
3Copyright (c) 2019 John Norrbin <jlnorrbin@johnex.se>
4
5Permission to use, copy, modify, and distribute this software for any
6purpose with or without fee is hereby granted, provided that the above
7copyright notice and this permission notice appear in all copies.
8
9THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
17FindLibreSSL
18------------
19
20Find the LibreSSL encryption library.
21
22Optional Components
23^^^^^^^^^^^^^^^^^^^
24
25This module supports two optional components: SSL and TLS. Both
26components have associated imported targets, as described below.
27
28Imported Targets
29^^^^^^^^^^^^^^^^
30
31This module defines the following imported targets:
32
33LibreSSL::Crypto
34 The LibreSSL crypto library, if found.
35
36LibreSSL::SSL
37 The LibreSSL ssl library, if found. Requires and includes LibreSSL::Crypto automatically.
38
39LibreSSL::TLS
40 The LibreSSL tls library, if found. Requires and includes LibreSSL::SSL and LibreSSL::Crypto automatically.
41
42Result Variables
43^^^^^^^^^^^^^^^^
44
45This module will set the following variables in your project:
46
47LIBRESSL_FOUND
48 System has the LibreSSL library. If no components are requested it only requires the crypto library.
49LIBRESSL_INCLUDE_DIR
50 The LibreSSL include directory.
51LIBRESSL_CRYPTO_LIBRARY
52 The LibreSSL crypto library.
53LIBRESSL_SSL_LIBRARY
54 The LibreSSL SSL library.
55LIBRESSL_TLS_LIBRARY
56 The LibreSSL TLS library.
57LIBRESSL_LIBRARIES
58 All LibreSSL libraries.
59LIBRESSL_VERSION
60 This is set to $major.$minor.$revision (e.g. 2.6.8).
61
62Hints
63^^^^^
64
65Set LIBRESSL_ROOT_DIR to the root directory of an LibreSSL installation.
66
67]=======================================================================]
68
69# Set Hints
70set(_LIBRESSL_ROOT_HINTS
71 ${LIBRESSL_ROOT_DIR}
72 ENV LIBRESSL_ROOT_DIR
73)
74
75# Set Paths
76if (WIN32)
77 file(TO_CMAKE_PATH "$ENV{PROGRAMFILES}" _programfiles)
78 set(_LIBRESSL_ROOT_PATHS
79 "${_programfiles}/LibreSSL"
80 )
81 unset(_programfiles)
82else()
83 set(_LIBRESSL_ROOT_PATHS
84 "/usr/local/"
85 )
86endif()
87
88# Combine
89set(_LIBRESSL_ROOT_HINTS_AND_PATHS
90 HINTS ${_LIBRESSL_ROOT_HINTS}
91 PATHS ${_LIBRESSL_ROOT_PATHS}
92)
93
94# Find Include Path
95find_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
104find_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
115find_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
126find_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
137set(LIBRESSL_LIBRARIES ${LIBRESSL_CRYPTO_LIBRARY} ${LIBRESSL_SSL_LIBRARY} ${LIBRESSL_TLS_LIBRARY})
138
139# Mark Variables As Advanced
140mark_as_advanced(LIBRESSL_INCLUDE_DIR LIBRESSL_LIBRARIES LIBRESSL_CRYPTO_LIBRARY LIBRESSL_SSL_LIBRARY LIBRESSL_TLS_LIBRARY)
141
142# Find Version File
143if(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
159endif()
160
161# Set Find Package Arguments
162find_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
174if(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
225endif(LIBRESSL_FOUND)
diff --git a/README.md b/README.md
index 9660cdc..2b45fc0 100644
--- a/README.md
+++ b/README.md
@@ -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
158Make 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
161set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMake;${CMAKE_MODULE_PATH}")
162```
163
164After your **add_executable** or **add_library** line in your CMakeLists.txt file add the following:
165
166```cmake
167find_package(LibreSSL REQUIRED)
168```
169
170It 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
176If 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
179target_link_libraries(test LibreSSL::TLS)
180```
181
182Full example:
183
184```cmake
185cmake_minimum_required(VERSION 3.10.0)
186
187set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMake;${CMAKE_MODULE_PATH}")
188
189project(test)
190
191add_executable(test Main.cpp)
192
193find_package(LibreSSL REQUIRED)
194
195target_link_libraries(test LibreSSL::TLS)
196```
197
198#### Linux ####
199
200Following 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
204Placing 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.