blob: 5985103865bf4c24acdd62ee10b5c147e47fa747 [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
Vinh Tran09581952023-05-16 16:03:20 -0400110 // The include dirs to the direct aidl files and those provided from transitive aidl_library deps
Vinh Tran0e7fd8a2023-04-28 11:21:25 -0400111 IncludeDirs android.DepSet
Vinh Tran09581952023-05-16 16:03:20 -0400112 // The direct hdrs and hdrs from transitive deps
113 Hdrs android.DepSet
Vinh Tran0e7fd8a2023-04-28 11:21:25 -0400114}
115
116// AidlLibraryProvider provides the srcs and the transitive include dirs
117var AidlLibraryProvider = blueprint.NewProvider(AidlLibraryInfo{})
118
119func (lib *AidlLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
120 includeDirsDepSetBuilder := android.NewDepSetBuilder(android.PREORDER)
Vinh Tran09581952023-05-16 16:03:20 -0400121 hdrsDepSetBuilder := android.NewDepSetBuilder(android.PREORDER)
Vinh Tran0e7fd8a2023-04-28 11:21:25 -0400122
123 if len(lib.properties.Srcs) == 0 && len(lib.properties.Hdrs) == 0 {
124 ctx.ModuleErrorf("at least srcs or hdrs prop must be non-empty")
125 }
126
127 srcs := android.PathsForModuleSrc(ctx, lib.properties.Srcs)
Vinh Tran09581952023-05-16 16:03:20 -0400128 hdrs := android.PathsForModuleSrc(ctx, lib.properties.Hdrs)
129
Vinh Tran0e7fd8a2023-04-28 11:21:25 -0400130 if lib.properties.Strip_import_prefix != nil {
131 srcs = android.PathsWithModuleSrcSubDir(
132 ctx,
133 srcs,
Vinh Tran09581952023-05-16 16:03:20 -0400134 android.String(lib.properties.Strip_import_prefix),
135 )
136
137 hdrs = android.PathsWithModuleSrcSubDir(
138 ctx,
139 hdrs,
140 android.String(lib.properties.Strip_import_prefix),
141 )
Vinh Tran0e7fd8a2023-04-28 11:21:25 -0400142 }
Vinh Tran09581952023-05-16 16:03:20 -0400143 hdrsDepSetBuilder.Direct(hdrs...)
Vinh Tran0e7fd8a2023-04-28 11:21:25 -0400144
145 includeDir := android.PathForModuleSrc(
146 ctx,
147 proptools.StringDefault(lib.properties.Strip_import_prefix, ""),
148 )
Vinh Tran0e7fd8a2023-04-28 11:21:25 -0400149 includeDirsDepSetBuilder.Direct(includeDir)
150
151 for _, dep := range ctx.GetDirectDepsWithTag(aidlLibraryTag) {
152 if ctx.OtherModuleHasProvider(dep, AidlLibraryProvider) {
153 info := ctx.OtherModuleProvider(dep, AidlLibraryProvider).(AidlLibraryInfo)
154 includeDirsDepSetBuilder.Transitive(&info.IncludeDirs)
Vinh Tran09581952023-05-16 16:03:20 -0400155 hdrsDepSetBuilder.Transitive(&info.Hdrs)
Vinh Tran0e7fd8a2023-04-28 11:21:25 -0400156 }
157 }
158
Vinh Tran0e7fd8a2023-04-28 11:21:25 -0400159 ctx.SetProvider(AidlLibraryProvider, AidlLibraryInfo{
160 Srcs: srcs,
161 IncludeDirs: *includeDirsDepSetBuilder.Build(),
Vinh Tran09581952023-05-16 16:03:20 -0400162 Hdrs: *hdrsDepSetBuilder.Build(),
Vinh Tran0e7fd8a2023-04-28 11:21:25 -0400163 })
164}
165
166// aidl_library contains a list of .aidl files and the strip_import_prefix to
167// to strip from the paths of the .aidl files. The sub-path left-over after stripping
168// corresponds to the aidl package path the aidl interfaces are scoped in
169func AidlLibraryFactory() android.Module {
170 module := &AidlLibrary{}
171 module.AddProperties(&module.properties)
172 android.InitAndroidModule(module)
Vinh Tran3d169902023-04-28 11:21:25 -0400173 android.InitBazelModule(module)
Vinh Tran0e7fd8a2023-04-28 11:21:25 -0400174 return module
175}
176
177type aidlDependencyTag struct {
178 blueprint.BaseDependencyTag
179}
180
181var aidlLibraryTag = aidlDependencyTag{}
182
183func (lib *AidlLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
184 for _, dep := range lib.properties.Deps {
185 ctx.AddDependency(lib, aidlLibraryTag, dep)
186 }
187}