Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 1 | // 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 Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame^] | 15 | package codegen |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 16 | |
| 17 | import ( |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 18 | "fmt" |
Yu Liu | f2b9401 | 2023-09-19 15:09:10 -0700 | [diff] [blame] | 19 | |
Yu Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame^] | 20 | "android/soong/aconfig" |
Yu Liu | f2b9401 | 2023-09-19 15:09:10 -0700 | [diff] [blame] | 21 | "android/soong/android" |
| 22 | "android/soong/bazel" |
| 23 | "android/soong/java" |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 24 | "github.com/google/blueprint" |
Yu Liu | f2b9401 | 2023-09-19 15:09:10 -0700 | [diff] [blame] | 25 | "github.com/google/blueprint/proptools" |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | type declarationsTagType struct { |
| 29 | blueprint.BaseDependencyTag |
| 30 | } |
| 31 | |
| 32 | var declarationsTag = declarationsTagType{} |
| 33 | |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 34 | var aconfigSupportedModes = []string{"production", "test", "exported"} |
| 35 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 36 | type JavaAconfigDeclarationsLibraryProperties struct { |
| 37 | // name of the aconfig_declarations module to generate a library for |
| 38 | Aconfig_declarations string |
Joe Onorato | b7c294a | 2023-07-28 05:30:25 -0700 | [diff] [blame] | 39 | |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 40 | // default mode is "production", the other accepted modes are: |
| 41 | // "test": to generate test mode version of the library |
| 42 | // "exported": to generate exported mode version of the library |
| 43 | // an error will be thrown if the mode is not supported |
| 44 | Mode *string |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | type JavaAconfigDeclarationsLibraryCallbacks struct { |
| 48 | properties JavaAconfigDeclarationsLibraryProperties |
| 49 | } |
| 50 | |
| 51 | func JavaDeclarationsLibraryFactory() android.Module { |
| 52 | callbacks := &JavaAconfigDeclarationsLibraryCallbacks{} |
| 53 | return java.GeneratedJavaLibraryModuleFactory("java_aconfig_library", callbacks, &callbacks.properties) |
| 54 | } |
| 55 | |
| 56 | func (callbacks *JavaAconfigDeclarationsLibraryCallbacks) DepsMutator(module *java.GeneratedJavaLibraryModule, ctx android.BottomUpMutatorContext) { |
| 57 | declarations := callbacks.properties.Aconfig_declarations |
| 58 | if len(declarations) == 0 { |
| 59 | // TODO: Add test for this case |
| 60 | ctx.PropertyErrorf("aconfig_declarations", "aconfig_declarations property required") |
| 61 | } else { |
| 62 | ctx.AddDependency(ctx.Module(), declarationsTag, declarations) |
| 63 | } |
Joe Onorato | 8f75585 | 2023-08-25 20:23:32 -0700 | [diff] [blame] | 64 | |
| 65 | // Add aconfig-annotations-lib as a dependency for the optimization / code stripping annotations |
| 66 | module.AddSharedLibrary("aconfig-annotations-lib") |
Zhi Dou | 1b052b0 | 2023-10-06 07:28:48 +0000 | [diff] [blame] | 67 | // TODO(b/303773055): Remove the annotation after access issue is resolved. |
| 68 | module.AddSharedLibrary("unsupportedappusage") |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Joe Onorato | 6fe59eb | 2023-07-16 13:20:33 -0700 | [diff] [blame] | 71 | func (callbacks *JavaAconfigDeclarationsLibraryCallbacks) GenerateSourceJarBuildActions(module *java.GeneratedJavaLibraryModule, ctx android.ModuleContext) android.Path { |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 72 | // Get the values that came from the global RELEASE_ACONFIG_VALUE_SETS flag |
| 73 | declarationsModules := ctx.GetDirectDepsWithTag(declarationsTag) |
| 74 | if len(declarationsModules) != 1 { |
| 75 | panic(fmt.Errorf("Exactly one aconfig_declarations property required")) |
| 76 | } |
Yu Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame^] | 77 | declarations := ctx.OtherModuleProvider(declarationsModules[0], aconfig.DeclarationsProviderKey).(aconfig.DeclarationsProviderData) |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 78 | |
Joe Onorato | 6fe59eb | 2023-07-16 13:20:33 -0700 | [diff] [blame] | 79 | // Generate the action to build the srcjar |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 80 | srcJarPath := android.PathForModuleGen(ctx, ctx.ModuleName()+".srcjar") |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 81 | |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 82 | mode := proptools.StringDefault(callbacks.properties.Mode, "production") |
| 83 | if !isModeSupported(mode) { |
| 84 | ctx.PropertyErrorf("mode", "%q is not a supported mode", mode) |
| 85 | } |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 86 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 87 | ctx.Build(pctx, android.BuildParams{ |
Joe Onorato | 37f900c | 2023-07-18 16:58:16 -0700 | [diff] [blame] | 88 | Rule: javaRule, |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 89 | Input: declarations.IntermediatePath, |
| 90 | Output: srcJarPath, |
| 91 | Description: "aconfig.srcjar", |
Joe Onorato | b7c294a | 2023-07-28 05:30:25 -0700 | [diff] [blame] | 92 | Args: map[string]string{ |
| 93 | "mode": mode, |
| 94 | }, |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 95 | }) |
| 96 | |
Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 97 | return srcJarPath |
| 98 | } |
Yu Liu | f2b9401 | 2023-09-19 15:09:10 -0700 | [diff] [blame] | 99 | |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 100 | func isModeSupported(mode string) bool { |
| 101 | return android.InList(mode, aconfigSupportedModes) |
| 102 | } |
| 103 | |
Yu Liu | f2b9401 | 2023-09-19 15:09:10 -0700 | [diff] [blame] | 104 | type bazelJavaAconfigLibraryAttributes struct { |
| 105 | Aconfig_declarations bazel.LabelAttribute |
Yu Liu | f2b9401 | 2023-09-19 15:09:10 -0700 | [diff] [blame] | 106 | Sdk_version *string |
Yu Liu | 873ad35 | 2023-10-13 18:19:48 +0000 | [diff] [blame] | 107 | Libs bazel.LabelListAttribute |
Yu Liu | f2b9401 | 2023-09-19 15:09:10 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | func (callbacks *JavaAconfigDeclarationsLibraryCallbacks) Bp2build(ctx android.Bp2buildMutatorContext, module *java.GeneratedJavaLibraryModule) { |
| 111 | if ctx.ModuleType() != "java_aconfig_library" { |
| 112 | return |
| 113 | } |
| 114 | |
| 115 | // By default, soong builds the aconfig java library with private_current, however |
| 116 | // bazel currently doesn't support it so we default it to system_current. One reason |
| 117 | // is that the dependency of all java_aconfig_library aconfig-annotations-lib is |
| 118 | // built with system_current. For the java aconfig library itself it doesn't really |
| 119 | // matter whether it uses private API or system API because the only module it uses |
| 120 | // is DeviceConfig which is in system, and the rdeps of the java aconfig library |
| 121 | // won't change its sdk version either, so this should be fine. |
| 122 | // Ideally we should only use the default value if it is not set by the user, but |
| 123 | // bazel only supports a limited sdk versions, for example, the java_aconfig_library |
| 124 | // modules in framework/base use core_platform which is not supported by bazel yet. |
| 125 | // TODO(b/302148527): change soong to default to system_current as well. |
| 126 | sdkVersion := "system_current" |
Yu Liu | 873ad35 | 2023-10-13 18:19:48 +0000 | [diff] [blame] | 127 | |
| 128 | var libs bazel.LabelListAttribute |
| 129 | archVariantProps := module.GetArchVariantProperties(ctx, &java.CommonProperties{}) |
| 130 | for axis, configToProps := range archVariantProps { |
| 131 | for config, p := range configToProps { |
| 132 | if archProps, ok := p.(*java.CommonProperties); ok { |
| 133 | var libLabels []bazel.Label |
| 134 | for _, d := range archProps.Libs { |
| 135 | neverlinkLabel := android.BazelLabelForModuleDepSingle(ctx, d) |
| 136 | neverlinkLabel.Label = neverlinkLabel.Label + "-neverlink" |
| 137 | libLabels = append(libLabels, neverlinkLabel) |
| 138 | } |
| 139 | libs.SetSelectValue(axis, config, (bazel.MakeLabelList(libLabels))) |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
Yu Liu | f2b9401 | 2023-09-19 15:09:10 -0700 | [diff] [blame] | 144 | attrs := bazelJavaAconfigLibraryAttributes{ |
| 145 | Aconfig_declarations: *bazel.MakeLabelAttribute(android.BazelLabelForModuleDepSingle(ctx, callbacks.properties.Aconfig_declarations).Label), |
Yu Liu | f2b9401 | 2023-09-19 15:09:10 -0700 | [diff] [blame] | 146 | Sdk_version: &sdkVersion, |
Yu Liu | 873ad35 | 2023-10-13 18:19:48 +0000 | [diff] [blame] | 147 | Libs: libs, |
Yu Liu | f2b9401 | 2023-09-19 15:09:10 -0700 | [diff] [blame] | 148 | } |
| 149 | props := bazel.BazelTargetModuleProperties{ |
| 150 | Rule_class: "java_aconfig_library", |
| 151 | Bzl_load_location: "//build/bazel/rules/java:java_aconfig_library.bzl", |
| 152 | } |
| 153 | |
| 154 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: ctx.ModuleName()}, &attrs) |
| 155 | } |