Skip to content

Commit c7a1412

Browse files
committed
driver/CMakeLists.txt
- If MYSQLCLIENT_STATIC_BINDING is set, compile with /MT on Windows FindMySQL.cmake - Added MYSQL_VERSION_ID, same as MYSQL_NUM_VERSION - Added MYSQL_CLIENT_LIBS, same as MYSQL_LIBRARIES - Corrected the order of things, so variables are set that are expected to be set - Generate "getmysqlversion.c" - Removed DOS line endings - Quote more variables, in case paths have space them
1 parent 9a2086d commit c7a1412

File tree

3 files changed

+120
-150
lines changed

3 files changed

+120
-150
lines changed

FindMySQL.cmake

Lines changed: 116 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,12 @@
6262
# In addition, the below CMake variables are set by this include file
6363
#
6464
# MYSQL_VERSION - Three position numeric version, like 5.6.41
65-
# MYSQL_NUM_VERSION - Numeric padded version, 5.13.4 => 51304
65+
# MYSQL_VERSION_ID - Numeric padded version, 5.13.4 => 51304
66+
# MYSQL_NUM_VERSION - Same as MYSQL_VERSION_ID, for compatibility
6667
# MYSQL_LIB - Path to the client library
6768
# MYSQL_LIBRARIES - Library name, might be "-lmysqlclient" while
6869
# MYSQL_LIB is the path to the library
70+
# MYSQL_CLIENT_LIBS - Same as MYSQL_LIBRARIES, for compatibility
6971
#
7072
# (1) If MYSQL_INCLUDE_DIR or MYSQL_LIB_DIR are given, these are
7173
# used and an error is reported if can't be used
@@ -90,8 +92,9 @@
9092
# FIXME cache variables, makes them command line args?
9193
# FIXME really do include_directories() and link_directories()? Likely
9294
# FIXME add check that if not static, not picked up .a or mysqlclient.lib
93-
# FIXME MYSQL_NUM_VERSION need to take into account Cluster versions
95+
# FIXME MYSQL_VERSION_ID need to take into account Cluster versions
9496
# and Connector/C versions
97+
# FIXME handle MYSQL_VERSION_ID, LIBMYSQL_VERSION and LIBMYSQL_VERSION_ID?
9598
#
9699
##########################################################################
97100

@@ -434,52 +437,7 @@ if(NOT WIN32)
434437
if(MYSQL_CONFIG_EXECUTABLE)
435438
message(STATUS "mysql_config was found ${MYSQL_CONFIG_EXECUTABLE}")
436439

437-
_mysql_conf(_mysql_version "--version")
438-
439-
# Clean up so only numeric, in case of "-alpha" or similar
440-
string(REGEX MATCHALL "([0-9]+.[0-9]+.[0-9]+)" MYSQL_VERSION "${_mysql_version}")
441-
# To create a fully numeric version, first normalize so N.NN.NN
442-
string(REGEX REPLACE "[.]([0-9])[.]" ".0\\1." MYSQL_NUM_VERSION "${MYSQL_VERSION}")
443-
string(REGEX REPLACE "[.]([0-9])$" ".0\\1" MYSQL_NUM_VERSION "${MYSQL_NUM_VERSION}")
444-
# Finally remove the dot
445-
string(REGEX REPLACE "[.]" "" MYSQL_NUM_VERSION "${MYSQL_NUM_VERSION}")
446-
endif()
447-
448-
endif()
449-
450-
##########################################################################
451-
#
452-
# Try determine if to use C++ linkage, and also find C++ flags
453-
#
454-
##########################################################################
455-
456-
if(NOT WIN32)
457-
458-
if(MYSQL_CONFIG_EXECUTABLE)
459-
460-
if(NOT MYSQL_CFLAGS)
461-
_mysql_conf(MYSQL_CFLAGS "--cflags")
462-
endif()
463-
464-
if(NOT MYSQL_CXXFLAGS)
465-
if(MYSQL_CXX_LINKAGE OR MYSQL_NUM_VERSION GREATER 50603)
466-
_mysql_conf(MYSQL_CXXFLAGS "--cxxflags")
467-
set(MYSQL_CXX_LINKAGE 1)
468-
else()
469-
set(MYSQL_CXXFLAGS "${MYSQL_CFLAGS}")
470-
endif()
471-
endif()
472-
473-
# FIXME this should not be needed, caller of this module should set
474-
# it's own flags and just use the library on it's on terms
475-
# (change the infe message if enabling this code)
476-
# if(NOT MYSQL_LINK_FLAGS)
477-
# # Find -mcpu -march -mt -m32 -m64 and other flags starting with "-m"
478-
# string(REGEX MATCHALL "(^| )-m([^\r\n ]+)" MYSQL_LINK_FLAGS "${MYSQL_CXXFLAGS}")
479-
# string(REGEX REPLACE "^ " "" MYSQL_LINK_FLAGS "${MYSQL_LINK_FLAGS}")
480-
# string(REGEX REPLACE "; " ";" MYSQL_LINK_FLAGS "${MYSQL_LINK_FLAGS}")
481-
# endif()
482-
440+
_mysql_conf(MYSQL_VERSION "--version")
483441
endif()
484442

485443
endif()
@@ -550,9 +508,9 @@ else()
550508
endif()
551509

552510
# No specific paths, try some common install paths
553-
find_path(MYSQL_INCLUDE_DIR mysql.h ${_include_fallback_path})
511+
find_path(MYSQL_INCLUDE_DIR mysql.h ${_include_fallback_path})
554512

555-
if(NOT MYSQL_INCLUDE_DIR)
513+
if(NOT MYSQL_INCLUDE_DIR)
556514
message(FATAL_ERROR "Could not find \"mysql.h\" from searching "
557515
"\"${_pp_include_fallback_path}\"")
558516
endif()
@@ -563,35 +521,6 @@ if(FINDMYSQL_DEBUG)
563521
message("DBG: MYSQL_INCLUDE_DIR = \"${MYSQL_INCLUDE_DIR}\"")
564522
endif()
565523

566-
if (MYSQL_INCLUDE_DIR AND NOT MYSQL_NUM_VERSION)
567-
try_run(RUN_RESULT_VAR COMPILE_RESULT_VAR
568-
"${CMAKE_BINARY_DIR}"
569-
"${CMAKE_SOURCE_DIR}/cmake/getmysqlversion.c"
570-
CMAKE_FLAGS -DINCLUDE_DIRECTORIES:STRING=${MYSQL_INCLUDE_DIR}
571-
ARGS "${CMAKE_BINARY_DIR}/cmake/mysql_version_info.cmake"
572-
"MYSQL_SERVER_VERSION"
573-
"MYSQL_VERSION_ID"
574-
"LIBMYSQL_VERSION"
575-
"LIBMYSQL_VERSION_ID")
576-
577-
if(COMPILE_RESULT_VAR AND RUN_RESULT_VAR)
578-
include(${CMAKE_BINARY_DIR}/cmake/mysql_version_info.cmake)
579-
if(NOT LIBMYSQL_CPP_VERSION STREQUAL "")
580-
set(MYSQL_VERSION "${LIBMYSQL_CPP_VERSION}")
581-
elseif(NOT MYSQL_CPP_SERVER_VERSION STREQUAL "")
582-
set(MYSQL_VERSION "${MYSQL_CPP_SERVER_VERSION}")
583-
endif(NOT LIBMYSQL_CPP_VERSION STREQUAL "")
584-
string(REGEX MATCHALL "([0-9]+.[0-9]+.[0-9]+)" MYSQL_VERSION "${MYSQL_VERSION}")
585-
586-
if(LIBMYSQL_CPP_VERSION_ID)
587-
set(MYSQL_NUM_VERSION ${LIBMYSQL_CPP_VERSION_ID})
588-
elseif(MYSQL_CPP_SERVER_VERSION_ID)
589-
set(MYSQL_NUM_VERSION ${MYSQL_CPP_SERVER_VERSION_ID})
590-
endif(LIBMYSQL_CPP_VERSION_ID)
591-
endif(COMPILE_RESULT_VAR AND RUN_RESULT_VAR)
592-
endif()
593-
594-
595524
##########################################################################
596525
#
597526
# Find MYSQL_LIB_DIR, MYSQL_LIB and MYSQL_LIBRARIES
@@ -612,7 +541,7 @@ if(MYSQL_LIB_DIR)
612541
NAMES
613542
${_search_libs}
614543
PATHS
615-
${MYSQL_LIB_DIR}
544+
"${MYSQL_LIB_DIR}"
616545
NO_DEFAULT_PATH
617546
)
618547
_check_lib_search_error(MYSQL_LIB_DIR MYSQL_LIB "")
@@ -631,14 +560,14 @@ elseif(MYSQL_DIR AND
631560
NAMES
632561
${_search_libs}
633562
PATHS
634-
${MYSQL_DIR}
563+
"${MYSQL_DIR}"
635564
PATH_SUFFIXES
636565
${_lib_subdirs}
637566
NO_DEFAULT_PATH
638567
)
639568
_check_lib_search_error(MYSQL_DIR MYSQL_LIB "in \"${_pp_lib_subdirs}\"")
640-
get_filename_component(MYSQL_LIB_DIR ${MYSQL_LIB} PATH)
641-
set(MYSQL_LIBRARIES ${MYSQL_LIB})
569+
get_filename_component(MYSQL_LIB_DIR "${MYSQL_LIB}" PATH)
570+
set(MYSQL_LIBRARIES "${MYSQL_LIB}")
642571

643572
elseif(MYSQL_CONFIG_EXECUTABLE)
644573

@@ -655,7 +584,7 @@ elseif(MYSQL_CONFIG_EXECUTABLE)
655584
endif()
656585

657586
if(NOT EXISTS "${MYSQL_LIB_DIR}")
658-
message(FATAL_ERROR "Could not find the directory \"${MYSQL_INCLUDE_DIR}\" "
587+
message(FATAL_ERROR "Could not find the directory \"${MYSQL_LIB_DIR}\" "
659588
"found from running \"${MYSQL_CONFIG_EXECUTABLE}\"")
660589
endif()
661590

@@ -678,7 +607,7 @@ elseif(MYSQL_CONFIG_EXECUTABLE)
678607
_check_lib_search_error(MYSQL_LIB_DIR MYSQL_LIB "in \"${_static_subdirs}\"")
679608

680609
# Adjust MYSQL_LIB_DIR in case it changes
681-
get_filename_component(MYSQL_LIB_DIR ${MYSQL_LIB} PATH)
610+
get_filename_component(MYSQL_LIB_DIR "${MYSQL_LIB}" PATH)
682611

683612
# Replace the current library references with the full path
684613
# to the library, i.e. the -L will be ignored
@@ -721,7 +650,7 @@ else()
721650
"\"${_pp_lib_fallback_path}\"")
722651
endif()
723652

724-
get_filename_component(MYSQL_LIB_DIR ${MYSQL_LIB} PATH)
653+
get_filename_component(MYSQL_LIB_DIR "${MYSQL_LIB}" PATH)
725654

726655
endif()
727656

@@ -743,18 +672,115 @@ if(MYSQL_EXTRA_LIBRARIES)
743672
list(APPEND MYSQL_LIBRARIES ${MYSQL_EXTRA_LIBRARIES})
744673
endif()
745674

675+
# For compatibility
676+
SET(MYSQL_CLIENT_LIBS ${MYSQL_LIBRARIES})
677+
746678
##########################################################################
747679
#
748-
# Add more libraries to MYSQL_LIBRARIES
680+
# If not found MySQL Serverv version, compile a small client app
681+
# and let it write a small cmake file with the settings
682+
#
683+
##########################################################################
684+
685+
if(MYSQL_INCLUDE_DIR AND NOT MYSQL_VERSION)
686+
687+
# Write the C source file that will include the MySQL headers
688+
set(GETMYSQLVERSION_SOURCEFILE "${CMAKE_CURRENT_BINARY_DIR}/getmysqlversion.c")
689+
file(WRITE "${GETMYSQLVERSION_SOURCEFILE}"
690+
"#include <mysql.h>\n"
691+
"#include <stdio.h>\n"
692+
"int main() {\n"
693+
" printf(\"%s\", MYSQL_SERVER_VERSION);\n"
694+
"}\n"
695+
)
696+
697+
# Compile and run the created executable, store output in MYSQL_VERSION
698+
try_run(_run_result _compile_result
699+
"${CMAKE_BINARY_DIR}"
700+
"${GETMYSQLVERSION_SOURCEFILE}"
701+
CMAKE_FLAGS "-DINCLUDE_DIRECTORIES:STRING=${MYSQL_INCLUDE_DIR}"
702+
RUN_OUTPUT_VARIABLE MYSQL_VERSION
703+
)
704+
705+
if(FINDMYSQL_DEBUG)
706+
if(NOT _compile_result)
707+
message("DBG: Could not compile \"getmysqlversion.c\"")
708+
endif()
709+
if(_run_result)
710+
message("DBG: Running \"getmysqlversion\" returned ${_run_result}")
711+
endif()
712+
endif()
713+
714+
endif()
715+
716+
##########################################################################
717+
#
718+
# Clean up MYSQL_VERSION and create MYSQL_VERSION_ID/MYSQL_NUM_VERSION
719+
#
720+
##########################################################################
721+
722+
if(NOT MYSQL_VERSION)
723+
message(FATAL_ERROR "Could not determine the MySQL Server version")
724+
endif()
725+
726+
# Clean up so only numeric, in case of "-alpha" or similar
727+
string(REGEX MATCHALL "([0-9]+.[0-9]+.[0-9]+)" MYSQL_VERSION "${MYSQL_VERSION}")
728+
# To create a fully numeric version, first normalize so N.NN.NN
729+
string(REGEX REPLACE "[.]([0-9])[.]" ".0\\1." MYSQL_VERSION_ID "${MYSQL_VERSION}")
730+
string(REGEX REPLACE "[.]([0-9])$" ".0\\1" MYSQL_VERSION_ID "${MYSQL_VERSION_ID}")
731+
# Finally remove the dot
732+
string(REGEX REPLACE "[.]" "" MYSQL_VERSION_ID "${MYSQL_VERSION_ID}")
733+
set(MYSQL_NUM_VERSION ${MYSQL_VERSION_ID})
734+
735+
##########################################################################
736+
#
737+
# Try determine if to use C++ linkage, and also find C++ flags
738+
#
739+
##########################################################################
740+
741+
if(NOT WIN32)
742+
743+
if(MYSQL_CONFIG_EXECUTABLE)
744+
745+
if(NOT MYSQL_CFLAGS)
746+
_mysql_conf(MYSQL_CFLAGS "--cflags")
747+
endif()
748+
749+
if(NOT MYSQL_CXXFLAGS)
750+
if(MYSQL_CXX_LINKAGE OR MYSQL_VERSION_ID GREATER 50603)
751+
_mysql_conf(MYSQL_CXXFLAGS "--cxxflags")
752+
set(MYSQL_CXX_LINKAGE 1)
753+
else()
754+
set(MYSQL_CXXFLAGS "${MYSQL_CFLAGS}")
755+
endif()
756+
endif()
757+
758+
# FIXME this should not be needed, caller of this module should set
759+
# it's own flags and just use the library on it's on terms
760+
# (change the infe message if enabling this code)
761+
# if(NOT MYSQL_LINK_FLAGS)
762+
# # Find -mcpu -march -mt -m32 -m64 and other flags starting with "-m"
763+
# string(REGEX MATCHALL "(^| )-m([^\r\n ]+)" MYSQL_LINK_FLAGS "${MYSQL_CXXFLAGS}")
764+
# string(REGEX REPLACE "^ " "" MYSQL_LINK_FLAGS "${MYSQL_LINK_FLAGS}")
765+
# string(REGEX REPLACE "; " ";" MYSQL_LINK_FLAGS "${MYSQL_LINK_FLAGS}")
766+
# endif()
767+
768+
endif()
769+
770+
endif()
771+
772+
##########################################################################
773+
#
774+
# Inform CMake where to look for headers and libraries
749775
#
750776
##########################################################################
751777

752778
# string(TOUPPER "${CMAKE_BUILD_TYPE}" CMAKEBT)
753779
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${MYSQL_CXXFLAGS}")
754780
# set(CMAKE_CXX_FLAGS_${CMAKEBT} "${CMAKE_CXX_FLAGS_${CMAKEBT}} ${MYSQL_CXXFLAGS}")
755781

756-
include_directories(${MYSQL_INCLUDE_DIR})
757-
link_directories(${MYSQL_LIB_DIR})
782+
include_directories("${MYSQL_INCLUDE_DIR}")
783+
link_directories("${MYSQL_LIB_DIR}")
758784

759785
##########################################################################
760786
#
@@ -782,6 +808,6 @@ message(STATUS " MYSQL_LINK_FLAGS : ${MYSQL_LINK_FLAGS}")
782808
message(STATUS "MySQL client settings that the user can't override")
783809

784810
message(STATUS " MYSQL_VERSION : ${MYSQL_VERSION}")
785-
message(STATUS " MYSQL_NUM_VERSION : ${MYSQL_NUM_VERSION}")
811+
message(STATUS " MYSQL_VERSION_ID : ${MYSQL_VERSION_ID}")
786812
message(STATUS " MYSQL_LIB : ${MYSQL_LIB}")
787813
message(STATUS " MYSQL_LIBRARIES : ${MYSQL_LIBRARIES}")

cmake/getmysqlversion.c

Lines changed: 0 additions & 58 deletions
This file was deleted.

driver/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,10 @@ IF(WIN32)
306306
# STATIC_LIBRARY_FLAGS_RELEASE "/NODEFAULTLIB:LIBCMT")
307307

308308
# Changing CRT from dynamic to static
309-
#INCLUDE(${CMAKE_SOURCE_DIR}/changeCrt.cmake)
310-
#CHANGE_CRT("/MT")
309+
IF(MYSQLCLIENT_STATIC_BINDING)
310+
INCLUDE(${CMAKE_SOURCE_DIR}/changeCrt.cmake)
311+
CHANGE_CRT("/MT")
312+
ENDIF()
311313

312314
INSTALL(TARGETS mysqlcppconn mysqlcppconn-static
313315
RUNTIME DESTINATION lib

0 commit comments

Comments
 (0)