Skip to content

Commit 9f58f46

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

File tree

5 files changed

+69
-4
lines changed

5 files changed

+69
-4
lines changed

rosidl_generator_cpp/bin/rosidl_generator_cpp

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_cpp(
35-
args.generator_arguments_file,
36-
)
34+
generate_cpp(args.generator_arguments_file)
3735

3836

3937
if __name__ == '__main__':

rosidl_generator_cpp/package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<!-- This is needed for the rosidl_message_type_support_t struct and visibility macros -->
1818
<build_export_depend>rosidl_generator_c</build_export_depend>
1919

20+
<exec_depend>rosidl_cli</exec_depend>
2021
<exec_depend>rosidl_parser</exec_depend>
2122

2223
<test_depend>ament_cmake_gtest</test_depend>

rosidl_generator_cpp/rosidl_generator_cpp/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def generate_cpp(generator_arguments_file):
3535
'idl__struct.hpp.em': 'detail/%s__struct.hpp',
3636
'idl__traits.hpp.em': 'detail/%s__traits.hpp',
3737
}
38-
generate_files(
38+
return generate_files(
3939
generator_arguments_file, mapping,
4040
post_process_callback=prefix_with_bom_if_necessary)
4141

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 legacy_generator_arguments_file
20+
from rosidl_cli.command.translate.api import translate
21+
22+
from rosidl_generator_cpp import generate_cpp
23+
24+
25+
class GenerateCpp(GenerateCommandExtension):
26+
27+
def generate(
28+
self,
29+
package_name,
30+
interface_files,
31+
include_paths,
32+
output_path
33+
):
34+
package_share_path = \
35+
pathlib.Path(get_package_share_directory('rosidl_generator_cpp'))
36+
templates_path = package_share_path / 'resource'
37+
38+
# Normalize interface definition format to .idl
39+
idl_interface_files = []
40+
non_idl_interface_files = []
41+
for path in interface_files:
42+
if not path.endswith('.idl'):
43+
non_idl_interface_files.append(path)
44+
else:
45+
idl_interface_files.append(path)
46+
if non_idl_interface_files:
47+
idl_interface_files.extend(translate(
48+
package_name=package_name,
49+
interface_files=non_idl_interface_files,
50+
include_paths=include_paths,
51+
output_format='idl',
52+
output_path=output_path / 'tmp',
53+
))
54+
55+
# Generate code
56+
with legacy_generator_arguments_file(
57+
package_name=package_name,
58+
interface_files=idl_interface_files,
59+
include_paths=include_paths,
60+
templates_path=templates_path,
61+
output_path=output_path
62+
) as path_to_arguments_file:
63+
return generate_cpp(path_to_arguments_file)

rosidl_generator_cpp/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+
cpp = rosidl_generator_cpp.cli:GenerateCpp

0 commit comments

Comments
 (0)