Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 1 | #------------------------------------------------------------------------------- |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 2 | # Copyright (c) 2018-2019, Arm Limited. All rights reserved. |
Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 3 | # |
| 4 | # SPDX-License-Identifier: BSD-3-Clause |
| 5 | # |
| 6 | #------------------------------------------------------------------------------- |
| 7 | |
| 8 | import os |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 9 | import io |
| 10 | from jinja2 import Environment, BaseLoader, select_autoescape |
Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 11 | |
| 12 | try: |
| 13 | import yaml |
| 14 | except ImportError as e: |
Mate Toth-Pal | a99ec6b | 2019-05-07 11:00:56 +0200 | [diff] [blame] | 15 | print (str(e) + " To install it, type:") |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 16 | print ("pip install PyYAML") |
Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 17 | exit(1) |
| 18 | |
Edison Ai | 48b2d9e | 2019-06-24 14:39:45 +0800 | [diff] [blame^] | 19 | donotedit_warning = \ |
| 20 | "/*********** " + \ |
| 21 | "WARNING: This is an auto-generated file. Do not edit!" + \ |
| 22 | " ***********/" |
| 23 | manifest_list_yaml_file_path = os.path.join('tools', 'tfm_manifest_list.yaml') |
| 24 | |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 25 | class TemplateLoader(BaseLoader): |
| 26 | """ |
| 27 | Template loader class. |
Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 28 | |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 29 | An instance of this class is passed to the template engine. It is |
| 30 | responsible for reading the template file |
| 31 | """ |
| 32 | def __init__(self): |
| 33 | pass |
Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 34 | |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 35 | def get_source(self, environment, template): |
| 36 | """ |
| 37 | This function reads the template files. |
| 38 | For detailed documentation see: |
| 39 | http://jinja.pocoo.org/docs/2.10/api/#jinja2.BaseLoader.get_source |
| 40 | |
| 41 | Please note that this function always return 'false' as 'uptodate' |
| 42 | value, so the output file will always be generated. |
| 43 | """ |
| 44 | if not os.path.isfile(template): |
| 45 | raise TemplateNotFound(template) |
| 46 | with open(template) as f: |
| 47 | source = f.read() |
| 48 | return source, template, False |
| 49 | |
Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 50 | def load_manifest_list(file): |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 51 | """ |
| 52 | Load the substitution data from the manifests. |
| 53 | |
| 54 | Parameters |
| 55 | ---------- |
| 56 | file : file |
| 57 | A yaml file containing the manifest list |
| 58 | |
| 59 | Returns |
| 60 | ------- |
| 61 | list |
| 62 | The list of the contents of the manifest files, as generated by the yaml |
| 63 | parser. |
| 64 | """ |
Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 65 | db = [] |
| 66 | manifest_list = yaml.load(file) |
| 67 | for item in manifest_list["manifest_list"]: |
Miklos Balint | 3a05c9d | 2018-05-31 09:31:27 +0200 | [diff] [blame] | 68 | manifest_path = item['manifest'] |
Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 69 | try: |
| 70 | file = open(manifest_path) |
| 71 | manifest = yaml.load(file) |
| 72 | db.append({"manifest": manifest, "attr": item}) |
| 73 | except IOError: |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 74 | raise Exception ("Manifest for "+item['name']+" cannot be opened at path "+item['manifest']) |
Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 75 | |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 76 | return db |
| 77 | |
Edison Ai | 48b2d9e | 2019-06-24 14:39:45 +0800 | [diff] [blame^] | 78 | def generate_manifestfilename(env): |
| 79 | """ |
| 80 | Generate manifestfilename header file. |
| 81 | |
| 82 | Parameters |
| 83 | ---------- |
| 84 | env : |
| 85 | The instance of Environment. |
| 86 | """ |
| 87 | with open(manifest_list_yaml_file_path) as manifest_list_yaml_file: |
| 88 | manifest_list = yaml.load(manifest_list_yaml_file) |
| 89 | templatefile_name = 'secure_fw/services/manifestfilename.template' |
| 90 | template = env.get_template(templatefile_name) |
| 91 | |
| 92 | for manifest_file in manifest_list["manifest_list"]: |
| 93 | manifest_path = manifest_file['manifest'] |
| 94 | file = open(manifest_path) |
| 95 | manifest = yaml.load(file) |
| 96 | |
| 97 | utilities = {} |
| 98 | utilities['donotedit_warning']=donotedit_warning |
| 99 | |
| 100 | context = {} |
| 101 | context['manifest'] = manifest |
| 102 | context['attr'] = manifest_file |
| 103 | context['utilities'] = utilities |
| 104 | |
| 105 | manifest_dir, sep, manifest_name = manifest_path.rpartition('/') |
| 106 | outfile_name = manifest_name.replace('yaml', 'h') |
| 107 | outfile_path = manifest_dir + sep + "psa_manifest/" + outfile_name |
| 108 | |
| 109 | context['file_name'] = outfile_name.replace('.h', '') |
| 110 | |
| 111 | print ("Generating " + outfile_path) |
| 112 | |
| 113 | if not os.path.exists(os.path.dirname(outfile_path)): |
| 114 | try: |
| 115 | os.makedirs(os.path.dirname(outfile_path)) |
| 116 | except OSError: |
| 117 | raise Exception ("Failed to create folder" + os.path.dirname(outfile_path)) |
| 118 | |
| 119 | outfile = io.open(outfile_path, "w", newline='\n') |
| 120 | outfile.write(template.render(context)) |
| 121 | outfile.close() |
| 122 | return |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 123 | |
Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 124 | def main(): |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 125 | """ |
| 126 | The entry point of the script. |
| 127 | |
| 128 | Generates the output files based on the templates and the manifests. |
| 129 | """ |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 130 | env = Environment( |
| 131 | loader = TemplateLoader(), |
| 132 | autoescape = select_autoescape(['html', 'xml']), |
| 133 | lstrip_blocks = True, |
| 134 | trim_blocks = True, |
| 135 | keep_trailing_newline = True |
| 136 | ) |
| 137 | |
Edison Ai | 48b2d9e | 2019-06-24 14:39:45 +0800 | [diff] [blame^] | 138 | with open(manifest_list_yaml_file_path) as manifest_list_yaml_file: |
Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 139 | # Read manifest list file, build database |
| 140 | db = load_manifest_list(manifest_list_yaml_file) |
| 141 | |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 142 | utilities = {} |
| 143 | utilities['donotedit_warning']=donotedit_warning |
| 144 | |
| 145 | context = {} |
| 146 | context['manifests'] = db |
| 147 | context['utilities'] = utilities |
| 148 | |
Miklos Balint | 3a05c9d | 2018-05-31 09:31:27 +0200 | [diff] [blame] | 149 | with open(os.path.join('tools', 'tfm_generated_file_list.yaml')) \ |
| 150 | as file_list_yaml_file: |
| 151 | # read list of files that need to be generated from templates using db |
| 152 | file_list_yaml = yaml.load(file_list_yaml_file) |
| 153 | file_list = file_list_yaml["file_list"] |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 154 | for file in file_list: |
| 155 | outfile_name = file["output"] |
| 156 | templatefile_name = outfile_name + '.template' |
| 157 | |
| 158 | print ("Generating " + outfile_name) |
| 159 | |
| 160 | template = env.get_template(templatefile_name) |
| 161 | |
| 162 | outfile = io.open(outfile_name, "w", newline='\n') |
| 163 | outfile.write(template.render(context)) |
| 164 | outfile.close() |
Edison Ai | 48b2d9e | 2019-06-24 14:39:45 +0800 | [diff] [blame^] | 165 | |
| 166 | # Generate manifestfilename |
| 167 | generate_manifestfilename(env) |
| 168 | |
Mate Toth-Pal | 36f2184 | 2018-11-08 16:12:51 +0100 | [diff] [blame] | 169 | print ("Generation of files done") |
Miklos Balint | 470919c | 2018-05-22 17:51:29 +0200 | [diff] [blame] | 170 | |
| 171 | if __name__ == "__main__": |
| 172 | main() |