Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 1 | // Copyright 2023 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Yu Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 15 | package codegen |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 16 | |
| 17 | import ( |
Yu Liu | f2b9401 | 2023-09-19 15:09:10 -0700 | [diff] [blame] | 18 | "android/soong/android" |
Yu Liu | f2b9401 | 2023-09-19 15:09:10 -0700 | [diff] [blame] | 19 | "android/soong/java" |
Jihoon Kang | cca3e0c | 2023-11-29 19:35:29 +0000 | [diff] [blame] | 20 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 21 | "github.com/google/blueprint" |
Yu Liu | f2b9401 | 2023-09-19 15:09:10 -0700 | [diff] [blame] | 22 | "github.com/google/blueprint/proptools" |
Zhi Dou | b94c779 | 2024-09-25 21:21:15 +0000 | [diff] [blame] | 23 | "strconv" |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | type declarationsTagType struct { |
| 27 | blueprint.BaseDependencyTag |
| 28 | } |
| 29 | |
| 30 | var declarationsTag = declarationsTagType{} |
| 31 | |
Zhi Dou | 70e2124 | 2023-12-20 23:14:34 +0000 | [diff] [blame] | 32 | var aconfigSupportedModes = []string{"production", "test", "exported", "force-read-only"} |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 33 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 34 | type JavaAconfigDeclarationsLibraryProperties struct { |
| 35 | // name of the aconfig_declarations module to generate a library for |
| 36 | Aconfig_declarations string |
Joe Onorato | b7c294a | 2023-07-28 05:30:25 -0700 | [diff] [blame] | 37 | |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 38 | // default mode is "production", the other accepted modes are: |
| 39 | // "test": to generate test mode version of the library |
| 40 | // "exported": to generate exported mode version of the library |
Zhi Dou | 70e2124 | 2023-12-20 23:14:34 +0000 | [diff] [blame] | 41 | // "force-read-only": to generate force-read-only mode version of the library |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 42 | // an error will be thrown if the mode is not supported |
| 43 | Mode *string |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | type JavaAconfigDeclarationsLibraryCallbacks struct { |
| 47 | properties JavaAconfigDeclarationsLibraryProperties |
| 48 | } |
| 49 | |
| 50 | func JavaDeclarationsLibraryFactory() android.Module { |
| 51 | callbacks := &JavaAconfigDeclarationsLibraryCallbacks{} |
| 52 | return java.GeneratedJavaLibraryModuleFactory("java_aconfig_library", callbacks, &callbacks.properties) |
| 53 | } |
| 54 | |
| 55 | func (callbacks *JavaAconfigDeclarationsLibraryCallbacks) DepsMutator(module *java.GeneratedJavaLibraryModule, ctx android.BottomUpMutatorContext) { |
| 56 | declarations := callbacks.properties.Aconfig_declarations |
| 57 | if len(declarations) == 0 { |
| 58 | // TODO: Add test for this case |
| 59 | ctx.PropertyErrorf("aconfig_declarations", "aconfig_declarations property required") |
| 60 | } else { |
| 61 | ctx.AddDependency(ctx.Module(), declarationsTag, declarations) |
| 62 | } |
Joe Onorato | 8f75585 | 2023-08-25 20:23:32 -0700 | [diff] [blame] | 63 | |
Victor Chang | bf0175e | 2023-12-18 17:36:34 +0000 | [diff] [blame] | 64 | // "libcore_aconfig_flags_lib" module has a circular dependency because the shared libraries |
| 65 | // are built on core_current and the module is used to flag the APIs in the core_current. |
| 66 | // http://b/316554963#comment2 has the details of the circular dependency chain. |
| 67 | // If a java_aconfig_library uses "none" sdk_version, it should include and build these |
| 68 | // annotation files as the shared library themselves. |
| 69 | var addLibraries bool = module.Library.Module.SdkVersion(ctx).Kind != android.SdkNone |
| 70 | if addLibraries { |
| 71 | // Add aconfig-annotations-lib as a dependency for the optimization / code stripping annotations |
| 72 | module.AddSharedLibrary("aconfig-annotations-lib") |
| 73 | // TODO(b/303773055): Remove the annotation after access issue is resolved. |
| 74 | module.AddSharedLibrary("unsupportedappusage") |
Zhi Dou | b94c779 | 2024-09-25 21:21:15 +0000 | [diff] [blame] | 75 | module.AddSharedLibrary("aconfig_storage_reader_java") |
Victor Chang | bf0175e | 2023-12-18 17:36:34 +0000 | [diff] [blame] | 76 | } |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Jihoon Kang | 3921f0b | 2024-03-12 23:51:37 +0000 | [diff] [blame] | 79 | func (callbacks *JavaAconfigDeclarationsLibraryCallbacks) GenerateSourceJarBuildActions(module *java.GeneratedJavaLibraryModule, ctx android.ModuleContext) (android.Path, android.Path) { |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 80 | // Get the values that came from the global RELEASE_ACONFIG_VALUE_SETS flag |
| 81 | declarationsModules := ctx.GetDirectDepsWithTag(declarationsTag) |
| 82 | if len(declarationsModules) != 1 { |
Cole Faust | 779d41c | 2024-06-14 11:14:33 -0700 | [diff] [blame] | 83 | panic("Exactly one aconfig_declarations property required") |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 84 | } |
LaMont Jones | aa005ae | 2023-12-19 19:01:57 +0000 | [diff] [blame] | 85 | declarations, _ := android.OtherModuleProvider(ctx, declarationsModules[0], android.AconfigDeclarationsProviderKey) |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 86 | |
Joe Onorato | 6fe59eb | 2023-07-16 13:20:33 -0700 | [diff] [blame] | 87 | // Generate the action to build the srcjar |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 88 | srcJarPath := android.PathForModuleGen(ctx, ctx.ModuleName()+".srcjar") |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 89 | |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 90 | mode := proptools.StringDefault(callbacks.properties.Mode, "production") |
| 91 | if !isModeSupported(mode) { |
| 92 | ctx.PropertyErrorf("mode", "%q is not a supported mode", mode) |
| 93 | } |
Zhi Dou | e11319d | 2024-03-05 22:21:03 +0000 | [diff] [blame] | 94 | |
| 95 | if mode == "exported" && !declarations.Exportable { |
| 96 | // if mode is exported, the corresponding aconfig_declaration must mark its |
| 97 | // exportable property true |
| 98 | ctx.PropertyErrorf("mode", "exported mode requires its aconfig_declaration has exportable prop true") |
| 99 | } |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 100 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 101 | ctx.Build(pctx, android.BuildParams{ |
Joe Onorato | 37f900c | 2023-07-18 16:58:16 -0700 | [diff] [blame] | 102 | Rule: javaRule, |
Jihoon Kang | cca3e0c | 2023-11-29 19:35:29 +0000 | [diff] [blame] | 103 | Input: declarations.IntermediateCacheOutputPath, |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 104 | Output: srcJarPath, |
| 105 | Description: "aconfig.srcjar", |
Joe Onorato | b7c294a | 2023-07-28 05:30:25 -0700 | [diff] [blame] | 106 | Args: map[string]string{ |
Zhi Dou | b94c779 | 2024-09-25 21:21:15 +0000 | [diff] [blame] | 107 | "mode": mode, |
| 108 | "debug": strconv.FormatBool(ctx.Config().ReleaseReadFromNewStorage()), |
Joe Onorato | b7c294a | 2023-07-28 05:30:25 -0700 | [diff] [blame] | 109 | }, |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 110 | }) |
| 111 | |
Zi Wang | 0e5d16c | 2024-02-08 06:19:34 +0000 | [diff] [blame] | 112 | 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", "") |
Jeff DeCew | 4fd1522 | 2024-04-24 14:25:38 +0000 | [diff] [blame] | 119 | module.AddJarJarRenameRule(declarations.Package+".CustomFeatureFlags", "") |
Zi Wang | 0e5d16c | 2024-02-08 06:19:34 +0000 | [diff] [blame] | 120 | module.AddJarJarRenameRule(declarations.Package+".FakeFeatureFlagsImpl", "") |
| 121 | } |
Joe Onorato | 349ae8d | 2024-02-05 22:46:00 +0000 | [diff] [blame] | 122 | |
Yu Liu | 67a2842 | 2024-03-05 00:36:31 +0000 | [diff] [blame] | 123 | android.SetProvider(ctx, android.CodegenInfoProvider, android.CodegenInfo{ |
Jihoon Kang | 2a43e56 | 2024-02-12 19:05:12 +0000 | [diff] [blame] | 124 | AconfigDeclarations: []string{declarationsModules[0].Name()}, |
| 125 | IntermediateCacheOutputPaths: android.Paths{declarations.IntermediateCacheOutputPath}, |
| 126 | Srcjars: android.Paths{srcJarPath}, |
Yu Liu | 67a2842 | 2024-03-05 00:36:31 +0000 | [diff] [blame] | 127 | ModeInfos: map[string]android.ModeInfo{ |
| 128 | ctx.ModuleName(): { |
| 129 | Container: declarations.Container, |
| 130 | Mode: mode, |
| 131 | }}, |
Jihoon Kang | 2a43e56 | 2024-02-12 19:05:12 +0000 | [diff] [blame] | 132 | }) |
| 133 | |
Jihoon Kang | 3921f0b | 2024-03-12 23:51:37 +0000 | [diff] [blame] | 134 | return srcJarPath, declarations.IntermediateCacheOutputPath |
| 135 | } |
| 136 | |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 137 | func isModeSupported(mode string) bool { |
| 138 | return android.InList(mode, aconfigSupportedModes) |
| 139 | } |