Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mysql-connector-cpp: new recipe #25170

Merged
merged 19 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions recipes/mysql-connector-cpp/all/conan_project_include.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Pre-find all dependencies, to prevent having to patch `find_dependency`
# which is included by Conan-generated CMake files, but also re-defined by the project itself

find_package(ZLIB REQUIRED CONFIG)
find_package(LZ4 REQUIRED CONFIG)
find_package(Zstd REQUIRED CONFIG)

find_package(RapidJSON REQUIRED CONFIG)
find_package(OpenSSL REQUIRED)

find_package(Protobuf REQUIRED CONFIG)
add_executable(ext::protoc ALIAS protobuf::protoc)
add_library(ext::protobuf-lite ALIAS protobuf::protobuf)


# This project will try combine all static libraries (and external dependencies)
# into a single library - mark all libraries from this project to limit
# the libraries that are combined
set(_CONAN_CMAKE_STATIC_LIBRARY_PREFIX_ORIG "${CMAKE_STATIC_LIBRARY_PREFIX}")
set(CMAKE_STATIC_LIBRARY_PREFIX "${CMAKE_STATIC_LIBRARY_PREFIX}_concpp_internal")


12 changes: 12 additions & 0 deletions recipes/mysql-connector-cpp/all/conandata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
sources:
"9.2.0":
url: "https://github.com/mysql/mysql-connector-cpp/archive/refs/tags/9.2.0.tar.gz"
sha256: "0b811559e57868487cfa7fa04af7f71d9df16cf6f7200bce83c5ddbeeedfb97c"
patches:
"9.2.0":
- patch_file: "patches/9.2.0-01-handle-protobuf-logic.patch"
patch_description: "Handle protobuf logic"
patch_type: "conan"
- patch_file: "patches/9.2.0-02-dont-embed-transitive-deps.patch"
patch_description: "Avoid embedding transitive deps"
patch_type: "conan"
134 changes: 134 additions & 0 deletions recipes/mysql-connector-cpp/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import os

from conan import ConanFile
from conan.tools.build import check_min_cppstd
from conan.tools.cmake import CMake, CMakeToolchain, CMakeDeps, cmake_layout
from conan.tools.files import get, copy, rm, export_conandata_patches, apply_conandata_patches, replace_in_file
from conan.tools.microsoft import is_msvc_static_runtime

required_conan_version = ">=2.0.9"

class MysqlConnectorCppConan(ConanFile):
name = "mysql-connector-cpp"
description = "MySQL database connector for C++ applications"
license = "GPL-2.0-only WITH Universal-FOSS-exception-1.0"
url = "https://github.com/conan-io/conan-center-index"
homepage = "https://dev.mysql.com/doc/connector-cpp/en/"
topics = ("mysql", "sql", "connector", "database")

package_type = "library"
settings = "os", "arch", "compiler", "build_type"
options = {
"shared": [True, False],
"fPIC": [True, False],
}
default_options = {
"shared": False,
"fPIC": True,
}
implements = ["auto_shared_fpic"]

def export_sources(self):
copy(self, "conan_project_include.cmake", self.recipe_folder, os.path.join(self.export_sources_folder, "src"))
export_conandata_patches(self)

def layout(self):
cmake_layout(self, src_folder="src")

def requirements(self):
# None of the dependencies are used transitively
self.requires("protobuf/3.21.12") # v4 and newer are not supported as of v9.0.0
self.requires("openssl/[>=1.1 <4]")
self.requires("rapidjson/1.1.0")
self.requires("zlib/[>=1.2.11 <2]")
self.requires("lz4/1.9.4")
self.requires("zstd/[~1.5]")

def validate(self):
check_min_cppstd(self, 17)

def build_requirements(self):
self.tool_requires("cmake/[>=3.24 <4]")
self.tool_requires("protobuf/<host_version>")

def source(self):
get(self, **self.conan_data["sources"][self.version], strip_root=True)
self._patch_sources()

def generate(self):
tc = CMakeToolchain(self)
tc.cache_variables["CMAKE_PROJECT_MySQL_CONCPP_INCLUDE"] = os.path.join(self.source_folder, "conan_project_include.cmake")
tc.cache_variables["BUNDLE_DEPENDENCIES"] = False
tc.cache_variables["BUILD_STATIC"] = not self.options.shared
tc.cache_variables["STATIC_MSVCRT"] = is_msvc_static_runtime(self)
tc.cache_variables["WITH_TESTS"] = False
tc.cache_variables["CMAKE_TRY_COMPILE_CONFIGURATION"] = str(self.settings.build_type)
tc.cache_variables["WITH_SSL"] = self.dependencies["openssl"].package_folder.replace("\\", "/")
tc.cache_variables["CMAKE_PREFIX_PATH"] = self.generators_folder.replace("\\", "/")
tc.cache_variables["IS64BIT"] = True
tc.cache_variables["use_full_protobuf"] = not self.dependencies["protobuf"].options.lite
tc.generate()

deps = CMakeDeps(self)
deps.set_property("protobuf::libprotobuf", "cmake_target_name", "ext::protobuf")
deps.set_property("protobuf::libprotobuf-lite", "cmake_target_name", "ext::protobuf-lite")
deps.set_property("rapidjson", "cmake_target_name", "RapidJSON::rapidjson")
deps.set_property("zlib", "cmake_target_name", "ext::z")
deps.set_property("lz4", "cmake_target_name", "ext::lz4")
deps.set_property("zstd", "cmake_target_name", "ext::zstd")
deps.generate()

def _patch_sources(self):
apply_conandata_patches(self)
# Disable boostrap(), which is unnecessary and fragile with variables set by Conan
# https://github.com/mysql/mysql-connector-cpp/blob/9.0.0/CMakeLists.txt#L69-L71
# https://github.com/mysql/mysql-connector-cpp/blob/9.0.0/cdk/cmake/bootstrap.cmake#L55
replace_in_file(self, os.path.join(self.source_folder, "CMakeLists.txt"), "bootstrap()", "")

def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()

def package(self):
copy(self, "LICENSE.txt", self.source_folder, os.path.join(self.package_folder, "licenses"))
cmake = CMake(self)
cmake.install()
rm(self, "INFO_SRC", self.package_folder)
rm(self, "INFO_BIN", self.package_folder)
rm(self, "*.cmake", self.package_folder)

def package_info(self):
self.cpp_info.set_property("cmake_file_name", "mysql-concpp")
self.cpp_info.set_property("cmake_target_name", "mysql::concpp")

aliases = ["mysql::concpp-xdevapi"]
if not self.options.shared:
aliases.append("mysql::concpp-static")
aliases.append("mysql::concpp-xdevapi-static")
if self.settings.build_type == "Debug":
aliases.append("mysql::concpp-static-debug")
aliases.append("mysql::concpp-xdevapi-static-debug")
aliases.append("mysql::openssl")
self.cpp_info.set_property("cmake_target_aliases", aliases)

lib = "mysqlcppconnx"
if not self.options.shared:
lib += "-static"
if is_msvc_static_runtime(self):
lib += "-mt"
self.cpp_info.libs = [lib]

if self.settings.os == "Windows":
self.cpp_info.libdirs = [os.path.join("lib", "vs14")]
self.cpp_info.bindirs = ["lib"]
self.cpp_info.system_libs.extend(["dnsapi", "ws2_32"])
if self.settings.os in ["Linux", "FreeBSD"]:
self.cpp_info.system_libs.extend(["m", "pthread", "dl"])
if self.settings.os == "SunOS":
self.cpp_info.system_libs.append(["socket", "nsl"])
if self.settings.os not in ["Windows", "FreeBSD"]:
self.cpp_info.system_libs.append("resolv")

if not self.options.shared:
self.cpp_info.defines = ["MYSQL_STATIC", "STATIC_CONCPP"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
diff --git a/cdk/cmake/DepFindProtobuf.cmake b/cdk/cmake/DepFindProtobuf.cmake
index 1fc785e..41d1405 100644
--- a/cdk/cmake/DepFindProtobuf.cmake
+++ b/cdk/cmake/DepFindProtobuf.cmake
@@ -44,6 +44,7 @@
#
#

+if(0)
if(TARGET ext::protobuf)
return()
endif()
@@ -64,6 +65,11 @@ add_ext_targets(protobuf
LIBRARY protobuf pb_libprotobuf
EXECUTABLE protoc pb_protoc
)
+endif()
+
+if (COMMAND mysqlx_protobuf_generate_cpp)
+ return()
+endif()


# Standard PROTOBUF_GENERATE_CPP modified to our usage

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
diff --git a/cmake/libutils.cmake b/cmake/libutils.cmake
index 848b7cf..cb748e6 100644
--- a/cmake/libutils.cmake
+++ b/cmake/libutils.cmake
@@ -164,6 +164,9 @@ function(merge_libraries TARGET)
set_property(SOURCE "${LIBUTILS_SCRIPT_DIR}/empty.cc" PROPERTY LANGUAGE CXX)

add_library(${TARGET} ${TYPE} "${LIBUTILS_SCRIPT_DIR}/empty.cc")
+ if(BUILD_STATIC AND DEFINED _CONAN_CMAKE_STATIC_LIBRARY_PREFIX_ORIG)
+ set_target_properties(${TARGET} PROPERTIES PREFIX "${_CONAN_CMAKE_STATIC_LIBRARY_PREFIX_ORIG}")
+ endif()
target_link_libraries(${TARGET} PRIVATE ${ARGN})

#
@@ -278,7 +281,8 @@ function(merge_libraries TARGET)
#

foreach(lib ${ARGN})
- if(CLANG)
+ if(1)
+ message("Will link target ${lib} into shared library with /wholearchive option")
target_link_options(${TARGET} PRIVATE /wholearchive:$<TARGET_FILE:${lib}>)
else()
target_link_options(${TARGET} PRIVATE /wholearchive:${lib})
diff --git a/cmake/libutils/merge_archives.cmake.in b/cmake/libutils/merge_archives.cmake.in
index 33094a6..506705a 100644
--- a/cmake/libutils/merge_archives.cmake.in
+++ b/cmake/libutils/merge_archives.cmake.in
@@ -570,9 +570,9 @@ function(process_deps)
AND NOT lib MATCHES "${NAME}${libext}$"
AND EXISTS "${libpath}"
)
- if(lib MATCHES "protobuf|uuid_gen|libssl|libcrypto|mysqlclient")
+ if(0)
list(APPEND LIBS1 "${lib}")
- else()
+ elseif(lib MATCHES concpp_internal)
list(APPEND LIBS "${lib}")
endif()
endif()

8 changes: 8 additions & 0 deletions recipes/mysql-connector-cpp/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cmake_minimum_required(VERSION 3.15)
project(test_package LANGUAGES CXX)

find_package(mysql-concpp REQUIRED CONFIG)

add_executable(${PROJECT_NAME} test_package.cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE mysql::concpp)
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_17)
26 changes: 26 additions & 0 deletions recipes/mysql-connector-cpp/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from conan import ConanFile
from conan.tools.build import can_run
from conan.tools.cmake import CMake, cmake_layout
import os


class TestPackageConan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
generators = "CMakeDeps", "CMakeToolchain", "VirtualRunEnv"
test_type = "explicit"

def layout(self):
cmake_layout(self)

def requirements(self):
self.requires(self.tested_reference_str)

def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()

def test(self):
if can_run(self):
bin_path = os.path.join(self.cpp.build.bindir, "test_package")
self.run(bin_path, env="conanrun")
7 changes: 7 additions & 0 deletions recipes/mysql-connector-cpp/all/test_package/test_package.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <mysqlx/xdevapi.h>

int main()
{
mysqlx::SessionSettings from_url("mysqlx://user:pwd@127.0.0.1:1234/db?ssl-mode=required");
return 0;
}
3 changes: 3 additions & 0 deletions recipes/mysql-connector-cpp/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
versions:
"9.2.0":
folder: all