rust.go 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800
  1. // Copyright 2019 The Android Open Source Project
  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 rust
  15. import (
  16. "android/soong/bloaty"
  17. "fmt"
  18. "strings"
  19. "github.com/google/blueprint"
  20. "github.com/google/blueprint/proptools"
  21. "android/soong/android"
  22. "android/soong/cc"
  23. cc_config "android/soong/cc/config"
  24. "android/soong/fuzz"
  25. "android/soong/multitree"
  26. "android/soong/rust/config"
  27. "android/soong/snapshot"
  28. )
  29. var pctx = android.NewPackageContext("android/soong/rust")
  30. func init() {
  31. android.RegisterModuleType("rust_defaults", defaultsFactory)
  32. android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
  33. ctx.BottomUp("rust_libraries", LibraryMutator).Parallel()
  34. ctx.BottomUp("rust_stdlinkage", LibstdMutator).Parallel()
  35. ctx.BottomUp("rust_begin", BeginMutator).Parallel()
  36. })
  37. android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
  38. ctx.BottomUp("rust_sanitizers", rustSanitizerRuntimeMutator).Parallel()
  39. })
  40. pctx.Import("android/soong/rust/config")
  41. pctx.ImportAs("cc_config", "android/soong/cc/config")
  42. android.InitRegistrationContext.RegisterParallelSingletonType("kythe_rust_extract", kytheExtractRustFactory)
  43. }
  44. type Flags struct {
  45. GlobalRustFlags []string // Flags that apply globally to rust
  46. GlobalLinkFlags []string // Flags that apply globally to linker
  47. RustFlags []string // Flags that apply to rust
  48. LinkFlags []string // Flags that apply to linker
  49. ClippyFlags []string // Flags that apply to clippy-driver, during the linting
  50. RustdocFlags []string // Flags that apply to rustdoc
  51. Toolchain config.Toolchain
  52. Coverage bool
  53. Clippy bool
  54. EmitXrefs bool // If true, emit rules to aid cross-referencing
  55. }
  56. type BaseProperties struct {
  57. AndroidMkRlibs []string `blueprint:"mutated"`
  58. AndroidMkDylibs []string `blueprint:"mutated"`
  59. AndroidMkProcMacroLibs []string `blueprint:"mutated"`
  60. AndroidMkSharedLibs []string `blueprint:"mutated"`
  61. AndroidMkStaticLibs []string `blueprint:"mutated"`
  62. ImageVariationPrefix string `blueprint:"mutated"`
  63. VndkVersion string `blueprint:"mutated"`
  64. SubName string `blueprint:"mutated"`
  65. // SubName is used by CC for tracking image variants / SDK versions. RustSubName is used for Rust-specific
  66. // subnaming which shouldn't be visible to CC modules (such as the rlib stdlinkage subname). This should be
  67. // appended before SubName.
  68. RustSubName string `blueprint:"mutated"`
  69. // Set by imageMutator
  70. CoreVariantNeeded bool `blueprint:"mutated"`
  71. VendorRamdiskVariantNeeded bool `blueprint:"mutated"`
  72. RamdiskVariantNeeded bool `blueprint:"mutated"`
  73. RecoveryVariantNeeded bool `blueprint:"mutated"`
  74. ExtraVariants []string `blueprint:"mutated"`
  75. // Allows this module to use non-APEX version of libraries. Useful
  76. // for building binaries that are started before APEXes are activated.
  77. Bootstrap *bool
  78. // Used by vendor snapshot to record dependencies from snapshot modules.
  79. SnapshotSharedLibs []string `blueprint:"mutated"`
  80. SnapshotStaticLibs []string `blueprint:"mutated"`
  81. // Make this module available when building for ramdisk.
  82. // On device without a dedicated recovery partition, the module is only
  83. // available after switching root into
  84. // /first_stage_ramdisk. To expose the module before switching root, install
  85. // the recovery variant instead.
  86. Ramdisk_available *bool
  87. // Make this module available when building for vendor ramdisk.
  88. // On device without a dedicated recovery partition, the module is only
  89. // available after switching root into
  90. // /first_stage_ramdisk. To expose the module before switching root, install
  91. // the recovery variant instead
  92. Vendor_ramdisk_available *bool
  93. // Normally Soong uses the directory structure to decide which modules
  94. // should be included (framework) or excluded (non-framework) from the
  95. // different snapshots (vendor, recovery, etc.), but this property
  96. // allows a partner to exclude a module normally thought of as a
  97. // framework module from the vendor snapshot.
  98. Exclude_from_vendor_snapshot *bool
  99. // Normally Soong uses the directory structure to decide which modules
  100. // should be included (framework) or excluded (non-framework) from the
  101. // different snapshots (vendor, recovery, etc.), but this property
  102. // allows a partner to exclude a module normally thought of as a
  103. // framework module from the recovery snapshot.
  104. Exclude_from_recovery_snapshot *bool
  105. // Make this module available when building for recovery
  106. Recovery_available *bool
  107. // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
  108. Min_sdk_version *string
  109. HideFromMake bool `blueprint:"mutated"`
  110. PreventInstall bool `blueprint:"mutated"`
  111. Installable *bool
  112. }
  113. type Module struct {
  114. fuzz.FuzzModule
  115. VendorProperties cc.VendorProperties
  116. Properties BaseProperties
  117. hod android.HostOrDeviceSupported
  118. multilib android.Multilib
  119. makeLinkType string
  120. afdo *afdo
  121. compiler compiler
  122. coverage *coverage
  123. clippy *clippy
  124. sanitize *sanitize
  125. cachedToolchain config.Toolchain
  126. sourceProvider SourceProvider
  127. subAndroidMkOnce map[SubAndroidMkProvider]bool
  128. // Output file to be installed, may be stripped or unstripped.
  129. outputFile android.OptionalPath
  130. // Cross-reference input file
  131. kytheFiles android.Paths
  132. docTimestampFile android.OptionalPath
  133. hideApexVariantFromMake bool
  134. // For apex variants, this is set as apex.min_sdk_version
  135. apexSdkVersion android.ApiLevel
  136. }
  137. func (mod *Module) Header() bool {
  138. //TODO: If Rust libraries provide header variants, this needs to be updated.
  139. return false
  140. }
  141. func (mod *Module) SetPreventInstall() {
  142. mod.Properties.PreventInstall = true
  143. }
  144. func (mod *Module) SetHideFromMake() {
  145. mod.Properties.HideFromMake = true
  146. }
  147. func (mod *Module) HiddenFromMake() bool {
  148. return mod.Properties.HideFromMake
  149. }
  150. func (mod *Module) SanitizePropDefined() bool {
  151. // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
  152. // nil since we need compiler to actually sanitize.
  153. return mod.sanitize != nil && mod.compiler != nil
  154. }
  155. func (mod *Module) IsPrebuilt() bool {
  156. if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
  157. return true
  158. }
  159. return false
  160. }
  161. func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
  162. switch tag {
  163. case "":
  164. if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
  165. return mod.sourceProvider.Srcs(), nil
  166. } else {
  167. if mod.OutputFile().Valid() {
  168. return android.Paths{mod.OutputFile().Path()}, nil
  169. }
  170. return android.Paths{}, nil
  171. }
  172. case "unstripped":
  173. if mod.compiler != nil {
  174. return android.PathsIfNonNil(mod.compiler.unstrippedOutputFilePath()), nil
  175. }
  176. return nil, nil
  177. default:
  178. return nil, fmt.Errorf("unsupported module reference tag %q", tag)
  179. }
  180. }
  181. func (mod *Module) SelectedStl() string {
  182. return ""
  183. }
  184. func (mod *Module) NonCcVariants() bool {
  185. if mod.compiler != nil {
  186. if _, ok := mod.compiler.(libraryInterface); ok {
  187. return false
  188. }
  189. }
  190. panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
  191. }
  192. func (mod *Module) Static() bool {
  193. if mod.compiler != nil {
  194. if library, ok := mod.compiler.(libraryInterface); ok {
  195. return library.static()
  196. }
  197. }
  198. return false
  199. }
  200. func (mod *Module) Shared() bool {
  201. if mod.compiler != nil {
  202. if library, ok := mod.compiler.(libraryInterface); ok {
  203. return library.shared()
  204. }
  205. }
  206. return false
  207. }
  208. func (mod *Module) Dylib() bool {
  209. if mod.compiler != nil {
  210. if library, ok := mod.compiler.(libraryInterface); ok {
  211. return library.dylib()
  212. }
  213. }
  214. return false
  215. }
  216. func (mod *Module) Rlib() bool {
  217. if mod.compiler != nil {
  218. if library, ok := mod.compiler.(libraryInterface); ok {
  219. return library.rlib()
  220. }
  221. }
  222. return false
  223. }
  224. func (mod *Module) Binary() bool {
  225. if binary, ok := mod.compiler.(binaryInterface); ok {
  226. return binary.binary()
  227. }
  228. return false
  229. }
  230. func (mod *Module) StaticExecutable() bool {
  231. if !mod.Binary() {
  232. return false
  233. }
  234. return mod.StaticallyLinked()
  235. }
  236. func (mod *Module) Object() bool {
  237. // Rust has no modules which produce only object files.
  238. return false
  239. }
  240. func (mod *Module) Toc() android.OptionalPath {
  241. if mod.compiler != nil {
  242. if lib, ok := mod.compiler.(libraryInterface); ok {
  243. return lib.toc()
  244. }
  245. }
  246. panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
  247. }
  248. func (mod *Module) UseSdk() bool {
  249. return false
  250. }
  251. func (mod *Module) RelativeInstallPath() string {
  252. if mod.compiler != nil {
  253. return mod.compiler.relativeInstallPath()
  254. }
  255. return ""
  256. }
  257. func (mod *Module) UseVndk() bool {
  258. return mod.Properties.VndkVersion != ""
  259. }
  260. func (mod *Module) Bootstrap() bool {
  261. return Bool(mod.Properties.Bootstrap)
  262. }
  263. func (mod *Module) MustUseVendorVariant() bool {
  264. return true
  265. }
  266. func (mod *Module) SubName() string {
  267. return mod.Properties.SubName
  268. }
  269. func (mod *Module) IsVndk() bool {
  270. // TODO(b/165791368)
  271. return false
  272. }
  273. func (mod *Module) IsVndkExt() bool {
  274. return false
  275. }
  276. func (mod *Module) IsVndkSp() bool {
  277. return false
  278. }
  279. func (mod *Module) IsVndkPrebuiltLibrary() bool {
  280. // Rust modules do not provide VNDK prebuilts
  281. return false
  282. }
  283. func (mod *Module) IsVendorPublicLibrary() bool {
  284. return mod.VendorProperties.IsVendorPublicLibrary
  285. }
  286. func (mod *Module) SdkAndPlatformVariantVisibleToMake() bool {
  287. // Rust modules to not provide Sdk variants
  288. return false
  289. }
  290. func (c *Module) IsVndkPrivate() bool {
  291. return false
  292. }
  293. func (c *Module) IsLlndk() bool {
  294. return false
  295. }
  296. func (c *Module) IsLlndkPublic() bool {
  297. return false
  298. }
  299. func (mod *Module) KernelHeadersDecorator() bool {
  300. return false
  301. }
  302. func (m *Module) NeedsLlndkVariants() bool {
  303. return false
  304. }
  305. func (m *Module) NeedsVendorPublicLibraryVariants() bool {
  306. return false
  307. }
  308. func (mod *Module) HasLlndkStubs() bool {
  309. return false
  310. }
  311. func (mod *Module) StubsVersion() string {
  312. panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
  313. }
  314. func (mod *Module) SdkVersion() string {
  315. return ""
  316. }
  317. func (mod *Module) AlwaysSdk() bool {
  318. return false
  319. }
  320. func (mod *Module) IsSdkVariant() bool {
  321. return false
  322. }
  323. func (mod *Module) SplitPerApiLevel() bool {
  324. return false
  325. }
  326. func (mod *Module) XrefRustFiles() android.Paths {
  327. return mod.kytheFiles
  328. }
  329. type Deps struct {
  330. Dylibs []string
  331. Rlibs []string
  332. Rustlibs []string
  333. Stdlibs []string
  334. ProcMacros []string
  335. SharedLibs []string
  336. StaticLibs []string
  337. WholeStaticLibs []string
  338. HeaderLibs []string
  339. // Used for data dependencies adjacent to tests
  340. DataLibs []string
  341. DataBins []string
  342. CrtBegin, CrtEnd []string
  343. }
  344. type PathDeps struct {
  345. DyLibs RustLibraries
  346. RLibs RustLibraries
  347. LibDeps android.Paths
  348. WholeStaticLibs android.Paths
  349. ProcMacros RustLibraries
  350. AfdoProfiles android.Paths
  351. // depFlags and depLinkFlags are rustc and linker (clang) flags.
  352. depFlags []string
  353. depLinkFlags []string
  354. // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
  355. // Both of these are exported and propagate to dependencies.
  356. linkDirs []string
  357. linkObjects android.Paths
  358. // Used by bindgen modules which call clang
  359. depClangFlags []string
  360. depIncludePaths android.Paths
  361. depGeneratedHeaders android.Paths
  362. depSystemIncludePaths android.Paths
  363. CrtBegin android.Paths
  364. CrtEnd android.Paths
  365. // Paths to generated source files
  366. SrcDeps android.Paths
  367. srcProviderFiles android.Paths
  368. }
  369. type RustLibraries []RustLibrary
  370. type RustLibrary struct {
  371. Path android.Path
  372. CrateName string
  373. }
  374. type compiler interface {
  375. initialize(ctx ModuleContext)
  376. compilerFlags(ctx ModuleContext, flags Flags) Flags
  377. cfgFlags(ctx ModuleContext, flags Flags) Flags
  378. featureFlags(ctx ModuleContext, flags Flags) Flags
  379. compilerProps() []interface{}
  380. compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput
  381. compilerDeps(ctx DepsContext, deps Deps) Deps
  382. crateName() string
  383. rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath
  384. // Output directory in which source-generated code from dependencies is
  385. // copied. This is equivalent to Cargo's OUT_DIR variable.
  386. CargoOutDir() android.OptionalPath
  387. // CargoPkgVersion returns the value of the Cargo_pkg_version property.
  388. CargoPkgVersion() string
  389. // CargoEnvCompat returns whether Cargo environment variables should be used.
  390. CargoEnvCompat() bool
  391. inData() bool
  392. install(ctx ModuleContext)
  393. relativeInstallPath() string
  394. everInstallable() bool
  395. nativeCoverage() bool
  396. Disabled() bool
  397. SetDisabled()
  398. stdLinkage(ctx *depsContext) RustLinkage
  399. noStdlibs() bool
  400. unstrippedOutputFilePath() android.Path
  401. strippedOutputFilePath() android.OptionalPath
  402. }
  403. type exportedFlagsProducer interface {
  404. exportLinkDirs(...string)
  405. exportLinkObjects(...android.Path)
  406. }
  407. type xref interface {
  408. XrefRustFiles() android.Paths
  409. }
  410. type flagExporter struct {
  411. linkDirs []string
  412. linkObjects android.Paths
  413. libDeps android.Paths
  414. }
  415. func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
  416. flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
  417. }
  418. func (flagExporter *flagExporter) exportLinkObjects(flags ...android.Path) {
  419. flagExporter.linkObjects = android.FirstUniquePaths(append(flagExporter.linkObjects, flags...))
  420. }
  421. func (flagExporter *flagExporter) exportLibDeps(paths ...android.Path) {
  422. flagExporter.libDeps = android.FirstUniquePaths(append(flagExporter.libDeps, paths...))
  423. }
  424. func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
  425. ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
  426. LinkDirs: flagExporter.linkDirs,
  427. LinkObjects: flagExporter.linkObjects,
  428. LibDeps: flagExporter.libDeps,
  429. })
  430. }
  431. var _ exportedFlagsProducer = (*flagExporter)(nil)
  432. func NewFlagExporter() *flagExporter {
  433. return &flagExporter{}
  434. }
  435. type FlagExporterInfo struct {
  436. Flags []string
  437. LinkDirs []string // TODO: this should be android.Paths
  438. LinkObjects android.Paths
  439. LibDeps android.Paths
  440. }
  441. var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
  442. func (mod *Module) isCoverageVariant() bool {
  443. return mod.coverage.Properties.IsCoverageVariant
  444. }
  445. var _ cc.Coverage = (*Module)(nil)
  446. func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
  447. return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
  448. }
  449. func (mod *Module) VndkVersion() string {
  450. return mod.Properties.VndkVersion
  451. }
  452. func (mod *Module) PreventInstall() bool {
  453. return mod.Properties.PreventInstall
  454. }
  455. func (mod *Module) MarkAsCoverageVariant(coverage bool) {
  456. mod.coverage.Properties.IsCoverageVariant = coverage
  457. }
  458. func (mod *Module) EnableCoverageIfNeeded() {
  459. mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
  460. }
  461. func defaultsFactory() android.Module {
  462. return DefaultsFactory()
  463. }
  464. type Defaults struct {
  465. android.ModuleBase
  466. android.DefaultsModuleBase
  467. }
  468. func DefaultsFactory(props ...interface{}) android.Module {
  469. module := &Defaults{}
  470. module.AddProperties(props...)
  471. module.AddProperties(
  472. &BaseProperties{},
  473. &cc.AfdoProperties{},
  474. &cc.VendorProperties{},
  475. &BenchmarkProperties{},
  476. &BindgenProperties{},
  477. &BaseCompilerProperties{},
  478. &BinaryCompilerProperties{},
  479. &LibraryCompilerProperties{},
  480. &ProcMacroCompilerProperties{},
  481. &PrebuiltProperties{},
  482. &SourceProviderProperties{},
  483. &TestProperties{},
  484. &cc.CoverageProperties{},
  485. &cc.RustBindgenClangProperties{},
  486. &ClippyProperties{},
  487. &SanitizeProperties{},
  488. )
  489. android.InitDefaultsModule(module)
  490. return module
  491. }
  492. func (mod *Module) CrateName() string {
  493. return mod.compiler.crateName()
  494. }
  495. func (mod *Module) CcLibrary() bool {
  496. if mod.compiler != nil {
  497. if _, ok := mod.compiler.(libraryInterface); ok {
  498. return true
  499. }
  500. }
  501. return false
  502. }
  503. func (mod *Module) CcLibraryInterface() bool {
  504. if mod.compiler != nil {
  505. // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
  506. // VariantIs{Static,Shared} is set.
  507. if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
  508. return true
  509. }
  510. }
  511. return false
  512. }
  513. func (mod *Module) RustLibraryInterface() bool {
  514. if mod.compiler != nil {
  515. if _, ok := mod.compiler.(libraryInterface); ok {
  516. return true
  517. }
  518. }
  519. return false
  520. }
  521. func (mod *Module) IsFuzzModule() bool {
  522. if _, ok := mod.compiler.(*fuzzDecorator); ok {
  523. return true
  524. }
  525. return false
  526. }
  527. func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
  528. return mod.FuzzModule
  529. }
  530. func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
  531. if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
  532. return fuzzer.fuzzPackagedModule
  533. }
  534. panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
  535. }
  536. func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
  537. if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
  538. return fuzzer.sharedLibraries
  539. }
  540. panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
  541. }
  542. func (mod *Module) UnstrippedOutputFile() android.Path {
  543. if mod.compiler != nil {
  544. return mod.compiler.unstrippedOutputFilePath()
  545. }
  546. return nil
  547. }
  548. func (mod *Module) IncludeDirs() android.Paths {
  549. if mod.compiler != nil {
  550. if library, ok := mod.compiler.(*libraryDecorator); ok {
  551. return library.includeDirs
  552. }
  553. }
  554. panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
  555. }
  556. func (mod *Module) SetStatic() {
  557. if mod.compiler != nil {
  558. if library, ok := mod.compiler.(libraryInterface); ok {
  559. library.setStatic()
  560. return
  561. }
  562. }
  563. panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
  564. }
  565. func (mod *Module) SetShared() {
  566. if mod.compiler != nil {
  567. if library, ok := mod.compiler.(libraryInterface); ok {
  568. library.setShared()
  569. return
  570. }
  571. }
  572. panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
  573. }
  574. func (mod *Module) BuildStaticVariant() bool {
  575. if mod.compiler != nil {
  576. if library, ok := mod.compiler.(libraryInterface); ok {
  577. return library.buildStatic()
  578. }
  579. }
  580. panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
  581. }
  582. func (mod *Module) BuildSharedVariant() bool {
  583. if mod.compiler != nil {
  584. if library, ok := mod.compiler.(libraryInterface); ok {
  585. return library.buildShared()
  586. }
  587. }
  588. panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
  589. }
  590. func (mod *Module) Module() android.Module {
  591. return mod
  592. }
  593. func (mod *Module) OutputFile() android.OptionalPath {
  594. return mod.outputFile
  595. }
  596. func (mod *Module) CoverageFiles() android.Paths {
  597. if mod.compiler != nil {
  598. return android.Paths{}
  599. }
  600. panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
  601. }
  602. // Rust does not produce gcno files, and therefore does not produce a coverage archive.
  603. func (mod *Module) CoverageOutputFile() android.OptionalPath {
  604. return android.OptionalPath{}
  605. }
  606. func (mod *Module) IsNdk(config android.Config) bool {
  607. return false
  608. }
  609. func (mod *Module) IsStubs() bool {
  610. return false
  611. }
  612. func (mod *Module) installable(apexInfo android.ApexInfo) bool {
  613. if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
  614. return false
  615. }
  616. // The apex variant is not installable because it is included in the APEX and won't appear
  617. // in the system partition as a standalone file.
  618. if !apexInfo.IsForPlatform() {
  619. return false
  620. }
  621. return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
  622. }
  623. func (ctx moduleContext) apexVariationName() string {
  624. return ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).ApexVariationName
  625. }
  626. var _ cc.LinkableInterface = (*Module)(nil)
  627. func (mod *Module) Init() android.Module {
  628. mod.AddProperties(&mod.Properties)
  629. mod.AddProperties(&mod.VendorProperties)
  630. if mod.afdo != nil {
  631. mod.AddProperties(mod.afdo.props()...)
  632. }
  633. if mod.compiler != nil {
  634. mod.AddProperties(mod.compiler.compilerProps()...)
  635. }
  636. if mod.coverage != nil {
  637. mod.AddProperties(mod.coverage.props()...)
  638. }
  639. if mod.clippy != nil {
  640. mod.AddProperties(mod.clippy.props()...)
  641. }
  642. if mod.sourceProvider != nil {
  643. mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
  644. }
  645. if mod.sanitize != nil {
  646. mod.AddProperties(mod.sanitize.props()...)
  647. }
  648. android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
  649. android.InitApexModule(mod)
  650. android.InitDefaultableModule(mod)
  651. return mod
  652. }
  653. func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
  654. return &Module{
  655. hod: hod,
  656. multilib: multilib,
  657. }
  658. }
  659. func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
  660. module := newBaseModule(hod, multilib)
  661. module.afdo = &afdo{}
  662. module.coverage = &coverage{}
  663. module.clippy = &clippy{}
  664. module.sanitize = &sanitize{}
  665. return module
  666. }
  667. type ModuleContext interface {
  668. android.ModuleContext
  669. ModuleContextIntf
  670. }
  671. type BaseModuleContext interface {
  672. android.BaseModuleContext
  673. ModuleContextIntf
  674. }
  675. type DepsContext interface {
  676. android.BottomUpMutatorContext
  677. ModuleContextIntf
  678. }
  679. type ModuleContextIntf interface {
  680. RustModule() *Module
  681. toolchain() config.Toolchain
  682. }
  683. type depsContext struct {
  684. android.BottomUpMutatorContext
  685. }
  686. type moduleContext struct {
  687. android.ModuleContext
  688. }
  689. type baseModuleContext struct {
  690. android.BaseModuleContext
  691. }
  692. func (ctx *moduleContext) RustModule() *Module {
  693. return ctx.Module().(*Module)
  694. }
  695. func (ctx *moduleContext) toolchain() config.Toolchain {
  696. return ctx.RustModule().toolchain(ctx)
  697. }
  698. func (ctx *depsContext) RustModule() *Module {
  699. return ctx.Module().(*Module)
  700. }
  701. func (ctx *depsContext) toolchain() config.Toolchain {
  702. return ctx.RustModule().toolchain(ctx)
  703. }
  704. func (ctx *baseModuleContext) RustModule() *Module {
  705. return ctx.Module().(*Module)
  706. }
  707. func (ctx *baseModuleContext) toolchain() config.Toolchain {
  708. return ctx.RustModule().toolchain(ctx)
  709. }
  710. func (mod *Module) nativeCoverage() bool {
  711. // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
  712. if mod.Target().NativeBridge == android.NativeBridgeEnabled {
  713. return false
  714. }
  715. return mod.compiler != nil && mod.compiler.nativeCoverage()
  716. }
  717. func (mod *Module) EverInstallable() bool {
  718. return mod.compiler != nil &&
  719. // Check to see whether the module is actually ever installable.
  720. mod.compiler.everInstallable()
  721. }
  722. func (mod *Module) Installable() *bool {
  723. return mod.Properties.Installable
  724. }
  725. func (mod *Module) ProcMacro() bool {
  726. if pm, ok := mod.compiler.(procMacroInterface); ok {
  727. return pm.ProcMacro()
  728. }
  729. return false
  730. }
  731. func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
  732. if mod.cachedToolchain == nil {
  733. mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
  734. }
  735. return mod.cachedToolchain
  736. }
  737. func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
  738. return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
  739. }
  740. func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  741. }
  742. func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
  743. ctx := &moduleContext{
  744. ModuleContext: actx,
  745. }
  746. apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
  747. if !apexInfo.IsForPlatform() {
  748. mod.hideApexVariantFromMake = true
  749. }
  750. toolchain := mod.toolchain(ctx)
  751. mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
  752. mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
  753. if !toolchain.Supported() {
  754. // This toolchain's unsupported, there's nothing to do for this mod.
  755. return
  756. }
  757. deps := mod.depsToPaths(ctx)
  758. flags := Flags{
  759. Toolchain: toolchain,
  760. }
  761. // Calculate rustc flags
  762. if mod.afdo != nil {
  763. flags, deps = mod.afdo.flags(actx, flags, deps)
  764. }
  765. if mod.compiler != nil {
  766. flags = mod.compiler.compilerFlags(ctx, flags)
  767. flags = mod.compiler.cfgFlags(ctx, flags)
  768. flags = mod.compiler.featureFlags(ctx, flags)
  769. }
  770. if mod.coverage != nil {
  771. flags, deps = mod.coverage.flags(ctx, flags, deps)
  772. }
  773. if mod.clippy != nil {
  774. flags, deps = mod.clippy.flags(ctx, flags, deps)
  775. }
  776. if mod.sanitize != nil {
  777. flags, deps = mod.sanitize.flags(ctx, flags, deps)
  778. }
  779. // SourceProvider needs to call GenerateSource() before compiler calls
  780. // compile() so it can provide the source. A SourceProvider has
  781. // multiple variants (e.g. source, rlib, dylib). Only the "source"
  782. // variant is responsible for effectively generating the source. The
  783. // remaining variants relies on the "source" variant output.
  784. if mod.sourceProvider != nil {
  785. if mod.compiler.(libraryInterface).source() {
  786. mod.sourceProvider.GenerateSource(ctx, deps)
  787. mod.sourceProvider.setSubName(ctx.ModuleSubDir())
  788. } else {
  789. sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
  790. sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
  791. mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
  792. }
  793. }
  794. if mod.compiler != nil && !mod.compiler.Disabled() {
  795. mod.compiler.initialize(ctx)
  796. buildOutput := mod.compiler.compile(ctx, flags, deps)
  797. if ctx.Failed() {
  798. return
  799. }
  800. mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
  801. if buildOutput.kytheFile != nil {
  802. mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
  803. }
  804. bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
  805. mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
  806. if mod.docTimestampFile.Valid() {
  807. ctx.CheckbuildFile(mod.docTimestampFile.Path())
  808. }
  809. // glob exported headers for snapshot, if BOARD_VNDK_VERSION is current or
  810. // RECOVERY_SNAPSHOT_VERSION is current.
  811. if lib, ok := mod.compiler.(snapshotLibraryInterface); ok {
  812. if cc.ShouldCollectHeadersForSnapshot(ctx, mod, apexInfo) {
  813. lib.collectHeadersForSnapshot(ctx, deps)
  814. }
  815. }
  816. apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
  817. if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
  818. // If the module has been specifically configure to not be installed then
  819. // hide from make as otherwise it will break when running inside make as the
  820. // output path to install will not be specified. Not all uninstallable
  821. // modules can be hidden from make as some are needed for resolving make
  822. // side dependencies. In particular, proc-macros need to be captured in the
  823. // host snapshot.
  824. mod.HideFromMake()
  825. } else if !mod.installable(apexInfo) {
  826. mod.SkipInstall()
  827. }
  828. // Still call install though, the installs will be stored as PackageSpecs to allow
  829. // using the outputs in a genrule.
  830. if mod.OutputFile().Valid() {
  831. mod.compiler.install(ctx)
  832. if ctx.Failed() {
  833. return
  834. }
  835. }
  836. ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
  837. }
  838. }
  839. func (mod *Module) deps(ctx DepsContext) Deps {
  840. deps := Deps{}
  841. if mod.compiler != nil {
  842. deps = mod.compiler.compilerDeps(ctx, deps)
  843. }
  844. if mod.sourceProvider != nil {
  845. deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
  846. }
  847. if mod.coverage != nil {
  848. deps = mod.coverage.deps(ctx, deps)
  849. }
  850. if mod.sanitize != nil {
  851. deps = mod.sanitize.deps(ctx, deps)
  852. }
  853. deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
  854. deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
  855. deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
  856. deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
  857. deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
  858. deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
  859. deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
  860. deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
  861. return deps
  862. }
  863. type dependencyTag struct {
  864. blueprint.BaseDependencyTag
  865. name string
  866. library bool
  867. procMacro bool
  868. dynamic bool
  869. }
  870. // InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
  871. // dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
  872. func (d dependencyTag) InstallDepNeeded() bool {
  873. return d.library || d.procMacro
  874. }
  875. var _ android.InstallNeededDependencyTag = dependencyTag{}
  876. func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
  877. if d.library && d.dynamic {
  878. return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
  879. }
  880. return nil
  881. }
  882. var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
  883. var (
  884. customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
  885. rlibDepTag = dependencyTag{name: "rlibTag", library: true}
  886. dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
  887. procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
  888. testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
  889. sourceDepTag = dependencyTag{name: "source"}
  890. dataLibDepTag = dependencyTag{name: "data lib"}
  891. dataBinDepTag = dependencyTag{name: "data bin"}
  892. )
  893. func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
  894. tag, ok := depTag.(dependencyTag)
  895. return ok && tag == dylibDepTag
  896. }
  897. func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
  898. tag, ok := depTag.(dependencyTag)
  899. return ok && tag == rlibDepTag
  900. }
  901. type autoDep struct {
  902. variation string
  903. depTag dependencyTag
  904. }
  905. var (
  906. rlibVariation = "rlib"
  907. dylibVariation = "dylib"
  908. rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
  909. dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
  910. )
  911. type autoDeppable interface {
  912. autoDep(ctx android.BottomUpMutatorContext) autoDep
  913. }
  914. func (mod *Module) begin(ctx BaseModuleContext) {
  915. if mod.coverage != nil {
  916. mod.coverage.begin(ctx)
  917. }
  918. if mod.sanitize != nil {
  919. mod.sanitize.begin(ctx)
  920. }
  921. }
  922. func (mod *Module) Prebuilt() *android.Prebuilt {
  923. if p, ok := mod.compiler.(rustPrebuilt); ok {
  924. return p.prebuilt()
  925. }
  926. return nil
  927. }
  928. func rustMakeLibName(ctx android.ModuleContext, c cc.LinkableInterface, dep cc.LinkableInterface, depName string) string {
  929. if rustDep, ok := dep.(*Module); ok {
  930. // Use base module name for snapshots when exporting to Makefile.
  931. if snapshotPrebuilt, ok := rustDep.compiler.(cc.SnapshotInterface); ok {
  932. baseName := rustDep.BaseModuleName()
  933. return baseName + snapshotPrebuilt.SnapshotAndroidMkSuffix() + rustDep.AndroidMkSuffix()
  934. }
  935. }
  936. return cc.MakeLibName(ctx, c, dep, depName)
  937. }
  938. func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
  939. var depPaths PathDeps
  940. directRlibDeps := []*Module{}
  941. directDylibDeps := []*Module{}
  942. directProcMacroDeps := []*Module{}
  943. directSharedLibDeps := []cc.SharedLibraryInfo{}
  944. directStaticLibDeps := [](cc.LinkableInterface){}
  945. directSrcProvidersDeps := []*Module{}
  946. directSrcDeps := [](android.SourceFileProducer){}
  947. // For the dependency from platform to apex, use the latest stubs
  948. mod.apexSdkVersion = android.FutureApiLevel
  949. apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
  950. if !apexInfo.IsForPlatform() {
  951. mod.apexSdkVersion = apexInfo.MinSdkVersion
  952. }
  953. if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
  954. // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
  955. // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
  956. // (b/144430859)
  957. mod.apexSdkVersion = android.FutureApiLevel
  958. }
  959. skipModuleList := map[string]bool{}
  960. var apiImportInfo multitree.ApiImportInfo
  961. hasApiImportInfo := false
  962. ctx.VisitDirectDeps(func(dep android.Module) {
  963. if dep.Name() == "api_imports" {
  964. apiImportInfo = ctx.OtherModuleProvider(dep, multitree.ApiImportsProvider).(multitree.ApiImportInfo)
  965. hasApiImportInfo = true
  966. }
  967. })
  968. if hasApiImportInfo {
  969. targetStubModuleList := map[string]string{}
  970. targetOrigModuleList := map[string]string{}
  971. // Search for dependency which both original module and API imported library with APEX stub exists
  972. ctx.VisitDirectDeps(func(dep android.Module) {
  973. depName := ctx.OtherModuleName(dep)
  974. if apiLibrary, ok := apiImportInfo.ApexSharedLibs[depName]; ok {
  975. targetStubModuleList[apiLibrary] = depName
  976. }
  977. })
  978. ctx.VisitDirectDeps(func(dep android.Module) {
  979. depName := ctx.OtherModuleName(dep)
  980. if origLibrary, ok := targetStubModuleList[depName]; ok {
  981. targetOrigModuleList[origLibrary] = depName
  982. }
  983. })
  984. // Decide which library should be used between original and API imported library
  985. ctx.VisitDirectDeps(func(dep android.Module) {
  986. depName := ctx.OtherModuleName(dep)
  987. if apiLibrary, ok := targetOrigModuleList[depName]; ok {
  988. if cc.ShouldUseStubForApex(ctx, dep) {
  989. skipModuleList[depName] = true
  990. } else {
  991. skipModuleList[apiLibrary] = true
  992. }
  993. }
  994. })
  995. }
  996. ctx.VisitDirectDeps(func(dep android.Module) {
  997. depName := ctx.OtherModuleName(dep)
  998. depTag := ctx.OtherModuleDependencyTag(dep)
  999. if _, exists := skipModuleList[depName]; exists {
  1000. return
  1001. }
  1002. if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
  1003. //Handle Rust Modules
  1004. makeLibName := rustMakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
  1005. switch depTag {
  1006. case dylibDepTag:
  1007. dylib, ok := rustDep.compiler.(libraryInterface)
  1008. if !ok || !dylib.dylib() {
  1009. ctx.ModuleErrorf("mod %q not an dylib library", depName)
  1010. return
  1011. }
  1012. directDylibDeps = append(directDylibDeps, rustDep)
  1013. mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
  1014. case rlibDepTag:
  1015. rlib, ok := rustDep.compiler.(libraryInterface)
  1016. if !ok || !rlib.rlib() {
  1017. ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
  1018. return
  1019. }
  1020. directRlibDeps = append(directRlibDeps, rustDep)
  1021. mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
  1022. case procMacroDepTag:
  1023. directProcMacroDeps = append(directProcMacroDeps, rustDep)
  1024. mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
  1025. }
  1026. if android.IsSourceDepTagWithOutputTag(depTag, "") {
  1027. // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
  1028. // OS/Arch variant is used.
  1029. var helper string
  1030. if ctx.Host() {
  1031. helper = "missing 'host_supported'?"
  1032. } else {
  1033. helper = "device module defined?"
  1034. }
  1035. if dep.Target().Os != ctx.Os() {
  1036. ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
  1037. return
  1038. } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
  1039. ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
  1040. return
  1041. }
  1042. directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
  1043. }
  1044. //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
  1045. if depTag != procMacroDepTag {
  1046. exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
  1047. depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
  1048. depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
  1049. depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
  1050. depPaths.LibDeps = append(depPaths.LibDeps, exportedInfo.LibDeps...)
  1051. }
  1052. if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
  1053. linkFile := rustDep.UnstrippedOutputFile()
  1054. linkDir := linkPathFromFilePath(linkFile)
  1055. if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
  1056. lib.exportLinkDirs(linkDir)
  1057. }
  1058. }
  1059. } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
  1060. //Handle C dependencies
  1061. makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
  1062. if _, ok := ccDep.(*Module); !ok {
  1063. if ccDep.Module().Target().Os != ctx.Os() {
  1064. ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
  1065. return
  1066. }
  1067. if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
  1068. ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
  1069. return
  1070. }
  1071. }
  1072. linkObject := ccDep.OutputFile()
  1073. linkPath := linkPathFromFilePath(linkObject.Path())
  1074. if !linkObject.Valid() {
  1075. ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
  1076. }
  1077. exportDep := false
  1078. switch {
  1079. case cc.IsStaticDepTag(depTag):
  1080. if cc.IsWholeStaticLib(depTag) {
  1081. // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
  1082. // if the library is not prefixed by "lib".
  1083. if mod.Binary() {
  1084. // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
  1085. // Since binaries don't need to 'rebundle' these like libraries and only use these for the
  1086. // final linkage, pass the args directly to the linker to handle these cases.
  1087. depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
  1088. } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
  1089. depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
  1090. depPaths.WholeStaticLibs = append(depPaths.WholeStaticLibs, linkObject.Path())
  1091. } else {
  1092. ctx.ModuleErrorf("'%q' cannot be listed as a whole_static_library in Rust modules unless the output is prefixed by 'lib'", depName, ctx.ModuleName())
  1093. }
  1094. }
  1095. // Add this to linkObjects to pass the library directly to the linker as well. This propagates
  1096. // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
  1097. depPaths.linkObjects = append(depPaths.linkObjects, linkObject.AsPaths()...)
  1098. depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
  1099. exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
  1100. depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
  1101. depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
  1102. depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
  1103. depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
  1104. directStaticLibDeps = append(directStaticLibDeps, ccDep)
  1105. // Record baseLibName for snapshots.
  1106. mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
  1107. mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
  1108. case cc.IsSharedDepTag(depTag):
  1109. // For the shared lib dependencies, we may link to the stub variant
  1110. // of the dependency depending on the context (e.g. if this
  1111. // dependency crosses the APEX boundaries).
  1112. sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
  1113. // Re-get linkObject as ChooseStubOrImpl actually tells us which
  1114. // object (either from stub or non-stub) to use.
  1115. linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
  1116. linkPath = linkPathFromFilePath(linkObject.Path())
  1117. depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
  1118. depPaths.linkObjects = append(depPaths.linkObjects, linkObject.AsPaths()...)
  1119. depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
  1120. depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
  1121. depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
  1122. depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
  1123. directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
  1124. // Record baseLibName for snapshots.
  1125. mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
  1126. mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, makeLibName)
  1127. exportDep = true
  1128. case cc.IsHeaderDepTag(depTag):
  1129. exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
  1130. depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
  1131. depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
  1132. depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
  1133. case depTag == cc.CrtBeginDepTag:
  1134. depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
  1135. case depTag == cc.CrtEndDepTag:
  1136. depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
  1137. }
  1138. // Make sure these dependencies are propagated
  1139. if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
  1140. lib.exportLinkDirs(linkPath)
  1141. if linkObject.Valid() {
  1142. lib.exportLinkObjects(linkObject.Path())
  1143. }
  1144. }
  1145. } else {
  1146. switch {
  1147. case depTag == cc.CrtBeginDepTag:
  1148. depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
  1149. case depTag == cc.CrtEndDepTag:
  1150. depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
  1151. }
  1152. }
  1153. if srcDep, ok := dep.(android.SourceFileProducer); ok {
  1154. if android.IsSourceDepTagWithOutputTag(depTag, "") {
  1155. // These are usually genrules which don't have per-target variants.
  1156. directSrcDeps = append(directSrcDeps, srcDep)
  1157. }
  1158. }
  1159. })
  1160. var rlibDepFiles RustLibraries
  1161. for _, dep := range directRlibDeps {
  1162. rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
  1163. }
  1164. var dylibDepFiles RustLibraries
  1165. for _, dep := range directDylibDeps {
  1166. dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
  1167. }
  1168. var procMacroDepFiles RustLibraries
  1169. for _, dep := range directProcMacroDeps {
  1170. procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
  1171. }
  1172. var libDepFiles android.Paths
  1173. for _, dep := range directStaticLibDeps {
  1174. libDepFiles = append(libDepFiles, dep.OutputFile().Path())
  1175. }
  1176. for _, dep := range directSharedLibDeps {
  1177. if dep.TableOfContents.Valid() {
  1178. libDepFiles = append(libDepFiles, dep.TableOfContents.Path())
  1179. } else {
  1180. libDepFiles = append(libDepFiles, dep.SharedLibrary)
  1181. }
  1182. }
  1183. var srcProviderDepFiles android.Paths
  1184. for _, dep := range directSrcProvidersDeps {
  1185. srcs, _ := dep.OutputFiles("")
  1186. srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
  1187. }
  1188. for _, dep := range directSrcDeps {
  1189. srcs := dep.Srcs()
  1190. srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
  1191. }
  1192. depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
  1193. depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
  1194. depPaths.LibDeps = append(depPaths.LibDeps, libDepFiles...)
  1195. depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
  1196. depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
  1197. // Dedup exported flags from dependencies
  1198. depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
  1199. depPaths.linkObjects = android.FirstUniquePaths(depPaths.linkObjects)
  1200. depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
  1201. depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
  1202. depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
  1203. depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
  1204. return depPaths
  1205. }
  1206. func (mod *Module) InstallInData() bool {
  1207. if mod.compiler == nil {
  1208. return false
  1209. }
  1210. return mod.compiler.inData()
  1211. }
  1212. func (mod *Module) InstallInRamdisk() bool {
  1213. return mod.InRamdisk()
  1214. }
  1215. func (mod *Module) InstallInVendorRamdisk() bool {
  1216. return mod.InVendorRamdisk()
  1217. }
  1218. func (mod *Module) InstallInRecovery() bool {
  1219. return mod.InRecovery()
  1220. }
  1221. func linkPathFromFilePath(filepath android.Path) string {
  1222. return strings.Split(filepath.String(), filepath.Base())[0]
  1223. }
  1224. // usePublicApi returns true if the rust variant should link against NDK (publicapi)
  1225. func (r *Module) usePublicApi() bool {
  1226. return r.Device() && r.UseSdk()
  1227. }
  1228. // useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
  1229. func (r *Module) useVendorApi() bool {
  1230. return r.Device() && (r.InVendor() || r.InProduct())
  1231. }
  1232. func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
  1233. ctx := &depsContext{
  1234. BottomUpMutatorContext: actx,
  1235. }
  1236. deps := mod.deps(ctx)
  1237. var commonDepVariations []blueprint.Variation
  1238. var snapshotInfo *cc.SnapshotInfo
  1239. apiImportInfo := cc.GetApiImports(mod, actx)
  1240. if mod.usePublicApi() || mod.useVendorApi() {
  1241. for idx, lib := range deps.SharedLibs {
  1242. deps.SharedLibs[idx] = cc.GetReplaceModuleName(lib, apiImportInfo.SharedLibs)
  1243. }
  1244. }
  1245. if ctx.Os() == android.Android {
  1246. deps.SharedLibs, _ = cc.RewriteLibs(mod, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
  1247. }
  1248. stdLinkage := "dylib-std"
  1249. if mod.compiler.stdLinkage(ctx) == RlibLinkage {
  1250. stdLinkage = "rlib-std"
  1251. }
  1252. rlibDepVariations := commonDepVariations
  1253. if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
  1254. rlibDepVariations = append(rlibDepVariations,
  1255. blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
  1256. }
  1257. // rlibs
  1258. rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
  1259. for _, lib := range deps.Rlibs {
  1260. depTag := rlibDepTag
  1261. lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
  1262. actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
  1263. }
  1264. // dylibs
  1265. actx.AddVariationDependencies(
  1266. append(commonDepVariations, []blueprint.Variation{
  1267. {Mutator: "rust_libraries", Variation: dylibVariation}}...),
  1268. dylibDepTag, deps.Dylibs...)
  1269. // rustlibs
  1270. if deps.Rustlibs != nil && !mod.compiler.Disabled() {
  1271. autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
  1272. for _, lib := range deps.Rustlibs {
  1273. if autoDep.depTag == rlibDepTag {
  1274. // Handle the rlib deptag case
  1275. addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
  1276. } else {
  1277. // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
  1278. // Check for the existence of the dylib deptag variant. Select it if available,
  1279. // otherwise select the rlib variant.
  1280. autoDepVariations := append(commonDepVariations,
  1281. blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
  1282. if actx.OtherModuleDependencyVariantExists(autoDepVariations, lib) {
  1283. actx.AddVariationDependencies(autoDepVariations, autoDep.depTag, lib)
  1284. } else {
  1285. // If there's no dylib dependency available, try to add the rlib dependency instead.
  1286. addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
  1287. }
  1288. }
  1289. }
  1290. }
  1291. // stdlibs
  1292. if deps.Stdlibs != nil {
  1293. if mod.compiler.stdLinkage(ctx) == RlibLinkage {
  1294. for _, lib := range deps.Stdlibs {
  1295. depTag := rlibDepTag
  1296. lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
  1297. actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
  1298. depTag, lib)
  1299. }
  1300. } else {
  1301. actx.AddVariationDependencies(
  1302. append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
  1303. dylibDepTag, deps.Stdlibs...)
  1304. }
  1305. }
  1306. for _, lib := range deps.SharedLibs {
  1307. depTag := cc.SharedDepTag()
  1308. name, version := cc.StubsLibNameAndVersion(lib)
  1309. variations := []blueprint.Variation{
  1310. {Mutator: "link", Variation: "shared"},
  1311. }
  1312. // For core variant, add a dep on the implementation (if it exists) and its .apiimport (if it exists)
  1313. // GenerateAndroidBuildActions will pick the correct impl/stub based on the api_domain boundary
  1314. if _, ok := apiImportInfo.ApexSharedLibs[name]; !ok || ctx.OtherModuleExists(name) {
  1315. cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
  1316. }
  1317. if apiLibraryName, ok := apiImportInfo.ApexSharedLibs[name]; ok {
  1318. cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, apiLibraryName, version, false)
  1319. }
  1320. }
  1321. for _, lib := range deps.WholeStaticLibs {
  1322. depTag := cc.StaticDepTag(true)
  1323. lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
  1324. actx.AddVariationDependencies([]blueprint.Variation{
  1325. {Mutator: "link", Variation: "static"},
  1326. }, depTag, lib)
  1327. }
  1328. for _, lib := range deps.StaticLibs {
  1329. depTag := cc.StaticDepTag(false)
  1330. lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
  1331. actx.AddVariationDependencies([]blueprint.Variation{
  1332. {Mutator: "link", Variation: "static"},
  1333. }, depTag, lib)
  1334. }
  1335. actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
  1336. crtVariations := cc.GetCrtVariations(ctx, mod)
  1337. for _, crt := range deps.CrtBegin {
  1338. actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag,
  1339. cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
  1340. }
  1341. for _, crt := range deps.CrtEnd {
  1342. actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag,
  1343. cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
  1344. }
  1345. if mod.sourceProvider != nil {
  1346. if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
  1347. bindgen.Properties.Custom_bindgen != "" {
  1348. actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
  1349. bindgen.Properties.Custom_bindgen)
  1350. }
  1351. }
  1352. actx.AddVariationDependencies([]blueprint.Variation{
  1353. {Mutator: "link", Variation: "shared"},
  1354. }, dataLibDepTag, deps.DataLibs...)
  1355. actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
  1356. // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
  1357. actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
  1358. mod.afdo.addDep(ctx, actx)
  1359. }
  1360. // addRlibDependency will add an rlib dependency, rewriting to the snapshot library if available.
  1361. func addRlibDependency(actx android.BottomUpMutatorContext, lib string, mod *Module, snapshotInfo **cc.SnapshotInfo, variations []blueprint.Variation) {
  1362. lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, snapshotInfo, actx).Rlibs)
  1363. actx.AddVariationDependencies(variations, rlibDepTag, lib)
  1364. }
  1365. func BeginMutator(ctx android.BottomUpMutatorContext) {
  1366. if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
  1367. mod.beginMutator(ctx)
  1368. }
  1369. }
  1370. func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
  1371. ctx := &baseModuleContext{
  1372. BaseModuleContext: actx,
  1373. }
  1374. mod.begin(ctx)
  1375. }
  1376. func (mod *Module) Name() string {
  1377. name := mod.ModuleBase.Name()
  1378. if p, ok := mod.compiler.(interface {
  1379. Name(string) string
  1380. }); ok {
  1381. name = p.Name(name)
  1382. }
  1383. return name
  1384. }
  1385. func (mod *Module) disableClippy() {
  1386. if mod.clippy != nil {
  1387. mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
  1388. }
  1389. }
  1390. var _ android.HostToolProvider = (*Module)(nil)
  1391. var _ snapshot.RelativeInstallPath = (*Module)(nil)
  1392. func (mod *Module) HostToolPath() android.OptionalPath {
  1393. if !mod.Host() {
  1394. return android.OptionalPath{}
  1395. }
  1396. if binary, ok := mod.compiler.(*binaryDecorator); ok {
  1397. return android.OptionalPathForPath(binary.baseCompiler.path)
  1398. } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
  1399. // Even though proc-macros aren't strictly "tools", since they target the compiler
  1400. // and act as compiler plugins, we treat them similarly.
  1401. return android.OptionalPathForPath(pm.baseCompiler.path)
  1402. }
  1403. return android.OptionalPath{}
  1404. }
  1405. var _ android.ApexModule = (*Module)(nil)
  1406. func (mod *Module) MinSdkVersion() string {
  1407. return String(mod.Properties.Min_sdk_version)
  1408. }
  1409. // Implements android.ApexModule
  1410. func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
  1411. minSdkVersion := mod.MinSdkVersion()
  1412. if minSdkVersion == "apex_inherit" {
  1413. return nil
  1414. }
  1415. if minSdkVersion == "" {
  1416. return fmt.Errorf("min_sdk_version is not specificed")
  1417. }
  1418. // Not using nativeApiLevelFromUser because the context here is not
  1419. // necessarily a native context.
  1420. ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
  1421. if err != nil {
  1422. return err
  1423. }
  1424. if ver.GreaterThan(sdkVersion) {
  1425. return fmt.Errorf("newer SDK(%v)", ver)
  1426. }
  1427. return nil
  1428. }
  1429. // Implements android.ApexModule
  1430. func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
  1431. depTag := ctx.OtherModuleDependencyTag(dep)
  1432. if ccm, ok := dep.(*cc.Module); ok {
  1433. if ccm.HasStubsVariants() {
  1434. if cc.IsSharedDepTag(depTag) {
  1435. // dynamic dep to a stubs lib crosses APEX boundary
  1436. return false
  1437. }
  1438. if cc.IsRuntimeDepTag(depTag) {
  1439. // runtime dep to a stubs lib also crosses APEX boundary
  1440. return false
  1441. }
  1442. if cc.IsHeaderDepTag(depTag) {
  1443. return false
  1444. }
  1445. }
  1446. if mod.Static() && cc.IsSharedDepTag(depTag) {
  1447. // shared_lib dependency from a static lib is considered as crossing
  1448. // the APEX boundary because the dependency doesn't actually is
  1449. // linked; the dependency is used only during the compilation phase.
  1450. return false
  1451. }
  1452. }
  1453. if depTag == procMacroDepTag || depTag == customBindgenDepTag {
  1454. return false
  1455. }
  1456. return true
  1457. }
  1458. // Overrides ApexModule.IsInstallabeToApex()
  1459. func (mod *Module) IsInstallableToApex() bool {
  1460. if mod.compiler != nil {
  1461. if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
  1462. return true
  1463. }
  1464. if _, ok := mod.compiler.(*binaryDecorator); ok {
  1465. return true
  1466. }
  1467. }
  1468. return false
  1469. }
  1470. // If a library file has a "lib" prefix, extract the library name without the prefix.
  1471. func libNameFromFilePath(filepath android.Path) (string, bool) {
  1472. libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
  1473. if strings.HasPrefix(libName, "lib") {
  1474. libName = libName[3:]
  1475. return libName, true
  1476. }
  1477. return "", false
  1478. }
  1479. func kytheExtractRustFactory() android.Singleton {
  1480. return &kytheExtractRustSingleton{}
  1481. }
  1482. type kytheExtractRustSingleton struct {
  1483. }
  1484. func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
  1485. var xrefTargets android.Paths
  1486. ctx.VisitAllModules(func(module android.Module) {
  1487. if rustModule, ok := module.(xref); ok {
  1488. xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
  1489. }
  1490. })
  1491. if len(xrefTargets) > 0 {
  1492. ctx.Phony("xref_rust", xrefTargets...)
  1493. }
  1494. }
  1495. func (c *Module) Partition() string {
  1496. return ""
  1497. }
  1498. var Bool = proptools.Bool
  1499. var BoolDefault = proptools.BoolDefault
  1500. var String = proptools.String
  1501. var StringPtr = proptools.StringPtr
  1502. var _ android.OutputFileProducer = (*Module)(nil)