xref: /aosp_15_r20/build/soong/aidl_library/aidl_library.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1// 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	"github.com/google/blueprint"
20	"github.com/google/blueprint/depset"
21	"github.com/google/blueprint/proptools"
22)
23
24var PrepareForTestWithAidlLibrary = android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) {
25	registerAidlLibraryBuildComponents(ctx)
26})
27
28func 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
61	// The include dirs to the direct aidl files and those provided from transitive aidl_library deps
62	IncludeDirs depset.DepSet[android.Path]
63	// The direct hdrs and hdrs from transitive deps
64	Hdrs depset.DepSet[android.Path]
65}
66
67// AidlLibraryProvider provides the srcs and the transitive include dirs
68var AidlLibraryProvider = blueprint.NewProvider[AidlLibraryInfo]()
69
70func (lib *AidlLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
71	includeDirsDepSetBuilder := depset.NewBuilder[android.Path](depset.PREORDER)
72	hdrsDepSetBuilder := depset.NewBuilder[android.Path](depset.PREORDER)
73
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)
79	hdrs := android.PathsForModuleSrc(ctx, lib.properties.Hdrs)
80
81	if lib.properties.Strip_import_prefix != nil {
82		srcs = android.PathsWithModuleSrcSubDir(
83			ctx,
84			srcs,
85			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		)
93	}
94	hdrsDepSetBuilder.Direct(hdrs...)
95
96	includeDir := android.PathForModuleSrc(
97		ctx,
98		proptools.StringDefault(lib.properties.Strip_import_prefix, ""),
99	)
100	includeDirsDepSetBuilder.Direct(includeDir)
101
102	for _, dep := range ctx.GetDirectDepsWithTag(aidlLibraryTag) {
103		if info, ok := android.OtherModuleProvider(ctx, dep, AidlLibraryProvider); ok {
104			includeDirsDepSetBuilder.Transitive(info.IncludeDirs)
105			hdrsDepSetBuilder.Transitive(info.Hdrs)
106		}
107	}
108
109	android.SetProvider(ctx, AidlLibraryProvider, AidlLibraryInfo{
110		Srcs:        srcs,
111		IncludeDirs: includeDirsDepSetBuilder.Build(),
112		Hdrs:        hdrsDepSetBuilder.Build(),
113	})
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)
123	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}
137