blob: bd49e4c6f6e897feee3c4f79581b9b051b1edd78 [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 {
41 Class string
Colin Crossa2344662016-03-24 13:14:12 -070042 SubName string
Dan Willemsen569edc52018-11-19 09:33:29 -080043 DistFile OptionalPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -070044 OutputFile OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -080045 Disabled bool
Colin Cross53499412017-09-07 13:20:25 -070046 Include string
Colin Cross92430102017-10-09 14:59:32 -070047 Required []string
Dan Willemsen218f6562015-07-08 18:13:11 -070048
Colin Cross0f86d182017-08-10 17:07:28 -070049 Custom func(w io.Writer, name, prefix, moduleDir string, data AndroidMkData)
Dan Willemsen218f6562015-07-08 18:13:11 -070050
Colin Cross27a4b052017-08-10 16:32:23 -070051 Extra []AndroidMkExtraFunc
Colin Cross0f86d182017-08-10 17:07:28 -070052
53 preamble bytes.Buffer
Dan Willemsen218f6562015-07-08 18:13:11 -070054}
55
Colin Cross27a4b052017-08-10 16:32:23 -070056type AndroidMkExtraFunc func(w io.Writer, outputFile Path)
57
Colin Cross0875c522017-11-28 17:34:01 -080058func AndroidMkSingleton() Singleton {
Dan Willemsen218f6562015-07-08 18:13:11 -070059 return &androidMkSingleton{}
60}
61
62type androidMkSingleton struct{}
63
Colin Cross0875c522017-11-28 17:34:01 -080064func (c *androidMkSingleton) GenerateBuildActions(ctx SingletonContext) {
Colin Crossaabf6792017-11-29 00:27:14 -080065 if !ctx.Config().EmbeddedInMake() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -080066 return
67 }
68
Colin Cross2465c3d2018-09-28 10:19:18 -070069 var androidMkModulesList []blueprint.Module
Colin Cross4f6e4e62016-01-11 12:55:55 -080070
Colin Cross2465c3d2018-09-28 10:19:18 -070071 ctx.VisitAllModulesBlueprint(func(module blueprint.Module) {
Colin Cross0875c522017-11-28 17:34:01 -080072 androidMkModulesList = append(androidMkModulesList, module)
Colin Cross4f6e4e62016-01-11 12:55:55 -080073 })
Dan Willemsen218f6562015-07-08 18:13:11 -070074
Colin Cross1ad81422019-01-14 12:47:35 -080075 sort.SliceStable(androidMkModulesList, func(i, j int) bool {
76 return ctx.ModuleName(androidMkModulesList[i]) < ctx.ModuleName(androidMkModulesList[j])
77 })
Colin Crossd779da42015-12-17 18:00:23 -080078
Dan Willemsen45133ac2018-03-09 21:22:06 -080079 transMk := PathForOutput(ctx, "Android"+String(ctx.Config().productVariables.Make_suffix)+".mk")
Dan Willemsen34cc69e2015-09-23 15:26:20 -070080 if ctx.Failed() {
81 return
82 }
Dan Willemsen218f6562015-07-08 18:13:11 -070083
Dan Willemsen34cc69e2015-09-23 15:26:20 -070084 err := translateAndroidMk(ctx, transMk.String(), androidMkModulesList)
Dan Willemsen218f6562015-07-08 18:13:11 -070085 if err != nil {
86 ctx.Errorf(err.Error())
87 }
88
Colin Cross0875c522017-11-28 17:34:01 -080089 ctx.Build(pctx, BuildParams{
90 Rule: blueprint.Phony,
91 Output: transMk,
Dan Willemsen218f6562015-07-08 18:13:11 -070092 })
93}
94
Colin Cross2465c3d2018-09-28 10:19:18 -070095func translateAndroidMk(ctx SingletonContext, mkFile string, mods []blueprint.Module) error {
Dan Willemsen218f6562015-07-08 18:13:11 -070096 buf := &bytes.Buffer{}
97
Dan Willemsen97750522016-02-09 17:43:51 -080098 fmt.Fprintln(buf, "LOCAL_MODULE_MAKEFILE := $(lastword $(MAKEFILE_LIST))")
Dan Willemsen218f6562015-07-08 18:13:11 -070099
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700100 type_stats := make(map[string]int)
Dan Willemsen218f6562015-07-08 18:13:11 -0700101 for _, mod := range mods {
102 err := translateAndroidMkModule(ctx, buf, mod)
103 if err != nil {
104 os.Remove(mkFile)
105 return err
106 }
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700107
Colin Cross2465c3d2018-09-28 10:19:18 -0700108 if amod, ok := mod.(Module); ok && ctx.PrimaryModule(amod) == amod {
109 type_stats[ctx.ModuleType(amod)] += 1
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700110 }
111 }
112
113 keys := []string{}
114 fmt.Fprintln(buf, "\nSTATS.SOONG_MODULE_TYPE :=")
115 for k := range type_stats {
116 keys = append(keys, k)
117 }
118 sort.Strings(keys)
119 for _, mod_type := range keys {
120 fmt.Fprintln(buf, "STATS.SOONG_MODULE_TYPE +=", mod_type)
121 fmt.Fprintf(buf, "STATS.SOONG_MODULE_TYPE.%s := %d\n", mod_type, type_stats[mod_type])
Dan Willemsen218f6562015-07-08 18:13:11 -0700122 }
123
124 // Don't write to the file if it hasn't changed
125 if _, err := os.Stat(mkFile); !os.IsNotExist(err) {
126 if data, err := ioutil.ReadFile(mkFile); err == nil {
127 matches := buf.Len() == len(data)
128
129 if matches {
130 for i, value := range buf.Bytes() {
131 if value != data[i] {
132 matches = false
133 break
134 }
135 }
136 }
137
138 if matches {
139 return nil
140 }
141 }
142 }
143
144 return ioutil.WriteFile(mkFile, buf.Bytes(), 0666)
145}
146
Colin Cross0875c522017-11-28 17:34:01 -0800147func translateAndroidMkModule(ctx SingletonContext, w io.Writer, mod blueprint.Module) error {
Colin Cross953d3a22018-09-05 16:23:54 -0700148 defer func() {
149 if r := recover(); r != nil {
150 panic(fmt.Errorf("%s in translateAndroidMkModule for module %s variant %s",
151 r, ctx.ModuleName(mod), ctx.ModuleSubDir(mod)))
152 }
153 }()
154
Colin Cross2465c3d2018-09-28 10:19:18 -0700155 switch x := mod.(type) {
156 case AndroidMkDataProvider:
157 return translateAndroidModule(ctx, w, mod, x)
158 case bootstrap.GoBinaryTool:
159 return translateGoBinaryModule(ctx, w, mod, x)
160 default:
Dan Willemsen218f6562015-07-08 18:13:11 -0700161 return nil
162 }
Colin Cross2465c3d2018-09-28 10:19:18 -0700163}
164
165func translateGoBinaryModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
166 goBinary bootstrap.GoBinaryTool) error {
167
168 name := ctx.ModuleName(mod)
169 fmt.Fprintln(w, ".PHONY:", name)
170 fmt.Fprintln(w, name+":", goBinary.InstallPath())
171 fmt.Fprintln(w, "")
172
173 return nil
174}
175
176func translateAndroidModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
177 provider AndroidMkDataProvider) error {
Dan Willemsen218f6562015-07-08 18:13:11 -0700178
Colin Crossce75d2c2016-10-06 16:12:58 -0700179 name := provider.BaseModuleName()
Colin Cross635c3b02016-05-18 15:37:25 -0700180 amod := mod.(Module).base()
Dan Willemsen218f6562015-07-08 18:13:11 -0700181
Dan Willemsen97750522016-02-09 17:43:51 -0800182 if !amod.Enabled() {
Colin Crossce75d2c2016-10-06 16:12:58 -0700183 return nil
184 }
185
186 if amod.commonProperties.SkipInstall {
187 return nil
Dan Willemsen218f6562015-07-08 18:13:11 -0700188 }
189
Jeff Gaston088e29e2017-11-29 16:47:17 -0800190 if !amod.commonProperties.NamespaceExportedToMake {
191 // TODO(jeffrygaston) do we want to validate that there are no modules being
192 // exported to Kati that depend on this module?
193 return nil
194 }
195
Colin Cross91825d22017-08-10 16:59:47 -0700196 data := provider.AndroidMk()
Colin Crosscc4f3e32016-11-23 15:41:09 -0800197
Colin Cross53499412017-09-07 13:20:25 -0700198 if data.Include == "" {
199 data.Include = "$(BUILD_PREBUILT)"
200 }
201
Logan Chien43d34c32017-12-20 01:17:32 +0800202 data.Required = append(data.Required, amod.commonProperties.Required...)
Colin Cross92430102017-10-09 14:59:32 -0700203
Dan Willemsen01a405a2016-06-13 17:19:03 -0700204 // Make does not understand LinuxBionic
205 if amod.Os() == LinuxBionic {
206 return nil
207 }
208
Colin Cross0f86d182017-08-10 17:07:28 -0700209 prefix := ""
210 if amod.ArchSpecific() {
211 switch amod.Os().Class {
212 case Host:
213 prefix = "HOST_"
214 case HostCross:
215 prefix = "HOST_CROSS_"
216 case Device:
217 prefix = "TARGET_"
Colin Crossa2344662016-03-24 13:14:12 -0700218
Dan Willemsen218f6562015-07-08 18:13:11 -0700219 }
220
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700221 if amod.Arch().ArchType != ctx.Config().Targets[amod.Os()][0].Arch.ArchType {
Colin Cross0f86d182017-08-10 17:07:28 -0700222 prefix = "2ND_" + prefix
223 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700224 }
225
Dan Willemsen569edc52018-11-19 09:33:29 -0800226 if len(amod.commonProperties.Dist.Targets) > 0 {
227 distFile := data.DistFile
228 if !distFile.Valid() {
229 distFile = data.OutputFile
230 }
231 if distFile.Valid() {
232 dest := filepath.Base(distFile.String())
233
234 if amod.commonProperties.Dist.Dest != nil {
235 var err error
236 dest, err = validateSafePath(*amod.commonProperties.Dist.Dest)
237 if err != nil {
238 // This was checked in ModuleBase.GenerateBuildActions
239 panic(err)
240 }
241 }
242
243 if amod.commonProperties.Dist.Suffix != nil {
244 ext := filepath.Ext(dest)
245 suffix := *amod.commonProperties.Dist.Suffix
246 dest = strings.TrimSuffix(dest, ext) + suffix + ext
247 }
248
249 if amod.commonProperties.Dist.Dir != nil {
250 var err error
251 dest, err = validateSafePath(*amod.commonProperties.Dist.Dir, dest)
252 if err != nil {
253 // This was checked in ModuleBase.GenerateBuildActions
254 panic(err)
255 }
256 }
257
258 goals := strings.Join(amod.commonProperties.Dist.Targets, " ")
259 fmt.Fprintln(&data.preamble, ".PHONY:", goals)
260 fmt.Fprintf(&data.preamble, "$(call dist-for-goals,%s,%s:%s)\n",
261 goals, distFile.String(), dest)
262 }
263 }
264
Colin Cross0f86d182017-08-10 17:07:28 -0700265 fmt.Fprintln(&data.preamble, "\ninclude $(CLEAR_VARS)")
266 fmt.Fprintln(&data.preamble, "LOCAL_PATH :=", filepath.Dir(ctx.BlueprintFile(mod)))
267 fmt.Fprintln(&data.preamble, "LOCAL_MODULE :=", name+data.SubName)
268 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_CLASS :=", data.Class)
269 fmt.Fprintln(&data.preamble, "LOCAL_PREBUILT_MODULE_FILE :=", data.OutputFile.String())
Dan Willemsen97750522016-02-09 17:43:51 -0800270
Colin Cross92430102017-10-09 14:59:32 -0700271 if len(data.Required) > 0 {
272 fmt.Fprintln(&data.preamble, "LOCAL_REQUIRED_MODULES := "+strings.Join(data.Required, " "))
Chris Wolfe998306e2016-08-15 14:47:23 -0400273 }
274
Dan Willemsen97750522016-02-09 17:43:51 -0800275 archStr := amod.Arch().ArchType.String()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700276 host := false
277 switch amod.Os().Class {
278 case Host:
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800279 // Make cannot identify LOCAL_MODULE_HOST_ARCH:= common.
280 if archStr != "common" {
Colin Cross0f86d182017-08-10 17:07:28 -0700281 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_HOST_ARCH :=", archStr)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800282 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700283 host = true
284 case HostCross:
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800285 // Make cannot identify LOCAL_MODULE_HOST_CROSS_ARCH:= common.
286 if archStr != "common" {
Colin Cross0f86d182017-08-10 17:07:28 -0700287 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800288 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700289 host = true
290 case Device:
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800291 // Make cannot identify LOCAL_MODULE_TARGET_ARCH:= common.
292 if archStr != "common" {
Colin Cross0f86d182017-08-10 17:07:28 -0700293 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800294 }
Dan Willemsen0fda89f2016-06-01 15:25:32 -0700295
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700296 if len(amod.commonProperties.Init_rc) > 0 {
Colin Cross0f86d182017-08-10 17:07:28 -0700297 fmt.Fprintln(&data.preamble, "LOCAL_INIT_RC := ", strings.Join(amod.commonProperties.Init_rc, " "))
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700298 }
Steven Moreland57a23d22018-04-04 15:42:19 -0700299 if len(amod.commonProperties.Vintf_fragments) > 0 {
300 fmt.Fprintln(&data.preamble, "LOCAL_VINTF_FRAGMENTS := ", strings.Join(amod.commonProperties.Vintf_fragments, " "))
301 }
Colin Cross7d716ba2017-11-01 10:38:29 -0700302 if Bool(amod.commonProperties.Proprietary) {
Colin Cross0f86d182017-08-10 17:07:28 -0700303 fmt.Fprintln(&data.preamble, "LOCAL_PROPRIETARY_MODULE := true")
Dan Willemsen7ed1ae32016-12-05 16:47:50 -0800304 }
Jiyong Park2db76922017-11-08 16:03:48 +0900305 if Bool(amod.commonProperties.Vendor) || Bool(amod.commonProperties.Soc_specific) {
Colin Cross0f86d182017-08-10 17:07:28 -0700306 fmt.Fprintln(&data.preamble, "LOCAL_VENDOR_MODULE := true")
Dan Willemsenaa118f92017-04-06 12:49:58 -0700307 }
Jiyong Park2db76922017-11-08 16:03:48 +0900308 if Bool(amod.commonProperties.Device_specific) {
309 fmt.Fprintln(&data.preamble, "LOCAL_ODM_MODULE := true")
310 }
311 if Bool(amod.commonProperties.Product_specific) {
Jaekyun Seok5cfbfbb2018-01-10 19:00:15 +0900312 fmt.Fprintln(&data.preamble, "LOCAL_PRODUCT_MODULE := true")
Jiyong Park2db76922017-11-08 16:03:48 +0900313 }
Dario Freni95cf7672018-08-17 00:57:57 +0100314 if Bool(amod.commonProperties.Product_services_specific) {
Dario Frenifd05a742018-05-29 13:28:54 +0100315 fmt.Fprintln(&data.preamble, "LOCAL_PRODUCT_SERVICES_MODULE := true")
316 }
Dan Willemsenefac4a82017-07-18 19:42:09 -0700317 if amod.commonProperties.Owner != nil {
Colin Cross0f86d182017-08-10 17:07:28 -0700318 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_OWNER :=", *amod.commonProperties.Owner)
Colin Cross55708f32017-03-20 13:23:34 -0700319 }
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +0000320 }
321
Jiyong Park52818fc2019-03-18 12:01:38 +0900322 if amod.noticeFile.Valid() {
Jaewoong Jung62707f72018-11-16 13:26:43 -0800323 fmt.Fprintln(&data.preamble, "LOCAL_NOTICE_FILE :=", amod.noticeFile.String())
Dan Willemsen97750522016-02-09 17:43:51 -0800324 }
325
Colin Crossa1ad8d12016-06-01 17:09:44 -0700326 if host {
Dan Willemsen866b5632017-09-22 12:28:24 -0700327 makeOs := amod.Os().String()
328 if amod.Os() == Linux || amod.Os() == LinuxBionic {
329 makeOs = "linux"
330 }
331 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_HOST_OS :=", makeOs)
Colin Cross0f86d182017-08-10 17:07:28 -0700332 fmt.Fprintln(&data.preamble, "LOCAL_IS_HOST_MODULE := true")
Colin Crossa1ad8d12016-06-01 17:09:44 -0700333 }
334
Colin Cross0f86d182017-08-10 17:07:28 -0700335 blueprintDir := filepath.Dir(ctx.BlueprintFile(mod))
336
337 if data.Custom != nil {
338 data.Custom(w, name, prefix, blueprintDir, data)
339 } else {
340 WriteAndroidMkData(w, data)
341 }
342
343 return nil
344}
345
346func WriteAndroidMkData(w io.Writer, data AndroidMkData) {
347 if data.Disabled {
348 return
349 }
350
351 if !data.OutputFile.Valid() {
352 return
353 }
354
355 w.Write(data.preamble.Bytes())
356
Colin Crossca860ac2016-01-04 14:34:37 -0800357 for _, extra := range data.Extra {
Colin Cross27a4b052017-08-10 16:32:23 -0700358 extra(w, data.OutputFile.Path())
Dan Willemsen97750522016-02-09 17:43:51 -0800359 }
360
Colin Cross53499412017-09-07 13:20:25 -0700361 fmt.Fprintln(w, "include "+data.Include)
Dan Willemsen218f6562015-07-08 18:13:11 -0700362}