blob: 623097dad40c470b2ba2b5c6f960a533a8f6ca28 [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 (
Martin Stjernholm257eb0c2018-10-15 13:05:27 +010018 "errors"
Colin Cross766efbc2017-08-17 14:55:15 -070019 "sort"
Jiyong Parkd5b18a52017-08-03 21:22:50 +090020 "strings"
21 "sync"
22
Justin Yun8effde42017-06-23 19:24:43 +090023 "android/soong/android"
24)
25
26type VndkProperties struct {
27 Vndk struct {
28 // declared as a VNDK or VNDK-SP module. The vendor variant
29 // will be installed in /system instead of /vendor partition.
30 //
Jiyong Park82e2bf32017-08-16 14:05:54 +090031 // `vendor_vailable` must be explicitly set to either true or
32 // false together with `vndk: {enabled: true}`.
Justin Yun8effde42017-06-23 19:24:43 +090033 Enabled *bool
34
35 // declared as a VNDK-SP module, which is a subset of VNDK.
36 //
37 // `vndk: { enabled: true }` must set together.
38 //
39 // All these modules are allowed to link to VNDK-SP or LL-NDK
40 // modules only. Other dependency will cause link-type errors.
41 //
42 // If `support_system_process` is not set or set to false,
43 // the module is VNDK-core and can link to other VNDK-core,
44 // VNDK-SP or LL-NDK modules only.
45 Support_system_process *bool
Logan Chienf3511742017-10-31 18:04:35 +080046
47 // Extending another module
48 Extends *string
Justin Yun8effde42017-06-23 19:24:43 +090049 }
50}
51
52type vndkdep struct {
53 Properties VndkProperties
54}
55
56func (vndk *vndkdep) props() []interface{} {
57 return []interface{}{&vndk.Properties}
58}
59
60func (vndk *vndkdep) begin(ctx BaseModuleContext) {}
61
62func (vndk *vndkdep) deps(ctx BaseModuleContext, deps Deps) Deps {
63 return deps
64}
65
66func (vndk *vndkdep) isVndk() bool {
67 return Bool(vndk.Properties.Vndk.Enabled)
68}
69
70func (vndk *vndkdep) isVndkSp() bool {
71 return Bool(vndk.Properties.Vndk.Support_system_process)
72}
73
Logan Chienf3511742017-10-31 18:04:35 +080074func (vndk *vndkdep) isVndkExt() bool {
75 return vndk.Properties.Vndk.Extends != nil
76}
77
78func (vndk *vndkdep) getVndkExtendsModuleName() string {
79 return String(vndk.Properties.Vndk.Extends)
80}
81
Justin Yun8effde42017-06-23 19:24:43 +090082func (vndk *vndkdep) typeName() string {
83 if !vndk.isVndk() {
84 return "native:vendor"
85 }
Logan Chienf3511742017-10-31 18:04:35 +080086 if !vndk.isVndkExt() {
87 if !vndk.isVndkSp() {
88 return "native:vendor:vndk"
89 }
90 return "native:vendor:vndksp"
Justin Yun8effde42017-06-23 19:24:43 +090091 }
Logan Chienf3511742017-10-31 18:04:35 +080092 if !vndk.isVndkSp() {
93 return "native:vendor:vndkext"
94 }
95 return "native:vendor:vndkspext"
Justin Yun8effde42017-06-23 19:24:43 +090096}
97
Logan Chienf3511742017-10-31 18:04:35 +080098func (vndk *vndkdep) vndkCheckLinkType(ctx android.ModuleContext, to *Module, tag dependencyTag) {
Justin Yun8effde42017-06-23 19:24:43 +090099 if to.linker == nil {
100 return
101 }
Jiyong Park82e2bf32017-08-16 14:05:54 +0900102 if !vndk.isVndk() {
103 // Non-VNDK modules (those installed to /vendor) can't depend on modules marked with
104 // vendor_available: false.
105 violation := false
Nan Zhang0007d812017-11-07 10:57:05 -0800106 if lib, ok := to.linker.(*llndkStubDecorator); ok && !Bool(lib.Properties.Vendor_available) {
Jiyong Park82e2bf32017-08-16 14:05:54 +0900107 violation = true
108 } else {
109 if _, ok := to.linker.(libraryInterface); ok && to.VendorProperties.Vendor_available != nil && !Bool(to.VendorProperties.Vendor_available) {
110 // Vendor_available == nil && !Bool(Vendor_available) should be okay since
111 // it means a vendor-only library which is a valid dependency for non-VNDK
112 // modules.
113 violation = true
114 }
115 }
116 if violation {
117 ctx.ModuleErrorf("Vendor module that is not VNDK should not link to %q which is marked as `vendor_available: false`", to.Name())
118 }
119 }
Justin Yun8effde42017-06-23 19:24:43 +0900120 if lib, ok := to.linker.(*libraryDecorator); !ok || !lib.shared() {
121 // Check only shared libraries.
122 // Other (static and LL-NDK) libraries are allowed to link.
123 return
124 }
125 if !to.Properties.UseVndk {
126 ctx.ModuleErrorf("(%s) should not link to %q which is not a vendor-available library",
127 vndk.typeName(), to.Name())
128 return
129 }
Logan Chienf3511742017-10-31 18:04:35 +0800130 if tag == vndkExtDepTag {
131 // Ensure `extends: "name"` property refers a vndk module that has vendor_available
132 // and has identical vndk properties.
133 if to.vndkdep == nil || !to.vndkdep.isVndk() {
134 ctx.ModuleErrorf("`extends` refers a non-vndk module %q", to.Name())
135 return
136 }
137 if vndk.isVndkSp() != to.vndkdep.isVndkSp() {
138 ctx.ModuleErrorf(
139 "`extends` refers a module %q with mismatched support_system_process",
140 to.Name())
141 return
142 }
143 if !Bool(to.VendorProperties.Vendor_available) {
144 ctx.ModuleErrorf(
145 "`extends` refers module %q which does not have `vendor_available: true`",
146 to.Name())
147 return
148 }
149 }
Justin Yun8effde42017-06-23 19:24:43 +0900150 if to.vndkdep == nil {
151 return
152 }
Logan Chienf3511742017-10-31 18:04:35 +0800153
Logan Chiend3c59a22018-03-29 14:08:15 +0800154 // Check the dependencies of VNDK shared libraries.
Martin Stjernholm257eb0c2018-10-15 13:05:27 +0100155 if err := vndkIsVndkDepAllowed(vndk, to.vndkdep); err != nil {
156 ctx.ModuleErrorf("(%s) should not link to %q (%s): %v",
157 vndk.typeName(), to.Name(), to.vndkdep.typeName(), err)
Logan Chienf3511742017-10-31 18:04:35 +0800158 return
159 }
Logan Chiend3c59a22018-03-29 14:08:15 +0800160}
Logan Chienf3511742017-10-31 18:04:35 +0800161
Martin Stjernholm257eb0c2018-10-15 13:05:27 +0100162func vndkIsVndkDepAllowed(from *vndkdep, to *vndkdep) error {
Logan Chiend3c59a22018-03-29 14:08:15 +0800163 // Check the dependencies of VNDK, VNDK-Ext, VNDK-SP, VNDK-SP-Ext and vendor modules.
164 if from.isVndkExt() {
165 if from.isVndkSp() {
Martin Stjernholm257eb0c2018-10-15 13:05:27 +0100166 if to.isVndk() && !to.isVndkSp() {
167 return errors.New("VNDK-SP extensions must not depend on VNDK or VNDK extensions")
168 }
169 return nil
Logan Chiend3c59a22018-03-29 14:08:15 +0800170 }
171 // VNDK-Ext may depend on VNDK, VNDK-Ext, VNDK-SP, VNDK-SP-Ext, or vendor libs.
Martin Stjernholm257eb0c2018-10-15 13:05:27 +0100172 return nil
Justin Yun8effde42017-06-23 19:24:43 +0900173 }
Logan Chiend3c59a22018-03-29 14:08:15 +0800174 if from.isVndk() {
175 if to.isVndkExt() {
Martin Stjernholm257eb0c2018-10-15 13:05:27 +0100176 return errors.New("VNDK-core and VNDK-SP must not depend on VNDK extensions")
Logan Chiend3c59a22018-03-29 14:08:15 +0800177 }
178 if from.isVndkSp() {
Martin Stjernholm257eb0c2018-10-15 13:05:27 +0100179 if !to.isVndkSp() {
180 return errors.New("VNDK-SP must only depend on VNDK-SP")
181 }
182 return nil
Logan Chiend3c59a22018-03-29 14:08:15 +0800183 }
Martin Stjernholm257eb0c2018-10-15 13:05:27 +0100184 if !to.isVndk() {
185 return errors.New("VNDK-core must only depend on VNDK-core or VNDK-SP")
186 }
187 return nil
Logan Chiend3c59a22018-03-29 14:08:15 +0800188 }
189 // Vendor modules may depend on VNDK, VNDK-Ext, VNDK-SP, VNDK-SP-Ext, or vendor libs.
Martin Stjernholm257eb0c2018-10-15 13:05:27 +0100190 return nil
Justin Yun8effde42017-06-23 19:24:43 +0900191}
Jiyong Parkd5b18a52017-08-03 21:22:50 +0900192
193var (
Jiyong Park82e2bf32017-08-16 14:05:54 +0900194 vndkCoreLibraries []string
195 vndkSpLibraries []string
196 llndkLibraries []string
197 vndkPrivateLibraries []string
198 vndkLibrariesLock sync.Mutex
Jiyong Parkd5b18a52017-08-03 21:22:50 +0900199)
200
201// gather list of vndk-core, vndk-sp, and ll-ndk libs
Jiyong Parkda6eb592018-12-19 17:12:36 +0900202func VndkMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park4b032222018-02-16 22:14:07 +0900203 if m, ok := mctx.Module().(*Module); ok && m.Enabled() {
Jiyong Park82e2bf32017-08-16 14:05:54 +0900204 if lib, ok := m.linker.(*llndkStubDecorator); ok {
Jiyong Parkd5b18a52017-08-03 21:22:50 +0900205 vndkLibrariesLock.Lock()
206 defer vndkLibrariesLock.Unlock()
207 name := strings.TrimSuffix(m.Name(), llndkLibrarySuffix)
208 if !inList(name, llndkLibraries) {
209 llndkLibraries = append(llndkLibraries, name)
Colin Cross766efbc2017-08-17 14:55:15 -0700210 sort.Strings(llndkLibraries)
Jiyong Parkd5b18a52017-08-03 21:22:50 +0900211 }
Nan Zhang0007d812017-11-07 10:57:05 -0800212 if !Bool(lib.Properties.Vendor_available) {
Jiyong Park82e2bf32017-08-16 14:05:54 +0900213 if !inList(name, vndkPrivateLibraries) {
214 vndkPrivateLibraries = append(vndkPrivateLibraries, name)
215 sort.Strings(vndkPrivateLibraries)
216 }
217 }
218 } else {
219 lib, is_lib := m.linker.(*libraryDecorator)
220 prebuilt_lib, is_prebuilt_lib := m.linker.(*prebuiltLibraryLinker)
221 if (is_lib && lib.shared()) || (is_prebuilt_lib && prebuilt_lib.shared()) {
222 name := strings.TrimPrefix(m.Name(), "prebuilt_")
Logan Chienf3511742017-10-31 18:04:35 +0800223 if m.vndkdep.isVndk() && !m.vndkdep.isVndkExt() {
Jiyong Park82e2bf32017-08-16 14:05:54 +0900224 vndkLibrariesLock.Lock()
225 defer vndkLibrariesLock.Unlock()
226 if m.vndkdep.isVndkSp() {
227 if !inList(name, vndkSpLibraries) {
228 vndkSpLibraries = append(vndkSpLibraries, name)
229 sort.Strings(vndkSpLibraries)
230 }
231 } else {
232 if !inList(name, vndkCoreLibraries) {
233 vndkCoreLibraries = append(vndkCoreLibraries, name)
234 sort.Strings(vndkCoreLibraries)
235 }
Jiyong Parkd5b18a52017-08-03 21:22:50 +0900236 }
Jiyong Park82e2bf32017-08-16 14:05:54 +0900237 if !Bool(m.VendorProperties.Vendor_available) {
238 if !inList(name, vndkPrivateLibraries) {
239 vndkPrivateLibraries = append(vndkPrivateLibraries, name)
240 sort.Strings(vndkPrivateLibraries)
241 }
Jiyong Parkd5b18a52017-08-03 21:22:50 +0900242 }
243 }
244 }
245 }
246 }
247}