build_conversion_test.go 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948
  1. // Copyright 2020 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/android/allowlists"
  21. "android/soong/bazel"
  22. "android/soong/python"
  23. )
  24. func TestGenerateSoongModuleTargets(t *testing.T) {
  25. testCases := []struct {
  26. description string
  27. bp string
  28. expectedBazelTarget string
  29. }{
  30. {
  31. description: "only name",
  32. bp: `custom { name: "foo" }
  33. `,
  34. expectedBazelTarget: `soong_module(
  35. name = "foo",
  36. soong_module_name = "foo",
  37. soong_module_type = "custom",
  38. soong_module_variant = "",
  39. soong_module_deps = [
  40. ],
  41. bool_prop = False,
  42. string_prop = "",
  43. )`,
  44. },
  45. {
  46. description: "handles bool",
  47. bp: `custom {
  48. name: "foo",
  49. bool_prop: true,
  50. }
  51. `,
  52. expectedBazelTarget: `soong_module(
  53. name = "foo",
  54. soong_module_name = "foo",
  55. soong_module_type = "custom",
  56. soong_module_variant = "",
  57. soong_module_deps = [
  58. ],
  59. bool_prop = True,
  60. string_prop = "",
  61. )`,
  62. },
  63. {
  64. description: "string escaping",
  65. bp: `custom {
  66. name: "foo",
  67. owner: "a_string_with\"quotes\"_and_\\backslashes\\\\",
  68. }
  69. `,
  70. expectedBazelTarget: `soong_module(
  71. name = "foo",
  72. soong_module_name = "foo",
  73. soong_module_type = "custom",
  74. soong_module_variant = "",
  75. soong_module_deps = [
  76. ],
  77. bool_prop = False,
  78. owner = "a_string_with\"quotes\"_and_\\backslashes\\\\",
  79. string_prop = "",
  80. )`,
  81. },
  82. {
  83. description: "single item string list",
  84. bp: `custom {
  85. name: "foo",
  86. required: ["bar"],
  87. }
  88. `,
  89. expectedBazelTarget: `soong_module(
  90. name = "foo",
  91. soong_module_name = "foo",
  92. soong_module_type = "custom",
  93. soong_module_variant = "",
  94. soong_module_deps = [
  95. ],
  96. bool_prop = False,
  97. required = ["bar"],
  98. string_prop = "",
  99. )`,
  100. },
  101. {
  102. description: "list of strings",
  103. bp: `custom {
  104. name: "foo",
  105. target_required: ["qux", "bazqux"],
  106. }
  107. `,
  108. expectedBazelTarget: `soong_module(
  109. name = "foo",
  110. soong_module_name = "foo",
  111. soong_module_type = "custom",
  112. soong_module_variant = "",
  113. soong_module_deps = [
  114. ],
  115. bool_prop = False,
  116. string_prop = "",
  117. target_required = [
  118. "qux",
  119. "bazqux",
  120. ],
  121. )`,
  122. },
  123. {
  124. description: "dist/dists",
  125. bp: `custom {
  126. name: "foo",
  127. dist: {
  128. targets: ["goal_foo"],
  129. tag: ".foo",
  130. },
  131. dists: [{
  132. targets: ["goal_bar"],
  133. tag: ".bar",
  134. }],
  135. }
  136. `,
  137. expectedBazelTarget: `soong_module(
  138. name = "foo",
  139. soong_module_name = "foo",
  140. soong_module_type = "custom",
  141. soong_module_variant = "",
  142. soong_module_deps = [
  143. ],
  144. bool_prop = False,
  145. dist = {
  146. "tag": ".foo",
  147. "targets": ["goal_foo"],
  148. },
  149. dists = [{
  150. "tag": ".bar",
  151. "targets": ["goal_bar"],
  152. }],
  153. string_prop = "",
  154. )`,
  155. },
  156. {
  157. description: "put it together",
  158. bp: `custom {
  159. name: "foo",
  160. required: ["bar"],
  161. target_required: ["qux", "bazqux"],
  162. bool_prop: true,
  163. owner: "custom_owner",
  164. dists: [
  165. {
  166. tag: ".tag",
  167. targets: ["my_goal"],
  168. },
  169. ],
  170. }
  171. `,
  172. expectedBazelTarget: `soong_module(
  173. name = "foo",
  174. soong_module_name = "foo",
  175. soong_module_type = "custom",
  176. soong_module_variant = "",
  177. soong_module_deps = [
  178. ],
  179. bool_prop = True,
  180. dists = [{
  181. "tag": ".tag",
  182. "targets": ["my_goal"],
  183. }],
  184. owner = "custom_owner",
  185. required = ["bar"],
  186. string_prop = "",
  187. target_required = [
  188. "qux",
  189. "bazqux",
  190. ],
  191. )`,
  192. },
  193. }
  194. dir := "."
  195. for _, testCase := range testCases {
  196. t.Run(testCase.description, func(t *testing.T) {
  197. config := android.TestConfig(buildDir, nil, testCase.bp, nil)
  198. ctx := android.NewTestContext(config)
  199. ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
  200. ctx.Register()
  201. _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
  202. android.FailIfErrored(t, errs)
  203. _, errs = ctx.PrepareBuildActions(config)
  204. android.FailIfErrored(t, errs)
  205. codegenCtx := NewCodegenContext(config, ctx.Context, QueryView, "")
  206. bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
  207. android.FailIfErrored(t, err)
  208. if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount {
  209. t.Fatalf("Expected %d bazel target, got %d", expectedCount, actualCount)
  210. }
  211. actualBazelTarget := bazelTargets[0]
  212. if actualBazelTarget.content != testCase.expectedBazelTarget {
  213. t.Errorf(
  214. "Expected generated Bazel target to be '%s', got '%s'",
  215. testCase.expectedBazelTarget,
  216. actualBazelTarget.content,
  217. )
  218. }
  219. })
  220. }
  221. }
  222. func TestGenerateBazelTargetModules(t *testing.T) {
  223. testCases := []Bp2buildTestCase{
  224. {
  225. Description: "string prop empty",
  226. Blueprint: `custom {
  227. name: "foo",
  228. string_literal_prop: "",
  229. bazel_module: { bp2build_available: true },
  230. }`,
  231. ExpectedBazelTargets: []string{
  232. MakeBazelTarget("custom", "foo", AttrNameToString{
  233. "string_literal_prop": `""`,
  234. }),
  235. },
  236. },
  237. {
  238. Description: `string prop "PROP"`,
  239. Blueprint: `custom {
  240. name: "foo",
  241. string_literal_prop: "PROP",
  242. bazel_module: { bp2build_available: true },
  243. }`,
  244. ExpectedBazelTargets: []string{
  245. MakeBazelTarget("custom", "foo", AttrNameToString{
  246. "string_literal_prop": `"PROP"`,
  247. }),
  248. },
  249. },
  250. {
  251. Description: `string prop arch variant`,
  252. Blueprint: `custom {
  253. name: "foo",
  254. arch: {
  255. arm: { string_literal_prop: "ARM" },
  256. arm64: { string_literal_prop: "ARM64" },
  257. },
  258. bazel_module: { bp2build_available: true },
  259. }`,
  260. ExpectedBazelTargets: []string{
  261. MakeBazelTarget("custom", "foo", AttrNameToString{
  262. "string_literal_prop": `select({
  263. "//build/bazel/platforms/arch:arm": "ARM",
  264. "//build/bazel/platforms/arch:arm64": "ARM64",
  265. "//conditions:default": None,
  266. })`,
  267. }),
  268. },
  269. },
  270. {
  271. Description: "string ptr props",
  272. Blueprint: `custom {
  273. name: "foo",
  274. string_ptr_prop: "",
  275. bazel_module: { bp2build_available: true },
  276. }`,
  277. ExpectedBazelTargets: []string{
  278. MakeBazelTarget("custom", "foo", AttrNameToString{
  279. "string_ptr_prop": `""`,
  280. }),
  281. },
  282. },
  283. {
  284. Description: "string list props",
  285. Blueprint: `custom {
  286. name: "foo",
  287. string_list_prop: ["a", "b"],
  288. string_ptr_prop: "a",
  289. bazel_module: { bp2build_available: true },
  290. }`,
  291. ExpectedBazelTargets: []string{
  292. MakeBazelTarget("custom", "foo", AttrNameToString{
  293. "string_list_prop": `[
  294. "a",
  295. "b",
  296. ]`,
  297. "string_ptr_prop": `"a"`,
  298. }),
  299. },
  300. },
  301. {
  302. Description: "control characters",
  303. Blueprint: `custom {
  304. name: "foo",
  305. string_list_prop: ["\t", "\n"],
  306. string_ptr_prop: "a\t\n\r",
  307. bazel_module: { bp2build_available: true },
  308. }`,
  309. ExpectedBazelTargets: []string{
  310. MakeBazelTarget("custom", "foo", AttrNameToString{
  311. "string_list_prop": `[
  312. "\t",
  313. "\n",
  314. ]`,
  315. "string_ptr_prop": `"a\t\n\r"`,
  316. }),
  317. },
  318. },
  319. {
  320. Description: "handles dep",
  321. Blueprint: `custom {
  322. name: "has_dep",
  323. arch_paths: [":dep"],
  324. bazel_module: { bp2build_available: true },
  325. }
  326. custom {
  327. name: "dep",
  328. arch_paths: ["abc"],
  329. bazel_module: { bp2build_available: true },
  330. }`,
  331. ExpectedBazelTargets: []string{
  332. MakeBazelTarget("custom", "dep", AttrNameToString{
  333. "arch_paths": `["abc"]`,
  334. }),
  335. MakeBazelTarget("custom", "has_dep", AttrNameToString{
  336. "arch_paths": `[":dep"]`,
  337. }),
  338. },
  339. },
  340. {
  341. Description: "non-existent dep",
  342. Blueprint: `custom {
  343. name: "has_dep",
  344. arch_paths: [":dep"],
  345. bazel_module: { bp2build_available: true },
  346. }`,
  347. ExpectedBazelTargets: []string{
  348. MakeBazelTarget("custom", "has_dep", AttrNameToString{
  349. "arch_paths": `[":dep__BP2BUILD__MISSING__DEP"]`,
  350. }),
  351. },
  352. },
  353. {
  354. Description: "arch-variant srcs",
  355. Blueprint: `custom {
  356. name: "arch_paths",
  357. arch: {
  358. x86: { arch_paths: ["x86.txt"] },
  359. x86_64: { arch_paths: ["x86_64.txt"] },
  360. arm: { arch_paths: ["arm.txt"] },
  361. arm64: { arch_paths: ["arm64.txt"] },
  362. riscv64: { arch_paths: ["riscv64.txt"] },
  363. },
  364. target: {
  365. linux: { arch_paths: ["linux.txt"] },
  366. bionic: { arch_paths: ["bionic.txt"] },
  367. host: { arch_paths: ["host.txt"] },
  368. not_windows: { arch_paths: ["not_windows.txt"] },
  369. android: { arch_paths: ["android.txt"] },
  370. linux_musl: { arch_paths: ["linux_musl.txt"] },
  371. musl: { arch_paths: ["musl.txt"] },
  372. linux_glibc: { arch_paths: ["linux_glibc.txt"] },
  373. glibc: { arch_paths: ["glibc.txt"] },
  374. linux_bionic: { arch_paths: ["linux_bionic.txt"] },
  375. darwin: { arch_paths: ["darwin.txt"] },
  376. windows: { arch_paths: ["windows.txt"] },
  377. },
  378. multilib: {
  379. lib32: { arch_paths: ["lib32.txt"] },
  380. lib64: { arch_paths: ["lib64.txt"] },
  381. },
  382. bazel_module: { bp2build_available: true },
  383. }`,
  384. ExpectedBazelTargets: []string{
  385. MakeBazelTarget("custom", "arch_paths", AttrNameToString{
  386. "arch_paths": `select({
  387. "//build/bazel/platforms/arch:arm": [
  388. "arm.txt",
  389. "lib32.txt",
  390. ],
  391. "//build/bazel/platforms/arch:arm64": [
  392. "arm64.txt",
  393. "lib64.txt",
  394. ],
  395. "//build/bazel/platforms/arch:riscv64": [
  396. "riscv64.txt",
  397. "lib64.txt",
  398. ],
  399. "//build/bazel/platforms/arch:x86": [
  400. "x86.txt",
  401. "lib32.txt",
  402. ],
  403. "//build/bazel/platforms/arch:x86_64": [
  404. "x86_64.txt",
  405. "lib64.txt",
  406. ],
  407. "//conditions:default": [],
  408. }) + select({
  409. "//build/bazel/platforms/os:android": [
  410. "linux.txt",
  411. "bionic.txt",
  412. "android.txt",
  413. ],
  414. "//build/bazel/platforms/os:darwin": [
  415. "host.txt",
  416. "darwin.txt",
  417. "not_windows.txt",
  418. ],
  419. "//build/bazel/platforms/os:linux_bionic": [
  420. "host.txt",
  421. "linux.txt",
  422. "bionic.txt",
  423. "linux_bionic.txt",
  424. "not_windows.txt",
  425. ],
  426. "//build/bazel/platforms/os:linux_glibc": [
  427. "host.txt",
  428. "linux.txt",
  429. "glibc.txt",
  430. "linux_glibc.txt",
  431. "not_windows.txt",
  432. ],
  433. "//build/bazel/platforms/os:linux_musl": [
  434. "host.txt",
  435. "linux.txt",
  436. "musl.txt",
  437. "linux_musl.txt",
  438. "not_windows.txt",
  439. ],
  440. "//build/bazel/platforms/os:windows": [
  441. "host.txt",
  442. "windows.txt",
  443. ],
  444. "//conditions:default": [],
  445. })`,
  446. }),
  447. },
  448. },
  449. {
  450. Description: "arch-variant deps",
  451. Blueprint: `custom {
  452. name: "has_dep",
  453. arch: {
  454. x86: {
  455. arch_paths: [":dep"],
  456. },
  457. },
  458. bazel_module: { bp2build_available: true },
  459. }
  460. custom {
  461. name: "dep",
  462. arch_paths: ["abc"],
  463. bazel_module: { bp2build_available: true },
  464. }`,
  465. ExpectedBazelTargets: []string{
  466. MakeBazelTarget("custom", "dep", AttrNameToString{
  467. "arch_paths": `["abc"]`,
  468. }),
  469. MakeBazelTarget("custom", "has_dep", AttrNameToString{
  470. "arch_paths": `select({
  471. "//build/bazel/platforms/arch:x86": [":dep"],
  472. "//conditions:default": [],
  473. })`,
  474. }),
  475. },
  476. },
  477. {
  478. Description: "embedded props",
  479. Blueprint: `custom {
  480. name: "embedded_props",
  481. embedded_prop: "abc",
  482. bazel_module: { bp2build_available: true },
  483. }`,
  484. ExpectedBazelTargets: []string{
  485. MakeBazelTarget("custom", "embedded_props", AttrNameToString{
  486. "embedded_attr": `"abc"`,
  487. }),
  488. },
  489. },
  490. {
  491. Description: "ptr to embedded props",
  492. Blueprint: `custom {
  493. name: "ptr_to_embedded_props",
  494. other_embedded_prop: "abc",
  495. bazel_module: { bp2build_available: true },
  496. }`,
  497. ExpectedBazelTargets: []string{
  498. MakeBazelTarget("custom", "ptr_to_embedded_props", AttrNameToString{
  499. "other_embedded_attr": `"abc"`,
  500. }),
  501. },
  502. },
  503. }
  504. dir := "."
  505. for _, testCase := range testCases {
  506. t.Run(testCase.Description, func(t *testing.T) {
  507. config := android.TestConfig(buildDir, nil, testCase.Blueprint, nil)
  508. ctx := android.NewTestContext(config)
  509. registerCustomModuleForBp2buildConversion(ctx)
  510. _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
  511. if errored(t, testCase, errs) {
  512. return
  513. }
  514. _, errs = ctx.ResolveDependencies(config)
  515. if errored(t, testCase, errs) {
  516. return
  517. }
  518. codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "")
  519. bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
  520. android.FailIfErrored(t, err)
  521. if actualCount, expectedCount := len(bazelTargets), len(testCase.ExpectedBazelTargets); actualCount != expectedCount {
  522. t.Errorf("Expected %d bazel target (%s),\ngot %d (%s)", expectedCount, testCase.ExpectedBazelTargets, actualCount, bazelTargets)
  523. } else {
  524. for i, expectedBazelTarget := range testCase.ExpectedBazelTargets {
  525. actualBazelTarget := bazelTargets[i]
  526. if actualBazelTarget.content != expectedBazelTarget {
  527. t.Errorf(
  528. "Expected generated Bazel target to be '%s', got '%s'",
  529. expectedBazelTarget,
  530. actualBazelTarget.content,
  531. )
  532. }
  533. }
  534. }
  535. })
  536. }
  537. }
  538. func TestBp2buildHostAndDevice(t *testing.T) {
  539. testCases := []Bp2buildTestCase{
  540. {
  541. Description: "host and device, device only",
  542. ModuleTypeUnderTest: "custom",
  543. ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
  544. Blueprint: `custom {
  545. name: "foo",
  546. bazel_module: { bp2build_available: true },
  547. }`,
  548. ExpectedBazelTargets: []string{
  549. makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported),
  550. },
  551. },
  552. {
  553. Description: "host and device, both",
  554. ModuleTypeUnderTest: "custom",
  555. ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
  556. Blueprint: `custom {
  557. name: "foo",
  558. host_supported: true,
  559. bazel_module: { bp2build_available: true },
  560. }`,
  561. ExpectedBazelTargets: []string{
  562. MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{}),
  563. },
  564. },
  565. {
  566. Description: "host and device, host explicitly disabled",
  567. ModuleTypeUnderTest: "custom",
  568. ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
  569. Blueprint: `custom {
  570. name: "foo",
  571. host_supported: false,
  572. bazel_module: { bp2build_available: true },
  573. }`,
  574. ExpectedBazelTargets: []string{
  575. makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported),
  576. },
  577. },
  578. {
  579. Description: "host and device, neither",
  580. ModuleTypeUnderTest: "custom",
  581. ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
  582. Blueprint: `custom {
  583. name: "foo",
  584. host_supported: false,
  585. device_supported: false,
  586. bazel_module: { bp2build_available: true },
  587. }`,
  588. ExpectedBazelTargets: []string{
  589. MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
  590. "target_compatible_with": `["@platforms//:incompatible"]`,
  591. }),
  592. },
  593. },
  594. {
  595. Description: "host and device, neither, cannot override with product_var",
  596. ModuleTypeUnderTest: "custom",
  597. ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
  598. Blueprint: `custom {
  599. name: "foo",
  600. host_supported: false,
  601. device_supported: false,
  602. product_variables: { unbundled_build: { enabled: true } },
  603. bazel_module: { bp2build_available: true },
  604. }`,
  605. ExpectedBazelTargets: []string{
  606. MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
  607. "target_compatible_with": `["@platforms//:incompatible"]`,
  608. }),
  609. },
  610. },
  611. {
  612. Description: "host and device, both, disabled overrided with product_var",
  613. ModuleTypeUnderTest: "custom",
  614. ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
  615. Blueprint: `custom {
  616. name: "foo",
  617. host_supported: true,
  618. device_supported: true,
  619. enabled: false,
  620. product_variables: { unbundled_build: { enabled: true } },
  621. bazel_module: { bp2build_available: true },
  622. }`,
  623. ExpectedBazelTargets: []string{
  624. MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
  625. "target_compatible_with": `["//build/bazel/product_variables:unbundled_build"]`,
  626. }),
  627. },
  628. },
  629. {
  630. Description: "host and device, neither, cannot override with arch enabled",
  631. ModuleTypeUnderTest: "custom",
  632. ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
  633. Blueprint: `custom {
  634. name: "foo",
  635. host_supported: false,
  636. device_supported: false,
  637. arch: { x86: { enabled: true } },
  638. bazel_module: { bp2build_available: true },
  639. }`,
  640. ExpectedBazelTargets: []string{
  641. MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
  642. "target_compatible_with": `["@platforms//:incompatible"]`,
  643. }),
  644. },
  645. },
  646. {
  647. Description: "host and device, host only",
  648. ModuleTypeUnderTest: "custom",
  649. ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
  650. Blueprint: `custom {
  651. name: "foo",
  652. host_supported: true,
  653. device_supported: false,
  654. bazel_module: { bp2build_available: true },
  655. }`,
  656. ExpectedBazelTargets: []string{
  657. makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported),
  658. },
  659. },
  660. {
  661. Description: "host only",
  662. ModuleTypeUnderTest: "custom",
  663. ModuleTypeUnderTestFactory: customModuleFactoryHostSupported,
  664. Blueprint: `custom {
  665. name: "foo",
  666. bazel_module: { bp2build_available: true },
  667. }`,
  668. ExpectedBazelTargets: []string{
  669. makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported),
  670. },
  671. },
  672. {
  673. Description: "device only",
  674. ModuleTypeUnderTest: "custom",
  675. ModuleTypeUnderTestFactory: customModuleFactoryDeviceSupported,
  676. Blueprint: `custom {
  677. name: "foo",
  678. bazel_module: { bp2build_available: true },
  679. }`,
  680. ExpectedBazelTargets: []string{
  681. makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported),
  682. },
  683. },
  684. {
  685. Description: "host and device default, default",
  686. ModuleTypeUnderTest: "custom",
  687. ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault,
  688. Blueprint: `custom {
  689. name: "foo",
  690. bazel_module: { bp2build_available: true },
  691. }`,
  692. ExpectedBazelTargets: []string{
  693. MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{}),
  694. },
  695. },
  696. {
  697. Description: "host and device default, device only",
  698. ModuleTypeUnderTest: "custom",
  699. ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault,
  700. Blueprint: `custom {
  701. name: "foo",
  702. host_supported: false,
  703. bazel_module: { bp2build_available: true },
  704. }`,
  705. ExpectedBazelTargets: []string{
  706. makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported),
  707. },
  708. },
  709. {
  710. Description: "host and device default, host only",
  711. ModuleTypeUnderTest: "custom",
  712. ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault,
  713. Blueprint: `custom {
  714. name: "foo",
  715. device_supported: false,
  716. bazel_module: { bp2build_available: true },
  717. }`,
  718. ExpectedBazelTargets: []string{
  719. makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported),
  720. },
  721. },
  722. {
  723. Description: "host and device default, neither",
  724. ModuleTypeUnderTest: "custom",
  725. ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault,
  726. Blueprint: `custom {
  727. name: "foo",
  728. host_supported: false,
  729. device_supported: false,
  730. bazel_module: { bp2build_available: true },
  731. }`,
  732. ExpectedBazelTargets: []string{
  733. MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
  734. "target_compatible_with": `["@platforms//:incompatible"]`,
  735. }),
  736. },
  737. },
  738. }
  739. for _, tc := range testCases {
  740. t.Run(tc.Description, func(t *testing.T) {
  741. RunBp2BuildTestCaseSimple(t, tc)
  742. })
  743. }
  744. }
  745. func TestLoadStatements(t *testing.T) {
  746. testCases := []struct {
  747. bazelTargets BazelTargets
  748. expectedLoadStatements string
  749. }{
  750. {
  751. bazelTargets: BazelTargets{
  752. BazelTarget{
  753. name: "foo",
  754. ruleClass: "cc_library",
  755. bzlLoadLocation: "//build/bazel/rules:cc.bzl",
  756. },
  757. },
  758. expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
  759. },
  760. {
  761. bazelTargets: BazelTargets{
  762. BazelTarget{
  763. name: "foo",
  764. ruleClass: "cc_library",
  765. bzlLoadLocation: "//build/bazel/rules:cc.bzl",
  766. },
  767. BazelTarget{
  768. name: "bar",
  769. ruleClass: "cc_library",
  770. bzlLoadLocation: "//build/bazel/rules:cc.bzl",
  771. },
  772. },
  773. expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
  774. },
  775. {
  776. bazelTargets: BazelTargets{
  777. BazelTarget{
  778. name: "foo",
  779. ruleClass: "cc_library",
  780. bzlLoadLocation: "//build/bazel/rules:cc.bzl",
  781. },
  782. BazelTarget{
  783. name: "bar",
  784. ruleClass: "cc_binary",
  785. bzlLoadLocation: "//build/bazel/rules:cc.bzl",
  786. },
  787. },
  788. expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`,
  789. },
  790. {
  791. bazelTargets: BazelTargets{
  792. BazelTarget{
  793. name: "foo",
  794. ruleClass: "cc_library",
  795. bzlLoadLocation: "//build/bazel/rules:cc.bzl",
  796. },
  797. BazelTarget{
  798. name: "bar",
  799. ruleClass: "cc_binary",
  800. bzlLoadLocation: "//build/bazel/rules:cc.bzl",
  801. },
  802. BazelTarget{
  803. name: "baz",
  804. ruleClass: "java_binary",
  805. bzlLoadLocation: "//build/bazel/rules:java.bzl",
  806. },
  807. },
  808. expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")
  809. load("//build/bazel/rules:java.bzl", "java_binary")`,
  810. },
  811. {
  812. bazelTargets: BazelTargets{
  813. BazelTarget{
  814. name: "foo",
  815. ruleClass: "cc_binary",
  816. bzlLoadLocation: "//build/bazel/rules:cc.bzl",
  817. },
  818. BazelTarget{
  819. name: "bar",
  820. ruleClass: "java_binary",
  821. bzlLoadLocation: "//build/bazel/rules:java.bzl",
  822. },
  823. BazelTarget{
  824. name: "baz",
  825. ruleClass: "genrule",
  826. // Note: no bzlLoadLocation for native rules
  827. },
  828. },
  829. expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary")
  830. load("//build/bazel/rules:java.bzl", "java_binary")`,
  831. },
  832. }
  833. for _, testCase := range testCases {
  834. actual := testCase.bazelTargets.LoadStatements()
  835. expected := testCase.expectedLoadStatements
  836. if actual != expected {
  837. t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
  838. }
  839. }
  840. }
  841. func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) {
  842. testCases := []struct {
  843. bp string
  844. expectedBazelTarget string
  845. expectedBazelTargetCount int
  846. expectedLoadStatements string
  847. }{
  848. {
  849. bp: `custom {
  850. name: "bar",
  851. host_supported: true,
  852. one_to_many_prop: true,
  853. bazel_module: { bp2build_available: true },
  854. }`,
  855. expectedBazelTarget: `my_library(
  856. name = "bar",
  857. )
  858. proto_library(
  859. name = "bar_proto_library_deps",
  860. )
  861. my_proto_library(
  862. name = "bar_my_proto_library_deps",
  863. )`,
  864. expectedBazelTargetCount: 3,
  865. expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library")
  866. load("//build/bazel/rules:rules.bzl", "my_library")`,
  867. },
  868. }
  869. dir := "."
  870. for _, testCase := range testCases {
  871. config := android.TestConfig(buildDir, nil, testCase.bp, nil)
  872. ctx := android.NewTestContext(config)
  873. ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
  874. ctx.RegisterForBazelConversion()
  875. _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
  876. android.FailIfErrored(t, errs)
  877. _, errs = ctx.ResolveDependencies(config)
  878. android.FailIfErrored(t, errs)
  879. codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "")
  880. bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
  881. android.FailIfErrored(t, err)
  882. if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount {
  883. t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount)
  884. }
  885. actualBazelTargets := bazelTargets.String()
  886. if actualBazelTargets != testCase.expectedBazelTarget {
  887. t.Errorf(
  888. "Expected generated Bazel target to be '%s', got '%s'",
  889. testCase.expectedBazelTarget,
  890. actualBazelTargets,
  891. )
  892. }
  893. actualLoadStatements := bazelTargets.LoadStatements()
  894. if actualLoadStatements != testCase.expectedLoadStatements {
  895. t.Errorf(
  896. "Expected generated load statements to be '%s', got '%s'",
  897. testCase.expectedLoadStatements,
  898. actualLoadStatements,
  899. )
  900. }
  901. }
  902. }
  903. func TestModuleTypeBp2Build(t *testing.T) {
  904. testCases := []Bp2buildTestCase{
  905. {
  906. Description: "filegroup with does not specify srcs",
  907. ModuleTypeUnderTest: "filegroup",
  908. ModuleTypeUnderTestFactory: android.FileGroupFactory,
  909. Blueprint: `filegroup {
  910. name: "fg_foo",
  911. bazel_module: { bp2build_available: true },
  912. }`,
  913. ExpectedBazelTargets: []string{
  914. MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}),
  915. },
  916. },
  917. {
  918. Description: "filegroup with no srcs",
  919. ModuleTypeUnderTest: "filegroup",
  920. ModuleTypeUnderTestFactory: android.FileGroupFactory,
  921. Blueprint: `filegroup {
  922. name: "fg_foo",
  923. srcs: [],
  924. bazel_module: { bp2build_available: true },
  925. }`,
  926. ExpectedBazelTargets: []string{
  927. MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}),
  928. },
  929. },
  930. {
  931. Description: "filegroup with srcs",
  932. ModuleTypeUnderTest: "filegroup",
  933. ModuleTypeUnderTestFactory: android.FileGroupFactory,
  934. Blueprint: `filegroup {
  935. name: "fg_foo",
  936. srcs: ["a", "b"],
  937. bazel_module: { bp2build_available: true },
  938. }`,
  939. ExpectedBazelTargets: []string{
  940. MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
  941. "srcs": `[
  942. "a",
  943. "b",
  944. ]`,
  945. }),
  946. },
  947. },
  948. {
  949. Description: "filegroup with dot-slash-prefixed srcs",
  950. ModuleTypeUnderTest: "filegroup",
  951. ModuleTypeUnderTestFactory: android.FileGroupFactory,
  952. Blueprint: `filegroup {
  953. name: "fg_foo",
  954. srcs: ["./a", "./b"],
  955. bazel_module: { bp2build_available: true },
  956. }`,
  957. ExpectedBazelTargets: []string{
  958. MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
  959. "srcs": `[
  960. "a",
  961. "b",
  962. ]`,
  963. }),
  964. },
  965. },
  966. {
  967. Description: "filegroup with excludes srcs",
  968. ModuleTypeUnderTest: "filegroup",
  969. ModuleTypeUnderTestFactory: android.FileGroupFactory,
  970. Blueprint: `filegroup {
  971. name: "fg_foo",
  972. srcs: ["a", "b"],
  973. exclude_srcs: ["a"],
  974. bazel_module: { bp2build_available: true },
  975. }`,
  976. ExpectedBazelTargets: []string{
  977. MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
  978. "srcs": `["b"]`,
  979. }),
  980. },
  981. },
  982. {
  983. Description: "depends_on_other_dir_module",
  984. ModuleTypeUnderTest: "filegroup",
  985. ModuleTypeUnderTestFactory: android.FileGroupFactory,
  986. Blueprint: `filegroup {
  987. name: "fg_foo",
  988. srcs: [
  989. ":foo",
  990. "c",
  991. ],
  992. bazel_module: { bp2build_available: true },
  993. }`,
  994. Filesystem: map[string]string{
  995. "other/Android.bp": `filegroup {
  996. name: "foo",
  997. srcs: ["a", "b"],
  998. bazel_module: { bp2build_available: true },
  999. }`,
  1000. },
  1001. ExpectedBazelTargets: []string{
  1002. MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
  1003. "srcs": `[
  1004. "//other:foo",
  1005. "c",
  1006. ]`,
  1007. }),
  1008. },
  1009. },
  1010. {
  1011. Description: "depends_on_other_unconverted_module_error",
  1012. ModuleTypeUnderTest: "filegroup",
  1013. ModuleTypeUnderTestFactory: android.FileGroupFactory,
  1014. UnconvertedDepsMode: errorModulesUnconvertedDeps,
  1015. Blueprint: `filegroup {
  1016. name: "foobar",
  1017. srcs: [
  1018. ":foo",
  1019. "c",
  1020. ],
  1021. bazel_module: { bp2build_available: true },
  1022. }`,
  1023. ExpectedErr: fmt.Errorf(`filegroup .:foobar depends on unconverted modules: foo`),
  1024. Filesystem: map[string]string{
  1025. "other/Android.bp": `filegroup {
  1026. name: "foo",
  1027. srcs: ["a", "b"],
  1028. }`,
  1029. },
  1030. },
  1031. {
  1032. Description: "depends_on_other_missing_module_error",
  1033. ModuleTypeUnderTest: "filegroup",
  1034. ModuleTypeUnderTestFactory: android.FileGroupFactory,
  1035. UnconvertedDepsMode: errorModulesUnconvertedDeps,
  1036. Blueprint: `filegroup {
  1037. name: "foobar",
  1038. srcs: [
  1039. "c",
  1040. "//other:foo",
  1041. "//other:goo",
  1042. ],
  1043. bazel_module: { bp2build_available: true },
  1044. }`,
  1045. ExpectedErr: fmt.Errorf(`filegroup .:foobar depends on missing modules: //other:goo`),
  1046. Filesystem: map[string]string{"other/Android.bp": `filegroup {
  1047. name: "foo",
  1048. srcs: ["a"],
  1049. bazel_module: { bp2build_available: true },
  1050. }
  1051. `,
  1052. },
  1053. },
  1054. }
  1055. for _, testCase := range testCases {
  1056. t.Run(testCase.Description, func(t *testing.T) {
  1057. RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, testCase)
  1058. })
  1059. }
  1060. }
  1061. func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) {
  1062. testCases := []struct {
  1063. moduleTypeUnderTest string
  1064. moduleTypeUnderTestFactory android.ModuleFactory
  1065. bp string
  1066. expectedCount int
  1067. description string
  1068. }{
  1069. {
  1070. description: "explicitly unavailable",
  1071. moduleTypeUnderTest: "filegroup",
  1072. moduleTypeUnderTestFactory: android.FileGroupFactory,
  1073. bp: `filegroup {
  1074. name: "foo",
  1075. srcs: ["a", "b"],
  1076. bazel_module: { bp2build_available: false },
  1077. }`,
  1078. expectedCount: 0,
  1079. },
  1080. {
  1081. description: "implicitly unavailable",
  1082. moduleTypeUnderTest: "filegroup",
  1083. moduleTypeUnderTestFactory: android.FileGroupFactory,
  1084. bp: `filegroup {
  1085. name: "foo",
  1086. srcs: ["a", "b"],
  1087. }`,
  1088. expectedCount: 0,
  1089. },
  1090. {
  1091. description: "explicitly available",
  1092. moduleTypeUnderTest: "filegroup",
  1093. moduleTypeUnderTestFactory: android.FileGroupFactory,
  1094. bp: `filegroup {
  1095. name: "foo",
  1096. srcs: ["a", "b"],
  1097. bazel_module: { bp2build_available: true },
  1098. }`,
  1099. expectedCount: 1,
  1100. },
  1101. {
  1102. description: "generates more than 1 target if needed",
  1103. moduleTypeUnderTest: "custom",
  1104. moduleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
  1105. bp: `custom {
  1106. name: "foo",
  1107. one_to_many_prop: true,
  1108. bazel_module: { bp2build_available: true },
  1109. }`,
  1110. expectedCount: 3,
  1111. },
  1112. }
  1113. dir := "."
  1114. for _, testCase := range testCases {
  1115. t.Run(testCase.description, func(t *testing.T) {
  1116. config := android.TestConfig(buildDir, nil, testCase.bp, nil)
  1117. ctx := android.NewTestContext(config)
  1118. ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
  1119. ctx.RegisterForBazelConversion()
  1120. _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
  1121. android.FailIfErrored(t, errs)
  1122. _, errs = ctx.ResolveDependencies(config)
  1123. android.FailIfErrored(t, errs)
  1124. codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "")
  1125. bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
  1126. android.FailIfErrored(t, err)
  1127. if actualCount := len(bazelTargets); actualCount != testCase.expectedCount {
  1128. t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount)
  1129. }
  1130. })
  1131. }
  1132. }
  1133. func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) {
  1134. testCases := []struct {
  1135. moduleTypeUnderTest string
  1136. moduleTypeUnderTestFactory android.ModuleFactory
  1137. expectedCount map[string]int
  1138. description string
  1139. bp2buildConfig allowlists.Bp2BuildConfig
  1140. checkDir string
  1141. fs map[string]string
  1142. forceEnabledModules []string
  1143. expectedErrorMessages []string
  1144. }{
  1145. {
  1146. description: "test bp2build config package and subpackages config",
  1147. moduleTypeUnderTest: "filegroup",
  1148. moduleTypeUnderTestFactory: android.FileGroupFactory,
  1149. expectedCount: map[string]int{
  1150. "migrated": 1,
  1151. "migrated/but_not_really": 0,
  1152. "migrated/but_not_really/but_really": 1,
  1153. "not_migrated": 0,
  1154. "also_not_migrated": 0,
  1155. },
  1156. bp2buildConfig: allowlists.Bp2BuildConfig{
  1157. "migrated": allowlists.Bp2BuildDefaultTrueRecursively,
  1158. "migrated/but_not_really": allowlists.Bp2BuildDefaultFalse,
  1159. "not_migrated": allowlists.Bp2BuildDefaultFalse,
  1160. },
  1161. fs: map[string]string{
  1162. "migrated/Android.bp": `filegroup { name: "a" }`,
  1163. "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`,
  1164. "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`,
  1165. "not_migrated/Android.bp": `filegroup { name: "d" }`,
  1166. "also_not_migrated/Android.bp": `filegroup { name: "e" }`,
  1167. },
  1168. },
  1169. {
  1170. description: "test bp2build config opt-in and opt-out",
  1171. moduleTypeUnderTest: "filegroup",
  1172. moduleTypeUnderTestFactory: android.FileGroupFactory,
  1173. expectedCount: map[string]int{
  1174. "package-opt-in": 2,
  1175. "package-opt-in/subpackage": 0,
  1176. "package-opt-out": 1,
  1177. "package-opt-out/subpackage": 0,
  1178. },
  1179. bp2buildConfig: allowlists.Bp2BuildConfig{
  1180. "package-opt-in": allowlists.Bp2BuildDefaultFalse,
  1181. "package-opt-out": allowlists.Bp2BuildDefaultTrueRecursively,
  1182. },
  1183. fs: map[string]string{
  1184. "package-opt-in/Android.bp": `
  1185. filegroup { name: "opt-in-a" }
  1186. filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } }
  1187. filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } }
  1188. `,
  1189. "package-opt-in/subpackage/Android.bp": `
  1190. filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively
  1191. `,
  1192. "package-opt-out/Android.bp": `
  1193. filegroup { name: "opt-out-a" }
  1194. filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } }
  1195. filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } }
  1196. `,
  1197. "package-opt-out/subpackage/Android.bp": `
  1198. filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } }
  1199. filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } }
  1200. `,
  1201. },
  1202. },
  1203. {
  1204. description: "test force-enabled errors out",
  1205. moduleTypeUnderTest: "filegroup",
  1206. moduleTypeUnderTestFactory: android.FileGroupFactory,
  1207. expectedCount: map[string]int{
  1208. "migrated": 0,
  1209. "not_migrated": 0,
  1210. },
  1211. bp2buildConfig: allowlists.Bp2BuildConfig{
  1212. "migrated/but_not_really": allowlists.Bp2BuildDefaultFalse,
  1213. "not_migrated": allowlists.Bp2BuildDefaultFalse,
  1214. },
  1215. fs: map[string]string{
  1216. "migrated/Android.bp": `filegroup { name: "a" }`,
  1217. },
  1218. forceEnabledModules: []string{"a"},
  1219. expectedErrorMessages: []string{"Force Enabled Module a not converted"},
  1220. },
  1221. }
  1222. dir := "."
  1223. for _, testCase := range testCases {
  1224. fs := make(map[string][]byte)
  1225. toParse := []string{
  1226. "Android.bp",
  1227. }
  1228. for f, content := range testCase.fs {
  1229. if strings.HasSuffix(f, "Android.bp") {
  1230. toParse = append(toParse, f)
  1231. }
  1232. fs[f] = []byte(content)
  1233. }
  1234. config := android.TestConfig(buildDir, nil, "", fs)
  1235. config.AddForceEnabledModules(testCase.forceEnabledModules)
  1236. ctx := android.NewTestContext(config)
  1237. ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
  1238. allowlist := android.NewBp2BuildAllowlist().SetDefaultConfig(testCase.bp2buildConfig)
  1239. ctx.RegisterBp2BuildConfig(allowlist)
  1240. ctx.RegisterForBazelConversion()
  1241. _, errs := ctx.ParseFileList(dir, toParse)
  1242. android.FailIfErrored(t, errs)
  1243. _, errs = ctx.ResolveDependencies(config)
  1244. android.FailIfErrored(t, errs)
  1245. codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "")
  1246. // For each directory, test that the expected number of generated targets is correct.
  1247. for dir, expectedCount := range testCase.expectedCount {
  1248. bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
  1249. android.CheckErrorsAgainstExpectations(t, err, testCase.expectedErrorMessages)
  1250. if actualCount := len(bazelTargets); actualCount != expectedCount {
  1251. t.Fatalf(
  1252. "%s: Expected %d bazel target for %s package, got %d",
  1253. testCase.description,
  1254. expectedCount,
  1255. dir,
  1256. actualCount)
  1257. }
  1258. }
  1259. }
  1260. }
  1261. func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
  1262. testCases := []Bp2buildTestCase{
  1263. {
  1264. Description: "filegroup bazel_module.label",
  1265. ModuleTypeUnderTest: "filegroup",
  1266. ModuleTypeUnderTestFactory: android.FileGroupFactory,
  1267. Blueprint: `filegroup {
  1268. name: "fg_foo",
  1269. bazel_module: { label: "//other:fg_foo" },
  1270. }`,
  1271. ExpectedBazelTargets: []string{},
  1272. Filesystem: map[string]string{
  1273. "other/BUILD.bazel": `// BUILD file`,
  1274. },
  1275. },
  1276. {
  1277. Description: "multiple bazel_module.label same BUILD",
  1278. ModuleTypeUnderTest: "filegroup",
  1279. ModuleTypeUnderTestFactory: android.FileGroupFactory,
  1280. Blueprint: `filegroup {
  1281. name: "fg_foo",
  1282. bazel_module: { label: "//other:fg_foo" },
  1283. }
  1284. filegroup {
  1285. name: "foo",
  1286. bazel_module: { label: "//other:foo" },
  1287. }`,
  1288. ExpectedBazelTargets: []string{},
  1289. Filesystem: map[string]string{
  1290. "other/BUILD.bazel": `// BUILD file`,
  1291. },
  1292. },
  1293. {
  1294. Description: "filegroup bazel_module.label and bp2build in subdir",
  1295. ModuleTypeUnderTest: "filegroup",
  1296. ModuleTypeUnderTestFactory: android.FileGroupFactory,
  1297. Dir: "other",
  1298. Blueprint: ``,
  1299. Filesystem: map[string]string{
  1300. "other/Android.bp": `filegroup {
  1301. name: "fg_foo",
  1302. bazel_module: {
  1303. bp2build_available: true,
  1304. },
  1305. }
  1306. filegroup {
  1307. name: "fg_bar",
  1308. bazel_module: {
  1309. label: "//other:fg_bar"
  1310. },
  1311. }`,
  1312. "other/BUILD.bazel": `// definition for fg_bar`,
  1313. },
  1314. ExpectedBazelTargets: []string{
  1315. MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}),
  1316. },
  1317. },
  1318. {
  1319. Description: "filegroup bazel_module.label and filegroup bp2build",
  1320. ModuleTypeUnderTest: "filegroup",
  1321. ModuleTypeUnderTestFactory: android.FileGroupFactory,
  1322. Filesystem: map[string]string{
  1323. "other/BUILD.bazel": `// BUILD file`,
  1324. },
  1325. Blueprint: `filegroup {
  1326. name: "fg_foo",
  1327. bazel_module: {
  1328. label: "//other:fg_foo",
  1329. },
  1330. }
  1331. filegroup {
  1332. name: "fg_bar",
  1333. bazel_module: {
  1334. bp2build_available: true,
  1335. },
  1336. }`,
  1337. ExpectedBazelTargets: []string{
  1338. MakeBazelTargetNoRestrictions("filegroup", "fg_bar", map[string]string{}),
  1339. },
  1340. },
  1341. }
  1342. dir := "."
  1343. for _, testCase := range testCases {
  1344. t.Run(testCase.Description, func(t *testing.T) {
  1345. fs := make(map[string][]byte)
  1346. toParse := []string{
  1347. "Android.bp",
  1348. }
  1349. for f, content := range testCase.Filesystem {
  1350. if strings.HasSuffix(f, "Android.bp") {
  1351. toParse = append(toParse, f)
  1352. }
  1353. fs[f] = []byte(content)
  1354. }
  1355. config := android.TestConfig(buildDir, nil, testCase.Blueprint, fs)
  1356. ctx := android.NewTestContext(config)
  1357. ctx.RegisterModuleType(testCase.ModuleTypeUnderTest, testCase.ModuleTypeUnderTestFactory)
  1358. ctx.RegisterForBazelConversion()
  1359. _, errs := ctx.ParseFileList(dir, toParse)
  1360. if errored(t, testCase, errs) {
  1361. return
  1362. }
  1363. _, errs = ctx.ResolveDependencies(config)
  1364. if errored(t, testCase, errs) {
  1365. return
  1366. }
  1367. checkDir := dir
  1368. if testCase.Dir != "" {
  1369. checkDir = testCase.Dir
  1370. }
  1371. codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "")
  1372. bazelTargets, err := generateBazelTargetsForDir(codegenCtx, checkDir)
  1373. android.FailIfErrored(t, err)
  1374. bazelTargets.sort()
  1375. actualCount := len(bazelTargets)
  1376. expectedCount := len(testCase.ExpectedBazelTargets)
  1377. if actualCount != expectedCount {
  1378. t.Errorf("Expected %d bazel target, got %d\n%s", expectedCount, actualCount, bazelTargets)
  1379. }
  1380. for i, target := range bazelTargets {
  1381. actualContent := target.content
  1382. expectedContent := testCase.ExpectedBazelTargets[i]
  1383. if expectedContent != actualContent {
  1384. t.Errorf(
  1385. "Expected generated Bazel target to be '%s', got '%s'",
  1386. expectedContent,
  1387. actualContent,
  1388. )
  1389. }
  1390. }
  1391. })
  1392. }
  1393. }
  1394. func TestGlob(t *testing.T) {
  1395. testCases := []Bp2buildTestCase{
  1396. {
  1397. Description: "filegroup with glob",
  1398. ModuleTypeUnderTest: "filegroup",
  1399. ModuleTypeUnderTestFactory: android.FileGroupFactory,
  1400. Blueprint: `filegroup {
  1401. name: "fg_foo",
  1402. srcs: ["**/*.txt"],
  1403. bazel_module: { bp2build_available: true },
  1404. }`,
  1405. ExpectedBazelTargets: []string{
  1406. MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
  1407. "srcs": `[
  1408. "other/a.txt",
  1409. "other/b.txt",
  1410. "other/subdir/a.txt",
  1411. ]`,
  1412. }),
  1413. },
  1414. Filesystem: map[string]string{
  1415. "other/a.txt": "",
  1416. "other/b.txt": "",
  1417. "other/subdir/a.txt": "",
  1418. "other/file": "",
  1419. },
  1420. },
  1421. {
  1422. Description: "filegroup with glob in subdir",
  1423. ModuleTypeUnderTest: "filegroup",
  1424. ModuleTypeUnderTestFactory: android.FileGroupFactory,
  1425. Dir: "other",
  1426. Filesystem: map[string]string{
  1427. "other/Android.bp": `filegroup {
  1428. name: "fg_foo",
  1429. srcs: ["**/*.txt"],
  1430. bazel_module: { bp2build_available: true },
  1431. }`,
  1432. "other/a.txt": "",
  1433. "other/b.txt": "",
  1434. "other/subdir/a.txt": "",
  1435. "other/file": "",
  1436. },
  1437. ExpectedBazelTargets: []string{
  1438. MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
  1439. "srcs": `[
  1440. "a.txt",
  1441. "b.txt",
  1442. "subdir/a.txt",
  1443. ]`,
  1444. }),
  1445. },
  1446. },
  1447. {
  1448. Description: "filegroup with glob with no kept BUILD files",
  1449. ModuleTypeUnderTest: "filegroup",
  1450. ModuleTypeUnderTestFactory: android.FileGroupFactory,
  1451. KeepBuildFileForDirs: []string{
  1452. // empty
  1453. },
  1454. Blueprint: `filegroup {
  1455. name: "fg_foo",
  1456. srcs: ["**/*.txt"],
  1457. bazel_module: { bp2build_available: true },
  1458. }`,
  1459. Filesystem: map[string]string{
  1460. "a.txt": "",
  1461. "b.txt": "",
  1462. "foo/BUILD": "",
  1463. "foo/a.txt": "",
  1464. "foo/bar/BUILD": "",
  1465. "foo/bar/b.txt": "",
  1466. },
  1467. ExpectedBazelTargets: []string{
  1468. MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
  1469. "srcs": `[
  1470. "a.txt",
  1471. "b.txt",
  1472. "foo/a.txt",
  1473. "foo/bar/b.txt",
  1474. ]`,
  1475. }),
  1476. },
  1477. },
  1478. {
  1479. Description: "filegroup with glob with kept BUILD file",
  1480. ModuleTypeUnderTest: "filegroup",
  1481. ModuleTypeUnderTestFactory: android.FileGroupFactory,
  1482. KeepBuildFileForDirs: []string{
  1483. "foo",
  1484. },
  1485. Blueprint: `filegroup {
  1486. name: "fg_foo",
  1487. srcs: ["**/*.txt"],
  1488. bazel_module: { bp2build_available: true },
  1489. }`,
  1490. Filesystem: map[string]string{
  1491. "a.txt": "",
  1492. "b.txt": "",
  1493. "foo/BUILD": "",
  1494. "foo/a.txt": "",
  1495. "foo/bar/BUILD": "",
  1496. "foo/bar/b.txt": "",
  1497. },
  1498. ExpectedBazelTargets: []string{
  1499. MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
  1500. "srcs": `[
  1501. "a.txt",
  1502. "b.txt",
  1503. "//foo:a.txt",
  1504. "//foo:bar/b.txt",
  1505. ]`,
  1506. }),
  1507. },
  1508. },
  1509. {
  1510. Description: "filegroup with glob with kept BUILD.bazel file",
  1511. ModuleTypeUnderTest: "filegroup",
  1512. ModuleTypeUnderTestFactory: android.FileGroupFactory,
  1513. KeepBuildFileForDirs: []string{
  1514. "foo",
  1515. },
  1516. Blueprint: `filegroup {
  1517. name: "fg_foo",
  1518. srcs: ["**/*.txt"],
  1519. bazel_module: { bp2build_available: true },
  1520. }`,
  1521. Filesystem: map[string]string{
  1522. "a.txt": "",
  1523. "b.txt": "",
  1524. "foo/BUILD.bazel": "",
  1525. "foo/a.txt": "",
  1526. "foo/bar/BUILD.bazel": "",
  1527. "foo/bar/b.txt": "",
  1528. },
  1529. ExpectedBazelTargets: []string{
  1530. MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
  1531. "srcs": `[
  1532. "a.txt",
  1533. "b.txt",
  1534. "//foo:a.txt",
  1535. "//foo:bar/b.txt",
  1536. ]`,
  1537. }),
  1538. },
  1539. },
  1540. {
  1541. Description: "filegroup with glob with Android.bp file as boundary",
  1542. ModuleTypeUnderTest: "filegroup",
  1543. ModuleTypeUnderTestFactory: android.FileGroupFactory,
  1544. Blueprint: `filegroup {
  1545. name: "fg_foo",
  1546. srcs: ["**/*.txt"],
  1547. bazel_module: { bp2build_available: true },
  1548. }`,
  1549. Filesystem: map[string]string{
  1550. "a.txt": "",
  1551. "b.txt": "",
  1552. "foo/Android.bp": "",
  1553. "foo/a.txt": "",
  1554. "foo/bar/Android.bp": "",
  1555. "foo/bar/b.txt": "",
  1556. },
  1557. ExpectedBazelTargets: []string{
  1558. MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
  1559. "srcs": `[
  1560. "a.txt",
  1561. "b.txt",
  1562. "//foo:a.txt",
  1563. "//foo/bar:b.txt",
  1564. ]`,
  1565. }),
  1566. },
  1567. },
  1568. {
  1569. Description: "filegroup with glob in subdir with kept BUILD and BUILD.bazel file",
  1570. ModuleTypeUnderTest: "filegroup",
  1571. ModuleTypeUnderTestFactory: android.FileGroupFactory,
  1572. Dir: "other",
  1573. KeepBuildFileForDirs: []string{
  1574. "other/foo",
  1575. "other/foo/bar",
  1576. // deliberately not other/foo/baz/BUILD.
  1577. },
  1578. Filesystem: map[string]string{
  1579. "other/Android.bp": `filegroup {
  1580. name: "fg_foo",
  1581. srcs: ["**/*.txt"],
  1582. bazel_module: { bp2build_available: true },
  1583. }`,
  1584. "other/a.txt": "",
  1585. "other/b.txt": "",
  1586. "other/foo/BUILD": "",
  1587. "other/foo/a.txt": "",
  1588. "other/foo/bar/BUILD.bazel": "",
  1589. "other/foo/bar/b.txt": "",
  1590. "other/foo/baz/BUILD": "",
  1591. "other/foo/baz/c.txt": "",
  1592. },
  1593. ExpectedBazelTargets: []string{
  1594. MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
  1595. "srcs": `[
  1596. "a.txt",
  1597. "b.txt",
  1598. "//other/foo:a.txt",
  1599. "//other/foo/bar:b.txt",
  1600. "//other/foo:baz/c.txt",
  1601. ]`,
  1602. }),
  1603. },
  1604. },
  1605. }
  1606. for _, testCase := range testCases {
  1607. t.Run(testCase.Description, func(t *testing.T) {
  1608. RunBp2BuildTestCaseSimple(t, testCase)
  1609. })
  1610. }
  1611. }
  1612. func TestGlobExcludeSrcs(t *testing.T) {
  1613. testCases := []Bp2buildTestCase{
  1614. {
  1615. Description: "filegroup top level exclude_srcs",
  1616. ModuleTypeUnderTest: "filegroup",
  1617. ModuleTypeUnderTestFactory: android.FileGroupFactory,
  1618. Blueprint: `filegroup {
  1619. name: "fg_foo",
  1620. srcs: ["**/*.txt"],
  1621. exclude_srcs: ["c.txt"],
  1622. bazel_module: { bp2build_available: true },
  1623. }`,
  1624. Filesystem: map[string]string{
  1625. "a.txt": "",
  1626. "b.txt": "",
  1627. "c.txt": "",
  1628. "dir/Android.bp": "",
  1629. "dir/e.txt": "",
  1630. "dir/f.txt": "",
  1631. },
  1632. ExpectedBazelTargets: []string{
  1633. MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
  1634. "srcs": `[
  1635. "a.txt",
  1636. "b.txt",
  1637. "//dir:e.txt",
  1638. "//dir:f.txt",
  1639. ]`,
  1640. }),
  1641. },
  1642. },
  1643. {
  1644. Description: "filegroup in subdir exclude_srcs",
  1645. ModuleTypeUnderTest: "filegroup",
  1646. ModuleTypeUnderTestFactory: android.FileGroupFactory,
  1647. Blueprint: "",
  1648. Dir: "dir",
  1649. Filesystem: map[string]string{
  1650. "dir/Android.bp": `filegroup {
  1651. name: "fg_foo",
  1652. srcs: ["**/*.txt"],
  1653. exclude_srcs: ["b.txt"],
  1654. bazel_module: { bp2build_available: true },
  1655. }
  1656. `,
  1657. "dir/a.txt": "",
  1658. "dir/b.txt": "",
  1659. "dir/subdir/Android.bp": "",
  1660. "dir/subdir/e.txt": "",
  1661. "dir/subdir/f.txt": "",
  1662. },
  1663. ExpectedBazelTargets: []string{
  1664. MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
  1665. "srcs": `[
  1666. "a.txt",
  1667. "//dir/subdir:e.txt",
  1668. "//dir/subdir:f.txt",
  1669. ]`,
  1670. }),
  1671. },
  1672. },
  1673. }
  1674. for _, testCase := range testCases {
  1675. t.Run(testCase.Description, func(t *testing.T) {
  1676. RunBp2BuildTestCaseSimple(t, testCase)
  1677. })
  1678. }
  1679. }
  1680. func TestCommonBp2BuildModuleAttrs(t *testing.T) {
  1681. testCases := []Bp2buildTestCase{
  1682. {
  1683. Description: "Required into data test",
  1684. ModuleTypeUnderTest: "filegroup",
  1685. ModuleTypeUnderTestFactory: android.FileGroupFactory,
  1686. Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + `
  1687. filegroup {
  1688. name: "fg_foo",
  1689. required: ["reqd"],
  1690. bazel_module: { bp2build_available: true },
  1691. }`,
  1692. ExpectedBazelTargets: []string{
  1693. MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
  1694. "data": `[":reqd"]`,
  1695. }),
  1696. },
  1697. },
  1698. {
  1699. Description: "Required into data test, cyclic self reference is filtered out",
  1700. ModuleTypeUnderTest: "filegroup",
  1701. ModuleTypeUnderTestFactory: android.FileGroupFactory,
  1702. Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + `
  1703. filegroup {
  1704. name: "fg_foo",
  1705. required: ["reqd", "fg_foo"],
  1706. bazel_module: { bp2build_available: true },
  1707. }`,
  1708. ExpectedBazelTargets: []string{
  1709. MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
  1710. "data": `[":reqd"]`,
  1711. }),
  1712. },
  1713. },
  1714. {
  1715. Description: "Required via arch into data test",
  1716. ModuleTypeUnderTest: "python_library",
  1717. ModuleTypeUnderTestFactory: python.PythonLibraryFactory,
  1718. Blueprint: simpleModuleDoNotConvertBp2build("python_library", "reqdx86") +
  1719. simpleModuleDoNotConvertBp2build("python_library", "reqdarm") + `
  1720. python_library {
  1721. name: "fg_foo",
  1722. arch: {
  1723. arm: {
  1724. required: ["reqdarm"],
  1725. },
  1726. x86: {
  1727. required: ["reqdx86"],
  1728. },
  1729. },
  1730. bazel_module: { bp2build_available: true },
  1731. }`,
  1732. ExpectedBazelTargets: []string{
  1733. MakeBazelTarget("py_library", "fg_foo", map[string]string{
  1734. "data": `select({
  1735. "//build/bazel/platforms/arch:arm": [":reqdarm"],
  1736. "//build/bazel/platforms/arch:x86": [":reqdx86"],
  1737. "//conditions:default": [],
  1738. })`,
  1739. "srcs_version": `"PY3"`,
  1740. "imports": `["."]`,
  1741. }),
  1742. },
  1743. },
  1744. {
  1745. Description: "Required appended to data test",
  1746. ModuleTypeUnderTest: "python_library",
  1747. ModuleTypeUnderTestFactory: python.PythonLibraryFactory,
  1748. Filesystem: map[string]string{
  1749. "data.bin": "",
  1750. "src.py": "",
  1751. },
  1752. Blueprint: simpleModuleDoNotConvertBp2build("python_library", "reqd") + `
  1753. python_library {
  1754. name: "fg_foo",
  1755. data: ["data.bin"],
  1756. required: ["reqd"],
  1757. bazel_module: { bp2build_available: true },
  1758. }`,
  1759. ExpectedBazelTargets: []string{
  1760. MakeBazelTarget("py_library", "fg_foo", map[string]string{
  1761. "data": `[
  1762. "data.bin",
  1763. ":reqd",
  1764. ]`,
  1765. "srcs_version": `"PY3"`,
  1766. "imports": `["."]`,
  1767. }),
  1768. },
  1769. },
  1770. {
  1771. Description: "All props-to-attrs at once together test",
  1772. ModuleTypeUnderTest: "filegroup",
  1773. ModuleTypeUnderTestFactory: android.FileGroupFactory,
  1774. Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + `
  1775. filegroup {
  1776. name: "fg_foo",
  1777. required: ["reqd"],
  1778. bazel_module: { bp2build_available: true },
  1779. }`,
  1780. ExpectedBazelTargets: []string{
  1781. MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
  1782. "data": `[":reqd"]`,
  1783. }),
  1784. },
  1785. },
  1786. }
  1787. for _, tc := range testCases {
  1788. t.Run(tc.Description, func(t *testing.T) {
  1789. RunBp2BuildTestCaseSimple(t, tc)
  1790. })
  1791. }
  1792. }
  1793. func TestLicensesAttrConversion(t *testing.T) {
  1794. RunBp2BuildTestCase(t,
  1795. func(ctx android.RegistrationContext) {
  1796. ctx.RegisterModuleType("license", android.LicenseFactory)
  1797. },
  1798. Bp2buildTestCase{
  1799. Description: "Test that licenses: attribute is converted",
  1800. ModuleTypeUnderTest: "filegroup",
  1801. ModuleTypeUnderTestFactory: android.FileGroupFactory,
  1802. Blueprint: `
  1803. license {
  1804. name: "my_license",
  1805. }
  1806. filegroup {
  1807. name: "my_filegroup",
  1808. licenses: ["my_license"],
  1809. }
  1810. `,
  1811. ExpectedBazelTargets: []string{
  1812. MakeBazelTargetNoRestrictions("filegroup", "my_filegroup", AttrNameToString{
  1813. "applicable_licenses": `[":my_license"]`,
  1814. }),
  1815. MakeBazelTargetNoRestrictions("android_license", "my_license", AttrNameToString{}),
  1816. },
  1817. })
  1818. }
  1819. func TestGenerateApiBazelTargets(t *testing.T) {
  1820. bp := `
  1821. custom {
  1822. name: "foo",
  1823. api: "foo.txt",
  1824. }
  1825. `
  1826. expectedBazelTarget := MakeBazelTarget(
  1827. "custom_api_contribution",
  1828. "foo",
  1829. AttrNameToString{
  1830. "api": `"foo.txt"`,
  1831. },
  1832. )
  1833. registerCustomModule := func(ctx android.RegistrationContext) {
  1834. ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
  1835. }
  1836. RunApiBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
  1837. Blueprint: bp,
  1838. ExpectedBazelTargets: []string{expectedBazelTarget},
  1839. Description: "Generating API contribution Bazel targets for custom module",
  1840. })
  1841. }
  1842. func TestGenerateConfigSetting(t *testing.T) {
  1843. bp := `
  1844. custom {
  1845. name: "foo",
  1846. test_config_setting: true,
  1847. }
  1848. `
  1849. expectedBazelTargets := []string{
  1850. MakeBazelTargetNoRestrictions(
  1851. "config_setting",
  1852. "foo_config_setting",
  1853. AttrNameToString{
  1854. "flag_values": `{
  1855. "//build/bazel/rules/my_string_setting": "foo",
  1856. }`,
  1857. },
  1858. ),
  1859. MakeBazelTarget(
  1860. "custom",
  1861. "foo",
  1862. AttrNameToString{},
  1863. ),
  1864. }
  1865. registerCustomModule := func(ctx android.RegistrationContext) {
  1866. ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
  1867. }
  1868. RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
  1869. Blueprint: bp,
  1870. ExpectedBazelTargets: expectedBazelTargets,
  1871. Description: "Generating API contribution Bazel targets for custom module",
  1872. })
  1873. }
  1874. // If values of all keys in an axis are equal to //conditions:default, drop the axis and print the common value
  1875. func TestPrettyPrintSelectMapEqualValues(t *testing.T) {
  1876. lla := bazel.LabelListAttribute{
  1877. Value: bazel.LabelList{},
  1878. }
  1879. libFooImplLabel := bazel.Label{
  1880. Label: ":libfoo.impl",
  1881. }
  1882. lla.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidPlatform, bazel.MakeLabelList([]bazel.Label{libFooImplLabel}))
  1883. lla.SetSelectValue(bazel.OsAndInApexAxis, bazel.ConditionsDefaultConfigKey, bazel.MakeLabelList([]bazel.Label{libFooImplLabel}))
  1884. actual, _ := prettyPrintAttribute(lla, 0)
  1885. android.AssertStringEquals(t, "Print the common value if all keys in an axis have the same value", `[":libfoo.impl"]`, actual)
  1886. }