blob: f21431d9345514386c533faf08bee88463286dff [file] [log] [blame]
Jeongik Chada36d5a2021-01-20 00:43:34 +09001// Copyright (C) 2021 The Android Open Source Project
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
16
17import (
18 "android/soong/android"
19 "android/soong/cc"
20 "android/soong/java"
21 "android/soong/rust"
22
23 "path/filepath"
Jeongik Chada36d5a2021-01-20 00:43:34 +090024 "strings"
25
26 "github.com/google/blueprint/proptools"
27)
28
Jeongik Cha52e98022021-01-20 18:37:20 +090029func addLibrary(mctx android.LoadHookContext, i *aidlInterface, version string, lang string) string {
30 if lang == langJava {
31 return addJavaLibrary(mctx, i, version)
32 } else if lang == langRust {
33 return addRustLibrary(mctx, i, version)
34 }
35 return addCppLibrary(mctx, i, version, lang)
36}
37
38func addCppLibrary(mctx android.LoadHookContext, i *aidlInterface, version string, lang string) string {
39 cppSourceGen := i.versionedName(version) + "-" + lang + "-source"
40 cppModuleGen := i.versionedName(version) + "-" + lang
41
Jeongik Chada36d5a2021-01-20 00:43:34 +090042 srcs, aidlRoot := i.srcsForVersion(mctx, version)
43 if len(srcs) == 0 {
44 // This can happen when the version is about to be frozen; the version
45 // directory is created but API dump hasn't been copied there.
46 // Don't create a library for the yet-to-be-frozen version.
47 return ""
48 }
49
50 var overrideVndkProperties cc.VndkProperties
51
Jeongik Cha52e98022021-01-20 18:37:20 +090052 if !i.isModuleForVndk(version) {
Jeongik Chada36d5a2021-01-20 00:43:34 +090053 // We only want the VNDK to include the latest interface. For interfaces in
54 // development, they will be frozen, so we put their latest version in the
55 // VNDK. For interfaces which are already frozen, we put their latest version
56 // in the VNDK, and when that version is frozen, the version in the VNDK can
57 // be updated. Otherwise, we remove this library from the VNDK, to avoid adding
58 // multiple versions of the same library to the VNDK.
59 overrideVndkProperties.Vndk.Enabled = proptools.BoolPtr(false)
60 overrideVndkProperties.Vndk.Support_system_process = proptools.BoolPtr(false)
61 }
62
63 var commonProperties *CommonNativeBackendProperties
64 if lang == langCpp {
65 commonProperties = &i.properties.Backend.Cpp.CommonNativeBackendProperties
66 } else if lang == langNdk || lang == langNdkPlatform {
67 commonProperties = &i.properties.Backend.Ndk.CommonNativeBackendProperties
68 }
69
70 genLog := proptools.Bool(commonProperties.Gen_log)
71 genTrace := proptools.Bool(i.properties.Gen_trace)
72
73 mctx.CreateModule(aidlGenFactory, &nameProperties{
74 Name: proptools.StringPtr(cppSourceGen),
75 }, &aidlGenProperties{
76 Srcs: srcs,
77 AidlRoot: aidlRoot,
78 Imports: concat(i.properties.Imports, []string{i.ModuleBase.Name()}),
79 Stability: i.properties.Stability,
80 Lang: lang,
81 BaseName: i.ModuleBase.Name(),
82 GenLog: genLog,
Jeongik Cha52e98022021-01-20 18:37:20 +090083 Version: i.versionForAidlGenRule(version),
Jeongik Chada36d5a2021-01-20 00:43:34 +090084 GenTrace: genTrace,
85 Unstable: i.properties.Unstable,
86 Visibility: srcsVisibility(mctx, lang),
87 Flags: i.properties.Flags,
88 })
Jeongik Chada36d5a2021-01-20 00:43:34 +090089
Jeongik Cha52e98022021-01-20 18:37:20 +090090 importExportDependencies := []string{}
Jeongik Chada36d5a2021-01-20 00:43:34 +090091 var sharedLibDependency []string
92 var headerLibs []string
93 var sdkVersion *string
94 var minSdkVersion *string
95 var stl *string
96 var cpp_std *string
97 var hostSupported *bool
98 var addCflags []string
99
100 if lang == langCpp {
101 importExportDependencies = append(importExportDependencies, "libbinder", "libutils")
102 if genTrace {
103 sharedLibDependency = append(sharedLibDependency, "libcutils")
104 }
105 hostSupported = i.properties.Host_supported
106 minSdkVersion = i.properties.Backend.Cpp.Min_sdk_version
107 } else if lang == langNdk {
108 importExportDependencies = append(importExportDependencies, "libbinder_ndk")
109 if genTrace {
110 sharedLibDependency = append(sharedLibDependency, "libandroid")
111 }
112 sdkVersion = proptools.StringPtr("current")
113 stl = proptools.StringPtr("c++_shared")
114 minSdkVersion = i.properties.Backend.Ndk.Min_sdk_version
115 } else if lang == langNdkPlatform {
116 importExportDependencies = append(importExportDependencies, "libbinder_ndk")
117 if genTrace {
118 headerLibs = append(headerLibs, "libandroid_aidltrace")
119 sharedLibDependency = append(sharedLibDependency, "libcutils")
120 }
121 hostSupported = i.properties.Host_supported
122 addCflags = append(addCflags, "-DBINDER_STABILITY_SUPPORT")
123 minSdkVersion = i.properties.Backend.Ndk.Min_sdk_version
124 } else {
125 panic("Unrecognized language: " + lang)
126 }
127
128 vendorAvailable := i.properties.Vendor_available
129 productAvailable := i.properties.Product_available
130 if lang == langCpp {
131 // Vendor and product modules cannot use the libbinder (cpp) backend of AIDL in a
132 // way that is stable. So, in order to prevent accidental usage of these library by
133 // vendor and product forcibly disabling this version of the library.
134 //
135 // It may be the case in the future that we will want to enable this (if some generic
136 // helper should be used by both libbinder vendor things using /dev/vndbinder as well
137 // as those things using /dev/binder + libbinder_ndk to talk to stable interfaces).
138 if "vintf" == proptools.String(i.properties.Stability) {
139 overrideVndkProperties.Vndk.Private = proptools.BoolPtr(true)
140 }
141 // As libbinder is not available for the product processes, we must not create
142 // product variant for the aidl_interface
143 productAvailable = nil
144 }
145
146 if lang == langNdk {
147 // TODO(b/121157555): when the NDK variant is its own variant, these wouldn't interact,
148 // but we can't create a vendor or product version of an NDK variant
149 //
150 // nil (unspecified) is used instead of false so that this can't conflict with
151 // 'vendor: true', for instance.
152 vendorAvailable = nil
153 productAvailable = nil
154 overrideVndkProperties.Vndk.Enabled = proptools.BoolPtr(false)
155 overrideVndkProperties.Vndk.Support_system_process = proptools.BoolPtr(false)
156 }
157
Jeongik Cha52e98022021-01-20 18:37:20 +0900158 mctx.CreateModule(aidlImplementationGeneratorFactory, &nameProperties{
159 Name: proptools.StringPtr(cppModuleGen + "-generator"),
160 }, &aidlImplementationGeneratorProperties{
161 Lang: lang,
162 AidlInterfaceName: i.ModuleBase.Name(),
163 Version: version,
164 ModuleProperties: []interface{}{
165 &ccProperties{
166 Name: proptools.StringPtr(cppModuleGen),
167 Vendor_available: vendorAvailable,
168 Product_available: productAvailable,
169 Host_supported: hostSupported,
170 Defaults: []string{"aidl-cpp-module-defaults"},
171 Double_loadable: i.properties.Double_loadable,
172 Generated_sources: []string{cppSourceGen},
173 Generated_headers: []string{cppSourceGen},
174 Export_generated_headers: []string{cppSourceGen},
175 Shared_libs: append(importExportDependencies, sharedLibDependency...),
176 Header_libs: headerLibs,
177 Export_shared_lib_headers: importExportDependencies,
178 Sdk_version: sdkVersion,
179 Stl: stl,
180 Cpp_std: cpp_std,
181 Cflags: append(addCflags, "-Wextra", "-Wall", "-Werror", "-Wextra-semi"),
182 Apex_available: commonProperties.Apex_available,
183 Min_sdk_version: minSdkVersion,
184 UseApexNameMacro: true,
185 Target: ccTargetProperties{
186 // Currently necessary for host builds
187 // TODO(b/31559095): bionic on host should define this
188 // TODO(b/146436251): default isn't applied because the module is created
189 // in PreArchMutators, when import behavior becomes explicit, the logic can
190 // be moved back to LoadHook
191 Host: hostProperties{Cflags: []string{
192 "-D__INTRODUCED_IN(n)=",
193 "-D__assert(a,b,c)=",
194 // We want all the APIs to be available on the host.
195 "-D__ANDROID_API__=10000"}},
196 Darwin: perTargetProperties{Enabled: proptools.BoolPtr(false)}},
197 Tidy: proptools.BoolPtr(true),
198 // Do the tidy check only for the generated headers
199 Tidy_flags: []string{"--header-filter=" + android.PathForOutput(mctx).String() + ".*"},
200 Tidy_checks_as_errors: []string{"*"},
201 }, &i.properties.VndkProperties,
202 &commonProperties.VndkProperties,
203 &overrideVndkProperties,
204 },
205 })
Jeongik Chada36d5a2021-01-20 00:43:34 +0900206
207 return cppModuleGen
208}
209
Jeongik Cha52e98022021-01-20 18:37:20 +0900210func addJavaLibrary(mctx android.LoadHookContext, i *aidlInterface, version string) string {
211 javaSourceGen := i.versionedName(version) + "-java-source"
212 javaModuleGen := i.versionedName(version) + "-java"
Jeongik Chada36d5a2021-01-20 00:43:34 +0900213 srcs, aidlRoot := i.srcsForVersion(mctx, version)
214 if len(srcs) == 0 {
215 // This can happen when the version is about to be frozen; the version
216 // directory is created but API dump hasn't been copied there.
217 // Don't create a library for the yet-to-be-frozen version.
218 return ""
219 }
220
221 sdkVersion := i.properties.Backend.Java.Sdk_version
222 if !proptools.Bool(i.properties.Backend.Java.Platform_apis) && sdkVersion == nil {
223 // platform apis requires no default
224 sdkVersion = proptools.StringPtr("system_current")
225 }
226
227 mctx.CreateModule(aidlGenFactory, &nameProperties{
228 Name: proptools.StringPtr(javaSourceGen),
229 }, &aidlGenProperties{
230 Srcs: srcs,
231 AidlRoot: aidlRoot,
232 Imports: concat(i.properties.Imports, []string{i.ModuleBase.Name()}),
233 Stability: i.properties.Stability,
234 Lang: langJava,
235 BaseName: i.ModuleBase.Name(),
Jeongik Cha52e98022021-01-20 18:37:20 +0900236 Version: i.versionForAidlGenRule(version),
Jeongik Chada36d5a2021-01-20 00:43:34 +0900237 GenTrace: proptools.Bool(i.properties.Gen_trace),
238 Unstable: i.properties.Unstable,
239 Visibility: srcsVisibility(mctx, langJava),
240 Flags: i.properties.Flags,
241 })
242
Jeongik Cha52e98022021-01-20 18:37:20 +0900243 mctx.CreateModule(aidlImplementationGeneratorFactory, &nameProperties{
244 Name: proptools.StringPtr(javaModuleGen + "-generator"),
245 }, &aidlImplementationGeneratorProperties{
246 Lang: langJava,
247 AidlInterfaceName: i.ModuleBase.Name(),
248 Version: version,
249 ModuleProperties: []interface{}{&javaProperties{
250 Name: proptools.StringPtr(javaModuleGen),
251 Installable: proptools.BoolPtr(true),
252 Defaults: []string{"aidl-java-module-defaults"},
253 Sdk_version: sdkVersion,
254 Platform_apis: i.properties.Backend.Java.Platform_apis,
255 Srcs: []string{":" + javaSourceGen},
256 Apex_available: i.properties.Backend.Java.Apex_available,
257 Min_sdk_version: i.properties.Backend.Java.Min_sdk_version,
258 }},
Jeongik Chada36d5a2021-01-20 00:43:34 +0900259 })
260
261 return javaModuleGen
262}
263
Jeongik Cha52e98022021-01-20 18:37:20 +0900264func addRustLibrary(mctx android.LoadHookContext, i *aidlInterface, version string) string {
265 rustSourceGen := i.versionedName(version) + "-rust-source"
266 rustModuleGen := i.versionedName(version) + "-rust"
Jeongik Chada36d5a2021-01-20 00:43:34 +0900267 srcs, aidlRoot := i.srcsForVersion(mctx, version)
268 if len(srcs) == 0 {
269 // This can happen when the version is about to be frozen; the version
270 // directory is created but API dump hasn't been copied there.
271 // Don't create a library for the yet-to-be-frozen version.
272 return ""
273 }
274
275 mctx.CreateModule(aidlGenFactory, &nameProperties{
276 Name: proptools.StringPtr(rustSourceGen),
277 }, &aidlGenProperties{
278 Srcs: srcs,
279 AidlRoot: aidlRoot,
280 Imports: concat(i.properties.Imports, []string{i.ModuleBase.Name()}),
281 Stability: i.properties.Stability,
282 Lang: langRust,
283 BaseName: i.ModuleBase.Name(),
Jeongik Cha52e98022021-01-20 18:37:20 +0900284 Version: i.versionForAidlGenRule(version),
Jeongik Chada36d5a2021-01-20 00:43:34 +0900285 Unstable: i.properties.Unstable,
286 Visibility: srcsVisibility(mctx, langRust),
287 Flags: i.properties.Flags,
288 })
289
Jeongik Cha52e98022021-01-20 18:37:20 +0900290 versionedRustName := fixRustName(i.versionedName(version))
291 rustCrateName := fixRustName(i.ModuleBase.Name())
Jeongik Chada36d5a2021-01-20 00:43:34 +0900292
293 mctx.CreateModule(aidlRustLibraryFactory, &rustProperties{
294 Name: proptools.StringPtr(rustModuleGen),
Jeongik Cha52e98022021-01-20 18:37:20 +0900295 Crate_name: rustCrateName,
Jeongik Chada36d5a2021-01-20 00:43:34 +0900296 Stem: proptools.StringPtr("lib" + versionedRustName),
297 Defaults: []string{"aidl-rust-module-defaults"},
298 Host_supported: i.properties.Host_supported,
Jeongik Cha52e98022021-01-20 18:37:20 +0900299 Target: rustTargetProperties{Darwin: perTargetProperties{Enabled: proptools.BoolPtr(false)}},
Jeongik Chada36d5a2021-01-20 00:43:34 +0900300 }, &rust.SourceProviderProperties{
301 Source_stem: proptools.StringPtr(versionedRustName),
302 }, &aidlRustSourceProviderProperties{
Jeongik Cha52e98022021-01-20 18:37:20 +0900303 SourceGen: rustSourceGen,
304 Imports: i.properties.Imports,
305 Version: version,
306 AidlInterfaceName: i.ModuleBase.Name(),
Jeongik Chada36d5a2021-01-20 00:43:34 +0900307 })
308
309 return rustModuleGen
310}
311
312// This function returns module name with version. Assume that there is foo of which latest version is 2
313// Version -> Module name
314// "1"->foo-V1
315// "2"->foo-V2
Jeongik Cha52e98022021-01-20 18:37:20 +0900316// "3"->foo-V3
317// And assume that there is 'bar' which is an 'unstable' interface.
318// ""->bar
Jeongik Chada36d5a2021-01-20 00:43:34 +0900319func (i *aidlInterface) versionedName(version string) string {
320 name := i.ModuleBase.Name()
Jeongik Cha52e98022021-01-20 18:37:20 +0900321 // TODO(b/150578172) remove exception when every module specify its version.
Jeongik Chada36d5a2021-01-20 00:43:34 +0900322 if version == "" {
323 return name
Jeongik Cha52e98022021-01-20 18:37:20 +0900324 } else if version == unstableVersion {
Jeongik Chada36d5a2021-01-20 00:43:34 +0900325 return name + "-" + unstableVersion
326 }
327 return name + "-V" + version
328}
329
Jeongik Chada36d5a2021-01-20 00:43:34 +0900330func (i *aidlInterface) srcsForVersion(mctx android.LoadHookContext, version string) (srcs []string, aidlRoot string) {
Jeongik Cha52e98022021-01-20 18:37:20 +0900331 // TODO(b/150578172) remove exception when every module specify its version.
332 version = i.normalizeVersion(version)
333 if version == i.nextVersion() {
Jeongik Chada36d5a2021-01-20 00:43:34 +0900334 return i.properties.Srcs, i.properties.Local_include_dir
335 } else {
336 aidlRoot = filepath.Join(aidlApiDir, i.ModuleBase.Name(), version)
337 full_paths, err := mctx.GlobWithDeps(filepath.Join(mctx.ModuleDir(), aidlRoot, "**/*.aidl"), nil)
338 if err != nil {
339 panic(err)
340 }
341 for _, path := range full_paths {
342 // Here, we need path local to the module
343 srcs = append(srcs, strings.TrimPrefix(path, mctx.ModuleDir()+"/"))
344 }
345 return srcs, aidlRoot
346 }
347}
Jeongik Cha52e98022021-01-20 18:37:20 +0900348
349func (i *aidlInterface) versionForAidlGenRule(version string) string {
350 if !i.hasVersion() {
351 return ""
352 }
353 // TODO(b/150578172) remove exception when every module specify its version.
354 if version == "" {
355 return i.latestVersion()
356 }
357 if version == unstableVersion {
358 return i.nextVersion()
359 }
360 return version
361}
362
363func (i *aidlInterface) isModuleForVndk(version string) bool {
364 // TODO(b/150578172) remove exception when every module specify its version.
365 version = i.normalizeVersion(version)
366
367 if i.properties.Vndk_use_version != nil {
368 if !i.hasVersion() {
369 panic("does not make sense, vndk_use_version specififed")
370 }
371 // Will be exactly one of the version numbers
372 return version == *i.properties.Vndk_use_version
373 }
374
375 // For an interface with no versions, this is the ToT interface.
376 if !i.hasVersion() {
377 return version == i.nextVersion()
378 }
379
380 return version == i.latestVersion()
381}
382
383// importing aidl_interface's version | imported aidl_interface | imported aidl_interface's version
384// --------------------------------------------------------------------------------------------------
385// whatever | unstable | unstable version
386// ToT version(including unstable) | whatever | ToT version(unstable if unstable)
387// otherwise | whatever | the latest stable version
388// TODO(b/146436251) Make import field specify the explicit version which it wants to import.
389func (i *aidlInterface) getImportWithVersion(version string, anImport string, config android.Config) string {
390 // TODO(b/150578172) remove exception when every module specify its version.
391 version = i.normalizeVersion(version)
392
393 other := lookupInterface(anImport, config)
394 if proptools.Bool(other.properties.Unstable) {
395 return anImport
396 }
397 if version == i.nextVersion() || !other.hasVersion() {
398 return other.versionedName(other.nextVersion())
399 }
400 return other.versionedName(other.latestVersion())
401}
402
403// TODO(b/150578172) remove exception when every module specify its version.
404func (i *aidlInterface) normalizeVersion(version string) string {
405 if version == "" {
406 if i.hasVersion() {
407 return i.latestVersion()
408 } else if !proptools.Bool(i.properties.Unstable) {
409 return i.nextVersion()
410 }
411 } else if version == unstableVersion {
412 return i.nextVersion()
413 }
414 return version
415}
416
417func aidlImplementationGeneratorFactory() android.Module {
418 g := &aidlImplementationGenerator{}
419 g.AddProperties(&g.properties)
420 android.InitAndroidModule(g)
421 return g
422}
423
424type aidlImplementationGenerator struct {
425 android.ModuleBase
426 properties aidlImplementationGeneratorProperties
427}
428
429type aidlImplementationGeneratorProperties struct {
430 Lang string
431 AidlInterfaceName string
432 Version string
433 ModuleProperties []interface{}
434}
435
436func (g *aidlImplementationGenerator) DepsMutator(ctx android.BottomUpMutatorContext) {
437}
438
439func (g *aidlImplementationGenerator) GenerateAndroidBuildActions(ctx android.ModuleContext) {
440}
441
442func (g *aidlImplementationGenerator) GenerateImplementation(ctx android.TopDownMutatorContext) {
443 i := lookupInterface(g.properties.AidlInterfaceName, ctx.Config())
444 version := g.properties.Version
445 lang := g.properties.Lang
446 if g.properties.Lang == langJava {
447 imports := make([]string, len(i.properties.Imports))
448 for idx, anImport := range i.properties.Imports {
449 imports[idx] = i.getImportWithVersion(version, anImport, ctx.Config()) + "-" + langJava
450 }
451 if p, ok := g.properties.ModuleProperties[0].(*javaProperties); ok {
452 p.Static_libs = imports
453 }
454 ctx.CreateModule(java.LibraryFactory, g.properties.ModuleProperties...)
455 } else {
456 imports := make([]string, len(i.properties.Imports))
457 for idx, anImport := range i.properties.Imports {
458 imports[idx] = i.getImportWithVersion(version, anImport, ctx.Config()) + "-" + lang
459 }
460 if p, ok := g.properties.ModuleProperties[0].(*ccProperties); ok {
461 p.Shared_libs = append(p.Shared_libs, imports...)
462 p.Export_shared_lib_headers = append(p.Export_shared_lib_headers, imports...)
463 }
464 ctx.CreateModule(cc.LibraryFactory, g.properties.ModuleProperties...)
465 }
466}