blob: 7077b71798b0331c5406ce4a617f5f6ef2cc6e24 [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,
Inseob Kimde5744a2020-12-02 13:14:28 +090034 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,
43 vendorSnapshotImageSingleton,
44 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
93var (
94 // Modules under following directories are ignored. They are OEM's and vendor's
Daniel Norman713387d2020-07-28 16:04:38 -070095 // proprietary modules(device/, kernel/, vendor/, and hardware/).
Inseob Kim8471cda2019-11-15 09:59:12 +090096 vendorProprietaryDirs = []string{
97 "device",
Daniel Norman713387d2020-07-28 16:04:38 -070098 "kernel",
Inseob Kim8471cda2019-11-15 09:59:12 +090099 "vendor",
100 "hardware",
101 }
102
Jose Galmesf7294582020-11-13 12:07:36 -0800103 // Modules under following directories are ignored. They are OEM's and vendor's
104 // proprietary modules(device/, kernel/, vendor/, and hardware/).
Jose Galmesf7294582020-11-13 12:07:36 -0800105 recoveryProprietaryDirs = []string{
Jose Galmesf7294582020-11-13 12:07:36 -0800106 "device",
107 "hardware",
108 "kernel",
109 "vendor",
110 }
111
Inseob Kim8471cda2019-11-15 09:59:12 +0900112 // Modules under following directories are included as they are in AOSP,
Daniel Norman713387d2020-07-28 16:04:38 -0700113 // although hardware/ and kernel/ are normally for vendor's own.
Inseob Kim8471cda2019-11-15 09:59:12 +0900114 aospDirsUnderProprietary = []string{
Daniel Norman713387d2020-07-28 16:04:38 -0700115 "kernel/configs",
116 "kernel/prebuilts",
117 "kernel/tests",
Inseob Kim8471cda2019-11-15 09:59:12 +0900118 "hardware/interfaces",
119 "hardware/libhardware",
120 "hardware/libhardware_legacy",
121 "hardware/ril",
122 }
123)
124
125// Determine if a dir under source tree is an SoC-owned proprietary directory, such as
126// device/, vendor/, etc.
127func isVendorProprietaryPath(dir string) bool {
Jose Galmesf7294582020-11-13 12:07:36 -0800128 return isProprietaryPath(dir, vendorProprietaryDirs)
129}
130
131func isRecoveryProprietaryPath(dir string) bool {
132 return isProprietaryPath(dir, recoveryProprietaryDirs)
133}
134
135// Determine if a dir under source tree is an SoC-owned proprietary directory, such as
136// device/, vendor/, etc.
137func isProprietaryPath(dir string, proprietaryDirs []string) bool {
138 for _, p := range proprietaryDirs {
Inseob Kim8471cda2019-11-15 09:59:12 +0900139 if strings.HasPrefix(dir, p) {
140 // filter out AOSP defined directories, e.g. hardware/interfaces/
141 aosp := false
142 for _, p := range aospDirsUnderProprietary {
143 if strings.HasPrefix(dir, p) {
144 aosp = true
145 break
146 }
147 }
148 if !aosp {
149 return true
150 }
151 }
152 }
153 return false
154}
155
Bill Peckham945441c2020-08-31 16:07:58 -0700156func isVendorProprietaryModule(ctx android.BaseModuleContext) bool {
Bill Peckham945441c2020-08-31 16:07:58 -0700157 // Any module in a vendor proprietary path is a vendor proprietary
158 // module.
Bill Peckham945441c2020-08-31 16:07:58 -0700159 if isVendorProprietaryPath(ctx.ModuleDir()) {
160 return true
161 }
162
163 // However if the module is not in a vendor proprietary path, it may
164 // still be a vendor proprietary module. This happens for cc modules
165 // that are excluded from the vendor snapshot, and it means that the
166 // vendor has assumed control of the framework-provided module.
Bill Peckham945441c2020-08-31 16:07:58 -0700167 if c, ok := ctx.Module().(*Module); ok {
168 if c.ExcludeFromVendorSnapshot() {
169 return true
170 }
171 }
172
173 return false
174}
175
Jose Galmes6f843bc2020-12-11 13:36:29 -0800176func isRecoveryProprietaryModule(ctx android.BaseModuleContext) bool {
177
Justin Yune09ac172021-01-20 19:49:01 +0900178 // Any module in a recovery proprietary path is a recovery proprietary
Jose Galmes6f843bc2020-12-11 13:36:29 -0800179 // module.
180 if isRecoveryProprietaryPath(ctx.ModuleDir()) {
181 return true
182 }
183
Justin Yune09ac172021-01-20 19:49:01 +0900184 // However if the module is not in a recovery proprietary path, it may
185 // still be a recovery proprietary module. This happens for cc modules
186 // that are excluded from the recovery snapshot, and it means that the
Jose Galmes6f843bc2020-12-11 13:36:29 -0800187 // vendor has assumed control of the framework-provided module.
188
189 if c, ok := ctx.Module().(*Module); ok {
190 if c.ExcludeFromRecoverySnapshot() {
191 return true
192 }
193 }
194
195 return false
196}
197
Inseob Kimde5744a2020-12-02 13:14:28 +0900198// Determines if the module is a candidate for snapshot.
Inseob Kim7cf14652021-01-06 23:06:52 +0900199func isSnapshotAware(cfg android.DeviceConfig, m *Module, inProprietaryPath bool, apexInfo android.ApexInfo, image snapshotImage) bool {
Inseob Kim7f283f42020-06-01 21:53:49 +0900200 if !m.Enabled() || m.Properties.HideFromMake {
Inseob Kim8471cda2019-11-15 09:59:12 +0900201 return false
202 }
Martin Stjernholm809d5182020-09-10 01:46:05 +0100203 // When android/prebuilt.go selects between source and prebuilt, it sets
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800204 // HideFromMake on the other one to avoid duplicate install rules in make.
205 if m.IsHideFromMake() {
Martin Stjernholm809d5182020-09-10 01:46:05 +0100206 return false
207 }
Jose Galmesf7294582020-11-13 12:07:36 -0800208 // skip proprietary modules, but (for the vendor snapshot only)
209 // include all VNDK (static)
210 if inProprietaryPath && (!image.includeVndk() || !m.IsVndk()) {
Bill Peckham945441c2020-08-31 16:07:58 -0700211 return false
212 }
213 // If the module would be included based on its path, check to see if
214 // the module is marked to be excluded. If so, skip it.
Jose Galmes6f843bc2020-12-11 13:36:29 -0800215 if image.excludeFromSnapshot(m) {
Inseob Kim8471cda2019-11-15 09:59:12 +0900216 return false
217 }
218 if m.Target().Os.Class != android.Device {
219 return false
220 }
221 if m.Target().NativeBridge == android.NativeBridgeEnabled {
222 return false
223 }
Inseob Kimde5744a2020-12-02 13:14:28 +0900224 // the module must be installed in target image
Jose Galmesf7294582020-11-13 12:07:36 -0800225 if !apexInfo.IsForPlatform() || m.isSnapshotPrebuilt() || !image.inImage(m)() {
Inseob Kim8471cda2019-11-15 09:59:12 +0900226 return false
227 }
Inseob Kim65ca36a2020-06-11 13:55:45 +0900228 // skip kernel_headers which always depend on vendor
229 if _, ok := m.linker.(*kernelHeadersDecorator); ok {
230 return false
231 }
Justin Yunf2664c62020-07-30 18:57:54 +0900232 // skip llndk_library and llndk_headers which are backward compatible
Colin Cross127bb8b2020-12-16 16:46:01 -0800233 if m.IsLlndk() {
234 return false
235 }
Justin Yunf2664c62020-07-30 18:57:54 +0900236 if _, ok := m.linker.(*llndkStubDecorator); ok {
237 return false
238 }
239 if _, ok := m.linker.(*llndkHeadersDecorator); ok {
240 return false
241 }
Inseob Kim8471cda2019-11-15 09:59:12 +0900242
243 // Libraries
244 if l, ok := m.linker.(snapshotLibraryInterface); ok {
Inseob Kim7f283f42020-06-01 21:53:49 +0900245 if m.sanitize != nil {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900246 // scs and hwasan export both sanitized and unsanitized variants for static and header
Inseob Kim7f283f42020-06-01 21:53:49 +0900247 // Always use unsanitized variants of them.
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500248 for _, t := range []SanitizerType{scs, hwasan} {
Inseob Kim7f283f42020-06-01 21:53:49 +0900249 if !l.shared() && m.sanitize.isSanitizerEnabled(t) {
250 return false
251 }
252 }
Inseob Kimc42f2f22020-07-29 20:32:10 +0900253 // cfi also exports both variants. But for static, we capture both.
Inseob Kimde5744a2020-12-02 13:14:28 +0900254 // This is because cfi static libraries can't be linked from non-cfi modules,
255 // and vice versa. This isn't the case for scs and hwasan sanitizers.
Inseob Kimc42f2f22020-07-29 20:32:10 +0900256 if !l.static() && !l.shared() && m.sanitize.isSanitizerEnabled(cfi) {
257 return false
258 }
Inseob Kim7f283f42020-06-01 21:53:49 +0900259 }
Inseob Kim8471cda2019-11-15 09:59:12 +0900260 if l.static() {
Justin Yune09ac172021-01-20 19:49:01 +0900261 return m.outputFile.Valid() && !image.private(m)
Inseob Kim8471cda2019-11-15 09:59:12 +0900262 }
263 if l.shared() {
Bill Peckham7d3f0962020-06-29 16:49:15 -0700264 if !m.outputFile.Valid() {
265 return false
266 }
Jose Galmesf7294582020-11-13 12:07:36 -0800267 if image.includeVndk() {
268 if !m.IsVndk() {
269 return true
270 }
Ivan Lozanof9e21722020-12-02 09:00:51 -0500271 return m.IsVndkExt()
Bill Peckham7d3f0962020-06-29 16:49:15 -0700272 }
Inseob Kim8471cda2019-11-15 09:59:12 +0900273 }
274 return true
275 }
276
Inseob Kim1042d292020-06-01 23:23:05 +0900277 // Binaries and Objects
278 if m.binary() || m.object() {
Justin Yune09ac172021-01-20 19:49:01 +0900279 return m.outputFile.Valid()
Inseob Kim8471cda2019-11-15 09:59:12 +0900280 }
Inseob Kim7f283f42020-06-01 21:53:49 +0900281
282 return false
Inseob Kim8471cda2019-11-15 09:59:12 +0900283}
284
Inseob Kimde5744a2020-12-02 13:14:28 +0900285// This is to be saved as .json files, which is for development/vendor_snapshot/update.py.
286// These flags become Android.bp snapshot module properties.
287type snapshotJsonFlags struct {
288 ModuleName string `json:",omitempty"`
289 RelativeInstallPath string `json:",omitempty"`
Colin Crossa8890802021-01-22 14:06:33 -0800290 AndroidMkSuffix string `json:",omitempty"`
Inseob Kimde5744a2020-12-02 13:14:28 +0900291
292 // library flags
293 ExportedDirs []string `json:",omitempty"`
294 ExportedSystemDirs []string `json:",omitempty"`
295 ExportedFlags []string `json:",omitempty"`
296 Sanitize string `json:",omitempty"`
297 SanitizeMinimalDep bool `json:",omitempty"`
298 SanitizeUbsanDep bool `json:",omitempty"`
299
300 // binary flags
301 Symlinks []string `json:",omitempty"`
302
303 // dependencies
304 SharedLibs []string `json:",omitempty"`
305 RuntimeLibs []string `json:",omitempty"`
306 Required []string `json:",omitempty"`
307
308 // extra config files
309 InitRc []string `json:",omitempty"`
310 VintfFragments []string `json:",omitempty"`
311}
312
Jose Galmesf7294582020-11-13 12:07:36 -0800313func (c *snapshotSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Jose Galmes6f843bc2020-12-11 13:36:29 -0800314 if !c.image.shouldGenerateSnapshot(ctx) {
Inseob Kim8471cda2019-11-15 09:59:12 +0900315 return
316 }
317
318 var snapshotOutputs android.Paths
319
320 /*
321 Vendor snapshot zipped artifacts directory structure:
322 {SNAPSHOT_ARCH}/
323 arch-{TARGET_ARCH}-{TARGET_ARCH_VARIANT}/
324 shared/
325 (.so shared libraries)
326 static/
327 (.a static libraries)
328 header/
329 (header only libraries)
330 binary/
331 (executable binaries)
Inseob Kim1042d292020-06-01 23:23:05 +0900332 object/
333 (.o object files)
Inseob Kim8471cda2019-11-15 09:59:12 +0900334 arch-{TARGET_2ND_ARCH}-{TARGET_2ND_ARCH_VARIANT}/
335 shared/
336 (.so shared libraries)
337 static/
338 (.a static libraries)
339 header/
340 (header only libraries)
341 binary/
342 (executable binaries)
Inseob Kim1042d292020-06-01 23:23:05 +0900343 object/
344 (.o object files)
Inseob Kim8471cda2019-11-15 09:59:12 +0900345 NOTICE_FILES/
346 (notice files, e.g. libbase.txt)
347 configs/
348 (config files, e.g. init.rc files, vintf_fragments.xml files, etc.)
349 include/
350 (header files of same directory structure with source tree)
351 */
352
Jose Galmesf7294582020-11-13 12:07:36 -0800353 snapshotDir := c.name + "-snapshot"
Inseob Kime9aec6a2021-01-05 20:03:22 +0900354 if c.fake {
355 // If this is a fake snapshot singleton, place all files under fake/ subdirectory to avoid
356 // collision with real snapshot files
357 snapshotDir = filepath.Join("fake", snapshotDir)
358 }
Inseob Kim8471cda2019-11-15 09:59:12 +0900359 snapshotArchDir := filepath.Join(snapshotDir, ctx.DeviceConfig().DeviceArch())
360
361 includeDir := filepath.Join(snapshotArchDir, "include")
362 configsDir := filepath.Join(snapshotArchDir, "configs")
363 noticeDir := filepath.Join(snapshotArchDir, "NOTICE_FILES")
364
365 installedNotices := make(map[string]bool)
366 installedConfigs := make(map[string]bool)
367
368 var headers android.Paths
369
Jose Galmes0a942a02021-02-03 14:23:15 -0800370 copyFile := func(ctx android.SingletonContext, path android.Path, out string, fake bool) android.OutputPath {
371 if fake {
372 // All prebuilt binaries and headers are installed by copyFile function. This makes a fake
373 // snapshot just touch prebuilts and headers, rather than installing real files.
Inseob Kime9aec6a2021-01-05 20:03:22 +0900374 return writeStringToFileRule(ctx, "", out)
Jose Galmes0a942a02021-02-03 14:23:15 -0800375 } else {
376 return copyFileRule(ctx, path, out)
Inseob Kime9aec6a2021-01-05 20:03:22 +0900377 }
378 }
379
Inseob Kimde5744a2020-12-02 13:14:28 +0900380 // installSnapshot function copies prebuilt file (.so, .a, or executable) and json flag file.
381 // For executables, init_rc and vintf_fragments files are also copied.
Jose Galmes0a942a02021-02-03 14:23:15 -0800382 installSnapshot := func(m *Module, fake bool) android.Paths {
Inseob Kim8471cda2019-11-15 09:59:12 +0900383 targetArch := "arch-" + m.Target().Arch.ArchType.String()
384 if m.Target().Arch.ArchVariant != "" {
385 targetArch += "-" + m.Target().Arch.ArchVariant
386 }
387
388 var ret android.Paths
389
Inseob Kimde5744a2020-12-02 13:14:28 +0900390 prop := snapshotJsonFlags{}
Inseob Kim8471cda2019-11-15 09:59:12 +0900391
392 // Common properties among snapshots.
393 prop.ModuleName = ctx.ModuleName(m)
Ivan Lozanof9e21722020-12-02 09:00:51 -0500394 if c.supportsVndkExt && m.IsVndkExt() {
Bill Peckham7d3f0962020-06-29 16:49:15 -0700395 // vndk exts are installed to /vendor/lib(64)?/vndk(-sp)?
396 if m.isVndkSp() {
397 prop.RelativeInstallPath = "vndk-sp"
398 } else {
399 prop.RelativeInstallPath = "vndk"
400 }
401 } else {
402 prop.RelativeInstallPath = m.RelativeInstallPath()
403 }
Colin Crossa8890802021-01-22 14:06:33 -0800404 prop.AndroidMkSuffix = m.Properties.SubName
Inseob Kim8471cda2019-11-15 09:59:12 +0900405 prop.RuntimeLibs = m.Properties.SnapshotRuntimeLibs
406 prop.Required = m.RequiredModuleNames()
407 for _, path := range m.InitRc() {
408 prop.InitRc = append(prop.InitRc, filepath.Join("configs", path.Base()))
409 }
410 for _, path := range m.VintfFragments() {
411 prop.VintfFragments = append(prop.VintfFragments, filepath.Join("configs", path.Base()))
412 }
413
414 // install config files. ignores any duplicates.
415 for _, path := range append(m.InitRc(), m.VintfFragments()...) {
416 out := filepath.Join(configsDir, path.Base())
417 if !installedConfigs[out] {
418 installedConfigs[out] = true
Jose Galmes0a942a02021-02-03 14:23:15 -0800419 ret = append(ret, copyFile(ctx, path, out, fake))
Inseob Kim8471cda2019-11-15 09:59:12 +0900420 }
421 }
422
423 var propOut string
424
Inseob Kimeda2e9c2020-03-03 22:06:32 +0900425 if l, ok := m.linker.(snapshotLibraryInterface); ok {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700426 exporterInfo := ctx.ModuleProvider(m, FlagExporterInfoProvider).(FlagExporterInfo)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900427
Inseob Kim8471cda2019-11-15 09:59:12 +0900428 // library flags
Colin Cross0de8a1e2020-09-18 14:15:30 -0700429 prop.ExportedFlags = exporterInfo.Flags
430 for _, dir := range exporterInfo.IncludeDirs {
Inseob Kim8471cda2019-11-15 09:59:12 +0900431 prop.ExportedDirs = append(prop.ExportedDirs, filepath.Join("include", dir.String()))
432 }
Colin Cross0de8a1e2020-09-18 14:15:30 -0700433 for _, dir := range exporterInfo.SystemIncludeDirs {
Inseob Kim8471cda2019-11-15 09:59:12 +0900434 prop.ExportedSystemDirs = append(prop.ExportedSystemDirs, filepath.Join("include", dir.String()))
435 }
436 // shared libs dependencies aren't meaningful on static or header libs
437 if l.shared() {
438 prop.SharedLibs = m.Properties.SnapshotSharedLibs
439 }
440 if l.static() && m.sanitize != nil {
441 prop.SanitizeMinimalDep = m.sanitize.Properties.MinimalRuntimeDep || enableMinimalRuntime(m.sanitize)
442 prop.SanitizeUbsanDep = m.sanitize.Properties.UbsanRuntimeDep || enableUbsanRuntime(m.sanitize)
443 }
444
445 var libType string
446 if l.static() {
447 libType = "static"
448 } else if l.shared() {
449 libType = "shared"
450 } else {
451 libType = "header"
452 }
453
454 var stem string
455
456 // install .a or .so
457 if libType != "header" {
458 libPath := m.outputFile.Path()
459 stem = libPath.Base()
Inseob Kimc42f2f22020-07-29 20:32:10 +0900460 if l.static() && m.sanitize != nil && m.sanitize.isSanitizerEnabled(cfi) {
461 // both cfi and non-cfi variant for static libraries can exist.
462 // attach .cfi to distinguish between cfi and non-cfi.
463 // e.g. libbase.a -> libbase.cfi.a
464 ext := filepath.Ext(stem)
465 stem = strings.TrimSuffix(stem, ext) + ".cfi" + ext
466 prop.Sanitize = "cfi"
467 prop.ModuleName += ".cfi"
468 }
Inseob Kim8471cda2019-11-15 09:59:12 +0900469 snapshotLibOut := filepath.Join(snapshotArchDir, targetArch, libType, stem)
Jose Galmes0a942a02021-02-03 14:23:15 -0800470 ret = append(ret, copyFile(ctx, libPath, snapshotLibOut, fake))
Inseob Kim8471cda2019-11-15 09:59:12 +0900471 } else {
472 stem = ctx.ModuleName(m)
473 }
474
475 propOut = filepath.Join(snapshotArchDir, targetArch, libType, stem+".json")
Inseob Kim7f283f42020-06-01 21:53:49 +0900476 } else if m.binary() {
Inseob Kim8471cda2019-11-15 09:59:12 +0900477 // binary flags
478 prop.Symlinks = m.Symlinks()
479 prop.SharedLibs = m.Properties.SnapshotSharedLibs
480
481 // install bin
482 binPath := m.outputFile.Path()
483 snapshotBinOut := filepath.Join(snapshotArchDir, targetArch, "binary", binPath.Base())
Jose Galmes0a942a02021-02-03 14:23:15 -0800484 ret = append(ret, copyFile(ctx, binPath, snapshotBinOut, fake))
Inseob Kim8471cda2019-11-15 09:59:12 +0900485 propOut = snapshotBinOut + ".json"
Inseob Kim1042d292020-06-01 23:23:05 +0900486 } else if m.object() {
487 // object files aren't installed to the device, so their names can conflict.
488 // Use module name as stem.
489 objPath := m.outputFile.Path()
490 snapshotObjOut := filepath.Join(snapshotArchDir, targetArch, "object",
491 ctx.ModuleName(m)+filepath.Ext(objPath.Base()))
Jose Galmes0a942a02021-02-03 14:23:15 -0800492 ret = append(ret, copyFile(ctx, objPath, snapshotObjOut, fake))
Inseob Kim1042d292020-06-01 23:23:05 +0900493 propOut = snapshotObjOut + ".json"
Inseob Kim7f283f42020-06-01 21:53:49 +0900494 } else {
495 ctx.Errorf("unknown module %q in vendor snapshot", m.String())
496 return nil
Inseob Kim8471cda2019-11-15 09:59:12 +0900497 }
498
499 j, err := json.Marshal(prop)
500 if err != nil {
501 ctx.Errorf("json marshal to %q failed: %#v", propOut, err)
502 return nil
503 }
Inseob Kimde5744a2020-12-02 13:14:28 +0900504 ret = append(ret, writeStringToFileRule(ctx, string(j), propOut))
Inseob Kim8471cda2019-11-15 09:59:12 +0900505
506 return ret
507 }
508
509 ctx.VisitAllModules(func(module android.Module) {
510 m, ok := module.(*Module)
Inseob Kimeda2e9c2020-03-03 22:06:32 +0900511 if !ok {
512 return
513 }
514
515 moduleDir := ctx.ModuleDir(module)
Jose Galmesf7294582020-11-13 12:07:36 -0800516 inProprietaryPath := c.image.isProprietaryPath(moduleDir)
Colin Cross56a83212020-09-15 18:30:11 -0700517 apexInfo := ctx.ModuleProvider(module, android.ApexInfoProvider).(android.ApexInfo)
Bill Peckham945441c2020-08-31 16:07:58 -0700518
Jose Galmes6f843bc2020-12-11 13:36:29 -0800519 if c.image.excludeFromSnapshot(m) {
Jose Galmesf7294582020-11-13 12:07:36 -0800520 if inProprietaryPath {
Bill Peckham945441c2020-08-31 16:07:58 -0700521 // Error: exclude_from_vendor_snapshot applies
522 // to framework-path modules only.
523 ctx.Errorf("module %q in vendor proprietary path %q may not use \"exclude_from_vendor_snapshot: true\"", m.String(), moduleDir)
524 return
525 }
Bill Peckham945441c2020-08-31 16:07:58 -0700526 }
527
Inseob Kim7cf14652021-01-06 23:06:52 +0900528 if !isSnapshotAware(ctx.DeviceConfig(), m, inProprietaryPath, apexInfo, c.image) {
Inseob Kim8471cda2019-11-15 09:59:12 +0900529 return
530 }
531
Jose Galmes0a942a02021-02-03 14:23:15 -0800532 // If we are using directed snapshot and a module is not included in the
533 // list, we will still include the module as if it was a fake module.
534 // The reason is that soong needs all the dependencies to be present, even
535 // if they are not using during the build.
536 installAsFake := c.fake
537 if c.image.excludeFromDirectedSnapshot(ctx.DeviceConfig(), m.BaseModuleName()) {
538 installAsFake = true
539 }
Inseob Kimde5744a2020-12-02 13:14:28 +0900540
Jose Galmes0a942a02021-02-03 14:23:15 -0800541 // installSnapshot installs prebuilts and json flag files
542 snapshotOutputs = append(snapshotOutputs, installSnapshot(m, installAsFake)...)
Inseob Kimde5744a2020-12-02 13:14:28 +0900543 // just gather headers and notice files here, because they are to be deduplicated
Inseob Kimeda2e9c2020-03-03 22:06:32 +0900544 if l, ok := m.linker.(snapshotLibraryInterface); ok {
545 headers = append(headers, l.snapshotHeaders()...)
Inseob Kim8471cda2019-11-15 09:59:12 +0900546 }
547
Bob Badoura75b0572020-02-18 20:21:55 -0800548 if len(m.NoticeFiles()) > 0 {
Inseob Kim8471cda2019-11-15 09:59:12 +0900549 noticeName := ctx.ModuleName(m) + ".txt"
550 noticeOut := filepath.Join(noticeDir, noticeName)
551 // skip already copied notice file
552 if !installedNotices[noticeOut] {
553 installedNotices[noticeOut] = true
Inseob Kime9aec6a2021-01-05 20:03:22 +0900554 snapshotOutputs = append(snapshotOutputs, combineNoticesRule(ctx, m.NoticeFiles(), noticeOut))
Inseob Kim8471cda2019-11-15 09:59:12 +0900555 }
556 }
557 })
558
559 // install all headers after removing duplicates
560 for _, header := range android.FirstUniquePaths(headers) {
Jose Galmes0a942a02021-02-03 14:23:15 -0800561 snapshotOutputs = append(snapshotOutputs, copyFile(ctx, header, filepath.Join(includeDir, header.String()), c.fake))
Inseob Kim8471cda2019-11-15 09:59:12 +0900562 }
563
564 // All artifacts are ready. Sort them to normalize ninja and then zip.
565 sort.Slice(snapshotOutputs, func(i, j int) bool {
566 return snapshotOutputs[i].String() < snapshotOutputs[j].String()
567 })
568
Jose Galmesf7294582020-11-13 12:07:36 -0800569 zipPath := android.PathForOutput(
570 ctx,
571 snapshotDir,
572 c.name+"-"+ctx.Config().DeviceName()+".zip")
Colin Crossf1a035e2020-11-16 17:32:30 -0800573 zipRule := android.NewRuleBuilder(pctx, ctx)
Inseob Kim8471cda2019-11-15 09:59:12 +0900574
575 // filenames in rspfile from FlagWithRspFileInputList might be single-quoted. Remove it with tr
Jose Galmesf7294582020-11-13 12:07:36 -0800576 snapshotOutputList := android.PathForOutput(
577 ctx,
578 snapshotDir,
579 c.name+"-"+ctx.Config().DeviceName()+"_list")
Inseob Kim8471cda2019-11-15 09:59:12 +0900580 zipRule.Command().
581 Text("tr").
582 FlagWithArg("-d ", "\\'").
583 FlagWithRspFileInputList("< ", snapshotOutputs).
584 FlagWithOutput("> ", snapshotOutputList)
585
586 zipRule.Temporary(snapshotOutputList)
587
588 zipRule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800589 BuiltTool("soong_zip").
Inseob Kim8471cda2019-11-15 09:59:12 +0900590 FlagWithOutput("-o ", zipPath).
591 FlagWithArg("-C ", android.PathForOutput(ctx, snapshotDir).String()).
592 FlagWithInput("-l ", snapshotOutputList)
593
Colin Crossf1a035e2020-11-16 17:32:30 -0800594 zipRule.Build(zipPath.String(), c.name+" snapshot "+zipPath.String())
Inseob Kim8471cda2019-11-15 09:59:12 +0900595 zipRule.DeleteTemporaryFiles()
Jose Galmesf7294582020-11-13 12:07:36 -0800596 c.snapshotZipFile = android.OptionalPathForPath(zipPath)
Inseob Kim8471cda2019-11-15 09:59:12 +0900597}
598
Jose Galmesf7294582020-11-13 12:07:36 -0800599func (c *snapshotSingleton) MakeVars(ctx android.MakeVarsContext) {
600 ctx.Strict(
601 c.makeVar,
602 c.snapshotZipFile.String())
Inseob Kim8471cda2019-11-15 09:59:12 +0900603}