| Archana | 1f1a34a | 2021-11-17 08:44:07 +0530 | [diff] [blame] | 1 | #!/usr/bin/env python3 | 
| Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 2 | """Generate library/psa_crypto_driver_wrappers.c | 
|  | 3 |  | 
| Andrzej Kurek | 5c65c57 | 2022-04-13 14:28:52 -0400 | [diff] [blame] | 4 | This module is invoked by the build scripts to auto generate the | 
| Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 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. | 
| Archana | 1f1a34a | 2021-11-17 08:44:07 +0530 | [diff] [blame] | 22 |  | 
|  | 23 | import sys | 
| Archana | 1f1a34a | 2021-11-17 08:44:07 +0530 | [diff] [blame] | 24 | import os | 
| Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 25 | import argparse | 
| Archana | 1f1a34a | 2021-11-17 08:44:07 +0530 | [diff] [blame] | 26 | import jinja2 | 
| Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 27 | from mbedtls_dev import build_tree | 
| Archana | 1f1a34a | 2021-11-17 08:44:07 +0530 | [diff] [blame] | 28 |  | 
| Archana | 6f21e45 | 2021-11-23 14:46:51 +0530 | [diff] [blame] | 29 | def render(template_path: str) -> str: | 
| Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 30 | """ | 
|  | 31 | Render template from the input file. | 
|  | 32 | """ | 
| Archana | 6f21e45 | 2021-11-23 14:46:51 +0530 | [diff] [blame] | 33 | 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)) | 
| Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 37 |  | 
| Archana | 6f21e45 | 2021-11-23 14:46:51 +0530 | [diff] [blame] | 38 | return template.render() | 
| Archana | 1f1a34a | 2021-11-17 08:44:07 +0530 | [diff] [blame] | 39 |  | 
| Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 40 | def 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 = \ | 
| Archana | c08248d | 2021-12-19 09:28:39 +0530 | [diff] [blame] | 45 | os.path.join(mbedtls_root, \ | 
|  | 46 | "scripts/data_files/driver_templates/psa_crypto_driver_wrappers.c.jinja") | 
| Archana | 1f1a34a | 2021-11-17 08:44:07 +0530 | [diff] [blame] | 47 |  | 
| Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 48 | result = render(driver_wrapper_template_filename) | 
| Archana | 1f1a34a | 2021-11-17 08:44:07 +0530 | [diff] [blame] | 49 |  | 
| Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 50 | with open(os.path.join(output_dir, "psa_crypto_driver_wrappers.c"), 'w') as out_file: | 
|  | 51 | out_file.write(result) | 
| Archana | 6f21e45 | 2021-11-23 14:46:51 +0530 | [diff] [blame] | 52 |  | 
| Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 53 | def main() -> int: | 
|  | 54 | """ | 
|  | 55 | Main with command line arguments. | 
|  | 56 | """ | 
| Archana | 4a9e026 | 2021-12-19 13:34:30 +0530 | [diff] [blame] | 57 | def_arg_mbedtls_root = build_tree.guess_mbedtls_root() | 
|  | 58 | def_arg_output_dir = os.path.join(def_arg_mbedtls_root, 'library') | 
|  | 59 |  | 
| Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 60 | parser = argparse.ArgumentParser() | 
| Archana | 4a9e026 | 2021-12-19 13:34:30 +0530 | [diff] [blame] | 61 | parser.add_argument('--mbedtls-root', nargs='?', default=def_arg_mbedtls_root, | 
| Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 62 | help='root directory of mbedtls source code') | 
|  | 63 | parser.add_argument('output_directory', nargs='?', | 
| Archana | 4a9e026 | 2021-12-19 13:34:30 +0530 | [diff] [blame] | 64 | default=def_arg_output_dir, help='output file\'s location') | 
| Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 65 | args = parser.parse_args() | 
| Archana | 4a9e026 | 2021-12-19 13:34:30 +0530 | [diff] [blame] | 66 |  | 
|  | 67 | mbedtls_root = os.path.abspath(args.mbedtls_root) | 
| Archana | e03960e | 2021-12-19 09:17:04 +0530 | [diff] [blame] | 68 | output_directory = args.output_directory | 
|  | 69 |  | 
|  | 70 | generate_driver_wrapper_file(mbedtls_root, output_directory) | 
|  | 71 |  | 
|  | 72 | return 0 | 
|  | 73 |  | 
|  | 74 | if __name__ == '__main__': | 
|  | 75 | sys.exit(main()) |