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