Skip to content

Commit

Permalink
Updates for Android NDK version 26 (#822)
Browse files Browse the repository at this point in the history
This PR adjusts the buildroot for Android NDK version 26. This NDK
version only supports clang, so it contains some cleanups of old
non-clang pathways.

The companion flutter/engine PR is
flutter/engine#48254.
  • Loading branch information
zanderso authored Feb 5, 2024
1 parent 52689fe commit 71c9a99
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 211 deletions.
96 changes: 10 additions & 86 deletions build/config/android/config.gni
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@
# This file contains common system config stuff for the Android build.

if (is_android) {
has_chrome_android_internal =
exec_script("//build/dir_exists.py",
[ rebase_path("//clank", root_build_dir) ],
"string") == "True"

if (has_chrome_android_internal) {
import("//clank/config.gni")
}

if (!defined(default_android_sdk_root)) {
default_android_sdk_root = "//third_party/android_tools/sdk"
default_android_sdk_version = "34"
Expand All @@ -25,22 +16,6 @@ if (is_android) {
android_sdk_version = default_android_sdk_version
android_sdk_build_tools_version = default_android_sdk_build_tools_version

android_default_keystore_path =
"//build/android/ant/chromium-debug.keystore"
android_default_keystore_name = "chromiumdebugkey"
android_default_keystore_password = "chromium"

# This is a unique identifier for a given build. It's used for
# identifying various build artifacts corresponding to a particular build of
# chrome (e.g. where to find archived symbols).
android_chrome_build_id = "\"\""

# Set to true to run findbugs on JAR targets.
run_findbugs = false

# Set to true to enable the Errorprone compiler
use_errorprone_java_compiler = false

# Unused. Required for GN files maintained in other buildroots.
enable_java_templates = false

Expand Down Expand Up @@ -92,69 +67,17 @@ if (is_android) {

zipalign_path = "$android_sdk_build_tools/zipalign"

# Subdirectories inside android_ndk_root that contain the sysroot for the
# associated platform.
if (current_cpu != "x64" && current_cpu != "arm64") {
android_api_level = 19
android_api_level = 21
}

# Toolchain root directory for each build. The actual binaries are inside
# a "bin" directory inside of these.
_android_toolchain_version = "4.9"

llvm_android_toolchain_root = "$android_ndk_root/toolchains/llvm/prebuilt/${android_host_os}-${android_host_arch}"

x86_android_toolchain_root = "$android_ndk_root/toolchains/x86-${_android_toolchain_version}/prebuilt/${android_host_os}-${android_host_arch}"
arm_android_toolchain_root = "$android_ndk_root/toolchains/arm-linux-androideabi-${_android_toolchain_version}/prebuilt/${android_host_os}-${android_host_arch}"
x86_64_android_toolchain_root = "$android_ndk_root/toolchains/x86_64-${_android_toolchain_version}/prebuilt/${android_host_os}-${android_host_arch}"
arm64_android_toolchain_root = "$android_ndk_root/toolchains/aarch64-linux-android-${_android_toolchain_version}/prebuilt/${android_host_os}-${android_host_arch}"

x86_android_lib = "$llvm_android_toolchain_root/sysroot/usr/lib/i686-linux-android/${android_api_level}"
arm_android_lib = "$llvm_android_toolchain_root/sysroot/usr/lib/arm-linux-androideabi/${android_api_level}"
x86_64_android_lib = "$llvm_android_toolchain_root/sysroot/usr/lib/x86_64-linux-android/${android_api_level}"
arm64_android_lib = "$llvm_android_toolchain_root/sysroot/usr/lib/aarch64-linux-android/${android_api_level}"

# Location of libgcc. This is only needed for the current GN toolchain, so we
# only need to define the current one, rather than one for every platform
# like the toolchain roots.
if (current_cpu == "x86") {
android_prebuilt_arch = "android-x86"
android_target_triple = "i686-linux-android"
android_toolchain_root = "$x86_android_toolchain_root"
android_lib = "$x86_android_lib"
} else if (current_cpu == "arm") {
android_prebuilt_arch = "android-arm"
android_target_triple = "arm-linux-androideabi"
android_toolchain_root = "$arm_android_toolchain_root"
android_lib = "$arm_android_lib"
} else if (current_cpu == "x64") {
android_prebuilt_arch = "android-x86_64"
android_target_triple = "x86_64-linux-android"
android_toolchain_root = "$x86_64_android_toolchain_root"
android_lib = "$x86_64_android_lib"
} else if (current_cpu == "arm64") {
android_prebuilt_arch = "android-arm64"
android_target_triple = "aarch64-linux-android"
android_toolchain_root = "$arm64_android_toolchain_root"
android_lib = "$arm64_android_lib"
} else {
assert(false, "Need android libgcc support for your target arch.")
}

android_tool_prefix = "$android_toolchain_root/bin/$android_target_triple-"
android_readelf = "${android_tool_prefix}readelf"
android_objcopy = "${android_tool_prefix}objcopy"
android_gdbserver =
"$android_ndk_root/prebuilt/$android_prebuilt_arch/gdbserver/gdbserver"
android_toolchain_root = "$android_ndk_root/toolchains/llvm/prebuilt/${android_host_os}-${android_host_arch}"

# Toolchain stuff ------------------------------------------------------------

android_libcpp_root = "$android_ndk_root/sources/cxx-stl/llvm-libc++"
if (component_mode == "shared_library") {
android_libcpp_library = "c++_shared"
} else {
android_libcpp_library = "c++_static"
}
_android_lib_prefix = "$android_toolchain_root/sysroot/usr/lib"

if (component_mode == "shared_library") {
# By appending .cr, we prevent name collisions with libraries already
Expand All @@ -168,20 +91,21 @@ if (is_android) {

if (current_cpu == "x86") {
android_app_abi = "x86"
_android_lib_dir = "i686-linux-android"
} else if (current_cpu == "arm") {
import("//build/config/arm.gni")
if (arm_version < 7) {
android_app_abi = "armeabi"
} else {
android_app_abi = "armeabi-v7a"
}
android_app_abi = "armeabi-v7a"
_android_lib_dir = "arm-linux-androideabi"
} else if (current_cpu == "x64") {
android_app_abi = "x86_64"
_android_lib_dir = "x86_64-linux-android"
} else if (current_cpu == "arm64") {
android_app_abi = "arm64-v8a"
_android_lib_dir = "aarch64-linux-android"
} else {
assert(false, "Unknown Android ABI: " + current_cpu)
}

android_lib = "$_android_lib_prefix/$_android_lib_dir/$android_api_level"

android_log_tag = "\"flutter\""
}
95 changes: 37 additions & 58 deletions build/config/compiler/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,6 @@ config("compiler") {
}
if (arm_use_thumb) {
cflags += [ "-mthumb" ]
if (is_android && !is_clang) { # Clang doesn't support this option.
cflags += [ "-mthumb-interwork" ]
}
}
if (!is_clang) {
# Clang doesn't support these flags.
Expand Down Expand Up @@ -392,31 +389,16 @@ config("compiler") {
"-fno-short-enums",
"-nostdinc++",
]
if (!is_clang) {
# Clang doesn't support these flags.
cflags += [ "-finline-limit=64" ]
}
if (current_cpu == "arm64") {
# TODO (https://github.com/flutter/flutter/issues/75348).
cflags += [ "-mno-outline-atomics" ]
}

defines += [ "ANDROID" ]

# The NDK has these things, but doesn't define the constants
# to say that it does. Define them here instead.
defines += [ "HAVE_SYS_UIO_H" ]

if (is_clang) {
# Let clang find libgcc.
ldflags += [ "--gcc-toolchain=" +
rebase_path(android_toolchain_root, root_build_dir) ]
}

ldflags += [
"-Wl,--no-undefined",
"-Wl,--exclude-libs,ALL",
"-fuse-ld=lld",

# Enable identical code folding to reduce size.
"-Wl,--icf=all",
Expand All @@ -426,20 +408,18 @@ config("compiler") {
"-Wl,-z,max-page-size=65536",
]

if (is_clang) {
if (current_cpu == "arm") {
cflags += [ "--target=arm-linux-androideabi" ]
ldflags += [ "--target=arm-linux-androideabi" ]
} else if (current_cpu == "arm64") {
cflags += [ "--target=aarch64-linux-android" ]
ldflags += [ "--target=aarch64-linux-android" ]
} else if (current_cpu == "x86") {
cflags += [ "--target=i686-linux-androideabi" ]
ldflags += [ "--target=i686-linux-androideabi" ]
} else if (current_cpu == "x64") {
cflags += [ "--target=x86_64-linux-androideabi" ]
ldflags += [ "--target=x86_64-linux-androideabi" ]
}
if (current_cpu == "arm") {
cflags += [ "--target=arm-linux-androideabi${android_api_level}" ]
ldflags += [ "--target=arm-linux-androideabi${android_api_level}" ]
} else if (current_cpu == "arm64") {
cflags += [ "--target=aarch64-linux-android${android_api_level}" ]
ldflags += [ "--target=aarch64-linux-android${android_api_level}" ]
} else if (current_cpu == "x86") {
cflags += [ "--target=i686-linux-androideabi${android_api_level}" ]
ldflags += [ "--target=i686-linux-androideabi${android_api_level}" ]
} else if (current_cpu == "x64") {
cflags += [ "--target=x86_64-linux-androideabi${android_api_level}" ]
ldflags += [ "--target=x86_64-linux-androideabi${android_api_level}" ]
}
}

Expand Down Expand Up @@ -561,40 +541,34 @@ config("runtime_library") {

# Android standard library setup.
if (is_android) {
if (is_clang) {
# Work around incompatibilities between bionic and clang headers.
defines += [
"__compiler_offsetof=__builtin_offsetof",
"nan=__builtin_nan",
]
}
# Work around incompatibilities between bionic and clang headers.
defines += [
"__compiler_offsetof=__builtin_offsetof",
"nan=__builtin_nan",
]

ldflags += [ "-nostdlib" ]

cflags += [ "-D__ANDROID_API__=$android_api_level" ]

# libunwind and libandroid_support also live in $android_libcpp_root.
lib_dirs += [ "$android_libcpp_root/libs/$android_app_abi" ]

if (android_api_level < 21) {
libs += [ "android_support" ]
}

if (current_cpu == "arm") {
libs += [ "unwind" ]
}
ldflags += [
"-Wl,--warn-shared-textrel",
]

libs += [
"gcc",
"c",
"dl",
"m",
]

# Clang with libc++ does not require an explicit atomic library reference.
if (!is_clang) {
libs += [ "atomic" ]
lib_dirs += [ "${android_toolchain_root}/lib/clang/17/lib/linux/" ]
current_android_cpu = current_cpu
if (current_cpu == "arm64") {
current_android_cpu = "aarch64"
} else if (current_cpu == "x64") {
current_android_cpu = "x86_64"
} else if (current_cpu == "x86") {
current_android_cpu = "i686"
}
libs += [ "clang_rt.builtins-${current_android_cpu}-android" ]
}

# Linux standard library setup.
Expand Down Expand Up @@ -682,10 +656,15 @@ if (is_win) {

# Needed for compiling Skia with clang-12
"-Wno-psabi",

# Needed for nlohmann/json.
"-Wno-deprecated-literal-operator",
]

if (!is_android) {
default_warning_flags += [
# Needed for nlohmann/json.
"-Wno-deprecated-literal-operator",
]
}

if (!is_wasm) {
default_warning_flags += [
# Unqualified std::move is pretty common.
Expand Down
2 changes: 1 addition & 1 deletion build/config/sysroot.gni
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ if (current_toolchain == default_toolchain && target_sysroot != "") {
sysroot = target_sysroot
} else if (is_android) {
import("//build/config/android/config.gni")
sysroot = rebase_path("$llvm_android_toolchain_root/sysroot", root_build_dir)
sysroot = rebase_path("$android_toolchain_root/sysroot", root_build_dir)
} else if (is_linux && !is_chromeos) {
if (use_default_linux_sysroot && !is_fuchsia) {
if (current_cpu == "x64") {
Expand Down
Loading

0 comments on commit 71c9a99

Please sign in to comment.