blob: ddd51ffd53361fcd5bd610938b3e2ca4240e1710 [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"
24 "sort"
Dan Willemsen0fda89f2016-06-01 15:25:32 -070025 "strings"
Dan Willemsen218f6562015-07-08 18:13:11 -070026
Dan Willemsen218f6562015-07-08 18:13:11 -070027 "github.com/google/blueprint"
Colin Cross2465c3d2018-09-28 10:19:18 -070028 "github.com/google/blueprint/bootstrap"
Dan Willemsen218f6562015-07-08 18:13:11 -070029)
30
31func init() {
Paul Duffin8c3fec42020-03-04 20:15:08 +000032 RegisterAndroidMkBuildComponents(InitRegistrationContext)
33}
34
35func RegisterAndroidMkBuildComponents(ctx RegistrationContext) {
36 ctx.RegisterSingletonType("androidmk", AndroidMkSingleton)
Dan Willemsen218f6562015-07-08 18:13:11 -070037}
38
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -070039// Deprecated: consider using AndroidMkEntriesProvider instead, especially if you're not going to
40// use the Custom function.
Dan Willemsen218f6562015-07-08 18:13:11 -070041type AndroidMkDataProvider interface {
Colin Crossa18e9cf2017-08-10 17:00:19 -070042 AndroidMk() AndroidMkData
Colin Crossce75d2c2016-10-06 16:12:58 -070043 BaseModuleName() string
Dan Willemsen218f6562015-07-08 18:13:11 -070044}
45
46type AndroidMkData struct {
Sasha Smundakb6d23052019-04-01 18:37:36 -070047 Class string
48 SubName string
Jingwen Chen40fd90a2020-06-15 05:24:19 +000049 DistFiles TaggedDistFiles
Sasha Smundakb6d23052019-04-01 18:37:36 -070050 OutputFile OptionalPath
51 Disabled bool
52 Include string
53 Required []string
54 Host_required []string
55 Target_required []string
Dan Willemsen218f6562015-07-08 18:13:11 -070056
Colin Cross0f86d182017-08-10 17:07:28 -070057 Custom func(w io.Writer, name, prefix, moduleDir string, data AndroidMkData)
Dan Willemsen218f6562015-07-08 18:13:11 -070058
Colin Cross27a4b052017-08-10 16:32:23 -070059 Extra []AndroidMkExtraFunc
Colin Cross0f86d182017-08-10 17:07:28 -070060
Jooyung Han2ed99d02020-06-24 23:26:26 +090061 Entries AndroidMkEntries
Dan Willemsen218f6562015-07-08 18:13:11 -070062}
63
Colin Cross27a4b052017-08-10 16:32:23 -070064type AndroidMkExtraFunc func(w io.Writer, outputFile Path)
65
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -070066// Allows modules to customize their Android*.mk output.
67type AndroidMkEntriesProvider interface {
Jiyong Park0b0e1b92019-12-03 13:24:29 +090068 AndroidMkEntries() []AndroidMkEntries
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -070069 BaseModuleName() string
70}
71
72type AndroidMkEntries struct {
73 Class string
74 SubName string
Jingwen Chen40fd90a2020-06-15 05:24:19 +000075 DistFiles TaggedDistFiles
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -070076 OutputFile OptionalPath
77 Disabled bool
78 Include string
79 Required []string
80 Host_required []string
81 Target_required []string
82
83 header bytes.Buffer
84 footer bytes.Buffer
85
Jaewoong Junge0dc8df2019-08-27 17:33:16 -070086 ExtraEntries []AndroidMkExtraEntriesFunc
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070087 ExtraFooters []AndroidMkExtraFootersFunc
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -070088
89 EntryMap map[string][]string
90 entryOrder []string
91}
92
Jaewoong Junge0dc8df2019-08-27 17:33:16 -070093type AndroidMkExtraEntriesFunc func(entries *AndroidMkEntries)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070094type AndroidMkExtraFootersFunc func(w io.Writer, name, prefix, moduleDir string, entries *AndroidMkEntries)
Jaewoong Junge0dc8df2019-08-27 17:33:16 -070095
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -070096func (a *AndroidMkEntries) SetString(name, value string) {
97 if _, ok := a.EntryMap[name]; !ok {
98 a.entryOrder = append(a.entryOrder, name)
99 }
100 a.EntryMap[name] = []string{value}
101}
102
Jaewoong Jung9a1e8bd2019-09-04 20:17:54 -0700103func (a *AndroidMkEntries) SetPath(name string, path Path) {
104 if _, ok := a.EntryMap[name]; !ok {
105 a.entryOrder = append(a.entryOrder, name)
106 }
107 a.EntryMap[name] = []string{path.String()}
108}
109
Colin Crossc0efd1d2020-07-03 11:56:24 -0700110func (a *AndroidMkEntries) SetOptionalPath(name string, path OptionalPath) {
111 if path.Valid() {
112 a.SetPath(name, path.Path())
113 }
114}
115
116func (a *AndroidMkEntries) AddPath(name string, path Path) {
117 if _, ok := a.EntryMap[name]; !ok {
118 a.entryOrder = append(a.entryOrder, name)
119 }
120 a.EntryMap[name] = append(a.EntryMap[name], path.String())
121}
122
123func (a *AndroidMkEntries) AddOptionalPath(name string, path OptionalPath) {
124 if path.Valid() {
125 a.AddPath(name, path.Path())
126 }
127}
128
Colin Cross08dca382020-07-21 20:31:17 -0700129func (a *AndroidMkEntries) SetPaths(name string, paths Paths) {
130 if _, ok := a.EntryMap[name]; !ok {
131 a.entryOrder = append(a.entryOrder, name)
132 }
133 a.EntryMap[name] = paths.Strings()
134}
135
136func (a *AndroidMkEntries) SetOptionalPaths(name string, paths Paths) {
137 if len(paths) > 0 {
138 a.SetPaths(name, paths)
139 }
140}
141
142func (a *AndroidMkEntries) AddPaths(name string, paths Paths) {
143 if _, ok := a.EntryMap[name]; !ok {
144 a.entryOrder = append(a.entryOrder, name)
145 }
146 a.EntryMap[name] = append(a.EntryMap[name], paths.Strings()...)
147}
148
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700149func (a *AndroidMkEntries) SetBoolIfTrue(name string, flag bool) {
150 if flag {
151 if _, ok := a.EntryMap[name]; !ok {
152 a.entryOrder = append(a.entryOrder, name)
153 }
154 a.EntryMap[name] = []string{"true"}
155 }
156}
157
Jaewoong Jung9a1e8bd2019-09-04 20:17:54 -0700158func (a *AndroidMkEntries) SetBool(name string, flag bool) {
159 if _, ok := a.EntryMap[name]; !ok {
160 a.entryOrder = append(a.entryOrder, name)
161 }
162 if flag {
163 a.EntryMap[name] = []string{"true"}
164 } else {
165 a.EntryMap[name] = []string{"false"}
166 }
167}
168
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700169func (a *AndroidMkEntries) AddStrings(name string, value ...string) {
170 if len(value) == 0 {
171 return
172 }
173 if _, ok := a.EntryMap[name]; !ok {
174 a.entryOrder = append(a.entryOrder, name)
175 }
176 a.EntryMap[name] = append(a.EntryMap[name], value...)
177}
178
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000179// Compute the list of Make strings to declare phone goals and dist-for-goals
180// calls from the module's dist and dists properties.
181func (a *AndroidMkEntries) GetDistForGoals(mod blueprint.Module) []string {
182 amod := mod.(Module).base()
183 name := amod.BaseModuleName()
184
185 var ret []string
Jingwen Chen7b27ca72020-07-24 09:13:49 +0000186 var availableTaggedDists TaggedDistFiles
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000187
Jingwen Chen84811862020-07-21 11:32:19 +0000188 if a.DistFiles != nil {
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000189 availableTaggedDists = a.DistFiles
190 } else if a.OutputFile.Valid() {
191 availableTaggedDists = MakeDefaultDistFiles(a.OutputFile.Path())
Jingwen Chen7b27ca72020-07-24 09:13:49 +0000192 } else {
193 // Nothing dist-able for this module.
194 return nil
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000195 }
196
197 // Iterate over this module's dist structs, merged from the dist and dists properties.
198 for _, dist := range amod.Dists() {
199 // Get the list of goals this dist should be enabled for. e.g. sdk, droidcore
200 goals := strings.Join(dist.Targets, " ")
201
202 // Get the tag representing the output files to be dist'd. e.g. ".jar", ".proguard_map"
203 var tag string
204 if dist.Tag == nil {
205 // If the dist struct does not specify a tag, use the default output files tag.
206 tag = ""
207 } else {
208 tag = *dist.Tag
209 }
210
211 // Get the paths of the output files to be dist'd, represented by the tag.
212 // Can be an empty list.
213 tagPaths := availableTaggedDists[tag]
214 if len(tagPaths) == 0 {
215 // Nothing to dist for this tag, continue to the next dist.
216 continue
217 }
218
219 if len(tagPaths) > 1 && (dist.Dest != nil || dist.Suffix != nil) {
220 errorMessage := "Cannot apply dest/suffix for more than one dist " +
221 "file for %s goals in module %s. The list of dist files, " +
222 "which should have a single element, is:\n%s"
223 panic(fmt.Errorf(errorMessage, goals, name, tagPaths))
224 }
225
226 ret = append(ret, fmt.Sprintf(".PHONY: %s\n", goals))
227
228 // Create dist-for-goals calls for each path in the dist'd files.
229 for _, path := range tagPaths {
230 // It's possible that the Path is nil from errant modules. Be defensive here.
231 if path == nil {
232 tagName := "default" // for error message readability
233 if dist.Tag != nil {
234 tagName = *dist.Tag
235 }
236 panic(fmt.Errorf("Dist file should not be nil for the %s tag in %s", tagName, name))
237 }
238
239 dest := filepath.Base(path.String())
240
241 if dist.Dest != nil {
242 var err error
243 if dest, err = validateSafePath(*dist.Dest); err != nil {
244 // This was checked in ModuleBase.GenerateBuildActions
245 panic(err)
246 }
247 }
248
249 if dist.Suffix != nil {
250 ext := filepath.Ext(dest)
251 suffix := *dist.Suffix
252 dest = strings.TrimSuffix(dest, ext) + suffix + ext
253 }
254
255 if dist.Dir != nil {
256 var err error
257 if dest, err = validateSafePath(*dist.Dir, dest); err != nil {
258 // This was checked in ModuleBase.GenerateBuildActions
259 panic(err)
260 }
261 }
262
263 ret = append(
264 ret,
265 fmt.Sprintf("$(call dist-for-goals,%s,%s:%s)\n", goals, path.String(), dest))
266 }
267 }
268
269 return ret
270}
271
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700272func (a *AndroidMkEntries) fillInEntries(config Config, bpPath string, mod blueprint.Module) {
273 a.EntryMap = make(map[string][]string)
274 amod := mod.(Module).base()
275 name := amod.BaseModuleName()
276
277 if a.Include == "" {
278 a.Include = "$(BUILD_PREBUILT)"
279 }
280 a.Required = append(a.Required, amod.commonProperties.Required...)
281 a.Host_required = append(a.Host_required, amod.commonProperties.Host_required...)
282 a.Target_required = append(a.Target_required, amod.commonProperties.Target_required...)
283
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000284 for _, distString := range a.GetDistForGoals(mod) {
285 fmt.Fprintf(&a.header, distString)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700286 }
287
288 fmt.Fprintln(&a.header, "\ninclude $(CLEAR_VARS)")
289
290 // Collect make variable assignment entries.
291 a.SetString("LOCAL_PATH", filepath.Dir(bpPath))
292 a.SetString("LOCAL_MODULE", name+a.SubName)
293 a.SetString("LOCAL_MODULE_CLASS", a.Class)
294 a.SetString("LOCAL_PREBUILT_MODULE_FILE", a.OutputFile.String())
295 a.AddStrings("LOCAL_REQUIRED_MODULES", a.Required...)
296 a.AddStrings("LOCAL_HOST_REQUIRED_MODULES", a.Host_required...)
297 a.AddStrings("LOCAL_TARGET_REQUIRED_MODULES", a.Target_required...)
298
Jiyong Park89e850a2020-04-07 16:37:39 +0900299 if am, ok := mod.(ApexModule); ok {
300 a.SetBoolIfTrue("LOCAL_NOT_AVAILABLE_FOR_PLATFORM", am.NotAvailableForPlatform())
301 }
302
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700303 archStr := amod.Arch().ArchType.String()
304 host := false
305 switch amod.Os().Class {
306 case Host:
Jiyong Park1613e552020-09-14 19:43:17 +0900307 if amod.Target().HostCross {
308 // Make cannot identify LOCAL_MODULE_HOST_CROSS_ARCH:= common.
309 if amod.Arch().ArchType != Common {
310 a.SetString("LOCAL_MODULE_HOST_CROSS_ARCH", archStr)
311 }
312 } else {
313 // Make cannot identify LOCAL_MODULE_HOST_ARCH:= common.
314 if amod.Arch().ArchType != Common {
315 a.SetString("LOCAL_MODULE_HOST_ARCH", archStr)
316 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700317 }
318 host = true
319 case Device:
320 // Make cannot identify LOCAL_MODULE_TARGET_ARCH:= common.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700321 if amod.Arch().ArchType != Common {
dimitry1f33e402019-03-26 12:39:31 +0100322 if amod.Target().NativeBridge {
dimitry8d6dde82019-07-11 10:23:53 +0200323 hostArchStr := amod.Target().NativeBridgeHostArchName
dimitry1f33e402019-03-26 12:39:31 +0100324 if hostArchStr != "" {
325 a.SetString("LOCAL_MODULE_TARGET_ARCH", hostArchStr)
326 }
327 } else {
328 a.SetString("LOCAL_MODULE_TARGET_ARCH", archStr)
329 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700330 }
331
332 a.AddStrings("LOCAL_INIT_RC", amod.commonProperties.Init_rc...)
333 a.AddStrings("LOCAL_VINTF_FRAGMENTS", amod.commonProperties.Vintf_fragments...)
334 a.SetBoolIfTrue("LOCAL_PROPRIETARY_MODULE", Bool(amod.commonProperties.Proprietary))
335 if Bool(amod.commonProperties.Vendor) || Bool(amod.commonProperties.Soc_specific) {
336 a.SetString("LOCAL_VENDOR_MODULE", "true")
337 }
338 a.SetBoolIfTrue("LOCAL_ODM_MODULE", Bool(amod.commonProperties.Device_specific))
339 a.SetBoolIfTrue("LOCAL_PRODUCT_MODULE", Bool(amod.commonProperties.Product_specific))
Justin Yund5f6c822019-06-25 16:47:17 +0900340 a.SetBoolIfTrue("LOCAL_SYSTEM_EXT_MODULE", Bool(amod.commonProperties.System_ext_specific))
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700341 if amod.commonProperties.Owner != nil {
342 a.SetString("LOCAL_MODULE_OWNER", *amod.commonProperties.Owner)
343 }
344 }
345
Bob Badoura75b0572020-02-18 20:21:55 -0800346 if len(amod.noticeFiles) > 0 {
347 a.SetString("LOCAL_NOTICE_FILE", strings.Join(amod.noticeFiles.Strings(), " "))
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700348 }
349
350 if host {
351 makeOs := amod.Os().String()
352 if amod.Os() == Linux || amod.Os() == LinuxBionic {
353 makeOs = "linux"
354 }
355 a.SetString("LOCAL_MODULE_HOST_OS", makeOs)
356 a.SetString("LOCAL_IS_HOST_MODULE", "true")
357 }
358
359 prefix := ""
360 if amod.ArchSpecific() {
361 switch amod.Os().Class {
362 case Host:
Jiyong Park1613e552020-09-14 19:43:17 +0900363 if amod.Target().HostCross {
364 prefix = "HOST_CROSS_"
365 } else {
366 prefix = "HOST_"
367 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700368 case Device:
369 prefix = "TARGET_"
370
371 }
372
373 if amod.Arch().ArchType != config.Targets[amod.Os()][0].Arch.ArchType {
374 prefix = "2ND_" + prefix
375 }
376 }
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700377 for _, extra := range a.ExtraEntries {
378 extra(a)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700379 }
380
381 // Write to footer.
382 fmt.Fprintln(&a.footer, "include "+a.Include)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700383 blueprintDir := filepath.Dir(bpPath)
384 for _, footerFunc := range a.ExtraFooters {
385 footerFunc(&a.footer, name, prefix, blueprintDir, a)
386 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700387}
388
389func (a *AndroidMkEntries) write(w io.Writer) {
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700390 if a.Disabled {
391 return
392 }
393
394 if !a.OutputFile.Valid() {
395 return
396 }
397
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700398 w.Write(a.header.Bytes())
399 for _, name := range a.entryOrder {
400 fmt.Fprintln(w, name+" := "+strings.Join(a.EntryMap[name], " "))
401 }
402 w.Write(a.footer.Bytes())
403}
404
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700405func (a *AndroidMkEntries) FooterLinesForTests() []string {
406 return strings.Split(string(a.footer.Bytes()), "\n")
407}
408
Colin Cross0875c522017-11-28 17:34:01 -0800409func AndroidMkSingleton() Singleton {
Dan Willemsen218f6562015-07-08 18:13:11 -0700410 return &androidMkSingleton{}
411}
412
413type androidMkSingleton struct{}
414
Colin Cross0875c522017-11-28 17:34:01 -0800415func (c *androidMkSingleton) GenerateBuildActions(ctx SingletonContext) {
Colin Crossaabf6792017-11-29 00:27:14 -0800416 if !ctx.Config().EmbeddedInMake() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800417 return
418 }
419
Colin Cross2465c3d2018-09-28 10:19:18 -0700420 var androidMkModulesList []blueprint.Module
Colin Cross4f6e4e62016-01-11 12:55:55 -0800421
Colin Cross2465c3d2018-09-28 10:19:18 -0700422 ctx.VisitAllModulesBlueprint(func(module blueprint.Module) {
Colin Cross0875c522017-11-28 17:34:01 -0800423 androidMkModulesList = append(androidMkModulesList, module)
Colin Cross4f6e4e62016-01-11 12:55:55 -0800424 })
Dan Willemsen218f6562015-07-08 18:13:11 -0700425
Colin Cross1ad81422019-01-14 12:47:35 -0800426 sort.SliceStable(androidMkModulesList, func(i, j int) bool {
427 return ctx.ModuleName(androidMkModulesList[i]) < ctx.ModuleName(androidMkModulesList[j])
428 })
Colin Crossd779da42015-12-17 18:00:23 -0800429
Dan Willemsen45133ac2018-03-09 21:22:06 -0800430 transMk := PathForOutput(ctx, "Android"+String(ctx.Config().productVariables.Make_suffix)+".mk")
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700431 if ctx.Failed() {
432 return
433 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700434
Colin Cross988414c2020-01-11 01:11:46 +0000435 err := translateAndroidMk(ctx, absolutePath(transMk.String()), androidMkModulesList)
Dan Willemsen218f6562015-07-08 18:13:11 -0700436 if err != nil {
437 ctx.Errorf(err.Error())
438 }
439
Colin Cross0875c522017-11-28 17:34:01 -0800440 ctx.Build(pctx, BuildParams{
441 Rule: blueprint.Phony,
442 Output: transMk,
Dan Willemsen218f6562015-07-08 18:13:11 -0700443 })
444}
445
Colin Cross2465c3d2018-09-28 10:19:18 -0700446func translateAndroidMk(ctx SingletonContext, mkFile string, mods []blueprint.Module) error {
Dan Willemsen218f6562015-07-08 18:13:11 -0700447 buf := &bytes.Buffer{}
448
Dan Willemsen97750522016-02-09 17:43:51 -0800449 fmt.Fprintln(buf, "LOCAL_MODULE_MAKEFILE := $(lastword $(MAKEFILE_LIST))")
Dan Willemsen218f6562015-07-08 18:13:11 -0700450
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700451 type_stats := make(map[string]int)
Dan Willemsen218f6562015-07-08 18:13:11 -0700452 for _, mod := range mods {
453 err := translateAndroidMkModule(ctx, buf, mod)
454 if err != nil {
455 os.Remove(mkFile)
456 return err
457 }
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700458
Colin Cross2465c3d2018-09-28 10:19:18 -0700459 if amod, ok := mod.(Module); ok && ctx.PrimaryModule(amod) == amod {
460 type_stats[ctx.ModuleType(amod)] += 1
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700461 }
462 }
463
464 keys := []string{}
465 fmt.Fprintln(buf, "\nSTATS.SOONG_MODULE_TYPE :=")
466 for k := range type_stats {
467 keys = append(keys, k)
468 }
469 sort.Strings(keys)
470 for _, mod_type := range keys {
471 fmt.Fprintln(buf, "STATS.SOONG_MODULE_TYPE +=", mod_type)
472 fmt.Fprintf(buf, "STATS.SOONG_MODULE_TYPE.%s := %d\n", mod_type, type_stats[mod_type])
Dan Willemsen218f6562015-07-08 18:13:11 -0700473 }
474
475 // Don't write to the file if it hasn't changed
Colin Cross988414c2020-01-11 01:11:46 +0000476 if _, err := os.Stat(absolutePath(mkFile)); !os.IsNotExist(err) {
477 if data, err := ioutil.ReadFile(absolutePath(mkFile)); err == nil {
Dan Willemsen218f6562015-07-08 18:13:11 -0700478 matches := buf.Len() == len(data)
479
480 if matches {
481 for i, value := range buf.Bytes() {
482 if value != data[i] {
483 matches = false
484 break
485 }
486 }
487 }
488
489 if matches {
490 return nil
491 }
492 }
493 }
494
Colin Cross988414c2020-01-11 01:11:46 +0000495 return ioutil.WriteFile(absolutePath(mkFile), buf.Bytes(), 0666)
Dan Willemsen218f6562015-07-08 18:13:11 -0700496}
497
Colin Cross0875c522017-11-28 17:34:01 -0800498func translateAndroidMkModule(ctx SingletonContext, w io.Writer, mod blueprint.Module) error {
Colin Cross953d3a22018-09-05 16:23:54 -0700499 defer func() {
500 if r := recover(); r != nil {
501 panic(fmt.Errorf("%s in translateAndroidMkModule for module %s variant %s",
502 r, ctx.ModuleName(mod), ctx.ModuleSubDir(mod)))
503 }
504 }()
505
Colin Cross2465c3d2018-09-28 10:19:18 -0700506 switch x := mod.(type) {
507 case AndroidMkDataProvider:
508 return translateAndroidModule(ctx, w, mod, x)
509 case bootstrap.GoBinaryTool:
510 return translateGoBinaryModule(ctx, w, mod, x)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700511 case AndroidMkEntriesProvider:
512 return translateAndroidMkEntriesModule(ctx, w, mod, x)
Colin Cross2465c3d2018-09-28 10:19:18 -0700513 default:
Dan Willemsen218f6562015-07-08 18:13:11 -0700514 return nil
515 }
Colin Cross2465c3d2018-09-28 10:19:18 -0700516}
517
518func translateGoBinaryModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
519 goBinary bootstrap.GoBinaryTool) error {
520
521 name := ctx.ModuleName(mod)
522 fmt.Fprintln(w, ".PHONY:", name)
523 fmt.Fprintln(w, name+":", goBinary.InstallPath())
524 fmt.Fprintln(w, "")
525
526 return nil
527}
528
Jooyung Han12df5fb2019-07-11 16:18:47 +0900529func (data *AndroidMkData) fillInData(config Config, bpPath string, mod blueprint.Module) {
530 // Get the preamble content through AndroidMkEntries logic.
Jooyung Han2ed99d02020-06-24 23:26:26 +0900531 data.Entries = AndroidMkEntries{
Jooyung Han12df5fb2019-07-11 16:18:47 +0900532 Class: data.Class,
533 SubName: data.SubName,
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000534 DistFiles: data.DistFiles,
Jooyung Han12df5fb2019-07-11 16:18:47 +0900535 OutputFile: data.OutputFile,
536 Disabled: data.Disabled,
537 Include: data.Include,
538 Required: data.Required,
539 Host_required: data.Host_required,
540 Target_required: data.Target_required,
541 }
Jooyung Han2ed99d02020-06-24 23:26:26 +0900542 data.Entries.fillInEntries(config, bpPath, mod)
Jooyung Han12df5fb2019-07-11 16:18:47 +0900543
544 // copy entries back to data since it is used in Custom
Jooyung Han2ed99d02020-06-24 23:26:26 +0900545 data.Required = data.Entries.Required
546 data.Host_required = data.Entries.Host_required
547 data.Target_required = data.Entries.Target_required
Jooyung Han12df5fb2019-07-11 16:18:47 +0900548}
549
Colin Cross2465c3d2018-09-28 10:19:18 -0700550func translateAndroidModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
551 provider AndroidMkDataProvider) error {
Dan Willemsen218f6562015-07-08 18:13:11 -0700552
Colin Cross635c3b02016-05-18 15:37:25 -0700553 amod := mod.(Module).base()
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700554 if shouldSkipAndroidMkProcessing(amod) {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800555 return nil
556 }
557
Colin Cross91825d22017-08-10 16:59:47 -0700558 data := provider.AndroidMk()
Colin Cross53499412017-09-07 13:20:25 -0700559 if data.Include == "" {
560 data.Include = "$(BUILD_PREBUILT)"
561 }
562
Jooyung Han12df5fb2019-07-11 16:18:47 +0900563 data.fillInData(ctx.Config(), ctx.BlueprintFile(mod), mod)
Dan Willemsen01a405a2016-06-13 17:19:03 -0700564
Colin Cross0f86d182017-08-10 17:07:28 -0700565 prefix := ""
566 if amod.ArchSpecific() {
567 switch amod.Os().Class {
568 case Host:
Jiyong Park1613e552020-09-14 19:43:17 +0900569 if amod.Target().HostCross {
570 prefix = "HOST_CROSS_"
571 } else {
572 prefix = "HOST_"
573 }
Colin Cross0f86d182017-08-10 17:07:28 -0700574 case Device:
575 prefix = "TARGET_"
Colin Crossa2344662016-03-24 13:14:12 -0700576
Dan Willemsen218f6562015-07-08 18:13:11 -0700577 }
578
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700579 if amod.Arch().ArchType != ctx.Config().Targets[amod.Os()][0].Arch.ArchType {
Colin Cross0f86d182017-08-10 17:07:28 -0700580 prefix = "2ND_" + prefix
581 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700582 }
583
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700584 name := provider.BaseModuleName()
Colin Cross0f86d182017-08-10 17:07:28 -0700585 blueprintDir := filepath.Dir(ctx.BlueprintFile(mod))
586
587 if data.Custom != nil {
588 data.Custom(w, name, prefix, blueprintDir, data)
589 } else {
590 WriteAndroidMkData(w, data)
591 }
592
593 return nil
594}
595
596func WriteAndroidMkData(w io.Writer, data AndroidMkData) {
597 if data.Disabled {
598 return
599 }
600
601 if !data.OutputFile.Valid() {
602 return
603 }
604
Jooyung Han2ed99d02020-06-24 23:26:26 +0900605 // write preamble via Entries
606 data.Entries.footer = bytes.Buffer{}
607 data.Entries.write(w)
Colin Cross0f86d182017-08-10 17:07:28 -0700608
Colin Crossca860ac2016-01-04 14:34:37 -0800609 for _, extra := range data.Extra {
Colin Cross27a4b052017-08-10 16:32:23 -0700610 extra(w, data.OutputFile.Path())
Dan Willemsen97750522016-02-09 17:43:51 -0800611 }
612
Colin Cross53499412017-09-07 13:20:25 -0700613 fmt.Fprintln(w, "include "+data.Include)
Dan Willemsen218f6562015-07-08 18:13:11 -0700614}
Sasha Smundakb6d23052019-04-01 18:37:36 -0700615
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700616func translateAndroidMkEntriesModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
617 provider AndroidMkEntriesProvider) error {
618 if shouldSkipAndroidMkProcessing(mod.(Module).base()) {
619 return nil
Sasha Smundakb6d23052019-04-01 18:37:36 -0700620 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700621
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900622 for _, entries := range provider.AndroidMkEntries() {
623 entries.fillInEntries(ctx.Config(), ctx.BlueprintFile(mod), mod)
624 entries.write(w)
625 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700626
627 return nil
628}
629
630func shouldSkipAndroidMkProcessing(module *ModuleBase) bool {
631 if !module.commonProperties.NamespaceExportedToMake {
632 // TODO(jeffrygaston) do we want to validate that there are no modules being
633 // exported to Kati that depend on this module?
634 return true
Sasha Smundakb6d23052019-04-01 18:37:36 -0700635 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700636
637 return !module.Enabled() ||
638 module.commonProperties.SkipInstall ||
639 // Make does not understand LinuxBionic
640 module.Os() == LinuxBionic
Sasha Smundakb6d23052019-04-01 18:37:36 -0700641}
Dan Shi31949122020-09-21 12:11:02 -0700642
643func AndroidMkDataPaths(data []DataPath) []string {
644 var testFiles []string
645 for _, d := range data {
646 rel := d.SrcPath.Rel()
647 path := d.SrcPath.String()
648 if !strings.HasSuffix(path, rel) {
649 panic(fmt.Errorf("path %q does not end with %q", path, rel))
650 }
651 path = strings.TrimSuffix(path, rel)
652 testFileString := path + ":" + rel
653 if len(d.RelativeInstallPath) > 0 {
654 testFileString += ":" + d.RelativeInstallPath
655 }
656 testFiles = append(testFiles, testFileString)
657 }
658 return testFiles
659}