androidmk.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. if len(fi.symlinks) > 0 {
  138. fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
  139. }
  140. newDataPaths := []android.DataPath{}
  141. for _, path := range fi.dataPaths {
  142. dataOutPath := modulePath + ":" + path.SrcPath.Rel()
  143. if ok := seenDataOutPaths[dataOutPath]; !ok {
  144. newDataPaths = append(newDataPaths, path)
  145. seenDataOutPaths[dataOutPath] = true
  146. }
  147. }
  148. if len(newDataPaths) > 0 {
  149. fmt.Fprintln(w, "LOCAL_TEST_DATA :=", strings.Join(android.AndroidMkDataPaths(newDataPaths), " "))
  150. }
  151. } else {
  152. modulePath = pathWhenActivated
  153. fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", pathWhenActivated)
  154. // For non-flattend APEXes, the merged notice file is attached to the APEX itself.
  155. // We don't need to have notice file for the individual modules in it. Otherwise,
  156. // we will have duplicated notice entries.
  157. fmt.Fprintln(w, "LOCAL_NO_NOTICE_FILE := true")
  158. }
  159. fmt.Fprintln(w, "LOCAL_SOONG_INSTALLED_MODULE :=", filepath.Join(modulePath, fi.stem()))
  160. fmt.Fprintln(w, "LOCAL_SOONG_INSTALL_PAIRS :=", fi.builtFile.String()+":"+filepath.Join(modulePath, fi.stem()))
  161. fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", fi.builtFile.String())
  162. fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", fi.class.nameInMake())
  163. if fi.module != nil {
  164. archStr := fi.module.Target().Arch.ArchType.String()
  165. host := false
  166. switch fi.module.Target().Os.Class {
  167. case android.Host:
  168. if fi.module.Target().HostCross {
  169. if fi.module.Target().Arch.ArchType != android.Common {
  170. fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
  171. }
  172. } else {
  173. if fi.module.Target().Arch.ArchType != android.Common {
  174. fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
  175. }
  176. }
  177. host = true
  178. case android.Device:
  179. if fi.module.Target().Arch.ArchType != android.Common {
  180. fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
  181. }
  182. }
  183. if host {
  184. makeOs := fi.module.Target().Os.String()
  185. if fi.module.Target().Os == android.Linux || fi.module.Target().Os == android.LinuxBionic || fi.module.Target().Os == android.LinuxMusl {
  186. makeOs = "linux"
  187. }
  188. fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", makeOs)
  189. fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
  190. }
  191. }
  192. if fi.jacocoReportClassesFile != nil {
  193. fmt.Fprintln(w, "LOCAL_SOONG_JACOCO_REPORT_CLASSES_JAR :=", fi.jacocoReportClassesFile.String())
  194. }
  195. switch fi.class {
  196. case javaSharedLib:
  197. // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
  198. // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
  199. // we will have foo.jar.jar
  200. fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.stem(), ".jar"))
  201. if javaModule, ok := fi.module.(java.ApexDependency); ok {
  202. fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", javaModule.ImplementationAndResourcesJars()[0].String())
  203. fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", javaModule.HeaderJars()[0].String())
  204. } else {
  205. fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", fi.builtFile.String())
  206. fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", fi.builtFile.String())
  207. }
  208. fmt.Fprintln(w, "LOCAL_SOONG_DEX_JAR :=", fi.builtFile.String())
  209. fmt.Fprintln(w, "LOCAL_DEX_PREOPT := false")
  210. fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_java_prebuilt.mk")
  211. case app:
  212. fmt.Fprintln(w, "LOCAL_CERTIFICATE :=", fi.certificate.AndroidMkString())
  213. // soong_app_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .apk Therefore
  214. // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
  215. // we will have foo.apk.apk
  216. fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", strings.TrimSuffix(fi.stem(), ".apk"))
  217. if app, ok := fi.module.(*java.AndroidApp); ok {
  218. if jniCoverageOutputs := app.JniCoverageOutputs(); len(jniCoverageOutputs) > 0 {
  219. fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", strings.Join(jniCoverageOutputs.Strings(), " "))
  220. }
  221. if jniLibSymbols := app.JNISymbolsInstalls(modulePath); len(jniLibSymbols) > 0 {
  222. fmt.Fprintln(w, "LOCAL_SOONG_JNI_LIBS_SYMBOLS :=", jniLibSymbols.String())
  223. }
  224. }
  225. fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_app_prebuilt.mk")
  226. case appSet:
  227. as, ok := fi.module.(*java.AndroidAppSet)
  228. if !ok {
  229. panic(fmt.Sprintf("Expected %s to be AndroidAppSet", fi.module))
  230. }
  231. fmt.Fprintln(w, "LOCAL_APK_SET_INSTALL_FILE :=", as.PackedAdditionalOutputs().String())
  232. fmt.Fprintln(w, "LOCAL_APKCERTS_FILE :=", as.APKCertsFile().String())
  233. fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_android_app_set.mk")
  234. case nativeSharedLib, nativeExecutable, nativeTest:
  235. fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.stem())
  236. if ccMod, ok := fi.module.(*cc.Module); ok {
  237. if ccMod.UnstrippedOutputFile() != nil {
  238. fmt.Fprintln(w, "LOCAL_SOONG_UNSTRIPPED_BINARY :=", ccMod.UnstrippedOutputFile().String())
  239. }
  240. ccMod.AndroidMkWriteAdditionalDependenciesForSourceAbiDiff(w)
  241. if ccMod.CoverageOutputFile().Valid() {
  242. fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", ccMod.CoverageOutputFile().String())
  243. }
  244. }
  245. fmt.Fprintln(w, "include $(BUILD_SYSTEM)/soong_cc_rust_prebuilt.mk")
  246. default:
  247. fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", fi.stem())
  248. if fi.builtFile == a.manifestPbOut && apexType == flattenedApex {
  249. if a.primaryApexType {
  250. // To install companion files (init_rc, vintf_fragments)
  251. // Copy some common properties of apexBundle to apex_manifest
  252. commonProperties := []string{
  253. "LOCAL_FULL_INIT_RC", "LOCAL_FULL_VINTF_FRAGMENTS",
  254. }
  255. for _, name := range commonProperties {
  256. if value, ok := apexAndroidMkData.Entries.EntryMap[name]; ok {
  257. fmt.Fprintln(w, name+" := "+strings.Join(value, " "))
  258. }
  259. }
  260. // Make apex_manifest.pb module for this APEX to override all other
  261. // modules in the APEXes being overridden by this APEX
  262. var patterns []string
  263. for _, o := range a.overridableProperties.Overrides {
  264. patterns = append(patterns, "%."+o+a.suffix)
  265. }
  266. if len(patterns) > 0 {
  267. fmt.Fprintln(w, "LOCAL_OVERRIDES_MODULES :=", strings.Join(patterns, " "))
  268. }
  269. }
  270. // File_contexts of flattened APEXes should be merged into file_contexts.bin
  271. fmt.Fprintln(w, "LOCAL_FILE_CONTEXTS :=", a.fileContexts)
  272. }
  273. fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
  274. }
  275. // m <module_name> will build <module_name>.<apex_name> as well.
  276. if fi.androidMkModuleName != moduleName && a.primaryApexType {
  277. fmt.Fprintf(w, ".PHONY: %s\n", fi.androidMkModuleName)
  278. fmt.Fprintf(w, "%s: %s\n", fi.androidMkModuleName, moduleName)
  279. }
  280. }
  281. return moduleNames
  282. }
  283. func (a *apexBundle) writeRequiredModules(w io.Writer, moduleNames []string) {
  284. if len(moduleNames) > 0 {
  285. fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(moduleNames, " "))
  286. }
  287. if len(a.requiredDeps) > 0 {
  288. fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(a.requiredDeps, " "))
  289. }
  290. var required []string
  291. var targetRequired []string
  292. var hostRequired []string
  293. required = append(required, a.RequiredModuleNames()...)
  294. targetRequired = append(targetRequired, a.TargetRequiredModuleNames()...)
  295. hostRequired = append(hostRequired, a.HostRequiredModuleNames()...)
  296. for _, fi := range a.filesInfo {
  297. required = append(required, fi.requiredModuleNames...)
  298. targetRequired = append(targetRequired, fi.targetRequiredModuleNames...)
  299. hostRequired = append(hostRequired, fi.hostRequiredModuleNames...)
  300. }
  301. if len(required) > 0 {
  302. fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES +=", strings.Join(required, " "))
  303. }
  304. if len(targetRequired) > 0 {
  305. fmt.Fprintln(w, "LOCAL_TARGET_REQUIRED_MODULES +=", strings.Join(targetRequired, " "))
  306. }
  307. if len(hostRequired) > 0 {
  308. fmt.Fprintln(w, "LOCAL_HOST_REQUIRED_MODULES +=", strings.Join(hostRequired, " "))
  309. }
  310. }
  311. func (a *apexBundle) androidMkForType() android.AndroidMkData {
  312. return android.AndroidMkData{
  313. Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
  314. moduleNames := []string{}
  315. apexType := a.properties.ApexType
  316. if a.installable() {
  317. apexName := proptools.StringDefault(a.properties.Apex_name, name)
  318. moduleNames = a.androidMkForFiles(w, name, apexName, moduleDir, data)
  319. }
  320. if apexType == flattenedApex {
  321. // Only image APEXes can be flattened.
  322. fmt.Fprintln(w, "\ninclude $(CLEAR_VARS) # apex.apexBundle.flat")
  323. fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
  324. fmt.Fprintln(w, "LOCAL_MODULE :=", name+a.suffix)
  325. data.Entries.WriteLicenseVariables(w)
  326. a.writeRequiredModules(w, moduleNames)
  327. fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
  328. } else {
  329. fmt.Fprintln(w, "\ninclude $(CLEAR_VARS) # apex.apexBundle")
  330. fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
  331. fmt.Fprintln(w, "LOCAL_MODULE :=", name+a.suffix)
  332. data.Entries.WriteLicenseVariables(w)
  333. fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") // do we need a new class?
  334. fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", a.outputFile.String())
  335. fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", a.installDir.String())
  336. stemSuffix := apexType.suffix()
  337. if a.isCompressed {
  338. stemSuffix = imageCapexSuffix
  339. }
  340. fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", name+stemSuffix)
  341. fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE :=", !a.installable())
  342. if a.installable() {
  343. fmt.Fprintln(w, "LOCAL_SOONG_INSTALLED_MODULE :=", a.installedFile.String())
  344. fmt.Fprintln(w, "LOCAL_SOONG_INSTALL_PAIRS :=", a.outputFile.String()+":"+a.installedFile.String())
  345. }
  346. // Because apex writes .mk with Custom(), we need to write manually some common properties
  347. // which are available via data.Entries
  348. commonProperties := []string{
  349. "LOCAL_FULL_INIT_RC", "LOCAL_FULL_VINTF_FRAGMENTS",
  350. "LOCAL_PROPRIETARY_MODULE", "LOCAL_VENDOR_MODULE", "LOCAL_ODM_MODULE", "LOCAL_PRODUCT_MODULE", "LOCAL_SYSTEM_EXT_MODULE",
  351. "LOCAL_MODULE_OWNER",
  352. }
  353. for _, name := range commonProperties {
  354. if value, ok := data.Entries.EntryMap[name]; ok {
  355. fmt.Fprintln(w, name+" := "+strings.Join(value, " "))
  356. }
  357. }
  358. if len(a.overridableProperties.Overrides) > 0 {
  359. fmt.Fprintln(w, "LOCAL_OVERRIDES_MODULES :=", strings.Join(a.overridableProperties.Overrides, " "))
  360. }
  361. a.writeRequiredModules(w, moduleNames)
  362. fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
  363. if apexType == imageApex {
  364. fmt.Fprintln(w, "ALL_MODULES.$(my_register_name).BUNDLE :=", a.bundleModuleFile.String())
  365. }
  366. if len(a.lintReports) > 0 {
  367. fmt.Fprintln(w, "ALL_MODULES.$(my_register_name).LINT_REPORTS :=",
  368. strings.Join(a.lintReports.Strings(), " "))
  369. }
  370. if a.installedFilesFile != nil {
  371. goal := "checkbuild"
  372. distFile := name + "-installed-files.txt"
  373. fmt.Fprintln(w, ".PHONY:", goal)
  374. fmt.Fprintf(w, "$(call dist-for-goals,%s,%s:%s)\n",
  375. goal, a.installedFilesFile.String(), distFile)
  376. fmt.Fprintf(w, "$(call declare-0p-target,%s)\n", a.installedFilesFile.String())
  377. }
  378. for _, dist := range data.Entries.GetDistForGoals(a) {
  379. fmt.Fprintf(w, dist)
  380. }
  381. distCoverageFiles(w, "ndk_apis_usedby_apex", a.nativeApisUsedByModuleFile.String())
  382. distCoverageFiles(w, "ndk_apis_backedby_apex", a.nativeApisBackedByModuleFile.String())
  383. distCoverageFiles(w, "java_apis_used_by_apex", a.javaApisUsedByModuleFile.String())
  384. }
  385. }}
  386. }
  387. func distCoverageFiles(w io.Writer, dir string, distfile string) {
  388. if distfile != "" {
  389. goal := "apps_only"
  390. fmt.Fprintf(w, "ifneq (,$(filter $(my_register_name),$(TARGET_BUILD_APPS)))\n"+
  391. " $(call dist-for-goals,%s,%s:%s/$(notdir %s))\n"+
  392. "endif\n", goal, distfile, dir, distfile)
  393. }
  394. }