Skip to content
Snippets Groups Projects
Verified Commit beae0b8b authored by Timm Fitschen's avatar Timm Fitschen
Browse files

c -> c++

parent 7a02694a
No related branches found
No related tags found
No related merge requests found
FROM debian:latest FROM debian:latest
RUN apt-get update && \
apt-get install -y cmake RUN echo "deb http://deb.debian.org/debian buster-backports main" > /etc/apt/sources.list.d/buster-backports.list
RUN apt-get install -y libcmocka-dev RUN apt-get update
RUN apt-get install -y cmake
RUN apt-get install -y lcov RUN apt-get install -y lcov
RUN apt-get install -y doxygen RUN apt-get install -y doxygen
RUN apt-get install -y clang-format-11 clang-tidy-11
RUN apt-get install -y python3-pip RUN apt-get install -y python3-pip
COPY doc/requirements.txt requirements.txt COPY doc/requirements.txt requirements.txt
RUN pip3 install -r 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
...@@ -40,7 +40,7 @@ build-testenv: ...@@ -40,7 +40,7 @@ build-testenv:
# use here general latest or specific branch latest... # use here general latest or specific branch latest...
- docker pull $CI_REGISTRY_IMAGE|| true - docker pull $CI_REGISTRY_IMAGE|| true
- docker build - docker build
-f test/.docker/Dockerfile -f .docker/Dockerfile
--pull --pull
--cache-from $CI_REGISTRY_IMAGE --cache-from $CI_REGISTRY_IMAGE
-t $CI_REGISTRY_IMAGE . -t $CI_REGISTRY_IMAGE .
...@@ -51,7 +51,7 @@ code_style: ...@@ -51,7 +51,7 @@ code_style:
tags: [ docker ] tags: [ docker ]
stage: test stage: test
script: script:
- clang-format-11 --dry-run --verbose **/*.c **/*.h **/*.h.in - clang-format-11 --dry-run --verbose **/*.cpp **/*.h **/*.h.in
allow_failure: true allow_failure: true
# cmocka tests # cmocka tests
......
...@@ -4,8 +4,11 @@ set(libcaosdb_VERSION 0.0.1) ...@@ -4,8 +4,11 @@ set(libcaosdb_VERSION 0.0.1)
project(libcaosdb project(libcaosdb
VERSION ${libcaosdb_VERSION} VERSION ${libcaosdb_VERSION}
DESCRIPTION "Plain C client libraries for CaosDB" DESCRIPTION "C++ client libraries for CaosDB"
LANGUAGES C) 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(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})
set(PROJECT_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/include") set(PROJECT_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/include")
...@@ -15,7 +18,7 @@ if(NOT iwyu) ...@@ -15,7 +18,7 @@ if(NOT iwyu)
message(STATUS "include-what-you-use: Not found") message(STATUS "include-what-you-use: Not found")
else() else()
message(STATUS "include-what-you-use: ${iwyu}") message(STATUS "include-what-you-use: ${iwyu}")
set(CMAKE_C_INCLUDE_WHAT_YOU_USE ${iwyu}) set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE ${iwyu})
endif() endif()
find_program(clang_tidy NAMES clang-tidy clang-tidy-11) find_program(clang_tidy NAMES clang-tidy clang-tidy-11)
...@@ -23,20 +26,23 @@ if(NOT clang_tidy) ...@@ -23,20 +26,23 @@ if(NOT clang_tidy)
message(STATUS "clang-tidy: Not found") message(STATUS "clang-tidy: Not found")
else() else()
message(STATUS "clang-tidy: ${clang_tidy}") 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() endif()
add_subdirectory(src) add_subdirectory(src)
add_subdirectory(include) add_subdirectory(include)
add_subdirectory(doc) add_subdirectory(doc)
add_library(libcaosdb STATIC ${libcaosdb_INCL} ${libcaosdb_SRC}) add_library(caosdb STATIC ${libcaosdb_INCL} ${libcaosdb_SRC})
add_executable(caosdbcli src/caosdbcli.c) add_executable(caosdbcli src/caosdbcli.cpp)
target_link_libraries(caosdbcli libcaosdb) target_link_libraries(caosdbcli caosdb)
#set_target_properties(caosdbcli libcaosdb PROPERTIES C_CLANG_TIDY "${DO_CLANG_TIDY}")
if("${CMAKE_BUILD_TYPE}" MATCHES "Debug") 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() enable_testing()
add_subdirectory(test) add_subdirectory(test)
append_coverage_compiler_flags() append_coverage_compiler_flags()
...@@ -53,8 +59,8 @@ set(libcaosdb_LIB_DEST "lib/libcaosdb-${libcaosdb_VERSION}") ...@@ -53,8 +59,8 @@ set(libcaosdb_LIB_DEST "lib/libcaosdb-${libcaosdb_VERSION}")
# installing headers changes the include path specify that libcaosdb requires # installing headers changes the include path specify that libcaosdb requires
# the files located in the include/ directory at compile time. This would # the files located in the include/ directory at compile time. This would
# normally look like # normally look like
# target_include_directories(libcaosdb PUBLIC include/) # target_include_directories(caosdb PUBLIC include/)
# PUBLIC means that other libraries including libcaosdb should also include # PUBLIC means that other libraries including caosdb should also include
# the directory include/. # the directory include/.
# However, there is a catch. If we are installing the project in # 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 # 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}") ...@@ -63,7 +69,7 @@ set(libcaosdb_LIB_DEST "lib/libcaosdb-${libcaosdb_VERSION}")
# The following CMake command handles this. $<BUILD_INTERFACE:...> and # The following CMake command handles this. $<BUILD_INTERFACE:...> and
# $<INSTALL_INTERFACE:...> are macros whose values change depending on if we # $<INSTALL_INTERFACE:...> are macros whose values change depending on if we
# are simply building the code or if we are installing it. # 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 # headers to include when building from source
$<BUILD_INTERFACE:${libcaosdb_SOURCE_DIR}/include> $<BUILD_INTERFACE:${libcaosdb_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${libcaosdb_BINARY_DIR}/include> $<BUILD_INTERFACE:${libcaosdb_BINARY_DIR}/include>
...@@ -91,7 +97,7 @@ target_include_directories(libcaosdb PUBLIC ...@@ -91,7 +97,7 @@ target_include_directories(libcaosdb PUBLIC
# libcaosdb with libcaosdbTargets. # libcaosdb with libcaosdbTargets.
install( install(
# targets to install # targets to install
TARGETS libcaosdb TARGETS caosdb
# name of the CMake "export group" containing the targets we want to install # name of the CMake "export group" containing the targets we want to install
EXPORT libcaosdbTargets EXPORT libcaosdbTargets
# Dynamic, static library and include destination locations after running # Dynamic, static library and include destination locations after running
...@@ -104,8 +110,8 @@ install( ...@@ -104,8 +110,8 @@ install(
# We now need to install the export libcaosdbTargets that we defined above. # We now need to install the export libcaosdbTargets that we defined above.
# This is needed in order for another project to import libcaosdb using # This is needed in order for another project to import libcaosdb using
# find_package(libcaosdb) # find_package(caosdb)
# find_package(libcaosdb) will look for libcaosdb-config.cmake to provide # find_package(caosdb) will look for caosdb-config.cmake to provide
# information about the targets contained in the project libcaosdb. # information about the targets contained in the project libcaosdb.
# Fortunately, this is specified in the export libcaosdbTargets, so we will # Fortunately, this is specified in the export libcaosdbTargets, so we will
# install this too. # install this too.
...@@ -119,8 +125,8 @@ install( ...@@ -119,8 +125,8 @@ install(
# CMake file in which to store the export's information # CMake file in which to store the export's information
FILE libcaosdbTargets.cmake FILE libcaosdbTargets.cmake
# Namespace prepends all targets in the export (when we import later, we # Namespace prepends all targets in the export (when we import later, we
# will use libcaosdb::libcaosdb) # will use caosdb::caosdb)
NAMESPACE libcaosdb:: NAMESPACE caosdb::
# where to place the resulting file (here, we're putting it with the library) # where to place the resulting file (here, we're putting it with the library)
DESTINATION ${libcaosdb_LIB_DEST} DESTINATION ${libcaosdb_LIB_DEST}
) )
......
# 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)
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)
set(libcaosdb_INCL set(libcaosdb_INCL
include/libcaosdbConfig.h include/caosdb_config.h
include/connection.h include/connection.h
) )
set(libcaosdb_INCL ${libcaosdb_INCL} PARENT_SCOPE) set(libcaosdb_INCL ${libcaosdb_INCL} PARENT_SCOPE)
configure_file(libcaosdbConfig.h.in libcaosdbConfig.h) configure_file(caosdb_config.h.in caosdb_config.h)
#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
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
* @date 2021-05-18 * @date 2021-05-18
* @brief Configuration and setup of the connection. * @brief Configuration and setup of the connection.
*/ */
#include <string>
/** /**
* @brief Configure the connection. * @brief Configure the connection.
...@@ -13,5 +14,7 @@ ...@@ -13,5 +14,7 @@
* @param port Port of the CaosDB server. * @param port Port of the CaosDB server.
* @return 0 if everything is ok. * @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 #endif
#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
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
# file(GLOB ...) or not, you will need to re-run cmake, but with an explicit # 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. # file list, you know beforehand why your code isn't compiling.
set(libcaosdb_SRC set(libcaosdb_SRC
src/connection.c src/connection.cpp
) )
# ... and pass the variable to the parent scope. # ... and pass the variable to the parent scope.
......
// 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;
}
// 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);
}
#include "connection.h" #include "connection.h"
#include <stdio.h> #include <iostream>
int configure_connection(char host[], int port) { namespace caosdb {
auto configure_connection(const std::string &host, int port) -> int {
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers) // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
if (port > 666 && port < 668) { if (port > 666 && port < 668) {
printf("667 - One step ahead of the Devil!"); std::cout << "667 - One step ahead of the Devil!\n";
} }
printf("Configure connection: %s:%d\n", host, port); std::cout << "Configure connection: " << host << ":" << port << "\n";
return 0; return 0;
} }
} // namespace caosdb
# 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 set(test_cases
test_connection test_connection
) )
# look for pre-installed cmocka headers include(FetchGTest)
find_library(cmocka cmocka) include(GoogleTest)
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 ()
# 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) list(LENGTH test_cases len_test_cases)
math(EXPR len_test_cases "${len_test_cases} - 1") math(EXPR len_test_cases "${len_test_cases} - 1")
foreach (i RANGE "${len_test_cases}") foreach (i RANGE "${len_test_cases}")
list(GET test_cases ${i} test_case_name) list(GET test_cases ${i} test_case_name)
add_executable(${test_case_name} ${test_case_name}.c) add_executable(${test_case_name} ${test_case_name}.cpp)
target_compile_features(${test_case_name} PRIVATE c_std_99) target_link_libraries(${test_case_name} PRIVATE gtest_main caosdb)
target_link_libraries(${test_case_name} PRIVATE ${cmocka-link} libcaosdb) set_target_properties(${test_case_name} PROPERTIES CXX_CLANG_TIDY "")
add_test(${test_case_name} ${test_case_name}) gtest_discover_tests(${test_case_name})
endforeach () endforeach ()
# code coverage report # code coverage report
include(CodeCoverage) include(CodeCoverage)
...@@ -38,7 +30,7 @@ if (LCOV_PATH) ...@@ -38,7 +30,7 @@ if (LCOV_PATH)
setup_target_for_coverage_lcov( setup_target_for_coverage_lcov(
NAME unit_test_coverage NAME unit_test_coverage
EXECUTABLE ctest EXECUTABLE ctest
DEPEDENCIES libcaosdb DEPEDENCIES caosdb
LCOV_ARGS --rc lcov_branch_coverage=1 LCOV_ARGS --rc lcov_branch_coverage=1
GENHTML_ARGS --rc lcov_branch_coverage=1 GENHTML_ARGS --rc lcov_branch_coverage=1
) )
......
#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);
}
#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));
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment