Add bp2build conversion to reference to fdo_profile targets
The fdo_profile targets are handcrafted in ag/20469925. Bp2build creates fdo_profile attribute on cc_library_shared and references to the fdo_profile target if the profile exists. This works under assumption that all afdo profiles have an associated Bazel fdo_profile target declared in the same folder/package.
As noted in b/253540178, this won't work when we check in the BUILD files (which is likely 6+ months away) because some profiles might only exist internally but not in AOSP. We will implement a long-term solution once we figure out all the requirements we need to support afdo both in Soong and Bazel.
Bug: 253540178
Test: go tests
Change-Id: Iebd5dd7a76583b4b2bf0d9ee56d58f247d313a54
Merged-In: Iebd5dd7a76583b4b2bf0d9ee56d58f247d313a54
diff --git a/bp2build/cc_library_conversion_test.go b/bp2build/cc_library_conversion_test.go
index 5feb02e..1ffdc0e 100644
--- a/bp2build/cc_library_conversion_test.go
+++ b/bp2build/cc_library_conversion_test.go
@@ -3603,6 +3603,70 @@
})
}
+func TestCcLibraryWithAfdoEnabled(t *testing.T) {
+ bp := `
+cc_library {
+ name: "foo",
+ afdo: true,
+ include_build_directory: false,
+}`
+
+ // TODO(b/260714900): Add test case for arch-specific afdo profile
+ testCases := []struct {
+ description string
+ filesystem map[string]string
+ expectedBazelTargets []string
+ }{
+ {
+ description: "cc_library with afdo enabled and existing profile",
+ filesystem: map[string]string{"vendor/google_data/pgo_profile/sampling/foo.afdo": ""},
+ expectedBazelTargets: []string{
+ MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
+ MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
+ "fdo_profile": `"//vendor/google_data/pgo_profile/sampling:foo"`,
+ }),
+ },
+ },
+ {
+ description: "cc_library with afdo enabled and existing profile in AOSP",
+ filesystem: map[string]string{"toolchain/pgo-profiles/sampling/foo.afdo": ""},
+ expectedBazelTargets: []string{
+ MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
+ MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
+ "fdo_profile": `"//toolchain/pgo-profiles/sampling:foo"`,
+ }),
+ },
+ },
+ {
+ description: "cc_library with afdo enabled but profile filename doesn't match with module name",
+ filesystem: map[string]string{"toolchain/pgo-profiles/sampling/bar.afdo": ""},
+ expectedBazelTargets: []string{
+ MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
+ MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{}),
+ },
+ },
+ {
+ description: "cc_library with afdo enabled but profile doesn't exist",
+ expectedBazelTargets: []string{
+ MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{}),
+ MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{}),
+ },
+ },
+ }
+ for _, testCase := range testCases {
+ t.Run(testCase.description, func(t *testing.T) {
+ runCcLibraryTestCase(t, Bp2buildTestCase{
+ ExpectedBazelTargets: testCase.expectedBazelTargets,
+ ModuleTypeUnderTest: "cc_library",
+ ModuleTypeUnderTestFactory: cc.LibraryFactory,
+ Description: testCase.description,
+ Blueprint: binaryReplacer.Replace(bp),
+ Filesystem: testCase.filesystem,
+ })
+ })
+ }
+}
+
func TestCcLibraryHeaderAbiChecker(t *testing.T) {
runCcLibraryTestCase(t, Bp2buildTestCase{
Description: "cc_library with header abi checker",
diff --git a/cc/bp2build.go b/cc/bp2build.go
index d41aa00..598e350 100644
--- a/cc/bp2build.go
+++ b/cc/bp2build.go
@@ -393,6 +393,8 @@
features bazel.StringListAttribute
suffix bazel.StringAttribute
+
+ fdoProfile bazel.LabelAttribute
}
type filterOutFn func(string) bool
@@ -777,6 +779,13 @@
(&compilerAttrs).srcs.Add(&convertedLSrcs.srcName)
(&compilerAttrs).cSrcs.Add(&convertedLSrcs.cSrcName)
+ if module.afdo != nil && module.afdo.Properties.Afdo {
+ fdoProfileDep := bp2buildFdoProfile(ctx, module)
+ if fdoProfileDep != nil {
+ (&compilerAttrs).fdoProfile.SetValue(*fdoProfileDep)
+ }
+ }
+
if !compilerAttrs.syspropSrcs.IsEmpty() {
(&linkerAttrs).wholeArchiveDeps.Add(bp2buildCcSysprop(ctx, module.Name(), module.Properties.Min_sdk_version, compilerAttrs.syspropSrcs))
}
@@ -793,6 +802,37 @@
}
}
+type fdoProfileAttributes struct {
+ Absolute_path_profile string
+}
+
+func bp2buildFdoProfile(
+ ctx android.Bp2buildMutatorContext,
+ m *Module,
+) *bazel.Label {
+ for _, project := range globalAfdoProfileProjects {
+ // We handcraft a BUILD file with fdo_profile targets that use the existing profiles in the project
+ // This implementation is assuming that every afdo profile in globalAfdoProfileProjects already has
+ // an associated fdo_profile target declared in the same package.
+ // TODO(b/260714900): Handle arch-specific afdo profiles (e.g. `<module-name>-arm<64>.afdo`)
+ path := android.ExistentPathForSource(ctx, project, m.Name()+".afdo")
+ if path.Valid() {
+ // FIXME: Some profiles only exist internally and are not released to AOSP.
+ // When generated BUILD files are checked in, we'll run into merge conflict.
+ // The cc_library_shared target in AOSP won't have reference to an fdo_profile target because
+ // the profile doesn't exist. Internally, the same cc_library_shared target will
+ // have reference to the fdo_profile.
+ // For more context, see b/258682955#comment2
+ fdoProfileLabel := "//" + strings.TrimSuffix(project, "/") + ":" + m.Name()
+ return &bazel.Label{
+ Label: fdoProfileLabel,
+ }
+ }
+ }
+
+ return nil
+}
+
func bp2buildCcAidlLibrary(
ctx android.Bp2buildMutatorContext,
m *Module,
diff --git a/cc/library.go b/cc/library.go
index 7059023..ed0ed01 100644
--- a/cc/library.go
+++ b/cc/library.go
@@ -417,6 +417,8 @@
Strip: stripAttrsFromLinkerAttrs(&linkerAttrs),
Features: baseAttributes.features,
bazelCcHeaderAbiCheckerAttributes: bp2buildParseAbiCheckerProps(ctx, m),
+
+ Fdo_profile: compilerAttrs.fdoProfile,
}
if compilerAttrs.stubsSymbolFile != nil && len(compilerAttrs.stubsVersions.Value) > 0 {
@@ -2930,6 +2932,8 @@
Suffix: compilerAttrs.suffix,
bazelCcHeaderAbiCheckerAttributes: bp2buildParseAbiCheckerProps(ctx, module),
+
+ Fdo_profile: compilerAttrs.fdoProfile,
}
if compilerAttrs.stubsSymbolFile != nil && len(compilerAttrs.stubsVersions.Value) > 0 {
hasStubs := true
@@ -3020,6 +3024,8 @@
Suffix bazel.StringAttribute
bazelCcHeaderAbiCheckerAttributes
+
+ Fdo_profile bazel.LabelAttribute
}
type bazelCcStubSuiteAttributes struct {