blob: 91a405c5885063a9c50d7d452f4a0342cd2e619c [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 Jung87a33e72020-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
Liz Kammer5bd365f2020-05-27 15:15:11 -070081 seenDataOutPaths := make(map[string]bool)
82
Jiyong Park09d77522019-11-18 11:16:27 +090083 for _, fi := range a.filesInfo {
Sasha Smundak18d98bc2020-05-27 16:36:07 -070084 if ccMod, ok := fi.module.(*cc.Module); ok && ccMod.Properties.HideFromMake {
Jiyong Park09d77522019-11-18 11:16:27 +090085 continue
86 }
87
Jiyong Park7cd10e32020-01-14 09:22:18 +090088 linkToSystemLib := a.linkToSystemLib && fi.transitiveDep && fi.AvailableToPlatform()
89
90 var moduleName string
91 if linkToSystemLib {
92 moduleName = fi.moduleName
93 } else {
Jiyong Parkdb334862020-02-05 17:19:28 +090094 moduleName = fi.moduleName + "." + apexBundleName + a.suffix
Jiyong Park7cd10e32020-01-14 09:22:18 +090095 }
96
97 if !android.InList(moduleName, moduleNames) {
98 moduleNames = append(moduleNames, moduleName)
99 }
100
101 if linkToSystemLib {
102 // No need to copy the file since it's linked to the system file
103 continue
Jiyong Park09d77522019-11-18 11:16:27 +0900104 }
105
106 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
Jiyong Park1833cef2019-12-13 13:28:36 +0900107 if fi.moduleDir != "" {
108 fmt.Fprintln(w, "LOCAL_PATH :=", fi.moduleDir)
109 } else {
110 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
111 }
Jiyong Park7cd10e32020-01-14 09:22:18 +0900112 fmt.Fprintln(w, "LOCAL_MODULE :=", moduleName)
Jiyong Park09d77522019-11-18 11:16:27 +0900113 // /apex/<apex_name>/{lib|framework|...}
114 pathWhenActivated := filepath.Join("$(PRODUCT_OUT)", "apex", apexName, fi.installDir)
115 if apexType == flattenedApex {
116 // /system/apex/<name>/{lib|framework|...}
Liz Kammer5bd365f2020-05-27 15:15:11 -0700117 modulePath := filepath.Join(a.installDir.ToMakePath().String(), apexBundleName, fi.installDir)
118 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", modulePath)
Jiyong Parkdb334862020-02-05 17:19:28 +0900119 if a.primaryApexType && !symbolFilesNotNeeded {
Jiyong Park09d77522019-11-18 11:16:27 +0900120 fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated)
121 }
122 if len(fi.symlinks) > 0 {
123 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
124 }
Liz Kammer5bd365f2020-05-27 15:15:11 -0700125 newDataPaths := []android.Path{}
126 for _, path := range fi.dataPaths {
127 dataOutPath := modulePath + ":" + path.Rel()
128 if ok := seenDataOutPaths[dataOutPath]; !ok {
129 newDataPaths = append(newDataPaths, path)
130 seenDataOutPaths[dataOutPath] = true
131 }
132 }
133 if len(newDataPaths) > 0 {
134 fmt.Fprintln(w, "LOCAL_TEST_DATA :=", strings.Join(cc.AndroidMkDataPaths(newDataPaths), " "))
Liz Kammer1c14a212020-05-12 15:26:55 -0700135 }
Jiyong Park09d77522019-11-18 11:16:27 +0900136
Bob Badoura75b0572020-02-18 20:21:55 -0800137 if fi.module != nil && len(fi.module.NoticeFiles()) > 0 {
138 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", strings.Join(fi.module.NoticeFiles().Strings(), " "))
Jiyong Park09d77522019-11-18 11:16:27 +0900139 }
140 } else {
141 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
Jiyong Park19972c72020-01-28 20:05:29 +0900142
143 // For non-flattend APEXes, the merged notice file is attached to the APEX itself.
144 // We don't need to have notice file for the individual modules in it. Otherwise,
145 // we will have duplicated notice entries.
146 fmt.Fprintln(w, "LOCAL_NO_NOTICE_FILE := true")
Jiyong Park09d77522019-11-18 11:16:27 +0900147 }
148 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
149 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
150 if fi.module != nil {
151 archStr := fi.module.Target().Arch.ArchType.String()
152 host := false
153 switch fi.module.Target().Os.Class {
154 case android.Host:
155 if fi.module.Target().Arch.ArchType != android.Common {
156 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
157 }
158 host = true
159 case android.HostCross:
160 if fi.module.Target().Arch.ArchType != android.Common {
161 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
162 }
163 host = true
164 case android.Device:
165 if fi.module.Target().Arch.ArchType != android.Common {
166 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
167 }
168 }
169 if host {
170 makeOs := fi.module.Target().Os.String()
171 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
172 makeOs = "linux"
173 }
174 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
175 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
176 }
177 }
Jiyong Park618922e2020-01-08 13:35:43 +0900178 if fi.jacocoReportClassesFile != nil {
179 fmt.Fprintln(w, "LOCAL_SOONG_JACOCO_REPORT_CLASSES_JAR :=", fi.jacocoReportClassesFile.String())
180 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700181 switch fi.class {
182 case javaSharedLib:
Jiyong Park09d77522019-11-18 11:16:27 +0900183 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
184 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
185 // we will have foo.jar.jar
Jiyong Parkf1493cc2020-05-29 21:29:20 +0900186 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.Stem(), ".jar"))
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900187 if javaModule, ok := fi.module.(java.Dependency); ok {
188 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
189 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
190 } else {
191 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", fi.builtFile.String())
192 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", fi.builtFile.String())
193 }
Jiyong Park09d77522019-11-18 11:16:27 +0900194 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
195 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
196 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700197 case app:
Colin Cross503c1d02020-01-28 14:00:53 -0800198 fmt.Fprintln(w, "LOCAL_CERTIFICATE :=", fi.certificate.AndroidMkString())
Jiyong Park618922e2020-01-08 13:35:43 +0900199 // soong_app_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .apk Therefore
200 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
201 // we will have foo.apk.apk
Jiyong Parkf1493cc2020-05-29 21:29:20 +0900202 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.Stem(), ".apk"))
Jaewoong Jung87a33e72020-03-26 14:01:48 -0700203 if app, ok := fi.module.(*java.AndroidApp); ok && len(app.JniCoverageOutputs()) > 0 {
204 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", strings.Join(app.JniCoverageOutputs().Strings(), " "))
205 }
Jiyong Park618922e2020-01-08 13:35:43 +0900206 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_app_prebuilt.mk")
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700207 case appSet:
208 as, ok := fi.module.(*java.AndroidAppSet)
209 if !ok {
210 panic(fmt.Sprintf("Expected %s to be AndroidAppSet", fi.module))
211 }
212 fmt.Fprintln(w, "LOCAL_APK_SET_MASTER_FILE :=", as.MasterFile())
213 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_android_app_set.mk")
214 case nativeSharedLib, nativeExecutable, nativeTest:
Jiyong Parkf1493cc2020-05-29 21:29:20 +0900215 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.Stem())
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700216 if ccMod, ok := fi.module.(*cc.Module); ok {
217 if ccMod.UnstrippedOutputFile() != nil {
218 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", ccMod.UnstrippedOutputFile().String())
Jiyong Park09d77522019-11-18 11:16:27 +0900219 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700220 ccMod.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w)
221 if ccMod.CoverageOutputFile().Valid() {
222 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", ccMod.CoverageOutputFile().String())
Jiyong Park09d77522019-11-18 11:16:27 +0900223 }
224 }
225 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700226 default:
Jiyong Parkf1493cc2020-05-29 21:29:20 +0900227 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.Stem())
Jiyong Park7aa3f762020-01-28 16:51:34 +0900228 if fi.builtFile == a.manifestPbOut && apexType == flattenedApex {
Jooyung Han75de2612020-01-24 02:02:45 +0900229 if a.primaryApexType {
230 // Make apex_manifest.pb module for this APEX to override all other
231 // modules in the APEXes being overridden by this APEX
232 var patterns []string
233 for _, o := range a.overridableProperties.Overrides {
234 patterns = append(patterns, "%."+o+a.suffix)
235 }
Yo Chiang12d9f7a2020-06-16 17:32:19 +0800236 if len(patterns) > 0 {
237 fmt.Fprintln(w, "LOCAL_OVERRIDES_MODULES :=", strings.Join(patterns, " "))
238 }
Jiyong Park7aa3f762020-01-28 16:51:34 +0900239 if len(a.compatSymlinks) > 0 {
Jooyung Han75de2612020-01-24 02:02:45 +0900240 // For flattened apexes, compat symlinks are attached to apex_manifest.json which is guaranteed for every apex
241 postInstallCommands = append(postInstallCommands, a.compatSymlinks...)
242 }
243 }
244 if len(postInstallCommands) > 0 {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900245 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", strings.Join(postInstallCommands, " && "))
Jiyong Park0abc1b42020-01-09 17:43:39 +0900246 }
Jiyong Park09d77522019-11-18 11:16:27 +0900247 }
248 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
249 }
Jiyong Park31af2672020-02-11 09:36:25 +0900250
251 // m <module_name> will build <module_name>.<apex_name> as well.
252 if fi.moduleName != moduleName && a.primaryApexType {
253 fmt.Fprintln(w, ".PHONY: "+fi.moduleName)
254 fmt.Fprintln(w, fi.moduleName+": "+moduleName)
255 }
Jiyong Park09d77522019-11-18 11:16:27 +0900256 }
257 return moduleNames
258}
259
Jiyong Park7afd1072019-12-30 16:56:33 +0900260func (a *apexBundle) writeRequiredModules(w io.Writer) {
261 var required []string
262 var targetRequired []string
263 var hostRequired []string
264 for _, fi := range a.filesInfo {
265 required = append(required, fi.requiredModuleNames...)
266 targetRequired = append(targetRequired, fi.targetRequiredModuleNames...)
267 hostRequired = append(hostRequired, fi.hostRequiredModuleNames...)
268 }
269
270 if len(required) > 0 {
271 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(required, " "))
272 }
273 if len(targetRequired) > 0 {
274 fmt.Fprintln(w, "LOCAL_TARGET_REQUIRED_MODULES +=", strings.Join(targetRequired, " "))
275 }
276 if len(hostRequired) > 0 {
277 fmt.Fprintln(w, "LOCAL_HOST_REQUIRED_MODULES +=", strings.Join(hostRequired, " "))
278 }
279}
280
Jiyong Park09d77522019-11-18 11:16:27 +0900281func (a *apexBundle) androidMkForType() android.AndroidMkData {
282 return android.AndroidMkData{
283 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
284 moduleNames := []string{}
285 apexType := a.properties.ApexType
286 if a.installable() {
287 apexName := proptools.StringDefault(a.properties.Apex_name, name)
Jiyong Parkdb334862020-02-05 17:19:28 +0900288 moduleNames = a.androidMkForFiles(w, name, apexName, moduleDir)
Jiyong Park09d77522019-11-18 11:16:27 +0900289 }
290
291 if apexType == flattenedApex {
292 // Only image APEXes can be flattened.
293 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
294 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
295 fmt.Fprintln(w, "LOCAL_MODULE :=", name+a.suffix)
296 if len(moduleNames) > 0 {
297 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
298 }
Jiyong Park7afd1072019-12-30 16:56:33 +0900299 a.writeRequiredModules(w)
Jiyong Park09d77522019-11-18 11:16:27 +0900300 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Jiyong Park09d77522019-11-18 11:16:27 +0900301
302 } else {
303 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
304 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
305 fmt.Fprintln(w, "LOCAL_MODULE :=", name+a.suffix)
306 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
307 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFile.String())
308 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", a.installDir.ToMakePath().String())
309 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
310 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Yo Chiang12d9f7a2020-06-16 17:32:19 +0800311 if len(a.overridableProperties.Overrides) > 0 {
312 fmt.Fprintln(w, "LOCAL_OVERRIDES_MODULES :=", strings.Join(a.overridableProperties.Overrides, " "))
313 }
Jiyong Park09d77522019-11-18 11:16:27 +0900314 if len(moduleNames) > 0 {
315 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
316 }
Jiyong Park956305c2020-01-09 12:32:06 +0900317 if len(a.requiredDeps) > 0 {
318 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.requiredDeps, " "))
Jiyong Park09d77522019-11-18 11:16:27 +0900319 }
Jiyong Park7afd1072019-12-30 16:56:33 +0900320 a.writeRequiredModules(w)
Jiyong Park09d77522019-11-18 11:16:27 +0900321 var postInstallCommands []string
322 if a.prebuiltFileToDelete != "" {
323 postInstallCommands = append(postInstallCommands, "rm -rf "+
324 filepath.Join(a.installDir.ToMakePath().String(), a.prebuiltFileToDelete))
325 }
326 // For unflattened apexes, compat symlinks are attached to apex package itself as LOCAL_POST_INSTALL_CMD
327 postInstallCommands = append(postInstallCommands, a.compatSymlinks...)
328 if len(postInstallCommands) > 0 {
329 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", strings.Join(postInstallCommands, " && "))
330 }
Jiyong Park19972c72020-01-28 20:05:29 +0900331
332 if a.mergedNotices.Merged.Valid() {
333 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", a.mergedNotices.Merged.Path().String())
334 }
335
Jiyong Park09d77522019-11-18 11:16:27 +0900336 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
337
338 if apexType == imageApex {
Yo Chiangef2d4892020-05-08 15:38:40 +0800339 fmt.Fprintln(w, "ALL_MODULES.$(my_register_name).BUNDLE :=", a.bundleModuleFile.String())
Jiyong Park09d77522019-11-18 11:16:27 +0900340 }
Jiyong Park3a1602e2020-01-14 14:39:19 +0900341
342 if a.installedFilesFile != nil {
Colin Cross1c85e8e2020-02-26 16:55:51 -0800343 goal := "checkbuild"
Jiyong Park3a1602e2020-01-14 14:39:19 +0900344 distFile := name + "-installed-files.txt"
345 fmt.Fprintln(w, ".PHONY:", goal)
346 fmt.Fprintf(w, "$(call dist-for-goals,%s,%s:%s)\n",
347 goal, a.installedFilesFile.String(), distFile)
348 }
Jiyong Park09d77522019-11-18 11:16:27 +0900349 }
350 }}
351}