Skip to content

Commit a113736

Browse files
committed
engine: Restructure code for better compilation times
1 parent 51f3c0a commit a113736

20 files changed

+845
-574
lines changed

engine/CMakeLists.txt

+68-37
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ set(PROJECT_GIT_REF "unknown")
2424
# Library libstack ---------------------------------------------------
2525
message(STATUS "Building cloe-stacklib library.")
2626
add_library(cloe-stacklib STATIC
27+
src/config.hpp
2728
src/stack.hpp
2829
src/stack.cpp
30+
src/stack_factory.hpp
31+
src/stack_factory.cpp
2932
src/plugin.hpp
3033
src/plugin.cpp
3134

@@ -63,6 +66,7 @@ if(BUILD_TESTING)
6366
message(STATUS "Building test-stacklib executable.")
6467
add_executable(test-stacklib
6568
src/stack_test.cpp
69+
src/stack_component_test.cpp
6670
)
6771
set_target_properties(test-stacklib PROPERTIES
6872
CXX_STANDARD 17
@@ -78,69 +82,96 @@ if(BUILD_TESTING)
7882
gtest_add_tests(TARGET test-stacklib)
7983
endif()
8084

81-
# Executable ---------------------------------------------------------
82-
add_executable(cloe-engine
83-
src/main.cpp
84-
src/main_stack.cpp
85+
# Library libengine ----------------------------------------------
86+
message(STATUS "Building cloe-enginelib library.")
87+
add_library(cloe-enginelib STATIC
8588
src/coordinator.cpp
89+
src/coordinator.hpp
90+
src/registrar.hpp
91+
# These are added below and depend on CLOE_ENGINE_WITH_SERVER:
92+
# src/server.cpp
93+
# src/server_mock.cpp
8694
src/simulation.cpp
95+
src/simulation.hpp
8796
src/simulation_context.cpp
8897
src/simulation_context.hpp
8998
src/simulation_progress.hpp
9099
src/utility/command.cpp
100+
src/utility/command.hpp
101+
src/utility/defer.hpp
102+
src/utility/progress.hpp
103+
src/utility/state_machine.hpp
104+
src/utility/time_event.hpp
91105
)
92-
set_target_properties(cloe-engine PROPERTIES
106+
add_library(cloe::enginelib ALIAS cloe-enginelib)
107+
set_target_properties(cloe-enginelib PROPERTIES
93108
CXX_STANDARD 17
94109
CXX_STANDARD_REQUIRED ON
95-
OUTPUT_NAME cloe-engine
110+
OUTPUT_NAME engine
96111
)
97-
target_compile_definitions(cloe-engine
98-
PRIVATE
99-
CLOE_ENGINE_VERSION="${CLOE_ENGINE_VERSION}"
100-
CLOE_ENGINE_TIMESTAMP="${CLOE_ENGINE_TIMESTAMP}"
112+
target_compile_definitions(cloe-enginelib
113+
PUBLIC
101114
PROJECT_SOURCE_DIR=\"${CMAKE_CURRENT_SOURCE_DIR}\"
102115
)
103-
target_include_directories(cloe-engine
116+
target_include_directories(cloe-enginelib
104117
PRIVATE
105118
src
106119
)
107-
target_link_libraries(cloe-engine
108-
PRIVATE
109-
CLI11::CLI11
110-
cloe::models
120+
target_link_libraries(cloe-enginelib
121+
PUBLIC
111122
cloe::stacklib
123+
cloe::models
124+
cloe::runtime
125+
fable::fable
126+
boost::boost
127+
Threads::Threads
112128
)
113129

114130
option(CLOE_ENGINE_WITH_SERVER "Enable integrated server component?" ON)
115131
if(CLOE_ENGINE_WITH_SERVER)
116-
message(STATUS "-> Enable server component")
117132
if(CLOE_FIND_PACKAGES)
118133
find_package(cloe-oak REQUIRED QUIET)
119134
endif()
120-
target_sources(cloe-engine
121-
PRIVATE
122-
src/server.cpp
123-
)
124-
target_link_libraries(cloe-engine
125-
PRIVATE
126-
cloe::oak
127-
)
128-
target_compile_definitions(cloe-engine
129-
PRIVATE
130-
CLOE_ENGINE_WITH_SERVER=1
131-
)
135+
target_sources(cloe-enginelib PRIVATE src/server.cpp)
136+
target_link_libraries(cloe-enginelib PRIVATE cloe::oak)
137+
target_compile_definitions(cloe-enginelib PUBLIC CLOE_ENGINE_WITH_SERVER=1)
132138
else()
133-
message(STATUS "-> Disable server component")
134-
target_sources(cloe-engine
135-
PRIVATE
136-
src/server_mock.cpp
137-
)
138-
target_compile_definitions(cloe-engine
139-
PRIVATE
140-
CLOE_ENGINE_WITH_SERVER=0
141-
)
139+
target_sources(cloe-enginelib PRIVATE src/server_mock.cpp)
140+
target_compile_definitions(cloe-enginelib PUBLIC CLOE_ENGINE_WITH_SERVER=0)
142141
endif()
143142

143+
# Executable ---------------------------------------------------------
144+
message(STATUS "Building cloe-engine executable [with server=${CLOE_ENGINE_WITH_SERVER}].")
145+
add_executable(cloe-engine
146+
src/main.cpp
147+
src/main_commands.hpp
148+
src/main_check.cpp
149+
src/main_dump.cpp
150+
src/main_run.cpp
151+
src/main_usage.cpp
152+
src/main_version.cpp
153+
)
154+
set_target_properties(cloe-engine PROPERTIES
155+
CXX_STANDARD 17
156+
CXX_STANDARD_REQUIRED ON
157+
OUTPUT_NAME cloe-engine
158+
)
159+
target_compile_definitions(cloe-engine
160+
PRIVATE
161+
CLOE_ENGINE_VERSION="${CLOE_ENGINE_VERSION}"
162+
CLOE_ENGINE_TIMESTAMP="${CLOE_ENGINE_TIMESTAMP}"
163+
)
164+
target_include_directories(cloe-engine
165+
PRIVATE
166+
src
167+
)
168+
target_link_libraries(cloe-engine
169+
PRIVATE
170+
cloe::stacklib
171+
cloe::enginelib
172+
CLI11::CLI11
173+
)
174+
144175
# Installation -------------------------------------------------------
145176
install(TARGETS cloe-engine
146177
RUNTIME

engine/src/config.hpp

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2020 Robert Bosch GmbH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
*/
18+
/**
19+
* \file config.hpp
20+
*/
21+
22+
#pragma once
23+
24+
#ifndef CLOE_CONTACT_EMAIL
25+
#define CLOE_CONTACT_EMAIL "[email protected]"
26+
#endif
27+
28+
#ifndef CLOE_STACK_VERSION
29+
#define CLOE_STACK_VERSION "4.1"
30+
#endif
31+
32+
#ifndef CLOE_STACK_SUPPORTED_VERSIONS
33+
#define CLOE_STACK_SUPPORTED_VERSIONS {"4", "4.0", "4.1"}
34+
#endif
35+
36+
#ifndef CLOE_XDG_SUFFIX
37+
#define CLOE_XDG_SUFFIX "cloe"
38+
#endif
39+
40+
#ifndef CLOE_CONFIG_HOME
41+
#define CLOE_CONFIG_HOME "${XDG_CONFIG_HOME-${HOME}/.config}/" CLOE_XDG_SUFFIX
42+
#endif
43+
44+
#ifndef CLOE_DATA_HOME
45+
#define CLOE_DATA_HOME "${XDG_DATA_HOME-${HOME}/.local/share}/" CLOE_XDG_SUFFIX
46+
#endif
47+
48+
#ifndef CLOE_SIMULATION_UUID_VAR
49+
#define CLOE_SIMULATION_UUID_VAR "CLOE_SIMULATION_UUID"
50+
#endif
51+
52+
// The environment variable from which additional plugins should
53+
// be loaded. Takes the same format as PATH.
54+
#ifndef CLOE_PLUGIN_PATH
55+
#define CLOE_PLUGIN_PATH "CLOE_PLUGIN_PATH"
56+
#endif

engine/src/error_handler.hpp

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2023 Robert Bosch GmbH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
*/
18+
19+
#pragma once
20+
21+
#include <iostream>
22+
23+
#include <cloe/core/error.hpp> // for Error
24+
#include <fable/error.hpp> // for ConfError, SchemaError
25+
#include <fable/utility.hpp> // for indent_string, pretty_print
26+
27+
namespace cloe {
28+
29+
inline std::string format_error(const std::exception& exception) {
30+
std::stringstream buf;
31+
if (const auto* err = dynamic_cast<const fable::SchemaError*>(&exception); err) {
32+
fable::pretty_print(*err, buf);
33+
} else if (const auto* err = dynamic_cast<const fable::ConfError*>(&exception); err) {
34+
fable::pretty_print(*err, buf);
35+
} else if (const auto* err = dynamic_cast<const cloe::Error*>(&exception); err) {
36+
buf << err->what() << "\n";
37+
if (err->has_explanation()) {
38+
buf << " Note:\n";
39+
buf << fable::indent_string(err->explanation(), " ");
40+
}
41+
} else {
42+
buf << exception.what();
43+
}
44+
return buf.str();
45+
}
46+
47+
template <typename Func>
48+
auto conclude_error(std::ostream& out, Func f) -> decltype(f()) {
49+
try {
50+
return f();
51+
} catch (cloe::ConcludedError&) {
52+
// Has already been logged.
53+
throw;
54+
} catch (std::exception& err) {
55+
out << "Error: " << format_error(err) << std::endl;
56+
throw cloe::ConcludedError(err);
57+
}
58+
}
59+
60+
} // namespace cloe

0 commit comments

Comments
 (0)