29
29
##############################################################################
30
30
#
31
31
# Targets:
32
- # SSL::ssl - main library
33
- # SSL::crypto - crypto library (automatically linked in when needed)
32
+ # OpenSSL::SSL - main library (includes crypto library)
33
+ # OpenSSL::Crypto - crypto library
34
+ #
35
+ # Output variables:
36
+ # OPENSSL_INCLUDE_DIR
37
+ # OPENSSL_LIB_DIR
38
+ # OPENSSL_VERSION
39
+ # OPENSSL_VERSION_MAJOR
40
+ # OPENSSL_LIBRARIES
34
41
#
35
42
36
- if (TARGET SSL::ssl )
43
+ if (TARGET OpenSSL::SSL )
37
44
return ()
38
45
endif ()
39
46
47
+ include (config_header ) # add_config()
40
48
include (CheckSymbolExists )
41
49
42
50
add_config_option (WITH_SSL STRING DEFAULT system
@@ -55,26 +63,15 @@ function(main)
55
63
set (OPENSSL_ROOT_DIR "${WITH_SSL} " )
56
64
endif ()
57
65
58
- endif ()
59
-
60
-
61
- # TODO: Is it needed for anything?
62
- #IF(STATIC_MSVCRT)
63
- # SET(OPENSSL_MSVC_STATIC_RT ON)
64
- #ENDIF()
65
-
66
+ if (NOT DEFINED OpenSSL_DIR )
67
+ set (OpenSSL_DIR "${WITH_SSL} " )
68
+ endif ()
66
69
67
- #
68
- # Note: FindOpenSSL is broken on earlier versions of cmake. We use
69
- # our simplified replacement in that case.
70
- #
71
- # Note: I got strange results on Win even with cmake 3.8
72
- #
70
+ endif ()
73
71
74
72
find_openssl ()
75
- #find_package(OpenSSL)
76
73
77
- if (NOT TARGET SSL::ssl )
74
+ if (NOT TARGET OpenSSL::SSL )
78
75
79
76
message (SEND_ERROR
80
77
"Cannot find appropriate system libraries for SSL. "
@@ -107,21 +104,6 @@ function(main)
107
104
108
105
check_x509_functions ()
109
106
110
- if (WIN32 AND EXISTS "${OPENSSL_INCLUDE_DIR} /openssl/applink.c" )
111
-
112
- #message("-- Handling applink.c")
113
-
114
- add_library (openssl-applink STATIC "${OPENSSL_INCLUDE_DIR} /openssl/applink.c" )
115
- target_link_libraries (SSL::ssl INTERFACE openssl-applink )
116
-
117
- set_target_properties (openssl-applink PROPERTIES FOLDER "Misc" )
118
- # Remove warnings from openssl applink.c
119
- if (CXX_FRONTEND_MSVC )
120
- target_compile_options (openssl-applink PRIVATE /wd4152 /wd4996 )
121
- endif ()
122
- endif ()
123
-
124
-
125
107
if (BUNDLE_DEPENDENCIES )
126
108
bundle_ssl_libs ()
127
109
endif ()
@@ -130,7 +112,7 @@ endfunction(main)
130
112
131
113
132
114
function (check_x509_functions )
133
- SET (CMAKE_REQUIRED_LIBRARIES SSL::ssl )
115
+ SET (CMAKE_REQUIRED_LIBRARIES OpenSSL::SSL )
134
116
135
117
CHECK_SYMBOL_EXISTS (X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS "openssl/x509v3.h"
136
118
HAVE_X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS )
@@ -152,16 +134,50 @@ endfunction(check_x509_functions)
152
134
153
135
154
136
#
155
- # output:
156
- # OPENSSL_INCLUDE_DIR
157
- # OPENSSL_LIB_DIR
158
- # OPENSSL_VERSION
159
- # OPENSSL_VERSION_MAJOR
137
+ # Find libraries, create import targets and set output variables.
160
138
#
161
139
162
140
function (find_openssl )
163
141
164
- set (hints )
142
+ if (CMAKE_VERSION VERSION_GREATER "3.8" OR USE_CMAKE_FIND_OPENSSL )
143
+
144
+ # message(STATUS "Using cmake OpenSSL module")
145
+ find_package (OpenSSL )
146
+
147
+ set (OPENSSL_LIBRARY "${OPENSSL_SSL_LIBRARY} " )
148
+
149
+ else ()
150
+
151
+ #
152
+ # Note: FindOpenSSL is broken on earlier versions of cmake. We use
153
+ # our simplified replacement in that case.
154
+ #
155
+ # Note: I got strange results on Win even with cmake 3.8
156
+ #
157
+
158
+ find_openssl_fix ()
159
+
160
+ endif ()
161
+
162
+ get_filename_component (OPENSSL_LIB_DIR "${OPENSSL_LIBRARY} " PATH CACHE )
163
+
164
+ # Set output variables
165
+
166
+ set (OPENSSL_FOUND "${OPENSSL_FOUND} " PARENT_SCOPE )
167
+ set (OPENSSL_VERSION "${OPENSSL_VERSION} " PARENT_SCOPE )
168
+ set (OPENSSL_VERSION_MAJOR "${OPENSSL_VERSION_MAJOR} " PARENT_SCOPE )
169
+ set (OPENSSL_INCLUDE_DIR "${OPENSSL_INCLUDE_DIR} " PARENT_SCOPE )
170
+ set (OPENSSL_LIB_DIR "${OPENSSL_LIB_DIR} " PARENT_SCOPE )
171
+ set (OPENSSL_LIBRARIES "${OPENSSL_LIBRARIES} " PARENT_SCOPE )
172
+
173
+ endfunction (find_openssl )
174
+
175
+
176
+ macro (find_openssl_fix )
177
+
178
+ set (add_applink true )
179
+ unset (hints )
180
+
165
181
if (OPENSSL_ROOT_DIR )
166
182
set (hints HINTS ${OPENSSL_ROOT_DIR} NO_DEFAULT_PATH )
167
183
endif ()
@@ -176,10 +192,8 @@ function(find_openssl)
176
192
return ()
177
193
endif ()
178
194
179
- set (OPENSSL_INCLUDE_DIR "${OPENSSL_INCLUDE_DIR} " PARENT_SCOPE )
180
195
message ("-- found OpenSSL headers at: ${OPENSSL_INCLUDE_DIR} " )
181
196
182
-
183
197
# Verify version number. Version information looks like:
184
198
# #define OPENSSL_VERSION_TEXT "OpenSSL 1.1.1a 20 Nov 2018"
185
199
@@ -208,13 +222,9 @@ function(find_openssl)
208
222
209
223
list (GET version_list 3 OPENSSL_VERSION_PATCH )
210
224
211
-
212
-
213
225
set (OPENSSL_VERSION
214
226
"${OPENSSL_VERSION_MAJOR} .${OPENSSL_VERSION_MINOR} .${OPENSSL_VERSION_FIX}${OPENSSL_VERSION_PATCH} "
215
- PARENT_SCOPE
216
227
)
217
- set (OPENSSL_VERSION_MAJOR ${OPENSSL_VERSION_MAJOR} PARENT_SCOPE )
218
228
219
229
220
230
find_library (OPENSSL_LIBRARY
@@ -239,29 +249,44 @@ function(find_openssl)
239
249
# Note: apparently UNKNOWN library type does not work
240
250
# https://stackoverflow.com/questions/39346679/cmake-imported-unknown-global-target
241
251
242
- add_library (SSL::ssl SHARED IMPORTED GLOBAL )
243
- set_target_properties (SSL::ssl PROPERTIES
252
+ add_library (OpenSSL::SSL SHARED IMPORTED GLOBAL )
253
+ set_target_properties (OpenSSL::SSL PROPERTIES
244
254
IMPORTED_LOCATION "${OPENSSL_LIBRARY} "
245
255
IMPORTED_IMPLIB "${OPENSSL_LIBRARY} "
246
256
INTERFACE_INCLUDE_DIRECTORIES "${OPENSSL_INCLUDE_DIR} "
247
257
)
248
258
249
- add_library (SSL::crypto SHARED IMPORTED GLOBAL )
250
- set_target_properties (SSL::crypto PROPERTIES
259
+ add_library (OpenSSL::Crypto SHARED IMPORTED GLOBAL )
260
+ set_target_properties (OpenSSL::Crypto PROPERTIES
251
261
IMPORTED_LOCATION "${CRYPTO_LIBRARY} "
252
262
IMPORTED_IMPLIB "${CRYPTO_LIBRARY} "
253
263
INTERFACE_INCLUDE_DIRECTORIES "${OPENSSL_INCLUDE_DIR} "
254
264
)
255
265
256
- set_property (TARGET SSL::ssl PROPERTY
257
- INTERFACE_LINK_LIBRARIES SSL::crypto
266
+ set_property (TARGET OpenSSL::SSL PROPERTY
267
+ INTERFACE_LINK_LIBRARIES OpenSSL::Crypto
258
268
)
259
269
260
- get_filename_component ( OPENSSL_LIB_DIR " ${OPENSSL_LIBRARY} " PATH CACHE )
270
+ # TODO: Is it needed also when OpenSSL is found via cmake module?
261
271
262
- set ( OPENSSL_FOUND TRUE PARENT_SCOPE )
272
+ if ( WIN32 AND EXISTS " ${OPENSSL_INCLUDE_DIR} /openssl/applink.c" )
263
273
264
- endfunction (find_openssl )
274
+ #message("-- Handling applink.c")
275
+
276
+ add_library (openssl-applink STATIC "${OPENSSL_INCLUDE_DIR} /openssl/applink.c" )
277
+ target_link_libraries (OpenSSL::SSL INTERFACE openssl-applink )
278
+
279
+ set_target_properties (openssl-applink PROPERTIES FOLDER "Misc" )
280
+ # Remove warnings from openssl applink.c
281
+ if (CXX_FRONTEND_MSVC )
282
+ target_compile_options (openssl-applink PRIVATE /wd4152 /wd4996 )
283
+ endif ()
284
+
285
+ endif ()
286
+
287
+ set (OPENSSL_FOUND true )
288
+
289
+ endmacro (find_openssl_fix )
265
290
266
291
267
292
#
@@ -276,16 +301,20 @@ function(bundle_ssl_libs)
276
301
endif ()
277
302
278
303
279
- if (NOT WIN32 AND EXISTS ${OPENSSL_LIBRARY} AND EXISTS ${CRYPTO_LIBRARY} )
304
+ if (NOT WIN32 )
280
305
281
306
# Note: On U**ix systems the files we link to are symlinks to
282
307
# the actual shared libs, so we read these symlinks here and
283
308
# bundle their targets as well.
284
309
285
- foreach (lib ${OPENSSL_LIBRARY} ${CRYPTO_LIBRARY} )
310
+ foreach (lib ${OPENSSL_LIBRARIES} )
311
+
312
+ if (NOT EXISTS "${lib} " )
313
+ continue ()
314
+ endif ()
286
315
287
- get_filename_component (path ${lib} REALPATH )
288
- list (APPEND glob1 ${lib} ${path} )
316
+ get_filename_component (path " ${lib} " REALPATH )
317
+ list (APPEND glob1 " ${lib} " " ${path} " )
289
318
290
319
endforeach ()
291
320
@@ -351,39 +380,70 @@ function(bundle_ssl_libs)
351
380
endif ()
352
381
353
382
if (APPLE )
354
- # Replace libcrypto local path of libssl
355
- EXECUTE_PROCESS (
356
- COMMAND otool -L "${OPENSSL_LIBRARY} "
357
- OUTPUT_VARIABLE OTOOL_OPENSSL_DEPS )
358
- STRING (REPLACE "\n " ";" DEPS_LIST ${OTOOL_OPENSSL_DEPS} )
359
383
360
- foreach (LINE ${DEPS_LIST} )
361
- if (${LINE} MATCHES "ssl" )
362
- STRING (REGEX MATCH "(/.*libssl.*${CMAKE_SHARED_LIBRARY_SUFFIX} )" XXXXX ${LINE} )
384
+ # Edit the main OpenSSL library to not include full path to the crypto
385
+ # dependency. Instead use path relative to the location of the main library.
386
+ # Dependency information is changed from something
387
+ # like this:
388
+ #
389
+ # $ otool -L SSL/libssl.dylib
390
+ # SSL/libssl.dylib:
391
+ # /opt/homebrew/Cellar/openssl@3/3.2.1/lib/libcrypto.3.dylib (compatibility version 3.0.0, current version 3.0.0)
392
+ #
393
+ # to something like this:
394
+ #
395
+ # $ otool -L SSL/libssl.dylib
396
+ # SSL/libssl.dylib:
397
+ # @loader_path/libcrypto.3.dylib (compatibility version 3.0.0, current version 3.0.0)
398
+
399
+ # Read original dependencies using otool (only for the main library)
400
+
401
+ set (lib_ssl ${OPENSSL_LIBRARIES} )
402
+ list (FILTER lib_ssl INCLUDE REGEX "libssl\. dylib$" )
403
+
404
+ execute_process (
405
+ COMMAND otool -L ${lib_ssl}
406
+ OUTPUT_VARIABLE OTOOL_OPENSSL_DEPS
407
+ )
363
408
364
- if (CMAKE_MATCH_1 )
365
- get_filename_component (OPENSSL_LIBRARY_VERSION ${CMAKE_MATCH_1} NAME )
366
- endif ()
367
- elseif (${LINE} MATCHES "crypto" )
368
- STRING (REGEX MATCH "(/.*libcrypto.*${CMAKE_SHARED_LIBRARY_SUFFIX} )" XXXXX ${LINE} )
409
+ # Parse output of otool to extract full paths and library names
410
+ # with versions.
411
+
412
+ string (REPLACE "\n " ";" DEPS_LIST ${OTOOL_OPENSSL_DEPS} )
413
+
414
+ foreach (line ${DEPS_LIST} )
415
+ foreach (lib ssl crypto )
416
+
417
+ if (line MATCHES "(/.*lib${lib} .*${CMAKE_SHARED_LIBRARY_SUFFIX} ).*compatibility version" )
369
418
370
419
if (CMAKE_MATCH_1 )
371
- SET ( LIBSSL_DEPS "${CMAKE_MATCH_1} " )
372
- get_filename_component (CRYPTO_LIBRARY_VERSION ${CMAKE_MATCH_1} NAME )
420
+ set ( lib_${lib}_path "${CMAKE_MATCH_1} " )
421
+ get_filename_component (lib_${lib}_name "${lib_ ${lib} _path}" NAME )
373
422
endif ()
423
+
374
424
endif ()
375
425
376
- endforeach ()
426
+ endforeach (lib )
427
+ endforeach (line )
377
428
378
- if (LIBSSL_DEPS )
379
- # install_name_tool -change old new file
380
- EXECUTE_PROCESS (
381
- COMMAND chmod +w "${CRYPTO_LIBRARY_VERSION} ${OPENSSL_LIBRARY_VERSION} "
382
- COMMAND install_name_tool -change "${LIBSSL_DEPS} " "@loader_path/${CRYPTO_LIBRARY_VERSION} " "${OPENSSL_LIBRARY_VERSION} "
383
- WORKING_DIRECTORY "${CMAKE_BINARY_DIR} /SSL"
384
- )
429
+
430
+ if (NOT lib_ssl_path OR NOT lib_crypto_path )
431
+ message ("Warning: Failed to edit OpenSSL library dependencies" )
432
+ return ()
385
433
endif ()
386
- endif ()
434
+
435
+ # Use install_name_tool to replace full path with @loader_path:
436
+ # $ install_name_tool -change old new file
437
+
438
+ execute_process (
439
+ COMMAND chmod +w ${lib_ssl_name} ${lib_crypto_name}
440
+ COMMAND install_name_tool
441
+ -change "${lib_crypto_path} " "@loader_path/${lib_crypto_name} "
442
+ "${lib_ssl_name} "
443
+ WORKING_DIRECTORY "${CMAKE_BINARY_DIR} /SSL"
444
+ )
445
+
446
+ endif (APPLE )
387
447
388
448
endfunction (bundle_ssl_libs )
389
449
0 commit comments