blob: a808e3260e0b887ae18df9078ce60c9307b09d87 [file] [log] [blame]
Spandan Das81593892022-09-06 17:31:48 +00001// Copyright 2022 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package android
16
17import (
18 "github.com/google/blueprint"
19
20 "android/soong/bazel"
21)
22
23func init() {
24 RegisterApiDomainBuildComponents(InitRegistrationContext)
25}
26
27func RegisterApiDomainBuildComponents(ctx RegistrationContext) {
28 ctx.RegisterModuleType("api_domain", ApiDomainFactory)
29}
30
31type apiDomain struct {
32 ModuleBase
33 BazelModuleBase
34
35 properties apiDomainProperties
36}
37
38type apiDomainProperties struct {
39 // cc library contributions (.h files/.map.txt) of this API domain
40 // This dependency is a no-op in Soong, but the corresponding Bazel target in the bp2build workspace will provide a `CcApiContributionInfo` provider
41 Cc_api_contributions []string
42}
43
44func ApiDomainFactory() Module {
45 m := &apiDomain{}
46 m.AddProperties(&m.properties)
47 InitAndroidArchModule(m, DeviceSupported, MultilibBoth)
48 InitBazelModule(m)
49 return m
50}
51
52func (a *apiDomain) DepsMutator(ctx BottomUpMutatorContext) {
53 for _, cc := range a.properties.Cc_api_contributions {
54 // Use FarVariationDependencies since the variants of api_domain is a subset of the variants of the dependency cc module
55 // Creating a dependency on the first variant is ok since this is a no-op in Soong
56 // The primary function of this dependency is to create a connected graph in the corresponding bp2build workspace
57 ctx.AddFarVariationDependencies([]blueprint.Variation{}, nil, cc)
58 }
59}
60
61// API domain does not have any builld actions yet
62func (a *apiDomain) GenerateAndroidBuildActions(ctx ModuleContext) {
63}
64
65const (
66 apiContributionSuffix = ".contribution"
67)
68
69// ApiContributionTargetName returns the name of the bp2build target (e.g. cc_api_contribution) of contribution modules (e.g. ndk_library)
70// A suffix is necessary to prevent a name collision with the base target in the same bp2build bazel package
71func ApiContributionTargetName(moduleName string) string {
72 return moduleName + apiContributionSuffix
73}
74
75// For each contributing cc_library, format the name to its corresponding contribution bazel target in the bp2build workspace
76func contributionBazelAttributes(ctx TopDownMutatorContext, contributions []string) bazel.LabelListAttribute {
77 addSuffix := func(ctx BazelConversionPathContext, module blueprint.Module) string {
78 baseLabel := BazelModuleLabel(ctx, module)
79 return ApiContributionTargetName(baseLabel)
80 }
81 bazelLabels := BazelLabelForModuleDepsWithFn(ctx, contributions, addSuffix)
82 return bazel.MakeLabelListAttribute(bazelLabels)
83}
84
85type bazelApiDomainAttributes struct {
86 Cc_api_contributions bazel.LabelListAttribute
87}
88
89func (a *apiDomain) ConvertWithBp2build(ctx TopDownMutatorContext) {
90 props := bazel.BazelTargetModuleProperties{
91 Rule_class: "api_domain",
92 Bzl_load_location: "//build/bazel/rules/apis:api_domain.bzl",
93 }
94 attrs := &bazelApiDomainAttributes{
95 Cc_api_contributions: contributionBazelAttributes(ctx, a.properties.Cc_api_contributions),
96 }
97 ctx.CreateBazelTargetModule(props, CommonAttributes{
98 Name: ctx.ModuleName(),
99 }, attrs)
100}