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