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