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" |
Colin Cross | 2465c3d | 2018-09-28 10:19:18 -0700 | [diff] [blame] | 28 | "github.com/google/blueprint/bootstrap" |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 29 | ) |
| 30 | |
| 31 | func init() { |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 32 | RegisterSingletonType("androidmk", AndroidMkSingleton) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 35 | // Deprecated: consider using AndroidMkEntriesProvider instead, especially if you're not going to |
| 36 | // use the Custom function. |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 37 | type AndroidMkDataProvider interface { |
Colin Cross | a18e9cf | 2017-08-10 17:00:19 -0700 | [diff] [blame] | 38 | AndroidMk() AndroidMkData |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 39 | BaseModuleName() string |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | type AndroidMkData struct { |
Sasha Smundak | b6d2305 | 2019-04-01 18:37:36 -0700 | [diff] [blame] | 43 | Class string |
| 44 | SubName string |
| 45 | DistFile OptionalPath |
| 46 | OutputFile OptionalPath |
| 47 | Disabled bool |
| 48 | Include string |
| 49 | Required []string |
| 50 | Host_required []string |
| 51 | Target_required []string |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 52 | |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 53 | Custom func(w io.Writer, name, prefix, moduleDir string, data AndroidMkData) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 54 | |
Colin Cross | 27a4b05 | 2017-08-10 16:32:23 -0700 | [diff] [blame] | 55 | Extra []AndroidMkExtraFunc |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 56 | |
| 57 | preamble bytes.Buffer |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Colin Cross | 27a4b05 | 2017-08-10 16:32:23 -0700 | [diff] [blame] | 60 | type AndroidMkExtraFunc func(w io.Writer, outputFile Path) |
| 61 | |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 62 | // Allows modules to customize their Android*.mk output. |
| 63 | type AndroidMkEntriesProvider interface { |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame^] | 64 | AndroidMkEntries() []AndroidMkEntries |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 65 | BaseModuleName() string |
| 66 | } |
| 67 | |
| 68 | type AndroidMkEntries struct { |
| 69 | Class string |
| 70 | SubName string |
| 71 | DistFile OptionalPath |
| 72 | OutputFile OptionalPath |
| 73 | Disabled bool |
| 74 | Include string |
| 75 | Required []string |
| 76 | Host_required []string |
| 77 | Target_required []string |
| 78 | |
| 79 | header bytes.Buffer |
| 80 | footer bytes.Buffer |
| 81 | |
Jaewoong Jung | e0dc8df | 2019-08-27 17:33:16 -0700 | [diff] [blame] | 82 | ExtraEntries []AndroidMkExtraEntriesFunc |
Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 83 | ExtraFooters []AndroidMkExtraFootersFunc |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 84 | |
| 85 | EntryMap map[string][]string |
| 86 | entryOrder []string |
| 87 | } |
| 88 | |
Jaewoong Jung | e0dc8df | 2019-08-27 17:33:16 -0700 | [diff] [blame] | 89 | type AndroidMkExtraEntriesFunc func(entries *AndroidMkEntries) |
Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 90 | type AndroidMkExtraFootersFunc func(w io.Writer, name, prefix, moduleDir string, entries *AndroidMkEntries) |
Jaewoong Jung | e0dc8df | 2019-08-27 17:33:16 -0700 | [diff] [blame] | 91 | |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 92 | func (a *AndroidMkEntries) SetString(name, value string) { |
| 93 | if _, ok := a.EntryMap[name]; !ok { |
| 94 | a.entryOrder = append(a.entryOrder, name) |
| 95 | } |
| 96 | a.EntryMap[name] = []string{value} |
| 97 | } |
| 98 | |
Jaewoong Jung | 9a1e8bd | 2019-09-04 20:17:54 -0700 | [diff] [blame] | 99 | func (a *AndroidMkEntries) SetPath(name string, path Path) { |
| 100 | if _, ok := a.EntryMap[name]; !ok { |
| 101 | a.entryOrder = append(a.entryOrder, name) |
| 102 | } |
| 103 | a.EntryMap[name] = []string{path.String()} |
| 104 | } |
| 105 | |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 106 | func (a *AndroidMkEntries) SetBoolIfTrue(name string, flag bool) { |
| 107 | if flag { |
| 108 | if _, ok := a.EntryMap[name]; !ok { |
| 109 | a.entryOrder = append(a.entryOrder, name) |
| 110 | } |
| 111 | a.EntryMap[name] = []string{"true"} |
| 112 | } |
| 113 | } |
| 114 | |
Jaewoong Jung | 9a1e8bd | 2019-09-04 20:17:54 -0700 | [diff] [blame] | 115 | func (a *AndroidMkEntries) SetBool(name string, flag bool) { |
| 116 | if _, ok := a.EntryMap[name]; !ok { |
| 117 | a.entryOrder = append(a.entryOrder, name) |
| 118 | } |
| 119 | if flag { |
| 120 | a.EntryMap[name] = []string{"true"} |
| 121 | } else { |
| 122 | a.EntryMap[name] = []string{"false"} |
| 123 | } |
| 124 | } |
| 125 | |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 126 | func (a *AndroidMkEntries) AddStrings(name string, value ...string) { |
| 127 | if len(value) == 0 { |
| 128 | return |
| 129 | } |
| 130 | if _, ok := a.EntryMap[name]; !ok { |
| 131 | a.entryOrder = append(a.entryOrder, name) |
| 132 | } |
| 133 | a.EntryMap[name] = append(a.EntryMap[name], value...) |
| 134 | } |
| 135 | |
| 136 | func (a *AndroidMkEntries) fillInEntries(config Config, bpPath string, mod blueprint.Module) { |
| 137 | a.EntryMap = make(map[string][]string) |
| 138 | amod := mod.(Module).base() |
| 139 | name := amod.BaseModuleName() |
| 140 | |
| 141 | if a.Include == "" { |
| 142 | a.Include = "$(BUILD_PREBUILT)" |
| 143 | } |
| 144 | a.Required = append(a.Required, amod.commonProperties.Required...) |
| 145 | a.Host_required = append(a.Host_required, amod.commonProperties.Host_required...) |
| 146 | a.Target_required = append(a.Target_required, amod.commonProperties.Target_required...) |
| 147 | |
| 148 | // Fill in the header part. |
| 149 | if len(amod.commonProperties.Dist.Targets) > 0 { |
| 150 | distFile := a.DistFile |
| 151 | if !distFile.Valid() { |
| 152 | distFile = a.OutputFile |
| 153 | } |
| 154 | if distFile.Valid() { |
| 155 | dest := filepath.Base(distFile.String()) |
| 156 | |
| 157 | if amod.commonProperties.Dist.Dest != nil { |
| 158 | var err error |
| 159 | if dest, err = validateSafePath(*amod.commonProperties.Dist.Dest); err != nil { |
| 160 | // This was checked in ModuleBase.GenerateBuildActions |
| 161 | panic(err) |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | if amod.commonProperties.Dist.Suffix != nil { |
| 166 | ext := filepath.Ext(dest) |
| 167 | suffix := *amod.commonProperties.Dist.Suffix |
| 168 | dest = strings.TrimSuffix(dest, ext) + suffix + ext |
| 169 | } |
| 170 | |
| 171 | if amod.commonProperties.Dist.Dir != nil { |
| 172 | var err error |
| 173 | if dest, err = validateSafePath(*amod.commonProperties.Dist.Dir, dest); err != nil { |
| 174 | // This was checked in ModuleBase.GenerateBuildActions |
| 175 | panic(err) |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | goals := strings.Join(amod.commonProperties.Dist.Targets, " ") |
| 180 | fmt.Fprintln(&a.header, ".PHONY:", goals) |
| 181 | fmt.Fprintf(&a.header, "$(call dist-for-goals,%s,%s:%s)\n", |
| 182 | goals, distFile.String(), dest) |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | fmt.Fprintln(&a.header, "\ninclude $(CLEAR_VARS)") |
| 187 | |
| 188 | // Collect make variable assignment entries. |
| 189 | a.SetString("LOCAL_PATH", filepath.Dir(bpPath)) |
| 190 | a.SetString("LOCAL_MODULE", name+a.SubName) |
| 191 | a.SetString("LOCAL_MODULE_CLASS", a.Class) |
| 192 | a.SetString("LOCAL_PREBUILT_MODULE_FILE", a.OutputFile.String()) |
| 193 | a.AddStrings("LOCAL_REQUIRED_MODULES", a.Required...) |
| 194 | a.AddStrings("LOCAL_HOST_REQUIRED_MODULES", a.Host_required...) |
| 195 | a.AddStrings("LOCAL_TARGET_REQUIRED_MODULES", a.Target_required...) |
| 196 | |
| 197 | archStr := amod.Arch().ArchType.String() |
| 198 | host := false |
| 199 | switch amod.Os().Class { |
| 200 | case Host: |
| 201 | // Make cannot identify LOCAL_MODULE_HOST_ARCH:= common. |
Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 202 | if amod.Arch().ArchType != Common { |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 203 | a.SetString("LOCAL_MODULE_HOST_ARCH", archStr) |
| 204 | } |
| 205 | host = true |
| 206 | case HostCross: |
| 207 | // Make cannot identify LOCAL_MODULE_HOST_CROSS_ARCH:= common. |
Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 208 | if amod.Arch().ArchType != Common { |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 209 | a.SetString("LOCAL_MODULE_HOST_CROSS_ARCH", archStr) |
| 210 | } |
| 211 | host = true |
| 212 | case Device: |
| 213 | // Make cannot identify LOCAL_MODULE_TARGET_ARCH:= common. |
Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 214 | if amod.Arch().ArchType != Common { |
dimitry | 1f33e40 | 2019-03-26 12:39:31 +0100 | [diff] [blame] | 215 | if amod.Target().NativeBridge { |
dimitry | 8d6dde8 | 2019-07-11 10:23:53 +0200 | [diff] [blame] | 216 | hostArchStr := amod.Target().NativeBridgeHostArchName |
dimitry | 1f33e40 | 2019-03-26 12:39:31 +0100 | [diff] [blame] | 217 | if hostArchStr != "" { |
| 218 | a.SetString("LOCAL_MODULE_TARGET_ARCH", hostArchStr) |
| 219 | } |
| 220 | } else { |
| 221 | a.SetString("LOCAL_MODULE_TARGET_ARCH", archStr) |
| 222 | } |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | a.AddStrings("LOCAL_INIT_RC", amod.commonProperties.Init_rc...) |
| 226 | a.AddStrings("LOCAL_VINTF_FRAGMENTS", amod.commonProperties.Vintf_fragments...) |
| 227 | a.SetBoolIfTrue("LOCAL_PROPRIETARY_MODULE", Bool(amod.commonProperties.Proprietary)) |
| 228 | if Bool(amod.commonProperties.Vendor) || Bool(amod.commonProperties.Soc_specific) { |
| 229 | a.SetString("LOCAL_VENDOR_MODULE", "true") |
| 230 | } |
| 231 | a.SetBoolIfTrue("LOCAL_ODM_MODULE", Bool(amod.commonProperties.Device_specific)) |
| 232 | a.SetBoolIfTrue("LOCAL_PRODUCT_MODULE", Bool(amod.commonProperties.Product_specific)) |
Justin Yun | d5f6c82 | 2019-06-25 16:47:17 +0900 | [diff] [blame] | 233 | a.SetBoolIfTrue("LOCAL_SYSTEM_EXT_MODULE", Bool(amod.commonProperties.System_ext_specific)) |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 234 | if amod.commonProperties.Owner != nil { |
| 235 | a.SetString("LOCAL_MODULE_OWNER", *amod.commonProperties.Owner) |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | if amod.noticeFile.Valid() { |
| 240 | a.SetString("LOCAL_NOTICE_FILE", amod.noticeFile.String()) |
| 241 | } |
| 242 | |
| 243 | if host { |
| 244 | makeOs := amod.Os().String() |
| 245 | if amod.Os() == Linux || amod.Os() == LinuxBionic { |
| 246 | makeOs = "linux" |
| 247 | } |
| 248 | a.SetString("LOCAL_MODULE_HOST_OS", makeOs) |
| 249 | a.SetString("LOCAL_IS_HOST_MODULE", "true") |
| 250 | } |
| 251 | |
| 252 | prefix := "" |
| 253 | if amod.ArchSpecific() { |
| 254 | switch amod.Os().Class { |
| 255 | case Host: |
| 256 | prefix = "HOST_" |
| 257 | case HostCross: |
| 258 | prefix = "HOST_CROSS_" |
| 259 | case Device: |
| 260 | prefix = "TARGET_" |
| 261 | |
| 262 | } |
| 263 | |
| 264 | if amod.Arch().ArchType != config.Targets[amod.Os()][0].Arch.ArchType { |
| 265 | prefix = "2ND_" + prefix |
| 266 | } |
| 267 | } |
Jaewoong Jung | e0dc8df | 2019-08-27 17:33:16 -0700 | [diff] [blame] | 268 | for _, extra := range a.ExtraEntries { |
| 269 | extra(a) |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | // Write to footer. |
| 273 | fmt.Fprintln(&a.footer, "include "+a.Include) |
Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 274 | blueprintDir := filepath.Dir(bpPath) |
| 275 | for _, footerFunc := range a.ExtraFooters { |
| 276 | footerFunc(&a.footer, name, prefix, blueprintDir, a) |
| 277 | } |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | func (a *AndroidMkEntries) write(w io.Writer) { |
Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 281 | if a.Disabled { |
| 282 | return |
| 283 | } |
| 284 | |
| 285 | if !a.OutputFile.Valid() { |
| 286 | return |
| 287 | } |
| 288 | |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 289 | w.Write(a.header.Bytes()) |
| 290 | for _, name := range a.entryOrder { |
| 291 | fmt.Fprintln(w, name+" := "+strings.Join(a.EntryMap[name], " ")) |
| 292 | } |
| 293 | w.Write(a.footer.Bytes()) |
| 294 | } |
| 295 | |
Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 296 | func (a *AndroidMkEntries) FooterLinesForTests() []string { |
| 297 | return strings.Split(string(a.footer.Bytes()), "\n") |
| 298 | } |
| 299 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 300 | func AndroidMkSingleton() Singleton { |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 301 | return &androidMkSingleton{} |
| 302 | } |
| 303 | |
| 304 | type androidMkSingleton struct{} |
| 305 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 306 | func (c *androidMkSingleton) GenerateBuildActions(ctx SingletonContext) { |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 307 | if !ctx.Config().EmbeddedInMake() { |
Dan Willemsen | 5ba07e8 | 2015-12-11 13:51:06 -0800 | [diff] [blame] | 308 | return |
| 309 | } |
| 310 | |
Colin Cross | 2465c3d | 2018-09-28 10:19:18 -0700 | [diff] [blame] | 311 | var androidMkModulesList []blueprint.Module |
Colin Cross | 4f6e4e6 | 2016-01-11 12:55:55 -0800 | [diff] [blame] | 312 | |
Colin Cross | 2465c3d | 2018-09-28 10:19:18 -0700 | [diff] [blame] | 313 | ctx.VisitAllModulesBlueprint(func(module blueprint.Module) { |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 314 | androidMkModulesList = append(androidMkModulesList, module) |
Colin Cross | 4f6e4e6 | 2016-01-11 12:55:55 -0800 | [diff] [blame] | 315 | }) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 316 | |
Colin Cross | 1ad8142 | 2019-01-14 12:47:35 -0800 | [diff] [blame] | 317 | sort.SliceStable(androidMkModulesList, func(i, j int) bool { |
| 318 | return ctx.ModuleName(androidMkModulesList[i]) < ctx.ModuleName(androidMkModulesList[j]) |
| 319 | }) |
Colin Cross | d779da4 | 2015-12-17 18:00:23 -0800 | [diff] [blame] | 320 | |
Dan Willemsen | 45133ac | 2018-03-09 21:22:06 -0800 | [diff] [blame] | 321 | transMk := PathForOutput(ctx, "Android"+String(ctx.Config().productVariables.Make_suffix)+".mk") |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 322 | if ctx.Failed() { |
| 323 | return |
| 324 | } |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 325 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 326 | err := translateAndroidMk(ctx, transMk.String(), androidMkModulesList) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 327 | if err != nil { |
| 328 | ctx.Errorf(err.Error()) |
| 329 | } |
| 330 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 331 | ctx.Build(pctx, BuildParams{ |
| 332 | Rule: blueprint.Phony, |
| 333 | Output: transMk, |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 334 | }) |
| 335 | } |
| 336 | |
Colin Cross | 2465c3d | 2018-09-28 10:19:18 -0700 | [diff] [blame] | 337 | func translateAndroidMk(ctx SingletonContext, mkFile string, mods []blueprint.Module) error { |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 338 | buf := &bytes.Buffer{} |
| 339 | |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 340 | fmt.Fprintln(buf, "LOCAL_MODULE_MAKEFILE := $(lastword $(MAKEFILE_LIST))") |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 341 | |
Dan Willemsen | 70e17fa | 2016-07-25 16:00:20 -0700 | [diff] [blame] | 342 | type_stats := make(map[string]int) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 343 | for _, mod := range mods { |
| 344 | err := translateAndroidMkModule(ctx, buf, mod) |
| 345 | if err != nil { |
| 346 | os.Remove(mkFile) |
| 347 | return err |
| 348 | } |
Dan Willemsen | 70e17fa | 2016-07-25 16:00:20 -0700 | [diff] [blame] | 349 | |
Colin Cross | 2465c3d | 2018-09-28 10:19:18 -0700 | [diff] [blame] | 350 | if amod, ok := mod.(Module); ok && ctx.PrimaryModule(amod) == amod { |
| 351 | type_stats[ctx.ModuleType(amod)] += 1 |
Dan Willemsen | 70e17fa | 2016-07-25 16:00:20 -0700 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | |
| 355 | keys := []string{} |
| 356 | fmt.Fprintln(buf, "\nSTATS.SOONG_MODULE_TYPE :=") |
| 357 | for k := range type_stats { |
| 358 | keys = append(keys, k) |
| 359 | } |
| 360 | sort.Strings(keys) |
| 361 | for _, mod_type := range keys { |
| 362 | fmt.Fprintln(buf, "STATS.SOONG_MODULE_TYPE +=", mod_type) |
| 363 | 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] | 364 | } |
| 365 | |
| 366 | // Don't write to the file if it hasn't changed |
| 367 | if _, err := os.Stat(mkFile); !os.IsNotExist(err) { |
| 368 | if data, err := ioutil.ReadFile(mkFile); err == nil { |
| 369 | matches := buf.Len() == len(data) |
| 370 | |
| 371 | if matches { |
| 372 | for i, value := range buf.Bytes() { |
| 373 | if value != data[i] { |
| 374 | matches = false |
| 375 | break |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | if matches { |
| 381 | return nil |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | return ioutil.WriteFile(mkFile, buf.Bytes(), 0666) |
| 387 | } |
| 388 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 389 | func translateAndroidMkModule(ctx SingletonContext, w io.Writer, mod blueprint.Module) error { |
Colin Cross | 953d3a2 | 2018-09-05 16:23:54 -0700 | [diff] [blame] | 390 | defer func() { |
| 391 | if r := recover(); r != nil { |
| 392 | panic(fmt.Errorf("%s in translateAndroidMkModule for module %s variant %s", |
| 393 | r, ctx.ModuleName(mod), ctx.ModuleSubDir(mod))) |
| 394 | } |
| 395 | }() |
| 396 | |
Colin Cross | 2465c3d | 2018-09-28 10:19:18 -0700 | [diff] [blame] | 397 | switch x := mod.(type) { |
| 398 | case AndroidMkDataProvider: |
| 399 | return translateAndroidModule(ctx, w, mod, x) |
| 400 | case bootstrap.GoBinaryTool: |
| 401 | return translateGoBinaryModule(ctx, w, mod, x) |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 402 | case AndroidMkEntriesProvider: |
| 403 | return translateAndroidMkEntriesModule(ctx, w, mod, x) |
Colin Cross | 2465c3d | 2018-09-28 10:19:18 -0700 | [diff] [blame] | 404 | default: |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 405 | return nil |
| 406 | } |
Colin Cross | 2465c3d | 2018-09-28 10:19:18 -0700 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | func translateGoBinaryModule(ctx SingletonContext, w io.Writer, mod blueprint.Module, |
| 410 | goBinary bootstrap.GoBinaryTool) error { |
| 411 | |
| 412 | name := ctx.ModuleName(mod) |
| 413 | fmt.Fprintln(w, ".PHONY:", name) |
| 414 | fmt.Fprintln(w, name+":", goBinary.InstallPath()) |
| 415 | fmt.Fprintln(w, "") |
| 416 | |
| 417 | return nil |
| 418 | } |
| 419 | |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 420 | func (data *AndroidMkData) fillInData(config Config, bpPath string, mod blueprint.Module) { |
| 421 | // Get the preamble content through AndroidMkEntries logic. |
| 422 | entries := AndroidMkEntries{ |
| 423 | Class: data.Class, |
| 424 | SubName: data.SubName, |
| 425 | DistFile: data.DistFile, |
| 426 | OutputFile: data.OutputFile, |
| 427 | Disabled: data.Disabled, |
| 428 | Include: data.Include, |
| 429 | Required: data.Required, |
| 430 | Host_required: data.Host_required, |
| 431 | Target_required: data.Target_required, |
| 432 | } |
| 433 | entries.fillInEntries(config, bpPath, mod) |
| 434 | |
| 435 | // preamble doesn't need the footer content. |
| 436 | entries.footer = bytes.Buffer{} |
| 437 | entries.write(&data.preamble) |
| 438 | |
| 439 | // copy entries back to data since it is used in Custom |
| 440 | data.Required = entries.Required |
| 441 | data.Host_required = entries.Host_required |
| 442 | data.Target_required = entries.Target_required |
| 443 | } |
| 444 | |
Colin Cross | 2465c3d | 2018-09-28 10:19:18 -0700 | [diff] [blame] | 445 | func translateAndroidModule(ctx SingletonContext, w io.Writer, mod blueprint.Module, |
| 446 | provider AndroidMkDataProvider) error { |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 447 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 448 | amod := mod.(Module).base() |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 449 | if shouldSkipAndroidMkProcessing(amod) { |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 450 | return nil |
| 451 | } |
| 452 | |
Colin Cross | 91825d2 | 2017-08-10 16:59:47 -0700 | [diff] [blame] | 453 | data := provider.AndroidMk() |
Colin Cross | 5349941 | 2017-09-07 13:20:25 -0700 | [diff] [blame] | 454 | if data.Include == "" { |
| 455 | data.Include = "$(BUILD_PREBUILT)" |
| 456 | } |
| 457 | |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 458 | data.fillInData(ctx.Config(), ctx.BlueprintFile(mod), mod) |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 459 | |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 460 | prefix := "" |
| 461 | if amod.ArchSpecific() { |
| 462 | switch amod.Os().Class { |
| 463 | case Host: |
| 464 | prefix = "HOST_" |
| 465 | case HostCross: |
| 466 | prefix = "HOST_CROSS_" |
| 467 | case Device: |
| 468 | prefix = "TARGET_" |
Colin Cross | a234466 | 2016-03-24 13:14:12 -0700 | [diff] [blame] | 469 | |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Dan Willemsen | 0ef639b | 2018-10-10 17:02:29 -0700 | [diff] [blame] | 472 | if amod.Arch().ArchType != ctx.Config().Targets[amod.Os()][0].Arch.ArchType { |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 473 | prefix = "2ND_" + prefix |
| 474 | } |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 477 | name := provider.BaseModuleName() |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 478 | blueprintDir := filepath.Dir(ctx.BlueprintFile(mod)) |
| 479 | |
| 480 | if data.Custom != nil { |
| 481 | data.Custom(w, name, prefix, blueprintDir, data) |
| 482 | } else { |
| 483 | WriteAndroidMkData(w, data) |
| 484 | } |
| 485 | |
| 486 | return nil |
| 487 | } |
| 488 | |
| 489 | func WriteAndroidMkData(w io.Writer, data AndroidMkData) { |
| 490 | if data.Disabled { |
| 491 | return |
| 492 | } |
| 493 | |
| 494 | if !data.OutputFile.Valid() { |
| 495 | return |
| 496 | } |
| 497 | |
| 498 | w.Write(data.preamble.Bytes()) |
| 499 | |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 500 | for _, extra := range data.Extra { |
Colin Cross | 27a4b05 | 2017-08-10 16:32:23 -0700 | [diff] [blame] | 501 | extra(w, data.OutputFile.Path()) |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 502 | } |
| 503 | |
Colin Cross | 5349941 | 2017-09-07 13:20:25 -0700 | [diff] [blame] | 504 | fmt.Fprintln(w, "include "+data.Include) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 505 | } |
Sasha Smundak | b6d2305 | 2019-04-01 18:37:36 -0700 | [diff] [blame] | 506 | |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 507 | func translateAndroidMkEntriesModule(ctx SingletonContext, w io.Writer, mod blueprint.Module, |
| 508 | provider AndroidMkEntriesProvider) error { |
| 509 | if shouldSkipAndroidMkProcessing(mod.(Module).base()) { |
| 510 | return nil |
Sasha Smundak | b6d2305 | 2019-04-01 18:37:36 -0700 | [diff] [blame] | 511 | } |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 512 | |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame^] | 513 | for _, entries := range provider.AndroidMkEntries() { |
| 514 | entries.fillInEntries(ctx.Config(), ctx.BlueprintFile(mod), mod) |
| 515 | entries.write(w) |
| 516 | } |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 517 | |
| 518 | return nil |
| 519 | } |
| 520 | |
| 521 | func shouldSkipAndroidMkProcessing(module *ModuleBase) bool { |
| 522 | if !module.commonProperties.NamespaceExportedToMake { |
| 523 | // TODO(jeffrygaston) do we want to validate that there are no modules being |
| 524 | // exported to Kati that depend on this module? |
| 525 | return true |
Sasha Smundak | b6d2305 | 2019-04-01 18:37:36 -0700 | [diff] [blame] | 526 | } |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 527 | |
| 528 | return !module.Enabled() || |
| 529 | module.commonProperties.SkipInstall || |
| 530 | // Make does not understand LinuxBionic |
| 531 | module.Os() == LinuxBionic |
Sasha Smundak | b6d2305 | 2019-04-01 18:37:36 -0700 | [diff] [blame] | 532 | } |