Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 1 | // 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 Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 15 | package android |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 16 | |
| 17 | import ( |
| 18 | "bytes" |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 19 | "fmt" |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 20 | "io" |
| 21 | "io/ioutil" |
| 22 | "os" |
| 23 | "path/filepath" |
| 24 | "sort" |
Dan Willemsen | 0fda89f | 2016-06-01 15:25:32 -0700 | [diff] [blame] | 25 | "strings" |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 26 | |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 27 | "github.com/google/blueprint" |
| 28 | ) |
| 29 | |
| 30 | func init() { |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 31 | RegisterSingletonType("androidmk", AndroidMkSingleton) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | type AndroidMkDataProvider interface { |
Colin Cross | a18e9cf | 2017-08-10 17:00:19 -0700 | [diff] [blame] | 35 | AndroidMk() AndroidMkData |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 36 | BaseModuleName() string |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | type AndroidMkData struct { |
| 40 | Class string |
Colin Cross | a234466 | 2016-03-24 13:14:12 -0700 | [diff] [blame] | 41 | SubName string |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 42 | OutputFile OptionalPath |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 43 | Disabled bool |
Colin Cross | 5349941 | 2017-09-07 13:20:25 -0700 | [diff] [blame] | 44 | Include string |
Colin Cross | 9243010 | 2017-10-09 14:59:32 -0700 | [diff] [blame] | 45 | Required []string |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 46 | |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 47 | Custom func(w io.Writer, name, prefix, moduleDir string, data AndroidMkData) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 48 | |
Colin Cross | 27a4b05 | 2017-08-10 16:32:23 -0700 | [diff] [blame] | 49 | Extra []AndroidMkExtraFunc |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 50 | |
| 51 | preamble bytes.Buffer |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Colin Cross | 27a4b05 | 2017-08-10 16:32:23 -0700 | [diff] [blame] | 54 | type AndroidMkExtraFunc func(w io.Writer, outputFile Path) |
| 55 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame^] | 56 | func AndroidMkSingleton() Singleton { |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 57 | return &androidMkSingleton{} |
| 58 | } |
| 59 | |
| 60 | type androidMkSingleton struct{} |
| 61 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame^] | 62 | func (c *androidMkSingleton) GenerateBuildActions(ctx SingletonContext) { |
Dan Willemsen | 174978c | 2016-05-11 00:27:49 -0700 | [diff] [blame] | 63 | config := ctx.Config().(Config) |
| 64 | |
| 65 | if !config.EmbeddedInMake() { |
Dan Willemsen | 5ba07e8 | 2015-12-11 13:51:06 -0800 | [diff] [blame] | 66 | return |
| 67 | } |
| 68 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 69 | var androidMkModulesList []Module |
Colin Cross | 4f6e4e6 | 2016-01-11 12:55:55 -0800 | [diff] [blame] | 70 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame^] | 71 | ctx.VisitAllModules(func(module Module) { |
| 72 | androidMkModulesList = append(androidMkModulesList, module) |
Colin Cross | 4f6e4e6 | 2016-01-11 12:55:55 -0800 | [diff] [blame] | 73 | }) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 74 | |
Colin Cross | d779da4 | 2015-12-17 18:00:23 -0800 | [diff] [blame] | 75 | sort.Sort(AndroidModulesByName{androidMkModulesList, ctx}) |
| 76 | |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 77 | transMk := PathForOutput(ctx, "Android"+String(config.ProductVariables.Make_suffix)+".mk") |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 78 | if ctx.Failed() { |
| 79 | return |
| 80 | } |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 81 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 82 | err := translateAndroidMk(ctx, transMk.String(), androidMkModulesList) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 83 | if err != nil { |
| 84 | ctx.Errorf(err.Error()) |
| 85 | } |
| 86 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame^] | 87 | ctx.Build(pctx, BuildParams{ |
| 88 | Rule: blueprint.Phony, |
| 89 | Output: transMk, |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 90 | }) |
| 91 | } |
| 92 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame^] | 93 | func translateAndroidMk(ctx SingletonContext, mkFile string, mods []Module) error { |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 94 | buf := &bytes.Buffer{} |
| 95 | |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 96 | fmt.Fprintln(buf, "LOCAL_MODULE_MAKEFILE := $(lastword $(MAKEFILE_LIST))") |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 97 | |
Dan Willemsen | 70e17fa | 2016-07-25 16:00:20 -0700 | [diff] [blame] | 98 | type_stats := make(map[string]int) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 99 | for _, mod := range mods { |
| 100 | err := translateAndroidMkModule(ctx, buf, mod) |
| 101 | if err != nil { |
| 102 | os.Remove(mkFile) |
| 103 | return err |
| 104 | } |
Dan Willemsen | 70e17fa | 2016-07-25 16:00:20 -0700 | [diff] [blame] | 105 | |
| 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 Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 120 | } |
| 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 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame^] | 145 | func translateAndroidMkModule(ctx SingletonContext, w io.Writer, mod blueprint.Module) error { |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 146 | provider, ok := mod.(AndroidMkDataProvider) |
| 147 | if !ok { |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 148 | return nil |
| 149 | } |
| 150 | |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 151 | name := provider.BaseModuleName() |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 152 | amod := mod.(Module).base() |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 153 | |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 154 | if !amod.Enabled() { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 155 | return nil |
| 156 | } |
| 157 | |
| 158 | if amod.commonProperties.SkipInstall { |
| 159 | return nil |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Colin Cross | 91825d2 | 2017-08-10 16:59:47 -0700 | [diff] [blame] | 162 | data := provider.AndroidMk() |
Colin Cross | cc4f3e3 | 2016-11-23 15:41:09 -0800 | [diff] [blame] | 163 | |
Colin Cross | 5349941 | 2017-09-07 13:20:25 -0700 | [diff] [blame] | 164 | if data.Include == "" { |
| 165 | data.Include = "$(BUILD_PREBUILT)" |
| 166 | } |
| 167 | |
Colin Cross | 9243010 | 2017-10-09 14:59:32 -0700 | [diff] [blame] | 168 | data.Required = amod.commonProperties.Required |
| 169 | |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 170 | // Make does not understand LinuxBionic |
| 171 | if amod.Os() == LinuxBionic { |
| 172 | return nil |
| 173 | } |
| 174 | |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 175 | prefix := "" |
| 176 | if amod.ArchSpecific() { |
| 177 | switch amod.Os().Class { |
| 178 | case Host: |
| 179 | prefix = "HOST_" |
| 180 | case HostCross: |
| 181 | prefix = "HOST_CROSS_" |
| 182 | case Device: |
| 183 | prefix = "TARGET_" |
Colin Cross | a234466 | 2016-03-24 13:14:12 -0700 | [diff] [blame] | 184 | |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 187 | config := ctx.Config().(Config) |
| 188 | if amod.Arch().ArchType != config.Targets[amod.Os().Class][0].Arch.ArchType { |
| 189 | prefix = "2ND_" + prefix |
| 190 | } |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 193 | fmt.Fprintln(&data.preamble, "\ninclude $(CLEAR_VARS)") |
| 194 | fmt.Fprintln(&data.preamble, "LOCAL_PATH :=", filepath.Dir(ctx.BlueprintFile(mod))) |
| 195 | fmt.Fprintln(&data.preamble, "LOCAL_MODULE :=", name+data.SubName) |
| 196 | fmt.Fprintln(&data.preamble, "LOCAL_MODULE_CLASS :=", data.Class) |
| 197 | fmt.Fprintln(&data.preamble, "LOCAL_PREBUILT_MODULE_FILE :=", data.OutputFile.String()) |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 198 | |
Colin Cross | 9243010 | 2017-10-09 14:59:32 -0700 | [diff] [blame] | 199 | if len(data.Required) > 0 { |
| 200 | fmt.Fprintln(&data.preamble, "LOCAL_REQUIRED_MODULES := "+strings.Join(data.Required, " ")) |
Chris Wolfe | 998306e | 2016-08-15 14:47:23 -0400 | [diff] [blame] | 201 | } |
| 202 | |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 203 | archStr := amod.Arch().ArchType.String() |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 204 | host := false |
| 205 | switch amod.Os().Class { |
| 206 | case Host: |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 207 | // Make cannot identify LOCAL_MODULE_HOST_ARCH:= common. |
| 208 | if archStr != "common" { |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 209 | fmt.Fprintln(&data.preamble, "LOCAL_MODULE_HOST_ARCH :=", archStr) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 210 | } |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 211 | host = true |
| 212 | case HostCross: |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 213 | // Make cannot identify LOCAL_MODULE_HOST_CROSS_ARCH:= common. |
| 214 | if archStr != "common" { |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 215 | fmt.Fprintln(&data.preamble, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 216 | } |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 217 | host = true |
| 218 | case Device: |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 219 | // Make cannot identify LOCAL_MODULE_TARGET_ARCH:= common. |
| 220 | if archStr != "common" { |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 221 | fmt.Fprintln(&data.preamble, "LOCAL_MODULE_TARGET_ARCH :=", archStr) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 222 | } |
Dan Willemsen | 0fda89f | 2016-06-01 15:25:32 -0700 | [diff] [blame] | 223 | |
| 224 | if len(amod.commonProperties.Logtags) > 0 { |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 225 | fmt.Fprintln(&data.preamble, "LOCAL_LOGTAGS_FILES := ", strings.Join(amod.commonProperties.Logtags, " ")) |
Dan Willemsen | 0fda89f | 2016-06-01 15:25:32 -0700 | [diff] [blame] | 226 | } |
Dan Willemsen | 2277bcb | 2016-07-25 20:27:39 -0700 | [diff] [blame] | 227 | if len(amod.commonProperties.Init_rc) > 0 { |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 228 | fmt.Fprintln(&data.preamble, "LOCAL_INIT_RC := ", strings.Join(amod.commonProperties.Init_rc, " ")) |
Dan Willemsen | 2277bcb | 2016-07-25 20:27:39 -0700 | [diff] [blame] | 229 | } |
Colin Cross | 7d716ba | 2017-11-01 10:38:29 -0700 | [diff] [blame] | 230 | if Bool(amod.commonProperties.Proprietary) { |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 231 | fmt.Fprintln(&data.preamble, "LOCAL_PROPRIETARY_MODULE := true") |
Dan Willemsen | 7ed1ae3 | 2016-12-05 16:47:50 -0800 | [diff] [blame] | 232 | } |
Colin Cross | 7d716ba | 2017-11-01 10:38:29 -0700 | [diff] [blame] | 233 | if Bool(amod.commonProperties.Vendor) { |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 234 | fmt.Fprintln(&data.preamble, "LOCAL_VENDOR_MODULE := true") |
Dan Willemsen | aa118f9 | 2017-04-06 12:49:58 -0700 | [diff] [blame] | 235 | } |
Dan Willemsen | efac4a8 | 2017-07-18 19:42:09 -0700 | [diff] [blame] | 236 | if amod.commonProperties.Owner != nil { |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 237 | fmt.Fprintln(&data.preamble, "LOCAL_MODULE_OWNER :=", *amod.commonProperties.Owner) |
Colin Cross | 55708f3 | 2017-03-20 13:23:34 -0700 | [diff] [blame] | 238 | } |
Colin Cross | 5aac362 | 2017-08-31 15:07:09 -0700 | [diff] [blame] | 239 | if amod.commonProperties.Notice != nil { |
Colin Cross | aa76858 | 2017-09-05 15:54:27 -0700 | [diff] [blame] | 240 | fmt.Fprintln(&data.preamble, "LOCAL_NOTICE_FILE :=", "$(LOCAL_PATH)/"+*amod.commonProperties.Notice) |
Colin Cross | 5aac362 | 2017-08-31 15:07:09 -0700 | [diff] [blame] | 241 | } |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 242 | } |
| 243 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 244 | if host { |
Dan Willemsen | 866b563 | 2017-09-22 12:28:24 -0700 | [diff] [blame] | 245 | makeOs := amod.Os().String() |
| 246 | if amod.Os() == Linux || amod.Os() == LinuxBionic { |
| 247 | makeOs = "linux" |
| 248 | } |
| 249 | fmt.Fprintln(&data.preamble, "LOCAL_MODULE_HOST_OS :=", makeOs) |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 250 | fmt.Fprintln(&data.preamble, "LOCAL_IS_HOST_MODULE := true") |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 253 | blueprintDir := filepath.Dir(ctx.BlueprintFile(mod)) |
| 254 | |
| 255 | if data.Custom != nil { |
| 256 | data.Custom(w, name, prefix, blueprintDir, data) |
| 257 | } else { |
| 258 | WriteAndroidMkData(w, data) |
| 259 | } |
| 260 | |
| 261 | return nil |
| 262 | } |
| 263 | |
| 264 | func WriteAndroidMkData(w io.Writer, data AndroidMkData) { |
| 265 | if data.Disabled { |
| 266 | return |
| 267 | } |
| 268 | |
| 269 | if !data.OutputFile.Valid() { |
| 270 | return |
| 271 | } |
| 272 | |
| 273 | w.Write(data.preamble.Bytes()) |
| 274 | |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 275 | for _, extra := range data.Extra { |
Colin Cross | 27a4b05 | 2017-08-10 16:32:23 -0700 | [diff] [blame] | 276 | extra(w, data.OutputFile.Path()) |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 277 | } |
| 278 | |
Colin Cross | 5349941 | 2017-09-07 13:20:25 -0700 | [diff] [blame] | 279 | fmt.Fprintln(w, "include "+data.Include) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 280 | } |