Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1 | // Copyright 2015 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 15 | package android |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 16 | |
| 17 | import ( |
Colin Cross | 74ba962 | 2019-02-11 15:11:14 -0800 | [diff] [blame] | 18 | "encoding" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 19 | "fmt" |
| 20 | "reflect" |
| 21 | "runtime" |
Colin Cross | 74ba962 | 2019-02-11 15:11:14 -0800 | [diff] [blame] | 22 | "strconv" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 23 | "strings" |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 24 | |
Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 25 | "github.com/google/blueprint" |
Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 26 | "github.com/google/blueprint/bootstrap" |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 27 | "github.com/google/blueprint/proptools" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 28 | ) |
| 29 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 30 | /* |
| 31 | Example blueprints file containing all variant property groups, with comment listing what type |
| 32 | of variants get properties in that group: |
| 33 | |
| 34 | module { |
| 35 | arch: { |
| 36 | arm: { |
| 37 | // Host or device variants with arm architecture |
| 38 | }, |
| 39 | arm64: { |
| 40 | // Host or device variants with arm64 architecture |
| 41 | }, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 42 | x86: { |
| 43 | // Host or device variants with x86 architecture |
| 44 | }, |
| 45 | x86_64: { |
| 46 | // Host or device variants with x86_64 architecture |
| 47 | }, |
| 48 | }, |
| 49 | multilib: { |
| 50 | lib32: { |
| 51 | // Host or device variants for 32-bit architectures |
| 52 | }, |
| 53 | lib64: { |
| 54 | // Host or device variants for 64-bit architectures |
| 55 | }, |
| 56 | }, |
| 57 | target: { |
| 58 | android: { |
Martin Stjernholm | e284b48 | 2020-09-23 21:03:27 +0100 | [diff] [blame] | 59 | // Device variants (implies Bionic) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 60 | }, |
| 61 | host: { |
| 62 | // Host variants |
| 63 | }, |
Martin Stjernholm | e284b48 | 2020-09-23 21:03:27 +0100 | [diff] [blame] | 64 | bionic: { |
| 65 | // Bionic (device and host) variants |
| 66 | }, |
| 67 | linux_bionic: { |
| 68 | // Bionic host variants |
| 69 | }, |
| 70 | linux: { |
| 71 | // Bionic (device and host) and Linux glibc variants |
| 72 | }, |
Dan Willemsen | 5746bd4 | 2017-10-02 19:42:01 -0700 | [diff] [blame] | 73 | linux_glibc: { |
Martin Stjernholm | e284b48 | 2020-09-23 21:03:27 +0100 | [diff] [blame] | 74 | // Linux host variants (using non-Bionic libc) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 75 | }, |
| 76 | darwin: { |
| 77 | // Darwin host variants |
| 78 | }, |
| 79 | windows: { |
| 80 | // Windows host variants |
| 81 | }, |
| 82 | not_windows: { |
| 83 | // Non-windows host variants |
| 84 | }, |
Martin Stjernholm | e284b48 | 2020-09-23 21:03:27 +0100 | [diff] [blame] | 85 | android_arm: { |
| 86 | // Any <os>_<arch> combination restricts to that os and arch |
| 87 | }, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 88 | }, |
| 89 | } |
| 90 | */ |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 91 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 92 | // An Arch indicates a single CPU architecture. |
| 93 | type Arch struct { |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 94 | // The type of the architecture (arm, arm64, x86, or x86_64). |
| 95 | ArchType ArchType |
| 96 | |
| 97 | // The variant of the architecture, for example "armv7-a" or "armv7-a-neon" for arm. |
| 98 | ArchVariant string |
| 99 | |
| 100 | // The variant of the CPU, for example "cortex-a53" for arm64. |
| 101 | CpuVariant string |
| 102 | |
| 103 | // The list of Android app ABIs supported by the CPU architecture, for example "arm64-v8a". |
| 104 | Abi []string |
| 105 | |
| 106 | // The list of arch-specific features supported by the CPU architecture, for example "neon". |
Colin Cross | c5c24ad | 2015-11-20 15:35:00 -0800 | [diff] [blame] | 107 | ArchFeatures []string |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 110 | // String returns the Arch as a string. The value is used as the name of the variant created |
| 111 | // by archMutator. |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 112 | func (a Arch) String() string { |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame] | 113 | s := a.ArchType.String() |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 114 | if a.ArchVariant != "" { |
| 115 | s += "_" + a.ArchVariant |
| 116 | } |
| 117 | if a.CpuVariant != "" { |
| 118 | s += "_" + a.CpuVariant |
| 119 | } |
| 120 | return s |
| 121 | } |
| 122 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 123 | // ArchType is used to define the 4 supported architecture types (arm, arm64, x86, x86_64), as |
| 124 | // well as the "common" architecture used for modules that support multiple architectures, for |
| 125 | // example Java modules. |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 126 | type ArchType struct { |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 127 | // Name is the name of the architecture type, "arm", "arm64", "x86", or "x86_64". |
| 128 | Name string |
| 129 | |
| 130 | // Field is the name of the field used in properties that refer to the architecture, e.g. "Arm64". |
| 131 | Field string |
| 132 | |
| 133 | // Multilib is either "lib32" or "lib64" for 32-bit or 64-bit architectures. |
Colin Cross | ec19363 | 2015-07-06 17:49:43 -0700 | [diff] [blame] | 134 | Multilib string |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 137 | // String returns the name of the ArchType. |
| 138 | func (a ArchType) String() string { |
| 139 | return a.Name |
| 140 | } |
| 141 | |
| 142 | const COMMON_VARIANT = "common" |
| 143 | |
| 144 | var ( |
| 145 | archTypeList []ArchType |
| 146 | |
| 147 | Arm = newArch("arm", "lib32") |
| 148 | Arm64 = newArch("arm64", "lib64") |
| 149 | X86 = newArch("x86", "lib32") |
| 150 | X86_64 = newArch("x86_64", "lib64") |
| 151 | |
| 152 | Common = ArchType{ |
| 153 | Name: COMMON_VARIANT, |
| 154 | } |
| 155 | ) |
| 156 | |
| 157 | var archTypeMap = map[string]ArchType{} |
| 158 | |
Colin Cross | ec19363 | 2015-07-06 17:49:43 -0700 | [diff] [blame] | 159 | func newArch(name, multilib string) ArchType { |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 160 | archType := ArchType{ |
Colin Cross | ec19363 | 2015-07-06 17:49:43 -0700 | [diff] [blame] | 161 | Name: name, |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 162 | Field: proptools.FieldNameForProperty(name), |
Colin Cross | ec19363 | 2015-07-06 17:49:43 -0700 | [diff] [blame] | 163 | Multilib: multilib, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 164 | } |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 165 | archTypeList = append(archTypeList, archType) |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 166 | archTypeMap[name] = archType |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 167 | return archType |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Jingwen Chen | 2f6a21e | 2021-04-05 07:33:05 +0000 | [diff] [blame] | 170 | // ArchTypeList returns the a slice copy of the 4 supported ArchTypes for arm, |
| 171 | // arm64, x86 and x86_64. |
Jaewoong Jung | 1ce9ac6 | 2019-08-13 14:11:33 -0700 | [diff] [blame] | 172 | func ArchTypeList() []ArchType { |
| 173 | return append([]ArchType(nil), archTypeList...) |
| 174 | } |
| 175 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 176 | // MarshalText allows an ArchType to be serialized through any encoder that supports |
| 177 | // encoding.TextMarshaler. |
Colin Cross | 74ba962 | 2019-02-11 15:11:14 -0800 | [diff] [blame] | 178 | func (a ArchType) MarshalText() ([]byte, error) { |
| 179 | return []byte(strconv.Quote(a.String())), nil |
| 180 | } |
| 181 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 182 | var _ encoding.TextMarshaler = ArchType{} |
Colin Cross | 74ba962 | 2019-02-11 15:11:14 -0800 | [diff] [blame] | 183 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 184 | // UnmarshalText allows an ArchType to be deserialized through any decoder that supports |
| 185 | // encoding.TextUnmarshaler. |
Colin Cross | 74ba962 | 2019-02-11 15:11:14 -0800 | [diff] [blame] | 186 | func (a *ArchType) UnmarshalText(text []byte) error { |
| 187 | if u, ok := archTypeMap[string(text)]; ok { |
| 188 | *a = u |
| 189 | return nil |
| 190 | } |
| 191 | |
| 192 | return fmt.Errorf("unknown ArchType %q", text) |
| 193 | } |
| 194 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 195 | var _ encoding.TextUnmarshaler = &ArchType{} |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 196 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 197 | // OsClass is an enum that describes whether a variant of a module runs on the host, on the device, |
| 198 | // or is generic. |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 199 | type OsClass int |
| 200 | |
| 201 | const ( |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 202 | // Generic is used for variants of modules that are not OS-specific. |
Dan Willemsen | 0e2d97b | 2016-11-28 17:50:06 -0800 | [diff] [blame] | 203 | Generic OsClass = iota |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 204 | // Device is used for variants of modules that run on the device. |
Dan Willemsen | 0e2d97b | 2016-11-28 17:50:06 -0800 | [diff] [blame] | 205 | Device |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 206 | // Host is used for variants of modules that run on the host. |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 207 | Host |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 208 | ) |
| 209 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 210 | // String returns the OsClass as a string. |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 211 | func (class OsClass) String() string { |
| 212 | switch class { |
| 213 | case Generic: |
| 214 | return "generic" |
| 215 | case Device: |
| 216 | return "device" |
| 217 | case Host: |
| 218 | return "host" |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 219 | default: |
| 220 | panic(fmt.Errorf("unknown class %d", class)) |
| 221 | } |
| 222 | } |
| 223 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 224 | // OsType describes an OS variant of a module. |
| 225 | type OsType struct { |
| 226 | // Name is the name of the OS. It is also used as the name of the property in Android.bp |
| 227 | // files. |
| 228 | Name string |
| 229 | |
| 230 | // Field is the name of the OS converted to an exported field name, i.e. with the first |
| 231 | // character capitalized. |
| 232 | Field string |
| 233 | |
| 234 | // Class is the OsClass of the OS. |
| 235 | Class OsClass |
| 236 | |
| 237 | // DefaultDisabled is set when the module variants for the OS should not be created unless |
| 238 | // the module explicitly requests them. This is used to limit Windows cross compilation to |
| 239 | // only modules that need it. |
| 240 | DefaultDisabled bool |
| 241 | } |
| 242 | |
| 243 | // String returns the name of the OsType. |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 244 | func (os OsType) String() string { |
| 245 | return os.Name |
Colin Cross | 54c7112 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 248 | // Bionic returns true if the OS uses the Bionic libc runtime, i.e. if the OS is Android or |
| 249 | // is Linux with Bionic. |
Dan Willemsen | 866b563 | 2017-09-22 12:28:24 -0700 | [diff] [blame] | 250 | func (os OsType) Bionic() bool { |
| 251 | return os == Android || os == LinuxBionic |
| 252 | } |
| 253 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 254 | // Linux returns true if the OS uses the Linux kernel, i.e. if the OS is Android or is Linux |
| 255 | // with or without the Bionic libc runtime. |
Dan Willemsen | 866b563 | 2017-09-22 12:28:24 -0700 | [diff] [blame] | 256 | func (os OsType) Linux() bool { |
| 257 | return os == Android || os == Linux || os == LinuxBionic |
| 258 | } |
| 259 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 260 | // newOsType constructs an OsType and adds it to the global lists. |
| 261 | func newOsType(name string, class OsClass, defDisabled bool, archTypes ...ArchType) OsType { |
| 262 | checkCalledFromInit() |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 263 | os := OsType{ |
| 264 | Name: name, |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 265 | Field: proptools.FieldNameForProperty(name), |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 266 | Class: class, |
Dan Willemsen | 0a37a2a | 2016-11-13 10:16:05 -0800 | [diff] [blame] | 267 | |
| 268 | DefaultDisabled: defDisabled, |
Colin Cross | 54c7112 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 269 | } |
Jingwen Chen | 2f6a21e | 2021-04-05 07:33:05 +0000 | [diff] [blame] | 270 | osTypeList = append(osTypeList, os) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 271 | |
| 272 | if _, found := commonTargetMap[name]; found { |
| 273 | panic(fmt.Errorf("Found Os type duplicate during OsType registration: %q", name)) |
| 274 | } else { |
Colin Cross | e9fe294 | 2020-11-10 18:12:15 -0800 | [diff] [blame] | 275 | commonTargetMap[name] = Target{Os: os, Arch: CommonArch} |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 276 | } |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 277 | osArchTypeMap[os] = archTypes |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 278 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 279 | return os |
| 280 | } |
| 281 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 282 | // osByName returns the OsType that has the given name, or NoOsType if none match. |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 283 | func osByName(name string) OsType { |
Jingwen Chen | 2f6a21e | 2021-04-05 07:33:05 +0000 | [diff] [blame] | 284 | for _, os := range osTypeList { |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 285 | if os.Name == name { |
| 286 | return os |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | return NoOsType |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 291 | } |
| 292 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 293 | // BuildOs returns the OsType for the OS that the build is running on. |
| 294 | var BuildOs = func() OsType { |
| 295 | switch runtime.GOOS { |
| 296 | case "linux": |
| 297 | return Linux |
| 298 | case "darwin": |
| 299 | return Darwin |
| 300 | default: |
| 301 | panic(fmt.Sprintf("unsupported OS: %s", runtime.GOOS)) |
| 302 | } |
| 303 | }() |
dimitry | 1f33e40 | 2019-03-26 12:39:31 +0100 | [diff] [blame] | 304 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 305 | // BuildArch returns the ArchType for the CPU that the build is running on. |
| 306 | var BuildArch = func() ArchType { |
| 307 | switch runtime.GOARCH { |
| 308 | case "amd64": |
| 309 | return X86_64 |
| 310 | default: |
| 311 | panic(fmt.Sprintf("unsupported Arch: %s", runtime.GOARCH)) |
| 312 | } |
| 313 | }() |
| 314 | |
| 315 | var ( |
Jingwen Chen | 2f6a21e | 2021-04-05 07:33:05 +0000 | [diff] [blame] | 316 | // osTypeList contains a list of all the supported OsTypes, including ones not supported |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 317 | // by the current build host or the target device. |
Jingwen Chen | 2f6a21e | 2021-04-05 07:33:05 +0000 | [diff] [blame] | 318 | osTypeList []OsType |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 319 | // commonTargetMap maps names of OsTypes to the corresponding common Target, i.e. the |
| 320 | // Target with the same OsType and the common ArchType. |
| 321 | commonTargetMap = make(map[string]Target) |
| 322 | // osArchTypeMap maps OsTypes to the list of supported ArchTypes for that OS. |
| 323 | osArchTypeMap = map[OsType][]ArchType{} |
| 324 | |
| 325 | // NoOsType is a placeholder for when no OS is needed. |
| 326 | NoOsType OsType |
| 327 | // Linux is the OS for the Linux kernel plus the glibc runtime. |
| 328 | Linux = newOsType("linux_glibc", Host, false, X86, X86_64) |
| 329 | // Darwin is the OS for MacOS/Darwin host machines. |
| 330 | Darwin = newOsType("darwin", Host, false, X86_64) |
| 331 | // LinuxBionic is the OS for the Linux kernel plus the Bionic libc runtime, but without the |
| 332 | // rest of Android. |
| 333 | LinuxBionic = newOsType("linux_bionic", Host, false, Arm64, X86_64) |
| 334 | // Windows the OS for Windows host machines. |
| 335 | Windows = newOsType("windows", Host, true, X86, X86_64) |
| 336 | // Android is the OS for target devices that run all of Android, including the Linux kernel |
| 337 | // and the Bionic libc runtime. |
| 338 | Android = newOsType("android", Device, false, Arm, Arm64, X86, X86_64) |
| 339 | // Fuchsia is the OS for target devices that run Fuchsia. |
| 340 | Fuchsia = newOsType("fuchsia", Device, false, Arm64, X86_64) |
| 341 | |
| 342 | // CommonOS is a pseudo OSType for a common OS variant, which is OsType agnostic and which |
| 343 | // has dependencies on all the OS variants. |
| 344 | CommonOS = newOsType("common_os", Generic, false) |
Colin Cross | e9fe294 | 2020-11-10 18:12:15 -0800 | [diff] [blame] | 345 | |
| 346 | // CommonArch is the Arch for all modules that are os-specific but not arch specific, |
| 347 | // for example most Java modules. |
| 348 | CommonArch = Arch{ArchType: Common} |
dimitry | 1f33e40 | 2019-03-26 12:39:31 +0100 | [diff] [blame] | 349 | ) |
| 350 | |
Jingwen Chen | 2f6a21e | 2021-04-05 07:33:05 +0000 | [diff] [blame] | 351 | // OsTypeList returns a slice copy of the supported OsTypes. |
| 352 | func OsTypeList() []OsType { |
| 353 | return append([]OsType(nil), osTypeList...) |
| 354 | } |
| 355 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 356 | // Target specifies the OS and architecture that a module is being compiled for. |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 357 | type Target struct { |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 358 | // Os the OS that the module is being compiled for (e.g. "linux_glibc", "android"). |
| 359 | Os OsType |
| 360 | // Arch is the architecture that the module is being compiled for. |
| 361 | Arch Arch |
| 362 | // NativeBridge is NativeBridgeEnabled if the architecture is supported using NativeBridge |
| 363 | // (i.e. arm on x86) for this device. |
| 364 | NativeBridge NativeBridgeSupport |
| 365 | // NativeBridgeHostArchName is the name of the real architecture that is used to implement |
| 366 | // the NativeBridge architecture. For example, for arm on x86 this would be "x86". |
dimitry | 8d6dde8 | 2019-07-11 10:23:53 +0200 | [diff] [blame] | 367 | NativeBridgeHostArchName string |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 368 | // NativeBridgeRelativePath is the name of the subdirectory that will contain NativeBridge |
| 369 | // libraries and binaries. |
dimitry | 8d6dde8 | 2019-07-11 10:23:53 +0200 | [diff] [blame] | 370 | NativeBridgeRelativePath string |
Jiyong Park | 1613e55 | 2020-09-14 19:43:17 +0900 | [diff] [blame] | 371 | |
| 372 | // HostCross is true when the target cannot run natively on the current build host. |
| 373 | // For example, linux_glibc_x86 returns true on a regular x86/i686/Linux machines, but returns false |
| 374 | // on Mac (different OS), or on 64-bit only i686/Linux machines (unsupported arch). |
| 375 | HostCross bool |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 378 | // NativeBridgeSupport is an enum that specifies if a Target supports NativeBridge. |
| 379 | type NativeBridgeSupport bool |
| 380 | |
| 381 | const ( |
| 382 | NativeBridgeDisabled NativeBridgeSupport = false |
| 383 | NativeBridgeEnabled NativeBridgeSupport = true |
| 384 | ) |
| 385 | |
| 386 | // String returns the OS and arch variations used for the Target. |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 387 | func (target Target) String() string { |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 388 | return target.OsVariation() + "_" + target.ArchVariation() |
| 389 | } |
| 390 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 391 | // OsVariation returns the name of the variation used by the osMutator for the Target. |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 392 | func (target Target) OsVariation() string { |
| 393 | return target.Os.String() |
Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 396 | // ArchVariation returns the name of the variation used by the archMutator for the Target. |
Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 397 | func (target Target) ArchVariation() string { |
| 398 | var variation string |
dimitry | 1f33e40 | 2019-03-26 12:39:31 +0100 | [diff] [blame] | 399 | if target.NativeBridge { |
Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 400 | variation = "native_bridge_" |
dimitry | 1f33e40 | 2019-03-26 12:39:31 +0100 | [diff] [blame] | 401 | } |
Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 402 | variation += target.Arch.String() |
| 403 | |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 404 | return variation |
Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 407 | // Variations returns a list of blueprint.Variations for the osMutator and archMutator for the |
| 408 | // Target. |
Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 409 | func (target Target) Variations() []blueprint.Variation { |
| 410 | return []blueprint.Variation{ |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 411 | {Mutator: "os", Variation: target.OsVariation()}, |
Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 412 | {Mutator: "arch", Variation: target.ArchVariation()}, |
| 413 | } |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 414 | } |
| 415 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 416 | // osMutator splits an arch-specific module into a variant for each OS that is enabled for the |
| 417 | // module. It uses the HostOrDevice value passed to InitAndroidArchModule and the |
| 418 | // device_supported and host_supported properties to determine which OsTypes are enabled for this |
| 419 | // module, then searches through the Targets to determine which have enabled Targets for this |
| 420 | // module. |
Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 421 | func osMutator(bpctx blueprint.BottomUpMutatorContext) { |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 422 | var module Module |
| 423 | var ok bool |
Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 424 | if module, ok = bpctx.Module().(Module); !ok { |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 425 | // The module is not a Soong module, it is a Blueprint module. |
Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 426 | if bootstrap.IsBootstrapModule(bpctx.Module()) { |
| 427 | // Bootstrap Go modules are always the build OS or linux bionic. |
| 428 | config := bpctx.Config().(Config) |
| 429 | osNames := []string{config.BuildOSTarget.OsVariation()} |
| 430 | for _, hostCrossTarget := range config.Targets[LinuxBionic] { |
| 431 | if hostCrossTarget.Arch.ArchType == config.BuildOSTarget.Arch.ArchType { |
| 432 | osNames = append(osNames, hostCrossTarget.OsVariation()) |
| 433 | } |
| 434 | } |
| 435 | osNames = FirstUniqueStrings(osNames) |
| 436 | bpctx.CreateVariations(osNames...) |
| 437 | } |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 438 | return |
| 439 | } |
| 440 | |
Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 441 | // Bootstrap Go module support above requires this mutator to be a |
| 442 | // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext |
| 443 | // filters out non-Soong modules. Now that we've handled them, create a |
| 444 | // normal android.BottomUpMutatorContext. |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 445 | mctx := bottomUpMutatorContextFactory(bpctx, module, false, false) |
Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 446 | |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 447 | base := module.base() |
| 448 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 449 | // Nothing to do for modules that are not architecture specific (e.g. a genrule). |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 450 | if !base.ArchSpecific() { |
| 451 | return |
| 452 | } |
| 453 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 454 | // Collect a list of OSTypes supported by this module based on the HostOrDevice value |
| 455 | // passed to InitAndroidArchModule and the device_supported and host_supported properties. |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 456 | var moduleOSList []OsType |
Jingwen Chen | 2f6a21e | 2021-04-05 07:33:05 +0000 | [diff] [blame] | 457 | for _, os := range osTypeList { |
Jiyong Park | 1613e55 | 2020-09-14 19:43:17 +0900 | [diff] [blame] | 458 | for _, t := range mctx.Config().Targets[os] { |
Colin Cross | 08d6f8f | 2020-11-19 02:33:19 +0000 | [diff] [blame] | 459 | if base.supportsTarget(t) { |
Jiyong Park | 1613e55 | 2020-09-14 19:43:17 +0900 | [diff] [blame] | 460 | moduleOSList = append(moduleOSList, os) |
| 461 | break |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 462 | } |
| 463 | } |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 466 | // If there are no supported OSes then disable the module. |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 467 | if len(moduleOSList) == 0 { |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 468 | base.Disable() |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 469 | return |
| 470 | } |
| 471 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 472 | // Convert the list of supported OsTypes to the variation names. |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 473 | osNames := make([]string, len(moduleOSList)) |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 474 | for i, os := range moduleOSList { |
| 475 | osNames[i] = os.String() |
| 476 | } |
| 477 | |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 478 | createCommonOSVariant := base.commonProperties.CreateCommonOSVariant |
| 479 | if createCommonOSVariant { |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 480 | // A CommonOS variant was requested so add it to the list of OS variants to |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 481 | // create. It needs to be added to the end because it needs to depend on the |
| 482 | // the other variants in the list returned by CreateVariations(...) and inter |
| 483 | // variant dependencies can only be created from a later variant in that list to |
| 484 | // an earlier one. That is because variants are always processed in the order in |
| 485 | // which they are returned from CreateVariations(...). |
| 486 | osNames = append(osNames, CommonOS.Name) |
| 487 | moduleOSList = append(moduleOSList, CommonOS) |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 490 | // Create the variations, annotate each one with which OS it was created for, and |
| 491 | // squash the appropriate OS-specific properties into the top level properties. |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 492 | modules := mctx.CreateVariations(osNames...) |
| 493 | for i, m := range modules { |
| 494 | m.base().commonProperties.CompileOS = moduleOSList[i] |
| 495 | m.base().setOSProperties(mctx) |
| 496 | } |
| 497 | |
| 498 | if createCommonOSVariant { |
| 499 | // A CommonOS variant was requested so add dependencies from it (the last one in |
| 500 | // the list) to the OS type specific variants. |
| 501 | last := len(modules) - 1 |
| 502 | commonOSVariant := modules[last] |
| 503 | commonOSVariant.base().commonProperties.CommonOSVariant = true |
| 504 | for _, module := range modules[0:last] { |
| 505 | // Ignore modules that are enabled. Note, this will only avoid adding |
| 506 | // dependencies on OsType variants that are explicitly disabled in their |
| 507 | // properties. The CommonOS variant will still depend on disabled variants |
| 508 | // if they are disabled afterwards, e.g. in archMutator if |
| 509 | if module.Enabled() { |
| 510 | mctx.AddInterVariantDependency(commonOsToOsSpecificVariantTag, commonOSVariant, module) |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | } |
| 515 | |
Colin Cross | c179ea6 | 2020-10-09 10:54:15 -0700 | [diff] [blame] | 516 | type archDepTag struct { |
| 517 | blueprint.BaseDependencyTag |
| 518 | name string |
| 519 | } |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 520 | |
Colin Cross | c179ea6 | 2020-10-09 10:54:15 -0700 | [diff] [blame] | 521 | // Identifies the dependency from CommonOS variant to the os specific variants. |
| 522 | var commonOsToOsSpecificVariantTag = archDepTag{name: "common os to os specific"} |
| 523 | |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 524 | // Get the OsType specific variants for the current CommonOS variant. |
| 525 | // |
| 526 | // The returned list will only contain enabled OsType specific variants of the |
| 527 | // module referenced in the supplied context. An empty list is returned if there |
| 528 | // are no enabled variants or the supplied context is not for an CommonOS |
| 529 | // variant. |
| 530 | func GetOsSpecificVariantsOfCommonOSVariant(mctx BaseModuleContext) []Module { |
| 531 | var variants []Module |
| 532 | mctx.VisitDirectDeps(func(m Module) { |
| 533 | if mctx.OtherModuleDependencyTag(m) == commonOsToOsSpecificVariantTag { |
| 534 | if m.Enabled() { |
| 535 | variants = append(variants, m) |
| 536 | } |
| 537 | } |
| 538 | }) |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 539 | return variants |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 540 | } |
| 541 | |
Colin Cross | ee0bc3b | 2018-10-02 22:01:37 -0700 | [diff] [blame] | 542 | // archMutator splits a module into a variant for each Target requested by the module. Target selection |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 543 | // for a module is in three levels, OsClass, multilib, and then Target. |
Colin Cross | ee0bc3b | 2018-10-02 22:01:37 -0700 | [diff] [blame] | 544 | // OsClass selection is determined by: |
| 545 | // - The HostOrDeviceSupported value passed in to InitAndroidArchModule by the module type factory, which selects |
| 546 | // whether the module type can compile for host, device or both. |
| 547 | // - The host_supported and device_supported properties on the module. |
Roland Levillain | f5b635d | 2019-06-05 14:42:57 +0100 | [diff] [blame] | 548 | // If host is supported for the module, the Host and HostCross OsClasses are selected. If device is supported |
Colin Cross | ee0bc3b | 2018-10-02 22:01:37 -0700 | [diff] [blame] | 549 | // for the module, the Device OsClass is selected. |
| 550 | // Within each selected OsClass, the multilib selection is determined by: |
Jaewoong Jung | 02b2d4d | 2019-06-06 15:19:57 -0700 | [diff] [blame] | 551 | // - The compile_multilib property if it set (which may be overridden by target.android.compile_multilib or |
Colin Cross | ee0bc3b | 2018-10-02 22:01:37 -0700 | [diff] [blame] | 552 | // target.host.compile_multilib). |
| 553 | // - The default multilib passed to InitAndroidArchModule if compile_multilib was not set. |
| 554 | // Valid multilib values include: |
| 555 | // "both": compile for all Targets supported by the OsClass (generally x86_64 and x86, or arm64 and arm). |
| 556 | // "first": compile for only a single preferred Target supported by the OsClass. This is generally x86_64 or arm64, |
Elliott Hughes | 79ae341 | 2020-04-17 15:49:49 -0700 | [diff] [blame] | 557 | // but may be arm for a 32-bit only build. |
Colin Cross | ee0bc3b | 2018-10-02 22:01:37 -0700 | [diff] [blame] | 558 | // "32": compile for only a single 32-bit Target supported by the OsClass. |
| 559 | // "64": compile for only a single 64-bit Target supported by the OsClass. |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 560 | // "common": compile a for a single Target that will work on all Targets supported by the OsClass (for example Java). |
| 561 | // "common_first": compile a for a Target that will work on all Targets supported by the OsClass |
| 562 | // (same as "common"), plus a second Target for the preferred Target supported by the OsClass |
| 563 | // (same as "first"). This is used for java_binary that produces a common .jar and a wrapper |
| 564 | // executable script. |
Colin Cross | ee0bc3b | 2018-10-02 22:01:37 -0700 | [diff] [blame] | 565 | // |
| 566 | // Once the list of Targets is determined, the module is split into a variant for each Target. |
| 567 | // |
| 568 | // Modules can be initialized with InitAndroidMultiTargetsArchModule, in which case they will be split by OsClass, |
| 569 | // but will have a common Target that is expected to handle all other selected Targets via ctx.MultiTargets(). |
Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 570 | func archMutator(bpctx blueprint.BottomUpMutatorContext) { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 571 | var module Module |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 572 | var ok bool |
Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 573 | if module, ok = bpctx.Module().(Module); !ok { |
| 574 | if bootstrap.IsBootstrapModule(bpctx.Module()) { |
| 575 | // Bootstrap Go modules are always the build architecture. |
| 576 | bpctx.CreateVariations(bpctx.Config().(Config).BuildOSTarget.ArchVariation()) |
| 577 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 578 | return |
| 579 | } |
| 580 | |
Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 581 | // Bootstrap Go module support above requires this mutator to be a |
| 582 | // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext |
| 583 | // filters out non-Soong modules. Now that we've handled them, create a |
| 584 | // normal android.BottomUpMutatorContext. |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 585 | mctx := bottomUpMutatorContextFactory(bpctx, module, false, false) |
Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 586 | |
Colin Cross | 5eca7cb | 2018-10-02 14:02:10 -0700 | [diff] [blame] | 587 | base := module.base() |
| 588 | |
| 589 | if !base.ArchSpecific() { |
Colin Cross | b9db480 | 2016-06-03 01:50:47 +0000 | [diff] [blame] | 590 | return |
| 591 | } |
| 592 | |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 593 | os := base.commonProperties.CompileOS |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 594 | if os == CommonOS { |
| 595 | // Make sure that the target related properties are initialized for the |
| 596 | // CommonOS variant. |
| 597 | addTargetProperties(module, commonTargetMap[os.Name], nil, true) |
| 598 | |
| 599 | // Do not create arch specific variants for the CommonOS variant. |
| 600 | return |
| 601 | } |
| 602 | |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 603 | osTargets := mctx.Config().Targets[os] |
Colin Cross | fb0c16e | 2019-11-20 17:12:35 -0800 | [diff] [blame] | 604 | image := base.commonProperties.ImageVariation |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 605 | // Filter NativeBridge targets unless they are explicitly supported. |
| 606 | // Skip creating native bridge variants for non-core modules. |
Colin Cross | 83bead4 | 2019-12-18 10:45:46 -0800 | [diff] [blame] | 607 | if os == Android && |
| 608 | !(Bool(base.commonProperties.Native_bridge_supported) && image == CoreVariation) { |
| 609 | |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 610 | var targets []Target |
| 611 | for _, t := range osTargets { |
| 612 | if !t.NativeBridge { |
| 613 | targets = append(targets, t) |
Dan Willemsen | 0ef639b | 2018-10-10 17:02:29 -0700 | [diff] [blame] | 614 | } |
| 615 | } |
Dan Willemsen | 0ef639b | 2018-10-10 17:02:29 -0700 | [diff] [blame] | 616 | |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 617 | osTargets = targets |
| 618 | } |
Colin Cross | ee0bc3b | 2018-10-02 22:01:37 -0700 | [diff] [blame] | 619 | |
Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 620 | // only the primary arch in the ramdisk / vendor_ramdisk / recovery partition |
| 621 | if os == Android && (module.InstallInRecovery() || module.InstallInRamdisk() || module.InstallInVendorRamdisk()) { |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 622 | osTargets = []Target{osTargets[0]} |
| 623 | } |
dimitry | 1f33e40 | 2019-03-26 12:39:31 +0100 | [diff] [blame] | 624 | |
Jaewoong Jung | 003d808 | 2021-02-24 17:39:54 -0800 | [diff] [blame] | 625 | // Windows builds always prefer 32-bit |
| 626 | prefer32 := os == Windows |
dimitry | 1f33e40 | 2019-03-26 12:39:31 +0100 | [diff] [blame] | 627 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 628 | // Determine the multilib selection for this module. |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 629 | multilib, extraMultilib := decodeMultilib(base, os.Class) |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 630 | |
| 631 | // Convert the multilib selection into a list of Targets. |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 632 | targets, err := decodeMultilibTargets(multilib, osTargets, prefer32) |
| 633 | if err != nil { |
| 634 | mctx.ModuleErrorf("%s", err.Error()) |
| 635 | } |
Colin Cross | 5eca7cb | 2018-10-02 14:02:10 -0700 | [diff] [blame] | 636 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 637 | // If the module is using extraMultilib, decode the extraMultilib selection into |
| 638 | // a separate list of Targets. |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 639 | var multiTargets []Target |
| 640 | if extraMultilib != "" { |
| 641 | multiTargets, err = decodeMultilibTargets(extraMultilib, osTargets, prefer32) |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 642 | if err != nil { |
| 643 | mctx.ModuleErrorf("%s", err.Error()) |
| 644 | } |
Colin Cross | b9db480 | 2016-06-03 01:50:47 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 647 | // Recovery is always the primary architecture, filter out any other architectures. |
Inseob Kim | 20fb5d4 | 2021-02-02 20:07:58 +0900 | [diff] [blame] | 648 | // Common arch is also allowed |
Colin Cross | fb0c16e | 2019-11-20 17:12:35 -0800 | [diff] [blame] | 649 | if image == RecoveryVariation { |
| 650 | primaryArch := mctx.Config().DevicePrimaryArchType() |
Inseob Kim | 20fb5d4 | 2021-02-02 20:07:58 +0900 | [diff] [blame] | 651 | targets = filterToArch(targets, primaryArch, Common) |
| 652 | multiTargets = filterToArch(multiTargets, primaryArch, Common) |
Colin Cross | fb0c16e | 2019-11-20 17:12:35 -0800 | [diff] [blame] | 653 | } |
| 654 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 655 | // If there are no supported targets disable the module. |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 656 | if len(targets) == 0 { |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 657 | base.Disable() |
Dan Willemsen | 3f32f03 | 2016-07-11 14:36:48 -0700 | [diff] [blame] | 658 | return |
| 659 | } |
| 660 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 661 | // Convert the targets into a list of arch variation names. |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 662 | targetNames := make([]string, len(targets)) |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 663 | for i, target := range targets { |
| 664 | targetNames[i] = target.ArchVariation() |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 665 | } |
| 666 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 667 | // Create the variations, annotate each one with which Target it was created for, and |
| 668 | // squash the appropriate arch-specific properties into the top level properties. |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 669 | modules := mctx.CreateVariations(targetNames...) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 670 | for i, m := range modules { |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 671 | addTargetProperties(m, targets[i], multiTargets, i == 0) |
Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 672 | m.base().setArchProperties(mctx) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 673 | } |
| 674 | } |
| 675 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 676 | // addTargetProperties annotates a variant with the Target is is being compiled for, the list |
| 677 | // of additional Targets it is supporting (if any), and whether it is the primary Target for |
| 678 | // the module. |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 679 | func addTargetProperties(m Module, target Target, multiTargets []Target, primaryTarget bool) { |
| 680 | m.base().commonProperties.CompileTarget = target |
| 681 | m.base().commonProperties.CompileMultiTargets = multiTargets |
| 682 | m.base().commonProperties.CompilePrimary = primaryTarget |
| 683 | } |
| 684 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 685 | // decodeMultilib returns the appropriate compile_multilib property for the module, or the default |
| 686 | // multilib from the factory's call to InitAndroidArchModule if none was set. For modules that |
| 687 | // called InitAndroidMultiTargetsArchModule it always returns "common" for multilib, and returns |
| 688 | // the actual multilib in extraMultilib. |
Colin Cross | ee0bc3b | 2018-10-02 22:01:37 -0700 | [diff] [blame] | 689 | func decodeMultilib(base *ModuleBase, class OsClass) (multilib, extraMultilib string) { |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 690 | // First check the "android.compile_multilib" or "host.compile_multilib" properties. |
Colin Cross | ee0bc3b | 2018-10-02 22:01:37 -0700 | [diff] [blame] | 691 | switch class { |
| 692 | case Device: |
| 693 | multilib = String(base.commonProperties.Target.Android.Compile_multilib) |
Jiyong Park | 1613e55 | 2020-09-14 19:43:17 +0900 | [diff] [blame] | 694 | case Host: |
Colin Cross | ee0bc3b | 2018-10-02 22:01:37 -0700 | [diff] [blame] | 695 | multilib = String(base.commonProperties.Target.Host.Compile_multilib) |
| 696 | } |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 697 | |
| 698 | // If those aren't set, try the "compile_multilib" property. |
Colin Cross | ee0bc3b | 2018-10-02 22:01:37 -0700 | [diff] [blame] | 699 | if multilib == "" { |
| 700 | multilib = String(base.commonProperties.Compile_multilib) |
| 701 | } |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 702 | |
| 703 | // If that wasn't set, use the default multilib set by the factory. |
Colin Cross | ee0bc3b | 2018-10-02 22:01:37 -0700 | [diff] [blame] | 704 | if multilib == "" { |
| 705 | multilib = base.commonProperties.Default_multilib |
| 706 | } |
| 707 | |
| 708 | if base.commonProperties.UseTargetVariants { |
| 709 | return multilib, "" |
| 710 | } else { |
| 711 | // For app modules a single arch variant will be created per OS class which is expected to handle all the |
| 712 | // selected arches. Return the common-type as multilib and any Android.bp provided multilib as extraMultilib |
| 713 | if multilib == base.commonProperties.Default_multilib { |
| 714 | multilib = "first" |
| 715 | } |
| 716 | return base.commonProperties.Default_multilib, multilib |
| 717 | } |
| 718 | } |
| 719 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 720 | // filterToArch takes a list of Targets and an ArchType, and returns a modified list that contains |
Inseob Kim | 20fb5d4 | 2021-02-02 20:07:58 +0900 | [diff] [blame] | 721 | // only Targets that have the specified ArchTypes. |
| 722 | func filterToArch(targets []Target, archs ...ArchType) []Target { |
Colin Cross | fb0c16e | 2019-11-20 17:12:35 -0800 | [diff] [blame] | 723 | for i := 0; i < len(targets); i++ { |
Inseob Kim | 20fb5d4 | 2021-02-02 20:07:58 +0900 | [diff] [blame] | 724 | found := false |
| 725 | for _, arch := range archs { |
| 726 | if targets[i].Arch.ArchType == arch { |
| 727 | found = true |
| 728 | break |
| 729 | } |
| 730 | } |
| 731 | if !found { |
Colin Cross | fb0c16e | 2019-11-20 17:12:35 -0800 | [diff] [blame] | 732 | targets = append(targets[:i], targets[i+1:]...) |
| 733 | i-- |
| 734 | } |
| 735 | } |
| 736 | return targets |
| 737 | } |
| 738 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 739 | // archPropRoot is a struct type used as the top level of the arch-specific properties. It |
| 740 | // contains the "arch", "multilib", and "target" property structs. It is used to split up the |
| 741 | // property structs to limit how much is allocated when a single arch-specific property group is |
| 742 | // used. The types are interface{} because they will hold instances of runtime-created types. |
Colin Cross | cbbd13f | 2020-01-17 14:08:22 -0800 | [diff] [blame] | 743 | type archPropRoot struct { |
| 744 | Arch, Multilib, Target interface{} |
| 745 | } |
| 746 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 747 | // archPropTypeDesc holds the runtime-created types for the property structs to instantiate to |
| 748 | // create an archPropRoot property struct. |
| 749 | type archPropTypeDesc struct { |
| 750 | arch, multilib, target reflect.Type |
| 751 | } |
| 752 | |
Colin Cross | cbbd13f | 2020-01-17 14:08:22 -0800 | [diff] [blame] | 753 | // createArchPropTypeDesc takes a reflect.Type that is either a struct or a pointer to a struct, and |
| 754 | // returns lists of reflect.Types that contains the arch-variant properties inside structs for each |
| 755 | // arch, multilib and target property. |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 756 | // |
| 757 | // This is a relatively expensive operation, so the results are cached in the global |
| 758 | // archPropTypeMap. It is constructed entirely based on compile-time data, so there is no need |
| 759 | // to isolate the results between multiple tests running in parallel. |
Colin Cross | cbbd13f | 2020-01-17 14:08:22 -0800 | [diff] [blame] | 760 | func createArchPropTypeDesc(props reflect.Type) []archPropTypeDesc { |
Colin Cross | b1d8c99 | 2020-01-21 11:43:29 -0800 | [diff] [blame] | 761 | // Each property struct shard will be nested many times under the runtime generated arch struct, |
| 762 | // which can hit the limit of 64kB for the name of runtime generated structs. They are nested |
| 763 | // 97 times now, which may grow in the future, plus there is some overhead for the containing |
| 764 | // type. This number may need to be reduced if too many are added, but reducing it too far |
| 765 | // could cause problems if a single deeply nested property no longer fits in the name. |
| 766 | const maxArchTypeNameSize = 500 |
| 767 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 768 | // Convert the type to a new set of types that contains only the arch-specific properties |
| 769 | // (those that are tagged with `android:"arch_specific"`), and sharded into multiple types |
| 770 | // to keep the runtime-generated names under the limit. |
Colin Cross | b1d8c99 | 2020-01-21 11:43:29 -0800 | [diff] [blame] | 771 | propShards, _ := proptools.FilterPropertyStructSharded(props, maxArchTypeNameSize, filterArchStruct) |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 772 | |
| 773 | // If the type has no arch-specific properties there is nothing to do. |
Colin Cross | cb98807 | 2019-01-24 14:58:11 -0800 | [diff] [blame] | 774 | if len(propShards) == 0 { |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 775 | return nil |
| 776 | } |
| 777 | |
Colin Cross | cbbd13f | 2020-01-17 14:08:22 -0800 | [diff] [blame] | 778 | var ret []archPropTypeDesc |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 779 | for _, props := range propShards { |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 780 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 781 | // variantFields takes a list of variant property field names and returns a list the |
| 782 | // StructFields with the names and the type of the current shard. |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 783 | variantFields := func(names []string) []reflect.StructField { |
| 784 | ret := make([]reflect.StructField, len(names)) |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 785 | |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 786 | for i, name := range names { |
| 787 | ret[i].Name = name |
| 788 | ret[i].Type = props |
Dan Willemsen | 866b563 | 2017-09-22 12:28:24 -0700 | [diff] [blame] | 789 | } |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 790 | |
| 791 | return ret |
| 792 | } |
| 793 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 794 | // Create a type that contains the properties in this shard repeated for each |
| 795 | // architecture, architecture variant, and architecture feature. |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 796 | archFields := make([]reflect.StructField, len(archTypeList)) |
| 797 | for i, arch := range archTypeList { |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 798 | var variants []string |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 799 | |
| 800 | for _, archVariant := range archVariants[arch] { |
| 801 | archVariant := variantReplacer.Replace(archVariant) |
| 802 | variants = append(variants, proptools.FieldNameForProperty(archVariant)) |
| 803 | } |
| 804 | for _, feature := range archFeatures[arch] { |
| 805 | feature := variantReplacer.Replace(feature) |
| 806 | variants = append(variants, proptools.FieldNameForProperty(feature)) |
| 807 | } |
| 808 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 809 | // Create the StructFields for each architecture variant architecture feature |
| 810 | // (e.g. "arch.arm.cortex-a53" or "arch.arm.neon"). |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 811 | fields := variantFields(variants) |
| 812 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 813 | // Create the StructField for the architecture itself (e.g. "arch.arm"). The special |
| 814 | // "BlueprintEmbed" name is used by Blueprint to put the properties in the |
| 815 | // parent struct. |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 816 | fields = append([]reflect.StructField{{ |
| 817 | Name: "BlueprintEmbed", |
| 818 | Type: props, |
| 819 | Anonymous: true, |
| 820 | }}, fields...) |
| 821 | |
| 822 | archFields[i] = reflect.StructField{ |
| 823 | Name: arch.Field, |
| 824 | Type: reflect.StructOf(fields), |
| 825 | } |
| 826 | } |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 827 | |
| 828 | // Create the type of the "arch" property struct for this shard. |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 829 | archType := reflect.StructOf(archFields) |
| 830 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 831 | // Create the type for the "multilib" property struct for this shard, containing the |
| 832 | // "multilib.lib32" and "multilib.lib64" property structs. |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 833 | multilibType := reflect.StructOf(variantFields([]string{"Lib32", "Lib64"})) |
| 834 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 835 | // Start with a list of the special targets |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 836 | targets := []string{ |
| 837 | "Host", |
| 838 | "Android64", |
| 839 | "Android32", |
| 840 | "Bionic", |
| 841 | "Linux", |
| 842 | "Not_windows", |
| 843 | "Arm_on_x86", |
| 844 | "Arm_on_x86_64", |
Victor Khimenko | c26fcf4 | 2020-05-07 22:16:33 +0200 | [diff] [blame] | 845 | "Native_bridge", |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 846 | } |
Jingwen Chen | 2f6a21e | 2021-04-05 07:33:05 +0000 | [diff] [blame] | 847 | for _, os := range osTypeList { |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 848 | // Add all the OSes. |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 849 | targets = append(targets, os.Field) |
| 850 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 851 | // Add the OS/Arch combinations, e.g. "android_arm64". |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 852 | for _, archType := range osArchTypeMap[os] { |
| 853 | targets = append(targets, os.Field+"_"+archType.Name) |
| 854 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 855 | // Also add the special "linux_<arch>" and "bionic_<arch>" property structs. |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 856 | if os.Linux() { |
| 857 | target := "Linux_" + archType.Name |
| 858 | if !InList(target, targets) { |
| 859 | targets = append(targets, target) |
| 860 | } |
| 861 | } |
| 862 | if os.Bionic() { |
| 863 | target := "Bionic_" + archType.Name |
| 864 | if !InList(target, targets) { |
| 865 | targets = append(targets, target) |
| 866 | } |
Dan Willemsen | 866b563 | 2017-09-22 12:28:24 -0700 | [diff] [blame] | 867 | } |
| 868 | } |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 869 | } |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 870 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 871 | // Create the type for the "target" property struct for this shard. |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 872 | targetType := reflect.StructOf(variantFields(targets)) |
Colin Cross | cbbd13f | 2020-01-17 14:08:22 -0800 | [diff] [blame] | 873 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 874 | // Return a descriptor of the 3 runtime-created types. |
Colin Cross | cbbd13f | 2020-01-17 14:08:22 -0800 | [diff] [blame] | 875 | ret = append(ret, archPropTypeDesc{ |
| 876 | arch: reflect.PtrTo(archType), |
| 877 | multilib: reflect.PtrTo(multilibType), |
| 878 | target: reflect.PtrTo(targetType), |
| 879 | }) |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 880 | } |
| 881 | return ret |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 882 | } |
| 883 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 884 | // variantReplacer converts architecture variant or architecture feature names into names that |
| 885 | // are valid for an Android.bp file. |
| 886 | var variantReplacer = strings.NewReplacer("-", "_", ".", "_") |
| 887 | |
| 888 | // filterArchStruct returns true if the given field is an architecture specific property. |
Colin Cross | 7444910 | 2019-09-25 11:26:40 -0700 | [diff] [blame] | 889 | func filterArchStruct(field reflect.StructField, prefix string) (bool, reflect.StructField) { |
| 890 | if proptools.HasTag(field, "android", "arch_variant") { |
| 891 | // The arch_variant field isn't necessary past this point |
| 892 | // Instead of wasting space, just remove it. Go also has a |
| 893 | // 16-bit limit on structure name length. The name is constructed |
| 894 | // based on the Go source representation of the structure, so |
| 895 | // the tag names count towards that length. |
Colin Cross | b4fecbf | 2020-01-21 11:38:47 -0800 | [diff] [blame] | 896 | |
| 897 | androidTag := field.Tag.Get("android") |
| 898 | values := strings.Split(androidTag, ",") |
| 899 | |
| 900 | if string(field.Tag) != `android:"`+strings.Join(values, ",")+`"` { |
| 901 | panic(fmt.Errorf("unexpected tag format %q", field.Tag)) |
Colin Cross | 7444910 | 2019-09-25 11:26:40 -0700 | [diff] [blame] | 902 | } |
Colin Cross | b4fecbf | 2020-01-21 11:38:47 -0800 | [diff] [blame] | 903 | // these tags don't need to be present in the runtime generated struct type. |
| 904 | values = RemoveListFromList(values, []string{"arch_variant", "variant_prepend", "path"}) |
| 905 | if len(values) > 0 { |
| 906 | panic(fmt.Errorf("unknown tags %q in field %q", values, prefix+field.Name)) |
| 907 | } |
| 908 | |
| 909 | field.Tag = "" |
Colin Cross | 7444910 | 2019-09-25 11:26:40 -0700 | [diff] [blame] | 910 | return true, field |
| 911 | } |
| 912 | return false, field |
| 913 | } |
| 914 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 915 | // archPropTypeMap contains a cache of the results of createArchPropTypeDesc for each type. It is |
| 916 | // shared across all Contexts, but is constructed based only on compile-time information so there |
| 917 | // is no risk of contaminating one Context with data from another. |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 918 | var archPropTypeMap OncePer |
| 919 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 920 | // initArchModule adds the architecture-specific property structs to a Module. |
| 921 | func initArchModule(m Module) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 922 | |
| 923 | base := m.base() |
| 924 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 925 | // Store the original list of top level property structs |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 926 | base.generalProperties = m.GetProperties() |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 927 | |
| 928 | for _, properties := range base.generalProperties { |
| 929 | propertiesValue := reflect.ValueOf(properties) |
Colin Cross | 62496a0 | 2016-08-08 15:49:17 -0700 | [diff] [blame] | 930 | t := propertiesValue.Type() |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 931 | if propertiesValue.Kind() != reflect.Ptr { |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 932 | panic(fmt.Errorf("properties must be a pointer to a struct, got %T", |
| 933 | propertiesValue.Interface())) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | propertiesValue = propertiesValue.Elem() |
| 937 | if propertiesValue.Kind() != reflect.Struct { |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 938 | panic(fmt.Errorf("properties must be a pointer to a struct, got %T", |
| 939 | propertiesValue.Interface())) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 940 | } |
| 941 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 942 | // Get or create the arch-specific property struct types for this property struct type. |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 943 | archPropTypes := archPropTypeMap.Once(NewCustomOnceKey(t), func() interface{} { |
Colin Cross | cbbd13f | 2020-01-17 14:08:22 -0800 | [diff] [blame] | 944 | return createArchPropTypeDesc(t) |
| 945 | }).([]archPropTypeDesc) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 946 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 947 | // Instantiate one of each arch-specific property struct type and add it to the |
| 948 | // properties for the Module. |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 949 | var archProperties []interface{} |
| 950 | for _, t := range archPropTypes { |
Colin Cross | cbbd13f | 2020-01-17 14:08:22 -0800 | [diff] [blame] | 951 | archProperties = append(archProperties, &archPropRoot{ |
| 952 | Arch: reflect.Zero(t.arch).Interface(), |
| 953 | Multilib: reflect.Zero(t.multilib).Interface(), |
| 954 | Target: reflect.Zero(t.target).Interface(), |
| 955 | }) |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 956 | } |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 957 | base.archProperties = append(base.archProperties, archProperties) |
| 958 | m.AddProperties(archProperties...) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 959 | } |
| 960 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 961 | // Update the list of properties that can be set by a defaults module or a call to |
| 962 | // AppendMatchingProperties or PrependMatchingProperties. |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 963 | base.customizableProperties = m.GetProperties() |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 964 | } |
| 965 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 966 | // appendProperties squashes properties from the given field of the given src property struct |
| 967 | // into the dst property struct. Returns the reflect.Value of the field in the src property |
| 968 | // struct to be used for further appendProperties calls on fields of that property struct. |
Colin Cross | 4157e88 | 2019-06-06 16:57:04 -0700 | [diff] [blame] | 969 | func (m *ModuleBase) appendProperties(ctx BottomUpMutatorContext, |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 970 | dst interface{}, src reflect.Value, field, srcPrefix string) reflect.Value { |
Colin Cross | 06a931b | 2015-10-28 17:23:31 -0700 | [diff] [blame] | 971 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 972 | // Step into non-nil pointers to structs in the src value. |
Colin Cross | cbbd13f | 2020-01-17 14:08:22 -0800 | [diff] [blame] | 973 | if src.Kind() == reflect.Ptr { |
| 974 | if src.IsNil() { |
| 975 | return src |
| 976 | } |
| 977 | src = src.Elem() |
| 978 | } |
| 979 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 980 | // Find the requested field in the src struct. |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 981 | src = src.FieldByName(field) |
| 982 | if !src.IsValid() { |
Colin Cross | eeabb89 | 2015-11-20 13:07:51 -0800 | [diff] [blame] | 983 | ctx.ModuleErrorf("field %q does not exist", srcPrefix) |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 984 | return src |
Colin Cross | 85a8897 | 2015-11-23 13:29:51 -0800 | [diff] [blame] | 985 | } |
| 986 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 987 | // Save the value of the field in the src struct to return. |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 988 | ret := src |
Colin Cross | 85a8897 | 2015-11-23 13:29:51 -0800 | [diff] [blame] | 989 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 990 | // If the value of the field is a struct (as opposed to a pointer to a struct) then step |
| 991 | // into the BlueprintEmbed field. |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 992 | if src.Kind() == reflect.Struct { |
| 993 | src = src.FieldByName("BlueprintEmbed") |
Colin Cross | 06a931b | 2015-10-28 17:23:31 -0700 | [diff] [blame] | 994 | } |
| 995 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 996 | // order checks the `android:"variant_prepend"` tag to handle properties where the |
| 997 | // arch-specific value needs to come before the generic value, for example for lists of |
| 998 | // include directories. |
Colin Cross | 6ee75b6 | 2016-05-05 15:57:15 -0700 | [diff] [blame] | 999 | order := func(property string, |
| 1000 | dstField, srcField reflect.StructField, |
| 1001 | dstValue, srcValue interface{}) (proptools.Order, error) { |
| 1002 | if proptools.HasTag(dstField, "android", "variant_prepend") { |
| 1003 | return proptools.Prepend, nil |
| 1004 | } else { |
| 1005 | return proptools.Append, nil |
| 1006 | } |
| 1007 | } |
| 1008 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1009 | // Squash the located property struct into the destination property struct. |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 1010 | err := proptools.ExtendMatchingProperties([]interface{}{dst}, src.Interface(), nil, order) |
Colin Cross | 06a931b | 2015-10-28 17:23:31 -0700 | [diff] [blame] | 1011 | if err != nil { |
| 1012 | if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok { |
| 1013 | ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error()) |
| 1014 | } else { |
| 1015 | panic(err) |
| 1016 | } |
| 1017 | } |
Colin Cross | 85a8897 | 2015-11-23 13:29:51 -0800 | [diff] [blame] | 1018 | |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 1019 | return ret |
Colin Cross | 06a931b | 2015-10-28 17:23:31 -0700 | [diff] [blame] | 1020 | } |
| 1021 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1022 | // Squash the appropriate OS-specific property structs into the matching top level property structs |
| 1023 | // based on the CompileOS value that was annotated on the variant. |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 1024 | func (m *ModuleBase) setOSProperties(ctx BottomUpMutatorContext) { |
| 1025 | os := m.commonProperties.CompileOS |
| 1026 | |
| 1027 | for i := range m.generalProperties { |
| 1028 | genProps := m.generalProperties[i] |
| 1029 | if m.archProperties[i] == nil { |
| 1030 | continue |
| 1031 | } |
| 1032 | for _, archProperties := range m.archProperties[i] { |
| 1033 | archPropValues := reflect.ValueOf(archProperties).Elem() |
| 1034 | |
Colin Cross | cbbd13f | 2020-01-17 14:08:22 -0800 | [diff] [blame] | 1035 | targetProp := archPropValues.FieldByName("Target").Elem() |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 1036 | |
| 1037 | // Handle host-specific properties in the form: |
| 1038 | // target: { |
| 1039 | // host: { |
| 1040 | // key: value, |
| 1041 | // }, |
| 1042 | // }, |
Jiyong Park | 1613e55 | 2020-09-14 19:43:17 +0900 | [diff] [blame] | 1043 | if os.Class == Host { |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 1044 | field := "Host" |
| 1045 | prefix := "target.host" |
| 1046 | m.appendProperties(ctx, genProps, targetProp, field, prefix) |
| 1047 | } |
| 1048 | |
| 1049 | // Handle target OS generalities of the form: |
| 1050 | // target: { |
| 1051 | // bionic: { |
| 1052 | // key: value, |
| 1053 | // }, |
| 1054 | // } |
| 1055 | if os.Linux() { |
| 1056 | field := "Linux" |
| 1057 | prefix := "target.linux" |
| 1058 | m.appendProperties(ctx, genProps, targetProp, field, prefix) |
| 1059 | } |
| 1060 | |
| 1061 | if os.Bionic() { |
| 1062 | field := "Bionic" |
| 1063 | prefix := "target.bionic" |
| 1064 | m.appendProperties(ctx, genProps, targetProp, field, prefix) |
| 1065 | } |
| 1066 | |
| 1067 | // Handle target OS properties in the form: |
| 1068 | // target: { |
| 1069 | // linux_glibc: { |
| 1070 | // key: value, |
| 1071 | // }, |
| 1072 | // not_windows: { |
| 1073 | // key: value, |
| 1074 | // }, |
| 1075 | // android { |
| 1076 | // key: value, |
| 1077 | // }, |
| 1078 | // }, |
| 1079 | field := os.Field |
| 1080 | prefix := "target." + os.Name |
| 1081 | m.appendProperties(ctx, genProps, targetProp, field, prefix) |
| 1082 | |
Jiyong Park | 1613e55 | 2020-09-14 19:43:17 +0900 | [diff] [blame] | 1083 | if os.Class == Host && os != Windows { |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 1084 | field := "Not_windows" |
| 1085 | prefix := "target.not_windows" |
| 1086 | m.appendProperties(ctx, genProps, targetProp, field, prefix) |
| 1087 | } |
| 1088 | |
| 1089 | // Handle 64-bit device properties in the form: |
| 1090 | // target { |
| 1091 | // android64 { |
| 1092 | // key: value, |
| 1093 | // }, |
| 1094 | // android32 { |
| 1095 | // key: value, |
| 1096 | // }, |
| 1097 | // }, |
| 1098 | // WARNING: this is probably not what you want to use in your blueprints file, it selects |
| 1099 | // options for all targets on a device that supports 64-bit binaries, not just the targets |
| 1100 | // that are being compiled for 64-bit. Its expected use case is binaries like linker and |
| 1101 | // debuggerd that need to know when they are a 32-bit process running on a 64-bit device |
| 1102 | if os.Class == Device { |
| 1103 | if ctx.Config().Android64() { |
| 1104 | field := "Android64" |
| 1105 | prefix := "target.android64" |
| 1106 | m.appendProperties(ctx, genProps, targetProp, field, prefix) |
| 1107 | } else { |
| 1108 | field := "Android32" |
| 1109 | prefix := "target.android32" |
| 1110 | m.appendProperties(ctx, genProps, targetProp, field, prefix) |
| 1111 | } |
| 1112 | } |
| 1113 | } |
| 1114 | } |
| 1115 | } |
| 1116 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1117 | // Squash the appropriate arch-specific property structs into the matching top level property |
| 1118 | // structs based on the CompileTarget value that was annotated on the variant. |
Colin Cross | 4157e88 | 2019-06-06 16:57:04 -0700 | [diff] [blame] | 1119 | func (m *ModuleBase) setArchProperties(ctx BottomUpMutatorContext) { |
| 1120 | arch := m.Arch() |
| 1121 | os := m.Os() |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame] | 1122 | |
Colin Cross | 4157e88 | 2019-06-06 16:57:04 -0700 | [diff] [blame] | 1123 | for i := range m.generalProperties { |
| 1124 | genProps := m.generalProperties[i] |
| 1125 | if m.archProperties[i] == nil { |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 1126 | continue |
| 1127 | } |
Colin Cross | 4157e88 | 2019-06-06 16:57:04 -0700 | [diff] [blame] | 1128 | for _, archProperties := range m.archProperties[i] { |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 1129 | archPropValues := reflect.ValueOf(archProperties).Elem() |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 1130 | |
Colin Cross | cbbd13f | 2020-01-17 14:08:22 -0800 | [diff] [blame] | 1131 | archProp := archPropValues.FieldByName("Arch").Elem() |
| 1132 | multilibProp := archPropValues.FieldByName("Multilib").Elem() |
| 1133 | targetProp := archPropValues.FieldByName("Target").Elem() |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 1134 | |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 1135 | // Handle arch-specific properties in the form: |
Colin Cross | d5934c8 | 2017-10-02 13:55:26 -0700 | [diff] [blame] | 1136 | // arch: { |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 1137 | // arm64: { |
Colin Cross | d5934c8 | 2017-10-02 13:55:26 -0700 | [diff] [blame] | 1138 | // key: value, |
| 1139 | // }, |
| 1140 | // }, |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 1141 | t := arch.ArchType |
| 1142 | |
| 1143 | if arch.ArchType != Common { |
| 1144 | field := proptools.FieldNameForProperty(t.Name) |
| 1145 | prefix := "arch." + t.Name |
Colin Cross | 4157e88 | 2019-06-06 16:57:04 -0700 | [diff] [blame] | 1146 | archStruct := m.appendProperties(ctx, genProps, archProp, field, prefix) |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 1147 | |
| 1148 | // Handle arch-variant-specific properties in the form: |
| 1149 | // arch: { |
| 1150 | // variant: { |
| 1151 | // key: value, |
| 1152 | // }, |
| 1153 | // }, |
| 1154 | v := variantReplacer.Replace(arch.ArchVariant) |
| 1155 | if v != "" { |
| 1156 | field := proptools.FieldNameForProperty(v) |
| 1157 | prefix := "arch." + t.Name + "." + v |
Colin Cross | 4157e88 | 2019-06-06 16:57:04 -0700 | [diff] [blame] | 1158 | m.appendProperties(ctx, genProps, archStruct, field, prefix) |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | // Handle cpu-variant-specific properties in the form: |
| 1162 | // arch: { |
| 1163 | // variant: { |
| 1164 | // key: value, |
| 1165 | // }, |
| 1166 | // }, |
| 1167 | if arch.CpuVariant != arch.ArchVariant { |
| 1168 | c := variantReplacer.Replace(arch.CpuVariant) |
| 1169 | if c != "" { |
| 1170 | field := proptools.FieldNameForProperty(c) |
| 1171 | prefix := "arch." + t.Name + "." + c |
Colin Cross | 4157e88 | 2019-06-06 16:57:04 -0700 | [diff] [blame] | 1172 | m.appendProperties(ctx, genProps, archStruct, field, prefix) |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 1173 | } |
| 1174 | } |
| 1175 | |
| 1176 | // Handle arch-feature-specific properties in the form: |
| 1177 | // arch: { |
| 1178 | // feature: { |
| 1179 | // key: value, |
| 1180 | // }, |
| 1181 | // }, |
| 1182 | for _, feature := range arch.ArchFeatures { |
| 1183 | field := proptools.FieldNameForProperty(feature) |
| 1184 | prefix := "arch." + t.Name + "." + feature |
Colin Cross | 4157e88 | 2019-06-06 16:57:04 -0700 | [diff] [blame] | 1185 | m.appendProperties(ctx, genProps, archStruct, field, prefix) |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | // Handle multilib-specific properties in the form: |
| 1189 | // multilib: { |
| 1190 | // lib32: { |
| 1191 | // key: value, |
| 1192 | // }, |
| 1193 | // }, |
| 1194 | field = proptools.FieldNameForProperty(t.Multilib) |
| 1195 | prefix = "multilib." + t.Multilib |
Colin Cross | 4157e88 | 2019-06-06 16:57:04 -0700 | [diff] [blame] | 1196 | m.appendProperties(ctx, genProps, multilibProp, field, prefix) |
Colin Cross | 0801633 | 2016-12-20 09:53:14 -0800 | [diff] [blame] | 1197 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1198 | |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 1199 | // Handle combined OS-feature and arch specific properties in the form: |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 1200 | // target: { |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 1201 | // bionic_x86: { |
| 1202 | // key: value, |
| 1203 | // }, |
| 1204 | // } |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 1205 | if os.Linux() && arch.ArchType != Common { |
| 1206 | field := "Linux_" + arch.ArchType.Name |
| 1207 | prefix := "target.linux_" + arch.ArchType.Name |
Colin Cross | 4157e88 | 2019-06-06 16:57:04 -0700 | [diff] [blame] | 1208 | m.appendProperties(ctx, genProps, targetProp, field, prefix) |
Colin Cross | d5934c8 | 2017-10-02 13:55:26 -0700 | [diff] [blame] | 1209 | } |
Colin Cross | c5c24ad | 2015-11-20 15:35:00 -0800 | [diff] [blame] | 1210 | |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 1211 | if os.Bionic() && arch.ArchType != Common { |
| 1212 | field := "Bionic_" + t.Name |
| 1213 | prefix := "target.bionic_" + t.Name |
Colin Cross | 4157e88 | 2019-06-06 16:57:04 -0700 | [diff] [blame] | 1214 | m.appendProperties(ctx, genProps, targetProp, field, prefix) |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 1215 | } |
| 1216 | |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 1217 | // Handle combined OS and arch specific properties in the form: |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 1218 | // target: { |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 1219 | // linux_glibc_x86: { |
| 1220 | // key: value, |
| 1221 | // }, |
| 1222 | // linux_glibc_arm: { |
| 1223 | // key: value, |
| 1224 | // }, |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 1225 | // android_arm { |
| 1226 | // key: value, |
| 1227 | // }, |
| 1228 | // android_x86 { |
Colin Cross | d5934c8 | 2017-10-02 13:55:26 -0700 | [diff] [blame] | 1229 | // key: value, |
| 1230 | // }, |
| 1231 | // }, |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 1232 | if arch.ArchType != Common { |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 1233 | field := os.Field + "_" + t.Name |
| 1234 | prefix := "target." + os.Name + "_" + t.Name |
Colin Cross | 4157e88 | 2019-06-06 16:57:04 -0700 | [diff] [blame] | 1235 | m.appendProperties(ctx, genProps, targetProp, field, prefix) |
Colin Cross | d5934c8 | 2017-10-02 13:55:26 -0700 | [diff] [blame] | 1236 | } |
| 1237 | |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 1238 | // Handle arm on x86 properties in the form: |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 1239 | // target { |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 1240 | // arm_on_x86 { |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 1241 | // key: value, |
| 1242 | // }, |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 1243 | // arm_on_x86_64 { |
Colin Cross | d5934c8 | 2017-10-02 13:55:26 -0700 | [diff] [blame] | 1244 | // key: value, |
| 1245 | // }, |
| 1246 | // }, |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 1247 | if os.Class == Device { |
Victor Khimenko | 1a31f80 | 2020-09-17 03:07:31 +0200 | [diff] [blame] | 1248 | if arch.ArchType == X86 && (hasArmAbi(arch) || |
| 1249 | hasArmAndroidArch(ctx.Config().Targets[Android])) { |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 1250 | field := "Arm_on_x86" |
| 1251 | prefix := "target.arm_on_x86" |
Colin Cross | 4157e88 | 2019-06-06 16:57:04 -0700 | [diff] [blame] | 1252 | m.appendProperties(ctx, genProps, targetProp, field, prefix) |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 1253 | } |
Victor Khimenko | 1a31f80 | 2020-09-17 03:07:31 +0200 | [diff] [blame] | 1254 | if arch.ArchType == X86_64 && (hasArmAbi(arch) || |
| 1255 | hasArmAndroidArch(ctx.Config().Targets[Android])) { |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 1256 | field := "Arm_on_x86_64" |
| 1257 | prefix := "target.arm_on_x86_64" |
Colin Cross | 4157e88 | 2019-06-06 16:57:04 -0700 | [diff] [blame] | 1258 | m.appendProperties(ctx, genProps, targetProp, field, prefix) |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 1259 | } |
Victor Khimenko | c26fcf4 | 2020-05-07 22:16:33 +0200 | [diff] [blame] | 1260 | if os == Android && m.Target().NativeBridge == NativeBridgeEnabled { |
| 1261 | field := "Native_bridge" |
| 1262 | prefix := "target.native_bridge" |
| 1263 | m.appendProperties(ctx, genProps, targetProp, field, prefix) |
| 1264 | } |
Colin Cross | 4247f0d | 2017-04-13 16:56:14 -0700 | [diff] [blame] | 1265 | } |
Colin Cross | bb2e2b7 | 2016-12-08 17:23:53 -0800 | [diff] [blame] | 1266 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1267 | } |
| 1268 | } |
| 1269 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1270 | // Convert the arch product variables into a list of targets for each OsType. |
Dan Willemsen | 0ef639b | 2018-10-10 17:02:29 -0700 | [diff] [blame] | 1271 | func decodeTargetProductVariables(config *config) (map[OsType][]Target, error) { |
Dan Willemsen | 45133ac | 2018-03-09 21:22:06 -0800 | [diff] [blame] | 1272 | variables := config.productVariables |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 1273 | |
Dan Willemsen | 0ef639b | 2018-10-10 17:02:29 -0700 | [diff] [blame] | 1274 | targets := make(map[OsType][]Target) |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 1275 | var targetErr error |
| 1276 | |
dimitry | 1f33e40 | 2019-03-26 12:39:31 +0100 | [diff] [blame] | 1277 | addTarget := func(os OsType, archName string, archVariant, cpuVariant *string, abi []string, |
dimitry | 8d6dde8 | 2019-07-11 10:23:53 +0200 | [diff] [blame] | 1278 | nativeBridgeEnabled NativeBridgeSupport, nativeBridgeHostArchName *string, |
| 1279 | nativeBridgeRelativePath *string) { |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 1280 | if targetErr != nil { |
| 1281 | return |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 1282 | } |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 1283 | |
Dan Willemsen | 01a3c25 | 2019-01-11 19:02:16 -0800 | [diff] [blame] | 1284 | arch, err := decodeArch(os, archName, archVariant, cpuVariant, abi) |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 1285 | if err != nil { |
| 1286 | targetErr = err |
| 1287 | return |
| 1288 | } |
dimitry | 8d6dde8 | 2019-07-11 10:23:53 +0200 | [diff] [blame] | 1289 | nativeBridgeRelativePathStr := String(nativeBridgeRelativePath) |
| 1290 | nativeBridgeHostArchNameStr := String(nativeBridgeHostArchName) |
| 1291 | |
| 1292 | // Use guest arch as relative install path by default |
| 1293 | if nativeBridgeEnabled && nativeBridgeRelativePathStr == "" { |
| 1294 | nativeBridgeRelativePathStr = arch.ArchType.String() |
| 1295 | } |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 1296 | |
Jiyong Park | 1613e55 | 2020-09-14 19:43:17 +0900 | [diff] [blame] | 1297 | // A target is considered as HostCross if it's a host target which can't run natively on |
| 1298 | // the currently configured build machine (either because the OS is different or because of |
| 1299 | // the unsupported arch) |
| 1300 | hostCross := false |
| 1301 | if os.Class == Host { |
| 1302 | var osSupported bool |
| 1303 | if os == BuildOs { |
| 1304 | osSupported = true |
| 1305 | } else if BuildOs.Linux() && os.Linux() { |
| 1306 | // LinuxBionic and Linux are compatible |
| 1307 | osSupported = true |
| 1308 | } else { |
| 1309 | osSupported = false |
| 1310 | } |
| 1311 | |
| 1312 | var archSupported bool |
| 1313 | if arch.ArchType == Common { |
| 1314 | archSupported = true |
| 1315 | } else if arch.ArchType.Name == *variables.HostArch { |
| 1316 | archSupported = true |
| 1317 | } else if variables.HostSecondaryArch != nil && arch.ArchType.Name == *variables.HostSecondaryArch { |
| 1318 | archSupported = true |
| 1319 | } else { |
| 1320 | archSupported = false |
| 1321 | } |
| 1322 | if !osSupported || !archSupported { |
| 1323 | hostCross = true |
| 1324 | } |
| 1325 | } |
| 1326 | |
Dan Willemsen | 0ef639b | 2018-10-10 17:02:29 -0700 | [diff] [blame] | 1327 | targets[os] = append(targets[os], |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 1328 | Target{ |
dimitry | 8d6dde8 | 2019-07-11 10:23:53 +0200 | [diff] [blame] | 1329 | Os: os, |
| 1330 | Arch: arch, |
| 1331 | NativeBridge: nativeBridgeEnabled, |
| 1332 | NativeBridgeHostArchName: nativeBridgeHostArchNameStr, |
| 1333 | NativeBridgeRelativePath: nativeBridgeRelativePathStr, |
Jiyong Park | 1613e55 | 2020-09-14 19:43:17 +0900 | [diff] [blame] | 1334 | HostCross: hostCross, |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 1335 | }) |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 1336 | } |
| 1337 | |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 1338 | if variables.HostArch == nil { |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 1339 | return nil, fmt.Errorf("No host primary architecture set") |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 1340 | } |
| 1341 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1342 | // The primary host target, which must always exist. |
dimitry | 8d6dde8 | 2019-07-11 10:23:53 +0200 | [diff] [blame] | 1343 | addTarget(BuildOs, *variables.HostArch, nil, nil, nil, NativeBridgeDisabled, nil, nil) |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 1344 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1345 | // An optional secondary host target. |
Colin Cross | eeabb89 | 2015-11-20 13:07:51 -0800 | [diff] [blame] | 1346 | if variables.HostSecondaryArch != nil && *variables.HostSecondaryArch != "" { |
dimitry | 8d6dde8 | 2019-07-11 10:23:53 +0200 | [diff] [blame] | 1347 | addTarget(BuildOs, *variables.HostSecondaryArch, nil, nil, nil, NativeBridgeDisabled, nil, nil) |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 1348 | } |
| 1349 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1350 | // Optional cross-compiled host targets, generally Windows. |
Colin Cross | ff3ae9d | 2018-04-10 16:15:18 -0700 | [diff] [blame] | 1351 | if String(variables.CrossHost) != "" { |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 1352 | crossHostOs := osByName(*variables.CrossHost) |
| 1353 | if crossHostOs == NoOsType { |
| 1354 | return nil, fmt.Errorf("Unknown cross host OS %q", *variables.CrossHost) |
| 1355 | } |
| 1356 | |
Colin Cross | ff3ae9d | 2018-04-10 16:15:18 -0700 | [diff] [blame] | 1357 | if String(variables.CrossHostArch) == "" { |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 1358 | return nil, fmt.Errorf("No cross-host primary architecture set") |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 1359 | } |
| 1360 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1361 | // The primary cross-compiled host target. |
dimitry | 8d6dde8 | 2019-07-11 10:23:53 +0200 | [diff] [blame] | 1362 | addTarget(crossHostOs, *variables.CrossHostArch, nil, nil, nil, NativeBridgeDisabled, nil, nil) |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 1363 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1364 | // An optional secondary cross-compiled host target. |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 1365 | if variables.CrossHostSecondaryArch != nil && *variables.CrossHostSecondaryArch != "" { |
dimitry | 8d6dde8 | 2019-07-11 10:23:53 +0200 | [diff] [blame] | 1366 | addTarget(crossHostOs, *variables.CrossHostSecondaryArch, nil, nil, nil, NativeBridgeDisabled, nil, nil) |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 1367 | } |
| 1368 | } |
| 1369 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1370 | // Optional device targets |
Dan Willemsen | 3f32f03 | 2016-07-11 14:36:48 -0700 | [diff] [blame] | 1371 | if variables.DeviceArch != nil && *variables.DeviceArch != "" { |
Doug Horn | 21b9427 | 2019-01-16 12:06:11 -0800 | [diff] [blame] | 1372 | var target = Android |
| 1373 | if Bool(variables.Fuchsia) { |
| 1374 | target = Fuchsia |
| 1375 | } |
| 1376 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1377 | // The primary device target. |
Doug Horn | 21b9427 | 2019-01-16 12:06:11 -0800 | [diff] [blame] | 1378 | addTarget(target, *variables.DeviceArch, variables.DeviceArchVariant, |
dimitry | 8d6dde8 | 2019-07-11 10:23:53 +0200 | [diff] [blame] | 1379 | variables.DeviceCpuVariant, variables.DeviceAbi, NativeBridgeDisabled, nil, nil) |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 1380 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1381 | // An optional secondary device target. |
Dan Willemsen | 3f32f03 | 2016-07-11 14:36:48 -0700 | [diff] [blame] | 1382 | if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" { |
| 1383 | addTarget(Android, *variables.DeviceSecondaryArch, |
| 1384 | variables.DeviceSecondaryArchVariant, variables.DeviceSecondaryCpuVariant, |
dimitry | 8d6dde8 | 2019-07-11 10:23:53 +0200 | [diff] [blame] | 1385 | variables.DeviceSecondaryAbi, NativeBridgeDisabled, nil, nil) |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 1386 | } |
dimitry | 1f33e40 | 2019-03-26 12:39:31 +0100 | [diff] [blame] | 1387 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1388 | // An optional NativeBridge device target. |
dimitry | 1f33e40 | 2019-03-26 12:39:31 +0100 | [diff] [blame] | 1389 | if variables.NativeBridgeArch != nil && *variables.NativeBridgeArch != "" { |
| 1390 | addTarget(Android, *variables.NativeBridgeArch, |
| 1391 | variables.NativeBridgeArchVariant, variables.NativeBridgeCpuVariant, |
dimitry | 8d6dde8 | 2019-07-11 10:23:53 +0200 | [diff] [blame] | 1392 | variables.NativeBridgeAbi, NativeBridgeEnabled, variables.DeviceArch, |
| 1393 | variables.NativeBridgeRelativePath) |
dimitry | 1f33e40 | 2019-03-26 12:39:31 +0100 | [diff] [blame] | 1394 | } |
| 1395 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1396 | // An optional secondary NativeBridge device target. |
dimitry | 1f33e40 | 2019-03-26 12:39:31 +0100 | [diff] [blame] | 1397 | if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" && |
| 1398 | variables.NativeBridgeSecondaryArch != nil && *variables.NativeBridgeSecondaryArch != "" { |
| 1399 | addTarget(Android, *variables.NativeBridgeSecondaryArch, |
| 1400 | variables.NativeBridgeSecondaryArchVariant, |
| 1401 | variables.NativeBridgeSecondaryCpuVariant, |
dimitry | 8d6dde8 | 2019-07-11 10:23:53 +0200 | [diff] [blame] | 1402 | variables.NativeBridgeSecondaryAbi, |
| 1403 | NativeBridgeEnabled, |
| 1404 | variables.DeviceSecondaryArch, |
| 1405 | variables.NativeBridgeSecondaryRelativePath) |
dimitry | 1f33e40 | 2019-03-26 12:39:31 +0100 | [diff] [blame] | 1406 | } |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 1407 | } |
| 1408 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 1409 | if targetErr != nil { |
| 1410 | return nil, targetErr |
| 1411 | } |
| 1412 | |
| 1413 | return targets, nil |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 1414 | } |
| 1415 | |
Colin Cross | bb2e2b7 | 2016-12-08 17:23:53 -0800 | [diff] [blame] | 1416 | // hasArmAbi returns true if arch has at least one arm ABI |
| 1417 | func hasArmAbi(arch Arch) bool { |
Jaewoong Jung | 3aff578 | 2020-02-11 07:54:35 -0800 | [diff] [blame] | 1418 | return PrefixInList(arch.Abi, "arm") |
Colin Cross | bb2e2b7 | 2016-12-08 17:23:53 -0800 | [diff] [blame] | 1419 | } |
| 1420 | |
dimitry | 628db6f | 2019-05-22 17:16:21 +0200 | [diff] [blame] | 1421 | // hasArmArch returns true if targets has at least non-native_bridge arm Android arch |
Colin Cross | 4247f0d | 2017-04-13 16:56:14 -0700 | [diff] [blame] | 1422 | func hasArmAndroidArch(targets []Target) bool { |
| 1423 | for _, target := range targets { |
Victor Khimenko | 1a31f80 | 2020-09-17 03:07:31 +0200 | [diff] [blame] | 1424 | if target.Os == Android && target.Arch.ArchType == Arm { |
Victor Khimenko | 5eb8ec1 | 2018-03-21 20:30:54 +0100 | [diff] [blame] | 1425 | return true |
| 1426 | } |
| 1427 | } |
| 1428 | return false |
| 1429 | } |
| 1430 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1431 | // archConfig describes a built-in configuration. |
Dan Albert | 4098deb | 2016-10-19 14:04:41 -0700 | [diff] [blame] | 1432 | type archConfig struct { |
| 1433 | arch string |
| 1434 | archVariant string |
| 1435 | cpuVariant string |
| 1436 | abi []string |
| 1437 | } |
| 1438 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1439 | // getNdkAbisConfig returns a list of archConfigs for the ABIs supported by the NDK. |
Dan Albert | 4098deb | 2016-10-19 14:04:41 -0700 | [diff] [blame] | 1440 | func getNdkAbisConfig() []archConfig { |
| 1441 | return []archConfig{ |
Dan Albert | 6bba644 | 2020-01-30 15:16:49 -0800 | [diff] [blame] | 1442 | {"arm", "armv7-a", "", []string{"armeabi-v7a"}}, |
Tamas Petz | bca786d | 2021-01-20 18:56:33 +0100 | [diff] [blame] | 1443 | {"arm64", "armv8-a-branchprot", "", []string{"arm64-v8a"}}, |
Dan Albert | 4098deb | 2016-10-19 14:04:41 -0700 | [diff] [blame] | 1444 | {"x86", "", "", []string{"x86"}}, |
| 1445 | {"x86_64", "", "", []string{"x86_64"}}, |
| 1446 | } |
| 1447 | } |
| 1448 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1449 | // getAmlAbisConfig returns a list of archConfigs for the ABIs supported by mainline modules. |
Martin Stjernholm | c1ecc43 | 2019-11-15 15:00:31 +0000 | [diff] [blame] | 1450 | func getAmlAbisConfig() []archConfig { |
| 1451 | return []archConfig{ |
Martin Stjernholm | 9368834 | 2020-10-16 21:45:10 +0100 | [diff] [blame] | 1452 | {"arm", "armv7-a-neon", "", []string{"armeabi-v7a"}}, |
Martin Stjernholm | c1ecc43 | 2019-11-15 15:00:31 +0000 | [diff] [blame] | 1453 | {"arm64", "armv8-a", "", []string{"arm64-v8a"}}, |
| 1454 | {"x86", "", "", []string{"x86"}}, |
| 1455 | {"x86_64", "", "", []string{"x86_64"}}, |
| 1456 | } |
| 1457 | } |
| 1458 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1459 | // decodeArchSettings converts a list of archConfigs into a list of Targets for the given OsType. |
Dan Willemsen | 01a3c25 | 2019-01-11 19:02:16 -0800 | [diff] [blame] | 1460 | func decodeArchSettings(os OsType, archConfigs []archConfig) ([]Target, error) { |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 1461 | var ret []Target |
Dan Willemsen | 322acaf | 2016-01-12 23:07:05 -0800 | [diff] [blame] | 1462 | |
Dan Albert | 4098deb | 2016-10-19 14:04:41 -0700 | [diff] [blame] | 1463 | for _, config := range archConfigs { |
Dan Willemsen | 01a3c25 | 2019-01-11 19:02:16 -0800 | [diff] [blame] | 1464 | arch, err := decodeArch(os, config.arch, &config.archVariant, |
Colin Cross | a74ca04 | 2019-01-31 14:31:51 -0800 | [diff] [blame] | 1465 | &config.cpuVariant, config.abi) |
Dan Willemsen | 322acaf | 2016-01-12 23:07:05 -0800 | [diff] [blame] | 1466 | if err != nil { |
| 1467 | return nil, err |
| 1468 | } |
Colin Cross | 3b19f5d | 2019-09-17 14:45:31 -0700 | [diff] [blame] | 1469 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 1470 | ret = append(ret, Target{ |
| 1471 | Os: Android, |
| 1472 | Arch: arch, |
| 1473 | }) |
Dan Willemsen | 322acaf | 2016-01-12 23:07:05 -0800 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | return ret, nil |
| 1477 | } |
| 1478 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1479 | // decodeArch converts a set of strings from product variables into an Arch struct. |
Colin Cross | a74ca04 | 2019-01-31 14:31:51 -0800 | [diff] [blame] | 1480 | func decodeArch(os OsType, arch string, archVariant, cpuVariant *string, abi []string) (Arch, error) { |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1481 | // Verify the arch is valid |
Colin Cross | eeabb89 | 2015-11-20 13:07:51 -0800 | [diff] [blame] | 1482 | archType, ok := archTypeMap[arch] |
| 1483 | if !ok { |
| 1484 | return Arch{}, fmt.Errorf("unknown arch %q", arch) |
| 1485 | } |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 1486 | |
Colin Cross | eeabb89 | 2015-11-20 13:07:51 -0800 | [diff] [blame] | 1487 | a := Arch{ |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 1488 | ArchType: archType, |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1489 | ArchVariant: String(archVariant), |
| 1490 | CpuVariant: String(cpuVariant), |
Colin Cross | a74ca04 | 2019-01-31 14:31:51 -0800 | [diff] [blame] | 1491 | Abi: abi, |
Colin Cross | eeabb89 | 2015-11-20 13:07:51 -0800 | [diff] [blame] | 1492 | } |
| 1493 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1494 | // Convert generic arch variants into the empty string. |
Colin Cross | eeabb89 | 2015-11-20 13:07:51 -0800 | [diff] [blame] | 1495 | if a.ArchVariant == a.ArchType.Name || a.ArchVariant == "generic" { |
| 1496 | a.ArchVariant = "" |
| 1497 | } |
| 1498 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1499 | // Convert generic CPU variants into the empty string. |
Colin Cross | eeabb89 | 2015-11-20 13:07:51 -0800 | [diff] [blame] | 1500 | if a.CpuVariant == a.ArchType.Name || a.CpuVariant == "generic" { |
| 1501 | a.CpuVariant = "" |
| 1502 | } |
| 1503 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1504 | // Filter empty ABIs out of the list. |
Colin Cross | eeabb89 | 2015-11-20 13:07:51 -0800 | [diff] [blame] | 1505 | for i := 0; i < len(a.Abi); i++ { |
| 1506 | if a.Abi[i] == "" { |
| 1507 | a.Abi = append(a.Abi[:i], a.Abi[i+1:]...) |
| 1508 | i-- |
| 1509 | } |
| 1510 | } |
| 1511 | |
Dan Willemsen | 01a3c25 | 2019-01-11 19:02:16 -0800 | [diff] [blame] | 1512 | if a.ArchVariant == "" { |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1513 | // Set ArchFeatures from the default arch features. |
Dan Willemsen | 01a3c25 | 2019-01-11 19:02:16 -0800 | [diff] [blame] | 1514 | if featureMap, ok := defaultArchFeatureMap[os]; ok { |
| 1515 | a.ArchFeatures = featureMap[archType] |
| 1516 | } |
| 1517 | } else { |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1518 | // Set ArchFeatures from the arch type. |
Dan Willemsen | 01a3c25 | 2019-01-11 19:02:16 -0800 | [diff] [blame] | 1519 | if featureMap, ok := archFeatureMap[archType]; ok { |
| 1520 | a.ArchFeatures = featureMap[a.ArchVariant] |
| 1521 | } |
Colin Cross | c5c24ad | 2015-11-20 15:35:00 -0800 | [diff] [blame] | 1522 | } |
| 1523 | |
Colin Cross | eeabb89 | 2015-11-20 13:07:51 -0800 | [diff] [blame] | 1524 | return a, nil |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 1525 | } |
| 1526 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1527 | // filterMultilibTargets takes a list of Targets and a multilib value and returns a new list of |
| 1528 | // Targets containing only those that have the given multilib value. |
Colin Cross | 69617d3 | 2016-09-06 10:39:07 -0700 | [diff] [blame] | 1529 | func filterMultilibTargets(targets []Target, multilib string) []Target { |
| 1530 | var ret []Target |
| 1531 | for _, t := range targets { |
| 1532 | if t.Arch.ArchType.Multilib == multilib { |
| 1533 | ret = append(ret, t) |
| 1534 | } |
| 1535 | } |
| 1536 | return ret |
| 1537 | } |
| 1538 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1539 | // getCommonTargets returns the set of Os specific common architecture targets for each Os in a list |
| 1540 | // of targets. |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 1541 | func getCommonTargets(targets []Target) []Target { |
| 1542 | var ret []Target |
| 1543 | set := make(map[string]bool) |
| 1544 | |
| 1545 | for _, t := range targets { |
| 1546 | if _, found := set[t.Os.String()]; !found { |
| 1547 | set[t.Os.String()] = true |
| 1548 | ret = append(ret, commonTargetMap[t.Os.String()]) |
| 1549 | } |
| 1550 | } |
| 1551 | |
| 1552 | return ret |
| 1553 | } |
| 1554 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1555 | // firstTarget takes a list of Targets and a list of multilib values and returns a list of Targets |
| 1556 | // that contains zero or one Target for each OsType, selecting the one that matches the earliest |
| 1557 | // filter. |
Colin Cross | 3dceee3 | 2018-09-06 10:19:57 -0700 | [diff] [blame] | 1558 | func firstTarget(targets []Target, filters ...string) []Target { |
Jiyong Park | 2210198 | 2020-09-17 19:09:58 +0900 | [diff] [blame] | 1559 | // find the first target from each OS |
| 1560 | var ret []Target |
| 1561 | hasHost := false |
| 1562 | set := make(map[OsType]bool) |
| 1563 | |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1564 | for _, filter := range filters { |
| 1565 | buildTargets := filterMultilibTargets(targets, filter) |
Jiyong Park | 2210198 | 2020-09-17 19:09:58 +0900 | [diff] [blame] | 1566 | for _, t := range buildTargets { |
| 1567 | if _, found := set[t.Os]; !found { |
| 1568 | hasHost = hasHost || (t.Os.Class == Host) |
| 1569 | set[t.Os] = true |
| 1570 | ret = append(ret, t) |
| 1571 | } |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1572 | } |
| 1573 | } |
Jiyong Park | 2210198 | 2020-09-17 19:09:58 +0900 | [diff] [blame] | 1574 | return ret |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1575 | } |
| 1576 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1577 | // decodeMultilibTargets uses the module's multilib setting to select one or more targets from a |
| 1578 | // list of Targets. |
Colin Cross | ee0bc3b | 2018-10-02 22:01:37 -0700 | [diff] [blame] | 1579 | func decodeMultilibTargets(multilib string, targets []Target, prefer32 bool) ([]Target, error) { |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1580 | var buildTargets []Target |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1581 | |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 1582 | switch multilib { |
| 1583 | case "common": |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1584 | buildTargets = getCommonTargets(targets) |
| 1585 | case "common_first": |
| 1586 | buildTargets = getCommonTargets(targets) |
| 1587 | if prefer32 { |
Colin Cross | 3dceee3 | 2018-09-06 10:19:57 -0700 | [diff] [blame] | 1588 | buildTargets = append(buildTargets, firstTarget(targets, "lib32", "lib64")...) |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1589 | } else { |
Colin Cross | 3dceee3 | 2018-09-06 10:19:57 -0700 | [diff] [blame] | 1590 | buildTargets = append(buildTargets, firstTarget(targets, "lib64", "lib32")...) |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1591 | } |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 1592 | case "both": |
Colin Cross | 8b74d17 | 2016-09-13 09:59:14 -0700 | [diff] [blame] | 1593 | if prefer32 { |
| 1594 | buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...) |
| 1595 | buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...) |
| 1596 | } else { |
| 1597 | buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...) |
| 1598 | buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...) |
| 1599 | } |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 1600 | case "32": |
Colin Cross | 69617d3 | 2016-09-06 10:39:07 -0700 | [diff] [blame] | 1601 | buildTargets = filterMultilibTargets(targets, "lib32") |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 1602 | case "64": |
Colin Cross | 69617d3 | 2016-09-06 10:39:07 -0700 | [diff] [blame] | 1603 | buildTargets = filterMultilibTargets(targets, "lib64") |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1604 | case "first": |
| 1605 | if prefer32 { |
Colin Cross | 3dceee3 | 2018-09-06 10:19:57 -0700 | [diff] [blame] | 1606 | buildTargets = firstTarget(targets, "lib32", "lib64") |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1607 | } else { |
Colin Cross | 3dceee3 | 2018-09-06 10:19:57 -0700 | [diff] [blame] | 1608 | buildTargets = firstTarget(targets, "lib64", "lib32") |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1609 | } |
Victor Chang | 9448e8f | 2020-09-14 15:34:16 +0100 | [diff] [blame] | 1610 | case "first_prefer32": |
| 1611 | buildTargets = firstTarget(targets, "lib32", "lib64") |
Colin Cross | 69617d3 | 2016-09-06 10:39:07 -0700 | [diff] [blame] | 1612 | case "prefer32": |
Colin Cross | 3dceee3 | 2018-09-06 10:19:57 -0700 | [diff] [blame] | 1613 | buildTargets = filterMultilibTargets(targets, "lib32") |
| 1614 | if len(buildTargets) == 0 { |
| 1615 | buildTargets = filterMultilibTargets(targets, "lib64") |
| 1616 | } |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 1617 | default: |
Victor Chang | 9448e8f | 2020-09-14 15:34:16 +0100 | [diff] [blame] | 1618 | return nil, fmt.Errorf(`compile_multilib must be "both", "first", "32", "64", "prefer32" or "first_prefer32" found %q`, |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 1619 | multilib) |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 1620 | } |
| 1621 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 1622 | return buildTargets, nil |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 1623 | } |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 1624 | |
| 1625 | // GetArchProperties returns a map of architectures to the values of the |
| 1626 | // properties of the 'dst' struct that are specific to that architecture. |
| 1627 | // |
| 1628 | // For example, passing a struct { Foo bool, Bar string } will return an |
| 1629 | // interface{} that can be type asserted back into the same struct, containing |
| 1630 | // the arch specific property value specified by the module if defined. |
| 1631 | func (m *ModuleBase) GetArchProperties(dst interface{}) map[ArchType]interface{} { |
| 1632 | // Return value of the arch types to the prop values for that arch. |
| 1633 | archToProp := map[ArchType]interface{}{} |
| 1634 | |
| 1635 | // Nothing to do for non-arch-specific modules. |
| 1636 | if !m.ArchSpecific() { |
| 1637 | return archToProp |
| 1638 | } |
| 1639 | |
| 1640 | // archProperties has the type of [][]interface{}. Looks complicated, so let's |
| 1641 | // explain this step by step. |
| 1642 | // |
| 1643 | // Loop over the outer index, which determines the property struct that |
| 1644 | // contains a matching set of properties in dst that we're interested in. |
| 1645 | // For example, BaseCompilerProperties or BaseLinkerProperties. |
| 1646 | for i := range m.archProperties { |
| 1647 | if m.archProperties[i] == nil { |
| 1648 | // Skip over nil arch props |
| 1649 | continue |
| 1650 | } |
| 1651 | |
| 1652 | // Non-nil arch prop, let's see if the props match up. |
| 1653 | for _, arch := range ArchTypeList() { |
| 1654 | // e.g X86, Arm |
| 1655 | field := arch.Field |
| 1656 | |
| 1657 | // If it's not nil, loop over the inner index, which determines the arch variant |
| 1658 | // of the prop type. In an Android.bp file, this is like looping over: |
| 1659 | // |
| 1660 | // arch: { arm: { key: value, ... }, x86: { key: value, ... } } |
| 1661 | for _, archProperties := range m.archProperties[i] { |
| 1662 | archPropValues := reflect.ValueOf(archProperties).Elem() |
| 1663 | |
| 1664 | // This is the archPropRoot struct. Traverse into the Arch nested struct. |
| 1665 | src := archPropValues.FieldByName("Arch").Elem() |
| 1666 | |
| 1667 | // Step into non-nil pointers to structs in the src value. |
| 1668 | if src.Kind() == reflect.Ptr { |
| 1669 | if src.IsNil() { |
| 1670 | // Ignore nil pointers. |
| 1671 | continue |
| 1672 | } |
| 1673 | src = src.Elem() |
| 1674 | } |
| 1675 | |
| 1676 | // Find the requested field (e.g. x86, x86_64) in the src struct. |
| 1677 | src = src.FieldByName(field) |
| 1678 | if !src.IsValid() { |
| 1679 | continue |
| 1680 | } |
| 1681 | |
| 1682 | // We only care about structs. These are not the droids you are looking for. |
| 1683 | if src.Kind() != reflect.Struct { |
| 1684 | continue |
| 1685 | } |
| 1686 | |
| 1687 | // If the value of the field is a struct then step into the |
| 1688 | // BlueprintEmbed field. The special "BlueprintEmbed" name is |
| 1689 | // used by createArchPropTypeDesc to embed the arch properties |
| 1690 | // in the parent struct, so the src arch prop should be in this |
| 1691 | // field. |
| 1692 | // |
| 1693 | // See createArchPropTypeDesc for more details on how Arch-specific |
| 1694 | // module properties are processed from the nested props and written |
| 1695 | // into the module's archProperties. |
| 1696 | src = src.FieldByName("BlueprintEmbed") |
| 1697 | |
| 1698 | // Clone the destination prop, since we want a unique prop struct per arch. |
| 1699 | dstClone := reflect.New(reflect.ValueOf(dst).Elem().Type()).Interface() |
| 1700 | |
| 1701 | // Copy the located property struct into the cloned destination property struct. |
| 1702 | err := proptools.ExtendMatchingProperties([]interface{}{dstClone}, src.Interface(), nil, proptools.OrderReplace) |
| 1703 | if err != nil { |
| 1704 | // This is fine, it just means the src struct doesn't match. |
| 1705 | continue |
| 1706 | } |
| 1707 | |
| 1708 | // Found the prop for the arch, you have. |
| 1709 | archToProp[arch] = dstClone |
| 1710 | |
| 1711 | // Go to the next prop. |
| 1712 | break |
| 1713 | } |
| 1714 | } |
| 1715 | } |
| 1716 | return archToProp |
| 1717 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 1718 | |
| 1719 | // GetTargetProperties returns a map of OS target (e.g. android, windows) to the |
| 1720 | // values of the properties of the 'dst' struct that are specific to that OS |
| 1721 | // target. |
| 1722 | // |
| 1723 | // For example, passing a struct { Foo bool, Bar string } will return an |
| 1724 | // interface{} that can be type asserted back into the same struct, containing |
| 1725 | // the os-specific property value specified by the module if defined. |
| 1726 | // |
| 1727 | // While this looks similar to GetArchProperties, the internal representation of |
| 1728 | // the properties have a slightly different layout to warrant a standalone |
| 1729 | // lookup function. |
| 1730 | func (m *ModuleBase) GetTargetProperties(dst interface{}) map[OsType]interface{} { |
| 1731 | // Return value of the arch types to the prop values for that arch. |
| 1732 | osToProp := map[OsType]interface{}{} |
| 1733 | |
| 1734 | // Nothing to do for non-OS/arch-specific modules. |
| 1735 | if !m.ArchSpecific() { |
| 1736 | return osToProp |
| 1737 | } |
| 1738 | |
| 1739 | // archProperties has the type of [][]interface{}. Looks complicated, so |
| 1740 | // let's explain this step by step. |
| 1741 | // |
| 1742 | // Loop over the outer index, which determines the property struct that |
| 1743 | // contains a matching set of properties in dst that we're interested in. |
| 1744 | // For example, BaseCompilerProperties or BaseLinkerProperties. |
| 1745 | for i := range m.archProperties { |
| 1746 | if m.archProperties[i] == nil { |
| 1747 | continue |
| 1748 | } |
| 1749 | |
| 1750 | // Iterate over the supported OS types |
Jingwen Chen | 2f6a21e | 2021-04-05 07:33:05 +0000 | [diff] [blame] | 1751 | for _, os := range osTypeList { |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 1752 | // e.g android, linux_bionic |
| 1753 | field := os.Field |
| 1754 | |
| 1755 | // If it's not nil, loop over the inner index, which determines the arch variant |
| 1756 | // of the prop type. In an Android.bp file, this is like looping over: |
| 1757 | // |
| 1758 | // target: { android: { key: value, ... }, linux_bionic: { key: value, ... } } |
| 1759 | for _, archProperties := range m.archProperties[i] { |
| 1760 | archPropValues := reflect.ValueOf(archProperties).Elem() |
| 1761 | |
| 1762 | // This is the archPropRoot struct. Traverse into the Targetnested struct. |
| 1763 | src := archPropValues.FieldByName("Target").Elem() |
| 1764 | |
| 1765 | // Step into non-nil pointers to structs in the src value. |
| 1766 | if src.Kind() == reflect.Ptr { |
| 1767 | if src.IsNil() { |
| 1768 | continue |
| 1769 | } |
| 1770 | src = src.Elem() |
| 1771 | } |
| 1772 | |
| 1773 | // Find the requested field (e.g. android, linux_bionic) in the src struct. |
| 1774 | src = src.FieldByName(field) |
| 1775 | |
| 1776 | // Validation steps. We want valid non-nil pointers to structs. |
| 1777 | if !src.IsValid() || src.IsNil() { |
| 1778 | continue |
| 1779 | } |
| 1780 | |
| 1781 | if src.Kind() != reflect.Ptr || src.Elem().Kind() != reflect.Struct { |
| 1782 | continue |
| 1783 | } |
| 1784 | |
| 1785 | // Clone the destination prop, since we want a unique prop struct per arch. |
| 1786 | dstClone := reflect.New(reflect.ValueOf(dst).Elem().Type()).Interface() |
| 1787 | |
| 1788 | // Copy the located property struct into the cloned destination property struct. |
| 1789 | err := proptools.ExtendMatchingProperties([]interface{}{dstClone}, src.Interface(), nil, proptools.OrderReplace) |
| 1790 | if err != nil { |
| 1791 | // This is fine, it just means the src struct doesn't match. |
| 1792 | continue |
| 1793 | } |
| 1794 | |
| 1795 | // Found the prop for the os, you have. |
| 1796 | osToProp[os] = dstClone |
| 1797 | |
| 1798 | // Go to the next prop. |
| 1799 | break |
| 1800 | } |
| 1801 | } |
| 1802 | } |
| 1803 | return osToProp |
| 1804 | } |