blob: d579e30c38d54eb7571bdeaf94eee0a4aee932da [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
Jiyong Park89e850a2020-04-07 16:37:39 +0900201 if am, ok := mod.(ApexModule); ok {
202 a.SetBoolIfTrue("LOCAL_NOT_AVAILABLE_FOR_PLATFORM", am.NotAvailableForPlatform())
203 }
204
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700205 archStr := amod.Arch().ArchType.String()
206 host := false
207 switch amod.Os().Class {
208 case Host:
209 // Make cannot identify LOCAL_MODULE_HOST_ARCH:= common.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700210 if amod.Arch().ArchType != Common {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700211 a.SetString("LOCAL_MODULE_HOST_ARCH", archStr)
212 }
213 host = true
214 case HostCross:
215 // Make cannot identify LOCAL_MODULE_HOST_CROSS_ARCH:= common.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700216 if amod.Arch().ArchType != Common {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700217 a.SetString("LOCAL_MODULE_HOST_CROSS_ARCH", archStr)
218 }
219 host = true
220 case Device:
221 // Make cannot identify LOCAL_MODULE_TARGET_ARCH:= common.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700222 if amod.Arch().ArchType != Common {
dimitry1f33e402019-03-26 12:39:31 +0100223 if amod.Target().NativeBridge {
dimitry8d6dde82019-07-11 10:23:53 +0200224 hostArchStr := amod.Target().NativeBridgeHostArchName
dimitry1f33e402019-03-26 12:39:31 +0100225 if hostArchStr != "" {
226 a.SetString("LOCAL_MODULE_TARGET_ARCH", hostArchStr)
227 }
228 } else {
229 a.SetString("LOCAL_MODULE_TARGET_ARCH", archStr)
230 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700231 }
232
233 a.AddStrings("LOCAL_INIT_RC", amod.commonProperties.Init_rc...)
234 a.AddStrings("LOCAL_VINTF_FRAGMENTS", amod.commonProperties.Vintf_fragments...)
235 a.SetBoolIfTrue("LOCAL_PROPRIETARY_MODULE", Bool(amod.commonProperties.Proprietary))
236 if Bool(amod.commonProperties.Vendor) || Bool(amod.commonProperties.Soc_specific) {
237 a.SetString("LOCAL_VENDOR_MODULE", "true")
238 }
239 a.SetBoolIfTrue("LOCAL_ODM_MODULE", Bool(amod.commonProperties.Device_specific))
240 a.SetBoolIfTrue("LOCAL_PRODUCT_MODULE", Bool(amod.commonProperties.Product_specific))
Justin Yund5f6c822019-06-25 16:47:17 +0900241 a.SetBoolIfTrue("LOCAL_SYSTEM_EXT_MODULE", Bool(amod.commonProperties.System_ext_specific))
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700242 if amod.commonProperties.Owner != nil {
243 a.SetString("LOCAL_MODULE_OWNER", *amod.commonProperties.Owner)
244 }
245 }
246
Bob Badoura75b0572020-02-18 20:21:55 -0800247 if len(amod.noticeFiles) > 0 {
248 a.SetString("LOCAL_NOTICE_FILE", strings.Join(amod.noticeFiles.Strings(), " "))
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700249 }
250
251 if host {
252 makeOs := amod.Os().String()
253 if amod.Os() == Linux || amod.Os() == LinuxBionic {
254 makeOs = "linux"
255 }
256 a.SetString("LOCAL_MODULE_HOST_OS", makeOs)
257 a.SetString("LOCAL_IS_HOST_MODULE", "true")
258 }
259
260 prefix := ""
261 if amod.ArchSpecific() {
262 switch amod.Os().Class {
263 case Host:
264 prefix = "HOST_"
265 case HostCross:
266 prefix = "HOST_CROSS_"
267 case Device:
268 prefix = "TARGET_"
269
270 }
271
272 if amod.Arch().ArchType != config.Targets[amod.Os()][0].Arch.ArchType {
273 prefix = "2ND_" + prefix
274 }
275 }
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700276 for _, extra := range a.ExtraEntries {
277 extra(a)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700278 }
279
280 // Write to footer.
281 fmt.Fprintln(&a.footer, "include "+a.Include)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700282 blueprintDir := filepath.Dir(bpPath)
283 for _, footerFunc := range a.ExtraFooters {
284 footerFunc(&a.footer, name, prefix, blueprintDir, a)
285 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700286}
287
288func (a *AndroidMkEntries) write(w io.Writer) {
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700289 if a.Disabled {
290 return
291 }
292
293 if !a.OutputFile.Valid() {
294 return
295 }
296
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700297 w.Write(a.header.Bytes())
298 for _, name := range a.entryOrder {
299 fmt.Fprintln(w, name+" := "+strings.Join(a.EntryMap[name], " "))
300 }
301 w.Write(a.footer.Bytes())
302}
303
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700304func (a *AndroidMkEntries) FooterLinesForTests() []string {
305 return strings.Split(string(a.footer.Bytes()), "\n")
306}
307
Colin Cross0875c522017-11-28 17:34:01 -0800308func AndroidMkSingleton() Singleton {
Dan Willemsen218f6562015-07-08 18:13:11 -0700309 return &androidMkSingleton{}
310}
311
312type androidMkSingleton struct{}
313
Colin Cross0875c522017-11-28 17:34:01 -0800314func (c *androidMkSingleton) GenerateBuildActions(ctx SingletonContext) {
Colin Crossaabf6792017-11-29 00:27:14 -0800315 if !ctx.Config().EmbeddedInMake() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800316 return
317 }
318
Colin Cross2465c3d2018-09-28 10:19:18 -0700319 var androidMkModulesList []blueprint.Module
Colin Cross4f6e4e62016-01-11 12:55:55 -0800320
Colin Cross2465c3d2018-09-28 10:19:18 -0700321 ctx.VisitAllModulesBlueprint(func(module blueprint.Module) {
Colin Cross0875c522017-11-28 17:34:01 -0800322 androidMkModulesList = append(androidMkModulesList, module)
Colin Cross4f6e4e62016-01-11 12:55:55 -0800323 })
Dan Willemsen218f6562015-07-08 18:13:11 -0700324
Colin Cross1ad81422019-01-14 12:47:35 -0800325 sort.SliceStable(androidMkModulesList, func(i, j int) bool {
326 return ctx.ModuleName(androidMkModulesList[i]) < ctx.ModuleName(androidMkModulesList[j])
327 })
Colin Crossd779da42015-12-17 18:00:23 -0800328
Dan Willemsen45133ac2018-03-09 21:22:06 -0800329 transMk := PathForOutput(ctx, "Android"+String(ctx.Config().productVariables.Make_suffix)+".mk")
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700330 if ctx.Failed() {
331 return
332 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700333
Colin Cross988414c2020-01-11 01:11:46 +0000334 err := translateAndroidMk(ctx, absolutePath(transMk.String()), androidMkModulesList)
Dan Willemsen218f6562015-07-08 18:13:11 -0700335 if err != nil {
336 ctx.Errorf(err.Error())
337 }
338
Colin Cross0875c522017-11-28 17:34:01 -0800339 ctx.Build(pctx, BuildParams{
340 Rule: blueprint.Phony,
341 Output: transMk,
Dan Willemsen218f6562015-07-08 18:13:11 -0700342 })
343}
344
Colin Cross2465c3d2018-09-28 10:19:18 -0700345func translateAndroidMk(ctx SingletonContext, mkFile string, mods []blueprint.Module) error {
Dan Willemsen218f6562015-07-08 18:13:11 -0700346 buf := &bytes.Buffer{}
347
Dan Willemsen97750522016-02-09 17:43:51 -0800348 fmt.Fprintln(buf, "LOCAL_MODULE_MAKEFILE := $(lastword $(MAKEFILE_LIST))")
Dan Willemsen218f6562015-07-08 18:13:11 -0700349
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700350 type_stats := make(map[string]int)
Dan Willemsen218f6562015-07-08 18:13:11 -0700351 for _, mod := range mods {
352 err := translateAndroidMkModule(ctx, buf, mod)
353 if err != nil {
354 os.Remove(mkFile)
355 return err
356 }
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700357
Colin Cross2465c3d2018-09-28 10:19:18 -0700358 if amod, ok := mod.(Module); ok && ctx.PrimaryModule(amod) == amod {
359 type_stats[ctx.ModuleType(amod)] += 1
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700360 }
361 }
362
363 keys := []string{}
364 fmt.Fprintln(buf, "\nSTATS.SOONG_MODULE_TYPE :=")
365 for k := range type_stats {
366 keys = append(keys, k)
367 }
368 sort.Strings(keys)
369 for _, mod_type := range keys {
370 fmt.Fprintln(buf, "STATS.SOONG_MODULE_TYPE +=", mod_type)
371 fmt.Fprintf(buf, "STATS.SOONG_MODULE_TYPE.%s := %d\n", mod_type, type_stats[mod_type])
Dan Willemsen218f6562015-07-08 18:13:11 -0700372 }
373
374 // Don't write to the file if it hasn't changed
Colin Cross988414c2020-01-11 01:11:46 +0000375 if _, err := os.Stat(absolutePath(mkFile)); !os.IsNotExist(err) {
376 if data, err := ioutil.ReadFile(absolutePath(mkFile)); err == nil {
Dan Willemsen218f6562015-07-08 18:13:11 -0700377 matches := buf.Len() == len(data)
378
379 if matches {
380 for i, value := range buf.Bytes() {
381 if value != data[i] {
382 matches = false
383 break
384 }
385 }
386 }
387
388 if matches {
389 return nil
390 }
391 }
392 }
393
Colin Cross988414c2020-01-11 01:11:46 +0000394 return ioutil.WriteFile(absolutePath(mkFile), buf.Bytes(), 0666)
Dan Willemsen218f6562015-07-08 18:13:11 -0700395}
396
Colin Cross0875c522017-11-28 17:34:01 -0800397func translateAndroidMkModule(ctx SingletonContext, w io.Writer, mod blueprint.Module) error {
Colin Cross953d3a22018-09-05 16:23:54 -0700398 defer func() {
399 if r := recover(); r != nil {
400 panic(fmt.Errorf("%s in translateAndroidMkModule for module %s variant %s",
401 r, ctx.ModuleName(mod), ctx.ModuleSubDir(mod)))
402 }
403 }()
404
Colin Cross2465c3d2018-09-28 10:19:18 -0700405 switch x := mod.(type) {
406 case AndroidMkDataProvider:
407 return translateAndroidModule(ctx, w, mod, x)
408 case bootstrap.GoBinaryTool:
409 return translateGoBinaryModule(ctx, w, mod, x)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700410 case AndroidMkEntriesProvider:
411 return translateAndroidMkEntriesModule(ctx, w, mod, x)
Colin Cross2465c3d2018-09-28 10:19:18 -0700412 default:
Dan Willemsen218f6562015-07-08 18:13:11 -0700413 return nil
414 }
Colin Cross2465c3d2018-09-28 10:19:18 -0700415}
416
417func translateGoBinaryModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
418 goBinary bootstrap.GoBinaryTool) error {
419
420 name := ctx.ModuleName(mod)
421 fmt.Fprintln(w, ".PHONY:", name)
422 fmt.Fprintln(w, name+":", goBinary.InstallPath())
423 fmt.Fprintln(w, "")
424
425 return nil
426}
427
Jooyung Han12df5fb2019-07-11 16:18:47 +0900428func (data *AndroidMkData) fillInData(config Config, bpPath string, mod blueprint.Module) {
429 // Get the preamble content through AndroidMkEntries logic.
430 entries := AndroidMkEntries{
431 Class: data.Class,
432 SubName: data.SubName,
433 DistFile: data.DistFile,
434 OutputFile: data.OutputFile,
435 Disabled: data.Disabled,
436 Include: data.Include,
437 Required: data.Required,
438 Host_required: data.Host_required,
439 Target_required: data.Target_required,
440 }
441 entries.fillInEntries(config, bpPath, mod)
442
443 // preamble doesn't need the footer content.
444 entries.footer = bytes.Buffer{}
445 entries.write(&data.preamble)
446
447 // copy entries back to data since it is used in Custom
448 data.Required = entries.Required
449 data.Host_required = entries.Host_required
450 data.Target_required = entries.Target_required
451}
452
Colin Cross2465c3d2018-09-28 10:19:18 -0700453func translateAndroidModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
454 provider AndroidMkDataProvider) error {
Dan Willemsen218f6562015-07-08 18:13:11 -0700455
Colin Cross635c3b02016-05-18 15:37:25 -0700456 amod := mod.(Module).base()
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700457 if shouldSkipAndroidMkProcessing(amod) {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800458 return nil
459 }
460
Colin Cross91825d22017-08-10 16:59:47 -0700461 data := provider.AndroidMk()
Colin Cross53499412017-09-07 13:20:25 -0700462 if data.Include == "" {
463 data.Include = "$(BUILD_PREBUILT)"
464 }
465
Jooyung Han12df5fb2019-07-11 16:18:47 +0900466 data.fillInData(ctx.Config(), ctx.BlueprintFile(mod), mod)
Dan Willemsen01a405a2016-06-13 17:19:03 -0700467
Colin Cross0f86d182017-08-10 17:07:28 -0700468 prefix := ""
469 if amod.ArchSpecific() {
470 switch amod.Os().Class {
471 case Host:
472 prefix = "HOST_"
473 case HostCross:
474 prefix = "HOST_CROSS_"
475 case Device:
476 prefix = "TARGET_"
Colin Crossa2344662016-03-24 13:14:12 -0700477
Dan Willemsen218f6562015-07-08 18:13:11 -0700478 }
479
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700480 if amod.Arch().ArchType != ctx.Config().Targets[amod.Os()][0].Arch.ArchType {
Colin Cross0f86d182017-08-10 17:07:28 -0700481 prefix = "2ND_" + prefix
482 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700483 }
484
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700485 name := provider.BaseModuleName()
Colin Cross0f86d182017-08-10 17:07:28 -0700486 blueprintDir := filepath.Dir(ctx.BlueprintFile(mod))
487
488 if data.Custom != nil {
489 data.Custom(w, name, prefix, blueprintDir, data)
490 } else {
491 WriteAndroidMkData(w, data)
492 }
493
494 return nil
495}
496
497func WriteAndroidMkData(w io.Writer, data AndroidMkData) {
498 if data.Disabled {
499 return
500 }
501
502 if !data.OutputFile.Valid() {
503 return
504 }
505
506 w.Write(data.preamble.Bytes())
507
Colin Crossca860ac2016-01-04 14:34:37 -0800508 for _, extra := range data.Extra {
Colin Cross27a4b052017-08-10 16:32:23 -0700509 extra(w, data.OutputFile.Path())
Dan Willemsen97750522016-02-09 17:43:51 -0800510 }
511
Colin Cross53499412017-09-07 13:20:25 -0700512 fmt.Fprintln(w, "include "+data.Include)
Dan Willemsen218f6562015-07-08 18:13:11 -0700513}
Sasha Smundakb6d23052019-04-01 18:37:36 -0700514
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700515func translateAndroidMkEntriesModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
516 provider AndroidMkEntriesProvider) error {
517 if shouldSkipAndroidMkProcessing(mod.(Module).base()) {
518 return nil
Sasha Smundakb6d23052019-04-01 18:37:36 -0700519 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700520
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900521 for _, entries := range provider.AndroidMkEntries() {
522 entries.fillInEntries(ctx.Config(), ctx.BlueprintFile(mod), mod)
523 entries.write(w)
524 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700525
526 return nil
527}
528
529func shouldSkipAndroidMkProcessing(module *ModuleBase) bool {
530 if !module.commonProperties.NamespaceExportedToMake {
531 // TODO(jeffrygaston) do we want to validate that there are no modules being
532 // exported to Kati that depend on this module?
533 return true
Sasha Smundakb6d23052019-04-01 18:37:36 -0700534 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700535
536 return !module.Enabled() ||
537 module.commonProperties.SkipInstall ||
538 // Make does not understand LinuxBionic
539 module.Os() == LinuxBionic
Sasha Smundakb6d23052019-04-01 18:37:36 -0700540}