Skip to content

Commit ff915ae

Browse files
committed
Expose C code generation via rosidl generate CLI.
Signed-off-by: Michel Hidalgo <[email protected]>
1 parent 594c97a commit ff915ae

File tree

5 files changed

+87
-4
lines changed

5 files changed

+87
-4
lines changed

rosidl_generator_c/bin/rosidl_generator_c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ def main(argv=sys.argv[1:]):
3131
help='The location of the file containing the generator arguments')
3232
args = parser.parse_args(argv)
3333

34-
return generate_c(
35-
args.generator_arguments_file,
36-
)
34+
generate_c(args.generator_arguments_file)
3735

3836

3937
if __name__ == '__main__':

rosidl_generator_c/package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
<build_export_depend>rosidl_typesupport_interface</build_export_depend>
2020

21+
<exec_depend>rosidl_cli</exec_depend>
2122
<exec_depend>rosidl_parser</exec_depend>
2223

2324
<test_depend>ament_cmake_gtest</test_depend>

rosidl_generator_c/rosidl_generator_c/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def generate_c(generator_arguments_file):
3434
'idl__struct.h.em': 'detail/%s__struct.h',
3535
'idl__type_support.h.em': 'detail/%s__type_support.h',
3636
}
37-
generate_files(
37+
return generate_files(
3838
generator_arguments_file, mapping,
3939
post_process_callback=prefix_with_bom_if_necessary)
4040

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Copyright 2021 Open Source Robotics Foundation, Inc.
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+
15+
import pathlib
16+
17+
from ament_index_python import get_package_share_directory
18+
from rosidl_cli.command.generate.extensions import GenerateCommandExtension
19+
from rosidl_cli.command.helpers import generate_visibility_control_file
20+
from rosidl_cli.command.helpers import legacy_generator_arguments_file
21+
from rosidl_cli.command.translate.api import translate
22+
23+
from rosidl_generator_c import generate_c
24+
25+
26+
class GenerateC(GenerateCommandExtension):
27+
28+
def generate(
29+
self,
30+
package_name,
31+
interface_files,
32+
include_paths,
33+
output_path
34+
):
35+
generated_files = []
36+
37+
package_share_path = \
38+
pathlib.Path(get_package_share_directory('rosidl_generator_c'))
39+
templates_path = package_share_path / 'resource'
40+
41+
# Normalize interface definition format to .idl
42+
idl_interface_files = []
43+
non_idl_interface_files = []
44+
for path in interface_files:
45+
if not path.endswith('.idl'):
46+
non_idl_interface_files.append(path)
47+
else:
48+
idl_interface_files.append(path)
49+
if non_idl_interface_files:
50+
idl_interface_files.extend(translate(
51+
package_name=package_name,
52+
interface_files=non_idl_interface_files,
53+
include_paths=include_paths,
54+
output_format='idl',
55+
output_path=output_path / 'tmp',
56+
))
57+
58+
# Generate visibility control file
59+
visibility_control_file_template_path = \
60+
templates_path / 'rosidl_generator_c__visibility_control.h.in'
61+
visibility_control_file_path = \
62+
output_path / 'msg' / 'rosidl_generator_c__visibility_control.h'
63+
64+
generate_visibility_control_file(
65+
package_name=package_name,
66+
template_path=visibility_control_file_template_path,
67+
output_path=visibility_control_file_path
68+
)
69+
generated_files.append(visibility_control_file_path)
70+
71+
# Generate code
72+
with legacy_generator_arguments_file(
73+
package_name=package_name,
74+
interface_files=idl_interface_files,
75+
include_paths=include_paths,
76+
templates_path=templates_path,
77+
output_path=output_path
78+
) as path_to_arguments_file:
79+
generated_files.extend(generate_c(path_to_arguments_file))
80+
81+
return generated_files

rosidl_generator_c/setup.cfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[options.entry_points]
2+
rosidl_cli.command.generate.type_extensions =
3+
c = rosidl_generator_c.cli:GenerateC

0 commit comments

Comments
 (0)