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