mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-03-29 13:20:21 +00:00
Restructure Python script to use argparse and main
Signed-off-by: Archana <archana.madhavan@silabs.com>
This commit is contained in:
parent
947cf611f2
commit
e03960e460
@ -164,8 +164,8 @@ if(GEN_FILES)
|
|||||||
COMMAND
|
COMMAND
|
||||||
${MBEDTLS_PYTHON_EXECUTABLE}
|
${MBEDTLS_PYTHON_EXECUTABLE}
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/../scripts/generate_driver_wrappers.py
|
${CMAKE_CURRENT_SOURCE_DIR}/../scripts/generate_driver_wrappers.py
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/../scripts/data_files/driver_templates/psa_crypto_driver_wrappers.conf
|
--mbedtls-root ${CMAKE_CURRENT_SOURCE_DIR}/..
|
||||||
${CMAKE_CURRENT_BINARY_DIR}/psa_crypto_driver_wrappers.c
|
${CMAKE_CURRENT_BINARY_DIR}
|
||||||
DEPENDS
|
DEPENDS
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/../scripts/generate_driver_wrappers.py
|
${CMAKE_CURRENT_SOURCE_DIR}/../scripts/generate_driver_wrappers.py
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/../scripts/data_files/driver_templates/psa_crypto_driver_wrappers.conf
|
${CMAKE_CURRENT_SOURCE_DIR}/../scripts/data_files/driver_templates/psa_crypto_driver_wrappers.conf
|
||||||
|
@ -323,8 +323,7 @@ psa_crypto_driver_wrappers.c: ../scripts/generate_driver_wrappers.py
|
|||||||
psa_crypto_driver_wrappers.c: ../scripts/data_files/driver_templates/psa_crypto_driver_wrappers.conf
|
psa_crypto_driver_wrappers.c: ../scripts/data_files/driver_templates/psa_crypto_driver_wrappers.conf
|
||||||
psa_crypto_driver_wrappers.c:
|
psa_crypto_driver_wrappers.c:
|
||||||
echo " Gen $@"
|
echo " Gen $@"
|
||||||
$(PYTHON) ../scripts/generate_driver_wrappers.py \
|
$(PYTHON) ../scripts/generate_driver_wrappers.py --mbedtls-root .. .
|
||||||
"../"
|
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
ifndef WINDOWS
|
ifndef WINDOWS
|
||||||
|
@ -1,35 +1,72 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
"""This script is required for the auto generation of the
|
"""Generate library/psa_crypto_driver_wrappers.c
|
||||||
psa_crypto_driver_wrappers.c file"""
|
|
||||||
|
This module is invoked by the build sripts to auto generate the
|
||||||
|
psa_crypto_driver_wrappers.c based on template files in
|
||||||
|
script/data_files/driver_templates/.
|
||||||
|
"""
|
||||||
|
# Copyright The Mbed TLS Contributors
|
||||||
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||||
|
# not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
|
import argparse
|
||||||
import jinja2
|
import jinja2
|
||||||
|
from mbedtls_dev import build_tree
|
||||||
|
|
||||||
def render(template_path: str) -> str:
|
def render(template_path: str) -> str:
|
||||||
|
"""
|
||||||
|
Render template from the input file.
|
||||||
|
"""
|
||||||
environment = jinja2.Environment(
|
environment = jinja2.Environment(
|
||||||
loader=jinja2.FileSystemLoader(os.path.dirname(template_path)),
|
loader=jinja2.FileSystemLoader(os.path.dirname(template_path)),
|
||||||
keep_trailing_newline=True)
|
keep_trailing_newline=True)
|
||||||
template = environment.get_template(os.path.basename(template_path))
|
template = environment.get_template(os.path.basename(template_path))
|
||||||
|
|
||||||
return template.render()
|
return template.render()
|
||||||
|
|
||||||
N = len(sys.argv)
|
def generate_driver_wrapper_file(mbedtls_root: str, output_dir: str) -> None:
|
||||||
if N != 2:
|
"""
|
||||||
# This is the Root directory.
|
Generate the file psa_crypto_driver_wrapper.c.
|
||||||
ROOT_DIR = ""
|
"""
|
||||||
else:
|
driver_wrapper_template_filename = \
|
||||||
# Set the root based on the argument passed.
|
os.path.join(mbedtls_root,
|
||||||
ROOT_DIR = sys.argv[1]
|
"scripts/data_files/driver_templates/psa_crypto_driver_wrappers.conf")
|
||||||
|
|
||||||
# Set template file name, output file name from the root directory
|
result = render(driver_wrapper_template_filename)
|
||||||
DRIVER_WRAPPER_TEMPLATE_FILENAME = ROOT_DIR +\
|
|
||||||
"scripts/data_files/driver_templates/psa_crypto_driver_wrappers.conf"
|
|
||||||
DRIVER_WRAPPER_OUTPUT_FILENAME = ROOT_DIR + "library/psa_crypto_driver_wrappers.c"
|
|
||||||
|
|
||||||
# Render the template
|
with open(os.path.join(output_dir, "psa_crypto_driver_wrappers.c"), 'w') as out_file:
|
||||||
RESULT = render(DRIVER_WRAPPER_TEMPLATE_FILENAME)
|
out_file.write(result)
|
||||||
|
out_file.close()
|
||||||
|
|
||||||
# Write output to file
|
def main() -> int:
|
||||||
OUT_FILE = open(DRIVER_WRAPPER_OUTPUT_FILENAME, "w")
|
"""
|
||||||
OUT_FILE.write(RESULT)
|
Main with command line arguments.
|
||||||
OUT_FILE.close()
|
"""
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument('--mbedtls-root', nargs='?', default=None,
|
||||||
|
help='root directory of mbedtls source code')
|
||||||
|
parser.add_argument('output_directory', nargs='?',
|
||||||
|
default='library', help='output file\'s location')
|
||||||
|
args = parser.parse_args()
|
||||||
|
mbedtls_root = os.path.abspath(args.mbedtls_root or build_tree.guess_mbedtls_root())
|
||||||
|
output_directory = args.output_directory
|
||||||
|
|
||||||
|
generate_driver_wrapper_file(mbedtls_root, output_directory)
|
||||||
|
|
||||||
|
return 0
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
sys.exit(main())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user