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" |
Cole Faust | c843b99 | 2022-08-02 18:06:50 -0700 | [diff] [blame] | 22 | "sort" |
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 | cb0ac95 | 2021-07-20 13:17:15 -0700 | [diff] [blame] | 25 | "android/soong/bazel" |
Liz Kammer | e8303bd | 2022-02-16 09:02:48 -0500 | [diff] [blame] | 26 | "android/soong/starlark_fmt" |
Colin Cross | cb0ac95 | 2021-07-20 13:17:15 -0700 | [diff] [blame] | 27 | |
Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 28 | "github.com/google/blueprint" |
Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 29 | "github.com/google/blueprint/bootstrap" |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 30 | "github.com/google/blueprint/proptools" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 31 | ) |
| 32 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 33 | /* |
| 34 | Example blueprints file containing all variant property groups, with comment listing what type |
| 35 | of variants get properties in that group: |
| 36 | |
| 37 | module { |
| 38 | arch: { |
| 39 | arm: { |
| 40 | // Host or device variants with arm architecture |
| 41 | }, |
| 42 | arm64: { |
| 43 | // Host or device variants with arm64 architecture |
| 44 | }, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 45 | x86: { |
| 46 | // Host or device variants with x86 architecture |
| 47 | }, |
| 48 | x86_64: { |
| 49 | // Host or device variants with x86_64 architecture |
| 50 | }, |
| 51 | }, |
| 52 | multilib: { |
| 53 | lib32: { |
| 54 | // Host or device variants for 32-bit architectures |
| 55 | }, |
| 56 | lib64: { |
| 57 | // Host or device variants for 64-bit architectures |
| 58 | }, |
| 59 | }, |
| 60 | target: { |
| 61 | android: { |
Martin Stjernholm | e284b48 | 2020-09-23 21:03:27 +0100 | [diff] [blame] | 62 | // Device variants (implies Bionic) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 63 | }, |
| 64 | host: { |
| 65 | // Host variants |
| 66 | }, |
Martin Stjernholm | e284b48 | 2020-09-23 21:03:27 +0100 | [diff] [blame] | 67 | bionic: { |
| 68 | // Bionic (device and host) variants |
| 69 | }, |
| 70 | linux_bionic: { |
| 71 | // Bionic host variants |
| 72 | }, |
| 73 | linux: { |
| 74 | // Bionic (device and host) and Linux glibc variants |
| 75 | }, |
Dan Willemsen | 5746bd4 | 2017-10-02 19:42:01 -0700 | [diff] [blame] | 76 | linux_glibc: { |
Martin Stjernholm | e284b48 | 2020-09-23 21:03:27 +0100 | [diff] [blame] | 77 | // Linux host variants (using non-Bionic libc) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 78 | }, |
| 79 | darwin: { |
| 80 | // Darwin host variants |
| 81 | }, |
| 82 | windows: { |
| 83 | // Windows host variants |
| 84 | }, |
| 85 | not_windows: { |
| 86 | // Non-windows host variants |
| 87 | }, |
Martin Stjernholm | e284b48 | 2020-09-23 21:03:27 +0100 | [diff] [blame] | 88 | android_arm: { |
| 89 | // Any <os>_<arch> combination restricts to that os and arch |
| 90 | }, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 91 | }, |
| 92 | } |
| 93 | */ |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 94 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 95 | // An Arch indicates a single CPU architecture. |
| 96 | type Arch struct { |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 97 | // The type of the architecture (arm, arm64, x86, or x86_64). |
| 98 | ArchType ArchType |
| 99 | |
| 100 | // The variant of the architecture, for example "armv7-a" or "armv7-a-neon" for arm. |
| 101 | ArchVariant string |
| 102 | |
| 103 | // The variant of the CPU, for example "cortex-a53" for arm64. |
| 104 | CpuVariant string |
| 105 | |
| 106 | // The list of Android app ABIs supported by the CPU architecture, for example "arm64-v8a". |
| 107 | Abi []string |
| 108 | |
| 109 | // 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] | 110 | ArchFeatures []string |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 113 | // String returns the Arch as a string. The value is used as the name of the variant created |
| 114 | // by archMutator. |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 115 | func (a Arch) String() string { |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame] | 116 | s := a.ArchType.String() |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 117 | if a.ArchVariant != "" { |
| 118 | s += "_" + a.ArchVariant |
| 119 | } |
| 120 | if a.CpuVariant != "" { |
| 121 | s += "_" + a.CpuVariant |
| 122 | } |
| 123 | return s |
| 124 | } |
| 125 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 126 | // ArchType is used to define the 4 supported architecture types (arm, arm64, x86, x86_64), as |
| 127 | // well as the "common" architecture used for modules that support multiple architectures, for |
| 128 | // example Java modules. |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 129 | type ArchType struct { |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 130 | // Name is the name of the architecture type, "arm", "arm64", "x86", or "x86_64". |
| 131 | Name string |
| 132 | |
| 133 | // Field is the name of the field used in properties that refer to the architecture, e.g. "Arm64". |
| 134 | Field string |
| 135 | |
| 136 | // 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] | 137 | Multilib string |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 140 | // String returns the name of the ArchType. |
| 141 | func (a ArchType) String() string { |
| 142 | return a.Name |
| 143 | } |
| 144 | |
| 145 | const COMMON_VARIANT = "common" |
| 146 | |
| 147 | var ( |
| 148 | archTypeList []ArchType |
| 149 | |
Colin Cross | f05b0d3 | 2022-07-14 18:10:34 -0700 | [diff] [blame] | 150 | Arm = newArch("arm", "lib32") |
| 151 | Arm64 = newArch("arm64", "lib64") |
| 152 | Riscv64 = newArch("riscv64", "lib64") |
| 153 | X86 = newArch("x86", "lib32") |
| 154 | X86_64 = newArch("x86_64", "lib64") |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 155 | |
| 156 | Common = ArchType{ |
| 157 | Name: COMMON_VARIANT, |
| 158 | } |
| 159 | ) |
| 160 | |
| 161 | var archTypeMap = map[string]ArchType{} |
| 162 | |
Colin Cross | ec19363 | 2015-07-06 17:49:43 -0700 | [diff] [blame] | 163 | func newArch(name, multilib string) ArchType { |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 164 | archType := ArchType{ |
Colin Cross | ec19363 | 2015-07-06 17:49:43 -0700 | [diff] [blame] | 165 | Name: name, |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 166 | Field: proptools.FieldNameForProperty(name), |
Colin Cross | ec19363 | 2015-07-06 17:49:43 -0700 | [diff] [blame] | 167 | Multilib: multilib, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 168 | } |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 169 | archTypeList = append(archTypeList, archType) |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 170 | archTypeMap[name] = archType |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 171 | return archType |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Usta | eabf0f3 | 2021-12-06 15:17:23 -0500 | [diff] [blame] | 174 | // ArchTypeList returns a slice copy of the 4 supported ArchTypes for arm, |
Jingwen Chen | 2f6a21e | 2021-04-05 07:33:05 +0000 | [diff] [blame] | 175 | // arm64, x86 and x86_64. |
Jaewoong Jung | 1ce9ac6 | 2019-08-13 14:11:33 -0700 | [diff] [blame] | 176 | func ArchTypeList() []ArchType { |
| 177 | return append([]ArchType(nil), archTypeList...) |
| 178 | } |
| 179 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 180 | // MarshalText allows an ArchType to be serialized through any encoder that supports |
| 181 | // encoding.TextMarshaler. |
Colin Cross | 74ba962 | 2019-02-11 15:11:14 -0800 | [diff] [blame] | 182 | func (a ArchType) MarshalText() ([]byte, error) { |
Jeongik Cha | bec4d03 | 2021-04-15 08:55:38 +0900 | [diff] [blame] | 183 | return []byte(a.String()), nil |
Colin Cross | 74ba962 | 2019-02-11 15:11:14 -0800 | [diff] [blame] | 184 | } |
| 185 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 186 | var _ encoding.TextMarshaler = ArchType{} |
Colin Cross | 74ba962 | 2019-02-11 15:11:14 -0800 | [diff] [blame] | 187 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 188 | // UnmarshalText allows an ArchType to be deserialized through any decoder that supports |
| 189 | // encoding.TextUnmarshaler. |
Colin Cross | 74ba962 | 2019-02-11 15:11:14 -0800 | [diff] [blame] | 190 | func (a *ArchType) UnmarshalText(text []byte) error { |
| 191 | if u, ok := archTypeMap[string(text)]; ok { |
| 192 | *a = u |
| 193 | return nil |
| 194 | } |
| 195 | |
| 196 | return fmt.Errorf("unknown ArchType %q", text) |
| 197 | } |
| 198 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 199 | var _ encoding.TextUnmarshaler = &ArchType{} |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 200 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 201 | // OsClass is an enum that describes whether a variant of a module runs on the host, on the device, |
| 202 | // or is generic. |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 203 | type OsClass int |
| 204 | |
| 205 | const ( |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 206 | // Generic is used for variants of modules that are not OS-specific. |
Dan Willemsen | 0e2d97b | 2016-11-28 17:50:06 -0800 | [diff] [blame] | 207 | Generic OsClass = iota |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 208 | // Device is used for variants of modules that run on the device. |
Dan Willemsen | 0e2d97b | 2016-11-28 17:50:06 -0800 | [diff] [blame] | 209 | Device |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 210 | // Host is used for variants of modules that run on the host. |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 211 | Host |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 212 | ) |
| 213 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 214 | // String returns the OsClass as a string. |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 215 | func (class OsClass) String() string { |
| 216 | switch class { |
| 217 | case Generic: |
| 218 | return "generic" |
| 219 | case Device: |
| 220 | return "device" |
| 221 | case Host: |
| 222 | return "host" |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 223 | default: |
| 224 | panic(fmt.Errorf("unknown class %d", class)) |
| 225 | } |
| 226 | } |
| 227 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 228 | // OsType describes an OS variant of a module. |
| 229 | type OsType struct { |
| 230 | // Name is the name of the OS. It is also used as the name of the property in Android.bp |
| 231 | // files. |
| 232 | Name string |
| 233 | |
| 234 | // Field is the name of the OS converted to an exported field name, i.e. with the first |
| 235 | // character capitalized. |
| 236 | Field string |
| 237 | |
| 238 | // Class is the OsClass of the OS. |
| 239 | Class OsClass |
| 240 | |
| 241 | // DefaultDisabled is set when the module variants for the OS should not be created unless |
| 242 | // the module explicitly requests them. This is used to limit Windows cross compilation to |
| 243 | // only modules that need it. |
| 244 | DefaultDisabled bool |
| 245 | } |
| 246 | |
| 247 | // String returns the name of the OsType. |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 248 | func (os OsType) String() string { |
| 249 | return os.Name |
Colin Cross | 54c7112 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 252 | // Bionic returns true if the OS uses the Bionic libc runtime, i.e. if the OS is Android or |
| 253 | // is Linux with Bionic. |
Dan Willemsen | 866b563 | 2017-09-22 12:28:24 -0700 | [diff] [blame] | 254 | func (os OsType) Bionic() bool { |
| 255 | return os == Android || os == LinuxBionic |
| 256 | } |
| 257 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 258 | // Linux returns true if the OS uses the Linux kernel, i.e. if the OS is Android or is Linux |
| 259 | // with or without the Bionic libc runtime. |
Dan Willemsen | 866b563 | 2017-09-22 12:28:24 -0700 | [diff] [blame] | 260 | func (os OsType) Linux() bool { |
Colin Cross | 528d67e | 2021-07-23 22:23:07 +0000 | [diff] [blame] | 261 | return os == Android || os == Linux || os == LinuxBionic || os == LinuxMusl |
Dan Willemsen | 866b563 | 2017-09-22 12:28:24 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 264 | // newOsType constructs an OsType and adds it to the global lists. |
| 265 | func newOsType(name string, class OsClass, defDisabled bool, archTypes ...ArchType) OsType { |
| 266 | checkCalledFromInit() |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 267 | os := OsType{ |
| 268 | Name: name, |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 269 | Field: proptools.FieldNameForProperty(name), |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 270 | Class: class, |
Dan Willemsen | 0a37a2a | 2016-11-13 10:16:05 -0800 | [diff] [blame] | 271 | |
| 272 | DefaultDisabled: defDisabled, |
Colin Cross | 54c7112 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 273 | } |
Jingwen Chen | 2f6a21e | 2021-04-05 07:33:05 +0000 | [diff] [blame] | 274 | osTypeList = append(osTypeList, os) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 275 | |
| 276 | if _, found := commonTargetMap[name]; found { |
| 277 | panic(fmt.Errorf("Found Os type duplicate during OsType registration: %q", name)) |
| 278 | } else { |
Colin Cross | e9fe294 | 2020-11-10 18:12:15 -0800 | [diff] [blame] | 279 | commonTargetMap[name] = Target{Os: os, Arch: CommonArch} |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 280 | } |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 281 | osArchTypeMap[os] = archTypes |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 282 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 283 | return os |
| 284 | } |
| 285 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 286 | // 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] | 287 | func osByName(name string) OsType { |
Jingwen Chen | 2f6a21e | 2021-04-05 07:33:05 +0000 | [diff] [blame] | 288 | for _, os := range osTypeList { |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 289 | if os.Name == name { |
| 290 | return os |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | return NoOsType |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 295 | } |
| 296 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 297 | var ( |
Jingwen Chen | 2f6a21e | 2021-04-05 07:33:05 +0000 | [diff] [blame] | 298 | // 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] | 299 | // by the current build host or the target device. |
Jingwen Chen | 2f6a21e | 2021-04-05 07:33:05 +0000 | [diff] [blame] | 300 | osTypeList []OsType |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 301 | // commonTargetMap maps names of OsTypes to the corresponding common Target, i.e. the |
| 302 | // Target with the same OsType and the common ArchType. |
| 303 | commonTargetMap = make(map[string]Target) |
| 304 | // osArchTypeMap maps OsTypes to the list of supported ArchTypes for that OS. |
| 305 | osArchTypeMap = map[OsType][]ArchType{} |
| 306 | |
| 307 | // NoOsType is a placeholder for when no OS is needed. |
| 308 | NoOsType OsType |
| 309 | // Linux is the OS for the Linux kernel plus the glibc runtime. |
| 310 | Linux = newOsType("linux_glibc", Host, false, X86, X86_64) |
Colin Cross | 528d67e | 2021-07-23 22:23:07 +0000 | [diff] [blame] | 311 | // LinuxMusl is the OS for the Linux kernel plus the musl runtime. |
Colin Cross | a9b2aac | 2022-06-15 17:25:51 -0700 | [diff] [blame] | 312 | LinuxMusl = newOsType("linux_musl", Host, false, X86, X86_64, Arm64, Arm) |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 313 | // Darwin is the OS for MacOS/Darwin host machines. |
Dan Willemsen | 8528f4e | 2021-10-19 00:22:06 -0700 | [diff] [blame] | 314 | Darwin = newOsType("darwin", Host, false, Arm64, X86_64) |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 315 | // LinuxBionic is the OS for the Linux kernel plus the Bionic libc runtime, but without the |
| 316 | // rest of Android. |
| 317 | LinuxBionic = newOsType("linux_bionic", Host, false, Arm64, X86_64) |
| 318 | // Windows the OS for Windows host machines. |
| 319 | Windows = newOsType("windows", Host, true, X86, X86_64) |
| 320 | // Android is the OS for target devices that run all of Android, including the Linux kernel |
| 321 | // and the Bionic libc runtime. |
Colin Cross | f05b0d3 | 2022-07-14 18:10:34 -0700 | [diff] [blame] | 322 | Android = newOsType("android", Device, false, Arm, Arm64, Riscv64, X86, X86_64) |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 323 | |
| 324 | // CommonOS is a pseudo OSType for a common OS variant, which is OsType agnostic and which |
| 325 | // has dependencies on all the OS variants. |
| 326 | CommonOS = newOsType("common_os", Generic, false) |
Colin Cross | e9fe294 | 2020-11-10 18:12:15 -0800 | [diff] [blame] | 327 | |
| 328 | // CommonArch is the Arch for all modules that are os-specific but not arch specific, |
| 329 | // for example most Java modules. |
| 330 | CommonArch = Arch{ArchType: Common} |
dimitry | 1f33e40 | 2019-03-26 12:39:31 +0100 | [diff] [blame] | 331 | ) |
| 332 | |
Jingwen Chen | 2f6a21e | 2021-04-05 07:33:05 +0000 | [diff] [blame] | 333 | // OsTypeList returns a slice copy of the supported OsTypes. |
| 334 | func OsTypeList() []OsType { |
| 335 | return append([]OsType(nil), osTypeList...) |
| 336 | } |
| 337 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 338 | // 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] | 339 | type Target struct { |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 340 | // Os the OS that the module is being compiled for (e.g. "linux_glibc", "android"). |
| 341 | Os OsType |
| 342 | // Arch is the architecture that the module is being compiled for. |
| 343 | Arch Arch |
| 344 | // NativeBridge is NativeBridgeEnabled if the architecture is supported using NativeBridge |
| 345 | // (i.e. arm on x86) for this device. |
| 346 | NativeBridge NativeBridgeSupport |
| 347 | // NativeBridgeHostArchName is the name of the real architecture that is used to implement |
| 348 | // the NativeBridge architecture. For example, for arm on x86 this would be "x86". |
dimitry | 8d6dde8 | 2019-07-11 10:23:53 +0200 | [diff] [blame] | 349 | NativeBridgeHostArchName string |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 350 | // NativeBridgeRelativePath is the name of the subdirectory that will contain NativeBridge |
| 351 | // libraries and binaries. |
dimitry | 8d6dde8 | 2019-07-11 10:23:53 +0200 | [diff] [blame] | 352 | NativeBridgeRelativePath string |
Jiyong Park | 1613e55 | 2020-09-14 19:43:17 +0900 | [diff] [blame] | 353 | |
| 354 | // HostCross is true when the target cannot run natively on the current build host. |
| 355 | // For example, linux_glibc_x86 returns true on a regular x86/i686/Linux machines, but returns false |
| 356 | // on Mac (different OS), or on 64-bit only i686/Linux machines (unsupported arch). |
| 357 | HostCross bool |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 360 | // NativeBridgeSupport is an enum that specifies if a Target supports NativeBridge. |
| 361 | type NativeBridgeSupport bool |
| 362 | |
| 363 | const ( |
| 364 | NativeBridgeDisabled NativeBridgeSupport = false |
| 365 | NativeBridgeEnabled NativeBridgeSupport = true |
| 366 | ) |
| 367 | |
| 368 | // String returns the OS and arch variations used for the Target. |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 369 | func (target Target) String() string { |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 370 | return target.OsVariation() + "_" + target.ArchVariation() |
| 371 | } |
| 372 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 373 | // 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] | 374 | func (target Target) OsVariation() string { |
| 375 | return target.Os.String() |
Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 378 | // 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] | 379 | func (target Target) ArchVariation() string { |
| 380 | var variation string |
dimitry | 1f33e40 | 2019-03-26 12:39:31 +0100 | [diff] [blame] | 381 | if target.NativeBridge { |
Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 382 | variation = "native_bridge_" |
dimitry | 1f33e40 | 2019-03-26 12:39:31 +0100 | [diff] [blame] | 383 | } |
Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 384 | variation += target.Arch.String() |
| 385 | |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 386 | return variation |
Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 389 | // Variations returns a list of blueprint.Variations for the osMutator and archMutator for the |
| 390 | // Target. |
Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 391 | func (target Target) Variations() []blueprint.Variation { |
| 392 | return []blueprint.Variation{ |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 393 | {Mutator: "os", Variation: target.OsVariation()}, |
Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 394 | {Mutator: "arch", Variation: target.ArchVariation()}, |
| 395 | } |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 396 | } |
| 397 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 398 | // osMutator splits an arch-specific module into a variant for each OS that is enabled for the |
| 399 | // module. It uses the HostOrDevice value passed to InitAndroidArchModule and the |
| 400 | // device_supported and host_supported properties to determine which OsTypes are enabled for this |
| 401 | // module, then searches through the Targets to determine which have enabled Targets for this |
| 402 | // module. |
Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 403 | func osMutator(bpctx blueprint.BottomUpMutatorContext) { |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 404 | var module Module |
| 405 | var ok bool |
Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 406 | if module, ok = bpctx.Module().(Module); !ok { |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 407 | // The module is not a Soong module, it is a Blueprint module. |
Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 408 | if bootstrap.IsBootstrapModule(bpctx.Module()) { |
| 409 | // Bootstrap Go modules are always the build OS or linux bionic. |
| 410 | config := bpctx.Config().(Config) |
| 411 | osNames := []string{config.BuildOSTarget.OsVariation()} |
| 412 | for _, hostCrossTarget := range config.Targets[LinuxBionic] { |
| 413 | if hostCrossTarget.Arch.ArchType == config.BuildOSTarget.Arch.ArchType { |
| 414 | osNames = append(osNames, hostCrossTarget.OsVariation()) |
| 415 | } |
| 416 | } |
| 417 | osNames = FirstUniqueStrings(osNames) |
| 418 | bpctx.CreateVariations(osNames...) |
| 419 | } |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 420 | return |
| 421 | } |
| 422 | |
Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 423 | // Bootstrap Go module support above requires this mutator to be a |
| 424 | // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext |
| 425 | // filters out non-Soong modules. Now that we've handled them, create a |
| 426 | // normal android.BottomUpMutatorContext. |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 427 | mctx := bottomUpMutatorContextFactory(bpctx, module, false, false) |
Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 428 | |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 429 | base := module.base() |
| 430 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 431 | // 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] | 432 | if !base.ArchSpecific() { |
| 433 | return |
| 434 | } |
| 435 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 436 | // Collect a list of OSTypes supported by this module based on the HostOrDevice value |
| 437 | // passed to InitAndroidArchModule and the device_supported and host_supported properties. |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 438 | var moduleOSList []OsType |
Jingwen Chen | 2f6a21e | 2021-04-05 07:33:05 +0000 | [diff] [blame] | 439 | for _, os := range osTypeList { |
Jiyong Park | 1613e55 | 2020-09-14 19:43:17 +0900 | [diff] [blame] | 440 | for _, t := range mctx.Config().Targets[os] { |
Colin Cross | 08d6f8f | 2020-11-19 02:33:19 +0000 | [diff] [blame] | 441 | if base.supportsTarget(t) { |
Jiyong Park | 1613e55 | 2020-09-14 19:43:17 +0900 | [diff] [blame] | 442 | moduleOSList = append(moduleOSList, os) |
| 443 | break |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 444 | } |
| 445 | } |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 448 | // If there are no supported OSes then disable the module. |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 449 | if len(moduleOSList) == 0 { |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 450 | base.Disable() |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 451 | return |
| 452 | } |
| 453 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 454 | // Convert the list of supported OsTypes to the variation names. |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 455 | osNames := make([]string, len(moduleOSList)) |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 456 | for i, os := range moduleOSList { |
| 457 | osNames[i] = os.String() |
| 458 | } |
| 459 | |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 460 | createCommonOSVariant := base.commonProperties.CreateCommonOSVariant |
| 461 | if createCommonOSVariant { |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 462 | // 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] | 463 | // create. It needs to be added to the end because it needs to depend on the |
| 464 | // the other variants in the list returned by CreateVariations(...) and inter |
| 465 | // variant dependencies can only be created from a later variant in that list to |
| 466 | // an earlier one. That is because variants are always processed in the order in |
| 467 | // which they are returned from CreateVariations(...). |
| 468 | osNames = append(osNames, CommonOS.Name) |
| 469 | moduleOSList = append(moduleOSList, CommonOS) |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 472 | // Create the variations, annotate each one with which OS it was created for, and |
| 473 | // squash the appropriate OS-specific properties into the top level properties. |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 474 | modules := mctx.CreateVariations(osNames...) |
| 475 | for i, m := range modules { |
| 476 | m.base().commonProperties.CompileOS = moduleOSList[i] |
| 477 | m.base().setOSProperties(mctx) |
| 478 | } |
| 479 | |
| 480 | if createCommonOSVariant { |
| 481 | // A CommonOS variant was requested so add dependencies from it (the last one in |
| 482 | // the list) to the OS type specific variants. |
| 483 | last := len(modules) - 1 |
| 484 | commonOSVariant := modules[last] |
| 485 | commonOSVariant.base().commonProperties.CommonOSVariant = true |
| 486 | for _, module := range modules[0:last] { |
| 487 | // Ignore modules that are enabled. Note, this will only avoid adding |
| 488 | // dependencies on OsType variants that are explicitly disabled in their |
| 489 | // properties. The CommonOS variant will still depend on disabled variants |
| 490 | // if they are disabled afterwards, e.g. in archMutator if |
| 491 | if module.Enabled() { |
| 492 | mctx.AddInterVariantDependency(commonOsToOsSpecificVariantTag, commonOSVariant, module) |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | |
Colin Cross | c179ea6 | 2020-10-09 10:54:15 -0700 | [diff] [blame] | 498 | type archDepTag struct { |
| 499 | blueprint.BaseDependencyTag |
| 500 | name string |
| 501 | } |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 502 | |
Colin Cross | c179ea6 | 2020-10-09 10:54:15 -0700 | [diff] [blame] | 503 | // Identifies the dependency from CommonOS variant to the os specific variants. |
| 504 | var commonOsToOsSpecificVariantTag = archDepTag{name: "common os to os specific"} |
| 505 | |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 506 | // Get the OsType specific variants for the current CommonOS variant. |
| 507 | // |
| 508 | // The returned list will only contain enabled OsType specific variants of the |
| 509 | // module referenced in the supplied context. An empty list is returned if there |
| 510 | // are no enabled variants or the supplied context is not for an CommonOS |
| 511 | // variant. |
| 512 | func GetOsSpecificVariantsOfCommonOSVariant(mctx BaseModuleContext) []Module { |
| 513 | var variants []Module |
| 514 | mctx.VisitDirectDeps(func(m Module) { |
| 515 | if mctx.OtherModuleDependencyTag(m) == commonOsToOsSpecificVariantTag { |
| 516 | if m.Enabled() { |
| 517 | variants = append(variants, m) |
| 518 | } |
| 519 | } |
| 520 | }) |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 521 | return variants |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 522 | } |
| 523 | |
Dan Willemsen | 4745007 | 2021-10-19 20:24:49 -0700 | [diff] [blame] | 524 | var DarwinUniversalVariantTag = archDepTag{name: "darwin universal binary"} |
| 525 | |
Colin Cross | ee0bc3b | 2018-10-02 22:01:37 -0700 | [diff] [blame] | 526 | // 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] | 527 | // for a module is in three levels, OsClass, multilib, and then Target. |
Colin Cross | ee0bc3b | 2018-10-02 22:01:37 -0700 | [diff] [blame] | 528 | // OsClass selection is determined by: |
Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 529 | // - The HostOrDeviceSupported value passed in to InitAndroidArchModule by the module type factory, which selects |
| 530 | // whether the module type can compile for host, device or both. |
| 531 | // - The host_supported and device_supported properties on the module. |
| 532 | // |
Roland Levillain | f5b635d | 2019-06-05 14:42:57 +0100 | [diff] [blame] | 533 | // 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] | 534 | // for the module, the Device OsClass is selected. |
| 535 | // Within each selected OsClass, the multilib selection is determined by: |
Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 536 | // - The compile_multilib property if it set (which may be overridden by target.android.compile_multilib or |
| 537 | // target.host.compile_multilib). |
| 538 | // - The default multilib passed to InitAndroidArchModule if compile_multilib was not set. |
| 539 | // |
Colin Cross | ee0bc3b | 2018-10-02 22:01:37 -0700 | [diff] [blame] | 540 | // Valid multilib values include: |
Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 541 | // |
| 542 | // "both": compile for all Targets supported by the OsClass (generally x86_64 and x86, or arm64 and arm). |
| 543 | // "first": compile for only a single preferred Target supported by the OsClass. This is generally x86_64 or arm64, |
| 544 | // but may be arm for a 32-bit only build. |
| 545 | // "32": compile for only a single 32-bit Target supported by the OsClass. |
| 546 | // "64": compile for only a single 64-bit Target supported by the OsClass. |
| 547 | // "common": compile a for a single Target that will work on all Targets supported by the OsClass (for example Java). |
| 548 | // "common_first": compile a for a Target that will work on all Targets supported by the OsClass |
| 549 | // (same as "common"), plus a second Target for the preferred Target supported by the OsClass |
| 550 | // (same as "first"). This is used for java_binary that produces a common .jar and a wrapper |
| 551 | // executable script. |
Colin Cross | ee0bc3b | 2018-10-02 22:01:37 -0700 | [diff] [blame] | 552 | // |
| 553 | // Once the list of Targets is determined, the module is split into a variant for each Target. |
| 554 | // |
| 555 | // Modules can be initialized with InitAndroidMultiTargetsArchModule, in which case they will be split by OsClass, |
| 556 | // 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] | 557 | func archMutator(bpctx blueprint.BottomUpMutatorContext) { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 558 | var module Module |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 559 | var ok bool |
Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 560 | if module, ok = bpctx.Module().(Module); !ok { |
| 561 | if bootstrap.IsBootstrapModule(bpctx.Module()) { |
| 562 | // Bootstrap Go modules are always the build architecture. |
| 563 | bpctx.CreateVariations(bpctx.Config().(Config).BuildOSTarget.ArchVariation()) |
| 564 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 565 | return |
| 566 | } |
| 567 | |
Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 568 | // Bootstrap Go module support above requires this mutator to be a |
| 569 | // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext |
| 570 | // filters out non-Soong modules. Now that we've handled them, create a |
| 571 | // normal android.BottomUpMutatorContext. |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 572 | mctx := bottomUpMutatorContextFactory(bpctx, module, false, false) |
Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 573 | |
Colin Cross | 5eca7cb | 2018-10-02 14:02:10 -0700 | [diff] [blame] | 574 | base := module.base() |
| 575 | |
| 576 | if !base.ArchSpecific() { |
Colin Cross | b9db480 | 2016-06-03 01:50:47 +0000 | [diff] [blame] | 577 | return |
| 578 | } |
| 579 | |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 580 | os := base.commonProperties.CompileOS |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 581 | if os == CommonOS { |
| 582 | // Make sure that the target related properties are initialized for the |
| 583 | // CommonOS variant. |
| 584 | addTargetProperties(module, commonTargetMap[os.Name], nil, true) |
| 585 | |
| 586 | // Do not create arch specific variants for the CommonOS variant. |
| 587 | return |
| 588 | } |
| 589 | |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 590 | osTargets := mctx.Config().Targets[os] |
Colin Cross | fb0c16e | 2019-11-20 17:12:35 -0800 | [diff] [blame] | 591 | image := base.commonProperties.ImageVariation |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 592 | // Filter NativeBridge targets unless they are explicitly supported. |
| 593 | // Skip creating native bridge variants for non-core modules. |
Paul Duffin | e3d1ae4 | 2021-09-03 17:47:17 +0100 | [diff] [blame] | 594 | if os == Android && !(base.IsNativeBridgeSupported() && image == CoreVariation) { |
Colin Cross | 83bead4 | 2019-12-18 10:45:46 -0800 | [diff] [blame] | 595 | |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 596 | var targets []Target |
| 597 | for _, t := range osTargets { |
| 598 | if !t.NativeBridge { |
| 599 | targets = append(targets, t) |
Dan Willemsen | 0ef639b | 2018-10-10 17:02:29 -0700 | [diff] [blame] | 600 | } |
| 601 | } |
Dan Willemsen | 0ef639b | 2018-10-10 17:02:29 -0700 | [diff] [blame] | 602 | |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 603 | osTargets = targets |
| 604 | } |
Colin Cross | ee0bc3b | 2018-10-02 22:01:37 -0700 | [diff] [blame] | 605 | |
Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 606 | // only the primary arch in the ramdisk / vendor_ramdisk / recovery partition |
Inseob Kim | 08758f0 | 2021-04-08 21:13:22 +0900 | [diff] [blame] | 607 | if os == Android && (module.InstallInRecovery() || module.InstallInRamdisk() || module.InstallInVendorRamdisk() || module.InstallInDebugRamdisk()) { |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 608 | osTargets = []Target{osTargets[0]} |
| 609 | } |
dimitry | 1f33e40 | 2019-03-26 12:39:31 +0100 | [diff] [blame] | 610 | |
Jaewoong Jung | 003d808 | 2021-02-24 17:39:54 -0800 | [diff] [blame] | 611 | // Windows builds always prefer 32-bit |
| 612 | prefer32 := os == Windows |
dimitry | 1f33e40 | 2019-03-26 12:39:31 +0100 | [diff] [blame] | 613 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 614 | // Determine the multilib selection for this module. |
Christopher Ferris | 98f1022 | 2022-07-13 23:16:52 -0700 | [diff] [blame] | 615 | ignorePrefer32OnDevice := mctx.Config().IgnorePrefer32OnDevice() |
| 616 | multilib, extraMultilib := decodeMultilib(base, os, ignorePrefer32OnDevice) |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 617 | |
| 618 | // Convert the multilib selection into a list of Targets. |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 619 | targets, err := decodeMultilibTargets(multilib, osTargets, prefer32) |
| 620 | if err != nil { |
| 621 | mctx.ModuleErrorf("%s", err.Error()) |
| 622 | } |
Colin Cross | 5eca7cb | 2018-10-02 14:02:10 -0700 | [diff] [blame] | 623 | |
Colin Cross | c0f0eb8 | 2022-07-19 14:41:11 -0700 | [diff] [blame] | 624 | // If there are no supported targets disable the module. |
| 625 | if len(targets) == 0 { |
| 626 | base.Disable() |
| 627 | return |
| 628 | } |
| 629 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 630 | // If the module is using extraMultilib, decode the extraMultilib selection into |
| 631 | // a separate list of Targets. |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 632 | var multiTargets []Target |
| 633 | if extraMultilib != "" { |
| 634 | multiTargets, err = decodeMultilibTargets(extraMultilib, osTargets, prefer32) |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 635 | if err != nil { |
| 636 | mctx.ModuleErrorf("%s", err.Error()) |
| 637 | } |
Colin Cross | c0f0eb8 | 2022-07-19 14:41:11 -0700 | [diff] [blame] | 638 | multiTargets = filterHostCross(multiTargets, targets[0].HostCross) |
Colin Cross | b9db480 | 2016-06-03 01:50:47 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 641 | // Recovery is always the primary architecture, filter out any other architectures. |
Inseob Kim | 20fb5d4 | 2021-02-02 20:07:58 +0900 | [diff] [blame] | 642 | // Common arch is also allowed |
Colin Cross | fb0c16e | 2019-11-20 17:12:35 -0800 | [diff] [blame] | 643 | if image == RecoveryVariation { |
| 644 | primaryArch := mctx.Config().DevicePrimaryArchType() |
Inseob Kim | 20fb5d4 | 2021-02-02 20:07:58 +0900 | [diff] [blame] | 645 | targets = filterToArch(targets, primaryArch, Common) |
| 646 | multiTargets = filterToArch(multiTargets, primaryArch, Common) |
Colin Cross | fb0c16e | 2019-11-20 17:12:35 -0800 | [diff] [blame] | 647 | } |
| 648 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 649 | // If there are no supported targets disable the module. |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 650 | if len(targets) == 0 { |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 651 | base.Disable() |
Dan Willemsen | 3f32f03 | 2016-07-11 14:36:48 -0700 | [diff] [blame] | 652 | return |
| 653 | } |
| 654 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 655 | // Convert the targets into a list of arch variation names. |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 656 | targetNames := make([]string, len(targets)) |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 657 | for i, target := range targets { |
| 658 | targetNames[i] = target.ArchVariation() |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 659 | } |
| 660 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 661 | // Create the variations, annotate each one with which Target it was created for, and |
| 662 | // squash the appropriate arch-specific properties into the top level properties. |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 663 | modules := mctx.CreateVariations(targetNames...) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 664 | for i, m := range modules { |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 665 | addTargetProperties(m, targets[i], multiTargets, i == 0) |
Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 666 | m.base().setArchProperties(mctx) |
Dan Willemsen | 8528f4e | 2021-10-19 00:22:06 -0700 | [diff] [blame] | 667 | |
| 668 | // Install support doesn't understand Darwin+Arm64 |
| 669 | if os == Darwin && targets[i].HostCross { |
| 670 | m.base().commonProperties.SkipInstall = true |
| 671 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 672 | } |
Dan Willemsen | 4745007 | 2021-10-19 20:24:49 -0700 | [diff] [blame] | 673 | |
| 674 | // Create a dependency for Darwin Universal binaries from the primary to secondary |
| 675 | // architecture. The module itself will be responsible for calling lipo to merge the outputs. |
| 676 | if os == Darwin { |
| 677 | if multilib == "darwin_universal" && len(modules) == 2 { |
| 678 | mctx.AddInterVariantDependency(DarwinUniversalVariantTag, modules[1], modules[0]) |
| 679 | } else if multilib == "darwin_universal_common_first" && len(modules) == 3 { |
| 680 | mctx.AddInterVariantDependency(DarwinUniversalVariantTag, modules[2], modules[1]) |
| 681 | } |
| 682 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 683 | } |
| 684 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 685 | // addTargetProperties annotates a variant with the Target is is being compiled for, the list |
| 686 | // of additional Targets it is supporting (if any), and whether it is the primary Target for |
| 687 | // the module. |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 688 | func addTargetProperties(m Module, target Target, multiTargets []Target, primaryTarget bool) { |
| 689 | m.base().commonProperties.CompileTarget = target |
| 690 | m.base().commonProperties.CompileMultiTargets = multiTargets |
| 691 | m.base().commonProperties.CompilePrimary = primaryTarget |
| 692 | } |
| 693 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 694 | // decodeMultilib returns the appropriate compile_multilib property for the module, or the default |
| 695 | // multilib from the factory's call to InitAndroidArchModule if none was set. For modules that |
| 696 | // called InitAndroidMultiTargetsArchModule it always returns "common" for multilib, and returns |
| 697 | // the actual multilib in extraMultilib. |
Christopher Ferris | 98f1022 | 2022-07-13 23:16:52 -0700 | [diff] [blame] | 698 | func decodeMultilib(base *ModuleBase, os OsType, ignorePrefer32OnDevice bool) (multilib, extraMultilib string) { |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 699 | // First check the "android.compile_multilib" or "host.compile_multilib" properties. |
Dan Willemsen | 4745007 | 2021-10-19 20:24:49 -0700 | [diff] [blame] | 700 | switch os.Class { |
Colin Cross | ee0bc3b | 2018-10-02 22:01:37 -0700 | [diff] [blame] | 701 | case Device: |
| 702 | multilib = String(base.commonProperties.Target.Android.Compile_multilib) |
Jiyong Park | 1613e55 | 2020-09-14 19:43:17 +0900 | [diff] [blame] | 703 | case Host: |
Colin Cross | ee0bc3b | 2018-10-02 22:01:37 -0700 | [diff] [blame] | 704 | multilib = String(base.commonProperties.Target.Host.Compile_multilib) |
| 705 | } |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 706 | |
| 707 | // If those aren't set, try the "compile_multilib" property. |
Colin Cross | ee0bc3b | 2018-10-02 22:01:37 -0700 | [diff] [blame] | 708 | if multilib == "" { |
| 709 | multilib = String(base.commonProperties.Compile_multilib) |
| 710 | } |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 711 | |
| 712 | // 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] | 713 | if multilib == "" { |
| 714 | multilib = base.commonProperties.Default_multilib |
| 715 | } |
| 716 | |
Christopher Ferris | 98f1022 | 2022-07-13 23:16:52 -0700 | [diff] [blame] | 717 | // If a device is configured with multiple targets, this option |
| 718 | // force all device targets that prefer32 to be compiled only as |
| 719 | // the first target. |
| 720 | if ignorePrefer32OnDevice && os.Class == Device && (multilib == "prefer32" || multilib == "first_prefer32") { |
| 721 | multilib = "first" |
| 722 | } |
| 723 | |
Colin Cross | ee0bc3b | 2018-10-02 22:01:37 -0700 | [diff] [blame] | 724 | if base.commonProperties.UseTargetVariants { |
Dan Willemsen | 4745007 | 2021-10-19 20:24:49 -0700 | [diff] [blame] | 725 | // Darwin has the concept of "universal binaries" which is implemented in Soong by |
| 726 | // building both x86_64 and arm64 variants, and having select module types know how to |
| 727 | // merge the outputs of their corresponding variants together into a final binary. Most |
| 728 | // module types don't need to understand this logic, as we only build a small portion |
| 729 | // of the tree for Darwin, and only module types writing macho files need to do the |
| 730 | // merging. |
| 731 | // |
| 732 | // This logic is not enabled for: |
| 733 | // "common", as it's not an arch-specific variant |
| 734 | // "32", as Darwin never has a 32-bit variant |
| 735 | // !UseTargetVariants, as the module has opted into handling the arch-specific logic on |
| 736 | // its own. |
| 737 | if os == Darwin && multilib != "common" && multilib != "32" { |
| 738 | if multilib == "common_first" { |
| 739 | multilib = "darwin_universal_common_first" |
| 740 | } else { |
| 741 | multilib = "darwin_universal" |
| 742 | } |
| 743 | } |
| 744 | |
Colin Cross | ee0bc3b | 2018-10-02 22:01:37 -0700 | [diff] [blame] | 745 | return multilib, "" |
| 746 | } else { |
| 747 | // For app modules a single arch variant will be created per OS class which is expected to handle all the |
| 748 | // selected arches. Return the common-type as multilib and any Android.bp provided multilib as extraMultilib |
| 749 | if multilib == base.commonProperties.Default_multilib { |
| 750 | multilib = "first" |
| 751 | } |
| 752 | return base.commonProperties.Default_multilib, multilib |
| 753 | } |
| 754 | } |
| 755 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 756 | // 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] | 757 | // only Targets that have the specified ArchTypes. |
| 758 | func filterToArch(targets []Target, archs ...ArchType) []Target { |
Colin Cross | fb0c16e | 2019-11-20 17:12:35 -0800 | [diff] [blame] | 759 | for i := 0; i < len(targets); i++ { |
Inseob Kim | 20fb5d4 | 2021-02-02 20:07:58 +0900 | [diff] [blame] | 760 | found := false |
| 761 | for _, arch := range archs { |
| 762 | if targets[i].Arch.ArchType == arch { |
| 763 | found = true |
| 764 | break |
| 765 | } |
| 766 | } |
| 767 | if !found { |
Colin Cross | fb0c16e | 2019-11-20 17:12:35 -0800 | [diff] [blame] | 768 | targets = append(targets[:i], targets[i+1:]...) |
| 769 | i-- |
| 770 | } |
| 771 | } |
| 772 | return targets |
| 773 | } |
| 774 | |
Colin Cross | c0f0eb8 | 2022-07-19 14:41:11 -0700 | [diff] [blame] | 775 | // filterHostCross takes a list of Targets and a hostCross value, and returns a modified list |
| 776 | // that contains only Targets that have the specified HostCross. |
| 777 | func filterHostCross(targets []Target, hostCross bool) []Target { |
| 778 | for i := 0; i < len(targets); i++ { |
| 779 | if targets[i].HostCross != hostCross { |
| 780 | targets = append(targets[:i], targets[i+1:]...) |
| 781 | i-- |
| 782 | } |
| 783 | } |
| 784 | return targets |
| 785 | } |
| 786 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 787 | // archPropRoot is a struct type used as the top level of the arch-specific properties. It |
| 788 | // contains the "arch", "multilib", and "target" property structs. It is used to split up the |
| 789 | // property structs to limit how much is allocated when a single arch-specific property group is |
| 790 | // 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] | 791 | type archPropRoot struct { |
| 792 | Arch, Multilib, Target interface{} |
| 793 | } |
| 794 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 795 | // archPropTypeDesc holds the runtime-created types for the property structs to instantiate to |
| 796 | // create an archPropRoot property struct. |
| 797 | type archPropTypeDesc struct { |
| 798 | arch, multilib, target reflect.Type |
| 799 | } |
| 800 | |
Colin Cross | cbbd13f | 2020-01-17 14:08:22 -0800 | [diff] [blame] | 801 | // createArchPropTypeDesc takes a reflect.Type that is either a struct or a pointer to a struct, and |
| 802 | // returns lists of reflect.Types that contains the arch-variant properties inside structs for each |
| 803 | // arch, multilib and target property. |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 804 | // |
| 805 | // This is a relatively expensive operation, so the results are cached in the global |
| 806 | // archPropTypeMap. It is constructed entirely based on compile-time data, so there is no need |
| 807 | // to isolate the results between multiple tests running in parallel. |
Colin Cross | cbbd13f | 2020-01-17 14:08:22 -0800 | [diff] [blame] | 808 | func createArchPropTypeDesc(props reflect.Type) []archPropTypeDesc { |
Colin Cross | b1d8c99 | 2020-01-21 11:43:29 -0800 | [diff] [blame] | 809 | // Each property struct shard will be nested many times under the runtime generated arch struct, |
| 810 | // which can hit the limit of 64kB for the name of runtime generated structs. They are nested |
| 811 | // 97 times now, which may grow in the future, plus there is some overhead for the containing |
| 812 | // type. This number may need to be reduced if too many are added, but reducing it too far |
| 813 | // could cause problems if a single deeply nested property no longer fits in the name. |
| 814 | const maxArchTypeNameSize = 500 |
| 815 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 816 | // Convert the type to a new set of types that contains only the arch-specific properties |
Usta Shrestha | 0b52d83 | 2022-02-04 21:37:39 -0500 | [diff] [blame] | 817 | // (those that are tagged with `android:"arch_variant"`), and sharded into multiple types |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 818 | // to keep the runtime-generated names under the limit. |
Colin Cross | b1d8c99 | 2020-01-21 11:43:29 -0800 | [diff] [blame] | 819 | propShards, _ := proptools.FilterPropertyStructSharded(props, maxArchTypeNameSize, filterArchStruct) |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 820 | |
| 821 | // 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] | 822 | if len(propShards) == 0 { |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 823 | return nil |
| 824 | } |
| 825 | |
Colin Cross | cbbd13f | 2020-01-17 14:08:22 -0800 | [diff] [blame] | 826 | var ret []archPropTypeDesc |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 827 | for _, props := range propShards { |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 828 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 829 | // variantFields takes a list of variant property field names and returns a list the |
| 830 | // StructFields with the names and the type of the current shard. |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 831 | variantFields := func(names []string) []reflect.StructField { |
| 832 | ret := make([]reflect.StructField, len(names)) |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 833 | |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 834 | for i, name := range names { |
| 835 | ret[i].Name = name |
| 836 | ret[i].Type = props |
Dan Willemsen | 866b563 | 2017-09-22 12:28:24 -0700 | [diff] [blame] | 837 | } |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 838 | |
| 839 | return ret |
| 840 | } |
| 841 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 842 | // Create a type that contains the properties in this shard repeated for each |
| 843 | // architecture, architecture variant, and architecture feature. |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 844 | archFields := make([]reflect.StructField, len(archTypeList)) |
| 845 | for i, arch := range archTypeList { |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 846 | var variants []string |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 847 | |
| 848 | for _, archVariant := range archVariants[arch] { |
| 849 | archVariant := variantReplacer.Replace(archVariant) |
| 850 | variants = append(variants, proptools.FieldNameForProperty(archVariant)) |
| 851 | } |
Liz Kammer | 2c2afe2 | 2022-02-11 11:35:03 -0500 | [diff] [blame] | 852 | for _, cpuVariant := range cpuVariants[arch] { |
| 853 | cpuVariant := variantReplacer.Replace(cpuVariant) |
| 854 | variants = append(variants, proptools.FieldNameForProperty(cpuVariant)) |
| 855 | } |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 856 | for _, feature := range archFeatures[arch] { |
| 857 | feature := variantReplacer.Replace(feature) |
| 858 | variants = append(variants, proptools.FieldNameForProperty(feature)) |
| 859 | } |
| 860 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 861 | // Create the StructFields for each architecture variant architecture feature |
| 862 | // (e.g. "arch.arm.cortex-a53" or "arch.arm.neon"). |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 863 | fields := variantFields(variants) |
| 864 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 865 | // Create the StructField for the architecture itself (e.g. "arch.arm"). The special |
| 866 | // "BlueprintEmbed" name is used by Blueprint to put the properties in the |
| 867 | // parent struct. |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 868 | fields = append([]reflect.StructField{{ |
| 869 | Name: "BlueprintEmbed", |
| 870 | Type: props, |
| 871 | Anonymous: true, |
| 872 | }}, fields...) |
| 873 | |
| 874 | archFields[i] = reflect.StructField{ |
| 875 | Name: arch.Field, |
| 876 | Type: reflect.StructOf(fields), |
| 877 | } |
| 878 | } |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 879 | |
| 880 | // Create the type of the "arch" property struct for this shard. |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 881 | archType := reflect.StructOf(archFields) |
| 882 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 883 | // Create the type for the "multilib" property struct for this shard, containing the |
| 884 | // "multilib.lib32" and "multilib.lib64" property structs. |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 885 | multilibType := reflect.StructOf(variantFields([]string{"Lib32", "Lib64"})) |
| 886 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 887 | // Start with a list of the special targets |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 888 | targets := []string{ |
| 889 | "Host", |
| 890 | "Android64", |
| 891 | "Android32", |
| 892 | "Bionic", |
Colin Cross | 528d67e | 2021-07-23 22:23:07 +0000 | [diff] [blame] | 893 | "Glibc", |
| 894 | "Musl", |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 895 | "Linux", |
Colin Cross | a98d36d | 2022-03-07 14:39:49 -0800 | [diff] [blame] | 896 | "Host_linux", |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 897 | "Not_windows", |
| 898 | "Arm_on_x86", |
| 899 | "Arm_on_x86_64", |
Victor Khimenko | c26fcf4 | 2020-05-07 22:16:33 +0200 | [diff] [blame] | 900 | "Native_bridge", |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 901 | } |
Jingwen Chen | 2f6a21e | 2021-04-05 07:33:05 +0000 | [diff] [blame] | 902 | for _, os := range osTypeList { |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 903 | // Add all the OSes. |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 904 | targets = append(targets, os.Field) |
| 905 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 906 | // Add the OS/Arch combinations, e.g. "android_arm64". |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 907 | for _, archType := range osArchTypeMap[os] { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 908 | targets = append(targets, GetCompoundTargetField(os, archType)) |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 909 | |
Colin Cross | 1aa45b0 | 2022-02-10 10:33:10 -0800 | [diff] [blame] | 910 | // Also add the special "linux_<arch>", "bionic_<arch>" , "glibc_<arch>", and |
| 911 | // "musl_<arch>" property structs. |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 912 | if os.Linux() { |
| 913 | target := "Linux_" + archType.Name |
| 914 | if !InList(target, targets) { |
| 915 | targets = append(targets, target) |
| 916 | } |
| 917 | } |
Colin Cross | a98d36d | 2022-03-07 14:39:49 -0800 | [diff] [blame] | 918 | if os.Linux() && os.Class == Host { |
| 919 | target := "Host_linux_" + archType.Name |
| 920 | if !InList(target, targets) { |
| 921 | targets = append(targets, target) |
| 922 | } |
| 923 | } |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 924 | if os.Bionic() { |
| 925 | target := "Bionic_" + archType.Name |
| 926 | if !InList(target, targets) { |
| 927 | targets = append(targets, target) |
| 928 | } |
Dan Willemsen | 866b563 | 2017-09-22 12:28:24 -0700 | [diff] [blame] | 929 | } |
Colin Cross | 1aa45b0 | 2022-02-10 10:33:10 -0800 | [diff] [blame] | 930 | if os == Linux { |
| 931 | target := "Glibc_" + archType.Name |
| 932 | if !InList(target, targets) { |
| 933 | targets = append(targets, target) |
| 934 | } |
| 935 | } |
| 936 | if os == LinuxMusl { |
| 937 | target := "Musl_" + archType.Name |
| 938 | if !InList(target, targets) { |
| 939 | targets = append(targets, target) |
| 940 | } |
| 941 | } |
Dan Willemsen | 866b563 | 2017-09-22 12:28:24 -0700 | [diff] [blame] | 942 | } |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 943 | } |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 944 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 945 | // Create the type for the "target" property struct for this shard. |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 946 | targetType := reflect.StructOf(variantFields(targets)) |
Colin Cross | cbbd13f | 2020-01-17 14:08:22 -0800 | [diff] [blame] | 947 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 948 | // Return a descriptor of the 3 runtime-created types. |
Colin Cross | cbbd13f | 2020-01-17 14:08:22 -0800 | [diff] [blame] | 949 | ret = append(ret, archPropTypeDesc{ |
| 950 | arch: reflect.PtrTo(archType), |
| 951 | multilib: reflect.PtrTo(multilibType), |
| 952 | target: reflect.PtrTo(targetType), |
| 953 | }) |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 954 | } |
| 955 | return ret |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 956 | } |
| 957 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 958 | // variantReplacer converts architecture variant or architecture feature names into names that |
| 959 | // are valid for an Android.bp file. |
| 960 | var variantReplacer = strings.NewReplacer("-", "_", ".", "_") |
| 961 | |
| 962 | // filterArchStruct returns true if the given field is an architecture specific property. |
Colin Cross | 7444910 | 2019-09-25 11:26:40 -0700 | [diff] [blame] | 963 | func filterArchStruct(field reflect.StructField, prefix string) (bool, reflect.StructField) { |
| 964 | if proptools.HasTag(field, "android", "arch_variant") { |
| 965 | // The arch_variant field isn't necessary past this point |
| 966 | // Instead of wasting space, just remove it. Go also has a |
| 967 | // 16-bit limit on structure name length. The name is constructed |
| 968 | // based on the Go source representation of the structure, so |
| 969 | // the tag names count towards that length. |
Colin Cross | b4fecbf | 2020-01-21 11:38:47 -0800 | [diff] [blame] | 970 | |
| 971 | androidTag := field.Tag.Get("android") |
| 972 | values := strings.Split(androidTag, ",") |
| 973 | |
| 974 | if string(field.Tag) != `android:"`+strings.Join(values, ",")+`"` { |
| 975 | panic(fmt.Errorf("unexpected tag format %q", field.Tag)) |
Colin Cross | 7444910 | 2019-09-25 11:26:40 -0700 | [diff] [blame] | 976 | } |
Colin Cross | b4fecbf | 2020-01-21 11:38:47 -0800 | [diff] [blame] | 977 | // these tags don't need to be present in the runtime generated struct type. |
Liz Kammer | ff966b1 | 2022-07-29 10:49:16 -0400 | [diff] [blame] | 978 | values = RemoveListFromList(values, []string{"arch_variant", "variant_prepend", "path"}) |
| 979 | if len(values) > 0 { |
Colin Cross | b4fecbf | 2020-01-21 11:38:47 -0800 | [diff] [blame] | 980 | panic(fmt.Errorf("unknown tags %q in field %q", values, prefix+field.Name)) |
| 981 | } |
| 982 | |
Liz Kammer | ff966b1 | 2022-07-29 10:49:16 -0400 | [diff] [blame] | 983 | field.Tag = `` |
Colin Cross | 7444910 | 2019-09-25 11:26:40 -0700 | [diff] [blame] | 984 | return true, field |
| 985 | } |
| 986 | return false, field |
| 987 | } |
| 988 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 989 | // archPropTypeMap contains a cache of the results of createArchPropTypeDesc for each type. It is |
| 990 | // shared across all Contexts, but is constructed based only on compile-time information so there |
| 991 | // is no risk of contaminating one Context with data from another. |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 992 | var archPropTypeMap OncePer |
| 993 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 994 | // initArchModule adds the architecture-specific property structs to a Module. |
| 995 | func initArchModule(m Module) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 996 | |
| 997 | base := m.base() |
| 998 | |
Usta | eabf0f3 | 2021-12-06 15:17:23 -0500 | [diff] [blame] | 999 | if len(base.archProperties) != 0 { |
| 1000 | panic(fmt.Errorf("module %s already has archProperties", m.Name())) |
| 1001 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1002 | |
Usta | eabf0f3 | 2021-12-06 15:17:23 -0500 | [diff] [blame] | 1003 | getStructType := func(properties interface{}) reflect.Type { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1004 | propertiesValue := reflect.ValueOf(properties) |
Colin Cross | 62496a0 | 2016-08-08 15:49:17 -0700 | [diff] [blame] | 1005 | t := propertiesValue.Type() |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1006 | if propertiesValue.Kind() != reflect.Ptr { |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 1007 | panic(fmt.Errorf("properties must be a pointer to a struct, got %T", |
| 1008 | propertiesValue.Interface())) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | propertiesValue = propertiesValue.Elem() |
| 1012 | if propertiesValue.Kind() != reflect.Struct { |
Usta | eabf0f3 | 2021-12-06 15:17:23 -0500 | [diff] [blame] | 1013 | panic(fmt.Errorf("properties must be a pointer to a struct, got a pointer to %T", |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 1014 | propertiesValue.Interface())) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1015 | } |
Usta | eabf0f3 | 2021-12-06 15:17:23 -0500 | [diff] [blame] | 1016 | return t |
| 1017 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1018 | |
Usta | 851a327 | 2022-01-05 23:42:33 -0500 | [diff] [blame] | 1019 | for _, properties := range m.GetProperties() { |
Usta | eabf0f3 | 2021-12-06 15:17:23 -0500 | [diff] [blame] | 1020 | t := getStructType(properties) |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1021 | // 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] | 1022 | archPropTypes := archPropTypeMap.Once(NewCustomOnceKey(t), func() interface{} { |
Colin Cross | cbbd13f | 2020-01-17 14:08:22 -0800 | [diff] [blame] | 1023 | return createArchPropTypeDesc(t) |
| 1024 | }).([]archPropTypeDesc) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1025 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1026 | // Instantiate one of each arch-specific property struct type and add it to the |
| 1027 | // properties for the Module. |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 1028 | var archProperties []interface{} |
| 1029 | for _, t := range archPropTypes { |
Colin Cross | cbbd13f | 2020-01-17 14:08:22 -0800 | [diff] [blame] | 1030 | archProperties = append(archProperties, &archPropRoot{ |
| 1031 | Arch: reflect.Zero(t.arch).Interface(), |
| 1032 | Multilib: reflect.Zero(t.multilib).Interface(), |
| 1033 | Target: reflect.Zero(t.target).Interface(), |
| 1034 | }) |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 1035 | } |
Colin Cross | c17727d | 2018-10-24 12:42:09 -0700 | [diff] [blame] | 1036 | base.archProperties = append(base.archProperties, archProperties) |
| 1037 | m.AddProperties(archProperties...) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1038 | } |
| 1039 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1040 | } |
| 1041 | |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 1042 | func maybeBlueprintEmbed(src reflect.Value) reflect.Value { |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1043 | // If the value of the field is a struct (as opposed to a pointer to a struct) then step |
| 1044 | // into the BlueprintEmbed field. |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 1045 | if src.Kind() == reflect.Struct { |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 1046 | return src.FieldByName("BlueprintEmbed") |
| 1047 | } else { |
| 1048 | return src |
Colin Cross | 06a931b | 2015-10-28 17:23:31 -0700 | [diff] [blame] | 1049 | } |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | // Merges the property struct in srcValue into dst. |
Liz Kammer | b6dbc87 | 2021-05-14 15:14:40 -0400 | [diff] [blame] | 1053 | func mergePropertyStruct(ctx ArchVariantContext, dst interface{}, srcValue reflect.Value) { |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 1054 | src := maybeBlueprintEmbed(srcValue).Interface() |
Colin Cross | 06a931b | 2015-10-28 17:23:31 -0700 | [diff] [blame] | 1055 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1056 | // order checks the `android:"variant_prepend"` tag to handle properties where the |
| 1057 | // arch-specific value needs to come before the generic value, for example for lists of |
| 1058 | // include directories. |
Colin Cross | 6ee75b6 | 2016-05-05 15:57:15 -0700 | [diff] [blame] | 1059 | order := func(property string, |
| 1060 | dstField, srcField reflect.StructField, |
| 1061 | dstValue, srcValue interface{}) (proptools.Order, error) { |
| 1062 | if proptools.HasTag(dstField, "android", "variant_prepend") { |
| 1063 | return proptools.Prepend, nil |
| 1064 | } else { |
| 1065 | return proptools.Append, nil |
| 1066 | } |
| 1067 | } |
| 1068 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1069 | // Squash the located property struct into the destination property struct. |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 1070 | err := proptools.ExtendMatchingProperties([]interface{}{dst}, src, nil, order) |
Colin Cross | 06a931b | 2015-10-28 17:23:31 -0700 | [diff] [blame] | 1071 | if err != nil { |
| 1072 | if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok { |
| 1073 | ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error()) |
| 1074 | } else { |
| 1075 | panic(err) |
| 1076 | } |
| 1077 | } |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 1078 | } |
Colin Cross | 85a8897 | 2015-11-23 13:29:51 -0800 | [diff] [blame] | 1079 | |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 1080 | // Returns the immediate child of the input property struct that corresponds to |
| 1081 | // the sub-property "field". |
Liz Kammer | b6dbc87 | 2021-05-14 15:14:40 -0400 | [diff] [blame] | 1082 | func getChildPropertyStruct(ctx ArchVariantContext, |
Lukacs T. Berki | 5f51839 | 2021-05-17 11:44:58 +0200 | [diff] [blame] | 1083 | src reflect.Value, field, userFriendlyField string) (reflect.Value, bool) { |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 1084 | |
| 1085 | // Step into non-nil pointers to structs in the src value. |
| 1086 | if src.Kind() == reflect.Ptr { |
| 1087 | if src.IsNil() { |
Lukacs T. Berki | 5f51839 | 2021-05-17 11:44:58 +0200 | [diff] [blame] | 1088 | return reflect.Value{}, false |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 1089 | } |
| 1090 | src = src.Elem() |
| 1091 | } |
| 1092 | |
| 1093 | // Find the requested field in the src struct. |
Lukacs T. Berki | 5f51839 | 2021-05-17 11:44:58 +0200 | [diff] [blame] | 1094 | child := src.FieldByName(proptools.FieldNameForProperty(field)) |
| 1095 | if !child.IsValid() { |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 1096 | ctx.ModuleErrorf("field %q does not exist", userFriendlyField) |
Lukacs T. Berki | 5f51839 | 2021-05-17 11:44:58 +0200 | [diff] [blame] | 1097 | return reflect.Value{}, false |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 1098 | } |
| 1099 | |
Lukacs T. Berki | 5f51839 | 2021-05-17 11:44:58 +0200 | [diff] [blame] | 1100 | if child.IsZero() { |
| 1101 | return reflect.Value{}, false |
| 1102 | } |
| 1103 | |
| 1104 | return child, true |
Colin Cross | 06a931b | 2015-10-28 17:23:31 -0700 | [diff] [blame] | 1105 | } |
| 1106 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1107 | // Squash the appropriate OS-specific property structs into the matching top level property structs |
| 1108 | // based on the CompileOS value that was annotated on the variant. |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 1109 | func (m *ModuleBase) setOSProperties(ctx BottomUpMutatorContext) { |
| 1110 | os := m.commonProperties.CompileOS |
| 1111 | |
Usta | dca0219 | 2021-12-20 12:56:46 -0500 | [diff] [blame] | 1112 | for i := range m.archProperties { |
Usta | 851a327 | 2022-01-05 23:42:33 -0500 | [diff] [blame] | 1113 | genProps := m.GetProperties()[i] |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 1114 | if m.archProperties[i] == nil { |
| 1115 | continue |
| 1116 | } |
| 1117 | for _, archProperties := range m.archProperties[i] { |
| 1118 | archPropValues := reflect.ValueOf(archProperties).Elem() |
| 1119 | |
Colin Cross | cbbd13f | 2020-01-17 14:08:22 -0800 | [diff] [blame] | 1120 | targetProp := archPropValues.FieldByName("Target").Elem() |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 1121 | |
| 1122 | // Handle host-specific properties in the form: |
| 1123 | // target: { |
| 1124 | // host: { |
| 1125 | // key: value, |
| 1126 | // }, |
| 1127 | // }, |
Jiyong Park | 1613e55 | 2020-09-14 19:43:17 +0900 | [diff] [blame] | 1128 | if os.Class == Host { |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 1129 | field := "Host" |
| 1130 | prefix := "target.host" |
Lukacs T. Berki | 5f51839 | 2021-05-17 11:44:58 +0200 | [diff] [blame] | 1131 | if hostProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok { |
| 1132 | mergePropertyStruct(ctx, genProps, hostProperties) |
| 1133 | } |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | // Handle target OS generalities of the form: |
| 1137 | // target: { |
| 1138 | // bionic: { |
| 1139 | // key: value, |
| 1140 | // }, |
| 1141 | // } |
| 1142 | if os.Linux() { |
| 1143 | field := "Linux" |
| 1144 | prefix := "target.linux" |
Lukacs T. Berki | 5f51839 | 2021-05-17 11:44:58 +0200 | [diff] [blame] | 1145 | if linuxProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok { |
| 1146 | mergePropertyStruct(ctx, genProps, linuxProperties) |
| 1147 | } |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 1148 | } |
| 1149 | |
Colin Cross | a98d36d | 2022-03-07 14:39:49 -0800 | [diff] [blame] | 1150 | if os.Linux() && os.Class == Host { |
| 1151 | field := "Host_linux" |
| 1152 | prefix := "target.host_linux" |
| 1153 | if linuxProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok { |
| 1154 | mergePropertyStruct(ctx, genProps, linuxProperties) |
| 1155 | } |
| 1156 | } |
| 1157 | |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 1158 | if os.Bionic() { |
| 1159 | field := "Bionic" |
| 1160 | prefix := "target.bionic" |
Lukacs T. Berki | 5f51839 | 2021-05-17 11:44:58 +0200 | [diff] [blame] | 1161 | if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok { |
| 1162 | mergePropertyStruct(ctx, genProps, bionicProperties) |
| 1163 | } |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 1164 | } |
| 1165 | |
Colin Cross | 528d67e | 2021-07-23 22:23:07 +0000 | [diff] [blame] | 1166 | if os == Linux { |
| 1167 | field := "Glibc" |
| 1168 | prefix := "target.glibc" |
| 1169 | if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok { |
| 1170 | mergePropertyStruct(ctx, genProps, bionicProperties) |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | if os == LinuxMusl { |
| 1175 | field := "Musl" |
| 1176 | prefix := "target.musl" |
| 1177 | if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok { |
| 1178 | mergePropertyStruct(ctx, genProps, bionicProperties) |
| 1179 | } |
Colin Cross | 528d67e | 2021-07-23 22:23:07 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 1182 | // Handle target OS properties in the form: |
| 1183 | // target: { |
| 1184 | // linux_glibc: { |
| 1185 | // key: value, |
| 1186 | // }, |
| 1187 | // not_windows: { |
| 1188 | // key: value, |
| 1189 | // }, |
| 1190 | // android { |
| 1191 | // key: value, |
| 1192 | // }, |
| 1193 | // }, |
| 1194 | field := os.Field |
| 1195 | prefix := "target." + os.Name |
Lukacs T. Berki | 5f51839 | 2021-05-17 11:44:58 +0200 | [diff] [blame] | 1196 | if osProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok { |
| 1197 | mergePropertyStruct(ctx, genProps, osProperties) |
| 1198 | } |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 1199 | |
Jiyong Park | 1613e55 | 2020-09-14 19:43:17 +0900 | [diff] [blame] | 1200 | if os.Class == Host && os != Windows { |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 1201 | field := "Not_windows" |
| 1202 | prefix := "target.not_windows" |
Lukacs T. Berki | 5f51839 | 2021-05-17 11:44:58 +0200 | [diff] [blame] | 1203 | if notWindowsProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok { |
| 1204 | mergePropertyStruct(ctx, genProps, notWindowsProperties) |
| 1205 | } |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | // Handle 64-bit device properties in the form: |
| 1209 | // target { |
| 1210 | // android64 { |
| 1211 | // key: value, |
| 1212 | // }, |
| 1213 | // android32 { |
| 1214 | // key: value, |
| 1215 | // }, |
| 1216 | // }, |
| 1217 | // WARNING: this is probably not what you want to use in your blueprints file, it selects |
| 1218 | // options for all targets on a device that supports 64-bit binaries, not just the targets |
| 1219 | // that are being compiled for 64-bit. Its expected use case is binaries like linker and |
| 1220 | // debuggerd that need to know when they are a 32-bit process running on a 64-bit device |
| 1221 | if os.Class == Device { |
| 1222 | if ctx.Config().Android64() { |
| 1223 | field := "Android64" |
| 1224 | prefix := "target.android64" |
Lukacs T. Berki | 5f51839 | 2021-05-17 11:44:58 +0200 | [diff] [blame] | 1225 | if android64Properties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok { |
| 1226 | mergePropertyStruct(ctx, genProps, android64Properties) |
| 1227 | } |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 1228 | } else { |
| 1229 | field := "Android32" |
| 1230 | prefix := "target.android32" |
Lukacs T. Berki | 5f51839 | 2021-05-17 11:44:58 +0200 | [diff] [blame] | 1231 | if android32Properties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok { |
| 1232 | mergePropertyStruct(ctx, genProps, android32Properties) |
| 1233 | } |
Colin Cross | a195f91 | 2019-10-16 11:07:20 -0700 | [diff] [blame] | 1234 | } |
| 1235 | } |
| 1236 | } |
| 1237 | } |
| 1238 | } |
| 1239 | |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 1240 | // Returns the struct containing the properties specific to the given |
| 1241 | // architecture type. These look like this in Blueprint files: |
Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 1242 | // |
| 1243 | // arch: { |
| 1244 | // arm64: { |
| 1245 | // key: value, |
| 1246 | // }, |
| 1247 | // }, |
| 1248 | // |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 1249 | // This struct will also contain sub-structs containing to the architecture/CPU |
| 1250 | // variants and features that themselves contain properties specific to those. |
Lukacs T. Berki | 5f51839 | 2021-05-17 11:44:58 +0200 | [diff] [blame] | 1251 | func getArchTypeStruct(ctx ArchVariantContext, archProperties interface{}, archType ArchType) (reflect.Value, bool) { |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 1252 | archPropValues := reflect.ValueOf(archProperties).Elem() |
| 1253 | archProp := archPropValues.FieldByName("Arch").Elem() |
| 1254 | prefix := "arch." + archType.Name |
Lukacs T. Berki | 5f51839 | 2021-05-17 11:44:58 +0200 | [diff] [blame] | 1255 | return getChildPropertyStruct(ctx, archProp, archType.Name, prefix) |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 1256 | } |
| 1257 | |
| 1258 | // Returns the struct containing the properties specific to a given multilib |
| 1259 | // value. These look like this in the Blueprint file: |
Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 1260 | // |
| 1261 | // multilib: { |
| 1262 | // lib32: { |
| 1263 | // key: value, |
| 1264 | // }, |
| 1265 | // }, |
Lukacs T. Berki | 5f51839 | 2021-05-17 11:44:58 +0200 | [diff] [blame] | 1266 | func getMultilibStruct(ctx ArchVariantContext, archProperties interface{}, archType ArchType) (reflect.Value, bool) { |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 1267 | archPropValues := reflect.ValueOf(archProperties).Elem() |
| 1268 | multilibProp := archPropValues.FieldByName("Multilib").Elem() |
Lukacs T. Berki | 5f51839 | 2021-05-17 11:44:58 +0200 | [diff] [blame] | 1269 | return getChildPropertyStruct(ctx, multilibProp, archType.Multilib, "multilib."+archType.Multilib) |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 1270 | } |
| 1271 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 1272 | func GetCompoundTargetField(os OsType, arch ArchType) string { |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 1273 | return os.Field + "_" + arch.Name |
| 1274 | } |
| 1275 | |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 1276 | // Returns the structs corresponding to the properties specific to the given |
| 1277 | // architecture and OS in archProperties. |
| 1278 | func getArchProperties(ctx BaseMutatorContext, archProperties interface{}, arch Arch, os OsType, nativeBridgeEnabled bool) []reflect.Value { |
| 1279 | result := make([]reflect.Value, 0) |
| 1280 | archPropValues := reflect.ValueOf(archProperties).Elem() |
| 1281 | |
| 1282 | targetProp := archPropValues.FieldByName("Target").Elem() |
| 1283 | |
| 1284 | archType := arch.ArchType |
| 1285 | |
| 1286 | if arch.ArchType != Common { |
Lukacs T. Berki | 5f51839 | 2021-05-17 11:44:58 +0200 | [diff] [blame] | 1287 | archStruct, ok := getArchTypeStruct(ctx, archProperties, arch.ArchType) |
| 1288 | if ok { |
| 1289 | result = append(result, archStruct) |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 1290 | |
Lukacs T. Berki | 5f51839 | 2021-05-17 11:44:58 +0200 | [diff] [blame] | 1291 | // Handle arch-variant-specific properties in the form: |
| 1292 | // arch: { |
| 1293 | // arm: { |
| 1294 | // variant: { |
| 1295 | // key: value, |
| 1296 | // }, |
| 1297 | // }, |
| 1298 | // }, |
| 1299 | v := variantReplacer.Replace(arch.ArchVariant) |
| 1300 | if v != "" { |
| 1301 | prefix := "arch." + archType.Name + "." + v |
| 1302 | if variantProperties, ok := getChildPropertyStruct(ctx, archStruct, v, prefix); ok { |
| 1303 | result = append(result, variantProperties) |
| 1304 | } |
| 1305 | } |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 1306 | |
Lukacs T. Berki | 5f51839 | 2021-05-17 11:44:58 +0200 | [diff] [blame] | 1307 | // Handle cpu-variant-specific properties in the form: |
| 1308 | // arch: { |
| 1309 | // arm: { |
| 1310 | // variant: { |
| 1311 | // key: value, |
| 1312 | // }, |
| 1313 | // }, |
| 1314 | // }, |
| 1315 | if arch.CpuVariant != arch.ArchVariant { |
| 1316 | c := variantReplacer.Replace(arch.CpuVariant) |
| 1317 | if c != "" { |
| 1318 | prefix := "arch." + archType.Name + "." + c |
| 1319 | if cpuVariantProperties, ok := getChildPropertyStruct(ctx, archStruct, c, prefix); ok { |
| 1320 | result = append(result, cpuVariantProperties) |
| 1321 | } |
| 1322 | } |
| 1323 | } |
| 1324 | |
| 1325 | // Handle arch-feature-specific properties in the form: |
| 1326 | // arch: { |
| 1327 | // arm: { |
| 1328 | // feature: { |
| 1329 | // key: value, |
| 1330 | // }, |
| 1331 | // }, |
| 1332 | // }, |
| 1333 | for _, feature := range arch.ArchFeatures { |
| 1334 | prefix := "arch." + archType.Name + "." + feature |
| 1335 | if featureProperties, ok := getChildPropertyStruct(ctx, archStruct, feature, prefix); ok { |
| 1336 | result = append(result, featureProperties) |
| 1337 | } |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 1338 | } |
| 1339 | } |
| 1340 | |
Lukacs T. Berki | 5f51839 | 2021-05-17 11:44:58 +0200 | [diff] [blame] | 1341 | if multilibProperties, ok := getMultilibStruct(ctx, archProperties, archType); ok { |
| 1342 | result = append(result, multilibProperties) |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 1343 | } |
| 1344 | |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 1345 | // Handle combined OS-feature and arch specific properties in the form: |
| 1346 | // target: { |
| 1347 | // bionic_x86: { |
| 1348 | // key: value, |
| 1349 | // }, |
| 1350 | // } |
| 1351 | if os.Linux() { |
| 1352 | field := "Linux_" + arch.ArchType.Name |
| 1353 | userFriendlyField := "target.linux_" + arch.ArchType.Name |
Lukacs T. Berki | 5f51839 | 2021-05-17 11:44:58 +0200 | [diff] [blame] | 1354 | if linuxProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok { |
| 1355 | result = append(result, linuxProperties) |
| 1356 | } |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 1357 | } |
| 1358 | |
| 1359 | if os.Bionic() { |
| 1360 | field := "Bionic_" + archType.Name |
| 1361 | userFriendlyField := "target.bionic_" + archType.Name |
Lukacs T. Berki | 5f51839 | 2021-05-17 11:44:58 +0200 | [diff] [blame] | 1362 | if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok { |
| 1363 | result = append(result, bionicProperties) |
| 1364 | } |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 1365 | } |
| 1366 | |
| 1367 | // Handle combined OS and arch specific properties in the form: |
| 1368 | // target: { |
| 1369 | // linux_glibc_x86: { |
| 1370 | // key: value, |
| 1371 | // }, |
| 1372 | // linux_glibc_arm: { |
| 1373 | // key: value, |
| 1374 | // }, |
| 1375 | // android_arm { |
| 1376 | // key: value, |
| 1377 | // }, |
| 1378 | // android_x86 { |
| 1379 | // key: value, |
| 1380 | // }, |
| 1381 | // }, |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 1382 | field := GetCompoundTargetField(os, archType) |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 1383 | userFriendlyField := "target." + os.Name + "_" + archType.Name |
Lukacs T. Berki | 5f51839 | 2021-05-17 11:44:58 +0200 | [diff] [blame] | 1384 | if osArchProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok { |
| 1385 | result = append(result, osArchProperties) |
| 1386 | } |
Colin Cross | 528d67e | 2021-07-23 22:23:07 +0000 | [diff] [blame] | 1387 | |
Colin Cross | 1aa45b0 | 2022-02-10 10:33:10 -0800 | [diff] [blame] | 1388 | if os == Linux { |
| 1389 | field := "Glibc_" + archType.Name |
| 1390 | userFriendlyField := "target.glibc_" + "_" + archType.Name |
| 1391 | if osArchProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok { |
| 1392 | result = append(result, osArchProperties) |
| 1393 | } |
| 1394 | } |
| 1395 | |
Colin Cross | 528d67e | 2021-07-23 22:23:07 +0000 | [diff] [blame] | 1396 | if os == LinuxMusl { |
Colin Cross | 1aa45b0 | 2022-02-10 10:33:10 -0800 | [diff] [blame] | 1397 | field := "Musl_" + archType.Name |
| 1398 | userFriendlyField := "target.musl_" + "_" + archType.Name |
| 1399 | if osArchProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok { |
| 1400 | result = append(result, osArchProperties) |
| 1401 | } |
Colin Cross | 528d67e | 2021-07-23 22:23:07 +0000 | [diff] [blame] | 1402 | } |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 1403 | } |
| 1404 | |
| 1405 | // Handle arm on x86 properties in the form: |
| 1406 | // target { |
| 1407 | // arm_on_x86 { |
| 1408 | // key: value, |
| 1409 | // }, |
| 1410 | // arm_on_x86_64 { |
| 1411 | // key: value, |
| 1412 | // }, |
| 1413 | // }, |
| 1414 | if os.Class == Device { |
| 1415 | if arch.ArchType == X86 && (hasArmAbi(arch) || |
| 1416 | hasArmAndroidArch(ctx.Config().Targets[Android])) { |
| 1417 | field := "Arm_on_x86" |
| 1418 | userFriendlyField := "target.arm_on_x86" |
Lukacs T. Berki | 5f51839 | 2021-05-17 11:44:58 +0200 | [diff] [blame] | 1419 | if armOnX86Properties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok { |
| 1420 | result = append(result, armOnX86Properties) |
| 1421 | } |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 1422 | } |
| 1423 | if arch.ArchType == X86_64 && (hasArmAbi(arch) || |
| 1424 | hasArmAndroidArch(ctx.Config().Targets[Android])) { |
| 1425 | field := "Arm_on_x86_64" |
| 1426 | userFriendlyField := "target.arm_on_x86_64" |
Lukacs T. Berki | 5f51839 | 2021-05-17 11:44:58 +0200 | [diff] [blame] | 1427 | if armOnX8664Properties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok { |
| 1428 | result = append(result, armOnX8664Properties) |
| 1429 | } |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 1430 | } |
| 1431 | if os == Android && nativeBridgeEnabled { |
| 1432 | userFriendlyField := "Native_bridge" |
| 1433 | prefix := "target.native_bridge" |
Lukacs T. Berki | 5f51839 | 2021-05-17 11:44:58 +0200 | [diff] [blame] | 1434 | if nativeBridgeProperties, ok := getChildPropertyStruct(ctx, targetProp, userFriendlyField, prefix); ok { |
| 1435 | result = append(result, nativeBridgeProperties) |
| 1436 | } |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 1437 | } |
| 1438 | } |
| 1439 | |
| 1440 | return result |
| 1441 | } |
| 1442 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1443 | // Squash the appropriate arch-specific property structs into the matching top level property |
| 1444 | // structs based on the CompileTarget value that was annotated on the variant. |
Colin Cross | 4157e88 | 2019-06-06 16:57:04 -0700 | [diff] [blame] | 1445 | func (m *ModuleBase) setArchProperties(ctx BottomUpMutatorContext) { |
| 1446 | arch := m.Arch() |
| 1447 | os := m.Os() |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame] | 1448 | |
Usta | dca0219 | 2021-12-20 12:56:46 -0500 | [diff] [blame] | 1449 | for i := range m.archProperties { |
Usta | 851a327 | 2022-01-05 23:42:33 -0500 | [diff] [blame] | 1450 | genProps := m.GetProperties()[i] |
Colin Cross | 4157e88 | 2019-06-06 16:57:04 -0700 | [diff] [blame] | 1451 | if m.archProperties[i] == nil { |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 1452 | continue |
| 1453 | } |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 1454 | |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 1455 | propStructs := make([]reflect.Value, 0) |
| 1456 | for _, archProperty := range m.archProperties[i] { |
| 1457 | propStructShard := getArchProperties(ctx, archProperty, arch, os, m.Target().NativeBridge == NativeBridgeEnabled) |
| 1458 | propStructs = append(propStructs, propStructShard...) |
| 1459 | } |
Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 1460 | |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 1461 | for _, propStruct := range propStructs { |
| 1462 | mergePropertyStruct(ctx, genProps, propStruct) |
Colin Cross | bb2e2b7 | 2016-12-08 17:23:53 -0800 | [diff] [blame] | 1463 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1464 | } |
| 1465 | } |
| 1466 | |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 1467 | // determineBuildOS stores the OS and architecture used for host targets used during the build into |
Colin Cross | 528d67e | 2021-07-23 22:23:07 +0000 | [diff] [blame] | 1468 | // config based on the runtime OS and architecture determined by Go and the product configuration. |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 1469 | func determineBuildOS(config *config) { |
| 1470 | config.BuildOS = func() OsType { |
| 1471 | switch runtime.GOOS { |
| 1472 | case "linux": |
Colin Cross | 528d67e | 2021-07-23 22:23:07 +0000 | [diff] [blame] | 1473 | if Bool(config.productVariables.HostMusl) { |
| 1474 | return LinuxMusl |
| 1475 | } |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 1476 | return Linux |
| 1477 | case "darwin": |
| 1478 | return Darwin |
| 1479 | default: |
| 1480 | panic(fmt.Sprintf("unsupported OS: %s", runtime.GOOS)) |
| 1481 | } |
| 1482 | }() |
| 1483 | |
| 1484 | config.BuildArch = func() ArchType { |
| 1485 | switch runtime.GOARCH { |
| 1486 | case "amd64": |
| 1487 | return X86_64 |
| 1488 | default: |
| 1489 | panic(fmt.Sprintf("unsupported Arch: %s", runtime.GOARCH)) |
| 1490 | } |
| 1491 | }() |
| 1492 | |
| 1493 | } |
| 1494 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1495 | // 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] | 1496 | func decodeTargetProductVariables(config *config) (map[OsType][]Target, error) { |
Dan Willemsen | 45133ac | 2018-03-09 21:22:06 -0800 | [diff] [blame] | 1497 | variables := config.productVariables |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 1498 | |
Dan Willemsen | 0ef639b | 2018-10-10 17:02:29 -0700 | [diff] [blame] | 1499 | targets := make(map[OsType][]Target) |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 1500 | var targetErr error |
| 1501 | |
Liz Kammer | b7f3366 | 2022-02-28 14:16:16 -0500 | [diff] [blame] | 1502 | type targetConfig struct { |
| 1503 | os OsType |
| 1504 | archName string |
| 1505 | archVariant *string |
| 1506 | cpuVariant *string |
| 1507 | abi []string |
| 1508 | nativeBridgeEnabled NativeBridgeSupport |
| 1509 | nativeBridgeHostArchName *string |
| 1510 | nativeBridgeRelativePath *string |
| 1511 | } |
| 1512 | |
| 1513 | addTarget := func(target targetConfig) { |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 1514 | if targetErr != nil { |
| 1515 | return |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 1516 | } |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 1517 | |
Liz Kammer | b7f3366 | 2022-02-28 14:16:16 -0500 | [diff] [blame] | 1518 | arch, err := decodeArch(target.os, target.archName, target.archVariant, target.cpuVariant, target.abi) |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 1519 | if err != nil { |
| 1520 | targetErr = err |
| 1521 | return |
| 1522 | } |
Liz Kammer | b7f3366 | 2022-02-28 14:16:16 -0500 | [diff] [blame] | 1523 | nativeBridgeRelativePathStr := String(target.nativeBridgeRelativePath) |
| 1524 | nativeBridgeHostArchNameStr := String(target.nativeBridgeHostArchName) |
dimitry | 8d6dde8 | 2019-07-11 10:23:53 +0200 | [diff] [blame] | 1525 | |
| 1526 | // Use guest arch as relative install path by default |
Liz Kammer | b7f3366 | 2022-02-28 14:16:16 -0500 | [diff] [blame] | 1527 | if target.nativeBridgeEnabled && nativeBridgeRelativePathStr == "" { |
dimitry | 8d6dde8 | 2019-07-11 10:23:53 +0200 | [diff] [blame] | 1528 | nativeBridgeRelativePathStr = arch.ArchType.String() |
| 1529 | } |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 1530 | |
Jiyong Park | 1613e55 | 2020-09-14 19:43:17 +0900 | [diff] [blame] | 1531 | // A target is considered as HostCross if it's a host target which can't run natively on |
| 1532 | // the currently configured build machine (either because the OS is different or because of |
| 1533 | // the unsupported arch) |
| 1534 | hostCross := false |
Liz Kammer | b7f3366 | 2022-02-28 14:16:16 -0500 | [diff] [blame] | 1535 | if target.os.Class == Host { |
Jiyong Park | 1613e55 | 2020-09-14 19:43:17 +0900 | [diff] [blame] | 1536 | var osSupported bool |
Liz Kammer | b7f3366 | 2022-02-28 14:16:16 -0500 | [diff] [blame] | 1537 | if target.os == config.BuildOS { |
Jiyong Park | 1613e55 | 2020-09-14 19:43:17 +0900 | [diff] [blame] | 1538 | osSupported = true |
Liz Kammer | b7f3366 | 2022-02-28 14:16:16 -0500 | [diff] [blame] | 1539 | } else if config.BuildOS.Linux() && target.os.Linux() { |
Jiyong Park | 1613e55 | 2020-09-14 19:43:17 +0900 | [diff] [blame] | 1540 | // LinuxBionic and Linux are compatible |
| 1541 | osSupported = true |
| 1542 | } else { |
| 1543 | osSupported = false |
| 1544 | } |
| 1545 | |
| 1546 | var archSupported bool |
| 1547 | if arch.ArchType == Common { |
| 1548 | archSupported = true |
| 1549 | } else if arch.ArchType.Name == *variables.HostArch { |
| 1550 | archSupported = true |
| 1551 | } else if variables.HostSecondaryArch != nil && arch.ArchType.Name == *variables.HostSecondaryArch { |
| 1552 | archSupported = true |
| 1553 | } else { |
| 1554 | archSupported = false |
| 1555 | } |
| 1556 | if !osSupported || !archSupported { |
| 1557 | hostCross = true |
| 1558 | } |
| 1559 | } |
| 1560 | |
Liz Kammer | b7f3366 | 2022-02-28 14:16:16 -0500 | [diff] [blame] | 1561 | targets[target.os] = append(targets[target.os], |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 1562 | Target{ |
Liz Kammer | b7f3366 | 2022-02-28 14:16:16 -0500 | [diff] [blame] | 1563 | Os: target.os, |
dimitry | 8d6dde8 | 2019-07-11 10:23:53 +0200 | [diff] [blame] | 1564 | Arch: arch, |
Liz Kammer | b7f3366 | 2022-02-28 14:16:16 -0500 | [diff] [blame] | 1565 | NativeBridge: target.nativeBridgeEnabled, |
dimitry | 8d6dde8 | 2019-07-11 10:23:53 +0200 | [diff] [blame] | 1566 | NativeBridgeHostArchName: nativeBridgeHostArchNameStr, |
| 1567 | NativeBridgeRelativePath: nativeBridgeRelativePathStr, |
Jiyong Park | 1613e55 | 2020-09-14 19:43:17 +0900 | [diff] [blame] | 1568 | HostCross: hostCross, |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 1569 | }) |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 1570 | } |
| 1571 | |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 1572 | if variables.HostArch == nil { |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 1573 | return nil, fmt.Errorf("No host primary architecture set") |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 1574 | } |
| 1575 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1576 | // The primary host target, which must always exist. |
Liz Kammer | b7f3366 | 2022-02-28 14:16:16 -0500 | [diff] [blame] | 1577 | addTarget(targetConfig{os: config.BuildOS, archName: *variables.HostArch, nativeBridgeEnabled: NativeBridgeDisabled}) |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 1578 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1579 | // An optional secondary host target. |
Colin Cross | eeabb89 | 2015-11-20 13:07:51 -0800 | [diff] [blame] | 1580 | if variables.HostSecondaryArch != nil && *variables.HostSecondaryArch != "" { |
Liz Kammer | b7f3366 | 2022-02-28 14:16:16 -0500 | [diff] [blame] | 1581 | addTarget(targetConfig{os: config.BuildOS, archName: *variables.HostSecondaryArch, nativeBridgeEnabled: NativeBridgeDisabled}) |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 1582 | } |
| 1583 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1584 | // Optional cross-compiled host targets, generally Windows. |
Colin Cross | ff3ae9d | 2018-04-10 16:15:18 -0700 | [diff] [blame] | 1585 | if String(variables.CrossHost) != "" { |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 1586 | crossHostOs := osByName(*variables.CrossHost) |
| 1587 | if crossHostOs == NoOsType { |
| 1588 | return nil, fmt.Errorf("Unknown cross host OS %q", *variables.CrossHost) |
| 1589 | } |
| 1590 | |
Colin Cross | ff3ae9d | 2018-04-10 16:15:18 -0700 | [diff] [blame] | 1591 | if String(variables.CrossHostArch) == "" { |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 1592 | return nil, fmt.Errorf("No cross-host primary architecture set") |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 1593 | } |
| 1594 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1595 | // The primary cross-compiled host target. |
Liz Kammer | b7f3366 | 2022-02-28 14:16:16 -0500 | [diff] [blame] | 1596 | addTarget(targetConfig{os: crossHostOs, archName: *variables.CrossHostArch, nativeBridgeEnabled: NativeBridgeDisabled}) |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 1597 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1598 | // An optional secondary cross-compiled host target. |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 1599 | if variables.CrossHostSecondaryArch != nil && *variables.CrossHostSecondaryArch != "" { |
Liz Kammer | b7f3366 | 2022-02-28 14:16:16 -0500 | [diff] [blame] | 1600 | addTarget(targetConfig{os: crossHostOs, archName: *variables.CrossHostSecondaryArch, nativeBridgeEnabled: NativeBridgeDisabled}) |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 1601 | } |
| 1602 | } |
| 1603 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1604 | // Optional device targets |
Dan Willemsen | 3f32f03 | 2016-07-11 14:36:48 -0700 | [diff] [blame] | 1605 | if variables.DeviceArch != nil && *variables.DeviceArch != "" { |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1606 | // The primary device target. |
Liz Kammer | b7f3366 | 2022-02-28 14:16:16 -0500 | [diff] [blame] | 1607 | addTarget(targetConfig{ |
| 1608 | os: Android, |
| 1609 | archName: *variables.DeviceArch, |
| 1610 | archVariant: variables.DeviceArchVariant, |
| 1611 | cpuVariant: variables.DeviceCpuVariant, |
| 1612 | abi: variables.DeviceAbi, |
| 1613 | nativeBridgeEnabled: NativeBridgeDisabled, |
| 1614 | }) |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 1615 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1616 | // An optional secondary device target. |
Dan Willemsen | 3f32f03 | 2016-07-11 14:36:48 -0700 | [diff] [blame] | 1617 | if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" { |
Liz Kammer | b7f3366 | 2022-02-28 14:16:16 -0500 | [diff] [blame] | 1618 | addTarget(targetConfig{ |
| 1619 | os: Android, |
| 1620 | archName: *variables.DeviceSecondaryArch, |
| 1621 | archVariant: variables.DeviceSecondaryArchVariant, |
| 1622 | cpuVariant: variables.DeviceSecondaryCpuVariant, |
| 1623 | abi: variables.DeviceSecondaryAbi, |
| 1624 | nativeBridgeEnabled: NativeBridgeDisabled, |
| 1625 | }) |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 1626 | } |
dimitry | 1f33e40 | 2019-03-26 12:39:31 +0100 | [diff] [blame] | 1627 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1628 | // An optional NativeBridge device target. |
dimitry | 1f33e40 | 2019-03-26 12:39:31 +0100 | [diff] [blame] | 1629 | if variables.NativeBridgeArch != nil && *variables.NativeBridgeArch != "" { |
Liz Kammer | b7f3366 | 2022-02-28 14:16:16 -0500 | [diff] [blame] | 1630 | addTarget(targetConfig{ |
| 1631 | os: Android, |
| 1632 | archName: *variables.NativeBridgeArch, |
| 1633 | archVariant: variables.NativeBridgeArchVariant, |
| 1634 | cpuVariant: variables.NativeBridgeCpuVariant, |
| 1635 | abi: variables.NativeBridgeAbi, |
| 1636 | nativeBridgeEnabled: NativeBridgeEnabled, |
| 1637 | nativeBridgeHostArchName: variables.DeviceArch, |
| 1638 | nativeBridgeRelativePath: variables.NativeBridgeRelativePath, |
| 1639 | }) |
dimitry | 1f33e40 | 2019-03-26 12:39:31 +0100 | [diff] [blame] | 1640 | } |
| 1641 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1642 | // An optional secondary NativeBridge device target. |
dimitry | 1f33e40 | 2019-03-26 12:39:31 +0100 | [diff] [blame] | 1643 | if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" && |
| 1644 | variables.NativeBridgeSecondaryArch != nil && *variables.NativeBridgeSecondaryArch != "" { |
Liz Kammer | b7f3366 | 2022-02-28 14:16:16 -0500 | [diff] [blame] | 1645 | addTarget(targetConfig{ |
| 1646 | os: Android, |
| 1647 | archName: *variables.NativeBridgeSecondaryArch, |
| 1648 | archVariant: variables.NativeBridgeSecondaryArchVariant, |
| 1649 | cpuVariant: variables.NativeBridgeSecondaryCpuVariant, |
| 1650 | abi: variables.NativeBridgeSecondaryAbi, |
| 1651 | nativeBridgeEnabled: NativeBridgeEnabled, |
| 1652 | nativeBridgeHostArchName: variables.DeviceSecondaryArch, |
| 1653 | nativeBridgeRelativePath: variables.NativeBridgeSecondaryRelativePath, |
| 1654 | }) |
dimitry | 1f33e40 | 2019-03-26 12:39:31 +0100 | [diff] [blame] | 1655 | } |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 1656 | } |
| 1657 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 1658 | if targetErr != nil { |
| 1659 | return nil, targetErr |
| 1660 | } |
| 1661 | |
| 1662 | return targets, nil |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 1663 | } |
| 1664 | |
Colin Cross | bb2e2b7 | 2016-12-08 17:23:53 -0800 | [diff] [blame] | 1665 | // hasArmAbi returns true if arch has at least one arm ABI |
| 1666 | func hasArmAbi(arch Arch) bool { |
Jaewoong Jung | 3aff578 | 2020-02-11 07:54:35 -0800 | [diff] [blame] | 1667 | return PrefixInList(arch.Abi, "arm") |
Colin Cross | bb2e2b7 | 2016-12-08 17:23:53 -0800 | [diff] [blame] | 1668 | } |
| 1669 | |
Lev Rumyantsev | 3458121 | 2021-10-13 09:47:59 -0700 | [diff] [blame] | 1670 | // hasArmAndroidArch returns true if targets has at least |
| 1671 | // one arm Android arch (possibly native bridged) |
Colin Cross | 4247f0d | 2017-04-13 16:56:14 -0700 | [diff] [blame] | 1672 | func hasArmAndroidArch(targets []Target) bool { |
| 1673 | for _, target := range targets { |
Lev Rumyantsev | 3458121 | 2021-10-13 09:47:59 -0700 | [diff] [blame] | 1674 | if target.Os == Android && |
| 1675 | (target.Arch.ArchType == Arm || target.Arch.ArchType == Arm64) { |
Victor Khimenko | 5eb8ec1 | 2018-03-21 20:30:54 +0100 | [diff] [blame] | 1676 | return true |
| 1677 | } |
| 1678 | } |
| 1679 | return false |
| 1680 | } |
| 1681 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1682 | // archConfig describes a built-in configuration. |
Dan Albert | 4098deb | 2016-10-19 14:04:41 -0700 | [diff] [blame] | 1683 | type archConfig struct { |
| 1684 | arch string |
| 1685 | archVariant string |
| 1686 | cpuVariant string |
| 1687 | abi []string |
| 1688 | } |
| 1689 | |
Elliott Hughes | c55b586 | 2022-10-27 23:46:22 +0000 | [diff] [blame^] | 1690 | // getNdkAbisConfig returns the list of archConfigs that are used for building |
| 1691 | // the API stubs and static libraries that are included in the NDK. |
Dan Albert | 4098deb | 2016-10-19 14:04:41 -0700 | [diff] [blame] | 1692 | func getNdkAbisConfig() []archConfig { |
| 1693 | return []archConfig{ |
Tamas Petz | bca786d | 2021-01-20 18:56:33 +0100 | [diff] [blame] | 1694 | {"arm64", "armv8-a-branchprot", "", []string{"arm64-v8a"}}, |
Elliott Hughes | c55b586 | 2022-10-27 23:46:22 +0000 | [diff] [blame^] | 1695 | {"arm", "armv7-a-neon", "", []string{"armeabi-v7a"}}, |
Dan Albert | 4098deb | 2016-10-19 14:04:41 -0700 | [diff] [blame] | 1696 | {"x86_64", "", "", []string{"x86_64"}}, |
Inseob Kim | 5219c0e | 2021-06-17 00:33:00 +0900 | [diff] [blame] | 1697 | {"x86", "", "", []string{"x86"}}, |
Dan Albert | 4098deb | 2016-10-19 14:04:41 -0700 | [diff] [blame] | 1698 | } |
| 1699 | } |
| 1700 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1701 | // 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] | 1702 | func getAmlAbisConfig() []archConfig { |
| 1703 | return []archConfig{ |
Martin Stjernholm | c1ecc43 | 2019-11-15 15:00:31 +0000 | [diff] [blame] | 1704 | {"arm64", "armv8-a", "", []string{"arm64-v8a"}}, |
Inseob Kim | 5219c0e | 2021-06-17 00:33:00 +0900 | [diff] [blame] | 1705 | {"arm", "armv7-a-neon", "", []string{"armeabi-v7a"}}, |
Martin Stjernholm | c1ecc43 | 2019-11-15 15:00:31 +0000 | [diff] [blame] | 1706 | {"x86_64", "", "", []string{"x86_64"}}, |
Inseob Kim | 5219c0e | 2021-06-17 00:33:00 +0900 | [diff] [blame] | 1707 | {"x86", "", "", []string{"x86"}}, |
Martin Stjernholm | c1ecc43 | 2019-11-15 15:00:31 +0000 | [diff] [blame] | 1708 | } |
| 1709 | } |
| 1710 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1711 | // decodeArchSettings converts a list of archConfigs into a list of Targets for the given OsType. |
Liz Kammer | b7f3366 | 2022-02-28 14:16:16 -0500 | [diff] [blame] | 1712 | func decodeAndroidArchSettings(archConfigs []archConfig) ([]Target, error) { |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 1713 | var ret []Target |
Dan Willemsen | 322acaf | 2016-01-12 23:07:05 -0800 | [diff] [blame] | 1714 | |
Dan Albert | 4098deb | 2016-10-19 14:04:41 -0700 | [diff] [blame] | 1715 | for _, config := range archConfigs { |
Liz Kammer | b7f3366 | 2022-02-28 14:16:16 -0500 | [diff] [blame] | 1716 | arch, err := decodeArch(Android, config.arch, &config.archVariant, |
Colin Cross | a74ca04 | 2019-01-31 14:31:51 -0800 | [diff] [blame] | 1717 | &config.cpuVariant, config.abi) |
Dan Willemsen | 322acaf | 2016-01-12 23:07:05 -0800 | [diff] [blame] | 1718 | if err != nil { |
| 1719 | return nil, err |
| 1720 | } |
Colin Cross | 3b19f5d | 2019-09-17 14:45:31 -0700 | [diff] [blame] | 1721 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 1722 | ret = append(ret, Target{ |
| 1723 | Os: Android, |
| 1724 | Arch: arch, |
| 1725 | }) |
Dan Willemsen | 322acaf | 2016-01-12 23:07:05 -0800 | [diff] [blame] | 1726 | } |
| 1727 | |
| 1728 | return ret, nil |
| 1729 | } |
| 1730 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1731 | // 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] | 1732 | 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] | 1733 | // Verify the arch is valid |
Colin Cross | eeabb89 | 2015-11-20 13:07:51 -0800 | [diff] [blame] | 1734 | archType, ok := archTypeMap[arch] |
| 1735 | if !ok { |
| 1736 | return Arch{}, fmt.Errorf("unknown arch %q", arch) |
| 1737 | } |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 1738 | |
Colin Cross | eeabb89 | 2015-11-20 13:07:51 -0800 | [diff] [blame] | 1739 | a := Arch{ |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 1740 | ArchType: archType, |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1741 | ArchVariant: String(archVariant), |
| 1742 | CpuVariant: String(cpuVariant), |
Colin Cross | a74ca04 | 2019-01-31 14:31:51 -0800 | [diff] [blame] | 1743 | Abi: abi, |
Colin Cross | eeabb89 | 2015-11-20 13:07:51 -0800 | [diff] [blame] | 1744 | } |
| 1745 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1746 | // Convert generic arch variants into the empty string. |
Colin Cross | eeabb89 | 2015-11-20 13:07:51 -0800 | [diff] [blame] | 1747 | if a.ArchVariant == a.ArchType.Name || a.ArchVariant == "generic" { |
| 1748 | a.ArchVariant = "" |
| 1749 | } |
| 1750 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1751 | // Convert generic CPU variants into the empty string. |
Colin Cross | eeabb89 | 2015-11-20 13:07:51 -0800 | [diff] [blame] | 1752 | if a.CpuVariant == a.ArchType.Name || a.CpuVariant == "generic" { |
| 1753 | a.CpuVariant = "" |
| 1754 | } |
| 1755 | |
Liz Kammer | 2c2afe2 | 2022-02-11 11:35:03 -0500 | [diff] [blame] | 1756 | if a.ArchVariant != "" { |
| 1757 | if validArchVariants := archVariants[archType]; !InList(a.ArchVariant, validArchVariants) { |
| 1758 | return Arch{}, fmt.Errorf("[%q] unknown arch variant %q, support variants: %q", archType, a.ArchVariant, validArchVariants) |
| 1759 | } |
| 1760 | } |
| 1761 | |
| 1762 | if a.CpuVariant != "" { |
| 1763 | if validCpuVariants := cpuVariants[archType]; !InList(a.CpuVariant, validCpuVariants) { |
| 1764 | return Arch{}, fmt.Errorf("[%q] unknown cpu variant %q, support variants: %q", archType, a.CpuVariant, validCpuVariants) |
| 1765 | } |
| 1766 | } |
| 1767 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1768 | // Filter empty ABIs out of the list. |
Colin Cross | eeabb89 | 2015-11-20 13:07:51 -0800 | [diff] [blame] | 1769 | for i := 0; i < len(a.Abi); i++ { |
| 1770 | if a.Abi[i] == "" { |
| 1771 | a.Abi = append(a.Abi[:i], a.Abi[i+1:]...) |
| 1772 | i-- |
| 1773 | } |
| 1774 | } |
| 1775 | |
Liz Kammer | e8303bd | 2022-02-16 09:02:48 -0500 | [diff] [blame] | 1776 | // Set ArchFeatures from the arch type. for Android OS, other os-es do not specify features |
| 1777 | if os == Android { |
| 1778 | if featureMap, ok := androidArchFeatureMap[archType]; ok { |
Dan Willemsen | 01a3c25 | 2019-01-11 19:02:16 -0800 | [diff] [blame] | 1779 | a.ArchFeatures = featureMap[a.ArchVariant] |
| 1780 | } |
Colin Cross | c5c24ad | 2015-11-20 15:35:00 -0800 | [diff] [blame] | 1781 | } |
| 1782 | |
Colin Cross | eeabb89 | 2015-11-20 13:07:51 -0800 | [diff] [blame] | 1783 | return a, nil |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 1784 | } |
| 1785 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1786 | // filterMultilibTargets takes a list of Targets and a multilib value and returns a new list of |
| 1787 | // Targets containing only those that have the given multilib value. |
Colin Cross | 69617d3 | 2016-09-06 10:39:07 -0700 | [diff] [blame] | 1788 | func filterMultilibTargets(targets []Target, multilib string) []Target { |
| 1789 | var ret []Target |
| 1790 | for _, t := range targets { |
| 1791 | if t.Arch.ArchType.Multilib == multilib { |
| 1792 | ret = append(ret, t) |
| 1793 | } |
| 1794 | } |
| 1795 | return ret |
| 1796 | } |
| 1797 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1798 | // getCommonTargets returns the set of Os specific common architecture targets for each Os in a list |
| 1799 | // of targets. |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 1800 | func getCommonTargets(targets []Target) []Target { |
| 1801 | var ret []Target |
| 1802 | set := make(map[string]bool) |
| 1803 | |
| 1804 | for _, t := range targets { |
| 1805 | if _, found := set[t.Os.String()]; !found { |
| 1806 | set[t.Os.String()] = true |
Colin Cross | 39a1814 | 2022-06-24 18:43:40 -0700 | [diff] [blame] | 1807 | common := commonTargetMap[t.Os.String()] |
| 1808 | common.HostCross = t.HostCross |
| 1809 | ret = append(ret, common) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 1810 | } |
| 1811 | } |
| 1812 | |
| 1813 | return ret |
| 1814 | } |
| 1815 | |
Sam Delmerico | cc271e2 | 2022-06-01 15:45:02 +0000 | [diff] [blame] | 1816 | // FirstTarget takes a list of Targets and a list of multilib values and returns a list of Targets |
Colin Cross | c0f0eb8 | 2022-07-19 14:41:11 -0700 | [diff] [blame] | 1817 | // that contains zero or one Target for each OsType and HostCross, selecting the one that matches |
| 1818 | // the earliest filter. |
Sam Delmerico | cc271e2 | 2022-06-01 15:45:02 +0000 | [diff] [blame] | 1819 | func FirstTarget(targets []Target, filters ...string) []Target { |
Jiyong Park | 2210198 | 2020-09-17 19:09:58 +0900 | [diff] [blame] | 1820 | // find the first target from each OS |
| 1821 | var ret []Target |
Colin Cross | c0f0eb8 | 2022-07-19 14:41:11 -0700 | [diff] [blame] | 1822 | type osHostCross struct { |
| 1823 | os OsType |
| 1824 | hostCross bool |
| 1825 | } |
| 1826 | set := make(map[osHostCross]bool) |
Jiyong Park | 2210198 | 2020-09-17 19:09:58 +0900 | [diff] [blame] | 1827 | |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1828 | for _, filter := range filters { |
| 1829 | buildTargets := filterMultilibTargets(targets, filter) |
Jiyong Park | 2210198 | 2020-09-17 19:09:58 +0900 | [diff] [blame] | 1830 | for _, t := range buildTargets { |
Colin Cross | c0f0eb8 | 2022-07-19 14:41:11 -0700 | [diff] [blame] | 1831 | key := osHostCross{t.Os, t.HostCross} |
| 1832 | if _, found := set[key]; !found { |
| 1833 | set[key] = true |
Jiyong Park | 2210198 | 2020-09-17 19:09:58 +0900 | [diff] [blame] | 1834 | ret = append(ret, t) |
| 1835 | } |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1836 | } |
| 1837 | } |
Jiyong Park | 2210198 | 2020-09-17 19:09:58 +0900 | [diff] [blame] | 1838 | return ret |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1839 | } |
| 1840 | |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1841 | // decodeMultilibTargets uses the module's multilib setting to select one or more targets from a |
| 1842 | // list of Targets. |
Colin Cross | ee0bc3b | 2018-10-02 22:01:37 -0700 | [diff] [blame] | 1843 | func decodeMultilibTargets(multilib string, targets []Target, prefer32 bool) ([]Target, error) { |
Colin Cross | a684540 | 2020-11-16 15:08:19 -0800 | [diff] [blame] | 1844 | var buildTargets []Target |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1845 | |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 1846 | switch multilib { |
| 1847 | case "common": |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1848 | buildTargets = getCommonTargets(targets) |
| 1849 | case "common_first": |
| 1850 | buildTargets = getCommonTargets(targets) |
| 1851 | if prefer32 { |
Sam Delmerico | cc271e2 | 2022-06-01 15:45:02 +0000 | [diff] [blame] | 1852 | buildTargets = append(buildTargets, FirstTarget(targets, "lib32", "lib64")...) |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1853 | } else { |
Sam Delmerico | cc271e2 | 2022-06-01 15:45:02 +0000 | [diff] [blame] | 1854 | buildTargets = append(buildTargets, FirstTarget(targets, "lib64", "lib32")...) |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1855 | } |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 1856 | case "both": |
Colin Cross | 8b74d17 | 2016-09-13 09:59:14 -0700 | [diff] [blame] | 1857 | if prefer32 { |
| 1858 | buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...) |
| 1859 | buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...) |
| 1860 | } else { |
| 1861 | buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...) |
| 1862 | buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...) |
| 1863 | } |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 1864 | case "32": |
Colin Cross | 69617d3 | 2016-09-06 10:39:07 -0700 | [diff] [blame] | 1865 | buildTargets = filterMultilibTargets(targets, "lib32") |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 1866 | case "64": |
Colin Cross | 69617d3 | 2016-09-06 10:39:07 -0700 | [diff] [blame] | 1867 | buildTargets = filterMultilibTargets(targets, "lib64") |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1868 | case "first": |
| 1869 | if prefer32 { |
Sam Delmerico | cc271e2 | 2022-06-01 15:45:02 +0000 | [diff] [blame] | 1870 | buildTargets = FirstTarget(targets, "lib32", "lib64") |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1871 | } else { |
Sam Delmerico | cc271e2 | 2022-06-01 15:45:02 +0000 | [diff] [blame] | 1872 | buildTargets = FirstTarget(targets, "lib64", "lib32") |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1873 | } |
Victor Chang | 9448e8f | 2020-09-14 15:34:16 +0100 | [diff] [blame] | 1874 | case "first_prefer32": |
Sam Delmerico | cc271e2 | 2022-06-01 15:45:02 +0000 | [diff] [blame] | 1875 | buildTargets = FirstTarget(targets, "lib32", "lib64") |
Colin Cross | 69617d3 | 2016-09-06 10:39:07 -0700 | [diff] [blame] | 1876 | case "prefer32": |
Colin Cross | 3dceee3 | 2018-09-06 10:19:57 -0700 | [diff] [blame] | 1877 | buildTargets = filterMultilibTargets(targets, "lib32") |
| 1878 | if len(buildTargets) == 0 { |
| 1879 | buildTargets = filterMultilibTargets(targets, "lib64") |
| 1880 | } |
Dan Willemsen | 4745007 | 2021-10-19 20:24:49 -0700 | [diff] [blame] | 1881 | case "darwin_universal": |
| 1882 | buildTargets = filterMultilibTargets(targets, "lib64") |
| 1883 | // Reverse the targets so that the first architecture can depend on the second |
| 1884 | // architecture module in order to merge the outputs. |
| 1885 | reverseSliceInPlace(buildTargets) |
| 1886 | case "darwin_universal_common_first": |
| 1887 | archTargets := filterMultilibTargets(targets, "lib64") |
| 1888 | reverseSliceInPlace(archTargets) |
| 1889 | buildTargets = append(getCommonTargets(targets), archTargets...) |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 1890 | default: |
Victor Chang | 9448e8f | 2020-09-14 15:34:16 +0100 | [diff] [blame] | 1891 | 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] | 1892 | multilib) |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 1893 | } |
| 1894 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 1895 | return buildTargets, nil |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 1896 | } |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 1897 | |
Chris Parsons | c424b76 | 2021-04-29 18:06:50 -0400 | [diff] [blame] | 1898 | func (m *ModuleBase) getArchPropertySet(propertySet interface{}, archType ArchType) interface{} { |
| 1899 | archString := archType.Field |
| 1900 | for i := range m.archProperties { |
| 1901 | if m.archProperties[i] == nil { |
| 1902 | // Skip over nil properties |
| 1903 | continue |
| 1904 | } |
| 1905 | |
| 1906 | // Not archProperties are usable; this function looks for properties of a very specific |
| 1907 | // form, and ignores the rest. |
| 1908 | for _, archProperty := range m.archProperties[i] { |
| 1909 | // archPropValue is a property struct, we are looking for the form: |
| 1910 | // `arch: { arm: { key: value, ... }}` |
| 1911 | archPropValue := reflect.ValueOf(archProperty).Elem() |
| 1912 | |
| 1913 | // Unwrap src so that it should looks like a pointer to `arm: { key: value, ... }` |
| 1914 | src := archPropValue.FieldByName("Arch").Elem() |
| 1915 | |
| 1916 | // Step into non-nil pointers to structs in the src value. |
| 1917 | if src.Kind() == reflect.Ptr { |
| 1918 | if src.IsNil() { |
| 1919 | continue |
| 1920 | } |
| 1921 | src = src.Elem() |
| 1922 | } |
| 1923 | |
| 1924 | // Find the requested field (e.g. arm, x86) in the src struct. |
| 1925 | src = src.FieldByName(archString) |
| 1926 | |
| 1927 | // We only care about structs. |
| 1928 | if !src.IsValid() || src.Kind() != reflect.Struct { |
| 1929 | continue |
| 1930 | } |
| 1931 | |
| 1932 | // If the value of the field is a struct then step into the |
| 1933 | // BlueprintEmbed field. The special "BlueprintEmbed" name is |
| 1934 | // used by createArchPropTypeDesc to embed the arch properties |
| 1935 | // in the parent struct, so the src arch prop should be in this |
| 1936 | // field. |
| 1937 | // |
| 1938 | // See createArchPropTypeDesc for more details on how Arch-specific |
| 1939 | // module properties are processed from the nested props and written |
| 1940 | // into the module's archProperties. |
| 1941 | src = src.FieldByName("BlueprintEmbed") |
| 1942 | |
| 1943 | // Clone the destination prop, since we want a unique prop struct per arch. |
| 1944 | propertySetClone := reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface() |
| 1945 | |
| 1946 | // Copy the located property struct into the cloned destination property struct. |
| 1947 | err := proptools.ExtendMatchingProperties([]interface{}{propertySetClone}, src.Interface(), nil, proptools.OrderReplace) |
| 1948 | if err != nil { |
| 1949 | // This is fine, it just means the src struct doesn't match the type of propertySet. |
| 1950 | continue |
| 1951 | } |
| 1952 | |
| 1953 | return propertySetClone |
| 1954 | } |
| 1955 | } |
| 1956 | // No property set was found specific to the given arch, so return an empty |
| 1957 | // property set. |
| 1958 | return reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface() |
| 1959 | } |
| 1960 | |
| 1961 | // getMultilibPropertySet returns a property set struct matching the type of |
| 1962 | // `propertySet`, containing multilib-specific module properties for the given architecture. |
| 1963 | // If no multilib-specific properties exist for the given architecture, returns an empty property |
| 1964 | // set matching `propertySet`'s type. |
| 1965 | func (m *ModuleBase) getMultilibPropertySet(propertySet interface{}, archType ArchType) interface{} { |
| 1966 | // archType.Multilib is lowercase (for example, lib32) but property struct field is |
| 1967 | // capitalized, such as Lib32, so use strings.Title to capitalize it. |
| 1968 | multiLibString := strings.Title(archType.Multilib) |
| 1969 | |
| 1970 | for i := range m.archProperties { |
| 1971 | if m.archProperties[i] == nil { |
| 1972 | // Skip over nil properties |
| 1973 | continue |
| 1974 | } |
| 1975 | |
| 1976 | // Not archProperties are usable; this function looks for properties of a very specific |
| 1977 | // form, and ignores the rest. |
| 1978 | for _, archProperties := range m.archProperties[i] { |
| 1979 | // archPropValue is a property struct, we are looking for the form: |
| 1980 | // `multilib: { lib32: { key: value, ... }}` |
| 1981 | archPropValue := reflect.ValueOf(archProperties).Elem() |
| 1982 | |
| 1983 | // Unwrap src so that it should looks like a pointer to `lib32: { key: value, ... }` |
| 1984 | src := archPropValue.FieldByName("Multilib").Elem() |
| 1985 | |
| 1986 | // Step into non-nil pointers to structs in the src value. |
| 1987 | if src.Kind() == reflect.Ptr { |
| 1988 | if src.IsNil() { |
| 1989 | // Ignore nil pointers. |
| 1990 | continue |
| 1991 | } |
| 1992 | src = src.Elem() |
| 1993 | } |
| 1994 | |
| 1995 | // Find the requested field (e.g. lib32) in the src struct. |
| 1996 | src = src.FieldByName(multiLibString) |
| 1997 | |
| 1998 | // We only care about valid struct pointers. |
| 1999 | if !src.IsValid() || src.Kind() != reflect.Ptr || src.Elem().Kind() != reflect.Struct { |
| 2000 | continue |
| 2001 | } |
| 2002 | |
| 2003 | // Get the zero value for the requested property set. |
| 2004 | propertySetClone := reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface() |
| 2005 | |
| 2006 | // Copy the located property struct into the "zero" property set struct. |
| 2007 | err := proptools.ExtendMatchingProperties([]interface{}{propertySetClone}, src.Interface(), nil, proptools.OrderReplace) |
| 2008 | |
| 2009 | if err != nil { |
| 2010 | // This is fine, it just means the src struct doesn't match. |
| 2011 | continue |
| 2012 | } |
| 2013 | |
| 2014 | return propertySetClone |
| 2015 | } |
| 2016 | } |
| 2017 | |
| 2018 | // There were no multilib properties specifically matching the given archtype. |
| 2019 | // Return zeroed value. |
| 2020 | return reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface() |
| 2021 | } |
| 2022 | |
Liz Kammer | b6dbc87 | 2021-05-14 15:14:40 -0400 | [diff] [blame] | 2023 | // ArchVariantContext defines the limited context necessary to retrieve arch_variant properties. |
| 2024 | type ArchVariantContext interface { |
| 2025 | ModuleErrorf(fmt string, args ...interface{}) |
| 2026 | PropertyErrorf(property, fmt string, args ...interface{}) |
| 2027 | } |
| 2028 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 2029 | // ArchVariantProperties represents a map of arch-variant config strings to a property interface{}. |
| 2030 | type ArchVariantProperties map[string]interface{} |
| 2031 | |
| 2032 | // ConfigurationAxisToArchVariantProperties represents a map of bazel.ConfigurationAxis to |
| 2033 | // ArchVariantProperties, such that each independent arch-variant axis maps to the |
| 2034 | // configs/properties for that axis. |
| 2035 | type ConfigurationAxisToArchVariantProperties map[bazel.ConfigurationAxis]ArchVariantProperties |
| 2036 | |
| 2037 | // GetArchVariantProperties returns a ConfigurationAxisToArchVariantProperties where the |
| 2038 | // arch-variant properties correspond to the values of the properties of the 'propertySet' struct |
| 2039 | // that are specific to that axis/configuration. Each axis is independent, containing |
| 2040 | // non-overlapping configs that correspond to the various "arch-variant" support, at this time: |
Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 2041 | // |
| 2042 | // arches (including multilib) |
| 2043 | // oses |
| 2044 | // arch+os combinations |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 2045 | // |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 2046 | // For example, passing a struct { Foo bool, Bar string } will return an interface{} that can be |
| 2047 | // type asserted back into the same struct, containing the config-specific property value specified |
| 2048 | // by the module if defined. |
Chris Parsons | c424b76 | 2021-04-29 18:06:50 -0400 | [diff] [blame] | 2049 | // |
| 2050 | // Arch-specific properties may come from an arch stanza or a multilib stanza; properties |
| 2051 | // in these stanzas are combined. |
| 2052 | // For example: `arch: { x86: { Foo: ["bar"] } }, multilib: { lib32: {` Foo: ["baz"] } }` |
| 2053 | // will result in `Foo: ["bar", "baz"]` being returned for architecture x86, if the given |
| 2054 | // propertyset contains `Foo []string`. |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 2055 | func (m *ModuleBase) GetArchVariantProperties(ctx ArchVariantContext, propertySet interface{}) ConfigurationAxisToArchVariantProperties { |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 2056 | // Return value of the arch types to the prop values for that arch. |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 2057 | axisToProps := ConfigurationAxisToArchVariantProperties{} |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 2058 | |
| 2059 | // Nothing to do for non-arch-specific modules. |
| 2060 | if !m.ArchSpecific() { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 2061 | return axisToProps |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 2062 | } |
| 2063 | |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 2064 | dstType := reflect.ValueOf(propertySet).Type() |
| 2065 | var archProperties []interface{} |
| 2066 | |
| 2067 | // First find the property set in the module that corresponds to the requested |
Usta | 851a327 | 2022-01-05 23:42:33 -0500 | [diff] [blame] | 2068 | // one. m.archProperties[i] corresponds to m.GetProperties()[i]. |
| 2069 | for i, generalProp := range m.GetProperties() { |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 2070 | srcType := reflect.ValueOf(generalProp).Type() |
| 2071 | if srcType == dstType { |
| 2072 | archProperties = m.archProperties[i] |
Liz Kammer | 135bf55 | 2021-08-11 10:46:06 -0400 | [diff] [blame] | 2073 | axisToProps[bazel.NoConfigAxis] = ArchVariantProperties{"": generalProp} |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 2074 | break |
| 2075 | } |
| 2076 | } |
| 2077 | |
| 2078 | if archProperties == nil { |
| 2079 | // This module does not have the property set requested |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 2080 | return axisToProps |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 2081 | } |
| 2082 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 2083 | archToProp := ArchVariantProperties{} |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 2084 | // For each arch type (x86, arm64, etc.) |
Chris Parsons | c424b76 | 2021-04-29 18:06:50 -0400 | [diff] [blame] | 2085 | for _, arch := range ArchTypeList() { |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 2086 | // Arch properties are sometimes sharded (see createArchPropTypeDesc() ). |
Cole Faust | c843b99 | 2022-08-02 18:06:50 -0700 | [diff] [blame] | 2087 | // Iterate over every shard and extract a struct with the same type as the |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 2088 | // input one that contains the data specific to that arch. |
| 2089 | propertyStructs := make([]reflect.Value, 0) |
Cole Faust | c843b99 | 2022-08-02 18:06:50 -0700 | [diff] [blame] | 2090 | archFeaturePropertyStructs := make(map[string][]reflect.Value, 0) |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 2091 | for _, archProperty := range archProperties { |
Lukacs T. Berki | 5f51839 | 2021-05-17 11:44:58 +0200 | [diff] [blame] | 2092 | archTypeStruct, ok := getArchTypeStruct(ctx, archProperty, arch) |
| 2093 | if ok { |
| 2094 | propertyStructs = append(propertyStructs, archTypeStruct) |
Cole Faust | c843b99 | 2022-08-02 18:06:50 -0700 | [diff] [blame] | 2095 | |
| 2096 | // For each feature this arch supports (arm: neon, x86: ssse3, sse4, ...) |
| 2097 | for _, feature := range archFeatures[arch] { |
| 2098 | prefix := "arch." + arch.Name + "." + feature |
| 2099 | if featureProperties, ok := getChildPropertyStruct(ctx, archTypeStruct, feature, prefix); ok { |
| 2100 | archFeaturePropertyStructs[feature] = append(archFeaturePropertyStructs[feature], featureProperties) |
| 2101 | } |
| 2102 | } |
Lukacs T. Berki | 5f51839 | 2021-05-17 11:44:58 +0200 | [diff] [blame] | 2103 | } |
| 2104 | multilibStruct, ok := getMultilibStruct(ctx, archProperty, arch) |
| 2105 | if ok { |
| 2106 | propertyStructs = append(propertyStructs, multilibStruct) |
| 2107 | } |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 2108 | } |
| 2109 | |
Cole Faust | c843b99 | 2022-08-02 18:06:50 -0700 | [diff] [blame] | 2110 | archToProp[arch.Name] = mergeStructs(ctx, propertyStructs, propertySet) |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 2111 | |
Cole Faust | c843b99 | 2022-08-02 18:06:50 -0700 | [diff] [blame] | 2112 | // In soong, if multiple features match the current configuration, they're |
| 2113 | // all used. In bazel, we have to have unambiguous select() statements, so |
| 2114 | // we can't have two features that are both active in the same select(). |
| 2115 | // One alternative is to split out each feature into a separate select(), |
| 2116 | // but then it's difficult to support exclude_srcs, which may need to |
| 2117 | // exclude things from the regular arch select() statement if a certain |
| 2118 | // feature is active. Instead, keep the features in the same select |
| 2119 | // statement as the arches, but emit the power set of all possible |
| 2120 | // combinations of features, so that bazel can match the most precise one. |
| 2121 | allFeatures := make([]string, 0, len(archFeaturePropertyStructs)) |
| 2122 | for feature := range archFeaturePropertyStructs { |
| 2123 | allFeatures = append(allFeatures, feature) |
| 2124 | } |
| 2125 | for _, features := range bazel.PowerSetWithoutEmptySet(allFeatures) { |
| 2126 | sort.Strings(features) |
| 2127 | propsForCurrentFeatureSet := make([]reflect.Value, 0) |
| 2128 | propsForCurrentFeatureSet = append(propsForCurrentFeatureSet, propertyStructs...) |
| 2129 | for _, feature := range features { |
| 2130 | propsForCurrentFeatureSet = append(propsForCurrentFeatureSet, archFeaturePropertyStructs[feature]...) |
| 2131 | } |
| 2132 | archToProp[arch.Name+"-"+strings.Join(features, "-")] = |
| 2133 | mergeStructs(ctx, propsForCurrentFeatureSet, propertySet) |
| 2134 | } |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 2135 | } |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 2136 | axisToProps[bazel.ArchConfigurationAxis] = archToProp |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 2137 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 2138 | osToProp := ArchVariantProperties{} |
| 2139 | archOsToProp := ArchVariantProperties{} |
Chris Parsons | 2dde0cb | 2021-10-01 14:45:30 -0400 | [diff] [blame] | 2140 | |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 2141 | linuxStructs := getTargetStructs(ctx, archProperties, "Linux") |
| 2142 | bionicStructs := getTargetStructs(ctx, archProperties, "Bionic") |
| 2143 | hostStructs := getTargetStructs(ctx, archProperties, "Host") |
Colin Cross | a98d36d | 2022-03-07 14:39:49 -0800 | [diff] [blame] | 2144 | hostLinuxStructs := getTargetStructs(ctx, archProperties, "Host_linux") |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 2145 | hostNotWindowsStructs := getTargetStructs(ctx, archProperties, "Not_windows") |
Chris Parsons | 2dde0cb | 2021-10-01 14:45:30 -0400 | [diff] [blame] | 2146 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 2147 | // For android, linux, ... |
| 2148 | for _, os := range osTypeList { |
| 2149 | if os == CommonOS { |
| 2150 | // It looks like this OS value is not used in Blueprint files |
| 2151 | continue |
| 2152 | } |
Chris Parsons | 2dde0cb | 2021-10-01 14:45:30 -0400 | [diff] [blame] | 2153 | osStructs := make([]reflect.Value, 0) |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 2154 | |
| 2155 | osSpecificStructs := getTargetStructs(ctx, archProperties, os.Field) |
| 2156 | if os.Class == Host { |
| 2157 | osStructs = append(osStructs, hostStructs...) |
Chris Parsons | a37e195 | 2021-09-28 16:47:36 -0400 | [diff] [blame] | 2158 | } |
Chris Parsons | 2dde0cb | 2021-10-01 14:45:30 -0400 | [diff] [blame] | 2159 | if os.Linux() { |
| 2160 | osStructs = append(osStructs, linuxStructs...) |
| 2161 | } |
| 2162 | if os.Bionic() { |
| 2163 | osStructs = append(osStructs, bionicStructs...) |
| 2164 | } |
Colin Cross | a98d36d | 2022-03-07 14:39:49 -0800 | [diff] [blame] | 2165 | if os.Linux() && os.Class == Host { |
| 2166 | osStructs = append(osStructs, hostLinuxStructs...) |
| 2167 | } |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 2168 | |
| 2169 | if os == LinuxMusl { |
| 2170 | osStructs = append(osStructs, getTargetStructs(ctx, archProperties, "Musl")...) |
| 2171 | } |
| 2172 | if os == Linux { |
| 2173 | osStructs = append(osStructs, getTargetStructs(ctx, archProperties, "Glibc")...) |
| 2174 | } |
| 2175 | |
| 2176 | osStructs = append(osStructs, osSpecificStructs...) |
| 2177 | |
| 2178 | if os.Class == Host && os != Windows { |
| 2179 | osStructs = append(osStructs, hostNotWindowsStructs...) |
| 2180 | } |
Chris Parsons | 2dde0cb | 2021-10-01 14:45:30 -0400 | [diff] [blame] | 2181 | osToProp[os.Name] = mergeStructs(ctx, osStructs, propertySet) |
Chris Parsons | a37e195 | 2021-09-28 16:47:36 -0400 | [diff] [blame] | 2182 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 2183 | // For arm, x86, ... |
| 2184 | for _, arch := range osArchTypeMap[os] { |
Chris Parsons | a37e195 | 2021-09-28 16:47:36 -0400 | [diff] [blame] | 2185 | osArchStructs := make([]reflect.Value, 0) |
| 2186 | |
Chris Parsons | a37e195 | 2021-09-28 16:47:36 -0400 | [diff] [blame] | 2187 | // Auto-combine with Linux_ and Bionic_ targets. This potentially results in |
| 2188 | // repetition and select() bloat, but use of Linux_* and Bionic_* targets is rare. |
| 2189 | // TODO(b/201423152): Look into cleanup. |
| 2190 | if os.Linux() { |
| 2191 | targetField := "Linux_" + arch.Name |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 2192 | targetStructs := getTargetStructs(ctx, archProperties, targetField) |
| 2193 | osArchStructs = append(osArchStructs, targetStructs...) |
Chris Parsons | a37e195 | 2021-09-28 16:47:36 -0400 | [diff] [blame] | 2194 | } |
| 2195 | if os.Bionic() { |
| 2196 | targetField := "Bionic_" + arch.Name |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 2197 | targetStructs := getTargetStructs(ctx, archProperties, targetField) |
| 2198 | osArchStructs = append(osArchStructs, targetStructs...) |
Chris Parsons | a37e195 | 2021-09-28 16:47:36 -0400 | [diff] [blame] | 2199 | } |
Colin Cross | 2d295a2 | 2022-03-07 14:46:20 -0800 | [diff] [blame] | 2200 | if os == LinuxMusl { |
| 2201 | targetField := "Musl_" + arch.Name |
| 2202 | targetStructs := getTargetStructs(ctx, archProperties, targetField) |
| 2203 | osArchStructs = append(osArchStructs, targetStructs...) |
| 2204 | } |
| 2205 | if os == Linux { |
| 2206 | targetField := "Glibc_" + arch.Name |
| 2207 | targetStructs := getTargetStructs(ctx, archProperties, targetField) |
| 2208 | osArchStructs = append(osArchStructs, targetStructs...) |
| 2209 | } |
Chris Parsons | a37e195 | 2021-09-28 16:47:36 -0400 | [diff] [blame] | 2210 | |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 2211 | targetField := GetCompoundTargetField(os, arch) |
| 2212 | targetName := fmt.Sprintf("%s_%s", os.Name, arch.Name) |
| 2213 | targetStructs := getTargetStructs(ctx, archProperties, targetField) |
| 2214 | osArchStructs = append(osArchStructs, targetStructs...) |
| 2215 | |
Chris Parsons | a37e195 | 2021-09-28 16:47:36 -0400 | [diff] [blame] | 2216 | archOsToProp[targetName] = mergeStructs(ctx, osArchStructs, propertySet) |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 2217 | } |
| 2218 | } |
Chris Parsons | 2dde0cb | 2021-10-01 14:45:30 -0400 | [diff] [blame] | 2219 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 2220 | axisToProps[bazel.OsConfigurationAxis] = osToProp |
| 2221 | axisToProps[bazel.OsArchConfigurationAxis] = archOsToProp |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 2222 | return axisToProps |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 2223 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 2224 | |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 2225 | // Returns a struct matching the propertySet interface, containing properties specific to the targetName |
| 2226 | // For example, given these arguments: |
Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 2227 | // |
| 2228 | // propertySet = BaseCompilerProperties |
| 2229 | // targetName = "android_arm" |
| 2230 | // |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 2231 | // And given this Android.bp fragment: |
Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 2232 | // |
| 2233 | // target: |
| 2234 | // android_arm: { |
| 2235 | // srcs: ["foo.c"], |
| 2236 | // } |
| 2237 | // android_arm64: { |
| 2238 | // srcs: ["bar.c"], |
| 2239 | // } |
| 2240 | // } |
| 2241 | // |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 2242 | // This would return a BaseCompilerProperties with BaseCompilerProperties.Srcs = ["foo.c"] |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 2243 | func getTargetStructs(ctx ArchVariantContext, archProperties []interface{}, targetName string) []reflect.Value { |
| 2244 | var propertyStructs []reflect.Value |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 2245 | for _, archProperty := range archProperties { |
| 2246 | archPropValues := reflect.ValueOf(archProperty).Elem() |
| 2247 | targetProp := archPropValues.FieldByName("Target").Elem() |
| 2248 | targetStruct, ok := getChildPropertyStruct(ctx, targetProp, targetName, targetName) |
| 2249 | if ok { |
| 2250 | propertyStructs = append(propertyStructs, targetStruct) |
Chris Parsons | a37e195 | 2021-09-28 16:47:36 -0400 | [diff] [blame] | 2251 | } else { |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 2252 | return []reflect.Value{} |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 2253 | } |
| 2254 | } |
| 2255 | |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 2256 | return propertyStructs |
Chris Parsons | a37e195 | 2021-09-28 16:47:36 -0400 | [diff] [blame] | 2257 | } |
| 2258 | |
| 2259 | func mergeStructs(ctx ArchVariantContext, propertyStructs []reflect.Value, propertySet interface{}) interface{} { |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 2260 | // Create a new instance of the requested property set |
| 2261 | value := reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface() |
| 2262 | |
| 2263 | // Merge all the structs together |
| 2264 | for _, propertyStruct := range propertyStructs { |
| 2265 | mergePropertyStruct(ctx, value, propertyStruct) |
| 2266 | } |
| 2267 | |
| 2268 | return value |
| 2269 | } |
Liz Kammer | e8303bd | 2022-02-16 09:02:48 -0500 | [diff] [blame] | 2270 | |
| 2271 | func printArchTypeStarlarkDict(dict map[ArchType][]string) string { |
| 2272 | valDict := make(map[string]string, len(dict)) |
| 2273 | for k, v := range dict { |
| 2274 | valDict[k.String()] = starlark_fmt.PrintStringList(v, 1) |
| 2275 | } |
| 2276 | return starlark_fmt.PrintDict(valDict, 0) |
| 2277 | } |
| 2278 | |
| 2279 | func printArchTypeNestedStarlarkDict(dict map[ArchType]map[string][]string) string { |
| 2280 | valDict := make(map[string]string, len(dict)) |
| 2281 | for k, v := range dict { |
| 2282 | valDict[k.String()] = starlark_fmt.PrintStringListDict(v, 1) |
| 2283 | } |
| 2284 | return starlark_fmt.PrintDict(valDict, 0) |
| 2285 | } |
| 2286 | |
| 2287 | func StarlarkArchConfigurations() string { |
| 2288 | return fmt.Sprintf(` |
| 2289 | _arch_to_variants = %s |
| 2290 | |
| 2291 | _arch_to_cpu_variants = %s |
| 2292 | |
| 2293 | _arch_to_features = %s |
| 2294 | |
| 2295 | _android_arch_feature_for_arch_variant = %s |
| 2296 | |
| 2297 | arch_to_variants = _arch_to_variants |
| 2298 | arch_to_cpu_variants = _arch_to_cpu_variants |
| 2299 | arch_to_features = _arch_to_features |
| 2300 | android_arch_feature_for_arch_variants = _android_arch_feature_for_arch_variant |
| 2301 | `, printArchTypeStarlarkDict(archVariants), |
| 2302 | printArchTypeStarlarkDict(cpuVariants), |
| 2303 | printArchTypeStarlarkDict(archFeatures), |
| 2304 | printArchTypeNestedStarlarkDict(androidArchFeatureMap), |
| 2305 | ) |
| 2306 | } |