blob: 5c15e8e8c17205f7d5e6db6d2e904fed9ba3bde9 [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 "fmt"
19 "io"
20 "path/filepath"
21 "strings"
22
23 "android/soong/android"
24 "android/soong/cc"
Jiyong Park09d77522019-11-18 11:16:27 +090025
26 "github.com/google/blueprint/proptools"
27)
28
29func (a *apexBundle) AndroidMk() android.AndroidMkData {
30 if a.properties.HideFromMake {
31 return android.AndroidMkData{
32 Disabled: true,
33 }
34 }
35 writers := []android.AndroidMkData{}
36 writers = append(writers, a.androidMkForType())
37 return android.AndroidMkData{
38 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
39 for _, data := range writers {
40 data.Custom(w, name, prefix, moduleDir, data)
41 }
42 }}
43}
44
Jiyong Parkdb334862020-02-05 17:19:28 +090045func (a *apexBundle) androidMkForFiles(w io.Writer, apexBundleName, apexName, moduleDir string) []string {
46 // apexBundleName comes from the 'name' property; apexName comes from 'apex_name' property.
47 // An apex is installed to /system/apex/<apexBundleName> and is activated at /apex/<apexName>
48 // In many cases, the two names are the same, but could be different in general.
49
Jiyong Park09d77522019-11-18 11:16:27 +090050 moduleNames := []string{}
51 apexType := a.properties.ApexType
52 // To avoid creating duplicate build rules, run this function only when primaryApexType is true
53 // to install symbol files in $(PRODUCT_OUT}/apex.
54 // And if apexType is flattened, run this function to install files in $(PRODUCT_OUT}/system/apex.
55 if !a.primaryApexType && apexType != flattenedApex {
56 return moduleNames
57 }
58
Jiyong Parkdb334862020-02-05 17:19:28 +090059 // b/140136207. When there are overriding APEXes for a VNDK APEX, the symbols file for the overridden
60 // APEX and the overriding APEX will have the same installation paths at /apex/com.android.vndk.v<ver>
61 // as their apexName will be the same. To avoid the path conflicts, skip installing the symbol files
62 // for the overriding VNDK APEXes.
63 symbolFilesNotNeeded := a.vndkApex && len(a.overridableProperties.Overrides) > 0
64 if symbolFilesNotNeeded && apexType != flattenedApex {
65 return moduleNames
66 }
67
Jiyong Park7cd10e32020-01-14 09:22:18 +090068 var postInstallCommands []string
69 for _, fi := range a.filesInfo {
70 if a.linkToSystemLib && fi.transitiveDep && fi.AvailableToPlatform() {
71 // TODO(jiyong): pathOnDevice should come from fi.module, not being calculated here
72 linkTarget := filepath.Join("/system", fi.Path())
Jiyong Parkdb334862020-02-05 17:19:28 +090073 linkPath := filepath.Join(a.installDir.ToMakePath().String(), apexBundleName, fi.Path())
Jiyong Park7cd10e32020-01-14 09:22:18 +090074 mkdirCmd := "mkdir -p " + filepath.Dir(linkPath)
75 linkCmd := "ln -sfn " + linkTarget + " " + linkPath
76 postInstallCommands = append(postInstallCommands, mkdirCmd, linkCmd)
77 }
78 }
Jiyong Park7cd10e32020-01-14 09:22:18 +090079
Jiyong Park09d77522019-11-18 11:16:27 +090080 for _, fi := range a.filesInfo {
81 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
82 continue
83 }
84
Jiyong Park7cd10e32020-01-14 09:22:18 +090085 linkToSystemLib := a.linkToSystemLib && fi.transitiveDep && fi.AvailableToPlatform()
86
87 var moduleName string
88 if linkToSystemLib {
89 moduleName = fi.moduleName
90 } else {
Jiyong Parkdb334862020-02-05 17:19:28 +090091 moduleName = fi.moduleName + "." + apexBundleName + a.suffix
Jiyong Park7cd10e32020-01-14 09:22:18 +090092 }
93
94 if !android.InList(moduleName, moduleNames) {
95 moduleNames = append(moduleNames, moduleName)
96 }
97
98 if linkToSystemLib {
99 // No need to copy the file since it's linked to the system file
100 continue
Jiyong Park09d77522019-11-18 11:16:27 +0900101 }
102
103 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
Jiyong Park1833cef2019-12-13 13:28:36 +0900104 if fi.moduleDir != "" {
105 fmt.Fprintln(w, "LOCAL_PATH :=", fi.moduleDir)
106 } else {
107 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
108 }
Jiyong Park7cd10e32020-01-14 09:22:18 +0900109 fmt.Fprintln(w, "LOCAL_MODULE :=", moduleName)
Jiyong Park09d77522019-11-18 11:16:27 +0900110 // /apex/<apex_name>/{lib|framework|...}
111 pathWhenActivated := filepath.Join("$(PRODUCT_OUT)", "apex", apexName, fi.installDir)
112 if apexType == flattenedApex {
113 // /system/apex/<name>/{lib|framework|...}
114 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join(a.installDir.ToMakePath().String(),
Jiyong Parkdb334862020-02-05 17:19:28 +0900115 apexBundleName, fi.installDir))
116 if a.primaryApexType && !symbolFilesNotNeeded {
Jiyong Park09d77522019-11-18 11:16:27 +0900117 fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated)
118 }
119 if len(fi.symlinks) > 0 {
120 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
121 }
122
Bob Badoura75b0572020-02-18 20:21:55 -0800123 if fi.module != nil && len(fi.module.NoticeFiles()) > 0 {
124 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", strings.Join(fi.module.NoticeFiles().Strings(), " "))
Jiyong Park09d77522019-11-18 11:16:27 +0900125 }
126 } else {
127 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
Jiyong Park19972c72020-01-28 20:05:29 +0900128
129 // For non-flattend APEXes, the merged notice file is attached to the APEX itself.
130 // We don't need to have notice file for the individual modules in it. Otherwise,
131 // we will have duplicated notice entries.
132 fmt.Fprintln(w, "LOCAL_NO_NOTICE_FILE := true")
Jiyong Park09d77522019-11-18 11:16:27 +0900133 }
134 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
135 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
136 if fi.module != nil {
137 archStr := fi.module.Target().Arch.ArchType.String()
138 host := false
139 switch fi.module.Target().Os.Class {
140 case android.Host:
141 if fi.module.Target().Arch.ArchType != android.Common {
142 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
143 }
144 host = true
145 case android.HostCross:
146 if fi.module.Target().Arch.ArchType != android.Common {
147 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
148 }
149 host = true
150 case android.Device:
151 if fi.module.Target().Arch.ArchType != android.Common {
152 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
153 }
154 }
155 if host {
156 makeOs := fi.module.Target().Os.String()
157 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
158 makeOs = "linux"
159 }
160 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
161 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
162 }
163 }
Jiyong Park618922e2020-01-08 13:35:43 +0900164 if fi.jacocoReportClassesFile != nil {
165 fmt.Fprintln(w, "LOCAL_SOONG_JACOCO_REPORT_CLASSES_JAR :=", fi.jacocoReportClassesFile.String())
166 }
Jiyong Park09d77522019-11-18 11:16:27 +0900167 if fi.class == javaSharedLib {
Jooyung Han58f26ab2019-12-18 15:34:32 +0900168 javaModule := fi.module.(javaLibrary)
Jiyong Park09d77522019-11-18 11:16:27 +0900169 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
170 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
171 // we will have foo.jar.jar
172 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
173 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
174 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
175 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
176 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
177 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
Jiyong Park618922e2020-01-08 13:35:43 +0900178 } else if fi.class == app {
Colin Cross503c1d02020-01-28 14:00:53 -0800179 fmt.Fprintln(w, "LOCAL_CERTIFICATE :=", fi.certificate.AndroidMkString())
Jiyong Park618922e2020-01-08 13:35:43 +0900180 // soong_app_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .apk Therefore
181 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
182 // we will have foo.apk.apk
183 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".apk"))
184 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_app_prebuilt.mk")
Jiyong Park09d77522019-11-18 11:16:27 +0900185 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable || fi.class == nativeTest {
186 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
187 if cc, ok := fi.module.(*cc.Module); ok {
188 if cc.UnstrippedOutputFile() != nil {
189 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
190 }
191 cc.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w)
192 if cc.CoverageOutputFile().Valid() {
193 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", cc.CoverageOutputFile().String())
194 }
195 }
196 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
197 } else {
198 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Jiyong Park7aa3f762020-01-28 16:51:34 +0900199 if fi.builtFile == a.manifestPbOut && apexType == flattenedApex {
Jooyung Han75de2612020-01-24 02:02:45 +0900200 if a.primaryApexType {
201 // Make apex_manifest.pb module for this APEX to override all other
202 // modules in the APEXes being overridden by this APEX
203 var patterns []string
204 for _, o := range a.overridableProperties.Overrides {
205 patterns = append(patterns, "%."+o+a.suffix)
206 }
207 fmt.Fprintln(w, "LOCAL_OVERRIDES_MODULES :=", strings.Join(patterns, " "))
Jiyong Park0abc1b42020-01-09 17:43:39 +0900208
Jiyong Park7aa3f762020-01-28 16:51:34 +0900209 if len(a.compatSymlinks) > 0 {
Jooyung Han75de2612020-01-24 02:02:45 +0900210 // For flattened apexes, compat symlinks are attached to apex_manifest.json which is guaranteed for every apex
211 postInstallCommands = append(postInstallCommands, a.compatSymlinks...)
212 }
213 }
214 if len(postInstallCommands) > 0 {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900215 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", strings.Join(postInstallCommands, " && "))
Jiyong Park0abc1b42020-01-09 17:43:39 +0900216 }
Jiyong Park09d77522019-11-18 11:16:27 +0900217 }
218 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
219 }
Jiyong Park31af2672020-02-11 09:36:25 +0900220
221 // m <module_name> will build <module_name>.<apex_name> as well.
222 if fi.moduleName != moduleName && a.primaryApexType {
223 fmt.Fprintln(w, ".PHONY: "+fi.moduleName)
224 fmt.Fprintln(w, fi.moduleName+": "+moduleName)
225 }
Jiyong Park09d77522019-11-18 11:16:27 +0900226 }
227 return moduleNames
228}
229
Jiyong Park7afd1072019-12-30 16:56:33 +0900230func (a *apexBundle) writeRequiredModules(w io.Writer) {
231 var required []string
232 var targetRequired []string
233 var hostRequired []string
234 for _, fi := range a.filesInfo {
235 required = append(required, fi.requiredModuleNames...)
236 targetRequired = append(targetRequired, fi.targetRequiredModuleNames...)
237 hostRequired = append(hostRequired, fi.hostRequiredModuleNames...)
238 }
239
240 if len(required) > 0 {
241 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(required, " "))
242 }
243 if len(targetRequired) > 0 {
244 fmt.Fprintln(w, "LOCAL_TARGET_REQUIRED_MODULES +=", strings.Join(targetRequired, " "))
245 }
246 if len(hostRequired) > 0 {
247 fmt.Fprintln(w, "LOCAL_HOST_REQUIRED_MODULES +=", strings.Join(hostRequired, " "))
248 }
249}
250
Jiyong Park09d77522019-11-18 11:16:27 +0900251func (a *apexBundle) androidMkForType() android.AndroidMkData {
252 return android.AndroidMkData{
253 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
254 moduleNames := []string{}
255 apexType := a.properties.ApexType
256 if a.installable() {
257 apexName := proptools.StringDefault(a.properties.Apex_name, name)
Jiyong Parkdb334862020-02-05 17:19:28 +0900258 moduleNames = a.androidMkForFiles(w, name, apexName, moduleDir)
Jiyong Park09d77522019-11-18 11:16:27 +0900259 }
260
261 if apexType == flattenedApex {
262 // Only image APEXes can be flattened.
263 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
264 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
265 fmt.Fprintln(w, "LOCAL_MODULE :=", name+a.suffix)
266 if len(moduleNames) > 0 {
267 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
268 }
Jiyong Park7afd1072019-12-30 16:56:33 +0900269 a.writeRequiredModules(w)
Jiyong Park09d77522019-11-18 11:16:27 +0900270 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Jiyong Park09d77522019-11-18 11:16:27 +0900271
272 } else {
273 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
274 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
275 fmt.Fprintln(w, "LOCAL_MODULE :=", name+a.suffix)
276 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
277 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFile.String())
278 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", a.installDir.ToMakePath().String())
279 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
280 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -0800281 fmt.Fprintln(w, "LOCAL_OVERRIDES_MODULES :=", strings.Join(a.overridableProperties.Overrides, " "))
Jiyong Park09d77522019-11-18 11:16:27 +0900282 if len(moduleNames) > 0 {
283 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
284 }
Jiyong Park956305c2020-01-09 12:32:06 +0900285 if len(a.requiredDeps) > 0 {
286 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.requiredDeps, " "))
Jiyong Park09d77522019-11-18 11:16:27 +0900287 }
Jiyong Park7afd1072019-12-30 16:56:33 +0900288 a.writeRequiredModules(w)
Jiyong Park09d77522019-11-18 11:16:27 +0900289 var postInstallCommands []string
290 if a.prebuiltFileToDelete != "" {
291 postInstallCommands = append(postInstallCommands, "rm -rf "+
292 filepath.Join(a.installDir.ToMakePath().String(), a.prebuiltFileToDelete))
293 }
294 // For unflattened apexes, compat symlinks are attached to apex package itself as LOCAL_POST_INSTALL_CMD
295 postInstallCommands = append(postInstallCommands, a.compatSymlinks...)
296 if len(postInstallCommands) > 0 {
297 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", strings.Join(postInstallCommands, " && "))
298 }
Jiyong Park19972c72020-01-28 20:05:29 +0900299
300 if a.mergedNotices.Merged.Valid() {
301 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", a.mergedNotices.Merged.Path().String())
302 }
303
Jiyong Park09d77522019-11-18 11:16:27 +0900304 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
305
306 if apexType == imageApex {
307 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
308 }
Jiyong Park3a1602e2020-01-14 14:39:19 +0900309
310 if a.installedFilesFile != nil {
Colin Cross1c85e8e2020-02-26 16:55:51 -0800311 goal := "checkbuild"
Jiyong Park3a1602e2020-01-14 14:39:19 +0900312 distFile := name + "-installed-files.txt"
313 fmt.Fprintln(w, ".PHONY:", goal)
314 fmt.Fprintf(w, "$(call dist-for-goals,%s,%s:%s)\n",
315 goal, a.installedFilesFile.String(), distFile)
316 }
Jiyong Park09d77522019-11-18 11:16:27 +0900317 }
318 }}
319}