| 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"]`, | 
| Jingwen Chen | 9c2e3ee | 2023-10-11 10:51:28 +0000 | [diff] [blame] | 131 | "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`, | 
| Yu Liu | 855cfc2 | 2023-09-14 15:10:03 -0700 | [diff] [blame] | 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 | } | 
| Yu Liu | 873ad35 | 2023-10-13 18:19:48 +0000 | [diff] [blame] | 150 | java_library { | 
|  | 151 | name: "foo_java_library", | 
|  | 152 | srcs: ["foo.java"], | 
|  | 153 | sdk_version: "current", | 
|  | 154 | } | 
| Yu Liu | f2b9401 | 2023-09-19 15:09:10 -0700 | [diff] [blame] | 155 | java_aconfig_library { | 
|  | 156 | name: "foo", | 
|  | 157 | aconfig_declarations: "foo_aconfig_declarations", | 
| Yu Liu | 873ad35 | 2023-10-13 18:19:48 +0000 | [diff] [blame] | 158 | libs: ["foo_java_library"], | 
| Colin Cross | 7e2e794 | 2023-11-16 12:56:02 -0800 | [diff] [blame] | 159 | mode: "test", | 
| Yu Liu | f2b9401 | 2023-09-19 15:09:10 -0700 | [diff] [blame] | 160 | } | 
|  | 161 | ` | 
|  | 162 | expectedBazelTargets := []string{ | 
|  | 163 | MakeBazelTargetNoRestrictions( | 
|  | 164 | "aconfig_declarations", | 
|  | 165 | "foo_aconfig_declarations", | 
|  | 166 | AttrNameToString{ | 
|  | 167 | "srcs":    `["foo1.aconfig"]`, | 
|  | 168 | "package": `"com.android.foo"`, | 
|  | 169 | }, | 
|  | 170 | ), | 
|  | 171 | MakeBazelTargetNoRestrictions( | 
| Yu Liu | 873ad35 | 2023-10-13 18:19:48 +0000 | [diff] [blame] | 172 | "java_library", | 
|  | 173 | "foo_java_library", | 
|  | 174 | AttrNameToString{ | 
|  | 175 | "srcs":                   `["foo.java"]`, | 
|  | 176 | "sdk_version":            `"current"`, | 
|  | 177 | "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`, | 
|  | 178 | }, | 
|  | 179 | ), | 
|  | 180 | MakeNeverlinkDuplicateTarget("java_library", "foo_java_library"), | 
|  | 181 | MakeBazelTargetNoRestrictions( | 
| Yu Liu | f2b9401 | 2023-09-19 15:09:10 -0700 | [diff] [blame] | 182 | "java_aconfig_library", | 
|  | 183 | "foo", | 
|  | 184 | AttrNameToString{ | 
|  | 185 | "aconfig_declarations":   `":foo_aconfig_declarations"`, | 
| Yu Liu | 873ad35 | 2023-10-13 18:19:48 +0000 | [diff] [blame] | 186 | "libs":                   `[":foo_java_library-neverlink"]`, | 
| Yu Liu | f2b9401 | 2023-09-19 15:09:10 -0700 | [diff] [blame] | 187 | "sdk_version":            `"system_current"`, | 
| Jingwen Chen | 9c2e3ee | 2023-10-11 10:51:28 +0000 | [diff] [blame] | 188 | "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`, | 
| Yu Liu | f2b9401 | 2023-09-19 15:09:10 -0700 | [diff] [blame] | 189 | }, | 
|  | 190 | )} | 
|  | 191 | RunBp2BuildTestCase(t, registerAconfigModuleTypes, Bp2buildTestCase{ | 
|  | 192 | Blueprint:            bp, | 
|  | 193 | ExpectedBazelTargets: expectedBazelTargets, | 
|  | 194 | }) | 
|  | 195 | } | 
|  | 196 |  | 
|  | 197 | func TestJavaAconfigLibraryAsTaggedOutput(t *testing.T) { | 
|  | 198 | bp := ` | 
|  | 199 | aconfig_declarations { | 
|  | 200 | name: "foo_aconfig_declarations", | 
|  | 201 | srcs: [ | 
|  | 202 | "foo.aconfig", | 
|  | 203 | ], | 
|  | 204 | package: "com.android.foo", | 
|  | 205 | } | 
|  | 206 | java_library { | 
|  | 207 | name: "foo_library", | 
|  | 208 | srcs: [":foo_aconfig_library{.generated_srcjars}"], | 
|  | 209 | sdk_version: "current", | 
|  | 210 | bazel_module: { bp2build_available: true }, | 
|  | 211 | } | 
|  | 212 | java_aconfig_library { | 
|  | 213 | name: "foo_aconfig_library", | 
|  | 214 | aconfig_declarations: "foo_aconfig_declarations", | 
| Colin Cross | 7e2e794 | 2023-11-16 12:56:02 -0800 | [diff] [blame] | 215 | mode: "test", | 
| Yu Liu | f2b9401 | 2023-09-19 15:09:10 -0700 | [diff] [blame] | 216 | } | 
|  | 217 | ` | 
|  | 218 | expectedBazelTargets := []string{ | 
|  | 219 | MakeBazelTargetNoRestrictions( | 
|  | 220 | "aconfig_declarations", | 
|  | 221 | "foo_aconfig_declarations", | 
|  | 222 | AttrNameToString{ | 
|  | 223 | "srcs":    `["foo.aconfig"]`, | 
|  | 224 | "package": `"com.android.foo"`, | 
|  | 225 | }, | 
|  | 226 | ), | 
|  | 227 | MakeBazelTargetNoRestrictions( | 
|  | 228 | "java_aconfig_library", | 
|  | 229 | "foo_aconfig_library", | 
|  | 230 | AttrNameToString{ | 
|  | 231 | "aconfig_declarations":   `":foo_aconfig_declarations"`, | 
| Yu Liu | f2b9401 | 2023-09-19 15:09:10 -0700 | [diff] [blame] | 232 | "sdk_version":            `"system_current"`, | 
| Jingwen Chen | 9c2e3ee | 2023-10-11 10:51:28 +0000 | [diff] [blame] | 233 | "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`, | 
| Yu Liu | f2b9401 | 2023-09-19 15:09:10 -0700 | [diff] [blame] | 234 | }, | 
|  | 235 | ), | 
|  | 236 | MakeBazelTargetNoRestrictions( | 
|  | 237 | "java_library", | 
|  | 238 | "foo_library", | 
|  | 239 | AttrNameToString{ | 
|  | 240 | "srcs":                   `[":foo_aconfig_library.generated_srcjars"]`, | 
|  | 241 | "sdk_version":            `"current"`, | 
| Jingwen Chen | 9c2e3ee | 2023-10-11 10:51:28 +0000 | [diff] [blame] | 242 | "target_compatible_with": `["//build/bazel_common_rules/platforms/os:android"]`, | 
| Yu Liu | f2b9401 | 2023-09-19 15:09:10 -0700 | [diff] [blame] | 243 | }, | 
|  | 244 | ), | 
|  | 245 | MakeNeverlinkDuplicateTarget("java_library", "foo_library"), | 
|  | 246 | } | 
|  | 247 |  | 
|  | 248 | RunBp2BuildTestCase(t, registerAconfigModuleTypes, Bp2buildTestCase{ | 
|  | 249 | Blueprint:            bp, | 
|  | 250 | ExpectedBazelTargets: expectedBazelTargets, | 
|  | 251 | }) | 
|  | 252 | } |