cc_binary_conversion_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  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. package bp2build
  15. import (
  16. "fmt"
  17. "strings"
  18. "testing"
  19. "android/soong/android"
  20. "android/soong/cc"
  21. "android/soong/genrule"
  22. )
  23. const (
  24. ccBinaryTypePlaceHolder = "{rule_name}"
  25. )
  26. type testBazelTarget struct {
  27. typ string
  28. name string
  29. attrs AttrNameToString
  30. }
  31. func generateBazelTargetsForTest(targets []testBazelTarget, hod android.HostOrDeviceSupported) []string {
  32. ret := make([]string, 0, len(targets))
  33. for _, t := range targets {
  34. attrs := t.attrs.clone()
  35. ret = append(ret, makeBazelTargetHostOrDevice(t.typ, t.name, attrs, hod))
  36. }
  37. return ret
  38. }
  39. type ccBinaryBp2buildTestCase struct {
  40. description string
  41. filesystem map[string]string
  42. blueprint string
  43. targets []testBazelTarget
  44. }
  45. func registerCcBinaryModuleTypes(ctx android.RegistrationContext) {
  46. cc.RegisterCCBuildComponents(ctx)
  47. ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
  48. ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory)
  49. ctx.RegisterModuleType("cc_library", cc.LibraryFactory)
  50. ctx.RegisterModuleType("genrule", genrule.GenRuleFactory)
  51. }
  52. var binaryReplacer = strings.NewReplacer(ccBinaryTypePlaceHolder, "cc_binary")
  53. var hostBinaryReplacer = strings.NewReplacer(ccBinaryTypePlaceHolder, "cc_binary_host")
  54. func runCcBinaryTests(t *testing.T, tc ccBinaryBp2buildTestCase) {
  55. t.Helper()
  56. runCcBinaryTestCase(t, tc)
  57. runCcHostBinaryTestCase(t, tc)
  58. }
  59. func runCcBinaryTestCase(t *testing.T, testCase ccBinaryBp2buildTestCase) {
  60. t.Helper()
  61. moduleTypeUnderTest := "cc_binary"
  62. description := fmt.Sprintf("%s %s", moduleTypeUnderTest, testCase.description)
  63. t.Run(description, func(t *testing.T) {
  64. t.Helper()
  65. RunBp2BuildTestCase(t, registerCcBinaryModuleTypes, Bp2buildTestCase{
  66. ExpectedBazelTargets: generateBazelTargetsForTest(testCase.targets, android.DeviceSupported),
  67. ModuleTypeUnderTest: moduleTypeUnderTest,
  68. ModuleTypeUnderTestFactory: cc.BinaryFactory,
  69. Description: description,
  70. Blueprint: binaryReplacer.Replace(testCase.blueprint),
  71. Filesystem: testCase.filesystem,
  72. })
  73. })
  74. }
  75. func runCcHostBinaryTestCase(t *testing.T, testCase ccBinaryBp2buildTestCase) {
  76. t.Helper()
  77. moduleTypeUnderTest := "cc_binary_host"
  78. description := fmt.Sprintf("%s %s", moduleTypeUnderTest, testCase.description)
  79. t.Run(description, func(t *testing.T) {
  80. RunBp2BuildTestCase(t, registerCcBinaryModuleTypes, Bp2buildTestCase{
  81. ExpectedBazelTargets: generateBazelTargetsForTest(testCase.targets, android.HostSupported),
  82. ModuleTypeUnderTest: moduleTypeUnderTest,
  83. ModuleTypeUnderTestFactory: cc.BinaryHostFactory,
  84. Description: description,
  85. Blueprint: hostBinaryReplacer.Replace(testCase.blueprint),
  86. Filesystem: testCase.filesystem,
  87. })
  88. })
  89. }
  90. func TestBasicCcBinary(t *testing.T) {
  91. runCcBinaryTests(t, ccBinaryBp2buildTestCase{
  92. description: "basic -- properties -> attrs with little/no transformation",
  93. filesystem: map[string]string{
  94. soongCcVersionLibBpPath: soongCcVersionLibBp,
  95. },
  96. blueprint: `
  97. {rule_name} {
  98. name: "foo",
  99. srcs: ["a.cc"],
  100. local_include_dirs: ["dir"],
  101. include_dirs: ["absolute_dir"],
  102. cflags: ["-Dcopt"],
  103. cppflags: ["-Dcppflag"],
  104. conlyflags: ["-Dconlyflag"],
  105. asflags: ["-Dasflag"],
  106. ldflags: ["ld-flag"],
  107. rtti: true,
  108. strip: {
  109. all: true,
  110. keep_symbols: true,
  111. keep_symbols_and_debug_frame: true,
  112. keep_symbols_list: ["symbol"],
  113. none: true,
  114. },
  115. sdk_version: "current",
  116. min_sdk_version: "29",
  117. use_version_lib: true,
  118. }
  119. `,
  120. targets: []testBazelTarget{
  121. {"cc_binary", "foo", AttrNameToString{
  122. "absolute_includes": `["absolute_dir"]`,
  123. "asflags": `["-Dasflag"]`,
  124. "conlyflags": `["-Dconlyflag"]`,
  125. "copts": `["-Dcopt"]`,
  126. "cppflags": `["-Dcppflag"]`,
  127. "linkopts": `["ld-flag"]`,
  128. "local_includes": `[
  129. "dir",
  130. ".",
  131. ]`,
  132. "rtti": `True`,
  133. "srcs": `["a.cc"]`,
  134. "strip": `{
  135. "all": True,
  136. "keep_symbols": True,
  137. "keep_symbols_and_debug_frame": True,
  138. "keep_symbols_list": ["symbol"],
  139. "none": True,
  140. }`,
  141. "sdk_version": `"current"`,
  142. "min_sdk_version": `"29"`,
  143. "use_version_lib": `True`,
  144. "whole_archive_deps": `["//build/soong/cc/libbuildversion:libbuildversion"]`,
  145. },
  146. },
  147. },
  148. })
  149. }
  150. func TestCcBinaryWithSharedLdflagDisableFeature(t *testing.T) {
  151. runCcBinaryTests(t, ccBinaryBp2buildTestCase{
  152. description: `ldflag "-shared" disables static_flag feature`,
  153. blueprint: `
  154. {rule_name} {
  155. name: "foo",
  156. ldflags: ["-shared"],
  157. include_build_directory: false,
  158. }
  159. `,
  160. targets: []testBazelTarget{
  161. {"cc_binary", "foo", AttrNameToString{
  162. "features": `["-static_flag"]`,
  163. "linkopts": `["-shared"]`,
  164. },
  165. },
  166. },
  167. })
  168. }
  169. func TestCcBinaryWithLinkStatic(t *testing.T) {
  170. runCcBinaryTests(t, ccBinaryBp2buildTestCase{
  171. description: "link static",
  172. blueprint: `
  173. {rule_name} {
  174. name: "foo",
  175. static_executable: true,
  176. include_build_directory: false,
  177. }
  178. `,
  179. targets: []testBazelTarget{
  180. {"cc_binary", "foo", AttrNameToString{
  181. "linkshared": `False`,
  182. },
  183. },
  184. },
  185. })
  186. }
  187. func TestCcBinaryVersionScript(t *testing.T) {
  188. runCcBinaryTests(t, ccBinaryBp2buildTestCase{
  189. description: `version script`,
  190. blueprint: `
  191. {rule_name} {
  192. name: "foo",
  193. include_build_directory: false,
  194. version_script: "vs",
  195. }
  196. `,
  197. targets: []testBazelTarget{
  198. {"cc_binary", "foo", AttrNameToString{
  199. "additional_linker_inputs": `["vs"]`,
  200. "linkopts": `["-Wl,--version-script,$(location vs)"]`,
  201. },
  202. },
  203. },
  204. })
  205. }
  206. func TestCcBinarySplitSrcsByLang(t *testing.T) {
  207. runCcHostBinaryTestCase(t, ccBinaryBp2buildTestCase{
  208. description: "split srcs by lang",
  209. blueprint: `
  210. {rule_name} {
  211. name: "foo",
  212. srcs: [
  213. "asonly.S",
  214. "conly.c",
  215. "cpponly.cpp",
  216. ":fg_foo",
  217. ],
  218. include_build_directory: false,
  219. }
  220. ` + simpleModuleDoNotConvertBp2build("filegroup", "fg_foo"),
  221. targets: []testBazelTarget{
  222. {"cc_binary", "foo", AttrNameToString{
  223. "srcs": `[
  224. "cpponly.cpp",
  225. ":fg_foo_cpp_srcs",
  226. ]`,
  227. "srcs_as": `[
  228. "asonly.S",
  229. ":fg_foo_as_srcs",
  230. ]`,
  231. "srcs_c": `[
  232. "conly.c",
  233. ":fg_foo_c_srcs",
  234. ]`,
  235. },
  236. },
  237. },
  238. })
  239. }
  240. func TestCcBinaryDoNotDistinguishBetweenDepsAndImplementationDeps(t *testing.T) {
  241. runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
  242. description: "no implementation deps",
  243. blueprint: `
  244. genrule {
  245. name: "generated_hdr",
  246. cmd: "nothing to see here",
  247. bazel_module: { bp2build_available: false },
  248. }
  249. genrule {
  250. name: "export_generated_hdr",
  251. cmd: "nothing to see here",
  252. bazel_module: { bp2build_available: false },
  253. }
  254. {rule_name} {
  255. name: "foo",
  256. srcs: ["foo.cpp"],
  257. shared_libs: ["implementation_shared_dep", "shared_dep"],
  258. export_shared_lib_headers: ["shared_dep"],
  259. static_libs: ["implementation_static_dep", "static_dep"],
  260. export_static_lib_headers: ["static_dep", "whole_static_dep"],
  261. whole_static_libs: ["not_explicitly_exported_whole_static_dep", "whole_static_dep"],
  262. include_build_directory: false,
  263. generated_headers: ["generated_hdr", "export_generated_hdr"],
  264. export_generated_headers: ["export_generated_hdr"],
  265. }
  266. ` +
  267. simpleModuleDoNotConvertBp2build("cc_library_static", "static_dep") +
  268. simpleModuleDoNotConvertBp2build("cc_library_static", "implementation_static_dep") +
  269. simpleModuleDoNotConvertBp2build("cc_library_static", "whole_static_dep") +
  270. simpleModuleDoNotConvertBp2build("cc_library_static", "not_explicitly_exported_whole_static_dep") +
  271. simpleModuleDoNotConvertBp2build("cc_library", "shared_dep") +
  272. simpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep"),
  273. targets: []testBazelTarget{
  274. {"cc_binary", "foo", AttrNameToString{
  275. "deps": `[
  276. ":implementation_static_dep",
  277. ":static_dep",
  278. ]`,
  279. "dynamic_deps": `[
  280. ":implementation_shared_dep",
  281. ":shared_dep",
  282. ]`,
  283. "srcs": `[
  284. "foo.cpp",
  285. ":generated_hdr",
  286. ":export_generated_hdr",
  287. ]`,
  288. "whole_archive_deps": `[
  289. ":not_explicitly_exported_whole_static_dep",
  290. ":whole_static_dep",
  291. ]`,
  292. "local_includes": `["."]`,
  293. },
  294. },
  295. },
  296. })
  297. }
  298. func TestCcBinaryNocrtTests(t *testing.T) {
  299. baseTestCases := []struct {
  300. description string
  301. soongProperty string
  302. bazelAttr AttrNameToString
  303. }{
  304. {
  305. description: "nocrt: true",
  306. soongProperty: `nocrt: true,`,
  307. bazelAttr: AttrNameToString{"link_crt": `False`},
  308. },
  309. {
  310. description: "nocrt: false",
  311. soongProperty: `nocrt: false,`,
  312. bazelAttr: AttrNameToString{},
  313. },
  314. {
  315. description: "nocrt: not set",
  316. bazelAttr: AttrNameToString{},
  317. },
  318. }
  319. baseBlueprint := `{rule_name} {
  320. name: "foo",%s
  321. include_build_directory: false,
  322. }
  323. `
  324. for _, btc := range baseTestCases {
  325. prop := btc.soongProperty
  326. if len(prop) > 0 {
  327. prop = "\n" + prop
  328. }
  329. runCcBinaryTests(t, ccBinaryBp2buildTestCase{
  330. description: btc.description,
  331. blueprint: fmt.Sprintf(baseBlueprint, prop),
  332. targets: []testBazelTarget{
  333. {"cc_binary", "foo", btc.bazelAttr},
  334. },
  335. })
  336. }
  337. }
  338. func TestCcBinaryNo_libcrtTests(t *testing.T) {
  339. baseTestCases := []struct {
  340. description string
  341. soongProperty string
  342. bazelAttr AttrNameToString
  343. }{
  344. {
  345. description: "no_libcrt: true",
  346. soongProperty: `no_libcrt: true,`,
  347. bazelAttr: AttrNameToString{"use_libcrt": `False`},
  348. },
  349. {
  350. description: "no_libcrt: false",
  351. soongProperty: `no_libcrt: false,`,
  352. bazelAttr: AttrNameToString{"use_libcrt": `True`},
  353. },
  354. {
  355. description: "no_libcrt: not set",
  356. bazelAttr: AttrNameToString{},
  357. },
  358. }
  359. baseBlueprint := `{rule_name} {
  360. name: "foo",%s
  361. include_build_directory: false,
  362. }
  363. `
  364. for _, btc := range baseTestCases {
  365. prop := btc.soongProperty
  366. if len(prop) > 0 {
  367. prop = "\n" + prop
  368. }
  369. runCcBinaryTests(t, ccBinaryBp2buildTestCase{
  370. description: btc.description,
  371. blueprint: fmt.Sprintf(baseBlueprint, prop),
  372. targets: []testBazelTarget{
  373. {"cc_binary", "foo", btc.bazelAttr},
  374. },
  375. })
  376. }
  377. }
  378. func TestCcBinaryPropertiesToFeatures(t *testing.T) {
  379. baseTestCases := []struct {
  380. description string
  381. soongProperty string
  382. bazelAttr AttrNameToString
  383. }{
  384. {
  385. description: "pack_relocation: true",
  386. soongProperty: `pack_relocations: true,`,
  387. bazelAttr: AttrNameToString{},
  388. },
  389. {
  390. description: "pack_relocations: false",
  391. soongProperty: `pack_relocations: false,`,
  392. bazelAttr: AttrNameToString{"features": `["disable_pack_relocations"]`},
  393. },
  394. {
  395. description: "pack_relocations: not set",
  396. bazelAttr: AttrNameToString{},
  397. },
  398. {
  399. description: "pack_relocation: true",
  400. soongProperty: `allow_undefined_symbols: true,`,
  401. bazelAttr: AttrNameToString{"features": `["-no_undefined_symbols"]`},
  402. },
  403. {
  404. description: "allow_undefined_symbols: false",
  405. soongProperty: `allow_undefined_symbols: false,`,
  406. bazelAttr: AttrNameToString{},
  407. },
  408. {
  409. description: "allow_undefined_symbols: not set",
  410. bazelAttr: AttrNameToString{},
  411. },
  412. }
  413. baseBlueprint := `{rule_name} {
  414. name: "foo",%s
  415. include_build_directory: false,
  416. }
  417. `
  418. for _, btc := range baseTestCases {
  419. prop := btc.soongProperty
  420. if len(prop) > 0 {
  421. prop = "\n" + prop
  422. }
  423. runCcBinaryTests(t, ccBinaryBp2buildTestCase{
  424. description: btc.description,
  425. blueprint: fmt.Sprintf(baseBlueprint, prop),
  426. targets: []testBazelTarget{
  427. {"cc_binary", "foo", btc.bazelAttr},
  428. },
  429. })
  430. }
  431. }
  432. func TestCcBinarySharedProto(t *testing.T) {
  433. runCcBinaryTests(t, ccBinaryBp2buildTestCase{
  434. blueprint: soongCcProtoLibraries + `{rule_name} {
  435. name: "foo",
  436. srcs: ["foo.proto"],
  437. proto: {
  438. },
  439. include_build_directory: false,
  440. }`,
  441. targets: []testBazelTarget{
  442. {"proto_library", "foo_proto", AttrNameToString{
  443. "srcs": `["foo.proto"]`,
  444. }}, {"cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
  445. "deps": `[":foo_proto"]`,
  446. }}, {"cc_binary", "foo", AttrNameToString{
  447. "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
  448. "whole_archive_deps": `[":foo_cc_proto_lite"]`,
  449. }},
  450. },
  451. })
  452. }
  453. func TestCcBinaryStaticProto(t *testing.T) {
  454. runCcBinaryTests(t, ccBinaryBp2buildTestCase{
  455. blueprint: soongCcProtoLibraries + `{rule_name} {
  456. name: "foo",
  457. srcs: ["foo.proto"],
  458. static_executable: true,
  459. proto: {
  460. },
  461. include_build_directory: false,
  462. }`,
  463. targets: []testBazelTarget{
  464. {"proto_library", "foo_proto", AttrNameToString{
  465. "srcs": `["foo.proto"]`,
  466. }}, {"cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
  467. "deps": `[":foo_proto"]`,
  468. }}, {"cc_binary", "foo", AttrNameToString{
  469. "deps": `[":libprotobuf-cpp-lite"]`,
  470. "whole_archive_deps": `[":foo_cc_proto_lite"]`,
  471. "linkshared": `False`,
  472. }},
  473. },
  474. })
  475. }
  476. func TestCcBinaryConvertLex(t *testing.T) {
  477. runCcBinaryTests(t, ccBinaryBp2buildTestCase{
  478. description: `.l and .ll sources converted to .c and .cc`,
  479. blueprint: `
  480. {rule_name} {
  481. name: "foo",
  482. srcs: ["foo.c", "bar.cc", "foo1.l", "foo2.l", "bar1.ll", "bar2.ll"],
  483. lex: { flags: ["--foo_opt", "--bar_opt"] },
  484. include_build_directory: false,
  485. }
  486. `,
  487. targets: []testBazelTarget{
  488. {"genlex", "foo_genlex_l", AttrNameToString{
  489. "srcs": `[
  490. "foo1.l",
  491. "foo2.l",
  492. ]`,
  493. "lexopts": `[
  494. "--foo_opt",
  495. "--bar_opt",
  496. ]`,
  497. }},
  498. {"genlex", "foo_genlex_ll", AttrNameToString{
  499. "srcs": `[
  500. "bar1.ll",
  501. "bar2.ll",
  502. ]`,
  503. "lexopts": `[
  504. "--foo_opt",
  505. "--bar_opt",
  506. ]`,
  507. }},
  508. {"cc_binary", "foo", AttrNameToString{
  509. "srcs": `[
  510. "bar.cc",
  511. ":foo_genlex_ll",
  512. ]`,
  513. "srcs_c": `[
  514. "foo.c",
  515. ":foo_genlex_l",
  516. ]`,
  517. }},
  518. },
  519. })
  520. }
  521. func TestCcBinaryRuntimeLibs(t *testing.T) {
  522. runCcBinaryTests(t, ccBinaryBp2buildTestCase{
  523. description: "cc_binary with runtime libs",
  524. blueprint: `
  525. cc_library {
  526. name: "bar",
  527. srcs: ["b.cc"],
  528. }
  529. {rule_name} {
  530. name: "foo",
  531. srcs: ["a.cc"],
  532. runtime_libs: ["bar"],
  533. }
  534. `,
  535. targets: []testBazelTarget{
  536. {"cc_library_static", "bar_bp2build_cc_library_static", AttrNameToString{
  537. "local_includes": `["."]`,
  538. "srcs": `["b.cc"]`,
  539. "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
  540. },
  541. },
  542. {"cc_library_shared", "bar", AttrNameToString{
  543. "local_includes": `["."]`,
  544. "srcs": `["b.cc"]`,
  545. "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
  546. },
  547. },
  548. {"cc_binary", "foo", AttrNameToString{
  549. "local_includes": `["."]`,
  550. "srcs": `["a.cc"]`,
  551. "runtime_deps": `[":bar"]`,
  552. },
  553. },
  554. },
  555. })
  556. }
  557. func TestCcBinaryWithInstructionSet(t *testing.T) {
  558. runCcBinaryTests(t, ccBinaryBp2buildTestCase{
  559. description: "instruction set",
  560. blueprint: `
  561. {rule_name} {
  562. name: "foo",
  563. arch: {
  564. arm: {
  565. instruction_set: "arm",
  566. }
  567. }
  568. }
  569. `,
  570. targets: []testBazelTarget{
  571. {"cc_binary", "foo", AttrNameToString{
  572. "features": `select({
  573. "//build/bazel/platforms/arch:arm": [
  574. "arm_isa_arm",
  575. "-arm_isa_thumb",
  576. ],
  577. "//conditions:default": [],
  578. })`,
  579. "local_includes": `["."]`,
  580. }},
  581. },
  582. })
  583. }
  584. func TestCcBinaryEmptySuffix(t *testing.T) {
  585. runCcBinaryTests(t, ccBinaryBp2buildTestCase{
  586. description: "binary with empty suffix",
  587. blueprint: `
  588. {rule_name} {
  589. name: "foo",
  590. suffix: "",
  591. }`,
  592. targets: []testBazelTarget{
  593. {"cc_binary", "foo", AttrNameToString{
  594. "local_includes": `["."]`,
  595. "suffix": `""`,
  596. }},
  597. },
  598. })
  599. }
  600. func TestCcBinarySuffix(t *testing.T) {
  601. runCcBinaryTests(t, ccBinaryBp2buildTestCase{
  602. description: "binary with suffix",
  603. blueprint: `
  604. {rule_name} {
  605. name: "foo",
  606. suffix: "-suf",
  607. }
  608. `,
  609. targets: []testBazelTarget{
  610. {"cc_binary", "foo", AttrNameToString{
  611. "local_includes": `["."]`,
  612. "suffix": `"-suf"`,
  613. }},
  614. },
  615. })
  616. }
  617. func TestCcArchVariantBinarySuffix(t *testing.T) {
  618. runCcBinaryTests(t, ccBinaryBp2buildTestCase{
  619. description: "binary with suffix",
  620. blueprint: `
  621. {rule_name} {
  622. name: "foo",
  623. arch: {
  624. arm64: { suffix: "-64" },
  625. arm: { suffix: "-32" },
  626. },
  627. }
  628. `,
  629. targets: []testBazelTarget{
  630. {"cc_binary", "foo", AttrNameToString{
  631. "local_includes": `["."]`,
  632. "suffix": `select({
  633. "//build/bazel/platforms/arch:arm": "-32",
  634. "//build/bazel/platforms/arch:arm64": "-64",
  635. "//conditions:default": None,
  636. })`,
  637. }},
  638. },
  639. })
  640. }
  641. func TestCcBinaryWithSyspropSrcs(t *testing.T) {
  642. runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
  643. description: "cc_binary with sysprop sources",
  644. blueprint: `
  645. {rule_name} {
  646. name: "foo",
  647. srcs: [
  648. "bar.sysprop",
  649. "baz.sysprop",
  650. "blah.cpp",
  651. ],
  652. min_sdk_version: "5",
  653. }`,
  654. targets: []testBazelTarget{
  655. {"sysprop_library", "foo_sysprop_library", AttrNameToString{
  656. "srcs": `[
  657. "bar.sysprop",
  658. "baz.sysprop",
  659. ]`,
  660. }},
  661. {"cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
  662. "dep": `":foo_sysprop_library"`,
  663. "min_sdk_version": `"5"`,
  664. }},
  665. {"cc_binary", "foo", AttrNameToString{
  666. "srcs": `["blah.cpp"]`,
  667. "local_includes": `["."]`,
  668. "min_sdk_version": `"5"`,
  669. "whole_archive_deps": `[":foo_cc_sysprop_library_static"]`,
  670. }},
  671. },
  672. })
  673. }
  674. func TestCcBinaryWithSyspropSrcsSomeConfigs(t *testing.T) {
  675. runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
  676. description: "cc_binary with sysprop sources in some configs but not others",
  677. blueprint: `
  678. {rule_name} {
  679. name: "foo",
  680. srcs: [
  681. "blah.cpp",
  682. ],
  683. target: {
  684. android: {
  685. srcs: ["bar.sysprop"],
  686. },
  687. },
  688. min_sdk_version: "5",
  689. }`,
  690. targets: []testBazelTarget{
  691. {"sysprop_library", "foo_sysprop_library", AttrNameToString{
  692. "srcs": `select({
  693. "//build/bazel/platforms/os:android": ["bar.sysprop"],
  694. "//conditions:default": [],
  695. })`,
  696. }},
  697. {"cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
  698. "dep": `":foo_sysprop_library"`,
  699. "min_sdk_version": `"5"`,
  700. }},
  701. {"cc_binary", "foo", AttrNameToString{
  702. "srcs": `["blah.cpp"]`,
  703. "local_includes": `["."]`,
  704. "min_sdk_version": `"5"`,
  705. "whole_archive_deps": `select({
  706. "//build/bazel/platforms/os:android": [":foo_cc_sysprop_library_static"],
  707. "//conditions:default": [],
  708. })`,
  709. }},
  710. },
  711. })
  712. }