Skip to content

Commit

Permalink
Introduce Jinja2 for templating
Browse files Browse the repository at this point in the history
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 olofk#166
  • Loading branch information
imphil authored and olofk committed May 1, 2018
1 parent 4d12e82 commit 74e96a2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 19 deletions.
63 changes: 44 additions & 19 deletions fusesoc/edatool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@ def read(fname):
'pytest>=3.3.0',
'pyyaml',
'simplesat>=0.8.0',
'Jinja2>=2.8',
],
)

0 comments on commit 74e96a2

Please sign in to comment.