NDK 19 and 20 support

Successfully tested to build Tensorflow's Android app on `4f9ae09cff5f357eb8260c468fbf22828f58e4b6` (HEAD today) with `bazel-dev build //tensorflow/examples/android:tensorflow_demo --cxxopt='--std=c++11' -c opt --fat_apk_cpu=x86,armeabi-v7a`

This change includes:

* Support for building with NDK 19 and 20
* Simplifying crosstool building by centralizing common configuration in `AndroidNdkCrosstoolsR19`.
* Applying recommended flags documented in the [NDK Build System Maintainer's Guide](https://android.googlesource.com/platform/ndk/+/ndk-release-r19/docs/BuildSystemMaintainers.md#build-system-maintainers-guide)

This change doesn't contain support for `lld` or use the new standalone GCC tools yet. These will come in a follow up PR.

RELNOTES: Added support for Android NDK 19 and 20.

Closes #8524.

Change-Id: I800b3661b24057316cbb537d51e6ba346cc30f45
PiperOrigin-RevId: 250911997
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/AndroidNdkCrosstools.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/AndroidNdkCrosstools.java
index d595dfe..a97ea71 100644
--- a/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/AndroidNdkCrosstools.java
+++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/AndroidNdkCrosstools.java
@@ -23,6 +23,7 @@
 import com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.r15.NdkMajorRevisionR15;
 import com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.r17.NdkMajorRevisionR17;
 import com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.r18.NdkMajorRevisionR18;
+import com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.r19.NdkMajorRevisionR19;
 import com.google.devtools.build.lib.util.OS;
 import java.util.Map;
 
@@ -50,6 +51,8 @@
           .put(16, new NdkMajorRevisionR15("5.0.300080")) // no changes relevant to Bazel
           .put(17, new NdkMajorRevisionR17("6.0.2"))
           .put(18, new NdkMajorRevisionR18("7.0.2"))
+          .put(19, new NdkMajorRevisionR19("8.0.2"))
+          .put(20, new NdkMajorRevisionR19("8.0.7")) // no changes relevant to Bazel
           .build();
 
   public static final Map.Entry<Integer, NdkMajorRevision> LATEST_KNOWN_REVISION =
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r19/AndroidNdkCrosstoolsR19.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r19/AndroidNdkCrosstoolsR19.java
new file mode 100644
index 0000000..a4b4b7e
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r19/AndroidNdkCrosstoolsR19.java
@@ -0,0 +1,107 @@
+// Copyright 2019 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.r19;
+
+import com.google.common.collect.ImmutableList;
+import com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.NdkPaths;
+import com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.StlImpl;
+import com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.CToolchain;
+import com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.CrosstoolRelease;
+import java.util.ArrayList;
+import java.util.List;
+
+/** Generates a CrosstoolRelease proto for the Android NDK. */
+final class AndroidNdkCrosstoolsR19 {
+
+  /**
+   * Creates a CrosstoolRelease proto for the Android NDK, given the API level to use and the
+   * release revision. The crosstools are generated through code rather than checked in as a flat
+   * file to reduce the amount of templating needed (for parameters like the release name and
+   * certain paths), to reduce duplication, and to make it easier to support future versions of the
+   * NDK. TODO(bazel-team): Eventually we should move this into Skylark so the crosstools can be
+   * updated independently of Bazel itself.
+   *
+   * @return A CrosstoolRelease for the Android NDK.
+   */
+  static CrosstoolRelease create(
+      NdkPaths ndkPaths, StlImpl stlImpl, String hostPlatform, String clangVersion) {
+    return CrosstoolRelease.newBuilder()
+        .setMajorVersion("android")
+        .setMinorVersion("")
+        .setDefaultTargetCpu("armeabi")
+        .addAllToolchain(createToolchains(ndkPaths, stlImpl, hostPlatform, clangVersion))
+        .build();
+  }
+
+  private static ImmutableList<CToolchain> createToolchains(
+      NdkPaths ndkPaths, StlImpl stlImpl, String hostPlatform, String clangVersion) {
+
+    List<CToolchain.Builder> toolchainBuilders = new ArrayList<>();
+    toolchainBuilders.addAll(new ArmCrosstools(ndkPaths, stlImpl, clangVersion).createCrosstools());
+    toolchainBuilders.addAll(new X86Crosstools(ndkPaths, stlImpl, clangVersion).createCrosstools());
+
+    ImmutableList.Builder<CToolchain> toolchains = new ImmutableList.Builder<>();
+
+    // Set attributes common to all toolchains.
+    for (CToolchain.Builder toolchainBuilder : toolchainBuilders) {
+      toolchainBuilder
+          .setHostSystemName(hostPlatform)
+          .setTargetLibc("local")
+          .setAbiVersion(toolchainBuilder.getTargetCpu())
+          .setAbiLibcVersion("local");
+
+      toolchainBuilder
+          .addCompilerFlag("-no-canonical-prefixes")
+          .addCompilerFlag("-Wno-invalid-command-line-argument")
+          .addCompilerFlag("-Wno-unused-command-line-argument")
+          .addCompilerFlag("-funwind-tables")
+          .addCompilerFlag("-fstack-protector-strong");
+
+      toolchainBuilder.addLinkerFlag("-no-canonical-prefixes");
+
+      // https://android.googlesource.com/platform/ndk/+/ndk-release-r19/docs/BuildSystemMaintainers.md#additional-required-arguments
+      toolchainBuilder
+          // "Clang uses -faddrsig by default, but this produces output that is incompatible with
+          // GNU binutils. To workaround this, -fno-addrsig must be passed to Clang when using GNU
+          // binutils."
+          .addCompilerFlag("-fno-addrsig")
+          // "All code must be linked with -Wl,-z,relro, which causes relocations to be made
+          // read-only after relocation is performed."
+          .addLinkerFlag("-Wl,-z,relro");
+
+      // https://android.googlesource.com/platform/ndk/+/ndk-release-r19/docs/BuildSystemMaintainers.md#controlling-binary-size
+      toolchainBuilder.addLinkerFlag("-Wl,--gc-sections");
+
+      // https://android.googlesource.com/platform/ndk/+/ndk-release-r19/docs/BuildSystemMaintainers.md#helpful-warnings
+      toolchainBuilder
+          .addCompilerFlag("-Werror=return-type")
+          .addCompilerFlag("-Werror=int-to-pointer-cast")
+          .addCompilerFlag("-Werror=pointer-to-int-cast")
+          .addCompilerFlag("-Werror=implicit-function-declaration");
+
+      // builtin_sysroot is set individually on each toolchain.
+      // platforms/arch sysroot
+      toolchainBuilder.addCxxBuiltinIncludeDirectory("%sysroot%/usr/include");
+      toolchainBuilder.addCxxBuiltinIncludeDirectory(
+          ndkPaths.createBuiltinSysroot() + "/usr/include");
+      toolchainBuilder.addUnfilteredCxxFlag(
+          "-isystem%ndk%/usr/include".replace("%ndk%", ndkPaths.createBuiltinSysroot()));
+
+      toolchains.add(toolchainBuilder.build());
+    }
+
+    return toolchains.build();
+  }
+}
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r19/ApiLevelR19.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r19/ApiLevelR19.java
new file mode 100644
index 0000000..894b7d3
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r19/ApiLevelR19.java
@@ -0,0 +1,61 @@
+// Copyright 2019 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.r19;
+
+import com.google.common.collect.ImmutableListMultimap;
+import com.google.common.collect.ImmutableMap;
+import com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.ApiLevel;
+import com.google.devtools.build.lib.events.EventHandler;
+
+/** Class which encodes information from the Android NDK makefiles about API levels. */
+final class ApiLevelR19 extends ApiLevel {
+  /** This is the contents of {@code platforms/android-*} */
+  private static final ImmutableListMultimap<String, String> API_LEVEL_TO_ARCHITECTURES =
+      ImmutableListMultimap.<String, String>builder()
+          .putAll("16", "arm", "x86")
+          .putAll("17", "arm", "x86")
+          .putAll("18", "arm", "x86")
+          .putAll("19", "arm", "x86")
+          .putAll("21", "arm", "x86", "arm64", "x86_64")
+          .putAll("22", "arm", "x86", "arm64", "x86_64")
+          .putAll("23", "arm", "x86", "arm64", "x86_64")
+          .putAll("24", "arm", "x86", "arm64", "x86_64")
+          .putAll("26", "arm", "x86", "arm64", "x86_64")
+          .putAll("27", "arm", "x86", "arm64", "x86_64")
+          .putAll("28", "arm", "x86", "arm64", "x86_64")
+          .build();
+
+  /** This map fill in the gaps of {@code API_LEVEL_TO_ARCHITECTURES}. */
+  private static final ImmutableMap<String, String> API_EQUIVALENCIES =
+      ImmutableMap.<String, String>builder()
+          .put("16", "16")
+          .put("17", "16")
+          .put("18", "18")
+          .put("19", "19")
+          .put("20", "19")
+          .put("21", "21")
+          .put("22", "22")
+          .put("23", "23")
+          .put("24", "24")
+          .put("25", "24")
+          .put("26", "26")
+          .put("27", "27")
+          .put("28", "28")
+          .build();
+
+  ApiLevelR19(EventHandler eventHandler, String repositoryName, String apiLevel) {
+    super(API_LEVEL_TO_ARCHITECTURES, API_EQUIVALENCIES, eventHandler, repositoryName, apiLevel);
+  }
+}
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r19/ArmCrosstools.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r19/ArmCrosstools.java
new file mode 100644
index 0000000..d8d6546
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r19/ArmCrosstools.java
@@ -0,0 +1,160 @@
+// Copyright 2019 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.r19;
+
+import com.google.common.collect.ImmutableList;
+import com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.NdkPaths;
+import com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.StlImpl;
+import com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.CToolchain;
+import com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.CompilationMode;
+import com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.CompilationModeFlags;
+
+/**
+ * Crosstool definitions for ARM. These values are based on the setup.mk files in the Android NDK
+ * toolchain directories.
+ */
+final class ArmCrosstools {
+  private final NdkPaths ndkPaths;
+  private final StlImpl stlImpl;
+  private final String clangVersion;
+
+  ArmCrosstools(NdkPaths ndkPaths, StlImpl stlImpl, String clangVersion) {
+    this.ndkPaths = ndkPaths;
+    this.stlImpl = stlImpl;
+    this.clangVersion = clangVersion;
+  }
+
+  ImmutableList<CToolchain.Builder> createCrosstools() {
+    CToolchain.Builder aarch64Toolchain = createAarch64ClangToolchain();
+    CToolchain.Builder armeabiToolchain = createArmeabiClangToolchain();
+
+    stlImpl.addStlImpl(aarch64Toolchain, "4.9");
+    stlImpl.addStlImpl(armeabiToolchain, "4.9");
+
+    return ImmutableList.<CToolchain.Builder>builder()
+        .add(aarch64Toolchain)
+        .add(armeabiToolchain)
+        .build();
+  }
+
+  private CToolchain.Builder createAarch64ClangToolchain() {
+    String toolchainName = "aarch64-linux-android-4.9";
+    String targetPlatform = "aarch64-linux-android";
+    String gccToolchain = ndkPaths.createGccToolchainPath(toolchainName);
+    String llvmTriple = "aarch64-none-linux-android";
+
+    return CToolchain.newBuilder()
+        .setToolchainIdentifier("aarch64-linux-android-clang" + clangVersion)
+        .setTargetSystemName(targetPlatform)
+        .setTargetCpu("arm64-v8a")
+        .setCompiler("clang" + clangVersion)
+        .addAllToolPath(ndkPaths.createClangToolpaths(toolchainName, targetPlatform, null))
+        .addCxxBuiltinIncludeDirectory(
+            ndkPaths.createClangToolchainBuiltinIncludeDirectory(clangVersion))
+        .setBuiltinSysroot(ndkPaths.createBuiltinSysroot("arm64"))
+
+        // Compiler flags
+        .addCompilerFlag("-gcc-toolchain")
+        .addCompilerFlag(gccToolchain)
+        .addCompilerFlag("-target")
+        .addCompilerFlag(llvmTriple)
+        .addCompilerFlag("-fpic")
+        .addCompilerFlag(
+            "-isystem%ndk%/usr/include/%triple%"
+                .replace("%ndk%", ndkPaths.createBuiltinSysroot())
+                .replace("%triple%", targetPlatform))
+        .addCompilerFlag("-D__ANDROID_API__=" + ndkPaths.getCorrectedApiLevel("arm"))
+
+        // Linker flags
+        .addLinkerFlag("-gcc-toolchain")
+        .addLinkerFlag(gccToolchain)
+        .addLinkerFlag("-target")
+        .addLinkerFlag(llvmTriple)
+
+        // Additional release flags
+        .addCompilationModeFlags(
+            CompilationModeFlags.newBuilder()
+                .setMode(CompilationMode.OPT)
+                .addCompilerFlag("-O2")
+                .addCompilerFlag("-g")
+                .addCompilerFlag("-DNDEBUG"))
+
+        // Additional debug flags
+        .addCompilationModeFlags(
+            CompilationModeFlags.newBuilder()
+                .setMode(CompilationMode.DBG)
+                .addCompilerFlag("-O0")
+                .addCompilerFlag("-g")
+                .addCompilerFlag("-UNDEBUG"));
+  }
+
+  private CToolchain.Builder createArmeabiClangToolchain() {
+    String toolchainName = "arm-linux-androideabi-4.9";
+    String targetPlatform = "arm-linux-androideabi";
+    String gccToolchain = ndkPaths.createGccToolchainPath("arm-linux-androideabi-4.9");
+
+    return CToolchain.newBuilder()
+        .setToolchainIdentifier("arm-linux-androideabi-clang" + clangVersion + "-v7a")
+        .setTargetCpu("armeabi-v7a")
+        .setTargetSystemName("arm-linux-androideabi")
+        .setCompiler("clang" + clangVersion)
+        .addAllToolPath(ndkPaths.createClangToolpaths(toolchainName, targetPlatform, null))
+        .addCxxBuiltinIncludeDirectory(
+            ndkPaths.createClangToolchainBuiltinIncludeDirectory(clangVersion))
+        .setBuiltinSysroot(ndkPaths.createBuiltinSysroot("arm"))
+        .addCompilerFlag("-D__ANDROID_API__=" + ndkPaths.getCorrectedApiLevel("arm"))
+        .addCompilerFlag(
+            "-isystem%ndk%/usr/include/%triple%"
+                .replace("%ndk%", ndkPaths.createBuiltinSysroot())
+                .replace("%triple%", targetPlatform))
+
+        // Compiler flags
+        .addCompilerFlag("-target")
+        .addCompilerFlag("armv7-none-linux-androideabi") // LLVM_TRIPLE
+        .addCompilerFlag("-march=armv7-a")
+        .addCompilerFlag("-mfloat-abi=softfp")
+        // "32-bit ARM targets should use -mfpu=vfpv3-d16 when compiling unless using NEON. This
+        // allows the compiler to make use of the FPU."
+        // https://android.googlesource.com/platform/ndk/+/ndk-release-r19/docs/BuildSystemMaintainers.md#additional-required-arguments
+        .addCompilerFlag("-mfpu=vfpv3-d16")
+        .addCompilerFlag("-gcc-toolchain")
+        .addCompilerFlag(gccToolchain)
+        .addCompilerFlag("-fpic")
+
+        // Linker flags
+        .addLinkerFlag("-target")
+        .addLinkerFlag("armv7-none-linux-androideabi") // LLVM_TRIPLE
+        .addLinkerFlag("-gcc-toolchain")
+        .addLinkerFlag(gccToolchain)
+
+        // Additional release flags
+        .addCompilationModeFlags(
+            CompilationModeFlags.newBuilder()
+                .setMode(CompilationMode.OPT)
+                .addCompilerFlag("-mthumb")
+                .addCompilerFlag("-Os")
+                .addCompilerFlag("-g")
+                .addCompilerFlag("-DNDEBUG"))
+
+        // Additional debug flags
+        .addCompilationModeFlags(
+            CompilationModeFlags.newBuilder()
+                .setMode(CompilationMode.DBG)
+                .addCompilerFlag("-g")
+                .addCompilerFlag("-fno-strict-aliasing")
+                .addCompilerFlag("-O0")
+                .addCompilerFlag("-UNDEBUG"));
+  }
+}
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r19/NdkMajorRevisionR19.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r19/NdkMajorRevisionR19.java
new file mode 100644
index 0000000..a9eae01
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r19/NdkMajorRevisionR19.java
@@ -0,0 +1,42 @@
+// Copyright 2019 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.r19;
+
+import com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.ApiLevel;
+import com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.NdkMajorRevision;
+import com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.NdkPaths;
+import com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.StlImpl;
+import com.google.devtools.build.lib.events.EventHandler;
+import com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.CrosstoolRelease;
+
+/** Logic specific to Android NDK R17. */
+public class NdkMajorRevisionR19 implements NdkMajorRevision {
+  private final String clangVersion;
+
+  public NdkMajorRevisionR19(String clangVersion) {
+    this.clangVersion = clangVersion;
+  }
+
+  @Override
+  public CrosstoolRelease crosstoolRelease(
+      NdkPaths ndkPaths, StlImpl stlImpl, String hostPlatform) {
+    return AndroidNdkCrosstoolsR19.create(ndkPaths, stlImpl, hostPlatform, clangVersion);
+  }
+
+  @Override
+  public ApiLevel apiLevel(EventHandler eventHandler, String name, String apiLevel) {
+    return new ApiLevelR19(eventHandler, name, apiLevel);
+  }
+}
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r19/X86Crosstools.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r19/X86Crosstools.java
new file mode 100644
index 0000000..9199e02
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r19/X86Crosstools.java
@@ -0,0 +1,122 @@
+// Copyright 2019 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.r19;
+
+import com.google.common.collect.ImmutableList;
+import com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.NdkPaths;
+import com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.StlImpl;
+import com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.CToolchain;
+import com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.CompilationMode;
+import com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.CompilationModeFlags;
+
+/**
+ * Crosstool definitions for x86. These values are based on the setup.mk files in the Android NDK
+ * toolchain directories.
+ */
+final class X86Crosstools {
+  private final NdkPaths ndkPaths;
+  private final StlImpl stlImpl;
+  private final String clangVersion;
+
+  X86Crosstools(NdkPaths ndkPaths, StlImpl stlImpl, String clangVersion) {
+    this.ndkPaths = ndkPaths;
+    this.stlImpl = stlImpl;
+    this.clangVersion = clangVersion;
+  }
+
+  ImmutableList<CToolchain.Builder> createCrosstools() {
+    /** x86 */
+    // clang
+    CToolchain.Builder x86Clang =
+        createBaseX86ClangToolchain("x86", "i686", "i686-linux-android")
+            // Workaround for https://code.google.com/p/android/issues/detail?id=220159.
+            .addCompilerFlag("-mstackrealign")
+            .setToolchainIdentifier("x86-clang" + clangVersion)
+            .setTargetCpu("x86")
+            .addAllToolPath(ndkPaths.createClangToolpaths("x86-4.9", "i686-linux-android", null))
+            .setBuiltinSysroot(ndkPaths.createBuiltinSysroot("x86"));
+
+    stlImpl.addStlImpl(x86Clang, null);
+
+    /** x86_64 */
+    CToolchain.Builder x8664Clang =
+        createBaseX86ClangToolchain("x86_64", "x86_64", "x86_64-linux-android")
+            .setToolchainIdentifier("x86_64-clang" + clangVersion)
+            .setTargetCpu("x86_64")
+            .addAllToolPath(
+                ndkPaths.createClangToolpaths("x86_64-4.9", "x86_64-linux-android", null))
+            .setBuiltinSysroot(ndkPaths.createBuiltinSysroot("x86_64"));
+
+    stlImpl.addStlImpl(x8664Clang, null);
+
+    return ImmutableList.of(x86Clang, x8664Clang);
+  }
+
+  private CToolchain.Builder createBaseX86ClangToolchain(
+      String x86Arch, String llvmArch, String triple) {
+    String gccToolchain = ndkPaths.createGccToolchainPath(x86Arch + "-4.9");
+    String llvmTriple = llvmArch + "-none-linux-android";
+
+    CToolchain.Builder cToolchainBuilder = CToolchain.newBuilder();
+
+    cToolchainBuilder
+        .setCompiler("clang" + clangVersion)
+        .addCxxBuiltinIncludeDirectory(
+            ndkPaths.createClangToolchainBuiltinIncludeDirectory(clangVersion))
+
+        // Compiler flags
+        .addCompilerFlag("-gcc-toolchain")
+        .addCompilerFlag(gccToolchain)
+        .addCompilerFlag("-target")
+        .addCompilerFlag(llvmTriple)
+        .addCompilerFlag("-fPIC")
+        .addCompilerFlag(
+            "-isystem%ndk%/usr/include/%triple%"
+                .replace("%ndk%", ndkPaths.createBuiltinSysroot())
+                .replace("%triple%", triple))
+        .addCompilerFlag("-D__ANDROID_API__=" + ndkPaths.getCorrectedApiLevel(x86Arch))
+
+        // Linker flags
+        .addLinkerFlag("-gcc-toolchain")
+        .addLinkerFlag(gccToolchain)
+        .addLinkerFlag("-target")
+        .addLinkerFlag(llvmTriple)
+
+        // Additional release flags
+        .addCompilationModeFlags(
+            CompilationModeFlags.newBuilder()
+                .setMode(CompilationMode.OPT)
+                .addCompilerFlag("-O2")
+                .addCompilerFlag("-g")
+                .addCompilerFlag("-DNDEBUG"))
+
+        // Additional debug flags
+        .addCompilationModeFlags(
+            CompilationModeFlags.newBuilder()
+                .setMode(CompilationMode.DBG)
+                .addCompilerFlag("-O0")
+                .addCompilerFlag("-g"))
+        .setTargetSystemName("x86-linux-android");
+
+    if (Integer.parseInt(ndkPaths.getCorrectedApiLevel(x86Arch)) < 24) {
+      // "For x86 targets prior to Android Nougat (API 24), -mstackrealign is needed to properly
+      // align stacks for global constructors. See Issue 635."
+      // https://android.googlesource.com/platform/ndk/+/ndk-release-r19/docs/BuildSystemMaintainers.md#additional-required-arguments
+      cToolchainBuilder.addCompilerFlag("-mstackrealign");
+    }
+
+    return cToolchainBuilder;
+  }
+}
diff --git a/src/test/java/com/google/devtools/build/lib/bazel/rules/android/AndroidNdkRepositoryTest.java b/src/test/java/com/google/devtools/build/lib/bazel/rules/android/AndroidNdkRepositoryTest.java
index 0ce4aa1..4bfc3cc 100644
--- a/src/test/java/com/google/devtools/build/lib/bazel/rules/android/AndroidNdkRepositoryTest.java
+++ b/src/test/java/com/google/devtools/build/lib/bazel/rules/android/AndroidNdkRepositoryTest.java
@@ -115,7 +115,7 @@
         eventCollector,
         "The revision of the Android NDK referenced by android_ndk_repository rule 'androidndk' "
             + "could not be determined (the revision string found is 'not a valid release string')."
-            + " Bazel will attempt to treat the NDK as if it was r18.");
+            + " Bazel will attempt to treat the NDK as if it was r20.");
   }
 
   @Test
@@ -142,7 +142,7 @@
         eventCollector,
         "The revision of the Android NDK referenced by android_ndk_repository rule 'androidndk' "
             + "could not be determined (the revision string found is 'invalid package revision'). "
-            + "Bazel will attempt to treat the NDK as if it was r18.");
+            + "Bazel will attempt to treat the NDK as if it was r20.");
   }
 
   @Test
@@ -158,16 +158,16 @@
         ")");
 
     scratch.overwriteFile(
-        "/ndk/source.properties", "Pkg.Desc = Android NDK", "Pkg.Revision = 19.0.3675639-beta2");
+        "/ndk/source.properties", "Pkg.Desc = Android NDK", "Pkg.Revision = 21.0.3675639-beta2");
     invalidatePackages();
 
     assertThat(getConfiguredTarget("@androidndk//:files")).isNotNull();
     MoreAsserts.assertContainsEvent(
         eventCollector,
         "The major revision of the Android NDK referenced by android_ndk_repository rule "
-            + "'androidndk' is 19. The major revisions supported by Bazel are "
-            + "[10, 11, 12, 13, 14, 15, 16, 17, 18]. Bazel will attempt to treat the NDK as if it "
-            + "was r18.");
+            + "'androidndk' is 21. The major revisions supported by Bazel are "
+            + "[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]. "
+            + "Bazel will attempt to treat the NDK as if it was r20.");
   }
 
   @Test