blob: 4134893820ff9ee2b7803a85b91df9908fe6e156 [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
20 "android/soong/android"
Yu Liuf2b94012023-09-19 15:09:10 -070021 "android/soong/java"
Jihoon Kangcca3e0c2023-11-29 19:35:29 +000022
Joe Onorato981c9262023-06-21 15:16:23 -070023 "github.com/google/blueprint"
Yu Liuf2b94012023-09-19 15:09:10 -070024 "github.com/google/blueprint/proptools"
Joe Onorato981c9262023-06-21 15:16:23 -070025)
26
27type declarationsTagType struct {
28 blueprint.BaseDependencyTag
29}
30
31var declarationsTag = declarationsTagType{}
32
Zi Wang275f6542023-11-09 14:59:31 -080033var aconfigSupportedModes = []string{"production", "test", "exported"}
34
Joe Onorato981c9262023-06-21 15:16:23 -070035type JavaAconfigDeclarationsLibraryProperties struct {
36 // name of the aconfig_declarations module to generate a library for
37 Aconfig_declarations string
Joe Onoratob7c294a2023-07-28 05:30:25 -070038
Zi Wang275f6542023-11-09 14:59:31 -080039 // default mode is "production", the other accepted modes are:
40 // "test": to generate test mode version of the library
41 // "exported": to generate exported mode version of the library
42 // an error will be thrown if the mode is not supported
43 Mode *string
Joe Onorato981c9262023-06-21 15:16:23 -070044}
45
46type JavaAconfigDeclarationsLibraryCallbacks struct {
47 properties JavaAconfigDeclarationsLibraryProperties
48}
49
50func JavaDeclarationsLibraryFactory() android.Module {
51 callbacks := &JavaAconfigDeclarationsLibraryCallbacks{}
52 return java.GeneratedJavaLibraryModuleFactory("java_aconfig_library", callbacks, &callbacks.properties)
53}
54
55func (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 Onorato8f755852023-08-25 20:23:32 -070063
64 // Add aconfig-annotations-lib as a dependency for the optimization / code stripping annotations
65 module.AddSharedLibrary("aconfig-annotations-lib")
Zhi Dou1b052b02023-10-06 07:28:48 +000066 // TODO(b/303773055): Remove the annotation after access issue is resolved.
67 module.AddSharedLibrary("unsupportedappusage")
Joe Onorato981c9262023-06-21 15:16:23 -070068}
69
Joe Onorato6fe59eb2023-07-16 13:20:33 -070070func (callbacks *JavaAconfigDeclarationsLibraryCallbacks) GenerateSourceJarBuildActions(module *java.GeneratedJavaLibraryModule, ctx android.ModuleContext) android.Path {
Joe Onorato981c9262023-06-21 15:16:23 -070071 // Get the values that came from the global RELEASE_ACONFIG_VALUE_SETS flag
72 declarationsModules := ctx.GetDirectDepsWithTag(declarationsTag)
73 if len(declarationsModules) != 1 {
74 panic(fmt.Errorf("Exactly one aconfig_declarations property required"))
75 }
LaMont Jonesaa005ae2023-12-19 19:01:57 +000076 declarations, _ := android.OtherModuleProvider(ctx, declarationsModules[0], android.AconfigDeclarationsProviderKey)
Joe Onorato981c9262023-06-21 15:16:23 -070077
Joe Onorato6fe59eb2023-07-16 13:20:33 -070078 // Generate the action to build the srcjar
Joe Onorato981c9262023-06-21 15:16:23 -070079 srcJarPath := android.PathForModuleGen(ctx, ctx.ModuleName()+".srcjar")
Zi Wang275f6542023-11-09 14:59:31 -080080
Zi Wang275f6542023-11-09 14:59:31 -080081 mode := proptools.StringDefault(callbacks.properties.Mode, "production")
82 if !isModeSupported(mode) {
83 ctx.PropertyErrorf("mode", "%q is not a supported mode", mode)
84 }
Zi Wang275f6542023-11-09 14:59:31 -080085
Joe Onorato981c9262023-06-21 15:16:23 -070086 ctx.Build(pctx, android.BuildParams{
Joe Onorato37f900c2023-07-18 16:58:16 -070087 Rule: javaRule,
Jihoon Kangcca3e0c2023-11-29 19:35:29 +000088 Input: declarations.IntermediateCacheOutputPath,
Joe Onorato981c9262023-06-21 15:16:23 -070089 Output: srcJarPath,
90 Description: "aconfig.srcjar",
Joe Onoratob7c294a2023-07-28 05:30:25 -070091 Args: map[string]string{
92 "mode": mode,
93 },
Joe Onorato981c9262023-06-21 15:16:23 -070094 })
95
Joe Onorato981c9262023-06-21 15:16:23 -070096 return srcJarPath
97}
Yu Liuf2b94012023-09-19 15:09:10 -070098
Zi Wang275f6542023-11-09 14:59:31 -080099func isModeSupported(mode string) bool {
100 return android.InList(mode, aconfigSupportedModes)
101}