blob: 811bc426edabf1d3dd3b245ff8cfe4cce33d24b5 [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"
Jaewoong Jung37ca4a12020-03-26 14:01:48 -070025 "android/soong/java"
Jiyong Park09d77522019-11-18 11:16:27 +090026
27 "github.com/google/blueprint/proptools"
28)
29
30func (a *apexBundle) AndroidMk() android.AndroidMkData {
31 if a.properties.HideFromMake {
32 return android.AndroidMkData{
33 Disabled: true,
34 }
35 }
36 writers := []android.AndroidMkData{}
37 writers = append(writers, a.androidMkForType())
38 return android.AndroidMkData{
39 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
40 for _, data := range writers {
41 data.Custom(w, name, prefix, moduleDir, data)
42 }
43 }}
44}
45
Jiyong Parkdb334862020-02-05 17:19:28 +090046func (a *apexBundle) androidMkForFiles(w io.Writer, apexBundleName, apexName, moduleDir string) []string {
47 // apexBundleName comes from the 'name' property; apexName comes from 'apex_name' property.
48 // An apex is installed to /system/apex/<apexBundleName> and is activated at /apex/<apexName>
49 // In many cases, the two names are the same, but could be different in general.
50
Jiyong Park09d77522019-11-18 11:16:27 +090051 moduleNames := []string{}
52 apexType := a.properties.ApexType
53 // To avoid creating duplicate build rules, run this function only when primaryApexType is true
54 // to install symbol files in $(PRODUCT_OUT}/apex.
55 // And if apexType is flattened, run this function to install files in $(PRODUCT_OUT}/system/apex.
56 if !a.primaryApexType && apexType != flattenedApex {
57 return moduleNames
58 }
59
Jiyong Parkdb334862020-02-05 17:19:28 +090060 // b/140136207. When there are overriding APEXes for a VNDK APEX, the symbols file for the overridden
61 // APEX and the overriding APEX will have the same installation paths at /apex/com.android.vndk.v<ver>
62 // as their apexName will be the same. To avoid the path conflicts, skip installing the symbol files
63 // for the overriding VNDK APEXes.
64 symbolFilesNotNeeded := a.vndkApex && len(a.overridableProperties.Overrides) > 0
65 if symbolFilesNotNeeded && apexType != flattenedApex {
66 return moduleNames
67 }
68
Jiyong Park7cd10e32020-01-14 09:22:18 +090069 var postInstallCommands []string
70 for _, fi := range a.filesInfo {
71 if a.linkToSystemLib && fi.transitiveDep && fi.AvailableToPlatform() {
72 // TODO(jiyong): pathOnDevice should come from fi.module, not being calculated here
73 linkTarget := filepath.Join("/system", fi.Path())
Jiyong Parkdb334862020-02-05 17:19:28 +090074 linkPath := filepath.Join(a.installDir.ToMakePath().String(), apexBundleName, fi.Path())
Jiyong Park7cd10e32020-01-14 09:22:18 +090075 mkdirCmd := "mkdir -p " + filepath.Dir(linkPath)
76 linkCmd := "ln -sfn " + linkTarget + " " + linkPath
77 postInstallCommands = append(postInstallCommands, mkdirCmd, linkCmd)
78 }
79 }
Jiyong Park7cd10e32020-01-14 09:22:18 +090080
Jiyong Park09d77522019-11-18 11:16:27 +090081 for _, fi := range a.filesInfo {
Sasha Smundakc4f0ff12020-05-27 16:36:07 -070082 if ccMod, ok := fi.module.(*cc.Module); ok && ccMod.Properties.HideFromMake {
Jiyong Park09d77522019-11-18 11:16:27 +090083 continue
84 }
85
Jiyong Park7cd10e32020-01-14 09:22:18 +090086 linkToSystemLib := a.linkToSystemLib && fi.transitiveDep && fi.AvailableToPlatform()
87
88 var moduleName string
89 if linkToSystemLib {
90 moduleName = fi.moduleName
91 } else {
Jiyong Parkdb334862020-02-05 17:19:28 +090092 moduleName = fi.moduleName + "." + apexBundleName + a.suffix
Jiyong Park7cd10e32020-01-14 09:22:18 +090093 }
94
95 if !android.InList(moduleName, moduleNames) {
96 moduleNames = append(moduleNames, moduleName)
97 }
98
99 if linkToSystemLib {
100 // No need to copy the file since it's linked to the system file
101 continue
Jiyong Park09d77522019-11-18 11:16:27 +0900102 }
103
104 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
Jiyong Park1833cef2019-12-13 13:28:36 +0900105 if fi.moduleDir != "" {
106 fmt.Fprintln(w, "LOCAL_PATH :=", fi.moduleDir)
107 } else {
108 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
109 }
Jiyong Park7cd10e32020-01-14 09:22:18 +0900110 fmt.Fprintln(w, "LOCAL_MODULE :=", moduleName)
Jiyong Park09d77522019-11-18 11:16:27 +0900111 // /apex/<apex_name>/{lib|framework|...}
112 pathWhenActivated := filepath.Join("$(PRODUCT_OUT)", "apex", apexName, fi.installDir)
113 if apexType == flattenedApex {
114 // /system/apex/<name>/{lib|framework|...}
115 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join(a.installDir.ToMakePath().String(),
Jiyong Parkdb334862020-02-05 17:19:28 +0900116 apexBundleName, fi.installDir))
117 if a.primaryApexType && !symbolFilesNotNeeded {
Jiyong Park09d77522019-11-18 11:16:27 +0900118 fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated)
119 }
120 if len(fi.symlinks) > 0 {
121 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
122 }
123
124 if fi.module != nil && fi.module.NoticeFile().Valid() {
125 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", fi.module.NoticeFile().Path().String())
126 }
127 } else {
128 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
Jiyong Park19972c72020-01-28 20:05:29 +0900129
130 // For non-flattend APEXes, the merged notice file is attached to the APEX itself.
131 // We don't need to have notice file for the individual modules in it. Otherwise,
132 // we will have duplicated notice entries.
133 fmt.Fprintln(w, "LOCAL_NO_NOTICE_FILE := true")
Jiyong Park09d77522019-11-18 11:16:27 +0900134 }
135 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
136 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
137 if fi.module != nil {
138 archStr := fi.module.Target().Arch.ArchType.String()
139 host := false
140 switch fi.module.Target().Os.Class {
141 case android.Host:
142 if fi.module.Target().Arch.ArchType != android.Common {
143 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
144 }
145 host = true
146 case android.HostCross:
147 if fi.module.Target().Arch.ArchType != android.Common {
148 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
149 }
150 host = true
151 case android.Device:
152 if fi.module.Target().Arch.ArchType != android.Common {
153 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
154 }
155 }
156 if host {
157 makeOs := fi.module.Target().Os.String()
158 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
159 makeOs = "linux"
160 }
161 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
162 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
163 }
164 }
Jiyong Park618922e2020-01-08 13:35:43 +0900165 if fi.jacocoReportClassesFile != nil {
166 fmt.Fprintln(w, "LOCAL_SOONG_JACOCO_REPORT_CLASSES_JAR :=", fi.jacocoReportClassesFile.String())
167 }
Sasha Smundakc4f0ff12020-05-27 16:36:07 -0700168 switch fi.class {
169 case javaSharedLib:
Jiyong Park09d77522019-11-18 11:16:27 +0900170 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
171 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
172 // we will have foo.jar.jar
Jiyong Parkcbe50c72020-05-29 21:29:20 +0900173 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.Stem(), ".jar"))
Jiyong Parke26a63e2020-06-11 00:35:03 +0900174 if javaModule, ok := fi.module.(java.Dependency); ok {
175 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
176 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
177 } else {
178 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", fi.builtFile.String())
179 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", fi.builtFile.String())
180 }
Jiyong Park09d77522019-11-18 11:16:27 +0900181 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
182 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
183 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
Sasha Smundakc4f0ff12020-05-27 16:36:07 -0700184 case app:
Colin Cross503c1d02020-01-28 14:00:53 -0800185 fmt.Fprintln(w, "LOCAL_CERTIFICATE :=", fi.certificate.AndroidMkString())
Jiyong Park618922e2020-01-08 13:35:43 +0900186 // soong_app_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .apk Therefore
187 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
188 // we will have foo.apk.apk
Jiyong Parkcbe50c72020-05-29 21:29:20 +0900189 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.Stem(), ".apk"))
Jaewoong Jung37ca4a12020-03-26 14:01:48 -0700190 if app, ok := fi.module.(*java.AndroidApp); ok && len(app.JniCoverageOutputs()) > 0 {
191 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", strings.Join(app.JniCoverageOutputs().Strings(), " "))
192 }
Jiyong Park618922e2020-01-08 13:35:43 +0900193 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_app_prebuilt.mk")
Sasha Smundakc4f0ff12020-05-27 16:36:07 -0700194 case appSet:
195 as, ok := fi.module.(*java.AndroidAppSet)
196 if !ok {
197 panic(fmt.Sprintf("Expected %s to be AndroidAppSet", fi.module))
198 }
199 fmt.Fprintln(w, "LOCAL_APK_SET_MASTER_FILE :=", as.MasterFile())
200 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_android_app_set.mk")
201 case nativeSharedLib, nativeExecutable, nativeTest:
Jiyong Parkcbe50c72020-05-29 21:29:20 +0900202 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.Stem())
Sasha Smundakc4f0ff12020-05-27 16:36:07 -0700203 if ccMod, ok := fi.module.(*cc.Module); ok {
204 if ccMod.UnstrippedOutputFile() != nil {
205 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", ccMod.UnstrippedOutputFile().String())
Jiyong Park09d77522019-11-18 11:16:27 +0900206 }
Sasha Smundakc4f0ff12020-05-27 16:36:07 -0700207 ccMod.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w)
208 if ccMod.CoverageOutputFile().Valid() {
209 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", ccMod.CoverageOutputFile().String())
Jiyong Park09d77522019-11-18 11:16:27 +0900210 }
211 }
212 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
Sasha Smundakc4f0ff12020-05-27 16:36:07 -0700213 default:
Jiyong Parkcbe50c72020-05-29 21:29:20 +0900214 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.Stem())
Jiyong Park7aa3f762020-01-28 16:51:34 +0900215 if fi.builtFile == a.manifestPbOut && apexType == flattenedApex {
Jooyung Han75de2612020-01-24 02:02:45 +0900216 if a.primaryApexType {
217 // Make apex_manifest.pb module for this APEX to override all other
218 // modules in the APEXes being overridden by this APEX
219 var patterns []string
220 for _, o := range a.overridableProperties.Overrides {
221 patterns = append(patterns, "%."+o+a.suffix)
222 }
223 fmt.Fprintln(w, "LOCAL_OVERRIDES_MODULES :=", strings.Join(patterns, " "))
Jiyong Park0abc1b42020-01-09 17:43:39 +0900224
Jiyong Park7aa3f762020-01-28 16:51:34 +0900225 if len(a.compatSymlinks) > 0 {
Jooyung Han75de2612020-01-24 02:02:45 +0900226 // For flattened apexes, compat symlinks are attached to apex_manifest.json which is guaranteed for every apex
227 postInstallCommands = append(postInstallCommands, a.compatSymlinks...)
228 }
229 }
230 if len(postInstallCommands) > 0 {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900231 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", strings.Join(postInstallCommands, " && "))
Jiyong Park0abc1b42020-01-09 17:43:39 +0900232 }
Jiyong Park09d77522019-11-18 11:16:27 +0900233 }
234 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
235 }
Jiyong Park31af2672020-02-11 09:36:25 +0900236
237 // m <module_name> will build <module_name>.<apex_name> as well.
238 if fi.moduleName != moduleName && a.primaryApexType {
239 fmt.Fprintln(w, ".PHONY: "+fi.moduleName)
240 fmt.Fprintln(w, fi.moduleName+": "+moduleName)
241 }
Jiyong Park09d77522019-11-18 11:16:27 +0900242 }
243 return moduleNames
244}
245
Jiyong Park7afd1072019-12-30 16:56:33 +0900246func (a *apexBundle) writeRequiredModules(w io.Writer) {
247 var required []string
248 var targetRequired []string
249 var hostRequired []string
250 for _, fi := range a.filesInfo {
251 required = append(required, fi.requiredModuleNames...)
252 targetRequired = append(targetRequired, fi.targetRequiredModuleNames...)
253 hostRequired = append(hostRequired, fi.hostRequiredModuleNames...)
254 }
255
256 if len(required) > 0 {
257 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(required, " "))
258 }
259 if len(targetRequired) > 0 {
260 fmt.Fprintln(w, "LOCAL_TARGET_REQUIRED_MODULES +=", strings.Join(targetRequired, " "))
261 }
262 if len(hostRequired) > 0 {
263 fmt.Fprintln(w, "LOCAL_HOST_REQUIRED_MODULES +=", strings.Join(hostRequired, " "))
264 }
265}
266
Jiyong Park09d77522019-11-18 11:16:27 +0900267func (a *apexBundle) androidMkForType() android.AndroidMkData {
268 return android.AndroidMkData{
269 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
270 moduleNames := []string{}
271 apexType := a.properties.ApexType
272 if a.installable() {
273 apexName := proptools.StringDefault(a.properties.Apex_name, name)
Jiyong Parkdb334862020-02-05 17:19:28 +0900274 moduleNames = a.androidMkForFiles(w, name, apexName, moduleDir)
Jiyong Park09d77522019-11-18 11:16:27 +0900275 }
276
277 if apexType == flattenedApex {
278 // Only image APEXes can be flattened.
279 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
280 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
281 fmt.Fprintln(w, "LOCAL_MODULE :=", name+a.suffix)
282 if len(moduleNames) > 0 {
283 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
284 }
Jiyong Park7afd1072019-12-30 16:56:33 +0900285 a.writeRequiredModules(w)
Jiyong Park09d77522019-11-18 11:16:27 +0900286 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Jiyong Park09d77522019-11-18 11:16:27 +0900287
288 } else {
289 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
290 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
291 fmt.Fprintln(w, "LOCAL_MODULE :=", name+a.suffix)
292 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
293 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFile.String())
294 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", a.installDir.ToMakePath().String())
295 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
296 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -0800297 fmt.Fprintln(w, "LOCAL_OVERRIDES_MODULES :=", strings.Join(a.overridableProperties.Overrides, " "))
Jiyong Park09d77522019-11-18 11:16:27 +0900298 if len(moduleNames) > 0 {
299 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
300 }
Jiyong Park956305c2020-01-09 12:32:06 +0900301 if len(a.requiredDeps) > 0 {
302 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.requiredDeps, " "))
Jiyong Park09d77522019-11-18 11:16:27 +0900303 }
Jiyong Park7afd1072019-12-30 16:56:33 +0900304 a.writeRequiredModules(w)
Jiyong Park09d77522019-11-18 11:16:27 +0900305 var postInstallCommands []string
306 if a.prebuiltFileToDelete != "" {
307 postInstallCommands = append(postInstallCommands, "rm -rf "+
308 filepath.Join(a.installDir.ToMakePath().String(), a.prebuiltFileToDelete))
309 }
310 // For unflattened apexes, compat symlinks are attached to apex package itself as LOCAL_POST_INSTALL_CMD
311 postInstallCommands = append(postInstallCommands, a.compatSymlinks...)
312 if len(postInstallCommands) > 0 {
313 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", strings.Join(postInstallCommands, " && "))
314 }
Jiyong Park19972c72020-01-28 20:05:29 +0900315
316 if a.mergedNotices.Merged.Valid() {
317 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", a.mergedNotices.Merged.Path().String())
318 }
319
Jiyong Park09d77522019-11-18 11:16:27 +0900320 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
321
322 if apexType == imageApex {
323 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
324 }
Jiyong Park3a1602e2020-01-14 14:39:19 +0900325
326 if a.installedFilesFile != nil {
Colin Crosscd33fa22020-02-26 16:55:51 -0800327 goal := "checkbuild"
Jiyong Park3a1602e2020-01-14 14:39:19 +0900328 distFile := name + "-installed-files.txt"
329 fmt.Fprintln(w, ".PHONY:", goal)
330 fmt.Fprintf(w, "$(call dist-for-goals,%s,%s:%s)\n",
331 goal, a.installedFilesFile.String(), distFile)
332 }
Jiyong Park09d77522019-11-18 11:16:27 +0900333 }
334 }}
335}