Merge cherrypicks of [1466961] into ndk-release-r22

Change-Id: Ib5b8bbe0ce50b9247dc1bb70a5007699d1de143d
diff --git a/build/cmake/android.toolchain.cmake b/build/cmake/android.toolchain.cmake
index 3c0fb9c..d1d344e 100644
--- a/build/cmake/android.toolchain.cmake
+++ b/build/cmake/android.toolchain.cmake
@@ -401,15 +401,6 @@
 
 list(APPEND CMAKE_PREFIX_PATH "${ANDROID_TOOLCHAIN_ROOT}")
 
-set(ANDROID_SYSROOT_PREFIX "${ANDROID_TOOLCHAIN_ROOT}/sysroot/usr")
-
-# Ideally we'd use CMAKE_PREFIX_PATH for this, but that causes the
-# non-arch-specific CMAKE_SYSTEM_LIBRARY_PATH to be searched first for
-# find_library, which will cause libdl.a to be found before libdl.so.
-list(APPEND CMAKE_SYSTEM_INCLUDE_PATH
-  "${ANDROID_SYSROOT_PREFIX}/include/${CMAKE_LIBRARY_ARCHITECTURE}")
-list(APPEND CMAKE_SYSTEM_INCLUDE_PATH "${ANDROID_SYSROOT_PREFIX}/include")
-
 # find_library searches a handful of paths as described by
 # https://cmake.org/cmake/help/v3.6/command/find_library.html. CMake doesn't
 # understand the Android sysroot layout, so we need to give the direct path to
@@ -417,14 +408,18 @@
 # (https://cmake.org/cmake/help/v3.6/variable/CMAKE_SYSTEM_LIBRARY_PATH.html)
 # instead.
 
+# NB: This variable causes CMake to automatically pass --sysroot to the
+# toolchain. Studio currently relies on this to recognize Android builds. If
+# this variable is removed, ensure that flag is still passed.
+# TODO: Teach Studio to recognize Android builds based on --target.
+set(CMAKE_SYSROOT "${ANDROID_TOOLCHAIN_ROOT}/sysroot")
+
 # Allows CMake to find headers in the architecture-specific include directories.
 set(CMAKE_LIBRARY_ARCHITECTURE "${ANDROID_TOOLCHAIN_NAME}")
 
 # Instructs CMake to search the correct API level for libraries.
 list(APPEND CMAKE_SYSTEM_LIBRARY_PATH
-  "${ANDROID_SYSROOT_PREFIX}/lib/${CMAKE_LIBRARY_ARCHITECTURE}/${ANDROID_PLATFORM_LEVEL}")
-list(APPEND CMAKE_SYSTEM_LIBRARY_PATH
-  "${ANDROID_SYSROOT_PREFIX}/lib/${CMAKE_LIBRARY_ARCHITECTURE}")
+  "/usr/lib/${ANDROID_TOOLCHAIN_NAME}/${ANDROID_PLATFORM_LEVEL}")
 
 set(ANDROID_HOST_PREBUILTS "${ANDROID_NDK}/prebuilt/${ANDROID_HOST_TAG}")
 
diff --git a/build/core/default-build-commands.mk b/build/core/default-build-commands.mk
index 56962a1..60ab955 100644
--- a/build/core/default-build-commands.mk
+++ b/build/core/default-build-commands.mk
@@ -118,6 +118,11 @@
     -funwind-tables \
     -no-canonical-prefixes \
 
+# This is unnecessary given the new toolchain layout, but Studio will not
+# recognize this as an Android build if there is no --sysroot flag.
+# TODO: Teach Studio to recognize Android builds based on --target.
+GLOBAL_CFLAGS += --sysroot $(call host-path,$(NDK_UNIFIED_SYSROOT_PATH))
+
 # Always enable debug info. We strip binaries when needed.
 GLOBAL_CFLAGS += -g
 
diff --git a/docs/changelogs/Changelog-r22.md b/docs/changelogs/Changelog-r22.md
index eeed7d3..ebaa00a 100644
--- a/docs/changelogs/Changelog-r22.md
+++ b/docs/changelogs/Changelog-r22.md
@@ -47,9 +47,6 @@
    stripping, so most users will need to upgrade to Android Gradle Plugin
    version 4.0 or newer to get the fix.
 
-* [Issue 929]: `find_library` now prefers shared libraries from the sysroot over
-  static libraries.
-
 * [Issue 1130]: Fixed undefined references to new that could occur when building
   for APIs prior to 21 and the static libc++. Note that LLD appears to have been
   unaffected, but the problem is still present for ndk-build when using the
@@ -156,8 +153,6 @@
   `No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android`,
   update your project file to [use plugin version 3.1 or newer]. You will also
   need to upgrade to Android Studio 3.1 or newer.
-* This version of the NDK is incompatible with Android Studio 3.2 and older.
-  Builds will still work, but IDE features will be unavailable.
 * [Issue 843]: Using LLD with binutils `strip` or `objcopy` breaks RelRO. Use
    `llvm-strip` and `llvm-objcopy` instead. This issue has been resolved in
    Android Gradle Plugin version 4.0 (for non-Gradle users, the fix is also in
diff --git a/tests/build/cmake_libdl_search_order/CMakeLists.txt b/tests/build/cmake_libdl_search_order/CMakeLists.txt
deleted file mode 100644
index 5573216..0000000
--- a/tests/build/cmake_libdl_search_order/CMakeLists.txt
+++ /dev/null
@@ -1,10 +0,0 @@
-# https://github.com/android/ndk/issues/929
-# Tests that the first libdl found by find_library is the shared library rather
-# than the static library.
-cmake_minimum_required(VERSION 3.6.0)
-project(cmake_libdl_search_order)
-
-find_library(LIBDL dl)
-if(LIBDL MATCHES ".a$")
-  message(FATAL_ERROR "found libdl.a")
-endif()