blob: 2a42170f238efa4307169dc673f4495cb78ea407 [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"
19
20 "github.com/google/blueprint"
21 "github.com/google/blueprint/proptools"
22)
23
24func init() {
25 registerAidlLibraryBuildComponents(android.InitRegistrationContext)
26}
27
28func registerAidlLibraryBuildComponents(ctx android.RegistrationContext) {
29 ctx.RegisterModuleType("aidl_library", AidlLibraryFactory)
30}
31
32type aidlLibraryProperties struct {
33 // srcs lists files that are included in this module for aidl compilation
34 Srcs []string `android:"path"`
35
36 // hdrs lists the headers that are imported by srcs but are not compiled by aidl to language binding code
37 // hdrs is provided to support Bazel migration. It is a no-op until
38 // we enable input sandbox in aidl compilation action
39 Hdrs []string `android:"path"`
40
41 // The prefix to strip from the paths of the .aidl files
42 // The remaining path is the package path of the aidl interface
43 Strip_import_prefix *string
44
45 // List of aidl files or aidl_library depended on by the module
46 Deps []string `android:"arch_variant"`
47}
48
49type AidlLibrary struct {
50 android.ModuleBase
51 properties aidlLibraryProperties
52}
53
54type AidlLibraryInfo struct {
55 // The direct aidl files of the module
56 Srcs android.Paths
57 // The include dirs to the direct aidl files and those provided from aidl_library deps
58 IncludeDirs android.DepSet
59}
60
61// AidlLibraryProvider provides the srcs and the transitive include dirs
62var AidlLibraryProvider = blueprint.NewProvider(AidlLibraryInfo{})
63
64func (lib *AidlLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
65 includeDirsDepSetBuilder := android.NewDepSetBuilder(android.PREORDER)
66
67 if len(lib.properties.Srcs) == 0 && len(lib.properties.Hdrs) == 0 {
68 ctx.ModuleErrorf("at least srcs or hdrs prop must be non-empty")
69 }
70
71 srcs := android.PathsForModuleSrc(ctx, lib.properties.Srcs)
72 if lib.properties.Strip_import_prefix != nil {
73 srcs = android.PathsWithModuleSrcSubDir(
74 ctx,
75 srcs,
76 android.String(lib.properties.Strip_import_prefix))
77 }
78
79 includeDir := android.PathForModuleSrc(
80 ctx,
81 proptools.StringDefault(lib.properties.Strip_import_prefix, ""),
82 )
83
84 includeDirsDepSetBuilder.Direct(includeDir)
85
86 for _, dep := range ctx.GetDirectDepsWithTag(aidlLibraryTag) {
87 if ctx.OtherModuleHasProvider(dep, AidlLibraryProvider) {
88 info := ctx.OtherModuleProvider(dep, AidlLibraryProvider).(AidlLibraryInfo)
89 includeDirsDepSetBuilder.Transitive(&info.IncludeDirs)
90 }
91 }
92
93 // TODO(b/279960133) Propagate direct and transitive headers/srcs when aidl action sandboxes inputs
94 ctx.SetProvider(AidlLibraryProvider, AidlLibraryInfo{
95 Srcs: srcs,
96 IncludeDirs: *includeDirsDepSetBuilder.Build(),
97 })
98}
99
100// aidl_library contains a list of .aidl files and the strip_import_prefix to
101// to strip from the paths of the .aidl files. The sub-path left-over after stripping
102// corresponds to the aidl package path the aidl interfaces are scoped in
103func AidlLibraryFactory() android.Module {
104 module := &AidlLibrary{}
105 module.AddProperties(&module.properties)
106 android.InitAndroidModule(module)
107 return module
108}
109
110type aidlDependencyTag struct {
111 blueprint.BaseDependencyTag
112}
113
114var aidlLibraryTag = aidlDependencyTag{}
115
116func (lib *AidlLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
117 for _, dep := range lib.properties.Deps {
118 ctx.AddDependency(lib, aidlLibraryTag, dep)
119 }
120}