blob: 6a1701df3e0e20af8f771ea2fa0083df4e39d40f [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"
Colin Crossd6fd0132023-11-06 13:54:06 -080033 "strconv"
Dan Willemsen0fda89f2016-06-01 15:25:32 -070034 "strings"
Dan Willemsen218f6562015-07-08 18:13:11 -070035
Dan Willemsen218f6562015-07-08 18:13:11 -070036 "github.com/google/blueprint"
Colin Cross836e3872021-11-09 12:30:59 -080037 "github.com/google/blueprint/pathtools"
Jiyong Park3f627e62024-05-01 16:14:38 +090038 "github.com/google/blueprint/proptools"
Dan Willemsen218f6562015-07-08 18:13:11 -070039)
40
41func init() {
Paul Duffin8c3fec42020-03-04 20:15:08 +000042 RegisterAndroidMkBuildComponents(InitRegistrationContext)
43}
44
45func RegisterAndroidMkBuildComponents(ctx RegistrationContext) {
LaMont Jones0c10e4d2023-05-16 00:58:37 +000046 ctx.RegisterParallelSingletonType("androidmk", AndroidMkSingleton)
Dan Willemsen218f6562015-07-08 18:13:11 -070047}
48
Paul Duffin6c9da042021-03-07 15:44:41 +000049// Enable androidmk support.
50// * Register the singleton
51// * Configure that we are inside make
52var PrepareForTestWithAndroidMk = GroupFixturePreparers(
53 FixtureRegisterWithContext(RegisterAndroidMkBuildComponents),
54 FixtureModifyConfig(SetKatiEnabledForTests),
55)
56
Jaewoong Jung7ef4a902020-11-16 12:50:29 -080057// Deprecated: Use AndroidMkEntriesProvider instead, especially if you're not going to use the
58// Custom function. It's easier to use and test.
Dan Willemsen218f6562015-07-08 18:13:11 -070059type AndroidMkDataProvider interface {
Colin Crossa18e9cf2017-08-10 17:00:19 -070060 AndroidMk() AndroidMkData
Colin Crossce75d2c2016-10-06 16:12:58 -070061 BaseModuleName() string
Dan Willemsen218f6562015-07-08 18:13:11 -070062}
63
64type AndroidMkData struct {
Sasha Smundakb6d23052019-04-01 18:37:36 -070065 Class string
66 SubName string
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
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800110 // The output file for Kati to process and/or install. If absent, the module is skipped.
111 OutputFile OptionalPath
112 // If true, the module is skipped and does not appear on the final Android-<product name>.mk
113 // file. Useful when a module needs to be skipped conditionally.
114 Disabled bool
Ivan Lozanod06cc742021-11-12 13:27:58 -0500115 // The postprocessing mk file to include, e.g. $(BUILD_SYSTEM)/soong_cc_rust_prebuilt.mk
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800116 // If not set, $(BUILD_SYSTEM)/prebuilt.mk is used.
117 Include string
118 // Required modules that need to be built and included in the final build output when building
119 // this module.
120 Required []string
121 // Required host modules that need to be built and included in the final build output when
122 // building this module.
123 Host_required []string
124 // Required device modules that need to be built and included in the final build output when
125 // building this module.
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700126 Target_required []string
127
128 header bytes.Buffer
129 footer bytes.Buffer
130
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800131 // Funcs to append additional Android.mk entries or modify the common ones. Multiple funcs are
132 // accepted so that common logic can be factored out as a shared func.
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700133 ExtraEntries []AndroidMkExtraEntriesFunc
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800134 // Funcs to add extra lines to the module's Android.mk output. Unlike AndroidMkExtraEntriesFunc,
135 // which simply sets Make variable values, this can be used for anything since it can write any
136 // Make statements directly to the final Android-*.mk file.
137 // Primarily used to call macros or declare/update Make targets.
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700138 ExtraFooters []AndroidMkExtraFootersFunc
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700139
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800140 // A map that holds the up-to-date Make variable values. Can be accessed from tests.
141 EntryMap map[string][]string
142 // A list of EntryMap keys in insertion order. This serves a few purposes:
143 // 1. Prevents churns. Golang map doesn't provide consistent iteration order, so without this,
144 // the outputted Android-*.mk file may change even though there have been no content changes.
145 // 2. Allows modules to refer to other variables, like LOCAL_BAR_VAR := $(LOCAL_FOO_VAR),
146 // without worrying about the variables being mixed up in the actual mk file.
147 // 3. Makes troubleshooting and spotting errors easier.
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700148 entryOrder []string
Trevor Radcliffe90727f42022-03-21 19:34:02 +0000149
150 // Provides data typically stored by Context objects that are commonly needed by
151 //AndroidMkEntries objects.
152 entryContext AndroidMkEntriesContext
153}
154
155type AndroidMkEntriesContext interface {
Yu Liuec810542024-08-26 18:09:15 +0000156 OtherModuleProviderContext
Trevor Radcliffe90727f42022-03-21 19:34:02 +0000157 Config() Config
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700158}
159
Colin Crossaa255532020-07-03 13:18:24 -0700160type AndroidMkExtraEntriesContext interface {
Colin Cross3c0a83d2023-12-12 14:13:26 -0800161 Provider(provider blueprint.AnyProviderKey) (any, bool)
Colin Crossaa255532020-07-03 13:18:24 -0700162}
163
164type androidMkExtraEntriesContext struct {
165 ctx fillInEntriesContext
Yu Liu14b81452025-02-18 23:26:13 +0000166 mod Module
Colin Crossaa255532020-07-03 13:18:24 -0700167}
168
Colin Cross3c0a83d2023-12-12 14:13:26 -0800169func (a *androidMkExtraEntriesContext) Provider(provider blueprint.AnyProviderKey) (any, bool) {
Yu Liu663e4502024-08-12 18:23:59 +0000170 return a.ctx.otherModuleProvider(a.mod, provider)
Colin Crossaa255532020-07-03 13:18:24 -0700171}
172
173type AndroidMkExtraEntriesFunc func(ctx AndroidMkExtraEntriesContext, entries *AndroidMkEntries)
Jaewoong Jung02b11a62020-12-07 10:23:54 -0800174type AndroidMkExtraFootersFunc func(w io.Writer, name, prefix, moduleDir string)
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700175
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800176// Utility funcs to manipulate Android.mk variable entries.
177
178// SetString sets a Make variable with the given name to the given value.
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700179func (a *AndroidMkEntries) SetString(name, value string) {
180 if _, ok := a.EntryMap[name]; !ok {
181 a.entryOrder = append(a.entryOrder, name)
182 }
183 a.EntryMap[name] = []string{value}
184}
185
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800186// SetPath sets a Make variable with the given name to the given path string.
Jaewoong Jung9a1e8bd2019-09-04 20:17:54 -0700187func (a *AndroidMkEntries) SetPath(name string, path Path) {
188 if _, ok := a.EntryMap[name]; !ok {
189 a.entryOrder = append(a.entryOrder, name)
190 }
191 a.EntryMap[name] = []string{path.String()}
192}
193
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800194// SetOptionalPath sets a Make variable with the given name to the given path string if it is valid.
195// It is a no-op if the given path is invalid.
Colin Crossc0efd1d2020-07-03 11:56:24 -0700196func (a *AndroidMkEntries) SetOptionalPath(name string, path OptionalPath) {
197 if path.Valid() {
198 a.SetPath(name, path.Path())
199 }
200}
201
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800202// AddPath appends the given path string to a Make variable with the given name.
Colin Crossc0efd1d2020-07-03 11:56:24 -0700203func (a *AndroidMkEntries) AddPath(name string, path Path) {
204 if _, ok := a.EntryMap[name]; !ok {
205 a.entryOrder = append(a.entryOrder, name)
206 }
207 a.EntryMap[name] = append(a.EntryMap[name], path.String())
208}
209
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800210// AddOptionalPath appends the given path string to a Make variable with the given name if it is
211// valid. It is a no-op if the given path is invalid.
Colin Crossc0efd1d2020-07-03 11:56:24 -0700212func (a *AndroidMkEntries) AddOptionalPath(name string, path OptionalPath) {
213 if path.Valid() {
214 a.AddPath(name, path.Path())
215 }
216}
217
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800218// SetPaths sets a Make variable with the given name to a slice of the given path strings.
Colin Cross08dca382020-07-21 20:31:17 -0700219func (a *AndroidMkEntries) SetPaths(name string, paths Paths) {
220 if _, ok := a.EntryMap[name]; !ok {
221 a.entryOrder = append(a.entryOrder, name)
222 }
223 a.EntryMap[name] = paths.Strings()
224}
225
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800226// SetOptionalPaths sets a Make variable with the given name to a slice of the given path strings
227// only if there are a non-zero amount of paths.
Colin Cross08dca382020-07-21 20:31:17 -0700228func (a *AndroidMkEntries) SetOptionalPaths(name string, paths Paths) {
229 if len(paths) > 0 {
230 a.SetPaths(name, paths)
231 }
232}
233
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800234// AddPaths appends the given path strings to a Make variable with the given name.
Colin Cross08dca382020-07-21 20:31:17 -0700235func (a *AndroidMkEntries) AddPaths(name string, paths Paths) {
236 if _, ok := a.EntryMap[name]; !ok {
237 a.entryOrder = append(a.entryOrder, name)
238 }
239 a.EntryMap[name] = append(a.EntryMap[name], paths.Strings()...)
240}
241
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800242// SetBoolIfTrue sets a Make variable with the given name to true if the given flag is true.
243// It is a no-op if the given flag is false.
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700244func (a *AndroidMkEntries) SetBoolIfTrue(name string, flag bool) {
245 if flag {
246 if _, ok := a.EntryMap[name]; !ok {
247 a.entryOrder = append(a.entryOrder, name)
248 }
249 a.EntryMap[name] = []string{"true"}
250 }
251}
252
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800253// SetBool sets a Make variable with the given name to if the given bool flag value.
Jaewoong Jung9a1e8bd2019-09-04 20:17:54 -0700254func (a *AndroidMkEntries) SetBool(name string, flag bool) {
255 if _, ok := a.EntryMap[name]; !ok {
256 a.entryOrder = append(a.entryOrder, name)
257 }
258 if flag {
259 a.EntryMap[name] = []string{"true"}
260 } else {
261 a.EntryMap[name] = []string{"false"}
262 }
263}
264
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800265// AddStrings appends the given strings to a Make variable with the given name.
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700266func (a *AndroidMkEntries) AddStrings(name string, value ...string) {
267 if len(value) == 0 {
268 return
269 }
270 if _, ok := a.EntryMap[name]; !ok {
271 a.entryOrder = append(a.entryOrder, name)
272 }
273 a.EntryMap[name] = append(a.EntryMap[name], value...)
274}
275
Liz Kammer57f5b332020-11-24 12:42:58 -0800276// AddCompatibilityTestSuites adds the supplied test suites to the EntryMap, with special handling
Tongbo Liuc5f7b962024-01-04 09:03:35 +0000277// for partial MTS and MCTS test suites.
Liz Kammer57f5b332020-11-24 12:42:58 -0800278func (a *AndroidMkEntries) AddCompatibilityTestSuites(suites ...string) {
Tongbo Liuc5f7b962024-01-04 09:03:35 +0000279 // M(C)TS supports a full test suite and partial per-module MTS test suites, with naming mts-${MODULE}.
280 // To reduce repetition, if we find a partial M(C)TS test suite without an full M(C)TS test suite,
Liz Kammer57f5b332020-11-24 12:42:58 -0800281 // we add the full test suite to our list.
282 if PrefixInList(suites, "mts-") && !InList("mts", suites) {
283 suites = append(suites, "mts")
284 }
Tongbo Liuc5f7b962024-01-04 09:03:35 +0000285 if PrefixInList(suites, "mcts-") && !InList("mcts", suites) {
286 suites = append(suites, "mcts")
287 }
Liz Kammer57f5b332020-11-24 12:42:58 -0800288 a.AddStrings("LOCAL_COMPATIBILITY_SUITE", suites...)
289}
290
Paul Duffin8b0349c2020-11-26 14:33:21 +0000291// The contributions to the dist.
292type distContributions struct {
Bob Badour51804382022-04-13 11:27:19 -0700293 // Path to license metadata file.
294 licenseMetadataFile Path
Paul Duffin8b0349c2020-11-26 14:33:21 +0000295 // List of goals and the dist copy instructions.
296 copiesForGoals []*copiesForGoals
297}
298
299// getCopiesForGoals returns a copiesForGoals into which copy instructions that
300// must be processed when building one or more of those goals can be added.
301func (d *distContributions) getCopiesForGoals(goals string) *copiesForGoals {
302 copiesForGoals := &copiesForGoals{goals: goals}
303 d.copiesForGoals = append(d.copiesForGoals, copiesForGoals)
304 return copiesForGoals
305}
306
307// Associates a list of dist copy instructions with a set of goals for which they
308// should be run.
309type copiesForGoals struct {
310 // goals are a space separated list of build targets that will trigger the
311 // copy instructions.
312 goals string
313
314 // A list of instructions to copy a module's output files to somewhere in the
315 // dist directory.
316 copies []distCopy
317}
318
319// Adds a copy instruction.
320func (d *copiesForGoals) addCopyInstruction(from Path, dest string) {
321 d.copies = append(d.copies, distCopy{from, dest})
322}
323
324// Instruction on a path that must be copied into the dist.
325type distCopy struct {
326 // The path to copy from.
327 from Path
328
329 // The destination within the dist directory to copy to.
330 dest string
331}
332
Jihoon Kang593171e2025-02-05 01:54:45 +0000333func (d *distCopy) String() string {
334 if len(d.dest) == 0 {
335 return d.from.String()
336 }
337 return fmt.Sprintf("%s:%s", d.from.String(), d.dest)
338}
339
340type distCopies []distCopy
341
342func (d *distCopies) Strings() (ret []string) {
343 if d == nil {
344 return
345 }
346 for _, dist := range *d {
347 ret = append(ret, dist.String())
348 }
349 return
350}
351
Cole Fausta8437c52025-02-25 14:45:43 -0800352// This gets the dist contributuions from the given module that were specified in the Android.bp
353// file using the dist: property. It does not include contribututions that the module's
354// implementation may have defined with ctx.DistForGoals(), for that, see DistProvider.
355func getDistContributions(ctx ConfigAndOtherModuleProviderContext, mod Module) *distContributions {
Yu Liu14b81452025-02-18 23:26:13 +0000356 amod := mod.base()
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000357 name := amod.BaseModuleName()
358
Cole Fausta8437c52025-02-25 14:45:43 -0800359 info := OtherModuleProviderOrDefault(ctx, mod, InstallFilesProvider)
360 availableTaggedDists := info.DistFiles
Paul Duffinaf970a22020-11-23 23:32:56 +0000361
Paul Duffin74f05592020-11-25 16:37:46 +0000362 if len(availableTaggedDists) == 0 {
Jingwen Chen7b27ca72020-07-24 09:13:49 +0000363 // Nothing dist-able for this module.
364 return nil
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000365 }
366
Paul Duffin8b0349c2020-11-26 14:33:21 +0000367 // Collate the contributions this module makes to the dist.
368 distContributions := &distContributions{}
369
Yu Liu14b81452025-02-18 23:26:13 +0000370 if !exemptFromRequiredApplicableLicensesProperty(mod) {
Yu Liuec810542024-08-26 18:09:15 +0000371 distContributions.licenseMetadataFile = info.LicenseMetadataFile
Bob Badour4660a982022-09-12 16:06:03 -0700372 }
Bob Badour51804382022-04-13 11:27:19 -0700373
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000374 // Iterate over this module's dist structs, merged from the dist and dists properties.
375 for _, dist := range amod.Dists() {
376 // Get the list of goals this dist should be enabled for. e.g. sdk, droidcore
377 goals := strings.Join(dist.Targets, " ")
378
379 // Get the tag representing the output files to be dist'd. e.g. ".jar", ".proguard_map"
380 var tag string
381 if dist.Tag == nil {
382 // If the dist struct does not specify a tag, use the default output files tag.
Paul Duffin74f05592020-11-25 16:37:46 +0000383 tag = DefaultDistTag
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000384 } else {
385 tag = *dist.Tag
386 }
387
388 // Get the paths of the output files to be dist'd, represented by the tag.
389 // Can be an empty list.
390 tagPaths := availableTaggedDists[tag]
391 if len(tagPaths) == 0 {
392 // Nothing to dist for this tag, continue to the next dist.
393 continue
394 }
395
396 if len(tagPaths) > 1 && (dist.Dest != nil || dist.Suffix != nil) {
Paul Duffin74f05592020-11-25 16:37:46 +0000397 errorMessage := "%s: Cannot apply dest/suffix for more than one dist " +
398 "file for %q goals tag %q in module %s. The list of dist files, " +
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000399 "which should have a single element, is:\n%s"
Paul Duffin74f05592020-11-25 16:37:46 +0000400 panic(fmt.Errorf(errorMessage, mod, goals, tag, name, tagPaths))
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000401 }
402
Paul Duffin8b0349c2020-11-26 14:33:21 +0000403 copiesForGoals := distContributions.getCopiesForGoals(goals)
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000404
Paul Duffin8b0349c2020-11-26 14:33:21 +0000405 // Iterate over each path adding a copy instruction to copiesForGoals
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000406 for _, path := range tagPaths {
407 // It's possible that the Path is nil from errant modules. Be defensive here.
408 if path == nil {
409 tagName := "default" // for error message readability
410 if dist.Tag != nil {
411 tagName = *dist.Tag
412 }
413 panic(fmt.Errorf("Dist file should not be nil for the %s tag in %s", tagName, name))
414 }
415
416 dest := filepath.Base(path.String())
417
418 if dist.Dest != nil {
419 var err error
420 if dest, err = validateSafePath(*dist.Dest); err != nil {
421 // This was checked in ModuleBase.GenerateBuildActions
422 panic(err)
423 }
424 }
425
Trevor Radcliffe90727f42022-03-21 19:34:02 +0000426 ext := filepath.Ext(dest)
427 suffix := ""
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000428 if dist.Suffix != nil {
Trevor Radcliffe90727f42022-03-21 19:34:02 +0000429 suffix = *dist.Suffix
430 }
431
432 productString := ""
433 if dist.Append_artifact_with_product != nil && *dist.Append_artifact_with_product {
Cole Fausta8437c52025-02-25 14:45:43 -0800434 productString = fmt.Sprintf("_%s", ctx.Config().DeviceProduct())
Trevor Radcliffe90727f42022-03-21 19:34:02 +0000435 }
436
437 if suffix != "" || productString != "" {
438 dest = strings.TrimSuffix(dest, ext) + suffix + productString + ext
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000439 }
440
441 if dist.Dir != nil {
442 var err error
443 if dest, err = validateSafePath(*dist.Dir, dest); err != nil {
444 // This was checked in ModuleBase.GenerateBuildActions
445 panic(err)
446 }
447 }
448
Paul Duffin8b0349c2020-11-26 14:33:21 +0000449 copiesForGoals.addCopyInstruction(path, dest)
450 }
451 }
452
453 return distContributions
454}
455
456// generateDistContributionsForMake generates make rules that will generate the
457// dist according to the instructions in the supplied distContribution.
458func generateDistContributionsForMake(distContributions *distContributions) []string {
459 var ret []string
460 for _, d := range distContributions.copiesForGoals {
Yu Liue70976d2024-10-15 20:45:35 +0000461 ret = append(ret, fmt.Sprintf(".PHONY: %s", d.goals))
Paul Duffin8b0349c2020-11-26 14:33:21 +0000462 // Create dist-for-goals calls for each of the copy instructions.
463 for _, c := range d.copies {
Bob Badour4660a982022-09-12 16:06:03 -0700464 if distContributions.licenseMetadataFile != nil {
465 ret = append(
466 ret,
Yu Liue70976d2024-10-15 20:45:35 +0000467 fmt.Sprintf("$(if $(strip $(ALL_TARGETS.%s.META_LIC)),,$(eval ALL_TARGETS.%s.META_LIC := %s))",
Bob Badour4660a982022-09-12 16:06:03 -0700468 c.from.String(), c.from.String(), distContributions.licenseMetadataFile.String()))
469 }
Bob Badour51804382022-04-13 11:27:19 -0700470 ret = append(
471 ret,
Yu Liue70976d2024-10-15 20:45:35 +0000472 fmt.Sprintf("$(call dist-for-goals,%s,%s:%s)", d.goals, c.from.String(), c.dest))
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000473 }
474 }
475
476 return ret
477}
478
Paul Duffin8b0349c2020-11-26 14:33:21 +0000479// Compute the list of Make strings to declare phony goals and dist-for-goals
480// calls from the module's dist and dists properties.
Yu Liu14b81452025-02-18 23:26:13 +0000481func (a *AndroidMkEntries) GetDistForGoals(mod Module) []string {
Cole Fausta8437c52025-02-25 14:45:43 -0800482 distContributions := getDistContributions(a.entryContext, mod)
Paul Duffin8b0349c2020-11-26 14:33:21 +0000483 if distContributions == nil {
484 return nil
485 }
486
487 return generateDistContributionsForMake(distContributions)
488}
489
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800490// fillInEntries goes through the common variable processing and calls the extra data funcs to
491// generate and fill in AndroidMkEntries's in-struct data, ready to be flushed to a file.
Colin Crossaa255532020-07-03 13:18:24 -0700492type fillInEntriesContext interface {
493 ModuleDir(module blueprint.Module) string
Cole Faust39aabe92023-02-23 16:57:43 -0800494 ModuleSubDir(module blueprint.Module) string
Colin Crossaa255532020-07-03 13:18:24 -0700495 Config() Config
Yu Liu663e4502024-08-12 18:23:59 +0000496 otherModuleProvider(module blueprint.Module, provider blueprint.AnyProviderKey) (any, bool)
Sasha Smundak5c4729d2022-12-01 10:49:23 -0800497 ModuleType(module blueprint.Module) string
Cole Faust43ddd082024-06-17 12:32:40 -0700498 OtherModulePropertyErrorf(module Module, property string, fmt string, args ...interface{})
Cole Faust4e2bf9f2024-09-11 13:26:20 -0700499 HasMutatorFinished(mutatorName string) bool
Colin Crossaa255532020-07-03 13:18:24 -0700500}
501
Yu Liu14b81452025-02-18 23:26:13 +0000502func (a *AndroidMkEntries) fillInEntries(ctx fillInEntriesContext, mod Module) {
Trevor Radcliffe90727f42022-03-21 19:34:02 +0000503 a.entryContext = ctx
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700504 a.EntryMap = make(map[string][]string)
Yu Liu14b81452025-02-18 23:26:13 +0000505 base := mod.base()
Colin Crossf1f763a2021-10-21 16:14:19 -0700506 name := base.BaseModuleName()
Colin Cross0477b422020-10-13 18:43:54 -0700507 if a.OverrideName != "" {
508 name = a.OverrideName
509 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700510
511 if a.Include == "" {
512 a.Include = "$(BUILD_PREBUILT)"
513 }
Yu Liu14b81452025-02-18 23:26:13 +0000514 a.Required = append(a.Required, mod.RequiredModuleNames(ctx)...)
515 a.Required = append(a.Required, mod.VintfFragmentModuleNames(ctx)...)
516 a.Host_required = append(a.Host_required, mod.HostRequiredModuleNames()...)
517 a.Target_required = append(a.Target_required, mod.TargetRequiredModuleNames()...)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700518
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000519 for _, distString := range a.GetDistForGoals(mod) {
Yu Liue70976d2024-10-15 20:45:35 +0000520 fmt.Fprintln(&a.header, distString)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700521 }
522
Cole Faust39aabe92023-02-23 16:57:43 -0800523 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 -0700524
525 // Collect make variable assignment entries.
Colin Crossaa255532020-07-03 13:18:24 -0700526 a.SetString("LOCAL_PATH", ctx.ModuleDir(mod))
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700527 a.SetString("LOCAL_MODULE", name+a.SubName)
528 a.SetString("LOCAL_MODULE_CLASS", a.Class)
529 a.SetString("LOCAL_PREBUILT_MODULE_FILE", a.OutputFile.String())
530 a.AddStrings("LOCAL_REQUIRED_MODULES", a.Required...)
531 a.AddStrings("LOCAL_HOST_REQUIRED_MODULES", a.Host_required...)
532 a.AddStrings("LOCAL_TARGET_REQUIRED_MODULES", a.Target_required...)
Yu Liu14b81452025-02-18 23:26:13 +0000533 a.AddStrings("LOCAL_SOONG_MODULE_TYPE", ctx.ModuleType(mod))
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700534
Colin Cross6301c3c2021-09-28 17:40:21 -0700535 // If the install rule was generated by Soong tell Make about it.
Yu Liud46e5ae2024-08-15 18:46:17 +0000536 info := OtherModuleProviderOrDefault(ctx, mod, InstallFilesProvider)
537 if len(info.KatiInstalls) > 0 {
Colin Cross6301c3c2021-09-28 17:40:21 -0700538 // Assume the primary install file is last since it probably needs to depend on any other
539 // installed files. If that is not the case we can add a method to specify the primary
540 // installed file.
Yu Liud46e5ae2024-08-15 18:46:17 +0000541 a.SetPath("LOCAL_SOONG_INSTALLED_MODULE", info.KatiInstalls[len(info.KatiInstalls)-1].to)
542 a.SetString("LOCAL_SOONG_INSTALL_PAIRS", info.KatiInstalls.BuiltInstalled())
543 a.SetPaths("LOCAL_SOONG_INSTALL_SYMLINKS", info.KatiSymlinks.InstallPaths().Paths())
Jiyong Park3f627e62024-05-01 16:14:38 +0900544 } else {
545 // Soong may not have generated the install rule also when `no_full_install: true`.
546 // Mark this module as uninstallable in order to prevent Make from creating an
547 // install rule there.
548 a.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", proptools.Bool(base.commonProperties.No_full_install))
Colin Cross6301c3c2021-09-28 17:40:21 -0700549 }
550
Colin Crossa6182ab2024-08-21 10:47:44 -0700551 if info.UncheckedModule {
552 a.SetBool("LOCAL_DONT_CHECK_MODULE", true)
553 } else if info.CheckbuildTarget != nil {
554 a.SetPath("LOCAL_CHECKED_MODULE", info.CheckbuildTarget)
555 } else {
556 a.SetOptionalPath("LOCAL_CHECKED_MODULE", a.OutputFile)
557 }
558
Yu Liud46e5ae2024-08-15 18:46:17 +0000559 if len(info.TestData) > 0 {
560 a.AddStrings("LOCAL_TEST_DATA", androidMkDataPaths(info.TestData)...)
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800561 }
562
Jiyong Park89e850a2020-04-07 16:37:39 +0900563 if am, ok := mod.(ApexModule); ok {
564 a.SetBoolIfTrue("LOCAL_NOT_AVAILABLE_FOR_PLATFORM", am.NotAvailableForPlatform())
565 }
566
Colin Crossf1f763a2021-10-21 16:14:19 -0700567 archStr := base.Arch().ArchType.String()
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700568 host := false
Colin Crossf1f763a2021-10-21 16:14:19 -0700569 switch base.Os().Class {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700570 case Host:
Colin Crossf1f763a2021-10-21 16:14:19 -0700571 if base.Target().HostCross {
Jiyong Park1613e552020-09-14 19:43:17 +0900572 // Make cannot identify LOCAL_MODULE_HOST_CROSS_ARCH:= common.
Colin Crossf1f763a2021-10-21 16:14:19 -0700573 if base.Arch().ArchType != Common {
Jiyong Park1613e552020-09-14 19:43:17 +0900574 a.SetString("LOCAL_MODULE_HOST_CROSS_ARCH", archStr)
575 }
576 } else {
577 // Make cannot identify LOCAL_MODULE_HOST_ARCH:= common.
Colin Crossf1f763a2021-10-21 16:14:19 -0700578 if base.Arch().ArchType != Common {
Jiyong Park1613e552020-09-14 19:43:17 +0900579 a.SetString("LOCAL_MODULE_HOST_ARCH", archStr)
580 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700581 }
582 host = true
583 case Device:
584 // Make cannot identify LOCAL_MODULE_TARGET_ARCH:= common.
Colin Crossf1f763a2021-10-21 16:14:19 -0700585 if base.Arch().ArchType != Common {
586 if base.Target().NativeBridge {
587 hostArchStr := base.Target().NativeBridgeHostArchName
dimitry1f33e402019-03-26 12:39:31 +0100588 if hostArchStr != "" {
589 a.SetString("LOCAL_MODULE_TARGET_ARCH", hostArchStr)
590 }
591 } else {
592 a.SetString("LOCAL_MODULE_TARGET_ARCH", archStr)
593 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700594 }
595
Kelvin Zhang46977252022-04-13 16:41:34 -0700596 if !base.InVendorRamdisk() {
Yu Liu82a6d142024-08-27 19:02:29 +0000597 a.AddPaths("LOCAL_FULL_INIT_RC", info.InitRcPaths)
Yifan Hong919dae12020-12-02 18:55:06 -0800598 }
Yu Liu82a6d142024-08-27 19:02:29 +0000599 if len(info.VintfFragmentsPaths) > 0 {
600 a.AddPaths("LOCAL_FULL_VINTF_FRAGMENTS", info.VintfFragmentsPaths)
Liz Kammer7b3dc8a2021-04-16 16:41:59 -0400601 }
Colin Crossf1f763a2021-10-21 16:14:19 -0700602 a.SetBoolIfTrue("LOCAL_PROPRIETARY_MODULE", Bool(base.commonProperties.Proprietary))
603 if Bool(base.commonProperties.Vendor) || Bool(base.commonProperties.Soc_specific) {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700604 a.SetString("LOCAL_VENDOR_MODULE", "true")
605 }
Colin Crossf1f763a2021-10-21 16:14:19 -0700606 a.SetBoolIfTrue("LOCAL_ODM_MODULE", Bool(base.commonProperties.Device_specific))
607 a.SetBoolIfTrue("LOCAL_PRODUCT_MODULE", Bool(base.commonProperties.Product_specific))
608 a.SetBoolIfTrue("LOCAL_SYSTEM_EXT_MODULE", Bool(base.commonProperties.System_ext_specific))
609 if base.commonProperties.Owner != nil {
610 a.SetString("LOCAL_MODULE_OWNER", *base.commonProperties.Owner)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700611 }
612 }
613
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700614 if host {
Colin Crossf1f763a2021-10-21 16:14:19 -0700615 makeOs := base.Os().String()
616 if base.Os() == Linux || base.Os() == LinuxBionic || base.Os() == LinuxMusl {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700617 makeOs = "linux"
618 }
619 a.SetString("LOCAL_MODULE_HOST_OS", makeOs)
620 a.SetString("LOCAL_IS_HOST_MODULE", "true")
621 }
622
623 prefix := ""
Colin Crossf1f763a2021-10-21 16:14:19 -0700624 if base.ArchSpecific() {
625 switch base.Os().Class {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700626 case Host:
Colin Crossf1f763a2021-10-21 16:14:19 -0700627 if base.Target().HostCross {
Jiyong Park1613e552020-09-14 19:43:17 +0900628 prefix = "HOST_CROSS_"
629 } else {
630 prefix = "HOST_"
631 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700632 case Device:
633 prefix = "TARGET_"
634
635 }
636
Colin Crossf1f763a2021-10-21 16:14:19 -0700637 if base.Arch().ArchType != ctx.Config().Targets[base.Os()][0].Arch.ArchType {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700638 prefix = "2ND_" + prefix
639 }
640 }
Colin Crossaa255532020-07-03 13:18:24 -0700641
Yu Liu663e4502024-08-12 18:23:59 +0000642 if licenseMetadata, ok := OtherModuleProvider(ctx, mod, LicenseMetadataProvider); ok {
Colin Cross4acaea92021-12-10 23:05:02 +0000643 a.SetPath("LOCAL_SOONG_LICENSE_METADATA", licenseMetadata.LicenseMetadataPath)
644 }
645
Yu Liu663e4502024-08-12 18:23:59 +0000646 if _, ok := OtherModuleProvider(ctx, mod, ModuleInfoJSONProvider); ok {
Colin Crossd6fd0132023-11-06 13:54:06 -0800647 a.SetBool("LOCAL_SOONG_MODULE_INFO_JSON", true)
648 }
649
Colin Crossaa255532020-07-03 13:18:24 -0700650 extraCtx := &androidMkExtraEntriesContext{
651 ctx: ctx,
652 mod: mod,
653 }
654
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700655 for _, extra := range a.ExtraEntries {
Colin Crossaa255532020-07-03 13:18:24 -0700656 extra(extraCtx, a)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700657 }
658
659 // Write to footer.
660 fmt.Fprintln(&a.footer, "include "+a.Include)
Colin Crossaa255532020-07-03 13:18:24 -0700661 blueprintDir := ctx.ModuleDir(mod)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700662 for _, footerFunc := range a.ExtraFooters {
Jaewoong Jung02b11a62020-12-07 10:23:54 -0800663 footerFunc(&a.footer, name, prefix, blueprintDir)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700664 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700665}
666
Colin Crossd6fd0132023-11-06 13:54:06 -0800667func (a *AndroidMkEntries) disabled() bool {
668 return a.Disabled || !a.OutputFile.Valid()
669}
670
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800671// write flushes the AndroidMkEntries's in-struct data populated by AndroidMkEntries into the
672// given Writer object.
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700673func (a *AndroidMkEntries) write(w io.Writer) {
Colin Crossd6fd0132023-11-06 13:54:06 -0800674 if a.disabled() {
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700675 return
676 }
677
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700678 w.Write(a.header.Bytes())
679 for _, name := range a.entryOrder {
Sasha Smundakdcb61292022-12-08 10:41:33 -0800680 AndroidMkEmitAssignList(w, name, a.EntryMap[name])
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700681 }
682 w.Write(a.footer.Bytes())
683}
684
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700685func (a *AndroidMkEntries) FooterLinesForTests() []string {
686 return strings.Split(string(a.footer.Bytes()), "\n")
687}
688
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800689// AndroidMkSingleton is a singleton to collect Android.mk data from all modules and dump them into
690// the final Android-<product_name>.mk file output.
Colin Cross0875c522017-11-28 17:34:01 -0800691func AndroidMkSingleton() Singleton {
Dan Willemsen218f6562015-07-08 18:13:11 -0700692 return &androidMkSingleton{}
693}
694
695type androidMkSingleton struct{}
696
Yu Liu14b81452025-02-18 23:26:13 +0000697func allModulesSorted(ctx SingletonContext) []Module {
698 var allModules []Module
Colin Cross4f6e4e62016-01-11 12:55:55 -0800699
Yu Liu14b81452025-02-18 23:26:13 +0000700 ctx.VisitAllModules(func(module Module) {
Jihoon Kang4be8eb32025-02-10 23:02:42 +0000701 allModules = append(allModules, module)
Colin Cross4f6e4e62016-01-11 12:55:55 -0800702 })
Dan Willemsen218f6562015-07-08 18:13:11 -0700703
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800704 // Sort the module list by the module names to eliminate random churns, which may erroneously
705 // invoke additional build processes.
Jihoon Kang4be8eb32025-02-10 23:02:42 +0000706 sort.SliceStable(allModules, func(i, j int) bool {
707 return ctx.ModuleName(allModules[i]) < ctx.ModuleName(allModules[j])
Colin Cross1ad81422019-01-14 12:47:35 -0800708 })
Colin Crossd779da42015-12-17 18:00:23 -0800709
Jihoon Kang4be8eb32025-02-10 23:02:42 +0000710 return allModules
711}
712
713func (c *androidMkSingleton) GenerateBuildActions(ctx SingletonContext) {
714 // If running in soong-only mode, more limited version of this singleton is run as
715 // soong only androidmk singleton
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800716 if !ctx.Config().KatiEnabled() {
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800717 return
718 }
719
Dan Willemsen45133ac2018-03-09 21:22:06 -0800720 transMk := PathForOutput(ctx, "Android"+String(ctx.Config().productVariables.Make_suffix)+".mk")
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700721 if ctx.Failed() {
722 return
723 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700724
Colin Crossd6fd0132023-11-06 13:54:06 -0800725 moduleInfoJSON := PathForOutput(ctx, "module-info"+String(ctx.Config().productVariables.Make_suffix)+".json")
726
Jihoon Kang4be8eb32025-02-10 23:02:42 +0000727 err := translateAndroidMk(ctx, absolutePath(transMk.String()), moduleInfoJSON, allModulesSorted(ctx))
Dan Willemsen218f6562015-07-08 18:13:11 -0700728 if err != nil {
729 ctx.Errorf(err.Error())
730 }
731
Colin Cross0875c522017-11-28 17:34:01 -0800732 ctx.Build(pctx, BuildParams{
733 Rule: blueprint.Phony,
734 Output: transMk,
Dan Willemsen218f6562015-07-08 18:13:11 -0700735 })
736}
737
Jihoon Kang4be8eb32025-02-10 23:02:42 +0000738type soongOnlyAndroidMkSingleton struct {
739 Singleton
740}
741
742func soongOnlyAndroidMkSingletonFactory() Singleton {
743 return &soongOnlyAndroidMkSingleton{}
744}
745
746func (so *soongOnlyAndroidMkSingleton) GenerateBuildActions(ctx SingletonContext) {
747 if !ctx.Config().KatiEnabled() {
748 so.soongOnlyBuildActions(ctx, allModulesSorted(ctx))
749 }
750}
751
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800752// In soong-only mode, we don't do most of the androidmk stuff. But disted files are still largely
753// defined through the androidmk mechanisms, so this function is an alternate implementation of
754// the androidmk singleton that just focuses on getting the dist contributions
Yu Liu64371e02025-02-19 23:44:48 +0000755// TODO(b/397766191): Change the signature to take ModuleProxy
756// Please only access the module's internal data through providers.
Yu Liu14b81452025-02-18 23:26:13 +0000757func (so *soongOnlyAndroidMkSingleton) soongOnlyBuildActions(ctx SingletonContext, mods []Module) {
Cole Faustf9a096c2025-01-21 16:55:43 -0800758 allDistContributions, moduleInfoJSONs := getSoongOnlyDataFromMods(ctx, mods)
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800759
Cole Faustf2aab5e2025-02-11 13:32:51 -0800760 singletonDists := getSingletonDists(ctx.Config())
761 singletonDists.lock.Lock()
762 if contribution := distsToDistContributions(singletonDists.dists); contribution != nil {
763 allDistContributions = append(allDistContributions, *contribution)
764 }
765 singletonDists.lock.Unlock()
766
Cole Faustf9a096c2025-01-21 16:55:43 -0800767 // Build module-info.json. Only in builds with HasDeviceProduct(), as we need a named
768 // device to have a TARGET_OUT folder.
769 if ctx.Config().HasDeviceProduct() {
Cole Faust601da062025-01-22 13:15:10 -0800770 preMergePath := PathForOutput(ctx, "module_info_pre_merging.json")
Cole Faustf9a096c2025-01-21 16:55:43 -0800771 moduleInfoJSONPath := pathForInstall(ctx, Android, X86_64, "", "module-info.json")
Cole Faust601da062025-01-22 13:15:10 -0800772 if err := writeModuleInfoJSON(ctx, moduleInfoJSONs, preMergePath); err != nil {
Cole Faustf9a096c2025-01-21 16:55:43 -0800773 ctx.Errorf("%s", err)
774 }
Cole Faust601da062025-01-22 13:15:10 -0800775 builder := NewRuleBuilder(pctx, ctx)
776 builder.Command().
777 BuiltTool("merge_module_info_json").
778 FlagWithOutput("-o ", moduleInfoJSONPath).
779 Input(preMergePath)
780 builder.Build("merge_module_info_json", "merge module info json")
Cole Faustf9a096c2025-01-21 16:55:43 -0800781 ctx.Phony("module-info", moduleInfoJSONPath)
782 ctx.Phony("droidcore-unbundled", moduleInfoJSONPath)
783 allDistContributions = append(allDistContributions, distContributions{
784 copiesForGoals: []*copiesForGoals{{
785 goals: "general-tests droidcore-unbundled",
786 copies: []distCopy{{
787 from: moduleInfoJSONPath,
788 dest: "module-info.json",
789 }},
790 }},
791 })
792 }
793
794 // Build dist.mk for the packaging step to read and generate dist targets
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800795 distMkFile := absolutePath(filepath.Join(ctx.Config().katiPackageMkDir(), "dist.mk"))
796
797 var goalOutputPairs []string
Cole Faust82611a12025-01-09 11:20:41 -0800798 var srcDstPairs []string
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800799 for _, contributions := range allDistContributions {
800 for _, copiesForGoal := range contributions.copiesForGoals {
801 goals := strings.Fields(copiesForGoal.goals)
802 for _, copy := range copiesForGoal.copies {
803 for _, goal := range goals {
Cole Faust82611a12025-01-09 11:20:41 -0800804 goalOutputPairs = append(goalOutputPairs, fmt.Sprintf(" %s:%s", goal, copy.dest))
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800805 }
Cole Faust82611a12025-01-09 11:20:41 -0800806 srcDstPairs = append(srcDstPairs, fmt.Sprintf(" %s:%s", copy.from.String(), copy.dest))
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800807 }
808 }
809 }
Cole Faust82611a12025-01-09 11:20:41 -0800810 // There are duplicates in the lists that we need to remove
811 goalOutputPairs = SortedUniqueStrings(goalOutputPairs)
812 srcDstPairs = SortedUniqueStrings(srcDstPairs)
813 var buf strings.Builder
814 buf.WriteString("DIST_SRC_DST_PAIRS :=")
815 for _, srcDstPair := range srcDstPairs {
816 buf.WriteString(srcDstPair)
817 }
818 buf.WriteString("\nDIST_GOAL_OUTPUT_PAIRS :=")
819 for _, goalOutputPair := range goalOutputPairs {
820 buf.WriteString(goalOutputPair)
821 }
822 buf.WriteString("\n")
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800823
824 writeValueIfChanged(ctx, distMkFile, buf.String())
825}
826
827func writeValueIfChanged(ctx SingletonContext, path string, value string) {
828 if err := os.MkdirAll(filepath.Dir(path), 0777); err != nil {
829 ctx.Errorf("%s\n", err)
830 return
831 }
832 previousValue := ""
833 rawPreviousValue, err := os.ReadFile(path)
834 if err == nil {
835 previousValue = string(rawPreviousValue)
836 }
837
838 if previousValue != value {
839 if err = os.WriteFile(path, []byte(value), 0666); err != nil {
840 ctx.Errorf("Failed to write: %v", err)
841 }
842 }
843}
844
Cole Faustd62a4892025-02-07 16:55:11 -0800845func distsToDistContributions(dists []dist) *distContributions {
846 if len(dists) == 0 {
Jihoon Kang593171e2025-02-05 01:54:45 +0000847 return nil
848 }
849
850 copyGoals := []*copiesForGoals{}
Cole Faustd62a4892025-02-07 16:55:11 -0800851 for _, dist := range dists {
Jihoon Kang593171e2025-02-05 01:54:45 +0000852 for _, goal := range dist.goals {
Cole Faustd62a4892025-02-07 16:55:11 -0800853 copyGoals = append(copyGoals, &copiesForGoals{
854 goals: goal,
855 copies: dist.paths,
856 })
Jihoon Kang593171e2025-02-05 01:54:45 +0000857 }
858 }
859
Cole Faustd62a4892025-02-07 16:55:11 -0800860 return &distContributions{
861 copiesForGoals: copyGoals,
862 }
Jihoon Kang593171e2025-02-05 01:54:45 +0000863}
864
Cole Faustf9a096c2025-01-21 16:55:43 -0800865// getSoongOnlyDataFromMods gathers data from the given modules needed in soong-only builds.
866// Currently, this is the dist contributions, and the module-info.json contents.
Yu Liu14b81452025-02-18 23:26:13 +0000867func getSoongOnlyDataFromMods(ctx fillInEntriesContext, mods []Module) ([]distContributions, []*ModuleInfoJSON) {
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800868 var allDistContributions []distContributions
Cole Faustf9a096c2025-01-21 16:55:43 -0800869 var moduleInfoJSONs []*ModuleInfoJSON
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800870 for _, mod := range mods {
Cole Faustd62a4892025-02-07 16:55:11 -0800871 if distInfo, ok := OtherModuleProvider(ctx, mod, DistProvider); ok {
872 if contribution := distsToDistContributions(distInfo.Dists); contribution != nil {
873 allDistContributions = append(allDistContributions, *contribution)
874 }
875 }
876
Yu Liu64371e02025-02-19 23:44:48 +0000877 commonInfo, _ := OtherModuleProvider(ctx, mod, CommonModuleInfoKey)
878 if commonInfo.SkipAndroidMkProcessing {
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800879 continue
880 }
881 if info, ok := OtherModuleProvider(ctx, mod, AndroidMkInfoProvider); ok {
Cole Faust556f1f42025-01-09 10:49:42 -0800882 // Deep copy the provider info since we need to modify the info later
883 info := deepCopyAndroidMkProviderInfo(info)
Yu Liu64371e02025-02-19 23:44:48 +0000884 info.PrimaryInfo.fillInEntries(ctx, mod, &commonInfo)
Cole Faust556f1f42025-01-09 10:49:42 -0800885 if info.PrimaryInfo.disabled() {
886 continue
887 }
Cole Faustf9a096c2025-01-21 16:55:43 -0800888 if moduleInfoJSON, ok := OtherModuleProvider(ctx, mod, ModuleInfoJSONProvider); ok {
Jihoon Kangd4063812025-01-24 00:25:30 +0000889 moduleInfoJSONs = append(moduleInfoJSONs, moduleInfoJSON...)
Cole Faustf9a096c2025-01-21 16:55:43 -0800890 }
Cole Fausta8437c52025-02-25 14:45:43 -0800891 if contribution := getDistContributions(ctx, mod); contribution != nil {
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800892 allDistContributions = append(allDistContributions, *contribution)
893 }
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800894 } else {
Jihoon Kange6daf662025-02-06 01:38:16 +0000895 if x, ok := mod.(AndroidMkDataProvider); ok {
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800896 data := x.AndroidMk()
897
898 if data.Include == "" {
899 data.Include = "$(BUILD_PREBUILT)"
900 }
901
902 data.fillInData(ctx, mod)
Cole Faust556f1f42025-01-09 10:49:42 -0800903 if data.Entries.disabled() {
904 continue
905 }
Cole Faustf9a096c2025-01-21 16:55:43 -0800906 if moduleInfoJSON, ok := OtherModuleProvider(ctx, mod, ModuleInfoJSONProvider); ok {
Jihoon Kangd4063812025-01-24 00:25:30 +0000907 moduleInfoJSONs = append(moduleInfoJSONs, moduleInfoJSON...)
Cole Faustf9a096c2025-01-21 16:55:43 -0800908 }
Cole Fausta8437c52025-02-25 14:45:43 -0800909 if contribution := getDistContributions(ctx, mod); contribution != nil {
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800910 allDistContributions = append(allDistContributions, *contribution)
911 }
Jihoon Kange6daf662025-02-06 01:38:16 +0000912 }
913 if x, ok := mod.(AndroidMkEntriesProvider); ok {
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800914 entriesList := x.AndroidMkEntries()
915 for _, entries := range entriesList {
916 entries.fillInEntries(ctx, mod)
Cole Faust556f1f42025-01-09 10:49:42 -0800917 if entries.disabled() {
918 continue
919 }
Cole Faustf9a096c2025-01-21 16:55:43 -0800920 if moduleInfoJSON, ok := OtherModuleProvider(ctx, mod, ModuleInfoJSONProvider); ok {
Jihoon Kangd4063812025-01-24 00:25:30 +0000921 moduleInfoJSONs = append(moduleInfoJSONs, moduleInfoJSON...)
Cole Faustf9a096c2025-01-21 16:55:43 -0800922 }
Cole Fausta8437c52025-02-25 14:45:43 -0800923 if contribution := getDistContributions(ctx, mod); contribution != nil {
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800924 allDistContributions = append(allDistContributions, *contribution)
925 }
926 }
Jihoon Kange6daf662025-02-06 01:38:16 +0000927 }
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800928 }
929 }
Cole Faustf9a096c2025-01-21 16:55:43 -0800930 return allDistContributions, moduleInfoJSONs
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800931}
932
Yu Liu14b81452025-02-18 23:26:13 +0000933func translateAndroidMk(ctx SingletonContext, absMkFile string, moduleInfoJSONPath WritablePath, mods []Module) error {
Dan Willemsen218f6562015-07-08 18:13:11 -0700934 buf := &bytes.Buffer{}
935
Colin Crossd6fd0132023-11-06 13:54:06 -0800936 var moduleInfoJSONs []*ModuleInfoJSON
937
Dan Willemsen97750522016-02-09 17:43:51 -0800938 fmt.Fprintln(buf, "LOCAL_MODULE_MAKEFILE := $(lastword $(MAKEFILE_LIST))")
Dan Willemsen218f6562015-07-08 18:13:11 -0700939
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800940 typeStats := make(map[string]int)
Dan Willemsen218f6562015-07-08 18:13:11 -0700941 for _, mod := range mods {
Colin Crossd6fd0132023-11-06 13:54:06 -0800942 err := translateAndroidMkModule(ctx, buf, &moduleInfoJSONs, mod)
Dan Willemsen218f6562015-07-08 18:13:11 -0700943 if err != nil {
Colin Cross836e3872021-11-09 12:30:59 -0800944 os.Remove(absMkFile)
Dan Willemsen218f6562015-07-08 18:13:11 -0700945 return err
946 }
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700947
Yu Liu14b81452025-02-18 23:26:13 +0000948 if ctx.PrimaryModule(mod) == mod {
949 typeStats[ctx.ModuleType(mod)] += 1
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700950 }
951 }
952
953 keys := []string{}
954 fmt.Fprintln(buf, "\nSTATS.SOONG_MODULE_TYPE :=")
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800955 for k := range typeStats {
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700956 keys = append(keys, k)
957 }
958 sort.Strings(keys)
959 for _, mod_type := range keys {
960 fmt.Fprintln(buf, "STATS.SOONG_MODULE_TYPE +=", mod_type)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800961 fmt.Fprintf(buf, "STATS.SOONG_MODULE_TYPE.%s := %d\n", mod_type, typeStats[mod_type])
Dan Willemsen218f6562015-07-08 18:13:11 -0700962 }
963
Colin Crossd6fd0132023-11-06 13:54:06 -0800964 err := pathtools.WriteFileIfChanged(absMkFile, buf.Bytes(), 0666)
965 if err != nil {
966 return err
967 }
968
969 return writeModuleInfoJSON(ctx, moduleInfoJSONs, moduleInfoJSONPath)
Dan Willemsen218f6562015-07-08 18:13:11 -0700970}
971
Colin Crossd6fd0132023-11-06 13:54:06 -0800972func writeModuleInfoJSON(ctx SingletonContext, moduleInfoJSONs []*ModuleInfoJSON, moduleInfoJSONPath WritablePath) error {
973 moduleInfoJSONBuf := &strings.Builder{}
974 moduleInfoJSONBuf.WriteString("[")
975 for i, moduleInfoJSON := range moduleInfoJSONs {
976 if i != 0 {
977 moduleInfoJSONBuf.WriteString(",\n")
978 }
979 moduleInfoJSONBuf.WriteString("{")
980 moduleInfoJSONBuf.WriteString(strconv.Quote(moduleInfoJSON.core.RegisterName))
981 moduleInfoJSONBuf.WriteString(":")
982 err := encodeModuleInfoJSON(moduleInfoJSONBuf, moduleInfoJSON)
983 moduleInfoJSONBuf.WriteString("}")
984 if err != nil {
985 return err
986 }
987 }
988 moduleInfoJSONBuf.WriteString("]")
989 WriteFileRule(ctx, moduleInfoJSONPath, moduleInfoJSONBuf.String())
990 return nil
991}
992
Yu Liu14b81452025-02-18 23:26:13 +0000993func translateAndroidMkModule(ctx SingletonContext, w io.Writer, moduleInfoJSONs *[]*ModuleInfoJSON, mod Module) error {
Colin Cross953d3a22018-09-05 16:23:54 -0700994 defer func() {
995 if r := recover(); r != nil {
996 panic(fmt.Errorf("%s in translateAndroidMkModule for module %s variant %s",
997 r, ctx.ModuleName(mod), ctx.ModuleSubDir(mod)))
998 }
999 }()
1000
Bob Badourb4999222021-01-07 03:34:31 +00001001 // Additional cases here require review for correct license propagation to make.
Colin Crossd6fd0132023-11-06 13:54:06 -08001002 var err error
mrziwang18420972024-09-03 15:12:51 -07001003
Yu Liue70976d2024-10-15 20:45:35 +00001004 if info, ok := OtherModuleProvider(ctx, mod, AndroidMkInfoProvider); ok {
1005 err = translateAndroidMkEntriesInfoModule(ctx, w, moduleInfoJSONs, mod, info)
mrziwang18420972024-09-03 15:12:51 -07001006 } else {
1007 switch x := mod.(type) {
1008 case AndroidMkDataProvider:
1009 err = translateAndroidModule(ctx, w, moduleInfoJSONs, mod, x)
mrziwang18420972024-09-03 15:12:51 -07001010 case AndroidMkEntriesProvider:
1011 err = translateAndroidMkEntriesModule(ctx, w, moduleInfoJSONs, mod, x)
1012 default:
1013 // Not exported to make so no make variables to set.
1014 }
Dan Willemsen218f6562015-07-08 18:13:11 -07001015 }
Colin Crossd6fd0132023-11-06 13:54:06 -08001016
1017 if err != nil {
1018 return err
1019 }
1020
1021 return err
Colin Cross2465c3d2018-09-28 10:19:18 -07001022}
1023
Yu Liu14b81452025-02-18 23:26:13 +00001024func (data *AndroidMkData) fillInData(ctx fillInEntriesContext, mod Module) {
Jooyung Han12df5fb2019-07-11 16:18:47 +09001025 // Get the preamble content through AndroidMkEntries logic.
Jooyung Han2ed99d02020-06-24 23:26:26 +09001026 data.Entries = AndroidMkEntries{
Jooyung Han12df5fb2019-07-11 16:18:47 +09001027 Class: data.Class,
1028 SubName: data.SubName,
Jooyung Han12df5fb2019-07-11 16:18:47 +09001029 OutputFile: data.OutputFile,
1030 Disabled: data.Disabled,
1031 Include: data.Include,
1032 Required: data.Required,
1033 Host_required: data.Host_required,
1034 Target_required: data.Target_required,
1035 }
Colin Crossaa255532020-07-03 13:18:24 -07001036 data.Entries.fillInEntries(ctx, mod)
Jooyung Han12df5fb2019-07-11 16:18:47 +09001037
1038 // copy entries back to data since it is used in Custom
Jooyung Han2ed99d02020-06-24 23:26:26 +09001039 data.Required = data.Entries.Required
1040 data.Host_required = data.Entries.Host_required
1041 data.Target_required = data.Entries.Target_required
Jooyung Han12df5fb2019-07-11 16:18:47 +09001042}
1043
Jaewoong Jung7ef4a902020-11-16 12:50:29 -08001044// A support func for the deprecated AndroidMkDataProvider interface. Use AndroidMkEntryProvider
1045// instead.
Colin Crossd6fd0132023-11-06 13:54:06 -08001046func translateAndroidModule(ctx SingletonContext, w io.Writer, moduleInfoJSONs *[]*ModuleInfoJSON,
Yu Liu14b81452025-02-18 23:26:13 +00001047 mod Module, provider AndroidMkDataProvider) error {
Dan Willemsen218f6562015-07-08 18:13:11 -07001048
Yu Liu14b81452025-02-18 23:26:13 +00001049 amod := mod.base()
Cole Fausta963b942024-04-11 17:43:00 -07001050 if shouldSkipAndroidMkProcessing(ctx, amod) {
Jeff Gaston088e29e2017-11-29 16:47:17 -08001051 return nil
1052 }
1053
Colin Cross91825d22017-08-10 16:59:47 -07001054 data := provider.AndroidMk()
Yu Liuddc28332024-08-09 22:48:30 +00001055
Colin Cross53499412017-09-07 13:20:25 -07001056 if data.Include == "" {
1057 data.Include = "$(BUILD_PREBUILT)"
1058 }
1059
Colin Crossaa255532020-07-03 13:18:24 -07001060 data.fillInData(ctx, mod)
Yu Liu14b81452025-02-18 23:26:13 +00001061 aconfigUpdateAndroidMkData(ctx, mod, &data)
Dan Willemsen01a405a2016-06-13 17:19:03 -07001062
Colin Cross0f86d182017-08-10 17:07:28 -07001063 prefix := ""
1064 if amod.ArchSpecific() {
1065 switch amod.Os().Class {
1066 case Host:
Jiyong Park1613e552020-09-14 19:43:17 +09001067 if amod.Target().HostCross {
1068 prefix = "HOST_CROSS_"
1069 } else {
1070 prefix = "HOST_"
1071 }
Colin Cross0f86d182017-08-10 17:07:28 -07001072 case Device:
1073 prefix = "TARGET_"
Colin Crossa2344662016-03-24 13:14:12 -07001074
Dan Willemsen218f6562015-07-08 18:13:11 -07001075 }
1076
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001077 if amod.Arch().ArchType != ctx.Config().Targets[amod.Os()][0].Arch.ArchType {
Colin Cross0f86d182017-08-10 17:07:28 -07001078 prefix = "2ND_" + prefix
1079 }
Dan Willemsen218f6562015-07-08 18:13:11 -07001080 }
1081
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -07001082 name := provider.BaseModuleName()
Colin Cross0f86d182017-08-10 17:07:28 -07001083 blueprintDir := filepath.Dir(ctx.BlueprintFile(mod))
1084
1085 if data.Custom != nil {
Bob Badourb4999222021-01-07 03:34:31 +00001086 // List of module types allowed to use .Custom(...)
1087 // Additions to the list require careful review for proper license handling.
Colin Crossaa255532020-07-03 13:18:24 -07001088 switch reflect.TypeOf(mod).String() { // ctx.ModuleType(mod) doesn't work: aidl_interface creates phony without type
Bob Badourb4999222021-01-07 03:34:31 +00001089 case "*aidl.aidlApi": // writes non-custom before adding .phony
1090 case "*aidl.aidlMapping": // writes non-custom before adding .phony
1091 case "*android.customModule": // appears in tests only
Dan Willemsen9fe14102021-07-13 21:52:04 -07001092 case "*android_sdk.sdkRepoHost": // doesn't go through base_rules
Bob Badourb4999222021-01-07 03:34:31 +00001093 case "*apex.apexBundle": // license properties written
1094 case "*bpf.bpf": // license properties written (both for module and objs)
Neill Kapron41efab72024-07-31 22:17:36 +00001095 case "*libbpf_prog.libbpfProg": // license properties written (both for module and objs)
Bob Badourb4999222021-01-07 03:34:31 +00001096 case "*genrule.Module": // writes non-custom before adding .phony
1097 case "*java.SystemModules": // doesn't go through base_rules
1098 case "*java.systemModulesImport": // doesn't go through base_rules
1099 case "*phony.phony": // license properties written
Nelson Lif3c70682023-12-20 02:37:52 +00001100 case "*phony.PhonyRule": // writes phony deps and acts like `.PHONY`
Bob Badourb4999222021-01-07 03:34:31 +00001101 case "*selinux.selinuxContextsModule": // license properties written
1102 case "*sysprop.syspropLibrary": // license properties written
Bill Yang3b3aac02024-09-05 09:22:09 +00001103 case "*vintf.vintfCompatibilityMatrixRule": // use case like phony
Bob Badourb4999222021-01-07 03:34:31 +00001104 default:
Bob Badour65ee90a2021-09-02 15:33:10 -07001105 if !ctx.Config().IsEnvFalse("ANDROID_REQUIRE_LICENSES") {
Bob Badourb4999222021-01-07 03:34:31 +00001106 return fmt.Errorf("custom make rules not allowed for %q (%q) module %q", ctx.ModuleType(mod), reflect.TypeOf(mod), ctx.ModuleName(mod))
1107 }
1108 }
Colin Cross0f86d182017-08-10 17:07:28 -07001109 data.Custom(w, name, prefix, blueprintDir, data)
1110 } else {
1111 WriteAndroidMkData(w, data)
1112 }
1113
Colin Crossd6fd0132023-11-06 13:54:06 -08001114 if !data.Entries.disabled() {
Yu Liu663e4502024-08-12 18:23:59 +00001115 if moduleInfoJSON, ok := OtherModuleProvider(ctx, mod, ModuleInfoJSONProvider); ok {
Jihoon Kangd4063812025-01-24 00:25:30 +00001116 *moduleInfoJSONs = append(*moduleInfoJSONs, moduleInfoJSON...)
Colin Crossd6fd0132023-11-06 13:54:06 -08001117 }
1118 }
1119
Colin Cross0f86d182017-08-10 17:07:28 -07001120 return nil
1121}
1122
Jaewoong Jung7ef4a902020-11-16 12:50:29 -08001123// A support func for the deprecated AndroidMkDataProvider interface. Use AndroidMkEntryProvider
1124// instead.
Colin Cross0f86d182017-08-10 17:07:28 -07001125func WriteAndroidMkData(w io.Writer, data AndroidMkData) {
Colin Crossd6fd0132023-11-06 13:54:06 -08001126 if data.Entries.disabled() {
Colin Cross0f86d182017-08-10 17:07:28 -07001127 return
1128 }
1129
Jooyung Han2ed99d02020-06-24 23:26:26 +09001130 // write preamble via Entries
1131 data.Entries.footer = bytes.Buffer{}
1132 data.Entries.write(w)
Colin Cross0f86d182017-08-10 17:07:28 -07001133
Colin Crossca860ac2016-01-04 14:34:37 -08001134 for _, extra := range data.Extra {
Colin Cross27a4b052017-08-10 16:32:23 -07001135 extra(w, data.OutputFile.Path())
Dan Willemsen97750522016-02-09 17:43:51 -08001136 }
1137
Colin Cross53499412017-09-07 13:20:25 -07001138 fmt.Fprintln(w, "include "+data.Include)
Dan Willemsen218f6562015-07-08 18:13:11 -07001139}
Sasha Smundakb6d23052019-04-01 18:37:36 -07001140
Colin Crossd6fd0132023-11-06 13:54:06 -08001141func translateAndroidMkEntriesModule(ctx SingletonContext, w io.Writer, moduleInfoJSONs *[]*ModuleInfoJSON,
Yu Liu14b81452025-02-18 23:26:13 +00001142 mod Module, provider AndroidMkEntriesProvider) error {
1143 if shouldSkipAndroidMkProcessing(ctx, mod.base()) {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -07001144 return nil
Sasha Smundakb6d23052019-04-01 18:37:36 -07001145 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -07001146
Colin Crossd6fd0132023-11-06 13:54:06 -08001147 entriesList := provider.AndroidMkEntries()
Yu Liu14b81452025-02-18 23:26:13 +00001148 aconfigUpdateAndroidMkEntries(ctx, mod, &entriesList)
Colin Crossd6fd0132023-11-06 13:54:06 -08001149
Jihoon Kangd4063812025-01-24 00:25:30 +00001150 moduleInfoJSON, providesModuleInfoJSON := OtherModuleProvider(ctx, mod, ModuleInfoJSONProvider)
1151
Bob Badourb4999222021-01-07 03:34:31 +00001152 // Any new or special cases here need review to verify correct propagation of license information.
Colin Crossd6fd0132023-11-06 13:54:06 -08001153 for _, entries := range entriesList {
Colin Crossaa255532020-07-03 13:18:24 -07001154 entries.fillInEntries(ctx, mod)
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001155 entries.write(w)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -07001156
Jihoon Kangd4063812025-01-24 00:25:30 +00001157 if providesModuleInfoJSON && !entries.disabled() {
1158 // append only the name matching moduleInfoJSON entry
1159 for _, m := range moduleInfoJSON {
1160 if m.RegisterNameOverride == entries.OverrideName && m.SubName == entries.SubName {
1161 *moduleInfoJSONs = append(*moduleInfoJSONs, m)
1162 }
1163 }
Colin Crossd6fd0132023-11-06 13:54:06 -08001164 }
1165 }
1166
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -07001167 return nil
1168}
1169
Cole Fauste8a87832024-09-11 11:35:46 -07001170func ShouldSkipAndroidMkProcessing(ctx ConfigurableEvaluatorContext, module Module) bool {
Cole Fausta963b942024-04-11 17:43:00 -07001171 return shouldSkipAndroidMkProcessing(ctx, module.base())
Chih-Hung Hsieh80783772021-10-11 16:46:56 -07001172}
1173
Cole Fauste8a87832024-09-11 11:35:46 -07001174func shouldSkipAndroidMkProcessing(ctx ConfigurableEvaluatorContext, module *ModuleBase) bool {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -07001175 if !module.commonProperties.NamespaceExportedToMake {
1176 // TODO(jeffrygaston) do we want to validate that there are no modules being
1177 // exported to Kati that depend on this module?
1178 return true
Sasha Smundakb6d23052019-04-01 18:37:36 -07001179 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -07001180
Dan Willemsendef7b5d2021-10-17 00:22:33 -07001181 // On Mac, only expose host darwin modules to Make, as that's all we claim to support.
1182 // In reality, some of them depend on device-built (Java) modules, so we can't disable all
1183 // device modules in Soong, but we can hide them from Make (and thus the build user interface)
1184 if runtime.GOOS == "darwin" && module.Os() != Darwin {
1185 return true
1186 }
1187
Dan Willemsen8528f4e2021-10-19 00:22:06 -07001188 // Only expose the primary Darwin target, as Make does not understand Darwin+Arm64
1189 if module.Os() == Darwin && module.Target().HostCross {
1190 return true
1191 }
1192
Cole Fausta963b942024-04-11 17:43:00 -07001193 return !module.Enabled(ctx) ||
Colin Crossa9c8c9f2020-12-16 10:20:23 -08001194 module.commonProperties.HideFromMake ||
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -07001195 // Make does not understand LinuxBionic
Colin Cross9a027be2022-06-24 18:45:58 -07001196 module.Os() == LinuxBionic ||
1197 // Make does not understand LinuxMusl, except when we are building with USE_HOST_MUSL=true
1198 // and all host binaries are LinuxMusl
1199 (module.Os() == LinuxMusl && module.Target().HostCross)
Sasha Smundakb6d23052019-04-01 18:37:36 -07001200}
Dan Shi31949122020-09-21 12:11:02 -07001201
Jaewoong Jung7ef4a902020-11-16 12:50:29 -08001202// A utility func to format LOCAL_TEST_DATA outputs. See the comments on DataPath to understand how
1203// to use this func.
Colin Cross5c1d5fb2023-11-15 12:39:40 -08001204func androidMkDataPaths(data []DataPath) []string {
Dan Shi31949122020-09-21 12:11:02 -07001205 var testFiles []string
1206 for _, d := range data {
1207 rel := d.SrcPath.Rel()
Colin Cross5c1d5fb2023-11-15 12:39:40 -08001208 if d.WithoutRel {
1209 rel = d.SrcPath.Base()
1210 }
Dan Shi31949122020-09-21 12:11:02 -07001211 path := d.SrcPath.String()
Jaewoong Jung7ef4a902020-11-16 12:50:29 -08001212 // LOCAL_TEST_DATA requires the rel portion of the path to be removed from the path.
Dan Shi31949122020-09-21 12:11:02 -07001213 if !strings.HasSuffix(path, rel) {
1214 panic(fmt.Errorf("path %q does not end with %q", path, rel))
1215 }
1216 path = strings.TrimSuffix(path, rel)
1217 testFileString := path + ":" + rel
1218 if len(d.RelativeInstallPath) > 0 {
1219 testFileString += ":" + d.RelativeInstallPath
1220 }
1221 testFiles = append(testFiles, testFileString)
1222 }
1223 return testFiles
1224}
Sasha Smundakdcb61292022-12-08 10:41:33 -08001225
1226// AndroidMkEmitAssignList emits the line
1227//
1228// VAR := ITEM ...
1229//
1230// Items are the elements to the given set of lists
1231// If all the passed lists are empty, no line will be emitted
1232func AndroidMkEmitAssignList(w io.Writer, varName string, lists ...[]string) {
1233 doPrint := false
1234 for _, l := range lists {
1235 if doPrint = len(l) > 0; doPrint {
1236 break
1237 }
1238 }
1239 if !doPrint {
1240 return
1241 }
1242 fmt.Fprint(w, varName, " :=")
1243 for _, l := range lists {
1244 for _, item := range l {
1245 fmt.Fprint(w, " ", item)
1246 }
1247 }
1248 fmt.Fprintln(w)
1249}
mrziwang18420972024-09-03 15:12:51 -07001250
1251type AndroidMkProviderInfo struct {
1252 PrimaryInfo AndroidMkInfo
1253 ExtraInfo []AndroidMkInfo
1254}
1255
1256type AndroidMkInfo struct {
1257 // Android.mk class string, e.g. EXECUTABLES, JAVA_LIBRARIES, ETC
1258 Class string
1259 // Optional suffix to append to the module name. Useful when a module wants to return multiple
1260 // AndroidMkEntries objects. For example, when a java_library returns an additional entry for
1261 // its hostdex sub-module, this SubName field is set to "-hostdex" so that it can have a
1262 // different name than the parent's.
1263 SubName string
1264 // If set, this value overrides the base module name. SubName is still appended.
1265 OverrideName string
mrziwang18420972024-09-03 15:12:51 -07001266 // The output file for Kati to process and/or install. If absent, the module is skipped.
1267 OutputFile OptionalPath
1268 // If true, the module is skipped and does not appear on the final Android-<product name>.mk
1269 // file. Useful when a module needs to be skipped conditionally.
1270 Disabled bool
1271 // The postprocessing mk file to include, e.g. $(BUILD_SYSTEM)/soong_cc_rust_prebuilt.mk
1272 // If not set, $(BUILD_SYSTEM)/prebuilt.mk is used.
1273 Include string
1274 // Required modules that need to be built and included in the final build output when building
1275 // this module.
1276 Required []string
1277 // Required host modules that need to be built and included in the final build output when
1278 // building this module.
1279 Host_required []string
1280 // Required device modules that need to be built and included in the final build output when
1281 // building this module.
1282 Target_required []string
1283
1284 HeaderStrings []string
1285 FooterStrings []string
1286
1287 // A map that holds the up-to-date Make variable values. Can be accessed from tests.
1288 EntryMap map[string][]string
1289 // A list of EntryMap keys in insertion order. This serves a few purposes:
1290 // 1. Prevents churns. Golang map doesn't provide consistent iteration order, so without this,
1291 // the outputted Android-*.mk file may change even though there have been no content changes.
1292 // 2. Allows modules to refer to other variables, like LOCAL_BAR_VAR := $(LOCAL_FOO_VAR),
1293 // without worrying about the variables being mixed up in the actual mk file.
1294 // 3. Makes troubleshooting and spotting errors easier.
1295 EntryOrder []string
1296}
1297
Yu Liue70976d2024-10-15 20:45:35 +00001298type AndroidMkProviderInfoProducer interface {
1299 PrepareAndroidMKProviderInfo(config Config) *AndroidMkProviderInfo
1300}
1301
mrziwang18420972024-09-03 15:12:51 -07001302// TODO: rename it to AndroidMkEntriesProvider after AndroidMkEntriesProvider interface is gone.
1303var AndroidMkInfoProvider = blueprint.NewProvider[*AndroidMkProviderInfo]()
1304
Yu Liu64371e02025-02-19 23:44:48 +00001305// TODO(b/397766191): Change the signature to take ModuleProxy
1306// Please only access the module's internal data through providers.
mrziwang18420972024-09-03 15:12:51 -07001307func translateAndroidMkEntriesInfoModule(ctx SingletonContext, w io.Writer, moduleInfoJSONs *[]*ModuleInfoJSON,
Yu Liu14b81452025-02-18 23:26:13 +00001308 mod Module, providerInfo *AndroidMkProviderInfo) error {
Yu Liu64371e02025-02-19 23:44:48 +00001309 commonInfo, _ := OtherModuleProvider(ctx, mod, CommonModuleInfoKey)
1310 if commonInfo.SkipAndroidMkProcessing {
mrziwang18420972024-09-03 15:12:51 -07001311 return nil
1312 }
1313
1314 // Deep copy the provider info since we need to modify the info later
1315 info := deepCopyAndroidMkProviderInfo(providerInfo)
1316
Yu Liu14b81452025-02-18 23:26:13 +00001317 aconfigUpdateAndroidMkInfos(ctx, mod, &info)
mrziwang18420972024-09-03 15:12:51 -07001318
1319 // Any new or special cases here need review to verify correct propagation of license information.
Yu Liu64371e02025-02-19 23:44:48 +00001320 info.PrimaryInfo.fillInEntries(ctx, mod, &commonInfo)
mrziwang18420972024-09-03 15:12:51 -07001321 info.PrimaryInfo.write(w)
1322 if len(info.ExtraInfo) > 0 {
1323 for _, ei := range info.ExtraInfo {
Yu Liu64371e02025-02-19 23:44:48 +00001324 ei.fillInEntries(ctx, mod, &commonInfo)
mrziwang18420972024-09-03 15:12:51 -07001325 ei.write(w)
1326 }
1327 }
1328
1329 if !info.PrimaryInfo.disabled() {
1330 if moduleInfoJSON, ok := OtherModuleProvider(ctx, mod, ModuleInfoJSONProvider); ok {
Jihoon Kangd4063812025-01-24 00:25:30 +00001331 *moduleInfoJSONs = append(*moduleInfoJSONs, moduleInfoJSON...)
mrziwang18420972024-09-03 15:12:51 -07001332 }
1333 }
1334
1335 return nil
1336}
1337
1338// Utility funcs to manipulate Android.mk variable entries.
1339
1340// SetString sets a Make variable with the given name to the given value.
1341func (a *AndroidMkInfo) SetString(name, value string) {
1342 if _, ok := a.EntryMap[name]; !ok {
1343 a.EntryOrder = append(a.EntryOrder, name)
1344 }
1345 a.EntryMap[name] = []string{value}
1346}
1347
1348// SetPath sets a Make variable with the given name to the given path string.
1349func (a *AndroidMkInfo) SetPath(name string, path Path) {
1350 if _, ok := a.EntryMap[name]; !ok {
1351 a.EntryOrder = append(a.EntryOrder, name)
1352 }
1353 a.EntryMap[name] = []string{path.String()}
1354}
1355
1356// SetOptionalPath sets a Make variable with the given name to the given path string if it is valid.
1357// It is a no-op if the given path is invalid.
1358func (a *AndroidMkInfo) SetOptionalPath(name string, path OptionalPath) {
1359 if path.Valid() {
1360 a.SetPath(name, path.Path())
1361 }
1362}
1363
1364// AddPath appends the given path string to a Make variable with the given name.
1365func (a *AndroidMkInfo) AddPath(name string, path Path) {
1366 if _, ok := a.EntryMap[name]; !ok {
1367 a.EntryOrder = append(a.EntryOrder, name)
1368 }
1369 a.EntryMap[name] = append(a.EntryMap[name], path.String())
1370}
1371
1372// AddOptionalPath appends the given path string to a Make variable with the given name if it is
1373// valid. It is a no-op if the given path is invalid.
1374func (a *AndroidMkInfo) AddOptionalPath(name string, path OptionalPath) {
1375 if path.Valid() {
1376 a.AddPath(name, path.Path())
1377 }
1378}
1379
1380// SetPaths sets a Make variable with the given name to a slice of the given path strings.
1381func (a *AndroidMkInfo) SetPaths(name string, paths Paths) {
1382 if _, ok := a.EntryMap[name]; !ok {
1383 a.EntryOrder = append(a.EntryOrder, name)
1384 }
1385 a.EntryMap[name] = paths.Strings()
1386}
1387
1388// SetOptionalPaths sets a Make variable with the given name to a slice of the given path strings
1389// only if there are a non-zero amount of paths.
1390func (a *AndroidMkInfo) SetOptionalPaths(name string, paths Paths) {
1391 if len(paths) > 0 {
1392 a.SetPaths(name, paths)
1393 }
1394}
1395
1396// AddPaths appends the given path strings to a Make variable with the given name.
1397func (a *AndroidMkInfo) AddPaths(name string, paths Paths) {
1398 if _, ok := a.EntryMap[name]; !ok {
1399 a.EntryOrder = append(a.EntryOrder, name)
1400 }
1401 a.EntryMap[name] = append(a.EntryMap[name], paths.Strings()...)
1402}
1403
1404// SetBoolIfTrue sets a Make variable with the given name to true if the given flag is true.
1405// It is a no-op if the given flag is false.
1406func (a *AndroidMkInfo) SetBoolIfTrue(name string, flag bool) {
1407 if flag {
1408 if _, ok := a.EntryMap[name]; !ok {
1409 a.EntryOrder = append(a.EntryOrder, name)
1410 }
1411 a.EntryMap[name] = []string{"true"}
1412 }
1413}
1414
1415// SetBool sets a Make variable with the given name to if the given bool flag value.
1416func (a *AndroidMkInfo) SetBool(name string, flag bool) {
1417 if _, ok := a.EntryMap[name]; !ok {
1418 a.EntryOrder = append(a.EntryOrder, name)
1419 }
1420 if flag {
1421 a.EntryMap[name] = []string{"true"}
1422 } else {
1423 a.EntryMap[name] = []string{"false"}
1424 }
1425}
1426
1427// AddStrings appends the given strings to a Make variable with the given name.
1428func (a *AndroidMkInfo) AddStrings(name string, value ...string) {
1429 if len(value) == 0 {
1430 return
1431 }
1432 if _, ok := a.EntryMap[name]; !ok {
1433 a.EntryOrder = append(a.EntryOrder, name)
1434 }
1435 a.EntryMap[name] = append(a.EntryMap[name], value...)
1436}
1437
1438// AddCompatibilityTestSuites adds the supplied test suites to the EntryMap, with special handling
1439// for partial MTS and MCTS test suites.
1440func (a *AndroidMkInfo) AddCompatibilityTestSuites(suites ...string) {
1441 // M(C)TS supports a full test suite and partial per-module MTS test suites, with naming mts-${MODULE}.
1442 // To reduce repetition, if we find a partial M(C)TS test suite without an full M(C)TS test suite,
1443 // we add the full test suite to our list.
1444 if PrefixInList(suites, "mts-") && !InList("mts", suites) {
1445 suites = append(suites, "mts")
1446 }
1447 if PrefixInList(suites, "mcts-") && !InList("mcts", suites) {
1448 suites = append(suites, "mcts")
1449 }
1450 a.AddStrings("LOCAL_COMPATIBILITY_SUITE", suites...)
1451}
1452
Yu Liu64371e02025-02-19 23:44:48 +00001453// TODO(b/397766191): Change the signature to take ModuleProxy
1454// Please only access the module's internal data through providers.
1455func (a *AndroidMkInfo) fillInEntries(ctx fillInEntriesContext, mod Module, commonInfo *CommonModuleInfo) {
mrziwang18420972024-09-03 15:12:51 -07001456 helperInfo := AndroidMkInfo{
1457 EntryMap: make(map[string][]string),
1458 }
1459
Yu Liu64371e02025-02-19 23:44:48 +00001460 name := commonInfo.BaseModuleName
mrziwang18420972024-09-03 15:12:51 -07001461 if a.OverrideName != "" {
1462 name = a.OverrideName
1463 }
1464
1465 if a.Include == "" {
1466 a.Include = "$(BUILD_PREBUILT)"
1467 }
Yu Liu64371e02025-02-19 23:44:48 +00001468 a.Required = append(a.Required, commonInfo.RequiredModuleNames...)
1469 a.Required = append(a.Required, commonInfo.VintfFragmentModuleNames...)
1470 a.Host_required = append(a.Host_required, commonInfo.HostRequiredModuleNames...)
1471 a.Target_required = append(a.Target_required, commonInfo.TargetRequiredModuleNames...)
mrziwang18420972024-09-03 15:12:51 -07001472
Yu Liu64371e02025-02-19 23:44:48 +00001473 for _, distString := range a.GetDistForGoals(ctx, mod, commonInfo) {
mrziwang18420972024-09-03 15:12:51 -07001474 a.HeaderStrings = append(a.HeaderStrings, distString)
1475 }
1476
Yu Liu64371e02025-02-19 23:44:48 +00001477 a.HeaderStrings = append(a.HeaderStrings, fmt.Sprintf("\ninclude $(CLEAR_VARS) # type: %s, name: %s, variant: %s", ctx.ModuleType(mod), commonInfo.BaseModuleName, ctx.ModuleSubDir(mod)))
mrziwang18420972024-09-03 15:12:51 -07001478
1479 // Collect make variable assignment entries.
1480 helperInfo.SetString("LOCAL_PATH", ctx.ModuleDir(mod))
1481 helperInfo.SetString("LOCAL_MODULE", name+a.SubName)
1482 helperInfo.SetString("LOCAL_MODULE_CLASS", a.Class)
1483 helperInfo.SetString("LOCAL_PREBUILT_MODULE_FILE", a.OutputFile.String())
1484 helperInfo.AddStrings("LOCAL_REQUIRED_MODULES", a.Required...)
1485 helperInfo.AddStrings("LOCAL_HOST_REQUIRED_MODULES", a.Host_required...)
1486 helperInfo.AddStrings("LOCAL_TARGET_REQUIRED_MODULES", a.Target_required...)
Yu Liu14b81452025-02-18 23:26:13 +00001487 helperInfo.AddStrings("LOCAL_SOONG_MODULE_TYPE", ctx.ModuleType(mod))
mrziwang18420972024-09-03 15:12:51 -07001488
1489 // If the install rule was generated by Soong tell Make about it.
1490 info := OtherModuleProviderOrDefault(ctx, mod, InstallFilesProvider)
1491 if len(info.KatiInstalls) > 0 {
1492 // Assume the primary install file is last since it probably needs to depend on any other
1493 // installed files. If that is not the case we can add a method to specify the primary
1494 // installed file.
1495 helperInfo.SetPath("LOCAL_SOONG_INSTALLED_MODULE", info.KatiInstalls[len(info.KatiInstalls)-1].to)
1496 helperInfo.SetString("LOCAL_SOONG_INSTALL_PAIRS", info.KatiInstalls.BuiltInstalled())
1497 helperInfo.SetPaths("LOCAL_SOONG_INSTALL_SYMLINKS", info.KatiSymlinks.InstallPaths().Paths())
1498 } else {
1499 // Soong may not have generated the install rule also when `no_full_install: true`.
1500 // Mark this module as uninstallable in order to prevent Make from creating an
1501 // install rule there.
Yu Liu64371e02025-02-19 23:44:48 +00001502 helperInfo.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", commonInfo.NoFullInstall)
mrziwang18420972024-09-03 15:12:51 -07001503 }
1504
Yu Liue70976d2024-10-15 20:45:35 +00001505 if info.UncheckedModule {
1506 helperInfo.SetBool("LOCAL_DONT_CHECK_MODULE", true)
1507 } else if info.CheckbuildTarget != nil {
1508 helperInfo.SetPath("LOCAL_CHECKED_MODULE", info.CheckbuildTarget)
1509 } else {
1510 helperInfo.SetOptionalPath("LOCAL_CHECKED_MODULE", a.OutputFile)
1511 }
1512
mrziwang18420972024-09-03 15:12:51 -07001513 if len(info.TestData) > 0 {
1514 helperInfo.AddStrings("LOCAL_TEST_DATA", androidMkDataPaths(info.TestData)...)
1515 }
1516
Yu Liu64371e02025-02-19 23:44:48 +00001517 if commonInfo.IsApexModule {
1518 helperInfo.SetBoolIfTrue("LOCAL_NOT_AVAILABLE_FOR_PLATFORM", commonInfo.NotAvailableForPlatform)
mrziwang18420972024-09-03 15:12:51 -07001519 }
1520
Yu Liu64371e02025-02-19 23:44:48 +00001521 archStr := commonInfo.Target.Arch.ArchType.String()
mrziwang18420972024-09-03 15:12:51 -07001522 host := false
Yu Liu64371e02025-02-19 23:44:48 +00001523 switch commonInfo.Target.Os.Class {
mrziwang18420972024-09-03 15:12:51 -07001524 case Host:
Yu Liu64371e02025-02-19 23:44:48 +00001525 if commonInfo.Target.HostCross {
mrziwang18420972024-09-03 15:12:51 -07001526 // Make cannot identify LOCAL_MODULE_HOST_CROSS_ARCH:= common.
Yu Liu64371e02025-02-19 23:44:48 +00001527 if commonInfo.Target.Arch.ArchType != Common {
mrziwang18420972024-09-03 15:12:51 -07001528 helperInfo.SetString("LOCAL_MODULE_HOST_CROSS_ARCH", archStr)
1529 }
1530 } else {
1531 // Make cannot identify LOCAL_MODULE_HOST_ARCH:= common.
Yu Liu64371e02025-02-19 23:44:48 +00001532 if commonInfo.Target.Arch.ArchType != Common {
mrziwang18420972024-09-03 15:12:51 -07001533 helperInfo.SetString("LOCAL_MODULE_HOST_ARCH", archStr)
1534 }
1535 }
1536 host = true
1537 case Device:
1538 // Make cannot identify LOCAL_MODULE_TARGET_ARCH:= common.
Yu Liu64371e02025-02-19 23:44:48 +00001539 if commonInfo.Target.Arch.ArchType != Common {
1540 if commonInfo.Target.NativeBridge {
1541 hostArchStr := commonInfo.Target.NativeBridgeHostArchName
mrziwang18420972024-09-03 15:12:51 -07001542 if hostArchStr != "" {
1543 helperInfo.SetString("LOCAL_MODULE_TARGET_ARCH", hostArchStr)
1544 }
1545 } else {
1546 helperInfo.SetString("LOCAL_MODULE_TARGET_ARCH", archStr)
1547 }
1548 }
1549
Yu Liu64371e02025-02-19 23:44:48 +00001550 if !commonInfo.InVendorRamdisk {
mrziwang18420972024-09-03 15:12:51 -07001551 helperInfo.AddPaths("LOCAL_FULL_INIT_RC", info.InitRcPaths)
1552 }
1553 if len(info.VintfFragmentsPaths) > 0 {
1554 helperInfo.AddPaths("LOCAL_FULL_VINTF_FRAGMENTS", info.VintfFragmentsPaths)
1555 }
Yu Liu64371e02025-02-19 23:44:48 +00001556 helperInfo.SetBoolIfTrue("LOCAL_PROPRIETARY_MODULE", commonInfo.Proprietary)
1557 if commonInfo.Vendor || commonInfo.SocSpecific {
mrziwang18420972024-09-03 15:12:51 -07001558 helperInfo.SetString("LOCAL_VENDOR_MODULE", "true")
1559 }
Yu Liu64371e02025-02-19 23:44:48 +00001560 helperInfo.SetBoolIfTrue("LOCAL_ODM_MODULE", commonInfo.DeviceSpecific)
1561 helperInfo.SetBoolIfTrue("LOCAL_PRODUCT_MODULE", commonInfo.ProductSpecific)
1562 helperInfo.SetBoolIfTrue("LOCAL_SYSTEM_EXT_MODULE", commonInfo.SystemExtSpecific)
1563 if commonInfo.Owner != "" {
1564 helperInfo.SetString("LOCAL_MODULE_OWNER", commonInfo.Owner)
mrziwang18420972024-09-03 15:12:51 -07001565 }
1566 }
1567
1568 if host {
Yu Liu64371e02025-02-19 23:44:48 +00001569 os := commonInfo.Target.Os
1570 makeOs := os.String()
1571 if os == Linux || os == LinuxBionic || os == LinuxMusl {
mrziwang18420972024-09-03 15:12:51 -07001572 makeOs = "linux"
1573 }
1574 helperInfo.SetString("LOCAL_MODULE_HOST_OS", makeOs)
1575 helperInfo.SetString("LOCAL_IS_HOST_MODULE", "true")
1576 }
1577
mrziwang18420972024-09-03 15:12:51 -07001578 if licenseMetadata, ok := OtherModuleProvider(ctx, mod, LicenseMetadataProvider); ok {
1579 helperInfo.SetPath("LOCAL_SOONG_LICENSE_METADATA", licenseMetadata.LicenseMetadataPath)
1580 }
1581
1582 if _, ok := OtherModuleProvider(ctx, mod, ModuleInfoJSONProvider); ok {
1583 helperInfo.SetBool("LOCAL_SOONG_MODULE_INFO_JSON", true)
1584 }
1585
1586 a.mergeEntries(&helperInfo)
1587
1588 // Write to footer.
1589 a.FooterStrings = append([]string{"include " + a.Include}, a.FooterStrings...)
1590}
1591
1592// This method merges the entries to helperInfo, then replaces a's EntryMap and
1593// EntryOrder with helperInfo's
1594func (a *AndroidMkInfo) mergeEntries(helperInfo *AndroidMkInfo) {
1595 for _, extraEntry := range a.EntryOrder {
1596 if v, ok := helperInfo.EntryMap[extraEntry]; ok {
1597 v = append(v, a.EntryMap[extraEntry]...)
1598 } else {
1599 helperInfo.EntryMap[extraEntry] = a.EntryMap[extraEntry]
1600 helperInfo.EntryOrder = append(helperInfo.EntryOrder, extraEntry)
1601 }
1602 }
1603 a.EntryOrder = helperInfo.EntryOrder
1604 a.EntryMap = helperInfo.EntryMap
1605}
1606
1607func (a *AndroidMkInfo) disabled() bool {
1608 return a.Disabled || !a.OutputFile.Valid()
1609}
1610
1611// write flushes the AndroidMkEntries's in-struct data populated by AndroidMkEntries into the
1612// given Writer object.
1613func (a *AndroidMkInfo) write(w io.Writer) {
1614 if a.disabled() {
1615 return
1616 }
1617
Yu Liue70976d2024-10-15 20:45:35 +00001618 combinedHeaderString := strings.Join(a.HeaderStrings, "\n") + "\n"
1619 combinedFooterString := strings.Join(a.FooterStrings, "\n") + "\n"
mrziwang18420972024-09-03 15:12:51 -07001620 w.Write([]byte(combinedHeaderString))
1621 for _, name := range a.EntryOrder {
1622 AndroidMkEmitAssignList(w, name, a.EntryMap[name])
1623 }
1624 w.Write([]byte(combinedFooterString))
1625}
1626
1627// Compute the list of Make strings to declare phony goals and dist-for-goals
1628// calls from the module's dist and dists properties.
Yu Liu64371e02025-02-19 23:44:48 +00001629// TODO(b/397766191): Change the signature to take ModuleProxy
1630// Please only access the module's internal data through providers.
1631func (a *AndroidMkInfo) GetDistForGoals(ctx fillInEntriesContext, mod Module, commonInfo *CommonModuleInfo) []string {
Cole Fausta8437c52025-02-25 14:45:43 -08001632 distContributions := getDistContributions(ctx, mod)
mrziwang18420972024-09-03 15:12:51 -07001633 if distContributions == nil {
1634 return nil
1635 }
1636
1637 return generateDistContributionsForMake(distContributions)
1638}
1639
mrziwang18420972024-09-03 15:12:51 -07001640func deepCopyAndroidMkProviderInfo(providerInfo *AndroidMkProviderInfo) AndroidMkProviderInfo {
1641 info := AndroidMkProviderInfo{
1642 PrimaryInfo: deepCopyAndroidMkInfo(&providerInfo.PrimaryInfo),
1643 }
1644 if len(providerInfo.ExtraInfo) > 0 {
1645 for _, i := range providerInfo.ExtraInfo {
1646 info.ExtraInfo = append(info.ExtraInfo, deepCopyAndroidMkInfo(&i))
1647 }
1648 }
1649 return info
1650}
1651
1652func deepCopyAndroidMkInfo(mkinfo *AndroidMkInfo) AndroidMkInfo {
1653 info := AndroidMkInfo{
1654 Class: mkinfo.Class,
1655 SubName: mkinfo.SubName,
1656 OverrideName: mkinfo.OverrideName,
Cole Faustd143f3e2025-02-24 16:18:18 -08001657 // There is no modification on OutputFile, so no need to
mrziwang18420972024-09-03 15:12:51 -07001658 // make their deep copy.
mrziwang18420972024-09-03 15:12:51 -07001659 OutputFile: mkinfo.OutputFile,
1660 Disabled: mkinfo.Disabled,
1661 Include: mkinfo.Include,
1662 Required: deepCopyStringSlice(mkinfo.Required),
1663 Host_required: deepCopyStringSlice(mkinfo.Host_required),
1664 Target_required: deepCopyStringSlice(mkinfo.Target_required),
1665 HeaderStrings: deepCopyStringSlice(mkinfo.HeaderStrings),
1666 FooterStrings: deepCopyStringSlice(mkinfo.FooterStrings),
1667 EntryOrder: deepCopyStringSlice(mkinfo.EntryOrder),
1668 }
1669 info.EntryMap = make(map[string][]string)
1670 for k, v := range mkinfo.EntryMap {
1671 info.EntryMap[k] = deepCopyStringSlice(v)
1672 }
1673
1674 return info
1675}
1676
1677func deepCopyStringSlice(original []string) []string {
1678 result := make([]string, len(original))
1679 copy(result, original)
1680 return result
1681}