blob: a0ed1e449bf58949048603b1040f9915ed718607 [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"
Dan Willemsen218f6562015-07-08 18:13:11 -070028 "os"
29 "path/filepath"
Bob Badourb4999222021-01-07 03:34:31 +000030 "reflect"
Dan Willemsendef7b5d2021-10-17 00:22:33 -070031 "runtime"
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"
Colin Cross836e3872021-11-09 12:30:59 -080037 "github.com/google/blueprint/pathtools"
Dan Willemsen218f6562015-07-08 18:13:11 -070038)
39
40func init() {
Paul Duffin8c3fec42020-03-04 20:15:08 +000041 RegisterAndroidMkBuildComponents(InitRegistrationContext)
42}
43
44func RegisterAndroidMkBuildComponents(ctx RegistrationContext) {
LaMont Jones0c10e4d2023-05-16 00:58:37 +000045 ctx.RegisterParallelSingletonType("androidmk", AndroidMkSingleton)
Dan Willemsen218f6562015-07-08 18:13:11 -070046}
47
Paul Duffin6c9da042021-03-07 15:44:41 +000048// Enable androidmk support.
49// * Register the singleton
50// * Configure that we are inside make
51var PrepareForTestWithAndroidMk = GroupFixturePreparers(
52 FixtureRegisterWithContext(RegisterAndroidMkBuildComponents),
53 FixtureModifyConfig(SetKatiEnabledForTests),
54)
55
Jaewoong Jung7ef4a902020-11-16 12:50:29 -080056// Deprecated: Use AndroidMkEntriesProvider instead, especially if you're not going to use the
57// Custom function. It's easier to use and test.
Dan Willemsen218f6562015-07-08 18:13:11 -070058type AndroidMkDataProvider interface {
Colin Crossa18e9cf2017-08-10 17:00:19 -070059 AndroidMk() AndroidMkData
Colin Crossce75d2c2016-10-06 16:12:58 -070060 BaseModuleName() string
Dan Willemsen218f6562015-07-08 18:13:11 -070061}
62
63type AndroidMkData struct {
Sasha Smundakb6d23052019-04-01 18:37:36 -070064 Class string
65 SubName string
Jingwen Chen40fd90a2020-06-15 05:24:19 +000066 DistFiles TaggedDistFiles
Sasha Smundakb6d23052019-04-01 18:37:36 -070067 OutputFile OptionalPath
68 Disabled bool
69 Include string
70 Required []string
71 Host_required []string
72 Target_required []string
Dan Willemsen218f6562015-07-08 18:13:11 -070073
Colin Cross0f86d182017-08-10 17:07:28 -070074 Custom func(w io.Writer, name, prefix, moduleDir string, data AndroidMkData)
Dan Willemsen218f6562015-07-08 18:13:11 -070075
Colin Cross27a4b052017-08-10 16:32:23 -070076 Extra []AndroidMkExtraFunc
Colin Cross0f86d182017-08-10 17:07:28 -070077
Jooyung Han2ed99d02020-06-24 23:26:26 +090078 Entries AndroidMkEntries
Dan Willemsen218f6562015-07-08 18:13:11 -070079}
80
Colin Cross27a4b052017-08-10 16:32:23 -070081type AndroidMkExtraFunc func(w io.Writer, outputFile Path)
82
Jaewoong Jung7ef4a902020-11-16 12:50:29 -080083// Interface for modules to declare their Android.mk outputs. Note that every module needs to
84// implement this in order to be included in the final Android-<product_name>.mk output, even if
85// they only need to output the common set of entries without any customizations.
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -070086type AndroidMkEntriesProvider interface {
Jaewoong Jung7ef4a902020-11-16 12:50:29 -080087 // Returns AndroidMkEntries objects that contain all basic info plus extra customization data
88 // if needed. This is the core func to implement.
89 // Note that one can return multiple objects. For example, java_library may return an additional
90 // AndroidMkEntries object for its hostdex sub-module.
Jiyong Park0b0e1b92019-12-03 13:24:29 +090091 AndroidMkEntries() []AndroidMkEntries
Jaewoong Jung7ef4a902020-11-16 12:50:29 -080092 // Modules don't need to implement this as it's already implemented by ModuleBase.
93 // AndroidMkEntries uses BaseModuleName() instead of ModuleName() because certain modules
94 // e.g. Prebuilts, override the Name() func and return modified names.
95 // If a different name is preferred, use SubName or OverrideName in AndroidMkEntries.
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -070096 BaseModuleName() string
97}
98
Jaewoong Jung7ef4a902020-11-16 12:50:29 -080099// The core data struct that modules use to provide their Android.mk data.
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700100type AndroidMkEntries struct {
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800101 // Android.mk class string, e.g EXECUTABLES, JAVA_LIBRARIES, ETC
102 Class string
103 // Optional suffix to append to the module name. Useful when a module wants to return multiple
104 // AndroidMkEntries objects. For example, when a java_library returns an additional entry for
105 // its hostdex sub-module, this SubName field is set to "-hostdex" so that it can have a
106 // different name than the parent's.
107 SubName string
108 // If set, this value overrides the base module name. SubName is still appended.
109 OverrideName string
110 // Dist files to output
111 DistFiles TaggedDistFiles
112 // The output file for Kati to process and/or install. If absent, the module is skipped.
113 OutputFile OptionalPath
114 // If true, the module is skipped and does not appear on the final Android-<product name>.mk
115 // file. Useful when a module needs to be skipped conditionally.
116 Disabled bool
Ivan Lozanod06cc742021-11-12 13:27:58 -0500117 // The postprocessing mk file to include, e.g. $(BUILD_SYSTEM)/soong_cc_rust_prebuilt.mk
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800118 // If not set, $(BUILD_SYSTEM)/prebuilt.mk is used.
119 Include string
120 // Required modules that need to be built and included in the final build output when building
121 // this module.
122 Required []string
123 // Required host modules that need to be built and included in the final build output when
124 // building this module.
125 Host_required []string
126 // Required device modules that need to be built and included in the final build output when
127 // building this module.
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700128 Target_required []string
129
130 header bytes.Buffer
131 footer bytes.Buffer
132
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800133 // Funcs to append additional Android.mk entries or modify the common ones. Multiple funcs are
134 // accepted so that common logic can be factored out as a shared func.
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700135 ExtraEntries []AndroidMkExtraEntriesFunc
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800136 // Funcs to add extra lines to the module's Android.mk output. Unlike AndroidMkExtraEntriesFunc,
137 // which simply sets Make variable values, this can be used for anything since it can write any
138 // Make statements directly to the final Android-*.mk file.
139 // Primarily used to call macros or declare/update Make targets.
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700140 ExtraFooters []AndroidMkExtraFootersFunc
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700141
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800142 // A map that holds the up-to-date Make variable values. Can be accessed from tests.
143 EntryMap map[string][]string
144 // A list of EntryMap keys in insertion order. This serves a few purposes:
145 // 1. Prevents churns. Golang map doesn't provide consistent iteration order, so without this,
146 // the outputted Android-*.mk file may change even though there have been no content changes.
147 // 2. Allows modules to refer to other variables, like LOCAL_BAR_VAR := $(LOCAL_FOO_VAR),
148 // without worrying about the variables being mixed up in the actual mk file.
149 // 3. Makes troubleshooting and spotting errors easier.
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700150 entryOrder []string
Trevor Radcliffe90727f42022-03-21 19:34:02 +0000151
152 // Provides data typically stored by Context objects that are commonly needed by
153 //AndroidMkEntries objects.
154 entryContext AndroidMkEntriesContext
155}
156
157type AndroidMkEntriesContext interface {
158 Config() Config
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700159}
160
Colin Crossaa255532020-07-03 13:18:24 -0700161type AndroidMkExtraEntriesContext interface {
Colin Cross3c0a83d2023-12-12 14:13:26 -0800162 Provider(provider blueprint.AnyProviderKey) (any, bool)
Colin Crossaa255532020-07-03 13:18:24 -0700163}
164
165type androidMkExtraEntriesContext struct {
166 ctx fillInEntriesContext
167 mod blueprint.Module
168}
169
Colin Cross3c0a83d2023-12-12 14:13:26 -0800170func (a *androidMkExtraEntriesContext) Provider(provider blueprint.AnyProviderKey) (any, bool) {
171 return a.ctx.moduleProvider(a.mod, provider)
Colin Crossaa255532020-07-03 13:18:24 -0700172}
173
174type AndroidMkExtraEntriesFunc func(ctx AndroidMkExtraEntriesContext, entries *AndroidMkEntries)
Jaewoong Jung02b11a62020-12-07 10:23:54 -0800175type AndroidMkExtraFootersFunc func(w io.Writer, name, prefix, moduleDir string)
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700176
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800177// Utility funcs to manipulate Android.mk variable entries.
178
179// SetString sets a Make variable with the given name to the given value.
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700180func (a *AndroidMkEntries) SetString(name, value string) {
181 if _, ok := a.EntryMap[name]; !ok {
182 a.entryOrder = append(a.entryOrder, name)
183 }
184 a.EntryMap[name] = []string{value}
185}
186
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800187// SetPath sets a Make variable with the given name to the given path string.
Jaewoong Jung9a1e8bd2019-09-04 20:17:54 -0700188func (a *AndroidMkEntries) SetPath(name string, path Path) {
189 if _, ok := a.EntryMap[name]; !ok {
190 a.entryOrder = append(a.entryOrder, name)
191 }
192 a.EntryMap[name] = []string{path.String()}
193}
194
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800195// SetOptionalPath sets a Make variable with the given name to the given path string if it is valid.
196// It is a no-op if the given path is invalid.
Colin Crossc0efd1d2020-07-03 11:56:24 -0700197func (a *AndroidMkEntries) SetOptionalPath(name string, path OptionalPath) {
198 if path.Valid() {
199 a.SetPath(name, path.Path())
200 }
201}
202
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800203// AddPath appends the given path string to a Make variable with the given name.
Colin Crossc0efd1d2020-07-03 11:56:24 -0700204func (a *AndroidMkEntries) AddPath(name string, path Path) {
205 if _, ok := a.EntryMap[name]; !ok {
206 a.entryOrder = append(a.entryOrder, name)
207 }
208 a.EntryMap[name] = append(a.EntryMap[name], path.String())
209}
210
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800211// AddOptionalPath appends the given path string to a Make variable with the given name if it is
212// valid. It is a no-op if the given path is invalid.
Colin Crossc0efd1d2020-07-03 11:56:24 -0700213func (a *AndroidMkEntries) AddOptionalPath(name string, path OptionalPath) {
214 if path.Valid() {
215 a.AddPath(name, path.Path())
216 }
217}
218
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800219// SetPaths sets a Make variable with the given name to a slice of the given path strings.
Colin Cross08dca382020-07-21 20:31:17 -0700220func (a *AndroidMkEntries) SetPaths(name string, paths Paths) {
221 if _, ok := a.EntryMap[name]; !ok {
222 a.entryOrder = append(a.entryOrder, name)
223 }
224 a.EntryMap[name] = paths.Strings()
225}
226
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800227// SetOptionalPaths sets a Make variable with the given name to a slice of the given path strings
228// only if there are a non-zero amount of paths.
Colin Cross08dca382020-07-21 20:31:17 -0700229func (a *AndroidMkEntries) SetOptionalPaths(name string, paths Paths) {
230 if len(paths) > 0 {
231 a.SetPaths(name, paths)
232 }
233}
234
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800235// AddPaths appends the given path strings to a Make variable with the given name.
Colin Cross08dca382020-07-21 20:31:17 -0700236func (a *AndroidMkEntries) AddPaths(name string, paths Paths) {
237 if _, ok := a.EntryMap[name]; !ok {
238 a.entryOrder = append(a.entryOrder, name)
239 }
240 a.EntryMap[name] = append(a.EntryMap[name], paths.Strings()...)
241}
242
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800243// SetBoolIfTrue sets a Make variable with the given name to true if the given flag is true.
244// It is a no-op if the given flag is false.
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700245func (a *AndroidMkEntries) SetBoolIfTrue(name string, flag bool) {
246 if flag {
247 if _, ok := a.EntryMap[name]; !ok {
248 a.entryOrder = append(a.entryOrder, name)
249 }
250 a.EntryMap[name] = []string{"true"}
251 }
252}
253
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800254// SetBool sets a Make variable with the given name to if the given bool flag value.
Jaewoong Jung9a1e8bd2019-09-04 20:17:54 -0700255func (a *AndroidMkEntries) SetBool(name string, flag bool) {
256 if _, ok := a.EntryMap[name]; !ok {
257 a.entryOrder = append(a.entryOrder, name)
258 }
259 if flag {
260 a.EntryMap[name] = []string{"true"}
261 } else {
262 a.EntryMap[name] = []string{"false"}
263 }
264}
265
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800266// AddStrings appends the given strings to a Make variable with the given name.
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700267func (a *AndroidMkEntries) AddStrings(name string, value ...string) {
268 if len(value) == 0 {
269 return
270 }
271 if _, ok := a.EntryMap[name]; !ok {
272 a.entryOrder = append(a.entryOrder, name)
273 }
274 a.EntryMap[name] = append(a.EntryMap[name], value...)
275}
276
Liz Kammer57f5b332020-11-24 12:42:58 -0800277// AddCompatibilityTestSuites adds the supplied test suites to the EntryMap, with special handling
278// for partial MTS test suites.
279func (a *AndroidMkEntries) AddCompatibilityTestSuites(suites ...string) {
280 // MTS supports a full test suite and partial per-module MTS test suites, with naming mts-${MODULE}.
281 // To reduce repetition, if we find a partial MTS test suite without an full MTS test suite,
282 // we add the full test suite to our list.
283 if PrefixInList(suites, "mts-") && !InList("mts", suites) {
284 suites = append(suites, "mts")
285 }
286 a.AddStrings("LOCAL_COMPATIBILITY_SUITE", suites...)
287}
288
Paul Duffin8b0349c2020-11-26 14:33:21 +0000289// The contributions to the dist.
290type distContributions struct {
Bob Badour51804382022-04-13 11:27:19 -0700291 // Path to license metadata file.
292 licenseMetadataFile Path
Paul Duffin8b0349c2020-11-26 14:33:21 +0000293 // List of goals and the dist copy instructions.
294 copiesForGoals []*copiesForGoals
295}
296
297// getCopiesForGoals returns a copiesForGoals into which copy instructions that
298// must be processed when building one or more of those goals can be added.
299func (d *distContributions) getCopiesForGoals(goals string) *copiesForGoals {
300 copiesForGoals := &copiesForGoals{goals: goals}
301 d.copiesForGoals = append(d.copiesForGoals, copiesForGoals)
302 return copiesForGoals
303}
304
305// Associates a list of dist copy instructions with a set of goals for which they
306// should be run.
307type copiesForGoals struct {
308 // goals are a space separated list of build targets that will trigger the
309 // copy instructions.
310 goals string
311
312 // A list of instructions to copy a module's output files to somewhere in the
313 // dist directory.
314 copies []distCopy
315}
316
317// Adds a copy instruction.
318func (d *copiesForGoals) addCopyInstruction(from Path, dest string) {
319 d.copies = append(d.copies, distCopy{from, dest})
320}
321
322// Instruction on a path that must be copied into the dist.
323type distCopy struct {
324 // The path to copy from.
325 from Path
326
327 // The destination within the dist directory to copy to.
328 dest string
329}
330
331// Compute the contributions that the module makes to the dist.
332func (a *AndroidMkEntries) getDistContributions(mod blueprint.Module) *distContributions {
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000333 amod := mod.(Module).base()
334 name := amod.BaseModuleName()
335
Paul Duffin74f05592020-11-25 16:37:46 +0000336 // Collate the set of associated tag/paths available for copying to the dist.
337 // Start with an empty (nil) set.
Jingwen Chen7b27ca72020-07-24 09:13:49 +0000338 var availableTaggedDists TaggedDistFiles
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000339
Paul Duffin74f05592020-11-25 16:37:46 +0000340 // Then merge in any that are provided explicitly by the module.
Jingwen Chen84811862020-07-21 11:32:19 +0000341 if a.DistFiles != nil {
Paul Duffin74f05592020-11-25 16:37:46 +0000342 // Merge the DistFiles into the set.
343 availableTaggedDists = availableTaggedDists.merge(a.DistFiles)
344 }
345
346 // If no paths have been provided for the DefaultDistTag and the output file is
347 // valid then add that as the default dist path.
348 if _, ok := availableTaggedDists[DefaultDistTag]; !ok && a.OutputFile.Valid() {
349 availableTaggedDists = availableTaggedDists.addPathsForTag(DefaultDistTag, a.OutputFile.Path())
350 }
351
Paul Duffinaf970a22020-11-23 23:32:56 +0000352 // If the distFiles created by GenerateTaggedDistFiles contains paths for the
353 // DefaultDistTag then that takes priority so delete any existing paths.
354 if _, ok := amod.distFiles[DefaultDistTag]; ok {
355 delete(availableTaggedDists, DefaultDistTag)
356 }
357
358 // Finally, merge the distFiles created by GenerateTaggedDistFiles.
359 availableTaggedDists = availableTaggedDists.merge(amod.distFiles)
360
Paul Duffin74f05592020-11-25 16:37:46 +0000361 if len(availableTaggedDists) == 0 {
Jingwen Chen7b27ca72020-07-24 09:13:49 +0000362 // Nothing dist-able for this module.
363 return nil
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000364 }
365
Paul Duffin8b0349c2020-11-26 14:33:21 +0000366 // Collate the contributions this module makes to the dist.
367 distContributions := &distContributions{}
368
Bob Badour4660a982022-09-12 16:06:03 -0700369 if !exemptFromRequiredApplicableLicensesProperty(mod.(Module)) {
370 distContributions.licenseMetadataFile = amod.licenseMetadataFile
371 }
Bob Badour51804382022-04-13 11:27:19 -0700372
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000373 // Iterate over this module's dist structs, merged from the dist and dists properties.
374 for _, dist := range amod.Dists() {
375 // Get the list of goals this dist should be enabled for. e.g. sdk, droidcore
376 goals := strings.Join(dist.Targets, " ")
377
378 // Get the tag representing the output files to be dist'd. e.g. ".jar", ".proguard_map"
379 var tag string
380 if dist.Tag == nil {
381 // If the dist struct does not specify a tag, use the default output files tag.
Paul Duffin74f05592020-11-25 16:37:46 +0000382 tag = DefaultDistTag
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000383 } else {
384 tag = *dist.Tag
385 }
386
387 // Get the paths of the output files to be dist'd, represented by the tag.
388 // Can be an empty list.
389 tagPaths := availableTaggedDists[tag]
390 if len(tagPaths) == 0 {
391 // Nothing to dist for this tag, continue to the next dist.
392 continue
393 }
394
395 if len(tagPaths) > 1 && (dist.Dest != nil || dist.Suffix != nil) {
Paul Duffin74f05592020-11-25 16:37:46 +0000396 errorMessage := "%s: Cannot apply dest/suffix for more than one dist " +
397 "file for %q goals tag %q in module %s. The list of dist files, " +
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000398 "which should have a single element, is:\n%s"
Paul Duffin74f05592020-11-25 16:37:46 +0000399 panic(fmt.Errorf(errorMessage, mod, goals, tag, name, tagPaths))
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000400 }
401
Paul Duffin8b0349c2020-11-26 14:33:21 +0000402 copiesForGoals := distContributions.getCopiesForGoals(goals)
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000403
Paul Duffin8b0349c2020-11-26 14:33:21 +0000404 // Iterate over each path adding a copy instruction to copiesForGoals
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000405 for _, path := range tagPaths {
406 // It's possible that the Path is nil from errant modules. Be defensive here.
407 if path == nil {
408 tagName := "default" // for error message readability
409 if dist.Tag != nil {
410 tagName = *dist.Tag
411 }
412 panic(fmt.Errorf("Dist file should not be nil for the %s tag in %s", tagName, name))
413 }
414
415 dest := filepath.Base(path.String())
416
417 if dist.Dest != nil {
418 var err error
419 if dest, err = validateSafePath(*dist.Dest); err != nil {
420 // This was checked in ModuleBase.GenerateBuildActions
421 panic(err)
422 }
423 }
424
Trevor Radcliffe90727f42022-03-21 19:34:02 +0000425 ext := filepath.Ext(dest)
426 suffix := ""
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000427 if dist.Suffix != nil {
Trevor Radcliffe90727f42022-03-21 19:34:02 +0000428 suffix = *dist.Suffix
429 }
430
431 productString := ""
432 if dist.Append_artifact_with_product != nil && *dist.Append_artifact_with_product {
433 productString = fmt.Sprintf("_%s", a.entryContext.Config().DeviceProduct())
434 }
435
436 if suffix != "" || productString != "" {
437 dest = strings.TrimSuffix(dest, ext) + suffix + productString + ext
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000438 }
439
440 if dist.Dir != nil {
441 var err error
442 if dest, err = validateSafePath(*dist.Dir, dest); err != nil {
443 // This was checked in ModuleBase.GenerateBuildActions
444 panic(err)
445 }
446 }
447
Paul Duffin8b0349c2020-11-26 14:33:21 +0000448 copiesForGoals.addCopyInstruction(path, dest)
449 }
450 }
451
452 return distContributions
453}
454
455// generateDistContributionsForMake generates make rules that will generate the
456// dist according to the instructions in the supplied distContribution.
457func generateDistContributionsForMake(distContributions *distContributions) []string {
458 var ret []string
459 for _, d := range distContributions.copiesForGoals {
460 ret = append(ret, fmt.Sprintf(".PHONY: %s\n", d.goals))
461 // Create dist-for-goals calls for each of the copy instructions.
462 for _, c := range d.copies {
Bob Badour4660a982022-09-12 16:06:03 -0700463 if distContributions.licenseMetadataFile != nil {
464 ret = append(
465 ret,
466 fmt.Sprintf("$(if $(strip $(ALL_TARGETS.%s.META_LIC)),,$(eval ALL_TARGETS.%s.META_LIC := %s))\n",
467 c.from.String(), c.from.String(), distContributions.licenseMetadataFile.String()))
468 }
Bob Badour51804382022-04-13 11:27:19 -0700469 ret = append(
470 ret,
Paul Duffin8b0349c2020-11-26 14:33:21 +0000471 fmt.Sprintf("$(call dist-for-goals,%s,%s:%s)\n", d.goals, c.from.String(), c.dest))
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000472 }
473 }
474
475 return ret
476}
477
Paul Duffin8b0349c2020-11-26 14:33:21 +0000478// Compute the list of Make strings to declare phony goals and dist-for-goals
479// calls from the module's dist and dists properties.
480func (a *AndroidMkEntries) GetDistForGoals(mod blueprint.Module) []string {
481 distContributions := a.getDistContributions(mod)
482 if distContributions == nil {
483 return nil
484 }
485
486 return generateDistContributionsForMake(distContributions)
487}
488
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800489// fillInEntries goes through the common variable processing and calls the extra data funcs to
490// generate and fill in AndroidMkEntries's in-struct data, ready to be flushed to a file.
Colin Crossaa255532020-07-03 13:18:24 -0700491type fillInEntriesContext interface {
492 ModuleDir(module blueprint.Module) string
Cole Faust39aabe92023-02-23 16:57:43 -0800493 ModuleSubDir(module blueprint.Module) string
Colin Crossaa255532020-07-03 13:18:24 -0700494 Config() Config
Colin Cross3c0a83d2023-12-12 14:13:26 -0800495 moduleProvider(module blueprint.Module, provider blueprint.AnyProviderKey) (any, bool)
Sasha Smundak5c4729d2022-12-01 10:49:23 -0800496 ModuleType(module blueprint.Module) string
Colin Crossaa255532020-07-03 13:18:24 -0700497}
498
499func (a *AndroidMkEntries) fillInEntries(ctx fillInEntriesContext, mod blueprint.Module) {
Trevor Radcliffe90727f42022-03-21 19:34:02 +0000500 a.entryContext = ctx
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700501 a.EntryMap = make(map[string][]string)
Colin Crossf1f763a2021-10-21 16:14:19 -0700502 amod := mod.(Module)
503 base := amod.base()
504 name := base.BaseModuleName()
Colin Cross0477b422020-10-13 18:43:54 -0700505 if a.OverrideName != "" {
506 name = a.OverrideName
507 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700508
509 if a.Include == "" {
510 a.Include = "$(BUILD_PREBUILT)"
511 }
Colin Crossf1f763a2021-10-21 16:14:19 -0700512 a.Required = append(a.Required, amod.RequiredModuleNames()...)
513 a.Host_required = append(a.Host_required, amod.HostRequiredModuleNames()...)
514 a.Target_required = append(a.Target_required, amod.TargetRequiredModuleNames()...)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700515
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000516 for _, distString := range a.GetDistForGoals(mod) {
517 fmt.Fprintf(&a.header, distString)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700518 }
519
Cole Faust39aabe92023-02-23 16:57:43 -0800520 fmt.Fprintf(&a.header, "\ninclude $(CLEAR_VARS) # type: %s, name: %s, variant: %s\n", ctx.ModuleType(mod), base.BaseModuleName(), ctx.ModuleSubDir(mod))
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700521
522 // Collect make variable assignment entries.
Colin Crossaa255532020-07-03 13:18:24 -0700523 a.SetString("LOCAL_PATH", ctx.ModuleDir(mod))
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700524 a.SetString("LOCAL_MODULE", name+a.SubName)
525 a.SetString("LOCAL_MODULE_CLASS", a.Class)
526 a.SetString("LOCAL_PREBUILT_MODULE_FILE", a.OutputFile.String())
527 a.AddStrings("LOCAL_REQUIRED_MODULES", a.Required...)
528 a.AddStrings("LOCAL_HOST_REQUIRED_MODULES", a.Host_required...)
529 a.AddStrings("LOCAL_TARGET_REQUIRED_MODULES", a.Target_required...)
Wei Li598f92d2023-01-04 17:12:24 -0800530 a.AddStrings("LOCAL_SOONG_MODULE_TYPE", ctx.ModuleType(amod))
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700531
Colin Cross6301c3c2021-09-28 17:40:21 -0700532 // If the install rule was generated by Soong tell Make about it.
Colin Crossc68db4b2021-11-11 18:59:15 -0800533 if len(base.katiInstalls) > 0 {
Colin Cross6301c3c2021-09-28 17:40:21 -0700534 // Assume the primary install file is last since it probably needs to depend on any other
535 // installed files. If that is not the case we can add a method to specify the primary
536 // installed file.
537 a.SetPath("LOCAL_SOONG_INSTALLED_MODULE", base.katiInstalls[len(base.katiInstalls)-1].to)
538 a.SetString("LOCAL_SOONG_INSTALL_PAIRS", base.katiInstalls.BuiltInstalled())
539 a.SetPaths("LOCAL_SOONG_INSTALL_SYMLINKS", base.katiSymlinks.InstallPaths().Paths())
540 }
541
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800542 if len(base.testData) > 0 {
543 a.AddStrings("LOCAL_TEST_DATA", androidMkDataPaths(base.testData)...)
544 }
545
Jiyong Park89e850a2020-04-07 16:37:39 +0900546 if am, ok := mod.(ApexModule); ok {
547 a.SetBoolIfTrue("LOCAL_NOT_AVAILABLE_FOR_PLATFORM", am.NotAvailableForPlatform())
548 }
549
Colin Crossf1f763a2021-10-21 16:14:19 -0700550 archStr := base.Arch().ArchType.String()
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700551 host := false
Colin Crossf1f763a2021-10-21 16:14:19 -0700552 switch base.Os().Class {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700553 case Host:
Colin Crossf1f763a2021-10-21 16:14:19 -0700554 if base.Target().HostCross {
Jiyong Park1613e552020-09-14 19:43:17 +0900555 // Make cannot identify LOCAL_MODULE_HOST_CROSS_ARCH:= common.
Colin Crossf1f763a2021-10-21 16:14:19 -0700556 if base.Arch().ArchType != Common {
Jiyong Park1613e552020-09-14 19:43:17 +0900557 a.SetString("LOCAL_MODULE_HOST_CROSS_ARCH", archStr)
558 }
559 } else {
560 // Make cannot identify LOCAL_MODULE_HOST_ARCH:= common.
Colin Crossf1f763a2021-10-21 16:14:19 -0700561 if base.Arch().ArchType != Common {
Jiyong Park1613e552020-09-14 19:43:17 +0900562 a.SetString("LOCAL_MODULE_HOST_ARCH", archStr)
563 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700564 }
565 host = true
566 case Device:
567 // Make cannot identify LOCAL_MODULE_TARGET_ARCH:= common.
Colin Crossf1f763a2021-10-21 16:14:19 -0700568 if base.Arch().ArchType != Common {
569 if base.Target().NativeBridge {
570 hostArchStr := base.Target().NativeBridgeHostArchName
dimitry1f33e402019-03-26 12:39:31 +0100571 if hostArchStr != "" {
572 a.SetString("LOCAL_MODULE_TARGET_ARCH", hostArchStr)
573 }
574 } else {
575 a.SetString("LOCAL_MODULE_TARGET_ARCH", archStr)
576 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700577 }
578
Kelvin Zhang46977252022-04-13 16:41:34 -0700579 if !base.InVendorRamdisk() {
Colin Crossf1f763a2021-10-21 16:14:19 -0700580 a.AddPaths("LOCAL_FULL_INIT_RC", base.initRcPaths)
Yifan Hong919dae12020-12-02 18:55:06 -0800581 }
Colin Crossf1f763a2021-10-21 16:14:19 -0700582 if len(base.vintfFragmentsPaths) > 0 {
583 a.AddPaths("LOCAL_FULL_VINTF_FRAGMENTS", base.vintfFragmentsPaths)
Liz Kammer7b3dc8a2021-04-16 16:41:59 -0400584 }
Colin Crossf1f763a2021-10-21 16:14:19 -0700585 a.SetBoolIfTrue("LOCAL_PROPRIETARY_MODULE", Bool(base.commonProperties.Proprietary))
586 if Bool(base.commonProperties.Vendor) || Bool(base.commonProperties.Soc_specific) {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700587 a.SetString("LOCAL_VENDOR_MODULE", "true")
588 }
Colin Crossf1f763a2021-10-21 16:14:19 -0700589 a.SetBoolIfTrue("LOCAL_ODM_MODULE", Bool(base.commonProperties.Device_specific))
590 a.SetBoolIfTrue("LOCAL_PRODUCT_MODULE", Bool(base.commonProperties.Product_specific))
591 a.SetBoolIfTrue("LOCAL_SYSTEM_EXT_MODULE", Bool(base.commonProperties.System_ext_specific))
592 if base.commonProperties.Owner != nil {
593 a.SetString("LOCAL_MODULE_OWNER", *base.commonProperties.Owner)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700594 }
595 }
596
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700597 if host {
Colin Crossf1f763a2021-10-21 16:14:19 -0700598 makeOs := base.Os().String()
599 if base.Os() == Linux || base.Os() == LinuxBionic || base.Os() == LinuxMusl {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700600 makeOs = "linux"
601 }
602 a.SetString("LOCAL_MODULE_HOST_OS", makeOs)
603 a.SetString("LOCAL_IS_HOST_MODULE", "true")
604 }
605
606 prefix := ""
Colin Crossf1f763a2021-10-21 16:14:19 -0700607 if base.ArchSpecific() {
608 switch base.Os().Class {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700609 case Host:
Colin Crossf1f763a2021-10-21 16:14:19 -0700610 if base.Target().HostCross {
Jiyong Park1613e552020-09-14 19:43:17 +0900611 prefix = "HOST_CROSS_"
612 } else {
613 prefix = "HOST_"
614 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700615 case Device:
616 prefix = "TARGET_"
617
618 }
619
Colin Crossf1f763a2021-10-21 16:14:19 -0700620 if base.Arch().ArchType != ctx.Config().Targets[base.Os()][0].Arch.ArchType {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700621 prefix = "2ND_" + prefix
622 }
623 }
Colin Crossaa255532020-07-03 13:18:24 -0700624
Colin Cross313aa542023-12-13 13:47:44 -0800625 if licenseMetadata, ok := SingletonModuleProvider(ctx, mod, LicenseMetadataProvider); ok {
Colin Cross4acaea92021-12-10 23:05:02 +0000626 a.SetPath("LOCAL_SOONG_LICENSE_METADATA", licenseMetadata.LicenseMetadataPath)
627 }
628
Colin Crossaa255532020-07-03 13:18:24 -0700629 extraCtx := &androidMkExtraEntriesContext{
630 ctx: ctx,
631 mod: mod,
632 }
633
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700634 for _, extra := range a.ExtraEntries {
Colin Crossaa255532020-07-03 13:18:24 -0700635 extra(extraCtx, a)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700636 }
637
638 // Write to footer.
639 fmt.Fprintln(&a.footer, "include "+a.Include)
Colin Crossaa255532020-07-03 13:18:24 -0700640 blueprintDir := ctx.ModuleDir(mod)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700641 for _, footerFunc := range a.ExtraFooters {
Jaewoong Jung02b11a62020-12-07 10:23:54 -0800642 footerFunc(&a.footer, name, prefix, blueprintDir)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700643 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700644}
645
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800646// write flushes the AndroidMkEntries's in-struct data populated by AndroidMkEntries into the
647// given Writer object.
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700648func (a *AndroidMkEntries) write(w io.Writer) {
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700649 if a.Disabled {
650 return
651 }
652
653 if !a.OutputFile.Valid() {
654 return
655 }
656
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700657 w.Write(a.header.Bytes())
658 for _, name := range a.entryOrder {
Sasha Smundakdcb61292022-12-08 10:41:33 -0800659 AndroidMkEmitAssignList(w, name, a.EntryMap[name])
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700660 }
661 w.Write(a.footer.Bytes())
662}
663
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700664func (a *AndroidMkEntries) FooterLinesForTests() []string {
665 return strings.Split(string(a.footer.Bytes()), "\n")
666}
667
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800668// AndroidMkSingleton is a singleton to collect Android.mk data from all modules and dump them into
669// the final Android-<product_name>.mk file output.
Colin Cross0875c522017-11-28 17:34:01 -0800670func AndroidMkSingleton() Singleton {
Dan Willemsen218f6562015-07-08 18:13:11 -0700671 return &androidMkSingleton{}
672}
673
674type androidMkSingleton struct{}
675
Colin Cross0875c522017-11-28 17:34:01 -0800676func (c *androidMkSingleton) GenerateBuildActions(ctx SingletonContext) {
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800677 // Skip if Soong wasn't invoked from Make.
Jingwen Chencda22c92020-11-23 00:22:30 -0500678 if !ctx.Config().KatiEnabled() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800679 return
680 }
681
Colin Cross2465c3d2018-09-28 10:19:18 -0700682 var androidMkModulesList []blueprint.Module
Colin Cross4f6e4e62016-01-11 12:55:55 -0800683
Colin Cross2465c3d2018-09-28 10:19:18 -0700684 ctx.VisitAllModulesBlueprint(func(module blueprint.Module) {
Colin Cross0875c522017-11-28 17:34:01 -0800685 androidMkModulesList = append(androidMkModulesList, module)
Colin Cross4f6e4e62016-01-11 12:55:55 -0800686 })
Dan Willemsen218f6562015-07-08 18:13:11 -0700687
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800688 // Sort the module list by the module names to eliminate random churns, which may erroneously
689 // invoke additional build processes.
Colin Cross1ad81422019-01-14 12:47:35 -0800690 sort.SliceStable(androidMkModulesList, func(i, j int) bool {
691 return ctx.ModuleName(androidMkModulesList[i]) < ctx.ModuleName(androidMkModulesList[j])
692 })
Colin Crossd779da42015-12-17 18:00:23 -0800693
Dan Willemsen45133ac2018-03-09 21:22:06 -0800694 transMk := PathForOutput(ctx, "Android"+String(ctx.Config().productVariables.Make_suffix)+".mk")
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700695 if ctx.Failed() {
696 return
697 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700698
Colin Cross988414c2020-01-11 01:11:46 +0000699 err := translateAndroidMk(ctx, absolutePath(transMk.String()), androidMkModulesList)
Dan Willemsen218f6562015-07-08 18:13:11 -0700700 if err != nil {
701 ctx.Errorf(err.Error())
702 }
703
Colin Cross0875c522017-11-28 17:34:01 -0800704 ctx.Build(pctx, BuildParams{
705 Rule: blueprint.Phony,
706 Output: transMk,
Dan Willemsen218f6562015-07-08 18:13:11 -0700707 })
708}
709
Colin Cross836e3872021-11-09 12:30:59 -0800710func translateAndroidMk(ctx SingletonContext, absMkFile string, mods []blueprint.Module) error {
Dan Willemsen218f6562015-07-08 18:13:11 -0700711 buf := &bytes.Buffer{}
712
Dan Willemsen97750522016-02-09 17:43:51 -0800713 fmt.Fprintln(buf, "LOCAL_MODULE_MAKEFILE := $(lastword $(MAKEFILE_LIST))")
Dan Willemsen218f6562015-07-08 18:13:11 -0700714
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800715 typeStats := make(map[string]int)
Dan Willemsen218f6562015-07-08 18:13:11 -0700716 for _, mod := range mods {
717 err := translateAndroidMkModule(ctx, buf, mod)
718 if err != nil {
Colin Cross836e3872021-11-09 12:30:59 -0800719 os.Remove(absMkFile)
Dan Willemsen218f6562015-07-08 18:13:11 -0700720 return err
721 }
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700722
Colin Cross2465c3d2018-09-28 10:19:18 -0700723 if amod, ok := mod.(Module); ok && ctx.PrimaryModule(amod) == amod {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800724 typeStats[ctx.ModuleType(amod)] += 1
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700725 }
726 }
727
728 keys := []string{}
729 fmt.Fprintln(buf, "\nSTATS.SOONG_MODULE_TYPE :=")
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800730 for k := range typeStats {
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700731 keys = append(keys, k)
732 }
733 sort.Strings(keys)
734 for _, mod_type := range keys {
735 fmt.Fprintln(buf, "STATS.SOONG_MODULE_TYPE +=", mod_type)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800736 fmt.Fprintf(buf, "STATS.SOONG_MODULE_TYPE.%s := %d\n", mod_type, typeStats[mod_type])
Dan Willemsen218f6562015-07-08 18:13:11 -0700737 }
738
Colin Cross836e3872021-11-09 12:30:59 -0800739 return pathtools.WriteFileIfChanged(absMkFile, buf.Bytes(), 0666)
Dan Willemsen218f6562015-07-08 18:13:11 -0700740}
741
Colin Cross0875c522017-11-28 17:34:01 -0800742func translateAndroidMkModule(ctx SingletonContext, w io.Writer, mod blueprint.Module) error {
Colin Cross953d3a22018-09-05 16:23:54 -0700743 defer func() {
744 if r := recover(); r != nil {
745 panic(fmt.Errorf("%s in translateAndroidMkModule for module %s variant %s",
746 r, ctx.ModuleName(mod), ctx.ModuleSubDir(mod)))
747 }
748 }()
749
Bob Badourb4999222021-01-07 03:34:31 +0000750 // Additional cases here require review for correct license propagation to make.
Colin Cross2465c3d2018-09-28 10:19:18 -0700751 switch x := mod.(type) {
752 case AndroidMkDataProvider:
753 return translateAndroidModule(ctx, w, mod, x)
754 case bootstrap.GoBinaryTool:
755 return translateGoBinaryModule(ctx, w, mod, x)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700756 case AndroidMkEntriesProvider:
757 return translateAndroidMkEntriesModule(ctx, w, mod, x)
Colin Cross2465c3d2018-09-28 10:19:18 -0700758 default:
Bob Badourb4999222021-01-07 03:34:31 +0000759 // Not exported to make so no make variables to set.
Dan Willemsen218f6562015-07-08 18:13:11 -0700760 return nil
761 }
Colin Cross2465c3d2018-09-28 10:19:18 -0700762}
763
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800764// A simple, special Android.mk entry output func to make it possible to build blueprint tools using
765// m by making them phony targets.
Colin Cross2465c3d2018-09-28 10:19:18 -0700766func translateGoBinaryModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
767 goBinary bootstrap.GoBinaryTool) error {
768
769 name := ctx.ModuleName(mod)
770 fmt.Fprintln(w, ".PHONY:", name)
771 fmt.Fprintln(w, name+":", goBinary.InstallPath())
772 fmt.Fprintln(w, "")
Bob Badourb4999222021-01-07 03:34:31 +0000773 // Assuming no rules in make include go binaries in distributables.
774 // If the assumption is wrong, make will fail to build without the necessary .meta_lic and .meta_module files.
775 // In that case, add the targets and rules here to build a .meta_lic file for `name` and a .meta_module for
776 // `goBinary.InstallPath()` pointing to the `name`.meta_lic file.
Colin Cross2465c3d2018-09-28 10:19:18 -0700777
778 return nil
779}
780
Colin Crossaa255532020-07-03 13:18:24 -0700781func (data *AndroidMkData) fillInData(ctx fillInEntriesContext, mod blueprint.Module) {
Jooyung Han12df5fb2019-07-11 16:18:47 +0900782 // Get the preamble content through AndroidMkEntries logic.
Jooyung Han2ed99d02020-06-24 23:26:26 +0900783 data.Entries = AndroidMkEntries{
Jooyung Han12df5fb2019-07-11 16:18:47 +0900784 Class: data.Class,
785 SubName: data.SubName,
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000786 DistFiles: data.DistFiles,
Jooyung Han12df5fb2019-07-11 16:18:47 +0900787 OutputFile: data.OutputFile,
788 Disabled: data.Disabled,
789 Include: data.Include,
790 Required: data.Required,
791 Host_required: data.Host_required,
792 Target_required: data.Target_required,
793 }
Colin Crossaa255532020-07-03 13:18:24 -0700794 data.Entries.fillInEntries(ctx, mod)
Jooyung Han12df5fb2019-07-11 16:18:47 +0900795
796 // copy entries back to data since it is used in Custom
Jooyung Han2ed99d02020-06-24 23:26:26 +0900797 data.Required = data.Entries.Required
798 data.Host_required = data.Entries.Host_required
799 data.Target_required = data.Entries.Target_required
Jooyung Han12df5fb2019-07-11 16:18:47 +0900800}
801
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800802// A support func for the deprecated AndroidMkDataProvider interface. Use AndroidMkEntryProvider
803// instead.
Colin Cross2465c3d2018-09-28 10:19:18 -0700804func translateAndroidModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
805 provider AndroidMkDataProvider) error {
Dan Willemsen218f6562015-07-08 18:13:11 -0700806
Colin Cross635c3b02016-05-18 15:37:25 -0700807 amod := mod.(Module).base()
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700808 if shouldSkipAndroidMkProcessing(amod) {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800809 return nil
810 }
811
Colin Cross91825d22017-08-10 16:59:47 -0700812 data := provider.AndroidMk()
Colin Cross53499412017-09-07 13:20:25 -0700813 if data.Include == "" {
814 data.Include = "$(BUILD_PREBUILT)"
815 }
816
Colin Crossaa255532020-07-03 13:18:24 -0700817 data.fillInData(ctx, mod)
Dan Willemsen01a405a2016-06-13 17:19:03 -0700818
Colin Cross0f86d182017-08-10 17:07:28 -0700819 prefix := ""
820 if amod.ArchSpecific() {
821 switch amod.Os().Class {
822 case Host:
Jiyong Park1613e552020-09-14 19:43:17 +0900823 if amod.Target().HostCross {
824 prefix = "HOST_CROSS_"
825 } else {
826 prefix = "HOST_"
827 }
Colin Cross0f86d182017-08-10 17:07:28 -0700828 case Device:
829 prefix = "TARGET_"
Colin Crossa2344662016-03-24 13:14:12 -0700830
Dan Willemsen218f6562015-07-08 18:13:11 -0700831 }
832
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700833 if amod.Arch().ArchType != ctx.Config().Targets[amod.Os()][0].Arch.ArchType {
Colin Cross0f86d182017-08-10 17:07:28 -0700834 prefix = "2ND_" + prefix
835 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700836 }
837
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700838 name := provider.BaseModuleName()
Colin Cross0f86d182017-08-10 17:07:28 -0700839 blueprintDir := filepath.Dir(ctx.BlueprintFile(mod))
840
841 if data.Custom != nil {
Bob Badourb4999222021-01-07 03:34:31 +0000842 // List of module types allowed to use .Custom(...)
843 // Additions to the list require careful review for proper license handling.
Colin Crossaa255532020-07-03 13:18:24 -0700844 switch reflect.TypeOf(mod).String() { // ctx.ModuleType(mod) doesn't work: aidl_interface creates phony without type
Bob Badourb4999222021-01-07 03:34:31 +0000845 case "*aidl.aidlApi": // writes non-custom before adding .phony
846 case "*aidl.aidlMapping": // writes non-custom before adding .phony
847 case "*android.customModule": // appears in tests only
Dan Willemsen9fe14102021-07-13 21:52:04 -0700848 case "*android_sdk.sdkRepoHost": // doesn't go through base_rules
Bob Badourb4999222021-01-07 03:34:31 +0000849 case "*apex.apexBundle": // license properties written
850 case "*bpf.bpf": // license properties written (both for module and objs)
851 case "*genrule.Module": // writes non-custom before adding .phony
852 case "*java.SystemModules": // doesn't go through base_rules
853 case "*java.systemModulesImport": // doesn't go through base_rules
854 case "*phony.phony": // license properties written
855 case "*selinux.selinuxContextsModule": // license properties written
856 case "*sysprop.syspropLibrary": // license properties written
857 default:
Bob Badour65ee90a2021-09-02 15:33:10 -0700858 if !ctx.Config().IsEnvFalse("ANDROID_REQUIRE_LICENSES") {
Bob Badourb4999222021-01-07 03:34:31 +0000859 return fmt.Errorf("custom make rules not allowed for %q (%q) module %q", ctx.ModuleType(mod), reflect.TypeOf(mod), ctx.ModuleName(mod))
860 }
861 }
Colin Cross0f86d182017-08-10 17:07:28 -0700862 data.Custom(w, name, prefix, blueprintDir, data)
863 } else {
864 WriteAndroidMkData(w, data)
865 }
866
867 return nil
868}
869
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800870// A support func for the deprecated AndroidMkDataProvider interface. Use AndroidMkEntryProvider
871// instead.
Colin Cross0f86d182017-08-10 17:07:28 -0700872func WriteAndroidMkData(w io.Writer, data AndroidMkData) {
873 if data.Disabled {
874 return
875 }
876
877 if !data.OutputFile.Valid() {
878 return
879 }
880
Jooyung Han2ed99d02020-06-24 23:26:26 +0900881 // write preamble via Entries
882 data.Entries.footer = bytes.Buffer{}
883 data.Entries.write(w)
Colin Cross0f86d182017-08-10 17:07:28 -0700884
Colin Crossca860ac2016-01-04 14:34:37 -0800885 for _, extra := range data.Extra {
Colin Cross27a4b052017-08-10 16:32:23 -0700886 extra(w, data.OutputFile.Path())
Dan Willemsen97750522016-02-09 17:43:51 -0800887 }
888
Colin Cross53499412017-09-07 13:20:25 -0700889 fmt.Fprintln(w, "include "+data.Include)
Dan Willemsen218f6562015-07-08 18:13:11 -0700890}
Sasha Smundakb6d23052019-04-01 18:37:36 -0700891
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700892func translateAndroidMkEntriesModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
893 provider AndroidMkEntriesProvider) error {
894 if shouldSkipAndroidMkProcessing(mod.(Module).base()) {
895 return nil
Sasha Smundakb6d23052019-04-01 18:37:36 -0700896 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700897
Bob Badourb4999222021-01-07 03:34:31 +0000898 // Any new or special cases here need review to verify correct propagation of license information.
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900899 for _, entries := range provider.AndroidMkEntries() {
Colin Crossaa255532020-07-03 13:18:24 -0700900 entries.fillInEntries(ctx, mod)
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900901 entries.write(w)
902 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700903
904 return nil
905}
906
Chih-Hung Hsieh80783772021-10-11 16:46:56 -0700907func ShouldSkipAndroidMkProcessing(module Module) bool {
908 return shouldSkipAndroidMkProcessing(module.base())
909}
910
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700911func shouldSkipAndroidMkProcessing(module *ModuleBase) bool {
912 if !module.commonProperties.NamespaceExportedToMake {
913 // TODO(jeffrygaston) do we want to validate that there are no modules being
914 // exported to Kati that depend on this module?
915 return true
Sasha Smundakb6d23052019-04-01 18:37:36 -0700916 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700917
Dan Willemsendef7b5d2021-10-17 00:22:33 -0700918 // On Mac, only expose host darwin modules to Make, as that's all we claim to support.
919 // In reality, some of them depend on device-built (Java) modules, so we can't disable all
920 // device modules in Soong, but we can hide them from Make (and thus the build user interface)
921 if runtime.GOOS == "darwin" && module.Os() != Darwin {
922 return true
923 }
924
Dan Willemsen8528f4e2021-10-19 00:22:06 -0700925 // Only expose the primary Darwin target, as Make does not understand Darwin+Arm64
926 if module.Os() == Darwin && module.Target().HostCross {
927 return true
928 }
929
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700930 return !module.Enabled() ||
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800931 module.commonProperties.HideFromMake ||
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700932 // Make does not understand LinuxBionic
Colin Cross9a027be2022-06-24 18:45:58 -0700933 module.Os() == LinuxBionic ||
934 // Make does not understand LinuxMusl, except when we are building with USE_HOST_MUSL=true
935 // and all host binaries are LinuxMusl
936 (module.Os() == LinuxMusl && module.Target().HostCross)
Sasha Smundakb6d23052019-04-01 18:37:36 -0700937}
Dan Shi31949122020-09-21 12:11:02 -0700938
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800939// A utility func to format LOCAL_TEST_DATA outputs. See the comments on DataPath to understand how
940// to use this func.
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800941func androidMkDataPaths(data []DataPath) []string {
Dan Shi31949122020-09-21 12:11:02 -0700942 var testFiles []string
943 for _, d := range data {
944 rel := d.SrcPath.Rel()
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800945 if d.WithoutRel {
946 rel = d.SrcPath.Base()
947 }
Dan Shi31949122020-09-21 12:11:02 -0700948 path := d.SrcPath.String()
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800949 // LOCAL_TEST_DATA requires the rel portion of the path to be removed from the path.
Dan Shi31949122020-09-21 12:11:02 -0700950 if !strings.HasSuffix(path, rel) {
951 panic(fmt.Errorf("path %q does not end with %q", path, rel))
952 }
953 path = strings.TrimSuffix(path, rel)
954 testFileString := path + ":" + rel
955 if len(d.RelativeInstallPath) > 0 {
956 testFileString += ":" + d.RelativeInstallPath
957 }
958 testFiles = append(testFiles, testFileString)
959 }
960 return testFiles
961}
Sasha Smundakdcb61292022-12-08 10:41:33 -0800962
963// AndroidMkEmitAssignList emits the line
964//
965// VAR := ITEM ...
966//
967// Items are the elements to the given set of lists
968// If all the passed lists are empty, no line will be emitted
969func AndroidMkEmitAssignList(w io.Writer, varName string, lists ...[]string) {
970 doPrint := false
971 for _, l := range lists {
972 if doPrint = len(l) > 0; doPrint {
973 break
974 }
975 }
976 if !doPrint {
977 return
978 }
979 fmt.Fprint(w, varName, " :=")
980 for _, l := range lists {
981 for _, item := range l {
982 fmt.Fprint(w, " ", item)
983 }
984 }
985 fmt.Fprintln(w)
986}