blob: c24ac5786632cd37613c94f1f63a953386f0f24e [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() {
Colin Cross798bfce2016-10-12 14:28:16 -070032 RegisterSingletonType("androidmk", AndroidMkSingleton)
Dan Willemsen218f6562015-07-08 18:13:11 -070033}
34
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -070035// Deprecated: consider using AndroidMkEntriesProvider instead, especially if you're not going to
36// use the Custom function.
Dan Willemsen218f6562015-07-08 18:13:11 -070037type AndroidMkDataProvider interface {
Colin Crossa18e9cf2017-08-10 17:00:19 -070038 AndroidMk() AndroidMkData
Colin Crossce75d2c2016-10-06 16:12:58 -070039 BaseModuleName() string
Dan Willemsen218f6562015-07-08 18:13:11 -070040}
41
42type AndroidMkData struct {
Sasha Smundakb6d23052019-04-01 18:37:36 -070043 Class string
44 SubName string
45 DistFile OptionalPath
46 OutputFile OptionalPath
47 Disabled bool
48 Include string
49 Required []string
50 Host_required []string
51 Target_required []string
Dan Willemsen218f6562015-07-08 18:13:11 -070052
Colin Cross0f86d182017-08-10 17:07:28 -070053 Custom func(w io.Writer, name, prefix, moduleDir string, data AndroidMkData)
Dan Willemsen218f6562015-07-08 18:13:11 -070054
Colin Cross27a4b052017-08-10 16:32:23 -070055 Extra []AndroidMkExtraFunc
Colin Cross0f86d182017-08-10 17:07:28 -070056
57 preamble bytes.Buffer
Dan Willemsen218f6562015-07-08 18:13:11 -070058}
59
Colin Cross27a4b052017-08-10 16:32:23 -070060type AndroidMkExtraFunc func(w io.Writer, outputFile Path)
61
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -070062// Allows modules to customize their Android*.mk output.
63type AndroidMkEntriesProvider interface {
64 AndroidMkEntries() AndroidMkEntries
65 BaseModuleName() string
66}
67
68type AndroidMkEntries struct {
69 Class string
70 SubName string
71 DistFile OptionalPath
72 OutputFile OptionalPath
73 Disabled bool
74 Include string
75 Required []string
76 Host_required []string
77 Target_required []string
78
79 header bytes.Buffer
80 footer bytes.Buffer
81
82 AddCustomEntries func(name, prefix, moduleDir string, entries *AndroidMkEntries)
83
84 EntryMap map[string][]string
85 entryOrder []string
86}
87
88func (a *AndroidMkEntries) SetString(name, value string) {
89 if _, ok := a.EntryMap[name]; !ok {
90 a.entryOrder = append(a.entryOrder, name)
91 }
92 a.EntryMap[name] = []string{value}
93}
94
95func (a *AndroidMkEntries) SetBoolIfTrue(name string, flag bool) {
96 if flag {
97 if _, ok := a.EntryMap[name]; !ok {
98 a.entryOrder = append(a.entryOrder, name)
99 }
100 a.EntryMap[name] = []string{"true"}
101 }
102}
103
104func (a *AndroidMkEntries) AddStrings(name string, value ...string) {
105 if len(value) == 0 {
106 return
107 }
108 if _, ok := a.EntryMap[name]; !ok {
109 a.entryOrder = append(a.entryOrder, name)
110 }
111 a.EntryMap[name] = append(a.EntryMap[name], value...)
112}
113
114func (a *AndroidMkEntries) fillInEntries(config Config, bpPath string, mod blueprint.Module) {
115 a.EntryMap = make(map[string][]string)
116 amod := mod.(Module).base()
117 name := amod.BaseModuleName()
118
119 if a.Include == "" {
120 a.Include = "$(BUILD_PREBUILT)"
121 }
122 a.Required = append(a.Required, amod.commonProperties.Required...)
123 a.Host_required = append(a.Host_required, amod.commonProperties.Host_required...)
124 a.Target_required = append(a.Target_required, amod.commonProperties.Target_required...)
125
126 // Fill in the header part.
127 if len(amod.commonProperties.Dist.Targets) > 0 {
128 distFile := a.DistFile
129 if !distFile.Valid() {
130 distFile = a.OutputFile
131 }
132 if distFile.Valid() {
133 dest := filepath.Base(distFile.String())
134
135 if amod.commonProperties.Dist.Dest != nil {
136 var err error
137 if dest, err = validateSafePath(*amod.commonProperties.Dist.Dest); err != nil {
138 // This was checked in ModuleBase.GenerateBuildActions
139 panic(err)
140 }
141 }
142
143 if amod.commonProperties.Dist.Suffix != nil {
144 ext := filepath.Ext(dest)
145 suffix := *amod.commonProperties.Dist.Suffix
146 dest = strings.TrimSuffix(dest, ext) + suffix + ext
147 }
148
149 if amod.commonProperties.Dist.Dir != nil {
150 var err error
151 if dest, err = validateSafePath(*amod.commonProperties.Dist.Dir, dest); err != nil {
152 // This was checked in ModuleBase.GenerateBuildActions
153 panic(err)
154 }
155 }
156
157 goals := strings.Join(amod.commonProperties.Dist.Targets, " ")
158 fmt.Fprintln(&a.header, ".PHONY:", goals)
159 fmt.Fprintf(&a.header, "$(call dist-for-goals,%s,%s:%s)\n",
160 goals, distFile.String(), dest)
161 }
162 }
163
164 fmt.Fprintln(&a.header, "\ninclude $(CLEAR_VARS)")
165
166 // Collect make variable assignment entries.
167 a.SetString("LOCAL_PATH", filepath.Dir(bpPath))
168 a.SetString("LOCAL_MODULE", name+a.SubName)
169 a.SetString("LOCAL_MODULE_CLASS", a.Class)
170 a.SetString("LOCAL_PREBUILT_MODULE_FILE", a.OutputFile.String())
171 a.AddStrings("LOCAL_REQUIRED_MODULES", a.Required...)
172 a.AddStrings("LOCAL_HOST_REQUIRED_MODULES", a.Host_required...)
173 a.AddStrings("LOCAL_TARGET_REQUIRED_MODULES", a.Target_required...)
174
175 archStr := amod.Arch().ArchType.String()
176 host := false
177 switch amod.Os().Class {
178 case Host:
179 // Make cannot identify LOCAL_MODULE_HOST_ARCH:= common.
180 if archStr != "common" {
181 a.SetString("LOCAL_MODULE_HOST_ARCH", archStr)
182 }
183 host = true
184 case HostCross:
185 // Make cannot identify LOCAL_MODULE_HOST_CROSS_ARCH:= common.
186 if archStr != "common" {
187 a.SetString("LOCAL_MODULE_HOST_CROSS_ARCH", archStr)
188 }
189 host = true
190 case Device:
191 // Make cannot identify LOCAL_MODULE_TARGET_ARCH:= common.
192 if archStr != "common" {
dimitry1f33e402019-03-26 12:39:31 +0100193 if amod.Target().NativeBridge {
194 // TODO: Unhardcode these rules.
195 guestArchStr := archStr
196 hostArchStr := ""
197 if guestArchStr == "arm" {
198 hostArchStr = "x86"
199 } else if guestArchStr == "arm64" {
200 hostArchStr = "x86_64"
201 }
202
203 if hostArchStr != "" {
204 a.SetString("LOCAL_MODULE_TARGET_ARCH", hostArchStr)
205 }
206 } else {
207 a.SetString("LOCAL_MODULE_TARGET_ARCH", archStr)
208 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700209 }
210
211 a.AddStrings("LOCAL_INIT_RC", amod.commonProperties.Init_rc...)
212 a.AddStrings("LOCAL_VINTF_FRAGMENTS", amod.commonProperties.Vintf_fragments...)
213 a.SetBoolIfTrue("LOCAL_PROPRIETARY_MODULE", Bool(amod.commonProperties.Proprietary))
214 if Bool(amod.commonProperties.Vendor) || Bool(amod.commonProperties.Soc_specific) {
215 a.SetString("LOCAL_VENDOR_MODULE", "true")
216 }
217 a.SetBoolIfTrue("LOCAL_ODM_MODULE", Bool(amod.commonProperties.Device_specific))
218 a.SetBoolIfTrue("LOCAL_PRODUCT_MODULE", Bool(amod.commonProperties.Product_specific))
Justin Yund5f6c822019-06-25 16:47:17 +0900219 // TODO(b/135957588) product_services_specific is matched to LOCAL_PRODUCT_MODULE
220 // as a workaround. Remove this after clearing all Android.bp
221 a.SetBoolIfTrue("LOCAL_PRODUCT_MODULE", Bool(amod.commonProperties.Product_services_specific))
222 a.SetBoolIfTrue("LOCAL_SYSTEM_EXT_MODULE", Bool(amod.commonProperties.System_ext_specific))
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700223 if amod.commonProperties.Owner != nil {
224 a.SetString("LOCAL_MODULE_OWNER", *amod.commonProperties.Owner)
225 }
226 }
227
228 if amod.noticeFile.Valid() {
229 a.SetString("LOCAL_NOTICE_FILE", amod.noticeFile.String())
230 }
231
232 if host {
233 makeOs := amod.Os().String()
234 if amod.Os() == Linux || amod.Os() == LinuxBionic {
235 makeOs = "linux"
236 }
237 a.SetString("LOCAL_MODULE_HOST_OS", makeOs)
238 a.SetString("LOCAL_IS_HOST_MODULE", "true")
239 }
240
241 prefix := ""
242 if amod.ArchSpecific() {
243 switch amod.Os().Class {
244 case Host:
245 prefix = "HOST_"
246 case HostCross:
247 prefix = "HOST_CROSS_"
248 case Device:
249 prefix = "TARGET_"
250
251 }
252
253 if amod.Arch().ArchType != config.Targets[amod.Os()][0].Arch.ArchType {
254 prefix = "2ND_" + prefix
255 }
256 }
257 blueprintDir := filepath.Dir(bpPath)
258 if a.AddCustomEntries != nil {
259 a.AddCustomEntries(name, prefix, blueprintDir, a)
260 }
261
262 // Write to footer.
263 fmt.Fprintln(&a.footer, "include "+a.Include)
264}
265
266func (a *AndroidMkEntries) write(w io.Writer) {
267 w.Write(a.header.Bytes())
268 for _, name := range a.entryOrder {
269 fmt.Fprintln(w, name+" := "+strings.Join(a.EntryMap[name], " "))
270 }
271 w.Write(a.footer.Bytes())
272}
273
Colin Cross0875c522017-11-28 17:34:01 -0800274func AndroidMkSingleton() Singleton {
Dan Willemsen218f6562015-07-08 18:13:11 -0700275 return &androidMkSingleton{}
276}
277
278type androidMkSingleton struct{}
279
Colin Cross0875c522017-11-28 17:34:01 -0800280func (c *androidMkSingleton) GenerateBuildActions(ctx SingletonContext) {
Colin Crossaabf6792017-11-29 00:27:14 -0800281 if !ctx.Config().EmbeddedInMake() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800282 return
283 }
284
Colin Cross2465c3d2018-09-28 10:19:18 -0700285 var androidMkModulesList []blueprint.Module
Colin Cross4f6e4e62016-01-11 12:55:55 -0800286
Colin Cross2465c3d2018-09-28 10:19:18 -0700287 ctx.VisitAllModulesBlueprint(func(module blueprint.Module) {
Colin Cross0875c522017-11-28 17:34:01 -0800288 androidMkModulesList = append(androidMkModulesList, module)
Colin Cross4f6e4e62016-01-11 12:55:55 -0800289 })
Dan Willemsen218f6562015-07-08 18:13:11 -0700290
Colin Cross1ad81422019-01-14 12:47:35 -0800291 sort.SliceStable(androidMkModulesList, func(i, j int) bool {
292 return ctx.ModuleName(androidMkModulesList[i]) < ctx.ModuleName(androidMkModulesList[j])
293 })
Colin Crossd779da42015-12-17 18:00:23 -0800294
Dan Willemsen45133ac2018-03-09 21:22:06 -0800295 transMk := PathForOutput(ctx, "Android"+String(ctx.Config().productVariables.Make_suffix)+".mk")
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700296 if ctx.Failed() {
297 return
298 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700299
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700300 err := translateAndroidMk(ctx, transMk.String(), androidMkModulesList)
Dan Willemsen218f6562015-07-08 18:13:11 -0700301 if err != nil {
302 ctx.Errorf(err.Error())
303 }
304
Colin Cross0875c522017-11-28 17:34:01 -0800305 ctx.Build(pctx, BuildParams{
306 Rule: blueprint.Phony,
307 Output: transMk,
Dan Willemsen218f6562015-07-08 18:13:11 -0700308 })
309}
310
Colin Cross2465c3d2018-09-28 10:19:18 -0700311func translateAndroidMk(ctx SingletonContext, mkFile string, mods []blueprint.Module) error {
Dan Willemsen218f6562015-07-08 18:13:11 -0700312 buf := &bytes.Buffer{}
313
Dan Willemsen97750522016-02-09 17:43:51 -0800314 fmt.Fprintln(buf, "LOCAL_MODULE_MAKEFILE := $(lastword $(MAKEFILE_LIST))")
Dan Willemsen218f6562015-07-08 18:13:11 -0700315
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700316 type_stats := make(map[string]int)
Dan Willemsen218f6562015-07-08 18:13:11 -0700317 for _, mod := range mods {
318 err := translateAndroidMkModule(ctx, buf, mod)
319 if err != nil {
320 os.Remove(mkFile)
321 return err
322 }
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700323
Colin Cross2465c3d2018-09-28 10:19:18 -0700324 if amod, ok := mod.(Module); ok && ctx.PrimaryModule(amod) == amod {
325 type_stats[ctx.ModuleType(amod)] += 1
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700326 }
327 }
328
329 keys := []string{}
330 fmt.Fprintln(buf, "\nSTATS.SOONG_MODULE_TYPE :=")
331 for k := range type_stats {
332 keys = append(keys, k)
333 }
334 sort.Strings(keys)
335 for _, mod_type := range keys {
336 fmt.Fprintln(buf, "STATS.SOONG_MODULE_TYPE +=", mod_type)
337 fmt.Fprintf(buf, "STATS.SOONG_MODULE_TYPE.%s := %d\n", mod_type, type_stats[mod_type])
Dan Willemsen218f6562015-07-08 18:13:11 -0700338 }
339
340 // Don't write to the file if it hasn't changed
341 if _, err := os.Stat(mkFile); !os.IsNotExist(err) {
342 if data, err := ioutil.ReadFile(mkFile); err == nil {
343 matches := buf.Len() == len(data)
344
345 if matches {
346 for i, value := range buf.Bytes() {
347 if value != data[i] {
348 matches = false
349 break
350 }
351 }
352 }
353
354 if matches {
355 return nil
356 }
357 }
358 }
359
360 return ioutil.WriteFile(mkFile, buf.Bytes(), 0666)
361}
362
Colin Cross0875c522017-11-28 17:34:01 -0800363func translateAndroidMkModule(ctx SingletonContext, w io.Writer, mod blueprint.Module) error {
Colin Cross953d3a22018-09-05 16:23:54 -0700364 defer func() {
365 if r := recover(); r != nil {
366 panic(fmt.Errorf("%s in translateAndroidMkModule for module %s variant %s",
367 r, ctx.ModuleName(mod), ctx.ModuleSubDir(mod)))
368 }
369 }()
370
Colin Cross2465c3d2018-09-28 10:19:18 -0700371 switch x := mod.(type) {
372 case AndroidMkDataProvider:
373 return translateAndroidModule(ctx, w, mod, x)
374 case bootstrap.GoBinaryTool:
375 return translateGoBinaryModule(ctx, w, mod, x)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700376 case AndroidMkEntriesProvider:
377 return translateAndroidMkEntriesModule(ctx, w, mod, x)
Colin Cross2465c3d2018-09-28 10:19:18 -0700378 default:
Dan Willemsen218f6562015-07-08 18:13:11 -0700379 return nil
380 }
Colin Cross2465c3d2018-09-28 10:19:18 -0700381}
382
383func translateGoBinaryModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
384 goBinary bootstrap.GoBinaryTool) error {
385
386 name := ctx.ModuleName(mod)
387 fmt.Fprintln(w, ".PHONY:", name)
388 fmt.Fprintln(w, name+":", goBinary.InstallPath())
389 fmt.Fprintln(w, "")
390
391 return nil
392}
393
Jooyung Han12df5fb2019-07-11 16:18:47 +0900394func (data *AndroidMkData) fillInData(config Config, bpPath string, mod blueprint.Module) {
395 // Get the preamble content through AndroidMkEntries logic.
396 entries := AndroidMkEntries{
397 Class: data.Class,
398 SubName: data.SubName,
399 DistFile: data.DistFile,
400 OutputFile: data.OutputFile,
401 Disabled: data.Disabled,
402 Include: data.Include,
403 Required: data.Required,
404 Host_required: data.Host_required,
405 Target_required: data.Target_required,
406 }
407 entries.fillInEntries(config, bpPath, mod)
408
409 // preamble doesn't need the footer content.
410 entries.footer = bytes.Buffer{}
411 entries.write(&data.preamble)
412
413 // copy entries back to data since it is used in Custom
414 data.Required = entries.Required
415 data.Host_required = entries.Host_required
416 data.Target_required = entries.Target_required
417}
418
Colin Cross2465c3d2018-09-28 10:19:18 -0700419func translateAndroidModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
420 provider AndroidMkDataProvider) error {
Dan Willemsen218f6562015-07-08 18:13:11 -0700421
Colin Cross635c3b02016-05-18 15:37:25 -0700422 amod := mod.(Module).base()
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700423 if shouldSkipAndroidMkProcessing(amod) {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800424 return nil
425 }
426
Colin Cross91825d22017-08-10 16:59:47 -0700427 data := provider.AndroidMk()
Colin Cross53499412017-09-07 13:20:25 -0700428 if data.Include == "" {
429 data.Include = "$(BUILD_PREBUILT)"
430 }
431
Jooyung Han12df5fb2019-07-11 16:18:47 +0900432 data.fillInData(ctx.Config(), ctx.BlueprintFile(mod), mod)
Dan Willemsen01a405a2016-06-13 17:19:03 -0700433
Colin Cross0f86d182017-08-10 17:07:28 -0700434 prefix := ""
435 if amod.ArchSpecific() {
436 switch amod.Os().Class {
437 case Host:
438 prefix = "HOST_"
439 case HostCross:
440 prefix = "HOST_CROSS_"
441 case Device:
442 prefix = "TARGET_"
Colin Crossa2344662016-03-24 13:14:12 -0700443
Dan Willemsen218f6562015-07-08 18:13:11 -0700444 }
445
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700446 if amod.Arch().ArchType != ctx.Config().Targets[amod.Os()][0].Arch.ArchType {
Colin Cross0f86d182017-08-10 17:07:28 -0700447 prefix = "2ND_" + prefix
448 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700449 }
450
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700451 name := provider.BaseModuleName()
Colin Cross0f86d182017-08-10 17:07:28 -0700452 blueprintDir := filepath.Dir(ctx.BlueprintFile(mod))
453
454 if data.Custom != nil {
455 data.Custom(w, name, prefix, blueprintDir, data)
456 } else {
457 WriteAndroidMkData(w, data)
458 }
459
460 return nil
461}
462
463func WriteAndroidMkData(w io.Writer, data AndroidMkData) {
464 if data.Disabled {
465 return
466 }
467
468 if !data.OutputFile.Valid() {
469 return
470 }
471
472 w.Write(data.preamble.Bytes())
473
Colin Crossca860ac2016-01-04 14:34:37 -0800474 for _, extra := range data.Extra {
Colin Cross27a4b052017-08-10 16:32:23 -0700475 extra(w, data.OutputFile.Path())
Dan Willemsen97750522016-02-09 17:43:51 -0800476 }
477
Colin Cross53499412017-09-07 13:20:25 -0700478 fmt.Fprintln(w, "include "+data.Include)
Dan Willemsen218f6562015-07-08 18:13:11 -0700479}
Sasha Smundakb6d23052019-04-01 18:37:36 -0700480
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700481func translateAndroidMkEntriesModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
482 provider AndroidMkEntriesProvider) error {
483 if shouldSkipAndroidMkProcessing(mod.(Module).base()) {
484 return nil
Sasha Smundakb6d23052019-04-01 18:37:36 -0700485 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700486
487 entries := provider.AndroidMkEntries()
488 entries.fillInEntries(ctx.Config(), ctx.BlueprintFile(mod), mod)
489
490 entries.write(w)
491
492 return nil
493}
494
495func shouldSkipAndroidMkProcessing(module *ModuleBase) bool {
496 if !module.commonProperties.NamespaceExportedToMake {
497 // TODO(jeffrygaston) do we want to validate that there are no modules being
498 // exported to Kati that depend on this module?
499 return true
Sasha Smundakb6d23052019-04-01 18:37:36 -0700500 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700501
502 return !module.Enabled() ||
503 module.commonProperties.SkipInstall ||
504 // Make does not understand LinuxBionic
505 module.Os() == LinuxBionic
Sasha Smundakb6d23052019-04-01 18:37:36 -0700506}