From ea38e4eb2270b7df56f0972e129f18c19cdb8b13 Mon Sep 17 00:00:00 2001 From: Motomu Utsumi Date: Thu, 9 Feb 2023 16:00:33 +0900 Subject: [PATCH] gn2bp: Pass ldflags to reduce cronet library size Bug: 265913942 Test: ./update_results.sh Change-Id: I650f354c7523b0649a190cb33dbd0dd0795c14d9 --- tools/gn2bp/Android.bp.swp | 118 +++++++++++++++++++++++++++++++++++++ tools/gn2bp/gen_android_bp | 17 +++++- 2 files changed, 132 insertions(+), 3 deletions(-) diff --git a/tools/gn2bp/Android.bp.swp b/tools/gn2bp/Android.bp.swp index 9cc72a14b3..0a18a7f204 100644 --- a/tools/gn2bp/Android.bp.swp +++ b/tools/gn2bp/Android.bp.swp @@ -284,6 +284,11 @@ cc_library_static { "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 @@ cc_library_static { "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 @@ cc_library_static { "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 @@ cc_library_static { "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 @@ cc_library_static { "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 @@ cc_library_shared { ], 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 @@ cc_library_static { "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 @@ cc_library_static { "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 @@ cc_library_static { "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 @@ cc_library_static { "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 @@ cc_library_static { "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 @@ cc_library_static { "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 @@ cc_library_static { "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 @@ cc_library_static { "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 @@ cc_library_static { "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 @@ cc_library_static { "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 @@ cc_library_static { "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 @@ cc_library_static { "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 @@ cc_library_static { "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 @@ cc_library_static { "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 @@ cc_library_static { "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 @@ cc_binary { "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 @@ cc_library_static { "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 @@ cc_library_static { "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 461fb0a56c..79860febce 100755 --- a/tools/gn2bp/gen_android_bp +++ b/tools/gn2bp/gen_android_bp @@ -128,6 +128,14 @@ cflag_allowlist = [ "-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 @@ def _get_cflags(cflags, defines): 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 @@ def create_modules_from_target(blueprint, gn, gn_target_name): 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