Vinh Tran | 0e7fd8a | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 1 | // Copyright 2023 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 aidl_library |
| 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
Vinh Tran | 3d16990 | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 19 | "android/soong/bazel" |
Vinh Tran | 0e7fd8a | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 20 | |
| 21 | "github.com/google/blueprint" |
| 22 | "github.com/google/blueprint/proptools" |
| 23 | ) |
| 24 | |
Vinh Tran | 367d89d | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 25 | var PrepareForTestWithAidlLibrary = android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) { |
| 26 | registerAidlLibraryBuildComponents(ctx) |
| 27 | }) |
| 28 | |
Vinh Tran | 0e7fd8a | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 29 | func init() { |
| 30 | registerAidlLibraryBuildComponents(android.InitRegistrationContext) |
| 31 | } |
| 32 | |
| 33 | func registerAidlLibraryBuildComponents(ctx android.RegistrationContext) { |
| 34 | ctx.RegisterModuleType("aidl_library", AidlLibraryFactory) |
| 35 | } |
| 36 | |
| 37 | type aidlLibraryProperties struct { |
| 38 | // srcs lists files that are included in this module for aidl compilation |
| 39 | Srcs []string `android:"path"` |
| 40 | |
| 41 | // hdrs lists the headers that are imported by srcs but are not compiled by aidl to language binding code |
| 42 | // hdrs is provided to support Bazel migration. It is a no-op until |
| 43 | // we enable input sandbox in aidl compilation action |
| 44 | Hdrs []string `android:"path"` |
| 45 | |
| 46 | // The prefix to strip from the paths of the .aidl files |
| 47 | // The remaining path is the package path of the aidl interface |
| 48 | Strip_import_prefix *string |
| 49 | |
| 50 | // List of aidl files or aidl_library depended on by the module |
| 51 | Deps []string `android:"arch_variant"` |
| 52 | } |
| 53 | |
| 54 | type AidlLibrary struct { |
| 55 | android.ModuleBase |
Vinh Tran | 3d16990 | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 56 | android.BazelModuleBase |
Vinh Tran | 0e7fd8a | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 57 | properties aidlLibraryProperties |
| 58 | } |
| 59 | |
Vinh Tran | 3d16990 | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 60 | type bazelAidlLibraryAttributes struct { |
| 61 | Srcs bazel.LabelListAttribute |
| 62 | Hdrs bazel.LabelListAttribute |
| 63 | Strip_import_prefix *string |
| 64 | Deps bazel.LabelListAttribute |
| 65 | } |
| 66 | |
| 67 | func (lib *AidlLibrary) ConvertWithBp2build(ctx android.TopDownMutatorContext) { |
| 68 | srcs := bazel.MakeLabelListAttribute( |
| 69 | android.BazelLabelForModuleSrc( |
| 70 | ctx, |
| 71 | lib.properties.Srcs, |
| 72 | ), |
| 73 | ) |
| 74 | |
| 75 | hdrs := bazel.MakeLabelListAttribute( |
| 76 | android.BazelLabelForModuleSrc( |
| 77 | ctx, |
| 78 | lib.properties.Hdrs, |
| 79 | ), |
| 80 | ) |
| 81 | |
| 82 | tags := []string{"apex_available=//apex_available:anyapex"} |
| 83 | deps := bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, lib.properties.Deps)) |
| 84 | |
| 85 | attrs := &bazelAidlLibraryAttributes{ |
| 86 | Srcs: srcs, |
| 87 | Hdrs: hdrs, |
| 88 | Strip_import_prefix: lib.properties.Strip_import_prefix, |
| 89 | Deps: deps, |
| 90 | } |
| 91 | |
| 92 | props := bazel.BazelTargetModuleProperties{ |
| 93 | Rule_class: "aidl_library", |
| 94 | Bzl_load_location: "//build/bazel/rules/aidl:aidl_library.bzl", |
| 95 | } |
| 96 | |
| 97 | ctx.CreateBazelTargetModule( |
| 98 | props, |
| 99 | android.CommonAttributes{ |
| 100 | Name: lib.Name(), |
| 101 | Tags: bazel.MakeStringListAttribute(tags), |
| 102 | }, |
| 103 | attrs, |
| 104 | ) |
| 105 | } |
| 106 | |
Vinh Tran | 0e7fd8a | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 107 | type AidlLibraryInfo struct { |
| 108 | // The direct aidl files of the module |
| 109 | Srcs android.Paths |
Vinh Tran | 0958195 | 2023-05-16 16:03:20 -0400 | [diff] [blame^] | 110 | // The include dirs to the direct aidl files and those provided from transitive aidl_library deps |
Vinh Tran | 0e7fd8a | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 111 | IncludeDirs android.DepSet |
Vinh Tran | 0958195 | 2023-05-16 16:03:20 -0400 | [diff] [blame^] | 112 | // The direct hdrs and hdrs from transitive deps |
| 113 | Hdrs android.DepSet |
Vinh Tran | 0e7fd8a | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | // AidlLibraryProvider provides the srcs and the transitive include dirs |
| 117 | var AidlLibraryProvider = blueprint.NewProvider(AidlLibraryInfo{}) |
| 118 | |
| 119 | func (lib *AidlLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 120 | includeDirsDepSetBuilder := android.NewDepSetBuilder(android.PREORDER) |
Vinh Tran | 0958195 | 2023-05-16 16:03:20 -0400 | [diff] [blame^] | 121 | hdrsDepSetBuilder := android.NewDepSetBuilder(android.PREORDER) |
Vinh Tran | 0e7fd8a | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 122 | |
| 123 | if len(lib.properties.Srcs) == 0 && len(lib.properties.Hdrs) == 0 { |
| 124 | ctx.ModuleErrorf("at least srcs or hdrs prop must be non-empty") |
| 125 | } |
| 126 | |
| 127 | srcs := android.PathsForModuleSrc(ctx, lib.properties.Srcs) |
Vinh Tran | 0958195 | 2023-05-16 16:03:20 -0400 | [diff] [blame^] | 128 | hdrs := android.PathsForModuleSrc(ctx, lib.properties.Hdrs) |
| 129 | |
Vinh Tran | 0e7fd8a | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 130 | if lib.properties.Strip_import_prefix != nil { |
| 131 | srcs = android.PathsWithModuleSrcSubDir( |
| 132 | ctx, |
| 133 | srcs, |
Vinh Tran | 0958195 | 2023-05-16 16:03:20 -0400 | [diff] [blame^] | 134 | android.String(lib.properties.Strip_import_prefix), |
| 135 | ) |
| 136 | |
| 137 | hdrs = android.PathsWithModuleSrcSubDir( |
| 138 | ctx, |
| 139 | hdrs, |
| 140 | android.String(lib.properties.Strip_import_prefix), |
| 141 | ) |
Vinh Tran | 0e7fd8a | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 142 | } |
Vinh Tran | 0958195 | 2023-05-16 16:03:20 -0400 | [diff] [blame^] | 143 | hdrsDepSetBuilder.Direct(hdrs...) |
Vinh Tran | 0e7fd8a | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 144 | |
| 145 | includeDir := android.PathForModuleSrc( |
| 146 | ctx, |
| 147 | proptools.StringDefault(lib.properties.Strip_import_prefix, ""), |
| 148 | ) |
Vinh Tran | 0e7fd8a | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 149 | includeDirsDepSetBuilder.Direct(includeDir) |
| 150 | |
| 151 | for _, dep := range ctx.GetDirectDepsWithTag(aidlLibraryTag) { |
| 152 | if ctx.OtherModuleHasProvider(dep, AidlLibraryProvider) { |
| 153 | info := ctx.OtherModuleProvider(dep, AidlLibraryProvider).(AidlLibraryInfo) |
| 154 | includeDirsDepSetBuilder.Transitive(&info.IncludeDirs) |
Vinh Tran | 0958195 | 2023-05-16 16:03:20 -0400 | [diff] [blame^] | 155 | hdrsDepSetBuilder.Transitive(&info.Hdrs) |
Vinh Tran | 0e7fd8a | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
Vinh Tran | 0e7fd8a | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 159 | ctx.SetProvider(AidlLibraryProvider, AidlLibraryInfo{ |
| 160 | Srcs: srcs, |
| 161 | IncludeDirs: *includeDirsDepSetBuilder.Build(), |
Vinh Tran | 0958195 | 2023-05-16 16:03:20 -0400 | [diff] [blame^] | 162 | Hdrs: *hdrsDepSetBuilder.Build(), |
Vinh Tran | 0e7fd8a | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 163 | }) |
| 164 | } |
| 165 | |
| 166 | // aidl_library contains a list of .aidl files and the strip_import_prefix to |
| 167 | // to strip from the paths of the .aidl files. The sub-path left-over after stripping |
| 168 | // corresponds to the aidl package path the aidl interfaces are scoped in |
| 169 | func AidlLibraryFactory() android.Module { |
| 170 | module := &AidlLibrary{} |
| 171 | module.AddProperties(&module.properties) |
| 172 | android.InitAndroidModule(module) |
Vinh Tran | 3d16990 | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 173 | android.InitBazelModule(module) |
Vinh Tran | 0e7fd8a | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 174 | return module |
| 175 | } |
| 176 | |
| 177 | type aidlDependencyTag struct { |
| 178 | blueprint.BaseDependencyTag |
| 179 | } |
| 180 | |
| 181 | var aidlLibraryTag = aidlDependencyTag{} |
| 182 | |
| 183 | func (lib *AidlLibrary) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 184 | for _, dep := range lib.properties.Deps { |
| 185 | ctx.AddDependency(lib, aidlLibraryTag, dep) |
| 186 | } |
| 187 | } |