blob: 2e563b16f3b1e85652e9d32756b185992e905b55 [file] [log] [blame]
Miklos Balint470919c2018-05-22 17:51:29 +02001#-------------------------------------------------------------------------------
Mate Toth-Pal36f21842018-11-08 16:12:51 +01002# Copyright (c) 2018-2019, Arm Limited. All rights reserved.
Miklos Balint470919c2018-05-22 17:51:29 +02003#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6#-------------------------------------------------------------------------------
7
8import os
Mate Toth-Pal36f21842018-11-08 16:12:51 +01009import io
10from jinja2 import Environment, BaseLoader, select_autoescape
Miklos Balint470919c2018-05-22 17:51:29 +020011
12try:
13 import yaml
14except ImportError as e:
Mate Toth-Pala99ec6b2019-05-07 11:00:56 +020015 print (str(e) + " To install it, type:")
Mate Toth-Pal36f21842018-11-08 16:12:51 +010016 print ("pip install PyYAML")
Miklos Balint470919c2018-05-22 17:51:29 +020017 exit(1)
18
Edison Ai48b2d9e2019-06-24 14:39:45 +080019donotedit_warning = \
20 "/*********** " + \
21 "WARNING: This is an auto-generated file. Do not edit!" + \
22 " ***********/"
23manifest_list_yaml_file_path = os.path.join('tools', 'tfm_manifest_list.yaml')
24
Mate Toth-Pal36f21842018-11-08 16:12:51 +010025class TemplateLoader(BaseLoader):
26 """
27 Template loader class.
Miklos Balint470919c2018-05-22 17:51:29 +020028
Mate Toth-Pal36f21842018-11-08 16:12:51 +010029 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 Balint470919c2018-05-22 17:51:29 +020034
Mate Toth-Pal36f21842018-11-08 16:12:51 +010035 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 Balint470919c2018-05-22 17:51:29 +020050def load_manifest_list(file):
Mate Toth-Pal36f21842018-11-08 16:12:51 +010051 """
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 Balint470919c2018-05-22 17:51:29 +020065 db = []
66 manifest_list = yaml.load(file)
67 for item in manifest_list["manifest_list"]:
Miklos Balint3a05c9d2018-05-31 09:31:27 +020068 manifest_path = item['manifest']
Miklos Balint470919c2018-05-22 17:51:29 +020069 try:
70 file = open(manifest_path)
71 manifest = yaml.load(file)
72 db.append({"manifest": manifest, "attr": item})
73 except IOError:
Mate Toth-Pal36f21842018-11-08 16:12:51 +010074 raise Exception ("Manifest for "+item['name']+" cannot be opened at path "+item['manifest'])
Miklos Balint470919c2018-05-22 17:51:29 +020075
Mate Toth-Pal36f21842018-11-08 16:12:51 +010076 return db
77
Edison Ai48b2d9e2019-06-24 14:39:45 +080078def generate_manifestfilename(env):
79 """
80 Generate manifestfilename header file.
81
82 Parameters
83 ----------
84 env :
85 The instance of Environment.
86 """
Edison Ai6e3f2a32019-06-11 15:29:05 +080087 manifest_header_list = []
Edison Ai48b2d9e2019-06-24 14:39:45 +080088 with open(manifest_list_yaml_file_path) as manifest_list_yaml_file:
89 manifest_list = yaml.load(manifest_list_yaml_file)
90 templatefile_name = 'secure_fw/services/manifestfilename.template'
91 template = env.get_template(templatefile_name)
92
93 for manifest_file in manifest_list["manifest_list"]:
94 manifest_path = manifest_file['manifest']
95 file = open(manifest_path)
96 manifest = yaml.load(file)
97
98 utilities = {}
99 utilities['donotedit_warning']=donotedit_warning
100
101 context = {}
102 context['manifest'] = manifest
103 context['attr'] = manifest_file
104 context['utilities'] = utilities
105
106 manifest_dir, sep, manifest_name = manifest_path.rpartition('/')
107 outfile_name = manifest_name.replace('yaml', 'h')
108 outfile_path = manifest_dir + sep + "psa_manifest/" + outfile_name
109
110 context['file_name'] = outfile_name.replace('.h', '')
111
Edison Ai6e3f2a32019-06-11 15:29:05 +0800112 manifest_header_list.append(outfile_path)
113
Edison Ai48b2d9e2019-06-24 14:39:45 +0800114 print ("Generating " + outfile_path)
115
116 if not os.path.exists(os.path.dirname(outfile_path)):
117 try:
118 os.makedirs(os.path.dirname(outfile_path))
119 except OSError:
120 raise Exception ("Failed to create folder" + os.path.dirname(outfile_path))
121
122 outfile = io.open(outfile_path, "w", newline='\n')
123 outfile.write(template.render(context))
124 outfile.close()
Edison Ai6e3f2a32019-06-11 15:29:05 +0800125 return manifest_header_list
Mate Toth-Pal36f21842018-11-08 16:12:51 +0100126
Miklos Balint470919c2018-05-22 17:51:29 +0200127def main():
Mate Toth-Pal36f21842018-11-08 16:12:51 +0100128 """
129 The entry point of the script.
130
131 Generates the output files based on the templates and the manifests.
132 """
Mate Toth-Pal36f21842018-11-08 16:12:51 +0100133 env = Environment(
134 loader = TemplateLoader(),
135 autoescape = select_autoescape(['html', 'xml']),
136 lstrip_blocks = True,
137 trim_blocks = True,
138 keep_trailing_newline = True
139 )
140
Edison Ai6e3f2a32019-06-11 15:29:05 +0800141 # Generate manifestfilename
142 manifest_header_list = generate_manifestfilename(env)
143 utilities = {}
144 context = {}
145
Edison Ai48b2d9e2019-06-24 14:39:45 +0800146 with open(manifest_list_yaml_file_path) as manifest_list_yaml_file:
Miklos Balint470919c2018-05-22 17:51:29 +0200147 # Read manifest list file, build database
148 db = load_manifest_list(manifest_list_yaml_file)
149
Mate Toth-Pal36f21842018-11-08 16:12:51 +0100150 utilities['donotedit_warning']=donotedit_warning
Edison Ai6e3f2a32019-06-11 15:29:05 +0800151 utilities['manifest_header_list']=manifest_header_list
Mate Toth-Pal36f21842018-11-08 16:12:51 +0100152
Mate Toth-Pal36f21842018-11-08 16:12:51 +0100153 context['manifests'] = db
154 context['utilities'] = utilities
155
Miklos Balint3a05c9d2018-05-31 09:31:27 +0200156 with open(os.path.join('tools', 'tfm_generated_file_list.yaml')) \
157 as file_list_yaml_file:
158 # read list of files that need to be generated from templates using db
159 file_list_yaml = yaml.load(file_list_yaml_file)
160 file_list = file_list_yaml["file_list"]
Mate Toth-Pal36f21842018-11-08 16:12:51 +0100161 for file in file_list:
162 outfile_name = file["output"]
163 templatefile_name = outfile_name + '.template'
164
165 print ("Generating " + outfile_name)
166
167 template = env.get_template(templatefile_name)
168
169 outfile = io.open(outfile_name, "w", newline='\n')
170 outfile.write(template.render(context))
171 outfile.close()
Edison Ai48b2d9e2019-06-24 14:39:45 +0800172
Mate Toth-Pal36f21842018-11-08 16:12:51 +0100173 print ("Generation of files done")
Miklos Balint470919c2018-05-22 17:51:29 +0200174
175if __name__ == "__main__":
176 main()