cc.go 134 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230
  1. // Copyright 2015 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 cc
  15. // This file contains the module types for compiling C/C++ for Android, and converts the properties
  16. // into the flags and filenames necessary to pass to the compiler. The final creation of the rules
  17. // is handled in builder.go
  18. import (
  19. "fmt"
  20. "io"
  21. "strconv"
  22. "strings"
  23. "android/soong/ui/metrics/bp2build_metrics_proto"
  24. "github.com/google/blueprint"
  25. "github.com/google/blueprint/proptools"
  26. "android/soong/aidl_library"
  27. "android/soong/android"
  28. "android/soong/bazel/cquery"
  29. "android/soong/cc/config"
  30. "android/soong/fuzz"
  31. "android/soong/genrule"
  32. "android/soong/multitree"
  33. "android/soong/snapshot"
  34. )
  35. func init() {
  36. RegisterCCBuildComponents(android.InitRegistrationContext)
  37. pctx.Import("android/soong/cc/config")
  38. }
  39. func RegisterCCBuildComponents(ctx android.RegistrationContext) {
  40. ctx.RegisterModuleType("cc_defaults", defaultsFactory)
  41. ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
  42. ctx.BottomUp("sdk", sdkMutator).Parallel()
  43. ctx.BottomUp("vndk", VndkMutator).Parallel()
  44. ctx.BottomUp("link", LinkageMutator).Parallel()
  45. ctx.BottomUp("test_per_src", TestPerSrcMutator).Parallel()
  46. ctx.BottomUp("version", versionMutator).Parallel()
  47. ctx.BottomUp("begin", BeginMutator).Parallel()
  48. ctx.BottomUp("fdo_profile", fdoProfileMutator)
  49. })
  50. ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
  51. for _, san := range Sanitizers {
  52. san.registerMutators(ctx)
  53. }
  54. ctx.TopDown("sanitize_runtime_deps", sanitizerRuntimeDepsMutator).Parallel()
  55. ctx.BottomUp("sanitize_runtime", sanitizerRuntimeMutator).Parallel()
  56. ctx.TopDown("fuzz_deps", fuzzMutatorDeps)
  57. ctx.BottomUp("coverage", coverageMutator).Parallel()
  58. ctx.TopDown("afdo_deps", afdoDepsMutator)
  59. ctx.BottomUp("afdo", afdoMutator).Parallel()
  60. ctx.TopDown("lto_deps", ltoDepsMutator)
  61. ctx.BottomUp("lto", ltoMutator).Parallel()
  62. ctx.BottomUp("check_linktype", checkLinkTypeMutator).Parallel()
  63. ctx.TopDown("double_loadable", checkDoubleLoadableLibraries).Parallel()
  64. })
  65. ctx.FinalDepsMutators(func(ctx android.RegisterMutatorsContext) {
  66. // sabi mutator needs to be run after apex mutator finishes.
  67. ctx.TopDown("sabi_deps", sabiDepsMutator)
  68. })
  69. ctx.RegisterParallelSingletonType("kythe_extract_all", kytheExtractAllFactory)
  70. }
  71. // Deps is a struct containing module names of dependencies, separated by the kind of dependency.
  72. // Mutators should use `AddVariationDependencies` or its sibling methods to add actual dependency
  73. // edges to these modules.
  74. // This object is constructed in DepsMutator, by calling to various module delegates to set
  75. // relevant fields. For example, `module.compiler.compilerDeps()` may append type-specific
  76. // dependencies.
  77. // This is then consumed by the same DepsMutator, which will call `ctx.AddVariationDependencies()`
  78. // (or its sibling methods) to set real dependencies on the given modules.
  79. type Deps struct {
  80. SharedLibs, LateSharedLibs []string
  81. StaticLibs, LateStaticLibs, WholeStaticLibs []string
  82. HeaderLibs []string
  83. RuntimeLibs []string
  84. // UnexportedStaticLibs are static libraries that are also passed to -Wl,--exclude-libs= to
  85. // prevent automatically exporting symbols.
  86. UnexportedStaticLibs []string
  87. // Used for data dependencies adjacent to tests
  88. DataLibs []string
  89. DataBins []string
  90. // Used by DepsMutator to pass system_shared_libs information to check_elf_file.py.
  91. SystemSharedLibs []string
  92. // Used by DepMutator to pass aidl_library modules to aidl compiler
  93. AidlLibs []string
  94. // If true, statically link the unwinder into native libraries/binaries.
  95. StaticUnwinderIfLegacy bool
  96. ReexportSharedLibHeaders, ReexportStaticLibHeaders, ReexportHeaderLibHeaders []string
  97. ObjFiles []string
  98. GeneratedSources []string
  99. GeneratedHeaders []string
  100. GeneratedDeps []string
  101. ReexportGeneratedHeaders []string
  102. CrtBegin, CrtEnd []string
  103. // Used for host bionic
  104. DynamicLinker string
  105. // List of libs that need to be excluded for APEX variant
  106. ExcludeLibsForApex []string
  107. }
  108. // PathDeps is a struct containing file paths to dependencies of a module.
  109. // It's constructed in depsToPath() by traversing the direct dependencies of the current module.
  110. // It's used to construct flags for various build statements (such as for compiling and linking).
  111. // It is then passed to module decorator functions responsible for registering build statements
  112. // (such as `module.compiler.compile()`).`
  113. type PathDeps struct {
  114. // Paths to .so files
  115. SharedLibs, EarlySharedLibs, LateSharedLibs android.Paths
  116. // Paths to the dependencies to use for .so files (.so.toc files)
  117. SharedLibsDeps, EarlySharedLibsDeps, LateSharedLibsDeps android.Paths
  118. // Paths to .a files
  119. StaticLibs, LateStaticLibs, WholeStaticLibs android.Paths
  120. // Transitive static library dependencies of static libraries for use in ordering.
  121. TranstiveStaticLibrariesForOrdering *android.DepSet
  122. // Paths to .o files
  123. Objs Objects
  124. // Paths to .o files in dependencies that provide them. Note that these lists
  125. // aren't complete since prebuilt modules don't provide the .o files.
  126. StaticLibObjs Objects
  127. WholeStaticLibObjs Objects
  128. // Paths to .a files in prebuilts. Complements WholeStaticLibObjs to contain
  129. // the libs from all whole_static_lib dependencies.
  130. WholeStaticLibsFromPrebuilts android.Paths
  131. // Paths to generated source files
  132. GeneratedSources android.Paths
  133. GeneratedDeps android.Paths
  134. Flags []string
  135. LdFlags []string
  136. IncludeDirs android.Paths
  137. SystemIncludeDirs android.Paths
  138. ReexportedDirs android.Paths
  139. ReexportedSystemDirs android.Paths
  140. ReexportedFlags []string
  141. ReexportedGeneratedHeaders android.Paths
  142. ReexportedDeps android.Paths
  143. // Paths to crt*.o files
  144. CrtBegin, CrtEnd android.Paths
  145. // Path to the dynamic linker binary
  146. DynamicLinker android.OptionalPath
  147. // For Darwin builds, the path to the second architecture's output that should
  148. // be combined with this architectures's output into a FAT MachO file.
  149. DarwinSecondArchOutput android.OptionalPath
  150. // Paths to direct srcs and transitive include dirs from direct aidl_library deps
  151. AidlLibraryInfos []aidl_library.AidlLibraryInfo
  152. }
  153. // LocalOrGlobalFlags contains flags that need to have values set globally by the build system or locally by the module
  154. // tracked separately, in order to maintain the required ordering (most of the global flags need to go first on the
  155. // command line so they can be overridden by the local module flags).
  156. type LocalOrGlobalFlags struct {
  157. CommonFlags []string // Flags that apply to C, C++, and assembly source files
  158. AsFlags []string // Flags that apply to assembly source files
  159. YasmFlags []string // Flags that apply to yasm assembly source files
  160. CFlags []string // Flags that apply to C and C++ source files
  161. ToolingCFlags []string // Flags that apply to C and C++ source files parsed by clang LibTooling tools
  162. ConlyFlags []string // Flags that apply to C source files
  163. CppFlags []string // Flags that apply to C++ source files
  164. ToolingCppFlags []string // Flags that apply to C++ source files parsed by clang LibTooling tools
  165. LdFlags []string // Flags that apply to linker command lines
  166. }
  167. // Flags contains various types of command line flags (and settings) for use in building build
  168. // statements related to C++.
  169. type Flags struct {
  170. // Local flags (which individual modules are responsible for). These may override global flags.
  171. Local LocalOrGlobalFlags
  172. // Global flags (which build system or toolchain is responsible for).
  173. Global LocalOrGlobalFlags
  174. aidlFlags []string // Flags that apply to aidl source files
  175. rsFlags []string // Flags that apply to renderscript source files
  176. libFlags []string // Flags to add libraries early to the link order
  177. extraLibFlags []string // Flags to add libraries late in the link order after LdFlags
  178. TidyFlags []string // Flags that apply to clang-tidy
  179. SAbiFlags []string // Flags that apply to header-abi-dumper
  180. // Global include flags that apply to C, C++, and assembly source files
  181. // These must be after any module include flags, which will be in CommonFlags.
  182. SystemIncludeFlags []string
  183. Toolchain config.Toolchain
  184. Tidy bool // True if ninja .tidy rules should be generated.
  185. NeedTidyFiles bool // True if module link should depend on .tidy files
  186. GcovCoverage bool // True if coverage files should be generated.
  187. SAbiDump bool // True if header abi dumps should be generated.
  188. EmitXrefs bool // If true, generate Ninja rules to generate emitXrefs input files for Kythe
  189. // The instruction set required for clang ("arm" or "thumb").
  190. RequiredInstructionSet string
  191. // The target-device system path to the dynamic linker.
  192. DynamicLinker string
  193. CFlagsDeps android.Paths // Files depended on by compiler flags
  194. LdFlagsDeps android.Paths // Files depended on by linker flags
  195. // True if .s files should be processed with the c preprocessor.
  196. AssemblerWithCpp bool
  197. proto android.ProtoFlags
  198. protoC bool // Whether to use C instead of C++
  199. protoOptionsFile bool // Whether to look for a .options file next to the .proto
  200. Yacc *YaccProperties
  201. Lex *LexProperties
  202. }
  203. // Properties used to compile all C or C++ modules
  204. type BaseProperties struct {
  205. // Deprecated. true is the default, false is invalid.
  206. Clang *bool `android:"arch_variant"`
  207. // The API level that this module is built against. The APIs of this API level will be
  208. // visible at build time, but use of any APIs newer than min_sdk_version will render the
  209. // module unloadable on older devices. In the future it will be possible to weakly-link new
  210. // APIs, making the behavior match Java: such modules will load on older devices, but
  211. // calling new APIs on devices that do not support them will result in a crash.
  212. //
  213. // This property has the same behavior as sdk_version does for Java modules. For those
  214. // familiar with Android Gradle, the property behaves similarly to how compileSdkVersion
  215. // does for Java code.
  216. //
  217. // In addition, setting this property causes two variants to be built, one for the platform
  218. // and one for apps.
  219. Sdk_version *string
  220. // Minimum OS API level supported by this C or C++ module. This property becomes the value
  221. // of the __ANDROID_API__ macro. When the C or C++ module is included in an APEX or an APK,
  222. // this property is also used to ensure that the min_sdk_version of the containing module is
  223. // not older (i.e. less) than this module's min_sdk_version. When not set, this property
  224. // defaults to the value of sdk_version. When this is set to "apex_inherit", this tracks
  225. // min_sdk_version of the containing APEX. When the module
  226. // is not built for an APEX, "apex_inherit" defaults to sdk_version.
  227. Min_sdk_version *string
  228. // If true, always create an sdk variant and don't create a platform variant.
  229. Sdk_variant_only *bool
  230. AndroidMkSharedLibs []string `blueprint:"mutated"`
  231. AndroidMkStaticLibs []string `blueprint:"mutated"`
  232. AndroidMkRuntimeLibs []string `blueprint:"mutated"`
  233. AndroidMkWholeStaticLibs []string `blueprint:"mutated"`
  234. AndroidMkHeaderLibs []string `blueprint:"mutated"`
  235. HideFromMake bool `blueprint:"mutated"`
  236. PreventInstall bool `blueprint:"mutated"`
  237. ApexesProvidingSharedLibs []string `blueprint:"mutated"`
  238. // Set by DepsMutator.
  239. AndroidMkSystemSharedLibs []string `blueprint:"mutated"`
  240. // The name of the image this module is built for, suffixed with a '.'
  241. ImageVariationPrefix string `blueprint:"mutated"`
  242. // The VNDK version this module is built against. If empty, the module is not
  243. // build against the VNDK.
  244. VndkVersion string `blueprint:"mutated"`
  245. // Suffix for the name of Android.mk entries generated by this module
  246. SubName string `blueprint:"mutated"`
  247. // *.logtags files, to combine together in order to generate the /system/etc/event-log-tags
  248. // file
  249. Logtags []string
  250. // Make this module available when building for ramdisk.
  251. // On device without a dedicated recovery partition, the module is only
  252. // available after switching root into
  253. // /first_stage_ramdisk. To expose the module before switching root, install
  254. // the recovery variant instead.
  255. Ramdisk_available *bool
  256. // Make this module available when building for vendor ramdisk.
  257. // On device without a dedicated recovery partition, the module is only
  258. // available after switching root into
  259. // /first_stage_ramdisk. To expose the module before switching root, install
  260. // the recovery variant instead.
  261. Vendor_ramdisk_available *bool
  262. // Make this module available when building for recovery
  263. Recovery_available *bool
  264. // Used by imageMutator, set by ImageMutatorBegin()
  265. CoreVariantNeeded bool `blueprint:"mutated"`
  266. RamdiskVariantNeeded bool `blueprint:"mutated"`
  267. VendorRamdiskVariantNeeded bool `blueprint:"mutated"`
  268. RecoveryVariantNeeded bool `blueprint:"mutated"`
  269. // A list of variations for the "image" mutator of the form
  270. //<image name> '.' <version char>, for example, 'vendor.S'
  271. ExtraVersionedImageVariations []string `blueprint:"mutated"`
  272. // Allows this module to use non-APEX version of libraries. Useful
  273. // for building binaries that are started before APEXes are activated.
  274. Bootstrap *bool
  275. // Even if DeviceConfig().VndkUseCoreVariant() is set, this module must use vendor variant.
  276. // see soong/cc/config/vndk.go
  277. MustUseVendorVariant bool `blueprint:"mutated"`
  278. // Used by vendor snapshot to record dependencies from snapshot modules.
  279. SnapshotSharedLibs []string `blueprint:"mutated"`
  280. SnapshotStaticLibs []string `blueprint:"mutated"`
  281. SnapshotRuntimeLibs []string `blueprint:"mutated"`
  282. Installable *bool `android:"arch_variant"`
  283. // Set by factories of module types that can only be referenced from variants compiled against
  284. // the SDK.
  285. AlwaysSdk bool `blueprint:"mutated"`
  286. // Variant is an SDK variant created by sdkMutator
  287. IsSdkVariant bool `blueprint:"mutated"`
  288. // Set when both SDK and platform variants are exported to Make to trigger renaming the SDK
  289. // variant to have a ".sdk" suffix.
  290. SdkAndPlatformVariantVisibleToMake bool `blueprint:"mutated"`
  291. // Normally Soong uses the directory structure to decide which modules
  292. // should be included (framework) or excluded (non-framework) from the
  293. // different snapshots (vendor, recovery, etc.), but this property
  294. // allows a partner to exclude a module normally thought of as a
  295. // framework module from the vendor snapshot.
  296. Exclude_from_vendor_snapshot *bool
  297. // Normally Soong uses the directory structure to decide which modules
  298. // should be included (framework) or excluded (non-framework) from the
  299. // different snapshots (vendor, recovery, etc.), but this property
  300. // allows a partner to exclude a module normally thought of as a
  301. // framework module from the recovery snapshot.
  302. Exclude_from_recovery_snapshot *bool
  303. // List of APEXes that this module has private access to for testing purpose. The module
  304. // can depend on libraries that are not exported by the APEXes and use private symbols
  305. // from the exported libraries.
  306. Test_for []string `android:"arch_variant"`
  307. Target struct {
  308. Platform struct {
  309. // List of modules required by the core variant.
  310. Required []string `android:"arch_variant"`
  311. // List of modules not required by the core variant.
  312. Exclude_required []string `android:"arch_variant"`
  313. } `android:"arch_variant"`
  314. Recovery struct {
  315. // List of modules required by the recovery variant.
  316. Required []string `android:"arch_variant"`
  317. // List of modules not required by the recovery variant.
  318. Exclude_required []string `android:"arch_variant"`
  319. } `android:"arch_variant"`
  320. } `android:"arch_variant"`
  321. }
  322. type VendorProperties struct {
  323. // whether this module should be allowed to be directly depended by other
  324. // modules with `vendor: true`, `proprietary: true`, or `vendor_available:true`.
  325. // If set to true, two variants will be built separately, one like
  326. // normal, and the other limited to the set of libraries and headers
  327. // that are exposed to /vendor modules.
  328. //
  329. // The vendor variant may be used with a different (newer) /system,
  330. // so it shouldn't have any unversioned runtime dependencies, or
  331. // make assumptions about the system that may not be true in the
  332. // future.
  333. //
  334. // If set to false, this module becomes inaccessible from /vendor modules.
  335. //
  336. // The modules with vndk: {enabled: true} must define 'vendor_available'
  337. // to 'true'.
  338. //
  339. // Nothing happens if BOARD_VNDK_VERSION isn't set in the BoardConfig.mk
  340. Vendor_available *bool
  341. // This is the same as the "vendor_available" except that the install path
  342. // of the vendor variant is /odm or /vendor/odm.
  343. // By replacing "vendor_available: true" with "odm_available: true", the
  344. // module will install its vendor variant to the /odm partition or /vendor/odm.
  345. // As the modules with "odm_available: true" still create the vendor variants,
  346. // they can link to the other vendor modules as the vendor_available modules do.
  347. // Also, the vendor modules can link to odm_available modules.
  348. //
  349. // It may not be used for VNDK modules.
  350. Odm_available *bool
  351. // whether this module should be allowed to be directly depended by other
  352. // modules with `product_specific: true` or `product_available: true`.
  353. // If set to true, an additional product variant will be built separately
  354. // that is limited to the set of libraries and headers that are exposed to
  355. // /product modules.
  356. //
  357. // The product variant may be used with a different (newer) /system,
  358. // so it shouldn't have any unversioned runtime dependencies, or
  359. // make assumptions about the system that may not be true in the
  360. // future.
  361. //
  362. // If set to false, this module becomes inaccessible from /product modules.
  363. //
  364. // Different from the 'vendor_available' property, the modules with
  365. // vndk: {enabled: true} don't have to define 'product_available'. The VNDK
  366. // library without 'product_available' may not be depended on by any other
  367. // modules that has product variants including the product available VNDKs.
  368. //
  369. // Nothing happens if BOARD_VNDK_VERSION isn't set in the BoardConfig.mk
  370. // and PRODUCT_PRODUCT_VNDK_VERSION isn't set.
  371. Product_available *bool
  372. // whether this module is capable of being loaded with other instance
  373. // (possibly an older version) of the same module in the same process.
  374. // Currently, a shared library that is a member of VNDK (vndk: {enabled: true})
  375. // can be double loaded in a vendor process if the library is also a
  376. // (direct and indirect) dependency of an LLNDK library. Such libraries must be
  377. // explicitly marked as `double_loadable: true` by the owner, or the dependency
  378. // from the LLNDK lib should be cut if the lib is not designed to be double loaded.
  379. Double_loadable *bool
  380. // IsLLNDK is set to true for the vendor variant of a cc_library module that has LLNDK stubs.
  381. IsLLNDK bool `blueprint:"mutated"`
  382. // IsVNDKUsingCoreVariant is true for VNDK modules if the global VndkUseCoreVariant option is
  383. // set and the module is not listed in VndkMustUseVendorVariantList.
  384. IsVNDKUsingCoreVariant bool `blueprint:"mutated"`
  385. // IsVNDKCore is set if a VNDK module does not set the vndk.support_system_process property.
  386. IsVNDKCore bool `blueprint:"mutated"`
  387. // IsVNDKSP is set if a VNDK module sets the vndk.support_system_process property.
  388. IsVNDKSP bool `blueprint:"mutated"`
  389. // IsVNDKPrivate is set if a VNDK module sets the vndk.private property or an LLNDK
  390. // module sets the llndk.private property.
  391. IsVNDKPrivate bool `blueprint:"mutated"`
  392. // IsVNDKProduct is set if a VNDK module sets the product_available property.
  393. IsVNDKProduct bool `blueprint:"mutated"`
  394. // IsVendorPublicLibrary is set for the core and product variants of a library that has
  395. // vendor_public_library stubs.
  396. IsVendorPublicLibrary bool `blueprint:"mutated"`
  397. }
  398. // ModuleContextIntf is an interface (on a module context helper) consisting of functions related
  399. // to understanding details about the type of the current module.
  400. // For example, one might call these functions to determine whether the current module is a static
  401. // library and/or is installed in vendor directories.
  402. type ModuleContextIntf interface {
  403. static() bool
  404. staticBinary() bool
  405. testBinary() bool
  406. testLibrary() bool
  407. header() bool
  408. binary() bool
  409. object() bool
  410. toolchain() config.Toolchain
  411. canUseSdk() bool
  412. useSdk() bool
  413. sdkVersion() string
  414. minSdkVersion() string
  415. isSdkVariant() bool
  416. useVndk() bool
  417. isNdk(config android.Config) bool
  418. IsLlndk() bool
  419. IsLlndkPublic() bool
  420. isImplementationForLLNDKPublic() bool
  421. IsVndkPrivate() bool
  422. isVndk() bool
  423. isVndkSp() bool
  424. IsVndkExt() bool
  425. IsVendorPublicLibrary() bool
  426. inProduct() bool
  427. inVendor() bool
  428. inRamdisk() bool
  429. inVendorRamdisk() bool
  430. inRecovery() bool
  431. selectedStl() string
  432. baseModuleName() string
  433. getVndkExtendsModuleName() string
  434. isAfdoCompile() bool
  435. isPgoCompile() bool
  436. isCfi() bool
  437. isFuzzer() bool
  438. isNDKStubLibrary() bool
  439. useClangLld(actx ModuleContext) bool
  440. isForPlatform() bool
  441. apexVariationName() string
  442. apexSdkVersion() android.ApiLevel
  443. bootstrap() bool
  444. mustUseVendorVariant() bool
  445. nativeCoverage() bool
  446. directlyInAnyApex() bool
  447. isPreventInstall() bool
  448. isCfiAssemblySupportEnabled() bool
  449. getSharedFlags() *SharedFlags
  450. }
  451. type SharedFlags struct {
  452. numSharedFlags int
  453. flagsMap map[string]string
  454. }
  455. type ModuleContext interface {
  456. android.ModuleContext
  457. ModuleContextIntf
  458. }
  459. type BaseModuleContext interface {
  460. android.BaseModuleContext
  461. ModuleContextIntf
  462. }
  463. type DepsContext interface {
  464. android.BottomUpMutatorContext
  465. ModuleContextIntf
  466. }
  467. // feature represents additional (optional) steps to building cc-related modules, such as invocation
  468. // of clang-tidy.
  469. type feature interface {
  470. flags(ctx ModuleContext, flags Flags) Flags
  471. props() []interface{}
  472. }
  473. // compiler is the interface for a compiler helper object. Different module decorators may implement
  474. // this helper differently.
  475. type compiler interface {
  476. compilerInit(ctx BaseModuleContext)
  477. compilerDeps(ctx DepsContext, deps Deps) Deps
  478. compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags
  479. compilerProps() []interface{}
  480. appendCflags([]string)
  481. appendAsflags([]string)
  482. compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects
  483. }
  484. // linker is the interface for a linker decorator object. Individual module types can provide
  485. // their own implementation for this decorator, and thus specify custom logic regarding build
  486. // statements pertaining to linking.
  487. type linker interface {
  488. linkerInit(ctx BaseModuleContext)
  489. linkerDeps(ctx DepsContext, deps Deps) Deps
  490. linkerFlags(ctx ModuleContext, flags Flags) Flags
  491. linkerProps() []interface{}
  492. useClangLld(actx ModuleContext) bool
  493. link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path
  494. appendLdflags([]string)
  495. unstrippedOutputFilePath() android.Path
  496. nativeCoverage() bool
  497. coverageOutputFilePath() android.OptionalPath
  498. // Get the deps that have been explicitly specified in the properties.
  499. linkerSpecifiedDeps(specifiedDeps specifiedDeps) specifiedDeps
  500. }
  501. // specifiedDeps is a tuple struct representing dependencies of a linked binary owned by the linker.
  502. type specifiedDeps struct {
  503. sharedLibs []string
  504. // Note nil and [] are semantically distinct. [] prevents linking against the defaults (usually
  505. // libc, libm, etc.)
  506. systemSharedLibs []string
  507. }
  508. // installer is the interface for an installer helper object. This helper is responsible for
  509. // copying build outputs to the appropriate locations so that they may be installed on device.
  510. type installer interface {
  511. installerProps() []interface{}
  512. install(ctx ModuleContext, path android.Path)
  513. everInstallable() bool
  514. inData() bool
  515. inSanitizerDir() bool
  516. hostToolPath() android.OptionalPath
  517. relativeInstallPath() string
  518. makeUninstallable(mod *Module)
  519. installInRoot() bool
  520. }
  521. type xref interface {
  522. XrefCcFiles() android.Paths
  523. }
  524. type overridable interface {
  525. overriddenModules() []string
  526. }
  527. type libraryDependencyKind int
  528. const (
  529. headerLibraryDependency = iota
  530. sharedLibraryDependency
  531. staticLibraryDependency
  532. )
  533. func (k libraryDependencyKind) String() string {
  534. switch k {
  535. case headerLibraryDependency:
  536. return "headerLibraryDependency"
  537. case sharedLibraryDependency:
  538. return "sharedLibraryDependency"
  539. case staticLibraryDependency:
  540. return "staticLibraryDependency"
  541. default:
  542. panic(fmt.Errorf("unknown libraryDependencyKind %d", k))
  543. }
  544. }
  545. type libraryDependencyOrder int
  546. const (
  547. earlyLibraryDependency = -1
  548. normalLibraryDependency = 0
  549. lateLibraryDependency = 1
  550. )
  551. func (o libraryDependencyOrder) String() string {
  552. switch o {
  553. case earlyLibraryDependency:
  554. return "earlyLibraryDependency"
  555. case normalLibraryDependency:
  556. return "normalLibraryDependency"
  557. case lateLibraryDependency:
  558. return "lateLibraryDependency"
  559. default:
  560. panic(fmt.Errorf("unknown libraryDependencyOrder %d", o))
  561. }
  562. }
  563. // libraryDependencyTag is used to tag dependencies on libraries. Unlike many dependency
  564. // tags that have a set of predefined tag objects that are reused for each dependency, a
  565. // libraryDependencyTag is designed to contain extra metadata and is constructed as needed.
  566. // That means that comparing a libraryDependencyTag for equality will only be equal if all
  567. // of the metadata is equal. Most usages will want to type assert to libraryDependencyTag and
  568. // then check individual metadata fields instead.
  569. type libraryDependencyTag struct {
  570. blueprint.BaseDependencyTag
  571. // These are exported so that fmt.Printf("%#v") can call their String methods.
  572. Kind libraryDependencyKind
  573. Order libraryDependencyOrder
  574. wholeStatic bool
  575. reexportFlags bool
  576. explicitlyVersioned bool
  577. dataLib bool
  578. ndk bool
  579. staticUnwinder bool
  580. makeSuffix string
  581. // Whether or not this dependency should skip the apex dependency check
  582. skipApexAllowedDependenciesCheck bool
  583. // Whether or not this dependency has to be followed for the apex variants
  584. excludeInApex bool
  585. // If true, don't automatically export symbols from the static library into a shared library.
  586. unexportedSymbols bool
  587. }
  588. // header returns true if the libraryDependencyTag is tagging a header lib dependency.
  589. func (d libraryDependencyTag) header() bool {
  590. return d.Kind == headerLibraryDependency
  591. }
  592. // shared returns true if the libraryDependencyTag is tagging a shared lib dependency.
  593. func (d libraryDependencyTag) shared() bool {
  594. return d.Kind == sharedLibraryDependency
  595. }
  596. // shared returns true if the libraryDependencyTag is tagging a static lib dependency.
  597. func (d libraryDependencyTag) static() bool {
  598. return d.Kind == staticLibraryDependency
  599. }
  600. func (d libraryDependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
  601. if d.shared() {
  602. return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
  603. }
  604. return nil
  605. }
  606. var _ android.LicenseAnnotationsDependencyTag = libraryDependencyTag{}
  607. // InstallDepNeeded returns true for shared libraries so that shared library dependencies of
  608. // binaries or other shared libraries are installed as dependencies.
  609. func (d libraryDependencyTag) InstallDepNeeded() bool {
  610. return d.shared()
  611. }
  612. var _ android.InstallNeededDependencyTag = libraryDependencyTag{}
  613. // dependencyTag is used for tagging miscellaneous dependency types that don't fit into
  614. // libraryDependencyTag. Each tag object is created globally and reused for multiple
  615. // dependencies (although since the object contains no references, assigning a tag to a
  616. // variable and modifying it will not modify the original). Users can compare the tag
  617. // returned by ctx.OtherModuleDependencyTag against the global original
  618. type dependencyTag struct {
  619. blueprint.BaseDependencyTag
  620. name string
  621. }
  622. // installDependencyTag is used for tagging miscellaneous dependency types that don't fit into
  623. // libraryDependencyTag, but where the dependency needs to be installed when the parent is
  624. // installed.
  625. type installDependencyTag struct {
  626. blueprint.BaseDependencyTag
  627. android.InstallAlwaysNeededDependencyTag
  628. name string
  629. }
  630. var (
  631. genSourceDepTag = dependencyTag{name: "gen source"}
  632. genHeaderDepTag = dependencyTag{name: "gen header"}
  633. genHeaderExportDepTag = dependencyTag{name: "gen header export"}
  634. objDepTag = dependencyTag{name: "obj"}
  635. dynamicLinkerDepTag = installDependencyTag{name: "dynamic linker"}
  636. reuseObjTag = dependencyTag{name: "reuse objects"}
  637. staticVariantTag = dependencyTag{name: "static variant"}
  638. vndkExtDepTag = dependencyTag{name: "vndk extends"}
  639. dataLibDepTag = dependencyTag{name: "data lib"}
  640. dataBinDepTag = dependencyTag{name: "data bin"}
  641. runtimeDepTag = installDependencyTag{name: "runtime lib"}
  642. testPerSrcDepTag = dependencyTag{name: "test_per_src"}
  643. stubImplDepTag = dependencyTag{name: "stub_impl"}
  644. JniFuzzLibTag = dependencyTag{name: "jni_fuzz_lib_tag"}
  645. FdoProfileTag = dependencyTag{name: "fdo_profile"}
  646. aidlLibraryTag = dependencyTag{name: "aidl_library"}
  647. )
  648. func IsSharedDepTag(depTag blueprint.DependencyTag) bool {
  649. ccLibDepTag, ok := depTag.(libraryDependencyTag)
  650. return ok && ccLibDepTag.shared()
  651. }
  652. func IsStaticDepTag(depTag blueprint.DependencyTag) bool {
  653. ccLibDepTag, ok := depTag.(libraryDependencyTag)
  654. return ok && ccLibDepTag.static()
  655. }
  656. func IsHeaderDepTag(depTag blueprint.DependencyTag) bool {
  657. ccLibDepTag, ok := depTag.(libraryDependencyTag)
  658. return ok && ccLibDepTag.header()
  659. }
  660. func IsRuntimeDepTag(depTag blueprint.DependencyTag) bool {
  661. return depTag == runtimeDepTag
  662. }
  663. func IsTestPerSrcDepTag(depTag blueprint.DependencyTag) bool {
  664. ccDepTag, ok := depTag.(dependencyTag)
  665. return ok && ccDepTag == testPerSrcDepTag
  666. }
  667. // bazelHandler is the interface for a helper object related to deferring to Bazel for
  668. // processing a cc module (during Bazel mixed builds). Individual module types should define
  669. // their own bazel handler if they support being handled by Bazel.
  670. type BazelHandler interface {
  671. // QueueBazelCall invokes request-queueing functions on the BazelContext
  672. //so that these requests are handled when Bazel's cquery is invoked.
  673. QueueBazelCall(ctx android.BaseModuleContext, label string)
  674. // ProcessBazelQueryResponse uses information retrieved from Bazel to set properties
  675. // on the current module with given label.
  676. ProcessBazelQueryResponse(ctx android.ModuleContext, label string)
  677. }
  678. // Module contains the properties and members used by all C/C++ module types, and implements
  679. // the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
  680. // to construct the output file. Behavior can be customized with a Customizer, or "decorator",
  681. // interface.
  682. //
  683. // To define a C/C++ related module, construct a new Module object and point its delegates to
  684. // type-specific structs. These delegates will be invoked to register module-specific build
  685. // statements which may be unique to the module type. For example, module.compiler.compile() should
  686. // be defined so as to register build statements which are responsible for compiling the module.
  687. //
  688. // Another example: to construct a cc_binary module, one can create a `cc.binaryDecorator` struct
  689. // which implements the `linker` and `installer` interfaces, and points the `linker` and `installer`
  690. // members of the cc.Module to this decorator. Thus, a cc_binary module has custom linker and
  691. // installer logic.
  692. type Module struct {
  693. fuzz.FuzzModule
  694. android.BazelModuleBase
  695. VendorProperties VendorProperties
  696. Properties BaseProperties
  697. // initialize before calling Init
  698. hod android.HostOrDeviceSupported
  699. multilib android.Multilib
  700. bazelable bool
  701. // Allowable SdkMemberTypes of this module type.
  702. sdkMemberTypes []android.SdkMemberType
  703. // decorator delegates, initialize before calling Init
  704. // these may contain module-specific implementations, and effectively allow for custom
  705. // type-specific logic. These members may reference different objects or the same object.
  706. // Functions of these decorators will be invoked to initialize and register type-specific
  707. // build statements.
  708. compiler compiler
  709. linker linker
  710. installer installer
  711. bazelHandler BazelHandler
  712. features []feature
  713. stl *stl
  714. sanitize *sanitize
  715. coverage *coverage
  716. fuzzer *fuzzer
  717. sabi *sabi
  718. vndkdep *vndkdep
  719. lto *lto
  720. afdo *afdo
  721. pgo *pgo
  722. library libraryInterface
  723. outputFile android.OptionalPath
  724. cachedToolchain config.Toolchain
  725. subAndroidMkOnce map[subAndroidMkProvider]bool
  726. // Flags used to compile this module
  727. flags Flags
  728. // Shared flags among build rules of this module
  729. sharedFlags SharedFlags
  730. // only non-nil when this is a shared library that reuses the objects of a static library
  731. staticAnalogue *StaticLibraryInfo
  732. makeLinkType string
  733. // Kythe (source file indexer) paths for this compilation module
  734. kytheFiles android.Paths
  735. // Object .o file output paths for this compilation module
  736. objFiles android.Paths
  737. // Tidy .tidy file output paths for this compilation module
  738. tidyFiles android.Paths
  739. // For apex variants, this is set as apex.min_sdk_version
  740. apexSdkVersion android.ApiLevel
  741. hideApexVariantFromMake bool
  742. }
  743. func (c *Module) AddJSONData(d *map[string]interface{}) {
  744. var hasAidl, hasLex, hasProto, hasRenderscript, hasSysprop, hasWinMsg, hasYacc bool
  745. if b, ok := c.compiler.(*baseCompiler); ok {
  746. hasAidl = b.hasSrcExt(".aidl")
  747. hasLex = b.hasSrcExt(".l") || b.hasSrcExt(".ll")
  748. hasProto = b.hasSrcExt(".proto")
  749. hasRenderscript = b.hasSrcExt(".rscript") || b.hasSrcExt(".fs")
  750. hasSysprop = b.hasSrcExt(".sysprop")
  751. hasWinMsg = b.hasSrcExt(".mc")
  752. hasYacc = b.hasSrcExt(".y") || b.hasSrcExt(".yy")
  753. }
  754. c.AndroidModuleBase().AddJSONData(d)
  755. (*d)["Cc"] = map[string]interface{}{
  756. "SdkVersion": c.SdkVersion(),
  757. "MinSdkVersion": c.MinSdkVersion(),
  758. "VndkVersion": c.VndkVersion(),
  759. "ProductSpecific": c.ProductSpecific(),
  760. "SocSpecific": c.SocSpecific(),
  761. "DeviceSpecific": c.DeviceSpecific(),
  762. "InProduct": c.InProduct(),
  763. "InVendor": c.InVendor(),
  764. "InRamdisk": c.InRamdisk(),
  765. "InVendorRamdisk": c.InVendorRamdisk(),
  766. "InRecovery": c.InRecovery(),
  767. "VendorAvailable": c.VendorAvailable(),
  768. "ProductAvailable": c.ProductAvailable(),
  769. "RamdiskAvailable": c.RamdiskAvailable(),
  770. "VendorRamdiskAvailable": c.VendorRamdiskAvailable(),
  771. "RecoveryAvailable": c.RecoveryAvailable(),
  772. "OdmAvailable": c.OdmAvailable(),
  773. "InstallInData": c.InstallInData(),
  774. "InstallInRamdisk": c.InstallInRamdisk(),
  775. "InstallInSanitizerDir": c.InstallInSanitizerDir(),
  776. "InstallInVendorRamdisk": c.InstallInVendorRamdisk(),
  777. "InstallInRecovery": c.InstallInRecovery(),
  778. "InstallInRoot": c.InstallInRoot(),
  779. "IsVndk": c.IsVndk(),
  780. "IsVndkExt": c.IsVndkExt(),
  781. "IsVndkPrivate": c.IsVndkPrivate(),
  782. "IsVndkSp": c.IsVndkSp(),
  783. "IsLlndk": c.IsLlndk(),
  784. "IsLlndkPublic": c.IsLlndkPublic(),
  785. "IsSnapshotLibrary": c.IsSnapshotLibrary(),
  786. "IsSnapshotPrebuilt": c.IsSnapshotPrebuilt(),
  787. "IsVendorPublicLibrary": c.IsVendorPublicLibrary(),
  788. "ApexSdkVersion": c.apexSdkVersion,
  789. "TestFor": c.TestFor(),
  790. "AidlSrcs": hasAidl,
  791. "LexSrcs": hasLex,
  792. "ProtoSrcs": hasProto,
  793. "RenderscriptSrcs": hasRenderscript,
  794. "SyspropSrcs": hasSysprop,
  795. "WinMsgSrcs": hasWinMsg,
  796. "YaccSrsc": hasYacc,
  797. "OnlyCSrcs": !(hasAidl || hasLex || hasProto || hasRenderscript || hasSysprop || hasWinMsg || hasYacc),
  798. }
  799. }
  800. func (c *Module) SetPreventInstall() {
  801. c.Properties.PreventInstall = true
  802. }
  803. func (c *Module) SetHideFromMake() {
  804. c.Properties.HideFromMake = true
  805. }
  806. func (c *Module) HiddenFromMake() bool {
  807. return c.Properties.HideFromMake
  808. }
  809. func (c *Module) RequiredModuleNames() []string {
  810. required := android.CopyOf(c.ModuleBase.RequiredModuleNames())
  811. if c.ImageVariation().Variation == android.CoreVariation {
  812. required = append(required, c.Properties.Target.Platform.Required...)
  813. required = removeListFromList(required, c.Properties.Target.Platform.Exclude_required)
  814. } else if c.InRecovery() {
  815. required = append(required, c.Properties.Target.Recovery.Required...)
  816. required = removeListFromList(required, c.Properties.Target.Recovery.Exclude_required)
  817. }
  818. return android.FirstUniqueStrings(required)
  819. }
  820. func (c *Module) Toc() android.OptionalPath {
  821. if c.linker != nil {
  822. if library, ok := c.linker.(libraryInterface); ok {
  823. return library.toc()
  824. }
  825. }
  826. panic(fmt.Errorf("Toc() called on non-library module: %q", c.BaseModuleName()))
  827. }
  828. func (c *Module) ApiLevel() string {
  829. if c.linker != nil {
  830. if stub, ok := c.linker.(*stubDecorator); ok {
  831. return stub.apiLevel.String()
  832. }
  833. }
  834. panic(fmt.Errorf("ApiLevel() called on non-stub library module: %q", c.BaseModuleName()))
  835. }
  836. func (c *Module) Static() bool {
  837. if c.linker != nil {
  838. if library, ok := c.linker.(libraryInterface); ok {
  839. return library.static()
  840. }
  841. }
  842. panic(fmt.Errorf("Static() called on non-library module: %q", c.BaseModuleName()))
  843. }
  844. func (c *Module) Shared() bool {
  845. if c.linker != nil {
  846. if library, ok := c.linker.(libraryInterface); ok {
  847. return library.shared()
  848. }
  849. }
  850. panic(fmt.Errorf("Shared() called on non-library module: %q", c.BaseModuleName()))
  851. }
  852. func (c *Module) SelectedStl() string {
  853. if c.stl != nil {
  854. return c.stl.Properties.SelectedStl
  855. }
  856. return ""
  857. }
  858. func (c *Module) NdkPrebuiltStl() bool {
  859. if _, ok := c.linker.(*ndkPrebuiltStlLinker); ok {
  860. return true
  861. }
  862. return false
  863. }
  864. func (c *Module) StubDecorator() bool {
  865. if _, ok := c.linker.(*stubDecorator); ok {
  866. return true
  867. }
  868. return false
  869. }
  870. func (c *Module) SdkVersion() string {
  871. return String(c.Properties.Sdk_version)
  872. }
  873. func (c *Module) MinSdkVersion() string {
  874. return String(c.Properties.Min_sdk_version)
  875. }
  876. func (c *Module) isCrt() bool {
  877. if linker, ok := c.linker.(*objectLinker); ok {
  878. return linker.isCrt()
  879. }
  880. return false
  881. }
  882. func (c *Module) SplitPerApiLevel() bool {
  883. return c.canUseSdk() && c.isCrt()
  884. }
  885. func (c *Module) AlwaysSdk() bool {
  886. return c.Properties.AlwaysSdk || Bool(c.Properties.Sdk_variant_only)
  887. }
  888. func (c *Module) CcLibrary() bool {
  889. if c.linker != nil {
  890. if _, ok := c.linker.(*libraryDecorator); ok {
  891. return true
  892. }
  893. if _, ok := c.linker.(*prebuiltLibraryLinker); ok {
  894. return true
  895. }
  896. }
  897. return false
  898. }
  899. func (c *Module) CcLibraryInterface() bool {
  900. if _, ok := c.linker.(libraryInterface); ok {
  901. return true
  902. }
  903. return false
  904. }
  905. func (c *Module) RustLibraryInterface() bool {
  906. return false
  907. }
  908. func (c *Module) IsFuzzModule() bool {
  909. if _, ok := c.compiler.(*fuzzBinary); ok {
  910. return true
  911. }
  912. return false
  913. }
  914. func (c *Module) FuzzModuleStruct() fuzz.FuzzModule {
  915. return c.FuzzModule
  916. }
  917. func (c *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
  918. if fuzzer, ok := c.compiler.(*fuzzBinary); ok {
  919. return fuzzer.fuzzPackagedModule
  920. }
  921. panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", c.BaseModuleName()))
  922. }
  923. func (c *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
  924. if fuzzer, ok := c.compiler.(*fuzzBinary); ok {
  925. return fuzzer.sharedLibraries
  926. }
  927. panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", c.BaseModuleName()))
  928. }
  929. func (c *Module) NonCcVariants() bool {
  930. return false
  931. }
  932. func (c *Module) SetStatic() {
  933. if c.linker != nil {
  934. if library, ok := c.linker.(libraryInterface); ok {
  935. library.setStatic()
  936. return
  937. }
  938. }
  939. panic(fmt.Errorf("SetStatic called on non-library module: %q", c.BaseModuleName()))
  940. }
  941. func (c *Module) SetShared() {
  942. if c.linker != nil {
  943. if library, ok := c.linker.(libraryInterface); ok {
  944. library.setShared()
  945. return
  946. }
  947. }
  948. panic(fmt.Errorf("SetShared called on non-library module: %q", c.BaseModuleName()))
  949. }
  950. func (c *Module) BuildStaticVariant() bool {
  951. if c.linker != nil {
  952. if library, ok := c.linker.(libraryInterface); ok {
  953. return library.buildStatic()
  954. }
  955. }
  956. panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", c.BaseModuleName()))
  957. }
  958. func (c *Module) BuildSharedVariant() bool {
  959. if c.linker != nil {
  960. if library, ok := c.linker.(libraryInterface); ok {
  961. return library.buildShared()
  962. }
  963. }
  964. panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", c.BaseModuleName()))
  965. }
  966. func (c *Module) Module() android.Module {
  967. return c
  968. }
  969. func (c *Module) OutputFile() android.OptionalPath {
  970. return c.outputFile
  971. }
  972. func (c *Module) CoverageFiles() android.Paths {
  973. if c.linker != nil {
  974. if library, ok := c.linker.(libraryInterface); ok {
  975. return library.objs().coverageFiles
  976. }
  977. }
  978. panic(fmt.Errorf("CoverageFiles called on non-library module: %q", c.BaseModuleName()))
  979. }
  980. var _ LinkableInterface = (*Module)(nil)
  981. func (c *Module) UnstrippedOutputFile() android.Path {
  982. if c.linker != nil {
  983. return c.linker.unstrippedOutputFilePath()
  984. }
  985. return nil
  986. }
  987. func (c *Module) CoverageOutputFile() android.OptionalPath {
  988. if c.linker != nil {
  989. return c.linker.coverageOutputFilePath()
  990. }
  991. return android.OptionalPath{}
  992. }
  993. func (c *Module) RelativeInstallPath() string {
  994. if c.installer != nil {
  995. return c.installer.relativeInstallPath()
  996. }
  997. return ""
  998. }
  999. func (c *Module) VndkVersion() string {
  1000. return c.Properties.VndkVersion
  1001. }
  1002. func (c *Module) Init() android.Module {
  1003. c.AddProperties(&c.Properties, &c.VendorProperties)
  1004. if c.compiler != nil {
  1005. c.AddProperties(c.compiler.compilerProps()...)
  1006. }
  1007. if c.linker != nil {
  1008. c.AddProperties(c.linker.linkerProps()...)
  1009. }
  1010. if c.installer != nil {
  1011. c.AddProperties(c.installer.installerProps()...)
  1012. }
  1013. if c.stl != nil {
  1014. c.AddProperties(c.stl.props()...)
  1015. }
  1016. if c.sanitize != nil {
  1017. c.AddProperties(c.sanitize.props()...)
  1018. }
  1019. if c.coverage != nil {
  1020. c.AddProperties(c.coverage.props()...)
  1021. }
  1022. if c.fuzzer != nil {
  1023. c.AddProperties(c.fuzzer.props()...)
  1024. }
  1025. if c.sabi != nil {
  1026. c.AddProperties(c.sabi.props()...)
  1027. }
  1028. if c.vndkdep != nil {
  1029. c.AddProperties(c.vndkdep.props()...)
  1030. }
  1031. if c.lto != nil {
  1032. c.AddProperties(c.lto.props()...)
  1033. }
  1034. if c.afdo != nil {
  1035. c.AddProperties(c.afdo.props()...)
  1036. }
  1037. if c.pgo != nil {
  1038. c.AddProperties(c.pgo.props()...)
  1039. }
  1040. for _, feature := range c.features {
  1041. c.AddProperties(feature.props()...)
  1042. }
  1043. android.InitAndroidArchModule(c, c.hod, c.multilib)
  1044. if c.bazelable {
  1045. android.InitBazelModule(c)
  1046. }
  1047. android.InitApexModule(c)
  1048. android.InitDefaultableModule(c)
  1049. return c
  1050. }
  1051. // UseVndk() returns true if this module is built against VNDK.
  1052. // This means the vendor and product variants of a module.
  1053. func (c *Module) UseVndk() bool {
  1054. return c.Properties.VndkVersion != ""
  1055. }
  1056. func (c *Module) canUseSdk() bool {
  1057. return c.Os() == android.Android && c.Target().NativeBridge == android.NativeBridgeDisabled &&
  1058. !c.UseVndk() && !c.InRamdisk() && !c.InRecovery() && !c.InVendorRamdisk()
  1059. }
  1060. func (c *Module) UseSdk() bool {
  1061. if c.canUseSdk() {
  1062. return String(c.Properties.Sdk_version) != ""
  1063. }
  1064. return false
  1065. }
  1066. func (c *Module) isCoverageVariant() bool {
  1067. return c.coverage.Properties.IsCoverageVariant
  1068. }
  1069. func (c *Module) IsNdk(config android.Config) bool {
  1070. return inList(c.BaseModuleName(), *getNDKKnownLibs(config))
  1071. }
  1072. func (c *Module) IsLlndk() bool {
  1073. return c.VendorProperties.IsLLNDK
  1074. }
  1075. func (c *Module) IsLlndkPublic() bool {
  1076. return c.VendorProperties.IsLLNDK && !c.VendorProperties.IsVNDKPrivate
  1077. }
  1078. func (m *Module) NeedsLlndkVariants() bool {
  1079. lib := moduleLibraryInterface(m)
  1080. return lib != nil && (lib.hasLLNDKStubs() || lib.hasLLNDKHeaders())
  1081. }
  1082. func (m *Module) NeedsVendorPublicLibraryVariants() bool {
  1083. lib := moduleLibraryInterface(m)
  1084. return lib != nil && (lib.hasVendorPublicLibrary())
  1085. }
  1086. // IsVendorPublicLibrary returns true for vendor public libraries.
  1087. func (c *Module) IsVendorPublicLibrary() bool {
  1088. return c.VendorProperties.IsVendorPublicLibrary
  1089. }
  1090. func (c *Module) IsVndkPrebuiltLibrary() bool {
  1091. if _, ok := c.linker.(*vndkPrebuiltLibraryDecorator); ok {
  1092. return true
  1093. }
  1094. return false
  1095. }
  1096. func (c *Module) SdkAndPlatformVariantVisibleToMake() bool {
  1097. return c.Properties.SdkAndPlatformVariantVisibleToMake
  1098. }
  1099. func (c *Module) HasLlndkStubs() bool {
  1100. lib := moduleLibraryInterface(c)
  1101. return lib != nil && lib.hasLLNDKStubs()
  1102. }
  1103. func (c *Module) StubsVersion() string {
  1104. if lib, ok := c.linker.(versionedInterface); ok {
  1105. return lib.stubsVersion()
  1106. }
  1107. panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", c.BaseModuleName()))
  1108. }
  1109. // isImplementationForLLNDKPublic returns true for any variant of a cc_library that has LLNDK stubs
  1110. // and does not set llndk.vendor_available: false.
  1111. func (c *Module) isImplementationForLLNDKPublic() bool {
  1112. library, _ := c.library.(*libraryDecorator)
  1113. return library != nil && library.hasLLNDKStubs() &&
  1114. !Bool(library.Properties.Llndk.Private)
  1115. }
  1116. // Returns true for LLNDK-private, VNDK-SP-private, and VNDK-core-private.
  1117. func (c *Module) IsVndkPrivate() bool {
  1118. // Check if VNDK-core-private or VNDK-SP-private
  1119. if c.IsVndk() {
  1120. return Bool(c.vndkdep.Properties.Vndk.Private)
  1121. }
  1122. // Check if LLNDK-private
  1123. if library, ok := c.library.(*libraryDecorator); ok && c.IsLlndk() {
  1124. return Bool(library.Properties.Llndk.Private)
  1125. }
  1126. return false
  1127. }
  1128. // IsVndk() returns true if this module has a vndk variant.
  1129. // Note that IsVndk() returns true for all variants of vndk-enabled libraries. Not only vendor variant,
  1130. // but also platform and product variants of vndk-enabled libraries return true for IsVndk().
  1131. func (c *Module) IsVndk() bool {
  1132. if vndkdep := c.vndkdep; vndkdep != nil {
  1133. return vndkdep.isVndk()
  1134. }
  1135. return false
  1136. }
  1137. func (c *Module) isAfdoCompile() bool {
  1138. if afdo := c.afdo; afdo != nil {
  1139. return afdo.Properties.FdoProfilePath != nil
  1140. }
  1141. return false
  1142. }
  1143. func (c *Module) isPgoCompile() bool {
  1144. if pgo := c.pgo; pgo != nil {
  1145. return pgo.Properties.PgoCompile
  1146. }
  1147. return false
  1148. }
  1149. func (c *Module) isCfi() bool {
  1150. if sanitize := c.sanitize; sanitize != nil {
  1151. return Bool(sanitize.Properties.Sanitize.Cfi)
  1152. }
  1153. return false
  1154. }
  1155. func (c *Module) isFuzzer() bool {
  1156. if sanitize := c.sanitize; sanitize != nil {
  1157. return Bool(sanitize.Properties.SanitizeMutated.Fuzzer)
  1158. }
  1159. return false
  1160. }
  1161. func (c *Module) isNDKStubLibrary() bool {
  1162. if _, ok := c.compiler.(*stubDecorator); ok {
  1163. return true
  1164. }
  1165. return false
  1166. }
  1167. func (c *Module) IsVndkSp() bool {
  1168. if vndkdep := c.vndkdep; vndkdep != nil {
  1169. return vndkdep.isVndkSp()
  1170. }
  1171. return false
  1172. }
  1173. func (c *Module) IsVndkExt() bool {
  1174. if vndkdep := c.vndkdep; vndkdep != nil {
  1175. return vndkdep.isVndkExt()
  1176. }
  1177. return false
  1178. }
  1179. func (c *Module) SubName() string {
  1180. return c.Properties.SubName
  1181. }
  1182. func (c *Module) MustUseVendorVariant() bool {
  1183. return c.IsVndkSp() || c.Properties.MustUseVendorVariant
  1184. }
  1185. func (c *Module) getVndkExtendsModuleName() string {
  1186. if vndkdep := c.vndkdep; vndkdep != nil {
  1187. return vndkdep.getVndkExtendsModuleName()
  1188. }
  1189. return ""
  1190. }
  1191. func (c *Module) IsStubs() bool {
  1192. if lib := c.library; lib != nil {
  1193. return lib.buildStubs()
  1194. }
  1195. return false
  1196. }
  1197. func (c *Module) HasStubsVariants() bool {
  1198. if lib := c.library; lib != nil {
  1199. return lib.hasStubsVariants()
  1200. }
  1201. return false
  1202. }
  1203. func (c *Module) IsStubsImplementationRequired() bool {
  1204. if lib := c.library; lib != nil {
  1205. return lib.isStubsImplementationRequired()
  1206. }
  1207. return false
  1208. }
  1209. // If this is a stubs library, ImplementationModuleName returns the name of the module that contains
  1210. // the implementation. If it is an implementation library it returns its own name.
  1211. func (c *Module) ImplementationModuleName(ctx android.BaseModuleContext) string {
  1212. name := ctx.OtherModuleName(c)
  1213. if versioned, ok := c.linker.(versionedInterface); ok {
  1214. name = versioned.implementationModuleName(name)
  1215. }
  1216. return name
  1217. }
  1218. // Similar to ImplementationModuleName, but uses the Make variant of the module
  1219. // name as base name, for use in AndroidMk output. E.g. for a prebuilt module
  1220. // where the Soong name is prebuilt_foo, this returns foo (which works in Make
  1221. // under the premise that the prebuilt module overrides its source counterpart
  1222. // if it is exposed to Make).
  1223. func (c *Module) ImplementationModuleNameForMake(ctx android.BaseModuleContext) string {
  1224. name := c.BaseModuleName()
  1225. if versioned, ok := c.linker.(versionedInterface); ok {
  1226. name = versioned.implementationModuleName(name)
  1227. }
  1228. return name
  1229. }
  1230. func (c *Module) Bootstrap() bool {
  1231. return Bool(c.Properties.Bootstrap)
  1232. }
  1233. func (c *Module) nativeCoverage() bool {
  1234. // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
  1235. if c.Target().NativeBridge == android.NativeBridgeEnabled {
  1236. return false
  1237. }
  1238. return c.linker != nil && c.linker.nativeCoverage()
  1239. }
  1240. func (c *Module) IsSnapshotPrebuilt() bool {
  1241. if p, ok := c.linker.(SnapshotInterface); ok {
  1242. return p.IsSnapshotPrebuilt()
  1243. }
  1244. return false
  1245. }
  1246. func (c *Module) ExcludeFromVendorSnapshot() bool {
  1247. return Bool(c.Properties.Exclude_from_vendor_snapshot)
  1248. }
  1249. func (c *Module) ExcludeFromRecoverySnapshot() bool {
  1250. return Bool(c.Properties.Exclude_from_recovery_snapshot)
  1251. }
  1252. func isBionic(name string) bool {
  1253. switch name {
  1254. case "libc", "libm", "libdl", "libdl_android", "linker":
  1255. return true
  1256. }
  1257. return false
  1258. }
  1259. func InstallToBootstrap(name string, config android.Config) bool {
  1260. // NOTE: also update //build/bazel/rules/apex/cc.bzl#_installed_to_bootstrap
  1261. // if this list is updated.
  1262. if name == "libclang_rt.hwasan" || name == "libc_hwasan" {
  1263. return true
  1264. }
  1265. return isBionic(name)
  1266. }
  1267. func (c *Module) XrefCcFiles() android.Paths {
  1268. return c.kytheFiles
  1269. }
  1270. func (c *Module) isCfiAssemblySupportEnabled() bool {
  1271. return c.sanitize != nil &&
  1272. Bool(c.sanitize.Properties.Sanitize.Config.Cfi_assembly_support)
  1273. }
  1274. func (c *Module) InstallInRoot() bool {
  1275. return c.installer != nil && c.installer.installInRoot()
  1276. }
  1277. type baseModuleContext struct {
  1278. android.BaseModuleContext
  1279. moduleContextImpl
  1280. }
  1281. type depsContext struct {
  1282. android.BottomUpMutatorContext
  1283. moduleContextImpl
  1284. }
  1285. type moduleContext struct {
  1286. android.ModuleContext
  1287. moduleContextImpl
  1288. }
  1289. type moduleContextImpl struct {
  1290. mod *Module
  1291. ctx BaseModuleContext
  1292. }
  1293. func (ctx *moduleContextImpl) toolchain() config.Toolchain {
  1294. return ctx.mod.toolchain(ctx.ctx)
  1295. }
  1296. func (ctx *moduleContextImpl) static() bool {
  1297. return ctx.mod.static()
  1298. }
  1299. func (ctx *moduleContextImpl) staticBinary() bool {
  1300. return ctx.mod.staticBinary()
  1301. }
  1302. func (ctx *moduleContextImpl) testBinary() bool {
  1303. return ctx.mod.testBinary()
  1304. }
  1305. func (ctx *moduleContextImpl) testLibrary() bool {
  1306. return ctx.mod.testLibrary()
  1307. }
  1308. func (ctx *moduleContextImpl) header() bool {
  1309. return ctx.mod.Header()
  1310. }
  1311. func (ctx *moduleContextImpl) binary() bool {
  1312. return ctx.mod.Binary()
  1313. }
  1314. func (ctx *moduleContextImpl) object() bool {
  1315. return ctx.mod.Object()
  1316. }
  1317. func (ctx *moduleContextImpl) canUseSdk() bool {
  1318. return ctx.mod.canUseSdk()
  1319. }
  1320. func (ctx *moduleContextImpl) useSdk() bool {
  1321. return ctx.mod.UseSdk()
  1322. }
  1323. func (ctx *moduleContextImpl) sdkVersion() string {
  1324. if ctx.ctx.Device() {
  1325. if ctx.useVndk() {
  1326. vndkVer := ctx.mod.VndkVersion()
  1327. if inList(vndkVer, ctx.ctx.Config().PlatformVersionActiveCodenames()) {
  1328. return "current"
  1329. }
  1330. return vndkVer
  1331. }
  1332. return String(ctx.mod.Properties.Sdk_version)
  1333. }
  1334. return ""
  1335. }
  1336. func (ctx *moduleContextImpl) minSdkVersion() string {
  1337. ver := ctx.mod.MinSdkVersion()
  1338. if ver == "apex_inherit" && !ctx.isForPlatform() {
  1339. ver = ctx.apexSdkVersion().String()
  1340. }
  1341. if ver == "apex_inherit" || ver == "" {
  1342. ver = ctx.sdkVersion()
  1343. }
  1344. // For crt objects, the meaning of min_sdk_version is very different from other types of
  1345. // module. For them, min_sdk_version defines the oldest version that the build system will
  1346. // create versioned variants for. For example, if min_sdk_version is 16, then sdk variant of
  1347. // the crt object has local variants of 16, 17, ..., up to the latest version. sdk_version
  1348. // and min_sdk_version properties of the variants are set to the corresponding version
  1349. // numbers. However, the non-sdk variant (for apex or platform) of the crt object is left
  1350. // untouched. min_sdk_version: 16 doesn't actually mean that the non-sdk variant has to
  1351. // support such an old version. The version is set to the later version in case when the
  1352. // non-sdk variant is for the platform, or the min_sdk_version of the containing APEX if
  1353. // it's for an APEX.
  1354. if ctx.mod.isCrt() && !ctx.isSdkVariant() {
  1355. if ctx.isForPlatform() {
  1356. ver = strconv.Itoa(android.FutureApiLevelInt)
  1357. } else { // for apex
  1358. ver = ctx.apexSdkVersion().String()
  1359. if ver == "" { // in case when min_sdk_version was not set by the APEX
  1360. ver = ctx.sdkVersion()
  1361. }
  1362. }
  1363. }
  1364. // Also make sure that minSdkVersion is not greater than sdkVersion, if they are both numbers
  1365. sdkVersionInt, err := strconv.Atoi(ctx.sdkVersion())
  1366. minSdkVersionInt, err2 := strconv.Atoi(ver)
  1367. if err == nil && err2 == nil {
  1368. if sdkVersionInt < minSdkVersionInt {
  1369. return strconv.Itoa(sdkVersionInt)
  1370. }
  1371. }
  1372. return ver
  1373. }
  1374. func (ctx *moduleContextImpl) isSdkVariant() bool {
  1375. return ctx.mod.IsSdkVariant()
  1376. }
  1377. func (ctx *moduleContextImpl) useVndk() bool {
  1378. return ctx.mod.UseVndk()
  1379. }
  1380. func (ctx *moduleContextImpl) isNdk(config android.Config) bool {
  1381. return ctx.mod.IsNdk(config)
  1382. }
  1383. func (ctx *moduleContextImpl) IsLlndk() bool {
  1384. return ctx.mod.IsLlndk()
  1385. }
  1386. func (ctx *moduleContextImpl) IsLlndkPublic() bool {
  1387. return ctx.mod.IsLlndkPublic()
  1388. }
  1389. func (ctx *moduleContextImpl) isImplementationForLLNDKPublic() bool {
  1390. return ctx.mod.isImplementationForLLNDKPublic()
  1391. }
  1392. func (ctx *moduleContextImpl) IsVndkPrivate() bool {
  1393. return ctx.mod.IsVndkPrivate()
  1394. }
  1395. func (ctx *moduleContextImpl) isVndk() bool {
  1396. return ctx.mod.IsVndk()
  1397. }
  1398. func (ctx *moduleContextImpl) isAfdoCompile() bool {
  1399. return ctx.mod.isAfdoCompile()
  1400. }
  1401. func (ctx *moduleContextImpl) isPgoCompile() bool {
  1402. return ctx.mod.isPgoCompile()
  1403. }
  1404. func (ctx *moduleContextImpl) isCfi() bool {
  1405. return ctx.mod.isCfi()
  1406. }
  1407. func (ctx *moduleContextImpl) isFuzzer() bool {
  1408. return ctx.mod.isFuzzer()
  1409. }
  1410. func (ctx *moduleContextImpl) isNDKStubLibrary() bool {
  1411. return ctx.mod.isNDKStubLibrary()
  1412. }
  1413. func (ctx *moduleContextImpl) isVndkSp() bool {
  1414. return ctx.mod.IsVndkSp()
  1415. }
  1416. func (ctx *moduleContextImpl) IsVndkExt() bool {
  1417. return ctx.mod.IsVndkExt()
  1418. }
  1419. func (ctx *moduleContextImpl) IsVendorPublicLibrary() bool {
  1420. return ctx.mod.IsVendorPublicLibrary()
  1421. }
  1422. func (ctx *moduleContextImpl) mustUseVendorVariant() bool {
  1423. return ctx.mod.MustUseVendorVariant()
  1424. }
  1425. func (ctx *moduleContextImpl) selectedStl() string {
  1426. if stl := ctx.mod.stl; stl != nil {
  1427. return stl.Properties.SelectedStl
  1428. }
  1429. return ""
  1430. }
  1431. func (ctx *moduleContextImpl) useClangLld(actx ModuleContext) bool {
  1432. return ctx.mod.linker.useClangLld(actx)
  1433. }
  1434. func (ctx *moduleContextImpl) baseModuleName() string {
  1435. return ctx.mod.ModuleBase.BaseModuleName()
  1436. }
  1437. func (ctx *moduleContextImpl) getVndkExtendsModuleName() string {
  1438. return ctx.mod.getVndkExtendsModuleName()
  1439. }
  1440. func (ctx *moduleContextImpl) isForPlatform() bool {
  1441. return ctx.ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform()
  1442. }
  1443. func (ctx *moduleContextImpl) apexVariationName() string {
  1444. return ctx.ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).ApexVariationName
  1445. }
  1446. func (ctx *moduleContextImpl) apexSdkVersion() android.ApiLevel {
  1447. return ctx.mod.apexSdkVersion
  1448. }
  1449. func (ctx *moduleContextImpl) bootstrap() bool {
  1450. return ctx.mod.Bootstrap()
  1451. }
  1452. func (ctx *moduleContextImpl) nativeCoverage() bool {
  1453. return ctx.mod.nativeCoverage()
  1454. }
  1455. func (ctx *moduleContextImpl) directlyInAnyApex() bool {
  1456. return ctx.mod.DirectlyInAnyApex()
  1457. }
  1458. func (ctx *moduleContextImpl) isPreventInstall() bool {
  1459. return ctx.mod.Properties.PreventInstall
  1460. }
  1461. func (ctx *moduleContextImpl) getSharedFlags() *SharedFlags {
  1462. shared := &ctx.mod.sharedFlags
  1463. if shared.flagsMap == nil {
  1464. shared.numSharedFlags = 0
  1465. shared.flagsMap = make(map[string]string)
  1466. }
  1467. return shared
  1468. }
  1469. func (ctx *moduleContextImpl) isCfiAssemblySupportEnabled() bool {
  1470. return ctx.mod.isCfiAssemblySupportEnabled()
  1471. }
  1472. func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
  1473. return &Module{
  1474. hod: hod,
  1475. multilib: multilib,
  1476. }
  1477. }
  1478. func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
  1479. module := newBaseModule(hod, multilib)
  1480. module.features = []feature{
  1481. &tidyFeature{},
  1482. }
  1483. module.stl = &stl{}
  1484. module.sanitize = &sanitize{}
  1485. module.coverage = &coverage{}
  1486. module.fuzzer = &fuzzer{}
  1487. module.sabi = &sabi{}
  1488. module.vndkdep = &vndkdep{}
  1489. module.lto = &lto{}
  1490. module.afdo = &afdo{}
  1491. module.pgo = &pgo{}
  1492. return module
  1493. }
  1494. func (c *Module) Prebuilt() *android.Prebuilt {
  1495. if p, ok := c.linker.(prebuiltLinkerInterface); ok {
  1496. return p.prebuilt()
  1497. }
  1498. return nil
  1499. }
  1500. func (c *Module) IsPrebuilt() bool {
  1501. return c.Prebuilt() != nil
  1502. }
  1503. func (c *Module) Name() string {
  1504. name := c.ModuleBase.Name()
  1505. if p, ok := c.linker.(interface {
  1506. Name(string) string
  1507. }); ok {
  1508. name = p.Name(name)
  1509. }
  1510. return name
  1511. }
  1512. func (c *Module) Symlinks() []string {
  1513. if p, ok := c.installer.(interface {
  1514. symlinkList() []string
  1515. }); ok {
  1516. return p.symlinkList()
  1517. }
  1518. return nil
  1519. }
  1520. func (c *Module) IsTestPerSrcAllTestsVariation() bool {
  1521. test, ok := c.linker.(testPerSrc)
  1522. return ok && test.isAllTestsVariation()
  1523. }
  1524. func (c *Module) DataPaths() []android.DataPath {
  1525. if p, ok := c.installer.(interface {
  1526. dataPaths() []android.DataPath
  1527. }); ok {
  1528. return p.dataPaths()
  1529. }
  1530. return nil
  1531. }
  1532. func getNameSuffixWithVndkVersion(ctx android.ModuleContext, c LinkableInterface) string {
  1533. // Returns the name suffix for product and vendor variants. If the VNDK version is not
  1534. // "current", it will append the VNDK version to the name suffix.
  1535. var vndkVersion string
  1536. var nameSuffix string
  1537. if c.InProduct() {
  1538. if c.ProductSpecific() {
  1539. // If the module is product specific with 'product_specific: true',
  1540. // do not add a name suffix because it is a base module.
  1541. return ""
  1542. }
  1543. vndkVersion = ctx.DeviceConfig().ProductVndkVersion()
  1544. nameSuffix = ProductSuffix
  1545. } else {
  1546. vndkVersion = ctx.DeviceConfig().VndkVersion()
  1547. nameSuffix = VendorSuffix
  1548. }
  1549. if vndkVersion == "current" {
  1550. vndkVersion = ctx.DeviceConfig().PlatformVndkVersion()
  1551. }
  1552. if c.VndkVersion() != vndkVersion && c.VndkVersion() != "" {
  1553. // add version suffix only if the module is using different vndk version than the
  1554. // version in product or vendor partition.
  1555. nameSuffix += "." + c.VndkVersion()
  1556. }
  1557. return nameSuffix
  1558. }
  1559. func GetSubnameProperty(actx android.ModuleContext, c LinkableInterface) string {
  1560. var subName = ""
  1561. if c.Target().NativeBridge == android.NativeBridgeEnabled {
  1562. subName += NativeBridgeSuffix
  1563. }
  1564. llndk := c.IsLlndk()
  1565. if llndk || (c.UseVndk() && c.HasNonSystemVariants()) {
  1566. // .vendor.{version} suffix is added for vendor variant or .product.{version} suffix is
  1567. // added for product variant only when we have vendor and product variants with core
  1568. // variant. The suffix is not added for vendor-only or product-only module.
  1569. subName += getNameSuffixWithVndkVersion(actx, c)
  1570. } else if c.IsVendorPublicLibrary() {
  1571. subName += vendorPublicLibrarySuffix
  1572. } else if c.IsVndkPrebuiltLibrary() {
  1573. // .vendor suffix is added for backward compatibility with VNDK snapshot whose names with
  1574. // such suffixes are already hard-coded in prebuilts/vndk/.../Android.bp.
  1575. subName += VendorSuffix
  1576. } else if c.InRamdisk() && !c.OnlyInRamdisk() {
  1577. subName += RamdiskSuffix
  1578. } else if c.InVendorRamdisk() && !c.OnlyInVendorRamdisk() {
  1579. subName += VendorRamdiskSuffix
  1580. } else if c.InRecovery() && !c.OnlyInRecovery() {
  1581. subName += RecoverySuffix
  1582. } else if c.IsSdkVariant() && (c.SdkAndPlatformVariantVisibleToMake() || c.SplitPerApiLevel()) {
  1583. subName += sdkSuffix
  1584. if c.SplitPerApiLevel() {
  1585. subName += "." + c.SdkVersion()
  1586. }
  1587. } else if c.IsStubs() && c.IsSdkVariant() {
  1588. // Public API surface (NDK)
  1589. // Add a suffix to this stub variant to distinguish it from the module-lib stub variant.
  1590. subName = sdkSuffix
  1591. }
  1592. return subName
  1593. }
  1594. var _ android.MixedBuildBuildable = (*Module)(nil)
  1595. func (c *Module) getBazelModuleLabel(ctx android.BaseModuleContext) string {
  1596. var bazelModuleLabel string
  1597. if c.typ() == fullLibrary && c.static() {
  1598. // cc_library is a special case in bp2build; two targets are generated -- one for each
  1599. // of the shared and static variants. The shared variant keeps the module name, but the
  1600. // static variant uses a different suffixed name.
  1601. bazelModuleLabel = bazelLabelForStaticModule(ctx, c)
  1602. } else {
  1603. bazelModuleLabel = c.GetBazelLabel(ctx, c)
  1604. }
  1605. labelNoPrebuilt := bazelModuleLabel
  1606. if c.IsPrebuilt() {
  1607. labelNoPrebuilt = android.RemoveOptionalPrebuiltPrefixFromBazelLabel(bazelModuleLabel)
  1608. }
  1609. return labelNoPrebuilt
  1610. }
  1611. func (c *Module) QueueBazelCall(ctx android.BaseModuleContext) {
  1612. c.bazelHandler.QueueBazelCall(ctx, c.getBazelModuleLabel(ctx))
  1613. }
  1614. // IsMixedBuildSupported returns true if the module should be analyzed by Bazel
  1615. // in any of the --bazel-mode(s).
  1616. func (c *Module) IsMixedBuildSupported(ctx android.BaseModuleContext) bool {
  1617. if !allEnabledSanitizersSupportedByBazel(ctx, c) {
  1618. //TODO(b/278772861) support sanitizers in Bazel rules
  1619. return false
  1620. }
  1621. return c.bazelHandler != nil
  1622. }
  1623. func allEnabledSanitizersSupportedByBazel(ctx android.BaseModuleContext, c *Module) bool {
  1624. if c.sanitize == nil {
  1625. return true
  1626. }
  1627. sanitizeProps := &c.sanitize.Properties.SanitizeMutated
  1628. unsupportedSanitizers := []*bool{
  1629. sanitizeProps.Safestack,
  1630. sanitizeProps.Scudo,
  1631. BoolPtr(len(c.sanitize.Properties.Sanitize.Recover) > 0),
  1632. }
  1633. for _, san := range unsupportedSanitizers {
  1634. if Bool(san) {
  1635. return false
  1636. }
  1637. }
  1638. for _, san := range Sanitizers {
  1639. if san == intOverflow {
  1640. // TODO(b/261058727): enable mixed builds for all modules with UBSan
  1641. // Currently we can only support ubsan when minimum runtime is used.
  1642. ubsanEnabled := Bool(sanitizeProps.Integer_overflow) || len(sanitizeProps.Misc_undefined) > 0
  1643. if !ubsanEnabled || c.MinimalRuntimeNeeded() {
  1644. continue
  1645. }
  1646. } else if san == cfi {
  1647. apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
  1648. // Only allow cfi if this is an apex variant
  1649. if !apexInfo.IsForPlatform() {
  1650. continue
  1651. }
  1652. }
  1653. if c.sanitize.isSanitizerEnabled(san) {
  1654. return false
  1655. }
  1656. }
  1657. return true
  1658. }
  1659. func GetApexConfigKey(ctx android.BaseModuleContext) *android.ApexConfigKey {
  1660. apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
  1661. if !apexInfo.IsForPlatform() {
  1662. if !ctx.Config().BazelContext.IsModuleDclaAllowed(ctx.Module().Name()) {
  1663. return nil
  1664. }
  1665. apexKey := android.ApexConfigKey{
  1666. WithinApex: true,
  1667. ApexSdkVersion: findApexSdkVersion(ctx, apexInfo).String(),
  1668. }
  1669. return &apexKey
  1670. }
  1671. return nil
  1672. }
  1673. func (c *Module) ProcessBazelQueryResponse(ctx android.ModuleContext) {
  1674. bazelModuleLabel := c.getBazelModuleLabel(ctx)
  1675. c.bazelHandler.ProcessBazelQueryResponse(ctx, bazelModuleLabel)
  1676. c.Properties.SubName = GetSubnameProperty(ctx, c)
  1677. apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
  1678. if !apexInfo.IsForPlatform() {
  1679. c.hideApexVariantFromMake = true
  1680. }
  1681. c.makeLinkType = GetMakeLinkType(ctx, c)
  1682. mctx := &moduleContext{
  1683. ModuleContext: ctx,
  1684. moduleContextImpl: moduleContextImpl{
  1685. mod: c,
  1686. },
  1687. }
  1688. mctx.ctx = mctx
  1689. // TODO(b/244432500): Get the tradefed config from the bazel target instead
  1690. // of generating it with Soong.
  1691. c.maybeInstall(mctx, apexInfo)
  1692. }
  1693. func moduleContextFromAndroidModuleContext(actx android.ModuleContext, c *Module) ModuleContext {
  1694. ctx := &moduleContext{
  1695. ModuleContext: actx,
  1696. moduleContextImpl: moduleContextImpl{
  1697. mod: c,
  1698. },
  1699. }
  1700. ctx.ctx = ctx
  1701. return ctx
  1702. }
  1703. // TODO (b/277651159): Remove this allowlist
  1704. var (
  1705. skipStubLibraryMultipleApexViolation = map[string]bool{
  1706. "libclang_rt.asan": true,
  1707. "libclang_rt.hwasan": true,
  1708. // runtime apex
  1709. "libc": true,
  1710. "libc_hwasan": true,
  1711. "libdl_android": true,
  1712. "libm": true,
  1713. "libdl": true,
  1714. // art apex
  1715. "libandroidio": true,
  1716. "libdexfile": true,
  1717. "libnativebridge": true,
  1718. "libnativehelper": true,
  1719. "libnativeloader": true,
  1720. "libsigchain": true,
  1721. }
  1722. )
  1723. // Returns true if a stub library could be installed in multiple apexes
  1724. func (c *Module) stubLibraryMultipleApexViolation(ctx android.ModuleContext) bool {
  1725. // If this is not an apex variant, no check necessary
  1726. if !c.InAnyApex() {
  1727. return false
  1728. }
  1729. // If this is not a stub library, no check necessary
  1730. if !c.HasStubsVariants() {
  1731. return false
  1732. }
  1733. // Skip the allowlist
  1734. // Use BaseModuleName so that this matches prebuilts.
  1735. if _, exists := skipStubLibraryMultipleApexViolation[c.BaseModuleName()]; exists {
  1736. return false
  1737. }
  1738. _, aaWithoutTestApexes, _ := android.ListSetDifference(c.ApexAvailable(), c.TestApexes())
  1739. // Stub libraries should not have more than one apex_available
  1740. if len(aaWithoutTestApexes) > 1 {
  1741. return true
  1742. }
  1743. // Stub libraries should not use the wildcard
  1744. if aaWithoutTestApexes[0] == android.AvailableToAnyApex {
  1745. return true
  1746. }
  1747. // Default: no violation
  1748. return false
  1749. }
  1750. func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
  1751. // Handle the case of a test module split by `test_per_src` mutator.
  1752. //
  1753. // The `test_per_src` mutator adds an extra variation named "", depending on all the other
  1754. // `test_per_src` variations of the test module. Set `outputFile` to an empty path for this
  1755. // module and return early, as this module does not produce an output file per se.
  1756. if c.IsTestPerSrcAllTestsVariation() {
  1757. c.outputFile = android.OptionalPath{}
  1758. return
  1759. }
  1760. c.Properties.SubName = GetSubnameProperty(actx, c)
  1761. apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
  1762. if !apexInfo.IsForPlatform() {
  1763. c.hideApexVariantFromMake = true
  1764. }
  1765. c.makeLinkType = GetMakeLinkType(actx, c)
  1766. ctx := moduleContextFromAndroidModuleContext(actx, c)
  1767. deps := c.depsToPaths(ctx)
  1768. if ctx.Failed() {
  1769. return
  1770. }
  1771. if c.stubLibraryMultipleApexViolation(actx) {
  1772. actx.PropertyErrorf("apex_available",
  1773. "Stub libraries should have a single apex_available (test apexes excluded). Got %v", c.ApexAvailable())
  1774. }
  1775. if c.Properties.Clang != nil && *c.Properties.Clang == false {
  1776. ctx.PropertyErrorf("clang", "false (GCC) is no longer supported")
  1777. } else if c.Properties.Clang != nil && !ctx.DeviceConfig().BuildBrokenClangProperty() {
  1778. ctx.PropertyErrorf("clang", "property is deprecated, see Changes.md file")
  1779. }
  1780. flags := Flags{
  1781. Toolchain: c.toolchain(ctx),
  1782. EmitXrefs: ctx.Config().EmitXrefRules(),
  1783. }
  1784. if c.compiler != nil {
  1785. flags = c.compiler.compilerFlags(ctx, flags, deps)
  1786. }
  1787. if c.linker != nil {
  1788. flags = c.linker.linkerFlags(ctx, flags)
  1789. }
  1790. if c.stl != nil {
  1791. flags = c.stl.flags(ctx, flags)
  1792. }
  1793. if c.sanitize != nil {
  1794. flags = c.sanitize.flags(ctx, flags)
  1795. }
  1796. if c.coverage != nil {
  1797. flags, deps = c.coverage.flags(ctx, flags, deps)
  1798. }
  1799. if c.fuzzer != nil {
  1800. flags = c.fuzzer.flags(ctx, flags)
  1801. }
  1802. if c.lto != nil {
  1803. flags = c.lto.flags(ctx, flags)
  1804. }
  1805. if c.afdo != nil {
  1806. flags = c.afdo.flags(ctx, flags)
  1807. }
  1808. if c.pgo != nil {
  1809. flags = c.pgo.flags(ctx, flags)
  1810. }
  1811. for _, feature := range c.features {
  1812. flags = feature.flags(ctx, flags)
  1813. }
  1814. if ctx.Failed() {
  1815. return
  1816. }
  1817. flags.Local.CFlags, _ = filterList(flags.Local.CFlags, config.IllegalFlags)
  1818. flags.Local.CppFlags, _ = filterList(flags.Local.CppFlags, config.IllegalFlags)
  1819. flags.Local.ConlyFlags, _ = filterList(flags.Local.ConlyFlags, config.IllegalFlags)
  1820. flags.Local.CommonFlags = append(flags.Local.CommonFlags, deps.Flags...)
  1821. for _, dir := range deps.IncludeDirs {
  1822. flags.Local.CommonFlags = append(flags.Local.CommonFlags, "-I"+dir.String())
  1823. }
  1824. for _, dir := range deps.SystemIncludeDirs {
  1825. flags.Local.CommonFlags = append(flags.Local.CommonFlags, "-isystem "+dir.String())
  1826. }
  1827. flags.Local.LdFlags = append(flags.Local.LdFlags, deps.LdFlags...)
  1828. c.flags = flags
  1829. // We need access to all the flags seen by a source file.
  1830. if c.sabi != nil {
  1831. flags = c.sabi.flags(ctx, flags)
  1832. }
  1833. flags.AssemblerWithCpp = inList("-xassembler-with-cpp", flags.Local.AsFlags)
  1834. var objs Objects
  1835. if c.compiler != nil {
  1836. objs = c.compiler.compile(ctx, flags, deps)
  1837. if ctx.Failed() {
  1838. return
  1839. }
  1840. c.kytheFiles = objs.kytheFiles
  1841. c.objFiles = objs.objFiles
  1842. c.tidyFiles = objs.tidyFiles
  1843. }
  1844. if c.linker != nil {
  1845. outputFile := c.linker.link(ctx, flags, deps, objs)
  1846. if ctx.Failed() {
  1847. return
  1848. }
  1849. c.outputFile = android.OptionalPathForPath(outputFile)
  1850. c.maybeUnhideFromMake()
  1851. // glob exported headers for snapshot, if BOARD_VNDK_VERSION is current or
  1852. // RECOVERY_SNAPSHOT_VERSION is current.
  1853. if i, ok := c.linker.(snapshotLibraryInterface); ok {
  1854. if ShouldCollectHeadersForSnapshot(ctx, c, apexInfo) {
  1855. i.collectHeadersForSnapshot(ctx)
  1856. }
  1857. }
  1858. }
  1859. c.maybeInstall(ctx, apexInfo)
  1860. }
  1861. func (c *Module) maybeUnhideFromMake() {
  1862. // If a lib is directly included in any of the APEXes or is not available to the
  1863. // platform (which is often the case when the stub is provided as a prebuilt),
  1864. // unhide the stubs variant having the latest version gets visible to make. In
  1865. // addition, the non-stubs variant is renamed to <libname>.bootstrap. This is to
  1866. // force anything in the make world to link against the stubs library. (unless it
  1867. // is explicitly referenced via .bootstrap suffix or the module is marked with
  1868. // 'bootstrap: true').
  1869. if c.HasStubsVariants() && c.NotInPlatform() && !c.InRamdisk() &&
  1870. !c.InRecovery() && !c.UseVndk() && !c.static() && !c.isCoverageVariant() &&
  1871. c.IsStubs() && !c.InVendorRamdisk() {
  1872. c.Properties.HideFromMake = false // unhide
  1873. // Note: this is still non-installable
  1874. }
  1875. }
  1876. // maybeInstall is called at the end of both GenerateAndroidBuildActions and
  1877. // ProcessBazelQueryResponse to run the install hooks for installable modules,
  1878. // like binaries and tests.
  1879. func (c *Module) maybeInstall(ctx ModuleContext, apexInfo android.ApexInfo) {
  1880. if !proptools.BoolDefault(c.Installable(), true) {
  1881. // If the module has been specifically configure to not be installed then
  1882. // hide from make as otherwise it will break when running inside make
  1883. // as the output path to install will not be specified. Not all uninstallable
  1884. // modules can be hidden from make as some are needed for resolving make side
  1885. // dependencies.
  1886. c.HideFromMake()
  1887. } else if !installable(c, apexInfo) {
  1888. c.SkipInstall()
  1889. }
  1890. // Still call c.installer.install though, the installs will be stored as PackageSpecs
  1891. // to allow using the outputs in a genrule.
  1892. if c.installer != nil && c.outputFile.Valid() {
  1893. c.installer.install(ctx, c.outputFile.Path())
  1894. if ctx.Failed() {
  1895. return
  1896. }
  1897. }
  1898. }
  1899. func (c *Module) setAndroidMkVariablesFromCquery(info cquery.CcAndroidMkInfo) {
  1900. c.Properties.AndroidMkSharedLibs = info.LocalSharedLibs
  1901. c.Properties.AndroidMkStaticLibs = info.LocalStaticLibs
  1902. c.Properties.AndroidMkWholeStaticLibs = info.LocalWholeStaticLibs
  1903. }
  1904. func (c *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
  1905. if c.cachedToolchain == nil {
  1906. c.cachedToolchain = config.FindToolchainWithContext(ctx)
  1907. }
  1908. return c.cachedToolchain
  1909. }
  1910. func (c *Module) begin(ctx BaseModuleContext) {
  1911. if c.compiler != nil {
  1912. c.compiler.compilerInit(ctx)
  1913. }
  1914. if c.linker != nil {
  1915. c.linker.linkerInit(ctx)
  1916. }
  1917. if c.stl != nil {
  1918. c.stl.begin(ctx)
  1919. }
  1920. if c.sanitize != nil {
  1921. c.sanitize.begin(ctx)
  1922. }
  1923. if c.coverage != nil {
  1924. c.coverage.begin(ctx)
  1925. }
  1926. if c.lto != nil {
  1927. c.lto.begin(ctx)
  1928. }
  1929. if c.pgo != nil {
  1930. c.pgo.begin(ctx)
  1931. }
  1932. if ctx.useSdk() && c.IsSdkVariant() {
  1933. version, err := nativeApiLevelFromUser(ctx, ctx.sdkVersion())
  1934. if err != nil {
  1935. ctx.PropertyErrorf("sdk_version", err.Error())
  1936. c.Properties.Sdk_version = nil
  1937. } else {
  1938. c.Properties.Sdk_version = StringPtr(version.String())
  1939. }
  1940. }
  1941. }
  1942. func (c *Module) deps(ctx DepsContext) Deps {
  1943. deps := Deps{}
  1944. if c.compiler != nil {
  1945. deps = c.compiler.compilerDeps(ctx, deps)
  1946. }
  1947. if c.linker != nil {
  1948. deps = c.linker.linkerDeps(ctx, deps)
  1949. }
  1950. if c.stl != nil {
  1951. deps = c.stl.deps(ctx, deps)
  1952. }
  1953. if c.coverage != nil {
  1954. deps = c.coverage.deps(ctx, deps)
  1955. }
  1956. deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
  1957. deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
  1958. deps.LateStaticLibs = android.LastUniqueStrings(deps.LateStaticLibs)
  1959. deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
  1960. deps.LateSharedLibs = android.LastUniqueStrings(deps.LateSharedLibs)
  1961. deps.HeaderLibs = android.LastUniqueStrings(deps.HeaderLibs)
  1962. deps.RuntimeLibs = android.LastUniqueStrings(deps.RuntimeLibs)
  1963. for _, lib := range deps.ReexportSharedLibHeaders {
  1964. if !inList(lib, deps.SharedLibs) {
  1965. ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
  1966. }
  1967. }
  1968. for _, lib := range deps.ReexportStaticLibHeaders {
  1969. if !inList(lib, deps.StaticLibs) && !inList(lib, deps.WholeStaticLibs) {
  1970. ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs or whole_static_libs: '%s'", lib)
  1971. }
  1972. }
  1973. for _, lib := range deps.ReexportHeaderLibHeaders {
  1974. if !inList(lib, deps.HeaderLibs) {
  1975. ctx.PropertyErrorf("export_header_lib_headers", "Header library not in header_libs: '%s'", lib)
  1976. }
  1977. }
  1978. for _, gen := range deps.ReexportGeneratedHeaders {
  1979. if !inList(gen, deps.GeneratedHeaders) {
  1980. ctx.PropertyErrorf("export_generated_headers", "Generated header module not in generated_headers: '%s'", gen)
  1981. }
  1982. }
  1983. return deps
  1984. }
  1985. func (c *Module) beginMutator(actx android.BottomUpMutatorContext) {
  1986. ctx := &baseModuleContext{
  1987. BaseModuleContext: actx,
  1988. moduleContextImpl: moduleContextImpl{
  1989. mod: c,
  1990. },
  1991. }
  1992. ctx.ctx = ctx
  1993. if !actx.Host() || !ctx.static() || ctx.staticBinary() {
  1994. c.afdo.addDep(ctx, actx)
  1995. }
  1996. c.begin(ctx)
  1997. }
  1998. // Split name#version into name and version
  1999. func StubsLibNameAndVersion(name string) (string, string) {
  2000. if sharp := strings.LastIndex(name, "#"); sharp != -1 && sharp != len(name)-1 {
  2001. version := name[sharp+1:]
  2002. libname := name[:sharp]
  2003. return libname, version
  2004. }
  2005. return name, ""
  2006. }
  2007. func GetCrtVariations(ctx android.BottomUpMutatorContext,
  2008. m LinkableInterface) []blueprint.Variation {
  2009. if ctx.Os() != android.Android {
  2010. return nil
  2011. }
  2012. if m.UseSdk() {
  2013. // Choose the CRT that best satisfies the min_sdk_version requirement of this module
  2014. minSdkVersion := m.MinSdkVersion()
  2015. if minSdkVersion == "" || minSdkVersion == "apex_inherit" {
  2016. minSdkVersion = m.SdkVersion()
  2017. }
  2018. apiLevel, err := android.ApiLevelFromUser(ctx, minSdkVersion)
  2019. if err != nil {
  2020. ctx.PropertyErrorf("min_sdk_version", err.Error())
  2021. }
  2022. // Raise the minSdkVersion to the minimum supported for the architecture.
  2023. minApiForArch := MinApiForArch(ctx, m.Target().Arch.ArchType)
  2024. if apiLevel.LessThan(minApiForArch) {
  2025. apiLevel = minApiForArch
  2026. }
  2027. return []blueprint.Variation{
  2028. {Mutator: "sdk", Variation: "sdk"},
  2029. {Mutator: "version", Variation: apiLevel.String()},
  2030. }
  2031. }
  2032. return []blueprint.Variation{
  2033. {Mutator: "sdk", Variation: ""},
  2034. }
  2035. }
  2036. func AddSharedLibDependenciesWithVersions(ctx android.BottomUpMutatorContext, mod LinkableInterface,
  2037. variations []blueprint.Variation, depTag blueprint.DependencyTag, name, version string, far bool) {
  2038. variations = append([]blueprint.Variation(nil), variations...)
  2039. if version != "" && canBeOrLinkAgainstVersionVariants(mod) {
  2040. // Version is explicitly specified. i.e. libFoo#30
  2041. variations = append(variations, blueprint.Variation{Mutator: "version", Variation: version})
  2042. if tag, ok := depTag.(libraryDependencyTag); ok {
  2043. tag.explicitlyVersioned = true
  2044. } else {
  2045. panic(fmt.Errorf("Unexpected dependency tag: %T", depTag))
  2046. }
  2047. }
  2048. if far {
  2049. ctx.AddFarVariationDependencies(variations, depTag, name)
  2050. } else {
  2051. ctx.AddVariationDependencies(variations, depTag, name)
  2052. }
  2053. }
  2054. func GetApiImports(c LinkableInterface, actx android.BottomUpMutatorContext) multitree.ApiImportInfo {
  2055. apiImportInfo := multitree.ApiImportInfo{}
  2056. if c.Device() {
  2057. var apiImportModule []blueprint.Module
  2058. if actx.OtherModuleExists("api_imports") {
  2059. apiImportModule = actx.AddDependency(c, nil, "api_imports")
  2060. if len(apiImportModule) > 0 && apiImportModule[0] != nil {
  2061. apiInfo := actx.OtherModuleProvider(apiImportModule[0], multitree.ApiImportsProvider).(multitree.ApiImportInfo)
  2062. apiImportInfo = apiInfo
  2063. actx.SetProvider(multitree.ApiImportsProvider, apiInfo)
  2064. }
  2065. }
  2066. }
  2067. return apiImportInfo
  2068. }
  2069. func GetSnapshot(c LinkableInterface, snapshotInfo **SnapshotInfo, actx android.BottomUpMutatorContext) SnapshotInfo {
  2070. // Only device modules with BOARD_VNDK_VERSION uses snapshot. Others use the zero value of
  2071. // SnapshotInfo, which provides no mappings.
  2072. if *snapshotInfo == nil && c.Device() {
  2073. // Only retrieve the snapshot on demand in order to avoid circular dependencies
  2074. // between the modules in the snapshot and the snapshot itself.
  2075. var snapshotModule []blueprint.Module
  2076. if c.InVendor() && c.VndkVersion() == actx.DeviceConfig().VndkVersion() {
  2077. snapshotModule = actx.AddVariationDependencies(nil, nil, "vendor_snapshot")
  2078. } else if recoverySnapshotVersion := actx.DeviceConfig().RecoverySnapshotVersion(); recoverySnapshotVersion != "current" && recoverySnapshotVersion != "" && c.InRecovery() {
  2079. snapshotModule = actx.AddVariationDependencies(nil, nil, "recovery_snapshot")
  2080. }
  2081. if len(snapshotModule) > 0 && snapshotModule[0] != nil {
  2082. snapshot := actx.OtherModuleProvider(snapshotModule[0], SnapshotInfoProvider).(SnapshotInfo)
  2083. *snapshotInfo = &snapshot
  2084. // republish the snapshot for use in later mutators on this module
  2085. actx.SetProvider(SnapshotInfoProvider, snapshot)
  2086. }
  2087. }
  2088. if *snapshotInfo == nil {
  2089. *snapshotInfo = &SnapshotInfo{}
  2090. }
  2091. return **snapshotInfo
  2092. }
  2093. func GetReplaceModuleName(lib string, replaceMap map[string]string) string {
  2094. if snapshot, ok := replaceMap[lib]; ok {
  2095. return snapshot
  2096. }
  2097. return lib
  2098. }
  2099. // RewriteLibs takes a list of names of shared libraries and scans it for three types
  2100. // of names:
  2101. //
  2102. // 1. Name of an NDK library that refers to a prebuilt module.
  2103. //
  2104. // For each of these, it adds the name of the prebuilt module (which will be in
  2105. // prebuilts/ndk) to the list of nonvariant libs.
  2106. //
  2107. // 2. Name of an NDK library that refers to an ndk_library module.
  2108. //
  2109. // For each of these, it adds the name of the ndk_library module to the list of
  2110. // variant libs.
  2111. //
  2112. // 3. Anything else (so anything that isn't an NDK library).
  2113. //
  2114. // It adds these to the nonvariantLibs list.
  2115. //
  2116. // The caller can then know to add the variantLibs dependencies differently from the
  2117. // nonvariantLibs
  2118. func RewriteLibs(c LinkableInterface, snapshotInfo **SnapshotInfo, actx android.BottomUpMutatorContext, config android.Config, list []string) (nonvariantLibs []string, variantLibs []string) {
  2119. variantLibs = []string{}
  2120. nonvariantLibs = []string{}
  2121. for _, entry := range list {
  2122. // strip #version suffix out
  2123. name, _ := StubsLibNameAndVersion(entry)
  2124. if c.InRecovery() {
  2125. nonvariantLibs = append(nonvariantLibs, GetReplaceModuleName(entry, GetSnapshot(c, snapshotInfo, actx).SharedLibs))
  2126. } else if c.UseSdk() && inList(name, *getNDKKnownLibs(config)) {
  2127. variantLibs = append(variantLibs, name+ndkLibrarySuffix)
  2128. } else if c.UseVndk() {
  2129. nonvariantLibs = append(nonvariantLibs, GetReplaceModuleName(entry, GetSnapshot(c, snapshotInfo, actx).SharedLibs))
  2130. } else {
  2131. // put name#version back
  2132. nonvariantLibs = append(nonvariantLibs, entry)
  2133. }
  2134. }
  2135. return nonvariantLibs, variantLibs
  2136. }
  2137. func rewriteLibsForApiImports(c LinkableInterface, libs []string, replaceList map[string]string, config android.Config) ([]string, []string) {
  2138. nonVariantLibs := []string{}
  2139. variantLibs := []string{}
  2140. for _, lib := range libs {
  2141. replaceLibName := GetReplaceModuleName(lib, replaceList)
  2142. if replaceLibName == lib {
  2143. // Do not handle any libs which are not in API imports
  2144. nonVariantLibs = append(nonVariantLibs, replaceLibName)
  2145. } else if c.UseSdk() && inList(replaceLibName, *getNDKKnownLibs(config)) {
  2146. variantLibs = append(variantLibs, replaceLibName)
  2147. } else {
  2148. nonVariantLibs = append(nonVariantLibs, replaceLibName)
  2149. }
  2150. }
  2151. return nonVariantLibs, variantLibs
  2152. }
  2153. func (c *Module) shouldUseApiSurface() bool {
  2154. if c.Os() == android.Android && c.Target().NativeBridge != android.NativeBridgeEnabled {
  2155. if GetImageVariantType(c) == vendorImageVariant || GetImageVariantType(c) == productImageVariant {
  2156. // LLNDK Variant
  2157. return true
  2158. }
  2159. if c.Properties.IsSdkVariant {
  2160. // NDK Variant
  2161. return true
  2162. }
  2163. if c.isImportedApiLibrary() {
  2164. // API Library should depend on API headers
  2165. return true
  2166. }
  2167. }
  2168. return false
  2169. }
  2170. func (c *Module) DepsMutator(actx android.BottomUpMutatorContext) {
  2171. if !c.Enabled() {
  2172. return
  2173. }
  2174. ctx := &depsContext{
  2175. BottomUpMutatorContext: actx,
  2176. moduleContextImpl: moduleContextImpl{
  2177. mod: c,
  2178. },
  2179. }
  2180. ctx.ctx = ctx
  2181. deps := c.deps(ctx)
  2182. apiImportInfo := GetApiImports(c, actx)
  2183. apiNdkLibs := []string{}
  2184. apiLateNdkLibs := []string{}
  2185. if c.shouldUseApiSurface() {
  2186. deps.SharedLibs, apiNdkLibs = rewriteLibsForApiImports(c, deps.SharedLibs, apiImportInfo.SharedLibs, ctx.Config())
  2187. deps.LateSharedLibs, apiLateNdkLibs = rewriteLibsForApiImports(c, deps.LateSharedLibs, apiImportInfo.SharedLibs, ctx.Config())
  2188. deps.SystemSharedLibs, _ = rewriteLibsForApiImports(c, deps.SystemSharedLibs, apiImportInfo.SharedLibs, ctx.Config())
  2189. deps.ReexportHeaderLibHeaders, _ = rewriteLibsForApiImports(c, deps.ReexportHeaderLibHeaders, apiImportInfo.SharedLibs, ctx.Config())
  2190. deps.ReexportSharedLibHeaders, _ = rewriteLibsForApiImports(c, deps.ReexportSharedLibHeaders, apiImportInfo.SharedLibs, ctx.Config())
  2191. }
  2192. c.Properties.AndroidMkSystemSharedLibs = deps.SystemSharedLibs
  2193. var snapshotInfo *SnapshotInfo
  2194. variantNdkLibs := []string{}
  2195. variantLateNdkLibs := []string{}
  2196. if ctx.Os() == android.Android {
  2197. deps.SharedLibs, variantNdkLibs = RewriteLibs(c, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
  2198. deps.LateSharedLibs, variantLateNdkLibs = RewriteLibs(c, &snapshotInfo, actx, ctx.Config(), deps.LateSharedLibs)
  2199. deps.ReexportSharedLibHeaders, _ = RewriteLibs(c, &snapshotInfo, actx, ctx.Config(), deps.ReexportSharedLibHeaders)
  2200. for idx, lib := range deps.RuntimeLibs {
  2201. deps.RuntimeLibs[idx] = GetReplaceModuleName(lib, GetSnapshot(c, &snapshotInfo, actx).SharedLibs)
  2202. }
  2203. }
  2204. for _, lib := range deps.HeaderLibs {
  2205. depTag := libraryDependencyTag{Kind: headerLibraryDependency}
  2206. if inList(lib, deps.ReexportHeaderLibHeaders) {
  2207. depTag.reexportFlags = true
  2208. }
  2209. // Check header lib replacement from API surface first, and then check again with VSDK
  2210. if c.shouldUseApiSurface() {
  2211. lib = GetReplaceModuleName(lib, apiImportInfo.HeaderLibs)
  2212. }
  2213. lib = GetReplaceModuleName(lib, GetSnapshot(c, &snapshotInfo, actx).HeaderLibs)
  2214. if c.isNDKStubLibrary() {
  2215. // ndk_headers do not have any variations
  2216. actx.AddFarVariationDependencies([]blueprint.Variation{}, depTag, lib)
  2217. } else if c.IsStubs() && !c.isImportedApiLibrary() {
  2218. actx.AddFarVariationDependencies(append(ctx.Target().Variations(), c.ImageVariation()),
  2219. depTag, lib)
  2220. } else {
  2221. actx.AddVariationDependencies(nil, depTag, lib)
  2222. }
  2223. }
  2224. if c.isNDKStubLibrary() {
  2225. // NDK stubs depend on their implementation because the ABI dumps are
  2226. // generated from the implementation library.
  2227. actx.AddFarVariationDependencies(append(ctx.Target().Variations(),
  2228. c.ImageVariation(),
  2229. blueprint.Variation{Mutator: "link", Variation: "shared"},
  2230. ), stubImplementation, c.BaseModuleName())
  2231. }
  2232. for _, lib := range deps.WholeStaticLibs {
  2233. depTag := libraryDependencyTag{Kind: staticLibraryDependency, wholeStatic: true, reexportFlags: true}
  2234. lib = GetReplaceModuleName(lib, GetSnapshot(c, &snapshotInfo, actx).StaticLibs)
  2235. actx.AddVariationDependencies([]blueprint.Variation{
  2236. {Mutator: "link", Variation: "static"},
  2237. }, depTag, lib)
  2238. }
  2239. for _, lib := range deps.StaticLibs {
  2240. depTag := libraryDependencyTag{Kind: staticLibraryDependency}
  2241. if inList(lib, deps.ReexportStaticLibHeaders) {
  2242. depTag.reexportFlags = true
  2243. }
  2244. if inList(lib, deps.ExcludeLibsForApex) {
  2245. depTag.excludeInApex = true
  2246. }
  2247. lib = GetReplaceModuleName(lib, GetSnapshot(c, &snapshotInfo, actx).StaticLibs)
  2248. actx.AddVariationDependencies([]blueprint.Variation{
  2249. {Mutator: "link", Variation: "static"},
  2250. }, depTag, lib)
  2251. }
  2252. // staticUnwinderDep is treated as staticDep for Q apexes
  2253. // so that native libraries/binaries are linked with static unwinder
  2254. // because Q libc doesn't have unwinder APIs
  2255. if deps.StaticUnwinderIfLegacy {
  2256. depTag := libraryDependencyTag{Kind: staticLibraryDependency, staticUnwinder: true}
  2257. actx.AddVariationDependencies([]blueprint.Variation{
  2258. {Mutator: "link", Variation: "static"},
  2259. }, depTag, GetReplaceModuleName(staticUnwinder(actx), GetSnapshot(c, &snapshotInfo, actx).StaticLibs))
  2260. }
  2261. // shared lib names without the #version suffix
  2262. var sharedLibNames []string
  2263. for _, lib := range deps.SharedLibs {
  2264. depTag := libraryDependencyTag{Kind: sharedLibraryDependency}
  2265. if inList(lib, deps.ReexportSharedLibHeaders) {
  2266. depTag.reexportFlags = true
  2267. }
  2268. if inList(lib, deps.ExcludeLibsForApex) {
  2269. depTag.excludeInApex = true
  2270. }
  2271. name, version := StubsLibNameAndVersion(lib)
  2272. if apiLibraryName, ok := apiImportInfo.SharedLibs[name]; ok && !ctx.OtherModuleExists(name) {
  2273. name = apiLibraryName
  2274. }
  2275. sharedLibNames = append(sharedLibNames, name)
  2276. variations := []blueprint.Variation{
  2277. {Mutator: "link", Variation: "shared"},
  2278. }
  2279. if _, ok := apiImportInfo.ApexSharedLibs[name]; !ok || ctx.OtherModuleExists(name) {
  2280. AddSharedLibDependenciesWithVersions(ctx, c, variations, depTag, name, version, false)
  2281. }
  2282. if apiLibraryName, ok := apiImportInfo.ApexSharedLibs[name]; ok {
  2283. AddSharedLibDependenciesWithVersions(ctx, c, variations, depTag, apiLibraryName, version, false)
  2284. }
  2285. }
  2286. for _, lib := range deps.LateStaticLibs {
  2287. depTag := libraryDependencyTag{Kind: staticLibraryDependency, Order: lateLibraryDependency}
  2288. actx.AddVariationDependencies([]blueprint.Variation{
  2289. {Mutator: "link", Variation: "static"},
  2290. }, depTag, GetReplaceModuleName(lib, GetSnapshot(c, &snapshotInfo, actx).StaticLibs))
  2291. }
  2292. for _, lib := range deps.UnexportedStaticLibs {
  2293. depTag := libraryDependencyTag{Kind: staticLibraryDependency, Order: lateLibraryDependency, unexportedSymbols: true}
  2294. actx.AddVariationDependencies([]blueprint.Variation{
  2295. {Mutator: "link", Variation: "static"},
  2296. }, depTag, GetReplaceModuleName(lib, GetSnapshot(c, &snapshotInfo, actx).StaticLibs))
  2297. }
  2298. for _, lib := range deps.LateSharedLibs {
  2299. if inList(lib, sharedLibNames) {
  2300. // This is to handle the case that some of the late shared libs (libc, libdl, libm, ...)
  2301. // are added also to SharedLibs with version (e.g., libc#10). If not skipped, we will be
  2302. // linking against both the stubs lib and the non-stubs lib at the same time.
  2303. continue
  2304. }
  2305. depTag := libraryDependencyTag{Kind: sharedLibraryDependency, Order: lateLibraryDependency}
  2306. variations := []blueprint.Variation{
  2307. {Mutator: "link", Variation: "shared"},
  2308. }
  2309. AddSharedLibDependenciesWithVersions(ctx, c, variations, depTag, lib, "", false)
  2310. }
  2311. actx.AddVariationDependencies([]blueprint.Variation{
  2312. {Mutator: "link", Variation: "shared"},
  2313. }, dataLibDepTag, deps.DataLibs...)
  2314. actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
  2315. actx.AddVariationDependencies([]blueprint.Variation{
  2316. {Mutator: "link", Variation: "shared"},
  2317. }, runtimeDepTag, deps.RuntimeLibs...)
  2318. actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
  2319. for _, gen := range deps.GeneratedHeaders {
  2320. depTag := genHeaderDepTag
  2321. if inList(gen, deps.ReexportGeneratedHeaders) {
  2322. depTag = genHeaderExportDepTag
  2323. }
  2324. actx.AddDependency(c, depTag, gen)
  2325. }
  2326. crtVariations := GetCrtVariations(ctx, c)
  2327. actx.AddVariationDependencies(crtVariations, objDepTag, deps.ObjFiles...)
  2328. for _, crt := range deps.CrtBegin {
  2329. actx.AddVariationDependencies(crtVariations, CrtBeginDepTag,
  2330. GetReplaceModuleName(crt, GetSnapshot(c, &snapshotInfo, actx).Objects))
  2331. }
  2332. for _, crt := range deps.CrtEnd {
  2333. actx.AddVariationDependencies(crtVariations, CrtEndDepTag,
  2334. GetReplaceModuleName(crt, GetSnapshot(c, &snapshotInfo, actx).Objects))
  2335. }
  2336. if deps.DynamicLinker != "" {
  2337. actx.AddDependency(c, dynamicLinkerDepTag, deps.DynamicLinker)
  2338. }
  2339. version := ctx.sdkVersion()
  2340. ndkStubDepTag := libraryDependencyTag{Kind: sharedLibraryDependency, ndk: true, makeSuffix: "." + version}
  2341. actx.AddVariationDependencies([]blueprint.Variation{
  2342. {Mutator: "version", Variation: version},
  2343. {Mutator: "link", Variation: "shared"},
  2344. }, ndkStubDepTag, variantNdkLibs...)
  2345. actx.AddVariationDependencies([]blueprint.Variation{
  2346. {Mutator: "version", Variation: version},
  2347. {Mutator: "link", Variation: "shared"},
  2348. }, ndkStubDepTag, apiNdkLibs...)
  2349. ndkLateStubDepTag := libraryDependencyTag{Kind: sharedLibraryDependency, Order: lateLibraryDependency, ndk: true, makeSuffix: "." + version}
  2350. actx.AddVariationDependencies([]blueprint.Variation{
  2351. {Mutator: "version", Variation: version},
  2352. {Mutator: "link", Variation: "shared"},
  2353. }, ndkLateStubDepTag, variantLateNdkLibs...)
  2354. actx.AddVariationDependencies([]blueprint.Variation{
  2355. {Mutator: "version", Variation: version},
  2356. {Mutator: "link", Variation: "shared"},
  2357. }, ndkLateStubDepTag, apiLateNdkLibs...)
  2358. if vndkdep := c.vndkdep; vndkdep != nil {
  2359. if vndkdep.isVndkExt() {
  2360. actx.AddVariationDependencies([]blueprint.Variation{
  2361. c.ImageVariation(),
  2362. {Mutator: "link", Variation: "shared"},
  2363. }, vndkExtDepTag, GetReplaceModuleName(vndkdep.getVndkExtendsModuleName(), GetSnapshot(c, &snapshotInfo, actx).SharedLibs))
  2364. }
  2365. }
  2366. if len(deps.AidlLibs) > 0 {
  2367. actx.AddDependency(
  2368. c,
  2369. aidlLibraryTag,
  2370. deps.AidlLibs...,
  2371. )
  2372. }
  2373. updateImportedLibraryDependency(ctx)
  2374. }
  2375. func BeginMutator(ctx android.BottomUpMutatorContext) {
  2376. if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
  2377. c.beginMutator(ctx)
  2378. }
  2379. }
  2380. // Whether a module can link to another module, taking into
  2381. // account NDK linking.
  2382. func checkLinkType(ctx android.BaseModuleContext, from LinkableInterface, to LinkableInterface,
  2383. tag blueprint.DependencyTag) {
  2384. switch t := tag.(type) {
  2385. case dependencyTag:
  2386. if t != vndkExtDepTag {
  2387. return
  2388. }
  2389. case libraryDependencyTag:
  2390. default:
  2391. return
  2392. }
  2393. if from.Target().Os != android.Android {
  2394. // Host code is not restricted
  2395. return
  2396. }
  2397. // VNDK is cc.Module supported only for now.
  2398. if ccFrom, ok := from.(*Module); ok && from.UseVndk() {
  2399. // Though allowed dependency is limited by the image mutator,
  2400. // each vendor and product module needs to check link-type
  2401. // for VNDK.
  2402. if ccTo, ok := to.(*Module); ok {
  2403. if ccFrom.vndkdep != nil {
  2404. ccFrom.vndkdep.vndkCheckLinkType(ctx, ccTo, tag)
  2405. }
  2406. } else if _, ok := to.(LinkableInterface); !ok {
  2407. ctx.ModuleErrorf("Attempting to link VNDK cc.Module with unsupported module type")
  2408. }
  2409. return
  2410. }
  2411. // TODO(b/244244438) : Remove this once all variants are implemented
  2412. if ccFrom, ok := from.(*Module); ok && ccFrom.isImportedApiLibrary() {
  2413. return
  2414. }
  2415. if from.SdkVersion() == "" {
  2416. // Platform code can link to anything
  2417. return
  2418. }
  2419. if from.InRamdisk() {
  2420. // Ramdisk code is not NDK
  2421. return
  2422. }
  2423. if from.InVendorRamdisk() {
  2424. // Vendor ramdisk code is not NDK
  2425. return
  2426. }
  2427. if from.InRecovery() {
  2428. // Recovery code is not NDK
  2429. return
  2430. }
  2431. if c, ok := to.(*Module); ok {
  2432. if c.NdkPrebuiltStl() {
  2433. // These are allowed, but they don't set sdk_version
  2434. return
  2435. }
  2436. if c.StubDecorator() {
  2437. // These aren't real libraries, but are the stub shared libraries that are included in
  2438. // the NDK.
  2439. return
  2440. }
  2441. if c.isImportedApiLibrary() {
  2442. // Imported library from the API surface is a stub library built against interface definition.
  2443. return
  2444. }
  2445. }
  2446. if strings.HasPrefix(ctx.ModuleName(), "libclang_rt.") && to.Module().Name() == "libc++" {
  2447. // Bug: http://b/121358700 - Allow libclang_rt.* shared libraries (with sdk_version)
  2448. // to link to libc++ (non-NDK and without sdk_version).
  2449. return
  2450. }
  2451. if to.SdkVersion() == "" {
  2452. // NDK code linking to platform code is never okay.
  2453. ctx.ModuleErrorf("depends on non-NDK-built library %q",
  2454. ctx.OtherModuleName(to.Module()))
  2455. return
  2456. }
  2457. // At this point we know we have two NDK libraries, but we need to
  2458. // check that we're not linking against anything built against a higher
  2459. // API level, as it is only valid to link against older or equivalent
  2460. // APIs.
  2461. // Current can link against anything.
  2462. if from.SdkVersion() != "current" {
  2463. // Otherwise we need to check.
  2464. if to.SdkVersion() == "current" {
  2465. // Current can't be linked against by anything else.
  2466. ctx.ModuleErrorf("links %q built against newer API version %q",
  2467. ctx.OtherModuleName(to.Module()), "current")
  2468. } else {
  2469. fromApi, err := strconv.Atoi(from.SdkVersion())
  2470. if err != nil {
  2471. ctx.PropertyErrorf("sdk_version",
  2472. "Invalid sdk_version value (must be int or current): %q",
  2473. from.SdkVersion())
  2474. }
  2475. toApi, err := strconv.Atoi(to.SdkVersion())
  2476. if err != nil {
  2477. ctx.PropertyErrorf("sdk_version",
  2478. "Invalid sdk_version value (must be int or current): %q",
  2479. to.SdkVersion())
  2480. }
  2481. if toApi > fromApi {
  2482. ctx.ModuleErrorf("links %q built against newer API version %q",
  2483. ctx.OtherModuleName(to.Module()), to.SdkVersion())
  2484. }
  2485. }
  2486. }
  2487. // Also check that the two STL choices are compatible.
  2488. fromStl := from.SelectedStl()
  2489. toStl := to.SelectedStl()
  2490. if fromStl == "" || toStl == "" {
  2491. // Libraries that don't use the STL are unrestricted.
  2492. } else if fromStl == "ndk_system" || toStl == "ndk_system" {
  2493. // We can be permissive with the system "STL" since it is only the C++
  2494. // ABI layer, but in the future we should make sure that everyone is
  2495. // using either libc++ or nothing.
  2496. } else if getNdkStlFamily(from) != getNdkStlFamily(to) {
  2497. ctx.ModuleErrorf("uses %q and depends on %q which uses incompatible %q",
  2498. from.SelectedStl(), ctx.OtherModuleName(to.Module()),
  2499. to.SelectedStl())
  2500. }
  2501. }
  2502. func checkLinkTypeMutator(ctx android.BottomUpMutatorContext) {
  2503. if c, ok := ctx.Module().(*Module); ok {
  2504. ctx.VisitDirectDeps(func(dep android.Module) {
  2505. depTag := ctx.OtherModuleDependencyTag(dep)
  2506. ccDep, ok := dep.(LinkableInterface)
  2507. if ok {
  2508. checkLinkType(ctx, c, ccDep, depTag)
  2509. }
  2510. })
  2511. }
  2512. }
  2513. // Tests whether the dependent library is okay to be double loaded inside a single process.
  2514. // If a library has a vendor variant and is a (transitive) dependency of an LLNDK library,
  2515. // it is subject to be double loaded. Such lib should be explicitly marked as double_loadable: true
  2516. // or as vndk-sp (vndk: { enabled: true, support_system_process: true}).
  2517. func checkDoubleLoadableLibraries(ctx android.TopDownMutatorContext) {
  2518. check := func(child, parent android.Module) bool {
  2519. to, ok := child.(*Module)
  2520. if !ok {
  2521. return false
  2522. }
  2523. if lib, ok := to.linker.(*libraryDecorator); !ok || !lib.shared() {
  2524. return false
  2525. }
  2526. // These dependencies are not excercised at runtime. Tracking these will give us
  2527. // false negative, so skip.
  2528. depTag := ctx.OtherModuleDependencyTag(child)
  2529. if IsHeaderDepTag(depTag) {
  2530. return false
  2531. }
  2532. if depTag == staticVariantTag {
  2533. return false
  2534. }
  2535. if depTag == stubImplDepTag {
  2536. return false
  2537. }
  2538. // Even if target lib has no vendor variant, keep checking dependency
  2539. // graph in case it depends on vendor_available or product_available
  2540. // but not double_loadable transtively.
  2541. if !to.HasNonSystemVariants() {
  2542. return true
  2543. }
  2544. // The happy path. Keep tracking dependencies until we hit a non double-loadable
  2545. // one.
  2546. if Bool(to.VendorProperties.Double_loadable) {
  2547. return true
  2548. }
  2549. if to.IsVndkSp() || to.IsLlndk() {
  2550. return false
  2551. }
  2552. ctx.ModuleErrorf("links a library %q which is not LL-NDK, "+
  2553. "VNDK-SP, or explicitly marked as 'double_loadable:true'. "+
  2554. "Dependency list: %s", ctx.OtherModuleName(to), ctx.GetPathString(false))
  2555. return false
  2556. }
  2557. if module, ok := ctx.Module().(*Module); ok {
  2558. if lib, ok := module.linker.(*libraryDecorator); ok && lib.shared() {
  2559. if lib.hasLLNDKStubs() {
  2560. ctx.WalkDeps(check)
  2561. }
  2562. }
  2563. }
  2564. }
  2565. func findApexSdkVersion(ctx android.BaseModuleContext, apexInfo android.ApexInfo) android.ApiLevel {
  2566. // For the dependency from platform to apex, use the latest stubs
  2567. apexSdkVersion := android.FutureApiLevel
  2568. if !apexInfo.IsForPlatform() {
  2569. apexSdkVersion = apexInfo.MinSdkVersion
  2570. }
  2571. if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
  2572. // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
  2573. // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
  2574. // (b/144430859)
  2575. apexSdkVersion = android.FutureApiLevel
  2576. }
  2577. return apexSdkVersion
  2578. }
  2579. // Convert dependencies to paths. Returns a PathDeps containing paths
  2580. func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
  2581. var depPaths PathDeps
  2582. var directStaticDeps []StaticLibraryInfo
  2583. var directSharedDeps []SharedLibraryInfo
  2584. reexportExporter := func(exporter FlagExporterInfo) {
  2585. depPaths.ReexportedDirs = append(depPaths.ReexportedDirs, exporter.IncludeDirs...)
  2586. depPaths.ReexportedSystemDirs = append(depPaths.ReexportedSystemDirs, exporter.SystemIncludeDirs...)
  2587. depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, exporter.Flags...)
  2588. depPaths.ReexportedDeps = append(depPaths.ReexportedDeps, exporter.Deps...)
  2589. depPaths.ReexportedGeneratedHeaders = append(depPaths.ReexportedGeneratedHeaders, exporter.GeneratedHeaders...)
  2590. }
  2591. apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
  2592. c.apexSdkVersion = findApexSdkVersion(ctx, apexInfo)
  2593. skipModuleList := map[string]bool{}
  2594. var apiImportInfo multitree.ApiImportInfo
  2595. hasApiImportInfo := false
  2596. ctx.VisitDirectDeps(func(dep android.Module) {
  2597. if dep.Name() == "api_imports" {
  2598. apiImportInfo = ctx.OtherModuleProvider(dep, multitree.ApiImportsProvider).(multitree.ApiImportInfo)
  2599. hasApiImportInfo = true
  2600. }
  2601. })
  2602. if hasApiImportInfo {
  2603. targetStubModuleList := map[string]string{}
  2604. targetOrigModuleList := map[string]string{}
  2605. // Search for dependency which both original module and API imported library with APEX stub exists
  2606. ctx.VisitDirectDeps(func(dep android.Module) {
  2607. depName := ctx.OtherModuleName(dep)
  2608. if apiLibrary, ok := apiImportInfo.ApexSharedLibs[depName]; ok {
  2609. targetStubModuleList[apiLibrary] = depName
  2610. }
  2611. })
  2612. ctx.VisitDirectDeps(func(dep android.Module) {
  2613. depName := ctx.OtherModuleName(dep)
  2614. if origLibrary, ok := targetStubModuleList[depName]; ok {
  2615. targetOrigModuleList[origLibrary] = depName
  2616. }
  2617. })
  2618. // Decide which library should be used between original and API imported library
  2619. ctx.VisitDirectDeps(func(dep android.Module) {
  2620. depName := ctx.OtherModuleName(dep)
  2621. if apiLibrary, ok := targetOrigModuleList[depName]; ok {
  2622. if ShouldUseStubForApex(ctx, dep) {
  2623. skipModuleList[depName] = true
  2624. } else {
  2625. skipModuleList[apiLibrary] = true
  2626. }
  2627. }
  2628. })
  2629. }
  2630. ctx.VisitDirectDeps(func(dep android.Module) {
  2631. depName := ctx.OtherModuleName(dep)
  2632. depTag := ctx.OtherModuleDependencyTag(dep)
  2633. if _, ok := skipModuleList[depName]; ok {
  2634. // skip this module because original module or API imported module matching with this should be used instead.
  2635. return
  2636. }
  2637. if depTag == android.DarwinUniversalVariantTag {
  2638. depPaths.DarwinSecondArchOutput = dep.(*Module).OutputFile()
  2639. return
  2640. }
  2641. if depTag == aidlLibraryTag {
  2642. if ctx.OtherModuleHasProvider(dep, aidl_library.AidlLibraryProvider) {
  2643. depPaths.AidlLibraryInfos = append(
  2644. depPaths.AidlLibraryInfos,
  2645. ctx.OtherModuleProvider(
  2646. dep,
  2647. aidl_library.AidlLibraryProvider).(aidl_library.AidlLibraryInfo),
  2648. )
  2649. }
  2650. }
  2651. ccDep, ok := dep.(LinkableInterface)
  2652. if !ok {
  2653. // handling for a few module types that aren't cc Module but that are also supported
  2654. switch depTag {
  2655. case genSourceDepTag:
  2656. if genRule, ok := dep.(genrule.SourceFileGenerator); ok {
  2657. depPaths.GeneratedSources = append(depPaths.GeneratedSources,
  2658. genRule.GeneratedSourceFiles()...)
  2659. } else {
  2660. ctx.ModuleErrorf("module %q is not a gensrcs or genrule", depName)
  2661. }
  2662. // Support exported headers from a generated_sources dependency
  2663. fallthrough
  2664. case genHeaderDepTag, genHeaderExportDepTag:
  2665. if genRule, ok := dep.(genrule.SourceFileGenerator); ok {
  2666. depPaths.GeneratedDeps = append(depPaths.GeneratedDeps,
  2667. genRule.GeneratedDeps()...)
  2668. dirs := genRule.GeneratedHeaderDirs()
  2669. depPaths.IncludeDirs = append(depPaths.IncludeDirs, dirs...)
  2670. if depTag == genHeaderExportDepTag {
  2671. depPaths.ReexportedDirs = append(depPaths.ReexportedDirs, dirs...)
  2672. depPaths.ReexportedGeneratedHeaders = append(depPaths.ReexportedGeneratedHeaders,
  2673. genRule.GeneratedSourceFiles()...)
  2674. depPaths.ReexportedDeps = append(depPaths.ReexportedDeps, genRule.GeneratedDeps()...)
  2675. // Add these re-exported flags to help header-abi-dumper to infer the abi exported by a library.
  2676. c.sabi.Properties.ReexportedIncludes = append(c.sabi.Properties.ReexportedIncludes, dirs.Strings()...)
  2677. }
  2678. } else {
  2679. ctx.ModuleErrorf("module %q is not a genrule", depName)
  2680. }
  2681. case CrtBeginDepTag:
  2682. depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
  2683. case CrtEndDepTag:
  2684. depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
  2685. }
  2686. return
  2687. }
  2688. if depTag == android.ProtoPluginDepTag {
  2689. return
  2690. }
  2691. if dep.Target().Os != ctx.Os() {
  2692. ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
  2693. return
  2694. }
  2695. if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
  2696. ctx.ModuleErrorf("Arch mismatch between %q(%v) and %q(%v)",
  2697. ctx.ModuleName(), ctx.Arch().ArchType, depName, dep.Target().Arch.ArchType)
  2698. return
  2699. }
  2700. if depTag == reuseObjTag {
  2701. // Skip reused objects for stub libraries, they use their own stub object file instead.
  2702. // The reuseObjTag dependency still exists because the LinkageMutator runs before the
  2703. // version mutator, so the stubs variant is created from the shared variant that
  2704. // already has the reuseObjTag dependency on the static variant.
  2705. if !c.library.buildStubs() {
  2706. staticAnalogue := ctx.OtherModuleProvider(dep, StaticLibraryInfoProvider).(StaticLibraryInfo)
  2707. objs := staticAnalogue.ReuseObjects
  2708. depPaths.Objs = depPaths.Objs.Append(objs)
  2709. depExporterInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
  2710. reexportExporter(depExporterInfo)
  2711. }
  2712. return
  2713. }
  2714. linkFile := ccDep.OutputFile()
  2715. if libDepTag, ok := depTag.(libraryDependencyTag); ok {
  2716. // Only use static unwinder for legacy (min_sdk_version = 29) apexes (b/144430859)
  2717. if libDepTag.staticUnwinder && c.apexSdkVersion.GreaterThan(android.SdkVersion_Android10) {
  2718. return
  2719. }
  2720. if !apexInfo.IsForPlatform() && libDepTag.excludeInApex {
  2721. return
  2722. }
  2723. depExporterInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
  2724. var ptr *android.Paths
  2725. var depPtr *android.Paths
  2726. depFile := android.OptionalPath{}
  2727. switch {
  2728. case libDepTag.header():
  2729. if !ctx.OtherModuleHasProvider(dep, HeaderLibraryInfoProvider) {
  2730. if !ctx.Config().AllowMissingDependencies() {
  2731. ctx.ModuleErrorf("module %q is not a header library", depName)
  2732. } else {
  2733. ctx.AddMissingDependencies([]string{depName})
  2734. }
  2735. return
  2736. }
  2737. case libDepTag.shared():
  2738. if !ctx.OtherModuleHasProvider(dep, SharedLibraryInfoProvider) {
  2739. if !ctx.Config().AllowMissingDependencies() {
  2740. ctx.ModuleErrorf("module %q is not a shared library", depName)
  2741. } else {
  2742. ctx.AddMissingDependencies([]string{depName})
  2743. }
  2744. return
  2745. }
  2746. sharedLibraryInfo, returnedDepExporterInfo := ChooseStubOrImpl(ctx, dep)
  2747. depExporterInfo = returnedDepExporterInfo
  2748. // Stubs lib doesn't link to the shared lib dependencies. Don't set
  2749. // linkFile, depFile, and ptr.
  2750. if c.IsStubs() {
  2751. break
  2752. }
  2753. linkFile = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
  2754. depFile = sharedLibraryInfo.TableOfContents
  2755. ptr = &depPaths.SharedLibs
  2756. switch libDepTag.Order {
  2757. case earlyLibraryDependency:
  2758. ptr = &depPaths.EarlySharedLibs
  2759. depPtr = &depPaths.EarlySharedLibsDeps
  2760. case normalLibraryDependency:
  2761. ptr = &depPaths.SharedLibs
  2762. depPtr = &depPaths.SharedLibsDeps
  2763. directSharedDeps = append(directSharedDeps, sharedLibraryInfo)
  2764. case lateLibraryDependency:
  2765. ptr = &depPaths.LateSharedLibs
  2766. depPtr = &depPaths.LateSharedLibsDeps
  2767. default:
  2768. panic(fmt.Errorf("unexpected library dependency order %d", libDepTag.Order))
  2769. }
  2770. case libDepTag.static():
  2771. if !ctx.OtherModuleHasProvider(dep, StaticLibraryInfoProvider) {
  2772. if !ctx.Config().AllowMissingDependencies() {
  2773. ctx.ModuleErrorf("module %q is not a static library", depName)
  2774. } else {
  2775. ctx.AddMissingDependencies([]string{depName})
  2776. }
  2777. return
  2778. }
  2779. // Stubs lib doesn't link to the static lib dependencies. Don't set
  2780. // linkFile, depFile, and ptr.
  2781. if c.IsStubs() {
  2782. break
  2783. }
  2784. staticLibraryInfo := ctx.OtherModuleProvider(dep, StaticLibraryInfoProvider).(StaticLibraryInfo)
  2785. linkFile = android.OptionalPathForPath(staticLibraryInfo.StaticLibrary)
  2786. if libDepTag.wholeStatic {
  2787. ptr = &depPaths.WholeStaticLibs
  2788. if len(staticLibraryInfo.Objects.objFiles) > 0 {
  2789. depPaths.WholeStaticLibObjs = depPaths.WholeStaticLibObjs.Append(staticLibraryInfo.Objects)
  2790. } else {
  2791. // This case normally catches prebuilt static
  2792. // libraries, but it can also occur when
  2793. // AllowMissingDependencies is on and the
  2794. // dependencies has no sources of its own
  2795. // but has a whole_static_libs dependency
  2796. // on a missing library. We want to depend
  2797. // on the .a file so that there is something
  2798. // in the dependency tree that contains the
  2799. // error rule for the missing transitive
  2800. // dependency.
  2801. depPaths.WholeStaticLibsFromPrebuilts = append(depPaths.WholeStaticLibsFromPrebuilts, linkFile.Path())
  2802. }
  2803. depPaths.WholeStaticLibsFromPrebuilts = append(depPaths.WholeStaticLibsFromPrebuilts,
  2804. staticLibraryInfo.WholeStaticLibsFromPrebuilts...)
  2805. } else {
  2806. switch libDepTag.Order {
  2807. case earlyLibraryDependency:
  2808. panic(fmt.Errorf("early static libs not suppported"))
  2809. case normalLibraryDependency:
  2810. // static dependencies will be handled separately so they can be ordered
  2811. // using transitive dependencies.
  2812. ptr = nil
  2813. directStaticDeps = append(directStaticDeps, staticLibraryInfo)
  2814. case lateLibraryDependency:
  2815. ptr = &depPaths.LateStaticLibs
  2816. default:
  2817. panic(fmt.Errorf("unexpected library dependency order %d", libDepTag.Order))
  2818. }
  2819. }
  2820. if libDepTag.unexportedSymbols {
  2821. depPaths.LdFlags = append(depPaths.LdFlags,
  2822. "-Wl,--exclude-libs="+staticLibraryInfo.StaticLibrary.Base())
  2823. }
  2824. }
  2825. if libDepTag.static() && !libDepTag.wholeStatic {
  2826. if !ccDep.CcLibraryInterface() || !ccDep.Static() {
  2827. ctx.ModuleErrorf("module %q not a static library", depName)
  2828. return
  2829. }
  2830. // When combining coverage files for shared libraries and executables, coverage files
  2831. // in static libraries act as if they were whole static libraries. The same goes for
  2832. // source based Abi dump files.
  2833. if c, ok := ccDep.(*Module); ok {
  2834. staticLib := c.linker.(libraryInterface)
  2835. depPaths.StaticLibObjs.coverageFiles = append(depPaths.StaticLibObjs.coverageFiles,
  2836. staticLib.objs().coverageFiles...)
  2837. depPaths.StaticLibObjs.sAbiDumpFiles = append(depPaths.StaticLibObjs.sAbiDumpFiles,
  2838. staticLib.objs().sAbiDumpFiles...)
  2839. } else {
  2840. // Handle non-CC modules here
  2841. depPaths.StaticLibObjs.coverageFiles = append(depPaths.StaticLibObjs.coverageFiles,
  2842. ccDep.CoverageFiles()...)
  2843. }
  2844. }
  2845. if ptr != nil {
  2846. if !linkFile.Valid() {
  2847. if !ctx.Config().AllowMissingDependencies() {
  2848. ctx.ModuleErrorf("module %q missing output file", depName)
  2849. } else {
  2850. ctx.AddMissingDependencies([]string{depName})
  2851. }
  2852. return
  2853. }
  2854. *ptr = append(*ptr, linkFile.Path())
  2855. }
  2856. if depPtr != nil {
  2857. dep := depFile
  2858. if !dep.Valid() {
  2859. dep = linkFile
  2860. }
  2861. *depPtr = append(*depPtr, dep.Path())
  2862. }
  2863. depPaths.IncludeDirs = append(depPaths.IncludeDirs, depExporterInfo.IncludeDirs...)
  2864. depPaths.SystemIncludeDirs = append(depPaths.SystemIncludeDirs, depExporterInfo.SystemIncludeDirs...)
  2865. depPaths.GeneratedDeps = append(depPaths.GeneratedDeps, depExporterInfo.Deps...)
  2866. depPaths.Flags = append(depPaths.Flags, depExporterInfo.Flags...)
  2867. if libDepTag.reexportFlags {
  2868. reexportExporter(depExporterInfo)
  2869. // Add these re-exported flags to help header-abi-dumper to infer the abi exported by a library.
  2870. // Re-exported shared library headers must be included as well since they can help us with type information
  2871. // about template instantiations (instantiated from their headers).
  2872. // -isystem headers are not included since for bionic libraries, abi-filtering is taken care of by version
  2873. // scripts.
  2874. c.sabi.Properties.ReexportedIncludes = append(
  2875. c.sabi.Properties.ReexportedIncludes, depExporterInfo.IncludeDirs.Strings()...)
  2876. }
  2877. makeLibName := MakeLibName(ctx, c, ccDep, depName) + libDepTag.makeSuffix
  2878. switch {
  2879. case libDepTag.header():
  2880. c.Properties.AndroidMkHeaderLibs = append(
  2881. c.Properties.AndroidMkHeaderLibs, makeLibName)
  2882. case libDepTag.shared():
  2883. if lib := moduleLibraryInterface(dep); lib != nil {
  2884. if lib.buildStubs() && dep.(android.ApexModule).InAnyApex() {
  2885. // Add the dependency to the APEX(es) providing the library so that
  2886. // m <module> can trigger building the APEXes as well.
  2887. depApexInfo := ctx.OtherModuleProvider(dep, android.ApexInfoProvider).(android.ApexInfo)
  2888. for _, an := range depApexInfo.InApexVariants {
  2889. c.Properties.ApexesProvidingSharedLibs = append(
  2890. c.Properties.ApexesProvidingSharedLibs, an)
  2891. }
  2892. }
  2893. }
  2894. // Note: the order of libs in this list is not important because
  2895. // they merely serve as Make dependencies and do not affect this lib itself.
  2896. c.Properties.AndroidMkSharedLibs = append(
  2897. c.Properties.AndroidMkSharedLibs, makeLibName)
  2898. // Record BaseLibName for snapshots.
  2899. c.Properties.SnapshotSharedLibs = append(c.Properties.SnapshotSharedLibs, BaseLibName(depName))
  2900. case libDepTag.static():
  2901. if libDepTag.wholeStatic {
  2902. c.Properties.AndroidMkWholeStaticLibs = append(
  2903. c.Properties.AndroidMkWholeStaticLibs, makeLibName)
  2904. } else {
  2905. c.Properties.AndroidMkStaticLibs = append(
  2906. c.Properties.AndroidMkStaticLibs, makeLibName)
  2907. }
  2908. // Record BaseLibName for snapshots.
  2909. c.Properties.SnapshotStaticLibs = append(c.Properties.SnapshotStaticLibs, BaseLibName(depName))
  2910. }
  2911. } else if !c.IsStubs() {
  2912. // Stubs lib doesn't link to the runtime lib, object, crt, etc. dependencies.
  2913. switch depTag {
  2914. case runtimeDepTag:
  2915. c.Properties.AndroidMkRuntimeLibs = append(
  2916. c.Properties.AndroidMkRuntimeLibs, MakeLibName(ctx, c, ccDep, depName)+libDepTag.makeSuffix)
  2917. // Record BaseLibName for snapshots.
  2918. c.Properties.SnapshotRuntimeLibs = append(c.Properties.SnapshotRuntimeLibs, BaseLibName(depName))
  2919. case objDepTag:
  2920. depPaths.Objs.objFiles = append(depPaths.Objs.objFiles, linkFile.Path())
  2921. case CrtBeginDepTag:
  2922. depPaths.CrtBegin = append(depPaths.CrtBegin, linkFile.Path())
  2923. case CrtEndDepTag:
  2924. depPaths.CrtEnd = append(depPaths.CrtEnd, linkFile.Path())
  2925. case dynamicLinkerDepTag:
  2926. depPaths.DynamicLinker = linkFile
  2927. }
  2928. }
  2929. })
  2930. // use the ordered dependencies as this module's dependencies
  2931. orderedStaticPaths, transitiveStaticLibs := orderStaticModuleDeps(directStaticDeps, directSharedDeps)
  2932. depPaths.TranstiveStaticLibrariesForOrdering = transitiveStaticLibs
  2933. depPaths.StaticLibs = append(depPaths.StaticLibs, orderedStaticPaths...)
  2934. // Dedup exported flags from dependencies
  2935. depPaths.Flags = android.FirstUniqueStrings(depPaths.Flags)
  2936. depPaths.IncludeDirs = android.FirstUniquePaths(depPaths.IncludeDirs)
  2937. depPaths.SystemIncludeDirs = android.FirstUniquePaths(depPaths.SystemIncludeDirs)
  2938. depPaths.GeneratedDeps = android.FirstUniquePaths(depPaths.GeneratedDeps)
  2939. depPaths.ReexportedDirs = android.FirstUniquePaths(depPaths.ReexportedDirs)
  2940. depPaths.ReexportedSystemDirs = android.FirstUniquePaths(depPaths.ReexportedSystemDirs)
  2941. depPaths.ReexportedFlags = android.FirstUniqueStrings(depPaths.ReexportedFlags)
  2942. depPaths.ReexportedDeps = android.FirstUniquePaths(depPaths.ReexportedDeps)
  2943. depPaths.ReexportedGeneratedHeaders = android.FirstUniquePaths(depPaths.ReexportedGeneratedHeaders)
  2944. if c.sabi != nil {
  2945. c.sabi.Properties.ReexportedIncludes = android.FirstUniqueStrings(c.sabi.Properties.ReexportedIncludes)
  2946. }
  2947. return depPaths
  2948. }
  2949. func ShouldUseStubForApex(ctx android.ModuleContext, dep android.Module) bool {
  2950. depName := ctx.OtherModuleName(dep)
  2951. thisModule, ok := ctx.Module().(android.ApexModule)
  2952. if !ok {
  2953. panic(fmt.Errorf("Not an APEX module: %q", ctx.ModuleName()))
  2954. }
  2955. useVndk := false
  2956. bootstrap := false
  2957. if linkable, ok := ctx.Module().(LinkableInterface); !ok {
  2958. panic(fmt.Errorf("Not a Linkable module: %q", ctx.ModuleName()))
  2959. } else {
  2960. useVndk = linkable.UseVndk()
  2961. bootstrap = linkable.Bootstrap()
  2962. }
  2963. apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
  2964. useStubs := false
  2965. if lib := moduleLibraryInterface(dep); lib.buildStubs() && useVndk { // LLNDK
  2966. if !apexInfo.IsForPlatform() {
  2967. // For platform libraries, use current version of LLNDK
  2968. // If this is for use_vendor apex we will apply the same rules
  2969. // of apex sdk enforcement below to choose right version.
  2970. useStubs = true
  2971. }
  2972. } else if apexInfo.IsForPlatform() || apexInfo.UsePlatformApis {
  2973. // If not building for APEX or the containing APEX allows the use of
  2974. // platform APIs, use stubs only when it is from an APEX (and not from
  2975. // platform) However, for host, ramdisk, vendor_ramdisk, recovery or
  2976. // bootstrap modules, always link to non-stub variant
  2977. isNotInPlatform := dep.(android.ApexModule).NotInPlatform()
  2978. isApexImportedApiLibrary := false
  2979. if cc, ok := dep.(*Module); ok {
  2980. if apiLibrary, ok := cc.linker.(*apiLibraryDecorator); ok {
  2981. if apiLibrary.hasApexStubs() {
  2982. isApexImportedApiLibrary = true
  2983. }
  2984. }
  2985. }
  2986. useStubs = (isNotInPlatform || isApexImportedApiLibrary) && !bootstrap
  2987. if useStubs {
  2988. // Another exception: if this module is a test for an APEX, then
  2989. // it is linked with the non-stub variant of a module in the APEX
  2990. // as if this is part of the APEX.
  2991. testFor := ctx.Provider(android.ApexTestForInfoProvider).(android.ApexTestForInfo)
  2992. for _, apexContents := range testFor.ApexContents {
  2993. if apexContents.DirectlyInApex(depName) {
  2994. useStubs = false
  2995. break
  2996. }
  2997. }
  2998. }
  2999. if useStubs {
  3000. // Yet another exception: If this module and the dependency are
  3001. // available to the same APEXes then skip stubs between their
  3002. // platform variants. This complements the test_for case above,
  3003. // which avoids the stubs on a direct APEX library dependency, by
  3004. // avoiding stubs for indirect test dependencies as well.
  3005. //
  3006. // TODO(b/183882457): This doesn't work if the two libraries have
  3007. // only partially overlapping apex_available. For that test_for
  3008. // modules would need to be split into APEX variants and resolved
  3009. // separately for each APEX they have access to.
  3010. if !isApexImportedApiLibrary && android.AvailableToSameApexes(thisModule, dep.(android.ApexModule)) {
  3011. useStubs = false
  3012. }
  3013. }
  3014. } else {
  3015. // If building for APEX, use stubs when the parent is in any APEX that
  3016. // the child is not in.
  3017. useStubs = !android.DirectlyInAllApexes(apexInfo, depName)
  3018. }
  3019. return useStubs
  3020. }
  3021. // ChooseStubOrImpl determines whether a given dependency should be redirected to the stub variant
  3022. // of the dependency or not, and returns the SharedLibraryInfo and FlagExporterInfo for the right
  3023. // dependency. The stub variant is selected when the dependency crosses a boundary where each side
  3024. // has different level of updatability. For example, if a library foo in an APEX depends on a
  3025. // library bar which provides stable interface and exists in the platform, foo uses the stub variant
  3026. // of bar. If bar doesn't provide a stable interface (i.e. buildStubs() == false) or is in the
  3027. // same APEX as foo, the non-stub variant of bar is used.
  3028. func ChooseStubOrImpl(ctx android.ModuleContext, dep android.Module) (SharedLibraryInfo, FlagExporterInfo) {
  3029. depTag := ctx.OtherModuleDependencyTag(dep)
  3030. libDepTag, ok := depTag.(libraryDependencyTag)
  3031. if !ok || !libDepTag.shared() {
  3032. panic(fmt.Errorf("Unexpected dependency tag: %T", depTag))
  3033. }
  3034. sharedLibraryInfo := ctx.OtherModuleProvider(dep, SharedLibraryInfoProvider).(SharedLibraryInfo)
  3035. depExporterInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
  3036. sharedLibraryStubsInfo := ctx.OtherModuleProvider(dep, SharedLibraryStubsProvider).(SharedLibraryStubsInfo)
  3037. if !libDepTag.explicitlyVersioned && len(sharedLibraryStubsInfo.SharedStubLibraries) > 0 {
  3038. // when to use (unspecified) stubs, use the latest one.
  3039. if ShouldUseStubForApex(ctx, dep) {
  3040. stubs := sharedLibraryStubsInfo.SharedStubLibraries
  3041. toUse := stubs[len(stubs)-1]
  3042. sharedLibraryInfo = toUse.SharedLibraryInfo
  3043. depExporterInfo = toUse.FlagExporterInfo
  3044. }
  3045. }
  3046. return sharedLibraryInfo, depExporterInfo
  3047. }
  3048. // orderStaticModuleDeps rearranges the order of the static library dependencies of the module
  3049. // to match the topological order of the dependency tree, including any static analogues of
  3050. // direct shared libraries. It returns the ordered static dependencies, and an android.DepSet
  3051. // of the transitive dependencies.
  3052. func orderStaticModuleDeps(staticDeps []StaticLibraryInfo, sharedDeps []SharedLibraryInfo) (ordered android.Paths, transitive *android.DepSet) {
  3053. transitiveStaticLibsBuilder := android.NewDepSetBuilder(android.TOPOLOGICAL)
  3054. var staticPaths android.Paths
  3055. for _, staticDep := range staticDeps {
  3056. staticPaths = append(staticPaths, staticDep.StaticLibrary)
  3057. transitiveStaticLibsBuilder.Transitive(staticDep.TransitiveStaticLibrariesForOrdering)
  3058. }
  3059. for _, sharedDep := range sharedDeps {
  3060. if sharedDep.TransitiveStaticLibrariesForOrdering != nil {
  3061. transitiveStaticLibsBuilder.Transitive(sharedDep.TransitiveStaticLibrariesForOrdering)
  3062. }
  3063. }
  3064. transitiveStaticLibs := transitiveStaticLibsBuilder.Build()
  3065. orderedTransitiveStaticLibs := transitiveStaticLibs.ToList()
  3066. // reorder the dependencies based on transitive dependencies
  3067. staticPaths = android.FirstUniquePaths(staticPaths)
  3068. _, orderedStaticPaths := android.FilterPathList(orderedTransitiveStaticLibs, staticPaths)
  3069. if len(orderedStaticPaths) != len(staticPaths) {
  3070. missing, _ := android.FilterPathList(staticPaths, orderedStaticPaths)
  3071. panic(fmt.Errorf("expected %d ordered static paths , got %d, missing %q %q %q", len(staticPaths), len(orderedStaticPaths), missing, orderedStaticPaths, staticPaths))
  3072. }
  3073. return orderedStaticPaths, transitiveStaticLibs
  3074. }
  3075. // BaseLibName trims known prefixes and suffixes
  3076. func BaseLibName(depName string) string {
  3077. libName := strings.TrimSuffix(depName, llndkLibrarySuffix)
  3078. libName = strings.TrimSuffix(libName, vendorPublicLibrarySuffix)
  3079. libName = android.RemoveOptionalPrebuiltPrefix(libName)
  3080. return libName
  3081. }
  3082. func MakeLibName(ctx android.ModuleContext, c LinkableInterface, ccDep LinkableInterface, depName string) string {
  3083. libName := BaseLibName(depName)
  3084. ccDepModule, _ := ccDep.(*Module)
  3085. isLLndk := ccDepModule != nil && ccDepModule.IsLlndk()
  3086. nonSystemVariantsExist := ccDep.HasNonSystemVariants() || isLLndk
  3087. if ccDepModule != nil {
  3088. // Use base module name for snapshots when exporting to Makefile.
  3089. if snapshotPrebuilt, ok := ccDepModule.linker.(SnapshotInterface); ok {
  3090. baseName := ccDepModule.BaseModuleName()
  3091. return baseName + snapshotPrebuilt.SnapshotAndroidMkSuffix()
  3092. }
  3093. }
  3094. if ctx.DeviceConfig().VndkUseCoreVariant() && ccDep.IsVndk() && !ccDep.MustUseVendorVariant() &&
  3095. !c.InRamdisk() && !c.InVendorRamdisk() && !c.InRecovery() {
  3096. // The vendor module is a no-vendor-variant VNDK library. Depend on the
  3097. // core module instead.
  3098. return libName
  3099. } else if ccDep.UseVndk() && nonSystemVariantsExist {
  3100. // The vendor and product modules in Make will have been renamed to not conflict with the
  3101. // core module, so update the dependency name here accordingly.
  3102. return libName + ccDep.SubName()
  3103. } else if ccDep.InRamdisk() && !ccDep.OnlyInRamdisk() {
  3104. return libName + RamdiskSuffix
  3105. } else if ccDep.InVendorRamdisk() && !ccDep.OnlyInVendorRamdisk() {
  3106. return libName + VendorRamdiskSuffix
  3107. } else if ccDep.InRecovery() && !ccDep.OnlyInRecovery() {
  3108. return libName + RecoverySuffix
  3109. } else if ccDep.Target().NativeBridge == android.NativeBridgeEnabled {
  3110. return libName + NativeBridgeSuffix
  3111. } else {
  3112. return libName
  3113. }
  3114. }
  3115. func (c *Module) InstallInData() bool {
  3116. if c.installer == nil {
  3117. return false
  3118. }
  3119. return c.installer.inData()
  3120. }
  3121. func (c *Module) InstallInSanitizerDir() bool {
  3122. if c.installer == nil {
  3123. return false
  3124. }
  3125. if c.sanitize != nil && c.sanitize.inSanitizerDir() {
  3126. return true
  3127. }
  3128. return c.installer.inSanitizerDir()
  3129. }
  3130. func (c *Module) InstallInRamdisk() bool {
  3131. return c.InRamdisk()
  3132. }
  3133. func (c *Module) InstallInVendorRamdisk() bool {
  3134. return c.InVendorRamdisk()
  3135. }
  3136. func (c *Module) InstallInRecovery() bool {
  3137. return c.InRecovery()
  3138. }
  3139. func (c *Module) MakeUninstallable() {
  3140. if c.installer == nil {
  3141. c.ModuleBase.MakeUninstallable()
  3142. return
  3143. }
  3144. c.installer.makeUninstallable(c)
  3145. }
  3146. func (c *Module) HostToolPath() android.OptionalPath {
  3147. if c.installer == nil {
  3148. return android.OptionalPath{}
  3149. }
  3150. return c.installer.hostToolPath()
  3151. }
  3152. func (c *Module) IntermPathForModuleOut() android.OptionalPath {
  3153. return c.outputFile
  3154. }
  3155. func (c *Module) OutputFiles(tag string) (android.Paths, error) {
  3156. switch tag {
  3157. case "":
  3158. if c.outputFile.Valid() {
  3159. return android.Paths{c.outputFile.Path()}, nil
  3160. }
  3161. return android.Paths{}, nil
  3162. case "unstripped":
  3163. if c.linker != nil {
  3164. return android.PathsIfNonNil(c.linker.unstrippedOutputFilePath()), nil
  3165. }
  3166. return nil, nil
  3167. default:
  3168. return nil, fmt.Errorf("unsupported module reference tag %q", tag)
  3169. }
  3170. }
  3171. func (c *Module) static() bool {
  3172. if static, ok := c.linker.(interface {
  3173. static() bool
  3174. }); ok {
  3175. return static.static()
  3176. }
  3177. return false
  3178. }
  3179. func (c *Module) staticBinary() bool {
  3180. if static, ok := c.linker.(interface {
  3181. staticBinary() bool
  3182. }); ok {
  3183. return static.staticBinary()
  3184. }
  3185. return false
  3186. }
  3187. func (c *Module) testBinary() bool {
  3188. if test, ok := c.linker.(interface {
  3189. testBinary() bool
  3190. }); ok {
  3191. return test.testBinary()
  3192. }
  3193. return false
  3194. }
  3195. func (c *Module) testLibrary() bool {
  3196. if test, ok := c.linker.(interface {
  3197. testLibrary() bool
  3198. }); ok {
  3199. return test.testLibrary()
  3200. }
  3201. return false
  3202. }
  3203. func (c *Module) benchmarkBinary() bool {
  3204. if b, ok := c.linker.(interface {
  3205. benchmarkBinary() bool
  3206. }); ok {
  3207. return b.benchmarkBinary()
  3208. }
  3209. return false
  3210. }
  3211. func (c *Module) fuzzBinary() bool {
  3212. if f, ok := c.linker.(interface {
  3213. fuzzBinary() bool
  3214. }); ok {
  3215. return f.fuzzBinary()
  3216. }
  3217. return false
  3218. }
  3219. // Header returns true if the module is a header-only variant. (See cc/library.go header()).
  3220. func (c *Module) Header() bool {
  3221. if h, ok := c.linker.(interface {
  3222. header() bool
  3223. }); ok {
  3224. return h.header()
  3225. }
  3226. return false
  3227. }
  3228. func (c *Module) Binary() bool {
  3229. if b, ok := c.linker.(interface {
  3230. binary() bool
  3231. }); ok {
  3232. return b.binary()
  3233. }
  3234. return false
  3235. }
  3236. func (c *Module) StaticExecutable() bool {
  3237. if b, ok := c.linker.(*binaryDecorator); ok {
  3238. return b.static()
  3239. }
  3240. return false
  3241. }
  3242. func (c *Module) Object() bool {
  3243. if o, ok := c.linker.(interface {
  3244. object() bool
  3245. }); ok {
  3246. return o.object()
  3247. }
  3248. return false
  3249. }
  3250. func GetMakeLinkType(actx android.ModuleContext, c LinkableInterface) string {
  3251. if c.UseVndk() {
  3252. if c.IsLlndk() {
  3253. if !c.IsLlndkPublic() {
  3254. return "native:vndk_private"
  3255. }
  3256. return "native:vndk"
  3257. }
  3258. if c.IsVndk() && !c.IsVndkExt() {
  3259. if c.IsVndkPrivate() {
  3260. return "native:vndk_private"
  3261. }
  3262. return "native:vndk"
  3263. }
  3264. if c.InProduct() {
  3265. return "native:product"
  3266. }
  3267. return "native:vendor"
  3268. } else if c.InRamdisk() {
  3269. return "native:ramdisk"
  3270. } else if c.InVendorRamdisk() {
  3271. return "native:vendor_ramdisk"
  3272. } else if c.InRecovery() {
  3273. return "native:recovery"
  3274. } else if c.Target().Os == android.Android && c.SdkVersion() != "" {
  3275. return "native:ndk:none:none"
  3276. // TODO(b/114741097): use the correct ndk stl once build errors have been fixed
  3277. //family, link := getNdkStlFamilyAndLinkType(c)
  3278. //return fmt.Sprintf("native:ndk:%s:%s", family, link)
  3279. } else if actx.DeviceConfig().VndkUseCoreVariant() && !c.MustUseVendorVariant() {
  3280. return "native:platform_vndk"
  3281. } else {
  3282. return "native:platform"
  3283. }
  3284. }
  3285. // Overrides ApexModule.IsInstallabeToApex()
  3286. // Only shared/runtime libraries and "test_per_src" tests are installable to APEX.
  3287. func (c *Module) IsInstallableToApex() bool {
  3288. if lib := c.library; lib != nil {
  3289. // Stub libs and prebuilt libs in a versioned SDK are not
  3290. // installable to APEX even though they are shared libs.
  3291. return lib.shared() && !lib.buildStubs()
  3292. } else if _, ok := c.linker.(testPerSrc); ok {
  3293. return true
  3294. }
  3295. return false
  3296. }
  3297. func (c *Module) AvailableFor(what string) bool {
  3298. if linker, ok := c.linker.(interface {
  3299. availableFor(string) bool
  3300. }); ok {
  3301. return c.ApexModuleBase.AvailableFor(what) || linker.availableFor(what)
  3302. } else {
  3303. return c.ApexModuleBase.AvailableFor(what)
  3304. }
  3305. }
  3306. func (c *Module) TestFor() []string {
  3307. return c.Properties.Test_for
  3308. }
  3309. func (c *Module) EverInstallable() bool {
  3310. return c.installer != nil &&
  3311. // Check to see whether the module is actually ever installable.
  3312. c.installer.everInstallable()
  3313. }
  3314. func (c *Module) PreventInstall() bool {
  3315. return c.Properties.PreventInstall
  3316. }
  3317. func (c *Module) Installable() *bool {
  3318. if c.library != nil {
  3319. if i := c.library.installable(); i != nil {
  3320. return i
  3321. }
  3322. }
  3323. return c.Properties.Installable
  3324. }
  3325. func installable(c LinkableInterface, apexInfo android.ApexInfo) bool {
  3326. ret := c.EverInstallable() &&
  3327. // Check to see whether the module has been configured to not be installed.
  3328. proptools.BoolDefault(c.Installable(), true) &&
  3329. !c.PreventInstall() && c.OutputFile().Valid()
  3330. // The platform variant doesn't need further condition. Apex variants however might not
  3331. // be installable because it will likely to be included in the APEX and won't appear
  3332. // in the system partition.
  3333. if apexInfo.IsForPlatform() {
  3334. return ret
  3335. }
  3336. // Special case for modules that are configured to be installed to /data, which includes
  3337. // test modules. For these modules, both APEX and non-APEX variants are considered as
  3338. // installable. This is because even the APEX variants won't be included in the APEX, but
  3339. // will anyway be installed to /data/*.
  3340. // See b/146995717
  3341. if c.InstallInData() {
  3342. return ret
  3343. }
  3344. return false
  3345. }
  3346. func (c *Module) AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w io.Writer) {
  3347. if c.linker != nil {
  3348. if library, ok := c.linker.(*libraryDecorator); ok {
  3349. library.androidMkWriteAdditionalDependenciesForSourceAbiDiff(w)
  3350. }
  3351. }
  3352. }
  3353. var _ android.ApexModule = (*Module)(nil)
  3354. // Implements android.ApexModule
  3355. func (c *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
  3356. depTag := ctx.OtherModuleDependencyTag(dep)
  3357. libDepTag, isLibDepTag := depTag.(libraryDependencyTag)
  3358. if cc, ok := dep.(*Module); ok {
  3359. if cc.HasStubsVariants() {
  3360. if isLibDepTag && libDepTag.shared() {
  3361. // dynamic dep to a stubs lib crosses APEX boundary
  3362. return false
  3363. }
  3364. if IsRuntimeDepTag(depTag) {
  3365. // runtime dep to a stubs lib also crosses APEX boundary
  3366. return false
  3367. }
  3368. }
  3369. if cc.IsLlndk() {
  3370. return false
  3371. }
  3372. if isLibDepTag && c.static() && libDepTag.shared() {
  3373. // shared_lib dependency from a static lib is considered as crossing
  3374. // the APEX boundary because the dependency doesn't actually is
  3375. // linked; the dependency is used only during the compilation phase.
  3376. return false
  3377. }
  3378. if isLibDepTag && libDepTag.excludeInApex {
  3379. return false
  3380. }
  3381. }
  3382. if depTag == stubImplDepTag {
  3383. // We don't track from an implementation library to its stubs.
  3384. return false
  3385. }
  3386. if depTag == staticVariantTag {
  3387. // This dependency is for optimization (reuse *.o from the static lib). It doesn't
  3388. // actually mean that the static lib (and its dependencies) are copied into the
  3389. // APEX.
  3390. return false
  3391. }
  3392. return true
  3393. }
  3394. // Implements android.ApexModule
  3395. func (c *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
  3396. sdkVersion android.ApiLevel) error {
  3397. // We ignore libclang_rt.* prebuilt libs since they declare sdk_version: 14(b/121358700)
  3398. if strings.HasPrefix(ctx.OtherModuleName(c), "libclang_rt") {
  3399. return nil
  3400. }
  3401. // We don't check for prebuilt modules
  3402. if _, ok := c.linker.(prebuiltLinkerInterface); ok {
  3403. return nil
  3404. }
  3405. minSdkVersion := c.MinSdkVersion()
  3406. if minSdkVersion == "apex_inherit" {
  3407. return nil
  3408. }
  3409. if minSdkVersion == "" {
  3410. // JNI libs within APK-in-APEX fall into here
  3411. // Those are okay to set sdk_version instead
  3412. // We don't have to check if this is a SDK variant because
  3413. // non-SDK variant resets sdk_version, which works too.
  3414. minSdkVersion = c.SdkVersion()
  3415. }
  3416. if minSdkVersion == "" {
  3417. return fmt.Errorf("neither min_sdk_version nor sdk_version specificed")
  3418. }
  3419. // Not using nativeApiLevelFromUser because the context here is not
  3420. // necessarily a native context.
  3421. ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
  3422. if err != nil {
  3423. return err
  3424. }
  3425. // A dependency only needs to support a min_sdk_version at least
  3426. // as high as the api level that the architecture was introduced in.
  3427. // This allows introducing new architectures in the platform that
  3428. // need to be included in apexes that normally require an older
  3429. // min_sdk_version.
  3430. minApiForArch := MinApiForArch(ctx, c.Target().Arch.ArchType)
  3431. if sdkVersion.LessThan(minApiForArch) {
  3432. sdkVersion = minApiForArch
  3433. }
  3434. if ver.GreaterThan(sdkVersion) {
  3435. return fmt.Errorf("newer SDK(%v)", ver)
  3436. }
  3437. return nil
  3438. }
  3439. // Implements android.ApexModule
  3440. func (c *Module) AlwaysRequiresPlatformApexVariant() bool {
  3441. // stub libraries and native bridge libraries are always available to platform
  3442. return c.IsStubs() || c.Target().NativeBridge == android.NativeBridgeEnabled
  3443. }
  3444. // Overrides android.ApexModuleBase.UniqueApexVariations
  3445. func (c *Module) UniqueApexVariations() bool {
  3446. // When a vendor APEX needs a VNDK lib in it (use_vndk_as_stable: false), it should be a unique
  3447. // APEX variation. Otherwise, another vendor APEX with use_vndk_as_stable:true may use a wrong
  3448. // variation of the VNDK lib because APEX variations are merged/grouped.
  3449. // TODO(b/274401041) Find a way to merge APEX variations for vendor apexes.
  3450. return c.UseVndk() && c.IsVndk()
  3451. }
  3452. func (c *Module) overriddenModules() []string {
  3453. if o, ok := c.linker.(overridable); ok {
  3454. return o.overriddenModules()
  3455. }
  3456. return nil
  3457. }
  3458. var _ snapshot.RelativeInstallPath = (*Module)(nil)
  3459. type moduleType int
  3460. const (
  3461. unknownType moduleType = iota
  3462. binary
  3463. object
  3464. fullLibrary
  3465. staticLibrary
  3466. sharedLibrary
  3467. headerLibrary
  3468. testBin // testBinary already declared
  3469. ndkLibrary
  3470. )
  3471. func (c *Module) typ() moduleType {
  3472. if c.testBinary() {
  3473. // testBinary is also a binary, so this comes before the c.Binary()
  3474. // conditional. A testBinary has additional implicit dependencies and
  3475. // other test-only semantics.
  3476. return testBin
  3477. } else if c.Binary() {
  3478. return binary
  3479. } else if c.Object() {
  3480. return object
  3481. } else if c.testLibrary() {
  3482. // TODO(b/244431896) properly convert cc_test_library to its own macro. This
  3483. // will let them add implicit compile deps on gtest, for example.
  3484. //
  3485. // For now, treat them as regular libraries.
  3486. return fullLibrary
  3487. } else if c.CcLibrary() {
  3488. static := false
  3489. shared := false
  3490. if library, ok := c.linker.(*libraryDecorator); ok {
  3491. static = library.MutatedProperties.BuildStatic
  3492. shared = library.MutatedProperties.BuildShared
  3493. } else if library, ok := c.linker.(*prebuiltLibraryLinker); ok {
  3494. static = library.MutatedProperties.BuildStatic
  3495. shared = library.MutatedProperties.BuildShared
  3496. }
  3497. if static && shared {
  3498. return fullLibrary
  3499. } else if !static && !shared {
  3500. return headerLibrary
  3501. } else if static {
  3502. return staticLibrary
  3503. }
  3504. return sharedLibrary
  3505. } else if c.isNDKStubLibrary() {
  3506. return ndkLibrary
  3507. }
  3508. return unknownType
  3509. }
  3510. // ConvertWithBp2build converts Module to Bazel for bp2build.
  3511. func (c *Module) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
  3512. prebuilt := c.IsPrebuilt()
  3513. switch c.typ() {
  3514. case binary:
  3515. if prebuilt {
  3516. prebuiltBinaryBp2Build(ctx, c)
  3517. } else {
  3518. binaryBp2build(ctx, c)
  3519. }
  3520. case testBin:
  3521. if !prebuilt {
  3522. testBinaryBp2build(ctx, c)
  3523. }
  3524. case object:
  3525. if prebuilt {
  3526. prebuiltObjectBp2Build(ctx, c)
  3527. } else {
  3528. objectBp2Build(ctx, c)
  3529. }
  3530. case fullLibrary:
  3531. if !prebuilt {
  3532. libraryBp2Build(ctx, c)
  3533. } else {
  3534. prebuiltLibraryBp2Build(ctx, c)
  3535. }
  3536. case headerLibrary:
  3537. libraryHeadersBp2Build(ctx, c)
  3538. case staticLibrary:
  3539. if prebuilt {
  3540. prebuiltLibraryStaticBp2Build(ctx, c, false)
  3541. } else {
  3542. sharedOrStaticLibraryBp2Build(ctx, c, true)
  3543. }
  3544. case sharedLibrary:
  3545. if prebuilt {
  3546. prebuiltLibrarySharedBp2Build(ctx, c)
  3547. } else {
  3548. sharedOrStaticLibraryBp2Build(ctx, c, false)
  3549. }
  3550. default:
  3551. ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_TYPE_UNSUPPORTED, "")
  3552. }
  3553. }
  3554. var _ android.ApiProvider = (*Module)(nil)
  3555. func (c *Module) ConvertWithApiBp2build(ctx android.TopDownMutatorContext) {
  3556. if c.IsPrebuilt() {
  3557. return
  3558. }
  3559. switch c.typ() {
  3560. case fullLibrary:
  3561. apiContributionBp2Build(ctx, c)
  3562. case sharedLibrary:
  3563. apiContributionBp2Build(ctx, c)
  3564. case headerLibrary:
  3565. // Aggressively generate api targets for all header modules
  3566. // This is necessary since the header module does not know if it is a dep of API surface stub library
  3567. apiLibraryHeadersBp2Build(ctx, c)
  3568. }
  3569. }
  3570. // Defaults
  3571. type Defaults struct {
  3572. android.ModuleBase
  3573. android.DefaultsModuleBase
  3574. android.ApexModuleBase
  3575. }
  3576. // cc_defaults provides a set of properties that can be inherited by other cc
  3577. // modules. A module can use the properties from a cc_defaults using
  3578. // `defaults: ["<:default_module_name>"]`. Properties of both modules are
  3579. // merged (when possible) by prepending the default module's values to the
  3580. // depending module's values.
  3581. func defaultsFactory() android.Module {
  3582. return DefaultsFactory()
  3583. }
  3584. func DefaultsFactory(props ...interface{}) android.Module {
  3585. module := &Defaults{}
  3586. module.AddProperties(props...)
  3587. module.AddProperties(
  3588. &BaseProperties{},
  3589. &VendorProperties{},
  3590. &BaseCompilerProperties{},
  3591. &BaseLinkerProperties{},
  3592. &ObjectLinkerProperties{},
  3593. &LibraryProperties{},
  3594. &StaticProperties{},
  3595. &SharedProperties{},
  3596. &FlagExporterProperties{},
  3597. &BinaryLinkerProperties{},
  3598. &TestLinkerProperties{},
  3599. &TestInstallerProperties{},
  3600. &TestBinaryProperties{},
  3601. &BenchmarkProperties{},
  3602. &fuzz.FuzzProperties{},
  3603. &StlProperties{},
  3604. &SanitizeProperties{},
  3605. &StripProperties{},
  3606. &InstallerProperties{},
  3607. &TidyProperties{},
  3608. &CoverageProperties{},
  3609. &SAbiProperties{},
  3610. &VndkProperties{},
  3611. &LTOProperties{},
  3612. &AfdoProperties{},
  3613. &PgoProperties{},
  3614. &android.ProtoProperties{},
  3615. // RustBindgenProperties is included here so that cc_defaults can be used for rust_bindgen modules.
  3616. &RustBindgenClangProperties{},
  3617. &prebuiltLinkerProperties{},
  3618. )
  3619. android.InitDefaultsModule(module)
  3620. return module
  3621. }
  3622. func (c *Module) IsSdkVariant() bool {
  3623. return c.Properties.IsSdkVariant
  3624. }
  3625. func (c *Module) isImportedApiLibrary() bool {
  3626. _, ok := c.linker.(*apiLibraryDecorator)
  3627. return ok
  3628. }
  3629. func kytheExtractAllFactory() android.Singleton {
  3630. return &kytheExtractAllSingleton{}
  3631. }
  3632. type kytheExtractAllSingleton struct {
  3633. }
  3634. func (ks *kytheExtractAllSingleton) GenerateBuildActions(ctx android.SingletonContext) {
  3635. var xrefTargets android.Paths
  3636. ctx.VisitAllModules(func(module android.Module) {
  3637. if ccModule, ok := module.(xref); ok {
  3638. xrefTargets = append(xrefTargets, ccModule.XrefCcFiles()...)
  3639. }
  3640. })
  3641. // TODO(asmundak): Perhaps emit a rule to output a warning if there were no xrefTargets
  3642. if len(xrefTargets) > 0 {
  3643. ctx.Phony("xref_cxx", xrefTargets...)
  3644. }
  3645. }
  3646. func (c *Module) Partition() string {
  3647. if p, ok := c.installer.(interface {
  3648. getPartition() string
  3649. }); ok {
  3650. return p.getPartition()
  3651. }
  3652. return ""
  3653. }
  3654. var Bool = proptools.Bool
  3655. var BoolDefault = proptools.BoolDefault
  3656. var BoolPtr = proptools.BoolPtr
  3657. var String = proptools.String
  3658. var StringPtr = proptools.StringPtr