Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [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 | soongCcLibraryPreamble = ` |
| 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: "", |
| 40 | }` |
| 41 | ) |
| 42 | |
| 43 | func TestCcLibraryBp2Build(t *testing.T) { |
| 44 | testCases := []struct { |
| 45 | description string |
| 46 | moduleTypeUnderTest string |
| 47 | moduleTypeUnderTestFactory android.ModuleFactory |
| 48 | moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext) |
| 49 | bp string |
| 50 | expectedBazelTargets []string |
| 51 | filesystem map[string]string |
| 52 | dir string |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 53 | depsMutators []android.RegisterMutatorFunc |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 54 | }{ |
| 55 | { |
| 56 | description: "cc_library - simple example", |
| 57 | moduleTypeUnderTest: "cc_library", |
| 58 | moduleTypeUnderTestFactory: cc.LibraryFactory, |
| 59 | moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build, |
| 60 | filesystem: map[string]string{ |
| 61 | "android.cpp": "", |
| 62 | "darwin.cpp": "", |
| 63 | // Refer to cc.headerExts for the supported header extensions in Soong. |
| 64 | "header.h": "", |
| 65 | "header.hh": "", |
| 66 | "header.hpp": "", |
| 67 | "header.hxx": "", |
| 68 | "header.h++": "", |
| 69 | "header.inl": "", |
| 70 | "header.inc": "", |
| 71 | "header.ipp": "", |
| 72 | "header.h.generic": "", |
| 73 | "impl.cpp": "", |
| 74 | "linux.cpp": "", |
| 75 | "x86.cpp": "", |
| 76 | "x86_64.cpp": "", |
| 77 | "foo-dir/a.h": "", |
| 78 | }, |
| 79 | bp: soongCcLibraryPreamble + ` |
| 80 | cc_library_headers { name: "some-headers" } |
| 81 | cc_library { |
| 82 | name: "foo-lib", |
| 83 | srcs: ["impl.cpp"], |
| 84 | cflags: ["-Wall"], |
| 85 | header_libs: ["some-headers"], |
| 86 | export_include_dirs: ["foo-dir"], |
| 87 | ldflags: ["-Wl,--exclude-libs=bar.a"], |
| 88 | arch: { |
| 89 | x86: { |
| 90 | ldflags: ["-Wl,--exclude-libs=baz.a"], |
| 91 | srcs: ["x86.cpp"], |
| 92 | }, |
| 93 | x86_64: { |
| 94 | ldflags: ["-Wl,--exclude-libs=qux.a"], |
| 95 | srcs: ["x86_64.cpp"], |
| 96 | }, |
| 97 | }, |
| 98 | target: { |
| 99 | android: { |
| 100 | srcs: ["android.cpp"], |
| 101 | }, |
| 102 | linux_glibc: { |
| 103 | srcs: ["linux.cpp"], |
| 104 | }, |
| 105 | darwin: { |
| 106 | srcs: ["darwin.cpp"], |
| 107 | }, |
| 108 | }, |
| 109 | } |
| 110 | `, |
| 111 | expectedBazelTargets: []string{`cc_library( |
| 112 | name = "foo-lib", |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 113 | copts = [ |
| 114 | "-Wall", |
| 115 | "-I.", |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 116 | "-I$(BINDIR)/.", |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 117 | ], |
Chris Parsons | d635877 | 2021-05-18 18:35:24 -0400 | [diff] [blame] | 118 | implementation_deps = [":some-headers"], |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 119 | includes = ["foo-dir"], |
| 120 | linkopts = ["-Wl,--exclude-libs=bar.a"] + select({ |
| 121 | "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=baz.a"], |
| 122 | "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=qux.a"], |
| 123 | "//conditions:default": [], |
| 124 | }), |
Jingwen Chen | 882bcc1 | 2021-04-27 05:54:20 +0000 | [diff] [blame] | 125 | srcs = ["impl.cpp"] + select({ |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 126 | "//build/bazel/platforms/arch:x86": ["x86.cpp"], |
| 127 | "//build/bazel/platforms/arch:x86_64": ["x86_64.cpp"], |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 128 | "//conditions:default": [], |
| 129 | }) + select({ |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 130 | "//build/bazel/platforms/os:android": ["android.cpp"], |
| 131 | "//build/bazel/platforms/os:darwin": ["darwin.cpp"], |
| 132 | "//build/bazel/platforms/os:linux": ["linux.cpp"], |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 133 | "//conditions:default": [], |
| 134 | }), |
| 135 | )`}, |
| 136 | }, |
| 137 | { |
| 138 | description: "cc_library - trimmed example of //bionic/linker:ld-android", |
| 139 | moduleTypeUnderTest: "cc_library", |
| 140 | moduleTypeUnderTestFactory: cc.LibraryFactory, |
| 141 | moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build, |
| 142 | filesystem: map[string]string{ |
| 143 | "ld-android.cpp": "", |
| 144 | "linked_list.h": "", |
| 145 | "linker.h": "", |
| 146 | "linker_block_allocator.h": "", |
| 147 | "linker_cfi.h": "", |
| 148 | }, |
| 149 | bp: soongCcLibraryPreamble + ` |
| 150 | cc_library_headers { name: "libc_headers" } |
| 151 | cc_library { |
| 152 | name: "fake-ld-android", |
| 153 | srcs: ["ld_android.cpp"], |
| 154 | cflags: [ |
| 155 | "-Wall", |
| 156 | "-Wextra", |
| 157 | "-Wunused", |
| 158 | "-Werror", |
| 159 | ], |
| 160 | header_libs: ["libc_headers"], |
| 161 | ldflags: [ |
| 162 | "-Wl,--exclude-libs=libgcc.a", |
| 163 | "-Wl,--exclude-libs=libgcc_stripped.a", |
| 164 | "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a", |
| 165 | "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a", |
| 166 | "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a", |
| 167 | "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a", |
| 168 | ], |
| 169 | arch: { |
| 170 | x86: { |
| 171 | ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"], |
| 172 | }, |
| 173 | x86_64: { |
| 174 | ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"], |
| 175 | }, |
| 176 | }, |
| 177 | } |
| 178 | `, |
| 179 | expectedBazelTargets: []string{`cc_library( |
| 180 | name = "fake-ld-android", |
| 181 | copts = [ |
| 182 | "-Wall", |
| 183 | "-Wextra", |
| 184 | "-Wunused", |
| 185 | "-Werror", |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 186 | "-I.", |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 187 | "-I$(BINDIR)/.", |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 188 | ], |
Chris Parsons | d635877 | 2021-05-18 18:35:24 -0400 | [diff] [blame] | 189 | implementation_deps = [":libc_headers"], |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 190 | linkopts = [ |
| 191 | "-Wl,--exclude-libs=libgcc.a", |
| 192 | "-Wl,--exclude-libs=libgcc_stripped.a", |
| 193 | "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a", |
| 194 | "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a", |
| 195 | "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a", |
| 196 | "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a", |
| 197 | ] + select({ |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 198 | "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=libgcc_eh.a"], |
| 199 | "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=libgcc_eh.a"], |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 200 | "//conditions:default": [], |
| 201 | }), |
Jingwen Chen | 882bcc1 | 2021-04-27 05:54:20 +0000 | [diff] [blame] | 202 | srcs = ["ld_android.cpp"], |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 203 | )`}, |
| 204 | }, |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 205 | { |
| 206 | description: "cc_library exclude_srcs - trimmed example of //external/arm-optimized-routines:libarm-optimized-routines-math", |
| 207 | moduleTypeUnderTest: "cc_library", |
| 208 | moduleTypeUnderTestFactory: cc.LibraryFactory, |
| 209 | moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build, |
| 210 | dir: "external", |
| 211 | filesystem: map[string]string{ |
| 212 | "external/math/cosf.c": "", |
| 213 | "external/math/erf.c": "", |
| 214 | "external/math/erf_data.c": "", |
| 215 | "external/math/erff.c": "", |
| 216 | "external/math/erff_data.c": "", |
| 217 | "external/Android.bp": ` |
| 218 | cc_library { |
| 219 | name: "fake-libarm-optimized-routines-math", |
| 220 | exclude_srcs: [ |
| 221 | // Provided by: |
| 222 | // bionic/libm/upstream-freebsd/lib/msun/src/s_erf.c |
| 223 | // bionic/libm/upstream-freebsd/lib/msun/src/s_erff.c |
| 224 | "math/erf.c", |
| 225 | "math/erf_data.c", |
| 226 | "math/erff.c", |
| 227 | "math/erff_data.c", |
| 228 | ], |
| 229 | srcs: [ |
| 230 | "math/*.c", |
| 231 | ], |
| 232 | // arch-specific settings |
| 233 | arch: { |
| 234 | arm64: { |
| 235 | cflags: [ |
| 236 | "-DHAVE_FAST_FMA=1", |
| 237 | ], |
| 238 | }, |
| 239 | }, |
| 240 | bazel_module: { bp2build_available: true }, |
| 241 | } |
| 242 | `, |
| 243 | }, |
| 244 | bp: soongCcLibraryPreamble, |
| 245 | expectedBazelTargets: []string{`cc_library( |
| 246 | name = "fake-libarm-optimized-routines-math", |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 247 | copts = [ |
| 248 | "-Iexternal", |
| 249 | "-I$(BINDIR)/external", |
| 250 | ] + select({ |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 251 | "//build/bazel/platforms/arch:arm64": ["-DHAVE_FAST_FMA=1"], |
| 252 | "//conditions:default": [], |
| 253 | }), |
| 254 | srcs = ["math/cosf.c"], |
| 255 | )`}, |
| 256 | }, |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 257 | { |
| 258 | description: "cc_library shared/static props", |
| 259 | moduleTypeUnderTest: "cc_library", |
| 260 | moduleTypeUnderTestFactory: cc.LibraryFactory, |
| 261 | moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build, |
| 262 | depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build}, |
| 263 | dir: "foo/bar", |
| 264 | filesystem: map[string]string{ |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 265 | "foo/bar/both.cpp": "", |
| 266 | "foo/bar/sharedonly.cpp": "", |
| 267 | "foo/bar/staticonly.cpp": "", |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 268 | "foo/bar/Android.bp": ` |
| 269 | cc_library { |
| 270 | name: "a", |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 271 | srcs: ["both.cpp"], |
| 272 | cflags: ["bothflag"], |
| 273 | shared_libs: ["shared_dep_for_both"], |
| 274 | static_libs: ["static_dep_for_both"], |
| 275 | whole_static_libs: ["whole_static_lib_for_both"], |
| 276 | static: { |
| 277 | srcs: ["staticonly.cpp"], |
| 278 | cflags: ["staticflag"], |
| 279 | shared_libs: ["shared_dep_for_static"], |
| 280 | static_libs: ["static_dep_for_static"], |
| 281 | whole_static_libs: ["whole_static_lib_for_static"], |
| 282 | }, |
| 283 | shared: { |
| 284 | srcs: ["sharedonly.cpp"], |
| 285 | cflags: ["sharedflag"], |
| 286 | shared_libs: ["shared_dep_for_shared"], |
| 287 | static_libs: ["static_dep_for_shared"], |
| 288 | whole_static_libs: ["whole_static_lib_for_shared"], |
| 289 | }, |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 290 | bazel_module: { bp2build_available: true }, |
| 291 | } |
| 292 | |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 293 | cc_library_static { name: "static_dep_for_shared" } |
| 294 | |
| 295 | cc_library_static { name: "static_dep_for_static" } |
| 296 | |
| 297 | cc_library_static { name: "static_dep_for_both" } |
| 298 | |
| 299 | cc_library_static { name: "whole_static_lib_for_shared" } |
| 300 | |
| 301 | cc_library_static { name: "whole_static_lib_for_static" } |
| 302 | |
| 303 | cc_library_static { name: "whole_static_lib_for_both" } |
| 304 | |
| 305 | cc_library { name: "shared_dep_for_shared" } |
| 306 | |
| 307 | cc_library { name: "shared_dep_for_static" } |
| 308 | |
| 309 | cc_library { name: "shared_dep_for_both" } |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 310 | `, |
| 311 | }, |
| 312 | bp: soongCcLibraryPreamble, |
| 313 | expectedBazelTargets: []string{`cc_library( |
| 314 | name = "a", |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 315 | copts = [ |
| 316 | "bothflag", |
| 317 | "-Ifoo/bar", |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 318 | "-I$(BINDIR)/foo/bar", |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 319 | ], |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 320 | dynamic_deps = [":shared_dep_for_both"], |
| 321 | dynamic_deps_for_shared = [":shared_dep_for_shared"], |
| 322 | dynamic_deps_for_static = [":shared_dep_for_static"], |
Chris Parsons | d635877 | 2021-05-18 18:35:24 -0400 | [diff] [blame] | 323 | implementation_deps = [":static_dep_for_both"], |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 324 | shared_copts = ["sharedflag"], |
| 325 | shared_srcs = ["sharedonly.cpp"], |
| 326 | srcs = ["both.cpp"], |
| 327 | static_copts = ["staticflag"], |
| 328 | static_deps_for_shared = [":static_dep_for_shared"], |
| 329 | static_deps_for_static = [":static_dep_for_static"], |
| 330 | static_srcs = ["staticonly.cpp"], |
| 331 | whole_archive_deps = [":whole_static_lib_for_both"], |
| 332 | whole_archive_deps_for_shared = [":whole_static_lib_for_shared"], |
| 333 | whole_archive_deps_for_static = [":whole_static_lib_for_static"], |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 334 | )`}, |
| 335 | }, |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 336 | { |
| 337 | description: "cc_library non-configured version script", |
| 338 | moduleTypeUnderTest: "cc_library", |
| 339 | moduleTypeUnderTestFactory: cc.LibraryFactory, |
| 340 | moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build, |
| 341 | depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build}, |
| 342 | dir: "foo/bar", |
| 343 | filesystem: map[string]string{ |
| 344 | "foo/bar/Android.bp": ` |
| 345 | cc_library { |
| 346 | name: "a", |
| 347 | srcs: ["a.cpp"], |
| 348 | version_script: "v.map", |
| 349 | bazel_module: { bp2build_available: true }, |
| 350 | } |
| 351 | `, |
| 352 | }, |
| 353 | bp: soongCcLibraryPreamble, |
| 354 | expectedBazelTargets: []string{`cc_library( |
| 355 | name = "a", |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 356 | copts = [ |
| 357 | "-Ifoo/bar", |
| 358 | "-I$(BINDIR)/foo/bar", |
| 359 | ], |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 360 | srcs = ["a.cpp"], |
| 361 | version_script = "v.map", |
| 362 | )`}, |
| 363 | }, |
Rupert Shuttleworth | c50fa8d | 2021-05-06 02:40:33 -0400 | [diff] [blame] | 364 | { |
Lukacs T. Berki | 56bb083 | 2021-05-12 12:36:45 +0200 | [diff] [blame] | 365 | description: "cc_library configured version script", |
| 366 | moduleTypeUnderTest: "cc_library", |
| 367 | moduleTypeUnderTestFactory: cc.LibraryFactory, |
| 368 | moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build, |
| 369 | depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build}, |
| 370 | dir: "foo/bar", |
| 371 | filesystem: map[string]string{ |
| 372 | "foo/bar/Android.bp": ` |
| 373 | cc_library { |
| 374 | name: "a", |
| 375 | srcs: ["a.cpp"], |
| 376 | arch: { |
| 377 | arm: { |
| 378 | version_script: "arm.map", |
| 379 | }, |
| 380 | arm64: { |
| 381 | version_script: "arm64.map", |
| 382 | }, |
| 383 | }, |
| 384 | |
| 385 | bazel_module: { bp2build_available: true }, |
| 386 | } |
| 387 | `, |
| 388 | }, |
| 389 | bp: soongCcLibraryPreamble, |
| 390 | expectedBazelTargets: []string{`cc_library( |
| 391 | name = "a", |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 392 | copts = [ |
| 393 | "-Ifoo/bar", |
| 394 | "-I$(BINDIR)/foo/bar", |
| 395 | ], |
Lukacs T. Berki | 56bb083 | 2021-05-12 12:36:45 +0200 | [diff] [blame] | 396 | srcs = ["a.cpp"], |
| 397 | version_script = select({ |
| 398 | "//build/bazel/platforms/arch:arm": "arm.map", |
| 399 | "//build/bazel/platforms/arch:arm64": "arm64.map", |
| 400 | "//conditions:default": None, |
| 401 | }), |
| 402 | )`}, |
| 403 | }, |
| 404 | { |
Rupert Shuttleworth | c50fa8d | 2021-05-06 02:40:33 -0400 | [diff] [blame] | 405 | description: "cc_library shared_libs", |
| 406 | moduleTypeUnderTest: "cc_library", |
| 407 | moduleTypeUnderTestFactory: cc.LibraryFactory, |
| 408 | moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build, |
| 409 | depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build}, |
| 410 | dir: "foo/bar", |
| 411 | filesystem: map[string]string{ |
| 412 | "foo/bar/Android.bp": ` |
| 413 | cc_library { |
| 414 | name: "mylib", |
| 415 | bazel_module: { bp2build_available: true }, |
| 416 | } |
| 417 | |
| 418 | cc_library { |
| 419 | name: "a", |
| 420 | shared_libs: ["mylib",], |
| 421 | bazel_module: { bp2build_available: true }, |
| 422 | } |
| 423 | `, |
| 424 | }, |
| 425 | bp: soongCcLibraryPreamble, |
| 426 | expectedBazelTargets: []string{`cc_library( |
| 427 | name = "a", |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 428 | copts = [ |
| 429 | "-Ifoo/bar", |
| 430 | "-I$(BINDIR)/foo/bar", |
| 431 | ], |
Rupert Shuttleworth | c50fa8d | 2021-05-06 02:40:33 -0400 | [diff] [blame] | 432 | dynamic_deps = [":mylib"], |
| 433 | )`, `cc_library( |
| 434 | name = "mylib", |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 435 | copts = [ |
| 436 | "-Ifoo/bar", |
| 437 | "-I$(BINDIR)/foo/bar", |
| 438 | ], |
Rupert Shuttleworth | c50fa8d | 2021-05-06 02:40:33 -0400 | [diff] [blame] | 439 | )`}, |
| 440 | }, |
Rupert Shuttleworth | 143be94 | 2021-05-09 23:55:51 -0400 | [diff] [blame] | 441 | { |
| 442 | description: "cc_library pack_relocations test", |
| 443 | moduleTypeUnderTest: "cc_library", |
| 444 | moduleTypeUnderTestFactory: cc.LibraryFactory, |
| 445 | moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build, |
| 446 | depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build}, |
| 447 | dir: "foo/bar", |
| 448 | filesystem: map[string]string{ |
| 449 | "foo/bar/Android.bp": ` |
| 450 | cc_library { |
| 451 | name: "a", |
| 452 | srcs: ["a.cpp"], |
| 453 | pack_relocations: false, |
| 454 | bazel_module: { bp2build_available: true }, |
| 455 | } |
| 456 | |
| 457 | cc_library { |
| 458 | name: "b", |
| 459 | srcs: ["b.cpp"], |
| 460 | arch: { |
| 461 | x86_64: { |
| 462 | pack_relocations: false, |
| 463 | }, |
| 464 | }, |
| 465 | bazel_module: { bp2build_available: true }, |
| 466 | } |
| 467 | |
| 468 | cc_library { |
| 469 | name: "c", |
| 470 | srcs: ["c.cpp"], |
| 471 | target: { |
| 472 | darwin: { |
| 473 | pack_relocations: false, |
| 474 | }, |
| 475 | }, |
| 476 | bazel_module: { bp2build_available: true }, |
| 477 | }`, |
| 478 | }, |
| 479 | bp: soongCcLibraryPreamble, |
| 480 | expectedBazelTargets: []string{`cc_library( |
| 481 | name = "a", |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 482 | copts = [ |
| 483 | "-Ifoo/bar", |
| 484 | "-I$(BINDIR)/foo/bar", |
| 485 | ], |
Rupert Shuttleworth | 143be94 | 2021-05-09 23:55:51 -0400 | [diff] [blame] | 486 | linkopts = ["-Wl,--pack-dyn-relocs=none"], |
| 487 | srcs = ["a.cpp"], |
| 488 | )`, `cc_library( |
| 489 | name = "b", |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 490 | copts = [ |
| 491 | "-Ifoo/bar", |
| 492 | "-I$(BINDIR)/foo/bar", |
| 493 | ], |
Rupert Shuttleworth | 143be94 | 2021-05-09 23:55:51 -0400 | [diff] [blame] | 494 | linkopts = select({ |
| 495 | "//build/bazel/platforms/arch:x86_64": ["-Wl,--pack-dyn-relocs=none"], |
| 496 | "//conditions:default": [], |
| 497 | }), |
| 498 | srcs = ["b.cpp"], |
| 499 | )`, `cc_library( |
| 500 | name = "c", |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 501 | copts = [ |
| 502 | "-Ifoo/bar", |
| 503 | "-I$(BINDIR)/foo/bar", |
| 504 | ], |
Rupert Shuttleworth | 143be94 | 2021-05-09 23:55:51 -0400 | [diff] [blame] | 505 | linkopts = select({ |
| 506 | "//build/bazel/platforms/os:darwin": ["-Wl,--pack-dyn-relocs=none"], |
| 507 | "//conditions:default": [], |
| 508 | }), |
| 509 | srcs = ["c.cpp"], |
| 510 | )`}, |
| 511 | }, |
Jingwen Chen | 3950cd6 | 2021-05-12 04:33:00 +0000 | [diff] [blame] | 512 | { |
| 513 | description: "cc_library spaces in copts", |
| 514 | moduleTypeUnderTest: "cc_library", |
| 515 | moduleTypeUnderTestFactory: cc.LibraryFactory, |
| 516 | moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build, |
| 517 | depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build}, |
| 518 | dir: "foo/bar", |
| 519 | filesystem: map[string]string{ |
| 520 | "foo/bar/Android.bp": ` |
| 521 | cc_library { |
| 522 | name: "a", |
| 523 | cflags: ["-include header.h",], |
| 524 | bazel_module: { bp2build_available: true }, |
| 525 | } |
| 526 | `, |
| 527 | }, |
| 528 | bp: soongCcLibraryPreamble, |
| 529 | expectedBazelTargets: []string{`cc_library( |
| 530 | name = "a", |
| 531 | copts = [ |
| 532 | "-include", |
| 533 | "header.h", |
| 534 | "-Ifoo/bar", |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 535 | "-I$(BINDIR)/foo/bar", |
Jingwen Chen | 3950cd6 | 2021-05-12 04:33:00 +0000 | [diff] [blame] | 536 | ], |
| 537 | )`}, |
| 538 | }, |
Jingwen Chen | 75be1ca | 2021-05-12 05:04:58 +0000 | [diff] [blame] | 539 | { |
| 540 | description: "cc_library cppflags goes into copts", |
| 541 | moduleTypeUnderTest: "cc_library", |
| 542 | moduleTypeUnderTestFactory: cc.LibraryFactory, |
| 543 | moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build, |
| 544 | depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build}, |
| 545 | dir: "foo/bar", |
| 546 | filesystem: map[string]string{ |
| 547 | "foo/bar/Android.bp": `cc_library { |
| 548 | name: "a", |
| 549 | srcs: ["a.cpp"], |
| 550 | cflags: [ |
| 551 | "-Wall", |
| 552 | ], |
| 553 | cppflags: [ |
| 554 | "-fsigned-char", |
| 555 | "-pedantic", |
| 556 | ], |
| 557 | arch: { |
| 558 | arm64: { |
| 559 | cppflags: ["-DARM64=1"], |
| 560 | }, |
| 561 | }, |
| 562 | target: { |
| 563 | android: { |
| 564 | cppflags: ["-DANDROID=1"], |
| 565 | }, |
| 566 | }, |
| 567 | bazel_module: { bp2build_available: true }, |
| 568 | } |
| 569 | `, |
| 570 | }, |
| 571 | bp: soongCcLibraryPreamble, |
| 572 | expectedBazelTargets: []string{`cc_library( |
| 573 | name = "a", |
| 574 | copts = [ |
| 575 | "-Wall", |
| 576 | "-fsigned-char", |
| 577 | "-pedantic", |
| 578 | "-Ifoo/bar", |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 579 | "-I$(BINDIR)/foo/bar", |
Jingwen Chen | 75be1ca | 2021-05-12 05:04:58 +0000 | [diff] [blame] | 580 | ] + select({ |
| 581 | "//build/bazel/platforms/arch:arm64": ["-DARM64=1"], |
| 582 | "//conditions:default": [], |
| 583 | }) + select({ |
| 584 | "//build/bazel/platforms/os:android": ["-DANDROID=1"], |
| 585 | "//conditions:default": [], |
| 586 | }), |
| 587 | srcs = ["a.cpp"], |
| 588 | )`}, |
| 589 | }, |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | dir := "." |
| 593 | for _, testCase := range testCases { |
| 594 | filesystem := make(map[string][]byte) |
| 595 | toParse := []string{ |
| 596 | "Android.bp", |
| 597 | } |
| 598 | for f, content := range testCase.filesystem { |
| 599 | if strings.HasSuffix(f, "Android.bp") { |
| 600 | toParse = append(toParse, f) |
| 601 | } |
| 602 | filesystem[f] = []byte(content) |
| 603 | } |
| 604 | config := android.TestConfig(buildDir, nil, testCase.bp, filesystem) |
| 605 | ctx := android.NewTestContext(config) |
| 606 | |
| 607 | cc.RegisterCCBuildComponents(ctx) |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 608 | ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory) |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 609 | ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory) |
| 610 | ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory) |
| 611 | ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory) |
| 612 | ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator) |
| 613 | ctx.RegisterBp2BuildConfig(bp2buildConfig) // TODO(jingwen): make this the default for all tests |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 614 | for _, m := range testCase.depsMutators { |
| 615 | ctx.DepsBp2BuildMutators(m) |
| 616 | } |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 617 | ctx.RegisterForBazelConversion() |
| 618 | |
| 619 | _, errs := ctx.ParseFileList(dir, toParse) |
Rupert Shuttleworth | 06559d0 | 2021-05-19 09:14:26 -0400 | [diff] [blame] | 620 | if errored(t, testCase.description, errs) { |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 621 | continue |
| 622 | } |
| 623 | _, errs = ctx.ResolveDependencies(config) |
Rupert Shuttleworth | 06559d0 | 2021-05-19 09:14:26 -0400 | [diff] [blame] | 624 | if errored(t, testCase.description, errs) { |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 625 | continue |
| 626 | } |
| 627 | |
| 628 | checkDir := dir |
| 629 | if testCase.dir != "" { |
| 630 | checkDir = testCase.dir |
| 631 | } |
| 632 | codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) |
| 633 | bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir) |
| 634 | if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount { |
| 635 | t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount) |
| 636 | } else { |
| 637 | for i, target := range bazelTargets { |
| 638 | if w, g := testCase.expectedBazelTargets[i], target.content; w != g { |
| 639 | t.Errorf( |
| 640 | "%s: Expected generated Bazel target to be '%s', got '%s'", |
| 641 | testCase.description, |
| 642 | w, |
| 643 | g, |
| 644 | ) |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | } |
| 649 | } |