blob: e2d0d0dbbbd637e04285f7a8fcdbb84b34bf5d79 [file] [log] [blame]
Colin Cross3f40fa42015-01-30 17:27:36 -08001// 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 Cross635c3b02016-05-18 15:37:25 -070015package android
Colin Cross3f40fa42015-01-30 17:27:36 -080016
17import (
Colin Cross74ba9622019-02-11 15:11:14 -080018 "encoding"
Colin Cross3f40fa42015-01-30 17:27:36 -080019 "fmt"
20 "reflect"
21 "runtime"
Ivan Lozano03b717d2024-07-18 15:13:50 +000022 "slices"
Colin Cross3f40fa42015-01-30 17:27:36 -080023 "strings"
Colin Crossf6566ed2015-03-24 11:13:38 -070024
Colin Cross0f7d2ef2019-10-16 11:03:10 -070025 "github.com/google/blueprint"
Colin Crossf6566ed2015-03-24 11:13:38 -070026 "github.com/google/blueprint/proptools"
Colin Cross3f40fa42015-01-30 17:27:36 -080027)
28
Colin Cross3f40fa42015-01-30 17:27:36 -080029/*
30Example blueprints file containing all variant property groups, with comment listing what type
31of variants get properties in that group:
32
33module {
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 Cross3f40fa42015-01-30 17:27:36 -080041 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 Stjernholme284b482020-09-23 21:03:27 +010058 // Device variants (implies Bionic)
Colin Cross3f40fa42015-01-30 17:27:36 -080059 },
60 host: {
61 // Host variants
62 },
Martin Stjernholme284b482020-09-23 21:03:27 +010063 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 Willemsen5746bd42017-10-02 19:42:01 -070072 linux_glibc: {
Martin Stjernholme284b482020-09-23 21:03:27 +010073 // Linux host variants (using non-Bionic libc)
Colin Cross3f40fa42015-01-30 17:27:36 -080074 },
75 darwin: {
76 // Darwin host variants
77 },
78 windows: {
79 // Windows host variants
80 },
81 not_windows: {
82 // Non-windows host variants
83 },
Martin Stjernholme284b482020-09-23 21:03:27 +010084 android_arm: {
85 // Any <os>_<arch> combination restricts to that os and arch
86 },
Colin Cross3f40fa42015-01-30 17:27:36 -080087 },
88}
89*/
Colin Cross7d5136f2015-05-11 13:39:40 -070090
Colin Cross3f40fa42015-01-30 17:27:36 -080091// An Arch indicates a single CPU architecture.
92type Arch struct {
Colin Crossa6845402020-11-16 15:08:19 -080093 // 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 Crossc5c24ad2015-11-20 15:35:00 -0800106 ArchFeatures []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800107}
108
Colin Crossa6845402020-11-16 15:08:19 -0800109// String returns the Arch as a string. The value is used as the name of the variant created
110// by archMutator.
Colin Cross3f40fa42015-01-30 17:27:36 -0800111func (a Arch) String() string {
Colin Crossd3ba0392015-05-07 14:11:29 -0700112 s := a.ArchType.String()
Colin Cross3f40fa42015-01-30 17:27:36 -0800113 if a.ArchVariant != "" {
114 s += "_" + a.ArchVariant
115 }
116 if a.CpuVariant != "" {
117 s += "_" + a.CpuVariant
118 }
119 return s
120}
121
Colin Crossa6845402020-11-16 15:08:19 -0800122// 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 Cross3f40fa42015-01-30 17:27:36 -0800125type ArchType struct {
Colin Crossa6845402020-11-16 15:08:19 -0800126 // 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 Crossec193632015-07-06 17:49:43 -0700133 Multilib string
Colin Cross3f40fa42015-01-30 17:27:36 -0800134}
135
Colin Crossa6845402020-11-16 15:08:19 -0800136// String returns the name of the ArchType.
137func (a ArchType) String() string {
138 return a.Name
139}
140
Spandan Dasd4530d62024-09-26 00:46:12 +0000141func (a ArchType) Bitness() string {
142 if a.Multilib == "lib32" {
143 return "32"
144 }
Spandan Dasa8b74522024-09-28 04:41:57 +0000145 if a.Multilib == "lib64" {
146 return "64"
147 }
148 panic("Bitness is not defined for the common variant")
Spandan Dasd4530d62024-09-26 00:46:12 +0000149}
150
Colin Crossa6845402020-11-16 15:08:19 -0800151const COMMON_VARIANT = "common"
152
153var (
154 archTypeList []ArchType
155
Colin Crossf05b0d32022-07-14 18:10:34 -0700156 Arm = newArch("arm", "lib32")
157 Arm64 = newArch("arm64", "lib64")
158 Riscv64 = newArch("riscv64", "lib64")
159 X86 = newArch("x86", "lib32")
160 X86_64 = newArch("x86_64", "lib64")
Colin Crossa6845402020-11-16 15:08:19 -0800161
162 Common = ArchType{
163 Name: COMMON_VARIANT,
164 }
165)
166
167var archTypeMap = map[string]ArchType{}
168
Colin Crossec193632015-07-06 17:49:43 -0700169func newArch(name, multilib string) ArchType {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700170 archType := ArchType{
Colin Crossec193632015-07-06 17:49:43 -0700171 Name: name,
Dan Willemsenb1957a52016-06-23 23:44:54 -0700172 Field: proptools.FieldNameForProperty(name),
Colin Crossec193632015-07-06 17:49:43 -0700173 Multilib: multilib,
Colin Cross3f40fa42015-01-30 17:27:36 -0800174 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700175 archTypeList = append(archTypeList, archType)
Colin Crossa6845402020-11-16 15:08:19 -0800176 archTypeMap[name] = archType
Dan Willemsenb1957a52016-06-23 23:44:54 -0700177 return archType
Colin Cross3f40fa42015-01-30 17:27:36 -0800178}
179
Ustaeabf0f32021-12-06 15:17:23 -0500180// ArchTypeList returns a slice copy of the 4 supported ArchTypes for arm,
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000181// arm64, x86 and x86_64.
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -0700182func ArchTypeList() []ArchType {
183 return append([]ArchType(nil), archTypeList...)
184}
185
Colin Crossa6845402020-11-16 15:08:19 -0800186// MarshalText allows an ArchType to be serialized through any encoder that supports
187// encoding.TextMarshaler.
Colin Cross74ba9622019-02-11 15:11:14 -0800188func (a ArchType) MarshalText() ([]byte, error) {
Jeongik Chabec4d032021-04-15 08:55:38 +0900189 return []byte(a.String()), nil
Colin Cross74ba9622019-02-11 15:11:14 -0800190}
191
Colin Crossa6845402020-11-16 15:08:19 -0800192var _ encoding.TextMarshaler = ArchType{}
Colin Cross74ba9622019-02-11 15:11:14 -0800193
Colin Crossa6845402020-11-16 15:08:19 -0800194// UnmarshalText allows an ArchType to be deserialized through any decoder that supports
195// encoding.TextUnmarshaler.
Colin Cross74ba9622019-02-11 15:11:14 -0800196func (a *ArchType) UnmarshalText(text []byte) error {
197 if u, ok := archTypeMap[string(text)]; ok {
198 *a = u
199 return nil
200 }
201
202 return fmt.Errorf("unknown ArchType %q", text)
203}
204
Colin Crossa6845402020-11-16 15:08:19 -0800205var _ encoding.TextUnmarshaler = &ArchType{}
Colin Crossa1ad8d12016-06-01 17:09:44 -0700206
Colin Crossa6845402020-11-16 15:08:19 -0800207// OsClass is an enum that describes whether a variant of a module runs on the host, on the device,
208// or is generic.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700209type OsClass int
210
211const (
Colin Crossa6845402020-11-16 15:08:19 -0800212 // Generic is used for variants of modules that are not OS-specific.
Dan Willemsen0e2d97b2016-11-28 17:50:06 -0800213 Generic OsClass = iota
Colin Crossa6845402020-11-16 15:08:19 -0800214 // Device is used for variants of modules that run on the device.
Dan Willemsen0e2d97b2016-11-28 17:50:06 -0800215 Device
Colin Crossa6845402020-11-16 15:08:19 -0800216 // Host is used for variants of modules that run on the host.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700217 Host
Colin Crossa1ad8d12016-06-01 17:09:44 -0700218)
219
Colin Crossa6845402020-11-16 15:08:19 -0800220// String returns the OsClass as a string.
Colin Cross67a5c132017-05-09 13:45:28 -0700221func (class OsClass) String() string {
222 switch class {
223 case Generic:
224 return "generic"
225 case Device:
226 return "device"
227 case Host:
228 return "host"
Colin Cross67a5c132017-05-09 13:45:28 -0700229 default:
230 panic(fmt.Errorf("unknown class %d", class))
231 }
232}
233
Colin Crossa6845402020-11-16 15:08:19 -0800234// OsType describes an OS variant of a module.
235type OsType struct {
236 // Name is the name of the OS. It is also used as the name of the property in Android.bp
237 // files.
238 Name string
239
240 // Field is the name of the OS converted to an exported field name, i.e. with the first
241 // character capitalized.
242 Field string
243
244 // Class is the OsClass of the OS.
245 Class OsClass
246
247 // DefaultDisabled is set when the module variants for the OS should not be created unless
248 // the module explicitly requests them. This is used to limit Windows cross compilation to
249 // only modules that need it.
250 DefaultDisabled bool
251}
252
253// String returns the name of the OsType.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700254func (os OsType) String() string {
255 return os.Name
Colin Cross54c71122016-06-01 17:09:44 -0700256}
257
Colin Crossa6845402020-11-16 15:08:19 -0800258// Bionic returns true if the OS uses the Bionic libc runtime, i.e. if the OS is Android or
259// is Linux with Bionic.
Dan Willemsen866b5632017-09-22 12:28:24 -0700260func (os OsType) Bionic() bool {
261 return os == Android || os == LinuxBionic
262}
263
Colin Crossa6845402020-11-16 15:08:19 -0800264// Linux returns true if the OS uses the Linux kernel, i.e. if the OS is Android or is Linux
265// with or without the Bionic libc runtime.
Dan Willemsen866b5632017-09-22 12:28:24 -0700266func (os OsType) Linux() bool {
Colin Cross528d67e2021-07-23 22:23:07 +0000267 return os == Android || os == Linux || os == LinuxBionic || os == LinuxMusl
Dan Willemsen866b5632017-09-22 12:28:24 -0700268}
269
Colin Crossa6845402020-11-16 15:08:19 -0800270// newOsType constructs an OsType and adds it to the global lists.
271func newOsType(name string, class OsClass, defDisabled bool, archTypes ...ArchType) OsType {
272 checkCalledFromInit()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700273 os := OsType{
274 Name: name,
Colin Crossa6845402020-11-16 15:08:19 -0800275 Field: proptools.FieldNameForProperty(name),
Colin Crossa1ad8d12016-06-01 17:09:44 -0700276 Class: class,
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800277
278 DefaultDisabled: defDisabled,
Colin Cross54c71122016-06-01 17:09:44 -0700279 }
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000280 osTypeList = append(osTypeList, os)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800281
282 if _, found := commonTargetMap[name]; found {
283 panic(fmt.Errorf("Found Os type duplicate during OsType registration: %q", name))
284 } else {
Colin Crosse9fe2942020-11-10 18:12:15 -0800285 commonTargetMap[name] = Target{Os: os, Arch: CommonArch}
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800286 }
Colin Crossa6845402020-11-16 15:08:19 -0800287 osArchTypeMap[os] = archTypes
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800288
Colin Crossa1ad8d12016-06-01 17:09:44 -0700289 return os
290}
291
Colin Crossa6845402020-11-16 15:08:19 -0800292// osByName returns the OsType that has the given name, or NoOsType if none match.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700293func osByName(name string) OsType {
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000294 for _, os := range osTypeList {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700295 if os.Name == name {
296 return os
297 }
298 }
299
300 return NoOsType
Dan Willemsen490fd492015-11-24 17:53:15 -0800301}
302
Colin Crossa6845402020-11-16 15:08:19 -0800303var (
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000304 // osTypeList contains a list of all the supported OsTypes, including ones not supported
Colin Crossa6845402020-11-16 15:08:19 -0800305 // by the current build host or the target device.
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000306 osTypeList []OsType
Colin Crossa6845402020-11-16 15:08:19 -0800307 // commonTargetMap maps names of OsTypes to the corresponding common Target, i.e. the
308 // Target with the same OsType and the common ArchType.
309 commonTargetMap = make(map[string]Target)
310 // osArchTypeMap maps OsTypes to the list of supported ArchTypes for that OS.
311 osArchTypeMap = map[OsType][]ArchType{}
312
313 // NoOsType is a placeholder for when no OS is needed.
314 NoOsType OsType
315 // Linux is the OS for the Linux kernel plus the glibc runtime.
316 Linux = newOsType("linux_glibc", Host, false, X86, X86_64)
Colin Cross528d67e2021-07-23 22:23:07 +0000317 // LinuxMusl is the OS for the Linux kernel plus the musl runtime.
Colin Crossa9b2aac2022-06-15 17:25:51 -0700318 LinuxMusl = newOsType("linux_musl", Host, false, X86, X86_64, Arm64, Arm)
Colin Crossa6845402020-11-16 15:08:19 -0800319 // Darwin is the OS for MacOS/Darwin host machines.
Dan Willemsen8528f4e2021-10-19 00:22:06 -0700320 Darwin = newOsType("darwin", Host, false, Arm64, X86_64)
Colin Crossa6845402020-11-16 15:08:19 -0800321 // LinuxBionic is the OS for the Linux kernel plus the Bionic libc runtime, but without the
322 // rest of Android.
323 LinuxBionic = newOsType("linux_bionic", Host, false, Arm64, X86_64)
324 // Windows the OS for Windows host machines.
325 Windows = newOsType("windows", Host, true, X86, X86_64)
326 // Android is the OS for target devices that run all of Android, including the Linux kernel
327 // and the Bionic libc runtime.
Colin Crossf05b0d32022-07-14 18:10:34 -0700328 Android = newOsType("android", Device, false, Arm, Arm64, Riscv64, X86, X86_64)
Colin Crossa6845402020-11-16 15:08:19 -0800329
330 // CommonOS is a pseudo OSType for a common OS variant, which is OsType agnostic and which
331 // has dependencies on all the OS variants.
332 CommonOS = newOsType("common_os", Generic, false)
Colin Crosse9fe2942020-11-10 18:12:15 -0800333
334 // CommonArch is the Arch for all modules that are os-specific but not arch specific,
335 // for example most Java modules.
336 CommonArch = Arch{ArchType: Common}
dimitry1f33e402019-03-26 12:39:31 +0100337)
338
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000339// OsTypeList returns a slice copy of the supported OsTypes.
340func OsTypeList() []OsType {
341 return append([]OsType(nil), osTypeList...)
342}
343
Colin Crossa6845402020-11-16 15:08:19 -0800344// Target specifies the OS and architecture that a module is being compiled for.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700345type Target struct {
Colin Crossa6845402020-11-16 15:08:19 -0800346 // Os the OS that the module is being compiled for (e.g. "linux_glibc", "android").
347 Os OsType
348 // Arch is the architecture that the module is being compiled for.
349 Arch Arch
350 // NativeBridge is NativeBridgeEnabled if the architecture is supported using NativeBridge
351 // (i.e. arm on x86) for this device.
352 NativeBridge NativeBridgeSupport
353 // NativeBridgeHostArchName is the name of the real architecture that is used to implement
354 // the NativeBridge architecture. For example, for arm on x86 this would be "x86".
dimitry8d6dde82019-07-11 10:23:53 +0200355 NativeBridgeHostArchName string
Colin Crossa6845402020-11-16 15:08:19 -0800356 // NativeBridgeRelativePath is the name of the subdirectory that will contain NativeBridge
357 // libraries and binaries.
dimitry8d6dde82019-07-11 10:23:53 +0200358 NativeBridgeRelativePath string
Jiyong Park1613e552020-09-14 19:43:17 +0900359
360 // HostCross is true when the target cannot run natively on the current build host.
361 // For example, linux_glibc_x86 returns true on a regular x86/i686/Linux machines, but returns false
362 // on Mac (different OS), or on 64-bit only i686/Linux machines (unsupported arch).
363 HostCross bool
Colin Crossd3ba0392015-05-07 14:11:29 -0700364}
365
Colin Crossa6845402020-11-16 15:08:19 -0800366// NativeBridgeSupport is an enum that specifies if a Target supports NativeBridge.
367type NativeBridgeSupport bool
368
369const (
370 NativeBridgeDisabled NativeBridgeSupport = false
371 NativeBridgeEnabled NativeBridgeSupport = true
372)
373
374// String returns the OS and arch variations used for the Target.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700375func (target Target) String() string {
Colin Crossa195f912019-10-16 11:07:20 -0700376 return target.OsVariation() + "_" + target.ArchVariation()
377}
378
Colin Crossa6845402020-11-16 15:08:19 -0800379// OsVariation returns the name of the variation used by the osMutator for the Target.
Colin Crossa195f912019-10-16 11:07:20 -0700380func (target Target) OsVariation() string {
381 return target.Os.String()
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700382}
383
Colin Crossa6845402020-11-16 15:08:19 -0800384// ArchVariation returns the name of the variation used by the archMutator for the Target.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700385func (target Target) ArchVariation() string {
386 var variation string
dimitry1f33e402019-03-26 12:39:31 +0100387 if target.NativeBridge {
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700388 variation = "native_bridge_"
dimitry1f33e402019-03-26 12:39:31 +0100389 }
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700390 variation += target.Arch.String()
391
Colin Crossa195f912019-10-16 11:07:20 -0700392 return variation
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700393}
394
Colin Crossa6845402020-11-16 15:08:19 -0800395// Variations returns a list of blueprint.Variations for the osMutator and archMutator for the
396// Target.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700397func (target Target) Variations() []blueprint.Variation {
398 return []blueprint.Variation{
Colin Crossa195f912019-10-16 11:07:20 -0700399 {Mutator: "os", Variation: target.OsVariation()},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700400 {Mutator: "arch", Variation: target.ArchVariation()},
401 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800402}
403
Colin Crossa6845402020-11-16 15:08:19 -0800404// osMutator splits an arch-specific module into a variant for each OS that is enabled for the
405// module. It uses the HostOrDevice value passed to InitAndroidArchModule and the
406// device_supported and host_supported properties to determine which OsTypes are enabled for this
407// module, then searches through the Targets to determine which have enabled Targets for this
408// module.
Colin Cross8bbc3d52024-09-11 15:33:54 -0700409type osTransitionMutator struct{}
Colin Crossa195f912019-10-16 11:07:20 -0700410
Colin Cross8bbc3d52024-09-11 15:33:54 -0700411type allOsInfo struct {
412 Os map[string]OsType
413 Variations []string
414}
Colin Crossa195f912019-10-16 11:07:20 -0700415
Colin Cross8bbc3d52024-09-11 15:33:54 -0700416var allOsProvider = blueprint.NewMutatorProvider[*allOsInfo]("os_propagate")
417
418// moduleOSList collects a list of OSTypes supported by this module based on the HostOrDevice
419// value passed to InitAndroidArchModule and the device_supported and host_supported properties.
420func moduleOSList(ctx ConfigContext, base *ModuleBase) []OsType {
Colin Crossa195f912019-10-16 11:07:20 -0700421 var moduleOSList []OsType
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000422 for _, os := range osTypeList {
Colin Cross8bbc3d52024-09-11 15:33:54 -0700423 for _, t := range ctx.Config().Targets[os] {
Colin Cross08d6f8f2020-11-19 02:33:19 +0000424 if base.supportsTarget(t) {
Jiyong Park1613e552020-09-14 19:43:17 +0900425 moduleOSList = append(moduleOSList, os)
426 break
Colin Crossa195f912019-10-16 11:07:20 -0700427 }
428 }
Colin Crossa195f912019-10-16 11:07:20 -0700429 }
430
Colin Cross8bbc3d52024-09-11 15:33:54 -0700431 if base.commonProperties.CreateCommonOSVariant {
432 // A CommonOS variant was requested so add it to the list of OS variants to
433 // create. It needs to be added to the end because it needs to depend on the
434 // the other variants and inter variant dependencies can only be created from a
435 // later variant in that list to an earlier one. That is because variants are
436 // always processed in the order in which they are created.
437 moduleOSList = append(moduleOSList, CommonOS)
438 }
439
440 return moduleOSList
441}
442
443func (o *osTransitionMutator) Split(ctx BaseModuleContext) []string {
444 module := ctx.Module()
445 base := module.base()
446
447 // Nothing to do for modules that are not architecture specific (e.g. a genrule).
448 if !base.ArchSpecific() {
449 return []string{""}
450 }
451
452 moduleOSList := moduleOSList(ctx, base)
Cole Faust8fc38f32023-12-12 17:14:22 -0800453
Colin Crossa6845402020-11-16 15:08:19 -0800454 // If there are no supported OSes then disable the module.
Colin Cross8bbc3d52024-09-11 15:33:54 -0700455 if len(moduleOSList) == 0 {
Inseob Kimeec88e12020-01-22 11:11:29 +0900456 base.Disable()
Colin Cross8bbc3d52024-09-11 15:33:54 -0700457 return []string{""}
Colin Crossa195f912019-10-16 11:07:20 -0700458 }
459
Colin Crossa6845402020-11-16 15:08:19 -0800460 // Convert the list of supported OsTypes to the variation names.
Colin Crossa195f912019-10-16 11:07:20 -0700461 osNames := make([]string, len(moduleOSList))
Colin Cross8bbc3d52024-09-11 15:33:54 -0700462 osMapping := make(map[string]OsType, len(moduleOSList))
Colin Crossa195f912019-10-16 11:07:20 -0700463 for i, os := range moduleOSList {
464 osNames[i] = os.String()
Colin Cross8bbc3d52024-09-11 15:33:54 -0700465 osMapping[osNames[i]] = os
Colin Crossa195f912019-10-16 11:07:20 -0700466 }
467
Colin Cross8bbc3d52024-09-11 15:33:54 -0700468 SetProvider(ctx, allOsProvider, &allOsInfo{
469 Os: osMapping,
470 Variations: osNames,
471 })
472
473 return osNames
474}
475
476func (o *osTransitionMutator) OutgoingTransition(ctx OutgoingTransitionContext, sourceVariation string) string {
477 return sourceVariation
478}
479
480func (o *osTransitionMutator) IncomingTransition(ctx IncomingTransitionContext, incomingVariation string) string {
481 module := ctx.Module()
482 base := module.base()
483
484 if !base.ArchSpecific() {
485 return ""
Colin Crossa195f912019-10-16 11:07:20 -0700486 }
487
Colin Cross8bbc3d52024-09-11 15:33:54 -0700488 return incomingVariation
489}
490
491func (o *osTransitionMutator) Mutate(ctx BottomUpMutatorContext, variation string) {
492 module := ctx.Module()
493 base := module.base()
494
495 if variation == "" {
496 return
497 }
498
499 allOsInfo, ok := ModuleProvider(ctx, allOsProvider)
500 if !ok {
501 panic(fmt.Errorf("missing allOsProvider"))
502 }
503
504 // Annotate this variant with which OS it was created for, and
Colin Crossa6845402020-11-16 15:08:19 -0800505 // squash the appropriate OS-specific properties into the top level properties.
Colin Cross8bbc3d52024-09-11 15:33:54 -0700506 base.commonProperties.CompileOS = allOsInfo.Os[variation]
507 base.setOSProperties(ctx)
Paul Duffin1356d8c2020-02-25 19:26:33 +0000508
Colin Cross8bbc3d52024-09-11 15:33:54 -0700509 if variation == CommonOS.String() {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000510 // A CommonOS variant was requested so add dependencies from it (the last one in
511 // the list) to the OS type specific variants.
Colin Cross8bbc3d52024-09-11 15:33:54 -0700512 osList := allOsInfo.Variations[:len(allOsInfo.Variations)-1]
513 for _, os := range osList {
514 variation := []blueprint.Variation{{"os", os}}
515 ctx.AddVariationDependencies(variation, commonOsToOsSpecificVariantTag, ctx.ModuleName())
Paul Duffin1356d8c2020-02-25 19:26:33 +0000516 }
517 }
518}
519
Colin Crossc179ea62020-10-09 10:54:15 -0700520type archDepTag struct {
521 blueprint.BaseDependencyTag
522 name string
523}
Paul Duffin1356d8c2020-02-25 19:26:33 +0000524
Colin Crossc179ea62020-10-09 10:54:15 -0700525// Identifies the dependency from CommonOS variant to the os specific variants.
526var commonOsToOsSpecificVariantTag = archDepTag{name: "common os to os specific"}
527
Paul Duffin1356d8c2020-02-25 19:26:33 +0000528// Get the OsType specific variants for the current CommonOS variant.
529//
530// The returned list will only contain enabled OsType specific variants of the
531// module referenced in the supplied context. An empty list is returned if there
532// are no enabled variants or the supplied context is not for an CommonOS
533// variant.
534func GetOsSpecificVariantsOfCommonOSVariant(mctx BaseModuleContext) []Module {
535 var variants []Module
536 mctx.VisitDirectDeps(func(m Module) {
537 if mctx.OtherModuleDependencyTag(m) == commonOsToOsSpecificVariantTag {
Cole Fausta963b942024-04-11 17:43:00 -0700538 if m.Enabled(mctx) {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000539 variants = append(variants, m)
540 }
541 }
542 })
Paul Duffin1356d8c2020-02-25 19:26:33 +0000543 return variants
Colin Crossa195f912019-10-16 11:07:20 -0700544}
545
Dan Willemsen47450072021-10-19 20:24:49 -0700546var DarwinUniversalVariantTag = archDepTag{name: "darwin universal binary"}
547
Colin Cross8bbc3d52024-09-11 15:33:54 -0700548// archTransitionMutator splits a module into a variant for each Target requested by the module. Target selection
Colin Crossa6845402020-11-16 15:08:19 -0800549// for a module is in three levels, OsClass, multilib, and then Target.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700550// OsClass selection is determined by:
Colin Crossd079e0b2022-08-16 10:27:33 -0700551// - The HostOrDeviceSupported value passed in to InitAndroidArchModule by the module type factory, which selects
552// whether the module type can compile for host, device or both.
553// - The host_supported and device_supported properties on the module.
554//
Roland Levillainf5b635d2019-06-05 14:42:57 +0100555// If host is supported for the module, the Host and HostCross OsClasses are selected. If device is supported
Colin Crossee0bc3b2018-10-02 22:01:37 -0700556// for the module, the Device OsClass is selected.
557// Within each selected OsClass, the multilib selection is determined by:
Colin Crossd079e0b2022-08-16 10:27:33 -0700558// - The compile_multilib property if it set (which may be overridden by target.android.compile_multilib or
559// target.host.compile_multilib).
560// - The default multilib passed to InitAndroidArchModule if compile_multilib was not set.
561//
Colin Crossee0bc3b2018-10-02 22:01:37 -0700562// Valid multilib values include:
Colin Crossd079e0b2022-08-16 10:27:33 -0700563//
564// "both": compile for all Targets supported by the OsClass (generally x86_64 and x86, or arm64 and arm).
565// "first": compile for only a single preferred Target supported by the OsClass. This is generally x86_64 or arm64,
566// but may be arm for a 32-bit only build.
567// "32": compile for only a single 32-bit Target supported by the OsClass.
568// "64": compile for only a single 64-bit Target supported by the OsClass.
569// "common": compile a for a single Target that will work on all Targets supported by the OsClass (for example Java).
570// "common_first": compile a for a Target that will work on all Targets supported by the OsClass
571// (same as "common"), plus a second Target for the preferred Target supported by the OsClass
572// (same as "first"). This is used for java_binary that produces a common .jar and a wrapper
573// executable script.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700574//
575// Once the list of Targets is determined, the module is split into a variant for each Target.
576//
577// Modules can be initialized with InitAndroidMultiTargetsArchModule, in which case they will be split by OsClass,
578// but will have a common Target that is expected to handle all other selected Targets via ctx.MultiTargets().
Colin Cross8bbc3d52024-09-11 15:33:54 -0700579type archTransitionMutator struct{}
580
581type allArchInfo struct {
582 Targets map[string]Target
583 MultiTargets []Target
584 Primary string
585 Multilib string
586}
587
588var allArchProvider = blueprint.NewMutatorProvider[*allArchInfo]("arch_propagate")
589
590func (a *archTransitionMutator) Split(ctx BaseModuleContext) []string {
591 module := ctx.Module()
Colin Cross5eca7cb2018-10-02 14:02:10 -0700592 base := module.base()
593
594 if !base.ArchSpecific() {
Colin Cross8bbc3d52024-09-11 15:33:54 -0700595 return []string{""}
Colin Crossb9db4802016-06-03 01:50:47 +0000596 }
597
Colin Crossa195f912019-10-16 11:07:20 -0700598 os := base.commonProperties.CompileOS
Paul Duffin1356d8c2020-02-25 19:26:33 +0000599 if os == CommonOS {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000600 // Do not create arch specific variants for the CommonOS variant.
Colin Cross8bbc3d52024-09-11 15:33:54 -0700601 return []string{""}
Paul Duffin1356d8c2020-02-25 19:26:33 +0000602 }
603
Colin Cross8bbc3d52024-09-11 15:33:54 -0700604 osTargets := ctx.Config().Targets[os]
Ivan Lozanoc7eafa72024-07-16 17:55:33 +0000605
Colin Crossfb0c16e2019-11-20 17:12:35 -0800606 image := base.commonProperties.ImageVariation
Colin Crossa6845402020-11-16 15:08:19 -0800607 // Filter NativeBridge targets unless they are explicitly supported.
608 // Skip creating native bridge variants for non-core modules.
Paul Duffine3d1ae42021-09-03 17:47:17 +0100609 if os == Android && !(base.IsNativeBridgeSupported() && image == CoreVariation) {
Ivan Lozano03b717d2024-07-18 15:13:50 +0000610 osTargets = slices.DeleteFunc(slices.Clone(osTargets), func(t Target) bool {
611 return bool(t.NativeBridge)
612 })
Colin Crossa195f912019-10-16 11:07:20 -0700613 }
Colin Crossee0bc3b2018-10-02 22:01:37 -0700614
Ivan Lozanoc7eafa72024-07-16 17:55:33 +0000615 // Filter HostCross targets if disabled.
616 if base.HostSupported() && !base.HostCrossSupported() {
Ivan Lozano03b717d2024-07-18 15:13:50 +0000617 osTargets = slices.DeleteFunc(slices.Clone(osTargets), func(t Target) bool {
618 return t.HostCross
619 })
Ivan Lozanoc7eafa72024-07-16 17:55:33 +0000620 }
621
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700622 // only the primary arch in the ramdisk / vendor_ramdisk / recovery partition
Inseob Kim08758f02021-04-08 21:13:22 +0900623 if os == Android && (module.InstallInRecovery() || module.InstallInRamdisk() || module.InstallInVendorRamdisk() || module.InstallInDebugRamdisk()) {
Colin Crossa195f912019-10-16 11:07:20 -0700624 osTargets = []Target{osTargets[0]}
625 }
dimitry1f33e402019-03-26 12:39:31 +0100626
Jaewoong Jung003d8082021-02-24 17:39:54 -0800627 // Windows builds always prefer 32-bit
628 prefer32 := os == Windows
dimitry1f33e402019-03-26 12:39:31 +0100629
Colin Crossa6845402020-11-16 15:08:19 -0800630 // Determine the multilib selection for this module.
Colin Cross8bbc3d52024-09-11 15:33:54 -0700631 multilib, extraMultilib := decodeMultilib(ctx, base)
Colin Crossa6845402020-11-16 15:08:19 -0800632
633 // Convert the multilib selection into a list of Targets.
Colin Crossa195f912019-10-16 11:07:20 -0700634 targets, err := decodeMultilibTargets(multilib, osTargets, prefer32)
635 if err != nil {
Colin Cross8bbc3d52024-09-11 15:33:54 -0700636 ctx.ModuleErrorf("%s", err.Error())
Colin Crossa195f912019-10-16 11:07:20 -0700637 }
Colin Cross5eca7cb2018-10-02 14:02:10 -0700638
Colin Crossc0f0eb82022-07-19 14:41:11 -0700639 // If there are no supported targets disable the module.
640 if len(targets) == 0 {
641 base.Disable()
Colin Cross8bbc3d52024-09-11 15:33:54 -0700642 return []string{""}
Colin Crossc0f0eb82022-07-19 14:41:11 -0700643 }
644
Colin Crossa6845402020-11-16 15:08:19 -0800645 // If the module is using extraMultilib, decode the extraMultilib selection into
646 // a separate list of Targets.
Colin Crossa195f912019-10-16 11:07:20 -0700647 var multiTargets []Target
648 if extraMultilib != "" {
649 multiTargets, err = decodeMultilibTargets(extraMultilib, osTargets, prefer32)
Colin Crossa1ad8d12016-06-01 17:09:44 -0700650 if err != nil {
Colin Cross8bbc3d52024-09-11 15:33:54 -0700651 ctx.ModuleErrorf("%s", err.Error())
Colin Crossa1ad8d12016-06-01 17:09:44 -0700652 }
Colin Crossc0f0eb82022-07-19 14:41:11 -0700653 multiTargets = filterHostCross(multiTargets, targets[0].HostCross)
Colin Crossb9db4802016-06-03 01:50:47 +0000654 }
655
Colin Crossa6845402020-11-16 15:08:19 -0800656 // Recovery is always the primary architecture, filter out any other architectures.
Inseob Kim20fb5d42021-02-02 20:07:58 +0900657 // Common arch is also allowed
Colin Crossfb0c16e2019-11-20 17:12:35 -0800658 if image == RecoveryVariation {
Colin Cross8bbc3d52024-09-11 15:33:54 -0700659 primaryArch := ctx.Config().DevicePrimaryArchType()
Inseob Kim20fb5d42021-02-02 20:07:58 +0900660 targets = filterToArch(targets, primaryArch, Common)
661 multiTargets = filterToArch(multiTargets, primaryArch, Common)
Colin Crossfb0c16e2019-11-20 17:12:35 -0800662 }
663
Colin Crossa6845402020-11-16 15:08:19 -0800664 // If there are no supported targets disable the module.
Colin Crossa195f912019-10-16 11:07:20 -0700665 if len(targets) == 0 {
Inseob Kimeec88e12020-01-22 11:11:29 +0900666 base.Disable()
Colin Cross8bbc3d52024-09-11 15:33:54 -0700667 return []string{""}
Dan Willemsen3f32f032016-07-11 14:36:48 -0700668 }
669
Colin Crossa6845402020-11-16 15:08:19 -0800670 // Convert the targets into a list of arch variation names.
Colin Crossa195f912019-10-16 11:07:20 -0700671 targetNames := make([]string, len(targets))
Colin Cross8bbc3d52024-09-11 15:33:54 -0700672 targetMapping := make(map[string]Target, len(targets))
Colin Crossa195f912019-10-16 11:07:20 -0700673 for i, target := range targets {
674 targetNames[i] = target.ArchVariation()
Colin Cross8bbc3d52024-09-11 15:33:54 -0700675 targetMapping[targetNames[i]] = targets[i]
Colin Crossa1ad8d12016-06-01 17:09:44 -0700676 }
677
Colin Cross8bbc3d52024-09-11 15:33:54 -0700678 SetProvider(ctx, allArchProvider, &allArchInfo{
679 Targets: targetMapping,
680 MultiTargets: multiTargets,
681 Primary: targetNames[0],
682 Multilib: multilib,
683 })
684 return targetNames
685}
Dan Willemsen8528f4e2021-10-19 00:22:06 -0700686
Colin Cross8bbc3d52024-09-11 15:33:54 -0700687func (a *archTransitionMutator) OutgoingTransition(ctx OutgoingTransitionContext, sourceVariation string) string {
688 return sourceVariation
689}
690
691func (a *archTransitionMutator) IncomingTransition(ctx IncomingTransitionContext, incomingVariation string) string {
692 module := ctx.Module()
693 base := module.base()
694
695 if !base.ArchSpecific() {
696 return ""
697 }
698
699 os := base.commonProperties.CompileOS
700 if os == CommonOS {
701 // Do not create arch specific variants for the CommonOS variant.
702 return ""
703 }
704
705 if incomingVariation == "" {
706 multilib, _ := decodeMultilib(ctx, base)
707 if multilib == "common" {
708 return "common"
Dan Willemsen8528f4e2021-10-19 00:22:06 -0700709 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800710 }
Colin Cross8bbc3d52024-09-11 15:33:54 -0700711 return incomingVariation
712}
713
714func (a *archTransitionMutator) Mutate(ctx BottomUpMutatorContext, variation string) {
715 module := ctx.Module()
716 base := module.base()
717 os := base.commonProperties.CompileOS
718
719 if os == CommonOS {
720 // Make sure that the target related properties are initialized for the
721 // CommonOS variant.
722 addTargetProperties(module, commonTargetMap[os.Name], nil, true)
723 return
724 }
725
726 if variation == "" {
727 return
728 }
729
730 if !base.ArchSpecific() {
731 panic(fmt.Errorf("found variation %q for non arch specifc module", variation))
732 }
733
734 allArchInfo, ok := ModuleProvider(ctx, allArchProvider)
735 if !ok {
736 return
737 }
738
739 target, ok := allArchInfo.Targets[variation]
740 if !ok {
741 panic(fmt.Errorf("missing Target for %q", variation))
742 }
743 primary := variation == allArchInfo.Primary
744 multiTargets := allArchInfo.MultiTargets
745
746 // Annotate the new variant with which Target it was created for, and
747 // squash the appropriate arch-specific properties into the top level properties.
748 addTargetProperties(ctx.Module(), target, multiTargets, primary)
749 base.setArchProperties(ctx)
750
751 // Install support doesn't understand Darwin+Arm64
752 if os == Darwin && target.HostCross {
753 base.commonProperties.SkipInstall = true
754 }
Dan Willemsen47450072021-10-19 20:24:49 -0700755
756 // Create a dependency for Darwin Universal binaries from the primary to secondary
757 // architecture. The module itself will be responsible for calling lipo to merge the outputs.
758 if os == Darwin {
Colin Cross8bbc3d52024-09-11 15:33:54 -0700759 isUniversalBinary := (allArchInfo.Multilib == "darwin_universal" && len(allArchInfo.Targets) == 2) ||
760 allArchInfo.Multilib == "darwin_universal_common_first" && len(allArchInfo.Targets) == 3
761 isPrimary := variation == ctx.Config().BuildArch.String()
762 hasSecondaryConfigured := len(ctx.Config().Targets[Darwin]) > 1
763 if isUniversalBinary && isPrimary && hasSecondaryConfigured {
764 secondaryArch := ctx.Config().Targets[Darwin][1].Arch.String()
765 variation := []blueprint.Variation{{"arch", secondaryArch}}
766 ctx.AddVariationDependencies(variation, DarwinUniversalVariantTag, ctx.ModuleName())
Dan Willemsen47450072021-10-19 20:24:49 -0700767 }
768 }
Colin Cross8bbc3d52024-09-11 15:33:54 -0700769
Colin Cross3f40fa42015-01-30 17:27:36 -0800770}
771
Colin Crossa6845402020-11-16 15:08:19 -0800772// addTargetProperties annotates a variant with the Target is is being compiled for, the list
773// of additional Targets it is supporting (if any), and whether it is the primary Target for
774// the module.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000775func addTargetProperties(m Module, target Target, multiTargets []Target, primaryTarget bool) {
776 m.base().commonProperties.CompileTarget = target
777 m.base().commonProperties.CompileMultiTargets = multiTargets
778 m.base().commonProperties.CompilePrimary = primaryTarget
Cole Faust0aa21cc2024-03-20 12:28:03 -0700779 m.base().commonProperties.ArchReady = true
Paul Duffin1356d8c2020-02-25 19:26:33 +0000780}
781
Colin Crossa6845402020-11-16 15:08:19 -0800782// decodeMultilib returns the appropriate compile_multilib property for the module, or the default
783// multilib from the factory's call to InitAndroidArchModule if none was set. For modules that
784// called InitAndroidMultiTargetsArchModule it always returns "common" for multilib, and returns
785// the actual multilib in extraMultilib.
Colin Cross8bbc3d52024-09-11 15:33:54 -0700786func decodeMultilib(ctx ConfigContext, base *ModuleBase) (multilib, extraMultilib string) {
787 os := base.commonProperties.CompileOS
788 ignorePrefer32OnDevice := ctx.Config().IgnorePrefer32OnDevice()
Colin Crossa6845402020-11-16 15:08:19 -0800789 // First check the "android.compile_multilib" or "host.compile_multilib" properties.
Dan Willemsen47450072021-10-19 20:24:49 -0700790 switch os.Class {
Colin Crossee0bc3b2018-10-02 22:01:37 -0700791 case Device:
792 multilib = String(base.commonProperties.Target.Android.Compile_multilib)
Jiyong Park1613e552020-09-14 19:43:17 +0900793 case Host:
Colin Crossee0bc3b2018-10-02 22:01:37 -0700794 multilib = String(base.commonProperties.Target.Host.Compile_multilib)
795 }
Colin Crossa6845402020-11-16 15:08:19 -0800796
797 // If those aren't set, try the "compile_multilib" property.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700798 if multilib == "" {
799 multilib = String(base.commonProperties.Compile_multilib)
800 }
Colin Crossa6845402020-11-16 15:08:19 -0800801
802 // If that wasn't set, use the default multilib set by the factory.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700803 if multilib == "" {
804 multilib = base.commonProperties.Default_multilib
805 }
806
Christopher Ferris98f10222022-07-13 23:16:52 -0700807 // If a device is configured with multiple targets, this option
808 // force all device targets that prefer32 to be compiled only as
809 // the first target.
810 if ignorePrefer32OnDevice && os.Class == Device && (multilib == "prefer32" || multilib == "first_prefer32") {
811 multilib = "first"
812 }
813
Colin Crossee0bc3b2018-10-02 22:01:37 -0700814 if base.commonProperties.UseTargetVariants {
Dan Willemsen47450072021-10-19 20:24:49 -0700815 // Darwin has the concept of "universal binaries" which is implemented in Soong by
816 // building both x86_64 and arm64 variants, and having select module types know how to
817 // merge the outputs of their corresponding variants together into a final binary. Most
818 // module types don't need to understand this logic, as we only build a small portion
819 // of the tree for Darwin, and only module types writing macho files need to do the
820 // merging.
821 //
822 // This logic is not enabled for:
823 // "common", as it's not an arch-specific variant
824 // "32", as Darwin never has a 32-bit variant
825 // !UseTargetVariants, as the module has opted into handling the arch-specific logic on
826 // its own.
827 if os == Darwin && multilib != "common" && multilib != "32" {
828 if multilib == "common_first" {
829 multilib = "darwin_universal_common_first"
830 } else {
831 multilib = "darwin_universal"
832 }
833 }
834
Colin Crossee0bc3b2018-10-02 22:01:37 -0700835 return multilib, ""
836 } else {
837 // For app modules a single arch variant will be created per OS class which is expected to handle all the
838 // selected arches. Return the common-type as multilib and any Android.bp provided multilib as extraMultilib
839 if multilib == base.commonProperties.Default_multilib {
840 multilib = "first"
841 }
842 return base.commonProperties.Default_multilib, multilib
843 }
844}
845
Colin Crossa6845402020-11-16 15:08:19 -0800846// filterToArch takes a list of Targets and an ArchType, and returns a modified list that contains
Inseob Kim20fb5d42021-02-02 20:07:58 +0900847// only Targets that have the specified ArchTypes.
848func filterToArch(targets []Target, archs ...ArchType) []Target {
Colin Crossfb0c16e2019-11-20 17:12:35 -0800849 for i := 0; i < len(targets); i++ {
Inseob Kim20fb5d42021-02-02 20:07:58 +0900850 found := false
851 for _, arch := range archs {
852 if targets[i].Arch.ArchType == arch {
853 found = true
854 break
855 }
856 }
857 if !found {
Colin Crossfb0c16e2019-11-20 17:12:35 -0800858 targets = append(targets[:i], targets[i+1:]...)
859 i--
860 }
861 }
862 return targets
863}
864
Colin Crossc0f0eb82022-07-19 14:41:11 -0700865// filterHostCross takes a list of Targets and a hostCross value, and returns a modified list
866// that contains only Targets that have the specified HostCross.
867func filterHostCross(targets []Target, hostCross bool) []Target {
868 for i := 0; i < len(targets); i++ {
869 if targets[i].HostCross != hostCross {
870 targets = append(targets[:i], targets[i+1:]...)
871 i--
872 }
873 }
874 return targets
875}
876
Colin Crossa6845402020-11-16 15:08:19 -0800877// archPropRoot is a struct type used as the top level of the arch-specific properties. It
878// contains the "arch", "multilib", and "target" property structs. It is used to split up the
879// property structs to limit how much is allocated when a single arch-specific property group is
880// used. The types are interface{} because they will hold instances of runtime-created types.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800881type archPropRoot struct {
882 Arch, Multilib, Target interface{}
883}
884
Colin Crossa6845402020-11-16 15:08:19 -0800885// archPropTypeDesc holds the runtime-created types for the property structs to instantiate to
886// create an archPropRoot property struct.
887type archPropTypeDesc struct {
888 arch, multilib, target reflect.Type
889}
890
Colin Crosscbbd13f2020-01-17 14:08:22 -0800891// createArchPropTypeDesc takes a reflect.Type that is either a struct or a pointer to a struct, and
892// returns lists of reflect.Types that contains the arch-variant properties inside structs for each
893// arch, multilib and target property.
Colin Crossa6845402020-11-16 15:08:19 -0800894//
895// This is a relatively expensive operation, so the results are cached in the global
896// archPropTypeMap. It is constructed entirely based on compile-time data, so there is no need
897// to isolate the results between multiple tests running in parallel.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800898func createArchPropTypeDesc(props reflect.Type) []archPropTypeDesc {
Colin Crossb1d8c992020-01-21 11:43:29 -0800899 // Each property struct shard will be nested many times under the runtime generated arch struct,
900 // which can hit the limit of 64kB for the name of runtime generated structs. They are nested
901 // 97 times now, which may grow in the future, plus there is some overhead for the containing
902 // type. This number may need to be reduced if too many are added, but reducing it too far
903 // could cause problems if a single deeply nested property no longer fits in the name.
904 const maxArchTypeNameSize = 500
905
Colin Crossa6845402020-11-16 15:08:19 -0800906 // Convert the type to a new set of types that contains only the arch-specific properties
Usta Shrestha0b52d832022-02-04 21:37:39 -0500907 // (those that are tagged with `android:"arch_variant"`), and sharded into multiple types
Colin Crossa6845402020-11-16 15:08:19 -0800908 // to keep the runtime-generated names under the limit.
Colin Crossb1d8c992020-01-21 11:43:29 -0800909 propShards, _ := proptools.FilterPropertyStructSharded(props, maxArchTypeNameSize, filterArchStruct)
Colin Crossa6845402020-11-16 15:08:19 -0800910
911 // If the type has no arch-specific properties there is nothing to do.
Colin Crosscb988072019-01-24 14:58:11 -0800912 if len(propShards) == 0 {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700913 return nil
914 }
915
Colin Crosscbbd13f2020-01-17 14:08:22 -0800916 var ret []archPropTypeDesc
Colin Crossc17727d2018-10-24 12:42:09 -0700917 for _, props := range propShards {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700918
Colin Crossa6845402020-11-16 15:08:19 -0800919 // variantFields takes a list of variant property field names and returns a list the
920 // StructFields with the names and the type of the current shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700921 variantFields := func(names []string) []reflect.StructField {
922 ret := make([]reflect.StructField, len(names))
Dan Willemsenb1957a52016-06-23 23:44:54 -0700923
Colin Crossc17727d2018-10-24 12:42:09 -0700924 for i, name := range names {
925 ret[i].Name = name
926 ret[i].Type = props
Dan Willemsen866b5632017-09-22 12:28:24 -0700927 }
Colin Crossc17727d2018-10-24 12:42:09 -0700928
929 return ret
930 }
931
Colin Crossa6845402020-11-16 15:08:19 -0800932 // Create a type that contains the properties in this shard repeated for each
933 // architecture, architecture variant, and architecture feature.
Colin Crossc17727d2018-10-24 12:42:09 -0700934 archFields := make([]reflect.StructField, len(archTypeList))
935 for i, arch := range archTypeList {
Colin Crossa6845402020-11-16 15:08:19 -0800936 var variants []string
Colin Crossc17727d2018-10-24 12:42:09 -0700937
938 for _, archVariant := range archVariants[arch] {
939 archVariant := variantReplacer.Replace(archVariant)
940 variants = append(variants, proptools.FieldNameForProperty(archVariant))
941 }
Liz Kammer2c2afe22022-02-11 11:35:03 -0500942 for _, cpuVariant := range cpuVariants[arch] {
943 cpuVariant := variantReplacer.Replace(cpuVariant)
944 variants = append(variants, proptools.FieldNameForProperty(cpuVariant))
945 }
Colin Crossc17727d2018-10-24 12:42:09 -0700946 for _, feature := range archFeatures[arch] {
947 feature := variantReplacer.Replace(feature)
948 variants = append(variants, proptools.FieldNameForProperty(feature))
949 }
950
Colin Crossa6845402020-11-16 15:08:19 -0800951 // Create the StructFields for each architecture variant architecture feature
952 // (e.g. "arch.arm.cortex-a53" or "arch.arm.neon").
Colin Crossc17727d2018-10-24 12:42:09 -0700953 fields := variantFields(variants)
954
Colin Crossa6845402020-11-16 15:08:19 -0800955 // Create the StructField for the architecture itself (e.g. "arch.arm"). The special
956 // "BlueprintEmbed" name is used by Blueprint to put the properties in the
957 // parent struct.
Colin Crossc17727d2018-10-24 12:42:09 -0700958 fields = append([]reflect.StructField{{
959 Name: "BlueprintEmbed",
960 Type: props,
961 Anonymous: true,
962 }}, fields...)
963
964 archFields[i] = reflect.StructField{
965 Name: arch.Field,
966 Type: reflect.StructOf(fields),
967 }
968 }
Colin Crossa6845402020-11-16 15:08:19 -0800969
970 // Create the type of the "arch" property struct for this shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700971 archType := reflect.StructOf(archFields)
972
Colin Crossa6845402020-11-16 15:08:19 -0800973 // Create the type for the "multilib" property struct for this shard, containing the
974 // "multilib.lib32" and "multilib.lib64" property structs.
Colin Crossc17727d2018-10-24 12:42:09 -0700975 multilibType := reflect.StructOf(variantFields([]string{"Lib32", "Lib64"}))
976
Colin Crossa6845402020-11-16 15:08:19 -0800977 // Start with a list of the special targets
Colin Crossc17727d2018-10-24 12:42:09 -0700978 targets := []string{
979 "Host",
980 "Android64",
981 "Android32",
982 "Bionic",
Colin Cross528d67e2021-07-23 22:23:07 +0000983 "Glibc",
984 "Musl",
Colin Crossc17727d2018-10-24 12:42:09 -0700985 "Linux",
Colin Crossa98d36d2022-03-07 14:39:49 -0800986 "Host_linux",
Colin Crossc17727d2018-10-24 12:42:09 -0700987 "Not_windows",
988 "Arm_on_x86",
989 "Arm_on_x86_64",
Victor Khimenkoc26fcf42020-05-07 22:16:33 +0200990 "Native_bridge",
Colin Crossc17727d2018-10-24 12:42:09 -0700991 }
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000992 for _, os := range osTypeList {
Colin Crossa6845402020-11-16 15:08:19 -0800993 // Add all the OSes.
Colin Crossc17727d2018-10-24 12:42:09 -0700994 targets = append(targets, os.Field)
995
Colin Crossa6845402020-11-16 15:08:19 -0800996 // Add the OS/Arch combinations, e.g. "android_arm64".
Colin Crossc17727d2018-10-24 12:42:09 -0700997 for _, archType := range osArchTypeMap[os] {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400998 targets = append(targets, GetCompoundTargetField(os, archType))
Colin Crossc17727d2018-10-24 12:42:09 -0700999
Colin Cross1aa45b02022-02-10 10:33:10 -08001000 // Also add the special "linux_<arch>", "bionic_<arch>" , "glibc_<arch>", and
1001 // "musl_<arch>" property structs.
Colin Crossc17727d2018-10-24 12:42:09 -07001002 if os.Linux() {
1003 target := "Linux_" + archType.Name
1004 if !InList(target, targets) {
1005 targets = append(targets, target)
1006 }
1007 }
Colin Crossa98d36d2022-03-07 14:39:49 -08001008 if os.Linux() && os.Class == Host {
1009 target := "Host_linux_" + archType.Name
1010 if !InList(target, targets) {
1011 targets = append(targets, target)
1012 }
1013 }
Colin Crossc17727d2018-10-24 12:42:09 -07001014 if os.Bionic() {
1015 target := "Bionic_" + archType.Name
1016 if !InList(target, targets) {
1017 targets = append(targets, target)
1018 }
Dan Willemsen866b5632017-09-22 12:28:24 -07001019 }
Colin Cross1aa45b02022-02-10 10:33:10 -08001020 if os == Linux {
1021 target := "Glibc_" + archType.Name
1022 if !InList(target, targets) {
1023 targets = append(targets, target)
1024 }
1025 }
1026 if os == LinuxMusl {
1027 target := "Musl_" + archType.Name
1028 if !InList(target, targets) {
1029 targets = append(targets, target)
1030 }
1031 }
Dan Willemsen866b5632017-09-22 12:28:24 -07001032 }
Dan Willemsenb1957a52016-06-23 23:44:54 -07001033 }
Dan Willemsenb1957a52016-06-23 23:44:54 -07001034
Colin Crossa6845402020-11-16 15:08:19 -08001035 // Create the type for the "target" property struct for this shard.
Colin Crossc17727d2018-10-24 12:42:09 -07001036 targetType := reflect.StructOf(variantFields(targets))
Colin Crosscbbd13f2020-01-17 14:08:22 -08001037
Colin Crossa6845402020-11-16 15:08:19 -08001038 // Return a descriptor of the 3 runtime-created types.
Colin Crosscbbd13f2020-01-17 14:08:22 -08001039 ret = append(ret, archPropTypeDesc{
1040 arch: reflect.PtrTo(archType),
1041 multilib: reflect.PtrTo(multilibType),
1042 target: reflect.PtrTo(targetType),
1043 })
Colin Crossc17727d2018-10-24 12:42:09 -07001044 }
1045 return ret
Dan Willemsenb1957a52016-06-23 23:44:54 -07001046}
1047
Colin Crossa6845402020-11-16 15:08:19 -08001048// variantReplacer converts architecture variant or architecture feature names into names that
1049// are valid for an Android.bp file.
1050var variantReplacer = strings.NewReplacer("-", "_", ".", "_")
1051
1052// filterArchStruct returns true if the given field is an architecture specific property.
Colin Cross74449102019-09-25 11:26:40 -07001053func filterArchStruct(field reflect.StructField, prefix string) (bool, reflect.StructField) {
1054 if proptools.HasTag(field, "android", "arch_variant") {
1055 // The arch_variant field isn't necessary past this point
1056 // Instead of wasting space, just remove it. Go also has a
1057 // 16-bit limit on structure name length. The name is constructed
1058 // based on the Go source representation of the structure, so
1059 // the tag names count towards that length.
Colin Crossb4fecbf2020-01-21 11:38:47 -08001060
1061 androidTag := field.Tag.Get("android")
1062 values := strings.Split(androidTag, ",")
1063
1064 if string(field.Tag) != `android:"`+strings.Join(values, ",")+`"` {
1065 panic(fmt.Errorf("unexpected tag format %q", field.Tag))
Colin Cross74449102019-09-25 11:26:40 -07001066 }
Colin Crossb4fecbf2020-01-21 11:38:47 -08001067 // these tags don't need to be present in the runtime generated struct type.
Cole Faust5fda87b2024-04-24 11:21:14 -07001068 // However replace_instead_of_append does, because it's read by the blueprint
1069 // property extending util functions, which can operate on these generated arch
1070 // property structs.
1071 values = RemoveListFromList(values, []string{"arch_variant", "variant_prepend", "path"})
Liz Kammerff966b12022-07-29 10:49:16 -04001072 if len(values) > 0 {
Cole Faust5fda87b2024-04-24 11:21:14 -07001073 if values[0] != "replace_instead_of_append" || len(values) > 1 {
1074 panic(fmt.Errorf("unknown tags %q in field %q", values, prefix+field.Name))
1075 }
1076 field.Tag = `android:"replace_instead_of_append"`
1077 } else {
1078 field.Tag = ``
Colin Crossb4fecbf2020-01-21 11:38:47 -08001079 }
Colin Cross74449102019-09-25 11:26:40 -07001080 return true, field
1081 }
1082 return false, field
1083}
1084
Colin Crossa6845402020-11-16 15:08:19 -08001085// archPropTypeMap contains a cache of the results of createArchPropTypeDesc for each type. It is
1086// shared across all Contexts, but is constructed based only on compile-time information so there
1087// is no risk of contaminating one Context with data from another.
Dan Willemsenb1957a52016-06-23 23:44:54 -07001088var archPropTypeMap OncePer
1089
Colin Crossa6845402020-11-16 15:08:19 -08001090// initArchModule adds the architecture-specific property structs to a Module.
1091func initArchModule(m Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001092
1093 base := m.base()
1094
Ustaeabf0f32021-12-06 15:17:23 -05001095 if len(base.archProperties) != 0 {
1096 panic(fmt.Errorf("module %s already has archProperties", m.Name()))
1097 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001098
Ustaeabf0f32021-12-06 15:17:23 -05001099 getStructType := func(properties interface{}) reflect.Type {
Colin Cross3f40fa42015-01-30 17:27:36 -08001100 propertiesValue := reflect.ValueOf(properties)
Colin Cross62496a02016-08-08 15:49:17 -07001101 t := propertiesValue.Type()
Colin Cross3f40fa42015-01-30 17:27:36 -08001102 if propertiesValue.Kind() != reflect.Ptr {
Colin Crossca860ac2016-01-04 14:34:37 -08001103 panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
1104 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -08001105 }
1106
1107 propertiesValue = propertiesValue.Elem()
1108 if propertiesValue.Kind() != reflect.Struct {
Ustaeabf0f32021-12-06 15:17:23 -05001109 panic(fmt.Errorf("properties must be a pointer to a struct, got a pointer to %T",
Colin Crossca860ac2016-01-04 14:34:37 -08001110 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -08001111 }
Ustaeabf0f32021-12-06 15:17:23 -05001112 return t
1113 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001114
Usta851a3272022-01-05 23:42:33 -05001115 for _, properties := range m.GetProperties() {
Ustaeabf0f32021-12-06 15:17:23 -05001116 t := getStructType(properties)
Colin Crossa6845402020-11-16 15:08:19 -08001117 // Get or create the arch-specific property struct types for this property struct type.
Colin Cross571cccf2019-02-04 11:22:08 -08001118 archPropTypes := archPropTypeMap.Once(NewCustomOnceKey(t), func() interface{} {
Colin Crosscbbd13f2020-01-17 14:08:22 -08001119 return createArchPropTypeDesc(t)
1120 }).([]archPropTypeDesc)
Colin Cross3f40fa42015-01-30 17:27:36 -08001121
Colin Crossa6845402020-11-16 15:08:19 -08001122 // Instantiate one of each arch-specific property struct type and add it to the
1123 // properties for the Module.
Colin Crossc17727d2018-10-24 12:42:09 -07001124 var archProperties []interface{}
1125 for _, t := range archPropTypes {
Colin Crosscbbd13f2020-01-17 14:08:22 -08001126 archProperties = append(archProperties, &archPropRoot{
1127 Arch: reflect.Zero(t.arch).Interface(),
1128 Multilib: reflect.Zero(t.multilib).Interface(),
1129 Target: reflect.Zero(t.target).Interface(),
1130 })
Dan Willemsenb1957a52016-06-23 23:44:54 -07001131 }
Colin Crossc17727d2018-10-24 12:42:09 -07001132 base.archProperties = append(base.archProperties, archProperties)
1133 m.AddProperties(archProperties...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001134 }
1135
Colin Cross3f40fa42015-01-30 17:27:36 -08001136}
1137
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001138func maybeBlueprintEmbed(src reflect.Value) reflect.Value {
Colin Crossa6845402020-11-16 15:08:19 -08001139 // If the value of the field is a struct (as opposed to a pointer to a struct) then step
1140 // into the BlueprintEmbed field.
Dan Willemsenb1957a52016-06-23 23:44:54 -07001141 if src.Kind() == reflect.Struct {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001142 return src.FieldByName("BlueprintEmbed")
1143 } else {
1144 return src
Colin Cross06a931b2015-10-28 17:23:31 -07001145 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001146}
1147
1148// Merges the property struct in srcValue into dst.
Liz Kammerb6dbc872021-05-14 15:14:40 -04001149func mergePropertyStruct(ctx ArchVariantContext, dst interface{}, srcValue reflect.Value) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001150 src := maybeBlueprintEmbed(srcValue).Interface()
Colin Cross06a931b2015-10-28 17:23:31 -07001151
Colin Crossa6845402020-11-16 15:08:19 -08001152 // order checks the `android:"variant_prepend"` tag to handle properties where the
1153 // arch-specific value needs to come before the generic value, for example for lists of
1154 // include directories.
Colin Cross1e7e0432024-02-02 10:59:50 -08001155 order := func(dstField, srcField reflect.StructField) (proptools.Order, error) {
Colin Cross6ee75b62016-05-05 15:57:15 -07001156 if proptools.HasTag(dstField, "android", "variant_prepend") {
1157 return proptools.Prepend, nil
1158 } else {
1159 return proptools.Append, nil
1160 }
1161 }
1162
Colin Crossa6845402020-11-16 15:08:19 -08001163 // Squash the located property struct into the destination property struct.
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001164 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src, nil, order)
Colin Cross06a931b2015-10-28 17:23:31 -07001165 if err != nil {
1166 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
1167 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
1168 } else {
1169 panic(err)
1170 }
1171 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001172}
Colin Cross85a88972015-11-23 13:29:51 -08001173
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001174// Returns the immediate child of the input property struct that corresponds to
1175// the sub-property "field".
Liz Kammerb6dbc872021-05-14 15:14:40 -04001176func getChildPropertyStruct(ctx ArchVariantContext,
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001177 src reflect.Value, field, userFriendlyField string) (reflect.Value, bool) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001178
1179 // Step into non-nil pointers to structs in the src value.
1180 if src.Kind() == reflect.Ptr {
1181 if src.IsNil() {
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001182 return reflect.Value{}, false
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001183 }
1184 src = src.Elem()
1185 }
1186
1187 // Find the requested field in the src struct.
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001188 child := src.FieldByName(proptools.FieldNameForProperty(field))
1189 if !child.IsValid() {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001190 ctx.ModuleErrorf("field %q does not exist", userFriendlyField)
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001191 return reflect.Value{}, false
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001192 }
1193
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001194 if child.IsZero() {
1195 return reflect.Value{}, false
1196 }
1197
1198 return child, true
Colin Cross06a931b2015-10-28 17:23:31 -07001199}
1200
Colin Crossa6845402020-11-16 15:08:19 -08001201// Squash the appropriate OS-specific property structs into the matching top level property structs
1202// based on the CompileOS value that was annotated on the variant.
Colin Crossa195f912019-10-16 11:07:20 -07001203func (m *ModuleBase) setOSProperties(ctx BottomUpMutatorContext) {
1204 os := m.commonProperties.CompileOS
1205
Ustadca02192021-12-20 12:56:46 -05001206 for i := range m.archProperties {
Usta851a3272022-01-05 23:42:33 -05001207 genProps := m.GetProperties()[i]
Colin Crossa195f912019-10-16 11:07:20 -07001208 if m.archProperties[i] == nil {
1209 continue
1210 }
1211 for _, archProperties := range m.archProperties[i] {
1212 archPropValues := reflect.ValueOf(archProperties).Elem()
1213
Colin Crosscbbd13f2020-01-17 14:08:22 -08001214 targetProp := archPropValues.FieldByName("Target").Elem()
Colin Crossa195f912019-10-16 11:07:20 -07001215
1216 // Handle host-specific properties in the form:
1217 // target: {
1218 // host: {
1219 // key: value,
1220 // },
1221 // },
Jiyong Park1613e552020-09-14 19:43:17 +09001222 if os.Class == Host {
Colin Crossa195f912019-10-16 11:07:20 -07001223 field := "Host"
1224 prefix := "target.host"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001225 if hostProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1226 mergePropertyStruct(ctx, genProps, hostProperties)
1227 }
Colin Crossa195f912019-10-16 11:07:20 -07001228 }
1229
1230 // Handle target OS generalities of the form:
1231 // target: {
1232 // bionic: {
1233 // key: value,
1234 // },
1235 // }
1236 if os.Linux() {
1237 field := "Linux"
1238 prefix := "target.linux"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001239 if linuxProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1240 mergePropertyStruct(ctx, genProps, linuxProperties)
1241 }
Colin Crossa195f912019-10-16 11:07:20 -07001242 }
1243
Colin Crossa98d36d2022-03-07 14:39:49 -08001244 if os.Linux() && os.Class == Host {
1245 field := "Host_linux"
1246 prefix := "target.host_linux"
1247 if linuxProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1248 mergePropertyStruct(ctx, genProps, linuxProperties)
1249 }
1250 }
1251
Colin Crossa195f912019-10-16 11:07:20 -07001252 if os.Bionic() {
1253 field := "Bionic"
1254 prefix := "target.bionic"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001255 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1256 mergePropertyStruct(ctx, genProps, bionicProperties)
1257 }
Colin Crossa195f912019-10-16 11:07:20 -07001258 }
1259
Colin Cross528d67e2021-07-23 22:23:07 +00001260 if os == Linux {
1261 field := "Glibc"
1262 prefix := "target.glibc"
1263 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1264 mergePropertyStruct(ctx, genProps, bionicProperties)
1265 }
1266 }
1267
1268 if os == LinuxMusl {
1269 field := "Musl"
1270 prefix := "target.musl"
1271 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1272 mergePropertyStruct(ctx, genProps, bionicProperties)
1273 }
Colin Cross528d67e2021-07-23 22:23:07 +00001274 }
1275
Colin Crossa195f912019-10-16 11:07:20 -07001276 // Handle target OS properties in the form:
1277 // target: {
1278 // linux_glibc: {
1279 // key: value,
1280 // },
1281 // not_windows: {
1282 // key: value,
1283 // },
1284 // android {
1285 // key: value,
1286 // },
1287 // },
1288 field := os.Field
1289 prefix := "target." + os.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001290 if osProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1291 mergePropertyStruct(ctx, genProps, osProperties)
1292 }
Colin Crossa195f912019-10-16 11:07:20 -07001293
Jiyong Park1613e552020-09-14 19:43:17 +09001294 if os.Class == Host && os != Windows {
Colin Crossa195f912019-10-16 11:07:20 -07001295 field := "Not_windows"
1296 prefix := "target.not_windows"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001297 if notWindowsProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1298 mergePropertyStruct(ctx, genProps, notWindowsProperties)
1299 }
Colin Crossa195f912019-10-16 11:07:20 -07001300 }
1301
1302 // Handle 64-bit device properties in the form:
1303 // target {
1304 // android64 {
1305 // key: value,
1306 // },
1307 // android32 {
1308 // key: value,
1309 // },
1310 // },
1311 // WARNING: this is probably not what you want to use in your blueprints file, it selects
1312 // options for all targets on a device that supports 64-bit binaries, not just the targets
1313 // that are being compiled for 64-bit. Its expected use case is binaries like linker and
1314 // debuggerd that need to know when they are a 32-bit process running on a 64-bit device
1315 if os.Class == Device {
1316 if ctx.Config().Android64() {
1317 field := "Android64"
1318 prefix := "target.android64"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001319 if android64Properties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1320 mergePropertyStruct(ctx, genProps, android64Properties)
1321 }
Colin Crossa195f912019-10-16 11:07:20 -07001322 } else {
1323 field := "Android32"
1324 prefix := "target.android32"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001325 if android32Properties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1326 mergePropertyStruct(ctx, genProps, android32Properties)
1327 }
Colin Crossa195f912019-10-16 11:07:20 -07001328 }
1329 }
1330 }
1331 }
1332}
1333
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001334// Returns the struct containing the properties specific to the given
1335// architecture type. These look like this in Blueprint files:
Colin Crossd079e0b2022-08-16 10:27:33 -07001336//
1337// arch: {
1338// arm64: {
1339// key: value,
1340// },
1341// },
1342//
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001343// This struct will also contain sub-structs containing to the architecture/CPU
1344// variants and features that themselves contain properties specific to those.
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001345func getArchTypeStruct(ctx ArchVariantContext, archProperties interface{}, archType ArchType) (reflect.Value, bool) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001346 archPropValues := reflect.ValueOf(archProperties).Elem()
1347 archProp := archPropValues.FieldByName("Arch").Elem()
1348 prefix := "arch." + archType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001349 return getChildPropertyStruct(ctx, archProp, archType.Name, prefix)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001350}
1351
1352// Returns the struct containing the properties specific to a given multilib
1353// value. These look like this in the Blueprint file:
Colin Crossd079e0b2022-08-16 10:27:33 -07001354//
1355// multilib: {
1356// lib32: {
1357// key: value,
1358// },
1359// },
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001360func getMultilibStruct(ctx ArchVariantContext, archProperties interface{}, archType ArchType) (reflect.Value, bool) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001361 archPropValues := reflect.ValueOf(archProperties).Elem()
1362 multilibProp := archPropValues.FieldByName("Multilib").Elem()
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001363 return getChildPropertyStruct(ctx, multilibProp, archType.Multilib, "multilib."+archType.Multilib)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001364}
1365
Liz Kammer9abd62d2021-05-21 08:37:59 -04001366func GetCompoundTargetField(os OsType, arch ArchType) string {
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04001367 return os.Field + "_" + arch.Name
1368}
1369
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001370// Returns the structs corresponding to the properties specific to the given
1371// architecture and OS in archProperties.
Colin Crossb2388e32024-10-07 15:05:23 -07001372func getArchProperties(ctx BaseModuleContext, archProperties interface{}, arch Arch, os OsType, nativeBridgeEnabled bool) []reflect.Value {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001373 result := make([]reflect.Value, 0)
1374 archPropValues := reflect.ValueOf(archProperties).Elem()
1375
1376 targetProp := archPropValues.FieldByName("Target").Elem()
1377
1378 archType := arch.ArchType
1379
1380 if arch.ArchType != Common {
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001381 archStruct, ok := getArchTypeStruct(ctx, archProperties, arch.ArchType)
1382 if ok {
1383 result = append(result, archStruct)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001384
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001385 // Handle arch-variant-specific properties in the form:
1386 // arch: {
1387 // arm: {
1388 // variant: {
1389 // key: value,
1390 // },
1391 // },
1392 // },
1393 v := variantReplacer.Replace(arch.ArchVariant)
1394 if v != "" {
1395 prefix := "arch." + archType.Name + "." + v
1396 if variantProperties, ok := getChildPropertyStruct(ctx, archStruct, v, prefix); ok {
1397 result = append(result, variantProperties)
1398 }
1399 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001400
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001401 // Handle cpu-variant-specific properties in the form:
1402 // arch: {
1403 // arm: {
1404 // variant: {
1405 // key: value,
1406 // },
1407 // },
1408 // },
1409 if arch.CpuVariant != arch.ArchVariant {
1410 c := variantReplacer.Replace(arch.CpuVariant)
1411 if c != "" {
1412 prefix := "arch." + archType.Name + "." + c
1413 if cpuVariantProperties, ok := getChildPropertyStruct(ctx, archStruct, c, prefix); ok {
1414 result = append(result, cpuVariantProperties)
1415 }
1416 }
1417 }
1418
1419 // Handle arch-feature-specific properties in the form:
1420 // arch: {
1421 // arm: {
1422 // feature: {
1423 // key: value,
1424 // },
1425 // },
1426 // },
1427 for _, feature := range arch.ArchFeatures {
1428 prefix := "arch." + archType.Name + "." + feature
1429 if featureProperties, ok := getChildPropertyStruct(ctx, archStruct, feature, prefix); ok {
1430 result = append(result, featureProperties)
1431 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001432 }
1433 }
1434
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001435 if multilibProperties, ok := getMultilibStruct(ctx, archProperties, archType); ok {
1436 result = append(result, multilibProperties)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001437 }
1438
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001439 // Handle combined OS-feature and arch specific properties in the form:
1440 // target: {
1441 // bionic_x86: {
1442 // key: value,
1443 // },
1444 // }
1445 if os.Linux() {
1446 field := "Linux_" + arch.ArchType.Name
1447 userFriendlyField := "target.linux_" + arch.ArchType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001448 if linuxProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1449 result = append(result, linuxProperties)
1450 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001451 }
1452
1453 if os.Bionic() {
1454 field := "Bionic_" + archType.Name
1455 userFriendlyField := "target.bionic_" + archType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001456 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1457 result = append(result, bionicProperties)
1458 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001459 }
1460
1461 // Handle combined OS and arch specific properties in the form:
1462 // target: {
1463 // linux_glibc_x86: {
1464 // key: value,
1465 // },
1466 // linux_glibc_arm: {
1467 // key: value,
1468 // },
1469 // android_arm {
1470 // key: value,
1471 // },
1472 // android_x86 {
1473 // key: value,
1474 // },
1475 // },
Liz Kammer9abd62d2021-05-21 08:37:59 -04001476 field := GetCompoundTargetField(os, archType)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001477 userFriendlyField := "target." + os.Name + "_" + archType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001478 if osArchProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1479 result = append(result, osArchProperties)
1480 }
Colin Cross528d67e2021-07-23 22:23:07 +00001481
Colin Cross1aa45b02022-02-10 10:33:10 -08001482 if os == Linux {
1483 field := "Glibc_" + archType.Name
1484 userFriendlyField := "target.glibc_" + "_" + archType.Name
1485 if osArchProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1486 result = append(result, osArchProperties)
1487 }
1488 }
1489
Colin Cross528d67e2021-07-23 22:23:07 +00001490 if os == LinuxMusl {
Colin Cross1aa45b02022-02-10 10:33:10 -08001491 field := "Musl_" + archType.Name
1492 userFriendlyField := "target.musl_" + "_" + archType.Name
1493 if osArchProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1494 result = append(result, osArchProperties)
1495 }
Colin Cross528d67e2021-07-23 22:23:07 +00001496 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001497 }
1498
1499 // Handle arm on x86 properties in the form:
1500 // target {
1501 // arm_on_x86 {
1502 // key: value,
1503 // },
1504 // arm_on_x86_64 {
1505 // key: value,
1506 // },
1507 // },
1508 if os.Class == Device {
1509 if arch.ArchType == X86 && (hasArmAbi(arch) ||
1510 hasArmAndroidArch(ctx.Config().Targets[Android])) {
1511 field := "Arm_on_x86"
1512 userFriendlyField := "target.arm_on_x86"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001513 if armOnX86Properties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1514 result = append(result, armOnX86Properties)
1515 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001516 }
1517 if arch.ArchType == X86_64 && (hasArmAbi(arch) ||
1518 hasArmAndroidArch(ctx.Config().Targets[Android])) {
1519 field := "Arm_on_x86_64"
1520 userFriendlyField := "target.arm_on_x86_64"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001521 if armOnX8664Properties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1522 result = append(result, armOnX8664Properties)
1523 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001524 }
1525 if os == Android && nativeBridgeEnabled {
1526 userFriendlyField := "Native_bridge"
1527 prefix := "target.native_bridge"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001528 if nativeBridgeProperties, ok := getChildPropertyStruct(ctx, targetProp, userFriendlyField, prefix); ok {
1529 result = append(result, nativeBridgeProperties)
1530 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001531 }
1532 }
1533
1534 return result
1535}
1536
Colin Crossa6845402020-11-16 15:08:19 -08001537// Squash the appropriate arch-specific property structs into the matching top level property
1538// structs based on the CompileTarget value that was annotated on the variant.
Colin Cross4157e882019-06-06 16:57:04 -07001539func (m *ModuleBase) setArchProperties(ctx BottomUpMutatorContext) {
1540 arch := m.Arch()
1541 os := m.Os()
Colin Crossd3ba0392015-05-07 14:11:29 -07001542
Ustadca02192021-12-20 12:56:46 -05001543 for i := range m.archProperties {
Usta851a3272022-01-05 23:42:33 -05001544 genProps := m.GetProperties()[i]
Colin Cross4157e882019-06-06 16:57:04 -07001545 if m.archProperties[i] == nil {
Dan Willemsenb1957a52016-06-23 23:44:54 -07001546 continue
1547 }
Dan Willemsenb1957a52016-06-23 23:44:54 -07001548
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001549 propStructs := make([]reflect.Value, 0)
1550 for _, archProperty := range m.archProperties[i] {
1551 propStructShard := getArchProperties(ctx, archProperty, arch, os, m.Target().NativeBridge == NativeBridgeEnabled)
1552 propStructs = append(propStructs, propStructShard...)
1553 }
Dan Willemsenb1957a52016-06-23 23:44:54 -07001554
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001555 for _, propStruct := range propStructs {
1556 mergePropertyStruct(ctx, genProps, propStruct)
Colin Crossbb2e2b72016-12-08 17:23:53 -08001557 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001558 }
1559}
1560
Colin Cross0c66bc62021-07-20 09:47:41 -07001561// determineBuildOS stores the OS and architecture used for host targets used during the build into
Colin Cross528d67e2021-07-23 22:23:07 +00001562// config based on the runtime OS and architecture determined by Go and the product configuration.
Colin Cross0c66bc62021-07-20 09:47:41 -07001563func determineBuildOS(config *config) {
1564 config.BuildOS = func() OsType {
1565 switch runtime.GOOS {
1566 case "linux":
Colin Cross528d67e2021-07-23 22:23:07 +00001567 if Bool(config.productVariables.HostMusl) {
1568 return LinuxMusl
1569 }
Colin Cross0c66bc62021-07-20 09:47:41 -07001570 return Linux
1571 case "darwin":
1572 return Darwin
1573 default:
1574 panic(fmt.Sprintf("unsupported OS: %s", runtime.GOOS))
1575 }
1576 }()
1577
1578 config.BuildArch = func() ArchType {
1579 switch runtime.GOARCH {
1580 case "amd64":
1581 return X86_64
1582 default:
1583 panic(fmt.Sprintf("unsupported Arch: %s", runtime.GOARCH))
1584 }
1585 }()
1586
1587}
1588
Colin Crossa6845402020-11-16 15:08:19 -08001589// Convert the arch product variables into a list of targets for each OsType.
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001590func decodeTargetProductVariables(config *config) (map[OsType][]Target, error) {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001591 variables := config.productVariables
Dan Willemsen490fd492015-11-24 17:53:15 -08001592
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001593 targets := make(map[OsType][]Target)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001594 var targetErr error
1595
Liz Kammerb7f33662022-02-28 14:16:16 -05001596 type targetConfig struct {
1597 os OsType
1598 archName string
1599 archVariant *string
1600 cpuVariant *string
1601 abi []string
1602 nativeBridgeEnabled NativeBridgeSupport
1603 nativeBridgeHostArchName *string
1604 nativeBridgeRelativePath *string
1605 }
1606
1607 addTarget := func(target targetConfig) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001608 if targetErr != nil {
1609 return
Dan Willemsen490fd492015-11-24 17:53:15 -08001610 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001611
Liz Kammerb7f33662022-02-28 14:16:16 -05001612 arch, err := decodeArch(target.os, target.archName, target.archVariant, target.cpuVariant, target.abi)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001613 if err != nil {
1614 targetErr = err
1615 return
1616 }
Liz Kammerb7f33662022-02-28 14:16:16 -05001617 nativeBridgeRelativePathStr := String(target.nativeBridgeRelativePath)
1618 nativeBridgeHostArchNameStr := String(target.nativeBridgeHostArchName)
dimitry8d6dde82019-07-11 10:23:53 +02001619
1620 // Use guest arch as relative install path by default
Liz Kammerb7f33662022-02-28 14:16:16 -05001621 if target.nativeBridgeEnabled && nativeBridgeRelativePathStr == "" {
dimitry8d6dde82019-07-11 10:23:53 +02001622 nativeBridgeRelativePathStr = arch.ArchType.String()
1623 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001624
Jiyong Park1613e552020-09-14 19:43:17 +09001625 // A target is considered as HostCross if it's a host target which can't run natively on
1626 // the currently configured build machine (either because the OS is different or because of
1627 // the unsupported arch)
1628 hostCross := false
Liz Kammerb7f33662022-02-28 14:16:16 -05001629 if target.os.Class == Host {
Jiyong Park1613e552020-09-14 19:43:17 +09001630 var osSupported bool
Liz Kammerb7f33662022-02-28 14:16:16 -05001631 if target.os == config.BuildOS {
Jiyong Park1613e552020-09-14 19:43:17 +09001632 osSupported = true
Liz Kammerb7f33662022-02-28 14:16:16 -05001633 } else if config.BuildOS.Linux() && target.os.Linux() {
Jiyong Park1613e552020-09-14 19:43:17 +09001634 // LinuxBionic and Linux are compatible
1635 osSupported = true
1636 } else {
1637 osSupported = false
1638 }
1639
1640 var archSupported bool
1641 if arch.ArchType == Common {
1642 archSupported = true
1643 } else if arch.ArchType.Name == *variables.HostArch {
1644 archSupported = true
1645 } else if variables.HostSecondaryArch != nil && arch.ArchType.Name == *variables.HostSecondaryArch {
1646 archSupported = true
1647 } else {
1648 archSupported = false
1649 }
1650 if !osSupported || !archSupported {
1651 hostCross = true
1652 }
1653 }
1654
Liz Kammerb7f33662022-02-28 14:16:16 -05001655 targets[target.os] = append(targets[target.os],
Colin Crossa1ad8d12016-06-01 17:09:44 -07001656 Target{
Liz Kammerb7f33662022-02-28 14:16:16 -05001657 Os: target.os,
dimitry8d6dde82019-07-11 10:23:53 +02001658 Arch: arch,
Liz Kammerb7f33662022-02-28 14:16:16 -05001659 NativeBridge: target.nativeBridgeEnabled,
dimitry8d6dde82019-07-11 10:23:53 +02001660 NativeBridgeHostArchName: nativeBridgeHostArchNameStr,
1661 NativeBridgeRelativePath: nativeBridgeRelativePathStr,
Jiyong Park1613e552020-09-14 19:43:17 +09001662 HostCross: hostCross,
Colin Crossa1ad8d12016-06-01 17:09:44 -07001663 })
Dan Willemsen490fd492015-11-24 17:53:15 -08001664 }
1665
Colin Cross4225f652015-09-17 14:33:42 -07001666 if variables.HostArch == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001667 return nil, fmt.Errorf("No host primary architecture set")
Colin Cross4225f652015-09-17 14:33:42 -07001668 }
1669
Colin Crossa6845402020-11-16 15:08:19 -08001670 // The primary host target, which must always exist.
Liz Kammerb7f33662022-02-28 14:16:16 -05001671 addTarget(targetConfig{os: config.BuildOS, archName: *variables.HostArch, nativeBridgeEnabled: NativeBridgeDisabled})
Colin Cross4225f652015-09-17 14:33:42 -07001672
Colin Crossa6845402020-11-16 15:08:19 -08001673 // An optional secondary host target.
Colin Crosseeabb892015-11-20 13:07:51 -08001674 if variables.HostSecondaryArch != nil && *variables.HostSecondaryArch != "" {
Liz Kammerb7f33662022-02-28 14:16:16 -05001675 addTarget(targetConfig{os: config.BuildOS, archName: *variables.HostSecondaryArch, nativeBridgeEnabled: NativeBridgeDisabled})
Dan Willemsen490fd492015-11-24 17:53:15 -08001676 }
1677
Colin Crossa6845402020-11-16 15:08:19 -08001678 // Optional cross-compiled host targets, generally Windows.
Colin Crossff3ae9d2018-04-10 16:15:18 -07001679 if String(variables.CrossHost) != "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001680 crossHostOs := osByName(*variables.CrossHost)
1681 if crossHostOs == NoOsType {
1682 return nil, fmt.Errorf("Unknown cross host OS %q", *variables.CrossHost)
1683 }
1684
Colin Crossff3ae9d2018-04-10 16:15:18 -07001685 if String(variables.CrossHostArch) == "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001686 return nil, fmt.Errorf("No cross-host primary architecture set")
Dan Willemsen490fd492015-11-24 17:53:15 -08001687 }
1688
Colin Crossa6845402020-11-16 15:08:19 -08001689 // The primary cross-compiled host target.
Liz Kammerb7f33662022-02-28 14:16:16 -05001690 addTarget(targetConfig{os: crossHostOs, archName: *variables.CrossHostArch, nativeBridgeEnabled: NativeBridgeDisabled})
Dan Willemsen490fd492015-11-24 17:53:15 -08001691
Colin Crossa6845402020-11-16 15:08:19 -08001692 // An optional secondary cross-compiled host target.
Dan Willemsen490fd492015-11-24 17:53:15 -08001693 if variables.CrossHostSecondaryArch != nil && *variables.CrossHostSecondaryArch != "" {
Liz Kammerb7f33662022-02-28 14:16:16 -05001694 addTarget(targetConfig{os: crossHostOs, archName: *variables.CrossHostSecondaryArch, nativeBridgeEnabled: NativeBridgeDisabled})
Dan Willemsen490fd492015-11-24 17:53:15 -08001695 }
1696 }
1697
Colin Crossa6845402020-11-16 15:08:19 -08001698 // Optional device targets
Dan Willemsen3f32f032016-07-11 14:36:48 -07001699 if variables.DeviceArch != nil && *variables.DeviceArch != "" {
Colin Crossa6845402020-11-16 15:08:19 -08001700 // The primary device target.
Liz Kammerb7f33662022-02-28 14:16:16 -05001701 addTarget(targetConfig{
1702 os: Android,
1703 archName: *variables.DeviceArch,
1704 archVariant: variables.DeviceArchVariant,
1705 cpuVariant: variables.DeviceCpuVariant,
1706 abi: variables.DeviceAbi,
1707 nativeBridgeEnabled: NativeBridgeDisabled,
1708 })
Colin Cross4225f652015-09-17 14:33:42 -07001709
Colin Crossa6845402020-11-16 15:08:19 -08001710 // An optional secondary device target.
Dan Willemsen3f32f032016-07-11 14:36:48 -07001711 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" {
Liz Kammerb7f33662022-02-28 14:16:16 -05001712 addTarget(targetConfig{
1713 os: Android,
1714 archName: *variables.DeviceSecondaryArch,
1715 archVariant: variables.DeviceSecondaryArchVariant,
1716 cpuVariant: variables.DeviceSecondaryCpuVariant,
1717 abi: variables.DeviceSecondaryAbi,
1718 nativeBridgeEnabled: NativeBridgeDisabled,
1719 })
Colin Cross4225f652015-09-17 14:33:42 -07001720 }
dimitry1f33e402019-03-26 12:39:31 +01001721
Colin Crossa6845402020-11-16 15:08:19 -08001722 // An optional NativeBridge device target.
dimitry1f33e402019-03-26 12:39:31 +01001723 if variables.NativeBridgeArch != nil && *variables.NativeBridgeArch != "" {
Liz Kammerb7f33662022-02-28 14:16:16 -05001724 addTarget(targetConfig{
1725 os: Android,
1726 archName: *variables.NativeBridgeArch,
1727 archVariant: variables.NativeBridgeArchVariant,
1728 cpuVariant: variables.NativeBridgeCpuVariant,
1729 abi: variables.NativeBridgeAbi,
1730 nativeBridgeEnabled: NativeBridgeEnabled,
1731 nativeBridgeHostArchName: variables.DeviceArch,
1732 nativeBridgeRelativePath: variables.NativeBridgeRelativePath,
1733 })
dimitry1f33e402019-03-26 12:39:31 +01001734 }
1735
Colin Crossa6845402020-11-16 15:08:19 -08001736 // An optional secondary NativeBridge device target.
dimitry1f33e402019-03-26 12:39:31 +01001737 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" &&
1738 variables.NativeBridgeSecondaryArch != nil && *variables.NativeBridgeSecondaryArch != "" {
Liz Kammerb7f33662022-02-28 14:16:16 -05001739 addTarget(targetConfig{
1740 os: Android,
1741 archName: *variables.NativeBridgeSecondaryArch,
1742 archVariant: variables.NativeBridgeSecondaryArchVariant,
1743 cpuVariant: variables.NativeBridgeSecondaryCpuVariant,
1744 abi: variables.NativeBridgeSecondaryAbi,
1745 nativeBridgeEnabled: NativeBridgeEnabled,
1746 nativeBridgeHostArchName: variables.DeviceSecondaryArch,
1747 nativeBridgeRelativePath: variables.NativeBridgeSecondaryRelativePath,
1748 })
dimitry1f33e402019-03-26 12:39:31 +01001749 }
Colin Cross4225f652015-09-17 14:33:42 -07001750 }
1751
Colin Crossa1ad8d12016-06-01 17:09:44 -07001752 if targetErr != nil {
1753 return nil, targetErr
1754 }
1755
1756 return targets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001757}
1758
Colin Crossbb2e2b72016-12-08 17:23:53 -08001759// hasArmAbi returns true if arch has at least one arm ABI
1760func hasArmAbi(arch Arch) bool {
Jaewoong Jung3aff5782020-02-11 07:54:35 -08001761 return PrefixInList(arch.Abi, "arm")
Colin Crossbb2e2b72016-12-08 17:23:53 -08001762}
1763
Lev Rumyantsev34581212021-10-13 09:47:59 -07001764// hasArmAndroidArch returns true if targets has at least
1765// one arm Android arch (possibly native bridged)
Colin Cross4247f0d2017-04-13 16:56:14 -07001766func hasArmAndroidArch(targets []Target) bool {
1767 for _, target := range targets {
Lev Rumyantsev34581212021-10-13 09:47:59 -07001768 if target.Os == Android &&
1769 (target.Arch.ArchType == Arm || target.Arch.ArchType == Arm64) {
Victor Khimenko5eb8ec12018-03-21 20:30:54 +01001770 return true
1771 }
1772 }
1773 return false
1774}
1775
Colin Crossa6845402020-11-16 15:08:19 -08001776// archConfig describes a built-in configuration.
Dan Albert4098deb2016-10-19 14:04:41 -07001777type archConfig struct {
Liz Kammer992918d2022-11-11 10:37:54 -05001778 Arch string `json:"arch"`
1779 ArchVariant string `json:"arch_variant"`
1780 CpuVariant string `json:"cpu_variant"`
1781 Abi []string `json:"abis"`
Dan Albert4098deb2016-10-19 14:04:41 -07001782}
1783
Elliott Hughesc55b5862022-10-27 23:46:22 +00001784// getNdkAbisConfig returns the list of archConfigs that are used for building
1785// the API stubs and static libraries that are included in the NDK.
Dan Albert4098deb2016-10-19 14:04:41 -07001786func getNdkAbisConfig() []archConfig {
1787 return []archConfig{
Tamas Petzbca786d2021-01-20 18:56:33 +01001788 {"arm64", "armv8-a-branchprot", "", []string{"arm64-v8a"}},
Elliott Hughesc55b5862022-10-27 23:46:22 +00001789 {"arm", "armv7-a-neon", "", []string{"armeabi-v7a"}},
Elliott Hughesf7d31092023-03-14 23:11:57 +00001790 {"riscv64", "", "", []string{"riscv64"}},
Dan Albert4098deb2016-10-19 14:04:41 -07001791 {"x86_64", "", "", []string{"x86_64"}},
Inseob Kim5219c0e2021-06-17 00:33:00 +09001792 {"x86", "", "", []string{"x86"}},
Dan Albert4098deb2016-10-19 14:04:41 -07001793 }
1794}
1795
Colin Crossa6845402020-11-16 15:08:19 -08001796// getAmlAbisConfig returns a list of archConfigs for the ABIs supported by mainline modules.
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001797func getAmlAbisConfig() []archConfig {
1798 return []archConfig{
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001799 {"arm64", "armv8-a", "", []string{"arm64-v8a"}},
Inseob Kim5219c0e2021-06-17 00:33:00 +09001800 {"arm", "armv7-a-neon", "", []string{"armeabi-v7a"}},
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001801 {"x86_64", "", "", []string{"x86_64"}},
Inseob Kim5219c0e2021-06-17 00:33:00 +09001802 {"x86", "", "", []string{"x86"}},
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001803 }
1804}
1805
Colin Crossa6845402020-11-16 15:08:19 -08001806// decodeArchSettings converts a list of archConfigs into a list of Targets for the given OsType.
Liz Kammerb7f33662022-02-28 14:16:16 -05001807func decodeAndroidArchSettings(archConfigs []archConfig) ([]Target, error) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001808 var ret []Target
Dan Willemsen322acaf2016-01-12 23:07:05 -08001809
Dan Albert4098deb2016-10-19 14:04:41 -07001810 for _, config := range archConfigs {
Liz Kammer992918d2022-11-11 10:37:54 -05001811 arch, err := decodeArch(Android, config.Arch, &config.ArchVariant,
1812 &config.CpuVariant, config.Abi)
Dan Willemsen322acaf2016-01-12 23:07:05 -08001813 if err != nil {
1814 return nil, err
1815 }
Colin Cross3b19f5d2019-09-17 14:45:31 -07001816
Colin Crossa1ad8d12016-06-01 17:09:44 -07001817 ret = append(ret, Target{
1818 Os: Android,
1819 Arch: arch,
1820 })
Dan Willemsen322acaf2016-01-12 23:07:05 -08001821 }
1822
1823 return ret, nil
1824}
1825
Colin Crossa6845402020-11-16 15:08:19 -08001826// decodeArch converts a set of strings from product variables into an Arch struct.
Colin Crossa74ca042019-01-31 14:31:51 -08001827func decodeArch(os OsType, arch string, archVariant, cpuVariant *string, abi []string) (Arch, error) {
Colin Crossa6845402020-11-16 15:08:19 -08001828 // Verify the arch is valid
Colin Crosseeabb892015-11-20 13:07:51 -08001829 archType, ok := archTypeMap[arch]
1830 if !ok {
1831 return Arch{}, fmt.Errorf("unknown arch %q", arch)
1832 }
Colin Cross4225f652015-09-17 14:33:42 -07001833
Colin Crosseeabb892015-11-20 13:07:51 -08001834 a := Arch{
Colin Cross4225f652015-09-17 14:33:42 -07001835 ArchType: archType,
Colin Crossa6845402020-11-16 15:08:19 -08001836 ArchVariant: String(archVariant),
1837 CpuVariant: String(cpuVariant),
Colin Crossa74ca042019-01-31 14:31:51 -08001838 Abi: abi,
Colin Crosseeabb892015-11-20 13:07:51 -08001839 }
1840
Colin Crossa6845402020-11-16 15:08:19 -08001841 // Convert generic arch variants into the empty string.
Colin Crosseeabb892015-11-20 13:07:51 -08001842 if a.ArchVariant == a.ArchType.Name || a.ArchVariant == "generic" {
1843 a.ArchVariant = ""
1844 }
1845
Colin Crossa6845402020-11-16 15:08:19 -08001846 // Convert generic CPU variants into the empty string.
Colin Crosseeabb892015-11-20 13:07:51 -08001847 if a.CpuVariant == a.ArchType.Name || a.CpuVariant == "generic" {
1848 a.CpuVariant = ""
1849 }
1850
Liz Kammer2c2afe22022-02-11 11:35:03 -05001851 if a.ArchVariant != "" {
1852 if validArchVariants := archVariants[archType]; !InList(a.ArchVariant, validArchVariants) {
1853 return Arch{}, fmt.Errorf("[%q] unknown arch variant %q, support variants: %q", archType, a.ArchVariant, validArchVariants)
1854 }
1855 }
1856
1857 if a.CpuVariant != "" {
1858 if validCpuVariants := cpuVariants[archType]; !InList(a.CpuVariant, validCpuVariants) {
1859 return Arch{}, fmt.Errorf("[%q] unknown cpu variant %q, support variants: %q", archType, a.CpuVariant, validCpuVariants)
1860 }
1861 }
1862
Colin Crossa6845402020-11-16 15:08:19 -08001863 // Filter empty ABIs out of the list.
Colin Crosseeabb892015-11-20 13:07:51 -08001864 for i := 0; i < len(a.Abi); i++ {
1865 if a.Abi[i] == "" {
1866 a.Abi = append(a.Abi[:i], a.Abi[i+1:]...)
1867 i--
1868 }
1869 }
1870
Liz Kammere8303bd2022-02-16 09:02:48 -05001871 // Set ArchFeatures from the arch type. for Android OS, other os-es do not specify features
1872 if os == Android {
1873 if featureMap, ok := androidArchFeatureMap[archType]; ok {
Dan Willemsen01a3c252019-01-11 19:02:16 -08001874 a.ArchFeatures = featureMap[a.ArchVariant]
1875 }
Colin Crossc5c24ad2015-11-20 15:35:00 -08001876 }
1877
Colin Crosseeabb892015-11-20 13:07:51 -08001878 return a, nil
Colin Cross4225f652015-09-17 14:33:42 -07001879}
1880
Colin Crossa6845402020-11-16 15:08:19 -08001881// filterMultilibTargets takes a list of Targets and a multilib value and returns a new list of
1882// Targets containing only those that have the given multilib value.
Colin Cross69617d32016-09-06 10:39:07 -07001883func filterMultilibTargets(targets []Target, multilib string) []Target {
1884 var ret []Target
1885 for _, t := range targets {
1886 if t.Arch.ArchType.Multilib == multilib {
1887 ret = append(ret, t)
1888 }
1889 }
1890 return ret
1891}
1892
Colin Crossa6845402020-11-16 15:08:19 -08001893// getCommonTargets returns the set of Os specific common architecture targets for each Os in a list
1894// of targets.
Nan Zhangdb0b9a32017-02-27 10:12:13 -08001895func getCommonTargets(targets []Target) []Target {
1896 var ret []Target
1897 set := make(map[string]bool)
1898
1899 for _, t := range targets {
1900 if _, found := set[t.Os.String()]; !found {
1901 set[t.Os.String()] = true
Colin Cross39a18142022-06-24 18:43:40 -07001902 common := commonTargetMap[t.Os.String()]
1903 common.HostCross = t.HostCross
1904 ret = append(ret, common)
Nan Zhangdb0b9a32017-02-27 10:12:13 -08001905 }
1906 }
1907
1908 return ret
1909}
1910
Sam Delmericocc271e22022-06-01 15:45:02 +00001911// FirstTarget takes a list of Targets and a list of multilib values and returns a list of Targets
Colin Crossc0f0eb82022-07-19 14:41:11 -07001912// that contains zero or one Target for each OsType and HostCross, selecting the one that matches
1913// the earliest filter.
Sam Delmericocc271e22022-06-01 15:45:02 +00001914func FirstTarget(targets []Target, filters ...string) []Target {
Jiyong Park22101982020-09-17 19:09:58 +09001915 // find the first target from each OS
1916 var ret []Target
Colin Crossc0f0eb82022-07-19 14:41:11 -07001917 type osHostCross struct {
1918 os OsType
1919 hostCross bool
1920 }
1921 set := make(map[osHostCross]bool)
Jiyong Park22101982020-09-17 19:09:58 +09001922
Colin Cross6b4a32d2017-12-05 13:42:45 -08001923 for _, filter := range filters {
1924 buildTargets := filterMultilibTargets(targets, filter)
Jiyong Park22101982020-09-17 19:09:58 +09001925 for _, t := range buildTargets {
Colin Crossc0f0eb82022-07-19 14:41:11 -07001926 key := osHostCross{t.Os, t.HostCross}
1927 if _, found := set[key]; !found {
1928 set[key] = true
Jiyong Park22101982020-09-17 19:09:58 +09001929 ret = append(ret, t)
1930 }
Colin Cross6b4a32d2017-12-05 13:42:45 -08001931 }
1932 }
Jiyong Park22101982020-09-17 19:09:58 +09001933 return ret
Colin Cross6b4a32d2017-12-05 13:42:45 -08001934}
1935
Colin Crossa6845402020-11-16 15:08:19 -08001936// decodeMultilibTargets uses the module's multilib setting to select one or more targets from a
1937// list of Targets.
Colin Crossee0bc3b2018-10-02 22:01:37 -07001938func decodeMultilibTargets(multilib string, targets []Target, prefer32 bool) ([]Target, error) {
Colin Crossa6845402020-11-16 15:08:19 -08001939 var buildTargets []Target
Colin Cross6b4a32d2017-12-05 13:42:45 -08001940
Colin Cross4225f652015-09-17 14:33:42 -07001941 switch multilib {
1942 case "common":
Colin Cross6b4a32d2017-12-05 13:42:45 -08001943 buildTargets = getCommonTargets(targets)
1944 case "common_first":
1945 buildTargets = getCommonTargets(targets)
1946 if prefer32 {
Sam Delmericocc271e22022-06-01 15:45:02 +00001947 buildTargets = append(buildTargets, FirstTarget(targets, "lib32", "lib64")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001948 } else {
Sam Delmericocc271e22022-06-01 15:45:02 +00001949 buildTargets = append(buildTargets, FirstTarget(targets, "lib64", "lib32")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001950 }
Colin Cross4225f652015-09-17 14:33:42 -07001951 case "both":
Colin Cross8b74d172016-09-13 09:59:14 -07001952 if prefer32 {
1953 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1954 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1955 } else {
1956 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1957 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1958 }
Colin Cross4225f652015-09-17 14:33:42 -07001959 case "32":
Colin Cross69617d32016-09-06 10:39:07 -07001960 buildTargets = filterMultilibTargets(targets, "lib32")
Colin Cross4225f652015-09-17 14:33:42 -07001961 case "64":
Colin Cross69617d32016-09-06 10:39:07 -07001962 buildTargets = filterMultilibTargets(targets, "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001963 case "first":
1964 if prefer32 {
Sam Delmericocc271e22022-06-01 15:45:02 +00001965 buildTargets = FirstTarget(targets, "lib32", "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001966 } else {
Sam Delmericocc271e22022-06-01 15:45:02 +00001967 buildTargets = FirstTarget(targets, "lib64", "lib32")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001968 }
Victor Chang9448e8f2020-09-14 15:34:16 +01001969 case "first_prefer32":
Sam Delmericocc271e22022-06-01 15:45:02 +00001970 buildTargets = FirstTarget(targets, "lib32", "lib64")
Colin Cross69617d32016-09-06 10:39:07 -07001971 case "prefer32":
Colin Cross3dceee32018-09-06 10:19:57 -07001972 buildTargets = filterMultilibTargets(targets, "lib32")
1973 if len(buildTargets) == 0 {
1974 buildTargets = filterMultilibTargets(targets, "lib64")
1975 }
Dan Willemsen47450072021-10-19 20:24:49 -07001976 case "darwin_universal":
1977 buildTargets = filterMultilibTargets(targets, "lib64")
1978 // Reverse the targets so that the first architecture can depend on the second
1979 // architecture module in order to merge the outputs.
Colin Crossb5e3f7d2023-07-06 15:37:53 -07001980 ReverseSliceInPlace(buildTargets)
Dan Willemsen47450072021-10-19 20:24:49 -07001981 case "darwin_universal_common_first":
1982 archTargets := filterMultilibTargets(targets, "lib64")
Colin Crossb5e3f7d2023-07-06 15:37:53 -07001983 ReverseSliceInPlace(archTargets)
Dan Willemsen47450072021-10-19 20:24:49 -07001984 buildTargets = append(getCommonTargets(targets), archTargets...)
Colin Cross4225f652015-09-17 14:33:42 -07001985 default:
Victor Chang9448e8f2020-09-14 15:34:16 +01001986 return nil, fmt.Errorf(`compile_multilib must be "both", "first", "32", "64", "prefer32" or "first_prefer32" found %q`,
Colin Cross4225f652015-09-17 14:33:42 -07001987 multilib)
Colin Cross4225f652015-09-17 14:33:42 -07001988 }
1989
Colin Crossa1ad8d12016-06-01 17:09:44 -07001990 return buildTargets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001991}
Jingwen Chen5d864492021-02-24 07:20:12 -05001992
Liz Kammerb6dbc872021-05-14 15:14:40 -04001993// ArchVariantContext defines the limited context necessary to retrieve arch_variant properties.
1994type ArchVariantContext interface {
1995 ModuleErrorf(fmt string, args ...interface{})
1996 PropertyErrorf(property, fmt string, args ...interface{})
1997}