MDEV-37996 CMake: INTERFACE targets for bundled-or-system libraries#5311
MDEV-37996 CMake: INTERFACE targets for bundled-or-system libraries#5311vaintroub wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request refactors the MariaDB build system to use modern CMake target-based dependency management (such as MariaDB::zlib, MariaDB::OpenSSL, and MariaDB::pcre2) instead of global directory-level include variables. While this is a great improvement, several issues were identified where removing the old include variables will break compilation. Specifically, the mariadb_ssl target must be defined even when SSL is disabled to prevent a fatal CMake error, client tools and performance schema unit tests will fail to compile due to missing transitive zlib and SSL headers, and the sql target needs to explicitly link to MariaDB::zlib to inherit its include directories.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| MESSAGE(FATAL_ERROR | ||
| "Wrong option for WITH_SSL. Valid values are: ${WITH_SSL_DOC}") | ||
| ENDIF() | ||
| ADD_LIBRARY(MariaDB::OpenSSL ALIAS mariadb_ssl) |
There was a problem hiding this comment.
If WITH_SSL is set to no or none (disabling SSL), the mariadb_ssl target is never created. This causes a fatal CMake error when ADD_LIBRARY(MariaDB::OpenSSL ALIAS mariadb_ssl) is called unconditionally at the end of the macro. To prevent this, ensure mariadb_ssl is always defined (even as an empty INTERFACE library) if it does not already exist.
IF(NOT TARGET mariadb_ssl)
ADD_LIBRARY(mariadb_ssl INTERFACE)
ENDIF()
ADD_LIBRARY(MariaDB::OpenSSL ALIAS mariadb_ssl)
There was a problem hiding this comment.
we can't compile without SSL.
| ${CMAKE_SOURCE_DIR}/mysys_ssl | ||
| ${ZLIB_INCLUDE_DIRS} | ||
| ${SSL_INCLUDE_DIRS} | ||
| $<TARGET_PROPERTY:mariadb_ssl,INTERFACE_INCLUDE_DIRECTORIES> |
There was a problem hiding this comment.
The ${ZLIB_INCLUDE_DIRS} variable was removed from the directory-level INCLUDE_DIRECTORIES in client/CMakeLists.txt, but it was not replaced with $<TARGET_PROPERTY:mariadb_zlib,INTERFACE_INCLUDE_DIRECTORIES>. Since client tools (such as mysqldump or mysqlbinlog) directly or transitively include zlib.h, and they do not link directly to MariaDB::zlib (they link to ${CLIENT_LIB} which is libmariadb), this will cause compilation failures when using a bundled or non-standard zlib. Please add the mariadb_zlib interface include directories here.
$<TARGET_PROPERTY:mariadb_zlib,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:mariadb_ssl,INTERFACE_INCLUDE_DIRECTORIES>
| INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include | ||
| ${CMAKE_SOURCE_DIR}/include/mysql | ||
| ${PCRE_INCLUDE_DIRS} | ||
| ${CMAKE_SOURCE_DIR}/sql |
There was a problem hiding this comment.
Removing ${SSL_INCLUDE_DIRS} from storage/perfschema/unittest/CMakeLists.txt will break compilation of the performance schema unit tests when SSL is enabled, because they transitively include <openssl/ssl.h> via server headers (e.g., violite.h). Since these unit tests are not plugins and do not use MYSQL_ADD_PLUGIN, they do not inherit the common include directories defined in cmake/plugin.cmake. Please add $<TARGET_PROPERTY:mariadb_ssl,INTERFACE_INCLUDE_DIRECTORIES> to the include directories list.
${CMAKE_SOURCE_DIR}/sql
$<TARGET_PROPERTY:mariadb_ssl,INTERFACE_INCLUDE_DIRECTORIES>
| tpool | ||
| ${LIBWRAP} ${LIBCRYPT} ${CMAKE_DL_LIBS} ${CMAKE_THREAD_LIBS_INIT} | ||
| ${SSL_LIBRARIES} | ||
| MariaDB::OpenSSL MariaDB::fmt |
There was a problem hiding this comment.
The sql target directly includes and uses zlib.h (e.g., in sql_compress.cc and other files). Since ${ZLIB_INCLUDE_DIRS} was removed from the directory-level INCLUDE_DIRECTORIES in sql/CMakeLists.txt, the sql target should explicitly link to MariaDB::zlib to inherit its include directories and link flags, adhering to the "link what you use" principle.
MariaDB::OpenSSL MariaDB::fmt MariaDB::zlib
There was a problem hiding this comment.
there is no sql_compress.cc
Stop overwriting the standard find_package() result variables (ZLIB_FOUND,
ZLIB_LIBRARIES, ZLIB_INCLUDE_DIR(S), ...), this breaks vcpkg.
Provide namespaced INTERFACE targets that point at either the bundled or
the system library and carry their include directories (and, for SSL, the
compile definitions):
MariaDB::zlib, MariaDB::OpenSSL, MariaDB::pcre2-8, MariaDB::pcre2-posix,
MariaDB::fmt, MariaDB::readline
Link these consistently instead of the scattered ${*_LIBRARIES} and
${*_INCLUDE_DIR(S)} variables sprinkled across the tree.
1c6d18b to
c9b660d
Compare
Remove hack in cmake/mariadb_connector_c.cmake that sets variables to make connectors FIND_PACKAGE(ZLIB) use server's zlib. it is not necessary anymore, and is expressed more directly.
Stop overwriting the standard find_package() result variables (ZLIB_FOUND, ZLIB_LIBRARIES, ZLIB_INCLUDE_DIR(S), ...), this breaks vcpkg.
Provide namespaced INTERFACE targets that point at either the bundled or the system library and carry their include directories (and, for SSL, the compile definitions):
MariaDB::zlib, MariaDB::OpenSSL, MariaDB::pcre2-8, MariaDB::pcre2-posix,
MariaDB::fmt, MariaDB::readline
Link these consistently instead of the scattered ${*_LIBRARIES} and ${*_INCLUDE_DIR(S)} variables sprinkled across the tree.