diff options
author | Rob Mohr <mohrr@google.com> | 2022-11-30 22:08:49 +0000 |
---|---|---|
committer | CQ Bot Account <pigweed-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2022-11-30 22:08:49 +0000 |
commit | 47398dfbbf3ed28a9751d521966e9b75b7897da7 (patch) | |
tree | 9c006f96bb34ffeb43a3a3d22ffbbb1efe381318 | |
parent | 3c8ba26bc89ae6762598655fd2133c0c150651df (diff) | |
download | pigweed-47398dfbbf3ed28a9751d521966e9b75b7897da7.tar.gz |
pw_stm32cube_build: Format with black
Bug: b/259595799
Change-Id: I4638fcbba8a8722cd255cf51c228428181994bd1
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/121798
Commit-Queue: Rob Mohr <mohrr@google.com>
Pigweed-Auto-Submit: Rob Mohr <mohrr@google.com>
Reviewed-by: Erik Gilling <konkers@google.com>
-rw-r--r-- | pw_stm32cube_build/py/pw_stm32cube_build/__main__.py | 52 | ||||
-rw-r--r-- | pw_stm32cube_build/py/pw_stm32cube_build/find_files.py | 112 | ||||
-rw-r--r-- | pw_stm32cube_build/py/pw_stm32cube_build/gen_file_list.py | 5 | ||||
-rw-r--r-- | pw_stm32cube_build/py/pw_stm32cube_build/icf_to_ld.py | 18 | ||||
-rw-r--r-- | pw_stm32cube_build/py/pw_stm32cube_build/inject_init.py | 10 | ||||
-rw-r--r-- | pw_stm32cube_build/py/tests/find_files_test.py | 128 | ||||
-rw-r--r-- | pw_stm32cube_build/py/tests/icf_to_ld_test.py | 73 | ||||
-rw-r--r-- | pw_stm32cube_build/py/tests/inject_init_test.py | 107 |
8 files changed, 304 insertions, 201 deletions
diff --git a/pw_stm32cube_build/py/pw_stm32cube_build/__main__.py b/pw_stm32cube_build/py/pw_stm32cube_build/__main__.py index 2170e34c6..5bfca64de 100644 --- a/pw_stm32cube_build/py/pw_stm32cube_build/__main__.py +++ b/pw_stm32cube_build/py/pw_stm32cube_build/__main__.py @@ -19,8 +19,12 @@ import pathlib import sys try: - from pw_stm32cube_build import (find_files, gen_file_list, icf_to_ld, - inject_init) + from pw_stm32cube_build import ( + find_files, + gen_file_list, + icf_to_ld, + inject_init, + ) except ImportError: # Load from this directory if pw_stm32cube_build is not available. import find_files # type: ignore @@ -33,37 +37,37 @@ def _parse_args() -> argparse.Namespace: """Setup argparse and parse command line args.""" parser = argparse.ArgumentParser() - subparsers = parser.add_subparsers(dest='command', - metavar='<command>', - required=True) + subparsers = parser.add_subparsers( + dest='command', metavar='<command>', required=True + ) gen_file_list_parser = subparsers.add_parser( - 'gen_file_list', help='generate files.txt for stm32cube directory') + 'gen_file_list', help='generate files.txt for stm32cube directory' + ) gen_file_list_parser.add_argument('stm32cube_dir', type=pathlib.Path) find_files_parser = subparsers.add_parser( - 'find_files', help='find files in stm32cube directory') + 'find_files', help='find files in stm32cube directory' + ) find_files_parser.add_argument('stm32cube_dir', type=pathlib.Path) find_files_parser.add_argument('product_str') - find_files_parser.add_argument('--init', - default=False, - action='store_true') + find_files_parser.add_argument('--init', default=False, action='store_true') icf_to_ld_parser = subparsers.add_parser( - 'icf_to_ld', help='convert stm32cube .icf linker files to .ld') + 'icf_to_ld', help='convert stm32cube .icf linker files to .ld' + ) icf_to_ld_parser.add_argument('icf_path', type=pathlib.Path) - icf_to_ld_parser.add_argument('--ld-path', - nargs=1, - default=None, - type=pathlib.Path) + icf_to_ld_parser.add_argument( + '--ld-path', nargs=1, default=None, type=pathlib.Path + ) inject_init_parser = subparsers.add_parser( - 'inject_init', help='inject `pw_stm32cube_Init()` into startup_*.s') + 'inject_init', help='inject `pw_stm32cube_Init()` into startup_*.s' + ) inject_init_parser.add_argument('in_startup_path', type=pathlib.Path) - inject_init_parser.add_argument('--out-startup-path', - nargs=1, - default=None, - type=pathlib.Path) + inject_init_parser.add_argument( + '--out-startup-path', nargs=1, default=None, type=pathlib.Path + ) return parser.parse_args() @@ -77,12 +81,14 @@ def main(): elif args.command == 'find_files': find_files.find_files(args.stm32cube_dir, args.product_str, args.init) elif args.command == 'icf_to_ld': - icf_to_ld.icf_to_ld(args.icf_path, - args.ld_path[0] if args.ld_path else None) + icf_to_ld.icf_to_ld( + args.icf_path, args.ld_path[0] if args.ld_path else None + ) elif args.command == 'inject_init': inject_init.inject_init( args.in_startup_path, - args.out_startup_path[0] if args.out_startup_path else None) + args.out_startup_path[0] if args.out_startup_path else None, + ) sys.exit(0) diff --git a/pw_stm32cube_build/py/pw_stm32cube_build/find_files.py b/pw_stm32cube_build/py/pw_stm32cube_build/find_files.py index 6e233ef88..549ca50a7 100644 --- a/pw_stm32cube_build/py/pw_stm32cube_build/find_files.py +++ b/pw_stm32cube_build/py/pw_stm32cube_build/find_files.py @@ -48,7 +48,8 @@ def parse_product_str(product_str: str) -> Tuple[str, Set[str], str]: if len(product_name) < 9: raise ValueError( - "Product string too short. Must specify at least the chip model.") + "Product string too short. Must specify at least the chip model." + ) family = product_name[:7] + 'xx' name = product_name @@ -91,8 +92,11 @@ def select_define(defines: Set[str], family_header: str) -> str: """ valid_defines = list( filter( - lambda x: f'defined({x})' in family_header or f'defined ({x})' in - family_header, defines)) + lambda x: f'defined({x})' in family_header + or f'defined ({x})' in family_header, + defines, + ) + ) if len(valid_defines) != 1: raise ValueError("Unable to select a valid define") @@ -112,8 +116,10 @@ def match_filename(product_name: str, filename: str): False otherwise. """ stm32_parts = list( - filter(lambda x: x.startswith('stm32'), - re.split(r'\.|_', filename.lower()))) + filter( + lambda x: x.startswith('stm32'), re.split(r'\.|_', filename.lower()) + ) + ) if len(stm32_parts) != 1: return False @@ -145,34 +151,42 @@ def find_linker_files( """ linker_files = list( filter( - lambda x: - (x.endswith('.ld') or x.endswith('.icf')) and '_flash.' in x.lower( - ), files)) + lambda x: (x.endswith('.ld') or x.endswith('.icf')) + and '_flash.' in x.lower(), + files, + ) + ) matching_linker_files = list( - filter(lambda x: match_filename(product_name, - pathlib.Path(x).name), linker_files)) + filter( + lambda x: match_filename(product_name, pathlib.Path(x).name), + linker_files, + ) + ) matching_ld_files = list( - filter(lambda x: x.endswith('.ld'), matching_linker_files)) + filter(lambda x: x.endswith('.ld'), matching_linker_files) + ) matching_icf_files = list( - filter(lambda x: x.endswith('.icf'), matching_linker_files)) + filter(lambda x: x.endswith('.icf'), matching_linker_files) + ) if len(matching_ld_files) > 1 or len(matching_icf_files) > 1: raise ValueError( - f'Too many linker file matches for {product_name}.' + - ' Provide a more specific product string or your own linker script' + f'Too many linker file matches for {product_name}. ' + 'Provide a more specific product string or your own linker script' ) if not matching_ld_files and not matching_icf_files: raise ValueError(f'No linker script matching {product_name} found') - return (stm32cube_path / - matching_ld_files[0] if matching_ld_files else None, - stm32cube_path / - matching_icf_files[0] if matching_icf_files else None) + return ( + stm32cube_path / matching_ld_files[0] if matching_ld_files else None, + stm32cube_path / matching_icf_files[0] if matching_icf_files else None, + ) -def find_startup_file(product_name: str, files: List[str], - stm32cube_path: pathlib.Path) -> pathlib.Path: +def find_startup_file( + product_name: str, files: List[str], stm32cube_path: pathlib.Path +) -> pathlib.Path: """Finds startup file for the given product. Searches for gcc startup files. @@ -192,8 +206,12 @@ def find_startup_file(product_name: str, files: List[str], # same filenames, so this looks for a 'gcc' folder in the path. matching_startup_files = list( filter( - lambda f: '/gcc/' in f and f.endswith('.s') and match_filename( - product_name, f), files)) + lambda f: '/gcc/' in f + and f.endswith('.s') + and match_filename(product_name, f), + files, + ) + ) if not matching_startup_files: raise ValueError(f'No matching startup file found for {product_name}') @@ -201,7 +219,8 @@ def find_startup_file(product_name: str, files: List[str], return stm32cube_path / matching_startup_files[0] raise ValueError( - f'Multiple matching startup files found for {product_name}') + f'Multiple matching startup files found for {product_name}' + ) _INCLUDE_DIRS = [ @@ -219,8 +238,8 @@ def get_include_dirs(stm32cube_path: pathlib.Path) -> List[pathlib.Path]: def get_sources_and_headers( - files: List[str], - stm32cube_path: pathlib.Path) -> Tuple[List[str], List[str]]: + files: List[str], stm32cube_path: pathlib.Path +) -> Tuple[List[str], List[str]]: """Gets list of all sources and headers needed to build the stm32cube hal. Args: @@ -234,24 +253,33 @@ def get_sources_and_headers( `headers` is a list of absolute paths to all needed headers """ source_files = filter( - lambda f: f.startswith('hal_driver/Src') and f.endswith('.c') and - 'template' not in f, files) + lambda f: f.startswith('hal_driver/Src') + and f.endswith('.c') + and 'template' not in f, + files, + ) header_files = filter( - lambda f: (any(f.startswith(dir) - for dir in _INCLUDE_DIRS)) and f.endswith('.h'), files) + lambda f: (any(f.startswith(dir) for dir in _INCLUDE_DIRS)) + and f.endswith('.h'), + files, + ) rebase_path = lambda f: str(stm32cube_path / f) - return list(map(rebase_path, - source_files)), list(map(rebase_path, header_files)) + return list(map(rebase_path, source_files)), list( + map(rebase_path, header_files) + ) def parse_files_txt(stm32cube_path: pathlib.Path) -> List[str]: """Reads files.txt into list.""" with open(stm32cube_path / 'files.txt', 'r') as files: return list( - filter(lambda x: not x.startswith('#'), - map(lambda f: f.strip(), files.readlines()))) + filter( + lambda x: not x.startswith('#'), + map(lambda f: f.strip(), files.readlines()), + ) + ) def _gn_str_out(name: str, val: Any): @@ -261,8 +289,9 @@ def _gn_str_out(name: str, val: Any): def _gn_list_str_out(name: str, val: List[Any]): """Outputs list of strings in GN format with correct escaping.""" - list_str = ','.join('"' + str(x).replace('"', r'\"').replace('$', r'\$') + - '"' for x in val) + list_str = ','.join( + '"' + str(x).replace('"', r'\"').replace('$', r'\$') + '"' for x in val + ) print(f'{name} = [{list_str}]') @@ -275,11 +304,13 @@ def find_files(stm32cube_path: pathlib.Path, product_str: str, init: bool): (family, defines, name) = parse_product_str(product_str) family_header_path = list( - filter(lambda p: p.endswith(f'/{family}.h'), headers))[0] + filter(lambda p: p.endswith(f'/{family}.h'), headers) + )[0] with open(family_header_path, 'rb') as family_header: - family_header_str = family_header.read().decode('utf-8', - errors='ignore') + family_header_str = family_header.read().decode( + 'utf-8', errors='ignore' + ) define = select_define(defines, family_header_str) @@ -291,8 +322,9 @@ def find_files(stm32cube_path: pathlib.Path, product_str: str, init: bool): if init: startup_file_path = find_startup_file(name, file_list, stm32cube_path) - gcc_linker, iar_linker = find_linker_files(name, file_list, - stm32cube_path) + gcc_linker, iar_linker = find_linker_files( + name, file_list, stm32cube_path + ) _gn_str_out('startup', startup_file_path) _gn_str_out('gcc_linker', gcc_linker if gcc_linker else '') diff --git a/pw_stm32cube_build/py/pw_stm32cube_build/gen_file_list.py b/pw_stm32cube_build/py/pw_stm32cube_build/gen_file_list.py index a685937cf..8a64fb8a3 100644 --- a/pw_stm32cube_build/py/pw_stm32cube_build/gen_file_list.py +++ b/pw_stm32cube_build/py/pw_stm32cube_build/gen_file_list.py @@ -43,9 +43,10 @@ def gen_file_list(stm32cube_dir: pathlib.Path): file_paths.extend(stm32cube_dir.glob("**/*.ld")) file_paths.extend(stm32cube_dir.glob("**/*.icf")) - #TODO(vars): allow arbitrary path for generated file list + # TODO(vars): allow arbitrary path for generated file list with open(stm32cube_dir / "files.txt", "w") as out_file: out_file.write('# Generated by pw_stm32cube_build/gen_file_list\n') for file_path in file_paths: out_file.write( - file_path.relative_to(stm32cube_dir).as_posix() + '\n') + file_path.relative_to(stm32cube_dir).as_posix() + '\n' + ) diff --git a/pw_stm32cube_build/py/pw_stm32cube_build/icf_to_ld.py b/pw_stm32cube_build/py/pw_stm32cube_build/icf_to_ld.py index 662ea2f18..ef18a642b 100644 --- a/pw_stm32cube_build/py/pw_stm32cube_build/icf_to_ld.py +++ b/pw_stm32cube_build/py/pw_stm32cube_build/icf_to_ld.py @@ -51,23 +51,25 @@ def parse_icf(icf_file: str) -> Tuple[Dict, Dict]: if tokens[1] == 'symbol': symbols[tokens[2]] = tokens[4].strip(';') elif tokens[1] == 'region': - regions[tokens[2].split('_')[0]] = (tokens[5], - tokens[7].strip('];')) + regions[tokens[2].split('_')[0]] = ( + tokens[5], + tokens[7].strip('];'), + ) elif tokens[1] == 'block': blocks[tokens[2]] = { tokens[4]: tokens[6].strip(','), - tokens[7]: tokens[9] + tokens[7]: tokens[9], } parsed_regions = { - region: (symbols[start] if start in symbols else start, - symbols[end] if end in symbols else end) + region: ( + symbols[start] if start in symbols else start, + symbols[end] if end in symbols else end, + ) for region, (start, end) in regions.items() } parsed_blocks = { - name: - {k: symbols[v] if v in symbols else v - for k, v in fields.items()} + name: {k: symbols[v] if v in symbols else v for k, v in fields.items()} for name, fields in blocks.items() } diff --git a/pw_stm32cube_build/py/pw_stm32cube_build/inject_init.py b/pw_stm32cube_build/py/pw_stm32cube_build/inject_init.py index 8a68cb659..26448891a 100644 --- a/pw_stm32cube_build/py/pw_stm32cube_build/inject_init.py +++ b/pw_stm32cube_build/py/pw_stm32cube_build/inject_init.py @@ -42,8 +42,11 @@ def add_pre_main_init(startup: str) -> str: if match is None: raise ValueError("`bl main` not found in startup script") - return startup[:match.start( - )] + '\nbl pw_stm32cube_Init' + startup[match.start():] + return ( + startup[: match.start()] + + '\nbl pw_stm32cube_Init' + + startup[match.start() :] + ) def inject_init(startup_in: pathlib.Path, startup_out: Optional[pathlib.Path]): @@ -55,8 +58,7 @@ def inject_init(startup_in: pathlib.Path, startup_out: Optional[pathlib.Path]): If None, output startup script printed to stdout """ with open(startup_in, 'rb') as startup_in_file: - startup_in_str = startup_in_file.read().decode('utf-8', - errors='ignore') + startup_in_str = startup_in_file.read().decode('utf-8', errors='ignore') startup_out_str = add_pre_main_init(startup_in_str) diff --git a/pw_stm32cube_build/py/tests/find_files_test.py b/pw_stm32cube_build/py/tests/find_files_test.py index 882c3bfea..364e2b081 100644 --- a/pw_stm32cube_build/py/tests/find_files_test.py +++ b/pw_stm32cube_build/py/tests/find_files_test.py @@ -22,6 +22,7 @@ from pw_stm32cube_build import find_files class ParseProductStringTest(unittest.TestCase): """parse_product_str tests.""" + def test_start_with_stm32(self): with self.assertRaises(ValueError): find_files.parse_product_str('f439zit') @@ -59,13 +60,13 @@ class ParseProductStringTest(unittest.TestCase): self.assertEqual(name, 'stm32f439xi') def test_stm32f439zit6u(self): - (family, defines, - name) = find_files.parse_product_str('stm32f439zit6u') + (family, defines, name) = find_files.parse_product_str('stm32f439zit6u') self.assertEqual(family, 'stm32f4xx') self.assertEqual( defines, - {'STM32F439xx', 'STM32F439Zx', 'STM32F439xI', 'STM32F439ZI'}) + {'STM32F439xx', 'STM32F439Zx', 'STM32F439xI', 'STM32F439ZI'}, + ) self.assertEqual(name, 'stm32f439zit6u') def test_stm32l552zet(self): @@ -74,7 +75,8 @@ class ParseProductStringTest(unittest.TestCase): self.assertEqual(family, 'stm32l5xx') self.assertEqual( defines, - {'STM32L552xx', 'STM32L552Zx', 'STM32L552xE', 'STM32L552ZE'}) + {'STM32L552xx', 'STM32L552Zx', 'STM32L552xE', 'STM32L552ZE'}, + ) self.assertEqual(name, 'stm32l552zet') def test_stm32l552xc(self): @@ -94,6 +96,7 @@ class ParseProductStringTest(unittest.TestCase): class SelectDefineTest(unittest.TestCase): """select_define tests.""" + def test_stm32f412zx_not_found(self): with self.assertRaises(ValueError): find_files.select_define({'STM32F412xx', 'STM32F412Zx'}, "") @@ -101,77 +104,101 @@ class SelectDefineTest(unittest.TestCase): def test_stm32f412zx_found(self): define = find_files.select_define( {'STM32F412xx', 'STM32F412Zx'}, - "asdf\nfdas\n#if defined(STM32F412Zx)\n") + "asdf\nfdas\n#if defined(STM32F412Zx)\n", + ) self.assertEqual(define, 'STM32F412Zx') def test_stm32f412zx_multiple_found(self): with self.assertRaises(ValueError): - find_files.select_define({ - 'STM32F412xx', 'STM32F412Zx' - }, "asdf\n#if defined (STM32F412xx)\n#elif defined(STM32F412Zx)\n") + find_files.select_define( + {'STM32F412xx', 'STM32F412Zx'}, + "asdf\n#if defined (STM32F412xx)\n#elif defined(STM32F412Zx)\n", + ) class MatchFilenameTest(unittest.TestCase): """match_filename tests.""" + def test_stm32f412zx(self): # Match should fail if product name is not specific enough self.assertTrue( - find_files.match_filename('stm32f412zx', 'stm32f412zx_flash.icf')) + find_files.match_filename('stm32f412zx', 'stm32f412zx_flash.icf') + ) self.assertFalse( - find_files.match_filename('stm32f412xx', 'stm32f412zx_flash.icf')) + find_files.match_filename('stm32f412xx', 'stm32f412zx_flash.icf') + ) self.assertTrue( - find_files.match_filename('stm32f412zx', 'startup_stm32f412zx.s')) + find_files.match_filename('stm32f412zx', 'startup_stm32f412zx.s') + ) self.assertFalse( - find_files.match_filename('stm32f412xx', 'startup_stm32f429zx.s')) + find_files.match_filename('stm32f412xx', 'startup_stm32f429zx.s') + ) def test_stm32f439xx(self): self.assertTrue( - find_files.match_filename('stm32f439xx', 'stm32f439xx_flash.icf')) + find_files.match_filename('stm32f439xx', 'stm32f439xx_flash.icf') + ) self.assertFalse( - find_files.match_filename('stm32f439xx', 'stm32f429xx_flash.icf')) + find_files.match_filename('stm32f439xx', 'stm32f429xx_flash.icf') + ) self.assertTrue( - find_files.match_filename('stm32f439xx', 'startup_stm32f439xx.s')) + find_files.match_filename('stm32f439xx', 'startup_stm32f439xx.s') + ) self.assertFalse( - find_files.match_filename('stm32f439xx', 'startup_stm32f429xx.s')) + find_files.match_filename('stm32f439xx', 'startup_stm32f429xx.s') + ) def test_stm32f439xi(self): self.assertTrue( - find_files.match_filename('stm32f439xi', 'stm32f439xx_flash.icf')) + find_files.match_filename('stm32f439xi', 'stm32f439xx_flash.icf') + ) self.assertFalse( - find_files.match_filename('stm32f439xi', 'stm32f429xx_flash.icf')) + find_files.match_filename('stm32f439xi', 'stm32f429xx_flash.icf') + ) self.assertTrue( - find_files.match_filename('stm32f439xi', 'startup_stm32f439xx.s')) + find_files.match_filename('stm32f439xi', 'startup_stm32f439xx.s') + ) self.assertFalse( - find_files.match_filename('stm32f439xi', 'startup_stm32f429xx.s')) + find_files.match_filename('stm32f439xi', 'startup_stm32f429xx.s') + ) def test_stm32l552zet(self): self.assertTrue( - find_files.match_filename('stm32l552zet', 'STM32L552xE_FLASH.ld')) + find_files.match_filename('stm32l552zet', 'STM32L552xE_FLASH.ld') + ) self.assertTrue( - find_files.match_filename('stm32l552zet', 'STM32L552xx_FLASH.ld')) + find_files.match_filename('stm32l552zet', 'STM32L552xx_FLASH.ld') + ) self.assertFalse( - find_files.match_filename('stm32l552zet', 'STM32L552xC_FLASH.ld')) + find_files.match_filename('stm32l552zet', 'STM32L552xC_FLASH.ld') + ) self.assertTrue( - find_files.match_filename('stm32l552zet', 'stm32l552xe_flash.icf')) + find_files.match_filename('stm32l552zet', 'stm32l552xe_flash.icf') + ) self.assertFalse( - find_files.match_filename('stm32l552zet', 'stm32l552xc_flash.icf')) + find_files.match_filename('stm32l552zet', 'stm32l552xc_flash.icf') + ) self.assertTrue( - find_files.match_filename('stm32l552zet', 'startup_stm32l552xx.s')) + find_files.match_filename('stm32l552zet', 'startup_stm32l552xx.s') + ) self.assertFalse( - find_files.match_filename('stm32l552zet', 'startup_stm32l562xx.s')) + find_files.match_filename('stm32l552zet', 'startup_stm32l562xx.s') + ) class FindLinkerFilesTest(unittest.TestCase): """find_linker_files tests.""" + TEST_PATH = pathlib.Path('/test/path') def test_stm32f439xx(self): files = [ 'path/to/stm32f439xx_flash.icf', - 'other/path/to/stm32f439xx_sram.icf' + 'other/path/to/stm32f439xx_sram.icf', ] gcc_linker, iar_linker = find_files.find_linker_files( - 'stm32f439xx', files, self.TEST_PATH) + 'stm32f439xx', files, self.TEST_PATH + ) self.assertEqual(gcc_linker, None) self.assertEqual(iar_linker, self.TEST_PATH / files[0]) @@ -183,7 +210,8 @@ class FindLinkerFilesTest(unittest.TestCase): 'path/to/STM32F439xx_FLASH.ld', ] gcc_linker, iar_linker = find_files.find_linker_files( - 'stm32f439xx', files, self.TEST_PATH) + 'stm32f439xx', files, self.TEST_PATH + ) self.assertEqual(gcc_linker, self.TEST_PATH / files[2]) self.assertEqual(iar_linker, self.TEST_PATH / files[0]) @@ -225,7 +253,8 @@ class FindLinkerFilesTest(unittest.TestCase): 'gcc/linker/STM32L552xE_FLASH.ld', ] gcc_linker, iar_linker = find_files.find_linker_files( - 'stm32l552xe', files, self.TEST_PATH) + 'stm32l552xe', files, self.TEST_PATH + ) self.assertEqual(gcc_linker, self.TEST_PATH / files[-1]) self.assertEqual(iar_linker, self.TEST_PATH / files[2]) @@ -233,6 +262,7 @@ class FindLinkerFilesTest(unittest.TestCase): class FindStartupFileTest(unittest.TestCase): """find_startup_file tests.""" + TEST_PATH = pathlib.Path('/test/path') def test_stm32f439xx_none_found(self): @@ -251,8 +281,9 @@ class FindStartupFileTest(unittest.TestCase): 'path/iar/startup_stm32f439xx.s', 'path/gcc/startup_stm32f439xx.s', ] - startup_file = find_files.find_startup_file('stm32f439xx', files, - self.TEST_PATH) + startup_file = find_files.find_startup_file( + 'stm32f439xx', files, self.TEST_PATH + ) self.assertEqual(startup_file, self.TEST_PATH / files[3]) @@ -269,6 +300,7 @@ class FindStartupFileTest(unittest.TestCase): class GetSourceAndHeadersTest(unittest.TestCase): """test_sources_and_headers tests.""" + def test_sources_and_headers(self): files = [ 'random/header.h', @@ -285,18 +317,26 @@ class GetSourceAndHeadersTest(unittest.TestCase): path = pathlib.Path('/test/path/to/stm32cube') sources, headers = find_files.get_sources_and_headers(files, path) self.assertSetEqual( - set([ - str(path / 'hal_driver/Src/stm32f4xx_hal_adc.c'), - str(path / 'hal_driver/Src/stm32f4xx_hal_eth.c'), - ]), set(sources)) + set( + [ + str(path / 'hal_driver/Src/stm32f4xx_hal_adc.c'), + str(path / 'hal_driver/Src/stm32f4xx_hal_eth.c'), + ] + ), + set(sources), + ) self.assertSetEqual( - set([ - str(path / 'cmsis_core/Include/core_cm4.h'), - str(path / 'cmsis_device/Include/stm32f4xx.h'), - str(path / 'cmsis_device/Include/stm32f439xx.h'), - str(path / 'hal_driver/Inc/stm32f4xx_hal_eth.h'), - str(path / 'hal_driver/Inc/stm32f4xx_hal.h'), - ]), set(headers)) + set( + [ + str(path / 'cmsis_core/Include/core_cm4.h'), + str(path / 'cmsis_device/Include/stm32f4xx.h'), + str(path / 'cmsis_device/Include/stm32f439xx.h'), + str(path / 'hal_driver/Inc/stm32f4xx_hal_eth.h'), + str(path / 'hal_driver/Inc/stm32f4xx_hal.h'), + ] + ), + set(headers), + ) if __name__ == '__main__': diff --git a/pw_stm32cube_build/py/tests/icf_to_ld_test.py b/pw_stm32cube_build/py/tests/icf_to_ld_test.py index 3d76d9163..892eb5fc7 100644 --- a/pw_stm32cube_build/py/tests/icf_to_ld_test.py +++ b/pw_stm32cube_build/py/tests/icf_to_ld_test.py @@ -21,6 +21,7 @@ from pw_stm32cube_build import icf_to_ld class ParseIcfTest(unittest.TestCase): """parse_icf tests.""" + TEST_ICF_1 = """ /*test comments*/ // some other comments @@ -106,73 +107,79 @@ place in RAM_region { readwrite, 'ROM': ('0x08000000', '0x081FFFFF'), 'RAM': ('0x20000000', '0x2002FFFF'), 'CCMRAM': ('0x10000000', '0x1000FFFF'), - }, regions) + }, + regions, + ) self.assertEqual( { - 'CSTACK': { - 'alignment': '8', - 'size': '0x400' - }, - 'HEAP': { - 'alignment': '8', - 'size': '0x200' - }, - }, blocks) + 'CSTACK': {'alignment': '8', 'size': '0x400'}, + 'HEAP': {'alignment': '8', 'size': '0x200'}, + }, + blocks, + ) class IcfRegionsToLdRegionsTest(unittest.TestCase): """icf_regions_to_ld_regions tests.""" + def test_icf_region(self): - ld_regions = icf_to_ld.icf_regions_to_ld_regions({ - 'ROM': ('0x08000000', '0x081FFFFF'), - 'RAM': ('0x20000000', '0x2002FFFF'), - 'CCMRAM': ('0x10000000', '0x1000FFFF'), - }) + ld_regions = icf_to_ld.icf_regions_to_ld_regions( + { + 'ROM': ('0x08000000', '0x081FFFFF'), + 'RAM': ('0x20000000', '0x2002FFFF'), + 'CCMRAM': ('0x10000000', '0x1000FFFF'), + } + ) self.assertEqual( { 'FLASH': ('0x08000000', '2048K'), 'RAM': ('0x20000000', '192K'), 'CCMRAM': ('0x10000000', '64K'), - }, ld_regions) + }, + ld_regions, + ) def test_icf_region_off_by_one(self): - ld_regions = icf_to_ld.icf_regions_to_ld_regions({ - 'ROM': ('0x08000000', '0x080FFFFF'), - 'RAM': ('0x20000000', '0x20020000'), - }) + ld_regions = icf_to_ld.icf_regions_to_ld_regions( + { + 'ROM': ('0x08000000', '0x080FFFFF'), + 'RAM': ('0x20000000', '0x20020000'), + } + ) self.assertEqual( { 'FLASH': ('0x08000000', '1024K'), 'RAM': ('0x20000000', '128K'), - }, ld_regions) + }, + ld_regions, + ) class CreateLdTest(unittest.TestCase): """create_ld tests.""" + def test_create_ld(self): ld_str = icf_to_ld.create_ld( { 'FLASH': ('0x08000000', '2048K'), 'RAM': ('0x20000000', '192K'), 'CCMRAM': ('0x10000000', '64K'), - }, { - 'CSTACK': { - 'alignment': '8', - 'size': '0x400' - }, - 'HEAP': { - 'alignment': '8', - 'size': '0x200' - }, - }) + }, + { + 'CSTACK': {'alignment': '8', 'size': '0x400'}, + 'HEAP': {'alignment': '8', 'size': '0x200'}, + }, + ) self.assertTrue( - 'RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 192K' in ld_str) + 'RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 192K' in ld_str + ) self.assertTrue( - 'FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 2048K' in ld_str) + 'FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 2048K' in ld_str + ) if __name__ == '__main__': diff --git a/pw_stm32cube_build/py/tests/inject_init_test.py b/pw_stm32cube_build/py/tests/inject_init_test.py index b08cf359e..5ce2dd6ac 100644 --- a/pw_stm32cube_build/py/tests/inject_init_test.py +++ b/pw_stm32cube_build/py/tests/inject_init_test.py @@ -21,79 +21,92 @@ from pw_stm32cube_build import inject_init class AddPreMainInitTest(unittest.TestCase): """add_pre_main_init tests.""" - def test_spaces(self): - startup = '\n'.join([ - '/* Call the clock system intitialization function.*/', - ' bl SystemInit ', - '/* Call static constructors */', - ' bl __libc_init_array', - '/* Call the application\'s entry point.*/', - ' bl main', - ' bx lr ', - '.size Reset_Handler, .-Reset_Handler', - ]) - - new_startup = inject_init.add_pre_main_init(startup) - self.assertEqual( - new_startup, '\n'.join([ + def test_spaces(self): + startup = '\n'.join( + [ '/* Call the clock system intitialization function.*/', ' bl SystemInit ', '/* Call static constructors */', ' bl __libc_init_array', '/* Call the application\'s entry point.*/', - 'bl pw_stm32cube_Init', ' bl main', ' bx lr ', '.size Reset_Handler, .-Reset_Handler', - ])) - - def test_tabs(self): - startup = '\n'.join([ - 'LoopFillZerobss:', - ' ldr r3, = _ebss', - ' cmp r2, r3', - ' bcc FillZerobss', - '' - '/* Call static constructors */', - ' bl __libc_init_array', - '/* Call the application\'s entry point.*/', - ' bl main', - '', - 'LoopForever:', - ' b LoopForever', - ]) + ] + ) new_startup = inject_init.add_pre_main_init(startup) self.assertEqual( - new_startup, '\n'.join([ + new_startup, + '\n'.join( + [ + '/* Call the clock system intitialization function.*/', + ' bl SystemInit ', + '/* Call static constructors */', + ' bl __libc_init_array', + '/* Call the application\'s entry point.*/', + 'bl pw_stm32cube_Init', + ' bl main', + ' bx lr ', + '.size Reset_Handler, .-Reset_Handler', + ] + ), + ) + + def test_tabs(self): + startup = '\n'.join( + [ 'LoopFillZerobss:', ' ldr r3, = _ebss', ' cmp r2, r3', ' bcc FillZerobss', - '' '/* Call static constructors */', ' bl __libc_init_array', '/* Call the application\'s entry point.*/', - 'bl pw_stm32cube_Init', ' bl main', '', 'LoopForever:', ' b LoopForever', - ])) + ] + ) + + new_startup = inject_init.add_pre_main_init(startup) + + self.assertEqual( + new_startup, + '\n'.join( + [ + 'LoopFillZerobss:', + ' ldr r3, = _ebss', + ' cmp r2, r3', + ' bcc FillZerobss', + '/* Call static constructors */', + ' bl __libc_init_array', + '/* Call the application\'s entry point.*/', + 'bl pw_stm32cube_Init', + ' bl main', + '', + 'LoopForever:', + ' b LoopForever', + ] + ), + ) def test_main_not_found(self): - startup = '\n'.join([ - '/* Call the clock system intitialization function.*/', - ' bl SystemInit ', - '/* Call static constructors */', - ' bl __libc_init_array', - '/* Call the application\'s entry point.*/', - ' bl application_entry', - ' bx lr ', - '.size Reset_Handler, .-Reset_Handler', - ]) + startup = '\n'.join( + [ + '/* Call the clock system intitialization function.*/', + ' bl SystemInit ', + '/* Call static constructors */', + ' bl __libc_init_array', + '/* Call the application\'s entry point.*/', + ' bl application_entry', + ' bx lr ', + '.size Reset_Handler, .-Reset_Handler', + ] + ) with self.assertRaises(ValueError): inject_init.add_pre_main_init(startup) |