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