cc_binary_conversion_test.go 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  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 TestCcBinaryVersionScriptAndDynamicList(t *testing.T) {
  188. runCcBinaryTests(t, ccBinaryBp2buildTestCase{
  189. description: `version script and dynamic list`,
  190. blueprint: `
  191. {rule_name} {
  192. name: "foo",
  193. include_build_directory: false,
  194. version_script: "vs",
  195. dynamic_list: "dynamic.list",
  196. }
  197. `,
  198. targets: []testBazelTarget{
  199. {"cc_binary", "foo", AttrNameToString{
  200. "additional_linker_inputs": `[
  201. "vs",
  202. "dynamic.list",
  203. ]`,
  204. "linkopts": `[
  205. "-Wl,--version-script,$(location vs)",
  206. "-Wl,--dynamic-list,$(location dynamic.list)",
  207. ]`,
  208. "features": `["android_cfi_exports_map"]`,
  209. },
  210. },
  211. },
  212. })
  213. }
  214. func TestCcBinaryLdflagsSplitBySpaceExceptSoongAdded(t *testing.T) {
  215. runCcBinaryTests(t, ccBinaryBp2buildTestCase{
  216. description: "ldflags are split by spaces except for the ones added by soong (version script and dynamic list)",
  217. blueprint: `
  218. {rule_name} {
  219. name: "foo",
  220. ldflags: [
  221. "--nospace_flag",
  222. "-z spaceflag",
  223. ],
  224. version_script: "version_script",
  225. dynamic_list: "dynamic.list",
  226. include_build_directory: false,
  227. }
  228. `,
  229. targets: []testBazelTarget{
  230. {"cc_binary", "foo", AttrNameToString{
  231. "additional_linker_inputs": `[
  232. "version_script",
  233. "dynamic.list",
  234. ]`,
  235. "features": `["android_cfi_exports_map"]`,
  236. "linkopts": `[
  237. "--nospace_flag",
  238. "-z",
  239. "spaceflag",
  240. "-Wl,--version-script,$(location version_script)",
  241. "-Wl,--dynamic-list,$(location dynamic.list)",
  242. ]`,
  243. }}},
  244. })
  245. }
  246. func TestCcBinarySplitSrcsByLang(t *testing.T) {
  247. runCcHostBinaryTestCase(t, ccBinaryBp2buildTestCase{
  248. description: "split srcs by lang",
  249. blueprint: `
  250. {rule_name} {
  251. name: "foo",
  252. srcs: [
  253. "asonly.S",
  254. "conly.c",
  255. "cpponly.cpp",
  256. ":fg_foo",
  257. ],
  258. include_build_directory: false,
  259. }
  260. ` + simpleModuleDoNotConvertBp2build("filegroup", "fg_foo"),
  261. targets: []testBazelTarget{
  262. {"cc_binary", "foo", AttrNameToString{
  263. "srcs": `[
  264. "cpponly.cpp",
  265. ":fg_foo_cpp_srcs",
  266. ]`,
  267. "srcs_as": `[
  268. "asonly.S",
  269. ":fg_foo_as_srcs",
  270. ]`,
  271. "srcs_c": `[
  272. "conly.c",
  273. ":fg_foo_c_srcs",
  274. ]`,
  275. },
  276. },
  277. },
  278. })
  279. }
  280. func TestCcBinaryDoNotDistinguishBetweenDepsAndImplementationDeps(t *testing.T) {
  281. runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
  282. description: "no implementation deps",
  283. blueprint: `
  284. genrule {
  285. name: "generated_hdr",
  286. cmd: "nothing to see here",
  287. bazel_module: { bp2build_available: false },
  288. }
  289. genrule {
  290. name: "export_generated_hdr",
  291. cmd: "nothing to see here",
  292. bazel_module: { bp2build_available: false },
  293. }
  294. {rule_name} {
  295. name: "foo",
  296. srcs: ["foo.cpp"],
  297. shared_libs: ["implementation_shared_dep", "shared_dep"],
  298. export_shared_lib_headers: ["shared_dep"],
  299. static_libs: ["implementation_static_dep", "static_dep"],
  300. export_static_lib_headers: ["static_dep", "whole_static_dep"],
  301. whole_static_libs: ["not_explicitly_exported_whole_static_dep", "whole_static_dep"],
  302. include_build_directory: false,
  303. generated_headers: ["generated_hdr", "export_generated_hdr"],
  304. export_generated_headers: ["export_generated_hdr"],
  305. }
  306. ` +
  307. simpleModuleDoNotConvertBp2build("cc_library_static", "static_dep") +
  308. simpleModuleDoNotConvertBp2build("cc_library_static", "implementation_static_dep") +
  309. simpleModuleDoNotConvertBp2build("cc_library_static", "whole_static_dep") +
  310. simpleModuleDoNotConvertBp2build("cc_library_static", "not_explicitly_exported_whole_static_dep") +
  311. simpleModuleDoNotConvertBp2build("cc_library", "shared_dep") +
  312. simpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep"),
  313. targets: []testBazelTarget{
  314. {"cc_binary", "foo", AttrNameToString{
  315. "deps": `[
  316. ":implementation_static_dep",
  317. ":static_dep",
  318. ]`,
  319. "dynamic_deps": `[
  320. ":implementation_shared_dep",
  321. ":shared_dep",
  322. ]`,
  323. "srcs": `[
  324. "foo.cpp",
  325. ":generated_hdr",
  326. ":export_generated_hdr",
  327. ]`,
  328. "whole_archive_deps": `[
  329. ":not_explicitly_exported_whole_static_dep",
  330. ":whole_static_dep",
  331. ]`,
  332. "local_includes": `["."]`,
  333. },
  334. },
  335. },
  336. })
  337. }
  338. func TestCcBinaryNocrtTests(t *testing.T) {
  339. baseTestCases := []struct {
  340. description string
  341. soongProperty string
  342. bazelAttr AttrNameToString
  343. }{
  344. {
  345. description: "nocrt: true",
  346. soongProperty: `nocrt: true,`,
  347. bazelAttr: AttrNameToString{"features": `["-link_crt"]`},
  348. },
  349. {
  350. description: "nocrt: false",
  351. soongProperty: `nocrt: false,`,
  352. bazelAttr: AttrNameToString{},
  353. },
  354. {
  355. description: "nocrt: 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 TestCcBinaryNo_libcrtTests(t *testing.T) {
  379. baseTestCases := []struct {
  380. description string
  381. soongProperty string
  382. bazelAttr AttrNameToString
  383. }{
  384. {
  385. description: "no_libcrt: true",
  386. soongProperty: `no_libcrt: true,`,
  387. bazelAttr: AttrNameToString{"features": `["-use_libcrt"]`},
  388. },
  389. {
  390. description: "no_libcrt: false",
  391. soongProperty: `no_libcrt: false,`,
  392. bazelAttr: AttrNameToString{},
  393. },
  394. {
  395. description: "no_libcrt: not set",
  396. bazelAttr: AttrNameToString{},
  397. },
  398. }
  399. baseBlueprint := `{rule_name} {
  400. name: "foo",%s
  401. include_build_directory: false,
  402. }
  403. `
  404. for _, btc := range baseTestCases {
  405. prop := btc.soongProperty
  406. if len(prop) > 0 {
  407. prop = "\n" + prop
  408. }
  409. runCcBinaryTests(t, ccBinaryBp2buildTestCase{
  410. description: btc.description,
  411. blueprint: fmt.Sprintf(baseBlueprint, prop),
  412. targets: []testBazelTarget{
  413. {"cc_binary", "foo", btc.bazelAttr},
  414. },
  415. })
  416. }
  417. }
  418. func TestCcBinaryPropertiesToFeatures(t *testing.T) {
  419. baseTestCases := []struct {
  420. description string
  421. soongProperty string
  422. bazelAttr AttrNameToString
  423. }{
  424. {
  425. description: "pack_relocation: true",
  426. soongProperty: `pack_relocations: true,`,
  427. bazelAttr: AttrNameToString{},
  428. },
  429. {
  430. description: "pack_relocations: false",
  431. soongProperty: `pack_relocations: false,`,
  432. bazelAttr: AttrNameToString{"features": `["disable_pack_relocations"]`},
  433. },
  434. {
  435. description: "pack_relocations: not set",
  436. bazelAttr: AttrNameToString{},
  437. },
  438. {
  439. description: "pack_relocation: true",
  440. soongProperty: `allow_undefined_symbols: true,`,
  441. bazelAttr: AttrNameToString{"features": `["-no_undefined_symbols"]`},
  442. },
  443. {
  444. description: "allow_undefined_symbols: false",
  445. soongProperty: `allow_undefined_symbols: false,`,
  446. bazelAttr: AttrNameToString{},
  447. },
  448. {
  449. description: "allow_undefined_symbols: not set",
  450. bazelAttr: AttrNameToString{},
  451. },
  452. }
  453. baseBlueprint := `{rule_name} {
  454. name: "foo",%s
  455. include_build_directory: false,
  456. }
  457. `
  458. for _, btc := range baseTestCases {
  459. prop := btc.soongProperty
  460. if len(prop) > 0 {
  461. prop = "\n" + prop
  462. }
  463. runCcBinaryTests(t, ccBinaryBp2buildTestCase{
  464. description: btc.description,
  465. blueprint: fmt.Sprintf(baseBlueprint, prop),
  466. targets: []testBazelTarget{
  467. {"cc_binary", "foo", btc.bazelAttr},
  468. },
  469. })
  470. }
  471. }
  472. func TestCcBinarySharedProto(t *testing.T) {
  473. runCcBinaryTests(t, ccBinaryBp2buildTestCase{
  474. blueprint: soongCcProtoLibraries + `{rule_name} {
  475. name: "foo",
  476. srcs: ["foo.proto"],
  477. proto: {
  478. },
  479. include_build_directory: false,
  480. }`,
  481. targets: []testBazelTarget{
  482. {"proto_library", "foo_proto", AttrNameToString{
  483. "srcs": `["foo.proto"]`,
  484. }}, {"cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
  485. "deps": `[":foo_proto"]`,
  486. }}, {"cc_binary", "foo", AttrNameToString{
  487. "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
  488. "whole_archive_deps": `[":foo_cc_proto_lite"]`,
  489. }},
  490. },
  491. })
  492. }
  493. func TestCcBinaryStaticProto(t *testing.T) {
  494. runCcBinaryTests(t, ccBinaryBp2buildTestCase{
  495. blueprint: soongCcProtoLibraries + `{rule_name} {
  496. name: "foo",
  497. srcs: ["foo.proto"],
  498. static_executable: true,
  499. proto: {
  500. },
  501. include_build_directory: false,
  502. }`,
  503. targets: []testBazelTarget{
  504. {"proto_library", "foo_proto", AttrNameToString{
  505. "srcs": `["foo.proto"]`,
  506. }}, {"cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
  507. "deps": `[":foo_proto"]`,
  508. }}, {"cc_binary", "foo", AttrNameToString{
  509. "deps": `[":libprotobuf-cpp-lite"]`,
  510. "whole_archive_deps": `[":foo_cc_proto_lite"]`,
  511. "linkshared": `False`,
  512. }},
  513. },
  514. })
  515. }
  516. func TestCcBinaryConvertLex(t *testing.T) {
  517. runCcBinaryTests(t, ccBinaryBp2buildTestCase{
  518. description: `.l and .ll sources converted to .c and .cc`,
  519. blueprint: `
  520. {rule_name} {
  521. name: "foo",
  522. srcs: ["foo.c", "bar.cc", "foo1.l", "foo2.l", "bar1.ll", "bar2.ll"],
  523. lex: { flags: ["--foo_opt", "--bar_opt"] },
  524. include_build_directory: false,
  525. }
  526. `,
  527. targets: []testBazelTarget{
  528. {"genlex", "foo_genlex_l", AttrNameToString{
  529. "srcs": `[
  530. "foo1.l",
  531. "foo2.l",
  532. ]`,
  533. "lexopts": `[
  534. "--foo_opt",
  535. "--bar_opt",
  536. ]`,
  537. }},
  538. {"genlex", "foo_genlex_ll", AttrNameToString{
  539. "srcs": `[
  540. "bar1.ll",
  541. "bar2.ll",
  542. ]`,
  543. "lexopts": `[
  544. "--foo_opt",
  545. "--bar_opt",
  546. ]`,
  547. }},
  548. {"cc_binary", "foo", AttrNameToString{
  549. "srcs": `[
  550. "bar.cc",
  551. ":foo_genlex_ll",
  552. ]`,
  553. "srcs_c": `[
  554. "foo.c",
  555. ":foo_genlex_l",
  556. ]`,
  557. }},
  558. },
  559. })
  560. }
  561. func TestCcBinaryRuntimeLibs(t *testing.T) {
  562. runCcBinaryTests(t, ccBinaryBp2buildTestCase{
  563. description: "cc_binary with runtime libs",
  564. blueprint: `
  565. cc_library {
  566. name: "bar",
  567. srcs: ["b.cc"],
  568. }
  569. {rule_name} {
  570. name: "foo",
  571. srcs: ["a.cc"],
  572. runtime_libs: ["bar"],
  573. }
  574. `,
  575. targets: []testBazelTarget{
  576. {"cc_library_static", "bar_bp2build_cc_library_static", AttrNameToString{
  577. "local_includes": `["."]`,
  578. "srcs": `["b.cc"]`,
  579. "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
  580. },
  581. },
  582. {"cc_library_shared", "bar", AttrNameToString{
  583. "local_includes": `["."]`,
  584. "srcs": `["b.cc"]`,
  585. "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
  586. },
  587. },
  588. {"cc_binary", "foo", AttrNameToString{
  589. "local_includes": `["."]`,
  590. "srcs": `["a.cc"]`,
  591. "runtime_deps": `[":bar"]`,
  592. },
  593. },
  594. },
  595. })
  596. }
  597. func TestCcBinaryWithInstructionSet(t *testing.T) {
  598. runCcBinaryTests(t, ccBinaryBp2buildTestCase{
  599. description: "instruction set",
  600. blueprint: `
  601. {rule_name} {
  602. name: "foo",
  603. arch: {
  604. arm: {
  605. instruction_set: "arm",
  606. }
  607. }
  608. }
  609. `,
  610. targets: []testBazelTarget{
  611. {"cc_binary", "foo", AttrNameToString{
  612. "features": `select({
  613. "//build/bazel/platforms/arch:arm": ["arm_isa_arm"],
  614. "//conditions:default": [],
  615. })`,
  616. "local_includes": `["."]`,
  617. }},
  618. },
  619. })
  620. }
  621. func TestCcBinaryEmptySuffix(t *testing.T) {
  622. runCcBinaryTests(t, ccBinaryBp2buildTestCase{
  623. description: "binary with empty suffix",
  624. blueprint: `
  625. {rule_name} {
  626. name: "foo",
  627. suffix: "",
  628. }`,
  629. targets: []testBazelTarget{
  630. {"cc_binary", "foo", AttrNameToString{
  631. "local_includes": `["."]`,
  632. "suffix": `""`,
  633. }},
  634. },
  635. })
  636. }
  637. func TestCcBinarySuffix(t *testing.T) {
  638. runCcBinaryTests(t, ccBinaryBp2buildTestCase{
  639. description: "binary with suffix",
  640. blueprint: `
  641. {rule_name} {
  642. name: "foo",
  643. suffix: "-suf",
  644. }
  645. `,
  646. targets: []testBazelTarget{
  647. {"cc_binary", "foo", AttrNameToString{
  648. "local_includes": `["."]`,
  649. "suffix": `"-suf"`,
  650. }},
  651. },
  652. })
  653. }
  654. func TestCcArchVariantBinarySuffix(t *testing.T) {
  655. runCcBinaryTests(t, ccBinaryBp2buildTestCase{
  656. description: "binary with suffix",
  657. blueprint: `
  658. {rule_name} {
  659. name: "foo",
  660. arch: {
  661. arm64: { suffix: "-64" },
  662. arm: { suffix: "-32" },
  663. },
  664. }
  665. `,
  666. targets: []testBazelTarget{
  667. {"cc_binary", "foo", AttrNameToString{
  668. "local_includes": `["."]`,
  669. "suffix": `select({
  670. "//build/bazel/platforms/arch:arm": "-32",
  671. "//build/bazel/platforms/arch:arm64": "-64",
  672. "//conditions:default": None,
  673. })`,
  674. }},
  675. },
  676. })
  677. }
  678. func TestCcBinaryWithSyspropSrcs(t *testing.T) {
  679. runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
  680. description: "cc_binary with sysprop sources",
  681. blueprint: `
  682. {rule_name} {
  683. name: "foo",
  684. srcs: [
  685. "bar.sysprop",
  686. "baz.sysprop",
  687. "blah.cpp",
  688. ],
  689. min_sdk_version: "5",
  690. }`,
  691. targets: []testBazelTarget{
  692. {"sysprop_library", "foo_sysprop_library", AttrNameToString{
  693. "srcs": `[
  694. "bar.sysprop",
  695. "baz.sysprop",
  696. ]`,
  697. }},
  698. {"cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
  699. "dep": `":foo_sysprop_library"`,
  700. "min_sdk_version": `"5"`,
  701. }},
  702. {"cc_binary", "foo", AttrNameToString{
  703. "srcs": `["blah.cpp"]`,
  704. "local_includes": `["."]`,
  705. "min_sdk_version": `"5"`,
  706. "whole_archive_deps": `[":foo_cc_sysprop_library_static"]`,
  707. }},
  708. },
  709. })
  710. }
  711. func TestCcBinaryWithSyspropSrcsSomeConfigs(t *testing.T) {
  712. runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
  713. description: "cc_binary with sysprop sources in some configs but not others",
  714. blueprint: `
  715. {rule_name} {
  716. name: "foo",
  717. srcs: [
  718. "blah.cpp",
  719. ],
  720. target: {
  721. android: {
  722. srcs: ["bar.sysprop"],
  723. },
  724. },
  725. min_sdk_version: "5",
  726. }`,
  727. targets: []testBazelTarget{
  728. {"sysprop_library", "foo_sysprop_library", AttrNameToString{
  729. "srcs": `select({
  730. "//build/bazel/platforms/os:android": ["bar.sysprop"],
  731. "//conditions:default": [],
  732. })`,
  733. }},
  734. {"cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
  735. "dep": `":foo_sysprop_library"`,
  736. "min_sdk_version": `"5"`,
  737. }},
  738. {"cc_binary", "foo", AttrNameToString{
  739. "srcs": `["blah.cpp"]`,
  740. "local_includes": `["."]`,
  741. "min_sdk_version": `"5"`,
  742. "whole_archive_deps": `select({
  743. "//build/bazel/platforms/os:android": [":foo_cc_sysprop_library_static"],
  744. "//conditions:default": [],
  745. })`,
  746. }},
  747. },
  748. })
  749. }
  750. func TestCcBinaryWithIntegerOverflowProperty(t *testing.T) {
  751. runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
  752. description: "cc_binary with integer overflow property specified",
  753. blueprint: `
  754. {rule_name} {
  755. name: "foo",
  756. sanitize: {
  757. integer_overflow: true,
  758. },
  759. }`,
  760. targets: []testBazelTarget{
  761. {"cc_binary", "foo", AttrNameToString{
  762. "local_includes": `["."]`,
  763. "features": `["ubsan_integer_overflow"]`,
  764. }},
  765. },
  766. })
  767. }
  768. func TestCcBinaryWithMiscUndefinedProperty(t *testing.T) {
  769. runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
  770. description: "cc_binary with miscellaneous properties specified",
  771. blueprint: `
  772. {rule_name} {
  773. name: "foo",
  774. sanitize: {
  775. misc_undefined: ["undefined", "nullability"],
  776. },
  777. }`,
  778. targets: []testBazelTarget{
  779. {"cc_binary", "foo", AttrNameToString{
  780. "local_includes": `["."]`,
  781. "features": `[
  782. "ubsan_undefined",
  783. "ubsan_nullability",
  784. ]`,
  785. }},
  786. },
  787. })
  788. }
  789. func TestCcBinaryWithUBSanPropertiesArchSpecific(t *testing.T) {
  790. runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
  791. description: "cc_binary has correct feature select when UBSan props are specified in arch specific blocks",
  792. blueprint: `
  793. {rule_name} {
  794. name: "foo",
  795. sanitize: {
  796. misc_undefined: ["undefined", "nullability"],
  797. },
  798. target: {
  799. android: {
  800. sanitize: {
  801. misc_undefined: ["alignment"],
  802. },
  803. },
  804. linux_glibc: {
  805. sanitize: {
  806. integer_overflow: true,
  807. },
  808. },
  809. },
  810. }`,
  811. targets: []testBazelTarget{
  812. {"cc_binary", "foo", AttrNameToString{
  813. "local_includes": `["."]`,
  814. "features": `[
  815. "ubsan_undefined",
  816. "ubsan_nullability",
  817. ] + select({
  818. "//build/bazel/platforms/os:android": ["ubsan_alignment"],
  819. "//build/bazel/platforms/os:linux_glibc": ["ubsan_integer_overflow"],
  820. "//conditions:default": [],
  821. })`,
  822. }},
  823. },
  824. })
  825. }
  826. func TestCcBinaryWithSanitizerBlocklist(t *testing.T) {
  827. runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
  828. description: "cc_binary has the correct feature when sanitize.blocklist is provided",
  829. blueprint: `
  830. {rule_name} {
  831. name: "foo",
  832. sanitize: {
  833. blocklist: "foo_blocklist.txt",
  834. },
  835. }`,
  836. targets: []testBazelTarget{
  837. {"cc_binary", "foo", AttrNameToString{
  838. "local_includes": `["."]`,
  839. "features": `["ubsan_blocklist_foo_blocklist_txt"]`,
  840. }},
  841. },
  842. })
  843. }
  844. func TestCcBinaryWithThinLto(t *testing.T) {
  845. runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
  846. description: "cc_binary has correct features when thin LTO is enabled",
  847. blueprint: `
  848. {rule_name} {
  849. name: "foo",
  850. lto: {
  851. thin: true,
  852. },
  853. }`,
  854. targets: []testBazelTarget{
  855. {"cc_binary", "foo", AttrNameToString{
  856. "local_includes": `["."]`,
  857. "features": `["android_thin_lto"]`,
  858. }},
  859. },
  860. })
  861. }
  862. func TestCcBinaryWithLtoNever(t *testing.T) {
  863. runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
  864. description: "cc_binary has correct features when LTO is explicitly disabled",
  865. blueprint: `
  866. {rule_name} {
  867. name: "foo",
  868. lto: {
  869. never: true,
  870. },
  871. }`,
  872. targets: []testBazelTarget{
  873. {"cc_binary", "foo", AttrNameToString{
  874. "local_includes": `["."]`,
  875. "features": `["-android_thin_lto"]`,
  876. }},
  877. },
  878. })
  879. }
  880. func TestCcBinaryWithThinLtoArchSpecific(t *testing.T) {
  881. runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
  882. description: "cc_binary has correct features when LTO differs across arch and os variants",
  883. blueprint: `
  884. {rule_name} {
  885. name: "foo",
  886. target: {
  887. android: {
  888. lto: {
  889. thin: true,
  890. },
  891. },
  892. },
  893. arch: {
  894. riscv64: {
  895. lto: {
  896. thin: false,
  897. },
  898. },
  899. },
  900. }`,
  901. targets: []testBazelTarget{
  902. {"cc_binary", "foo", AttrNameToString{
  903. "local_includes": `["."]`,
  904. "features": `select({
  905. "//build/bazel/platforms/os_arch:android_arm": ["android_thin_lto"],
  906. "//build/bazel/platforms/os_arch:android_arm64": ["android_thin_lto"],
  907. "//build/bazel/platforms/os_arch:android_riscv64": ["-android_thin_lto"],
  908. "//build/bazel/platforms/os_arch:android_x86": ["android_thin_lto"],
  909. "//build/bazel/platforms/os_arch:android_x86_64": ["android_thin_lto"],
  910. "//conditions:default": [],
  911. })`,
  912. }},
  913. },
  914. })
  915. }
  916. func TestCcBinaryWithThinLtoDisabledDefaultEnabledVariant(t *testing.T) {
  917. runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
  918. description: "cc_binary has correct features when LTO disabled by default but enabled on a particular variant",
  919. blueprint: `
  920. {rule_name} {
  921. name: "foo",
  922. lto: {
  923. never: true,
  924. },
  925. target: {
  926. android: {
  927. lto: {
  928. thin: true,
  929. never: false,
  930. },
  931. },
  932. },
  933. }`,
  934. targets: []testBazelTarget{
  935. {"cc_binary", "foo", AttrNameToString{
  936. "local_includes": `["."]`,
  937. "features": `select({
  938. "//build/bazel/platforms/os:android": ["android_thin_lto"],
  939. "//conditions:default": ["-android_thin_lto"],
  940. })`,
  941. }},
  942. },
  943. })
  944. }
  945. func TestCcBinaryWithThinLtoAndWholeProgramVtables(t *testing.T) {
  946. runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
  947. description: "cc_binary has correct features when thin LTO is enabled with whole_program_vtables",
  948. blueprint: `
  949. {rule_name} {
  950. name: "foo",
  951. lto: {
  952. thin: true,
  953. },
  954. whole_program_vtables: true,
  955. }`,
  956. targets: []testBazelTarget{
  957. {"cc_binary", "foo", AttrNameToString{
  958. "local_includes": `["."]`,
  959. "features": `[
  960. "android_thin_lto",
  961. "android_thin_lto_whole_program_vtables",
  962. ]`,
  963. }},
  964. },
  965. })
  966. }
  967. func TestCcBinaryHiddenVisibilityConvertedToFeature(t *testing.T) {
  968. runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
  969. description: "cc_binary changes hidden visibility to feature",
  970. blueprint: `
  971. {rule_name} {
  972. name: "foo",
  973. cflags: ["-fvisibility=hidden"],
  974. }`,
  975. targets: []testBazelTarget{
  976. {"cc_binary", "foo", AttrNameToString{
  977. "local_includes": `["."]`,
  978. "features": `["visibility_hidden"]`,
  979. }},
  980. },
  981. })
  982. }
  983. func TestCcBinaryHiddenVisibilityConvertedToFeatureOsSpecific(t *testing.T) {
  984. runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
  985. description: "cc_binary changes hidden visibility to feature for specific os",
  986. blueprint: `
  987. {rule_name} {
  988. name: "foo",
  989. target: {
  990. android: {
  991. cflags: ["-fvisibility=hidden"],
  992. },
  993. },
  994. }`,
  995. targets: []testBazelTarget{
  996. {"cc_binary", "foo", AttrNameToString{
  997. "local_includes": `["."]`,
  998. "features": `select({
  999. "//build/bazel/platforms/os:android": ["visibility_hidden"],
  1000. "//conditions:default": [],
  1001. })`,
  1002. }},
  1003. },
  1004. })
  1005. }
  1006. func TestCcBinaryWithCfi(t *testing.T) {
  1007. runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
  1008. description: "cc_binary has correct features when cfi is enabled",
  1009. blueprint: `
  1010. {rule_name} {
  1011. name: "foo",
  1012. sanitize: {
  1013. cfi: true,
  1014. },
  1015. }`,
  1016. targets: []testBazelTarget{
  1017. {"cc_binary", "foo", AttrNameToString{
  1018. "features": `["android_cfi"]`,
  1019. "local_includes": `["."]`,
  1020. }},
  1021. },
  1022. })
  1023. }
  1024. func TestCcBinaryWithCfiOsSpecific(t *testing.T) {
  1025. runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
  1026. description: "cc_binary has correct features when cfi is enabled for specific variants",
  1027. blueprint: `
  1028. {rule_name} {
  1029. name: "foo",
  1030. target: {
  1031. android: {
  1032. sanitize: {
  1033. cfi: true,
  1034. },
  1035. },
  1036. },
  1037. }`,
  1038. targets: []testBazelTarget{
  1039. {"cc_binary", "foo", AttrNameToString{
  1040. "features": `select({
  1041. "//build/bazel/platforms/os:android": ["android_cfi"],
  1042. "//conditions:default": [],
  1043. })`,
  1044. "local_includes": `["."]`,
  1045. }},
  1046. },
  1047. })
  1048. }
  1049. func TestCcBinaryWithCfiAndCfiAssemblySupport(t *testing.T) {
  1050. runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
  1051. description: "cc_binary has correct features when cfi is enabled with cfi assembly support",
  1052. blueprint: `
  1053. {rule_name} {
  1054. name: "foo",
  1055. sanitize: {
  1056. cfi: true,
  1057. config: {
  1058. cfi_assembly_support: true,
  1059. },
  1060. },
  1061. }`,
  1062. targets: []testBazelTarget{
  1063. {"cc_binary", "foo", AttrNameToString{
  1064. "features": `[
  1065. "android_cfi",
  1066. "android_cfi_assembly_support",
  1067. ]`,
  1068. "local_includes": `["."]`,
  1069. }},
  1070. },
  1071. })
  1072. }
  1073. func TestCcBinaryExplicitlyDisablesCfiWhenFalse(t *testing.T) {
  1074. runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
  1075. description: "cc_binary disables cfi when explciitly set to false in the bp",
  1076. blueprint: `
  1077. {rule_name} {
  1078. name: "foo",
  1079. sanitize: {
  1080. cfi: false,
  1081. },
  1082. }
  1083. `,
  1084. targets: []testBazelTarget{
  1085. {"cc_binary", "foo", AttrNameToString{
  1086. "features": `["-android_cfi"]`,
  1087. "local_includes": `["."]`,
  1088. }},
  1089. },
  1090. })
  1091. }
  1092. func TestCcBinaryStem(t *testing.T) {
  1093. runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
  1094. description: "cc_binary with stem property",
  1095. blueprint: `
  1096. cc_binary {
  1097. name: "foo_with_stem_simple",
  1098. stem: "foo",
  1099. }
  1100. cc_binary {
  1101. name: "foo_with_arch_variant_stem",
  1102. arch: {
  1103. arm: {
  1104. stem: "foo-arm",
  1105. },
  1106. arm64: {
  1107. stem: "foo-arm64",
  1108. },
  1109. },
  1110. }
  1111. `,
  1112. targets: []testBazelTarget{
  1113. {"cc_binary", "foo_with_stem_simple", AttrNameToString{
  1114. "stem": `"foo"`,
  1115. "local_includes": `["."]`,
  1116. }},
  1117. {"cc_binary", "foo_with_arch_variant_stem", AttrNameToString{
  1118. "stem": `select({
  1119. "//build/bazel/platforms/arch:arm": "foo-arm",
  1120. "//build/bazel/platforms/arch:arm64": "foo-arm64",
  1121. "//conditions:default": None,
  1122. })`,
  1123. "local_includes": `["."]`,
  1124. }},
  1125. },
  1126. })
  1127. }
  1128. func TestCCBinaryRscriptSrc(t *testing.T) {
  1129. runCcBinaryTests(t, ccBinaryBp2buildTestCase{
  1130. description: `cc_binary with rscript files in sources`,
  1131. blueprint: `
  1132. {rule_name} {
  1133. name : "foo",
  1134. srcs : [
  1135. "ccSrc.cc",
  1136. "rsSrc.rscript",
  1137. ],
  1138. include_build_directory: false,
  1139. }
  1140. `,
  1141. targets: []testBazelTarget{
  1142. {"rscript_to_cpp", "foo_renderscript", AttrNameToString{
  1143. "srcs": `["rsSrc.rscript"]`,
  1144. }},
  1145. {"cc_binary", "foo", AttrNameToString{
  1146. "absolute_includes": `[
  1147. "frameworks/rs",
  1148. "frameworks/rs/cpp",
  1149. ]`,
  1150. "local_includes": `["."]`,
  1151. "srcs": `[
  1152. "ccSrc.cc",
  1153. "foo_renderscript",
  1154. ]`,
  1155. }},
  1156. },
  1157. })
  1158. }