blob: 42e143951e64bffb51f4b6a9076c2c7af510fb91 [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
35type AndroidMkDataProvider interface {
Colin Crossa18e9cf2017-08-10 17:00:19 -070036 AndroidMk() AndroidMkData
Colin Crossce75d2c2016-10-06 16:12:58 -070037 BaseModuleName() string
Dan Willemsen218f6562015-07-08 18:13:11 -070038}
39
40type AndroidMkData struct {
Sasha Smundakb6d23052019-04-01 18:37:36 -070041 Class string
42 SubName string
43 DistFile OptionalPath
44 OutputFile OptionalPath
45 Disabled bool
46 Include string
47 Required []string
48 Host_required []string
49 Target_required []string
Dan Willemsen218f6562015-07-08 18:13:11 -070050
Colin Cross0f86d182017-08-10 17:07:28 -070051 Custom func(w io.Writer, name, prefix, moduleDir string, data AndroidMkData)
Dan Willemsen218f6562015-07-08 18:13:11 -070052
Colin Cross27a4b052017-08-10 16:32:23 -070053 Extra []AndroidMkExtraFunc
Colin Cross0f86d182017-08-10 17:07:28 -070054
55 preamble bytes.Buffer
Dan Willemsen218f6562015-07-08 18:13:11 -070056}
57
Colin Cross27a4b052017-08-10 16:32:23 -070058type AndroidMkExtraFunc func(w io.Writer, outputFile Path)
59
Colin Cross0875c522017-11-28 17:34:01 -080060func AndroidMkSingleton() Singleton {
Dan Willemsen218f6562015-07-08 18:13:11 -070061 return &androidMkSingleton{}
62}
63
64type androidMkSingleton struct{}
65
Colin Cross0875c522017-11-28 17:34:01 -080066func (c *androidMkSingleton) GenerateBuildActions(ctx SingletonContext) {
Colin Crossaabf6792017-11-29 00:27:14 -080067 if !ctx.Config().EmbeddedInMake() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -080068 return
69 }
70
Colin Cross2465c3d2018-09-28 10:19:18 -070071 var androidMkModulesList []blueprint.Module
Colin Cross4f6e4e62016-01-11 12:55:55 -080072
Colin Cross2465c3d2018-09-28 10:19:18 -070073 ctx.VisitAllModulesBlueprint(func(module blueprint.Module) {
Colin Cross0875c522017-11-28 17:34:01 -080074 androidMkModulesList = append(androidMkModulesList, module)
Colin Cross4f6e4e62016-01-11 12:55:55 -080075 })
Dan Willemsen218f6562015-07-08 18:13:11 -070076
Colin Cross1ad81422019-01-14 12:47:35 -080077 sort.SliceStable(androidMkModulesList, func(i, j int) bool {
78 return ctx.ModuleName(androidMkModulesList[i]) < ctx.ModuleName(androidMkModulesList[j])
79 })
Colin Crossd779da42015-12-17 18:00:23 -080080
Dan Willemsen45133ac2018-03-09 21:22:06 -080081 transMk := PathForOutput(ctx, "Android"+String(ctx.Config().productVariables.Make_suffix)+".mk")
Dan Willemsen34cc69e2015-09-23 15:26:20 -070082 if ctx.Failed() {
83 return
84 }
Dan Willemsen218f6562015-07-08 18:13:11 -070085
Dan Willemsen34cc69e2015-09-23 15:26:20 -070086 err := translateAndroidMk(ctx, transMk.String(), androidMkModulesList)
Dan Willemsen218f6562015-07-08 18:13:11 -070087 if err != nil {
88 ctx.Errorf(err.Error())
89 }
90
Colin Cross0875c522017-11-28 17:34:01 -080091 ctx.Build(pctx, BuildParams{
92 Rule: blueprint.Phony,
93 Output: transMk,
Dan Willemsen218f6562015-07-08 18:13:11 -070094 })
95}
96
Colin Cross2465c3d2018-09-28 10:19:18 -070097func translateAndroidMk(ctx SingletonContext, mkFile string, mods []blueprint.Module) error {
Dan Willemsen218f6562015-07-08 18:13:11 -070098 buf := &bytes.Buffer{}
99
Dan Willemsen97750522016-02-09 17:43:51 -0800100 fmt.Fprintln(buf, "LOCAL_MODULE_MAKEFILE := $(lastword $(MAKEFILE_LIST))")
Dan Willemsen218f6562015-07-08 18:13:11 -0700101
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700102 type_stats := make(map[string]int)
Dan Willemsen218f6562015-07-08 18:13:11 -0700103 for _, mod := range mods {
104 err := translateAndroidMkModule(ctx, buf, mod)
105 if err != nil {
106 os.Remove(mkFile)
107 return err
108 }
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700109
Colin Cross2465c3d2018-09-28 10:19:18 -0700110 if amod, ok := mod.(Module); ok && ctx.PrimaryModule(amod) == amod {
111 type_stats[ctx.ModuleType(amod)] += 1
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700112 }
113 }
114
115 keys := []string{}
116 fmt.Fprintln(buf, "\nSTATS.SOONG_MODULE_TYPE :=")
117 for k := range type_stats {
118 keys = append(keys, k)
119 }
120 sort.Strings(keys)
121 for _, mod_type := range keys {
122 fmt.Fprintln(buf, "STATS.SOONG_MODULE_TYPE +=", mod_type)
123 fmt.Fprintf(buf, "STATS.SOONG_MODULE_TYPE.%s := %d\n", mod_type, type_stats[mod_type])
Dan Willemsen218f6562015-07-08 18:13:11 -0700124 }
125
126 // Don't write to the file if it hasn't changed
127 if _, err := os.Stat(mkFile); !os.IsNotExist(err) {
128 if data, err := ioutil.ReadFile(mkFile); err == nil {
129 matches := buf.Len() == len(data)
130
131 if matches {
132 for i, value := range buf.Bytes() {
133 if value != data[i] {
134 matches = false
135 break
136 }
137 }
138 }
139
140 if matches {
141 return nil
142 }
143 }
144 }
145
146 return ioutil.WriteFile(mkFile, buf.Bytes(), 0666)
147}
148
Colin Cross0875c522017-11-28 17:34:01 -0800149func translateAndroidMkModule(ctx SingletonContext, w io.Writer, mod blueprint.Module) error {
Colin Cross953d3a22018-09-05 16:23:54 -0700150 defer func() {
151 if r := recover(); r != nil {
152 panic(fmt.Errorf("%s in translateAndroidMkModule for module %s variant %s",
153 r, ctx.ModuleName(mod), ctx.ModuleSubDir(mod)))
154 }
155 }()
156
Colin Cross2465c3d2018-09-28 10:19:18 -0700157 switch x := mod.(type) {
158 case AndroidMkDataProvider:
159 return translateAndroidModule(ctx, w, mod, x)
160 case bootstrap.GoBinaryTool:
161 return translateGoBinaryModule(ctx, w, mod, x)
162 default:
Dan Willemsen218f6562015-07-08 18:13:11 -0700163 return nil
164 }
Colin Cross2465c3d2018-09-28 10:19:18 -0700165}
166
167func translateGoBinaryModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
168 goBinary bootstrap.GoBinaryTool) error {
169
170 name := ctx.ModuleName(mod)
171 fmt.Fprintln(w, ".PHONY:", name)
172 fmt.Fprintln(w, name+":", goBinary.InstallPath())
173 fmt.Fprintln(w, "")
174
175 return nil
176}
177
178func translateAndroidModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
179 provider AndroidMkDataProvider) error {
Dan Willemsen218f6562015-07-08 18:13:11 -0700180
Colin Crossce75d2c2016-10-06 16:12:58 -0700181 name := provider.BaseModuleName()
Colin Cross635c3b02016-05-18 15:37:25 -0700182 amod := mod.(Module).base()
Dan Willemsen218f6562015-07-08 18:13:11 -0700183
Dan Willemsen97750522016-02-09 17:43:51 -0800184 if !amod.Enabled() {
Colin Crossce75d2c2016-10-06 16:12:58 -0700185 return nil
186 }
187
188 if amod.commonProperties.SkipInstall {
189 return nil
Dan Willemsen218f6562015-07-08 18:13:11 -0700190 }
191
Jeff Gaston088e29e2017-11-29 16:47:17 -0800192 if !amod.commonProperties.NamespaceExportedToMake {
193 // TODO(jeffrygaston) do we want to validate that there are no modules being
194 // exported to Kati that depend on this module?
195 return nil
196 }
197
Colin Cross91825d22017-08-10 16:59:47 -0700198 data := provider.AndroidMk()
Colin Crosscc4f3e32016-11-23 15:41:09 -0800199
Colin Cross53499412017-09-07 13:20:25 -0700200 if data.Include == "" {
201 data.Include = "$(BUILD_PREBUILT)"
202 }
203
Logan Chien43d34c32017-12-20 01:17:32 +0800204 data.Required = append(data.Required, amod.commonProperties.Required...)
Sasha Smundakb6d23052019-04-01 18:37:36 -0700205 data.Host_required = append(data.Host_required, amod.commonProperties.Host_required...)
206 data.Target_required = append(data.Target_required, amod.commonProperties.Target_required...)
Colin Cross92430102017-10-09 14:59:32 -0700207
Dan Willemsen01a405a2016-06-13 17:19:03 -0700208 // Make does not understand LinuxBionic
209 if amod.Os() == LinuxBionic {
210 return nil
211 }
212
Colin Cross0f86d182017-08-10 17:07:28 -0700213 prefix := ""
214 if amod.ArchSpecific() {
215 switch amod.Os().Class {
216 case Host:
217 prefix = "HOST_"
218 case HostCross:
219 prefix = "HOST_CROSS_"
220 case Device:
221 prefix = "TARGET_"
Colin Crossa2344662016-03-24 13:14:12 -0700222
Dan Willemsen218f6562015-07-08 18:13:11 -0700223 }
224
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700225 if amod.Arch().ArchType != ctx.Config().Targets[amod.Os()][0].Arch.ArchType {
Colin Cross0f86d182017-08-10 17:07:28 -0700226 prefix = "2ND_" + prefix
227 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700228 }
229
Dan Willemsen569edc52018-11-19 09:33:29 -0800230 if len(amod.commonProperties.Dist.Targets) > 0 {
231 distFile := data.DistFile
232 if !distFile.Valid() {
233 distFile = data.OutputFile
234 }
235 if distFile.Valid() {
236 dest := filepath.Base(distFile.String())
237
238 if amod.commonProperties.Dist.Dest != nil {
239 var err error
240 dest, err = validateSafePath(*amod.commonProperties.Dist.Dest)
241 if err != nil {
242 // This was checked in ModuleBase.GenerateBuildActions
243 panic(err)
244 }
245 }
246
247 if amod.commonProperties.Dist.Suffix != nil {
248 ext := filepath.Ext(dest)
249 suffix := *amod.commonProperties.Dist.Suffix
250 dest = strings.TrimSuffix(dest, ext) + suffix + ext
251 }
252
253 if amod.commonProperties.Dist.Dir != nil {
254 var err error
255 dest, err = validateSafePath(*amod.commonProperties.Dist.Dir, dest)
256 if err != nil {
257 // This was checked in ModuleBase.GenerateBuildActions
258 panic(err)
259 }
260 }
261
262 goals := strings.Join(amod.commonProperties.Dist.Targets, " ")
263 fmt.Fprintln(&data.preamble, ".PHONY:", goals)
264 fmt.Fprintf(&data.preamble, "$(call dist-for-goals,%s,%s:%s)\n",
265 goals, distFile.String(), dest)
266 }
267 }
268
Colin Cross0f86d182017-08-10 17:07:28 -0700269 fmt.Fprintln(&data.preamble, "\ninclude $(CLEAR_VARS)")
270 fmt.Fprintln(&data.preamble, "LOCAL_PATH :=", filepath.Dir(ctx.BlueprintFile(mod)))
271 fmt.Fprintln(&data.preamble, "LOCAL_MODULE :=", name+data.SubName)
272 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_CLASS :=", data.Class)
273 fmt.Fprintln(&data.preamble, "LOCAL_PREBUILT_MODULE_FILE :=", data.OutputFile.String())
Sasha Smundakb6d23052019-04-01 18:37:36 -0700274 WriteRequiredModulesSettings(&data.preamble, data)
Chris Wolfe998306e2016-08-15 14:47:23 -0400275
Dan Willemsen97750522016-02-09 17:43:51 -0800276 archStr := amod.Arch().ArchType.String()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700277 host := false
278 switch amod.Os().Class {
279 case Host:
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800280 // Make cannot identify LOCAL_MODULE_HOST_ARCH:= common.
281 if archStr != "common" {
Colin Cross0f86d182017-08-10 17:07:28 -0700282 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_HOST_ARCH :=", archStr)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800283 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700284 host = true
285 case HostCross:
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800286 // Make cannot identify LOCAL_MODULE_HOST_CROSS_ARCH:= common.
287 if archStr != "common" {
Colin Cross0f86d182017-08-10 17:07:28 -0700288 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800289 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700290 host = true
291 case Device:
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800292 // Make cannot identify LOCAL_MODULE_TARGET_ARCH:= common.
293 if archStr != "common" {
Colin Cross0f86d182017-08-10 17:07:28 -0700294 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800295 }
Dan Willemsen0fda89f2016-06-01 15:25:32 -0700296
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700297 if len(amod.commonProperties.Init_rc) > 0 {
Colin Cross0f86d182017-08-10 17:07:28 -0700298 fmt.Fprintln(&data.preamble, "LOCAL_INIT_RC := ", strings.Join(amod.commonProperties.Init_rc, " "))
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700299 }
Steven Moreland57a23d22018-04-04 15:42:19 -0700300 if len(amod.commonProperties.Vintf_fragments) > 0 {
301 fmt.Fprintln(&data.preamble, "LOCAL_VINTF_FRAGMENTS := ", strings.Join(amod.commonProperties.Vintf_fragments, " "))
302 }
Colin Cross7d716ba2017-11-01 10:38:29 -0700303 if Bool(amod.commonProperties.Proprietary) {
Colin Cross0f86d182017-08-10 17:07:28 -0700304 fmt.Fprintln(&data.preamble, "LOCAL_PROPRIETARY_MODULE := true")
Dan Willemsen7ed1ae32016-12-05 16:47:50 -0800305 }
Jiyong Park2db76922017-11-08 16:03:48 +0900306 if Bool(amod.commonProperties.Vendor) || Bool(amod.commonProperties.Soc_specific) {
Colin Cross0f86d182017-08-10 17:07:28 -0700307 fmt.Fprintln(&data.preamble, "LOCAL_VENDOR_MODULE := true")
Dan Willemsenaa118f92017-04-06 12:49:58 -0700308 }
Jiyong Park2db76922017-11-08 16:03:48 +0900309 if Bool(amod.commonProperties.Device_specific) {
310 fmt.Fprintln(&data.preamble, "LOCAL_ODM_MODULE := true")
311 }
312 if Bool(amod.commonProperties.Product_specific) {
Jaekyun Seok5cfbfbb2018-01-10 19:00:15 +0900313 fmt.Fprintln(&data.preamble, "LOCAL_PRODUCT_MODULE := true")
Jiyong Park2db76922017-11-08 16:03:48 +0900314 }
Dario Freni95cf7672018-08-17 00:57:57 +0100315 if Bool(amod.commonProperties.Product_services_specific) {
Dario Frenifd05a742018-05-29 13:28:54 +0100316 fmt.Fprintln(&data.preamble, "LOCAL_PRODUCT_SERVICES_MODULE := true")
317 }
Dan Willemsenefac4a82017-07-18 19:42:09 -0700318 if amod.commonProperties.Owner != nil {
Colin Cross0f86d182017-08-10 17:07:28 -0700319 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_OWNER :=", *amod.commonProperties.Owner)
Colin Cross55708f32017-03-20 13:23:34 -0700320 }
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +0000321 }
322
Jiyong Park52818fc2019-03-18 12:01:38 +0900323 if amod.noticeFile.Valid() {
Jaewoong Jung62707f72018-11-16 13:26:43 -0800324 fmt.Fprintln(&data.preamble, "LOCAL_NOTICE_FILE :=", amod.noticeFile.String())
Dan Willemsen97750522016-02-09 17:43:51 -0800325 }
326
Colin Crossa1ad8d12016-06-01 17:09:44 -0700327 if host {
Dan Willemsen866b5632017-09-22 12:28:24 -0700328 makeOs := amod.Os().String()
329 if amod.Os() == Linux || amod.Os() == LinuxBionic {
330 makeOs = "linux"
331 }
332 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_HOST_OS :=", makeOs)
Colin Cross0f86d182017-08-10 17:07:28 -0700333 fmt.Fprintln(&data.preamble, "LOCAL_IS_HOST_MODULE := true")
Colin Crossa1ad8d12016-06-01 17:09:44 -0700334 }
335
Colin Cross0f86d182017-08-10 17:07:28 -0700336 blueprintDir := filepath.Dir(ctx.BlueprintFile(mod))
337
338 if data.Custom != nil {
339 data.Custom(w, name, prefix, blueprintDir, data)
340 } else {
341 WriteAndroidMkData(w, data)
342 }
343
344 return nil
345}
346
347func WriteAndroidMkData(w io.Writer, data AndroidMkData) {
348 if data.Disabled {
349 return
350 }
351
352 if !data.OutputFile.Valid() {
353 return
354 }
355
356 w.Write(data.preamble.Bytes())
357
Colin Crossca860ac2016-01-04 14:34:37 -0800358 for _, extra := range data.Extra {
Colin Cross27a4b052017-08-10 16:32:23 -0700359 extra(w, data.OutputFile.Path())
Dan Willemsen97750522016-02-09 17:43:51 -0800360 }
361
Colin Cross53499412017-09-07 13:20:25 -0700362 fmt.Fprintln(w, "include "+data.Include)
Dan Willemsen218f6562015-07-08 18:13:11 -0700363}
Sasha Smundakb6d23052019-04-01 18:37:36 -0700364
365func WriteRequiredModulesSettings(w io.Writer, data AndroidMkData) {
366 if len(data.Required) > 0 {
367 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=", strings.Join(data.Required, " "))
368 }
369 if len(data.Host_required) > 0 {
370 fmt.Fprintln(w, "LOCAL_HOST_REQUIRED_MODULES :=", strings.Join(data.Host_required, " "))
371 }
372 if len(data.Target_required) > 0 {
373 fmt.Fprintln(w, "LOCAL_TARGET_REQUIRED_MODULES :=", strings.Join(data.Target_required, " "))
374 }
375}