blob: 8e58b66f51922cfd6279922382aebe7b2c1594a5 [file] [log] [blame]
Archana1f1a34a2021-11-17 08:44:07 +05301#!/usr/bin/env python3
Archanae03960e2021-12-19 09:17:04 +05302"""Generate library/psa_crypto_driver_wrappers.c
3
4 This module is invoked by the build sripts to auto generate the
5 psa_crypto_driver_wrappers.c based on template files in
6 script/data_files/driver_templates/.
7"""
8# Copyright The Mbed TLS Contributors
9# SPDX-License-Identifier: Apache-2.0
10#
11# Licensed under the Apache License, Version 2.0 (the "License"); you may
12# not use this file except in compliance with the License.
13# You may obtain a copy of the License at
14#
15# http://www.apache.org/licenses/LICENSE-2.0
16#
17# Unless required by applicable law or agreed to in writing, software
18# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20# See the License for the specific language governing permissions and
21# limitations under the License.
Archana1f1a34a2021-11-17 08:44:07 +053022
23import sys
Archana1f1a34a2021-11-17 08:44:07 +053024import os
Archanae829cd62021-12-24 12:50:36 +053025import json
Archanae03960e2021-12-19 09:17:04 +053026import argparse
Archana1f1a34a2021-11-17 08:44:07 +053027import jinja2
Archanae03960e2021-12-19 09:17:04 +053028from mbedtls_dev import build_tree
Archana1f1a34a2021-11-17 08:44:07 +053029
Archanae829cd62021-12-24 12:50:36 +053030def render(template_path: str, driver_jsoncontext: list) -> str:
Archanae03960e2021-12-19 09:17:04 +053031 """
Archanae829cd62021-12-24 12:50:36 +053032 Render template from the input file and driver JSON.
Archanae03960e2021-12-19 09:17:04 +053033 """
Archana6f21e452021-11-23 14:46:51 +053034 environment = jinja2.Environment(
35 loader=jinja2.FileSystemLoader(os.path.dirname(template_path)),
36 keep_trailing_newline=True)
37 template = environment.get_template(os.path.basename(template_path))
Archanae03960e2021-12-19 09:17:04 +053038
Archanae829cd62021-12-24 12:50:36 +053039 return template.render(drivers = driver_jsoncontext)
Archana1f1a34a2021-11-17 08:44:07 +053040
Archanae829cd62021-12-24 12:50:36 +053041
42def generate_driver_wrapper_file(template_dir: str, output_dir: str, driver_jsoncontext: list ) -> None:
Archanae03960e2021-12-19 09:17:04 +053043 """
44 Generate the file psa_crypto_driver_wrapper.c.
45 """
46 driver_wrapper_template_filename = \
Archanae829cd62021-12-24 12:50:36 +053047 os.path.join(template_dir, "psa_crypto_driver_wrappers.c.jinja")
Archana1f1a34a2021-11-17 08:44:07 +053048
Archanae829cd62021-12-24 12:50:36 +053049 result = render(driver_wrapper_template_filename, driver_jsoncontext)
Archana1f1a34a2021-11-17 08:44:07 +053050
Archanae03960e2021-12-19 09:17:04 +053051 with open(os.path.join(output_dir, "psa_crypto_driver_wrappers.c"), 'w') as out_file:
52 out_file.write(result)
Archana6f21e452021-11-23 14:46:51 +053053
Archanae829cd62021-12-24 12:50:36 +053054def validate_mergedjson(merged_driverjson: list) -> int:
55 """
56 Validate the merged Driver JSON for errors that we can catch early
57 """
58 return 0
59
60
61def merge_driverjsonfiles(json_directory: str, jsondriverlistName: str) -> list:
62 """
63 Merge driver JSON files into a single ordered JSON.
64 """
65 result = list()
66 driverlist = list()
67 with open(os.path.join(json_directory, jsondriverlistName), 'r') as driverlistfile:
68 driverlist = json.load(driverlistfile)
69 for file_name in driverlist:
70 with open(os.path.join(json_directory, file_name), 'r') as infile:
71 result.extend(json.load(infile))
72
73 return result
74
75
Archanae03960e2021-12-19 09:17:04 +053076def main() -> int:
77 """
78 Main with command line arguments.
79 """
Archana4a9e0262021-12-19 13:34:30 +053080 def_arg_mbedtls_root = build_tree.guess_mbedtls_root()
81 def_arg_output_dir = os.path.join(def_arg_mbedtls_root, 'library')
Archanae829cd62021-12-24 12:50:36 +053082 def_arg_template_dir = os.path.join(def_arg_mbedtls_root, 'scripts/data_files/driver_templates/')
83 def_arg_json_dir = os.path.join(def_arg_mbedtls_root, 'scripts/data_files/driver_jsons/')
Archana4a9e0262021-12-19 13:34:30 +053084
Archanae03960e2021-12-19 09:17:04 +053085 parser = argparse.ArgumentParser()
Archana4a9e0262021-12-19 13:34:30 +053086 parser.add_argument('--mbedtls-root', nargs='?', default=def_arg_mbedtls_root,
Archanae03960e2021-12-19 09:17:04 +053087 help='root directory of mbedtls source code')
Archanae829cd62021-12-24 12:50:36 +053088 parser.add_argument('--template_dir', nargs='?', default=def_arg_template_dir,
89 help='root directory of mbedtls source code')
90 parser.add_argument('--json_dir', nargs='?', default=def_arg_json_dir,
91 help='root directory of mbedtls source code')
Archanae03960e2021-12-19 09:17:04 +053092 parser.add_argument('output_directory', nargs='?',
Archana4a9e0262021-12-19 13:34:30 +053093 default=def_arg_output_dir, help='output file\'s location')
Archanae03960e2021-12-19 09:17:04 +053094 args = parser.parse_args()
Archana4a9e0262021-12-19 13:34:30 +053095
Archanae829cd62021-12-24 12:50:36 +053096 mbedtls_root = os.path.abspath(args.mbedtls_root)
97 output_directory = args.output_directory
98 template_directory = args.template_dir
99 json_directory = args.json_dir
Archanae03960e2021-12-19 09:17:04 +0530100
Archanae829cd62021-12-24 12:50:36 +0530101 # load list of driver jsons from driverlist.json
102 merged_driverjson = merge_driverjsonfiles(json_directory, 'driverlist.json')
103 ret = validate_mergedjson(merged_driverjson)
104 if ret == 1:
105 print("Validation failed ")
106 return 1
107
108 generate_driver_wrapper_file(template_directory, output_directory, merged_driverjson)
Archanae03960e2021-12-19 09:17:04 +0530109
110 return 0
111
112if __name__ == '__main__':
113 sys.exit(main())