blob: ad7d2f1428513a2ff87af9337efa7d207c961247 [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
55 for _, fi := range a.filesInfo {
56 if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake {
57 continue
58 }
59
60 if !android.InList(fi.moduleName, moduleNames) {
61 moduleNames = append(moduleNames, fi.moduleName)
62 }
63
64 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
Jiyong Park1833cef2019-12-13 13:28:36 +090065 if fi.moduleDir != "" {
66 fmt.Fprintln(w, "LOCAL_PATH :=", fi.moduleDir)
67 } else {
68 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
69 }
Jiyong Park09d77522019-11-18 11:16:27 +090070 fmt.Fprintln(w, "LOCAL_MODULE :=", fi.moduleName)
71 // /apex/<apex_name>/{lib|framework|...}
72 pathWhenActivated := filepath.Join("$(PRODUCT_OUT)", "apex", apexName, fi.installDir)
73 if apexType == flattenedApex {
74 // /system/apex/<name>/{lib|framework|...}
75 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", filepath.Join(a.installDir.ToMakePath().String(),
76 apexName, fi.installDir))
77 if a.primaryApexType {
78 fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathWhenActivated)
79 }
80 if len(fi.symlinks) > 0 {
81 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
82 }
83
84 if fi.module != nil && fi.module.NoticeFile().Valid() {
85 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", fi.module.NoticeFile().Path().String())
86 }
87 } else {
88 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
89 }
90 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
91 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake())
92 if fi.module != nil {
93 archStr := fi.module.Target().Arch.ArchType.String()
94 host := false
95 switch fi.module.Target().Os.Class {
96 case android.Host:
97 if fi.module.Target().Arch.ArchType != android.Common {
98 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
99 }
100 host = true
101 case android.HostCross:
102 if fi.module.Target().Arch.ArchType != android.Common {
103 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
104 }
105 host = true
106 case android.Device:
107 if fi.module.Target().Arch.ArchType != android.Common {
108 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
109 }
110 }
111 if host {
112 makeOs := fi.module.Target().Os.String()
113 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic {
114 makeOs = "linux"
115 }
116 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
117 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
118 }
119 }
Jiyong Park618922e2020-01-08 13:35:43 +0900120 if fi.jacocoReportClassesFile != nil {
121 fmt.Fprintln(w, "LOCAL_SOONG_JACOCO_REPORT_CLASSES_JAR :=", fi.jacocoReportClassesFile.String())
122 }
Jiyong Park09d77522019-11-18 11:16:27 +0900123 if fi.class == javaSharedLib {
Jooyung Han58f26ab2019-12-18 15:34:32 +0900124 javaModule := fi.module.(javaLibrary)
Jiyong Park09d77522019-11-18 11:16:27 +0900125 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
126 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
127 // we will have foo.jar.jar
128 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar"))
129 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
130 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
131 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
132 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
133 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
Jiyong Park618922e2020-01-08 13:35:43 +0900134 } else if fi.class == app {
135 // soong_app_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .apk Therefore
136 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
137 // we will have foo.apk.apk
138 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".apk"))
139 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_app_prebuilt.mk")
Jiyong Park09d77522019-11-18 11:16:27 +0900140 } else if fi.class == nativeSharedLib || fi.class == nativeExecutable || fi.class == nativeTest {
141 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
142 if cc, ok := fi.module.(*cc.Module); ok {
143 if cc.UnstrippedOutputFile() != nil {
144 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String())
145 }
146 cc.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w)
147 if cc.CoverageOutputFile().Valid() {
148 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", cc.CoverageOutputFile().String())
149 }
150 }
151 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk")
152 } else {
153 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base())
Jiyong Park0abc1b42020-01-09 17:43:39 +0900154 if a.primaryApexType && fi.builtFile == a.manifestPbOut {
155 // Make apex_manifest.pb module for this APEX to override all other
156 // modules in the APEXes being overridden by this APEX
157 var patterns []string
158 for _, o := range a.overridableProperties.Overrides {
159 patterns = append(patterns, "%."+o+a.suffix)
160 }
161 fmt.Fprintln(w, "LOCAL_OVERRIDES_MODULES :=", strings.Join(patterns, " "))
162
163 if len(a.compatSymlinks) > 0 {
164 // For flattened apexes, compat symlinks are attached to apex_manifest.json which is guaranteed for every apex
165 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", strings.Join(a.compatSymlinks, " && "))
166 }
Jiyong Park09d77522019-11-18 11:16:27 +0900167 }
168 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
169 }
170 }
171 return moduleNames
172}
173
Jiyong Park7afd1072019-12-30 16:56:33 +0900174func (a *apexBundle) writeRequiredModules(w io.Writer) {
175 var required []string
176 var targetRequired []string
177 var hostRequired []string
178 for _, fi := range a.filesInfo {
179 required = append(required, fi.requiredModuleNames...)
180 targetRequired = append(targetRequired, fi.targetRequiredModuleNames...)
181 hostRequired = append(hostRequired, fi.hostRequiredModuleNames...)
182 }
183
184 if len(required) > 0 {
185 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(required, " "))
186 }
187 if len(targetRequired) > 0 {
188 fmt.Fprintln(w, "LOCAL_TARGET_REQUIRED_MODULES +=", strings.Join(targetRequired, " "))
189 }
190 if len(hostRequired) > 0 {
191 fmt.Fprintln(w, "LOCAL_HOST_REQUIRED_MODULES +=", strings.Join(hostRequired, " "))
192 }
193}
194
Jiyong Park09d77522019-11-18 11:16:27 +0900195func (a *apexBundle) androidMkForType() android.AndroidMkData {
196 return android.AndroidMkData{
197 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
198 moduleNames := []string{}
199 apexType := a.properties.ApexType
200 if a.installable() {
201 apexName := proptools.StringDefault(a.properties.Apex_name, name)
202 moduleNames = a.androidMkForFiles(w, apexName, moduleDir)
203 }
204
205 if apexType == flattenedApex {
206 // Only image APEXes can be flattened.
207 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
208 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
209 fmt.Fprintln(w, "LOCAL_MODULE :=", name+a.suffix)
210 if len(moduleNames) > 0 {
211 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " "))
212 }
Jiyong Park7afd1072019-12-30 16:56:33 +0900213 a.writeRequiredModules(w)
Jiyong Park09d77522019-11-18 11:16:27 +0900214 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
Jiyong Park09d77522019-11-18 11:16:27 +0900215
216 } else {
217 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
218 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
219 fmt.Fprintln(w, "LOCAL_MODULE :=", name+a.suffix)
220 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
221 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFile.String())
222 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", a.installDir.ToMakePath().String())
223 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix())
224 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -0800225 fmt.Fprintln(w, "LOCAL_OVERRIDES_MODULES :=", strings.Join(a.overridableProperties.Overrides, " "))
Jiyong Park09d77522019-11-18 11:16:27 +0900226 if len(moduleNames) > 0 {
227 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
228 }
Jiyong Park956305c2020-01-09 12:32:06 +0900229 if len(a.requiredDeps) > 0 {
230 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.requiredDeps, " "))
Jiyong Park09d77522019-11-18 11:16:27 +0900231 }
Jiyong Park7afd1072019-12-30 16:56:33 +0900232 a.writeRequiredModules(w)
Jiyong Park09d77522019-11-18 11:16:27 +0900233 var postInstallCommands []string
234 if a.prebuiltFileToDelete != "" {
235 postInstallCommands = append(postInstallCommands, "rm -rf "+
236 filepath.Join(a.installDir.ToMakePath().String(), a.prebuiltFileToDelete))
237 }
238 // For unflattened apexes, compat symlinks are attached to apex package itself as LOCAL_POST_INSTALL_CMD
239 postInstallCommands = append(postInstallCommands, a.compatSymlinks...)
240 if len(postInstallCommands) > 0 {
241 fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", strings.Join(postInstallCommands, " && "))
242 }
243 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
244
245 if apexType == imageApex {
246 fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String())
247 }
Jiyong Park3a1602e2020-01-14 14:39:19 +0900248
249 if a.installedFilesFile != nil {
250 goal := "droidcore"
251 distFile := name + "-installed-files.txt"
252 fmt.Fprintln(w, ".PHONY:", goal)
253 fmt.Fprintf(w, "$(call dist-for-goals,%s,%s:%s)\n",
254 goal, a.installedFilesFile.String(), distFile)
255 }
Jiyong Park09d77522019-11-18 11:16:27 +0900256 }
257 }}
258}