blob: 79eaab382ba3adccb489f4e09c9ecbfe2ee1891a [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
24const (
25 snapshotRlibSuffix = "_rlib."
26)
27
28type 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
40func init() {
41 registerRustSnapshotModules(android.InitRegistrationContext)
42}
43
44func registerRustSnapshotModules(ctx android.RegistrationContext) {
45 cc.VendorSnapshotImageSingleton.RegisterAdditionalModule(ctx,
46 "vendor_snapshot_rlib", VendorSnapshotRlibFactory)
47}
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
70func (library *snapshotLibraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
71 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
78 }
79
80 if !library.dylib() {
81 // TODO(184042776): Remove this check when dylibs are supported in snapshots.
82 library.SetSnapshotAndroidMkSuffix(ctx, variant)
83 }
84
85 if !library.MatchesWithDevice(ctx.DeviceConfig()) {
86 return nil
87 }
88
89 return android.PathForModuleSrc(ctx, *library.properties.Src)
90}
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
107func (library *snapshotLibraryDecorator) MatchesWithDevice(config android.DeviceConfig) bool {
108 arches := config.Arches()
109 if len(arches) == 0 || arches[0].ArchType.String() != library.Arch() {
110 return false
111 }
112 if library.properties.Src == nil {
113 return false
114 }
115 return true
116}
117
118func (library *snapshotLibraryDecorator) IsSnapshotPrebuilt() bool {
119 return true
120}
121
122var _ cc.SnapshotInterface = (*snapshotLibraryDecorator)(nil)