Rupert Shuttleworth | 095081c | 2021-03-25 09:06:03 +0000 | [diff] [blame] | 1 | // Copyright 2021 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 | "android/soong/android" |
| 19 | "android/soong/cc" |
| 20 | "strings" |
| 21 | "testing" |
| 22 | ) |
| 23 | |
| 24 | const ( |
| 25 | // See cc/testing.go for more context |
| 26 | soongCcLibraryStaticPreamble = ` |
| 27 | cc_defaults { |
| 28 | name: "linux_bionic_supported", |
| 29 | } |
| 30 | |
| 31 | toolchain_library { |
| 32 | name: "libclang_rt.builtins-x86_64-android", |
| 33 | defaults: ["linux_bionic_supported"], |
| 34 | vendor_available: true, |
| 35 | vendor_ramdisk_available: true, |
| 36 | product_available: true, |
| 37 | recovery_available: true, |
| 38 | native_bridge_supported: true, |
| 39 | src: "", |
Rupert Shuttleworth | 095081c | 2021-03-25 09:06:03 +0000 | [diff] [blame] | 40 | }` |
| 41 | ) |
| 42 | |
| 43 | func TestCcLibraryStaticLoadStatement(t *testing.T) { |
| 44 | testCases := []struct { |
| 45 | bazelTargets BazelTargets |
| 46 | expectedLoadStatements string |
| 47 | }{ |
| 48 | { |
| 49 | bazelTargets: BazelTargets{ |
| 50 | BazelTarget{ |
| 51 | name: "cc_library_static_target", |
| 52 | ruleClass: "cc_library_static", |
| 53 | // NOTE: No bzlLoadLocation for native rules |
| 54 | }, |
| 55 | }, |
| 56 | expectedLoadStatements: ``, |
| 57 | }, |
| 58 | } |
| 59 | |
| 60 | for _, testCase := range testCases { |
| 61 | actual := testCase.bazelTargets.LoadStatements() |
| 62 | expected := testCase.expectedLoadStatements |
| 63 | if actual != expected { |
| 64 | t.Fatalf("Expected load statements to be %s, got %s", expected, actual) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | } |
| 69 | |
| 70 | func TestCcLibraryStaticBp2Build(t *testing.T) { |
| 71 | testCases := []struct { |
| 72 | description string |
| 73 | moduleTypeUnderTest string |
| 74 | moduleTypeUnderTestFactory android.ModuleFactory |
| 75 | moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext) |
| 76 | preArchMutators []android.RegisterMutatorFunc |
| 77 | depsMutators []android.RegisterMutatorFunc |
| 78 | bp string |
| 79 | expectedBazelTargets []string |
| 80 | filesystem map[string]string |
| 81 | dir string |
| 82 | }{ |
| 83 | { |
| 84 | description: "cc_library_static test", |
| 85 | moduleTypeUnderTest: "cc_library_static", |
| 86 | moduleTypeUnderTestFactory: cc.LibraryStaticFactory, |
| 87 | moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build, |
| 88 | filesystem: map[string]string{ |
| 89 | // NOTE: include_dir headers *should not* appear in Bazel hdrs later (?) |
| 90 | "include_dir_1/include_dir_1_a.h": "", |
| 91 | "include_dir_1/include_dir_1_b.h": "", |
| 92 | "include_dir_2/include_dir_2_a.h": "", |
| 93 | "include_dir_2/include_dir_2_b.h": "", |
| 94 | // NOTE: local_include_dir headers *should not* appear in Bazel hdrs later (?) |
| 95 | "local_include_dir_1/local_include_dir_1_a.h": "", |
| 96 | "local_include_dir_1/local_include_dir_1_b.h": "", |
| 97 | "local_include_dir_2/local_include_dir_2_a.h": "", |
| 98 | "local_include_dir_2/local_include_dir_2_b.h": "", |
| 99 | // NOTE: export_include_dir headers *should* appear in Bazel hdrs later |
| 100 | "export_include_dir_1/export_include_dir_1_a.h": "", |
| 101 | "export_include_dir_1/export_include_dir_1_b.h": "", |
| 102 | "export_include_dir_2/export_include_dir_2_a.h": "", |
| 103 | "export_include_dir_2/export_include_dir_2_b.h": "", |
Rupert Shuttleworth | c58d3d2 | 2021-04-06 16:37:15 +0000 | [diff] [blame] | 104 | // NOTE: Soong implicitly includes headers in the current directory |
| 105 | "implicit_include_1.h": "", |
| 106 | "implicit_include_2.h": "", |
Rupert Shuttleworth | 095081c | 2021-03-25 09:06:03 +0000 | [diff] [blame] | 107 | }, |
| 108 | bp: soongCcLibraryStaticPreamble + ` |
| 109 | cc_library_headers { |
| 110 | name: "header_lib_1", |
| 111 | export_include_dirs: ["header_lib_1"], |
| 112 | } |
| 113 | |
| 114 | cc_library_headers { |
| 115 | name: "header_lib_2", |
| 116 | export_include_dirs: ["header_lib_2"], |
| 117 | } |
| 118 | |
| 119 | cc_library_static { |
| 120 | name: "static_lib_1", |
| 121 | srcs: ["static_lib_1.cc"], |
| 122 | bazel_module: { bp2build_available: true }, |
| 123 | } |
| 124 | |
| 125 | cc_library_static { |
| 126 | name: "static_lib_2", |
| 127 | srcs: ["static_lib_2.cc"], |
| 128 | bazel_module: { bp2build_available: true }, |
| 129 | } |
| 130 | |
| 131 | cc_library_static { |
| 132 | name: "whole_static_lib_1", |
| 133 | srcs: ["whole_static_lib_1.cc"], |
| 134 | bazel_module: { bp2build_available: true }, |
| 135 | } |
| 136 | |
| 137 | cc_library_static { |
| 138 | name: "whole_static_lib_2", |
| 139 | srcs: ["whole_static_lib_2.cc"], |
| 140 | bazel_module: { bp2build_available: true }, |
| 141 | } |
| 142 | |
| 143 | cc_library_static { |
| 144 | name: "foo_static", |
| 145 | srcs: [ |
| 146 | "foo_static1.cc", |
| 147 | "foo_static2.cc", |
| 148 | ], |
| 149 | cflags: [ |
| 150 | "-Dflag1", |
| 151 | "-Dflag2" |
| 152 | ], |
| 153 | static_libs: [ |
| 154 | "static_lib_1", |
| 155 | "static_lib_2" |
| 156 | ], |
| 157 | whole_static_libs: [ |
| 158 | "whole_static_lib_1", |
| 159 | "whole_static_lib_2" |
| 160 | ], |
| 161 | include_dirs: [ |
| 162 | "include_dir_1", |
| 163 | "include_dir_2", |
| 164 | ], |
| 165 | local_include_dirs: [ |
| 166 | "local_include_dir_1", |
| 167 | "local_include_dir_2", |
| 168 | ], |
| 169 | export_include_dirs: [ |
| 170 | "export_include_dir_1", |
| 171 | "export_include_dir_2" |
| 172 | ], |
| 173 | header_libs: [ |
| 174 | "header_lib_1", |
| 175 | "header_lib_2" |
| 176 | ], |
| 177 | |
| 178 | // TODO: Also support export_header_lib_headers |
| 179 | |
| 180 | bazel_module: { bp2build_available: true }, |
| 181 | }`, |
| 182 | expectedBazelTargets: []string{`cc_library_static( |
| 183 | name = "foo_static", |
| 184 | copts = [ |
| 185 | "-Dflag1", |
| 186 | "-Dflag2", |
| 187 | ], |
| 188 | deps = [ |
| 189 | ":header_lib_1", |
| 190 | ":header_lib_2", |
| 191 | ":static_lib_1", |
| 192 | ":static_lib_2", |
| 193 | ":whole_static_lib_1", |
| 194 | ":whole_static_lib_2", |
| 195 | ], |
| 196 | hdrs = [ |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 197 | "implicit_include_1.h", |
| 198 | "implicit_include_2.h", |
Rupert Shuttleworth | 095081c | 2021-03-25 09:06:03 +0000 | [diff] [blame] | 199 | "export_include_dir_1/export_include_dir_1_a.h", |
| 200 | "export_include_dir_1/export_include_dir_1_b.h", |
| 201 | "export_include_dir_2/export_include_dir_2_a.h", |
| 202 | "export_include_dir_2/export_include_dir_2_b.h", |
| 203 | ], |
| 204 | includes = [ |
| 205 | "export_include_dir_1", |
| 206 | "export_include_dir_2", |
| 207 | "include_dir_1", |
| 208 | "include_dir_2", |
| 209 | "local_include_dir_1", |
| 210 | "local_include_dir_2", |
Rupert Shuttleworth | c58d3d2 | 2021-04-06 16:37:15 +0000 | [diff] [blame] | 211 | ".", |
Rupert Shuttleworth | 095081c | 2021-03-25 09:06:03 +0000 | [diff] [blame] | 212 | ], |
| 213 | linkstatic = True, |
| 214 | srcs = [ |
| 215 | "foo_static1.cc", |
| 216 | "foo_static2.cc", |
Rupert Shuttleworth | c58d3d2 | 2021-04-06 16:37:15 +0000 | [diff] [blame] | 217 | "include_dir_1/include_dir_1_a.h", |
| 218 | "include_dir_1/include_dir_1_b.h", |
| 219 | "include_dir_2/include_dir_2_a.h", |
| 220 | "include_dir_2/include_dir_2_b.h", |
| 221 | "local_include_dir_1/local_include_dir_1_a.h", |
| 222 | "local_include_dir_1/local_include_dir_1_b.h", |
| 223 | "local_include_dir_2/local_include_dir_2_a.h", |
| 224 | "local_include_dir_2/local_include_dir_2_b.h", |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 225 | "implicit_include_1.h", |
| 226 | "implicit_include_2.h", |
Rupert Shuttleworth | 095081c | 2021-03-25 09:06:03 +0000 | [diff] [blame] | 227 | ], |
| 228 | )`, `cc_library_static( |
| 229 | name = "static_lib_1", |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 230 | hdrs = [ |
| 231 | "implicit_include_1.h", |
| 232 | "implicit_include_2.h", |
| 233 | ], |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 234 | includes = ["."], |
Rupert Shuttleworth | 095081c | 2021-03-25 09:06:03 +0000 | [diff] [blame] | 235 | linkstatic = True, |
| 236 | srcs = [ |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 237 | "static_lib_1.cc", |
Rupert Shuttleworth | c58d3d2 | 2021-04-06 16:37:15 +0000 | [diff] [blame] | 238 | "implicit_include_1.h", |
| 239 | "implicit_include_2.h", |
Rupert Shuttleworth | 095081c | 2021-03-25 09:06:03 +0000 | [diff] [blame] | 240 | ], |
| 241 | )`, `cc_library_static( |
| 242 | name = "static_lib_2", |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 243 | hdrs = [ |
| 244 | "implicit_include_1.h", |
| 245 | "implicit_include_2.h", |
| 246 | ], |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 247 | includes = ["."], |
Rupert Shuttleworth | 095081c | 2021-03-25 09:06:03 +0000 | [diff] [blame] | 248 | linkstatic = True, |
| 249 | srcs = [ |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 250 | "static_lib_2.cc", |
Rupert Shuttleworth | c58d3d2 | 2021-04-06 16:37:15 +0000 | [diff] [blame] | 251 | "implicit_include_1.h", |
| 252 | "implicit_include_2.h", |
Rupert Shuttleworth | 095081c | 2021-03-25 09:06:03 +0000 | [diff] [blame] | 253 | ], |
| 254 | )`, `cc_library_static( |
| 255 | name = "whole_static_lib_1", |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 256 | hdrs = [ |
| 257 | "implicit_include_1.h", |
| 258 | "implicit_include_2.h", |
| 259 | ], |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 260 | includes = ["."], |
Rupert Shuttleworth | 095081c | 2021-03-25 09:06:03 +0000 | [diff] [blame] | 261 | linkstatic = True, |
| 262 | srcs = [ |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 263 | "whole_static_lib_1.cc", |
Rupert Shuttleworth | c58d3d2 | 2021-04-06 16:37:15 +0000 | [diff] [blame] | 264 | "implicit_include_1.h", |
| 265 | "implicit_include_2.h", |
Rupert Shuttleworth | 095081c | 2021-03-25 09:06:03 +0000 | [diff] [blame] | 266 | ], |
| 267 | )`, `cc_library_static( |
| 268 | name = "whole_static_lib_2", |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 269 | hdrs = [ |
| 270 | "implicit_include_1.h", |
| 271 | "implicit_include_2.h", |
| 272 | ], |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 273 | includes = ["."], |
Rupert Shuttleworth | 095081c | 2021-03-25 09:06:03 +0000 | [diff] [blame] | 274 | linkstatic = True, |
| 275 | srcs = [ |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 276 | "whole_static_lib_2.cc", |
Rupert Shuttleworth | c58d3d2 | 2021-04-06 16:37:15 +0000 | [diff] [blame] | 277 | "implicit_include_1.h", |
| 278 | "implicit_include_2.h", |
Rupert Shuttleworth | 095081c | 2021-03-25 09:06:03 +0000 | [diff] [blame] | 279 | ], |
| 280 | )`}, |
| 281 | }, |
Rupert Shuttleworth | c143cc5 | 2021-04-13 13:08:04 -0400 | [diff] [blame^] | 282 | { |
| 283 | description: "cc_library_static subpackage test", |
| 284 | moduleTypeUnderTest: "cc_library_static", |
| 285 | moduleTypeUnderTestFactory: cc.LibraryStaticFactory, |
| 286 | moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build, |
| 287 | filesystem: map[string]string{ |
| 288 | // subpackage with subdirectory |
| 289 | "subpackage/Android.bp": "", |
| 290 | "subpackage/subpackage_header.h": "", |
| 291 | "subpackage/subdirectory/subdirectory_header.h": "", |
| 292 | // subsubpackage with subdirectory |
| 293 | "subpackage/subsubpackage/Android.bp": "", |
| 294 | "subpackage/subsubpackage/subsubpackage_header.h": "", |
| 295 | "subpackage/subsubpackage/subdirectory/subdirectory_header.h": "", |
| 296 | // subsubsubpackage with subdirectory |
| 297 | "subpackage/subsubpackage/subsubsubpackage/Android.bp": "", |
| 298 | "subpackage/subsubpackage/subsubsubpackage/subsubsubpackage_header.h": "", |
| 299 | "subpackage/subsubpackage/subsubsubpackage/subdirectory/subdirectory_header.h": "", |
| 300 | }, |
| 301 | bp: soongCcLibraryStaticPreamble + ` |
| 302 | cc_library_static { |
| 303 | name: "foo_static", |
| 304 | srcs: [ |
| 305 | ], |
| 306 | include_dirs: [ |
| 307 | "subpackage", |
| 308 | ], |
| 309 | |
| 310 | bazel_module: { bp2build_available: true }, |
| 311 | }`, |
| 312 | expectedBazelTargets: []string{`cc_library_static( |
| 313 | name = "foo_static", |
| 314 | includes = [ |
| 315 | "subpackage", |
| 316 | ".", |
| 317 | ], |
| 318 | linkstatic = True, |
| 319 | srcs = [ |
| 320 | "//subpackage:subpackage_header.h", |
| 321 | "//subpackage:subdirectory/subdirectory_header.h", |
| 322 | "//subpackage/subsubpackage:subsubpackage_header.h", |
| 323 | "//subpackage/subsubpackage:subdirectory/subdirectory_header.h", |
| 324 | "//subpackage/subsubpackage/subsubsubpackage:subsubsubpackage_header.h", |
| 325 | "//subpackage/subsubpackage/subsubsubpackage:subdirectory/subdirectory_header.h", |
| 326 | ], |
| 327 | )`}, |
| 328 | }, |
Rupert Shuttleworth | 095081c | 2021-03-25 09:06:03 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | dir := "." |
| 332 | for _, testCase := range testCases { |
| 333 | filesystem := make(map[string][]byte) |
| 334 | toParse := []string{ |
| 335 | "Android.bp", |
| 336 | } |
| 337 | for f, content := range testCase.filesystem { |
| 338 | if strings.HasSuffix(f, "Android.bp") { |
| 339 | toParse = append(toParse, f) |
| 340 | } |
| 341 | filesystem[f] = []byte(content) |
| 342 | } |
| 343 | config := android.TestConfig(buildDir, nil, testCase.bp, filesystem) |
| 344 | ctx := android.NewTestContext(config) |
| 345 | |
| 346 | cc.RegisterCCBuildComponents(ctx) |
| 347 | ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory) |
| 348 | ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory) |
| 349 | |
| 350 | ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory) |
| 351 | for _, m := range testCase.depsMutators { |
| 352 | ctx.DepsBp2BuildMutators(m) |
| 353 | } |
| 354 | ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator) |
| 355 | ctx.RegisterForBazelConversion() |
| 356 | |
| 357 | _, errs := ctx.ParseFileList(dir, toParse) |
| 358 | if Errored(t, testCase.description, errs) { |
| 359 | continue |
| 360 | } |
| 361 | _, errs = ctx.ResolveDependencies(config) |
| 362 | if Errored(t, testCase.description, errs) { |
| 363 | continue |
| 364 | } |
| 365 | |
| 366 | checkDir := dir |
| 367 | if testCase.dir != "" { |
| 368 | checkDir = testCase.dir |
| 369 | } |
| 370 | codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) |
| 371 | bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir) |
| 372 | if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount { |
| 373 | t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount) |
| 374 | } else { |
| 375 | for i, target := range bazelTargets { |
| 376 | if w, g := testCase.expectedBazelTargets[i], target.content; w != g { |
| 377 | t.Errorf( |
| 378 | "%s: Expected generated Bazel target to be '%s', got '%s'", |
| 379 | testCase.description, |
| 380 | w, |
| 381 | g, |
| 382 | ) |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | } |