gn2bp: Pass ldflags to reduce cronet library size

Bug: 265913942
Test: ./update_results.sh
Change-Id: I650f354c7523b0649a190cb33dbd0dd0795c14d9
This commit is contained in:
Motomu Utsumi 2023-02-09 16:00:33 +09:00
commit ea38e4eb22
2 changed files with 132 additions and 3 deletions

View file

@ -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: [

View file

@ -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