• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// 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 ApiSurface int
32
33// TODO(b/246656800): Reconcile with android.SdkKind
34const (
35	// API surface provided by platform and mainline modules to other mainline modules
36	ModuleLibApi ApiSurface = iota
37	PublicApi               // Aka NDK
38	VendorApi               // Aka LLNDK
39)
40
41func (a ApiSurface) String() string {
42	switch a {
43	case ModuleLibApi:
44		return "module-libapi"
45	case PublicApi:
46		return "publicapi"
47	case VendorApi:
48		return "vendorapi"
49	default:
50		return "invalid"
51	}
52}
53
54type 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
63	// This dependency is a no-op in Soong, but the corresponding Bazel target in the api_bp2build workspace
64	// will provide a `CcApiContributionInfo` provider
65	Cc_api_contributions []string
66
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
71}
72
73func ApiDomainFactory() Module {
74	m := &apiDomain{}
75	m.AddProperties(&m.properties)
76	InitAndroidArchModule(m, DeviceSupported, MultilibBoth)
77	return m
78}
79
80// 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
84func (a *apiDomain) DepsMutator(ctx BottomUpMutatorContext) {
85}
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 {
112	Cc_api_contributions   bazel.LabelListAttribute
113	Java_api_contributions bazel.LabelListAttribute
114}
115
116var _ ApiProvider = (*apiDomain)(nil)
117
118func (a *apiDomain) ConvertWithApiBp2build(ctx TopDownMutatorContext) {
119	props := bazel.BazelTargetModuleProperties{
120		Rule_class:        "api_domain",
121		Bzl_load_location: "//build/bazel/rules/apis:api_domain.bzl",
122	}
123	attrs := &bazelApiDomainAttributes{
124		Cc_api_contributions:   contributionBazelAttributes(ctx, a.properties.Cc_api_contributions),
125		Java_api_contributions: contributionBazelAttributes(ctx, a.properties.Java_api_contributions),
126	}
127	ctx.CreateBazelTargetModule(props, CommonAttributes{
128		Name: ctx.ModuleName(),
129	}, attrs)
130}
131