blob: fd745075e48602916adb771252daf6de57c06ab5 [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 {
Dan Willemsen97750522016-02-09 17:43:51 -080036 AndroidMk() (AndroidMkData, error)
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 Cross91825d22017-08-10 16:59:47 -070046 Custom func(w io.Writer, name, prefix, moduleDir string)
Dan Willemsen218f6562015-07-08 18:13:11 -070047
Colin Cross27a4b052017-08-10 16:32:23 -070048 Extra []AndroidMkExtraFunc
Dan Willemsen218f6562015-07-08 18:13:11 -070049}
50
Colin Cross27a4b052017-08-10 16:32:23 -070051type AndroidMkExtraFunc func(w io.Writer, outputFile Path)
52
Dan Willemsen218f6562015-07-08 18:13:11 -070053func AndroidMkSingleton() blueprint.Singleton {
54 return &androidMkSingleton{}
55}
56
57type androidMkSingleton struct{}
58
59func (c *androidMkSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) {
Dan Willemsen174978c2016-05-11 00:27:49 -070060 config := ctx.Config().(Config)
61
62 if !config.EmbeddedInMake() {
Dan Willemsen5ba07e82015-12-11 13:51:06 -080063 return
64 }
65
Colin Cross635c3b02016-05-18 15:37:25 -070066 var androidMkModulesList []Module
Colin Cross4f6e4e62016-01-11 12:55:55 -080067
Dan Willemsen218f6562015-07-08 18:13:11 -070068 ctx.VisitAllModules(func(module blueprint.Module) {
Colin Cross635c3b02016-05-18 15:37:25 -070069 if amod, ok := module.(Module); ok {
Dan Willemsen218f6562015-07-08 18:13:11 -070070 androidMkModulesList = append(androidMkModulesList, amod)
71 }
Colin Cross4f6e4e62016-01-11 12:55:55 -080072 })
Dan Willemsen218f6562015-07-08 18:13:11 -070073
Colin Crossd779da42015-12-17 18:00:23 -080074 sort.Sort(AndroidModulesByName{androidMkModulesList, ctx})
75
Dan Willemsen174978c2016-05-11 00:27:49 -070076 transMk := PathForOutput(ctx, "Android"+proptools.String(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
86 ctx.Build(pctx, blueprint.BuildParams{
87 Rule: blueprint.Phony,
Dan Willemsen34cc69e2015-09-23 15:26:20 -070088 Outputs: []string{transMk.String()},
Dan Willemsen218f6562015-07-08 18:13:11 -070089 Optional: true,
90 })
91}
92
Colin Cross635c3b02016-05-18 15:37:25 -070093func translateAndroidMk(ctx blueprint.SingletonContext, mkFile string, mods []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
106 if ctx.PrimaryModule(mod) == mod {
107 type_stats[ctx.ModuleType(mod)] += 1
108 }
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
145func translateAndroidMkModule(ctx blueprint.SingletonContext, w io.Writer, mod blueprint.Module) error {
Dan Willemsen97750522016-02-09 17:43:51 -0800146 provider, ok := mod.(AndroidMkDataProvider)
147 if !ok {
Dan Willemsen218f6562015-07-08 18:13:11 -0700148 return nil
149 }
150
Colin Crossce75d2c2016-10-06 16:12:58 -0700151 name := provider.BaseModuleName()
Colin Cross635c3b02016-05-18 15:37:25 -0700152 amod := mod.(Module).base()
Dan Willemsen218f6562015-07-08 18:13:11 -0700153
Dan Willemsen97750522016-02-09 17:43:51 -0800154 if !amod.Enabled() {
Colin Crossce75d2c2016-10-06 16:12:58 -0700155 return nil
156 }
157
158 if amod.commonProperties.SkipInstall {
159 return nil
Dan Willemsen218f6562015-07-08 18:13:11 -0700160 }
161
Colin Cross91825d22017-08-10 16:59:47 -0700162 data := provider.AndroidMk()
Colin Crosscc4f3e32016-11-23 15:41:09 -0800163
Dan Willemsen01a405a2016-06-13 17:19:03 -0700164 // Make does not understand LinuxBionic
165 if amod.Os() == LinuxBionic {
166 return nil
167 }
168
Colin Crossa2344662016-03-24 13:14:12 -0700169 if data.SubName != "" {
Dan Albert6a047692016-07-18 17:24:47 -0700170 name += data.SubName
Colin Crossa2344662016-03-24 13:14:12 -0700171 }
172
Dan Willemsen97750522016-02-09 17:43:51 -0800173 if data.Custom != nil {
Dan Willemsen218f6562015-07-08 18:13:11 -0700174 prefix := ""
Nan Zhang280802c2017-03-29 16:24:19 -0700175 if amod.ArchSpecific() {
176 switch amod.Os().Class {
177 case Host:
178 prefix = "HOST_"
179 case HostCross:
180 prefix = "HOST_CROSS_"
181 case Device:
182 prefix = "TARGET_"
Colin Crossa1ad8d12016-06-01 17:09:44 -0700183
Nan Zhang280802c2017-03-29 16:24:19 -0700184 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700185
Nan Zhang280802c2017-03-29 16:24:19 -0700186 config := ctx.Config().(Config)
187 if amod.Arch().ArchType != config.Targets[amod.Os().Class][0].Arch.ArchType {
188 prefix = "2ND_" + prefix
189 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700190 }
191
Colin Cross91825d22017-08-10 16:59:47 -0700192 data.Custom(w, name, prefix, filepath.Dir(ctx.BlueprintFile(mod)))
193
194 return nil
Dan Willemsen218f6562015-07-08 18:13:11 -0700195 }
196
Colin Crossca860ac2016-01-04 14:34:37 -0800197 if data.Disabled {
198 return nil
199 }
200
Dan Willemsen97750522016-02-09 17:43:51 -0800201 if !data.OutputFile.Valid() {
Colin Cross91825d22017-08-10 16:59:47 -0700202 return nil
Dan Willemsen97750522016-02-09 17:43:51 -0800203 }
204
205 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
Colin Cross26302132016-05-13 12:28:46 -0700206 fmt.Fprintln(w, "LOCAL_PATH :=", filepath.Dir(ctx.BlueprintFile(mod)))
Dan Willemsen97750522016-02-09 17:43:51 -0800207 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
208 fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", data.Class)
Colin Cross26302132016-05-13 12:28:46 -0700209 fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", data.OutputFile.String())
Dan Willemsen97750522016-02-09 17:43:51 -0800210
Chris Wolfe998306e2016-08-15 14:47:23 -0400211 if len(amod.commonProperties.Required) > 0 {
212 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES := "+strings.Join(amod.commonProperties.Required, " "))
213 }
214
Dan Willemsen97750522016-02-09 17:43:51 -0800215 archStr := amod.Arch().ArchType.String()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700216 host := false
217 switch amod.Os().Class {
218 case Host:
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800219 // Make cannot identify LOCAL_MODULE_HOST_ARCH:= common.
220 if archStr != "common" {
221 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr)
222 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700223 host = true
224 case HostCross:
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800225 // Make cannot identify LOCAL_MODULE_HOST_CROSS_ARCH:= common.
226 if archStr != "common" {
227 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr)
228 }
Colin Crossa1ad8d12016-06-01 17:09:44 -0700229 host = true
230 case Device:
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800231 // Make cannot identify LOCAL_MODULE_TARGET_ARCH:= common.
232 if archStr != "common" {
233 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr)
234 }
Dan Willemsen0fda89f2016-06-01 15:25:32 -0700235
236 if len(amod.commonProperties.Logtags) > 0 {
237 fmt.Fprintln(w, "LOCAL_LOGTAGS_FILES := ", strings.Join(amod.commonProperties.Logtags, " "))
238 }
Dan Willemsen2277bcb2016-07-25 20:27:39 -0700239 if len(amod.commonProperties.Init_rc) > 0 {
240 fmt.Fprintln(w, "LOCAL_INIT_RC := ", strings.Join(amod.commonProperties.Init_rc, " "))
241 }
Dan Willemsen7ed1ae32016-12-05 16:47:50 -0800242 if amod.commonProperties.Proprietary {
243 fmt.Fprintln(w, "LOCAL_PROPRIETARY_MODULE := true")
244 }
Dan Willemsenaa118f92017-04-06 12:49:58 -0700245 if amod.commonProperties.Vendor {
246 fmt.Fprintln(w, "LOCAL_VENDOR_MODULE := true")
247 }
Dan Willemsenefac4a82017-07-18 19:42:09 -0700248 if amod.commonProperties.Owner != nil {
249 fmt.Fprintln(w, "LOCAL_MODULE_OWNER :=", *amod.commonProperties.Owner)
Colin Cross55708f32017-03-20 13:23:34 -0700250 }
Dan Willemsen97750522016-02-09 17:43:51 -0800251 }
252
Colin Crossa1ad8d12016-06-01 17:09:44 -0700253 if host {
254 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", amod.Os().String())
255 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
256 }
257
Colin Crossca860ac2016-01-04 14:34:37 -0800258 for _, extra := range data.Extra {
Colin Cross27a4b052017-08-10 16:32:23 -0700259 extra(w, data.OutputFile.Path())
Dan Willemsen97750522016-02-09 17:43:51 -0800260 }
261
262 fmt.Fprintln(w, "include $(BUILD_PREBUILT)")
263
Colin Cross91825d22017-08-10 16:59:47 -0700264 return nil
Dan Willemsen218f6562015-07-08 18:13:11 -0700265}