Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ cmake_minimum_required(VERSION 3.16)
project(J1939)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
add_compile_options("$<$<COMPILE_LANGUAGE:C>:-Wall;-Wextra;-Werror;-Wno-unused-parameter>")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

option(ENABLE_COVERAGE "Build with gcov instrumentation for coverage reports" OFF)
Expand Down Expand Up @@ -60,6 +65,7 @@ install(TARGETS dbc_compiler RUNTIME DESTINATION bin)
find_package(GTest)

add_definitions(-DDBC_FOLDER="${CMAKE_CURRENT_SOURCE_DIR}/tests/dbc/")
add_definitions(-DJ1939_INCLUDE_DIR="${CMAKE_CURRENT_SOURCE_DIR}/j1939_parser")
# Sample CAN logs: committed excerpts in testdata/canboat/, and the full captures that
# scripts/fetch_test_logs.sh downloads into testdata/external/ (see testdata/README.md).
add_definitions(-DTESTDATA_FOLDER="${CMAKE_CURRENT_SOURCE_DIR}/testdata/")
Expand Down Expand Up @@ -87,7 +93,7 @@ add_executable(all_tests ${ALL_TESTS})
add_test(all_tests ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/all_tests)

# Tell linker about all libs to be used there
target_link_libraries(all_tests GTest::GTest GTest::Main GTest::gmock j1939_parser can_streams can_sandbox_lib can_reader)
target_link_libraries(all_tests GTest::GTest GTest::Main GTest::gmock j1939_parser can_streams can_sandbox_lib can_reader ${CMAKE_DL_LIBS})
# Make it discoverable
gtest_discover_tests(all_tests)

Expand Down
35 changes: 34 additions & 1 deletion dbc_compiler/dbc_compiler.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <iostream>
#include <fstream>
#include <memory>
#include <sstream>

#include "dbc_parser.h"
#include "options.h"
Expand All @@ -23,18 +25,22 @@ int main(int argc, const char* argv[]) {
std::vector<std::string> inputs;
std::string pgn_path;
std::string union_path;
std::string c_path;
std::string var_name;
bool no_strings = false;

options.addOption("-in", "Full path of the input DBC file (multiple input files are supported).", &inputs);
options.addOption("-name", "Defines variable name in the output files. By default, the first input DBC name will be used.", &var_name);
options.addOption("-pgn_path", "Full path of the output file to store the DBC content in PGN format.", &pgn_path);
options.addOption("-union_path", "Full path of the output file to store the DBC content in the 'union' format.", &union_path);
options.addOption("-c_path", "Full path of the generated plain-C database (.c). A matching .h is written beside it.", &c_path);
options.addOption("-no-strings", "Omit signal and message names from the plain-C database.", &no_strings);

if (!options.processArgs(argc, argv)) {
return 1;
}

if (inputs.size() == 0 || (union_path.empty() && pgn_path.empty())) {
if (inputs.size() == 0 || (union_path.empty() && pgn_path.empty() && c_path.empty())) {
printf( "Usage: dbc_compiler <options>\n");
printf( " where <options> could be:\n");
options.print();
Expand All @@ -51,6 +57,33 @@ int main(int argc, const char* argv[]) {
return -3;
}
}
// The C database has its own collapse rule and must see every source address,
// so it is emitted before removeDuplicates() rewrites the message keys.
if (!c_path.empty()) {
std::string header_path = c_path;
auto dot = header_path.rfind('.');
if (dot == std::string::npos) {
header_path += ".h";
} else {
header_path.replace(dot, std::string::npos, ".h");
}
std::ostringstream c_text;
std::ostringstream h_text;
std::string error;
if (!parser.generateCOutput(c_text, h_text, no_strings, error)) {
std::cerr << error << std::endl;
return 4;
}
std::ofstream c_file(c_path.c_str());
std::ofstream h_file(header_path.c_str());
if (!c_file.is_open() || !h_file.is_open()) {
std::cerr << "Error opening C database output " << c_path << std::endl;
return 2;
}
c_file << c_text.str();
h_file << h_text.str();
}

// TODO: add option to disable the following step.
parser.removeDuplicates();

Expand Down
2 changes: 1 addition & 1 deletion j1939_parser/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ file(GLOB DBC_PARSER_SRC dbc_parser/*)
# library (define_lib's own glob-filter only covers j1939_parser/*.cpp, not this one).
list(FILTER DBC_PARSER_SRC EXCLUDE REGEX ".*_tests?\\.cpp$")

define_lib(j1939_parser ${DBC_PARSER_SRC})
define_lib(j1939_parser ${DBC_PARSER_SRC} j1939_db.c)
Loading
Loading