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 | |
| 15 | package aconfig |
| 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
| 19 | "android/soong/java" |
| 20 | "fmt" |
| 21 | "github.com/google/blueprint" |
| 22 | ) |
| 23 | |
| 24 | type declarationsTagType struct { |
| 25 | blueprint.BaseDependencyTag |
| 26 | } |
| 27 | |
| 28 | var declarationsTag = declarationsTagType{} |
| 29 | |
| 30 | type JavaAconfigDeclarationsLibraryProperties struct { |
| 31 | // name of the aconfig_declarations module to generate a library for |
| 32 | Aconfig_declarations string |
Joe Onorato | b7c294a | 2023-07-28 05:30:25 -0700 | [diff] [blame] | 33 | |
| 34 | // whether to generate test mode version of the library |
| 35 | Test bool |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | type JavaAconfigDeclarationsLibraryCallbacks struct { |
| 39 | properties JavaAconfigDeclarationsLibraryProperties |
| 40 | } |
| 41 | |
| 42 | func JavaDeclarationsLibraryFactory() android.Module { |
| 43 | callbacks := &JavaAconfigDeclarationsLibraryCallbacks{} |
| 44 | return java.GeneratedJavaLibraryModuleFactory("java_aconfig_library", callbacks, &callbacks.properties) |
| 45 | } |
| 46 | |
| 47 | func (callbacks *JavaAconfigDeclarationsLibraryCallbacks) DepsMutator(module *java.GeneratedJavaLibraryModule, ctx android.BottomUpMutatorContext) { |
| 48 | declarations := callbacks.properties.Aconfig_declarations |
| 49 | if len(declarations) == 0 { |
| 50 | // TODO: Add test for this case |
| 51 | ctx.PropertyErrorf("aconfig_declarations", "aconfig_declarations property required") |
| 52 | } else { |
| 53 | ctx.AddDependency(ctx.Module(), declarationsTag, declarations) |
| 54 | } |
Joe Onorato | 8f75585 | 2023-08-25 20:23:32 -0700 | [diff] [blame^] | 55 | |
| 56 | // Add aconfig-annotations-lib as a dependency for the optimization / code stripping annotations |
| 57 | module.AddSharedLibrary("aconfig-annotations-lib") |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Joe Onorato | 6fe59eb | 2023-07-16 13:20:33 -0700 | [diff] [blame] | 60 | func (callbacks *JavaAconfigDeclarationsLibraryCallbacks) GenerateSourceJarBuildActions(module *java.GeneratedJavaLibraryModule, ctx android.ModuleContext) android.Path { |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 61 | // Get the values that came from the global RELEASE_ACONFIG_VALUE_SETS flag |
| 62 | declarationsModules := ctx.GetDirectDepsWithTag(declarationsTag) |
| 63 | if len(declarationsModules) != 1 { |
| 64 | panic(fmt.Errorf("Exactly one aconfig_declarations property required")) |
| 65 | } |
| 66 | declarations := ctx.OtherModuleProvider(declarationsModules[0], declarationsProviderKey).(declarationsProviderData) |
| 67 | |
Joe Onorato | 6fe59eb | 2023-07-16 13:20:33 -0700 | [diff] [blame] | 68 | // Generate the action to build the srcjar |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 69 | srcJarPath := android.PathForModuleGen(ctx, ctx.ModuleName()+".srcjar") |
Joe Onorato | b7c294a | 2023-07-28 05:30:25 -0700 | [diff] [blame] | 70 | var mode string |
| 71 | if callbacks.properties.Test { |
| 72 | mode = "test" |
| 73 | } else { |
| 74 | mode = "production" |
| 75 | } |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 76 | ctx.Build(pctx, android.BuildParams{ |
Joe Onorato | 37f900c | 2023-07-18 16:58:16 -0700 | [diff] [blame] | 77 | Rule: javaRule, |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 78 | Input: declarations.IntermediatePath, |
| 79 | Output: srcJarPath, |
| 80 | Description: "aconfig.srcjar", |
Joe Onorato | b7c294a | 2023-07-28 05:30:25 -0700 | [diff] [blame] | 81 | Args: map[string]string{ |
| 82 | "mode": mode, |
| 83 | }, |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 84 | }) |
| 85 | |
Joe Onorato | 6fe59eb | 2023-07-16 13:20:33 -0700 | [diff] [blame] | 86 | // Tell the java module about the .aconfig files, so they can be propagated up the dependency chain. |
| 87 | // TODO: It would be nice to have that propagation code here instead of on java.Module and java.JavaInfo. |
| 88 | module.AddAconfigIntermediate(declarations.IntermediatePath) |
| 89 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 90 | return srcJarPath |
| 91 | } |