blob: 156906dbaa0f313a273d15ccde43c01d0e0be17a [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 }
Jooyung Han2ed99d02020-06-24 23:26:26 +090036 return a.androidMkForType()
Jiyong Park09d77522019-11-18 11:16:27 +090037}
38
Jiyong Parkdb334862020-02-05 17:19:28 +090039func (a *apexBundle) androidMkForFiles(w io.Writer, apexBundleName, apexName, moduleDir string) []string {
40 // apexBundleName comes from the 'name' property; apexName comes from 'apex_name' property.
41 // An apex is installed to /system/apex/<apexBundleName> and is activated at /apex/<apexName>
42 // In many cases, the two names are the same, but could be different in general.
43
Jiyong Park09d77522019-11-18 11:16:27 +090044 moduleNames := []string{}
45 apexType := a.properties.ApexType
46 // To avoid creating duplicate build rules, run this function only when primaryApexType is true
47 // to install symbol files in $(PRODUCT_OUT}/apex.
48 // And if apexType is flattened, run this function to install files in $(PRODUCT_OUT}/system/apex.
49 if !a.primaryApexType && apexType != flattenedApex {
50 return moduleNames
51 }
52
Jiyong Parkdb334862020-02-05 17:19:28 +090053 // b/140136207. When there are overriding APEXes for a VNDK APEX, the symbols file for the overridden
54 // APEX and the overriding APEX will have the same installation paths at /apex/com.android.vndk.v<ver>
55 // as their apexName will be the same. To avoid the path conflicts, skip installing the symbol files
56 // for the overriding VNDK APEXes.
57 symbolFilesNotNeeded := a.vndkApex && len(a.overridableProperties.Overrides) > 0
58 if symbolFilesNotNeeded && apexType != flattenedApex {
59 return moduleNames
60 }
61
Jiyong Park7cd10e32020-01-14 09:22:18 +090062 var postInstallCommands []string
63 for _, fi := range a.filesInfo {
64 if a.linkToSystemLib && fi.transitiveDep && fi.AvailableToPlatform() {
65 // TODO(jiyong): pathOnDevice should come from fi.module, not being calculated here
66 linkTarget := filepath.Join("/system", fi.Path())
Jiyong Parkdb334862020-02-05 17:19:28 +090067 linkPath := filepath.Join(a.installDir.ToMakePath().String(), apexBundleName, fi.Path())
Jiyong Park7cd10e32020-01-14 09:22:18 +090068 mkdirCmd := "mkdir -p " + filepath.Dir(linkPath)
69 linkCmd := "ln -sfn " + linkTarget + " " + linkPath
70 postInstallCommands = append(postInstallCommands, mkdirCmd, linkCmd)
71 }
72 }
Jiyong Park7cd10e32020-01-14 09:22:18 +090073
Liz Kammer5bd365f2020-05-27 15:15:11 -070074 seenDataOutPaths := make(map[string]bool)
75
Jiyong Park09d77522019-11-18 11:16:27 +090076 for _, fi := range a.filesInfo {
Sasha Smundak18d98bc2020-05-27 16:36:07 -070077 if ccMod, ok := fi.module.(*cc.Module); ok && ccMod.Properties.HideFromMake {
Jiyong Park09d77522019-11-18 11:16:27 +090078 continue
79 }
80
Jiyong Park7cd10e32020-01-14 09:22:18 +090081 linkToSystemLib := a.linkToSystemLib && fi.transitiveDep && fi.AvailableToPlatform()
82
83 var moduleName string
84 if linkToSystemLib {
85 moduleName = fi.moduleName
86 } else {
Jiyong Parkdb334862020-02-05 17:19:28 +090087 moduleName = fi.moduleName + "." + apexBundleName + a.suffix
Jiyong Park7cd10e32020-01-14 09:22:18 +090088 }
89
90 if !android.InList(moduleName, moduleNames) {
91 moduleNames = append(moduleNames, moduleName)
92 }
93
94 if linkToSystemLib {
95 // No need to copy the file since it's linked to the system file
96 continue
Jiyong Park09d77522019-11-18 11:16:27 +090097 }
98
99 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
Jiyong Park1833cef2019-12-13 13:28:36 +0900100 if fi.moduleDir != "" {
101 fmt.Fprintln(w, "LOCAL_PATH :=", fi.moduleDir)
102 } else {
103 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
104 }
Jiyong Park7cd10e32020-01-14 09:22:18 +0900105 fmt.Fprintln(w, "LOCAL_MODULE :=", moduleName)
Jiyong Park09d77522019-11-18 11:16:27 +0900106 // /apex/<apex_name>/{lib|framework|...}
107 pathWhenActivated := filepath.Join("$(PRODUCT_OUT)", "apex", apexName, fi.installDir)
108 if apexType == flattenedApex {
109 // /system/apex/<name>/{lib|framework|...}
Liz Kammer5bd365f2020-05-27 15:15:11 -0700110 modulePath := filepath.Join(a.installDir.ToMakePath().String(), apexBundleName, fi.installDir)
111 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", modulePath)
Jiyong Parkdb334862020-02-05 17:19:28 +0900112 if a.primaryApexType && !symbolFilesNotNeeded {
Jiyong Park09d77522019-11-18 11:16:27 +0900113 fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated)
114 }
115 if len(fi.symlinks) > 0 {
116 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
117 }
Liz Kammer5bd365f2020-05-27 15:15:11 -0700118 newDataPaths := []android.Path{}
119 for _, path := range fi.dataPaths {
120 dataOutPath := modulePath + ":" + path.Rel()
121 if ok := seenDataOutPaths[dataOutPath]; !ok {
122 newDataPaths = append(newDataPaths, path)
123 seenDataOutPaths[dataOutPath] = true
124 }
125 }
126 if len(newDataPaths) > 0 {
127 fmt.Fprintln(w, "LOCAL_TEST_DATA :=", strings.Join(cc.AndroidMkDataPaths(newDataPaths), " "))
Liz Kammer1c14a212020-05-12 15:26:55 -0700128 }
Jiyong Park09d77522019-11-18 11:16:27 +0900129
Bob Badoura75b0572020-02-18 20:21:55 -0800130 if fi.module != nil && len(fi.module.NoticeFiles()) > 0 {
131 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", strings.Join(fi.module.NoticeFiles().Strings(), " "))
Jiyong Park09d77522019-11-18 11:16:27 +0900132 }
133 } else {
134 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
Jiyong Park19972c72020-01-28 20:05:29 +0900135
136 // For non-flattend APEXes, the merged notice file is attached to the APEX itself.
137 // We don't need to have notice file for the individual modules in it. Otherwise,
138 // we will have duplicated notice entries.
139 fmt.Fprintln(w, "LOCAL_NO_NOTICE_FILE := true")
Jiyong Park09d77522019-11-18 11:16:27 +0900140 }
141 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
142 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
143 if fi.module != nil {
144 archStr := fi.module.Target().Arch.ArchType.String()
145 host := false
146 switch fi.module.Target().Os.Class {
147 case android.Host:
148 if fi.module.Target().Arch.ArchType != android.Common {
149 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
150 }
151 host = true
152 case android.HostCross:
153 if fi.module.Target().Arch.ArchType != android.Common {
154 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
155 }
156 host = true
157 case android.Device:
158 if fi.module.Target().Arch.ArchType != android.Common {
159 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
160 }
161 }
162 if host {
163 makeOs := fi.module.Target().Os.String()
164 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
165 makeOs = "linux"
166 }
167 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
168 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
169 }
170 }
Jiyong Park618922e2020-01-08 13:35:43 +0900171 if fi.jacocoReportClassesFile != nil {
172 fmt.Fprintln(w, "LOCAL_SOONG_JACOCO_REPORT_CLASSES_JAR :=", fi.jacocoReportClassesFile.String())
173 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700174 switch fi.class {
175 case javaSharedLib:
Jiyong Park09d77522019-11-18 11:16:27 +0900176 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
177 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
178 // we will have foo.jar.jar
Jiyong Parkf1493cc2020-05-29 21:29:20 +0900179 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.Stem(), ".jar"))
Paul Duffin44b481b2020-06-17 16:59:43 +0100180 if javaModule, ok := fi.module.(java.ApexDependency); ok {
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900181 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
182 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
183 } else {
184 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", fi.builtFile.String())
185 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", fi.builtFile.String())
186 }
Jiyong Park09d77522019-11-18 11:16:27 +0900187 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
188 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
189 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700190 case app:
Colin Cross503c1d02020-01-28 14:00:53 -0800191 fmt.Fprintln(w, "LOCAL_CERTIFICATE :=", fi.certificate.AndroidMkString())
Jiyong Park618922e2020-01-08 13:35:43 +0900192 // soong_app_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .apk Therefore
193 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
194 // we will have foo.apk.apk
Jiyong Parkf1493cc2020-05-29 21:29:20 +0900195 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.Stem(), ".apk"))
Jaewoong Jung87a33e72020-03-26 14:01:48 -0700196 if app, ok := fi.module.(*java.AndroidApp); ok && len(app.JniCoverageOutputs()) > 0 {
197 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", strings.Join(app.JniCoverageOutputs().Strings(), " "))
198 }
Jiyong Park618922e2020-01-08 13:35:43 +0900199 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_app_prebuilt.mk")
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700200 case appSet:
201 as, ok := fi.module.(*java.AndroidAppSet)
202 if !ok {
203 panic(fmt.Sprintf("Expected %s to be AndroidAppSet", fi.module))
204 }
205 fmt.Fprintln(w, "LOCAL_APK_SET_MASTER_FILE :=", as.MasterFile())
Colin Cross4fb652d2020-07-09 19:05:35 -0700206 fmt.Fprintln(w, "LOCAL_APKCERTS_FILE :=", as.APKCertsFile().String())
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700207 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_android_app_set.mk")
208 case nativeSharedLib, nativeExecutable, nativeTest:
Jiyong Parkf1493cc2020-05-29 21:29:20 +0900209 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.Stem())
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700210 if ccMod, ok := fi.module.(*cc.Module); ok {
211 if ccMod.UnstrippedOutputFile() != nil {
212 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", ccMod.UnstrippedOutputFile().String())
Jiyong Park09d77522019-11-18 11:16:27 +0900213 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700214 ccMod.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w)
215 if ccMod.CoverageOutputFile().Valid() {
216 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", ccMod.CoverageOutputFile().String())
Jiyong Park09d77522019-11-18 11:16:27 +0900217 }
218 }
219 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700220 default:
Jiyong Parkf1493cc2020-05-29 21:29:20 +0900221 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.Stem())
Jiyong Park7aa3f762020-01-28 16:51:34 +0900222 if fi.builtFile == a.manifestPbOut && apexType == flattenedApex {
Jooyung Han75de2612020-01-24 02:02:45 +0900223 if a.primaryApexType {
224 // Make apex_manifest.pb module for this APEX to override all other
225 // modules in the APEXes being overridden by this APEX
226 var patterns []string
227 for _, o := range a.overridableProperties.Overrides {
228 patterns = append(patterns, "%."+o+a.suffix)
229 }
Yo Chiang12d9f7a2020-06-16 17:32:19 +0800230 if len(patterns) > 0 {
231 fmt.Fprintln(w, "LOCAL_OVERRIDES_MODULES :=", strings.Join(patterns, " "))
232 }
Jiyong Park7aa3f762020-01-28 16:51:34 +0900233 if len(a.compatSymlinks) > 0 {
Jooyung Han75de2612020-01-24 02:02:45 +0900234 // For flattened apexes, compat symlinks are attached to apex_manifest.json which is guaranteed for every apex
235 postInstallCommands = append(postInstallCommands, a.compatSymlinks...)
236 }
237 }
238 if len(postInstallCommands) > 0 {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900239 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", strings.Join(postInstallCommands, " && "))
Jiyong Park0abc1b42020-01-09 17:43:39 +0900240 }
Jiyong Park09d77522019-11-18 11:16:27 +0900241 }
242 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
243 }
Jiyong Park31af2672020-02-11 09:36:25 +0900244
245 // m <module_name> will build <module_name>.<apex_name> as well.
246 if fi.moduleName != moduleName && a.primaryApexType {
247 fmt.Fprintln(w, ".PHONY: "+fi.moduleName)
248 fmt.Fprintln(w, fi.moduleName+": "+moduleName)
249 }
Jiyong Park09d77522019-11-18 11:16:27 +0900250 }
251 return moduleNames
252}
253
Jiyong Park7afd1072019-12-30 16:56:33 +0900254func (a *apexBundle) writeRequiredModules(w io.Writer) {
255 var required []string
256 var targetRequired []string
257 var hostRequired []string
258 for _, fi := range a.filesInfo {
259 required = append(required, fi.requiredModuleNames...)
260 targetRequired = append(targetRequired, fi.targetRequiredModuleNames...)
261 hostRequired = append(hostRequired, fi.hostRequiredModuleNames...)
262 }
263
264 if len(required) > 0 {
265 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(required, " "))
266 }
267 if len(targetRequired) > 0 {
268 fmt.Fprintln(w, "LOCAL_TARGET_REQUIRED_MODULES +=", strings.Join(targetRequired, " "))
269 }
270 if len(hostRequired) > 0 {
271 fmt.Fprintln(w, "LOCAL_HOST_REQUIRED_MODULES +=", strings.Join(hostRequired, " "))
272 }
273}
274
Jiyong Park09d77522019-11-18 11:16:27 +0900275func (a *apexBundle) androidMkForType() android.AndroidMkData {
276 return android.AndroidMkData{
277 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
278 moduleNames := []string{}
279 apexType := a.properties.ApexType
280 if a.installable() {
281 apexName := proptools.StringDefault(a.properties.Apex_name, name)
Jiyong Parkdb334862020-02-05 17:19:28 +0900282 moduleNames = a.androidMkForFiles(w, name, apexName, moduleDir)
Jiyong Park09d77522019-11-18 11:16:27 +0900283 }
284
285 if apexType == flattenedApex {
286 // Only image APEXes can be flattened.
287 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
288 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
289 fmt.Fprintln(w, "LOCAL_MODULE :=", name+a.suffix)
290 if len(moduleNames) > 0 {
291 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
292 }
Jiyong Park7afd1072019-12-30 16:56:33 +0900293 a.writeRequiredModules(w)
Jiyong Park09d77522019-11-18 11:16:27 +0900294 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Jiyong Park09d77522019-11-18 11:16:27 +0900295
296 } else {
297 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
298 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
299 fmt.Fprintln(w, "LOCAL_MODULE :=", name+a.suffix)
300 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
301 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFile.String())
302 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", a.installDir.ToMakePath().String())
303 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
304 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jooyung Han2ed99d02020-06-24 23:26:26 +0900305
306 // Because apex writes .mk with Custom(), we need to write manually some common properties
307 // which are available via data.Entries
308 commonProperties := []string{
309 "LOCAL_INIT_RC", "LOCAL_VINTF_FRAGMENTS",
310 "LOCAL_PROPRIETARY_MODULE", "LOCAL_VENDOR_MODULE", "LOCAL_ODM_MODULE", "LOCAL_PRODUCT_MODULE", "LOCAL_SYSTEM_EXT_MODULE",
311 "LOCAL_MODULE_OWNER",
312 }
313 for _, name := range commonProperties {
314 if value, ok := data.Entries.EntryMap[name]; ok {
315 fmt.Fprintln(w, name+" := "+strings.Join(value, " "))
316 }
317 }
318
Yo Chiang12d9f7a2020-06-16 17:32:19 +0800319 if len(a.overridableProperties.Overrides) > 0 {
320 fmt.Fprintln(w, "LOCAL_OVERRIDES_MODULES :=", strings.Join(a.overridableProperties.Overrides, " "))
321 }
Jiyong Park09d77522019-11-18 11:16:27 +0900322 if len(moduleNames) > 0 {
323 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
324 }
Jiyong Park956305c2020-01-09 12:32:06 +0900325 if len(a.requiredDeps) > 0 {
326 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.requiredDeps, " "))
Jiyong Park09d77522019-11-18 11:16:27 +0900327 }
Jiyong Park7afd1072019-12-30 16:56:33 +0900328 a.writeRequiredModules(w)
Jiyong Park09d77522019-11-18 11:16:27 +0900329 var postInstallCommands []string
330 if a.prebuiltFileToDelete != "" {
331 postInstallCommands = append(postInstallCommands, "rm -rf "+
332 filepath.Join(a.installDir.ToMakePath().String(), a.prebuiltFileToDelete))
333 }
334 // For unflattened apexes, compat symlinks are attached to apex package itself as LOCAL_POST_INSTALL_CMD
335 postInstallCommands = append(postInstallCommands, a.compatSymlinks...)
336 if len(postInstallCommands) > 0 {
337 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", strings.Join(postInstallCommands, " && "))
338 }
Jiyong Park19972c72020-01-28 20:05:29 +0900339
340 if a.mergedNotices.Merged.Valid() {
341 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", a.mergedNotices.Merged.Path().String())
342 }
343
Jiyong Park09d77522019-11-18 11:16:27 +0900344 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
345
346 if apexType == imageApex {
Yo Chiangef2d4892020-05-08 15:38:40 +0800347 fmt.Fprintln(w, "ALL_MODULES.$(my_register_name).BUNDLE :=", a.bundleModuleFile.String())
Jiyong Park09d77522019-11-18 11:16:27 +0900348 }
Jiyong Park3a1602e2020-01-14 14:39:19 +0900349
350 if a.installedFilesFile != nil {
Colin Cross1c85e8e2020-02-26 16:55:51 -0800351 goal := "checkbuild"
Jiyong Park3a1602e2020-01-14 14:39:19 +0900352 distFile := name + "-installed-files.txt"
353 fmt.Fprintln(w, ".PHONY:", goal)
354 fmt.Fprintf(w, "$(call dist-for-goals,%s,%s:%s)\n",
355 goal, a.installedFilesFile.String(), distFile)
356 }
Jiyong Park09d77522019-11-18 11:16:27 +0900357 }
358 }}
359}