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 15package apex 16 17import ( 18 "fmt" 19 "io" 20 "path/filepath" 21 "strings" 22 23 "android/soong/android" 24 "android/soong/cc" 25 "android/soong/java" 26 "android/soong/rust" 27) 28 29func (a *apexBundle) AndroidMk() android.AndroidMkData { 30 if a.properties.HideFromMake { 31 return android.AndroidMkData{ 32 Disabled: true, 33 } 34 } 35 return a.androidMkForType() 36} 37 38// nameInMake converts apexFileClass into the corresponding class name in Make. 39func (class apexFileClass) nameInMake() string { 40 switch class { 41 case etc: 42 return "ETC" 43 case nativeSharedLib: 44 return "SHARED_LIBRARIES" 45 case nativeExecutable, shBinary, pyBinary, goBinary: 46 return "EXECUTABLES" 47 case javaSharedLib: 48 return "JAVA_LIBRARIES" 49 case nativeTest: 50 return "NATIVE_TESTS" 51 case app, appSet: 52 // b/142537672 Why isn't this APP? We want to have full control over 53 // the paths and file names of the apk file under the flattend APEX. 54 // If this is set to APP, then the paths and file names are modified 55 // by the Make build system. For example, it is installed to 56 // /system/apex/<apexname>/app/<Appname>/<apexname>.<Appname>/ instead of 57 // /system/apex/<apexname>/app/<Appname> because the build system automatically 58 // appends module name (which is <apexname>.<Appname> to the path. 59 return "ETC" 60 default: 61 panic(fmt.Errorf("unknown class %d", class)) 62 } 63} 64 65// Return the full module name for a dependency module, which appends the apex module name unless re-using a system lib. 66func (a *apexBundle) fullModuleName(apexBundleName string, linkToSystemLib bool, fi *apexFile) string { 67 if linkToSystemLib { 68 return fi.androidMkModuleName 69 } 70 return fi.androidMkModuleName + "." + apexBundleName + a.suffix 71} 72 73// androidMkForFiles generates Make definitions for the contents of an 74// apexBundle (apexBundle#filesInfo). The filesInfo structure can either be 75// populated by Soong for unconverted APEXes, or Bazel in mixed mode. Use 76// apexFile#isBazelPrebuilt to differentiate. 77func (a *apexBundle) androidMkForFiles(w io.Writer, apexBundleName, moduleDir string, 78 apexAndroidMkData android.AndroidMkData) []string { 79 80 // apexBundleName comes from the 'name' property or soong module. 81 // apexName comes from 'name' property of apex_manifest. 82 // An apex is installed to /system/apex/<apexBundleName> and is activated at /apex/<apexName> 83 // In many cases, the two names are the same, but could be different in general. 84 // However, symbol files for apex files are installed under /apex/<apexBundleName> to avoid 85 // conflicts between two apexes with the same apexName. 86 87 moduleNames := []string{} 88 apexType := a.properties.ApexType 89 // To avoid creating duplicate build rules, run this function only when primaryApexType is true 90 // to install symbol files in $(PRODUCT_OUT}/apex. 91 // And if apexType is flattened, run this function to install files in $(PRODUCT_OUT}/system/apex. 92 if !a.primaryApexType && apexType != flattenedApex { 93 return moduleNames 94 } 95 96 seenDataOutPaths := make(map[string]bool) 97 98 for _, fi := range a.filesInfo { 99 linkToSystemLib := a.linkToSystemLib && fi.transitiveDep && fi.availableToPlatform() 100 moduleName := a.fullModuleName(apexBundleName, linkToSystemLib, &fi) 101 102 // This name will be added to LOCAL_REQUIRED_MODULES of the APEX. We need to be 103 // arch-specific otherwise we will end up installing both ABIs even when only 104 // either of the ABI is requested. 105 aName := moduleName 106 switch fi.multilib { 107 case "lib32": 108 aName = aName + ":32" 109 case "lib64": 110 aName = aName + ":64" 111 } 112 if !android.InList(aName, moduleNames) { 113 moduleNames = append(moduleNames, aName) 114 } 115 116 if linkToSystemLib { 117 // No need to copy the file since it's linked to the system file 118 continue 119 } 120 121 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS) # apex.apexBundle.files") 122 if fi.moduleDir != "" { 123 fmt.Fprintln(w, "LOCAL_PATH :=", fi.moduleDir) 124 } else { 125 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir) 126 } 127 fmt.Fprintln(w, "LOCAL_MODULE :=", moduleName) 128 129 if fi.module != nil && fi.module.Owner() != "" { 130 fmt.Fprintln(w, "LOCAL_MODULE_OWNER :=", fi.module.Owner()) 131 } 132 // /apex/<apexBundleName>/{lib|framework|...} 133 pathForSymbol := filepath.Join("$(PRODUCT_OUT)", "apex", apexBundleName, fi.installDir) 134 var modulePath string 135 if apexType == flattenedApex { 136 // /system/apex/<apexBundleName>/{lib|framework|...} 137 modulePath = filepath.Join(a.installDir.String(), apexBundleName, fi.installDir) 138 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", modulePath) 139 if a.primaryApexType { 140 fmt.Fprintln(w, "LOCAL_SOONG_SYMBOL_PATH :=", pathForSymbol) 141 } 142 android.AndroidMkEmitAssignList(w, "LOCAL_MODULE_SYMLINKS", fi.symlinks) 143 newDataPaths := []android.DataPath{} 144 for _, path := range fi.dataPaths { 145 dataOutPath := modulePath + ":" + path.SrcPath.Rel() 146 if ok := seenDataOutPaths[dataOutPath]; !ok { 147 newDataPaths = append(newDataPaths, path) 148 seenDataOutPaths[dataOutPath] = true 149 } 150 } 151 android.AndroidMkEmitAssignList(w, "LOCAL_TEST_DATA", android.AndroidMkDataPaths(newDataPaths)) 152 } else { 153 modulePath = pathForSymbol 154 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", modulePath) 155 156 // For non-flattend APEXes, the merged notice file is attached to the APEX itself. 157 // We don't need to have notice file for the individual modules in it. Otherwise, 158 // we will have duplicated notice entries. 159 fmt.Fprintln(w, "LOCAL_NO_NOTICE_FILE := true") 160 } 161 fmt.Fprintln(w, "LOCAL_SOONG_INSTALLED_MODULE :=", filepath.Join(modulePath, fi.stem())) 162 fmt.Fprintln(w, "LOCAL_SOONG_INSTALL_PAIRS :=", fi.builtFile.String()+":"+filepath.Join(modulePath, fi.stem())) 163 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String()) 164 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.nameInMake()) 165 if fi.module != nil { 166 // This apexFile's module comes from Soong 167 archStr := fi.module.Target().Arch.ArchType.String() 168 host := false 169 switch fi.module.Target().Os.Class { 170 case android.Host: 171 if fi.module.Target().HostCross { 172 if fi.module.Target().Arch.ArchType != android.Common { 173 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr) 174 } 175 } else { 176 if fi.module.Target().Arch.ArchType != android.Common { 177 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr) 178 } 179 } 180 host = true 181 case android.Device: 182 if fi.module.Target().Arch.ArchType != android.Common { 183 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr) 184 } 185 } 186 if host { 187 makeOs := fi.module.Target().Os.String() 188 if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic || fi.module.Target().Os == android.LinuxMusl { 189 makeOs = "linux" 190 } 191 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs) 192 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true") 193 } 194 } else if fi.isBazelPrebuilt && fi.arch != "" { 195 // This apexFile comes from Bazel 196 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", fi.arch) 197 } 198 if fi.jacocoReportClassesFile != nil { 199 fmt.Fprintln(w, "LOCAL_SOONG_JACOCO_REPORT_CLASSES_JAR :=", fi.jacocoReportClassesFile.String()) 200 } 201 switch fi.class { 202 case javaSharedLib: 203 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore 204 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise 205 // we will have foo.jar.jar 206 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.stem(), ".jar")) 207 if javaModule, ok := fi.module.(java.ApexDependency); ok { 208 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String()) 209 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String()) 210 } else { 211 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", fi.builtFile.String()) 212 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", fi.builtFile.String()) 213 } 214 fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String()) 215 fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false") 216 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk") 217 case app: 218 fmt.Fprintln(w, "LOCAL_CERTIFICATE :=", fi.certificate.AndroidMkString()) 219 // soong_app_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .apk Therefore 220 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise 221 // we will have foo.apk.apk 222 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.stem(), ".apk")) 223 if app, ok := fi.module.(*java.AndroidApp); ok { 224 android.AndroidMkEmitAssignList(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE", app.JniCoverageOutputs().Strings()) 225 if jniLibSymbols := app.JNISymbolsInstalls(modulePath); len(jniLibSymbols) > 0 { 226 fmt.Fprintln(w, "LOCAL_SOONG_JNI_LIBS_SYMBOLS :=", jniLibSymbols.String()) 227 } 228 } 229 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_app_prebuilt.mk") 230 case appSet: 231 as, ok := fi.module.(*java.AndroidAppSet) 232 if !ok { 233 panic(fmt.Sprintf("Expected %s to be AndroidAppSet", fi.module)) 234 } 235 fmt.Fprintln(w, "LOCAL_APK_SET_INSTALL_FILE :=", as.PackedAdditionalOutputs().String()) 236 fmt.Fprintln(w, "LOCAL_APKCERTS_FILE :=", as.APKCertsFile().String()) 237 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_android_app_set.mk") 238 case nativeSharedLib, nativeExecutable, nativeTest: 239 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.stem()) 240 if fi.isBazelPrebuilt { 241 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", fi.unstrippedBuiltFile) 242 } else { 243 if ccMod, ok := fi.module.(*cc.Module); ok { 244 if ccMod.UnstrippedOutputFile() != nil { 245 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", ccMod.UnstrippedOutputFile().String()) 246 } 247 ccMod.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w) 248 if ccMod.CoverageOutputFile().Valid() { 249 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", ccMod.CoverageOutputFile().String()) 250 } 251 } else if rustMod, ok := fi.module.(*rust.Module); ok { 252 if rustMod.UnstrippedOutputFile() != nil { 253 fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", rustMod.UnstrippedOutputFile().String()) 254 } 255 } 256 } 257 fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_rust_prebuilt.mk") 258 default: 259 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.stem()) 260 if fi.builtFile == a.manifestPbOut && apexType == flattenedApex { 261 if a.primaryApexType { 262 // To install companion files (init_rc, vintf_fragments) 263 // Copy some common properties of apexBundle to apex_manifest 264 commonProperties := []string{ 265 "LOCAL_FULL_INIT_RC", "LOCAL_FULL_VINTF_FRAGMENTS", 266 } 267 for _, name := range commonProperties { 268 if value, ok := apexAndroidMkData.Entries.EntryMap[name]; ok { 269 android.AndroidMkEmitAssignList(w, name, value) 270 } 271 } 272 273 // Make apex_manifest.pb module for this APEX to override all other 274 // modules in the APEXes being overridden by this APEX 275 var patterns []string 276 for _, o := range a.overridableProperties.Overrides { 277 patterns = append(patterns, "%."+o+a.suffix) 278 } 279 android.AndroidMkEmitAssignList(w, "LOCAL_OVERRIDES_MODULES", patterns) 280 } 281 282 // File_contexts of flattened APEXes should be merged into file_contexts.bin 283 fmt.Fprintln(w, "LOCAL_FILE_CONTEXTS :=", a.fileContexts) 284 } 285 fmt.Fprintln(w, "include $(BUILD_PREBUILT)") 286 } 287 288 // m <module_name> will build <module_name>.<apex_name> as well. 289 if fi.androidMkModuleName != moduleName && a.primaryApexType { 290 fmt.Fprintf(w, ".PHONY: %s\n", fi.androidMkModuleName) 291 fmt.Fprintf(w, "%s: %s\n", fi.androidMkModuleName, moduleName) 292 } 293 } 294 return moduleNames 295} 296 297func (a *apexBundle) writeRequiredModules(w io.Writer, moduleNames []string) { 298 var required []string 299 var targetRequired []string 300 var hostRequired []string 301 required = append(required, a.RequiredModuleNames()...) 302 targetRequired = append(targetRequired, a.TargetRequiredModuleNames()...) 303 hostRequired = append(hostRequired, a.HostRequiredModuleNames()...) 304 for _, fi := range a.filesInfo { 305 required = append(required, fi.requiredModuleNames...) 306 targetRequired = append(targetRequired, fi.targetRequiredModuleNames...) 307 hostRequired = append(hostRequired, fi.hostRequiredModuleNames...) 308 } 309 android.AndroidMkEmitAssignList(w, "LOCAL_REQUIRED_MODULES", moduleNames, a.makeModulesToInstall, required) 310 android.AndroidMkEmitAssignList(w, "LOCAL_TARGET_REQUIRED_MODULES", targetRequired) 311 android.AndroidMkEmitAssignList(w, "LOCAL_HOST_REQUIRED_MODULES", hostRequired) 312} 313 314func (a *apexBundle) androidMkForType() android.AndroidMkData { 315 return android.AndroidMkData{ 316 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) { 317 moduleNames := []string{} 318 apexType := a.properties.ApexType 319 if a.installable() { 320 moduleNames = a.androidMkForFiles(w, name, moduleDir, data) 321 } 322 323 if apexType == flattenedApex { 324 // Only image APEXes can be flattened. 325 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS) # apex.apexBundle.flat") 326 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir) 327 fmt.Fprintln(w, "LOCAL_MODULE :=", name+a.suffix) 328 data.Entries.WriteLicenseVariables(w) 329 a.writeRequiredModules(w, moduleNames) 330 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)") 331 332 } else { 333 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS) # apex.apexBundle") 334 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir) 335 fmt.Fprintln(w, "LOCAL_MODULE :=", name+a.suffix) 336 data.Entries.WriteLicenseVariables(w) 337 fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class? 338 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFile.String()) 339 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", a.installDir.String()) 340 stemSuffix := apexType.suffix() 341 if a.isCompressed { 342 stemSuffix = imageCapexSuffix 343 } 344 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+stemSuffix) 345 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable()) 346 if a.installable() { 347 fmt.Fprintln(w, "LOCAL_SOONG_INSTALLED_MODULE :=", a.installedFile.String()) 348 fmt.Fprintln(w, "LOCAL_SOONG_INSTALL_PAIRS :=", a.outputFile.String()+":"+a.installedFile.String()) 349 } 350 351 // Because apex writes .mk with Custom(), we need to write manually some common properties 352 // which are available via data.Entries 353 commonProperties := []string{ 354 "LOCAL_FULL_INIT_RC", "LOCAL_FULL_VINTF_FRAGMENTS", 355 "LOCAL_PROPRIETARY_MODULE", "LOCAL_VENDOR_MODULE", "LOCAL_ODM_MODULE", "LOCAL_PRODUCT_MODULE", "LOCAL_SYSTEM_EXT_MODULE", 356 "LOCAL_MODULE_OWNER", 357 } 358 for _, name := range commonProperties { 359 if value, ok := data.Entries.EntryMap[name]; ok { 360 android.AndroidMkEmitAssignList(w, name, value) 361 } 362 } 363 364 android.AndroidMkEmitAssignList(w, "LOCAL_OVERRIDES_MODULES", a.overridableProperties.Overrides) 365 a.writeRequiredModules(w, moduleNames) 366 367 fmt.Fprintln(w, "include $(BUILD_PREBUILT)") 368 369 if apexType == imageApex { 370 fmt.Fprintln(w, "ALL_MODULES.$(my_register_name).BUNDLE :=", a.bundleModuleFile.String()) 371 } 372 android.AndroidMkEmitAssignList(w, "ALL_MODULES.$(my_register_name).LINT_REPORTS", a.lintReports.Strings()) 373 374 if a.installedFilesFile != nil { 375 goal := "checkbuild" 376 distFile := name + "-installed-files.txt" 377 fmt.Fprintln(w, ".PHONY:", goal) 378 fmt.Fprintf(w, "$(call dist-for-goals,%s,%s:%s)\n", 379 goal, a.installedFilesFile.String(), distFile) 380 fmt.Fprintf(w, "$(call declare-0p-target,%s)\n", a.installedFilesFile.String()) 381 } 382 for _, dist := range data.Entries.GetDistForGoals(a) { 383 fmt.Fprintf(w, dist) 384 } 385 386 distCoverageFiles(w, "ndk_apis_usedby_apex", a.nativeApisUsedByModuleFile.String()) 387 distCoverageFiles(w, "ndk_apis_backedby_apex", a.nativeApisBackedByModuleFile.String()) 388 distCoverageFiles(w, "java_apis_used_by_apex", a.javaApisUsedByModuleFile.String()) 389 } 390 }} 391} 392 393func distCoverageFiles(w io.Writer, dir string, distfile string) { 394 if distfile != "" { 395 goal := "apps_only" 396 fmt.Fprintf(w, "ifneq (,$(filter $(my_register_name),$(TARGET_BUILD_APPS)))\n"+ 397 " $(call dist-for-goals,%s,%s:%s/$(notdir %s))\n"+ 398 "endif\n", goal, distfile, dir, distfile) 399 } 400} 401