blob: 703052315fd9821b33a84ad6eab4908063b588d6 [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 Willemsen34cc69e2015-09-23 15:26:20 -070043 OutputFile OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -080044 Disabled bool
Colin Cross53499412017-09-07 13:20:25 -070045 Include string
Colin Cross92430102017-10-09 14:59:32 -070046 Required []string
Dan Willemsen218f6562015-07-08 18:13:11 -070047
Colin Cross0f86d182017-08-10 17:07:28 -070048 Custom func(w io.Writer, name, prefix, moduleDir string, data AndroidMkData)
Dan Willemsen218f6562015-07-08 18:13:11 -070049
Colin Cross27a4b052017-08-10 16:32:23 -070050 Extra []AndroidMkExtraFunc
Colin Cross0f86d182017-08-10 17:07:28 -070051
52 preamble bytes.Buffer
Dan Willemsen218f6562015-07-08 18:13:11 -070053}
54
Colin Cross27a4b052017-08-10 16:32:23 -070055type AndroidMkExtraFunc func(w io.Writer, outputFile Path)
56
Colin Cross0875c522017-11-28 17:34:01 -080057func AndroidMkSingleton() Singleton {
Dan Willemsen218f6562015-07-08 18:13:11 -070058 return &androidMkSingleton{}
59}
60
61type androidMkSingleton struct{}
62
Colin Cross0875c522017-11-28 17:34:01 -080063func (c *androidMkSingleton) GenerateBuildActions(ctx SingletonContext) {
Colin Crossaabf6792017-11-29 00:27:14 -080064 if !ctx.Config().EmbeddedInMake() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -080065 return
66 }
67
Colin Cross2465c3d2018-09-28 10:19:18 -070068 var androidMkModulesList []blueprint.Module
Colin Cross4f6e4e62016-01-11 12:55:55 -080069
Colin Cross2465c3d2018-09-28 10:19:18 -070070 ctx.VisitAllModulesBlueprint(func(module blueprint.Module) {
Colin Cross0875c522017-11-28 17:34:01 -080071 androidMkModulesList = append(androidMkModulesList, module)
Colin Cross4f6e4e62016-01-11 12:55:55 -080072 })
Dan Willemsen218f6562015-07-08 18:13:11 -070073
Colin Cross2465c3d2018-09-28 10:19:18 -070074 sort.Sort(ModulesByName{androidMkModulesList, ctx})
Colin Crossd779da42015-12-17 18:00:23 -080075
Dan Willemsen45133ac2018-03-09 21:22:06 -080076 transMk := PathForOutput(ctx, "Android"+String(ctx.Config().productVariables.Make_suffix)+".mk")
Dan Willemsen34cc69e2015-09-23 15:26:20 -070077 if ctx.Failed() {
78 return
79 }
Dan Willemsen218f6562015-07-08 18:13:11 -070080
Dan Willemsen34cc69e2015-09-23 15:26:20 -070081 err := translateAndroidMk(ctx, transMk.String(), androidMkModulesList)
Dan Willemsen218f6562015-07-08 18:13:11 -070082 if err != nil {
83 ctx.Errorf(err.Error())
84 }
85
Colin Cross0875c522017-11-28 17:34:01 -080086 ctx.Build(pctx, BuildParams{
87 Rule: blueprint.Phony,
88 Output: transMk,
Dan Willemsen218f6562015-07-08 18:13:11 -070089 })
90}
91
Colin Cross2465c3d2018-09-28 10:19:18 -070092func translateAndroidMk(ctx SingletonContext, mkFile string, mods []blueprint.Module) error {
Dan Willemsen218f6562015-07-08 18:13:11 -070093 buf := &bytes.Buffer{}
94
Dan Willemsen97750522016-02-09 17:43:51 -080095 fmt.Fprintln(buf, "LOCAL_MODULE_MAKEFILE := $(lastword $(MAKEFILE_LIST))")
Dan Willemsen218f6562015-07-08 18:13:11 -070096
Dan Willemsen70e17fa2016-07-25 16:00:20 -070097 type_stats := make(map[string]int)
Dan Willemsen218f6562015-07-08 18:13:11 -070098 for _, mod := range mods {
99 err := translateAndroidMkModule(ctx, buf, mod)
100 if err != nil {
101 os.Remove(mkFile)
102 return err
103 }
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700104
Colin Cross2465c3d2018-09-28 10:19:18 -0700105 if amod, ok := mod.(Module); ok && ctx.PrimaryModule(amod) == amod {
106 type_stats[ctx.ModuleType(amod)] += 1
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700107 }
108 }
109
110 keys := []string{}
111 fmt.Fprintln(buf, "\nSTATS.SOONG_MODULE_TYPE :=")
112 for k := range type_stats {
113 keys = append(keys, k)
114 }
115 sort.Strings(keys)
116 for _, mod_type := range keys {
117 fmt.Fprintln(buf, "STATS.SOONG_MODULE_TYPE +=", mod_type)
118 fmt.Fprintf(buf, "STATS.SOONG_MODULE_TYPE.%s := %d\n", mod_type, type_stats[mod_type])
Dan Willemsen218f6562015-07-08 18:13:11 -0700119 }
120
121 // Don't write to the file if it hasn't changed
122 if _, err := os.Stat(mkFile); !os.IsNotExist(err) {
123 if data, err := ioutil.ReadFile(mkFile); err == nil {
124 matches := buf.Len() == len(data)
125
126 if matches {
127 for i, value := range buf.Bytes() {
128 if value != data[i] {
129 matches = false
130 break
131 }
132 }
133 }
134
135 if matches {
136 return nil
137 }
138 }
139 }
140
141 return ioutil.WriteFile(mkFile, buf.Bytes(), 0666)
142}
143
Colin Cross0875c522017-11-28 17:34:01 -0800144func translateAndroidMkModule(ctx SingletonContext, w io.Writer, mod blueprint.Module) error {
Colin Cross953d3a22018-09-05 16:23:54 -0700145 defer func() {
146 if r := recover(); r != nil {
147 panic(fmt.Errorf("%s in translateAndroidMkModule for module %s variant %s",
148 r, ctx.ModuleName(mod), ctx.ModuleSubDir(mod)))
149 }
150 }()
151
Colin Cross2465c3d2018-09-28 10:19:18 -0700152 switch x := mod.(type) {
153 case AndroidMkDataProvider:
154 return translateAndroidModule(ctx, w, mod, x)
155 case bootstrap.GoBinaryTool:
156 return translateGoBinaryModule(ctx, w, mod, x)
157 default:
Dan Willemsen218f6562015-07-08 18:13:11 -0700158 return nil
159 }
Colin Cross2465c3d2018-09-28 10:19:18 -0700160}
161
162func translateGoBinaryModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
163 goBinary bootstrap.GoBinaryTool) error {
164
165 name := ctx.ModuleName(mod)
166 fmt.Fprintln(w, ".PHONY:", name)
167 fmt.Fprintln(w, name+":", goBinary.InstallPath())
168 fmt.Fprintln(w, "")
169
170 return nil
171}
172
173func translateAndroidModule(ctx SingletonContext, w io.Writer, mod blueprint.Module,
174 provider AndroidMkDataProvider) error {
Dan Willemsen218f6562015-07-08 18:13:11 -0700175
Colin Crossce75d2c2016-10-06 16:12:58 -0700176 name := provider.BaseModuleName()
Colin Cross635c3b02016-05-18 15:37:25 -0700177 amod := mod.(Module).base()
Dan Willemsen218f6562015-07-08 18:13:11 -0700178
Dan Willemsen97750522016-02-09 17:43:51 -0800179 if !amod.Enabled() {
Colin Crossce75d2c2016-10-06 16:12:58 -0700180 return nil
181 }
182
183 if amod.commonProperties.SkipInstall {
184 return nil
Dan Willemsen218f6562015-07-08 18:13:11 -0700185 }
186
Jeff Gaston088e29e2017-11-29 16:47:17 -0800187 if !amod.commonProperties.NamespaceExportedToMake {
188 // TODO(jeffrygaston) do we want to validate that there are no modules being
189 // exported to Kati that depend on this module?
190 return nil
191 }
192
Colin Cross91825d22017-08-10 16:59:47 -0700193 data := provider.AndroidMk()
Colin Crosscc4f3e32016-11-23 15:41:09 -0800194
Colin Cross53499412017-09-07 13:20:25 -0700195 if data.Include == "" {
196 data.Include = "$(BUILD_PREBUILT)"
197 }
198
Logan Chien43d34c32017-12-20 01:17:32 +0800199 data.Required = append(data.Required, amod.commonProperties.Required...)
Colin Cross92430102017-10-09 14:59:32 -0700200
Dan Willemsen01a405a2016-06-13 17:19:03 -0700201 // Make does not understand LinuxBionic
202 if amod.Os() == LinuxBionic {
203 return nil
204 }
205
Colin Cross0f86d182017-08-10 17:07:28 -0700206 prefix := ""
207 if amod.ArchSpecific() {
208 switch amod.Os().Class {
209 case Host:
210 prefix = "HOST_"
211 case HostCross:
212 prefix = "HOST_CROSS_"
213 case Device:
214 prefix = "TARGET_"
Colin Crossa2344662016-03-24 13:14:12 -0700215
Dan Willemsen218f6562015-07-08 18:13:11 -0700216 }
217
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700218 if amod.Arch().ArchType != ctx.Config().Targets[amod.Os()][0].Arch.ArchType {
Colin Cross0f86d182017-08-10 17:07:28 -0700219 prefix = "2ND_" + prefix
220 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700221 }
222
Colin Cross0f86d182017-08-10 17:07:28 -0700223 fmt.Fprintln(&data.preamble, "\ninclude $(CLEAR_VARS)")
224 fmt.Fprintln(&data.preamble, "LOCAL_PATH :=", filepath.Dir(ctx.BlueprintFile(mod)))
225 fmt.Fprintln(&data.preamble, "LOCAL_MODULE :=", name+data.SubName)
226 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_CLASS :=", data.Class)
227 fmt.Fprintln(&data.preamble, "LOCAL_PREBUILT_MODULE_FILE :=", data.OutputFile.String())
Dan Willemsen97750522016-02-09 17:43:51 -0800228
Colin Cross92430102017-10-09 14:59:32 -0700229 if len(data.Required) > 0 {
230 fmt.Fprintln(&data.preamble, "LOCAL_REQUIRED_MODULES := "+strings.Join(data.Required, " "))
Chris Wolfe998306e2016-08-15 14:47:23 -0400231 }
232
Dan Willemsen97750522016-02-09 17:43:51 -0800233 archStr := amod.Arch().ArchType.String()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700234 host := false
235 switch amod.Os().Class {
236 case Host:
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800237 // Make cannot identify LOCAL_MODULE_HOST_ARCH:= common.
238 if archStr != "common" {
Colin Cross0f86d182017-08-10 17:07:28 -0700239 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_HOST_ARCH :=", archStr)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800240 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700241 host = true
242 case HostCross:
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800243 // Make cannot identify LOCAL_MODULE_HOST_CROSS_ARCH:= common.
244 if archStr != "common" {
Colin Cross0f86d182017-08-10 17:07:28 -0700245 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800246 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700247 host = true
248 case Device:
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800249 // Make cannot identify LOCAL_MODULE_TARGET_ARCH:= common.
250 if archStr != "common" {
Colin Cross0f86d182017-08-10 17:07:28 -0700251 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800252 }
Dan Willemsen0fda89f2016-06-01 15:25:32 -0700253
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700254 if len(amod.commonProperties.Init_rc) > 0 {
Colin Cross0f86d182017-08-10 17:07:28 -0700255 fmt.Fprintln(&data.preamble, "LOCAL_INIT_RC := ", strings.Join(amod.commonProperties.Init_rc, " "))
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700256 }
Steven Moreland57a23d22018-04-04 15:42:19 -0700257 if len(amod.commonProperties.Vintf_fragments) > 0 {
258 fmt.Fprintln(&data.preamble, "LOCAL_VINTF_FRAGMENTS := ", strings.Join(amod.commonProperties.Vintf_fragments, " "))
259 }
Colin Cross7d716ba2017-11-01 10:38:29 -0700260 if Bool(amod.commonProperties.Proprietary) {
Colin Cross0f86d182017-08-10 17:07:28 -0700261 fmt.Fprintln(&data.preamble, "LOCAL_PROPRIETARY_MODULE := true")
Dan Willemsen7ed1ae32016-12-05 16:47:50 -0800262 }
Jiyong Park2db76922017-11-08 16:03:48 +0900263 if Bool(amod.commonProperties.Vendor) || Bool(amod.commonProperties.Soc_specific) {
Colin Cross0f86d182017-08-10 17:07:28 -0700264 fmt.Fprintln(&data.preamble, "LOCAL_VENDOR_MODULE := true")
Dan Willemsenaa118f92017-04-06 12:49:58 -0700265 }
Jiyong Park2db76922017-11-08 16:03:48 +0900266 if Bool(amod.commonProperties.Device_specific) {
267 fmt.Fprintln(&data.preamble, "LOCAL_ODM_MODULE := true")
268 }
269 if Bool(amod.commonProperties.Product_specific) {
Jaekyun Seok5cfbfbb2018-01-10 19:00:15 +0900270 fmt.Fprintln(&data.preamble, "LOCAL_PRODUCT_MODULE := true")
Jiyong Park2db76922017-11-08 16:03:48 +0900271 }
Dario Freni95cf7672018-08-17 00:57:57 +0100272 if Bool(amod.commonProperties.Product_services_specific) {
Dario Frenifd05a742018-05-29 13:28:54 +0100273 fmt.Fprintln(&data.preamble, "LOCAL_PRODUCT_SERVICES_MODULE := true")
274 }
Dan Willemsenefac4a82017-07-18 19:42:09 -0700275 if amod.commonProperties.Owner != nil {
Colin Cross0f86d182017-08-10 17:07:28 -0700276 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_OWNER :=", *amod.commonProperties.Owner)
Colin Cross55708f32017-03-20 13:23:34 -0700277 }
Colin Cross5aac3622017-08-31 15:07:09 -0700278 if amod.commonProperties.Notice != nil {
Colin Crossaa768582017-09-05 15:54:27 -0700279 fmt.Fprintln(&data.preamble, "LOCAL_NOTICE_FILE :=", "$(LOCAL_PATH)/"+*amod.commonProperties.Notice)
Colin Cross5aac3622017-08-31 15:07:09 -0700280 }
Dan Willemsen97750522016-02-09 17:43:51 -0800281 }
282
Colin Crossa1ad8d12016-06-01 17:09:44 -0700283 if host {
Dan Willemsen866b5632017-09-22 12:28:24 -0700284 makeOs := amod.Os().String()
285 if amod.Os() == Linux || amod.Os() == LinuxBionic {
286 makeOs = "linux"
287 }
288 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_HOST_OS :=", makeOs)
Colin Cross0f86d182017-08-10 17:07:28 -0700289 fmt.Fprintln(&data.preamble, "LOCAL_IS_HOST_MODULE := true")
Colin Crossa1ad8d12016-06-01 17:09:44 -0700290 }
291
Colin Cross0f86d182017-08-10 17:07:28 -0700292 blueprintDir := filepath.Dir(ctx.BlueprintFile(mod))
293
294 if data.Custom != nil {
295 data.Custom(w, name, prefix, blueprintDir, data)
296 } else {
297 WriteAndroidMkData(w, data)
298 }
299
300 return nil
301}
302
303func WriteAndroidMkData(w io.Writer, data AndroidMkData) {
304 if data.Disabled {
305 return
306 }
307
308 if !data.OutputFile.Valid() {
309 return
310 }
311
312 w.Write(data.preamble.Bytes())
313
Colin Crossca860ac2016-01-04 14:34:37 -0800314 for _, extra := range data.Extra {
Colin Cross27a4b052017-08-10 16:32:23 -0700315 extra(w, data.OutputFile.Path())
Dan Willemsen97750522016-02-09 17:43:51 -0800316 }
317
Colin Cross53499412017-09-07 13:20:25 -0700318 fmt.Fprintln(w, "include "+data.Include)
Dan Willemsen218f6562015-07-08 18:13:11 -0700319}