Merge "Change lto_test to use GlobalThinLTO configuration" into main

This commit is contained in:
Yi Kong 2023-10-09 10:41:28 +00:00 committed by Gerrit Code Review
commit 0b6c4b7c08
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
// 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.
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"
Lto struct {
Never *bool `android:"arch_variant"`
@ -67,10 +67,12 @@ func (lto *lto) props() []interface{} {
}
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)
if ctx.Config().IsEnvTrue("DISABLE_LTO") {
ltoDefault = false
} else if lto.Never() {
ltoDefault = false
} else if ctx.Host() {
// Performance and binary size are less important for host binaries.
ltoDefault = false

View file

@ -23,11 +23,19 @@ import (
"github.com/google/blueprint"
)
var NoGlobalThinLTOPreparer = android.GroupFixturePreparers(
var LTOPreparer = android.GroupFixturePreparers(
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) {
t.Parallel()
@ -37,9 +45,6 @@ func TestThinLtoDeps(t *testing.T) {
srcs: ["src.c"],
static_libs: ["foo", "lib_never_lto"],
shared_libs: ["bar"],
lto: {
thin: true,
}
}
cc_library_static {
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()
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").Module()
if !hasDep(result, libLto, libFoo) {
t.Errorf("'lto_enabled' missing dependency on the default variant of 'foo'")
}
libFoo := result.ModuleForTests("foo", "android_arm64_armv8-a_static_lto-thin").Module()
if !hasDep(libLto, libFoo) {
t.Errorf("'lto_enabled' missing dependency on thin lto variant of 'foo'")
libBaz := result.ModuleForTests("baz", "android_arm64_armv8-a_static").Module()
if !hasDep(result, libFoo, libBaz) {
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()
if !hasDep(libFoo, libBaz) {
t.Errorf("'foo' missing dependency on thin lto variant of transitive dep 'baz'")
}
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'")
libNeverLto := result.ModuleForTests("lib_never_lto", "android_arm64_armv8-a_static").Module()
if !hasDep(result, libLto, libNeverLto) {
t.Errorf("'lto_enabled' missing dependency on the default variant of 'lib_never_lto'")
}
libBar := result.ModuleForTests("bar", "android_arm64_armv8-a_shared").Module()
if !hasDep(libLto, libBar) {
t.Errorf("'lto_enabled' missing dependency on non-thin lto variant of 'bar'")
if !hasDep(result, libLto, libBar) {
t.Errorf("'lto_enabled' missing dependency on the default variant of 'bar'")
}
barVariants := result.ModuleVariantsForTests("bar")
for _, v := range barVariants {
if strings.Contains(v, "lto-thin") {
t.Errorf("Expected variants for 'bar' to not contain 'lto-thin', but found %q", v)
if strings.Contains(v, "lto-none") {
t.Errorf("Expected variants for 'bar' to not contain 'lto-none', but found %q", v)
}
}
quxVariants := result.ModuleVariantsForTests("qux")
for _, v := range quxVariants {
if strings.Contains(v, "lto-thin") {
t.Errorf("Expected variants for 'qux' to not contain 'lto-thin', but found %q", v)
if strings.Contains(v, "lto-none") {
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()
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")
if !hasDep(libRoot, libFoo.Module()) {
t.Errorf("'root' missing dependency on thin lto variant of 'foo'")
if !hasDep(result, libRoot, libFoo.Module()) {
t.Errorf("'root' missing dependency on the default variant of 'foo'")
}
if !hasDep(libRootLtoNever, libFoo.Module()) {
t.Errorf("'root_no_lto' missing dependency on thin lto variant of 'foo'")
if !hasDep(result, libRootLtoNever, libFoo.Module()) {
t.Errorf("'root_no_lto' missing dependency on the default variant of 'foo'")
}
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)
}
libBaz := result.ModuleForTests("baz", "android_arm64_armv8-a_static_lto-thin")
if !hasDep(libFoo.Module(), libBaz.Module()) {
t.Errorf("'foo' missing dependency on thin lto variant of transitive dep 'baz'")
libBaz := result.ModuleForTests("baz", "android_arm64_armv8-a_static")
if !hasDep(result, libFoo.Module(), libBaz.Module()) {
t.Errorf("'foo' missing dependency on the default variant of transitive dep 'baz'")
}
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")
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")
libBar := result.ModuleForTests("runtime_libbar", "android_arm_armv7-a-neon_shared").Rule("ld")