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() { |
Paul Duffin | 8c3fec4 | 2020-03-04 20:15:08 +0000 | [diff] [blame] | 32 | RegisterAndroidMkBuildComponents(InitRegistrationContext) |
| 33 | } |
| 34 | |
| 35 | func RegisterAndroidMkBuildComponents(ctx RegistrationContext) { |
| 36 | ctx.RegisterSingletonType("androidmk", AndroidMkSingleton) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 39 | // Deprecated: consider using AndroidMkEntriesProvider instead, especially if you're not going to |
| 40 | // use the Custom function. |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 41 | type AndroidMkDataProvider interface { |
Colin Cross | a18e9cf | 2017-08-10 17:00:19 -0700 | [diff] [blame] | 42 | AndroidMk() AndroidMkData |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 43 | BaseModuleName() string |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | type AndroidMkData struct { |
Sasha Smundak | b6d2305 | 2019-04-01 18:37:36 -0700 | [diff] [blame] | 47 | Class string |
| 48 | SubName string |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 49 | DistFiles TaggedDistFiles |
Sasha Smundak | b6d2305 | 2019-04-01 18:37:36 -0700 | [diff] [blame] | 50 | OutputFile OptionalPath |
| 51 | Disabled bool |
| 52 | Include string |
| 53 | Required []string |
| 54 | Host_required []string |
| 55 | Target_required []string |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 56 | |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 57 | Custom func(w io.Writer, name, prefix, moduleDir string, data AndroidMkData) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 58 | |
Colin Cross | 27a4b05 | 2017-08-10 16:32:23 -0700 | [diff] [blame] | 59 | Extra []AndroidMkExtraFunc |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 60 | |
Jooyung Han | 2ed99d0 | 2020-06-24 23:26:26 +0900 | [diff] [blame] | 61 | Entries AndroidMkEntries |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Colin Cross | 27a4b05 | 2017-08-10 16:32:23 -0700 | [diff] [blame] | 64 | type AndroidMkExtraFunc func(w io.Writer, outputFile Path) |
| 65 | |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 66 | // Allows modules to customize their Android*.mk output. |
| 67 | type AndroidMkEntriesProvider interface { |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 68 | AndroidMkEntries() []AndroidMkEntries |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 69 | BaseModuleName() string |
| 70 | } |
| 71 | |
| 72 | type AndroidMkEntries struct { |
| 73 | Class string |
| 74 | SubName string |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 75 | DistFiles TaggedDistFiles |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 76 | OutputFile OptionalPath |
| 77 | Disabled bool |
| 78 | Include string |
| 79 | Required []string |
| 80 | Host_required []string |
| 81 | Target_required []string |
| 82 | |
| 83 | header bytes.Buffer |
| 84 | footer bytes.Buffer |
| 85 | |
Jaewoong Jung | e0dc8df | 2019-08-27 17:33:16 -0700 | [diff] [blame] | 86 | ExtraEntries []AndroidMkExtraEntriesFunc |
Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 87 | ExtraFooters []AndroidMkExtraFootersFunc |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 88 | |
| 89 | EntryMap map[string][]string |
| 90 | entryOrder []string |
| 91 | } |
| 92 | |
Jaewoong Jung | e0dc8df | 2019-08-27 17:33:16 -0700 | [diff] [blame] | 93 | type AndroidMkExtraEntriesFunc func(entries *AndroidMkEntries) |
Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 94 | type AndroidMkExtraFootersFunc func(w io.Writer, name, prefix, moduleDir string, entries *AndroidMkEntries) |
Jaewoong Jung | e0dc8df | 2019-08-27 17:33:16 -0700 | [diff] [blame] | 95 | |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 96 | func (a *AndroidMkEntries) SetString(name, value string) { |
| 97 | if _, ok := a.EntryMap[name]; !ok { |
| 98 | a.entryOrder = append(a.entryOrder, name) |
| 99 | } |
| 100 | a.EntryMap[name] = []string{value} |
| 101 | } |
| 102 | |
Jaewoong Jung | 9a1e8bd | 2019-09-04 20:17:54 -0700 | [diff] [blame] | 103 | func (a *AndroidMkEntries) SetPath(name string, path Path) { |
| 104 | if _, ok := a.EntryMap[name]; !ok { |
| 105 | a.entryOrder = append(a.entryOrder, name) |
| 106 | } |
| 107 | a.EntryMap[name] = []string{path.String()} |
| 108 | } |
| 109 | |
Colin Cross | c0efd1d | 2020-07-03 11:56:24 -0700 | [diff] [blame] | 110 | func (a *AndroidMkEntries) SetOptionalPath(name string, path OptionalPath) { |
| 111 | if path.Valid() { |
| 112 | a.SetPath(name, path.Path()) |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | func (a *AndroidMkEntries) AddPath(name string, path Path) { |
| 117 | if _, ok := a.EntryMap[name]; !ok { |
| 118 | a.entryOrder = append(a.entryOrder, name) |
| 119 | } |
| 120 | a.EntryMap[name] = append(a.EntryMap[name], path.String()) |
| 121 | } |
| 122 | |
| 123 | func (a *AndroidMkEntries) AddOptionalPath(name string, path OptionalPath) { |
| 124 | if path.Valid() { |
| 125 | a.AddPath(name, path.Path()) |
| 126 | } |
| 127 | } |
| 128 | |
Colin Cross | 08dca38 | 2020-07-21 20:31:17 -0700 | [diff] [blame] | 129 | func (a *AndroidMkEntries) SetPaths(name string, paths Paths) { |
| 130 | if _, ok := a.EntryMap[name]; !ok { |
| 131 | a.entryOrder = append(a.entryOrder, name) |
| 132 | } |
| 133 | a.EntryMap[name] = paths.Strings() |
| 134 | } |
| 135 | |
| 136 | func (a *AndroidMkEntries) SetOptionalPaths(name string, paths Paths) { |
| 137 | if len(paths) > 0 { |
| 138 | a.SetPaths(name, paths) |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | func (a *AndroidMkEntries) AddPaths(name string, paths Paths) { |
| 143 | if _, ok := a.EntryMap[name]; !ok { |
| 144 | a.entryOrder = append(a.entryOrder, name) |
| 145 | } |
| 146 | a.EntryMap[name] = append(a.EntryMap[name], paths.Strings()...) |
| 147 | } |
| 148 | |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 149 | func (a *AndroidMkEntries) SetBoolIfTrue(name string, flag bool) { |
| 150 | if flag { |
| 151 | if _, ok := a.EntryMap[name]; !ok { |
| 152 | a.entryOrder = append(a.entryOrder, name) |
| 153 | } |
| 154 | a.EntryMap[name] = []string{"true"} |
| 155 | } |
| 156 | } |
| 157 | |
Jaewoong Jung | 9a1e8bd | 2019-09-04 20:17:54 -0700 | [diff] [blame] | 158 | func (a *AndroidMkEntries) SetBool(name string, flag bool) { |
| 159 | if _, ok := a.EntryMap[name]; !ok { |
| 160 | a.entryOrder = append(a.entryOrder, name) |
| 161 | } |
| 162 | if flag { |
| 163 | a.EntryMap[name] = []string{"true"} |
| 164 | } else { |
| 165 | a.EntryMap[name] = []string{"false"} |
| 166 | } |
| 167 | } |
| 168 | |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 169 | func (a *AndroidMkEntries) AddStrings(name string, value ...string) { |
| 170 | if len(value) == 0 { |
| 171 | return |
| 172 | } |
| 173 | if _, ok := a.EntryMap[name]; !ok { |
| 174 | a.entryOrder = append(a.entryOrder, name) |
| 175 | } |
| 176 | a.EntryMap[name] = append(a.EntryMap[name], value...) |
| 177 | } |
| 178 | |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 179 | // Compute the list of Make strings to declare phone goals and dist-for-goals |
| 180 | // calls from the module's dist and dists properties. |
| 181 | func (a *AndroidMkEntries) GetDistForGoals(mod blueprint.Module) []string { |
| 182 | amod := mod.(Module).base() |
| 183 | name := amod.BaseModuleName() |
| 184 | |
| 185 | var ret []string |
Jingwen Chen | 7b27ca7 | 2020-07-24 09:13:49 +0000 | [diff] [blame] | 186 | var availableTaggedDists TaggedDistFiles |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 187 | |
Jingwen Chen | 8481186 | 2020-07-21 11:32:19 +0000 | [diff] [blame] | 188 | if a.DistFiles != nil { |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 189 | availableTaggedDists = a.DistFiles |
| 190 | } else if a.OutputFile.Valid() { |
| 191 | availableTaggedDists = MakeDefaultDistFiles(a.OutputFile.Path()) |
Jingwen Chen | 7b27ca7 | 2020-07-24 09:13:49 +0000 | [diff] [blame] | 192 | } else { |
| 193 | // Nothing dist-able for this module. |
| 194 | return nil |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | // Iterate over this module's dist structs, merged from the dist and dists properties. |
| 198 | for _, dist := range amod.Dists() { |
| 199 | // Get the list of goals this dist should be enabled for. e.g. sdk, droidcore |
| 200 | goals := strings.Join(dist.Targets, " ") |
| 201 | |
| 202 | // Get the tag representing the output files to be dist'd. e.g. ".jar", ".proguard_map" |
| 203 | var tag string |
| 204 | if dist.Tag == nil { |
| 205 | // If the dist struct does not specify a tag, use the default output files tag. |
| 206 | tag = "" |
| 207 | } else { |
| 208 | tag = *dist.Tag |
| 209 | } |
| 210 | |
| 211 | // Get the paths of the output files to be dist'd, represented by the tag. |
| 212 | // Can be an empty list. |
| 213 | tagPaths := availableTaggedDists[tag] |
| 214 | if len(tagPaths) == 0 { |
| 215 | // Nothing to dist for this tag, continue to the next dist. |
| 216 | continue |
| 217 | } |
| 218 | |
| 219 | if len(tagPaths) > 1 && (dist.Dest != nil || dist.Suffix != nil) { |
| 220 | errorMessage := "Cannot apply dest/suffix for more than one dist " + |
| 221 | "file for %s goals in module %s. The list of dist files, " + |
| 222 | "which should have a single element, is:\n%s" |
| 223 | panic(fmt.Errorf(errorMessage, goals, name, tagPaths)) |
| 224 | } |
| 225 | |
| 226 | ret = append(ret, fmt.Sprintf(".PHONY: %s\n", goals)) |
| 227 | |
| 228 | // Create dist-for-goals calls for each path in the dist'd files. |
| 229 | for _, path := range tagPaths { |
| 230 | // It's possible that the Path is nil from errant modules. Be defensive here. |
| 231 | if path == nil { |
| 232 | tagName := "default" // for error message readability |
| 233 | if dist.Tag != nil { |
| 234 | tagName = *dist.Tag |
| 235 | } |
| 236 | panic(fmt.Errorf("Dist file should not be nil for the %s tag in %s", tagName, name)) |
| 237 | } |
| 238 | |
| 239 | dest := filepath.Base(path.String()) |
| 240 | |
| 241 | if dist.Dest != nil { |
| 242 | var err error |
| 243 | if dest, err = validateSafePath(*dist.Dest); err != nil { |
| 244 | // This was checked in ModuleBase.GenerateBuildActions |
| 245 | panic(err) |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | if dist.Suffix != nil { |
| 250 | ext := filepath.Ext(dest) |
| 251 | suffix := *dist.Suffix |
| 252 | dest = strings.TrimSuffix(dest, ext) + suffix + ext |
| 253 | } |
| 254 | |
| 255 | if dist.Dir != nil { |
| 256 | var err error |
| 257 | if dest, err = validateSafePath(*dist.Dir, dest); err != nil { |
| 258 | // This was checked in ModuleBase.GenerateBuildActions |
| 259 | panic(err) |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | ret = append( |
| 264 | ret, |
| 265 | fmt.Sprintf("$(call dist-for-goals,%s,%s:%s)\n", goals, path.String(), dest)) |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | return ret |
| 270 | } |
| 271 | |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 272 | func (a *AndroidMkEntries) fillInEntries(config Config, bpPath string, mod blueprint.Module) { |
| 273 | a.EntryMap = make(map[string][]string) |
| 274 | amod := mod.(Module).base() |
| 275 | name := amod.BaseModuleName() |
| 276 | |
| 277 | if a.Include == "" { |
| 278 | a.Include = "$(BUILD_PREBUILT)" |
| 279 | } |
| 280 | a.Required = append(a.Required, amod.commonProperties.Required...) |
| 281 | a.Host_required = append(a.Host_required, amod.commonProperties.Host_required...) |
| 282 | a.Target_required = append(a.Target_required, amod.commonProperties.Target_required...) |
| 283 | |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 284 | for _, distString := range a.GetDistForGoals(mod) { |
| 285 | fmt.Fprintf(&a.header, distString) |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | fmt.Fprintln(&a.header, "\ninclude $(CLEAR_VARS)") |
| 289 | |
| 290 | // Collect make variable assignment entries. |
| 291 | a.SetString("LOCAL_PATH", filepath.Dir(bpPath)) |
| 292 | a.SetString("LOCAL_MODULE", name+a.SubName) |
| 293 | a.SetString("LOCAL_MODULE_CLASS", a.Class) |
| 294 | a.SetString("LOCAL_PREBUILT_MODULE_FILE", a.OutputFile.String()) |
| 295 | a.AddStrings("LOCAL_REQUIRED_MODULES", a.Required...) |
| 296 | a.AddStrings("LOCAL_HOST_REQUIRED_MODULES", a.Host_required...) |
| 297 | a.AddStrings("LOCAL_TARGET_REQUIRED_MODULES", a.Target_required...) |
| 298 | |
Jiyong Park | 89e850a | 2020-04-07 16:37:39 +0900 | [diff] [blame] | 299 | if am, ok := mod.(ApexModule); ok { |
| 300 | a.SetBoolIfTrue("LOCAL_NOT_AVAILABLE_FOR_PLATFORM", am.NotAvailableForPlatform()) |
| 301 | } |
| 302 | |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 303 | archStr := amod.Arch().ArchType.String() |
| 304 | host := false |
| 305 | switch amod.Os().Class { |
| 306 | case Host: |
| 307 | // Make cannot identify LOCAL_MODULE_HOST_ARCH:= common. |
Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 308 | if amod.Arch().ArchType != Common { |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 309 | a.SetString("LOCAL_MODULE_HOST_ARCH", archStr) |
| 310 | } |
| 311 | host = true |
| 312 | case HostCross: |
| 313 | // Make cannot identify LOCAL_MODULE_HOST_CROSS_ARCH:= common. |
Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 314 | if amod.Arch().ArchType != Common { |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 315 | a.SetString("LOCAL_MODULE_HOST_CROSS_ARCH", archStr) |
| 316 | } |
| 317 | host = true |
| 318 | case Device: |
| 319 | // Make cannot identify LOCAL_MODULE_TARGET_ARCH:= common. |
Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 320 | if amod.Arch().ArchType != Common { |
dimitry | 1f33e40 | 2019-03-26 12:39:31 +0100 | [diff] [blame] | 321 | if amod.Target().NativeBridge { |
dimitry | 8d6dde8 | 2019-07-11 10:23:53 +0200 | [diff] [blame] | 322 | hostArchStr := amod.Target().NativeBridgeHostArchName |
dimitry | 1f33e40 | 2019-03-26 12:39:31 +0100 | [diff] [blame] | 323 | if hostArchStr != "" { |
| 324 | a.SetString("LOCAL_MODULE_TARGET_ARCH", hostArchStr) |
| 325 | } |
| 326 | } else { |
| 327 | a.SetString("LOCAL_MODULE_TARGET_ARCH", archStr) |
| 328 | } |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | a.AddStrings("LOCAL_INIT_RC", amod.commonProperties.Init_rc...) |
| 332 | a.AddStrings("LOCAL_VINTF_FRAGMENTS", amod.commonProperties.Vintf_fragments...) |
| 333 | a.SetBoolIfTrue("LOCAL_PROPRIETARY_MODULE", Bool(amod.commonProperties.Proprietary)) |
| 334 | if Bool(amod.commonProperties.Vendor) || Bool(amod.commonProperties.Soc_specific) { |
| 335 | a.SetString("LOCAL_VENDOR_MODULE", "true") |
| 336 | } |
| 337 | a.SetBoolIfTrue("LOCAL_ODM_MODULE", Bool(amod.commonProperties.Device_specific)) |
| 338 | a.SetBoolIfTrue("LOCAL_PRODUCT_MODULE", Bool(amod.commonProperties.Product_specific)) |
Justin Yun | d5f6c82 | 2019-06-25 16:47:17 +0900 | [diff] [blame] | 339 | a.SetBoolIfTrue("LOCAL_SYSTEM_EXT_MODULE", Bool(amod.commonProperties.System_ext_specific)) |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 340 | if amod.commonProperties.Owner != nil { |
| 341 | a.SetString("LOCAL_MODULE_OWNER", *amod.commonProperties.Owner) |
| 342 | } |
| 343 | } |
| 344 | |
Bob Badour | a75b057 | 2020-02-18 20:21:55 -0800 | [diff] [blame] | 345 | if len(amod.noticeFiles) > 0 { |
| 346 | a.SetString("LOCAL_NOTICE_FILE", strings.Join(amod.noticeFiles.Strings(), " ")) |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | if host { |
| 350 | makeOs := amod.Os().String() |
| 351 | if amod.Os() == Linux || amod.Os() == LinuxBionic { |
| 352 | makeOs = "linux" |
| 353 | } |
| 354 | a.SetString("LOCAL_MODULE_HOST_OS", makeOs) |
| 355 | a.SetString("LOCAL_IS_HOST_MODULE", "true") |
| 356 | } |
| 357 | |
| 358 | prefix := "" |
| 359 | if amod.ArchSpecific() { |
| 360 | switch amod.Os().Class { |
| 361 | case Host: |
| 362 | prefix = "HOST_" |
| 363 | case HostCross: |
| 364 | prefix = "HOST_CROSS_" |
| 365 | case Device: |
| 366 | prefix = "TARGET_" |
| 367 | |
| 368 | } |
| 369 | |
| 370 | if amod.Arch().ArchType != config.Targets[amod.Os()][0].Arch.ArchType { |
| 371 | prefix = "2ND_" + prefix |
| 372 | } |
| 373 | } |
Jaewoong Jung | e0dc8df | 2019-08-27 17:33:16 -0700 | [diff] [blame] | 374 | for _, extra := range a.ExtraEntries { |
| 375 | extra(a) |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | // Write to footer. |
| 379 | fmt.Fprintln(&a.footer, "include "+a.Include) |
Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 380 | blueprintDir := filepath.Dir(bpPath) |
| 381 | for _, footerFunc := range a.ExtraFooters { |
| 382 | footerFunc(&a.footer, name, prefix, blueprintDir, a) |
| 383 | } |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | func (a *AndroidMkEntries) write(w io.Writer) { |
Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 387 | if a.Disabled { |
| 388 | return |
| 389 | } |
| 390 | |
| 391 | if !a.OutputFile.Valid() { |
| 392 | return |
| 393 | } |
| 394 | |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 395 | w.Write(a.header.Bytes()) |
| 396 | for _, name := range a.entryOrder { |
| 397 | fmt.Fprintln(w, name+" := "+strings.Join(a.EntryMap[name], " ")) |
| 398 | } |
| 399 | w.Write(a.footer.Bytes()) |
| 400 | } |
| 401 | |
Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 402 | func (a *AndroidMkEntries) FooterLinesForTests() []string { |
| 403 | return strings.Split(string(a.footer.Bytes()), "\n") |
| 404 | } |
| 405 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 406 | func AndroidMkSingleton() Singleton { |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 407 | return &androidMkSingleton{} |
| 408 | } |
| 409 | |
| 410 | type androidMkSingleton struct{} |
| 411 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 412 | func (c *androidMkSingleton) GenerateBuildActions(ctx SingletonContext) { |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 413 | if !ctx.Config().EmbeddedInMake() { |
Dan Willemsen | 5ba07e8 | 2015-12-11 13:51:06 -0800 | [diff] [blame] | 414 | return |
| 415 | } |
| 416 | |
Colin Cross | 2465c3d | 2018-09-28 10:19:18 -0700 | [diff] [blame] | 417 | var androidMkModulesList []blueprint.Module |
Colin Cross | 4f6e4e6 | 2016-01-11 12:55:55 -0800 | [diff] [blame] | 418 | |
Colin Cross | 2465c3d | 2018-09-28 10:19:18 -0700 | [diff] [blame] | 419 | ctx.VisitAllModulesBlueprint(func(module blueprint.Module) { |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 420 | androidMkModulesList = append(androidMkModulesList, module) |
Colin Cross | 4f6e4e6 | 2016-01-11 12:55:55 -0800 | [diff] [blame] | 421 | }) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 422 | |
Colin Cross | 1ad8142 | 2019-01-14 12:47:35 -0800 | [diff] [blame] | 423 | sort.SliceStable(androidMkModulesList, func(i, j int) bool { |
| 424 | return ctx.ModuleName(androidMkModulesList[i]) < ctx.ModuleName(androidMkModulesList[j]) |
| 425 | }) |
Colin Cross | d779da4 | 2015-12-17 18:00:23 -0800 | [diff] [blame] | 426 | |
Dan Willemsen | 45133ac | 2018-03-09 21:22:06 -0800 | [diff] [blame] | 427 | transMk := PathForOutput(ctx, "Android"+String(ctx.Config().productVariables.Make_suffix)+".mk") |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 428 | if ctx.Failed() { |
| 429 | return |
| 430 | } |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 431 | |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 432 | err := translateAndroidMk(ctx, absolutePath(transMk.String()), androidMkModulesList) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 433 | if err != nil { |
| 434 | ctx.Errorf(err.Error()) |
| 435 | } |
| 436 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 437 | ctx.Build(pctx, BuildParams{ |
| 438 | Rule: blueprint.Phony, |
| 439 | Output: transMk, |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 440 | }) |
| 441 | } |
| 442 | |
Colin Cross | 2465c3d | 2018-09-28 10:19:18 -0700 | [diff] [blame] | 443 | func translateAndroidMk(ctx SingletonContext, mkFile string, mods []blueprint.Module) error { |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 444 | buf := &bytes.Buffer{} |
| 445 | |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 446 | fmt.Fprintln(buf, "LOCAL_MODULE_MAKEFILE := $(lastword $(MAKEFILE_LIST))") |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 447 | |
Dan Willemsen | 70e17fa | 2016-07-25 16:00:20 -0700 | [diff] [blame] | 448 | type_stats := make(map[string]int) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 449 | for _, mod := range mods { |
| 450 | err := translateAndroidMkModule(ctx, buf, mod) |
| 451 | if err != nil { |
| 452 | os.Remove(mkFile) |
| 453 | return err |
| 454 | } |
Dan Willemsen | 70e17fa | 2016-07-25 16:00:20 -0700 | [diff] [blame] | 455 | |
Colin Cross | 2465c3d | 2018-09-28 10:19:18 -0700 | [diff] [blame] | 456 | if amod, ok := mod.(Module); ok && ctx.PrimaryModule(amod) == amod { |
| 457 | type_stats[ctx.ModuleType(amod)] += 1 |
Dan Willemsen | 70e17fa | 2016-07-25 16:00:20 -0700 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | |
| 461 | keys := []string{} |
| 462 | fmt.Fprintln(buf, "\nSTATS.SOONG_MODULE_TYPE :=") |
| 463 | for k := range type_stats { |
| 464 | keys = append(keys, k) |
| 465 | } |
| 466 | sort.Strings(keys) |
| 467 | for _, mod_type := range keys { |
| 468 | fmt.Fprintln(buf, "STATS.SOONG_MODULE_TYPE +=", mod_type) |
| 469 | 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] | 470 | } |
| 471 | |
| 472 | // Don't write to the file if it hasn't changed |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 473 | if _, err := os.Stat(absolutePath(mkFile)); !os.IsNotExist(err) { |
| 474 | if data, err := ioutil.ReadFile(absolutePath(mkFile)); err == nil { |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 475 | matches := buf.Len() == len(data) |
| 476 | |
| 477 | if matches { |
| 478 | for i, value := range buf.Bytes() { |
| 479 | if value != data[i] { |
| 480 | matches = false |
| 481 | break |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | if matches { |
| 487 | return nil |
| 488 | } |
| 489 | } |
| 490 | } |
| 491 | |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 492 | return ioutil.WriteFile(absolutePath(mkFile), buf.Bytes(), 0666) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 493 | } |
| 494 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 495 | func translateAndroidMkModule(ctx SingletonContext, w io.Writer, mod blueprint.Module) error { |
Colin Cross | 953d3a2 | 2018-09-05 16:23:54 -0700 | [diff] [blame] | 496 | defer func() { |
| 497 | if r := recover(); r != nil { |
| 498 | panic(fmt.Errorf("%s in translateAndroidMkModule for module %s variant %s", |
| 499 | r, ctx.ModuleName(mod), ctx.ModuleSubDir(mod))) |
| 500 | } |
| 501 | }() |
| 502 | |
Colin Cross | 2465c3d | 2018-09-28 10:19:18 -0700 | [diff] [blame] | 503 | switch x := mod.(type) { |
| 504 | case AndroidMkDataProvider: |
| 505 | return translateAndroidModule(ctx, w, mod, x) |
| 506 | case bootstrap.GoBinaryTool: |
| 507 | return translateGoBinaryModule(ctx, w, mod, x) |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 508 | case AndroidMkEntriesProvider: |
| 509 | return translateAndroidMkEntriesModule(ctx, w, mod, x) |
Colin Cross | 2465c3d | 2018-09-28 10:19:18 -0700 | [diff] [blame] | 510 | default: |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 511 | return nil |
| 512 | } |
Colin Cross | 2465c3d | 2018-09-28 10:19:18 -0700 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | func translateGoBinaryModule(ctx SingletonContext, w io.Writer, mod blueprint.Module, |
| 516 | goBinary bootstrap.GoBinaryTool) error { |
| 517 | |
| 518 | name := ctx.ModuleName(mod) |
| 519 | fmt.Fprintln(w, ".PHONY:", name) |
| 520 | fmt.Fprintln(w, name+":", goBinary.InstallPath()) |
| 521 | fmt.Fprintln(w, "") |
| 522 | |
| 523 | return nil |
| 524 | } |
| 525 | |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 526 | func (data *AndroidMkData) fillInData(config Config, bpPath string, mod blueprint.Module) { |
| 527 | // Get the preamble content through AndroidMkEntries logic. |
Jooyung Han | 2ed99d0 | 2020-06-24 23:26:26 +0900 | [diff] [blame] | 528 | data.Entries = AndroidMkEntries{ |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 529 | Class: data.Class, |
| 530 | SubName: data.SubName, |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 531 | DistFiles: data.DistFiles, |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 532 | OutputFile: data.OutputFile, |
| 533 | Disabled: data.Disabled, |
| 534 | Include: data.Include, |
| 535 | Required: data.Required, |
| 536 | Host_required: data.Host_required, |
| 537 | Target_required: data.Target_required, |
| 538 | } |
Jooyung Han | 2ed99d0 | 2020-06-24 23:26:26 +0900 | [diff] [blame] | 539 | data.Entries.fillInEntries(config, bpPath, mod) |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 540 | |
| 541 | // copy entries back to data since it is used in Custom |
Jooyung Han | 2ed99d0 | 2020-06-24 23:26:26 +0900 | [diff] [blame] | 542 | data.Required = data.Entries.Required |
| 543 | data.Host_required = data.Entries.Host_required |
| 544 | data.Target_required = data.Entries.Target_required |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 545 | } |
| 546 | |
Colin Cross | 2465c3d | 2018-09-28 10:19:18 -0700 | [diff] [blame] | 547 | func translateAndroidModule(ctx SingletonContext, w io.Writer, mod blueprint.Module, |
| 548 | provider AndroidMkDataProvider) error { |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 549 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 550 | amod := mod.(Module).base() |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 551 | if shouldSkipAndroidMkProcessing(amod) { |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 552 | return nil |
| 553 | } |
| 554 | |
Colin Cross | 91825d2 | 2017-08-10 16:59:47 -0700 | [diff] [blame] | 555 | data := provider.AndroidMk() |
Colin Cross | 5349941 | 2017-09-07 13:20:25 -0700 | [diff] [blame] | 556 | if data.Include == "" { |
| 557 | data.Include = "$(BUILD_PREBUILT)" |
| 558 | } |
| 559 | |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 560 | data.fillInData(ctx.Config(), ctx.BlueprintFile(mod), mod) |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 561 | |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 562 | prefix := "" |
| 563 | if amod.ArchSpecific() { |
| 564 | switch amod.Os().Class { |
| 565 | case Host: |
| 566 | prefix = "HOST_" |
| 567 | case HostCross: |
| 568 | prefix = "HOST_CROSS_" |
| 569 | case Device: |
| 570 | prefix = "TARGET_" |
Colin Cross | a234466 | 2016-03-24 13:14:12 -0700 | [diff] [blame] | 571 | |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 572 | } |
| 573 | |
Dan Willemsen | 0ef639b | 2018-10-10 17:02:29 -0700 | [diff] [blame] | 574 | if amod.Arch().ArchType != ctx.Config().Targets[amod.Os()][0].Arch.ArchType { |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 575 | prefix = "2ND_" + prefix |
| 576 | } |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 577 | } |
| 578 | |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 579 | name := provider.BaseModuleName() |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 580 | blueprintDir := filepath.Dir(ctx.BlueprintFile(mod)) |
| 581 | |
| 582 | if data.Custom != nil { |
| 583 | data.Custom(w, name, prefix, blueprintDir, data) |
| 584 | } else { |
| 585 | WriteAndroidMkData(w, data) |
| 586 | } |
| 587 | |
| 588 | return nil |
| 589 | } |
| 590 | |
| 591 | func WriteAndroidMkData(w io.Writer, data AndroidMkData) { |
| 592 | if data.Disabled { |
| 593 | return |
| 594 | } |
| 595 | |
| 596 | if !data.OutputFile.Valid() { |
| 597 | return |
| 598 | } |
| 599 | |
Jooyung Han | 2ed99d0 | 2020-06-24 23:26:26 +0900 | [diff] [blame] | 600 | // write preamble via Entries |
| 601 | data.Entries.footer = bytes.Buffer{} |
| 602 | data.Entries.write(w) |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 603 | |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 604 | for _, extra := range data.Extra { |
Colin Cross | 27a4b05 | 2017-08-10 16:32:23 -0700 | [diff] [blame] | 605 | extra(w, data.OutputFile.Path()) |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 606 | } |
| 607 | |
Colin Cross | 5349941 | 2017-09-07 13:20:25 -0700 | [diff] [blame] | 608 | fmt.Fprintln(w, "include "+data.Include) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 609 | } |
Sasha Smundak | b6d2305 | 2019-04-01 18:37:36 -0700 | [diff] [blame] | 610 | |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 611 | func translateAndroidMkEntriesModule(ctx SingletonContext, w io.Writer, mod blueprint.Module, |
| 612 | provider AndroidMkEntriesProvider) error { |
| 613 | if shouldSkipAndroidMkProcessing(mod.(Module).base()) { |
| 614 | return nil |
Sasha Smundak | b6d2305 | 2019-04-01 18:37:36 -0700 | [diff] [blame] | 615 | } |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 616 | |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 617 | for _, entries := range provider.AndroidMkEntries() { |
| 618 | entries.fillInEntries(ctx.Config(), ctx.BlueprintFile(mod), mod) |
| 619 | entries.write(w) |
| 620 | } |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 621 | |
| 622 | return nil |
| 623 | } |
| 624 | |
| 625 | func shouldSkipAndroidMkProcessing(module *ModuleBase) bool { |
| 626 | if !module.commonProperties.NamespaceExportedToMake { |
| 627 | // TODO(jeffrygaston) do we want to validate that there are no modules being |
| 628 | // exported to Kati that depend on this module? |
| 629 | return true |
Sasha Smundak | b6d2305 | 2019-04-01 18:37:36 -0700 | [diff] [blame] | 630 | } |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 631 | |
| 632 | return !module.Enabled() || |
| 633 | module.commonProperties.SkipInstall || |
| 634 | // Make does not understand LinuxBionic |
| 635 | module.Os() == LinuxBionic |
Sasha Smundak | b6d2305 | 2019-04-01 18:37:36 -0700 | [diff] [blame] | 636 | } |
Dan Shi | 3194912 | 2020-09-21 12:11:02 -0700 | [diff] [blame^] | 637 | |
| 638 | func AndroidMkDataPaths(data []DataPath) []string { |
| 639 | var testFiles []string |
| 640 | for _, d := range data { |
| 641 | rel := d.SrcPath.Rel() |
| 642 | path := d.SrcPath.String() |
| 643 | if !strings.HasSuffix(path, rel) { |
| 644 | panic(fmt.Errorf("path %q does not end with %q", path, rel)) |
| 645 | } |
| 646 | path = strings.TrimSuffix(path, rel) |
| 647 | testFileString := path + ":" + rel |
| 648 | if len(d.RelativeInstallPath) > 0 { |
| 649 | testFileString += ":" + d.RelativeInstallPath |
| 650 | } |
| 651 | testFiles = append(testFiles, testFileString) |
| 652 | } |
| 653 | return testFiles |
| 654 | } |