rust.go 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  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/bloaty"
  22. "android/soong/cc"
  23. cc_config "android/soong/cc/config"
  24. "android/soong/rust/config"
  25. )
  26. var pctx = android.NewPackageContext("android/soong/rust")
  27. func init() {
  28. // Only allow rust modules to be defined for certain projects
  29. android.AddNeverAllowRules(
  30. android.NeverAllow().
  31. NotIn(append(config.RustAllowedPaths, config.DownstreamRustAllowedPaths...)...).
  32. ModuleType(config.RustModuleTypes...))
  33. android.RegisterModuleType("rust_defaults", defaultsFactory)
  34. android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
  35. ctx.BottomUp("rust_libraries", LibraryMutator).Parallel()
  36. ctx.BottomUp("rust_stdlinkage", LibstdMutator).Parallel()
  37. ctx.BottomUp("rust_begin", BeginMutator).Parallel()
  38. })
  39. android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
  40. ctx.BottomUp("rust_sanitizers", rustSanitizerRuntimeMutator).Parallel()
  41. })
  42. pctx.Import("android/soong/rust/config")
  43. pctx.ImportAs("cc_config", "android/soong/cc/config")
  44. }
  45. type Flags struct {
  46. GlobalRustFlags []string // Flags that apply globally to rust
  47. GlobalLinkFlags []string // Flags that apply globally to linker
  48. RustFlags []string // Flags that apply to rust
  49. LinkFlags []string // Flags that apply to linker
  50. ClippyFlags []string // Flags that apply to clippy-driver, during the linting
  51. RustdocFlags []string // Flags that apply to rustdoc
  52. Toolchain config.Toolchain
  53. Coverage bool
  54. Clippy bool
  55. }
  56. type BaseProperties struct {
  57. AndroidMkRlibs []string
  58. AndroidMkDylibs []string
  59. AndroidMkProcMacroLibs []string
  60. AndroidMkSharedLibs []string
  61. AndroidMkStaticLibs []string
  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. ExtraVariants []string `blueprint:"mutated"`
  73. // Used by vendor snapshot to record dependencies from snapshot modules.
  74. SnapshotSharedLibs []string `blueprint:"mutated"`
  75. SnapshotStaticLibs []string `blueprint:"mutated"`
  76. // Make this module available when building for vendor ramdisk.
  77. // On device without a dedicated recovery partition, the module is only
  78. // available after switching root into
  79. // /first_stage_ramdisk. To expose the module before switching root, install
  80. // the recovery variant instead (TODO(b/165791368) recovery not yet supported)
  81. Vendor_ramdisk_available *bool
  82. // Normally Soong uses the directory structure to decide which modules
  83. // should be included (framework) or excluded (non-framework) from the
  84. // different snapshots (vendor, recovery, etc.), but this property
  85. // allows a partner to exclude a module normally thought of as a
  86. // framework module from the vendor snapshot.
  87. Exclude_from_vendor_snapshot *bool
  88. // Normally Soong uses the directory structure to decide which modules
  89. // should be included (framework) or excluded (non-framework) from the
  90. // different snapshots (vendor, recovery, etc.), but this property
  91. // allows a partner to exclude a module normally thought of as a
  92. // framework module from the recovery snapshot.
  93. Exclude_from_recovery_snapshot *bool
  94. // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
  95. Min_sdk_version *string
  96. PreventInstall bool
  97. HideFromMake bool
  98. Installable *bool
  99. }
  100. type Module struct {
  101. android.ModuleBase
  102. android.DefaultableModuleBase
  103. android.ApexModuleBase
  104. VendorProperties cc.VendorProperties
  105. Properties BaseProperties
  106. hod android.HostOrDeviceSupported
  107. multilib android.Multilib
  108. makeLinkType string
  109. compiler compiler
  110. coverage *coverage
  111. clippy *clippy
  112. sanitize *sanitize
  113. cachedToolchain config.Toolchain
  114. sourceProvider SourceProvider
  115. subAndroidMkOnce map[SubAndroidMkProvider]bool
  116. // Unstripped output. This is usually used when this module is linked to another module
  117. // as a library. The stripped output which is used for installation can be found via
  118. // compiler.strippedOutputFile if it exists.
  119. unstrippedOutputFile android.OptionalPath
  120. docTimestampFile android.OptionalPath
  121. hideApexVariantFromMake bool
  122. }
  123. func (mod *Module) Header() bool {
  124. //TODO: If Rust libraries provide header variants, this needs to be updated.
  125. return false
  126. }
  127. func (mod *Module) SetPreventInstall() {
  128. mod.Properties.PreventInstall = true
  129. }
  130. func (mod *Module) SetHideFromMake() {
  131. mod.Properties.HideFromMake = true
  132. }
  133. func (c *Module) HiddenFromMake() bool {
  134. return c.Properties.HideFromMake
  135. }
  136. func (mod *Module) SanitizePropDefined() bool {
  137. // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
  138. // nil since we need compiler to actually sanitize.
  139. return mod.sanitize != nil && mod.compiler != nil
  140. }
  141. func (mod *Module) IsPrebuilt() bool {
  142. if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
  143. return true
  144. }
  145. return false
  146. }
  147. func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
  148. switch tag {
  149. case "":
  150. if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
  151. return mod.sourceProvider.Srcs(), nil
  152. } else {
  153. if mod.OutputFile().Valid() {
  154. return android.Paths{mod.OutputFile().Path()}, nil
  155. }
  156. return android.Paths{}, nil
  157. }
  158. default:
  159. return nil, fmt.Errorf("unsupported module reference tag %q", tag)
  160. }
  161. }
  162. func (mod *Module) SelectedStl() string {
  163. return ""
  164. }
  165. func (mod *Module) NonCcVariants() bool {
  166. if mod.compiler != nil {
  167. if _, ok := mod.compiler.(libraryInterface); ok {
  168. return false
  169. }
  170. }
  171. panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
  172. }
  173. func (mod *Module) Static() bool {
  174. if mod.compiler != nil {
  175. if library, ok := mod.compiler.(libraryInterface); ok {
  176. return library.static()
  177. }
  178. }
  179. return false
  180. }
  181. func (mod *Module) Shared() bool {
  182. if mod.compiler != nil {
  183. if library, ok := mod.compiler.(libraryInterface); ok {
  184. return library.shared()
  185. }
  186. }
  187. return false
  188. }
  189. func (mod *Module) Dylib() bool {
  190. if mod.compiler != nil {
  191. if library, ok := mod.compiler.(libraryInterface); ok {
  192. return library.dylib()
  193. }
  194. }
  195. return false
  196. }
  197. func (mod *Module) Rlib() bool {
  198. if mod.compiler != nil {
  199. if library, ok := mod.compiler.(libraryInterface); ok {
  200. return library.rlib()
  201. }
  202. }
  203. return false
  204. }
  205. func (mod *Module) Binary() bool {
  206. if mod.compiler != nil {
  207. if _, ok := mod.compiler.(*binaryDecorator); ok {
  208. return true
  209. }
  210. }
  211. return false
  212. }
  213. func (mod *Module) StaticExecutable() bool {
  214. if !mod.Binary() {
  215. return false
  216. }
  217. return Bool(mod.compiler.(*binaryDecorator).Properties.Static_executable)
  218. }
  219. func (mod *Module) Object() bool {
  220. // Rust has no modules which produce only object files.
  221. return false
  222. }
  223. func (mod *Module) Toc() android.OptionalPath {
  224. if mod.compiler != nil {
  225. if _, ok := mod.compiler.(libraryInterface); ok {
  226. return android.OptionalPath{}
  227. }
  228. }
  229. panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
  230. }
  231. func (mod *Module) UseSdk() bool {
  232. return false
  233. }
  234. func (mod *Module) RelativeInstallPath() string {
  235. if mod.compiler != nil {
  236. return mod.compiler.relativeInstallPath()
  237. }
  238. return ""
  239. }
  240. func (mod *Module) UseVndk() bool {
  241. return mod.Properties.VndkVersion != ""
  242. }
  243. func (mod *Module) Bootstrap() bool {
  244. return false
  245. }
  246. func (mod *Module) MustUseVendorVariant() bool {
  247. return true
  248. }
  249. func (mod *Module) SubName() string {
  250. return mod.Properties.SubName
  251. }
  252. func (mod *Module) IsVndk() bool {
  253. // TODO(b/165791368)
  254. return false
  255. }
  256. func (mod *Module) IsVndkExt() bool {
  257. return false
  258. }
  259. func (mod *Module) IsVndkSp() bool {
  260. return false
  261. }
  262. func (c *Module) IsVndkPrivate() bool {
  263. return false
  264. }
  265. func (c *Module) IsLlndk() bool {
  266. return false
  267. }
  268. func (c *Module) IsLlndkPublic() bool {
  269. return false
  270. }
  271. func (mod *Module) KernelHeadersDecorator() bool {
  272. return false
  273. }
  274. func (m *Module) NeedsLlndkVariants() bool {
  275. return false
  276. }
  277. func (m *Module) NeedsVendorPublicLibraryVariants() bool {
  278. return false
  279. }
  280. func (mod *Module) HasLlndkStubs() bool {
  281. return false
  282. }
  283. func (mod *Module) StubsVersion() string {
  284. panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
  285. }
  286. func (mod *Module) SdkVersion() string {
  287. return ""
  288. }
  289. func (mod *Module) MinSdkVersion() string {
  290. return ""
  291. }
  292. func (mod *Module) AlwaysSdk() bool {
  293. return false
  294. }
  295. func (mod *Module) IsSdkVariant() bool {
  296. return false
  297. }
  298. func (mod *Module) SplitPerApiLevel() bool {
  299. return false
  300. }
  301. type Deps struct {
  302. Dylibs []string
  303. Rlibs []string
  304. Rustlibs []string
  305. Stdlibs []string
  306. ProcMacros []string
  307. SharedLibs []string
  308. StaticLibs []string
  309. WholeStaticLibs []string
  310. HeaderLibs []string
  311. CrtBegin, CrtEnd string
  312. }
  313. type PathDeps struct {
  314. DyLibs RustLibraries
  315. RLibs RustLibraries
  316. SharedLibs android.Paths
  317. SharedLibDeps android.Paths
  318. StaticLibs android.Paths
  319. ProcMacros RustLibraries
  320. // depFlags and depLinkFlags are rustc and linker (clang) flags.
  321. depFlags []string
  322. depLinkFlags []string
  323. // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
  324. // Both of these are exported and propagate to dependencies.
  325. linkDirs []string
  326. linkObjects []string
  327. // Used by bindgen modules which call clang
  328. depClangFlags []string
  329. depIncludePaths android.Paths
  330. depGeneratedHeaders android.Paths
  331. depSystemIncludePaths android.Paths
  332. CrtBegin android.OptionalPath
  333. CrtEnd android.OptionalPath
  334. // Paths to generated source files
  335. SrcDeps android.Paths
  336. srcProviderFiles android.Paths
  337. }
  338. type RustLibraries []RustLibrary
  339. type RustLibrary struct {
  340. Path android.Path
  341. CrateName string
  342. }
  343. type compiler interface {
  344. initialize(ctx ModuleContext)
  345. compilerFlags(ctx ModuleContext, flags Flags) Flags
  346. compilerProps() []interface{}
  347. compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
  348. compilerDeps(ctx DepsContext, deps Deps) Deps
  349. crateName() string
  350. rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath
  351. // Output directory in which source-generated code from dependencies is
  352. // copied. This is equivalent to Cargo's OUT_DIR variable.
  353. CargoOutDir() android.OptionalPath
  354. inData() bool
  355. install(ctx ModuleContext)
  356. relativeInstallPath() string
  357. everInstallable() bool
  358. nativeCoverage() bool
  359. Disabled() bool
  360. SetDisabled()
  361. stdLinkage(ctx *depsContext) RustLinkage
  362. strippedOutputFilePath() android.OptionalPath
  363. }
  364. type exportedFlagsProducer interface {
  365. exportLinkDirs(...string)
  366. exportLinkObjects(...string)
  367. }
  368. type flagExporter struct {
  369. linkDirs []string
  370. linkObjects []string
  371. }
  372. func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
  373. flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
  374. }
  375. func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
  376. flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
  377. }
  378. func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
  379. ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
  380. LinkDirs: flagExporter.linkDirs,
  381. LinkObjects: flagExporter.linkObjects,
  382. })
  383. }
  384. var _ exportedFlagsProducer = (*flagExporter)(nil)
  385. func NewFlagExporter() *flagExporter {
  386. return &flagExporter{}
  387. }
  388. type FlagExporterInfo struct {
  389. Flags []string
  390. LinkDirs []string // TODO: this should be android.Paths
  391. LinkObjects []string // TODO: this should be android.Paths
  392. }
  393. var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
  394. func (mod *Module) isCoverageVariant() bool {
  395. return mod.coverage.Properties.IsCoverageVariant
  396. }
  397. var _ cc.Coverage = (*Module)(nil)
  398. func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
  399. return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
  400. }
  401. func (mod *Module) VndkVersion() string {
  402. return mod.Properties.VndkVersion
  403. }
  404. func (mod *Module) PreventInstall() bool {
  405. return mod.Properties.PreventInstall
  406. }
  407. func (mod *Module) HideFromMake() {
  408. mod.Properties.HideFromMake = true
  409. }
  410. func (mod *Module) MarkAsCoverageVariant(coverage bool) {
  411. mod.coverage.Properties.IsCoverageVariant = coverage
  412. }
  413. func (mod *Module) EnableCoverageIfNeeded() {
  414. mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
  415. }
  416. func defaultsFactory() android.Module {
  417. return DefaultsFactory()
  418. }
  419. type Defaults struct {
  420. android.ModuleBase
  421. android.DefaultsModuleBase
  422. }
  423. func DefaultsFactory(props ...interface{}) android.Module {
  424. module := &Defaults{}
  425. module.AddProperties(props...)
  426. module.AddProperties(
  427. &BaseProperties{},
  428. &cc.VendorProperties{},
  429. &BenchmarkProperties{},
  430. &BindgenProperties{},
  431. &BaseCompilerProperties{},
  432. &BinaryCompilerProperties{},
  433. &LibraryCompilerProperties{},
  434. &ProcMacroCompilerProperties{},
  435. &PrebuiltProperties{},
  436. &SourceProviderProperties{},
  437. &TestProperties{},
  438. &cc.CoverageProperties{},
  439. &cc.RustBindgenClangProperties{},
  440. &ClippyProperties{},
  441. &SanitizeProperties{},
  442. )
  443. android.InitDefaultsModule(module)
  444. return module
  445. }
  446. func (mod *Module) CrateName() string {
  447. return mod.compiler.crateName()
  448. }
  449. func (mod *Module) CcLibrary() bool {
  450. if mod.compiler != nil {
  451. if _, ok := mod.compiler.(*libraryDecorator); ok {
  452. return true
  453. }
  454. }
  455. return false
  456. }
  457. func (mod *Module) CcLibraryInterface() bool {
  458. if mod.compiler != nil {
  459. // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
  460. // VariantIs{Static,Shared} is set.
  461. if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
  462. return true
  463. }
  464. }
  465. return false
  466. }
  467. func (mod *Module) IncludeDirs() android.Paths {
  468. if mod.compiler != nil {
  469. if library, ok := mod.compiler.(*libraryDecorator); ok {
  470. return library.includeDirs
  471. }
  472. }
  473. panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
  474. }
  475. func (mod *Module) SetStatic() {
  476. if mod.compiler != nil {
  477. if library, ok := mod.compiler.(libraryInterface); ok {
  478. library.setStatic()
  479. return
  480. }
  481. }
  482. panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
  483. }
  484. func (mod *Module) SetShared() {
  485. if mod.compiler != nil {
  486. if library, ok := mod.compiler.(libraryInterface); ok {
  487. library.setShared()
  488. return
  489. }
  490. }
  491. panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
  492. }
  493. func (mod *Module) BuildStaticVariant() bool {
  494. if mod.compiler != nil {
  495. if library, ok := mod.compiler.(libraryInterface); ok {
  496. return library.buildStatic()
  497. }
  498. }
  499. panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
  500. }
  501. func (mod *Module) BuildSharedVariant() bool {
  502. if mod.compiler != nil {
  503. if library, ok := mod.compiler.(libraryInterface); ok {
  504. return library.buildShared()
  505. }
  506. }
  507. panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
  508. }
  509. func (mod *Module) Module() android.Module {
  510. return mod
  511. }
  512. func (mod *Module) OutputFile() android.OptionalPath {
  513. if mod.compiler != nil && mod.compiler.strippedOutputFilePath().Valid() {
  514. return mod.compiler.strippedOutputFilePath()
  515. }
  516. return mod.unstrippedOutputFile
  517. }
  518. func (mod *Module) CoverageFiles() android.Paths {
  519. if mod.compiler != nil {
  520. return android.Paths{}
  521. }
  522. panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
  523. }
  524. func (mod *Module) installable(apexInfo android.ApexInfo) bool {
  525. if !mod.EverInstallable() {
  526. return false
  527. }
  528. // The apex variant is not installable because it is included in the APEX and won't appear
  529. // in the system partition as a standalone file.
  530. if !apexInfo.IsForPlatform() {
  531. return false
  532. }
  533. return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
  534. }
  535. var _ cc.LinkableInterface = (*Module)(nil)
  536. func (mod *Module) Init() android.Module {
  537. mod.AddProperties(&mod.Properties)
  538. mod.AddProperties(&mod.VendorProperties)
  539. if mod.compiler != nil {
  540. mod.AddProperties(mod.compiler.compilerProps()...)
  541. }
  542. if mod.coverage != nil {
  543. mod.AddProperties(mod.coverage.props()...)
  544. }
  545. if mod.clippy != nil {
  546. mod.AddProperties(mod.clippy.props()...)
  547. }
  548. if mod.sourceProvider != nil {
  549. mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
  550. }
  551. if mod.sanitize != nil {
  552. mod.AddProperties(mod.sanitize.props()...)
  553. }
  554. android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
  555. android.InitApexModule(mod)
  556. android.InitDefaultableModule(mod)
  557. return mod
  558. }
  559. func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
  560. return &Module{
  561. hod: hod,
  562. multilib: multilib,
  563. }
  564. }
  565. func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
  566. module := newBaseModule(hod, multilib)
  567. module.coverage = &coverage{}
  568. module.clippy = &clippy{}
  569. module.sanitize = &sanitize{}
  570. return module
  571. }
  572. type ModuleContext interface {
  573. android.ModuleContext
  574. ModuleContextIntf
  575. }
  576. type BaseModuleContext interface {
  577. android.BaseModuleContext
  578. ModuleContextIntf
  579. }
  580. type DepsContext interface {
  581. android.BottomUpMutatorContext
  582. ModuleContextIntf
  583. }
  584. type ModuleContextIntf interface {
  585. RustModule() *Module
  586. toolchain() config.Toolchain
  587. }
  588. type depsContext struct {
  589. android.BottomUpMutatorContext
  590. }
  591. type moduleContext struct {
  592. android.ModuleContext
  593. }
  594. type baseModuleContext struct {
  595. android.BaseModuleContext
  596. }
  597. func (ctx *moduleContext) RustModule() *Module {
  598. return ctx.Module().(*Module)
  599. }
  600. func (ctx *moduleContext) toolchain() config.Toolchain {
  601. return ctx.RustModule().toolchain(ctx)
  602. }
  603. func (ctx *depsContext) RustModule() *Module {
  604. return ctx.Module().(*Module)
  605. }
  606. func (ctx *depsContext) toolchain() config.Toolchain {
  607. return ctx.RustModule().toolchain(ctx)
  608. }
  609. func (ctx *baseModuleContext) RustModule() *Module {
  610. return ctx.Module().(*Module)
  611. }
  612. func (ctx *baseModuleContext) toolchain() config.Toolchain {
  613. return ctx.RustModule().toolchain(ctx)
  614. }
  615. func (mod *Module) nativeCoverage() bool {
  616. return mod.compiler != nil && mod.compiler.nativeCoverage()
  617. }
  618. func (mod *Module) EverInstallable() bool {
  619. return mod.compiler != nil &&
  620. // Check to see whether the module is actually ever installable.
  621. mod.compiler.everInstallable()
  622. }
  623. func (mod *Module) Installable() *bool {
  624. return mod.Properties.Installable
  625. }
  626. func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
  627. if mod.cachedToolchain == nil {
  628. mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
  629. }
  630. return mod.cachedToolchain
  631. }
  632. func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
  633. return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
  634. }
  635. func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  636. }
  637. func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
  638. ctx := &moduleContext{
  639. ModuleContext: actx,
  640. }
  641. apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
  642. if !apexInfo.IsForPlatform() {
  643. mod.hideApexVariantFromMake = true
  644. }
  645. toolchain := mod.toolchain(ctx)
  646. mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
  647. // Differentiate static libraries that are vendor available
  648. if mod.UseVndk() {
  649. mod.Properties.SubName += cc.VendorSuffix
  650. } else if mod.InVendorRamdisk() && !mod.OnlyInVendorRamdisk() {
  651. mod.Properties.SubName += cc.VendorRamdiskSuffix
  652. }
  653. if !toolchain.Supported() {
  654. // This toolchain's unsupported, there's nothing to do for this mod.
  655. return
  656. }
  657. deps := mod.depsToPaths(ctx)
  658. flags := Flags{
  659. Toolchain: toolchain,
  660. }
  661. if mod.compiler != nil {
  662. flags = mod.compiler.compilerFlags(ctx, flags)
  663. }
  664. if mod.coverage != nil {
  665. flags, deps = mod.coverage.flags(ctx, flags, deps)
  666. }
  667. if mod.clippy != nil {
  668. flags, deps = mod.clippy.flags(ctx, flags, deps)
  669. }
  670. if mod.sanitize != nil {
  671. flags, deps = mod.sanitize.flags(ctx, flags, deps)
  672. }
  673. // SourceProvider needs to call GenerateSource() before compiler calls
  674. // compile() so it can provide the source. A SourceProvider has
  675. // multiple variants (e.g. source, rlib, dylib). Only the "source"
  676. // variant is responsible for effectively generating the source. The
  677. // remaining variants relies on the "source" variant output.
  678. if mod.sourceProvider != nil {
  679. if mod.compiler.(libraryInterface).source() {
  680. mod.sourceProvider.GenerateSource(ctx, deps)
  681. mod.sourceProvider.setSubName(ctx.ModuleSubDir())
  682. } else {
  683. sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
  684. sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
  685. mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
  686. }
  687. }
  688. if mod.compiler != nil && !mod.compiler.Disabled() {
  689. mod.compiler.initialize(ctx)
  690. unstrippedOutputFile := mod.compiler.compile(ctx, flags, deps)
  691. mod.unstrippedOutputFile = android.OptionalPathForPath(unstrippedOutputFile)
  692. bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), mod.unstrippedOutputFile)
  693. mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
  694. // glob exported headers for snapshot, if BOARD_VNDK_VERSION is current or
  695. // RECOVERY_SNAPSHOT_VERSION is current.
  696. if lib, ok := mod.compiler.(snapshotLibraryInterface); ok {
  697. if cc.ShouldCollectHeadersForSnapshot(ctx, mod, apexInfo) {
  698. lib.collectHeadersForSnapshot(ctx, deps)
  699. }
  700. }
  701. apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
  702. if mod.installable(apexInfo) {
  703. mod.compiler.install(ctx)
  704. }
  705. }
  706. }
  707. func (mod *Module) deps(ctx DepsContext) Deps {
  708. deps := Deps{}
  709. if mod.compiler != nil {
  710. deps = mod.compiler.compilerDeps(ctx, deps)
  711. }
  712. if mod.sourceProvider != nil {
  713. deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
  714. }
  715. if mod.coverage != nil {
  716. deps = mod.coverage.deps(ctx, deps)
  717. }
  718. if mod.sanitize != nil {
  719. deps = mod.sanitize.deps(ctx, deps)
  720. }
  721. deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
  722. deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
  723. deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
  724. deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
  725. deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
  726. deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
  727. deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
  728. return deps
  729. }
  730. type dependencyTag struct {
  731. blueprint.BaseDependencyTag
  732. name string
  733. library bool
  734. procMacro bool
  735. }
  736. // InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
  737. // dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
  738. func (d dependencyTag) InstallDepNeeded() bool {
  739. return d.library || d.procMacro
  740. }
  741. var _ android.InstallNeededDependencyTag = dependencyTag{}
  742. var (
  743. customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
  744. rlibDepTag = dependencyTag{name: "rlibTag", library: true}
  745. dylibDepTag = dependencyTag{name: "dylib", library: true}
  746. procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
  747. testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
  748. sourceDepTag = dependencyTag{name: "source"}
  749. )
  750. func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
  751. tag, ok := depTag.(dependencyTag)
  752. return ok && tag == dylibDepTag
  753. }
  754. func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
  755. tag, ok := depTag.(dependencyTag)
  756. return ok && tag == rlibDepTag
  757. }
  758. type autoDep struct {
  759. variation string
  760. depTag dependencyTag
  761. }
  762. var (
  763. rlibVariation = "rlib"
  764. dylibVariation = "dylib"
  765. rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
  766. dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
  767. )
  768. type autoDeppable interface {
  769. autoDep(ctx android.BottomUpMutatorContext) autoDep
  770. }
  771. func (mod *Module) begin(ctx BaseModuleContext) {
  772. if mod.coverage != nil {
  773. mod.coverage.begin(ctx)
  774. }
  775. if mod.sanitize != nil {
  776. mod.sanitize.begin(ctx)
  777. }
  778. }
  779. func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
  780. var depPaths PathDeps
  781. directRlibDeps := []*Module{}
  782. directDylibDeps := []*Module{}
  783. directProcMacroDeps := []*Module{}
  784. directSharedLibDeps := []cc.SharedLibraryInfo{}
  785. directStaticLibDeps := [](cc.LinkableInterface){}
  786. directSrcProvidersDeps := []*Module{}
  787. directSrcDeps := [](android.SourceFileProducer){}
  788. ctx.VisitDirectDeps(func(dep android.Module) {
  789. depName := ctx.OtherModuleName(dep)
  790. depTag := ctx.OtherModuleDependencyTag(dep)
  791. if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
  792. //Handle Rust Modules
  793. makeLibName := cc.MakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
  794. switch depTag {
  795. case dylibDepTag:
  796. dylib, ok := rustDep.compiler.(libraryInterface)
  797. if !ok || !dylib.dylib() {
  798. ctx.ModuleErrorf("mod %q not an dylib library", depName)
  799. return
  800. }
  801. directDylibDeps = append(directDylibDeps, rustDep)
  802. mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
  803. case rlibDepTag:
  804. rlib, ok := rustDep.compiler.(libraryInterface)
  805. if !ok || !rlib.rlib() {
  806. ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
  807. return
  808. }
  809. directRlibDeps = append(directRlibDeps, rustDep)
  810. mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
  811. case procMacroDepTag:
  812. directProcMacroDeps = append(directProcMacroDeps, rustDep)
  813. mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
  814. }
  815. if android.IsSourceDepTagWithOutputTag(depTag, "") {
  816. // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
  817. // OS/Arch variant is used.
  818. var helper string
  819. if ctx.Host() {
  820. helper = "missing 'host_supported'?"
  821. } else {
  822. helper = "device module defined?"
  823. }
  824. if dep.Target().Os != ctx.Os() {
  825. ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
  826. return
  827. } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
  828. ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
  829. return
  830. }
  831. directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
  832. }
  833. //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
  834. if depTag != procMacroDepTag {
  835. exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
  836. depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
  837. depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
  838. depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
  839. }
  840. if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
  841. linkFile := rustDep.unstrippedOutputFile
  842. if !linkFile.Valid() {
  843. ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
  844. depName, ctx.ModuleName())
  845. return
  846. }
  847. linkDir := linkPathFromFilePath(linkFile.Path())
  848. if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
  849. lib.exportLinkDirs(linkDir)
  850. }
  851. }
  852. } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
  853. //Handle C dependencies
  854. makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
  855. if _, ok := ccDep.(*Module); !ok {
  856. if ccDep.Module().Target().Os != ctx.Os() {
  857. ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
  858. return
  859. }
  860. if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
  861. ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
  862. return
  863. }
  864. }
  865. linkObject := ccDep.OutputFile()
  866. linkPath := linkPathFromFilePath(linkObject.Path())
  867. if !linkObject.Valid() {
  868. ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
  869. }
  870. exportDep := false
  871. switch {
  872. case cc.IsStaticDepTag(depTag):
  873. if cc.IsWholeStaticLib(depTag) {
  874. // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
  875. // if the library is not prefixed by "lib".
  876. if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
  877. depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
  878. } else {
  879. ctx.ModuleErrorf("'%q' cannot be listed as a whole_static_library in Rust modules unless the output is prefixed by 'lib'", depName, ctx.ModuleName())
  880. }
  881. }
  882. // Add this to linkObjects to pass the library directly to the linker as well. This propagates
  883. // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
  884. depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
  885. depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
  886. exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
  887. depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
  888. depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
  889. depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
  890. depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
  891. directStaticLibDeps = append(directStaticLibDeps, ccDep)
  892. mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
  893. case cc.IsSharedDepTag(depTag):
  894. // For the shared lib dependencies, we may link to the stub variant
  895. // of the dependency depending on the context (e.g. if this
  896. // dependency crosses the APEX boundaries).
  897. sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
  898. // Re-get linkObject as ChooseStubOrImpl actually tells us which
  899. // object (either from stub or non-stub) to use.
  900. linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
  901. linkPath = linkPathFromFilePath(linkObject.Path())
  902. depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
  903. depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
  904. depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
  905. depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
  906. depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
  907. depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
  908. directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
  909. // Record baseLibName for snapshots.
  910. mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
  911. mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
  912. mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, makeLibName)
  913. exportDep = true
  914. case cc.IsHeaderDepTag(depTag):
  915. exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
  916. depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
  917. depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
  918. depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
  919. case depTag == cc.CrtBeginDepTag:
  920. depPaths.CrtBegin = linkObject
  921. case depTag == cc.CrtEndDepTag:
  922. depPaths.CrtEnd = linkObject
  923. }
  924. // Make sure these dependencies are propagated
  925. if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
  926. lib.exportLinkDirs(linkPath)
  927. lib.exportLinkObjects(linkObject.String())
  928. }
  929. }
  930. if srcDep, ok := dep.(android.SourceFileProducer); ok {
  931. if android.IsSourceDepTagWithOutputTag(depTag, "") {
  932. // These are usually genrules which don't have per-target variants.
  933. directSrcDeps = append(directSrcDeps, srcDep)
  934. }
  935. }
  936. })
  937. var rlibDepFiles RustLibraries
  938. for _, dep := range directRlibDeps {
  939. rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
  940. }
  941. var dylibDepFiles RustLibraries
  942. for _, dep := range directDylibDeps {
  943. dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
  944. }
  945. var procMacroDepFiles RustLibraries
  946. for _, dep := range directProcMacroDeps {
  947. procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
  948. }
  949. var staticLibDepFiles android.Paths
  950. for _, dep := range directStaticLibDeps {
  951. staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
  952. }
  953. var sharedLibFiles android.Paths
  954. var sharedLibDepFiles android.Paths
  955. for _, dep := range directSharedLibDeps {
  956. sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
  957. if dep.TableOfContents.Valid() {
  958. sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
  959. } else {
  960. sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
  961. }
  962. }
  963. var srcProviderDepFiles android.Paths
  964. for _, dep := range directSrcProvidersDeps {
  965. srcs, _ := dep.OutputFiles("")
  966. srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
  967. }
  968. for _, dep := range directSrcDeps {
  969. srcs := dep.Srcs()
  970. srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
  971. }
  972. depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
  973. depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
  974. depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
  975. depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
  976. depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
  977. depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
  978. depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
  979. // Dedup exported flags from dependencies
  980. depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
  981. depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
  982. depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
  983. depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
  984. depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
  985. depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
  986. return depPaths
  987. }
  988. func (mod *Module) InstallInData() bool {
  989. if mod.compiler == nil {
  990. return false
  991. }
  992. return mod.compiler.inData()
  993. }
  994. func linkPathFromFilePath(filepath android.Path) string {
  995. return strings.Split(filepath.String(), filepath.Base())[0]
  996. }
  997. func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
  998. ctx := &depsContext{
  999. BottomUpMutatorContext: actx,
  1000. }
  1001. deps := mod.deps(ctx)
  1002. var commonDepVariations []blueprint.Variation
  1003. var snapshotInfo *cc.SnapshotInfo
  1004. if ctx.Os() == android.Android {
  1005. deps.SharedLibs, _ = cc.RewriteLibs(mod, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
  1006. }
  1007. stdLinkage := "dylib-std"
  1008. if mod.compiler.stdLinkage(ctx) == RlibLinkage {
  1009. stdLinkage = "rlib-std"
  1010. }
  1011. rlibDepVariations := commonDepVariations
  1012. if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
  1013. rlibDepVariations = append(rlibDepVariations,
  1014. blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
  1015. }
  1016. // rlibs
  1017. for _, lib := range deps.Rlibs {
  1018. depTag := rlibDepTag
  1019. lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
  1020. actx.AddVariationDependencies(append(rlibDepVariations, []blueprint.Variation{
  1021. {Mutator: "rust_libraries", Variation: rlibVariation},
  1022. }...), depTag, lib)
  1023. }
  1024. // dylibs
  1025. actx.AddVariationDependencies(
  1026. append(commonDepVariations, []blueprint.Variation{
  1027. {Mutator: "rust_libraries", Variation: dylibVariation}}...),
  1028. dylibDepTag, deps.Dylibs...)
  1029. // rustlibs
  1030. if deps.Rustlibs != nil && !mod.compiler.Disabled() {
  1031. autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
  1032. if autoDep.depTag == rlibDepTag {
  1033. for _, lib := range deps.Rustlibs {
  1034. depTag := autoDep.depTag
  1035. lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
  1036. actx.AddVariationDependencies(append(rlibDepVariations, []blueprint.Variation{
  1037. {Mutator: "rust_libraries", Variation: autoDep.variation},
  1038. }...), depTag, lib)
  1039. }
  1040. } else {
  1041. actx.AddVariationDependencies(
  1042. append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
  1043. autoDep.depTag, deps.Rustlibs...)
  1044. }
  1045. }
  1046. // stdlibs
  1047. if deps.Stdlibs != nil {
  1048. if mod.compiler.stdLinkage(ctx) == RlibLinkage {
  1049. for _, lib := range deps.Stdlibs {
  1050. depTag := rlibDepTag
  1051. lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
  1052. actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
  1053. depTag, lib)
  1054. }
  1055. } else {
  1056. actx.AddVariationDependencies(
  1057. append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
  1058. dylibDepTag, deps.Stdlibs...)
  1059. }
  1060. }
  1061. for _, lib := range deps.SharedLibs {
  1062. depTag := cc.SharedDepTag()
  1063. name, version := cc.StubsLibNameAndVersion(lib)
  1064. variations := []blueprint.Variation{
  1065. {Mutator: "link", Variation: "shared"},
  1066. }
  1067. cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
  1068. }
  1069. for _, lib := range deps.WholeStaticLibs {
  1070. depTag := cc.StaticDepTag(true)
  1071. lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
  1072. actx.AddVariationDependencies([]blueprint.Variation{
  1073. {Mutator: "link", Variation: "static"},
  1074. }, depTag, lib)
  1075. }
  1076. for _, lib := range deps.StaticLibs {
  1077. depTag := cc.StaticDepTag(false)
  1078. lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
  1079. actx.AddVariationDependencies([]blueprint.Variation{
  1080. {Mutator: "link", Variation: "static"},
  1081. }, depTag, lib)
  1082. }
  1083. actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
  1084. crtVariations := cc.GetCrtVariations(ctx, mod)
  1085. if deps.CrtBegin != "" {
  1086. actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag,
  1087. cc.RewriteSnapshotLib(deps.CrtBegin, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
  1088. }
  1089. if deps.CrtEnd != "" {
  1090. actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag,
  1091. cc.RewriteSnapshotLib(deps.CrtEnd, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
  1092. }
  1093. if mod.sourceProvider != nil {
  1094. if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
  1095. bindgen.Properties.Custom_bindgen != "" {
  1096. actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
  1097. bindgen.Properties.Custom_bindgen)
  1098. }
  1099. }
  1100. // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
  1101. actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
  1102. }
  1103. func BeginMutator(ctx android.BottomUpMutatorContext) {
  1104. if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
  1105. mod.beginMutator(ctx)
  1106. }
  1107. }
  1108. func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
  1109. ctx := &baseModuleContext{
  1110. BaseModuleContext: actx,
  1111. }
  1112. mod.begin(ctx)
  1113. }
  1114. func (mod *Module) Name() string {
  1115. name := mod.ModuleBase.Name()
  1116. if p, ok := mod.compiler.(interface {
  1117. Name(string) string
  1118. }); ok {
  1119. name = p.Name(name)
  1120. }
  1121. return name
  1122. }
  1123. func (mod *Module) disableClippy() {
  1124. if mod.clippy != nil {
  1125. mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
  1126. }
  1127. }
  1128. var _ android.HostToolProvider = (*Module)(nil)
  1129. func (mod *Module) HostToolPath() android.OptionalPath {
  1130. if !mod.Host() {
  1131. return android.OptionalPath{}
  1132. }
  1133. if binary, ok := mod.compiler.(*binaryDecorator); ok {
  1134. return android.OptionalPathForPath(binary.baseCompiler.path)
  1135. }
  1136. return android.OptionalPath{}
  1137. }
  1138. var _ android.ApexModule = (*Module)(nil)
  1139. func (mod *Module) minSdkVersion() string {
  1140. return String(mod.Properties.Min_sdk_version)
  1141. }
  1142. var _ android.ApexModule = (*Module)(nil)
  1143. // Implements android.ApexModule
  1144. func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
  1145. minSdkVersion := mod.minSdkVersion()
  1146. if minSdkVersion == "apex_inherit" {
  1147. return nil
  1148. }
  1149. if minSdkVersion == "" {
  1150. return fmt.Errorf("min_sdk_version is not specificed")
  1151. }
  1152. // Not using nativeApiLevelFromUser because the context here is not
  1153. // necessarily a native context.
  1154. ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
  1155. if err != nil {
  1156. return err
  1157. }
  1158. if ver.GreaterThan(sdkVersion) {
  1159. return fmt.Errorf("newer SDK(%v)", ver)
  1160. }
  1161. return nil
  1162. }
  1163. // Implements android.ApexModule
  1164. func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
  1165. depTag := ctx.OtherModuleDependencyTag(dep)
  1166. if ccm, ok := dep.(*cc.Module); ok {
  1167. if ccm.HasStubsVariants() {
  1168. if cc.IsSharedDepTag(depTag) {
  1169. // dynamic dep to a stubs lib crosses APEX boundary
  1170. return false
  1171. }
  1172. if cc.IsRuntimeDepTag(depTag) {
  1173. // runtime dep to a stubs lib also crosses APEX boundary
  1174. return false
  1175. }
  1176. if cc.IsHeaderDepTag(depTag) {
  1177. return false
  1178. }
  1179. }
  1180. if mod.Static() && cc.IsSharedDepTag(depTag) {
  1181. // shared_lib dependency from a static lib is considered as crossing
  1182. // the APEX boundary because the dependency doesn't actually is
  1183. // linked; the dependency is used only during the compilation phase.
  1184. return false
  1185. }
  1186. }
  1187. if depTag == procMacroDepTag {
  1188. return false
  1189. }
  1190. return true
  1191. }
  1192. // Overrides ApexModule.IsInstallabeToApex()
  1193. func (mod *Module) IsInstallableToApex() bool {
  1194. if mod.compiler != nil {
  1195. if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
  1196. return true
  1197. }
  1198. if _, ok := mod.compiler.(*binaryDecorator); ok {
  1199. return true
  1200. }
  1201. }
  1202. return false
  1203. }
  1204. // If a library file has a "lib" prefix, extract the library name without the prefix.
  1205. func libNameFromFilePath(filepath android.Path) (string, bool) {
  1206. libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
  1207. if strings.HasPrefix(libName, "lib") {
  1208. libName = libName[3:]
  1209. return libName, true
  1210. }
  1211. return "", false
  1212. }
  1213. var Bool = proptools.Bool
  1214. var BoolDefault = proptools.BoolDefault
  1215. var String = proptools.String
  1216. var StringPtr = proptools.StringPtr
  1217. var _ android.OutputFileProducer = (*Module)(nil)