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