blob: 15f7f87fe3f0b15dfddd70ccc068a506afefb39d [file] [log] [blame]
Jiyong Park09d77522019-11-18 11:16:27 +09001// 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
15package apex
16
17import (
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
28const (
29 vndkApexNamePrefix = "com.android.vndk.v"
30)
31
32// apex_vndk creates a special variant of apex modules which contains only VNDK libraries.
33// If `vndk_version` is specified, the VNDK libraries of the specified VNDK version are gathered automatically.
34// If not specified, then the "current" versions are gathered.
35func vndkApexBundleFactory() android.Module {
36 bundle := newApexBundle()
37 bundle.vndkApex = true
38 bundle.AddProperties(&bundle.vndkProperties)
39 android.AddLoadHook(bundle, func(ctx android.LoadHookContext) {
40 ctx.AppendProperties(&struct {
41 Compile_multilib *string
42 }{
43 proptools.StringPtr("both"),
44 })
45 })
46 return bundle
47}
48
49func (a *apexBundle) vndkVersion(config android.DeviceConfig) string {
50 vndkVersion := proptools.StringDefault(a.vndkProperties.Vndk_version, "current")
51 if vndkVersion == "current" {
52 vndkVersion = config.PlatformVndkVersion()
53 }
54 return vndkVersion
55}
56
57type apexVndkProperties struct {
58 // Indicates VNDK version of which this VNDK APEX bundles VNDK libs. Default is Platform VNDK Version.
59 Vndk_version *string
60}
61
62var (
63 vndkApexListKey = android.NewOnceKey("vndkApexList")
64 vndkApexListMutex sync.Mutex
65)
66
67func vndkApexList(config android.Config) map[string]string {
68 return config.Once(vndkApexListKey, func() interface{} {
69 return map[string]string{}
70 }).(map[string]string)
71}
72
73func apexVndkMutator(mctx android.TopDownMutatorContext) {
74 if ab, ok := mctx.Module().(*apexBundle); ok && ab.vndkApex {
75 if ab.IsNativeBridgeSupported() {
76 mctx.PropertyErrorf("native_bridge_supported", "%q doesn't support native bridge binary.", mctx.ModuleType())
77 }
78
79 vndkVersion := ab.vndkVersion(mctx.DeviceConfig())
80 // Ensure VNDK APEX mount point is formatted as com.android.vndk.v###
81 ab.properties.Apex_name = proptools.StringPtr(vndkApexNamePrefix + vndkVersion)
82
83 // vndk_version should be unique
84 vndkApexListMutex.Lock()
85 defer vndkApexListMutex.Unlock()
86 vndkApexList := vndkApexList(mctx.Config())
87 if other, ok := vndkApexList[vndkVersion]; ok {
88 mctx.PropertyErrorf("vndk_version", "%v is already defined in %q", vndkVersion, other)
89 }
90 vndkApexList[vndkVersion] = mctx.ModuleName()
91 }
92}
93
94func apexVndkDepsMutator(mctx android.BottomUpMutatorContext) {
95 if m, ok := mctx.Module().(*cc.Module); ok && cc.IsForVndkApex(mctx, m) {
96 vndkVersion := m.VndkVersion()
97 vndkApexList := vndkApexList(mctx.Config())
98 if vndkApex, ok := vndkApexList[vndkVersion]; ok {
99 mctx.AddReverseDependency(mctx.Module(), sharedLibTag, vndkApex)
100 }
101 } else if a, ok := mctx.Module().(*apexBundle); ok && a.vndkApex {
102 vndkVersion := proptools.StringDefault(a.vndkProperties.Vndk_version, "current")
103 mctx.AddDependency(mctx.Module(), prebuiltTag, cc.VndkLibrariesTxtModules(vndkVersion)...)
104 }
105}
106
107func makeCompatSymlinks(apexName string, ctx android.ModuleContext) (symlinks []string) {
108 // small helper to add symlink commands
109 addSymlink := func(target, dir, linkName string) {
110 outDir := filepath.Join("$(PRODUCT_OUT)", dir)
111 link := filepath.Join(outDir, linkName)
112 symlinks = append(symlinks, "mkdir -p "+outDir+" && rm -rf "+link+" && ln -sf "+target+" "+link)
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() {
123 addSymlink("/apex/"+apexName+"/lib64", "/system/lib64", "vndk-sp-"+vndkVersion)
124 addSymlink("/apex/"+apexName+"/lib64", "/system/lib64", "vndk-"+vndkVersion)
125 }
126 if !ctx.Config().Android64() || ctx.DeviceConfig().DeviceSecondaryArch() != "" {
127 addSymlink("/apex/"+apexName+"/lib", "/system/lib", "vndk-sp-"+vndkVersion)
128 addSymlink("/apex/"+apexName+"/lib", "/system/lib", "vndk-"+vndkVersion)
129 }
130 }
131 return
132}