|
| 1 | +""" |
| 2 | +Utility script to migrate SYS_INIT() calls from the old signature |
| 3 | +``int (*init_fn)(const struct device *dev)`` to ``int (*init_fn)(void)``. |
| 4 | +
|
| 5 | +.. warning:: |
| 6 | + Review the output carefully! This script may not cover all cases! |
| 7 | +
|
| 8 | +Usage:: |
| 9 | +
|
| 10 | + python $ZEPHYR_BASE/scripts/utils/migrate_sys_init.py \ |
| 11 | + -p path/to/zephyr-based-project |
| 12 | +
|
| 13 | +Copyright (c) 2022 Nordic Semiconductor ASA |
| 14 | +SPDX-License-Identifier: Apache-2.0 |
| 15 | +""" |
| 16 | + |
| 17 | +import argparse |
| 18 | +from pathlib import Path |
| 19 | +import re |
| 20 | + |
| 21 | + |
| 22 | +ZEPHYR_BASE = Path(__file__).parents[2] |
| 23 | + |
| 24 | + |
| 25 | +def update_sys_init(project, dry_run): |
| 26 | + for p in project.glob("**/*"): |
| 27 | + if not p.is_file() or not p.suffix or p.suffix[1:] not in ("c", "cpp"): |
| 28 | + continue |
| 29 | + |
| 30 | + with open(p) as f: |
| 31 | + lines = f.readlines() |
| 32 | + |
| 33 | + sys_inits = [] |
| 34 | + for line in lines: |
| 35 | + m = re.match(r"^SYS_INIT\(([A-Za-z0-9_]+),.*", line) |
| 36 | + if m: |
| 37 | + sys_inits.append(m.group(1)) |
| 38 | + continue |
| 39 | + |
| 40 | + m = re.match(r"^SYS_INIT_NAMED\([A-Za-z0-9_]+,\s?([A-Za-z0-9_]+).*", line) |
| 41 | + if m: |
| 42 | + sys_inits.append(m.group(1)) |
| 43 | + continue |
| 44 | + |
| 45 | + if not sys_inits: |
| 46 | + continue |
| 47 | + |
| 48 | + arg = None |
| 49 | + content = "" |
| 50 | + update = False |
| 51 | + for line in lines: |
| 52 | + m = re.match( |
| 53 | + r"(.*)int (" |
| 54 | + + "|".join(sys_inits) |
| 55 | + + r")\(const\s+struct\s+device\s+\*\s?(.*)\)(.*)", |
| 56 | + line, |
| 57 | + ) |
| 58 | + if m: |
| 59 | + b, sys_init, arg, e = m.groups() |
| 60 | + content += f"{b}int {sys_init}(void){e}\n" |
| 61 | + update = True |
| 62 | + elif arg: |
| 63 | + m = re.match(r"^\s?ARG_UNUSED\(" + arg + r"\);.*$", line) |
| 64 | + if m: |
| 65 | + arg = None |
| 66 | + else: |
| 67 | + content += line |
| 68 | + else: |
| 69 | + content += line |
| 70 | + |
| 71 | + if update: |
| 72 | + print(f"Updating {p}{' (dry run)' if dry_run else ''}") |
| 73 | + if not dry_run: |
| 74 | + with open(p, "w") as f: |
| 75 | + f.write(content) |
| 76 | + |
| 77 | + |
| 78 | +if __name__ == "__main__": |
| 79 | + parser = argparse.ArgumentParser(allow_abbrev=False) |
| 80 | + parser.add_argument( |
| 81 | + "-p", "--project", type=Path, required=True, help="Zephyr-based project path" |
| 82 | + ) |
| 83 | + parser.add_argument("--dry-run", action="store_true", help="Dry run") |
| 84 | + args = parser.parse_args() |
| 85 | + |
| 86 | + update_sys_init(args.project, args.dry_run) |
0 commit comments