Skip to content

Commit b6625fd

Browse files
committed
Fix CMake Unity Build variable clashes in the generated C code
Without this fix, compiler complains about the same variable defined in different generated files. To test: enable unity build with -DCMAKE_UNITY_BUILD=ON when configuring the project that uses generated rosidl c files. Signed-off-by: Alexander Borsuk <[email protected]>
1 parent 1d92ce3 commit b6625fd

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

rosidl_generator_c/resource/full__description.c.em

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ type_name = referenced_type_description['type_name']
6969
c_typename = type_name.replace('/', '__')
7070
}@
7171
@[ if type_name not in full_type_names]@
72-
static const rosidl_type_hash_t @(c_typename)__EXPECTED_HASH = @(type_hash_to_c_definition(hash_lookup[type_name]));
72+
static const rosidl_type_hash_t @(unique_prefix)_@(c_typename)__EXPECTED_HASH = @(type_hash_to_c_definition(hash_lookup[type_name]));
7373
@[ end if]@
7474
@[end for]@
7575
#endif
@@ -78,7 +78,7 @@ static const rosidl_type_hash_t @(c_typename)__EXPECTED_HASH = @(type_hash_to_c_
7878
@#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
7979
@# Names for all types
8080
@[for itype_description in all_type_descriptions]@
81-
static char @(typename_to_c(itype_description['type_name']))__TYPE_NAME[] = "@(itype_description['type_name'])";
81+
static char @(unique_prefix)_@(typename_to_c(itype_description['type_name']))__TYPE_NAME[] = "@(itype_description['type_name'])";
8282
@[end for]@
8383
@#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
8484

@@ -111,7 +111,7 @@ static rosidl_runtime_c__type_description__Field @(td_c_typename)__FIELDS[] = {
111111
rosidl_runtime_c__type_description__FieldType__@(FIELD_TYPE_ID_TO_NAME[field['type']['type_id']]),
112112
@(field['type']['capacity']),
113113
@(field['type']['string_capacity']),
114-
@(static_seq(f"{typename_to_c(field['type']['nested_type_name'])}__TYPE_NAME", field['type']['nested_type_name'])),
114+
@(static_seq(f"{unique_prefix}_{typename_to_c(field['type']['nested_type_name'])}__TYPE_NAME", field['type']['nested_type_name'])),
115115
},
116116
@(static_seq(f"{td_c_typename}__DEFAULT_VALUE__{field['name']}", field['default_value'])),
117117
},
@@ -124,7 +124,7 @@ static rosidl_runtime_c__type_description__Field @(td_c_typename)__FIELDS[] = {
124124
static rosidl_runtime_c__type_description__IndividualTypeDescription @(td_c_typename)__REFERENCED_TYPE_DESCRIPTIONS[] = {
125125
@[ for ref_td in ref_tds]@
126126
{
127-
@(static_seq(f"{typename_to_c(ref_td['type_name'])}__TYPE_NAME", ref_td['type_name'])),
127+
@(static_seq(f"{unique_prefix}_{typename_to_c(ref_td['type_name'])}__TYPE_NAME", ref_td['type_name'])),
128128
{NULL, 0, 0},
129129
},
130130
@[ end for]@
@@ -139,7 +139,7 @@ const rosidl_runtime_c__type_description__TypeDescription *
139139
static bool constructed = false;
140140
static const rosidl_runtime_c__type_description__TypeDescription description = {
141141
{
142-
@(static_seq(f'{td_c_typename}__TYPE_NAME', td_typename)),
142+
@(static_seq(f'{unique_prefix}_{td_c_typename}__TYPE_NAME', td_typename)),
143143
@(static_seq(f'{td_c_typename}__FIELDS', msg['type_description']['fields'])),
144144
},
145145
@(static_seq(f'{td_c_typename}__REFERENCED_TYPE_DESCRIPTIONS', ref_tds)),
@@ -150,7 +150,7 @@ const rosidl_runtime_c__type_description__TypeDescription *
150150
c_typename = typename_to_c(ref_td['type_name'])
151151
}@
152152
@[ if ref_td['type_name'] not in full_type_names]@
153-
assert(0 == memcmp(&@(c_typename)__EXPECTED_HASH, @(c_typename)__@(GET_HASH_FUNC)(NULL), sizeof(rosidl_type_hash_t)));
153+
assert(0 == memcmp(&@(unique_prefix)_@(c_typename)__EXPECTED_HASH, @(c_typename)__@(GET_HASH_FUNC)(NULL), sizeof(rosidl_type_hash_t)));
154154
@[ end if]@
155155
description.referenced_type_descriptions.data[@(idx)].fields = @(c_typename)__@(GET_DESCRIPTION_FUNC)(NULL)->type_description.fields;
156156
@[ end for]@
@@ -164,16 +164,16 @@ c_typename = typename_to_c(ref_td['type_name'])
164164
@#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
165165
@# Define individual raw sources
166166
@[if raw_source_content]@
167-
static char toplevel_type_raw_source[] =@
167+
static char @(unique_prefix)_toplevel_type_raw_source[] =@
168168
@[ for line in raw_source_content.splitlines()[:-1]]
169169
"@(utf8_encode(line))\n"@
170170
@[ end for]
171171
"@(utf8_encode(raw_source_content.splitlines()[-1]))";
172172
@[end if]@
173173

174-
static char @(toplevel_encoding)_encoding[] = "@(toplevel_encoding)";
174+
static char @(unique_prefix)_@(toplevel_encoding)_encoding[] = "@(toplevel_encoding)";
175175
@[if implicit_type_descriptions]@
176-
static char implicit_encoding[] = "implicit";
176+
static char @(unique_prefix)_implicit_encoding[] = "implicit";
177177
@[end if]@
178178

179179
// Define all individual source functions
@@ -188,7 +188,7 @@ if td_typename in implicit_type_names:
188188
contents = None
189189
else:
190190
encoding = toplevel_encoding
191-
contents_var = 'toplevel_type_raw_source'
191+
contents_var = unique_prefix + '_toplevel_type_raw_source'
192192
contents = raw_source_content
193193
}@
194194

@@ -198,8 +198,8 @@ const rosidl_runtime_c__type_description__TypeSource *
198198
{
199199
(void)type_support;
200200
static const rosidl_runtime_c__type_description__TypeSource source = {
201-
@(static_seq(f'{td_c_typename}__TYPE_NAME', td_typename)),
202-
@(static_seq(f'{encoding}_encoding', encoding)),
201+
@(static_seq(f'{unique_prefix}_{td_c_typename}__TYPE_NAME', td_typename)),
202+
@(static_seq(f'{unique_prefix}_{encoding}_encoding', encoding)),
203203
@(static_seq(contents_var, contents)),
204204
};
205205
return &source;

rosidl_generator_c/resource/idl__description.c.em

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ TEMPLATE(
101101
toplevel_type_description=toplevel_type_description,
102102
implicit_type_descriptions=implicit_type_descriptions,
103103
hash_lookup=hash_lookup,
104-
type_source_file=type_source_file)
104+
type_source_file=type_source_file,
105+
unique_prefix=interface_path.stem)
105106
}@
106107
@[end if]@
107108
@#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

0 commit comments

Comments
 (0)