cc.go 131 KB

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