def generate_cpp(output_path, template_dir): data = generate_messages() template_file = os.path.join(template_dir, 'convert_rosbag_message.cpp.em') output_file = os.path.join(output_path, 'convert_rosbag_message.cpp') data_for_template = {'mappings': data['mappings']} expand_template(template_file, data_for_template, output_file)
def generate_cpp(generator_arguments_file): args = read_generator_arguments(generator_arguments_file) template_dir = args['template_dir'] mapping_msgs = { os.path.join(template_dir, 'msg__type_support.cpp.template'): '%s__type_support.cpp', } mapping_srvs = { os.path.join(template_dir, 'srv__type_support.cpp.template'): '%s__type_support.cpp', } for template_file in mapping_msgs.keys(): assert os.path.exists(template_file), 'Could not find template: ' + template_file for template_file in mapping_srvs.keys(): assert os.path.exists(template_file), 'Could not find template: ' + template_file pkg_name = args['package_name'] known_msg_types = extract_message_types( pkg_name, args['ros_interface_files'], args.get('ros_interface_dependencies', [])) functions = { 'get_header_filename_from_msg_name': convert_camel_case_to_lower_case_underscore, } latest_target_timestamp = get_newest_modification_time(args['target_dependencies']) for ros_interface_file in args['ros_interface_files']: extension = os.path.splitext(ros_interface_file)[1] subfolder = os.path.basename(os.path.dirname(ros_interface_file)) if extension == '.msg': spec = parse_message_file(pkg_name, ros_interface_file) validate_field_types(spec, known_msg_types) for template_file, generated_filename in mapping_msgs.items(): generated_file = os.path.join( args['output_dir'], subfolder, generated_filename % convert_camel_case_to_lower_case_underscore(spec.base_type.type)) data = {'spec': spec, 'subfolder': subfolder} data.update(functions) expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) elif extension == '.srv': spec = parse_service_file(pkg_name, ros_interface_file) validate_field_types(spec, known_msg_types) for template_file, generated_filename in mapping_srvs.items(): generated_file = os.path.join( args['output_dir'], subfolder, generated_filename % convert_camel_case_to_lower_case_underscore(spec.srv_name)) data = {'spec': spec} data.update(functions) expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) return 0
def generate_cpp(generator_arguments_file): args = read_generator_arguments(generator_arguments_file) template_dir = args['template_dir'] mapping_msgs = { os.path.join(template_dir, 'msg.hpp.em'): '%s.hpp', os.path.join(template_dir, 'msg__struct.hpp.em'): '%s__struct.hpp', os.path.join(template_dir, 'msg__traits.hpp.em'): '%s__traits.hpp', } mapping_srvs = { os.path.join(template_dir, 'srv.hpp.em'): '%s.hpp', os.path.join(template_dir, 'srv__struct.hpp.em'): '%s__struct.hpp', os.path.join(template_dir, 'srv__traits.hpp.em'): '%s__traits.hpp', } for template_file in mapping_msgs.keys(): assert os.path.exists( template_file), 'Could not find template: ' + template_file for template_file in mapping_srvs.keys(): assert os.path.exists( template_file), 'Could not find template: ' + template_file functions = { 'get_header_filename_from_msg_name': convert_camel_case_to_lower_case_underscore, } latest_target_timestamp = get_newest_modification_time( args['target_dependencies']) for ros_interface_file in args['ros_interface_files']: extension = os.path.splitext(ros_interface_file)[1] subfolder = os.path.basename(os.path.dirname(ros_interface_file)) if extension == '.msg': spec = parse_message_file(args['package_name'], ros_interface_file) for template_file, generated_filename in mapping_msgs.items(): data = {'spec': spec, 'subfolder': subfolder} data.update(functions) generated_file = os.path.join( args['output_dir'], subfolder, generated_filename % convert_camel_case_to_lower_case_underscore( spec.base_type.type)) expand_template(template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) elif extension == '.srv': spec = parse_service_file(args['package_name'], ros_interface_file) for template_file, generated_filename in mapping_srvs.items(): data = {'spec': spec} data.update(functions) generated_file = os.path.join( args['output_dir'], subfolder, generated_filename % convert_camel_case_to_lower_case_underscore(spec.srv_name)) expand_template(template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) return 0
def generate_c(generator_arguments_file): args = read_generator_arguments(generator_arguments_file) template_dir = args['template_dir'] mapping = { os.path.join(template_dir, 'msg.h.template'): '%s.h', os.path.join(template_dir, 'msg__functions.c.template'): '%s__functions.c', os.path.join(template_dir, 'msg__functions.h.template'): '%s__functions.h', os.path.join(template_dir, 'msg__struct.h.template'): '%s__struct.h', os.path.join(template_dir, 'msg__type_support.h.template'): '%s__type_support.h', } for template_file in mapping.keys(): assert os.path.exists(template_file), 'Could not find template: ' + template_file functions = { 'get_header_filename_from_msg_name': convert_camel_case_to_lower_case_underscore, } latest_target_timestamp = get_newest_modification_time(args['target_dependencies']) for ros_interface_file in args['ros_interface_files']: extension = os.path.splitext(ros_interface_file)[1] subfolder = os.path.basename(os.path.dirname(ros_interface_file)) if extension == '.msg': spec = parse_message_file(args['package_name'], ros_interface_file) for template_file, generated_filename in mapping.items(): generated_file = os.path.join( args['output_dir'], subfolder, generated_filename % convert_camel_case_to_lower_case_underscore(spec.base_type.type)) data = {'spec': spec, 'subfolder': subfolder} data.update(functions) expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) return 0
def generate_py(generator_arguments_file): args = read_generator_arguments(generator_arguments_file) template_dir = args['template_dir'] mapping_msgs = { os.path.join(template_dir, '_msg.py.template'): '_%s.py', } mapping_srvs = { os.path.join(template_dir, '_srv.py.template'): '_%s.py', } for template_file in mapping_msgs.keys(): assert os.path.exists(template_file), \ 'Messages template file %s not found' % template_file for template_file in mapping_srvs.keys(): assert os.path.exists(template_file), \ 'Services template file %s not found' % template_file functions = { 'constant_value_to_py': constant_value_to_py, 'value_to_py': value_to_py, } latest_target_timestamp = get_newest_modification_time(args['target_dependencies']) modules = defaultdict(list) for ros_interface_file in args['ros_interface_files']: extension = os.path.splitext(ros_interface_file)[1] subfolder = os.path.basename(os.path.dirname(ros_interface_file)) if extension == '.msg': spec = parse_message_file(args['package_name'], ros_interface_file) mapping = mapping_msgs type_name = spec.base_type.type elif extension == '.srv': spec = parse_service_file(args['package_name'], ros_interface_file) mapping = mapping_srvs type_name = spec.srv_name else: continue module_name = convert_camel_case_to_lower_case_underscore(type_name) modules[subfolder].append((module_name, type_name)) for template_file, generated_filename in mapping.items(): data = {'package_name': args['package_name'], 'spec': spec} data.update(functions) generated_file = os.path.join( args['output_dir'], subfolder, generated_filename % module_name) expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) open(os.path.join(args['output_dir'], '__init__.py'), 'w').close() for module in modules: with open(os.path.join(args['output_dir'], module, '__init__.py'), 'w') as f: for module_, type_ in modules[module]: f.write('from %s.msg._%s import %s\n' % (args['package_name'], module_, type_)) return 0
def generate_cpp(args, message_specs, service_specs, known_msg_types): template_dir = args['template_dir'] mapping_msgs = { os.path.join(template_dir, 'msg__type_support.hpp.em'): '%s__type_support.hpp', os.path.join(template_dir, 'msg__type_support.cpp.em'): '%s__type_support.cpp', } mapping_srvs = { os.path.join(template_dir, 'srv__type_support.cpp.em'): '%s__type_support.cpp', os.path.join(template_dir, 'srv__type_support.hpp.em'): '%s__type_support.hpp', } for template_file in mapping_msgs.keys(): assert os.path.exists(template_file), 'Could not find template: ' + template_file for template_file in mapping_srvs.keys(): assert os.path.exists(template_file), 'Could not find template: ' + template_file functions = { 'get_header_filename_from_msg_name': convert_camel_case_to_lower_case_underscore, } # generate_dds_connext_cpp() and therefore the make target depend on the additional files # therefore they must be listed here even if the generated type support files are independent latest_target_timestamp = get_newest_modification_time( args['target_dependencies'] + args.get('additional_files', [])) for idl_file, spec in message_specs: validate_field_types(spec, known_msg_types) subfolder = os.path.basename(os.path.dirname(idl_file)) for template_file, generated_filename in mapping_msgs.items(): generated_file = os.path.join( args['output_dir'], subfolder, 'dds_connext', generated_filename % convert_camel_case_to_lower_case_underscore(spec.base_type.type)) data = {'spec': spec, 'subfolder': subfolder} data.update(functions) expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) for spec in service_specs: validate_field_types(spec, known_msg_types) for template_file, generated_filename in mapping_srvs.items(): generated_file = os.path.join( args['output_dir'], 'srv', 'dds_connext', generated_filename % convert_camel_case_to_lower_case_underscore(spec.srv_name)) data = {'spec': spec} data.update(functions) expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) return 0
def generate_ros_transport(package, output_dir, messages=[]): output_dir = pathlib.Path(output_dir) data = { 'pkg_name': package, 'messages': messages, } expand_template('ros_transport_plugin.cpp.em', data=data, output_file=(output_dir / "ros_transport_plugin.cpp"), template_basepath=TEMPLATE_DIR)
def generate_cpp(output_file, template_dir): rospack = rospkg.RosPack() ros1_msgs = get_ros1_messages(rospack=rospack) ros2_msgs, mapping_rules = get_ros2_messages() package_pairs = determine_package_pairs(ros1_msgs, ros2_msgs, mapping_rules) message_pairs = determine_message_pairs(ros1_msgs, ros2_msgs, package_pairs, mapping_rules) mappings = [] for ros1_msg, ros2_msg in message_pairs: mapping = determine_field_mapping(ros1_msg, ros2_msg, mapping_rules, rospack=rospack) if mapping: mappings.append(mapping) # order mappings topologically to allow template specialization ordered_mappings = [] while mappings: # pick first mapping without unsatisfied dependencies for m in mappings: if not m.depends_on_ros2_messages: break else: break # move mapping to ordered list mappings.remove(m) ordered_mappings.append(m) ros2_msg = m.ros2_msg # update unsatisfied dependencies of remaining mappings for m in mappings: if ros2_msg in m.depends_on_ros2_messages: m.depends_on_ros2_messages.remove(ros2_msg) if mappings: print('%d mappings can not be generated due to missing dependencies:' % len(mappings), file=sys.stderr) for m in mappings: print('- %s <-> %s:' % ('%s/%s' % (m.ros1_msg.package_name, m.ros1_msg.message_name), '%s/%s' % (m.ros2_msg.package_name, m.ros2_msg.message_name)), file=sys.stderr) for d in m.depends_on_ros2_messages: print(' -', '%s/%s' % (d.package_name, d.message_name), file=sys.stderr) print(file=sys.stderr) data = { 'ros1_msgs': [m.ros1_msg for m in ordered_mappings], 'ros2_msgs': [m.ros2_msg for m in ordered_mappings], 'mappings': ordered_mappings, } template_file = os.path.join(template_dir, 'generated_factories.cpp.template') expand_template(template_file, data, output_file)
def generate_service_plugin(package, output_dir, services=[], extra_includes=[]): output_dir = pathlib.Path(output_dir) data = { 'pkg_name': package, 'services': services, 'extra_includes': extra_includes, } expand_template('ros_service_plugin.cpp.em', data=data, output_file=(output_dir / "ros_service_plugin.cpp"), template_basepath=TEMPLATE_DIR)
def generate_swift(generator_arguments_file, typesupport_impls): mapping = { 'idl.swift.em': '%s.swift', 'idl.c.em': '%s.c', 'idl.h.em': 'rclswift_%s.h', } generated_files = generate_files(generator_arguments_file, mapping) header_files = list(filter(lambda file: os.path.splitext(file)[1] == '.h', generated_files)) args = read_generator_arguments(generator_arguments_file) data = { 'package_name': args['package_name'], 'headers': header_files, } template_basepath = pathlib.Path(args['template_dir']) generated_file = os.path.join( args['output_dir'], 'module.modulemap') latest_target_timestamp = get_newest_modification_time(args['target_dependencies']) expand_template(os.path.basename('idl.modulemap.em'), data, generated_file, minimum_timestamp=latest_target_timestamp, template_basepath=template_basepath) return 0
def generate_cpp(output_path, template_dir): rospack = rospkg.RosPack() data = generate_messages(rospack) data.update(generate_services(rospack)) template_file = os.path.join(template_dir, 'get_mappings.cpp.em') output_file = os.path.join(output_path, 'get_mappings.cpp') data_for_template = { 'mappings': data['mappings'], 'services': data['services'] } expand_template(template_file, data_for_template, output_file) unique_package_names = set(data['ros2_package_names_msg'] + data['ros2_package_names_srv']) # skip builtin_interfaces since there is a custom implementation unique_package_names -= {'builtin_interfaces'} data['ros2_package_names'] = list(unique_package_names) template_file = os.path.join(template_dir, 'get_factory.cpp.em') output_file = os.path.join(output_path, 'get_factory.cpp') expand_template(template_file, data, output_file) for ros2_package_name in data['ros2_package_names']: for extension in ['cpp', 'hpp']: data_pkg = { 'ros2_package_name': ros2_package_name, 'mappings': [ m for m in data['mappings'] if m.ros2_msg.package_name == ros2_package_name ], 'services': [ s for s in data['services'] if s['ros2_package'] == ros2_package_name ] } if extension == 'hpp': data_pkg.update({ 'ros1_msgs': [ m.ros1_msg for m in data['mappings'] if m.ros2_msg.package_name == ros2_package_name ], 'ros2_msgs': [ m.ros2_msg for m in data['mappings'] if m.ros2_msg.package_name == ros2_package_name ], }) template_file = os.path.join(template_dir, 'pkg_factories.%s.em' % extension) output_file = os.path.join( output_path, '%s_factories.%s' % (ros2_package_name, extension)) expand_template(template_file, data_pkg, output_file)
def generate_cpp(output_path, template_dir): data = generate_messages() template_file = os.path.join(template_dir, 'get_mappings.cpp.em') output_file = os.path.join(output_path, 'get_mappings.cpp') data_for_template = {'mappings': data['mappings']} expand_template(template_file, data_for_template, output_file) data.update(generate_services()) unique_package_names = set(data['ros2_package_names_msg'] + data['ros2_package_names_srv']) data['ros2_package_names'] = list(unique_package_names) template_file = os.path.join(template_dir, 'get_factory.cpp.em') output_file = os.path.join(output_path, 'get_factory.cpp') expand_template(template_file, data, output_file) for ros2_package_name in data['ros2_package_names']: for extension in ['cpp', 'hpp']: data_pkg = { 'ros2_package_name': ros2_package_name, 'mappings': [ m for m in data['mappings'] if m.ros2_msg.package_name == ros2_package_name], 'services': [ s for s in data['services'] if s['ros2_package'] == ros2_package_name] } if extension == 'hpp': data_pkg.update({ 'ros1_msgs': [ m.ros1_msg for m in data['mappings'] if m.ros2_msg.package_name == ros2_package_name], 'ros2_msgs': [ m.ros2_msg for m in data['mappings'] if m.ros2_msg.package_name == ros2_package_name], }) template_file = os.path.join(template_dir, 'pkg_factories.%s.em' % extension) output_file = os.path.join( output_path, '%s_factories.%s' % (ros2_package_name, extension)) expand_template(template_file, data_pkg, output_file)
def generate_cpp(generator_arguments_file): args = read_generator_arguments(generator_arguments_file) template_dir = args['template_dir'] mapping_msgs = { os.path.join(template_dir, 'msg__rosidl_typesupport_introspection_cpp.hpp.em'): '%s__rosidl_typesupport_introspection_cpp.hpp', os.path.join(template_dir, 'msg__type_support.cpp.em'): '%s__type_support.cpp', } mapping_srvs = { os.path.join(template_dir, 'srv__rosidl_typesupport_introspection_cpp.hpp.em'): '%s__rosidl_typesupport_introspection_cpp.hpp', os.path.join(template_dir, 'srv__type_support.cpp.em'): '%s__type_support.cpp', } for template_file in mapping_msgs.keys(): assert os.path.exists( template_file), 'Could not find template: ' + template_file for template_file in mapping_srvs.keys(): assert os.path.exists( template_file), 'Could not find template: ' + template_file pkg_name = args['package_name'] known_msg_types = extract_message_types( pkg_name, args['ros_interface_files'], args.get('ros_interface_dependencies', [])) functions = { 'get_header_filename_from_msg_name': convert_camel_case_to_lower_case_underscore, } latest_target_timestamp = get_newest_modification_time( args['target_dependencies']) for ros_interface_file in args['ros_interface_files']: extension = os.path.splitext(ros_interface_file)[1] subfolder = os.path.basename(os.path.dirname(ros_interface_file)) if extension == '.msg': spec = parse_message_file(pkg_name, ros_interface_file) validate_field_types(spec, known_msg_types) for template_file, generated_filename in mapping_msgs.items(): generated_file = os.path.join( args['output_dir'], subfolder, generated_filename % convert_camel_case_to_lower_case_underscore( spec.base_type.type)) data = { 'spec': spec, 'subfolder': subfolder, 'cpp_primitives': MSG_TYPE_TO_CPP } data.update(functions) expand_template(template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) elif extension == '.srv': spec = parse_service_file(pkg_name, ros_interface_file) validate_field_types(spec, known_msg_types) for template_file, generated_filename in mapping_srvs.items(): generated_file = os.path.join( args['output_dir'], subfolder, generated_filename % convert_camel_case_to_lower_case_underscore(spec.srv_name)) data = {'spec': spec, 'subfolder': subfolder} data.update(functions) expand_template(template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) return 0
def generate_py(generator_arguments_file, typesupport_impls): mapping = { '_idl.py.em': '_%s.py', '_idl_support.c.em': '_%s_s.c', } generate_files(generator_arguments_file, mapping) args = read_generator_arguments(generator_arguments_file) package_name = args['package_name'] # expand init modules for each directory modules = {} idl_content = IdlContent() for idl_tuple in args.get('idl_tuples', []): idl_parts = idl_tuple.rsplit(':', 1) assert len(idl_parts) == 2 idl_rel_path = pathlib.Path(idl_parts[1]) idl_stems = modules.setdefault(str(idl_rel_path.parent), set()) idl_stems.add(idl_rel_path.stem) locator = IdlLocator(*idl_parts) idl_file = parse_idl_file(locator) idl_content.elements += idl_file.content.elements for subfolder in modules.keys(): with open(os.path.join(args['output_dir'], subfolder, '__init__.py'), 'w') as f: for idl_stem in sorted(modules[subfolder]): module_name = '_' + \ convert_camel_case_to_lower_case_underscore(idl_stem) f.write('from {package_name}.{subfolder}.{module_name} import ' '{idl_stem} # noqa: F401\n'.format_map(locals())) # expand templates per available typesupport implementation template_dir = args['template_dir'] type_support_impl_by_filename = { '_%s_s.ep.{0}.c'.format(impl): impl for impl in typesupport_impls } mapping_msg_pkg_extension = { os.path.join(template_dir, '_idl_pkg_typesupport_entry_point.c.em'): type_support_impl_by_filename.keys(), } for template_file in mapping_msg_pkg_extension.keys(): assert os.path.exists( template_file), 'Could not find template: ' + template_file latest_target_timestamp = get_newest_modification_time( args['target_dependencies']) for template_file, generated_filenames in mapping_msg_pkg_extension.items( ): for generated_filename in generated_filenames: package_name = args['package_name'] data = { 'package_name': args['package_name'], 'content': idl_content, 'typesupport_impl': type_support_impl_by_filename.get(generated_filename, ''), } generated_file = os.path.join(args['output_dir'], generated_filename % package_name) expand_template(template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) return 0
def generate_typesupport_freertps_cpp(template_dir, pkg_name, ros_interface_files, ros_interface_dependencies, target_dependencies, additional_files, output_dir): mapping_msgs = { os.path.join(template_dir, 'msg__type_support.hpp.template'): '%s__type_support.hpp', os.path.join(template_dir, 'msg__type_support.cpp.template'): '%s__type_support.cpp', } mapping_srvs = { os.path.join(template_dir, 'srv__type_support.cpp.template'): '%s__type_support.cpp', } for template_file in mapping_msgs.keys(): assert os.path.exists(template_file), 'Could not find template: ' + template_file for template_file in mapping_srvs.keys(): assert os.path.exists(template_file), 'Could not find template: ' + template_file known_msg_types = extract_message_types( pkg_name, ros_interface_files, ros_interface_dependencies) functions = { 'get_header_filename_from_msg_name': convert_camel_case_to_lower_case_underscore, 'enforce_alignment': enforce_alignment, 'enforce_read_alignment': enforce_read_alignment, 'enforce_alignment_buffer_size': enforce_alignment_buffer_size, 'get_alignment': get_alignment, 'msg_type_to_cpp': msg_type_to_cpp, } # generate_dds_freertps_cpp() and therefore the make target depend on the additional files # therefore they must be listed here even if the generated type support files are independent latest_target_timestamp = get_newest_modification_time( target_dependencies + additional_files) for idl_file in ros_interface_files: extension = os.path.splitext(idl_file)[1] if extension == '.msg': spec = parse_message_file(pkg_name, idl_file) validate_field_types(spec, known_msg_types) subfolder = os.path.basename(os.path.dirname(idl_file)) for template_file, generated_filename in mapping_msgs.items(): generated_file = os.path.join( output_dir, subfolder, 'freertps', generated_filename % convert_camel_case_to_lower_case_underscore(spec.base_type.type)) data = {'spec': spec, 'subfolder': subfolder} data.update(functions) expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) elif extension == '.srv': spec = parse_service_file(pkg_name, idl_file) validate_field_types(spec, known_msg_types) for template_file, generated_filename in mapping_srvs.items(): generated_file = os.path.join( output_dir, 'srv', 'freertps', generated_filename % convert_camel_case_to_lower_case_underscore(spec.srv_name)) data = {'spec': spec} data.update(functions) expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) return 0
def generate_cpp(output_path, template_dir): rospack = rospkg.RosPack() data = generate_messages(rospack) message_string_pairs = { ('%s/%s' % (m.ros1_msg.package_name, m.ros1_msg.message_name), '%s/%s' % (m.ros2_msg.package_name, m.ros2_msg.message_name)) for m in data['mappings'] } data.update( generate_services(rospack, message_string_pairs=message_string_pairs)) template_file = os.path.join(template_dir, 'get_mappings.cpp.em') output_file = os.path.join(output_path, 'get_mappings.cpp') data_for_template = { 'mappings': data['mappings'], 'services': data['services'] } expand_template(template_file, data_for_template, output_file) unique_package_names = set(data['ros2_package_names_msg'] + data['ros2_package_names_srv']) # skip builtin_interfaces since there is a custom implementation unique_package_names -= {'builtin_interfaces'} data['ros2_package_names'] = list(unique_package_names) template_file = os.path.join(template_dir, 'get_factory.cpp.em') output_file = os.path.join(output_path, 'get_factory.cpp') expand_template(template_file, data, output_file) for ros2_package_name in data['ros2_package_names']: data_pkg_hpp = { 'ros2_package_name': ros2_package_name, # include directives and template types 'mapped_ros1_msgs': [ m.ros1_msg for m in data['mappings'] if m.ros2_msg.package_name == ros2_package_name ], 'mapped_ros2_msgs': [ m.ros2_msg for m in data['mappings'] if m.ros2_msg.package_name == ros2_package_name ], # forward declaration of factory functions 'ros2_msg_types': [ m for m in data['all_ros2_msgs'] if m.package_name == ros2_package_name ], 'ros2_srv_types': [ s for s in data['all_ros2_srvs'] if s.package_name == ros2_package_name ], # forward declaration of template specializations 'mappings': [ m for m in data['mappings'] if m.ros2_msg.package_name == ros2_package_name ], } template_file = os.path.join(template_dir, 'pkg_factories.hpp.em') output_file = os.path.join(output_path, '%s_factories.hpp' % ros2_package_name) expand_template(template_file, data_pkg_hpp, output_file) data_pkg_cpp = { 'ros2_package_name': ros2_package_name, # call interface specific factory functions 'ros2_msg_types': data_pkg_hpp['ros2_msg_types'], 'ros2_srv_types': data_pkg_hpp['ros2_srv_types'], } template_file = os.path.join(template_dir, 'pkg_factories.cpp.em') output_file = os.path.join(output_path, '%s_factories.cpp' % ros2_package_name) expand_template(template_file, data_pkg_cpp, output_file) for interface_type, interfaces in zip( ['msg', 'srv'], [data['all_ros2_msgs'], data['all_ros2_srvs']]): for interface in interfaces: if interface.package_name != ros2_package_name: continue data_idl_cpp = { 'ros2_package_name': ros2_package_name, 'interface_type': interface_type, 'interface': interface, 'mapped_msgs': [], 'mapped_services': [], } if interface_type == 'msg': data_idl_cpp['mapped_msgs'] += [ m for m in data['mappings'] if m.ros2_msg.package_name == ros2_package_name and m.ros2_msg.message_name == interface.message_name ] if interface_type == 'srv': data_idl_cpp['mapped_services'] += [ s for s in data['services'] if s['ros2_package'] == ros2_package_name and s['ros2_name'] == interface.message_name ] template_file = os.path.join(template_dir, 'interface_factories.cpp.em') output_file = os.path.join( output_path, '%s__%s__%s__factories.cpp' % (ros2_package_name, interface_type, interface.message_name)) expand_template(template_file, data_idl_cpp, output_file)
def generate_cpp(output_file, template_dir): rospack = rospkg.RosPack() ros1_msgs = get_ros1_messages(rospack=rospack) ros2_msgs, mapping_rules = get_ros2_messages() package_pairs = determine_package_pairs(ros1_msgs, ros2_msgs, mapping_rules) message_pairs = determine_message_pairs(ros1_msgs, ros2_msgs, package_pairs, mapping_rules) mappings = [] for ros1_msg, ros2_msg in message_pairs: mapping = determine_field_mapping(ros1_msg, ros2_msg, mapping_rules, rospack=rospack) if mapping: mappings.append(mapping) # order mappings topologically to allow template specialization ordered_mappings = [] while mappings: # pick first mapping without unsatisfied dependencies for m in mappings: if not m.depends_on_ros2_messages: break else: break # move mapping to ordered list mappings.remove(m) ordered_mappings.append(m) ros2_msg = m.ros2_msg # update unsatisfied dependencies of remaining mappings for m in mappings: if ros2_msg in m.depends_on_ros2_messages: m.depends_on_ros2_messages.remove(ros2_msg) if mappings: print('%d mappings can not be generated due to missing dependencies:' % len(mappings), file=sys.stderr) for m in mappings: print( '- %s <-> %s:' % ('%s/%s' % (m.ros1_msg.package_name, m.ros1_msg.message_name), '%s/%s' % (m.ros2_msg.package_name, m.ros2_msg.message_name)), file=sys.stderr) for d in m.depends_on_ros2_messages: print(' -', '%s/%s' % (d.package_name, d.message_name), file=sys.stderr) print(file=sys.stderr) data = { 'ros1_msgs': [m.ros1_msg for m in ordered_mappings], 'ros2_msgs': [m.ros2_msg for m in ordered_mappings], 'mappings': ordered_mappings, } template_file = os.path.join(template_dir, 'generated_factories.cpp.template') expand_template(template_file, data, output_file)
def generate_dds_idl(generator_arguments_file, subfolders, extension_module_name): args = read_generator_arguments(generator_arguments_file) template_file = os.path.join(args['template_dir'], 'msg.idl.template') assert os.path.exists(template_file), \ "The template '%s' does not exist" % template_file # look for extensions for the default functions functions = { 'get_include_directives': get_include_directives, 'get_post_struct_lines': get_post_struct_lines, 'msg_type_to_idl': msg_type_to_idl, } if extension_module_name is not None: pkg = __import__(extension_module_name) module_name = extension_module_name.rsplit('.', 1)[1] if hasattr(pkg, module_name): module = getattr(pkg, module_name) for function_name in functions.keys(): if hasattr(module, function_name): functions[function_name] = \ getattr(module, function_name) pkg_name = args['package_name'] latest_target_timestamp = get_newest_modification_time(args['target_dependencies']) for ros_interface_file in args['ros_interface_files']: subfolder = os.path.basename(os.path.dirname(ros_interface_file)) extension = os.path.splitext(ros_interface_file)[1] output_path = os.path.join(args['output_dir'], subfolder) for sub in subfolders: output_path = os.path.join(output_path, sub) if extension == '.msg': spec = parse_message_file(pkg_name, ros_interface_file) generated_file = os.path.join(output_path, '%s_.idl' % spec.base_type.type) data = {'spec': spec, 'subfolder': subfolder, 'subfolders': subfolders} data.update(functions) expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) elif extension == '.srv': srv_spec = parse_service_file(pkg_name, ros_interface_file) request_fields = [ Field( Type('uint64', context_package_name=pkg_name), 'client_guid_0', default_value_string=None), Field( Type('uint64', context_package_name=pkg_name), 'client_guid_1', default_value_string=None), Field( Type('int64', context_package_name=pkg_name), 'sequence_number', default_value_string=None), Field( Type('%s_Request' % srv_spec.srv_name, context_package_name=pkg_name), 'request', default_value_string=None) ] response_fields = [ Field( Type('uint64', context_package_name=pkg_name), 'client_guid_0', default_value_string=None), Field( Type('uint64', context_package_name=pkg_name), 'client_guid_1', default_value_string=None), Field( Type('int64', context_package_name=pkg_name), 'sequence_number', default_value_string=None), Field( Type('%s_Response' % srv_spec.srv_name, context_package_name=pkg_name), 'response', default_value_string=None) ] constants = [] sample_spec_request = MessageSpecification( srv_spec.pkg_name, 'Sample_%s_Request' % srv_spec.srv_name, request_fields, constants) sample_spec_response = MessageSpecification( srv_spec.pkg_name, 'Sample_%s_Response' % srv_spec.srv_name, response_fields, constants) generated_files = [ (sample_spec_request, os.path.join( output_path, '%s_.idl' % sample_spec_request.base_type.type)), (sample_spec_response, os.path.join( output_path, '%s_.idl' % sample_spec_response.base_type.type)), ] for spec, generated_file in generated_files: data = {'spec': spec, 'subfolder': 'srv', 'subfolders': subfolders} data.update(functions) expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) return 0
def generate_cpp(output_path, template_dir): rospack = rospkg.RosPack() data = generate_messages(rospack) data.update(generate_services(rospack)) template_file = os.path.join(template_dir, 'get_mappings.cpp.em') output_file = os.path.join(output_path, 'get_mappings.cpp') data_for_template = { 'mappings': data['mappings'], 'services': data['services']} expand_template(template_file, data_for_template, output_file) unique_package_names = set(data['ros2_package_names_msg'] + data['ros2_package_names_srv']) # skip builtin_interfaces since there is a custom implementation unique_package_names -= {'builtin_interfaces'} data['ros2_package_names'] = list(unique_package_names) template_file = os.path.join(template_dir, 'get_factory.cpp.em') output_file = os.path.join(output_path, 'get_factory.cpp') expand_template(template_file, data, output_file) for ros2_package_name in data['ros2_package_names']: data_pkg_hpp = { 'ros2_package_name': ros2_package_name, # include directives and template types 'mapped_ros1_msgs': [ m.ros1_msg for m in data['mappings'] if m.ros2_msg.package_name == ros2_package_name], 'mapped_ros2_msgs': [ m.ros2_msg for m in data['mappings'] if m.ros2_msg.package_name == ros2_package_name], # forward declaration of factory functions 'ros2_msg_types': [ m for m in data['all_ros2_msgs'] if m.package_name == ros2_package_name], 'ros2_srv_types': [ s for s in data['all_ros2_srvs'] if s.package_name == ros2_package_name], # forward declaration of template specializations 'mappings': [ m for m in data['mappings'] if m.ros2_msg.package_name == ros2_package_name], } template_file = os.path.join(template_dir, 'pkg_factories.hpp.em') output_file = os.path.join( output_path, '%s_factories.hpp' % ros2_package_name) expand_template(template_file, data_pkg_hpp, output_file) data_pkg_cpp = { 'ros2_package_name': ros2_package_name, # call interface specific factory functions 'ros2_msg_types': data_pkg_hpp['ros2_msg_types'], 'ros2_srv_types': data_pkg_hpp['ros2_srv_types'], } template_file = os.path.join(template_dir, 'pkg_factories.cpp.em') output_file = os.path.join( output_path, '%s_factories.cpp' % ros2_package_name) expand_template(template_file, data_pkg_cpp, output_file) for interface_type, interfaces in zip( ['msg', 'srv'], [data['all_ros2_msgs'], data['all_ros2_srvs']] ): for interface in interfaces: if interface.package_name != ros2_package_name: continue data_idl_cpp = { 'ros2_package_name': ros2_package_name, 'interface_type': interface_type, 'interface': interface, 'mapped_msgs': [ m for m in data['mappings'] if m.ros2_msg.package_name == ros2_package_name and m.ros2_msg.message_name == interface.message_name], 'mapped_services': [ s for s in data['services'] if s['ros2_package'] == ros2_package_name and s['ros2_name'] == interface.message_name], } template_file = os.path.join(template_dir, 'interface_factories.cpp.em') output_file = os.path.join( output_path, '%s__%s__%s__factories.cpp' % (ros2_package_name, interface_type, interface.message_name)) expand_template(template_file, data_idl_cpp, output_file)
def generate_rs(generator_arguments_file, typesupport_impls): args = read_generator_arguments(generator_arguments_file) typesupport_impls = typesupport_impls.split(';') template_dir = args['template_dir'] type_support_impl_by_filename = { '%s_rs.ep.{0}.c'.format(impl): impl for impl in typesupport_impls } mapping_msgs = { os.path.join(template_dir, 'msg.rs.em'): ['rust/src/%s.rs'], os.path.join(template_dir, 'msg.c.em'): type_support_impl_by_filename.keys(), } mapping_srvs = { os.path.join(template_dir, 'srv.rs.em'): ['rust/src/%s.rs'], os.path.join(template_dir, 'srv.c.em'): type_support_impl_by_filename.keys(), } for template_file in mapping_msgs.keys(): assert os.path.exists(template_file), \ 'Messages template file %s not found' % template_file for template_file in mapping_srvs.keys(): assert os.path.exists(template_file), \ 'Services template file %s not found' % template_file data = { 'get_c_type': get_c_type, 'get_rs_type': get_rs_type, 'constant_value_to_rs': constant_value_to_rs, 'value_to_rs': value_to_rs, 'convert_camel_case_to_lower_case_underscore': convert_camel_case_to_lower_case_underscore, 'convert_lower_case_underscore_to_camel_case': convert_lower_case_underscore_to_camel_case, 'get_builtin_rs_type': get_builtin_rs_type, 'msg_specs': [], 'srv_specs': [], 'package_name': args['package_name'], 'typesupport_impls': typesupport_impls, } latest_target_timestamp = get_newest_modification_time( args['target_dependencies']) for ros_interface_file in args['ros_interface_files']: extension = os.path.splitext(ros_interface_file)[1] subfolder = os.path.basename(os.path.dirname(ros_interface_file)) if extension == '.msg': data['msg_specs'].append((subfolder, parse_message_file( args['package_name'], ros_interface_file))) elif extension == '.srv': data['srv_specs'].append((subfolder, parse_service_file( args['package_name'], ros_interface_file))) else: continue if data['msg_specs']: for template_file, generated_filenames in mapping_msgs.items(): for generated_filename in generated_filenames: generated_file = os.path.join(args['output_dir'], generated_filename % 'msg') expand_template( os.path.join(template_dir, template_file), data.copy(), generated_file, minimum_timestamp=latest_target_timestamp) if data['srv_specs']: for template_file, generated_filenames in mapping_srvs.items(): for generated_filename in generated_filenames: generated_file = os.path.join(args['output_dir'], generated_filename % 'srv') expand_template( os.path.join(template_dir, template_file), data.copy(), generated_file, minimum_timestamp=latest_target_timestamp) expand_template( os.path.join(template_dir, 'lib.rs.em'), data.copy(), os.path.join(args['output_dir'], 'rust/src/lib.rs'), minimum_timestamp=latest_target_timestamp) return 0
def generate_rs(generator_arguments_file, typesupport_impls): args = read_generator_arguments(generator_arguments_file) typesupport_impls = typesupport_impls.split(';') template_dir = args['template_dir'] type_support_impl_by_filename = { '%s_rs.ep.{0}.c'.format(impl): impl for impl in typesupport_impls } mapping_msgs = { os.path.join(template_dir, 'msg.rs.em'): ['rust/src/%s.rs'], os.path.join(template_dir, 'msg.c.em'): type_support_impl_by_filename.keys(), } mapping_srvs = { os.path.join(template_dir, 'srv.rs.em'): ['rust/src/%s.rs'], os.path.join(template_dir, 'srv.c.em'): type_support_impl_by_filename.keys(), } for template_file in mapping_msgs.keys(): assert os.path.exists(template_file), \ 'Messages template file %s not found' % template_file for template_file in mapping_srvs.keys(): assert os.path.exists(template_file), \ 'Services template file %s not found' % template_file data = { 'get_c_type': get_c_type, 'get_rs_type': get_rs_type, 'constant_value_to_rs': constant_value_to_rs, 'value_to_rs': value_to_rs, 'convert_camel_case_to_lower_case_underscore': convert_camel_case_to_lower_case_underscore, 'convert_lower_case_underscore_to_camel_case': convert_lower_case_underscore_to_camel_case, 'get_builtin_rs_type': get_builtin_rs_type, 'msg_specs': [], 'srv_specs': [], 'package_name': args['package_name'], 'typesupport_impls': typesupport_impls, } latest_target_timestamp = get_newest_modification_time( args['target_dependencies']) for ros_interface_file in args['ros_interface_files']: extension = os.path.splitext(ros_interface_file)[1] subfolder = os.path.basename(os.path.dirname(ros_interface_file)) if extension == '.msg': data['msg_specs'].append( (subfolder, parse_message_file(args['package_name'], ros_interface_file))) elif extension == '.srv': data['srv_specs'].append( (subfolder, parse_service_file(args['package_name'], ros_interface_file))) else: continue if data['msg_specs']: for template_file, generated_filenames in mapping_msgs.items(): for generated_filename in generated_filenames: generated_file = os.path.join(args['output_dir'], generated_filename % 'msg') expand_template(os.path.join(template_dir, template_file), data.copy(), generated_file, minimum_timestamp=latest_target_timestamp) if data['srv_specs']: for template_file, generated_filenames in mapping_srvs.items(): for generated_filename in generated_filenames: generated_file = os.path.join(args['output_dir'], generated_filename % 'srv') expand_template(os.path.join(template_dir, template_file), data.copy(), generated_file, minimum_timestamp=latest_target_timestamp) expand_template(os.path.join(template_dir, 'lib.rs.em'), data.copy(), os.path.join(args['output_dir'], 'rust/src/lib.rs'), minimum_timestamp=latest_target_timestamp) return 0
def generate_rs(generator_arguments_file, typesupport_impls): args = read_generator_arguments(generator_arguments_file) package_name = args['package_name'] # expand init modules for each directory modules = {} idl_content = IdlContent() dependency_packages = set() (Path(args['output_dir']) / 'rust/src').mkdir(parents=True, exist_ok=True) for dep_tuple in args.get('ros_interface_dependencies', []): dep_parts = dep_tuple.rsplit(':', 1) assert len(dep_parts) == 2 if dep_parts[0] != package_name: dependency_packages.add(dep_parts[0]) for idl_tuple in args.get('idl_tuples', []): idl_parts = idl_tuple.rsplit(':', 1) assert len(idl_parts) == 2 idl_rel_path = pathlib.Path(idl_parts[1]) idl_stems = modules.setdefault(str(idl_rel_path.parent), set()) idl_stems.add(idl_rel_path.stem) locator = IdlLocator(*idl_parts) idl_file = parse_idl_file(locator) idl_content.elements += idl_file.content.elements typesupport_impls = typesupport_impls.split(';') template_dir = args['template_dir'] mapping_msgs = { os.path.join(template_dir, 'msg.rs.em'): ['rust/src/%s.rs'], } mapping_srvs = { os.path.join(template_dir, 'srv.rs.em'): ['rust/src/%s.rs'], } # Ensure the required templates exist for template_file in mapping_msgs.keys(): assert os.path.exists(template_file), \ 'Messages template file %s not found' % template_file for template_file in mapping_srvs.keys(): assert os.path.exists(template_file), \ 'Services template file %s not found' % template_file data = { 'get_rmw_rs_type': make_get_rmw_rs_type(args['package_name']), 'get_rs_name': get_rs_name, 'get_idiomatic_rs_type': make_get_idiomatic_rs_type(args['package_name']), 'constant_value_to_rs': constant_value_to_rs, 'value_to_rs': value_to_rs, 'convert_camel_case_to_lower_case_underscore': convert_camel_case_to_lower_case_underscore, 'convert_lower_case_underscore_to_camel_case': convert_lower_case_underscore_to_camel_case, 'msg_specs': [], 'srv_specs': [], 'package_name': args['package_name'], 'typesupport_impls': typesupport_impls, } latest_target_timestamp = get_newest_modification_time( args['target_dependencies']) for message in idl_content.get_elements_of_type(Message): data['msg_specs'].append(('msg', message)) for service in idl_content.get_elements_of_type(Service): data['srv_specs'].append(('srv', service)) if data['msg_specs']: for template_file, generated_filenames in mapping_msgs.items(): for generated_filename in generated_filenames: generated_file = os.path.join(args['output_dir'], generated_filename % 'msg') expand_template(os.path.join(template_dir, template_file), data.copy(), generated_file, minimum_timestamp=latest_target_timestamp) if data['srv_specs']: for template_file, generated_filenames in mapping_srvs.items(): for generated_filename in generated_filenames: generated_file = os.path.join(args['output_dir'], generated_filename % 'srv') expand_template(os.path.join(template_dir, template_file), data.copy(), generated_file, minimum_timestamp=latest_target_timestamp) expand_template(os.path.join(template_dir, 'lib.rs.em'), data.copy(), os.path.join(args['output_dir'], 'rust/src/lib.rs'), minimum_timestamp=latest_target_timestamp) cargo_toml_data = { 'dependency_packages': dependency_packages, 'package_name': args['package_name'], 'package_version': args['package_version'], } expand_template(os.path.join(template_dir, 'Cargo.toml.em'), cargo_toml_data, os.path.join(args['output_dir'], 'rust/Cargo.toml'), minimum_timestamp=latest_target_timestamp) expand_template(os.path.join(template_dir, 'build.rs.em'), {}, os.path.join(args['output_dir'], 'rust/build.rs'), minimum_timestamp=latest_target_timestamp) return 0
def generate_typesupport_opensplice_c(args): template_dir = args['template_dir'] mapping_msgs = { os.path.join(template_dir, 'msg__type_support_c.cpp.template'): '%s__type_support_c.cpp', } mapping_srvs = { os.path.join(template_dir, 'srv__type_support_c.cpp.template'): '%s__type_support_c.cpp', } for template_file in mapping_msgs.keys(): assert os.path.exists(template_file), 'Could not find template: ' + template_file for template_file in mapping_srvs.keys(): assert os.path.exists(template_file), 'Could not find template: ' + template_file pkg_name = args['package_name'] known_msg_types = extract_message_types( pkg_name, args['ros_interface_files'], args.get('ros_interface_dependencies', [])) functions = { 'get_header_filename_from_msg_name': convert_camel_case_to_lower_case_underscore, } # generate_dds_opensplice_cpp() and therefore the make target depend on the additional files # therefore they must be listed here even if the generated type support files are independent latest_target_timestamp = get_newest_modification_time( args['target_dependencies'] + args.get('additional_files', [])) for idl_file in args['ros_interface_files']: extension = os.path.splitext(idl_file)[1] if extension == '.msg': spec = parse_message_file(pkg_name, idl_file) validate_field_types(spec, known_msg_types) subfolder = os.path.basename(os.path.dirname(idl_file)) for template_file, generated_filename in mapping_msgs.items(): generated_file = os.path.join( args['output_dir'], subfolder, 'dds_opensplice_c', generated_filename % convert_camel_case_to_lower_case_underscore(spec.base_type.type)) data = { 'spec': spec, 'pkg': spec.base_type.pkg_name, 'msg': spec.msg_name, 'type': spec.base_type.type, 'subfolder': subfolder, } data.update(functions) try: expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) except Exception: print("\nException when expanding '{0}' into '{1}':\n" .format(template_file, generated_file)) raise elif extension == '.srv': spec = parse_service_file(pkg_name, idl_file) validate_field_types(spec, known_msg_types) for template_file, generated_filename in mapping_srvs.items(): generated_file = os.path.join( args['output_dir'], 'srv', 'dds_opensplice_c', generated_filename % convert_camel_case_to_lower_case_underscore(spec.srv_name)) data = {'spec': spec} data.update(functions) expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) return 0
def generate_objc(generator_arguments_file, typesupport_impl, typesupport_impls): args = read_generator_arguments(generator_arguments_file) typesupport_impls = typesupport_impls.split(';') template_dir = args['template_dir'] type_support_impl_by_filename = { '%s_s.ep.{0}.m'.format(impl): impl for impl in typesupport_impls } mapping_msgs = { os.path.join(template_dir, 'msg.h.template'): ['%s.h'], os.path.join(template_dir, 'msg.m.template'): type_support_impl_by_filename.keys(), } mapping_srvs = { os.path.join(template_dir, 'srv.h.template'): ['%s.h'], os.path.join(template_dir, 'srv.m.template'): type_support_impl_by_filename.keys(), } for template_file in mapping_msgs.keys(): assert os.path.exists(template_file), \ 'Messages template file %s not found' % template_file for template_file in mapping_srvs.keys(): assert os.path.exists(template_file), \ 'Services template file %s not found' % template_file functions = { 'get_objc_type': get_objc_type, } latest_target_timestamp = get_newest_modification_time(args['target_dependencies']) modules = defaultdict(list) for ros_interface_file in args['ros_interface_files']: extension = os.path.splitext(ros_interface_file)[1] subfolder = os.path.basename(os.path.dirname(ros_interface_file)) output_dir = args['output_dir'] if extension == '.msg': spec = parse_message_file(args['package_name'], ros_interface_file) mapping = mapping_msgs type_name = spec.base_type.type elif extension == '.srv': spec = parse_service_file(args['package_name'], ros_interface_file) mapping = mapping_srvs type_name = spec.srv_name else: continue module_name = convert_camel_case_to_lower_case_underscore(type_name) modules[subfolder].append((module_name, type_name)) package_name = args['package_name'] jni_package_name = package_name.replace('_', '_1') jni_type_name = type_name.replace('_', '_1') for template_file, generated_filenames in mapping.items(): for generated_filename in generated_filenames: data = { 'constant_value_to_objc': constant_value_to_objc, 'convert_camel_case_to_lower_case_underscore': convert_camel_case_to_lower_case_underscore, 'get_builtin_objc_type': get_builtin_objc_type, 'module_name': module_name, 'package_name': package_name, 'jni_package_name': jni_package_name, 'jni_type_name': jni_type_name, 'spec': spec, 'subfolder': subfolder, 'typesupport_impl': type_support_impl_by_filename.get(generated_filename, ''), 'typesupport_impls': typesupport_impls, 'type_name': type_name, } data.update(functions) generated_file = os.path.join( output_dir, subfolder, generated_filename % type_name) expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) return 0
def generate_dds_idl(generator_arguments_file, subfolders, extension_module_name): args = read_generator_arguments(generator_arguments_file) template_file = os.path.join(args['template_dir'], 'msg.idl.em') assert os.path.exists(template_file), \ "The template '%s' does not exist" % template_file # look for extensions for the default functions functions = { 'get_include_directives': get_include_directives, 'get_post_struct_lines': get_post_struct_lines, 'msg_type_to_idl': msg_type_to_idl, } if extension_module_name is not None: pkg = __import__(extension_module_name) module_name = extension_module_name.rsplit('.', 1)[1] if hasattr(pkg, module_name): module = getattr(pkg, module_name) for function_name in functions.keys(): if hasattr(module, function_name): functions[function_name] = \ getattr(module, function_name) pkg_name = args['package_name'] latest_target_timestamp = get_newest_modification_time(args['target_dependencies']) for ros_interface_file in args['ros_interface_files']: subfolder = os.path.basename(os.path.dirname(ros_interface_file)) extension = os.path.splitext(ros_interface_file)[1] output_path = os.path.join(args['output_dir'], subfolder) for sub in subfolders: output_path = os.path.join(output_path, sub) if extension == '.msg': spec = parse_message_file(pkg_name, ros_interface_file) generated_file = os.path.join(output_path, '%s_.idl' % spec.base_type.type) data = {'spec': spec, 'subfolder': subfolder, 'subfolders': subfolders} data.update(functions) expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) elif extension == '.srv': srv_spec = parse_service_file(pkg_name, ros_interface_file) request_fields = [ Field( Type('uint64', context_package_name=pkg_name), 'client_guid_0', default_value_string=None), Field( Type('uint64', context_package_name=pkg_name), 'client_guid_1', default_value_string=None), Field( Type('int64', context_package_name=pkg_name), 'sequence_number', default_value_string=None), Field( Type('%s_Request' % srv_spec.srv_name, context_package_name=pkg_name), 'request', default_value_string=None) ] response_fields = [ Field( Type('uint64', context_package_name=pkg_name), 'client_guid_0', default_value_string=None), Field( Type('uint64', context_package_name=pkg_name), 'client_guid_1', default_value_string=None), Field( Type('int64', context_package_name=pkg_name), 'sequence_number', default_value_string=None), Field( Type('%s_Response' % srv_spec.srv_name, context_package_name=pkg_name), 'response', default_value_string=None) ] constants = [] sample_spec_request = MessageSpecification( srv_spec.pkg_name, 'Sample_%s_Request' % srv_spec.srv_name, request_fields, constants) sample_spec_response = MessageSpecification( srv_spec.pkg_name, 'Sample_%s_Response' % srv_spec.srv_name, response_fields, constants) generated_files = [ (sample_spec_request, os.path.join( output_path, '%s_.idl' % sample_spec_request.base_type.type)), (sample_spec_response, os.path.join( output_path, '%s_.idl' % sample_spec_response.base_type.type)), ] for spec, generated_file in generated_files: data = {'spec': spec, 'subfolder': 'srv', 'subfolders': subfolders} data.update(functions) expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) return 0
def generate_cpp(args, message_specs, service_specs, known_msg_types): template_dir = args['template_dir'] mapping_msgs = { os.path.join(template_dir, 'msg__rosidl_typesupport_coredx_cpp.hpp.em'): '%s__rosidl_typesupport_coredx_cpp.hpp', os.path.join(template_dir, 'msg__type_support.cpp.em'): '%s__type_support.cpp', } mapping_srvs = { os.path.join(template_dir, 'srv__rosidl_typesupport_coredx_cpp.hpp.em'): '%s__rosidl_typesupport_coredx_cpp.hpp', os.path.join(template_dir, 'srv__type_support.cpp.em'): '%s__type_support.cpp', } for template_file in mapping_msgs.keys(): assert os.path.exists( template_file), '**** Could not find template: ' + template_file for template_file in mapping_srvs.keys(): assert os.path.exists( template_file), '**** Could not find template: ' + template_file functions = { 'get_header_filename_from_msg_name': convert_camel_case_to_lower_case_underscore, } # generate_dds_coredx_cpp() and therefore the make target depend on the additional files # therefore they must be listed here even if the generated type support files are independent latest_target_timestamp = get_newest_modification_time( args['target_dependencies'] + args.get('additional_files', [])) for idl_file, spec in message_specs: validate_field_types(spec, known_msg_types) subfolder = os.path.basename(os.path.dirname(idl_file)) for template_file, generated_filename in mapping_msgs.items(): generated_file = os.path.join(args['output_dir'], subfolder) if generated_filename.endswith('.cpp'): generated_file = os.path.join(generated_file, 'dds_coredx') generated_file = os.path.join( generated_file, generated_filename % convert_camel_case_to_lower_case_underscore( spec.base_type.type)) data = {'spec': spec, 'subfolder': subfolder} data.update(functions) # print(" ... expanding msg " + template_file + " for idl_file " # + idl_file + " to " + generated_file) expand_template(template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) for spec in service_specs: validate_field_types(spec, known_msg_types) for template_file, generated_filename in mapping_srvs.items(): generated_file = os.path.join(args['output_dir'], 'srv') if generated_filename.endswith('.cpp'): generated_file = os.path.join(generated_file, 'dds_coredx') generated_file = os.path.join( generated_file, generated_filename % convert_camel_case_to_lower_case_underscore(spec.srv_name)) data = {'spec': spec} data.update(functions) # print(" ... expanding srv " + template_file + " for idl_file " + # idl_file + " to " + generated_file) expand_template(template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) return 0
def generate_py(generator_arguments_file, typesupport_impls): args = read_generator_arguments(generator_arguments_file) template_dir = args['template_dir'] type_support_impl_by_filename = { '_%s_s.ep.{0}.c'.format(impl): impl for impl in typesupport_impls } mapping_msgs = { os.path.join(template_dir, '_msg.py.em'): ['_%s.py'], os.path.join(template_dir, '_msg_support.c.em'): ['_%s_s.c'], } mapping_extension_msgs = { os.path.join(template_dir, '_msg_support.entry_point.c.em'): type_support_impl_by_filename.keys(), } mapping_srvs = { os.path.join(template_dir, '_srv.py.em'): ['_%s.py'], } for template_file in mapping_msgs.keys(): assert os.path.exists(template_file), 'Could not find template: ' + template_file for template_file in mapping_extension_msgs.keys(): assert os.path.exists(template_file), 'Could not find template: ' + template_file for template_file in mapping_srvs.keys(): assert os.path.exists(template_file), 'Could not find template: ' + template_file functions = { 'constant_value_to_py': constant_value_to_py, 'get_python_type': get_python_type, 'primitive_msg_type_to_c': primitive_msg_type_to_c, 'value_to_py': value_to_py, 'convert_camel_case_to_lower_case_underscore': convert_camel_case_to_lower_case_underscore, } latest_target_timestamp = get_newest_modification_time(args['target_dependencies']) modules = defaultdict(list) message_specs = [] service_specs = [] for ros_interface_file in args['ros_interface_files']: extension = os.path.splitext(ros_interface_file)[1] subfolder = os.path.basename(os.path.dirname(ros_interface_file)) if extension == '.msg': spec = parse_message_file(args['package_name'], ros_interface_file) message_specs.append((spec, subfolder)) mapping = mapping_msgs type_name = spec.base_type.type elif extension == '.srv': spec = parse_service_file(args['package_name'], ros_interface_file) service_specs.append((spec, subfolder)) mapping = mapping_srvs type_name = spec.srv_name else: continue module_name = convert_camel_case_to_lower_case_underscore(type_name) modules[subfolder].append((module_name, type_name)) for template_file, generated_filenames in mapping.items(): for generated_filename in generated_filenames: data = { 'module_name': module_name, 'package_name': args['package_name'], 'spec': spec, 'subfolder': subfolder, 'typesupport_impl': type_support_impl_by_filename.get(generated_filename, ''), } data.update(functions) generated_file = os.path.join( args['output_dir'], subfolder, generated_filename % module_name) expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) for subfolder in modules.keys(): import_list = {} for module_name, type_ in modules[subfolder]: if subfolder == 'srv' and (type_.endswith('Request') or type_.endswith('Response')): continue import_list['%s # noqa\n' % type_] = 'from %s.%s._%s import %s\n' % \ (args['package_name'], subfolder, module_name, type_) with open(os.path.join(args['output_dir'], subfolder, '__init__.py'), 'w') as f: for import_line in sorted(import_list.values()): f.write(import_line) for noqa_line in sorted(import_list.keys()): f.write(noqa_line) for template_file, generated_filenames in mapping_extension_msgs.items(): for generated_filename in generated_filenames: data = { 'module_name': module_name, 'package_name': args['package_name'], 'message_specs': message_specs, 'service_specs': service_specs, 'typesupport_impl': type_support_impl_by_filename.get(generated_filename, ''), } data.update(functions) generated_file = os.path.join( args['output_dir'], generated_filename % args['package_name']) expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) return 0
def generate_py(generator_arguments_file, typesupport_impls): args = read_generator_arguments(generator_arguments_file) template_dir = args['template_dir'] type_support_impl_by_filename = { '_%s_s.ep.{0}.c'.format(impl): impl for impl in typesupport_impls } mapping_msgs = { os.path.join(template_dir, '_msg.py.em'): ['_%s.py'], os.path.join(template_dir, '_msg_support.c.em'): ['_%s_s.c'], } mapping_msg_pkg_extension = { os.path.join(template_dir, '_msg_pkg_typesupport_entry_point.c.em'): type_support_impl_by_filename.keys(), } mapping_srvs = { os.path.join(template_dir, '_srv.py.em'): ['_%s.py'], } mapping_actions = { os.path.join(template_dir, '_action.py.em'): ['_%s.py'], } for template_file in mapping_msgs.keys(): assert os.path.exists( template_file), 'Could not find template: ' + template_file for template_file in mapping_msg_pkg_extension.keys(): assert os.path.exists( template_file), 'Could not find template: ' + template_file for template_file in mapping_srvs.keys(): assert os.path.exists( template_file), 'Could not find template: ' + template_file for template_file in mapping_actions.keys(): assert os.path.exists( template_file), 'Could not find template: ' + template_file functions = { 'constant_value_to_py': constant_value_to_py, 'get_python_type': get_python_type, 'primitive_msg_type_to_c': primitive_msg_type_to_c, 'value_to_py': value_to_py, 'convert_camel_case_to_lower_case_underscore': convert_camel_case_to_lower_case_underscore, } latest_target_timestamp = get_newest_modification_time( args['target_dependencies']) modules = defaultdict(list) message_specs = [] service_specs = [] action_specs = [] for ros_interface_file in args['ros_interface_files']: extension = os.path.splitext(ros_interface_file)[1] subfolder = os.path.basename(os.path.dirname(ros_interface_file)) if extension == '.msg': spec = parse_message_file(args['package_name'], ros_interface_file) message_specs.append((spec, subfolder)) mapping = mapping_msgs type_name = spec.base_type.type elif extension == '.srv': spec = parse_service_file(args['package_name'], ros_interface_file) service_specs.append((spec, subfolder)) mapping = mapping_srvs type_name = spec.srv_name elif extension == '.action': spec = parse_action_file(args['package_name'], ros_interface_file) action_specs.append((spec, subfolder)) mapping = mapping_actions type_name = spec.action_name else: continue module_name = convert_camel_case_to_lower_case_underscore(type_name) modules[subfolder].append((module_name, type_name)) for template_file, generated_filenames in mapping.items(): for generated_filename in generated_filenames: data = { 'module_name': module_name, 'package_name': args['package_name'], 'spec': spec, 'subfolder': subfolder, } data.update(functions) generated_file = os.path.join(args['output_dir'], subfolder, generated_filename % module_name) expand_template(template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) for subfolder in modules.keys(): import_list = {} for module_name, type_ in modules[subfolder]: if (subfolder == 'srv' or subfolder == 'action') and \ (type_.endswith('Request') or type_.endswith('Response')): continue import_list['%s # noqa\n' % type_] = 'from %s.%s._%s import %s\n' % \ (args['package_name'], subfolder, module_name, type_) path_to_module = os.path.join(args['output_dir'], subfolder, '__init__.py') content = '' if os.path.isfile(path_to_module): with open(path_to_module, 'r') as f: content = f.read() with open(path_to_module, 'w') as f: block_name = args['package_name'] if action_specs: block_name += '_action' content = re.sub( r'# BEGIN %s$.*^# END %s' % (block_name, block_name), '', content, 0, re.M | re.S) content = re.sub(r'^\s*$', '', content, 0, re.M) content = ''.join(['# BEGIN %s\n' % block_name] + sorted(import_list.values()) + # import_line sorted(import_list.keys()) + # noqa_line ['# END %s\n' % block_name]) + content f.write(content) for template_file, generated_filenames in mapping_msg_pkg_extension.items( ): for generated_filename in generated_filenames: package_name = args['package_name'] if action_specs: package_name += '_action' data = { 'package_name': package_name, 'action_specs': action_specs, 'message_specs': message_specs, 'service_specs': service_specs, 'typesupport_impl': type_support_impl_by_filename.get(generated_filename, ''), } data.update(functions) generated_file = os.path.join(args['output_dir'], generated_filename % package_name) expand_template(template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) return 0
def generate_rs(generator_arguments_file, typesupport_impls): args = read_generator_arguments(generator_arguments_file) package_name = args['package_name'] # expand init modules for each directory modules = {} idl_content = IdlContent() for idl_tuple in args.get('idl_tuples', []): idl_parts = idl_tuple.rsplit(':', 1) assert len(idl_parts) == 2 idl_rel_path = pathlib.Path(idl_parts[1]) idl_stems = modules.setdefault(str(idl_rel_path.parent), set()) idl_stems.add(idl_rel_path.stem) locator = IdlLocator(*idl_parts) idl_file = parse_idl_file(locator) idl_content.elements += idl_file.content.elements typesupport_impls = typesupport_impls.split(';') template_dir = args['template_dir'] type_support_impl_by_filename = { '%s_rs.ep.{0}.c'.format(impl): impl for impl in typesupport_impls } mapping_msgs = { os.path.join(template_dir, 'msg.rs.em'): ['rust/src/%s.rs'], os.path.join(template_dir, 'msg.c.em'): type_support_impl_by_filename.keys(), } mapping_srvs = { os.path.join(template_dir, 'srv.rs.em'): ['rust/src/%s.rs'], os.path.join(template_dir, 'srv.c.em'): type_support_impl_by_filename.keys(), } # Ensure the required templates exist for template_file in mapping_msgs.keys(): assert os.path.exists(template_file), \ 'Messages template file %s not found' % template_file for template_file in mapping_srvs.keys(): assert os.path.exists(template_file), \ 'Services template file %s not found' % template_file data = { 'get_c_type': get_c_type, 'get_rs_type': get_rs_type, 'constant_value_to_rs': constant_value_to_rs, 'value_to_rs': value_to_rs, 'convert_camel_case_to_lower_case_underscore': convert_camel_case_to_lower_case_underscore, 'convert_lower_case_underscore_to_camel_case': convert_lower_case_underscore_to_camel_case, 'get_builtin_rs_type': get_builtin_rs_type, 'msg_specs': [], 'srv_specs': [], 'package_name': args['package_name'], 'typesupport_impls': typesupport_impls, } latest_target_timestamp = get_newest_modification_time( args['target_dependencies']) for message in idl_content.get_elements_of_type(Message): data['msg_specs'].append(('msg', message)) for service in idl_content.get_elements_of_type(Service): data['srv_specs'].append(('srv', service)) if data['msg_specs']: for template_file, generated_filenames in mapping_msgs.items(): for generated_filename in generated_filenames: generated_file = os.path.join(args['output_dir'], generated_filename % 'msg') expand_template( os.path.join(template_dir, template_file), data.copy(), generated_file, minimum_timestamp=latest_target_timestamp) if data['srv_specs']: for template_file, generated_filenames in mapping_srvs.items(): for generated_filename in generated_filenames: generated_file = os.path.join(args['output_dir'], generated_filename % 'srv') expand_template( os.path.join(template_dir, template_file), data.copy(), generated_file, minimum_timestamp=latest_target_timestamp) expand_template( os.path.join(template_dir, 'lib.rs.em'), data.copy(), os.path.join(args['output_dir'], 'rust/src/lib.rs'), minimum_timestamp=latest_target_timestamp) return 0
def generate_py(generator_arguments_file, typesupport_impls): mapping = { '_idl.py.em': '_%s.py', '_idl_support.c.em': '_%s_s.c', } generate_files(generator_arguments_file, mapping) args = read_generator_arguments(generator_arguments_file) package_name = args['package_name'] # expand init modules for each directory modules = {} idl_content = IdlContent() for idl_tuple in args.get('idl_tuples', []): idl_parts = idl_tuple.rsplit(':', 1) assert len(idl_parts) == 2 idl_rel_path = pathlib.Path(idl_parts[1]) idl_stems = modules.setdefault(str(idl_rel_path.parent), set()) idl_stems.add(idl_rel_path.stem) locator = IdlLocator(*idl_parts) idl_file = parse_idl_file(locator) idl_content.elements += idl_file.content.elements # NOTE(sam): remove when a language specific name mangling is implemented def print_warning_if_reserved_keyword(member_name, interface_type, interface_name): if (keyword.iskeyword(member.name)): print("Member name '{}' in the {} '{}' is a " 'reserved keyword in Python and is not supported ' 'at the moment. Please use a different name.'.format( member_name, interface_type, interface_name), file=sys.stderr) for message in idl_content.get_elements_of_type(Message): for member in message.structure.members: print_warning_if_reserved_keyword( member.name, 'message', message.structure.namespaced_type.name) for service in idl_content.get_elements_of_type(Service): for member in service.request_message.structure.members: print_warning_if_reserved_keyword(member.name, 'service request', service.namespaced_type.name) for member in service.response_message.structure.members: print_warning_if_reserved_keyword(member.name, 'service response', service.namespaced_type.name) for action in idl_content.get_elements_of_type(Action): for member in action.goal.structure.members: print_warning_if_reserved_keyword(member.name, 'action goal', action.namespaced_type.name) for member in action.feedback.structure.members: print_warning_if_reserved_keyword(member.name, 'action feedback', action.namespaced_type.name) for member in action.result.structure.members: print_warning_if_reserved_keyword(member.name, 'action result', action.namespaced_type.name) for subfolder in modules.keys(): with open(os.path.join(args['output_dir'], subfolder, '__init__.py'), 'w') as f: for idl_stem in sorted(modules[subfolder]): module_name = '_' + \ convert_camel_case_to_lower_case_underscore(idl_stem) f.write( f'from {package_name}.{subfolder}.{module_name} import ' f'{idl_stem} # noqa: F401\n') # expand templates per available typesupport implementation template_dir = args['template_dir'] type_support_impl_by_filename = { '_%s_s.ep.{0}.c'.format(impl): impl for impl in typesupport_impls } mapping_msg_pkg_extension = { os.path.join(template_dir, '_idl_pkg_typesupport_entry_point.c.em'): type_support_impl_by_filename.keys(), } for template_file in mapping_msg_pkg_extension.keys(): assert os.path.exists( template_file), 'Could not find template: ' + template_file latest_target_timestamp = get_newest_modification_time( args['target_dependencies']) for template_file, generated_filenames in mapping_msg_pkg_extension.items( ): for generated_filename in generated_filenames: package_name = args['package_name'] data = { 'package_name': args['package_name'], 'content': idl_content, 'typesupport_impl': type_support_impl_by_filename.get(generated_filename, ''), } generated_file = os.path.join(args['output_dir'], generated_filename % package_name) expand_template(template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) return 0
def generate_typekit(package, output_dir, messages=[], services=[], actions=[]): output_dir = pathlib.Path(output_dir) for message in messages: header_name = convert_camel_case_to_lower_case_underscore(message) data = { 'header_name': header_name, 'msg_header': f'{package}/msg/{header_name}.hpp', 'msg_name': message, 'pkg_name': package } expand_template('msg_Types.hpp.em', data=data, output_file=(output_dir / "msg" / (header_name + "_Types.hpp")), template_basepath=TEMPLATE_DIR) expand_template('msg_typekit.cpp.em', data=data, output_file=(output_dir / "msg" / (header_name + "_typekit.cpp")), template_basepath=TEMPLATE_DIR) for service in services: header_name = convert_camel_case_to_lower_case_underscore(service) data = { 'header_name': header_name, 'srv_header': f'{package}/srv/{header_name}.hpp', 'srv_name': f'{service}', 'pkg_name': package } expand_template('srv_Types.hpp.em', data=data, output_file=(output_dir / "srv" / (header_name + "_Types.hpp")), template_basepath=TEMPLATE_DIR) expand_template('srv_typekit.cpp.em', data=data, output_file=(output_dir / "srv" / (header_name + "_typekit.cpp")), template_basepath=TEMPLATE_DIR) for action in actions: header_name = convert_camel_case_to_lower_case_underscore(action) data = { 'header_name': header_name, 'action_header': f'{package}/action/{header_name}.hpp', 'action_name': f'{action}', 'pkg_name': package } expand_template('action_Types.hpp.em', data=data, output_file=(output_dir / "action" / (header_name + "_Types.hpp")), template_basepath=TEMPLATE_DIR) expand_template('action_typekit.cpp.em', data=data, output_file=(output_dir / "action" / (header_name + "_typekit.cpp")), template_basepath=TEMPLATE_DIR) data = { 'pkg_name': package, 'messages': messages, 'services': services, 'actions': actions } expand_template('Types.hpp.em', data=data, output_file=(output_dir / "Types.hpp"), template_basepath=TEMPLATE_DIR) expand_template('typekit_plugin.cpp.em', data=data, output_file=(output_dir / "typekit_plugin.cpp"), template_basepath=TEMPLATE_DIR)
def generate_java(generator_arguments_file, typesupport_impl, typesupport_impls): args = read_generator_arguments(generator_arguments_file) typesupport_impls = typesupport_impls.split(';') template_dir = args['template_dir'] type_support_impl_by_filename = { '%s.ep.{0}.cpp'.format(impl): impl for impl in typesupport_impls } mapping_msgs = { os.path.join(template_dir, 'msg.java.em'): ['%s.java'], os.path.join(template_dir, 'msg.cpp.em'): type_support_impl_by_filename.keys(), } mapping_srvs = { os.path.join(template_dir, 'srv.java.em'): ['%s.java'], os.path.join(template_dir, 'srv.cpp.em'): type_support_impl_by_filename.keys(), } for template_file in mapping_msgs.keys(): assert os.path.exists(template_file), \ 'Messages template file %s not found' % template_file for template_file in mapping_srvs.keys(): assert os.path.exists(template_file), \ 'Services template file %s not found' % template_file functions = {'get_java_type': get_java_type, } latest_target_timestamp = get_newest_modification_time(args['target_dependencies']) modules = defaultdict(list) for ros_interface_file in args['ros_interface_files']: extension = os.path.splitext(ros_interface_file)[1] subfolder = os.path.basename(os.path.dirname(ros_interface_file)) if extension == '.msg': spec = parse_message_file(args['package_name'], ros_interface_file) mapping = mapping_msgs type_name = spec.base_type.type elif extension == '.srv': spec = parse_service_file(args['package_name'], ros_interface_file) mapping = mapping_srvs type_name = spec.srv_name else: continue module_name = convert_camel_case_to_lower_case_underscore(type_name) modules[subfolder].append((module_name, type_name)) package_name = args['package_name'] jni_package_name = package_name.replace('_', '_1') jni_type_name = type_name.replace('_', '_1') for template_file, generated_filenames in mapping.items(): for generated_filename in generated_filenames: data = { 'constant_value_to_java': constant_value_to_java, 'value_to_java': value_to_java, 'convert_camel_case_to_lower_case_underscore': convert_camel_case_to_lower_case_underscore, 'convert_lower_case_underscore_to_camel_case': convert_lower_case_underscore_to_camel_case, 'get_builtin_java_type': get_builtin_java_type, 'module_name': module_name, 'package_name': package_name, 'jni_package_name': jni_package_name, 'jni_type_name': jni_type_name, 'spec': spec, 'subfolder': subfolder, 'typesupport_impl': type_support_impl_by_filename.get(generated_filename, ''), 'typesupport_impls': typesupport_impls, 'type_name': type_name, } data.update(functions) generated_file = os.path.join(args['output_dir'], subfolder, generated_filename % type_name) expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) return 0
def generate_cpp(output_path, template_dir): msgs = generate_messages() for pack in msgs['ros2_package_names_msg']: msg_list = { 'mappings': [ m for m in msgs['mappings'] if (m.ros2_msg.package_name == pack and m.fields is not None) ] } data_for_template = { 'mappings': msg_list['mappings'], 'ros2_package_name': pack } template_file = os.path.join(template_dir, 'pkg_building_interface.cpp.em') output_file = os.path.join(output_path, '%s_building_interface.cpp' % pack) try: expand_template(template_file, data_for_template, output_file) except RuntimeError as e: print(str(e), file=sys.stderr) data_for_template = { 'mappings': msg_list['mappings'], 'ros2_package_name': pack } template_file = os.path.join(template_dir, 'pkg_building_interface.hpp.em') output_file = os.path.join(output_path, '%s_building_interface.hpp' % pack) try: expand_template(template_file, data_for_template, output_file) except RuntimeError as e: print(str(e), file=sys.stderr) template_file = os.path.join(template_dir, 'building_interface.cpp.em') output_file = os.path.join(output_path, 'building_interface.cpp') data_for_template = {'ros2_package_names': msgs['ros2_package_names_msg']} try: expand_template(template_file, data_for_template, output_file) except RuntimeError as e: print(str(e), file=sys.stderr) template_file = os.path.join(template_dir, 'pkg_yaml_helper.cpp.em') output_file = os.path.join(output_path, 'yaml-helper.cpp') data_for_template = {'mappings': msgs['mappings']} try: expand_template(template_file, data_for_template, output_file) except RuntimeError as e: print(str(e), file=sys.stderr) template_file = os.path.join(template_dir, 'csv.hpp.em') output_file = os.path.join(output_path, 'csv.hpp') data_for_template = {'mappings': msgs['mappings']} try: expand_template(template_file, data_for_template, output_file) except RuntimeError as e: print(str(e), file=sys.stderr) template_file = os.path.join(template_dir, 'csv.cpp.em') output_file = os.path.join(output_path, 'csv.cpp') data_for_template = {'mappings': msgs['mappings']} try: expand_template(template_file, data_for_template, output_file) except RuntimeError as e: print(str(e), file=sys.stderr) template_file = os.path.join(template_dir, 'ros2_msgs.hpp.em') output_file = os.path.join(output_path, 'ros2_msgs.hpp') data_for_template = {'mappings': msgs['mappings']} try: expand_template(template_file, data_for_template, output_file) except RuntimeError as e: print(str(e), file=sys.stderr)
def generate_typesupport_opensplice_cpp(args): template_dir = args['template_dir'] mapping_msgs = { os.path.join(template_dir, 'msg__rosidl_typesupport_opensplice_cpp.hpp.em'): '%s__rosidl_typesupport_opensplice_cpp.hpp', os.path.join(template_dir, 'msg__type_support.cpp.em'): '%s__type_support.cpp', } mapping_srvs = { os.path.join(template_dir, 'srv__rosidl_typesupport_opensplice_cpp.hpp.em'): '%s__rosidl_typesupport_opensplice_cpp.hpp', os.path.join(template_dir, 'srv__type_support.cpp.em'): '%s__type_support.cpp', } for template_file in mapping_msgs.keys(): assert os.path.exists(template_file), 'Could not find template: ' + template_file for template_file in mapping_srvs.keys(): assert os.path.exists(template_file), 'Could not find template: ' + template_file pkg_name = args['package_name'] known_msg_types = extract_message_types( pkg_name, args['ros_interface_files'], args.get('ros_interface_dependencies', [])) functions = { 'get_header_filename_from_msg_name': convert_camel_case_to_lower_case_underscore, } # generate_dds_opensplice_cpp() and therefore the make target depend on the additional files # therefore they must be listed here even if the generated type support files are independent latest_target_timestamp = get_newest_modification_time( args['target_dependencies'] + args.get('additional_files', [])) for idl_file in args['ros_interface_files']: extension = os.path.splitext(idl_file)[1] if extension == '.msg': spec = parse_message_file(pkg_name, idl_file) validate_field_types(spec, known_msg_types) subfolder = os.path.basename(os.path.dirname(idl_file)) for template_file, generated_filename in mapping_msgs.items(): generated_file = os.path.join(args['output_dir'], subfolder) if generated_filename.endswith('.cpp'): generated_file = os.path.join(generated_file, 'dds_opensplice') generated_file = os.path.join( generated_file, generated_filename % convert_camel_case_to_lower_case_underscore(spec.base_type.type)) data = {'spec': spec, 'subfolder': subfolder} data.update(functions) expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) elif extension == '.srv': spec = parse_service_file(pkg_name, idl_file) validate_field_types(spec, known_msg_types) for template_file, generated_filename in mapping_srvs.items(): generated_file = os.path.join(args['output_dir'], 'srv') if generated_filename.endswith('.cpp'): generated_file = os.path.join(generated_file, 'dds_opensplice') generated_file = os.path.join( generated_file, generated_filename % convert_camel_case_to_lower_case_underscore(spec.srv_name)) data = {'spec': spec} data.update(functions) expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) return 0
def generate_cpp(generator_arguments_file, type_supports): args = read_generator_arguments(generator_arguments_file) template_dir = args['template_dir'] mapping_msgs = { os.path.join(template_dir, 'msg__type_support.cpp.em'): '%s__type_support.cpp', } mapping_srvs = { os.path.join(template_dir, 'srv__type_support.cpp.em'): '%s__type_support.cpp', } mapping_actions = { os.path.join(template_dir, 'action__type_support.cpp.em'): '%s__type_support.cpp', } for template_file in mapping_msgs.keys(): assert os.path.exists( template_file), 'Could not find template: ' + template_file for template_file in mapping_srvs.keys(): assert os.path.exists( template_file), 'Could not find template: ' + template_file for template_file in mapping_actions.keys(): assert os.path.exists( template_file), 'Could not find template: ' + template_file pkg_name = args['package_name'] known_msg_types = extract_message_types( pkg_name, args['ros_interface_files'], args.get('ros_interface_dependencies', [])) functions = { 'get_header_filename_from_msg_name': convert_camel_case_to_lower_case_underscore, } latest_target_timestamp = get_newest_modification_time( args['target_dependencies']) for ros_interface_file in args['ros_interface_files']: extension = os.path.splitext(ros_interface_file)[1] subfolder = os.path.basename(os.path.dirname(ros_interface_file)) if extension == '.msg': spec = parse_message_file(pkg_name, ros_interface_file) validate_field_types(spec, known_msg_types) for template_file, generated_filename in mapping_msgs.items(): generated_file = os.path.join( args['output_dir'], subfolder, generated_filename % convert_camel_case_to_lower_case_underscore( spec.base_type.type)) data = { 'spec': spec, 'subfolder': subfolder, 'type_supports': type_supports } data.update(functions) expand_template(template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) elif extension == '.srv': spec = parse_service_file(pkg_name, ros_interface_file) validate_field_types(spec, known_msg_types) for template_file, generated_filename in mapping_srvs.items(): generated_file = os.path.join( args['output_dir'], subfolder, generated_filename % convert_camel_case_to_lower_case_underscore(spec.srv_name)) data = { 'spec': spec, 'subfolder': subfolder, 'type_supports': type_supports } data.update(functions) expand_template(template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) elif extension == '.action': spec = parse_action_file(pkg_name, ros_interface_file) # TODO(sloretz) validate field types when overall action is generated with msg and srv # https://github.com/ros2/rosidl/issues/348#issuecomment-462874513 # validate_field_types(spec, known_msg_types) for template_file, generated_filename in mapping_actions.items(): generated_file = os.path.join( args['output_dir'], subfolder, generated_filename % convert_camel_case_to_lower_case_underscore( spec.action_name)) data = {'spec': spec, 'subfolder': subfolder} data.update(functions) expand_template(template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) return 0