prebuilt_etc.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. // Copyright 2016 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package etc
  15. // This file implements module types that install prebuilt artifacts.
  16. //
  17. // There exist two classes of prebuilt modules in the Android tree. The first class are the ones
  18. // based on `android.Prebuilt`, such as `cc_prebuilt_library` and `java_import`. This kind of
  19. // modules may exist both as prebuilts and source at the same time, though only one would be
  20. // installed and the other would be marked disabled. The `prebuilt_postdeps` mutator would select
  21. // the actual modules to be installed. More details in android/prebuilt.go.
  22. //
  23. // The second class is described in this file. Unlike `android.Prebuilt` based module types,
  24. // `prebuilt_etc` exist only as prebuilts and cannot have a same-named source module counterpart.
  25. // This makes the logic of `prebuilt_etc` to be much simpler as they don't need to go through the
  26. // various `prebuilt_*` mutators.
  27. import (
  28. "encoding/json"
  29. "fmt"
  30. "path/filepath"
  31. "reflect"
  32. "strings"
  33. "github.com/google/blueprint/proptools"
  34. "android/soong/android"
  35. "android/soong/bazel"
  36. "android/soong/snapshot"
  37. )
  38. var pctx = android.NewPackageContext("android/soong/etc")
  39. // TODO(jungw): Now that it handles more than the ones in etc/, consider renaming this file.
  40. func init() {
  41. pctx.Import("android/soong/android")
  42. RegisterPrebuiltEtcBuildComponents(android.InitRegistrationContext)
  43. snapshot.RegisterSnapshotAction(generatePrebuiltSnapshot)
  44. }
  45. func RegisterPrebuiltEtcBuildComponents(ctx android.RegistrationContext) {
  46. ctx.RegisterModuleType("prebuilt_etc", PrebuiltEtcFactory)
  47. ctx.RegisterModuleType("prebuilt_etc_host", PrebuiltEtcHostFactory)
  48. ctx.RegisterModuleType("prebuilt_etc_cacerts", PrebuiltEtcCaCertsFactory)
  49. ctx.RegisterModuleType("prebuilt_root", PrebuiltRootFactory)
  50. ctx.RegisterModuleType("prebuilt_root_host", PrebuiltRootHostFactory)
  51. ctx.RegisterModuleType("prebuilt_usr_share", PrebuiltUserShareFactory)
  52. ctx.RegisterModuleType("prebuilt_usr_share_host", PrebuiltUserShareHostFactory)
  53. ctx.RegisterModuleType("prebuilt_font", PrebuiltFontFactory)
  54. ctx.RegisterModuleType("prebuilt_firmware", PrebuiltFirmwareFactory)
  55. ctx.RegisterModuleType("prebuilt_dsp", PrebuiltDSPFactory)
  56. ctx.RegisterModuleType("prebuilt_rfsa", PrebuiltRFSAFactory)
  57. ctx.RegisterModuleType("prebuilt_defaults", defaultsFactory)
  58. }
  59. var PrepareForTestWithPrebuiltEtc = android.FixtureRegisterWithContext(RegisterPrebuiltEtcBuildComponents)
  60. type prebuiltEtcProperties struct {
  61. // Source file of this prebuilt. Can reference a genrule type module with the ":module" syntax.
  62. Src *string `android:"path,arch_variant"`
  63. // Optional name for the installed file. If unspecified, name of the module is used as the file
  64. // name.
  65. Filename *string `android:"arch_variant"`
  66. // When set to true, and filename property is not set, the name for the installed file
  67. // is the same as the file name of the source file.
  68. Filename_from_src *bool `android:"arch_variant"`
  69. // Make this module available when building for ramdisk.
  70. // On device without a dedicated recovery partition, the module is only
  71. // available after switching root into
  72. // /first_stage_ramdisk. To expose the module before switching root, install
  73. // the recovery variant instead.
  74. Ramdisk_available *bool
  75. // Make this module available when building for vendor ramdisk.
  76. // On device without a dedicated recovery partition, the module is only
  77. // available after switching root into
  78. // /first_stage_ramdisk. To expose the module before switching root, install
  79. // the recovery variant instead.
  80. Vendor_ramdisk_available *bool
  81. // Make this module available when building for debug ramdisk.
  82. Debug_ramdisk_available *bool
  83. // Make this module available when building for recovery.
  84. Recovery_available *bool
  85. // Whether this module is directly installable to one of the partitions. Default: true.
  86. Installable *bool
  87. // Install symlinks to the installed file.
  88. Symlinks []string `android:"arch_variant"`
  89. }
  90. type prebuiltSubdirProperties struct {
  91. // Optional subdirectory under which this file is installed into, cannot be specified with
  92. // relative_install_path, prefer relative_install_path.
  93. Sub_dir *string `android:"arch_variant"`
  94. // Optional subdirectory under which this file is installed into, cannot be specified with
  95. // sub_dir.
  96. Relative_install_path *string `android:"arch_variant"`
  97. }
  98. type PrebuiltEtcModule interface {
  99. android.Module
  100. // Returns the base install directory, such as "etc", "usr/share".
  101. BaseDir() string
  102. // Returns the sub install directory relative to BaseDir().
  103. SubDir() string
  104. // Returns an android.OutputPath to the intermeidate file, which is the renamed prebuilt source
  105. // file.
  106. OutputFile() android.OutputPath
  107. }
  108. type PrebuiltEtc struct {
  109. android.ModuleBase
  110. android.DefaultableModuleBase
  111. android.BazelModuleBase
  112. snapshot.VendorSnapshotModuleInterface
  113. snapshot.RecoverySnapshotModuleInterface
  114. properties prebuiltEtcProperties
  115. subdirProperties prebuiltSubdirProperties
  116. sourceFilePath android.Path
  117. outputFilePath android.OutputPath
  118. // The base install location, e.g. "etc" for prebuilt_etc, "usr/share" for prebuilt_usr_share.
  119. installDirBase string
  120. // The base install location when soc_specific property is set to true, e.g. "firmware" for
  121. // prebuilt_firmware.
  122. socInstallDirBase string
  123. installDirPath android.InstallPath
  124. additionalDependencies *android.Paths
  125. }
  126. type Defaults struct {
  127. android.ModuleBase
  128. android.DefaultsModuleBase
  129. }
  130. func (p *PrebuiltEtc) inRamdisk() bool {
  131. return p.ModuleBase.InRamdisk() || p.ModuleBase.InstallInRamdisk()
  132. }
  133. func (p *PrebuiltEtc) onlyInRamdisk() bool {
  134. return p.ModuleBase.InstallInRamdisk()
  135. }
  136. func (p *PrebuiltEtc) InstallInRamdisk() bool {
  137. return p.inRamdisk()
  138. }
  139. func (p *PrebuiltEtc) inVendorRamdisk() bool {
  140. return p.ModuleBase.InVendorRamdisk() || p.ModuleBase.InstallInVendorRamdisk()
  141. }
  142. func (p *PrebuiltEtc) onlyInVendorRamdisk() bool {
  143. return p.ModuleBase.InstallInVendorRamdisk()
  144. }
  145. func (p *PrebuiltEtc) InstallInVendorRamdisk() bool {
  146. return p.inVendorRamdisk()
  147. }
  148. func (p *PrebuiltEtc) inDebugRamdisk() bool {
  149. return p.ModuleBase.InDebugRamdisk() || p.ModuleBase.InstallInDebugRamdisk()
  150. }
  151. func (p *PrebuiltEtc) onlyInDebugRamdisk() bool {
  152. return p.ModuleBase.InstallInDebugRamdisk()
  153. }
  154. func (p *PrebuiltEtc) InstallInDebugRamdisk() bool {
  155. return p.inDebugRamdisk()
  156. }
  157. func (p *PrebuiltEtc) InRecovery() bool {
  158. return p.ModuleBase.InRecovery() || p.ModuleBase.InstallInRecovery()
  159. }
  160. func (p *PrebuiltEtc) onlyInRecovery() bool {
  161. return p.ModuleBase.InstallInRecovery()
  162. }
  163. func (p *PrebuiltEtc) InstallInRecovery() bool {
  164. return p.InRecovery()
  165. }
  166. var _ android.ImageInterface = (*PrebuiltEtc)(nil)
  167. func (p *PrebuiltEtc) ImageMutatorBegin(ctx android.BaseModuleContext) {}
  168. func (p *PrebuiltEtc) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
  169. return !p.ModuleBase.InstallInRecovery() && !p.ModuleBase.InstallInRamdisk() &&
  170. !p.ModuleBase.InstallInVendorRamdisk() && !p.ModuleBase.InstallInDebugRamdisk()
  171. }
  172. func (p *PrebuiltEtc) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
  173. return proptools.Bool(p.properties.Ramdisk_available) || p.ModuleBase.InstallInRamdisk()
  174. }
  175. func (p *PrebuiltEtc) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
  176. return proptools.Bool(p.properties.Vendor_ramdisk_available) || p.ModuleBase.InstallInVendorRamdisk()
  177. }
  178. func (p *PrebuiltEtc) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
  179. return proptools.Bool(p.properties.Debug_ramdisk_available) || p.ModuleBase.InstallInDebugRamdisk()
  180. }
  181. func (p *PrebuiltEtc) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
  182. return proptools.Bool(p.properties.Recovery_available) || p.ModuleBase.InstallInRecovery()
  183. }
  184. func (p *PrebuiltEtc) ExtraImageVariations(ctx android.BaseModuleContext) []string {
  185. return nil
  186. }
  187. func (p *PrebuiltEtc) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
  188. }
  189. func (p *PrebuiltEtc) SourceFilePath(ctx android.ModuleContext) android.Path {
  190. return android.PathForModuleSrc(ctx, proptools.String(p.properties.Src))
  191. }
  192. func (p *PrebuiltEtc) InstallDirPath() android.InstallPath {
  193. return p.installDirPath
  194. }
  195. // This allows other derivative modules (e.g. prebuilt_etc_xml) to perform
  196. // additional steps (like validating the src) before the file is installed.
  197. func (p *PrebuiltEtc) SetAdditionalDependencies(paths android.Paths) {
  198. p.additionalDependencies = &paths
  199. }
  200. func (p *PrebuiltEtc) OutputFile() android.OutputPath {
  201. return p.outputFilePath
  202. }
  203. var _ android.OutputFileProducer = (*PrebuiltEtc)(nil)
  204. func (p *PrebuiltEtc) OutputFiles(tag string) (android.Paths, error) {
  205. switch tag {
  206. case "":
  207. return android.Paths{p.outputFilePath}, nil
  208. default:
  209. return nil, fmt.Errorf("unsupported module reference tag %q", tag)
  210. }
  211. }
  212. func (p *PrebuiltEtc) SubDir() string {
  213. if subDir := proptools.String(p.subdirProperties.Sub_dir); subDir != "" {
  214. return subDir
  215. }
  216. return proptools.String(p.subdirProperties.Relative_install_path)
  217. }
  218. func (p *PrebuiltEtc) BaseDir() string {
  219. return p.installDirBase
  220. }
  221. func (p *PrebuiltEtc) Installable() bool {
  222. return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
  223. }
  224. func (p *PrebuiltEtc) InVendor() bool {
  225. return p.ModuleBase.InstallInVendor()
  226. }
  227. func (p *PrebuiltEtc) ExcludeFromVendorSnapshot() bool {
  228. return false
  229. }
  230. func (p *PrebuiltEtc) ExcludeFromRecoverySnapshot() bool {
  231. return false
  232. }
  233. func (p *PrebuiltEtc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  234. filename := proptools.String(p.properties.Filename)
  235. filenameFromSrc := proptools.Bool(p.properties.Filename_from_src)
  236. if p.properties.Src != nil {
  237. p.sourceFilePath = android.PathForModuleSrc(ctx, proptools.String(p.properties.Src))
  238. // Determine the output file basename.
  239. // If Filename is set, use the name specified by the property.
  240. // If Filename_from_src is set, use the source file name.
  241. // Otherwise use the module name.
  242. if filename != "" {
  243. if filenameFromSrc {
  244. ctx.PropertyErrorf("filename_from_src", "filename is set. filename_from_src can't be true")
  245. return
  246. }
  247. } else if filenameFromSrc {
  248. filename = p.sourceFilePath.Base()
  249. } else {
  250. filename = ctx.ModuleName()
  251. }
  252. } else if ctx.Config().AllowMissingDependencies() {
  253. // If no srcs was set and AllowMissingDependencies is enabled then
  254. // mark the module as missing dependencies and set a fake source path
  255. // and file name.
  256. ctx.AddMissingDependencies([]string{"MISSING_PREBUILT_SRC_FILE"})
  257. p.sourceFilePath = android.PathForModuleSrc(ctx)
  258. if filename == "" {
  259. filename = ctx.ModuleName()
  260. }
  261. } else {
  262. ctx.PropertyErrorf("src", "missing prebuilt source file")
  263. return
  264. }
  265. p.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
  266. if strings.Contains(filename, "/") {
  267. ctx.PropertyErrorf("filename", "filename cannot contain separator '/'")
  268. return
  269. }
  270. // Check that `sub_dir` and `relative_install_path` are not set at the same time.
  271. if p.subdirProperties.Sub_dir != nil && p.subdirProperties.Relative_install_path != nil {
  272. ctx.PropertyErrorf("sub_dir", "relative_install_path is set. Cannot set sub_dir")
  273. }
  274. // If soc install dir was specified and SOC specific is set, set the installDirPath to the
  275. // specified socInstallDirBase.
  276. installBaseDir := p.installDirBase
  277. if p.SocSpecific() && p.socInstallDirBase != "" {
  278. installBaseDir = p.socInstallDirBase
  279. }
  280. p.installDirPath = android.PathForModuleInstall(ctx, installBaseDir, p.SubDir())
  281. // This ensures that outputFilePath has the correct name for others to
  282. // use, as the source file may have a different name.
  283. ctx.Build(pctx, android.BuildParams{
  284. Rule: android.Cp,
  285. Output: p.outputFilePath,
  286. Input: p.sourceFilePath,
  287. })
  288. if !p.Installable() {
  289. p.SkipInstall()
  290. }
  291. // Call InstallFile even when uninstallable to make the module included in the package
  292. installPath := ctx.InstallFile(p.installDirPath, p.outputFilePath.Base(), p.outputFilePath)
  293. for _, sl := range p.properties.Symlinks {
  294. ctx.InstallSymlink(p.installDirPath, sl, installPath)
  295. }
  296. }
  297. func (p *PrebuiltEtc) AndroidMkEntries() []android.AndroidMkEntries {
  298. nameSuffix := ""
  299. if p.inRamdisk() && !p.onlyInRamdisk() {
  300. nameSuffix = ".ramdisk"
  301. }
  302. if p.inVendorRamdisk() && !p.onlyInVendorRamdisk() {
  303. nameSuffix = ".vendor_ramdisk"
  304. }
  305. if p.inDebugRamdisk() && !p.onlyInDebugRamdisk() {
  306. nameSuffix = ".debug_ramdisk"
  307. }
  308. if p.InRecovery() && !p.onlyInRecovery() {
  309. nameSuffix = ".recovery"
  310. }
  311. return []android.AndroidMkEntries{android.AndroidMkEntries{
  312. Class: "ETC",
  313. SubName: nameSuffix,
  314. OutputFile: android.OptionalPathForPath(p.outputFilePath),
  315. ExtraEntries: []android.AndroidMkExtraEntriesFunc{
  316. func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
  317. entries.SetString("LOCAL_MODULE_TAGS", "optional")
  318. entries.SetString("LOCAL_MODULE_PATH", p.installDirPath.String())
  319. entries.SetString("LOCAL_INSTALLED_MODULE_STEM", p.outputFilePath.Base())
  320. if len(p.properties.Symlinks) > 0 {
  321. entries.AddStrings("LOCAL_MODULE_SYMLINKS", p.properties.Symlinks...)
  322. }
  323. entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.Installable())
  324. if p.additionalDependencies != nil {
  325. entries.AddStrings("LOCAL_ADDITIONAL_DEPENDENCIES", p.additionalDependencies.Strings()...)
  326. }
  327. },
  328. },
  329. }}
  330. }
  331. func InitPrebuiltEtcModule(p *PrebuiltEtc, dirBase string) {
  332. p.installDirBase = dirBase
  333. p.AddProperties(&p.properties)
  334. p.AddProperties(&p.subdirProperties)
  335. }
  336. func InitPrebuiltRootModule(p *PrebuiltEtc) {
  337. p.installDirBase = "."
  338. p.AddProperties(&p.properties)
  339. }
  340. // prebuilt_etc is for a prebuilt artifact that is installed in
  341. // <partition>/etc/<sub_dir> directory.
  342. func PrebuiltEtcFactory() android.Module {
  343. module := &PrebuiltEtc{}
  344. InitPrebuiltEtcModule(module, "etc")
  345. // This module is device-only
  346. android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
  347. android.InitDefaultableModule(module)
  348. android.InitBazelModule(module)
  349. return module
  350. }
  351. func defaultsFactory() android.Module {
  352. return DefaultsFactory()
  353. }
  354. func DefaultsFactory(props ...interface{}) android.Module {
  355. module := &Defaults{}
  356. module.AddProperties(props...)
  357. module.AddProperties(
  358. &prebuiltEtcProperties{},
  359. &prebuiltSubdirProperties{},
  360. )
  361. android.InitDefaultsModule(module)
  362. return module
  363. }
  364. // prebuilt_etc_host is for a host prebuilt artifact that is installed in
  365. // $(HOST_OUT)/etc/<sub_dir> directory.
  366. func PrebuiltEtcHostFactory() android.Module {
  367. module := &PrebuiltEtc{}
  368. InitPrebuiltEtcModule(module, "etc")
  369. // This module is host-only
  370. android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
  371. android.InitDefaultableModule(module)
  372. android.InitBazelModule(module)
  373. return module
  374. }
  375. // prebuilt_etc_host is for a host prebuilt artifact that is installed in
  376. // <partition>/etc/<sub_dir> directory.
  377. func PrebuiltEtcCaCertsFactory() android.Module {
  378. module := &PrebuiltEtc{}
  379. InitPrebuiltEtcModule(module, "cacerts")
  380. // This module is device-only
  381. android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
  382. android.InitBazelModule(module)
  383. return module
  384. }
  385. // prebuilt_root is for a prebuilt artifact that is installed in
  386. // <partition>/ directory. Can't have any sub directories.
  387. func PrebuiltRootFactory() android.Module {
  388. module := &PrebuiltEtc{}
  389. InitPrebuiltRootModule(module)
  390. // This module is device-only
  391. android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
  392. android.InitDefaultableModule(module)
  393. return module
  394. }
  395. // prebuilt_root_host is for a host prebuilt artifact that is installed in $(HOST_OUT)/<sub_dir>
  396. // directory.
  397. func PrebuiltRootHostFactory() android.Module {
  398. module := &PrebuiltEtc{}
  399. InitPrebuiltEtcModule(module, ".")
  400. // This module is host-only
  401. android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
  402. android.InitDefaultableModule(module)
  403. return module
  404. }
  405. // prebuilt_usr_share is for a prebuilt artifact that is installed in
  406. // <partition>/usr/share/<sub_dir> directory.
  407. func PrebuiltUserShareFactory() android.Module {
  408. module := &PrebuiltEtc{}
  409. InitPrebuiltEtcModule(module, "usr/share")
  410. // This module is device-only
  411. android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
  412. android.InitDefaultableModule(module)
  413. android.InitBazelModule(module)
  414. return module
  415. }
  416. // prebuild_usr_share_host is for a host prebuilt artifact that is installed in
  417. // $(HOST_OUT)/usr/share/<sub_dir> directory.
  418. func PrebuiltUserShareHostFactory() android.Module {
  419. module := &PrebuiltEtc{}
  420. InitPrebuiltEtcModule(module, "usr/share")
  421. // This module is host-only
  422. android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
  423. android.InitDefaultableModule(module)
  424. return module
  425. }
  426. // prebuilt_font installs a font in <partition>/fonts directory.
  427. func PrebuiltFontFactory() android.Module {
  428. module := &PrebuiltEtc{}
  429. InitPrebuiltEtcModule(module, "fonts")
  430. // This module is device-only
  431. android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
  432. android.InitDefaultableModule(module)
  433. return module
  434. }
  435. // prebuilt_firmware installs a firmware file to <partition>/etc/firmware directory for system
  436. // image.
  437. // If soc_specific property is set to true, the firmware file is installed to the
  438. // vendor <partition>/firmware directory for vendor image.
  439. func PrebuiltFirmwareFactory() android.Module {
  440. module := &PrebuiltEtc{}
  441. module.socInstallDirBase = "firmware"
  442. InitPrebuiltEtcModule(module, "etc/firmware")
  443. // This module is device-only
  444. android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
  445. android.InitDefaultableModule(module)
  446. return module
  447. }
  448. // prebuilt_dsp installs a DSP related file to <partition>/etc/dsp directory for system image.
  449. // If soc_specific property is set to true, the DSP related file is installed to the
  450. // vendor <partition>/dsp directory for vendor image.
  451. func PrebuiltDSPFactory() android.Module {
  452. module := &PrebuiltEtc{}
  453. module.socInstallDirBase = "dsp"
  454. InitPrebuiltEtcModule(module, "etc/dsp")
  455. // This module is device-only
  456. android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
  457. android.InitDefaultableModule(module)
  458. return module
  459. }
  460. // prebuilt_rfsa installs a firmware file that will be available through Qualcomm's RFSA
  461. // to the <partition>/lib/rfsa directory.
  462. func PrebuiltRFSAFactory() android.Module {
  463. module := &PrebuiltEtc{}
  464. // Ideally these would go in /vendor/dsp, but the /vendor/lib/rfsa paths are hardcoded in too
  465. // many places outside of the application processor. They could be moved to /vendor/dsp once
  466. // that is cleaned up.
  467. InitPrebuiltEtcModule(module, "lib/rfsa")
  468. // This module is device-only
  469. android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
  470. android.InitDefaultableModule(module)
  471. return module
  472. }
  473. // Copy file into the snapshot
  474. func copyFile(ctx android.SingletonContext, path android.Path, out string, fake bool) android.OutputPath {
  475. if fake {
  476. // Create empty file instead for the fake snapshot
  477. return snapshot.WriteStringToFileRule(ctx, "", out)
  478. } else {
  479. return snapshot.CopyFileRule(pctx, ctx, path, out)
  480. }
  481. }
  482. // Check if the module is target of the snapshot
  483. func isSnapshotAware(ctx android.SingletonContext, m *PrebuiltEtc, image snapshot.SnapshotImage) bool {
  484. if !m.Enabled() {
  485. return false
  486. }
  487. // Skip if the module is not included in the image
  488. if !image.InImage(m)() {
  489. return false
  490. }
  491. // When android/prebuilt.go selects between source and prebuilt, it sets
  492. // HideFromMake on the other one to avoid duplicate install rules in make.
  493. if m.IsHideFromMake() {
  494. return false
  495. }
  496. // There are some prebuilt_etc module with multiple definition of same name.
  497. // Check if the target would be included from the build
  498. if !m.ExportedToMake() {
  499. return false
  500. }
  501. // Skip if the module is in the predefined path list to skip
  502. if image.IsProprietaryPath(ctx.ModuleDir(m), ctx.DeviceConfig()) {
  503. return false
  504. }
  505. // Skip if the module should be excluded
  506. if image.ExcludeFromSnapshot(m) || image.ExcludeFromDirectedSnapshot(ctx.DeviceConfig(), m.BaseModuleName()) {
  507. return false
  508. }
  509. // Skip from other exceptional cases
  510. if m.Target().Os.Class != android.Device {
  511. return false
  512. }
  513. if m.Target().NativeBridge == android.NativeBridgeEnabled {
  514. return false
  515. }
  516. return true
  517. }
  518. func generatePrebuiltSnapshot(s snapshot.SnapshotSingleton, ctx android.SingletonContext, snapshotArchDir string) snapshot.SnapshotPaths {
  519. /*
  520. Snapshot zipped artifacts directory structure for etc modules:
  521. {SNAPSHOT_ARCH}/
  522. arch-{TARGET_ARCH}-{TARGET_ARCH_VARIANT}/
  523. etc/
  524. (prebuilt etc files)
  525. arch-{TARGET_2ND_ARCH}-{TARGET_2ND_ARCH_VARIANT}/
  526. etc/
  527. (prebuilt etc files)
  528. NOTICE_FILES/
  529. (notice files)
  530. */
  531. var snapshotOutputs android.Paths
  532. var snapshotNotices android.Paths
  533. installedNotices := make(map[string]bool)
  534. ctx.VisitAllModules(func(module android.Module) {
  535. m, ok := module.(*PrebuiltEtc)
  536. if !ok {
  537. return
  538. }
  539. if !isSnapshotAware(ctx, m, s.Image) {
  540. return
  541. }
  542. targetArch := "arch-" + m.Target().Arch.ArchType.String()
  543. snapshotLibOut := filepath.Join(snapshotArchDir, targetArch, "etc", m.BaseModuleName())
  544. snapshotOutputs = append(snapshotOutputs, copyFile(ctx, m.OutputFile(), snapshotLibOut, s.Fake))
  545. prop := snapshot.SnapshotJsonFlags{}
  546. propOut := snapshotLibOut + ".json"
  547. prop.InitBaseSnapshotProps(m)
  548. if m.subdirProperties.Relative_install_path != nil {
  549. prop.RelativeInstallPath = *m.subdirProperties.Relative_install_path
  550. }
  551. if m.properties.Filename != nil {
  552. prop.Filename = *m.properties.Filename
  553. }
  554. j, err := json.Marshal(prop)
  555. if err != nil {
  556. ctx.Errorf("json marshal to %q failed: %#v", propOut, err)
  557. return
  558. }
  559. snapshotOutputs = append(snapshotOutputs, snapshot.WriteStringToFileRule(ctx, string(j), propOut))
  560. for _, notice := range m.EffectiveLicenseFiles() {
  561. if _, ok := installedNotices[notice.String()]; !ok {
  562. installedNotices[notice.String()] = true
  563. snapshotNotices = append(snapshotNotices, notice)
  564. }
  565. }
  566. })
  567. return snapshot.SnapshotPaths{OutputFiles: snapshotOutputs, NoticeFiles: snapshotNotices}
  568. }
  569. // For Bazel / bp2build
  570. type bazelPrebuiltFileAttributes struct {
  571. Src bazel.LabelAttribute
  572. Filename bazel.LabelAttribute
  573. Dir string
  574. Installable bazel.BoolAttribute
  575. Filename_from_src bazel.BoolAttribute
  576. }
  577. // Bp2buildHelper returns a bazelPrebuiltFileAttributes used for the conversion
  578. // of prebuilt_* modules. bazelPrebuiltFileAttributes has the common attributes
  579. // used by both prebuilt_etc_xml and other prebuilt_* moodules
  580. func (module *PrebuiltEtc) Bp2buildHelper(ctx android.TopDownMutatorContext) *bazelPrebuiltFileAttributes {
  581. var src bazel.LabelAttribute
  582. for axis, configToProps := range module.GetArchVariantProperties(ctx, &prebuiltEtcProperties{}) {
  583. for config, p := range configToProps {
  584. props, ok := p.(*prebuiltEtcProperties)
  585. if !ok {
  586. continue
  587. }
  588. if props.Src != nil {
  589. label := android.BazelLabelForModuleSrcSingle(ctx, *props.Src)
  590. src.SetSelectValue(axis, config, label)
  591. }
  592. }
  593. for propName, productConfigProps := range android.ProductVariableProperties(ctx, ctx.Module()) {
  594. for configProp, propVal := range productConfigProps {
  595. if propName == "Src" {
  596. props, ok := propVal.(*string)
  597. if !ok {
  598. ctx.PropertyErrorf(" Expected Property to have type string, but was %s\n", reflect.TypeOf(propVal).String())
  599. continue
  600. }
  601. if props != nil {
  602. label := android.BazelLabelForModuleSrcSingle(ctx, *props)
  603. src.SetSelectValue(configProp.ConfigurationAxis(), configProp.SelectKey(), label)
  604. }
  605. }
  606. }
  607. }
  608. }
  609. var filename string
  610. var filenameFromSrc bool
  611. moduleProps := module.properties
  612. if moduleProps.Filename != nil && *moduleProps.Filename != "" {
  613. filename = *moduleProps.Filename
  614. } else if moduleProps.Filename_from_src != nil && *moduleProps.Filename_from_src {
  615. if moduleProps.Src != nil {
  616. filename = *moduleProps.Src
  617. }
  618. filenameFromSrc = true
  619. } else {
  620. filename = ctx.ModuleName()
  621. }
  622. var dir = module.installDirBase
  623. if subDir := module.subdirProperties.Sub_dir; subDir != nil {
  624. dir = dir + "/" + *subDir
  625. }
  626. var installable bazel.BoolAttribute
  627. if install := module.properties.Installable; install != nil {
  628. installable.Value = install
  629. }
  630. attrs := &bazelPrebuiltFileAttributes{
  631. Src: src,
  632. Dir: dir,
  633. Installable: installable,
  634. }
  635. if filename != "" {
  636. attrs.Filename = bazel.LabelAttribute{Value: &bazel.Label{Label: filename}}
  637. } else if filenameFromSrc {
  638. attrs.Filename_from_src = bazel.BoolAttribute{Value: moduleProps.Filename_from_src}
  639. }
  640. return attrs
  641. }
  642. // ConvertWithBp2build performs bp2build conversion of PrebuiltEtc
  643. // prebuilt_* modules (except prebuilt_etc_xml) are PrebuiltEtc,
  644. // which we treat as *PrebuiltFile*
  645. func (module *PrebuiltEtc) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
  646. var dir = module.installDirBase
  647. // prebuilt_file supports only `etc` or `usr/share`
  648. if !(dir == "etc" || dir == "usr/share") {
  649. return
  650. }
  651. attrs := module.Bp2buildHelper(ctx)
  652. props := bazel.BazelTargetModuleProperties{
  653. Rule_class: "prebuilt_file",
  654. Bzl_load_location: "//build/bazel/rules:prebuilt_file.bzl",
  655. }
  656. ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, attrs)
  657. }