Merge "Change lto_test to use GlobalThinLTO configuration" into main am: 0b6c4b7c08 am: b0c93d0f17

Original change: https://android-review.googlesource.com/c/platform/build/soong/+/2774344

Change-Id: I283a3d7843e4b89561abbb6d0f4cbfc7003c9acf
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Yi Kong 2023-10-09 12:07:48 +00:00 committed by Automerger Merge Worker
commit 556d4a56d0
2 changed files with 43 additions and 56 deletions

View file

@ -35,11 +35,11 @@ import (
// optimized at link time and may not be compatible with features that require // optimized at link time and may not be compatible with features that require
// LTO, such as CFI. // LTO, such as CFI.
// //
// This file adds support to soong to automatically propogate LTO options to a // This file adds support to soong to automatically propagate LTO options to a
// new variant of all static dependencies for each module with LTO enabled. // new variant of all static dependencies for each module with LTO enabled.
type LTOProperties struct { type LTOProperties struct {
// Lto must violate capitialization style for acronyms so that it can be // Lto must violate capitalization style for acronyms so that it can be
// referred to in blueprint files as "lto" // referred to in blueprint files as "lto"
Lto struct { Lto struct {
Never *bool `android:"arch_variant"` Never *bool `android:"arch_variant"`
@ -67,10 +67,12 @@ func (lto *lto) props() []interface{} {
} }
func (lto *lto) begin(ctx BaseModuleContext) { func (lto *lto) begin(ctx BaseModuleContext) {
// First, determine the module indepedent default LTO mode. // First, determine the module independent default LTO mode.
ltoDefault := GlobalThinLTO(ctx) ltoDefault := GlobalThinLTO(ctx)
if ctx.Config().IsEnvTrue("DISABLE_LTO") { if ctx.Config().IsEnvTrue("DISABLE_LTO") {
ltoDefault = false ltoDefault = false
} else if lto.Never() {
ltoDefault = false
} else if ctx.Host() { } else if ctx.Host() {
// Performance and binary size are less important for host binaries. // Performance and binary size are less important for host binaries.
ltoDefault = false ltoDefault = false

View file

@ -23,11 +23,19 @@ import (
"github.com/google/blueprint" "github.com/google/blueprint"
) )
var NoGlobalThinLTOPreparer = android.GroupFixturePreparers( var LTOPreparer = android.GroupFixturePreparers(
prepareForCcTest, prepareForCcTest,
android.FixtureModifyEnv(func(env map[string]string) { )
env["GLOBAL_THINLTO"] = "false"
})) func hasDep(result *android.TestResult, m android.Module, wantDep android.Module) bool {
var found bool
result.VisitDirectDeps(m, func(dep blueprint.Module) {
if dep == wantDep {
found = true
}
})
return found
}
func TestThinLtoDeps(t *testing.T) { func TestThinLtoDeps(t *testing.T) {
t.Parallel() t.Parallel()
@ -37,9 +45,6 @@ func TestThinLtoDeps(t *testing.T) {
srcs: ["src.c"], srcs: ["src.c"],
static_libs: ["foo", "lib_never_lto"], static_libs: ["foo", "lib_never_lto"],
shared_libs: ["bar"], shared_libs: ["bar"],
lto: {
thin: true,
}
} }
cc_library_static { cc_library_static {
name: "foo", name: "foo",
@ -63,50 +68,40 @@ func TestThinLtoDeps(t *testing.T) {
} }
` `
result := NoGlobalThinLTOPreparer.RunTestWithBp(t, bp) result := LTOPreparer.RunTestWithBp(t, bp)
libLto := result.ModuleForTests("lto_enabled", "android_arm64_armv8-a_shared").Module() libLto := result.ModuleForTests("lto_enabled", "android_arm64_armv8-a_shared").Module()
hasDep := func(m android.Module, wantDep android.Module) bool { libFoo := result.ModuleForTests("foo", "android_arm64_armv8-a_static").Module()
var found bool if !hasDep(result, libLto, libFoo) {
result.VisitDirectDeps(m, func(dep blueprint.Module) { t.Errorf("'lto_enabled' missing dependency on the default variant of 'foo'")
if dep == wantDep {
found = true
}
})
return found
} }
libFoo := result.ModuleForTests("foo", "android_arm64_armv8-a_static_lto-thin").Module() libBaz := result.ModuleForTests("baz", "android_arm64_armv8-a_static").Module()
if !hasDep(libLto, libFoo) { if !hasDep(result, libFoo, libBaz) {
t.Errorf("'lto_enabled' missing dependency on thin lto variant of 'foo'") t.Errorf("'foo' missing dependency on the default variant of transitive dep 'baz'")
} }
libBaz := result.ModuleForTests("baz", "android_arm64_armv8-a_static_lto-thin").Module() libNeverLto := result.ModuleForTests("lib_never_lto", "android_arm64_armv8-a_static").Module()
if !hasDep(libFoo, libBaz) { if !hasDep(result, libLto, libNeverLto) {
t.Errorf("'foo' missing dependency on thin lto variant of transitive dep 'baz'") t.Errorf("'lto_enabled' missing dependency on the default variant of 'lib_never_lto'")
}
libNeverLto := result.ModuleForTests("lib_never_lto", "android_arm64_armv8-a_static_lto-thin").Module()
if !hasDep(libLto, libNeverLto) {
t.Errorf("'lto_enabled' missing dependency on NO-thin lto variant of 'lib_never_lto'")
} }
libBar := result.ModuleForTests("bar", "android_arm64_armv8-a_shared").Module() libBar := result.ModuleForTests("bar", "android_arm64_armv8-a_shared").Module()
if !hasDep(libLto, libBar) { if !hasDep(result, libLto, libBar) {
t.Errorf("'lto_enabled' missing dependency on non-thin lto variant of 'bar'") t.Errorf("'lto_enabled' missing dependency on the default variant of 'bar'")
} }
barVariants := result.ModuleVariantsForTests("bar") barVariants := result.ModuleVariantsForTests("bar")
for _, v := range barVariants { for _, v := range barVariants {
if strings.Contains(v, "lto-thin") { if strings.Contains(v, "lto-none") {
t.Errorf("Expected variants for 'bar' to not contain 'lto-thin', but found %q", v) t.Errorf("Expected variants for 'bar' to not contain 'lto-none', but found %q", v)
} }
} }
quxVariants := result.ModuleVariantsForTests("qux") quxVariants := result.ModuleVariantsForTests("qux")
for _, v := range quxVariants { for _, v := range quxVariants {
if strings.Contains(v, "lto-thin") { if strings.Contains(v, "lto-none") {
t.Errorf("Expected variants for 'qux' to not contain 'lto-thin', but found %q", v) t.Errorf("Expected variants for 'qux' to not contain 'lto-none', but found %q", v)
} }
} }
} }
@ -141,28 +136,18 @@ func TestThinLtoOnlyOnStaticDep(t *testing.T) {
} }
` `
result := NoGlobalThinLTOPreparer.RunTestWithBp(t, bp) result := LTOPreparer.RunTestWithBp(t, bp)
libRoot := result.ModuleForTests("root", "android_arm64_armv8-a_shared").Module() libRoot := result.ModuleForTests("root", "android_arm64_armv8-a_shared").Module()
libRootLtoNever := result.ModuleForTests("root_no_lto", "android_arm64_armv8-a_shared").Module() libRootLtoNever := result.ModuleForTests("root_no_lto", "android_arm64_armv8-a_shared").Module()
hasDep := func(m android.Module, wantDep android.Module) bool {
var found bool
result.VisitDirectDeps(m, func(dep blueprint.Module) {
if dep == wantDep {
found = true
}
})
return found
}
libFoo := result.ModuleForTests("foo", "android_arm64_armv8-a_static") libFoo := result.ModuleForTests("foo", "android_arm64_armv8-a_static")
if !hasDep(libRoot, libFoo.Module()) { if !hasDep(result, libRoot, libFoo.Module()) {
t.Errorf("'root' missing dependency on thin lto variant of 'foo'") t.Errorf("'root' missing dependency on the default variant of 'foo'")
} }
if !hasDep(libRootLtoNever, libFoo.Module()) { if !hasDep(result, libRootLtoNever, libFoo.Module()) {
t.Errorf("'root_no_lto' missing dependency on thin lto variant of 'foo'") t.Errorf("'root_no_lto' missing dependency on the default variant of 'foo'")
} }
libFooCFlags := libFoo.Rule("cc").Args["cFlags"] libFooCFlags := libFoo.Rule("cc").Args["cFlags"]
@ -170,9 +155,9 @@ func TestThinLtoOnlyOnStaticDep(t *testing.T) {
t.Errorf("'foo' expected to have flags %q, but got %q", w, libFooCFlags) t.Errorf("'foo' expected to have flags %q, but got %q", w, libFooCFlags)
} }
libBaz := result.ModuleForTests("baz", "android_arm64_armv8-a_static_lto-thin") libBaz := result.ModuleForTests("baz", "android_arm64_armv8-a_static")
if !hasDep(libFoo.Module(), libBaz.Module()) { if !hasDep(result, libFoo.Module(), libBaz.Module()) {
t.Errorf("'foo' missing dependency on thin lto variant of transitive dep 'baz'") t.Errorf("'foo' missing dependency on the default variant of transitive dep 'baz'")
} }
libBazCFlags := libFoo.Rule("cc").Args["cFlags"] libBazCFlags := libFoo.Rule("cc").Args["cFlags"]
@ -199,7 +184,7 @@ func TestLtoDisabledButEnabledForArch(t *testing.T) {
}, },
}, },
}` }`
result := NoGlobalThinLTOPreparer.RunTestWithBp(t, bp) result := LTOPreparer.RunTestWithBp(t, bp)
libFooWithLto := result.ModuleForTests("libfoo", "android_arm_armv7-a-neon_shared").Rule("ld") libFooWithLto := result.ModuleForTests("libfoo", "android_arm_armv7-a-neon_shared").Rule("ld")
libFooWithoutLto := result.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("ld") libFooWithoutLto := result.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("ld")
@ -227,7 +212,7 @@ func TestLtoDoesNotPropagateToRuntimeLibs(t *testing.T) {
}, },
}` }`
result := NoGlobalThinLTOPreparer.RunTestWithBp(t, bp) result := LTOPreparer.RunTestWithBp(t, bp)
libFoo := result.ModuleForTests("libfoo", "android_arm_armv7-a-neon_shared").Rule("ld") libFoo := result.ModuleForTests("libfoo", "android_arm_armv7-a-neon_shared").Rule("ld")
libBar := result.ModuleForTests("runtime_libbar", "android_arm_armv7-a-neon_shared").Rule("ld") libBar := result.ModuleForTests("runtime_libbar", "android_arm_armv7-a-neon_shared").Rule("ld")