blob: 3c59a5ba9dc268d2fd17dfe9eb0fa1697de4dfbc [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
Archanae03960e2021-12-19 09:17:04 +053025import argparse
Archana1f1a34a2021-11-17 08:44:07 +053026import jinja2
Archanae03960e2021-12-19 09:17:04 +053027from mbedtls_dev import build_tree
Archana1f1a34a2021-11-17 08:44:07 +053028
Archana6f21e452021-11-23 14:46:51 +053029def render(template_path: str) -> str:
Archanae03960e2021-12-19 09:17:04 +053030 """
31 Render template from the input file.
32 """
Archana6f21e452021-11-23 14:46:51 +053033 environment = jinja2.Environment(
34 loader=jinja2.FileSystemLoader(os.path.dirname(template_path)),
35 keep_trailing_newline=True)
36 template = environment.get_template(os.path.basename(template_path))
Archanae03960e2021-12-19 09:17:04 +053037
Archana6f21e452021-11-23 14:46:51 +053038 return template.render()
Archana1f1a34a2021-11-17 08:44:07 +053039
Archanae03960e2021-12-19 09:17:04 +053040def generate_driver_wrapper_file(mbedtls_root: str, output_dir: str) -> None:
41 """
42 Generate the file psa_crypto_driver_wrapper.c.
43 """
44 driver_wrapper_template_filename = \
45 os.path.join(mbedtls_root,
46 "scripts/data_files/driver_templates/psa_crypto_driver_wrappers.conf")
Archana1f1a34a2021-11-17 08:44:07 +053047
Archanae03960e2021-12-19 09:17:04 +053048 result = render(driver_wrapper_template_filename)
Archana1f1a34a2021-11-17 08:44:07 +053049
Archanae03960e2021-12-19 09:17:04 +053050 with open(os.path.join(output_dir, "psa_crypto_driver_wrappers.c"), 'w') as out_file:
51 out_file.write(result)
52 out_file.close()
Archana6f21e452021-11-23 14:46:51 +053053
Archanae03960e2021-12-19 09:17:04 +053054def main() -> int:
55 """
56 Main with command line arguments.
57 """
58 parser = argparse.ArgumentParser()
59 parser.add_argument('--mbedtls-root', nargs='?', default=None,
60 help='root directory of mbedtls source code')
61 parser.add_argument('output_directory', nargs='?',
62 default='library', help='output file\'s location')
63 args = parser.parse_args()
64 mbedtls_root = os.path.abspath(args.mbedtls_root or build_tree.guess_mbedtls_root())
65 output_directory = args.output_directory
66
67 generate_driver_wrapper_file(mbedtls_root, output_directory)
68
69 return 0
70
71if __name__ == '__main__':
72 sys.exit(main())