blob: 211bdce909dd79bc64b86ef44dc53fb1cf0d14ee [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
Yu Liueae7b362023-11-16 17:05:47 -080020 "android/soong/aconfig"
Yu Liuf2b94012023-09-19 15:09:10 -070021 "android/soong/android"
22 "android/soong/bazel"
23 "android/soong/java"
Jihoon Kangcca3e0c2023-11-29 19:35:29 +000024
Joe Onorato981c9262023-06-21 15:16:23 -070025 "github.com/google/blueprint"
Yu Liuf2b94012023-09-19 15:09:10 -070026 "github.com/google/blueprint/proptools"
Joe Onorato981c9262023-06-21 15:16:23 -070027)
28
29type declarationsTagType struct {
30 blueprint.BaseDependencyTag
31}
32
33var declarationsTag = declarationsTagType{}
34
Zi Wang275f6542023-11-09 14:59:31 -080035var aconfigSupportedModes = []string{"production", "test", "exported"}
36
Joe Onorato981c9262023-06-21 15:16:23 -070037type JavaAconfigDeclarationsLibraryProperties struct {
38 // name of the aconfig_declarations module to generate a library for
39 Aconfig_declarations string
Joe Onoratob7c294a2023-07-28 05:30:25 -070040
Zi Wang275f6542023-11-09 14:59:31 -080041 // default mode is "production", the other accepted modes are:
42 // "test": to generate test mode version of the library
43 // "exported": to generate exported mode version of the library
44 // an error will be thrown if the mode is not supported
45 Mode *string
Joe Onorato981c9262023-06-21 15:16:23 -070046}
47
48type JavaAconfigDeclarationsLibraryCallbacks struct {
49 properties JavaAconfigDeclarationsLibraryProperties
50}
51
52func JavaDeclarationsLibraryFactory() android.Module {
53 callbacks := &JavaAconfigDeclarationsLibraryCallbacks{}
54 return java.GeneratedJavaLibraryModuleFactory("java_aconfig_library", callbacks, &callbacks.properties)
55}
56
57func (callbacks *JavaAconfigDeclarationsLibraryCallbacks) DepsMutator(module *java.GeneratedJavaLibraryModule, ctx android.BottomUpMutatorContext) {
58 declarations := callbacks.properties.Aconfig_declarations
59 if len(declarations) == 0 {
60 // TODO: Add test for this case
61 ctx.PropertyErrorf("aconfig_declarations", "aconfig_declarations property required")
62 } else {
63 ctx.AddDependency(ctx.Module(), declarationsTag, declarations)
64 }
Joe Onorato8f755852023-08-25 20:23:32 -070065
66 // Add aconfig-annotations-lib as a dependency for the optimization / code stripping annotations
67 module.AddSharedLibrary("aconfig-annotations-lib")
Zhi Dou1b052b02023-10-06 07:28:48 +000068 // TODO(b/303773055): Remove the annotation after access issue is resolved.
69 module.AddSharedLibrary("unsupportedappusage")
Joe Onorato981c9262023-06-21 15:16:23 -070070}
71
Joe Onorato6fe59eb2023-07-16 13:20:33 -070072func (callbacks *JavaAconfigDeclarationsLibraryCallbacks) GenerateSourceJarBuildActions(module *java.GeneratedJavaLibraryModule, ctx android.ModuleContext) android.Path {
Joe Onorato981c9262023-06-21 15:16:23 -070073 // Get the values that came from the global RELEASE_ACONFIG_VALUE_SETS flag
74 declarationsModules := ctx.GetDirectDepsWithTag(declarationsTag)
75 if len(declarationsModules) != 1 {
76 panic(fmt.Errorf("Exactly one aconfig_declarations property required"))
77 }
Yu Liueae7b362023-11-16 17:05:47 -080078 declarations := ctx.OtherModuleProvider(declarationsModules[0], aconfig.DeclarationsProviderKey).(aconfig.DeclarationsProviderData)
Joe Onorato981c9262023-06-21 15:16:23 -070079
Joe Onorato6fe59eb2023-07-16 13:20:33 -070080 // Generate the action to build the srcjar
Joe Onorato981c9262023-06-21 15:16:23 -070081 srcJarPath := android.PathForModuleGen(ctx, ctx.ModuleName()+".srcjar")
Zi Wang275f6542023-11-09 14:59:31 -080082
Zi Wang275f6542023-11-09 14:59:31 -080083 mode := proptools.StringDefault(callbacks.properties.Mode, "production")
84 if !isModeSupported(mode) {
85 ctx.PropertyErrorf("mode", "%q is not a supported mode", mode)
86 }
Zi Wang275f6542023-11-09 14:59:31 -080087
Joe Onorato981c9262023-06-21 15:16:23 -070088 ctx.Build(pctx, android.BuildParams{
Joe Onorato37f900c2023-07-18 16:58:16 -070089 Rule: javaRule,
Jihoon Kangcca3e0c2023-11-29 19:35:29 +000090 Input: declarations.IntermediateCacheOutputPath,
Joe Onorato981c9262023-06-21 15:16:23 -070091 Output: srcJarPath,
92 Description: "aconfig.srcjar",
Joe Onoratob7c294a2023-07-28 05:30:25 -070093 Args: map[string]string{
94 "mode": mode,
95 },
Joe Onorato981c9262023-06-21 15:16:23 -070096 })
97
Joe Onorato981c9262023-06-21 15:16:23 -070098 return srcJarPath
99}
Yu Liuf2b94012023-09-19 15:09:10 -0700100
Zi Wang275f6542023-11-09 14:59:31 -0800101func isModeSupported(mode string) bool {
102 return android.InList(mode, aconfigSupportedModes)
103}
104
Yu Liuf2b94012023-09-19 15:09:10 -0700105type bazelJavaAconfigLibraryAttributes struct {
106 Aconfig_declarations bazel.LabelAttribute
Yu Liuf2b94012023-09-19 15:09:10 -0700107 Sdk_version *string
Yu Liu873ad352023-10-13 18:19:48 +0000108 Libs bazel.LabelListAttribute
Yu Liuf2b94012023-09-19 15:09:10 -0700109}
110
111func (callbacks *JavaAconfigDeclarationsLibraryCallbacks) Bp2build(ctx android.Bp2buildMutatorContext, module *java.GeneratedJavaLibraryModule) {
112 if ctx.ModuleType() != "java_aconfig_library" {
113 return
114 }
115
116 // By default, soong builds the aconfig java library with private_current, however
117 // bazel currently doesn't support it so we default it to system_current. One reason
118 // is that the dependency of all java_aconfig_library aconfig-annotations-lib is
119 // built with system_current. For the java aconfig library itself it doesn't really
120 // matter whether it uses private API or system API because the only module it uses
121 // is DeviceConfig which is in system, and the rdeps of the java aconfig library
122 // won't change its sdk version either, so this should be fine.
123 // Ideally we should only use the default value if it is not set by the user, but
124 // bazel only supports a limited sdk versions, for example, the java_aconfig_library
125 // modules in framework/base use core_platform which is not supported by bazel yet.
126 // TODO(b/302148527): change soong to default to system_current as well.
127 sdkVersion := "system_current"
Yu Liu873ad352023-10-13 18:19:48 +0000128
129 var libs bazel.LabelListAttribute
130 archVariantProps := module.GetArchVariantProperties(ctx, &java.CommonProperties{})
131 for axis, configToProps := range archVariantProps {
132 for config, p := range configToProps {
133 if archProps, ok := p.(*java.CommonProperties); ok {
134 var libLabels []bazel.Label
135 for _, d := range archProps.Libs {
136 neverlinkLabel := android.BazelLabelForModuleDepSingle(ctx, d)
137 neverlinkLabel.Label = neverlinkLabel.Label + "-neverlink"
138 libLabels = append(libLabels, neverlinkLabel)
139 }
140 libs.SetSelectValue(axis, config, (bazel.MakeLabelList(libLabels)))
141 }
142 }
143 }
144
Yu Liuf2b94012023-09-19 15:09:10 -0700145 attrs := bazelJavaAconfigLibraryAttributes{
146 Aconfig_declarations: *bazel.MakeLabelAttribute(android.BazelLabelForModuleDepSingle(ctx, callbacks.properties.Aconfig_declarations).Label),
Yu Liuf2b94012023-09-19 15:09:10 -0700147 Sdk_version: &sdkVersion,
Yu Liu873ad352023-10-13 18:19:48 +0000148 Libs: libs,
Yu Liuf2b94012023-09-19 15:09:10 -0700149 }
150 props := bazel.BazelTargetModuleProperties{
151 Rule_class: "java_aconfig_library",
152 Bzl_load_location: "//build/bazel/rules/java:java_aconfig_library.bzl",
153 }
154
155 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: ctx.ModuleName()}, &attrs)
156}