blob: 18b26d907939c1bbed673625baca00f52a2c698c [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 Cross2465c3d2018-09-28 10:19:18 -070075 sort.Sort(ModulesByName{androidMkModulesList, ctx})
Colin Crossd779da42015-12-17 18:00:23 -080076
Dan Willemsen45133ac2018-03-09 21:22:06 -080077 transMk := PathForOutput(ctx, "Android"+String(ctx.Config().productVariables.Make_suffix)+".mk")
Dan Willemsen34cc69e2015-09-23 15:26:20 -070078 if ctx.Failed() {
79 return
80 }
Dan Willemsen218f6562015-07-08 18:13:11 -070081
Dan Willemsen34cc69e2015-09-23 15:26:20 -070082 err := translateAndroidMk(ctx, transMk.String(), androidMkModulesList)
Dan Willemsen218f6562015-07-08 18:13:11 -070083 if err != nil {
84 ctx.Errorf(err.Error())
85 }
86
Colin Cross0875c522017-11-28 17:34:01 -080087 ctx.Build(pctx, BuildParams{
88 Rule: blueprint.Phony,
89 Output: transMk,
Dan Willemsen218f6562015-07-08 18:13:11 -070090 })
91}
92
Colin Cross2465c3d2018-09-28 10:19:18 -070093func translateAndroidMk(ctx SingletonContext, mkFile string, mods []blueprint.Module) error {
Dan Willemsen218f6562015-07-08 18:13:11 -070094 buf := &bytes.Buffer{}
95
Dan Willemsen97750522016-02-09 17:43:51 -080096 fmt.Fprintln(buf, "LOCAL_MODULE_MAKEFILE := $(lastword $(MAKEFILE_LIST))")
Dan Willemsen218f6562015-07-08 18:13:11 -070097
Dan Willemsen70e17fa2016-07-25 16:00:20 -070098 type_stats := make(map[string]int)
Dan Willemsen218f6562015-07-08 18:13:11 -070099 for _, mod := range mods {
100 err := translateAndroidMkModule(ctx, buf, mod)
101 if err != nil {
102 os.Remove(mkFile)
103 return err
104 }
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700105
Colin Cross2465c3d2018-09-28 10:19:18 -0700106 if amod, ok := mod.(Module); ok && ctx.PrimaryModule(amod) == amod {
107 type_stats[ctx.ModuleType(amod)] += 1
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700108 }
109 }
110
111 keys := []string{}
112 fmt.Fprintln(buf, "\nSTATS.SOONG_MODULE_TYPE :=")
113 for k := range type_stats {
114 keys = append(keys, k)
115 }
116 sort.Strings(keys)
117 for _, mod_type := range keys {
118 fmt.Fprintln(buf, "STATS.SOONG_MODULE_TYPE +=", mod_type)
119 fmt.Fprintf(buf, "STATS.SOONG_MODULE_TYPE.%s := %d\n", mod_type, type_stats[mod_type])
Dan Willemsen218f6562015-07-08 18:13:11 -0700120 }
121
122 // Don't write to the file if it hasn't changed
123 if _, err := os.Stat(mkFile); !os.IsNotExist(err) {
124 if data, err := ioutil.ReadFile(mkFile); err == nil {
125 matches := buf.Len() == len(data)
126
127 if matches {
128 for i, value := range buf.Bytes() {
129 if value != data[i] {
130 matches = false
131 break
132 }
133 }
134 }
135
136 if matches {
137 return nil
138 }
139 }
140 }
141
142 return ioutil.WriteFile(mkFile, buf.Bytes(), 0666)
143}
144
Colin Cross0875c522017-11-28 17:34:01 -0800145func translateAndroidMkModule(ctx SingletonContext, w io.Writer, mod blueprint.Module) error {
Colin Cross953d3a22018-09-05 16:23:54 -0700146 defer func() {
147 if r := recover(); r != nil {
148 panic(fmt.Errorf("%s in translateAndroidMkModule for module %s variant %s",
149 r, ctx.ModuleName(mod), ctx.ModuleSubDir(mod)))
150 }
151 }()
152
Colin Cross2465c3d2018-09-28 10:19:18 -0700153 switch x := mod.(type) {
154 case AndroidMkDataProvider:
155 return translateAndroidModule(ctx, w, mod, x)
156 case bootstrap.GoBinaryTool:
157 return translateGoBinaryModule(ctx, w, mod, x)
158 default:
Dan Willemsen218f6562015-07-08 18:13:11 -0700159 return nil
160 }
Colin Cross2465c3d2018-09-28 10:19:18 -0700161}
162
163func translateGoBinaryModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
164 goBinary bootstrap.GoBinaryTool) error {
165
166 name := ctx.ModuleName(mod)
167 fmt.Fprintln(w, ".PHONY:", name)
168 fmt.Fprintln(w, name+":", goBinary.InstallPath())
169 fmt.Fprintln(w, "")
170
171 return nil
172}
173
174func translateAndroidModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
175 provider AndroidMkDataProvider) error {
Dan Willemsen218f6562015-07-08 18:13:11 -0700176
Colin Crossce75d2c2016-10-06 16:12:58 -0700177 name := provider.BaseModuleName()
Colin Cross635c3b02016-05-18 15:37:25 -0700178 amod := mod.(Module).base()
Dan Willemsen218f6562015-07-08 18:13:11 -0700179
Dan Willemsen97750522016-02-09 17:43:51 -0800180 if !amod.Enabled() {
Colin Crossce75d2c2016-10-06 16:12:58 -0700181 return nil
182 }
183
184 if amod.commonProperties.SkipInstall {
185 return nil
Dan Willemsen218f6562015-07-08 18:13:11 -0700186 }
187
Jeff Gaston088e29e2017-11-29 16:47:17 -0800188 if !amod.commonProperties.NamespaceExportedToMake {
189 // TODO(jeffrygaston) do we want to validate that there are no modules being
190 // exported to Kati that depend on this module?
191 return nil
192 }
193
Colin Cross91825d22017-08-10 16:59:47 -0700194 data := provider.AndroidMk()
Colin Crosscc4f3e32016-11-23 15:41:09 -0800195
Colin Cross53499412017-09-07 13:20:25 -0700196 if data.Include == "" {
197 data.Include = "$(BUILD_PREBUILT)"
198 }
199
Logan Chien43d34c32017-12-20 01:17:32 +0800200 data.Required = append(data.Required, amod.commonProperties.Required...)
Colin Cross92430102017-10-09 14:59:32 -0700201
Dan Willemsen01a405a2016-06-13 17:19:03 -0700202 // Make does not understand LinuxBionic
203 if amod.Os() == LinuxBionic {
204 return nil
205 }
206
Colin Cross0f86d182017-08-10 17:07:28 -0700207 prefix := ""
208 if amod.ArchSpecific() {
209 switch amod.Os().Class {
210 case Host:
211 prefix = "HOST_"
212 case HostCross:
213 prefix = "HOST_CROSS_"
214 case Device:
215 prefix = "TARGET_"
Colin Crossa2344662016-03-24 13:14:12 -0700216
Dan Willemsen218f6562015-07-08 18:13:11 -0700217 }
218
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700219 if amod.Arch().ArchType != ctx.Config().Targets[amod.Os()][0].Arch.ArchType {
Colin Cross0f86d182017-08-10 17:07:28 -0700220 prefix = "2ND_" + prefix
221 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700222 }
223
Dan Willemsen569edc52018-11-19 09:33:29 -0800224 if len(amod.commonProperties.Dist.Targets) > 0 {
225 distFile := data.DistFile
226 if !distFile.Valid() {
227 distFile = data.OutputFile
228 }
229 if distFile.Valid() {
230 dest := filepath.Base(distFile.String())
231
232 if amod.commonProperties.Dist.Dest != nil {
233 var err error
234 dest, err = validateSafePath(*amod.commonProperties.Dist.Dest)
235 if err != nil {
236 // This was checked in ModuleBase.GenerateBuildActions
237 panic(err)
238 }
239 }
240
241 if amod.commonProperties.Dist.Suffix != nil {
242 ext := filepath.Ext(dest)
243 suffix := *amod.commonProperties.Dist.Suffix
244 dest = strings.TrimSuffix(dest, ext) + suffix + ext
245 }
246
247 if amod.commonProperties.Dist.Dir != nil {
248 var err error
249 dest, err = validateSafePath(*amod.commonProperties.Dist.Dir, dest)
250 if err != nil {
251 // This was checked in ModuleBase.GenerateBuildActions
252 panic(err)
253 }
254 }
255
256 goals := strings.Join(amod.commonProperties.Dist.Targets, " ")
257 fmt.Fprintln(&data.preamble, ".PHONY:", goals)
258 fmt.Fprintf(&data.preamble, "$(call dist-for-goals,%s,%s:%s)\n",
259 goals, distFile.String(), dest)
260 }
261 }
262
Colin Cross0f86d182017-08-10 17:07:28 -0700263 fmt.Fprintln(&data.preamble, "\ninclude $(CLEAR_VARS)")
264 fmt.Fprintln(&data.preamble, "LOCAL_PATH :=", filepath.Dir(ctx.BlueprintFile(mod)))
265 fmt.Fprintln(&data.preamble, "LOCAL_MODULE :=", name+data.SubName)
266 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_CLASS :=", data.Class)
267 fmt.Fprintln(&data.preamble, "LOCAL_PREBUILT_MODULE_FILE :=", data.OutputFile.String())
Dan Willemsen97750522016-02-09 17:43:51 -0800268
Colin Cross92430102017-10-09 14:59:32 -0700269 if len(data.Required) > 0 {
270 fmt.Fprintln(&data.preamble, "LOCAL_REQUIRED_MODULES := "+strings.Join(data.Required, " "))
Chris Wolfe998306e2016-08-15 14:47:23 -0400271 }
272
Dan Willemsen97750522016-02-09 17:43:51 -0800273 archStr := amod.Arch().ArchType.String()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700274 host := false
275 switch amod.Os().Class {
276 case Host:
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800277 // Make cannot identify LOCAL_MODULE_HOST_ARCH:= common.
278 if archStr != "common" {
Colin Cross0f86d182017-08-10 17:07:28 -0700279 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_HOST_ARCH :=", archStr)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800280 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700281 host = true
282 case HostCross:
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800283 // Make cannot identify LOCAL_MODULE_HOST_CROSS_ARCH:= common.
284 if archStr != "common" {
Colin Cross0f86d182017-08-10 17:07:28 -0700285 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800286 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700287 host = true
288 case Device:
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800289 // Make cannot identify LOCAL_MODULE_TARGET_ARCH:= common.
290 if archStr != "common" {
Colin Cross0f86d182017-08-10 17:07:28 -0700291 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800292 }
Dan Willemsen0fda89f2016-06-01 15:25:32 -0700293
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700294 if len(amod.commonProperties.Init_rc) > 0 {
Colin Cross0f86d182017-08-10 17:07:28 -0700295 fmt.Fprintln(&data.preamble, "LOCAL_INIT_RC := ", strings.Join(amod.commonProperties.Init_rc, " "))
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700296 }
Steven Moreland57a23d22018-04-04 15:42:19 -0700297 if len(amod.commonProperties.Vintf_fragments) > 0 {
298 fmt.Fprintln(&data.preamble, "LOCAL_VINTF_FRAGMENTS := ", strings.Join(amod.commonProperties.Vintf_fragments, " "))
299 }
Colin Cross7d716ba2017-11-01 10:38:29 -0700300 if Bool(amod.commonProperties.Proprietary) {
Colin Cross0f86d182017-08-10 17:07:28 -0700301 fmt.Fprintln(&data.preamble, "LOCAL_PROPRIETARY_MODULE := true")
Dan Willemsen7ed1ae32016-12-05 16:47:50 -0800302 }
Jiyong Park2db76922017-11-08 16:03:48 +0900303 if Bool(amod.commonProperties.Vendor) || Bool(amod.commonProperties.Soc_specific) {
Colin Cross0f86d182017-08-10 17:07:28 -0700304 fmt.Fprintln(&data.preamble, "LOCAL_VENDOR_MODULE := true")
Dan Willemsenaa118f92017-04-06 12:49:58 -0700305 }
Jiyong Park2db76922017-11-08 16:03:48 +0900306 if Bool(amod.commonProperties.Device_specific) {
307 fmt.Fprintln(&data.preamble, "LOCAL_ODM_MODULE := true")
308 }
309 if Bool(amod.commonProperties.Product_specific) {
Jaekyun Seok5cfbfbb2018-01-10 19:00:15 +0900310 fmt.Fprintln(&data.preamble, "LOCAL_PRODUCT_MODULE := true")
Jiyong Park2db76922017-11-08 16:03:48 +0900311 }
Dario Freni95cf7672018-08-17 00:57:57 +0100312 if Bool(amod.commonProperties.Product_services_specific) {
Dario Frenifd05a742018-05-29 13:28:54 +0100313 fmt.Fprintln(&data.preamble, "LOCAL_PRODUCT_SERVICES_MODULE := true")
314 }
Dan Willemsenefac4a82017-07-18 19:42:09 -0700315 if amod.commonProperties.Owner != nil {
Colin Cross0f86d182017-08-10 17:07:28 -0700316 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_OWNER :=", *amod.commonProperties.Owner)
Colin Cross55708f32017-03-20 13:23:34 -0700317 }
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +0000318 }
319
Jaewoong Jung62707f72018-11-16 13:26:43 -0800320 if amod.noticeFile != nil {
321 fmt.Fprintln(&data.preamble, "LOCAL_NOTICE_FILE :=", amod.noticeFile.String())
Dan Willemsen97750522016-02-09 17:43:51 -0800322 }
323
Colin Crossa1ad8d12016-06-01 17:09:44 -0700324 if host {
Dan Willemsen866b5632017-09-22 12:28:24 -0700325 makeOs := amod.Os().String()
326 if amod.Os() == Linux || amod.Os() == LinuxBionic {
327 makeOs = "linux"
328 }
329 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_HOST_OS :=", makeOs)
Colin Cross0f86d182017-08-10 17:07:28 -0700330 fmt.Fprintln(&data.preamble, "LOCAL_IS_HOST_MODULE := true")
Colin Crossa1ad8d12016-06-01 17:09:44 -0700331 }
332
Colin Cross0f86d182017-08-10 17:07:28 -0700333 blueprintDir := filepath.Dir(ctx.BlueprintFile(mod))
334
335 if data.Custom != nil {
336 data.Custom(w, name, prefix, blueprintDir, data)
337 } else {
338 WriteAndroidMkData(w, data)
339 }
340
341 return nil
342}
343
344func WriteAndroidMkData(w io.Writer, data AndroidMkData) {
345 if data.Disabled {
346 return
347 }
348
349 if !data.OutputFile.Valid() {
350 return
351 }
352
353 w.Write(data.preamble.Bytes())
354
Colin Crossca860ac2016-01-04 14:34:37 -0800355 for _, extra := range data.Extra {
Colin Cross27a4b052017-08-10 16:32:23 -0700356 extra(w, data.OutputFile.Path())
Dan Willemsen97750522016-02-09 17:43:51 -0800357 }
358
Colin Cross53499412017-09-07 13:20:25 -0700359 fmt.Fprintln(w, "include "+data.Include)
Dan Willemsen218f6562015-07-08 18:13:11 -0700360}