diff --git a/inspections.rst b/inspections.rst new file mode 100644 index 0000000..39f231c --- /dev/null +++ b/inspections.rst @@ -0,0 +1,38 @@ +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 + +- Find all targets that depends on file ``projects/acdc/ac:src/ac.c`` + + .. code-block:: bash + + 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))" 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