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