blob: 2cae1c3bb615e3bb6a9660c382351b0484bca54f [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"
Dan Willemsen174978c2016-05-11 00:27:49 -070028 "github.com/google/blueprint/proptools"
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
Dan Willemsen218f6562015-07-08 18:13:11 -070057func AndroidMkSingleton() blueprint.Singleton {
58 return &androidMkSingleton{}
59}
60
61type androidMkSingleton struct{}
62
63func (c *androidMkSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) {
Dan Willemsen174978c2016-05-11 00:27:49 -070064 config := ctx.Config().(Config)
65
66 if !config.EmbeddedInMake() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -080067 return
68 }
69
Colin Cross635c3b02016-05-18 15:37:25 -070070 var androidMkModulesList []Module
Colin Cross4f6e4e62016-01-11 12:55:55 -080071
Dan Willemsen218f6562015-07-08 18:13:11 -070072 ctx.VisitAllModules(func(module blueprint.Module) {
Colin Cross635c3b02016-05-18 15:37:25 -070073 if amod, ok := module.(Module); ok {
Dan Willemsen218f6562015-07-08 18:13:11 -070074 androidMkModulesList = append(androidMkModulesList, amod)
75 }
Colin Cross4f6e4e62016-01-11 12:55:55 -080076 })
Dan Willemsen218f6562015-07-08 18:13:11 -070077
Colin Crossd779da42015-12-17 18:00:23 -080078 sort.Sort(AndroidModulesByName{androidMkModulesList, ctx})
79
Dan Willemsen174978c2016-05-11 00:27:49 -070080 transMk := PathForOutput(ctx, "Android"+proptools.String(config.ProductVariables.Make_suffix)+".mk")
Dan Willemsen34cc69e2015-09-23 15:26:20 -070081 if ctx.Failed() {
82 return
83 }
Dan Willemsen218f6562015-07-08 18:13:11 -070084
Dan Willemsen34cc69e2015-09-23 15:26:20 -070085 err := translateAndroidMk(ctx, transMk.String(), androidMkModulesList)
Dan Willemsen218f6562015-07-08 18:13:11 -070086 if err != nil {
87 ctx.Errorf(err.Error())
88 }
89
90 ctx.Build(pctx, blueprint.BuildParams{
91 Rule: blueprint.Phony,
Dan Willemsen34cc69e2015-09-23 15:26:20 -070092 Outputs: []string{transMk.String()},
Dan Willemsen218f6562015-07-08 18:13:11 -070093 Optional: true,
94 })
95}
96
Colin Cross635c3b02016-05-18 15:37:25 -070097func translateAndroidMk(ctx blueprint.SingletonContext, mkFile string, mods []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
110 if ctx.PrimaryModule(mod) == mod {
111 type_stats[ctx.ModuleType(mod)] += 1
112 }
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
149func translateAndroidMkModule(ctx blueprint.SingletonContext, w io.Writer, mod blueprint.Module) error {
Dan Willemsen97750522016-02-09 17:43:51 -0800150 provider, ok := mod.(AndroidMkDataProvider)
151 if !ok {
Dan Willemsen218f6562015-07-08 18:13:11 -0700152 return nil
153 }
154
Colin Crossce75d2c2016-10-06 16:12:58 -0700155 name := provider.BaseModuleName()
Colin Cross635c3b02016-05-18 15:37:25 -0700156 amod := mod.(Module).base()
Dan Willemsen218f6562015-07-08 18:13:11 -0700157
Dan Willemsen97750522016-02-09 17:43:51 -0800158 if !amod.Enabled() {
Colin Crossce75d2c2016-10-06 16:12:58 -0700159 return nil
160 }
161
162 if amod.commonProperties.SkipInstall {
163 return nil
Dan Willemsen218f6562015-07-08 18:13:11 -0700164 }
165
Colin Cross91825d22017-08-10 16:59:47 -0700166 data := provider.AndroidMk()
Colin Crosscc4f3e32016-11-23 15:41:09 -0800167
Colin Cross53499412017-09-07 13:20:25 -0700168 if data.Include == "" {
169 data.Include = "$(BUILD_PREBUILT)"
170 }
171
Colin Cross92430102017-10-09 14:59:32 -0700172 data.Required = amod.commonProperties.Required
173
Dan Willemsen01a405a2016-06-13 17:19:03 -0700174 // Make does not understand LinuxBionic
175 if amod.Os() == LinuxBionic {
176 return nil
177 }
178
Colin Cross0f86d182017-08-10 17:07:28 -0700179 prefix := ""
180 if amod.ArchSpecific() {
181 switch amod.Os().Class {
182 case Host:
183 prefix = "HOST_"
184 case HostCross:
185 prefix = "HOST_CROSS_"
186 case Device:
187 prefix = "TARGET_"
Colin Crossa2344662016-03-24 13:14:12 -0700188
Dan Willemsen218f6562015-07-08 18:13:11 -0700189 }
190
Colin Cross0f86d182017-08-10 17:07:28 -0700191 config := ctx.Config().(Config)
192 if amod.Arch().ArchType != config.Targets[amod.Os().Class][0].Arch.ArchType {
193 prefix = "2ND_" + prefix
194 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700195 }
196
Colin Cross0f86d182017-08-10 17:07:28 -0700197 fmt.Fprintln(&data.preamble, "\ninclude $(CLEAR_VARS)")
198 fmt.Fprintln(&data.preamble, "LOCAL_PATH :=", filepath.Dir(ctx.BlueprintFile(mod)))
199 fmt.Fprintln(&data.preamble, "LOCAL_MODULE :=", name+data.SubName)
200 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_CLASS :=", data.Class)
201 fmt.Fprintln(&data.preamble, "LOCAL_PREBUILT_MODULE_FILE :=", data.OutputFile.String())
Dan Willemsen97750522016-02-09 17:43:51 -0800202
Colin Cross92430102017-10-09 14:59:32 -0700203 if len(data.Required) > 0 {
204 fmt.Fprintln(&data.preamble, "LOCAL_REQUIRED_MODULES := "+strings.Join(data.Required, " "))
Chris Wolfe998306e2016-08-15 14:47:23 -0400205 }
206
Dan Willemsen97750522016-02-09 17:43:51 -0800207 archStr := amod.Arch().ArchType.String()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700208 host := false
209 switch amod.Os().Class {
210 case Host:
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800211 // Make cannot identify LOCAL_MODULE_HOST_ARCH:= common.
212 if archStr != "common" {
Colin Cross0f86d182017-08-10 17:07:28 -0700213 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_HOST_ARCH :=", archStr)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800214 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700215 host = true
216 case HostCross:
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800217 // Make cannot identify LOCAL_MODULE_HOST_CROSS_ARCH:= common.
218 if archStr != "common" {
Colin Cross0f86d182017-08-10 17:07:28 -0700219 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800220 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700221 host = true
222 case Device:
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800223 // Make cannot identify LOCAL_MODULE_TARGET_ARCH:= common.
224 if archStr != "common" {
Colin Cross0f86d182017-08-10 17:07:28 -0700225 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800226 }
Dan Willemsen0fda89f2016-06-01 15:25:32 -0700227
228 if len(amod.commonProperties.Logtags) > 0 {
Colin Cross0f86d182017-08-10 17:07:28 -0700229 fmt.Fprintln(&data.preamble, "LOCAL_LOGTAGS_FILES := ", strings.Join(amod.commonProperties.Logtags, " "))
Dan Willemsen0fda89f2016-06-01 15:25:32 -0700230 }
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700231 if len(amod.commonProperties.Init_rc) > 0 {
Colin Cross0f86d182017-08-10 17:07:28 -0700232 fmt.Fprintln(&data.preamble, "LOCAL_INIT_RC := ", strings.Join(amod.commonProperties.Init_rc, " "))
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700233 }
Colin Cross7d716ba2017-11-01 10:38:29 -0700234 if Bool(amod.commonProperties.Proprietary) {
Colin Cross0f86d182017-08-10 17:07:28 -0700235 fmt.Fprintln(&data.preamble, "LOCAL_PROPRIETARY_MODULE := true")
Dan Willemsen7ed1ae32016-12-05 16:47:50 -0800236 }
Colin Cross7d716ba2017-11-01 10:38:29 -0700237 if Bool(amod.commonProperties.Vendor) {
Colin Cross0f86d182017-08-10 17:07:28 -0700238 fmt.Fprintln(&data.preamble, "LOCAL_VENDOR_MODULE := true")
Dan Willemsenaa118f92017-04-06 12:49:58 -0700239 }
Dan Willemsenefac4a82017-07-18 19:42:09 -0700240 if amod.commonProperties.Owner != nil {
Colin Cross0f86d182017-08-10 17:07:28 -0700241 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_OWNER :=", *amod.commonProperties.Owner)
Colin Cross55708f32017-03-20 13:23:34 -0700242 }
Colin Cross5aac3622017-08-31 15:07:09 -0700243 if amod.commonProperties.Notice != nil {
Colin Crossaa768582017-09-05 15:54:27 -0700244 fmt.Fprintln(&data.preamble, "LOCAL_NOTICE_FILE :=", "$(LOCAL_PATH)/"+*amod.commonProperties.Notice)
Colin Cross5aac3622017-08-31 15:07:09 -0700245 }
Dan Willemsen97750522016-02-09 17:43:51 -0800246 }
247
Colin Crossa1ad8d12016-06-01 17:09:44 -0700248 if host {
Dan Willemsen866b5632017-09-22 12:28:24 -0700249 makeOs := amod.Os().String()
250 if amod.Os() == Linux || amod.Os() == LinuxBionic {
251 makeOs = "linux"
252 }
253 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_HOST_OS :=", makeOs)
Colin Cross0f86d182017-08-10 17:07:28 -0700254 fmt.Fprintln(&data.preamble, "LOCAL_IS_HOST_MODULE := true")
Colin Crossa1ad8d12016-06-01 17:09:44 -0700255 }
256
Colin Cross0f86d182017-08-10 17:07:28 -0700257 blueprintDir := filepath.Dir(ctx.BlueprintFile(mod))
258
259 if data.Custom != nil {
260 data.Custom(w, name, prefix, blueprintDir, data)
261 } else {
262 WriteAndroidMkData(w, data)
263 }
264
265 return nil
266}
267
268func WriteAndroidMkData(w io.Writer, data AndroidMkData) {
269 if data.Disabled {
270 return
271 }
272
273 if !data.OutputFile.Valid() {
274 return
275 }
276
277 w.Write(data.preamble.Bytes())
278
Colin Crossca860ac2016-01-04 14:34:37 -0800279 for _, extra := range data.Extra {
Colin Cross27a4b052017-08-10 16:32:23 -0700280 extra(w, data.OutputFile.Path())
Dan Willemsen97750522016-02-09 17:43:51 -0800281 }
282
Colin Cross53499412017-09-07 13:20:25 -0700283 fmt.Fprintln(w, "include "+data.Include)
Dan Willemsen218f6562015-07-08 18:13:11 -0700284}