blob: 124523f4c6a657a084f3671326ae3c435f2840f2 [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
Jaewoong Junge0dc8df2019-08-27 17:33:16 -070082 ExtraEntries []AndroidMkExtraEntriesFunc
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -070083
84 EntryMap map[string][]string
85 entryOrder []string
86}
87
Jaewoong Junge0dc8df2019-08-27 17:33:16 -070088type AndroidMkExtraEntriesFunc func(entries *AndroidMkEntries)
89
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -070090func (a *AndroidMkEntries) SetString(name, value string) {
91 if _, ok := a.EntryMap[name]; !ok {
92 a.entryOrder = append(a.entryOrder, name)
93 }
94 a.EntryMap[name] = []string{value}
95}
96
97func (a *AndroidMkEntries) SetBoolIfTrue(name string, flag bool) {
98 if flag {
99 if _, ok := a.EntryMap[name]; !ok {
100 a.entryOrder = append(a.entryOrder, name)
101 }
102 a.EntryMap[name] = []string{"true"}
103 }
104}
105
106func (a *AndroidMkEntries) AddStrings(name string, value ...string) {
107 if len(value) == 0 {
108 return
109 }
110 if _, ok := a.EntryMap[name]; !ok {
111 a.entryOrder = append(a.entryOrder, name)
112 }
113 a.EntryMap[name] = append(a.EntryMap[name], value...)
114}
115
116func (a *AndroidMkEntries) fillInEntries(config Config, bpPath string, mod blueprint.Module) {
117 a.EntryMap = make(map[string][]string)
118 amod := mod.(Module).base()
119 name := amod.BaseModuleName()
120
121 if a.Include == "" {
122 a.Include = "$(BUILD_PREBUILT)"
123 }
124 a.Required = append(a.Required, amod.commonProperties.Required...)
125 a.Host_required = append(a.Host_required, amod.commonProperties.Host_required...)
126 a.Target_required = append(a.Target_required, amod.commonProperties.Target_required...)
127
128 // Fill in the header part.
129 if len(amod.commonProperties.Dist.Targets) > 0 {
130 distFile := a.DistFile
131 if !distFile.Valid() {
132 distFile = a.OutputFile
133 }
134 if distFile.Valid() {
135 dest := filepath.Base(distFile.String())
136
137 if amod.commonProperties.Dist.Dest != nil {
138 var err error
139 if dest, err = validateSafePath(*amod.commonProperties.Dist.Dest); err != nil {
140 // This was checked in ModuleBase.GenerateBuildActions
141 panic(err)
142 }
143 }
144
145 if amod.commonProperties.Dist.Suffix != nil {
146 ext := filepath.Ext(dest)
147 suffix := *amod.commonProperties.Dist.Suffix
148 dest = strings.TrimSuffix(dest, ext) + suffix + ext
149 }
150
151 if amod.commonProperties.Dist.Dir != nil {
152 var err error
153 if dest, err = validateSafePath(*amod.commonProperties.Dist.Dir, dest); err != nil {
154 // This was checked in ModuleBase.GenerateBuildActions
155 panic(err)
156 }
157 }
158
159 goals := strings.Join(amod.commonProperties.Dist.Targets, " ")
160 fmt.Fprintln(&a.header, ".PHONY:", goals)
161 fmt.Fprintf(&a.header, "$(call dist-for-goals,%s,%s:%s)\n",
162 goals, distFile.String(), dest)
163 }
164 }
165
166 fmt.Fprintln(&a.header, "\ninclude $(CLEAR_VARS)")
167
168 // Collect make variable assignment entries.
169 a.SetString("LOCAL_PATH", filepath.Dir(bpPath))
170 a.SetString("LOCAL_MODULE", name+a.SubName)
171 a.SetString("LOCAL_MODULE_CLASS", a.Class)
172 a.SetString("LOCAL_PREBUILT_MODULE_FILE", a.OutputFile.String())
173 a.AddStrings("LOCAL_REQUIRED_MODULES", a.Required...)
174 a.AddStrings("LOCAL_HOST_REQUIRED_MODULES", a.Host_required...)
175 a.AddStrings("LOCAL_TARGET_REQUIRED_MODULES", a.Target_required...)
176
177 archStr := amod.Arch().ArchType.String()
178 host := false
179 switch amod.Os().Class {
180 case Host:
181 // Make cannot identify LOCAL_MODULE_HOST_ARCH:= common.
182 if archStr != "common" {
183 a.SetString("LOCAL_MODULE_HOST_ARCH", archStr)
184 }
185 host = true
186 case HostCross:
187 // Make cannot identify LOCAL_MODULE_HOST_CROSS_ARCH:= common.
188 if archStr != "common" {
189 a.SetString("LOCAL_MODULE_HOST_CROSS_ARCH", archStr)
190 }
191 host = true
192 case Device:
193 // Make cannot identify LOCAL_MODULE_TARGET_ARCH:= common.
194 if archStr != "common" {
dimitry1f33e402019-03-26 12:39:31 +0100195 if amod.Target().NativeBridge {
dimitry8d6dde82019-07-11 10:23:53 +0200196 hostArchStr := amod.Target().NativeBridgeHostArchName
dimitry1f33e402019-03-26 12:39:31 +0100197 if hostArchStr != "" {
198 a.SetString("LOCAL_MODULE_TARGET_ARCH", hostArchStr)
199 }
200 } else {
201 a.SetString("LOCAL_MODULE_TARGET_ARCH", archStr)
202 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700203 }
204
205 a.AddStrings("LOCAL_INIT_RC", amod.commonProperties.Init_rc...)
206 a.AddStrings("LOCAL_VINTF_FRAGMENTS", amod.commonProperties.Vintf_fragments...)
207 a.SetBoolIfTrue("LOCAL_PROPRIETARY_MODULE", Bool(amod.commonProperties.Proprietary))
208 if Bool(amod.commonProperties.Vendor) || Bool(amod.commonProperties.Soc_specific) {
209 a.SetString("LOCAL_VENDOR_MODULE", "true")
210 }
211 a.SetBoolIfTrue("LOCAL_ODM_MODULE", Bool(amod.commonProperties.Device_specific))
212 a.SetBoolIfTrue("LOCAL_PRODUCT_MODULE", Bool(amod.commonProperties.Product_specific))
Justin Yund5f6c822019-06-25 16:47:17 +0900213 // TODO(b/135957588) product_services_specific is matched to LOCAL_PRODUCT_MODULE
214 // as a workaround. Remove this after clearing all Android.bp
215 a.SetBoolIfTrue("LOCAL_PRODUCT_MODULE", Bool(amod.commonProperties.Product_services_specific))
216 a.SetBoolIfTrue("LOCAL_SYSTEM_EXT_MODULE", Bool(amod.commonProperties.System_ext_specific))
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700217 if amod.commonProperties.Owner != nil {
218 a.SetString("LOCAL_MODULE_OWNER", *amod.commonProperties.Owner)
219 }
220 }
221
222 if amod.noticeFile.Valid() {
223 a.SetString("LOCAL_NOTICE_FILE", amod.noticeFile.String())
224 }
225
226 if host {
227 makeOs := amod.Os().String()
228 if amod.Os() == Linux || amod.Os() == LinuxBionic {
229 makeOs = "linux"
230 }
231 a.SetString("LOCAL_MODULE_HOST_OS", makeOs)
232 a.SetString("LOCAL_IS_HOST_MODULE", "true")
233 }
234
235 prefix := ""
236 if amod.ArchSpecific() {
237 switch amod.Os().Class {
238 case Host:
239 prefix = "HOST_"
240 case HostCross:
241 prefix = "HOST_CROSS_"
242 case Device:
243 prefix = "TARGET_"
244
245 }
246
247 if amod.Arch().ArchType != config.Targets[amod.Os()][0].Arch.ArchType {
248 prefix = "2ND_" + prefix
249 }
250 }
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700251 for _, extra := range a.ExtraEntries {
252 extra(a)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700253 }
254
255 // Write to footer.
256 fmt.Fprintln(&a.footer, "include "+a.Include)
257}
258
259func (a *AndroidMkEntries) write(w io.Writer) {
260 w.Write(a.header.Bytes())
261 for _, name := range a.entryOrder {
262 fmt.Fprintln(w, name+" := "+strings.Join(a.EntryMap[name], " "))
263 }
264 w.Write(a.footer.Bytes())
265}
266
Colin Cross0875c522017-11-28 17:34:01 -0800267func AndroidMkSingleton() Singleton {
Dan Willemsen218f6562015-07-08 18:13:11 -0700268 return &androidMkSingleton{}
269}
270
271type androidMkSingleton struct{}
272
Colin Cross0875c522017-11-28 17:34:01 -0800273func (c *androidMkSingleton) GenerateBuildActions(ctx SingletonContext) {
Colin Crossaabf6792017-11-29 00:27:14 -0800274 if !ctx.Config().EmbeddedInMake() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -0800275 return
276 }
277
Colin Cross2465c3d2018-09-28 10:19:18 -0700278 var androidMkModulesList []blueprint.Module
Colin Cross4f6e4e62016-01-11 12:55:55 -0800279
Colin Cross2465c3d2018-09-28 10:19:18 -0700280 ctx.VisitAllModulesBlueprint(func(module blueprint.Module) {
Colin Cross0875c522017-11-28 17:34:01 -0800281 androidMkModulesList = append(androidMkModulesList, module)
Colin Cross4f6e4e62016-01-11 12:55:55 -0800282 })
Dan Willemsen218f6562015-07-08 18:13:11 -0700283
Colin Cross1ad81422019-01-14 12:47:35 -0800284 sort.SliceStable(androidMkModulesList, func(i, j int) bool {
285 return ctx.ModuleName(androidMkModulesList[i]) < ctx.ModuleName(androidMkModulesList[j])
286 })
Colin Crossd779da42015-12-17 18:00:23 -0800287
Dan Willemsen45133ac2018-03-09 21:22:06 -0800288 transMk := PathForOutput(ctx, "Android"+String(ctx.Config().productVariables.Make_suffix)+".mk")
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700289 if ctx.Failed() {
290 return
291 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700292
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700293 err := translateAndroidMk(ctx, transMk.String(), androidMkModulesList)
Dan Willemsen218f6562015-07-08 18:13:11 -0700294 if err != nil {
295 ctx.Errorf(err.Error())
296 }
297
Colin Cross0875c522017-11-28 17:34:01 -0800298 ctx.Build(pctx, BuildParams{
299 Rule: blueprint.Phony,
300 Output: transMk,
Dan Willemsen218f6562015-07-08 18:13:11 -0700301 })
302}
303
Colin Cross2465c3d2018-09-28 10:19:18 -0700304func translateAndroidMk(ctx SingletonContext, mkFile string, mods []blueprint.Module) error {
Dan Willemsen218f6562015-07-08 18:13:11 -0700305 buf := &bytes.Buffer{}
306
Dan Willemsen97750522016-02-09 17:43:51 -0800307 fmt.Fprintln(buf, "LOCAL_MODULE_MAKEFILE := $(lastword $(MAKEFILE_LIST))")
Dan Willemsen218f6562015-07-08 18:13:11 -0700308
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700309 type_stats := make(map[string]int)
Dan Willemsen218f6562015-07-08 18:13:11 -0700310 for _, mod := range mods {
311 err := translateAndroidMkModule(ctx, buf, mod)
312 if err != nil {
313 os.Remove(mkFile)
314 return err
315 }
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700316
Colin Cross2465c3d2018-09-28 10:19:18 -0700317 if amod, ok := mod.(Module); ok && ctx.PrimaryModule(amod) == amod {
318 type_stats[ctx.ModuleType(amod)] += 1
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700319 }
320 }
321
322 keys := []string{}
323 fmt.Fprintln(buf, "\nSTATS.SOONG_MODULE_TYPE :=")
324 for k := range type_stats {
325 keys = append(keys, k)
326 }
327 sort.Strings(keys)
328 for _, mod_type := range keys {
329 fmt.Fprintln(buf, "STATS.SOONG_MODULE_TYPE +=", mod_type)
330 fmt.Fprintf(buf, "STATS.SOONG_MODULE_TYPE.%s := %d\n", mod_type, type_stats[mod_type])
Dan Willemsen218f6562015-07-08 18:13:11 -0700331 }
332
333 // Don't write to the file if it hasn't changed
334 if _, err := os.Stat(mkFile); !os.IsNotExist(err) {
335 if data, err := ioutil.ReadFile(mkFile); err == nil {
336 matches := buf.Len() == len(data)
337
338 if matches {
339 for i, value := range buf.Bytes() {
340 if value != data[i] {
341 matches = false
342 break
343 }
344 }
345 }
346
347 if matches {
348 return nil
349 }
350 }
351 }
352
353 return ioutil.WriteFile(mkFile, buf.Bytes(), 0666)
354}
355
Colin Cross0875c522017-11-28 17:34:01 -0800356func translateAndroidMkModule(ctx SingletonContext, w io.Writer, mod blueprint.Module) error {
Colin Cross953d3a22018-09-05 16:23:54 -0700357 defer func() {
358 if r := recover(); r != nil {
359 panic(fmt.Errorf("%s in translateAndroidMkModule for module %s variant %s",
360 r, ctx.ModuleName(mod), ctx.ModuleSubDir(mod)))
361 }
362 }()
363
Colin Cross2465c3d2018-09-28 10:19:18 -0700364 switch x := mod.(type) {
365 case AndroidMkDataProvider:
366 return translateAndroidModule(ctx, w, mod, x)
367 case bootstrap.GoBinaryTool:
368 return translateGoBinaryModule(ctx, w, mod, x)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700369 case AndroidMkEntriesProvider:
370 return translateAndroidMkEntriesModule(ctx, w, mod, x)
Colin Cross2465c3d2018-09-28 10:19:18 -0700371 default:
Dan Willemsen218f6562015-07-08 18:13:11 -0700372 return nil
373 }
Colin Cross2465c3d2018-09-28 10:19:18 -0700374}
375
376func translateGoBinaryModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
377 goBinary bootstrap.GoBinaryTool) error {
378
379 name := ctx.ModuleName(mod)
380 fmt.Fprintln(w, ".PHONY:", name)
381 fmt.Fprintln(w, name+":", goBinary.InstallPath())
382 fmt.Fprintln(w, "")
383
384 return nil
385}
386
Jooyung Han12df5fb2019-07-11 16:18:47 +0900387func (data *AndroidMkData) fillInData(config Config, bpPath string, mod blueprint.Module) {
388 // Get the preamble content through AndroidMkEntries logic.
389 entries := AndroidMkEntries{
390 Class: data.Class,
391 SubName: data.SubName,
392 DistFile: data.DistFile,
393 OutputFile: data.OutputFile,
394 Disabled: data.Disabled,
395 Include: data.Include,
396 Required: data.Required,
397 Host_required: data.Host_required,
398 Target_required: data.Target_required,
399 }
400 entries.fillInEntries(config, bpPath, mod)
401
402 // preamble doesn't need the footer content.
403 entries.footer = bytes.Buffer{}
404 entries.write(&data.preamble)
405
406 // copy entries back to data since it is used in Custom
407 data.Required = entries.Required
408 data.Host_required = entries.Host_required
409 data.Target_required = entries.Target_required
410}
411
Colin Cross2465c3d2018-09-28 10:19:18 -0700412func translateAndroidModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
413 provider AndroidMkDataProvider) error {
Dan Willemsen218f6562015-07-08 18:13:11 -0700414
Colin Cross635c3b02016-05-18 15:37:25 -0700415 amod := mod.(Module).base()
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700416 if shouldSkipAndroidMkProcessing(amod) {
Jeff Gaston088e29e2017-11-29 16:47:17 -0800417 return nil
418 }
419
Colin Cross91825d22017-08-10 16:59:47 -0700420 data := provider.AndroidMk()
Colin Cross53499412017-09-07 13:20:25 -0700421 if data.Include == "" {
422 data.Include = "$(BUILD_PREBUILT)"
423 }
424
Jooyung Han12df5fb2019-07-11 16:18:47 +0900425 data.fillInData(ctx.Config(), ctx.BlueprintFile(mod), mod)
Dan Willemsen01a405a2016-06-13 17:19:03 -0700426
Colin Cross0f86d182017-08-10 17:07:28 -0700427 prefix := ""
428 if amod.ArchSpecific() {
429 switch amod.Os().Class {
430 case Host:
431 prefix = "HOST_"
432 case HostCross:
433 prefix = "HOST_CROSS_"
434 case Device:
435 prefix = "TARGET_"
Colin Crossa2344662016-03-24 13:14:12 -0700436
Dan Willemsen218f6562015-07-08 18:13:11 -0700437 }
438
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700439 if amod.Arch().ArchType != ctx.Config().Targets[amod.Os()][0].Arch.ArchType {
Colin Cross0f86d182017-08-10 17:07:28 -0700440 prefix = "2ND_" + prefix
441 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700442 }
443
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700444 name := provider.BaseModuleName()
Colin Cross0f86d182017-08-10 17:07:28 -0700445 blueprintDir := filepath.Dir(ctx.BlueprintFile(mod))
446
447 if data.Custom != nil {
448 data.Custom(w, name, prefix, blueprintDir, data)
449 } else {
450 WriteAndroidMkData(w, data)
451 }
452
453 return nil
454}
455
456func WriteAndroidMkData(w io.Writer, data AndroidMkData) {
457 if data.Disabled {
458 return
459 }
460
461 if !data.OutputFile.Valid() {
462 return
463 }
464
465 w.Write(data.preamble.Bytes())
466
Colin Crossca860ac2016-01-04 14:34:37 -0800467 for _, extra := range data.Extra {
Colin Cross27a4b052017-08-10 16:32:23 -0700468 extra(w, data.OutputFile.Path())
Dan Willemsen97750522016-02-09 17:43:51 -0800469 }
470
Colin Cross53499412017-09-07 13:20:25 -0700471 fmt.Fprintln(w, "include "+data.Include)
Dan Willemsen218f6562015-07-08 18:13:11 -0700472}
Sasha Smundakb6d23052019-04-01 18:37:36 -0700473
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700474func translateAndroidMkEntriesModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
475 provider AndroidMkEntriesProvider) error {
476 if shouldSkipAndroidMkProcessing(mod.(Module).base()) {
477 return nil
Sasha Smundakb6d23052019-04-01 18:37:36 -0700478 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700479
480 entries := provider.AndroidMkEntries()
481 entries.fillInEntries(ctx.Config(), ctx.BlueprintFile(mod), mod)
482
483 entries.write(w)
484
485 return nil
486}
487
488func shouldSkipAndroidMkProcessing(module *ModuleBase) bool {
489 if !module.commonProperties.NamespaceExportedToMake {
490 // TODO(jeffrygaston) do we want to validate that there are no modules being
491 // exported to Kati that depend on this module?
492 return true
Sasha Smundakb6d23052019-04-01 18:37:36 -0700493 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700494
495 return !module.Enabled() ||
496 module.commonProperties.SkipInstall ||
497 // Make does not understand LinuxBionic
498 module.Os() == LinuxBionic
Sasha Smundakb6d23052019-04-01 18:37:36 -0700499}