| 1 |
""" |
| 2 |
This file is copied from the SkCMS repository. Original file: |
| 3 |
https://skia.googlesource.com/skcms/+/ba39d81f9797aa973bdf01aa6b0363b280352fba/toolchain/ndk_linux_arm64_toolchain_config.bzl |
| 4 |
|
| 5 |
This module defines the ndk_cc_toolchain_config rule. |
| 6 |
|
| 7 |
This file is based on the `external/androidndk/cc_toolchain_config.bzl` file produced by the |
| 8 |
built-in `android_ndk_repository` Bazel rule[1], which was used to build the SkCMS repository up |
| 9 |
until this revision[2]. |
| 10 |
|
| 11 |
The paths in this file point to locations inside the expanded Android NDK ZIP file (found at |
| 12 |
external/ndk_linux_amd64), and must be updated every time we upgrade to a new Android NDK version. |
| 13 |
|
| 14 |
[1] https://github.com/bazelbuild/bazel/blob/4710ef82ce34572878e07c52e83a0144d707f140/src/main/java/com/google/devtools/build/lib/bazel/rules/android/AndroidNdkRepositoryFunction.java#L422 |
| 15 |
[2] https://skia.googlesource.com/skcms/+/30c8e303800c256febb03a09fdcda7f75d119b1b/WORKSPACE#22 |
| 16 |
""" |
| 17 |
|
| 18 |
load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES") |
| 19 |
load( |
| 20 |
"@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", |
| 21 |
"feature", |
| 22 |
"flag_group", |
| 23 |
"flag_set", |
| 24 |
"tool_path", |
| 25 |
"with_feature_set", |
| 26 |
) |
| 27 |
load("@rules_cc//cc/common:cc_common.bzl", "cc_common") |
| 28 |
load(":download_ndk_linux_amd64_toolchain.bzl", "NDK_PATH") |
| 29 |
|
| 30 |
# Supported CPUs. |
| 31 |
_ARMEABI_V7A = "armeabi-v7a" |
| 32 |
_ARM64_V8A = "arm64-v8a" |
| 33 |
|
| 34 |
_all_compile_actions = [ |
| 35 |
ACTION_NAMES.c_compile, |
| 36 |
ACTION_NAMES.cpp_compile, |
| 37 |
ACTION_NAMES.linkstamp_compile, |
| 38 |
ACTION_NAMES.assemble, |
| 39 |
ACTION_NAMES.preprocess_assemble, |
| 40 |
ACTION_NAMES.cpp_header_parsing, |
| 41 |
ACTION_NAMES.cpp_module_compile, |
| 42 |
ACTION_NAMES.cpp_module_codegen, |
| 43 |
ACTION_NAMES.clif_match, |
| 44 |
ACTION_NAMES.lto_backend, |
| 45 |
] |
| 46 |
|
| 47 |
_all_link_actions = [ |
| 48 |
ACTION_NAMES.cpp_link_executable, |
| 49 |
ACTION_NAMES.cpp_link_dynamic_library, |
| 50 |
ACTION_NAMES.cpp_link_nodeps_dynamic_library, |
| 51 |
] |
| 52 |
|
| 53 |
def _get_default_compile_flags(cpu): |
| 54 |
if cpu == _ARMEABI_V7A: |
| 55 |
return [ |
| 56 |
"-D__ANDROID_API__=29", |
| 57 |
"-isystem", |
| 58 |
NDK_PATH + "/sysroot/usr/include/arm-linux-androideabi", |
| 59 |
"-target", |
| 60 |
"armv7-none-linux-androideabi", |
| 61 |
"-march=armv7-a", |
| 62 |
"-mfloat-abi=softfp", |
| 63 |
"-mfpu=vfpv3-d16", |
| 64 |
"-gcc-toolchain", |
| 65 |
NDK_PATH + "/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64", |
| 66 |
"-fpic", |
| 67 |
"-no-canonical-prefixes", |
| 68 |
"-Wno-invalid-command-line-argument", |
| 69 |
"-Wno-unused-command-line-argument", |
| 70 |
"-funwind-tables", |
| 71 |
"-fstack-protector-strong", |
| 72 |
"-fno-addrsig", |
| 73 |
] |
| 74 |
if cpu == _ARM64_V8A: |
| 75 |
return [ |
| 76 |
"-gcc-toolchain", |
| 77 |
NDK_PATH + "/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64", |
| 78 |
"-target", |
| 79 |
"aarch64-none-linux-android", |
| 80 |
"-fpic", |
| 81 |
"-isystem", |
| 82 |
NDK_PATH + "/sysroot/usr/include/aarch64-linux-android", |
| 83 |
"-D__ANDROID_API__=29", |
| 84 |
"-no-canonical-prefixes", |
| 85 |
"-Wno-invalid-command-line-argument", |
| 86 |
"-Wno-unused-command-line-argument", |
| 87 |
"-funwind-tables", |
| 88 |
"-fstack-protector-strong", |
| 89 |
"-fno-addrsig", |
| 90 |
] |
| 91 |
fail("Unknown CPU: " + cpu) |
| 92 |
|
| 93 |
def _get_default_link_flags(cpu): |
| 94 |
if cpu == _ARMEABI_V7A: |
| 95 |
return [ |
| 96 |
"-target", |
| 97 |
"armv7-none-linux-androideabi", |
| 98 |
"-gcc-toolchain", |
| 99 |
NDK_PATH + "/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64", |
| 100 |
"-L", |
| 101 |
NDK_PATH + "/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a", |
| 102 |
"-no-canonical-prefixes", |
| 103 |
"-Wl,-z,relro", |
| 104 |
"-lm", |
| 105 |
] |
| 106 |
if cpu == _ARM64_V8A: |
| 107 |
return [ |
| 108 |
"-gcc-toolchain", |
| 109 |
NDK_PATH + "/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64", |
| 110 |
"-target", |
| 111 |
"aarch64-none-linux-android", |
| 112 |
"-L", |
| 113 |
NDK_PATH + "/sources/cxx-stl/llvm-libc++/libs/arm64-v8a", |
| 114 |
"-no-canonical-prefixes", |
| 115 |
"-Wl,-z,relro", |
| 116 |
"-lm", |
| 117 |
] |
| 118 |
fail("Unknown CPU: " + cpu) |
| 119 |
|
| 120 |
def _get_default_dbg_flags(cpu): |
| 121 |
if cpu == _ARMEABI_V7A: |
| 122 |
return ["-g", "-fno-strict-aliasing", "-O0", "-UNDEBUG"] |
| 123 |
if cpu == _ARM64_V8A: |
| 124 |
return ["-O0", "-g", "-UNDEBUG"] |
| 125 |
fail("Unknown CPU: " + cpu) |
| 126 |
|
| 127 |
def _get_default_opt_flags(cpu): |
| 128 |
if cpu == _ARMEABI_V7A: |
| 129 |
return ["-mthumb", "-Os", "-g", "-DNDEBUG"] |
| 130 |
if cpu == _ARM64_V8A: |
| 131 |
return ["-O2", "-g", "-DNDEBUG"] |
| 132 |
fail("Unknown CPU: " + cpu) |
| 133 |
|
| 134 |
def _get_toolchain_identifier(cpu): |
| 135 |
if cpu == _ARMEABI_V7A: |
| 136 |
return "ndk-armeabi-v7a-toolchain" |
| 137 |
if cpu == _ARM64_V8A: |
| 138 |
return "ndk-arm64-v8a-toolchain" |
| 139 |
fail("Unknown CPU: " + cpu) |
| 140 |
|
| 141 |
def _get_target_system_name(cpu): |
| 142 |
if cpu == _ARMEABI_V7A: |
| 143 |
return "arm-linux-androideabi" |
| 144 |
if cpu == _ARM64_V8A: |
| 145 |
return "aarch64-linux-android" |
| 146 |
fail("Unknown CPU: " + cpu) |
| 147 |
|
| 148 |
def _get_builtin_sysroot(cpu): |
| 149 |
if cpu == _ARMEABI_V7A: |
| 150 |
return NDK_PATH + "/platforms/android-29/arch-arm" |
| 151 |
if cpu == _ARM64_V8A: |
| 152 |
return NDK_PATH + "/platforms/android-29/arch-arm64" |
| 153 |
fail("Unknown CPU: " + cpu) |
| 154 |
|
| 155 |
def _get_tool_paths(cpu): |
| 156 |
# The cc_common.create_cc_toolchain_config_info function expects tool paths to point to files |
| 157 |
# under the directory in which it is invoked. This means we cannot directly reference tools |
| 158 |
# under external/android_ndk. The solution is to use "trampoline" scripts that pass through |
| 159 |
# any command-line arguments to the NDK binaries under external/android_sdk. |
| 160 |
|
| 161 |
if cpu == _ARMEABI_V7A: |
| 162 |
return [ |
| 163 |
tool_path( |
| 164 |
name = "ar", |
| 165 |
path = "android_trampolines/arm-linux-androideabi-ar.sh", |
| 166 |
), |
| 167 |
tool_path( |
| 168 |
name = "cpp", |
| 169 |
path = "android_trampolines/clang.sh", |
| 170 |
), |
| 171 |
tool_path( |
| 172 |
name = "dwp", |
| 173 |
path = "android_trampolines/arm-linux-androideabi-dwp.sh", |
| 174 |
), |
| 175 |
tool_path( |
| 176 |
name = "gcc", |
| 177 |
path = "android_trampolines/clang.sh", |
| 178 |
), |
| 179 |
tool_path( |
| 180 |
name = "gcov", |
| 181 |
path = "/bin/false", |
| 182 |
), |
| 183 |
tool_path( |
| 184 |
name = "ld", |
| 185 |
path = "android_trampolines/arm-linux-androideabi-ld.sh", |
| 186 |
), |
| 187 |
tool_path( |
| 188 |
name = "nm", |
| 189 |
path = "android_trampolines/arm-linux-androideabi-nm.sh", |
| 190 |
), |
| 191 |
tool_path( |
| 192 |
name = "objcopy", |
| 193 |
path = "android_trampolines/arm-linux-androideabi-objcopy.sh", |
| 194 |
), |
| 195 |
tool_path( |
| 196 |
name = "objdump", |
| 197 |
path = "android_trampolines/arm-linux-androideabi-objdump.sh", |
| 198 |
), |
| 199 |
tool_path( |
| 200 |
name = "strip", |
| 201 |
path = "android_trampolines/arm-linux-androideabi-strip.sh", |
| 202 |
), |
| 203 |
] |
| 204 |
if cpu == _ARM64_V8A: |
| 205 |
return [ |
| 206 |
tool_path( |
| 207 |
name = "ar", |
| 208 |
path = "android_trampolines/aarch64-linux-android-ar.sh", |
| 209 |
), |
| 210 |
tool_path( |
| 211 |
name = "cpp", |
| 212 |
path = "android_trampolines/clang.sh", |
| 213 |
), |
| 214 |
tool_path( |
| 215 |
name = "dwp", |
| 216 |
path = "android_trampolines/aarch64-linux-android-dwp.sh", |
| 217 |
), |
| 218 |
tool_path( |
| 219 |
name = "gcc", |
| 220 |
path = "android_trampolines/clang.sh", |
| 221 |
), |
| 222 |
tool_path( |
| 223 |
name = "gcov", |
| 224 |
path = "/bin/false", |
| 225 |
), |
| 226 |
tool_path( |
| 227 |
name = "ld", |
| 228 |
path = "android_trampolines/aarch64-linux-android-ld.sh", |
| 229 |
), |
| 230 |
tool_path( |
| 231 |
name = "nm", |
| 232 |
path = "android_trampolines/aarch64-linux-android-nm.sh", |
| 233 |
), |
| 234 |
tool_path( |
| 235 |
name = "objcopy", |
| 236 |
path = "android_trampolines/aarch64-linux-android-objcopy.sh", |
| 237 |
), |
| 238 |
tool_path( |
| 239 |
name = "objdump", |
| 240 |
path = "android_trampolines/aarch64-linux-android-objdump.sh", |
| 241 |
), |
| 242 |
tool_path( |
| 243 |
name = "strip", |
| 244 |
path = "android_trampolines/aarch64-linux-android-strip.sh", |
| 245 |
), |
| 246 |
] |
| 247 |
fail("Unknown CPU: " + cpu) |
| 248 |
|
| 249 |
def _ndk_cc_toolchain_config_impl(ctx): |
| 250 |
default_compile_flags = _get_default_compile_flags(ctx.attr.cpu) |
| 251 |
unfiltered_compile_flags = [ |
| 252 |
"-isystem", |
| 253 |
NDK_PATH + "/sources/cxx-stl/llvm-libc++/include", |
| 254 |
"-isystem", |
| 255 |
NDK_PATH + "/sources/cxx-stl/llvm-libc++abi/include", |
| 256 |
"-isystem", |
| 257 |
NDK_PATH + "/sources/android/support/include", |
| 258 |
"-isystem", |
| 259 |
NDK_PATH + "/sysroot/usr/include", |
| 260 |
] |
| 261 |
default_link_flags = _get_default_link_flags(ctx.attr.cpu) |
| 262 |
default_fastbuild_flags = [""] |
| 263 |
default_dbg_flags = _get_default_dbg_flags(ctx.attr.cpu) |
| 264 |
default_opt_flags = _get_default_opt_flags(ctx.attr.cpu) |
| 265 |
|
| 266 |
opt_feature = feature(name = "opt") |
| 267 |
fastbuild_feature = feature(name = "fastbuild") |
| 268 |
dbg_feature = feature(name = "dbg") |
| 269 |
supports_dynamic_linker_feature = feature(name = "supports_dynamic_linker", enabled = True) |
| 270 |
supports_pic_feature = feature(name = "supports_pic", enabled = True) |
| 271 |
static_link_cpp_runtimes_feature = feature(name = "static_link_cpp_runtimes", enabled = True) |
| 272 |
|
| 273 |
default_compile_flags_feature = feature( |
| 274 |
name = "default_compile_flags", |
| 275 |
enabled = True, |
| 276 |
flag_sets = [ |
| 277 |
flag_set( |
| 278 |
actions = _all_compile_actions, |
| 279 |
flag_groups = [flag_group(flags = default_compile_flags)], |
| 280 |
), |
| 281 |
flag_set( |
| 282 |
actions = _all_compile_actions, |
| 283 |
flag_groups = [flag_group(flags = default_fastbuild_flags)], |
| 284 |
with_features = [with_feature_set(features = ["fastbuild"])], |
| 285 |
), |
| 286 |
flag_set( |
| 287 |
actions = _all_compile_actions, |
| 288 |
flag_groups = [flag_group(flags = default_dbg_flags)], |
| 289 |
with_features = [with_feature_set(features = ["dbg"])], |
| 290 |
), |
| 291 |
flag_set( |
| 292 |
actions = _all_compile_actions, |
| 293 |
flag_groups = [flag_group(flags = default_opt_flags)], |
| 294 |
with_features = [with_feature_set(features = ["opt"])], |
| 295 |
), |
| 296 |
flag_set( |
| 297 |
actions = [ACTION_NAMES.cpp_compile], |
| 298 |
# TODO(kjlubick) update this when updating the NDK |
| 299 |
flag_groups = [flag_group(flags = ["-std=c++2a"])], |
| 300 |
), |
| 301 |
], |
| 302 |
) |
| 303 |
|
| 304 |
default_link_flags_feature = feature( |
| 305 |
name = "default_link_flags", |
| 306 |
enabled = True, |
| 307 |
flag_sets = [ |
| 308 |
flag_set( |
| 309 |
actions = _all_link_actions, |
| 310 |
flag_groups = [flag_group(flags = default_link_flags)], |
| 311 |
), |
| 312 |
], |
| 313 |
) |
| 314 |
|
| 315 |
user_compile_flags_feature = feature( |
| 316 |
name = "user_compile_flags", |
| 317 |
enabled = True, |
| 318 |
flag_sets = [ |
| 319 |
flag_set( |
| 320 |
actions = _all_compile_actions, |
| 321 |
flag_groups = [ |
| 322 |
flag_group( |
| 323 |
flags = ["%{user_compile_flags}"], |
| 324 |
iterate_over = "user_compile_flags", |
| 325 |
expand_if_available = "user_compile_flags", |
| 326 |
), |
| 327 |
], |
| 328 |
), |
| 329 |
], |
| 330 |
) |
| 331 |
|
| 332 |
sysroot_feature = feature( |
| 333 |
name = "sysroot", |
| 334 |
enabled = True, |
| 335 |
flag_sets = [ |
| 336 |
flag_set( |
| 337 |
actions = _all_compile_actions + _all_link_actions, |
| 338 |
flag_groups = [ |
| 339 |
flag_group( |
| 340 |
flags = ["--sysroot=%{sysroot}"], |
| 341 |
expand_if_available = "sysroot", |
| 342 |
), |
| 343 |
], |
| 344 |
), |
| 345 |
], |
| 346 |
) |
| 347 |
|
| 348 |
unfiltered_compile_flags_feature = feature( |
| 349 |
name = "unfiltered_compile_flags", |
| 350 |
enabled = True, |
| 351 |
flag_sets = [ |
| 352 |
flag_set( |
| 353 |
actions = _all_compile_actions, |
| 354 |
flag_groups = [flag_group(flags = unfiltered_compile_flags)], |
| 355 |
), |
| 356 |
], |
| 357 |
) |
| 358 |
|
| 359 |
features = [ |
| 360 |
default_compile_flags_feature, |
| 361 |
default_link_flags_feature, |
| 362 |
supports_dynamic_linker_feature, |
| 363 |
supports_pic_feature, |
| 364 |
static_link_cpp_runtimes_feature, |
| 365 |
fastbuild_feature, |
| 366 |
dbg_feature, |
| 367 |
opt_feature, |
| 368 |
user_compile_flags_feature, |
| 369 |
sysroot_feature, |
| 370 |
unfiltered_compile_flags_feature, |
| 371 |
] |
| 372 |
|
| 373 |
cxx_builtin_include_directories = [ |
| 374 |
NDK_PATH + "/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/9.0.9/include", |
| 375 |
"%sysroot%/usr/include", |
| 376 |
NDK_PATH + "/sysroot/usr/include", |
| 377 |
] |
| 378 |
|
| 379 |
# https://bazel.build/rules/lib/cc_common#create_cc_toolchain_config_info |
| 380 |
return cc_common.create_cc_toolchain_config_info( |
| 381 |
ctx = ctx, |
| 382 |
toolchain_identifier = _get_toolchain_identifier(ctx.attr.cpu), |
| 383 |
host_system_name = "local", |
| 384 |
target_system_name = _get_target_system_name(ctx.attr.cpu), |
| 385 |
target_cpu = ctx.attr.cpu, |
| 386 |
target_libc = "local", |
| 387 |
compiler = "clang9.0.9", |
| 388 |
abi_version = ctx.attr.cpu, |
| 389 |
abi_libc_version = "local", |
| 390 |
features = features, |
| 391 |
tool_paths = _get_tool_paths(ctx.attr.cpu), |
| 392 |
cxx_builtin_include_directories = cxx_builtin_include_directories, |
| 393 |
builtin_sysroot = _get_builtin_sysroot(ctx.attr.cpu), |
| 394 |
) |
| 395 |
|
| 396 |
ndk_cc_toolchain_config = rule( |
| 397 |
implementation = _ndk_cc_toolchain_config_impl, |
| 398 |
attrs = { |
| 399 |
"cpu": attr.string( |
| 400 |
mandatory = True, |
| 401 |
values = [_ARMEABI_V7A, _ARM64_V8A], |
| 402 |
doc = "Target CPU.", |
| 403 |
), |
| 404 |
}, |
| 405 |
provides = [CcToolchainConfigInfo], |
| 406 |
) |