blob: 787665475071d562b6df8c1eb802e8982b444ff2 [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
Spandan Das1278c2c2022-08-19 18:17:28 +000031type ApiSurface int
32
33// TODO(b/246656800): Reconcile with android.SdkKind
34const (
35 PublicApi ApiSurface = iota
36 SystemApi
37 VendorApi
38)
39
40func (a ApiSurface) String() string {
41 switch a {
42 case PublicApi:
43 return "publicapi"
44 case SystemApi:
45 return "systemapi"
46 case VendorApi:
47 return "vendorapi"
48 default:
49 return "invalid"
50 }
51}
52
Spandan Das81593892022-09-06 17:31:48 +000053type apiDomain struct {
54 ModuleBase
55 BazelModuleBase
56
57 properties apiDomainProperties
58}
59
60type apiDomainProperties struct {
61 // cc library contributions (.h files/.map.txt) of this API domain
62 // This dependency is a no-op in Soong, but the corresponding Bazel target in the bp2build workspace will provide a `CcApiContributionInfo` provider
63 Cc_api_contributions []string
64}
65
66func ApiDomainFactory() Module {
67 m := &apiDomain{}
68 m.AddProperties(&m.properties)
69 InitAndroidArchModule(m, DeviceSupported, MultilibBoth)
Spandan Das81593892022-09-06 17:31:48 +000070 return m
71}
72
73func (a *apiDomain) DepsMutator(ctx BottomUpMutatorContext) {
74 for _, cc := range a.properties.Cc_api_contributions {
75 // Use FarVariationDependencies since the variants of api_domain is a subset of the variants of the dependency cc module
76 // Creating a dependency on the first variant is ok since this is a no-op in Soong
77 // The primary function of this dependency is to create a connected graph in the corresponding bp2build workspace
78 ctx.AddFarVariationDependencies([]blueprint.Variation{}, nil, cc)
79 }
80}
81
82// API domain does not have any builld actions yet
83func (a *apiDomain) GenerateAndroidBuildActions(ctx ModuleContext) {
84}
85
86const (
87 apiContributionSuffix = ".contribution"
88)
89
90// ApiContributionTargetName returns the name of the bp2build target (e.g. cc_api_contribution) of contribution modules (e.g. ndk_library)
91// A suffix is necessary to prevent a name collision with the base target in the same bp2build bazel package
92func ApiContributionTargetName(moduleName string) string {
93 return moduleName + apiContributionSuffix
94}
95
96// For each contributing cc_library, format the name to its corresponding contribution bazel target in the bp2build workspace
97func contributionBazelAttributes(ctx TopDownMutatorContext, contributions []string) bazel.LabelListAttribute {
98 addSuffix := func(ctx BazelConversionPathContext, module blueprint.Module) string {
99 baseLabel := BazelModuleLabel(ctx, module)
100 return ApiContributionTargetName(baseLabel)
101 }
102 bazelLabels := BazelLabelForModuleDepsWithFn(ctx, contributions, addSuffix)
103 return bazel.MakeLabelListAttribute(bazelLabels)
104}
105
106type bazelApiDomainAttributes struct {
107 Cc_api_contributions bazel.LabelListAttribute
108}
109
Spandan Das8b4a5f32022-09-29 00:28:24 +0000110var _ ApiProvider = (*apiDomain)(nil)
111
112func (a *apiDomain) ConvertWithApiBp2build(ctx TopDownMutatorContext) {
Spandan Das81593892022-09-06 17:31:48 +0000113 props := bazel.BazelTargetModuleProperties{
114 Rule_class: "api_domain",
115 Bzl_load_location: "//build/bazel/rules/apis:api_domain.bzl",
116 }
117 attrs := &bazelApiDomainAttributes{
118 Cc_api_contributions: contributionBazelAttributes(ctx, a.properties.Cc_api_contributions),
119 }
120 ctx.CreateBazelTargetModule(props, CommonAttributes{
121 Name: ctx.ModuleName(),
122 }, attrs)
123}