blob: 860678d6fee4c7a5d47d3ef436c6dd0c9c8a2634 [file] [log] [blame]
Justin Yun8effde42017-06-23 19:24:43 +09001// 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
15package cc
16
17import (
Colin Cross766efbc2017-08-17 14:55:15 -070018 "sort"
Jiyong Parkd5b18a52017-08-03 21:22:50 +090019 "strings"
20 "sync"
21
Justin Yun8effde42017-06-23 19:24:43 +090022 "android/soong/android"
23)
24
25type VndkProperties struct {
26 Vndk struct {
27 // declared as a VNDK or VNDK-SP module. The vendor variant
28 // will be installed in /system instead of /vendor partition.
29 //
Jiyong Park82e2bf32017-08-16 14:05:54 +090030 // `vendor_vailable` must be explicitly set to either true or
31 // false together with `vndk: {enabled: true}`.
Justin Yun8effde42017-06-23 19:24:43 +090032 Enabled *bool
33
34 // declared as a VNDK-SP module, which is a subset of VNDK.
35 //
36 // `vndk: { enabled: true }` must set together.
37 //
38 // All these modules are allowed to link to VNDK-SP or LL-NDK
39 // modules only. Other dependency will cause link-type errors.
40 //
41 // If `support_system_process` is not set or set to false,
42 // the module is VNDK-core and can link to other VNDK-core,
43 // VNDK-SP or LL-NDK modules only.
44 Support_system_process *bool
45 }
46}
47
48type vndkdep struct {
49 Properties VndkProperties
50}
51
52func (vndk *vndkdep) props() []interface{} {
53 return []interface{}{&vndk.Properties}
54}
55
56func (vndk *vndkdep) begin(ctx BaseModuleContext) {}
57
58func (vndk *vndkdep) deps(ctx BaseModuleContext, deps Deps) Deps {
59 return deps
60}
61
62func (vndk *vndkdep) isVndk() bool {
63 return Bool(vndk.Properties.Vndk.Enabled)
64}
65
66func (vndk *vndkdep) isVndkSp() bool {
67 return Bool(vndk.Properties.Vndk.Support_system_process)
68}
69
70func (vndk *vndkdep) typeName() string {
71 if !vndk.isVndk() {
72 return "native:vendor"
73 }
74 if !vndk.isVndkSp() {
75 return "native:vendor:vndk"
76 }
77 return "native:vendor:vndksp"
78}
79
80func (vndk *vndkdep) vndkCheckLinkType(ctx android.ModuleContext, to *Module) {
81 if to.linker == nil {
82 return
83 }
Jiyong Park82e2bf32017-08-16 14:05:54 +090084 if !vndk.isVndk() {
85 // Non-VNDK modules (those installed to /vendor) can't depend on modules marked with
86 // vendor_available: false.
87 violation := false
88 if lib, ok := to.linker.(*llndkStubDecorator); ok && !lib.Properties.Vendor_available {
89 violation = true
90 } else {
91 if _, ok := to.linker.(libraryInterface); ok && to.VendorProperties.Vendor_available != nil && !Bool(to.VendorProperties.Vendor_available) {
92 // Vendor_available == nil && !Bool(Vendor_available) should be okay since
93 // it means a vendor-only library which is a valid dependency for non-VNDK
94 // modules.
95 violation = true
96 }
97 }
98 if violation {
99 ctx.ModuleErrorf("Vendor module that is not VNDK should not link to %q which is marked as `vendor_available: false`", to.Name())
100 }
101 }
Justin Yun8effde42017-06-23 19:24:43 +0900102 if lib, ok := to.linker.(*libraryDecorator); !ok || !lib.shared() {
103 // Check only shared libraries.
104 // Other (static and LL-NDK) libraries are allowed to link.
105 return
106 }
107 if !to.Properties.UseVndk {
108 ctx.ModuleErrorf("(%s) should not link to %q which is not a vendor-available library",
109 vndk.typeName(), to.Name())
110 return
111 }
112 if to.vndkdep == nil {
113 return
114 }
115 if (vndk.isVndk() && !to.vndkdep.isVndk()) || (vndk.isVndkSp() && !to.vndkdep.isVndkSp()) {
116 ctx.ModuleErrorf("(%s) should not link to %q(%s)",
117 vndk.typeName(), to.Name(), to.vndkdep.typeName())
118 return
119 }
120}
Jiyong Parkd5b18a52017-08-03 21:22:50 +0900121
122var (
Jiyong Park82e2bf32017-08-16 14:05:54 +0900123 vndkCoreLibraries []string
124 vndkSpLibraries []string
125 llndkLibraries []string
126 vndkPrivateLibraries []string
127 vndkLibrariesLock sync.Mutex
Jiyong Parkd5b18a52017-08-03 21:22:50 +0900128)
129
130// gather list of vndk-core, vndk-sp, and ll-ndk libs
131func vndkMutator(mctx android.BottomUpMutatorContext) {
132 if m, ok := mctx.Module().(*Module); ok {
Jiyong Park82e2bf32017-08-16 14:05:54 +0900133 if lib, ok := m.linker.(*llndkStubDecorator); ok {
Jiyong Parkd5b18a52017-08-03 21:22:50 +0900134 vndkLibrariesLock.Lock()
135 defer vndkLibrariesLock.Unlock()
136 name := strings.TrimSuffix(m.Name(), llndkLibrarySuffix)
137 if !inList(name, llndkLibraries) {
138 llndkLibraries = append(llndkLibraries, name)
Colin Cross766efbc2017-08-17 14:55:15 -0700139 sort.Strings(llndkLibraries)
Jiyong Parkd5b18a52017-08-03 21:22:50 +0900140 }
Jiyong Park82e2bf32017-08-16 14:05:54 +0900141 if !lib.Properties.Vendor_available {
142 if !inList(name, vndkPrivateLibraries) {
143 vndkPrivateLibraries = append(vndkPrivateLibraries, name)
144 sort.Strings(vndkPrivateLibraries)
145 }
146 }
147 } else {
148 lib, is_lib := m.linker.(*libraryDecorator)
149 prebuilt_lib, is_prebuilt_lib := m.linker.(*prebuiltLibraryLinker)
150 if (is_lib && lib.shared()) || (is_prebuilt_lib && prebuilt_lib.shared()) {
151 name := strings.TrimPrefix(m.Name(), "prebuilt_")
152 if m.vndkdep.isVndk() {
153 vndkLibrariesLock.Lock()
154 defer vndkLibrariesLock.Unlock()
155 if m.vndkdep.isVndkSp() {
156 if !inList(name, vndkSpLibraries) {
157 vndkSpLibraries = append(vndkSpLibraries, name)
158 sort.Strings(vndkSpLibraries)
159 }
160 } else {
161 if !inList(name, vndkCoreLibraries) {
162 vndkCoreLibraries = append(vndkCoreLibraries, name)
163 sort.Strings(vndkCoreLibraries)
164 }
Jiyong Parkd5b18a52017-08-03 21:22:50 +0900165 }
Jiyong Park82e2bf32017-08-16 14:05:54 +0900166 if !Bool(m.VendorProperties.Vendor_available) {
167 if !inList(name, vndkPrivateLibraries) {
168 vndkPrivateLibraries = append(vndkPrivateLibraries, name)
169 sort.Strings(vndkPrivateLibraries)
170 }
Jiyong Parkd5b18a52017-08-03 21:22:50 +0900171 }
172 }
173 }
174 }
Jiyong Park82e2bf32017-08-16 14:05:54 +0900175
Jiyong Parkd5b18a52017-08-03 21:22:50 +0900176 }
177}