diff --git a/build/cmake/android.toolchain.cmake b/build/cmake/android.toolchain.cmake index d9e5fb7e7..cf5312f0e 100644 --- a/build/cmake/android.toolchain.cmake +++ b/build/cmake/android.toolchain.cmake @@ -452,9 +452,22 @@ list(APPEND ANDROID_COMPILER_FLAGS -funwind-tables -fstack-protector-strong -no-canonical-prefixes) -list(APPEND ANDROID_LINKER_FLAGS - -Wl,--build-id=sha1 - -Wl,--fatal-warnings) + +# https://github.com/android/ndk/issues/885 +# If we're using LLD we need to use a slower build-id algorithm to work around +# the old version of LLDB in Android Studio, which doesn't understand LLD's +# default hash ("fast"). +# +# Note that because we cannot see the user's flags, we can't detect this very +# accurately. Users that explicitly use -fuse-ld=lld instead of ANDROID_LD will +# not be able to debug. +if(ANDROID_LD STREQUAL lld) + list(APPEND ANDROID_LINKER_FLAGS -Wl,--build-id=sha1) +else() + list(APPEND ANDROID_LINKER_FLAGS -Wl,--build-id) +endif() + +list(APPEND ANDROID_LINKER_FLAGS -Wl,--fatal-warnings) list(APPEND ANDROID_LINKER_FLAGS_EXE -Wl,--gc-sections) # Debug and release flags. diff --git a/build/core/build-binary.mk b/build/core/build-binary.mk index 2e1c0648c..21c79cdc5 100644 --- a/build/core/build-binary.mk +++ b/build/core/build-binary.mk @@ -164,8 +164,6 @@ ifeq ($(LOCAL_CPP_EXTENSION),) endif LOCAL_RS_EXTENSION := $(default-rs-extensions) -LOCAL_LDFLAGS += -Wl,--build-id=sha1 - ifneq ($(NDK_APP_STL),system) LOCAL_CFLAGS += -nostdinc++ LOCAL_LDFLAGS += -nostdlib++ @@ -498,8 +496,33 @@ CLEAN_OBJS_DIRS += $(LOCAL_OBJS_DIR) # linker_ldflags := +using_lld := false ifeq ($(APP_LD),lld) linker_ldflags := -fuse-ld=lld + using_lld := true +endif + +combined_ldflags := $(TARGET_LDFLAGS) $(NDK_APP_LDFLAGS) $(LOCAL_LDFLAGS) +ndk_fuse_ld_flags := $(filter -fuse-ld=%,$(combined_ldflags)) +ndk_used_linker := $(lastword $(ndk_fuse_ld_flags)) +ifeq ($(ndk_used_linker),-fuse-ld=lld) + using_lld := true +else + # In case the user has set APP_LD=lld but also disabled it for a specific + # module. + ifneq ($(ndk_used_linker),) + using_lld := false + endif +endif + +# https://github.com/android/ndk/issues/885 +# If we're using LLD we need to use a slower build-id algorithm to work around +# the old version of LLDB in Android Studio, which doesn't understand LLD's +# default hash ("fast"). +ifeq ($(using_lld),true) + linker_ldflags += -Wl,--build-id=tree +else + linker_ldflags += -Wl,--build-id endif my_ldflags := $(TARGET_LDFLAGS) $(linker_ldflags) $(NDK_APP_LDFLAGS) $(LOCAL_LDFLAGS) diff --git a/docs/changelogs/Changelog-r21.md b/docs/changelogs/Changelog-r21.md index e3ce22396..1b349cce3 100644 --- a/docs/changelogs/Changelog-r21.md +++ b/docs/changelogs/Changelog-r21.md @@ -57,7 +57,10 @@ For Android Studio issues, follow the docs on the [Android Studio site]. * Updated glibc to 2.17. * Updated gdb to 8.3. * [Issue 885]: For LLD+LLDB compatibility, the NDK build systems now pass - `-Wl,--build-id=sha1` instead of `-Wl,--build-id`. Third-party build systems + `-Wl,--build-id=tree` instead of `-Wl,--build-id` when using LLD. Note that + the CMake toolchain does not have access to flags set in CMakeLists.txt, so + using an explicit `-fuse-ld=lld` instead of `ANDROID_LD=lld` will produce + output that cannot be debugged with Android Studio. Third-party build systems need to apply the workaround manually. For more details, see the [Build System Maintainers Guide][maintainer_linkers]. * Fixed ndk-build to use Clang's default C++ standard version (currently C++14)