blob: 045cb59b3f770c5a512ddd36fbede1e34eb81eb3 [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
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
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000140// Compute the list of Make strings to declare phone goals and dist-for-goals
141// calls from the module's dist and dists properties.
142func (a *AndroidMkEntries) GetDistForGoals(mod blueprint.Module) []string {
143 amod := mod.(Module).base()
144 name := amod.BaseModuleName()
145
146 var ret []string
147
148 availableTaggedDists := TaggedDistFiles{}
149 if a.DistFiles != nil && len(a.DistFiles[""]) > 0 {
150 availableTaggedDists = a.DistFiles
151 } else if a.OutputFile.Valid() {
152 availableTaggedDists = MakeDefaultDistFiles(a.OutputFile.Path())
153 }
154
155 // Iterate over this module's dist structs, merged from the dist and dists properties.
156 for _, dist := range amod.Dists() {
157 // Get the list of goals this dist should be enabled for. e.g. sdk, droidcore
158 goals := strings.Join(dist.Targets, " ")
159
160 // Get the tag representing the output files to be dist'd. e.g. ".jar", ".proguard_map"
161 var tag string
162 if dist.Tag == nil {
163 // If the dist struct does not specify a tag, use the default output files tag.
164 tag = ""
165 } else {
166 tag = *dist.Tag
167 }
168
169 // Get the paths of the output files to be dist'd, represented by the tag.
170 // Can be an empty list.
171 tagPaths := availableTaggedDists[tag]
172 if len(tagPaths) == 0 {
173 // Nothing to dist for this tag, continue to the next dist.
174 continue
175 }
176
177 if len(tagPaths) > 1 && (dist.Dest != nil || dist.Suffix != nil) {
178 errorMessage := "Cannot apply dest/suffix for more than one dist " +
179 "file for %s goals in module %s. The list of dist files, " +
180 "which should have a single element, is:\n%s"
181 panic(fmt.Errorf(errorMessage, goals, name, tagPaths))
182 }
183
184 ret = append(ret, fmt.Sprintf(".PHONY: %s\n", goals))
185
186 // Create dist-for-goals calls for each path in the dist'd files.
187 for _, path := range tagPaths {
188 // It's possible that the Path is nil from errant modules. Be defensive here.
189 if path == nil {
190 tagName := "default" // for error message readability
191 if dist.Tag != nil {
192 tagName = *dist.Tag
193 }
194 panic(fmt.Errorf("Dist file should not be nil for the %s tag in %s", tagName, name))
195 }
196
197 dest := filepath.Base(path.String())
198
199 if dist.Dest != nil {
200 var err error
201 if dest, err = validateSafePath(*dist.Dest); err != nil {
202 // This was checked in ModuleBase.GenerateBuildActions
203 panic(err)
204 }
205 }
206
207 if dist.Suffix != nil {
208 ext := filepath.Ext(dest)
209 suffix := *dist.Suffix
210 dest = strings.TrimSuffix(dest, ext) + suffix + ext
211 }
212
213 if dist.Dir != nil {
214 var err error
215 if dest, err = validateSafePath(*dist.Dir, dest); err != nil {
216 // This was checked in ModuleBase.GenerateBuildActions
217 panic(err)
218 }
219 }
220
221 ret = append(
222 ret,
223 fmt.Sprintf("$(call dist-for-goals,%s,%s:%s)\n", goals, path.String(), dest))
224 }
225 }
226
227 return ret
228}
229
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700230func (a *AndroidMkEntries) fillInEntries(config Config, bpPath string, mod blueprint.Module) {
231 a.EntryMap = make(map[string][]string)
232 amod := mod.(Module).base()
233 name := amod.BaseModuleName()
234
235 if a.Include == "" {
236 a.Include = "$(BUILD_PREBUILT)"
237 }
238 a.Required = append(a.Required, amod.commonProperties.Required...)
239 a.Host_required = append(a.Host_required, amod.commonProperties.Host_required...)
240 a.Target_required = append(a.Target_required, amod.commonProperties.Target_required...)
241
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000242 for _, distString := range a.GetDistForGoals(mod) {
243 fmt.Fprintf(&a.header, distString)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700244 }
245
246 fmt.Fprintln(&a.header, "\ninclude $(CLEAR_VARS)")
247
248 // Collect make variable assignment entries.
249 a.SetString("LOCAL_PATH", filepath.Dir(bpPath))
250 a.SetString("LOCAL_MODULE", name+a.SubName)
251 a.SetString("LOCAL_MODULE_CLASS", a.Class)
252 a.SetString("LOCAL_PREBUILT_MODULE_FILE", a.OutputFile.String())
253 a.AddStrings("LOCAL_REQUIRED_MODULES", a.Required...)
254 a.AddStrings("LOCAL_HOST_REQUIRED_MODULES", a.Host_required...)
255 a.AddStrings("LOCAL_TARGET_REQUIRED_MODULES", a.Target_required...)
256
Jiyong Park89e850a2020-04-07 16:37:39 +0900257 if am, ok := mod.(ApexModule); ok {
258 a.SetBoolIfTrue("LOCAL_NOT_AVAILABLE_FOR_PLATFORM", am.NotAvailableForPlatform())
259 }
260
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700261 archStr := amod.Arch().ArchType.String()
262 host := false
263 switch amod.Os().Class {
264 case Host:
265 // Make cannot identify LOCAL_MODULE_HOST_ARCH:= common.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700266 if amod.Arch().ArchType != Common {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700267 a.SetString("LOCAL_MODULE_HOST_ARCH", archStr)
268 }
269 host = true
270 case HostCross:
271 // Make cannot identify LOCAL_MODULE_HOST_CROSS_ARCH:= common.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700272 if amod.Arch().ArchType != Common {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700273 a.SetString("LOCAL_MODULE_HOST_CROSS_ARCH", archStr)
274 }
275 host = true
276 case Device:
277 // Make cannot identify LOCAL_MODULE_TARGET_ARCH:= common.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700278 if amod.Arch().ArchType != Common {
dimitry1f33e402019-03-26 12:39:31 +0100279 if amod.Target().NativeBridge {
dimitry8d6dde82019-07-11 10:23:53 +0200280 hostArchStr := amod.Target().NativeBridgeHostArchName
dimitry1f33e402019-03-26 12:39:31 +0100281 if hostArchStr != "" {
282 a.SetString("LOCAL_MODULE_TARGET_ARCH", hostArchStr)
283 }
284 } else {
285 a.SetString("LOCAL_MODULE_TARGET_ARCH", archStr)
286 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700287 }
288
289 a.AddStrings("LOCAL_INIT_RC", amod.commonProperties.Init_rc...)
290 a.AddStrings("LOCAL_VINTF_FRAGMENTS", amod.commonProperties.Vintf_fragments...)
291 a.SetBoolIfTrue("LOCAL_PROPRIETARY_MODULE", Bool(amod.commonProperties.Proprietary))
292 if Bool(amod.commonProperties.Vendor) || Bool(amod.commonProperties.Soc_specific) {
293 a.SetString("LOCAL_VENDOR_MODULE", "true")
294 }
295 a.SetBoolIfTrue("LOCAL_ODM_MODULE", Bool(amod.commonProperties.Device_specific))
296 a.SetBoolIfTrue("LOCAL_PRODUCT_MODULE", Bool(amod.commonProperties.Product_specific))
Justin Yund5f6c822019-06-25 16:47:17 +0900297 a.SetBoolIfTrue("LOCAL_SYSTEM_EXT_MODULE", Bool(amod.commonProperties.System_ext_specific))
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700298 if amod.commonProperties.Owner != nil {
299 a.SetString("LOCAL_MODULE_OWNER", *amod.commonProperties.Owner)
300 }
301 }
302
Bob Badoura75b0572020-02-18 20:21:55 -0800303 if len(amod.noticeFiles) > 0 {
304 a.SetString("LOCAL_NOTICE_FILE", strings.Join(amod.noticeFiles.Strings(), " "))
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700305 }
306
307 if host {
308 makeOs := amod.Os().String()
309 if amod.Os() == Linux || amod.Os() == LinuxBionic {
310 makeOs = "linux"
311 }
312 a.SetString("LOCAL_MODULE_HOST_OS", makeOs)
313 a.SetString("LOCAL_IS_HOST_MODULE", "true")
314 }
315
316 prefix := ""
317 if amod.ArchSpecific() {
318 switch amod.Os().Class {
319 case Host:
320 prefix = "HOST_"
321 case HostCross:
322 prefix = "HOST_CROSS_"
323 case Device:
324 prefix = "TARGET_"
325
326 }
327
328 if amod.Arch().ArchType != config.Targets[amod.Os()][0].Arch.ArchType {
329 prefix = "2ND_" + prefix
330 }
331 }
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700332 for _, extra := range a.ExtraEntries {
333 extra(a)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700334 }
335
336 // Write to footer.
337 fmt.Fprintln(&a.footer, "include "+a.Include)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700338 blueprintDir := filepath.Dir(bpPath)
339 for _, footerFunc := range a.ExtraFooters {
340 footerFunc(&a.footer, name, prefix, blueprintDir, a)
341 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700342}
343
344func (a *AndroidMkEntries) write(w io.Writer) {
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700345 if a.Disabled {
346 return
347 }
348
349 if !a.OutputFile.Valid() {
350 return
351 }
352
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700353 w.Write(a.header.Bytes())
354 for _, name := range a.entryOrder {
355 fmt.Fprintln(w, name+" := "+strings.Join(a.EntryMap[name], " "))
356 }
357 w.Write(a.footer.Bytes())
358}
359
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700360func (a *AndroidMkEntries) FooterLinesForTests() []string {
361 return strings.Split(string(a.footer.Bytes()), "\n")
362}
363
Colin Cross0875c522017-11-28 17:34:01 -0800364func AndroidMkSingleton() Singleton {
Dan Willemsen218f6562015-07-08 18:13:11 -0700365 return &androidMkSingleton{}
366}
367
368type androidMkSingleton struct{}
369
Colin Cross0875c522017-11-28 17:34:01 -0800370func (c *androidMkSingleton) GenerateBuildActions(ctx SingletonContext) {
Colin Crossaabf6792017-11-29 00:27:14 -0800371 if !ctx.Config().EmbeddedInMake() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800372 return
373 }
374
Colin Cross2465c3d2018-09-28 10:19:18 -0700375 var androidMkModulesList []blueprint.Module
Colin Cross4f6e4e62016-01-11 12:55:55 -0800376
Colin Cross2465c3d2018-09-28 10:19:18 -0700377 ctx.VisitAllModulesBlueprint(func(module blueprint.Module) {
Colin Cross0875c522017-11-28 17:34:01 -0800378 androidMkModulesList = append(androidMkModulesList, module)
Colin Cross4f6e4e62016-01-11 12:55:55 -0800379 })
Dan Willemsen218f6562015-07-08 18:13:11 -0700380
Colin Cross1ad81422019-01-14 12:47:35 -0800381 sort.SliceStable(androidMkModulesList, func(i, j int) bool {
382 return ctx.ModuleName(androidMkModulesList[i]) < ctx.ModuleName(androidMkModulesList[j])
383 })
Colin Crossd779da42015-12-17 18:00:23 -0800384
Dan Willemsen45133ac2018-03-09 21:22:06 -0800385 transMk := PathForOutput(ctx, "Android"+String(ctx.Config().productVariables.Make_suffix)+".mk")
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700386 if ctx.Failed() {
387 return
388 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700389
Colin Cross988414c2020-01-11 01:11:46 +0000390 err := translateAndroidMk(ctx, absolutePath(transMk.String()), androidMkModulesList)
Dan Willemsen218f6562015-07-08 18:13:11 -0700391 if err != nil {
392 ctx.Errorf(err.Error())
393 }
394
Colin Cross0875c522017-11-28 17:34:01 -0800395 ctx.Build(pctx, BuildParams{
396 Rule: blueprint.Phony,
397 Output: transMk,
Dan Willemsen218f6562015-07-08 18:13:11 -0700398 })
399}
400
Colin Cross2465c3d2018-09-28 10:19:18 -0700401func translateAndroidMk(ctx SingletonContext, mkFile string, mods []blueprint.Module) error {
Dan Willemsen218f6562015-07-08 18:13:11 -0700402 buf := &bytes.Buffer{}
403
Dan Willemsen97750522016-02-09 17:43:51 -0800404 fmt.Fprintln(buf, "LOCAL_MODULE_MAKEFILE := $(lastword $(MAKEFILE_LIST))")
Dan Willemsen218f6562015-07-08 18:13:11 -0700405
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700406 type_stats := make(map[string]int)
Dan Willemsen218f6562015-07-08 18:13:11 -0700407 for _, mod := range mods {
408 err := translateAndroidMkModule(ctx, buf, mod)
409 if err != nil {
410 os.Remove(mkFile)
411 return err
412 }
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700413
Colin Cross2465c3d2018-09-28 10:19:18 -0700414 if amod, ok := mod.(Module); ok && ctx.PrimaryModule(amod) == amod {
415 type_stats[ctx.ModuleType(amod)] += 1
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700416 }
417 }
418
419 keys := []string{}
420 fmt.Fprintln(buf, "\nSTATS.SOONG_MODULE_TYPE :=")
421 for k := range type_stats {
422 keys = append(keys, k)
423 }
424 sort.Strings(keys)
425 for _, mod_type := range keys {
426 fmt.Fprintln(buf, "STATS.SOONG_MODULE_TYPE +=", mod_type)
427 fmt.Fprintf(buf, "STATS.SOONG_MODULE_TYPE.%s := %d\n", mod_type, type_stats[mod_type])
Dan Willemsen218f6562015-07-08 18:13:11 -0700428 }
429
430 // Don't write to the file if it hasn't changed
Colin Cross988414c2020-01-11 01:11:46 +0000431 if _, err := os.Stat(absolutePath(mkFile)); !os.IsNotExist(err) {
432 if data, err := ioutil.ReadFile(absolutePath(mkFile)); err == nil {
Dan Willemsen218f6562015-07-08 18:13:11 -0700433 matches := buf.Len() == len(data)
434
435 if matches {
436 for i, value := range buf.Bytes() {
437 if value != data[i] {
438 matches = false
439 break
440 }
441 }
442 }
443
444 if matches {
445 return nil
446 }
447 }
448 }
449
Colin Cross988414c2020-01-11 01:11:46 +0000450 return ioutil.WriteFile(absolutePath(mkFile), buf.Bytes(), 0666)
Dan Willemsen218f6562015-07-08 18:13:11 -0700451}
452
Colin Cross0875c522017-11-28 17:34:01 -0800453func translateAndroidMkModule(ctx SingletonContext, w io.Writer, mod blueprint.Module) error {
Colin Cross953d3a22018-09-05 16:23:54 -0700454 defer func() {
455 if r := recover(); r != nil {
456 panic(fmt.Errorf("%s in translateAndroidMkModule for module %s variant %s",
457 r, ctx.ModuleName(mod), ctx.ModuleSubDir(mod)))
458 }
459 }()
460
Colin Cross2465c3d2018-09-28 10:19:18 -0700461 switch x := mod.(type) {
462 case AndroidMkDataProvider:
463 return translateAndroidModule(ctx, w, mod, x)
464 case bootstrap.GoBinaryTool:
465 return translateGoBinaryModule(ctx, w, mod, x)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700466 case AndroidMkEntriesProvider:
467 return translateAndroidMkEntriesModule(ctx, w, mod, x)
Colin Cross2465c3d2018-09-28 10:19:18 -0700468 default:
Dan Willemsen218f6562015-07-08 18:13:11 -0700469 return nil
470 }
Colin Cross2465c3d2018-09-28 10:19:18 -0700471}
472
473func translateGoBinaryModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
474 goBinary bootstrap.GoBinaryTool) error {
475
476 name := ctx.ModuleName(mod)
477 fmt.Fprintln(w, ".PHONY:", name)
478 fmt.Fprintln(w, name+":", goBinary.InstallPath())
479 fmt.Fprintln(w, "")
480
481 return nil
482}
483
Jooyung Han12df5fb2019-07-11 16:18:47 +0900484func (data *AndroidMkData) fillInData(config Config, bpPath string, mod blueprint.Module) {
485 // Get the preamble content through AndroidMkEntries logic.
Jooyung Han2ed99d02020-06-24 23:26:26 +0900486 data.Entries = AndroidMkEntries{
Jooyung Han12df5fb2019-07-11 16:18:47 +0900487 Class: data.Class,
488 SubName: data.SubName,
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000489 DistFiles: data.DistFiles,
Jooyung Han12df5fb2019-07-11 16:18:47 +0900490 OutputFile: data.OutputFile,
491 Disabled: data.Disabled,
492 Include: data.Include,
493 Required: data.Required,
494 Host_required: data.Host_required,
495 Target_required: data.Target_required,
496 }
Jooyung Han2ed99d02020-06-24 23:26:26 +0900497 data.Entries.fillInEntries(config, bpPath, mod)
Jooyung Han12df5fb2019-07-11 16:18:47 +0900498
499 // copy entries back to data since it is used in Custom
Jooyung Han2ed99d02020-06-24 23:26:26 +0900500 data.Required = data.Entries.Required
501 data.Host_required = data.Entries.Host_required
502 data.Target_required = data.Entries.Target_required
Jooyung Han12df5fb2019-07-11 16:18:47 +0900503}
504
Colin Cross2465c3d2018-09-28 10:19:18 -0700505func translateAndroidModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
506 provider AndroidMkDataProvider) error {
Dan Willemsen218f6562015-07-08 18:13:11 -0700507
Colin Cross635c3b02016-05-18 15:37:25 -0700508 amod := mod.(Module).base()
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700509 if shouldSkipAndroidMkProcessing(amod) {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800510 return nil
511 }
512
Colin Cross91825d22017-08-10 16:59:47 -0700513 data := provider.AndroidMk()
Colin Cross53499412017-09-07 13:20:25 -0700514 if data.Include == "" {
515 data.Include = "$(BUILD_PREBUILT)"
516 }
517
Jooyung Han12df5fb2019-07-11 16:18:47 +0900518 data.fillInData(ctx.Config(), ctx.BlueprintFile(mod), mod)
Dan Willemsen01a405a2016-06-13 17:19:03 -0700519
Colin Cross0f86d182017-08-10 17:07:28 -0700520 prefix := ""
521 if amod.ArchSpecific() {
522 switch amod.Os().Class {
523 case Host:
524 prefix = "HOST_"
525 case HostCross:
526 prefix = "HOST_CROSS_"
527 case Device:
528 prefix = "TARGET_"
Colin Crossa2344662016-03-24 13:14:12 -0700529
Dan Willemsen218f6562015-07-08 18:13:11 -0700530 }
531
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700532 if amod.Arch().ArchType != ctx.Config().Targets[amod.Os()][0].Arch.ArchType {
Colin Cross0f86d182017-08-10 17:07:28 -0700533 prefix = "2ND_" + prefix
534 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700535 }
536
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700537 name := provider.BaseModuleName()
Colin Cross0f86d182017-08-10 17:07:28 -0700538 blueprintDir := filepath.Dir(ctx.BlueprintFile(mod))
539
540 if data.Custom != nil {
541 data.Custom(w, name, prefix, blueprintDir, data)
542 } else {
543 WriteAndroidMkData(w, data)
544 }
545
546 return nil
547}
548
549func WriteAndroidMkData(w io.Writer, data AndroidMkData) {
550 if data.Disabled {
551 return
552 }
553
554 if !data.OutputFile.Valid() {
555 return
556 }
557
Jooyung Han2ed99d02020-06-24 23:26:26 +0900558 // write preamble via Entries
559 data.Entries.footer = bytes.Buffer{}
560 data.Entries.write(w)
Colin Cross0f86d182017-08-10 17:07:28 -0700561
Colin Crossca860ac2016-01-04 14:34:37 -0800562 for _, extra := range data.Extra {
Colin Cross27a4b052017-08-10 16:32:23 -0700563 extra(w, data.OutputFile.Path())
Dan Willemsen97750522016-02-09 17:43:51 -0800564 }
565
Colin Cross53499412017-09-07 13:20:25 -0700566 fmt.Fprintln(w, "include "+data.Include)
Dan Willemsen218f6562015-07-08 18:13:11 -0700567}
Sasha Smundakb6d23052019-04-01 18:37:36 -0700568
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700569func translateAndroidMkEntriesModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
570 provider AndroidMkEntriesProvider) error {
571 if shouldSkipAndroidMkProcessing(mod.(Module).base()) {
572 return nil
Sasha Smundakb6d23052019-04-01 18:37:36 -0700573 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700574
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900575 for _, entries := range provider.AndroidMkEntries() {
576 entries.fillInEntries(ctx.Config(), ctx.BlueprintFile(mod), mod)
577 entries.write(w)
578 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700579
580 return nil
581}
582
583func shouldSkipAndroidMkProcessing(module *ModuleBase) bool {
584 if !module.commonProperties.NamespaceExportedToMake {
585 // TODO(jeffrygaston) do we want to validate that there are no modules being
586 // exported to Kati that depend on this module?
587 return true
Sasha Smundakb6d23052019-04-01 18:37:36 -0700588 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700589
590 return !module.Enabled() ||
591 module.commonProperties.SkipInstall ||
592 // Make does not understand LinuxBionic
593 module.Os() == LinuxBionic
Sasha Smundakb6d23052019-04-01 18:37:36 -0700594}