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