blob: 102ff139cf5b3f5b55c0f1494a7561517c7b3289 [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 """
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-Pal36f21842018-11-08 16:12:51 +0100123
Miklos Balint470919c2018-05-22 17:51:29 +0200124def main():
Mate Toth-Pal36f21842018-11-08 16:12:51 +0100125 """
126 The entry point of the script.
127
128 Generates the output files based on the templates and the manifests.
129 """
Mate Toth-Pal36f21842018-11-08 16:12:51 +0100130 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 Ai48b2d9e2019-06-24 14:39:45 +0800138 with open(manifest_list_yaml_file_path) as manifest_list_yaml_file:
Miklos Balint470919c2018-05-22 17:51:29 +0200139 # Read manifest list file, build database
140 db = load_manifest_list(manifest_list_yaml_file)
141
Mate Toth-Pal36f21842018-11-08 16:12:51 +0100142 utilities = {}
143 utilities['donotedit_warning']=donotedit_warning
144
145 context = {}
146 context['manifests'] = db
147 context['utilities'] = utilities
148
Miklos Balint3a05c9d2018-05-31 09:31:27 +0200149 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-Pal36f21842018-11-08 16:12:51 +0100154 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 Ai48b2d9e2019-06-24 14:39:45 +0800165
166 # Generate manifestfilename
167 generate_manifestfilename(env)
168
Mate Toth-Pal36f21842018-11-08 16:12:51 +0100169 print ("Generation of files done")
Miklos Balint470919c2018-05-22 17:51:29 +0200170
171if __name__ == "__main__":
172 main()