blob: b6c90fc5f2b47c8f6ffc662b4168a81121adbcb9 [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
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
39 // whether to generate test mode version of the library
Zi Wang275f6542023-11-09 14:59:31 -080040 // TODO: remove "Test" property when "Mode" can be used in all the branches
Yu Liuf2b94012023-09-19 15:09:10 -070041 Test *bool
Zi Wang275f6542023-11-09 14:59:31 -080042
43 // default mode is "production", the other accepted modes are:
44 // "test": to generate test mode version of the library
45 // "exported": to generate exported mode version of the library
46 // an error will be thrown if the mode is not supported
47 Mode *string
Joe Onorato981c9262023-06-21 15:16:23 -070048}
49
50type JavaAconfigDeclarationsLibraryCallbacks struct {
51 properties JavaAconfigDeclarationsLibraryProperties
52}
53
54func JavaDeclarationsLibraryFactory() android.Module {
55 callbacks := &JavaAconfigDeclarationsLibraryCallbacks{}
56 return java.GeneratedJavaLibraryModuleFactory("java_aconfig_library", callbacks, &callbacks.properties)
57}
58
59func (callbacks *JavaAconfigDeclarationsLibraryCallbacks) DepsMutator(module *java.GeneratedJavaLibraryModule, ctx android.BottomUpMutatorContext) {
60 declarations := callbacks.properties.Aconfig_declarations
61 if len(declarations) == 0 {
62 // TODO: Add test for this case
63 ctx.PropertyErrorf("aconfig_declarations", "aconfig_declarations property required")
64 } else {
65 ctx.AddDependency(ctx.Module(), declarationsTag, declarations)
66 }
Joe Onorato8f755852023-08-25 20:23:32 -070067
68 // Add aconfig-annotations-lib as a dependency for the optimization / code stripping annotations
69 module.AddSharedLibrary("aconfig-annotations-lib")
Zhi Dou1b052b02023-10-06 07:28:48 +000070 // TODO(b/303773055): Remove the annotation after access issue is resolved.
71 module.AddSharedLibrary("unsupportedappusage")
Joe Onorato981c9262023-06-21 15:16:23 -070072}
73
Joe Onorato6fe59eb2023-07-16 13:20:33 -070074func (callbacks *JavaAconfigDeclarationsLibraryCallbacks) GenerateSourceJarBuildActions(module *java.GeneratedJavaLibraryModule, ctx android.ModuleContext) android.Path {
Joe Onorato981c9262023-06-21 15:16:23 -070075 // Get the values that came from the global RELEASE_ACONFIG_VALUE_SETS flag
76 declarationsModules := ctx.GetDirectDepsWithTag(declarationsTag)
77 if len(declarationsModules) != 1 {
78 panic(fmt.Errorf("Exactly one aconfig_declarations property required"))
79 }
80 declarations := ctx.OtherModuleProvider(declarationsModules[0], declarationsProviderKey).(declarationsProviderData)
81
Joe Onorato6fe59eb2023-07-16 13:20:33 -070082 // Generate the action to build the srcjar
Joe Onorato981c9262023-06-21 15:16:23 -070083 srcJarPath := android.PathForModuleGen(ctx, ctx.ModuleName()+".srcjar")
Zi Wang275f6542023-11-09 14:59:31 -080084
85 if callbacks.properties.Mode != nil && callbacks.properties.Test != nil {
86 ctx.PropertyErrorf("test", "test prop should not be specified when mode prop is set")
87 }
88 mode := proptools.StringDefault(callbacks.properties.Mode, "production")
89 if !isModeSupported(mode) {
90 ctx.PropertyErrorf("mode", "%q is not a supported mode", mode)
91 }
92 // TODO: remove "Test" property
Yu Liuf2b94012023-09-19 15:09:10 -070093 if proptools.Bool(callbacks.properties.Test) {
Joe Onoratob7c294a2023-07-28 05:30:25 -070094 mode = "test"
Joe Onoratob7c294a2023-07-28 05:30:25 -070095 }
Zi Wang275f6542023-11-09 14:59:31 -080096
Joe Onorato981c9262023-06-21 15:16:23 -070097 ctx.Build(pctx, android.BuildParams{
Joe Onorato37f900c2023-07-18 16:58:16 -070098 Rule: javaRule,
Joe Onorato981c9262023-06-21 15:16:23 -070099 Input: declarations.IntermediatePath,
100 Output: srcJarPath,
101 Description: "aconfig.srcjar",
Joe Onoratob7c294a2023-07-28 05:30:25 -0700102 Args: map[string]string{
103 "mode": mode,
104 },
Joe Onorato981c9262023-06-21 15:16:23 -0700105 })
106
Joe Onorato6fe59eb2023-07-16 13:20:33 -0700107 // Tell the java module about the .aconfig files, so they can be propagated up the dependency chain.
108 // TODO: It would be nice to have that propagation code here instead of on java.Module and java.JavaInfo.
109 module.AddAconfigIntermediate(declarations.IntermediatePath)
110
Joe Onorato981c9262023-06-21 15:16:23 -0700111 return srcJarPath
112}
Yu Liuf2b94012023-09-19 15:09:10 -0700113
Zi Wang275f6542023-11-09 14:59:31 -0800114func isModeSupported(mode string) bool {
115 return android.InList(mode, aconfigSupportedModes)
116}
117
Yu Liuf2b94012023-09-19 15:09:10 -0700118type bazelJavaAconfigLibraryAttributes struct {
119 Aconfig_declarations bazel.LabelAttribute
Yu Liuf2b94012023-09-19 15:09:10 -0700120 Sdk_version *string
Yu Liu873ad352023-10-13 18:19:48 +0000121 Libs bazel.LabelListAttribute
Yu Liuf2b94012023-09-19 15:09:10 -0700122}
123
124func (callbacks *JavaAconfigDeclarationsLibraryCallbacks) Bp2build(ctx android.Bp2buildMutatorContext, module *java.GeneratedJavaLibraryModule) {
125 if ctx.ModuleType() != "java_aconfig_library" {
126 return
127 }
128
129 // By default, soong builds the aconfig java library with private_current, however
130 // bazel currently doesn't support it so we default it to system_current. One reason
131 // is that the dependency of all java_aconfig_library aconfig-annotations-lib is
132 // built with system_current. For the java aconfig library itself it doesn't really
133 // matter whether it uses private API or system API because the only module it uses
134 // is DeviceConfig which is in system, and the rdeps of the java aconfig library
135 // won't change its sdk version either, so this should be fine.
136 // Ideally we should only use the default value if it is not set by the user, but
137 // bazel only supports a limited sdk versions, for example, the java_aconfig_library
138 // modules in framework/base use core_platform which is not supported by bazel yet.
139 // TODO(b/302148527): change soong to default to system_current as well.
140 sdkVersion := "system_current"
Yu Liu873ad352023-10-13 18:19:48 +0000141
142 var libs bazel.LabelListAttribute
143 archVariantProps := module.GetArchVariantProperties(ctx, &java.CommonProperties{})
144 for axis, configToProps := range archVariantProps {
145 for config, p := range configToProps {
146 if archProps, ok := p.(*java.CommonProperties); ok {
147 var libLabels []bazel.Label
148 for _, d := range archProps.Libs {
149 neverlinkLabel := android.BazelLabelForModuleDepSingle(ctx, d)
150 neverlinkLabel.Label = neverlinkLabel.Label + "-neverlink"
151 libLabels = append(libLabels, neverlinkLabel)
152 }
153 libs.SetSelectValue(axis, config, (bazel.MakeLabelList(libLabels)))
154 }
155 }
156 }
157
Yu Liuf2b94012023-09-19 15:09:10 -0700158 attrs := bazelJavaAconfigLibraryAttributes{
159 Aconfig_declarations: *bazel.MakeLabelAttribute(android.BazelLabelForModuleDepSingle(ctx, callbacks.properties.Aconfig_declarations).Label),
Yu Liuf2b94012023-09-19 15:09:10 -0700160 Sdk_version: &sdkVersion,
Yu Liu873ad352023-10-13 18:19:48 +0000161 Libs: libs,
Yu Liuf2b94012023-09-19 15:09:10 -0700162 }
163 props := bazel.BazelTargetModuleProperties{
164 Rule_class: "java_aconfig_library",
165 Bzl_load_location: "//build/bazel/rules/java:java_aconfig_library.bzl",
166 }
167
168 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: ctx.ModuleName()}, &attrs)
169}