Skip to content

Commit 42dda2f

Browse files
committed
bug#35584977 Contribution: cdk: Use cmake''s FindOpenSSL, not custom FindSSL module
Based on pull request from Sam James: mysql#33
1 parent 9d54fa8 commit 42dda2f

File tree

6 files changed

+157
-87
lines changed

6 files changed

+157
-87
lines changed

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,12 @@ if (CMAKE_COMPILER_IS_GNUCC AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 12.0)
303303
endif()
304304

305305

306+
# Note: Find OpenSSL early because it is needed by both CDK and JDBC (in case
307+
# of static linking with the client library)
308+
309+
find_dependency(SSL)
310+
311+
306312
#
307313
# Testing framework
308314
#

cdk/cmake/DepFindSSL.cmake

Lines changed: 144 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,22 @@
2929
##############################################################################
3030
#
3131
# 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
3441
#
3542

36-
if(TARGET SSL::ssl)
43+
if(TARGET OpenSSL::SSL)
3744
return()
3845
endif()
3946

47+
include(config_header) # add_config()
4048
include(CheckSymbolExists)
4149

4250
add_config_option(WITH_SSL STRING DEFAULT system
@@ -55,26 +63,15 @@ function(main)
5563
set(OPENSSL_ROOT_DIR "${WITH_SSL}")
5664
endif()
5765

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()
6669

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()
7371

7472
find_openssl()
75-
#find_package(OpenSSL)
7673

77-
if(NOT TARGET SSL::ssl)
74+
if(NOT TARGET OpenSSL::SSL)
7875

7976
message(SEND_ERROR
8077
"Cannot find appropriate system libraries for SSL. "
@@ -107,21 +104,6 @@ function(main)
107104

108105
check_x509_functions()
109106

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-
125107
if(BUNDLE_DEPENDENCIES)
126108
bundle_ssl_libs()
127109
endif()
@@ -130,7 +112,7 @@ endfunction(main)
130112

131113

132114
function(check_x509_functions)
133-
SET(CMAKE_REQUIRED_LIBRARIES SSL::ssl)
115+
SET(CMAKE_REQUIRED_LIBRARIES OpenSSL::SSL)
134116

135117
CHECK_SYMBOL_EXISTS(X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS "openssl/x509v3.h"
136118
HAVE_X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS)
@@ -152,16 +134,50 @@ endfunction(check_x509_functions)
152134

153135

154136
#
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.
160138
#
161139

162140
function(find_openssl)
163141

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+
165181
if(OPENSSL_ROOT_DIR)
166182
set(hints HINTS ${OPENSSL_ROOT_DIR} NO_DEFAULT_PATH)
167183
endif()
@@ -176,10 +192,8 @@ function(find_openssl)
176192
return()
177193
endif()
178194

179-
set(OPENSSL_INCLUDE_DIR "${OPENSSL_INCLUDE_DIR}" PARENT_SCOPE)
180195
message("-- found OpenSSL headers at: ${OPENSSL_INCLUDE_DIR}")
181196

182-
183197
# Verify version number. Version information looks like:
184198
# #define OPENSSL_VERSION_TEXT "OpenSSL 1.1.1a 20 Nov 2018"
185199

@@ -208,13 +222,9 @@ function(find_openssl)
208222

209223
list(GET version_list 3 OPENSSL_VERSION_PATCH)
210224

211-
212-
213225
set(OPENSSL_VERSION
214226
"${OPENSSL_VERSION_MAJOR}.${OPENSSL_VERSION_MINOR}.${OPENSSL_VERSION_FIX}${OPENSSL_VERSION_PATCH}"
215-
PARENT_SCOPE
216227
)
217-
set(OPENSSL_VERSION_MAJOR ${OPENSSL_VERSION_MAJOR} PARENT_SCOPE)
218228

219229

220230
find_library(OPENSSL_LIBRARY
@@ -239,29 +249,44 @@ function(find_openssl)
239249
# Note: apparently UNKNOWN library type does not work
240250
# https://stackoverflow.com/questions/39346679/cmake-imported-unknown-global-target
241251

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
244254
IMPORTED_LOCATION "${OPENSSL_LIBRARY}"
245255
IMPORTED_IMPLIB "${OPENSSL_LIBRARY}"
246256
INTERFACE_INCLUDE_DIRECTORIES "${OPENSSL_INCLUDE_DIR}"
247257
)
248258

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
251261
IMPORTED_LOCATION "${CRYPTO_LIBRARY}"
252262
IMPORTED_IMPLIB "${CRYPTO_LIBRARY}"
253263
INTERFACE_INCLUDE_DIRECTORIES "${OPENSSL_INCLUDE_DIR}"
254264
)
255265

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
258268
)
259269

260-
get_filename_component(OPENSSL_LIB_DIR "${OPENSSL_LIBRARY}" PATH CACHE)
270+
#TODO: Is it needed also when OpenSSL is found via cmake module?
261271

262-
set(OPENSSL_FOUND TRUE PARENT_SCOPE)
272+
if(WIN32 AND EXISTS "${OPENSSL_INCLUDE_DIR}/openssl/applink.c")
263273

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)
265290

266291

267292
#
@@ -276,16 +301,20 @@ function(bundle_ssl_libs)
276301
endif()
277302

278303

279-
if(NOT WIN32 AND EXISTS ${OPENSSL_LIBRARY} AND EXISTS ${CRYPTO_LIBRARY})
304+
if(NOT WIN32)
280305

281306
# Note: On U**ix systems the files we link to are symlinks to
282307
# the actual shared libs, so we read these symlinks here and
283308
# bundle their targets as well.
284309

285-
foreach(lib ${OPENSSL_LIBRARY} ${CRYPTO_LIBRARY})
310+
foreach(lib ${OPENSSL_LIBRARIES})
311+
312+
if(NOT EXISTS "${lib}")
313+
continue()
314+
endif()
286315

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}")
289318

290319
endforeach()
291320

@@ -351,39 +380,70 @@ function(bundle_ssl_libs)
351380
endif()
352381

353382
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})
359383

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+
)
363408

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")
369418

370419
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)
373422
endif()
423+
374424
endif()
375425

376-
endforeach()
426+
endforeach(lib)
427+
endforeach(line)
377428

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()
385433
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)
387447

388448
endfunction(bundle_ssl_libs)
389449

cdk/cmake/config_header.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242
# the variable is set to the given value.
4343
#
4444

45+
if(COMMAND ADD_CONFIG)
46+
return()
47+
endif()
48+
4549
set(CONFIG_VARS "" CACHE INTERNAL "configuration settings" FORCE)
4650
set(CONFIG_VARS_VAL "" CACHE INTERNAL "configuration settings" FORCE)
4751

cdk/foundation/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ target_include_directories(cdk_foundation PUBLIC
6262

6363
target_link_libraries(cdk_foundation
6464
PUBLIC RapidJSON::rapidjson
65-
PRIVATE SSL::ssl
65+
PRIVATE OpenSSL::SSL
6666
)
6767

6868
IF(WIN32)

cdk/mysqlx/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ ADD_LIBRARY(cdk_mysqlx STATIC
5151
target_link_libraries(cdk_mysqlx PUBLIC cdk_proto_mysqlx cdk_foundation)
5252

5353
# this is used by auth_hash.cc
54-
target_link_libraries(cdk_mysqlx PRIVATE SSL::ssl)
54+
target_link_libraries(cdk_mysqlx PRIVATE OpenSSL::SSL)
5555

5656
ADD_COVERAGE(cdk_mysqlx)
5757

jdbc/cmake/DepFindMySQL.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ function(main)
297297

298298
if(MYSQL_LIB_STATIC)
299299
target_link_libraries(mysql-client-if INTERFACE MySQL::client-static)
300-
target_link_libraries(mysql-client-if INTERFACE SSL::ssl SSL::crypto)
300+
target_link_libraries(mysql-client-if INTERFACE OpenSSL::SSL)
301301
else()
302302
target_link_libraries(mysql-client-if INTERFACE MySQL::client-shared)
303303
endif()

0 commit comments

Comments
 (0)