From 9c21648e3afe436692f891296e3183f2bf9c2caa Mon Sep 17 00:00:00 2001 From: juiwenchen Date: Wed, 8 Oct 2025 13:29:15 +0200 Subject: [PATCH 1/2] add query cmds and aspects --- inspections.rst | 28 ++++++++++++++++++++++++++++ print.bzl | 31 +++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 inspections.rst create mode 100644 print.bzl diff --git a/inspections.rst b/inspections.rst new file mode 100644 index 0000000..6a8b286 --- /dev/null +++ b/inspections.rst @@ -0,0 +1,28 @@ +## Experiments with Bazel queries and aspects to inspect targets + + + +- Exploring targets + +.. code-block:: bash + + bazel query //tools/sphinx/codelinks/... + +- Find all ``codelinks_analyse`` rule type + +.. code-block:: bash + + bazel query 'kind(codelinks_analyse, //...)' + +- Print out the source files of ``codelinks_analyse`` target in ``//projects/acdc/ac`` + +.. code-block:: bash + + bazel build //projects/acdc/ac:codelinks_analyse --aspects print.bzl%print_aspect + +- print out the source files of all ``codelinks_analyse`` targets used in the repo + +.. code-block:: bash + + bazel build $(bazel query "kind(codelinks_analyse, //...)") --aspects //:print.bzl%print_aspect + diff --git a/print.bzl b/print.bzl new file mode 100644 index 0000000..63f7e6d --- /dev/null +++ b/print.bzl @@ -0,0 +1,31 @@ +# Reference: https://bazel.build/extending/aspects + +def _print_aspect_impl(target, ctx): + + print("+ print_aspect_impl") + print("Target: " + str(target.label)) + print(ctx.rule.kind) + target_kind = getattr(ctx.attr, "target_kind", "") + print("Target kind from attr: " + target_kind) + # Make sure the rule has a srcs attribute. + if target_kind != ctx.rule.kind: + return [] + + if hasattr(ctx.rule.attr, 'srcs'): + # Iterate through the files that make up the sources and + # print their paths. + for src in ctx.rule.attr.srcs: + for f in src.files.to_list(): + print(f.path) + print("- print_aspect_impl") + return [] + +print_aspect = aspect( + implementation = _print_aspect_impl, + attr_aspects = ['srcs'], # the edge to track + attrs = { + 'target_kind': attr.string( + default = 'codelinks_analyse',), + } + # required_providers = [CcInfo], +) \ No newline at end of file From 8b0e6e985fdb4ba7e436a005446813f35cac5a66 Mon Sep 17 00:00:00 2001 From: juiwenchen Date: Fri, 10 Oct 2025 16:37:26 +0200 Subject: [PATCH 2/2] updated --- inspections.rst | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/inspections.rst b/inspections.rst index 6a8b286..39f231c 100644 --- a/inspections.rst +++ b/inspections.rst @@ -1,28 +1,38 @@ -## Experiments with Bazel queries and aspects to inspect targets - - +Experiments with Bazel queries and aspects to inspect targets +============================================================= - Exploring targets -.. code-block:: bash + .. code-block:: bash - bazel query //tools/sphinx/codelinks/... + bazel query //tools/sphinx/codelinks/... - Find all ``codelinks_analyse`` rule type -.. code-block:: bash + .. code-block:: bash - bazel query 'kind(codelinks_analyse, //...)' + bazel query 'kind(codelinks_analyse, //...)' - Print out the source files of ``codelinks_analyse`` target in ``//projects/acdc/ac`` -.. code-block:: bash + .. code-block:: bash - bazel build //projects/acdc/ac:codelinks_analyse --aspects print.bzl%print_aspect + bazel build //projects/acdc/ac:codelinks_analyse --aspects print.bzl%print_aspect - print out the source files of all ``codelinks_analyse`` targets used in the repo -.. code-block:: bash + .. code-block:: bash + + bazel build $(bazel query "kind(codelinks_analyse, //...)") --aspects //:print.bzl%print_aspect + +- Find all targets that depends on file ``projects/acdc/ac:src/ac.c`` + + .. code-block:: bash - bazel build $(bazel query "kind(codelinks_analyse, //...)") --aspects //:print.bzl%print_aspect + bazel query "rdeps(//..., //projects/acdc/ac:src/ac.c)" +- Find which targets of a specific kind depend on a certain file + + .. code-block:: bash + + bazel query "kind(codelinks_analyse, rdeps(//..., //projects/acdc/ac:codelinks.toml))"