build_conversion_test.go 53 KB

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