blob: 53f8bd1ba0ccda8ca84d01e579e5236cf164bd0b [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
15package aconfig
16
17import (
18 "android/soong/android"
19 "android/soong/java"
20 "fmt"
21 "github.com/google/blueprint"
22)
23
24type declarationsTagType struct {
25 blueprint.BaseDependencyTag
26}
27
28var declarationsTag = declarationsTagType{}
29
30type JavaAconfigDeclarationsLibraryProperties struct {
31 // name of the aconfig_declarations module to generate a library for
32 Aconfig_declarations string
Joe Onoratob7c294a2023-07-28 05:30:25 -070033
34 // whether to generate test mode version of the library
35 Test bool
Joe Onorato981c9262023-06-21 15:16:23 -070036}
37
38type JavaAconfigDeclarationsLibraryCallbacks struct {
39 properties JavaAconfigDeclarationsLibraryProperties
40}
41
42func JavaDeclarationsLibraryFactory() android.Module {
43 callbacks := &JavaAconfigDeclarationsLibraryCallbacks{}
44 return java.GeneratedJavaLibraryModuleFactory("java_aconfig_library", callbacks, &callbacks.properties)
45}
46
47func (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 Onorato6fe59eb2023-07-16 13:20:33 -070057func (callbacks *JavaAconfigDeclarationsLibraryCallbacks) GenerateSourceJarBuildActions(module *java.GeneratedJavaLibraryModule, ctx android.ModuleContext) android.Path {
Joe Onorato981c9262023-06-21 15:16:23 -070058 // 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 Onorato6fe59eb2023-07-16 13:20:33 -070065 // Generate the action to build the srcjar
Joe Onorato981c9262023-06-21 15:16:23 -070066 srcJarPath := android.PathForModuleGen(ctx, ctx.ModuleName()+".srcjar")
Joe Onoratob7c294a2023-07-28 05:30:25 -070067 var mode string
68 if callbacks.properties.Test {
69 mode = "test"
70 } else {
71 mode = "production"
72 }
Joe Onorato981c9262023-06-21 15:16:23 -070073 ctx.Build(pctx, android.BuildParams{
Joe Onorato37f900c2023-07-18 16:58:16 -070074 Rule: javaRule,
Joe Onorato981c9262023-06-21 15:16:23 -070075 Input: declarations.IntermediatePath,
76 Output: srcJarPath,
77 Description: "aconfig.srcjar",
Joe Onoratob7c294a2023-07-28 05:30:25 -070078 Args: map[string]string{
79 "mode": mode,
80 },
Joe Onorato981c9262023-06-21 15:16:23 -070081 })
82
Joe Onorato6fe59eb2023-07-16 13:20:33 -070083 // 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 Onorato981c9262023-06-21 15:16:23 -070087 return srcJarPath
88}