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