blob: 54a0c6465af6238bc42b3e3eee7166b397eebf76 [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 Duffin22595f92020-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
Colin Cross1d11c872020-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 Cross5bc17442020-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
179func (a *AndroidMkEntries) fillInEntries(config Config, bpPath string, mod blueprint.Module) {
180 a.EntryMap = make(map[string][]string)
181 amod := mod.(Module).base()
182 name := amod.BaseModuleName()
183
184 if a.Include == "" {
185 a.Include = "$(BUILD_PREBUILT)"
186 }
187 a.Required = append(a.Required, amod.commonProperties.Required...)
188 a.Host_required = append(a.Host_required, amod.commonProperties.Host_required...)
189 a.Target_required = append(a.Target_required, amod.commonProperties.Target_required...)
190
191 // Fill in the header part.
192 if len(amod.commonProperties.Dist.Targets) > 0 {
193 distFile := a.DistFile
194 if !distFile.Valid() {
195 distFile = a.OutputFile
196 }
197 if distFile.Valid() {
198 dest := filepath.Base(distFile.String())
199
200 if amod.commonProperties.Dist.Dest != nil {
201 var err error
202 if dest, err = validateSafePath(*amod.commonProperties.Dist.Dest); err != nil {
203 // This was checked in ModuleBase.GenerateBuildActions
204 panic(err)
205 }
206 }
207
208 if amod.commonProperties.Dist.Suffix != nil {
209 ext := filepath.Ext(dest)
210 suffix := *amod.commonProperties.Dist.Suffix
211 dest = strings.TrimSuffix(dest, ext) + suffix + ext
212 }
213
214 if amod.commonProperties.Dist.Dir != nil {
215 var err error
216 if dest, err = validateSafePath(*amod.commonProperties.Dist.Dir, dest); err != nil {
217 // This was checked in ModuleBase.GenerateBuildActions
218 panic(err)
219 }
220 }
221
222 goals := strings.Join(amod.commonProperties.Dist.Targets, " ")
223 fmt.Fprintln(&a.header, ".PHONY:", goals)
224 fmt.Fprintf(&a.header, "$(call dist-for-goals,%s,%s:%s)\n",
225 goals, distFile.String(), dest)
226 }
227 }
228
229 fmt.Fprintln(&a.header, "\ninclude $(CLEAR_VARS)")
230
231 // Collect make variable assignment entries.
232 a.SetString("LOCAL_PATH", filepath.Dir(bpPath))
233 a.SetString("LOCAL_MODULE", name+a.SubName)
234 a.SetString("LOCAL_MODULE_CLASS", a.Class)
235 a.SetString("LOCAL_PREBUILT_MODULE_FILE", a.OutputFile.String())
236 a.AddStrings("LOCAL_REQUIRED_MODULES", a.Required...)
237 a.AddStrings("LOCAL_HOST_REQUIRED_MODULES", a.Host_required...)
238 a.AddStrings("LOCAL_TARGET_REQUIRED_MODULES", a.Target_required...)
239
Jiyong Park6a9ddc32020-04-07 16:37:39 +0900240 if am, ok := mod.(ApexModule); ok {
241 a.SetBoolIfTrue("LOCAL_NOT_AVAILABLE_FOR_PLATFORM", am.NotAvailableForPlatform())
242 }
243
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700244 archStr := amod.Arch().ArchType.String()
245 host := false
246 switch amod.Os().Class {
247 case Host:
248 // Make cannot identify LOCAL_MODULE_HOST_ARCH:= common.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700249 if amod.Arch().ArchType != Common {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700250 a.SetString("LOCAL_MODULE_HOST_ARCH", archStr)
251 }
252 host = true
253 case HostCross:
254 // Make cannot identify LOCAL_MODULE_HOST_CROSS_ARCH:= common.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700255 if amod.Arch().ArchType != Common {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700256 a.SetString("LOCAL_MODULE_HOST_CROSS_ARCH", archStr)
257 }
258 host = true
259 case Device:
260 // Make cannot identify LOCAL_MODULE_TARGET_ARCH:= common.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700261 if amod.Arch().ArchType != Common {
dimitry1f33e402019-03-26 12:39:31 +0100262 if amod.Target().NativeBridge {
dimitry8d6dde82019-07-11 10:23:53 +0200263 hostArchStr := amod.Target().NativeBridgeHostArchName
dimitry1f33e402019-03-26 12:39:31 +0100264 if hostArchStr != "" {
265 a.SetString("LOCAL_MODULE_TARGET_ARCH", hostArchStr)
266 }
267 } else {
268 a.SetString("LOCAL_MODULE_TARGET_ARCH", archStr)
269 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700270 }
271
272 a.AddStrings("LOCAL_INIT_RC", amod.commonProperties.Init_rc...)
273 a.AddStrings("LOCAL_VINTF_FRAGMENTS", amod.commonProperties.Vintf_fragments...)
274 a.SetBoolIfTrue("LOCAL_PROPRIETARY_MODULE", Bool(amod.commonProperties.Proprietary))
275 if Bool(amod.commonProperties.Vendor) || Bool(amod.commonProperties.Soc_specific) {
276 a.SetString("LOCAL_VENDOR_MODULE", "true")
277 }
278 a.SetBoolIfTrue("LOCAL_ODM_MODULE", Bool(amod.commonProperties.Device_specific))
279 a.SetBoolIfTrue("LOCAL_PRODUCT_MODULE", Bool(amod.commonProperties.Product_specific))
Justin Yund5f6c822019-06-25 16:47:17 +0900280 a.SetBoolIfTrue("LOCAL_SYSTEM_EXT_MODULE", Bool(amod.commonProperties.System_ext_specific))
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700281 if amod.commonProperties.Owner != nil {
282 a.SetString("LOCAL_MODULE_OWNER", *amod.commonProperties.Owner)
283 }
284 }
285
286 if amod.noticeFile.Valid() {
287 a.SetString("LOCAL_NOTICE_FILE", amod.noticeFile.String())
288 }
289
290 if host {
291 makeOs := amod.Os().String()
292 if amod.Os() == Linux || amod.Os() == LinuxBionic {
293 makeOs = "linux"
294 }
295 a.SetString("LOCAL_MODULE_HOST_OS", makeOs)
296 a.SetString("LOCAL_IS_HOST_MODULE", "true")
297 }
298
299 prefix := ""
300 if amod.ArchSpecific() {
301 switch amod.Os().Class {
302 case Host:
303 prefix = "HOST_"
304 case HostCross:
305 prefix = "HOST_CROSS_"
306 case Device:
307 prefix = "TARGET_"
308
309 }
310
311 if amod.Arch().ArchType != config.Targets[amod.Os()][0].Arch.ArchType {
312 prefix = "2ND_" + prefix
313 }
314 }
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700315 for _, extra := range a.ExtraEntries {
316 extra(a)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700317 }
318
319 // Write to footer.
320 fmt.Fprintln(&a.footer, "include "+a.Include)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700321 blueprintDir := filepath.Dir(bpPath)
322 for _, footerFunc := range a.ExtraFooters {
323 footerFunc(&a.footer, name, prefix, blueprintDir, a)
324 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700325}
326
327func (a *AndroidMkEntries) write(w io.Writer) {
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700328 if a.Disabled {
329 return
330 }
331
332 if !a.OutputFile.Valid() {
333 return
334 }
335
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700336 w.Write(a.header.Bytes())
337 for _, name := range a.entryOrder {
338 fmt.Fprintln(w, name+" := "+strings.Join(a.EntryMap[name], " "))
339 }
340 w.Write(a.footer.Bytes())
341}
342
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700343func (a *AndroidMkEntries) FooterLinesForTests() []string {
344 return strings.Split(string(a.footer.Bytes()), "\n")
345}
346
Colin Cross0875c522017-11-28 17:34:01 -0800347func AndroidMkSingleton() Singleton {
Dan Willemsen218f6562015-07-08 18:13:11 -0700348 return &androidMkSingleton{}
349}
350
351type androidMkSingleton struct{}
352
Colin Cross0875c522017-11-28 17:34:01 -0800353func (c *androidMkSingleton) GenerateBuildActions(ctx SingletonContext) {
Colin Crossaabf6792017-11-29 00:27:14 -0800354 if !ctx.Config().EmbeddedInMake() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800355 return
356 }
357
Colin Cross2465c3d2018-09-28 10:19:18 -0700358 var androidMkModulesList []blueprint.Module
Colin Cross4f6e4e62016-01-11 12:55:55 -0800359
Colin Cross2465c3d2018-09-28 10:19:18 -0700360 ctx.VisitAllModulesBlueprint(func(module blueprint.Module) {
Colin Cross0875c522017-11-28 17:34:01 -0800361 androidMkModulesList = append(androidMkModulesList, module)
Colin Cross4f6e4e62016-01-11 12:55:55 -0800362 })
Dan Willemsen218f6562015-07-08 18:13:11 -0700363
Colin Cross1ad81422019-01-14 12:47:35 -0800364 sort.SliceStable(androidMkModulesList, func(i, j int) bool {
365 return ctx.ModuleName(androidMkModulesList[i]) < ctx.ModuleName(androidMkModulesList[j])
366 })
Colin Crossd779da42015-12-17 18:00:23 -0800367
Dan Willemsen45133ac2018-03-09 21:22:06 -0800368 transMk := PathForOutput(ctx, "Android"+String(ctx.Config().productVariables.Make_suffix)+".mk")
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700369 if ctx.Failed() {
370 return
371 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700372
Colin Cross988414c2020-01-11 01:11:46 +0000373 err := translateAndroidMk(ctx, absolutePath(transMk.String()), androidMkModulesList)
Dan Willemsen218f6562015-07-08 18:13:11 -0700374 if err != nil {
375 ctx.Errorf(err.Error())
376 }
377
Colin Cross0875c522017-11-28 17:34:01 -0800378 ctx.Build(pctx, BuildParams{
379 Rule: blueprint.Phony,
380 Output: transMk,
Dan Willemsen218f6562015-07-08 18:13:11 -0700381 })
382}
383
Colin Cross2465c3d2018-09-28 10:19:18 -0700384func translateAndroidMk(ctx SingletonContext, mkFile string, mods []blueprint.Module) error {
Dan Willemsen218f6562015-07-08 18:13:11 -0700385 buf := &bytes.Buffer{}
386
Dan Willemsen97750522016-02-09 17:43:51 -0800387 fmt.Fprintln(buf, "LOCAL_MODULE_MAKEFILE := $(lastword $(MAKEFILE_LIST))")
Dan Willemsen218f6562015-07-08 18:13:11 -0700388
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700389 type_stats := make(map[string]int)
Dan Willemsen218f6562015-07-08 18:13:11 -0700390 for _, mod := range mods {
391 err := translateAndroidMkModule(ctx, buf, mod)
392 if err != nil {
393 os.Remove(mkFile)
394 return err
395 }
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700396
Colin Cross2465c3d2018-09-28 10:19:18 -0700397 if amod, ok := mod.(Module); ok && ctx.PrimaryModule(amod) == amod {
398 type_stats[ctx.ModuleType(amod)] += 1
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700399 }
400 }
401
402 keys := []string{}
403 fmt.Fprintln(buf, "\nSTATS.SOONG_MODULE_TYPE :=")
404 for k := range type_stats {
405 keys = append(keys, k)
406 }
407 sort.Strings(keys)
408 for _, mod_type := range keys {
409 fmt.Fprintln(buf, "STATS.SOONG_MODULE_TYPE +=", mod_type)
410 fmt.Fprintf(buf, "STATS.SOONG_MODULE_TYPE.%s := %d\n", mod_type, type_stats[mod_type])
Dan Willemsen218f6562015-07-08 18:13:11 -0700411 }
412
413 // Don't write to the file if it hasn't changed
Colin Cross988414c2020-01-11 01:11:46 +0000414 if _, err := os.Stat(absolutePath(mkFile)); !os.IsNotExist(err) {
415 if data, err := ioutil.ReadFile(absolutePath(mkFile)); err == nil {
Dan Willemsen218f6562015-07-08 18:13:11 -0700416 matches := buf.Len() == len(data)
417
418 if matches {
419 for i, value := range buf.Bytes() {
420 if value != data[i] {
421 matches = false
422 break
423 }
424 }
425 }
426
427 if matches {
428 return nil
429 }
430 }
431 }
432
Colin Cross988414c2020-01-11 01:11:46 +0000433 return ioutil.WriteFile(absolutePath(mkFile), buf.Bytes(), 0666)
Dan Willemsen218f6562015-07-08 18:13:11 -0700434}
435
Colin Cross0875c522017-11-28 17:34:01 -0800436func translateAndroidMkModule(ctx SingletonContext, w io.Writer, mod blueprint.Module) error {
Colin Cross953d3a22018-09-05 16:23:54 -0700437 defer func() {
438 if r := recover(); r != nil {
439 panic(fmt.Errorf("%s in translateAndroidMkModule for module %s variant %s",
440 r, ctx.ModuleName(mod), ctx.ModuleSubDir(mod)))
441 }
442 }()
443
Colin Cross2465c3d2018-09-28 10:19:18 -0700444 switch x := mod.(type) {
445 case AndroidMkDataProvider:
446 return translateAndroidModule(ctx, w, mod, x)
447 case bootstrap.GoBinaryTool:
448 return translateGoBinaryModule(ctx, w, mod, x)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700449 case AndroidMkEntriesProvider:
450 return translateAndroidMkEntriesModule(ctx, w, mod, x)
Colin Cross2465c3d2018-09-28 10:19:18 -0700451 default:
Dan Willemsen218f6562015-07-08 18:13:11 -0700452 return nil
453 }
Colin Cross2465c3d2018-09-28 10:19:18 -0700454}
455
456func translateGoBinaryModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
457 goBinary bootstrap.GoBinaryTool) error {
458
459 name := ctx.ModuleName(mod)
460 fmt.Fprintln(w, ".PHONY:", name)
461 fmt.Fprintln(w, name+":", goBinary.InstallPath())
462 fmt.Fprintln(w, "")
463
464 return nil
465}
466
Jooyung Han12df5fb2019-07-11 16:18:47 +0900467func (data *AndroidMkData) fillInData(config Config, bpPath string, mod blueprint.Module) {
468 // Get the preamble content through AndroidMkEntries logic.
469 entries := AndroidMkEntries{
470 Class: data.Class,
471 SubName: data.SubName,
472 DistFile: data.DistFile,
473 OutputFile: data.OutputFile,
474 Disabled: data.Disabled,
475 Include: data.Include,
476 Required: data.Required,
477 Host_required: data.Host_required,
478 Target_required: data.Target_required,
479 }
480 entries.fillInEntries(config, bpPath, mod)
481
482 // preamble doesn't need the footer content.
483 entries.footer = bytes.Buffer{}
484 entries.write(&data.preamble)
485
486 // copy entries back to data since it is used in Custom
487 data.Required = entries.Required
488 data.Host_required = entries.Host_required
489 data.Target_required = entries.Target_required
490}
491
Colin Cross2465c3d2018-09-28 10:19:18 -0700492func translateAndroidModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
493 provider AndroidMkDataProvider) error {
Dan Willemsen218f6562015-07-08 18:13:11 -0700494
Colin Cross635c3b02016-05-18 15:37:25 -0700495 amod := mod.(Module).base()
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700496 if shouldSkipAndroidMkProcessing(amod) {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800497 return nil
498 }
499
Colin Cross91825d22017-08-10 16:59:47 -0700500 data := provider.AndroidMk()
Colin Cross53499412017-09-07 13:20:25 -0700501 if data.Include == "" {
502 data.Include = "$(BUILD_PREBUILT)"
503 }
504
Jooyung Han12df5fb2019-07-11 16:18:47 +0900505 data.fillInData(ctx.Config(), ctx.BlueprintFile(mod), mod)
Dan Willemsen01a405a2016-06-13 17:19:03 -0700506
Colin Cross0f86d182017-08-10 17:07:28 -0700507 prefix := ""
508 if amod.ArchSpecific() {
509 switch amod.Os().Class {
510 case Host:
511 prefix = "HOST_"
512 case HostCross:
513 prefix = "HOST_CROSS_"
514 case Device:
515 prefix = "TARGET_"
Colin Crossa2344662016-03-24 13:14:12 -0700516
Dan Willemsen218f6562015-07-08 18:13:11 -0700517 }
518
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700519 if amod.Arch().ArchType != ctx.Config().Targets[amod.Os()][0].Arch.ArchType {
Colin Cross0f86d182017-08-10 17:07:28 -0700520 prefix = "2ND_" + prefix
521 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700522 }
523
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700524 name := provider.BaseModuleName()
Colin Cross0f86d182017-08-10 17:07:28 -0700525 blueprintDir := filepath.Dir(ctx.BlueprintFile(mod))
526
527 if data.Custom != nil {
528 data.Custom(w, name, prefix, blueprintDir, data)
529 } else {
530 WriteAndroidMkData(w, data)
531 }
532
533 return nil
534}
535
536func WriteAndroidMkData(w io.Writer, data AndroidMkData) {
537 if data.Disabled {
538 return
539 }
540
541 if !data.OutputFile.Valid() {
542 return
543 }
544
545 w.Write(data.preamble.Bytes())
546
Colin Crossca860ac2016-01-04 14:34:37 -0800547 for _, extra := range data.Extra {
Colin Cross27a4b052017-08-10 16:32:23 -0700548 extra(w, data.OutputFile.Path())
Dan Willemsen97750522016-02-09 17:43:51 -0800549 }
550
Colin Cross53499412017-09-07 13:20:25 -0700551 fmt.Fprintln(w, "include "+data.Include)
Dan Willemsen218f6562015-07-08 18:13:11 -0700552}
Sasha Smundakb6d23052019-04-01 18:37:36 -0700553
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700554func translateAndroidMkEntriesModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
555 provider AndroidMkEntriesProvider) error {
556 if shouldSkipAndroidMkProcessing(mod.(Module).base()) {
557 return nil
Sasha Smundakb6d23052019-04-01 18:37:36 -0700558 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700559
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900560 for _, entries := range provider.AndroidMkEntries() {
561 entries.fillInEntries(ctx.Config(), ctx.BlueprintFile(mod), mod)
562 entries.write(w)
563 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700564
565 return nil
566}
567
568func shouldSkipAndroidMkProcessing(module *ModuleBase) bool {
569 if !module.commonProperties.NamespaceExportedToMake {
570 // TODO(jeffrygaston) do we want to validate that there are no modules being
571 // exported to Kati that depend on this module?
572 return true
Sasha Smundakb6d23052019-04-01 18:37:36 -0700573 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700574
575 return !module.Enabled() ||
576 module.commonProperties.SkipInstall ||
577 // Make does not understand LinuxBionic
578 module.Os() == LinuxBionic
Sasha Smundakb6d23052019-04-01 18:37:36 -0700579}