blob: 89299105ce43923b43ac00a8497aebb5df4af1ec [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
45func (a *apexBundle) androidMkForFiles(w io.Writer, apexName, moduleDir string) []string {
46 moduleNames := []string{}
47 apexType := a.properties.ApexType
48 // To avoid creating duplicate build rules, run this function only when primaryApexType is true
49 // to install symbol files in $(PRODUCT_OUT}/apex.
50 // And if apexType is flattened, run this function to install files in $(PRODUCT_OUT}/system/apex.
51 if !a.primaryApexType && apexType != flattenedApex {
52 return moduleNames
53 }
54
Jiyong Park7cd10e32020-01-14 09:22:18 +090055 var postInstallCommands []string
56 for _, fi := range a.filesInfo {
57 if a.linkToSystemLib && fi.transitiveDep && fi.AvailableToPlatform() {
58 // TODO(jiyong): pathOnDevice should come from fi.module, not being calculated here
59 linkTarget := filepath.Join("/system", fi.Path())
60 linkPath := filepath.Join(a.installDir.ToMakePath().String(), apexName, fi.Path())
61 mkdirCmd := "mkdir -p " + filepath.Dir(linkPath)
62 linkCmd := "ln -sfn " + linkTarget + " " + linkPath
63 postInstallCommands = append(postInstallCommands, mkdirCmd, linkCmd)
64 }
65 }
Jiyong Park7cd10e32020-01-14 09:22:18 +090066
Jiyong Park09d77522019-11-18 11:16:27 +090067 for _, fi := range a.filesInfo {
68 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
69 continue
70 }
71
Jiyong Park7cd10e32020-01-14 09:22:18 +090072 linkToSystemLib := a.linkToSystemLib && fi.transitiveDep && fi.AvailableToPlatform()
73
74 var moduleName string
75 if linkToSystemLib {
76 moduleName = fi.moduleName
77 } else {
78 moduleName = fi.moduleName + "." + apexName + a.suffix
79 }
80
81 if !android.InList(moduleName, moduleNames) {
82 moduleNames = append(moduleNames, moduleName)
83 }
84
85 if linkToSystemLib {
86 // No need to copy the file since it's linked to the system file
87 continue
Jiyong Park09d77522019-11-18 11:16:27 +090088 }
89
90 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
Jiyong Park1833cef2019-12-13 13:28:36 +090091 if fi.moduleDir != "" {
92 fmt.Fprintln(w, "LOCAL_PATH :=", fi.moduleDir)
93 } else {
94 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
95 }
Jiyong Park7cd10e32020-01-14 09:22:18 +090096 fmt.Fprintln(w, "LOCAL_MODULE :=", moduleName)
Jiyong Park09d77522019-11-18 11:16:27 +090097 // /apex/<apex_name>/{lib|framework|...}
98 pathWhenActivated := filepath.Join("$(PRODUCT_OUT)", "apex", apexName, fi.installDir)
99 if apexType == flattenedApex {
100 // /system/apex/<name>/{lib|framework|...}
101 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join(a.installDir.ToMakePath().String(),
102 apexName, fi.installDir))
103 if a.primaryApexType {
104 fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated)
105 }
106 if len(fi.symlinks) > 0 {
107 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
108 }
109
110 if fi.module != nil && fi.module.NoticeFile().Valid() {
111 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", fi.module.NoticeFile().Path().String())
112 }
113 } else {
114 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
Jiyong Park19972c72020-01-28 20:05:29 +0900115
116 // For non-flattend APEXes, the merged notice file is attached to the APEX itself.
117 // We don't need to have notice file for the individual modules in it. Otherwise,
118 // we will have duplicated notice entries.
119 fmt.Fprintln(w, "LOCAL_NO_NOTICE_FILE := true")
Jiyong Park09d77522019-11-18 11:16:27 +0900120 }
121 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
122 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
123 if fi.module != nil {
124 archStr := fi.module.Target().Arch.ArchType.String()
125 host := false
126 switch fi.module.Target().Os.Class {
127 case android.Host:
128 if fi.module.Target().Arch.ArchType != android.Common {
129 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
130 }
131 host = true
132 case android.HostCross:
133 if fi.module.Target().Arch.ArchType != android.Common {
134 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
135 }
136 host = true
137 case android.Device:
138 if fi.module.Target().Arch.ArchType != android.Common {
139 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
140 }
141 }
142 if host {
143 makeOs := fi.module.Target().Os.String()
144 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
145 makeOs = "linux"
146 }
147 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
148 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
149 }
150 }
Jiyong Park618922e2020-01-08 13:35:43 +0900151 if fi.jacocoReportClassesFile != nil {
152 fmt.Fprintln(w, "LOCAL_SOONG_JACOCO_REPORT_CLASSES_JAR :=", fi.jacocoReportClassesFile.String())
153 }
Jiyong Park09d77522019-11-18 11:16:27 +0900154 if fi.class == javaSharedLib {
Jooyung Han58f26ab2019-12-18 15:34:32 +0900155 javaModule := fi.module.(javaLibrary)
Jiyong Park09d77522019-11-18 11:16:27 +0900156 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
157 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
158 // we will have foo.jar.jar
159 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
160 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
161 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
162 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
163 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
164 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
Jiyong Park618922e2020-01-08 13:35:43 +0900165 } else if fi.class == app {
Colin Cross503c1d02020-01-28 14:00:53 -0800166 fmt.Fprintln(w, "LOCAL_CERTIFICATE :=", fi.certificate.AndroidMkString())
Jiyong Park618922e2020-01-08 13:35:43 +0900167 // soong_app_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .apk Therefore
168 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
169 // we will have foo.apk.apk
170 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".apk"))
171 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_app_prebuilt.mk")
Jiyong Park09d77522019-11-18 11:16:27 +0900172 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable || fi.class == nativeTest {
173 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
174 if cc, ok := fi.module.(*cc.Module); ok {
175 if cc.UnstrippedOutputFile() != nil {
176 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
177 }
178 cc.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w)
179 if cc.CoverageOutputFile().Valid() {
180 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", cc.CoverageOutputFile().String())
181 }
182 }
183 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
184 } else {
185 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Jiyong Park7aa3f762020-01-28 16:51:34 +0900186 if fi.builtFile == a.manifestPbOut && apexType == flattenedApex {
Jooyung Han75de2612020-01-24 02:02:45 +0900187 if a.primaryApexType {
188 // Make apex_manifest.pb module for this APEX to override all other
189 // modules in the APEXes being overridden by this APEX
190 var patterns []string
191 for _, o := range a.overridableProperties.Overrides {
192 patterns = append(patterns, "%."+o+a.suffix)
193 }
194 fmt.Fprintln(w, "LOCAL_OVERRIDES_MODULES :=", strings.Join(patterns, " "))
Jiyong Park0abc1b42020-01-09 17:43:39 +0900195
Jiyong Park7aa3f762020-01-28 16:51:34 +0900196 if len(a.compatSymlinks) > 0 {
Jooyung Han75de2612020-01-24 02:02:45 +0900197 // For flattened apexes, compat symlinks are attached to apex_manifest.json which is guaranteed for every apex
198 postInstallCommands = append(postInstallCommands, a.compatSymlinks...)
199 }
200 }
201 if len(postInstallCommands) > 0 {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900202 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", strings.Join(postInstallCommands, " && "))
Jiyong Park0abc1b42020-01-09 17:43:39 +0900203 }
Jiyong Park09d77522019-11-18 11:16:27 +0900204 }
205 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
206 }
207 }
208 return moduleNames
209}
210
Jiyong Park7afd1072019-12-30 16:56:33 +0900211func (a *apexBundle) writeRequiredModules(w io.Writer) {
212 var required []string
213 var targetRequired []string
214 var hostRequired []string
215 for _, fi := range a.filesInfo {
216 required = append(required, fi.requiredModuleNames...)
217 targetRequired = append(targetRequired, fi.targetRequiredModuleNames...)
218 hostRequired = append(hostRequired, fi.hostRequiredModuleNames...)
219 }
220
221 if len(required) > 0 {
222 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(required, " "))
223 }
224 if len(targetRequired) > 0 {
225 fmt.Fprintln(w, "LOCAL_TARGET_REQUIRED_MODULES +=", strings.Join(targetRequired, " "))
226 }
227 if len(hostRequired) > 0 {
228 fmt.Fprintln(w, "LOCAL_HOST_REQUIRED_MODULES +=", strings.Join(hostRequired, " "))
229 }
230}
231
Jiyong Park09d77522019-11-18 11:16:27 +0900232func (a *apexBundle) androidMkForType() android.AndroidMkData {
233 return android.AndroidMkData{
234 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
235 moduleNames := []string{}
236 apexType := a.properties.ApexType
237 if a.installable() {
238 apexName := proptools.StringDefault(a.properties.Apex_name, name)
239 moduleNames = a.androidMkForFiles(w, apexName, moduleDir)
240 }
241
242 if apexType == flattenedApex {
243 // Only image APEXes can be flattened.
244 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
245 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
246 fmt.Fprintln(w, "LOCAL_MODULE :=", name+a.suffix)
247 if len(moduleNames) > 0 {
248 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
249 }
Jiyong Park7afd1072019-12-30 16:56:33 +0900250 a.writeRequiredModules(w)
Jiyong Park09d77522019-11-18 11:16:27 +0900251 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Jiyong Park09d77522019-11-18 11:16:27 +0900252
253 } else {
254 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
255 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
256 fmt.Fprintln(w, "LOCAL_MODULE :=", name+a.suffix)
257 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
258 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFile.String())
259 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", a.installDir.ToMakePath().String())
260 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
261 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -0800262 fmt.Fprintln(w, "LOCAL_OVERRIDES_MODULES :=", strings.Join(a.overridableProperties.Overrides, " "))
Jiyong Park09d77522019-11-18 11:16:27 +0900263 if len(moduleNames) > 0 {
264 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
265 }
Jiyong Park956305c2020-01-09 12:32:06 +0900266 if len(a.requiredDeps) > 0 {
267 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.requiredDeps, " "))
Jiyong Park09d77522019-11-18 11:16:27 +0900268 }
Jiyong Park7afd1072019-12-30 16:56:33 +0900269 a.writeRequiredModules(w)
Jiyong Park09d77522019-11-18 11:16:27 +0900270 var postInstallCommands []string
271 if a.prebuiltFileToDelete != "" {
272 postInstallCommands = append(postInstallCommands, "rm -rf "+
273 filepath.Join(a.installDir.ToMakePath().String(), a.prebuiltFileToDelete))
274 }
275 // For unflattened apexes, compat symlinks are attached to apex package itself as LOCAL_POST_INSTALL_CMD
276 postInstallCommands = append(postInstallCommands, a.compatSymlinks...)
277 if len(postInstallCommands) > 0 {
278 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", strings.Join(postInstallCommands, " && "))
279 }
Jiyong Park19972c72020-01-28 20:05:29 +0900280
281 if a.mergedNotices.Merged.Valid() {
282 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", a.mergedNotices.Merged.Path().String())
283 }
284
Jiyong Park09d77522019-11-18 11:16:27 +0900285 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
286
287 if apexType == imageApex {
288 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
289 }
Jiyong Park3a1602e2020-01-14 14:39:19 +0900290
291 if a.installedFilesFile != nil {
292 goal := "droidcore"
293 distFile := name + "-installed-files.txt"
294 fmt.Fprintln(w, ".PHONY:", goal)
295 fmt.Fprintf(w, "$(call dist-for-goals,%s,%s:%s)\n",
296 goal, a.installedFilesFile.String(), distFile)
297 }
Jiyong Park09d77522019-11-18 11:16:27 +0900298 }
299 }}
300}