androidmk.go 14 KB

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