blob: c2f97d96cdbf1c62babd62e805a9a02c6d9b03e2 [file] [log] [blame]
Vinh Tran0e7fd8a2023-04-28 11:21:25 -04001// 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
15package aidl_library
16
17import (
18 "android/soong/android"
Vinh Tran0e7fd8a2023-04-28 11:21:25 -040019 "github.com/google/blueprint"
20 "github.com/google/blueprint/proptools"
21)
22
Vinh Tran367d89d2023-04-28 11:21:25 -040023var PrepareForTestWithAidlLibrary = android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) {
24 registerAidlLibraryBuildComponents(ctx)
25})
26
Vinh Tran0e7fd8a2023-04-28 11:21:25 -040027func init() {
28 registerAidlLibraryBuildComponents(android.InitRegistrationContext)
29}
30
31func registerAidlLibraryBuildComponents(ctx android.RegistrationContext) {
32 ctx.RegisterModuleType("aidl_library", AidlLibraryFactory)
33}
34
35type 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
52type AidlLibrary struct {
53 android.ModuleBase
54 properties aidlLibraryProperties
55}
56
57type AidlLibraryInfo struct {
58 // The direct aidl files of the module
59 Srcs android.Paths
Vinh Tran09581952023-05-16 16:03:20 -040060 // The include dirs to the direct aidl files and those provided from transitive aidl_library deps
Colin Crossc85750b2022-04-21 12:50:51 -070061 IncludeDirs android.DepSet[android.Path]
Vinh Tran09581952023-05-16 16:03:20 -040062 // The direct hdrs and hdrs from transitive deps
Colin Crossc85750b2022-04-21 12:50:51 -070063 Hdrs android.DepSet[android.Path]
Vinh Tran0e7fd8a2023-04-28 11:21:25 -040064}
65
66// AidlLibraryProvider provides the srcs and the transitive include dirs
Colin Crossbc7d76c2023-12-12 16:39:03 -080067var AidlLibraryProvider = blueprint.NewProvider[AidlLibraryInfo]()
Vinh Tran0e7fd8a2023-04-28 11:21:25 -040068
69func (lib *AidlLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossc85750b2022-04-21 12:50:51 -070070 includeDirsDepSetBuilder := android.NewDepSetBuilder[android.Path](android.PREORDER)
71 hdrsDepSetBuilder := android.NewDepSetBuilder[android.Path](android.PREORDER)
Vinh Tran0e7fd8a2023-04-28 11:21:25 -040072
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 Tran09581952023-05-16 16:03:20 -040078 hdrs := android.PathsForModuleSrc(ctx, lib.properties.Hdrs)
79
Vinh Tran0e7fd8a2023-04-28 11:21:25 -040080 if lib.properties.Strip_import_prefix != nil {
81 srcs = android.PathsWithModuleSrcSubDir(
82 ctx,
83 srcs,
Vinh Tran09581952023-05-16 16:03:20 -040084 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 Tran0e7fd8a2023-04-28 11:21:25 -040092 }
Vinh Tran09581952023-05-16 16:03:20 -040093 hdrsDepSetBuilder.Direct(hdrs...)
Vinh Tran0e7fd8a2023-04-28 11:21:25 -040094
95 includeDir := android.PathForModuleSrc(
96 ctx,
97 proptools.StringDefault(lib.properties.Strip_import_prefix, ""),
98 )
Vinh Tran0e7fd8a2023-04-28 11:21:25 -040099 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 Tran09581952023-05-16 16:03:20 -0400105 hdrsDepSetBuilder.Transitive(&info.Hdrs)
Vinh Tran0e7fd8a2023-04-28 11:21:25 -0400106 }
107 }
108
Vinh Tran0e7fd8a2023-04-28 11:21:25 -0400109 ctx.SetProvider(AidlLibraryProvider, AidlLibraryInfo{
110 Srcs: srcs,
111 IncludeDirs: *includeDirsDepSetBuilder.Build(),
Vinh Tran09581952023-05-16 16:03:20 -0400112 Hdrs: *hdrsDepSetBuilder.Build(),
Vinh Tran0e7fd8a2023-04-28 11:21:25 -0400113 })
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
119func AidlLibraryFactory() android.Module {
120 module := &AidlLibrary{}
121 module.AddProperties(&module.properties)
122 android.InitAndroidModule(module)
Vinh Tran0e7fd8a2023-04-28 11:21:25 -0400123 return module
124}
125
126type aidlDependencyTag struct {
127 blueprint.BaseDependencyTag
128}
129
130var aidlLibraryTag = aidlDependencyTag{}
131
132func (lib *AidlLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
133 for _, dep := range lib.properties.Deps {
134 ctx.AddDependency(lib, aidlLibraryTag, dep)
135 }
136}