blob: 32d3916295a101e08606244c42f949d670e18bd4 [file] [log] [blame]
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001// 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
15package rust
16
17import (
18 "android/soong/android"
19 "android/soong/cc"
Kiyoung Kim48f37782021-07-07 12:42:39 +090020
Ivan Lozano3149e6e2021-06-01 15:09:53 -040021 "github.com/google/blueprint/proptools"
22)
23
Ivan Lozano3149e6e2021-06-01 15:09:53 -040024type snapshotLibraryDecorator struct {
25 cc.BaseSnapshotDecorator
26 *libraryDecorator
27 properties cc.SnapshotLibraryProperties
28 sanitizerProperties struct {
29 CfiEnabled bool `blueprint:"mutated"`
30
31 // Library flags for cfi variant.
32 Cfi cc.SnapshotLibraryProperties `android:"arch_variant"`
33 }
34}
35
36func init() {
37 registerRustSnapshotModules(android.InitRegistrationContext)
38}
39
40func registerRustSnapshotModules(ctx android.RegistrationContext) {
41 cc.VendorSnapshotImageSingleton.RegisterAdditionalModule(ctx,
42 "vendor_snapshot_rlib", VendorSnapshotRlibFactory)
Ivan Lozanoadd122a2023-07-13 11:01:41 -040043 cc.VendorSnapshotImageSingleton.RegisterAdditionalModule(ctx,
44 "vendor_snapshot_dylib", VendorSnapshotDylibFactory)
Jose Galmesd7d99be2021-11-05 14:04:54 -070045 cc.RecoverySnapshotImageSingleton.RegisterAdditionalModule(ctx,
46 "recovery_snapshot_rlib", RecoverySnapshotRlibFactory)
Ivan Lozano3149e6e2021-06-01 15:09:53 -040047}
48
49func snapshotLibraryFactory(image cc.SnapshotImage, moduleSuffix string) (*Module, *snapshotLibraryDecorator) {
50 module, library := NewRustLibrary(android.DeviceSupported)
51
52 module.sanitize = nil
53 library.stripper.StripProperties.Strip.None = proptools.BoolPtr(true)
54
55 prebuilt := &snapshotLibraryDecorator{
56 libraryDecorator: library,
57 }
58
59 module.compiler = prebuilt
60
61 prebuilt.Init(module, image, moduleSuffix)
62 module.AddProperties(
63 &prebuilt.properties,
64 &prebuilt.sanitizerProperties,
65 )
66
67 return module, prebuilt
68}
69
Sasha Smundaka76acba2022-04-18 20:12:56 -070070func (library *snapshotLibraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
Ivan Lozano3149e6e2021-06-01 15:09:53 -040071 var variant string
72 if library.static() {
73 variant = cc.SnapshotStaticSuffix
74 } else if library.shared() {
75 variant = cc.SnapshotSharedSuffix
76 } else if library.rlib() {
77 variant = cc.SnapshotRlibSuffix
Ivan Lozanoadd122a2023-07-13 11:01:41 -040078 } else if library.dylib() {
79 variant = cc.SnapshotDylibSuffix
Ivan Lozano3149e6e2021-06-01 15:09:53 -040080 }
81
Ivan Lozanoadd122a2023-07-13 11:01:41 -040082 library.SetSnapshotAndroidMkSuffix(ctx, variant)
Ivan Lozano3149e6e2021-06-01 15:09:53 -040083
84 if !library.MatchesWithDevice(ctx.DeviceConfig()) {
Sasha Smundaka76acba2022-04-18 20:12:56 -070085 return buildOutput{}
Ivan Lozano3149e6e2021-06-01 15:09:53 -040086 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -040087 outputFile := android.PathForModuleSrc(ctx, *library.properties.Src)
88 library.unstrippedOutputFile = outputFile
Sasha Smundaka76acba2022-04-18 20:12:56 -070089 return buildOutput{outputFile: outputFile}
Ivan Lozano3149e6e2021-06-01 15:09:53 -040090}
91
92func (library *snapshotLibraryDecorator) rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath {
93 return android.OptionalPath{}
94}
95
96// vendor_snapshot_rlib is a special prebuilt rlib library which is auto-generated by
97// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_rlib
98// overrides the vendor variant of the rust rlib library with the same name, if BOARD_VNDK_VERSION
99// is set.
100func VendorSnapshotRlibFactory() android.Module {
101 module, prebuilt := snapshotLibraryFactory(cc.VendorSnapshotImageSingleton, cc.SnapshotRlibSuffix)
102 prebuilt.libraryDecorator.BuildOnlyRlib()
103 prebuilt.libraryDecorator.setNoStdlibs()
104 return module.Init()
105}
106
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400107// vendor_snapshot_dylib is a special prebuilt dylib library which is auto-generated by
108// development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_dylib
109// overrides the vendor variant of the rust dylib library with the same name, if BOARD_VNDK_VERSION
110// is set.
111func VendorSnapshotDylibFactory() android.Module {
112 module, prebuilt := snapshotLibraryFactory(cc.VendorSnapshotImageSingleton, cc.SnapshotDylibSuffix)
113 prebuilt.libraryDecorator.BuildOnlyDylib()
114 prebuilt.libraryDecorator.setNoStdlibs()
115 return module.Init()
116}
117
Jose Galmesd7d99be2021-11-05 14:04:54 -0700118func RecoverySnapshotRlibFactory() android.Module {
119 module, prebuilt := snapshotLibraryFactory(cc.RecoverySnapshotImageSingleton, cc.SnapshotRlibSuffix)
120 prebuilt.libraryDecorator.BuildOnlyRlib()
121 prebuilt.libraryDecorator.setNoStdlibs()
122 return module.Init()
123}
124
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400125func (library *snapshotLibraryDecorator) MatchesWithDevice(config android.DeviceConfig) bool {
126 arches := config.Arches()
127 if len(arches) == 0 || arches[0].ArchType.String() != library.Arch() {
128 return false
129 }
130 if library.properties.Src == nil {
131 return false
132 }
133 return true
134}
135
136func (library *snapshotLibraryDecorator) IsSnapshotPrebuilt() bool {
137 return true
138}
139
140var _ cc.SnapshotInterface = (*snapshotLibraryDecorator)(nil)