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