cc_library_conversion_test.go 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228
  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. )
  21. const (
  22. // See cc/testing.go for more context
  23. soongCcLibraryPreamble = `
  24. cc_defaults {
  25. name: "linux_bionic_supported",
  26. }
  27. `
  28. soongCcVersionLibBpPath = "build/soong/cc/libbuildversion/Android.bp"
  29. soongCcVersionLibBp = `
  30. cc_library_static {
  31. name: "libbuildversion",
  32. bazel_module: { bp2build_available: false },
  33. }
  34. `
  35. soongCcProtoLibraries = `
  36. cc_library {
  37. name: "libprotobuf-cpp-lite",
  38. bazel_module: { bp2build_available: false },
  39. }
  40. cc_library {
  41. name: "libprotobuf-cpp-full",
  42. bazel_module: { bp2build_available: false },
  43. }`
  44. soongCcProtoPreamble = soongCcLibraryPreamble + soongCcProtoLibraries
  45. )
  46. func runCcLibraryTestCase(t *testing.T, tc Bp2buildTestCase) {
  47. t.Helper()
  48. RunBp2BuildTestCase(t, registerCcLibraryModuleTypes, tc)
  49. }
  50. func registerCcLibraryModuleTypes(ctx android.RegistrationContext) {
  51. cc.RegisterCCBuildComponents(ctx)
  52. ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
  53. ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory)
  54. ctx.RegisterModuleType("cc_prebuilt_library_static", cc.PrebuiltStaticLibraryFactory)
  55. ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
  56. }
  57. func TestCcLibrarySimple(t *testing.T) {
  58. runCcLibraryTestCase(t, Bp2buildTestCase{
  59. Description: "cc_library - simple example",
  60. ModuleTypeUnderTest: "cc_library",
  61. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  62. Filesystem: map[string]string{
  63. soongCcVersionLibBpPath: soongCcVersionLibBp,
  64. "android.cpp": "",
  65. "bionic.cpp": "",
  66. "darwin.cpp": "",
  67. // Refer to cc.headerExts for the supported header extensions in Soong.
  68. "header.h": "",
  69. "header.hh": "",
  70. "header.hpp": "",
  71. "header.hxx": "",
  72. "header.h++": "",
  73. "header.inl": "",
  74. "header.inc": "",
  75. "header.ipp": "",
  76. "header.h.generic": "",
  77. "impl.cpp": "",
  78. "linux.cpp": "",
  79. "x86.cpp": "",
  80. "x86_64.cpp": "",
  81. "foo-dir/a.h": "",
  82. },
  83. Blueprint: soongCcLibraryPreamble +
  84. simpleModuleDoNotConvertBp2build("cc_library_headers", "some-headers") + `
  85. cc_library {
  86. name: "foo-lib",
  87. srcs: ["impl.cpp"],
  88. cflags: ["-Wall"],
  89. header_libs: ["some-headers"],
  90. export_include_dirs: ["foo-dir"],
  91. ldflags: ["-Wl,--exclude-libs=bar.a"],
  92. arch: {
  93. x86: {
  94. ldflags: ["-Wl,--exclude-libs=baz.a"],
  95. srcs: ["x86.cpp"],
  96. },
  97. x86_64: {
  98. ldflags: ["-Wl,--exclude-libs=qux.a"],
  99. srcs: ["x86_64.cpp"],
  100. },
  101. },
  102. target: {
  103. android: {
  104. srcs: ["android.cpp"],
  105. },
  106. linux_glibc: {
  107. srcs: ["linux.cpp"],
  108. },
  109. darwin: {
  110. srcs: ["darwin.cpp"],
  111. },
  112. bionic: {
  113. srcs: ["bionic.cpp"]
  114. },
  115. },
  116. include_build_directory: false,
  117. sdk_version: "current",
  118. min_sdk_version: "29",
  119. use_version_lib: true,
  120. }
  121. `,
  122. ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
  123. "copts": `["-Wall"]`,
  124. "export_includes": `["foo-dir"]`,
  125. "implementation_deps": `[":some-headers"]`,
  126. "linkopts": `["-Wl,--exclude-libs=bar.a"] + select({
  127. "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=baz.a"],
  128. "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=qux.a"],
  129. "//conditions:default": [],
  130. })`,
  131. "srcs": `["impl.cpp"] + select({
  132. "//build/bazel/platforms/arch:x86": ["x86.cpp"],
  133. "//build/bazel/platforms/arch:x86_64": ["x86_64.cpp"],
  134. "//conditions:default": [],
  135. }) + select({
  136. "//build/bazel/platforms/os:android": [
  137. "bionic.cpp",
  138. "android.cpp",
  139. ],
  140. "//build/bazel/platforms/os:darwin": ["darwin.cpp"],
  141. "//build/bazel/platforms/os:linux": ["linux.cpp"],
  142. "//build/bazel/platforms/os:linux_bionic": ["bionic.cpp"],
  143. "//conditions:default": [],
  144. })`,
  145. "sdk_version": `"current"`,
  146. "min_sdk_version": `"29"`,
  147. "use_version_lib": `True`,
  148. "implementation_whole_archive_deps": `["//build/soong/cc/libbuildversion:libbuildversion"]`,
  149. }),
  150. })
  151. }
  152. func TestCcLibraryTrimmedLdAndroid(t *testing.T) {
  153. runCcLibraryTestCase(t, Bp2buildTestCase{
  154. Description: "cc_library - trimmed example of //bionic/linker:ld-android",
  155. ModuleTypeUnderTest: "cc_library",
  156. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  157. Filesystem: map[string]string{
  158. "ld-android.cpp": "",
  159. "linked_list.h": "",
  160. "linker.h": "",
  161. "linker_block_allocator.h": "",
  162. "linker_cfi.h": "",
  163. },
  164. Blueprint: soongCcLibraryPreamble +
  165. simpleModuleDoNotConvertBp2build("cc_library_headers", "libc_headers") + `
  166. cc_library {
  167. name: "fake-ld-android",
  168. srcs: ["ld_android.cpp"],
  169. cflags: [
  170. "-Wall",
  171. "-Wextra",
  172. "-Wunused",
  173. "-Werror",
  174. ],
  175. header_libs: ["libc_headers"],
  176. ldflags: [
  177. "-Wl,--exclude-libs=libgcc.a",
  178. "-Wl,--exclude-libs=libgcc_stripped.a",
  179. "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
  180. "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
  181. "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
  182. "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
  183. ],
  184. arch: {
  185. x86: {
  186. ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
  187. },
  188. x86_64: {
  189. ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
  190. },
  191. },
  192. include_build_directory: false,
  193. }
  194. `,
  195. ExpectedBazelTargets: makeCcLibraryTargets("fake-ld-android", AttrNameToString{
  196. "srcs": `["ld_android.cpp"]`,
  197. "copts": `[
  198. "-Wall",
  199. "-Wextra",
  200. "-Wunused",
  201. "-Werror",
  202. ]`,
  203. "implementation_deps": `[":libc_headers"]`,
  204. "linkopts": `[
  205. "-Wl,--exclude-libs=libgcc.a",
  206. "-Wl,--exclude-libs=libgcc_stripped.a",
  207. "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
  208. "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
  209. "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
  210. "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
  211. ] + select({
  212. "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=libgcc_eh.a"],
  213. "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=libgcc_eh.a"],
  214. "//conditions:default": [],
  215. })`,
  216. }),
  217. })
  218. }
  219. func TestCcLibraryExcludeSrcs(t *testing.T) {
  220. runCcLibraryTestCase(t, Bp2buildTestCase{
  221. Description: "cc_library exclude_srcs - trimmed example of //external/arm-optimized-routines:libarm-optimized-routines-math",
  222. ModuleTypeUnderTest: "cc_library",
  223. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  224. Dir: "external",
  225. Filesystem: map[string]string{
  226. "external/math/cosf.c": "",
  227. "external/math/erf.c": "",
  228. "external/math/erf_data.c": "",
  229. "external/math/erff.c": "",
  230. "external/math/erff_data.c": "",
  231. "external/Android.bp": `
  232. cc_library {
  233. name: "fake-libarm-optimized-routines-math",
  234. exclude_srcs: [
  235. // Provided by:
  236. // bionic/libm/upstream-freebsd/lib/msun/src/s_erf.c
  237. // bionic/libm/upstream-freebsd/lib/msun/src/s_erff.c
  238. "math/erf.c",
  239. "math/erf_data.c",
  240. "math/erff.c",
  241. "math/erff_data.c",
  242. ],
  243. srcs: [
  244. "math/*.c",
  245. ],
  246. // arch-specific settings
  247. arch: {
  248. arm64: {
  249. cflags: [
  250. "-DHAVE_FAST_FMA=1",
  251. ],
  252. },
  253. },
  254. bazel_module: { bp2build_available: true },
  255. }
  256. `,
  257. },
  258. Blueprint: soongCcLibraryPreamble,
  259. ExpectedBazelTargets: makeCcLibraryTargets("fake-libarm-optimized-routines-math", AttrNameToString{
  260. "copts": `select({
  261. "//build/bazel/platforms/arch:arm64": ["-DHAVE_FAST_FMA=1"],
  262. "//conditions:default": [],
  263. })`,
  264. "local_includes": `["."]`,
  265. "srcs_c": `["math/cosf.c"]`,
  266. }),
  267. })
  268. }
  269. func TestCcLibrarySharedStaticProps(t *testing.T) {
  270. runCcLibraryTestCase(t, Bp2buildTestCase{
  271. Description: "cc_library shared/static props",
  272. ModuleTypeUnderTest: "cc_library",
  273. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  274. Filesystem: map[string]string{
  275. "both.cpp": "",
  276. "sharedonly.cpp": "",
  277. "staticonly.cpp": "",
  278. },
  279. Blueprint: soongCcLibraryPreamble + `
  280. cc_library {
  281. name: "a",
  282. srcs: ["both.cpp"],
  283. cflags: ["bothflag"],
  284. shared_libs: ["shared_dep_for_both"],
  285. static_libs: ["static_dep_for_both", "whole_and_static_lib_for_both"],
  286. whole_static_libs: ["whole_static_lib_for_both", "whole_and_static_lib_for_both"],
  287. static: {
  288. srcs: ["staticonly.cpp"],
  289. cflags: ["staticflag"],
  290. shared_libs: ["shared_dep_for_static"],
  291. static_libs: ["static_dep_for_static"],
  292. whole_static_libs: ["whole_static_lib_for_static"],
  293. },
  294. shared: {
  295. srcs: ["sharedonly.cpp"],
  296. cflags: ["sharedflag"],
  297. shared_libs: ["shared_dep_for_shared"],
  298. static_libs: ["static_dep_for_shared"],
  299. whole_static_libs: ["whole_static_lib_for_shared"],
  300. },
  301. include_build_directory: false,
  302. }
  303. cc_library_static {
  304. name: "static_dep_for_shared",
  305. bazel_module: { bp2build_available: false },
  306. }
  307. cc_library_static {
  308. name: "static_dep_for_static",
  309. bazel_module: { bp2build_available: false },
  310. }
  311. cc_library_static {
  312. name: "static_dep_for_both",
  313. bazel_module: { bp2build_available: false },
  314. }
  315. cc_library_static {
  316. name: "whole_static_lib_for_shared",
  317. bazel_module: { bp2build_available: false },
  318. }
  319. cc_library_static {
  320. name: "whole_static_lib_for_static",
  321. bazel_module: { bp2build_available: false },
  322. }
  323. cc_library_static {
  324. name: "whole_static_lib_for_both",
  325. bazel_module: { bp2build_available: false },
  326. }
  327. cc_library_static {
  328. name: "whole_and_static_lib_for_both",
  329. bazel_module: { bp2build_available: false },
  330. }
  331. cc_library {
  332. name: "shared_dep_for_shared",
  333. bazel_module: { bp2build_available: false },
  334. }
  335. cc_library {
  336. name: "shared_dep_for_static",
  337. bazel_module: { bp2build_available: false },
  338. }
  339. cc_library {
  340. name: "shared_dep_for_both",
  341. bazel_module: { bp2build_available: false },
  342. }
  343. `,
  344. ExpectedBazelTargets: []string{
  345. MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
  346. "copts": `[
  347. "bothflag",
  348. "staticflag",
  349. ]`,
  350. "implementation_deps": `[
  351. ":static_dep_for_both",
  352. ":static_dep_for_static",
  353. ]`,
  354. "implementation_dynamic_deps": `[
  355. ":shared_dep_for_both",
  356. ":shared_dep_for_static",
  357. ]`,
  358. "srcs": `[
  359. "both.cpp",
  360. "staticonly.cpp",
  361. ]`,
  362. "whole_archive_deps": `[
  363. ":whole_static_lib_for_both",
  364. ":whole_and_static_lib_for_both",
  365. ":whole_static_lib_for_static",
  366. ]`}),
  367. MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
  368. "copts": `[
  369. "bothflag",
  370. "sharedflag",
  371. ]`,
  372. "implementation_deps": `[
  373. ":static_dep_for_both",
  374. ":static_dep_for_shared",
  375. ]`,
  376. "implementation_dynamic_deps": `[
  377. ":shared_dep_for_both",
  378. ":shared_dep_for_shared",
  379. ]`,
  380. "srcs": `[
  381. "both.cpp",
  382. "sharedonly.cpp",
  383. ]`,
  384. "whole_archive_deps": `[
  385. ":whole_static_lib_for_both",
  386. ":whole_and_static_lib_for_both",
  387. ":whole_static_lib_for_shared",
  388. ]`,
  389. }),
  390. },
  391. })
  392. }
  393. func TestCcLibraryDeps(t *testing.T) {
  394. runCcLibraryTestCase(t, Bp2buildTestCase{
  395. Description: "cc_library shared/static props",
  396. ModuleTypeUnderTest: "cc_library",
  397. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  398. Filesystem: map[string]string{
  399. "both.cpp": "",
  400. "sharedonly.cpp": "",
  401. "staticonly.cpp": "",
  402. },
  403. Blueprint: soongCcLibraryPreamble + `
  404. cc_library {
  405. name: "a",
  406. srcs: ["both.cpp"],
  407. cflags: ["bothflag"],
  408. shared_libs: ["implementation_shared_dep_for_both", "shared_dep_for_both"],
  409. export_shared_lib_headers: ["shared_dep_for_both"],
  410. static_libs: ["implementation_static_dep_for_both", "static_dep_for_both"],
  411. export_static_lib_headers: ["static_dep_for_both", "whole_static_dep_for_both"],
  412. whole_static_libs: ["not_explicitly_exported_whole_static_dep_for_both", "whole_static_dep_for_both"],
  413. static: {
  414. srcs: ["staticonly.cpp"],
  415. cflags: ["staticflag"],
  416. shared_libs: ["implementation_shared_dep_for_static", "shared_dep_for_static"],
  417. export_shared_lib_headers: ["shared_dep_for_static"],
  418. static_libs: ["implementation_static_dep_for_static", "static_dep_for_static"],
  419. export_static_lib_headers: ["static_dep_for_static", "whole_static_dep_for_static"],
  420. whole_static_libs: ["not_explicitly_exported_whole_static_dep_for_static", "whole_static_dep_for_static"],
  421. },
  422. shared: {
  423. srcs: ["sharedonly.cpp"],
  424. cflags: ["sharedflag"],
  425. shared_libs: ["implementation_shared_dep_for_shared", "shared_dep_for_shared"],
  426. export_shared_lib_headers: ["shared_dep_for_shared"],
  427. static_libs: ["implementation_static_dep_for_shared", "static_dep_for_shared"],
  428. export_static_lib_headers: ["static_dep_for_shared", "whole_static_dep_for_shared"],
  429. whole_static_libs: ["not_explicitly_exported_whole_static_dep_for_shared", "whole_static_dep_for_shared"],
  430. },
  431. include_build_directory: false,
  432. }
  433. ` + simpleModuleDoNotConvertBp2build("cc_library_static", "static_dep_for_shared") +
  434. simpleModuleDoNotConvertBp2build("cc_library_static", "implementation_static_dep_for_shared") +
  435. simpleModuleDoNotConvertBp2build("cc_library_static", "static_dep_for_static") +
  436. simpleModuleDoNotConvertBp2build("cc_library_static", "implementation_static_dep_for_static") +
  437. simpleModuleDoNotConvertBp2build("cc_library_static", "static_dep_for_both") +
  438. simpleModuleDoNotConvertBp2build("cc_library_static", "implementation_static_dep_for_both") +
  439. simpleModuleDoNotConvertBp2build("cc_library_static", "whole_static_dep_for_shared") +
  440. simpleModuleDoNotConvertBp2build("cc_library_static", "not_explicitly_exported_whole_static_dep_for_shared") +
  441. simpleModuleDoNotConvertBp2build("cc_library_static", "whole_static_dep_for_static") +
  442. simpleModuleDoNotConvertBp2build("cc_library_static", "not_explicitly_exported_whole_static_dep_for_static") +
  443. simpleModuleDoNotConvertBp2build("cc_library_static", "whole_static_dep_for_both") +
  444. simpleModuleDoNotConvertBp2build("cc_library_static", "not_explicitly_exported_whole_static_dep_for_both") +
  445. simpleModuleDoNotConvertBp2build("cc_library", "shared_dep_for_shared") +
  446. simpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep_for_shared") +
  447. simpleModuleDoNotConvertBp2build("cc_library", "shared_dep_for_static") +
  448. simpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep_for_static") +
  449. simpleModuleDoNotConvertBp2build("cc_library", "shared_dep_for_both") +
  450. simpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep_for_both"),
  451. ExpectedBazelTargets: []string{
  452. MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
  453. "copts": `[
  454. "bothflag",
  455. "staticflag",
  456. ]`,
  457. "deps": `[
  458. ":static_dep_for_both",
  459. ":static_dep_for_static",
  460. ]`,
  461. "dynamic_deps": `[
  462. ":shared_dep_for_both",
  463. ":shared_dep_for_static",
  464. ]`,
  465. "implementation_deps": `[
  466. ":implementation_static_dep_for_both",
  467. ":implementation_static_dep_for_static",
  468. ]`,
  469. "implementation_dynamic_deps": `[
  470. ":implementation_shared_dep_for_both",
  471. ":implementation_shared_dep_for_static",
  472. ]`,
  473. "srcs": `[
  474. "both.cpp",
  475. "staticonly.cpp",
  476. ]`,
  477. "whole_archive_deps": `[
  478. ":not_explicitly_exported_whole_static_dep_for_both",
  479. ":whole_static_dep_for_both",
  480. ":not_explicitly_exported_whole_static_dep_for_static",
  481. ":whole_static_dep_for_static",
  482. ]`,
  483. }),
  484. MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
  485. "copts": `[
  486. "bothflag",
  487. "sharedflag",
  488. ]`,
  489. "deps": `[
  490. ":static_dep_for_both",
  491. ":static_dep_for_shared",
  492. ]`,
  493. "dynamic_deps": `[
  494. ":shared_dep_for_both",
  495. ":shared_dep_for_shared",
  496. ]`,
  497. "implementation_deps": `[
  498. ":implementation_static_dep_for_both",
  499. ":implementation_static_dep_for_shared",
  500. ]`,
  501. "implementation_dynamic_deps": `[
  502. ":implementation_shared_dep_for_both",
  503. ":implementation_shared_dep_for_shared",
  504. ]`,
  505. "srcs": `[
  506. "both.cpp",
  507. "sharedonly.cpp",
  508. ]`,
  509. "whole_archive_deps": `[
  510. ":not_explicitly_exported_whole_static_dep_for_both",
  511. ":whole_static_dep_for_both",
  512. ":not_explicitly_exported_whole_static_dep_for_shared",
  513. ":whole_static_dep_for_shared",
  514. ]`,
  515. })},
  516. },
  517. )
  518. }
  519. func TestCcLibraryWholeStaticLibsAlwaysLink(t *testing.T) {
  520. runCcLibraryTestCase(t, Bp2buildTestCase{
  521. ModuleTypeUnderTest: "cc_library",
  522. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  523. Dir: "foo/bar",
  524. Filesystem: map[string]string{
  525. "foo/bar/Android.bp": `
  526. cc_library {
  527. name: "a",
  528. whole_static_libs: ["whole_static_lib_for_both"],
  529. static: {
  530. whole_static_libs: ["whole_static_lib_for_static"],
  531. },
  532. shared: {
  533. whole_static_libs: ["whole_static_lib_for_shared"],
  534. },
  535. bazel_module: { bp2build_available: true },
  536. include_build_directory: false,
  537. }
  538. cc_prebuilt_library_static { name: "whole_static_lib_for_shared" }
  539. cc_prebuilt_library_static { name: "whole_static_lib_for_static" }
  540. cc_prebuilt_library_static { name: "whole_static_lib_for_both" }
  541. `,
  542. },
  543. Blueprint: soongCcLibraryPreamble,
  544. ExpectedBazelTargets: []string{
  545. MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
  546. "whole_archive_deps": `[
  547. ":whole_static_lib_for_both_alwayslink",
  548. ":whole_static_lib_for_static_alwayslink",
  549. ]`,
  550. }),
  551. MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
  552. "whole_archive_deps": `[
  553. ":whole_static_lib_for_both_alwayslink",
  554. ":whole_static_lib_for_shared_alwayslink",
  555. ]`,
  556. }),
  557. },
  558. },
  559. )
  560. }
  561. func TestCcLibrarySharedStaticPropsInArch(t *testing.T) {
  562. runCcLibraryTestCase(t, Bp2buildTestCase{
  563. Description: "cc_library shared/static props in arch",
  564. ModuleTypeUnderTest: "cc_library",
  565. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  566. Dir: "foo/bar",
  567. Filesystem: map[string]string{
  568. "foo/bar/arm.cpp": "",
  569. "foo/bar/x86.cpp": "",
  570. "foo/bar/sharedonly.cpp": "",
  571. "foo/bar/staticonly.cpp": "",
  572. "foo/bar/Android.bp": `
  573. cc_library {
  574. name: "a",
  575. arch: {
  576. arm: {
  577. shared: {
  578. srcs: ["arm_shared.cpp"],
  579. cflags: ["-DARM_SHARED"],
  580. static_libs: ["arm_static_dep_for_shared"],
  581. whole_static_libs: ["arm_whole_static_dep_for_shared"],
  582. shared_libs: ["arm_shared_dep_for_shared"],
  583. },
  584. },
  585. x86: {
  586. static: {
  587. srcs: ["x86_static.cpp"],
  588. cflags: ["-DX86_STATIC"],
  589. static_libs: ["x86_dep_for_static"],
  590. },
  591. },
  592. },
  593. target: {
  594. android: {
  595. shared: {
  596. srcs: ["android_shared.cpp"],
  597. cflags: ["-DANDROID_SHARED"],
  598. static_libs: ["android_dep_for_shared"],
  599. },
  600. },
  601. android_arm: {
  602. shared: {
  603. cflags: ["-DANDROID_ARM_SHARED"],
  604. },
  605. },
  606. },
  607. srcs: ["both.cpp"],
  608. cflags: ["bothflag"],
  609. static_libs: ["static_dep_for_both"],
  610. static: {
  611. srcs: ["staticonly.cpp"],
  612. cflags: ["staticflag"],
  613. static_libs: ["static_dep_for_static"],
  614. },
  615. shared: {
  616. srcs: ["sharedonly.cpp"],
  617. cflags: ["sharedflag"],
  618. static_libs: ["static_dep_for_shared"],
  619. },
  620. bazel_module: { bp2build_available: true },
  621. }
  622. cc_library_static { name: "static_dep_for_shared" }
  623. cc_library_static { name: "static_dep_for_static" }
  624. cc_library_static { name: "static_dep_for_both" }
  625. cc_library_static { name: "arm_static_dep_for_shared" }
  626. cc_library_static { name: "arm_whole_static_dep_for_shared" }
  627. cc_library_static { name: "arm_shared_dep_for_shared" }
  628. cc_library_static { name: "x86_dep_for_static" }
  629. cc_library_static { name: "android_dep_for_shared" }
  630. `,
  631. },
  632. Blueprint: soongCcLibraryPreamble,
  633. ExpectedBazelTargets: []string{
  634. MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
  635. "copts": `[
  636. "bothflag",
  637. "staticflag",
  638. ] + select({
  639. "//build/bazel/platforms/arch:x86": ["-DX86_STATIC"],
  640. "//conditions:default": [],
  641. })`,
  642. "implementation_deps": `[
  643. ":static_dep_for_both",
  644. ":static_dep_for_static",
  645. ] + select({
  646. "//build/bazel/platforms/arch:x86": [":x86_dep_for_static"],
  647. "//conditions:default": [],
  648. })`,
  649. "local_includes": `["."]`,
  650. "srcs": `[
  651. "both.cpp",
  652. "staticonly.cpp",
  653. ] + select({
  654. "//build/bazel/platforms/arch:x86": ["x86_static.cpp"],
  655. "//conditions:default": [],
  656. })`,
  657. }),
  658. MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
  659. "copts": `[
  660. "bothflag",
  661. "sharedflag",
  662. ] + select({
  663. "//build/bazel/platforms/arch:arm": ["-DARM_SHARED"],
  664. "//conditions:default": [],
  665. }) + select({
  666. "//build/bazel/platforms/os:android": ["-DANDROID_SHARED"],
  667. "//conditions:default": [],
  668. }) + select({
  669. "//build/bazel/platforms/os_arch:android_arm": ["-DANDROID_ARM_SHARED"],
  670. "//conditions:default": [],
  671. })`,
  672. "implementation_deps": `[
  673. ":static_dep_for_both",
  674. ":static_dep_for_shared",
  675. ] + select({
  676. "//build/bazel/platforms/arch:arm": [":arm_static_dep_for_shared"],
  677. "//conditions:default": [],
  678. }) + select({
  679. "//build/bazel/platforms/os:android": [":android_dep_for_shared"],
  680. "//conditions:default": [],
  681. })`,
  682. "implementation_dynamic_deps": `select({
  683. "//build/bazel/platforms/arch:arm": [":arm_shared_dep_for_shared"],
  684. "//conditions:default": [],
  685. })`,
  686. "local_includes": `["."]`,
  687. "srcs": `[
  688. "both.cpp",
  689. "sharedonly.cpp",
  690. ] + select({
  691. "//build/bazel/platforms/arch:arm": ["arm_shared.cpp"],
  692. "//conditions:default": [],
  693. }) + select({
  694. "//build/bazel/platforms/os:android": ["android_shared.cpp"],
  695. "//conditions:default": [],
  696. })`,
  697. "whole_archive_deps": `select({
  698. "//build/bazel/platforms/arch:arm": [":arm_whole_static_dep_for_shared"],
  699. "//conditions:default": [],
  700. })`,
  701. }),
  702. },
  703. },
  704. )
  705. }
  706. func TestCcLibrarySharedStaticPropsWithMixedSources(t *testing.T) {
  707. runCcLibraryTestCase(t, Bp2buildTestCase{
  708. Description: "cc_library shared/static props with c/cpp/s mixed sources",
  709. ModuleTypeUnderTest: "cc_library",
  710. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  711. Dir: "foo/bar",
  712. Filesystem: map[string]string{
  713. "foo/bar/both_source.cpp": "",
  714. "foo/bar/both_source.cc": "",
  715. "foo/bar/both_source.c": "",
  716. "foo/bar/both_source.s": "",
  717. "foo/bar/both_source.S": "",
  718. "foo/bar/shared_source.cpp": "",
  719. "foo/bar/shared_source.cc": "",
  720. "foo/bar/shared_source.c": "",
  721. "foo/bar/shared_source.s": "",
  722. "foo/bar/shared_source.S": "",
  723. "foo/bar/static_source.cpp": "",
  724. "foo/bar/static_source.cc": "",
  725. "foo/bar/static_source.c": "",
  726. "foo/bar/static_source.s": "",
  727. "foo/bar/static_source.S": "",
  728. "foo/bar/Android.bp": `
  729. cc_library {
  730. name: "a",
  731. srcs: [
  732. "both_source.cpp",
  733. "both_source.cc",
  734. "both_source.c",
  735. "both_source.s",
  736. "both_source.S",
  737. ":both_filegroup",
  738. ],
  739. static: {
  740. srcs: [
  741. "static_source.cpp",
  742. "static_source.cc",
  743. "static_source.c",
  744. "static_source.s",
  745. "static_source.S",
  746. ":static_filegroup",
  747. ],
  748. },
  749. shared: {
  750. srcs: [
  751. "shared_source.cpp",
  752. "shared_source.cc",
  753. "shared_source.c",
  754. "shared_source.s",
  755. "shared_source.S",
  756. ":shared_filegroup",
  757. ],
  758. },
  759. bazel_module: { bp2build_available: true },
  760. }
  761. filegroup {
  762. name: "both_filegroup",
  763. srcs: [
  764. // Not relevant, handled by filegroup macro
  765. ],
  766. }
  767. filegroup {
  768. name: "shared_filegroup",
  769. srcs: [
  770. // Not relevant, handled by filegroup macro
  771. ],
  772. }
  773. filegroup {
  774. name: "static_filegroup",
  775. srcs: [
  776. // Not relevant, handled by filegroup macro
  777. ],
  778. }
  779. `,
  780. },
  781. Blueprint: soongCcLibraryPreamble,
  782. ExpectedBazelTargets: []string{
  783. MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
  784. "local_includes": `["."]`,
  785. "srcs": `[
  786. "both_source.cpp",
  787. "both_source.cc",
  788. ":both_filegroup_cpp_srcs",
  789. "static_source.cpp",
  790. "static_source.cc",
  791. ":static_filegroup_cpp_srcs",
  792. ]`,
  793. "srcs_as": `[
  794. "both_source.s",
  795. "both_source.S",
  796. ":both_filegroup_as_srcs",
  797. "static_source.s",
  798. "static_source.S",
  799. ":static_filegroup_as_srcs",
  800. ]`,
  801. "srcs_c": `[
  802. "both_source.c",
  803. ":both_filegroup_c_srcs",
  804. "static_source.c",
  805. ":static_filegroup_c_srcs",
  806. ]`,
  807. }),
  808. MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
  809. "local_includes": `["."]`,
  810. "srcs": `[
  811. "both_source.cpp",
  812. "both_source.cc",
  813. ":both_filegroup_cpp_srcs",
  814. "shared_source.cpp",
  815. "shared_source.cc",
  816. ":shared_filegroup_cpp_srcs",
  817. ]`,
  818. "srcs_as": `[
  819. "both_source.s",
  820. "both_source.S",
  821. ":both_filegroup_as_srcs",
  822. "shared_source.s",
  823. "shared_source.S",
  824. ":shared_filegroup_as_srcs",
  825. ]`,
  826. "srcs_c": `[
  827. "both_source.c",
  828. ":both_filegroup_c_srcs",
  829. "shared_source.c",
  830. ":shared_filegroup_c_srcs",
  831. ]`,
  832. })}})
  833. }
  834. func TestCcLibraryNonConfiguredVersionScript(t *testing.T) {
  835. runCcLibraryTestCase(t, Bp2buildTestCase{
  836. Description: "cc_library non-configured version script",
  837. ModuleTypeUnderTest: "cc_library",
  838. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  839. Dir: "foo/bar",
  840. Filesystem: map[string]string{
  841. "foo/bar/Android.bp": `
  842. cc_library {
  843. name: "a",
  844. srcs: ["a.cpp"],
  845. version_script: "v.map",
  846. bazel_module: { bp2build_available: true },
  847. include_build_directory: false,
  848. }
  849. `,
  850. },
  851. Blueprint: soongCcLibraryPreamble,
  852. ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
  853. "additional_linker_inputs": `["v.map"]`,
  854. "linkopts": `["-Wl,--version-script,$(location v.map)"]`,
  855. "srcs": `["a.cpp"]`,
  856. }),
  857. },
  858. )
  859. }
  860. func TestCcLibraryConfiguredVersionScript(t *testing.T) {
  861. runCcLibraryTestCase(t, Bp2buildTestCase{
  862. Description: "cc_library configured version script",
  863. ModuleTypeUnderTest: "cc_library",
  864. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  865. Dir: "foo/bar",
  866. Filesystem: map[string]string{
  867. "foo/bar/Android.bp": `
  868. cc_library {
  869. name: "a",
  870. srcs: ["a.cpp"],
  871. arch: {
  872. arm: {
  873. version_script: "arm.map",
  874. },
  875. arm64: {
  876. version_script: "arm64.map",
  877. },
  878. },
  879. bazel_module: { bp2build_available: true },
  880. include_build_directory: false,
  881. }
  882. `,
  883. },
  884. Blueprint: soongCcLibraryPreamble,
  885. ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
  886. "additional_linker_inputs": `select({
  887. "//build/bazel/platforms/arch:arm": ["arm.map"],
  888. "//build/bazel/platforms/arch:arm64": ["arm64.map"],
  889. "//conditions:default": [],
  890. })`,
  891. "linkopts": `select({
  892. "//build/bazel/platforms/arch:arm": ["-Wl,--version-script,$(location arm.map)"],
  893. "//build/bazel/platforms/arch:arm64": ["-Wl,--version-script,$(location arm64.map)"],
  894. "//conditions:default": [],
  895. })`,
  896. "srcs": `["a.cpp"]`,
  897. }),
  898. },
  899. )
  900. }
  901. func TestCcLibrarySharedLibs(t *testing.T) {
  902. runCcLibraryTestCase(t, Bp2buildTestCase{
  903. Description: "cc_library shared_libs",
  904. ModuleTypeUnderTest: "cc_library",
  905. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  906. Blueprint: soongCcLibraryPreamble + `
  907. cc_library {
  908. name: "mylib",
  909. bazel_module: { bp2build_available: false },
  910. }
  911. cc_library {
  912. name: "a",
  913. shared_libs: ["mylib",],
  914. include_build_directory: false,
  915. }
  916. `,
  917. ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
  918. "implementation_dynamic_deps": `[":mylib"]`,
  919. }),
  920. },
  921. )
  922. }
  923. func TestCcLibraryFeatures(t *testing.T) {
  924. expected_targets := []string{}
  925. expected_targets = append(expected_targets, makeCcLibraryTargets("a", AttrNameToString{
  926. "features": `[
  927. "disable_pack_relocations",
  928. "-no_undefined_symbols",
  929. "-coverage",
  930. ]`,
  931. "srcs": `["a.cpp"]`,
  932. })...)
  933. expected_targets = append(expected_targets, makeCcLibraryTargets("b", AttrNameToString{
  934. "features": `["-coverage"] + select({
  935. "//build/bazel/platforms/arch:x86_64": [
  936. "disable_pack_relocations",
  937. "-no_undefined_symbols",
  938. ],
  939. "//conditions:default": [],
  940. })`,
  941. "srcs": `["b.cpp"]`,
  942. })...)
  943. expected_targets = append(expected_targets, makeCcLibraryTargets("c", AttrNameToString{
  944. "features": `select({
  945. "//build/bazel/platforms/os:darwin": [
  946. "disable_pack_relocations",
  947. "-no_undefined_symbols",
  948. ],
  949. "//conditions:default": [],
  950. })`,
  951. "srcs": `["c.cpp"]`,
  952. })...)
  953. runCcLibraryTestCase(t, Bp2buildTestCase{
  954. Description: "cc_library pack_relocations test",
  955. ModuleTypeUnderTest: "cc_library",
  956. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  957. Blueprint: soongCcLibraryPreamble + `
  958. cc_library {
  959. name: "a",
  960. srcs: ["a.cpp"],
  961. pack_relocations: false,
  962. allow_undefined_symbols: true,
  963. include_build_directory: false,
  964. native_coverage: false,
  965. }
  966. cc_library {
  967. name: "b",
  968. srcs: ["b.cpp"],
  969. arch: {
  970. x86_64: {
  971. pack_relocations: false,
  972. allow_undefined_symbols: true,
  973. },
  974. },
  975. include_build_directory: false,
  976. native_coverage: false,
  977. }
  978. cc_library {
  979. name: "c",
  980. srcs: ["c.cpp"],
  981. target: {
  982. darwin: {
  983. pack_relocations: false,
  984. allow_undefined_symbols: true,
  985. },
  986. },
  987. include_build_directory: false,
  988. }`,
  989. ExpectedBazelTargets: expected_targets,
  990. })
  991. }
  992. func TestCcLibrarySpacesInCopts(t *testing.T) {
  993. runCcLibraryTestCase(t, Bp2buildTestCase{
  994. Description: "cc_library spaces in copts",
  995. ModuleTypeUnderTest: "cc_library",
  996. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  997. Blueprint: soongCcLibraryPreamble + `
  998. cc_library {
  999. name: "a",
  1000. cflags: ["-include header.h",],
  1001. include_build_directory: false,
  1002. }
  1003. `,
  1004. ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
  1005. "copts": `[
  1006. "-include",
  1007. "header.h",
  1008. ]`,
  1009. }),
  1010. },
  1011. )
  1012. }
  1013. func TestCcLibraryCppFlagsGoesIntoCopts(t *testing.T) {
  1014. runCcLibraryTestCase(t, Bp2buildTestCase{
  1015. Description: "cc_library cppflags usage",
  1016. ModuleTypeUnderTest: "cc_library",
  1017. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  1018. Blueprint: soongCcLibraryPreamble + `cc_library {
  1019. name: "a",
  1020. srcs: ["a.cpp"],
  1021. cflags: ["-Wall"],
  1022. cppflags: [
  1023. "-fsigned-char",
  1024. "-pedantic",
  1025. ],
  1026. arch: {
  1027. arm64: {
  1028. cppflags: ["-DARM64=1"],
  1029. },
  1030. },
  1031. target: {
  1032. android: {
  1033. cppflags: ["-DANDROID=1"],
  1034. },
  1035. },
  1036. include_build_directory: false,
  1037. }
  1038. `,
  1039. ExpectedBazelTargets: makeCcLibraryTargets("a", AttrNameToString{
  1040. "copts": `["-Wall"]`,
  1041. "cppflags": `[
  1042. "-fsigned-char",
  1043. "-pedantic",
  1044. ] + select({
  1045. "//build/bazel/platforms/arch:arm64": ["-DARM64=1"],
  1046. "//conditions:default": [],
  1047. }) + select({
  1048. "//build/bazel/platforms/os:android": ["-DANDROID=1"],
  1049. "//conditions:default": [],
  1050. })`,
  1051. "srcs": `["a.cpp"]`,
  1052. }),
  1053. },
  1054. )
  1055. }
  1056. func TestCcLibraryExcludeLibs(t *testing.T) {
  1057. runCcLibraryTestCase(t, Bp2buildTestCase{
  1058. ModuleTypeUnderTest: "cc_library",
  1059. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  1060. Filesystem: map[string]string{},
  1061. Blueprint: soongCcLibraryStaticPreamble + `
  1062. cc_library {
  1063. name: "foo_static",
  1064. srcs: ["common.c"],
  1065. whole_static_libs: [
  1066. "arm_whole_static_lib_excludes",
  1067. "malloc_not_svelte_whole_static_lib_excludes"
  1068. ],
  1069. static_libs: [
  1070. "arm_static_lib_excludes",
  1071. "malloc_not_svelte_static_lib_excludes"
  1072. ],
  1073. shared_libs: [
  1074. "arm_shared_lib_excludes",
  1075. ],
  1076. arch: {
  1077. arm: {
  1078. exclude_shared_libs: [
  1079. "arm_shared_lib_excludes",
  1080. ],
  1081. exclude_static_libs: [
  1082. "arm_static_lib_excludes",
  1083. "arm_whole_static_lib_excludes",
  1084. ],
  1085. },
  1086. },
  1087. product_variables: {
  1088. malloc_not_svelte: {
  1089. shared_libs: ["malloc_not_svelte_shared_lib"],
  1090. whole_static_libs: ["malloc_not_svelte_whole_static_lib"],
  1091. exclude_static_libs: [
  1092. "malloc_not_svelte_static_lib_excludes",
  1093. "malloc_not_svelte_whole_static_lib_excludes",
  1094. ],
  1095. },
  1096. },
  1097. include_build_directory: false,
  1098. }
  1099. cc_library {
  1100. name: "arm_whole_static_lib_excludes",
  1101. bazel_module: { bp2build_available: false },
  1102. }
  1103. cc_library {
  1104. name: "malloc_not_svelte_whole_static_lib",
  1105. bazel_module: { bp2build_available: false },
  1106. }
  1107. cc_library {
  1108. name: "malloc_not_svelte_whole_static_lib_excludes",
  1109. bazel_module: { bp2build_available: false },
  1110. }
  1111. cc_library {
  1112. name: "arm_static_lib_excludes",
  1113. bazel_module: { bp2build_available: false },
  1114. }
  1115. cc_library {
  1116. name: "malloc_not_svelte_static_lib_excludes",
  1117. bazel_module: { bp2build_available: false },
  1118. }
  1119. cc_library {
  1120. name: "arm_shared_lib_excludes",
  1121. bazel_module: { bp2build_available: false },
  1122. }
  1123. cc_library {
  1124. name: "malloc_not_svelte_shared_lib",
  1125. bazel_module: { bp2build_available: false },
  1126. }
  1127. `,
  1128. ExpectedBazelTargets: makeCcLibraryTargets("foo_static", AttrNameToString{
  1129. "implementation_deps": `select({
  1130. "//build/bazel/platforms/arch:arm": [],
  1131. "//conditions:default": [":arm_static_lib_excludes_bp2build_cc_library_static"],
  1132. }) + select({
  1133. "//build/bazel/product_variables:malloc_not_svelte": [],
  1134. "//conditions:default": [":malloc_not_svelte_static_lib_excludes_bp2build_cc_library_static"],
  1135. })`,
  1136. "implementation_dynamic_deps": `select({
  1137. "//build/bazel/platforms/arch:arm": [],
  1138. "//conditions:default": [":arm_shared_lib_excludes"],
  1139. }) + select({
  1140. "//build/bazel/product_variables:malloc_not_svelte": [":malloc_not_svelte_shared_lib"],
  1141. "//conditions:default": [],
  1142. })`,
  1143. "srcs_c": `["common.c"]`,
  1144. "whole_archive_deps": `select({
  1145. "//build/bazel/platforms/arch:arm": [],
  1146. "//conditions:default": [":arm_whole_static_lib_excludes_bp2build_cc_library_static"],
  1147. }) + select({
  1148. "//build/bazel/product_variables:malloc_not_svelte": [":malloc_not_svelte_whole_static_lib_bp2build_cc_library_static"],
  1149. "//conditions:default": [":malloc_not_svelte_whole_static_lib_excludes_bp2build_cc_library_static"],
  1150. })`,
  1151. }),
  1152. },
  1153. )
  1154. }
  1155. func TestCcLibraryProductVariablesHeaderLibs(t *testing.T) {
  1156. runCcLibraryTestCase(t, Bp2buildTestCase{
  1157. ModuleTypeUnderTest: "cc_library",
  1158. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  1159. Filesystem: map[string]string{},
  1160. Blueprint: soongCcLibraryStaticPreamble + `
  1161. cc_library {
  1162. name: "foo_static",
  1163. srcs: ["common.c"],
  1164. product_variables: {
  1165. malloc_not_svelte: {
  1166. header_libs: ["malloc_not_svelte_header_lib"],
  1167. },
  1168. },
  1169. include_build_directory: false,
  1170. }
  1171. cc_library {
  1172. name: "malloc_not_svelte_header_lib",
  1173. bazel_module: { bp2build_available: false },
  1174. }
  1175. `,
  1176. ExpectedBazelTargets: makeCcLibraryTargets("foo_static", AttrNameToString{
  1177. "implementation_deps": `select({
  1178. "//build/bazel/product_variables:malloc_not_svelte": [":malloc_not_svelte_header_lib"],
  1179. "//conditions:default": [],
  1180. })`,
  1181. "srcs_c": `["common.c"]`,
  1182. "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
  1183. }),
  1184. },
  1185. )
  1186. }
  1187. func TestCCLibraryNoCrtTrue(t *testing.T) {
  1188. runCcLibraryTestCase(t, Bp2buildTestCase{
  1189. Description: "cc_library - nocrt: true emits attribute",
  1190. ModuleTypeUnderTest: "cc_library",
  1191. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  1192. Filesystem: map[string]string{
  1193. "impl.cpp": "",
  1194. },
  1195. Blueprint: soongCcLibraryPreamble + `
  1196. cc_library {
  1197. name: "foo-lib",
  1198. srcs: ["impl.cpp"],
  1199. nocrt: true,
  1200. include_build_directory: false,
  1201. }
  1202. `,
  1203. ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
  1204. "link_crt": `False`,
  1205. "srcs": `["impl.cpp"]`,
  1206. }),
  1207. },
  1208. )
  1209. }
  1210. func TestCCLibraryNoCrtFalse(t *testing.T) {
  1211. runCcLibraryTestCase(t, Bp2buildTestCase{
  1212. Description: "cc_library - nocrt: false - does not emit attribute",
  1213. ModuleTypeUnderTest: "cc_library",
  1214. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  1215. Filesystem: map[string]string{
  1216. "impl.cpp": "",
  1217. },
  1218. Blueprint: soongCcLibraryPreamble + `
  1219. cc_library {
  1220. name: "foo-lib",
  1221. srcs: ["impl.cpp"],
  1222. nocrt: false,
  1223. include_build_directory: false,
  1224. }
  1225. `,
  1226. ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
  1227. "srcs": `["impl.cpp"]`,
  1228. }),
  1229. })
  1230. }
  1231. func TestCCLibraryNoCrtArchVariant(t *testing.T) {
  1232. runCcLibraryTestCase(t, Bp2buildTestCase{
  1233. Description: "cc_library - nocrt in select",
  1234. ModuleTypeUnderTest: "cc_library",
  1235. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  1236. Filesystem: map[string]string{
  1237. "impl.cpp": "",
  1238. },
  1239. Blueprint: soongCcLibraryPreamble + `
  1240. cc_library {
  1241. name: "foo-lib",
  1242. srcs: ["impl.cpp"],
  1243. arch: {
  1244. arm: {
  1245. nocrt: true,
  1246. },
  1247. x86: {
  1248. nocrt: false,
  1249. },
  1250. },
  1251. include_build_directory: false,
  1252. }
  1253. `,
  1254. ExpectedErr: fmt.Errorf("module \"foo-lib\": nocrt is not supported for arch variants"),
  1255. })
  1256. }
  1257. func TestCCLibraryNoLibCrtTrue(t *testing.T) {
  1258. runCcLibraryTestCase(t, Bp2buildTestCase{
  1259. ModuleTypeUnderTest: "cc_library",
  1260. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  1261. Filesystem: map[string]string{
  1262. "impl.cpp": "",
  1263. },
  1264. Blueprint: soongCcLibraryPreamble + `
  1265. cc_library {
  1266. name: "foo-lib",
  1267. srcs: ["impl.cpp"],
  1268. no_libcrt: true,
  1269. include_build_directory: false,
  1270. }
  1271. `,
  1272. ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
  1273. "srcs": `["impl.cpp"]`,
  1274. "use_libcrt": `False`,
  1275. }),
  1276. })
  1277. }
  1278. func makeCcLibraryTargets(name string, attrs AttrNameToString) []string {
  1279. STATIC_ONLY_ATTRS := map[string]bool{}
  1280. SHARED_ONLY_ATTRS := map[string]bool{
  1281. "link_crt": true,
  1282. "additional_linker_inputs": true,
  1283. "linkopts": true,
  1284. "strip": true,
  1285. "inject_bssl_hash": true,
  1286. "has_stubs": true,
  1287. "use_version_lib": true,
  1288. }
  1289. sharedAttrs := AttrNameToString{}
  1290. staticAttrs := AttrNameToString{}
  1291. for key, val := range attrs {
  1292. if _, staticOnly := STATIC_ONLY_ATTRS[key]; !staticOnly {
  1293. sharedAttrs[key] = val
  1294. }
  1295. if _, sharedOnly := SHARED_ONLY_ATTRS[key]; !sharedOnly {
  1296. staticAttrs[key] = val
  1297. }
  1298. }
  1299. sharedTarget := MakeBazelTarget("cc_library_shared", name, sharedAttrs)
  1300. staticTarget := MakeBazelTarget("cc_library_static", name+"_bp2build_cc_library_static", staticAttrs)
  1301. return []string{staticTarget, sharedTarget}
  1302. }
  1303. func makeCcStubSuiteTargets(name string, attrs AttrNameToString) string {
  1304. if _, hasStubs := attrs["stubs_symbol_file"]; !hasStubs {
  1305. return ""
  1306. }
  1307. STUB_SUITE_ATTRS := map[string]string{
  1308. "stubs_symbol_file": "symbol_file",
  1309. "stubs_versions": "versions",
  1310. "soname": "soname",
  1311. "source_library": "source_library",
  1312. }
  1313. stubSuiteAttrs := AttrNameToString{}
  1314. for key, _ := range attrs {
  1315. if _, stubSuiteAttr := STUB_SUITE_ATTRS[key]; stubSuiteAttr {
  1316. stubSuiteAttrs[STUB_SUITE_ATTRS[key]] = attrs[key]
  1317. }
  1318. }
  1319. return MakeBazelTarget("cc_stub_suite", name+"_stub_libs", stubSuiteAttrs)
  1320. }
  1321. func TestCCLibraryNoLibCrtFalse(t *testing.T) {
  1322. runCcLibraryTestCase(t, Bp2buildTestCase{
  1323. ModuleTypeUnderTest: "cc_library",
  1324. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  1325. Filesystem: map[string]string{
  1326. "impl.cpp": "",
  1327. },
  1328. Blueprint: soongCcLibraryPreamble + `
  1329. cc_library {
  1330. name: "foo-lib",
  1331. srcs: ["impl.cpp"],
  1332. no_libcrt: false,
  1333. include_build_directory: false,
  1334. }
  1335. `,
  1336. ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
  1337. "srcs": `["impl.cpp"]`,
  1338. "use_libcrt": `True`,
  1339. }),
  1340. })
  1341. }
  1342. func TestCCLibraryNoLibCrtArchVariant(t *testing.T) {
  1343. runCcLibraryTestCase(t, Bp2buildTestCase{
  1344. ModuleTypeUnderTest: "cc_library",
  1345. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  1346. Filesystem: map[string]string{
  1347. "impl.cpp": "",
  1348. },
  1349. Blueprint: soongCcLibraryPreamble + `
  1350. cc_library {
  1351. name: "foo-lib",
  1352. srcs: ["impl.cpp"],
  1353. arch: {
  1354. arm: {
  1355. no_libcrt: true,
  1356. },
  1357. x86: {
  1358. no_libcrt: true,
  1359. },
  1360. },
  1361. include_build_directory: false,
  1362. }
  1363. `,
  1364. ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
  1365. "srcs": `["impl.cpp"]`,
  1366. "use_libcrt": `select({
  1367. "//build/bazel/platforms/arch:arm": False,
  1368. "//build/bazel/platforms/arch:x86": False,
  1369. "//conditions:default": None,
  1370. })`,
  1371. }),
  1372. })
  1373. }
  1374. func TestCCLibraryNoLibCrtArchAndTargetVariant(t *testing.T) {
  1375. runCcLibraryTestCase(t, Bp2buildTestCase{
  1376. ModuleTypeUnderTest: "cc_library",
  1377. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  1378. Filesystem: map[string]string{
  1379. "impl.cpp": "",
  1380. },
  1381. Blueprint: soongCcLibraryPreamble + `
  1382. cc_library {
  1383. name: "foo-lib",
  1384. srcs: ["impl.cpp"],
  1385. arch: {
  1386. arm: {
  1387. no_libcrt: true,
  1388. },
  1389. x86: {
  1390. no_libcrt: true,
  1391. },
  1392. },
  1393. target: {
  1394. darwin: {
  1395. no_libcrt: true,
  1396. }
  1397. },
  1398. include_build_directory: false,
  1399. }
  1400. `,
  1401. ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
  1402. "srcs": `["impl.cpp"]`,
  1403. "use_libcrt": `select({
  1404. "//build/bazel/platforms/os_arch:android_arm": False,
  1405. "//build/bazel/platforms/os_arch:android_x86": False,
  1406. "//build/bazel/platforms/os_arch:darwin_arm64": False,
  1407. "//build/bazel/platforms/os_arch:darwin_x86_64": False,
  1408. "//build/bazel/platforms/os_arch:linux_glibc_x86": False,
  1409. "//build/bazel/platforms/os_arch:linux_musl_x86": False,
  1410. "//build/bazel/platforms/os_arch:windows_x86": False,
  1411. "//conditions:default": None,
  1412. })`,
  1413. }),
  1414. })
  1415. }
  1416. func TestCCLibraryNoLibCrtArchAndTargetVariantConflict(t *testing.T) {
  1417. runCcLibraryTestCase(t, Bp2buildTestCase{
  1418. ModuleTypeUnderTest: "cc_library",
  1419. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  1420. Filesystem: map[string]string{
  1421. "impl.cpp": "",
  1422. },
  1423. Blueprint: soongCcLibraryPreamble + `
  1424. cc_library {
  1425. name: "foo-lib",
  1426. srcs: ["impl.cpp"],
  1427. arch: {
  1428. arm: {
  1429. no_libcrt: true,
  1430. },
  1431. // This is expected to override the value for darwin_x86_64.
  1432. x86_64: {
  1433. no_libcrt: true,
  1434. },
  1435. },
  1436. target: {
  1437. darwin: {
  1438. no_libcrt: false,
  1439. }
  1440. },
  1441. include_build_directory: false,
  1442. }
  1443. `,
  1444. ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
  1445. "srcs": `["impl.cpp"]`,
  1446. "use_libcrt": `select({
  1447. "//build/bazel/platforms/os_arch:android_arm": False,
  1448. "//build/bazel/platforms/os_arch:android_x86_64": False,
  1449. "//build/bazel/platforms/os_arch:darwin_arm64": True,
  1450. "//build/bazel/platforms/os_arch:darwin_x86_64": False,
  1451. "//build/bazel/platforms/os_arch:linux_bionic_x86_64": False,
  1452. "//build/bazel/platforms/os_arch:linux_glibc_x86_64": False,
  1453. "//build/bazel/platforms/os_arch:linux_musl_x86_64": False,
  1454. "//build/bazel/platforms/os_arch:windows_x86_64": False,
  1455. "//conditions:default": None,
  1456. })`,
  1457. }),
  1458. })
  1459. }
  1460. func TestCcLibraryStrip(t *testing.T) {
  1461. expectedTargets := []string{}
  1462. expectedTargets = append(expectedTargets, makeCcLibraryTargets("all", AttrNameToString{
  1463. "strip": `{
  1464. "all": True,
  1465. }`,
  1466. })...)
  1467. expectedTargets = append(expectedTargets, makeCcLibraryTargets("keep_symbols", AttrNameToString{
  1468. "strip": `{
  1469. "keep_symbols": True,
  1470. }`,
  1471. })...)
  1472. expectedTargets = append(expectedTargets, makeCcLibraryTargets("keep_symbols_and_debug_frame", AttrNameToString{
  1473. "strip": `{
  1474. "keep_symbols_and_debug_frame": True,
  1475. }`,
  1476. })...)
  1477. expectedTargets = append(expectedTargets, makeCcLibraryTargets("keep_symbols_list", AttrNameToString{
  1478. "strip": `{
  1479. "keep_symbols_list": ["symbol"],
  1480. }`,
  1481. })...)
  1482. expectedTargets = append(expectedTargets, makeCcLibraryTargets("none", AttrNameToString{
  1483. "strip": `{
  1484. "none": True,
  1485. }`,
  1486. })...)
  1487. expectedTargets = append(expectedTargets, makeCcLibraryTargets("nothing", AttrNameToString{})...)
  1488. runCcLibraryTestCase(t, Bp2buildTestCase{
  1489. Description: "cc_library strip args",
  1490. ModuleTypeUnderTest: "cc_library",
  1491. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  1492. Blueprint: soongCcLibraryPreamble + `
  1493. cc_library {
  1494. name: "nothing",
  1495. include_build_directory: false,
  1496. }
  1497. cc_library {
  1498. name: "keep_symbols",
  1499. strip: {
  1500. keep_symbols: true,
  1501. },
  1502. include_build_directory: false,
  1503. }
  1504. cc_library {
  1505. name: "keep_symbols_and_debug_frame",
  1506. strip: {
  1507. keep_symbols_and_debug_frame: true,
  1508. },
  1509. include_build_directory: false,
  1510. }
  1511. cc_library {
  1512. name: "none",
  1513. strip: {
  1514. none: true,
  1515. },
  1516. include_build_directory: false,
  1517. }
  1518. cc_library {
  1519. name: "keep_symbols_list",
  1520. strip: {
  1521. keep_symbols_list: ["symbol"],
  1522. },
  1523. include_build_directory: false,
  1524. }
  1525. cc_library {
  1526. name: "all",
  1527. strip: {
  1528. all: true,
  1529. },
  1530. include_build_directory: false,
  1531. }
  1532. `,
  1533. ExpectedBazelTargets: expectedTargets,
  1534. })
  1535. }
  1536. func TestCcLibraryStripWithArch(t *testing.T) {
  1537. runCcLibraryTestCase(t, Bp2buildTestCase{
  1538. Description: "cc_library strip args",
  1539. ModuleTypeUnderTest: "cc_library",
  1540. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  1541. Blueprint: soongCcLibraryPreamble + `
  1542. cc_library {
  1543. name: "multi-arch",
  1544. target: {
  1545. darwin: {
  1546. strip: {
  1547. keep_symbols_list: ["foo", "bar"]
  1548. }
  1549. },
  1550. },
  1551. arch: {
  1552. arm: {
  1553. strip: {
  1554. keep_symbols_and_debug_frame: true,
  1555. },
  1556. },
  1557. arm64: {
  1558. strip: {
  1559. keep_symbols: true,
  1560. },
  1561. },
  1562. },
  1563. include_build_directory: false,
  1564. }
  1565. `,
  1566. ExpectedBazelTargets: makeCcLibraryTargets("multi-arch", AttrNameToString{
  1567. "strip": `{
  1568. "keep_symbols": select({
  1569. "//build/bazel/platforms/arch:arm64": True,
  1570. "//conditions:default": None,
  1571. }),
  1572. "keep_symbols_and_debug_frame": select({
  1573. "//build/bazel/platforms/arch:arm": True,
  1574. "//conditions:default": None,
  1575. }),
  1576. "keep_symbols_list": select({
  1577. "//build/bazel/platforms/os:darwin": [
  1578. "foo",
  1579. "bar",
  1580. ],
  1581. "//conditions:default": [],
  1582. }),
  1583. }`,
  1584. }),
  1585. },
  1586. )
  1587. }
  1588. func TestCcLibrary_SystemSharedLibsRootEmpty(t *testing.T) {
  1589. runCcLibraryTestCase(t, Bp2buildTestCase{
  1590. Description: "cc_library system_shared_libs empty at root",
  1591. ModuleTypeUnderTest: "cc_library",
  1592. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  1593. Blueprint: soongCcLibraryPreamble + `
  1594. cc_library {
  1595. name: "root_empty",
  1596. system_shared_libs: [],
  1597. include_build_directory: false,
  1598. }
  1599. `,
  1600. ExpectedBazelTargets: makeCcLibraryTargets("root_empty", AttrNameToString{
  1601. "system_dynamic_deps": `[]`,
  1602. }),
  1603. },
  1604. )
  1605. }
  1606. func TestCcLibrary_SystemSharedLibsStaticEmpty(t *testing.T) {
  1607. runCcLibraryTestCase(t, Bp2buildTestCase{
  1608. Description: "cc_library system_shared_libs empty for static variant",
  1609. ModuleTypeUnderTest: "cc_library",
  1610. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  1611. Blueprint: soongCcLibraryPreamble + `
  1612. cc_library {
  1613. name: "static_empty",
  1614. static: {
  1615. system_shared_libs: [],
  1616. },
  1617. include_build_directory: false,
  1618. }
  1619. `,
  1620. ExpectedBazelTargets: []string{
  1621. MakeBazelTarget("cc_library_static", "static_empty_bp2build_cc_library_static", AttrNameToString{
  1622. "system_dynamic_deps": "[]",
  1623. }),
  1624. MakeBazelTarget("cc_library_shared", "static_empty", AttrNameToString{}),
  1625. },
  1626. })
  1627. }
  1628. func TestCcLibrary_SystemSharedLibsSharedEmpty(t *testing.T) {
  1629. runCcLibraryTestCase(t, Bp2buildTestCase{
  1630. Description: "cc_library system_shared_libs empty for shared variant",
  1631. ModuleTypeUnderTest: "cc_library",
  1632. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  1633. Blueprint: soongCcLibraryPreamble + `
  1634. cc_library {
  1635. name: "shared_empty",
  1636. shared: {
  1637. system_shared_libs: [],
  1638. },
  1639. include_build_directory: false,
  1640. }
  1641. `,
  1642. ExpectedBazelTargets: []string{
  1643. MakeBazelTarget("cc_library_static", "shared_empty_bp2build_cc_library_static", AttrNameToString{}),
  1644. MakeBazelTarget("cc_library_shared", "shared_empty", AttrNameToString{
  1645. "system_dynamic_deps": "[]",
  1646. }),
  1647. },
  1648. })
  1649. }
  1650. func TestCcLibrary_SystemSharedLibsSharedBionicEmpty(t *testing.T) {
  1651. runCcLibraryTestCase(t, Bp2buildTestCase{
  1652. Description: "cc_library system_shared_libs empty for shared, bionic variant",
  1653. ModuleTypeUnderTest: "cc_library",
  1654. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  1655. Blueprint: soongCcLibraryPreamble + `
  1656. cc_library {
  1657. name: "shared_empty",
  1658. target: {
  1659. bionic: {
  1660. shared: {
  1661. system_shared_libs: [],
  1662. }
  1663. }
  1664. },
  1665. include_build_directory: false,
  1666. }
  1667. `,
  1668. ExpectedBazelTargets: []string{
  1669. MakeBazelTarget("cc_library_static", "shared_empty_bp2build_cc_library_static", AttrNameToString{}),
  1670. MakeBazelTarget("cc_library_shared", "shared_empty", AttrNameToString{
  1671. "system_dynamic_deps": "[]",
  1672. }),
  1673. },
  1674. })
  1675. }
  1676. func TestCcLibrary_SystemSharedLibsLinuxBionicEmpty(t *testing.T) {
  1677. // Note that this behavior is technically incorrect (it's a simplification).
  1678. // The correct behavior would be if bp2build wrote `system_dynamic_deps = []`
  1679. // only for linux_bionic, but `android` had `["libc", "libdl", "libm"].
  1680. // b/195791252 tracks the fix.
  1681. runCcLibraryTestCase(t, Bp2buildTestCase{
  1682. Description: "cc_library system_shared_libs empty for linux_bionic variant",
  1683. ModuleTypeUnderTest: "cc_library",
  1684. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  1685. Blueprint: soongCcLibraryPreamble + `
  1686. cc_library {
  1687. name: "target_linux_bionic_empty",
  1688. target: {
  1689. linux_bionic: {
  1690. system_shared_libs: [],
  1691. },
  1692. },
  1693. include_build_directory: false,
  1694. }
  1695. `,
  1696. ExpectedBazelTargets: makeCcLibraryTargets("target_linux_bionic_empty", AttrNameToString{
  1697. "system_dynamic_deps": `[]`,
  1698. }),
  1699. },
  1700. )
  1701. }
  1702. func TestCcLibrary_SystemSharedLibsBionicEmpty(t *testing.T) {
  1703. runCcLibraryTestCase(t, Bp2buildTestCase{
  1704. Description: "cc_library system_shared_libs empty for bionic variant",
  1705. ModuleTypeUnderTest: "cc_library",
  1706. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  1707. Blueprint: soongCcLibraryPreamble + `
  1708. cc_library {
  1709. name: "target_bionic_empty",
  1710. target: {
  1711. bionic: {
  1712. system_shared_libs: [],
  1713. },
  1714. },
  1715. include_build_directory: false,
  1716. }
  1717. `,
  1718. ExpectedBazelTargets: makeCcLibraryTargets("target_bionic_empty", AttrNameToString{
  1719. "system_dynamic_deps": `[]`,
  1720. }),
  1721. },
  1722. )
  1723. }
  1724. func TestCcLibrary_SystemSharedLibsSharedAndRoot(t *testing.T) {
  1725. runCcLibraryTestCase(t, Bp2buildTestCase{
  1726. Description: "cc_library system_shared_libs set for shared and root",
  1727. ModuleTypeUnderTest: "cc_library",
  1728. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  1729. Blueprint: soongCcLibraryPreamble + `
  1730. cc_library {
  1731. name: "libc",
  1732. bazel_module: { bp2build_available: false },
  1733. }
  1734. cc_library {
  1735. name: "libm",
  1736. bazel_module: { bp2build_available: false },
  1737. }
  1738. cc_library {
  1739. name: "foo",
  1740. system_shared_libs: ["libc"],
  1741. shared: {
  1742. system_shared_libs: ["libm"],
  1743. },
  1744. include_build_directory: false,
  1745. }
  1746. `,
  1747. ExpectedBazelTargets: []string{
  1748. MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
  1749. "system_dynamic_deps": `[":libc"]`,
  1750. }),
  1751. MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
  1752. "system_dynamic_deps": `[
  1753. ":libc",
  1754. ":libm",
  1755. ]`,
  1756. }),
  1757. },
  1758. })
  1759. }
  1760. func TestCcLibraryOsSelects(t *testing.T) {
  1761. runCcLibraryTestCase(t, Bp2buildTestCase{
  1762. Description: "cc_library - selects for all os targets",
  1763. ModuleTypeUnderTest: "cc_library",
  1764. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  1765. Filesystem: map[string]string{},
  1766. Blueprint: soongCcLibraryPreamble + `
  1767. cc_library {
  1768. name: "foo-lib",
  1769. srcs: ["base.cpp"],
  1770. target: {
  1771. android: {
  1772. srcs: ["android.cpp"],
  1773. },
  1774. linux: {
  1775. srcs: ["linux.cpp"],
  1776. },
  1777. linux_glibc: {
  1778. srcs: ["linux_glibc.cpp"],
  1779. },
  1780. darwin: {
  1781. srcs: ["darwin.cpp"],
  1782. },
  1783. bionic: {
  1784. srcs: ["bionic.cpp"],
  1785. },
  1786. linux_musl: {
  1787. srcs: ["linux_musl.cpp"],
  1788. },
  1789. windows: {
  1790. srcs: ["windows.cpp"],
  1791. },
  1792. },
  1793. include_build_directory: false,
  1794. }
  1795. `,
  1796. ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
  1797. "srcs": `["base.cpp"] + select({
  1798. "//build/bazel/platforms/os:android": [
  1799. "linux.cpp",
  1800. "bionic.cpp",
  1801. "android.cpp",
  1802. ],
  1803. "//build/bazel/platforms/os:darwin": ["darwin.cpp"],
  1804. "//build/bazel/platforms/os:linux": [
  1805. "linux.cpp",
  1806. "linux_glibc.cpp",
  1807. ],
  1808. "//build/bazel/platforms/os:linux_bionic": [
  1809. "linux.cpp",
  1810. "bionic.cpp",
  1811. ],
  1812. "//build/bazel/platforms/os:linux_musl": [
  1813. "linux.cpp",
  1814. "linux_musl.cpp",
  1815. ],
  1816. "//build/bazel/platforms/os:windows": ["windows.cpp"],
  1817. "//conditions:default": [],
  1818. })`,
  1819. }),
  1820. },
  1821. )
  1822. }
  1823. func TestLibcryptoHashInjection(t *testing.T) {
  1824. runCcLibraryTestCase(t, Bp2buildTestCase{
  1825. Description: "cc_library - libcrypto hash injection",
  1826. ModuleTypeUnderTest: "cc_library",
  1827. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  1828. Filesystem: map[string]string{},
  1829. Blueprint: soongCcLibraryPreamble + `
  1830. cc_library {
  1831. name: "libcrypto",
  1832. target: {
  1833. android: {
  1834. inject_bssl_hash: true,
  1835. },
  1836. },
  1837. include_build_directory: false,
  1838. }
  1839. `,
  1840. ExpectedBazelTargets: makeCcLibraryTargets("libcrypto", AttrNameToString{
  1841. "inject_bssl_hash": `select({
  1842. "//build/bazel/platforms/os:android": True,
  1843. "//conditions:default": None,
  1844. })`,
  1845. }),
  1846. },
  1847. )
  1848. }
  1849. func TestCcLibraryCppStdWithGnuExtensions_ConvertsToFeatureAttr(t *testing.T) {
  1850. type testCase struct {
  1851. cpp_std string
  1852. c_std string
  1853. gnu_extensions string
  1854. bazel_cpp_std string
  1855. bazel_c_std string
  1856. }
  1857. testCases := []testCase{
  1858. // Existing usages of cpp_std in AOSP are:
  1859. // experimental, c++11, c++17, c++2a, c++98, gnu++11, gnu++17
  1860. //
  1861. // not set, only emit if gnu_extensions is disabled. the default (gnu+17
  1862. // is set in the toolchain.)
  1863. {cpp_std: "", gnu_extensions: "", bazel_cpp_std: ""},
  1864. {cpp_std: "", gnu_extensions: "false", bazel_cpp_std: "cpp_std_default_no_gnu", bazel_c_std: "c_std_default_no_gnu"},
  1865. {cpp_std: "", gnu_extensions: "true", bazel_cpp_std: ""},
  1866. // experimental defaults to gnu++2a
  1867. {cpp_std: "experimental", gnu_extensions: "", bazel_cpp_std: "cpp_std_experimental"},
  1868. {cpp_std: "experimental", gnu_extensions: "false", bazel_cpp_std: "cpp_std_experimental_no_gnu", bazel_c_std: "c_std_default_no_gnu"},
  1869. {cpp_std: "experimental", gnu_extensions: "true", bazel_cpp_std: "cpp_std_experimental"},
  1870. // Explicitly setting a c++ std does not use replace gnu++ std even if
  1871. // gnu_extensions is true.
  1872. // "c++11",
  1873. {cpp_std: "c++11", gnu_extensions: "", bazel_cpp_std: "c++11"},
  1874. {cpp_std: "c++11", gnu_extensions: "false", bazel_cpp_std: "c++11", bazel_c_std: "c_std_default_no_gnu"},
  1875. {cpp_std: "c++11", gnu_extensions: "true", bazel_cpp_std: "c++11"},
  1876. // "c++17",
  1877. {cpp_std: "c++17", gnu_extensions: "", bazel_cpp_std: "c++17"},
  1878. {cpp_std: "c++17", gnu_extensions: "false", bazel_cpp_std: "c++17", bazel_c_std: "c_std_default_no_gnu"},
  1879. {cpp_std: "c++17", gnu_extensions: "true", bazel_cpp_std: "c++17"},
  1880. // "c++2a",
  1881. {cpp_std: "c++2a", gnu_extensions: "", bazel_cpp_std: "c++2a"},
  1882. {cpp_std: "c++2a", gnu_extensions: "false", bazel_cpp_std: "c++2a", bazel_c_std: "c_std_default_no_gnu"},
  1883. {cpp_std: "c++2a", gnu_extensions: "true", bazel_cpp_std: "c++2a"},
  1884. // "c++98",
  1885. {cpp_std: "c++98", gnu_extensions: "", bazel_cpp_std: "c++98"},
  1886. {cpp_std: "c++98", gnu_extensions: "false", bazel_cpp_std: "c++98", bazel_c_std: "c_std_default_no_gnu"},
  1887. {cpp_std: "c++98", gnu_extensions: "true", bazel_cpp_std: "c++98"},
  1888. // gnu++ is replaced with c++ if gnu_extensions is explicitly false.
  1889. // "gnu++11",
  1890. {cpp_std: "gnu++11", gnu_extensions: "", bazel_cpp_std: "gnu++11"},
  1891. {cpp_std: "gnu++11", gnu_extensions: "false", bazel_cpp_std: "c++11", bazel_c_std: "c_std_default_no_gnu"},
  1892. {cpp_std: "gnu++11", gnu_extensions: "true", bazel_cpp_std: "gnu++11"},
  1893. // "gnu++17",
  1894. {cpp_std: "gnu++17", gnu_extensions: "", bazel_cpp_std: "gnu++17"},
  1895. {cpp_std: "gnu++17", gnu_extensions: "false", bazel_cpp_std: "c++17", bazel_c_std: "c_std_default_no_gnu"},
  1896. {cpp_std: "gnu++17", gnu_extensions: "true", bazel_cpp_std: "gnu++17"},
  1897. // some c_std test cases
  1898. {c_std: "experimental", gnu_extensions: "", bazel_c_std: "c_std_experimental"},
  1899. {c_std: "experimental", gnu_extensions: "false", bazel_cpp_std: "cpp_std_default_no_gnu", bazel_c_std: "c_std_experimental_no_gnu"},
  1900. {c_std: "experimental", gnu_extensions: "true", bazel_c_std: "c_std_experimental"},
  1901. {c_std: "gnu11", cpp_std: "gnu++17", gnu_extensions: "", bazel_cpp_std: "gnu++17", bazel_c_std: "gnu11"},
  1902. {c_std: "gnu11", cpp_std: "gnu++17", gnu_extensions: "false", bazel_cpp_std: "c++17", bazel_c_std: "c11"},
  1903. {c_std: "gnu11", cpp_std: "gnu++17", gnu_extensions: "true", bazel_cpp_std: "gnu++17", bazel_c_std: "gnu11"},
  1904. }
  1905. for i, tc := range testCases {
  1906. name := fmt.Sprintf("cpp std: %q, c std: %q, gnu_extensions: %q", tc.cpp_std, tc.c_std, tc.gnu_extensions)
  1907. t.Run(name, func(t *testing.T) {
  1908. name_prefix := fmt.Sprintf("a_%v", i)
  1909. cppStdProp := ""
  1910. if tc.cpp_std != "" {
  1911. cppStdProp = fmt.Sprintf(" cpp_std: \"%s\",", tc.cpp_std)
  1912. }
  1913. cStdProp := ""
  1914. if tc.c_std != "" {
  1915. cStdProp = fmt.Sprintf(" c_std: \"%s\",", tc.c_std)
  1916. }
  1917. gnuExtensionsProp := ""
  1918. if tc.gnu_extensions != "" {
  1919. gnuExtensionsProp = fmt.Sprintf(" gnu_extensions: %s,", tc.gnu_extensions)
  1920. }
  1921. attrs := AttrNameToString{}
  1922. if tc.bazel_cpp_std != "" {
  1923. attrs["cpp_std"] = fmt.Sprintf(`"%s"`, tc.bazel_cpp_std)
  1924. }
  1925. if tc.bazel_c_std != "" {
  1926. attrs["c_std"] = fmt.Sprintf(`"%s"`, tc.bazel_c_std)
  1927. }
  1928. runCcLibraryTestCase(t, Bp2buildTestCase{
  1929. Description: fmt.Sprintf(
  1930. "cc_library with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions),
  1931. ModuleTypeUnderTest: "cc_library",
  1932. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  1933. Blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
  1934. cc_library {
  1935. name: "%s_full",
  1936. %s // cpp_std: *string
  1937. %s // c_std: *string
  1938. %s // gnu_extensions: *bool
  1939. include_build_directory: false,
  1940. }
  1941. `, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
  1942. ExpectedBazelTargets: makeCcLibraryTargets(name_prefix+"_full", attrs),
  1943. })
  1944. runCcLibraryStaticTestCase(t, Bp2buildTestCase{
  1945. Description: fmt.Sprintf(
  1946. "cc_library_static with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions),
  1947. ModuleTypeUnderTest: "cc_library_static",
  1948. ModuleTypeUnderTestFactory: cc.LibraryStaticFactory,
  1949. Blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
  1950. cc_library_static {
  1951. name: "%s_static",
  1952. %s // cpp_std: *string
  1953. %s // c_std: *string
  1954. %s // gnu_extensions: *bool
  1955. include_build_directory: false,
  1956. }
  1957. `, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
  1958. ExpectedBazelTargets: []string{
  1959. MakeBazelTarget("cc_library_static", name_prefix+"_static", attrs),
  1960. },
  1961. })
  1962. runCcLibrarySharedTestCase(t, Bp2buildTestCase{
  1963. Description: fmt.Sprintf(
  1964. "cc_library_shared with cpp_std: %s and gnu_extensions: %s", tc.cpp_std, tc.gnu_extensions),
  1965. ModuleTypeUnderTest: "cc_library_shared",
  1966. ModuleTypeUnderTestFactory: cc.LibrarySharedFactory,
  1967. Blueprint: soongCcLibraryPreamble + fmt.Sprintf(`
  1968. cc_library_shared {
  1969. name: "%s_shared",
  1970. %s // cpp_std: *string
  1971. %s // c_std: *string
  1972. %s // gnu_extensions: *bool
  1973. include_build_directory: false,
  1974. }
  1975. `, name_prefix, cppStdProp, cStdProp, gnuExtensionsProp),
  1976. ExpectedBazelTargets: []string{
  1977. MakeBazelTarget("cc_library_shared", name_prefix+"_shared", attrs),
  1978. },
  1979. })
  1980. })
  1981. }
  1982. }
  1983. func TestCcLibraryProtoSimple(t *testing.T) {
  1984. runCcLibraryTestCase(t, Bp2buildTestCase{
  1985. ModuleTypeUnderTest: "cc_library",
  1986. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  1987. Blueprint: soongCcProtoPreamble + `cc_library {
  1988. name: "foo",
  1989. srcs: ["foo.proto"],
  1990. include_build_directory: false,
  1991. }`,
  1992. ExpectedBazelTargets: []string{
  1993. MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
  1994. "srcs": `["foo.proto"]`,
  1995. }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
  1996. "deps": `[":foo_proto"]`,
  1997. }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
  1998. "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
  1999. "deps": `[":libprotobuf-cpp-lite"]`,
  2000. }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
  2001. "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
  2002. "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
  2003. }),
  2004. },
  2005. })
  2006. }
  2007. func TestCcLibraryProtoNoCanonicalPathFromRoot(t *testing.T) {
  2008. runCcLibraryTestCase(t, Bp2buildTestCase{
  2009. ModuleTypeUnderTest: "cc_library",
  2010. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  2011. Blueprint: soongCcProtoPreamble + `cc_library {
  2012. name: "foo",
  2013. srcs: ["foo.proto"],
  2014. proto: { canonical_path_from_root: false},
  2015. include_build_directory: false,
  2016. }`,
  2017. ExpectedBazelTargets: []string{
  2018. MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
  2019. "srcs": `["foo.proto"]`,
  2020. "strip_import_prefix": `""`,
  2021. }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
  2022. "deps": `[":foo_proto"]`,
  2023. }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
  2024. "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
  2025. "deps": `[":libprotobuf-cpp-lite"]`,
  2026. }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
  2027. "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
  2028. "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
  2029. }),
  2030. },
  2031. })
  2032. }
  2033. func TestCcLibraryProtoExplicitCanonicalPathFromRoot(t *testing.T) {
  2034. runCcLibraryTestCase(t, Bp2buildTestCase{
  2035. ModuleTypeUnderTest: "cc_library",
  2036. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  2037. Blueprint: soongCcProtoPreamble + `cc_library {
  2038. name: "foo",
  2039. srcs: ["foo.proto"],
  2040. proto: { canonical_path_from_root: true},
  2041. include_build_directory: false,
  2042. }`,
  2043. ExpectedBazelTargets: []string{
  2044. MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
  2045. "srcs": `["foo.proto"]`,
  2046. }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
  2047. "deps": `[":foo_proto"]`,
  2048. }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
  2049. "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
  2050. "deps": `[":libprotobuf-cpp-lite"]`,
  2051. }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
  2052. "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
  2053. "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
  2054. }),
  2055. },
  2056. })
  2057. }
  2058. func TestCcLibraryProtoFull(t *testing.T) {
  2059. runCcLibraryTestCase(t, Bp2buildTestCase{
  2060. ModuleTypeUnderTest: "cc_library",
  2061. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  2062. Blueprint: soongCcProtoPreamble + `cc_library {
  2063. name: "foo",
  2064. srcs: ["foo.proto"],
  2065. proto: {
  2066. type: "full",
  2067. },
  2068. include_build_directory: false,
  2069. }`,
  2070. ExpectedBazelTargets: []string{
  2071. MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
  2072. "srcs": `["foo.proto"]`,
  2073. }), MakeBazelTarget("cc_proto_library", "foo_cc_proto", AttrNameToString{
  2074. "deps": `[":foo_proto"]`,
  2075. }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
  2076. "implementation_whole_archive_deps": `[":foo_cc_proto"]`,
  2077. "deps": `[":libprotobuf-cpp-full"]`,
  2078. }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
  2079. "dynamic_deps": `[":libprotobuf-cpp-full"]`,
  2080. "implementation_whole_archive_deps": `[":foo_cc_proto"]`,
  2081. }),
  2082. },
  2083. })
  2084. }
  2085. func TestCcLibraryProtoLite(t *testing.T) {
  2086. runCcLibraryTestCase(t, Bp2buildTestCase{
  2087. ModuleTypeUnderTest: "cc_library",
  2088. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  2089. Blueprint: soongCcProtoPreamble + `cc_library {
  2090. name: "foo",
  2091. srcs: ["foo.proto"],
  2092. proto: {
  2093. type: "lite",
  2094. },
  2095. include_build_directory: false,
  2096. }`,
  2097. ExpectedBazelTargets: []string{
  2098. MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
  2099. "srcs": `["foo.proto"]`,
  2100. }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
  2101. "deps": `[":foo_proto"]`,
  2102. }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
  2103. "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
  2104. "deps": `[":libprotobuf-cpp-lite"]`,
  2105. }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
  2106. "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
  2107. "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
  2108. }),
  2109. },
  2110. })
  2111. }
  2112. func TestCcLibraryProtoExportHeaders(t *testing.T) {
  2113. runCcLibraryTestCase(t, Bp2buildTestCase{
  2114. ModuleTypeUnderTest: "cc_library",
  2115. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  2116. Blueprint: soongCcProtoPreamble + `cc_library {
  2117. name: "foo",
  2118. srcs: ["foo.proto"],
  2119. proto: {
  2120. export_proto_headers: true,
  2121. },
  2122. include_build_directory: false,
  2123. }`,
  2124. ExpectedBazelTargets: []string{
  2125. MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
  2126. "srcs": `["foo.proto"]`,
  2127. }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
  2128. "deps": `[":foo_proto"]`,
  2129. }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
  2130. "deps": `[":libprotobuf-cpp-lite"]`,
  2131. "whole_archive_deps": `[":foo_cc_proto_lite"]`,
  2132. }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
  2133. "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
  2134. "whole_archive_deps": `[":foo_cc_proto_lite"]`,
  2135. }),
  2136. },
  2137. })
  2138. }
  2139. func TestCcLibraryProtoIncludeDirs(t *testing.T) {
  2140. runCcLibraryTestCase(t, Bp2buildTestCase{
  2141. ModuleTypeUnderTest: "cc_library",
  2142. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  2143. Blueprint: soongCcProtoPreamble + `cc_library {
  2144. name: "foo",
  2145. srcs: ["foo.proto"],
  2146. proto: {
  2147. include_dirs: ["external/protobuf/src"],
  2148. },
  2149. include_build_directory: false,
  2150. }`,
  2151. ExpectedBazelTargets: []string{
  2152. MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
  2153. "srcs": `["foo.proto"]`,
  2154. "deps": `["//external/protobuf:libprotobuf-proto"]`,
  2155. }), MakeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
  2156. "deps": `[":foo_proto"]`,
  2157. }), MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
  2158. "deps": `[":libprotobuf-cpp-lite"]`,
  2159. "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
  2160. }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
  2161. "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
  2162. "implementation_whole_archive_deps": `[":foo_cc_proto_lite"]`,
  2163. }),
  2164. },
  2165. })
  2166. }
  2167. func TestCcLibraryProtoIncludeDirsUnknown(t *testing.T) {
  2168. runCcLibraryTestCase(t, Bp2buildTestCase{
  2169. ModuleTypeUnderTest: "cc_library",
  2170. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  2171. Blueprint: soongCcProtoPreamble + `cc_library {
  2172. name: "foo",
  2173. srcs: ["foo.proto"],
  2174. proto: {
  2175. include_dirs: ["external/protobuf/abc"],
  2176. },
  2177. include_build_directory: false,
  2178. }`,
  2179. ExpectedErr: fmt.Errorf("module \"foo\": Could not find the proto_library target for include dir: external/protobuf/abc"),
  2180. })
  2181. }
  2182. func TestCcLibraryConvertedProtoFilegroups(t *testing.T) {
  2183. runCcLibraryTestCase(t, Bp2buildTestCase{
  2184. ModuleTypeUnderTest: "cc_library",
  2185. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  2186. Blueprint: soongCcProtoPreamble + `
  2187. filegroup {
  2188. name: "a_fg_proto",
  2189. srcs: ["a_fg.proto"],
  2190. }
  2191. cc_library {
  2192. name: "a",
  2193. srcs: [
  2194. ":a_fg_proto",
  2195. "a.proto",
  2196. ],
  2197. proto: {
  2198. export_proto_headers: true,
  2199. },
  2200. include_build_directory: false,
  2201. }`,
  2202. ExpectedBazelTargets: []string{
  2203. MakeBazelTarget("proto_library", "a_proto", AttrNameToString{
  2204. "deps": `[":a_fg_proto_bp2build_converted"]`,
  2205. "srcs": `["a.proto"]`,
  2206. }), MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
  2207. "deps": `[
  2208. ":a_fg_proto_bp2build_converted",
  2209. ":a_proto",
  2210. ]`,
  2211. }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
  2212. "deps": `[":libprotobuf-cpp-lite"]`,
  2213. "whole_archive_deps": `[":a_cc_proto_lite"]`,
  2214. }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
  2215. "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
  2216. "whole_archive_deps": `[":a_cc_proto_lite"]`,
  2217. }), MakeBazelTargetNoRestrictions("proto_library", "a_fg_proto_bp2build_converted", AttrNameToString{
  2218. "srcs": `["a_fg.proto"]`,
  2219. "tags": `["manual"]`,
  2220. }), MakeBazelTargetNoRestrictions("filegroup", "a_fg_proto", AttrNameToString{
  2221. "srcs": `["a_fg.proto"]`,
  2222. }),
  2223. },
  2224. })
  2225. }
  2226. func TestCcLibraryConvertedProtoFilegroupsNoProtoFiles(t *testing.T) {
  2227. runCcLibraryTestCase(t, Bp2buildTestCase{
  2228. ModuleTypeUnderTest: "cc_library",
  2229. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  2230. Blueprint: soongCcProtoPreamble + `
  2231. filegroup {
  2232. name: "a_fg_proto",
  2233. srcs: ["a_fg.proto"],
  2234. }
  2235. cc_library {
  2236. name: "a",
  2237. srcs: [
  2238. ":a_fg_proto",
  2239. ],
  2240. proto: {
  2241. export_proto_headers: true,
  2242. },
  2243. include_build_directory: false,
  2244. }`,
  2245. ExpectedBazelTargets: []string{
  2246. MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
  2247. "deps": `[":a_fg_proto_bp2build_converted"]`,
  2248. }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
  2249. "deps": `[":libprotobuf-cpp-lite"]`,
  2250. "whole_archive_deps": `[":a_cc_proto_lite"]`,
  2251. }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
  2252. "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
  2253. "whole_archive_deps": `[":a_cc_proto_lite"]`,
  2254. }), MakeBazelTargetNoRestrictions("proto_library", "a_fg_proto_bp2build_converted", AttrNameToString{
  2255. "srcs": `["a_fg.proto"]`,
  2256. "tags": `["manual"]`,
  2257. }), MakeBazelTargetNoRestrictions("filegroup", "a_fg_proto", AttrNameToString{
  2258. "srcs": `["a_fg.proto"]`,
  2259. }),
  2260. },
  2261. })
  2262. }
  2263. func TestCcLibraryExternalConvertedProtoFilegroups(t *testing.T) {
  2264. runCcLibraryTestCase(t, Bp2buildTestCase{
  2265. ModuleTypeUnderTest: "cc_library",
  2266. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  2267. Filesystem: map[string]string{
  2268. "path/to/A/Android.bp": `
  2269. filegroup {
  2270. name: "a_fg_proto",
  2271. srcs: ["a_fg.proto"],
  2272. }`,
  2273. },
  2274. Blueprint: soongCcProtoPreamble + `
  2275. cc_library {
  2276. name: "a",
  2277. srcs: [
  2278. ":a_fg_proto",
  2279. "a.proto",
  2280. ],
  2281. proto: {
  2282. export_proto_headers: true,
  2283. },
  2284. include_build_directory: false,
  2285. }`,
  2286. ExpectedBazelTargets: []string{
  2287. MakeBazelTarget("proto_library", "a_proto", AttrNameToString{
  2288. "deps": `["//path/to/A:a_fg_proto_bp2build_converted"]`,
  2289. "srcs": `["a.proto"]`,
  2290. }), MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
  2291. "deps": `[
  2292. "//path/to/A:a_fg_proto_bp2build_converted",
  2293. ":a_proto",
  2294. ]`,
  2295. }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
  2296. "deps": `[":libprotobuf-cpp-lite"]`,
  2297. "whole_archive_deps": `[":a_cc_proto_lite"]`,
  2298. }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
  2299. "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
  2300. "whole_archive_deps": `[":a_cc_proto_lite"]`,
  2301. }),
  2302. },
  2303. })
  2304. }
  2305. func TestCcLibraryProtoFilegroups(t *testing.T) {
  2306. runCcLibraryTestCase(t, Bp2buildTestCase{
  2307. ModuleTypeUnderTest: "cc_library",
  2308. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  2309. Blueprint: soongCcProtoPreamble +
  2310. simpleModuleDoNotConvertBp2build("filegroup", "a_fg_proto") +
  2311. simpleModuleDoNotConvertBp2build("filegroup", "b_protos") +
  2312. simpleModuleDoNotConvertBp2build("filegroup", "c-proto-srcs") +
  2313. simpleModuleDoNotConvertBp2build("filegroup", "proto-srcs-d") + `
  2314. cc_library {
  2315. name: "a",
  2316. srcs: [":a_fg_proto"],
  2317. proto: {
  2318. export_proto_headers: true,
  2319. },
  2320. include_build_directory: false,
  2321. }
  2322. cc_library {
  2323. name: "b",
  2324. srcs: [":b_protos"],
  2325. proto: {
  2326. export_proto_headers: true,
  2327. },
  2328. include_build_directory: false,
  2329. }
  2330. cc_library {
  2331. name: "c",
  2332. srcs: [":c-proto-srcs"],
  2333. proto: {
  2334. export_proto_headers: true,
  2335. },
  2336. include_build_directory: false,
  2337. }
  2338. cc_library {
  2339. name: "d",
  2340. srcs: [":proto-srcs-d"],
  2341. proto: {
  2342. export_proto_headers: true,
  2343. },
  2344. include_build_directory: false,
  2345. }`,
  2346. ExpectedBazelTargets: []string{
  2347. MakeBazelTarget("proto_library", "a_proto", AttrNameToString{
  2348. "srcs": `[":a_fg_proto"]`,
  2349. }), MakeBazelTarget("cc_lite_proto_library", "a_cc_proto_lite", AttrNameToString{
  2350. "deps": `[":a_proto"]`,
  2351. }), MakeBazelTarget("cc_library_static", "a_bp2build_cc_library_static", AttrNameToString{
  2352. "deps": `[":libprotobuf-cpp-lite"]`,
  2353. "whole_archive_deps": `[":a_cc_proto_lite"]`,
  2354. "srcs": `[":a_fg_proto_cpp_srcs"]`,
  2355. "srcs_as": `[":a_fg_proto_as_srcs"]`,
  2356. "srcs_c": `[":a_fg_proto_c_srcs"]`,
  2357. }), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
  2358. "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
  2359. "whole_archive_deps": `[":a_cc_proto_lite"]`,
  2360. "srcs": `[":a_fg_proto_cpp_srcs"]`,
  2361. "srcs_as": `[":a_fg_proto_as_srcs"]`,
  2362. "srcs_c": `[":a_fg_proto_c_srcs"]`,
  2363. }), MakeBazelTarget("proto_library", "b_proto", AttrNameToString{
  2364. "srcs": `[":b_protos"]`,
  2365. }), MakeBazelTarget("cc_lite_proto_library", "b_cc_proto_lite", AttrNameToString{
  2366. "deps": `[":b_proto"]`,
  2367. }), MakeBazelTarget("cc_library_static", "b_bp2build_cc_library_static", AttrNameToString{
  2368. "deps": `[":libprotobuf-cpp-lite"]`,
  2369. "whole_archive_deps": `[":b_cc_proto_lite"]`,
  2370. "srcs": `[":b_protos_cpp_srcs"]`,
  2371. "srcs_as": `[":b_protos_as_srcs"]`,
  2372. "srcs_c": `[":b_protos_c_srcs"]`,
  2373. }), MakeBazelTarget("cc_library_shared", "b", AttrNameToString{
  2374. "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
  2375. "whole_archive_deps": `[":b_cc_proto_lite"]`,
  2376. "srcs": `[":b_protos_cpp_srcs"]`,
  2377. "srcs_as": `[":b_protos_as_srcs"]`,
  2378. "srcs_c": `[":b_protos_c_srcs"]`,
  2379. }), MakeBazelTarget("proto_library", "c_proto", AttrNameToString{
  2380. "srcs": `[":c-proto-srcs"]`,
  2381. }), MakeBazelTarget("cc_lite_proto_library", "c_cc_proto_lite", AttrNameToString{
  2382. "deps": `[":c_proto"]`,
  2383. }), MakeBazelTarget("cc_library_static", "c_bp2build_cc_library_static", AttrNameToString{
  2384. "deps": `[":libprotobuf-cpp-lite"]`,
  2385. "whole_archive_deps": `[":c_cc_proto_lite"]`,
  2386. "srcs": `[":c-proto-srcs_cpp_srcs"]`,
  2387. "srcs_as": `[":c-proto-srcs_as_srcs"]`,
  2388. "srcs_c": `[":c-proto-srcs_c_srcs"]`,
  2389. }), MakeBazelTarget("cc_library_shared", "c", AttrNameToString{
  2390. "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
  2391. "whole_archive_deps": `[":c_cc_proto_lite"]`,
  2392. "srcs": `[":c-proto-srcs_cpp_srcs"]`,
  2393. "srcs_as": `[":c-proto-srcs_as_srcs"]`,
  2394. "srcs_c": `[":c-proto-srcs_c_srcs"]`,
  2395. }), MakeBazelTarget("proto_library", "d_proto", AttrNameToString{
  2396. "srcs": `[":proto-srcs-d"]`,
  2397. }), MakeBazelTarget("cc_lite_proto_library", "d_cc_proto_lite", AttrNameToString{
  2398. "deps": `[":d_proto"]`,
  2399. }), MakeBazelTarget("cc_library_static", "d_bp2build_cc_library_static", AttrNameToString{
  2400. "deps": `[":libprotobuf-cpp-lite"]`,
  2401. "whole_archive_deps": `[":d_cc_proto_lite"]`,
  2402. "srcs": `[":proto-srcs-d_cpp_srcs"]`,
  2403. "srcs_as": `[":proto-srcs-d_as_srcs"]`,
  2404. "srcs_c": `[":proto-srcs-d_c_srcs"]`,
  2405. }), MakeBazelTarget("cc_library_shared", "d", AttrNameToString{
  2406. "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
  2407. "whole_archive_deps": `[":d_cc_proto_lite"]`,
  2408. "srcs": `[":proto-srcs-d_cpp_srcs"]`,
  2409. "srcs_as": `[":proto-srcs-d_as_srcs"]`,
  2410. "srcs_c": `[":proto-srcs-d_c_srcs"]`,
  2411. }),
  2412. },
  2413. })
  2414. }
  2415. func TestCcLibraryDisabledArchAndTarget(t *testing.T) {
  2416. runCcLibraryTestCase(t, Bp2buildTestCase{
  2417. ModuleTypeUnderTest: "cc_library",
  2418. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  2419. Blueprint: soongCcProtoPreamble + `cc_library {
  2420. name: "foo",
  2421. srcs: ["foo.cpp"],
  2422. host_supported: true,
  2423. target: {
  2424. darwin: {
  2425. enabled: false,
  2426. },
  2427. windows: {
  2428. enabled: false,
  2429. },
  2430. linux_glibc_x86: {
  2431. enabled: false,
  2432. },
  2433. },
  2434. include_build_directory: false,
  2435. }`,
  2436. ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
  2437. "srcs": `["foo.cpp"]`,
  2438. "target_compatible_with": `select({
  2439. "//build/bazel/platforms/os_arch:darwin_arm64": ["@platforms//:incompatible"],
  2440. "//build/bazel/platforms/os_arch:darwin_x86_64": ["@platforms//:incompatible"],
  2441. "//build/bazel/platforms/os_arch:linux_glibc_x86": ["@platforms//:incompatible"],
  2442. "//build/bazel/platforms/os_arch:windows_x86": ["@platforms//:incompatible"],
  2443. "//build/bazel/platforms/os_arch:windows_x86_64": ["@platforms//:incompatible"],
  2444. "//conditions:default": [],
  2445. })`,
  2446. }),
  2447. })
  2448. }
  2449. func TestCcLibraryDisabledArchAndTargetWithDefault(t *testing.T) {
  2450. runCcLibraryTestCase(t, Bp2buildTestCase{
  2451. ModuleTypeUnderTest: "cc_library",
  2452. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  2453. Blueprint: soongCcProtoPreamble + `cc_library {
  2454. name: "foo",
  2455. srcs: ["foo.cpp"],
  2456. enabled: false,
  2457. host_supported: true,
  2458. target: {
  2459. darwin: {
  2460. enabled: true,
  2461. },
  2462. windows: {
  2463. enabled: false,
  2464. },
  2465. linux_glibc_x86: {
  2466. enabled: false,
  2467. },
  2468. },
  2469. include_build_directory: false,
  2470. }`,
  2471. ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
  2472. "srcs": `["foo.cpp"]`,
  2473. "target_compatible_with": `select({
  2474. "//build/bazel/platforms/os_arch:darwin_arm64": [],
  2475. "//build/bazel/platforms/os_arch:darwin_x86_64": [],
  2476. "//conditions:default": ["@platforms//:incompatible"],
  2477. })`,
  2478. }),
  2479. })
  2480. }
  2481. func TestCcLibrarySharedDisabled(t *testing.T) {
  2482. runCcLibraryTestCase(t, Bp2buildTestCase{
  2483. ModuleTypeUnderTest: "cc_library",
  2484. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  2485. Blueprint: soongCcProtoPreamble + `cc_library {
  2486. name: "foo",
  2487. srcs: ["foo.cpp"],
  2488. enabled: false,
  2489. shared: {
  2490. enabled: true,
  2491. },
  2492. target: {
  2493. android: {
  2494. shared: {
  2495. enabled: false,
  2496. },
  2497. }
  2498. },
  2499. include_build_directory: false,
  2500. }`,
  2501. ExpectedBazelTargets: []string{MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
  2502. "srcs": `["foo.cpp"]`,
  2503. "target_compatible_with": `["@platforms//:incompatible"]`,
  2504. }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
  2505. "srcs": `["foo.cpp"]`,
  2506. "target_compatible_with": `select({
  2507. "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
  2508. "//conditions:default": [],
  2509. })`,
  2510. }),
  2511. },
  2512. })
  2513. }
  2514. func TestCcLibraryStaticDisabledForSomeArch(t *testing.T) {
  2515. runCcLibraryTestCase(t, Bp2buildTestCase{
  2516. ModuleTypeUnderTest: "cc_library",
  2517. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  2518. Blueprint: soongCcProtoPreamble + `cc_library {
  2519. name: "foo",
  2520. host_supported: true,
  2521. srcs: ["foo.cpp"],
  2522. shared: {
  2523. enabled: false
  2524. },
  2525. target: {
  2526. darwin: {
  2527. enabled: true,
  2528. },
  2529. windows: {
  2530. enabled: false,
  2531. },
  2532. linux_glibc_x86: {
  2533. shared: {
  2534. enabled: true,
  2535. },
  2536. },
  2537. },
  2538. include_build_directory: false,
  2539. }`,
  2540. ExpectedBazelTargets: []string{MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
  2541. "srcs": `["foo.cpp"]`,
  2542. "target_compatible_with": `select({
  2543. "//build/bazel/platforms/os:windows": ["@platforms//:incompatible"],
  2544. "//conditions:default": [],
  2545. })`,
  2546. }), MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
  2547. "srcs": `["foo.cpp"]`,
  2548. "target_compatible_with": `select({
  2549. "//build/bazel/platforms/os_arch:darwin_arm64": [],
  2550. "//build/bazel/platforms/os_arch:darwin_x86_64": [],
  2551. "//build/bazel/platforms/os_arch:linux_glibc_x86": [],
  2552. "//conditions:default": ["@platforms//:incompatible"],
  2553. })`,
  2554. }),
  2555. }})
  2556. }
  2557. func TestCcLibraryStubs(t *testing.T) {
  2558. expectedBazelTargets := makeCcLibraryTargets("a", AttrNameToString{
  2559. "has_stubs": `True`,
  2560. })
  2561. expectedBazelTargets = append(expectedBazelTargets, makeCcStubSuiteTargets("a", AttrNameToString{
  2562. "soname": `"a.so"`,
  2563. "source_library": `":a"`,
  2564. "stubs_symbol_file": `"a.map.txt"`,
  2565. "stubs_versions": `[
  2566. "28",
  2567. "29",
  2568. "current",
  2569. ]`,
  2570. }))
  2571. runCcLibraryTestCase(t, Bp2buildTestCase{
  2572. Description: "cc_library stubs",
  2573. ModuleTypeUnderTest: "cc_library",
  2574. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  2575. Dir: "foo/bar",
  2576. Filesystem: map[string]string{
  2577. "foo/bar/Android.bp": `
  2578. cc_library {
  2579. name: "a",
  2580. stubs: { symbol_file: "a.map.txt", versions: ["28", "29", "current"] },
  2581. bazel_module: { bp2build_available: true },
  2582. include_build_directory: false,
  2583. }
  2584. `,
  2585. },
  2586. Blueprint: soongCcLibraryPreamble,
  2587. ExpectedBazelTargets: expectedBazelTargets,
  2588. },
  2589. )
  2590. }
  2591. func TestCcLibraryStubsAcrossConfigsDuplicatesRemoved(t *testing.T) {
  2592. runCcLibraryTestCase(t, Bp2buildTestCase{
  2593. Description: "stub target generation of the same lib across configs should not result in duplicates",
  2594. ModuleTypeUnderTest: "cc_library",
  2595. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  2596. Filesystem: map[string]string{
  2597. "bar.map.txt": "",
  2598. },
  2599. Blueprint: `
  2600. cc_library {
  2601. name: "barlib",
  2602. stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] },
  2603. bazel_module: { bp2build_available: false },
  2604. }
  2605. cc_library {
  2606. name: "foolib",
  2607. shared_libs: ["barlib"],
  2608. target: {
  2609. android: {
  2610. shared_libs: ["barlib"],
  2611. },
  2612. },
  2613. bazel_module: { bp2build_available: true },
  2614. }`,
  2615. ExpectedBazelTargets: makeCcLibraryTargets("foolib", AttrNameToString{
  2616. "implementation_dynamic_deps": `select({
  2617. "//build/bazel/rules/apex:android-in_apex": [":barlib_stub_libs_current"],
  2618. "//conditions:default": [":barlib"],
  2619. })`,
  2620. "local_includes": `["."]`,
  2621. }),
  2622. })
  2623. }
  2624. func TestCcLibraryEscapeLdflags(t *testing.T) {
  2625. runCcLibraryTestCase(t, Bp2buildTestCase{
  2626. ModuleTypeUnderTest: "cc_library",
  2627. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  2628. Blueprint: soongCcProtoPreamble + `cc_library {
  2629. name: "foo",
  2630. ldflags: ["-Wl,--rpath,${ORIGIN}"],
  2631. include_build_directory: false,
  2632. }`,
  2633. ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
  2634. "linkopts": `["-Wl,--rpath,$${ORIGIN}"]`,
  2635. }),
  2636. })
  2637. }
  2638. func TestCcLibraryConvertLex(t *testing.T) {
  2639. runCcLibraryTestCase(t, Bp2buildTestCase{
  2640. ModuleTypeUnderTest: "cc_library",
  2641. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  2642. Filesystem: map[string]string{
  2643. "foo.c": "",
  2644. "bar.cc": "",
  2645. "foo1.l": "",
  2646. "bar1.ll": "",
  2647. "foo2.l": "",
  2648. "bar2.ll": "",
  2649. },
  2650. Blueprint: `cc_library {
  2651. name: "foo_lib",
  2652. srcs: ["foo.c", "bar.cc", "foo1.l", "foo2.l", "bar1.ll", "bar2.ll"],
  2653. lex: { flags: ["--foo_flags"] },
  2654. include_build_directory: false,
  2655. bazel_module: { bp2build_available: true },
  2656. }`,
  2657. ExpectedBazelTargets: append([]string{
  2658. MakeBazelTarget("genlex", "foo_lib_genlex_l", AttrNameToString{
  2659. "srcs": `[
  2660. "foo1.l",
  2661. "foo2.l",
  2662. ]`,
  2663. "lexopts": `["--foo_flags"]`,
  2664. }),
  2665. MakeBazelTarget("genlex", "foo_lib_genlex_ll", AttrNameToString{
  2666. "srcs": `[
  2667. "bar1.ll",
  2668. "bar2.ll",
  2669. ]`,
  2670. "lexopts": `["--foo_flags"]`,
  2671. }),
  2672. },
  2673. makeCcLibraryTargets("foo_lib", AttrNameToString{
  2674. "srcs": `[
  2675. "bar.cc",
  2676. ":foo_lib_genlex_ll",
  2677. ]`,
  2678. "srcs_c": `[
  2679. "foo.c",
  2680. ":foo_lib_genlex_l",
  2681. ]`,
  2682. })...),
  2683. })
  2684. }
  2685. func TestCCLibraryRuntimeDeps(t *testing.T) {
  2686. runCcLibrarySharedTestCase(t, Bp2buildTestCase{
  2687. Blueprint: `cc_library_shared {
  2688. name: "bar",
  2689. }
  2690. cc_library {
  2691. name: "foo",
  2692. runtime_libs: ["foo"],
  2693. }`,
  2694. ExpectedBazelTargets: []string{
  2695. MakeBazelTarget("cc_library_shared", "bar", AttrNameToString{
  2696. "local_includes": `["."]`,
  2697. }),
  2698. MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
  2699. "runtime_deps": `[":foo"]`,
  2700. "local_includes": `["."]`,
  2701. }),
  2702. MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
  2703. "runtime_deps": `[":foo"]`,
  2704. "local_includes": `["."]`,
  2705. }),
  2706. },
  2707. })
  2708. }
  2709. func TestCcLibraryWithInstructionSet(t *testing.T) {
  2710. runCcLibraryTestCase(t, Bp2buildTestCase{
  2711. ModuleTypeUnderTest: "cc_library",
  2712. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  2713. Blueprint: `cc_library {
  2714. name: "foo",
  2715. arch: {
  2716. arm: {
  2717. instruction_set: "arm",
  2718. }
  2719. }
  2720. }
  2721. `,
  2722. ExpectedBazelTargets: makeCcLibraryTargets("foo", AttrNameToString{
  2723. "features": `select({
  2724. "//build/bazel/platforms/arch:arm": [
  2725. "arm_isa_arm",
  2726. "-arm_isa_thumb",
  2727. ],
  2728. "//conditions:default": [],
  2729. })`,
  2730. "local_includes": `["."]`,
  2731. }),
  2732. })
  2733. }
  2734. func TestCcLibraryEmptySuffix(t *testing.T) {
  2735. runCcLibraryTestCase(t, Bp2buildTestCase{
  2736. Description: "cc_library with empty suffix",
  2737. ModuleTypeUnderTest: "cc_library",
  2738. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  2739. Filesystem: map[string]string{
  2740. "foo.c": "",
  2741. },
  2742. Blueprint: `cc_library {
  2743. name: "foo",
  2744. suffix: "",
  2745. srcs: ["foo.c"],
  2746. include_build_directory: false,
  2747. }`,
  2748. ExpectedBazelTargets: []string{
  2749. MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
  2750. "srcs_c": `["foo.c"]`,
  2751. }),
  2752. MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
  2753. "srcs_c": `["foo.c"]`,
  2754. "suffix": `""`,
  2755. }),
  2756. },
  2757. })
  2758. }
  2759. func TestCcLibrarySuffix(t *testing.T) {
  2760. runCcLibraryTestCase(t, Bp2buildTestCase{
  2761. Description: "cc_library with suffix",
  2762. ModuleTypeUnderTest: "cc_library",
  2763. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  2764. Filesystem: map[string]string{
  2765. "foo.c": "",
  2766. },
  2767. Blueprint: `cc_library {
  2768. name: "foo",
  2769. suffix: "-suf",
  2770. srcs: ["foo.c"],
  2771. include_build_directory: false,
  2772. }`,
  2773. ExpectedBazelTargets: []string{
  2774. MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
  2775. "srcs_c": `["foo.c"]`,
  2776. }),
  2777. MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
  2778. "srcs_c": `["foo.c"]`,
  2779. "suffix": `"-suf"`,
  2780. }),
  2781. },
  2782. })
  2783. }
  2784. func TestCcLibraryArchVariantSuffix(t *testing.T) {
  2785. runCcLibraryTestCase(t, Bp2buildTestCase{
  2786. Description: "cc_library with arch-variant suffix",
  2787. ModuleTypeUnderTest: "cc_library",
  2788. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  2789. Filesystem: map[string]string{
  2790. "foo.c": "",
  2791. },
  2792. Blueprint: `cc_library {
  2793. name: "foo",
  2794. arch: {
  2795. arm64: { suffix: "-64" },
  2796. arm: { suffix: "-32" },
  2797. },
  2798. srcs: ["foo.c"],
  2799. include_build_directory: false,
  2800. }`,
  2801. ExpectedBazelTargets: []string{
  2802. MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
  2803. "srcs_c": `["foo.c"]`,
  2804. }),
  2805. MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
  2806. "srcs_c": `["foo.c"]`,
  2807. "suffix": `select({
  2808. "//build/bazel/platforms/arch:arm": "-32",
  2809. "//build/bazel/platforms/arch:arm64": "-64",
  2810. "//conditions:default": None,
  2811. })`,
  2812. }),
  2813. },
  2814. })
  2815. }
  2816. func TestCcLibraryWithAidlSrcs(t *testing.T) {
  2817. runCcLibraryTestCase(t, Bp2buildTestCase{
  2818. Description: "cc_library with aidl srcs",
  2819. ModuleTypeUnderTest: "cc_library",
  2820. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  2821. Blueprint: `
  2822. filegroup {
  2823. name: "A_aidl",
  2824. srcs: ["aidl/A.aidl"],
  2825. path: "aidl",
  2826. }
  2827. cc_library {
  2828. name: "foo",
  2829. srcs: [
  2830. ":A_aidl",
  2831. "B.aidl",
  2832. ],
  2833. }`,
  2834. ExpectedBazelTargets: []string{
  2835. MakeBazelTargetNoRestrictions("aidl_library", "A_aidl", AttrNameToString{
  2836. "srcs": `["aidl/A.aidl"]`,
  2837. "strip_import_prefix": `"aidl"`,
  2838. }),
  2839. MakeBazelTarget("aidl_library", "foo_aidl_library", AttrNameToString{
  2840. "srcs": `["B.aidl"]`,
  2841. }),
  2842. MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
  2843. "deps": `[
  2844. ":A_aidl",
  2845. ":foo_aidl_library",
  2846. ]`,
  2847. }),
  2848. MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
  2849. "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
  2850. "local_includes": `["."]`,
  2851. }),
  2852. MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
  2853. "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
  2854. "local_includes": `["."]`,
  2855. }),
  2856. },
  2857. })
  2858. }
  2859. func TestCcLibraryWithNonAdjacentAidlFilegroup(t *testing.T) {
  2860. runCcLibraryTestCase(t, Bp2buildTestCase{
  2861. Description: "cc_library with non aidl filegroup",
  2862. ModuleTypeUnderTest: "cc_library",
  2863. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  2864. Filesystem: map[string]string{
  2865. "path/to/A/Android.bp": `
  2866. filegroup {
  2867. name: "A_aidl",
  2868. srcs: ["aidl/A.aidl"],
  2869. path: "aidl",
  2870. }`,
  2871. },
  2872. Blueprint: `
  2873. cc_library {
  2874. name: "foo",
  2875. srcs: [
  2876. ":A_aidl",
  2877. ],
  2878. }`,
  2879. ExpectedBazelTargets: []string{
  2880. MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
  2881. "deps": `["//path/to/A:A_aidl"]`,
  2882. }),
  2883. MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
  2884. "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
  2885. "local_includes": `["."]`,
  2886. }),
  2887. MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
  2888. "local_includes": `["."]`,
  2889. "implementation_whole_archive_deps": `[":foo_cc_aidl_library"]`,
  2890. }),
  2891. },
  2892. })
  2893. }
  2894. func TestCcLibraryWithExportAidlHeaders(t *testing.T) {
  2895. runCcLibraryTestCase(t, Bp2buildTestCase{
  2896. Description: "cc_library with export aidl headers",
  2897. ModuleTypeUnderTest: "cc_library",
  2898. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  2899. Blueprint: `
  2900. cc_library {
  2901. name: "foo",
  2902. srcs: [
  2903. "Foo.aidl",
  2904. ],
  2905. aidl: {
  2906. export_aidl_headers: true,
  2907. }
  2908. }`,
  2909. ExpectedBazelTargets: []string{
  2910. MakeBazelTarget("aidl_library", "foo_aidl_library", AttrNameToString{
  2911. "srcs": `["Foo.aidl"]`,
  2912. }),
  2913. MakeBazelTarget("cc_aidl_library", "foo_cc_aidl_library", AttrNameToString{
  2914. "deps": `[":foo_aidl_library"]`,
  2915. }),
  2916. MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
  2917. "whole_archive_deps": `[":foo_cc_aidl_library"]`,
  2918. "local_includes": `["."]`,
  2919. }),
  2920. MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
  2921. "whole_archive_deps": `[":foo_cc_aidl_library"]`,
  2922. "local_includes": `["."]`,
  2923. }),
  2924. },
  2925. })
  2926. }
  2927. func TestCcLibraryWithTargetApex(t *testing.T) {
  2928. runCcLibraryTestCase(t, Bp2buildTestCase{
  2929. Description: "cc_library with target.apex",
  2930. ModuleTypeUnderTest: "cc_library",
  2931. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  2932. Blueprint: `
  2933. cc_library {
  2934. name: "foo",
  2935. shared_libs: ["bar", "baz"],
  2936. static_libs: ["baz", "buh"],
  2937. target: {
  2938. apex: {
  2939. exclude_shared_libs: ["bar"],
  2940. exclude_static_libs: ["buh"],
  2941. }
  2942. }
  2943. }`,
  2944. ExpectedBazelTargets: []string{
  2945. MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
  2946. "implementation_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
  2947. "//build/bazel/rules/apex:non_apex": [":buh__BP2BUILD__MISSING__DEP"],
  2948. "//conditions:default": [],
  2949. })`,
  2950. "implementation_dynamic_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
  2951. "//build/bazel/rules/apex:non_apex": [":bar__BP2BUILD__MISSING__DEP"],
  2952. "//conditions:default": [],
  2953. })`,
  2954. "local_includes": `["."]`,
  2955. }),
  2956. MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
  2957. "implementation_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
  2958. "//build/bazel/rules/apex:non_apex": [":buh__BP2BUILD__MISSING__DEP"],
  2959. "//conditions:default": [],
  2960. })`,
  2961. "implementation_dynamic_deps": `[":baz__BP2BUILD__MISSING__DEP"] + select({
  2962. "//build/bazel/rules/apex:non_apex": [":bar__BP2BUILD__MISSING__DEP"],
  2963. "//conditions:default": [],
  2964. })`,
  2965. "local_includes": `["."]`,
  2966. }),
  2967. },
  2968. })
  2969. }
  2970. func TestCcLibraryWithTargetApexAndExportLibHeaders(t *testing.T) {
  2971. runCcLibraryTestCase(t, Bp2buildTestCase{
  2972. Description: "cc_library with target.apex and export_shared|static_lib_headers",
  2973. ModuleTypeUnderTest: "cc_library",
  2974. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  2975. Blueprint: `
  2976. cc_library_static {
  2977. name: "foo",
  2978. shared_libs: ["bar", "baz"],
  2979. static_libs: ["abc"],
  2980. export_shared_lib_headers: ["baz"],
  2981. export_static_lib_headers: ["abc"],
  2982. target: {
  2983. apex: {
  2984. exclude_shared_libs: ["baz", "bar"],
  2985. exclude_static_libs: ["abc"],
  2986. }
  2987. }
  2988. }`,
  2989. ExpectedBazelTargets: []string{
  2990. MakeBazelTarget("cc_library_static", "foo", AttrNameToString{
  2991. "implementation_dynamic_deps": `select({
  2992. "//build/bazel/rules/apex:non_apex": [":bar__BP2BUILD__MISSING__DEP"],
  2993. "//conditions:default": [],
  2994. })`,
  2995. "dynamic_deps": `select({
  2996. "//build/bazel/rules/apex:non_apex": [":baz__BP2BUILD__MISSING__DEP"],
  2997. "//conditions:default": [],
  2998. })`,
  2999. "deps": `select({
  3000. "//build/bazel/rules/apex:non_apex": [":abc__BP2BUILD__MISSING__DEP"],
  3001. "//conditions:default": [],
  3002. })`,
  3003. "local_includes": `["."]`,
  3004. }),
  3005. },
  3006. })
  3007. }
  3008. func TestCcLibraryWithSyspropSrcs(t *testing.T) {
  3009. runCcLibraryTestCase(t, Bp2buildTestCase{
  3010. Description: "cc_library with sysprop sources",
  3011. ModuleTypeUnderTest: "cc_library",
  3012. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  3013. Blueprint: `
  3014. cc_library {
  3015. name: "foo",
  3016. srcs: [
  3017. "bar.sysprop",
  3018. "baz.sysprop",
  3019. "blah.cpp",
  3020. ],
  3021. min_sdk_version: "5",
  3022. }`,
  3023. ExpectedBazelTargets: []string{
  3024. MakeBazelTarget("sysprop_library", "foo_sysprop_library", AttrNameToString{
  3025. "srcs": `[
  3026. "bar.sysprop",
  3027. "baz.sysprop",
  3028. ]`,
  3029. }),
  3030. MakeBazelTarget("cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
  3031. "dep": `":foo_sysprop_library"`,
  3032. "min_sdk_version": `"5"`,
  3033. }),
  3034. MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
  3035. "srcs": `["blah.cpp"]`,
  3036. "local_includes": `["."]`,
  3037. "min_sdk_version": `"5"`,
  3038. "whole_archive_deps": `[":foo_cc_sysprop_library_static"]`,
  3039. }),
  3040. MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
  3041. "srcs": `["blah.cpp"]`,
  3042. "local_includes": `["."]`,
  3043. "min_sdk_version": `"5"`,
  3044. "whole_archive_deps": `[":foo_cc_sysprop_library_static"]`,
  3045. }),
  3046. },
  3047. })
  3048. }
  3049. func TestCcLibraryWithSyspropSrcsSomeConfigs(t *testing.T) {
  3050. runCcLibraryTestCase(t, Bp2buildTestCase{
  3051. Description: "cc_library with sysprop sources in some configs but not others",
  3052. ModuleTypeUnderTest: "cc_library",
  3053. ModuleTypeUnderTestFactory: cc.LibraryFactory,
  3054. Blueprint: `
  3055. cc_library {
  3056. name: "foo",
  3057. host_supported: true,
  3058. srcs: [
  3059. "blah.cpp",
  3060. ],
  3061. target: {
  3062. android: {
  3063. srcs: ["bar.sysprop"],
  3064. },
  3065. },
  3066. min_sdk_version: "5",
  3067. }`,
  3068. ExpectedBazelTargets: []string{
  3069. MakeBazelTargetNoRestrictions("sysprop_library", "foo_sysprop_library", AttrNameToString{
  3070. "srcs": `select({
  3071. "//build/bazel/platforms/os:android": ["bar.sysprop"],
  3072. "//conditions:default": [],
  3073. })`,
  3074. }),
  3075. MakeBazelTargetNoRestrictions("cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
  3076. "dep": `":foo_sysprop_library"`,
  3077. "min_sdk_version": `"5"`,
  3078. }),
  3079. MakeBazelTargetNoRestrictions("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
  3080. "srcs": `["blah.cpp"]`,
  3081. "local_includes": `["."]`,
  3082. "min_sdk_version": `"5"`,
  3083. "whole_archive_deps": `select({
  3084. "//build/bazel/platforms/os:android": [":foo_cc_sysprop_library_static"],
  3085. "//conditions:default": [],
  3086. })`,
  3087. }),
  3088. MakeBazelTargetNoRestrictions("cc_library_shared", "foo", AttrNameToString{
  3089. "srcs": `["blah.cpp"]`,
  3090. "local_includes": `["."]`,
  3091. "min_sdk_version": `"5"`,
  3092. "whole_archive_deps": `select({
  3093. "//build/bazel/platforms/os:android": [":foo_cc_sysprop_library_static"],
  3094. "//conditions:default": [],
  3095. })`,
  3096. }),
  3097. },
  3098. })
  3099. }