androidmk.go 16 KB

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