Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions dev_tools/configure_vscode_for_bazel.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ def parse_arguments(argv: Sequence[str] | None = None) -> argparse.Namespace:
action="store_false",
help="Do not generate the `launch.json` file.",
)
parser.add_argument(
"--additional-debug-arg",
type=str,
nargs="*",
default=[],
action="extend",
help="Additional arguments to pass to the bazel build command when building targets for the `launch.json`.",
)
# TODO(#80): use https://docs.python.org/3/library/argparse.html#argparse.BooleanOptionalAction with Python >= 3.9 # noqa: FIX002
parser.set_defaults(generate_launch_json=True)
parser.add_argument(
Expand All @@ -72,6 +80,14 @@ def parse_arguments(argv: Sequence[str] | None = None) -> argparse.Namespace:
action="store_true",
help="Don't ask for confirmation.",
)
parser.add_argument(
"--additional-compile-commands-arg",
type=str,
nargs="*",
default=[],
action="extend",
help="Additional arguments to pass to the `compile_commands.json` refresh template.",
)

return parser.parse_args(argv)

Expand Down Expand Up @@ -201,19 +217,18 @@ def update_launch_json(bazel_patterns: list[str], config_location: Path, force:
return False


def update_cc_build_file(bazel_patterns: list[str], config_location: Path, force: bool) -> bool: # noqa: FBT001
def update_cc_build_file(bazel_patterns: list[str], bazel_args: list[str], config_location: Path, force: bool) -> bool: # noqa: FBT001
if confirm_config_overwrite(config_location, force):
sep = "," + "\n" + " " * 24
args = " ".join(bazel_args)
targets = dict.fromkeys(bazel_patterns, args)
config_location.write_text(
textwrap.dedent(
f"""
load("@hedron_compile_commands//:refresh_compile_commands.bzl", "refresh_compile_commands")

refresh_compile_commands(
name = "refresh_compile_commands",
targets = [
{sep.join(repr(p) for p in bazel_patterns)}
],
targets = {targets},
)
"""
).strip()
Expand Down Expand Up @@ -243,15 +258,15 @@ def main() -> int:
if args.generate_launch_json:
if update_launch_json(args.bazel_pattern, vscode_dir / "launch.json", args.force):
logging.info("You can now run the debug target(s) in VS Code.")
recommended_actions.append(("bazel", "build", "--config=debug", *args.bazel_pattern))
recommended_actions.append(("bazel", "build", *args.additional_debug_arg, *args.bazel_pattern))
else:
logging.error("No executable targets were found, no `launch.json` file was generated.")

if args.generate_compile_commands and update_cc_build_file(
args.bazel_pattern, vscode_dir / "BUILD.bazel", args.force
args.bazel_pattern, args.additional_compile_commands_arg, vscode_dir / "BUILD.bazel", args.force
):
logging.info("You can now generate the `compile_commands.json` file.")
recommended_actions.append(("bazel", "build", *args.bazel_pattern))
recommended_actions.append(("bazel", "build", *args.additional_compile_commands_arg, *args.bazel_pattern))
recommended_actions.append(("bazel", "run", "//.vscode:refresh_compile_commands"))

if args.build:
Expand Down