From 74e96a22850daf402525a7c3cfb92cf685e18042 Mon Sep 17 00:00:00 2001 From: Philipp Wagner Date: Tue, 22 Aug 2017 15:32:25 +0200 Subject: [PATCH] Introduce Jinja2 for templating Currently many backends have templates for the files they write out (project files, TCL files, Makefiles, etc) included in the Python source code of the backend. Other backends concatenate strings to create the resulting output files. This commit introduces Jinja2 to fusesoc, making it possible to use Jinja2 templates instead. No backends are converted in this commit, this will follow later. Fixes #166 --- fusesoc/edatool.py | 63 ++++++++++++++++++++++++++++++++-------------- setup.py | 1 + 2 files changed, 45 insertions(+), 19 deletions(-) diff --git a/fusesoc/edatool.py b/fusesoc/edatool.py index 38d181ab..b9da6d49 100644 --- a/fusesoc/edatool.py +++ b/fusesoc/edatool.py @@ -5,10 +5,42 @@ import logging import sys import yaml +from jinja2 import Environment, PackageLoader from fusesoc.utils import Launcher logger = logging.getLogger(__name__) +# Jinja2 tests and filters, available in all templates +def jinja_is_verilog_file(f): + return f.file_type.startswith('verilogSource') + +def jinja_is_system_verilog_file(f): + return f.file_type.startswith('systemVerilogSource') + +def jinja_is_vhdl_file(f): + return f.file_type.startswith('vhdlSource') + +def jinja_filter_param_value_str(value, str_quote_style=""): + """ Convert a parameter value to string suitable to be passed to an EDA tool + + Rules: + - Booleans are represented as 0/1 + - Strings are either passed through or enclosed in the characters specified + in str_quote_style (e.g. '"' or '\\"') + - Everything else (including int, float, etc.) are converted using the str() + function. + """ + if type(value) == bool: + if (value) == True: + return '1' + else: + return '0' + elif type(value) == str: + return str_quote_style + str(value) + str_quote_style + else: + return str(value) + + class FileAction(argparse.Action): def __call__(self, parser, namespace, values, option_string=None): path = os.path.expandvars(values[0]) @@ -75,6 +107,17 @@ def __init__(self, eda_api_file, work_root=None): self.cmdlinearg = OrderedDict() self.parsed_args = False + self.jinja_env = Environment( + loader = PackageLoader(__package__, 'templates'), + trim_blocks = True, + lstrip_blocks = True, + ) + self.jinja_env.tests['verilog_file'] = jinja_is_verilog_file + self.jinja_env.tests['system_verilog_file'] = jinja_is_system_verilog_file + self.jinja_env.tests['vhdl_file'] = jinja_is_vhdl_file + self.jinja_env.filters['param_value_str'] = jinja_filter_param_value_str + + def configure(self, args): logger.info("Setting up project") self.configure_pre(args) @@ -212,26 +255,8 @@ def __init__(self, name, file_type, logical_name): logical_name)) return (src_files, incdirs) - """ Convert a parameter value to string suitable to be passed to an EDA tool - - Rules: - - Booleans are represented as 0/1 - - Strings are either passed through or enclosed in the characters specified - in str_quote_style (e.g. '"' or '\\"') - - Everything else (including int, float, etc.) are converted using the str() - function. - """ def _param_value_str(self, param_value, str_quote_style=""): - - if type(param_value) == bool: - if (param_value) == True: - return '1' - else: - return '0' - elif type(param_value) == str: - return str_quote_style+str(param_value)+str_quote_style - else: - return str(param_value) + return jinja_filter_param_value_str(param_value, str_quote_style) def _run_scripts(self, scripts): for script in scripts: diff --git a/setup.py b/setup.py index 685cad45..3e76a2bb 100644 --- a/setup.py +++ b/setup.py @@ -42,5 +42,6 @@ def read(fname): 'pytest>=3.3.0', 'pyyaml', 'simplesat>=0.8.0', + 'Jinja2>=2.8', ], )