cc.go 116 KB

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