blob: 587ceaefa1ca6c45d3cbfe5c38b0897db1c6b026 [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 (
Spandan Das627fc3e2023-01-26 23:02:00 +000035 // API surface provided by platform and mainline modules to other mainline modules
36 ModuleLibApi ApiSurface = iota
37 PublicApi // Aka NDK
38 VendorApi // Aka LLNDK
Spandan Das1278c2c2022-08-19 18:17:28 +000039)
40
41func (a ApiSurface) String() string {
42 switch a {
Spandan Das627fc3e2023-01-26 23:02:00 +000043 case ModuleLibApi:
44 return "module-libapi"
Spandan Das1278c2c2022-08-19 18:17:28 +000045 case PublicApi:
46 return "publicapi"
Spandan Das1278c2c2022-08-19 18:17:28 +000047 case VendorApi:
48 return "vendorapi"
49 default:
50 return "invalid"
51 }
52}
53
Spandan Das81593892022-09-06 17:31:48 +000054type apiDomain struct {
55 ModuleBase
56 BazelModuleBase
57
58 properties apiDomainProperties
59}
60
61type apiDomainProperties struct {
62 // cc library contributions (.h files/.map.txt) of this API domain
Spandan Das0b555e32022-11-28 18:48:51 +000063 // This dependency is a no-op in Soong, but the corresponding Bazel target in the api_bp2build workspace
64 // will provide a `CcApiContributionInfo` provider
Spandan Das81593892022-09-06 17:31:48 +000065 Cc_api_contributions []string
Spandan Das0b555e32022-11-28 18:48:51 +000066
67 // java library contributions (as .txt) of this API domain
68 // This dependency is a no-op in Soong, but the corresponding Bazel target in the api_bp2build workspace
69 // will provide a `JavaApiContributionInfo` provider
70 Java_api_contributions []string
Spandan Das81593892022-09-06 17:31:48 +000071}
72
73func ApiDomainFactory() Module {
74 m := &apiDomain{}
75 m.AddProperties(&m.properties)
76 InitAndroidArchModule(m, DeviceSupported, MultilibBoth)
Spandan Das81593892022-09-06 17:31:48 +000077 return m
78}
79
Spandan Daseb6b9072022-10-08 04:29:37 +000080// Do not create any dependency edges in Soong for now to skip visibility checks for some systemapi libraries.
81// Currently, all api_domain modules reside in build/orchestrator/apis/Android.bp
82// However, cc libraries like libsigchain (com.android.art) restrict their visibility to art/*
83// When the api_domain module types are collocated with their contributions, this dependency edge can be restored
Spandan Das81593892022-09-06 17:31:48 +000084func (a *apiDomain) DepsMutator(ctx BottomUpMutatorContext) {
Spandan Das81593892022-09-06 17:31:48 +000085}
86
87// API domain does not have any builld actions yet
88func (a *apiDomain) GenerateAndroidBuildActions(ctx ModuleContext) {
89}
90
91const (
92 apiContributionSuffix = ".contribution"
93)
94
95// ApiContributionTargetName returns the name of the bp2build target (e.g. cc_api_contribution) of contribution modules (e.g. ndk_library)
96// A suffix is necessary to prevent a name collision with the base target in the same bp2build bazel package
97func ApiContributionTargetName(moduleName string) string {
98 return moduleName + apiContributionSuffix
99}
100
101// For each contributing cc_library, format the name to its corresponding contribution bazel target in the bp2build workspace
102func contributionBazelAttributes(ctx TopDownMutatorContext, contributions []string) bazel.LabelListAttribute {
103 addSuffix := func(ctx BazelConversionPathContext, module blueprint.Module) string {
104 baseLabel := BazelModuleLabel(ctx, module)
105 return ApiContributionTargetName(baseLabel)
106 }
107 bazelLabels := BazelLabelForModuleDepsWithFn(ctx, contributions, addSuffix)
108 return bazel.MakeLabelListAttribute(bazelLabels)
109}
110
111type bazelApiDomainAttributes struct {
Spandan Das0b555e32022-11-28 18:48:51 +0000112 Cc_api_contributions bazel.LabelListAttribute
113 Java_api_contributions bazel.LabelListAttribute
Spandan Das81593892022-09-06 17:31:48 +0000114}
115
Spandan Das8b4a5f32022-09-29 00:28:24 +0000116var _ ApiProvider = (*apiDomain)(nil)
117
118func (a *apiDomain) ConvertWithApiBp2build(ctx TopDownMutatorContext) {
Spandan Das81593892022-09-06 17:31:48 +0000119 props := bazel.BazelTargetModuleProperties{
120 Rule_class: "api_domain",
121 Bzl_load_location: "//build/bazel/rules/apis:api_domain.bzl",
122 }
123 attrs := &bazelApiDomainAttributes{
Spandan Das0b555e32022-11-28 18:48:51 +0000124 Cc_api_contributions: contributionBazelAttributes(ctx, a.properties.Cc_api_contributions),
125 Java_api_contributions: contributionBazelAttributes(ctx, a.properties.Java_api_contributions),
Spandan Das81593892022-09-06 17:31:48 +0000126 }
127 ctx.CreateBazelTargetModule(props, CommonAttributes{
128 Name: ctx.ModuleName(),
129 }, attrs)
130}