|
| 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 | + |
| 19 | +from rosidl_cli.command.generate.extensions import GenerateCommandExtension |
| 20 | +from rosidl_cli.command.helpers import generate_visibility_control_file |
| 21 | +from rosidl_cli.command.helpers import legacy_generator_arguments_file |
| 22 | +from rosidl_cli.command.translate.api import translate |
| 23 | + |
| 24 | +from rosidl_typesupport_introspection_c import generate_c |
| 25 | + |
| 26 | + |
| 27 | +class GenerateIntrospectionCTypesupport(GenerateCommandExtension): |
| 28 | + |
| 29 | + def generate( |
| 30 | + self, |
| 31 | + package_name, |
| 32 | + interface_files, |
| 33 | + include_paths, |
| 34 | + output_path |
| 35 | + ): |
| 36 | + generated_files = [] |
| 37 | + |
| 38 | + package_share_path = pathlib.Path( |
| 39 | + get_package_share_directory('rosidl_typesupport_introspection_c')) |
| 40 | + |
| 41 | + templates_path = package_share_path / 'resource' |
| 42 | + |
| 43 | + # Normalize interface definition format to .idl |
| 44 | + idl_interface_files = [] |
| 45 | + non_idl_interface_files = [] |
| 46 | + for path in interface_files: |
| 47 | + if not path.endswith('.idl'): |
| 48 | + non_idl_interface_files.append(path) |
| 49 | + else: |
| 50 | + idl_interface_files.append(path) |
| 51 | + if non_idl_interface_files: |
| 52 | + idl_interface_files.extend(translate( |
| 53 | + package_name=package_name, |
| 54 | + interface_files=non_idl_interface_files, |
| 55 | + include_paths=include_paths, |
| 56 | + output_format='idl', |
| 57 | + output_path=output_path / 'tmp', |
| 58 | + )) |
| 59 | + |
| 60 | + # Generate visibility control file |
| 61 | + visibility_control_file_template_path = \ |
| 62 | + 'rosidl_typesupport_introspection_c__visibility_control.h.in' |
| 63 | + visibility_control_file_template_path = \ |
| 64 | + templates_path / visibility_control_file_template_path |
| 65 | + visibility_control_file_path = \ |
| 66 | + 'rosidl_typesupport_introspection_c__visibility_control.h' |
| 67 | + visibility_control_file_path = \ |
| 68 | + output_path / 'msg' / visibility_control_file_path |
| 69 | + |
| 70 | + generate_visibility_control_file( |
| 71 | + package_name=package_name, |
| 72 | + template_path=visibility_control_file_template_path, |
| 73 | + output_path=visibility_control_file_path |
| 74 | + ) |
| 75 | + generated_files.append(visibility_control_file_path) |
| 76 | + |
| 77 | + # Generate typesupport code |
| 78 | + with legacy_generator_arguments_file( |
| 79 | + package_name=package_name, |
| 80 | + interface_files=idl_interface_files, |
| 81 | + include_paths=include_paths, |
| 82 | + templates_path=templates_path, |
| 83 | + output_path=output_path |
| 84 | + ) as path_to_arguments_file: |
| 85 | + generated_files.extend(generate_c(path_to_arguments_file)) |
| 86 | + |
| 87 | + return generated_files |
0 commit comments