blob: 5856851494c17100c70536b2491856340ff4b854 [file] [log] [blame]
Dan Willemsen218f6562015-07-08 18:13:11 -07001// 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
Jaewoong Jung7ef4a902020-11-16 12:50:29 -080015// This file offers AndroidMkEntriesProvider, which individual modules implement to output
16// Android.mk entries that contain information about the modules built through Soong. Kati reads
17// and combines them with the legacy Make-based module definitions to produce the complete view of
18// the source tree, which makes this a critical point of Make-Soong interoperability.
19//
20// Naturally, Soong-only builds do not rely on this mechanism.
21
Colin Cross635c3b02016-05-18 15:37:25 -070022package android
Dan Willemsen218f6562015-07-08 18:13:11 -070023
24import (
25 "bytes"
Dan Willemsen97750522016-02-09 17:43:51 -080026 "fmt"
Dan Willemsen218f6562015-07-08 18:13:11 -070027 "io"
28 "io/ioutil"
29 "os"
30 "path/filepath"
Bob Badourb4999222021-01-07 03:34:31 +000031 "reflect"
Dan Willemsen218f6562015-07-08 18:13:11 -070032 "sort"
Dan Willemsen0fda89f2016-06-01 15:25:32 -070033 "strings"
Dan Willemsen218f6562015-07-08 18:13:11 -070034
Dan Willemsen218f6562015-07-08 18:13:11 -070035 "github.com/google/blueprint"
Colin Cross2465c3d2018-09-28 10:19:18 -070036 "github.com/google/blueprint/bootstrap"
Dan Willemsen218f6562015-07-08 18:13:11 -070037)
38
39func init() {
Paul Duffin8c3fec42020-03-04 20:15:08 +000040 RegisterAndroidMkBuildComponents(InitRegistrationContext)
41}
42
43func RegisterAndroidMkBuildComponents(ctx RegistrationContext) {
44 ctx.RegisterSingletonType("androidmk", AndroidMkSingleton)
Dan Willemsen218f6562015-07-08 18:13:11 -070045}
46
Jaewoong Jung7ef4a902020-11-16 12:50:29 -080047// Deprecated: Use AndroidMkEntriesProvider instead, especially if you're not going to use the
48// Custom function. It's easier to use and test.
Dan Willemsen218f6562015-07-08 18:13:11 -070049type AndroidMkDataProvider interface {
Colin Crossa18e9cf2017-08-10 17:00:19 -070050 AndroidMk() AndroidMkData
Colin Crossce75d2c2016-10-06 16:12:58 -070051 BaseModuleName() string
Dan Willemsen218f6562015-07-08 18:13:11 -070052}
53
54type AndroidMkData struct {
Sasha Smundakb6d23052019-04-01 18:37:36 -070055 Class string
56 SubName string
Jingwen Chen40fd90a2020-06-15 05:24:19 +000057 DistFiles TaggedDistFiles
Sasha Smundakb6d23052019-04-01 18:37:36 -070058 OutputFile OptionalPath
59 Disabled bool
60 Include string
61 Required []string
62 Host_required []string
63 Target_required []string
Dan Willemsen218f6562015-07-08 18:13:11 -070064
Colin Cross0f86d182017-08-10 17:07:28 -070065 Custom func(w io.Writer, name, prefix, moduleDir string, data AndroidMkData)
Dan Willemsen218f6562015-07-08 18:13:11 -070066
Colin Cross27a4b052017-08-10 16:32:23 -070067 Extra []AndroidMkExtraFunc
Colin Cross0f86d182017-08-10 17:07:28 -070068
Jooyung Han2ed99d02020-06-24 23:26:26 +090069 Entries AndroidMkEntries
Dan Willemsen218f6562015-07-08 18:13:11 -070070}
71
Colin Cross27a4b052017-08-10 16:32:23 -070072type AndroidMkExtraFunc func(w io.Writer, outputFile Path)
73
Jaewoong Jung7ef4a902020-11-16 12:50:29 -080074// Interface for modules to declare their Android.mk outputs. Note that every module needs to
75// implement this in order to be included in the final Android-<product_name>.mk output, even if
76// they only need to output the common set of entries without any customizations.
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -070077type AndroidMkEntriesProvider interface {
Jaewoong Jung7ef4a902020-11-16 12:50:29 -080078 // Returns AndroidMkEntries objects that contain all basic info plus extra customization data
79 // if needed. This is the core func to implement.
80 // Note that one can return multiple objects. For example, java_library may return an additional
81 // AndroidMkEntries object for its hostdex sub-module.
Jiyong Park0b0e1b92019-12-03 13:24:29 +090082 AndroidMkEntries() []AndroidMkEntries
Jaewoong Jung7ef4a902020-11-16 12:50:29 -080083 // Modules don't need to implement this as it's already implemented by ModuleBase.
84 // AndroidMkEntries uses BaseModuleName() instead of ModuleName() because certain modules
85 // e.g. Prebuilts, override the Name() func and return modified names.
86 // If a different name is preferred, use SubName or OverrideName in AndroidMkEntries.
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -070087 BaseModuleName() string
88}
89
Jaewoong Jung7ef4a902020-11-16 12:50:29 -080090// The core data struct that modules use to provide their Android.mk data.
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -070091type AndroidMkEntries struct {
Jaewoong Jung7ef4a902020-11-16 12:50:29 -080092 // Android.mk class string, e.g EXECUTABLES, JAVA_LIBRARIES, ETC
93 Class string
94 // Optional suffix to append to the module name. Useful when a module wants to return multiple
95 // AndroidMkEntries objects. For example, when a java_library returns an additional entry for
96 // its hostdex sub-module, this SubName field is set to "-hostdex" so that it can have a
97 // different name than the parent's.
98 SubName string
99 // If set, this value overrides the base module name. SubName is still appended.
100 OverrideName string
101 // Dist files to output
102 DistFiles TaggedDistFiles
103 // The output file for Kati to process and/or install. If absent, the module is skipped.
104 OutputFile OptionalPath
105 // If true, the module is skipped and does not appear on the final Android-<product name>.mk
106 // file. Useful when a module needs to be skipped conditionally.
107 Disabled bool
108 // The postprocessing mk file to include, e.g. $(BUILD_SYSTEM)/soong_cc_prebuilt.mk
109 // If not set, $(BUILD_SYSTEM)/prebuilt.mk is used.
110 Include string
111 // Required modules that need to be built and included in the final build output when building
112 // this module.
113 Required []string
114 // Required host modules that need to be built and included in the final build output when
115 // building this module.
116 Host_required []string
117 // Required device modules that need to be built and included in the final build output when
118 // building this module.
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700119 Target_required []string
120
121 header bytes.Buffer
122 footer bytes.Buffer
123
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800124 // Funcs to append additional Android.mk entries or modify the common ones. Multiple funcs are
125 // accepted so that common logic can be factored out as a shared func.
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700126 ExtraEntries []AndroidMkExtraEntriesFunc
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800127 // Funcs to add extra lines to the module's Android.mk output. Unlike AndroidMkExtraEntriesFunc,
128 // which simply sets Make variable values, this can be used for anything since it can write any
129 // Make statements directly to the final Android-*.mk file.
130 // Primarily used to call macros or declare/update Make targets.
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700131 ExtraFooters []AndroidMkExtraFootersFunc
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700132
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800133 // A map that holds the up-to-date Make variable values. Can be accessed from tests.
134 EntryMap map[string][]string
135 // A list of EntryMap keys in insertion order. This serves a few purposes:
136 // 1. Prevents churns. Golang map doesn't provide consistent iteration order, so without this,
137 // the outputted Android-*.mk file may change even though there have been no content changes.
138 // 2. Allows modules to refer to other variables, like LOCAL_BAR_VAR := $(LOCAL_FOO_VAR),
139 // without worrying about the variables being mixed up in the actual mk file.
140 // 3. Makes troubleshooting and spotting errors easier.
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700141 entryOrder []string
142}
143
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700144type AndroidMkExtraEntriesFunc func(entries *AndroidMkEntries)
Jaewoong Jung02b11a62020-12-07 10:23:54 -0800145type AndroidMkExtraFootersFunc func(w io.Writer, name, prefix, moduleDir string)
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700146
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800147// Utility funcs to manipulate Android.mk variable entries.
148
149// SetString sets a Make variable with the given name to the given value.
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700150func (a *AndroidMkEntries) SetString(name, value string) {
151 if _, ok := a.EntryMap[name]; !ok {
152 a.entryOrder = append(a.entryOrder, name)
153 }
154 a.EntryMap[name] = []string{value}
155}
156
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800157// SetPath sets a Make variable with the given name to the given path string.
Jaewoong Jung9a1e8bd2019-09-04 20:17:54 -0700158func (a *AndroidMkEntries) SetPath(name string, path Path) {
159 if _, ok := a.EntryMap[name]; !ok {
160 a.entryOrder = append(a.entryOrder, name)
161 }
162 a.EntryMap[name] = []string{path.String()}
163}
164
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800165// SetOptionalPath sets a Make variable with the given name to the given path string if it is valid.
166// It is a no-op if the given path is invalid.
Colin Crossc0efd1d2020-07-03 11:56:24 -0700167func (a *AndroidMkEntries) SetOptionalPath(name string, path OptionalPath) {
168 if path.Valid() {
169 a.SetPath(name, path.Path())
170 }
171}
172
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800173// AddPath appends the given path string to a Make variable with the given name.
Colin Crossc0efd1d2020-07-03 11:56:24 -0700174func (a *AndroidMkEntries) AddPath(name string, path Path) {
175 if _, ok := a.EntryMap[name]; !ok {
176 a.entryOrder = append(a.entryOrder, name)
177 }
178 a.EntryMap[name] = append(a.EntryMap[name], path.String())
179}
180
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800181// 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.
Colin Crossc0efd1d2020-07-03 11:56:24 -0700183func (a *AndroidMkEntries) AddOptionalPath(name string, path OptionalPath) {
184 if path.Valid() {
185 a.AddPath(name, path.Path())
186 }
187}
188
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800189// SetPaths sets a Make variable with the given name to a slice of the given path strings.
Colin Cross08dca382020-07-21 20:31:17 -0700190func (a *AndroidMkEntries) SetPaths(name string, paths Paths) {
191 if _, ok := a.EntryMap[name]; !ok {
192 a.entryOrder = append(a.entryOrder, name)
193 }
194 a.EntryMap[name] = paths.Strings()
195}
196
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800197// SetOptionalPaths sets a Make variable with the given name to a slice of the given path strings
198// only if there are a non-zero amount of paths.
Colin Cross08dca382020-07-21 20:31:17 -0700199func (a *AndroidMkEntries) SetOptionalPaths(name string, paths Paths) {
200 if len(paths) > 0 {
201 a.SetPaths(name, paths)
202 }
203}
204
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800205// AddPaths appends the given path strings to a Make variable with the given name.
Colin Cross08dca382020-07-21 20:31:17 -0700206func (a *AndroidMkEntries) AddPaths(name string, paths Paths) {
207 if _, ok := a.EntryMap[name]; !ok {
208 a.entryOrder = append(a.entryOrder, name)
209 }
210 a.EntryMap[name] = append(a.EntryMap[name], paths.Strings()...)
211}
212
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800213// SetBoolIfTrue sets a Make variable with the given name to true if the given flag is true.
214// It is a no-op if the given flag is false.
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700215func (a *AndroidMkEntries) SetBoolIfTrue(name string, flag bool) {
216 if flag {
217 if _, ok := a.EntryMap[name]; !ok {
218 a.entryOrder = append(a.entryOrder, name)
219 }
220 a.EntryMap[name] = []string{"true"}
221 }
222}
223
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800224// SetBool sets a Make variable with the given name to if the given bool flag value.
Jaewoong Jung9a1e8bd2019-09-04 20:17:54 -0700225func (a *AndroidMkEntries) SetBool(name string, flag bool) {
226 if _, ok := a.EntryMap[name]; !ok {
227 a.entryOrder = append(a.entryOrder, name)
228 }
229 if flag {
230 a.EntryMap[name] = []string{"true"}
231 } else {
232 a.EntryMap[name] = []string{"false"}
233 }
234}
235
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800236// AddStrings appends the given strings to a Make variable with the given name.
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700237func (a *AndroidMkEntries) AddStrings(name string, value ...string) {
238 if len(value) == 0 {
239 return
240 }
241 if _, ok := a.EntryMap[name]; !ok {
242 a.entryOrder = append(a.entryOrder, name)
243 }
244 a.EntryMap[name] = append(a.EntryMap[name], value...)
245}
246
Liz Kammer57f5b332020-11-24 12:42:58 -0800247// AddCompatibilityTestSuites adds the supplied test suites to the EntryMap, with special handling
248// for partial MTS test suites.
249func (a *AndroidMkEntries) AddCompatibilityTestSuites(suites ...string) {
250 // MTS supports a full test suite and partial per-module MTS test suites, with naming mts-${MODULE}.
251 // To reduce repetition, if we find a partial MTS test suite without an full MTS test suite,
252 // we add the full test suite to our list.
253 if PrefixInList(suites, "mts-") && !InList("mts", suites) {
254 suites = append(suites, "mts")
255 }
256 a.AddStrings("LOCAL_COMPATIBILITY_SUITE", suites...)
257}
258
Paul Duffin8b0349c2020-11-26 14:33:21 +0000259// The contributions to the dist.
260type distContributions struct {
261 // List of goals and the dist copy instructions.
262 copiesForGoals []*copiesForGoals
263}
264
265// getCopiesForGoals returns a copiesForGoals into which copy instructions that
266// must be processed when building one or more of those goals can be added.
267func (d *distContributions) getCopiesForGoals(goals string) *copiesForGoals {
268 copiesForGoals := &copiesForGoals{goals: goals}
269 d.copiesForGoals = append(d.copiesForGoals, copiesForGoals)
270 return copiesForGoals
271}
272
273// Associates a list of dist copy instructions with a set of goals for which they
274// should be run.
275type copiesForGoals struct {
276 // goals are a space separated list of build targets that will trigger the
277 // copy instructions.
278 goals string
279
280 // A list of instructions to copy a module's output files to somewhere in the
281 // dist directory.
282 copies []distCopy
283}
284
285// Adds a copy instruction.
286func (d *copiesForGoals) addCopyInstruction(from Path, dest string) {
287 d.copies = append(d.copies, distCopy{from, dest})
288}
289
290// Instruction on a path that must be copied into the dist.
291type distCopy struct {
292 // The path to copy from.
293 from Path
294
295 // The destination within the dist directory to copy to.
296 dest string
297}
298
299// Compute the contributions that the module makes to the dist.
300func (a *AndroidMkEntries) getDistContributions(mod blueprint.Module) *distContributions {
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000301 amod := mod.(Module).base()
302 name := amod.BaseModuleName()
303
Paul Duffin74f05592020-11-25 16:37:46 +0000304 // Collate the set of associated tag/paths available for copying to the dist.
305 // Start with an empty (nil) set.
Jingwen Chen7b27ca72020-07-24 09:13:49 +0000306 var availableTaggedDists TaggedDistFiles
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000307
Paul Duffin74f05592020-11-25 16:37:46 +0000308 // Then merge in any that are provided explicitly by the module.
Jingwen Chen84811862020-07-21 11:32:19 +0000309 if a.DistFiles != nil {
Paul Duffin74f05592020-11-25 16:37:46 +0000310 // Merge the DistFiles into the set.
311 availableTaggedDists = availableTaggedDists.merge(a.DistFiles)
312 }
313
314 // If no paths have been provided for the DefaultDistTag and the output file is
315 // valid then add that as the default dist path.
316 if _, ok := availableTaggedDists[DefaultDistTag]; !ok && a.OutputFile.Valid() {
317 availableTaggedDists = availableTaggedDists.addPathsForTag(DefaultDistTag, a.OutputFile.Path())
318 }
319
Paul Duffinaf970a22020-11-23 23:32:56 +0000320 // If the distFiles created by GenerateTaggedDistFiles contains paths for the
321 // DefaultDistTag then that takes priority so delete any existing paths.
322 if _, ok := amod.distFiles[DefaultDistTag]; ok {
323 delete(availableTaggedDists, DefaultDistTag)
324 }
325
326 // Finally, merge the distFiles created by GenerateTaggedDistFiles.
327 availableTaggedDists = availableTaggedDists.merge(amod.distFiles)
328
Paul Duffin74f05592020-11-25 16:37:46 +0000329 if len(availableTaggedDists) == 0 {
Jingwen Chen7b27ca72020-07-24 09:13:49 +0000330 // Nothing dist-able for this module.
331 return nil
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000332 }
333
Paul Duffin8b0349c2020-11-26 14:33:21 +0000334 // Collate the contributions this module makes to the dist.
335 distContributions := &distContributions{}
336
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000337 // Iterate over this module's dist structs, merged from the dist and dists properties.
338 for _, dist := range amod.Dists() {
339 // Get the list of goals this dist should be enabled for. e.g. sdk, droidcore
340 goals := strings.Join(dist.Targets, " ")
341
342 // Get the tag representing the output files to be dist'd. e.g. ".jar", ".proguard_map"
343 var tag string
344 if dist.Tag == nil {
345 // If the dist struct does not specify a tag, use the default output files tag.
Paul Duffin74f05592020-11-25 16:37:46 +0000346 tag = DefaultDistTag
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000347 } else {
348 tag = *dist.Tag
349 }
350
351 // Get the paths of the output files to be dist'd, represented by the tag.
352 // Can be an empty list.
353 tagPaths := availableTaggedDists[tag]
354 if len(tagPaths) == 0 {
355 // Nothing to dist for this tag, continue to the next dist.
356 continue
357 }
358
359 if len(tagPaths) > 1 && (dist.Dest != nil || dist.Suffix != nil) {
Paul Duffin74f05592020-11-25 16:37:46 +0000360 errorMessage := "%s: Cannot apply dest/suffix for more than one dist " +
361 "file for %q goals tag %q in module %s. The list of dist files, " +
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000362 "which should have a single element, is:\n%s"
Paul Duffin74f05592020-11-25 16:37:46 +0000363 panic(fmt.Errorf(errorMessage, mod, goals, tag, name, tagPaths))
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000364 }
365
Paul Duffin8b0349c2020-11-26 14:33:21 +0000366 copiesForGoals := distContributions.getCopiesForGoals(goals)
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000367
Paul Duffin8b0349c2020-11-26 14:33:21 +0000368 // Iterate over each path adding a copy instruction to copiesForGoals
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000369 for _, path := range tagPaths {
370 // It's possible that the Path is nil from errant modules. Be defensive here.
371 if path == nil {
372 tagName := "default" // for error message readability
373 if dist.Tag != nil {
374 tagName = *dist.Tag
375 }
376 panic(fmt.Errorf("Dist file should not be nil for the %s tag in %s", tagName, name))
377 }
378
379 dest := filepath.Base(path.String())
380
381 if dist.Dest != nil {
382 var err error
383 if dest, err = validateSafePath(*dist.Dest); err != nil {
384 // This was checked in ModuleBase.GenerateBuildActions
385 panic(err)
386 }
387 }
388
389 if dist.Suffix != nil {
390 ext := filepath.Ext(dest)
391 suffix := *dist.Suffix
392 dest = strings.TrimSuffix(dest, ext) + suffix + ext
393 }
394
395 if dist.Dir != nil {
396 var err error
397 if dest, err = validateSafePath(*dist.Dir, dest); err != nil {
398 // This was checked in ModuleBase.GenerateBuildActions
399 panic(err)
400 }
401 }
402
Paul Duffin8b0349c2020-11-26 14:33:21 +0000403 copiesForGoals.addCopyInstruction(path, dest)
404 }
405 }
406
407 return distContributions
408}
409
410// generateDistContributionsForMake generates make rules that will generate the
411// dist according to the instructions in the supplied distContribution.
412func generateDistContributionsForMake(distContributions *distContributions) []string {
413 var ret []string
414 for _, d := range distContributions.copiesForGoals {
415 ret = append(ret, fmt.Sprintf(".PHONY: %s\n", d.goals))
416 // Create dist-for-goals calls for each of the copy instructions.
417 for _, c := range d.copies {
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000418 ret = append(
419 ret,
Paul Duffin8b0349c2020-11-26 14:33:21 +0000420 fmt.Sprintf("$(call dist-for-goals,%s,%s:%s)\n", d.goals, c.from.String(), c.dest))
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000421 }
422 }
423
424 return ret
425}
426
Paul Duffin8b0349c2020-11-26 14:33:21 +0000427// Compute the list of Make strings to declare phony goals and dist-for-goals
428// calls from the module's dist and dists properties.
429func (a *AndroidMkEntries) GetDistForGoals(mod blueprint.Module) []string {
430 distContributions := a.getDistContributions(mod)
431 if distContributions == nil {
432 return nil
433 }
434
435 return generateDistContributionsForMake(distContributions)
436}
437
Bob Badourb4999222021-01-07 03:34:31 +0000438// Write the license variables to Make for AndroidMkData.Custom(..) methods that do not call WriteAndroidMkData(..)
439// It's required to propagate the license metadata even for module types that have non-standard interfaces to Make.
440func (a *AndroidMkEntries) WriteLicenseVariables(w io.Writer) {
441 fmt.Fprintln(w, "LOCAL_LICENSE_KINDS :=", strings.Join(a.EntryMap["LOCAL_LICENSE_KINDS"], " "))
442 fmt.Fprintln(w, "LOCAL_LICENSE_CONDITIONS :=", strings.Join(a.EntryMap["LOCAL_LICENSE_CONDITIONS"], " "))
443 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", strings.Join(a.EntryMap["LOCAL_NOTICE_FILE"], " "))
444 if pn, ok := a.EntryMap["LOCAL_LICENSE_PACKAGE_NAME"]; ok {
445 fmt.Fprintln(w, "LOCAL_LICENSE_PACKAGE_NAME :=", strings.Join(pn, " "))
446 }
447}
448
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800449// fillInEntries goes through the common variable processing and calls the extra data funcs to
450// generate and fill in AndroidMkEntries's in-struct data, ready to be flushed to a file.
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700451func (a *AndroidMkEntries) fillInEntries(config Config, bpPath string, mod blueprint.Module) {
452 a.EntryMap = make(map[string][]string)
453 amod := mod.(Module).base()
454 name := amod.BaseModuleName()
Colin Cross0477b422020-10-13 18:43:54 -0700455 if a.OverrideName != "" {
456 name = a.OverrideName
457 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700458
459 if a.Include == "" {
460 a.Include = "$(BUILD_PREBUILT)"
461 }
462 a.Required = append(a.Required, amod.commonProperties.Required...)
463 a.Host_required = append(a.Host_required, amod.commonProperties.Host_required...)
464 a.Target_required = append(a.Target_required, amod.commonProperties.Target_required...)
465
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000466 for _, distString := range a.GetDistForGoals(mod) {
467 fmt.Fprintf(&a.header, distString)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700468 }
469
470 fmt.Fprintln(&a.header, "\ninclude $(CLEAR_VARS)")
471
472 // Collect make variable assignment entries.
473 a.SetString("LOCAL_PATH", filepath.Dir(bpPath))
474 a.SetString("LOCAL_MODULE", name+a.SubName)
Bob Badourb4999222021-01-07 03:34:31 +0000475 a.AddStrings("LOCAL_LICENSE_KINDS", amod.commonProperties.Effective_license_kinds...)
476 a.AddStrings("LOCAL_LICENSE_CONDITIONS", amod.commonProperties.Effective_license_conditions...)
477 a.AddStrings("LOCAL_NOTICE_FILE", amod.commonProperties.Effective_license_text...)
478 // TODO(b/151177513): Does this code need to set LOCAL_MODULE_IS_CONTAINER ?
479 if amod.commonProperties.Effective_package_name != nil {
480 a.SetString("LOCAL_LICENSE_PACKAGE_NAME", *amod.commonProperties.Effective_package_name)
481 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700482 a.SetString("LOCAL_MODULE_CLASS", a.Class)
483 a.SetString("LOCAL_PREBUILT_MODULE_FILE", a.OutputFile.String())
484 a.AddStrings("LOCAL_REQUIRED_MODULES", a.Required...)
485 a.AddStrings("LOCAL_HOST_REQUIRED_MODULES", a.Host_required...)
486 a.AddStrings("LOCAL_TARGET_REQUIRED_MODULES", a.Target_required...)
487
Jiyong Park89e850a2020-04-07 16:37:39 +0900488 if am, ok := mod.(ApexModule); ok {
489 a.SetBoolIfTrue("LOCAL_NOT_AVAILABLE_FOR_PLATFORM", am.NotAvailableForPlatform())
490 }
491
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700492 archStr := amod.Arch().ArchType.String()
493 host := false
494 switch amod.Os().Class {
495 case Host:
Jiyong Park1613e552020-09-14 19:43:17 +0900496 if amod.Target().HostCross {
497 // Make cannot identify LOCAL_MODULE_HOST_CROSS_ARCH:= common.
498 if amod.Arch().ArchType != Common {
499 a.SetString("LOCAL_MODULE_HOST_CROSS_ARCH", archStr)
500 }
501 } else {
502 // Make cannot identify LOCAL_MODULE_HOST_ARCH:= common.
503 if amod.Arch().ArchType != Common {
504 a.SetString("LOCAL_MODULE_HOST_ARCH", archStr)
505 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700506 }
507 host = true
508 case Device:
509 // Make cannot identify LOCAL_MODULE_TARGET_ARCH:= common.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700510 if amod.Arch().ArchType != Common {
dimitry1f33e402019-03-26 12:39:31 +0100511 if amod.Target().NativeBridge {
dimitry8d6dde82019-07-11 10:23:53 +0200512 hostArchStr := amod.Target().NativeBridgeHostArchName
dimitry1f33e402019-03-26 12:39:31 +0100513 if hostArchStr != "" {
514 a.SetString("LOCAL_MODULE_TARGET_ARCH", hostArchStr)
515 }
516 } else {
517 a.SetString("LOCAL_MODULE_TARGET_ARCH", archStr)
518 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700519 }
520
Yifan Hong919dae12020-12-02 18:55:06 -0800521 if !amod.InRamdisk() && !amod.InVendorRamdisk() {
522 a.AddStrings("LOCAL_INIT_RC", amod.commonProperties.Init_rc...)
523 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700524 a.AddStrings("LOCAL_VINTF_FRAGMENTS", amod.commonProperties.Vintf_fragments...)
525 a.SetBoolIfTrue("LOCAL_PROPRIETARY_MODULE", Bool(amod.commonProperties.Proprietary))
526 if Bool(amod.commonProperties.Vendor) || Bool(amod.commonProperties.Soc_specific) {
527 a.SetString("LOCAL_VENDOR_MODULE", "true")
528 }
529 a.SetBoolIfTrue("LOCAL_ODM_MODULE", Bool(amod.commonProperties.Device_specific))
530 a.SetBoolIfTrue("LOCAL_PRODUCT_MODULE", Bool(amod.commonProperties.Product_specific))
Justin Yund5f6c822019-06-25 16:47:17 +0900531 a.SetBoolIfTrue("LOCAL_SYSTEM_EXT_MODULE", Bool(amod.commonProperties.System_ext_specific))
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700532 if amod.commonProperties.Owner != nil {
533 a.SetString("LOCAL_MODULE_OWNER", *amod.commonProperties.Owner)
534 }
535 }
536
Bob Badoura75b0572020-02-18 20:21:55 -0800537 if len(amod.noticeFiles) > 0 {
538 a.SetString("LOCAL_NOTICE_FILE", strings.Join(amod.noticeFiles.Strings(), " "))
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700539 }
540
541 if host {
542 makeOs := amod.Os().String()
543 if amod.Os() == Linux || amod.Os() == LinuxBionic {
544 makeOs = "linux"
545 }
546 a.SetString("LOCAL_MODULE_HOST_OS", makeOs)
547 a.SetString("LOCAL_IS_HOST_MODULE", "true")
548 }
549
550 prefix := ""
551 if amod.ArchSpecific() {
552 switch amod.Os().Class {
553 case Host:
Jiyong Park1613e552020-09-14 19:43:17 +0900554 if amod.Target().HostCross {
555 prefix = "HOST_CROSS_"
556 } else {
557 prefix = "HOST_"
558 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700559 case Device:
560 prefix = "TARGET_"
561
562 }
563
564 if amod.Arch().ArchType != config.Targets[amod.Os()][0].Arch.ArchType {
565 prefix = "2ND_" + prefix
566 }
567 }
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700568 for _, extra := range a.ExtraEntries {
569 extra(a)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700570 }
571
572 // Write to footer.
573 fmt.Fprintln(&a.footer, "include "+a.Include)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700574 blueprintDir := filepath.Dir(bpPath)
575 for _, footerFunc := range a.ExtraFooters {
Jaewoong Jung02b11a62020-12-07 10:23:54 -0800576 footerFunc(&a.footer, name, prefix, blueprintDir)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700577 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700578}
579
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800580// write flushes the AndroidMkEntries's in-struct data populated by AndroidMkEntries into the
581// given Writer object.
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700582func (a *AndroidMkEntries) write(w io.Writer) {
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700583 if a.Disabled {
584 return
585 }
586
587 if !a.OutputFile.Valid() {
588 return
589 }
590
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700591 w.Write(a.header.Bytes())
592 for _, name := range a.entryOrder {
593 fmt.Fprintln(w, name+" := "+strings.Join(a.EntryMap[name], " "))
594 }
595 w.Write(a.footer.Bytes())
596}
597
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700598func (a *AndroidMkEntries) FooterLinesForTests() []string {
599 return strings.Split(string(a.footer.Bytes()), "\n")
600}
601
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800602// AndroidMkSingleton is a singleton to collect Android.mk data from all modules and dump them into
603// the final Android-<product_name>.mk file output.
Colin Cross0875c522017-11-28 17:34:01 -0800604func AndroidMkSingleton() Singleton {
Dan Willemsen218f6562015-07-08 18:13:11 -0700605 return &androidMkSingleton{}
606}
607
608type androidMkSingleton struct{}
609
Colin Cross0875c522017-11-28 17:34:01 -0800610func (c *androidMkSingleton) GenerateBuildActions(ctx SingletonContext) {
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800611 // Skip if Soong wasn't invoked from Make.
Jingwen Chencda22c92020-11-23 00:22:30 -0500612 if !ctx.Config().KatiEnabled() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800613 return
614 }
615
Colin Cross2465c3d2018-09-28 10:19:18 -0700616 var androidMkModulesList []blueprint.Module
Colin Cross4f6e4e62016-01-11 12:55:55 -0800617
Colin Cross2465c3d2018-09-28 10:19:18 -0700618 ctx.VisitAllModulesBlueprint(func(module blueprint.Module) {
Colin Cross0875c522017-11-28 17:34:01 -0800619 androidMkModulesList = append(androidMkModulesList, module)
Colin Cross4f6e4e62016-01-11 12:55:55 -0800620 })
Dan Willemsen218f6562015-07-08 18:13:11 -0700621
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800622 // Sort the module list by the module names to eliminate random churns, which may erroneously
623 // invoke additional build processes.
Colin Cross1ad81422019-01-14 12:47:35 -0800624 sort.SliceStable(androidMkModulesList, func(i, j int) bool {
625 return ctx.ModuleName(androidMkModulesList[i]) < ctx.ModuleName(androidMkModulesList[j])
626 })
Colin Crossd779da42015-12-17 18:00:23 -0800627
Dan Willemsen45133ac2018-03-09 21:22:06 -0800628 transMk := PathForOutput(ctx, "Android"+String(ctx.Config().productVariables.Make_suffix)+".mk")
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700629 if ctx.Failed() {
630 return
631 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700632
Colin Cross988414c2020-01-11 01:11:46 +0000633 err := translateAndroidMk(ctx, absolutePath(transMk.String()), androidMkModulesList)
Dan Willemsen218f6562015-07-08 18:13:11 -0700634 if err != nil {
635 ctx.Errorf(err.Error())
636 }
637
Colin Cross0875c522017-11-28 17:34:01 -0800638 ctx.Build(pctx, BuildParams{
639 Rule: blueprint.Phony,
640 Output: transMk,
Dan Willemsen218f6562015-07-08 18:13:11 -0700641 })
642}
643
Colin Cross2465c3d2018-09-28 10:19:18 -0700644func translateAndroidMk(ctx SingletonContext, mkFile string, mods []blueprint.Module) error {
Dan Willemsen218f6562015-07-08 18:13:11 -0700645 buf := &bytes.Buffer{}
646
Dan Willemsen97750522016-02-09 17:43:51 -0800647 fmt.Fprintln(buf, "LOCAL_MODULE_MAKEFILE := $(lastword $(MAKEFILE_LIST))")
Dan Willemsen218f6562015-07-08 18:13:11 -0700648
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800649 typeStats := make(map[string]int)
Dan Willemsen218f6562015-07-08 18:13:11 -0700650 for _, mod := range mods {
651 err := translateAndroidMkModule(ctx, buf, mod)
652 if err != nil {
653 os.Remove(mkFile)
654 return err
655 }
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700656
Colin Cross2465c3d2018-09-28 10:19:18 -0700657 if amod, ok := mod.(Module); ok && ctx.PrimaryModule(amod) == amod {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800658 typeStats[ctx.ModuleType(amod)] += 1
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700659 }
660 }
661
662 keys := []string{}
663 fmt.Fprintln(buf, "\nSTATS.SOONG_MODULE_TYPE :=")
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800664 for k := range typeStats {
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700665 keys = append(keys, k)
666 }
667 sort.Strings(keys)
668 for _, mod_type := range keys {
669 fmt.Fprintln(buf, "STATS.SOONG_MODULE_TYPE +=", mod_type)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800670 fmt.Fprintf(buf, "STATS.SOONG_MODULE_TYPE.%s := %d\n", mod_type, typeStats[mod_type])
Dan Willemsen218f6562015-07-08 18:13:11 -0700671 }
672
673 // Don't write to the file if it hasn't changed
Colin Cross988414c2020-01-11 01:11:46 +0000674 if _, err := os.Stat(absolutePath(mkFile)); !os.IsNotExist(err) {
675 if data, err := ioutil.ReadFile(absolutePath(mkFile)); err == nil {
Dan Willemsen218f6562015-07-08 18:13:11 -0700676 matches := buf.Len() == len(data)
677
678 if matches {
679 for i, value := range buf.Bytes() {
680 if value != data[i] {
681 matches = false
682 break
683 }
684 }
685 }
686
687 if matches {
688 return nil
689 }
690 }
691 }
692
Colin Cross988414c2020-01-11 01:11:46 +0000693 return ioutil.WriteFile(absolutePath(mkFile), buf.Bytes(), 0666)
Dan Willemsen218f6562015-07-08 18:13:11 -0700694}
695
Colin Cross0875c522017-11-28 17:34:01 -0800696func translateAndroidMkModule(ctx SingletonContext, w io.Writer, mod blueprint.Module) error {
Colin Cross953d3a22018-09-05 16:23:54 -0700697 defer func() {
698 if r := recover(); r != nil {
699 panic(fmt.Errorf("%s in translateAndroidMkModule for module %s variant %s",
700 r, ctx.ModuleName(mod), ctx.ModuleSubDir(mod)))
701 }
702 }()
703
Bob Badourb4999222021-01-07 03:34:31 +0000704 // Additional cases here require review for correct license propagation to make.
Colin Cross2465c3d2018-09-28 10:19:18 -0700705 switch x := mod.(type) {
706 case AndroidMkDataProvider:
707 return translateAndroidModule(ctx, w, mod, x)
708 case bootstrap.GoBinaryTool:
709 return translateGoBinaryModule(ctx, w, mod, x)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700710 case AndroidMkEntriesProvider:
711 return translateAndroidMkEntriesModule(ctx, w, mod, x)
Colin Cross2465c3d2018-09-28 10:19:18 -0700712 default:
Bob Badourb4999222021-01-07 03:34:31 +0000713 // Not exported to make so no make variables to set.
Dan Willemsen218f6562015-07-08 18:13:11 -0700714 return nil
715 }
Colin Cross2465c3d2018-09-28 10:19:18 -0700716}
717
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800718// A simple, special Android.mk entry output func to make it possible to build blueprint tools using
719// m by making them phony targets.
Colin Cross2465c3d2018-09-28 10:19:18 -0700720func translateGoBinaryModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
721 goBinary bootstrap.GoBinaryTool) error {
722
723 name := ctx.ModuleName(mod)
724 fmt.Fprintln(w, ".PHONY:", name)
725 fmt.Fprintln(w, name+":", goBinary.InstallPath())
726 fmt.Fprintln(w, "")
Bob Badourb4999222021-01-07 03:34:31 +0000727 // Assuming no rules in make include go binaries in distributables.
728 // If the assumption is wrong, make will fail to build without the necessary .meta_lic and .meta_module files.
729 // In that case, add the targets and rules here to build a .meta_lic file for `name` and a .meta_module for
730 // `goBinary.InstallPath()` pointing to the `name`.meta_lic file.
Colin Cross2465c3d2018-09-28 10:19:18 -0700731
732 return nil
733}
734
Jooyung Han12df5fb2019-07-11 16:18:47 +0900735func (data *AndroidMkData) fillInData(config Config, bpPath string, mod blueprint.Module) {
736 // Get the preamble content through AndroidMkEntries logic.
Jooyung Han2ed99d02020-06-24 23:26:26 +0900737 data.Entries = AndroidMkEntries{
Jooyung Han12df5fb2019-07-11 16:18:47 +0900738 Class: data.Class,
739 SubName: data.SubName,
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000740 DistFiles: data.DistFiles,
Jooyung Han12df5fb2019-07-11 16:18:47 +0900741 OutputFile: data.OutputFile,
742 Disabled: data.Disabled,
743 Include: data.Include,
744 Required: data.Required,
745 Host_required: data.Host_required,
746 Target_required: data.Target_required,
747 }
Jooyung Han2ed99d02020-06-24 23:26:26 +0900748 data.Entries.fillInEntries(config, bpPath, mod)
Jooyung Han12df5fb2019-07-11 16:18:47 +0900749
750 // copy entries back to data since it is used in Custom
Jooyung Han2ed99d02020-06-24 23:26:26 +0900751 data.Required = data.Entries.Required
752 data.Host_required = data.Entries.Host_required
753 data.Target_required = data.Entries.Target_required
Jooyung Han12df5fb2019-07-11 16:18:47 +0900754}
755
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800756// A support func for the deprecated AndroidMkDataProvider interface. Use AndroidMkEntryProvider
757// instead.
Colin Cross2465c3d2018-09-28 10:19:18 -0700758func translateAndroidModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
759 provider AndroidMkDataProvider) error {
Dan Willemsen218f6562015-07-08 18:13:11 -0700760
Colin Cross635c3b02016-05-18 15:37:25 -0700761 amod := mod.(Module).base()
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700762 if shouldSkipAndroidMkProcessing(amod) {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800763 return nil
764 }
765
Colin Cross91825d22017-08-10 16:59:47 -0700766 data := provider.AndroidMk()
Colin Cross53499412017-09-07 13:20:25 -0700767 if data.Include == "" {
768 data.Include = "$(BUILD_PREBUILT)"
769 }
770
Jooyung Han12df5fb2019-07-11 16:18:47 +0900771 data.fillInData(ctx.Config(), ctx.BlueprintFile(mod), mod)
Dan Willemsen01a405a2016-06-13 17:19:03 -0700772
Colin Cross0f86d182017-08-10 17:07:28 -0700773 prefix := ""
774 if amod.ArchSpecific() {
775 switch amod.Os().Class {
776 case Host:
Jiyong Park1613e552020-09-14 19:43:17 +0900777 if amod.Target().HostCross {
778 prefix = "HOST_CROSS_"
779 } else {
780 prefix = "HOST_"
781 }
Colin Cross0f86d182017-08-10 17:07:28 -0700782 case Device:
783 prefix = "TARGET_"
Colin Crossa2344662016-03-24 13:14:12 -0700784
Dan Willemsen218f6562015-07-08 18:13:11 -0700785 }
786
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700787 if amod.Arch().ArchType != ctx.Config().Targets[amod.Os()][0].Arch.ArchType {
Colin Cross0f86d182017-08-10 17:07:28 -0700788 prefix = "2ND_" + prefix
789 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700790 }
791
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700792 name := provider.BaseModuleName()
Colin Cross0f86d182017-08-10 17:07:28 -0700793 blueprintDir := filepath.Dir(ctx.BlueprintFile(mod))
794
795 if data.Custom != nil {
Bob Badourb4999222021-01-07 03:34:31 +0000796 // List of module types allowed to use .Custom(...)
797 // Additions to the list require careful review for proper license handling.
798 switch reflect.TypeOf(mod).String() { // ctx.ModuleType(mod) doesn't work: aidl_interface creates phony without type
799 case "*aidl.aidlApi": // writes non-custom before adding .phony
800 case "*aidl.aidlMapping": // writes non-custom before adding .phony
801 case "*android.customModule": // appears in tests only
802 case "*apex.apexBundle": // license properties written
803 case "*bpf.bpf": // license properties written (both for module and objs)
804 case "*genrule.Module": // writes non-custom before adding .phony
805 case "*java.SystemModules": // doesn't go through base_rules
806 case "*java.systemModulesImport": // doesn't go through base_rules
807 case "*phony.phony": // license properties written
808 case "*selinux.selinuxContextsModule": // license properties written
809 case "*sysprop.syspropLibrary": // license properties written
810 default:
811 if ctx.Config().IsEnvTrue("ANDROID_REQUIRE_LICENSES") {
812 return fmt.Errorf("custom make rules not allowed for %q (%q) module %q", ctx.ModuleType(mod), reflect.TypeOf(mod), ctx.ModuleName(mod))
813 }
814 }
Colin Cross0f86d182017-08-10 17:07:28 -0700815 data.Custom(w, name, prefix, blueprintDir, data)
816 } else {
817 WriteAndroidMkData(w, data)
818 }
819
820 return nil
821}
822
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800823// A support func for the deprecated AndroidMkDataProvider interface. Use AndroidMkEntryProvider
824// instead.
Colin Cross0f86d182017-08-10 17:07:28 -0700825func WriteAndroidMkData(w io.Writer, data AndroidMkData) {
826 if data.Disabled {
827 return
828 }
829
830 if !data.OutputFile.Valid() {
831 return
832 }
833
Jooyung Han2ed99d02020-06-24 23:26:26 +0900834 // write preamble via Entries
835 data.Entries.footer = bytes.Buffer{}
836 data.Entries.write(w)
Colin Cross0f86d182017-08-10 17:07:28 -0700837
Colin Crossca860ac2016-01-04 14:34:37 -0800838 for _, extra := range data.Extra {
Colin Cross27a4b052017-08-10 16:32:23 -0700839 extra(w, data.OutputFile.Path())
Dan Willemsen97750522016-02-09 17:43:51 -0800840 }
841
Colin Cross53499412017-09-07 13:20:25 -0700842 fmt.Fprintln(w, "include "+data.Include)
Dan Willemsen218f6562015-07-08 18:13:11 -0700843}
Sasha Smundakb6d23052019-04-01 18:37:36 -0700844
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700845func translateAndroidMkEntriesModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
846 provider AndroidMkEntriesProvider) error {
847 if shouldSkipAndroidMkProcessing(mod.(Module).base()) {
848 return nil
Sasha Smundakb6d23052019-04-01 18:37:36 -0700849 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700850
Bob Badourb4999222021-01-07 03:34:31 +0000851 // Any new or special cases here need review to verify correct propagation of license information.
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900852 for _, entries := range provider.AndroidMkEntries() {
853 entries.fillInEntries(ctx.Config(), ctx.BlueprintFile(mod), mod)
854 entries.write(w)
855 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700856
857 return nil
858}
859
860func shouldSkipAndroidMkProcessing(module *ModuleBase) bool {
861 if !module.commonProperties.NamespaceExportedToMake {
862 // TODO(jeffrygaston) do we want to validate that there are no modules being
863 // exported to Kati that depend on this module?
864 return true
Sasha Smundakb6d23052019-04-01 18:37:36 -0700865 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700866
867 return !module.Enabled() ||
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800868 module.commonProperties.HideFromMake ||
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700869 // Make does not understand LinuxBionic
870 module.Os() == LinuxBionic
Sasha Smundakb6d23052019-04-01 18:37:36 -0700871}
Dan Shi31949122020-09-21 12:11:02 -0700872
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800873// A utility func to format LOCAL_TEST_DATA outputs. See the comments on DataPath to understand how
874// to use this func.
Dan Shi31949122020-09-21 12:11:02 -0700875func AndroidMkDataPaths(data []DataPath) []string {
876 var testFiles []string
877 for _, d := range data {
878 rel := d.SrcPath.Rel()
879 path := d.SrcPath.String()
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800880 // LOCAL_TEST_DATA requires the rel portion of the path to be removed from the path.
Dan Shi31949122020-09-21 12:11:02 -0700881 if !strings.HasSuffix(path, rel) {
882 panic(fmt.Errorf("path %q does not end with %q", path, rel))
883 }
884 path = strings.TrimSuffix(path, rel)
885 testFileString := path + ":" + rel
886 if len(d.RelativeInstallPath) > 0 {
887 testFileString += ":" + d.RelativeInstallPath
888 }
889 testFiles = append(testFiles, testFileString)
890 }
891 return testFiles
892}