Skip to content

Commit

Permalink
Ensure MySQL is found in vcpkg context
Browse files Browse the repository at this point in the history
  • Loading branch information
Krzmbrzl committed May 19, 2024
1 parent b9d38ba commit c00788e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
14 changes: 8 additions & 6 deletions cmake/find_modules/FindMySQL.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
include(CheckCXXSourceCompiles)

if(WIN32)
find_path(MySQL_INCLUDE_DIRS mysql.h
find_path(MySQL_INCLUDE_DIRS mysql/mysql.h
PATHS
$ENV{MYSQL_INCLUDE_DIR}
$ENV{MYSQL_INCLUDE_DIRS}
Expand All @@ -26,7 +26,7 @@ if(WIN32)
$ENV{ProgramW6432}/MySQL/*/include
)
else()
find_path(MySQL_INCLUDE_DIRS mysql.h
find_path(MySQL_INCLUDE_DIRS mysql/mysql.h
PATHS
$ENV{MYSQL_INCLUDE_DIR}
$ENV{MYSQL_INCLUDE_DIRS}
Expand Down Expand Up @@ -93,7 +93,9 @@ find_package_handle_standard_args(MySQL
REQUIRED_VARS MySQL_LIBRARIES MySQL_INCLUDE_DIRS
)

add_library(MySQL INTERFACE)
target_link_libraries(MySQL INTERFACE ${MySQL_LIBRARIES})
target_include_directories(MySQL INTERFACE ${MySQL_INCLUDE_DIRS})
add_library(MySQL::MySQL ALIAS MySQL)
if (MySQL_FOUND)
add_library(MySQL INTERFACE)
target_link_libraries(MySQL INTERFACE ${MySQL_LIBRARIES})
target_include_directories(MySQL INTERFACE ${MySQL_INCLUDE_DIRS})
add_library(MySQL::MySQL ALIAS MySQL)
endif()
4 changes: 2 additions & 2 deletions include/soci/mysql/soci-mysql.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
SOCI_GCC_WARNING_SUPPRESS(pedantic)
#endif

#include <mysql.h> // MySQL Client
#include <errmsg.h> // MySQL Error codes
#include <mysql/mysql.h> // MySQL Client
#include <mysql/errmsg.h> // MySQL Error codes

#ifdef SOCI_GCC_WARNING_RESTORE
SOCI_GCC_WARNING_RESTORE(pedantic)
Expand Down
33 changes: 24 additions & 9 deletions src/backends/mysql/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
if (SOCI_MYSQL_AS_AVAILABLE)
set(REQUIRED_FLAG "")
else()
set(REQUIRED_FLAG "REQUIRED")
endif()
option(SOCI_MYSQL_VCPKG_ONLY "Whether to look for the MySQL lib only as a vcpkg package" OFF)

find_package(MySQL ${REQUIRED_FLAG})
if (NOT SOCI_MYSQL_VCPKG_ONLY)
find_package(MySQL)
endif()

if (NOT MySQL_FOUND)
message(STATUS "Disabling MySQL backend as the required dependencies were not found")
set(SOCI_MYSQL OFF CACHE BOOL "" FORCE)
return()
# The MySQL package has a different name when using vcpkg
if (DEFINED VCPKG_TARGET_TRIPLET)
find_package(unofficial-libmysql)
endif()
if (unofficial-libmysql_FOUND)
add_library(MySQL::MySQL ALIAS unofficial::libmysql::libmysql)
message(STATUS "Found MySQL as a vcpkg package")
else()
if (NOT SOCI_MYSQL_AS_AVAILABLE)
message(FATAL_ERROR "Unable to find MySQL")
endif()
message(STATUS "Disabling MySQL backend as the required dependencies were not found")
set(SOCI_MYSQL OFF CACHE BOOL "" FORCE)
return()
endif()
endif()

get_target_property(UNDERLYING MySQL::MySQL ALIASED_TARGET)
message(STATUS "Underlying: ${UNDERLYING}")
get_target_property(INCLUDES ${UNDERLYING} INTERFACE_INCLUDE_DIRECTORIES)
message(STATUS "Includes: ${INCLUDES}")

add_library(soci_mysql
${SOCI_LIB_TYPE}
"blob.cpp"
Expand Down
5 changes: 3 additions & 2 deletions tests/mysql/mysql_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
#include <ctime>
#include <ciso646>
#include <cstdlib>
#include <mysqld_error.h>
#include <errmsg.h>
#include <cstdint>

#include <mysql/mysqld_error.h>
#include <mysql/errmsg.h>

std::string connectString;
backend_factory const &backEnd = *soci::factory_mysql();

Expand Down

0 comments on commit c00788e

Please sign in to comment.