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 | } |
| 55 | } |
| 56 | |
Joe Onorato | 6fe59eb | 2023-07-16 13:20:33 -0700 | [diff] [blame] | 57 | func (callbacks *JavaAconfigDeclarationsLibraryCallbacks) GenerateSourceJarBuildActions(module *java.GeneratedJavaLibraryModule, ctx android.ModuleContext) android.Path { |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 58 | // Get the values that came from the global RELEASE_ACONFIG_VALUE_SETS flag |
| 59 | declarationsModules := ctx.GetDirectDepsWithTag(declarationsTag) |
| 60 | if len(declarationsModules) != 1 { |
| 61 | panic(fmt.Errorf("Exactly one aconfig_declarations property required")) |
| 62 | } |
| 63 | declarations := ctx.OtherModuleProvider(declarationsModules[0], declarationsProviderKey).(declarationsProviderData) |
| 64 | |
Joe Onorato | 6fe59eb | 2023-07-16 13:20:33 -0700 | [diff] [blame] | 65 | // Generate the action to build the srcjar |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 66 | srcJarPath := android.PathForModuleGen(ctx, ctx.ModuleName()+".srcjar") |
Joe Onorato | b7c294a | 2023-07-28 05:30:25 -0700 | [diff] [blame^] | 67 | var mode string |
| 68 | if callbacks.properties.Test { |
| 69 | mode = "test" |
| 70 | } else { |
| 71 | mode = "production" |
| 72 | } |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 73 | ctx.Build(pctx, android.BuildParams{ |
Joe Onorato | 37f900c | 2023-07-18 16:58:16 -0700 | [diff] [blame] | 74 | Rule: javaRule, |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 75 | Input: declarations.IntermediatePath, |
| 76 | Output: srcJarPath, |
| 77 | Description: "aconfig.srcjar", |
Joe Onorato | b7c294a | 2023-07-28 05:30:25 -0700 | [diff] [blame^] | 78 | Args: map[string]string{ |
| 79 | "mode": mode, |
| 80 | }, |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 81 | }) |
| 82 | |
Joe Onorato | 6fe59eb | 2023-07-16 13:20:33 -0700 | [diff] [blame] | 83 | // Tell the java module about the .aconfig files, so they can be propagated up the dependency chain. |
| 84 | // TODO: It would be nice to have that propagation code here instead of on java.Module and java.JavaInfo. |
| 85 | module.AddAconfigIntermediate(declarations.IntermediatePath) |
| 86 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 87 | return srcJarPath |
| 88 | } |