blob: e3283594658dd5dbc39d389962cc10efb9e34e72 [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
Bill Yang870dbdc2025-03-20 08:54:11 +0000438 prependProductString := ""
439 if proptools.Bool(dist.Prepend_artifact_with_product) {
440 prependProductString = fmt.Sprintf("%s-", ctx.Config().DeviceProduct())
Trevor Radcliffe90727f42022-03-21 19:34:02 +0000441 }
442
Bill Yang870dbdc2025-03-20 08:54:11 +0000443 appendProductString := ""
444 if proptools.Bool(dist.Append_artifact_with_product) {
445 appendProductString = fmt.Sprintf("_%s", ctx.Config().DeviceProduct())
446 }
447
448 if suffix != "" || appendProductString != "" || prependProductString != "" {
449 dest = prependProductString + strings.TrimSuffix(dest, ext) + suffix + appendProductString + ext
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000450 }
451
452 if dist.Dir != nil {
453 var err error
454 if dest, err = validateSafePath(*dist.Dir, dest); err != nil {
455 // This was checked in ModuleBase.GenerateBuildActions
456 panic(err)
457 }
458 }
459
Paul Duffin8b0349c2020-11-26 14:33:21 +0000460 copiesForGoals.addCopyInstruction(path, dest)
461 }
462 }
463
464 return distContributions
465}
466
467// generateDistContributionsForMake generates make rules that will generate the
468// dist according to the instructions in the supplied distContribution.
469func generateDistContributionsForMake(distContributions *distContributions) []string {
470 var ret []string
471 for _, d := range distContributions.copiesForGoals {
Yu Liue70976d2024-10-15 20:45:35 +0000472 ret = append(ret, fmt.Sprintf(".PHONY: %s", d.goals))
Paul Duffin8b0349c2020-11-26 14:33:21 +0000473 // Create dist-for-goals calls for each of the copy instructions.
474 for _, c := range d.copies {
Bob Badour4660a982022-09-12 16:06:03 -0700475 if distContributions.licenseMetadataFile != nil {
476 ret = append(
477 ret,
Yu Liue70976d2024-10-15 20:45:35 +0000478 fmt.Sprintf("$(if $(strip $(ALL_TARGETS.%s.META_LIC)),,$(eval ALL_TARGETS.%s.META_LIC := %s))",
Bob Badour4660a982022-09-12 16:06:03 -0700479 c.from.String(), c.from.String(), distContributions.licenseMetadataFile.String()))
480 }
Bob Badour51804382022-04-13 11:27:19 -0700481 ret = append(
482 ret,
Yu Liue70976d2024-10-15 20:45:35 +0000483 fmt.Sprintf("$(call dist-for-goals,%s,%s:%s)", d.goals, c.from.String(), c.dest))
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000484 }
485 }
486
487 return ret
488}
489
Paul Duffin8b0349c2020-11-26 14:33:21 +0000490// Compute the list of Make strings to declare phony goals and dist-for-goals
491// calls from the module's dist and dists properties.
Yu Liu14b81452025-02-18 23:26:13 +0000492func (a *AndroidMkEntries) GetDistForGoals(mod Module) []string {
Cole Fausta8437c52025-02-25 14:45:43 -0800493 distContributions := getDistContributions(a.entryContext, mod)
Paul Duffin8b0349c2020-11-26 14:33:21 +0000494 if distContributions == nil {
495 return nil
496 }
497
498 return generateDistContributionsForMake(distContributions)
499}
500
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800501// fillInEntries goes through the common variable processing and calls the extra data funcs to
502// generate and fill in AndroidMkEntries's in-struct data, ready to be flushed to a file.
Colin Crossaa255532020-07-03 13:18:24 -0700503type fillInEntriesContext interface {
504 ModuleDir(module blueprint.Module) string
Cole Faust39aabe92023-02-23 16:57:43 -0800505 ModuleSubDir(module blueprint.Module) string
Colin Crossaa255532020-07-03 13:18:24 -0700506 Config() Config
Yu Liu663e4502024-08-12 18:23:59 +0000507 otherModuleProvider(module blueprint.Module, provider blueprint.AnyProviderKey) (any, bool)
Sasha Smundak5c4729d2022-12-01 10:49:23 -0800508 ModuleType(module blueprint.Module) string
Cole Faust43ddd082024-06-17 12:32:40 -0700509 OtherModulePropertyErrorf(module Module, property string, fmt string, args ...interface{})
Cole Faust4e2bf9f2024-09-11 13:26:20 -0700510 HasMutatorFinished(mutatorName string) bool
Colin Crossaa255532020-07-03 13:18:24 -0700511}
512
Yu Liu14b81452025-02-18 23:26:13 +0000513func (a *AndroidMkEntries) fillInEntries(ctx fillInEntriesContext, mod Module) {
Trevor Radcliffe90727f42022-03-21 19:34:02 +0000514 a.entryContext = ctx
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700515 a.EntryMap = make(map[string][]string)
Yu Liu14b81452025-02-18 23:26:13 +0000516 base := mod.base()
Colin Crossf1f763a2021-10-21 16:14:19 -0700517 name := base.BaseModuleName()
Spandan Dasb9a83f12025-03-20 23:35:47 +0000518 if bmn, ok := mod.(baseModuleName); ok {
519 name = bmn.BaseModuleName()
520 }
Colin Cross0477b422020-10-13 18:43:54 -0700521 if a.OverrideName != "" {
522 name = a.OverrideName
523 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700524
525 if a.Include == "" {
526 a.Include = "$(BUILD_PREBUILT)"
527 }
Yu Liu14b81452025-02-18 23:26:13 +0000528 a.Required = append(a.Required, mod.RequiredModuleNames(ctx)...)
529 a.Required = append(a.Required, mod.VintfFragmentModuleNames(ctx)...)
530 a.Host_required = append(a.Host_required, mod.HostRequiredModuleNames()...)
531 a.Target_required = append(a.Target_required, mod.TargetRequiredModuleNames()...)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700532
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000533 for _, distString := range a.GetDistForGoals(mod) {
Yu Liue70976d2024-10-15 20:45:35 +0000534 fmt.Fprintln(&a.header, distString)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700535 }
536
Cole Faust39aabe92023-02-23 16:57:43 -0800537 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 -0700538
Cole Faust5e1454a2025-03-11 15:55:59 -0700539 // Add the TestSuites from the provider to LOCAL_SOONG_PROVIDER_TEST_SUITES.
540 // LOCAL_SOONG_PROVIDER_TEST_SUITES will be compared against LOCAL_COMPATIBILITY_SUITES
541 // in make and enforced they're the same, to ensure we've successfully translated all
542 // LOCAL_COMPATIBILITY_SUITES usages to the provider.
543 if testSuiteInfo, ok := OtherModuleProvider(ctx, mod, TestSuiteInfoProvider); ok {
544 a.AddStrings("LOCAL_SOONG_PROVIDER_TEST_SUITES", testSuiteInfo.TestSuites...)
545 }
546
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700547 // Collect make variable assignment entries.
Colin Crossaa255532020-07-03 13:18:24 -0700548 a.SetString("LOCAL_PATH", ctx.ModuleDir(mod))
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700549 a.SetString("LOCAL_MODULE", name+a.SubName)
550 a.SetString("LOCAL_MODULE_CLASS", a.Class)
551 a.SetString("LOCAL_PREBUILT_MODULE_FILE", a.OutputFile.String())
552 a.AddStrings("LOCAL_REQUIRED_MODULES", a.Required...)
553 a.AddStrings("LOCAL_HOST_REQUIRED_MODULES", a.Host_required...)
554 a.AddStrings("LOCAL_TARGET_REQUIRED_MODULES", a.Target_required...)
Yu Liu14b81452025-02-18 23:26:13 +0000555 a.AddStrings("LOCAL_SOONG_MODULE_TYPE", ctx.ModuleType(mod))
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700556
Colin Cross6301c3c2021-09-28 17:40:21 -0700557 // If the install rule was generated by Soong tell Make about it.
Yu Liud46e5ae2024-08-15 18:46:17 +0000558 info := OtherModuleProviderOrDefault(ctx, mod, InstallFilesProvider)
559 if len(info.KatiInstalls) > 0 {
Colin Cross6301c3c2021-09-28 17:40:21 -0700560 // Assume the primary install file is last since it probably needs to depend on any other
561 // installed files. If that is not the case we can add a method to specify the primary
562 // installed file.
Yu Liud46e5ae2024-08-15 18:46:17 +0000563 a.SetPath("LOCAL_SOONG_INSTALLED_MODULE", info.KatiInstalls[len(info.KatiInstalls)-1].to)
564 a.SetString("LOCAL_SOONG_INSTALL_PAIRS", info.KatiInstalls.BuiltInstalled())
565 a.SetPaths("LOCAL_SOONG_INSTALL_SYMLINKS", info.KatiSymlinks.InstallPaths().Paths())
Jiyong Park3f627e62024-05-01 16:14:38 +0900566 } else {
567 // Soong may not have generated the install rule also when `no_full_install: true`.
568 // Mark this module as uninstallable in order to prevent Make from creating an
569 // install rule there.
570 a.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", proptools.Bool(base.commonProperties.No_full_install))
Colin Cross6301c3c2021-09-28 17:40:21 -0700571 }
572
Colin Crossa6182ab2024-08-21 10:47:44 -0700573 if info.UncheckedModule {
574 a.SetBool("LOCAL_DONT_CHECK_MODULE", true)
575 } else if info.CheckbuildTarget != nil {
576 a.SetPath("LOCAL_CHECKED_MODULE", info.CheckbuildTarget)
577 } else {
578 a.SetOptionalPath("LOCAL_CHECKED_MODULE", a.OutputFile)
579 }
580
Yu Liud46e5ae2024-08-15 18:46:17 +0000581 if len(info.TestData) > 0 {
582 a.AddStrings("LOCAL_TEST_DATA", androidMkDataPaths(info.TestData)...)
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800583 }
584
Jiyong Park89e850a2020-04-07 16:37:39 +0900585 if am, ok := mod.(ApexModule); ok {
586 a.SetBoolIfTrue("LOCAL_NOT_AVAILABLE_FOR_PLATFORM", am.NotAvailableForPlatform())
587 }
588
Colin Crossf1f763a2021-10-21 16:14:19 -0700589 archStr := base.Arch().ArchType.String()
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700590 host := false
Colin Crossf1f763a2021-10-21 16:14:19 -0700591 switch base.Os().Class {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700592 case Host:
Colin Crossf1f763a2021-10-21 16:14:19 -0700593 if base.Target().HostCross {
Jiyong Park1613e552020-09-14 19:43:17 +0900594 // Make cannot identify LOCAL_MODULE_HOST_CROSS_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_CROSS_ARCH", archStr)
597 }
598 } else {
599 // Make cannot identify LOCAL_MODULE_HOST_ARCH:= common.
Colin Crossf1f763a2021-10-21 16:14:19 -0700600 if base.Arch().ArchType != Common {
Jiyong Park1613e552020-09-14 19:43:17 +0900601 a.SetString("LOCAL_MODULE_HOST_ARCH", archStr)
602 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700603 }
604 host = true
605 case Device:
606 // Make cannot identify LOCAL_MODULE_TARGET_ARCH:= common.
Colin Crossf1f763a2021-10-21 16:14:19 -0700607 if base.Arch().ArchType != Common {
608 if base.Target().NativeBridge {
609 hostArchStr := base.Target().NativeBridgeHostArchName
dimitry1f33e402019-03-26 12:39:31 +0100610 if hostArchStr != "" {
611 a.SetString("LOCAL_MODULE_TARGET_ARCH", hostArchStr)
612 }
613 } else {
614 a.SetString("LOCAL_MODULE_TARGET_ARCH", archStr)
615 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700616 }
617
Kelvin Zhang46977252022-04-13 16:41:34 -0700618 if !base.InVendorRamdisk() {
Yu Liu82a6d142024-08-27 19:02:29 +0000619 a.AddPaths("LOCAL_FULL_INIT_RC", info.InitRcPaths)
Yifan Hong919dae12020-12-02 18:55:06 -0800620 }
Yu Liu82a6d142024-08-27 19:02:29 +0000621 if len(info.VintfFragmentsPaths) > 0 {
622 a.AddPaths("LOCAL_FULL_VINTF_FRAGMENTS", info.VintfFragmentsPaths)
Liz Kammer7b3dc8a2021-04-16 16:41:59 -0400623 }
Colin Crossf1f763a2021-10-21 16:14:19 -0700624 a.SetBoolIfTrue("LOCAL_PROPRIETARY_MODULE", Bool(base.commonProperties.Proprietary))
625 if Bool(base.commonProperties.Vendor) || Bool(base.commonProperties.Soc_specific) {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700626 a.SetString("LOCAL_VENDOR_MODULE", "true")
627 }
Colin Crossf1f763a2021-10-21 16:14:19 -0700628 a.SetBoolIfTrue("LOCAL_ODM_MODULE", Bool(base.commonProperties.Device_specific))
629 a.SetBoolIfTrue("LOCAL_PRODUCT_MODULE", Bool(base.commonProperties.Product_specific))
630 a.SetBoolIfTrue("LOCAL_SYSTEM_EXT_MODULE", Bool(base.commonProperties.System_ext_specific))
631 if base.commonProperties.Owner != nil {
632 a.SetString("LOCAL_MODULE_OWNER", *base.commonProperties.Owner)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700633 }
634 }
635
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700636 if host {
Colin Crossf1f763a2021-10-21 16:14:19 -0700637 makeOs := base.Os().String()
638 if base.Os() == Linux || base.Os() == LinuxBionic || base.Os() == LinuxMusl {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700639 makeOs = "linux"
640 }
641 a.SetString("LOCAL_MODULE_HOST_OS", makeOs)
642 a.SetString("LOCAL_IS_HOST_MODULE", "true")
643 }
644
645 prefix := ""
Colin Crossf1f763a2021-10-21 16:14:19 -0700646 if base.ArchSpecific() {
647 switch base.Os().Class {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700648 case Host:
Colin Crossf1f763a2021-10-21 16:14:19 -0700649 if base.Target().HostCross {
Jiyong Park1613e552020-09-14 19:43:17 +0900650 prefix = "HOST_CROSS_"
651 } else {
652 prefix = "HOST_"
653 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700654 case Device:
655 prefix = "TARGET_"
656
657 }
658
Colin Crossf1f763a2021-10-21 16:14:19 -0700659 if base.Arch().ArchType != ctx.Config().Targets[base.Os()][0].Arch.ArchType {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700660 prefix = "2ND_" + prefix
661 }
662 }
Colin Crossaa255532020-07-03 13:18:24 -0700663
Yu Liu663e4502024-08-12 18:23:59 +0000664 if licenseMetadata, ok := OtherModuleProvider(ctx, mod, LicenseMetadataProvider); ok {
Colin Cross4acaea92021-12-10 23:05:02 +0000665 a.SetPath("LOCAL_SOONG_LICENSE_METADATA", licenseMetadata.LicenseMetadataPath)
666 }
667
Yu Liu663e4502024-08-12 18:23:59 +0000668 if _, ok := OtherModuleProvider(ctx, mod, ModuleInfoJSONProvider); ok {
Colin Crossd6fd0132023-11-06 13:54:06 -0800669 a.SetBool("LOCAL_SOONG_MODULE_INFO_JSON", true)
670 }
671
Colin Crossaa255532020-07-03 13:18:24 -0700672 extraCtx := &androidMkExtraEntriesContext{
673 ctx: ctx,
674 mod: mod,
675 }
676
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700677 for _, extra := range a.ExtraEntries {
Colin Crossaa255532020-07-03 13:18:24 -0700678 extra(extraCtx, a)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700679 }
680
681 // Write to footer.
682 fmt.Fprintln(&a.footer, "include "+a.Include)
Colin Crossaa255532020-07-03 13:18:24 -0700683 blueprintDir := ctx.ModuleDir(mod)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700684 for _, footerFunc := range a.ExtraFooters {
Jaewoong Jung02b11a62020-12-07 10:23:54 -0800685 footerFunc(&a.footer, name, prefix, blueprintDir)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700686 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700687}
688
Colin Crossd6fd0132023-11-06 13:54:06 -0800689func (a *AndroidMkEntries) disabled() bool {
690 return a.Disabled || !a.OutputFile.Valid()
691}
692
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800693// write flushes the AndroidMkEntries's in-struct data populated by AndroidMkEntries into the
694// given Writer object.
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700695func (a *AndroidMkEntries) write(w io.Writer) {
Colin Crossd6fd0132023-11-06 13:54:06 -0800696 if a.disabled() {
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700697 return
698 }
699
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700700 w.Write(a.header.Bytes())
701 for _, name := range a.entryOrder {
Sasha Smundakdcb61292022-12-08 10:41:33 -0800702 AndroidMkEmitAssignList(w, name, a.EntryMap[name])
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700703 }
704 w.Write(a.footer.Bytes())
705}
706
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700707func (a *AndroidMkEntries) FooterLinesForTests() []string {
708 return strings.Split(string(a.footer.Bytes()), "\n")
709}
710
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800711// AndroidMkSingleton is a singleton to collect Android.mk data from all modules and dump them into
712// the final Android-<product_name>.mk file output.
Colin Cross0875c522017-11-28 17:34:01 -0800713func AndroidMkSingleton() Singleton {
Dan Willemsen218f6562015-07-08 18:13:11 -0700714 return &androidMkSingleton{}
715}
716
717type androidMkSingleton struct{}
718
Yu Liu14b81452025-02-18 23:26:13 +0000719func allModulesSorted(ctx SingletonContext) []Module {
720 var allModules []Module
Colin Cross4f6e4e62016-01-11 12:55:55 -0800721
Yu Liu14b81452025-02-18 23:26:13 +0000722 ctx.VisitAllModules(func(module Module) {
Jihoon Kang4be8eb32025-02-10 23:02:42 +0000723 allModules = append(allModules, module)
Colin Cross4f6e4e62016-01-11 12:55:55 -0800724 })
Dan Willemsen218f6562015-07-08 18:13:11 -0700725
Jaewoong Jung7ef4a902020-11-16 12:50:29 -0800726 // Sort the module list by the module names to eliminate random churns, which may erroneously
727 // invoke additional build processes.
Jihoon Kang4be8eb32025-02-10 23:02:42 +0000728 sort.SliceStable(allModules, func(i, j int) bool {
729 return ctx.ModuleName(allModules[i]) < ctx.ModuleName(allModules[j])
Colin Cross1ad81422019-01-14 12:47:35 -0800730 })
Colin Crossd779da42015-12-17 18:00:23 -0800731
Jihoon Kang4be8eb32025-02-10 23:02:42 +0000732 return allModules
733}
734
735func (c *androidMkSingleton) GenerateBuildActions(ctx SingletonContext) {
736 // If running in soong-only mode, more limited version of this singleton is run as
737 // soong only androidmk singleton
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800738 if !ctx.Config().KatiEnabled() {
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800739 return
740 }
741
Dan Willemsen45133ac2018-03-09 21:22:06 -0800742 transMk := PathForOutput(ctx, "Android"+String(ctx.Config().productVariables.Make_suffix)+".mk")
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700743 if ctx.Failed() {
744 return
745 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700746
Colin Crossd6fd0132023-11-06 13:54:06 -0800747 moduleInfoJSON := PathForOutput(ctx, "module-info"+String(ctx.Config().productVariables.Make_suffix)+".json")
748
Jihoon Kang4be8eb32025-02-10 23:02:42 +0000749 err := translateAndroidMk(ctx, absolutePath(transMk.String()), moduleInfoJSON, allModulesSorted(ctx))
Dan Willemsen218f6562015-07-08 18:13:11 -0700750 if err != nil {
751 ctx.Errorf(err.Error())
752 }
753
Colin Cross0875c522017-11-28 17:34:01 -0800754 ctx.Build(pctx, BuildParams{
755 Rule: blueprint.Phony,
756 Output: transMk,
Dan Willemsen218f6562015-07-08 18:13:11 -0700757 })
758}
759
Jihoon Kang4be8eb32025-02-10 23:02:42 +0000760type soongOnlyAndroidMkSingleton struct {
761 Singleton
762}
763
764func soongOnlyAndroidMkSingletonFactory() Singleton {
765 return &soongOnlyAndroidMkSingleton{}
766}
767
768func (so *soongOnlyAndroidMkSingleton) GenerateBuildActions(ctx SingletonContext) {
769 if !ctx.Config().KatiEnabled() {
770 so.soongOnlyBuildActions(ctx, allModulesSorted(ctx))
771 }
772}
773
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800774// In soong-only mode, we don't do most of the androidmk stuff. But disted files are still largely
775// defined through the androidmk mechanisms, so this function is an alternate implementation of
776// the androidmk singleton that just focuses on getting the dist contributions
Yu Liu64371e02025-02-19 23:44:48 +0000777// TODO(b/397766191): Change the signature to take ModuleProxy
778// Please only access the module's internal data through providers.
Yu Liu14b81452025-02-18 23:26:13 +0000779func (so *soongOnlyAndroidMkSingleton) soongOnlyBuildActions(ctx SingletonContext, mods []Module) {
Cole Faustf9a096c2025-01-21 16:55:43 -0800780 allDistContributions, moduleInfoJSONs := getSoongOnlyDataFromMods(ctx, mods)
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800781
Cole Faustf2aab5e2025-02-11 13:32:51 -0800782 singletonDists := getSingletonDists(ctx.Config())
783 singletonDists.lock.Lock()
784 if contribution := distsToDistContributions(singletonDists.dists); contribution != nil {
785 allDistContributions = append(allDistContributions, *contribution)
786 }
787 singletonDists.lock.Unlock()
788
Cole Faustf9a096c2025-01-21 16:55:43 -0800789 // Build module-info.json. Only in builds with HasDeviceProduct(), as we need a named
790 // device to have a TARGET_OUT folder.
791 if ctx.Config().HasDeviceProduct() {
Cole Faust601da062025-01-22 13:15:10 -0800792 preMergePath := PathForOutput(ctx, "module_info_pre_merging.json")
Cole Faustf9a096c2025-01-21 16:55:43 -0800793 moduleInfoJSONPath := pathForInstall(ctx, Android, X86_64, "", "module-info.json")
Cole Faust601da062025-01-22 13:15:10 -0800794 if err := writeModuleInfoJSON(ctx, moduleInfoJSONs, preMergePath); err != nil {
Cole Faustf9a096c2025-01-21 16:55:43 -0800795 ctx.Errorf("%s", err)
796 }
Cole Faust601da062025-01-22 13:15:10 -0800797 builder := NewRuleBuilder(pctx, ctx)
798 builder.Command().
799 BuiltTool("merge_module_info_json").
800 FlagWithOutput("-o ", moduleInfoJSONPath).
801 Input(preMergePath)
802 builder.Build("merge_module_info_json", "merge module info json")
Cole Faustf9a096c2025-01-21 16:55:43 -0800803 ctx.Phony("module-info", moduleInfoJSONPath)
804 ctx.Phony("droidcore-unbundled", moduleInfoJSONPath)
805 allDistContributions = append(allDistContributions, distContributions{
806 copiesForGoals: []*copiesForGoals{{
807 goals: "general-tests droidcore-unbundled",
808 copies: []distCopy{{
809 from: moduleInfoJSONPath,
810 dest: "module-info.json",
811 }},
812 }},
813 })
814 }
815
816 // Build dist.mk for the packaging step to read and generate dist targets
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800817 distMkFile := absolutePath(filepath.Join(ctx.Config().katiPackageMkDir(), "dist.mk"))
818
819 var goalOutputPairs []string
Cole Faust82611a12025-01-09 11:20:41 -0800820 var srcDstPairs []string
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800821 for _, contributions := range allDistContributions {
822 for _, copiesForGoal := range contributions.copiesForGoals {
823 goals := strings.Fields(copiesForGoal.goals)
824 for _, copy := range copiesForGoal.copies {
825 for _, goal := range goals {
Cole Faust82611a12025-01-09 11:20:41 -0800826 goalOutputPairs = append(goalOutputPairs, fmt.Sprintf(" %s:%s", goal, copy.dest))
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800827 }
Cole Faust82611a12025-01-09 11:20:41 -0800828 srcDstPairs = append(srcDstPairs, fmt.Sprintf(" %s:%s", copy.from.String(), copy.dest))
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800829 }
830 }
831 }
Cole Faust82611a12025-01-09 11:20:41 -0800832 // There are duplicates in the lists that we need to remove
833 goalOutputPairs = SortedUniqueStrings(goalOutputPairs)
834 srcDstPairs = SortedUniqueStrings(srcDstPairs)
835 var buf strings.Builder
836 buf.WriteString("DIST_SRC_DST_PAIRS :=")
837 for _, srcDstPair := range srcDstPairs {
838 buf.WriteString(srcDstPair)
839 }
840 buf.WriteString("\nDIST_GOAL_OUTPUT_PAIRS :=")
841 for _, goalOutputPair := range goalOutputPairs {
842 buf.WriteString(goalOutputPair)
843 }
844 buf.WriteString("\n")
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800845
846 writeValueIfChanged(ctx, distMkFile, buf.String())
847}
848
849func writeValueIfChanged(ctx SingletonContext, path string, value string) {
850 if err := os.MkdirAll(filepath.Dir(path), 0777); err != nil {
851 ctx.Errorf("%s\n", err)
852 return
853 }
854 previousValue := ""
855 rawPreviousValue, err := os.ReadFile(path)
856 if err == nil {
857 previousValue = string(rawPreviousValue)
858 }
859
860 if previousValue != value {
861 if err = os.WriteFile(path, []byte(value), 0666); err != nil {
862 ctx.Errorf("Failed to write: %v", err)
863 }
864 }
865}
866
Cole Faustd62a4892025-02-07 16:55:11 -0800867func distsToDistContributions(dists []dist) *distContributions {
868 if len(dists) == 0 {
Jihoon Kang593171e2025-02-05 01:54:45 +0000869 return nil
870 }
871
872 copyGoals := []*copiesForGoals{}
Cole Faustd62a4892025-02-07 16:55:11 -0800873 for _, dist := range dists {
Jihoon Kang593171e2025-02-05 01:54:45 +0000874 for _, goal := range dist.goals {
Cole Faustd62a4892025-02-07 16:55:11 -0800875 copyGoals = append(copyGoals, &copiesForGoals{
876 goals: goal,
877 copies: dist.paths,
878 })
Jihoon Kang593171e2025-02-05 01:54:45 +0000879 }
880 }
881
Cole Faustd62a4892025-02-07 16:55:11 -0800882 return &distContributions{
883 copiesForGoals: copyGoals,
884 }
Jihoon Kang593171e2025-02-05 01:54:45 +0000885}
886
Cole Faustf9a096c2025-01-21 16:55:43 -0800887// getSoongOnlyDataFromMods gathers data from the given modules needed in soong-only builds.
888// Currently, this is the dist contributions, and the module-info.json contents.
Yu Liu14b81452025-02-18 23:26:13 +0000889func getSoongOnlyDataFromMods(ctx fillInEntriesContext, mods []Module) ([]distContributions, []*ModuleInfoJSON) {
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800890 var allDistContributions []distContributions
Cole Faustf9a096c2025-01-21 16:55:43 -0800891 var moduleInfoJSONs []*ModuleInfoJSON
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800892 for _, mod := range mods {
Cole Faustd62a4892025-02-07 16:55:11 -0800893 if distInfo, ok := OtherModuleProvider(ctx, mod, DistProvider); ok {
894 if contribution := distsToDistContributions(distInfo.Dists); contribution != nil {
895 allDistContributions = append(allDistContributions, *contribution)
896 }
897 }
898
Yu Liuf22120f2025-03-13 18:36:35 +0000899 commonInfo := OtherModulePointerProviderOrDefault(ctx, mod, CommonModuleInfoProvider)
Yu Liu64371e02025-02-19 23:44:48 +0000900 if commonInfo.SkipAndroidMkProcessing {
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800901 continue
902 }
903 if info, ok := OtherModuleProvider(ctx, mod, AndroidMkInfoProvider); ok {
Cole Faust556f1f42025-01-09 10:49:42 -0800904 // Deep copy the provider info since we need to modify the info later
905 info := deepCopyAndroidMkProviderInfo(info)
Yu Liuf22120f2025-03-13 18:36:35 +0000906 info.PrimaryInfo.fillInEntries(ctx, mod, commonInfo)
Cole Faust556f1f42025-01-09 10:49:42 -0800907 if info.PrimaryInfo.disabled() {
908 continue
909 }
Cole Faustf9a096c2025-01-21 16:55:43 -0800910 if moduleInfoJSON, ok := OtherModuleProvider(ctx, mod, ModuleInfoJSONProvider); ok {
Jihoon Kangd4063812025-01-24 00:25:30 +0000911 moduleInfoJSONs = append(moduleInfoJSONs, moduleInfoJSON...)
Cole Faustf9a096c2025-01-21 16:55:43 -0800912 }
Cole Fausta8437c52025-02-25 14:45:43 -0800913 if contribution := getDistContributions(ctx, mod); contribution != nil {
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800914 allDistContributions = append(allDistContributions, *contribution)
915 }
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800916 } else {
Jihoon Kange6daf662025-02-06 01:38:16 +0000917 if x, ok := mod.(AndroidMkDataProvider); ok {
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800918 data := x.AndroidMk()
919
920 if data.Include == "" {
921 data.Include = "$(BUILD_PREBUILT)"
922 }
923
924 data.fillInData(ctx, mod)
Cole Faust556f1f42025-01-09 10:49:42 -0800925 if data.Entries.disabled() {
926 continue
927 }
Cole Faustf9a096c2025-01-21 16:55:43 -0800928 if moduleInfoJSON, ok := OtherModuleProvider(ctx, mod, ModuleInfoJSONProvider); ok {
Jihoon Kangd4063812025-01-24 00:25:30 +0000929 moduleInfoJSONs = append(moduleInfoJSONs, moduleInfoJSON...)
Cole Faustf9a096c2025-01-21 16:55:43 -0800930 }
Cole Fausta8437c52025-02-25 14:45:43 -0800931 if contribution := getDistContributions(ctx, mod); contribution != nil {
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800932 allDistContributions = append(allDistContributions, *contribution)
933 }
Jihoon Kange6daf662025-02-06 01:38:16 +0000934 }
935 if x, ok := mod.(AndroidMkEntriesProvider); ok {
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800936 entriesList := x.AndroidMkEntries()
937 for _, entries := range entriesList {
938 entries.fillInEntries(ctx, mod)
Cole Faust556f1f42025-01-09 10:49:42 -0800939 if entries.disabled() {
940 continue
941 }
Cole Faustf9a096c2025-01-21 16:55:43 -0800942 if moduleInfoJSON, ok := OtherModuleProvider(ctx, mod, ModuleInfoJSONProvider); ok {
Jihoon Kangd4063812025-01-24 00:25:30 +0000943 moduleInfoJSONs = append(moduleInfoJSONs, moduleInfoJSON...)
Cole Faustf9a096c2025-01-21 16:55:43 -0800944 }
Cole Fausta8437c52025-02-25 14:45:43 -0800945 if contribution := getDistContributions(ctx, mod); contribution != nil {
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800946 allDistContributions = append(allDistContributions, *contribution)
947 }
948 }
Jihoon Kange6daf662025-02-06 01:38:16 +0000949 }
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800950 }
951 }
Cole Faustf9a096c2025-01-21 16:55:43 -0800952 return allDistContributions, moduleInfoJSONs
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800953}
954
Yu Liu14b81452025-02-18 23:26:13 +0000955func translateAndroidMk(ctx SingletonContext, absMkFile string, moduleInfoJSONPath WritablePath, mods []Module) error {
Dan Willemsen218f6562015-07-08 18:13:11 -0700956 buf := &bytes.Buffer{}
957
Colin Crossd6fd0132023-11-06 13:54:06 -0800958 var moduleInfoJSONs []*ModuleInfoJSON
959
Dan Willemsen97750522016-02-09 17:43:51 -0800960 fmt.Fprintln(buf, "LOCAL_MODULE_MAKEFILE := $(lastword $(MAKEFILE_LIST))")
Dan Willemsen218f6562015-07-08 18:13:11 -0700961
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800962 typeStats := make(map[string]int)
Dan Willemsen218f6562015-07-08 18:13:11 -0700963 for _, mod := range mods {
Colin Crossd6fd0132023-11-06 13:54:06 -0800964 err := translateAndroidMkModule(ctx, buf, &moduleInfoJSONs, mod)
Dan Willemsen218f6562015-07-08 18:13:11 -0700965 if err != nil {
Colin Cross836e3872021-11-09 12:30:59 -0800966 os.Remove(absMkFile)
Dan Willemsen218f6562015-07-08 18:13:11 -0700967 return err
968 }
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700969
Yu Liu14b81452025-02-18 23:26:13 +0000970 if ctx.PrimaryModule(mod) == mod {
971 typeStats[ctx.ModuleType(mod)] += 1
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700972 }
973 }
974
975 keys := []string{}
976 fmt.Fprintln(buf, "\nSTATS.SOONG_MODULE_TYPE :=")
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800977 for k := range typeStats {
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700978 keys = append(keys, k)
979 }
980 sort.Strings(keys)
981 for _, mod_type := range keys {
982 fmt.Fprintln(buf, "STATS.SOONG_MODULE_TYPE +=", mod_type)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800983 fmt.Fprintf(buf, "STATS.SOONG_MODULE_TYPE.%s := %d\n", mod_type, typeStats[mod_type])
Dan Willemsen218f6562015-07-08 18:13:11 -0700984 }
985
Colin Crossd6fd0132023-11-06 13:54:06 -0800986 err := pathtools.WriteFileIfChanged(absMkFile, buf.Bytes(), 0666)
987 if err != nil {
988 return err
989 }
990
991 return writeModuleInfoJSON(ctx, moduleInfoJSONs, moduleInfoJSONPath)
Dan Willemsen218f6562015-07-08 18:13:11 -0700992}
993
Colin Crossd6fd0132023-11-06 13:54:06 -0800994func writeModuleInfoJSON(ctx SingletonContext, moduleInfoJSONs []*ModuleInfoJSON, moduleInfoJSONPath WritablePath) error {
995 moduleInfoJSONBuf := &strings.Builder{}
996 moduleInfoJSONBuf.WriteString("[")
997 for i, moduleInfoJSON := range moduleInfoJSONs {
998 if i != 0 {
999 moduleInfoJSONBuf.WriteString(",\n")
1000 }
1001 moduleInfoJSONBuf.WriteString("{")
1002 moduleInfoJSONBuf.WriteString(strconv.Quote(moduleInfoJSON.core.RegisterName))
1003 moduleInfoJSONBuf.WriteString(":")
1004 err := encodeModuleInfoJSON(moduleInfoJSONBuf, moduleInfoJSON)
1005 moduleInfoJSONBuf.WriteString("}")
1006 if err != nil {
1007 return err
1008 }
1009 }
1010 moduleInfoJSONBuf.WriteString("]")
1011 WriteFileRule(ctx, moduleInfoJSONPath, moduleInfoJSONBuf.String())
1012 return nil
1013}
1014
Yu Liu14b81452025-02-18 23:26:13 +00001015func translateAndroidMkModule(ctx SingletonContext, w io.Writer, moduleInfoJSONs *[]*ModuleInfoJSON, mod Module) error {
Colin Cross953d3a22018-09-05 16:23:54 -07001016 defer func() {
1017 if r := recover(); r != nil {
1018 panic(fmt.Errorf("%s in translateAndroidMkModule for module %s variant %s",
1019 r, ctx.ModuleName(mod), ctx.ModuleSubDir(mod)))
1020 }
1021 }()
1022
Bob Badourb4999222021-01-07 03:34:31 +00001023 // Additional cases here require review for correct license propagation to make.
Colin Crossd6fd0132023-11-06 13:54:06 -08001024 var err error
mrziwang18420972024-09-03 15:12:51 -07001025
Yu Liue70976d2024-10-15 20:45:35 +00001026 if info, ok := OtherModuleProvider(ctx, mod, AndroidMkInfoProvider); ok {
1027 err = translateAndroidMkEntriesInfoModule(ctx, w, moduleInfoJSONs, mod, info)
mrziwang18420972024-09-03 15:12:51 -07001028 } else {
1029 switch x := mod.(type) {
1030 case AndroidMkDataProvider:
1031 err = translateAndroidModule(ctx, w, moduleInfoJSONs, mod, x)
mrziwang18420972024-09-03 15:12:51 -07001032 case AndroidMkEntriesProvider:
1033 err = translateAndroidMkEntriesModule(ctx, w, moduleInfoJSONs, mod, x)
1034 default:
1035 // Not exported to make so no make variables to set.
1036 }
Dan Willemsen218f6562015-07-08 18:13:11 -07001037 }
Colin Crossd6fd0132023-11-06 13:54:06 -08001038
1039 if err != nil {
1040 return err
1041 }
1042
1043 return err
Colin Cross2465c3d2018-09-28 10:19:18 -07001044}
1045
Yu Liu14b81452025-02-18 23:26:13 +00001046func (data *AndroidMkData) fillInData(ctx fillInEntriesContext, mod Module) {
Jooyung Han12df5fb2019-07-11 16:18:47 +09001047 // Get the preamble content through AndroidMkEntries logic.
Jooyung Han2ed99d02020-06-24 23:26:26 +09001048 data.Entries = AndroidMkEntries{
Jooyung Han12df5fb2019-07-11 16:18:47 +09001049 Class: data.Class,
1050 SubName: data.SubName,
Jooyung Han12df5fb2019-07-11 16:18:47 +09001051 OutputFile: data.OutputFile,
1052 Disabled: data.Disabled,
1053 Include: data.Include,
1054 Required: data.Required,
1055 Host_required: data.Host_required,
1056 Target_required: data.Target_required,
1057 }
Colin Crossaa255532020-07-03 13:18:24 -07001058 data.Entries.fillInEntries(ctx, mod)
Jooyung Han12df5fb2019-07-11 16:18:47 +09001059
1060 // copy entries back to data since it is used in Custom
Jooyung Han2ed99d02020-06-24 23:26:26 +09001061 data.Required = data.Entries.Required
1062 data.Host_required = data.Entries.Host_required
1063 data.Target_required = data.Entries.Target_required
Jooyung Han12df5fb2019-07-11 16:18:47 +09001064}
1065
Jaewoong Jung7ef4a902020-11-16 12:50:29 -08001066// A support func for the deprecated AndroidMkDataProvider interface. Use AndroidMkEntryProvider
1067// instead.
Colin Crossd6fd0132023-11-06 13:54:06 -08001068func translateAndroidModule(ctx SingletonContext, w io.Writer, moduleInfoJSONs *[]*ModuleInfoJSON,
Yu Liu14b81452025-02-18 23:26:13 +00001069 mod Module, provider AndroidMkDataProvider) error {
Dan Willemsen218f6562015-07-08 18:13:11 -07001070
Yu Liu14b81452025-02-18 23:26:13 +00001071 amod := mod.base()
Cole Fausta963b942024-04-11 17:43:00 -07001072 if shouldSkipAndroidMkProcessing(ctx, amod) {
Jeff Gaston088e29e2017-11-29 16:47:17 -08001073 return nil
1074 }
1075
Colin Cross91825d22017-08-10 16:59:47 -07001076 data := provider.AndroidMk()
Yu Liuddc28332024-08-09 22:48:30 +00001077
Colin Cross53499412017-09-07 13:20:25 -07001078 if data.Include == "" {
1079 data.Include = "$(BUILD_PREBUILT)"
1080 }
1081
Colin Crossaa255532020-07-03 13:18:24 -07001082 data.fillInData(ctx, mod)
Yu Liu14b81452025-02-18 23:26:13 +00001083 aconfigUpdateAndroidMkData(ctx, mod, &data)
Dan Willemsen01a405a2016-06-13 17:19:03 -07001084
Colin Cross0f86d182017-08-10 17:07:28 -07001085 prefix := ""
1086 if amod.ArchSpecific() {
1087 switch amod.Os().Class {
1088 case Host:
Jiyong Park1613e552020-09-14 19:43:17 +09001089 if amod.Target().HostCross {
1090 prefix = "HOST_CROSS_"
1091 } else {
1092 prefix = "HOST_"
1093 }
Colin Cross0f86d182017-08-10 17:07:28 -07001094 case Device:
1095 prefix = "TARGET_"
Colin Crossa2344662016-03-24 13:14:12 -07001096
Dan Willemsen218f6562015-07-08 18:13:11 -07001097 }
1098
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001099 if amod.Arch().ArchType != ctx.Config().Targets[amod.Os()][0].Arch.ArchType {
Colin Cross0f86d182017-08-10 17:07:28 -07001100 prefix = "2ND_" + prefix
1101 }
Dan Willemsen218f6562015-07-08 18:13:11 -07001102 }
1103
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -07001104 name := provider.BaseModuleName()
Colin Cross0f86d182017-08-10 17:07:28 -07001105 blueprintDir := filepath.Dir(ctx.BlueprintFile(mod))
1106
1107 if data.Custom != nil {
Bob Badourb4999222021-01-07 03:34:31 +00001108 // List of module types allowed to use .Custom(...)
1109 // Additions to the list require careful review for proper license handling.
Colin Crossaa255532020-07-03 13:18:24 -07001110 switch reflect.TypeOf(mod).String() { // ctx.ModuleType(mod) doesn't work: aidl_interface creates phony without type
Bob Badourb4999222021-01-07 03:34:31 +00001111 case "*aidl.aidlApi": // writes non-custom before adding .phony
1112 case "*aidl.aidlMapping": // writes non-custom before adding .phony
1113 case "*android.customModule": // appears in tests only
Dan Willemsen9fe14102021-07-13 21:52:04 -07001114 case "*android_sdk.sdkRepoHost": // doesn't go through base_rules
Bob Badourb4999222021-01-07 03:34:31 +00001115 case "*apex.apexBundle": // license properties written
1116 case "*bpf.bpf": // license properties written (both for module and objs)
Neill Kapron41efab72024-07-31 22:17:36 +00001117 case "*libbpf_prog.libbpfProg": // license properties written (both for module and objs)
Bob Badourb4999222021-01-07 03:34:31 +00001118 case "*genrule.Module": // writes non-custom before adding .phony
1119 case "*java.SystemModules": // doesn't go through base_rules
1120 case "*java.systemModulesImport": // doesn't go through base_rules
1121 case "*phony.phony": // license properties written
Nelson Lif3c70682023-12-20 02:37:52 +00001122 case "*phony.PhonyRule": // writes phony deps and acts like `.PHONY`
Bob Badourb4999222021-01-07 03:34:31 +00001123 case "*selinux.selinuxContextsModule": // license properties written
1124 case "*sysprop.syspropLibrary": // license properties written
Bill Yang3b3aac02024-09-05 09:22:09 +00001125 case "*vintf.vintfCompatibilityMatrixRule": // use case like phony
Bob Badourb4999222021-01-07 03:34:31 +00001126 default:
Bob Badour65ee90a2021-09-02 15:33:10 -07001127 if !ctx.Config().IsEnvFalse("ANDROID_REQUIRE_LICENSES") {
Bob Badourb4999222021-01-07 03:34:31 +00001128 return fmt.Errorf("custom make rules not allowed for %q (%q) module %q", ctx.ModuleType(mod), reflect.TypeOf(mod), ctx.ModuleName(mod))
1129 }
1130 }
Colin Cross0f86d182017-08-10 17:07:28 -07001131 data.Custom(w, name, prefix, blueprintDir, data)
1132 } else {
1133 WriteAndroidMkData(w, data)
1134 }
1135
Colin Crossd6fd0132023-11-06 13:54:06 -08001136 if !data.Entries.disabled() {
Yu Liu663e4502024-08-12 18:23:59 +00001137 if moduleInfoJSON, ok := OtherModuleProvider(ctx, mod, ModuleInfoJSONProvider); ok {
Jihoon Kangd4063812025-01-24 00:25:30 +00001138 *moduleInfoJSONs = append(*moduleInfoJSONs, moduleInfoJSON...)
Colin Crossd6fd0132023-11-06 13:54:06 -08001139 }
1140 }
1141
Colin Cross0f86d182017-08-10 17:07:28 -07001142 return nil
1143}
1144
Jaewoong Jung7ef4a902020-11-16 12:50:29 -08001145// A support func for the deprecated AndroidMkDataProvider interface. Use AndroidMkEntryProvider
1146// instead.
Colin Cross0f86d182017-08-10 17:07:28 -07001147func WriteAndroidMkData(w io.Writer, data AndroidMkData) {
Colin Crossd6fd0132023-11-06 13:54:06 -08001148 if data.Entries.disabled() {
Colin Cross0f86d182017-08-10 17:07:28 -07001149 return
1150 }
1151
Jooyung Han2ed99d02020-06-24 23:26:26 +09001152 // write preamble via Entries
1153 data.Entries.footer = bytes.Buffer{}
1154 data.Entries.write(w)
Colin Cross0f86d182017-08-10 17:07:28 -07001155
Colin Crossca860ac2016-01-04 14:34:37 -08001156 for _, extra := range data.Extra {
Colin Cross27a4b052017-08-10 16:32:23 -07001157 extra(w, data.OutputFile.Path())
Dan Willemsen97750522016-02-09 17:43:51 -08001158 }
1159
Colin Cross53499412017-09-07 13:20:25 -07001160 fmt.Fprintln(w, "include "+data.Include)
Dan Willemsen218f6562015-07-08 18:13:11 -07001161}
Sasha Smundakb6d23052019-04-01 18:37:36 -07001162
Colin Crossd6fd0132023-11-06 13:54:06 -08001163func translateAndroidMkEntriesModule(ctx SingletonContext, w io.Writer, moduleInfoJSONs *[]*ModuleInfoJSON,
Yu Liu14b81452025-02-18 23:26:13 +00001164 mod Module, provider AndroidMkEntriesProvider) error {
1165 if shouldSkipAndroidMkProcessing(ctx, mod.base()) {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -07001166 return nil
Sasha Smundakb6d23052019-04-01 18:37:36 -07001167 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -07001168
Colin Crossd6fd0132023-11-06 13:54:06 -08001169 entriesList := provider.AndroidMkEntries()
Yu Liu14b81452025-02-18 23:26:13 +00001170 aconfigUpdateAndroidMkEntries(ctx, mod, &entriesList)
Colin Crossd6fd0132023-11-06 13:54:06 -08001171
Jihoon Kangd4063812025-01-24 00:25:30 +00001172 moduleInfoJSON, providesModuleInfoJSON := OtherModuleProvider(ctx, mod, ModuleInfoJSONProvider)
1173
Bob Badourb4999222021-01-07 03:34:31 +00001174 // Any new or special cases here need review to verify correct propagation of license information.
Colin Crossd6fd0132023-11-06 13:54:06 -08001175 for _, entries := range entriesList {
Colin Crossaa255532020-07-03 13:18:24 -07001176 entries.fillInEntries(ctx, mod)
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001177 entries.write(w)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -07001178
Jihoon Kangd4063812025-01-24 00:25:30 +00001179 if providesModuleInfoJSON && !entries.disabled() {
1180 // append only the name matching moduleInfoJSON entry
1181 for _, m := range moduleInfoJSON {
1182 if m.RegisterNameOverride == entries.OverrideName && m.SubName == entries.SubName {
1183 *moduleInfoJSONs = append(*moduleInfoJSONs, m)
1184 }
1185 }
Colin Crossd6fd0132023-11-06 13:54:06 -08001186 }
1187 }
1188
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -07001189 return nil
1190}
1191
Cole Fauste8a87832024-09-11 11:35:46 -07001192func ShouldSkipAndroidMkProcessing(ctx ConfigurableEvaluatorContext, module Module) bool {
Cole Fausta963b942024-04-11 17:43:00 -07001193 return shouldSkipAndroidMkProcessing(ctx, module.base())
Chih-Hung Hsieh80783772021-10-11 16:46:56 -07001194}
1195
Cole Fauste8a87832024-09-11 11:35:46 -07001196func shouldSkipAndroidMkProcessing(ctx ConfigurableEvaluatorContext, module *ModuleBase) bool {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -07001197 if !module.commonProperties.NamespaceExportedToMake {
1198 // TODO(jeffrygaston) do we want to validate that there are no modules being
1199 // exported to Kati that depend on this module?
1200 return true
Sasha Smundakb6d23052019-04-01 18:37:36 -07001201 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -07001202
Dan Willemsendef7b5d2021-10-17 00:22:33 -07001203 // On Mac, only expose host darwin modules to Make, as that's all we claim to support.
1204 // In reality, some of them depend on device-built (Java) modules, so we can't disable all
1205 // device modules in Soong, but we can hide them from Make (and thus the build user interface)
1206 if runtime.GOOS == "darwin" && module.Os() != Darwin {
1207 return true
1208 }
1209
Dan Willemsen8528f4e2021-10-19 00:22:06 -07001210 // Only expose the primary Darwin target, as Make does not understand Darwin+Arm64
1211 if module.Os() == Darwin && module.Target().HostCross {
1212 return true
1213 }
1214
Cole Fausta963b942024-04-11 17:43:00 -07001215 return !module.Enabled(ctx) ||
Colin Crossa9c8c9f2020-12-16 10:20:23 -08001216 module.commonProperties.HideFromMake ||
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -07001217 // Make does not understand LinuxBionic
Colin Cross9a027be2022-06-24 18:45:58 -07001218 module.Os() == LinuxBionic ||
1219 // Make does not understand LinuxMusl, except when we are building with USE_HOST_MUSL=true
1220 // and all host binaries are LinuxMusl
1221 (module.Os() == LinuxMusl && module.Target().HostCross)
Sasha Smundakb6d23052019-04-01 18:37:36 -07001222}
Dan Shi31949122020-09-21 12:11:02 -07001223
Jaewoong Jung7ef4a902020-11-16 12:50:29 -08001224// A utility func to format LOCAL_TEST_DATA outputs. See the comments on DataPath to understand how
1225// to use this func.
Colin Cross5c1d5fb2023-11-15 12:39:40 -08001226func androidMkDataPaths(data []DataPath) []string {
Dan Shi31949122020-09-21 12:11:02 -07001227 var testFiles []string
1228 for _, d := range data {
1229 rel := d.SrcPath.Rel()
Colin Cross5c1d5fb2023-11-15 12:39:40 -08001230 if d.WithoutRel {
1231 rel = d.SrcPath.Base()
1232 }
Dan Shi31949122020-09-21 12:11:02 -07001233 path := d.SrcPath.String()
Jaewoong Jung7ef4a902020-11-16 12:50:29 -08001234 // LOCAL_TEST_DATA requires the rel portion of the path to be removed from the path.
Dan Shi31949122020-09-21 12:11:02 -07001235 if !strings.HasSuffix(path, rel) {
1236 panic(fmt.Errorf("path %q does not end with %q", path, rel))
1237 }
1238 path = strings.TrimSuffix(path, rel)
1239 testFileString := path + ":" + rel
1240 if len(d.RelativeInstallPath) > 0 {
1241 testFileString += ":" + d.RelativeInstallPath
1242 }
1243 testFiles = append(testFiles, testFileString)
1244 }
1245 return testFiles
1246}
Sasha Smundakdcb61292022-12-08 10:41:33 -08001247
1248// AndroidMkEmitAssignList emits the line
1249//
1250// VAR := ITEM ...
1251//
1252// Items are the elements to the given set of lists
1253// If all the passed lists are empty, no line will be emitted
1254func AndroidMkEmitAssignList(w io.Writer, varName string, lists ...[]string) {
1255 doPrint := false
1256 for _, l := range lists {
1257 if doPrint = len(l) > 0; doPrint {
1258 break
1259 }
1260 }
1261 if !doPrint {
1262 return
1263 }
1264 fmt.Fprint(w, varName, " :=")
1265 for _, l := range lists {
1266 for _, item := range l {
1267 fmt.Fprint(w, " ", item)
1268 }
1269 }
1270 fmt.Fprintln(w)
1271}
mrziwang18420972024-09-03 15:12:51 -07001272
1273type AndroidMkProviderInfo struct {
1274 PrimaryInfo AndroidMkInfo
1275 ExtraInfo []AndroidMkInfo
1276}
1277
1278type AndroidMkInfo struct {
1279 // Android.mk class string, e.g. EXECUTABLES, JAVA_LIBRARIES, ETC
1280 Class string
1281 // Optional suffix to append to the module name. Useful when a module wants to return multiple
1282 // AndroidMkEntries objects. For example, when a java_library returns an additional entry for
1283 // its hostdex sub-module, this SubName field is set to "-hostdex" so that it can have a
1284 // different name than the parent's.
1285 SubName string
1286 // If set, this value overrides the base module name. SubName is still appended.
1287 OverrideName string
mrziwang18420972024-09-03 15:12:51 -07001288 // The output file for Kati to process and/or install. If absent, the module is skipped.
1289 OutputFile OptionalPath
1290 // If true, the module is skipped and does not appear on the final Android-<product name>.mk
1291 // file. Useful when a module needs to be skipped conditionally.
1292 Disabled bool
1293 // The postprocessing mk file to include, e.g. $(BUILD_SYSTEM)/soong_cc_rust_prebuilt.mk
1294 // If not set, $(BUILD_SYSTEM)/prebuilt.mk is used.
1295 Include string
1296 // Required modules that need to be built and included in the final build output when building
1297 // this module.
1298 Required []string
1299 // Required host modules that need to be built and included in the final build output when
1300 // building this module.
1301 Host_required []string
1302 // Required device modules that need to be built and included in the final build output when
1303 // building this module.
1304 Target_required []string
1305
1306 HeaderStrings []string
1307 FooterStrings []string
1308
1309 // A map that holds the up-to-date Make variable values. Can be accessed from tests.
1310 EntryMap map[string][]string
1311 // A list of EntryMap keys in insertion order. This serves a few purposes:
1312 // 1. Prevents churns. Golang map doesn't provide consistent iteration order, so without this,
1313 // the outputted Android-*.mk file may change even though there have been no content changes.
1314 // 2. Allows modules to refer to other variables, like LOCAL_BAR_VAR := $(LOCAL_FOO_VAR),
1315 // without worrying about the variables being mixed up in the actual mk file.
1316 // 3. Makes troubleshooting and spotting errors easier.
1317 EntryOrder []string
1318}
1319
Yu Liue70976d2024-10-15 20:45:35 +00001320type AndroidMkProviderInfoProducer interface {
1321 PrepareAndroidMKProviderInfo(config Config) *AndroidMkProviderInfo
1322}
1323
mrziwang18420972024-09-03 15:12:51 -07001324// TODO: rename it to AndroidMkEntriesProvider after AndroidMkEntriesProvider interface is gone.
1325var AndroidMkInfoProvider = blueprint.NewProvider[*AndroidMkProviderInfo]()
1326
Yu Liu64371e02025-02-19 23:44:48 +00001327// TODO(b/397766191): Change the signature to take ModuleProxy
1328// Please only access the module's internal data through providers.
mrziwang18420972024-09-03 15:12:51 -07001329func translateAndroidMkEntriesInfoModule(ctx SingletonContext, w io.Writer, moduleInfoJSONs *[]*ModuleInfoJSON,
Yu Liu14b81452025-02-18 23:26:13 +00001330 mod Module, providerInfo *AndroidMkProviderInfo) error {
Yu Liuf22120f2025-03-13 18:36:35 +00001331 commonInfo := OtherModulePointerProviderOrDefault(ctx, mod, CommonModuleInfoProvider)
Yu Liu64371e02025-02-19 23:44:48 +00001332 if commonInfo.SkipAndroidMkProcessing {
mrziwang18420972024-09-03 15:12:51 -07001333 return nil
1334 }
1335
1336 // Deep copy the provider info since we need to modify the info later
1337 info := deepCopyAndroidMkProviderInfo(providerInfo)
1338
Yu Liu14b81452025-02-18 23:26:13 +00001339 aconfigUpdateAndroidMkInfos(ctx, mod, &info)
mrziwang18420972024-09-03 15:12:51 -07001340
1341 // Any new or special cases here need review to verify correct propagation of license information.
Yu Liuf22120f2025-03-13 18:36:35 +00001342 info.PrimaryInfo.fillInEntries(ctx, mod, commonInfo)
mrziwang18420972024-09-03 15:12:51 -07001343 info.PrimaryInfo.write(w)
1344 if len(info.ExtraInfo) > 0 {
1345 for _, ei := range info.ExtraInfo {
Yu Liuf22120f2025-03-13 18:36:35 +00001346 ei.fillInEntries(ctx, mod, commonInfo)
mrziwang18420972024-09-03 15:12:51 -07001347 ei.write(w)
1348 }
1349 }
1350
1351 if !info.PrimaryInfo.disabled() {
1352 if moduleInfoJSON, ok := OtherModuleProvider(ctx, mod, ModuleInfoJSONProvider); ok {
Jihoon Kangd4063812025-01-24 00:25:30 +00001353 *moduleInfoJSONs = append(*moduleInfoJSONs, moduleInfoJSON...)
mrziwang18420972024-09-03 15:12:51 -07001354 }
1355 }
1356
1357 return nil
1358}
1359
1360// Utility funcs to manipulate Android.mk variable entries.
1361
1362// SetString sets a Make variable with the given name to the given value.
1363func (a *AndroidMkInfo) SetString(name, value string) {
1364 if _, ok := a.EntryMap[name]; !ok {
1365 a.EntryOrder = append(a.EntryOrder, name)
1366 }
1367 a.EntryMap[name] = []string{value}
1368}
1369
1370// SetPath sets a Make variable with the given name to the given path string.
1371func (a *AndroidMkInfo) SetPath(name string, path Path) {
1372 if _, ok := a.EntryMap[name]; !ok {
1373 a.EntryOrder = append(a.EntryOrder, name)
1374 }
1375 a.EntryMap[name] = []string{path.String()}
1376}
1377
1378// SetOptionalPath sets a Make variable with the given name to the given path string if it is valid.
1379// It is a no-op if the given path is invalid.
1380func (a *AndroidMkInfo) SetOptionalPath(name string, path OptionalPath) {
1381 if path.Valid() {
1382 a.SetPath(name, path.Path())
1383 }
1384}
1385
1386// AddPath appends the given path string to a Make variable with the given name.
1387func (a *AndroidMkInfo) AddPath(name string, path Path) {
1388 if _, ok := a.EntryMap[name]; !ok {
1389 a.EntryOrder = append(a.EntryOrder, name)
1390 }
1391 a.EntryMap[name] = append(a.EntryMap[name], path.String())
1392}
1393
1394// AddOptionalPath appends the given path string to a Make variable with the given name if it is
1395// valid. It is a no-op if the given path is invalid.
1396func (a *AndroidMkInfo) AddOptionalPath(name string, path OptionalPath) {
1397 if path.Valid() {
1398 a.AddPath(name, path.Path())
1399 }
1400}
1401
1402// SetPaths sets a Make variable with the given name to a slice of the given path strings.
1403func (a *AndroidMkInfo) SetPaths(name string, paths Paths) {
1404 if _, ok := a.EntryMap[name]; !ok {
1405 a.EntryOrder = append(a.EntryOrder, name)
1406 }
1407 a.EntryMap[name] = paths.Strings()
1408}
1409
1410// SetOptionalPaths sets a Make variable with the given name to a slice of the given path strings
1411// only if there are a non-zero amount of paths.
1412func (a *AndroidMkInfo) SetOptionalPaths(name string, paths Paths) {
1413 if len(paths) > 0 {
1414 a.SetPaths(name, paths)
1415 }
1416}
1417
1418// AddPaths appends the given path strings to a Make variable with the given name.
1419func (a *AndroidMkInfo) AddPaths(name string, paths Paths) {
1420 if _, ok := a.EntryMap[name]; !ok {
1421 a.EntryOrder = append(a.EntryOrder, name)
1422 }
1423 a.EntryMap[name] = append(a.EntryMap[name], paths.Strings()...)
1424}
1425
1426// SetBoolIfTrue sets a Make variable with the given name to true if the given flag is true.
1427// It is a no-op if the given flag is false.
1428func (a *AndroidMkInfo) SetBoolIfTrue(name string, flag bool) {
1429 if flag {
1430 if _, ok := a.EntryMap[name]; !ok {
1431 a.EntryOrder = append(a.EntryOrder, name)
1432 }
1433 a.EntryMap[name] = []string{"true"}
1434 }
1435}
1436
1437// SetBool sets a Make variable with the given name to if the given bool flag value.
1438func (a *AndroidMkInfo) SetBool(name string, flag bool) {
1439 if _, ok := a.EntryMap[name]; !ok {
1440 a.EntryOrder = append(a.EntryOrder, name)
1441 }
1442 if flag {
1443 a.EntryMap[name] = []string{"true"}
1444 } else {
1445 a.EntryMap[name] = []string{"false"}
1446 }
1447}
1448
1449// AddStrings appends the given strings to a Make variable with the given name.
1450func (a *AndroidMkInfo) AddStrings(name string, value ...string) {
1451 if len(value) == 0 {
1452 return
1453 }
1454 if _, ok := a.EntryMap[name]; !ok {
1455 a.EntryOrder = append(a.EntryOrder, name)
1456 }
1457 a.EntryMap[name] = append(a.EntryMap[name], value...)
1458}
1459
1460// AddCompatibilityTestSuites adds the supplied test suites to the EntryMap, with special handling
1461// for partial MTS and MCTS test suites.
1462func (a *AndroidMkInfo) AddCompatibilityTestSuites(suites ...string) {
1463 // M(C)TS supports a full test suite and partial per-module MTS test suites, with naming mts-${MODULE}.
1464 // To reduce repetition, if we find a partial M(C)TS test suite without an full M(C)TS test suite,
1465 // we add the full test suite to our list.
1466 if PrefixInList(suites, "mts-") && !InList("mts", suites) {
1467 suites = append(suites, "mts")
1468 }
1469 if PrefixInList(suites, "mcts-") && !InList("mcts", suites) {
1470 suites = append(suites, "mcts")
1471 }
1472 a.AddStrings("LOCAL_COMPATIBILITY_SUITE", suites...)
1473}
1474
Yu Liu64371e02025-02-19 23:44:48 +00001475// TODO(b/397766191): Change the signature to take ModuleProxy
1476// Please only access the module's internal data through providers.
1477func (a *AndroidMkInfo) fillInEntries(ctx fillInEntriesContext, mod Module, commonInfo *CommonModuleInfo) {
mrziwang18420972024-09-03 15:12:51 -07001478 helperInfo := AndroidMkInfo{
1479 EntryMap: make(map[string][]string),
1480 }
1481
Yu Liu64371e02025-02-19 23:44:48 +00001482 name := commonInfo.BaseModuleName
mrziwang18420972024-09-03 15:12:51 -07001483 if a.OverrideName != "" {
1484 name = a.OverrideName
1485 }
1486
1487 if a.Include == "" {
1488 a.Include = "$(BUILD_PREBUILT)"
1489 }
Yu Liu64371e02025-02-19 23:44:48 +00001490 a.Required = append(a.Required, commonInfo.RequiredModuleNames...)
1491 a.Required = append(a.Required, commonInfo.VintfFragmentModuleNames...)
1492 a.Host_required = append(a.Host_required, commonInfo.HostRequiredModuleNames...)
1493 a.Target_required = append(a.Target_required, commonInfo.TargetRequiredModuleNames...)
mrziwang18420972024-09-03 15:12:51 -07001494
Cole Faust5e1454a2025-03-11 15:55:59 -07001495 a.HeaderStrings = append(a.HeaderStrings, a.GetDistForGoals(ctx, mod, commonInfo)...)
Yu Liu64371e02025-02-19 23:44:48 +00001496 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 -07001497
Cole Faust5e1454a2025-03-11 15:55:59 -07001498 // Add the TestSuites from the provider to LOCAL_SOONG_PROVIDER_TEST_SUITES.
1499 // LOCAL_SOONG_PROVIDER_TEST_SUITES will be compared against LOCAL_COMPATIBILITY_SUITES
1500 // in make and enforced they're the same, to ensure we've successfully translated all
1501 // LOCAL_COMPATIBILITY_SUITES usages to the provider.
1502 if testSuiteInfo, ok := OtherModuleProvider(ctx, mod, TestSuiteInfoProvider); ok {
1503 helperInfo.AddStrings("LOCAL_SOONG_PROVIDER_TEST_SUITES", testSuiteInfo.TestSuites...)
1504 }
1505
mrziwang18420972024-09-03 15:12:51 -07001506 // Collect make variable assignment entries.
1507 helperInfo.SetString("LOCAL_PATH", ctx.ModuleDir(mod))
1508 helperInfo.SetString("LOCAL_MODULE", name+a.SubName)
1509 helperInfo.SetString("LOCAL_MODULE_CLASS", a.Class)
1510 helperInfo.SetString("LOCAL_PREBUILT_MODULE_FILE", a.OutputFile.String())
1511 helperInfo.AddStrings("LOCAL_REQUIRED_MODULES", a.Required...)
1512 helperInfo.AddStrings("LOCAL_HOST_REQUIRED_MODULES", a.Host_required...)
1513 helperInfo.AddStrings("LOCAL_TARGET_REQUIRED_MODULES", a.Target_required...)
Yu Liu14b81452025-02-18 23:26:13 +00001514 helperInfo.AddStrings("LOCAL_SOONG_MODULE_TYPE", ctx.ModuleType(mod))
mrziwang18420972024-09-03 15:12:51 -07001515
1516 // If the install rule was generated by Soong tell Make about it.
1517 info := OtherModuleProviderOrDefault(ctx, mod, InstallFilesProvider)
1518 if len(info.KatiInstalls) > 0 {
1519 // Assume the primary install file is last since it probably needs to depend on any other
1520 // installed files. If that is not the case we can add a method to specify the primary
1521 // installed file.
1522 helperInfo.SetPath("LOCAL_SOONG_INSTALLED_MODULE", info.KatiInstalls[len(info.KatiInstalls)-1].to)
1523 helperInfo.SetString("LOCAL_SOONG_INSTALL_PAIRS", info.KatiInstalls.BuiltInstalled())
1524 helperInfo.SetPaths("LOCAL_SOONG_INSTALL_SYMLINKS", info.KatiSymlinks.InstallPaths().Paths())
1525 } else {
1526 // Soong may not have generated the install rule also when `no_full_install: true`.
1527 // Mark this module as uninstallable in order to prevent Make from creating an
1528 // install rule there.
Yu Liu64371e02025-02-19 23:44:48 +00001529 helperInfo.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", commonInfo.NoFullInstall)
mrziwang18420972024-09-03 15:12:51 -07001530 }
1531
Yu Liue70976d2024-10-15 20:45:35 +00001532 if info.UncheckedModule {
1533 helperInfo.SetBool("LOCAL_DONT_CHECK_MODULE", true)
1534 } else if info.CheckbuildTarget != nil {
1535 helperInfo.SetPath("LOCAL_CHECKED_MODULE", info.CheckbuildTarget)
1536 } else {
1537 helperInfo.SetOptionalPath("LOCAL_CHECKED_MODULE", a.OutputFile)
1538 }
1539
mrziwang18420972024-09-03 15:12:51 -07001540 if len(info.TestData) > 0 {
1541 helperInfo.AddStrings("LOCAL_TEST_DATA", androidMkDataPaths(info.TestData)...)
1542 }
1543
Yu Liu64371e02025-02-19 23:44:48 +00001544 if commonInfo.IsApexModule {
1545 helperInfo.SetBoolIfTrue("LOCAL_NOT_AVAILABLE_FOR_PLATFORM", commonInfo.NotAvailableForPlatform)
mrziwang18420972024-09-03 15:12:51 -07001546 }
1547
Yu Liu64371e02025-02-19 23:44:48 +00001548 archStr := commonInfo.Target.Arch.ArchType.String()
mrziwang18420972024-09-03 15:12:51 -07001549 host := false
Yu Liu64371e02025-02-19 23:44:48 +00001550 switch commonInfo.Target.Os.Class {
mrziwang18420972024-09-03 15:12:51 -07001551 case Host:
Yu Liu64371e02025-02-19 23:44:48 +00001552 if commonInfo.Target.HostCross {
mrziwang18420972024-09-03 15:12:51 -07001553 // Make cannot identify LOCAL_MODULE_HOST_CROSS_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_CROSS_ARCH", archStr)
1556 }
1557 } else {
1558 // Make cannot identify LOCAL_MODULE_HOST_ARCH:= common.
Yu Liu64371e02025-02-19 23:44:48 +00001559 if commonInfo.Target.Arch.ArchType != Common {
mrziwang18420972024-09-03 15:12:51 -07001560 helperInfo.SetString("LOCAL_MODULE_HOST_ARCH", archStr)
1561 }
1562 }
1563 host = true
1564 case Device:
1565 // Make cannot identify LOCAL_MODULE_TARGET_ARCH:= common.
Yu Liu64371e02025-02-19 23:44:48 +00001566 if commonInfo.Target.Arch.ArchType != Common {
1567 if commonInfo.Target.NativeBridge {
1568 hostArchStr := commonInfo.Target.NativeBridgeHostArchName
mrziwang18420972024-09-03 15:12:51 -07001569 if hostArchStr != "" {
1570 helperInfo.SetString("LOCAL_MODULE_TARGET_ARCH", hostArchStr)
1571 }
1572 } else {
1573 helperInfo.SetString("LOCAL_MODULE_TARGET_ARCH", archStr)
1574 }
1575 }
1576
Yu Liu64371e02025-02-19 23:44:48 +00001577 if !commonInfo.InVendorRamdisk {
mrziwang18420972024-09-03 15:12:51 -07001578 helperInfo.AddPaths("LOCAL_FULL_INIT_RC", info.InitRcPaths)
1579 }
1580 if len(info.VintfFragmentsPaths) > 0 {
1581 helperInfo.AddPaths("LOCAL_FULL_VINTF_FRAGMENTS", info.VintfFragmentsPaths)
1582 }
Yu Liu64371e02025-02-19 23:44:48 +00001583 helperInfo.SetBoolIfTrue("LOCAL_PROPRIETARY_MODULE", commonInfo.Proprietary)
1584 if commonInfo.Vendor || commonInfo.SocSpecific {
mrziwang18420972024-09-03 15:12:51 -07001585 helperInfo.SetString("LOCAL_VENDOR_MODULE", "true")
1586 }
Yu Liu64371e02025-02-19 23:44:48 +00001587 helperInfo.SetBoolIfTrue("LOCAL_ODM_MODULE", commonInfo.DeviceSpecific)
1588 helperInfo.SetBoolIfTrue("LOCAL_PRODUCT_MODULE", commonInfo.ProductSpecific)
1589 helperInfo.SetBoolIfTrue("LOCAL_SYSTEM_EXT_MODULE", commonInfo.SystemExtSpecific)
1590 if commonInfo.Owner != "" {
1591 helperInfo.SetString("LOCAL_MODULE_OWNER", commonInfo.Owner)
mrziwang18420972024-09-03 15:12:51 -07001592 }
1593 }
1594
1595 if host {
Yu Liu64371e02025-02-19 23:44:48 +00001596 os := commonInfo.Target.Os
1597 makeOs := os.String()
1598 if os == Linux || os == LinuxBionic || os == LinuxMusl {
mrziwang18420972024-09-03 15:12:51 -07001599 makeOs = "linux"
1600 }
1601 helperInfo.SetString("LOCAL_MODULE_HOST_OS", makeOs)
1602 helperInfo.SetString("LOCAL_IS_HOST_MODULE", "true")
1603 }
1604
mrziwang18420972024-09-03 15:12:51 -07001605 if licenseMetadata, ok := OtherModuleProvider(ctx, mod, LicenseMetadataProvider); ok {
1606 helperInfo.SetPath("LOCAL_SOONG_LICENSE_METADATA", licenseMetadata.LicenseMetadataPath)
1607 }
1608
1609 if _, ok := OtherModuleProvider(ctx, mod, ModuleInfoJSONProvider); ok {
1610 helperInfo.SetBool("LOCAL_SOONG_MODULE_INFO_JSON", true)
1611 }
1612
1613 a.mergeEntries(&helperInfo)
1614
1615 // Write to footer.
1616 a.FooterStrings = append([]string{"include " + a.Include}, a.FooterStrings...)
1617}
1618
1619// This method merges the entries to helperInfo, then replaces a's EntryMap and
1620// EntryOrder with helperInfo's
1621func (a *AndroidMkInfo) mergeEntries(helperInfo *AndroidMkInfo) {
1622 for _, extraEntry := range a.EntryOrder {
1623 if v, ok := helperInfo.EntryMap[extraEntry]; ok {
1624 v = append(v, a.EntryMap[extraEntry]...)
1625 } else {
1626 helperInfo.EntryMap[extraEntry] = a.EntryMap[extraEntry]
1627 helperInfo.EntryOrder = append(helperInfo.EntryOrder, extraEntry)
1628 }
1629 }
1630 a.EntryOrder = helperInfo.EntryOrder
1631 a.EntryMap = helperInfo.EntryMap
1632}
1633
1634func (a *AndroidMkInfo) disabled() bool {
1635 return a.Disabled || !a.OutputFile.Valid()
1636}
1637
1638// write flushes the AndroidMkEntries's in-struct data populated by AndroidMkEntries into the
1639// given Writer object.
1640func (a *AndroidMkInfo) write(w io.Writer) {
1641 if a.disabled() {
1642 return
1643 }
1644
Yu Liue70976d2024-10-15 20:45:35 +00001645 combinedHeaderString := strings.Join(a.HeaderStrings, "\n") + "\n"
1646 combinedFooterString := strings.Join(a.FooterStrings, "\n") + "\n"
mrziwang18420972024-09-03 15:12:51 -07001647 w.Write([]byte(combinedHeaderString))
1648 for _, name := range a.EntryOrder {
1649 AndroidMkEmitAssignList(w, name, a.EntryMap[name])
1650 }
1651 w.Write([]byte(combinedFooterString))
1652}
1653
1654// Compute the list of Make strings to declare phony goals and dist-for-goals
1655// calls from the module's dist and dists properties.
Yu Liu64371e02025-02-19 23:44:48 +00001656// TODO(b/397766191): Change the signature to take ModuleProxy
1657// Please only access the module's internal data through providers.
1658func (a *AndroidMkInfo) GetDistForGoals(ctx fillInEntriesContext, mod Module, commonInfo *CommonModuleInfo) []string {
Cole Fausta8437c52025-02-25 14:45:43 -08001659 distContributions := getDistContributions(ctx, mod)
mrziwang18420972024-09-03 15:12:51 -07001660 if distContributions == nil {
1661 return nil
1662 }
1663
1664 return generateDistContributionsForMake(distContributions)
1665}
1666
mrziwang18420972024-09-03 15:12:51 -07001667func deepCopyAndroidMkProviderInfo(providerInfo *AndroidMkProviderInfo) AndroidMkProviderInfo {
1668 info := AndroidMkProviderInfo{
1669 PrimaryInfo: deepCopyAndroidMkInfo(&providerInfo.PrimaryInfo),
1670 }
1671 if len(providerInfo.ExtraInfo) > 0 {
1672 for _, i := range providerInfo.ExtraInfo {
1673 info.ExtraInfo = append(info.ExtraInfo, deepCopyAndroidMkInfo(&i))
1674 }
1675 }
1676 return info
1677}
1678
1679func deepCopyAndroidMkInfo(mkinfo *AndroidMkInfo) AndroidMkInfo {
1680 info := AndroidMkInfo{
1681 Class: mkinfo.Class,
1682 SubName: mkinfo.SubName,
1683 OverrideName: mkinfo.OverrideName,
Cole Faustd143f3e2025-02-24 16:18:18 -08001684 // There is no modification on OutputFile, so no need to
mrziwang18420972024-09-03 15:12:51 -07001685 // make their deep copy.
mrziwang18420972024-09-03 15:12:51 -07001686 OutputFile: mkinfo.OutputFile,
1687 Disabled: mkinfo.Disabled,
1688 Include: mkinfo.Include,
1689 Required: deepCopyStringSlice(mkinfo.Required),
1690 Host_required: deepCopyStringSlice(mkinfo.Host_required),
1691 Target_required: deepCopyStringSlice(mkinfo.Target_required),
1692 HeaderStrings: deepCopyStringSlice(mkinfo.HeaderStrings),
1693 FooterStrings: deepCopyStringSlice(mkinfo.FooterStrings),
1694 EntryOrder: deepCopyStringSlice(mkinfo.EntryOrder),
1695 }
1696 info.EntryMap = make(map[string][]string)
1697 for k, v := range mkinfo.EntryMap {
1698 info.EntryMap[k] = deepCopyStringSlice(v)
1699 }
1700
1701 return info
1702}
1703
1704func deepCopyStringSlice(original []string) []string {
1705 result := make([]string, len(original))
1706 copy(result, original)
1707 return result
1708}