Ivan Lozano | 3149e6e | 2021-06-01 15:09:53 -0400 | [diff] [blame] | 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 | |
| 15 | package rust |
| 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
| 19 | "android/soong/cc" |
Kiyoung Kim | 48f3778 | 2021-07-07 12:42:39 +0900 | [diff] [blame] | 20 | |
Ivan Lozano | 3149e6e | 2021-06-01 15:09:53 -0400 | [diff] [blame] | 21 | "github.com/google/blueprint/proptools" |
| 22 | ) |
| 23 | |
| 24 | const ( |
| 25 | snapshotRlibSuffix = "_rlib." |
| 26 | ) |
| 27 | |
| 28 | type snapshotLibraryDecorator struct { |
| 29 | cc.BaseSnapshotDecorator |
| 30 | *libraryDecorator |
| 31 | properties cc.SnapshotLibraryProperties |
| 32 | sanitizerProperties struct { |
| 33 | CfiEnabled bool `blueprint:"mutated"` |
| 34 | |
| 35 | // Library flags for cfi variant. |
| 36 | Cfi cc.SnapshotLibraryProperties `android:"arch_variant"` |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func init() { |
| 41 | registerRustSnapshotModules(android.InitRegistrationContext) |
| 42 | } |
| 43 | |
| 44 | func registerRustSnapshotModules(ctx android.RegistrationContext) { |
| 45 | cc.VendorSnapshotImageSingleton.RegisterAdditionalModule(ctx, |
| 46 | "vendor_snapshot_rlib", VendorSnapshotRlibFactory) |
Jose Galmes | d7d99be | 2021-11-05 14:04:54 -0700 | [diff] [blame] | 47 | cc.RecoverySnapshotImageSingleton.RegisterAdditionalModule(ctx, |
| 48 | "recovery_snapshot_rlib", RecoverySnapshotRlibFactory) |
Ivan Lozano | 3149e6e | 2021-06-01 15:09:53 -0400 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | func snapshotLibraryFactory(image cc.SnapshotImage, moduleSuffix string) (*Module, *snapshotLibraryDecorator) { |
| 52 | module, library := NewRustLibrary(android.DeviceSupported) |
| 53 | |
| 54 | module.sanitize = nil |
| 55 | library.stripper.StripProperties.Strip.None = proptools.BoolPtr(true) |
| 56 | |
| 57 | prebuilt := &snapshotLibraryDecorator{ |
| 58 | libraryDecorator: library, |
| 59 | } |
| 60 | |
| 61 | module.compiler = prebuilt |
| 62 | |
| 63 | prebuilt.Init(module, image, moduleSuffix) |
| 64 | module.AddProperties( |
| 65 | &prebuilt.properties, |
| 66 | &prebuilt.sanitizerProperties, |
| 67 | ) |
| 68 | |
| 69 | return module, prebuilt |
| 70 | } |
| 71 | |
| 72 | func (library *snapshotLibraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path { |
| 73 | var variant string |
| 74 | if library.static() { |
| 75 | variant = cc.SnapshotStaticSuffix |
| 76 | } else if library.shared() { |
| 77 | variant = cc.SnapshotSharedSuffix |
| 78 | } else if library.rlib() { |
| 79 | variant = cc.SnapshotRlibSuffix |
| 80 | } |
| 81 | |
| 82 | if !library.dylib() { |
| 83 | // TODO(184042776): Remove this check when dylibs are supported in snapshots. |
| 84 | library.SetSnapshotAndroidMkSuffix(ctx, variant) |
| 85 | } |
| 86 | |
| 87 | if !library.MatchesWithDevice(ctx.DeviceConfig()) { |
| 88 | return nil |
| 89 | } |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 90 | outputFile := android.PathForModuleSrc(ctx, *library.properties.Src) |
| 91 | library.unstrippedOutputFile = outputFile |
| 92 | return outputFile |
Ivan Lozano | 3149e6e | 2021-06-01 15:09:53 -0400 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | func (library *snapshotLibraryDecorator) rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath { |
| 96 | return android.OptionalPath{} |
| 97 | } |
| 98 | |
| 99 | // vendor_snapshot_rlib is a special prebuilt rlib library which is auto-generated by |
| 100 | // development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_rlib |
| 101 | // overrides the vendor variant of the rust rlib library with the same name, if BOARD_VNDK_VERSION |
| 102 | // is set. |
| 103 | func VendorSnapshotRlibFactory() android.Module { |
| 104 | module, prebuilt := snapshotLibraryFactory(cc.VendorSnapshotImageSingleton, cc.SnapshotRlibSuffix) |
| 105 | prebuilt.libraryDecorator.BuildOnlyRlib() |
| 106 | prebuilt.libraryDecorator.setNoStdlibs() |
| 107 | return module.Init() |
| 108 | } |
| 109 | |
Jose Galmes | d7d99be | 2021-11-05 14:04:54 -0700 | [diff] [blame] | 110 | func RecoverySnapshotRlibFactory() android.Module { |
| 111 | module, prebuilt := snapshotLibraryFactory(cc.RecoverySnapshotImageSingleton, cc.SnapshotRlibSuffix) |
| 112 | prebuilt.libraryDecorator.BuildOnlyRlib() |
| 113 | prebuilt.libraryDecorator.setNoStdlibs() |
| 114 | return module.Init() |
| 115 | } |
| 116 | |
Ivan Lozano | 3149e6e | 2021-06-01 15:09:53 -0400 | [diff] [blame] | 117 | func (library *snapshotLibraryDecorator) MatchesWithDevice(config android.DeviceConfig) bool { |
| 118 | arches := config.Arches() |
| 119 | if len(arches) == 0 || arches[0].ArchType.String() != library.Arch() { |
| 120 | return false |
| 121 | } |
| 122 | if library.properties.Src == nil { |
| 123 | return false |
| 124 | } |
| 125 | return true |
| 126 | } |
| 127 | |
| 128 | func (library *snapshotLibraryDecorator) IsSnapshotPrebuilt() bool { |
| 129 | return true |
| 130 | } |
| 131 | |
| 132 | var _ cc.SnapshotInterface = (*snapshotLibraryDecorator)(nil) |