blob: f39ee502486e3d631b399a8e48a6ef84d31087b8 [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"
Jooyung Han0302a842019-10-30 18:43:49 +090020 "fmt"
Inseob Kim1f086e22019-05-09 13:29:15 +090021 "path/filepath"
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
Jooyung Han0302a842019-10-30 18:43:49 +0900211func vndkCoreLibraries(config android.Config) map[string]string {
Inseob Kim9516ee92019-05-09 10:56:13 +0900212 return config.Once(vndkCoreLibrariesKey, func() interface{} {
Jooyung Han0302a842019-10-30 18:43:49 +0900213 return make(map[string]string)
214 }).(map[string]string)
Inseob Kim9516ee92019-05-09 10:56:13 +0900215}
216
Jooyung Han0302a842019-10-30 18:43:49 +0900217func vndkSpLibraries(config android.Config) map[string]string {
Inseob Kim9516ee92019-05-09 10:56:13 +0900218 return config.Once(vndkSpLibrariesKey, func() interface{} {
Jooyung Han0302a842019-10-30 18:43:49 +0900219 return make(map[string]string)
220 }).(map[string]string)
Inseob Kim9516ee92019-05-09 10:56:13 +0900221}
222
Jooyung Han0302a842019-10-30 18:43:49 +0900223func isLlndkLibrary(baseModuleName string, config android.Config) bool {
224 _, ok := llndkLibraries(config)[baseModuleName]
225 return ok
226}
227
228func llndkLibraries(config android.Config) map[string]string {
Inseob Kim9516ee92019-05-09 10:56:13 +0900229 return config.Once(llndkLibrariesKey, func() interface{} {
Jooyung Han0302a842019-10-30 18:43:49 +0900230 return make(map[string]string)
231 }).(map[string]string)
Inseob Kim9516ee92019-05-09 10:56:13 +0900232}
233
Jooyung Han0302a842019-10-30 18:43:49 +0900234func isVndkPrivateLibrary(baseModuleName string, config android.Config) bool {
235 _, ok := vndkPrivateLibraries(config)[baseModuleName]
236 return ok
237}
238
239func vndkPrivateLibraries(config android.Config) map[string]string {
Inseob Kim9516ee92019-05-09 10:56:13 +0900240 return config.Once(vndkPrivateLibrariesKey, func() interface{} {
Jooyung Han0302a842019-10-30 18:43:49 +0900241 return make(map[string]string)
242 }).(map[string]string)
Inseob Kim9516ee92019-05-09 10:56:13 +0900243}
244
Jooyung Han0302a842019-10-30 18:43:49 +0900245func vndkUsingCoreVariantLibraries(config android.Config) map[string]string {
Inseob Kim9516ee92019-05-09 10:56:13 +0900246 return config.Once(vndkUsingCoreVariantLibrariesKey, func() interface{} {
Jooyung Han0302a842019-10-30 18:43:49 +0900247 return make(map[string]string)
248 }).(map[string]string)
Inseob Kim9516ee92019-05-09 10:56:13 +0900249}
250
Inseob Kim1f086e22019-05-09 13:29:15 +0900251func modulePaths(config android.Config) map[string]string {
252 return config.Once(modulePathsKey, func() interface{} {
253 return make(map[string]string)
254 }).(map[string]string)
255}
Inseob Kim9516ee92019-05-09 10:56:13 +0900256
Inseob Kimae553032019-05-14 18:52:49 +0900257func vndkSnapshotOutputs(config android.Config) *android.RuleBuilderInstalls {
Inseob Kim1f086e22019-05-09 13:29:15 +0900258 return config.Once(vndkSnapshotOutputsKey, func() interface{} {
Inseob Kimae553032019-05-14 18:52:49 +0900259 return &android.RuleBuilderInstalls{}
260 }).(*android.RuleBuilderInstalls)
Inseob Kim1f086e22019-05-09 13:29:15 +0900261}
Inseob Kim9516ee92019-05-09 10:56:13 +0900262
Jooyung Han097087b2019-10-22 19:32:18 +0900263func vndkMustUseVendorVariantList(cfg android.Config) []string {
264 return cfg.Once(vndkMustUseVendorVariantListKey, func() interface{} {
265 override := cfg.Once(testVndkMustUseVendorVariantListKey, func() interface{} {
266 return []string(nil)
267 }).([]string)
268 if override != nil {
269 return override
270 }
271 return config.VndkMustUseVendorVariantList
272 }).([]string)
273}
274
275// test may call this to override global configuration(config.VndkMustUseVendorVariantList)
276// when it is called, it must be before the first call to vndkMustUseVendorVariantList()
277func setVndkMustUseVendorVariantListForTest(config android.Config, mustUseVendorVariantList []string) {
278 config.Once(testVndkMustUseVendorVariantListKey, func() interface{} {
279 return mustUseVendorVariantList
280 })
281}
282
Inseob Kim1f086e22019-05-09 13:29:15 +0900283func processLlndkLibrary(mctx android.BottomUpMutatorContext, m *Module) {
284 lib := m.linker.(*llndkStubDecorator)
Jooyung Han0302a842019-10-30 18:43:49 +0900285 name := m.BaseModuleName()
286 filename := m.BaseModuleName() + ".so"
Inseob Kim9516ee92019-05-09 10:56:13 +0900287
Inseob Kim1f086e22019-05-09 13:29:15 +0900288 vndkLibrariesLock.Lock()
289 defer vndkLibrariesLock.Unlock()
Inseob Kim9516ee92019-05-09 10:56:13 +0900290
Jooyung Han0302a842019-10-30 18:43:49 +0900291 llndkLibraries(mctx.Config())[name] = filename
Inseob Kim1f086e22019-05-09 13:29:15 +0900292 if !Bool(lib.Properties.Vendor_available) {
Jooyung Han0302a842019-10-30 18:43:49 +0900293 vndkPrivateLibraries(mctx.Config())[name] = filename
Jiyong Parkd5b18a52017-08-03 21:22:50 +0900294 }
295}
Inseob Kim1f086e22019-05-09 13:29:15 +0900296
297func processVndkLibrary(mctx android.BottomUpMutatorContext, m *Module) {
Jooyung Han0302a842019-10-30 18:43:49 +0900298 name := m.BaseModuleName()
299 filename, err := getVndkFileName(m)
300 if err != nil {
301 panic(err)
302 }
Inseob Kim1f086e22019-05-09 13:29:15 +0900303
304 vndkLibrariesLock.Lock()
305 defer vndkLibrariesLock.Unlock()
306
Jooyung Han0302a842019-10-30 18:43:49 +0900307 modulePaths(mctx.Config())[name] = mctx.ModuleDir()
308
Jooyung Han097087b2019-10-22 19:32:18 +0900309 if inList(name, vndkMustUseVendorVariantList(mctx.Config())) {
310 m.Properties.MustUseVendorVariant = true
311 }
Jooyung Han0302a842019-10-30 18:43:49 +0900312 if mctx.DeviceConfig().VndkUseCoreVariant() && !m.Properties.MustUseVendorVariant {
313 vndkUsingCoreVariantLibraries(mctx.Config())[name] = filename
Inseob Kim1f086e22019-05-09 13:29:15 +0900314 }
Jooyung Han0302a842019-10-30 18:43:49 +0900315
Inseob Kim1f086e22019-05-09 13:29:15 +0900316 if m.vndkdep.isVndkSp() {
Jooyung Han0302a842019-10-30 18:43:49 +0900317 vndkSpLibraries(mctx.Config())[name] = filename
Inseob Kim1f086e22019-05-09 13:29:15 +0900318 } else {
Jooyung Han0302a842019-10-30 18:43:49 +0900319 vndkCoreLibraries(mctx.Config())[name] = filename
Inseob Kim1f086e22019-05-09 13:29:15 +0900320 }
321 if !Bool(m.VendorProperties.Vendor_available) {
Jooyung Han0302a842019-10-30 18:43:49 +0900322 vndkPrivateLibraries(mctx.Config())[name] = filename
Inseob Kim1f086e22019-05-09 13:29:15 +0900323 }
324}
325
Jooyung Han31c470b2019-10-18 16:26:59 +0900326func IsForVndkApex(mctx android.BottomUpMutatorContext, m *Module) bool {
327 if !m.Enabled() {
328 return false
329 }
330
Jooyung Han87a7f302019-10-29 05:18:21 +0900331 if !mctx.Device() {
332 return false
333 }
334
Jooyung Han31c470b2019-10-18 16:26:59 +0900335 if m.Target().NativeBridge == android.NativeBridgeEnabled {
336 return false
337 }
338
339 // prebuilt vndk modules should match with device
340 // TODO(b/142675459): Use enabled: to select target device in vndk_prebuilt_shared
341 // When b/142675459 is landed, remove following check
342 if p, ok := m.linker.(*vndkPrebuiltLibraryDecorator); ok && !p.matchesWithDevice(mctx.DeviceConfig()) {
343 return false
344 }
345
346 if lib, ok := m.linker.(libraryInterface); ok {
347 useCoreVariant := m.vndkVersion() == mctx.DeviceConfig().PlatformVndkVersion() &&
Jooyung Han87a7f302019-10-29 05:18:21 +0900348 mctx.DeviceConfig().VndkUseCoreVariant() && !m.MustUseVendorVariant()
Ivan Lozano52767be2019-10-18 14:49:46 -0700349 return lib.shared() && m.UseVndk() && m.IsVndk() && !m.isVndkExt() && !useCoreVariant
Jooyung Han31c470b2019-10-18 16:26:59 +0900350 }
351 return false
352}
353
Inseob Kim1f086e22019-05-09 13:29:15 +0900354// gather list of vndk-core, vndk-sp, and ll-ndk libs
355func VndkMutator(mctx android.BottomUpMutatorContext) {
356 m, ok := mctx.Module().(*Module)
357 if !ok {
358 return
359 }
Inseob Kim1f086e22019-05-09 13:29:15 +0900360 if !m.Enabled() {
361 return
362 }
Justin Yun7390ea32019-09-08 11:34:06 +0900363 if m.Target().NativeBridge == android.NativeBridgeEnabled {
364 // Skip native_bridge modules
365 return
366 }
Inseob Kim1f086e22019-05-09 13:29:15 +0900367
368 if _, ok := m.linker.(*llndkStubDecorator); ok {
369 processLlndkLibrary(mctx, m)
370 return
371 }
372
373 lib, is_lib := m.linker.(*libraryDecorator)
374 prebuilt_lib, is_prebuilt_lib := m.linker.(*prebuiltLibraryLinker)
375
Inseob Kim64c43952019-08-26 16:52:35 +0900376 if (is_lib && lib.buildShared()) || (is_prebuilt_lib && prebuilt_lib.buildShared()) {
377 if m.vndkdep != nil && m.vndkdep.isVndk() && !m.vndkdep.isVndkExt() {
Inseob Kim1f086e22019-05-09 13:29:15 +0900378 processVndkLibrary(mctx, m)
379 return
380 }
381 }
382}
383
384func init() {
385 android.RegisterSingletonType("vndk-snapshot", VndkSnapshotSingleton)
386 android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) {
387 outputs := vndkSnapshotOutputs(ctx.Config())
Inseob Kimae553032019-05-14 18:52:49 +0900388 ctx.Strict("SOONG_VNDK_SNAPSHOT_FILES", outputs.String())
Inseob Kim1f086e22019-05-09 13:29:15 +0900389 })
390}
391
392func VndkSnapshotSingleton() android.Singleton {
393 return &vndkSnapshotSingleton{}
394}
395
Jooyung Han0302a842019-10-30 18:43:49 +0900396type vndkSnapshotSingleton struct {
397 installedLlndkLibraries []string
398 llnkdLibrariesFile android.Path
399 vndkSpLibrariesFile android.Path
400 vndkCoreLibrariesFile android.Path
401 vndkPrivateLibrariesFile android.Path
402 vndkCoreVariantLibrariesFile android.Path
403 vndkLibrariesFile android.Path
404}
Inseob Kim1f086e22019-05-09 13:29:15 +0900405
Inseob Kim1f086e22019-05-09 13:29:15 +0900406func (c *vndkSnapshotSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Jooyung Han0302a842019-10-30 18:43:49 +0900407 // build these files even if PlatformVndkVersion or BoardVndkVersion is not set
408 c.buildVndkLibrariesTxtFiles(ctx)
409
Inseob Kim1f086e22019-05-09 13:29:15 +0900410 // BOARD_VNDK_VERSION must be set to 'current' in order to generate a VNDK snapshot.
411 if ctx.DeviceConfig().VndkVersion() != "current" {
412 return
413 }
414
415 if ctx.DeviceConfig().PlatformVndkVersion() == "" {
416 return
417 }
418
419 if ctx.DeviceConfig().BoardVndkRuntimeDisable() {
420 return
421 }
422
423 outputs := vndkSnapshotOutputs(ctx.Config())
424
425 snapshotDir := "vndk-snapshot"
426
Inseob Kimae553032019-05-14 18:52:49 +0900427 vndkLibDir := make(map[android.ArchType]string)
Inseob Kim1f086e22019-05-09 13:29:15 +0900428
Inseob Kimae553032019-05-14 18:52:49 +0900429 snapshotVariantDir := ctx.DeviceConfig().DeviceArch()
430 for _, target := range ctx.Config().Targets[android.Android] {
431 dir := snapshotVariantDir
432 if ctx.DeviceConfig().BinderBitness() == "32" {
433 dir = filepath.Join(dir, "binder32")
434 }
435 arch := "arch-" + target.Arch.ArchType.String()
436 if target.Arch.ArchVariant != "" {
437 arch += "-" + target.Arch.ArchVariant
438 }
439 dir = filepath.Join(dir, arch)
440 vndkLibDir[target.Arch.ArchType] = dir
Inseob Kim1f086e22019-05-09 13:29:15 +0900441 }
Inseob Kimae553032019-05-14 18:52:49 +0900442 configsDir := filepath.Join(snapshotVariantDir, "configs")
443 noticeDir := filepath.Join(snapshotVariantDir, "NOTICE_FILES")
444 includeDir := filepath.Join(snapshotVariantDir, "include")
Inseob Kim1f086e22019-05-09 13:29:15 +0900445 noticeBuilt := make(map[string]bool)
446
Inseob Kimae553032019-05-14 18:52:49 +0900447 installSnapshotFileFromPath := func(path android.Path, out string) {
448 ctx.Build(pctx, android.BuildParams{
449 Rule: android.Cp,
450 Input: path,
451 Output: android.PathForOutput(ctx, snapshotDir, out),
452 Description: "vndk snapshot " + out,
453 Args: map[string]string{
454 "cpFlags": "-f -L",
455 },
456 })
457 *outputs = append(*outputs, android.RuleBuilderInstall{
458 From: android.PathForOutput(ctx, snapshotDir, out),
459 To: out,
460 })
461 }
462 installSnapshotFileFromContent := func(content, out string) {
463 ctx.Build(pctx, android.BuildParams{
464 Rule: android.WriteFile,
465 Output: android.PathForOutput(ctx, snapshotDir, out),
466 Description: "vndk snapshot " + out,
467 Args: map[string]string{
468 "content": content,
469 },
470 })
471 *outputs = append(*outputs, android.RuleBuilderInstall{
472 From: android.PathForOutput(ctx, snapshotDir, out),
473 To: out,
474 })
475 }
476
Inseob Kim1f086e22019-05-09 13:29:15 +0900477 tryBuildNotice := func(m *Module) {
Inseob Kimae553032019-05-14 18:52:49 +0900478 name := ctx.ModuleName(m) + ".so.txt"
Inseob Kim1f086e22019-05-09 13:29:15 +0900479
480 if _, ok := noticeBuilt[name]; ok {
481 return
482 }
483
484 noticeBuilt[name] = true
485
486 if m.NoticeFile().Valid() {
Inseob Kimae553032019-05-14 18:52:49 +0900487 installSnapshotFileFromPath(m.NoticeFile().Path(), filepath.Join(noticeDir, name))
Inseob Kim1f086e22019-05-09 13:29:15 +0900488 }
489 }
490
Jooyung Han0302a842019-10-30 18:43:49 +0900491 vndkCoreLibraries := android.SortedStringKeys(vndkCoreLibraries(ctx.Config()))
492 vndkSpLibraries := android.SortedStringKeys(vndkSpLibraries(ctx.Config()))
493 vndkPrivateLibraries := android.SortedStringKeys(vndkPrivateLibraries(ctx.Config()))
Inseob Kim1f086e22019-05-09 13:29:15 +0900494
Inseob Kimae553032019-05-14 18:52:49 +0900495 var generatedHeaders android.Paths
496 includeDirs := make(map[string]bool)
497
498 type vndkSnapshotLibraryInterface interface {
499 exportedFlagsProducer
500 libraryInterface
501 }
502
503 var _ vndkSnapshotLibraryInterface = (*prebuiltLibraryLinker)(nil)
504 var _ vndkSnapshotLibraryInterface = (*libraryDecorator)(nil)
505
506 installVndkSnapshotLib := func(m *Module, l vndkSnapshotLibraryInterface, dir string) bool {
507 name := ctx.ModuleName(m)
508 libOut := filepath.Join(dir, name+".so")
509
510 installSnapshotFileFromPath(m.outputFile.Path(), libOut)
511 tryBuildNotice(m)
512
513 if ctx.Config().VndkSnapshotBuildArtifacts() {
514 prop := struct {
515 ExportedDirs []string `json:",omitempty"`
516 ExportedSystemDirs []string `json:",omitempty"`
517 ExportedFlags []string `json:",omitempty"`
518 RelativeInstallPath string `json:",omitempty"`
519 }{}
520 prop.ExportedFlags = l.exportedFlags()
Jiyong Park74955042019-10-22 20:19:51 +0900521 prop.ExportedDirs = l.exportedDirs().Strings()
522 prop.ExportedSystemDirs = l.exportedSystemDirs().Strings()
Inseob Kimae553032019-05-14 18:52:49 +0900523 prop.RelativeInstallPath = m.RelativeInstallPath()
524
525 propOut := libOut + ".json"
526
527 j, err := json.Marshal(prop)
528 if err != nil {
529 ctx.Errorf("json marshal to %q failed: %#v", propOut, err)
530 return false
531 }
532
533 installSnapshotFileFromContent(string(j), propOut)
534 }
535 return true
536 }
537
538 isVndkSnapshotLibrary := func(m *Module) (i vndkSnapshotLibraryInterface, libDir string, isVndkSnapshotLib bool) {
539 if m.Target().NativeBridge == android.NativeBridgeEnabled {
540 return nil, "", false
541 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700542 if !m.UseVndk() || !m.IsForPlatform() || !m.installable() {
Inseob Kimae553032019-05-14 18:52:49 +0900543 return nil, "", false
544 }
545 l, ok := m.linker.(vndkSnapshotLibraryInterface)
546 if !ok || !l.shared() {
547 return nil, "", false
548 }
549 name := ctx.ModuleName(m)
Jooyung Han0302a842019-10-30 18:43:49 +0900550 if inList(name, vndkCoreLibraries) {
Inseob Kimae553032019-05-14 18:52:49 +0900551 return l, filepath.Join("shared", "vndk-core"), true
Jooyung Han0302a842019-10-30 18:43:49 +0900552 } else if inList(name, vndkSpLibraries) {
Inseob Kimae553032019-05-14 18:52:49 +0900553 return l, filepath.Join("shared", "vndk-sp"), true
554 } else {
555 return nil, "", false
556 }
557 }
558
Inseob Kim1f086e22019-05-09 13:29:15 +0900559 ctx.VisitAllModules(func(module android.Module) {
560 m, ok := module.(*Module)
Inseob Kimae553032019-05-14 18:52:49 +0900561 if !ok || !m.Enabled() {
Inseob Kim1f086e22019-05-09 13:29:15 +0900562 return
563 }
564
Inseob Kimae553032019-05-14 18:52:49 +0900565 baseDir, ok := vndkLibDir[m.Target().Arch.ArchType]
566 if !ok {
dimitry51ea18a2019-05-20 10:39:52 +0200567 return
568 }
569
Inseob Kimae553032019-05-14 18:52:49 +0900570 l, libDir, ok := isVndkSnapshotLibrary(m)
571 if !ok {
Inseob Kim1f086e22019-05-09 13:29:15 +0900572 return
573 }
574
Inseob Kimae553032019-05-14 18:52:49 +0900575 if !installVndkSnapshotLib(m, l, filepath.Join(baseDir, libDir)) {
576 return
577 }
Inseob Kim1f086e22019-05-09 13:29:15 +0900578
Inseob Kimae553032019-05-14 18:52:49 +0900579 generatedHeaders = append(generatedHeaders, l.exportedDeps()...)
580 for _, dir := range append(l.exportedDirs(), l.exportedSystemDirs()...) {
Jiyong Park74955042019-10-22 20:19:51 +0900581 includeDirs[dir.String()] = true
Inseob Kimae553032019-05-14 18:52:49 +0900582 }
583 })
Inseob Kim1f086e22019-05-09 13:29:15 +0900584
Inseob Kimae553032019-05-14 18:52:49 +0900585 if ctx.Config().VndkSnapshotBuildArtifacts() {
586 headers := make(map[string]bool)
587
588 for _, dir := range android.SortedStringKeys(includeDirs) {
589 // workaround to determine if dir is under output directory
590 if strings.HasPrefix(dir, android.PathForOutput(ctx).String()) {
591 continue
Inseob Kim1f086e22019-05-09 13:29:15 +0900592 }
Inseob Kimae553032019-05-14 18:52:49 +0900593 exts := headerExts
594 // Glob all files under this special directory, because of C++ headers.
595 if strings.HasPrefix(dir, "external/libcxx/include") {
596 exts = []string{""}
Inseob Kim1f086e22019-05-09 13:29:15 +0900597 }
Inseob Kimae553032019-05-14 18:52:49 +0900598 for _, ext := range exts {
599 glob, err := ctx.GlobWithDeps(dir+"/**/*"+ext, nil)
600 if err != nil {
601 ctx.Errorf("%#v\n", err)
602 return
603 }
604 for _, header := range glob {
605 if strings.HasSuffix(header, "/") {
606 continue
607 }
608 headers[header] = true
609 }
610 }
Inseob Kim1f086e22019-05-09 13:29:15 +0900611 }
Inseob Kim1f086e22019-05-09 13:29:15 +0900612
Inseob Kimae553032019-05-14 18:52:49 +0900613 for _, header := range android.SortedStringKeys(headers) {
614 installSnapshotFileFromPath(android.PathForSource(ctx, header),
615 filepath.Join(includeDir, header))
616 }
Inseob Kim1f086e22019-05-09 13:29:15 +0900617
Inseob Kimae553032019-05-14 18:52:49 +0900618 isHeader := func(path string) bool {
619 for _, ext := range headerExts {
620 if strings.HasSuffix(path, ext) {
621 return true
622 }
623 }
624 return false
625 }
Inseob Kim1f086e22019-05-09 13:29:15 +0900626
Inseob Kimae553032019-05-14 18:52:49 +0900627 for _, path := range android.PathsToDirectorySortedPaths(android.FirstUniquePaths(generatedHeaders)) {
628 header := path.String()
629
630 if !isHeader(header) {
631 continue
632 }
633
634 installSnapshotFileFromPath(path, filepath.Join(includeDir, header))
635 }
636 }
637
Jooyung Han0302a842019-10-30 18:43:49 +0900638 installSnapshotFileFromContent(android.JoinWithSuffix(vndkCoreLibraries, ".so", "\\n"),
Inseob Kimae553032019-05-14 18:52:49 +0900639 filepath.Join(configsDir, "vndkcore.libraries.txt"))
Jooyung Han0302a842019-10-30 18:43:49 +0900640 installSnapshotFileFromContent(android.JoinWithSuffix(vndkPrivateLibraries, ".so", "\\n"),
Inseob Kimae553032019-05-14 18:52:49 +0900641 filepath.Join(configsDir, "vndkprivate.libraries.txt"))
Inseob Kim1f086e22019-05-09 13:29:15 +0900642
643 var modulePathTxtBuilder strings.Builder
644
Colin Cross4c2c46f2019-06-03 15:26:05 -0700645 modulePaths := modulePaths(ctx.Config())
Colin Cross4c2c46f2019-06-03 15:26:05 -0700646
Inseob Kim1f086e22019-05-09 13:29:15 +0900647 first := true
Inseob Kimae553032019-05-14 18:52:49 +0900648 for _, lib := range android.SortedStringKeys(modulePaths) {
Inseob Kim1f086e22019-05-09 13:29:15 +0900649 if first {
650 first = false
651 } else {
652 modulePathTxtBuilder.WriteString("\\n")
653 }
654 modulePathTxtBuilder.WriteString(lib)
655 modulePathTxtBuilder.WriteString(".so ")
Colin Cross4c2c46f2019-06-03 15:26:05 -0700656 modulePathTxtBuilder.WriteString(modulePaths[lib])
Inseob Kim1f086e22019-05-09 13:29:15 +0900657 }
658
Inseob Kimae553032019-05-14 18:52:49 +0900659 installSnapshotFileFromContent(modulePathTxtBuilder.String(),
660 filepath.Join(configsDir, "module_paths.txt"))
Inseob Kim1f086e22019-05-09 13:29:15 +0900661}
Jooyung Han097087b2019-10-22 19:32:18 +0900662
Jooyung Han0302a842019-10-30 18:43:49 +0900663func getVndkFileName(m *Module) (string, error) {
664 if library, ok := m.linker.(*libraryDecorator); ok {
665 return library.getLibNameHelper(m.BaseModuleName(), true) + ".so", nil
666 }
667 if prebuilt, ok := m.linker.(*prebuiltLibraryLinker); ok {
668 return prebuilt.libraryDecorator.getLibNameHelper(m.BaseModuleName(), true) + ".so", nil
669 }
670 return "", fmt.Errorf("VNDK library should have libraryDecorator or prebuiltLibraryLinker as linker: %T", m.linker)
Jooyung Han097087b2019-10-22 19:32:18 +0900671}
672
673func (c *vndkSnapshotSingleton) buildVndkLibrariesTxtFiles(ctx android.SingletonContext) {
Jooyung Han0302a842019-10-30 18:43:49 +0900674 // Make uses LLNDK_LIBRARIES to determine which libraries to install.
675 // HWASAN is only part of the LL-NDK in builds in which libc depends on HWASAN.
676 // Therefore, by removing the library here, we cause it to only be installed if libc
677 // depends on it.
678 installedLlndkLibraries := make(map[string]string)
679 for lib, filename := range llndkLibraries(ctx.Config()) {
680 if strings.HasPrefix(lib, "libclang_rt.hwasan-") {
681 continue
Jooyung Han097087b2019-10-22 19:32:18 +0900682 }
Jooyung Han0302a842019-10-30 18:43:49 +0900683 installedLlndkLibraries[lib] = filename
684 }
Jooyung Han097087b2019-10-22 19:32:18 +0900685
Jooyung Han0302a842019-10-30 18:43:49 +0900686 installListFile := func(list []string, fileName string) android.Path {
687 out := android.PathForOutput(ctx, "vndk", fileName)
688 ctx.Build(pctx, android.BuildParams{
689 Rule: android.WriteFile,
690 Output: out,
691 Description: "Writing " + out.String(),
692 Args: map[string]string{
693 "content": strings.Join(list, "\\n"),
694 },
695 })
696 return out
697 }
Jooyung Han097087b2019-10-22 19:32:18 +0900698
Jooyung Han0302a842019-10-30 18:43:49 +0900699 c.installedLlndkLibraries = android.SortedStringKeys(installedLlndkLibraries)
Jooyung Han097087b2019-10-22 19:32:18 +0900700
Jooyung Han0302a842019-10-30 18:43:49 +0900701 llndk := android.SortedStringMapValues(installedLlndkLibraries)
702 vndkcore := android.SortedStringMapValues(vndkCoreLibraries(ctx.Config()))
703 vndksp := android.SortedStringMapValues(vndkSpLibraries(ctx.Config()))
704 vndkprivate := android.SortedStringMapValues(vndkPrivateLibraries(ctx.Config()))
705 vndkcorevariant := android.SortedStringMapValues(vndkUsingCoreVariantLibraries(ctx.Config()))
706
707 c.llnkdLibrariesFile = installListFile(llndk, "llndk.libraries.txt")
708 c.vndkCoreLibrariesFile = installListFile(vndkcore, "vndkcore.libraries.txt")
709 c.vndkSpLibrariesFile = installListFile(vndksp, "vndksp.libraries.txt")
710 c.vndkPrivateLibrariesFile = installListFile(vndkprivate, "vndkprivate.libraries.txt")
711 c.vndkCoreVariantLibrariesFile = installListFile(vndkcorevariant, "vndkcorevariant.libraries.txt")
Jooyung Han097087b2019-10-22 19:32:18 +0900712
713 // merged & tagged & filtered-out(libclang_rt)
Jooyung Han0302a842019-10-30 18:43:49 +0900714 // Since each target have different set of libclang_rt.* files,
715 // keep the common set of files in vndk.libraries.txt
716 var merged []string
Jooyung Han097087b2019-10-22 19:32:18 +0900717 filterOutLibClangRt := func(libList []string) (filtered []string) {
718 for _, lib := range libList {
719 if !strings.HasPrefix(lib, "libclang_rt.") {
720 filtered = append(filtered, lib)
721 }
722 }
723 return
724 }
725 merged = append(merged, addPrefix(filterOutLibClangRt(llndk), "LLNDK: ")...)
726 merged = append(merged, addPrefix(vndksp, "VNDK-SP: ")...)
727 merged = append(merged, addPrefix(filterOutLibClangRt(vndkcore), "VNDK-core: ")...)
728 merged = append(merged, addPrefix(vndkprivate, "VNDK-private: ")...)
Jooyung Han0302a842019-10-30 18:43:49 +0900729 c.vndkLibrariesFile = installListFile(merged, "vndk.libraries.txt")
730}
Jooyung Han097087b2019-10-22 19:32:18 +0900731
Jooyung Han0302a842019-10-30 18:43:49 +0900732func (c *vndkSnapshotSingleton) MakeVars(ctx android.MakeVarsContext) {
733 // Make uses LLNDK_MOVED_TO_APEX_LIBRARIES to avoid installing libraries on /system if
734 // they been moved to an apex.
735 movedToApexLlndkLibraries := []string{}
736 for _, lib := range c.installedLlndkLibraries {
737 // Skip bionic libs, they are handled in different manner
738 if android.DirectlyInAnyApex(&notOnHostContext{}, lib) && !isBionic(lib) {
739 movedToApexLlndkLibraries = append(movedToApexLlndkLibraries, lib)
740 }
741 }
742 ctx.Strict("LLNDK_MOVED_TO_APEX_LIBRARIES", strings.Join(movedToApexLlndkLibraries, " "))
743 ctx.Strict("LLNDK_LIBRARIES", strings.Join(c.installedLlndkLibraries, " "))
744 ctx.Strict("VNDK_CORE_LIBRARIES", strings.Join(android.SortedStringKeys(vndkCoreLibraries(ctx.Config())), " "))
745 ctx.Strict("VNDK_SAMEPROCESS_LIBRARIES", strings.Join(android.SortedStringKeys(vndkSpLibraries(ctx.Config())), " "))
746 ctx.Strict("VNDK_PRIVATE_LIBRARIES", strings.Join(android.SortedStringKeys(vndkPrivateLibraries(ctx.Config())), " "))
747 ctx.Strict("VNDK_USING_CORE_VARIANT_LIBRARIES", strings.Join(android.SortedStringKeys(vndkUsingCoreVariantLibraries(ctx.Config())), " "))
748
749 ctx.Strict("LLNDK_LIBRARIES_FILE", c.llnkdLibrariesFile.String())
750 ctx.Strict("VNDKCORE_LIBRARIES_FILE", c.vndkCoreLibrariesFile.String())
751 ctx.Strict("VNDKSP_LIBRARIES_FILE", c.vndkSpLibrariesFile.String())
752 ctx.Strict("VNDKPRIVATE_LIBRARIES_FILE", c.vndkPrivateLibrariesFile.String())
753 ctx.Strict("VNDKCOREVARIANT_LIBRARIES_FILE", c.vndkCoreVariantLibrariesFile.String())
754
755 ctx.Strict("VNDK_LIBRARIES_FILE", c.vndkLibrariesFile.String())
Jooyung Han097087b2019-10-22 19:32:18 +0900756}