From 9bba29e483ee8748d461fbfb789528cb89350da3 Mon Sep 17 00:00:00 2001 From: ruben Date: Wed, 3 Jun 2020 18:03:16 +0100 Subject: [PATCH] Added CMake target exports and install rules --- .appveyor.yml | 24 +++- .travis.yml | 12 +- CMakeLists.txt | 118 ++++++++++++------ README.md | 5 +- TODO.txt | 4 +- cmake/FindMysqlvalgrind.cmake | 54 -------- cmake/config.cmake.in | 17 +++ cmake/test_utils.cmake | 84 ++++++++++++- cmake/utils.cmake | 39 ------ doc/main_page.hpp | 2 +- example/CMakeLists.txt | 42 +++---- test/CMakeLists.txt | 85 ++++++------- tools/build_date.py | 42 +++++++ {ci => tools}/build_unix.sh | 30 +++-- {ci => tools}/clang-gcov-linux.sh | 0 {ci => tools}/clang-gcov-osx.sh | 0 {ci => tools}/root_user_setup.sql | 0 {ci => tools/ssl}/ca-cert.pem | 0 {ci => tools/ssl}/server-cert.pem | 0 {ci => tools/ssl}/server-key.pem | 0 {ci => tools}/unix-ci.cnf | 0 .../user_project_find_package/CMakeLists.txt | 23 ++++ tools/user_project_find_package/build.py | 30 +++++ {ci => tools}/windows-ci.cnf | 0 24 files changed, 378 insertions(+), 233 deletions(-) delete mode 100644 cmake/FindMysqlvalgrind.cmake create mode 100644 cmake/config.cmake.in delete mode 100644 cmake/utils.cmake create mode 100755 tools/build_date.py rename {ci => tools}/build_unix.sh (72%) rename {ci => tools}/clang-gcov-linux.sh (100%) rename {ci => tools}/clang-gcov-osx.sh (100%) rename {ci => tools}/root_user_setup.sql (100%) rename {ci => tools/ssl}/ca-cert.pem (100%) rename {ci => tools/ssl}/server-cert.pem (100%) rename {ci => tools/ssl}/server-key.pem (100%) rename {ci => tools}/unix-ci.cnf (100%) create mode 100644 tools/user_project_find_package/CMakeLists.txt create mode 100644 tools/user_project_find_package/build.py rename {ci => tools}/windows-ci.cnf (100%) diff --git a/.appveyor.yml b/.appveyor.yml index 658228c0..e38da6fa 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -16,10 +16,11 @@ matrix: fast_finish: true build_script: - - cp ci\ca-cert.pem C:\ - - cp ci\server-cert.pem C:\ - - cp ci\server-key.pem C:\ - - cp ci\windows-ci.cnf C:\ + - cp tools\ssl\ca-cert.pem C:\ + - cp tools\ssl\server-cert.pem C:\ + - cp tools\ssl\server-key.pem C:\ + - cp tools\windows-ci.cnf C:\ + - C:\Python37-x64\python.exe tools\build_date.py C:\date - 'echo !include C:\\windows-ci.cnf >> "C:\Program Files\MySQL\MySQL Server 5.7\my.ini"' - net stop MySQL57 && net start MySQL57 - set PATH=C:\Program Files\MySQL\MySQL Server 5.7\bin;C:\Libraries\boost_1_73_0\lib64-msvc-14.2;C:\Libraries\boost_1_73_0\lib32-msvc-14.2;%PATH% @@ -27,11 +28,22 @@ build_script: - call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" %PLATFORM% - mkdir build - cd build - - cmake -G Ninja -DBOOST_ROOT=C:\Libraries\boost_1_73_0 -DCMAKE_BUILD_TYPE=%CONFIGURATION% -DCMAKE_C_COMPILER=cl -DCMAKE_CXX_COMPILER=cl .. - - cmake --build . -j + - cmake + -G Ninja + -DCMAKE_INSTALL_PREFIX=C:\boost_mysql + -DCMAKE_PREFIX_PATH=C:\date + -DBOOST_ROOT=C:\Libraries\boost_1_73_0 + -DCMAKE_BUILD_TYPE=%CONFIGURATION% + -DCMAKE_C_COMPILER=cl + -DCMAKE_CXX_COMPILER=cl + -DBOOST_MYSQL_ALLOW_FETCH_CONTENT=OFF + .. + - cmake --build . -j --target install test_script: - ctest --output-on-failure + - C:\Python37-x64\python.exe ..\tools\user_project_find_package\build.py + -DCMAKE_PREFIX_PATH=C:\boost_mysql;C:\date;C:\Libraries\boost_1_73_0 services: - mysql \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 879c8ba9..32542754 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,6 +12,7 @@ __linux_addons_defaults: &__linux_addons_defaults - python3 - valgrind - lcov + - ninja-build __linux_defaults: &__linux_defaults os: linux @@ -20,7 +21,7 @@ __linux_defaults: &__linux_defaults addons: <<: *__linux_addons_defaults script: - - bash -e -x ci/build_unix.sh + - bash -e -x tools/build_unix.sh __linux_mysql_defaults: &__linux_mysql_defaults <<: *__linux_defaults @@ -39,11 +40,8 @@ __osx_defaults: &__osx_defaults sudo: true compiler: clang script: - - bash -e -x ci/build_unix.sh + - bash -e -x tools/build_unix.sh -__osx_cmake_options: &__osx_cmake_options "CMAKE_OPTIONS=-DOPENSSL_ROOT_DIR=/usr/local/opt/openssl" - - language: cpp matrix: @@ -119,7 +117,6 @@ matrix: - name: osx_clang_x64_debug_mysql <<: *__osx_defaults env: - - *__osx_cmake_options - CMAKE_BUILD_TYPE=Debug - DATABASE=mysql - USE_COVERAGE=1 @@ -127,20 +124,17 @@ matrix: - name: osx_clang_x64_release_mysql <<: *__osx_defaults env: - - *__osx_cmake_options - CMAKE_BUILD_TYPE=Release - DATABASE=mysql - HAS_SHA256=1 - name: osx_clang_x64_debug_mariadb <<: *__osx_defaults env: - - *__osx_cmake_options - CMAKE_BUILD_TYPE=Debug - DATABASE=mariadb - USE_COVERAGE=1 - name: osx_clang_x64_release_mariadb <<: *__osx_defaults env: - - *__osx_cmake_options - CMAKE_BUILD_TYPE=Release - DATABASE=mariadb diff --git a/CMakeLists.txt b/CMakeLists.txt index c4a24243..b0a63332 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,16 +6,13 @@ # cmake_minimum_required(VERSION 3.13.0) # Determined by requiring Boost 1.70 -project(mysql-asio) - -# We require having access to cmake/ in the module path -list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake) +project(boost_mysql VERSION 0.0.1) # If we are the top-level project, enable testing if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) include(CTest) if(BUILD_TESTING) - set(_MYSQL_TESTING_ENABLED ON) + set(_TESTING_ENABLED ON) endif() # Valgrind tests and Valgrind-friendly code (e.g. mark SSL buffers as initialized) @@ -31,56 +28,66 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) mark_as_advanced(BOOST_MYSQL_SHA256_TESTS) endif() +option(BOOST_MYSQL_ALLOW_FETCH_CONTENT ON "Allow FetchContent for date library") +mark_as_advanced(BOOST_MYSQL_ALLOW_FETCH_CONTENT) -# Some common utilities -include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/utils.cmake) - -# Dependencies +# Includes +include(GNUInstallDirs) include(FetchContent) -find_package(Boost 1.70.0 REQUIRED COMPONENTS system) +# Dependencies +find_package(Boost 1.72.0 REQUIRED COMPONENTS system) find_package(Threads REQUIRED) find_package(OpenSSL REQUIRED) - -if (BOOST_MYSQL_VALGRIND_TESTS) - find_package(Mysqlvalgrind REQUIRED) +if (NOT BOOST_MYSQL_ALLOW_FETCH_CONTENT) + find_package(date REQUIRED) +else() + find_package(date QUIET) endif() -# Date -FetchContent_Declare( - date - GIT_REPOSITORY https://github.com/HowardHinnant/date.git - GIT_TAG v2.4.1 -) - -FetchContent_GetProperties(date) -if(NOT date_POPULATED) - FetchContent_Populate(date) - # Building date requires CMake 3.14, and we only need headers +if (NOT date_FOUND) + message("-- Could NOT find date, fetching it") + FetchContent_Declare( + date + GIT_REPOSITORY https://github.com/HowardHinnant/date.git + GIT_TAG v2.4.1 + ) + + FetchContent_GetProperties(date) + if(NOT date_POPULATED) + FetchContent_Populate(date) + set(ENABLE_DATE_TESTING OFF CACHE BOOL "") + set(USE_SYSTEM_TZ_DB ON CACHE BOOL "") + add_subdirectory(${date_SOURCE_DIR} ${date_BINARY_DIR}) + endif() endif() # Interface library (header-only) -add_library( - Boost_mysql - INTERFACE -) +add_library(boost_mysql INTERFACE) +add_library(Boost::mysql ALIAS boost_mysql) +target_link_directories( + boost_mysql + INTERFACE + ${Boost_LIBRARY_DIRS} # Prevent link errors in Windows +) target_link_libraries( - Boost_mysql + boost_mysql INTERFACE Boost::system Threads::Threads OpenSSL::Crypto OpenSSL::SSL + date_interface ) target_include_directories( - Boost_mysql + boost_mysql INTERFACE - include - ${date_SOURCE_DIR}/include + "$" + "$" ) target_compile_features( - Boost_mysql + boost_mysql INTERFACE cxx_std_17 ) @@ -92,22 +99,53 @@ target_compile_features( # available on every C++11 or higher capable compiler. Force it. if (MSVC) target_compile_definitions( - Boost_mysql + boost_mysql INTERFACE BOOST_ASIO_HAS_DEFAULT_FUNCTION_TEMPLATE_ARGUMENTS=1 ) endif() -if (BOOST_MYSQL_VALGRIND_TESTS) - target_link_libraries( - Boost_mysql - INTERFACE - Mysqlvalgrind::Mysqlvalgrind +# Installing +if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) + set_target_properties(boost_mysql PROPERTIES EXPORT_NAME mysql) + install( + DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/ + DESTINATION include + ) + install( + TARGETS boost_mysql + EXPORT boost_mysql_targets + ) + install( + EXPORT boost_mysql_targets + FILE boost_mysql-targets.cmake + NAMESPACE Boost:: + DESTINATION lib/cmake/boost_mysql + ) + + include(CMakePackageConfigHelpers) + + configure_package_config_file( + cmake/config.cmake.in + ${CMAKE_CURRENT_BINARY_DIR}/boost_mysql-config.cmake + INSTALL_DESTINATION lib/cmake/boost_mysql + ) + + write_basic_package_version_file( + ${CMAKE_CURRENT_BINARY_DIR}/boost_mysql-config-version.cmake + VERSION ${PROJECT_VERSION} + COMPATIBILITY SameMajorVersion + ) + + install(FILES + ${CMAKE_CURRENT_BINARY_DIR}/boost_mysql-config.cmake + ${CMAKE_CURRENT_BINARY_DIR}/boost_mysql-config-version.cmake + DESTINATION lib/cmake/boost_mysql ) endif() # Examples and tests -if(_MYSQL_TESTING_ENABLED) +if(_TESTING_ENABLED) include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/test_utils.cmake) add_subdirectory(example) add_subdirectory(test) diff --git a/README.md b/README.md index fc968438..a994e9eb 100644 --- a/README.md +++ b/README.md @@ -27,15 +27,16 @@ Download Boost.MySQL and make it available to your CMake script (we suggest you CMake's FetchContent module to do this), and then call add_subdirectory() on the Boost.MySQL root directory. This will look for all the required dependencies. -Finally, link your target against the **Boost_mysql** interface library, and you will be done! +Finally, link your target against the **Boost::mysql** interface library, and you will be done! ## Requirements - C++17 capable compiler (tested with gcc 7.4, clang 7.0, Apple clang 11.0, MSVC 19.25). -- Boost 1.70 or higher. The following Boost libraries are used: +- Boost 1.72 or higher. The following Boost libraries are used: - Boost.Asio (and in consequence, Boost.System). - Boost.Lexical_Cast. - Boost.Endian. + - Boost.Coroutine (only if you are using boost::asio::yield_context). - OpenSSL. - CMake 3.13.0 or higher, if using CMake to build against the library (this is the preferred way). - Howard Hinnant's date library (https://github.com/HowardHinnant/date) v2.4.1 or higher. diff --git a/TODO.txt b/TODO.txt index 803da4fe..fb665cee 100644 --- a/TODO.txt +++ b/TODO.txt @@ -58,11 +58,11 @@ Technical debt Concept checking for StreamType Review valid() for moved-from resultsets (should it return always true?) Force the same number of values in each row as in fields() - CMake exporting? Rework deserialize_row_fn to allow cursors Test prepared statement binding to procedure out params More thorough testing for several NULLs in integration testing Query and statement tests for DELETEs prepared_statement::execute(): static_assert(), handle value&, const value&, anything convertible Test for too many connections - Executor tests? \ No newline at end of file + Executor tests? + Update date version to remove target_compile_definitions() for default completion tokens diff --git a/cmake/FindMysqlvalgrind.cmake b/cmake/FindMysqlvalgrind.cmake deleted file mode 100644 index 6ddbe795..00000000 --- a/cmake/FindMysqlvalgrind.cmake +++ /dev/null @@ -1,54 +0,0 @@ -# -# Copyright (c) 2019-2020 Ruben Perez Hidalgo (rubenperez038 at gmail dot com) -# -# Distributed under the Boost Software License, Version 1.0. (See accompanying -# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -# - -# Perform the search -find_program(Mysqlvalgrind_EXECUTABLE valgrind) -find_path(Mysqlvalgrind_INCLUDE_DIR "valgrind/memcheck.h") - -# Inform CMake of the results -include(FindPackageHandleStandardArgs) -find_package_handle_standard_args( - Mysqlvalgrind - DEFAULT_MSG - Mysqlvalgrind_EXECUTABLE - Mysqlvalgrind_INCLUDE_DIR -) - -# Valgrind includes -if(Mysqlvalgrind_FOUND AND NOT TARGET Mysqlvalgrind::Mysqlvalgrind) - add_library(Mysqlvalgrind::Mysqlvalgrind INTERFACE IMPORTED) - target_include_directories(Mysqlvalgrind::Mysqlvalgrind INTERFACE ${Mysqlvalgrind_INCLUDE_DIR}) - target_compile_definitions(Mysqlvalgrind::Mysqlvalgrind INTERFACE BOOST_MYSQL_VALGRIND_TESTS) -endif() - -if (Mysqlvalgrind_FOUND AND NOT COMMAND MysqlValgrind_AddTest) - function(Mysqlvalgrind_AddMemcheckTest) - set(options "") - set(oneValueArgs NAME) - set(multiValueArgs COMMAND) - cmake_parse_arguments( - AddMemcheckTest - "${options}" - "${oneValueArgs}" - "${multiValueArgs}" - ${ARGN} - ) - - add_test( - NAME ${AddMemcheckTest_NAME} - COMMAND - ${Mysqlvalgrind_EXECUTABLE} - --leak-check=full - --error-limit=yes - --suppressions=${CMAKE_SOURCE_DIR}/tools/valgrind_suppressions.txt - --error-exitcode=1 - --gen-suppressions=all - ${AddMemcheckTest_COMMAND} - ) - endfunction() -endif() - diff --git a/cmake/config.cmake.in b/cmake/config.cmake.in new file mode 100644 index 00000000..da7e1113 --- /dev/null +++ b/cmake/config.cmake.in @@ -0,0 +1,17 @@ +# +# Copyright (c) 2019-2020 Ruben Perez Hidalgo (rubenperez038 at gmail dot com) +# +# Distributed under the Boost Software License, Version 1.0. (See accompanying +# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +# + +@PACKAGE_INIT@ + +include(CMakeFindDependencyMacro) + +find_dependency(Boost 1.72.0 REQUIRED COMPONENTS system) +find_dependency(Threads REQUIRED) +find_dependency(OpenSSL REQUIRED) +find_dependency(date REQUIRED) + +include("${CMAKE_CURRENT_LIST_DIR}/boost_mysql-targets.cmake") \ No newline at end of file diff --git a/cmake/test_utils.cmake b/cmake/test_utils.cmake index 7f93e449..6938877e 100644 --- a/cmake/test_utils.cmake +++ b/cmake/test_utils.cmake @@ -7,8 +7,46 @@ find_package(Python3 REQUIRED) +# Utility function to set warnings and other compile properties of +# our test targets +function(common_target_settings TARGET_NAME) + if(MSVC) + if (WIN32 AND CMAKE_SYSTEM_VERSION) + set(WINNT_VERSION ${CMAKE_SYSTEM_VERSION}) + string(REPLACE "." "" WINNT_VERSION ${WINNT_VERSION}) + string(REGEX REPLACE "([0-9])" "0\\1" WINNT_VERSION ${WINNT_VERSION}) + + set(WINNT_VERSION "0x${WINNT_VERSION}") + endif() + target_compile_definitions( + ${TARGET_NAME} + PRIVATE + _CRT_SECURE_NO_WARNINGS + _WIN32_WINNT=${WINNT_VERSION} # Silence warnings in Windows + _SILENCE_CXX17_ADAPTOR_TYPEDEFS_DEPRECATION_WARNING # Warnings in C++17 for Asio + ) + target_compile_options(${TARGET_NAME} PRIVATE /bigobj) # Prevent failures on Windows + else() + target_compile_options(${TARGET_NAME} PRIVATE -Wall -Wextra -pedantic -Werror) + endif() + + set_target_properties(${TARGET_NAME} PROPERTIES CXX_EXTENSIONS OFF) # disable extensions + + # Valgrind + if (BOOST_MYSQL_VALGRIND_TESTS) + target_include_directories(${TARGET_NAME} PRIVATE ${VALGRIND_INCLUDE_DIR}) + target_compile_definitions(${TARGET_NAME} PRIVATE BOOST_MYSQL_VALGRIND_TESTS) + endif() + + # Coverage + if (BOOST_MYSQL_COVERAGE) + target_compile_options(${TARGET_NAME} PRIVATE --coverage) + target_link_options(${TARGET_NAME} PRIVATE --coverage) + endif() +endfunction() + # Adds a test fixture that consists of running a SQL file in the MySQL server -function (_mysql_sql_setup_fixture) +function (add_sql_setup_fixture) set(options "") set(oneValueArgs TEST_NAME FIXTURE_NAME SQL_FILE SKIP_VAR) set(multiValueArgs "") @@ -36,4 +74,46 @@ function (_mysql_sql_setup_fixture) ${ADDITIONAL_OPTIONS} ) set_tests_properties(${SQLFIXT_TEST_NAME} PROPERTIES FIXTURES_SETUP ${SQLFIXT_FIXTURE_NAME}) -endfunction() \ No newline at end of file +endfunction() + +# Valgrind stuff +if (BOOST_MYSQL_VALGRIND_TESTS) + # Locate executable + find_program(VALGRIND_EXECUTABLE valgrind) + if (NOT VALGRIND_EXECUTABLE) + message(FATAL_ERROR "Cannot locate valgrind executable") + endif() + + # Locate includes + find_path(VALGRIND_INCLUDE_DIR "valgrind/memcheck.h") + if (NOT VALGRIND_INCLUDE_DIR) + message(FATAL_ERROR "Cannot locate valgrind include files") + endif() + + # Helper to define tests + function(add_memcheck_test) + set(options "") + set(oneValueArgs NAME) + set(multiValueArgs COMMAND) + cmake_parse_arguments( + AddMemcheckTest + "${options}" + "${oneValueArgs}" + "${multiValueArgs}" + ${ARGN} + ) + + add_test( + NAME ${AddMemcheckTest_NAME} + COMMAND + ${VALGRIND_EXECUTABLE} + --leak-check=full + --error-limit=yes + --suppressions=${CMAKE_SOURCE_DIR}/tools/valgrind_suppressions.txt + --error-exitcode=1 + --gen-suppressions=all + ${AddMemcheckTest_COMMAND} + ) + endfunction() +endif() + diff --git a/cmake/utils.cmake b/cmake/utils.cmake deleted file mode 100644 index f608b4ed..00000000 --- a/cmake/utils.cmake +++ /dev/null @@ -1,39 +0,0 @@ -# -# Copyright (c) 2019-2020 Ruben Perez Hidalgo (rubenperez038 at gmail dot com) -# -# Distributed under the Boost Software License, Version 1.0. (See accompanying -# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -# - -# Utility function to set warnings and other compile properties of -# our test targets -function(_mysql_common_target_settings TARGET_NAME) - if(MSVC) - if (WIN32 AND CMAKE_SYSTEM_VERSION) - set(WINNT_VERSION ${CMAKE_SYSTEM_VERSION}) - string(REPLACE "." "" WINNT_VERSION ${WINNT_VERSION}) - string(REGEX REPLACE "([0-9])" "0\\1" WINNT_VERSION ${WINNT_VERSION}) - - set(WINNT_VERSION "0x${WINNT_VERSION}") - endif() - target_compile_definitions( - ${TARGET_NAME} - PRIVATE - _CRT_SECURE_NO_WARNINGS - _WIN32_WINNT=${WINNT_VERSION} # Silence warnings in Windows - _SILENCE_CXX17_ADAPTOR_TYPEDEFS_DEPRECATION_WARNING # Warnings in C++17 for Asio - ) - # For some reason, Appveyor setup needs this to link against coroutine - target_link_directories(${TARGET_NAME} PRIVATE ${Boost_LIBRARY_DIRS}) - target_compile_options(${TARGET_NAME} PRIVATE /bigobj) # Prevent failures on Windows - else() - target_compile_options(${TARGET_NAME} PRIVATE -Wall -Wextra -pedantic -Werror) - endif() - set_target_properties(${TARGET_NAME} PROPERTIES CXX_EXTENSIONS OFF) # disable extensions - - # Coverage - if (BOOST_MYSQL_COVERAGE) - target_compile_options(${TARGET_NAME} PRIVATE --coverage) - target_link_options(${TARGET_NAME} PRIVATE --coverage) - endif() -endfunction() diff --git a/doc/main_page.hpp b/doc/main_page.hpp index 5e9af274..614b5157 100644 --- a/doc/main_page.hpp +++ b/doc/main_page.hpp @@ -30,7 +30,7 @@ * CMake's FetchContent module to do this), and then call add_subdirectory() on the * Boost.MySQL root directory. This will look for all the required dependencies. * - * Finally, link your target against the **Boost_mysql** interface library, and you will be done! + * Finally, link your target against the **Boost::mysql** interface library, and you will be done! * * \section Requirements * diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 10e06033..2dab40d5 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -8,7 +8,7 @@ find_package(Boost REQUIRED COMPONENTS coroutine) # Compile the example -function (_mysql_build_example EXECUTABLE_NAME CPPFILE) +function (build_example EXECUTABLE_NAME CPPFILE) add_executable( ${EXECUTABLE_NAME} ${CPPFILE} @@ -16,36 +16,36 @@ function (_mysql_build_example EXECUTABLE_NAME CPPFILE) target_link_libraries( ${EXECUTABLE_NAME} PRIVATE - Boost_mysql + Boost::mysql Boost::coroutine ) - _mysql_common_target_settings(${EXECUTABLE_NAME}) + common_target_settings(${EXECUTABLE_NAME}) endfunction() # Run it as a test -function (_mysql_test_example EXECUTABLE_NAME) +function (test_example EXECUTABLE_NAME) set(EXECUTABLE_PATH ${CMAKE_CURRENT_BINARY_DIR}/${EXECUTABLE_NAME}) - set(TEST_NAME "mysql_${EXECUTABLE_NAME}") + set(TEST_NAME ${EXECUTABLE_NAME}) add_test( NAME ${TEST_NAME} COMMAND ${EXECUTABLE_PATH} example_user example_password ) - set_tests_properties(${TEST_NAME} PROPERTIES FIXTURES_REQUIRED mysql_examples_fixture) + set_tests_properties(${TEST_NAME} PROPERTIES FIXTURES_REQUIRED boost_mysql_examples_fixture) endfunction() # Run it as a test using Valgrind -function (_mysql_memcheck_example EXECUTABLE_NAME) +function (memcheck_example EXECUTABLE_NAME) set(EXECUTABLE_PATH ${CMAKE_CURRENT_BINARY_DIR}/${EXECUTABLE_NAME}) - set(TEST_NAME "mysql_${EXECUTABLE_NAME}_memcheck") - Mysqlvalgrind_AddMemcheckTest( + set(TEST_NAME "${EXECUTABLE_NAME}_memcheck") + add_memcheck_test( NAME ${TEST_NAME} COMMAND ${EXECUTABLE_PATH} example_user example_password ) - set_tests_properties(${TEST_NAME} PROPERTIES FIXTURES_REQUIRED mysql_examples_fixture) + set_tests_properties(${TEST_NAME} PROPERTIES FIXTURES_REQUIRED boost_mysql_examples_fixture) endfunction() # The list of all the examples we have -set(MYSQL_EXAMPLES +set(BOOST_MYSQL_EXAMPLES query_sync query_async_callbacks query_async_coroutines @@ -57,27 +57,27 @@ set(MYSQL_EXAMPLES ) # The examples we do NOT want to ever memcheck -set(MYSQL_EXAMPLES_NOMEMCHECK +set(BOOST_MYSQL_EXAMPLES_NOMEMCHECK query_async_coroutines ) # Run setup -_mysql_sql_setup_fixture( - TEST_NAME mysql_examples_setup - FIXTURE_NAME mysql_examples_fixture +add_sql_setup_fixture( + TEST_NAME boost_mysql_examples_setup + FIXTURE_NAME boost_mysql_examples_fixture SQL_FILE ${CMAKE_CURRENT_SOURCE_DIR}/db_setup.sql ) # Build and run examples -foreach(EXAMPLE_NAME ${MYSQL_EXAMPLES}) - set(EXECUTABLE_NAME "example_${EXAMPLE_NAME}") +foreach(EXAMPLE_NAME ${BOOST_MYSQL_EXAMPLES}) + set(EXECUTABLE_NAME "boost_mysql_example_${EXAMPLE_NAME}") - _mysql_build_example(${EXECUTABLE_NAME} "${EXAMPLE_NAME}.cpp") + build_example(${EXECUTABLE_NAME} "${EXAMPLE_NAME}.cpp") # Don't run examples twice. Some of them would require re-running setup - if (BOOST_MYSQL_VALGRIND_TESTS AND NOT ${EXAMPLE_NAME} IN_LIST MYSQL_EXAMPLES_NOMEMCHECK) - _mysql_memcheck_example(${EXECUTABLE_NAME}) + if (BOOST_MYSQL_VALGRIND_TESTS AND NOT ${EXAMPLE_NAME} IN_LIST BOOST_MYSQL_EXAMPLES_NOMEMCHECK) + memcheck_example(${EXECUTABLE_NAME}) else() - _mysql_test_example(${EXECUTABLE_NAME}) + test_example(${EXECUTABLE_NAME}) endif() endforeach() diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 26c7ecbd..a02e88ed 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -22,7 +22,7 @@ endif() # Unit testing. We do not use gtest_discover_tests because # of the runtime penalty (specially considerable in integration tests) add_executable( - mysql_unittests + boost_mysql_unittests unit/detail/auxiliar/static_string.cpp unit/detail/auth/auth_calculator.cpp unit/detail/protocol/serialization_test_common.cpp @@ -39,6 +39,7 @@ add_executable( unit/detail/protocol/row_deserialization.cpp unit/detail/protocol/binary_serialization.cpp unit/detail/protocol/null_bitmap_traits.cpp + unit/detail/protocol/channel.cpp unit/metadata.cpp unit/value.cpp unit/row.cpp @@ -47,35 +48,30 @@ add_executable( unit/resultset.cpp unit/connection.cpp ) -# A codegen issue in MSVC C++17 makes gmock expectations not work -if (NOT MSVC) - target_sources(mysql_unittests PRIVATE unit/detail/protocol/channel.cpp) -endif() - target_include_directories( - mysql_unittests + boost_mysql_unittests PRIVATE common ) target_link_libraries( - mysql_unittests + boost_mysql_unittests PRIVATE gtest gtest_main gmock - Boost_mysql + Boost::mysql ) -_mysql_common_target_settings(mysql_unittests) +common_target_settings(boost_mysql_unittests) if (BOOST_MYSQL_VALGRIND_TESTS) - Mysqlvalgrind_AddMemcheckTest( - NAME mysql_unittests_memcheck - COMMAND ${CMAKE_CURRENT_BINARY_DIR}/mysql_unittests + add_memcheck_test( + NAME boost_mysql_unittests_memcheck + COMMAND ${CMAKE_CURRENT_BINARY_DIR}/boost_mysql_unittests ) else() add_test( - NAME mysql_unittests - COMMAND ${CMAKE_CURRENT_BINARY_DIR}/mysql_unittests + NAME boost_mysql_unittests + COMMAND ${CMAKE_CURRENT_BINARY_DIR}/boost_mysql_unittests ) endif() @@ -83,7 +79,7 @@ endif() find_package(Boost REQUIRED COMPONENTS coroutine) add_executable( - mysql_integrationtests + boost_mysql_integrationtests integration/network_functions/network_functions_impl.cpp integration/network_functions/sync.cpp integration/network_functions/async_callback.cpp @@ -105,83 +101,76 @@ add_executable( integration/close_connection.cpp ) target_link_libraries( - mysql_integrationtests + boost_mysql_integrationtests PRIVATE gtest gtest_main gmock - Boost_mysql + Boost::mysql Boost::coroutine ) target_include_directories( - mysql_integrationtests + boost_mysql_integrationtests PRIVATE common ) -_mysql_common_target_settings(mysql_integrationtests) +common_target_settings(boost_mysql_integrationtests) # Integration test setup -_mysql_sql_setup_fixture( - TEST_NAME mysql_integrationtests_setup - FIXTURE_NAME mysql_integrationtests_fixture +add_sql_setup_fixture( + TEST_NAME boost_mysql_integrationtests_setup + FIXTURE_NAME boost_mysql_integrationtests_fixture SQL_FILE ${CMAKE_CURRENT_SOURCE_DIR}/integration/db_setup.sql SKIP_VAR BOOST_MYSQL_SKIP_DB_SETUP ) # Regular integration tests add_test( - NAME mysql_integrationtests + NAME boost_mysql_integrationtests COMMAND - ${CMAKE_CURRENT_BINARY_DIR}/mysql_integrationtests + ${CMAKE_CURRENT_BINARY_DIR}/boost_mysql_integrationtests "--gtest_filter=-*RequiresSha256*" # Exclude anything using SHA256 ) set_tests_properties( - mysql_integrationtests - PROPERTIES FIXTURES_REQUIRED mysql_integrationtests_setup + boost_mysql_integrationtests + PROPERTIES FIXTURES_REQUIRED boost_mysql_integrationtests_setup ) # If we are using memcheck, then run a subset of the integration tests # under valgrind. Coroutine tests don't work well under Valgrind, and -# SSL tests are too slow +# SSL tests are too slow. We do some other exclusions to reduce runtime if (BOOST_MYSQL_VALGRIND_TESTS) - if (BOOST_MYSQL_SHA256_TESTS) - set(GTEST_FILTER "--gtest_filter=-*sslrequire*:*coroutine*") - else() - set(GTEST_FILTER "--gtest_filter=-*RequiresSha256*:*sslrequire*:*coroutine*") - endif() - - add_test( - NAME mysql_integrationtests_memcheck - COMMAND - ${CMAKE_CURRENT_BINARY_DIR}/mysql_integrationtests - ${GTEST_FILTER} + set(GTEST_FILTER "--gtest_filter=-*RequiresSha256*:*sslrequire*:*coroutine*:*UNIX*:*noerrinfo*:*TCPDefaultToken*") + add_memcheck_test( + NAME boost_mysql_integrationtests_memcheck + COMMAND ${CMAKE_CURRENT_BINARY_DIR}/boost_mysql_integrationtests ${GTEST_FILTER} ) set_tests_properties( - mysql_integrationtests_memcheck - PROPERTIES FIXTURES_REQUIRED mysql_integrationtests_setup + boost_mysql_integrationtests_memcheck + PROPERTIES FIXTURES_REQUIRED boost_mysql_integrationtests_setup ) endif() # SHA256 tests if (BOOST_MYSQL_SHA256_TESTS) # Setup - _mysql_sql_setup_fixture( - TEST_NAME mysql_integrationtests_sha256_setup - FIXTURE_NAME mysql_integrationtests_sha256_fixture + add_sql_setup_fixture( + TEST_NAME boost_mysql_integrationtests_sha256_setup + FIXTURE_NAME boost_mysql_integrationtests_sha256_fixture SQL_FILE ${CMAKE_CURRENT_SOURCE_DIR}/integration/db_setup_sha256.sql SKIP_VAR BOOST_MYSQL_SKIP_DB_SETUP ) # Actual tests add_test( - NAME mysql_integrationtests_sha256 + NAME boost_mysql_integrationtests_sha256 COMMAND - ${CMAKE_CURRENT_BINARY_DIR}/mysql_integrationtests + ${CMAKE_CURRENT_BINARY_DIR}/boost_mysql_integrationtests "--gtest_filter=*RequiresSha256*" # Run only SHA256 stuff ) set_tests_properties( - mysql_integrationtests_sha256 + boost_mysql_integrationtests_sha256 PROPERTIES FIXTURES_REQUIRED - "mysql_integrationtests_fixture;mysql_integrationtests_sha256_fixture" + "boost_mysql_integrationtests_fixture;boost_mysql_integrationtests_sha256_fixture" ) endif() diff --git a/tools/build_date.py b/tools/build_date.py new file mode 100755 index 00000000..5dc28e15 --- /dev/null +++ b/tools/build_date.py @@ -0,0 +1,42 @@ +# +# Copyright (c) 2019-2020 Ruben Perez Hidalgo (rubenperez038 at gmail dot com) +# +# Distributed under the Boost Software License, Version 1.0. (See accompanying +# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +# + +from sys import argv +from subprocess import check_call +from os import mkdir, chdir, makedirs + +VERSION_TAG = 'v2.4.1' + +def usage(): + print('Usage: {} '.format(argv[0])) + exit(1) + +def main(): + if len(argv) != 2: + usage() + install_dir = argv[1] + print('Installing date in ', install_dir) + makedirs(install_dir) + check_call(['git', 'clone', 'https://github.com/HowardHinnant/date.git']) + chdir('date') + check_call(['git', 'checkout', VERSION_TAG]) + mkdir('build') + chdir('build') + check_call([ + 'cmake', + '-G', + 'Ninja', + '-DCMAKE_BUILD_TYPE=Release', + '-DENABLE_DATE_TESTING=OFF', + '-DUSE_SYSTEM_TZ_DB=ON', + '-DCMAKE_INSTALL_PREFIX={}'.format(install_dir), + '..' + ]) + check_call(['cmake', '--build', '.', '--target', 'install']) + +if __name__ == '__main__': + main() diff --git a/ci/build_unix.sh b/tools/build_unix.sh similarity index 72% rename from ci/build_unix.sh rename to tools/build_unix.sh index c7ab7786..a9d8ed3d 100644 --- a/ci/build_unix.sh +++ b/tools/build_unix.sh @@ -40,43 +40,50 @@ function build_boost { # SHA256 functionality is only supported in MySQL 8+. From our # CI systems, only OSX has this version. -cp ci/*.pem /tmp # Copy SSL certs/keys to a known location +cp tools/ssl/*.pem /tmp # Copy SSL certs/keys to a known location if [ $TRAVIS_OS_NAME == "osx" ]; then brew update - brew install $DATABASE lcov - cp ci/unix-ci.cnf ~/.my.cnf # This location is checked by both MySQL and MariaDB + brew install $DATABASE lcov ninja + cp tools/unix-ci.cnf ~/.my.cnf # This location is checked by both MySQL and MariaDB sudo mkdir -p /var/run/mysqld/ sudo chmod 777 /var/run/mysqld/ mysql.server start # Note that running this with sudo fails if [ $DATABASE == "mariadb" ]; then - sudo mysql -u root < ci/root_user_setup.sql + sudo mysql -u root < tools/root_user_setup.sql fi + OPENSSL_ROOT_DIR_ARG=-DOPENSSL_ROOT_DIR=/usr/local/opt/openssl else - sudo cp ci/unix-ci.cnf /etc/mysql/conf.d/ + sudo cp tools/unix-ci.cnf /etc/mysql/conf.d/ sudo service mysql restart get_cmake # OSX cmake is good enough fi build_boost +python3 tools/build_date.py /tmp/date/ mkdir -p build cd build -cmake -DCMAKE_BUILD_TYPE=$CMAKE_BUILD_TYPE \ +cmake \ + -DCMAKE_INSTALL_PREFIX=/tmp/boost_mysql \ + -DCMAKE_PREFIX_PATH=/tmp/date/ \ + -DCMAKE_BUILD_TYPE=$CMAKE_BUILD_TYPE \ $(if [ $USE_VALGRIND ]; then echo -DBOOST_MYSQL_VALGRIND_TESTS=ON; fi) \ $(if [ $USE_COVERAGE ]; then echo -DBOOST_MYSQL_COVERAGE=ON; fi) \ $(if [ $HAS_SHA256 ]; then echo -DBOOST_MYSQL_SHA256_TESTS=ON; fi) \ + $OPENSSL_ROOT_DIR_ARG \ -DCMAKE_CXX_FLAGS="$CMAKE_CXX_FLAGS" \ + -DBOOST_MYSQL_ALLOW_FETCH_CONTENT=OFF \ $CMAKE_OPTIONS \ .. -make -j6 CTEST_OUTPUT_ON_FAILURE=1 all test +make -j6 CTEST_OUTPUT_ON_FAILURE=1 all install test # Coverage collection if [ $USE_COVERAGE ]; then # Select the gcov tool to use if [ "$TRAVIS_OS_NAME" == "osx" ]; then - GCOV_TOOL="$TRAVIS_BUILD_DIR/ci/clang-gcov-osx.sh" + GCOV_TOOL="$TRAVIS_BUILD_DIR/tools/clang-gcov-osx.sh" elif [ "$TRAVIS_COMPILER" == "clang" ]; then - GCOV_TOOL="$TRAVIS_BUILD_DIR/ci/clang-gcov-linux.sh" + GCOV_TOOL="$TRAVIS_BUILD_DIR/tools/clang-gcov-linux.sh" else GCOV_TOOL=gcov fi; @@ -91,3 +98,8 @@ if [ $USE_COVERAGE ]; then curl -s https://codecov.io/bash -o codecov.sh bash +x codecov.sh -f coverage.info fi + +# Test that a project using our cmake can do it +python3 ../tools/user_project_find_package/build.py \ + "-DCMAKE_PREFIX_PATH=/tmp/boost_mysql;/tmp/date" \ + $OPENSSL_ROOT_DIR_ARG diff --git a/ci/clang-gcov-linux.sh b/tools/clang-gcov-linux.sh similarity index 100% rename from ci/clang-gcov-linux.sh rename to tools/clang-gcov-linux.sh diff --git a/ci/clang-gcov-osx.sh b/tools/clang-gcov-osx.sh similarity index 100% rename from ci/clang-gcov-osx.sh rename to tools/clang-gcov-osx.sh diff --git a/ci/root_user_setup.sql b/tools/root_user_setup.sql similarity index 100% rename from ci/root_user_setup.sql rename to tools/root_user_setup.sql diff --git a/ci/ca-cert.pem b/tools/ssl/ca-cert.pem similarity index 100% rename from ci/ca-cert.pem rename to tools/ssl/ca-cert.pem diff --git a/ci/server-cert.pem b/tools/ssl/server-cert.pem similarity index 100% rename from ci/server-cert.pem rename to tools/ssl/server-cert.pem diff --git a/ci/server-key.pem b/tools/ssl/server-key.pem similarity index 100% rename from ci/server-key.pem rename to tools/ssl/server-key.pem diff --git a/ci/unix-ci.cnf b/tools/unix-ci.cnf similarity index 100% rename from ci/unix-ci.cnf rename to tools/unix-ci.cnf diff --git a/tools/user_project_find_package/CMakeLists.txt b/tools/user_project_find_package/CMakeLists.txt new file mode 100644 index 00000000..d7f55897 --- /dev/null +++ b/tools/user_project_find_package/CMakeLists.txt @@ -0,0 +1,23 @@ +# +# Copyright (c) 2019-2020 Ruben Perez Hidalgo (rubenperez038 at gmail dot com) +# +# Distributed under the Boost Software License, Version 1.0. (See accompanying +# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +# + +# A sample project that imports Boost.Mysql using find_package +# Used to test our CMake exports + +cmake_minimum_required(VERSION 3.13.0) +project(user_project) + +find_package(boost_mysql REQUIRED) + +add_executable(main query_sync.cpp) +target_link_libraries(main PRIVATE Boost::mysql) + +enable_testing() +add_test( + NAME test + COMMAND ${CMAKE_CURRENT_BINARY_DIR}/main example_user example_password +) diff --git a/tools/user_project_find_package/build.py b/tools/user_project_find_package/build.py new file mode 100644 index 00000000..c0f00543 --- /dev/null +++ b/tools/user_project_find_package/build.py @@ -0,0 +1,30 @@ +# +# Copyright (c) 2019-2020 Ruben Perez Hidalgo (rubenperez038 at gmail dot com) +# +# Distributed under the Boost Software License, Version 1.0. (See accompanying +# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +# + +from sys import argv +from os import mkdir, chdir, path +from shutil import copy +from subprocess import check_call + +PROJECT_DIR = 'user_project_find_package' + +def main(): + cmake_args = argv[1:] + this_dir = path.dirname(path.realpath(__file__)) + mkdir(PROJECT_DIR) + copy(path.join(this_dir, 'CMakeLists.txt'), PROJECT_DIR) + copy(path.join(this_dir, '..', '..', 'example', 'query_sync.cpp'), PROJECT_DIR) + chdir(PROJECT_DIR) + mkdir('build') + chdir('build') + check_call(['cmake'] + cmake_args + ['-G', 'Ninja', '..']) + check_call(['cmake', '--build', '.']) + check_call(['ctest', '--output-on-failure']) + +if __name__ == '__main__': + main() + \ No newline at end of file diff --git a/ci/windows-ci.cnf b/tools/windows-ci.cnf similarity index 100% rename from ci/windows-ci.cnf rename to tools/windows-ci.cnf