Skip to content

Commit

Permalink
inject BOM into generated C/C++ code when necessary
Browse files Browse the repository at this point in the history
Signed-off-by: Dirk Thomas <[email protected]>
  • Loading branch information
dirk-thomas committed Jul 25, 2019
1 parent d5e21dc commit 18f022b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
13 changes: 10 additions & 3 deletions rosidl_cmake/rosidl_cmake/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ def get_newest_modification_time(target_dependencies):
return newest_timestamp


def generate_files(generator_arguments_file, mapping, additional_context=None, keep_case=False):
def generate_files(
generator_arguments_file, mapping, additional_context=None,
keep_case=False, post_process_callback=None
):
args = read_generator_arguments(generator_arguments_file)

template_basepath = pathlib.Path(args['template_dir'])
Expand Down Expand Up @@ -82,7 +85,8 @@ def generate_files(generator_arguments_file, mapping, additional_context=None, k
expand_template(
os.path.basename(template_file), data,
generated_file, minimum_timestamp=latest_target_timestamp,
template_basepath=template_basepath)
template_basepath=template_basepath,
post_process_callback=post_process_callback)
except Exception as e:
print(
'Error processing idl file: ' +
Expand Down Expand Up @@ -110,7 +114,7 @@ def get_template_path(template_name):

def expand_template(
template_name, data, output_file, minimum_timestamp=None,
template_basepath=None
template_basepath=None, post_process_callback=None
):
# in the legacy API the first argument was the path to the template
if template_basepath is None:
Expand Down Expand Up @@ -155,6 +159,9 @@ def expand_template(
content = output.getvalue()
interpreter.shutdown()

if post_process_callback:
content = post_process_callback(content)

# only overwrite file if necessary
# which is either when the timestamp is too old or when the content is different
if os.path.exists(output_file):
Expand Down
15 changes: 14 additions & 1 deletion rosidl_generator_c/rosidl_generator_c/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,20 @@ def generate_c(generator_arguments_file):
'idl__struct.h.em': '%s__struct.h',
'idl__type_support.h.em': '%s__type_support.h',
}
generate_files(generator_arguments_file, mapping)
generate_files(
generator_arguments_file, mapping,
post_process_callback=prefix_with_bom_if_necessary)


def prefix_with_bom_if_necessary(content):
try:
content.encode('ASCII')
except UnicodeError:
prefix = '\ufeff' + \
'// NOLINT: This file starts with a BOM ' + \
'since it contain non-ASCII characters\n'
content = prefix + content
return content


BASIC_IDL_TYPES_TO_C = {
Expand Down
15 changes: 14 additions & 1 deletion rosidl_generator_cpp/rosidl_generator_cpp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,20 @@ def generate_cpp(generator_arguments_file):
'idl__struct.hpp.em': '%s__struct.hpp',
'idl__traits.hpp.em': '%s__traits.hpp',
}
generate_files(generator_arguments_file, mapping)
generate_files(
generator_arguments_file, mapping,
post_process_callback=prefix_with_bom_if_necessary)


def prefix_with_bom_if_necessary(content):
try:
content.encode('ASCII')
except UnicodeError:
prefix = '\ufeff' + \
'// NOLINT: This file starts with a BOM ' + \
'since it contain non-ASCII characters\n'
content = prefix + content
return content


MSG_TYPE_TO_CPP = {
Expand Down

0 comments on commit 18f022b

Please sign in to comment.