fix(msg): escape IDL keywords in uORB field names for idlc (#29696)

A uORB field named after an IDL keyword, such as `sequence` in several
MAVLink-M messages, made CycloneDDS idlc fail with a syntax error on any
build with CONFIG_LIB_CDRSTREAM, such as the Zenoh targets. Escaping the
name in the shared IDL is not enough: rosidl_parser, which computes the
RIHS01 type hash, rejects escaped identifiers.

Generate a second IDL copy for idlc with every field and constant name
escaped by a leading underscore. idlc strips the underscore, so the
generated C member names are unchanged. The hash step keeps reading the
plain IDL, so RIHS01 hashes and Zenoh key expressions are unchanged too.

Fixes #29600

Signed-off-by: Sofian Elmotiem <191787501+SofianElmotiem@users.noreply.github.com>
Co-authored-by: Sofian Elmotiem <191787501+SofianElmotiem@users.noreply.github.com>
This commit is contained in:
Sofian ElmotiemandSofian Elmotiem authored and GitHub committed 2026-11-09 13:17:42 +00:00
1 parent ca6b8c8e03
commit 92b18d17f3
2 files changed
+46 -7

No files matched your search

+13 -4
View File
@@ -471,12 +471,16 @@ add_dependencies(uorb_msgs prebuild_targets uorb_headers)
if(CONFIG_LIB_CDRSTREAM)
set(uorb_cdr_idl)
set(uorb_cdr_idlc)
set(uorb_cdr_msg)
set(uorb_cdr_hash)
set(uorb_cdr_idl_uorb)
set(idl_include_path ${PX4_BINARY_DIR}/uORB/idl)
set(idl_out_path ${idl_include_path}/px4/msg)
set(idl_rihs01_out_path ${idl_include_path}/px4)
# idlc compiles a copy with escaped identifiers, see msg2idl.py
set(idlc_include_path ${PX4_BINARY_DIR}/uORB/idlc)
set(idlc_out_path ${idlc_include_path}/px4/msg)
set(idl_uorb_path ${PX4_BINARY_DIR}/msg/px4/msg)
# Make sure that CycloneDDS has been checkout out
@@ -520,6 +524,7 @@ if(CONFIG_LIB_CDRSTREAM)
get_filename_component(msg ${msg_file} NAME_WE)
configure_file(${msg_file} ${idl_out_path}/${msg}.msg COPYONLY)
list(APPEND uorb_cdr_idl ${idl_out_path}/${msg}.idl)
list(APPEND uorb_cdr_idlc ${idlc_out_path}/${msg}.idl)
list(APPEND uorb_cdr_msg ${idl_out_path}/${msg}.msg)
list(APPEND uorb_cdr_hash ${idl_out_path}/${msg}.json)
list(APPEND uorb_cdr_idl_uorb ${idl_uorb_path}/${msg}.h)
@@ -528,13 +533,15 @@ if(CONFIG_LIB_CDRSTREAM)
# Generate IDL from .msg using rosidl_adapter
# Note this a submodule inside PX4 hence no ROS2 installation required
add_custom_command(
OUTPUT ${uorb_cdr_idl}
OUTPUT ${uorb_cdr_idl} ${uorb_cdr_idlc}
COMMAND ${CMAKE_COMMAND}
-E env "PYTHONPATH=${PX4_SOURCE_DIR}/src/lib/cdrstream/rosidl/rosidl_adapter:${PX4_SOURCE_DIR}/src/lib/cdrstream/rosidl/rosidl_cli"
${PYTHON_EXECUTABLE} ${PX4_SOURCE_DIR}/src/lib/cdrstream/msg2idl.py
--idlc-output-dir ${idlc_out_path}
${uorb_cdr_msg}
DEPENDS
${uorb_cdr_msg}
${PX4_SOURCE_DIR}/src/lib/cdrstream/msg2idl.py
git_cyclonedds
COMMENT "Generating IDL from uORB topic headers"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
@@ -542,6 +549,8 @@ if(CONFIG_LIB_CDRSTREAM)
)
file(CREATE_LINK ${idl_rihs01_out_path} ${idl_include_path}/px4_msgs SYMBOLIC)
file(MAKE_DIRECTORY ${idlc_out_path})
file(CREATE_LINK ${idlc_include_path}/px4 ${idlc_include_path}/px4_msgs SYMBOLIC)
# Generate IDL from .msg using rosidl_adapter
# Note this is a submodule inside PX4 hence no ROS2 installation required
@@ -565,9 +574,9 @@ if(CONFIG_LIB_CDRSTREAM)
include("${CYCLONEDDS_DIR}/cmake/Modules/Generate.cmake")
idlc_generate(TARGET uorb_cdrstream
FEATURES "cdrstream-desc"
FILES ${uorb_cdr_idl}
INCLUDES ${idl_include_path}
BASE_DIR ${idl_include_path}
FILES ${uorb_cdr_idlc}
INCLUDES ${idlc_include_path}
BASE_DIR ${idlc_include_path}
WARNINGS no-implicit-extensibility no-unsupported-annotations)
target_link_libraries(uorb_cdrstream INTERFACE cdr)
+33 -3
View File
@@ -39,6 +39,33 @@ import pathlib
import sys
from rosidl_adapter.msg import convert_msg_to_idl
from rosidl_adapter.parser import parse_message_string
from rosidl_adapter.resource import expand_template
def write_idlc_idl(package_dir, package_name, input_file, output_dir):
"""Write the IDL that CycloneDDS idlc compiles.
uORB field names such as 'sequence' or 'string' are IDL keywords, which
idlc rejects. A leading underscore escapes an IDL identifier and idlc strips
it again, so the generated C names stay unchanged. Escaping every name means
no keyword list has to track idlc. rosidl_parser rejects escaped names, so
the RIHS01 hash keeps using the plain IDL from convert_msg_to_idl.
"""
content = (package_dir / input_file).read_text(encoding='utf-8')
msg = parse_message_string(package_name, input_file.stem, content)
for member in msg.fields + msg.constants:
member.name = f'_{member.name}'
data = {
'pkg_name': package_name,
'relative_input_file': input_file.as_posix(),
'msg': msg,
}
expand_template('msg.idl.em', data, output_dir / input_file.with_suffix('.idl').name,
encoding='iso-8859-1')
if __name__ == '__main__':
parser = argparse.ArgumentParser(
@@ -46,6 +73,9 @@ if __name__ == '__main__':
parser.add_argument(
'interface_files', nargs='+',
help='The interface files to convert')
parser.add_argument(
'--idlc-output-dir', type=pathlib.Path, required=True,
help='Directory for the keyword-escaped IDL compiled by idlc')
args = parser.parse_args(sys.argv[1:])
# convert_msg_to_idl prints a Reading/Writing line per file. Filter those
@@ -64,10 +94,10 @@ if __name__ == '__main__':
for interface_file in args.interface_files:
interface_file = pathlib.Path(interface_file)
package_dir = interface_file.parent.absolute()
input_file = interface_file.absolute().relative_to(package_dir)
convert_msg_to_idl(
package_dir, 'px4_msgs',
interface_file.absolute().relative_to(package_dir),
interface_file.parent)
package_dir, 'px4_msgs', input_file, interface_file.parent)
write_idlc_idl(package_dir, 'px4_msgs', input_file, args.idlc_output_dir)
finally:
builtins.print = real_print