Skip to content

Commit b3d5c83

Browse files
committed
Working single header creation
Clang format added line endings because the creation of single header needs extra line before EOF Reformatted the spacing of the cmake for the tests Added line before EOF in files.
1 parent f1ec46a commit b3d5c83

File tree

10 files changed

+113
-29
lines changed

10 files changed

+113
-29
lines changed

.clang-format

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,5 @@ AllowShortLambdasOnASingleLine: Empty
102102

103103
# We do not want clang-format to put all arguments on a new line
104104
AllowAllArgumentsOnNextLine: false
105+
106+
InsertNewlineAtEOF: true

CMakeLists.txt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ project(dbc
99
option(DBC_ENABLE_TESTS "Enable Unittests" ON)
1010
option(DBC_TEST_LOCALE_INDEPENDENCE "Used to deterime if the libary is locale agnostic when it comes to converting floats. You need `de_DE.UTF-8` locale installed for this testing." OFF)
1111
option(DBC_GENERATE_DOCS "Use doxygen if installed to generated documentation files" OFF)
12+
option(DBC_GENERATE_SINGLE_HEADER "This will run the generator for the single header file version. Default is OFF since we make a static build. Requires cargo installed." OFF)
1213
# ---------------------- #
1314

1415
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
@@ -94,11 +95,12 @@ target_sources(${PROJECT_NAME} INTERFACE FILE_SET HEADERS
9495
FILES ${HEADER_FILES}
9596
)
9697

97-
add_custom_target(release
98-
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
99-
COMMAND ${CMAKE_SOURCE_DIR}/scripts/create_single_header.sh
100-
DEPENDS ${PROJECT_NAME}
101-
)
98+
if(DBC_GENERATE_SINGLE_HEADER)
99+
add_custom_target(single_header ALL
100+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
101+
COMMAND ${CMAKE_SOURCE_DIR}/scripts/create_single_header.sh
102+
)
103+
endif()
102104

103105
## Installation
104106
# install lib

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ cmake -LH .. | grep -B1 "DBC_"
3636
cmake -LAH ..
3737
```
3838

39+
### Creating a Single Header File
40+
41+
If you want to generate one header file you will need to run the `./scripts/create_single_header.sh`.
42+
43+
It requires you have `cargo` installed from rust. See these instructions if you don't have that https://www.rust-lang.org/tools/install.
44+
It uses the https://github.com/Felerius/cpp-amalgamate crate to do the single header file creation.
45+
46+
The output will be generated in the `build/single_header/libdbc/` folder. You can run a cmake command to build this as well as other targets.
47+
3948
## Testing
4049

4150
I am trying to always make sure that this is very well tested code. I am using Catch2 to do this

include/libdbc/dbc.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
#ifndef __DBC_HPP__
32
#define __DBC_HPP__
43

include/libdbc/exceptions/error.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ class validity_error : public exception {
2121

2222
} // libdbc
2323

24-
#endif // __ERROR_HPP__
24+
#endif // __ERROR_HPP__

scripts/create_single_header.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ cargo install cpp-amalgamate
77
rm -rf build/single_header/
88
mkdir -p build/single_header/libdbc
99

10-
files=$(find src -name "*.cpp")
11-
files="${files} $(find include -name "*.hpp")"
10+
source_files=$(find src -name "*.cpp")
11+
include_files=$(find include -name "*.hpp")
1212

13-
cpp-amalgamate -d include -d build/_deps/fastfloat-src/include ${files} -o build/single_header/libdbc/libdbc.hpp
13+
cpp-amalgamate -d include -d build/_deps/fastfloat-src/include -o build/single_header/libdbc/libdbc.hpp ${source_files} ${include_files}

src/signal.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,4 @@ std::ostream& operator<<(std::ostream& out, const Signal& sig) {
5151
out << r;
5252
return out << "}";
5353
}
54-
}
54+
}

test/CMakeLists.txt

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ enable_testing()
33
# Download and build Catch2 test framework
44
Include(FetchContent)
55
FetchContent_Declare(
6-
Catch2
7-
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
8-
GIT_TAG v3.5.2
6+
Catch2
7+
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
8+
GIT_TAG v3.5.2
99
)
1010
FetchContent_MakeAvailable(Catch2)
1111
include(Catch)
@@ -14,21 +14,22 @@ include(Catch)
1414
set(CMAKE_CXX_STANDARD 17)
1515

1616
if (MSVC)
17-
add_compile_options(/W4 /WX)
17+
add_compile_options(/W4 /WX)
1818
else()
19-
add_compile_options(-Wall -Wextra -Wpedantic -Werror)
19+
add_compile_options(-Wall -Wextra -Wpedantic -Werror)
2020
endif()
2121

2222
# Code coverage compiler specific
2323
if (GCC)
2424
add_compile_options(--coverage)
2525
endif()
2626

27+
2728
add_executable(dbcParserTests
28-
test_dbc.cpp
29-
test_utils.cpp
30-
test_parseMessage.cpp
31-
common.cpp
29+
test_dbc.cpp
30+
test_utils.cpp
31+
test_parse_message.cpp
32+
common.cpp
3233
)
3334

3435
target_compile_definitions(dbcParserTests PRIVATE TESTDBCFILES_PATH="${CMAKE_CURRENT_SOURCE_DIR}/dbcs")
@@ -40,16 +41,31 @@ catch_discover_tests(dbcParserTests)
4041
# We want a seperate binary for this test. We setup global locals which mess with all of the testing.
4142
# Opting for a sperate test running so we don't conflict
4243
if(DBC_TEST_LOCALE_INDEPENDENCE)
43-
add_executable(dbcLocaleTests
44-
locale_testing/test_locale_main.cpp
45-
common.cpp
46-
)
44+
add_executable(dbcLocaleTests
45+
locale_testing/test_locale_main.cpp
46+
common.cpp
47+
)
4748

48-
target_compile_definitions(dbcLocaleTests PRIVATE TESTDBCFILES_PATH="${CMAKE_CURRENT_SOURCE_DIR}/dbcs")
49-
target_link_libraries(dbcLocaleTests PRIVATE dbc Catch2::Catch2WithMain)
50-
target_include_directories(dbcLocaleTests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
49+
target_compile_definitions(dbcLocaleTests PRIVATE TESTDBCFILES_PATH="${CMAKE_CURRENT_SOURCE_DIR}/dbcs")
50+
target_link_libraries(dbcLocaleTests PRIVATE dbc Catch2::Catch2WithMain)
51+
target_include_directories(dbcLocaleTests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
5152

52-
catch_discover_tests(dbcLocaleTests)
53+
catch_discover_tests(dbcLocaleTests)
5354
else()
54-
message(WARNING "Locale independent testing is turned off!")
55+
message(WARNING "Locale independent testing is turned off!")
56+
endif()
57+
58+
# Again another test binary to ensure we aren't including our other headers.
59+
# It should compile and run on one include
60+
if(DBC_GENERATE_SINGLE_HEADER)
61+
add_executable(dbcSingleHeaderTest
62+
single_header_testing/test_single_header.cpp
63+
common.cpp
64+
)
65+
66+
target_compile_definitions(dbcSingleHeaderTest PRIVATE TESTDBCFILES_PATH="${CMAKE_CURRENT_SOURCE_DIR}/dbcs")
67+
target_link_libraries(dbcSingleHeaderTest PRIVATE Catch2::Catch2WithMain)
68+
target_include_directories(dbcSingleHeaderTest PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_BINARY_DIR}/single_header/)
69+
70+
catch_discover_tests(dbcSingleHeaderTest)
5571
endif()
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include "common.hpp"
2+
#include "defines.hpp"
3+
#include <libdbc/libdbc.hpp>
4+
5+
#include <catch2/catch_approx.hpp>
6+
#include <catch2/catch_test_macros.hpp>
7+
#include <catch2/matchers/catch_matchers.hpp>
8+
9+
TEST_CASE("Testing dbc file loading", "[fileio]") {
10+
auto parser = std::unique_ptr<libdbc::DbcParser>(new libdbc::DbcParser());
11+
12+
SECTION("Loading a single simple dbc file", "[dbc]") {
13+
std::vector<std::string> nodes = {"DBG", "DRIVER", "IO", "MOTOR", "SENSOR"};
14+
15+
libdbc::Message msg(500, "IO_DEBUG", 4, "IO");
16+
17+
std::vector<std::string> receivers{"DBG"};
18+
libdbc::Signal sig("IO_DEBUG_test_unsigned", false, 0, 8, false, false, 1, 0, 0, 0, "", receivers);
19+
msg.appendSignal(sig);
20+
21+
std::vector<libdbc::Message> msgs = {msg};
22+
23+
parser->parse_file(SIMPLE_DBC_FILE);
24+
25+
REQUIRE(parser->get_version() == "1.0.0");
26+
27+
REQUIRE(parser->get_nodes() == nodes);
28+
29+
REQUIRE(parser->get_messages() == msgs);
30+
31+
REQUIRE(parser->get_messages().front().getSignals() == msg.getSignals());
32+
}
33+
}
34+
35+
TEST_CASE("Testing big endian, little endian") {
36+
std::string dbc_contents = PRIMITIVE_DBC + R"(BO_ 234 MSG1: 8 Vector__XXX
37+
SG_ Sig1 : 55|16@0- (0.1,0) [-3276.8|-3276.7] "C" Vector__XXX
38+
SG_ Sig2 : 39|16@1- (0.1,0) [-3276.8|-3276.7] "C" Vector__XXX)";
39+
const auto filename = create_temporary_dbc_with(dbc_contents.c_str());
40+
41+
auto parser = libdbc::DbcParser();
42+
parser.parse_file(filename.c_str());
43+
44+
REQUIRE(parser.get_messages().size() == 1);
45+
REQUIRE(parser.get_messages().at(0).name() == "MSG1");
46+
REQUIRE(parser.get_messages().at(0).size() == 8);
47+
REQUIRE(parser.get_messages().at(0).getSignals().size() == 2);
48+
{
49+
const auto signal = parser.get_messages().at(0).getSignals().at(0);
50+
REQUIRE(signal.is_bigendian == true);
51+
}
52+
{
53+
const auto signal = parser.get_messages().at(0).getSignals().at(1);
54+
REQUIRE(signal.is_bigendian == false);
55+
}
56+
}
File renamed without changes.

0 commit comments

Comments
 (0)