Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 1 | // 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 | |
| 15 | package apex |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "io" |
| 20 | "path/filepath" |
| 21 | "strings" |
| 22 | |
| 23 | "android/soong/android" |
| 24 | "android/soong/cc" |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 25 | |
| 26 | "github.com/google/blueprint/proptools" |
| 27 | ) |
| 28 | |
| 29 | func (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 | |
| 45 | func (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 Park | 7cd10e3 | 2020-01-14 09:22:18 +0900 | [diff] [blame] | 55 | 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 Park | 7cd10e3 | 2020-01-14 09:22:18 +0900 | [diff] [blame] | 66 | |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 67 | for _, fi := range a.filesInfo { |
| 68 | if cc, ok := fi.module.(*cc.Module); ok && cc.Properties.HideFromMake { |
| 69 | continue |
| 70 | } |
| 71 | |
Jiyong Park | 7cd10e3 | 2020-01-14 09:22:18 +0900 | [diff] [blame] | 72 | 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 Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)") |
Jiyong Park | 1833cef | 2019-12-13 13:28:36 +0900 | [diff] [blame] | 91 | if fi.moduleDir != "" { |
| 92 | fmt.Fprintln(w, "LOCAL_PATH :=", fi.moduleDir) |
| 93 | } else { |
| 94 | fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir) |
| 95 | } |
Jiyong Park | 7cd10e3 | 2020-01-14 09:22:18 +0900 | [diff] [blame] | 96 | fmt.Fprintln(w, "LOCAL_MODULE :=", moduleName) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 97 | // /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) |
| 115 | } |
| 116 | fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String()) |
| 117 | fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.NameInMake()) |
| 118 | if fi.module != nil { |
| 119 | archStr := fi.module.Target().Arch.ArchType.String() |
| 120 | host := false |
| 121 | switch fi.module.Target().Os.Class { |
| 122 | case android.Host: |
| 123 | if fi.module.Target().Arch.ArchType != android.Common { |
| 124 | fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr) |
| 125 | } |
| 126 | host = true |
| 127 | case android.HostCross: |
| 128 | if fi.module.Target().Arch.ArchType != android.Common { |
| 129 | fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr) |
| 130 | } |
| 131 | host = true |
| 132 | case android.Device: |
| 133 | if fi.module.Target().Arch.ArchType != android.Common { |
| 134 | fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr) |
| 135 | } |
| 136 | } |
| 137 | if host { |
| 138 | makeOs := fi.module.Target().Os.String() |
| 139 | if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic { |
| 140 | makeOs = "linux" |
| 141 | } |
| 142 | fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs) |
| 143 | fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true") |
| 144 | } |
| 145 | } |
Jiyong Park | 618922e | 2020-01-08 13:35:43 +0900 | [diff] [blame] | 146 | if fi.jacocoReportClassesFile != nil { |
| 147 | fmt.Fprintln(w, "LOCAL_SOONG_JACOCO_REPORT_CLASSES_JAR :=", fi.jacocoReportClassesFile.String()) |
| 148 | } |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 149 | if fi.class == javaSharedLib { |
Jooyung Han | 58f26ab | 2019-12-18 15:34:32 +0900 | [diff] [blame] | 150 | javaModule := fi.module.(javaLibrary) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 151 | // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore |
| 152 | // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise |
| 153 | // we will have foo.jar.jar |
| 154 | fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".jar")) |
| 155 | fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String()) |
| 156 | fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String()) |
| 157 | fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String()) |
| 158 | fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false") |
| 159 | fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk") |
Jiyong Park | 618922e | 2020-01-08 13:35:43 +0900 | [diff] [blame] | 160 | } else if fi.class == app { |
| 161 | // soong_app_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .apk Therefore |
| 162 | // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise |
| 163 | // we will have foo.apk.apk |
| 164 | fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.builtFile.Base(), ".apk")) |
| 165 | fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_app_prebuilt.mk") |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 166 | } else if fi.class == nativeSharedLib || fi.class == nativeExecutable || fi.class == nativeTest { |
| 167 | fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base()) |
| 168 | if cc, ok := fi.module.(*cc.Module); ok { |
| 169 | if cc.UnstrippedOutputFile() != nil { |
| 170 | fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", cc.UnstrippedOutputFile().String()) |
| 171 | } |
| 172 | cc.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w) |
| 173 | if cc.CoverageOutputFile().Valid() { |
| 174 | fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", cc.CoverageOutputFile().String()) |
| 175 | } |
| 176 | } |
| 177 | fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_prebuilt.mk") |
| 178 | } else { |
| 179 | fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.builtFile.Base()) |
Jooyung Han | 75de261 | 2020-01-24 02:02:45 +0900 | [diff] [blame^] | 180 | if fi.builtFile == a.manifestPbOut { |
| 181 | if a.primaryApexType { |
| 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, " ")) |
Jiyong Park | 0abc1b4 | 2020-01-09 17:43:39 +0900 | [diff] [blame] | 189 | |
Jooyung Han | 75de261 | 2020-01-24 02:02:45 +0900 | [diff] [blame^] | 190 | if apexType == flattenedApex && len(a.compatSymlinks) > 0 { |
| 191 | // For flattened apexes, compat symlinks are attached to apex_manifest.json which is guaranteed for every apex |
| 192 | postInstallCommands = append(postInstallCommands, a.compatSymlinks...) |
| 193 | } |
| 194 | } |
| 195 | if len(postInstallCommands) > 0 { |
Jiyong Park | 7cd10e3 | 2020-01-14 09:22:18 +0900 | [diff] [blame] | 196 | fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", strings.Join(postInstallCommands, " && ")) |
Jiyong Park | 0abc1b4 | 2020-01-09 17:43:39 +0900 | [diff] [blame] | 197 | } |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 198 | } |
| 199 | fmt.Fprintln(w, "include $(BUILD_PREBUILT)") |
| 200 | } |
| 201 | } |
| 202 | return moduleNames |
| 203 | } |
| 204 | |
Jiyong Park | 7afd107 | 2019-12-30 16:56:33 +0900 | [diff] [blame] | 205 | func (a *apexBundle) writeRequiredModules(w io.Writer) { |
| 206 | var required []string |
| 207 | var targetRequired []string |
| 208 | var hostRequired []string |
| 209 | for _, fi := range a.filesInfo { |
| 210 | required = append(required, fi.requiredModuleNames...) |
| 211 | targetRequired = append(targetRequired, fi.targetRequiredModuleNames...) |
| 212 | hostRequired = append(hostRequired, fi.hostRequiredModuleNames...) |
| 213 | } |
| 214 | |
| 215 | if len(required) > 0 { |
| 216 | fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(required, " ")) |
| 217 | } |
| 218 | if len(targetRequired) > 0 { |
| 219 | fmt.Fprintln(w, "LOCAL_TARGET_REQUIRED_MODULES +=", strings.Join(targetRequired, " ")) |
| 220 | } |
| 221 | if len(hostRequired) > 0 { |
| 222 | fmt.Fprintln(w, "LOCAL_HOST_REQUIRED_MODULES +=", strings.Join(hostRequired, " ")) |
| 223 | } |
| 224 | } |
| 225 | |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 226 | func (a *apexBundle) androidMkForType() android.AndroidMkData { |
| 227 | return android.AndroidMkData{ |
| 228 | Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) { |
| 229 | moduleNames := []string{} |
| 230 | apexType := a.properties.ApexType |
| 231 | if a.installable() { |
| 232 | apexName := proptools.StringDefault(a.properties.Apex_name, name) |
| 233 | moduleNames = a.androidMkForFiles(w, apexName, moduleDir) |
| 234 | } |
| 235 | |
| 236 | if apexType == flattenedApex { |
| 237 | // Only image APEXes can be flattened. |
| 238 | fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)") |
| 239 | fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir) |
| 240 | fmt.Fprintln(w, "LOCAL_MODULE :=", name+a.suffix) |
| 241 | if len(moduleNames) > 0 { |
| 242 | fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(moduleNames, " ")) |
| 243 | } |
Jiyong Park | 7afd107 | 2019-12-30 16:56:33 +0900 | [diff] [blame] | 244 | a.writeRequiredModules(w) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 245 | fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)") |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 246 | |
| 247 | } else { |
| 248 | fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)") |
| 249 | fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir) |
| 250 | fmt.Fprintln(w, "LOCAL_MODULE :=", name+a.suffix) |
| 251 | fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class? |
| 252 | fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFile.String()) |
| 253 | fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", a.installDir.ToMakePath().String()) |
| 254 | fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+apexType.suffix()) |
| 255 | fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable()) |
Jaewoong Jung | 7abcf8e | 2019-12-19 17:32:06 -0800 | [diff] [blame] | 256 | fmt.Fprintln(w, "LOCAL_OVERRIDES_MODULES :=", strings.Join(a.overridableProperties.Overrides, " ")) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 257 | if len(moduleNames) > 0 { |
| 258 | fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " ")) |
| 259 | } |
Jiyong Park | 956305c | 2020-01-09 12:32:06 +0900 | [diff] [blame] | 260 | if len(a.requiredDeps) > 0 { |
| 261 | fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.requiredDeps, " ")) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 262 | } |
Jiyong Park | 7afd107 | 2019-12-30 16:56:33 +0900 | [diff] [blame] | 263 | a.writeRequiredModules(w) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 264 | var postInstallCommands []string |
| 265 | if a.prebuiltFileToDelete != "" { |
| 266 | postInstallCommands = append(postInstallCommands, "rm -rf "+ |
| 267 | filepath.Join(a.installDir.ToMakePath().String(), a.prebuiltFileToDelete)) |
| 268 | } |
| 269 | // For unflattened apexes, compat symlinks are attached to apex package itself as LOCAL_POST_INSTALL_CMD |
| 270 | postInstallCommands = append(postInstallCommands, a.compatSymlinks...) |
| 271 | if len(postInstallCommands) > 0 { |
| 272 | fmt.Fprintln(w, "LOCAL_POST_INSTALL_CMD :=", strings.Join(postInstallCommands, " && ")) |
| 273 | } |
| 274 | fmt.Fprintln(w, "include $(BUILD_PREBUILT)") |
| 275 | |
| 276 | if apexType == imageApex { |
| 277 | fmt.Fprintln(w, "ALL_MODULES.$(LOCAL_MODULE).BUNDLE :=", a.bundleModuleFile.String()) |
| 278 | } |
Jiyong Park | 3a1602e | 2020-01-14 14:39:19 +0900 | [diff] [blame] | 279 | |
| 280 | if a.installedFilesFile != nil { |
| 281 | goal := "droidcore" |
| 282 | distFile := name + "-installed-files.txt" |
| 283 | fmt.Fprintln(w, ".PHONY:", goal) |
| 284 | fmt.Fprintf(w, "$(call dist-for-goals,%s,%s:%s)\n", |
| 285 | goal, a.installedFilesFile.String(), distFile) |
| 286 | } |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 287 | } |
| 288 | }} |
| 289 | } |