blob: 5bcf1fe9e0854f1340d86143b362d33adb9dbf89 [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
Colin Cross635c3b02016-05-18 15:37:25 -070015package android
Dan Willemsen218f6562015-07-08 18:13:11 -070016
17import (
18 "bytes"
Dan Willemsen97750522016-02-09 17:43:51 -080019 "fmt"
Dan Willemsen218f6562015-07-08 18:13:11 -070020 "io"
21 "io/ioutil"
22 "os"
23 "path/filepath"
Bob Badour987f33c2021-01-07 03:34:31 +000024 "reflect"
Dan Willemsen218f6562015-07-08 18:13:11 -070025 "sort"
Dan Willemsen0fda89f2016-06-01 15:25:32 -070026 "strings"
Dan Willemsen218f6562015-07-08 18:13:11 -070027
Dan Willemsen218f6562015-07-08 18:13:11 -070028 "github.com/google/blueprint"
Colin Cross2465c3d2018-09-28 10:19:18 -070029 "github.com/google/blueprint/bootstrap"
Dan Willemsen218f6562015-07-08 18:13:11 -070030)
31
32func init() {
Paul Duffin22595f92020-03-04 20:15:08 +000033 RegisterAndroidMkBuildComponents(InitRegistrationContext)
34}
35
36func RegisterAndroidMkBuildComponents(ctx RegistrationContext) {
37 ctx.RegisterSingletonType("androidmk", AndroidMkSingleton)
Dan Willemsen218f6562015-07-08 18:13:11 -070038}
39
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -070040// Deprecated: consider using AndroidMkEntriesProvider instead, especially if you're not going to
41// use the Custom function.
Dan Willemsen218f6562015-07-08 18:13:11 -070042type AndroidMkDataProvider interface {
Colin Crossa18e9cf2017-08-10 17:00:19 -070043 AndroidMk() AndroidMkData
Colin Crossce75d2c2016-10-06 16:12:58 -070044 BaseModuleName() string
Dan Willemsen218f6562015-07-08 18:13:11 -070045}
46
47type AndroidMkData struct {
Sasha Smundakb6d23052019-04-01 18:37:36 -070048 Class string
49 SubName string
Jingwen Chen803474e2020-06-15 05:24:19 +000050 DistFiles TaggedDistFiles
Sasha Smundakb6d23052019-04-01 18:37:36 -070051 OutputFile OptionalPath
52 Disabled bool
53 Include string
54 Required []string
55 Host_required []string
56 Target_required []string
Dan Willemsen218f6562015-07-08 18:13:11 -070057
Colin Cross0f86d182017-08-10 17:07:28 -070058 Custom func(w io.Writer, name, prefix, moduleDir string, data AndroidMkData)
Dan Willemsen218f6562015-07-08 18:13:11 -070059
Colin Cross27a4b052017-08-10 16:32:23 -070060 Extra []AndroidMkExtraFunc
Colin Cross0f86d182017-08-10 17:07:28 -070061
62 preamble bytes.Buffer
Dan Willemsen218f6562015-07-08 18:13:11 -070063}
64
Colin Cross27a4b052017-08-10 16:32:23 -070065type AndroidMkExtraFunc func(w io.Writer, outputFile Path)
66
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -070067// Allows modules to customize their Android*.mk output.
68type AndroidMkEntriesProvider interface {
Jiyong Park0b0e1b92019-12-03 13:24:29 +090069 AndroidMkEntries() []AndroidMkEntries
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -070070 BaseModuleName() string
71}
72
73type AndroidMkEntries struct {
74 Class string
75 SubName string
Jingwen Chen803474e2020-06-15 05:24:19 +000076 DistFiles TaggedDistFiles
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -070077 OutputFile OptionalPath
78 Disabled bool
79 Include string
80 Required []string
81 Host_required []string
82 Target_required []string
83
84 header bytes.Buffer
85 footer bytes.Buffer
86
Jaewoong Junge0dc8df2019-08-27 17:33:16 -070087 ExtraEntries []AndroidMkExtraEntriesFunc
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070088 ExtraFooters []AndroidMkExtraFootersFunc
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -070089
90 EntryMap map[string][]string
91 entryOrder []string
92}
93
Jaewoong Junge0dc8df2019-08-27 17:33:16 -070094type AndroidMkExtraEntriesFunc func(entries *AndroidMkEntries)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070095type AndroidMkExtraFootersFunc func(w io.Writer, name, prefix, moduleDir string, entries *AndroidMkEntries)
Jaewoong Junge0dc8df2019-08-27 17:33:16 -070096
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -070097func (a *AndroidMkEntries) SetString(name, value string) {
98 if _, ok := a.EntryMap[name]; !ok {
99 a.entryOrder = append(a.entryOrder, name)
100 }
101 a.EntryMap[name] = []string{value}
102}
103
Jaewoong Jung9a1e8bd2019-09-04 20:17:54 -0700104func (a *AndroidMkEntries) SetPath(name string, path Path) {
105 if _, ok := a.EntryMap[name]; !ok {
106 a.entryOrder = append(a.entryOrder, name)
107 }
108 a.EntryMap[name] = []string{path.String()}
109}
110
Colin Cross1d11c872020-07-03 11:56:24 -0700111func (a *AndroidMkEntries) SetOptionalPath(name string, path OptionalPath) {
112 if path.Valid() {
113 a.SetPath(name, path.Path())
114 }
115}
116
117func (a *AndroidMkEntries) AddPath(name string, path Path) {
118 if _, ok := a.EntryMap[name]; !ok {
119 a.entryOrder = append(a.entryOrder, name)
120 }
121 a.EntryMap[name] = append(a.EntryMap[name], path.String())
122}
123
124func (a *AndroidMkEntries) AddOptionalPath(name string, path OptionalPath) {
125 if path.Valid() {
126 a.AddPath(name, path.Path())
127 }
128}
129
Colin Cross5bc17442020-07-21 20:31:17 -0700130func (a *AndroidMkEntries) SetPaths(name string, paths Paths) {
131 if _, ok := a.EntryMap[name]; !ok {
132 a.entryOrder = append(a.entryOrder, name)
133 }
134 a.EntryMap[name] = paths.Strings()
135}
136
137func (a *AndroidMkEntries) SetOptionalPaths(name string, paths Paths) {
138 if len(paths) > 0 {
139 a.SetPaths(name, paths)
140 }
141}
142
143func (a *AndroidMkEntries) AddPaths(name string, paths Paths) {
144 if _, ok := a.EntryMap[name]; !ok {
145 a.entryOrder = append(a.entryOrder, name)
146 }
147 a.EntryMap[name] = append(a.EntryMap[name], paths.Strings()...)
148}
149
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700150func (a *AndroidMkEntries) SetBoolIfTrue(name string, flag bool) {
151 if flag {
152 if _, ok := a.EntryMap[name]; !ok {
153 a.entryOrder = append(a.entryOrder, name)
154 }
155 a.EntryMap[name] = []string{"true"}
156 }
157}
158
Jaewoong Jung9a1e8bd2019-09-04 20:17:54 -0700159func (a *AndroidMkEntries) SetBool(name string, flag bool) {
160 if _, ok := a.EntryMap[name]; !ok {
161 a.entryOrder = append(a.entryOrder, name)
162 }
163 if flag {
164 a.EntryMap[name] = []string{"true"}
165 } else {
166 a.EntryMap[name] = []string{"false"}
167 }
168}
169
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700170func (a *AndroidMkEntries) AddStrings(name string, value ...string) {
171 if len(value) == 0 {
172 return
173 }
174 if _, ok := a.EntryMap[name]; !ok {
175 a.entryOrder = append(a.entryOrder, name)
176 }
177 a.EntryMap[name] = append(a.EntryMap[name], value...)
178}
179
Treehugger Robot6a421b32020-12-01 22:37:46 +0000180// AddCompatibilityTestSuites adds the supplied test suites to the EntryMap, with special handling
181// for partial MTS test suites.
182func (a *AndroidMkEntries) AddCompatibilityTestSuites(suites ...string) {
183 // MTS supports a full test suite and partial per-module MTS test suites, with naming mts-${MODULE}.
184 // To reduce repetition, if we find a partial MTS test suite without an full MTS test suite,
185 // we add the full test suite to our list.
186 if PrefixInList(suites, "mts-") && !InList("mts", suites) {
187 suites = append(suites, "mts")
188 }
189 a.AddStrings("LOCAL_COMPATIBILITY_SUITE", suites...)
190}
191
Jingwen Chen803474e2020-06-15 05:24:19 +0000192// Compute the list of Make strings to declare phone goals and dist-for-goals
193// calls from the module's dist and dists properties.
194func (a *AndroidMkEntries) GetDistForGoals(mod blueprint.Module) []string {
195 amod := mod.(Module).base()
196 name := amod.BaseModuleName()
197
198 var ret []string
Jingwen Chene80262a2020-07-24 09:13:49 +0000199 var availableTaggedDists TaggedDistFiles
Jingwen Chen803474e2020-06-15 05:24:19 +0000200
Jingwen Chen06c3b412020-07-21 11:32:19 +0000201 if a.DistFiles != nil {
Jingwen Chen803474e2020-06-15 05:24:19 +0000202 availableTaggedDists = a.DistFiles
203 } else if a.OutputFile.Valid() {
204 availableTaggedDists = MakeDefaultDistFiles(a.OutputFile.Path())
Jingwen Chene80262a2020-07-24 09:13:49 +0000205 } else {
206 // Nothing dist-able for this module.
207 return nil
Jingwen Chen803474e2020-06-15 05:24:19 +0000208 }
209
210 // Iterate over this module's dist structs, merged from the dist and dists properties.
211 for _, dist := range amod.Dists() {
212 // Get the list of goals this dist should be enabled for. e.g. sdk, droidcore
213 goals := strings.Join(dist.Targets, " ")
214
215 // Get the tag representing the output files to be dist'd. e.g. ".jar", ".proguard_map"
216 var tag string
217 if dist.Tag == nil {
218 // If the dist struct does not specify a tag, use the default output files tag.
219 tag = ""
220 } else {
221 tag = *dist.Tag
222 }
223
224 // Get the paths of the output files to be dist'd, represented by the tag.
225 // Can be an empty list.
226 tagPaths := availableTaggedDists[tag]
227 if len(tagPaths) == 0 {
228 // Nothing to dist for this tag, continue to the next dist.
229 continue
230 }
231
232 if len(tagPaths) > 1 && (dist.Dest != nil || dist.Suffix != nil) {
233 errorMessage := "Cannot apply dest/suffix for more than one dist " +
234 "file for %s goals in module %s. The list of dist files, " +
235 "which should have a single element, is:\n%s"
236 panic(fmt.Errorf(errorMessage, goals, name, tagPaths))
237 }
238
239 ret = append(ret, fmt.Sprintf(".PHONY: %s\n", goals))
240
241 // Create dist-for-goals calls for each path in the dist'd files.
242 for _, path := range tagPaths {
243 // It's possible that the Path is nil from errant modules. Be defensive here.
244 if path == nil {
245 tagName := "default" // for error message readability
246 if dist.Tag != nil {
247 tagName = *dist.Tag
248 }
249 panic(fmt.Errorf("Dist file should not be nil for the %s tag in %s", tagName, name))
250 }
251
252 dest := filepath.Base(path.String())
253
254 if dist.Dest != nil {
255 var err error
256 if dest, err = validateSafePath(*dist.Dest); err != nil {
257 // This was checked in ModuleBase.GenerateBuildActions
258 panic(err)
259 }
260 }
261
262 if dist.Suffix != nil {
263 ext := filepath.Ext(dest)
264 suffix := *dist.Suffix
265 dest = strings.TrimSuffix(dest, ext) + suffix + ext
266 }
267
268 if dist.Dir != nil {
269 var err error
270 if dest, err = validateSafePath(*dist.Dir, dest); err != nil {
271 // This was checked in ModuleBase.GenerateBuildActions
272 panic(err)
273 }
274 }
275
276 ret = append(
277 ret,
278 fmt.Sprintf("$(call dist-for-goals,%s,%s:%s)\n", goals, path.String(), dest))
279 }
280 }
281
282 return ret
283}
284
Bob Badour987f33c2021-01-07 03:34:31 +0000285// Write the license variables to Make for AndroidMkData.Custom(..) methods that do not call WriteAndroidMkData(..)
286// It's required to propagate the license metadata even for module types that have non-standard interfaces to Make.
287func (a *AndroidMkEntries) WriteLicenseVariables(w io.Writer) {
288 fmt.Fprintln(w, "LOCAL_LICENSE_KINDS :=", strings.Join(a.EntryMap["LOCAL_LICENSE_KINDS"], " "))
289 fmt.Fprintln(w, "LOCAL_LICENSE_CONDITIONS :=", strings.Join(a.EntryMap["LOCAL_LICENSE_CONDITIONS"], " "))
290 fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", strings.Join(a.EntryMap["LOCAL_NOTICE_FILE"], " "))
291 if pn, ok := a.EntryMap["LOCAL_LICENSE_PACKAGE_NAME"]; ok {
292 fmt.Fprintln(w, "LOCAL_LICENSE_PACKAGE_NAME :=", strings.Join(pn, " "))
293 }
294}
295
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700296func (a *AndroidMkEntries) fillInEntries(config Config, bpPath string, mod blueprint.Module) {
297 a.EntryMap = make(map[string][]string)
298 amod := mod.(Module).base()
299 name := amod.BaseModuleName()
300
301 if a.Include == "" {
302 a.Include = "$(BUILD_PREBUILT)"
303 }
304 a.Required = append(a.Required, amod.commonProperties.Required...)
305 a.Host_required = append(a.Host_required, amod.commonProperties.Host_required...)
306 a.Target_required = append(a.Target_required, amod.commonProperties.Target_required...)
307
Jingwen Chen803474e2020-06-15 05:24:19 +0000308 for _, distString := range a.GetDistForGoals(mod) {
309 fmt.Fprintf(&a.header, distString)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700310 }
311
312 fmt.Fprintln(&a.header, "\ninclude $(CLEAR_VARS)")
313
314 // Collect make variable assignment entries.
315 a.SetString("LOCAL_PATH", filepath.Dir(bpPath))
316 a.SetString("LOCAL_MODULE", name+a.SubName)
Bob Badour987f33c2021-01-07 03:34:31 +0000317 a.AddStrings("LOCAL_LICENSE_KINDS", amod.commonProperties.Effective_license_kinds...)
318 a.AddStrings("LOCAL_LICENSE_CONDITIONS", amod.commonProperties.Effective_license_conditions...)
319 a.AddStrings("LOCAL_NOTICE_FILE", amod.commonProperties.Effective_license_text...)
320 // TODO(b/151177513): Does this code need to set LOCAL_MODULE_IS_CONTAINER ?
321 if amod.commonProperties.Effective_package_name != nil {
322 a.SetString("LOCAL_LICENSE_PACKAGE_NAME", *amod.commonProperties.Effective_package_name)
323 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700324 a.SetString("LOCAL_MODULE_CLASS", a.Class)
325 a.SetString("LOCAL_PREBUILT_MODULE_FILE", a.OutputFile.String())
326 a.AddStrings("LOCAL_REQUIRED_MODULES", a.Required...)
327 a.AddStrings("LOCAL_HOST_REQUIRED_MODULES", a.Host_required...)
328 a.AddStrings("LOCAL_TARGET_REQUIRED_MODULES", a.Target_required...)
329
Jiyong Park6a9ddc32020-04-07 16:37:39 +0900330 if am, ok := mod.(ApexModule); ok {
331 a.SetBoolIfTrue("LOCAL_NOT_AVAILABLE_FOR_PLATFORM", am.NotAvailableForPlatform())
332 }
333
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700334 archStr := amod.Arch().ArchType.String()
335 host := false
336 switch amod.Os().Class {
337 case Host:
338 // Make cannot identify LOCAL_MODULE_HOST_ARCH:= common.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700339 if amod.Arch().ArchType != Common {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700340 a.SetString("LOCAL_MODULE_HOST_ARCH", archStr)
341 }
342 host = true
343 case HostCross:
344 // Make cannot identify LOCAL_MODULE_HOST_CROSS_ARCH:= common.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700345 if amod.Arch().ArchType != Common {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700346 a.SetString("LOCAL_MODULE_HOST_CROSS_ARCH", archStr)
347 }
348 host = true
349 case Device:
350 // Make cannot identify LOCAL_MODULE_TARGET_ARCH:= common.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700351 if amod.Arch().ArchType != Common {
dimitry1f33e402019-03-26 12:39:31 +0100352 if amod.Target().NativeBridge {
dimitry8d6dde82019-07-11 10:23:53 +0200353 hostArchStr := amod.Target().NativeBridgeHostArchName
dimitry1f33e402019-03-26 12:39:31 +0100354 if hostArchStr != "" {
355 a.SetString("LOCAL_MODULE_TARGET_ARCH", hostArchStr)
356 }
357 } else {
358 a.SetString("LOCAL_MODULE_TARGET_ARCH", archStr)
359 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700360 }
361
362 a.AddStrings("LOCAL_INIT_RC", amod.commonProperties.Init_rc...)
363 a.AddStrings("LOCAL_VINTF_FRAGMENTS", amod.commonProperties.Vintf_fragments...)
364 a.SetBoolIfTrue("LOCAL_PROPRIETARY_MODULE", Bool(amod.commonProperties.Proprietary))
365 if Bool(amod.commonProperties.Vendor) || Bool(amod.commonProperties.Soc_specific) {
366 a.SetString("LOCAL_VENDOR_MODULE", "true")
367 }
368 a.SetBoolIfTrue("LOCAL_ODM_MODULE", Bool(amod.commonProperties.Device_specific))
369 a.SetBoolIfTrue("LOCAL_PRODUCT_MODULE", Bool(amod.commonProperties.Product_specific))
Justin Yund5f6c822019-06-25 16:47:17 +0900370 a.SetBoolIfTrue("LOCAL_SYSTEM_EXT_MODULE", Bool(amod.commonProperties.System_ext_specific))
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700371 if amod.commonProperties.Owner != nil {
372 a.SetString("LOCAL_MODULE_OWNER", *amod.commonProperties.Owner)
373 }
374 }
375
376 if amod.noticeFile.Valid() {
377 a.SetString("LOCAL_NOTICE_FILE", amod.noticeFile.String())
378 }
379
380 if host {
381 makeOs := amod.Os().String()
382 if amod.Os() == Linux || amod.Os() == LinuxBionic {
383 makeOs = "linux"
384 }
385 a.SetString("LOCAL_MODULE_HOST_OS", makeOs)
386 a.SetString("LOCAL_IS_HOST_MODULE", "true")
387 }
388
389 prefix := ""
390 if amod.ArchSpecific() {
391 switch amod.Os().Class {
392 case Host:
393 prefix = "HOST_"
394 case HostCross:
395 prefix = "HOST_CROSS_"
396 case Device:
397 prefix = "TARGET_"
398
399 }
400
401 if amod.Arch().ArchType != config.Targets[amod.Os()][0].Arch.ArchType {
402 prefix = "2ND_" + prefix
403 }
404 }
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700405 for _, extra := range a.ExtraEntries {
406 extra(a)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700407 }
408
409 // Write to footer.
410 fmt.Fprintln(&a.footer, "include "+a.Include)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700411 blueprintDir := filepath.Dir(bpPath)
412 for _, footerFunc := range a.ExtraFooters {
413 footerFunc(&a.footer, name, prefix, blueprintDir, a)
414 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700415}
416
417func (a *AndroidMkEntries) write(w io.Writer) {
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700418 if a.Disabled {
419 return
420 }
421
422 if !a.OutputFile.Valid() {
423 return
424 }
425
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700426 w.Write(a.header.Bytes())
427 for _, name := range a.entryOrder {
428 fmt.Fprintln(w, name+" := "+strings.Join(a.EntryMap[name], " "))
429 }
430 w.Write(a.footer.Bytes())
431}
432
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700433func (a *AndroidMkEntries) FooterLinesForTests() []string {
434 return strings.Split(string(a.footer.Bytes()), "\n")
435}
436
Colin Cross0875c522017-11-28 17:34:01 -0800437func AndroidMkSingleton() Singleton {
Dan Willemsen218f6562015-07-08 18:13:11 -0700438 return &androidMkSingleton{}
439}
440
441type androidMkSingleton struct{}
442
Colin Cross0875c522017-11-28 17:34:01 -0800443func (c *androidMkSingleton) GenerateBuildActions(ctx SingletonContext) {
Colin Crossaabf6792017-11-29 00:27:14 -0800444 if !ctx.Config().EmbeddedInMake() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800445 return
446 }
447
Colin Cross2465c3d2018-09-28 10:19:18 -0700448 var androidMkModulesList []blueprint.Module
Colin Cross4f6e4e62016-01-11 12:55:55 -0800449
Colin Cross2465c3d2018-09-28 10:19:18 -0700450 ctx.VisitAllModulesBlueprint(func(module blueprint.Module) {
Colin Cross0875c522017-11-28 17:34:01 -0800451 androidMkModulesList = append(androidMkModulesList, module)
Colin Cross4f6e4e62016-01-11 12:55:55 -0800452 })
Dan Willemsen218f6562015-07-08 18:13:11 -0700453
Colin Cross1ad81422019-01-14 12:47:35 -0800454 sort.SliceStable(androidMkModulesList, func(i, j int) bool {
455 return ctx.ModuleName(androidMkModulesList[i]) < ctx.ModuleName(androidMkModulesList[j])
456 })
Colin Crossd779da42015-12-17 18:00:23 -0800457
Dan Willemsen45133ac2018-03-09 21:22:06 -0800458 transMk := PathForOutput(ctx, "Android"+String(ctx.Config().productVariables.Make_suffix)+".mk")
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700459 if ctx.Failed() {
460 return
461 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700462
Colin Cross988414c2020-01-11 01:11:46 +0000463 err := translateAndroidMk(ctx, absolutePath(transMk.String()), androidMkModulesList)
Dan Willemsen218f6562015-07-08 18:13:11 -0700464 if err != nil {
465 ctx.Errorf(err.Error())
466 }
467
Colin Cross0875c522017-11-28 17:34:01 -0800468 ctx.Build(pctx, BuildParams{
469 Rule: blueprint.Phony,
470 Output: transMk,
Dan Willemsen218f6562015-07-08 18:13:11 -0700471 })
472}
473
Colin Cross2465c3d2018-09-28 10:19:18 -0700474func translateAndroidMk(ctx SingletonContext, mkFile string, mods []blueprint.Module) error {
Dan Willemsen218f6562015-07-08 18:13:11 -0700475 buf := &bytes.Buffer{}
476
Dan Willemsen97750522016-02-09 17:43:51 -0800477 fmt.Fprintln(buf, "LOCAL_MODULE_MAKEFILE := $(lastword $(MAKEFILE_LIST))")
Dan Willemsen218f6562015-07-08 18:13:11 -0700478
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700479 type_stats := make(map[string]int)
Dan Willemsen218f6562015-07-08 18:13:11 -0700480 for _, mod := range mods {
481 err := translateAndroidMkModule(ctx, buf, mod)
482 if err != nil {
483 os.Remove(mkFile)
484 return err
485 }
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700486
Colin Cross2465c3d2018-09-28 10:19:18 -0700487 if amod, ok := mod.(Module); ok && ctx.PrimaryModule(amod) == amod {
488 type_stats[ctx.ModuleType(amod)] += 1
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700489 }
490 }
491
492 keys := []string{}
493 fmt.Fprintln(buf, "\nSTATS.SOONG_MODULE_TYPE :=")
494 for k := range type_stats {
495 keys = append(keys, k)
496 }
497 sort.Strings(keys)
498 for _, mod_type := range keys {
499 fmt.Fprintln(buf, "STATS.SOONG_MODULE_TYPE +=", mod_type)
500 fmt.Fprintf(buf, "STATS.SOONG_MODULE_TYPE.%s := %d\n", mod_type, type_stats[mod_type])
Dan Willemsen218f6562015-07-08 18:13:11 -0700501 }
502
503 // Don't write to the file if it hasn't changed
Colin Cross988414c2020-01-11 01:11:46 +0000504 if _, err := os.Stat(absolutePath(mkFile)); !os.IsNotExist(err) {
505 if data, err := ioutil.ReadFile(absolutePath(mkFile)); err == nil {
Dan Willemsen218f6562015-07-08 18:13:11 -0700506 matches := buf.Len() == len(data)
507
508 if matches {
509 for i, value := range buf.Bytes() {
510 if value != data[i] {
511 matches = false
512 break
513 }
514 }
515 }
516
517 if matches {
518 return nil
519 }
520 }
521 }
522
Colin Cross988414c2020-01-11 01:11:46 +0000523 return ioutil.WriteFile(absolutePath(mkFile), buf.Bytes(), 0666)
Dan Willemsen218f6562015-07-08 18:13:11 -0700524}
525
Colin Cross0875c522017-11-28 17:34:01 -0800526func translateAndroidMkModule(ctx SingletonContext, w io.Writer, mod blueprint.Module) error {
Colin Cross953d3a22018-09-05 16:23:54 -0700527 defer func() {
528 if r := recover(); r != nil {
529 panic(fmt.Errorf("%s in translateAndroidMkModule for module %s variant %s",
530 r, ctx.ModuleName(mod), ctx.ModuleSubDir(mod)))
531 }
532 }()
533
Bob Badour987f33c2021-01-07 03:34:31 +0000534 // Additional cases here require review for correct license propagation to make.
Colin Cross2465c3d2018-09-28 10:19:18 -0700535 switch x := mod.(type) {
536 case AndroidMkDataProvider:
537 return translateAndroidModule(ctx, w, mod, x)
538 case bootstrap.GoBinaryTool:
539 return translateGoBinaryModule(ctx, w, mod, x)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700540 case AndroidMkEntriesProvider:
541 return translateAndroidMkEntriesModule(ctx, w, mod, x)
Colin Cross2465c3d2018-09-28 10:19:18 -0700542 default:
Bob Badour987f33c2021-01-07 03:34:31 +0000543 // Not exported to make so no make variables to set.
Dan Willemsen218f6562015-07-08 18:13:11 -0700544 return nil
545 }
Colin Cross2465c3d2018-09-28 10:19:18 -0700546}
547
548func translateGoBinaryModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
549 goBinary bootstrap.GoBinaryTool) error {
550
551 name := ctx.ModuleName(mod)
552 fmt.Fprintln(w, ".PHONY:", name)
553 fmt.Fprintln(w, name+":", goBinary.InstallPath())
554 fmt.Fprintln(w, "")
Bob Badour987f33c2021-01-07 03:34:31 +0000555 // Assuming no rules in make include go binaries in distributables.
556 // If the assumption is wrong, make will fail to build without the necessary .meta_lic and .meta_module files.
557 // In that case, add the targets and rules here to build a .meta_lic file for `name` and a .meta_module for
558 // `goBinary.InstallPath()` pointing to the `name`.meta_lic file.
Colin Cross2465c3d2018-09-28 10:19:18 -0700559
560 return nil
561}
562
Jooyung Han12df5fb2019-07-11 16:18:47 +0900563func (data *AndroidMkData) fillInData(config Config, bpPath string, mod blueprint.Module) {
564 // Get the preamble content through AndroidMkEntries logic.
565 entries := AndroidMkEntries{
566 Class: data.Class,
567 SubName: data.SubName,
Jingwen Chen803474e2020-06-15 05:24:19 +0000568 DistFiles: data.DistFiles,
Jooyung Han12df5fb2019-07-11 16:18:47 +0900569 OutputFile: data.OutputFile,
570 Disabled: data.Disabled,
571 Include: data.Include,
572 Required: data.Required,
573 Host_required: data.Host_required,
574 Target_required: data.Target_required,
575 }
576 entries.fillInEntries(config, bpPath, mod)
577
578 // preamble doesn't need the footer content.
579 entries.footer = bytes.Buffer{}
580 entries.write(&data.preamble)
581
582 // copy entries back to data since it is used in Custom
583 data.Required = entries.Required
584 data.Host_required = entries.Host_required
585 data.Target_required = entries.Target_required
586}
587
Colin Cross2465c3d2018-09-28 10:19:18 -0700588func translateAndroidModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
589 provider AndroidMkDataProvider) error {
Dan Willemsen218f6562015-07-08 18:13:11 -0700590
Colin Cross635c3b02016-05-18 15:37:25 -0700591 amod := mod.(Module).base()
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700592 if shouldSkipAndroidMkProcessing(amod) {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800593 return nil
594 }
595
Colin Cross91825d22017-08-10 16:59:47 -0700596 data := provider.AndroidMk()
Colin Cross53499412017-09-07 13:20:25 -0700597 if data.Include == "" {
598 data.Include = "$(BUILD_PREBUILT)"
599 }
600
Jooyung Han12df5fb2019-07-11 16:18:47 +0900601 data.fillInData(ctx.Config(), ctx.BlueprintFile(mod), mod)
Dan Willemsen01a405a2016-06-13 17:19:03 -0700602
Colin Cross0f86d182017-08-10 17:07:28 -0700603 prefix := ""
604 if amod.ArchSpecific() {
605 switch amod.Os().Class {
606 case Host:
607 prefix = "HOST_"
608 case HostCross:
609 prefix = "HOST_CROSS_"
610 case Device:
611 prefix = "TARGET_"
Colin Crossa2344662016-03-24 13:14:12 -0700612
Dan Willemsen218f6562015-07-08 18:13:11 -0700613 }
614
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700615 if amod.Arch().ArchType != ctx.Config().Targets[amod.Os()][0].Arch.ArchType {
Colin Cross0f86d182017-08-10 17:07:28 -0700616 prefix = "2ND_" + prefix
617 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700618 }
619
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700620 name := provider.BaseModuleName()
Colin Cross0f86d182017-08-10 17:07:28 -0700621 blueprintDir := filepath.Dir(ctx.BlueprintFile(mod))
622
623 if data.Custom != nil {
Bob Badour987f33c2021-01-07 03:34:31 +0000624 // List of module types allowed to use .Custom(...)
625 // Additions to the list require careful review for proper license handling.
626 switch reflect.TypeOf(mod).String() { // ctx.ModuleType(mod) doesn't work: aidl_interface creates phony without type
627 case "*aidl.aidlApi": // writes non-custom before adding .phony
628 case "*aidl.aidlMapping": // writes non-custom before adding .phony
629 case "*android.customModule": // appears in tests only
630 case "*apex.apexBundle": // license properties written
631 case "*bpf.bpf": // license properties written (both for module and objs)
632 case "*genrule.Module": // writes non-custom before adding .phony
633 case "*java.SystemModules": // doesn't go through base_rules
634 case "*java.systemModulesImport": // doesn't go through base_rules
635 case "*phony.phony": // license properties written
636 case "*selinux.selinuxContextsModule": // license properties written
637 case "*sysprop.syspropLibrary": // license properties written
638 default:
639 if ctx.Config().IsEnvTrue("ANDROID_REQUIRE_LICENSES") {
640 return fmt.Errorf("custom make rules not allowed for %q (%q) module %q", ctx.ModuleType(mod), reflect.TypeOf(mod), ctx.ModuleName(mod))
641 }
642 }
Colin Cross0f86d182017-08-10 17:07:28 -0700643 data.Custom(w, name, prefix, blueprintDir, data)
644 } else {
645 WriteAndroidMkData(w, data)
646 }
647
648 return nil
649}
650
651func WriteAndroidMkData(w io.Writer, data AndroidMkData) {
652 if data.Disabled {
653 return
654 }
655
656 if !data.OutputFile.Valid() {
657 return
658 }
659
660 w.Write(data.preamble.Bytes())
661
Colin Crossca860ac2016-01-04 14:34:37 -0800662 for _, extra := range data.Extra {
Colin Cross27a4b052017-08-10 16:32:23 -0700663 extra(w, data.OutputFile.Path())
Dan Willemsen97750522016-02-09 17:43:51 -0800664 }
665
Colin Cross53499412017-09-07 13:20:25 -0700666 fmt.Fprintln(w, "include "+data.Include)
Dan Willemsen218f6562015-07-08 18:13:11 -0700667}
Sasha Smundakb6d23052019-04-01 18:37:36 -0700668
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700669func translateAndroidMkEntriesModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
670 provider AndroidMkEntriesProvider) error {
671 if shouldSkipAndroidMkProcessing(mod.(Module).base()) {
672 return nil
Sasha Smundakb6d23052019-04-01 18:37:36 -0700673 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700674
Bob Badour987f33c2021-01-07 03:34:31 +0000675 // Any new or special cases here need review to verify correct propagation of license information.
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900676 for _, entries := range provider.AndroidMkEntries() {
677 entries.fillInEntries(ctx.Config(), ctx.BlueprintFile(mod), mod)
678 entries.write(w)
679 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700680
681 return nil
682}
683
684func shouldSkipAndroidMkProcessing(module *ModuleBase) bool {
685 if !module.commonProperties.NamespaceExportedToMake {
686 // TODO(jeffrygaston) do we want to validate that there are no modules being
687 // exported to Kati that depend on this module?
688 return true
Sasha Smundakb6d23052019-04-01 18:37:36 -0700689 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700690
691 return !module.Enabled() ||
692 module.commonProperties.SkipInstall ||
693 // Make does not understand LinuxBionic
694 module.Os() == LinuxBionic
Sasha Smundakb6d23052019-04-01 18:37:36 -0700695}