androidmk.go 34 KB

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