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

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 18 additions & 0 deletions recipes/mysql-connector-cpp/all/conandata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
sources:
"9.0.0":
url: "https://github.com/mysql/mysql-connector-cpp/archive/refs/tags/9.0.0.zip"
sha256: "9d4b90a4efe8861e821136fb3024074d14875749d8b69d1821361b2f2b8bfe55"
patches:
"9.0.0":
- patch_file: "patches/9.0.0/0002-rename-find_dependency.patch"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer having an issue pointed in the upstream about this. I understand the risk of name collision, but keeping it only here is not good as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not just a potential risk. The CMake configuration step consistently failed for me due to find_package() calls importing CMakeFindDependencyMacro.

patch_description: "Rename find_dependency() to avoid conflicts with the official CMake function"
patch_type: "conan"
- patch_file: "patches/9.0.0/0003-inject-conan-dependencies.patch"
patch_description: "Inject Conan dependencies"
patch_type: "conan"
- patch_file: "patches/9.0.0/0005-simplify-install-directories.patch"
patch_description: "Do not install libraries into subdirectories"
patch_type: "conan"
- patch_file: "patches/9.0.0/0006-replace-merge_libraries-with-object-libs.patch"
patch_description: "Replace fragile merge_libraries() call with object libraries"
patch_type: "conan"
163 changes: 163 additions & 0 deletions recipes/mysql-connector-cpp/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import os

from conan import ConanFile
from conan.errors import ConanInvalidConfiguration
from conan.tools.build import check_min_cppstd
from conan.tools.cmake import CMake, CMakeToolchain, CMakeDeps, cmake_layout
from conan.tools.env import VirtualBuildEnv, VirtualRunEnv
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
from conan.tools.scm import Version

required_conan_version = ">=1.55.0"


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,
}

@property
def _min_cppstd(self):
return 17

@property
def _compilers_minimum_version(self):
return {
"Visual Studio": "16",
"msvc": "192",
"gcc": "8",
"clang": "7",
"apple-clang": "10",
}

def export_sources(self):
export_conandata_patches(self)

def config_options(self):
if self.settings.os == "Windows":
del self.options.fPIC

def configure(self):
if self.options.shared:
self.options.rm_safe("fPIC")

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):
if self.settings.compiler.get_safe("cppstd"):
check_min_cppstd(self, self._min_cppstd)
minimum_version = self._compilers_minimum_version.get(str(self.settings.compiler), False)
if minimum_version and Version(self.settings.compiler.version) < minimum_version:
raise ConanInvalidConfiguration(f"{self.ref} requires {self.settings.compiler} {minimum_version} or newer")

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)

def generate(self):
VirtualBuildEnv(self).generate()
if self.dependencies["protobuf"].options.shared:
VirtualRunEnv(self).generate(scope="build")

tc = CMakeToolchain(self)
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["CMAKE_POLICY_DEFAULT_CMP0077"] = "NEW"
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):
self._patch_sources()
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.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,96 @@
From 4d5a57343a8cb54a787b68c42a6b2379bbce2a84 Mon Sep 17 00:00:00 2001
From: Martin Valgur <martin.valgur@gmail.com>
Date: Fri, 6 Sep 2024 19:04:24 +0300
Subject: [PATCH 1/2] Rename find_dependency() to avoid with official version

---
CMakeLists.txt | 4 ++--
cdk/CMakeLists.txt | 8 ++++----
cdk/cmake/dependency.cmake | 6 +++---
cdk/protocol/mysqlx/CMakeLists.txt | 4 ++--
4 files changed, 11 insertions(+), 11 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 4d747849..65b043a5 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -177,7 +177,7 @@ endif()
# Gcov support (Linux only)
#

-find_dependency(Coverage) # defines add_coverage() command
+find_dep(Coverage) # defines add_coverage() command
#message("WITH_COVERAGE: ${WITH_COVERAGE}")


@@ -306,7 +306,7 @@ endif()
# Note: Find OpenSSL early because it is needed by both CDK and JDBC (in case
# of static linking with the client library)

-find_dependency(SSL)
+find_dep(SSL)


#
diff --git a/cdk/CMakeLists.txt b/cdk/CMakeLists.txt
index dc8e3adc..4a9a164d 100644
--- a/cdk/CMakeLists.txt
+++ b/cdk/CMakeLists.txt
@@ -93,10 +93,10 @@ add_config(CDK_BIG_ENDIAN ${BIG_ENDIAN})
# Dependencies
#

-find_dependency(SSL)
-#find_dependency(Protobuf)
-find_dependency(RapidJSON)
-find_dependency(Coverage)
+find_dep(SSL)
+#find_dep(Protobuf)
+find_dep(RapidJSON)
+find_dep(Coverage)


# TODO: These macros should not be used in public headers because they are
diff --git a/cdk/cmake/dependency.cmake b/cdk/cmake/dependency.cmake
index e3fec4ee..3c38ca5c 100644
--- a/cdk/cmake/dependency.cmake
+++ b/cdk/cmake/dependency.cmake
@@ -33,20 +33,20 @@ include(config_options)

##########################################################################
#
-# find_dependency(XXX) command.
+# find_dep(XXX) command.
#
# Currently it is looking for DepFindXXX.cmake script that should perform
# all steps required to locate given dependency and make it available in
# the project. It is a layer on top of find_module().
#

-function(find_dependency NAME)
+macro(find_dep NAME)

# TODO: Fallback to find_module()

include(DepFind${NAME})

-endfunction(find_dependency)
+endmacro(find_dep)


# Variables to forward from this build configuration to external
diff --git a/cdk/protocol/mysqlx/CMakeLists.txt b/cdk/protocol/mysqlx/CMakeLists.txt
index a9873127..2ee4f937 100644
--- a/cdk/protocol/mysqlx/CMakeLists.txt
+++ b/cdk/protocol/mysqlx/CMakeLists.txt
@@ -26,8 +26,8 @@
# along with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

-find_dependency(Protobuf)
-find_dependency(Compression)
+find_dep(Protobuf)
+find_dep(Compression)

include(CheckIncludeFile)

Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
From cbcd5a641f10ed9a85bbccd93a0adf85939ea6c9 Mon Sep 17 00:00:00 2001
From: Martin Valgur <martin.valgur@gmail.com>
Date: Fri, 6 Sep 2024 17:33:25 +0300
Subject: [PATCH] Inject Conan dependencies

---
cdk/cmake/DepFindCompression.cmake | 5 +++++
cdk/cmake/DepFindProtobuf.cmake | 18 ++++--------------
cdk/cmake/DepFindRapidJSON.cmake | 3 +++
cdk/core/CMakeLists.txt | 2 +-
4 files changed, 13 insertions(+), 15 deletions(-)

diff --git a/cdk/cmake/DepFindCompression.cmake b/cdk/cmake/DepFindCompression.cmake
index 68f8e106..775e0b53 100644
--- a/cdk/cmake/DepFindCompression.cmake
+++ b/cdk/cmake/DepFindCompression.cmake
@@ -39,6 +39,11 @@
# ext::zstd
#

+find_package(ZLIB REQUIRED CONFIG)
+find_package(LZ4 REQUIRED CONFIG)
+find_package(Zstd REQUIRED CONFIG)
+return()
+
if(TARGET ext::z)
return()
endif()
diff --git a/cdk/cmake/DepFindProtobuf.cmake b/cdk/cmake/DepFindProtobuf.cmake
index 1fc785e3..f371e29a 100644
--- a/cdk/cmake/DepFindProtobuf.cmake
+++ b/cdk/cmake/DepFindProtobuf.cmake
@@ -50,22 +50,12 @@ endif()

message(STATUS "Setting up Protobuf.")

-# Setup extrnal project that builds protobuf from bundled sources
-add_ext(protobuf google/protobuf/api.pb.h)
+find_package(Protobuf REQUIRED CONFIG GLOBAL)

-if(NOT PROTOBUF_FOUND)
- message(FATAL_ERROR "Can't build without protobuf support")
+if(NOT TARGET ext::protobuf-lite)
+ add_library(ext::protobuf-lite ALIAS ext::protobuf)
endif()

-# import targets from the external project
-# Note: The pb_ targets are created by protobuf/exports.cmake
-add_ext_targets(protobuf
- LIBRARY protobuf-lite pb_libprotobuf-lite
- LIBRARY protobuf pb_libprotobuf
- EXECUTABLE protoc pb_protoc
-)
-
-
# Standard PROTOBUF_GENERATE_CPP modified to our usage
function(mysqlx_protobuf_generate_cpp SRCS HDRS)
IF(NOT ARGN)
@@ -90,7 +80,7 @@ function(mysqlx_protobuf_generate_cpp SRCS HDRS)
"${CMAKE_CURRENT_BINARY_DIR}/protobuf/${FIL_WE}.pb.h"
COMMAND ${CMAKE_COMMAND}
-E make_directory "${CMAKE_CURRENT_BINARY_DIR}/protobuf"
- COMMAND ext::protoc
+ COMMAND protobuf::protoc
ARGS --cpp_out "${CMAKE_CURRENT_BINARY_DIR}/protobuf"
-I ${ABS_PATH} ${ABS_FIL}

diff --git a/cdk/cmake/DepFindRapidJSON.cmake b/cdk/cmake/DepFindRapidJSON.cmake
index 24f2c486..ed09e7bf 100644
--- a/cdk/cmake/DepFindRapidJSON.cmake
+++ b/cdk/cmake/DepFindRapidJSON.cmake
@@ -36,6 +36,9 @@ if(TARGET RapidJSON::rapidjson)
return()
endif()

+find_package(RapidJSON REQUIRED CONFIG)
+return()
+
message(STATUS "Setting up RapidJSON.")

# TODO: how to make it GLOBAL...
diff --git a/cdk/core/CMakeLists.txt b/cdk/core/CMakeLists.txt
index ba7ee3e0..c8242233 100644
--- a/cdk/core/CMakeLists.txt
+++ b/cdk/core/CMakeLists.txt
@@ -42,7 +42,7 @@ SET(cdk_sources
file(GLOB HEADERS *.h)

add_library(cdk STATIC ${cdk_sources} ${HEADERS})
-
+find_dep(Protobuf)
target_link_libraries(cdk
PUBLIC cdk_mysqlx cdk_parser
PRIVATE ext::protobuf-lite # required by codecc.cc
Loading
Loading