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

Merge branch 'main' into dev

parents c00d3074 5da26977
No related branches found
No related tags found
No related merge requests found
Pipeline #9552 passed with warnings
......@@ -22,6 +22,7 @@
variables:
CI_REGISTRY_IMAGE: $CI_REGISTRY/caosdb/src/caosdb-cpplib/testenv:latest
GIT_SUBMODULE_STRATEGY: normal
image: $CI_REGISTRY_IMAGE
......
[submodule "proto"]
path = proto
url = git@gitlab.indiscale.com:caosdb/src/caosdb-proto.git
url = ../caosdb-proto.git
......@@ -27,6 +27,9 @@ project(libcaosdb
DESCRIPTION "C++ client libraries for CaosDB"
LANGUAGES CXX)
# override options with variables (globally)
# needed for turning off unused code from grpc
cmake_policy(SET CMP0077 NEW)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
......@@ -38,7 +41,7 @@ if(NOT iwyu)
message(STATUS "include-what-you-use: Not found")
else()
message(STATUS "include-what-you-use: ${iwyu}")
set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE ${iwyu})
set(_CMAKE_CXX_INCLUDE_WHAT_YOU_USE ${iwyu})
endif()
find_program(clang_tidy NAMES clang-tidy clang-tidy-11)
......@@ -46,7 +49,7 @@ if(NOT clang_tidy)
message(STATUS "clang-tidy: Not found")
else()
message(STATUS "clang-tidy: ${clang_tidy}")
set(CMAKE_CXX_CLANG_TIDY "${clang_tidy}" "--header-filter=."
set(_CMAKE_CXX_CLANG_TIDY "${clang_tidy}" "--header-filter=connection.h"
"--checks=*,-fuchsia-*,-llvm-include-order,-llvmlibc-*"
"--warnings-as-errors=*")
endif()
......@@ -56,7 +59,15 @@ add_subdirectory(include)
add_subdirectory(doc)
add_library(caosdb STATIC ${libcaosdb_INCL} ${libcaosdb_SRC})
set_target_properties(caosdb PROPERTIES
CXX_CLANG_TIDY "${_CMAKE_CXX_CLANG_TIDY}"
CXX_INCLUDE_WHAT_YOU_USE "${_CMAKE_CXX_CLANG_TIDY}"
)
add_executable(caosdbcli src/caosdbcli.cpp)
set_target_properties(caosdbcli PROPERTIES
CXX_CLANG_TIDY "${_CMAKE_CXX_CLANG_TIDY}"
CXX_INCLUDE_WHAT_YOU_USE "${_CMAKE_CXX_CLANG_TIDY}"
)
target_link_libraries(caosdbcli caosdb)
if("${CMAKE_BUILD_TYPE}" MATCHES "Debug")
......@@ -65,7 +76,9 @@ if("${CMAKE_BUILD_TYPE}" MATCHES "Debug")
target_link_libraries(caosdb gcov)
enable_testing()
add_subdirectory(test)
append_coverage_compiler_flags()
set_target_properties(caosdb PROPERTIES
COMPILE_FLAGS "${CMAKE_CXX_FLAGS} ${COVERAGE_COMPILER_FLAGS}")
endif()
......@@ -99,33 +112,33 @@ target_include_directories(caosdb PUBLIC
)
# Install libcaosdb in CMAKE_INSTALL_PREFIX (defaults to /usr/local on linux).
# To change the install location, run
# cmake -DCMAKE_INSTALL_PREFIX=<desired-install-path> ..
# install(...) specifies installation rules for the project. It can specify
# location of installed files on the system, user permissions, build
# configurations, etc. Here, we are only copying files.
# install(TARGETS ...) specifies rules for installing targets.
# Here, we are taking a target or list of targets (libcosdb) and telling
# CMake the following:
# - put shared libraries associated with libcaosdb in ${libcaosdb_LIB_DEST}
# - put static libraries associated with libcaosdb in ${libcaosdb_LIB_DEST}
# - put include files associated with libcaosdb in ${libcaosdb_INCLUDE_DEST}
# We also need to specify the export that is associated with libcaosdb; an
# export is just a list of targets to be installed. So we are associating
# libcaosdb with libcaosdbTargets.
install(
# targets to install
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
# "make install"
LIBRARY DESTINATION ${libcaosdb_LIB_DEST}
ARCHIVE DESTINATION ${libcaosdb_LIB_DEST}
INCLUDES DESTINATION ${libcaosdb_INCLUDE_DEST}
)
## Install libcaosdb in CMAKE_INSTALL_PREFIX (defaults to /usr/local on linux).
## To change the install location, run
## cmake -DCMAKE_INSTALL_PREFIX=<desired-install-path> ..
## install(...) specifies installation rules for the project. It can specify
## location of installed files on the system, user permissions, build
## configurations, etc. Here, we are only copying files.
## install(TARGETS ...) specifies rules for installing targets.
## Here, we are taking a target or list of targets (libcosdb) and telling
## CMake the following:
## - put shared libraries associated with libcaosdb in ${libcaosdb_LIB_DEST}
## - put static libraries associated with libcaosdb in ${libcaosdb_LIB_DEST}
## - put include files associated with libcaosdb in ${libcaosdb_INCLUDE_DEST}
## We also need to specify the export that is associated with libcaosdb; an
## export is just a list of targets to be installed. So we are associating
## libcaosdb with libcaosdbTargets.
#install(
## targets to install
#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
## "make install"
#LIBRARY DESTINATION ${libcaosdb_LIB_DEST}
#ARCHIVE DESTINATION ${libcaosdb_LIB_DEST}
#INCLUDES DESTINATION ${libcaosdb_INCLUDE_DEST}
#)
# We now need to install the export libcaosdbTargets that we defined above.
......@@ -138,19 +151,71 @@ install(
# install(EXPORT ...) will install the information about an export. Here, we
# save it to a file {$libcaosdb_LIB_DEST}/libcaosdbTargets.cmake and prepend
# everything inside libcaosdbTargets with the namespace libcaosdb::.
install(
# The export we want to save (matches name defined above containing the
# install targets)
EXPORT libcaosdbTargets
# 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 caosdb::caosdb)
NAMESPACE caosdb::
# where to place the resulting file (here, we're putting it with the library)
DESTINATION ${libcaosdb_LIB_DEST}
)
#install(
## The export we want to save (matches name defined above containing the
## install targets)
#EXPORT libcaosdbTargets
## 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 caosdb::caosdb)
#NAMESPACE caosdb::
## where to place the resulting file (here, we're putting it with the library)
#DESTINATION ${libcaosdb_LIB_DEST}
#)
## install(FILES ...) simply puts files in a certain place with certain
## properties. We're just copying them to the desired place here.
#install(FILES ${libcaosdb_INC} DESTINATION ${libcaosdb_INCLUDE_DEST})
# install(FILES ...) simply puts files in a certain place with certain
# properties. We're just copying them to the desired place here.
install(FILES ${libcaosdb_INC} DESTINATION ${libcaosdb_INCLUDE_DEST})
## CODE GENERATION
include(FetchGRPC)
#
# Protobuf/Grpc source files
#
set(PROTO_FILES
${PROJECT_SOURCE_DIR}/proto/proto/caosdb/info/v1alpha1/main.proto
)
set(PROTO_PATH ${PROJECT_SOURCE_DIR}/proto/proto)
# Generated sources
set(hw_proto_srcs "${CMAKE_CURRENT_BINARY_DIR}/include/caosdb/info/v1alpha1/main.pb.cc")
set(hw_proto_hdrs "${CMAKE_CURRENT_BINARY_DIR}/include/caosdb/info/v1alpha1/main.pb.h")
set(hw_grpc_srcs "${CMAKE_CURRENT_BINARY_DIR}/include/caosdb/info/v1alpha1/main.grpc.pb.cc")
set(hw_grpc_hdrs "${CMAKE_CURRENT_BINARY_DIR}/include/caosdb/info/v1alpha1/main.grpc.pb.h")
add_custom_command(
OUTPUT "${hw_proto_srcs}" "${hw_proto_hdrs}" "${hw_grpc_srcs}" "${hw_grpc_hdrs}"
COMMAND ${_PROTOBUF_PROTOC}
ARGS --grpc_out "${CMAKE_CURRENT_BINARY_DIR}/include"
--cpp_out "${CMAKE_CURRENT_BINARY_DIR}/include"
-I "${PROTO_PATH}"
--plugin=protoc-gen-grpc="${_GRPC_CPP_PLUGIN_EXECUTABLE}"
"${PROTO_FILES}"
DEPENDS "${PROTO_FILES}")
# Include generated *.pb.h files
target_include_directories(caosdb PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/include")
# hw_grpc_proto
add_library(caosdb_info_v1alpha1
${hw_grpc_srcs}
${hw_grpc_hdrs}
${hw_proto_srcs}
${hw_proto_hdrs})
target_link_libraries(caosdb_info_v1alpha1
${_REFLECTION}
${_GRPC_GRPCPP}
${_PROTOBUF_LIBPROTOBUF})
target_include_directories(caosdb_info_v1alpha1 PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/include")
target_link_libraries(caosdb
caosdb_info_v1alpha1
${_REFLECTION}
${_GRPC_GRPCPP}
${_PROTOBUF_LIBPROTOBUF})
# Copyright 2018 gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# cmake build file for C++ route_guide example.
# Assumes protobuf and gRPC have been installed using cmake.
# See cmake_externalproject/CMakeLists.txt for all-in-one cmake build
# that automatically builds all the dependencies before building route_guide.
cmake_minimum_required(VERSION 3.15)
if(MSVC)
add_definitions(-D_WIN32_WINNT=0x600)
endif()
find_package(Threads REQUIRED)
# Another way is to use CMake's FetchContent module to clone gRPC at
# configure time. This makes gRPC's source code available to your project,
# similar to a git submodule.
message(STATUS "Using gRPC via add_subdirectory (FetchContent).")
include(FetchContent)
FetchContent_Declare(
grpc
GIT_REPOSITORY https://github.com/grpc/grpc.git
GIT_TAG v1.38.0
GIT_SHALLOW 1
)
# disable a lot of stuff
set(gRPC_BUILD_TESTS OFF)
set(gRPC_BUILD_CSHARP_EXT OFF)
set(gRPC_BUILD_GRPC_CSHARP_PLUGIN OFF)
set(gRPC_BUILD_GRPC_PYTHON_PLUGIN OFF)
set(gRPC_BUILD_GRPC_RUBY_PLUGIN OFF)
set(gRPC_BUILD_GRPC_PHP_PLUGIN OFF)
set(gRPC_BUILD_GRPC_OBJECTIVE_C_PLUGIN OFF)
set(gRPC_BUILD_GRPC_NODE_PLUGIN OFF)
set(protobuf_BUILD_TESTS OFF)
FetchContent_MakeAvailable(grpc)
# Since FetchContent uses add_subdirectory under the hood, we can use
# the grpc targets directly from this build.
set(_PROTOBUF_LIBPROTOBUF libprotobuf)
set(_REFLECTION grpc++_reflection)
set(_PROTOBUF_PROTOC $<TARGET_FILE:protoc>)
set(_GRPC_GRPCPP grpc++)
if(CMAKE_CROSSCOMPILING)
find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin)
else()
set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:grpc_cpp_plugin>)
endif()
set_target_properties(
zlibstatic
libprotobuf
libprotoc
grpc_plugin_support
grpc_cpp_plugin
upb
address_sorting
absl_log_severity
absl_raw_logging_internal
absl_bad_optional_access
absl_spinlock_wait
absl_throw_delegate
absl_base
absl_malloc_internal
absl_debugging_internal
absl_stacktrace
absl_strings_internal
absl_int128
absl_strings
absl_demangle_internal
absl_symbolize
absl_str_format_internal
absl_cord
absl_status
absl_civil_time
absl_time_zone
absl_time
absl_graphcycles_internal
absl_synchronization
gpr
absl_exponential_biased
absl_hashtablez_sampler
absl_raw_hash_set
absl_wyhash
absl_city
absl_bad_variant_access
absl_hash
absl_statusor
c-ares
re2
ssl
crypto
gen_hpack_tables
gen_legal_metadata_characters
check_epollexclusive
grpcpp_channelz
gen_percent_encoding_tables
grpc++_error_details
grpc_unsecure
grpc++_alts
grpc++_unsecure
absl_strerror
absl_periodic_sampler
absl_scoped_set_env
absl_examine_stack
absl_failure_signal_handler
absl_leak_check
absl_leak_check_disable
absl_flags_program_name
absl_flags_config
absl_flags_commandlineflag_internal
absl_flags_commandlineflag
absl_flags_private_handle_accessor
absl_flags_reflection
absl_flags_marshalling
absl_flags_internal
absl_flags
absl_flags_usage_internal
absl_flags_usage
absl_flags_parse
absl_random_seed_gen_exception
absl_random_internal_seed_material
absl_random_internal_platform
absl_random_internal_randen_slow
absl_random_internal_randen_hwaes_impl
absl_random_internal_randen_hwaes
absl_random_internal_randen
absl_random_internal_pool_urbg
absl_random_distributions
absl_random_seed_sequences
absl_random_internal_distribution_test_util
absl_bad_any_cast_impl
acountry
adig
ahost
libprotobuf-lite
testing
search_test
regexp_test
possible_match_test
mimics_pcre_test
parse_test
required_prefix_test
filtered_re2_test
compile_test
re2_arg_test
charclass_test
exhaustive1_test
re2_test
simplify_test
string_generator_test
dfa_test
exhaustive3_test
exhaustive2_test
exhaustive_test
regexp_benchmark
random_test
set_test
bssl
zlib
example64
minigzip
example
minigzip64
grpc
grpc++
protoc
grpc++_reflection
PROPERTIES
CXX_CLANG_TIDY ""
CXX_INCLUDE_WHAT_YOU_USE ""
EXCLUDE_FROM_ALL 1)
......@@ -28,16 +28,48 @@
* @date 2021-05-18
* @brief Configuration and setup of the connection.
*/
#include "caosdb/info/v1alpha1/main.grpc.pb.h"
#include <string>
#include <grpc/grpc.h>
#include <grpcpp/channel.h>
#include <grpcpp/client_context.h>
#include <grpcpp/create_channel.h>
#include <grpcpp/security/credentials.h>
namespace caosdb {
using caosdb::info::v1alpha1::GeneralInfoService;
using caosdb::info::v1alpha1::VersionInfo;
/**
* @brief Configuration of the CaosDB connection.
*/
class CaosDBConnectionConfig {
std::string host;
int port;
public:
CaosDBConnectionConfig(const std::string &host, int port);
friend auto operator << (std::ostream &out, const CaosDBConnectionConfig &config) -> std::ostream &;
[[nodiscard]]
auto getHost() -> std::string;
[[nodiscard]]
auto getPort() const -> int;
};
/**
* @brief Configure the connection.
* @param host Host name or ip address of the CaosDB server.
* @param port Port of the CaosDB server.
* @return 0 if everything is ok.
* @brief A reusable connection to a CaosDBServer.
*/
auto configure_connection(const std::string &host, int port) -> int;
class CaosDBConnection {
CaosDBConnectionConfig config;
std::unique_ptr<GeneralInfoService::Stub> stub_;
public:
explicit CaosDBConnection(const CaosDBConnectionConfig &config);
friend auto operator << (std::ostream &out, const CaosDBConnection &connection) -> std::ostream &;
auto getGeneralInfoService() -> GeneralInfoService::Stub &;
[[nodiscard]]
auto getVersionInfo() const -> const VersionInfo &;
};
} // namespace caosdb
#endif
......@@ -25,6 +25,7 @@
#include "caosdb_config.h"
#include <memory>
#include <iostream>
#include <string>
auto main() -> int {
......@@ -33,6 +34,14 @@ auto main() -> int {
<< caosdb::LIBCAOSDB_VERSION_PATCH << ")" << std::endl;
std::cout << "We don't miss the H of caos." << std::endl;
std::string host = "localhost";
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
return caosdb::configure_connection("localhost", 8080);
int port = 8080;
caosdb::CaosDBConnectionConfig config(host, port);
caosdb::CaosDBConnection connection(config);
std::cout << std::endl << connection << std::endl;
const caosdb::info::v1alpha1::VersionInfo &v_info = connection.getVersionInfo();
std::cout << "VersionInfo(" << v_info.major() << ")" << std::endl;
return 0;
}
......@@ -21,15 +21,61 @@
*/
#include "connection.h"
#include "caosdb/info/v1alpha1/main.grpc.pb.h"
#include <iostream>
#include <string>
#include <stdexcept>
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";
using caosdb::info::v1alpha1::GeneralInfoService;
using caosdb::info::v1alpha1::VersionInfo;
using caosdb::info::v1alpha1::GetVersionInfoRequest;
using caosdb::info::v1alpha1::GetVersionInfoResponse;
CaosDBConnectionConfig::CaosDBConnectionConfig(const std::string &host, int port) {
this->host = host;
this->port = port;
}
auto CaosDBConnectionConfig::getHost() -> std::string {
return this->host;
}
auto CaosDBConnectionConfig::getPort() const -> int {
return this->port;
}
auto operator << (std::ostream &out, const CaosDBConnectionConfig &config) -> std::ostream & {
out << "CaosDBConnectionConfig(\"" << config.host << "\"," << config.port << ")";
return out;
}
CaosDBConnection::CaosDBConnection(const CaosDBConnectionConfig &config) : config(config) {
this->config = config;
std::shared_ptr<grpc::Channel> channel = grpc::CreateChannel(this->config.getHost() + ":" + std::to_string(this->config.getPort()), grpc::InsecureChannelCredentials());
this->stub_ = GeneralInfoService::NewStub(channel);
}
auto operator << (std::ostream &out, const CaosDBConnection &connection) -> std::ostream & {
out << "CaosDBConnection(" << connection.config << ")";
return out;
}
auto CaosDBConnection::getGeneralInfoService() -> GeneralInfoService::Stub & {
return *stub_;
}
[[nodiscard]]
auto CaosDBConnection::getVersionInfo() const -> const VersionInfo & {
const GetVersionInfoRequest request;
GetVersionInfoResponse response;
grpc::ClientContext context;
const grpc::Status status = stub_->GetVersionInfo(&context, request, &response);
if(!status.ok()) {
throw std::runtime_error(status.error_message());
}
std::cout << "Configure connection: " << host << ":" << port << "\n";
return 0;
return response.version_info();
}
} // namespace caosdb
......@@ -29,10 +29,10 @@ 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 "")
set_target_properties(gmock_main PROPERTIES CXX_CLANG_TIDY "" CXX_INCLUDE_WHAT_YOU_USE "")
set_target_properties(gmock_main gmock gtest gtest_main PROPERTIES
CXX_CLANG_TIDY ""
CXX_INCLUDE_WHAT_YOU_USE ""
EXCLUDE_FROM_ALL 1)
# loop over all test cases and add them to the test runner
list(LENGTH test_cases len_test_cases)
......@@ -42,7 +42,9 @@ foreach (i RANGE "${len_test_cases}")
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})
gtest_discover_tests(${test_case_name}
PROPERTIES
LABELS "caosdb-cpplib-unit-tests")
endforeach ()
......@@ -53,8 +55,9 @@ include(CodeCoverage)
if (LCOV_PATH)
setup_target_for_coverage_lcov(
NAME unit_test_coverage
EXECUTABLE ctest
DEPEDENCIES caosdb
EXECUTABLE ctest -L caosdb-cpplib-unit-tests
EXCLUDE "${CMAKE_BINARY_DIR}/*" "g++*"
DEPEDENCIES caosdb ${test_cases} gtest_main
LCOV_ARGS --rc lcov_branch_coverage=1
GENHTML_ARGS --rc lcov_branch_coverage=1
)
......
......@@ -26,6 +26,9 @@
#include <memory>
#include "gtest/gtest_pred_impl.h"
TEST(configure_connection, localhost_8080) {
EXPECT_EQ(0, caosdb::configure_connection("localhost", 8080));
TEST(test_connection, localhost_8080) {
caosdb::CaosDBConnectionConfig config("localhost", 8000);
EXPECT_EQ("localhost", config.getHost());
EXPECT_EQ(8000, config.getPort());
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment