Justin Yun | 8effde4 | 2017-06-23 19:24:43 +0900 | [diff] [blame] | 1 | // 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 | |
| 15 | package cc |
| 16 | |
| 17 | import ( |
Martin Stjernholm | 257eb0c | 2018-10-15 13:05:27 +0100 | [diff] [blame] | 18 | "errors" |
Colin Cross | 766efbc | 2017-08-17 14:55:15 -0700 | [diff] [blame] | 19 | "sort" |
Jiyong Park | d5b18a5 | 2017-08-03 21:22:50 +0900 | [diff] [blame] | 20 | "strings" |
| 21 | "sync" |
| 22 | |
Justin Yun | 8effde4 | 2017-06-23 19:24:43 +0900 | [diff] [blame] | 23 | "android/soong/android" |
| 24 | ) |
| 25 | |
| 26 | type 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 Park | 82e2bf3 | 2017-08-16 14:05:54 +0900 | [diff] [blame] | 31 | // `vendor_vailable` must be explicitly set to either true or |
| 32 | // false together with `vndk: {enabled: true}`. |
Justin Yun | 8effde4 | 2017-06-23 19:24:43 +0900 | [diff] [blame] | 33 | 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 Chien | f351174 | 2017-10-31 18:04:35 +0800 | [diff] [blame] | 46 | |
| 47 | // Extending another module |
| 48 | Extends *string |
Justin Yun | 8effde4 | 2017-06-23 19:24:43 +0900 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
| 52 | type vndkdep struct { |
| 53 | Properties VndkProperties |
| 54 | } |
| 55 | |
| 56 | func (vndk *vndkdep) props() []interface{} { |
| 57 | return []interface{}{&vndk.Properties} |
| 58 | } |
| 59 | |
| 60 | func (vndk *vndkdep) begin(ctx BaseModuleContext) {} |
| 61 | |
| 62 | func (vndk *vndkdep) deps(ctx BaseModuleContext, deps Deps) Deps { |
| 63 | return deps |
| 64 | } |
| 65 | |
| 66 | func (vndk *vndkdep) isVndk() bool { |
| 67 | return Bool(vndk.Properties.Vndk.Enabled) |
| 68 | } |
| 69 | |
| 70 | func (vndk *vndkdep) isVndkSp() bool { |
| 71 | return Bool(vndk.Properties.Vndk.Support_system_process) |
| 72 | } |
| 73 | |
Logan Chien | f351174 | 2017-10-31 18:04:35 +0800 | [diff] [blame] | 74 | func (vndk *vndkdep) isVndkExt() bool { |
| 75 | return vndk.Properties.Vndk.Extends != nil |
| 76 | } |
| 77 | |
| 78 | func (vndk *vndkdep) getVndkExtendsModuleName() string { |
| 79 | return String(vndk.Properties.Vndk.Extends) |
| 80 | } |
| 81 | |
Justin Yun | 8effde4 | 2017-06-23 19:24:43 +0900 | [diff] [blame] | 82 | func (vndk *vndkdep) typeName() string { |
| 83 | if !vndk.isVndk() { |
| 84 | return "native:vendor" |
| 85 | } |
Logan Chien | f351174 | 2017-10-31 18:04:35 +0800 | [diff] [blame] | 86 | if !vndk.isVndkExt() { |
| 87 | if !vndk.isVndkSp() { |
| 88 | return "native:vendor:vndk" |
| 89 | } |
| 90 | return "native:vendor:vndksp" |
Justin Yun | 8effde4 | 2017-06-23 19:24:43 +0900 | [diff] [blame] | 91 | } |
Logan Chien | f351174 | 2017-10-31 18:04:35 +0800 | [diff] [blame] | 92 | if !vndk.isVndkSp() { |
| 93 | return "native:vendor:vndkext" |
| 94 | } |
| 95 | return "native:vendor:vndkspext" |
Justin Yun | 8effde4 | 2017-06-23 19:24:43 +0900 | [diff] [blame] | 96 | } |
| 97 | |
Logan Chien | f351174 | 2017-10-31 18:04:35 +0800 | [diff] [blame] | 98 | func (vndk *vndkdep) vndkCheckLinkType(ctx android.ModuleContext, to *Module, tag dependencyTag) { |
Justin Yun | 8effde4 | 2017-06-23 19:24:43 +0900 | [diff] [blame] | 99 | if to.linker == nil { |
| 100 | return |
| 101 | } |
Jiyong Park | 82e2bf3 | 2017-08-16 14:05:54 +0900 | [diff] [blame] | 102 | 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 Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 106 | if lib, ok := to.linker.(*llndkStubDecorator); ok && !Bool(lib.Properties.Vendor_available) { |
Jiyong Park | 82e2bf3 | 2017-08-16 14:05:54 +0900 | [diff] [blame] | 107 | 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 Yun | 8effde4 | 2017-06-23 19:24:43 +0900 | [diff] [blame] | 120 | 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 Chien | f351174 | 2017-10-31 18:04:35 +0800 | [diff] [blame] | 130 | 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 Yun | 8effde4 | 2017-06-23 19:24:43 +0900 | [diff] [blame] | 150 | if to.vndkdep == nil { |
| 151 | return |
| 152 | } |
Logan Chien | f351174 | 2017-10-31 18:04:35 +0800 | [diff] [blame] | 153 | |
Logan Chien | d3c59a2 | 2018-03-29 14:08:15 +0800 | [diff] [blame] | 154 | // Check the dependencies of VNDK shared libraries. |
Martin Stjernholm | 257eb0c | 2018-10-15 13:05:27 +0100 | [diff] [blame] | 155 | 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 Chien | f351174 | 2017-10-31 18:04:35 +0800 | [diff] [blame] | 158 | return |
| 159 | } |
Logan Chien | d3c59a2 | 2018-03-29 14:08:15 +0800 | [diff] [blame] | 160 | } |
Logan Chien | f351174 | 2017-10-31 18:04:35 +0800 | [diff] [blame] | 161 | |
Martin Stjernholm | 257eb0c | 2018-10-15 13:05:27 +0100 | [diff] [blame] | 162 | func vndkIsVndkDepAllowed(from *vndkdep, to *vndkdep) error { |
Logan Chien | d3c59a2 | 2018-03-29 14:08:15 +0800 | [diff] [blame] | 163 | // Check the dependencies of VNDK, VNDK-Ext, VNDK-SP, VNDK-SP-Ext and vendor modules. |
| 164 | if from.isVndkExt() { |
| 165 | if from.isVndkSp() { |
Martin Stjernholm | 257eb0c | 2018-10-15 13:05:27 +0100 | [diff] [blame] | 166 | 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 Chien | d3c59a2 | 2018-03-29 14:08:15 +0800 | [diff] [blame] | 170 | } |
| 171 | // VNDK-Ext may depend on VNDK, VNDK-Ext, VNDK-SP, VNDK-SP-Ext, or vendor libs. |
Martin Stjernholm | 257eb0c | 2018-10-15 13:05:27 +0100 | [diff] [blame] | 172 | return nil |
Justin Yun | 8effde4 | 2017-06-23 19:24:43 +0900 | [diff] [blame] | 173 | } |
Logan Chien | d3c59a2 | 2018-03-29 14:08:15 +0800 | [diff] [blame] | 174 | if from.isVndk() { |
| 175 | if to.isVndkExt() { |
Martin Stjernholm | 257eb0c | 2018-10-15 13:05:27 +0100 | [diff] [blame] | 176 | return errors.New("VNDK-core and VNDK-SP must not depend on VNDK extensions") |
Logan Chien | d3c59a2 | 2018-03-29 14:08:15 +0800 | [diff] [blame] | 177 | } |
| 178 | if from.isVndkSp() { |
Martin Stjernholm | 257eb0c | 2018-10-15 13:05:27 +0100 | [diff] [blame] | 179 | if !to.isVndkSp() { |
| 180 | return errors.New("VNDK-SP must only depend on VNDK-SP") |
| 181 | } |
| 182 | return nil |
Logan Chien | d3c59a2 | 2018-03-29 14:08:15 +0800 | [diff] [blame] | 183 | } |
Martin Stjernholm | 257eb0c | 2018-10-15 13:05:27 +0100 | [diff] [blame] | 184 | if !to.isVndk() { |
| 185 | return errors.New("VNDK-core must only depend on VNDK-core or VNDK-SP") |
| 186 | } |
| 187 | return nil |
Logan Chien | d3c59a2 | 2018-03-29 14:08:15 +0800 | [diff] [blame] | 188 | } |
| 189 | // Vendor modules may depend on VNDK, VNDK-Ext, VNDK-SP, VNDK-SP-Ext, or vendor libs. |
Martin Stjernholm | 257eb0c | 2018-10-15 13:05:27 +0100 | [diff] [blame] | 190 | return nil |
Justin Yun | 8effde4 | 2017-06-23 19:24:43 +0900 | [diff] [blame] | 191 | } |
Jiyong Park | d5b18a5 | 2017-08-03 21:22:50 +0900 | [diff] [blame] | 192 | |
| 193 | var ( |
Jiyong Park | 82e2bf3 | 2017-08-16 14:05:54 +0900 | [diff] [blame] | 194 | vndkCoreLibraries []string |
| 195 | vndkSpLibraries []string |
| 196 | llndkLibraries []string |
| 197 | vndkPrivateLibraries []string |
| 198 | vndkLibrariesLock sync.Mutex |
Jiyong Park | d5b18a5 | 2017-08-03 21:22:50 +0900 | [diff] [blame] | 199 | ) |
| 200 | |
| 201 | // gather list of vndk-core, vndk-sp, and ll-ndk libs |
Jiyong Park | da6eb59 | 2018-12-19 17:12:36 +0900 | [diff] [blame] | 202 | func VndkMutator(mctx android.BottomUpMutatorContext) { |
Jiyong Park | 4b03222 | 2018-02-16 22:14:07 +0900 | [diff] [blame] | 203 | if m, ok := mctx.Module().(*Module); ok && m.Enabled() { |
Jiyong Park | 82e2bf3 | 2017-08-16 14:05:54 +0900 | [diff] [blame] | 204 | if lib, ok := m.linker.(*llndkStubDecorator); ok { |
Jiyong Park | d5b18a5 | 2017-08-03 21:22:50 +0900 | [diff] [blame] | 205 | vndkLibrariesLock.Lock() |
| 206 | defer vndkLibrariesLock.Unlock() |
| 207 | name := strings.TrimSuffix(m.Name(), llndkLibrarySuffix) |
| 208 | if !inList(name, llndkLibraries) { |
| 209 | llndkLibraries = append(llndkLibraries, name) |
Colin Cross | 766efbc | 2017-08-17 14:55:15 -0700 | [diff] [blame] | 210 | sort.Strings(llndkLibraries) |
Jiyong Park | d5b18a5 | 2017-08-03 21:22:50 +0900 | [diff] [blame] | 211 | } |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 212 | if !Bool(lib.Properties.Vendor_available) { |
Jiyong Park | 82e2bf3 | 2017-08-16 14:05:54 +0900 | [diff] [blame] | 213 | 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 Chien | f351174 | 2017-10-31 18:04:35 +0800 | [diff] [blame] | 223 | if m.vndkdep.isVndk() && !m.vndkdep.isVndkExt() { |
Jiyong Park | 82e2bf3 | 2017-08-16 14:05:54 +0900 | [diff] [blame] | 224 | 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 Park | d5b18a5 | 2017-08-03 21:22:50 +0900 | [diff] [blame] | 236 | } |
Jiyong Park | 82e2bf3 | 2017-08-16 14:05:54 +0900 | [diff] [blame] | 237 | if !Bool(m.VendorProperties.Vendor_available) { |
| 238 | if !inList(name, vndkPrivateLibraries) { |
| 239 | vndkPrivateLibraries = append(vndkPrivateLibraries, name) |
| 240 | sort.Strings(vndkPrivateLibraries) |
| 241 | } |
Jiyong Park | d5b18a5 | 2017-08-03 21:22:50 +0900 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | } |