blob: ec8f023c23ae1e8deb3886ebffa0419d9bb4fdef [file] [log] [blame]
Justin Yun8effde42017-06-23 19:24:43 +09001// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17import (
Inseob Kimae553032019-05-14 18:52:49 +090018 "encoding/json"
Martin Stjernholm257eb0c2018-10-15 13:05:27 +010019 "errors"
Inseob Kim1f086e22019-05-09 13:29:15 +090020 "path/filepath"
Colin Cross766efbc2017-08-17 14:55:15 -070021 "sort"
Jiyong Parkd5b18a52017-08-03 21:22:50 +090022 "strings"
23 "sync"
24
Justin Yun8effde42017-06-23 19:24:43 +090025 "android/soong/android"
Vic Yangefd249e2018-11-12 20:19:56 -080026 "android/soong/cc/config"
Justin Yun8effde42017-06-23 19:24:43 +090027)
28
29type VndkProperties struct {
30 Vndk struct {
31 // declared as a VNDK or VNDK-SP module. The vendor variant
32 // will be installed in /system instead of /vendor partition.
33 //
Roland Levillaindfe75b32019-07-23 16:53:32 +010034 // `vendor_available` must be explicitly set to either true or
Jiyong Park82e2bf32017-08-16 14:05:54 +090035 // false together with `vndk: {enabled: true}`.
Justin Yun8effde42017-06-23 19:24:43 +090036 Enabled *bool
37
38 // declared as a VNDK-SP module, which is a subset of VNDK.
39 //
40 // `vndk: { enabled: true }` must set together.
41 //
42 // All these modules are allowed to link to VNDK-SP or LL-NDK
43 // modules only. Other dependency will cause link-type errors.
44 //
45 // If `support_system_process` is not set or set to false,
46 // the module is VNDK-core and can link to other VNDK-core,
47 // VNDK-SP or LL-NDK modules only.
48 Support_system_process *bool
Logan Chienf3511742017-10-31 18:04:35 +080049
50 // Extending another module
51 Extends *string
Justin Yun8effde42017-06-23 19:24:43 +090052 }
53}
54
55type vndkdep struct {
56 Properties VndkProperties
57}
58
59func (vndk *vndkdep) props() []interface{} {
60 return []interface{}{&vndk.Properties}
61}
62
63func (vndk *vndkdep) begin(ctx BaseModuleContext) {}
64
65func (vndk *vndkdep) deps(ctx BaseModuleContext, deps Deps) Deps {
66 return deps
67}
68
69func (vndk *vndkdep) isVndk() bool {
70 return Bool(vndk.Properties.Vndk.Enabled)
71}
72
73func (vndk *vndkdep) isVndkSp() bool {
74 return Bool(vndk.Properties.Vndk.Support_system_process)
75}
76
Logan Chienf3511742017-10-31 18:04:35 +080077func (vndk *vndkdep) isVndkExt() bool {
78 return vndk.Properties.Vndk.Extends != nil
79}
80
81func (vndk *vndkdep) getVndkExtendsModuleName() string {
82 return String(vndk.Properties.Vndk.Extends)
83}
84
Justin Yun8effde42017-06-23 19:24:43 +090085func (vndk *vndkdep) typeName() string {
86 if !vndk.isVndk() {
87 return "native:vendor"
88 }
Logan Chienf3511742017-10-31 18:04:35 +080089 if !vndk.isVndkExt() {
90 if !vndk.isVndkSp() {
91 return "native:vendor:vndk"
92 }
93 return "native:vendor:vndksp"
Justin Yun8effde42017-06-23 19:24:43 +090094 }
Logan Chienf3511742017-10-31 18:04:35 +080095 if !vndk.isVndkSp() {
96 return "native:vendor:vndkext"
97 }
98 return "native:vendor:vndkspext"
Justin Yun8effde42017-06-23 19:24:43 +090099}
100
Ivan Lozano183a3212019-10-18 14:18:45 -0700101func (vndk *vndkdep) vndkCheckLinkType(ctx android.ModuleContext, to *Module, tag DependencyTag) {
Justin Yun8effde42017-06-23 19:24:43 +0900102 if to.linker == nil {
103 return
104 }
Jiyong Park82e2bf32017-08-16 14:05:54 +0900105 if !vndk.isVndk() {
106 // Non-VNDK modules (those installed to /vendor) can't depend on modules marked with
107 // vendor_available: false.
108 violation := false
Nan Zhang0007d812017-11-07 10:57:05 -0800109 if lib, ok := to.linker.(*llndkStubDecorator); ok && !Bool(lib.Properties.Vendor_available) {
Jiyong Park82e2bf32017-08-16 14:05:54 +0900110 violation = true
111 } else {
112 if _, ok := to.linker.(libraryInterface); ok && to.VendorProperties.Vendor_available != nil && !Bool(to.VendorProperties.Vendor_available) {
113 // Vendor_available == nil && !Bool(Vendor_available) should be okay since
114 // it means a vendor-only library which is a valid dependency for non-VNDK
115 // modules.
116 violation = true
117 }
118 }
119 if violation {
120 ctx.ModuleErrorf("Vendor module that is not VNDK should not link to %q which is marked as `vendor_available: false`", to.Name())
121 }
122 }
Justin Yun8effde42017-06-23 19:24:43 +0900123 if lib, ok := to.linker.(*libraryDecorator); !ok || !lib.shared() {
124 // Check only shared libraries.
125 // Other (static and LL-NDK) libraries are allowed to link.
126 return
127 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700128 if !to.UseVndk() {
Justin Yun8effde42017-06-23 19:24:43 +0900129 ctx.ModuleErrorf("(%s) should not link to %q which is not a vendor-available library",
130 vndk.typeName(), to.Name())
131 return
132 }
Logan Chienf3511742017-10-31 18:04:35 +0800133 if tag == vndkExtDepTag {
134 // Ensure `extends: "name"` property refers a vndk module that has vendor_available
135 // and has identical vndk properties.
136 if to.vndkdep == nil || !to.vndkdep.isVndk() {
137 ctx.ModuleErrorf("`extends` refers a non-vndk module %q", to.Name())
138 return
139 }
140 if vndk.isVndkSp() != to.vndkdep.isVndkSp() {
141 ctx.ModuleErrorf(
142 "`extends` refers a module %q with mismatched support_system_process",
143 to.Name())
144 return
145 }
146 if !Bool(to.VendorProperties.Vendor_available) {
147 ctx.ModuleErrorf(
148 "`extends` refers module %q which does not have `vendor_available: true`",
149 to.Name())
150 return
151 }
152 }
Justin Yun8effde42017-06-23 19:24:43 +0900153 if to.vndkdep == nil {
154 return
155 }
Logan Chienf3511742017-10-31 18:04:35 +0800156
Logan Chiend3c59a22018-03-29 14:08:15 +0800157 // Check the dependencies of VNDK shared libraries.
Martin Stjernholm257eb0c2018-10-15 13:05:27 +0100158 if err := vndkIsVndkDepAllowed(vndk, to.vndkdep); err != nil {
159 ctx.ModuleErrorf("(%s) should not link to %q (%s): %v",
160 vndk.typeName(), to.Name(), to.vndkdep.typeName(), err)
Logan Chienf3511742017-10-31 18:04:35 +0800161 return
162 }
Logan Chiend3c59a22018-03-29 14:08:15 +0800163}
Logan Chienf3511742017-10-31 18:04:35 +0800164
Martin Stjernholm257eb0c2018-10-15 13:05:27 +0100165func vndkIsVndkDepAllowed(from *vndkdep, to *vndkdep) error {
Logan Chiend3c59a22018-03-29 14:08:15 +0800166 // Check the dependencies of VNDK, VNDK-Ext, VNDK-SP, VNDK-SP-Ext and vendor modules.
167 if from.isVndkExt() {
168 if from.isVndkSp() {
Martin Stjernholm257eb0c2018-10-15 13:05:27 +0100169 if to.isVndk() && !to.isVndkSp() {
170 return errors.New("VNDK-SP extensions must not depend on VNDK or VNDK extensions")
171 }
172 return nil
Logan Chiend3c59a22018-03-29 14:08:15 +0800173 }
174 // VNDK-Ext may depend on VNDK, VNDK-Ext, VNDK-SP, VNDK-SP-Ext, or vendor libs.
Martin Stjernholm257eb0c2018-10-15 13:05:27 +0100175 return nil
Justin Yun8effde42017-06-23 19:24:43 +0900176 }
Logan Chiend3c59a22018-03-29 14:08:15 +0800177 if from.isVndk() {
178 if to.isVndkExt() {
Martin Stjernholm257eb0c2018-10-15 13:05:27 +0100179 return errors.New("VNDK-core and VNDK-SP must not depend on VNDK extensions")
Logan Chiend3c59a22018-03-29 14:08:15 +0800180 }
181 if from.isVndkSp() {
Martin Stjernholm257eb0c2018-10-15 13:05:27 +0100182 if !to.isVndkSp() {
183 return errors.New("VNDK-SP must only depend on VNDK-SP")
184 }
185 return nil
Logan Chiend3c59a22018-03-29 14:08:15 +0800186 }
Martin Stjernholm257eb0c2018-10-15 13:05:27 +0100187 if !to.isVndk() {
188 return errors.New("VNDK-core must only depend on VNDK-core or VNDK-SP")
189 }
190 return nil
Logan Chiend3c59a22018-03-29 14:08:15 +0800191 }
192 // Vendor modules may depend on VNDK, VNDK-Ext, VNDK-SP, VNDK-SP-Ext, or vendor libs.
Martin Stjernholm257eb0c2018-10-15 13:05:27 +0100193 return nil
Justin Yun8effde42017-06-23 19:24:43 +0900194}
Jiyong Parkd5b18a52017-08-03 21:22:50 +0900195
196var (
Jooyung Han097087b2019-10-22 19:32:18 +0900197 vndkCoreLibrariesKey = android.NewOnceKey("vndkCoreLibrarires")
198 vndkSpLibrariesKey = android.NewOnceKey("vndkSpLibrarires")
199 llndkLibrariesKey = android.NewOnceKey("llndkLibrarires")
200 vndkPrivateLibrariesKey = android.NewOnceKey("vndkPrivateLibrarires")
201 vndkUsingCoreVariantLibrariesKey = android.NewOnceKey("vndkUsingCoreVariantLibrarires")
202 modulePathsKey = android.NewOnceKey("modulePaths")
203 vndkSnapshotOutputsKey = android.NewOnceKey("vndkSnapshotOutputs")
204 vndkMustUseVendorVariantListKey = android.NewOnceKey("vndkMustUseVendorVariantListKey")
205 testVndkMustUseVendorVariantListKey = android.NewOnceKey("testVndkMustUseVendorVariantListKey")
206 vndkLibrariesLock sync.Mutex
Jiyong Parkd5b18a52017-08-03 21:22:50 +0900207
Inseob Kimae553032019-05-14 18:52:49 +0900208 headerExts = []string{".h", ".hh", ".hpp", ".hxx", ".h++", ".inl", ".inc", ".ipp", ".h.generic"}
209)
Inseob Kim1f086e22019-05-09 13:29:15 +0900210
Inseob Kim9516ee92019-05-09 10:56:13 +0900211func vndkCoreLibraries(config android.Config) *[]string {
212 return config.Once(vndkCoreLibrariesKey, func() interface{} {
213 return &[]string{}
214 }).(*[]string)
215}
216
217func vndkSpLibraries(config android.Config) *[]string {
218 return config.Once(vndkSpLibrariesKey, func() interface{} {
219 return &[]string{}
220 }).(*[]string)
221}
222
223func llndkLibraries(config android.Config) *[]string {
224 return config.Once(llndkLibrariesKey, func() interface{} {
225 return &[]string{}
226 }).(*[]string)
227}
228
229func vndkPrivateLibraries(config android.Config) *[]string {
230 return config.Once(vndkPrivateLibrariesKey, func() interface{} {
231 return &[]string{}
232 }).(*[]string)
233}
234
235func vndkUsingCoreVariantLibraries(config android.Config) *[]string {
236 return config.Once(vndkUsingCoreVariantLibrariesKey, func() interface{} {
237 return &[]string{}
238 }).(*[]string)
239}
240
Inseob Kim1f086e22019-05-09 13:29:15 +0900241func modulePaths(config android.Config) map[string]string {
242 return config.Once(modulePathsKey, func() interface{} {
243 return make(map[string]string)
244 }).(map[string]string)
245}
Inseob Kim9516ee92019-05-09 10:56:13 +0900246
Inseob Kimae553032019-05-14 18:52:49 +0900247func vndkSnapshotOutputs(config android.Config) *android.RuleBuilderInstalls {
Inseob Kim1f086e22019-05-09 13:29:15 +0900248 return config.Once(vndkSnapshotOutputsKey, func() interface{} {
Inseob Kimae553032019-05-14 18:52:49 +0900249 return &android.RuleBuilderInstalls{}
250 }).(*android.RuleBuilderInstalls)
Inseob Kim1f086e22019-05-09 13:29:15 +0900251}
Inseob Kim9516ee92019-05-09 10:56:13 +0900252
Jooyung Han097087b2019-10-22 19:32:18 +0900253func vndkMustUseVendorVariantList(cfg android.Config) []string {
254 return cfg.Once(vndkMustUseVendorVariantListKey, func() interface{} {
255 override := cfg.Once(testVndkMustUseVendorVariantListKey, func() interface{} {
256 return []string(nil)
257 }).([]string)
258 if override != nil {
259 return override
260 }
261 return config.VndkMustUseVendorVariantList
262 }).([]string)
263}
264
265// test may call this to override global configuration(config.VndkMustUseVendorVariantList)
266// when it is called, it must be before the first call to vndkMustUseVendorVariantList()
267func setVndkMustUseVendorVariantListForTest(config android.Config, mustUseVendorVariantList []string) {
268 config.Once(testVndkMustUseVendorVariantListKey, func() interface{} {
269 return mustUseVendorVariantList
270 })
271}
272
Inseob Kim1f086e22019-05-09 13:29:15 +0900273func processLlndkLibrary(mctx android.BottomUpMutatorContext, m *Module) {
274 lib := m.linker.(*llndkStubDecorator)
275 name := strings.TrimSuffix(m.Name(), llndkLibrarySuffix)
Inseob Kim9516ee92019-05-09 10:56:13 +0900276
Inseob Kim1f086e22019-05-09 13:29:15 +0900277 vndkLibrariesLock.Lock()
278 defer vndkLibrariesLock.Unlock()
Inseob Kim9516ee92019-05-09 10:56:13 +0900279
Inseob Kim1f086e22019-05-09 13:29:15 +0900280 llndkLibraries := llndkLibraries(mctx.Config())
281 if !inList(name, *llndkLibraries) {
282 *llndkLibraries = append(*llndkLibraries, name)
283 sort.Strings(*llndkLibraries)
284 }
285 if !Bool(lib.Properties.Vendor_available) {
286 vndkPrivateLibraries := vndkPrivateLibraries(mctx.Config())
287 if !inList(name, *vndkPrivateLibraries) {
288 *vndkPrivateLibraries = append(*vndkPrivateLibraries, name)
289 sort.Strings(*vndkPrivateLibraries)
Jiyong Parkd5b18a52017-08-03 21:22:50 +0900290 }
291 }
292}
Inseob Kim1f086e22019-05-09 13:29:15 +0900293
294func processVndkLibrary(mctx android.BottomUpMutatorContext, m *Module) {
295 name := strings.TrimPrefix(m.Name(), "prebuilt_")
296
297 vndkLibrariesLock.Lock()
298 defer vndkLibrariesLock.Unlock()
299
300 modulePaths := modulePaths(mctx.Config())
Jooyung Han097087b2019-10-22 19:32:18 +0900301 if inList(name, vndkMustUseVendorVariantList(mctx.Config())) {
302 m.Properties.MustUseVendorVariant = true
303 }
Jooyung Han6db2f172019-10-28 14:12:48 +0900304 if mctx.DeviceConfig().VndkUseCoreVariant() && !inList(name, vndkMustUseVendorVariantList(mctx.Config())) {
Inseob Kim1f086e22019-05-09 13:29:15 +0900305 vndkUsingCoreVariantLibraries := vndkUsingCoreVariantLibraries(mctx.Config())
306 if !inList(name, *vndkUsingCoreVariantLibraries) {
307 *vndkUsingCoreVariantLibraries = append(*vndkUsingCoreVariantLibraries, name)
308 sort.Strings(*vndkUsingCoreVariantLibraries)
309 }
310 }
311 if m.vndkdep.isVndkSp() {
312 vndkSpLibraries := vndkSpLibraries(mctx.Config())
313 if !inList(name, *vndkSpLibraries) {
314 *vndkSpLibraries = append(*vndkSpLibraries, name)
315 sort.Strings(*vndkSpLibraries)
316 modulePaths[name] = mctx.ModuleDir()
317 }
318 } else {
319 vndkCoreLibraries := vndkCoreLibraries(mctx.Config())
320 if !inList(name, *vndkCoreLibraries) {
321 *vndkCoreLibraries = append(*vndkCoreLibraries, name)
322 sort.Strings(*vndkCoreLibraries)
323 modulePaths[name] = mctx.ModuleDir()
324 }
325 }
326 if !Bool(m.VendorProperties.Vendor_available) {
327 vndkPrivateLibraries := vndkPrivateLibraries(mctx.Config())
328 if !inList(name, *vndkPrivateLibraries) {
329 *vndkPrivateLibraries = append(*vndkPrivateLibraries, name)
330 sort.Strings(*vndkPrivateLibraries)
331 }
332 }
333}
334
Jooyung Han31c470b2019-10-18 16:26:59 +0900335func IsForVndkApex(mctx android.BottomUpMutatorContext, m *Module) bool {
336 if !m.Enabled() {
337 return false
338 }
339
340 if m.Target().NativeBridge == android.NativeBridgeEnabled {
341 return false
342 }
343
344 // prebuilt vndk modules should match with device
345 // TODO(b/142675459): Use enabled: to select target device in vndk_prebuilt_shared
346 // When b/142675459 is landed, remove following check
347 if p, ok := m.linker.(*vndkPrebuiltLibraryDecorator); ok && !p.matchesWithDevice(mctx.DeviceConfig()) {
348 return false
349 }
350
351 if lib, ok := m.linker.(libraryInterface); ok {
352 useCoreVariant := m.vndkVersion() == mctx.DeviceConfig().PlatformVndkVersion() &&
353 mctx.DeviceConfig().VndkUseCoreVariant() &&
354 !inList(m.BaseModuleName(), config.VndkMustUseVendorVariantList)
Ivan Lozano52767be2019-10-18 14:49:46 -0700355 return lib.shared() && m.UseVndk() && m.IsVndk() && !m.isVndkExt() && !useCoreVariant
Jooyung Han31c470b2019-10-18 16:26:59 +0900356 }
357 return false
358}
359
Inseob Kim1f086e22019-05-09 13:29:15 +0900360// gather list of vndk-core, vndk-sp, and ll-ndk libs
361func VndkMutator(mctx android.BottomUpMutatorContext) {
362 m, ok := mctx.Module().(*Module)
363 if !ok {
364 return
365 }
Inseob Kim1f086e22019-05-09 13:29:15 +0900366 if !m.Enabled() {
367 return
368 }
Justin Yun7390ea32019-09-08 11:34:06 +0900369 if m.Target().NativeBridge == android.NativeBridgeEnabled {
370 // Skip native_bridge modules
371 return
372 }
Inseob Kim1f086e22019-05-09 13:29:15 +0900373
374 if _, ok := m.linker.(*llndkStubDecorator); ok {
375 processLlndkLibrary(mctx, m)
376 return
377 }
378
379 lib, is_lib := m.linker.(*libraryDecorator)
380 prebuilt_lib, is_prebuilt_lib := m.linker.(*prebuiltLibraryLinker)
381
Inseob Kim64c43952019-08-26 16:52:35 +0900382 if (is_lib && lib.buildShared()) || (is_prebuilt_lib && prebuilt_lib.buildShared()) {
383 if m.vndkdep != nil && m.vndkdep.isVndk() && !m.vndkdep.isVndkExt() {
Inseob Kim1f086e22019-05-09 13:29:15 +0900384 processVndkLibrary(mctx, m)
385 return
386 }
387 }
388}
389
390func init() {
391 android.RegisterSingletonType("vndk-snapshot", VndkSnapshotSingleton)
392 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
393 outputs := vndkSnapshotOutputs(ctx.Config())
Inseob Kimae553032019-05-14 18:52:49 +0900394 ctx.Strict("SOONG_VNDK_SNAPSHOT_FILES", outputs.String())
Inseob Kim1f086e22019-05-09 13:29:15 +0900395 })
396}
397
398func VndkSnapshotSingleton() android.Singleton {
399 return &vndkSnapshotSingleton{}
400}
401
402type vndkSnapshotSingleton struct{}
403
Inseob Kim1f086e22019-05-09 13:29:15 +0900404func (c *vndkSnapshotSingleton) GenerateBuildActions(ctx android.SingletonContext) {
405 // BOARD_VNDK_VERSION must be set to 'current' in order to generate a VNDK snapshot.
406 if ctx.DeviceConfig().VndkVersion() != "current" {
407 return
408 }
409
410 if ctx.DeviceConfig().PlatformVndkVersion() == "" {
411 return
412 }
413
414 if ctx.DeviceConfig().BoardVndkRuntimeDisable() {
415 return
416 }
417
Jooyung Han097087b2019-10-22 19:32:18 +0900418 c.buildVndkLibrariesTxtFiles(ctx)
419
Inseob Kim1f086e22019-05-09 13:29:15 +0900420 outputs := vndkSnapshotOutputs(ctx.Config())
421
422 snapshotDir := "vndk-snapshot"
423
Inseob Kimae553032019-05-14 18:52:49 +0900424 vndkLibDir := make(map[android.ArchType]string)
Inseob Kim1f086e22019-05-09 13:29:15 +0900425
Inseob Kimae553032019-05-14 18:52:49 +0900426 snapshotVariantDir := ctx.DeviceConfig().DeviceArch()
427 for _, target := range ctx.Config().Targets[android.Android] {
428 dir := snapshotVariantDir
429 if ctx.DeviceConfig().BinderBitness() == "32" {
430 dir = filepath.Join(dir, "binder32")
431 }
432 arch := "arch-" + target.Arch.ArchType.String()
433 if target.Arch.ArchVariant != "" {
434 arch += "-" + target.Arch.ArchVariant
435 }
436 dir = filepath.Join(dir, arch)
437 vndkLibDir[target.Arch.ArchType] = dir
Inseob Kim1f086e22019-05-09 13:29:15 +0900438 }
Inseob Kimae553032019-05-14 18:52:49 +0900439 configsDir := filepath.Join(snapshotVariantDir, "configs")
440 noticeDir := filepath.Join(snapshotVariantDir, "NOTICE_FILES")
441 includeDir := filepath.Join(snapshotVariantDir, "include")
Inseob Kim1f086e22019-05-09 13:29:15 +0900442 noticeBuilt := make(map[string]bool)
443
Inseob Kimae553032019-05-14 18:52:49 +0900444 installSnapshotFileFromPath := func(path android.Path, out string) {
445 ctx.Build(pctx, android.BuildParams{
446 Rule: android.Cp,
447 Input: path,
448 Output: android.PathForOutput(ctx, snapshotDir, out),
449 Description: "vndk snapshot " + out,
450 Args: map[string]string{
451 "cpFlags": "-f -L",
452 },
453 })
454 *outputs = append(*outputs, android.RuleBuilderInstall{
455 From: android.PathForOutput(ctx, snapshotDir, out),
456 To: out,
457 })
458 }
459 installSnapshotFileFromContent := func(content, out string) {
460 ctx.Build(pctx, android.BuildParams{
461 Rule: android.WriteFile,
462 Output: android.PathForOutput(ctx, snapshotDir, out),
463 Description: "vndk snapshot " + out,
464 Args: map[string]string{
465 "content": content,
466 },
467 })
468 *outputs = append(*outputs, android.RuleBuilderInstall{
469 From: android.PathForOutput(ctx, snapshotDir, out),
470 To: out,
471 })
472 }
473
Inseob Kim1f086e22019-05-09 13:29:15 +0900474 tryBuildNotice := func(m *Module) {
Inseob Kimae553032019-05-14 18:52:49 +0900475 name := ctx.ModuleName(m) + ".so.txt"
Inseob Kim1f086e22019-05-09 13:29:15 +0900476
477 if _, ok := noticeBuilt[name]; ok {
478 return
479 }
480
481 noticeBuilt[name] = true
482
483 if m.NoticeFile().Valid() {
Inseob Kimae553032019-05-14 18:52:49 +0900484 installSnapshotFileFromPath(m.NoticeFile().Path(), filepath.Join(noticeDir, name))
Inseob Kim1f086e22019-05-09 13:29:15 +0900485 }
486 }
487
488 vndkCoreLibraries := vndkCoreLibraries(ctx.Config())
489 vndkSpLibraries := vndkSpLibraries(ctx.Config())
490 vndkPrivateLibraries := vndkPrivateLibraries(ctx.Config())
491
Inseob Kimae553032019-05-14 18:52:49 +0900492 var generatedHeaders android.Paths
493 includeDirs := make(map[string]bool)
494
495 type vndkSnapshotLibraryInterface interface {
496 exportedFlagsProducer
497 libraryInterface
498 }
499
500 var _ vndkSnapshotLibraryInterface = (*prebuiltLibraryLinker)(nil)
501 var _ vndkSnapshotLibraryInterface = (*libraryDecorator)(nil)
502
503 installVndkSnapshotLib := func(m *Module, l vndkSnapshotLibraryInterface, dir string) bool {
504 name := ctx.ModuleName(m)
505 libOut := filepath.Join(dir, name+".so")
506
507 installSnapshotFileFromPath(m.outputFile.Path(), libOut)
508 tryBuildNotice(m)
509
510 if ctx.Config().VndkSnapshotBuildArtifacts() {
511 prop := struct {
512 ExportedDirs []string `json:",omitempty"`
513 ExportedSystemDirs []string `json:",omitempty"`
514 ExportedFlags []string `json:",omitempty"`
515 RelativeInstallPath string `json:",omitempty"`
516 }{}
517 prop.ExportedFlags = l.exportedFlags()
Jiyong Park74955042019-10-22 20:19:51 +0900518 prop.ExportedDirs = l.exportedDirs().Strings()
519 prop.ExportedSystemDirs = l.exportedSystemDirs().Strings()
Inseob Kimae553032019-05-14 18:52:49 +0900520 prop.RelativeInstallPath = m.RelativeInstallPath()
521
522 propOut := libOut + ".json"
523
524 j, err := json.Marshal(prop)
525 if err != nil {
526 ctx.Errorf("json marshal to %q failed: %#v", propOut, err)
527 return false
528 }
529
530 installSnapshotFileFromContent(string(j), propOut)
531 }
532 return true
533 }
534
535 isVndkSnapshotLibrary := func(m *Module) (i vndkSnapshotLibraryInterface, libDir string, isVndkSnapshotLib bool) {
536 if m.Target().NativeBridge == android.NativeBridgeEnabled {
537 return nil, "", false
538 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700539 if !m.UseVndk() || !m.IsForPlatform() || !m.installable() {
Inseob Kimae553032019-05-14 18:52:49 +0900540 return nil, "", false
541 }
542 l, ok := m.linker.(vndkSnapshotLibraryInterface)
543 if !ok || !l.shared() {
544 return nil, "", false
545 }
546 name := ctx.ModuleName(m)
547 if inList(name, *vndkCoreLibraries) {
548 return l, filepath.Join("shared", "vndk-core"), true
549 } else if inList(name, *vndkSpLibraries) {
550 return l, filepath.Join("shared", "vndk-sp"), true
551 } else {
552 return nil, "", false
553 }
554 }
555
Inseob Kim1f086e22019-05-09 13:29:15 +0900556 ctx.VisitAllModules(func(module android.Module) {
557 m, ok := module.(*Module)
Inseob Kimae553032019-05-14 18:52:49 +0900558 if !ok || !m.Enabled() {
Inseob Kim1f086e22019-05-09 13:29:15 +0900559 return
560 }
561
Inseob Kimae553032019-05-14 18:52:49 +0900562 baseDir, ok := vndkLibDir[m.Target().Arch.ArchType]
563 if !ok {
dimitry51ea18a2019-05-20 10:39:52 +0200564 return
565 }
566
Inseob Kimae553032019-05-14 18:52:49 +0900567 l, libDir, ok := isVndkSnapshotLibrary(m)
568 if !ok {
Inseob Kim1f086e22019-05-09 13:29:15 +0900569 return
570 }
571
Inseob Kimae553032019-05-14 18:52:49 +0900572 if !installVndkSnapshotLib(m, l, filepath.Join(baseDir, libDir)) {
573 return
574 }
Inseob Kim1f086e22019-05-09 13:29:15 +0900575
Inseob Kimae553032019-05-14 18:52:49 +0900576 generatedHeaders = append(generatedHeaders, l.exportedDeps()...)
577 for _, dir := range append(l.exportedDirs(), l.exportedSystemDirs()...) {
Jiyong Park74955042019-10-22 20:19:51 +0900578 includeDirs[dir.String()] = true
Inseob Kimae553032019-05-14 18:52:49 +0900579 }
580 })
Inseob Kim1f086e22019-05-09 13:29:15 +0900581
Inseob Kimae553032019-05-14 18:52:49 +0900582 if ctx.Config().VndkSnapshotBuildArtifacts() {
583 headers := make(map[string]bool)
584
585 for _, dir := range android.SortedStringKeys(includeDirs) {
586 // workaround to determine if dir is under output directory
587 if strings.HasPrefix(dir, android.PathForOutput(ctx).String()) {
588 continue
Inseob Kim1f086e22019-05-09 13:29:15 +0900589 }
Inseob Kimae553032019-05-14 18:52:49 +0900590 exts := headerExts
591 // Glob all files under this special directory, because of C++ headers.
592 if strings.HasPrefix(dir, "external/libcxx/include") {
593 exts = []string{""}
Inseob Kim1f086e22019-05-09 13:29:15 +0900594 }
Inseob Kimae553032019-05-14 18:52:49 +0900595 for _, ext := range exts {
596 glob, err := ctx.GlobWithDeps(dir+"/**/*"+ext, nil)
597 if err != nil {
598 ctx.Errorf("%#v\n", err)
599 return
600 }
601 for _, header := range glob {
602 if strings.HasSuffix(header, "/") {
603 continue
604 }
605 headers[header] = true
606 }
607 }
Inseob Kim1f086e22019-05-09 13:29:15 +0900608 }
Inseob Kim1f086e22019-05-09 13:29:15 +0900609
Inseob Kimae553032019-05-14 18:52:49 +0900610 for _, header := range android.SortedStringKeys(headers) {
611 installSnapshotFileFromPath(android.PathForSource(ctx, header),
612 filepath.Join(includeDir, header))
613 }
Inseob Kim1f086e22019-05-09 13:29:15 +0900614
Inseob Kimae553032019-05-14 18:52:49 +0900615 isHeader := func(path string) bool {
616 for _, ext := range headerExts {
617 if strings.HasSuffix(path, ext) {
618 return true
619 }
620 }
621 return false
622 }
Inseob Kim1f086e22019-05-09 13:29:15 +0900623
Inseob Kimae553032019-05-14 18:52:49 +0900624 for _, path := range android.PathsToDirectorySortedPaths(android.FirstUniquePaths(generatedHeaders)) {
625 header := path.String()
626
627 if !isHeader(header) {
628 continue
629 }
630
631 installSnapshotFileFromPath(path, filepath.Join(includeDir, header))
632 }
633 }
634
635 installSnapshotFileFromContent(android.JoinWithSuffix(*vndkCoreLibraries, ".so", "\\n"),
636 filepath.Join(configsDir, "vndkcore.libraries.txt"))
637 installSnapshotFileFromContent(android.JoinWithSuffix(*vndkPrivateLibraries, ".so", "\\n"),
638 filepath.Join(configsDir, "vndkprivate.libraries.txt"))
Inseob Kim1f086e22019-05-09 13:29:15 +0900639
640 var modulePathTxtBuilder strings.Builder
641
Colin Cross4c2c46f2019-06-03 15:26:05 -0700642 modulePaths := modulePaths(ctx.Config())
Colin Cross4c2c46f2019-06-03 15:26:05 -0700643
Inseob Kim1f086e22019-05-09 13:29:15 +0900644 first := true
Inseob Kimae553032019-05-14 18:52:49 +0900645 for _, lib := range android.SortedStringKeys(modulePaths) {
Inseob Kim1f086e22019-05-09 13:29:15 +0900646 if first {
647 first = false
648 } else {
649 modulePathTxtBuilder.WriteString("\\n")
650 }
651 modulePathTxtBuilder.WriteString(lib)
652 modulePathTxtBuilder.WriteString(".so ")
Colin Cross4c2c46f2019-06-03 15:26:05 -0700653 modulePathTxtBuilder.WriteString(modulePaths[lib])
Inseob Kim1f086e22019-05-09 13:29:15 +0900654 }
655
Inseob Kimae553032019-05-14 18:52:49 +0900656 installSnapshotFileFromContent(modulePathTxtBuilder.String(),
657 filepath.Join(configsDir, "module_paths.txt"))
Inseob Kim1f086e22019-05-09 13:29:15 +0900658}
Jooyung Han097087b2019-10-22 19:32:18 +0900659
660func installListFile(ctx android.SingletonContext, list []string, pathComponents ...string) android.OutputPath {
661 out := android.PathForOutput(ctx, pathComponents...)
662 ctx.Build(pctx, android.BuildParams{
663 Rule: android.WriteFile,
664 Output: out,
665 Description: "Writing " + out.String(),
666 Args: map[string]string{
667 "content": strings.Join(list, "\\n"),
668 },
669 })
670 return out
671}
672
673func (c *vndkSnapshotSingleton) buildVndkLibrariesTxtFiles(ctx android.SingletonContext) {
674 var (
675 llndk, vndkcore, vndksp, vndkprivate, vndkcorevariant, merged []string
676 )
677 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
678 config := ctx.Config()
679 ctx.VisitAllModules(func(m android.Module) {
680 if !m.Enabled() {
681 return
682 }
683 c, ok := m.(*Module)
684 if !ok || c.Os().Class != android.Device {
685 return
686 }
687 lib, ok := c.linker.(interface{ shared() bool })
688 if !ok || !lib.shared() {
689 return
690 }
691
692 if !c.OutputFile().Valid() {
693 return
694 }
695
696 filename := c.OutputFile().Path().Base()
697 if c.isLlndk(config) {
698 llndk = append(llndk, filename)
699 if c.isVndkPrivate(config) {
700 vndkprivate = append(vndkprivate, filename)
701 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700702 } else if c.vndkVersion() == vndkVersion && c.IsVndk() && !c.isVndkExt() {
Jooyung Han097087b2019-10-22 19:32:18 +0900703 if c.isVndkSp() {
704 vndksp = append(vndksp, filename)
705 } else {
706 vndkcore = append(vndkcore, filename)
707 }
708 if c.isVndkPrivate(config) {
709 vndkprivate = append(vndkprivate, filename)
710 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700711 if ctx.DeviceConfig().VndkUseCoreVariant() && !c.MustUseVendorVariant() {
Jooyung Han097087b2019-10-22 19:32:18 +0900712 vndkcorevariant = append(vndkcorevariant, filename)
713 }
714 }
715 })
716 llndk = android.SortedUniqueStrings(llndk)
717 vndkcore = android.SortedUniqueStrings(vndkcore)
718 vndksp = android.SortedUniqueStrings(vndksp)
719 vndkprivate = android.SortedUniqueStrings(vndkprivate)
720 vndkcorevariant = android.SortedUniqueStrings(vndkcorevariant)
721
722 installListFile(ctx, llndk, "vndk", "llndk.libraries.txt")
723 installListFile(ctx, vndkcore, "vndk", "vndkcore.libraries.txt")
724 installListFile(ctx, vndksp, "vndk", "vndksp.libraries.txt")
725 installListFile(ctx, vndkprivate, "vndk", "vndkprivate.libraries.txt")
726 installListFile(ctx, vndkcorevariant, "vndk", "vndkcorevariant.libraries.txt")
727
728 // merged & tagged & filtered-out(libclang_rt)
729 filterOutLibClangRt := func(libList []string) (filtered []string) {
730 for _, lib := range libList {
731 if !strings.HasPrefix(lib, "libclang_rt.") {
732 filtered = append(filtered, lib)
733 }
734 }
735 return
736 }
737 merged = append(merged, addPrefix(filterOutLibClangRt(llndk), "LLNDK: ")...)
738 merged = append(merged, addPrefix(vndksp, "VNDK-SP: ")...)
739 merged = append(merged, addPrefix(filterOutLibClangRt(vndkcore), "VNDK-core: ")...)
740 merged = append(merged, addPrefix(vndkprivate, "VNDK-private: ")...)
741
742 installListFile(ctx, merged, "vndk", "vndk.libraries.txt")
743}