From beae0b8bebe5c8b9a8c89f64adf6d1973a2a3c57 Mon Sep 17 00:00:00 2001 From: Timm Fitschen <t.fitschen@indiscale.com> Date: Thu, 20 May 2021 22:17:47 +0200 Subject: [PATCH] c -> c++ --- {test/.docker => .docker}/Dockerfile | 12 ++++----- .gitlab-ci.yml | 4 +-- CMakeLists.txt | 40 ++++++++++++++++------------ cmake/FetchCMocka.cmake | 29 -------------------- cmake/FetchGTest.cmake | 11 ++++++++ include/CMakeLists.txt | 4 +-- include/caosdb_config.h.in | 10 +++++++ include/connection.h | 5 +++- include/libcaosdbConfig.h.in | 8 ------ src/CMakeLists.txt | 2 +- src/caosdbcli.c | 16 ----------- src/caosdbcli.cpp | 16 +++++++++++ src/connection.c | 11 -------- src/connection.cpp | 13 +++++++++ test/CMakeLists.txt | 34 +++++++++-------------- test/test_connection.c | 18 ------------- test/test_connection.cpp | 9 +++++++ 17 files changed, 109 insertions(+), 133 deletions(-) rename {test/.docker => .docker}/Dockerfile (66%) delete mode 100644 cmake/FetchCMocka.cmake create mode 100644 cmake/FetchGTest.cmake create mode 100644 include/caosdb_config.h.in delete mode 100644 include/libcaosdbConfig.h.in delete mode 100644 src/caosdbcli.c create mode 100644 src/caosdbcli.cpp delete mode 100644 src/connection.c create mode 100644 src/connection.cpp delete mode 100644 test/test_connection.c create mode 100644 test/test_connection.cpp diff --git a/test/.docker/Dockerfile b/.docker/Dockerfile similarity index 66% rename from test/.docker/Dockerfile rename to .docker/Dockerfile index 590430c..f91c47e 100644 --- a/test/.docker/Dockerfile +++ b/.docker/Dockerfile @@ -1,15 +1,13 @@ FROM debian:latest -RUN apt-get update && \ - apt-get install -y cmake -RUN apt-get install -y libcmocka-dev + +RUN echo "deb http://deb.debian.org/debian buster-backports main" > /etc/apt/sources.list.d/buster-backports.list +RUN apt-get update +RUN apt-get install -y cmake RUN apt-get install -y lcov RUN apt-get install -y doxygen +RUN apt-get install -y clang-format-11 clang-tidy-11 RUN apt-get install -y python3-pip COPY doc/requirements.txt requirements.txt RUN pip3 install -r requirements.txt -RUN echo "deb http://deb.debian.org/debian buster-backports main" > /etc/apt/sources.list.d/buster-backports.list -RUN apt-get update -RUN apt-get install -y clang-format-11 -RUN apt-get install -y clang-tidy-11 diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index b8c7945..a413511 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -40,7 +40,7 @@ build-testenv: # use here general latest or specific branch latest... - docker pull $CI_REGISTRY_IMAGE|| true - docker build - -f test/.docker/Dockerfile + -f .docker/Dockerfile --pull --cache-from $CI_REGISTRY_IMAGE -t $CI_REGISTRY_IMAGE . @@ -51,7 +51,7 @@ code_style: tags: [ docker ] stage: test script: - - clang-format-11 --dry-run --verbose **/*.c **/*.h **/*.h.in + - clang-format-11 --dry-run --verbose **/*.cpp **/*.h **/*.h.in allow_failure: true # cmocka tests diff --git a/CMakeLists.txt b/CMakeLists.txt index 435e2ed..d1da6f6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,8 +4,11 @@ set(libcaosdb_VERSION 0.0.1) project(libcaosdb VERSION ${libcaosdb_VERSION} - DESCRIPTION "Plain C client libraries for CaosDB" - LANGUAGES C) + DESCRIPTION "C++ client libraries for CaosDB" + LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH}) set(PROJECT_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/include") @@ -15,7 +18,7 @@ if(NOT iwyu) message(STATUS "include-what-you-use: Not found") else() message(STATUS "include-what-you-use: ${iwyu}") - set(CMAKE_C_INCLUDE_WHAT_YOU_USE ${iwyu}) + set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE ${iwyu}) endif() find_program(clang_tidy NAMES clang-tidy clang-tidy-11) @@ -23,20 +26,23 @@ if(NOT clang_tidy) message(STATUS "clang-tidy: Not found") else() message(STATUS "clang-tidy: ${clang_tidy}") - set(CMAKE_C_CLANG_TIDY "${clang_tidy}" "--header-filter=." "--checks=*,-llvm-include-order,-llvmlibc-restrict-system-libc-headers" "--warnings-as-errors=*") + set(CMAKE_CXX_CLANG_TIDY "${clang_tidy}" "--header-filter=." + "--checks=*,-fuchsia-*,-llvm-include-order,-llvmlibc-*" + "--warnings-as-errors=*") endif() add_subdirectory(src) add_subdirectory(include) add_subdirectory(doc) -add_library(libcaosdb STATIC ${libcaosdb_INCL} ${libcaosdb_SRC}) -add_executable(caosdbcli src/caosdbcli.c) -target_link_libraries(caosdbcli libcaosdb) -#set_target_properties(caosdbcli libcaosdb PROPERTIES C_CLANG_TIDY "${DO_CLANG_TIDY}") +add_library(caosdb STATIC ${libcaosdb_INCL} ${libcaosdb_SRC}) +add_executable(caosdbcli src/caosdbcli.cpp) +target_link_libraries(caosdbcli caosdb) if("${CMAKE_BUILD_TYPE}" MATCHES "Debug") - target_link_libraries(libcaosdb gcov) + # supress warnings during build of gtest + cmake_policy(SET CMP0054 NEW) + target_link_libraries(caosdb gcov) enable_testing() add_subdirectory(test) append_coverage_compiler_flags() @@ -53,8 +59,8 @@ set(libcaosdb_LIB_DEST "lib/libcaosdb-${libcaosdb_VERSION}") # installing headers changes the include path specify that libcaosdb requires # the files located in the include/ directory at compile time. This would # normally look like -# target_include_directories(libcaosdb PUBLIC include/) -# PUBLIC means that other libraries including libcaosdb should also include +# target_include_directories(caosdb PUBLIC include/) +# PUBLIC means that other libraries including caosdb should also include # the directory include/. # However, there is a catch. If we are installing the project in # CMAKE_INSTALL_PREFIX, we can't specify include/ in the build directory: we @@ -63,7 +69,7 @@ set(libcaosdb_LIB_DEST "lib/libcaosdb-${libcaosdb_VERSION}") # The following CMake command handles this. $<BUILD_INTERFACE:...> and # $<INSTALL_INTERFACE:...> are macros whose values change depending on if we # are simply building the code or if we are installing it. -target_include_directories(libcaosdb PUBLIC +target_include_directories(caosdb PUBLIC # headers to include when building from source $<BUILD_INTERFACE:${libcaosdb_SOURCE_DIR}/include> $<BUILD_INTERFACE:${libcaosdb_BINARY_DIR}/include> @@ -91,7 +97,7 @@ target_include_directories(libcaosdb PUBLIC # libcaosdb with libcaosdbTargets. install( # targets to install - TARGETS libcaosdb + TARGETS caosdb # name of the CMake "export group" containing the targets we want to install EXPORT libcaosdbTargets # Dynamic, static library and include destination locations after running @@ -104,8 +110,8 @@ install( # We now need to install the export libcaosdbTargets that we defined above. # This is needed in order for another project to import libcaosdb using -# find_package(libcaosdb) -# find_package(libcaosdb) will look for libcaosdb-config.cmake to provide +# find_package(caosdb) +# find_package(caosdb) will look for caosdb-config.cmake to provide # information about the targets contained in the project libcaosdb. # Fortunately, this is specified in the export libcaosdbTargets, so we will # install this too. @@ -119,8 +125,8 @@ install( # CMake file in which to store the export's information FILE libcaosdbTargets.cmake # Namespace prepends all targets in the export (when we import later, we - # will use libcaosdb::libcaosdb) - NAMESPACE libcaosdb:: + # will use caosdb::caosdb) + NAMESPACE caosdb:: # where to place the resulting file (here, we're putting it with the library) DESTINATION ${libcaosdb_LIB_DEST} ) diff --git a/cmake/FetchCMocka.cmake b/cmake/FetchCMocka.cmake deleted file mode 100644 index 77ba8f9..0000000 --- a/cmake/FetchCMocka.cmake +++ /dev/null @@ -1,29 +0,0 @@ -# Copyright 2020 OLIVIER LE DOEUFF -# -# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -include(FetchContent) - -# Declare our target. We want the lastest stable version, not the master. -# Also specify GIT_SHALLOW to avoid cloning branch we don't care about -FetchContent_Declare( - cmocka - GIT_REPOSITORY https://git.cryptomilk.org/projects/cmocka.git - GIT_TAG cmocka-1.1.5 - GIT_SHALLOW 1 -) - -# We want to link to cmocka-static, so we need to set this option before calling the FetchContent_MakeAvailable -# We also don't care about example and tests -set(WITH_STATIC_LIB ON CACHE BOOL "CMocka: Build with a static library" FORCE) -set(WITH_CMOCKERY_SUPPORT OFF CACHE BOOL "CMocka: Install a cmockery header" FORCE) -set(WITH_EXAMPLES OFF CACHE BOOL "CMocka: Build examples" FORCE) -set(UNIT_TESTING OFF CACHE BOOL "CMocka: Build with unit testing" FORCE) -set(PICKY_DEVELOPER OFF CACHE BOOL "CMocka: Build with picky developer flags" FORCE) - -# Download cmocka, and execute its cmakelists.txt -FetchContent_MakeAvailable(cmocka) diff --git a/cmake/FetchGTest.cmake b/cmake/FetchGTest.cmake new file mode 100644 index 0000000..b87975c --- /dev/null +++ b/cmake/FetchGTest.cmake @@ -0,0 +1,11 @@ +include(FetchContent) + +FetchContent_Declare( + googletest + GIT_REPOSITORY https://github.com/google/googletest.git + GIT_TAG release-1.10.0 + GIT_SHALLOW 1 +) +# For Windows: Prevent overriding the parent project's compiler/linker settings +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) +FetchContent_MakeAvailable(googletest) diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt index 6c4b0d5..0288f1a 100644 --- a/include/CMakeLists.txt +++ b/include/CMakeLists.txt @@ -1,8 +1,8 @@ set(libcaosdb_INCL - include/libcaosdbConfig.h + include/caosdb_config.h include/connection.h ) set(libcaosdb_INCL ${libcaosdb_INCL} PARENT_SCOPE) -configure_file(libcaosdbConfig.h.in libcaosdbConfig.h) +configure_file(caosdb_config.h.in caosdb_config.h) diff --git a/include/caosdb_config.h.in b/include/caosdb_config.h.in new file mode 100644 index 0000000..b92fc11 --- /dev/null +++ b/include/caosdb_config.h.in @@ -0,0 +1,10 @@ +#ifndef CAOSDB_CONFIG_H +#define CAOSDB_CONFIG_H +namespace caosdb { +// clang-format off +constexpr int LIBCAOSDB_VERSION_MAJOR = @libcaosdb_VERSION_MAJOR@; +constexpr int LIBCAOSDB_VERSION_MINOR = @libcaosdb_VERSION_MINOR@; +constexpr int LIBCAOSDB_VERSION_PATCH = @libcaosdb_VERSION_PATCH@; +// clang-format on +} // namespace caosdb +#endif diff --git a/include/connection.h b/include/connection.h index fb0c6c6..8d11db6 100644 --- a/include/connection.h +++ b/include/connection.h @@ -6,6 +6,7 @@ * @date 2021-05-18 * @brief Configuration and setup of the connection. */ +#include <string> /** * @brief Configure the connection. @@ -13,5 +14,7 @@ * @param port Port of the CaosDB server. * @return 0 if everything is ok. */ -int configure_connection(char host[], int port); +namespace caosdb { +auto configure_connection(const std::string &host, int port) -> int; +} // namespace caosdb #endif diff --git a/include/libcaosdbConfig.h.in b/include/libcaosdbConfig.h.in deleted file mode 100644 index de099a6..0000000 --- a/include/libcaosdbConfig.h.in +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef LIBCAOSDBCONFIG_H -#define LIBCAOSDBCONFIG_H -// clang-format off -#define LIBCAOSDB_VERSION_MAJOR @libcaosdb_VERSION_MAJOR@ -#define LIBCAOSDB_VERSION_MINOR @libcaosdb_VERSION_MINOR@ -#define LIBCAOSDB_VERSION_PATCH @libcaosdb_VERSION_PATCH@ -// clang-format on -#endif diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f5ba296..7c031e9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -7,7 +7,7 @@ # file(GLOB ...) or not, you will need to re-run cmake, but with an explicit # file list, you know beforehand why your code isn't compiling. set(libcaosdb_SRC - src/connection.c + src/connection.cpp ) # ... and pass the variable to the parent scope. diff --git a/src/caosdbcli.c b/src/caosdbcli.c deleted file mode 100644 index f6e439d..0000000 --- a/src/caosdbcli.c +++ /dev/null @@ -1,16 +0,0 @@ -// A simple caosdb client -#include "connection.h" -#include "libcaosdbConfig.h" -#include <stdio.h> - -int main(void) { - - printf("CaosDB (libcaosdb %d.%d.%d)\n", LIBCAOSDB_VERSION_MAJOR, - LIBCAOSDB_VERSION_MINOR, LIBCAOSDB_VERSION_PATCH); - printf("We don't miss the H of caos.\n"); - - // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers) - configure_connection("localhost", 8080); - - return 0; -} diff --git a/src/caosdbcli.cpp b/src/caosdbcli.cpp new file mode 100644 index 0000000..4562a63 --- /dev/null +++ b/src/caosdbcli.cpp @@ -0,0 +1,16 @@ +// A simple caosdb client +#include "connection.h" +#include "caosdb_config.h" +#include <memory> +#include <iostream> + +auto main() -> int { + + std::cout << "CaosDB (libcaosdb " << caosdb::LIBCAOSDB_VERSION_MINOR << "." + << caosdb::LIBCAOSDB_VERSION_MINOR << "." + << caosdb::LIBCAOSDB_VERSION_PATCH << ")" << std::endl; + std::cout << "We don't miss the H of caos." << std::endl; + + // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers) + return caosdb::configure_connection("localhost", 8080); +} diff --git a/src/connection.c b/src/connection.c deleted file mode 100644 index 32ce2bf..0000000 --- a/src/connection.c +++ /dev/null @@ -1,11 +0,0 @@ -#include "connection.h" -#include <stdio.h> - -int configure_connection(char host[], int port) { - // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers) - if (port > 666 && port < 668) { - printf("667 - One step ahead of the Devil!"); - } - printf("Configure connection: %s:%d\n", host, port); - return 0; -} diff --git a/src/connection.cpp b/src/connection.cpp new file mode 100644 index 0000000..e402e7e --- /dev/null +++ b/src/connection.cpp @@ -0,0 +1,13 @@ +#include "connection.h" +#include <iostream> + +namespace caosdb { +auto configure_connection(const std::string &host, int port) -> int { + // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers) + if (port > 666 && port < 668) { + std::cout << "667 - One step ahead of the Devil!\n"; + } + std::cout << "Configure connection: " << host << ":" << port << "\n"; + return 0; +} +} // namespace caosdb diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a6fd8b2..31c3d5a 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,36 +1,28 @@ -# append all the test cases here (file name without the ".c" suffix) +# append all the test cases here (file name without the ".cc" suffix) set(test_cases test_connection ) -# look for pre-installed cmocka headers -find_library(cmocka cmocka) -if (cmocka) - message(STATUS "Found pre-installed cmocka: ${cmocka-static}") - set(cmocka-link "cmocka") -elseif (${CMAKE_VERSION} VERSION_LESS "3.14.0") - message(FATAL_ERROR -"Failed to run tests. Pre-installed cmocka headers could not be found -and your cmake version is prior to 3.14. Please consider to upgrade your -cmake (>=3.14) or install cmocka headers.") -else () - # cmake >= 3.14 - include(FetchCMocka) - set(cmocka-link "cmocka-static") -endif () +include(FetchGTest) +include(GoogleTest) +# disable linting for gtest targets (not our concern) +set_target_properties(gtest PROPERTIES CXX_CLANG_TIDY "" CXX_INCLUDE_WHAT_YOU_USE "") +set_target_properties(gtest_main PROPERTIES CXX_CLANG_TIDY "" CXX_INCLUDE_WHAT_YOU_USE "") +set_target_properties(gmock PROPERTIES CXX_CLANG_TIDY "" CXX_INCLUDE_WHAT_YOU_USE "") list(LENGTH test_cases len_test_cases) math(EXPR len_test_cases "${len_test_cases} - 1") foreach (i RANGE "${len_test_cases}") list(GET test_cases ${i} test_case_name) - add_executable(${test_case_name} ${test_case_name}.c) - target_compile_features(${test_case_name} PRIVATE c_std_99) - target_link_libraries(${test_case_name} PRIVATE ${cmocka-link} libcaosdb) - add_test(${test_case_name} ${test_case_name}) + add_executable(${test_case_name} ${test_case_name}.cpp) + target_link_libraries(${test_case_name} PRIVATE gtest_main caosdb) + set_target_properties(${test_case_name} PROPERTIES CXX_CLANG_TIDY "") + gtest_discover_tests(${test_case_name}) endforeach () + # code coverage report include(CodeCoverage) @@ -38,7 +30,7 @@ if (LCOV_PATH) setup_target_for_coverage_lcov( NAME unit_test_coverage EXECUTABLE ctest - DEPEDENCIES libcaosdb + DEPEDENCIES caosdb LCOV_ARGS --rc lcov_branch_coverage=1 GENHTML_ARGS --rc lcov_branch_coverage=1 ) diff --git a/test/test_connection.c b/test/test_connection.c deleted file mode 100644 index c32aa14..0000000 --- a/test/test_connection.c +++ /dev/null @@ -1,18 +0,0 @@ -#include "connection.h" -#include <setjmp.h> // IWYU pragma: keep -#include <stdarg.h> // IWYU pragma: keep -#include <stddef.h> -#include <cmocka.h> - -// NOLINTNEXTLINE(misc-unused-parameters) -static void test_configure_connection(void **state) { - assert_int_equal(0, configure_connection("localhost", 8080)); -} - -int main() { - const struct CMUnitTest tests[] = { - cmocka_unit_test(test_configure_connection), - }; - - return cmocka_run_group_tests(tests, NULL, NULL); -} diff --git a/test/test_connection.cpp b/test/test_connection.cpp new file mode 100644 index 0000000..8b49ddc --- /dev/null +++ b/test/test_connection.cpp @@ -0,0 +1,9 @@ +#include "connection.h" +#include <gtest/gtest-message.h> +#include <gtest/gtest-test-part.h> +#include <memory> +#include "gtest/gtest_pred_impl.h" + +TEST(configure_connection, localhost_8080) { + EXPECT_EQ(0, caosdb::configure_connection("localhost", 8080)); +} -- GitLab