rust.go 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  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. "fmt"
  17. "strings"
  18. "github.com/google/blueprint"
  19. "github.com/google/blueprint/proptools"
  20. "android/soong/android"
  21. "android/soong/cc"
  22. "android/soong/rust/config"
  23. )
  24. var pctx = android.NewPackageContext("android/soong/rust")
  25. func init() {
  26. // Only allow rust modules to be defined for certain projects
  27. android.AddNeverAllowRules(
  28. android.NeverAllow().
  29. NotIn(config.RustAllowedPaths...).
  30. ModuleType(config.RustModuleTypes...))
  31. android.RegisterModuleType("rust_defaults", defaultsFactory)
  32. android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
  33. ctx.BottomUp("rust_libraries", LibraryMutator).Parallel()
  34. ctx.BottomUp("rust_begin", BeginMutator).Parallel()
  35. })
  36. pctx.Import("android/soong/rust/config")
  37. pctx.ImportAs("ccConfig", "android/soong/cc/config")
  38. }
  39. type Flags struct {
  40. GlobalRustFlags []string // Flags that apply globally to rust
  41. GlobalLinkFlags []string // Flags that apply globally to linker
  42. RustFlags []string // Flags that apply to rust
  43. LinkFlags []string // Flags that apply to linker
  44. ClippyFlags []string // Flags that apply to clippy-driver, during the linting
  45. Toolchain config.Toolchain
  46. Coverage bool
  47. Clippy bool
  48. }
  49. type BaseProperties struct {
  50. AndroidMkRlibs []string
  51. AndroidMkDylibs []string
  52. AndroidMkProcMacroLibs []string
  53. AndroidMkSharedLibs []string
  54. AndroidMkStaticLibs []string
  55. SubName string `blueprint:"mutated"`
  56. PreventInstall bool
  57. HideFromMake bool
  58. }
  59. type Module struct {
  60. android.ModuleBase
  61. android.DefaultableModuleBase
  62. Properties BaseProperties
  63. hod android.HostOrDeviceSupported
  64. multilib android.Multilib
  65. compiler compiler
  66. coverage *coverage
  67. clippy *clippy
  68. cachedToolchain config.Toolchain
  69. sourceProvider SourceProvider
  70. subAndroidMkOnce map[SubAndroidMkProvider]bool
  71. outputFile android.OptionalPath
  72. generatedFile android.OptionalPath
  73. }
  74. func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
  75. switch tag {
  76. case "":
  77. if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
  78. return mod.sourceProvider.Srcs(), nil
  79. } else {
  80. if mod.outputFile.Valid() {
  81. return android.Paths{mod.outputFile.Path()}, nil
  82. }
  83. return android.Paths{}, nil
  84. }
  85. default:
  86. return nil, fmt.Errorf("unsupported module reference tag %q", tag)
  87. }
  88. }
  89. var _ android.ImageInterface = (*Module)(nil)
  90. func (mod *Module) ImageMutatorBegin(ctx android.BaseModuleContext) {}
  91. func (mod *Module) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
  92. return true
  93. }
  94. func (mod *Module) RamdiskVariantNeeded(android.BaseModuleContext) bool {
  95. return mod.InRamdisk()
  96. }
  97. func (mod *Module) RecoveryVariantNeeded(android.BaseModuleContext) bool {
  98. return mod.InRecovery()
  99. }
  100. func (mod *Module) ExtraImageVariations(android.BaseModuleContext) []string {
  101. return nil
  102. }
  103. func (c *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) {
  104. }
  105. func (mod *Module) BuildStubs() bool {
  106. return false
  107. }
  108. func (mod *Module) HasStubsVariants() bool {
  109. return false
  110. }
  111. func (mod *Module) SelectedStl() string {
  112. return ""
  113. }
  114. func (mod *Module) NonCcVariants() bool {
  115. if mod.compiler != nil {
  116. if _, ok := mod.compiler.(libraryInterface); ok {
  117. return false
  118. }
  119. }
  120. panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
  121. }
  122. func (mod *Module) ApiLevel() string {
  123. panic(fmt.Errorf("Called ApiLevel on Rust module %q; stubs libraries are not yet supported.", mod.BaseModuleName()))
  124. }
  125. func (mod *Module) Static() bool {
  126. if mod.compiler != nil {
  127. if library, ok := mod.compiler.(libraryInterface); ok {
  128. return library.static()
  129. }
  130. }
  131. return false
  132. }
  133. func (mod *Module) Shared() bool {
  134. if mod.compiler != nil {
  135. if library, ok := mod.compiler.(libraryInterface); ok {
  136. return library.shared()
  137. }
  138. }
  139. return false
  140. }
  141. func (mod *Module) Toc() android.OptionalPath {
  142. if mod.compiler != nil {
  143. if _, ok := mod.compiler.(libraryInterface); ok {
  144. return android.OptionalPath{}
  145. }
  146. }
  147. panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
  148. }
  149. func (mod *Module) OnlyInRamdisk() bool {
  150. return false
  151. }
  152. func (mod *Module) OnlyInRecovery() bool {
  153. return false
  154. }
  155. func (mod *Module) UseSdk() bool {
  156. return false
  157. }
  158. func (mod *Module) UseVndk() bool {
  159. return false
  160. }
  161. func (mod *Module) MustUseVendorVariant() bool {
  162. return false
  163. }
  164. func (mod *Module) IsVndk() bool {
  165. return false
  166. }
  167. func (mod *Module) HasVendorVariant() bool {
  168. return false
  169. }
  170. func (mod *Module) SdkVersion() string {
  171. return ""
  172. }
  173. func (mod *Module) AlwaysSdk() bool {
  174. return false
  175. }
  176. func (mod *Module) IsSdkVariant() bool {
  177. return false
  178. }
  179. func (mod *Module) ToolchainLibrary() bool {
  180. return false
  181. }
  182. func (mod *Module) NdkPrebuiltStl() bool {
  183. return false
  184. }
  185. func (mod *Module) StubDecorator() bool {
  186. return false
  187. }
  188. type Deps struct {
  189. Dylibs []string
  190. Rlibs []string
  191. Rustlibs []string
  192. ProcMacros []string
  193. SharedLibs []string
  194. StaticLibs []string
  195. CrtBegin, CrtEnd string
  196. }
  197. type PathDeps struct {
  198. DyLibs RustLibraries
  199. RLibs RustLibraries
  200. SharedLibs android.Paths
  201. StaticLibs android.Paths
  202. ProcMacros RustLibraries
  203. linkDirs []string
  204. depFlags []string
  205. //ReexportedDeps android.Paths
  206. // Used by bindgen modules which call clang
  207. depClangFlags []string
  208. depIncludePaths android.Paths
  209. depSystemIncludePaths android.Paths
  210. coverageFiles android.Paths
  211. CrtBegin android.OptionalPath
  212. CrtEnd android.OptionalPath
  213. // Paths to generated source files
  214. SrcDeps android.Paths
  215. }
  216. type RustLibraries []RustLibrary
  217. type RustLibrary struct {
  218. Path android.Path
  219. CrateName string
  220. }
  221. type compiler interface {
  222. compilerFlags(ctx ModuleContext, flags Flags) Flags
  223. compilerProps() []interface{}
  224. compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
  225. compilerDeps(ctx DepsContext, deps Deps) Deps
  226. crateName() string
  227. inData() bool
  228. install(ctx ModuleContext, path android.Path)
  229. relativeInstallPath() string
  230. nativeCoverage() bool
  231. Disabled() bool
  232. SetDisabled()
  233. }
  234. type exportedFlagsProducer interface {
  235. exportedLinkDirs() []string
  236. exportedDepFlags() []string
  237. exportLinkDirs(...string)
  238. exportDepFlags(...string)
  239. }
  240. type flagExporter struct {
  241. depFlags []string
  242. linkDirs []string
  243. }
  244. func (flagExporter *flagExporter) exportedLinkDirs() []string {
  245. return flagExporter.linkDirs
  246. }
  247. func (flagExporter *flagExporter) exportedDepFlags() []string {
  248. return flagExporter.depFlags
  249. }
  250. func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
  251. flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
  252. }
  253. func (flagExporter *flagExporter) exportDepFlags(flags ...string) {
  254. flagExporter.depFlags = android.FirstUniqueStrings(append(flagExporter.depFlags, flags...))
  255. }
  256. var _ exportedFlagsProducer = (*flagExporter)(nil)
  257. func NewFlagExporter() *flagExporter {
  258. return &flagExporter{
  259. depFlags: []string{},
  260. linkDirs: []string{},
  261. }
  262. }
  263. func (mod *Module) isCoverageVariant() bool {
  264. return mod.coverage.Properties.IsCoverageVariant
  265. }
  266. var _ cc.Coverage = (*Module)(nil)
  267. func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
  268. return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
  269. }
  270. func (mod *Module) PreventInstall() {
  271. mod.Properties.PreventInstall = true
  272. }
  273. func (mod *Module) HideFromMake() {
  274. mod.Properties.HideFromMake = true
  275. }
  276. func (mod *Module) MarkAsCoverageVariant(coverage bool) {
  277. mod.coverage.Properties.IsCoverageVariant = coverage
  278. }
  279. func (mod *Module) EnableCoverageIfNeeded() {
  280. mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
  281. }
  282. func defaultsFactory() android.Module {
  283. return DefaultsFactory()
  284. }
  285. type Defaults struct {
  286. android.ModuleBase
  287. android.DefaultsModuleBase
  288. }
  289. func DefaultsFactory(props ...interface{}) android.Module {
  290. module := &Defaults{}
  291. module.AddProperties(props...)
  292. module.AddProperties(
  293. &BaseProperties{},
  294. &BaseCompilerProperties{},
  295. &BinaryCompilerProperties{},
  296. &LibraryCompilerProperties{},
  297. &ProcMacroCompilerProperties{},
  298. &PrebuiltProperties{},
  299. &SourceProviderProperties{},
  300. &TestProperties{},
  301. &cc.CoverageProperties{},
  302. &ClippyProperties{},
  303. )
  304. android.InitDefaultsModule(module)
  305. return module
  306. }
  307. func (mod *Module) CrateName() string {
  308. return mod.compiler.crateName()
  309. }
  310. func (mod *Module) CcLibrary() bool {
  311. if mod.compiler != nil {
  312. if _, ok := mod.compiler.(*libraryDecorator); ok {
  313. return true
  314. }
  315. }
  316. return false
  317. }
  318. func (mod *Module) CcLibraryInterface() bool {
  319. if mod.compiler != nil {
  320. // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
  321. // VariantIs{Static,Shared} is set.
  322. if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
  323. return true
  324. }
  325. }
  326. return false
  327. }
  328. func (mod *Module) IncludeDirs() android.Paths {
  329. if mod.compiler != nil {
  330. if library, ok := mod.compiler.(*libraryDecorator); ok {
  331. return library.includeDirs
  332. }
  333. }
  334. panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
  335. }
  336. func (mod *Module) SetStatic() {
  337. if mod.compiler != nil {
  338. if library, ok := mod.compiler.(libraryInterface); ok {
  339. library.setStatic()
  340. return
  341. }
  342. }
  343. panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
  344. }
  345. func (mod *Module) SetShared() {
  346. if mod.compiler != nil {
  347. if library, ok := mod.compiler.(libraryInterface); ok {
  348. library.setShared()
  349. return
  350. }
  351. }
  352. panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
  353. }
  354. func (mod *Module) SetBuildStubs() {
  355. panic("SetBuildStubs not yet implemented for rust modules")
  356. }
  357. func (mod *Module) SetStubsVersions(string) {
  358. panic("SetStubsVersions not yet implemented for rust modules")
  359. }
  360. func (mod *Module) StubsVersion() string {
  361. panic("SetStubsVersions not yet implemented for rust modules")
  362. }
  363. func (mod *Module) BuildStaticVariant() bool {
  364. if mod.compiler != nil {
  365. if library, ok := mod.compiler.(libraryInterface); ok {
  366. return library.buildStatic()
  367. }
  368. }
  369. panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
  370. }
  371. func (mod *Module) BuildSharedVariant() bool {
  372. if mod.compiler != nil {
  373. if library, ok := mod.compiler.(libraryInterface); ok {
  374. return library.buildShared()
  375. }
  376. }
  377. panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
  378. }
  379. // Rust module deps don't have a link order (?)
  380. func (mod *Module) SetDepsInLinkOrder([]android.Path) {}
  381. func (mod *Module) GetDepsInLinkOrder() []android.Path {
  382. return []android.Path{}
  383. }
  384. func (mod *Module) GetStaticVariant() cc.LinkableInterface {
  385. return nil
  386. }
  387. func (mod *Module) Module() android.Module {
  388. return mod
  389. }
  390. func (mod *Module) StubsVersions() []string {
  391. // For now, Rust has no stubs versions.
  392. if mod.compiler != nil {
  393. if _, ok := mod.compiler.(libraryInterface); ok {
  394. return []string{}
  395. }
  396. }
  397. panic(fmt.Errorf("StubsVersions called on non-library module: %q", mod.BaseModuleName()))
  398. }
  399. func (mod *Module) OutputFile() android.OptionalPath {
  400. return mod.outputFile
  401. }
  402. func (mod *Module) InRecovery() bool {
  403. // For now, Rust has no notion of the recovery image
  404. return false
  405. }
  406. func (mod *Module) HasStaticVariant() bool {
  407. if mod.GetStaticVariant() != nil {
  408. return true
  409. }
  410. return false
  411. }
  412. func (mod *Module) CoverageFiles() android.Paths {
  413. if mod.compiler != nil {
  414. if !mod.compiler.nativeCoverage() {
  415. return android.Paths{}
  416. }
  417. if library, ok := mod.compiler.(*libraryDecorator); ok {
  418. if library.coverageFile != nil {
  419. return android.Paths{library.coverageFile}
  420. }
  421. return android.Paths{}
  422. }
  423. }
  424. panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
  425. }
  426. var _ cc.LinkableInterface = (*Module)(nil)
  427. func (mod *Module) Init() android.Module {
  428. mod.AddProperties(&mod.Properties)
  429. if mod.compiler != nil {
  430. mod.AddProperties(mod.compiler.compilerProps()...)
  431. }
  432. if mod.coverage != nil {
  433. mod.AddProperties(mod.coverage.props()...)
  434. }
  435. if mod.clippy != nil {
  436. mod.AddProperties(mod.clippy.props()...)
  437. }
  438. if mod.sourceProvider != nil {
  439. mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
  440. }
  441. android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
  442. android.InitDefaultableModule(mod)
  443. // Explicitly disable unsupported targets.
  444. android.AddLoadHook(mod, func(ctx android.LoadHookContext) {
  445. disableTargets := struct {
  446. Target struct {
  447. Linux_bionic struct {
  448. Enabled *bool
  449. }
  450. }
  451. }{}
  452. disableTargets.Target.Linux_bionic.Enabled = proptools.BoolPtr(false)
  453. ctx.AppendProperties(&disableTargets)
  454. })
  455. return mod
  456. }
  457. func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
  458. return &Module{
  459. hod: hod,
  460. multilib: multilib,
  461. }
  462. }
  463. func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
  464. module := newBaseModule(hod, multilib)
  465. module.coverage = &coverage{}
  466. module.clippy = &clippy{}
  467. return module
  468. }
  469. type ModuleContext interface {
  470. android.ModuleContext
  471. ModuleContextIntf
  472. }
  473. type BaseModuleContext interface {
  474. android.BaseModuleContext
  475. ModuleContextIntf
  476. }
  477. type DepsContext interface {
  478. android.BottomUpMutatorContext
  479. ModuleContextIntf
  480. }
  481. type ModuleContextIntf interface {
  482. RustModule() *Module
  483. toolchain() config.Toolchain
  484. }
  485. type depsContext struct {
  486. android.BottomUpMutatorContext
  487. }
  488. type moduleContext struct {
  489. android.ModuleContext
  490. }
  491. type baseModuleContext struct {
  492. android.BaseModuleContext
  493. }
  494. func (ctx *moduleContext) RustModule() *Module {
  495. return ctx.Module().(*Module)
  496. }
  497. func (ctx *moduleContext) toolchain() config.Toolchain {
  498. return ctx.RustModule().toolchain(ctx)
  499. }
  500. func (ctx *depsContext) RustModule() *Module {
  501. return ctx.Module().(*Module)
  502. }
  503. func (ctx *depsContext) toolchain() config.Toolchain {
  504. return ctx.RustModule().toolchain(ctx)
  505. }
  506. func (ctx *baseModuleContext) RustModule() *Module {
  507. return ctx.Module().(*Module)
  508. }
  509. func (ctx *baseModuleContext) toolchain() config.Toolchain {
  510. return ctx.RustModule().toolchain(ctx)
  511. }
  512. func (mod *Module) nativeCoverage() bool {
  513. return mod.compiler != nil && mod.compiler.nativeCoverage()
  514. }
  515. func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
  516. if mod.cachedToolchain == nil {
  517. mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
  518. }
  519. return mod.cachedToolchain
  520. }
  521. func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  522. }
  523. func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
  524. ctx := &moduleContext{
  525. ModuleContext: actx,
  526. }
  527. toolchain := mod.toolchain(ctx)
  528. if !toolchain.Supported() {
  529. // This toolchain's unsupported, there's nothing to do for this mod.
  530. return
  531. }
  532. deps := mod.depsToPaths(ctx)
  533. flags := Flags{
  534. Toolchain: toolchain,
  535. }
  536. if mod.compiler != nil {
  537. flags = mod.compiler.compilerFlags(ctx, flags)
  538. }
  539. if mod.coverage != nil {
  540. flags, deps = mod.coverage.flags(ctx, flags, deps)
  541. }
  542. if mod.clippy != nil {
  543. flags, deps = mod.clippy.flags(ctx, flags, deps)
  544. }
  545. // SourceProvider needs to call GenerateSource() before compiler calls compile() so it can provide the source.
  546. // TODO(b/162588681) This shouldn't have to run for every variant.
  547. if mod.sourceProvider != nil {
  548. generatedFile := mod.sourceProvider.GenerateSource(ctx, deps)
  549. mod.generatedFile = android.OptionalPathForPath(generatedFile)
  550. mod.sourceProvider.setSubName(ctx.ModuleSubDir())
  551. }
  552. if mod.compiler != nil && !mod.compiler.Disabled() {
  553. outputFile := mod.compiler.compile(ctx, flags, deps)
  554. mod.outputFile = android.OptionalPathForPath(outputFile)
  555. if mod.outputFile.Valid() && !mod.Properties.PreventInstall {
  556. mod.compiler.install(ctx, mod.outputFile.Path())
  557. }
  558. }
  559. }
  560. func (mod *Module) deps(ctx DepsContext) Deps {
  561. deps := Deps{}
  562. if mod.compiler != nil {
  563. deps = mod.compiler.compilerDeps(ctx, deps)
  564. }
  565. if mod.sourceProvider != nil {
  566. deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
  567. }
  568. if mod.coverage != nil {
  569. deps = mod.coverage.deps(ctx, deps)
  570. }
  571. deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
  572. deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
  573. deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
  574. deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
  575. deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
  576. deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
  577. return deps
  578. }
  579. type dependencyTag struct {
  580. blueprint.BaseDependencyTag
  581. name string
  582. library bool
  583. proc_macro bool
  584. }
  585. var (
  586. customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
  587. rlibDepTag = dependencyTag{name: "rlibTag", library: true}
  588. dylibDepTag = dependencyTag{name: "dylib", library: true}
  589. procMacroDepTag = dependencyTag{name: "procMacro", proc_macro: true}
  590. testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
  591. )
  592. type autoDep struct {
  593. variation string
  594. depTag dependencyTag
  595. }
  596. var (
  597. rlibAutoDep = autoDep{variation: "rlib", depTag: rlibDepTag}
  598. dylibAutoDep = autoDep{variation: "dylib", depTag: dylibDepTag}
  599. )
  600. type autoDeppable interface {
  601. autoDep() autoDep
  602. }
  603. func (mod *Module) begin(ctx BaseModuleContext) {
  604. if mod.coverage != nil {
  605. mod.coverage.begin(ctx)
  606. }
  607. }
  608. func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
  609. var depPaths PathDeps
  610. directRlibDeps := []*Module{}
  611. directDylibDeps := []*Module{}
  612. directProcMacroDeps := []*Module{}
  613. directSharedLibDeps := [](cc.LinkableInterface){}
  614. directStaticLibDeps := [](cc.LinkableInterface){}
  615. directSrcProvidersDeps := []*Module{}
  616. directSrcDeps := [](android.SourceFileProducer){}
  617. ctx.VisitDirectDeps(func(dep android.Module) {
  618. depName := ctx.OtherModuleName(dep)
  619. depTag := ctx.OtherModuleDependencyTag(dep)
  620. if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
  621. //Handle Rust Modules
  622. switch depTag {
  623. case dylibDepTag:
  624. dylib, ok := rustDep.compiler.(libraryInterface)
  625. if !ok || !dylib.dylib() {
  626. ctx.ModuleErrorf("mod %q not an dylib library", depName)
  627. return
  628. }
  629. directDylibDeps = append(directDylibDeps, rustDep)
  630. mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
  631. case rlibDepTag:
  632. rlib, ok := rustDep.compiler.(libraryInterface)
  633. if !ok || !rlib.rlib() {
  634. ctx.ModuleErrorf("mod %q not an rlib library", depName)
  635. return
  636. }
  637. depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
  638. directRlibDeps = append(directRlibDeps, rustDep)
  639. mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName)
  640. case procMacroDepTag:
  641. directProcMacroDeps = append(directProcMacroDeps, rustDep)
  642. mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
  643. case android.SourceDepTag:
  644. // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
  645. // OS/Arch variant is used.
  646. var helper string
  647. if ctx.Host() {
  648. helper = "missing 'host_supported'?"
  649. } else {
  650. helper = "device module defined?"
  651. }
  652. if dep.Target().Os != ctx.Os() {
  653. ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
  654. return
  655. } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
  656. ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
  657. return
  658. }
  659. directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
  660. }
  661. //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
  662. if lib, ok := rustDep.compiler.(exportedFlagsProducer); ok && depTag != procMacroDepTag {
  663. depPaths.linkDirs = append(depPaths.linkDirs, lib.exportedLinkDirs()...)
  664. depPaths.depFlags = append(depPaths.depFlags, lib.exportedDepFlags()...)
  665. }
  666. if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
  667. linkFile := rustDep.outputFile
  668. if !linkFile.Valid() {
  669. ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
  670. depName, ctx.ModuleName())
  671. return
  672. }
  673. linkDir := linkPathFromFilePath(linkFile.Path())
  674. if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
  675. lib.exportLinkDirs(linkDir)
  676. }
  677. }
  678. } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
  679. //Handle C dependencies
  680. if _, ok := ccDep.(*Module); !ok {
  681. if ccDep.Module().Target().Os != ctx.Os() {
  682. ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
  683. return
  684. }
  685. if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
  686. ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
  687. return
  688. }
  689. }
  690. linkFile := ccDep.OutputFile()
  691. linkPath := linkPathFromFilePath(linkFile.Path())
  692. libName := libNameFromFilePath(linkFile.Path())
  693. depFlag := "-l" + libName
  694. if !linkFile.Valid() {
  695. ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
  696. }
  697. exportDep := false
  698. switch {
  699. case cc.IsStaticDepTag(depTag):
  700. depFlag = "-lstatic=" + libName
  701. depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
  702. depPaths.depFlags = append(depPaths.depFlags, depFlag)
  703. depPaths.depIncludePaths = append(depPaths.depIncludePaths, ccDep.IncludeDirs()...)
  704. if mod, ok := ccDep.(*cc.Module); ok {
  705. depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, mod.ExportedSystemIncludeDirs()...)
  706. depPaths.depClangFlags = append(depPaths.depClangFlags, mod.ExportedFlags()...)
  707. }
  708. depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
  709. directStaticLibDeps = append(directStaticLibDeps, ccDep)
  710. mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
  711. case cc.IsSharedDepTag(depTag):
  712. depFlag = "-ldylib=" + libName
  713. depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
  714. depPaths.depFlags = append(depPaths.depFlags, depFlag)
  715. depPaths.depIncludePaths = append(depPaths.depIncludePaths, ccDep.IncludeDirs()...)
  716. if mod, ok := ccDep.(*cc.Module); ok {
  717. depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, mod.ExportedSystemIncludeDirs()...)
  718. depPaths.depClangFlags = append(depPaths.depClangFlags, mod.ExportedFlags()...)
  719. }
  720. directSharedLibDeps = append(directSharedLibDeps, ccDep)
  721. mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
  722. exportDep = true
  723. case depTag == cc.CrtBeginDepTag:
  724. depPaths.CrtBegin = linkFile
  725. case depTag == cc.CrtEndDepTag:
  726. depPaths.CrtEnd = linkFile
  727. }
  728. // Make sure these dependencies are propagated
  729. if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
  730. lib.exportLinkDirs(linkPath)
  731. lib.exportDepFlags(depFlag)
  732. }
  733. }
  734. if srcDep, ok := dep.(android.SourceFileProducer); ok {
  735. switch depTag {
  736. case android.SourceDepTag:
  737. // These are usually genrules which don't have per-target variants.
  738. directSrcDeps = append(directSrcDeps, srcDep)
  739. }
  740. }
  741. })
  742. var rlibDepFiles RustLibraries
  743. for _, dep := range directRlibDeps {
  744. rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
  745. }
  746. var dylibDepFiles RustLibraries
  747. for _, dep := range directDylibDeps {
  748. dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
  749. }
  750. var procMacroDepFiles RustLibraries
  751. for _, dep := range directProcMacroDeps {
  752. procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
  753. }
  754. var staticLibDepFiles android.Paths
  755. for _, dep := range directStaticLibDeps {
  756. staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
  757. }
  758. var sharedLibDepFiles android.Paths
  759. for _, dep := range directSharedLibDeps {
  760. sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
  761. }
  762. var srcProviderDepFiles android.Paths
  763. for _, dep := range directSrcProvidersDeps {
  764. srcs, _ := dep.OutputFiles("")
  765. srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
  766. }
  767. for _, dep := range directSrcDeps {
  768. srcs := dep.Srcs()
  769. srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
  770. }
  771. depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
  772. depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
  773. depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
  774. depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
  775. depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
  776. depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
  777. // Dedup exported flags from dependencies
  778. depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
  779. depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
  780. depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
  781. depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
  782. depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
  783. return depPaths
  784. }
  785. func (mod *Module) InstallInData() bool {
  786. if mod.compiler == nil {
  787. return false
  788. }
  789. return mod.compiler.inData()
  790. }
  791. func linkPathFromFilePath(filepath android.Path) string {
  792. return strings.Split(filepath.String(), filepath.Base())[0]
  793. }
  794. func libNameFromFilePath(filepath android.Path) string {
  795. libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
  796. if strings.HasPrefix(libName, "lib") {
  797. libName = libName[3:]
  798. }
  799. return libName
  800. }
  801. func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
  802. ctx := &depsContext{
  803. BottomUpMutatorContext: actx,
  804. }
  805. deps := mod.deps(ctx)
  806. commonDepVariations := []blueprint.Variation{}
  807. if cc.VersionVariantAvailable(mod) {
  808. commonDepVariations = append(commonDepVariations,
  809. blueprint.Variation{Mutator: "version", Variation: ""})
  810. }
  811. if !mod.Host() {
  812. commonDepVariations = append(commonDepVariations,
  813. blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
  814. }
  815. actx.AddVariationDependencies(
  816. append(commonDepVariations, []blueprint.Variation{
  817. {Mutator: "rust_libraries", Variation: "rlib"}}...),
  818. rlibDepTag, deps.Rlibs...)
  819. actx.AddVariationDependencies(
  820. append(commonDepVariations, []blueprint.Variation{
  821. {Mutator: "rust_libraries", Variation: "dylib"}}...),
  822. dylibDepTag, deps.Dylibs...)
  823. if deps.Rustlibs != nil {
  824. autoDep := mod.compiler.(autoDeppable).autoDep()
  825. actx.AddVariationDependencies(
  826. append(commonDepVariations, []blueprint.Variation{
  827. {Mutator: "rust_libraries", Variation: autoDep.variation}}...),
  828. autoDep.depTag, deps.Rustlibs...)
  829. }
  830. actx.AddVariationDependencies(append(commonDepVariations,
  831. blueprint.Variation{Mutator: "link", Variation: "shared"}),
  832. cc.SharedDepTag(), deps.SharedLibs...)
  833. actx.AddVariationDependencies(append(commonDepVariations,
  834. blueprint.Variation{Mutator: "link", Variation: "static"}),
  835. cc.StaticDepTag(), deps.StaticLibs...)
  836. crtVariations := append(cc.GetCrtVariations(ctx, mod), commonDepVariations...)
  837. if deps.CrtBegin != "" {
  838. actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, deps.CrtBegin)
  839. }
  840. if deps.CrtEnd != "" {
  841. actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, deps.CrtEnd)
  842. }
  843. if mod.sourceProvider != nil {
  844. if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
  845. bindgen.Properties.Custom_bindgen != "" {
  846. actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
  847. bindgen.Properties.Custom_bindgen)
  848. }
  849. }
  850. // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
  851. actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
  852. }
  853. func BeginMutator(ctx android.BottomUpMutatorContext) {
  854. if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
  855. mod.beginMutator(ctx)
  856. }
  857. }
  858. func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
  859. ctx := &baseModuleContext{
  860. BaseModuleContext: actx,
  861. }
  862. mod.begin(ctx)
  863. }
  864. func (mod *Module) Name() string {
  865. name := mod.ModuleBase.Name()
  866. if p, ok := mod.compiler.(interface {
  867. Name(string) string
  868. }); ok {
  869. name = p.Name(name)
  870. }
  871. return name
  872. }
  873. func (mod *Module) disableClippy() {
  874. if mod.clippy != nil {
  875. mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
  876. }
  877. }
  878. var _ android.HostToolProvider = (*Module)(nil)
  879. func (mod *Module) HostToolPath() android.OptionalPath {
  880. if !mod.Host() {
  881. return android.OptionalPath{}
  882. }
  883. if binary, ok := mod.compiler.(*binaryDecorator); ok {
  884. return android.OptionalPathForPath(binary.baseCompiler.path)
  885. }
  886. return android.OptionalPath{}
  887. }
  888. var Bool = proptools.Bool
  889. var BoolDefault = proptools.BoolDefault
  890. var String = proptools.String
  891. var StringPtr = proptools.StringPtr
  892. var _ android.OutputFileProducer = (*Module)(nil)