Search code examples
bazelbazel-rules

How to see the native implementation of predefined rules in Bazel


I am learning currently Bazel and after going through a few tutorials I feel like I have some basic working knowledge with it. Now I would like to dig slightly deeper and see how some of the rules I am using, like android_binary, android_sdk_repository, etc, are internally defined.

So I headed to rules_android on Github, and checked this one for example: https://github.com/bazelbuild/rules_android/blob/master/android/android_sdk_repository.bzl, but as it can be seen from above, the rule definition delegates the implementation further to some native component. I checked out the source code, but I could not find anywhere where native is defined.

So what I am looking for is:

  • Some guidance on how to navigate the Bazel repo
  • And, specifically for the above example, where can I find the rule definition for native.android_sdk_repository?

Appreciate any tips.


Solution

  • The native rules are defined in two places:

    https://github.com/bazelbuild/bazel/tree/master/src/main/java/com/google/devtools/build/lib/rules

    https://github.com/bazelbuild/bazel/tree/master/src/main/java/com/google/devtools/build/lib/bazel/rules

    (the reason for lib/rules vs lib/bazel/rules is that the code under lib/rules is shared between Bazel and Google's internal version, while the code under lib/bazel/rules is Bazel only)

    The Android rules specifically are being re-written into Starlark, the pre-release code is in another branch: https://github.com/bazelbuild/rules_android/tree/pre-alpha

    and NDK integration into this repo: https://github.com/bazelbuild/rules_android_ndk

    android_sdk_repository specifically is here: https://github.com/bazelbuild/bazel/blob/master/src/main/java/com/google/devtools/build/lib/bazel/rules/android/AndroidSdkRepositoryFunction.java

    You may find it easier to explore the codebase using Bazel's CodeSearch site, because it has cross-references:

    https://cs.opensource.google/bazel/bazel/+/master:src/main/java/com/google/devtools/build/lib/rules/;bpv=0;bpt=0