Skip to content

Commit

Permalink
Use OrderedDict for reproducible codegen
Browse files Browse the repository at this point in the history
Python set's do not have a deterministic iteration order (unlike dicts),
therefore replace usage of set with OrderedDict's to make codegen
reproducible.

Signed-off-by: Harry Sarson <harry.sarson@codethink.co.uk>
  • Loading branch information
harrysarson committed Nov 27, 2024
1 parent dd05300 commit 1c760e0
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion rosidl_generator_c/resource/action__type_support.h.em
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// already included above
// @
@[else]@
@{include_directives.add(header_file)}@
@{include_directives[header_file] = None}@
@[end if]@
#include "@(header_file)"

Expand Down
5 changes: 3 additions & 2 deletions rosidl_generator_c/resource/full__description.c.em
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@# Included from rosidl_generator_c/resource/idl__description.c.em
@{
from collections import OrderedDict
from rosidl_generator_c import escape_string
from rosidl_generator_c import idl_structure_type_to_c_include_prefix
from rosidl_generator_c import type_hash_to_c_definition
Expand Down Expand Up @@ -31,7 +32,7 @@ def utf8_encode(value_string):
return escape_string(repr(value_string.encode('utf-8'))[2:-1])

implicit_type_names = set(td['type_description']['type_name'] for td, _ in implicit_type_descriptions)
includes = set()
includes = OrderedDict()
toplevel_msg, _ = toplevel_type_description

for referenced_td in toplevel_msg['referenced_type_descriptions']:
Expand All @@ -40,7 +41,7 @@ for referenced_td in toplevel_msg['referenced_type_descriptions']:
names = referenced_td['type_name'].split('/')
_type = NamespacedType(names[:-1], names[-1])
include_prefix = idl_structure_type_to_c_include_prefix(_type, 'detail')
includes.add(include_prefix + '__functions.h')
includes[include_prefix + '__functions.h'] = None

full_type_descriptions = [toplevel_type_description] + implicit_type_descriptions
full_type_names = [t['type_description']['type_name'] for t, _ in full_type_descriptions]
Expand Down
3 changes: 2 additions & 1 deletion rosidl_generator_c/resource/idl__functions.c.em
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
@# - content (IdlContent, list of elements, e.g. Messages or Services)
@#######################################################################
@{
from collections import OrderedDict
from rosidl_pycommon import convert_camel_case_to_lower_case_underscore
include_parts = [package_name] + list(interface_path.parents[0].parts) + [
'detail', convert_camel_case_to_lower_case_underscore(interface_path.stem)]
include_base = '/'.join(include_parts)

include_directives = {include_base + '__functions.h', 'rcutils/allocator.h'}
include_directives = OrderedDict.fromkeys([include_base + '__functions.h', 'rcutils/allocator.h'])
}@
#include "@(include_base)__functions.h"

Expand Down
3 changes: 2 additions & 1 deletion rosidl_generator_c/resource/idl__struct.h.em
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
@# - content (IdlContent, list of elements, e.g. Messages or Services)
@#######################################################################
@{
from collections import OrderedDict
from rosidl_generator_c import idl_structure_type_to_c_typename
from rosidl_pycommon import convert_camel_case_to_lower_case_underscore
include_parts = [package_name] + list(interface_path.parents[0].parts) + [
'detail', convert_camel_case_to_lower_case_underscore(interface_path.stem)]
header_guard_variable = '__'.join([x.upper() for x in include_parts]) + \
'__STRUCT_H_'
include_directives = set()
include_directives = OrderedDict()
}@
#ifndef @(header_guard_variable)
Expand Down
5 changes: 3 additions & 2 deletions rosidl_generator_c/resource/idl__type_support.c.em
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@
@# - content (IdlContent, list of elements, e.g. Messages or Services)
@#######################################################################
@{
from collections import OrderedDict
from rosidl_pycommon import convert_camel_case_to_lower_case_underscore
include_parts = [package_name] + list(interface_path.parents[0].parts) + [
'detail', convert_camel_case_to_lower_case_underscore(interface_path.stem)]
include_base = '/'.join(include_parts)

include_directives = {
include_directives = OrderedDict.fromkeys([
'rosidl_typesupport_interface/macros.h',
include_base + '__type_support.h',
include_base + '__struct.h',
include_base + '__functions.h'}
include_base + '__functions.h'])
}@

#include <string.h>
Expand Down
6 changes: 4 additions & 2 deletions rosidl_generator_c/resource/idl__type_support.h.em
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// generated from rosidl_generator_c/resource/idl__type_support.h.em
// with input from @(package_name):@(interface_path)
// generated code does not contain a copyright notice
@{from rosidl_pycommon import convert_camel_case_to_lower_case_underscore}
@{
from collections import OrderedDict
from rosidl_pycommon import convert_camel_case_to_lower_case_underscore}
// IWYU pragma: private, include "@(package_name)/@(interface_path.parent)/@(convert_camel_case_to_lower_case_underscore(interface_path.stem)).h"
@
Expand All @@ -20,7 +22,7 @@ include_parts = [package_name] + list(interface_path.parents[0].parts) + [
header_guard_variable = '__'.join([x.upper() for x in include_parts]) + \
'__TYPE_SUPPORT_H_'
include_directives = set()
include_directives = OrderedDict()
}@
#ifndef @(header_guard_variable)
Expand Down
2 changes: 1 addition & 1 deletion rosidl_generator_c/resource/msg__functions.c.em
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ for member in message.structure.members:
// already included above
// @
@[ else]@
@{include_directives.add(header_file)}@
@{include_directives[header_file] = None}@
@[ end if]@
#include "@(header_file)"
@[ end for]@
Expand Down
2 changes: 1 addition & 1 deletion rosidl_generator_c/resource/msg__struct.h.em
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ static const char * const @(idl_structure_type_to_c_typename(message.structure.n
// already included above
// @
@[ else]@
@{include_directives.add(header_file)}@
@{include_directives[header_file] = None}@
@[ end if]@
#include "@(header_file)"
@[ end for]@
Expand Down
2 changes: 1 addition & 1 deletion rosidl_generator_c/resource/msg__type_support.h.em
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// already included above
// @
@[else]@
@{include_directives.add(header_file)}@
@{include_directives[header_file] = None}@
@[end if]@
#include "@(header_file)"

Expand Down
2 changes: 1 addition & 1 deletion rosidl_generator_c/resource/srv__type_support.h.em
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ TEMPLATE(
// already included above
// @
@[else]@
@{include_directives.add(header_file)}@
@{include_directives[header_file] = None}@
@[end if]@
#include "@(header_file)"

Expand Down

0 comments on commit 1c760e0

Please sign in to comment.