Rupert Swarbrick | a23dfec | 2020-09-07 10:01:28 +0100 | [diff] [blame^] | 1 | # Copyright lowRISC contributors. |
| 2 | # Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | |
| 5 | import logging as log |
| 6 | import sys |
| 7 | |
| 8 | from CfgJson import load_hjson |
| 9 | |
| 10 | import FpvCfg |
| 11 | import LintCfg |
| 12 | import SimCfg |
| 13 | import SynCfg |
| 14 | |
| 15 | |
| 16 | def _load_cfg(path, initial_values): |
| 17 | '''Worker function for make_cfg. |
| 18 | |
| 19 | initial_values is passed to load_hjson (see documentation there). |
| 20 | |
| 21 | Returns a pair (cls, hjson_data) on success or raises a RuntimeError on |
| 22 | failure. |
| 23 | |
| 24 | ''' |
| 25 | # Start by loading up the hjson file and any included files |
| 26 | hjson_data = load_hjson(path, initial_values) |
| 27 | |
| 28 | # Look up the value of flow in the loaded data. This is a required field, |
| 29 | # and tells us what sort of FlowCfg to make. |
| 30 | flow = hjson_data.get('flow') |
| 31 | if flow is None: |
| 32 | raise RuntimeError('{!r}: No value for the "flow" key. Are you sure ' |
| 33 | 'this is a dvsim configuration file?' |
| 34 | .format(path)) |
| 35 | |
| 36 | classes = [ |
| 37 | LintCfg.LintCfg, |
| 38 | SynCfg.SynCfg, |
| 39 | FpvCfg.FpvCfg, |
| 40 | SimCfg.SimCfg |
| 41 | ] |
| 42 | found_cls = None |
| 43 | known_types = [] |
| 44 | for cls in classes: |
| 45 | assert cls.flow is not None |
| 46 | known_types.append(cls.flow) |
| 47 | if cls.flow == flow: |
| 48 | found_cls = cls |
| 49 | break |
| 50 | if found_cls is None: |
| 51 | raise RuntimeError('{}: Configuration file sets "flow" to {!r}, but ' |
| 52 | 'this is not a known flow (known: {}).' |
| 53 | .format(path, flow, ', '.join(known_types))) |
| 54 | |
| 55 | return (found_cls, hjson_data) |
| 56 | |
| 57 | |
| 58 | def _make_child_cfg(path, args, initial_values): |
| 59 | try: |
| 60 | cls, hjson_data = _load_cfg(path, initial_values) |
| 61 | except RuntimeError as err: |
| 62 | log.error(str(err)) |
| 63 | sys.exit(1) |
| 64 | |
| 65 | # Since this is a child configuration (from some primary configuration), |
| 66 | # make sure that we aren't ourselves a primary configuration. We don't need |
| 67 | # multi-level hierarchies and this avoids circular dependencies. |
| 68 | if 'use_cfgs' in hjson_data: |
| 69 | raise RuntimeError('{}: Configuration file has use_cfgs, but is ' |
| 70 | 'itself included from another configuration.' |
| 71 | .format(path)) |
| 72 | |
| 73 | # Call cls as a constructor. Note that we pass None as the mk_config |
| 74 | # argument: this is not supposed to load anything else. |
| 75 | return cls(path, hjson_data, args, None) |
| 76 | |
| 77 | |
| 78 | def make_cfg(path, args, proj_root): |
| 79 | '''Make a flow config by loading the config file at path |
| 80 | |
| 81 | args is the arguments passed to the dvsim.py tool and proj_root is the top |
| 82 | of the project. |
| 83 | |
| 84 | ''' |
| 85 | initial_values = {'proj_root': proj_root} |
| 86 | if args.tool is not None: |
| 87 | initial_values['tool'] = args.tool |
| 88 | |
| 89 | try: |
| 90 | cls, hjson_data = _load_cfg(path, initial_values) |
| 91 | except RuntimeError as err: |
| 92 | log.error(str(err)) |
| 93 | sys.exit(1) |
| 94 | |
| 95 | def factory(child_path): |
| 96 | child_ivs = initial_values.copy() |
| 97 | child_ivs['flow'] = hjson_data['flow'] |
| 98 | return _make_child_cfg(child_path, args, child_ivs) |
| 99 | |
| 100 | return cls(path, hjson_data, args, factory) |