diff options
author | Craig Hesling <hesling@chromium.org> | 2022-10-05 15:43:20 -0400 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2022-10-11 18:51:02 +0000 |
commit | bd7adc27d9800ed6d32dab1aad764502ddc82b2c (patch) | |
tree | ff06a4f1777be64b51147ad7cecd1709e9c738d0 | |
parent | 93929652936de91fb7bbc20fe4c959425327d585 (diff) | |
download | chrome-ec-bd7adc27d9800ed6d32dab1aad764502ddc82b2c.tar.gz |
util: Add --os support to clangd_config.py
BRANCH=none
BUG=b:236389226,b:176500425
TEST=./util/clangd_config.py bloonchipper
TEST=./util/clangd_config.py --os auto bloonchipper
TEST=./util/clangd_config.py --os ec bloonchipper
TEST=./util/clangd_config.py --os zephyr bloonchipper
# Expect error
TEST=./util/clangd_config.py herobrine
# Expect error
Signed-off-by: Craig Hesling <hesling@chromium.org>
Change-Id: Iee6c7d8626f046f4950bce48f5a5be0b84f4eeeb
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3931851
Reviewed-by: Andrea Grandi <agrandi@google.com>
-rwxr-xr-x | util/clangd_config.py | 67 |
1 files changed, 58 insertions, 9 deletions
diff --git a/util/clangd_config.py b/util/clangd_config.py index b99888dbfa..96bcb7ebf1 100755 --- a/util/clangd_config.py +++ b/util/clangd_config.py @@ -17,8 +17,8 @@ from pathlib import Path from typing import List, Optional -def fetch_boards(ec_root: Path) -> Optional[List[str]]: - """Return a list of boards.""" +def ec_fetch_boards(ec_root: Path) -> Optional[List[str]]: + """Return a list of EC boards seen.""" base = str(ec_root) + "/board/" @@ -29,9 +29,21 @@ def fetch_boards(ec_root: Path) -> Optional[List[str]]: return [b[len(base) :] for b in boards] +def zephyr_fetch_projects(ec_root: Path) -> Optional[List[str]]: + """Return a list of Zephyr projects seen.""" + + base = str(ec_root) + "/zephyr/projects/" + + boards = glob.glob(base + "*") + if boards is None: + return None + + return [b[len(base) :] for b in boards] + + # We would use image: Literal["RW", "RO"], but it was only added in Python 3.8. -def build(ec_root: Path, board: str, image: str) -> Optional[Path]: - """Build the correct compile_commands.json.""" +def ec_build(ec_root: Path, board: str, image: str) -> Optional[Path]: + """Build the correct compile_commands.json for EC board/image.""" target = Path(f"build/{board}/{image}/compile_commands.json") cmd = [ @@ -49,6 +61,13 @@ def build(ec_root: Path, board: str, image: str) -> Optional[Path]: return target +# We would use image: Literal["RW", "RO"], but it was only added in Python 3.8. +def zephyr_build(ec_root: Path, board: str, image: str) -> Optional[Path]: + """Build the correct compile_commands.json for Zephyr board/image""" + + raise NotImplementedError("Zephyr is currently unsupported.") + + def copy(ec_root: Path, target: Path) -> None: """Copy the correct compile_commands.json to the EC root.""" @@ -87,17 +106,47 @@ def main(argv: List[str]) -> int: parser.add_argument( "-s", help="symbolically link instead of copying.", action="store_true" ) + parser.add_argument( + "--os", + help="OS used to build board", + nargs="?", + choices=["auto", "ec", "zephyr"], + default="auto", + type=str.lower, + ) args = parser.parse_args(argv) ec_root = Path(os.path.relpath(os.path.dirname(__file__) + "/..")) - boards = fetch_boards(ec_root) - if boards is None: - parser.error("Can't find boards directory.") - if not args.board in boards: + ec_boards = ec_fetch_boards(ec_root) + if ec_boards is None: + parser.error("Can't find EC boards directory.") + + zephyr_projects = zephyr_fetch_projects(ec_root) + if zephyr_projects is None: + parser.error("Can't find Zephyr projects directory.") + + is_in_ec = args.board in ec_boards + is_in_zephyr = args.board in zephyr_projects + + if not is_in_ec and not is_in_zephyr: parser.error(f"Board '{args.board}' does not exist.") - target = build(ec_root, args.board, args.image.upper()) + # When "os" is auto, try Zephyr first, fall back to EC. + os_selection = args.os + if os_selection == "auto": + if is_in_zephyr: + os_selection = "zephyr" + else: + os_selection = "ec" + + print(f"Configuring for {os_selection}.") + + if os_selection == "ec": + target = ec_build(ec_root, args.board, args.image.upper()) + else: + target = zephyr_build(ec_root, args.board, args.image.upper()) + if target is None: print("Failed to build compile_commands.json") return 1 |