blob: 48cfb769523dd68174bbad13c39001707e2e38c3 [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 (
Joe Onorato981c9262023-06-21 15:16:23 -070018 "fmt"
Yu Liuf2b94012023-09-19 15:09:10 -070019
20 "android/soong/android"
21 "android/soong/bazel"
22 "android/soong/java"
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
33type JavaAconfigDeclarationsLibraryProperties struct {
34 // name of the aconfig_declarations module to generate a library for
35 Aconfig_declarations string
Joe Onoratob7c294a2023-07-28 05:30:25 -070036
37 // whether to generate test mode version of the library
Yu Liuf2b94012023-09-19 15:09:10 -070038 Test *bool
Joe Onorato981c9262023-06-21 15:16:23 -070039}
40
41type JavaAconfigDeclarationsLibraryCallbacks struct {
42 properties JavaAconfigDeclarationsLibraryProperties
43}
44
45func JavaDeclarationsLibraryFactory() android.Module {
46 callbacks := &JavaAconfigDeclarationsLibraryCallbacks{}
47 return java.GeneratedJavaLibraryModuleFactory("java_aconfig_library", callbacks, &callbacks.properties)
48}
49
50func (callbacks *JavaAconfigDeclarationsLibraryCallbacks) DepsMutator(module *java.GeneratedJavaLibraryModule, ctx android.BottomUpMutatorContext) {
51 declarations := callbacks.properties.Aconfig_declarations
52 if len(declarations) == 0 {
53 // TODO: Add test for this case
54 ctx.PropertyErrorf("aconfig_declarations", "aconfig_declarations property required")
55 } else {
56 ctx.AddDependency(ctx.Module(), declarationsTag, declarations)
57 }
Joe Onorato8f755852023-08-25 20:23:32 -070058
59 // Add aconfig-annotations-lib as a dependency for the optimization / code stripping annotations
60 module.AddSharedLibrary("aconfig-annotations-lib")
Joe Onorato981c9262023-06-21 15:16:23 -070061}
62
Joe Onorato6fe59eb2023-07-16 13:20:33 -070063func (callbacks *JavaAconfigDeclarationsLibraryCallbacks) GenerateSourceJarBuildActions(module *java.GeneratedJavaLibraryModule, ctx android.ModuleContext) android.Path {
Joe Onorato981c9262023-06-21 15:16:23 -070064 // Get the values that came from the global RELEASE_ACONFIG_VALUE_SETS flag
65 declarationsModules := ctx.GetDirectDepsWithTag(declarationsTag)
66 if len(declarationsModules) != 1 {
67 panic(fmt.Errorf("Exactly one aconfig_declarations property required"))
68 }
69 declarations := ctx.OtherModuleProvider(declarationsModules[0], declarationsProviderKey).(declarationsProviderData)
70
Joe Onorato6fe59eb2023-07-16 13:20:33 -070071 // Generate the action to build the srcjar
Joe Onorato981c9262023-06-21 15:16:23 -070072 srcJarPath := android.PathForModuleGen(ctx, ctx.ModuleName()+".srcjar")
Joe Onoratob7c294a2023-07-28 05:30:25 -070073 var mode string
Yu Liuf2b94012023-09-19 15:09:10 -070074 if proptools.Bool(callbacks.properties.Test) {
Joe Onoratob7c294a2023-07-28 05:30:25 -070075 mode = "test"
76 } else {
77 mode = "production"
78 }
Joe Onorato981c9262023-06-21 15:16:23 -070079 ctx.Build(pctx, android.BuildParams{
Joe Onorato37f900c2023-07-18 16:58:16 -070080 Rule: javaRule,
Joe Onorato981c9262023-06-21 15:16:23 -070081 Input: declarations.IntermediatePath,
82 Output: srcJarPath,
83 Description: "aconfig.srcjar",
Joe Onoratob7c294a2023-07-28 05:30:25 -070084 Args: map[string]string{
85 "mode": mode,
86 },
Joe Onorato981c9262023-06-21 15:16:23 -070087 })
88
Joe Onorato6fe59eb2023-07-16 13:20:33 -070089 // Tell the java module about the .aconfig files, so they can be propagated up the dependency chain.
90 // TODO: It would be nice to have that propagation code here instead of on java.Module and java.JavaInfo.
91 module.AddAconfigIntermediate(declarations.IntermediatePath)
92
Joe Onorato981c9262023-06-21 15:16:23 -070093 return srcJarPath
94}
Yu Liuf2b94012023-09-19 15:09:10 -070095
96type bazelJavaAconfigLibraryAttributes struct {
97 Aconfig_declarations bazel.LabelAttribute
98 Test *bool
99 Sdk_version *string
100}
101
102func (callbacks *JavaAconfigDeclarationsLibraryCallbacks) Bp2build(ctx android.Bp2buildMutatorContext, module *java.GeneratedJavaLibraryModule) {
103 if ctx.ModuleType() != "java_aconfig_library" {
104 return
105 }
106
107 // By default, soong builds the aconfig java library with private_current, however
108 // bazel currently doesn't support it so we default it to system_current. One reason
109 // is that the dependency of all java_aconfig_library aconfig-annotations-lib is
110 // built with system_current. For the java aconfig library itself it doesn't really
111 // matter whether it uses private API or system API because the only module it uses
112 // is DeviceConfig which is in system, and the rdeps of the java aconfig library
113 // won't change its sdk version either, so this should be fine.
114 // Ideally we should only use the default value if it is not set by the user, but
115 // bazel only supports a limited sdk versions, for example, the java_aconfig_library
116 // modules in framework/base use core_platform which is not supported by bazel yet.
117 // TODO(b/302148527): change soong to default to system_current as well.
118 sdkVersion := "system_current"
119 attrs := bazelJavaAconfigLibraryAttributes{
120 Aconfig_declarations: *bazel.MakeLabelAttribute(android.BazelLabelForModuleDepSingle(ctx, callbacks.properties.Aconfig_declarations).Label),
121 Test: callbacks.properties.Test,
122 Sdk_version: &sdkVersion,
123 }
124 props := bazel.BazelTargetModuleProperties{
125 Rule_class: "java_aconfig_library",
126 Bzl_load_location: "//build/bazel/rules/java:java_aconfig_library.bzl",
127 }
128
129 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: ctx.ModuleName()}, &attrs)
130}