blob: 5adc849827667f3936f59607249664c06b9dfcb6 [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
Yu Liu71f1ea32025-02-26 23:39:20 +000081type AndroidMkDataInfo struct {
82 Class string
83}
84
85var AndroidMkDataInfoProvider = blueprint.NewProvider[AndroidMkDataInfo]()
86
Colin Cross27a4b052017-08-10 16:32:23 -070087type AndroidMkExtraFunc func(w io.Writer, outputFile Path)
88
Jaewoong Jung7ef4a902020-11-16 12:50:29 -080089// Interface for modules to declare their Android.mk outputs. Note that every module needs to
90// implement this in order to be included in the final Android-<product_name>.mk output, even if
91// they only need to output the common set of entries without any customizations.
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -070092type AndroidMkEntriesProvider interface {
Jaewoong Jung7ef4a902020-11-16 12:50:29 -080093 // Returns AndroidMkEntries objects that contain all basic info plus extra customization data
94 // if needed. This is the core func to implement.
95 // Note that one can return multiple objects. For example, java_library may return an additional
96 // AndroidMkEntries object for its hostdex sub-module.
Jiyong Park0b0e1b92019-12-03 13:24:29 +090097 AndroidMkEntries() []AndroidMkEntries
Jaewoong Jung7ef4a902020-11-16 12:50:29 -080098 // Modules don't need to implement this as it's already implemented by ModuleBase.
99 // AndroidMkEntries uses BaseModuleName() instead of ModuleName() because certain modules
100 // e.g. Prebuilts, override the Name() func and return modified names.
101 // If a different name is preferred, use SubName or OverrideName in AndroidMkEntries.
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700102 BaseModuleName() string
103}
104
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800105// The core data struct that modules use to provide their Android.mk data.
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700106type AndroidMkEntries struct {
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800107 // Android.mk class string, e.g EXECUTABLES, JAVA_LIBRARIES, ETC
108 Class string
109 // Optional suffix to append to the module name. Useful when a module wants to return multiple
110 // AndroidMkEntries objects. For example, when a java_library returns an additional entry for
111 // its hostdex sub-module, this SubName field is set to "-hostdex" so that it can have a
112 // different name than the parent's.
113 SubName string
114 // If set, this value overrides the base module name. SubName is still appended.
115 OverrideName string
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800116 // The output file for Kati to process and/or install. If absent, the module is skipped.
117 OutputFile OptionalPath
118 // If true, the module is skipped and does not appear on the final Android-<product name>.mk
119 // file. Useful when a module needs to be skipped conditionally.
120 Disabled bool
Ivan Lozanod06cc742021-11-12 13:27:58 -0500121 // The postprocessing mk file to include, e.g. $(BUILD_SYSTEM)/soong_cc_rust_prebuilt.mk
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800122 // If not set, $(BUILD_SYSTEM)/prebuilt.mk is used.
123 Include string
124 // Required modules that need to be built and included in the final build output when building
125 // this module.
126 Required []string
127 // Required host modules that need to be built and included in the final build output when
128 // building this module.
129 Host_required []string
130 // Required device modules that need to be built and included in the final build output when
131 // building this module.
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700132 Target_required []string
133
134 header bytes.Buffer
135 footer bytes.Buffer
136
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800137 // Funcs to append additional Android.mk entries or modify the common ones. Multiple funcs are
138 // accepted so that common logic can be factored out as a shared func.
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700139 ExtraEntries []AndroidMkExtraEntriesFunc
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800140 // Funcs to add extra lines to the module's Android.mk output. Unlike AndroidMkExtraEntriesFunc,
141 // which simply sets Make variable values, this can be used for anything since it can write any
142 // Make statements directly to the final Android-*.mk file.
143 // Primarily used to call macros or declare/update Make targets.
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700144 ExtraFooters []AndroidMkExtraFootersFunc
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700145
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800146 // A map that holds the up-to-date Make variable values. Can be accessed from tests.
147 EntryMap map[string][]string
148 // A list of EntryMap keys in insertion order. This serves a few purposes:
149 // 1. Prevents churns. Golang map doesn't provide consistent iteration order, so without this,
150 // the outputted Android-*.mk file may change even though there have been no content changes.
151 // 2. Allows modules to refer to other variables, like LOCAL_BAR_VAR := $(LOCAL_FOO_VAR),
152 // without worrying about the variables being mixed up in the actual mk file.
153 // 3. Makes troubleshooting and spotting errors easier.
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700154 entryOrder []string
Trevor Radcliffe90727f42022-03-21 19:34:02 +0000155
156 // Provides data typically stored by Context objects that are commonly needed by
157 //AndroidMkEntries objects.
158 entryContext AndroidMkEntriesContext
159}
160
161type AndroidMkEntriesContext interface {
Yu Liuec810542024-08-26 18:09:15 +0000162 OtherModuleProviderContext
Trevor Radcliffe90727f42022-03-21 19:34:02 +0000163 Config() Config
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700164}
165
Colin Crossaa255532020-07-03 13:18:24 -0700166type AndroidMkExtraEntriesContext interface {
Colin Cross3c0a83d2023-12-12 14:13:26 -0800167 Provider(provider blueprint.AnyProviderKey) (any, bool)
Colin Crossaa255532020-07-03 13:18:24 -0700168}
169
170type androidMkExtraEntriesContext struct {
171 ctx fillInEntriesContext
Yu Liu14b81452025-02-18 23:26:13 +0000172 mod Module
Colin Crossaa255532020-07-03 13:18:24 -0700173}
174
Colin Cross3c0a83d2023-12-12 14:13:26 -0800175func (a *androidMkExtraEntriesContext) Provider(provider blueprint.AnyProviderKey) (any, bool) {
Yu Liu663e4502024-08-12 18:23:59 +0000176 return a.ctx.otherModuleProvider(a.mod, provider)
Colin Crossaa255532020-07-03 13:18:24 -0700177}
178
179type AndroidMkExtraEntriesFunc func(ctx AndroidMkExtraEntriesContext, entries *AndroidMkEntries)
Jaewoong Jung02b11a62020-12-07 10:23:54 -0800180type AndroidMkExtraFootersFunc func(w io.Writer, name, prefix, moduleDir string)
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700181
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800182// Utility funcs to manipulate Android.mk variable entries.
183
184// SetString sets a Make variable with the given name to the given value.
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700185func (a *AndroidMkEntries) SetString(name, value string) {
186 if _, ok := a.EntryMap[name]; !ok {
187 a.entryOrder = append(a.entryOrder, name)
188 }
189 a.EntryMap[name] = []string{value}
190}
191
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800192// SetPath sets a Make variable with the given name to the given path string.
Jaewoong Jung9a1e8bd2019-09-04 20:17:54 -0700193func (a *AndroidMkEntries) SetPath(name string, path Path) {
194 if _, ok := a.EntryMap[name]; !ok {
195 a.entryOrder = append(a.entryOrder, name)
196 }
197 a.EntryMap[name] = []string{path.String()}
198}
199
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800200// SetOptionalPath sets a Make variable with the given name to the given path string if it is valid.
201// It is a no-op if the given path is invalid.
Colin Crossc0efd1d2020-07-03 11:56:24 -0700202func (a *AndroidMkEntries) SetOptionalPath(name string, path OptionalPath) {
203 if path.Valid() {
204 a.SetPath(name, path.Path())
205 }
206}
207
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800208// AddPath appends the given path string to a Make variable with the given name.
Colin Crossc0efd1d2020-07-03 11:56:24 -0700209func (a *AndroidMkEntries) AddPath(name string, path Path) {
210 if _, ok := a.EntryMap[name]; !ok {
211 a.entryOrder = append(a.entryOrder, name)
212 }
213 a.EntryMap[name] = append(a.EntryMap[name], path.String())
214}
215
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800216// AddOptionalPath appends the given path string to a Make variable with the given name if it is
217// valid. It is a no-op if the given path is invalid.
Colin Crossc0efd1d2020-07-03 11:56:24 -0700218func (a *AndroidMkEntries) AddOptionalPath(name string, path OptionalPath) {
219 if path.Valid() {
220 a.AddPath(name, path.Path())
221 }
222}
223
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800224// SetPaths sets a Make variable with the given name to a slice of the given path strings.
Colin Cross08dca382020-07-21 20:31:17 -0700225func (a *AndroidMkEntries) SetPaths(name string, paths Paths) {
226 if _, ok := a.EntryMap[name]; !ok {
227 a.entryOrder = append(a.entryOrder, name)
228 }
229 a.EntryMap[name] = paths.Strings()
230}
231
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800232// SetOptionalPaths sets a Make variable with the given name to a slice of the given path strings
233// only if there are a non-zero amount of paths.
Colin Cross08dca382020-07-21 20:31:17 -0700234func (a *AndroidMkEntries) SetOptionalPaths(name string, paths Paths) {
235 if len(paths) > 0 {
236 a.SetPaths(name, paths)
237 }
238}
239
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800240// AddPaths appends the given path strings to a Make variable with the given name.
Colin Cross08dca382020-07-21 20:31:17 -0700241func (a *AndroidMkEntries) AddPaths(name string, paths Paths) {
242 if _, ok := a.EntryMap[name]; !ok {
243 a.entryOrder = append(a.entryOrder, name)
244 }
245 a.EntryMap[name] = append(a.EntryMap[name], paths.Strings()...)
246}
247
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800248// SetBoolIfTrue sets a Make variable with the given name to true if the given flag is true.
249// It is a no-op if the given flag is false.
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700250func (a *AndroidMkEntries) SetBoolIfTrue(name string, flag bool) {
251 if flag {
252 if _, ok := a.EntryMap[name]; !ok {
253 a.entryOrder = append(a.entryOrder, name)
254 }
255 a.EntryMap[name] = []string{"true"}
256 }
257}
258
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800259// SetBool sets a Make variable with the given name to if the given bool flag value.
Jaewoong Jung9a1e8bd2019-09-04 20:17:54 -0700260func (a *AndroidMkEntries) SetBool(name string, flag bool) {
261 if _, ok := a.EntryMap[name]; !ok {
262 a.entryOrder = append(a.entryOrder, name)
263 }
264 if flag {
265 a.EntryMap[name] = []string{"true"}
266 } else {
267 a.EntryMap[name] = []string{"false"}
268 }
269}
270
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800271// AddStrings appends the given strings to a Make variable with the given name.
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700272func (a *AndroidMkEntries) AddStrings(name string, value ...string) {
273 if len(value) == 0 {
274 return
275 }
276 if _, ok := a.EntryMap[name]; !ok {
277 a.entryOrder = append(a.entryOrder, name)
278 }
279 a.EntryMap[name] = append(a.EntryMap[name], value...)
280}
281
Liz Kammer57f5b332020-11-24 12:42:58 -0800282// AddCompatibilityTestSuites adds the supplied test suites to the EntryMap, with special handling
Tongbo Liuc5f7b962024-01-04 09:03:35 +0000283// for partial MTS and MCTS test suites.
Liz Kammer57f5b332020-11-24 12:42:58 -0800284func (a *AndroidMkEntries) AddCompatibilityTestSuites(suites ...string) {
Tongbo Liuc5f7b962024-01-04 09:03:35 +0000285 // M(C)TS supports a full test suite and partial per-module MTS test suites, with naming mts-${MODULE}.
286 // 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 -0800287 // we add the full test suite to our list.
288 if PrefixInList(suites, "mts-") && !InList("mts", suites) {
289 suites = append(suites, "mts")
290 }
Tongbo Liuc5f7b962024-01-04 09:03:35 +0000291 if PrefixInList(suites, "mcts-") && !InList("mcts", suites) {
292 suites = append(suites, "mcts")
293 }
Liz Kammer57f5b332020-11-24 12:42:58 -0800294 a.AddStrings("LOCAL_COMPATIBILITY_SUITE", suites...)
295}
296
Paul Duffin8b0349c2020-11-26 14:33:21 +0000297// The contributions to the dist.
298type distContributions struct {
Bob Badour51804382022-04-13 11:27:19 -0700299 // Path to license metadata file.
300 licenseMetadataFile Path
Paul Duffin8b0349c2020-11-26 14:33:21 +0000301 // List of goals and the dist copy instructions.
302 copiesForGoals []*copiesForGoals
303}
304
305// getCopiesForGoals returns a copiesForGoals into which copy instructions that
306// must be processed when building one or more of those goals can be added.
307func (d *distContributions) getCopiesForGoals(goals string) *copiesForGoals {
308 copiesForGoals := &copiesForGoals{goals: goals}
309 d.copiesForGoals = append(d.copiesForGoals, copiesForGoals)
310 return copiesForGoals
311}
312
313// Associates a list of dist copy instructions with a set of goals for which they
314// should be run.
315type copiesForGoals struct {
316 // goals are a space separated list of build targets that will trigger the
317 // copy instructions.
318 goals string
319
320 // A list of instructions to copy a module's output files to somewhere in the
321 // dist directory.
322 copies []distCopy
323}
324
325// Adds a copy instruction.
326func (d *copiesForGoals) addCopyInstruction(from Path, dest string) {
327 d.copies = append(d.copies, distCopy{from, dest})
328}
329
330// Instruction on a path that must be copied into the dist.
331type distCopy struct {
332 // The path to copy from.
333 from Path
334
335 // The destination within the dist directory to copy to.
336 dest string
337}
338
Jihoon Kang593171e2025-02-05 01:54:45 +0000339func (d *distCopy) String() string {
340 if len(d.dest) == 0 {
341 return d.from.String()
342 }
343 return fmt.Sprintf("%s:%s", d.from.String(), d.dest)
344}
345
346type distCopies []distCopy
347
348func (d *distCopies) Strings() (ret []string) {
349 if d == nil {
350 return
351 }
352 for _, dist := range *d {
353 ret = append(ret, dist.String())
354 }
355 return
356}
357
Cole Fausta8437c52025-02-25 14:45:43 -0800358// This gets the dist contributuions from the given module that were specified in the Android.bp
359// file using the dist: property. It does not include contribututions that the module's
360// implementation may have defined with ctx.DistForGoals(), for that, see DistProvider.
361func getDistContributions(ctx ConfigAndOtherModuleProviderContext, mod Module) *distContributions {
Yu Liu14b81452025-02-18 23:26:13 +0000362 amod := mod.base()
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000363 name := amod.BaseModuleName()
364
Cole Fausta8437c52025-02-25 14:45:43 -0800365 info := OtherModuleProviderOrDefault(ctx, mod, InstallFilesProvider)
366 availableTaggedDists := info.DistFiles
Paul Duffinaf970a22020-11-23 23:32:56 +0000367
Paul Duffin74f05592020-11-25 16:37:46 +0000368 if len(availableTaggedDists) == 0 {
Jingwen Chen7b27ca72020-07-24 09:13:49 +0000369 // Nothing dist-able for this module.
370 return nil
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000371 }
372
Paul Duffin8b0349c2020-11-26 14:33:21 +0000373 // Collate the contributions this module makes to the dist.
374 distContributions := &distContributions{}
375
Yu Liu14b81452025-02-18 23:26:13 +0000376 if !exemptFromRequiredApplicableLicensesProperty(mod) {
Yu Liuec810542024-08-26 18:09:15 +0000377 distContributions.licenseMetadataFile = info.LicenseMetadataFile
Bob Badour4660a982022-09-12 16:06:03 -0700378 }
Bob Badour51804382022-04-13 11:27:19 -0700379
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000380 // Iterate over this module's dist structs, merged from the dist and dists properties.
381 for _, dist := range amod.Dists() {
382 // Get the list of goals this dist should be enabled for. e.g. sdk, droidcore
383 goals := strings.Join(dist.Targets, " ")
384
385 // Get the tag representing the output files to be dist'd. e.g. ".jar", ".proguard_map"
386 var tag string
387 if dist.Tag == nil {
388 // If the dist struct does not specify a tag, use the default output files tag.
Paul Duffin74f05592020-11-25 16:37:46 +0000389 tag = DefaultDistTag
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000390 } else {
391 tag = *dist.Tag
392 }
393
394 // Get the paths of the output files to be dist'd, represented by the tag.
395 // Can be an empty list.
396 tagPaths := availableTaggedDists[tag]
397 if len(tagPaths) == 0 {
398 // Nothing to dist for this tag, continue to the next dist.
399 continue
400 }
401
402 if len(tagPaths) > 1 && (dist.Dest != nil || dist.Suffix != nil) {
Paul Duffin74f05592020-11-25 16:37:46 +0000403 errorMessage := "%s: Cannot apply dest/suffix for more than one dist " +
404 "file for %q goals tag %q in module %s. The list of dist files, " +
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000405 "which should have a single element, is:\n%s"
Paul Duffin74f05592020-11-25 16:37:46 +0000406 panic(fmt.Errorf(errorMessage, mod, goals, tag, name, tagPaths))
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000407 }
408
Paul Duffin8b0349c2020-11-26 14:33:21 +0000409 copiesForGoals := distContributions.getCopiesForGoals(goals)
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000410
Paul Duffin8b0349c2020-11-26 14:33:21 +0000411 // Iterate over each path adding a copy instruction to copiesForGoals
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000412 for _, path := range tagPaths {
413 // It's possible that the Path is nil from errant modules. Be defensive here.
414 if path == nil {
415 tagName := "default" // for error message readability
416 if dist.Tag != nil {
417 tagName = *dist.Tag
418 }
419 panic(fmt.Errorf("Dist file should not be nil for the %s tag in %s", tagName, name))
420 }
421
422 dest := filepath.Base(path.String())
423
424 if dist.Dest != nil {
425 var err error
426 if dest, err = validateSafePath(*dist.Dest); err != nil {
427 // This was checked in ModuleBase.GenerateBuildActions
428 panic(err)
429 }
430 }
431
Trevor Radcliffe90727f42022-03-21 19:34:02 +0000432 ext := filepath.Ext(dest)
433 suffix := ""
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000434 if dist.Suffix != nil {
Trevor Radcliffe90727f42022-03-21 19:34:02 +0000435 suffix = *dist.Suffix
436 }
437
438 productString := ""
439 if dist.Append_artifact_with_product != nil && *dist.Append_artifact_with_product {
Cole Fausta8437c52025-02-25 14:45:43 -0800440 productString = fmt.Sprintf("_%s", ctx.Config().DeviceProduct())
Trevor Radcliffe90727f42022-03-21 19:34:02 +0000441 }
442
443 if suffix != "" || productString != "" {
444 dest = strings.TrimSuffix(dest, ext) + suffix + productString + ext
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000445 }
446
447 if dist.Dir != nil {
448 var err error
449 if dest, err = validateSafePath(*dist.Dir, dest); err != nil {
450 // This was checked in ModuleBase.GenerateBuildActions
451 panic(err)
452 }
453 }
454
Paul Duffin8b0349c2020-11-26 14:33:21 +0000455 copiesForGoals.addCopyInstruction(path, dest)
456 }
457 }
458
459 return distContributions
460}
461
462// generateDistContributionsForMake generates make rules that will generate the
463// dist according to the instructions in the supplied distContribution.
464func generateDistContributionsForMake(distContributions *distContributions) []string {
465 var ret []string
466 for _, d := range distContributions.copiesForGoals {
Yu Liue70976d2024-10-15 20:45:35 +0000467 ret = append(ret, fmt.Sprintf(".PHONY: %s", d.goals))
Paul Duffin8b0349c2020-11-26 14:33:21 +0000468 // Create dist-for-goals calls for each of the copy instructions.
469 for _, c := range d.copies {
Bob Badour4660a982022-09-12 16:06:03 -0700470 if distContributions.licenseMetadataFile != nil {
471 ret = append(
472 ret,
Yu Liue70976d2024-10-15 20:45:35 +0000473 fmt.Sprintf("$(if $(strip $(ALL_TARGETS.%s.META_LIC)),,$(eval ALL_TARGETS.%s.META_LIC := %s))",
Bob Badour4660a982022-09-12 16:06:03 -0700474 c.from.String(), c.from.String(), distContributions.licenseMetadataFile.String()))
475 }
Bob Badour51804382022-04-13 11:27:19 -0700476 ret = append(
477 ret,
Yu Liue70976d2024-10-15 20:45:35 +0000478 fmt.Sprintf("$(call dist-for-goals,%s,%s:%s)", d.goals, c.from.String(), c.dest))
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000479 }
480 }
481
482 return ret
483}
484
Paul Duffin8b0349c2020-11-26 14:33:21 +0000485// Compute the list of Make strings to declare phony goals and dist-for-goals
486// calls from the module's dist and dists properties.
Yu Liu14b81452025-02-18 23:26:13 +0000487func (a *AndroidMkEntries) GetDistForGoals(mod Module) []string {
Cole Fausta8437c52025-02-25 14:45:43 -0800488 distContributions := getDistContributions(a.entryContext, mod)
Paul Duffin8b0349c2020-11-26 14:33:21 +0000489 if distContributions == nil {
490 return nil
491 }
492
493 return generateDistContributionsForMake(distContributions)
494}
495
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800496// fillInEntries goes through the common variable processing and calls the extra data funcs to
497// generate and fill in AndroidMkEntries's in-struct data, ready to be flushed to a file.
Colin Crossaa255532020-07-03 13:18:24 -0700498type fillInEntriesContext interface {
499 ModuleDir(module blueprint.Module) string
Cole Faust39aabe92023-02-23 16:57:43 -0800500 ModuleSubDir(module blueprint.Module) string
Colin Crossaa255532020-07-03 13:18:24 -0700501 Config() Config
Yu Liu663e4502024-08-12 18:23:59 +0000502 otherModuleProvider(module blueprint.Module, provider blueprint.AnyProviderKey) (any, bool)
Sasha Smundak5c4729d2022-12-01 10:49:23 -0800503 ModuleType(module blueprint.Module) string
Cole Faust43ddd082024-06-17 12:32:40 -0700504 OtherModulePropertyErrorf(module Module, property string, fmt string, args ...interface{})
Cole Faust4e2bf9f2024-09-11 13:26:20 -0700505 HasMutatorFinished(mutatorName string) bool
Colin Crossaa255532020-07-03 13:18:24 -0700506}
507
Yu Liu14b81452025-02-18 23:26:13 +0000508func (a *AndroidMkEntries) fillInEntries(ctx fillInEntriesContext, mod Module) {
Trevor Radcliffe90727f42022-03-21 19:34:02 +0000509 a.entryContext = ctx
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700510 a.EntryMap = make(map[string][]string)
Yu Liu14b81452025-02-18 23:26:13 +0000511 base := mod.base()
Colin Crossf1f763a2021-10-21 16:14:19 -0700512 name := base.BaseModuleName()
Spandan Dasb9a83f12025-03-20 23:35:47 +0000513 if bmn, ok := mod.(baseModuleName); ok {
514 name = bmn.BaseModuleName()
515 }
Colin Cross0477b422020-10-13 18:43:54 -0700516 if a.OverrideName != "" {
517 name = a.OverrideName
518 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700519
520 if a.Include == "" {
521 a.Include = "$(BUILD_PREBUILT)"
522 }
Yu Liu14b81452025-02-18 23:26:13 +0000523 a.Required = append(a.Required, mod.RequiredModuleNames(ctx)...)
524 a.Required = append(a.Required, mod.VintfFragmentModuleNames(ctx)...)
525 a.Host_required = append(a.Host_required, mod.HostRequiredModuleNames()...)
526 a.Target_required = append(a.Target_required, mod.TargetRequiredModuleNames()...)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700527
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000528 for _, distString := range a.GetDistForGoals(mod) {
Yu Liue70976d2024-10-15 20:45:35 +0000529 fmt.Fprintln(&a.header, distString)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700530 }
531
Cole Faust39aabe92023-02-23 16:57:43 -0800532 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 -0700533
Cole Faust5e1454a2025-03-11 15:55:59 -0700534 // Add the TestSuites from the provider to LOCAL_SOONG_PROVIDER_TEST_SUITES.
535 // LOCAL_SOONG_PROVIDER_TEST_SUITES will be compared against LOCAL_COMPATIBILITY_SUITES
536 // in make and enforced they're the same, to ensure we've successfully translated all
537 // LOCAL_COMPATIBILITY_SUITES usages to the provider.
538 if testSuiteInfo, ok := OtherModuleProvider(ctx, mod, TestSuiteInfoProvider); ok {
539 a.AddStrings("LOCAL_SOONG_PROVIDER_TEST_SUITES", testSuiteInfo.TestSuites...)
540 }
541
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700542 // Collect make variable assignment entries.
Colin Crossaa255532020-07-03 13:18:24 -0700543 a.SetString("LOCAL_PATH", ctx.ModuleDir(mod))
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700544 a.SetString("LOCAL_MODULE", name+a.SubName)
545 a.SetString("LOCAL_MODULE_CLASS", a.Class)
546 a.SetString("LOCAL_PREBUILT_MODULE_FILE", a.OutputFile.String())
547 a.AddStrings("LOCAL_REQUIRED_MODULES", a.Required...)
548 a.AddStrings("LOCAL_HOST_REQUIRED_MODULES", a.Host_required...)
549 a.AddStrings("LOCAL_TARGET_REQUIRED_MODULES", a.Target_required...)
Yu Liu14b81452025-02-18 23:26:13 +0000550 a.AddStrings("LOCAL_SOONG_MODULE_TYPE", ctx.ModuleType(mod))
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700551
Colin Cross6301c3c2021-09-28 17:40:21 -0700552 // If the install rule was generated by Soong tell Make about it.
Yu Liud46e5ae2024-08-15 18:46:17 +0000553 info := OtherModuleProviderOrDefault(ctx, mod, InstallFilesProvider)
554 if len(info.KatiInstalls) > 0 {
Colin Cross6301c3c2021-09-28 17:40:21 -0700555 // Assume the primary install file is last since it probably needs to depend on any other
556 // installed files. If that is not the case we can add a method to specify the primary
557 // installed file.
Yu Liud46e5ae2024-08-15 18:46:17 +0000558 a.SetPath("LOCAL_SOONG_INSTALLED_MODULE", info.KatiInstalls[len(info.KatiInstalls)-1].to)
559 a.SetString("LOCAL_SOONG_INSTALL_PAIRS", info.KatiInstalls.BuiltInstalled())
560 a.SetPaths("LOCAL_SOONG_INSTALL_SYMLINKS", info.KatiSymlinks.InstallPaths().Paths())
Jiyong Park3f627e62024-05-01 16:14:38 +0900561 } else {
562 // Soong may not have generated the install rule also when `no_full_install: true`.
563 // Mark this module as uninstallable in order to prevent Make from creating an
564 // install rule there.
565 a.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", proptools.Bool(base.commonProperties.No_full_install))
Colin Cross6301c3c2021-09-28 17:40:21 -0700566 }
567
Colin Crossa6182ab2024-08-21 10:47:44 -0700568 if info.UncheckedModule {
569 a.SetBool("LOCAL_DONT_CHECK_MODULE", true)
570 } else if info.CheckbuildTarget != nil {
571 a.SetPath("LOCAL_CHECKED_MODULE", info.CheckbuildTarget)
572 } else {
573 a.SetOptionalPath("LOCAL_CHECKED_MODULE", a.OutputFile)
574 }
575
Yu Liud46e5ae2024-08-15 18:46:17 +0000576 if len(info.TestData) > 0 {
577 a.AddStrings("LOCAL_TEST_DATA", androidMkDataPaths(info.TestData)...)
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800578 }
579
Jiyong Park89e850a2020-04-07 16:37:39 +0900580 if am, ok := mod.(ApexModule); ok {
581 a.SetBoolIfTrue("LOCAL_NOT_AVAILABLE_FOR_PLATFORM", am.NotAvailableForPlatform())
582 }
583
Colin Crossf1f763a2021-10-21 16:14:19 -0700584 archStr := base.Arch().ArchType.String()
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700585 host := false
Colin Crossf1f763a2021-10-21 16:14:19 -0700586 switch base.Os().Class {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700587 case Host:
Colin Crossf1f763a2021-10-21 16:14:19 -0700588 if base.Target().HostCross {
Jiyong Park1613e552020-09-14 19:43:17 +0900589 // Make cannot identify LOCAL_MODULE_HOST_CROSS_ARCH:= common.
Colin Crossf1f763a2021-10-21 16:14:19 -0700590 if base.Arch().ArchType != Common {
Jiyong Park1613e552020-09-14 19:43:17 +0900591 a.SetString("LOCAL_MODULE_HOST_CROSS_ARCH", archStr)
592 }
593 } else {
594 // Make cannot identify LOCAL_MODULE_HOST_ARCH:= common.
Colin Crossf1f763a2021-10-21 16:14:19 -0700595 if base.Arch().ArchType != Common {
Jiyong Park1613e552020-09-14 19:43:17 +0900596 a.SetString("LOCAL_MODULE_HOST_ARCH", archStr)
597 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700598 }
599 host = true
600 case Device:
601 // Make cannot identify LOCAL_MODULE_TARGET_ARCH:= common.
Colin Crossf1f763a2021-10-21 16:14:19 -0700602 if base.Arch().ArchType != Common {
603 if base.Target().NativeBridge {
604 hostArchStr := base.Target().NativeBridgeHostArchName
dimitry1f33e402019-03-26 12:39:31 +0100605 if hostArchStr != "" {
606 a.SetString("LOCAL_MODULE_TARGET_ARCH", hostArchStr)
607 }
608 } else {
609 a.SetString("LOCAL_MODULE_TARGET_ARCH", archStr)
610 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700611 }
612
Kelvin Zhang46977252022-04-13 16:41:34 -0700613 if !base.InVendorRamdisk() {
Yu Liu82a6d142024-08-27 19:02:29 +0000614 a.AddPaths("LOCAL_FULL_INIT_RC", info.InitRcPaths)
Yifan Hong919dae12020-12-02 18:55:06 -0800615 }
Yu Liu82a6d142024-08-27 19:02:29 +0000616 if len(info.VintfFragmentsPaths) > 0 {
617 a.AddPaths("LOCAL_FULL_VINTF_FRAGMENTS", info.VintfFragmentsPaths)
Liz Kammer7b3dc8a2021-04-16 16:41:59 -0400618 }
Colin Crossf1f763a2021-10-21 16:14:19 -0700619 a.SetBoolIfTrue("LOCAL_PROPRIETARY_MODULE", Bool(base.commonProperties.Proprietary))
620 if Bool(base.commonProperties.Vendor) || Bool(base.commonProperties.Soc_specific) {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700621 a.SetString("LOCAL_VENDOR_MODULE", "true")
622 }
Colin Crossf1f763a2021-10-21 16:14:19 -0700623 a.SetBoolIfTrue("LOCAL_ODM_MODULE", Bool(base.commonProperties.Device_specific))
624 a.SetBoolIfTrue("LOCAL_PRODUCT_MODULE", Bool(base.commonProperties.Product_specific))
625 a.SetBoolIfTrue("LOCAL_SYSTEM_EXT_MODULE", Bool(base.commonProperties.System_ext_specific))
626 if base.commonProperties.Owner != nil {
627 a.SetString("LOCAL_MODULE_OWNER", *base.commonProperties.Owner)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700628 }
629 }
630
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700631 if host {
Colin Crossf1f763a2021-10-21 16:14:19 -0700632 makeOs := base.Os().String()
633 if base.Os() == Linux || base.Os() == LinuxBionic || base.Os() == LinuxMusl {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700634 makeOs = "linux"
635 }
636 a.SetString("LOCAL_MODULE_HOST_OS", makeOs)
637 a.SetString("LOCAL_IS_HOST_MODULE", "true")
638 }
639
640 prefix := ""
Colin Crossf1f763a2021-10-21 16:14:19 -0700641 if base.ArchSpecific() {
642 switch base.Os().Class {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700643 case Host:
Colin Crossf1f763a2021-10-21 16:14:19 -0700644 if base.Target().HostCross {
Jiyong Park1613e552020-09-14 19:43:17 +0900645 prefix = "HOST_CROSS_"
646 } else {
647 prefix = "HOST_"
648 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700649 case Device:
650 prefix = "TARGET_"
651
652 }
653
Colin Crossf1f763a2021-10-21 16:14:19 -0700654 if base.Arch().ArchType != ctx.Config().Targets[base.Os()][0].Arch.ArchType {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700655 prefix = "2ND_" + prefix
656 }
657 }
Colin Crossaa255532020-07-03 13:18:24 -0700658
Yu Liu663e4502024-08-12 18:23:59 +0000659 if licenseMetadata, ok := OtherModuleProvider(ctx, mod, LicenseMetadataProvider); ok {
Colin Cross4acaea92021-12-10 23:05:02 +0000660 a.SetPath("LOCAL_SOONG_LICENSE_METADATA", licenseMetadata.LicenseMetadataPath)
661 }
662
Yu Liu663e4502024-08-12 18:23:59 +0000663 if _, ok := OtherModuleProvider(ctx, mod, ModuleInfoJSONProvider); ok {
Colin Crossd6fd0132023-11-06 13:54:06 -0800664 a.SetBool("LOCAL_SOONG_MODULE_INFO_JSON", true)
665 }
666
Colin Crossaa255532020-07-03 13:18:24 -0700667 extraCtx := &androidMkExtraEntriesContext{
668 ctx: ctx,
669 mod: mod,
670 }
671
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700672 for _, extra := range a.ExtraEntries {
Colin Crossaa255532020-07-03 13:18:24 -0700673 extra(extraCtx, a)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700674 }
675
676 // Write to footer.
677 fmt.Fprintln(&a.footer, "include "+a.Include)
Colin Crossaa255532020-07-03 13:18:24 -0700678 blueprintDir := ctx.ModuleDir(mod)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700679 for _, footerFunc := range a.ExtraFooters {
Jaewoong Jung02b11a62020-12-07 10:23:54 -0800680 footerFunc(&a.footer, name, prefix, blueprintDir)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700681 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700682}
683
Colin Crossd6fd0132023-11-06 13:54:06 -0800684func (a *AndroidMkEntries) disabled() bool {
685 return a.Disabled || !a.OutputFile.Valid()
686}
687
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800688// write flushes the AndroidMkEntries's in-struct data populated by AndroidMkEntries into the
689// given Writer object.
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700690func (a *AndroidMkEntries) write(w io.Writer) {
Colin Crossd6fd0132023-11-06 13:54:06 -0800691 if a.disabled() {
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700692 return
693 }
694
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700695 w.Write(a.header.Bytes())
696 for _, name := range a.entryOrder {
Sasha Smundakdcb61292022-12-08 10:41:33 -0800697 AndroidMkEmitAssignList(w, name, a.EntryMap[name])
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700698 }
699 w.Write(a.footer.Bytes())
700}
701
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700702func (a *AndroidMkEntries) FooterLinesForTests() []string {
703 return strings.Split(string(a.footer.Bytes()), "\n")
704}
705
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800706// AndroidMkSingleton is a singleton to collect Android.mk data from all modules and dump them into
707// the final Android-<product_name>.mk file output.
Colin Cross0875c522017-11-28 17:34:01 -0800708func AndroidMkSingleton() Singleton {
Dan Willemsen218f6562015-07-08 18:13:11 -0700709 return &androidMkSingleton{}
710}
711
712type androidMkSingleton struct{}
713
Yu Liu14b81452025-02-18 23:26:13 +0000714func allModulesSorted(ctx SingletonContext) []Module {
715 var allModules []Module
Colin Cross4f6e4e62016-01-11 12:55:55 -0800716
Yu Liu14b81452025-02-18 23:26:13 +0000717 ctx.VisitAllModules(func(module Module) {
Jihoon Kang4be8eb32025-02-10 23:02:42 +0000718 allModules = append(allModules, module)
Colin Cross4f6e4e62016-01-11 12:55:55 -0800719 })
Dan Willemsen218f6562015-07-08 18:13:11 -0700720
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800721 // Sort the module list by the module names to eliminate random churns, which may erroneously
722 // invoke additional build processes.
Jihoon Kang4be8eb32025-02-10 23:02:42 +0000723 sort.SliceStable(allModules, func(i, j int) bool {
724 return ctx.ModuleName(allModules[i]) < ctx.ModuleName(allModules[j])
Colin Cross1ad81422019-01-14 12:47:35 -0800725 })
Colin Crossd779da42015-12-17 18:00:23 -0800726
Jihoon Kang4be8eb32025-02-10 23:02:42 +0000727 return allModules
728}
729
730func (c *androidMkSingleton) GenerateBuildActions(ctx SingletonContext) {
731 // If running in soong-only mode, more limited version of this singleton is run as
732 // soong only androidmk singleton
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800733 if !ctx.Config().KatiEnabled() {
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800734 return
735 }
736
Dan Willemsen45133ac2018-03-09 21:22:06 -0800737 transMk := PathForOutput(ctx, "Android"+String(ctx.Config().productVariables.Make_suffix)+".mk")
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700738 if ctx.Failed() {
739 return
740 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700741
Colin Crossd6fd0132023-11-06 13:54:06 -0800742 moduleInfoJSON := PathForOutput(ctx, "module-info"+String(ctx.Config().productVariables.Make_suffix)+".json")
743
Jihoon Kang4be8eb32025-02-10 23:02:42 +0000744 err := translateAndroidMk(ctx, absolutePath(transMk.String()), moduleInfoJSON, allModulesSorted(ctx))
Dan Willemsen218f6562015-07-08 18:13:11 -0700745 if err != nil {
746 ctx.Errorf(err.Error())
747 }
748
Colin Cross0875c522017-11-28 17:34:01 -0800749 ctx.Build(pctx, BuildParams{
750 Rule: blueprint.Phony,
751 Output: transMk,
Dan Willemsen218f6562015-07-08 18:13:11 -0700752 })
753}
754
Jihoon Kang4be8eb32025-02-10 23:02:42 +0000755type soongOnlyAndroidMkSingleton struct {
756 Singleton
757}
758
759func soongOnlyAndroidMkSingletonFactory() Singleton {
760 return &soongOnlyAndroidMkSingleton{}
761}
762
763func (so *soongOnlyAndroidMkSingleton) GenerateBuildActions(ctx SingletonContext) {
764 if !ctx.Config().KatiEnabled() {
765 so.soongOnlyBuildActions(ctx, allModulesSorted(ctx))
766 }
767}
768
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800769// In soong-only mode, we don't do most of the androidmk stuff. But disted files are still largely
770// defined through the androidmk mechanisms, so this function is an alternate implementation of
771// the androidmk singleton that just focuses on getting the dist contributions
Yu Liu64371e02025-02-19 23:44:48 +0000772// TODO(b/397766191): Change the signature to take ModuleProxy
773// Please only access the module's internal data through providers.
Yu Liu14b81452025-02-18 23:26:13 +0000774func (so *soongOnlyAndroidMkSingleton) soongOnlyBuildActions(ctx SingletonContext, mods []Module) {
Cole Faustf9a096c2025-01-21 16:55:43 -0800775 allDistContributions, moduleInfoJSONs := getSoongOnlyDataFromMods(ctx, mods)
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800776
Cole Faustf2aab5e2025-02-11 13:32:51 -0800777 singletonDists := getSingletonDists(ctx.Config())
778 singletonDists.lock.Lock()
779 if contribution := distsToDistContributions(singletonDists.dists); contribution != nil {
780 allDistContributions = append(allDistContributions, *contribution)
781 }
782 singletonDists.lock.Unlock()
783
Cole Faustf9a096c2025-01-21 16:55:43 -0800784 // Build module-info.json. Only in builds with HasDeviceProduct(), as we need a named
785 // device to have a TARGET_OUT folder.
786 if ctx.Config().HasDeviceProduct() {
Cole Faust601da062025-01-22 13:15:10 -0800787 preMergePath := PathForOutput(ctx, "module_info_pre_merging.json")
Cole Faustf9a096c2025-01-21 16:55:43 -0800788 moduleInfoJSONPath := pathForInstall(ctx, Android, X86_64, "", "module-info.json")
Cole Faust601da062025-01-22 13:15:10 -0800789 if err := writeModuleInfoJSON(ctx, moduleInfoJSONs, preMergePath); err != nil {
Cole Faustf9a096c2025-01-21 16:55:43 -0800790 ctx.Errorf("%s", err)
791 }
Cole Faust601da062025-01-22 13:15:10 -0800792 builder := NewRuleBuilder(pctx, ctx)
793 builder.Command().
794 BuiltTool("merge_module_info_json").
795 FlagWithOutput("-o ", moduleInfoJSONPath).
796 Input(preMergePath)
797 builder.Build("merge_module_info_json", "merge module info json")
Cole Faustf9a096c2025-01-21 16:55:43 -0800798 ctx.Phony("module-info", moduleInfoJSONPath)
799 ctx.Phony("droidcore-unbundled", moduleInfoJSONPath)
800 allDistContributions = append(allDistContributions, distContributions{
801 copiesForGoals: []*copiesForGoals{{
802 goals: "general-tests droidcore-unbundled",
803 copies: []distCopy{{
804 from: moduleInfoJSONPath,
805 dest: "module-info.json",
806 }},
807 }},
808 })
809 }
810
811 // Build dist.mk for the packaging step to read and generate dist targets
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800812 distMkFile := absolutePath(filepath.Join(ctx.Config().katiPackageMkDir(), "dist.mk"))
813
814 var goalOutputPairs []string
Cole Faust82611a12025-01-09 11:20:41 -0800815 var srcDstPairs []string
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800816 for _, contributions := range allDistContributions {
817 for _, copiesForGoal := range contributions.copiesForGoals {
818 goals := strings.Fields(copiesForGoal.goals)
819 for _, copy := range copiesForGoal.copies {
820 for _, goal := range goals {
Cole Faust82611a12025-01-09 11:20:41 -0800821 goalOutputPairs = append(goalOutputPairs, fmt.Sprintf(" %s:%s", goal, copy.dest))
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800822 }
Cole Faust82611a12025-01-09 11:20:41 -0800823 srcDstPairs = append(srcDstPairs, fmt.Sprintf(" %s:%s", copy.from.String(), copy.dest))
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800824 }
825 }
826 }
Cole Faust82611a12025-01-09 11:20:41 -0800827 // There are duplicates in the lists that we need to remove
828 goalOutputPairs = SortedUniqueStrings(goalOutputPairs)
829 srcDstPairs = SortedUniqueStrings(srcDstPairs)
830 var buf strings.Builder
831 buf.WriteString("DIST_SRC_DST_PAIRS :=")
832 for _, srcDstPair := range srcDstPairs {
833 buf.WriteString(srcDstPair)
834 }
835 buf.WriteString("\nDIST_GOAL_OUTPUT_PAIRS :=")
836 for _, goalOutputPair := range goalOutputPairs {
837 buf.WriteString(goalOutputPair)
838 }
839 buf.WriteString("\n")
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800840
841 writeValueIfChanged(ctx, distMkFile, buf.String())
842}
843
844func writeValueIfChanged(ctx SingletonContext, path string, value string) {
845 if err := os.MkdirAll(filepath.Dir(path), 0777); err != nil {
846 ctx.Errorf("%s\n", err)
847 return
848 }
849 previousValue := ""
850 rawPreviousValue, err := os.ReadFile(path)
851 if err == nil {
852 previousValue = string(rawPreviousValue)
853 }
854
855 if previousValue != value {
856 if err = os.WriteFile(path, []byte(value), 0666); err != nil {
857 ctx.Errorf("Failed to write: %v", err)
858 }
859 }
860}
861
Cole Faustd62a4892025-02-07 16:55:11 -0800862func distsToDistContributions(dists []dist) *distContributions {
863 if len(dists) == 0 {
Jihoon Kang593171e2025-02-05 01:54:45 +0000864 return nil
865 }
866
867 copyGoals := []*copiesForGoals{}
Cole Faustd62a4892025-02-07 16:55:11 -0800868 for _, dist := range dists {
Jihoon Kang593171e2025-02-05 01:54:45 +0000869 for _, goal := range dist.goals {
Cole Faustd62a4892025-02-07 16:55:11 -0800870 copyGoals = append(copyGoals, &copiesForGoals{
871 goals: goal,
872 copies: dist.paths,
873 })
Jihoon Kang593171e2025-02-05 01:54:45 +0000874 }
875 }
876
Cole Faustd62a4892025-02-07 16:55:11 -0800877 return &distContributions{
878 copiesForGoals: copyGoals,
879 }
Jihoon Kang593171e2025-02-05 01:54:45 +0000880}
881
Cole Faustf9a096c2025-01-21 16:55:43 -0800882// getSoongOnlyDataFromMods gathers data from the given modules needed in soong-only builds.
883// Currently, this is the dist contributions, and the module-info.json contents.
Yu Liu14b81452025-02-18 23:26:13 +0000884func getSoongOnlyDataFromMods(ctx fillInEntriesContext, mods []Module) ([]distContributions, []*ModuleInfoJSON) {
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800885 var allDistContributions []distContributions
Cole Faustf9a096c2025-01-21 16:55:43 -0800886 var moduleInfoJSONs []*ModuleInfoJSON
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800887 for _, mod := range mods {
Cole Faustd62a4892025-02-07 16:55:11 -0800888 if distInfo, ok := OtherModuleProvider(ctx, mod, DistProvider); ok {
889 if contribution := distsToDistContributions(distInfo.Dists); contribution != nil {
890 allDistContributions = append(allDistContributions, *contribution)
891 }
892 }
893
Yu Liuef9e63e2025-03-04 19:01:28 +0000894 commonInfo, _ := OtherModuleProvider(ctx, mod, CommonModuleInfoProvider)
Yu Liu64371e02025-02-19 23:44:48 +0000895 if commonInfo.SkipAndroidMkProcessing {
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800896 continue
897 }
898 if info, ok := OtherModuleProvider(ctx, mod, AndroidMkInfoProvider); ok {
Cole Faust556f1f42025-01-09 10:49:42 -0800899 // Deep copy the provider info since we need to modify the info later
900 info := deepCopyAndroidMkProviderInfo(info)
Yu Liu64371e02025-02-19 23:44:48 +0000901 info.PrimaryInfo.fillInEntries(ctx, mod, &commonInfo)
Cole Faust556f1f42025-01-09 10:49:42 -0800902 if info.PrimaryInfo.disabled() {
903 continue
904 }
Cole Faustf9a096c2025-01-21 16:55:43 -0800905 if moduleInfoJSON, ok := OtherModuleProvider(ctx, mod, ModuleInfoJSONProvider); ok {
Jihoon Kangd4063812025-01-24 00:25:30 +0000906 moduleInfoJSONs = append(moduleInfoJSONs, moduleInfoJSON...)
Cole Faustf9a096c2025-01-21 16:55:43 -0800907 }
Cole Fausta8437c52025-02-25 14:45:43 -0800908 if contribution := getDistContributions(ctx, mod); contribution != nil {
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800909 allDistContributions = append(allDistContributions, *contribution)
910 }
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800911 } else {
Jihoon Kange6daf662025-02-06 01:38:16 +0000912 if x, ok := mod.(AndroidMkDataProvider); ok {
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800913 data := x.AndroidMk()
914
915 if data.Include == "" {
916 data.Include = "$(BUILD_PREBUILT)"
917 }
918
919 data.fillInData(ctx, mod)
Cole Faust556f1f42025-01-09 10:49:42 -0800920 if data.Entries.disabled() {
921 continue
922 }
Cole Faustf9a096c2025-01-21 16:55:43 -0800923 if moduleInfoJSON, ok := OtherModuleProvider(ctx, mod, ModuleInfoJSONProvider); ok {
Jihoon Kangd4063812025-01-24 00:25:30 +0000924 moduleInfoJSONs = append(moduleInfoJSONs, moduleInfoJSON...)
Cole Faustf9a096c2025-01-21 16:55:43 -0800925 }
Cole Fausta8437c52025-02-25 14:45:43 -0800926 if contribution := getDistContributions(ctx, mod); contribution != nil {
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800927 allDistContributions = append(allDistContributions, *contribution)
928 }
Jihoon Kange6daf662025-02-06 01:38:16 +0000929 }
930 if x, ok := mod.(AndroidMkEntriesProvider); ok {
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800931 entriesList := x.AndroidMkEntries()
932 for _, entries := range entriesList {
933 entries.fillInEntries(ctx, mod)
Cole Faust556f1f42025-01-09 10:49:42 -0800934 if entries.disabled() {
935 continue
936 }
Cole Faustf9a096c2025-01-21 16:55:43 -0800937 if moduleInfoJSON, ok := OtherModuleProvider(ctx, mod, ModuleInfoJSONProvider); ok {
Jihoon Kangd4063812025-01-24 00:25:30 +0000938 moduleInfoJSONs = append(moduleInfoJSONs, moduleInfoJSON...)
Cole Faustf9a096c2025-01-21 16:55:43 -0800939 }
Cole Fausta8437c52025-02-25 14:45:43 -0800940 if contribution := getDistContributions(ctx, mod); contribution != nil {
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800941 allDistContributions = append(allDistContributions, *contribution)
942 }
943 }
Jihoon Kange6daf662025-02-06 01:38:16 +0000944 }
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800945 }
946 }
Cole Faustf9a096c2025-01-21 16:55:43 -0800947 return allDistContributions, moduleInfoJSONs
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800948}
949
Yu Liu14b81452025-02-18 23:26:13 +0000950func translateAndroidMk(ctx SingletonContext, absMkFile string, moduleInfoJSONPath WritablePath, mods []Module) error {
Dan Willemsen218f6562015-07-08 18:13:11 -0700951 buf := &bytes.Buffer{}
952
Colin Crossd6fd0132023-11-06 13:54:06 -0800953 var moduleInfoJSONs []*ModuleInfoJSON
954
Dan Willemsen97750522016-02-09 17:43:51 -0800955 fmt.Fprintln(buf, "LOCAL_MODULE_MAKEFILE := $(lastword $(MAKEFILE_LIST))")
Dan Willemsen218f6562015-07-08 18:13:11 -0700956
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800957 typeStats := make(map[string]int)
Dan Willemsen218f6562015-07-08 18:13:11 -0700958 for _, mod := range mods {
Colin Crossd6fd0132023-11-06 13:54:06 -0800959 err := translateAndroidMkModule(ctx, buf, &moduleInfoJSONs, mod)
Dan Willemsen218f6562015-07-08 18:13:11 -0700960 if err != nil {
Colin Cross836e3872021-11-09 12:30:59 -0800961 os.Remove(absMkFile)
Dan Willemsen218f6562015-07-08 18:13:11 -0700962 return err
963 }
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700964
Yu Liu14b81452025-02-18 23:26:13 +0000965 if ctx.PrimaryModule(mod) == mod {
966 typeStats[ctx.ModuleType(mod)] += 1
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700967 }
968 }
969
970 keys := []string{}
971 fmt.Fprintln(buf, "\nSTATS.SOONG_MODULE_TYPE :=")
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800972 for k := range typeStats {
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700973 keys = append(keys, k)
974 }
975 sort.Strings(keys)
976 for _, mod_type := range keys {
977 fmt.Fprintln(buf, "STATS.SOONG_MODULE_TYPE +=", mod_type)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800978 fmt.Fprintf(buf, "STATS.SOONG_MODULE_TYPE.%s := %d\n", mod_type, typeStats[mod_type])
Dan Willemsen218f6562015-07-08 18:13:11 -0700979 }
980
Colin Crossd6fd0132023-11-06 13:54:06 -0800981 err := pathtools.WriteFileIfChanged(absMkFile, buf.Bytes(), 0666)
982 if err != nil {
983 return err
984 }
985
986 return writeModuleInfoJSON(ctx, moduleInfoJSONs, moduleInfoJSONPath)
Dan Willemsen218f6562015-07-08 18:13:11 -0700987}
988
Colin Crossd6fd0132023-11-06 13:54:06 -0800989func writeModuleInfoJSON(ctx SingletonContext, moduleInfoJSONs []*ModuleInfoJSON, moduleInfoJSONPath WritablePath) error {
990 moduleInfoJSONBuf := &strings.Builder{}
991 moduleInfoJSONBuf.WriteString("[")
992 for i, moduleInfoJSON := range moduleInfoJSONs {
993 if i != 0 {
994 moduleInfoJSONBuf.WriteString(",\n")
995 }
996 moduleInfoJSONBuf.WriteString("{")
997 moduleInfoJSONBuf.WriteString(strconv.Quote(moduleInfoJSON.core.RegisterName))
998 moduleInfoJSONBuf.WriteString(":")
999 err := encodeModuleInfoJSON(moduleInfoJSONBuf, moduleInfoJSON)
1000 moduleInfoJSONBuf.WriteString("}")
1001 if err != nil {
1002 return err
1003 }
1004 }
1005 moduleInfoJSONBuf.WriteString("]")
1006 WriteFileRule(ctx, moduleInfoJSONPath, moduleInfoJSONBuf.String())
1007 return nil
1008}
1009
Yu Liu14b81452025-02-18 23:26:13 +00001010func translateAndroidMkModule(ctx SingletonContext, w io.Writer, moduleInfoJSONs *[]*ModuleInfoJSON, mod Module) error {
Colin Cross953d3a22018-09-05 16:23:54 -07001011 defer func() {
1012 if r := recover(); r != nil {
1013 panic(fmt.Errorf("%s in translateAndroidMkModule for module %s variant %s",
1014 r, ctx.ModuleName(mod), ctx.ModuleSubDir(mod)))
1015 }
1016 }()
1017
Bob Badourb4999222021-01-07 03:34:31 +00001018 // Additional cases here require review for correct license propagation to make.
Colin Crossd6fd0132023-11-06 13:54:06 -08001019 var err error
mrziwang18420972024-09-03 15:12:51 -07001020
Yu Liue70976d2024-10-15 20:45:35 +00001021 if info, ok := OtherModuleProvider(ctx, mod, AndroidMkInfoProvider); ok {
1022 err = translateAndroidMkEntriesInfoModule(ctx, w, moduleInfoJSONs, mod, info)
mrziwang18420972024-09-03 15:12:51 -07001023 } else {
1024 switch x := mod.(type) {
1025 case AndroidMkDataProvider:
1026 err = translateAndroidModule(ctx, w, moduleInfoJSONs, mod, x)
mrziwang18420972024-09-03 15:12:51 -07001027 case AndroidMkEntriesProvider:
1028 err = translateAndroidMkEntriesModule(ctx, w, moduleInfoJSONs, mod, x)
1029 default:
1030 // Not exported to make so no make variables to set.
1031 }
Dan Willemsen218f6562015-07-08 18:13:11 -07001032 }
Colin Crossd6fd0132023-11-06 13:54:06 -08001033
1034 if err != nil {
1035 return err
1036 }
1037
1038 return err
Colin Cross2465c3d2018-09-28 10:19:18 -07001039}
1040
Yu Liu14b81452025-02-18 23:26:13 +00001041func (data *AndroidMkData) fillInData(ctx fillInEntriesContext, mod Module) {
Jooyung Han12df5fb2019-07-11 16:18:47 +09001042 // Get the preamble content through AndroidMkEntries logic.
Jooyung Han2ed99d02020-06-24 23:26:26 +09001043 data.Entries = AndroidMkEntries{
Jooyung Han12df5fb2019-07-11 16:18:47 +09001044 Class: data.Class,
1045 SubName: data.SubName,
Jooyung Han12df5fb2019-07-11 16:18:47 +09001046 OutputFile: data.OutputFile,
1047 Disabled: data.Disabled,
1048 Include: data.Include,
1049 Required: data.Required,
1050 Host_required: data.Host_required,
1051 Target_required: data.Target_required,
1052 }
Colin Crossaa255532020-07-03 13:18:24 -07001053 data.Entries.fillInEntries(ctx, mod)
Jooyung Han12df5fb2019-07-11 16:18:47 +09001054
1055 // copy entries back to data since it is used in Custom
Jooyung Han2ed99d02020-06-24 23:26:26 +09001056 data.Required = data.Entries.Required
1057 data.Host_required = data.Entries.Host_required
1058 data.Target_required = data.Entries.Target_required
Jooyung Han12df5fb2019-07-11 16:18:47 +09001059}
1060
Jaewoong Jung7ef4a902020-11-16 12:50:29 -08001061// A support func for the deprecated AndroidMkDataProvider interface. Use AndroidMkEntryProvider
1062// instead.
Colin Crossd6fd0132023-11-06 13:54:06 -08001063func translateAndroidModule(ctx SingletonContext, w io.Writer, moduleInfoJSONs *[]*ModuleInfoJSON,
Yu Liu14b81452025-02-18 23:26:13 +00001064 mod Module, provider AndroidMkDataProvider) error {
Dan Willemsen218f6562015-07-08 18:13:11 -07001065
Yu Liu14b81452025-02-18 23:26:13 +00001066 amod := mod.base()
Cole Fausta963b942024-04-11 17:43:00 -07001067 if shouldSkipAndroidMkProcessing(ctx, amod) {
Jeff Gaston088e29e2017-11-29 16:47:17 -08001068 return nil
1069 }
1070
Colin Cross91825d22017-08-10 16:59:47 -07001071 data := provider.AndroidMk()
Yu Liuddc28332024-08-09 22:48:30 +00001072
Colin Cross53499412017-09-07 13:20:25 -07001073 if data.Include == "" {
1074 data.Include = "$(BUILD_PREBUILT)"
1075 }
1076
Colin Crossaa255532020-07-03 13:18:24 -07001077 data.fillInData(ctx, mod)
Yu Liu14b81452025-02-18 23:26:13 +00001078 aconfigUpdateAndroidMkData(ctx, mod, &data)
Dan Willemsen01a405a2016-06-13 17:19:03 -07001079
Colin Cross0f86d182017-08-10 17:07:28 -07001080 prefix := ""
1081 if amod.ArchSpecific() {
1082 switch amod.Os().Class {
1083 case Host:
Jiyong Park1613e552020-09-14 19:43:17 +09001084 if amod.Target().HostCross {
1085 prefix = "HOST_CROSS_"
1086 } else {
1087 prefix = "HOST_"
1088 }
Colin Cross0f86d182017-08-10 17:07:28 -07001089 case Device:
1090 prefix = "TARGET_"
Colin Crossa2344662016-03-24 13:14:12 -07001091
Dan Willemsen218f6562015-07-08 18:13:11 -07001092 }
1093
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001094 if amod.Arch().ArchType != ctx.Config().Targets[amod.Os()][0].Arch.ArchType {
Colin Cross0f86d182017-08-10 17:07:28 -07001095 prefix = "2ND_" + prefix
1096 }
Dan Willemsen218f6562015-07-08 18:13:11 -07001097 }
1098
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -07001099 name := provider.BaseModuleName()
Colin Cross0f86d182017-08-10 17:07:28 -07001100 blueprintDir := filepath.Dir(ctx.BlueprintFile(mod))
1101
1102 if data.Custom != nil {
Bob Badourb4999222021-01-07 03:34:31 +00001103 // List of module types allowed to use .Custom(...)
1104 // Additions to the list require careful review for proper license handling.
Colin Crossaa255532020-07-03 13:18:24 -07001105 switch reflect.TypeOf(mod).String() { // ctx.ModuleType(mod) doesn't work: aidl_interface creates phony without type
Bob Badourb4999222021-01-07 03:34:31 +00001106 case "*aidl.aidlApi": // writes non-custom before adding .phony
1107 case "*aidl.aidlMapping": // writes non-custom before adding .phony
1108 case "*android.customModule": // appears in tests only
Dan Willemsen9fe14102021-07-13 21:52:04 -07001109 case "*android_sdk.sdkRepoHost": // doesn't go through base_rules
Bob Badourb4999222021-01-07 03:34:31 +00001110 case "*apex.apexBundle": // license properties written
1111 case "*bpf.bpf": // license properties written (both for module and objs)
Neill Kapron41efab72024-07-31 22:17:36 +00001112 case "*libbpf_prog.libbpfProg": // license properties written (both for module and objs)
Bob Badourb4999222021-01-07 03:34:31 +00001113 case "*genrule.Module": // writes non-custom before adding .phony
1114 case "*java.SystemModules": // doesn't go through base_rules
1115 case "*java.systemModulesImport": // doesn't go through base_rules
1116 case "*phony.phony": // license properties written
Nelson Lif3c70682023-12-20 02:37:52 +00001117 case "*phony.PhonyRule": // writes phony deps and acts like `.PHONY`
Bob Badourb4999222021-01-07 03:34:31 +00001118 case "*selinux.selinuxContextsModule": // license properties written
1119 case "*sysprop.syspropLibrary": // license properties written
Bill Yang3b3aac02024-09-05 09:22:09 +00001120 case "*vintf.vintfCompatibilityMatrixRule": // use case like phony
Bob Badourb4999222021-01-07 03:34:31 +00001121 default:
Bob Badour65ee90a2021-09-02 15:33:10 -07001122 if !ctx.Config().IsEnvFalse("ANDROID_REQUIRE_LICENSES") {
Bob Badourb4999222021-01-07 03:34:31 +00001123 return fmt.Errorf("custom make rules not allowed for %q (%q) module %q", ctx.ModuleType(mod), reflect.TypeOf(mod), ctx.ModuleName(mod))
1124 }
1125 }
Colin Cross0f86d182017-08-10 17:07:28 -07001126 data.Custom(w, name, prefix, blueprintDir, data)
1127 } else {
1128 WriteAndroidMkData(w, data)
1129 }
1130
Colin Crossd6fd0132023-11-06 13:54:06 -08001131 if !data.Entries.disabled() {
Yu Liu663e4502024-08-12 18:23:59 +00001132 if moduleInfoJSON, ok := OtherModuleProvider(ctx, mod, ModuleInfoJSONProvider); ok {
Jihoon Kangd4063812025-01-24 00:25:30 +00001133 *moduleInfoJSONs = append(*moduleInfoJSONs, moduleInfoJSON...)
Colin Crossd6fd0132023-11-06 13:54:06 -08001134 }
1135 }
1136
Colin Cross0f86d182017-08-10 17:07:28 -07001137 return nil
1138}
1139
Jaewoong Jung7ef4a902020-11-16 12:50:29 -08001140// A support func for the deprecated AndroidMkDataProvider interface. Use AndroidMkEntryProvider
1141// instead.
Colin Cross0f86d182017-08-10 17:07:28 -07001142func WriteAndroidMkData(w io.Writer, data AndroidMkData) {
Colin Crossd6fd0132023-11-06 13:54:06 -08001143 if data.Entries.disabled() {
Colin Cross0f86d182017-08-10 17:07:28 -07001144 return
1145 }
1146
Jooyung Han2ed99d02020-06-24 23:26:26 +09001147 // write preamble via Entries
1148 data.Entries.footer = bytes.Buffer{}
1149 data.Entries.write(w)
Colin Cross0f86d182017-08-10 17:07:28 -07001150
Colin Crossca860ac2016-01-04 14:34:37 -08001151 for _, extra := range data.Extra {
Colin Cross27a4b052017-08-10 16:32:23 -07001152 extra(w, data.OutputFile.Path())
Dan Willemsen97750522016-02-09 17:43:51 -08001153 }
1154
Colin Cross53499412017-09-07 13:20:25 -07001155 fmt.Fprintln(w, "include "+data.Include)
Dan Willemsen218f6562015-07-08 18:13:11 -07001156}
Sasha Smundakb6d23052019-04-01 18:37:36 -07001157
Colin Crossd6fd0132023-11-06 13:54:06 -08001158func translateAndroidMkEntriesModule(ctx SingletonContext, w io.Writer, moduleInfoJSONs *[]*ModuleInfoJSON,
Yu Liu14b81452025-02-18 23:26:13 +00001159 mod Module, provider AndroidMkEntriesProvider) error {
1160 if shouldSkipAndroidMkProcessing(ctx, mod.base()) {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -07001161 return nil
Sasha Smundakb6d23052019-04-01 18:37:36 -07001162 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -07001163
Colin Crossd6fd0132023-11-06 13:54:06 -08001164 entriesList := provider.AndroidMkEntries()
Yu Liu14b81452025-02-18 23:26:13 +00001165 aconfigUpdateAndroidMkEntries(ctx, mod, &entriesList)
Colin Crossd6fd0132023-11-06 13:54:06 -08001166
Jihoon Kangd4063812025-01-24 00:25:30 +00001167 moduleInfoJSON, providesModuleInfoJSON := OtherModuleProvider(ctx, mod, ModuleInfoJSONProvider)
1168
Bob Badourb4999222021-01-07 03:34:31 +00001169 // Any new or special cases here need review to verify correct propagation of license information.
Colin Crossd6fd0132023-11-06 13:54:06 -08001170 for _, entries := range entriesList {
Colin Crossaa255532020-07-03 13:18:24 -07001171 entries.fillInEntries(ctx, mod)
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001172 entries.write(w)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -07001173
Jihoon Kangd4063812025-01-24 00:25:30 +00001174 if providesModuleInfoJSON && !entries.disabled() {
1175 // append only the name matching moduleInfoJSON entry
1176 for _, m := range moduleInfoJSON {
1177 if m.RegisterNameOverride == entries.OverrideName && m.SubName == entries.SubName {
1178 *moduleInfoJSONs = append(*moduleInfoJSONs, m)
1179 }
1180 }
Colin Crossd6fd0132023-11-06 13:54:06 -08001181 }
1182 }
1183
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -07001184 return nil
1185}
1186
Cole Fauste8a87832024-09-11 11:35:46 -07001187func ShouldSkipAndroidMkProcessing(ctx ConfigurableEvaluatorContext, module Module) bool {
Cole Fausta963b942024-04-11 17:43:00 -07001188 return shouldSkipAndroidMkProcessing(ctx, module.base())
Chih-Hung Hsieh80783772021-10-11 16:46:56 -07001189}
1190
Cole Fauste8a87832024-09-11 11:35:46 -07001191func shouldSkipAndroidMkProcessing(ctx ConfigurableEvaluatorContext, module *ModuleBase) bool {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -07001192 if !module.commonProperties.NamespaceExportedToMake {
1193 // TODO(jeffrygaston) do we want to validate that there are no modules being
1194 // exported to Kati that depend on this module?
1195 return true
Sasha Smundakb6d23052019-04-01 18:37:36 -07001196 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -07001197
Dan Willemsendef7b5d2021-10-17 00:22:33 -07001198 // On Mac, only expose host darwin modules to Make, as that's all we claim to support.
1199 // In reality, some of them depend on device-built (Java) modules, so we can't disable all
1200 // device modules in Soong, but we can hide them from Make (and thus the build user interface)
1201 if runtime.GOOS == "darwin" && module.Os() != Darwin {
1202 return true
1203 }
1204
Dan Willemsen8528f4e2021-10-19 00:22:06 -07001205 // Only expose the primary Darwin target, as Make does not understand Darwin+Arm64
1206 if module.Os() == Darwin && module.Target().HostCross {
1207 return true
1208 }
1209
Cole Fausta963b942024-04-11 17:43:00 -07001210 return !module.Enabled(ctx) ||
Colin Crossa9c8c9f2020-12-16 10:20:23 -08001211 module.commonProperties.HideFromMake ||
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -07001212 // Make does not understand LinuxBionic
Colin Cross9a027be2022-06-24 18:45:58 -07001213 module.Os() == LinuxBionic ||
1214 // Make does not understand LinuxMusl, except when we are building with USE_HOST_MUSL=true
1215 // and all host binaries are LinuxMusl
1216 (module.Os() == LinuxMusl && module.Target().HostCross)
Sasha Smundakb6d23052019-04-01 18:37:36 -07001217}
Dan Shi31949122020-09-21 12:11:02 -07001218
Jaewoong Jung7ef4a902020-11-16 12:50:29 -08001219// A utility func to format LOCAL_TEST_DATA outputs. See the comments on DataPath to understand how
1220// to use this func.
Colin Cross5c1d5fb2023-11-15 12:39:40 -08001221func androidMkDataPaths(data []DataPath) []string {
Dan Shi31949122020-09-21 12:11:02 -07001222 var testFiles []string
1223 for _, d := range data {
1224 rel := d.SrcPath.Rel()
Colin Cross5c1d5fb2023-11-15 12:39:40 -08001225 if d.WithoutRel {
1226 rel = d.SrcPath.Base()
1227 }
Dan Shi31949122020-09-21 12:11:02 -07001228 path := d.SrcPath.String()
Jaewoong Jung7ef4a902020-11-16 12:50:29 -08001229 // LOCAL_TEST_DATA requires the rel portion of the path to be removed from the path.
Dan Shi31949122020-09-21 12:11:02 -07001230 if !strings.HasSuffix(path, rel) {
1231 panic(fmt.Errorf("path %q does not end with %q", path, rel))
1232 }
1233 path = strings.TrimSuffix(path, rel)
1234 testFileString := path + ":" + rel
1235 if len(d.RelativeInstallPath) > 0 {
1236 testFileString += ":" + d.RelativeInstallPath
1237 }
1238 testFiles = append(testFiles, testFileString)
1239 }
1240 return testFiles
1241}
Sasha Smundakdcb61292022-12-08 10:41:33 -08001242
1243// AndroidMkEmitAssignList emits the line
1244//
1245// VAR := ITEM ...
1246//
1247// Items are the elements to the given set of lists
1248// If all the passed lists are empty, no line will be emitted
1249func AndroidMkEmitAssignList(w io.Writer, varName string, lists ...[]string) {
1250 doPrint := false
1251 for _, l := range lists {
1252 if doPrint = len(l) > 0; doPrint {
1253 break
1254 }
1255 }
1256 if !doPrint {
1257 return
1258 }
1259 fmt.Fprint(w, varName, " :=")
1260 for _, l := range lists {
1261 for _, item := range l {
1262 fmt.Fprint(w, " ", item)
1263 }
1264 }
1265 fmt.Fprintln(w)
1266}
mrziwang18420972024-09-03 15:12:51 -07001267
1268type AndroidMkProviderInfo struct {
1269 PrimaryInfo AndroidMkInfo
1270 ExtraInfo []AndroidMkInfo
1271}
1272
1273type AndroidMkInfo struct {
1274 // Android.mk class string, e.g. EXECUTABLES, JAVA_LIBRARIES, ETC
1275 Class string
1276 // Optional suffix to append to the module name. Useful when a module wants to return multiple
1277 // AndroidMkEntries objects. For example, when a java_library returns an additional entry for
1278 // its hostdex sub-module, this SubName field is set to "-hostdex" so that it can have a
1279 // different name than the parent's.
1280 SubName string
1281 // If set, this value overrides the base module name. SubName is still appended.
1282 OverrideName string
mrziwang18420972024-09-03 15:12:51 -07001283 // The output file for Kati to process and/or install. If absent, the module is skipped.
1284 OutputFile OptionalPath
1285 // If true, the module is skipped and does not appear on the final Android-<product name>.mk
1286 // file. Useful when a module needs to be skipped conditionally.
1287 Disabled bool
1288 // The postprocessing mk file to include, e.g. $(BUILD_SYSTEM)/soong_cc_rust_prebuilt.mk
1289 // If not set, $(BUILD_SYSTEM)/prebuilt.mk is used.
1290 Include string
1291 // Required modules that need to be built and included in the final build output when building
1292 // this module.
1293 Required []string
1294 // Required host modules that need to be built and included in the final build output when
1295 // building this module.
1296 Host_required []string
1297 // Required device modules that need to be built and included in the final build output when
1298 // building this module.
1299 Target_required []string
1300
1301 HeaderStrings []string
1302 FooterStrings []string
1303
1304 // A map that holds the up-to-date Make variable values. Can be accessed from tests.
1305 EntryMap map[string][]string
1306 // A list of EntryMap keys in insertion order. This serves a few purposes:
1307 // 1. Prevents churns. Golang map doesn't provide consistent iteration order, so without this,
1308 // the outputted Android-*.mk file may change even though there have been no content changes.
1309 // 2. Allows modules to refer to other variables, like LOCAL_BAR_VAR := $(LOCAL_FOO_VAR),
1310 // without worrying about the variables being mixed up in the actual mk file.
1311 // 3. Makes troubleshooting and spotting errors easier.
1312 EntryOrder []string
1313}
1314
Yu Liue70976d2024-10-15 20:45:35 +00001315type AndroidMkProviderInfoProducer interface {
1316 PrepareAndroidMKProviderInfo(config Config) *AndroidMkProviderInfo
1317}
1318
mrziwang18420972024-09-03 15:12:51 -07001319// TODO: rename it to AndroidMkEntriesProvider after AndroidMkEntriesProvider interface is gone.
1320var AndroidMkInfoProvider = blueprint.NewProvider[*AndroidMkProviderInfo]()
1321
Yu Liu64371e02025-02-19 23:44:48 +00001322// TODO(b/397766191): Change the signature to take ModuleProxy
1323// Please only access the module's internal data through providers.
mrziwang18420972024-09-03 15:12:51 -07001324func translateAndroidMkEntriesInfoModule(ctx SingletonContext, w io.Writer, moduleInfoJSONs *[]*ModuleInfoJSON,
Yu Liu14b81452025-02-18 23:26:13 +00001325 mod Module, providerInfo *AndroidMkProviderInfo) error {
Yu Liuef9e63e2025-03-04 19:01:28 +00001326 commonInfo, _ := OtherModuleProvider(ctx, mod, CommonModuleInfoProvider)
Yu Liu64371e02025-02-19 23:44:48 +00001327 if commonInfo.SkipAndroidMkProcessing {
mrziwang18420972024-09-03 15:12:51 -07001328 return nil
1329 }
1330
1331 // Deep copy the provider info since we need to modify the info later
1332 info := deepCopyAndroidMkProviderInfo(providerInfo)
1333
Yu Liu14b81452025-02-18 23:26:13 +00001334 aconfigUpdateAndroidMkInfos(ctx, mod, &info)
mrziwang18420972024-09-03 15:12:51 -07001335
1336 // Any new or special cases here need review to verify correct propagation of license information.
Yu Liu64371e02025-02-19 23:44:48 +00001337 info.PrimaryInfo.fillInEntries(ctx, mod, &commonInfo)
mrziwang18420972024-09-03 15:12:51 -07001338 info.PrimaryInfo.write(w)
1339 if len(info.ExtraInfo) > 0 {
1340 for _, ei := range info.ExtraInfo {
Yu Liu64371e02025-02-19 23:44:48 +00001341 ei.fillInEntries(ctx, mod, &commonInfo)
mrziwang18420972024-09-03 15:12:51 -07001342 ei.write(w)
1343 }
1344 }
1345
1346 if !info.PrimaryInfo.disabled() {
1347 if moduleInfoJSON, ok := OtherModuleProvider(ctx, mod, ModuleInfoJSONProvider); ok {
Jihoon Kangd4063812025-01-24 00:25:30 +00001348 *moduleInfoJSONs = append(*moduleInfoJSONs, moduleInfoJSON...)
mrziwang18420972024-09-03 15:12:51 -07001349 }
1350 }
1351
1352 return nil
1353}
1354
1355// Utility funcs to manipulate Android.mk variable entries.
1356
1357// SetString sets a Make variable with the given name to the given value.
1358func (a *AndroidMkInfo) SetString(name, value string) {
1359 if _, ok := a.EntryMap[name]; !ok {
1360 a.EntryOrder = append(a.EntryOrder, name)
1361 }
1362 a.EntryMap[name] = []string{value}
1363}
1364
1365// SetPath sets a Make variable with the given name to the given path string.
1366func (a *AndroidMkInfo) SetPath(name string, path Path) {
1367 if _, ok := a.EntryMap[name]; !ok {
1368 a.EntryOrder = append(a.EntryOrder, name)
1369 }
1370 a.EntryMap[name] = []string{path.String()}
1371}
1372
1373// SetOptionalPath sets a Make variable with the given name to the given path string if it is valid.
1374// It is a no-op if the given path is invalid.
1375func (a *AndroidMkInfo) SetOptionalPath(name string, path OptionalPath) {
1376 if path.Valid() {
1377 a.SetPath(name, path.Path())
1378 }
1379}
1380
1381// AddPath appends the given path string to a Make variable with the given name.
1382func (a *AndroidMkInfo) AddPath(name string, path Path) {
1383 if _, ok := a.EntryMap[name]; !ok {
1384 a.EntryOrder = append(a.EntryOrder, name)
1385 }
1386 a.EntryMap[name] = append(a.EntryMap[name], path.String())
1387}
1388
1389// AddOptionalPath appends the given path string to a Make variable with the given name if it is
1390// valid. It is a no-op if the given path is invalid.
1391func (a *AndroidMkInfo) AddOptionalPath(name string, path OptionalPath) {
1392 if path.Valid() {
1393 a.AddPath(name, path.Path())
1394 }
1395}
1396
1397// SetPaths sets a Make variable with the given name to a slice of the given path strings.
1398func (a *AndroidMkInfo) SetPaths(name string, paths Paths) {
1399 if _, ok := a.EntryMap[name]; !ok {
1400 a.EntryOrder = append(a.EntryOrder, name)
1401 }
1402 a.EntryMap[name] = paths.Strings()
1403}
1404
1405// SetOptionalPaths sets a Make variable with the given name to a slice of the given path strings
1406// only if there are a non-zero amount of paths.
1407func (a *AndroidMkInfo) SetOptionalPaths(name string, paths Paths) {
1408 if len(paths) > 0 {
1409 a.SetPaths(name, paths)
1410 }
1411}
1412
1413// AddPaths appends the given path strings to a Make variable with the given name.
1414func (a *AndroidMkInfo) AddPaths(name string, paths Paths) {
1415 if _, ok := a.EntryMap[name]; !ok {
1416 a.EntryOrder = append(a.EntryOrder, name)
1417 }
1418 a.EntryMap[name] = append(a.EntryMap[name], paths.Strings()...)
1419}
1420
1421// SetBoolIfTrue sets a Make variable with the given name to true if the given flag is true.
1422// It is a no-op if the given flag is false.
1423func (a *AndroidMkInfo) SetBoolIfTrue(name string, flag bool) {
1424 if flag {
1425 if _, ok := a.EntryMap[name]; !ok {
1426 a.EntryOrder = append(a.EntryOrder, name)
1427 }
1428 a.EntryMap[name] = []string{"true"}
1429 }
1430}
1431
1432// SetBool sets a Make variable with the given name to if the given bool flag value.
1433func (a *AndroidMkInfo) SetBool(name string, flag bool) {
1434 if _, ok := a.EntryMap[name]; !ok {
1435 a.EntryOrder = append(a.EntryOrder, name)
1436 }
1437 if flag {
1438 a.EntryMap[name] = []string{"true"}
1439 } else {
1440 a.EntryMap[name] = []string{"false"}
1441 }
1442}
1443
1444// AddStrings appends the given strings to a Make variable with the given name.
1445func (a *AndroidMkInfo) AddStrings(name string, value ...string) {
1446 if len(value) == 0 {
1447 return
1448 }
1449 if _, ok := a.EntryMap[name]; !ok {
1450 a.EntryOrder = append(a.EntryOrder, name)
1451 }
1452 a.EntryMap[name] = append(a.EntryMap[name], value...)
1453}
1454
1455// AddCompatibilityTestSuites adds the supplied test suites to the EntryMap, with special handling
1456// for partial MTS and MCTS test suites.
1457func (a *AndroidMkInfo) AddCompatibilityTestSuites(suites ...string) {
1458 // M(C)TS supports a full test suite and partial per-module MTS test suites, with naming mts-${MODULE}.
1459 // To reduce repetition, if we find a partial M(C)TS test suite without an full M(C)TS test suite,
1460 // we add the full test suite to our list.
1461 if PrefixInList(suites, "mts-") && !InList("mts", suites) {
1462 suites = append(suites, "mts")
1463 }
1464 if PrefixInList(suites, "mcts-") && !InList("mcts", suites) {
1465 suites = append(suites, "mcts")
1466 }
1467 a.AddStrings("LOCAL_COMPATIBILITY_SUITE", suites...)
1468}
1469
Yu Liu64371e02025-02-19 23:44:48 +00001470// TODO(b/397766191): Change the signature to take ModuleProxy
1471// Please only access the module's internal data through providers.
1472func (a *AndroidMkInfo) fillInEntries(ctx fillInEntriesContext, mod Module, commonInfo *CommonModuleInfo) {
mrziwang18420972024-09-03 15:12:51 -07001473 helperInfo := AndroidMkInfo{
1474 EntryMap: make(map[string][]string),
1475 }
1476
Yu Liu64371e02025-02-19 23:44:48 +00001477 name := commonInfo.BaseModuleName
mrziwang18420972024-09-03 15:12:51 -07001478 if a.OverrideName != "" {
1479 name = a.OverrideName
1480 }
1481
1482 if a.Include == "" {
1483 a.Include = "$(BUILD_PREBUILT)"
1484 }
Yu Liu64371e02025-02-19 23:44:48 +00001485 a.Required = append(a.Required, commonInfo.RequiredModuleNames...)
1486 a.Required = append(a.Required, commonInfo.VintfFragmentModuleNames...)
1487 a.Host_required = append(a.Host_required, commonInfo.HostRequiredModuleNames...)
1488 a.Target_required = append(a.Target_required, commonInfo.TargetRequiredModuleNames...)
mrziwang18420972024-09-03 15:12:51 -07001489
Cole Faust5e1454a2025-03-11 15:55:59 -07001490 a.HeaderStrings = append(a.HeaderStrings, a.GetDistForGoals(ctx, mod, commonInfo)...)
Yu Liu64371e02025-02-19 23:44:48 +00001491 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 -07001492
Cole Faust5e1454a2025-03-11 15:55:59 -07001493 // Add the TestSuites from the provider to LOCAL_SOONG_PROVIDER_TEST_SUITES.
1494 // LOCAL_SOONG_PROVIDER_TEST_SUITES will be compared against LOCAL_COMPATIBILITY_SUITES
1495 // in make and enforced they're the same, to ensure we've successfully translated all
1496 // LOCAL_COMPATIBILITY_SUITES usages to the provider.
1497 if testSuiteInfo, ok := OtherModuleProvider(ctx, mod, TestSuiteInfoProvider); ok {
1498 helperInfo.AddStrings("LOCAL_SOONG_PROVIDER_TEST_SUITES", testSuiteInfo.TestSuites...)
1499 }
1500
mrziwang18420972024-09-03 15:12:51 -07001501 // Collect make variable assignment entries.
1502 helperInfo.SetString("LOCAL_PATH", ctx.ModuleDir(mod))
1503 helperInfo.SetString("LOCAL_MODULE", name+a.SubName)
1504 helperInfo.SetString("LOCAL_MODULE_CLASS", a.Class)
1505 helperInfo.SetString("LOCAL_PREBUILT_MODULE_FILE", a.OutputFile.String())
1506 helperInfo.AddStrings("LOCAL_REQUIRED_MODULES", a.Required...)
1507 helperInfo.AddStrings("LOCAL_HOST_REQUIRED_MODULES", a.Host_required...)
1508 helperInfo.AddStrings("LOCAL_TARGET_REQUIRED_MODULES", a.Target_required...)
Yu Liu14b81452025-02-18 23:26:13 +00001509 helperInfo.AddStrings("LOCAL_SOONG_MODULE_TYPE", ctx.ModuleType(mod))
mrziwang18420972024-09-03 15:12:51 -07001510
1511 // If the install rule was generated by Soong tell Make about it.
1512 info := OtherModuleProviderOrDefault(ctx, mod, InstallFilesProvider)
1513 if len(info.KatiInstalls) > 0 {
1514 // Assume the primary install file is last since it probably needs to depend on any other
1515 // installed files. If that is not the case we can add a method to specify the primary
1516 // installed file.
1517 helperInfo.SetPath("LOCAL_SOONG_INSTALLED_MODULE", info.KatiInstalls[len(info.KatiInstalls)-1].to)
1518 helperInfo.SetString("LOCAL_SOONG_INSTALL_PAIRS", info.KatiInstalls.BuiltInstalled())
1519 helperInfo.SetPaths("LOCAL_SOONG_INSTALL_SYMLINKS", info.KatiSymlinks.InstallPaths().Paths())
1520 } else {
1521 // Soong may not have generated the install rule also when `no_full_install: true`.
1522 // Mark this module as uninstallable in order to prevent Make from creating an
1523 // install rule there.
Yu Liu64371e02025-02-19 23:44:48 +00001524 helperInfo.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", commonInfo.NoFullInstall)
mrziwang18420972024-09-03 15:12:51 -07001525 }
1526
Yu Liue70976d2024-10-15 20:45:35 +00001527 if info.UncheckedModule {
1528 helperInfo.SetBool("LOCAL_DONT_CHECK_MODULE", true)
1529 } else if info.CheckbuildTarget != nil {
1530 helperInfo.SetPath("LOCAL_CHECKED_MODULE", info.CheckbuildTarget)
1531 } else {
1532 helperInfo.SetOptionalPath("LOCAL_CHECKED_MODULE", a.OutputFile)
1533 }
1534
mrziwang18420972024-09-03 15:12:51 -07001535 if len(info.TestData) > 0 {
1536 helperInfo.AddStrings("LOCAL_TEST_DATA", androidMkDataPaths(info.TestData)...)
1537 }
1538
Yu Liu64371e02025-02-19 23:44:48 +00001539 if commonInfo.IsApexModule {
1540 helperInfo.SetBoolIfTrue("LOCAL_NOT_AVAILABLE_FOR_PLATFORM", commonInfo.NotAvailableForPlatform)
mrziwang18420972024-09-03 15:12:51 -07001541 }
1542
Yu Liu64371e02025-02-19 23:44:48 +00001543 archStr := commonInfo.Target.Arch.ArchType.String()
mrziwang18420972024-09-03 15:12:51 -07001544 host := false
Yu Liu64371e02025-02-19 23:44:48 +00001545 switch commonInfo.Target.Os.Class {
mrziwang18420972024-09-03 15:12:51 -07001546 case Host:
Yu Liu64371e02025-02-19 23:44:48 +00001547 if commonInfo.Target.HostCross {
mrziwang18420972024-09-03 15:12:51 -07001548 // Make cannot identify LOCAL_MODULE_HOST_CROSS_ARCH:= common.
Yu Liu64371e02025-02-19 23:44:48 +00001549 if commonInfo.Target.Arch.ArchType != Common {
mrziwang18420972024-09-03 15:12:51 -07001550 helperInfo.SetString("LOCAL_MODULE_HOST_CROSS_ARCH", archStr)
1551 }
1552 } else {
1553 // Make cannot identify LOCAL_MODULE_HOST_ARCH:= common.
Yu Liu64371e02025-02-19 23:44:48 +00001554 if commonInfo.Target.Arch.ArchType != Common {
mrziwang18420972024-09-03 15:12:51 -07001555 helperInfo.SetString("LOCAL_MODULE_HOST_ARCH", archStr)
1556 }
1557 }
1558 host = true
1559 case Device:
1560 // Make cannot identify LOCAL_MODULE_TARGET_ARCH:= common.
Yu Liu64371e02025-02-19 23:44:48 +00001561 if commonInfo.Target.Arch.ArchType != Common {
1562 if commonInfo.Target.NativeBridge {
1563 hostArchStr := commonInfo.Target.NativeBridgeHostArchName
mrziwang18420972024-09-03 15:12:51 -07001564 if hostArchStr != "" {
1565 helperInfo.SetString("LOCAL_MODULE_TARGET_ARCH", hostArchStr)
1566 }
1567 } else {
1568 helperInfo.SetString("LOCAL_MODULE_TARGET_ARCH", archStr)
1569 }
1570 }
1571
Yu Liu64371e02025-02-19 23:44:48 +00001572 if !commonInfo.InVendorRamdisk {
mrziwang18420972024-09-03 15:12:51 -07001573 helperInfo.AddPaths("LOCAL_FULL_INIT_RC", info.InitRcPaths)
1574 }
1575 if len(info.VintfFragmentsPaths) > 0 {
1576 helperInfo.AddPaths("LOCAL_FULL_VINTF_FRAGMENTS", info.VintfFragmentsPaths)
1577 }
Yu Liu64371e02025-02-19 23:44:48 +00001578 helperInfo.SetBoolIfTrue("LOCAL_PROPRIETARY_MODULE", commonInfo.Proprietary)
1579 if commonInfo.Vendor || commonInfo.SocSpecific {
mrziwang18420972024-09-03 15:12:51 -07001580 helperInfo.SetString("LOCAL_VENDOR_MODULE", "true")
1581 }
Yu Liu64371e02025-02-19 23:44:48 +00001582 helperInfo.SetBoolIfTrue("LOCAL_ODM_MODULE", commonInfo.DeviceSpecific)
1583 helperInfo.SetBoolIfTrue("LOCAL_PRODUCT_MODULE", commonInfo.ProductSpecific)
1584 helperInfo.SetBoolIfTrue("LOCAL_SYSTEM_EXT_MODULE", commonInfo.SystemExtSpecific)
1585 if commonInfo.Owner != "" {
1586 helperInfo.SetString("LOCAL_MODULE_OWNER", commonInfo.Owner)
mrziwang18420972024-09-03 15:12:51 -07001587 }
1588 }
1589
1590 if host {
Yu Liu64371e02025-02-19 23:44:48 +00001591 os := commonInfo.Target.Os
1592 makeOs := os.String()
1593 if os == Linux || os == LinuxBionic || os == LinuxMusl {
mrziwang18420972024-09-03 15:12:51 -07001594 makeOs = "linux"
1595 }
1596 helperInfo.SetString("LOCAL_MODULE_HOST_OS", makeOs)
1597 helperInfo.SetString("LOCAL_IS_HOST_MODULE", "true")
1598 }
1599
mrziwang18420972024-09-03 15:12:51 -07001600 if licenseMetadata, ok := OtherModuleProvider(ctx, mod, LicenseMetadataProvider); ok {
1601 helperInfo.SetPath("LOCAL_SOONG_LICENSE_METADATA", licenseMetadata.LicenseMetadataPath)
1602 }
1603
1604 if _, ok := OtherModuleProvider(ctx, mod, ModuleInfoJSONProvider); ok {
1605 helperInfo.SetBool("LOCAL_SOONG_MODULE_INFO_JSON", true)
1606 }
1607
1608 a.mergeEntries(&helperInfo)
1609
1610 // Write to footer.
1611 a.FooterStrings = append([]string{"include " + a.Include}, a.FooterStrings...)
1612}
1613
1614// This method merges the entries to helperInfo, then replaces a's EntryMap and
1615// EntryOrder with helperInfo's
1616func (a *AndroidMkInfo) mergeEntries(helperInfo *AndroidMkInfo) {
1617 for _, extraEntry := range a.EntryOrder {
1618 if v, ok := helperInfo.EntryMap[extraEntry]; ok {
1619 v = append(v, a.EntryMap[extraEntry]...)
1620 } else {
1621 helperInfo.EntryMap[extraEntry] = a.EntryMap[extraEntry]
1622 helperInfo.EntryOrder = append(helperInfo.EntryOrder, extraEntry)
1623 }
1624 }
1625 a.EntryOrder = helperInfo.EntryOrder
1626 a.EntryMap = helperInfo.EntryMap
1627}
1628
1629func (a *AndroidMkInfo) disabled() bool {
1630 return a.Disabled || !a.OutputFile.Valid()
1631}
1632
1633// write flushes the AndroidMkEntries's in-struct data populated by AndroidMkEntries into the
1634// given Writer object.
1635func (a *AndroidMkInfo) write(w io.Writer) {
1636 if a.disabled() {
1637 return
1638 }
1639
Yu Liue70976d2024-10-15 20:45:35 +00001640 combinedHeaderString := strings.Join(a.HeaderStrings, "\n") + "\n"
1641 combinedFooterString := strings.Join(a.FooterStrings, "\n") + "\n"
mrziwang18420972024-09-03 15:12:51 -07001642 w.Write([]byte(combinedHeaderString))
1643 for _, name := range a.EntryOrder {
1644 AndroidMkEmitAssignList(w, name, a.EntryMap[name])
1645 }
1646 w.Write([]byte(combinedFooterString))
1647}
1648
1649// Compute the list of Make strings to declare phony goals and dist-for-goals
1650// calls from the module's dist and dists properties.
Yu Liu64371e02025-02-19 23:44:48 +00001651// TODO(b/397766191): Change the signature to take ModuleProxy
1652// Please only access the module's internal data through providers.
1653func (a *AndroidMkInfo) GetDistForGoals(ctx fillInEntriesContext, mod Module, commonInfo *CommonModuleInfo) []string {
Cole Fausta8437c52025-02-25 14:45:43 -08001654 distContributions := getDistContributions(ctx, mod)
mrziwang18420972024-09-03 15:12:51 -07001655 if distContributions == nil {
1656 return nil
1657 }
1658
1659 return generateDistContributionsForMake(distContributions)
1660}
1661
mrziwang18420972024-09-03 15:12:51 -07001662func deepCopyAndroidMkProviderInfo(providerInfo *AndroidMkProviderInfo) AndroidMkProviderInfo {
1663 info := AndroidMkProviderInfo{
1664 PrimaryInfo: deepCopyAndroidMkInfo(&providerInfo.PrimaryInfo),
1665 }
1666 if len(providerInfo.ExtraInfo) > 0 {
1667 for _, i := range providerInfo.ExtraInfo {
1668 info.ExtraInfo = append(info.ExtraInfo, deepCopyAndroidMkInfo(&i))
1669 }
1670 }
1671 return info
1672}
1673
1674func deepCopyAndroidMkInfo(mkinfo *AndroidMkInfo) AndroidMkInfo {
1675 info := AndroidMkInfo{
1676 Class: mkinfo.Class,
1677 SubName: mkinfo.SubName,
1678 OverrideName: mkinfo.OverrideName,
Cole Faustd143f3e2025-02-24 16:18:18 -08001679 // There is no modification on OutputFile, so no need to
mrziwang18420972024-09-03 15:12:51 -07001680 // make their deep copy.
mrziwang18420972024-09-03 15:12:51 -07001681 OutputFile: mkinfo.OutputFile,
1682 Disabled: mkinfo.Disabled,
1683 Include: mkinfo.Include,
1684 Required: deepCopyStringSlice(mkinfo.Required),
1685 Host_required: deepCopyStringSlice(mkinfo.Host_required),
1686 Target_required: deepCopyStringSlice(mkinfo.Target_required),
1687 HeaderStrings: deepCopyStringSlice(mkinfo.HeaderStrings),
1688 FooterStrings: deepCopyStringSlice(mkinfo.FooterStrings),
1689 EntryOrder: deepCopyStringSlice(mkinfo.EntryOrder),
1690 }
1691 info.EntryMap = make(map[string][]string)
1692 for k, v := range mkinfo.EntryMap {
1693 info.EntryMap[k] = deepCopyStringSlice(v)
1694 }
1695
1696 return info
1697}
1698
1699func deepCopyStringSlice(original []string) []string {
1700 result := make([]string, len(original))
1701 copy(result, original)
1702 return result
1703}