Skip to content

Commit ad577af

Browse files
authored
Move to sygnal_dbc package installation and auto generated library (#4)
* Moving things into a clearer format and auto-generating a library for usage * Clean up package.xml * Update readme.md without the generated stuff * Remove unnecessary package reference in sygnal_dbc * Clean up published include dirs * Clean up cmakelists a bit * Remove unnecessary branch
1 parent 01d1ebc commit ad577af

32 files changed

+192
-0
lines changed

sygnal_dbc/CMakeLists.txt

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Copyright (c) 2025-present Polymath Robotics, Inc. All rights reserved
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
cmake_minimum_required(VERSION 3.5)
15+
16+
project(sygnal_dbc VERSION 0.1.0 LANGUAGES C)
17+
18+
find_package(ament_cmake REQUIRED)
19+
find_package(Python3 COMPONENTS Interpreter REQUIRED)
20+
include(GNUInstallDirs)
21+
22+
# Install the DBC database directory
23+
install(
24+
DIRECTORY database/
25+
DESTINATION share/${PROJECT_NAME}/database
26+
)
27+
28+
29+
# Generate C sources/headers from all .dbc files under database/
30+
file(GLOB_RECURSE DBC_FILES CONFIGURE_DEPENDS
31+
"${CMAKE_CURRENT_SOURCE_DIR}/database/*.dbc")
32+
33+
set(GENERATED_C_SOURCES)
34+
set(GENERATED_HEADERS)
35+
36+
foreach(DBC_FILE IN LISTS DBC_FILES)
37+
# Compute path pieces to mirror the database/ layout under build dir
38+
file(RELATIVE_PATH REL_PATH "${CMAKE_CURRENT_SOURCE_DIR}/database" "${DBC_FILE}")
39+
get_filename_component(REL_DIR "${REL_PATH}" DIRECTORY)
40+
get_filename_component(DBC_BASENAME_WE "${DBC_FILE}" NAME_WE)
41+
42+
# cantools generates lowercase file basenames; mirror that here
43+
string(TOLOWER "${DBC_BASENAME_WE}" MOD_NAME)
44+
45+
# Generate directly into the build include tree so dependents
46+
# can include headers during the same colcon invocation.
47+
set(OUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/include/${PROJECT_NAME}/${REL_DIR}")
48+
set(OUT_C "${OUT_DIR}/${MOD_NAME}.c")
49+
set(OUT_H "${OUT_DIR}/${MOD_NAME}.h")
50+
51+
add_custom_command(
52+
OUTPUT ${OUT_C} ${OUT_H}
53+
COMMAND ${CMAKE_COMMAND} -E make_directory ${OUT_DIR}
54+
COMMAND ${Python3_EXECUTABLE} -m cantools generate_c_source ${DBC_FILE} -o ${OUT_DIR} --database-name ${MOD_NAME}
55+
DEPENDS ${DBC_FILE}
56+
COMMENT "Generating C source from DBC '${REL_PATH}' with cantools"
57+
VERBATIM
58+
)
59+
60+
list(APPEND GENERATED_C_SOURCES ${OUT_C})
61+
list(APPEND GENERATED_HEADERS ${OUT_H})
62+
endforeach()
63+
64+
add_library(${PROJECT_NAME} STATIC ${GENERATED_C_SOURCES})
65+
target_include_directories(${PROJECT_NAME}
66+
PUBLIC
67+
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
68+
$<INSTALL_INTERFACE:include>
69+
)
70+
71+
# Ensure generation runs as part of normal build (sources already bind it)
72+
add_custom_target(generate_can_sources ALL
73+
DEPENDS ${GENERATED_C_SOURCES} ${GENERATED_HEADERS})
74+
75+
# Install generated headers so dependents can include them
76+
install(
77+
DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/${PROJECT_NAME}/
78+
DESTINATION include/${PROJECT_NAME}
79+
FILES_MATCHING PATTERN "*.h"
80+
)
81+
82+
install(
83+
TARGETS ${PROJECT_NAME}
84+
EXPORT export_${PROJECT_NAME}
85+
ARCHIVE DESTINATION lib
86+
LIBRARY DESTINATION lib
87+
RUNTIME DESTINATION bin
88+
)
89+
90+
# Export only modern CMake targets; include dirs and libs are already
91+
# encoded on the exported target usage requirements above.
92+
ament_export_targets(export_${PROJECT_NAME} HAS_LIBRARY_TARGET)
93+
ament_package(CONFIG_EXTRAS "cmake/${PROJECT_NAME}-extras.cmake")

sygnal_dbc/README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# sygnal_dbc
2+
3+
Provides Sygnal DBC files and a C library produced by cantools. The package
4+
installs the raw DBC database and, at build time, generates `.c/.h` files for
5+
each `.dbc` and exposes them via a static library for consumers.
6+
7+
## Contents
8+
- Installed DBCs: `share/sygnal_dbc/database/...`
9+
- Installed headers: `include/sygnal_dbc/<subdir>/<name>.h`
10+
- Library: `sygnal_dbc`
11+
- CMake var: `SYGNAL_DBC_DIR` → points to installed `database` directory
12+
13+
## Build
14+
Prereqs:
15+
- ROS 2 ament environment sourced
16+
- Python 3 with cantools: `python3 -m pip install cantools`
17+
18+
Build only this package:
19+
20+
```
21+
colcon build --packages-select sygnal_dbc
22+
```
23+
24+
## Using in C++ (ament_cmake)
25+
In your package CMakeLists.txt:
26+
27+
```
28+
find_package(ament_cmake REQUIRED)
29+
find_package(sygnal_dbc REQUIRED)
30+
31+
add_executable(my_node src/my_node.cpp)
32+
ament_target_dependencies(my_node )
33+
# Link the library (headers are exported)
34+
target_link_libraries(my_node sygnal_dbc)
35+
```
36+
37+
Include headers (lowercase file basenames, folder layout mirrors `database/`):
38+
39+
```
40+
#include <sygnal_dbc/cb/configuration.h>
41+
#include <sygnal_dbc/mcm/heartbeat.h>
42+
```
43+
44+
The exact header names come from each DBC file name, lowercased.
45+
46+
## Accessing raw DBCs
47+
After `find_package(sygnal_dbc)`, the CMake variable `SYGNAL_DBC_DIR` is set to
48+
`<install-prefix>/share/sygnal_dbc/database`. Use it to locate raw `.dbc` files:
49+
50+
```
51+
# Example: install a copy of a specific dbc next to your target
52+
install(FILES "${SYGNAL_DBC_DIR}/cb/Configuration.dbc" DESTINATION share/${PROJECT_NAME}/dbc)
53+
```
54+
55+
## Notes
56+
- The installed headers mirror the folder structure under `database/`
57+
(`cb/`, `io/`, `mcm/`, `vehicles/`).
58+
- If you add or rename `.dbc` files, just rebuild; sources regenerate
59+
automatically.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright (c) 2025-present Polymath Robotics, Inc. All rights reserved
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# sygnal_dbc extras included by ament's find_package
15+
16+
# Determine the installed share directory from the location of this config
17+
get_filename_component(_pkg_share_dir "${sygnal_dbc_DIR}/.." ABSOLUTE)
18+
19+
# Public variable for consumers to locate the DBC database
20+
set(SYGNAL_DBC_DIR "${_pkg_share_dir}/database")
21+
22+
if(NOT sygnal_dbc_FIND_QUIETLY)
23+
message(STATUS "sygnal_dbc: SYGNAL_DBC_DIR='${SYGNAL_DBC_DIR}'")
24+
endif()
File renamed without changes.

0 commit comments

Comments
 (0)