Skip to content

Commit

Permalink
Merge pull request #17 from dspace-group/ModernCMake
Browse files Browse the repository at this point in the history
Modern c make
  • Loading branch information
ACanisLupus authored Sep 25, 2024
2 parents 3740c69 + 6edd1a5 commit e320de1
Show file tree
Hide file tree
Showing 66 changed files with 362 additions and 232 deletions.
37 changes: 11 additions & 26 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright dSPACE GmbH. All rights reserved.

cmake_minimum_required(VERSION 3.13)
cmake_minimum_required(VERSION 3.15)

project(DsVeosCoSim VERSION 1.0)

Expand All @@ -11,43 +11,28 @@ option(DSVEOSCOSIM_BUILD_BENCHMARKS "Create benchmarks for dSPACE VEOS CoSim" OF
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)

if(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /DWIN32 /D_WINDOWS /Zm1000 /EHsc /GR /W4 /wd4324 /wd4459")
string(REGEX REPLACE "/W3" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
add_compile_options(/Zi /external:anglebrackets /external:W0)
else()
add_compile_options(-Wall -Wextra -pedantic)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
add_compile_options(-Wno-stringop-overflow)
endif()
endif()

set(DSVEOSCOSIM_TYPE STATIC)
if(BUILD_SHARED_LIBS)
set(DSVEOSCOSIM_TYPE SHARED)
endif()

add_subdirectory(shared)
add_subdirectory(src)

if(DSVEOSCOSIM_BUILD_TESTS OR DSVEOSCOSIM_BUILD_BENCHMARKS)
add_subdirectory(shared)
add_subdirectory(third_party)
add_subdirectory(third_party/fmt)
add_subdirectory(utilities/PerformanceTestClient)
add_subdirectory(utilities/PerformanceTestServer)
add_subdirectory(utilities/TestClient)
add_subdirectory(utilities/TestServer)
endif()

if(DSVEOSCOSIM_BUILD_TESTS)
set(INSTALL_GTEST OFF)
set(gtest_force_shared_crt ON)
add_subdirectory(third_party/googletest)
add_subdirectory(test)
add_subdirectory(utilities/TestClient)
add_subdirectory(utilities/TestServer)
endif()

if(DSVEOSCOSIM_BUILD_BENCHMARKS)
set(BENCHMARK_ENABLE_INSTALL OFF)
set(BENCHMARK_INSTALL_DOCS OFF)
set(BENCHMARK_ENABLE_GTEST_TESTS OFF)
set(BENCHMARK_USE_BUNDLED_GTEST OFF)
add_subdirectory(third_party/benchmark)
add_subdirectory(benchmark)
add_subdirectory(utilities/PerformanceTestClient)
add_subdirectory(utilities/PerformanceTestServer)
endif()

install(EXPORT DsVeosCoSimTargets
Expand Down
11 changes: 8 additions & 3 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# Copyright dSPACE GmbH. All rights reserved.

add_executable(
${PROJECT_NAME}Benchmark
DsVeosCoSimBenchmark
)

target_sources(
DsVeosCoSimBenchmark
PRIVATE
Communication/BenchmarkChannel.cpp
OsAbstraction/BenchmarkNamedEvent.cpp
OsAbstraction/BenchmarkSocket.cpp
Expand All @@ -11,8 +16,8 @@ add_executable(
)

target_link_libraries(
${PROJECT_NAME}Benchmark
${PROJECT_NAME}
DsVeosCoSimBenchmark
DsVeosCoSim
shared
benchmark::benchmark
)
1 change: 1 addition & 0 deletions clean.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ set currentDir=%~dp0.

echo Cleaning ...

rmdir /s /q "%currentDir%\obj" >nul 2>&1
rmdir /s /q "%currentDir%\tmpwin" >nul 2>&1

echo Cleaning finished successfully.
Expand Down
1 change: 1 addition & 0 deletions clean.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ currentDir=$(dirname "$scriptFile")/.

echo Cleaning ...

rm -rf "$currentDir/obj"
rm -rf "$currentDir/tmplin"

echo Cleaning finished successfully.
9 changes: 8 additions & 1 deletion shared/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
add_library(
shared
STATIC
)

target_sources(
shared
PRIVATE
BackgroundService.cpp
ClientServerTestHelper.cpp
Generator.cpp
Expand All @@ -21,15 +26,17 @@ target_include_directories(
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../src/OsAbstraction>
)

if(WIN32)
target_compile_definitions(
shared
PUBLIC
_CRT_SECURE_NO_WARNINGS
WIN32_LEAN_AND_MEAN
)
endif()

target_link_libraries(
shared
${PROJECT_NAME}
DsVeosCoSim
fmt::fmt
)
4 changes: 3 additions & 1 deletion shared/ClientServerTestHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "ClientServerTestHelper.h"

#include <fmt/format.h>

#include "BusBuffer.h"
#include "CoSimHelper.h"
#include "Generator.h"
Expand All @@ -16,7 +18,7 @@ bool g_sendEthMessages;
bool g_sendLinMessages;

void PrintStatus(bool value, std::string_view what) {
LogInfo("{} sending {}.", value ? "Enabled" : "Disabled", what);
LogInfo(fmt::format("{} sending {}.", value ? "Enabled" : "Disabled", what));
}

} // namespace
Expand Down
2 changes: 2 additions & 0 deletions shared/Generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "Generator.h"

#include <fmt/format.h>

using namespace DsVeosCoSim;

int32_t Random(int32_t min, int32_t max) {
Expand Down
2 changes: 1 addition & 1 deletion shared/LogHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void InitializeOutput() {
DWORD dwMode = 0;
if (::GetConsoleMode(console, &dwMode) != 0) {
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
::SetConsoleMode(console, dwMode);
(void)::SetConsoleMode(console, dwMode);
}
#endif

Expand Down
22 changes: 22 additions & 0 deletions shared/LogHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,35 @@

#pragma once

#include <fmt/format.h>
#include <string>
#include <string_view>

#include "CoSimHelper.h"
#include "DsVeosCoSim/DsVeosCoSim.h"

void InitializeOutput();

template <typename... T>
void LogError(fmt::format_string<T...> format, T&&... args) {
DsVeosCoSim::LogError(fmt::vformat(format, fmt::make_format_args(args...)));
}

template <typename... T>
void LogWarning(fmt::format_string<T...> format, T&&... args) {
DsVeosCoSim::LogWarning(fmt::vformat(format, fmt::make_format_args(args...)));
}

template <typename... T>
void LogInfo(fmt::format_string<T...> format, T&&... args) {
DsVeosCoSim::LogInfo(fmt::vformat(format, fmt::make_format_args(args...)));
}

template <typename... T>
void LogTrace(fmt::format_string<T...> format, T&&... args) {
DsVeosCoSim::LogTrace(fmt::vformat(format, fmt::make_format_args(args...)));
}

void OnLogCallback(DsVeosCoSim_Severity severity, std::string_view message);

void LogIoSignal(const DsVeosCoSim_IoSignal& ioSignal);
Expand Down
18 changes: 9 additions & 9 deletions src/BusBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,15 @@ BusBuffer::BusBuffer(CoSimType coSimType,
}
#endif

std::string_view suffixForTransmit = coSimType == CoSimType::Client ? "Transmit" : "Receive";
std::string_view suffixForReceive = coSimType == CoSimType::Client ? "Receive" : "Transmit";

_canTransmitBuffer->Initialize(coSimType, fmt::format("{}.Can.{}", name, suffixForTransmit), canControllers);
_ethTransmitBuffer->Initialize(coSimType, fmt::format("{}.Eth.{}", name, suffixForTransmit), ethControllers);
_linTransmitBuffer->Initialize(coSimType, fmt::format("{}.Lin.{}", name, suffixForTransmit), linControllers);
_canReceiveBuffer->Initialize(coSimType, fmt::format("{}.Can.{}", name, suffixForReceive), canControllers);
_ethReceiveBuffer->Initialize(coSimType, fmt::format("{}.Eth.{}", name, suffixForReceive), ethControllers);
_linReceiveBuffer->Initialize(coSimType, fmt::format("{}.Lin.{}", name, suffixForReceive), linControllers);
std::string suffixForTransmit = coSimType == CoSimType::Client ? "Transmit" : "Receive";
std::string suffixForReceive = coSimType == CoSimType::Client ? "Receive" : "Transmit";

_canTransmitBuffer->Initialize(coSimType, std::string(name) + ".Can." + suffixForTransmit, canControllers);
_ethTransmitBuffer->Initialize(coSimType, std::string(name) + ".Eth." + suffixForTransmit, ethControllers);
_linTransmitBuffer->Initialize(coSimType, std::string(name) + ".Lin." + suffixForTransmit, linControllers);
_canReceiveBuffer->Initialize(coSimType, std::string(name) + ".Can." + suffixForReceive, canControllers);
_ethReceiveBuffer->Initialize(coSimType, std::string(name) + ".Eth." + suffixForReceive, ethControllers);
_linReceiveBuffer->Initialize(coSimType, std::string(name) + ".Lin." + suffixForReceive, linControllers);
}

BusBuffer::BusBuffer(CoSimType coSimType,
Expand Down
14 changes: 8 additions & 6 deletions src/BusBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

#pragma once

#include <fmt/format.h>
#include <concepts>
#include <memory>
#include <mutex>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -135,7 +135,7 @@ class BusProtocolBufferBase {
for (const auto& controller : controllers) {
const auto search = _controllers.find(controller.id);
if (search != _controllers.end()) {
throw CoSimException(fmt::format("Duplicated controller id {}.", controller.id));
throw CoSimException("Duplicated controller id " + std::to_string(controller.id) + ".");
}

ControllerExtension extension{};
Expand Down Expand Up @@ -215,7 +215,7 @@ class BusProtocolBufferBase {
return search->second;
}

throw CoSimException(fmt::format("Controller id {} is unknown.", controllerId));
throw CoSimException("Controller id " + std::to_string(controllerId) + " is unknown.");
}

std::unordered_map<DsVeosCoSim_BusControllerId, ControllerExtension> _controllers;
Expand Down Expand Up @@ -263,7 +263,8 @@ class RemoteBusProtocolBuffer final : public BusProtocolBufferBase<TMessageExter

if (_messageCountPerController[extension.controllerIndex] == extension.info.queueSize) {
if (!extension.warningSent) {
LogWarning("Queue for controller '{}' is full. Messages are dropped.", extension.info.name);
LogWarning("Queue for controller '" + std::string(extension.info.name) +
"' is full. Messages are dropped.");
extension.warningSent = true;
}

Expand Down Expand Up @@ -326,7 +327,7 @@ class RemoteBusProtocolBuffer final : public BusProtocolBufferBase<TMessageExter

if (_messageCountPerController[extension.controllerIndex] == extension.info.queueSize) {
if (!extension.warningSent) {
LogWarning("Receive buffer for controller '{}' is full.", extension.info.name);
LogWarning("Receive buffer for controller '" + std::string(extension.info.name) + "' is full.");
extension.warningSent = true;
}

Expand Down Expand Up @@ -412,7 +413,8 @@ class LocalBusProtocolBuffer final : public BusProtocolBufferBase<TMessageExtern

if (messageCount.load() == extension.info.queueSize) {
if (!extension.warningSent) {
LogWarning("Queue for controller '{}' is full. Messages are dropped.", extension.info.name);
LogWarning("Queue for controller '" + std::string(extension.info.name) +
"' is full. Messages are dropped.");
extension.warningSent = true;
}

Expand Down
48 changes: 37 additions & 11 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
# Copyright dSPACE GmbH. All rights reserved.

add_library(
${PROJECT_NAME}
DsVeosCoSim
${DSVEOSCOSIM_TYPE}
)

set_target_properties(
DsVeosCoSim
PROPERTIES
SOVERSION 1
VERSION 1.1.0
)

target_sources(
DsVeosCoSim
PRIVATE
Communication/LocalChannel.cpp
Communication/SocketChannel.cpp
Helpers/CoSimHelper.cpp
OsAbstraction/Handle.cpp
OsAbstraction/NamedEvent.cpp
OsAbstraction/NamedMutex.cpp
OsAbstraction/OsUtilities.cpp
OsAbstraction/SharedMemory.cpp
OsAbstraction/Socket.cpp
OsAbstraction/OsUtilities.cpp
BusBuffer.cpp
CoSimClient.cpp
CoSimServer.cpp
Expand All @@ -22,13 +34,13 @@ add_library(
)

target_include_directories(
${PROJECT_NAME}
DsVeosCoSim
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../include>
)

target_include_directories(
${PROJECT_NAME}
DsVeosCoSim
PRIVATE
./
Communication/
Expand All @@ -37,20 +49,34 @@ target_include_directories(
)

target_compile_definitions(
${PROJECT_NAME}
DsVeosCoSim
PRIVATE
_CRT_SECURE_NO_WARNINGS
DSVEOSCOSIM_EXPORT
)

if(WIN32)
target_compile_definitions(
DsVeosCoSim
PRIVATE
_CRT_SECURE_NO_WARNINGS
WIN32_LEAN_AND_MEAN
)
endif()

if(WIN32)
target_link_libraries(
${PROJECT_NAME}
fmt::fmt
DsVeosCoSim
WS2_32
)
endif()

install(
TARGETS ${PROJECT_NAME}
TARGETS DsVeosCoSim
EXPORT DsVeosCoSimTargets
DESTINATION lib)
install(FILES ../include/DsVeosCoSim/DsVeosCoSim.h DESTINATION include)
DESTINATION lib
)

install(
FILES ../include/DsVeosCoSim/DsVeosCoSim.h
DESTINATION include
)
Loading

0 comments on commit e320de1

Please sign in to comment.