blob: b5f4b2bd5e9cc5eb8159e768a60adcf61ca697a2 [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
49 DistFile OptionalPath
50 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
61 preamble bytes.Buffer
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
75 DistFile OptionalPath
76 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
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700110func (a *AndroidMkEntries) SetBoolIfTrue(name string, flag bool) {
111 if flag {
112 if _, ok := a.EntryMap[name]; !ok {
113 a.entryOrder = append(a.entryOrder, name)
114 }
115 a.EntryMap[name] = []string{"true"}
116 }
117}
118
Jaewoong Jung9a1e8bd2019-09-04 20:17:54 -0700119func (a *AndroidMkEntries) SetBool(name string, flag bool) {
120 if _, ok := a.EntryMap[name]; !ok {
121 a.entryOrder = append(a.entryOrder, name)
122 }
123 if flag {
124 a.EntryMap[name] = []string{"true"}
125 } else {
126 a.EntryMap[name] = []string{"false"}
127 }
128}
129
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700130func (a *AndroidMkEntries) AddStrings(name string, value ...string) {
131 if len(value) == 0 {
132 return
133 }
134 if _, ok := a.EntryMap[name]; !ok {
135 a.entryOrder = append(a.entryOrder, name)
136 }
137 a.EntryMap[name] = append(a.EntryMap[name], value...)
138}
139
140func (a *AndroidMkEntries) fillInEntries(config Config, bpPath string, mod blueprint.Module) {
141 a.EntryMap = make(map[string][]string)
142 amod := mod.(Module).base()
143 name := amod.BaseModuleName()
144
145 if a.Include == "" {
146 a.Include = "$(BUILD_PREBUILT)"
147 }
148 a.Required = append(a.Required, amod.commonProperties.Required...)
149 a.Host_required = append(a.Host_required, amod.commonProperties.Host_required...)
150 a.Target_required = append(a.Target_required, amod.commonProperties.Target_required...)
151
152 // Fill in the header part.
153 if len(amod.commonProperties.Dist.Targets) > 0 {
154 distFile := a.DistFile
155 if !distFile.Valid() {
156 distFile = a.OutputFile
157 }
158 if distFile.Valid() {
159 dest := filepath.Base(distFile.String())
160
161 if amod.commonProperties.Dist.Dest != nil {
162 var err error
163 if dest, err = validateSafePath(*amod.commonProperties.Dist.Dest); err != nil {
164 // This was checked in ModuleBase.GenerateBuildActions
165 panic(err)
166 }
167 }
168
169 if amod.commonProperties.Dist.Suffix != nil {
170 ext := filepath.Ext(dest)
171 suffix := *amod.commonProperties.Dist.Suffix
172 dest = strings.TrimSuffix(dest, ext) + suffix + ext
173 }
174
175 if amod.commonProperties.Dist.Dir != nil {
176 var err error
177 if dest, err = validateSafePath(*amod.commonProperties.Dist.Dir, dest); err != nil {
178 // This was checked in ModuleBase.GenerateBuildActions
179 panic(err)
180 }
181 }
182
183 goals := strings.Join(amod.commonProperties.Dist.Targets, " ")
184 fmt.Fprintln(&a.header, ".PHONY:", goals)
185 fmt.Fprintf(&a.header, "$(call dist-for-goals,%s,%s:%s)\n",
186 goals, distFile.String(), dest)
187 }
188 }
189
190 fmt.Fprintln(&a.header, "\ninclude $(CLEAR_VARS)")
191
192 // Collect make variable assignment entries.
193 a.SetString("LOCAL_PATH", filepath.Dir(bpPath))
194 a.SetString("LOCAL_MODULE", name+a.SubName)
195 a.SetString("LOCAL_MODULE_CLASS", a.Class)
196 a.SetString("LOCAL_PREBUILT_MODULE_FILE", a.OutputFile.String())
197 a.AddStrings("LOCAL_REQUIRED_MODULES", a.Required...)
198 a.AddStrings("LOCAL_HOST_REQUIRED_MODULES", a.Host_required...)
199 a.AddStrings("LOCAL_TARGET_REQUIRED_MODULES", a.Target_required...)
200
201 archStr := amod.Arch().ArchType.String()
202 host := false
203 switch amod.Os().Class {
204 case Host:
205 // Make cannot identify LOCAL_MODULE_HOST_ARCH:= common.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700206 if amod.Arch().ArchType != Common {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700207 a.SetString("LOCAL_MODULE_HOST_ARCH", archStr)
208 }
209 host = true
210 case HostCross:
211 // Make cannot identify LOCAL_MODULE_HOST_CROSS_ARCH:= common.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700212 if amod.Arch().ArchType != Common {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700213 a.SetString("LOCAL_MODULE_HOST_CROSS_ARCH", archStr)
214 }
215 host = true
216 case Device:
217 // Make cannot identify LOCAL_MODULE_TARGET_ARCH:= common.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700218 if amod.Arch().ArchType != Common {
dimitry1f33e402019-03-26 12:39:31 +0100219 if amod.Target().NativeBridge {
dimitry8d6dde82019-07-11 10:23:53 +0200220 hostArchStr := amod.Target().NativeBridgeHostArchName
dimitry1f33e402019-03-26 12:39:31 +0100221 if hostArchStr != "" {
222 a.SetString("LOCAL_MODULE_TARGET_ARCH", hostArchStr)
223 }
224 } else {
225 a.SetString("LOCAL_MODULE_TARGET_ARCH", archStr)
226 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700227 }
228
229 a.AddStrings("LOCAL_INIT_RC", amod.commonProperties.Init_rc...)
230 a.AddStrings("LOCAL_VINTF_FRAGMENTS", amod.commonProperties.Vintf_fragments...)
231 a.SetBoolIfTrue("LOCAL_PROPRIETARY_MODULE", Bool(amod.commonProperties.Proprietary))
232 if Bool(amod.commonProperties.Vendor) || Bool(amod.commonProperties.Soc_specific) {
233 a.SetString("LOCAL_VENDOR_MODULE", "true")
234 }
235 a.SetBoolIfTrue("LOCAL_ODM_MODULE", Bool(amod.commonProperties.Device_specific))
236 a.SetBoolIfTrue("LOCAL_PRODUCT_MODULE", Bool(amod.commonProperties.Product_specific))
Justin Yund5f6c822019-06-25 16:47:17 +0900237 a.SetBoolIfTrue("LOCAL_SYSTEM_EXT_MODULE", Bool(amod.commonProperties.System_ext_specific))
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700238 if amod.commonProperties.Owner != nil {
239 a.SetString("LOCAL_MODULE_OWNER", *amod.commonProperties.Owner)
240 }
241 }
242
Bob Badoura75b0572020-02-18 20:21:55 -0800243 if len(amod.noticeFiles) > 0 {
244 a.SetString("LOCAL_NOTICE_FILE", strings.Join(amod.noticeFiles.Strings(), " "))
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700245 }
246
247 if host {
248 makeOs := amod.Os().String()
249 if amod.Os() == Linux || amod.Os() == LinuxBionic {
250 makeOs = "linux"
251 }
252 a.SetString("LOCAL_MODULE_HOST_OS", makeOs)
253 a.SetString("LOCAL_IS_HOST_MODULE", "true")
254 }
255
256 prefix := ""
257 if amod.ArchSpecific() {
258 switch amod.Os().Class {
259 case Host:
260 prefix = "HOST_"
261 case HostCross:
262 prefix = "HOST_CROSS_"
263 case Device:
264 prefix = "TARGET_"
265
266 }
267
268 if amod.Arch().ArchType != config.Targets[amod.Os()][0].Arch.ArchType {
269 prefix = "2ND_" + prefix
270 }
271 }
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700272 for _, extra := range a.ExtraEntries {
273 extra(a)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700274 }
275
276 // Write to footer.
277 fmt.Fprintln(&a.footer, "include "+a.Include)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700278 blueprintDir := filepath.Dir(bpPath)
279 for _, footerFunc := range a.ExtraFooters {
280 footerFunc(&a.footer, name, prefix, blueprintDir, a)
281 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700282}
283
284func (a *AndroidMkEntries) write(w io.Writer) {
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700285 if a.Disabled {
286 return
287 }
288
289 if !a.OutputFile.Valid() {
290 return
291 }
292
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700293 w.Write(a.header.Bytes())
294 for _, name := range a.entryOrder {
295 fmt.Fprintln(w, name+" := "+strings.Join(a.EntryMap[name], " "))
296 }
297 w.Write(a.footer.Bytes())
298}
299
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700300func (a *AndroidMkEntries) FooterLinesForTests() []string {
301 return strings.Split(string(a.footer.Bytes()), "\n")
302}
303
Colin Cross0875c522017-11-28 17:34:01 -0800304func AndroidMkSingleton() Singleton {
Dan Willemsen218f6562015-07-08 18:13:11 -0700305 return &androidMkSingleton{}
306}
307
308type androidMkSingleton struct{}
309
Colin Cross0875c522017-11-28 17:34:01 -0800310func (c *androidMkSingleton) GenerateBuildActions(ctx SingletonContext) {
Colin Crossaabf6792017-11-29 00:27:14 -0800311 if !ctx.Config().EmbeddedInMake() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800312 return
313 }
314
Colin Cross2465c3d2018-09-28 10:19:18 -0700315 var androidMkModulesList []blueprint.Module
Colin Cross4f6e4e62016-01-11 12:55:55 -0800316
Colin Cross2465c3d2018-09-28 10:19:18 -0700317 ctx.VisitAllModulesBlueprint(func(module blueprint.Module) {
Colin Cross0875c522017-11-28 17:34:01 -0800318 androidMkModulesList = append(androidMkModulesList, module)
Colin Cross4f6e4e62016-01-11 12:55:55 -0800319 })
Dan Willemsen218f6562015-07-08 18:13:11 -0700320
Colin Cross1ad81422019-01-14 12:47:35 -0800321 sort.SliceStable(androidMkModulesList, func(i, j int) bool {
322 return ctx.ModuleName(androidMkModulesList[i]) < ctx.ModuleName(androidMkModulesList[j])
323 })
Colin Crossd779da42015-12-17 18:00:23 -0800324
Dan Willemsen45133ac2018-03-09 21:22:06 -0800325 transMk := PathForOutput(ctx, "Android"+String(ctx.Config().productVariables.Make_suffix)+".mk")
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700326 if ctx.Failed() {
327 return
328 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700329
Colin Cross988414c2020-01-11 01:11:46 +0000330 err := translateAndroidMk(ctx, absolutePath(transMk.String()), androidMkModulesList)
Dan Willemsen218f6562015-07-08 18:13:11 -0700331 if err != nil {
332 ctx.Errorf(err.Error())
333 }
334
Colin Cross0875c522017-11-28 17:34:01 -0800335 ctx.Build(pctx, BuildParams{
336 Rule: blueprint.Phony,
337 Output: transMk,
Dan Willemsen218f6562015-07-08 18:13:11 -0700338 })
339}
340
Colin Cross2465c3d2018-09-28 10:19:18 -0700341func translateAndroidMk(ctx SingletonContext, mkFile string, mods []blueprint.Module) error {
Dan Willemsen218f6562015-07-08 18:13:11 -0700342 buf := &bytes.Buffer{}
343
Dan Willemsen97750522016-02-09 17:43:51 -0800344 fmt.Fprintln(buf, "LOCAL_MODULE_MAKEFILE := $(lastword $(MAKEFILE_LIST))")
Dan Willemsen218f6562015-07-08 18:13:11 -0700345
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700346 type_stats := make(map[string]int)
Dan Willemsen218f6562015-07-08 18:13:11 -0700347 for _, mod := range mods {
348 err := translateAndroidMkModule(ctx, buf, mod)
349 if err != nil {
350 os.Remove(mkFile)
351 return err
352 }
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700353
Colin Cross2465c3d2018-09-28 10:19:18 -0700354 if amod, ok := mod.(Module); ok && ctx.PrimaryModule(amod) == amod {
355 type_stats[ctx.ModuleType(amod)] += 1
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700356 }
357 }
358
359 keys := []string{}
360 fmt.Fprintln(buf, "\nSTATS.SOONG_MODULE_TYPE :=")
361 for k := range type_stats {
362 keys = append(keys, k)
363 }
364 sort.Strings(keys)
365 for _, mod_type := range keys {
366 fmt.Fprintln(buf, "STATS.SOONG_MODULE_TYPE +=", mod_type)
367 fmt.Fprintf(buf, "STATS.SOONG_MODULE_TYPE.%s := %d\n", mod_type, type_stats[mod_type])
Dan Willemsen218f6562015-07-08 18:13:11 -0700368 }
369
370 // Don't write to the file if it hasn't changed
Colin Cross988414c2020-01-11 01:11:46 +0000371 if _, err := os.Stat(absolutePath(mkFile)); !os.IsNotExist(err) {
372 if data, err := ioutil.ReadFile(absolutePath(mkFile)); err == nil {
Dan Willemsen218f6562015-07-08 18:13:11 -0700373 matches := buf.Len() == len(data)
374
375 if matches {
376 for i, value := range buf.Bytes() {
377 if value != data[i] {
378 matches = false
379 break
380 }
381 }
382 }
383
384 if matches {
385 return nil
386 }
387 }
388 }
389
Colin Cross988414c2020-01-11 01:11:46 +0000390 return ioutil.WriteFile(absolutePath(mkFile), buf.Bytes(), 0666)
Dan Willemsen218f6562015-07-08 18:13:11 -0700391}
392
Colin Cross0875c522017-11-28 17:34:01 -0800393func translateAndroidMkModule(ctx SingletonContext, w io.Writer, mod blueprint.Module) error {
Colin Cross953d3a22018-09-05 16:23:54 -0700394 defer func() {
395 if r := recover(); r != nil {
396 panic(fmt.Errorf("%s in translateAndroidMkModule for module %s variant %s",
397 r, ctx.ModuleName(mod), ctx.ModuleSubDir(mod)))
398 }
399 }()
400
Colin Cross2465c3d2018-09-28 10:19:18 -0700401 switch x := mod.(type) {
402 case AndroidMkDataProvider:
403 return translateAndroidModule(ctx, w, mod, x)
404 case bootstrap.GoBinaryTool:
405 return translateGoBinaryModule(ctx, w, mod, x)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700406 case AndroidMkEntriesProvider:
407 return translateAndroidMkEntriesModule(ctx, w, mod, x)
Colin Cross2465c3d2018-09-28 10:19:18 -0700408 default:
Dan Willemsen218f6562015-07-08 18:13:11 -0700409 return nil
410 }
Colin Cross2465c3d2018-09-28 10:19:18 -0700411}
412
413func translateGoBinaryModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
414 goBinary bootstrap.GoBinaryTool) error {
415
416 name := ctx.ModuleName(mod)
417 fmt.Fprintln(w, ".PHONY:", name)
418 fmt.Fprintln(w, name+":", goBinary.InstallPath())
419 fmt.Fprintln(w, "")
420
421 return nil
422}
423
Jooyung Han12df5fb2019-07-11 16:18:47 +0900424func (data *AndroidMkData) fillInData(config Config, bpPath string, mod blueprint.Module) {
425 // Get the preamble content through AndroidMkEntries logic.
426 entries := AndroidMkEntries{
427 Class: data.Class,
428 SubName: data.SubName,
429 DistFile: data.DistFile,
430 OutputFile: data.OutputFile,
431 Disabled: data.Disabled,
432 Include: data.Include,
433 Required: data.Required,
434 Host_required: data.Host_required,
435 Target_required: data.Target_required,
436 }
437 entries.fillInEntries(config, bpPath, mod)
438
439 // preamble doesn't need the footer content.
440 entries.footer = bytes.Buffer{}
441 entries.write(&data.preamble)
442
443 // copy entries back to data since it is used in Custom
444 data.Required = entries.Required
445 data.Host_required = entries.Host_required
446 data.Target_required = entries.Target_required
447}
448
Colin Cross2465c3d2018-09-28 10:19:18 -0700449func translateAndroidModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
450 provider AndroidMkDataProvider) error {
Dan Willemsen218f6562015-07-08 18:13:11 -0700451
Colin Cross635c3b02016-05-18 15:37:25 -0700452 amod := mod.(Module).base()
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700453 if shouldSkipAndroidMkProcessing(amod) {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800454 return nil
455 }
456
Colin Cross91825d22017-08-10 16:59:47 -0700457 data := provider.AndroidMk()
Colin Cross53499412017-09-07 13:20:25 -0700458 if data.Include == "" {
459 data.Include = "$(BUILD_PREBUILT)"
460 }
461
Jooyung Han12df5fb2019-07-11 16:18:47 +0900462 data.fillInData(ctx.Config(), ctx.BlueprintFile(mod), mod)
Dan Willemsen01a405a2016-06-13 17:19:03 -0700463
Colin Cross0f86d182017-08-10 17:07:28 -0700464 prefix := ""
465 if amod.ArchSpecific() {
466 switch amod.Os().Class {
467 case Host:
468 prefix = "HOST_"
469 case HostCross:
470 prefix = "HOST_CROSS_"
471 case Device:
472 prefix = "TARGET_"
Colin Crossa2344662016-03-24 13:14:12 -0700473
Dan Willemsen218f6562015-07-08 18:13:11 -0700474 }
475
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700476 if amod.Arch().ArchType != ctx.Config().Targets[amod.Os()][0].Arch.ArchType {
Colin Cross0f86d182017-08-10 17:07:28 -0700477 prefix = "2ND_" + prefix
478 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700479 }
480
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700481 name := provider.BaseModuleName()
Colin Cross0f86d182017-08-10 17:07:28 -0700482 blueprintDir := filepath.Dir(ctx.BlueprintFile(mod))
483
484 if data.Custom != nil {
485 data.Custom(w, name, prefix, blueprintDir, data)
486 } else {
487 WriteAndroidMkData(w, data)
488 }
489
490 return nil
491}
492
493func WriteAndroidMkData(w io.Writer, data AndroidMkData) {
494 if data.Disabled {
495 return
496 }
497
498 if !data.OutputFile.Valid() {
499 return
500 }
501
502 w.Write(data.preamble.Bytes())
503
Colin Crossca860ac2016-01-04 14:34:37 -0800504 for _, extra := range data.Extra {
Colin Cross27a4b052017-08-10 16:32:23 -0700505 extra(w, data.OutputFile.Path())
Dan Willemsen97750522016-02-09 17:43:51 -0800506 }
507
Colin Cross53499412017-09-07 13:20:25 -0700508 fmt.Fprintln(w, "include "+data.Include)
Dan Willemsen218f6562015-07-08 18:13:11 -0700509}
Sasha Smundakb6d23052019-04-01 18:37:36 -0700510
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700511func translateAndroidMkEntriesModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
512 provider AndroidMkEntriesProvider) error {
513 if shouldSkipAndroidMkProcessing(mod.(Module).base()) {
514 return nil
Sasha Smundakb6d23052019-04-01 18:37:36 -0700515 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700516
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900517 for _, entries := range provider.AndroidMkEntries() {
518 entries.fillInEntries(ctx.Config(), ctx.BlueprintFile(mod), mod)
519 entries.write(w)
520 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700521
522 return nil
523}
524
525func shouldSkipAndroidMkProcessing(module *ModuleBase) bool {
526 if !module.commonProperties.NamespaceExportedToMake {
527 // TODO(jeffrygaston) do we want to validate that there are no modules being
528 // exported to Kati that depend on this module?
529 return true
Sasha Smundakb6d23052019-04-01 18:37:36 -0700530 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700531
532 return !module.Enabled() ||
533 module.commonProperties.SkipInstall ||
534 // Make does not understand LinuxBionic
535 module.Os() == LinuxBionic
Sasha Smundakb6d23052019-04-01 18:37:36 -0700536}