blob: bdd4e6fa57fd597cbb6407e9edd82c319e08fc7d [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
Spandan Das0b555e32022-11-28 18:48:51 +000062 // This dependency is a no-op in Soong, but the corresponding Bazel target in the api_bp2build workspace
63 // will provide a `CcApiContributionInfo` provider
Spandan Das81593892022-09-06 17:31:48 +000064 Cc_api_contributions []string
Spandan Das0b555e32022-11-28 18:48:51 +000065
66 // java library contributions (as .txt) of this API domain
67 // This dependency is a no-op in Soong, but the corresponding Bazel target in the api_bp2build workspace
68 // will provide a `JavaApiContributionInfo` provider
69 Java_api_contributions []string
Spandan Das81593892022-09-06 17:31:48 +000070}
71
72func ApiDomainFactory() Module {
73 m := &apiDomain{}
74 m.AddProperties(&m.properties)
75 InitAndroidArchModule(m, DeviceSupported, MultilibBoth)
Spandan Das81593892022-09-06 17:31:48 +000076 return m
77}
78
Spandan Daseb6b9072022-10-08 04:29:37 +000079// Do not create any dependency edges in Soong for now to skip visibility checks for some systemapi libraries.
80// Currently, all api_domain modules reside in build/orchestrator/apis/Android.bp
81// However, cc libraries like libsigchain (com.android.art) restrict their visibility to art/*
82// When the api_domain module types are collocated with their contributions, this dependency edge can be restored
Spandan Das81593892022-09-06 17:31:48 +000083func (a *apiDomain) DepsMutator(ctx BottomUpMutatorContext) {
Spandan Das81593892022-09-06 17:31:48 +000084}
85
86// API domain does not have any builld actions yet
87func (a *apiDomain) GenerateAndroidBuildActions(ctx ModuleContext) {
88}
89
90const (
91 apiContributionSuffix = ".contribution"
92)
93
94// ApiContributionTargetName returns the name of the bp2build target (e.g. cc_api_contribution) of contribution modules (e.g. ndk_library)
95// A suffix is necessary to prevent a name collision with the base target in the same bp2build bazel package
96func ApiContributionTargetName(moduleName string) string {
97 return moduleName + apiContributionSuffix
98}
99
100// For each contributing cc_library, format the name to its corresponding contribution bazel target in the bp2build workspace
101func contributionBazelAttributes(ctx TopDownMutatorContext, contributions []string) bazel.LabelListAttribute {
102 addSuffix := func(ctx BazelConversionPathContext, module blueprint.Module) string {
103 baseLabel := BazelModuleLabel(ctx, module)
104 return ApiContributionTargetName(baseLabel)
105 }
106 bazelLabels := BazelLabelForModuleDepsWithFn(ctx, contributions, addSuffix)
107 return bazel.MakeLabelListAttribute(bazelLabels)
108}
109
110type bazelApiDomainAttributes struct {
Spandan Das0b555e32022-11-28 18:48:51 +0000111 Cc_api_contributions bazel.LabelListAttribute
112 Java_api_contributions bazel.LabelListAttribute
Spandan Das81593892022-09-06 17:31:48 +0000113}
114
Spandan Das8b4a5f32022-09-29 00:28:24 +0000115var _ ApiProvider = (*apiDomain)(nil)
116
117func (a *apiDomain) ConvertWithApiBp2build(ctx TopDownMutatorContext) {
Spandan Das81593892022-09-06 17:31:48 +0000118 props := bazel.BazelTargetModuleProperties{
119 Rule_class: "api_domain",
120 Bzl_load_location: "//build/bazel/rules/apis:api_domain.bzl",
121 }
122 attrs := &bazelApiDomainAttributes{
Spandan Das0b555e32022-11-28 18:48:51 +0000123 Cc_api_contributions: contributionBazelAttributes(ctx, a.properties.Cc_api_contributions),
124 Java_api_contributions: contributionBazelAttributes(ctx, a.properties.Java_api_contributions),
Spandan Das81593892022-09-06 17:31:48 +0000125 }
126 ctx.CreateBazelTargetModule(props, CommonAttributes{
127 Name: ctx.ModuleName(),
128 }, attrs)
129}