Skip to content

Commit 8d2e490

Browse files
author
jonas
committed
Allow to disable the build of # TOPICS if the module is not enabled inside px4boards
1 parent e2239e1 commit 8d2e490

File tree

6 files changed

+105
-24
lines changed

6 files changed

+105
-24
lines changed

Tools/msg/px_generate_uorb_topic_files.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,35 @@
7474
TOPICS_LIST_TEMPLATE_FILE = ['uORBTopics.hpp.em', 'uORBTopics.cpp.em', None, None]
7575
INCL_DEFAULT = ['std_msgs:./msg/std_msgs']
7676
PACKAGE = 'px4'
77-
TOPICS_TOKEN = '# TOPICS '
77+
TOPICS_TOKEN = '# TOPICS'
7878

7979

80-
def get_topics(filename):
80+
def get_topics(filename, defines=None):
8181
"""
8282
Get TOPICS names from a "# TOPICS" line
8383
"""
8484
ofile = open(filename, 'r')
8585
text = ofile.read()
8686
result = []
87+
defined_configs = set(defines or [])
8788
for each_line in text.split('\n'):
8889
if each_line.startswith(TOPICS_TOKEN):
89-
topic_names_str = each_line.strip()
90-
topic_names_str = topic_names_str.replace(TOPICS_TOKEN, "")
91-
topic_names_list = topic_names_str.split(" ")
90+
topic_names_str = each_line[len(TOPICS_TOKEN):].strip()
91+
92+
config_guard = None
93+
if topic_names_str.startswith('['):
94+
closing_idx = topic_names_str.find(']')
95+
if closing_idx != -1:
96+
config_guard = topic_names_str[1:closing_idx]
97+
topic_names_str = topic_names_str[closing_idx + 1:].strip()
98+
99+
if config_guard and config_guard not in defined_configs:
100+
continue
101+
102+
if not topic_names_str:
103+
continue
104+
105+
topic_names_list = topic_names_str.split()
92106
for topic in topic_names_list:
93107
# topic name PascalCase (file name) to snake_case (topic name)
94108
topic_name = re.sub(r'(?<!^)(?=[A-Z])', '_', topic).lower()
@@ -104,7 +118,7 @@ def get_topics(filename):
104118
return result
105119

106120

107-
def generate_output_from_file(format_idx, filename, outputdir, package, templatedir, includepath, all_topics):
121+
def generate_output_from_file(format_idx, filename, outputdir, package, templatedir, includepath, all_topics, defines=None):
108122
"""
109123
Converts a single .msg file to an uorb header/source file
110124
"""
@@ -137,7 +151,7 @@ def generate_output_from_file(format_idx, filename, outputdir, package, template
137151
" msg definition is not of type uint64 but rather of type " + field_name_and_type.get('timestamp') + "!")
138152
exit(1)
139153

140-
topics = get_topics(filename)
154+
topics = get_topics(filename, defines)
141155

142156
if includepath:
143157
search_path = genmsg.command_line.includepath_to_dict(includepath)
@@ -235,6 +249,8 @@ def append_to_include_path(path_to_append, curr_include, package):
235249
parser.add_argument('-p', dest='prefix', default='',
236250
help='string added as prefix to the output file '
237251
' name when converting directories')
252+
parser.add_argument('--define', dest='define', action='append', default=[],
253+
help='Enable optional topic groups guarded by the provided Kconfig symbols')
238254
args = parser.parse_args()
239255

240256
if args.include_paths:
@@ -252,13 +268,15 @@ def append_to_include_path(path_to_append, curr_include, package):
252268
print('Error: either --headers, --sources or --json must be specified')
253269
exit(-1)
254270
if args.file is not None:
271+
defines = set(args.define or [])
272+
255273
all_topics = []
256274
for msg_filename in args.file:
257-
all_topics.extend(get_topics(msg_filename))
275+
all_topics.extend(get_topics(msg_filename, defines))
258276
all_topics.sort()
259277

260278
for f in args.file:
261-
generate_output_from_file(generate_idx, f, args.outputdir, args.package, args.templatedir, INCL_DEFAULT, all_topics)
279+
generate_output_from_file(generate_idx, f, args.outputdir, args.package, args.templatedir, INCL_DEFAULT, all_topics, defines)
262280

263281
# Generate topics list header and source file
264282
if TOPICS_LIST_TEMPLATE_FILE[generate_idx] is not None and os.path.isfile(os.path.join(args.templatedir, TOPICS_LIST_TEMPLATE_FILE[generate_idx])):

Tools/zenoh/px_generate_zenoh_topic_files.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,35 @@
7171
__email__ = "thomasgubler@gmail.com"
7272

7373
ZENOH_TEMPLATE_FILE = ['Kconfig.topics.em', 'uorb_pubsub_factory.hpp.em']
74-
TOPICS_TOKEN = '# TOPICS '
74+
TOPICS_TOKEN = '# TOPICS'
7575

7676

77-
def get_topics(filename):
77+
def get_topics(filename, defines=None):
7878
"""
7979
Get TOPICS names from a "# TOPICS" line
8080
"""
8181
ofile = open(filename, 'r')
8282
text = ofile.read()
8383
result = []
84+
defined_configs = set(defines or [])
8485
for each_line in text.split('\n'):
8586
if each_line.startswith(TOPICS_TOKEN):
86-
topic_names_str = each_line.strip()
87-
topic_names_str = topic_names_str.replace(TOPICS_TOKEN, "")
88-
topic_names_list = topic_names_str.split(" ")
87+
topic_names_str = each_line[len(TOPICS_TOKEN):].strip()
88+
89+
config_guard = None
90+
if topic_names_str.startswith('['):
91+
closing_idx = topic_names_str.find(']')
92+
if closing_idx != -1:
93+
config_guard = topic_names_str[1:closing_idx]
94+
topic_names_str = topic_names_str[closing_idx + 1:].strip()
95+
96+
if config_guard and config_guard not in defined_configs:
97+
continue
98+
99+
if not topic_names_str:
100+
continue
101+
102+
topic_names_list = topic_names_str.split()
89103
for topic in topic_names_list:
90104
# topic name PascalCase (file name) to snake_case (topic name)
91105
topic_name = re.sub(r'(?<!^)(?=[A-Z])', '_', topic).lower()
@@ -125,7 +139,7 @@ def generate_by_template(output_file, template_file, em_globals):
125139
return True
126140

127141

128-
def generate_topics_list_file_from_files(files, outputdir, template_filename, templatedir, rihs_path):
142+
def generate_topics_list_file_from_files(files, outputdir, template_filename, templatedir, rihs_path, defines=None):
129143
# generate cpp file with topics list
130144
filenames = []
131145
for filename in [os.path.basename(p) for p in files if os.path.basename(p).endswith(".msg")]:
@@ -154,9 +168,11 @@ def generate_topics_list_file_from_files(files, outputdir, template_filename, te
154168

155169
topics = []
156170
datatypes_with_topics = dict()
171+
defined_configs = set(defines or [])
172+
157173
for msg_filename in files:
158174
datatype = re.sub(r'(?<!^)(?=[A-Z])', '_', os.path.basename(msg_filename)).lower().replace(".msg","")
159-
datatypes_with_topics[datatype] = get_topics(msg_filename)
175+
datatypes_with_topics[datatype] = get_topics(msg_filename, defined_configs)
160176
topics.extend(datatypes_with_topics[datatype])
161177

162178
tl_globals = {"msgs": filenames, "topics": topics, "datatypes": datatypes, "full_base_names": full_base_names, "rihs01_hashes": rihs01_hashes, "datatypes_with_topics": datatypes_with_topics}
@@ -181,13 +197,17 @@ def generate_topics_list_file_from_files(files, outputdir, template_filename, te
181197
' name when converting directories')
182198
parser.add_argument('--rihs', dest='rihs', default='',
183199
help='path where rihs01 json files located')
200+
parser.add_argument('--define', dest='define', action='append', default=[],
201+
help='Enable optional topic groups guarded by the provided Kconfig symbols')
184202
args = parser.parse_args()
185203

204+
defines = set(args.define or [])
205+
186206
if args.zenoh_config:
187-
generate_topics_list_file_from_files(args.file, args.outputdir, ZENOH_TEMPLATE_FILE[0], args.templatedir, args.rihs)
207+
generate_topics_list_file_from_files(args.file, args.outputdir, ZENOH_TEMPLATE_FILE[0], args.templatedir, args.rihs, defines)
188208
exit(0)
189209
elif args.zenoh_pub_sub:
190-
generate_topics_list_file_from_files(args.file, args.outputdir, ZENOH_TEMPLATE_FILE[1], args.templatedir, args.rihs)
210+
generate_topics_list_file_from_files(args.file, args.outputdir, ZENOH_TEMPLATE_FILE[1], args.templatedir, args.rihs, defines)
191211
exit(0)
192212
else:
193213
print('Error: either --headers or --sources must be specified')

docs/en/middleware/uorb.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,23 @@ See the existing [`msg`](../msg_docs/index.md) files for other examples of how m
7777
### Multi-Topic Messages
7878

7979
Sometimes it is useful to use the same message definition for multiple topics.
80-
This can be specified at the end of the message using a line prefixed with `# TOPICS `, followed by space-separated topic ids.
80+
This can be specified at the end of the message using a line prefixed with `# TOPICS`, followed by space-separated topic ids.
8181
For example, the [ActuatorOutputs](../msg_docs/ActuatorOutputs.md) message definition is used to define the topic ids as shown:
8282

8383
```text
8484
# TOPICS actuator_outputs actuator_outputs_sim actuator_outputs_debug
8585
```
8686

87+
If a topic should only be generated when a particular configuration option is enabled (for example a module that is only present on some boards), add the Kconfig symbol in square brackets immediately after `# TOPICS`.
88+
The build system scans all message definitions, determines which guarded topics are required for the currently selected `.px4board`, and automatically forwards the relevant `--define` flags to the uORB/Zenoh code generators.
89+
90+
```text
91+
# TOPICS estimator_aid_src_ev_yaw
92+
# TOPICS[CONFIG_MODULES_VISION_TARGET_ESTIMATOR] vte_aid_ev_yaw
93+
```
94+
95+
In the example above `estimator_aid_src_ev_yaw` is always generated, while `vte_aid_ev_yaw` only exists when `CONFIG_MODULES_VISION_TARGET_ESTIMATOR` is set to `y` in the board configuration.
96+
8797
### Nested Messages
8898

8999
Message definitions can be nested within other messages to create complex data structures.

msg/CMakeLists.txt

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,32 @@ if(NOT EXTERNAL_MODULES_LOCATION STREQUAL "")
299299
endif()
300300
endif()
301301

302+
set(uorb_generator_defines)
303+
foreach(msg_path ${msg_files})
304+
if(EXISTS ${msg_path})
305+
file(STRINGS ${msg_path} _guarded_topic_lines REGEX "^# TOPICS.*\\[")
306+
foreach(_guarded_line ${_guarded_topic_lines})
307+
string(REGEX MATCHALL "\\[([^]]+)\\]" _config_matches ${_guarded_line})
308+
foreach(_config_match ${_config_matches})
309+
string(REGEX REPLACE "\\[|\\]" "" _config ${_config_match})
310+
if(DEFINED ${_config})
311+
if(${_config})
312+
list(APPEND uorb_generator_defines ${_config})
313+
endif()
314+
endif()
315+
endforeach()
316+
endforeach()
317+
endif()
318+
endforeach()
319+
if(uorb_generator_defines)
320+
list(REMOVE_DUPLICATES uorb_generator_defines)
321+
endif()
322+
323+
set(uorb_generator_define_args)
324+
foreach(define ${uorb_generator_defines})
325+
list(APPEND uorb_generator_define_args --define ${define})
326+
endforeach()
327+
302328
# headers
303329
set(msg_out_path ${PX4_BINARY_DIR}/uORB/topics)
304330
set(ucdr_out_path ${PX4_BINARY_DIR}/uORB/ucdr)
@@ -331,6 +357,7 @@ add_custom_command(
331357
${uorb_headers}
332358
${msg_out_path}/uORBTopics.hpp
333359
COMMAND ${PYTHON_EXECUTABLE} ${PX4_SOURCE_DIR}/Tools/msg/px_generate_uorb_topic_files.py
360+
${uorb_generator_define_args}
334361
--headers
335362
-f ${msg_files}
336363
-i ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/versioned
@@ -352,6 +379,7 @@ add_custom_command(
352379
OUTPUT
353380
${uorb_json_files}
354381
COMMAND ${PYTHON_EXECUTABLE} ${PX4_SOURCE_DIR}/Tools/msg/px_generate_uorb_topic_files.py
382+
${uorb_generator_define_args}
355383
--json
356384
-f ${msg_files}
357385
-i ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/versioned
@@ -390,6 +418,7 @@ add_custom_command(
390418
add_custom_command(
391419
OUTPUT ${uorb_ucdr_headers}
392420
COMMAND ${PYTHON_EXECUTABLE} ${PX4_SOURCE_DIR}/Tools/msg/px_generate_uorb_topic_files.py
421+
${uorb_generator_define_args}
393422
--headers
394423
-f ${msg_files}
395424
-i ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/versioned
@@ -412,6 +441,7 @@ add_custom_command(
412441
${uorb_sources}
413442
${msg_source_out_path}/uORBTopics.cpp
414443
COMMAND ${PYTHON_EXECUTABLE} ${PX4_SOURCE_DIR}/Tools/msg/px_generate_uorb_topic_files.py
444+
${uorb_generator_define_args}
415445
--sources
416446
-f ${msg_files}
417447
-i ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/versioned
@@ -529,8 +559,9 @@ if(CONFIG_LIB_CDRSTREAM)
529559

530560
add_custom_target(
531561
uorb_idl_header
532-
COMMAND ${PYTHON_EXECUTABLE} ${PX4_SOURCE_DIR}/Tools/msg/px_generate_uorb_topic_files.py
533-
--uorb-idl-header
562+
COMMAND ${PYTHON_EXECUTABLE} ${PX4_SOURCE_DIR}/Tools/msg/px_generate_uorb_topic_files.py
563+
${uorb_generator_define_args}
564+
--uorb-idl-header
534565
-f ${msg_files}
535566
-i ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/versioned
536567
-o ${idl_uorb_path}
@@ -559,6 +590,7 @@ if(CONFIG_MODULES_ZENOH)
559590
-f ${msg_files}
560591
-o ${PX4_SOURCE_DIR}/src/modules/zenoh/
561592
-e ${PX4_SOURCE_DIR}/Tools/zenoh/templates/zenoh
593+
${uorb_generator_define_args}
562594
)
563595
add_custom_command(
564596
OUTPUT
@@ -569,6 +601,7 @@ if(CONFIG_MODULES_ZENOH)
569601
-o ${PX4_BINARY_DIR}/src/modules/zenoh/
570602
-e ${PX4_SOURCE_DIR}/Tools/zenoh/templates/zenoh
571603
--rihs ${idl_rihs01_out_path}
604+
${uorb_generator_define_args}
572605
DEPENDS
573606
${msg_files}
574607
${uorb_cdr_hash}

msg/EstimatorAidSource1d.msg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ bool fused # true if the sample was successfully fused
2626
# TOPICS estimator_aid_src_fake_hgt
2727
# TOPICS estimator_aid_src_mag_heading estimator_aid_src_gnss_yaw estimator_aid_src_ev_yaw
2828
# TOPICS estimator_aid_src_terrain_range_finder
29-
# TOPICS vte_aid_ev_yaw
29+
# TOPICS[CONFIG_MODULES_VISION_TARGET_ESTIMATOR] vte_aid_ev_yaw

msg/EstimatorAidSource3d.msg

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ bool innovation_rejected # true if the observation has been rejected
2222
bool fused # true if the sample was successfully fused
2323

2424
# TOPICS estimator_aid_src_ev_vel estimator_aid_src_gnss_vel estimator_aid_src_gravity estimator_aid_src_mag
25-
# TOPICS vte_aid_gps_pos_target vte_aid_gps_pos_mission vte_aid_gps_vel_target vte_aid_gps_vel_uav
26-
# TOPICS vte_aid_fiducial_marker vte_aid_uwb vte_aid_irlock
25+
# TOPICS[CONFIG_MODULES_VISION_TARGET_ESTIMATOR] vte_aid_gps_pos_target vte_aid_gps_pos_mission vte_aid_gps_vel_target vte_aid_gps_vel_uav
26+
# TOPICS[CONFIG_MODULES_VISION_TARGET_ESTIMATOR] vte_aid_fiducial_marker vte_aid_uwb vte_aid_irlock

0 commit comments

Comments
 (0)