snapshot_prebuilt.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright 2021 The Android Open Source Project
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package rust
  15. import (
  16. "android/soong/android"
  17. "android/soong/cc"
  18. "github.com/google/blueprint/proptools"
  19. )
  20. const (
  21. snapshotRlibSuffix = "_rlib."
  22. )
  23. type snapshotLibraryDecorator struct {
  24. cc.BaseSnapshotDecorator
  25. *libraryDecorator
  26. properties cc.SnapshotLibraryProperties
  27. sanitizerProperties struct {
  28. CfiEnabled bool `blueprint:"mutated"`
  29. // Library flags for cfi variant.
  30. Cfi cc.SnapshotLibraryProperties `android:"arch_variant"`
  31. }
  32. }
  33. func init() {
  34. registerRustSnapshotModules(android.InitRegistrationContext)
  35. }
  36. func registerRustSnapshotModules(ctx android.RegistrationContext) {
  37. cc.VendorSnapshotImageSingleton.RegisterAdditionalModule(ctx,
  38. "vendor_snapshot_rlib", VendorSnapshotRlibFactory)
  39. cc.RecoverySnapshotImageSingleton.RegisterAdditionalModule(ctx,
  40. "recovery_snapshot_rlib", RecoverySnapshotRlibFactory)
  41. }
  42. func snapshotLibraryFactory(image cc.SnapshotImage, moduleSuffix string) (*Module, *snapshotLibraryDecorator) {
  43. module, library := NewRustLibrary(android.DeviceSupported)
  44. module.sanitize = nil
  45. library.stripper.StripProperties.Strip.None = proptools.BoolPtr(true)
  46. prebuilt := &snapshotLibraryDecorator{
  47. libraryDecorator: library,
  48. }
  49. module.compiler = prebuilt
  50. prebuilt.Init(module, image, moduleSuffix)
  51. module.AddProperties(
  52. &prebuilt.properties,
  53. &prebuilt.sanitizerProperties,
  54. )
  55. return module, prebuilt
  56. }
  57. func (library *snapshotLibraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
  58. var variant string
  59. if library.static() {
  60. variant = cc.SnapshotStaticSuffix
  61. } else if library.shared() {
  62. variant = cc.SnapshotSharedSuffix
  63. } else if library.rlib() {
  64. variant = cc.SnapshotRlibSuffix
  65. }
  66. if !library.dylib() {
  67. // TODO(184042776): Remove this check when dylibs are supported in snapshots.
  68. library.SetSnapshotAndroidMkSuffix(ctx, variant)
  69. }
  70. if !library.MatchesWithDevice(ctx.DeviceConfig()) {
  71. return buildOutput{}
  72. }
  73. outputFile := android.PathForModuleSrc(ctx, *library.properties.Src)
  74. library.unstrippedOutputFile = outputFile
  75. return buildOutput{outputFile: outputFile}
  76. }
  77. func (library *snapshotLibraryDecorator) rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath {
  78. return android.OptionalPath{}
  79. }
  80. // vendor_snapshot_rlib is a special prebuilt rlib library which is auto-generated by
  81. // development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_rlib
  82. // overrides the vendor variant of the rust rlib library with the same name, if BOARD_VNDK_VERSION
  83. // is set.
  84. func VendorSnapshotRlibFactory() android.Module {
  85. module, prebuilt := snapshotLibraryFactory(cc.VendorSnapshotImageSingleton, cc.SnapshotRlibSuffix)
  86. prebuilt.libraryDecorator.BuildOnlyRlib()
  87. prebuilt.libraryDecorator.setNoStdlibs()
  88. return module.Init()
  89. }
  90. func RecoverySnapshotRlibFactory() android.Module {
  91. module, prebuilt := snapshotLibraryFactory(cc.RecoverySnapshotImageSingleton, cc.SnapshotRlibSuffix)
  92. prebuilt.libraryDecorator.BuildOnlyRlib()
  93. prebuilt.libraryDecorator.setNoStdlibs()
  94. return module.Init()
  95. }
  96. func (library *snapshotLibraryDecorator) MatchesWithDevice(config android.DeviceConfig) bool {
  97. arches := config.Arches()
  98. if len(arches) == 0 || arches[0].ArchType.String() != library.Arch() {
  99. return false
  100. }
  101. if library.properties.Src == nil {
  102. return false
  103. }
  104. return true
  105. }
  106. func (library *snapshotLibraryDecorator) IsSnapshotPrebuilt() bool {
  107. return true
  108. }
  109. var _ cc.SnapshotInterface = (*snapshotLibraryDecorator)(nil)