[Lldb-commits] [lldb] lldb: get lldb API tests working with newer Android NDKs (PR #106443)

Andrew Rogers via lldb-commits lldb-commits at lists.llvm.org
Wed Aug 28 12:33:36 PDT 2024


https://github.com/andrurogerz created https://github.com/llvm/llvm-project/pull/106443

Running the LLDB API tests against a remote Android target with NDK version r22 or later fails when compiling the test inferiors.

## Problem Details
Android introduced a unified tools layout in NDK r19 (2019) and
 removed the old layout in r22 (2021). Releases r19, r20, and r21 support both the old and new layout side-by-side. More details are [here](https://github.com/android/ndk/issues/780). NDK r21 is the most recent NDK that is still compatible with the LLDB API tests.

## Solution

This change updates `Android.rules` to match the newer unified tools
 structure in NDK r19, which is described in more detail at . It significantly simplifies `Android.rules` in the process.

**With this change, the pre-2019 NDK structure is no longer supported. Only NDK r19 (from 2019) and later can be used when running the LLDB API tests. ** We can support both the old and new NDK toolchain layouts side-by-side if the maintainers object to only supporting NDKs released since 2019 (at the cost of readability and maintainability).

## Test Plan
Run a sub-set of the LLDB API tests against remote Android targets for the four primary architectures i386, x86_64, arm, and aarch64. Run with both r19 (the oldest supported) and r26 (more recent, unified layout only) NDK versions.

For each case, run the copy of `lldb-server` from the Android NDK on the device.

Example test command for aarch64:
```
./build/bin/lldb-dotest --out-of-tree-debugserver --arch aarch64 --platform-name remote-android --platform-url connect://localhost:5432 --platform-working-dir /data/local/tmp --compiler=$ANDROID_NDK_ROOT/toolchains/llvm/prebuilt/linux-x86_64/bin/clang --exclude /home/user/src/ds2/Support/Testing/Excluded/upstream/android-x86_64.excluded --env OBJCOPY=/home/user/src/llvm/llvm-project/build/bin/llvm-objcopy --env ARCHIVER=/home/user/src/llvm/llvm-project/build/bin/llvm-ar lldb/test/API/android/
```

NOTE: there are a lot of failures when running the full suite. These failures occur independent of this change.

>From 476a0109d5889a80d90ae484a553cce058a6e5ce Mon Sep 17 00:00:00 2001
From: Andrew Rogers <andrurogerz at gmail.com>
Date: Mon, 26 Aug 2024 17:42:35 -0700
Subject: [PATCH 1/2] [android] get lldb tests working against newer Android
 NDKs

Android introduced a unified tools layout in NDK r19 (2019) and
removed the old layout in r22 (2021). Running lldb tests with NDK r22
or newer fails when compiling the test inferiors.

This change updates `Android.rules` to match the newer unified tools
structure introduced in NDK r19, which is described in more detail at
https://github.com/android/ndk/issues/780.

NOTE: After this change, ONLY NDK r19c and later can be used when
running the lldb tests. The pre-2019 NDK structure is no longer
supported.
---
 .../Python/lldbsuite/test/make/Android.rules  | 78 ++++++-------------
 1 file changed, 23 insertions(+), 55 deletions(-)

diff --git a/lldb/packages/Python/lldbsuite/test/make/Android.rules b/lldb/packages/Python/lldbsuite/test/make/Android.rules
index cd7d8ae74d6bf3..518a90a5ba84d7 100644
--- a/lldb/packages/Python/lldbsuite/test/make/Android.rules
+++ b/lldb/packages/Python/lldbsuite/test/make/Android.rules
@@ -1,81 +1,49 @@
 NDK_ROOT := $(shell dirname $(CC))/../../../../..
 
-ifeq "$(findstring 64, $(ARCH))" "64"
-	# lowest 64-bit API level
-	API_LEVEL := 21
-else ifeq "$(ARCH)" "i386"
-	# clone(2) declaration is present only since this api level
-	API_LEVEL := 17
+ifeq "$(HOST_OS)" "Linux"
+	HOST_TAG := linux-x86_64
+else ifeq "$(HOST_OS)" "Darwin"
+	HOST_TAG := darwin-x86_64
 else
-	# lowest supported 32-bit API level
-	API_LEVEL := 16
+	HOST_TAG := windows-x86_64
 endif
 
 ifeq "$(ARCH)" "arm"
-	SYSROOT_ARCH := arm
-	STL_ARCH := armeabi-v7a
 	TRIPLE := armv7-none-linux-androideabi
 	ARCH_CFLAGS += -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -marm
 else ifeq "$(ARCH)" "aarch64"
-	SYSROOT_ARCH := arm64
-	STL_ARCH := arm64-v8a
 	TRIPLE := aarch64-none-linux-android
 else ifeq "$(ARCH)" "i386"
-	SYSROOT_ARCH := x86
-	STL_ARCH := x86
 	TRIPLE := i686-none-linux-android
 else
-	SYSROOT_ARCH := $(ARCH)
-	STL_ARCH := $(ARCH)
 	TRIPLE := $(ARCH)-none-linux-android
 endif
 
-ifeq "$(findstring 86,$(ARCH))" "86"
-	TOOLCHAIN_DIR := $(STL_ARCH)-4.9
-else ifeq "$(ARCH)" "arm"
-	TOOLCHAIN_DIR := arm-linux-androideabi-4.9
-else
-	TOOLCHAIN_DIR := $(subst -none,,$(TRIPLE))-4.9
-endif
+TOOLCHAIN_SYSROOT := $(NDK_ROOT)/toolchains/llvm/prebuilt/$(HOST_TAG)/sysroot
 
-ifeq "$(ARCH)" "arm"
-	TOOL_PREFIX := arm-linux-androideabi
-else
-	TOOL_PREFIX := $(subst -none,,$(TRIPLE))
-endif
+# lowest 64-bit API level
+API_LEVEL := 21
 
-ifeq "$(HOST_OS)" "Linux"
-	HOST_TAG := linux-x86_64
-else ifeq "$(HOST_OS)" "Darwin"
-	HOST_TAG := darwin-x86_64
+ifeq "$(ARCH)" "arm"
+	ARCH_DIR := arm-linux-androideabi
 else
-	HOST_TAG := windows-x86_64
+	ARCH_DIR := $(subst -none,,$(TRIPLE))
 endif
 
-GCC_TOOLCHAIN = $(NDK_ROOT)/toolchains/$(TOOLCHAIN_DIR)/prebuilt/$(HOST_TAG)
-
-OBJCOPY ?= $(GCC_TOOLCHAIN)/bin/$(TOOL_PREFIX)-objcopy
-ARCHIVER ?= $(GCC_TOOLCHAIN)/bin/$(TOOL_PREFIX)-ar
-
-ifeq "$(findstring clang,$(CC))" "clang"
-	ARCH_CFLAGS += -target $(TRIPLE) --gcc-toolchain=$(GCC_TOOLCHAIN)
-	ARCH_LDFLAGS += -target $(TRIPLE) --gcc-toolchain=$(GCC_TOOLCHAIN)
-endif
-
-ARCH_CFLAGS += --sysroot=$(NDK_ROOT)/sysroot \
-	-isystem $(NDK_ROOT)/sysroot/usr/include/$(TOOL_PREFIX) \
-	-D__ANDROID_API__=$(API_LEVEL) \
-	-isystem $(NDK_ROOT)/platforms/android-$(API_LEVEL)/arch-$(SYSROOT_ARCH)/usr/include
-
-ARCH_LDFLAGS += --sysroot=$(NDK_ROOT)/platforms/android-$(API_LEVEL)/arch-$(SYSROOT_ARCH) -lm
+ARCH_CFLAGS += \
+	--target=$(TRIPLE) \
+	--sysroot=$(TOOLCHAIN_SYSROOT) \
+	-D __ANDROID_API__=$(API_LEVEL) \
 
 ARCH_CXXFLAGS += \
-	-isystem $(NDK_ROOT)/sources/cxx-stl/llvm-libc++/include \
-	-isystem $(NDK_ROOT)/sources/android/support/include \
-	-isystem $(NDK_ROOT)/sources/cxx-stl/llvm-libc++abi/include
+	-isystem $(TOOLCHAIN_SYSROOT)/usr/include/c++/v1 \
 
 ARCH_LDFLAGS += \
-	-L$(NDK_ROOT)/sources/cxx-stl/llvm-libc++/libs/$(STL_ARCH) \
-	$(NDK_ROOT)/sources/cxx-stl/llvm-libc++/libs/$(STL_ARCH)/libc++_static.a \
+	--target=$(TRIPLE) \
+	--sysroot=$(TOOLCHAIN_SYSROOT) \
+	--prefix=$(TOOLCHAIN_SYSROOT)/usr/lib/$(ARCH_DIR)/$(API_LEVEL) \
+	--library-directory=$(TOOLCHAIN_SYSROOT)/usr/lib/$(ARCH_DIR)/$(API_LEVEL) \
+	$(TOOLCHAIN_SYSROOT)/usr/lib/$(ARCH_DIR)/libc++_static.a \
+	-lm \
 	-lc++abi \
-	-nostdlib++
+	-nostdlib++ \

>From 14064c39f8c53bcdcf3f7ace7455aa3a4b9bca28 Mon Sep 17 00:00:00 2001
From: Andrew Rogers <andrurogerz at gmail.com>
Date: Wed, 28 Aug 2024 12:08:10 -0700
Subject: [PATCH 2/2] [android] explicitly fail inferior compilation on old
 NDKs

Provide an error message when compiling LLDB API test inferiors against
an older Android NDK without the unified toolchain layout. This message
will help guide developers to install and use an NDK r19 or newer.
---
 lldb/packages/Python/lldbsuite/test/make/Android.rules | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/lldb/packages/Python/lldbsuite/test/make/Android.rules b/lldb/packages/Python/lldbsuite/test/make/Android.rules
index 518a90a5ba84d7..3bece0d0ae8045 100644
--- a/lldb/packages/Python/lldbsuite/test/make/Android.rules
+++ b/lldb/packages/Python/lldbsuite/test/make/Android.rules
@@ -8,6 +8,14 @@ else
 	HOST_TAG := windows-x86_64
 endif
 
+TOOLCHAIN_SYSROOT := $(NDK_ROOT)/toolchains/llvm/prebuilt/$(HOST_TAG)/sysroot
+
+ifeq "$(wildcard $(TOOLCHAIN_SYSROOT)/.)" ""
+# Compiling test inferiors for Android requires an NDK with the unified
+# toolchain introduced in version r19.
+$(error "No unified toolchain sysroot found in $(NDK_ROOT). NDK must be r19 or later.")
+endif
+
 ifeq "$(ARCH)" "arm"
 	TRIPLE := armv7-none-linux-androideabi
 	ARCH_CFLAGS += -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -marm
@@ -19,8 +27,6 @@ else
 	TRIPLE := $(ARCH)-none-linux-android
 endif
 
-TOOLCHAIN_SYSROOT := $(NDK_ROOT)/toolchains/llvm/prebuilt/$(HOST_TAG)/sysroot
-
 # lowest 64-bit API level
 API_LEVEL := 21
 



More information about the lldb-commits mailing list