blob: 704b5605a2fb6f3677392f6377eacc75a5da15e0 [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"
28)
29
30func init() {
Colin Cross798bfce2016-10-12 14:28:16 -070031 RegisterSingletonType("androidmk", AndroidMkSingleton)
Dan Willemsen218f6562015-07-08 18:13:11 -070032}
33
34type AndroidMkDataProvider interface {
Colin Crossa18e9cf2017-08-10 17:00:19 -070035 AndroidMk() AndroidMkData
Colin Crossce75d2c2016-10-06 16:12:58 -070036 BaseModuleName() string
Dan Willemsen218f6562015-07-08 18:13:11 -070037}
38
39type AndroidMkData struct {
40 Class string
Colin Crossa2344662016-03-24 13:14:12 -070041 SubName string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070042 OutputFile OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -080043 Disabled bool
Colin Cross53499412017-09-07 13:20:25 -070044 Include string
Colin Cross92430102017-10-09 14:59:32 -070045 Required []string
Dan Willemsen218f6562015-07-08 18:13:11 -070046
Colin Cross0f86d182017-08-10 17:07:28 -070047 Custom func(w io.Writer, name, prefix, moduleDir string, data AndroidMkData)
Dan Willemsen218f6562015-07-08 18:13:11 -070048
Colin Cross27a4b052017-08-10 16:32:23 -070049 Extra []AndroidMkExtraFunc
Colin Cross0f86d182017-08-10 17:07:28 -070050
51 preamble bytes.Buffer
Dan Willemsen218f6562015-07-08 18:13:11 -070052}
53
Colin Cross27a4b052017-08-10 16:32:23 -070054type AndroidMkExtraFunc func(w io.Writer, outputFile Path)
55
Colin Cross0875c522017-11-28 17:34:01 -080056func AndroidMkSingleton() Singleton {
Dan Willemsen218f6562015-07-08 18:13:11 -070057 return &androidMkSingleton{}
58}
59
60type androidMkSingleton struct{}
61
Colin Cross0875c522017-11-28 17:34:01 -080062func (c *androidMkSingleton) GenerateBuildActions(ctx SingletonContext) {
Colin Crossaabf6792017-11-29 00:27:14 -080063 if !ctx.Config().EmbeddedInMake() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -080064 return
65 }
66
Colin Cross635c3b02016-05-18 15:37:25 -070067 var androidMkModulesList []Module
Colin Cross4f6e4e62016-01-11 12:55:55 -080068
Colin Cross0875c522017-11-28 17:34:01 -080069 ctx.VisitAllModules(func(module Module) {
70 androidMkModulesList = append(androidMkModulesList, module)
Colin Cross4f6e4e62016-01-11 12:55:55 -080071 })
Dan Willemsen218f6562015-07-08 18:13:11 -070072
Colin Crossd779da42015-12-17 18:00:23 -080073 sort.Sort(AndroidModulesByName{androidMkModulesList, ctx})
74
Colin Crossaabf6792017-11-29 00:27:14 -080075 transMk := PathForOutput(ctx, "Android"+String(ctx.Config().ProductVariables.Make_suffix)+".mk")
Dan Willemsen34cc69e2015-09-23 15:26:20 -070076 if ctx.Failed() {
77 return
78 }
Dan Willemsen218f6562015-07-08 18:13:11 -070079
Dan Willemsen34cc69e2015-09-23 15:26:20 -070080 err := translateAndroidMk(ctx, transMk.String(), androidMkModulesList)
Dan Willemsen218f6562015-07-08 18:13:11 -070081 if err != nil {
82 ctx.Errorf(err.Error())
83 }
84
Colin Cross0875c522017-11-28 17:34:01 -080085 ctx.Build(pctx, BuildParams{
86 Rule: blueprint.Phony,
87 Output: transMk,
Dan Willemsen218f6562015-07-08 18:13:11 -070088 })
89}
90
Colin Cross0875c522017-11-28 17:34:01 -080091func translateAndroidMk(ctx SingletonContext, mkFile string, mods []Module) error {
Dan Willemsen218f6562015-07-08 18:13:11 -070092 buf := &bytes.Buffer{}
93
Dan Willemsen97750522016-02-09 17:43:51 -080094 fmt.Fprintln(buf, "LOCAL_MODULE_MAKEFILE := $(lastword $(MAKEFILE_LIST))")
Dan Willemsen218f6562015-07-08 18:13:11 -070095
Dan Willemsen70e17fa2016-07-25 16:00:20 -070096 type_stats := make(map[string]int)
Dan Willemsen218f6562015-07-08 18:13:11 -070097 for _, mod := range mods {
98 err := translateAndroidMkModule(ctx, buf, mod)
99 if err != nil {
100 os.Remove(mkFile)
101 return err
102 }
Dan Willemsen70e17fa2016-07-25 16:00:20 -0700103
104 if ctx.PrimaryModule(mod) == mod {
105 type_stats[ctx.ModuleType(mod)] += 1
106 }
107 }
108
109 keys := []string{}
110 fmt.Fprintln(buf, "\nSTATS.SOONG_MODULE_TYPE :=")
111 for k := range type_stats {
112 keys = append(keys, k)
113 }
114 sort.Strings(keys)
115 for _, mod_type := range keys {
116 fmt.Fprintln(buf, "STATS.SOONG_MODULE_TYPE +=", mod_type)
117 fmt.Fprintf(buf, "STATS.SOONG_MODULE_TYPE.%s := %d\n", mod_type, type_stats[mod_type])
Dan Willemsen218f6562015-07-08 18:13:11 -0700118 }
119
120 // Don't write to the file if it hasn't changed
121 if _, err := os.Stat(mkFile); !os.IsNotExist(err) {
122 if data, err := ioutil.ReadFile(mkFile); err == nil {
123 matches := buf.Len() == len(data)
124
125 if matches {
126 for i, value := range buf.Bytes() {
127 if value != data[i] {
128 matches = false
129 break
130 }
131 }
132 }
133
134 if matches {
135 return nil
136 }
137 }
138 }
139
140 return ioutil.WriteFile(mkFile, buf.Bytes(), 0666)
141}
142
Colin Cross0875c522017-11-28 17:34:01 -0800143func translateAndroidMkModule(ctx SingletonContext, w io.Writer, mod blueprint.Module) error {
Dan Willemsen97750522016-02-09 17:43:51 -0800144 provider, ok := mod.(AndroidMkDataProvider)
145 if !ok {
Dan Willemsen218f6562015-07-08 18:13:11 -0700146 return nil
147 }
148
Colin Crossce75d2c2016-10-06 16:12:58 -0700149 name := provider.BaseModuleName()
Colin Cross635c3b02016-05-18 15:37:25 -0700150 amod := mod.(Module).base()
Dan Willemsen218f6562015-07-08 18:13:11 -0700151
Dan Willemsen97750522016-02-09 17:43:51 -0800152 if !amod.Enabled() {
Colin Crossce75d2c2016-10-06 16:12:58 -0700153 return nil
154 }
155
156 if amod.commonProperties.SkipInstall {
157 return nil
Dan Willemsen218f6562015-07-08 18:13:11 -0700158 }
159
Jeff Gaston088e29e2017-11-29 16:47:17 -0800160 if !amod.commonProperties.NamespaceExportedToMake {
161 // TODO(jeffrygaston) do we want to validate that there are no modules being
162 // exported to Kati that depend on this module?
163 return nil
164 }
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 Crossaabf6792017-11-29 00:27:14 -0800191 if amod.Arch().ArchType != ctx.Config().Targets[amod.Os().Class][0].Arch.ArchType {
Colin Cross0f86d182017-08-10 17:07:28 -0700192 prefix = "2ND_" + prefix
193 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700194 }
195
Colin Cross0f86d182017-08-10 17:07:28 -0700196 fmt.Fprintln(&data.preamble, "\ninclude $(CLEAR_VARS)")
197 fmt.Fprintln(&data.preamble, "LOCAL_PATH :=", filepath.Dir(ctx.BlueprintFile(mod)))
198 fmt.Fprintln(&data.preamble, "LOCAL_MODULE :=", name+data.SubName)
199 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_CLASS :=", data.Class)
200 fmt.Fprintln(&data.preamble, "LOCAL_PREBUILT_MODULE_FILE :=", data.OutputFile.String())
Dan Willemsen97750522016-02-09 17:43:51 -0800201
Colin Cross92430102017-10-09 14:59:32 -0700202 if len(data.Required) > 0 {
203 fmt.Fprintln(&data.preamble, "LOCAL_REQUIRED_MODULES := "+strings.Join(data.Required, " "))
Chris Wolfe998306e2016-08-15 14:47:23 -0400204 }
205
Dan Willemsen97750522016-02-09 17:43:51 -0800206 archStr := amod.Arch().ArchType.String()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700207 host := false
208 switch amod.Os().Class {
209 case Host:
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800210 // Make cannot identify LOCAL_MODULE_HOST_ARCH:= common.
211 if archStr != "common" {
Colin Cross0f86d182017-08-10 17:07:28 -0700212 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_HOST_ARCH :=", archStr)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800213 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700214 host = true
215 case HostCross:
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800216 // Make cannot identify LOCAL_MODULE_HOST_CROSS_ARCH:= common.
217 if archStr != "common" {
Colin Cross0f86d182017-08-10 17:07:28 -0700218 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800219 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700220 host = true
221 case Device:
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800222 // Make cannot identify LOCAL_MODULE_TARGET_ARCH:= common.
223 if archStr != "common" {
Colin Cross0f86d182017-08-10 17:07:28 -0700224 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800225 }
Dan Willemsen0fda89f2016-06-01 15:25:32 -0700226
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700227 if len(amod.commonProperties.Init_rc) > 0 {
Colin Cross0f86d182017-08-10 17:07:28 -0700228 fmt.Fprintln(&data.preamble, "LOCAL_INIT_RC := ", strings.Join(amod.commonProperties.Init_rc, " "))
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700229 }
Colin Cross7d716ba2017-11-01 10:38:29 -0700230 if Bool(amod.commonProperties.Proprietary) {
Colin Cross0f86d182017-08-10 17:07:28 -0700231 fmt.Fprintln(&data.preamble, "LOCAL_PROPRIETARY_MODULE := true")
Dan Willemsen7ed1ae32016-12-05 16:47:50 -0800232 }
Jiyong Park2db76922017-11-08 16:03:48 +0900233 if Bool(amod.commonProperties.Vendor) || Bool(amod.commonProperties.Soc_specific) {
Colin Cross0f86d182017-08-10 17:07:28 -0700234 fmt.Fprintln(&data.preamble, "LOCAL_VENDOR_MODULE := true")
Dan Willemsenaa118f92017-04-06 12:49:58 -0700235 }
Jiyong Park2db76922017-11-08 16:03:48 +0900236 if Bool(amod.commonProperties.Device_specific) {
237 fmt.Fprintln(&data.preamble, "LOCAL_ODM_MODULE := true")
238 }
239 if Bool(amod.commonProperties.Product_specific) {
240 fmt.Fprintln(&data.preamble, "LOCAL_OEM_MODULE := true")
241 }
Dan Willemsenefac4a82017-07-18 19:42:09 -0700242 if amod.commonProperties.Owner != nil {
Colin Cross0f86d182017-08-10 17:07:28 -0700243 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_OWNER :=", *amod.commonProperties.Owner)
Colin Cross55708f32017-03-20 13:23:34 -0700244 }
Colin Cross5aac3622017-08-31 15:07:09 -0700245 if amod.commonProperties.Notice != nil {
Colin Crossaa768582017-09-05 15:54:27 -0700246 fmt.Fprintln(&data.preamble, "LOCAL_NOTICE_FILE :=", "$(LOCAL_PATH)/"+*amod.commonProperties.Notice)
Colin Cross5aac3622017-08-31 15:07:09 -0700247 }
Dan Willemsen97750522016-02-09 17:43:51 -0800248 }
249
Colin Crossa1ad8d12016-06-01 17:09:44 -0700250 if host {
Dan Willemsen866b5632017-09-22 12:28:24 -0700251 makeOs := amod.Os().String()
252 if amod.Os() == Linux || amod.Os() == LinuxBionic {
253 makeOs = "linux"
254 }
255 fmt.Fprintln(&data.preamble, "LOCAL_MODULE_HOST_OS :=", makeOs)
Colin Cross0f86d182017-08-10 17:07:28 -0700256 fmt.Fprintln(&data.preamble, "LOCAL_IS_HOST_MODULE := true")
Colin Crossa1ad8d12016-06-01 17:09:44 -0700257 }
258
Colin Cross0f86d182017-08-10 17:07:28 -0700259 blueprintDir := filepath.Dir(ctx.BlueprintFile(mod))
260
261 if data.Custom != nil {
262 data.Custom(w, name, prefix, blueprintDir, data)
263 } else {
264 WriteAndroidMkData(w, data)
265 }
266
267 return nil
268}
269
270func WriteAndroidMkData(w io.Writer, data AndroidMkData) {
271 if data.Disabled {
272 return
273 }
274
275 if !data.OutputFile.Valid() {
276 return
277 }
278
279 w.Write(data.preamble.Bytes())
280
Colin Crossca860ac2016-01-04 14:34:37 -0800281 for _, extra := range data.Extra {
Colin Cross27a4b052017-08-10 16:32:23 -0700282 extra(w, data.OutputFile.Path())
Dan Willemsen97750522016-02-09 17:43:51 -0800283 }
284
Colin Cross53499412017-09-07 13:20:25 -0700285 fmt.Fprintln(w, "include "+data.Include)
Dan Willemsen218f6562015-07-08 18:13:11 -0700286}