androidmk.go 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  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. distContributions.licenseMetadataFile = amod.licenseMetadataFile
  317. // Iterate over this module's dist structs, merged from the dist and dists properties.
  318. for _, dist := range amod.Dists() {
  319. // Get the list of goals this dist should be enabled for. e.g. sdk, droidcore
  320. goals := strings.Join(dist.Targets, " ")
  321. // Get the tag representing the output files to be dist'd. e.g. ".jar", ".proguard_map"
  322. var tag string
  323. if dist.Tag == nil {
  324. // If the dist struct does not specify a tag, use the default output files tag.
  325. tag = DefaultDistTag
  326. } else {
  327. tag = *dist.Tag
  328. }
  329. // Get the paths of the output files to be dist'd, represented by the tag.
  330. // Can be an empty list.
  331. tagPaths := availableTaggedDists[tag]
  332. if len(tagPaths) == 0 {
  333. // Nothing to dist for this tag, continue to the next dist.
  334. continue
  335. }
  336. if len(tagPaths) > 1 && (dist.Dest != nil || dist.Suffix != nil) {
  337. errorMessage := "%s: Cannot apply dest/suffix for more than one dist " +
  338. "file for %q goals tag %q in module %s. The list of dist files, " +
  339. "which should have a single element, is:\n%s"
  340. panic(fmt.Errorf(errorMessage, mod, goals, tag, name, tagPaths))
  341. }
  342. copiesForGoals := distContributions.getCopiesForGoals(goals)
  343. // Iterate over each path adding a copy instruction to copiesForGoals
  344. for _, path := range tagPaths {
  345. // It's possible that the Path is nil from errant modules. Be defensive here.
  346. if path == nil {
  347. tagName := "default" // for error message readability
  348. if dist.Tag != nil {
  349. tagName = *dist.Tag
  350. }
  351. panic(fmt.Errorf("Dist file should not be nil for the %s tag in %s", tagName, name))
  352. }
  353. dest := filepath.Base(path.String())
  354. if dist.Dest != nil {
  355. var err error
  356. if dest, err = validateSafePath(*dist.Dest); err != nil {
  357. // This was checked in ModuleBase.GenerateBuildActions
  358. panic(err)
  359. }
  360. }
  361. ext := filepath.Ext(dest)
  362. suffix := ""
  363. if dist.Suffix != nil {
  364. suffix = *dist.Suffix
  365. }
  366. productString := ""
  367. if dist.Append_artifact_with_product != nil && *dist.Append_artifact_with_product {
  368. productString = fmt.Sprintf("_%s", a.entryContext.Config().DeviceProduct())
  369. }
  370. if suffix != "" || productString != "" {
  371. dest = strings.TrimSuffix(dest, ext) + suffix + productString + ext
  372. }
  373. if dist.Dir != nil {
  374. var err error
  375. if dest, err = validateSafePath(*dist.Dir, dest); err != nil {
  376. // This was checked in ModuleBase.GenerateBuildActions
  377. panic(err)
  378. }
  379. }
  380. copiesForGoals.addCopyInstruction(path, dest)
  381. }
  382. }
  383. return distContributions
  384. }
  385. // generateDistContributionsForMake generates make rules that will generate the
  386. // dist according to the instructions in the supplied distContribution.
  387. func generateDistContributionsForMake(distContributions *distContributions) []string {
  388. var ret []string
  389. for _, d := range distContributions.copiesForGoals {
  390. ret = append(ret, fmt.Sprintf(".PHONY: %s\n", d.goals))
  391. // Create dist-for-goals calls for each of the copy instructions.
  392. for _, c := range d.copies {
  393. ret = append(
  394. ret,
  395. fmt.Sprintf("$(if $(strip $(ALL_TARGETS.%s.META_LIC)),,$(eval ALL_TARGETS.%s.META_LIC := %s))\n",
  396. c.from.String(), c.from.String(), distContributions.licenseMetadataFile.String()))
  397. ret = append(
  398. ret,
  399. fmt.Sprintf("$(call dist-for-goals,%s,%s:%s)\n", d.goals, c.from.String(), c.dest))
  400. }
  401. }
  402. return ret
  403. }
  404. // Compute the list of Make strings to declare phony goals and dist-for-goals
  405. // calls from the module's dist and dists properties.
  406. func (a *AndroidMkEntries) GetDistForGoals(mod blueprint.Module) []string {
  407. distContributions := a.getDistContributions(mod)
  408. if distContributions == nil {
  409. return nil
  410. }
  411. return generateDistContributionsForMake(distContributions)
  412. }
  413. // Write the license variables to Make for AndroidMkData.Custom(..) methods that do not call WriteAndroidMkData(..)
  414. // It's required to propagate the license metadata even for module types that have non-standard interfaces to Make.
  415. func (a *AndroidMkEntries) WriteLicenseVariables(w io.Writer) {
  416. fmt.Fprintln(w, "LOCAL_LICENSE_KINDS :=", strings.Join(a.EntryMap["LOCAL_LICENSE_KINDS"], " "))
  417. fmt.Fprintln(w, "LOCAL_LICENSE_CONDITIONS :=", strings.Join(a.EntryMap["LOCAL_LICENSE_CONDITIONS"], " "))
  418. fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", strings.Join(a.EntryMap["LOCAL_NOTICE_FILE"], " "))
  419. if pn, ok := a.EntryMap["LOCAL_LICENSE_PACKAGE_NAME"]; ok {
  420. fmt.Fprintln(w, "LOCAL_LICENSE_PACKAGE_NAME :=", strings.Join(pn, " "))
  421. }
  422. }
  423. // fillInEntries goes through the common variable processing and calls the extra data funcs to
  424. // generate and fill in AndroidMkEntries's in-struct data, ready to be flushed to a file.
  425. type fillInEntriesContext interface {
  426. ModuleDir(module blueprint.Module) string
  427. Config() Config
  428. ModuleProvider(module blueprint.Module, provider blueprint.ProviderKey) interface{}
  429. ModuleHasProvider(module blueprint.Module, provider blueprint.ProviderKey) bool
  430. }
  431. func (a *AndroidMkEntries) fillInEntries(ctx fillInEntriesContext, mod blueprint.Module) {
  432. a.entryContext = ctx
  433. a.EntryMap = make(map[string][]string)
  434. amod := mod.(Module)
  435. base := amod.base()
  436. name := base.BaseModuleName()
  437. if a.OverrideName != "" {
  438. name = a.OverrideName
  439. }
  440. if a.Include == "" {
  441. a.Include = "$(BUILD_PREBUILT)"
  442. }
  443. a.Required = append(a.Required, amod.RequiredModuleNames()...)
  444. a.Host_required = append(a.Host_required, amod.HostRequiredModuleNames()...)
  445. a.Target_required = append(a.Target_required, amod.TargetRequiredModuleNames()...)
  446. for _, distString := range a.GetDistForGoals(mod) {
  447. fmt.Fprintf(&a.header, distString)
  448. }
  449. fmt.Fprintln(&a.header, "\ninclude $(CLEAR_VARS)")
  450. // Collect make variable assignment entries.
  451. a.SetString("LOCAL_PATH", ctx.ModuleDir(mod))
  452. a.SetString("LOCAL_MODULE", name+a.SubName)
  453. a.AddStrings("LOCAL_LICENSE_KINDS", base.commonProperties.Effective_license_kinds...)
  454. a.AddStrings("LOCAL_LICENSE_CONDITIONS", base.commonProperties.Effective_license_conditions...)
  455. a.AddStrings("LOCAL_NOTICE_FILE", base.commonProperties.Effective_license_text.Strings()...)
  456. // TODO(b/151177513): Does this code need to set LOCAL_MODULE_IS_CONTAINER ?
  457. if base.commonProperties.Effective_package_name != nil {
  458. a.SetString("LOCAL_LICENSE_PACKAGE_NAME", *base.commonProperties.Effective_package_name)
  459. } else if len(base.commonProperties.Effective_licenses) > 0 {
  460. a.SetString("LOCAL_LICENSE_PACKAGE_NAME", strings.Join(base.commonProperties.Effective_licenses, " "))
  461. }
  462. a.SetString("LOCAL_MODULE_CLASS", a.Class)
  463. a.SetString("LOCAL_PREBUILT_MODULE_FILE", a.OutputFile.String())
  464. a.AddStrings("LOCAL_REQUIRED_MODULES", a.Required...)
  465. a.AddStrings("LOCAL_HOST_REQUIRED_MODULES", a.Host_required...)
  466. a.AddStrings("LOCAL_TARGET_REQUIRED_MODULES", a.Target_required...)
  467. // If the install rule was generated by Soong tell Make about it.
  468. if len(base.katiInstalls) > 0 {
  469. // Assume the primary install file is last since it probably needs to depend on any other
  470. // installed files. If that is not the case we can add a method to specify the primary
  471. // installed file.
  472. a.SetPath("LOCAL_SOONG_INSTALLED_MODULE", base.katiInstalls[len(base.katiInstalls)-1].to)
  473. a.SetString("LOCAL_SOONG_INSTALL_PAIRS", base.katiInstalls.BuiltInstalled())
  474. a.SetPaths("LOCAL_SOONG_INSTALL_SYMLINKS", base.katiSymlinks.InstallPaths().Paths())
  475. }
  476. if am, ok := mod.(ApexModule); ok {
  477. a.SetBoolIfTrue("LOCAL_NOT_AVAILABLE_FOR_PLATFORM", am.NotAvailableForPlatform())
  478. }
  479. archStr := base.Arch().ArchType.String()
  480. host := false
  481. switch base.Os().Class {
  482. case Host:
  483. if base.Target().HostCross {
  484. // Make cannot identify LOCAL_MODULE_HOST_CROSS_ARCH:= common.
  485. if base.Arch().ArchType != Common {
  486. a.SetString("LOCAL_MODULE_HOST_CROSS_ARCH", archStr)
  487. }
  488. } else {
  489. // Make cannot identify LOCAL_MODULE_HOST_ARCH:= common.
  490. if base.Arch().ArchType != Common {
  491. a.SetString("LOCAL_MODULE_HOST_ARCH", archStr)
  492. }
  493. }
  494. host = true
  495. case Device:
  496. // Make cannot identify LOCAL_MODULE_TARGET_ARCH:= common.
  497. if base.Arch().ArchType != Common {
  498. if base.Target().NativeBridge {
  499. hostArchStr := base.Target().NativeBridgeHostArchName
  500. if hostArchStr != "" {
  501. a.SetString("LOCAL_MODULE_TARGET_ARCH", hostArchStr)
  502. }
  503. } else {
  504. a.SetString("LOCAL_MODULE_TARGET_ARCH", archStr)
  505. }
  506. }
  507. if !base.InVendorRamdisk() {
  508. a.AddPaths("LOCAL_FULL_INIT_RC", base.initRcPaths)
  509. }
  510. if len(base.vintfFragmentsPaths) > 0 {
  511. a.AddPaths("LOCAL_FULL_VINTF_FRAGMENTS", base.vintfFragmentsPaths)
  512. }
  513. a.SetBoolIfTrue("LOCAL_PROPRIETARY_MODULE", Bool(base.commonProperties.Proprietary))
  514. if Bool(base.commonProperties.Vendor) || Bool(base.commonProperties.Soc_specific) {
  515. a.SetString("LOCAL_VENDOR_MODULE", "true")
  516. }
  517. a.SetBoolIfTrue("LOCAL_ODM_MODULE", Bool(base.commonProperties.Device_specific))
  518. a.SetBoolIfTrue("LOCAL_PRODUCT_MODULE", Bool(base.commonProperties.Product_specific))
  519. a.SetBoolIfTrue("LOCAL_SYSTEM_EXT_MODULE", Bool(base.commonProperties.System_ext_specific))
  520. if base.commonProperties.Owner != nil {
  521. a.SetString("LOCAL_MODULE_OWNER", *base.commonProperties.Owner)
  522. }
  523. }
  524. if len(base.noticeFiles) > 0 {
  525. a.SetString("LOCAL_NOTICE_FILE", strings.Join(base.noticeFiles.Strings(), " "))
  526. }
  527. if host {
  528. makeOs := base.Os().String()
  529. if base.Os() == Linux || base.Os() == LinuxBionic || base.Os() == LinuxMusl {
  530. makeOs = "linux"
  531. }
  532. a.SetString("LOCAL_MODULE_HOST_OS", makeOs)
  533. a.SetString("LOCAL_IS_HOST_MODULE", "true")
  534. }
  535. prefix := ""
  536. if base.ArchSpecific() {
  537. switch base.Os().Class {
  538. case Host:
  539. if base.Target().HostCross {
  540. prefix = "HOST_CROSS_"
  541. } else {
  542. prefix = "HOST_"
  543. }
  544. case Device:
  545. prefix = "TARGET_"
  546. }
  547. if base.Arch().ArchType != ctx.Config().Targets[base.Os()][0].Arch.ArchType {
  548. prefix = "2ND_" + prefix
  549. }
  550. }
  551. if ctx.ModuleHasProvider(mod, LicenseMetadataProvider) {
  552. licenseMetadata := ctx.ModuleProvider(mod, LicenseMetadataProvider).(*LicenseMetadataInfo)
  553. a.SetPath("LOCAL_SOONG_LICENSE_METADATA", licenseMetadata.LicenseMetadataPath)
  554. }
  555. extraCtx := &androidMkExtraEntriesContext{
  556. ctx: ctx,
  557. mod: mod,
  558. }
  559. for _, extra := range a.ExtraEntries {
  560. extra(extraCtx, a)
  561. }
  562. // Write to footer.
  563. fmt.Fprintln(&a.footer, "include "+a.Include)
  564. blueprintDir := ctx.ModuleDir(mod)
  565. for _, footerFunc := range a.ExtraFooters {
  566. footerFunc(&a.footer, name, prefix, blueprintDir)
  567. }
  568. }
  569. // write flushes the AndroidMkEntries's in-struct data populated by AndroidMkEntries into the
  570. // given Writer object.
  571. func (a *AndroidMkEntries) write(w io.Writer) {
  572. if a.Disabled {
  573. return
  574. }
  575. if !a.OutputFile.Valid() {
  576. return
  577. }
  578. w.Write(a.header.Bytes())
  579. for _, name := range a.entryOrder {
  580. fmt.Fprintln(w, name+" := "+strings.Join(a.EntryMap[name], " "))
  581. }
  582. w.Write(a.footer.Bytes())
  583. }
  584. func (a *AndroidMkEntries) FooterLinesForTests() []string {
  585. return strings.Split(string(a.footer.Bytes()), "\n")
  586. }
  587. // AndroidMkSingleton is a singleton to collect Android.mk data from all modules and dump them into
  588. // the final Android-<product_name>.mk file output.
  589. func AndroidMkSingleton() Singleton {
  590. return &androidMkSingleton{}
  591. }
  592. type androidMkSingleton struct{}
  593. func (c *androidMkSingleton) GenerateBuildActions(ctx SingletonContext) {
  594. // Skip if Soong wasn't invoked from Make.
  595. if !ctx.Config().KatiEnabled() {
  596. return
  597. }
  598. var androidMkModulesList []blueprint.Module
  599. ctx.VisitAllModulesBlueprint(func(module blueprint.Module) {
  600. androidMkModulesList = append(androidMkModulesList, module)
  601. })
  602. // Sort the module list by the module names to eliminate random churns, which may erroneously
  603. // invoke additional build processes.
  604. sort.SliceStable(androidMkModulesList, func(i, j int) bool {
  605. return ctx.ModuleName(androidMkModulesList[i]) < ctx.ModuleName(androidMkModulesList[j])
  606. })
  607. transMk := PathForOutput(ctx, "Android"+String(ctx.Config().productVariables.Make_suffix)+".mk")
  608. if ctx.Failed() {
  609. return
  610. }
  611. err := translateAndroidMk(ctx, absolutePath(transMk.String()), androidMkModulesList)
  612. if err != nil {
  613. ctx.Errorf(err.Error())
  614. }
  615. ctx.Build(pctx, BuildParams{
  616. Rule: blueprint.Phony,
  617. Output: transMk,
  618. })
  619. }
  620. func translateAndroidMk(ctx SingletonContext, absMkFile string, mods []blueprint.Module) error {
  621. buf := &bytes.Buffer{}
  622. fmt.Fprintln(buf, "LOCAL_MODULE_MAKEFILE := $(lastword $(MAKEFILE_LIST))")
  623. typeStats := make(map[string]int)
  624. for _, mod := range mods {
  625. err := translateAndroidMkModule(ctx, buf, mod)
  626. if err != nil {
  627. os.Remove(absMkFile)
  628. return err
  629. }
  630. if amod, ok := mod.(Module); ok && ctx.PrimaryModule(amod) == amod {
  631. typeStats[ctx.ModuleType(amod)] += 1
  632. }
  633. }
  634. keys := []string{}
  635. fmt.Fprintln(buf, "\nSTATS.SOONG_MODULE_TYPE :=")
  636. for k := range typeStats {
  637. keys = append(keys, k)
  638. }
  639. sort.Strings(keys)
  640. for _, mod_type := range keys {
  641. fmt.Fprintln(buf, "STATS.SOONG_MODULE_TYPE +=", mod_type)
  642. fmt.Fprintf(buf, "STATS.SOONG_MODULE_TYPE.%s := %d\n", mod_type, typeStats[mod_type])
  643. }
  644. return pathtools.WriteFileIfChanged(absMkFile, buf.Bytes(), 0666)
  645. }
  646. func translateAndroidMkModule(ctx SingletonContext, w io.Writer, mod blueprint.Module) error {
  647. defer func() {
  648. if r := recover(); r != nil {
  649. panic(fmt.Errorf("%s in translateAndroidMkModule for module %s variant %s",
  650. r, ctx.ModuleName(mod), ctx.ModuleSubDir(mod)))
  651. }
  652. }()
  653. // Additional cases here require review for correct license propagation to make.
  654. switch x := mod.(type) {
  655. case AndroidMkDataProvider:
  656. return translateAndroidModule(ctx, w, mod, x)
  657. case bootstrap.GoBinaryTool:
  658. return translateGoBinaryModule(ctx, w, mod, x)
  659. case AndroidMkEntriesProvider:
  660. return translateAndroidMkEntriesModule(ctx, w, mod, x)
  661. default:
  662. // Not exported to make so no make variables to set.
  663. return nil
  664. }
  665. }
  666. // A simple, special Android.mk entry output func to make it possible to build blueprint tools using
  667. // m by making them phony targets.
  668. func translateGoBinaryModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
  669. goBinary bootstrap.GoBinaryTool) error {
  670. name := ctx.ModuleName(mod)
  671. fmt.Fprintln(w, ".PHONY:", name)
  672. fmt.Fprintln(w, name+":", goBinary.InstallPath())
  673. fmt.Fprintln(w, "")
  674. // Assuming no rules in make include go binaries in distributables.
  675. // If the assumption is wrong, make will fail to build without the necessary .meta_lic and .meta_module files.
  676. // In that case, add the targets and rules here to build a .meta_lic file for `name` and a .meta_module for
  677. // `goBinary.InstallPath()` pointing to the `name`.meta_lic file.
  678. return nil
  679. }
  680. func (data *AndroidMkData) fillInData(ctx fillInEntriesContext, mod blueprint.Module) {
  681. // Get the preamble content through AndroidMkEntries logic.
  682. data.Entries = AndroidMkEntries{
  683. Class: data.Class,
  684. SubName: data.SubName,
  685. DistFiles: data.DistFiles,
  686. OutputFile: data.OutputFile,
  687. Disabled: data.Disabled,
  688. Include: data.Include,
  689. Required: data.Required,
  690. Host_required: data.Host_required,
  691. Target_required: data.Target_required,
  692. }
  693. data.Entries.fillInEntries(ctx, mod)
  694. // copy entries back to data since it is used in Custom
  695. data.Required = data.Entries.Required
  696. data.Host_required = data.Entries.Host_required
  697. data.Target_required = data.Entries.Target_required
  698. }
  699. // A support func for the deprecated AndroidMkDataProvider interface. Use AndroidMkEntryProvider
  700. // instead.
  701. func translateAndroidModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
  702. provider AndroidMkDataProvider) error {
  703. amod := mod.(Module).base()
  704. if shouldSkipAndroidMkProcessing(amod) {
  705. return nil
  706. }
  707. data := provider.AndroidMk()
  708. if data.Include == "" {
  709. data.Include = "$(BUILD_PREBUILT)"
  710. }
  711. data.fillInData(ctx, mod)
  712. prefix := ""
  713. if amod.ArchSpecific() {
  714. switch amod.Os().Class {
  715. case Host:
  716. if amod.Target().HostCross {
  717. prefix = "HOST_CROSS_"
  718. } else {
  719. prefix = "HOST_"
  720. }
  721. case Device:
  722. prefix = "TARGET_"
  723. }
  724. if amod.Arch().ArchType != ctx.Config().Targets[amod.Os()][0].Arch.ArchType {
  725. prefix = "2ND_" + prefix
  726. }
  727. }
  728. name := provider.BaseModuleName()
  729. blueprintDir := filepath.Dir(ctx.BlueprintFile(mod))
  730. if data.Custom != nil {
  731. // List of module types allowed to use .Custom(...)
  732. // Additions to the list require careful review for proper license handling.
  733. switch reflect.TypeOf(mod).String() { // ctx.ModuleType(mod) doesn't work: aidl_interface creates phony without type
  734. case "*aidl.aidlApi": // writes non-custom before adding .phony
  735. case "*aidl.aidlMapping": // writes non-custom before adding .phony
  736. case "*android.customModule": // appears in tests only
  737. case "*android_sdk.sdkRepoHost": // doesn't go through base_rules
  738. case "*apex.apexBundle": // license properties written
  739. case "*bpf.bpf": // license properties written (both for module and objs)
  740. case "*genrule.Module": // writes non-custom before adding .phony
  741. case "*java.SystemModules": // doesn't go through base_rules
  742. case "*java.systemModulesImport": // doesn't go through base_rules
  743. case "*phony.phony": // license properties written
  744. case "*selinux.selinuxContextsModule": // license properties written
  745. case "*sysprop.syspropLibrary": // license properties written
  746. default:
  747. if !ctx.Config().IsEnvFalse("ANDROID_REQUIRE_LICENSES") {
  748. return fmt.Errorf("custom make rules not allowed for %q (%q) module %q", ctx.ModuleType(mod), reflect.TypeOf(mod), ctx.ModuleName(mod))
  749. }
  750. }
  751. data.Custom(w, name, prefix, blueprintDir, data)
  752. } else {
  753. WriteAndroidMkData(w, data)
  754. }
  755. return nil
  756. }
  757. // A support func for the deprecated AndroidMkDataProvider interface. Use AndroidMkEntryProvider
  758. // instead.
  759. func WriteAndroidMkData(w io.Writer, data AndroidMkData) {
  760. if data.Disabled {
  761. return
  762. }
  763. if !data.OutputFile.Valid() {
  764. return
  765. }
  766. // write preamble via Entries
  767. data.Entries.footer = bytes.Buffer{}
  768. data.Entries.write(w)
  769. for _, extra := range data.Extra {
  770. extra(w, data.OutputFile.Path())
  771. }
  772. fmt.Fprintln(w, "include "+data.Include)
  773. }
  774. func translateAndroidMkEntriesModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
  775. provider AndroidMkEntriesProvider) error {
  776. if shouldSkipAndroidMkProcessing(mod.(Module).base()) {
  777. return nil
  778. }
  779. // Any new or special cases here need review to verify correct propagation of license information.
  780. for _, entries := range provider.AndroidMkEntries() {
  781. entries.fillInEntries(ctx, mod)
  782. entries.write(w)
  783. }
  784. return nil
  785. }
  786. func ShouldSkipAndroidMkProcessing(module Module) bool {
  787. return shouldSkipAndroidMkProcessing(module.base())
  788. }
  789. func shouldSkipAndroidMkProcessing(module *ModuleBase) bool {
  790. if !module.commonProperties.NamespaceExportedToMake {
  791. // TODO(jeffrygaston) do we want to validate that there are no modules being
  792. // exported to Kati that depend on this module?
  793. return true
  794. }
  795. // On Mac, only expose host darwin modules to Make, as that's all we claim to support.
  796. // In reality, some of them depend on device-built (Java) modules, so we can't disable all
  797. // device modules in Soong, but we can hide them from Make (and thus the build user interface)
  798. if runtime.GOOS == "darwin" && module.Os() != Darwin {
  799. return true
  800. }
  801. // Only expose the primary Darwin target, as Make does not understand Darwin+Arm64
  802. if module.Os() == Darwin && module.Target().HostCross {
  803. return true
  804. }
  805. return !module.Enabled() ||
  806. module.commonProperties.HideFromMake ||
  807. // Make does not understand LinuxBionic
  808. module.Os() == LinuxBionic
  809. }
  810. // A utility func to format LOCAL_TEST_DATA outputs. See the comments on DataPath to understand how
  811. // to use this func.
  812. func AndroidMkDataPaths(data []DataPath) []string {
  813. var testFiles []string
  814. for _, d := range data {
  815. rel := d.SrcPath.Rel()
  816. path := d.SrcPath.String()
  817. // LOCAL_TEST_DATA requires the rel portion of the path to be removed from the path.
  818. if !strings.HasSuffix(path, rel) {
  819. panic(fmt.Errorf("path %q does not end with %q", path, rel))
  820. }
  821. path = strings.TrimSuffix(path, rel)
  822. testFileString := path + ":" + rel
  823. if len(d.RelativeInstallPath) > 0 {
  824. testFileString += ":" + d.RelativeInstallPath
  825. }
  826. testFiles = append(testFiles, testFileString)
  827. }
  828. return testFiles
  829. }