From 3de69da0323bb0bd075611a4f12e590dd1204cf9 Mon Sep 17 00:00:00 2001 From: Alex Humesky Date: Wed, 3 Nov 2021 16:34:42 -0400 Subject: [PATCH] Remove R23 code generation and replace with static build and bzl files --- .../android/AndroidNdkRepositoryFunction.java | 29 +- .../ndkcrosstools/AndroidNdkCrosstools.java | 2 +- .../ndkcrosstools/NdkMajorRevision.java | 19 + .../r23/AndroidNdkCrosstoolsR23.java | 107 ---- .../ndkcrosstools/r23/ApiLevelR23.java | 67 -- .../ndkcrosstools/r23/ArmCrosstools.java | 142 ----- .../rules/android/ndkcrosstools/r23/BUILD | 2 + .../android/ndkcrosstools/r23/BUILD.bazel.txt | 340 +++++++++++ .../r23/NdkMajorRevisionR23.java | 33 +- .../ndkcrosstools/r23/X86Crosstools.java | 114 ---- .../r23/cc_toolchain_config.bzl.txt | 571 ++++++++++++++++++ 11 files changed, 989 insertions(+), 437 deletions(-) delete mode 100644 src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r23/AndroidNdkCrosstoolsR23.java delete mode 100644 src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r23/ApiLevelR23.java delete mode 100644 src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r23/ArmCrosstools.java create mode 100644 src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r23/BUILD.bazel.txt delete mode 100644 src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r23/X86Crosstools.java create mode 100644 src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r23/cc_toolchain_config.bzl.txt 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 f6a4c25fdd3a39..77fc9dd1101a68 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 @@ -26,6 +26,7 @@ import com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.AndroidNdkCrosstools.NdkCrosstoolsException; 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.NdkMajorRevision.NdkFile; import com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.NdkPaths; import com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.NdkRelease; import com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.StlImpl; @@ -386,6 +387,33 @@ public RepositoryDirectoryValue.Builder fetch( boolean siblingRepositoryLayout = starlarkSemantics.getBool(BuildLanguageOptions.EXPERIMENTAL_SIBLING_REPOSITORY_LAYOUT); + if (ndkMajorRevision.isStatic()) { + for (NdkFile ndkFile : ndkMajorRevision.getStaticFiles()) { + writeFile(outputDirectory, ndkFile.name, ndkFile.content); + } + } else { + generateNdkFiles( + env, + ndkMajorRevision, + ruleName, + apiLevelString, + ndkRelease, + siblingRepositoryLayout, + outputDirectory); + } + + return RepositoryDirectoryValue.builder().setPath(outputDirectory); + } + + private void generateNdkFiles( + Environment env, + NdkMajorRevision ndkMajorRevision, + String ruleName, + String apiLevelString, + NdkRelease ndkRelease, + boolean siblingRepositoryLayout, + Path outputDirectory) throws RepositoryFunctionException { + ApiLevel apiLevel = ndkMajorRevision.apiLevel(env.getListener(), ruleName, apiLevelString); ImmutableList.Builder crosstoolsAndStls = ImmutableList.builder(); @@ -424,7 +452,6 @@ public RepositoryDirectoryValue.Builder fetch( .replaceAll( "%big_conditional_populating_variables%", Joiner.on("\n" + " ").join(bigConditional.build()))); - return RepositoryDirectoryValue.builder().setPath(outputDirectory); } private ImmutableList generateBzlConfigFor(String version, CToolchain toolchain) { 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 536115f6b8bce2..bc2402800fc940 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 @@ -58,7 +58,7 @@ private AndroidNdkCrosstools() {} .put(20, new NdkMajorRevisionR20("8.0.7")) .put(21, new NdkMajorRevisionR21("9.0.8")) .put(22, new NdkMajorRevisionR21("11.0.5")) - .put(23, new NdkMajorRevisionR23("12.0.5")) + .put(23, new NdkMajorRevisionR23()) .build(); public static final Map.Entry LATEST_KNOWN_REVISION = diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/NdkMajorRevision.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/NdkMajorRevision.java index ba107809ecee12..6255448634fa40 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/NdkMajorRevision.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/NdkMajorRevision.java @@ -14,6 +14,7 @@ package com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools; +import com.google.common.collect.ImmutableList; import com.google.devtools.build.lib.events.EventHandler; import com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.CrosstoolRelease; @@ -32,4 +33,22 @@ public interface NdkMajorRevision { * and api level to architecture mappings. */ ApiLevel apiLevel(EventHandler eventHandler, String name, String apiLevel); + + class NdkFile { + public final String name; + public final String content; + + public NdkFile(String name, String content) { + this.name = name; + this.content = content; + } + } + + default boolean isStatic() { + return false; + } + + default ImmutableList getStaticFiles() { + return ImmutableList.of(); + } } diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r23/AndroidNdkCrosstoolsR23.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r23/AndroidNdkCrosstoolsR23.java deleted file mode 100644 index d8b7bed18fea95..00000000000000 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r23/AndroidNdkCrosstoolsR23.java +++ /dev/null @@ -1,107 +0,0 @@ -// 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.r23; - -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. */ -public final class AndroidNdkCrosstoolsR23 { - - /** - * 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 Starlark so the crosstools can be - * updated independently of Bazel itself. - * - * @return A CrosstoolRelease for the Android NDK. - */ - public 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 createToolchains( - NdkPaths ndkPaths, StlImpl stlImpl, String hostPlatform, String clangVersion) { - - List toolchainBuilders = new ArrayList<>(); - toolchainBuilders.addAll(new ArmCrosstools(ndkPaths, stlImpl, clangVersion).createCrosstools()); - toolchainBuilders.addAll(new X86Crosstools(ndkPaths, stlImpl, clangVersion).createCrosstools()); - - ImmutableList.Builder 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/r23/ApiLevelR23.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r23/ApiLevelR23.java deleted file mode 100644 index fc8f233834c029..00000000000000 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r23/ApiLevelR23.java +++ /dev/null @@ -1,67 +0,0 @@ -// Copyright 2021 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.r23; - -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 ApiLevelR23 extends ApiLevel { - /** This is the contents of {@code platforms/android-*} */ - private static final ImmutableListMultimap API_LEVEL_TO_ARCHITECTURES = - ImmutableListMultimap.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") - .putAll("29", "arm", "x86", "arm64", "x86_64") - .putAll("30", "arm", "x86", "arm64", "x86_64") - .putAll("31", "arm", "x86", "arm64", "x86_64") - .build(); - - /** This map fill in the gaps of {@code API_LEVEL_TO_ARCHITECTURES}. */ - private static final ImmutableMap API_EQUIVALENCIES = - ImmutableMap.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") - .put("29", "29") - .put("30", "30") - .put("31", "31") - .build(); - - ApiLevelR23(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/r23/ArmCrosstools.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r23/ArmCrosstools.java deleted file mode 100644 index 889916c6d3b943..00000000000000 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r23/ArmCrosstools.java +++ /dev/null @@ -1,142 +0,0 @@ -// Copyright 2021 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.r23; - -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 createCrosstools() { - CToolchain.Builder aarch64Toolchain = createAarch64ClangToolchain(); - CToolchain.Builder armeabiToolchain = createArmeabiClangToolchain(); - - stlImpl.addStlImpl(aarch64Toolchain, "4.9"); - stlImpl.addStlImpl(armeabiToolchain, "4.9"); - - return ImmutableList.builder() - .add(aarch64Toolchain) - .add(armeabiToolchain) - .build(); - } - - private CToolchain.Builder createAarch64ClangToolchain() { - String toolchainName = "llvm"; - String targetPlatform = "aarch64-none-linux-android"; - String llvmTriple = "aarch64-none-linux-android" + ndkPaths.getCorrectedApiLevel("arm"); - - 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.createClangBuiltinSysroot()) - - .addCompilerFlag("-target") - .addCompilerFlag(llvmTriple) - .addCompilerFlag("-fpic") - - // Linker flags - .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 toolchainName = "llvm"; - String targetPlatform = "arm-linux-androideabi"; - // String gccToolchain = ndkPaths.createGccToolchainPath("arm-linux-androideabi-4.9"); - String llvmTriple = "arm-none-linux-android" + ndkPaths.getCorrectedApiLevel("arm"); - - 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.createClangBuiltinSysroot()) - .addCompilerFlag("-D__ANDROID_API__=" + ndkPaths.getCorrectedApiLevel("arm")) - // Compiler flags - .addCompilerFlag("-target") - .addCompilerFlag(llvmTriple) - .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("-fpic") - - // Linker flags - .addLinkerFlag("-target") - .addLinkerFlag(llvmTriple) - - // 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/r23/BUILD b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r23/BUILD index 5d5a550e205324..27001ba9fde7dc 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r23/BUILD +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r23/BUILD @@ -15,9 +15,11 @@ filegroup( java_library( name = "r23", srcs = glob(["*.java"]), + resources = glob(["*.txt"]), deps = [ "//src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools", "//src/main/java/com/google/devtools/build/lib/events", + "//src/main/java/com/google/devtools/build/lib/util", "//src/main/protobuf:crosstool_config_java_proto", "//third_party:guava", ], diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r23/BUILD.bazel.txt b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r23/BUILD.bazel.txt new file mode 100644 index 00000000000000..33ff5f025c99ca --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r23/BUILD.bazel.txt @@ -0,0 +1,340 @@ +# Copyright 2021 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. + +# Build file for NDK R23 + +load(":cc_toolchain_config.bzl", "cc_toolchain_config") + +package(default_visibility = ["//visibility:public"]) + +filegroup( + name = "files", + srcs = ["ndk"], +) + +cc_library( + name = "malloc", + srcs = [], +) + +alias( + name = "default_crosstool", + actual = "//:toolchain-libcpp", +) + +################################################################ +# cc toolchain suite rules (one for each STL in the NDK) +################################################################ + +cc_toolchain_suite( + name = "toolchain-libcpp", + toolchains = { + 'arm64-v8a': ':aarch64-linux-android-clang12.0.5-libcpp', + 'arm64-v8a|clang12.0.5': ':aarch64-linux-android-clang12.0.5-libcpp', + 'armeabi-v7a': ':arm-linux-androideabi-clang12.0.5-v7a-libcpp', + 'armeabi-v7a|clang12.0.5': ':arm-linux-androideabi-clang12.0.5-v7a-libcpp', + 'x86': ':x86-clang12.0.5-libcpp', + 'x86|clang12.0.5': ':x86-clang12.0.5-libcpp', + 'x86_64': ':x86_64-clang12.0.5-libcpp', + 'x86_64|clang12.0.5': ':x86_64-clang12.0.5-libcpp', + }, +) + + + +################################################################ +# cc toolchain rules +################################################################ + +################################################################ +# aarch64-linux-android-clang12.0.5-libcpp +################################################################ + +cc_toolchain( + name = "aarch64-linux-android-clang12.0.5-libcpp", + all_files = ":aarch64-linux-android-clang12.0.5-libcpp-all_files", + ar_files = ":aarch64-linux-android-clang12.0.5-libcpp-all_files", + as_files = ":aarch64-linux-android-clang12.0.5-libcpp-all_files", + compiler_files = ":aarch64-linux-android-clang12.0.5-libcpp-all_files", + dwp_files = ":aarch64-linux-android-clang12.0.5-libcpp-all_files", + dynamic_runtime_lib = ":libcpp-arm64-v8a-dynamic-runtime-libraries", + linker_files = ":aarch64-linux-android-clang12.0.5-libcpp-all_files", + objcopy_files = ":aarch64-linux-android-clang12.0.5-libcpp-all_files", + static_runtime_lib = ":libcpp-arm64-v8a-static-runtime-libraries", + strip_files = ":aarch64-linux-android-clang12.0.5-libcpp-all_files", + supports_param_files = 0, + toolchain_identifier = "aarch64-linux-android-clang12.0.5-libcpp", + toolchain_config = ":aarch64-linux-android-clang12.0.5-libcpp-config", +) + +cc_toolchain_config( + name = "aarch64-linux-android-clang12.0.5-libcpp-config", + cpu = "arm64-v8a", + compiler = "clang12.0.5", + version = "libcpp", +) + +toolchain( + name = "aarch64-linux-android-clang12.0.5-libcpp_toolchain", + target_compatible_with = [ + "@platforms//os:android", + "@platforms//cpu:aarch64", + ], + toolchain = "@androidndk//:aarch64-linux-android-clang12.0.5-libcpp", + toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", +) + +filegroup( + name = "aarch64-linux-android-clang12.0.5-libcpp-all_files", + srcs = glob(["ndk/toolchains/llvm/**"]) + glob([ + "ndk/toolchains/llvm/prebuilt/linux-x86_64/sysroot/**/*", + "ndk/sources/cxx-stl/llvm-libc++/include/**/*", + "ndk/sources/cxx-stl/llvm-libc++abi/include/**/*", + "ndk/sources/android/support/include/**/*", + "ndk/sysroot/**/*", + ]) + [ + ":libcpp-arm64-v8a-dynamic-runtime-libraries", + ":libcpp-arm64-v8a-static-runtime-libraries", + ], +) + +################################################################ +# arm-linux-androideabi-clang12.0.5-v7a-libcpp +################################################################ + +cc_toolchain( + name = "arm-linux-androideabi-clang12.0.5-v7a-libcpp", + all_files = ":arm-linux-androideabi-clang12.0.5-v7a-libcpp-all_files", + ar_files = ":arm-linux-androideabi-clang12.0.5-v7a-libcpp-all_files", + as_files = ":arm-linux-androideabi-clang12.0.5-v7a-libcpp-all_files", + compiler_files = ":arm-linux-androideabi-clang12.0.5-v7a-libcpp-all_files", + dwp_files = ":arm-linux-androideabi-clang12.0.5-v7a-libcpp-all_files", + dynamic_runtime_lib = ":libcpp-armeabi-v7a-dynamic-runtime-libraries", + linker_files = ":arm-linux-androideabi-clang12.0.5-v7a-libcpp-all_files", + objcopy_files = ":arm-linux-androideabi-clang12.0.5-v7a-libcpp-all_files", + static_runtime_lib = ":libcpp-armeabi-v7a-static-runtime-libraries", + strip_files = ":arm-linux-androideabi-clang12.0.5-v7a-libcpp-all_files", + supports_param_files = 0, + toolchain_identifier = "arm-linux-androideabi-clang12.0.5-v7a-libcpp", + toolchain_config = ":arm-linux-androideabi-clang12.0.5-v7a-libcpp-config", +) + +cc_toolchain_config( + name = "arm-linux-androideabi-clang12.0.5-v7a-libcpp-config", + cpu = "armeabi-v7a", + compiler = "clang12.0.5", + version = "libcpp", +) + +toolchain( + name = "arm-linux-androideabi-clang12.0.5-v7a-libcpp_toolchain", + target_compatible_with = [ + "@platforms//os:android", + "@platforms//cpu:arm", + ], + toolchain = "@androidndk//:arm-linux-androideabi-clang12.0.5-v7a-libcpp", + toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", +) + +filegroup( + name = "arm-linux-androideabi-clang12.0.5-v7a-libcpp-all_files", + srcs = glob(["ndk/toolchains/llvm/**"]) + glob([ + "ndk/toolchains/llvm/prebuilt/linux-x86_64/sysroot/**/*", + "ndk/sources/cxx-stl/llvm-libc++/include/**/*", + "ndk/sources/cxx-stl/llvm-libc++abi/include/**/*", + "ndk/sources/android/support/include/**/*", + "ndk/sysroot/**/*", + ]) + [ + ":libcpp-armeabi-v7a-dynamic-runtime-libraries", + ":libcpp-armeabi-v7a-static-runtime-libraries", + ], +) + +################################################################ +# x86-clang12.0.5-libcpp +################################################################ + +cc_toolchain( + name = "x86-clang12.0.5-libcpp", + all_files = ":x86-clang12.0.5-libcpp-all_files", + ar_files = ":x86-clang12.0.5-libcpp-all_files", + as_files = ":x86-clang12.0.5-libcpp-all_files", + compiler_files = ":x86-clang12.0.5-libcpp-all_files", + dwp_files = ":x86-clang12.0.5-libcpp-all_files", + dynamic_runtime_lib = ":libcpp-x86-dynamic-runtime-libraries", + linker_files = ":x86-clang12.0.5-libcpp-all_files", + objcopy_files = ":x86-clang12.0.5-libcpp-all_files", + static_runtime_lib = ":libcpp-x86-static-runtime-libraries", + strip_files = ":x86-clang12.0.5-libcpp-all_files", + supports_param_files = 0, + toolchain_identifier = "x86-clang12.0.5-libcpp", + toolchain_config = ":x86-clang12.0.5-libcpp-config", +) + +cc_toolchain_config( + name = "x86-clang12.0.5-libcpp-config", + cpu = "x86", + compiler = "clang12.0.5", + version = "libcpp", +) + +toolchain( + name = "x86-clang12.0.5-libcpp_toolchain", + target_compatible_with = [ + "@platforms//os:android", + "@platforms//cpu:x86_32", + ], + toolchain = "@androidndk//:x86-clang12.0.5-libcpp", + toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", +) + +filegroup( + name = "x86-clang12.0.5-libcpp-all_files", + srcs = glob(["ndk/toolchains/llvm/**"]) + glob([ + "ndk/toolchains/llvm/prebuilt/linux-x86_64/sysroot/**/*", + "ndk/sources/cxx-stl/llvm-libc++/include/**/*", + "ndk/sources/cxx-stl/llvm-libc++abi/include/**/*", + "ndk/sources/android/support/include/**/*", + "ndk/sysroot/**/*", + ]) + [ + ":libcpp-x86-dynamic-runtime-libraries", + ":libcpp-x86-static-runtime-libraries", + ], +) + +################################################################ +# x86_64-clang12.0.5-libcpp +################################################################ + +cc_toolchain( + name = "x86_64-clang12.0.5-libcpp", + all_files = ":x86_64-clang12.0.5-libcpp-all_files", + ar_files = ":x86_64-clang12.0.5-libcpp-all_files", + as_files = ":x86_64-clang12.0.5-libcpp-all_files", + compiler_files = ":x86_64-clang12.0.5-libcpp-all_files", + dwp_files = ":x86_64-clang12.0.5-libcpp-all_files", + dynamic_runtime_lib = ":libcpp-x86_64-dynamic-runtime-libraries", + linker_files = ":x86_64-clang12.0.5-libcpp-all_files", + objcopy_files = ":x86_64-clang12.0.5-libcpp-all_files", + static_runtime_lib = ":libcpp-x86_64-static-runtime-libraries", + strip_files = ":x86_64-clang12.0.5-libcpp-all_files", + supports_param_files = 0, + toolchain_identifier = "x86_64-clang12.0.5-libcpp", + toolchain_config = ":x86_64-clang12.0.5-libcpp-config", +) + +cc_toolchain_config( + name = "x86_64-clang12.0.5-libcpp-config", + cpu = "x86_64", + compiler = "clang12.0.5", + version = "libcpp", +) + +toolchain( + name = "x86_64-clang12.0.5-libcpp_toolchain", + target_compatible_with = [ + "@platforms//os:android", + "@platforms//cpu:x86_64", + ], + toolchain = "@androidndk//:x86_64-clang12.0.5-libcpp", + toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", +) + +filegroup( + name = "x86_64-clang12.0.5-libcpp-all_files", + srcs = glob(["ndk/toolchains/llvm/**"]) + glob([ + "ndk/toolchains/llvm/prebuilt/linux-x86_64/sysroot/**/*", + "ndk/sources/cxx-stl/llvm-libc++/include/**/*", + "ndk/sources/cxx-stl/llvm-libc++abi/include/**/*", + "ndk/sources/android/support/include/**/*", + "ndk/sysroot/**/*", + ]) + [ + ":libcpp-x86_64-dynamic-runtime-libraries", + ":libcpp-x86_64-static-runtime-libraries", + ], +) + + + +################################################################ +# STL runtime library filegroups +################################################################ + +filegroup( + name = "libcpp-arm64-v8a-dynamic-runtime-libraries", + srcs = glob(["ndk/sources/cxx-stl/llvm-libc++/libs/arm64-v8a/*.so"]), +) +filegroup( + name = "libcpp-arm64-v8a-static-runtime-libraries", + srcs = glob(["ndk/sources/cxx-stl/llvm-libc++/libs/arm64-v8a/*.a"]), +) +filegroup( + name = "libcpp-armeabi-v7a-dynamic-runtime-libraries", + srcs = glob(["ndk/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/*.so"]), +) +filegroup( + name = "libcpp-armeabi-v7a-static-runtime-libraries", + srcs = glob(["ndk/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/*.a"]), +) +filegroup( + name = "libcpp-x86-dynamic-runtime-libraries", + srcs = glob(["ndk/sources/cxx-stl/llvm-libc++/libs/x86/*.so"]), +) +filegroup( + name = "libcpp-x86-static-runtime-libraries", + srcs = glob(["ndk/sources/cxx-stl/llvm-libc++/libs/x86/*.a"]), +) +filegroup( + name = "libcpp-x86_64-dynamic-runtime-libraries", + srcs = glob(["ndk/sources/cxx-stl/llvm-libc++/libs/x86_64/*.so"]), +) +filegroup( + name = "libcpp-x86_64-static-runtime-libraries", + srcs = glob(["ndk/sources/cxx-stl/llvm-libc++/libs/x86_64/*.a"]), +) + + +################################################################ +# Vulkan validation layers libraries +################################################################ + +cc_library( + name = "vulkan_validation_layers_aarch64-linux-android-clang12.0.5-libcpp", + srcs = glob(["ndk/sources/third_party/vulkan/src/build-android/jniLibs/arm64-v8a/libVkLayer_*.so"]), +) +cc_library( + name = "vulkan_validation_layers_arm-linux-androideabi-clang12.0.5-v7a-libcpp", + srcs = glob(["ndk/sources/third_party/vulkan/src/build-android/jniLibs/armeabi-v7a/libVkLayer_*.so"]), +) +cc_library( + name = "vulkan_validation_layers_x86-clang12.0.5-libcpp", + srcs = glob(["ndk/sources/third_party/vulkan/src/build-android/jniLibs/x86/libVkLayer_*.so"]), +) +cc_library( + name = "vulkan_validation_layers_x86_64-clang12.0.5-libcpp", + srcs = glob(["ndk/sources/third_party/vulkan/src/build-android/jniLibs/x86_64/libVkLayer_*.so"]), +) + + +################################################################ +# Miscellaneous runtime libraries +################################################################ + +cc_library( + name = "cpufeatures", + srcs = glob(["ndk/sources/android/cpufeatures/*.c"]), + hdrs = glob(["ndk/sources/android/cpufeatures/*.h"]), + linkopts = ["-ldl"], +) + diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r23/NdkMajorRevisionR23.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r23/NdkMajorRevisionR23.java index 680b9c78131b99..f4dbfbe8302649 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r23/NdkMajorRevisionR23.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r23/NdkMajorRevisionR23.java @@ -14,29 +14,52 @@ package com.google.devtools.build.lib.bazel.rules.android.ndkcrosstools.r23; +import com.google.common.collect.ImmutableList; 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.util.ResourceFileLoader; import com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.CrosstoolRelease; +import java.io.IOException; +import sun.reflect.generics.reflectiveObjects.NotImplementedException; /** Logic specific to Android NDK R23. */ public class NdkMajorRevisionR23 implements NdkMajorRevision { - private final String clangVersion; - public NdkMajorRevisionR23(String clangVersion) { - this.clangVersion = clangVersion; + public NdkMajorRevisionR23() { } @Override public CrosstoolRelease crosstoolRelease( NdkPaths ndkPaths, StlImpl stlImpl, String hostPlatform) { - return AndroidNdkCrosstoolsR23.create(ndkPaths, stlImpl, hostPlatform, clangVersion); + throw new NotImplementedException(); } @Override public ApiLevel apiLevel(EventHandler eventHandler, String name, String apiLevel) { - return new ApiLevelR23(eventHandler, name, apiLevel); + throw new NotImplementedException(); + } + + @Override + public boolean isStatic() { + return true; + } + + @Override + public ImmutableList getStaticFiles() { + return ImmutableList.of( + new NdkFile("BUILD.bazel", getResourceFileContent("BUILD.bazel.txt")), + new NdkFile("cc_toolchain_config.bzl", getResourceFileContent("cc_toolchain_config.bzl.txt")) + ); + } + + private static String getResourceFileContent(String filename) { + try { + return ResourceFileLoader.loadResource(NdkMajorRevisionR23.class, filename); + } catch (IOException e) { + throw new IllegalStateException(e); + } } } diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r23/X86Crosstools.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r23/X86Crosstools.java deleted file mode 100644 index 5a3f2e65cecb15..00000000000000 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r23/X86Crosstools.java +++ /dev/null @@ -1,114 +0,0 @@ -// Copyright 2021 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.r23; - -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 createCrosstools() { - String targetPlatform = "x86-none-linux-android"; - /** 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("llvm", "x86-none-linux-android", null)) - .setBuiltinSysroot(ndkPaths.createClangBuiltinSysroot()); - - stlImpl.addStlImpl(x86Clang, null); - - /** x86_64 */ - CToolchain.Builder x8664Clang = - createBaseX86ClangToolchain("x86_64", "x86_64", "x86_64-linux-android") - .setTargetCpu("x86_64") - .setToolchainIdentifier("x86_64-clang" + clangVersion) - .addAllToolPath(ndkPaths.createClangToolpaths("llvm", "x86_64-none-linux-android", null)) - .setBuiltinSysroot(ndkPaths.createClangBuiltinSysroot()); - - 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" + ndkPaths.getCorrectedApiLevel("x86"); - - CToolchain.Builder cToolchainBuilder = CToolchain.newBuilder(); - - cToolchainBuilder - .setCompiler("clang" + clangVersion) - .addCxxBuiltinIncludeDirectory( - ndkPaths.createClangToolchainBuiltinIncludeDirectory(clangVersion)) - - // Compiler flags - .addCompilerFlag("-target") - .addCompilerFlag(llvmTriple) - .addCompilerFlag("-fPIC") - .addCompilerFlag("-D__ANDROID_API__=" + ndkPaths.getCorrectedApiLevel(x86Arch)) - - // Linker flags - .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/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r23/cc_toolchain_config.bzl.txt b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r23/cc_toolchain_config.bzl.txt new file mode 100644 index 00000000000000..248952d835597d --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/r23/cc_toolchain_config.bzl.txt @@ -0,0 +1,571 @@ +"""Definition of cc_toolchain_config for NDK R23 version. + +Cc_toolchain depends on targets of this rule to construct the command lines +and to detect which C++ features are supported by this toolchain. +""" +load( + "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", + "action_config", + "artifact_name_pattern", + "env_entry", + "env_set", + "feature", + "feature_set", + "flag_group", + "flag_set", + "make_variable", + "tool", + "tool_path", + "variable_with_value", + "with_feature_set", +) +load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES") + +def _impl(ctx): + cpu = ctx.attr.cpu + compiler = ctx.attr.compiler + version = ctx.attr.version + all_compile_actions = [ + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.assemble, + ACTION_NAMES.preprocess_assemble, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.clif_match, + ACTION_NAMES.lto_backend, + ] + + all_link_actions = [ + ACTION_NAMES.cpp_link_executable, + ACTION_NAMES.cpp_link_dynamic_library, + ACTION_NAMES.cpp_link_nodeps_dynamic_library, + ] + + opt_feature = feature(name = "opt") + fastbuild_feature = feature(name = "fastbuild") + dbg_feature = feature(name = "dbg") + supports_dynamic_linker_feature = feature(name = "supports_dynamic_linker", enabled = True) + supports_pic_feature = feature(name = "supports_pic", enabled = True) + static_link_cpp_runtimes_feature = feature(name = "static_link_cpp_runtimes", enabled = True) + + + default_compile_flags = [] + unfiltered_compile_flags = [] + default_link_flags = [] + default_fastbuild_flags = [] + default_dbg_flags = [] + default_opt_flags = [] + cxx_builtin_include_directories = [] + target_cpu = None + toolchain_identifier = None + host_system_name = None + target_system_name = None + abi_version = None + abi_libc_version = None + target_libc = None + target_compiler = None + abi_version = None + builtin_sysroot = None + ar_path = None + cpp_path = None + dwp_path = None + gcc_path = None + gcov_path = None + gcov_tool_path = None + ld_path = None + nm_path = None + objcopy_path = None + objdump_path = None + strip_path = None + llvm_profdata_path = None + + if cpu == 'arm64-v8a' and compiler == 'clang12.0.5' and version == 'libcpp': + print("arm64-v8a") + default_compile_flags = [ + '-target', + 'aarch64-none-linux-android29', + '-fpic', + '-no-canonical-prefixes', + '-Wno-invalid-command-line-argument', + '-Wno-unused-command-line-argument', + '-funwind-tables', + '-fstack-protector-strong', + '-fno-addrsig', + '-Werror=return-type', + '-Werror=int-to-pointer-cast', + '-Werror=pointer-to-int-cast', + '-Werror=implicit-function-declaration', + ] + unfiltered_compile_flags = [ + '-isystem', + 'external/androidndk/ndk/sources/cxx-stl/llvm-libc++/include', + '-isystem', + 'external/androidndk/ndk/sources/cxx-stl/llvm-libc++abi/include', + '-isystem', + 'external/androidndk/ndk/sources/android/support/include', + '-isystemexternal/androidndk/ndk/sysroot/usr/include', + ] + default_link_flags = [ + '-target', + 'aarch64-none-linux-android29', + '-Lexternal/androidndk/ndk/sources/cxx-stl/llvm-libc++/libs/arm64-v8a', + '-no-canonical-prefixes', + '-Wl,-z,relro', + '-Wl,--gc-sections', + ] + default_fastbuild_flags = [''] + default_dbg_flags = [ + '-O0', + '-g', + '-UNDEBUG', + ] + default_opt_flags = [ + '-O2', + '-g', + '-DNDEBUG', + ] + cxx_builtin_include_directories = [ + 'external/androidndk/ndk/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/12.0.5/include', + '%sysroot%/usr/include', + 'external/androidndk/ndk/sysroot/usr/include', + ] + target_cpu = 'arm64-v8a' + toolchain_identifier = 'aarch64-linux-android-clang12.0.5-libcpp' + host_system_name = 'linux-x86_64' + target_system_name = 'aarch64-none-linux-android' + target_libc = 'local' + target_compiler = 'clang12.0.5' + abi_version = 'arm64-v8a' + abi_libc_version = 'local' + builtin_sysroot = 'external/androidndk/ndk/toolchains/llvm/prebuilt/linux-x86_64/sysroot' + ar_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-ar' + cpp_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-none-linux-android-cpp' + gcov_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-none-linux-android-gcov' + gcov_tool_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-none-linux-android-gcov-tool' + ld_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-none-linux-android-ld' + llvm_cov_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-none-linux-android-llvm-cov' + nm_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-none-linux-android-nm' + objcopy_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-none-linux-android-objcopy' + objdump_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-none-linux-android-objdump' + strip_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-none-linux-android-strip' + dwp_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-none-linux-android-dwp' + llvm_profdata_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-none-linux-android-llvm-profdata' + gcc_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/clang' + + if cpu == 'armeabi-v7a' and compiler == 'clang12.0.5' and version == 'libcpp': + print("armeabi-v7a") + default_compile_flags = [ + '-D__ANDROID_API__=29', + '-target', + 'arm-none-linux-android29', + '-march=armv7-a', + '-mfloat-abi=softfp', + '-mfpu=vfpv3-d16', + '-fpic', + '-no-canonical-prefixes', + '-Wno-invalid-command-line-argument', + '-Wno-unused-command-line-argument', + '-funwind-tables', + '-fstack-protector-strong', + '-fno-addrsig', + '-Werror=return-type', + '-Werror=int-to-pointer-cast', + '-Werror=pointer-to-int-cast', + '-Werror=implicit-function-declaration', + ] + unfiltered_compile_flags = [ + #'-isystem', + #'external/androidndk/ndk/sources/cxx-stl/llvm-libc++/include', + #'-isystem', + #'external/androidndk/ndk/sources/cxx-stl/llvm-libc++abi/include', + #'-isystem', + #'external/androidndk/ndk/sources/android/support/include', + #'-isystemexternal/androidndk/ndk/sysroot/usr/include', + ] + default_link_flags = [ + '-target', + 'arm-none-linux-android29', + #'-Lexternal/androidndk/ndk/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a', + '-no-canonical-prefixes', + '-Wl,-z,relro', + '-Wl,--gc-sections', + ] + default_fastbuild_flags = [''] + default_dbg_flags = [ + '-g', + '-fno-strict-aliasing', + '-O0', + '-UNDEBUG', + ] + default_opt_flags = [ + '-mthumb', + '-Os', + '-g', + '-DNDEBUG', + ] + cxx_builtin_include_directories = [ + #'external/androidndk/ndk/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/12.0.5/include', + #'%sysroot%/usr/include', + #'external/androidndk/ndk/sysroot/usr/include', + ] + target_cpu = 'armeabi-v7a' + toolchain_identifier = 'arm-linux-androideabi-clang12.0.5-v7a-libcpp' + host_system_name = 'linux-x86_64' + target_system_name = 'arm-linux-androideabi' + target_libc = 'local' + target_compiler = 'clang12.0.5' + abi_version = 'armeabi-v7a' + abi_libc_version = 'local' + #builtin_sysroot = 'external/androidndk/ndk/toolchains/llvm/prebuilt/linux-x86_64/sysroot' + ar_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-ar' + cpp_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/arm-linux-androideabi-cpp' + gcov_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcov' + gcov_tool_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcov-tool' + ld_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/arm-linux-androideabi-ld' + llvm_cov_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/arm-linux-androideabi-llvm-cov' + nm_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/arm-linux-androideabi-nm' + objcopy_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/arm-linux-androideabi-objcopy' + objdump_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/arm-linux-androideabi-objdump' + strip_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/arm-linux-androideabi-strip' + dwp_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/arm-linux-androideabi-dwp' + llvm_profdata_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/arm-linux-androideabi-llvm-profdata' + gcc_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/clang' + + if cpu == 'x86' and compiler == 'clang12.0.5' and version == 'libcpp': + print("x86") + default_compile_flags = [ + '-target', + 'i686-none-linux-android29', + '-fPIC', + '-D__ANDROID_API__=29', + '-mstackrealign', + '-no-canonical-prefixes', + '-Wno-invalid-command-line-argument', + '-Wno-unused-command-line-argument', + '-funwind-tables', + '-fstack-protector-strong', + '-fno-addrsig', + '-Werror=return-type', + '-Werror=int-to-pointer-cast', + '-Werror=pointer-to-int-cast', + '-Werror=implicit-function-declaration', + ] + unfiltered_compile_flags = [ + #'-isystem', + #'external/androidndk/ndk/sources/cxx-stl/llvm-libc++/include', + #'-isystem', + #'external/androidndk/ndk/sources/cxx-stl/llvm-libc++abi/include', + #'-isystem', + #'external/androidndk/ndk/sources/android/support/include', + #'-isystemexternal/androidndk/ndk/sysroot/usr/include', + ] + default_link_flags = ['-target', + 'i686-none-linux-android29', + #'-Lexternal/androidndk/ndk/sources/cxx-stl/llvm-libc++/libs/x86', + '-no-canonical-prefixes', + '-Wl,-z,relro', + '-Wl,--gc-sections', + ] + default_fastbuild_flags = [''] + default_dbg_flags = [ + '-O0', + '-g', + ] + default_opt_flags = [ + '-O2', + '-g', + '-DNDEBUG', + ] + cxx_builtin_include_directories = [ + #'external/androidndk/ndk/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/12.0.5/include', + #'%sysroot%/usr/include', + #'external/androidndk/ndk/sysroot/usr/include', + ] + target_cpu = 'x86' + toolchain_identifier = 'x86-clang12.0.5-libcpp' + host_system_name = 'linux-x86_64' + target_system_name = 'x86-linux-android' + target_libc = 'local' + target_compiler = 'clang12.0.5' + abi_version = 'x86' + abi_libc_version = 'local' + #builtin_sysroot = 'external/androidndk/ndk/toolchains/llvm/prebuilt/linux-x86_64/sysroot' + ar_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-ar' + cpp_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/x86-none-linux-android-cpp' + gcov_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/x86-none-linux-android-gcov' + gcov_tool_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/x86-none-linux-android-gcov-tool' + ld_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/x86-none-linux-android-ld' + llvm_cov_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/x86-none-linux-android-llvm-cov' + nm_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/x86-none-linux-android-nm' + objcopy_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/x86-none-linux-android-objcopy' + objdump_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/x86-none-linux-android-objdump' + strip_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/x86-none-linux-android-strip' + dwp_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/x86-none-linux-android-dwp' + llvm_profdata_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/x86-none-linux-android-llvm-profdata' + gcc_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/clang' + + if cpu == 'x86_64' and compiler == 'clang12.0.5' and version == 'libcpp': + print("x86_64") + default_compile_flags = [ + '-target', + 'x86_64-none-linux-android29', + '-fPIC', + '-D__ANDROID_API__=29', + '-no-canonical-prefixes', + '-Wno-invalid-command-line-argument', + '-Wno-unused-command-line-argument', + '-funwind-tables', + '-fstack-protector-strong', + '-fno-addrsig', + '-Werror=return-type', + '-Werror=int-to-pointer-cast', + '-Werror=pointer-to-int-cast', + '-Werror=implicit-function-declaration', + ] + unfiltered_compile_flags = [ + '-isystem', + 'external/androidndk/ndk/sources/cxx-stl/llvm-libc++/include', + '-isystem', + 'external/androidndk/ndk/sources/cxx-stl/llvm-libc++abi/include', + '-isystem', + 'external/androidndk/ndk/sources/android/support/include', + '-isystemexternal/androidndk/ndk/sysroot/usr/include', + ] + default_link_flags = [ + '-target', + 'x86_64-none-linux-android29', + '-Lexternal/androidndk/ndk/sources/cxx-stl/llvm-libc++/libs/x86_64', + '-no-canonical-prefixes', + '-Wl,-z,relro', + '-Wl,--gc-sections', + ] + default_fastbuild_flags = [''] + default_dbg_flags = [ + '-O0', + '-g', + ] + default_opt_flags = [ + '-O2', + '-g', + '-DNDEBUG', + ] + cxx_builtin_include_directories = [ + 'external/androidndk/ndk/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/12.0.5/include', + '%sysroot%/usr/include', + 'external/androidndk/ndk/sysroot/usr/include', + ] + target_cpu = 'x86_64' + toolchain_identifier = 'x86_64-clang12.0.5-libcpp' + host_system_name = 'linux-x86_64' + target_system_name = 'x86-linux-android' + target_libc = 'local' + target_compiler = 'clang12.0.5' + abi_version = 'x86_64' + abi_libc_version = 'local' + builtin_sysroot = 'external/androidndk/ndk/toolchains/llvm/prebuilt/linux-x86_64/sysroot' + ar_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-ar' + cpp_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/x86_64-none-linux-android-cpp' + gcov_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/x86_64-none-linux-android-gcov' + gcov_tool_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/x86_64-none-linux-android-gcov-tool' + ld_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/x86_64-none-linux-android-ld' + llvm_cov_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/x86_64-none-linux-android-llvm-cov' + nm_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/x86_64-none-linux-android-nm' + objcopy_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/x86_64-none-linux-android-objcopy' + objdump_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/x86_64-none-linux-android-objdump' + strip_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/x86_64-none-linux-android-strip' + dwp_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/x86_64-none-linux-android-dwp' + llvm_profdata_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/x86_64-none-linux-android-llvm-profdata' + gcc_path = 'ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/clang' + + + default_compile_flag_sets = [] + if default_compile_flags: + default_compile_flag_sets.append( + flag_set( + actions = all_compile_actions, + flag_groups = [ + flag_group( + flags = default_compile_flags, + ), + ], + ), + ) + if default_fastbuild_flags: + default_compile_flag_sets.append( + flag_set( + actions = all_compile_actions, + flag_groups = [flag_group(flags = default_fastbuild_flags)], + with_features = [with_feature_set(features = ["fastbuild"])], + ), + ) + if default_dbg_flags: + default_compile_flag_sets.append( + flag_set( + actions = all_compile_actions, + flag_groups = [flag_group(flags = default_dbg_flags)], + with_features = [with_feature_set(features = ["dbg"])], + ), + ) + if default_opt_flags: + default_compile_flag_sets.append( + flag_set( + actions = all_compile_actions, + flag_groups = [flag_group(flags = default_opt_flags)], + with_features = [with_feature_set(features = ["opt"])], + ), + ) + + default_compile_flags_feature = feature( + name = "default_compile_flags", + enabled = True, + flag_sets = default_compile_flag_sets, + ) + + default_link_flag_sets = [] + if default_link_flags: + default_link_flag_sets.append( + flag_set( + actions = all_link_actions, + flag_groups = [ + flag_group( + flags = default_link_flags, + ), + ], + ), + ) + + default_link_flags_feature = feature( + name = "default_link_flags", + enabled = True, + flag_sets = default_link_flag_sets, + ) + + user_compile_flags_feature = feature( + name = "user_compile_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = all_compile_actions, + flag_groups = [ + flag_group( + flags = ["%{user_compile_flags}"], + iterate_over = "user_compile_flags", + expand_if_available = "user_compile_flags", + ), + ], + ), + ], + ) + + sysroot_feature = feature( + name = "sysroot", + enabled = True, + flag_sets = [ + flag_set( + actions = all_compile_actions + all_link_actions, + flag_groups = [ + flag_group( + flags = ["--sysroot=%{sysroot}"], + expand_if_available = "sysroot", + ), + ], + ), + ], + ) + + unfiltered_compile_flag_sets = [] + if unfiltered_compile_flags: + unfiltered_compile_flag_sets.append( + flag_set( + actions = all_compile_actions, + flag_groups = [ + flag_group( + flags = unfiltered_compile_flags, + ), + ], + )) + unfiltered_compile_flags_feature = feature( + name = "unfiltered_compile_flags", + enabled = True, + flag_sets = unfiltered_compile_flag_sets, + ) + + features = [ + default_compile_flags_feature, + default_link_flags_feature, + supports_dynamic_linker_feature, + supports_pic_feature, + static_link_cpp_runtimes_feature, + fastbuild_feature, + dbg_feature, + opt_feature, + user_compile_flags_feature, + sysroot_feature, + unfiltered_compile_flags_feature, + ] + + tool_paths = [] + if ar_path: + tool_paths.append(tool_path(name = "ar", path = ar_path)) + if cpp_path: + tool_paths.append(tool_path(name = "cpp", path = cpp_path)) + if dwp_path: + tool_paths.append(tool_path(name = "dwp", path = dwp_path)) + if gcc_path: + tool_paths.append(tool_path(name = "gcc", path = gcc_path)) + if gcov_path: + tool_paths.append(tool_path(name = "gcov", path = gcov_path)) + if gcov_tool_path: + tool_paths.append(tool_path(name = "gcov", path = gcov_tool_path)) + if ld_path: + tool_paths.append(tool_path(name = "ld", path = ld_path)) + if nm_path: + tool_paths.append(tool_path(name = "nm", path = nm_path)) + if objcopy_path: + tool_paths.append(tool_path(name = "objcopy", path = objcopy_path)) + if objdump_path: + tool_paths.append(tool_path(name = "objdump", path = objdump_path)) + if strip_path: + tool_paths.append(tool_path(name = "strip", path = strip_path)) + if llvm_profdata_path: + tool_paths.append(tool_path(name = "llvm_profdata", path = llvm_profdata_path)) + + out = ctx.actions.declare_file(ctx.label.name) + ctx.actions.write(out, "Fake executable") + return [ + cc_common.create_cc_toolchain_config_info( + ctx = ctx, + features = features, + action_configs = [], + artifact_name_patterns = [], + cxx_builtin_include_directories = cxx_builtin_include_directories, + toolchain_identifier = toolchain_identifier, + host_system_name = host_system_name, + target_system_name = target_system_name, + target_cpu = target_cpu, + target_libc = target_libc, + compiler = target_compiler, + abi_version = abi_version, + abi_libc_version = abi_libc_version, + tool_paths = tool_paths, + make_variables = [], + builtin_sysroot = builtin_sysroot, + ), + DefaultInfo( + executable = out, + ), + ] + +cc_toolchain_config = rule( + implementation = _impl, + attrs = { + "cpu": attr.string(mandatory = True), + "compiler": attr.string(mandatory = True), + "version": attr.string(mandatory = True), + }, + provides = [CcToolchainConfigInfo], + executable = True, +)