mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-11-03 02:51:36 +08:00
Libuavcan DSDL compiler: Speed optimization - full generation takes less than a second (way faster than Mako)
This commit is contained in:
@@ -95,12 +95,13 @@ def run_parser(source_dirs, search_dirs):
|
|||||||
|
|
||||||
def run_generator(types, dest_dir):
|
def run_generator(types, dest_dir):
|
||||||
try:
|
try:
|
||||||
|
template_expander = make_template_expander(TEMPLATE_FILENAME)
|
||||||
dest_dir = os.path.abspath(dest_dir) # Removing '..'
|
dest_dir = os.path.abspath(dest_dir) # Removing '..'
|
||||||
makedirs(dest_dir)
|
makedirs(dest_dir)
|
||||||
for t in types:
|
for t in types:
|
||||||
logger.info('Generating type %s', t.full_name)
|
logger.info('Generating type %s', t.full_name)
|
||||||
filename = os.path.join(dest_dir, type_output_filename(t))
|
filename = os.path.join(dest_dir, type_output_filename(t))
|
||||||
text = generate_one_type(t)
|
text = generate_one_type(template_expander, t)
|
||||||
write_generated_data(filename, text)
|
write_generated_data(filename, text)
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
logger.info('Generator failure', exc_info=True)
|
logger.info('Generator failure', exc_info=True)
|
||||||
@@ -157,7 +158,7 @@ def type_to_cpp_type(t):
|
|||||||
else:
|
else:
|
||||||
raise DsdlCompilerException('Unknown type category: %s' % t.category)
|
raise DsdlCompilerException('Unknown type category: %s' % t.category)
|
||||||
|
|
||||||
def generate_one_type(t):
|
def generate_one_type(template_expander, t):
|
||||||
t.short_name = t.full_name.split('.')[-1]
|
t.short_name = t.full_name.split('.')[-1]
|
||||||
t.cpp_type_name = t.short_name + '_'
|
t.cpp_type_name = t.short_name + '_'
|
||||||
t.cpp_full_type_name = '::' + t.full_name.replace('.', '::')
|
t.cpp_full_type_name = '::' + t.full_name.replace('.', '::')
|
||||||
@@ -220,13 +221,13 @@ def generate_one_type(t):
|
|||||||
}[t.kind]
|
}[t.kind]
|
||||||
|
|
||||||
# Generation
|
# Generation
|
||||||
text = expand_template(t=t) # t for Type
|
text = template_expander(t=t) # t for Type
|
||||||
text = '\n'.join(x.rstrip() for x in text.splitlines())
|
text = '\n'.join(x.rstrip() for x in text.splitlines())
|
||||||
text = text.replace('\n\n\n\n\n', '\n\n').replace('\n\n\n\n', '\n\n').replace('\n\n\n', '\n\n')
|
text = text.replace('\n\n\n\n\n', '\n\n').replace('\n\n\n\n', '\n\n').replace('\n\n\n', '\n\n')
|
||||||
text = text.replace('{\n\n ', '{\n ')
|
text = text.replace('{\n\n ', '{\n ')
|
||||||
return text
|
return text
|
||||||
|
|
||||||
def expand_template(**args):
|
def make_template_expander(filename):
|
||||||
'''
|
'''
|
||||||
Templating is based on pyratemp (http://www.simple-is-better.org/template/pyratemp.html).
|
Templating is based on pyratemp (http://www.simple-is-better.org/template/pyratemp.html).
|
||||||
The pyratemp's syntax is rather verbose and not so human friendly, so we define some
|
The pyratemp's syntax is rather verbose and not so human friendly, so we define some
|
||||||
@@ -245,7 +246,7 @@ def expand_template(**args):
|
|||||||
% endfor
|
% endfor
|
||||||
The extended syntax is converted into pyratemp's through regexp substitution.
|
The extended syntax is converted into pyratemp's through regexp substitution.
|
||||||
'''
|
'''
|
||||||
with open(TEMPLATE_FILENAME) as f:
|
with open(filename) as f:
|
||||||
template_text = f.read()
|
template_text = f.read()
|
||||||
|
|
||||||
# Backslash-newline elimination
|
# Backslash-newline elimination
|
||||||
@@ -269,11 +270,14 @@ def expand_template(**args):
|
|||||||
template_text = re.sub(r'(\<\!--\(macro\ [a-zA-Z0-9_]+\)--\>.*?)', r'\1\n', template_text)
|
template_text = re.sub(r'(\<\!--\(macro\ [a-zA-Z0-9_]+\)--\>.*?)', r'\1\n', template_text)
|
||||||
|
|
||||||
# Preprocessed text output for debugging
|
# Preprocessed text output for debugging
|
||||||
# with open(TEMPLATE_FILENAME + '.d', 'w') as f:
|
# with open(filename + '.d', 'w') as f:
|
||||||
# f.write(template_text)
|
# f.write(template_text)
|
||||||
|
|
||||||
# This function adds one indentation level (4 spaces); it will be used from the template
|
template = Template(template_text)
|
||||||
args['indent'] = lambda text, idnt = ' ': idnt + text.replace('\n', '\n' + idnt)
|
|
||||||
|
|
||||||
t = Template(template_text)
|
def expand(**args):
|
||||||
return t(**args)
|
# This function adds one indentation level (4 spaces); it will be used from the template
|
||||||
|
args['indent'] = lambda text, idnt = ' ': idnt + text.replace('\n', '\n' + idnt)
|
||||||
|
return template(**args)
|
||||||
|
|
||||||
|
return expand
|
||||||
|
|||||||
Reference in New Issue
Block a user