Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 1 | // Copyright (C) 2019 The Android Open Source Project |
| 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 apex |
| 16 | |
| 17 | import ( |
| 18 | "path/filepath" |
| 19 | "strings" |
| 20 | "sync" |
| 21 | |
| 22 | "android/soong/android" |
| 23 | "android/soong/cc" |
| 24 | |
| 25 | "github.com/google/blueprint/proptools" |
| 26 | ) |
| 27 | |
| 28 | const ( |
Jooyung Han | 27151d9 | 2019-12-16 17:45:32 +0900 | [diff] [blame] | 29 | vndkApexName = "com.android.vndk" |
| 30 | vndkApexNamePrefix = vndkApexName + ".v" |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 31 | ) |
| 32 | |
| 33 | // apex_vndk creates a special variant of apex modules which contains only VNDK libraries. |
| 34 | // If `vndk_version` is specified, the VNDK libraries of the specified VNDK version are gathered automatically. |
| 35 | // If not specified, then the "current" versions are gathered. |
| 36 | func vndkApexBundleFactory() android.Module { |
| 37 | bundle := newApexBundle() |
| 38 | bundle.vndkApex = true |
| 39 | bundle.AddProperties(&bundle.vndkProperties) |
| 40 | android.AddLoadHook(bundle, func(ctx android.LoadHookContext) { |
| 41 | ctx.AppendProperties(&struct { |
| 42 | Compile_multilib *string |
| 43 | }{ |
| 44 | proptools.StringPtr("both"), |
| 45 | }) |
| 46 | }) |
| 47 | return bundle |
| 48 | } |
| 49 | |
| 50 | func (a *apexBundle) vndkVersion(config android.DeviceConfig) string { |
| 51 | vndkVersion := proptools.StringDefault(a.vndkProperties.Vndk_version, "current") |
| 52 | if vndkVersion == "current" { |
| 53 | vndkVersion = config.PlatformVndkVersion() |
| 54 | } |
| 55 | return vndkVersion |
| 56 | } |
| 57 | |
| 58 | type apexVndkProperties struct { |
| 59 | // Indicates VNDK version of which this VNDK APEX bundles VNDK libs. Default is Platform VNDK Version. |
| 60 | Vndk_version *string |
| 61 | } |
| 62 | |
| 63 | var ( |
| 64 | vndkApexListKey = android.NewOnceKey("vndkApexList") |
| 65 | vndkApexListMutex sync.Mutex |
| 66 | ) |
| 67 | |
| 68 | func vndkApexList(config android.Config) map[string]string { |
| 69 | return config.Once(vndkApexListKey, func() interface{} { |
| 70 | return map[string]string{} |
| 71 | }).(map[string]string) |
| 72 | } |
| 73 | |
| 74 | func apexVndkMutator(mctx android.TopDownMutatorContext) { |
| 75 | if ab, ok := mctx.Module().(*apexBundle); ok && ab.vndkApex { |
| 76 | if ab.IsNativeBridgeSupported() { |
| 77 | mctx.PropertyErrorf("native_bridge_supported", "%q doesn't support native bridge binary.", mctx.ModuleType()) |
| 78 | } |
| 79 | |
| 80 | vndkVersion := ab.vndkVersion(mctx.DeviceConfig()) |
| 81 | // Ensure VNDK APEX mount point is formatted as com.android.vndk.v### |
| 82 | ab.properties.Apex_name = proptools.StringPtr(vndkApexNamePrefix + vndkVersion) |
| 83 | |
| 84 | // vndk_version should be unique |
| 85 | vndkApexListMutex.Lock() |
| 86 | defer vndkApexListMutex.Unlock() |
| 87 | vndkApexList := vndkApexList(mctx.Config()) |
| 88 | if other, ok := vndkApexList[vndkVersion]; ok { |
| 89 | mctx.PropertyErrorf("vndk_version", "%v is already defined in %q", vndkVersion, other) |
| 90 | } |
| 91 | vndkApexList[vndkVersion] = mctx.ModuleName() |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | func apexVndkDepsMutator(mctx android.BottomUpMutatorContext) { |
| 96 | if m, ok := mctx.Module().(*cc.Module); ok && cc.IsForVndkApex(mctx, m) { |
| 97 | vndkVersion := m.VndkVersion() |
| 98 | vndkApexList := vndkApexList(mctx.Config()) |
| 99 | if vndkApex, ok := vndkApexList[vndkVersion]; ok { |
| 100 | mctx.AddReverseDependency(mctx.Module(), sharedLibTag, vndkApex) |
| 101 | } |
| 102 | } else if a, ok := mctx.Module().(*apexBundle); ok && a.vndkApex { |
| 103 | vndkVersion := proptools.StringDefault(a.vndkProperties.Vndk_version, "current") |
| 104 | mctx.AddDependency(mctx.Module(), prebuiltTag, cc.VndkLibrariesTxtModules(vndkVersion)...) |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | func makeCompatSymlinks(apexName string, ctx android.ModuleContext) (symlinks []string) { |
| 109 | // small helper to add symlink commands |
| 110 | addSymlink := func(target, dir, linkName string) { |
Jooyung Han | 4791cb5 | 2019-10-25 18:17:23 +0900 | [diff] [blame^] | 111 | link := filepath.Join(dir, linkName) |
| 112 | symlinks = append(symlinks, "mkdir -p "+dir+" && rm -rf "+link+" && ln -sf "+target+" "+link) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | // TODO(b/142911355): [VNDK APEX] Fix hard-coded references to /system/lib/vndk |
| 116 | // When all hard-coded references are fixed, remove symbolic links |
| 117 | // Note that we should keep following symlinks for older VNDKs (<=29) |
| 118 | // Since prebuilt vndk libs still depend on system/lib/vndk path |
| 119 | if strings.HasPrefix(apexName, vndkApexNamePrefix) { |
| 120 | // the name of vndk apex is formatted "com.android.vndk.v" + version |
| 121 | vndkVersion := strings.TrimPrefix(apexName, vndkApexNamePrefix) |
| 122 | if ctx.Config().Android64() { |
Jooyung Han | 4791cb5 | 2019-10-25 18:17:23 +0900 | [diff] [blame^] | 123 | addSymlink("/apex/"+apexName+"/lib64", "$(TARGET_OUT)/lib64", "vndk-sp-"+vndkVersion) |
| 124 | addSymlink("/apex/"+apexName+"/lib64", "$(TARGET_OUT)/lib64", "vndk-"+vndkVersion) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 125 | } |
| 126 | if !ctx.Config().Android64() || ctx.DeviceConfig().DeviceSecondaryArch() != "" { |
Jooyung Han | 4791cb5 | 2019-10-25 18:17:23 +0900 | [diff] [blame^] | 127 | addSymlink("/apex/"+apexName+"/lib", "$(TARGET_OUT)/lib", "vndk-sp-"+vndkVersion) |
| 128 | addSymlink("/apex/"+apexName+"/lib", "$(TARGET_OUT)/lib", "vndk-"+vndkVersion) |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // http://b/121248172 - create a link from /system/usr/icu to |
| 133 | // /apex/com.android.i18n/etc/icu so that apps can find the ICU .dat file. |
| 134 | // A symlink can't overwrite a directory and the /system/usr/icu directory once |
| 135 | // existed so the required structure must be created whatever we find. |
| 136 | if apexName == "com.android.i18n" { |
| 137 | addSymlink("/apex/"+apexName+"/etc/icu", "$(TARGET_OUT)/usr", "icu") |
| 138 | } |
| 139 | |
| 140 | // TODO(b/124106384): Clean up compat symlinks for ART binaries. |
| 141 | if strings.HasPrefix(apexName, "com.android.art.") { |
| 142 | artBinaries := []string{"dalvikvm", "dex2oat"} |
| 143 | for _, b := range artBinaries { |
| 144 | addSymlink("/apex/com.android.art/bin/"+b, "$(TARGET_OUT)/bin", b) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | return |
| 148 | } |