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