gn2bp: Pass ldflags to reduce cronet library size
Bug: 265913942
Test: ./update_results.sh
Change-Id: I650f354c7523b0649a190cb33dbd0dd0795c14d9
diff --git a/tools/gn2bp/Android.bp.swp b/tools/gn2bp/Android.bp.swp
index 9cc72a1..0a18a7f 100644
--- a/tools/gn2bp/Android.bp.swp
+++ b/tools/gn2bp/Android.bp.swp
@@ -284,6 +284,11 @@
"libgtest_prod_headers",
],
cpp_std: "c++17",
+ ldflags: [
+ "-Wl,--as-needed",
+ "-Wl,--gc-sections",
+ "-Wl,--icf=all",
+ ],
target: {
android_arm: {
srcs: [
@@ -1002,6 +1007,11 @@
"libgtest_prod_headers",
],
cpp_std: "c++17",
+ ldflags: [
+ "-Wl,--as-needed",
+ "-Wl,--gc-sections",
+ "-Wl,--icf=all",
+ ],
target: {
android_arm: {
srcs: [
@@ -1382,6 +1392,11 @@
"buildtools/third_party/libc++abi/trunk/include",
],
cpp_std: "c++17",
+ ldflags: [
+ "-Wl,--as-needed",
+ "-Wl,--gc-sections",
+ "-Wl,--icf=all",
+ ],
target: {
android_x86: {
cflags: [
@@ -1932,6 +1947,11 @@
"buildtools/third_party/libc++abi/trunk/include",
],
cpp_std: "c++17",
+ ldflags: [
+ "-Wl,--as-needed",
+ "-Wl,--gc-sections",
+ "-Wl,--icf=all",
+ ],
target: {
android_x86: {
cflags: [
@@ -1976,6 +1996,11 @@
"buildtools/third_party/libc++abi/trunk/include",
],
cpp_std: "c++17",
+ ldflags: [
+ "-Wl,--as-needed",
+ "-Wl,--gc-sections",
+ "-Wl,--icf=all",
+ ],
target: {
android_x86: {
cflags: [
@@ -2527,6 +2552,9 @@
],
cpp_std: "c++17",
ldflags: [
+ "-Wl,--as-needed",
+ "-Wl,--gc-sections",
+ "-Wl,--icf=all",
"-Wl,--script,external/cronet/base/android/library_loader/anchor_functions.lds",
],
stem: "libcronet.108.0.5359.128",
@@ -3858,6 +3886,11 @@
"third_party/boringssl/src/include/",
],
cpp_std: "c++17",
+ ldflags: [
+ "-Wl,--as-needed",
+ "-Wl,--gc-sections",
+ "-Wl,--icf=all",
+ ],
target: {
android_x86: {
cflags: [
@@ -3971,6 +4004,11 @@
"third_party/boringssl/src/include/",
],
cpp_std: "c++17",
+ ldflags: [
+ "-Wl,--as-needed",
+ "-Wl,--gc-sections",
+ "-Wl,--icf=all",
+ ],
target: {
android_x86: {
cflags: [
@@ -5516,6 +5554,11 @@
"third_party/protobuf/src/",
],
cpp_std: "c++17",
+ ldflags: [
+ "-Wl,--as-needed",
+ "-Wl,--gc-sections",
+ "-Wl,--icf=all",
+ ],
target: {
android_arm: {
srcs: [
@@ -5919,6 +5962,11 @@
"third_party/boringssl/src/include/",
],
cpp_std: "c++17",
+ ldflags: [
+ "-Wl,--as-needed",
+ "-Wl,--gc-sections",
+ "-Wl,--icf=all",
+ ],
target: {
android_x86: {
cflags: [
@@ -6423,6 +6471,11 @@
"third_party/protobuf/src/",
],
cpp_std: "c++17",
+ ldflags: [
+ "-Wl,--as-needed",
+ "-Wl,--gc-sections",
+ "-Wl,--icf=all",
+ ],
target: {
android_x86: {
cflags: [
@@ -6557,6 +6610,11 @@
"third_party/boringssl/src/include/",
],
cpp_std: "c++17",
+ ldflags: [
+ "-Wl,--as-needed",
+ "-Wl,--gc-sections",
+ "-Wl,--icf=all",
+ ],
target: {
android_x86: {
cflags: [
@@ -9201,6 +9259,11 @@
"third_party/boringssl/src/include/",
],
cpp_std: "c++17",
+ ldflags: [
+ "-Wl,--as-needed",
+ "-Wl,--gc-sections",
+ "-Wl,--icf=all",
+ ],
target: {
android_x86: {
cflags: [
@@ -9370,6 +9433,11 @@
"third_party/brotli/include/",
],
cpp_std: "c++17",
+ ldflags: [
+ "-Wl,--as-needed",
+ "-Wl,--gc-sections",
+ "-Wl,--icf=all",
+ ],
target: {
android_x86: {
cflags: [
@@ -9421,6 +9489,11 @@
"third_party/brotli/include/",
],
cpp_std: "c++17",
+ ldflags: [
+ "-Wl,--as-needed",
+ "-Wl,--gc-sections",
+ "-Wl,--icf=all",
+ ],
target: {
android_x86: {
cflags: [
@@ -9721,6 +9794,11 @@
"third_party/icu/source/i18n/",
],
cpp_std: "c++17",
+ ldflags: [
+ "-Wl,--as-needed",
+ "-Wl,--gc-sections",
+ "-Wl,--icf=all",
+ ],
rtti: true,
target: {
android_x86: {
@@ -9979,6 +10057,11 @@
"third_party/icu/source/i18n/",
],
cpp_std: "c++17",
+ ldflags: [
+ "-Wl,--as-needed",
+ "-Wl,--gc-sections",
+ "-Wl,--icf=all",
+ ],
rtti: true,
target: {
android_x86: {
@@ -10039,6 +10122,11 @@
"third_party/libevent/android/",
],
cpp_std: "c++17",
+ ldflags: [
+ "-Wl,--as-needed",
+ "-Wl,--gc-sections",
+ "-Wl,--icf=all",
+ ],
target: {
android_x86: {
cflags: [
@@ -10231,6 +10319,11 @@
"buildtools/third_party/libc++abi/trunk/include",
],
cpp_std: "c++17",
+ ldflags: [
+ "-Wl,--as-needed",
+ "-Wl,--gc-sections",
+ "-Wl,--icf=all",
+ ],
target: {
android_x86: {
cflags: [
@@ -10375,6 +10468,11 @@
"third_party/protobuf/src/",
],
cpp_std: "c++20",
+ ldflags: [
+ "-Wl,--as-needed",
+ "-Wl,--gc-sections",
+ "-Wl,--icf=all",
+ ],
}
// GN: //third_party/protobuf:protobuf_lite
@@ -10446,6 +10544,11 @@
"third_party/protobuf/src/",
],
cpp_std: "c++17",
+ ldflags: [
+ "-Wl,--as-needed",
+ "-Wl,--gc-sections",
+ "-Wl,--icf=all",
+ ],
target: {
android_x86: {
cflags: [
@@ -10512,6 +10615,11 @@
"third_party/protobuf/src/",
],
cpp_std: "c++20",
+ ldflags: [
+ "-Wl,--as-needed",
+ "-Wl,--gc-sections",
+ "-Wl,--icf=all",
+ ],
}
// GN: //third_party/protobuf:protoc_lib
@@ -10647,6 +10755,11 @@
"third_party/protobuf/src/",
],
cpp_std: "c++20",
+ ldflags: [
+ "-Wl,--as-needed",
+ "-Wl,--gc-sections",
+ "-Wl,--icf=all",
+ ],
}
// GN: //url:buildflags
@@ -10759,6 +10872,11 @@
"third_party/boringssl/src/include/",
],
cpp_std: "c++17",
+ ldflags: [
+ "-Wl,--as-needed",
+ "-Wl,--gc-sections",
+ "-Wl,--icf=all",
+ ],
target: {
android_x86: {
cflags: [
diff --git a/tools/gn2bp/gen_android_bp b/tools/gn2bp/gen_android_bp
index 461fb0a..79860fe 100755
--- a/tools/gn2bp/gen_android_bp
+++ b/tools/gn2bp/gen_android_bp
@@ -128,6 +128,14 @@
"-msse4.2",
]
+# Linker flags which are passed through to the blueprint.
+ldflag_allowlist = [
+ # flags to reduce binary size
+ "-Wl,--as-needed",
+ "-Wl,--gc-sections",
+ "-Wl,--icf=all",
+]
+
def get_linker_script_ldflag(script_path):
return f'-Wl,--script,{tree_path}/{script_path}'
@@ -1244,8 +1252,10 @@
cflags |= set("-D%s" % define.replace("\"", "\\\"") for define in defines)
return cflags
-def set_module_flags(module, cflags, defines):
+def set_module_flags(module, module_type, cflags, defines, ldflags):
module.cflags.update(_get_cflags(cflags, defines))
+ if module_type != 'cc_object':
+ module.ldflags.update({flag for flag in ldflags if flag in ldflag_allowlist})
# TODO: implement proper cflag parsing.
for flag in cflags:
if '-std=' in flag:
@@ -1337,11 +1347,12 @@
module.rtti = target.rtti
if target.type in gn_utils.LINKER_UNIT_TYPES:
- set_module_flags(module, target.cflags, target.defines)
+ set_module_flags(module, module.type, target.cflags, target.defines, target.ldflags)
set_module_include_dirs(module, target.cflags, target.include_dirs)
# TODO: set_module_xxx is confusing, apply similar function to module and target in better way.
for arch_name, arch in target.arch.items():
- set_module_flags(module.target[arch_name], arch.cflags, arch.defines)
+ set_module_flags(module.target[arch_name], module.type,
+ arch.cflags, arch.defines, arch.ldflags)
# -Xclang -target-feature -Xclang +mte are used to enable MTE (Memory Tagging Extensions).
# Flags which does not start with '-' could not be in the cflags so enabling MTE by
# -march and -mcpu Feature Modifiers. MTE is only available on arm64. This is needed for