blob: 2c29a89aaaa1375353edc8fb7b8c49a18af42c36 [file] [log] [blame]
Inseob Kim8471cda2019-11-15 09:59:12 +09001// Copyright 2020 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.
14package cc
15
Inseob Kimde5744a2020-12-02 13:14:28 +090016// This file contains singletons to capture vendor and recovery snapshot. They consist of prebuilt
17// modules under AOSP so older vendor and recovery can be built with a newer system in a single
18// source tree.
19
Inseob Kim8471cda2019-11-15 09:59:12 +090020import (
21 "encoding/json"
22 "path/filepath"
23 "sort"
24 "strings"
25
Inseob Kim8471cda2019-11-15 09:59:12 +090026 "android/soong/android"
27)
28
Jose Galmesf7294582020-11-13 12:07:36 -080029var vendorSnapshotSingleton = snapshotSingleton{
30 "vendor",
31 "SOONG_VENDOR_SNAPSHOT_ZIP",
32 android.OptionalPath{},
33 true,
Ivan Lozanod67a6b02021-05-20 13:01:32 -040034 VendorSnapshotImageSingleton,
Inseob Kime9aec6a2021-01-05 20:03:22 +090035 false, /* fake */
36}
37
38var vendorFakeSnapshotSingleton = snapshotSingleton{
39 "vendor",
40 "SOONG_VENDOR_FAKE_SNAPSHOT_ZIP",
41 android.OptionalPath{},
42 true,
Ivan Lozanod67a6b02021-05-20 13:01:32 -040043 VendorSnapshotImageSingleton,
Inseob Kime9aec6a2021-01-05 20:03:22 +090044 true, /* fake */
Jose Galmesf7294582020-11-13 12:07:36 -080045}
46
47var recoverySnapshotSingleton = snapshotSingleton{
48 "recovery",
49 "SOONG_RECOVERY_SNAPSHOT_ZIP",
50 android.OptionalPath{},
51 false,
Inseob Kimde5744a2020-12-02 13:14:28 +090052 recoverySnapshotImageSingleton,
Inseob Kime9aec6a2021-01-05 20:03:22 +090053 false, /* fake */
Inseob Kim8471cda2019-11-15 09:59:12 +090054}
55
56func VendorSnapshotSingleton() android.Singleton {
Jose Galmesf7294582020-11-13 12:07:36 -080057 return &vendorSnapshotSingleton
Inseob Kim8471cda2019-11-15 09:59:12 +090058}
59
Inseob Kime9aec6a2021-01-05 20:03:22 +090060func VendorFakeSnapshotSingleton() android.Singleton {
61 return &vendorFakeSnapshotSingleton
62}
63
Jose Galmesf7294582020-11-13 12:07:36 -080064func RecoverySnapshotSingleton() android.Singleton {
65 return &recoverySnapshotSingleton
66}
67
68type snapshotSingleton struct {
69 // Name, e.g., "vendor", "recovery", "ramdisk".
70 name string
71
72 // Make variable that points to the snapshot file, e.g.,
73 // "SOONG_RECOVERY_SNAPSHOT_ZIP".
74 makeVar string
75
76 // Path to the snapshot zip file.
77 snapshotZipFile android.OptionalPath
78
79 // Whether the image supports VNDK extension modules.
80 supportsVndkExt bool
81
82 // Implementation of the image interface specific to the image
83 // associated with this snapshot (e.g., specific to the vendor image,
84 // recovery image, etc.).
Inseob Kimde5744a2020-12-02 13:14:28 +090085 image snapshotImage
Inseob Kime9aec6a2021-01-05 20:03:22 +090086
87 // Whether this singleton is for fake snapshot or not.
88 // Fake snapshot is a snapshot whose prebuilt binaries and headers are empty.
89 // It is much faster to generate, and can be used to inspect dependencies.
90 fake bool
Inseob Kim8471cda2019-11-15 09:59:12 +090091}
92
Justin DeMartino383bfb32021-02-24 10:49:43 -080093// Determine if a dir under source tree is an SoC-owned proprietary directory based
94// on vendor snapshot configuration
95// Examples: device/, vendor/
96func isVendorProprietaryPath(dir string, deviceConfig android.DeviceConfig) bool {
97 return VendorSnapshotSingleton().(*snapshotSingleton).image.isProprietaryPath(dir, deviceConfig)
Jose Galmesf7294582020-11-13 12:07:36 -080098}
99
Justin DeMartino383bfb32021-02-24 10:49:43 -0800100// Determine if a dir under source tree is an SoC-owned proprietary directory based
101// on recovery snapshot configuration
102// Examples: device/, vendor/
103func isRecoveryProprietaryPath(dir string, deviceConfig android.DeviceConfig) bool {
104 return RecoverySnapshotSingleton().(*snapshotSingleton).image.isProprietaryPath(dir, deviceConfig)
Inseob Kim8471cda2019-11-15 09:59:12 +0900105}
106
Ivan Lozanod67a6b02021-05-20 13:01:32 -0400107func IsVendorProprietaryModule(ctx android.BaseModuleContext) bool {
Bill Peckham945441c2020-08-31 16:07:58 -0700108 // Any module in a vendor proprietary path is a vendor proprietary
109 // module.
Justin DeMartino383bfb32021-02-24 10:49:43 -0800110 if isVendorProprietaryPath(ctx.ModuleDir(), ctx.DeviceConfig()) {
Bill Peckham945441c2020-08-31 16:07:58 -0700111 return true
112 }
113
114 // However if the module is not in a vendor proprietary path, it may
115 // still be a vendor proprietary module. This happens for cc modules
116 // that are excluded from the vendor snapshot, and it means that the
117 // vendor has assumed control of the framework-provided module.
Ivan Lozanod7586b62021-04-01 09:49:36 -0400118 if c, ok := ctx.Module().(LinkableInterface); ok {
Bill Peckham945441c2020-08-31 16:07:58 -0700119 if c.ExcludeFromVendorSnapshot() {
120 return true
121 }
122 }
123
124 return false
125}
126
Jose Galmes6f843bc2020-12-11 13:36:29 -0800127func isRecoveryProprietaryModule(ctx android.BaseModuleContext) bool {
128
Justin Yune09ac172021-01-20 19:49:01 +0900129 // Any module in a recovery proprietary path is a recovery proprietary
Jose Galmes6f843bc2020-12-11 13:36:29 -0800130 // module.
Justin DeMartino383bfb32021-02-24 10:49:43 -0800131 if isRecoveryProprietaryPath(ctx.ModuleDir(), ctx.DeviceConfig()) {
Jose Galmes6f843bc2020-12-11 13:36:29 -0800132 return true
133 }
134
Justin Yune09ac172021-01-20 19:49:01 +0900135 // However if the module is not in a recovery proprietary path, it may
136 // still be a recovery proprietary module. This happens for cc modules
137 // that are excluded from the recovery snapshot, and it means that the
Jose Galmes6f843bc2020-12-11 13:36:29 -0800138 // vendor has assumed control of the framework-provided module.
139
Ivan Lozanod7586b62021-04-01 09:49:36 -0400140 if c, ok := ctx.Module().(LinkableInterface); ok {
Jose Galmes6f843bc2020-12-11 13:36:29 -0800141 if c.ExcludeFromRecoverySnapshot() {
142 return true
143 }
144 }
145
146 return false
147}
148
Inseob Kimde5744a2020-12-02 13:14:28 +0900149// Determines if the module is a candidate for snapshot.
Ivan Lozanod7586b62021-04-01 09:49:36 -0400150func isSnapshotAware(cfg android.DeviceConfig, m LinkableInterface, inProprietaryPath bool, apexInfo android.ApexInfo, image snapshotImage) bool {
151 if !m.Enabled() || m.HiddenFromMake() {
Inseob Kim8471cda2019-11-15 09:59:12 +0900152 return false
153 }
Martin Stjernholm809d5182020-09-10 01:46:05 +0100154 // When android/prebuilt.go selects between source and prebuilt, it sets
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800155 // HideFromMake on the other one to avoid duplicate install rules in make.
156 if m.IsHideFromMake() {
Martin Stjernholm809d5182020-09-10 01:46:05 +0100157 return false
158 }
Jose Galmesf7294582020-11-13 12:07:36 -0800159 // skip proprietary modules, but (for the vendor snapshot only)
160 // include all VNDK (static)
161 if inProprietaryPath && (!image.includeVndk() || !m.IsVndk()) {
Bill Peckham945441c2020-08-31 16:07:58 -0700162 return false
163 }
164 // If the module would be included based on its path, check to see if
165 // the module is marked to be excluded. If so, skip it.
Jose Galmes6f843bc2020-12-11 13:36:29 -0800166 if image.excludeFromSnapshot(m) {
Inseob Kim8471cda2019-11-15 09:59:12 +0900167 return false
168 }
169 if m.Target().Os.Class != android.Device {
170 return false
171 }
172 if m.Target().NativeBridge == android.NativeBridgeEnabled {
173 return false
174 }
Inseob Kimde5744a2020-12-02 13:14:28 +0900175 // the module must be installed in target image
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400176 if !apexInfo.IsForPlatform() || m.IsSnapshotPrebuilt() || !image.inImage(m)() {
Inseob Kim8471cda2019-11-15 09:59:12 +0900177 return false
178 }
Inseob Kim65ca36a2020-06-11 13:55:45 +0900179 // skip kernel_headers which always depend on vendor
Ivan Lozanod7586b62021-04-01 09:49:36 -0400180 if m.KernelHeadersDecorator() {
Inseob Kim65ca36a2020-06-11 13:55:45 +0900181 return false
182 }
Ivan Lozanod7586b62021-04-01 09:49:36 -0400183
Colin Cross127bb8b2020-12-16 16:46:01 -0800184 if m.IsLlndk() {
185 return false
186 }
Inseob Kim8471cda2019-11-15 09:59:12 +0900187
188 // Libraries
Ivan Lozanod7586b62021-04-01 09:49:36 -0400189 if sanitizable, ok := m.(PlatformSanitizeable); ok && sanitizable.IsSnapshotLibrary() {
190 if sanitizable.SanitizePropDefined() {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900191 // scs and hwasan export both sanitized and unsanitized variants for static and header
Inseob Kim7f283f42020-06-01 21:53:49 +0900192 // Always use unsanitized variants of them.
Tri Vo6eafc362021-04-01 11:29:09 -0700193 for _, t := range []SanitizerType{scs, Hwasan} {
Ivan Lozanod7586b62021-04-01 09:49:36 -0400194 if !sanitizable.Shared() && sanitizable.IsSanitizerEnabled(t) {
Inseob Kim7f283f42020-06-01 21:53:49 +0900195 return false
196 }
197 }
Inseob Kimc42f2f22020-07-29 20:32:10 +0900198 // cfi also exports both variants. But for static, we capture both.
Inseob Kimde5744a2020-12-02 13:14:28 +0900199 // This is because cfi static libraries can't be linked from non-cfi modules,
200 // and vice versa. This isn't the case for scs and hwasan sanitizers.
Ivan Lozanod7586b62021-04-01 09:49:36 -0400201 if !sanitizable.Static() && !sanitizable.Shared() && sanitizable.IsSanitizerEnabled(cfi) {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900202 return false
203 }
Inseob Kim7f283f42020-06-01 21:53:49 +0900204 }
Ivan Lozanod7586b62021-04-01 09:49:36 -0400205 if sanitizable.Static() {
206 return sanitizable.OutputFile().Valid() && !image.private(m)
Inseob Kim8471cda2019-11-15 09:59:12 +0900207 }
Ivan Lozanod7586b62021-04-01 09:49:36 -0400208 if sanitizable.Shared() {
209 if !sanitizable.OutputFile().Valid() {
Bill Peckham7d3f0962020-06-29 16:49:15 -0700210 return false
211 }
Jose Galmesf7294582020-11-13 12:07:36 -0800212 if image.includeVndk() {
Ivan Lozanod7586b62021-04-01 09:49:36 -0400213 if !sanitizable.IsVndk() {
Jose Galmesf7294582020-11-13 12:07:36 -0800214 return true
215 }
Ivan Lozanod7586b62021-04-01 09:49:36 -0400216 return sanitizable.IsVndkExt()
Bill Peckham7d3f0962020-06-29 16:49:15 -0700217 }
Inseob Kim8471cda2019-11-15 09:59:12 +0900218 }
219 return true
220 }
221
Inseob Kim1042d292020-06-01 23:23:05 +0900222 // Binaries and Objects
Ivan Lozanod7586b62021-04-01 09:49:36 -0400223 if m.Binary() || m.Object() {
224 return m.OutputFile().Valid()
Inseob Kim8471cda2019-11-15 09:59:12 +0900225 }
Inseob Kim7f283f42020-06-01 21:53:49 +0900226
227 return false
Inseob Kim8471cda2019-11-15 09:59:12 +0900228}
229
Inseob Kimde5744a2020-12-02 13:14:28 +0900230// This is to be saved as .json files, which is for development/vendor_snapshot/update.py.
231// These flags become Android.bp snapshot module properties.
232type snapshotJsonFlags struct {
233 ModuleName string `json:",omitempty"`
234 RelativeInstallPath string `json:",omitempty"`
235
236 // library flags
237 ExportedDirs []string `json:",omitempty"`
238 ExportedSystemDirs []string `json:",omitempty"`
239 ExportedFlags []string `json:",omitempty"`
240 Sanitize string `json:",omitempty"`
241 SanitizeMinimalDep bool `json:",omitempty"`
242 SanitizeUbsanDep bool `json:",omitempty"`
243
244 // binary flags
245 Symlinks []string `json:",omitempty"`
246
247 // dependencies
248 SharedLibs []string `json:",omitempty"`
249 RuntimeLibs []string `json:",omitempty"`
250 Required []string `json:",omitempty"`
251
252 // extra config files
253 InitRc []string `json:",omitempty"`
254 VintfFragments []string `json:",omitempty"`
255}
256
Jose Galmesf7294582020-11-13 12:07:36 -0800257func (c *snapshotSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Jose Galmes6f843bc2020-12-11 13:36:29 -0800258 if !c.image.shouldGenerateSnapshot(ctx) {
Inseob Kim8471cda2019-11-15 09:59:12 +0900259 return
260 }
261
262 var snapshotOutputs android.Paths
263
264 /*
265 Vendor snapshot zipped artifacts directory structure:
266 {SNAPSHOT_ARCH}/
267 arch-{TARGET_ARCH}-{TARGET_ARCH_VARIANT}/
268 shared/
269 (.so shared libraries)
270 static/
271 (.a static libraries)
272 header/
273 (header only libraries)
274 binary/
275 (executable binaries)
Inseob Kim1042d292020-06-01 23:23:05 +0900276 object/
277 (.o object files)
Inseob Kim8471cda2019-11-15 09:59:12 +0900278 arch-{TARGET_2ND_ARCH}-{TARGET_2ND_ARCH_VARIANT}/
279 shared/
280 (.so shared libraries)
281 static/
282 (.a static libraries)
283 header/
284 (header only libraries)
285 binary/
286 (executable binaries)
Inseob Kim1042d292020-06-01 23:23:05 +0900287 object/
288 (.o object files)
Inseob Kim8471cda2019-11-15 09:59:12 +0900289 NOTICE_FILES/
290 (notice files, e.g. libbase.txt)
291 configs/
292 (config files, e.g. init.rc files, vintf_fragments.xml files, etc.)
293 include/
294 (header files of same directory structure with source tree)
295 */
296
Jose Galmesf7294582020-11-13 12:07:36 -0800297 snapshotDir := c.name + "-snapshot"
Inseob Kime9aec6a2021-01-05 20:03:22 +0900298 if c.fake {
299 // If this is a fake snapshot singleton, place all files under fake/ subdirectory to avoid
300 // collision with real snapshot files
301 snapshotDir = filepath.Join("fake", snapshotDir)
302 }
Inseob Kim8471cda2019-11-15 09:59:12 +0900303 snapshotArchDir := filepath.Join(snapshotDir, ctx.DeviceConfig().DeviceArch())
304
305 includeDir := filepath.Join(snapshotArchDir, "include")
306 configsDir := filepath.Join(snapshotArchDir, "configs")
307 noticeDir := filepath.Join(snapshotArchDir, "NOTICE_FILES")
308
309 installedNotices := make(map[string]bool)
310 installedConfigs := make(map[string]bool)
311
312 var headers android.Paths
313
Jose Galmes0a942a02021-02-03 14:23:15 -0800314 copyFile := func(ctx android.SingletonContext, path android.Path, out string, fake bool) android.OutputPath {
315 if fake {
316 // All prebuilt binaries and headers are installed by copyFile function. This makes a fake
317 // snapshot just touch prebuilts and headers, rather than installing real files.
Inseob Kime9aec6a2021-01-05 20:03:22 +0900318 return writeStringToFileRule(ctx, "", out)
Jose Galmes0a942a02021-02-03 14:23:15 -0800319 } else {
320 return copyFileRule(ctx, path, out)
Inseob Kime9aec6a2021-01-05 20:03:22 +0900321 }
322 }
323
Inseob Kimde5744a2020-12-02 13:14:28 +0900324 // installSnapshot function copies prebuilt file (.so, .a, or executable) and json flag file.
325 // For executables, init_rc and vintf_fragments files are also copied.
Ivan Lozanod7586b62021-04-01 09:49:36 -0400326 installSnapshot := func(m LinkableInterface, fake bool) android.Paths {
Inseob Kim8471cda2019-11-15 09:59:12 +0900327 targetArch := "arch-" + m.Target().Arch.ArchType.String()
328 if m.Target().Arch.ArchVariant != "" {
329 targetArch += "-" + m.Target().Arch.ArchVariant
330 }
331
332 var ret android.Paths
333
Inseob Kimde5744a2020-12-02 13:14:28 +0900334 prop := snapshotJsonFlags{}
Inseob Kim8471cda2019-11-15 09:59:12 +0900335
336 // Common properties among snapshots.
337 prop.ModuleName = ctx.ModuleName(m)
Ivan Lozanof9e21722020-12-02 09:00:51 -0500338 if c.supportsVndkExt && m.IsVndkExt() {
Bill Peckham7d3f0962020-06-29 16:49:15 -0700339 // vndk exts are installed to /vendor/lib(64)?/vndk(-sp)?
Ivan Lozanod7586b62021-04-01 09:49:36 -0400340 if m.IsVndkSp() {
Bill Peckham7d3f0962020-06-29 16:49:15 -0700341 prop.RelativeInstallPath = "vndk-sp"
342 } else {
343 prop.RelativeInstallPath = "vndk"
344 }
345 } else {
346 prop.RelativeInstallPath = m.RelativeInstallPath()
347 }
Ivan Lozanod7586b62021-04-01 09:49:36 -0400348 prop.RuntimeLibs = m.SnapshotRuntimeLibs()
Inseob Kim8471cda2019-11-15 09:59:12 +0900349 prop.Required = m.RequiredModuleNames()
350 for _, path := range m.InitRc() {
351 prop.InitRc = append(prop.InitRc, filepath.Join("configs", path.Base()))
352 }
353 for _, path := range m.VintfFragments() {
354 prop.VintfFragments = append(prop.VintfFragments, filepath.Join("configs", path.Base()))
355 }
356
357 // install config files. ignores any duplicates.
358 for _, path := range append(m.InitRc(), m.VintfFragments()...) {
359 out := filepath.Join(configsDir, path.Base())
360 if !installedConfigs[out] {
361 installedConfigs[out] = true
Jose Galmes0a942a02021-02-03 14:23:15 -0800362 ret = append(ret, copyFile(ctx, path, out, fake))
Inseob Kim8471cda2019-11-15 09:59:12 +0900363 }
364 }
365
366 var propOut string
367
Ivan Lozanod7586b62021-04-01 09:49:36 -0400368 if m.IsSnapshotLibrary() {
369 exporterInfo := ctx.ModuleProvider(m.Module(), FlagExporterInfoProvider).(FlagExporterInfo)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900370
Inseob Kim8471cda2019-11-15 09:59:12 +0900371 // library flags
Colin Cross0de8a1e2020-09-18 14:15:30 -0700372 prop.ExportedFlags = exporterInfo.Flags
373 for _, dir := range exporterInfo.IncludeDirs {
Inseob Kim8471cda2019-11-15 09:59:12 +0900374 prop.ExportedDirs = append(prop.ExportedDirs, filepath.Join("include", dir.String()))
375 }
Colin Cross0de8a1e2020-09-18 14:15:30 -0700376 for _, dir := range exporterInfo.SystemIncludeDirs {
Inseob Kim8471cda2019-11-15 09:59:12 +0900377 prop.ExportedSystemDirs = append(prop.ExportedSystemDirs, filepath.Join("include", dir.String()))
378 }
Ivan Lozanod7586b62021-04-01 09:49:36 -0400379
Inseob Kim8471cda2019-11-15 09:59:12 +0900380 // shared libs dependencies aren't meaningful on static or header libs
Ivan Lozanod7586b62021-04-01 09:49:36 -0400381 if m.Shared() {
382 prop.SharedLibs = m.SnapshotSharedLibs()
Inseob Kim8471cda2019-11-15 09:59:12 +0900383 }
Ivan Lozanod7586b62021-04-01 09:49:36 -0400384 if sanitizable, ok := m.(PlatformSanitizeable); ok {
385 if sanitizable.Static() && sanitizable.SanitizePropDefined() {
386 prop.SanitizeMinimalDep = sanitizable.MinimalRuntimeDep() || sanitizable.MinimalRuntimeNeeded()
387 prop.SanitizeUbsanDep = sanitizable.UbsanRuntimeDep() || sanitizable.UbsanRuntimeNeeded()
388 }
Inseob Kim8471cda2019-11-15 09:59:12 +0900389 }
390
391 var libType string
Ivan Lozanod7586b62021-04-01 09:49:36 -0400392 if m.Static() {
Inseob Kim8471cda2019-11-15 09:59:12 +0900393 libType = "static"
Ivan Lozanod7586b62021-04-01 09:49:36 -0400394 } else if m.Shared() {
Inseob Kim8471cda2019-11-15 09:59:12 +0900395 libType = "shared"
396 } else {
397 libType = "header"
398 }
399
400 var stem string
401
402 // install .a or .so
403 if libType != "header" {
Ivan Lozanod7586b62021-04-01 09:49:36 -0400404 libPath := m.OutputFile().Path()
Inseob Kim8471cda2019-11-15 09:59:12 +0900405 stem = libPath.Base()
Ivan Lozanod7586b62021-04-01 09:49:36 -0400406 if sanitizable, ok := m.(PlatformSanitizeable); ok {
407 if sanitizable.Static() && sanitizable.SanitizePropDefined() && sanitizable.IsSanitizerEnabled(cfi) {
408 // both cfi and non-cfi variant for static libraries can exist.
409 // attach .cfi to distinguish between cfi and non-cfi.
410 // e.g. libbase.a -> libbase.cfi.a
411 ext := filepath.Ext(stem)
412 stem = strings.TrimSuffix(stem, ext) + ".cfi" + ext
413 prop.Sanitize = "cfi"
414 prop.ModuleName += ".cfi"
415 }
Inseob Kimc42f2f22020-07-29 20:32:10 +0900416 }
Inseob Kim8471cda2019-11-15 09:59:12 +0900417 snapshotLibOut := filepath.Join(snapshotArchDir, targetArch, libType, stem)
Jose Galmes0a942a02021-02-03 14:23:15 -0800418 ret = append(ret, copyFile(ctx, libPath, snapshotLibOut, fake))
Inseob Kim8471cda2019-11-15 09:59:12 +0900419 } else {
420 stem = ctx.ModuleName(m)
421 }
422
423 propOut = filepath.Join(snapshotArchDir, targetArch, libType, stem+".json")
Ivan Lozanod7586b62021-04-01 09:49:36 -0400424 } else if m.Binary() {
Inseob Kim8471cda2019-11-15 09:59:12 +0900425 // binary flags
426 prop.Symlinks = m.Symlinks()
Ivan Lozanod7586b62021-04-01 09:49:36 -0400427 prop.SharedLibs = m.SnapshotSharedLibs()
Inseob Kim8471cda2019-11-15 09:59:12 +0900428
429 // install bin
Ivan Lozanod7586b62021-04-01 09:49:36 -0400430 binPath := m.OutputFile().Path()
Inseob Kim8471cda2019-11-15 09:59:12 +0900431 snapshotBinOut := filepath.Join(snapshotArchDir, targetArch, "binary", binPath.Base())
Jose Galmes0a942a02021-02-03 14:23:15 -0800432 ret = append(ret, copyFile(ctx, binPath, snapshotBinOut, fake))
Inseob Kim8471cda2019-11-15 09:59:12 +0900433 propOut = snapshotBinOut + ".json"
Ivan Lozanod7586b62021-04-01 09:49:36 -0400434 } else if m.Object() {
Inseob Kim1042d292020-06-01 23:23:05 +0900435 // object files aren't installed to the device, so their names can conflict.
436 // Use module name as stem.
Ivan Lozanod7586b62021-04-01 09:49:36 -0400437 objPath := m.OutputFile().Path()
Inseob Kim1042d292020-06-01 23:23:05 +0900438 snapshotObjOut := filepath.Join(snapshotArchDir, targetArch, "object",
439 ctx.ModuleName(m)+filepath.Ext(objPath.Base()))
Jose Galmes0a942a02021-02-03 14:23:15 -0800440 ret = append(ret, copyFile(ctx, objPath, snapshotObjOut, fake))
Inseob Kim1042d292020-06-01 23:23:05 +0900441 propOut = snapshotObjOut + ".json"
Inseob Kim7f283f42020-06-01 21:53:49 +0900442 } else {
443 ctx.Errorf("unknown module %q in vendor snapshot", m.String())
444 return nil
Inseob Kim8471cda2019-11-15 09:59:12 +0900445 }
446
447 j, err := json.Marshal(prop)
448 if err != nil {
449 ctx.Errorf("json marshal to %q failed: %#v", propOut, err)
450 return nil
451 }
Inseob Kimde5744a2020-12-02 13:14:28 +0900452 ret = append(ret, writeStringToFileRule(ctx, string(j), propOut))
Inseob Kim8471cda2019-11-15 09:59:12 +0900453
454 return ret
455 }
456
457 ctx.VisitAllModules(func(module android.Module) {
Ivan Lozanod7586b62021-04-01 09:49:36 -0400458 m, ok := module.(LinkableInterface)
Inseob Kimeda2e9c2020-03-03 22:06:32 +0900459 if !ok {
460 return
461 }
462
463 moduleDir := ctx.ModuleDir(module)
Justin DeMartino383bfb32021-02-24 10:49:43 -0800464 inProprietaryPath := c.image.isProprietaryPath(moduleDir, ctx.DeviceConfig())
Colin Cross56a83212020-09-15 18:30:11 -0700465 apexInfo := ctx.ModuleProvider(module, android.ApexInfoProvider).(android.ApexInfo)
Bill Peckham945441c2020-08-31 16:07:58 -0700466
Jose Galmes6f843bc2020-12-11 13:36:29 -0800467 if c.image.excludeFromSnapshot(m) {
Jose Galmesf7294582020-11-13 12:07:36 -0800468 if inProprietaryPath {
Bill Peckham945441c2020-08-31 16:07:58 -0700469 // Error: exclude_from_vendor_snapshot applies
470 // to framework-path modules only.
471 ctx.Errorf("module %q in vendor proprietary path %q may not use \"exclude_from_vendor_snapshot: true\"", m.String(), moduleDir)
472 return
473 }
Bill Peckham945441c2020-08-31 16:07:58 -0700474 }
475
Inseob Kim7cf14652021-01-06 23:06:52 +0900476 if !isSnapshotAware(ctx.DeviceConfig(), m, inProprietaryPath, apexInfo, c.image) {
Inseob Kim8471cda2019-11-15 09:59:12 +0900477 return
478 }
479
Jose Galmes0a942a02021-02-03 14:23:15 -0800480 // If we are using directed snapshot and a module is not included in the
481 // list, we will still include the module as if it was a fake module.
482 // The reason is that soong needs all the dependencies to be present, even
483 // if they are not using during the build.
484 installAsFake := c.fake
485 if c.image.excludeFromDirectedSnapshot(ctx.DeviceConfig(), m.BaseModuleName()) {
486 installAsFake = true
487 }
Inseob Kimde5744a2020-12-02 13:14:28 +0900488
Jose Galmes0a942a02021-02-03 14:23:15 -0800489 // installSnapshot installs prebuilts and json flag files
490 snapshotOutputs = append(snapshotOutputs, installSnapshot(m, installAsFake)...)
Inseob Kimde5744a2020-12-02 13:14:28 +0900491 // just gather headers and notice files here, because they are to be deduplicated
Ivan Lozanod7586b62021-04-01 09:49:36 -0400492 if m.IsSnapshotLibrary() {
493 headers = append(headers, m.SnapshotHeaders()...)
Inseob Kim8471cda2019-11-15 09:59:12 +0900494 }
495
Bob Badoura75b0572020-02-18 20:21:55 -0800496 if len(m.NoticeFiles()) > 0 {
Inseob Kim8471cda2019-11-15 09:59:12 +0900497 noticeName := ctx.ModuleName(m) + ".txt"
498 noticeOut := filepath.Join(noticeDir, noticeName)
499 // skip already copied notice file
500 if !installedNotices[noticeOut] {
501 installedNotices[noticeOut] = true
Inseob Kime9aec6a2021-01-05 20:03:22 +0900502 snapshotOutputs = append(snapshotOutputs, combineNoticesRule(ctx, m.NoticeFiles(), noticeOut))
Inseob Kim8471cda2019-11-15 09:59:12 +0900503 }
504 }
505 })
506
507 // install all headers after removing duplicates
508 for _, header := range android.FirstUniquePaths(headers) {
Jose Galmes0a942a02021-02-03 14:23:15 -0800509 snapshotOutputs = append(snapshotOutputs, copyFile(ctx, header, filepath.Join(includeDir, header.String()), c.fake))
Inseob Kim8471cda2019-11-15 09:59:12 +0900510 }
511
512 // All artifacts are ready. Sort them to normalize ninja and then zip.
513 sort.Slice(snapshotOutputs, func(i, j int) bool {
514 return snapshotOutputs[i].String() < snapshotOutputs[j].String()
515 })
516
Jose Galmesf7294582020-11-13 12:07:36 -0800517 zipPath := android.PathForOutput(
518 ctx,
519 snapshotDir,
520 c.name+"-"+ctx.Config().DeviceName()+".zip")
Colin Crossf1a035e2020-11-16 17:32:30 -0800521 zipRule := android.NewRuleBuilder(pctx, ctx)
Inseob Kim8471cda2019-11-15 09:59:12 +0900522
523 // filenames in rspfile from FlagWithRspFileInputList might be single-quoted. Remove it with tr
Jose Galmesf7294582020-11-13 12:07:36 -0800524 snapshotOutputList := android.PathForOutput(
525 ctx,
526 snapshotDir,
527 c.name+"-"+ctx.Config().DeviceName()+"_list")
Colin Cross70c47412021-03-12 17:48:14 -0800528 rspFile := snapshotOutputList.ReplaceExtension(ctx, "rsp")
Inseob Kim8471cda2019-11-15 09:59:12 +0900529 zipRule.Command().
530 Text("tr").
531 FlagWithArg("-d ", "\\'").
Colin Cross70c47412021-03-12 17:48:14 -0800532 FlagWithRspFileInputList("< ", rspFile, snapshotOutputs).
Inseob Kim8471cda2019-11-15 09:59:12 +0900533 FlagWithOutput("> ", snapshotOutputList)
534
535 zipRule.Temporary(snapshotOutputList)
536
537 zipRule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800538 BuiltTool("soong_zip").
Inseob Kim8471cda2019-11-15 09:59:12 +0900539 FlagWithOutput("-o ", zipPath).
540 FlagWithArg("-C ", android.PathForOutput(ctx, snapshotDir).String()).
541 FlagWithInput("-l ", snapshotOutputList)
542
Colin Crossf1a035e2020-11-16 17:32:30 -0800543 zipRule.Build(zipPath.String(), c.name+" snapshot "+zipPath.String())
Inseob Kim8471cda2019-11-15 09:59:12 +0900544 zipRule.DeleteTemporaryFiles()
Jose Galmesf7294582020-11-13 12:07:36 -0800545 c.snapshotZipFile = android.OptionalPathForPath(zipPath)
Inseob Kim8471cda2019-11-15 09:59:12 +0900546}
547
Jose Galmesf7294582020-11-13 12:07:36 -0800548func (c *snapshotSingleton) MakeVars(ctx android.MakeVarsContext) {
549 ctx.Strict(
550 c.makeVar,
551 c.snapshotZipFile.String())
Inseob Kim8471cda2019-11-15 09:59:12 +0900552}