• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2018 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 java
16
17import (
18	"fmt"
19	"path"
20	"path/filepath"
21	"reflect"
22	"regexp"
23	"sort"
24	"strings"
25	"sync"
26
27	"github.com/google/blueprint"
28	"github.com/google/blueprint/proptools"
29
30	"android/soong/android"
31)
32
33const (
34	sdkXmlFileSuffix    = ".xml"
35	permissionsTemplate = `<?xml version=\"1.0\" encoding=\"utf-8\"?>\n` +
36		`<!-- Copyright (C) 2018 The Android Open Source Project\n` +
37		`\n` +
38		`    Licensed under the Apache License, Version 2.0 (the \"License\");\n` +
39		`    you may not use this file except in compliance with the License.\n` +
40		`    You may obtain a copy of the License at\n` +
41		`\n` +
42		`        http://www.apache.org/licenses/LICENSE-2.0\n` +
43		`\n` +
44		`    Unless required by applicable law or agreed to in writing, software\n` +
45		`    distributed under the License is distributed on an \"AS IS\" BASIS,\n` +
46		`    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n` +
47		`    See the License for the specific language governing permissions and\n` +
48		`    limitations under the License.\n` +
49		`-->\n` +
50		`<permissions>\n` +
51		`    <library name=\"%s\" file=\"%s\"/>\n` +
52		`</permissions>\n`
53)
54
55// A tag to associated a dependency with a specific api scope.
56type scopeDependencyTag struct {
57	blueprint.BaseDependencyTag
58	name     string
59	apiScope *apiScope
60
61	// Function for extracting appropriate path information from the dependency.
62	depInfoExtractor func(paths *scopePaths, dep android.Module) error
63}
64
65// Extract tag specific information from the dependency.
66func (tag scopeDependencyTag) extractDepInfo(ctx android.ModuleContext, dep android.Module, paths *scopePaths) {
67	err := tag.depInfoExtractor(paths, dep)
68	if err != nil {
69		ctx.ModuleErrorf("has an invalid {scopeDependencyTag: %s} dependency on module %s: %s", tag.name, ctx.OtherModuleName(dep), err.Error())
70	}
71}
72
73// Provides information about an api scope, e.g. public, system, test.
74type apiScope struct {
75	// The name of the api scope, e.g. public, system, test
76	name string
77
78	// The api scope that this scope extends.
79	extends *apiScope
80
81	// The legacy enabled status for a specific scope can be dependent on other
82	// properties that have been specified on the library so it is provided by
83	// a function that can determine the status by examining those properties.
84	legacyEnabledStatus func(module *SdkLibrary) bool
85
86	// The default enabled status for non-legacy behavior, which is triggered by
87	// explicitly enabling at least one api scope.
88	defaultEnabledStatus bool
89
90	// Gets a pointer to the scope specific properties.
91	scopeSpecificProperties func(module *SdkLibrary) *ApiScopeProperties
92
93	// The name of the field in the dynamically created structure.
94	fieldName string
95
96	// The name of the property in the java_sdk_library_import
97	propertyName string
98
99	// The tag to use to depend on the stubs library module.
100	stubsTag scopeDependencyTag
101
102	// The tag to use to depend on the stubs source module (if separate from the API module).
103	stubsSourceTag scopeDependencyTag
104
105	// The tag to use to depend on the API file generating module (if separate from the stubs source module).
106	apiFileTag scopeDependencyTag
107
108	// The tag to use to depend on the stubs source and API module.
109	stubsSourceAndApiTag scopeDependencyTag
110
111	// The scope specific prefix to add to the api file base of "current.txt" or "removed.txt".
112	apiFilePrefix string
113
114	// The scope specific prefix to add to the sdk library module name to construct a scope specific
115	// module name.
116	moduleSuffix string
117
118	// SDK version that the stubs library is built against. Note that this is always
119	// *current. Older stubs library built with a numbered SDK version is created from
120	// the prebuilt jar.
121	sdkVersion string
122
123	// Extra arguments to pass to droidstubs for this scope.
124	droidstubsArgs []string
125
126	// The args that must be passed to droidstubs to generate the stubs source
127	// for this scope.
128	//
129	// The stubs source must include the definitions of everything that is in this
130	// api scope and all the scopes that this one extends.
131	droidstubsArgsForGeneratingStubsSource []string
132
133	// The args that must be passed to droidstubs to generate the API for this scope.
134	//
135	// The API only includes the additional members that this scope adds over the scope
136	// that it extends.
137	droidstubsArgsForGeneratingApi []string
138
139	// True if the stubs source and api can be created by the same metalava invocation.
140	createStubsSourceAndApiTogether bool
141
142	// Whether the api scope can be treated as unstable, and should skip compat checks.
143	unstable bool
144}
145
146// Initialize a scope, creating and adding appropriate dependency tags
147func initApiScope(scope *apiScope) *apiScope {
148	name := scope.name
149	scopeByName[name] = scope
150	allScopeNames = append(allScopeNames, name)
151	scope.propertyName = strings.ReplaceAll(name, "-", "_")
152	scope.fieldName = proptools.FieldNameForProperty(scope.propertyName)
153	scope.stubsTag = scopeDependencyTag{
154		name:             name + "-stubs",
155		apiScope:         scope,
156		depInfoExtractor: (*scopePaths).extractStubsLibraryInfoFromDependency,
157	}
158	scope.stubsSourceTag = scopeDependencyTag{
159		name:             name + "-stubs-source",
160		apiScope:         scope,
161		depInfoExtractor: (*scopePaths).extractStubsSourceInfoFromDep,
162	}
163	scope.apiFileTag = scopeDependencyTag{
164		name:             name + "-api",
165		apiScope:         scope,
166		depInfoExtractor: (*scopePaths).extractApiInfoFromDep,
167	}
168	scope.stubsSourceAndApiTag = scopeDependencyTag{
169		name:             name + "-stubs-source-and-api",
170		apiScope:         scope,
171		depInfoExtractor: (*scopePaths).extractStubsSourceAndApiInfoFromApiStubsProvider,
172	}
173
174	// To get the args needed to generate the stubs source append all the args from
175	// this scope and all the scopes it extends as each set of args adds additional
176	// members to the stubs.
177	var stubsSourceArgs []string
178	for s := scope; s != nil; s = s.extends {
179		stubsSourceArgs = append(stubsSourceArgs, s.droidstubsArgs...)
180	}
181	scope.droidstubsArgsForGeneratingStubsSource = stubsSourceArgs
182
183	// Currently the args needed to generate the API are the same as the args
184	// needed to add additional members.
185	apiArgs := scope.droidstubsArgs
186	scope.droidstubsArgsForGeneratingApi = apiArgs
187
188	// If the args needed to generate the stubs and API are the same then they
189	// can be generated in a single invocation of metalava, otherwise they will
190	// need separate invocations.
191	scope.createStubsSourceAndApiTogether = reflect.DeepEqual(stubsSourceArgs, apiArgs)
192
193	return scope
194}
195
196func (scope *apiScope) stubsLibraryModuleName(baseName string) string {
197	return baseName + ".stubs" + scope.moduleSuffix
198}
199
200func (scope *apiScope) stubsSourceModuleName(baseName string) string {
201	return baseName + ".stubs.source" + scope.moduleSuffix
202}
203
204func (scope *apiScope) apiModuleName(baseName string) string {
205	return baseName + ".api" + scope.moduleSuffix
206}
207
208func (scope *apiScope) String() string {
209	return scope.name
210}
211
212type apiScopes []*apiScope
213
214func (scopes apiScopes) Strings(accessor func(*apiScope) string) []string {
215	var list []string
216	for _, scope := range scopes {
217		list = append(list, accessor(scope))
218	}
219	return list
220}
221
222var (
223	scopeByName    = make(map[string]*apiScope)
224	allScopeNames  []string
225	apiScopePublic = initApiScope(&apiScope{
226		name: "public",
227
228		// Public scope is enabled by default for both legacy and non-legacy modes.
229		legacyEnabledStatus: func(module *SdkLibrary) bool {
230			return true
231		},
232		defaultEnabledStatus: true,
233
234		scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
235			return &module.sdkLibraryProperties.Public
236		},
237		sdkVersion: "current",
238	})
239	apiScopeSystem = initApiScope(&apiScope{
240		name:                "system",
241		extends:             apiScopePublic,
242		legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
243		scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
244			return &module.sdkLibraryProperties.System
245		},
246		apiFilePrefix:  "system-",
247		moduleSuffix:   ".system",
248		sdkVersion:     "system_current",
249		droidstubsArgs: []string{"-showAnnotation android.annotation.SystemApi\\(client=android.annotation.SystemApi.Client.PRIVILEGED_APPS\\)"},
250	})
251	apiScopeTest = initApiScope(&apiScope{
252		name:                "test",
253		extends:             apiScopePublic,
254		legacyEnabledStatus: (*SdkLibrary).generateTestAndSystemScopesByDefault,
255		scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
256			return &module.sdkLibraryProperties.Test
257		},
258		apiFilePrefix:  "test-",
259		moduleSuffix:   ".test",
260		sdkVersion:     "test_current",
261		droidstubsArgs: []string{"-showAnnotation android.annotation.TestApi"},
262		unstable:       true,
263	})
264	apiScopeModuleLib = initApiScope(&apiScope{
265		name:    "module-lib",
266		extends: apiScopeSystem,
267		// The module-lib scope is disabled by default in legacy mode.
268		//
269		// Enabling this would break existing usages.
270		legacyEnabledStatus: func(module *SdkLibrary) bool {
271			return false
272		},
273		scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
274			return &module.sdkLibraryProperties.Module_lib
275		},
276		apiFilePrefix: "module-lib-",
277		moduleSuffix:  ".module_lib",
278		sdkVersion:    "module_current",
279		droidstubsArgs: []string{
280			"--show-annotation android.annotation.SystemApi\\(client=android.annotation.SystemApi.Client.MODULE_LIBRARIES\\)",
281		},
282	})
283	apiScopeSystemServer = initApiScope(&apiScope{
284		name:    "system-server",
285		extends: apiScopePublic,
286		// The system-server scope is disabled by default in legacy mode.
287		//
288		// Enabling this would break existing usages.
289		legacyEnabledStatus: func(module *SdkLibrary) bool {
290			return false
291		},
292		scopeSpecificProperties: func(module *SdkLibrary) *ApiScopeProperties {
293			return &module.sdkLibraryProperties.System_server
294		},
295		apiFilePrefix: "system-server-",
296		moduleSuffix:  ".system_server",
297		sdkVersion:    "system_server_current",
298		droidstubsArgs: []string{
299			"--show-annotation android.annotation.SystemApi\\(client=android.annotation.SystemApi.Client.SYSTEM_SERVER\\) ",
300			"--hide-annotation android.annotation.Hide",
301			// com.android.* classes are okay in this interface"
302			"--hide InternalClasses",
303		},
304	})
305	allApiScopes = apiScopes{
306		apiScopePublic,
307		apiScopeSystem,
308		apiScopeTest,
309		apiScopeModuleLib,
310		apiScopeSystemServer,
311	}
312)
313
314var (
315	javaSdkLibrariesLock sync.Mutex
316)
317
318// TODO: these are big features that are currently missing
319// 1) disallowing linking to the runtime shared lib
320// 2) HTML generation
321
322func init() {
323	RegisterSdkLibraryBuildComponents(android.InitRegistrationContext)
324
325	android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
326		javaSdkLibraries := javaSdkLibraries(ctx.Config())
327		sort.Strings(*javaSdkLibraries)
328		ctx.Strict("JAVA_SDK_LIBRARIES", strings.Join(*javaSdkLibraries, " "))
329	})
330
331	// Register sdk member types.
332	android.RegisterSdkMemberType(&sdkLibrarySdkMemberType{
333		android.SdkMemberTypeBase{
334			PropertyName: "java_sdk_libs",
335			SupportsSdk:  true,
336		},
337	})
338}
339
340func RegisterSdkLibraryBuildComponents(ctx android.RegistrationContext) {
341	ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
342	ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
343}
344
345// Properties associated with each api scope.
346type ApiScopeProperties struct {
347	// Indicates whether the api surface is generated.
348	//
349	// If this is set for any scope then all scopes must explicitly specify if they
350	// are enabled. This is to prevent new usages from depending on legacy behavior.
351	//
352	// Otherwise, if this is not set for any scope then the default  behavior is
353	// scope specific so please refer to the scope specific property documentation.
354	Enabled *bool
355
356	// The sdk_version to use for building the stubs.
357	//
358	// If not specified then it will use an sdk_version determined as follows:
359	// 1) If the sdk_version specified on the java_sdk_library is none then this
360	//    will be none. This is used for java_sdk_library instances that are used
361	//    to create stubs that contribute to the core_current sdk version.
362	// 2) Otherwise, it is assumed that this library extends but does not contribute
363	//    directly to a specific sdk_version and so this uses the sdk_version appropriate
364	//    for the api scope. e.g. public will use sdk_version: current, system will use
365	//    sdk_version: system_current, etc.
366	//
367	// This does not affect the sdk_version used for either generating the stubs source
368	// or the API file. They both have to use the same sdk_version as is used for
369	// compiling the implementation library.
370	Sdk_version *string
371}
372
373type sdkLibraryProperties struct {
374	// Visibility for impl library module. If not specified then defaults to the
375	// visibility property.
376	Impl_library_visibility []string
377
378	// Visibility for stubs library modules. If not specified then defaults to the
379	// visibility property.
380	Stubs_library_visibility []string
381
382	// Visibility for stubs source modules. If not specified then defaults to the
383	// visibility property.
384	Stubs_source_visibility []string
385
386	// List of Java libraries that will be in the classpath when building stubs
387	Stub_only_libs []string `android:"arch_variant"`
388
389	// list of package names that will be documented and publicized as API.
390	// This allows the API to be restricted to a subset of the source files provided.
391	// If this is unspecified then all the source files will be treated as being part
392	// of the API.
393	Api_packages []string
394
395	// list of package names that must be hidden from the API
396	Hidden_api_packages []string
397
398	// the relative path to the directory containing the api specification files.
399	// Defaults to "api".
400	Api_dir *string
401
402	// Determines whether a runtime implementation library is built; defaults to false.
403	//
404	// If true then it also prevents the module from being used as a shared module, i.e.
405	// it is as is shared_library: false, was set.
406	Api_only *bool
407
408	// local files that are used within user customized droiddoc options.
409	Droiddoc_option_files []string
410
411	// additional droiddoc options
412	// Available variables for substitution:
413	//
414	//  $(location <label>): the path to the droiddoc_option_files with name <label>
415	Droiddoc_options []string
416
417	// is set to true, Metalava will allow framework SDK to contain annotations.
418	Annotations_enabled *bool
419
420	// a list of top-level directories containing files to merge qualifier annotations
421	// (i.e. those intended to be included in the stubs written) from.
422	Merge_annotations_dirs []string
423
424	// a list of top-level directories containing Java stub files to merge show/hide annotations from.
425	Merge_inclusion_annotations_dirs []string
426
427	// If set to true, the path of dist files is apistubs/core. Defaults to false.
428	Core_lib *bool
429
430	// don't create dist rules.
431	No_dist *bool `blueprint:"mutated"`
432
433	// indicates whether system and test apis should be generated.
434	Generate_system_and_test_apis bool `blueprint:"mutated"`
435
436	// The properties specific to the public api scope
437	//
438	// Unless explicitly specified by using public.enabled the public api scope is
439	// enabled by default in both legacy and non-legacy mode.
440	Public ApiScopeProperties
441
442	// The properties specific to the system api scope
443	//
444	// In legacy mode the system api scope is enabled by default when sdk_version
445	// is set to something other than "none".
446	//
447	// In non-legacy mode the system api scope is disabled by default.
448	System ApiScopeProperties
449
450	// The properties specific to the test api scope
451	//
452	// In legacy mode the test api scope is enabled by default when sdk_version
453	// is set to something other than "none".
454	//
455	// In non-legacy mode the test api scope is disabled by default.
456	Test ApiScopeProperties
457
458	// The properties specific to the module-lib api scope
459	//
460	// Unless explicitly specified by using test.enabled the module-lib api scope is
461	// disabled by default.
462	Module_lib ApiScopeProperties
463
464	// The properties specific to the system-server api scope
465	//
466	// Unless explicitly specified by using test.enabled the module-lib api scope is
467	// disabled by default.
468	System_server ApiScopeProperties
469
470	// Determines if the stubs are preferred over the implementation library
471	// for linking, even when the client doesn't specify sdk_version. When this
472	// is set to true, such clients are provided with the widest API surface that
473	// this lib provides. Note however that this option doesn't affect the clients
474	// that are in the same APEX as this library. In that case, the clients are
475	// always linked with the implementation library. Default is false.
476	Default_to_stubs *bool
477
478	// Properties related to api linting.
479	Api_lint struct {
480		// Enable api linting.
481		Enabled *bool
482	}
483
484	// TODO: determines whether to create HTML doc or not
485	//Html_doc *bool
486}
487
488// Paths to outputs from java_sdk_library and java_sdk_library_import.
489//
490// Fields that are android.Paths are always set (during GenerateAndroidBuildActions).
491// OptionalPaths are always set by java_sdk_library but may not be set by
492// java_sdk_library_import as not all instances provide that information.
493type scopePaths struct {
494	// The path (represented as Paths for convenience when returning) to the stubs header jar.
495	//
496	// That is the jar that is created by turbine.
497	stubsHeaderPath android.Paths
498
499	// The path (represented as Paths for convenience when returning) to the stubs implementation jar.
500	//
501	// This is not the implementation jar, it still only contains stubs.
502	stubsImplPath android.Paths
503
504	// The API specification file, e.g. system_current.txt.
505	currentApiFilePath android.OptionalPath
506
507	// The specification of API elements removed since the last release.
508	removedApiFilePath android.OptionalPath
509
510	// The stubs source jar.
511	stubsSrcJar android.OptionalPath
512}
513
514func (paths *scopePaths) extractStubsLibraryInfoFromDependency(dep android.Module) error {
515	if lib, ok := dep.(Dependency); ok {
516		paths.stubsHeaderPath = lib.HeaderJars()
517		paths.stubsImplPath = lib.ImplementationJars()
518		return nil
519	} else {
520		return fmt.Errorf("expected module that implements Dependency, e.g. java_library")
521	}
522}
523
524func (paths *scopePaths) treatDepAsApiStubsProvider(dep android.Module, action func(provider ApiStubsProvider)) error {
525	if apiStubsProvider, ok := dep.(ApiStubsProvider); ok {
526		action(apiStubsProvider)
527		return nil
528	} else {
529		return fmt.Errorf("expected module that implements ApiStubsProvider, e.g. droidstubs")
530	}
531}
532
533func (paths *scopePaths) treatDepAsApiStubsSrcProvider(dep android.Module, action func(provider ApiStubsSrcProvider)) error {
534	if apiStubsProvider, ok := dep.(ApiStubsSrcProvider); ok {
535		action(apiStubsProvider)
536		return nil
537	} else {
538		return fmt.Errorf("expected module that implements ApiStubsSrcProvider, e.g. droidstubs")
539	}
540}
541
542func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
543	paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
544	paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
545}
546
547func (paths *scopePaths) extractApiInfoFromDep(dep android.Module) error {
548	return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
549		paths.extractApiInfoFromApiStubsProvider(provider)
550	})
551}
552
553func (paths *scopePaths) extractStubsSourceInfoFromApiStubsProviders(provider ApiStubsSrcProvider) {
554	paths.stubsSrcJar = android.OptionalPathForPath(provider.StubsSrcJar())
555}
556
557func (paths *scopePaths) extractStubsSourceInfoFromDep(dep android.Module) error {
558	return paths.treatDepAsApiStubsSrcProvider(dep, func(provider ApiStubsSrcProvider) {
559		paths.extractStubsSourceInfoFromApiStubsProviders(provider)
560	})
561}
562
563func (paths *scopePaths) extractStubsSourceAndApiInfoFromApiStubsProvider(dep android.Module) error {
564	return paths.treatDepAsApiStubsProvider(dep, func(provider ApiStubsProvider) {
565		paths.extractApiInfoFromApiStubsProvider(provider)
566		paths.extractStubsSourceInfoFromApiStubsProviders(provider)
567	})
568}
569
570type commonToSdkLibraryAndImportProperties struct {
571	// The naming scheme to use for the components that this module creates.
572	//
573	// If not specified then it defaults to "default". The other allowable value is
574	// "framework-modules" which matches the scheme currently used by framework modules
575	// for the equivalent components represented as separate Soong modules.
576	//
577	// This is a temporary mechanism to simplify conversion from separate modules for each
578	// component that follow a different naming pattern to the default one.
579	//
580	// TODO(b/155480189) - Remove once naming inconsistencies have been resolved.
581	Naming_scheme *string
582
583	// Specifies whether this module can be used as an Android shared library; defaults
584	// to true.
585	//
586	// An Android shared library is one that can be referenced in a <uses-library> element
587	// in an AndroidManifest.xml.
588	Shared_library *bool
589}
590
591// Common code between sdk library and sdk library import
592type commonToSdkLibraryAndImport struct {
593	moduleBase *android.ModuleBase
594
595	scopePaths map[*apiScope]*scopePaths
596
597	namingScheme sdkLibraryComponentNamingScheme
598
599	commonSdkLibraryProperties commonToSdkLibraryAndImportProperties
600
601	// Functionality related to this being used as a component of a java_sdk_library.
602	EmbeddableSdkLibraryComponent
603}
604
605func (c *commonToSdkLibraryAndImport) initCommon(moduleBase *android.ModuleBase) {
606	c.moduleBase = moduleBase
607
608	moduleBase.AddProperties(&c.commonSdkLibraryProperties)
609
610	// Initialize this as an sdk library component.
611	c.initSdkLibraryComponent(moduleBase)
612}
613
614func (c *commonToSdkLibraryAndImport) initCommonAfterDefaultsApplied(ctx android.DefaultableHookContext) bool {
615	schemeProperty := proptools.StringDefault(c.commonSdkLibraryProperties.Naming_scheme, "default")
616	switch schemeProperty {
617	case "default":
618		c.namingScheme = &defaultNamingScheme{}
619	case "framework-modules":
620		c.namingScheme = &frameworkModulesNamingScheme{}
621	default:
622		ctx.PropertyErrorf("naming_scheme", "expected 'default' but was %q", schemeProperty)
623		return false
624	}
625
626	// Only track this sdk library if this can be used as a shared library.
627	if c.sharedLibrary() {
628		// Use the name specified in the module definition as the owner.
629		c.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.moduleBase.BaseModuleName())
630	}
631
632	return true
633}
634
635// Module name of the runtime implementation library
636func (c *commonToSdkLibraryAndImport) implLibraryModuleName() string {
637	return c.moduleBase.BaseModuleName() + ".impl"
638}
639
640// Module name of the XML file for the lib
641func (c *commonToSdkLibraryAndImport) xmlPermissionsModuleName() string {
642	return c.moduleBase.BaseModuleName() + sdkXmlFileSuffix
643}
644
645// Name of the java_library module that compiles the stubs source.
646func (c *commonToSdkLibraryAndImport) stubsLibraryModuleName(apiScope *apiScope) string {
647	return c.namingScheme.stubsLibraryModuleName(apiScope, c.moduleBase.BaseModuleName())
648}
649
650// Name of the droidstubs module that generates the stubs source and may also
651// generate/check the API.
652func (c *commonToSdkLibraryAndImport) stubsSourceModuleName(apiScope *apiScope) string {
653	return c.namingScheme.stubsSourceModuleName(apiScope, c.moduleBase.BaseModuleName())
654}
655
656// Name of the droidstubs module that generates/checks the API. Only used if it
657// requires different arts to the stubs source generating module.
658func (c *commonToSdkLibraryAndImport) apiModuleName(apiScope *apiScope) string {
659	return c.namingScheme.apiModuleName(apiScope, c.moduleBase.BaseModuleName())
660}
661
662// The component names for different outputs of the java_sdk_library.
663//
664// They are similar to the names used for the child modules it creates
665const (
666	stubsSourceComponentName = "stubs.source"
667
668	apiTxtComponentName = "api.txt"
669
670	removedApiTxtComponentName = "removed-api.txt"
671)
672
673// A regular expression to match tags that reference a specific stubs component.
674//
675// It will only match if given a valid scope and a valid component. It is verfy strict
676// to ensure it does not accidentally match a similar looking tag that should be processed
677// by the embedded Library.
678var tagSplitter = func() *regexp.Regexp {
679	// Given a list of literal string items returns a regular expression that will
680	// match any one of the items.
681	choice := func(items ...string) string {
682		return `\Q` + strings.Join(items, `\E|\Q`) + `\E`
683	}
684
685	// Regular expression to match one of the scopes.
686	scopesRegexp := choice(allScopeNames...)
687
688	// Regular expression to match one of the components.
689	componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName)
690
691	// Regular expression to match any combination of one scope and one component.
692	return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
693}()
694
695// For OutputFileProducer interface
696//
697// .<scope>.stubs.source
698// .<scope>.api.txt
699// .<scope>.removed-api.txt
700func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
701	if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
702		scopeName := groups[1]
703		component := groups[2]
704
705		if scope, ok := scopeByName[scopeName]; ok {
706			paths := c.findScopePaths(scope)
707			if paths == nil {
708				return nil, fmt.Errorf("%q does not provide api scope %s", c.moduleBase.BaseModuleName(), scopeName)
709			}
710
711			switch component {
712			case stubsSourceComponentName:
713				if paths.stubsSrcJar.Valid() {
714					return android.Paths{paths.stubsSrcJar.Path()}, nil
715				}
716
717			case apiTxtComponentName:
718				if paths.currentApiFilePath.Valid() {
719					return android.Paths{paths.currentApiFilePath.Path()}, nil
720				}
721
722			case removedApiTxtComponentName:
723				if paths.removedApiFilePath.Valid() {
724					return android.Paths{paths.removedApiFilePath.Path()}, nil
725				}
726			}
727
728			return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
729		} else {
730			return nil, fmt.Errorf("unknown scope %s in %s", scope, tag)
731		}
732
733	} else {
734		return nil, nil
735	}
736}
737
738func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
739	if c.scopePaths == nil {
740		c.scopePaths = make(map[*apiScope]*scopePaths)
741	}
742	paths := c.scopePaths[scope]
743	if paths == nil {
744		paths = &scopePaths{}
745		c.scopePaths[scope] = paths
746	}
747
748	return paths
749}
750
751func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
752	if c.scopePaths == nil {
753		return nil
754	}
755
756	return c.scopePaths[scope]
757}
758
759// If this does not support the requested api scope then find the closest available
760// scope it does support. Returns nil if no such scope is available.
761func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
762	for s := scope; s != nil; s = s.extends {
763		if paths := c.findScopePaths(s); paths != nil {
764			return paths
765		}
766	}
767
768	// This should never happen outside tests as public should be the base scope for every
769	// scope and is enabled by default.
770	return nil
771}
772
773func (c *commonToSdkLibraryAndImport) selectHeaderJarsForSdkVersion(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
774
775	// If a specific numeric version has been requested then use prebuilt versions of the sdk.
776	if sdkVersion.version.isNumbered() {
777		return PrebuiltJars(ctx, c.moduleBase.BaseModuleName(), sdkVersion)
778	}
779
780	var apiScope *apiScope
781	switch sdkVersion.kind {
782	case sdkSystem:
783		apiScope = apiScopeSystem
784	case sdkModule:
785		apiScope = apiScopeModuleLib
786	case sdkTest:
787		apiScope = apiScopeTest
788	case sdkSystemServer:
789		apiScope = apiScopeSystemServer
790	default:
791		apiScope = apiScopePublic
792	}
793
794	paths := c.findClosestScopePath(apiScope)
795	if paths == nil {
796		var scopes []string
797		for _, s := range allApiScopes {
798			if c.findScopePaths(s) != nil {
799				scopes = append(scopes, s.name)
800			}
801		}
802		ctx.ModuleErrorf("requires api scope %s from %s but it only has %q available", apiScope.name, c.moduleBase.BaseModuleName(), scopes)
803		return nil
804	}
805
806	return paths.stubsHeaderPath
807}
808
809func (c *commonToSdkLibraryAndImport) sdkComponentPropertiesForChildLibrary() interface{} {
810	componentProps := &struct {
811		SdkLibraryToImplicitlyTrack *string
812	}{}
813
814	if c.sharedLibrary() {
815		// Mark the stubs library as being components of this java_sdk_library so that
816		// any app that includes code which depends (directly or indirectly) on the stubs
817		// library will have the appropriate <uses-library> invocation inserted into its
818		// manifest if necessary.
819		componentProps.SdkLibraryToImplicitlyTrack = proptools.StringPtr(c.moduleBase.BaseModuleName())
820	}
821
822	return componentProps
823}
824
825// Check if this can be used as a shared library.
826func (c *commonToSdkLibraryAndImport) sharedLibrary() bool {
827	return proptools.BoolDefault(c.commonSdkLibraryProperties.Shared_library, true)
828}
829
830// Properties related to the use of a module as an component of a java_sdk_library.
831type SdkLibraryComponentProperties struct {
832
833	// The name of the java_sdk_library/_import to add to a <uses-library> entry
834	// in the AndroidManifest.xml of any Android app that includes code that references
835	// this module. If not set then no java_sdk_library/_import is tracked.
836	SdkLibraryToImplicitlyTrack *string `blueprint:"mutated"`
837}
838
839// Structure to be embedded in a module struct that needs to support the
840// SdkLibraryComponentDependency interface.
841type EmbeddableSdkLibraryComponent struct {
842	sdkLibraryComponentProperties SdkLibraryComponentProperties
843}
844
845func (e *EmbeddableSdkLibraryComponent) initSdkLibraryComponent(moduleBase *android.ModuleBase) {
846	moduleBase.AddProperties(&e.sdkLibraryComponentProperties)
847}
848
849// to satisfy SdkLibraryComponentDependency
850func (e *EmbeddableSdkLibraryComponent) OptionalImplicitSdkLibrary() []string {
851	if e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack != nil {
852		return []string{*e.sdkLibraryComponentProperties.SdkLibraryToImplicitlyTrack}
853	}
854	return nil
855}
856
857// Implemented by modules that are (or possibly could be) a component of a java_sdk_library
858// (including the java_sdk_library) itself.
859type SdkLibraryComponentDependency interface {
860	// The optional name of the sdk library that should be implicitly added to the
861	// AndroidManifest of an app that contains code which references the sdk library.
862	//
863	// Returns an array containing 0 or 1 items rather than a *string to make it easier
864	// to append this to the list of exported sdk libraries.
865	OptionalImplicitSdkLibrary() []string
866}
867
868// Make sure that all the module types that are components of java_sdk_library/_import
869// and which can be referenced (directly or indirectly) from an android app implement
870// the SdkLibraryComponentDependency interface.
871var _ SdkLibraryComponentDependency = (*Library)(nil)
872var _ SdkLibraryComponentDependency = (*Import)(nil)
873var _ SdkLibraryComponentDependency = (*SdkLibrary)(nil)
874var _ SdkLibraryComponentDependency = (*SdkLibraryImport)(nil)
875
876// Provides access to sdk_version related header and implentation jars.
877type SdkLibraryDependency interface {
878	SdkLibraryComponentDependency
879
880	// Get the header jars appropriate for the supplied sdk_version.
881	//
882	// These are turbine generated jars so they only change if the externals of the
883	// class changes but it does not contain and implementation or JavaDoc.
884	SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
885
886	// Get the implementation jars appropriate for the supplied sdk version.
887	//
888	// These are either the implementation jar for the whole sdk library or the implementation
889	// jars for the stubs. The latter should only be needed when generating JavaDoc as otherwise
890	// they are identical to the corresponding header jars.
891	SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
892}
893
894type SdkLibrary struct {
895	Library
896
897	sdkLibraryProperties sdkLibraryProperties
898
899	// Map from api scope to the scope specific property structure.
900	scopeToProperties map[*apiScope]*ApiScopeProperties
901
902	commonToSdkLibraryAndImport
903}
904
905var _ Dependency = (*SdkLibrary)(nil)
906var _ SdkLibraryDependency = (*SdkLibrary)(nil)
907
908func (module *SdkLibrary) generateTestAndSystemScopesByDefault() bool {
909	return module.sdkLibraryProperties.Generate_system_and_test_apis
910}
911
912func (module *SdkLibrary) getGeneratedApiScopes(ctx android.EarlyModuleContext) apiScopes {
913	// Check to see if any scopes have been explicitly enabled. If any have then all
914	// must be.
915	anyScopesExplicitlyEnabled := false
916	for _, scope := range allApiScopes {
917		scopeProperties := module.scopeToProperties[scope]
918		if scopeProperties.Enabled != nil {
919			anyScopesExplicitlyEnabled = true
920			break
921		}
922	}
923
924	var generatedScopes apiScopes
925	enabledScopes := make(map[*apiScope]struct{})
926	for _, scope := range allApiScopes {
927		scopeProperties := module.scopeToProperties[scope]
928		// If any scopes are explicitly enabled then ignore the legacy enabled status.
929		// This is to ensure that any new usages of this module type do not rely on legacy
930		// behaviour.
931		defaultEnabledStatus := false
932		if anyScopesExplicitlyEnabled {
933			defaultEnabledStatus = scope.defaultEnabledStatus
934		} else {
935			defaultEnabledStatus = scope.legacyEnabledStatus(module)
936		}
937		enabled := proptools.BoolDefault(scopeProperties.Enabled, defaultEnabledStatus)
938		if enabled {
939			enabledScopes[scope] = struct{}{}
940			generatedScopes = append(generatedScopes, scope)
941		}
942	}
943
944	// Now check to make sure that any scope that is extended by an enabled scope is also
945	// enabled.
946	for _, scope := range allApiScopes {
947		if _, ok := enabledScopes[scope]; ok {
948			extends := scope.extends
949			if extends != nil {
950				if _, ok := enabledScopes[extends]; !ok {
951					ctx.ModuleErrorf("enabled api scope %q depends on disabled scope %q", scope, extends)
952				}
953			}
954		}
955	}
956
957	return generatedScopes
958}
959
960type sdkLibraryComponentTag struct {
961	blueprint.BaseDependencyTag
962	name string
963}
964
965// Mark this tag so dependencies that use it are excluded from visibility enforcement.
966func (t sdkLibraryComponentTag) ExcludeFromVisibilityEnforcement() {}
967
968var xmlPermissionsFileTag = sdkLibraryComponentTag{name: "xml-permissions-file"}
969
970func IsXmlPermissionsFileDepTag(depTag blueprint.DependencyTag) bool {
971	if dt, ok := depTag.(sdkLibraryComponentTag); ok {
972		return dt == xmlPermissionsFileTag
973	}
974	return false
975}
976
977var implLibraryTag = sdkLibraryComponentTag{name: "impl-library"}
978
979func (module *SdkLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
980	for _, apiScope := range module.getGeneratedApiScopes(ctx) {
981		// Add dependencies to the stubs library
982		ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
983
984		// If the stubs source and API cannot be generated together then add an additional dependency on
985		// the API module.
986		if apiScope.createStubsSourceAndApiTogether {
987			// Add a dependency on the stubs source in order to access both stubs source and api information.
988			ctx.AddVariationDependencies(nil, apiScope.stubsSourceAndApiTag, module.stubsSourceModuleName(apiScope))
989		} else {
990			// Add separate dependencies on the creators of the stubs source files and the API.
991			ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, module.stubsSourceModuleName(apiScope))
992			ctx.AddVariationDependencies(nil, apiScope.apiFileTag, module.apiModuleName(apiScope))
993		}
994	}
995
996	if module.requiresRuntimeImplementationLibrary() {
997		// Add dependency to the rule for generating the implementation library.
998		ctx.AddDependency(module, implLibraryTag, module.implLibraryModuleName())
999
1000		if module.sharedLibrary() {
1001			// Add dependency to the rule for generating the xml permissions file
1002			ctx.AddDependency(module, xmlPermissionsFileTag, module.xmlPermissionsModuleName())
1003		}
1004
1005		// Only add the deps for the library if it is actually going to be built.
1006		module.Library.deps(ctx)
1007	}
1008}
1009
1010func (module *SdkLibrary) OutputFiles(tag string) (android.Paths, error) {
1011	paths, err := module.commonOutputFiles(tag)
1012	if paths == nil && err == nil {
1013		return module.Library.OutputFiles(tag)
1014	} else {
1015		return paths, err
1016	}
1017}
1018
1019func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1020	// Only build an implementation library if required.
1021	if module.requiresRuntimeImplementationLibrary() {
1022		module.Library.GenerateAndroidBuildActions(ctx)
1023	}
1024
1025	// Record the paths to the header jars of the library (stubs and impl).
1026	// When this java_sdk_library is depended upon from others via "libs" property,
1027	// the recorded paths will be returned depending on the link type of the caller.
1028	ctx.VisitDirectDeps(func(to android.Module) {
1029		tag := ctx.OtherModuleDependencyTag(to)
1030
1031		// Extract information from any of the scope specific dependencies.
1032		if scopeTag, ok := tag.(scopeDependencyTag); ok {
1033			apiScope := scopeTag.apiScope
1034			scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
1035
1036			// Extract information from the dependency. The exact information extracted
1037			// is determined by the nature of the dependency which is determined by the tag.
1038			scopeTag.extractDepInfo(ctx, to, scopePaths)
1039		}
1040	})
1041}
1042
1043func (module *SdkLibrary) AndroidMkEntries() []android.AndroidMkEntries {
1044	if !module.requiresRuntimeImplementationLibrary() {
1045		return nil
1046	}
1047	entriesList := module.Library.AndroidMkEntries()
1048	entries := &entriesList[0]
1049	entries.Required = append(entries.Required, module.xmlPermissionsModuleName())
1050	return entriesList
1051}
1052
1053// The dist path of the stub artifacts
1054func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
1055	if module.ModuleBase.Owner() != "" {
1056		return path.Join("apistubs", module.ModuleBase.Owner(), apiScope.name)
1057	} else if Bool(module.sdkLibraryProperties.Core_lib) {
1058		return path.Join("apistubs", "core", apiScope.name)
1059	} else {
1060		return path.Join("apistubs", "android", apiScope.name)
1061	}
1062}
1063
1064// Get the sdk version for use when compiling the stubs library.
1065func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.EarlyModuleContext, apiScope *apiScope) string {
1066	scopeProperties := module.scopeToProperties[apiScope]
1067	if scopeProperties.Sdk_version != nil {
1068		return proptools.String(scopeProperties.Sdk_version)
1069	}
1070
1071	sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1072	if sdkDep.hasStandardLibs() {
1073		// If building against a standard sdk then use the sdk version appropriate for the scope.
1074		return apiScope.sdkVersion
1075	} else {
1076		// Otherwise, use no system module.
1077		return "none"
1078	}
1079}
1080
1081func (module *SdkLibrary) latestApiFilegroupName(apiScope *apiScope) string {
1082	return ":" + module.BaseModuleName() + ".api." + apiScope.name + ".latest"
1083}
1084
1085func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) string {
1086	return ":" + module.BaseModuleName() + "-removed.api." + apiScope.name + ".latest"
1087}
1088
1089// Creates the implementation java library
1090func (module *SdkLibrary) createImplLibrary(mctx android.DefaultableHookContext) {
1091
1092	moduleNamePtr := proptools.StringPtr(module.BaseModuleName())
1093
1094	props := struct {
1095		Name              *string
1096		Visibility        []string
1097		Instrument        bool
1098		ConfigurationName *string
1099	}{
1100		Name:       proptools.StringPtr(module.implLibraryModuleName()),
1101		Visibility: module.sdkLibraryProperties.Impl_library_visibility,
1102		// Set the instrument property to ensure it is instrumented when instrumentation is required.
1103		Instrument: true,
1104
1105		// Make the created library behave as if it had the same name as this module.
1106		ConfigurationName: moduleNamePtr,
1107	}
1108
1109	properties := []interface{}{
1110		&module.properties,
1111		&module.protoProperties,
1112		&module.deviceProperties,
1113		&module.dexpreoptProperties,
1114		&module.linter.properties,
1115		&props,
1116		module.sdkComponentPropertiesForChildLibrary(),
1117	}
1118	mctx.CreateModule(LibraryFactory, properties...)
1119}
1120
1121// Creates a static java library that has API stubs
1122func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
1123	props := struct {
1124		Name              *string
1125		Visibility        []string
1126		Srcs              []string
1127		Installable       *bool
1128		Sdk_version       *string
1129		System_modules    *string
1130		Patch_module      *string
1131		Libs              []string
1132		Compile_dex       *bool
1133		Java_version      *string
1134		Product_variables struct {
1135			Pdk struct {
1136				Enabled *bool
1137			}
1138		}
1139		Openjdk9 struct {
1140			Srcs       []string
1141			Javacflags []string
1142		}
1143		Dist struct {
1144			Targets []string
1145			Dest    *string
1146			Dir     *string
1147			Tag     *string
1148		}
1149	}{}
1150
1151	props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
1152
1153	// If stubs_library_visibility is not set then the created module will use the
1154	// visibility of this module.
1155	visibility := module.sdkLibraryProperties.Stubs_library_visibility
1156	props.Visibility = visibility
1157
1158	// sources are generated from the droiddoc
1159	props.Srcs = []string{":" + module.stubsSourceModuleName(apiScope)}
1160	sdkVersion := module.sdkVersionForStubsLibrary(mctx, apiScope)
1161	props.Sdk_version = proptools.StringPtr(sdkVersion)
1162	props.System_modules = module.deviceProperties.System_modules
1163	props.Patch_module = module.properties.Patch_module
1164	props.Installable = proptools.BoolPtr(false)
1165	props.Libs = module.sdkLibraryProperties.Stub_only_libs
1166	// The stub-annotations library contains special versions of the annotations
1167	// with CLASS retention policy, so that they're kept.
1168	if proptools.Bool(module.sdkLibraryProperties.Annotations_enabled) {
1169		props.Libs = append(props.Libs, "stub-annotations")
1170	}
1171	props.Product_variables.Pdk.Enabled = proptools.BoolPtr(false)
1172	props.Openjdk9.Srcs = module.properties.Openjdk9.Srcs
1173	props.Openjdk9.Javacflags = module.properties.Openjdk9.Javacflags
1174	// We compile the stubs for 1.8 in line with the main android.jar stubs, and potential
1175	// interop with older developer tools that don't support 1.9.
1176	props.Java_version = proptools.StringPtr("1.8")
1177	if module.deviceProperties.Compile_dex != nil {
1178		props.Compile_dex = module.deviceProperties.Compile_dex
1179	}
1180
1181	// Dist the class jar artifact for sdk builds.
1182	if !Bool(module.sdkLibraryProperties.No_dist) {
1183		props.Dist.Targets = []string{"sdk", "win_sdk"}
1184		props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.jar", module.BaseModuleName()))
1185		props.Dist.Dir = proptools.StringPtr(module.apiDistPath(apiScope))
1186		props.Dist.Tag = proptools.StringPtr(".jar")
1187	}
1188
1189	mctx.CreateModule(LibraryFactory, &props, module.sdkComponentPropertiesForChildLibrary())
1190}
1191
1192// Creates a droidstubs module that creates stubs source files from the given full source
1193// files and also updates and checks the API specification files.
1194func (module *SdkLibrary) createStubsSourcesAndApi(mctx android.DefaultableHookContext, apiScope *apiScope, name string, createStubSources, createApi bool, scopeSpecificDroidstubsArgs []string) {
1195	props := struct {
1196		Name                             *string
1197		Visibility                       []string
1198		Srcs                             []string
1199		Installable                      *bool
1200		Sdk_version                      *string
1201		System_modules                   *string
1202		Libs                             []string
1203		Arg_files                        []string
1204		Args                             *string
1205		Java_version                     *string
1206		Annotations_enabled              *bool
1207		Merge_annotations_dirs           []string
1208		Merge_inclusion_annotations_dirs []string
1209		Generate_stubs                   *bool
1210		Check_api                        struct {
1211			Current                   ApiToCheck
1212			Last_released             ApiToCheck
1213			Ignore_missing_latest_api *bool
1214
1215			Api_lint struct {
1216				Enabled       *bool
1217				New_since     *string
1218				Baseline_file *string
1219			}
1220		}
1221		Aidl struct {
1222			Include_dirs       []string
1223			Local_include_dirs []string
1224		}
1225		Dist struct {
1226			Targets []string
1227			Dest    *string
1228			Dir     *string
1229		}
1230	}{}
1231
1232	// The stubs source processing uses the same compile time classpath when extracting the
1233	// API from the implementation library as it does when compiling it. i.e. the same
1234	// * sdk version
1235	// * system_modules
1236	// * libs (static_libs/libs)
1237
1238	props.Name = proptools.StringPtr(name)
1239
1240	// If stubs_source_visibility is not set then the created module will use the
1241	// visibility of this module.
1242	visibility := module.sdkLibraryProperties.Stubs_source_visibility
1243	props.Visibility = visibility
1244
1245	props.Srcs = append(props.Srcs, module.properties.Srcs...)
1246	props.Sdk_version = module.deviceProperties.Sdk_version
1247	props.System_modules = module.deviceProperties.System_modules
1248	props.Installable = proptools.BoolPtr(false)
1249	// A droiddoc module has only one Libs property and doesn't distinguish between
1250	// shared libs and static libs. So we need to add both of these libs to Libs property.
1251	props.Libs = module.properties.Libs
1252	props.Libs = append(props.Libs, module.properties.Static_libs...)
1253	props.Aidl.Include_dirs = module.deviceProperties.Aidl.Include_dirs
1254	props.Aidl.Local_include_dirs = module.deviceProperties.Aidl.Local_include_dirs
1255	props.Java_version = module.properties.Java_version
1256
1257	props.Annotations_enabled = module.sdkLibraryProperties.Annotations_enabled
1258	props.Merge_annotations_dirs = module.sdkLibraryProperties.Merge_annotations_dirs
1259	props.Merge_inclusion_annotations_dirs = module.sdkLibraryProperties.Merge_inclusion_annotations_dirs
1260
1261	droidstubsArgs := []string{}
1262	if len(module.sdkLibraryProperties.Api_packages) != 0 {
1263		droidstubsArgs = append(droidstubsArgs, "--stub-packages "+strings.Join(module.sdkLibraryProperties.Api_packages, ":"))
1264	}
1265	if len(module.sdkLibraryProperties.Hidden_api_packages) != 0 {
1266		droidstubsArgs = append(droidstubsArgs,
1267			android.JoinWithPrefix(module.sdkLibraryProperties.Hidden_api_packages, " --hide-package "))
1268	}
1269	droidstubsArgs = append(droidstubsArgs, module.sdkLibraryProperties.Droiddoc_options...)
1270	disabledWarnings := []string{
1271		"MissingPermission",
1272		"BroadcastBehavior",
1273		"HiddenSuperclass",
1274		"DeprecationMismatch",
1275		"UnavailableSymbol",
1276		"SdkConstant",
1277		"HiddenTypeParameter",
1278		"Todo",
1279		"Typo",
1280	}
1281	droidstubsArgs = append(droidstubsArgs, android.JoinWithPrefix(disabledWarnings, "--hide "))
1282
1283	if !createStubSources {
1284		// Stubs are not required.
1285		props.Generate_stubs = proptools.BoolPtr(false)
1286	}
1287
1288	// Add in scope specific arguments.
1289	droidstubsArgs = append(droidstubsArgs, scopeSpecificDroidstubsArgs...)
1290	props.Arg_files = module.sdkLibraryProperties.Droiddoc_option_files
1291	props.Args = proptools.StringPtr(strings.Join(droidstubsArgs, " "))
1292
1293	if createApi {
1294		// List of APIs identified from the provided source files are created. They are later
1295		// compared against to the not-yet-released (a.k.a current) list of APIs and to the
1296		// last-released (a.k.a numbered) list of API.
1297		currentApiFileName := apiScope.apiFilePrefix + "current.txt"
1298		removedApiFileName := apiScope.apiFilePrefix + "removed.txt"
1299		apiDir := module.getApiDir()
1300		currentApiFileName = path.Join(apiDir, currentApiFileName)
1301		removedApiFileName = path.Join(apiDir, removedApiFileName)
1302
1303		// check against the not-yet-release API
1304		props.Check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
1305		props.Check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
1306
1307		if !apiScope.unstable {
1308			// check against the latest released API
1309			latestApiFilegroupName := proptools.StringPtr(module.latestApiFilegroupName(apiScope))
1310			props.Check_api.Last_released.Api_file = latestApiFilegroupName
1311			props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
1312				module.latestRemovedApiFilegroupName(apiScope))
1313			props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
1314
1315			if proptools.Bool(module.sdkLibraryProperties.Api_lint.Enabled) {
1316				// Enable api lint.
1317				props.Check_api.Api_lint.Enabled = proptools.BoolPtr(true)
1318				props.Check_api.Api_lint.New_since = latestApiFilegroupName
1319
1320				// If it exists then pass a lint-baseline.txt through to droidstubs.
1321				baselinePath := path.Join(apiDir, apiScope.apiFilePrefix+"lint-baseline.txt")
1322				baselinePathRelativeToRoot := path.Join(mctx.ModuleDir(), baselinePath)
1323				paths, err := mctx.GlobWithDeps(baselinePathRelativeToRoot, nil)
1324				if err != nil {
1325					mctx.ModuleErrorf("error checking for presence of %s: %s", baselinePathRelativeToRoot, err)
1326				}
1327				if len(paths) == 1 {
1328					props.Check_api.Api_lint.Baseline_file = proptools.StringPtr(baselinePath)
1329				} else if len(paths) != 0 {
1330					mctx.ModuleErrorf("error checking for presence of %s: expected one path, found: %v", baselinePathRelativeToRoot, paths)
1331				}
1332			}
1333		}
1334
1335		// Dist the api txt artifact for sdk builds.
1336		if !Bool(module.sdkLibraryProperties.No_dist) {
1337			props.Dist.Targets = []string{"sdk", "win_sdk"}
1338			props.Dist.Dest = proptools.StringPtr(fmt.Sprintf("%v.txt", module.BaseModuleName()))
1339			props.Dist.Dir = proptools.StringPtr(path.Join(module.apiDistPath(apiScope), "api"))
1340		}
1341	}
1342
1343	mctx.CreateModule(DroidstubsFactory, &props)
1344}
1345
1346func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1347	depTag := mctx.OtherModuleDependencyTag(dep)
1348	if depTag == xmlPermissionsFileTag {
1349		return true
1350	}
1351	return module.Library.DepIsInSameApex(mctx, dep)
1352}
1353
1354// Creates the xml file that publicizes the runtime library
1355func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
1356	props := struct {
1357		Name           *string
1358		Lib_name       *string
1359		Apex_available []string
1360	}{
1361		Name:           proptools.StringPtr(module.xmlPermissionsModuleName()),
1362		Lib_name:       proptools.StringPtr(module.BaseModuleName()),
1363		Apex_available: module.ApexProperties.Apex_available,
1364	}
1365
1366	mctx.CreateModule(sdkLibraryXmlFactory, &props)
1367}
1368
1369func PrebuiltJars(ctx android.BaseModuleContext, baseName string, s sdkSpec) android.Paths {
1370	var ver sdkVersion
1371	var kind sdkKind
1372	if s.usePrebuilt(ctx) {
1373		ver = s.version
1374		kind = s.kind
1375	} else {
1376		// We don't have prebuilt SDK for the specific sdkVersion.
1377		// Instead of breaking the build, fallback to use "system_current"
1378		ver = sdkVersionCurrent
1379		kind = sdkSystem
1380	}
1381
1382	dir := filepath.Join("prebuilts", "sdk", ver.String(), kind.String())
1383	jar := filepath.Join(dir, baseName+".jar")
1384	jarPath := android.ExistentPathForSource(ctx, jar)
1385	if !jarPath.Valid() {
1386		if ctx.Config().AllowMissingDependencies() {
1387			return android.Paths{android.PathForSource(ctx, jar)}
1388		} else {
1389			ctx.PropertyErrorf("sdk_library", "invalid sdk version %q, %q does not exist", s.raw, jar)
1390		}
1391		return nil
1392	}
1393	return android.Paths{jarPath.Path()}
1394}
1395
1396// Get the apex name for module, "" if it is for platform.
1397func getApexNameForModule(module android.Module) string {
1398	if apex, ok := module.(android.ApexModule); ok {
1399		return apex.ApexName()
1400	}
1401
1402	return ""
1403}
1404
1405// Check to see if the other module is within the same named APEX as this module.
1406//
1407// If either this or the other module are on the platform then this will return
1408// false.
1409func withinSameApexAs(module android.ApexModule, other android.Module) bool {
1410	name := module.ApexName()
1411	return name != "" && getApexNameForModule(other) == name
1412}
1413
1414func (module *SdkLibrary) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
1415	// If the client doesn't set sdk_version, but if this library prefers stubs over
1416	// the impl library, let's provide the widest API surface possible. To do so,
1417	// force override sdk_version to module_current so that the closest possible API
1418	// surface could be found in selectHeaderJarsForSdkVersion
1419	if module.defaultsToStubs() && !sdkVersion.specified() {
1420		sdkVersion = sdkSpecFrom("module_current")
1421	}
1422
1423	// Only provide access to the implementation library if it is actually built.
1424	if module.requiresRuntimeImplementationLibrary() {
1425		// Check any special cases for java_sdk_library.
1426		//
1427		// Only allow access to the implementation library in the following condition:
1428		// * No sdk_version specified on the referencing module.
1429		// * The referencing module is in the same apex as this.
1430		if sdkVersion.kind == sdkPrivate || withinSameApexAs(module, ctx.Module()) {
1431			if headerJars {
1432				return module.HeaderJars()
1433			} else {
1434				return module.ImplementationJars()
1435			}
1436		}
1437	}
1438
1439	return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
1440}
1441
1442// to satisfy SdkLibraryDependency interface
1443func (module *SdkLibrary) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
1444	return module.sdkJars(ctx, sdkVersion, true /*headerJars*/)
1445}
1446
1447// to satisfy SdkLibraryDependency interface
1448func (module *SdkLibrary) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
1449	return module.sdkJars(ctx, sdkVersion, false /*headerJars*/)
1450}
1451
1452func (module *SdkLibrary) SetNoDist() {
1453	module.sdkLibraryProperties.No_dist = proptools.BoolPtr(true)
1454}
1455
1456var javaSdkLibrariesKey = android.NewOnceKey("javaSdkLibraries")
1457
1458func javaSdkLibraries(config android.Config) *[]string {
1459	return config.Once(javaSdkLibrariesKey, func() interface{} {
1460		return &[]string{}
1461	}).(*[]string)
1462}
1463
1464func (module *SdkLibrary) getApiDir() string {
1465	return proptools.StringDefault(module.sdkLibraryProperties.Api_dir, "api")
1466}
1467
1468// For a java_sdk_library module, create internal modules for stubs, docs,
1469// runtime libs and xml file. If requested, the stubs and docs are created twice
1470// once for public API level and once for system API level
1471func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
1472	// If the module has been disabled then don't create any child modules.
1473	if !module.Enabled() {
1474		return
1475	}
1476
1477	if len(module.properties.Srcs) == 0 {
1478		mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
1479		return
1480	}
1481
1482	// If this builds against standard libraries (i.e. is not part of the core libraries)
1483	// then assume it provides both system and test apis. Otherwise, assume it does not and
1484	// also assume it does not contribute to the dist build.
1485	sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
1486	hasSystemAndTestApis := sdkDep.hasStandardLibs()
1487	module.sdkLibraryProperties.Generate_system_and_test_apis = hasSystemAndTestApis
1488	module.sdkLibraryProperties.No_dist = proptools.BoolPtr(!hasSystemAndTestApis)
1489
1490	missing_current_api := false
1491
1492	generatedScopes := module.getGeneratedApiScopes(mctx)
1493
1494	apiDir := module.getApiDir()
1495	for _, scope := range generatedScopes {
1496		for _, api := range []string{"current.txt", "removed.txt"} {
1497			path := path.Join(mctx.ModuleDir(), apiDir, scope.apiFilePrefix+api)
1498			p := android.ExistentPathForSource(mctx, path)
1499			if !p.Valid() {
1500				mctx.ModuleErrorf("Current api file %#v doesn't exist", path)
1501				missing_current_api = true
1502			}
1503		}
1504	}
1505
1506	if missing_current_api {
1507		script := "build/soong/scripts/gen-java-current-api-files.sh"
1508		p := android.ExistentPathForSource(mctx, script)
1509
1510		if !p.Valid() {
1511			panic(fmt.Sprintf("script file %s doesn't exist", script))
1512		}
1513
1514		mctx.ModuleErrorf("One or more current api files are missing. "+
1515			"You can update them by:\n"+
1516			"%s %q %s && m update-api",
1517			script, filepath.Join(mctx.ModuleDir(), apiDir),
1518			strings.Join(generatedScopes.Strings(func(s *apiScope) string { return s.apiFilePrefix }), " "))
1519		return
1520	}
1521
1522	for _, scope := range generatedScopes {
1523		stubsSourceArgs := scope.droidstubsArgsForGeneratingStubsSource
1524		stubsSourceModuleName := module.stubsSourceModuleName(scope)
1525
1526		// If the args needed to generate the stubs and API are the same then they
1527		// can be generated in a single invocation of metalava, otherwise they will
1528		// need separate invocations.
1529		if scope.createStubsSourceAndApiTogether {
1530			// Use the stubs source name for legacy reasons.
1531			module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, true, stubsSourceArgs)
1532		} else {
1533			module.createStubsSourcesAndApi(mctx, scope, stubsSourceModuleName, true, false, stubsSourceArgs)
1534
1535			apiArgs := scope.droidstubsArgsForGeneratingApi
1536			apiName := module.apiModuleName(scope)
1537			module.createStubsSourcesAndApi(mctx, scope, apiName, false, true, apiArgs)
1538		}
1539
1540		module.createStubsLibrary(mctx, scope)
1541	}
1542
1543	if module.requiresRuntimeImplementationLibrary() {
1544		// Create child module to create an implementation library.
1545		//
1546		// This temporarily creates a second implementation library that can be explicitly
1547		// referenced.
1548		//
1549		// TODO(b/156618935) - update comment once only one implementation library is created.
1550		module.createImplLibrary(mctx)
1551
1552		// Only create an XML permissions file that declares the library as being usable
1553		// as a shared library if required.
1554		if module.sharedLibrary() {
1555			module.createXmlFile(mctx)
1556		}
1557
1558		// record java_sdk_library modules so that they are exported to make
1559		javaSdkLibraries := javaSdkLibraries(mctx.Config())
1560		javaSdkLibrariesLock.Lock()
1561		defer javaSdkLibrariesLock.Unlock()
1562		*javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1563	}
1564}
1565
1566func (module *SdkLibrary) InitSdkLibraryProperties() {
1567	module.addHostAndDeviceProperties()
1568	module.AddProperties(&module.sdkLibraryProperties)
1569
1570	module.initSdkLibraryComponent(&module.ModuleBase)
1571
1572	module.properties.Installable = proptools.BoolPtr(true)
1573	module.deviceProperties.IsSDKLibrary = true
1574}
1575
1576func (module *SdkLibrary) requiresRuntimeImplementationLibrary() bool {
1577	return !proptools.Bool(module.sdkLibraryProperties.Api_only)
1578}
1579
1580func (module *SdkLibrary) defaultsToStubs() bool {
1581	return proptools.Bool(module.sdkLibraryProperties.Default_to_stubs)
1582}
1583
1584// Defines how to name the individual component modules the sdk library creates.
1585type sdkLibraryComponentNamingScheme interface {
1586	stubsLibraryModuleName(scope *apiScope, baseName string) string
1587
1588	stubsSourceModuleName(scope *apiScope, baseName string) string
1589
1590	apiModuleName(scope *apiScope, baseName string) string
1591}
1592
1593type defaultNamingScheme struct {
1594}
1595
1596func (s *defaultNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1597	return scope.stubsLibraryModuleName(baseName)
1598}
1599
1600func (s *defaultNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1601	return scope.stubsSourceModuleName(baseName)
1602}
1603
1604func (s *defaultNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1605	return scope.apiModuleName(baseName)
1606}
1607
1608var _ sdkLibraryComponentNamingScheme = (*defaultNamingScheme)(nil)
1609
1610type frameworkModulesNamingScheme struct {
1611}
1612
1613func (s *frameworkModulesNamingScheme) moduleSuffix(scope *apiScope) string {
1614	suffix := scope.name
1615	if scope == apiScopeModuleLib {
1616		suffix = "module_libs_"
1617	}
1618	return suffix
1619}
1620
1621func (s *frameworkModulesNamingScheme) stubsLibraryModuleName(scope *apiScope, baseName string) string {
1622	return fmt.Sprintf("%s-stubs-%sapi", baseName, s.moduleSuffix(scope))
1623}
1624
1625func (s *frameworkModulesNamingScheme) stubsSourceModuleName(scope *apiScope, baseName string) string {
1626	return fmt.Sprintf("%s-stubs-srcs-%sapi", baseName, s.moduleSuffix(scope))
1627}
1628
1629func (s *frameworkModulesNamingScheme) apiModuleName(scope *apiScope, baseName string) string {
1630	return fmt.Sprintf("%s-api-%sapi", baseName, s.moduleSuffix(scope))
1631}
1632
1633var _ sdkLibraryComponentNamingScheme = (*frameworkModulesNamingScheme)(nil)
1634
1635func moduleStubLinkType(name string) (stub bool, ret linkType) {
1636	// This suffix-based approach is fragile and could potentially mis-trigger.
1637	// TODO(b/155164730): Clean this up when modules no longer reference sdk_lib stubs directly.
1638	if strings.HasSuffix(name, ".stubs.public") || strings.HasSuffix(name, "-stubs-publicapi") {
1639		return true, javaSdk
1640	}
1641	if strings.HasSuffix(name, ".stubs.system") || strings.HasSuffix(name, "-stubs-systemapi") {
1642		return true, javaSystem
1643	}
1644	if strings.HasSuffix(name, ".stubs.module_lib") || strings.HasSuffix(name, "-stubs-module_libs_api") {
1645		return true, javaModule
1646	}
1647	if strings.HasSuffix(name, ".stubs.test") {
1648		return true, javaSystem
1649	}
1650	return false, javaPlatform
1651}
1652
1653// java_sdk_library is a special Java library that provides optional platform APIs to apps.
1654// In practice, it can be viewed as a combination of several modules: 1) stubs library that clients
1655// are linked against to, 2) droiddoc module that internally generates API stubs source files,
1656// 3) the real runtime shared library that implements the APIs, and 4) XML file for adding
1657// the runtime lib to the classpath at runtime if requested via <uses-library>.
1658func SdkLibraryFactory() android.Module {
1659	module := &SdkLibrary{}
1660
1661	// Initialize information common between source and prebuilt.
1662	module.initCommon(&module.ModuleBase)
1663
1664	module.InitSdkLibraryProperties()
1665	android.InitApexModule(module)
1666	InitJavaModule(module, android.HostAndDeviceSupported)
1667
1668	// Initialize the map from scope to scope specific properties.
1669	scopeToProperties := make(map[*apiScope]*ApiScopeProperties)
1670	for _, scope := range allApiScopes {
1671		scopeToProperties[scope] = scope.scopeSpecificProperties(module)
1672	}
1673	module.scopeToProperties = scopeToProperties
1674
1675	// Add the properties containing visibility rules so that they are checked.
1676	android.AddVisibilityProperty(module, "impl_library_visibility", &module.sdkLibraryProperties.Impl_library_visibility)
1677	android.AddVisibilityProperty(module, "stubs_library_visibility", &module.sdkLibraryProperties.Stubs_library_visibility)
1678	android.AddVisibilityProperty(module, "stubs_source_visibility", &module.sdkLibraryProperties.Stubs_source_visibility)
1679
1680	module.SetDefaultableHook(func(ctx android.DefaultableHookContext) {
1681		// If no implementation is required then it cannot be used as a shared library
1682		// either.
1683		if !module.requiresRuntimeImplementationLibrary() {
1684			// If shared_library has been explicitly set to true then it is incompatible
1685			// with api_only: true.
1686			if proptools.Bool(module.commonSdkLibraryProperties.Shared_library) {
1687				ctx.PropertyErrorf("api_only/shared_library", "inconsistent settings, shared_library and api_only cannot both be true")
1688			}
1689			// Set shared_library: false.
1690			module.commonSdkLibraryProperties.Shared_library = proptools.BoolPtr(false)
1691		}
1692
1693		if module.initCommonAfterDefaultsApplied(ctx) {
1694			module.CreateInternalModules(ctx)
1695		}
1696	})
1697	return module
1698}
1699
1700//
1701// SDK library prebuilts
1702//
1703
1704// Properties associated with each api scope.
1705type sdkLibraryScopeProperties struct {
1706	Jars []string `android:"path"`
1707
1708	Sdk_version *string
1709
1710	// List of shared java libs that this module has dependencies to
1711	Libs []string
1712
1713	// The stubs source.
1714	Stub_srcs []string `android:"path"`
1715
1716	// The current.txt
1717	Current_api *string `android:"path"`
1718
1719	// The removed.txt
1720	Removed_api *string `android:"path"`
1721}
1722
1723type sdkLibraryImportProperties struct {
1724	// List of shared java libs, common to all scopes, that this module has
1725	// dependencies to
1726	Libs []string
1727}
1728
1729type SdkLibraryImport struct {
1730	android.ModuleBase
1731	android.DefaultableModuleBase
1732	prebuilt android.Prebuilt
1733	android.ApexModuleBase
1734	android.SdkBase
1735
1736	properties sdkLibraryImportProperties
1737
1738	// Map from api scope to the scope specific property structure.
1739	scopeProperties map[*apiScope]*sdkLibraryScopeProperties
1740
1741	commonToSdkLibraryAndImport
1742
1743	// The reference to the implementation library created by the source module.
1744	// Is nil if the source module does not exist.
1745	implLibraryModule *Library
1746
1747	// The reference to the xml permissions module created by the source module.
1748	// Is nil if the source module does not exist.
1749	xmlPermissionsFileModule *sdkLibraryXml
1750}
1751
1752var _ SdkLibraryDependency = (*SdkLibraryImport)(nil)
1753
1754// The type of a structure that contains a field of type sdkLibraryScopeProperties
1755// for each apiscope in allApiScopes, e.g. something like:
1756// struct {
1757//   Public sdkLibraryScopeProperties
1758//   System sdkLibraryScopeProperties
1759//   ...
1760// }
1761var allScopeStructType = createAllScopePropertiesStructType()
1762
1763// Dynamically create a structure type for each apiscope in allApiScopes.
1764func createAllScopePropertiesStructType() reflect.Type {
1765	var fields []reflect.StructField
1766	for _, apiScope := range allApiScopes {
1767		field := reflect.StructField{
1768			Name: apiScope.fieldName,
1769			Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
1770		}
1771		fields = append(fields, field)
1772	}
1773
1774	return reflect.StructOf(fields)
1775}
1776
1777// Create an instance of the scope specific structure type and return a map
1778// from apiscope to a pointer to each scope specific field.
1779func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
1780	allScopePropertiesPtr := reflect.New(allScopeStructType)
1781	allScopePropertiesStruct := allScopePropertiesPtr.Elem()
1782	scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)
1783
1784	for _, apiScope := range allApiScopes {
1785		field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
1786		scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
1787	}
1788
1789	return allScopePropertiesPtr.Interface(), scopeProperties
1790}
1791
1792// java_sdk_library_import imports a prebuilt java_sdk_library.
1793func sdkLibraryImportFactory() android.Module {
1794	module := &SdkLibraryImport{}
1795
1796	allScopeProperties, scopeToProperties := createPropertiesInstance()
1797	module.scopeProperties = scopeToProperties
1798	module.AddProperties(&module.properties, allScopeProperties)
1799
1800	// Initialize information common between source and prebuilt.
1801	module.initCommon(&module.ModuleBase)
1802
1803	android.InitPrebuiltModule(module, &[]string{""})
1804	android.InitApexModule(module)
1805	android.InitSdkAwareModule(module)
1806	InitJavaModule(module, android.HostAndDeviceSupported)
1807
1808	module.SetDefaultableHook(func(mctx android.DefaultableHookContext) {
1809		if module.initCommonAfterDefaultsApplied(mctx) {
1810			module.createInternalModules(mctx)
1811		}
1812	})
1813	return module
1814}
1815
1816func (module *SdkLibraryImport) Prebuilt() *android.Prebuilt {
1817	return &module.prebuilt
1818}
1819
1820func (module *SdkLibraryImport) Name() string {
1821	return module.prebuilt.Name(module.ModuleBase.Name())
1822}
1823
1824func (module *SdkLibraryImport) createInternalModules(mctx android.DefaultableHookContext) {
1825
1826	// If the build is configured to use prebuilts then force this to be preferred.
1827	if mctx.Config().UnbundledBuildUsePrebuiltSdks() {
1828		module.prebuilt.ForcePrefer()
1829	}
1830
1831	for apiScope, scopeProperties := range module.scopeProperties {
1832		if len(scopeProperties.Jars) == 0 {
1833			continue
1834		}
1835
1836		module.createJavaImportForStubs(mctx, apiScope, scopeProperties)
1837
1838		if len(scopeProperties.Stub_srcs) > 0 {
1839			module.createPrebuiltStubsSources(mctx, apiScope, scopeProperties)
1840		}
1841	}
1842
1843	javaSdkLibraries := javaSdkLibraries(mctx.Config())
1844	javaSdkLibrariesLock.Lock()
1845	defer javaSdkLibrariesLock.Unlock()
1846	*javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
1847}
1848
1849func (module *SdkLibraryImport) createJavaImportForStubs(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
1850	// Creates a java import for the jar with ".stubs" suffix
1851	props := struct {
1852		Name        *string
1853		Sdk_version *string
1854		Libs        []string
1855		Jars        []string
1856		Prefer      *bool
1857	}{}
1858	props.Name = proptools.StringPtr(module.stubsLibraryModuleName(apiScope))
1859	props.Sdk_version = scopeProperties.Sdk_version
1860	// Prepend any of the libs from the legacy public properties to the libs for each of the
1861	// scopes to avoid having to duplicate them in each scope.
1862	props.Libs = append(module.properties.Libs, scopeProperties.Libs...)
1863	props.Jars = scopeProperties.Jars
1864
1865	// The imports are preferred if the java_sdk_library_import is preferred.
1866	props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
1867
1868	mctx.CreateModule(ImportFactory, &props, module.sdkComponentPropertiesForChildLibrary())
1869}
1870
1871func (module *SdkLibraryImport) createPrebuiltStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope, scopeProperties *sdkLibraryScopeProperties) {
1872	props := struct {
1873		Name   *string
1874		Srcs   []string
1875		Prefer *bool
1876	}{}
1877	props.Name = proptools.StringPtr(module.stubsSourceModuleName(apiScope))
1878	props.Srcs = scopeProperties.Stub_srcs
1879	mctx.CreateModule(PrebuiltStubsSourcesFactory, &props)
1880
1881	// The stubs source is preferred if the java_sdk_library_import is preferred.
1882	props.Prefer = proptools.BoolPtr(module.prebuilt.Prefer())
1883}
1884
1885func (module *SdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
1886	for apiScope, scopeProperties := range module.scopeProperties {
1887		if len(scopeProperties.Jars) == 0 {
1888			continue
1889		}
1890
1891		// Add dependencies to the prebuilt stubs library
1892		ctx.AddVariationDependencies(nil, apiScope.stubsTag, module.stubsLibraryModuleName(apiScope))
1893
1894		if len(scopeProperties.Stub_srcs) > 0 {
1895			// Add dependencies to the prebuilt stubs source library
1896			ctx.AddVariationDependencies(nil, apiScope.stubsSourceTag, module.stubsSourceModuleName(apiScope))
1897		}
1898	}
1899
1900	implName := module.implLibraryModuleName()
1901	if ctx.OtherModuleExists(implName) {
1902		ctx.AddVariationDependencies(nil, implLibraryTag, implName)
1903
1904		xmlPermissionsModuleName := module.xmlPermissionsModuleName()
1905		if module.sharedLibrary() && ctx.OtherModuleExists(xmlPermissionsModuleName) {
1906			// Add dependency to the rule for generating the xml permissions file
1907			ctx.AddDependency(module, xmlPermissionsFileTag, xmlPermissionsModuleName)
1908		}
1909	}
1910}
1911
1912func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
1913	depTag := mctx.OtherModuleDependencyTag(dep)
1914	if depTag == xmlPermissionsFileTag {
1915		return true
1916	}
1917
1918	// None of the other dependencies of the java_sdk_library_import are in the same apex
1919	// as the one that references this module.
1920	return false
1921}
1922
1923func (module *SdkLibraryImport) OutputFiles(tag string) (android.Paths, error) {
1924	return module.commonOutputFiles(tag)
1925}
1926
1927func (module *SdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1928	// Record the paths to the prebuilt stubs library and stubs source.
1929	ctx.VisitDirectDeps(func(to android.Module) {
1930		tag := ctx.OtherModuleDependencyTag(to)
1931
1932		// Extract information from any of the scope specific dependencies.
1933		if scopeTag, ok := tag.(scopeDependencyTag); ok {
1934			apiScope := scopeTag.apiScope
1935			scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
1936
1937			// Extract information from the dependency. The exact information extracted
1938			// is determined by the nature of the dependency which is determined by the tag.
1939			scopeTag.extractDepInfo(ctx, to, scopePaths)
1940		} else if tag == implLibraryTag {
1941			if implLibrary, ok := to.(*Library); ok {
1942				module.implLibraryModule = implLibrary
1943			} else {
1944				ctx.ModuleErrorf("implementation library must be of type *java.Library but was %T", to)
1945			}
1946		} else if tag == xmlPermissionsFileTag {
1947			if xmlPermissionsFileModule, ok := to.(*sdkLibraryXml); ok {
1948				module.xmlPermissionsFileModule = xmlPermissionsFileModule
1949			} else {
1950				ctx.ModuleErrorf("xml permissions file module must be of type *sdkLibraryXml but was %T", to)
1951			}
1952		}
1953	})
1954
1955	// Populate the scope paths with information from the properties.
1956	for apiScope, scopeProperties := range module.scopeProperties {
1957		if len(scopeProperties.Jars) == 0 {
1958			continue
1959		}
1960
1961		paths := module.getScopePathsCreateIfNeeded(apiScope)
1962		paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
1963		paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
1964	}
1965}
1966
1967func (module *SdkLibraryImport) sdkJars(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
1968
1969	// For consistency with SdkLibrary make the implementation jar available to libraries that
1970	// are within the same APEX.
1971	implLibraryModule := module.implLibraryModule
1972	if implLibraryModule != nil && withinSameApexAs(module, ctx.Module()) {
1973		if headerJars {
1974			return implLibraryModule.HeaderJars()
1975		} else {
1976			return implLibraryModule.ImplementationJars()
1977		}
1978	}
1979
1980	return module.selectHeaderJarsForSdkVersion(ctx, sdkVersion)
1981}
1982
1983// to satisfy SdkLibraryDependency interface
1984func (module *SdkLibraryImport) SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
1985	// This module is just a wrapper for the prebuilt stubs.
1986	return module.sdkJars(ctx, sdkVersion, true)
1987}
1988
1989// to satisfy SdkLibraryDependency interface
1990func (module *SdkLibraryImport) SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths {
1991	// This module is just a wrapper for the stubs.
1992	return module.sdkJars(ctx, sdkVersion, false)
1993}
1994
1995// to satisfy apex.javaDependency interface
1996func (module *SdkLibraryImport) DexJar() android.Path {
1997	if module.implLibraryModule == nil {
1998		return nil
1999	} else {
2000		return module.implLibraryModule.DexJar()
2001	}
2002}
2003
2004// to satisfy apex.javaDependency interface
2005func (module *SdkLibraryImport) JacocoReportClassesFile() android.Path {
2006	if module.implLibraryModule == nil {
2007		return nil
2008	} else {
2009		return module.implLibraryModule.JacocoReportClassesFile()
2010	}
2011}
2012
2013// to satisfy apex.javaDependency interface
2014func (module *SdkLibraryImport) LintDepSets() LintDepSets {
2015	if module.implLibraryModule == nil {
2016		return LintDepSets{}
2017	} else {
2018		return module.implLibraryModule.LintDepSets()
2019	}
2020}
2021
2022// to satisfy apex.javaDependency interface
2023func (module *SdkLibraryImport) Stem() string {
2024	return module.BaseModuleName()
2025}
2026
2027var _ ApexDependency = (*SdkLibraryImport)(nil)
2028
2029// to satisfy java.ApexDependency interface
2030func (module *SdkLibraryImport) HeaderJars() android.Paths {
2031	if module.implLibraryModule == nil {
2032		return nil
2033	} else {
2034		return module.implLibraryModule.HeaderJars()
2035	}
2036}
2037
2038// to satisfy java.ApexDependency interface
2039func (module *SdkLibraryImport) ImplementationAndResourcesJars() android.Paths {
2040	if module.implLibraryModule == nil {
2041		return nil
2042	} else {
2043		return module.implLibraryModule.ImplementationAndResourcesJars()
2044	}
2045}
2046
2047//
2048// java_sdk_library_xml
2049//
2050type sdkLibraryXml struct {
2051	android.ModuleBase
2052	android.DefaultableModuleBase
2053	android.ApexModuleBase
2054
2055	properties sdkLibraryXmlProperties
2056
2057	outputFilePath android.OutputPath
2058	installDirPath android.InstallPath
2059}
2060
2061type sdkLibraryXmlProperties struct {
2062	// canonical name of the lib
2063	Lib_name *string
2064}
2065
2066// java_sdk_library_xml builds the permission xml file for a java_sdk_library.
2067// Not to be used directly by users. java_sdk_library internally uses this.
2068func sdkLibraryXmlFactory() android.Module {
2069	module := &sdkLibraryXml{}
2070
2071	module.AddProperties(&module.properties)
2072
2073	android.InitApexModule(module)
2074	android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
2075
2076	return module
2077}
2078
2079// from android.PrebuiltEtcModule
2080func (module *sdkLibraryXml) SubDir() string {
2081	return "permissions"
2082}
2083
2084// from android.PrebuiltEtcModule
2085func (module *sdkLibraryXml) OutputFile() android.OutputPath {
2086	return module.outputFilePath
2087}
2088
2089// from android.ApexModule
2090func (module *sdkLibraryXml) AvailableFor(what string) bool {
2091	return true
2092}
2093
2094func (module *sdkLibraryXml) DepsMutator(ctx android.BottomUpMutatorContext) {
2095	// do nothing
2096}
2097
2098// File path to the runtime implementation library
2099func (module *sdkLibraryXml) implPath() string {
2100	implName := proptools.String(module.properties.Lib_name)
2101	if apexName := module.ApexName(); apexName != "" {
2102		// TODO(b/146468504): ApexName() is only a soong module name, not apex name.
2103		// In most cases, this works fine. But when apex_name is set or override_apex is used
2104		// this can be wrong.
2105		return fmt.Sprintf("/apex/%s/javalib/%s.jar", apexName, implName)
2106	}
2107	partition := "system"
2108	if module.SocSpecific() {
2109		partition = "vendor"
2110	} else if module.DeviceSpecific() {
2111		partition = "odm"
2112	} else if module.ProductSpecific() {
2113		partition = "product"
2114	} else if module.SystemExtSpecific() {
2115		partition = "system_ext"
2116	}
2117	return "/" + partition + "/framework/" + implName + ".jar"
2118}
2119
2120func (module *sdkLibraryXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2121	libName := proptools.String(module.properties.Lib_name)
2122	xmlContent := fmt.Sprintf(permissionsTemplate, libName, module.implPath())
2123
2124	module.outputFilePath = android.PathForModuleOut(ctx, libName+".xml").OutputPath
2125	rule := android.NewRuleBuilder()
2126	rule.Command().
2127		Text("/bin/bash -c \"echo -e '" + xmlContent + "'\" > ").
2128		Output(module.outputFilePath)
2129
2130	rule.Build(pctx, ctx, "java_sdk_xml", "Permission XML")
2131
2132	module.installDirPath = android.PathForModuleInstall(ctx, "etc", module.SubDir())
2133}
2134
2135func (module *sdkLibraryXml) AndroidMkEntries() []android.AndroidMkEntries {
2136	if !module.IsForPlatform() {
2137		return []android.AndroidMkEntries{android.AndroidMkEntries{
2138			Disabled: true,
2139		}}
2140	}
2141
2142	return []android.AndroidMkEntries{android.AndroidMkEntries{
2143		Class:      "ETC",
2144		OutputFile: android.OptionalPathForPath(module.outputFilePath),
2145		ExtraEntries: []android.AndroidMkExtraEntriesFunc{
2146			func(entries *android.AndroidMkEntries) {
2147				entries.SetString("LOCAL_MODULE_TAGS", "optional")
2148				entries.SetString("LOCAL_MODULE_PATH", module.installDirPath.ToMakePath().String())
2149				entries.SetString("LOCAL_INSTALLED_MODULE_STEM", module.outputFilePath.Base())
2150			},
2151		},
2152	}}
2153}
2154
2155type sdkLibrarySdkMemberType struct {
2156	android.SdkMemberTypeBase
2157}
2158
2159func (s *sdkLibrarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2160	mctx.AddVariationDependencies(nil, dependencyTag, names...)
2161}
2162
2163func (s *sdkLibrarySdkMemberType) IsInstance(module android.Module) bool {
2164	_, ok := module.(*SdkLibrary)
2165	return ok
2166}
2167
2168func (s *sdkLibrarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2169	return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_sdk_library_import")
2170}
2171
2172func (s *sdkLibrarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2173	return &sdkLibrarySdkMemberProperties{}
2174}
2175
2176type sdkLibrarySdkMemberProperties struct {
2177	android.SdkMemberPropertiesBase
2178
2179	// Scope to per scope properties.
2180	Scopes map[*apiScope]scopeProperties
2181
2182	// Additional libraries that the exported stubs libraries depend upon.
2183	Libs []string
2184
2185	// The Java stubs source files.
2186	Stub_srcs []string
2187
2188	// The naming scheme.
2189	Naming_scheme *string
2190
2191	// True if the java_sdk_library_import is for a shared library, false
2192	// otherwise.
2193	Shared_library *bool
2194}
2195
2196type scopeProperties struct {
2197	Jars           android.Paths
2198	StubsSrcJar    android.Path
2199	CurrentApiFile android.Path
2200	RemovedApiFile android.Path
2201	SdkVersion     string
2202}
2203
2204func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
2205	sdk := variant.(*SdkLibrary)
2206
2207	s.Scopes = make(map[*apiScope]scopeProperties)
2208	for _, apiScope := range allApiScopes {
2209		paths := sdk.findScopePaths(apiScope)
2210		if paths == nil {
2211			continue
2212		}
2213
2214		jars := paths.stubsImplPath
2215		if len(jars) > 0 {
2216			properties := scopeProperties{}
2217			properties.Jars = jars
2218			properties.SdkVersion = sdk.sdkVersionForStubsLibrary(ctx.SdkModuleContext(), apiScope)
2219			properties.StubsSrcJar = paths.stubsSrcJar.Path()
2220			if paths.currentApiFilePath.Valid() {
2221				properties.CurrentApiFile = paths.currentApiFilePath.Path()
2222			}
2223			if paths.removedApiFilePath.Valid() {
2224				properties.RemovedApiFile = paths.removedApiFilePath.Path()
2225			}
2226			s.Scopes[apiScope] = properties
2227		}
2228	}
2229
2230	s.Libs = sdk.properties.Libs
2231	s.Naming_scheme = sdk.commonSdkLibraryProperties.Naming_scheme
2232	s.Shared_library = proptools.BoolPtr(sdk.sharedLibrary())
2233}
2234
2235func (s *sdkLibrarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
2236	if s.Naming_scheme != nil {
2237		propertySet.AddProperty("naming_scheme", proptools.String(s.Naming_scheme))
2238	}
2239	if s.Shared_library != nil {
2240		propertySet.AddProperty("shared_library", *s.Shared_library)
2241	}
2242
2243	for _, apiScope := range allApiScopes {
2244		if properties, ok := s.Scopes[apiScope]; ok {
2245			scopeSet := propertySet.AddPropertySet(apiScope.propertyName)
2246
2247			scopeDir := filepath.Join("sdk_library", s.OsPrefix(), apiScope.name)
2248
2249			var jars []string
2250			for _, p := range properties.Jars {
2251				dest := filepath.Join(scopeDir, ctx.Name()+"-stubs.jar")
2252				ctx.SnapshotBuilder().CopyToSnapshot(p, dest)
2253				jars = append(jars, dest)
2254			}
2255			scopeSet.AddProperty("jars", jars)
2256
2257			// Merge the stubs source jar into the snapshot zip so that when it is unpacked
2258			// the source files are also unpacked.
2259			snapshotRelativeDir := filepath.Join(scopeDir, ctx.Name()+"_stub_sources")
2260			ctx.SnapshotBuilder().UnzipToSnapshot(properties.StubsSrcJar, snapshotRelativeDir)
2261			scopeSet.AddProperty("stub_srcs", []string{snapshotRelativeDir})
2262
2263			if properties.CurrentApiFile != nil {
2264				currentApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+".txt")
2265				ctx.SnapshotBuilder().CopyToSnapshot(properties.CurrentApiFile, currentApiSnapshotPath)
2266				scopeSet.AddProperty("current_api", currentApiSnapshotPath)
2267			}
2268
2269			if properties.RemovedApiFile != nil {
2270				removedApiSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"-removed.txt")
2271				ctx.SnapshotBuilder().CopyToSnapshot(properties.RemovedApiFile, removedApiSnapshotPath)
2272				scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
2273			}
2274
2275			if properties.SdkVersion != "" {
2276				scopeSet.AddProperty("sdk_version", properties.SdkVersion)
2277			}
2278		}
2279	}
2280
2281	if len(s.Libs) > 0 {
2282		propertySet.AddPropertyWithTag("libs", s.Libs, ctx.SnapshotBuilder().SdkMemberReferencePropertyTag(false))
2283	}
2284}
2285