blob: cfd7c91de7a5b7c8d5ee96af6783c2aa253087cc [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
Colin Cross0477b422020-10-13 18:43:54 -070075 OverrideName string
Jingwen Chen40fd90a2020-06-15 05:24:19 +000076 DistFiles TaggedDistFiles
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -070077 OutputFile OptionalPath
78 Disabled bool
79 Include string
80 Required []string
81 Host_required []string
82 Target_required []string
83
84 header bytes.Buffer
85 footer bytes.Buffer
86
Jaewoong Junge0dc8df2019-08-27 17:33:16 -070087 ExtraEntries []AndroidMkExtraEntriesFunc
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070088 ExtraFooters []AndroidMkExtraFootersFunc
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -070089
90 EntryMap map[string][]string
91 entryOrder []string
92}
93
Jaewoong Junge0dc8df2019-08-27 17:33:16 -070094type AndroidMkExtraEntriesFunc func(entries *AndroidMkEntries)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070095type AndroidMkExtraFootersFunc func(w io.Writer, name, prefix, moduleDir string, entries *AndroidMkEntries)
Jaewoong Junge0dc8df2019-08-27 17:33:16 -070096
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -070097func (a *AndroidMkEntries) SetString(name, value string) {
98 if _, ok := a.EntryMap[name]; !ok {
99 a.entryOrder = append(a.entryOrder, name)
100 }
101 a.EntryMap[name] = []string{value}
102}
103
Jaewoong Jung9a1e8bd2019-09-04 20:17:54 -0700104func (a *AndroidMkEntries) SetPath(name string, path Path) {
105 if _, ok := a.EntryMap[name]; !ok {
106 a.entryOrder = append(a.entryOrder, name)
107 }
108 a.EntryMap[name] = []string{path.String()}
109}
110
Colin Crossc0efd1d2020-07-03 11:56:24 -0700111func (a *AndroidMkEntries) SetOptionalPath(name string, path OptionalPath) {
112 if path.Valid() {
113 a.SetPath(name, path.Path())
114 }
115}
116
117func (a *AndroidMkEntries) AddPath(name string, path Path) {
118 if _, ok := a.EntryMap[name]; !ok {
119 a.entryOrder = append(a.entryOrder, name)
120 }
121 a.EntryMap[name] = append(a.EntryMap[name], path.String())
122}
123
124func (a *AndroidMkEntries) AddOptionalPath(name string, path OptionalPath) {
125 if path.Valid() {
126 a.AddPath(name, path.Path())
127 }
128}
129
Colin Cross08dca382020-07-21 20:31:17 -0700130func (a *AndroidMkEntries) SetPaths(name string, paths Paths) {
131 if _, ok := a.EntryMap[name]; !ok {
132 a.entryOrder = append(a.entryOrder, name)
133 }
134 a.EntryMap[name] = paths.Strings()
135}
136
137func (a *AndroidMkEntries) SetOptionalPaths(name string, paths Paths) {
138 if len(paths) > 0 {
139 a.SetPaths(name, paths)
140 }
141}
142
143func (a *AndroidMkEntries) AddPaths(name string, paths Paths) {
144 if _, ok := a.EntryMap[name]; !ok {
145 a.entryOrder = append(a.entryOrder, name)
146 }
147 a.EntryMap[name] = append(a.EntryMap[name], paths.Strings()...)
148}
149
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700150func (a *AndroidMkEntries) SetBoolIfTrue(name string, flag bool) {
151 if flag {
152 if _, ok := a.EntryMap[name]; !ok {
153 a.entryOrder = append(a.entryOrder, name)
154 }
155 a.EntryMap[name] = []string{"true"}
156 }
157}
158
Jaewoong Jung9a1e8bd2019-09-04 20:17:54 -0700159func (a *AndroidMkEntries) SetBool(name string, flag bool) {
160 if _, ok := a.EntryMap[name]; !ok {
161 a.entryOrder = append(a.entryOrder, name)
162 }
163 if flag {
164 a.EntryMap[name] = []string{"true"}
165 } else {
166 a.EntryMap[name] = []string{"false"}
167 }
168}
169
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700170func (a *AndroidMkEntries) AddStrings(name string, value ...string) {
171 if len(value) == 0 {
172 return
173 }
174 if _, ok := a.EntryMap[name]; !ok {
175 a.entryOrder = append(a.entryOrder, name)
176 }
177 a.EntryMap[name] = append(a.EntryMap[name], value...)
178}
179
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000180// Compute the list of Make strings to declare phone goals and dist-for-goals
181// calls from the module's dist and dists properties.
182func (a *AndroidMkEntries) GetDistForGoals(mod blueprint.Module) []string {
183 amod := mod.(Module).base()
184 name := amod.BaseModuleName()
185
186 var ret []string
Jingwen Chen7b27ca72020-07-24 09:13:49 +0000187 var availableTaggedDists TaggedDistFiles
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000188
Jingwen Chen84811862020-07-21 11:32:19 +0000189 if a.DistFiles != nil {
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000190 availableTaggedDists = a.DistFiles
191 } else if a.OutputFile.Valid() {
192 availableTaggedDists = MakeDefaultDistFiles(a.OutputFile.Path())
Jingwen Chen7b27ca72020-07-24 09:13:49 +0000193 } else {
194 // Nothing dist-able for this module.
195 return nil
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000196 }
197
198 // Iterate over this module's dist structs, merged from the dist and dists properties.
199 for _, dist := range amod.Dists() {
200 // Get the list of goals this dist should be enabled for. e.g. sdk, droidcore
201 goals := strings.Join(dist.Targets, " ")
202
203 // Get the tag representing the output files to be dist'd. e.g. ".jar", ".proguard_map"
204 var tag string
205 if dist.Tag == nil {
206 // If the dist struct does not specify a tag, use the default output files tag.
207 tag = ""
208 } else {
209 tag = *dist.Tag
210 }
211
212 // Get the paths of the output files to be dist'd, represented by the tag.
213 // Can be an empty list.
214 tagPaths := availableTaggedDists[tag]
215 if len(tagPaths) == 0 {
216 // Nothing to dist for this tag, continue to the next dist.
217 continue
218 }
219
220 if len(tagPaths) > 1 && (dist.Dest != nil || dist.Suffix != nil) {
221 errorMessage := "Cannot apply dest/suffix for more than one dist " +
222 "file for %s goals in module %s. The list of dist files, " +
223 "which should have a single element, is:\n%s"
224 panic(fmt.Errorf(errorMessage, goals, name, tagPaths))
225 }
226
227 ret = append(ret, fmt.Sprintf(".PHONY: %s\n", goals))
228
229 // Create dist-for-goals calls for each path in the dist'd files.
230 for _, path := range tagPaths {
231 // It's possible that the Path is nil from errant modules. Be defensive here.
232 if path == nil {
233 tagName := "default" // for error message readability
234 if dist.Tag != nil {
235 tagName = *dist.Tag
236 }
237 panic(fmt.Errorf("Dist file should not be nil for the %s tag in %s", tagName, name))
238 }
239
240 dest := filepath.Base(path.String())
241
242 if dist.Dest != nil {
243 var err error
244 if dest, err = validateSafePath(*dist.Dest); err != nil {
245 // This was checked in ModuleBase.GenerateBuildActions
246 panic(err)
247 }
248 }
249
250 if dist.Suffix != nil {
251 ext := filepath.Ext(dest)
252 suffix := *dist.Suffix
253 dest = strings.TrimSuffix(dest, ext) + suffix + ext
254 }
255
256 if dist.Dir != nil {
257 var err error
258 if dest, err = validateSafePath(*dist.Dir, dest); err != nil {
259 // This was checked in ModuleBase.GenerateBuildActions
260 panic(err)
261 }
262 }
263
264 ret = append(
265 ret,
266 fmt.Sprintf("$(call dist-for-goals,%s,%s:%s)\n", goals, path.String(), dest))
267 }
268 }
269
270 return ret
271}
272
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700273func (a *AndroidMkEntries) fillInEntries(config Config, bpPath string, mod blueprint.Module) {
274 a.EntryMap = make(map[string][]string)
275 amod := mod.(Module).base()
276 name := amod.BaseModuleName()
Colin Cross0477b422020-10-13 18:43:54 -0700277 if a.OverrideName != "" {
278 name = a.OverrideName
279 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700280
281 if a.Include == "" {
282 a.Include = "$(BUILD_PREBUILT)"
283 }
284 a.Required = append(a.Required, amod.commonProperties.Required...)
285 a.Host_required = append(a.Host_required, amod.commonProperties.Host_required...)
286 a.Target_required = append(a.Target_required, amod.commonProperties.Target_required...)
287
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000288 for _, distString := range a.GetDistForGoals(mod) {
289 fmt.Fprintf(&a.header, distString)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700290 }
291
292 fmt.Fprintln(&a.header, "\ninclude $(CLEAR_VARS)")
293
294 // Collect make variable assignment entries.
295 a.SetString("LOCAL_PATH", filepath.Dir(bpPath))
296 a.SetString("LOCAL_MODULE", name+a.SubName)
297 a.SetString("LOCAL_MODULE_CLASS", a.Class)
298 a.SetString("LOCAL_PREBUILT_MODULE_FILE", a.OutputFile.String())
299 a.AddStrings("LOCAL_REQUIRED_MODULES", a.Required...)
300 a.AddStrings("LOCAL_HOST_REQUIRED_MODULES", a.Host_required...)
301 a.AddStrings("LOCAL_TARGET_REQUIRED_MODULES", a.Target_required...)
302
Jiyong Park89e850a2020-04-07 16:37:39 +0900303 if am, ok := mod.(ApexModule); ok {
304 a.SetBoolIfTrue("LOCAL_NOT_AVAILABLE_FOR_PLATFORM", am.NotAvailableForPlatform())
305 }
306
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700307 archStr := amod.Arch().ArchType.String()
308 host := false
309 switch amod.Os().Class {
310 case Host:
Jiyong Park1613e552020-09-14 19:43:17 +0900311 if amod.Target().HostCross {
312 // Make cannot identify LOCAL_MODULE_HOST_CROSS_ARCH:= common.
313 if amod.Arch().ArchType != Common {
314 a.SetString("LOCAL_MODULE_HOST_CROSS_ARCH", archStr)
315 }
316 } else {
317 // Make cannot identify LOCAL_MODULE_HOST_ARCH:= common.
318 if amod.Arch().ArchType != Common {
319 a.SetString("LOCAL_MODULE_HOST_ARCH", archStr)
320 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700321 }
322 host = true
323 case Device:
324 // Make cannot identify LOCAL_MODULE_TARGET_ARCH:= common.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700325 if amod.Arch().ArchType != Common {
dimitry1f33e402019-03-26 12:39:31 +0100326 if amod.Target().NativeBridge {
dimitry8d6dde82019-07-11 10:23:53 +0200327 hostArchStr := amod.Target().NativeBridgeHostArchName
dimitry1f33e402019-03-26 12:39:31 +0100328 if hostArchStr != "" {
329 a.SetString("LOCAL_MODULE_TARGET_ARCH", hostArchStr)
330 }
331 } else {
332 a.SetString("LOCAL_MODULE_TARGET_ARCH", archStr)
333 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700334 }
335
336 a.AddStrings("LOCAL_INIT_RC", amod.commonProperties.Init_rc...)
337 a.AddStrings("LOCAL_VINTF_FRAGMENTS", amod.commonProperties.Vintf_fragments...)
338 a.SetBoolIfTrue("LOCAL_PROPRIETARY_MODULE", Bool(amod.commonProperties.Proprietary))
339 if Bool(amod.commonProperties.Vendor) || Bool(amod.commonProperties.Soc_specific) {
340 a.SetString("LOCAL_VENDOR_MODULE", "true")
341 }
342 a.SetBoolIfTrue("LOCAL_ODM_MODULE", Bool(amod.commonProperties.Device_specific))
343 a.SetBoolIfTrue("LOCAL_PRODUCT_MODULE", Bool(amod.commonProperties.Product_specific))
Justin Yund5f6c822019-06-25 16:47:17 +0900344 a.SetBoolIfTrue("LOCAL_SYSTEM_EXT_MODULE", Bool(amod.commonProperties.System_ext_specific))
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700345 if amod.commonProperties.Owner != nil {
346 a.SetString("LOCAL_MODULE_OWNER", *amod.commonProperties.Owner)
347 }
348 }
349
Bob Badoura75b0572020-02-18 20:21:55 -0800350 if len(amod.noticeFiles) > 0 {
351 a.SetString("LOCAL_NOTICE_FILE", strings.Join(amod.noticeFiles.Strings(), " "))
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700352 }
353
354 if host {
355 makeOs := amod.Os().String()
356 if amod.Os() == Linux || amod.Os() == LinuxBionic {
357 makeOs = "linux"
358 }
359 a.SetString("LOCAL_MODULE_HOST_OS", makeOs)
360 a.SetString("LOCAL_IS_HOST_MODULE", "true")
361 }
362
363 prefix := ""
364 if amod.ArchSpecific() {
365 switch amod.Os().Class {
366 case Host:
Jiyong Park1613e552020-09-14 19:43:17 +0900367 if amod.Target().HostCross {
368 prefix = "HOST_CROSS_"
369 } else {
370 prefix = "HOST_"
371 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700372 case Device:
373 prefix = "TARGET_"
374
375 }
376
377 if amod.Arch().ArchType != config.Targets[amod.Os()][0].Arch.ArchType {
378 prefix = "2ND_" + prefix
379 }
380 }
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700381 for _, extra := range a.ExtraEntries {
382 extra(a)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700383 }
384
385 // Write to footer.
386 fmt.Fprintln(&a.footer, "include "+a.Include)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700387 blueprintDir := filepath.Dir(bpPath)
388 for _, footerFunc := range a.ExtraFooters {
389 footerFunc(&a.footer, name, prefix, blueprintDir, a)
390 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700391}
392
393func (a *AndroidMkEntries) write(w io.Writer) {
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700394 if a.Disabled {
395 return
396 }
397
398 if !a.OutputFile.Valid() {
399 return
400 }
401
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700402 w.Write(a.header.Bytes())
403 for _, name := range a.entryOrder {
404 fmt.Fprintln(w, name+" := "+strings.Join(a.EntryMap[name], " "))
405 }
406 w.Write(a.footer.Bytes())
407}
408
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700409func (a *AndroidMkEntries) FooterLinesForTests() []string {
410 return strings.Split(string(a.footer.Bytes()), "\n")
411}
412
Colin Cross0875c522017-11-28 17:34:01 -0800413func AndroidMkSingleton() Singleton {
Dan Willemsen218f6562015-07-08 18:13:11 -0700414 return &androidMkSingleton{}
415}
416
417type androidMkSingleton struct{}
418
Colin Cross0875c522017-11-28 17:34:01 -0800419func (c *androidMkSingleton) GenerateBuildActions(ctx SingletonContext) {
Colin Crossaabf6792017-11-29 00:27:14 -0800420 if !ctx.Config().EmbeddedInMake() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800421 return
422 }
423
Colin Cross2465c3d2018-09-28 10:19:18 -0700424 var androidMkModulesList []blueprint.Module
Colin Cross4f6e4e62016-01-11 12:55:55 -0800425
Colin Cross2465c3d2018-09-28 10:19:18 -0700426 ctx.VisitAllModulesBlueprint(func(module blueprint.Module) {
Colin Cross0875c522017-11-28 17:34:01 -0800427 androidMkModulesList = append(androidMkModulesList, module)
Colin Cross4f6e4e62016-01-11 12:55:55 -0800428 })
Dan Willemsen218f6562015-07-08 18:13:11 -0700429
Colin Cross1ad81422019-01-14 12:47:35 -0800430 sort.SliceStable(androidMkModulesList, func(i, j int) bool {
431 return ctx.ModuleName(androidMkModulesList[i]) < ctx.ModuleName(androidMkModulesList[j])
432 })
Colin Crossd779da42015-12-17 18:00:23 -0800433
Dan Willemsen45133ac2018-03-09 21:22:06 -0800434 transMk := PathForOutput(ctx, "Android"+String(ctx.Config().productVariables.Make_suffix)+".mk")
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700435 if ctx.Failed() {
436 return
437 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700438
Colin Cross988414c2020-01-11 01:11:46 +0000439 err := translateAndroidMk(ctx, absolutePath(transMk.String()), androidMkModulesList)
Dan Willemsen218f6562015-07-08 18:13:11 -0700440 if err != nil {
441 ctx.Errorf(err.Error())
442 }
443
Colin Cross0875c522017-11-28 17:34:01 -0800444 ctx.Build(pctx, BuildParams{
445 Rule: blueprint.Phony,
446 Output: transMk,
Dan Willemsen218f6562015-07-08 18:13:11 -0700447 })
448}
449
Colin Cross2465c3d2018-09-28 10:19:18 -0700450func translateAndroidMk(ctx SingletonContext, mkFile string, mods []blueprint.Module) error {
Dan Willemsen218f6562015-07-08 18:13:11 -0700451 buf := &bytes.Buffer{}
452
Dan Willemsen97750522016-02-09 17:43:51 -0800453 fmt.Fprintln(buf, "LOCAL_MODULE_MAKEFILE := $(lastword $(MAKEFILE_LIST))")
Dan Willemsen218f6562015-07-08 18:13:11 -0700454
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700455 type_stats := make(map[string]int)
Dan Willemsen218f6562015-07-08 18:13:11 -0700456 for _, mod := range mods {
457 err := translateAndroidMkModule(ctx, buf, mod)
458 if err != nil {
459 os.Remove(mkFile)
460 return err
461 }
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700462
Colin Cross2465c3d2018-09-28 10:19:18 -0700463 if amod, ok := mod.(Module); ok && ctx.PrimaryModule(amod) == amod {
464 type_stats[ctx.ModuleType(amod)] += 1
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700465 }
466 }
467
468 keys := []string{}
469 fmt.Fprintln(buf, "\nSTATS.SOONG_MODULE_TYPE :=")
470 for k := range type_stats {
471 keys = append(keys, k)
472 }
473 sort.Strings(keys)
474 for _, mod_type := range keys {
475 fmt.Fprintln(buf, "STATS.SOONG_MODULE_TYPE +=", mod_type)
476 fmt.Fprintf(buf, "STATS.SOONG_MODULE_TYPE.%s := %d\n", mod_type, type_stats[mod_type])
Dan Willemsen218f6562015-07-08 18:13:11 -0700477 }
478
479 // Don't write to the file if it hasn't changed
Colin Cross988414c2020-01-11 01:11:46 +0000480 if _, err := os.Stat(absolutePath(mkFile)); !os.IsNotExist(err) {
481 if data, err := ioutil.ReadFile(absolutePath(mkFile)); err == nil {
Dan Willemsen218f6562015-07-08 18:13:11 -0700482 matches := buf.Len() == len(data)
483
484 if matches {
485 for i, value := range buf.Bytes() {
486 if value != data[i] {
487 matches = false
488 break
489 }
490 }
491 }
492
493 if matches {
494 return nil
495 }
496 }
497 }
498
Colin Cross988414c2020-01-11 01:11:46 +0000499 return ioutil.WriteFile(absolutePath(mkFile), buf.Bytes(), 0666)
Dan Willemsen218f6562015-07-08 18:13:11 -0700500}
501
Colin Cross0875c522017-11-28 17:34:01 -0800502func translateAndroidMkModule(ctx SingletonContext, w io.Writer, mod blueprint.Module) error {
Colin Cross953d3a22018-09-05 16:23:54 -0700503 defer func() {
504 if r := recover(); r != nil {
505 panic(fmt.Errorf("%s in translateAndroidMkModule for module %s variant %s",
506 r, ctx.ModuleName(mod), ctx.ModuleSubDir(mod)))
507 }
508 }()
509
Colin Cross2465c3d2018-09-28 10:19:18 -0700510 switch x := mod.(type) {
511 case AndroidMkDataProvider:
512 return translateAndroidModule(ctx, w, mod, x)
513 case bootstrap.GoBinaryTool:
514 return translateGoBinaryModule(ctx, w, mod, x)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700515 case AndroidMkEntriesProvider:
516 return translateAndroidMkEntriesModule(ctx, w, mod, x)
Colin Cross2465c3d2018-09-28 10:19:18 -0700517 default:
Dan Willemsen218f6562015-07-08 18:13:11 -0700518 return nil
519 }
Colin Cross2465c3d2018-09-28 10:19:18 -0700520}
521
522func translateGoBinaryModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
523 goBinary bootstrap.GoBinaryTool) error {
524
525 name := ctx.ModuleName(mod)
526 fmt.Fprintln(w, ".PHONY:", name)
527 fmt.Fprintln(w, name+":", goBinary.InstallPath())
528 fmt.Fprintln(w, "")
529
530 return nil
531}
532
Jooyung Han12df5fb2019-07-11 16:18:47 +0900533func (data *AndroidMkData) fillInData(config Config, bpPath string, mod blueprint.Module) {
534 // Get the preamble content through AndroidMkEntries logic.
Jooyung Han2ed99d02020-06-24 23:26:26 +0900535 data.Entries = AndroidMkEntries{
Jooyung Han12df5fb2019-07-11 16:18:47 +0900536 Class: data.Class,
537 SubName: data.SubName,
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000538 DistFiles: data.DistFiles,
Jooyung Han12df5fb2019-07-11 16:18:47 +0900539 OutputFile: data.OutputFile,
540 Disabled: data.Disabled,
541 Include: data.Include,
542 Required: data.Required,
543 Host_required: data.Host_required,
544 Target_required: data.Target_required,
545 }
Jooyung Han2ed99d02020-06-24 23:26:26 +0900546 data.Entries.fillInEntries(config, bpPath, mod)
Jooyung Han12df5fb2019-07-11 16:18:47 +0900547
548 // copy entries back to data since it is used in Custom
Jooyung Han2ed99d02020-06-24 23:26:26 +0900549 data.Required = data.Entries.Required
550 data.Host_required = data.Entries.Host_required
551 data.Target_required = data.Entries.Target_required
Jooyung Han12df5fb2019-07-11 16:18:47 +0900552}
553
Colin Cross2465c3d2018-09-28 10:19:18 -0700554func translateAndroidModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
555 provider AndroidMkDataProvider) error {
Dan Willemsen218f6562015-07-08 18:13:11 -0700556
Colin Cross635c3b02016-05-18 15:37:25 -0700557 amod := mod.(Module).base()
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700558 if shouldSkipAndroidMkProcessing(amod) {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800559 return nil
560 }
561
Colin Cross91825d22017-08-10 16:59:47 -0700562 data := provider.AndroidMk()
Colin Cross53499412017-09-07 13:20:25 -0700563 if data.Include == "" {
564 data.Include = "$(BUILD_PREBUILT)"
565 }
566
Jooyung Han12df5fb2019-07-11 16:18:47 +0900567 data.fillInData(ctx.Config(), ctx.BlueprintFile(mod), mod)
Dan Willemsen01a405a2016-06-13 17:19:03 -0700568
Colin Cross0f86d182017-08-10 17:07:28 -0700569 prefix := ""
570 if amod.ArchSpecific() {
571 switch amod.Os().Class {
572 case Host:
Jiyong Park1613e552020-09-14 19:43:17 +0900573 if amod.Target().HostCross {
574 prefix = "HOST_CROSS_"
575 } else {
576 prefix = "HOST_"
577 }
Colin Cross0f86d182017-08-10 17:07:28 -0700578 case Device:
579 prefix = "TARGET_"
Colin Crossa2344662016-03-24 13:14:12 -0700580
Dan Willemsen218f6562015-07-08 18:13:11 -0700581 }
582
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700583 if amod.Arch().ArchType != ctx.Config().Targets[amod.Os()][0].Arch.ArchType {
Colin Cross0f86d182017-08-10 17:07:28 -0700584 prefix = "2ND_" + prefix
585 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700586 }
587
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700588 name := provider.BaseModuleName()
Colin Cross0f86d182017-08-10 17:07:28 -0700589 blueprintDir := filepath.Dir(ctx.BlueprintFile(mod))
590
591 if data.Custom != nil {
592 data.Custom(w, name, prefix, blueprintDir, data)
593 } else {
594 WriteAndroidMkData(w, data)
595 }
596
597 return nil
598}
599
600func WriteAndroidMkData(w io.Writer, data AndroidMkData) {
601 if data.Disabled {
602 return
603 }
604
605 if !data.OutputFile.Valid() {
606 return
607 }
608
Jooyung Han2ed99d02020-06-24 23:26:26 +0900609 // write preamble via Entries
610 data.Entries.footer = bytes.Buffer{}
611 data.Entries.write(w)
Colin Cross0f86d182017-08-10 17:07:28 -0700612
Colin Crossca860ac2016-01-04 14:34:37 -0800613 for _, extra := range data.Extra {
Colin Cross27a4b052017-08-10 16:32:23 -0700614 extra(w, data.OutputFile.Path())
Dan Willemsen97750522016-02-09 17:43:51 -0800615 }
616
Colin Cross53499412017-09-07 13:20:25 -0700617 fmt.Fprintln(w, "include "+data.Include)
Dan Willemsen218f6562015-07-08 18:13:11 -0700618}
Sasha Smundakb6d23052019-04-01 18:37:36 -0700619
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700620func translateAndroidMkEntriesModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
621 provider AndroidMkEntriesProvider) error {
622 if shouldSkipAndroidMkProcessing(mod.(Module).base()) {
623 return nil
Sasha Smundakb6d23052019-04-01 18:37:36 -0700624 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700625
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900626 for _, entries := range provider.AndroidMkEntries() {
627 entries.fillInEntries(ctx.Config(), ctx.BlueprintFile(mod), mod)
628 entries.write(w)
629 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700630
631 return nil
632}
633
634func shouldSkipAndroidMkProcessing(module *ModuleBase) bool {
635 if !module.commonProperties.NamespaceExportedToMake {
636 // TODO(jeffrygaston) do we want to validate that there are no modules being
637 // exported to Kati that depend on this module?
638 return true
Sasha Smundakb6d23052019-04-01 18:37:36 -0700639 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700640
641 return !module.Enabled() ||
642 module.commonProperties.SkipInstall ||
643 // Make does not understand LinuxBionic
644 module.Os() == LinuxBionic
Sasha Smundakb6d23052019-04-01 18:37:36 -0700645}
Dan Shi31949122020-09-21 12:11:02 -0700646
647func AndroidMkDataPaths(data []DataPath) []string {
648 var testFiles []string
649 for _, d := range data {
650 rel := d.SrcPath.Rel()
651 path := d.SrcPath.String()
652 if !strings.HasSuffix(path, rel) {
653 panic(fmt.Errorf("path %q does not end with %q", path, rel))
654 }
655 path = strings.TrimSuffix(path, rel)
656 testFileString := path + ":" + rel
657 if len(d.RelativeInstallPath) > 0 {
658 testFileString += ":" + d.RelativeInstallPath
659 }
660 testFiles = append(testFiles, testFileString)
661 }
662 return testFiles
663}