blob: 46e7fcf20695b487de66d2e15cfda1838b9ed5ab [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
Dan Willemsen218f6562015-07-08 18:13:11 -070045
Colin Cross0f86d182017-08-10 17:07:28 -070046 Custom func(w io.Writer, name, prefix, moduleDir string, data AndroidMkData)
Dan Willemsen218f6562015-07-08 18:13:11 -070047
Colin Cross27a4b052017-08-10 16:32:23 -070048 Extra []AndroidMkExtraFunc
Colin Cross0f86d182017-08-10 17:07:28 -070049
50 preamble bytes.Buffer
Dan Willemsen218f6562015-07-08 18:13:11 -070051}
52
Colin Cross27a4b052017-08-10 16:32:23 -070053type AndroidMkExtraFunc func(w io.Writer, outputFile Path)
54
Dan Willemsen218f6562015-07-08 18:13:11 -070055func AndroidMkSingleton() blueprint.Singleton {
56 return &androidMkSingleton{}
57}
58
59type androidMkSingleton struct{}
60
61func (c *androidMkSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) {
Dan Willemsen174978c2016-05-11 00:27:49 -070062 config := ctx.Config().(Config)
63
64 if !config.EmbeddedInMake() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -080065 return
66 }
67
Colin Cross635c3b02016-05-18 15:37:25 -070068 var androidMkModulesList []Module
Colin Cross4f6e4e62016-01-11 12:55:55 -080069
Dan Willemsen218f6562015-07-08 18:13:11 -070070 ctx.VisitAllModules(func(module blueprint.Module) {
Colin Cross635c3b02016-05-18 15:37:25 -070071 if amod, ok := module.(Module); ok {
Dan Willemsen218f6562015-07-08 18:13:11 -070072 androidMkModulesList = append(androidMkModulesList, amod)
73 }
Colin Cross4f6e4e62016-01-11 12:55:55 -080074 })
Dan Willemsen218f6562015-07-08 18:13:11 -070075
Colin Crossd779da42015-12-17 18:00:23 -080076 sort.Sort(AndroidModulesByName{androidMkModulesList, ctx})
77
Dan Willemsen174978c2016-05-11 00:27:49 -070078 transMk := PathForOutput(ctx, "Android"+proptools.String(config.ProductVariables.Make_suffix)+".mk")
Dan Willemsen34cc69e2015-09-23 15:26:20 -070079 if ctx.Failed() {
80 return
81 }
Dan Willemsen218f6562015-07-08 18:13:11 -070082
Dan Willemsen34cc69e2015-09-23 15:26:20 -070083 err := translateAndroidMk(ctx, transMk.String(), androidMkModulesList)
Dan Willemsen218f6562015-07-08 18:13:11 -070084 if err != nil {
85 ctx.Errorf(err.Error())
86 }
87
88 ctx.Build(pctx, blueprint.BuildParams{
89 Rule: blueprint.Phony,
Dan Willemsen34cc69e2015-09-23 15:26:20 -070090 Outputs: []string{transMk.String()},
Dan Willemsen218f6562015-07-08 18:13:11 -070091 Optional: true,
92 })
93}
94
Colin Cross635c3b02016-05-18 15:37:25 -070095func translateAndroidMk(ctx blueprint.SingletonContext, mkFile string, mods []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
108 if ctx.PrimaryModule(mod) == mod {
109 type_stats[ctx.ModuleType(mod)] += 1
110 }
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
147func translateAndroidMkModule(ctx blueprint.SingletonContext, w io.Writer, mod blueprint.Module) error {
Dan Willemsen97750522016-02-09 17:43:51 -0800148 provider, ok := mod.(AndroidMkDataProvider)
149 if !ok {
Dan Willemsen218f6562015-07-08 18:13:11 -0700150 return nil
151 }
152
Colin Crossce75d2c2016-10-06 16:12:58 -0700153 name := provider.BaseModuleName()
Colin Cross635c3b02016-05-18 15:37:25 -0700154 amod := mod.(Module).base()
Dan Willemsen218f6562015-07-08 18:13:11 -0700155
Dan Willemsen97750522016-02-09 17:43:51 -0800156 if !amod.Enabled() {
Colin Crossce75d2c2016-10-06 16:12:58 -0700157 return nil
158 }
159
160 if amod.commonProperties.SkipInstall {
161 return nil
Dan Willemsen218f6562015-07-08 18:13:11 -0700162 }
163
Colin Cross91825d22017-08-10 16:59:47 -0700164 data := provider.AndroidMk()
Colin Crosscc4f3e32016-11-23 15:41:09 -0800165
Dan Willemsen01a405a2016-06-13 17:19:03 -0700166 // Make does not understand LinuxBionic
167 if amod.Os() == LinuxBionic {
168 return nil
169 }
170
Colin Cross0f86d182017-08-10 17:07:28 -0700171 prefix := ""
172 if amod.ArchSpecific() {
173 switch amod.Os().Class {
174 case Host:
175 prefix = "HOST_"
176 case HostCross:
177 prefix = "HOST_CROSS_"
178 case Device:
179 prefix = "TARGET_"
Colin Crossa2344662016-03-24 13:14:12 -0700180
Dan Willemsen218f6562015-07-08 18:13:11 -0700181 }
182
Colin Cross0f86d182017-08-10 17:07:28 -0700183 config := ctx.Config().(Config)
184 if amod.Arch().ArchType != config.Targets[amod.Os().Class][0].Arch.ArchType {
185 prefix = "2ND_" + prefix
186 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700187 }
188
Colin Cross0f86d182017-08-10 17:07:28 -0700189 fmt.Fprintln(&data.preamble, "\ninclude $(CLEAR_VARS)")
190 fmt.Fprintln(&data.preamble, "LOCAL_PATH :=", filepath.Dir(ctx.BlueprintFile(mod)))
191 fmt.Fprintln(&data.preamble, "LOCAL_MODULE :=", name+data.SubName)
192 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_CLASS :=", data.Class)
193 fmt.Fprintln(&data.preamble, "LOCAL_PREBUILT_MODULE_FILE :=", data.OutputFile.String())
Dan Willemsen97750522016-02-09 17:43:51 -0800194
Chris Wolfe998306e2016-08-15 14:47:23 -0400195 if len(amod.commonProperties.Required) > 0 {
Colin Cross0f86d182017-08-10 17:07:28 -0700196 fmt.Fprintln(&data.preamble, "LOCAL_REQUIRED_MODULES := "+strings.Join(amod.commonProperties.Required, " "))
Chris Wolfe998306e2016-08-15 14:47:23 -0400197 }
198
Dan Willemsen97750522016-02-09 17:43:51 -0800199 archStr := amod.Arch().ArchType.String()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700200 host := false
201 switch amod.Os().Class {
202 case Host:
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800203 // Make cannot identify LOCAL_MODULE_HOST_ARCH:= common.
204 if archStr != "common" {
Colin Cross0f86d182017-08-10 17:07:28 -0700205 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_HOST_ARCH :=", archStr)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800206 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700207 host = true
208 case HostCross:
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800209 // Make cannot identify LOCAL_MODULE_HOST_CROSS_ARCH:= common.
210 if archStr != "common" {
Colin Cross0f86d182017-08-10 17:07:28 -0700211 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800212 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700213 host = true
214 case Device:
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800215 // Make cannot identify LOCAL_MODULE_TARGET_ARCH:= common.
216 if archStr != "common" {
Colin Cross0f86d182017-08-10 17:07:28 -0700217 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800218 }
Dan Willemsen0fda89f2016-06-01 15:25:32 -0700219
220 if len(amod.commonProperties.Logtags) > 0 {
Colin Cross0f86d182017-08-10 17:07:28 -0700221 fmt.Fprintln(&data.preamble, "LOCAL_LOGTAGS_FILES := ", strings.Join(amod.commonProperties.Logtags, " "))
Dan Willemsen0fda89f2016-06-01 15:25:32 -0700222 }
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700223 if len(amod.commonProperties.Init_rc) > 0 {
Colin Cross0f86d182017-08-10 17:07:28 -0700224 fmt.Fprintln(&data.preamble, "LOCAL_INIT_RC := ", strings.Join(amod.commonProperties.Init_rc, " "))
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700225 }
Dan Willemsen7ed1ae32016-12-05 16:47:50 -0800226 if amod.commonProperties.Proprietary {
Colin Cross0f86d182017-08-10 17:07:28 -0700227 fmt.Fprintln(&data.preamble, "LOCAL_PROPRIETARY_MODULE := true")
Dan Willemsen7ed1ae32016-12-05 16:47:50 -0800228 }
Dan Willemsenaa118f92017-04-06 12:49:58 -0700229 if amod.commonProperties.Vendor {
Colin Cross0f86d182017-08-10 17:07:28 -0700230 fmt.Fprintln(&data.preamble, "LOCAL_VENDOR_MODULE := true")
Dan Willemsenaa118f92017-04-06 12:49:58 -0700231 }
Dan Willemsenefac4a82017-07-18 19:42:09 -0700232 if amod.commonProperties.Owner != nil {
Colin Cross0f86d182017-08-10 17:07:28 -0700233 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_OWNER :=", *amod.commonProperties.Owner)
Colin Cross55708f32017-03-20 13:23:34 -0700234 }
Dan Willemsen97750522016-02-09 17:43:51 -0800235 }
236
Colin Crossa1ad8d12016-06-01 17:09:44 -0700237 if host {
Colin Cross0f86d182017-08-10 17:07:28 -0700238 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_HOST_OS :=", amod.Os().String())
239 fmt.Fprintln(&data.preamble, "LOCAL_IS_HOST_MODULE := true")
Colin Crossa1ad8d12016-06-01 17:09:44 -0700240 }
241
Colin Cross0f86d182017-08-10 17:07:28 -0700242 blueprintDir := filepath.Dir(ctx.BlueprintFile(mod))
243
244 if data.Custom != nil {
245 data.Custom(w, name, prefix, blueprintDir, data)
246 } else {
247 WriteAndroidMkData(w, data)
248 }
249
250 return nil
251}
252
253func WriteAndroidMkData(w io.Writer, data AndroidMkData) {
254 if data.Disabled {
255 return
256 }
257
258 if !data.OutputFile.Valid() {
259 return
260 }
261
262 w.Write(data.preamble.Bytes())
263
Colin Crossca860ac2016-01-04 14:34:37 -0800264 for _, extra := range data.Extra {
Colin Cross27a4b052017-08-10 16:32:23 -0700265 extra(w, data.OutputFile.Path())
Dan Willemsen97750522016-02-09 17:43:51 -0800266 }
267
268 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
Dan Willemsen218f6562015-07-08 18:13:11 -0700269}