From abedff0ca7d48dc5cb73b16290beeec09627fa6e Mon Sep 17 00:00:00 2001 From: Spandan Das Date: Tue, 7 Mar 2023 19:24:34 +0000 Subject: [PATCH 1/2] Add a method in bp2build to create aliases in another directory The expected use case for this is to create aliases for stub libraries in the @api_surfaces repository in build/bazel/api_surfaces. This restricts the scope to just aliases. If we have a use case for generating actual Bazel targets in another directory, a workaround could be to generate the targets in the current directory (via CreateBazelTargetModule) and aliases to it in the other directory Test: go test ./bp2build Change-Id: I6b63d9d018618d447fc7c260a2a94aaa00e57a4d --- android/mutator.go | 33 +++++++++++++++++++++++++++++++++ bp2build/build_conversion.go | 17 +++++++++++++++-- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/android/mutator.go b/android/mutator.go index 4dacb8df8..676f8a511 100644 --- a/android/mutator.go +++ b/android/mutator.go @@ -268,6 +268,11 @@ type TopDownMutatorContext interface { // platforms, as dictated by a given bool attribute: the target will not be buildable in // any platform for which this bool attribute is false. CreateBazelTargetModuleWithRestrictions(bazel.BazelTargetModuleProperties, CommonAttributes, interface{}, bazel.BoolAttribute) + + // CreateBazelTargetAliasInDir creates an alias definition in `dir` directory. + // This function can be used to create alias definitions in a directory that is different + // from the directory of the visited Soong module. + CreateBazelTargetAliasInDir(dir string, name string, actual bazel.Label) } type topDownMutatorContext struct { @@ -705,6 +710,34 @@ func (t *topDownMutatorContext) CreateBazelTargetModuleWithRestrictions( t.createBazelTargetModule(bazelProps, commonAttrs, attrs, enabledProperty) } +var ( + bazelAliasModuleProperties = bazel.BazelTargetModuleProperties{ + Rule_class: "alias", + } +) + +type bazelAliasAttributes struct { + Actual *bazel.LabelAttribute +} + +func (t *topDownMutatorContext) CreateBazelTargetAliasInDir( + dir string, + name string, + actual bazel.Label) { + mod := t.Module() + attrs := &bazelAliasAttributes{ + Actual: bazel.MakeLabelAttribute(actual.Label), + } + info := bp2buildInfo{ + Dir: dir, + BazelProps: bazelAliasModuleProperties, + CommonAttrs: CommonAttributes{Name: name}, + ConstraintAttrs: constraintAttributes{}, + Attrs: attrs, + } + mod.base().addBp2buildInfo(info) +} + // ApexAvailableTags converts the apex_available property value of an ApexModule // module and returns it as a list of keyed tags. func ApexAvailableTags(mod Module) bazel.StringListAttribute { diff --git a/bp2build/build_conversion.go b/bp2build/build_conversion.go index ced779c33..fde9b6949 100644 --- a/bp2build/build_conversion.go +++ b/bp2build/build_conversion.go @@ -60,6 +60,15 @@ func (t BazelTarget) Label() string { } } +// PackageName returns the package of the Bazel target. +// Defaults to root of tree. +func (t BazelTarget) PackageName() string { + if t.packageName == "" { + return "." + } + return t.packageName +} + // BazelTargets is a typedef for a slice of BazelTarget objects. type BazelTargets []BazelTarget @@ -337,7 +346,10 @@ func GenerateBazelTargets(ctx *CodegenContext, generateFilegroups bool) (convers return } - buildFileToTargets[dir] = append(buildFileToTargets[dir], targets...) + for _, target := range targets { + targetDir := target.PackageName() + buildFileToTargets[targetDir] = append(buildFileToTargets[targetDir], target) + } }) if len(errs) > 0 { @@ -454,7 +466,8 @@ func generateSoongModuleTarget(ctx bpToBuildContext, m blueprint.Module) (BazelT targetName := targetNameWithVariant(ctx, m) return BazelTarget{ - name: targetName, + name: targetName, + packageName: ctx.ModuleDir(m), content: fmt.Sprintf( soongModuleTargetTemplate, targetName, From 9e93d3d6e1cb19c486a9f50c4530aa5f607fe56d Mon Sep 17 00:00:00 2001 From: Spandan Das Date: Wed, 8 Mar 2023 05:47:29 +0000 Subject: [PATCH 2/2] Create aliases for stubs in build/bazel/api_surfaces - Create the alias under the module-libapi directory. This is the api_surface that cc_stubs_suite maps to. - Create the alias only for "current" (atleast for now) - Create one alias for the stub shared lib, and another for its headers Test: b build @api_surfaces//... (with aosp/2475091) Change-Id: Ib004c2c34256f971e74d75317fa5fbbe7273720e --- android/config.go | 11 +++++++++++ cc/library.go | 15 +++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/android/config.go b/android/config.go index b37d5c827..07151f9de 100644 --- a/android/config.go +++ b/android/config.go @@ -1837,3 +1837,14 @@ func (c *config) LogMixedBuild(ctx BaseModuleContext, useBazel bool) { c.mixedBuildDisabledModules[moduleName] = struct{}{} } } + +// ApiSurfaces directory returns the source path inside the api_surfaces repo +// (relative to workspace root). +func (c *config) ApiSurfacesDir(s ApiSurface, version string) string { + return filepath.Join( + "build", + "bazel", + "api_surfaces", + s.String(), + version) +} diff --git a/cc/library.go b/cc/library.go index e73af8179..27f06230b 100644 --- a/cc/library.go +++ b/cc/library.go @@ -464,6 +464,21 @@ func createStubsBazelTargetIfNeeded(ctx android.TopDownMutatorContext, m *Module ctx.CreateBazelTargetModule(stubSuitesProps, android.CommonAttributes{Name: m.Name() + "_stub_libs"}, stubSuitesAttrs) + + // Add alias for the stub shared_library in @api_surfaces repository + currentModuleLibApiDir := ctx.Config().ApiSurfacesDir(android.ModuleLibApi, "current") + actualLabelInMainWorkspace := bazel.Label{ + Label: fmt.Sprintf("@//%s:%s_stub_libs_current", ctx.ModuleDir(), m.Name()), + } + ctx.CreateBazelTargetAliasInDir(currentModuleLibApiDir, m.Name(), actualLabelInMainWorkspace) + + // Add alias for headers exported by the stub library + headerLabelInMainWorkspace := bazel.Label{ + // This label is generated from cc_stub_suite macro + Label: fmt.Sprintf("@//%s:%s_stub_libs_%s_headers", ctx.ModuleDir(), m.Name(), android.ModuleLibApi.String()), + } + headerAlias := m.Name() + "_headers" + ctx.CreateBazelTargetAliasInDir(currentModuleLibApiDir, headerAlias, headerLabelInMainWorkspace) } }