androidmk.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. // Copyright 2015 Google Inc. All rights reserved.
  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. // This file offers AndroidMkEntriesProvider, which individual modules implement to output
  15. // Android.mk entries that contain information about the modules built through Soong. Kati reads
  16. // and combines them with the legacy Make-based module definitions to produce the complete view of
  17. // the source tree, which makes this a critical point of Make-Soong interoperability.
  18. //
  19. // Naturally, Soong-only builds do not rely on this mechanism.
  20. package android
  21. import (
  22. "bytes"
  23. "fmt"
  24. "io"
  25. "io/ioutil"
  26. "os"
  27. "path/filepath"
  28. "reflect"
  29. "sort"
  30. "strings"
  31. "github.com/google/blueprint"
  32. "github.com/google/blueprint/bootstrap"
  33. )
  34. func init() {
  35. RegisterAndroidMkBuildComponents(InitRegistrationContext)
  36. }
  37. func RegisterAndroidMkBuildComponents(ctx RegistrationContext) {
  38. ctx.RegisterSingletonType("androidmk", AndroidMkSingleton)
  39. }
  40. // Enable androidmk support.
  41. // * Register the singleton
  42. // * Configure that we are inside make
  43. var PrepareForTestWithAndroidMk = GroupFixturePreparers(
  44. FixtureRegisterWithContext(RegisterAndroidMkBuildComponents),
  45. FixtureModifyConfig(SetKatiEnabledForTests),
  46. )
  47. // Deprecated: Use AndroidMkEntriesProvider instead, especially if you're not going to use the
  48. // Custom function. It's easier to use and test.
  49. type AndroidMkDataProvider interface {
  50. AndroidMk() AndroidMkData
  51. BaseModuleName() string
  52. }
  53. type AndroidMkData struct {
  54. Class string
  55. SubName string
  56. DistFiles TaggedDistFiles
  57. OutputFile OptionalPath
  58. Disabled bool
  59. Include string
  60. Required []string
  61. Host_required []string
  62. Target_required []string
  63. Custom func(w io.Writer, name, prefix, moduleDir string, data AndroidMkData)
  64. Extra []AndroidMkExtraFunc
  65. Entries AndroidMkEntries
  66. }
  67. type AndroidMkExtraFunc func(w io.Writer, outputFile Path)
  68. // Interface for modules to declare their Android.mk outputs. Note that every module needs to
  69. // implement this in order to be included in the final Android-<product_name>.mk output, even if
  70. // they only need to output the common set of entries without any customizations.
  71. type AndroidMkEntriesProvider interface {
  72. // Returns AndroidMkEntries objects that contain all basic info plus extra customization data
  73. // if needed. This is the core func to implement.
  74. // Note that one can return multiple objects. For example, java_library may return an additional
  75. // AndroidMkEntries object for its hostdex sub-module.
  76. AndroidMkEntries() []AndroidMkEntries
  77. // Modules don't need to implement this as it's already implemented by ModuleBase.
  78. // AndroidMkEntries uses BaseModuleName() instead of ModuleName() because certain modules
  79. // e.g. Prebuilts, override the Name() func and return modified names.
  80. // If a different name is preferred, use SubName or OverrideName in AndroidMkEntries.
  81. BaseModuleName() string
  82. }
  83. // The core data struct that modules use to provide their Android.mk data.
  84. type AndroidMkEntries struct {
  85. // Android.mk class string, e.g EXECUTABLES, JAVA_LIBRARIES, ETC
  86. Class string
  87. // Optional suffix to append to the module name. Useful when a module wants to return multiple
  88. // AndroidMkEntries objects. For example, when a java_library returns an additional entry for
  89. // its hostdex sub-module, this SubName field is set to "-hostdex" so that it can have a
  90. // different name than the parent's.
  91. SubName string
  92. // If set, this value overrides the base module name. SubName is still appended.
  93. OverrideName string
  94. // Dist files to output
  95. DistFiles TaggedDistFiles
  96. // The output file for Kati to process and/or install. If absent, the module is skipped.
  97. OutputFile OptionalPath
  98. // If true, the module is skipped and does not appear on the final Android-<product name>.mk
  99. // file. Useful when a module needs to be skipped conditionally.
  100. Disabled bool
  101. // The postprocessing mk file to include, e.g. $(BUILD_SYSTEM)/soong_cc_prebuilt.mk
  102. // If not set, $(BUILD_SYSTEM)/prebuilt.mk is used.
  103. Include string
  104. // Required modules that need to be built and included in the final build output when building
  105. // this module.
  106. Required []string
  107. // Required host modules that need to be built and included in the final build output when
  108. // building this module.
  109. Host_required []string
  110. // Required device modules that need to be built and included in the final build output when
  111. // building this module.
  112. Target_required []string
  113. header bytes.Buffer
  114. footer bytes.Buffer
  115. // Funcs to append additional Android.mk entries or modify the common ones. Multiple funcs are
  116. // accepted so that common logic can be factored out as a shared func.
  117. ExtraEntries []AndroidMkExtraEntriesFunc
  118. // Funcs to add extra lines to the module's Android.mk output. Unlike AndroidMkExtraEntriesFunc,
  119. // which simply sets Make variable values, this can be used for anything since it can write any
  120. // Make statements directly to the final Android-*.mk file.
  121. // Primarily used to call macros or declare/update Make targets.
  122. ExtraFooters []AndroidMkExtraFootersFunc
  123. // A map that holds the up-to-date Make variable values. Can be accessed from tests.
  124. EntryMap map[string][]string
  125. // A list of EntryMap keys in insertion order. This serves a few purposes:
  126. // 1. Prevents churns. Golang map doesn't provide consistent iteration order, so without this,
  127. // the outputted Android-*.mk file may change even though there have been no content changes.
  128. // 2. Allows modules to refer to other variables, like LOCAL_BAR_VAR := $(LOCAL_FOO_VAR),
  129. // without worrying about the variables being mixed up in the actual mk file.
  130. // 3. Makes troubleshooting and spotting errors easier.
  131. entryOrder []string
  132. }
  133. type AndroidMkExtraEntriesContext interface {
  134. Provider(provider blueprint.ProviderKey) interface{}
  135. }
  136. type androidMkExtraEntriesContext struct {
  137. ctx fillInEntriesContext
  138. mod blueprint.Module
  139. }
  140. func (a *androidMkExtraEntriesContext) Provider(provider blueprint.ProviderKey) interface{} {
  141. return a.ctx.ModuleProvider(a.mod, provider)
  142. }
  143. type AndroidMkExtraEntriesFunc func(ctx AndroidMkExtraEntriesContext, entries *AndroidMkEntries)
  144. type AndroidMkExtraFootersFunc func(w io.Writer, name, prefix, moduleDir string)
  145. // Utility funcs to manipulate Android.mk variable entries.
  146. // SetString sets a Make variable with the given name to the given value.
  147. func (a *AndroidMkEntries) SetString(name, value string) {
  148. if _, ok := a.EntryMap[name]; !ok {
  149. a.entryOrder = append(a.entryOrder, name)
  150. }
  151. a.EntryMap[name] = []string{value}
  152. }
  153. // SetPath sets a Make variable with the given name to the given path string.
  154. func (a *AndroidMkEntries) SetPath(name string, path Path) {
  155. if _, ok := a.EntryMap[name]; !ok {
  156. a.entryOrder = append(a.entryOrder, name)
  157. }
  158. a.EntryMap[name] = []string{path.String()}
  159. }
  160. // SetOptionalPath sets a Make variable with the given name to the given path string if it is valid.
  161. // It is a no-op if the given path is invalid.
  162. func (a *AndroidMkEntries) SetOptionalPath(name string, path OptionalPath) {
  163. if path.Valid() {
  164. a.SetPath(name, path.Path())
  165. }
  166. }
  167. // AddPath appends the given path string to a Make variable with the given name.
  168. func (a *AndroidMkEntries) AddPath(name string, path Path) {
  169. if _, ok := a.EntryMap[name]; !ok {
  170. a.entryOrder = append(a.entryOrder, name)
  171. }
  172. a.EntryMap[name] = append(a.EntryMap[name], path.String())
  173. }
  174. // AddOptionalPath appends the given path string to a Make variable with the given name if it is
  175. // valid. It is a no-op if the given path is invalid.
  176. func (a *AndroidMkEntries) AddOptionalPath(name string, path OptionalPath) {
  177. if path.Valid() {
  178. a.AddPath(name, path.Path())
  179. }
  180. }
  181. // SetPaths sets a Make variable with the given name to a slice of the given path strings.
  182. func (a *AndroidMkEntries) SetPaths(name string, paths Paths) {
  183. if _, ok := a.EntryMap[name]; !ok {
  184. a.entryOrder = append(a.entryOrder, name)
  185. }
  186. a.EntryMap[name] = paths.Strings()
  187. }
  188. // SetOptionalPaths sets a Make variable with the given name to a slice of the given path strings
  189. // only if there are a non-zero amount of paths.
  190. func (a *AndroidMkEntries) SetOptionalPaths(name string, paths Paths) {
  191. if len(paths) > 0 {
  192. a.SetPaths(name, paths)
  193. }
  194. }
  195. // AddPaths appends the given path strings to a Make variable with the given name.
  196. func (a *AndroidMkEntries) AddPaths(name string, paths Paths) {
  197. if _, ok := a.EntryMap[name]; !ok {
  198. a.entryOrder = append(a.entryOrder, name)
  199. }
  200. a.EntryMap[name] = append(a.EntryMap[name], paths.Strings()...)
  201. }
  202. // SetBoolIfTrue sets a Make variable with the given name to true if the given flag is true.
  203. // It is a no-op if the given flag is false.
  204. func (a *AndroidMkEntries) SetBoolIfTrue(name string, flag bool) {
  205. if flag {
  206. if _, ok := a.EntryMap[name]; !ok {
  207. a.entryOrder = append(a.entryOrder, name)
  208. }
  209. a.EntryMap[name] = []string{"true"}
  210. }
  211. }
  212. // SetBool sets a Make variable with the given name to if the given bool flag value.
  213. func (a *AndroidMkEntries) SetBool(name string, flag bool) {
  214. if _, ok := a.EntryMap[name]; !ok {
  215. a.entryOrder = append(a.entryOrder, name)
  216. }
  217. if flag {
  218. a.EntryMap[name] = []string{"true"}
  219. } else {
  220. a.EntryMap[name] = []string{"false"}
  221. }
  222. }
  223. // AddStrings appends the given strings to a Make variable with the given name.
  224. func (a *AndroidMkEntries) AddStrings(name string, value ...string) {
  225. if len(value) == 0 {
  226. return
  227. }
  228. if _, ok := a.EntryMap[name]; !ok {
  229. a.entryOrder = append(a.entryOrder, name)
  230. }
  231. a.EntryMap[name] = append(a.EntryMap[name], value...)
  232. }
  233. // AddCompatibilityTestSuites adds the supplied test suites to the EntryMap, with special handling
  234. // for partial MTS test suites.
  235. func (a *AndroidMkEntries) AddCompatibilityTestSuites(suites ...string) {
  236. // MTS supports a full test suite and partial per-module MTS test suites, with naming mts-${MODULE}.
  237. // To reduce repetition, if we find a partial MTS test suite without an full MTS test suite,
  238. // we add the full test suite to our list.
  239. if PrefixInList(suites, "mts-") && !InList("mts", suites) {
  240. suites = append(suites, "mts")
  241. }
  242. a.AddStrings("LOCAL_COMPATIBILITY_SUITE", suites...)
  243. }
  244. // The contributions to the dist.
  245. type distContributions struct {
  246. // List of goals and the dist copy instructions.
  247. copiesForGoals []*copiesForGoals
  248. }
  249. // getCopiesForGoals returns a copiesForGoals into which copy instructions that
  250. // must be processed when building one or more of those goals can be added.
  251. func (d *distContributions) getCopiesForGoals(goals string) *copiesForGoals {
  252. copiesForGoals := &copiesForGoals{goals: goals}
  253. d.copiesForGoals = append(d.copiesForGoals, copiesForGoals)
  254. return copiesForGoals
  255. }
  256. // Associates a list of dist copy instructions with a set of goals for which they
  257. // should be run.
  258. type copiesForGoals struct {
  259. // goals are a space separated list of build targets that will trigger the
  260. // copy instructions.
  261. goals string
  262. // A list of instructions to copy a module's output files to somewhere in the
  263. // dist directory.
  264. copies []distCopy
  265. }
  266. // Adds a copy instruction.
  267. func (d *copiesForGoals) addCopyInstruction(from Path, dest string) {
  268. d.copies = append(d.copies, distCopy{from, dest})
  269. }
  270. // Instruction on a path that must be copied into the dist.
  271. type distCopy struct {
  272. // The path to copy from.
  273. from Path
  274. // The destination within the dist directory to copy to.
  275. dest string
  276. }
  277. // Compute the contributions that the module makes to the dist.
  278. func (a *AndroidMkEntries) getDistContributions(mod blueprint.Module) *distContributions {
  279. amod := mod.(Module).base()
  280. name := amod.BaseModuleName()
  281. // Collate the set of associated tag/paths available for copying to the dist.
  282. // Start with an empty (nil) set.
  283. var availableTaggedDists TaggedDistFiles
  284. // Then merge in any that are provided explicitly by the module.
  285. if a.DistFiles != nil {
  286. // Merge the DistFiles into the set.
  287. availableTaggedDists = availableTaggedDists.merge(a.DistFiles)
  288. }
  289. // If no paths have been provided for the DefaultDistTag and the output file is
  290. // valid then add that as the default dist path.
  291. if _, ok := availableTaggedDists[DefaultDistTag]; !ok && a.OutputFile.Valid() {
  292. availableTaggedDists = availableTaggedDists.addPathsForTag(DefaultDistTag, a.OutputFile.Path())
  293. }
  294. // If the distFiles created by GenerateTaggedDistFiles contains paths for the
  295. // DefaultDistTag then that takes priority so delete any existing paths.
  296. if _, ok := amod.distFiles[DefaultDistTag]; ok {
  297. delete(availableTaggedDists, DefaultDistTag)
  298. }
  299. // Finally, merge the distFiles created by GenerateTaggedDistFiles.
  300. availableTaggedDists = availableTaggedDists.merge(amod.distFiles)
  301. if len(availableTaggedDists) == 0 {
  302. // Nothing dist-able for this module.
  303. return nil
  304. }
  305. // Collate the contributions this module makes to the dist.
  306. distContributions := &distContributions{}
  307. // Iterate over this module's dist structs, merged from the dist and dists properties.
  308. for _, dist := range amod.Dists() {
  309. // Get the list of goals this dist should be enabled for. e.g. sdk, droidcore
  310. goals := strings.Join(dist.Targets, " ")
  311. // Get the tag representing the output files to be dist'd. e.g. ".jar", ".proguard_map"
  312. var tag string
  313. if dist.Tag == nil {
  314. // If the dist struct does not specify a tag, use the default output files tag.
  315. tag = DefaultDistTag
  316. } else {
  317. tag = *dist.Tag
  318. }
  319. // Get the paths of the output files to be dist'd, represented by the tag.
  320. // Can be an empty list.
  321. tagPaths := availableTaggedDists[tag]
  322. if len(tagPaths) == 0 {
  323. // Nothing to dist for this tag, continue to the next dist.
  324. continue
  325. }
  326. if len(tagPaths) > 1 && (dist.Dest != nil || dist.Suffix != nil) {
  327. errorMessage := "%s: Cannot apply dest/suffix for more than one dist " +
  328. "file for %q goals tag %q in module %s. The list of dist files, " +
  329. "which should have a single element, is:\n%s"
  330. panic(fmt.Errorf(errorMessage, mod, goals, tag, name, tagPaths))
  331. }
  332. copiesForGoals := distContributions.getCopiesForGoals(goals)
  333. // Iterate over each path adding a copy instruction to copiesForGoals
  334. for _, path := range tagPaths {
  335. // It's possible that the Path is nil from errant modules. Be defensive here.
  336. if path == nil {
  337. tagName := "default" // for error message readability
  338. if dist.Tag != nil {
  339. tagName = *dist.Tag
  340. }
  341. panic(fmt.Errorf("Dist file should not be nil for the %s tag in %s", tagName, name))
  342. }
  343. dest := filepath.Base(path.String())
  344. if dist.Dest != nil {
  345. var err error
  346. if dest, err = validateSafePath(*dist.Dest); err != nil {
  347. // This was checked in ModuleBase.GenerateBuildActions
  348. panic(err)
  349. }
  350. }
  351. if dist.Suffix != nil {
  352. ext := filepath.Ext(dest)
  353. suffix := *dist.Suffix
  354. dest = strings.TrimSuffix(dest, ext) + suffix + ext
  355. }
  356. if dist.Dir != nil {
  357. var err error
  358. if dest, err = validateSafePath(*dist.Dir, dest); err != nil {
  359. // This was checked in ModuleBase.GenerateBuildActions
  360. panic(err)
  361. }
  362. }
  363. copiesForGoals.addCopyInstruction(path, dest)
  364. }
  365. }
  366. return distContributions
  367. }
  368. // generateDistContributionsForMake generates make rules that will generate the
  369. // dist according to the instructions in the supplied distContribution.
  370. func generateDistContributionsForMake(distContributions *distContributions) []string {
  371. var ret []string
  372. for _, d := range distContributions.copiesForGoals {
  373. ret = append(ret, fmt.Sprintf(".PHONY: %s\n", d.goals))
  374. // Create dist-for-goals calls for each of the copy instructions.
  375. for _, c := range d.copies {
  376. ret = append(
  377. ret,
  378. fmt.Sprintf("$(call dist-for-goals,%s,%s:%s)\n", d.goals, c.from.String(), c.dest))
  379. }
  380. }
  381. return ret
  382. }
  383. // Compute the list of Make strings to declare phony goals and dist-for-goals
  384. // calls from the module's dist and dists properties.
  385. func (a *AndroidMkEntries) GetDistForGoals(mod blueprint.Module) []string {
  386. distContributions := a.getDistContributions(mod)
  387. if distContributions == nil {
  388. return nil
  389. }
  390. return generateDistContributionsForMake(distContributions)
  391. }
  392. // Write the license variables to Make for AndroidMkData.Custom(..) methods that do not call WriteAndroidMkData(..)
  393. // It's required to propagate the license metadata even for module types that have non-standard interfaces to Make.
  394. func (a *AndroidMkEntries) WriteLicenseVariables(w io.Writer) {
  395. fmt.Fprintln(w, "LOCAL_LICENSE_KINDS :=", strings.Join(a.EntryMap["LOCAL_LICENSE_KINDS"], " "))
  396. fmt.Fprintln(w, "LOCAL_LICENSE_CONDITIONS :=", strings.Join(a.EntryMap["LOCAL_LICENSE_CONDITIONS"], " "))
  397. fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", strings.Join(a.EntryMap["LOCAL_NOTICE_FILE"], " "))
  398. if pn, ok := a.EntryMap["LOCAL_LICENSE_PACKAGE_NAME"]; ok {
  399. fmt.Fprintln(w, "LOCAL_LICENSE_PACKAGE_NAME :=", strings.Join(pn, " "))
  400. }
  401. }
  402. // fillInEntries goes through the common variable processing and calls the extra data funcs to
  403. // generate and fill in AndroidMkEntries's in-struct data, ready to be flushed to a file.
  404. type fillInEntriesContext interface {
  405. ModuleDir(module blueprint.Module) string
  406. Config() Config
  407. ModuleProvider(module blueprint.Module, provider blueprint.ProviderKey) interface{}
  408. }
  409. func (a *AndroidMkEntries) fillInEntries(ctx fillInEntriesContext, mod blueprint.Module) {
  410. a.EntryMap = make(map[string][]string)
  411. amod := mod.(Module).base()
  412. name := amod.BaseModuleName()
  413. if a.OverrideName != "" {
  414. name = a.OverrideName
  415. }
  416. if a.Include == "" {
  417. a.Include = "$(BUILD_PREBUILT)"
  418. }
  419. a.Required = append(a.Required, mod.(Module).RequiredModuleNames()...)
  420. a.Host_required = append(a.Host_required, mod.(Module).HostRequiredModuleNames()...)
  421. a.Target_required = append(a.Target_required, mod.(Module).TargetRequiredModuleNames()...)
  422. for _, distString := range a.GetDistForGoals(mod) {
  423. fmt.Fprintf(&a.header, distString)
  424. }
  425. fmt.Fprintln(&a.header, "\ninclude $(CLEAR_VARS)")
  426. // Collect make variable assignment entries.
  427. a.SetString("LOCAL_PATH", ctx.ModuleDir(mod))
  428. a.SetString("LOCAL_MODULE", name+a.SubName)
  429. a.AddStrings("LOCAL_LICENSE_KINDS", amod.commonProperties.Effective_license_kinds...)
  430. a.AddStrings("LOCAL_LICENSE_CONDITIONS", amod.commonProperties.Effective_license_conditions...)
  431. a.AddStrings("LOCAL_NOTICE_FILE", amod.commonProperties.Effective_license_text.Strings()...)
  432. // TODO(b/151177513): Does this code need to set LOCAL_MODULE_IS_CONTAINER ?
  433. if amod.commonProperties.Effective_package_name != nil {
  434. a.SetString("LOCAL_LICENSE_PACKAGE_NAME", *amod.commonProperties.Effective_package_name)
  435. } else if len(amod.commonProperties.Effective_licenses) > 0 {
  436. a.SetString("LOCAL_LICENSE_PACKAGE_NAME", strings.Join(amod.commonProperties.Effective_licenses, " "))
  437. }
  438. a.SetString("LOCAL_MODULE_CLASS", a.Class)
  439. a.SetString("LOCAL_PREBUILT_MODULE_FILE", a.OutputFile.String())
  440. a.AddStrings("LOCAL_REQUIRED_MODULES", a.Required...)
  441. a.AddStrings("LOCAL_HOST_REQUIRED_MODULES", a.Host_required...)
  442. a.AddStrings("LOCAL_TARGET_REQUIRED_MODULES", a.Target_required...)
  443. if am, ok := mod.(ApexModule); ok {
  444. a.SetBoolIfTrue("LOCAL_NOT_AVAILABLE_FOR_PLATFORM", am.NotAvailableForPlatform())
  445. }
  446. archStr := amod.Arch().ArchType.String()
  447. host := false
  448. switch amod.Os().Class {
  449. case Host:
  450. if amod.Target().HostCross {
  451. // Make cannot identify LOCAL_MODULE_HOST_CROSS_ARCH:= common.
  452. if amod.Arch().ArchType != Common {
  453. a.SetString("LOCAL_MODULE_HOST_CROSS_ARCH", archStr)
  454. }
  455. } else {
  456. // Make cannot identify LOCAL_MODULE_HOST_ARCH:= common.
  457. if amod.Arch().ArchType != Common {
  458. a.SetString("LOCAL_MODULE_HOST_ARCH", archStr)
  459. }
  460. }
  461. host = true
  462. case Device:
  463. // Make cannot identify LOCAL_MODULE_TARGET_ARCH:= common.
  464. if amod.Arch().ArchType != Common {
  465. if amod.Target().NativeBridge {
  466. hostArchStr := amod.Target().NativeBridgeHostArchName
  467. if hostArchStr != "" {
  468. a.SetString("LOCAL_MODULE_TARGET_ARCH", hostArchStr)
  469. }
  470. } else {
  471. a.SetString("LOCAL_MODULE_TARGET_ARCH", archStr)
  472. }
  473. }
  474. if !amod.InRamdisk() && !amod.InVendorRamdisk() {
  475. a.AddPaths("LOCAL_FULL_INIT_RC", amod.initRcPaths)
  476. }
  477. if len(amod.vintfFragmentsPaths) > 0 {
  478. a.AddPaths("LOCAL_FULL_VINTF_FRAGMENTS", amod.vintfFragmentsPaths)
  479. }
  480. a.SetBoolIfTrue("LOCAL_PROPRIETARY_MODULE", Bool(amod.commonProperties.Proprietary))
  481. if Bool(amod.commonProperties.Vendor) || Bool(amod.commonProperties.Soc_specific) {
  482. a.SetString("LOCAL_VENDOR_MODULE", "true")
  483. }
  484. a.SetBoolIfTrue("LOCAL_ODM_MODULE", Bool(amod.commonProperties.Device_specific))
  485. a.SetBoolIfTrue("LOCAL_PRODUCT_MODULE", Bool(amod.commonProperties.Product_specific))
  486. a.SetBoolIfTrue("LOCAL_SYSTEM_EXT_MODULE", Bool(amod.commonProperties.System_ext_specific))
  487. if amod.commonProperties.Owner != nil {
  488. a.SetString("LOCAL_MODULE_OWNER", *amod.commonProperties.Owner)
  489. }
  490. }
  491. if len(amod.noticeFiles) > 0 {
  492. a.SetString("LOCAL_NOTICE_FILE", strings.Join(amod.noticeFiles.Strings(), " "))
  493. }
  494. if host {
  495. makeOs := amod.Os().String()
  496. if amod.Os() == Linux || amod.Os() == LinuxBionic || amod.Os() == LinuxMusl {
  497. makeOs = "linux"
  498. }
  499. a.SetString("LOCAL_MODULE_HOST_OS", makeOs)
  500. a.SetString("LOCAL_IS_HOST_MODULE", "true")
  501. }
  502. prefix := ""
  503. if amod.ArchSpecific() {
  504. switch amod.Os().Class {
  505. case Host:
  506. if amod.Target().HostCross {
  507. prefix = "HOST_CROSS_"
  508. } else {
  509. prefix = "HOST_"
  510. }
  511. case Device:
  512. prefix = "TARGET_"
  513. }
  514. if amod.Arch().ArchType != ctx.Config().Targets[amod.Os()][0].Arch.ArchType {
  515. prefix = "2ND_" + prefix
  516. }
  517. }
  518. extraCtx := &androidMkExtraEntriesContext{
  519. ctx: ctx,
  520. mod: mod,
  521. }
  522. for _, extra := range a.ExtraEntries {
  523. extra(extraCtx, a)
  524. }
  525. // Write to footer.
  526. fmt.Fprintln(&a.footer, "include "+a.Include)
  527. blueprintDir := ctx.ModuleDir(mod)
  528. for _, footerFunc := range a.ExtraFooters {
  529. footerFunc(&a.footer, name, prefix, blueprintDir)
  530. }
  531. }
  532. // write flushes the AndroidMkEntries's in-struct data populated by AndroidMkEntries into the
  533. // given Writer object.
  534. func (a *AndroidMkEntries) write(w io.Writer) {
  535. if a.Disabled {
  536. return
  537. }
  538. if !a.OutputFile.Valid() {
  539. return
  540. }
  541. w.Write(a.header.Bytes())
  542. for _, name := range a.entryOrder {
  543. fmt.Fprintln(w, name+" := "+strings.Join(a.EntryMap[name], " "))
  544. }
  545. w.Write(a.footer.Bytes())
  546. }
  547. func (a *AndroidMkEntries) FooterLinesForTests() []string {
  548. return strings.Split(string(a.footer.Bytes()), "\n")
  549. }
  550. // AndroidMkSingleton is a singleton to collect Android.mk data from all modules and dump them into
  551. // the final Android-<product_name>.mk file output.
  552. func AndroidMkSingleton() Singleton {
  553. return &androidMkSingleton{}
  554. }
  555. type androidMkSingleton struct{}
  556. func (c *androidMkSingleton) GenerateBuildActions(ctx SingletonContext) {
  557. // Skip if Soong wasn't invoked from Make.
  558. if !ctx.Config().KatiEnabled() {
  559. return
  560. }
  561. var androidMkModulesList []blueprint.Module
  562. ctx.VisitAllModulesBlueprint(func(module blueprint.Module) {
  563. androidMkModulesList = append(androidMkModulesList, module)
  564. })
  565. // Sort the module list by the module names to eliminate random churns, which may erroneously
  566. // invoke additional build processes.
  567. sort.SliceStable(androidMkModulesList, func(i, j int) bool {
  568. return ctx.ModuleName(androidMkModulesList[i]) < ctx.ModuleName(androidMkModulesList[j])
  569. })
  570. transMk := PathForOutput(ctx, "Android"+String(ctx.Config().productVariables.Make_suffix)+".mk")
  571. if ctx.Failed() {
  572. return
  573. }
  574. err := translateAndroidMk(ctx, absolutePath(transMk.String()), androidMkModulesList)
  575. if err != nil {
  576. ctx.Errorf(err.Error())
  577. }
  578. ctx.Build(pctx, BuildParams{
  579. Rule: blueprint.Phony,
  580. Output: transMk,
  581. })
  582. }
  583. func translateAndroidMk(ctx SingletonContext, mkFile string, mods []blueprint.Module) error {
  584. buf := &bytes.Buffer{}
  585. fmt.Fprintln(buf, "LOCAL_MODULE_MAKEFILE := $(lastword $(MAKEFILE_LIST))")
  586. typeStats := make(map[string]int)
  587. for _, mod := range mods {
  588. err := translateAndroidMkModule(ctx, buf, mod)
  589. if err != nil {
  590. os.Remove(mkFile)
  591. return err
  592. }
  593. if amod, ok := mod.(Module); ok && ctx.PrimaryModule(amod) == amod {
  594. typeStats[ctx.ModuleType(amod)] += 1
  595. }
  596. }
  597. keys := []string{}
  598. fmt.Fprintln(buf, "\nSTATS.SOONG_MODULE_TYPE :=")
  599. for k := range typeStats {
  600. keys = append(keys, k)
  601. }
  602. sort.Strings(keys)
  603. for _, mod_type := range keys {
  604. fmt.Fprintln(buf, "STATS.SOONG_MODULE_TYPE +=", mod_type)
  605. fmt.Fprintf(buf, "STATS.SOONG_MODULE_TYPE.%s := %d\n", mod_type, typeStats[mod_type])
  606. }
  607. // Don't write to the file if it hasn't changed
  608. if _, err := os.Stat(absolutePath(mkFile)); !os.IsNotExist(err) {
  609. if data, err := ioutil.ReadFile(absolutePath(mkFile)); err == nil {
  610. matches := buf.Len() == len(data)
  611. if matches {
  612. for i, value := range buf.Bytes() {
  613. if value != data[i] {
  614. matches = false
  615. break
  616. }
  617. }
  618. }
  619. if matches {
  620. return nil
  621. }
  622. }
  623. }
  624. return ioutil.WriteFile(absolutePath(mkFile), buf.Bytes(), 0666)
  625. }
  626. func translateAndroidMkModule(ctx SingletonContext, w io.Writer, mod blueprint.Module) error {
  627. defer func() {
  628. if r := recover(); r != nil {
  629. panic(fmt.Errorf("%s in translateAndroidMkModule for module %s variant %s",
  630. r, ctx.ModuleName(mod), ctx.ModuleSubDir(mod)))
  631. }
  632. }()
  633. // Additional cases here require review for correct license propagation to make.
  634. switch x := mod.(type) {
  635. case AndroidMkDataProvider:
  636. return translateAndroidModule(ctx, w, mod, x)
  637. case bootstrap.GoBinaryTool:
  638. return translateGoBinaryModule(ctx, w, mod, x)
  639. case AndroidMkEntriesProvider:
  640. return translateAndroidMkEntriesModule(ctx, w, mod, x)
  641. default:
  642. // Not exported to make so no make variables to set.
  643. return nil
  644. }
  645. }
  646. // A simple, special Android.mk entry output func to make it possible to build blueprint tools using
  647. // m by making them phony targets.
  648. func translateGoBinaryModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
  649. goBinary bootstrap.GoBinaryTool) error {
  650. name := ctx.ModuleName(mod)
  651. fmt.Fprintln(w, ".PHONY:", name)
  652. fmt.Fprintln(w, name+":", goBinary.InstallPath())
  653. fmt.Fprintln(w, "")
  654. // Assuming no rules in make include go binaries in distributables.
  655. // If the assumption is wrong, make will fail to build without the necessary .meta_lic and .meta_module files.
  656. // In that case, add the targets and rules here to build a .meta_lic file for `name` and a .meta_module for
  657. // `goBinary.InstallPath()` pointing to the `name`.meta_lic file.
  658. return nil
  659. }
  660. func (data *AndroidMkData) fillInData(ctx fillInEntriesContext, mod blueprint.Module) {
  661. // Get the preamble content through AndroidMkEntries logic.
  662. data.Entries = AndroidMkEntries{
  663. Class: data.Class,
  664. SubName: data.SubName,
  665. DistFiles: data.DistFiles,
  666. OutputFile: data.OutputFile,
  667. Disabled: data.Disabled,
  668. Include: data.Include,
  669. Required: data.Required,
  670. Host_required: data.Host_required,
  671. Target_required: data.Target_required,
  672. }
  673. data.Entries.fillInEntries(ctx, mod)
  674. // copy entries back to data since it is used in Custom
  675. data.Required = data.Entries.Required
  676. data.Host_required = data.Entries.Host_required
  677. data.Target_required = data.Entries.Target_required
  678. }
  679. // A support func for the deprecated AndroidMkDataProvider interface. Use AndroidMkEntryProvider
  680. // instead.
  681. func translateAndroidModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
  682. provider AndroidMkDataProvider) error {
  683. amod := mod.(Module).base()
  684. if shouldSkipAndroidMkProcessing(amod) {
  685. return nil
  686. }
  687. data := provider.AndroidMk()
  688. if data.Include == "" {
  689. data.Include = "$(BUILD_PREBUILT)"
  690. }
  691. data.fillInData(ctx, mod)
  692. prefix := ""
  693. if amod.ArchSpecific() {
  694. switch amod.Os().Class {
  695. case Host:
  696. if amod.Target().HostCross {
  697. prefix = "HOST_CROSS_"
  698. } else {
  699. prefix = "HOST_"
  700. }
  701. case Device:
  702. prefix = "TARGET_"
  703. }
  704. if amod.Arch().ArchType != ctx.Config().Targets[amod.Os()][0].Arch.ArchType {
  705. prefix = "2ND_" + prefix
  706. }
  707. }
  708. name := provider.BaseModuleName()
  709. blueprintDir := filepath.Dir(ctx.BlueprintFile(mod))
  710. if data.Custom != nil {
  711. // List of module types allowed to use .Custom(...)
  712. // Additions to the list require careful review for proper license handling.
  713. switch reflect.TypeOf(mod).String() { // ctx.ModuleType(mod) doesn't work: aidl_interface creates phony without type
  714. case "*aidl.aidlApi": // writes non-custom before adding .phony
  715. case "*aidl.aidlMapping": // writes non-custom before adding .phony
  716. case "*android.customModule": // appears in tests only
  717. case "*android_sdk.sdkRepoHost": // doesn't go through base_rules
  718. case "*apex.apexBundle": // license properties written
  719. case "*bpf.bpf": // license properties written (both for module and objs)
  720. case "*genrule.Module": // writes non-custom before adding .phony
  721. case "*java.SystemModules": // doesn't go through base_rules
  722. case "*java.systemModulesImport": // doesn't go through base_rules
  723. case "*phony.phony": // license properties written
  724. case "*selinux.selinuxContextsModule": // license properties written
  725. case "*sysprop.syspropLibrary": // license properties written
  726. default:
  727. if !ctx.Config().IsEnvFalse("ANDROID_REQUIRE_LICENSES") {
  728. return fmt.Errorf("custom make rules not allowed for %q (%q) module %q", ctx.ModuleType(mod), reflect.TypeOf(mod), ctx.ModuleName(mod))
  729. }
  730. }
  731. data.Custom(w, name, prefix, blueprintDir, data)
  732. } else {
  733. WriteAndroidMkData(w, data)
  734. }
  735. return nil
  736. }
  737. // A support func for the deprecated AndroidMkDataProvider interface. Use AndroidMkEntryProvider
  738. // instead.
  739. func WriteAndroidMkData(w io.Writer, data AndroidMkData) {
  740. if data.Disabled {
  741. return
  742. }
  743. if !data.OutputFile.Valid() {
  744. return
  745. }
  746. // write preamble via Entries
  747. data.Entries.footer = bytes.Buffer{}
  748. data.Entries.write(w)
  749. for _, extra := range data.Extra {
  750. extra(w, data.OutputFile.Path())
  751. }
  752. fmt.Fprintln(w, "include "+data.Include)
  753. }
  754. func translateAndroidMkEntriesModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
  755. provider AndroidMkEntriesProvider) error {
  756. if shouldSkipAndroidMkProcessing(mod.(Module).base()) {
  757. return nil
  758. }
  759. // Any new or special cases here need review to verify correct propagation of license information.
  760. for _, entries := range provider.AndroidMkEntries() {
  761. entries.fillInEntries(ctx, mod)
  762. entries.write(w)
  763. }
  764. return nil
  765. }
  766. func shouldSkipAndroidMkProcessing(module *ModuleBase) bool {
  767. if !module.commonProperties.NamespaceExportedToMake {
  768. // TODO(jeffrygaston) do we want to validate that there are no modules being
  769. // exported to Kati that depend on this module?
  770. return true
  771. }
  772. return !module.Enabled() ||
  773. module.commonProperties.HideFromMake ||
  774. // Make does not understand LinuxBionic
  775. module.Os() == LinuxBionic
  776. }
  777. // A utility func to format LOCAL_TEST_DATA outputs. See the comments on DataPath to understand how
  778. // to use this func.
  779. func AndroidMkDataPaths(data []DataPath) []string {
  780. var testFiles []string
  781. for _, d := range data {
  782. rel := d.SrcPath.Rel()
  783. path := d.SrcPath.String()
  784. // LOCAL_TEST_DATA requires the rel portion of the path to be removed from the path.
  785. if !strings.HasSuffix(path, rel) {
  786. panic(fmt.Errorf("path %q does not end with %q", path, rel))
  787. }
  788. path = strings.TrimSuffix(path, rel)
  789. testFileString := path + ":" + rel
  790. if len(d.RelativeInstallPath) > 0 {
  791. testFileString += ":" + d.RelativeInstallPath
  792. }
  793. testFiles = append(testFiles, testFileString)
  794. }
  795. return testFiles
  796. }