rust.go 47 KB

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