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