cc_library_static_conversion_test.go 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217
  1. // Copyright 2021 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package bp2build
  15. import (
  16. "fmt"
  17. "testing"
  18. "android/soong/android"
  19. "android/soong/cc"
  20. "android/soong/genrule"
  21. )
  22. const (
  23. // See cc/testing.go for more context
  24. soongCcLibraryStaticPreamble = `
  25. cc_defaults {
  26. name: "linux_bionic_supported",
  27. }`
  28. )
  29. func TestCcLibraryStaticLoadStatement(t *testing.T) {
  30. testCases := []struct {
  31. bazelTargets BazelTargets
  32. expectedLoadStatements string
  33. }{
  34. {
  35. bazelTargets: BazelTargets{
  36. BazelTarget{
  37. name: "cc_library_static_target",
  38. ruleClass: "cc_library_static",
  39. // NOTE: No bzlLoadLocation for native rules
  40. },
  41. },
  42. expectedLoadStatements: ``,
  43. },
  44. }
  45. for _, testCase := range testCases {
  46. actual := testCase.bazelTargets.LoadStatements()
  47. expected := testCase.expectedLoadStatements
  48. if actual != expected {
  49. t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
  50. }
  51. }
  52. }
  53. func registerCcLibraryStaticModuleTypes(ctx android.RegistrationContext) {
  54. cc.RegisterCCBuildComponents(ctx)
  55. ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
  56. ctx.RegisterModuleType("genrule", genrule.GenRuleFactory)
  57. // Required for system_shared_libs dependencies.
  58. ctx.RegisterModuleType("cc_library", cc.LibraryFactory)
  59. }
  60. func runCcLibraryStaticTestCase(t *testing.T, tc Bp2buildTestCase) {
  61. t.Helper()
  62. (&tc).ModuleTypeUnderTest = "cc_library_static"
  63. (&tc).ModuleTypeUnderTestFactory = cc.LibraryStaticFactory
  64. RunBp2BuildTestCase(t, registerCcLibraryStaticModuleTypes, tc)
  65. }
  66. func TestCcLibraryStaticSimple(t *testing.T) {
  67. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  68. Description: "cc_library_static test",
  69. Filesystem: map[string]string{
  70. // NOTE: include_dir headers *should not* appear in Bazel hdrs later (?)
  71. "include_dir_1/include_dir_1_a.h": "",
  72. "include_dir_1/include_dir_1_b.h": "",
  73. "include_dir_2/include_dir_2_a.h": "",
  74. "include_dir_2/include_dir_2_b.h": "",
  75. // NOTE: local_include_dir headers *should not* appear in Bazel hdrs later (?)
  76. "local_include_dir_1/local_include_dir_1_a.h": "",
  77. "local_include_dir_1/local_include_dir_1_b.h": "",
  78. "local_include_dir_2/local_include_dir_2_a.h": "",
  79. "local_include_dir_2/local_include_dir_2_b.h": "",
  80. // NOTE: export_include_dir headers *should* appear in Bazel hdrs later
  81. "export_include_dir_1/export_include_dir_1_a.h": "",
  82. "export_include_dir_1/export_include_dir_1_b.h": "",
  83. "export_include_dir_2/export_include_dir_2_a.h": "",
  84. "export_include_dir_2/export_include_dir_2_b.h": "",
  85. // NOTE: Soong implicitly includes headers in the current directory
  86. "implicit_include_1.h": "",
  87. "implicit_include_2.h": "",
  88. },
  89. Blueprint: soongCcLibraryStaticPreamble + `
  90. cc_library_headers {
  91. name: "header_lib_1",
  92. export_include_dirs: ["header_lib_1"],
  93. bazel_module: { bp2build_available: false },
  94. }
  95. cc_library_headers {
  96. name: "header_lib_2",
  97. export_include_dirs: ["header_lib_2"],
  98. bazel_module: { bp2build_available: false },
  99. }
  100. cc_library_static {
  101. name: "static_lib_1",
  102. srcs: ["static_lib_1.cc"],
  103. bazel_module: { bp2build_available: false },
  104. }
  105. cc_library_static {
  106. name: "static_lib_2",
  107. srcs: ["static_lib_2.cc"],
  108. bazel_module: { bp2build_available: false },
  109. }
  110. cc_library_static {
  111. name: "whole_static_lib_1",
  112. srcs: ["whole_static_lib_1.cc"],
  113. bazel_module: { bp2build_available: false },
  114. }
  115. cc_library_static {
  116. name: "whole_static_lib_2",
  117. srcs: ["whole_static_lib_2.cc"],
  118. bazel_module: { bp2build_available: false },
  119. }
  120. cc_library_static {
  121. name: "foo_static",
  122. srcs: [
  123. "foo_static1.cc",
  124. "foo_static2.cc",
  125. ],
  126. cflags: [
  127. "-Dflag1",
  128. "-Dflag2"
  129. ],
  130. static_libs: [
  131. "static_lib_1",
  132. "static_lib_2"
  133. ],
  134. whole_static_libs: [
  135. "whole_static_lib_1",
  136. "whole_static_lib_2"
  137. ],
  138. include_dirs: [
  139. "include_dir_1",
  140. "include_dir_2",
  141. ],
  142. local_include_dirs: [
  143. "local_include_dir_1",
  144. "local_include_dir_2",
  145. ],
  146. export_include_dirs: [
  147. "export_include_dir_1",
  148. "export_include_dir_2"
  149. ],
  150. header_libs: [
  151. "header_lib_1",
  152. "header_lib_2"
  153. ],
  154. sdk_version: "current",
  155. min_sdk_version: "29",
  156. // TODO: Also support export_header_lib_headers
  157. }`,
  158. ExpectedBazelTargets: []string{
  159. MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{
  160. "absolute_includes": `[
  161. "include_dir_1",
  162. "include_dir_2",
  163. ]`,
  164. "copts": `[
  165. "-Dflag1",
  166. "-Dflag2",
  167. ]`,
  168. "export_includes": `[
  169. "export_include_dir_1",
  170. "export_include_dir_2",
  171. ]`,
  172. "implementation_deps": `[
  173. ":header_lib_1",
  174. ":header_lib_2",
  175. ":static_lib_1",
  176. ":static_lib_2",
  177. ]`,
  178. "local_includes": `[
  179. "local_include_dir_1",
  180. "local_include_dir_2",
  181. ".",
  182. ]`,
  183. "srcs": `[
  184. "foo_static1.cc",
  185. "foo_static2.cc",
  186. ]`,
  187. "whole_archive_deps": `[
  188. ":whole_static_lib_1",
  189. ":whole_static_lib_2",
  190. ]`,
  191. "sdk_version": `"current"`,
  192. "min_sdk_version": `"29"`,
  193. }),
  194. },
  195. })
  196. }
  197. func TestCcLibraryStaticSubpackage(t *testing.T) {
  198. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  199. Description: "cc_library_static subpackage test",
  200. Filesystem: map[string]string{
  201. // subpackage with subdirectory
  202. "subpackage/Android.bp": "",
  203. "subpackage/subpackage_header.h": "",
  204. "subpackage/subdirectory/subdirectory_header.h": "",
  205. // subsubpackage with subdirectory
  206. "subpackage/subsubpackage/Android.bp": "",
  207. "subpackage/subsubpackage/subsubpackage_header.h": "",
  208. "subpackage/subsubpackage/subdirectory/subdirectory_header.h": "",
  209. // subsubsubpackage with subdirectory
  210. "subpackage/subsubpackage/subsubsubpackage/Android.bp": "",
  211. "subpackage/subsubpackage/subsubsubpackage/subsubsubpackage_header.h": "",
  212. "subpackage/subsubpackage/subsubsubpackage/subdirectory/subdirectory_header.h": "",
  213. },
  214. Blueprint: soongCcLibraryStaticPreamble + `
  215. cc_library_static {
  216. name: "foo_static",
  217. srcs: [],
  218. include_dirs: [
  219. "subpackage",
  220. ],
  221. }`,
  222. ExpectedBazelTargets: []string{
  223. MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{
  224. "absolute_includes": `["subpackage"]`,
  225. "local_includes": `["."]`,
  226. }),
  227. },
  228. })
  229. }
  230. func TestCcLibraryStaticExportIncludeDir(t *testing.T) {
  231. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  232. Description: "cc_library_static export include dir",
  233. Filesystem: map[string]string{
  234. // subpackage with subdirectory
  235. "subpackage/Android.bp": "",
  236. "subpackage/subpackage_header.h": "",
  237. "subpackage/subdirectory/subdirectory_header.h": "",
  238. },
  239. Blueprint: soongCcLibraryStaticPreamble + `
  240. cc_library_static {
  241. name: "foo_static",
  242. export_include_dirs: ["subpackage"],
  243. include_build_directory: false,
  244. }`,
  245. ExpectedBazelTargets: []string{
  246. MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{
  247. "export_includes": `["subpackage"]`,
  248. }),
  249. },
  250. })
  251. }
  252. func TestCcLibraryStaticExportSystemIncludeDir(t *testing.T) {
  253. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  254. Description: "cc_library_static export system include dir",
  255. Filesystem: map[string]string{
  256. // subpackage with subdirectory
  257. "subpackage/Android.bp": "",
  258. "subpackage/subpackage_header.h": "",
  259. "subpackage/subdirectory/subdirectory_header.h": "",
  260. },
  261. Blueprint: soongCcLibraryStaticPreamble + `
  262. cc_library_static {
  263. name: "foo_static",
  264. export_system_include_dirs: ["subpackage"],
  265. include_build_directory: false,
  266. }`,
  267. ExpectedBazelTargets: []string{
  268. MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{
  269. "export_system_includes": `["subpackage"]`,
  270. }),
  271. },
  272. })
  273. }
  274. func TestCcLibraryStaticManyIncludeDirs(t *testing.T) {
  275. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  276. Description: "cc_library_static include_dirs, local_include_dirs, export_include_dirs (b/183742505)",
  277. Dir: "subpackage",
  278. Filesystem: map[string]string{
  279. // subpackage with subdirectory
  280. "subpackage/Android.bp": `
  281. cc_library_static {
  282. name: "foo_static",
  283. // include_dirs are workspace/root relative
  284. include_dirs: [
  285. "subpackage/subsubpackage",
  286. "subpackage2",
  287. "subpackage3/subsubpackage"
  288. ],
  289. local_include_dirs: ["subsubpackage2"], // module dir relative
  290. export_include_dirs: ["./exported_subsubpackage"], // module dir relative
  291. include_build_directory: true,
  292. bazel_module: { bp2build_available: true },
  293. }`,
  294. "subpackage/subsubpackage/header.h": "",
  295. "subpackage/subsubpackage2/header.h": "",
  296. "subpackage/exported_subsubpackage/header.h": "",
  297. "subpackage2/header.h": "",
  298. "subpackage3/subsubpackage/header.h": "",
  299. },
  300. Blueprint: soongCcLibraryStaticPreamble,
  301. ExpectedBazelTargets: []string{
  302. MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{
  303. "absolute_includes": `[
  304. "subpackage/subsubpackage",
  305. "subpackage2",
  306. "subpackage3/subsubpackage",
  307. ]`,
  308. "export_includes": `["./exported_subsubpackage"]`,
  309. "local_includes": `[
  310. "subsubpackage2",
  311. ".",
  312. ]`,
  313. })},
  314. })
  315. }
  316. func TestCcLibraryStaticIncludeBuildDirectoryDisabled(t *testing.T) {
  317. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  318. Description: "cc_library_static include_build_directory disabled",
  319. Filesystem: map[string]string{
  320. // subpackage with subdirectory
  321. "subpackage/Android.bp": "",
  322. "subpackage/subpackage_header.h": "",
  323. "subpackage/subdirectory/subdirectory_header.h": "",
  324. },
  325. Blueprint: soongCcLibraryStaticPreamble + `
  326. cc_library_static {
  327. name: "foo_static",
  328. include_dirs: ["subpackage"], // still used, but local_include_dirs is recommended
  329. local_include_dirs: ["subpackage2"],
  330. include_build_directory: false,
  331. }`,
  332. ExpectedBazelTargets: []string{
  333. MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{
  334. "absolute_includes": `["subpackage"]`,
  335. "local_includes": `["subpackage2"]`,
  336. }),
  337. },
  338. })
  339. }
  340. func TestCcLibraryStaticIncludeBuildDirectoryEnabled(t *testing.T) {
  341. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  342. Description: "cc_library_static include_build_directory enabled",
  343. Filesystem: map[string]string{
  344. // subpackage with subdirectory
  345. "subpackage/Android.bp": "",
  346. "subpackage/subpackage_header.h": "",
  347. "subpackage2/Android.bp": "",
  348. "subpackage2/subpackage2_header.h": "",
  349. "subpackage/subdirectory/subdirectory_header.h": "",
  350. },
  351. Blueprint: soongCcLibraryStaticPreamble + `
  352. cc_library_static {
  353. name: "foo_static",
  354. include_dirs: ["subpackage"], // still used, but local_include_dirs is recommended
  355. local_include_dirs: ["subpackage2"],
  356. include_build_directory: true,
  357. }`,
  358. ExpectedBazelTargets: []string{
  359. MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{
  360. "absolute_includes": `["subpackage"]`,
  361. "local_includes": `[
  362. "subpackage2",
  363. ".",
  364. ]`,
  365. }),
  366. },
  367. })
  368. }
  369. func TestCcLibraryStaticArchSpecificStaticLib(t *testing.T) {
  370. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  371. Description: "cc_library_static arch-specific static_libs",
  372. Filesystem: map[string]string{},
  373. Blueprint: soongCcLibraryStaticPreamble + `
  374. cc_library_static {
  375. name: "static_dep",
  376. bazel_module: { bp2build_available: false },
  377. }
  378. cc_library_static {
  379. name: "static_dep2",
  380. bazel_module: { bp2build_available: false },
  381. }
  382. cc_library_static {
  383. name: "foo_static",
  384. arch: { arm64: { static_libs: ["static_dep"], whole_static_libs: ["static_dep2"] } },
  385. include_build_directory: false,
  386. }`,
  387. ExpectedBazelTargets: []string{
  388. MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{
  389. "implementation_deps": `select({
  390. "//build/bazel/platforms/arch:arm64": [":static_dep"],
  391. "//conditions:default": [],
  392. })`,
  393. "whole_archive_deps": `select({
  394. "//build/bazel/platforms/arch:arm64": [":static_dep2"],
  395. "//conditions:default": [],
  396. })`,
  397. }),
  398. },
  399. })
  400. }
  401. func TestCcLibraryStaticOsSpecificStaticLib(t *testing.T) {
  402. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  403. Description: "cc_library_static os-specific static_libs",
  404. Filesystem: map[string]string{},
  405. Blueprint: soongCcLibraryStaticPreamble + `
  406. cc_library_static {
  407. name: "static_dep",
  408. bazel_module: { bp2build_available: false },
  409. }
  410. cc_library_static {
  411. name: "static_dep2",
  412. bazel_module: { bp2build_available: false },
  413. }
  414. cc_library_static {
  415. name: "foo_static",
  416. target: { android: { static_libs: ["static_dep"], whole_static_libs: ["static_dep2"] } },
  417. include_build_directory: false,
  418. }`,
  419. ExpectedBazelTargets: []string{
  420. MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{
  421. "implementation_deps": `select({
  422. "//build/bazel/platforms/os:android": [":static_dep"],
  423. "//conditions:default": [],
  424. })`,
  425. "whole_archive_deps": `select({
  426. "//build/bazel/platforms/os:android": [":static_dep2"],
  427. "//conditions:default": [],
  428. })`,
  429. }),
  430. },
  431. })
  432. }
  433. func TestCcLibraryStaticBaseArchOsSpecificStaticLib(t *testing.T) {
  434. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  435. Description: "cc_library_static base, arch and os-specific static_libs",
  436. Filesystem: map[string]string{},
  437. Blueprint: soongCcLibraryStaticPreamble + `
  438. cc_library_static {
  439. name: "static_dep",
  440. bazel_module: { bp2build_available: false },
  441. }
  442. cc_library_static {
  443. name: "static_dep2",
  444. bazel_module: { bp2build_available: false },
  445. }
  446. cc_library_static {
  447. name: "static_dep3",
  448. bazel_module: { bp2build_available: false },
  449. }
  450. cc_library_static {
  451. name: "static_dep4",
  452. bazel_module: { bp2build_available: false },
  453. }
  454. cc_library_static {
  455. name: "foo_static",
  456. static_libs: ["static_dep"],
  457. whole_static_libs: ["static_dep2"],
  458. target: { android: { static_libs: ["static_dep3"] } },
  459. arch: { arm64: { static_libs: ["static_dep4"] } },
  460. include_build_directory: false,
  461. }`,
  462. ExpectedBazelTargets: []string{
  463. MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{
  464. "implementation_deps": `[":static_dep"] + select({
  465. "//build/bazel/platforms/arch:arm64": [":static_dep4"],
  466. "//conditions:default": [],
  467. }) + select({
  468. "//build/bazel/platforms/os:android": [":static_dep3"],
  469. "//conditions:default": [],
  470. })`,
  471. "whole_archive_deps": `[":static_dep2"]`,
  472. }),
  473. },
  474. })
  475. }
  476. func TestCcLibraryStaticSimpleExcludeSrcs(t *testing.T) {
  477. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  478. Description: "cc_library_static simple exclude_srcs",
  479. Filesystem: map[string]string{
  480. "common.c": "",
  481. "foo-a.c": "",
  482. "foo-excluded.c": "",
  483. },
  484. Blueprint: soongCcLibraryStaticPreamble + `
  485. cc_library_static {
  486. name: "foo_static",
  487. srcs: ["common.c", "foo-*.c"],
  488. exclude_srcs: ["foo-excluded.c"],
  489. include_build_directory: false,
  490. }`,
  491. ExpectedBazelTargets: []string{
  492. MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{
  493. "srcs_c": `[
  494. "common.c",
  495. "foo-a.c",
  496. ]`,
  497. }),
  498. },
  499. })
  500. }
  501. func TestCcLibraryStaticOneArchSrcs(t *testing.T) {
  502. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  503. Description: "cc_library_static one arch specific srcs",
  504. Filesystem: map[string]string{
  505. "common.c": "",
  506. "foo-arm.c": "",
  507. },
  508. Blueprint: soongCcLibraryStaticPreamble + `
  509. cc_library_static {
  510. name: "foo_static",
  511. srcs: ["common.c"],
  512. arch: { arm: { srcs: ["foo-arm.c"] } },
  513. include_build_directory: false,
  514. }`,
  515. ExpectedBazelTargets: []string{
  516. MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{
  517. "srcs_c": `["common.c"] + select({
  518. "//build/bazel/platforms/arch:arm": ["foo-arm.c"],
  519. "//conditions:default": [],
  520. })`,
  521. }),
  522. },
  523. })
  524. }
  525. func TestCcLibraryStaticOneArchSrcsExcludeSrcs(t *testing.T) {
  526. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  527. Description: "cc_library_static one arch specific srcs and exclude_srcs",
  528. Filesystem: map[string]string{
  529. "common.c": "",
  530. "for-arm.c": "",
  531. "not-for-arm.c": "",
  532. "not-for-anything.c": "",
  533. },
  534. Blueprint: soongCcLibraryStaticPreamble + `
  535. cc_library_static {
  536. name: "foo_static",
  537. srcs: ["common.c", "not-for-*.c"],
  538. exclude_srcs: ["not-for-anything.c"],
  539. arch: {
  540. arm: { srcs: ["for-arm.c"], exclude_srcs: ["not-for-arm.c"] },
  541. },
  542. include_build_directory: false,
  543. }`,
  544. ExpectedBazelTargets: []string{
  545. MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{
  546. "srcs_c": `["common.c"] + select({
  547. "//build/bazel/platforms/arch:arm": ["for-arm.c"],
  548. "//conditions:default": ["not-for-arm.c"],
  549. })`,
  550. }),
  551. },
  552. })
  553. }
  554. func TestCcLibraryStaticTwoArchExcludeSrcs(t *testing.T) {
  555. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  556. Description: "cc_library_static arch specific exclude_srcs for 2 architectures",
  557. Filesystem: map[string]string{
  558. "common.c": "",
  559. "for-arm.c": "",
  560. "for-x86.c": "",
  561. "not-for-arm.c": "",
  562. "not-for-x86.c": "",
  563. },
  564. Blueprint: soongCcLibraryStaticPreamble + `
  565. cc_library_static {
  566. name: "foo_static",
  567. srcs: ["common.c", "not-for-*.c"],
  568. exclude_srcs: ["not-for-everything.c"],
  569. arch: {
  570. arm: { srcs: ["for-arm.c"], exclude_srcs: ["not-for-arm.c"] },
  571. x86: { srcs: ["for-x86.c"], exclude_srcs: ["not-for-x86.c"] },
  572. },
  573. include_build_directory: false,
  574. } `,
  575. ExpectedBazelTargets: []string{
  576. MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{
  577. "srcs_c": `["common.c"] + select({
  578. "//build/bazel/platforms/arch:arm": [
  579. "not-for-x86.c",
  580. "for-arm.c",
  581. ],
  582. "//build/bazel/platforms/arch:x86": [
  583. "not-for-arm.c",
  584. "for-x86.c",
  585. ],
  586. "//conditions:default": [
  587. "not-for-arm.c",
  588. "not-for-x86.c",
  589. ],
  590. })`,
  591. }),
  592. },
  593. })
  594. }
  595. func TestCcLibraryStaticFourArchExcludeSrcs(t *testing.T) {
  596. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  597. Description: "cc_library_static arch specific exclude_srcs for 4 architectures",
  598. Filesystem: map[string]string{
  599. "common.c": "",
  600. "for-arm.c": "",
  601. "for-arm64.c": "",
  602. "for-x86.c": "",
  603. "for-x86_64.c": "",
  604. "not-for-arm.c": "",
  605. "not-for-arm64.c": "",
  606. "not-for-x86.c": "",
  607. "not-for-x86_64.c": "",
  608. "not-for-everything.c": "",
  609. },
  610. Blueprint: soongCcLibraryStaticPreamble + `
  611. cc_library_static {
  612. name: "foo_static",
  613. srcs: ["common.c", "not-for-*.c"],
  614. exclude_srcs: ["not-for-everything.c"],
  615. arch: {
  616. arm: { srcs: ["for-arm.c"], exclude_srcs: ["not-for-arm.c"] },
  617. arm64: { srcs: ["for-arm64.c"], exclude_srcs: ["not-for-arm64.c"] },
  618. x86: { srcs: ["for-x86.c"], exclude_srcs: ["not-for-x86.c"] },
  619. x86_64: { srcs: ["for-x86_64.c"], exclude_srcs: ["not-for-x86_64.c"] },
  620. },
  621. include_build_directory: false,
  622. } `,
  623. ExpectedBazelTargets: []string{
  624. MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{
  625. "srcs_c": `["common.c"] + select({
  626. "//build/bazel/platforms/arch:arm": [
  627. "not-for-arm64.c",
  628. "not-for-x86.c",
  629. "not-for-x86_64.c",
  630. "for-arm.c",
  631. ],
  632. "//build/bazel/platforms/arch:arm64": [
  633. "not-for-arm.c",
  634. "not-for-x86.c",
  635. "not-for-x86_64.c",
  636. "for-arm64.c",
  637. ],
  638. "//build/bazel/platforms/arch:x86": [
  639. "not-for-arm.c",
  640. "not-for-arm64.c",
  641. "not-for-x86_64.c",
  642. "for-x86.c",
  643. ],
  644. "//build/bazel/platforms/arch:x86_64": [
  645. "not-for-arm.c",
  646. "not-for-arm64.c",
  647. "not-for-x86.c",
  648. "for-x86_64.c",
  649. ],
  650. "//conditions:default": [
  651. "not-for-arm.c",
  652. "not-for-arm64.c",
  653. "not-for-x86.c",
  654. "not-for-x86_64.c",
  655. ],
  656. })`,
  657. }),
  658. },
  659. })
  660. }
  661. func TestCcLibraryStaticOneArchEmpty(t *testing.T) {
  662. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  663. Description: "cc_library_static one arch empty",
  664. Filesystem: map[string]string{
  665. "common.cc": "",
  666. "foo-no-arm.cc": "",
  667. "foo-excluded.cc": "",
  668. },
  669. Blueprint: soongCcLibraryStaticPreamble + `
  670. cc_library_static {
  671. name: "foo_static",
  672. srcs: ["common.cc", "foo-*.cc"],
  673. exclude_srcs: ["foo-excluded.cc"],
  674. arch: {
  675. arm: { exclude_srcs: ["foo-no-arm.cc"] },
  676. },
  677. include_build_directory: false,
  678. }`,
  679. ExpectedBazelTargets: []string{
  680. MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{
  681. "srcs": `["common.cc"] + select({
  682. "//build/bazel/platforms/arch:arm": [],
  683. "//conditions:default": ["foo-no-arm.cc"],
  684. })`,
  685. }),
  686. },
  687. })
  688. }
  689. func TestCcLibraryStaticOneArchEmptyOtherSet(t *testing.T) {
  690. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  691. Description: "cc_library_static one arch empty other set",
  692. Filesystem: map[string]string{
  693. "common.cc": "",
  694. "foo-no-arm.cc": "",
  695. "x86-only.cc": "",
  696. "foo-excluded.cc": "",
  697. },
  698. Blueprint: soongCcLibraryStaticPreamble + `
  699. cc_library_static {
  700. name: "foo_static",
  701. srcs: ["common.cc", "foo-*.cc"],
  702. exclude_srcs: ["foo-excluded.cc"],
  703. arch: {
  704. arm: { exclude_srcs: ["foo-no-arm.cc"] },
  705. x86: { srcs: ["x86-only.cc"] },
  706. },
  707. include_build_directory: false,
  708. }`,
  709. ExpectedBazelTargets: []string{
  710. MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{
  711. "srcs": `["common.cc"] + select({
  712. "//build/bazel/platforms/arch:arm": [],
  713. "//build/bazel/platforms/arch:x86": [
  714. "foo-no-arm.cc",
  715. "x86-only.cc",
  716. ],
  717. "//conditions:default": ["foo-no-arm.cc"],
  718. })`,
  719. }),
  720. },
  721. })
  722. }
  723. func TestCcLibraryStaticMultipleDepSameName(t *testing.T) {
  724. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  725. Description: "cc_library_static multiple dep same name panic",
  726. Filesystem: map[string]string{},
  727. Blueprint: soongCcLibraryStaticPreamble + `
  728. cc_library_static {
  729. name: "static_dep",
  730. bazel_module: { bp2build_available: false },
  731. }
  732. cc_library_static {
  733. name: "foo_static",
  734. static_libs: ["static_dep", "static_dep"],
  735. include_build_directory: false,
  736. }`,
  737. ExpectedBazelTargets: []string{
  738. MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{
  739. "implementation_deps": `[":static_dep"]`,
  740. }),
  741. },
  742. })
  743. }
  744. func TestCcLibraryStaticOneMultilibSrcsExcludeSrcs(t *testing.T) {
  745. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  746. Description: "cc_library_static 1 multilib srcs and exclude_srcs",
  747. Filesystem: map[string]string{
  748. "common.c": "",
  749. "for-lib32.c": "",
  750. "not-for-lib32.c": "",
  751. },
  752. Blueprint: soongCcLibraryStaticPreamble + `
  753. cc_library_static {
  754. name: "foo_static",
  755. srcs: ["common.c", "not-for-*.c"],
  756. multilib: {
  757. lib32: { srcs: ["for-lib32.c"], exclude_srcs: ["not-for-lib32.c"] },
  758. },
  759. include_build_directory: false,
  760. } `,
  761. ExpectedBazelTargets: []string{
  762. MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{
  763. "srcs_c": `["common.c"] + select({
  764. "//build/bazel/platforms/arch:arm": ["for-lib32.c"],
  765. "//build/bazel/platforms/arch:x86": ["for-lib32.c"],
  766. "//conditions:default": ["not-for-lib32.c"],
  767. })`,
  768. }),
  769. },
  770. })
  771. }
  772. func TestCcLibraryStaticTwoMultilibSrcsExcludeSrcs(t *testing.T) {
  773. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  774. Description: "cc_library_static 2 multilib srcs and exclude_srcs",
  775. Filesystem: map[string]string{
  776. "common.c": "",
  777. "for-lib32.c": "",
  778. "for-lib64.c": "",
  779. "not-for-lib32.c": "",
  780. "not-for-lib64.c": "",
  781. },
  782. Blueprint: soongCcLibraryStaticPreamble + `
  783. cc_library_static {
  784. name: "foo_static",
  785. srcs: ["common.c", "not-for-*.c"],
  786. multilib: {
  787. lib32: { srcs: ["for-lib32.c"], exclude_srcs: ["not-for-lib32.c"] },
  788. lib64: { srcs: ["for-lib64.c"], exclude_srcs: ["not-for-lib64.c"] },
  789. },
  790. include_build_directory: false,
  791. } `,
  792. ExpectedBazelTargets: []string{
  793. MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{
  794. "srcs_c": `["common.c"] + select({
  795. "//build/bazel/platforms/arch:arm": [
  796. "not-for-lib64.c",
  797. "for-lib32.c",
  798. ],
  799. "//build/bazel/platforms/arch:arm64": [
  800. "not-for-lib32.c",
  801. "for-lib64.c",
  802. ],
  803. "//build/bazel/platforms/arch:riscv64": [
  804. "not-for-lib32.c",
  805. "for-lib64.c",
  806. ],
  807. "//build/bazel/platforms/arch:x86": [
  808. "not-for-lib64.c",
  809. "for-lib32.c",
  810. ],
  811. "//build/bazel/platforms/arch:x86_64": [
  812. "not-for-lib32.c",
  813. "for-lib64.c",
  814. ],
  815. "//conditions:default": [
  816. "not-for-lib32.c",
  817. "not-for-lib64.c",
  818. ],
  819. })`,
  820. }),
  821. },
  822. })
  823. }
  824. func TestCcLibrarySTaticArchMultilibSrcsExcludeSrcs(t *testing.T) {
  825. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  826. Description: "cc_library_static arch and multilib srcs and exclude_srcs",
  827. Filesystem: map[string]string{
  828. "common.c": "",
  829. "for-arm.c": "",
  830. "for-arm64.c": "",
  831. "for-x86.c": "",
  832. "for-x86_64.c": "",
  833. "for-lib32.c": "",
  834. "for-lib64.c": "",
  835. "not-for-arm.c": "",
  836. "not-for-arm64.c": "",
  837. "not-for-riscv64.c": "",
  838. "not-for-x86.c": "",
  839. "not-for-x86_64.c": "",
  840. "not-for-lib32.c": "",
  841. "not-for-lib64.c": "",
  842. "not-for-everything.c": "",
  843. },
  844. Blueprint: soongCcLibraryStaticPreamble + `
  845. cc_library_static {
  846. name: "foo_static",
  847. srcs: ["common.c", "not-for-*.c"],
  848. exclude_srcs: ["not-for-everything.c"],
  849. arch: {
  850. arm: { srcs: ["for-arm.c"], exclude_srcs: ["not-for-arm.c"] },
  851. arm64: { srcs: ["for-arm64.c"], exclude_srcs: ["not-for-arm64.c"] },
  852. riscv64: { srcs: ["for-riscv64.c"], exclude_srcs: ["not-for-riscv64.c"] },
  853. x86: { srcs: ["for-x86.c"], exclude_srcs: ["not-for-x86.c"] },
  854. x86_64: { srcs: ["for-x86_64.c"], exclude_srcs: ["not-for-x86_64.c"] },
  855. },
  856. multilib: {
  857. lib32: { srcs: ["for-lib32.c"], exclude_srcs: ["not-for-lib32.c"] },
  858. lib64: { srcs: ["for-lib64.c"], exclude_srcs: ["not-for-lib64.c"] },
  859. },
  860. include_build_directory: false,
  861. }`,
  862. ExpectedBazelTargets: []string{
  863. MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{
  864. "srcs_c": `["common.c"] + select({
  865. "//build/bazel/platforms/arch:arm": [
  866. "not-for-arm64.c",
  867. "not-for-lib64.c",
  868. "not-for-riscv64.c",
  869. "not-for-x86.c",
  870. "not-for-x86_64.c",
  871. "for-arm.c",
  872. "for-lib32.c",
  873. ],
  874. "//build/bazel/platforms/arch:arm64": [
  875. "not-for-arm.c",
  876. "not-for-lib32.c",
  877. "not-for-riscv64.c",
  878. "not-for-x86.c",
  879. "not-for-x86_64.c",
  880. "for-arm64.c",
  881. "for-lib64.c",
  882. ],
  883. "//build/bazel/platforms/arch:riscv64": [
  884. "not-for-arm.c",
  885. "not-for-arm64.c",
  886. "not-for-lib32.c",
  887. "not-for-x86.c",
  888. "not-for-x86_64.c",
  889. "for-riscv64.c",
  890. "for-lib64.c",
  891. ],
  892. "//build/bazel/platforms/arch:x86": [
  893. "not-for-arm.c",
  894. "not-for-arm64.c",
  895. "not-for-lib64.c",
  896. "not-for-riscv64.c",
  897. "not-for-x86_64.c",
  898. "for-x86.c",
  899. "for-lib32.c",
  900. ],
  901. "//build/bazel/platforms/arch:x86_64": [
  902. "not-for-arm.c",
  903. "not-for-arm64.c",
  904. "not-for-lib32.c",
  905. "not-for-riscv64.c",
  906. "not-for-x86.c",
  907. "for-x86_64.c",
  908. "for-lib64.c",
  909. ],
  910. "//conditions:default": [
  911. "not-for-arm.c",
  912. "not-for-arm64.c",
  913. "not-for-lib32.c",
  914. "not-for-lib64.c",
  915. "not-for-riscv64.c",
  916. "not-for-x86.c",
  917. "not-for-x86_64.c",
  918. ],
  919. })`,
  920. }),
  921. },
  922. })
  923. }
  924. func TestCcLibraryStaticGeneratedHeadersAllPartitions(t *testing.T) {
  925. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  926. Blueprint: soongCcLibraryStaticPreamble + `
  927. genrule {
  928. name: "generated_hdr",
  929. cmd: "nothing to see here",
  930. bazel_module: { bp2build_available: false },
  931. }
  932. genrule {
  933. name: "export_generated_hdr",
  934. cmd: "nothing to see here",
  935. bazel_module: { bp2build_available: false },
  936. }
  937. cc_library_static {
  938. name: "foo_static",
  939. srcs: ["cpp_src.cpp", "as_src.S", "c_src.c"],
  940. generated_headers: ["generated_hdr", "export_generated_hdr"],
  941. export_generated_headers: ["export_generated_hdr"],
  942. include_build_directory: false,
  943. }`,
  944. ExpectedBazelTargets: []string{
  945. MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{
  946. "export_includes": `["."]`,
  947. "local_includes": `["."]`,
  948. "hdrs": `[":export_generated_hdr"]`,
  949. "srcs": `[
  950. "cpp_src.cpp",
  951. ":generated_hdr",
  952. ]`,
  953. "srcs_as": `[
  954. "as_src.S",
  955. ":generated_hdr",
  956. ]`,
  957. "srcs_c": `[
  958. "c_src.c",
  959. ":generated_hdr",
  960. ]`,
  961. }),
  962. },
  963. })
  964. }
  965. // generated_headers has "variant_prepend" tag. In bp2build output,
  966. // variant info(select) should go before general info.
  967. func TestCcLibraryStaticArchSrcsExcludeSrcsGeneratedFiles(t *testing.T) {
  968. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  969. Description: "cc_library_static arch srcs/exclude_srcs with generated files",
  970. Filesystem: map[string]string{
  971. "common.cpp": "",
  972. "for-x86.cpp": "",
  973. "not-for-x86.cpp": "",
  974. "not-for-everything.cpp": "",
  975. "dep/Android.bp": simpleModuleDoNotConvertBp2build("genrule", "generated_src_other_pkg") +
  976. simpleModuleDoNotConvertBp2build("genrule", "generated_hdr_other_pkg") +
  977. simpleModuleDoNotConvertBp2build("genrule", "generated_src_other_pkg_x86") +
  978. simpleModuleDoNotConvertBp2build("genrule", "generated_hdr_other_pkg_x86") +
  979. simpleModuleDoNotConvertBp2build("genrule", "generated_hdr_other_pkg_android"),
  980. },
  981. Blueprint: soongCcLibraryStaticPreamble +
  982. simpleModuleDoNotConvertBp2build("genrule", "generated_src") +
  983. simpleModuleDoNotConvertBp2build("genrule", "generated_src_not_x86") +
  984. simpleModuleDoNotConvertBp2build("genrule", "generated_src_android") +
  985. simpleModuleDoNotConvertBp2build("genrule", "generated_hdr") + `
  986. cc_library_static {
  987. name: "foo_static",
  988. srcs: ["common.cpp", "not-for-*.cpp"],
  989. exclude_srcs: ["not-for-everything.cpp"],
  990. generated_sources: ["generated_src", "generated_src_other_pkg", "generated_src_not_x86"],
  991. generated_headers: ["generated_hdr", "generated_hdr_other_pkg"],
  992. export_generated_headers: ["generated_hdr_other_pkg"],
  993. arch: {
  994. x86: {
  995. srcs: ["for-x86.cpp"],
  996. exclude_srcs: ["not-for-x86.cpp"],
  997. generated_headers: ["generated_hdr_other_pkg_x86"],
  998. exclude_generated_sources: ["generated_src_not_x86"],
  999. export_generated_headers: ["generated_hdr_other_pkg_x86"],
  1000. },
  1001. },
  1002. target: {
  1003. android: {
  1004. generated_sources: ["generated_src_android"],
  1005. generated_headers: ["generated_hdr_other_pkg_android"],
  1006. export_generated_headers: ["generated_hdr_other_pkg_android"],
  1007. },
  1008. },
  1009. include_build_directory: false,
  1010. }
  1011. `,
  1012. ExpectedBazelTargets: []string{
  1013. MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{
  1014. "srcs": `[
  1015. "common.cpp",
  1016. ":generated_src",
  1017. "//dep:generated_src_other_pkg",
  1018. ":generated_hdr",
  1019. ] + select({
  1020. "//build/bazel/platforms/arch:x86": ["for-x86.cpp"],
  1021. "//conditions:default": [
  1022. "not-for-x86.cpp",
  1023. ":generated_src_not_x86",
  1024. ],
  1025. }) + select({
  1026. "//build/bazel/platforms/os:android": [":generated_src_android"],
  1027. "//conditions:default": [],
  1028. })`,
  1029. "hdrs": `select({
  1030. "//build/bazel/platforms/os:android": ["//dep:generated_hdr_other_pkg_android"],
  1031. "//conditions:default": [],
  1032. }) + select({
  1033. "//build/bazel/platforms/arch:x86": ["//dep:generated_hdr_other_pkg_x86"],
  1034. "//conditions:default": [],
  1035. }) + ["//dep:generated_hdr_other_pkg"]`,
  1036. "local_includes": `["."]`,
  1037. "export_absolute_includes": `["dep"]`,
  1038. }),
  1039. },
  1040. })
  1041. }
  1042. func TestCcLibraryStaticGetTargetProperties(t *testing.T) {
  1043. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  1044. Description: "cc_library_static complex GetTargetProperties",
  1045. Blueprint: soongCcLibraryStaticPreamble + `
  1046. cc_library_static {
  1047. name: "foo_static",
  1048. target: {
  1049. android: {
  1050. srcs: ["android_src.c"],
  1051. },
  1052. android_arm: {
  1053. srcs: ["android_arm_src.c"],
  1054. },
  1055. android_arm64: {
  1056. srcs: ["android_arm64_src.c"],
  1057. },
  1058. android_x86: {
  1059. srcs: ["android_x86_src.c"],
  1060. },
  1061. android_x86_64: {
  1062. srcs: ["android_x86_64_src.c"],
  1063. },
  1064. linux_bionic_arm64: {
  1065. srcs: ["linux_bionic_arm64_src.c"],
  1066. },
  1067. linux_bionic_x86_64: {
  1068. srcs: ["linux_bionic_x86_64_src.c"],
  1069. },
  1070. },
  1071. include_build_directory: false,
  1072. }`,
  1073. ExpectedBazelTargets: []string{
  1074. MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{
  1075. "srcs_c": `select({
  1076. "//build/bazel/platforms/os:android": ["android_src.c"],
  1077. "//conditions:default": [],
  1078. }) + select({
  1079. "//build/bazel/platforms/os_arch:android_arm": ["android_arm_src.c"],
  1080. "//build/bazel/platforms/os_arch:android_arm64": ["android_arm64_src.c"],
  1081. "//build/bazel/platforms/os_arch:android_x86": ["android_x86_src.c"],
  1082. "//build/bazel/platforms/os_arch:android_x86_64": ["android_x86_64_src.c"],
  1083. "//build/bazel/platforms/os_arch:linux_bionic_arm64": ["linux_bionic_arm64_src.c"],
  1084. "//build/bazel/platforms/os_arch:linux_bionic_x86_64": ["linux_bionic_x86_64_src.c"],
  1085. "//conditions:default": [],
  1086. })`,
  1087. }),
  1088. },
  1089. })
  1090. }
  1091. func TestCcLibraryStaticProductVariableSelects(t *testing.T) {
  1092. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  1093. Description: "cc_library_static product variable selects",
  1094. Blueprint: soongCcLibraryStaticPreamble + `
  1095. cc_library_static {
  1096. name: "foo_static",
  1097. srcs: ["common.c"],
  1098. product_variables: {
  1099. malloc_not_svelte: {
  1100. cflags: ["-Wmalloc_not_svelte"],
  1101. },
  1102. malloc_zero_contents: {
  1103. cflags: ["-Wmalloc_zero_contents"],
  1104. },
  1105. binder32bit: {
  1106. cflags: ["-Wbinder32bit"],
  1107. },
  1108. },
  1109. include_build_directory: false,
  1110. } `,
  1111. ExpectedBazelTargets: []string{
  1112. MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{
  1113. "copts": `select({
  1114. "//build/bazel/product_variables:binder32bit": ["-Wbinder32bit"],
  1115. "//conditions:default": [],
  1116. }) + select({
  1117. "//build/bazel/product_variables:malloc_not_svelte": ["-Wmalloc_not_svelte"],
  1118. "//conditions:default": [],
  1119. }) + select({
  1120. "//build/bazel/product_variables:malloc_zero_contents": ["-Wmalloc_zero_contents"],
  1121. "//conditions:default": [],
  1122. })`,
  1123. "srcs_c": `["common.c"]`,
  1124. }),
  1125. },
  1126. })
  1127. }
  1128. func TestCcLibraryStaticProductVariableArchSpecificSelects(t *testing.T) {
  1129. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  1130. Description: "cc_library_static arch-specific product variable selects",
  1131. Filesystem: map[string]string{},
  1132. Blueprint: soongCcLibraryStaticPreamble + `
  1133. cc_library_static {
  1134. name: "foo_static",
  1135. srcs: ["common.c"],
  1136. product_variables: {
  1137. malloc_not_svelte: {
  1138. cflags: ["-Wmalloc_not_svelte"],
  1139. },
  1140. },
  1141. arch: {
  1142. arm64: {
  1143. product_variables: {
  1144. malloc_not_svelte: {
  1145. cflags: ["-Warm64_malloc_not_svelte"],
  1146. },
  1147. },
  1148. },
  1149. },
  1150. multilib: {
  1151. lib32: {
  1152. product_variables: {
  1153. malloc_not_svelte: {
  1154. cflags: ["-Wlib32_malloc_not_svelte"],
  1155. },
  1156. },
  1157. },
  1158. },
  1159. target: {
  1160. android: {
  1161. product_variables: {
  1162. malloc_not_svelte: {
  1163. cflags: ["-Wandroid_malloc_not_svelte"],
  1164. },
  1165. },
  1166. }
  1167. },
  1168. include_build_directory: false,
  1169. } `,
  1170. ExpectedBazelTargets: []string{
  1171. MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{
  1172. "copts": `select({
  1173. "//build/bazel/product_variables:malloc_not_svelte": ["-Wmalloc_not_svelte"],
  1174. "//conditions:default": [],
  1175. }) + select({
  1176. "//build/bazel/product_variables:malloc_not_svelte-android": ["-Wandroid_malloc_not_svelte"],
  1177. "//conditions:default": [],
  1178. }) + select({
  1179. "//build/bazel/product_variables:malloc_not_svelte-arm": ["-Wlib32_malloc_not_svelte"],
  1180. "//conditions:default": [],
  1181. }) + select({
  1182. "//build/bazel/product_variables:malloc_not_svelte-arm64": ["-Warm64_malloc_not_svelte"],
  1183. "//conditions:default": [],
  1184. }) + select({
  1185. "//build/bazel/product_variables:malloc_not_svelte-x86": ["-Wlib32_malloc_not_svelte"],
  1186. "//conditions:default": [],
  1187. })`,
  1188. "srcs_c": `["common.c"]`,
  1189. }),
  1190. },
  1191. })
  1192. }
  1193. func TestCcLibraryStaticProductVariableStringReplacement(t *testing.T) {
  1194. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  1195. Description: "cc_library_static product variable string replacement",
  1196. Filesystem: map[string]string{},
  1197. Blueprint: soongCcLibraryStaticPreamble + `
  1198. cc_library_static {
  1199. name: "foo_static",
  1200. srcs: ["common.S"],
  1201. product_variables: {
  1202. platform_sdk_version: {
  1203. asflags: ["-DPLATFORM_SDK_VERSION=%d"],
  1204. },
  1205. },
  1206. include_build_directory: false,
  1207. } `,
  1208. ExpectedBazelTargets: []string{
  1209. MakeBazelTarget("cc_library_static", "foo_static", AttrNameToString{
  1210. "asflags": `select({
  1211. "//build/bazel/product_variables:platform_sdk_version": ["-DPLATFORM_SDK_VERSION=$(Platform_sdk_version)"],
  1212. "//conditions:default": [],
  1213. })`,
  1214. "srcs_as": `["common.S"]`,
  1215. }),
  1216. },
  1217. })
  1218. }
  1219. func TestStaticLibrary_SystemSharedLibsRootEmpty(t *testing.T) {
  1220. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  1221. Description: "cc_library_static system_shared_lib empty root",
  1222. Blueprint: soongCcLibraryStaticPreamble + `
  1223. cc_library_static {
  1224. name: "root_empty",
  1225. system_shared_libs: [],
  1226. include_build_directory: false,
  1227. }
  1228. `,
  1229. ExpectedBazelTargets: []string{
  1230. MakeBazelTarget("cc_library_static", "root_empty", AttrNameToString{
  1231. "system_dynamic_deps": `[]`,
  1232. }),
  1233. },
  1234. })
  1235. }
  1236. func TestStaticLibrary_SystemSharedLibsStaticEmpty(t *testing.T) {
  1237. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  1238. Description: "cc_library_static system_shared_lib empty static default",
  1239. Blueprint: soongCcLibraryStaticPreamble + `
  1240. cc_defaults {
  1241. name: "static_empty_defaults",
  1242. static: {
  1243. system_shared_libs: [],
  1244. },
  1245. include_build_directory: false,
  1246. }
  1247. cc_library_static {
  1248. name: "static_empty",
  1249. defaults: ["static_empty_defaults"],
  1250. }
  1251. `,
  1252. ExpectedBazelTargets: []string{
  1253. MakeBazelTarget("cc_library_static", "static_empty", AttrNameToString{
  1254. "system_dynamic_deps": `[]`,
  1255. }),
  1256. },
  1257. })
  1258. }
  1259. func TestStaticLibrary_SystemSharedLibsBionicEmpty(t *testing.T) {
  1260. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  1261. Description: "cc_library_static system_shared_lib empty for bionic variant",
  1262. Blueprint: soongCcLibraryStaticPreamble + `
  1263. cc_library {
  1264. name: "libc_musl",
  1265. bazel_module: { bp2build_available: false },
  1266. }
  1267. cc_library_static {
  1268. name: "target_bionic_empty",
  1269. target: {
  1270. bionic: {
  1271. system_shared_libs: [],
  1272. },
  1273. },
  1274. include_build_directory: false,
  1275. }
  1276. `,
  1277. ExpectedBazelTargets: []string{
  1278. MakeBazelTarget("cc_library_static", "target_bionic_empty", AttrNameToString{
  1279. "system_dynamic_deps": `select({
  1280. "//build/bazel/platforms/os:linux_musl": [":libc_musl"],
  1281. "//conditions:default": [],
  1282. })`,
  1283. }),
  1284. },
  1285. })
  1286. }
  1287. func TestStaticLibrary_SystemSharedLibsLinuxBionicEmpty(t *testing.T) {
  1288. // Note that this behavior is technically incorrect (it's a simplification).
  1289. // The correct behavior would be if bp2build wrote `system_dynamic_deps = []`
  1290. // only for linux_bionic, but `android` had `["libc", "libdl", "libm"].
  1291. // b/195791252 tracks the fix.
  1292. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  1293. Description: "cc_library_static system_shared_lib empty for linux_bionic variant",
  1294. Blueprint: soongCcLibraryStaticPreamble + `
  1295. cc_library {
  1296. name: "libc_musl",
  1297. bazel_module: { bp2build_available: false },
  1298. }
  1299. cc_library_static {
  1300. name: "target_linux_bionic_empty",
  1301. target: {
  1302. linux_bionic: {
  1303. system_shared_libs: [],
  1304. },
  1305. },
  1306. include_build_directory: false,
  1307. }
  1308. `,
  1309. ExpectedBazelTargets: []string{
  1310. MakeBazelTarget("cc_library_static", "target_linux_bionic_empty", AttrNameToString{
  1311. "system_dynamic_deps": `select({
  1312. "//build/bazel/platforms/os:linux_musl": [":libc_musl"],
  1313. "//conditions:default": [],
  1314. })`,
  1315. }),
  1316. },
  1317. })
  1318. }
  1319. func TestStaticLibrary_SystemSharedLibsMuslEmpty(t *testing.T) {
  1320. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  1321. Description: "cc_library_static system_shared_lib empty for musl variant",
  1322. Blueprint: soongCcLibraryStaticPreamble + `
  1323. cc_library {
  1324. name: "libc_musl",
  1325. bazel_module: { bp2build_available: false },
  1326. }
  1327. cc_library_static {
  1328. name: "target_musl_empty",
  1329. target: {
  1330. musl: {
  1331. system_shared_libs: [],
  1332. },
  1333. },
  1334. include_build_directory: false,
  1335. }
  1336. `,
  1337. ExpectedBazelTargets: []string{
  1338. MakeBazelTarget("cc_library_static", "target_musl_empty", AttrNameToString{
  1339. "system_dynamic_deps": `[]`,
  1340. }),
  1341. },
  1342. })
  1343. }
  1344. func TestStaticLibrary_SystemSharedLibsLinuxMuslEmpty(t *testing.T) {
  1345. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  1346. Description: "cc_library_static system_shared_lib empty for linux_musl variant",
  1347. Blueprint: soongCcLibraryStaticPreamble + `
  1348. cc_library {
  1349. name: "libc_musl",
  1350. bazel_module: { bp2build_available: false },
  1351. }
  1352. cc_library_static {
  1353. name: "target_linux_musl_empty",
  1354. target: {
  1355. linux_musl: {
  1356. system_shared_libs: [],
  1357. },
  1358. },
  1359. include_build_directory: false,
  1360. }
  1361. `,
  1362. ExpectedBazelTargets: []string{
  1363. MakeBazelTarget("cc_library_static", "target_linux_musl_empty", AttrNameToString{
  1364. "system_dynamic_deps": `[]`,
  1365. }),
  1366. },
  1367. })
  1368. }
  1369. func TestStaticLibrary_SystemSharedLibsBionic(t *testing.T) {
  1370. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  1371. Description: "cc_library_static system_shared_libs set for bionic variant",
  1372. Blueprint: soongCcLibraryStaticPreamble +
  1373. simpleModuleDoNotConvertBp2build("cc_library", "libc") + `
  1374. cc_library {
  1375. name: "libc_musl",
  1376. bazel_module: { bp2build_available: false },
  1377. }
  1378. cc_library_static {
  1379. name: "target_bionic",
  1380. target: {
  1381. bionic: {
  1382. system_shared_libs: ["libc"],
  1383. },
  1384. },
  1385. include_build_directory: false,
  1386. }
  1387. `,
  1388. ExpectedBazelTargets: []string{
  1389. MakeBazelTarget("cc_library_static", "target_bionic", AttrNameToString{
  1390. "system_dynamic_deps": `select({
  1391. "//build/bazel/platforms/os:android": [":libc"],
  1392. "//build/bazel/platforms/os:linux_bionic": [":libc"],
  1393. "//build/bazel/platforms/os:linux_musl": [":libc_musl"],
  1394. "//conditions:default": [],
  1395. })`,
  1396. }),
  1397. },
  1398. })
  1399. }
  1400. func TestStaticLibrary_SystemSharedLibsLinuxRootAndLinuxBionic(t *testing.T) {
  1401. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  1402. Description: "cc_library_static system_shared_libs set for root and linux_bionic variant",
  1403. Blueprint: soongCcLibraryStaticPreamble +
  1404. simpleModuleDoNotConvertBp2build("cc_library", "libc") +
  1405. simpleModuleDoNotConvertBp2build("cc_library", "libm") + `
  1406. cc_library {
  1407. name: "libc_musl",
  1408. bazel_module: { bp2build_available: false },
  1409. }
  1410. cc_library_static {
  1411. name: "target_linux_bionic",
  1412. system_shared_libs: ["libc"],
  1413. target: {
  1414. linux_bionic: {
  1415. system_shared_libs: ["libm"],
  1416. },
  1417. },
  1418. include_build_directory: false,
  1419. }
  1420. `,
  1421. ExpectedBazelTargets: []string{
  1422. MakeBazelTarget("cc_library_static", "target_linux_bionic", AttrNameToString{
  1423. "system_dynamic_deps": `[":libc"] + select({
  1424. "//build/bazel/platforms/os:linux_bionic": [":libm"],
  1425. "//build/bazel/platforms/os:linux_musl": [":libc_musl"],
  1426. "//conditions:default": [],
  1427. })`,
  1428. }),
  1429. },
  1430. })
  1431. }
  1432. func TestCcLibrarystatic_SystemSharedLibUsedAsDep(t *testing.T) {
  1433. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  1434. Description: "cc_library_static system_shared_lib empty for linux_bionic variant",
  1435. Blueprint: soongCcLibraryStaticPreamble +
  1436. simpleModuleDoNotConvertBp2build("cc_library", "libc") + `
  1437. cc_library {
  1438. name: "libm",
  1439. stubs: {
  1440. symbol_file: "libm.map.txt",
  1441. versions: ["current"],
  1442. },
  1443. bazel_module: { bp2build_available: false },
  1444. apex_available: ["com.android.runtime"],
  1445. }
  1446. cc_library_static {
  1447. name: "used_in_bionic_oses",
  1448. target: {
  1449. android: {
  1450. shared_libs: ["libc"],
  1451. },
  1452. linux_bionic: {
  1453. shared_libs: ["libc"],
  1454. },
  1455. },
  1456. include_build_directory: false,
  1457. apex_available: ["foo"],
  1458. }
  1459. cc_library_static {
  1460. name: "all",
  1461. shared_libs: ["libc"],
  1462. include_build_directory: false,
  1463. apex_available: ["foo"],
  1464. }
  1465. cc_library_static {
  1466. name: "keep_for_empty_system_shared_libs",
  1467. shared_libs: ["libc"],
  1468. system_shared_libs: [],
  1469. include_build_directory: false,
  1470. apex_available: ["foo"],
  1471. }
  1472. cc_library_static {
  1473. name: "used_with_stubs",
  1474. shared_libs: ["libm"],
  1475. include_build_directory: false,
  1476. apex_available: ["foo"],
  1477. }
  1478. cc_library_static {
  1479. name: "keep_with_stubs",
  1480. shared_libs: ["libm"],
  1481. system_shared_libs: [],
  1482. include_build_directory: false,
  1483. apex_available: ["foo"],
  1484. }
  1485. `,
  1486. ExpectedBazelTargets: []string{
  1487. MakeBazelTarget("cc_library_static", "all", AttrNameToString{
  1488. "tags": `["apex_available=foo"]`,
  1489. }),
  1490. MakeBazelTarget("cc_library_static", "keep_for_empty_system_shared_libs", AttrNameToString{
  1491. "implementation_dynamic_deps": `[":libc"]`,
  1492. "system_dynamic_deps": `[]`,
  1493. "tags": `["apex_available=foo"]`,
  1494. }),
  1495. MakeBazelTarget("cc_library_static", "keep_with_stubs", AttrNameToString{
  1496. "implementation_dynamic_deps": `select({
  1497. "//build/bazel/rules/apex:foo": ["@api_surfaces//module-libapi/current:libm"],
  1498. "//build/bazel/rules/apex:system": ["@api_surfaces//module-libapi/current:libm"],
  1499. "//conditions:default": [":libm"],
  1500. })`,
  1501. "system_dynamic_deps": `[]`,
  1502. "tags": `["apex_available=foo"]`,
  1503. }),
  1504. MakeBazelTarget("cc_library_static", "used_in_bionic_oses", AttrNameToString{
  1505. "tags": `["apex_available=foo"]`,
  1506. }),
  1507. MakeBazelTarget("cc_library_static", "used_with_stubs", AttrNameToString{
  1508. "tags": `["apex_available=foo"]`,
  1509. }),
  1510. },
  1511. })
  1512. }
  1513. func TestCcLibraryStaticProto(t *testing.T) {
  1514. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  1515. Blueprint: soongCcProtoPreamble + `cc_library_static {
  1516. name: "foo",
  1517. srcs: ["foo.proto"],
  1518. proto: {
  1519. export_proto_headers: true,
  1520. },
  1521. include_build_directory: false,
  1522. }`,
  1523. ExpectedBazelTargets: []string{
  1524. MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
  1525. "srcs": `["foo.proto"]`,
  1526. }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
  1527. "deps": `[":foo_proto"]`,
  1528. }), MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
  1529. "deps": `[":libprotobuf-cpp-lite"]`,
  1530. "whole_archive_deps": `[":foo_cc_proto_lite"]`,
  1531. }),
  1532. },
  1533. })
  1534. }
  1535. func TestCcLibraryStaticUseVersionLib(t *testing.T) {
  1536. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  1537. Filesystem: map[string]string{
  1538. soongCcVersionLibBpPath: soongCcVersionLibBp,
  1539. },
  1540. Blueprint: soongCcProtoPreamble + `cc_library_static {
  1541. name: "foo",
  1542. use_version_lib: true,
  1543. static_libs: ["libbuildversion"],
  1544. include_build_directory: false,
  1545. }`,
  1546. ExpectedBazelTargets: []string{
  1547. MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
  1548. "whole_archive_deps": `["//build/soong/cc/libbuildversion:libbuildversion"]`,
  1549. }),
  1550. },
  1551. })
  1552. }
  1553. func TestCcLibraryStaticUseVersionLibHasDep(t *testing.T) {
  1554. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  1555. Filesystem: map[string]string{
  1556. soongCcVersionLibBpPath: soongCcVersionLibBp,
  1557. },
  1558. Blueprint: soongCcProtoPreamble + `cc_library_static {
  1559. name: "foo",
  1560. use_version_lib: true,
  1561. whole_static_libs: ["libbuildversion"],
  1562. include_build_directory: false,
  1563. }`,
  1564. ExpectedBazelTargets: []string{
  1565. MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
  1566. "whole_archive_deps": `["//build/soong/cc/libbuildversion:libbuildversion"]`,
  1567. }),
  1568. },
  1569. })
  1570. }
  1571. func TestCcLibraryStaticStdInFlags(t *testing.T) {
  1572. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  1573. Blueprint: soongCcProtoPreamble + `cc_library_static {
  1574. name: "foo",
  1575. cflags: ["-std=candcpp"],
  1576. conlyflags: ["-std=conly"],
  1577. cppflags: ["-std=cpp"],
  1578. include_build_directory: false,
  1579. }`,
  1580. ExpectedBazelTargets: []string{
  1581. MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
  1582. "conlyflags": `["-std=conly"]`,
  1583. "cppflags": `["-std=cpp"]`,
  1584. }),
  1585. },
  1586. })
  1587. }
  1588. func TestCcLibraryStaticStl(t *testing.T) {
  1589. testCases := []struct {
  1590. desc string
  1591. prop string
  1592. attr AttrNameToString
  1593. }{
  1594. {
  1595. desc: "c++_shared deduped to libc++",
  1596. prop: `stl: "c++_shared",`,
  1597. attr: AttrNameToString{
  1598. "stl": `"libc++"`,
  1599. },
  1600. },
  1601. {
  1602. desc: "libc++ to libc++",
  1603. prop: `stl: "libc++",`,
  1604. attr: AttrNameToString{
  1605. "stl": `"libc++"`,
  1606. },
  1607. },
  1608. {
  1609. desc: "c++_static to libc++_static",
  1610. prop: `stl: "c++_static",`,
  1611. attr: AttrNameToString{
  1612. "stl": `"libc++_static"`,
  1613. },
  1614. },
  1615. {
  1616. desc: "libc++_static to libc++_static",
  1617. prop: `stl: "libc++_static",`,
  1618. attr: AttrNameToString{
  1619. "stl": `"libc++_static"`,
  1620. },
  1621. },
  1622. {
  1623. desc: "system to system",
  1624. prop: `stl: "system",`,
  1625. attr: AttrNameToString{
  1626. "stl": `"system"`,
  1627. },
  1628. },
  1629. {
  1630. desc: "none to none",
  1631. prop: `stl: "none",`,
  1632. attr: AttrNameToString{
  1633. "stl": `"none"`,
  1634. },
  1635. },
  1636. {
  1637. desc: "empty to empty",
  1638. attr: AttrNameToString{},
  1639. },
  1640. }
  1641. for _, tc := range testCases {
  1642. t.Run(tc.desc, func(*testing.T) {
  1643. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  1644. Blueprint: fmt.Sprintf(`cc_library_static {
  1645. name: "foo",
  1646. include_build_directory: false,
  1647. %s
  1648. }`, tc.prop),
  1649. ExpectedBazelTargets: []string{
  1650. MakeBazelTarget("cc_library_static", "foo", tc.attr),
  1651. },
  1652. })
  1653. })
  1654. }
  1655. }
  1656. func TestCCLibraryStaticRuntimeDeps(t *testing.T) {
  1657. runCcLibrarySharedTestCase(t, Bp2buildTestCase{
  1658. Blueprint: `cc_library_shared {
  1659. name: "bar",
  1660. }
  1661. cc_library_static {
  1662. name: "foo",
  1663. runtime_libs: ["foo"],
  1664. }`,
  1665. ExpectedBazelTargets: []string{
  1666. MakeBazelTarget("cc_library_shared", "bar", AttrNameToString{
  1667. "local_includes": `["."]`,
  1668. }),
  1669. MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
  1670. "runtime_deps": `[":foo"]`,
  1671. "local_includes": `["."]`,
  1672. }),
  1673. },
  1674. })
  1675. }
  1676. func TestCcLibraryStaticWithSyspropSrcs(t *testing.T) {
  1677. runCcLibraryTestCase(t, Bp2buildTestCase{
  1678. Description: "cc_library_static with sysprop sources",
  1679. Blueprint: `
  1680. cc_library_static {
  1681. name: "foo",
  1682. srcs: [
  1683. "bar.sysprop",
  1684. "baz.sysprop",
  1685. "blah.cpp",
  1686. ],
  1687. min_sdk_version: "5",
  1688. }`,
  1689. ExpectedBazelTargets: []string{
  1690. MakeBazelTarget("sysprop_library", "foo_sysprop_library", AttrNameToString{
  1691. "srcs": `[
  1692. "bar.sysprop",
  1693. "baz.sysprop",
  1694. ]`,
  1695. }),
  1696. MakeBazelTarget("cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
  1697. "dep": `":foo_sysprop_library"`,
  1698. "min_sdk_version": `"5"`,
  1699. }),
  1700. MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
  1701. "srcs": `["blah.cpp"]`,
  1702. "local_includes": `["."]`,
  1703. "min_sdk_version": `"5"`,
  1704. "whole_archive_deps": `[":foo_cc_sysprop_library_static"]`,
  1705. }),
  1706. },
  1707. })
  1708. }
  1709. func TestCcLibraryStaticWithSyspropSrcsSomeConfigs(t *testing.T) {
  1710. runCcLibraryTestCase(t, Bp2buildTestCase{
  1711. Description: "cc_library_static with sysprop sources in some configs but not others",
  1712. Blueprint: `
  1713. cc_library_static {
  1714. name: "foo",
  1715. srcs: [
  1716. "blah.cpp",
  1717. ],
  1718. target: {
  1719. android: {
  1720. srcs: ["bar.sysprop"],
  1721. },
  1722. },
  1723. min_sdk_version: "5",
  1724. }`,
  1725. ExpectedBazelTargets: []string{
  1726. MakeBazelTarget("sysprop_library", "foo_sysprop_library", AttrNameToString{
  1727. "srcs": `select({
  1728. "//build/bazel/platforms/os:android": ["bar.sysprop"],
  1729. "//conditions:default": [],
  1730. })`,
  1731. }),
  1732. MakeBazelTarget("cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
  1733. "dep": `":foo_sysprop_library"`,
  1734. "min_sdk_version": `"5"`,
  1735. }),
  1736. MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
  1737. "srcs": `["blah.cpp"]`,
  1738. "local_includes": `["."]`,
  1739. "min_sdk_version": `"5"`,
  1740. "whole_archive_deps": `select({
  1741. "//build/bazel/platforms/os:android": [":foo_cc_sysprop_library_static"],
  1742. "//conditions:default": [],
  1743. })`,
  1744. }),
  1745. },
  1746. })
  1747. }
  1748. func TestCcLibraryStaticWithIntegerOverflowProperty(t *testing.T) {
  1749. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  1750. Description: "cc_library_static has correct features when integer_overflow property is provided",
  1751. Blueprint: `
  1752. cc_library_static {
  1753. name: "foo",
  1754. sanitize: {
  1755. integer_overflow: true,
  1756. },
  1757. }
  1758. `,
  1759. ExpectedBazelTargets: []string{
  1760. MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
  1761. "features": `["ubsan_integer_overflow"]`,
  1762. "local_includes": `["."]`,
  1763. }),
  1764. },
  1765. })
  1766. }
  1767. func TestCcLibraryStaticWithMiscUndefinedProperty(t *testing.T) {
  1768. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  1769. Description: "cc_library_static has correct features when misc_undefined property is provided",
  1770. Blueprint: `
  1771. cc_library_static {
  1772. name: "foo",
  1773. sanitize: {
  1774. misc_undefined: ["undefined", "nullability"],
  1775. },
  1776. }
  1777. `,
  1778. ExpectedBazelTargets: []string{
  1779. MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
  1780. "features": `[
  1781. "ubsan_undefined",
  1782. "ubsan_nullability",
  1783. ]`,
  1784. "local_includes": `["."]`,
  1785. }),
  1786. },
  1787. })
  1788. }
  1789. func TestCcLibraryStaticWithUBSanPropertiesArchSpecific(t *testing.T) {
  1790. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  1791. Description: "cc_library_static has correct feature select when UBSan props are specified in arch specific blocks",
  1792. Blueprint: `
  1793. cc_library_static {
  1794. name: "foo",
  1795. sanitize: {
  1796. misc_undefined: ["undefined", "nullability"],
  1797. },
  1798. target: {
  1799. android: {
  1800. sanitize: {
  1801. misc_undefined: ["alignment"],
  1802. },
  1803. },
  1804. linux_glibc: {
  1805. sanitize: {
  1806. integer_overflow: true,
  1807. },
  1808. },
  1809. },
  1810. }
  1811. `,
  1812. ExpectedBazelTargets: []string{
  1813. MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
  1814. "features": `[
  1815. "ubsan_undefined",
  1816. "ubsan_nullability",
  1817. ] + select({
  1818. "//build/bazel/platforms/os:android": ["ubsan_alignment"],
  1819. "//build/bazel/platforms/os:linux_glibc": ["ubsan_integer_overflow"],
  1820. "//conditions:default": [],
  1821. })`,
  1822. "local_includes": `["."]`,
  1823. }),
  1824. },
  1825. })
  1826. }
  1827. func TestCcLibraryStaticWithSanitizerBlocklist(t *testing.T) {
  1828. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  1829. Description: "cc_library_static has correct features when sanitize.blocklist is provided",
  1830. Blueprint: `
  1831. cc_library_static {
  1832. name: "foo",
  1833. sanitize: {
  1834. blocklist: "foo_blocklist.txt",
  1835. },
  1836. }
  1837. `,
  1838. ExpectedBazelTargets: []string{
  1839. MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
  1840. "features": `["ubsan_blocklist_foo_blocklist_txt"]`,
  1841. "local_includes": `["."]`,
  1842. }),
  1843. },
  1844. })
  1845. }
  1846. func TestCcLibraryStaticWithThinLto(t *testing.T) {
  1847. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  1848. Description: "cc_library_static has correct features when thin lto is enabled",
  1849. Blueprint: `
  1850. cc_library_static {
  1851. name: "foo",
  1852. lto: {
  1853. thin: true,
  1854. },
  1855. }
  1856. `,
  1857. ExpectedBazelTargets: []string{
  1858. MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
  1859. "features": `["android_thin_lto"]`,
  1860. "local_includes": `["."]`,
  1861. }),
  1862. },
  1863. })
  1864. }
  1865. func TestCcLibraryStaticWithLtoNever(t *testing.T) {
  1866. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  1867. Description: "cc_library_static has correct features when thin lto is enabled",
  1868. Blueprint: `
  1869. cc_library_static {
  1870. name: "foo",
  1871. lto: {
  1872. never: true,
  1873. },
  1874. }
  1875. `,
  1876. ExpectedBazelTargets: []string{
  1877. MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
  1878. "features": `["-android_thin_lto"]`,
  1879. "local_includes": `["."]`,
  1880. }),
  1881. },
  1882. })
  1883. }
  1884. func TestCcLibraryStaticWithThinLtoArchSpecific(t *testing.T) {
  1885. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  1886. Description: "cc_library_static has correct features when LTO differs across arch and os variants",
  1887. Blueprint: `
  1888. cc_library_static {
  1889. name: "foo",
  1890. target: {
  1891. android: {
  1892. lto: {
  1893. thin: true,
  1894. },
  1895. },
  1896. },
  1897. arch: {
  1898. riscv64: {
  1899. lto: {
  1900. thin: false,
  1901. },
  1902. },
  1903. },
  1904. }`,
  1905. ExpectedBazelTargets: []string{
  1906. MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
  1907. "local_includes": `["."]`,
  1908. "features": `select({
  1909. "//build/bazel/platforms/os_arch:android_arm": ["android_thin_lto"],
  1910. "//build/bazel/platforms/os_arch:android_arm64": ["android_thin_lto"],
  1911. "//build/bazel/platforms/os_arch:android_riscv64": ["-android_thin_lto"],
  1912. "//build/bazel/platforms/os_arch:android_x86": ["android_thin_lto"],
  1913. "//build/bazel/platforms/os_arch:android_x86_64": ["android_thin_lto"],
  1914. "//conditions:default": [],
  1915. })`}),
  1916. },
  1917. })
  1918. }
  1919. func TestCcLibraryStaticWithThinLtoDisabledDefaultEnabledVariant(t *testing.T) {
  1920. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  1921. Description: "cc_library_static has correct features when LTO disabled by default but enabled on a particular variant",
  1922. Blueprint: `
  1923. cc_library_static {
  1924. name: "foo",
  1925. lto: {
  1926. never: true,
  1927. },
  1928. target: {
  1929. android: {
  1930. lto: {
  1931. thin: true,
  1932. never: false,
  1933. },
  1934. },
  1935. },
  1936. }`,
  1937. ExpectedBazelTargets: []string{
  1938. MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
  1939. "local_includes": `["."]`,
  1940. "features": `select({
  1941. "//build/bazel/platforms/os:android": ["android_thin_lto"],
  1942. "//conditions:default": ["-android_thin_lto"],
  1943. })`,
  1944. }),
  1945. },
  1946. })
  1947. }
  1948. func TestCcLibraryStaticWithThinLtoAndWholeProgramVtables(t *testing.T) {
  1949. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  1950. Description: "cc_library_static has correct features when thin lto is enabled with whole_program_vtables",
  1951. Blueprint: `
  1952. cc_library_static {
  1953. name: "foo",
  1954. lto: {
  1955. thin: true,
  1956. },
  1957. whole_program_vtables: true,
  1958. }
  1959. `,
  1960. ExpectedBazelTargets: []string{
  1961. MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
  1962. "features": `[
  1963. "android_thin_lto",
  1964. "android_thin_lto_whole_program_vtables",
  1965. ]`,
  1966. "local_includes": `["."]`,
  1967. }),
  1968. },
  1969. })
  1970. }
  1971. func TestCcLibraryStaticHiddenVisibilityConvertedToFeature(t *testing.T) {
  1972. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  1973. Description: "cc_library_static changes hidden visibility flag to feature",
  1974. Blueprint: `
  1975. cc_library_static {
  1976. name: "foo",
  1977. cflags: ["-fvisibility=hidden"],
  1978. }`,
  1979. ExpectedBazelTargets: []string{
  1980. MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
  1981. "features": `["visibility_hidden"]`,
  1982. "local_includes": `["."]`,
  1983. }),
  1984. },
  1985. })
  1986. }
  1987. func TestCcLibraryStaticHiddenVisibilityConvertedToFeatureOsSpecific(t *testing.T) {
  1988. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  1989. Description: "cc_library_static changes hidden visibility flag to feature for specific os",
  1990. Blueprint: `
  1991. cc_library_static {
  1992. name: "foo",
  1993. target: {
  1994. android: {
  1995. cflags: ["-fvisibility=hidden"],
  1996. },
  1997. },
  1998. }`,
  1999. ExpectedBazelTargets: []string{
  2000. MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
  2001. "features": `select({
  2002. "//build/bazel/platforms/os:android": ["visibility_hidden"],
  2003. "//conditions:default": [],
  2004. })`,
  2005. "local_includes": `["."]`,
  2006. }),
  2007. },
  2008. })
  2009. }
  2010. func TestCcLibraryStaticWithCfi(t *testing.T) {
  2011. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  2012. Description: "cc_library_static has correct features when cfi is enabled",
  2013. Blueprint: `
  2014. cc_library_static {
  2015. name: "foo",
  2016. sanitize: {
  2017. cfi: true,
  2018. },
  2019. }`,
  2020. ExpectedBazelTargets: []string{
  2021. MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
  2022. "features": `["android_cfi"]`,
  2023. "local_includes": `["."]`,
  2024. }),
  2025. },
  2026. })
  2027. }
  2028. func TestCcLibraryStaticWithCfiOsSpecific(t *testing.T) {
  2029. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  2030. Description: "cc_library_static has correct features when cfi is enabled for specific variants",
  2031. Blueprint: `
  2032. cc_library_static {
  2033. name: "foo",
  2034. target: {
  2035. android: {
  2036. sanitize: {
  2037. cfi: true,
  2038. },
  2039. },
  2040. },
  2041. }`,
  2042. ExpectedBazelTargets: []string{
  2043. MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
  2044. "features": `select({
  2045. "//build/bazel/platforms/os:android": ["android_cfi"],
  2046. "//conditions:default": [],
  2047. })`,
  2048. "local_includes": `["."]`,
  2049. }),
  2050. },
  2051. })
  2052. }
  2053. func TestCcLibraryStaticWithCfiAndCfiAssemblySupport(t *testing.T) {
  2054. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  2055. Description: "cc_library_static has correct features when cfi is enabled with cfi_assembly_support",
  2056. Blueprint: `
  2057. cc_library_static {
  2058. name: "foo",
  2059. sanitize: {
  2060. cfi: true,
  2061. config: {
  2062. cfi_assembly_support: true,
  2063. },
  2064. },
  2065. }`,
  2066. ExpectedBazelTargets: []string{
  2067. MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
  2068. "features": `[
  2069. "android_cfi",
  2070. "android_cfi_assembly_support",
  2071. ]`,
  2072. "local_includes": `["."]`,
  2073. }),
  2074. },
  2075. })
  2076. }
  2077. func TestCcLibraryStaticExplicitlyDisablesCfiWhenFalse(t *testing.T) {
  2078. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  2079. Description: "cc_library_static disables cfi when explciitly set to false in the bp",
  2080. Blueprint: `
  2081. cc_library_static {
  2082. name: "foo",
  2083. sanitize: {
  2084. cfi: false,
  2085. },
  2086. }
  2087. `,
  2088. ExpectedBazelTargets: []string{
  2089. MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
  2090. "features": `["-android_cfi"]`,
  2091. "local_includes": `["."]`,
  2092. }),
  2093. },
  2094. })
  2095. }
  2096. func TestCCLibraryStaticRscriptSrc(t *testing.T) {
  2097. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  2098. Description: `cc_library_static with rscript files in sources`,
  2099. Blueprint: `
  2100. cc_library_static{
  2101. name : "foo",
  2102. srcs : [
  2103. "ccSrc.cc",
  2104. "rsSrc.rscript",
  2105. ],
  2106. include_build_directory: false,
  2107. }
  2108. `,
  2109. ExpectedBazelTargets: []string{
  2110. MakeBazelTarget("rscript_to_cpp", "foo_renderscript", AttrNameToString{
  2111. "srcs": `["rsSrc.rscript"]`,
  2112. }),
  2113. MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
  2114. "absolute_includes": `[
  2115. "frameworks/rs",
  2116. "frameworks/rs/cpp",
  2117. ]`,
  2118. "local_includes": `["."]`,
  2119. "srcs": `[
  2120. "ccSrc.cc",
  2121. "foo_renderscript",
  2122. ]`,
  2123. })}})
  2124. }