blob: f1194be71eaa015966ef7964f05246e43612756c [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 }
66 postInstallCommands = append(postInstallCommands, a.compatSymlinks...)
67
Jiyong Park09d77522019-11-18 11:16:27 +090068 for _, fi := range a.filesInfo {
69 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
70 continue
71 }
72
Jiyong Park7cd10e32020-01-14 09:22:18 +090073 linkToSystemLib := a.linkToSystemLib && fi.transitiveDep && fi.AvailableToPlatform()
74
75 var moduleName string
76 if linkToSystemLib {
77 moduleName = fi.moduleName
78 } else {
79 moduleName = fi.moduleName + "." + apexName + a.suffix
80 }
81
82 if !android.InList(moduleName, moduleNames) {
83 moduleNames = append(moduleNames, moduleName)
84 }
85
86 if linkToSystemLib {
87 // No need to copy the file since it's linked to the system file
88 continue
Jiyong Park09d77522019-11-18 11:16:27 +090089 }
90
91 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
Jiyong Park1833cef2019-12-13 13:28:36 +090092 if fi.moduleDir != "" {
93 fmt.Fprintln(w, "LOCAL_PATH :=", fi.moduleDir)
94 } else {
95 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
96 }
Jiyong Park7cd10e32020-01-14 09:22:18 +090097 fmt.Fprintln(w, "LOCAL_MODULE :=", moduleName)
Jiyong Park09d77522019-11-18 11:16:27 +090098 // /apex/<apex_name>/{lib|framework|...}
99 pathWhenActivated := filepath.Join("$(PRODUCT_OUT)", "apex", apexName, fi.installDir)
100 if apexType == flattenedApex {
101 // /system/apex/<name>/{lib|framework|...}
102 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join(a.installDir.ToMakePath().String(),
103 apexName, fi.installDir))
104 if a.primaryApexType {
105 fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated)
106 }
107 if len(fi.symlinks) > 0 {
108 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
109 }
110
111 if fi.module != nil && fi.module.NoticeFile().Valid() {
112 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", fi.module.NoticeFile().Path().String())
113 }
114 } else {
115 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
116 }
117 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
118 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
119 if fi.module != nil {
120 archStr := fi.module.Target().Arch.ArchType.String()
121 host := false
122 switch fi.module.Target().Os.Class {
123 case android.Host:
124 if fi.module.Target().Arch.ArchType != android.Common {
125 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
126 }
127 host = true
128 case android.HostCross:
129 if fi.module.Target().Arch.ArchType != android.Common {
130 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
131 }
132 host = true
133 case android.Device:
134 if fi.module.Target().Arch.ArchType != android.Common {
135 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
136 }
137 }
138 if host {
139 makeOs := fi.module.Target().Os.String()
140 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
141 makeOs = "linux"
142 }
143 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
144 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
145 }
146 }
Jiyong Park618922e2020-01-08 13:35:43 +0900147 if fi.jacocoReportClassesFile != nil {
148 fmt.Fprintln(w, "LOCAL_SOONG_JACOCO_REPORT_CLASSES_JAR :=", fi.jacocoReportClassesFile.String())
149 }
Jiyong Park09d77522019-11-18 11:16:27 +0900150 if fi.class == javaSharedLib {
Jooyung Han58f26ab2019-12-18 15:34:32 +0900151 javaModule := fi.module.(javaLibrary)
Jiyong Park09d77522019-11-18 11:16:27 +0900152 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
153 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
154 // we will have foo.jar.jar
155 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
156 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
157 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
158 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
159 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
160 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
Jiyong Park618922e2020-01-08 13:35:43 +0900161 } else if fi.class == app {
162 // soong_app_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .apk Therefore
163 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
164 // we will have foo.apk.apk
165 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".apk"))
166 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_app_prebuilt.mk")
Jiyong Park09d77522019-11-18 11:16:27 +0900167 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable || fi.class == nativeTest {
168 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
169 if cc, ok := fi.module.(*cc.Module); ok {
170 if cc.UnstrippedOutputFile() != nil {
171 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
172 }
173 cc.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w)
174 if cc.CoverageOutputFile().Valid() {
175 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", cc.CoverageOutputFile().String())
176 }
177 }
178 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
179 } else {
180 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Jiyong Park0abc1b42020-01-09 17:43:39 +0900181 if a.primaryApexType && fi.builtFile == a.manifestPbOut {
182 // Make apex_manifest.pb module for this APEX to override all other
183 // modules in the APEXes being overridden by this APEX
184 var patterns []string
185 for _, o := range a.overridableProperties.Overrides {
186 patterns = append(patterns, "%."+o+a.suffix)
187 }
188 fmt.Fprintln(w, "LOCAL_OVERRIDES_MODULES :=", strings.Join(patterns, " "))
189
Jiyong Park7cd10e32020-01-14 09:22:18 +0900190 if apexType == flattenedApex && len(postInstallCommands) > 0 {
Jiyong Park0abc1b42020-01-09 17:43:39 +0900191 // For flattened apexes, compat symlinks are attached to apex_manifest.json which is guaranteed for every apex
Jiyong Park7cd10e32020-01-14 09:22:18 +0900192 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", strings.Join(postInstallCommands, " && "))
Jiyong Park0abc1b42020-01-09 17:43:39 +0900193 }
Jiyong Park09d77522019-11-18 11:16:27 +0900194 }
195 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
196 }
197 }
198 return moduleNames
199}
200
Jiyong Park7afd1072019-12-30 16:56:33 +0900201func (a *apexBundle) writeRequiredModules(w io.Writer) {
202 var required []string
203 var targetRequired []string
204 var hostRequired []string
205 for _, fi := range a.filesInfo {
206 required = append(required, fi.requiredModuleNames...)
207 targetRequired = append(targetRequired, fi.targetRequiredModuleNames...)
208 hostRequired = append(hostRequired, fi.hostRequiredModuleNames...)
209 }
210
211 if len(required) > 0 {
212 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(required, " "))
213 }
214 if len(targetRequired) > 0 {
215 fmt.Fprintln(w, "LOCAL_TARGET_REQUIRED_MODULES +=", strings.Join(targetRequired, " "))
216 }
217 if len(hostRequired) > 0 {
218 fmt.Fprintln(w, "LOCAL_HOST_REQUIRED_MODULES +=", strings.Join(hostRequired, " "))
219 }
220}
221
Jiyong Park09d77522019-11-18 11:16:27 +0900222func (a *apexBundle) androidMkForType() android.AndroidMkData {
223 return android.AndroidMkData{
224 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
225 moduleNames := []string{}
226 apexType := a.properties.ApexType
227 if a.installable() {
228 apexName := proptools.StringDefault(a.properties.Apex_name, name)
229 moduleNames = a.androidMkForFiles(w, apexName, moduleDir)
230 }
231
232 if apexType == flattenedApex {
233 // Only image APEXes can be flattened.
234 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
235 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
236 fmt.Fprintln(w, "LOCAL_MODULE :=", name+a.suffix)
237 if len(moduleNames) > 0 {
238 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
239 }
Jiyong Park7afd1072019-12-30 16:56:33 +0900240 a.writeRequiredModules(w)
Jiyong Park09d77522019-11-18 11:16:27 +0900241 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Jiyong Park09d77522019-11-18 11:16:27 +0900242
243 } else {
244 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
245 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
246 fmt.Fprintln(w, "LOCAL_MODULE :=", name+a.suffix)
247 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
248 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFile.String())
249 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", a.installDir.ToMakePath().String())
250 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
251 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -0800252 fmt.Fprintln(w, "LOCAL_OVERRIDES_MODULES :=", strings.Join(a.overridableProperties.Overrides, " "))
Jiyong Park09d77522019-11-18 11:16:27 +0900253 if len(moduleNames) > 0 {
254 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
255 }
Jiyong Park956305c2020-01-09 12:32:06 +0900256 if len(a.requiredDeps) > 0 {
257 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.requiredDeps, " "))
Jiyong Park09d77522019-11-18 11:16:27 +0900258 }
Jiyong Park7afd1072019-12-30 16:56:33 +0900259 a.writeRequiredModules(w)
Jiyong Park09d77522019-11-18 11:16:27 +0900260 var postInstallCommands []string
261 if a.prebuiltFileToDelete != "" {
262 postInstallCommands = append(postInstallCommands, "rm -rf "+
263 filepath.Join(a.installDir.ToMakePath().String(), a.prebuiltFileToDelete))
264 }
265 // For unflattened apexes, compat symlinks are attached to apex package itself as LOCAL_POST_INSTALL_CMD
266 postInstallCommands = append(postInstallCommands, a.compatSymlinks...)
267 if len(postInstallCommands) > 0 {
268 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", strings.Join(postInstallCommands, " && "))
269 }
270 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
271
272 if apexType == imageApex {
273 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
274 }
Jiyong Park3a1602e2020-01-14 14:39:19 +0900275
276 if a.installedFilesFile != nil {
277 goal := "droidcore"
278 distFile := name + "-installed-files.txt"
279 fmt.Fprintln(w, ".PHONY:", goal)
280 fmt.Fprintf(w, "$(call dist-for-goals,%s,%s:%s)\n",
281 goal, a.installedFilesFile.String(), distFile)
282 }
Jiyong Park09d77522019-11-18 11:16:27 +0900283 }
284 }}
285}