bazelbuild / bazel

a fast, scalable, multi-language and extensible build system
https://bazel.build
Apache License 2.0
22.99k stars 4.03k forks source link

[Android NDK] libandroid_support.a incorrectly being linked on API 21+ when building fully static binary with c++-static #12840

Open chenxiaolong opened 3 years ago

chenxiaolong commented 3 years ago

Description of the problem / feature request:

When building a fully statically linked binary with the c++-static STL on Android, libandroid_support.a gets linked unconditionally, which leads to multiple definition errors like:

external/androidndk/ndk/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: error: external/androidndk/ndk/platforms/android-29/arch-arm/usr/lib/../lib/libc.a(locale.o): multiple definition of 'duplocale'

As I understand it, libandroid_support.a is only supposed to be used on API versions older than 21 and only on armeabi-v7a and x86.

I suspect this behavior is caused by the glob defined here: https://github.com/bazelbuild/bazel/blob/8346ea4cfdd9fbd170d51a528fee26f912dad2d5/src/main/java/com/google/devtools/build/lib/bazel/rules/android/ndkcrosstools/NdkPaths.java#L270-L271

libandroid_support.a is in the same directory as libc++_static.a.

Bugs: what's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

I've created a simple repro here: https://gist.github.com/chenxiaolong/dcda157428e60489efa7a2f9346e581e

git clone https://gist.github.com/dcda157428e60489efa7a2f9346e581e.git
cd dcda157428e60489efa7a2f9346e581e
# Both armeabi-v7a and x86 fail
bazel build --config=android_armeabi-v7a :main
bazel build --config=android_x86 :main

What operating system are you running Bazel on?

Ubuntu 20.04.1 on WSL2

What's the output of bazel info release?

3.7.2

If bazel info release returns "development version" or "(@non-git)", tell us how you built Bazel.

n/a

What's the output of git remote get-url origin ; git rev-parse master ; git rev-parse HEAD ?

n/a

Have you found anything relevant by searching the web?

This was the same issue, but for CMake: https://github.com/android/ndk/issues/17

This is the comment from an NDK maintainer mentioning that libandroid_support is mean for API <21: https://github.com/android/ndk/issues/17#issuecomment-401997677

Any other information, logs, or outputs that you want to share?

n/a

ahumesky commented 1 year ago

Bazel's NDK integration has been rewritten into a separate Starlark implementation, you might try using the new rules: https://github.com/bazelbuild/rules_android_ndk