fix test_cmake_as_package fail

Signed-off-by: Jerry Yu <jerry.h.yu@arm.com>
This commit is contained in:
Jerry Yu 2021-12-02 13:51:26 +08:00
parent eb96fb508e
commit e6369b0061
3 changed files with 61 additions and 14 deletions

View File

@ -151,6 +151,8 @@ if(GEN_FILES)
COMMAND COMMAND
${MBEDTLS_PYTHON_EXECUTABLE} ${MBEDTLS_PYTHON_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/../scripts/generate_ssl_debug_helpers.py ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/generate_ssl_debug_helpers.py
--mbedtls-root ${CMAKE_CURRENT_SOURCE_DIR}/..
${CMAKE_CURRENT_BINARY_DIR}
DEPENDS DEPENDS
${CMAKE_CURRENT_SOURCE_DIR}/../scripts/generate_ssl_debug_helpers.py ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/generate_ssl_debug_helpers.py
${error_headers} ${error_headers}

View File

@ -25,8 +25,10 @@ import sys
import re import re
import os import os
import textwrap import textwrap
import argparse
from mbedtls_dev import build_tree from mbedtls_dev import build_tree
def remove_c_comments(string): def remove_c_comments(string):
""" """
Remove C style comments from input string Remove C style comments from input string
@ -34,16 +36,19 @@ def remove_c_comments(string):
string_pattern = r"(?P<string>\".*?\"|\'.*?\')" string_pattern = r"(?P<string>\".*?\"|\'.*?\')"
comment_pattern = r"(?P<comment>/\*.*?\*/|//[^\r\n]*$)" comment_pattern = r"(?P<comment>/\*.*?\*/|//[^\r\n]*$)"
pattern = re.compile(string_pattern + r'|' + comment_pattern, pattern = re.compile(string_pattern + r'|' + comment_pattern,
re.MULTILINE|re.DOTALL) re.MULTILINE | re.DOTALL)
def replacer(match): def replacer(match):
if match.lastgroup == 'comment': if match.lastgroup == 'comment':
return "" return ""
return match.group() return match.group()
return pattern.sub(replacer, string) return pattern.sub(replacer, string)
class CondDirectiveNotMatch(Exception): class CondDirectiveNotMatch(Exception):
pass pass
def preprocess_c_source_code(source, *classes): def preprocess_c_source_code(source, *classes):
""" """
Simple preprocessor for C source code. Simple preprocessor for C source code.
@ -124,7 +129,6 @@ def preprocess_c_source_code(source, *classes):
assert not stack, len(stack) assert not stack, len(stack)
class EnumDefinition: class EnumDefinition:
""" """
Generate helper functions around enumeration. Generate helper functions around enumeration.
@ -153,7 +157,7 @@ class EnumDefinition:
enum_pattern = re.compile(r'enum\s*(?P<prefix_name>\w*)\s*' + enum_pattern = re.compile(r'enum\s*(?P<prefix_name>\w*)\s*' +
r'{\s*(?P<body>[^}]*)}' + r'{\s*(?P<body>[^}]*)}' +
r'\s*(?P<suffix_name>\w*)\s*;', r'\s*(?P<suffix_name>\w*)\s*;',
re.MULTILINE|re.DOTALL) re.MULTILINE | re.DOTALL)
for match in enum_pattern.finditer(source_code, start, end): for match in enum_pattern.finditer(source_code, start, end):
yield EnumDefinition(source_code, yield EnumDefinition(source_code,
@ -233,6 +237,7 @@ class EnumDefinition:
prototype=self._prototype) prototype=self._prototype)
return body, prototype return body, prototype
OUTPUT_C_TEMPLATE = '''\ OUTPUT_C_TEMPLATE = '''\
/* Automatically generated by generate_ssl_debug_helpers.py. DO NOT EDIT. */ /* Automatically generated by generate_ssl_debug_helpers.py. DO NOT EDIT. */
@ -272,11 +277,11 @@ OUTPUT_H_TEMPLATE = '''\
''' '''
def generate_ssl_debug_helpers(target_dir): def generate_ssl_debug_helpers(output_directory, mbedtls_root):
""" """
Generate functions of debug helps Generate functions of debug helps
""" """
with open('include/mbedtls/ssl.h') as f: with open(os.path.join(mbedtls_root, 'include/mbedtls/ssl.h')) as f:
source_code = remove_c_comments(f.read()) source_code = remove_c_comments(f.read())
definitions = dict() definitions = dict()
@ -292,19 +297,38 @@ def generate_ssl_debug_helpers(target_dir):
definitions[start] = definition definitions[start] = definition
prototypes[start] = prototype prototypes[start] = prototype
functions = [str(v) for _, v in sorted(definitions.items())] function_definitions = [str(v) for _, v in sorted(definitions.items())]
with open(os.path.join(target_dir, 'ssl_debug_helpers_generated.c'), 'w') as f: function_prototypes = [str(v) for _, v in sorted(prototypes.items())]
f.write(OUTPUT_C_TEMPLATE.format(functions='\n'.join(functions))) if output_directory == sys.stdout:
sys.stdout.write(OUTPUT_H_TEMPLATE.format(
functions='\n'.join(function_prototypes)))
sys.stdout.write(OUTPUT_C_TEMPLATE.format(
functions='\n'.join(function_definitions)))
else:
with open(os.path.join(output_directory, 'ssl_debug_helpers_generated.c'), 'w') as f:
f.write(OUTPUT_C_TEMPLATE.format(
functions='\n'.join(function_definitions)))
functions = [str(v) for _, v in sorted(prototypes.items())] with open(os.path.join(output_directory, 'ssl_debug_helpers_generated.h'), 'w') as f:
with open(os.path.join(target_dir, 'ssl_debug_helpers_generated.h'), 'w') as f: f.write(OUTPUT_H_TEMPLATE.format(
f.write(OUTPUT_H_TEMPLATE.format(functions='\n'.join(functions))) functions='\n'.join(function_prototypes)))
def main():
"""
Command line entry
"""
parser = argparse.ArgumentParser()
parser.add_argument('--mbedtls-root', nargs='?', default=build_tree.guess_mbedtls_root(),
help='root directory of mbedtls source code')
parser.add_argument('output_directory', nargs='?',
default=sys.stdout, help='source/header files location')
args = parser.parse_args()
generate_ssl_debug_helpers(args.output_directory, args.mbedtls_root)
return 0
if __name__ == '__main__': if __name__ == '__main__':
build_tree.chdir_to_root() sys.exit(main())
OUTPUT_FILE_DIR = sys.argv[1] if len(sys.argv) == 2 else "library"
generate_ssl_debug_helpers(OUTPUT_FILE_DIR)

View File

@ -17,12 +17,15 @@
# limitations under the License. # limitations under the License.
import os import os
import inspect
def looks_like_mbedtls_root(path: str) -> bool: def looks_like_mbedtls_root(path: str) -> bool:
"""Whether the given directory looks like the root of the Mbed TLS source tree.""" """Whether the given directory looks like the root of the Mbed TLS source tree."""
return all(os.path.isdir(os.path.join(path, subdir)) return all(os.path.isdir(os.path.join(path, subdir))
for subdir in ['include', 'library', 'programs', 'tests']) for subdir in ['include', 'library', 'programs', 'tests'])
def chdir_to_root() -> None: def chdir_to_root() -> None:
"""Detect the root of the Mbed TLS source tree and change to it. """Detect the root of the Mbed TLS source tree and change to it.
@ -36,3 +39,21 @@ def chdir_to_root() -> None:
os.chdir(d) os.chdir(d)
return return
raise Exception('Mbed TLS source tree not found') raise Exception('Mbed TLS source tree not found')
def guess_mbedtls_root():
"""Guess mbedTLS source code directory.
Return the first possible mbedTLS root directory
"""
dirs = set({})
for i in inspect.stack():
path = os.path.dirname(i.filename)
for d in ['.', os.path.pardir, os.path.join(*([os.path.pardir]*2))]:
d = os.path.abspath(os.path.join(path, d))
if d in dirs:
continue
dirs.add(d)
if looks_like_mbedtls_root(d):
return d
raise Exception('Mbed TLS source tree not found')