blob: 1e0ab30568e912e53b1f45eac5c236f069f31c4c [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"
Colin Crossa14fb6a2024-10-23 16:57:06 -070020 "github.com/google/blueprint/depset"
Vinh Tran0e7fd8a2023-04-28 11:21:25 -040021 "github.com/google/blueprint/proptools"
22)
23
Vinh Tran367d89d2023-04-28 11:21:25 -040024var PrepareForTestWithAidlLibrary = android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) {
25 registerAidlLibraryBuildComponents(ctx)
26})
27
Vinh Tran0e7fd8a2023-04-28 11:21:25 -040028func init() {
29 registerAidlLibraryBuildComponents(android.InitRegistrationContext)
30}
31
32func registerAidlLibraryBuildComponents(ctx android.RegistrationContext) {
33 ctx.RegisterModuleType("aidl_library", AidlLibraryFactory)
34}
35
36type aidlLibraryProperties struct {
37 // srcs lists files that are included in this module for aidl compilation
38 Srcs []string `android:"path"`
39
40 // hdrs lists the headers that are imported by srcs but are not compiled by aidl to language binding code
41 // hdrs is provided to support Bazel migration. It is a no-op until
42 // we enable input sandbox in aidl compilation action
43 Hdrs []string `android:"path"`
44
45 // The prefix to strip from the paths of the .aidl files
46 // The remaining path is the package path of the aidl interface
47 Strip_import_prefix *string
48
49 // List of aidl files or aidl_library depended on by the module
50 Deps []string `android:"arch_variant"`
51}
52
53type AidlLibrary struct {
54 android.ModuleBase
55 properties aidlLibraryProperties
56}
57
58type AidlLibraryInfo struct {
59 // The direct aidl files of the module
60 Srcs android.Paths
Vinh Tran09581952023-05-16 16:03:20 -040061 // The include dirs to the direct aidl files and those provided from transitive aidl_library deps
Colin Crossa14fb6a2024-10-23 16:57:06 -070062 IncludeDirs depset.DepSet[android.Path]
Vinh Tran09581952023-05-16 16:03:20 -040063 // The direct hdrs and hdrs from transitive deps
Colin Crossa14fb6a2024-10-23 16:57:06 -070064 Hdrs depset.DepSet[android.Path]
Vinh Tran0e7fd8a2023-04-28 11:21:25 -040065}
66
67// AidlLibraryProvider provides the srcs and the transitive include dirs
Colin Crossbc7d76c2023-12-12 16:39:03 -080068var AidlLibraryProvider = blueprint.NewProvider[AidlLibraryInfo]()
Vinh Tran0e7fd8a2023-04-28 11:21:25 -040069
70func (lib *AidlLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossa14fb6a2024-10-23 16:57:06 -070071 includeDirsDepSetBuilder := depset.NewBuilder[android.Path](depset.PREORDER)
72 hdrsDepSetBuilder := depset.NewBuilder[android.Path](depset.PREORDER)
Vinh Tran0e7fd8a2023-04-28 11:21:25 -040073
74 if len(lib.properties.Srcs) == 0 && len(lib.properties.Hdrs) == 0 {
75 ctx.ModuleErrorf("at least srcs or hdrs prop must be non-empty")
76 }
77
78 srcs := android.PathsForModuleSrc(ctx, lib.properties.Srcs)
Vinh Tran09581952023-05-16 16:03:20 -040079 hdrs := android.PathsForModuleSrc(ctx, lib.properties.Hdrs)
80
Vinh Tran0e7fd8a2023-04-28 11:21:25 -040081 if lib.properties.Strip_import_prefix != nil {
82 srcs = android.PathsWithModuleSrcSubDir(
83 ctx,
84 srcs,
Vinh Tran09581952023-05-16 16:03:20 -040085 android.String(lib.properties.Strip_import_prefix),
86 )
87
88 hdrs = android.PathsWithModuleSrcSubDir(
89 ctx,
90 hdrs,
91 android.String(lib.properties.Strip_import_prefix),
92 )
Vinh Tran0e7fd8a2023-04-28 11:21:25 -040093 }
Vinh Tran09581952023-05-16 16:03:20 -040094 hdrsDepSetBuilder.Direct(hdrs...)
Vinh Tran0e7fd8a2023-04-28 11:21:25 -040095
96 includeDir := android.PathForModuleSrc(
97 ctx,
98 proptools.StringDefault(lib.properties.Strip_import_prefix, ""),
99 )
Vinh Tran0e7fd8a2023-04-28 11:21:25 -0400100 includeDirsDepSetBuilder.Direct(includeDir)
101
102 for _, dep := range ctx.GetDirectDepsWithTag(aidlLibraryTag) {
Colin Cross313aa542023-12-13 13:47:44 -0800103 if info, ok := android.OtherModuleProvider(ctx, dep, AidlLibraryProvider); ok {
Colin Crossa14fb6a2024-10-23 16:57:06 -0700104 includeDirsDepSetBuilder.Transitive(info.IncludeDirs)
105 hdrsDepSetBuilder.Transitive(info.Hdrs)
Vinh Tran0e7fd8a2023-04-28 11:21:25 -0400106 }
107 }
108
Colin Cross40213022023-12-13 15:19:49 -0800109 android.SetProvider(ctx, AidlLibraryProvider, AidlLibraryInfo{
Vinh Tran0e7fd8a2023-04-28 11:21:25 -0400110 Srcs: srcs,
Colin Crossa14fb6a2024-10-23 16:57:06 -0700111 IncludeDirs: includeDirsDepSetBuilder.Build(),
112 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}