Repository: bazelbuild/bazel
Issue: Android NDK package: add filegroup for Vulkan validation layers
Labels: team-Android

### Description of the feature request:

Provide a mean to access Vulkan validation layers which are precompiled dynamic libraries shipped in Android NDK.

Android NDK BUILD file is generated by Bazel itself (`@androidndk`), and offers predefined rules/target related to the NDK. Currently, I cannot find an easy way to refer to the Vulkan validation layers. These layers are shipped in:

`$ANDROID_NDK_HOME/sources/third_party/vulkan/src/build-android/jniLibs/<ABI>/libVkLayer_*.so`

Where ABI is arm64-v8a, x86, etc.

### Feature requests: what underlying problem are you trying to solve with this feature?

Easily add Vulkan validation layers to Android app.

### Bugs: what's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

Try to refer to the Vulkan validation layers using Bazel. I think that currently you need a repository rule that can access ANDROID_NDK_HOME environment variable, and search for those layers yourself.

###  Have you found anything relevant by searching the web?

Nothing relevant shows up.


Issue comments:
`//src/test/java/com/google/devtools/build/lib/bazel:bazel-rules-tests` is failing. Please take a look at that. It's probably because the mocked NDK doesn't have these files..?

Pull request review comments:
This is not a unique name, if `toolchain.getTargetCpu()` is common between the outer crosstools loop.
Thanks for this, I've changed the rule name to contain the toolchain name, and it seems to have fixed the tests.

Patch that resolved the issue:
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/android/AndroidNdkRepositoryFunction.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/AndroidNdkRepositoryFunction.java
index dc33d59a0aa264..7c5cd30e452b2f 100644
--- a/src/main/java/com/google/devtools/build/lib/bazel/rules/android/AndroidNdkRepositoryFunction.java
+++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/AndroidNdkRepositoryFunction.java
@@ -85,11 +85,13 @@ private static String createBuildFile(
     String ccToolchainSuiteTemplate = getTemplate("android_ndk_cc_toolchain_suite_template.txt");
     String ccToolchainTemplate = getTemplate("android_ndk_cc_toolchain_template.txt");
     String stlFilegroupTemplate = getTemplate("android_ndk_stl_filegroup_template.txt");
+    String vulkanValidationLayersTemplate = getTemplate("android_ndk_vulkan_validation_layers_template.txt");
     String miscLibrariesTemplate = getTemplate("android_ndk_misc_libraries_template.txt");
 
     StringBuilder ccToolchainSuites = new StringBuilder();
     StringBuilder ccToolchainRules = new StringBuilder();
     StringBuilder stlFilegroups = new StringBuilder();
+    StringBuilder vulkanValidationLayers = new StringBuilder();
     for (CrosstoolStlPair crosstoolStlPair : crosstools) {
 
       // Create the cc_toolchain_suite rule
@@ -131,6 +133,15 @@ private static String createBuildFile(
                 .replace("%name%", entry.getKey())
                 .replace("%fileGlobPattern%", entry.getValue()));
       }
+
+      // Create the Vulkan validation layers libraries
+      for (CToolchain toolchain : crosstool.getToolchainList()) {
+        vulkanValidationLayers.append(
+            vulkanValidationLayersTemplate
+                .replace("%toolchainName%", toolchain.getToolchainIdentifier())
+                .replace("%cpu%", toolchain.getTargetCpu()));
+      }
+
     }
 
     return buildFileTemplate
@@ -139,6 +150,7 @@ private static String createBuildFile(
         .replace("%ccToolchainSuites%", ccToolchainSuites)
         .replace("%ccToolchainRules%", ccToolchainRules)
         .replace("%stlFilegroups%", stlFilegroups)
+        .replace("%vulkanValidationLayers%", vulkanValidationLayers)
         .replace("%miscLibraries%", miscLibrariesTemplate);
   }
 
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/android/android_ndk_build_file_template.txt b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/android_ndk_build_file_template.txt
index 0d0fbb45652c6f..b8e58e5d5a6262 100644
--- a/src/main/java/com/google/devtools/build/lib/bazel/rules/android/android_ndk_build_file_template.txt
+++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/android_ndk_build_file_template.txt
@@ -52,6 +52,12 @@ alias(
 
 %stlFilegroups%
 
+################################################################
+# Vulkan validation layers libraries
+################################################################
+
+%vulkanValidationLayers%
+
 ################################################################
 # Miscellaneous runtime libraries
 ################################################################
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/android/android_ndk_vulkan_validation_layers_template.txt b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/android_ndk_vulkan_validation_layers_template.txt
new file mode 100644
index 00000000000000..dd32111cb8f98e
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/android_ndk_vulkan_validation_layers_template.txt
@@ -0,0 +1,4 @@
+cc_library(
+    name = "vulkan_validation_layers_%toolchainName%",
+    srcs = glob(["ndk/sources/third_party/vulkan/src/build-android/jniLibs/%cpu%/libVkLayer_*.so"]),
+)


Test patch: