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 ( |
Inseob Kim | ae55303 | 2019-05-14 18:52:49 +0900 | [diff] [blame] | 18 | "encoding/json" |
Martin Stjernholm | 257eb0c | 2018-10-15 13:05:27 +0100 | [diff] [blame] | 19 | "errors" |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 20 | "path/filepath" |
Colin Cross | 766efbc | 2017-08-17 14:55:15 -0700 | [diff] [blame] | 21 | "sort" |
Jiyong Park | d5b18a5 | 2017-08-03 21:22:50 +0900 | [diff] [blame] | 22 | "strings" |
| 23 | "sync" |
| 24 | |
Justin Yun | 8effde4 | 2017-06-23 19:24:43 +0900 | [diff] [blame] | 25 | "android/soong/android" |
Vic Yang | efd249e | 2018-11-12 20:19:56 -0800 | [diff] [blame] | 26 | "android/soong/cc/config" |
Justin Yun | 8effde4 | 2017-06-23 19:24:43 +0900 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | type 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 Levillain | dfe75b3 | 2019-07-23 16:53:32 +0100 | [diff] [blame] | 34 | // `vendor_available` must be explicitly set to either true or |
Jiyong Park | 82e2bf3 | 2017-08-16 14:05:54 +0900 | [diff] [blame] | 35 | // false together with `vndk: {enabled: true}`. |
Justin Yun | 8effde4 | 2017-06-23 19:24:43 +0900 | [diff] [blame] | 36 | 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 Chien | f351174 | 2017-10-31 18:04:35 +0800 | [diff] [blame] | 49 | |
| 50 | // Extending another module |
| 51 | Extends *string |
Justin Yun | 8effde4 | 2017-06-23 19:24:43 +0900 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
| 55 | type vndkdep struct { |
| 56 | Properties VndkProperties |
| 57 | } |
| 58 | |
| 59 | func (vndk *vndkdep) props() []interface{} { |
| 60 | return []interface{}{&vndk.Properties} |
| 61 | } |
| 62 | |
| 63 | func (vndk *vndkdep) begin(ctx BaseModuleContext) {} |
| 64 | |
| 65 | func (vndk *vndkdep) deps(ctx BaseModuleContext, deps Deps) Deps { |
| 66 | return deps |
| 67 | } |
| 68 | |
| 69 | func (vndk *vndkdep) isVndk() bool { |
| 70 | return Bool(vndk.Properties.Vndk.Enabled) |
| 71 | } |
| 72 | |
| 73 | func (vndk *vndkdep) isVndkSp() bool { |
| 74 | return Bool(vndk.Properties.Vndk.Support_system_process) |
| 75 | } |
| 76 | |
Logan Chien | f351174 | 2017-10-31 18:04:35 +0800 | [diff] [blame] | 77 | func (vndk *vndkdep) isVndkExt() bool { |
| 78 | return vndk.Properties.Vndk.Extends != nil |
| 79 | } |
| 80 | |
| 81 | func (vndk *vndkdep) getVndkExtendsModuleName() string { |
| 82 | return String(vndk.Properties.Vndk.Extends) |
| 83 | } |
| 84 | |
Justin Yun | 8effde4 | 2017-06-23 19:24:43 +0900 | [diff] [blame] | 85 | func (vndk *vndkdep) typeName() string { |
| 86 | if !vndk.isVndk() { |
| 87 | return "native:vendor" |
| 88 | } |
Logan Chien | f351174 | 2017-10-31 18:04:35 +0800 | [diff] [blame] | 89 | if !vndk.isVndkExt() { |
| 90 | if !vndk.isVndkSp() { |
| 91 | return "native:vendor:vndk" |
| 92 | } |
| 93 | return "native:vendor:vndksp" |
Justin Yun | 8effde4 | 2017-06-23 19:24:43 +0900 | [diff] [blame] | 94 | } |
Logan Chien | f351174 | 2017-10-31 18:04:35 +0800 | [diff] [blame] | 95 | if !vndk.isVndkSp() { |
| 96 | return "native:vendor:vndkext" |
| 97 | } |
| 98 | return "native:vendor:vndkspext" |
Justin Yun | 8effde4 | 2017-06-23 19:24:43 +0900 | [diff] [blame] | 99 | } |
| 100 | |
Logan Chien | f351174 | 2017-10-31 18:04:35 +0800 | [diff] [blame] | 101 | func (vndk *vndkdep) vndkCheckLinkType(ctx android.ModuleContext, to *Module, tag dependencyTag) { |
Justin Yun | 8effde4 | 2017-06-23 19:24:43 +0900 | [diff] [blame] | 102 | if to.linker == nil { |
| 103 | return |
| 104 | } |
Jiyong Park | 82e2bf3 | 2017-08-16 14:05:54 +0900 | [diff] [blame] | 105 | 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 Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 109 | if lib, ok := to.linker.(*llndkStubDecorator); ok && !Bool(lib.Properties.Vendor_available) { |
Jiyong Park | 82e2bf3 | 2017-08-16 14:05:54 +0900 | [diff] [blame] | 110 | 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 Yun | 8effde4 | 2017-06-23 19:24:43 +0900 | [diff] [blame] | 123 | 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 | } |
Inseob Kim | 64c4395 | 2019-08-26 16:52:35 +0900 | [diff] [blame] | 128 | if !to.useVndk() { |
Justin Yun | 8effde4 | 2017-06-23 19:24:43 +0900 | [diff] [blame] | 129 | ctx.ModuleErrorf("(%s) should not link to %q which is not a vendor-available library", |
| 130 | vndk.typeName(), to.Name()) |
| 131 | return |
| 132 | } |
Logan Chien | f351174 | 2017-10-31 18:04:35 +0800 | [diff] [blame] | 133 | 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 Yun | 8effde4 | 2017-06-23 19:24:43 +0900 | [diff] [blame] | 153 | if to.vndkdep == nil { |
| 154 | return |
| 155 | } |
Logan Chien | f351174 | 2017-10-31 18:04:35 +0800 | [diff] [blame] | 156 | |
Logan Chien | d3c59a2 | 2018-03-29 14:08:15 +0800 | [diff] [blame] | 157 | // Check the dependencies of VNDK shared libraries. |
Martin Stjernholm | 257eb0c | 2018-10-15 13:05:27 +0100 | [diff] [blame] | 158 | 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 Chien | f351174 | 2017-10-31 18:04:35 +0800 | [diff] [blame] | 161 | return |
| 162 | } |
Logan Chien | d3c59a2 | 2018-03-29 14:08:15 +0800 | [diff] [blame] | 163 | } |
Logan Chien | f351174 | 2017-10-31 18:04:35 +0800 | [diff] [blame] | 164 | |
Martin Stjernholm | 257eb0c | 2018-10-15 13:05:27 +0100 | [diff] [blame] | 165 | func vndkIsVndkDepAllowed(from *vndkdep, to *vndkdep) error { |
Logan Chien | d3c59a2 | 2018-03-29 14:08:15 +0800 | [diff] [blame] | 166 | // Check the dependencies of VNDK, VNDK-Ext, VNDK-SP, VNDK-SP-Ext and vendor modules. |
| 167 | if from.isVndkExt() { |
| 168 | if from.isVndkSp() { |
Martin Stjernholm | 257eb0c | 2018-10-15 13:05:27 +0100 | [diff] [blame] | 169 | 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 Chien | d3c59a2 | 2018-03-29 14:08:15 +0800 | [diff] [blame] | 173 | } |
| 174 | // 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] | 175 | return nil |
Justin Yun | 8effde4 | 2017-06-23 19:24:43 +0900 | [diff] [blame] | 176 | } |
Logan Chien | d3c59a2 | 2018-03-29 14:08:15 +0800 | [diff] [blame] | 177 | if from.isVndk() { |
| 178 | if to.isVndkExt() { |
Martin Stjernholm | 257eb0c | 2018-10-15 13:05:27 +0100 | [diff] [blame] | 179 | 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] | 180 | } |
| 181 | if from.isVndkSp() { |
Martin Stjernholm | 257eb0c | 2018-10-15 13:05:27 +0100 | [diff] [blame] | 182 | if !to.isVndkSp() { |
| 183 | return errors.New("VNDK-SP must only depend on VNDK-SP") |
| 184 | } |
| 185 | return nil |
Logan Chien | d3c59a2 | 2018-03-29 14:08:15 +0800 | [diff] [blame] | 186 | } |
Martin Stjernholm | 257eb0c | 2018-10-15 13:05:27 +0100 | [diff] [blame] | 187 | if !to.isVndk() { |
| 188 | return errors.New("VNDK-core must only depend on VNDK-core or VNDK-SP") |
| 189 | } |
| 190 | return nil |
Logan Chien | d3c59a2 | 2018-03-29 14:08:15 +0800 | [diff] [blame] | 191 | } |
| 192 | // 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] | 193 | return nil |
Justin Yun | 8effde4 | 2017-06-23 19:24:43 +0900 | [diff] [blame] | 194 | } |
Jiyong Park | d5b18a5 | 2017-08-03 21:22:50 +0900 | [diff] [blame] | 195 | |
| 196 | var ( |
Jooyung Han | 097087b | 2019-10-22 19:32:18 +0900 | [diff] [blame^] | 197 | 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 Park | d5b18a5 | 2017-08-03 21:22:50 +0900 | [diff] [blame] | 207 | |
Inseob Kim | ae55303 | 2019-05-14 18:52:49 +0900 | [diff] [blame] | 208 | headerExts = []string{".h", ".hh", ".hpp", ".hxx", ".h++", ".inl", ".inc", ".ipp", ".h.generic"} |
| 209 | ) |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 210 | |
Inseob Kim | 9516ee9 | 2019-05-09 10:56:13 +0900 | [diff] [blame] | 211 | func vndkCoreLibraries(config android.Config) *[]string { |
| 212 | return config.Once(vndkCoreLibrariesKey, func() interface{} { |
| 213 | return &[]string{} |
| 214 | }).(*[]string) |
| 215 | } |
| 216 | |
| 217 | func vndkSpLibraries(config android.Config) *[]string { |
| 218 | return config.Once(vndkSpLibrariesKey, func() interface{} { |
| 219 | return &[]string{} |
| 220 | }).(*[]string) |
| 221 | } |
| 222 | |
| 223 | func llndkLibraries(config android.Config) *[]string { |
| 224 | return config.Once(llndkLibrariesKey, func() interface{} { |
| 225 | return &[]string{} |
| 226 | }).(*[]string) |
| 227 | } |
| 228 | |
| 229 | func vndkPrivateLibraries(config android.Config) *[]string { |
| 230 | return config.Once(vndkPrivateLibrariesKey, func() interface{} { |
| 231 | return &[]string{} |
| 232 | }).(*[]string) |
| 233 | } |
| 234 | |
| 235 | func vndkUsingCoreVariantLibraries(config android.Config) *[]string { |
| 236 | return config.Once(vndkUsingCoreVariantLibrariesKey, func() interface{} { |
| 237 | return &[]string{} |
| 238 | }).(*[]string) |
| 239 | } |
| 240 | |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 241 | func 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 Kim | 9516ee9 | 2019-05-09 10:56:13 +0900 | [diff] [blame] | 246 | |
Inseob Kim | ae55303 | 2019-05-14 18:52:49 +0900 | [diff] [blame] | 247 | func vndkSnapshotOutputs(config android.Config) *android.RuleBuilderInstalls { |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 248 | return config.Once(vndkSnapshotOutputsKey, func() interface{} { |
Inseob Kim | ae55303 | 2019-05-14 18:52:49 +0900 | [diff] [blame] | 249 | return &android.RuleBuilderInstalls{} |
| 250 | }).(*android.RuleBuilderInstalls) |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 251 | } |
Inseob Kim | 9516ee9 | 2019-05-09 10:56:13 +0900 | [diff] [blame] | 252 | |
Jooyung Han | 097087b | 2019-10-22 19:32:18 +0900 | [diff] [blame^] | 253 | func 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() |
| 267 | func setVndkMustUseVendorVariantListForTest(config android.Config, mustUseVendorVariantList []string) { |
| 268 | config.Once(testVndkMustUseVendorVariantListKey, func() interface{} { |
| 269 | return mustUseVendorVariantList |
| 270 | }) |
| 271 | } |
| 272 | |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 273 | func processLlndkLibrary(mctx android.BottomUpMutatorContext, m *Module) { |
| 274 | lib := m.linker.(*llndkStubDecorator) |
| 275 | name := strings.TrimSuffix(m.Name(), llndkLibrarySuffix) |
Inseob Kim | 9516ee9 | 2019-05-09 10:56:13 +0900 | [diff] [blame] | 276 | |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 277 | vndkLibrariesLock.Lock() |
| 278 | defer vndkLibrariesLock.Unlock() |
Inseob Kim | 9516ee9 | 2019-05-09 10:56:13 +0900 | [diff] [blame] | 279 | |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 280 | 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 Park | d5b18a5 | 2017-08-03 21:22:50 +0900 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | } |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 293 | |
| 294 | func 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 Han | 097087b | 2019-10-22 19:32:18 +0900 | [diff] [blame^] | 301 | if inList(name, vndkMustUseVendorVariantList(mctx.Config())) { |
| 302 | m.Properties.MustUseVendorVariant = true |
| 303 | } |
| 304 | if mctx.DeviceConfig().VndkUseCoreVariant() && !m.mustUseVendorVariant() { |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 305 | 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 | |
| 335 | // gather list of vndk-core, vndk-sp, and ll-ndk libs |
| 336 | func VndkMutator(mctx android.BottomUpMutatorContext) { |
| 337 | m, ok := mctx.Module().(*Module) |
| 338 | if !ok { |
| 339 | return |
| 340 | } |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 341 | if !m.Enabled() { |
| 342 | return |
| 343 | } |
Justin Yun | 7390ea3 | 2019-09-08 11:34:06 +0900 | [diff] [blame] | 344 | if m.Target().NativeBridge == android.NativeBridgeEnabled { |
| 345 | // Skip native_bridge modules |
| 346 | return |
| 347 | } |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 348 | |
| 349 | if _, ok := m.linker.(*llndkStubDecorator); ok { |
| 350 | processLlndkLibrary(mctx, m) |
| 351 | return |
| 352 | } |
| 353 | |
| 354 | lib, is_lib := m.linker.(*libraryDecorator) |
| 355 | prebuilt_lib, is_prebuilt_lib := m.linker.(*prebuiltLibraryLinker) |
| 356 | |
Inseob Kim | 64c4395 | 2019-08-26 16:52:35 +0900 | [diff] [blame] | 357 | if (is_lib && lib.buildShared()) || (is_prebuilt_lib && prebuilt_lib.buildShared()) { |
| 358 | if m.vndkdep != nil && m.vndkdep.isVndk() && !m.vndkdep.isVndkExt() { |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 359 | processVndkLibrary(mctx, m) |
| 360 | return |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | func init() { |
| 366 | android.RegisterSingletonType("vndk-snapshot", VndkSnapshotSingleton) |
| 367 | android.RegisterMakeVarsProvider(pctx, func(ctx android.MakeVarsContext) { |
| 368 | outputs := vndkSnapshotOutputs(ctx.Config()) |
Inseob Kim | ae55303 | 2019-05-14 18:52:49 +0900 | [diff] [blame] | 369 | ctx.Strict("SOONG_VNDK_SNAPSHOT_FILES", outputs.String()) |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 370 | }) |
| 371 | } |
| 372 | |
| 373 | func VndkSnapshotSingleton() android.Singleton { |
| 374 | return &vndkSnapshotSingleton{} |
| 375 | } |
| 376 | |
| 377 | type vndkSnapshotSingleton struct{} |
| 378 | |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 379 | func (c *vndkSnapshotSingleton) GenerateBuildActions(ctx android.SingletonContext) { |
| 380 | // BOARD_VNDK_VERSION must be set to 'current' in order to generate a VNDK snapshot. |
| 381 | if ctx.DeviceConfig().VndkVersion() != "current" { |
| 382 | return |
| 383 | } |
| 384 | |
| 385 | if ctx.DeviceConfig().PlatformVndkVersion() == "" { |
| 386 | return |
| 387 | } |
| 388 | |
| 389 | if ctx.DeviceConfig().BoardVndkRuntimeDisable() { |
| 390 | return |
| 391 | } |
| 392 | |
Jooyung Han | 097087b | 2019-10-22 19:32:18 +0900 | [diff] [blame^] | 393 | c.buildVndkLibrariesTxtFiles(ctx) |
| 394 | |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 395 | outputs := vndkSnapshotOutputs(ctx.Config()) |
| 396 | |
| 397 | snapshotDir := "vndk-snapshot" |
| 398 | |
Inseob Kim | ae55303 | 2019-05-14 18:52:49 +0900 | [diff] [blame] | 399 | vndkLibDir := make(map[android.ArchType]string) |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 400 | |
Inseob Kim | ae55303 | 2019-05-14 18:52:49 +0900 | [diff] [blame] | 401 | snapshotVariantDir := ctx.DeviceConfig().DeviceArch() |
| 402 | for _, target := range ctx.Config().Targets[android.Android] { |
| 403 | dir := snapshotVariantDir |
| 404 | if ctx.DeviceConfig().BinderBitness() == "32" { |
| 405 | dir = filepath.Join(dir, "binder32") |
| 406 | } |
| 407 | arch := "arch-" + target.Arch.ArchType.String() |
| 408 | if target.Arch.ArchVariant != "" { |
| 409 | arch += "-" + target.Arch.ArchVariant |
| 410 | } |
| 411 | dir = filepath.Join(dir, arch) |
| 412 | vndkLibDir[target.Arch.ArchType] = dir |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 413 | } |
Inseob Kim | ae55303 | 2019-05-14 18:52:49 +0900 | [diff] [blame] | 414 | configsDir := filepath.Join(snapshotVariantDir, "configs") |
| 415 | noticeDir := filepath.Join(snapshotVariantDir, "NOTICE_FILES") |
| 416 | includeDir := filepath.Join(snapshotVariantDir, "include") |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 417 | noticeBuilt := make(map[string]bool) |
| 418 | |
Inseob Kim | ae55303 | 2019-05-14 18:52:49 +0900 | [diff] [blame] | 419 | installSnapshotFileFromPath := func(path android.Path, out string) { |
| 420 | ctx.Build(pctx, android.BuildParams{ |
| 421 | Rule: android.Cp, |
| 422 | Input: path, |
| 423 | Output: android.PathForOutput(ctx, snapshotDir, out), |
| 424 | Description: "vndk snapshot " + out, |
| 425 | Args: map[string]string{ |
| 426 | "cpFlags": "-f -L", |
| 427 | }, |
| 428 | }) |
| 429 | *outputs = append(*outputs, android.RuleBuilderInstall{ |
| 430 | From: android.PathForOutput(ctx, snapshotDir, out), |
| 431 | To: out, |
| 432 | }) |
| 433 | } |
| 434 | installSnapshotFileFromContent := func(content, out string) { |
| 435 | ctx.Build(pctx, android.BuildParams{ |
| 436 | Rule: android.WriteFile, |
| 437 | Output: android.PathForOutput(ctx, snapshotDir, out), |
| 438 | Description: "vndk snapshot " + out, |
| 439 | Args: map[string]string{ |
| 440 | "content": content, |
| 441 | }, |
| 442 | }) |
| 443 | *outputs = append(*outputs, android.RuleBuilderInstall{ |
| 444 | From: android.PathForOutput(ctx, snapshotDir, out), |
| 445 | To: out, |
| 446 | }) |
| 447 | } |
| 448 | |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 449 | tryBuildNotice := func(m *Module) { |
Inseob Kim | ae55303 | 2019-05-14 18:52:49 +0900 | [diff] [blame] | 450 | name := ctx.ModuleName(m) + ".so.txt" |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 451 | |
| 452 | if _, ok := noticeBuilt[name]; ok { |
| 453 | return |
| 454 | } |
| 455 | |
| 456 | noticeBuilt[name] = true |
| 457 | |
| 458 | if m.NoticeFile().Valid() { |
Inseob Kim | ae55303 | 2019-05-14 18:52:49 +0900 | [diff] [blame] | 459 | installSnapshotFileFromPath(m.NoticeFile().Path(), filepath.Join(noticeDir, name)) |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | |
| 463 | vndkCoreLibraries := vndkCoreLibraries(ctx.Config()) |
| 464 | vndkSpLibraries := vndkSpLibraries(ctx.Config()) |
| 465 | vndkPrivateLibraries := vndkPrivateLibraries(ctx.Config()) |
| 466 | |
Inseob Kim | ae55303 | 2019-05-14 18:52:49 +0900 | [diff] [blame] | 467 | var generatedHeaders android.Paths |
| 468 | includeDirs := make(map[string]bool) |
| 469 | |
| 470 | type vndkSnapshotLibraryInterface interface { |
| 471 | exportedFlagsProducer |
| 472 | libraryInterface |
| 473 | } |
| 474 | |
| 475 | var _ vndkSnapshotLibraryInterface = (*prebuiltLibraryLinker)(nil) |
| 476 | var _ vndkSnapshotLibraryInterface = (*libraryDecorator)(nil) |
| 477 | |
| 478 | installVndkSnapshotLib := func(m *Module, l vndkSnapshotLibraryInterface, dir string) bool { |
| 479 | name := ctx.ModuleName(m) |
| 480 | libOut := filepath.Join(dir, name+".so") |
| 481 | |
| 482 | installSnapshotFileFromPath(m.outputFile.Path(), libOut) |
| 483 | tryBuildNotice(m) |
| 484 | |
| 485 | if ctx.Config().VndkSnapshotBuildArtifacts() { |
| 486 | prop := struct { |
| 487 | ExportedDirs []string `json:",omitempty"` |
| 488 | ExportedSystemDirs []string `json:",omitempty"` |
| 489 | ExportedFlags []string `json:",omitempty"` |
| 490 | RelativeInstallPath string `json:",omitempty"` |
| 491 | }{} |
| 492 | prop.ExportedFlags = l.exportedFlags() |
| 493 | prop.ExportedDirs = l.exportedDirs() |
| 494 | prop.ExportedSystemDirs = l.exportedSystemDirs() |
| 495 | prop.RelativeInstallPath = m.RelativeInstallPath() |
| 496 | |
| 497 | propOut := libOut + ".json" |
| 498 | |
| 499 | j, err := json.Marshal(prop) |
| 500 | if err != nil { |
| 501 | ctx.Errorf("json marshal to %q failed: %#v", propOut, err) |
| 502 | return false |
| 503 | } |
| 504 | |
| 505 | installSnapshotFileFromContent(string(j), propOut) |
| 506 | } |
| 507 | return true |
| 508 | } |
| 509 | |
| 510 | isVndkSnapshotLibrary := func(m *Module) (i vndkSnapshotLibraryInterface, libDir string, isVndkSnapshotLib bool) { |
| 511 | if m.Target().NativeBridge == android.NativeBridgeEnabled { |
| 512 | return nil, "", false |
| 513 | } |
| 514 | if !m.useVndk() || !m.IsForPlatform() || !m.installable() { |
| 515 | return nil, "", false |
| 516 | } |
| 517 | l, ok := m.linker.(vndkSnapshotLibraryInterface) |
| 518 | if !ok || !l.shared() { |
| 519 | return nil, "", false |
| 520 | } |
| 521 | name := ctx.ModuleName(m) |
| 522 | if inList(name, *vndkCoreLibraries) { |
| 523 | return l, filepath.Join("shared", "vndk-core"), true |
| 524 | } else if inList(name, *vndkSpLibraries) { |
| 525 | return l, filepath.Join("shared", "vndk-sp"), true |
| 526 | } else { |
| 527 | return nil, "", false |
| 528 | } |
| 529 | } |
| 530 | |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 531 | ctx.VisitAllModules(func(module android.Module) { |
| 532 | m, ok := module.(*Module) |
Inseob Kim | ae55303 | 2019-05-14 18:52:49 +0900 | [diff] [blame] | 533 | if !ok || !m.Enabled() { |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 534 | return |
| 535 | } |
| 536 | |
Inseob Kim | ae55303 | 2019-05-14 18:52:49 +0900 | [diff] [blame] | 537 | baseDir, ok := vndkLibDir[m.Target().Arch.ArchType] |
| 538 | if !ok { |
dimitry | 51ea18a | 2019-05-20 10:39:52 +0200 | [diff] [blame] | 539 | return |
| 540 | } |
| 541 | |
Inseob Kim | ae55303 | 2019-05-14 18:52:49 +0900 | [diff] [blame] | 542 | l, libDir, ok := isVndkSnapshotLibrary(m) |
| 543 | if !ok { |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 544 | return |
| 545 | } |
| 546 | |
Inseob Kim | ae55303 | 2019-05-14 18:52:49 +0900 | [diff] [blame] | 547 | if !installVndkSnapshotLib(m, l, filepath.Join(baseDir, libDir)) { |
| 548 | return |
| 549 | } |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 550 | |
Inseob Kim | ae55303 | 2019-05-14 18:52:49 +0900 | [diff] [blame] | 551 | generatedHeaders = append(generatedHeaders, l.exportedDeps()...) |
| 552 | for _, dir := range append(l.exportedDirs(), l.exportedSystemDirs()...) { |
| 553 | includeDirs[dir] = true |
| 554 | } |
| 555 | }) |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 556 | |
Inseob Kim | ae55303 | 2019-05-14 18:52:49 +0900 | [diff] [blame] | 557 | if ctx.Config().VndkSnapshotBuildArtifacts() { |
| 558 | headers := make(map[string]bool) |
| 559 | |
| 560 | for _, dir := range android.SortedStringKeys(includeDirs) { |
| 561 | // workaround to determine if dir is under output directory |
| 562 | if strings.HasPrefix(dir, android.PathForOutput(ctx).String()) { |
| 563 | continue |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 564 | } |
Inseob Kim | ae55303 | 2019-05-14 18:52:49 +0900 | [diff] [blame] | 565 | exts := headerExts |
| 566 | // Glob all files under this special directory, because of C++ headers. |
| 567 | if strings.HasPrefix(dir, "external/libcxx/include") { |
| 568 | exts = []string{""} |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 569 | } |
Inseob Kim | ae55303 | 2019-05-14 18:52:49 +0900 | [diff] [blame] | 570 | for _, ext := range exts { |
| 571 | glob, err := ctx.GlobWithDeps(dir+"/**/*"+ext, nil) |
| 572 | if err != nil { |
| 573 | ctx.Errorf("%#v\n", err) |
| 574 | return |
| 575 | } |
| 576 | for _, header := range glob { |
| 577 | if strings.HasSuffix(header, "/") { |
| 578 | continue |
| 579 | } |
| 580 | headers[header] = true |
| 581 | } |
| 582 | } |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 583 | } |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 584 | |
Inseob Kim | ae55303 | 2019-05-14 18:52:49 +0900 | [diff] [blame] | 585 | for _, header := range android.SortedStringKeys(headers) { |
| 586 | installSnapshotFileFromPath(android.PathForSource(ctx, header), |
| 587 | filepath.Join(includeDir, header)) |
| 588 | } |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 589 | |
Inseob Kim | ae55303 | 2019-05-14 18:52:49 +0900 | [diff] [blame] | 590 | isHeader := func(path string) bool { |
| 591 | for _, ext := range headerExts { |
| 592 | if strings.HasSuffix(path, ext) { |
| 593 | return true |
| 594 | } |
| 595 | } |
| 596 | return false |
| 597 | } |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 598 | |
Inseob Kim | ae55303 | 2019-05-14 18:52:49 +0900 | [diff] [blame] | 599 | for _, path := range android.PathsToDirectorySortedPaths(android.FirstUniquePaths(generatedHeaders)) { |
| 600 | header := path.String() |
| 601 | |
| 602 | if !isHeader(header) { |
| 603 | continue |
| 604 | } |
| 605 | |
| 606 | installSnapshotFileFromPath(path, filepath.Join(includeDir, header)) |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | installSnapshotFileFromContent(android.JoinWithSuffix(*vndkCoreLibraries, ".so", "\\n"), |
| 611 | filepath.Join(configsDir, "vndkcore.libraries.txt")) |
| 612 | installSnapshotFileFromContent(android.JoinWithSuffix(*vndkPrivateLibraries, ".so", "\\n"), |
| 613 | filepath.Join(configsDir, "vndkprivate.libraries.txt")) |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 614 | |
| 615 | var modulePathTxtBuilder strings.Builder |
| 616 | |
Colin Cross | 4c2c46f | 2019-06-03 15:26:05 -0700 | [diff] [blame] | 617 | modulePaths := modulePaths(ctx.Config()) |
Colin Cross | 4c2c46f | 2019-06-03 15:26:05 -0700 | [diff] [blame] | 618 | |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 619 | first := true |
Inseob Kim | ae55303 | 2019-05-14 18:52:49 +0900 | [diff] [blame] | 620 | for _, lib := range android.SortedStringKeys(modulePaths) { |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 621 | if first { |
| 622 | first = false |
| 623 | } else { |
| 624 | modulePathTxtBuilder.WriteString("\\n") |
| 625 | } |
| 626 | modulePathTxtBuilder.WriteString(lib) |
| 627 | modulePathTxtBuilder.WriteString(".so ") |
Colin Cross | 4c2c46f | 2019-06-03 15:26:05 -0700 | [diff] [blame] | 628 | modulePathTxtBuilder.WriteString(modulePaths[lib]) |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 629 | } |
| 630 | |
Inseob Kim | ae55303 | 2019-05-14 18:52:49 +0900 | [diff] [blame] | 631 | installSnapshotFileFromContent(modulePathTxtBuilder.String(), |
| 632 | filepath.Join(configsDir, "module_paths.txt")) |
Inseob Kim | 1f086e2 | 2019-05-09 13:29:15 +0900 | [diff] [blame] | 633 | } |
Jooyung Han | 097087b | 2019-10-22 19:32:18 +0900 | [diff] [blame^] | 634 | |
| 635 | func installListFile(ctx android.SingletonContext, list []string, pathComponents ...string) android.OutputPath { |
| 636 | out := android.PathForOutput(ctx, pathComponents...) |
| 637 | ctx.Build(pctx, android.BuildParams{ |
| 638 | Rule: android.WriteFile, |
| 639 | Output: out, |
| 640 | Description: "Writing " + out.String(), |
| 641 | Args: map[string]string{ |
| 642 | "content": strings.Join(list, "\\n"), |
| 643 | }, |
| 644 | }) |
| 645 | return out |
| 646 | } |
| 647 | |
| 648 | func (c *vndkSnapshotSingleton) buildVndkLibrariesTxtFiles(ctx android.SingletonContext) { |
| 649 | var ( |
| 650 | llndk, vndkcore, vndksp, vndkprivate, vndkcorevariant, merged []string |
| 651 | ) |
| 652 | vndkVersion := ctx.DeviceConfig().PlatformVndkVersion() |
| 653 | config := ctx.Config() |
| 654 | ctx.VisitAllModules(func(m android.Module) { |
| 655 | if !m.Enabled() { |
| 656 | return |
| 657 | } |
| 658 | c, ok := m.(*Module) |
| 659 | if !ok || c.Os().Class != android.Device { |
| 660 | return |
| 661 | } |
| 662 | lib, ok := c.linker.(interface{ shared() bool }) |
| 663 | if !ok || !lib.shared() { |
| 664 | return |
| 665 | } |
| 666 | |
| 667 | if !c.OutputFile().Valid() { |
| 668 | return |
| 669 | } |
| 670 | |
| 671 | filename := c.OutputFile().Path().Base() |
| 672 | if c.isLlndk(config) { |
| 673 | llndk = append(llndk, filename) |
| 674 | if c.isVndkPrivate(config) { |
| 675 | vndkprivate = append(vndkprivate, filename) |
| 676 | } |
| 677 | } else if c.vndkVersion() == vndkVersion && c.isVndk() && !c.isVndkExt() { |
| 678 | if c.isVndkSp() { |
| 679 | vndksp = append(vndksp, filename) |
| 680 | } else { |
| 681 | vndkcore = append(vndkcore, filename) |
| 682 | } |
| 683 | if c.isVndkPrivate(config) { |
| 684 | vndkprivate = append(vndkprivate, filename) |
| 685 | } |
| 686 | if ctx.DeviceConfig().VndkUseCoreVariant() && !c.mustUseVendorVariant() { |
| 687 | vndkcorevariant = append(vndkcorevariant, filename) |
| 688 | } |
| 689 | } |
| 690 | }) |
| 691 | llndk = android.SortedUniqueStrings(llndk) |
| 692 | vndkcore = android.SortedUniqueStrings(vndkcore) |
| 693 | vndksp = android.SortedUniqueStrings(vndksp) |
| 694 | vndkprivate = android.SortedUniqueStrings(vndkprivate) |
| 695 | vndkcorevariant = android.SortedUniqueStrings(vndkcorevariant) |
| 696 | |
| 697 | installListFile(ctx, llndk, "vndk", "llndk.libraries.txt") |
| 698 | installListFile(ctx, vndkcore, "vndk", "vndkcore.libraries.txt") |
| 699 | installListFile(ctx, vndksp, "vndk", "vndksp.libraries.txt") |
| 700 | installListFile(ctx, vndkprivate, "vndk", "vndkprivate.libraries.txt") |
| 701 | installListFile(ctx, vndkcorevariant, "vndk", "vndkcorevariant.libraries.txt") |
| 702 | |
| 703 | // merged & tagged & filtered-out(libclang_rt) |
| 704 | filterOutLibClangRt := func(libList []string) (filtered []string) { |
| 705 | for _, lib := range libList { |
| 706 | if !strings.HasPrefix(lib, "libclang_rt.") { |
| 707 | filtered = append(filtered, lib) |
| 708 | } |
| 709 | } |
| 710 | return |
| 711 | } |
| 712 | merged = append(merged, addPrefix(filterOutLibClangRt(llndk), "LLNDK: ")...) |
| 713 | merged = append(merged, addPrefix(vndksp, "VNDK-SP: ")...) |
| 714 | merged = append(merged, addPrefix(filterOutLibClangRt(vndkcore), "VNDK-core: ")...) |
| 715 | merged = append(merged, addPrefix(vndkprivate, "VNDK-private: ")...) |
| 716 | |
| 717 | installListFile(ctx, merged, "vndk", "vndk.libraries.txt") |
| 718 | } |