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