blob: 7d7296e6a1caafaa9728afeb75ea490082773538 [file] [log] [blame]
Joe Onorato981c9262023-06-21 15:16:23 -07001// Copyright 2023 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Yu Liueae7b362023-11-16 17:05:47 -080015package codegen
Joe Onorato981c9262023-06-21 15:16:23 -070016
17import (
Joe Onorato981c9262023-06-21 15:16:23 -070018 "fmt"
Yu Liuf2b94012023-09-19 15:09:10 -070019
Jihoon Kang2a43e562024-02-12 19:05:12 +000020 "android/soong/aconfig"
Yu Liuf2b94012023-09-19 15:09:10 -070021 "android/soong/android"
Yu Liuf2b94012023-09-19 15:09:10 -070022 "android/soong/java"
Jihoon Kangcca3e0c2023-11-29 19:35:29 +000023
Joe Onorato981c9262023-06-21 15:16:23 -070024 "github.com/google/blueprint"
Yu Liuf2b94012023-09-19 15:09:10 -070025 "github.com/google/blueprint/proptools"
Joe Onorato981c9262023-06-21 15:16:23 -070026)
27
28type declarationsTagType struct {
29 blueprint.BaseDependencyTag
30}
31
32var declarationsTag = declarationsTagType{}
33
Zhi Dou70e21242023-12-20 23:14:34 +000034var aconfigSupportedModes = []string{"production", "test", "exported", "force-read-only"}
Zi Wang275f6542023-11-09 14:59:31 -080035
Joe Onorato981c9262023-06-21 15:16:23 -070036type JavaAconfigDeclarationsLibraryProperties struct {
37 // name of the aconfig_declarations module to generate a library for
38 Aconfig_declarations string
Joe Onoratob7c294a2023-07-28 05:30:25 -070039
Zi Wang275f6542023-11-09 14:59:31 -080040 // default mode is "production", the other accepted modes are:
41 // "test": to generate test mode version of the library
42 // "exported": to generate exported mode version of the library
Zhi Dou70e21242023-12-20 23:14:34 +000043 // "force-read-only": to generate force-read-only mode version of the library
Zi Wang275f6542023-11-09 14:59:31 -080044 // an error will be thrown if the mode is not supported
45 Mode *string
Joe Onorato981c9262023-06-21 15:16:23 -070046}
47
48type JavaAconfigDeclarationsLibraryCallbacks struct {
49 properties JavaAconfigDeclarationsLibraryProperties
50}
51
52func JavaDeclarationsLibraryFactory() android.Module {
53 callbacks := &JavaAconfigDeclarationsLibraryCallbacks{}
54 return java.GeneratedJavaLibraryModuleFactory("java_aconfig_library", callbacks, &callbacks.properties)
55}
56
57func (callbacks *JavaAconfigDeclarationsLibraryCallbacks) DepsMutator(module *java.GeneratedJavaLibraryModule, ctx android.BottomUpMutatorContext) {
58 declarations := callbacks.properties.Aconfig_declarations
59 if len(declarations) == 0 {
60 // TODO: Add test for this case
61 ctx.PropertyErrorf("aconfig_declarations", "aconfig_declarations property required")
62 } else {
63 ctx.AddDependency(ctx.Module(), declarationsTag, declarations)
64 }
Joe Onorato8f755852023-08-25 20:23:32 -070065
Victor Changbf0175e2023-12-18 17:36:34 +000066 // "libcore_aconfig_flags_lib" module has a circular dependency because the shared libraries
67 // are built on core_current and the module is used to flag the APIs in the core_current.
68 // http://b/316554963#comment2 has the details of the circular dependency chain.
69 // If a java_aconfig_library uses "none" sdk_version, it should include and build these
70 // annotation files as the shared library themselves.
71 var addLibraries bool = module.Library.Module.SdkVersion(ctx).Kind != android.SdkNone
72 if addLibraries {
73 // Add aconfig-annotations-lib as a dependency for the optimization / code stripping annotations
74 module.AddSharedLibrary("aconfig-annotations-lib")
75 // TODO(b/303773055): Remove the annotation after access issue is resolved.
76 module.AddSharedLibrary("unsupportedappusage")
77 }
Joe Onorato981c9262023-06-21 15:16:23 -070078}
79
Joe Onorato6fe59eb2023-07-16 13:20:33 -070080func (callbacks *JavaAconfigDeclarationsLibraryCallbacks) GenerateSourceJarBuildActions(module *java.GeneratedJavaLibraryModule, ctx android.ModuleContext) android.Path {
Joe Onorato981c9262023-06-21 15:16:23 -070081 // Get the values that came from the global RELEASE_ACONFIG_VALUE_SETS flag
82 declarationsModules := ctx.GetDirectDepsWithTag(declarationsTag)
83 if len(declarationsModules) != 1 {
84 panic(fmt.Errorf("Exactly one aconfig_declarations property required"))
85 }
LaMont Jonesaa005ae2023-12-19 19:01:57 +000086 declarations, _ := android.OtherModuleProvider(ctx, declarationsModules[0], android.AconfigDeclarationsProviderKey)
Joe Onorato981c9262023-06-21 15:16:23 -070087
Joe Onorato6fe59eb2023-07-16 13:20:33 -070088 // Generate the action to build the srcjar
Joe Onorato981c9262023-06-21 15:16:23 -070089 srcJarPath := android.PathForModuleGen(ctx, ctx.ModuleName()+".srcjar")
Zi Wang275f6542023-11-09 14:59:31 -080090
Zi Wang275f6542023-11-09 14:59:31 -080091 mode := proptools.StringDefault(callbacks.properties.Mode, "production")
92 if !isModeSupported(mode) {
93 ctx.PropertyErrorf("mode", "%q is not a supported mode", mode)
94 }
Zi Wang0e5d16c2024-02-08 06:19:34 +000095 // TODO: uncomment this part after internal clean up
96 //if mode == "exported" && !declarations.Exportable {
97 // // if mode is exported, the corresponding aconfig_declaration must mark its
98 // // exportable property true
99 // ctx.PropertyErrorf("mode", "exported mode requires its aconfig_declaration has exportable prop true")
100 //}
Zi Wang275f6542023-11-09 14:59:31 -0800101
Joe Onorato981c9262023-06-21 15:16:23 -0700102 ctx.Build(pctx, android.BuildParams{
Joe Onorato37f900c2023-07-18 16:58:16 -0700103 Rule: javaRule,
Jihoon Kangcca3e0c2023-11-29 19:35:29 +0000104 Input: declarations.IntermediateCacheOutputPath,
Joe Onorato981c9262023-06-21 15:16:23 -0700105 Output: srcJarPath,
106 Description: "aconfig.srcjar",
Joe Onoratob7c294a2023-07-28 05:30:25 -0700107 Args: map[string]string{
108 "mode": mode,
109 },
Joe Onorato981c9262023-06-21 15:16:23 -0700110 })
111
Zi Wang0e5d16c2024-02-08 06:19:34 +0000112 if declarations.Exportable {
113 // Mark our generated code as possibly needing jarjar repackaging
114 // The repackaging only happens when the corresponding aconfig_declaration
115 // has property exportable true
116 module.AddJarJarRenameRule(declarations.Package+".Flags", "")
117 module.AddJarJarRenameRule(declarations.Package+".FeatureFlags", "")
118 module.AddJarJarRenameRule(declarations.Package+".FeatureFlagsImpl", "")
119 module.AddJarJarRenameRule(declarations.Package+".FakeFeatureFlagsImpl", "")
120 }
Joe Onorato349ae8d2024-02-05 22:46:00 +0000121
Jihoon Kang2a43e562024-02-12 19:05:12 +0000122 android.SetProvider(ctx, aconfig.CodegenInfoProvider, aconfig.CodegenInfo{
123 AconfigDeclarations: []string{declarationsModules[0].Name()},
124 IntermediateCacheOutputPaths: android.Paths{declarations.IntermediateCacheOutputPath},
125 Srcjars: android.Paths{srcJarPath},
126 })
127
Joe Onorato981c9262023-06-21 15:16:23 -0700128 return srcJarPath
129}
Yu Liuf2b94012023-09-19 15:09:10 -0700130
Zi Wang275f6542023-11-09 14:59:31 -0800131func isModeSupported(mode string) bool {
132 return android.InList(mode, aconfigSupportedModes)
133}