Yu Liu | 2cc802a | 2023-09-05 17:19:45 -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 bp2build |
| 16 | |
| 17 | import ( |
| 18 | "testing" |
| 19 | |
| 20 | "android/soong/aconfig" |
| 21 | "android/soong/android" |
Yu Liu | 855cfc2 | 2023-09-14 15:10:03 -0700 | [diff] [blame] | 22 | "android/soong/cc" |
Yu Liu | f2b9401 | 2023-09-19 15:09:10 -0700 | [diff] [blame] | 23 | "android/soong/java" |
Yu Liu | 2cc802a | 2023-09-05 17:19:45 -0700 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | func registerAconfigModuleTypes(ctx android.RegistrationContext) { |
| 27 | aconfig.RegisterBuildComponents(ctx) |
Yu Liu | 855cfc2 | 2023-09-14 15:10:03 -0700 | [diff] [blame] | 28 | ctx.RegisterModuleType("cc_library", cc.LibraryFactory) |
Yu Liu | f2b9401 | 2023-09-19 15:09:10 -0700 | [diff] [blame] | 29 | ctx.RegisterModuleType("java_library", java.LibraryFactory) |
Yu Liu | 2cc802a | 2023-09-05 17:19:45 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | func TestAconfigDeclarations(t *testing.T) { |
| 33 | bp := ` |
| 34 | aconfig_declarations { |
| 35 | name: "foo", |
| 36 | srcs: [ |
| 37 | "foo1.aconfig", |
| 38 | "test/foo2.aconfig", |
| 39 | ], |
| 40 | package: "com.android.foo", |
| 41 | } |
| 42 | ` |
| 43 | expectedBazelTarget := MakeBazelTargetNoRestrictions( |
| 44 | "aconfig_declarations", |
| 45 | "foo", |
| 46 | AttrNameToString{ |
| 47 | "srcs": `[ |
| 48 | "foo1.aconfig", |
| 49 | "test/foo2.aconfig", |
| 50 | ]`, |
| 51 | "package": `"com.android.foo"`, |
| 52 | }, |
| 53 | ) |
| 54 | RunBp2BuildTestCase(t, registerAconfigModuleTypes, Bp2buildTestCase{ |
| 55 | Blueprint: bp, |
| 56 | ExpectedBazelTargets: []string{expectedBazelTarget}, |
| 57 | }) |
| 58 | } |
| 59 | |
| 60 | func TestAconfigValues(t *testing.T) { |
| 61 | bp := ` |
| 62 | aconfig_values { |
| 63 | name: "foo", |
| 64 | srcs: [ |
| 65 | "foo1.textproto", |
| 66 | ], |
| 67 | package: "com.android.foo", |
| 68 | } |
| 69 | aconfig_value_set { |
| 70 | name: "bar", |
| 71 | values: [ |
| 72 | "foo" |
| 73 | ] |
| 74 | } |
| 75 | ` |
| 76 | expectedBazelTargets := []string{ |
| 77 | MakeBazelTargetNoRestrictions( |
| 78 | "aconfig_values", |
| 79 | "foo", |
| 80 | AttrNameToString{ |
| 81 | "srcs": `["foo1.textproto"]`, |
| 82 | "package": `"com.android.foo"`, |
| 83 | }, |
| 84 | ), |
| 85 | MakeBazelTargetNoRestrictions( |
| 86 | "aconfig_value_set", |
| 87 | "bar", |
| 88 | AttrNameToString{ |
| 89 | "values": `[":foo"]`, |
| 90 | }, |
| 91 | )} |
| 92 | RunBp2BuildTestCase(t, registerAconfigModuleTypes, Bp2buildTestCase{ |
| 93 | Blueprint: bp, |
| 94 | ExpectedBazelTargets: expectedBazelTargets, |
| 95 | }) |
| 96 | } |
Yu Liu | 855cfc2 | 2023-09-14 15:10:03 -0700 | [diff] [blame] | 97 | |
| 98 | func TestCcAconfigLibrary(t *testing.T) { |
| 99 | bp := ` |
| 100 | aconfig_declarations { |
| 101 | name: "foo_aconfig_declarations", |
| 102 | srcs: [ |
| 103 | "foo1.aconfig", |
| 104 | ], |
| 105 | package: "com.android.foo", |
| 106 | } |
| 107 | cc_library { |
| 108 | name: "server_configurable_flags", |
| 109 | srcs: ["bar.cc"], |
Yu Liu | 855cfc2 | 2023-09-14 15:10:03 -0700 | [diff] [blame] | 110 | } |
| 111 | cc_aconfig_library { |
| 112 | name: "foo", |
| 113 | aconfig_declarations: "foo_aconfig_declarations", |
| 114 | } |
| 115 | ` |
| 116 | expectedBazelTargets := []string{ |
| 117 | MakeBazelTargetNoRestrictions( |
| 118 | "aconfig_declarations", |
| 119 | "foo_aconfig_declarations", |
| 120 | AttrNameToString{ |
| 121 | "srcs": `["foo1.aconfig"]`, |
| 122 | "package": `"com.android.foo"`, |
| 123 | }, |
| 124 | ), |
| 125 | MakeBazelTargetNoRestrictions( |
| 126 | "cc_aconfig_library", |
| 127 | "foo", |
| 128 | AttrNameToString{ |
| 129 | "aconfig_declarations": `":foo_aconfig_declarations"`, |
| 130 | "dynamic_deps": `[":server_configurable_flags"]`, |
| 131 | "target_compatible_with": `["//build/bazel/platforms/os:android"]`, |
| 132 | }, |
| 133 | )} |
| 134 | RunBp2BuildTestCase(t, registerAconfigModuleTypes, Bp2buildTestCase{ |
Chris Parsons | 8a532b7 | 2023-09-27 23:11:26 +0000 | [diff] [blame] | 135 | Blueprint: bp, |
| 136 | ExpectedBazelTargets: expectedBazelTargets, |
| 137 | StubbedBuildDefinitions: []string{"server_configurable_flags"}, |
Yu Liu | 855cfc2 | 2023-09-14 15:10:03 -0700 | [diff] [blame] | 138 | }) |
| 139 | } |
Yu Liu | f2b9401 | 2023-09-19 15:09:10 -0700 | [diff] [blame] | 140 | |
| 141 | func TestJavaAconfigLibrary(t *testing.T) { |
| 142 | bp := ` |
| 143 | aconfig_declarations { |
| 144 | name: "foo_aconfig_declarations", |
| 145 | srcs: [ |
| 146 | "foo1.aconfig", |
| 147 | ], |
| 148 | package: "com.android.foo", |
| 149 | } |
| 150 | java_aconfig_library { |
| 151 | name: "foo", |
| 152 | aconfig_declarations: "foo_aconfig_declarations", |
| 153 | test: true, |
| 154 | } |
| 155 | ` |
| 156 | expectedBazelTargets := []string{ |
| 157 | MakeBazelTargetNoRestrictions( |
| 158 | "aconfig_declarations", |
| 159 | "foo_aconfig_declarations", |
| 160 | AttrNameToString{ |
| 161 | "srcs": `["foo1.aconfig"]`, |
| 162 | "package": `"com.android.foo"`, |
| 163 | }, |
| 164 | ), |
| 165 | MakeBazelTargetNoRestrictions( |
| 166 | "java_aconfig_library", |
| 167 | "foo", |
| 168 | AttrNameToString{ |
| 169 | "aconfig_declarations": `":foo_aconfig_declarations"`, |
| 170 | "test": `True`, |
| 171 | "sdk_version": `"system_current"`, |
| 172 | "target_compatible_with": `["//build/bazel/platforms/os:android"]`, |
| 173 | }, |
| 174 | )} |
| 175 | RunBp2BuildTestCase(t, registerAconfigModuleTypes, Bp2buildTestCase{ |
| 176 | Blueprint: bp, |
| 177 | ExpectedBazelTargets: expectedBazelTargets, |
| 178 | }) |
| 179 | } |
| 180 | |
| 181 | func TestJavaAconfigLibraryAsTaggedOutput(t *testing.T) { |
| 182 | bp := ` |
| 183 | aconfig_declarations { |
| 184 | name: "foo_aconfig_declarations", |
| 185 | srcs: [ |
| 186 | "foo.aconfig", |
| 187 | ], |
| 188 | package: "com.android.foo", |
| 189 | } |
| 190 | java_library { |
| 191 | name: "foo_library", |
| 192 | srcs: [":foo_aconfig_library{.generated_srcjars}"], |
| 193 | sdk_version: "current", |
| 194 | bazel_module: { bp2build_available: true }, |
| 195 | } |
| 196 | java_aconfig_library { |
| 197 | name: "foo_aconfig_library", |
| 198 | aconfig_declarations: "foo_aconfig_declarations", |
| 199 | test: true, |
| 200 | } |
| 201 | ` |
| 202 | expectedBazelTargets := []string{ |
| 203 | MakeBazelTargetNoRestrictions( |
| 204 | "aconfig_declarations", |
| 205 | "foo_aconfig_declarations", |
| 206 | AttrNameToString{ |
| 207 | "srcs": `["foo.aconfig"]`, |
| 208 | "package": `"com.android.foo"`, |
| 209 | }, |
| 210 | ), |
| 211 | MakeBazelTargetNoRestrictions( |
| 212 | "java_aconfig_library", |
| 213 | "foo_aconfig_library", |
| 214 | AttrNameToString{ |
| 215 | "aconfig_declarations": `":foo_aconfig_declarations"`, |
| 216 | "test": `True`, |
| 217 | "sdk_version": `"system_current"`, |
| 218 | "target_compatible_with": `["//build/bazel/platforms/os:android"]`, |
| 219 | }, |
| 220 | ), |
| 221 | MakeBazelTargetNoRestrictions( |
| 222 | "java_library", |
| 223 | "foo_library", |
| 224 | AttrNameToString{ |
| 225 | "srcs": `[":foo_aconfig_library.generated_srcjars"]`, |
| 226 | "sdk_version": `"current"`, |
| 227 | "target_compatible_with": `["//build/bazel/platforms/os:android"]`, |
| 228 | }, |
| 229 | ), |
| 230 | MakeNeverlinkDuplicateTarget("java_library", "foo_library"), |
| 231 | } |
| 232 | |
| 233 | RunBp2BuildTestCase(t, registerAconfigModuleTypes, Bp2buildTestCase{ |
| 234 | Blueprint: bp, |
| 235 | ExpectedBazelTargets: expectedBazelTargets, |
| 236 | }) |
| 237 | } |