Spandan Das | 8159389 | 2022-09-06 17:31:48 +0000 | [diff] [blame] | 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 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
| 18 | "github.com/google/blueprint" |
| 19 | |
| 20 | "android/soong/bazel" |
| 21 | ) |
| 22 | |
| 23 | func init() { |
| 24 | RegisterApiDomainBuildComponents(InitRegistrationContext) |
| 25 | } |
| 26 | |
| 27 | func RegisterApiDomainBuildComponents(ctx RegistrationContext) { |
| 28 | ctx.RegisterModuleType("api_domain", ApiDomainFactory) |
| 29 | } |
| 30 | |
Spandan Das | 1278c2c | 2022-08-19 18:17:28 +0000 | [diff] [blame] | 31 | type ApiSurface int |
| 32 | |
| 33 | // TODO(b/246656800): Reconcile with android.SdkKind |
| 34 | const ( |
Spandan Das | 627fc3e | 2023-01-26 23:02:00 +0000 | [diff] [blame^] | 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 |
Spandan Das | 1278c2c | 2022-08-19 18:17:28 +0000 | [diff] [blame] | 39 | ) |
| 40 | |
| 41 | func (a ApiSurface) String() string { |
| 42 | switch a { |
Spandan Das | 627fc3e | 2023-01-26 23:02:00 +0000 | [diff] [blame^] | 43 | case ModuleLibApi: |
| 44 | return "module-libapi" |
Spandan Das | 1278c2c | 2022-08-19 18:17:28 +0000 | [diff] [blame] | 45 | case PublicApi: |
| 46 | return "publicapi" |
Spandan Das | 1278c2c | 2022-08-19 18:17:28 +0000 | [diff] [blame] | 47 | case VendorApi: |
| 48 | return "vendorapi" |
| 49 | default: |
| 50 | return "invalid" |
| 51 | } |
| 52 | } |
| 53 | |
Spandan Das | 8159389 | 2022-09-06 17:31:48 +0000 | [diff] [blame] | 54 | type apiDomain struct { |
| 55 | ModuleBase |
| 56 | BazelModuleBase |
| 57 | |
| 58 | properties apiDomainProperties |
| 59 | } |
| 60 | |
| 61 | type apiDomainProperties struct { |
| 62 | // cc library contributions (.h files/.map.txt) of this API domain |
Spandan Das | 0b555e3 | 2022-11-28 18:48:51 +0000 | [diff] [blame] | 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 |
Spandan Das | 8159389 | 2022-09-06 17:31:48 +0000 | [diff] [blame] | 65 | Cc_api_contributions []string |
Spandan Das | 0b555e3 | 2022-11-28 18:48:51 +0000 | [diff] [blame] | 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 |
Spandan Das | 8159389 | 2022-09-06 17:31:48 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | func ApiDomainFactory() Module { |
| 74 | m := &apiDomain{} |
| 75 | m.AddProperties(&m.properties) |
| 76 | InitAndroidArchModule(m, DeviceSupported, MultilibBoth) |
Spandan Das | 8159389 | 2022-09-06 17:31:48 +0000 | [diff] [blame] | 77 | return m |
| 78 | } |
| 79 | |
Spandan Das | eb6b907 | 2022-10-08 04:29:37 +0000 | [diff] [blame] | 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 |
Spandan Das | 8159389 | 2022-09-06 17:31:48 +0000 | [diff] [blame] | 84 | func (a *apiDomain) DepsMutator(ctx BottomUpMutatorContext) { |
Spandan Das | 8159389 | 2022-09-06 17:31:48 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | // API domain does not have any builld actions yet |
| 88 | func (a *apiDomain) GenerateAndroidBuildActions(ctx ModuleContext) { |
| 89 | } |
| 90 | |
| 91 | const ( |
| 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 |
| 97 | func 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 |
| 102 | func 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 | |
| 111 | type bazelApiDomainAttributes struct { |
Spandan Das | 0b555e3 | 2022-11-28 18:48:51 +0000 | [diff] [blame] | 112 | Cc_api_contributions bazel.LabelListAttribute |
| 113 | Java_api_contributions bazel.LabelListAttribute |
Spandan Das | 8159389 | 2022-09-06 17:31:48 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Spandan Das | 8b4a5f3 | 2022-09-29 00:28:24 +0000 | [diff] [blame] | 116 | var _ ApiProvider = (*apiDomain)(nil) |
| 117 | |
| 118 | func (a *apiDomain) ConvertWithApiBp2build(ctx TopDownMutatorContext) { |
Spandan Das | 8159389 | 2022-09-06 17:31:48 +0000 | [diff] [blame] | 119 | props := bazel.BazelTargetModuleProperties{ |
| 120 | Rule_class: "api_domain", |
| 121 | Bzl_load_location: "//build/bazel/rules/apis:api_domain.bzl", |
| 122 | } |
| 123 | attrs := &bazelApiDomainAttributes{ |
Spandan Das | 0b555e3 | 2022-11-28 18:48:51 +0000 | [diff] [blame] | 124 | Cc_api_contributions: contributionBazelAttributes(ctx, a.properties.Cc_api_contributions), |
| 125 | Java_api_contributions: contributionBazelAttributes(ctx, a.properties.Java_api_contributions), |
Spandan Das | 8159389 | 2022-09-06 17:31:48 +0000 | [diff] [blame] | 126 | } |
| 127 | ctx.CreateBazelTargetModule(props, CommonAttributes{ |
| 128 | Name: ctx.ModuleName(), |
| 129 | }, attrs) |
| 130 | } |