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 ( |
| 35 | PublicApi ApiSurface = iota |
| 36 | SystemApi |
| 37 | VendorApi |
| 38 | ) |
| 39 | |
| 40 | func (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 Das | 8159389 | 2022-09-06 17:31:48 +0000 | [diff] [blame] | 53 | type apiDomain struct { |
| 54 | ModuleBase |
| 55 | BazelModuleBase |
| 56 | |
| 57 | properties apiDomainProperties |
| 58 | } |
| 59 | |
| 60 | type 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 | |
| 66 | func ApiDomainFactory() Module { |
| 67 | m := &apiDomain{} |
| 68 | m.AddProperties(&m.properties) |
| 69 | InitAndroidArchModule(m, DeviceSupported, MultilibBoth) |
| 70 | InitBazelModule(m) |
| 71 | return m |
| 72 | } |
| 73 | |
| 74 | func (a *apiDomain) DepsMutator(ctx BottomUpMutatorContext) { |
| 75 | for _, cc := range a.properties.Cc_api_contributions { |
| 76 | // Use FarVariationDependencies since the variants of api_domain is a subset of the variants of the dependency cc module |
| 77 | // Creating a dependency on the first variant is ok since this is a no-op in Soong |
| 78 | // The primary function of this dependency is to create a connected graph in the corresponding bp2build workspace |
| 79 | ctx.AddFarVariationDependencies([]blueprint.Variation{}, nil, cc) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // API domain does not have any builld actions yet |
| 84 | func (a *apiDomain) GenerateAndroidBuildActions(ctx ModuleContext) { |
| 85 | } |
| 86 | |
| 87 | const ( |
| 88 | apiContributionSuffix = ".contribution" |
| 89 | ) |
| 90 | |
| 91 | // ApiContributionTargetName returns the name of the bp2build target (e.g. cc_api_contribution) of contribution modules (e.g. ndk_library) |
| 92 | // A suffix is necessary to prevent a name collision with the base target in the same bp2build bazel package |
| 93 | func ApiContributionTargetName(moduleName string) string { |
| 94 | return moduleName + apiContributionSuffix |
| 95 | } |
| 96 | |
| 97 | // For each contributing cc_library, format the name to its corresponding contribution bazel target in the bp2build workspace |
| 98 | func contributionBazelAttributes(ctx TopDownMutatorContext, contributions []string) bazel.LabelListAttribute { |
| 99 | addSuffix := func(ctx BazelConversionPathContext, module blueprint.Module) string { |
| 100 | baseLabel := BazelModuleLabel(ctx, module) |
| 101 | return ApiContributionTargetName(baseLabel) |
| 102 | } |
| 103 | bazelLabels := BazelLabelForModuleDepsWithFn(ctx, contributions, addSuffix) |
| 104 | return bazel.MakeLabelListAttribute(bazelLabels) |
| 105 | } |
| 106 | |
| 107 | type bazelApiDomainAttributes struct { |
| 108 | Cc_api_contributions bazel.LabelListAttribute |
| 109 | } |
| 110 | |
| 111 | func (a *apiDomain) ConvertWithBp2build(ctx TopDownMutatorContext) { |
| 112 | props := bazel.BazelTargetModuleProperties{ |
| 113 | Rule_class: "api_domain", |
| 114 | Bzl_load_location: "//build/bazel/rules/apis:api_domain.bzl", |
| 115 | } |
| 116 | attrs := &bazelApiDomainAttributes{ |
| 117 | Cc_api_contributions: contributionBazelAttributes(ctx, a.properties.Cc_api_contributions), |
| 118 | } |
| 119 | ctx.CreateBazelTargetModule(props, CommonAttributes{ |
| 120 | Name: ctx.ModuleName(), |
| 121 | }, attrs) |
| 122 | } |