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