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
15package apex
16
17import (
18	"strings"
19
20	"android/soong/android"
21	"android/soong/cc"
22
23	"github.com/google/blueprint/proptools"
24)
25
26const (
27	vndkApexName       = "com.android.vndk"
28	vndkApexNamePrefix = vndkApexName + ".v"
29)
30
31// apex_vndk creates a special variant of apex modules which contains only VNDK libraries.
32// If `vndk_version` is specified, the VNDK libraries of the specified VNDK version are gathered automatically.
33// If not specified, then the "current" versions are gathered.
34func vndkApexBundleFactory() android.Module {
35	bundle := newApexBundle()
36	bundle.vndkApex = true
37	bundle.AddProperties(&bundle.vndkProperties)
38	android.AddLoadHook(bundle, func(ctx android.LoadHookContext) {
39		ctx.AppendProperties(&struct {
40			Compile_multilib *string
41		}{
42			proptools.StringPtr("both"),
43		})
44	})
45	return bundle
46}
47
48func (a *apexBundle) vndkVersion() string {
49	return proptools.StringDefault(a.vndkProperties.Vndk_version, "current")
50}
51
52type apexVndkProperties struct {
53	// Indicates VNDK version of which this VNDK APEX bundles VNDK libs.
54	Vndk_version *string
55}
56
57func apexVndkMutator(mctx android.TopDownMutatorContext) {
58	if ab, ok := mctx.Module().(*apexBundle); ok && ab.vndkApex {
59		if ab.IsNativeBridgeSupported() {
60			mctx.PropertyErrorf("native_bridge_supported", "%q doesn't support native bridge binary.", mctx.ModuleType())
61		}
62
63		vndkVersion := ab.vndkVersion()
64		if vndkVersion != "" {
65			apiLevel, err := android.ApiLevelFromUser(mctx, vndkVersion)
66			if err != nil {
67				mctx.PropertyErrorf("vndk_version", "%s", err.Error())
68				return
69			}
70
71			targets := mctx.MultiTargets()
72			if len(targets) > 0 && apiLevel.LessThan(cc.MinApiForArch(mctx, targets[0].Arch.ArchType)) {
73				// Disable VNDK APEXes for VNDK versions less than the minimum supported API
74				// level for the primary architecture.
75				ab.Disable()
76			}
77		}
78	}
79}
80
81func apexVndkDepsMutator(mctx android.BottomUpMutatorContext) {
82	if m, ok := mctx.Module().(*cc.Module); ok && cc.IsForVndkApex(mctx, m) {
83		vndkVersion := m.VndkVersion()
84
85		if vndkVersion == "" {
86			return
87		}
88		vndkVersion = "v" + vndkVersion
89
90		vndkApexName := "com.android.vndk." + vndkVersion
91
92		if mctx.OtherModuleExists(vndkApexName) {
93			mctx.AddReverseDependency(mctx.Module(), sharedLibTag, vndkApexName)
94		}
95	} else if a, ok := mctx.Module().(*apexBundle); ok && a.vndkApex {
96		vndkVersion := proptools.StringDefault(a.vndkProperties.Vndk_version, "current")
97		mctx.AddDependency(mctx.Module(), prebuiltTag, cc.VndkLibrariesTxtModules(vndkVersion, mctx)...)
98	}
99}
100
101// name is module.BaseModuleName() which is used as LOCAL_MODULE_NAME and also LOCAL_OVERRIDES_*
102func makeCompatSymlinks(name string, ctx android.ModuleContext) (symlinks android.InstallPaths) {
103	// small helper to add symlink commands
104	addSymlink := func(target string, dir android.InstallPath, linkName string) {
105		symlinks = append(symlinks, ctx.InstallAbsoluteSymlink(dir, linkName, target))
106	}
107
108	// TODO(b/142911355): [VNDK APEX] Fix hard-coded references to /system/lib/vndk
109	// When all hard-coded references are fixed, remove symbolic links
110	// Note that  we should keep following symlinks for older VNDKs (<=29)
111	// Since prebuilt vndk libs still depend on system/lib/vndk path
112	if strings.HasPrefix(name, vndkApexNamePrefix) {
113		vndkVersion := strings.TrimPrefix(name, vndkApexNamePrefix)
114		if ver, err := android.ApiLevelFromUser(ctx, vndkVersion); err != nil {
115			ctx.ModuleErrorf("apex_vndk should be named as %v<ver:number>: %s", vndkApexNamePrefix, name)
116			return
117		} else if ver.GreaterThan(android.SdkVersion_Android10) {
118			return
119		}
120		// the name of vndk apex is formatted "com.android.vndk.v" + version
121		apexName := vndkApexNamePrefix + vndkVersion
122		if ctx.Config().Android64() {
123			dir := android.PathForModuleInPartitionInstall(ctx, "system", "lib64")
124			addSymlink("/apex/"+apexName+"/lib64", dir, "vndk-sp-"+vndkVersion)
125			addSymlink("/apex/"+apexName+"/lib64", dir, "vndk-"+vndkVersion)
126		}
127		if !ctx.Config().Android64() || ctx.DeviceConfig().DeviceSecondaryArch() != "" {
128			dir := android.PathForModuleInPartitionInstall(ctx, "system", "lib")
129			addSymlink("/apex/"+apexName+"/lib", dir, "vndk-sp-"+vndkVersion)
130			addSymlink("/apex/"+apexName+"/lib", dir, "vndk-"+vndkVersion)
131		}
132	}
133
134	// http://b/121248172 - create a link from /system/usr/icu to
135	// /apex/com.android.i18n/etc/icu so that apps can find the ICU .dat file.
136	// A symlink can't overwrite a directory and the /system/usr/icu directory once
137	// existed so the required structure must be created whatever we find.
138	if name == "com.android.i18n" {
139		dir := android.PathForModuleInPartitionInstall(ctx, "system", "usr")
140		addSymlink("/apex/com.android.i18n/etc/icu", dir, "icu")
141	}
142
143	return symlinks
144}
145