blob: 93aece4fca0f00e470f50b5c0ff40bfb6b59c758 [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
16import (
17 "encoding/json"
18 "path/filepath"
19 "sort"
20 "strings"
Inseob Kimeec88e12020-01-22 11:11:29 +090021 "sync"
Inseob Kim8471cda2019-11-15 09:59:12 +090022
23 "github.com/google/blueprint/proptools"
24
25 "android/soong/android"
26)
27
Inseob Kimeec88e12020-01-22 11:11:29 +090028const (
29 vendorSnapshotHeaderSuffix = ".vendor_header."
30 vendorSnapshotSharedSuffix = ".vendor_shared."
31 vendorSnapshotStaticSuffix = ".vendor_static."
32 vendorSnapshotBinarySuffix = ".vendor_binary."
Inseob Kim1042d292020-06-01 23:23:05 +090033 vendorSnapshotObjectSuffix = ".vendor_object."
Inseob Kimeec88e12020-01-22 11:11:29 +090034)
35
36var (
37 vendorSnapshotsLock sync.Mutex
38 vendorSuffixModulesKey = android.NewOnceKey("vendorSuffixModules")
39 vendorSnapshotHeaderLibsKey = android.NewOnceKey("vendorSnapshotHeaderLibs")
40 vendorSnapshotStaticLibsKey = android.NewOnceKey("vendorSnapshotStaticLibs")
41 vendorSnapshotSharedLibsKey = android.NewOnceKey("vendorSnapshotSharedLibs")
42 vendorSnapshotBinariesKey = android.NewOnceKey("vendorSnapshotBinaries")
Inseob Kim1042d292020-06-01 23:23:05 +090043 vendorSnapshotObjectsKey = android.NewOnceKey("vendorSnapshotObjects")
Inseob Kimeec88e12020-01-22 11:11:29 +090044)
45
Inseob Kim5f64aec2020-02-18 17:27:19 +090046// vendor snapshot maps hold names of vendor snapshot modules per arch
Inseob Kimeec88e12020-01-22 11:11:29 +090047func vendorSuffixModules(config android.Config) map[string]bool {
48 return config.Once(vendorSuffixModulesKey, func() interface{} {
49 return make(map[string]bool)
50 }).(map[string]bool)
51}
52
53func vendorSnapshotHeaderLibs(config android.Config) *snapshotMap {
54 return config.Once(vendorSnapshotHeaderLibsKey, func() interface{} {
55 return newSnapshotMap()
56 }).(*snapshotMap)
57}
58
59func vendorSnapshotSharedLibs(config android.Config) *snapshotMap {
60 return config.Once(vendorSnapshotSharedLibsKey, func() interface{} {
61 return newSnapshotMap()
62 }).(*snapshotMap)
63}
64
65func vendorSnapshotStaticLibs(config android.Config) *snapshotMap {
66 return config.Once(vendorSnapshotStaticLibsKey, func() interface{} {
67 return newSnapshotMap()
68 }).(*snapshotMap)
69}
70
71func vendorSnapshotBinaries(config android.Config) *snapshotMap {
72 return config.Once(vendorSnapshotBinariesKey, func() interface{} {
73 return newSnapshotMap()
74 }).(*snapshotMap)
75}
76
Inseob Kim1042d292020-06-01 23:23:05 +090077func vendorSnapshotObjects(config android.Config) *snapshotMap {
78 return config.Once(vendorSnapshotObjectsKey, func() interface{} {
79 return newSnapshotMap()
80 }).(*snapshotMap)
81}
82
Inseob Kim2d34ad92020-07-30 21:04:09 +090083type vendorSnapshotBaseProperties struct {
Inseob Kimeec88e12020-01-22 11:11:29 +090084 // snapshot version.
85 Version string
86
87 // Target arch name of the snapshot (e.g. 'arm64' for variant 'aosp_arm64')
88 Target_arch string
Inseob Kim2d34ad92020-07-30 21:04:09 +090089}
Inseob Kimeec88e12020-01-22 11:11:29 +090090
Inseob Kim2d34ad92020-07-30 21:04:09 +090091// vendorSnapshotModuleBase provides common basic functions for all snapshot modules.
92type vendorSnapshotModuleBase struct {
93 baseProperties vendorSnapshotBaseProperties
94 moduleSuffix string
95}
96
97func (p *vendorSnapshotModuleBase) Name(name string) string {
98 return name + p.NameSuffix()
99}
100
101func (p *vendorSnapshotModuleBase) NameSuffix() string {
102 versionSuffix := p.version()
103 if p.arch() != "" {
104 versionSuffix += "." + p.arch()
105 }
106
107 return p.moduleSuffix + versionSuffix
108}
109
110func (p *vendorSnapshotModuleBase) version() string {
111 return p.baseProperties.Version
112}
113
114func (p *vendorSnapshotModuleBase) arch() string {
115 return p.baseProperties.Target_arch
116}
117
118func (p *vendorSnapshotModuleBase) isSnapshotPrebuilt() bool {
119 return true
120}
121
122// Call this after creating a snapshot module with module suffix
123// such as vendorSnapshotSharedSuffix
124func (p *vendorSnapshotModuleBase) init(m *Module, suffix string) {
125 p.moduleSuffix = suffix
126 m.AddProperties(&p.baseProperties)
127 android.AddLoadHook(m, func(ctx android.LoadHookContext) {
128 vendorSnapshotLoadHook(ctx, p)
129 })
130}
131
132func vendorSnapshotLoadHook(ctx android.LoadHookContext, p *vendorSnapshotModuleBase) {
133 if p.version() != ctx.DeviceConfig().VndkVersion() {
134 ctx.Module().Disable()
135 return
136 }
137}
138
139type vendorSnapshotLibraryProperties struct {
Inseob Kimeec88e12020-01-22 11:11:29 +0900140 // Prebuilt file for each arch.
141 Src *string `android:"arch_variant"`
142
Inseob Kimc42f2f22020-07-29 20:32:10 +0900143 // list of directories that will be added to the include path (using -I).
144 Export_include_dirs []string `android:"arch_variant"`
145
146 // list of directories that will be added to the system path (using -isystem).
147 Export_system_include_dirs []string `android:"arch_variant"`
148
Inseob Kimeec88e12020-01-22 11:11:29 +0900149 // list of flags that will be used for any module that links against this module.
150 Export_flags []string `android:"arch_variant"`
151
Inseob Kimeec88e12020-01-22 11:11:29 +0900152 // Whether this prebuilt needs to depend on sanitize ubsan runtime or not.
153 Sanitize_ubsan_dep *bool `android:"arch_variant"`
154
155 // Whether this prebuilt needs to depend on sanitize minimal runtime or not.
156 Sanitize_minimal_dep *bool `android:"arch_variant"`
157}
158
Inseob Kimc42f2f22020-07-29 20:32:10 +0900159type snapshotSanitizer interface {
160 isSanitizerEnabled(t sanitizerType) bool
161 setSanitizerVariation(t sanitizerType, enabled bool)
162}
163
Inseob Kimeec88e12020-01-22 11:11:29 +0900164type vendorSnapshotLibraryDecorator struct {
Inseob Kim2d34ad92020-07-30 21:04:09 +0900165 vendorSnapshotModuleBase
Inseob Kimeec88e12020-01-22 11:11:29 +0900166 *libraryDecorator
Inseob Kimc42f2f22020-07-29 20:32:10 +0900167 properties vendorSnapshotLibraryProperties
168 sanitizerProperties struct {
169 CfiEnabled bool `blueprint:"mutated"`
170
171 // Library flags for cfi variant.
172 Cfi vendorSnapshotLibraryProperties `android:"arch_variant"`
173 }
Inseob Kimeec88e12020-01-22 11:11:29 +0900174 androidMkVendorSuffix bool
175}
176
Inseob Kimeec88e12020-01-22 11:11:29 +0900177func (p *vendorSnapshotLibraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
178 p.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(), p.NameSuffix())
179 return p.libraryDecorator.linkerFlags(ctx, flags)
180}
181
182func (p *vendorSnapshotLibraryDecorator) matchesWithDevice(config android.DeviceConfig) bool {
183 arches := config.Arches()
184 if len(arches) == 0 || arches[0].ArchType.String() != p.arch() {
185 return false
186 }
187 if !p.header() && p.properties.Src == nil {
188 return false
189 }
190 return true
191}
192
193func (p *vendorSnapshotLibraryDecorator) link(ctx ModuleContext,
194 flags Flags, deps PathDeps, objs Objects) android.Path {
195 m := ctx.Module().(*Module)
196 p.androidMkVendorSuffix = vendorSuffixModules(ctx.Config())[m.BaseModuleName()]
197
198 if p.header() {
199 return p.libraryDecorator.link(ctx, flags, deps, objs)
200 }
201
Inseob Kimc42f2f22020-07-29 20:32:10 +0900202 if p.sanitizerProperties.CfiEnabled {
203 p.properties = p.sanitizerProperties.Cfi
204 }
205
Inseob Kimeec88e12020-01-22 11:11:29 +0900206 if !p.matchesWithDevice(ctx.DeviceConfig()) {
207 return nil
208 }
209
Inseob Kimc42f2f22020-07-29 20:32:10 +0900210 p.libraryDecorator.reexportDirs(android.PathsForModuleSrc(ctx, p.properties.Export_include_dirs)...)
211 p.libraryDecorator.reexportSystemDirs(android.PathsForModuleSrc(ctx, p.properties.Export_system_include_dirs)...)
Inseob Kimeec88e12020-01-22 11:11:29 +0900212 p.libraryDecorator.reexportFlags(p.properties.Export_flags...)
213
214 in := android.PathForModuleSrc(ctx, *p.properties.Src)
215 p.unstrippedOutputFile = in
216
217 if p.shared() {
218 libName := in.Base()
219 builderFlags := flagsToBuilderFlags(flags)
220
221 // Optimize out relinking against shared libraries whose interface hasn't changed by
222 // depending on a table of contents file instead of the library itself.
223 tocFile := android.PathForModuleOut(ctx, libName+".toc")
224 p.tocFile = android.OptionalPathForPath(tocFile)
225 TransformSharedObjectToToc(ctx, in, tocFile, builderFlags)
226 }
227
228 return in
229}
230
Inseob Kimeec88e12020-01-22 11:11:29 +0900231func (p *vendorSnapshotLibraryDecorator) install(ctx ModuleContext, file android.Path) {
232 if p.matchesWithDevice(ctx.DeviceConfig()) && (p.shared() || p.static()) {
233 p.baseInstaller.install(ctx, file)
234 }
235}
236
Inseob Kim2d34ad92020-07-30 21:04:09 +0900237func (p *vendorSnapshotLibraryDecorator) nativeCoverage() bool {
238 return false
Inseob Kimeec88e12020-01-22 11:11:29 +0900239}
240
Inseob Kimc42f2f22020-07-29 20:32:10 +0900241func (p *vendorSnapshotLibraryDecorator) isSanitizerEnabled(t sanitizerType) bool {
242 switch t {
243 case cfi:
244 return p.sanitizerProperties.Cfi.Src != nil
245 default:
246 return false
247 }
248}
249
250func (p *vendorSnapshotLibraryDecorator) setSanitizerVariation(t sanitizerType, enabled bool) {
251 if !enabled {
252 return
253 }
254 switch t {
255 case cfi:
256 p.sanitizerProperties.CfiEnabled = true
257 default:
258 return
259 }
260}
261
Inseob Kim2d34ad92020-07-30 21:04:09 +0900262func vendorSnapshotLibrary(suffix string) (*Module, *vendorSnapshotLibraryDecorator) {
Inseob Kimeec88e12020-01-22 11:11:29 +0900263 module, library := NewLibrary(android.DeviceSupported)
264
265 module.stl = nil
266 module.sanitize = nil
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200267 library.disableStripping()
Inseob Kimeec88e12020-01-22 11:11:29 +0900268
269 prebuilt := &vendorSnapshotLibraryDecorator{
270 libraryDecorator: library,
271 }
272
273 prebuilt.baseLinker.Properties.No_libcrt = BoolPtr(true)
274 prebuilt.baseLinker.Properties.Nocrt = BoolPtr(true)
275
276 // Prevent default system libs (libc, libm, and libdl) from being linked
277 if prebuilt.baseLinker.Properties.System_shared_libs == nil {
278 prebuilt.baseLinker.Properties.System_shared_libs = []string{}
279 }
280
281 module.compiler = nil
282 module.linker = prebuilt
283 module.installer = prebuilt
284
Inseob Kim2d34ad92020-07-30 21:04:09 +0900285 prebuilt.init(module, suffix)
Inseob Kimc42f2f22020-07-29 20:32:10 +0900286 module.AddProperties(
287 &prebuilt.properties,
288 &prebuilt.sanitizerProperties,
289 )
Inseob Kimeec88e12020-01-22 11:11:29 +0900290
291 return module, prebuilt
292}
293
294func VendorSnapshotSharedFactory() android.Module {
Inseob Kim2d34ad92020-07-30 21:04:09 +0900295 module, prebuilt := vendorSnapshotLibrary(vendorSnapshotSharedSuffix)
Inseob Kimeec88e12020-01-22 11:11:29 +0900296 prebuilt.libraryDecorator.BuildOnlyShared()
Inseob Kimeec88e12020-01-22 11:11:29 +0900297 return module.Init()
298}
299
300func VendorSnapshotStaticFactory() android.Module {
Inseob Kim2d34ad92020-07-30 21:04:09 +0900301 module, prebuilt := vendorSnapshotLibrary(vendorSnapshotStaticSuffix)
Inseob Kimeec88e12020-01-22 11:11:29 +0900302 prebuilt.libraryDecorator.BuildOnlyStatic()
Inseob Kimeec88e12020-01-22 11:11:29 +0900303 return module.Init()
304}
305
306func VendorSnapshotHeaderFactory() android.Module {
Inseob Kim2d34ad92020-07-30 21:04:09 +0900307 module, prebuilt := vendorSnapshotLibrary(vendorSnapshotHeaderSuffix)
Inseob Kimeec88e12020-01-22 11:11:29 +0900308 prebuilt.libraryDecorator.HeaderOnly()
Inseob Kimeec88e12020-01-22 11:11:29 +0900309 return module.Init()
310}
311
Inseob Kimc42f2f22020-07-29 20:32:10 +0900312var _ snapshotSanitizer = (*vendorSnapshotLibraryDecorator)(nil)
313
Inseob Kimeec88e12020-01-22 11:11:29 +0900314type vendorSnapshotBinaryProperties struct {
Inseob Kimeec88e12020-01-22 11:11:29 +0900315 // Prebuilt file for each arch.
316 Src *string `android:"arch_variant"`
317}
318
319type vendorSnapshotBinaryDecorator struct {
Inseob Kim2d34ad92020-07-30 21:04:09 +0900320 vendorSnapshotModuleBase
Inseob Kimeec88e12020-01-22 11:11:29 +0900321 *binaryDecorator
322 properties vendorSnapshotBinaryProperties
323 androidMkVendorSuffix bool
324}
325
Inseob Kimeec88e12020-01-22 11:11:29 +0900326func (p *vendorSnapshotBinaryDecorator) matchesWithDevice(config android.DeviceConfig) bool {
327 if config.DeviceArch() != p.arch() {
328 return false
329 }
330 if p.properties.Src == nil {
331 return false
332 }
333 return true
334}
335
336func (p *vendorSnapshotBinaryDecorator) link(ctx ModuleContext,
337 flags Flags, deps PathDeps, objs Objects) android.Path {
338 if !p.matchesWithDevice(ctx.DeviceConfig()) {
339 return nil
340 }
341
342 in := android.PathForModuleSrc(ctx, *p.properties.Src)
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200343 stripFlags := flagsToStripFlags(flags)
Inseob Kimeec88e12020-01-22 11:11:29 +0900344 p.unstrippedOutputFile = in
345 binName := in.Base()
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200346 if p.stripper.NeedsStrip(ctx) {
Inseob Kimeec88e12020-01-22 11:11:29 +0900347 stripped := android.PathForModuleOut(ctx, "stripped", binName)
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200348 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, stripFlags)
Inseob Kimeec88e12020-01-22 11:11:29 +0900349 in = stripped
350 }
351
352 m := ctx.Module().(*Module)
353 p.androidMkVendorSuffix = vendorSuffixModules(ctx.Config())[m.BaseModuleName()]
354
355 // use cpExecutable to make it executable
356 outputFile := android.PathForModuleOut(ctx, binName)
357 ctx.Build(pctx, android.BuildParams{
358 Rule: android.CpExecutable,
359 Description: "prebuilt",
360 Output: outputFile,
361 Input: in,
362 })
363
364 return outputFile
365}
366
Inseob Kim2d34ad92020-07-30 21:04:09 +0900367func (p *vendorSnapshotBinaryDecorator) nativeCoverage() bool {
368 return false
Inseob Kim1042d292020-06-01 23:23:05 +0900369}
370
Inseob Kimeec88e12020-01-22 11:11:29 +0900371func VendorSnapshotBinaryFactory() android.Module {
372 module, binary := NewBinary(android.DeviceSupported)
373 binary.baseLinker.Properties.No_libcrt = BoolPtr(true)
374 binary.baseLinker.Properties.Nocrt = BoolPtr(true)
375
376 // Prevent default system libs (libc, libm, and libdl) from being linked
377 if binary.baseLinker.Properties.System_shared_libs == nil {
378 binary.baseLinker.Properties.System_shared_libs = []string{}
379 }
380
381 prebuilt := &vendorSnapshotBinaryDecorator{
382 binaryDecorator: binary,
383 }
384
385 module.compiler = nil
386 module.sanitize = nil
387 module.stl = nil
388 module.linker = prebuilt
389
Inseob Kim2d34ad92020-07-30 21:04:09 +0900390 prebuilt.init(module, vendorSnapshotBinarySuffix)
Inseob Kimeec88e12020-01-22 11:11:29 +0900391 module.AddProperties(&prebuilt.properties)
392 return module.Init()
393}
394
Inseob Kim1042d292020-06-01 23:23:05 +0900395type vendorSnapshotObjectProperties struct {
Inseob Kim1042d292020-06-01 23:23:05 +0900396 // Prebuilt file for each arch.
397 Src *string `android:"arch_variant"`
398}
399
400type vendorSnapshotObjectLinker struct {
Inseob Kim2d34ad92020-07-30 21:04:09 +0900401 vendorSnapshotModuleBase
Inseob Kim1042d292020-06-01 23:23:05 +0900402 objectLinker
403 properties vendorSnapshotObjectProperties
404 androidMkVendorSuffix bool
405}
406
Inseob Kim1042d292020-06-01 23:23:05 +0900407func (p *vendorSnapshotObjectLinker) matchesWithDevice(config android.DeviceConfig) bool {
408 if config.DeviceArch() != p.arch() {
409 return false
410 }
411 if p.properties.Src == nil {
412 return false
413 }
414 return true
415}
416
417func (p *vendorSnapshotObjectLinker) link(ctx ModuleContext,
418 flags Flags, deps PathDeps, objs Objects) android.Path {
419 if !p.matchesWithDevice(ctx.DeviceConfig()) {
420 return nil
421 }
422
423 m := ctx.Module().(*Module)
424 p.androidMkVendorSuffix = vendorSuffixModules(ctx.Config())[m.BaseModuleName()]
425
426 return android.PathForModuleSrc(ctx, *p.properties.Src)
427}
428
429func (p *vendorSnapshotObjectLinker) nativeCoverage() bool {
430 return false
431}
432
Inseob Kim1042d292020-06-01 23:23:05 +0900433func VendorSnapshotObjectFactory() android.Module {
434 module := newObject()
435
436 prebuilt := &vendorSnapshotObjectLinker{
437 objectLinker: objectLinker{
438 baseLinker: NewBaseLinker(nil),
439 },
440 }
441 module.linker = prebuilt
442
Inseob Kim2d34ad92020-07-30 21:04:09 +0900443 prebuilt.init(module, vendorSnapshotObjectSuffix)
Inseob Kim1042d292020-06-01 23:23:05 +0900444 module.AddProperties(&prebuilt.properties)
445 return module.Init()
446}
447
Inseob Kim8471cda2019-11-15 09:59:12 +0900448func init() {
449 android.RegisterSingletonType("vendor-snapshot", VendorSnapshotSingleton)
Inseob Kimeec88e12020-01-22 11:11:29 +0900450 android.RegisterModuleType("vendor_snapshot_shared", VendorSnapshotSharedFactory)
451 android.RegisterModuleType("vendor_snapshot_static", VendorSnapshotStaticFactory)
452 android.RegisterModuleType("vendor_snapshot_header", VendorSnapshotHeaderFactory)
453 android.RegisterModuleType("vendor_snapshot_binary", VendorSnapshotBinaryFactory)
Inseob Kim1042d292020-06-01 23:23:05 +0900454 android.RegisterModuleType("vendor_snapshot_object", VendorSnapshotObjectFactory)
Inseob Kim8471cda2019-11-15 09:59:12 +0900455}
456
457func VendorSnapshotSingleton() android.Singleton {
458 return &vendorSnapshotSingleton{}
459}
460
461type vendorSnapshotSingleton struct {
462 vendorSnapshotZipFile android.OptionalPath
463}
464
465var (
466 // Modules under following directories are ignored. They are OEM's and vendor's
Daniel Norman713387d2020-07-28 16:04:38 -0700467 // proprietary modules(device/, kernel/, vendor/, and hardware/).
Inseob Kim8471cda2019-11-15 09:59:12 +0900468 // TODO(b/65377115): Clean up these with more maintainable way
469 vendorProprietaryDirs = []string{
470 "device",
Daniel Norman713387d2020-07-28 16:04:38 -0700471 "kernel",
Inseob Kim8471cda2019-11-15 09:59:12 +0900472 "vendor",
473 "hardware",
474 }
475
476 // Modules under following directories are included as they are in AOSP,
Daniel Norman713387d2020-07-28 16:04:38 -0700477 // although hardware/ and kernel/ are normally for vendor's own.
Inseob Kim8471cda2019-11-15 09:59:12 +0900478 // TODO(b/65377115): Clean up these with more maintainable way
479 aospDirsUnderProprietary = []string{
Daniel Norman713387d2020-07-28 16:04:38 -0700480 "kernel/configs",
481 "kernel/prebuilts",
482 "kernel/tests",
Inseob Kim8471cda2019-11-15 09:59:12 +0900483 "hardware/interfaces",
484 "hardware/libhardware",
485 "hardware/libhardware_legacy",
486 "hardware/ril",
487 }
488)
489
490// Determine if a dir under source tree is an SoC-owned proprietary directory, such as
491// device/, vendor/, etc.
492func isVendorProprietaryPath(dir string) bool {
493 for _, p := range vendorProprietaryDirs {
494 if strings.HasPrefix(dir, p) {
495 // filter out AOSP defined directories, e.g. hardware/interfaces/
496 aosp := false
497 for _, p := range aospDirsUnderProprietary {
498 if strings.HasPrefix(dir, p) {
499 aosp = true
500 break
501 }
502 }
503 if !aosp {
504 return true
505 }
506 }
507 }
508 return false
509}
510
511// Determine if a module is going to be included in vendor snapshot or not.
512//
513// Targets of vendor snapshot are "vendor: true" or "vendor_available: true" modules in
514// AOSP. They are not guaranteed to be compatible with older vendor images. (e.g. might
515// depend on newer VNDK) So they are captured as vendor snapshot To build older vendor
516// image and newer system image altogether.
Inseob Kimeda2e9c2020-03-03 22:06:32 +0900517func isVendorSnapshotModule(m *Module, moduleDir string) bool {
Inseob Kim7f283f42020-06-01 21:53:49 +0900518 if !m.Enabled() || m.Properties.HideFromMake {
Inseob Kim8471cda2019-11-15 09:59:12 +0900519 return false
520 }
521 // skip proprietary modules, but include all VNDK (static)
Inseob Kimeda2e9c2020-03-03 22:06:32 +0900522 if isVendorProprietaryPath(moduleDir) && !m.IsVndk() {
Inseob Kim8471cda2019-11-15 09:59:12 +0900523 return false
524 }
525 if m.Target().Os.Class != android.Device {
526 return false
527 }
528 if m.Target().NativeBridge == android.NativeBridgeEnabled {
529 return false
530 }
531 // the module must be installed in /vendor
Inseob Kim7f283f42020-06-01 21:53:49 +0900532 if !m.IsForPlatform() || m.isSnapshotPrebuilt() || !m.inVendor() {
Inseob Kim8471cda2019-11-15 09:59:12 +0900533 return false
534 }
Inseob Kim65ca36a2020-06-11 13:55:45 +0900535 // skip kernel_headers which always depend on vendor
536 if _, ok := m.linker.(*kernelHeadersDecorator); ok {
537 return false
538 }
Justin Yunf2664c62020-07-30 18:57:54 +0900539 // skip llndk_library and llndk_headers which are backward compatible
540 if _, ok := m.linker.(*llndkStubDecorator); ok {
541 return false
542 }
543 if _, ok := m.linker.(*llndkHeadersDecorator); ok {
544 return false
545 }
Inseob Kim8471cda2019-11-15 09:59:12 +0900546
547 // Libraries
548 if l, ok := m.linker.(snapshotLibraryInterface); ok {
Inseob Kim7f283f42020-06-01 21:53:49 +0900549 // TODO(b/65377115): add full support for sanitizer
550 if m.sanitize != nil {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900551 // scs and hwasan export both sanitized and unsanitized variants for static and header
Inseob Kim7f283f42020-06-01 21:53:49 +0900552 // Always use unsanitized variants of them.
Inseob Kimc42f2f22020-07-29 20:32:10 +0900553 for _, t := range []sanitizerType{scs, hwasan} {
Inseob Kim7f283f42020-06-01 21:53:49 +0900554 if !l.shared() && m.sanitize.isSanitizerEnabled(t) {
555 return false
556 }
557 }
Inseob Kimc42f2f22020-07-29 20:32:10 +0900558 // cfi also exports both variants. But for static, we capture both.
559 if !l.static() && !l.shared() && m.sanitize.isSanitizerEnabled(cfi) {
560 return false
561 }
Inseob Kim7f283f42020-06-01 21:53:49 +0900562 }
Inseob Kim8471cda2019-11-15 09:59:12 +0900563 if l.static() {
Inseob Kim7f283f42020-06-01 21:53:49 +0900564 return m.outputFile.Valid() && proptools.BoolDefault(m.VendorProperties.Vendor_available, true)
Inseob Kim8471cda2019-11-15 09:59:12 +0900565 }
566 if l.shared() {
Bill Peckham7d3f0962020-06-29 16:49:15 -0700567 if !m.outputFile.Valid() {
568 return false
569 }
570 if !m.IsVndk() {
571 return true
572 }
573 return m.isVndkExt()
Inseob Kim8471cda2019-11-15 09:59:12 +0900574 }
575 return true
576 }
577
Inseob Kim1042d292020-06-01 23:23:05 +0900578 // Binaries and Objects
579 if m.binary() || m.object() {
Inseob Kim7f283f42020-06-01 21:53:49 +0900580 return m.outputFile.Valid() && proptools.BoolDefault(m.VendorProperties.Vendor_available, true)
Inseob Kim8471cda2019-11-15 09:59:12 +0900581 }
Inseob Kim7f283f42020-06-01 21:53:49 +0900582
583 return false
Inseob Kim8471cda2019-11-15 09:59:12 +0900584}
585
586func (c *vendorSnapshotSingleton) GenerateBuildActions(ctx android.SingletonContext) {
587 // BOARD_VNDK_VERSION must be set to 'current' in order to generate a vendor snapshot.
588 if ctx.DeviceConfig().VndkVersion() != "current" {
589 return
590 }
591
592 var snapshotOutputs android.Paths
593
594 /*
595 Vendor snapshot zipped artifacts directory structure:
596 {SNAPSHOT_ARCH}/
597 arch-{TARGET_ARCH}-{TARGET_ARCH_VARIANT}/
598 shared/
599 (.so shared libraries)
600 static/
601 (.a static libraries)
602 header/
603 (header only libraries)
604 binary/
605 (executable binaries)
Inseob Kim1042d292020-06-01 23:23:05 +0900606 object/
607 (.o object files)
Inseob Kim8471cda2019-11-15 09:59:12 +0900608 arch-{TARGET_2ND_ARCH}-{TARGET_2ND_ARCH_VARIANT}/
609 shared/
610 (.so shared libraries)
611 static/
612 (.a static libraries)
613 header/
614 (header only libraries)
615 binary/
616 (executable binaries)
Inseob Kim1042d292020-06-01 23:23:05 +0900617 object/
618 (.o object files)
Inseob Kim8471cda2019-11-15 09:59:12 +0900619 NOTICE_FILES/
620 (notice files, e.g. libbase.txt)
621 configs/
622 (config files, e.g. init.rc files, vintf_fragments.xml files, etc.)
623 include/
624 (header files of same directory structure with source tree)
625 */
626
627 snapshotDir := "vendor-snapshot"
628 snapshotArchDir := filepath.Join(snapshotDir, ctx.DeviceConfig().DeviceArch())
629
630 includeDir := filepath.Join(snapshotArchDir, "include")
631 configsDir := filepath.Join(snapshotArchDir, "configs")
632 noticeDir := filepath.Join(snapshotArchDir, "NOTICE_FILES")
633
634 installedNotices := make(map[string]bool)
635 installedConfigs := make(map[string]bool)
636
637 var headers android.Paths
638
Inseob Kim8471cda2019-11-15 09:59:12 +0900639 installSnapshot := func(m *Module) android.Paths {
640 targetArch := "arch-" + m.Target().Arch.ArchType.String()
641 if m.Target().Arch.ArchVariant != "" {
642 targetArch += "-" + m.Target().Arch.ArchVariant
643 }
644
645 var ret android.Paths
646
647 prop := struct {
648 ModuleName string `json:",omitempty"`
649 RelativeInstallPath string `json:",omitempty"`
650
651 // library flags
652 ExportedDirs []string `json:",omitempty"`
653 ExportedSystemDirs []string `json:",omitempty"`
654 ExportedFlags []string `json:",omitempty"`
Inseob Kimc42f2f22020-07-29 20:32:10 +0900655 Sanitize string `json:",omitempty"`
Inseob Kim8471cda2019-11-15 09:59:12 +0900656 SanitizeMinimalDep bool `json:",omitempty"`
657 SanitizeUbsanDep bool `json:",omitempty"`
658
659 // binary flags
660 Symlinks []string `json:",omitempty"`
661
662 // dependencies
663 SharedLibs []string `json:",omitempty"`
664 RuntimeLibs []string `json:",omitempty"`
665 Required []string `json:",omitempty"`
666
667 // extra config files
668 InitRc []string `json:",omitempty"`
669 VintfFragments []string `json:",omitempty"`
670 }{}
671
672 // Common properties among snapshots.
673 prop.ModuleName = ctx.ModuleName(m)
Bill Peckham7d3f0962020-06-29 16:49:15 -0700674 if m.isVndkExt() {
675 // vndk exts are installed to /vendor/lib(64)?/vndk(-sp)?
676 if m.isVndkSp() {
677 prop.RelativeInstallPath = "vndk-sp"
678 } else {
679 prop.RelativeInstallPath = "vndk"
680 }
681 } else {
682 prop.RelativeInstallPath = m.RelativeInstallPath()
683 }
Inseob Kim8471cda2019-11-15 09:59:12 +0900684 prop.RuntimeLibs = m.Properties.SnapshotRuntimeLibs
685 prop.Required = m.RequiredModuleNames()
686 for _, path := range m.InitRc() {
687 prop.InitRc = append(prop.InitRc, filepath.Join("configs", path.Base()))
688 }
689 for _, path := range m.VintfFragments() {
690 prop.VintfFragments = append(prop.VintfFragments, filepath.Join("configs", path.Base()))
691 }
692
693 // install config files. ignores any duplicates.
694 for _, path := range append(m.InitRc(), m.VintfFragments()...) {
695 out := filepath.Join(configsDir, path.Base())
696 if !installedConfigs[out] {
697 installedConfigs[out] = true
698 ret = append(ret, copyFile(ctx, path, out))
699 }
700 }
701
702 var propOut string
703
Inseob Kimeda2e9c2020-03-03 22:06:32 +0900704 if l, ok := m.linker.(snapshotLibraryInterface); ok {
Inseob Kimc42f2f22020-07-29 20:32:10 +0900705
Inseob Kim8471cda2019-11-15 09:59:12 +0900706 // library flags
707 prop.ExportedFlags = l.exportedFlags()
708 for _, dir := range l.exportedDirs() {
709 prop.ExportedDirs = append(prop.ExportedDirs, filepath.Join("include", dir.String()))
710 }
711 for _, dir := range l.exportedSystemDirs() {
712 prop.ExportedSystemDirs = append(prop.ExportedSystemDirs, filepath.Join("include", dir.String()))
713 }
714 // shared libs dependencies aren't meaningful on static or header libs
715 if l.shared() {
716 prop.SharedLibs = m.Properties.SnapshotSharedLibs
717 }
718 if l.static() && m.sanitize != nil {
719 prop.SanitizeMinimalDep = m.sanitize.Properties.MinimalRuntimeDep || enableMinimalRuntime(m.sanitize)
720 prop.SanitizeUbsanDep = m.sanitize.Properties.UbsanRuntimeDep || enableUbsanRuntime(m.sanitize)
721 }
722
723 var libType string
724 if l.static() {
725 libType = "static"
726 } else if l.shared() {
727 libType = "shared"
728 } else {
729 libType = "header"
730 }
731
732 var stem string
733
734 // install .a or .so
735 if libType != "header" {
736 libPath := m.outputFile.Path()
737 stem = libPath.Base()
Inseob Kimc42f2f22020-07-29 20:32:10 +0900738 if l.static() && m.sanitize != nil && m.sanitize.isSanitizerEnabled(cfi) {
739 // both cfi and non-cfi variant for static libraries can exist.
740 // attach .cfi to distinguish between cfi and non-cfi.
741 // e.g. libbase.a -> libbase.cfi.a
742 ext := filepath.Ext(stem)
743 stem = strings.TrimSuffix(stem, ext) + ".cfi" + ext
744 prop.Sanitize = "cfi"
745 prop.ModuleName += ".cfi"
746 }
Inseob Kim8471cda2019-11-15 09:59:12 +0900747 snapshotLibOut := filepath.Join(snapshotArchDir, targetArch, libType, stem)
748 ret = append(ret, copyFile(ctx, libPath, snapshotLibOut))
749 } else {
750 stem = ctx.ModuleName(m)
751 }
752
753 propOut = filepath.Join(snapshotArchDir, targetArch, libType, stem+".json")
Inseob Kim7f283f42020-06-01 21:53:49 +0900754 } else if m.binary() {
Inseob Kim8471cda2019-11-15 09:59:12 +0900755 // binary flags
756 prop.Symlinks = m.Symlinks()
757 prop.SharedLibs = m.Properties.SnapshotSharedLibs
758
759 // install bin
760 binPath := m.outputFile.Path()
761 snapshotBinOut := filepath.Join(snapshotArchDir, targetArch, "binary", binPath.Base())
762 ret = append(ret, copyFile(ctx, binPath, snapshotBinOut))
763 propOut = snapshotBinOut + ".json"
Inseob Kim1042d292020-06-01 23:23:05 +0900764 } else if m.object() {
765 // object files aren't installed to the device, so their names can conflict.
766 // Use module name as stem.
767 objPath := m.outputFile.Path()
768 snapshotObjOut := filepath.Join(snapshotArchDir, targetArch, "object",
769 ctx.ModuleName(m)+filepath.Ext(objPath.Base()))
770 ret = append(ret, copyFile(ctx, objPath, snapshotObjOut))
771 propOut = snapshotObjOut + ".json"
Inseob Kim7f283f42020-06-01 21:53:49 +0900772 } else {
773 ctx.Errorf("unknown module %q in vendor snapshot", m.String())
774 return nil
Inseob Kim8471cda2019-11-15 09:59:12 +0900775 }
776
777 j, err := json.Marshal(prop)
778 if err != nil {
779 ctx.Errorf("json marshal to %q failed: %#v", propOut, err)
780 return nil
781 }
782 ret = append(ret, writeStringToFile(ctx, string(j), propOut))
783
784 return ret
785 }
786
787 ctx.VisitAllModules(func(module android.Module) {
788 m, ok := module.(*Module)
Inseob Kimeda2e9c2020-03-03 22:06:32 +0900789 if !ok {
790 return
791 }
792
793 moduleDir := ctx.ModuleDir(module)
794 if !isVendorSnapshotModule(m, moduleDir) {
Inseob Kim8471cda2019-11-15 09:59:12 +0900795 return
796 }
797
798 snapshotOutputs = append(snapshotOutputs, installSnapshot(m)...)
Inseob Kimeda2e9c2020-03-03 22:06:32 +0900799 if l, ok := m.linker.(snapshotLibraryInterface); ok {
800 headers = append(headers, l.snapshotHeaders()...)
Inseob Kim8471cda2019-11-15 09:59:12 +0900801 }
802
Bob Badoura75b0572020-02-18 20:21:55 -0800803 if len(m.NoticeFiles()) > 0 {
Inseob Kim8471cda2019-11-15 09:59:12 +0900804 noticeName := ctx.ModuleName(m) + ".txt"
805 noticeOut := filepath.Join(noticeDir, noticeName)
806 // skip already copied notice file
807 if !installedNotices[noticeOut] {
808 installedNotices[noticeOut] = true
Bob Badoura75b0572020-02-18 20:21:55 -0800809 snapshotOutputs = append(snapshotOutputs, combineNotices(
810 ctx, m.NoticeFiles(), noticeOut))
Inseob Kim8471cda2019-11-15 09:59:12 +0900811 }
812 }
813 })
814
815 // install all headers after removing duplicates
816 for _, header := range android.FirstUniquePaths(headers) {
817 snapshotOutputs = append(snapshotOutputs, copyFile(
818 ctx, header, filepath.Join(includeDir, header.String())))
819 }
820
821 // All artifacts are ready. Sort them to normalize ninja and then zip.
822 sort.Slice(snapshotOutputs, func(i, j int) bool {
823 return snapshotOutputs[i].String() < snapshotOutputs[j].String()
824 })
825
826 zipPath := android.PathForOutput(ctx, snapshotDir, "vendor-"+ctx.Config().DeviceName()+".zip")
827 zipRule := android.NewRuleBuilder()
828
829 // filenames in rspfile from FlagWithRspFileInputList might be single-quoted. Remove it with tr
830 snapshotOutputList := android.PathForOutput(ctx, snapshotDir, "vendor-"+ctx.Config().DeviceName()+"_list")
831 zipRule.Command().
832 Text("tr").
833 FlagWithArg("-d ", "\\'").
834 FlagWithRspFileInputList("< ", snapshotOutputs).
835 FlagWithOutput("> ", snapshotOutputList)
836
837 zipRule.Temporary(snapshotOutputList)
838
839 zipRule.Command().
840 BuiltTool(ctx, "soong_zip").
841 FlagWithOutput("-o ", zipPath).
842 FlagWithArg("-C ", android.PathForOutput(ctx, snapshotDir).String()).
843 FlagWithInput("-l ", snapshotOutputList)
844
845 zipRule.Build(pctx, ctx, zipPath.String(), "vendor snapshot "+zipPath.String())
846 zipRule.DeleteTemporaryFiles()
847 c.vendorSnapshotZipFile = android.OptionalPathForPath(zipPath)
848}
849
850func (c *vendorSnapshotSingleton) MakeVars(ctx android.MakeVarsContext) {
851 ctx.Strict("SOONG_VENDOR_SNAPSHOT_ZIP", c.vendorSnapshotZipFile.String())
852}
Inseob Kimeec88e12020-01-22 11:11:29 +0900853
854type snapshotInterface interface {
855 matchesWithDevice(config android.DeviceConfig) bool
856}
857
858var _ snapshotInterface = (*vndkPrebuiltLibraryDecorator)(nil)
859var _ snapshotInterface = (*vendorSnapshotLibraryDecorator)(nil)
860var _ snapshotInterface = (*vendorSnapshotBinaryDecorator)(nil)
Inseob Kim1042d292020-06-01 23:23:05 +0900861var _ snapshotInterface = (*vendorSnapshotObjectLinker)(nil)
Inseob Kimeec88e12020-01-22 11:11:29 +0900862
863// gathers all snapshot modules for vendor, and disable unnecessary snapshots
864// TODO(b/145966707): remove mutator and utilize android.Prebuilt to override source modules
865func VendorSnapshotMutator(ctx android.BottomUpMutatorContext) {
866 vndkVersion := ctx.DeviceConfig().VndkVersion()
867 // don't need snapshot if current
868 if vndkVersion == "current" || vndkVersion == "" {
869 return
870 }
871
872 module, ok := ctx.Module().(*Module)
873 if !ok || !module.Enabled() || module.VndkVersion() != vndkVersion {
874 return
875 }
876
Inseob Kim1042d292020-06-01 23:23:05 +0900877 if !module.isSnapshotPrebuilt() {
Inseob Kimeec88e12020-01-22 11:11:29 +0900878 return
879 }
880
Inseob Kim1042d292020-06-01 23:23:05 +0900881 // isSnapshotPrebuilt ensures snapshotInterface
882 if !module.linker.(snapshotInterface).matchesWithDevice(ctx.DeviceConfig()) {
Inseob Kimeec88e12020-01-22 11:11:29 +0900883 // Disable unnecessary snapshot module, but do not disable
884 // vndk_prebuilt_shared because they might be packed into vndk APEX
885 if !module.IsVndk() {
886 module.Disable()
887 }
888 return
889 }
890
891 var snapshotMap *snapshotMap
892
893 if lib, ok := module.linker.(libraryInterface); ok {
894 if lib.static() {
895 snapshotMap = vendorSnapshotStaticLibs(ctx.Config())
896 } else if lib.shared() {
897 snapshotMap = vendorSnapshotSharedLibs(ctx.Config())
898 } else {
899 // header
900 snapshotMap = vendorSnapshotHeaderLibs(ctx.Config())
901 }
902 } else if _, ok := module.linker.(*vendorSnapshotBinaryDecorator); ok {
903 snapshotMap = vendorSnapshotBinaries(ctx.Config())
Inseob Kim1042d292020-06-01 23:23:05 +0900904 } else if _, ok := module.linker.(*vendorSnapshotObjectLinker); ok {
905 snapshotMap = vendorSnapshotObjects(ctx.Config())
Inseob Kimeec88e12020-01-22 11:11:29 +0900906 } else {
907 return
908 }
909
910 vendorSnapshotsLock.Lock()
911 defer vendorSnapshotsLock.Unlock()
912 snapshotMap.add(module.BaseModuleName(), ctx.Arch().ArchType, ctx.ModuleName())
913}
914
915// Disables source modules which have snapshots
916func VendorSnapshotSourceMutator(ctx android.BottomUpMutatorContext) {
Inseob Kim5f64aec2020-02-18 17:27:19 +0900917 if !ctx.Device() {
918 return
919 }
920
Inseob Kimeec88e12020-01-22 11:11:29 +0900921 vndkVersion := ctx.DeviceConfig().VndkVersion()
922 // don't need snapshot if current
923 if vndkVersion == "current" || vndkVersion == "" {
924 return
925 }
926
927 module, ok := ctx.Module().(*Module)
928 if !ok {
929 return
930 }
931
Inseob Kim5f64aec2020-02-18 17:27:19 +0900932 // vendor suffix should be added to snapshots if the source module isn't vendor: true.
933 if !module.SocSpecific() {
934 // But we can't just check SocSpecific() since we already passed the image mutator.
935 // Check ramdisk and recovery to see if we are real "vendor: true" module.
936 ramdisk_available := module.InRamdisk() && !module.OnlyInRamdisk()
937 recovery_available := module.InRecovery() && !module.OnlyInRecovery()
Inseob Kimeec88e12020-01-22 11:11:29 +0900938
Inseob Kim5f64aec2020-02-18 17:27:19 +0900939 if !ramdisk_available && !recovery_available {
940 vendorSnapshotsLock.Lock()
941 defer vendorSnapshotsLock.Unlock()
942
943 vendorSuffixModules(ctx.Config())[ctx.ModuleName()] = true
944 }
Inseob Kimeec88e12020-01-22 11:11:29 +0900945 }
946
947 if module.isSnapshotPrebuilt() || module.VndkVersion() != ctx.DeviceConfig().VndkVersion() {
948 // only non-snapshot modules with BOARD_VNDK_VERSION
949 return
950 }
951
Inseob Kim206665c2020-06-02 23:48:32 +0900952 // .. and also filter out llndk library
953 if module.isLlndk(ctx.Config()) {
954 return
955 }
956
Inseob Kimeec88e12020-01-22 11:11:29 +0900957 var snapshotMap *snapshotMap
958
959 if lib, ok := module.linker.(libraryInterface); ok {
960 if lib.static() {
961 snapshotMap = vendorSnapshotStaticLibs(ctx.Config())
962 } else if lib.shared() {
963 snapshotMap = vendorSnapshotSharedLibs(ctx.Config())
964 } else {
965 // header
966 snapshotMap = vendorSnapshotHeaderLibs(ctx.Config())
967 }
Inseob Kim7f283f42020-06-01 21:53:49 +0900968 } else if module.binary() {
Inseob Kimeec88e12020-01-22 11:11:29 +0900969 snapshotMap = vendorSnapshotBinaries(ctx.Config())
Inseob Kim1042d292020-06-01 23:23:05 +0900970 } else if module.object() {
971 snapshotMap = vendorSnapshotObjects(ctx.Config())
Inseob Kimeec88e12020-01-22 11:11:29 +0900972 } else {
973 return
974 }
975
976 if _, ok := snapshotMap.get(ctx.ModuleName(), ctx.Arch().ArchType); !ok {
977 // Corresponding snapshot doesn't exist
978 return
979 }
980
981 // Disables source modules if corresponding snapshot exists.
982 if lib, ok := module.linker.(libraryInterface); ok && lib.buildStatic() && lib.buildShared() {
983 // But do not disable because the shared variant depends on the static variant.
984 module.SkipInstall()
985 module.Properties.HideFromMake = true
986 } else {
987 module.Disable()
988 }
989}