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" |
| 19 | |
| 20 | "github.com/google/blueprint" |
| 21 | "github.com/google/blueprint/proptools" |
| 22 | ) |
| 23 | |
| 24 | func init() { |
| 25 | registerAidlLibraryBuildComponents(android.InitRegistrationContext) |
| 26 | } |
| 27 | |
| 28 | func registerAidlLibraryBuildComponents(ctx android.RegistrationContext) { |
| 29 | ctx.RegisterModuleType("aidl_library", AidlLibraryFactory) |
| 30 | } |
| 31 | |
| 32 | type aidlLibraryProperties struct { |
| 33 | // srcs lists files that are included in this module for aidl compilation |
| 34 | Srcs []string `android:"path"` |
| 35 | |
| 36 | // hdrs lists the headers that are imported by srcs but are not compiled by aidl to language binding code |
| 37 | // hdrs is provided to support Bazel migration. It is a no-op until |
| 38 | // we enable input sandbox in aidl compilation action |
| 39 | Hdrs []string `android:"path"` |
| 40 | |
| 41 | // The prefix to strip from the paths of the .aidl files |
| 42 | // The remaining path is the package path of the aidl interface |
| 43 | Strip_import_prefix *string |
| 44 | |
| 45 | // List of aidl files or aidl_library depended on by the module |
| 46 | Deps []string `android:"arch_variant"` |
| 47 | } |
| 48 | |
| 49 | type AidlLibrary struct { |
| 50 | android.ModuleBase |
| 51 | properties aidlLibraryProperties |
| 52 | } |
| 53 | |
| 54 | type AidlLibraryInfo struct { |
| 55 | // The direct aidl files of the module |
| 56 | Srcs android.Paths |
| 57 | // The include dirs to the direct aidl files and those provided from aidl_library deps |
| 58 | IncludeDirs android.DepSet |
| 59 | } |
| 60 | |
| 61 | // AidlLibraryProvider provides the srcs and the transitive include dirs |
| 62 | var AidlLibraryProvider = blueprint.NewProvider(AidlLibraryInfo{}) |
| 63 | |
| 64 | func (lib *AidlLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 65 | includeDirsDepSetBuilder := android.NewDepSetBuilder(android.PREORDER) |
| 66 | |
| 67 | if len(lib.properties.Srcs) == 0 && len(lib.properties.Hdrs) == 0 { |
| 68 | ctx.ModuleErrorf("at least srcs or hdrs prop must be non-empty") |
| 69 | } |
| 70 | |
| 71 | srcs := android.PathsForModuleSrc(ctx, lib.properties.Srcs) |
| 72 | if lib.properties.Strip_import_prefix != nil { |
| 73 | srcs = android.PathsWithModuleSrcSubDir( |
| 74 | ctx, |
| 75 | srcs, |
| 76 | android.String(lib.properties.Strip_import_prefix)) |
| 77 | } |
| 78 | |
| 79 | includeDir := android.PathForModuleSrc( |
| 80 | ctx, |
| 81 | proptools.StringDefault(lib.properties.Strip_import_prefix, ""), |
| 82 | ) |
| 83 | |
| 84 | includeDirsDepSetBuilder.Direct(includeDir) |
| 85 | |
| 86 | for _, dep := range ctx.GetDirectDepsWithTag(aidlLibraryTag) { |
| 87 | if ctx.OtherModuleHasProvider(dep, AidlLibraryProvider) { |
| 88 | info := ctx.OtherModuleProvider(dep, AidlLibraryProvider).(AidlLibraryInfo) |
| 89 | includeDirsDepSetBuilder.Transitive(&info.IncludeDirs) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // TODO(b/279960133) Propagate direct and transitive headers/srcs when aidl action sandboxes inputs |
| 94 | ctx.SetProvider(AidlLibraryProvider, AidlLibraryInfo{ |
| 95 | Srcs: srcs, |
| 96 | IncludeDirs: *includeDirsDepSetBuilder.Build(), |
| 97 | }) |
| 98 | } |
| 99 | |
| 100 | // aidl_library contains a list of .aidl files and the strip_import_prefix to |
| 101 | // to strip from the paths of the .aidl files. The sub-path left-over after stripping |
| 102 | // corresponds to the aidl package path the aidl interfaces are scoped in |
| 103 | func AidlLibraryFactory() android.Module { |
| 104 | module := &AidlLibrary{} |
| 105 | module.AddProperties(&module.properties) |
| 106 | android.InitAndroidModule(module) |
| 107 | return module |
| 108 | } |
| 109 | |
| 110 | type aidlDependencyTag struct { |
| 111 | blueprint.BaseDependencyTag |
| 112 | } |
| 113 | |
| 114 | var aidlLibraryTag = aidlDependencyTag{} |
| 115 | |
| 116 | func (lib *AidlLibrary) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 117 | for _, dep := range lib.properties.Deps { |
| 118 | ctx.AddDependency(lib, aidlLibraryTag, dep) |
| 119 | } |
| 120 | } |