blob: 942727ace2135075479f5fbb608bcf6c886cfb55 [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
141const COMMON_VARIANT = "common"
142
143var (
144 archTypeList []ArchType
145
Colin Crossf05b0d32022-07-14 18:10:34 -0700146 Arm = newArch("arm", "lib32")
147 Arm64 = newArch("arm64", "lib64")
148 Riscv64 = newArch("riscv64", "lib64")
149 X86 = newArch("x86", "lib32")
150 X86_64 = newArch("x86_64", "lib64")
Colin Crossa6845402020-11-16 15:08:19 -0800151
152 Common = ArchType{
153 Name: COMMON_VARIANT,
154 }
155)
156
157var archTypeMap = map[string]ArchType{}
158
Colin Crossec193632015-07-06 17:49:43 -0700159func newArch(name, multilib string) ArchType {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700160 archType := ArchType{
Colin Crossec193632015-07-06 17:49:43 -0700161 Name: name,
Dan Willemsenb1957a52016-06-23 23:44:54 -0700162 Field: proptools.FieldNameForProperty(name),
Colin Crossec193632015-07-06 17:49:43 -0700163 Multilib: multilib,
Colin Cross3f40fa42015-01-30 17:27:36 -0800164 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700165 archTypeList = append(archTypeList, archType)
Colin Crossa6845402020-11-16 15:08:19 -0800166 archTypeMap[name] = archType
Dan Willemsenb1957a52016-06-23 23:44:54 -0700167 return archType
Colin Cross3f40fa42015-01-30 17:27:36 -0800168}
169
Ustaeabf0f32021-12-06 15:17:23 -0500170// ArchTypeList returns a slice copy of the 4 supported ArchTypes for arm,
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000171// arm64, x86 and x86_64.
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -0700172func ArchTypeList() []ArchType {
173 return append([]ArchType(nil), archTypeList...)
174}
175
Colin Crossa6845402020-11-16 15:08:19 -0800176// MarshalText allows an ArchType to be serialized through any encoder that supports
177// encoding.TextMarshaler.
Colin Cross74ba9622019-02-11 15:11:14 -0800178func (a ArchType) MarshalText() ([]byte, error) {
Jeongik Chabec4d032021-04-15 08:55:38 +0900179 return []byte(a.String()), nil
Colin Cross74ba9622019-02-11 15:11:14 -0800180}
181
Colin Crossa6845402020-11-16 15:08:19 -0800182var _ encoding.TextMarshaler = ArchType{}
Colin Cross74ba9622019-02-11 15:11:14 -0800183
Colin Crossa6845402020-11-16 15:08:19 -0800184// UnmarshalText allows an ArchType to be deserialized through any decoder that supports
185// encoding.TextUnmarshaler.
Colin Cross74ba9622019-02-11 15:11:14 -0800186func (a *ArchType) UnmarshalText(text []byte) error {
187 if u, ok := archTypeMap[string(text)]; ok {
188 *a = u
189 return nil
190 }
191
192 return fmt.Errorf("unknown ArchType %q", text)
193}
194
Colin Crossa6845402020-11-16 15:08:19 -0800195var _ encoding.TextUnmarshaler = &ArchType{}
Colin Crossa1ad8d12016-06-01 17:09:44 -0700196
Colin Crossa6845402020-11-16 15:08:19 -0800197// OsClass is an enum that describes whether a variant of a module runs on the host, on the device,
198// or is generic.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700199type OsClass int
200
201const (
Colin Crossa6845402020-11-16 15:08:19 -0800202 // Generic is used for variants of modules that are not OS-specific.
Dan Willemsen0e2d97b2016-11-28 17:50:06 -0800203 Generic OsClass = iota
Colin Crossa6845402020-11-16 15:08:19 -0800204 // Device is used for variants of modules that run on the device.
Dan Willemsen0e2d97b2016-11-28 17:50:06 -0800205 Device
Colin Crossa6845402020-11-16 15:08:19 -0800206 // Host is used for variants of modules that run on the host.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700207 Host
Colin Crossa1ad8d12016-06-01 17:09:44 -0700208)
209
Colin Crossa6845402020-11-16 15:08:19 -0800210// String returns the OsClass as a string.
Colin Cross67a5c132017-05-09 13:45:28 -0700211func (class OsClass) String() string {
212 switch class {
213 case Generic:
214 return "generic"
215 case Device:
216 return "device"
217 case Host:
218 return "host"
Colin Cross67a5c132017-05-09 13:45:28 -0700219 default:
220 panic(fmt.Errorf("unknown class %d", class))
221 }
222}
223
Colin Crossa6845402020-11-16 15:08:19 -0800224// OsType describes an OS variant of a module.
225type OsType struct {
226 // Name is the name of the OS. It is also used as the name of the property in Android.bp
227 // files.
228 Name string
229
230 // Field is the name of the OS converted to an exported field name, i.e. with the first
231 // character capitalized.
232 Field string
233
234 // Class is the OsClass of the OS.
235 Class OsClass
236
237 // DefaultDisabled is set when the module variants for the OS should not be created unless
238 // the module explicitly requests them. This is used to limit Windows cross compilation to
239 // only modules that need it.
240 DefaultDisabled bool
241}
242
243// String returns the name of the OsType.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700244func (os OsType) String() string {
245 return os.Name
Colin Cross54c71122016-06-01 17:09:44 -0700246}
247
Colin Crossa6845402020-11-16 15:08:19 -0800248// Bionic returns true if the OS uses the Bionic libc runtime, i.e. if the OS is Android or
249// is Linux with Bionic.
Dan Willemsen866b5632017-09-22 12:28:24 -0700250func (os OsType) Bionic() bool {
251 return os == Android || os == LinuxBionic
252}
253
Colin Crossa6845402020-11-16 15:08:19 -0800254// Linux returns true if the OS uses the Linux kernel, i.e. if the OS is Android or is Linux
255// with or without the Bionic libc runtime.
Dan Willemsen866b5632017-09-22 12:28:24 -0700256func (os OsType) Linux() bool {
Colin Cross528d67e2021-07-23 22:23:07 +0000257 return os == Android || os == Linux || os == LinuxBionic || os == LinuxMusl
Dan Willemsen866b5632017-09-22 12:28:24 -0700258}
259
Colin Crossa6845402020-11-16 15:08:19 -0800260// newOsType constructs an OsType and adds it to the global lists.
261func newOsType(name string, class OsClass, defDisabled bool, archTypes ...ArchType) OsType {
262 checkCalledFromInit()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700263 os := OsType{
264 Name: name,
Colin Crossa6845402020-11-16 15:08:19 -0800265 Field: proptools.FieldNameForProperty(name),
Colin Crossa1ad8d12016-06-01 17:09:44 -0700266 Class: class,
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800267
268 DefaultDisabled: defDisabled,
Colin Cross54c71122016-06-01 17:09:44 -0700269 }
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000270 osTypeList = append(osTypeList, os)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800271
272 if _, found := commonTargetMap[name]; found {
273 panic(fmt.Errorf("Found Os type duplicate during OsType registration: %q", name))
274 } else {
Colin Crosse9fe2942020-11-10 18:12:15 -0800275 commonTargetMap[name] = Target{Os: os, Arch: CommonArch}
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800276 }
Colin Crossa6845402020-11-16 15:08:19 -0800277 osArchTypeMap[os] = archTypes
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800278
Colin Crossa1ad8d12016-06-01 17:09:44 -0700279 return os
280}
281
Colin Crossa6845402020-11-16 15:08:19 -0800282// osByName returns the OsType that has the given name, or NoOsType if none match.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700283func osByName(name string) OsType {
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000284 for _, os := range osTypeList {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700285 if os.Name == name {
286 return os
287 }
288 }
289
290 return NoOsType
Dan Willemsen490fd492015-11-24 17:53:15 -0800291}
292
Colin Crossa6845402020-11-16 15:08:19 -0800293var (
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000294 // osTypeList contains a list of all the supported OsTypes, including ones not supported
Colin Crossa6845402020-11-16 15:08:19 -0800295 // by the current build host or the target device.
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000296 osTypeList []OsType
Colin Crossa6845402020-11-16 15:08:19 -0800297 // commonTargetMap maps names of OsTypes to the corresponding common Target, i.e. the
298 // Target with the same OsType and the common ArchType.
299 commonTargetMap = make(map[string]Target)
300 // osArchTypeMap maps OsTypes to the list of supported ArchTypes for that OS.
301 osArchTypeMap = map[OsType][]ArchType{}
302
303 // NoOsType is a placeholder for when no OS is needed.
304 NoOsType OsType
305 // Linux is the OS for the Linux kernel plus the glibc runtime.
306 Linux = newOsType("linux_glibc", Host, false, X86, X86_64)
Colin Cross528d67e2021-07-23 22:23:07 +0000307 // LinuxMusl is the OS for the Linux kernel plus the musl runtime.
Colin Crossa9b2aac2022-06-15 17:25:51 -0700308 LinuxMusl = newOsType("linux_musl", Host, false, X86, X86_64, Arm64, Arm)
Colin Crossa6845402020-11-16 15:08:19 -0800309 // Darwin is the OS for MacOS/Darwin host machines.
Dan Willemsen8528f4e2021-10-19 00:22:06 -0700310 Darwin = newOsType("darwin", Host, false, Arm64, X86_64)
Colin Crossa6845402020-11-16 15:08:19 -0800311 // LinuxBionic is the OS for the Linux kernel plus the Bionic libc runtime, but without the
312 // rest of Android.
313 LinuxBionic = newOsType("linux_bionic", Host, false, Arm64, X86_64)
314 // Windows the OS for Windows host machines.
315 Windows = newOsType("windows", Host, true, X86, X86_64)
316 // Android is the OS for target devices that run all of Android, including the Linux kernel
317 // and the Bionic libc runtime.
Colin Crossf05b0d32022-07-14 18:10:34 -0700318 Android = newOsType("android", Device, false, Arm, Arm64, Riscv64, X86, X86_64)
Colin Crossa6845402020-11-16 15:08:19 -0800319
320 // CommonOS is a pseudo OSType for a common OS variant, which is OsType agnostic and which
321 // has dependencies on all the OS variants.
322 CommonOS = newOsType("common_os", Generic, false)
Colin Crosse9fe2942020-11-10 18:12:15 -0800323
324 // CommonArch is the Arch for all modules that are os-specific but not arch specific,
325 // for example most Java modules.
326 CommonArch = Arch{ArchType: Common}
dimitry1f33e402019-03-26 12:39:31 +0100327)
328
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000329// OsTypeList returns a slice copy of the supported OsTypes.
330func OsTypeList() []OsType {
331 return append([]OsType(nil), osTypeList...)
332}
333
Colin Crossa6845402020-11-16 15:08:19 -0800334// Target specifies the OS and architecture that a module is being compiled for.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700335type Target struct {
Colin Crossa6845402020-11-16 15:08:19 -0800336 // Os the OS that the module is being compiled for (e.g. "linux_glibc", "android").
337 Os OsType
338 // Arch is the architecture that the module is being compiled for.
339 Arch Arch
340 // NativeBridge is NativeBridgeEnabled if the architecture is supported using NativeBridge
341 // (i.e. arm on x86) for this device.
342 NativeBridge NativeBridgeSupport
343 // NativeBridgeHostArchName is the name of the real architecture that is used to implement
344 // the NativeBridge architecture. For example, for arm on x86 this would be "x86".
dimitry8d6dde82019-07-11 10:23:53 +0200345 NativeBridgeHostArchName string
Colin Crossa6845402020-11-16 15:08:19 -0800346 // NativeBridgeRelativePath is the name of the subdirectory that will contain NativeBridge
347 // libraries and binaries.
dimitry8d6dde82019-07-11 10:23:53 +0200348 NativeBridgeRelativePath string
Jiyong Park1613e552020-09-14 19:43:17 +0900349
350 // HostCross is true when the target cannot run natively on the current build host.
351 // For example, linux_glibc_x86 returns true on a regular x86/i686/Linux machines, but returns false
352 // on Mac (different OS), or on 64-bit only i686/Linux machines (unsupported arch).
353 HostCross bool
Colin Crossd3ba0392015-05-07 14:11:29 -0700354}
355
Colin Crossa6845402020-11-16 15:08:19 -0800356// NativeBridgeSupport is an enum that specifies if a Target supports NativeBridge.
357type NativeBridgeSupport bool
358
359const (
360 NativeBridgeDisabled NativeBridgeSupport = false
361 NativeBridgeEnabled NativeBridgeSupport = true
362)
363
364// String returns the OS and arch variations used for the Target.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700365func (target Target) String() string {
Colin Crossa195f912019-10-16 11:07:20 -0700366 return target.OsVariation() + "_" + target.ArchVariation()
367}
368
Colin Crossa6845402020-11-16 15:08:19 -0800369// OsVariation returns the name of the variation used by the osMutator for the Target.
Colin Crossa195f912019-10-16 11:07:20 -0700370func (target Target) OsVariation() string {
371 return target.Os.String()
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700372}
373
Colin Crossa6845402020-11-16 15:08:19 -0800374// ArchVariation returns the name of the variation used by the archMutator for the Target.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700375func (target Target) ArchVariation() string {
376 var variation string
dimitry1f33e402019-03-26 12:39:31 +0100377 if target.NativeBridge {
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700378 variation = "native_bridge_"
dimitry1f33e402019-03-26 12:39:31 +0100379 }
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700380 variation += target.Arch.String()
381
Colin Crossa195f912019-10-16 11:07:20 -0700382 return variation
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700383}
384
Colin Crossa6845402020-11-16 15:08:19 -0800385// Variations returns a list of blueprint.Variations for the osMutator and archMutator for the
386// Target.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700387func (target Target) Variations() []blueprint.Variation {
388 return []blueprint.Variation{
Colin Crossa195f912019-10-16 11:07:20 -0700389 {Mutator: "os", Variation: target.OsVariation()},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700390 {Mutator: "arch", Variation: target.ArchVariation()},
391 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800392}
393
Colin Crossa6845402020-11-16 15:08:19 -0800394// osMutator splits an arch-specific module into a variant for each OS that is enabled for the
395// module. It uses the HostOrDevice value passed to InitAndroidArchModule and the
396// device_supported and host_supported properties to determine which OsTypes are enabled for this
397// module, then searches through the Targets to determine which have enabled Targets for this
398// module.
Colin Cross8bbc3d52024-09-11 15:33:54 -0700399type osTransitionMutator struct{}
Colin Crossa195f912019-10-16 11:07:20 -0700400
Colin Cross8bbc3d52024-09-11 15:33:54 -0700401type allOsInfo struct {
402 Os map[string]OsType
403 Variations []string
404}
Colin Crossa195f912019-10-16 11:07:20 -0700405
Colin Cross8bbc3d52024-09-11 15:33:54 -0700406var allOsProvider = blueprint.NewMutatorProvider[*allOsInfo]("os_propagate")
407
408// moduleOSList collects a list of OSTypes supported by this module based on the HostOrDevice
409// value passed to InitAndroidArchModule and the device_supported and host_supported properties.
410func moduleOSList(ctx ConfigContext, base *ModuleBase) []OsType {
Colin Crossa195f912019-10-16 11:07:20 -0700411 var moduleOSList []OsType
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000412 for _, os := range osTypeList {
Colin Cross8bbc3d52024-09-11 15:33:54 -0700413 for _, t := range ctx.Config().Targets[os] {
Colin Cross08d6f8f2020-11-19 02:33:19 +0000414 if base.supportsTarget(t) {
Jiyong Park1613e552020-09-14 19:43:17 +0900415 moduleOSList = append(moduleOSList, os)
416 break
Colin Crossa195f912019-10-16 11:07:20 -0700417 }
418 }
Colin Crossa195f912019-10-16 11:07:20 -0700419 }
420
Colin Cross8bbc3d52024-09-11 15:33:54 -0700421 if base.commonProperties.CreateCommonOSVariant {
422 // A CommonOS variant was requested so add it to the list of OS variants to
423 // create. It needs to be added to the end because it needs to depend on the
424 // the other variants and inter variant dependencies can only be created from a
425 // later variant in that list to an earlier one. That is because variants are
426 // always processed in the order in which they are created.
427 moduleOSList = append(moduleOSList, CommonOS)
428 }
429
430 return moduleOSList
431}
432
433func (o *osTransitionMutator) Split(ctx BaseModuleContext) []string {
434 module := ctx.Module()
435 base := module.base()
436
437 // Nothing to do for modules that are not architecture specific (e.g. a genrule).
438 if !base.ArchSpecific() {
439 return []string{""}
440 }
441
442 moduleOSList := moduleOSList(ctx, base)
Cole Faust8fc38f32023-12-12 17:14:22 -0800443
Colin Crossa6845402020-11-16 15:08:19 -0800444 // If there are no supported OSes then disable the module.
Colin Cross8bbc3d52024-09-11 15:33:54 -0700445 if len(moduleOSList) == 0 {
Inseob Kimeec88e12020-01-22 11:11:29 +0900446 base.Disable()
Colin Cross8bbc3d52024-09-11 15:33:54 -0700447 return []string{""}
Colin Crossa195f912019-10-16 11:07:20 -0700448 }
449
Colin Crossa6845402020-11-16 15:08:19 -0800450 // Convert the list of supported OsTypes to the variation names.
Colin Crossa195f912019-10-16 11:07:20 -0700451 osNames := make([]string, len(moduleOSList))
Colin Cross8bbc3d52024-09-11 15:33:54 -0700452 osMapping := make(map[string]OsType, len(moduleOSList))
Colin Crossa195f912019-10-16 11:07:20 -0700453 for i, os := range moduleOSList {
454 osNames[i] = os.String()
Colin Cross8bbc3d52024-09-11 15:33:54 -0700455 osMapping[osNames[i]] = os
Colin Crossa195f912019-10-16 11:07:20 -0700456 }
457
Colin Cross8bbc3d52024-09-11 15:33:54 -0700458 SetProvider(ctx, allOsProvider, &allOsInfo{
459 Os: osMapping,
460 Variations: osNames,
461 })
462
463 return osNames
464}
465
466func (o *osTransitionMutator) OutgoingTransition(ctx OutgoingTransitionContext, sourceVariation string) string {
467 return sourceVariation
468}
469
470func (o *osTransitionMutator) IncomingTransition(ctx IncomingTransitionContext, incomingVariation string) string {
471 module := ctx.Module()
472 base := module.base()
473
474 if !base.ArchSpecific() {
475 return ""
Colin Crossa195f912019-10-16 11:07:20 -0700476 }
477
Colin Cross8bbc3d52024-09-11 15:33:54 -0700478 return incomingVariation
479}
480
481func (o *osTransitionMutator) Mutate(ctx BottomUpMutatorContext, variation string) {
482 module := ctx.Module()
483 base := module.base()
484
485 if variation == "" {
486 return
487 }
488
489 allOsInfo, ok := ModuleProvider(ctx, allOsProvider)
490 if !ok {
491 panic(fmt.Errorf("missing allOsProvider"))
492 }
493
494 // Annotate this variant with which OS it was created for, and
Colin Crossa6845402020-11-16 15:08:19 -0800495 // squash the appropriate OS-specific properties into the top level properties.
Colin Cross8bbc3d52024-09-11 15:33:54 -0700496 base.commonProperties.CompileOS = allOsInfo.Os[variation]
497 base.setOSProperties(ctx)
Paul Duffin1356d8c2020-02-25 19:26:33 +0000498
Colin Cross8bbc3d52024-09-11 15:33:54 -0700499 if variation == CommonOS.String() {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000500 // A CommonOS variant was requested so add dependencies from it (the last one in
501 // the list) to the OS type specific variants.
Colin Cross8bbc3d52024-09-11 15:33:54 -0700502 osList := allOsInfo.Variations[:len(allOsInfo.Variations)-1]
503 for _, os := range osList {
504 variation := []blueprint.Variation{{"os", os}}
505 ctx.AddVariationDependencies(variation, commonOsToOsSpecificVariantTag, ctx.ModuleName())
Paul Duffin1356d8c2020-02-25 19:26:33 +0000506 }
507 }
508}
509
Colin Crossc179ea62020-10-09 10:54:15 -0700510type archDepTag struct {
511 blueprint.BaseDependencyTag
512 name string
513}
Paul Duffin1356d8c2020-02-25 19:26:33 +0000514
Colin Crossc179ea62020-10-09 10:54:15 -0700515// Identifies the dependency from CommonOS variant to the os specific variants.
516var commonOsToOsSpecificVariantTag = archDepTag{name: "common os to os specific"}
517
Paul Duffin1356d8c2020-02-25 19:26:33 +0000518// Get the OsType specific variants for the current CommonOS variant.
519//
520// The returned list will only contain enabled OsType specific variants of the
521// module referenced in the supplied context. An empty list is returned if there
522// are no enabled variants or the supplied context is not for an CommonOS
523// variant.
524func GetOsSpecificVariantsOfCommonOSVariant(mctx BaseModuleContext) []Module {
525 var variants []Module
526 mctx.VisitDirectDeps(func(m Module) {
527 if mctx.OtherModuleDependencyTag(m) == commonOsToOsSpecificVariantTag {
Cole Fausta963b942024-04-11 17:43:00 -0700528 if m.Enabled(mctx) {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000529 variants = append(variants, m)
530 }
531 }
532 })
Paul Duffin1356d8c2020-02-25 19:26:33 +0000533 return variants
Colin Crossa195f912019-10-16 11:07:20 -0700534}
535
Dan Willemsen47450072021-10-19 20:24:49 -0700536var DarwinUniversalVariantTag = archDepTag{name: "darwin universal binary"}
537
Colin Cross8bbc3d52024-09-11 15:33:54 -0700538// archTransitionMutator splits a module into a variant for each Target requested by the module. Target selection
Colin Crossa6845402020-11-16 15:08:19 -0800539// for a module is in three levels, OsClass, multilib, and then Target.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700540// OsClass selection is determined by:
Colin Crossd079e0b2022-08-16 10:27:33 -0700541// - The HostOrDeviceSupported value passed in to InitAndroidArchModule by the module type factory, which selects
542// whether the module type can compile for host, device or both.
543// - The host_supported and device_supported properties on the module.
544//
Roland Levillainf5b635d2019-06-05 14:42:57 +0100545// 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 -0700546// for the module, the Device OsClass is selected.
547// Within each selected OsClass, the multilib selection is determined by:
Colin Crossd079e0b2022-08-16 10:27:33 -0700548// - The compile_multilib property if it set (which may be overridden by target.android.compile_multilib or
549// target.host.compile_multilib).
550// - The default multilib passed to InitAndroidArchModule if compile_multilib was not set.
551//
Colin Crossee0bc3b2018-10-02 22:01:37 -0700552// Valid multilib values include:
Colin Crossd079e0b2022-08-16 10:27:33 -0700553//
554// "both": compile for all Targets supported by the OsClass (generally x86_64 and x86, or arm64 and arm).
555// "first": compile for only a single preferred Target supported by the OsClass. This is generally x86_64 or arm64,
556// but may be arm for a 32-bit only build.
557// "32": compile for only a single 32-bit Target supported by the OsClass.
558// "64": compile for only a single 64-bit Target supported by the OsClass.
559// "common": compile a for a single Target that will work on all Targets supported by the OsClass (for example Java).
560// "common_first": compile a for a Target that will work on all Targets supported by the OsClass
561// (same as "common"), plus a second Target for the preferred Target supported by the OsClass
562// (same as "first"). This is used for java_binary that produces a common .jar and a wrapper
563// executable script.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700564//
565// Once the list of Targets is determined, the module is split into a variant for each Target.
566//
567// Modules can be initialized with InitAndroidMultiTargetsArchModule, in which case they will be split by OsClass,
568// 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 -0700569type archTransitionMutator struct{}
570
571type allArchInfo struct {
572 Targets map[string]Target
573 MultiTargets []Target
574 Primary string
575 Multilib string
576}
577
578var allArchProvider = blueprint.NewMutatorProvider[*allArchInfo]("arch_propagate")
579
580func (a *archTransitionMutator) Split(ctx BaseModuleContext) []string {
581 module := ctx.Module()
Colin Cross5eca7cb2018-10-02 14:02:10 -0700582 base := module.base()
583
584 if !base.ArchSpecific() {
Colin Cross8bbc3d52024-09-11 15:33:54 -0700585 return []string{""}
Colin Crossb9db4802016-06-03 01:50:47 +0000586 }
587
Colin Crossa195f912019-10-16 11:07:20 -0700588 os := base.commonProperties.CompileOS
Paul Duffin1356d8c2020-02-25 19:26:33 +0000589 if os == CommonOS {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000590 // Do not create arch specific variants for the CommonOS variant.
Colin Cross8bbc3d52024-09-11 15:33:54 -0700591 return []string{""}
Paul Duffin1356d8c2020-02-25 19:26:33 +0000592 }
593
Colin Cross8bbc3d52024-09-11 15:33:54 -0700594 osTargets := ctx.Config().Targets[os]
Ivan Lozanoc7eafa72024-07-16 17:55:33 +0000595
Colin Crossfb0c16e2019-11-20 17:12:35 -0800596 image := base.commonProperties.ImageVariation
Colin Crossa6845402020-11-16 15:08:19 -0800597 // Filter NativeBridge targets unless they are explicitly supported.
598 // Skip creating native bridge variants for non-core modules.
Paul Duffine3d1ae42021-09-03 17:47:17 +0100599 if os == Android && !(base.IsNativeBridgeSupported() && image == CoreVariation) {
Ivan Lozano03b717d2024-07-18 15:13:50 +0000600 osTargets = slices.DeleteFunc(slices.Clone(osTargets), func(t Target) bool {
601 return bool(t.NativeBridge)
602 })
Colin Crossa195f912019-10-16 11:07:20 -0700603 }
Colin Crossee0bc3b2018-10-02 22:01:37 -0700604
Ivan Lozanoc7eafa72024-07-16 17:55:33 +0000605 // Filter HostCross targets if disabled.
606 if base.HostSupported() && !base.HostCrossSupported() {
Ivan Lozano03b717d2024-07-18 15:13:50 +0000607 osTargets = slices.DeleteFunc(slices.Clone(osTargets), func(t Target) bool {
608 return t.HostCross
609 })
Ivan Lozanoc7eafa72024-07-16 17:55:33 +0000610 }
611
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700612 // only the primary arch in the ramdisk / vendor_ramdisk / recovery partition
Inseob Kim08758f02021-04-08 21:13:22 +0900613 if os == Android && (module.InstallInRecovery() || module.InstallInRamdisk() || module.InstallInVendorRamdisk() || module.InstallInDebugRamdisk()) {
Colin Crossa195f912019-10-16 11:07:20 -0700614 osTargets = []Target{osTargets[0]}
615 }
dimitry1f33e402019-03-26 12:39:31 +0100616
Jaewoong Jung003d8082021-02-24 17:39:54 -0800617 // Windows builds always prefer 32-bit
618 prefer32 := os == Windows
dimitry1f33e402019-03-26 12:39:31 +0100619
Colin Crossa6845402020-11-16 15:08:19 -0800620 // Determine the multilib selection for this module.
Colin Cross8bbc3d52024-09-11 15:33:54 -0700621 multilib, extraMultilib := decodeMultilib(ctx, base)
Colin Crossa6845402020-11-16 15:08:19 -0800622
623 // Convert the multilib selection into a list of Targets.
Colin Crossa195f912019-10-16 11:07:20 -0700624 targets, err := decodeMultilibTargets(multilib, osTargets, prefer32)
625 if err != nil {
Colin Cross8bbc3d52024-09-11 15:33:54 -0700626 ctx.ModuleErrorf("%s", err.Error())
Colin Crossa195f912019-10-16 11:07:20 -0700627 }
Colin Cross5eca7cb2018-10-02 14:02:10 -0700628
Colin Crossc0f0eb82022-07-19 14:41:11 -0700629 // If there are no supported targets disable the module.
630 if len(targets) == 0 {
631 base.Disable()
Colin Cross8bbc3d52024-09-11 15:33:54 -0700632 return []string{""}
Colin Crossc0f0eb82022-07-19 14:41:11 -0700633 }
634
Colin Crossa6845402020-11-16 15:08:19 -0800635 // If the module is using extraMultilib, decode the extraMultilib selection into
636 // a separate list of Targets.
Colin Crossa195f912019-10-16 11:07:20 -0700637 var multiTargets []Target
638 if extraMultilib != "" {
639 multiTargets, err = decodeMultilibTargets(extraMultilib, osTargets, prefer32)
Colin Crossa1ad8d12016-06-01 17:09:44 -0700640 if err != nil {
Colin Cross8bbc3d52024-09-11 15:33:54 -0700641 ctx.ModuleErrorf("%s", err.Error())
Colin Crossa1ad8d12016-06-01 17:09:44 -0700642 }
Colin Crossc0f0eb82022-07-19 14:41:11 -0700643 multiTargets = filterHostCross(multiTargets, targets[0].HostCross)
Colin Crossb9db4802016-06-03 01:50:47 +0000644 }
645
Colin Crossa6845402020-11-16 15:08:19 -0800646 // Recovery is always the primary architecture, filter out any other architectures.
Inseob Kim20fb5d42021-02-02 20:07:58 +0900647 // Common arch is also allowed
Colin Crossfb0c16e2019-11-20 17:12:35 -0800648 if image == RecoveryVariation {
Colin Cross8bbc3d52024-09-11 15:33:54 -0700649 primaryArch := ctx.Config().DevicePrimaryArchType()
Inseob Kim20fb5d42021-02-02 20:07:58 +0900650 targets = filterToArch(targets, primaryArch, Common)
651 multiTargets = filterToArch(multiTargets, primaryArch, Common)
Colin Crossfb0c16e2019-11-20 17:12:35 -0800652 }
653
Colin Crossa6845402020-11-16 15:08:19 -0800654 // If there are no supported targets disable the module.
Colin Crossa195f912019-10-16 11:07:20 -0700655 if len(targets) == 0 {
Inseob Kimeec88e12020-01-22 11:11:29 +0900656 base.Disable()
Colin Cross8bbc3d52024-09-11 15:33:54 -0700657 return []string{""}
Dan Willemsen3f32f032016-07-11 14:36:48 -0700658 }
659
Colin Crossa6845402020-11-16 15:08:19 -0800660 // Convert the targets into a list of arch variation names.
Colin Crossa195f912019-10-16 11:07:20 -0700661 targetNames := make([]string, len(targets))
Colin Cross8bbc3d52024-09-11 15:33:54 -0700662 targetMapping := make(map[string]Target, len(targets))
Colin Crossa195f912019-10-16 11:07:20 -0700663 for i, target := range targets {
664 targetNames[i] = target.ArchVariation()
Colin Cross8bbc3d52024-09-11 15:33:54 -0700665 targetMapping[targetNames[i]] = targets[i]
Colin Crossa1ad8d12016-06-01 17:09:44 -0700666 }
667
Colin Cross8bbc3d52024-09-11 15:33:54 -0700668 SetProvider(ctx, allArchProvider, &allArchInfo{
669 Targets: targetMapping,
670 MultiTargets: multiTargets,
671 Primary: targetNames[0],
672 Multilib: multilib,
673 })
674 return targetNames
675}
Dan Willemsen8528f4e2021-10-19 00:22:06 -0700676
Colin Cross8bbc3d52024-09-11 15:33:54 -0700677func (a *archTransitionMutator) OutgoingTransition(ctx OutgoingTransitionContext, sourceVariation string) string {
678 return sourceVariation
679}
680
681func (a *archTransitionMutator) IncomingTransition(ctx IncomingTransitionContext, incomingVariation string) string {
682 module := ctx.Module()
683 base := module.base()
684
685 if !base.ArchSpecific() {
686 return ""
687 }
688
689 os := base.commonProperties.CompileOS
690 if os == CommonOS {
691 // Do not create arch specific variants for the CommonOS variant.
692 return ""
693 }
694
695 if incomingVariation == "" {
696 multilib, _ := decodeMultilib(ctx, base)
697 if multilib == "common" {
698 return "common"
Dan Willemsen8528f4e2021-10-19 00:22:06 -0700699 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800700 }
Colin Cross8bbc3d52024-09-11 15:33:54 -0700701 return incomingVariation
702}
703
704func (a *archTransitionMutator) Mutate(ctx BottomUpMutatorContext, variation string) {
705 module := ctx.Module()
706 base := module.base()
707 os := base.commonProperties.CompileOS
708
709 if os == CommonOS {
710 // Make sure that the target related properties are initialized for the
711 // CommonOS variant.
712 addTargetProperties(module, commonTargetMap[os.Name], nil, true)
713 return
714 }
715
716 if variation == "" {
717 return
718 }
719
720 if !base.ArchSpecific() {
721 panic(fmt.Errorf("found variation %q for non arch specifc module", variation))
722 }
723
724 allArchInfo, ok := ModuleProvider(ctx, allArchProvider)
725 if !ok {
726 return
727 }
728
729 target, ok := allArchInfo.Targets[variation]
730 if !ok {
731 panic(fmt.Errorf("missing Target for %q", variation))
732 }
733 primary := variation == allArchInfo.Primary
734 multiTargets := allArchInfo.MultiTargets
735
736 // Annotate the new variant with which Target it was created for, and
737 // squash the appropriate arch-specific properties into the top level properties.
738 addTargetProperties(ctx.Module(), target, multiTargets, primary)
739 base.setArchProperties(ctx)
740
741 // Install support doesn't understand Darwin+Arm64
742 if os == Darwin && target.HostCross {
743 base.commonProperties.SkipInstall = true
744 }
Dan Willemsen47450072021-10-19 20:24:49 -0700745
746 // Create a dependency for Darwin Universal binaries from the primary to secondary
747 // architecture. The module itself will be responsible for calling lipo to merge the outputs.
748 if os == Darwin {
Colin Cross8bbc3d52024-09-11 15:33:54 -0700749 isUniversalBinary := (allArchInfo.Multilib == "darwin_universal" && len(allArchInfo.Targets) == 2) ||
750 allArchInfo.Multilib == "darwin_universal_common_first" && len(allArchInfo.Targets) == 3
751 isPrimary := variation == ctx.Config().BuildArch.String()
752 hasSecondaryConfigured := len(ctx.Config().Targets[Darwin]) > 1
753 if isUniversalBinary && isPrimary && hasSecondaryConfigured {
754 secondaryArch := ctx.Config().Targets[Darwin][1].Arch.String()
755 variation := []blueprint.Variation{{"arch", secondaryArch}}
756 ctx.AddVariationDependencies(variation, DarwinUniversalVariantTag, ctx.ModuleName())
Dan Willemsen47450072021-10-19 20:24:49 -0700757 }
758 }
Colin Cross8bbc3d52024-09-11 15:33:54 -0700759
Colin Cross3f40fa42015-01-30 17:27:36 -0800760}
761
Colin Crossa6845402020-11-16 15:08:19 -0800762// addTargetProperties annotates a variant with the Target is is being compiled for, the list
763// of additional Targets it is supporting (if any), and whether it is the primary Target for
764// the module.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000765func addTargetProperties(m Module, target Target, multiTargets []Target, primaryTarget bool) {
766 m.base().commonProperties.CompileTarget = target
767 m.base().commonProperties.CompileMultiTargets = multiTargets
768 m.base().commonProperties.CompilePrimary = primaryTarget
Cole Faust0aa21cc2024-03-20 12:28:03 -0700769 m.base().commonProperties.ArchReady = true
Paul Duffin1356d8c2020-02-25 19:26:33 +0000770}
771
Colin Crossa6845402020-11-16 15:08:19 -0800772// decodeMultilib returns the appropriate compile_multilib property for the module, or the default
773// multilib from the factory's call to InitAndroidArchModule if none was set. For modules that
774// called InitAndroidMultiTargetsArchModule it always returns "common" for multilib, and returns
775// the actual multilib in extraMultilib.
Colin Cross8bbc3d52024-09-11 15:33:54 -0700776func decodeMultilib(ctx ConfigContext, base *ModuleBase) (multilib, extraMultilib string) {
777 os := base.commonProperties.CompileOS
778 ignorePrefer32OnDevice := ctx.Config().IgnorePrefer32OnDevice()
Colin Crossa6845402020-11-16 15:08:19 -0800779 // First check the "android.compile_multilib" or "host.compile_multilib" properties.
Dan Willemsen47450072021-10-19 20:24:49 -0700780 switch os.Class {
Colin Crossee0bc3b2018-10-02 22:01:37 -0700781 case Device:
782 multilib = String(base.commonProperties.Target.Android.Compile_multilib)
Jiyong Park1613e552020-09-14 19:43:17 +0900783 case Host:
Colin Crossee0bc3b2018-10-02 22:01:37 -0700784 multilib = String(base.commonProperties.Target.Host.Compile_multilib)
785 }
Colin Crossa6845402020-11-16 15:08:19 -0800786
787 // If those aren't set, try the "compile_multilib" property.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700788 if multilib == "" {
789 multilib = String(base.commonProperties.Compile_multilib)
790 }
Colin Crossa6845402020-11-16 15:08:19 -0800791
792 // If that wasn't set, use the default multilib set by the factory.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700793 if multilib == "" {
794 multilib = base.commonProperties.Default_multilib
795 }
796
Christopher Ferris98f10222022-07-13 23:16:52 -0700797 // If a device is configured with multiple targets, this option
798 // force all device targets that prefer32 to be compiled only as
799 // the first target.
800 if ignorePrefer32OnDevice && os.Class == Device && (multilib == "prefer32" || multilib == "first_prefer32") {
801 multilib = "first"
802 }
803
Colin Crossee0bc3b2018-10-02 22:01:37 -0700804 if base.commonProperties.UseTargetVariants {
Dan Willemsen47450072021-10-19 20:24:49 -0700805 // Darwin has the concept of "universal binaries" which is implemented in Soong by
806 // building both x86_64 and arm64 variants, and having select module types know how to
807 // merge the outputs of their corresponding variants together into a final binary. Most
808 // module types don't need to understand this logic, as we only build a small portion
809 // of the tree for Darwin, and only module types writing macho files need to do the
810 // merging.
811 //
812 // This logic is not enabled for:
813 // "common", as it's not an arch-specific variant
814 // "32", as Darwin never has a 32-bit variant
815 // !UseTargetVariants, as the module has opted into handling the arch-specific logic on
816 // its own.
817 if os == Darwin && multilib != "common" && multilib != "32" {
818 if multilib == "common_first" {
819 multilib = "darwin_universal_common_first"
820 } else {
821 multilib = "darwin_universal"
822 }
823 }
824
Colin Crossee0bc3b2018-10-02 22:01:37 -0700825 return multilib, ""
826 } else {
827 // For app modules a single arch variant will be created per OS class which is expected to handle all the
828 // selected arches. Return the common-type as multilib and any Android.bp provided multilib as extraMultilib
829 if multilib == base.commonProperties.Default_multilib {
830 multilib = "first"
831 }
832 return base.commonProperties.Default_multilib, multilib
833 }
834}
835
Colin Crossa6845402020-11-16 15:08:19 -0800836// filterToArch takes a list of Targets and an ArchType, and returns a modified list that contains
Inseob Kim20fb5d42021-02-02 20:07:58 +0900837// only Targets that have the specified ArchTypes.
838func filterToArch(targets []Target, archs ...ArchType) []Target {
Colin Crossfb0c16e2019-11-20 17:12:35 -0800839 for i := 0; i < len(targets); i++ {
Inseob Kim20fb5d42021-02-02 20:07:58 +0900840 found := false
841 for _, arch := range archs {
842 if targets[i].Arch.ArchType == arch {
843 found = true
844 break
845 }
846 }
847 if !found {
Colin Crossfb0c16e2019-11-20 17:12:35 -0800848 targets = append(targets[:i], targets[i+1:]...)
849 i--
850 }
851 }
852 return targets
853}
854
Colin Crossc0f0eb82022-07-19 14:41:11 -0700855// filterHostCross takes a list of Targets and a hostCross value, and returns a modified list
856// that contains only Targets that have the specified HostCross.
857func filterHostCross(targets []Target, hostCross bool) []Target {
858 for i := 0; i < len(targets); i++ {
859 if targets[i].HostCross != hostCross {
860 targets = append(targets[:i], targets[i+1:]...)
861 i--
862 }
863 }
864 return targets
865}
866
Colin Crossa6845402020-11-16 15:08:19 -0800867// archPropRoot is a struct type used as the top level of the arch-specific properties. It
868// contains the "arch", "multilib", and "target" property structs. It is used to split up the
869// property structs to limit how much is allocated when a single arch-specific property group is
870// used. The types are interface{} because they will hold instances of runtime-created types.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800871type archPropRoot struct {
872 Arch, Multilib, Target interface{}
873}
874
Colin Crossa6845402020-11-16 15:08:19 -0800875// archPropTypeDesc holds the runtime-created types for the property structs to instantiate to
876// create an archPropRoot property struct.
877type archPropTypeDesc struct {
878 arch, multilib, target reflect.Type
879}
880
Colin Crosscbbd13f2020-01-17 14:08:22 -0800881// createArchPropTypeDesc takes a reflect.Type that is either a struct or a pointer to a struct, and
882// returns lists of reflect.Types that contains the arch-variant properties inside structs for each
883// arch, multilib and target property.
Colin Crossa6845402020-11-16 15:08:19 -0800884//
885// This is a relatively expensive operation, so the results are cached in the global
886// archPropTypeMap. It is constructed entirely based on compile-time data, so there is no need
887// to isolate the results between multiple tests running in parallel.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800888func createArchPropTypeDesc(props reflect.Type) []archPropTypeDesc {
Colin Crossb1d8c992020-01-21 11:43:29 -0800889 // Each property struct shard will be nested many times under the runtime generated arch struct,
890 // which can hit the limit of 64kB for the name of runtime generated structs. They are nested
891 // 97 times now, which may grow in the future, plus there is some overhead for the containing
892 // type. This number may need to be reduced if too many are added, but reducing it too far
893 // could cause problems if a single deeply nested property no longer fits in the name.
894 const maxArchTypeNameSize = 500
895
Colin Crossa6845402020-11-16 15:08:19 -0800896 // Convert the type to a new set of types that contains only the arch-specific properties
Usta Shrestha0b52d832022-02-04 21:37:39 -0500897 // (those that are tagged with `android:"arch_variant"`), and sharded into multiple types
Colin Crossa6845402020-11-16 15:08:19 -0800898 // to keep the runtime-generated names under the limit.
Colin Crossb1d8c992020-01-21 11:43:29 -0800899 propShards, _ := proptools.FilterPropertyStructSharded(props, maxArchTypeNameSize, filterArchStruct)
Colin Crossa6845402020-11-16 15:08:19 -0800900
901 // If the type has no arch-specific properties there is nothing to do.
Colin Crosscb988072019-01-24 14:58:11 -0800902 if len(propShards) == 0 {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700903 return nil
904 }
905
Colin Crosscbbd13f2020-01-17 14:08:22 -0800906 var ret []archPropTypeDesc
Colin Crossc17727d2018-10-24 12:42:09 -0700907 for _, props := range propShards {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700908
Colin Crossa6845402020-11-16 15:08:19 -0800909 // variantFields takes a list of variant property field names and returns a list the
910 // StructFields with the names and the type of the current shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700911 variantFields := func(names []string) []reflect.StructField {
912 ret := make([]reflect.StructField, len(names))
Dan Willemsenb1957a52016-06-23 23:44:54 -0700913
Colin Crossc17727d2018-10-24 12:42:09 -0700914 for i, name := range names {
915 ret[i].Name = name
916 ret[i].Type = props
Dan Willemsen866b5632017-09-22 12:28:24 -0700917 }
Colin Crossc17727d2018-10-24 12:42:09 -0700918
919 return ret
920 }
921
Colin Crossa6845402020-11-16 15:08:19 -0800922 // Create a type that contains the properties in this shard repeated for each
923 // architecture, architecture variant, and architecture feature.
Colin Crossc17727d2018-10-24 12:42:09 -0700924 archFields := make([]reflect.StructField, len(archTypeList))
925 for i, arch := range archTypeList {
Colin Crossa6845402020-11-16 15:08:19 -0800926 var variants []string
Colin Crossc17727d2018-10-24 12:42:09 -0700927
928 for _, archVariant := range archVariants[arch] {
929 archVariant := variantReplacer.Replace(archVariant)
930 variants = append(variants, proptools.FieldNameForProperty(archVariant))
931 }
Liz Kammer2c2afe22022-02-11 11:35:03 -0500932 for _, cpuVariant := range cpuVariants[arch] {
933 cpuVariant := variantReplacer.Replace(cpuVariant)
934 variants = append(variants, proptools.FieldNameForProperty(cpuVariant))
935 }
Colin Crossc17727d2018-10-24 12:42:09 -0700936 for _, feature := range archFeatures[arch] {
937 feature := variantReplacer.Replace(feature)
938 variants = append(variants, proptools.FieldNameForProperty(feature))
939 }
940
Colin Crossa6845402020-11-16 15:08:19 -0800941 // Create the StructFields for each architecture variant architecture feature
942 // (e.g. "arch.arm.cortex-a53" or "arch.arm.neon").
Colin Crossc17727d2018-10-24 12:42:09 -0700943 fields := variantFields(variants)
944
Colin Crossa6845402020-11-16 15:08:19 -0800945 // Create the StructField for the architecture itself (e.g. "arch.arm"). The special
946 // "BlueprintEmbed" name is used by Blueprint to put the properties in the
947 // parent struct.
Colin Crossc17727d2018-10-24 12:42:09 -0700948 fields = append([]reflect.StructField{{
949 Name: "BlueprintEmbed",
950 Type: props,
951 Anonymous: true,
952 }}, fields...)
953
954 archFields[i] = reflect.StructField{
955 Name: arch.Field,
956 Type: reflect.StructOf(fields),
957 }
958 }
Colin Crossa6845402020-11-16 15:08:19 -0800959
960 // Create the type of the "arch" property struct for this shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700961 archType := reflect.StructOf(archFields)
962
Colin Crossa6845402020-11-16 15:08:19 -0800963 // Create the type for the "multilib" property struct for this shard, containing the
964 // "multilib.lib32" and "multilib.lib64" property structs.
Colin Crossc17727d2018-10-24 12:42:09 -0700965 multilibType := reflect.StructOf(variantFields([]string{"Lib32", "Lib64"}))
966
Colin Crossa6845402020-11-16 15:08:19 -0800967 // Start with a list of the special targets
Colin Crossc17727d2018-10-24 12:42:09 -0700968 targets := []string{
969 "Host",
970 "Android64",
971 "Android32",
972 "Bionic",
Colin Cross528d67e2021-07-23 22:23:07 +0000973 "Glibc",
974 "Musl",
Colin Crossc17727d2018-10-24 12:42:09 -0700975 "Linux",
Colin Crossa98d36d2022-03-07 14:39:49 -0800976 "Host_linux",
Colin Crossc17727d2018-10-24 12:42:09 -0700977 "Not_windows",
978 "Arm_on_x86",
979 "Arm_on_x86_64",
Victor Khimenkoc26fcf42020-05-07 22:16:33 +0200980 "Native_bridge",
Colin Crossc17727d2018-10-24 12:42:09 -0700981 }
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000982 for _, os := range osTypeList {
Colin Crossa6845402020-11-16 15:08:19 -0800983 // Add all the OSes.
Colin Crossc17727d2018-10-24 12:42:09 -0700984 targets = append(targets, os.Field)
985
Colin Crossa6845402020-11-16 15:08:19 -0800986 // Add the OS/Arch combinations, e.g. "android_arm64".
Colin Crossc17727d2018-10-24 12:42:09 -0700987 for _, archType := range osArchTypeMap[os] {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400988 targets = append(targets, GetCompoundTargetField(os, archType))
Colin Crossc17727d2018-10-24 12:42:09 -0700989
Colin Cross1aa45b02022-02-10 10:33:10 -0800990 // Also add the special "linux_<arch>", "bionic_<arch>" , "glibc_<arch>", and
991 // "musl_<arch>" property structs.
Colin Crossc17727d2018-10-24 12:42:09 -0700992 if os.Linux() {
993 target := "Linux_" + archType.Name
994 if !InList(target, targets) {
995 targets = append(targets, target)
996 }
997 }
Colin Crossa98d36d2022-03-07 14:39:49 -0800998 if os.Linux() && os.Class == Host {
999 target := "Host_linux_" + archType.Name
1000 if !InList(target, targets) {
1001 targets = append(targets, target)
1002 }
1003 }
Colin Crossc17727d2018-10-24 12:42:09 -07001004 if os.Bionic() {
1005 target := "Bionic_" + archType.Name
1006 if !InList(target, targets) {
1007 targets = append(targets, target)
1008 }
Dan Willemsen866b5632017-09-22 12:28:24 -07001009 }
Colin Cross1aa45b02022-02-10 10:33:10 -08001010 if os == Linux {
1011 target := "Glibc_" + archType.Name
1012 if !InList(target, targets) {
1013 targets = append(targets, target)
1014 }
1015 }
1016 if os == LinuxMusl {
1017 target := "Musl_" + archType.Name
1018 if !InList(target, targets) {
1019 targets = append(targets, target)
1020 }
1021 }
Dan Willemsen866b5632017-09-22 12:28:24 -07001022 }
Dan Willemsenb1957a52016-06-23 23:44:54 -07001023 }
Dan Willemsenb1957a52016-06-23 23:44:54 -07001024
Colin Crossa6845402020-11-16 15:08:19 -08001025 // Create the type for the "target" property struct for this shard.
Colin Crossc17727d2018-10-24 12:42:09 -07001026 targetType := reflect.StructOf(variantFields(targets))
Colin Crosscbbd13f2020-01-17 14:08:22 -08001027
Colin Crossa6845402020-11-16 15:08:19 -08001028 // Return a descriptor of the 3 runtime-created types.
Colin Crosscbbd13f2020-01-17 14:08:22 -08001029 ret = append(ret, archPropTypeDesc{
1030 arch: reflect.PtrTo(archType),
1031 multilib: reflect.PtrTo(multilibType),
1032 target: reflect.PtrTo(targetType),
1033 })
Colin Crossc17727d2018-10-24 12:42:09 -07001034 }
1035 return ret
Dan Willemsenb1957a52016-06-23 23:44:54 -07001036}
1037
Colin Crossa6845402020-11-16 15:08:19 -08001038// variantReplacer converts architecture variant or architecture feature names into names that
1039// are valid for an Android.bp file.
1040var variantReplacer = strings.NewReplacer("-", "_", ".", "_")
1041
1042// filterArchStruct returns true if the given field is an architecture specific property.
Colin Cross74449102019-09-25 11:26:40 -07001043func filterArchStruct(field reflect.StructField, prefix string) (bool, reflect.StructField) {
1044 if proptools.HasTag(field, "android", "arch_variant") {
1045 // The arch_variant field isn't necessary past this point
1046 // Instead of wasting space, just remove it. Go also has a
1047 // 16-bit limit on structure name length. The name is constructed
1048 // based on the Go source representation of the structure, so
1049 // the tag names count towards that length.
Colin Crossb4fecbf2020-01-21 11:38:47 -08001050
1051 androidTag := field.Tag.Get("android")
1052 values := strings.Split(androidTag, ",")
1053
1054 if string(field.Tag) != `android:"`+strings.Join(values, ",")+`"` {
1055 panic(fmt.Errorf("unexpected tag format %q", field.Tag))
Colin Cross74449102019-09-25 11:26:40 -07001056 }
Colin Crossb4fecbf2020-01-21 11:38:47 -08001057 // these tags don't need to be present in the runtime generated struct type.
Cole Faust5fda87b2024-04-24 11:21:14 -07001058 // However replace_instead_of_append does, because it's read by the blueprint
1059 // property extending util functions, which can operate on these generated arch
1060 // property structs.
1061 values = RemoveListFromList(values, []string{"arch_variant", "variant_prepend", "path"})
Liz Kammerff966b12022-07-29 10:49:16 -04001062 if len(values) > 0 {
Cole Faust5fda87b2024-04-24 11:21:14 -07001063 if values[0] != "replace_instead_of_append" || len(values) > 1 {
1064 panic(fmt.Errorf("unknown tags %q in field %q", values, prefix+field.Name))
1065 }
1066 field.Tag = `android:"replace_instead_of_append"`
1067 } else {
1068 field.Tag = ``
Colin Crossb4fecbf2020-01-21 11:38:47 -08001069 }
Colin Cross74449102019-09-25 11:26:40 -07001070 return true, field
1071 }
1072 return false, field
1073}
1074
Colin Crossa6845402020-11-16 15:08:19 -08001075// archPropTypeMap contains a cache of the results of createArchPropTypeDesc for each type. It is
1076// shared across all Contexts, but is constructed based only on compile-time information so there
1077// is no risk of contaminating one Context with data from another.
Dan Willemsenb1957a52016-06-23 23:44:54 -07001078var archPropTypeMap OncePer
1079
Colin Crossa6845402020-11-16 15:08:19 -08001080// initArchModule adds the architecture-specific property structs to a Module.
1081func initArchModule(m Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001082
1083 base := m.base()
1084
Ustaeabf0f32021-12-06 15:17:23 -05001085 if len(base.archProperties) != 0 {
1086 panic(fmt.Errorf("module %s already has archProperties", m.Name()))
1087 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001088
Ustaeabf0f32021-12-06 15:17:23 -05001089 getStructType := func(properties interface{}) reflect.Type {
Colin Cross3f40fa42015-01-30 17:27:36 -08001090 propertiesValue := reflect.ValueOf(properties)
Colin Cross62496a02016-08-08 15:49:17 -07001091 t := propertiesValue.Type()
Colin Cross3f40fa42015-01-30 17:27:36 -08001092 if propertiesValue.Kind() != reflect.Ptr {
Colin Crossca860ac2016-01-04 14:34:37 -08001093 panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
1094 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -08001095 }
1096
1097 propertiesValue = propertiesValue.Elem()
1098 if propertiesValue.Kind() != reflect.Struct {
Ustaeabf0f32021-12-06 15:17:23 -05001099 panic(fmt.Errorf("properties must be a pointer to a struct, got a pointer to %T",
Colin Crossca860ac2016-01-04 14:34:37 -08001100 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -08001101 }
Ustaeabf0f32021-12-06 15:17:23 -05001102 return t
1103 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001104
Usta851a3272022-01-05 23:42:33 -05001105 for _, properties := range m.GetProperties() {
Ustaeabf0f32021-12-06 15:17:23 -05001106 t := getStructType(properties)
Colin Crossa6845402020-11-16 15:08:19 -08001107 // Get or create the arch-specific property struct types for this property struct type.
Colin Cross571cccf2019-02-04 11:22:08 -08001108 archPropTypes := archPropTypeMap.Once(NewCustomOnceKey(t), func() interface{} {
Colin Crosscbbd13f2020-01-17 14:08:22 -08001109 return createArchPropTypeDesc(t)
1110 }).([]archPropTypeDesc)
Colin Cross3f40fa42015-01-30 17:27:36 -08001111
Colin Crossa6845402020-11-16 15:08:19 -08001112 // Instantiate one of each arch-specific property struct type and add it to the
1113 // properties for the Module.
Colin Crossc17727d2018-10-24 12:42:09 -07001114 var archProperties []interface{}
1115 for _, t := range archPropTypes {
Colin Crosscbbd13f2020-01-17 14:08:22 -08001116 archProperties = append(archProperties, &archPropRoot{
1117 Arch: reflect.Zero(t.arch).Interface(),
1118 Multilib: reflect.Zero(t.multilib).Interface(),
1119 Target: reflect.Zero(t.target).Interface(),
1120 })
Dan Willemsenb1957a52016-06-23 23:44:54 -07001121 }
Colin Crossc17727d2018-10-24 12:42:09 -07001122 base.archProperties = append(base.archProperties, archProperties)
1123 m.AddProperties(archProperties...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001124 }
1125
Colin Cross3f40fa42015-01-30 17:27:36 -08001126}
1127
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001128func maybeBlueprintEmbed(src reflect.Value) reflect.Value {
Colin Crossa6845402020-11-16 15:08:19 -08001129 // If the value of the field is a struct (as opposed to a pointer to a struct) then step
1130 // into the BlueprintEmbed field.
Dan Willemsenb1957a52016-06-23 23:44:54 -07001131 if src.Kind() == reflect.Struct {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001132 return src.FieldByName("BlueprintEmbed")
1133 } else {
1134 return src
Colin Cross06a931b2015-10-28 17:23:31 -07001135 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001136}
1137
1138// Merges the property struct in srcValue into dst.
Liz Kammerb6dbc872021-05-14 15:14:40 -04001139func mergePropertyStruct(ctx ArchVariantContext, dst interface{}, srcValue reflect.Value) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001140 src := maybeBlueprintEmbed(srcValue).Interface()
Colin Cross06a931b2015-10-28 17:23:31 -07001141
Colin Crossa6845402020-11-16 15:08:19 -08001142 // order checks the `android:"variant_prepend"` tag to handle properties where the
1143 // arch-specific value needs to come before the generic value, for example for lists of
1144 // include directories.
Colin Cross1e7e0432024-02-02 10:59:50 -08001145 order := func(dstField, srcField reflect.StructField) (proptools.Order, error) {
Colin Cross6ee75b62016-05-05 15:57:15 -07001146 if proptools.HasTag(dstField, "android", "variant_prepend") {
1147 return proptools.Prepend, nil
1148 } else {
1149 return proptools.Append, nil
1150 }
1151 }
1152
Colin Crossa6845402020-11-16 15:08:19 -08001153 // Squash the located property struct into the destination property struct.
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001154 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src, nil, order)
Colin Cross06a931b2015-10-28 17:23:31 -07001155 if err != nil {
1156 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
1157 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
1158 } else {
1159 panic(err)
1160 }
1161 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001162}
Colin Cross85a88972015-11-23 13:29:51 -08001163
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001164// Returns the immediate child of the input property struct that corresponds to
1165// the sub-property "field".
Liz Kammerb6dbc872021-05-14 15:14:40 -04001166func getChildPropertyStruct(ctx ArchVariantContext,
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001167 src reflect.Value, field, userFriendlyField string) (reflect.Value, bool) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001168
1169 // Step into non-nil pointers to structs in the src value.
1170 if src.Kind() == reflect.Ptr {
1171 if src.IsNil() {
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001172 return reflect.Value{}, false
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001173 }
1174 src = src.Elem()
1175 }
1176
1177 // Find the requested field in the src struct.
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001178 child := src.FieldByName(proptools.FieldNameForProperty(field))
1179 if !child.IsValid() {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001180 ctx.ModuleErrorf("field %q does not exist", userFriendlyField)
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001181 return reflect.Value{}, false
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001182 }
1183
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001184 if child.IsZero() {
1185 return reflect.Value{}, false
1186 }
1187
1188 return child, true
Colin Cross06a931b2015-10-28 17:23:31 -07001189}
1190
Colin Crossa6845402020-11-16 15:08:19 -08001191// Squash the appropriate OS-specific property structs into the matching top level property structs
1192// based on the CompileOS value that was annotated on the variant.
Colin Crossa195f912019-10-16 11:07:20 -07001193func (m *ModuleBase) setOSProperties(ctx BottomUpMutatorContext) {
1194 os := m.commonProperties.CompileOS
1195
Ustadca02192021-12-20 12:56:46 -05001196 for i := range m.archProperties {
Usta851a3272022-01-05 23:42:33 -05001197 genProps := m.GetProperties()[i]
Colin Crossa195f912019-10-16 11:07:20 -07001198 if m.archProperties[i] == nil {
1199 continue
1200 }
1201 for _, archProperties := range m.archProperties[i] {
1202 archPropValues := reflect.ValueOf(archProperties).Elem()
1203
Colin Crosscbbd13f2020-01-17 14:08:22 -08001204 targetProp := archPropValues.FieldByName("Target").Elem()
Colin Crossa195f912019-10-16 11:07:20 -07001205
1206 // Handle host-specific properties in the form:
1207 // target: {
1208 // host: {
1209 // key: value,
1210 // },
1211 // },
Jiyong Park1613e552020-09-14 19:43:17 +09001212 if os.Class == Host {
Colin Crossa195f912019-10-16 11:07:20 -07001213 field := "Host"
1214 prefix := "target.host"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001215 if hostProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1216 mergePropertyStruct(ctx, genProps, hostProperties)
1217 }
Colin Crossa195f912019-10-16 11:07:20 -07001218 }
1219
1220 // Handle target OS generalities of the form:
1221 // target: {
1222 // bionic: {
1223 // key: value,
1224 // },
1225 // }
1226 if os.Linux() {
1227 field := "Linux"
1228 prefix := "target.linux"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001229 if linuxProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1230 mergePropertyStruct(ctx, genProps, linuxProperties)
1231 }
Colin Crossa195f912019-10-16 11:07:20 -07001232 }
1233
Colin Crossa98d36d2022-03-07 14:39:49 -08001234 if os.Linux() && os.Class == Host {
1235 field := "Host_linux"
1236 prefix := "target.host_linux"
1237 if linuxProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1238 mergePropertyStruct(ctx, genProps, linuxProperties)
1239 }
1240 }
1241
Colin Crossa195f912019-10-16 11:07:20 -07001242 if os.Bionic() {
1243 field := "Bionic"
1244 prefix := "target.bionic"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001245 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1246 mergePropertyStruct(ctx, genProps, bionicProperties)
1247 }
Colin Crossa195f912019-10-16 11:07:20 -07001248 }
1249
Colin Cross528d67e2021-07-23 22:23:07 +00001250 if os == Linux {
1251 field := "Glibc"
1252 prefix := "target.glibc"
1253 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1254 mergePropertyStruct(ctx, genProps, bionicProperties)
1255 }
1256 }
1257
1258 if os == LinuxMusl {
1259 field := "Musl"
1260 prefix := "target.musl"
1261 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1262 mergePropertyStruct(ctx, genProps, bionicProperties)
1263 }
Colin Cross528d67e2021-07-23 22:23:07 +00001264 }
1265
Colin Crossa195f912019-10-16 11:07:20 -07001266 // Handle target OS properties in the form:
1267 // target: {
1268 // linux_glibc: {
1269 // key: value,
1270 // },
1271 // not_windows: {
1272 // key: value,
1273 // },
1274 // android {
1275 // key: value,
1276 // },
1277 // },
1278 field := os.Field
1279 prefix := "target." + os.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001280 if osProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1281 mergePropertyStruct(ctx, genProps, osProperties)
1282 }
Colin Crossa195f912019-10-16 11:07:20 -07001283
Jiyong Park1613e552020-09-14 19:43:17 +09001284 if os.Class == Host && os != Windows {
Colin Crossa195f912019-10-16 11:07:20 -07001285 field := "Not_windows"
1286 prefix := "target.not_windows"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001287 if notWindowsProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1288 mergePropertyStruct(ctx, genProps, notWindowsProperties)
1289 }
Colin Crossa195f912019-10-16 11:07:20 -07001290 }
1291
1292 // Handle 64-bit device properties in the form:
1293 // target {
1294 // android64 {
1295 // key: value,
1296 // },
1297 // android32 {
1298 // key: value,
1299 // },
1300 // },
1301 // WARNING: this is probably not what you want to use in your blueprints file, it selects
1302 // options for all targets on a device that supports 64-bit binaries, not just the targets
1303 // that are being compiled for 64-bit. Its expected use case is binaries like linker and
1304 // debuggerd that need to know when they are a 32-bit process running on a 64-bit device
1305 if os.Class == Device {
1306 if ctx.Config().Android64() {
1307 field := "Android64"
1308 prefix := "target.android64"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001309 if android64Properties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1310 mergePropertyStruct(ctx, genProps, android64Properties)
1311 }
Colin Crossa195f912019-10-16 11:07:20 -07001312 } else {
1313 field := "Android32"
1314 prefix := "target.android32"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001315 if android32Properties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1316 mergePropertyStruct(ctx, genProps, android32Properties)
1317 }
Colin Crossa195f912019-10-16 11:07:20 -07001318 }
1319 }
1320 }
1321 }
1322}
1323
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001324// Returns the struct containing the properties specific to the given
1325// architecture type. These look like this in Blueprint files:
Colin Crossd079e0b2022-08-16 10:27:33 -07001326//
1327// arch: {
1328// arm64: {
1329// key: value,
1330// },
1331// },
1332//
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001333// This struct will also contain sub-structs containing to the architecture/CPU
1334// variants and features that themselves contain properties specific to those.
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001335func getArchTypeStruct(ctx ArchVariantContext, archProperties interface{}, archType ArchType) (reflect.Value, bool) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001336 archPropValues := reflect.ValueOf(archProperties).Elem()
1337 archProp := archPropValues.FieldByName("Arch").Elem()
1338 prefix := "arch." + archType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001339 return getChildPropertyStruct(ctx, archProp, archType.Name, prefix)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001340}
1341
1342// Returns the struct containing the properties specific to a given multilib
1343// value. These look like this in the Blueprint file:
Colin Crossd079e0b2022-08-16 10:27:33 -07001344//
1345// multilib: {
1346// lib32: {
1347// key: value,
1348// },
1349// },
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001350func getMultilibStruct(ctx ArchVariantContext, archProperties interface{}, archType ArchType) (reflect.Value, bool) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001351 archPropValues := reflect.ValueOf(archProperties).Elem()
1352 multilibProp := archPropValues.FieldByName("Multilib").Elem()
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001353 return getChildPropertyStruct(ctx, multilibProp, archType.Multilib, "multilib."+archType.Multilib)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001354}
1355
Liz Kammer9abd62d2021-05-21 08:37:59 -04001356func GetCompoundTargetField(os OsType, arch ArchType) string {
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04001357 return os.Field + "_" + arch.Name
1358}
1359
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001360// Returns the structs corresponding to the properties specific to the given
1361// architecture and OS in archProperties.
1362func getArchProperties(ctx BaseMutatorContext, archProperties interface{}, arch Arch, os OsType, nativeBridgeEnabled bool) []reflect.Value {
1363 result := make([]reflect.Value, 0)
1364 archPropValues := reflect.ValueOf(archProperties).Elem()
1365
1366 targetProp := archPropValues.FieldByName("Target").Elem()
1367
1368 archType := arch.ArchType
1369
1370 if arch.ArchType != Common {
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001371 archStruct, ok := getArchTypeStruct(ctx, archProperties, arch.ArchType)
1372 if ok {
1373 result = append(result, archStruct)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001374
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001375 // Handle arch-variant-specific properties in the form:
1376 // arch: {
1377 // arm: {
1378 // variant: {
1379 // key: value,
1380 // },
1381 // },
1382 // },
1383 v := variantReplacer.Replace(arch.ArchVariant)
1384 if v != "" {
1385 prefix := "arch." + archType.Name + "." + v
1386 if variantProperties, ok := getChildPropertyStruct(ctx, archStruct, v, prefix); ok {
1387 result = append(result, variantProperties)
1388 }
1389 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001390
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001391 // Handle cpu-variant-specific properties in the form:
1392 // arch: {
1393 // arm: {
1394 // variant: {
1395 // key: value,
1396 // },
1397 // },
1398 // },
1399 if arch.CpuVariant != arch.ArchVariant {
1400 c := variantReplacer.Replace(arch.CpuVariant)
1401 if c != "" {
1402 prefix := "arch." + archType.Name + "." + c
1403 if cpuVariantProperties, ok := getChildPropertyStruct(ctx, archStruct, c, prefix); ok {
1404 result = append(result, cpuVariantProperties)
1405 }
1406 }
1407 }
1408
1409 // Handle arch-feature-specific properties in the form:
1410 // arch: {
1411 // arm: {
1412 // feature: {
1413 // key: value,
1414 // },
1415 // },
1416 // },
1417 for _, feature := range arch.ArchFeatures {
1418 prefix := "arch." + archType.Name + "." + feature
1419 if featureProperties, ok := getChildPropertyStruct(ctx, archStruct, feature, prefix); ok {
1420 result = append(result, featureProperties)
1421 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001422 }
1423 }
1424
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001425 if multilibProperties, ok := getMultilibStruct(ctx, archProperties, archType); ok {
1426 result = append(result, multilibProperties)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001427 }
1428
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001429 // Handle combined OS-feature and arch specific properties in the form:
1430 // target: {
1431 // bionic_x86: {
1432 // key: value,
1433 // },
1434 // }
1435 if os.Linux() {
1436 field := "Linux_" + arch.ArchType.Name
1437 userFriendlyField := "target.linux_" + arch.ArchType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001438 if linuxProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1439 result = append(result, linuxProperties)
1440 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001441 }
1442
1443 if os.Bionic() {
1444 field := "Bionic_" + archType.Name
1445 userFriendlyField := "target.bionic_" + archType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001446 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1447 result = append(result, bionicProperties)
1448 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001449 }
1450
1451 // Handle combined OS and arch specific properties in the form:
1452 // target: {
1453 // linux_glibc_x86: {
1454 // key: value,
1455 // },
1456 // linux_glibc_arm: {
1457 // key: value,
1458 // },
1459 // android_arm {
1460 // key: value,
1461 // },
1462 // android_x86 {
1463 // key: value,
1464 // },
1465 // },
Liz Kammer9abd62d2021-05-21 08:37:59 -04001466 field := GetCompoundTargetField(os, archType)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001467 userFriendlyField := "target." + os.Name + "_" + archType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001468 if osArchProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1469 result = append(result, osArchProperties)
1470 }
Colin Cross528d67e2021-07-23 22:23:07 +00001471
Colin Cross1aa45b02022-02-10 10:33:10 -08001472 if os == Linux {
1473 field := "Glibc_" + archType.Name
1474 userFriendlyField := "target.glibc_" + "_" + archType.Name
1475 if osArchProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1476 result = append(result, osArchProperties)
1477 }
1478 }
1479
Colin Cross528d67e2021-07-23 22:23:07 +00001480 if os == LinuxMusl {
Colin Cross1aa45b02022-02-10 10:33:10 -08001481 field := "Musl_" + archType.Name
1482 userFriendlyField := "target.musl_" + "_" + archType.Name
1483 if osArchProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1484 result = append(result, osArchProperties)
1485 }
Colin Cross528d67e2021-07-23 22:23:07 +00001486 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001487 }
1488
1489 // Handle arm on x86 properties in the form:
1490 // target {
1491 // arm_on_x86 {
1492 // key: value,
1493 // },
1494 // arm_on_x86_64 {
1495 // key: value,
1496 // },
1497 // },
1498 if os.Class == Device {
1499 if arch.ArchType == X86 && (hasArmAbi(arch) ||
1500 hasArmAndroidArch(ctx.Config().Targets[Android])) {
1501 field := "Arm_on_x86"
1502 userFriendlyField := "target.arm_on_x86"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001503 if armOnX86Properties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1504 result = append(result, armOnX86Properties)
1505 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001506 }
1507 if arch.ArchType == X86_64 && (hasArmAbi(arch) ||
1508 hasArmAndroidArch(ctx.Config().Targets[Android])) {
1509 field := "Arm_on_x86_64"
1510 userFriendlyField := "target.arm_on_x86_64"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001511 if armOnX8664Properties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1512 result = append(result, armOnX8664Properties)
1513 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001514 }
1515 if os == Android && nativeBridgeEnabled {
1516 userFriendlyField := "Native_bridge"
1517 prefix := "target.native_bridge"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001518 if nativeBridgeProperties, ok := getChildPropertyStruct(ctx, targetProp, userFriendlyField, prefix); ok {
1519 result = append(result, nativeBridgeProperties)
1520 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001521 }
1522 }
1523
1524 return result
1525}
1526
Colin Crossa6845402020-11-16 15:08:19 -08001527// Squash the appropriate arch-specific property structs into the matching top level property
1528// structs based on the CompileTarget value that was annotated on the variant.
Colin Cross4157e882019-06-06 16:57:04 -07001529func (m *ModuleBase) setArchProperties(ctx BottomUpMutatorContext) {
1530 arch := m.Arch()
1531 os := m.Os()
Colin Crossd3ba0392015-05-07 14:11:29 -07001532
Ustadca02192021-12-20 12:56:46 -05001533 for i := range m.archProperties {
Usta851a3272022-01-05 23:42:33 -05001534 genProps := m.GetProperties()[i]
Colin Cross4157e882019-06-06 16:57:04 -07001535 if m.archProperties[i] == nil {
Dan Willemsenb1957a52016-06-23 23:44:54 -07001536 continue
1537 }
Dan Willemsenb1957a52016-06-23 23:44:54 -07001538
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001539 propStructs := make([]reflect.Value, 0)
1540 for _, archProperty := range m.archProperties[i] {
1541 propStructShard := getArchProperties(ctx, archProperty, arch, os, m.Target().NativeBridge == NativeBridgeEnabled)
1542 propStructs = append(propStructs, propStructShard...)
1543 }
Dan Willemsenb1957a52016-06-23 23:44:54 -07001544
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001545 for _, propStruct := range propStructs {
1546 mergePropertyStruct(ctx, genProps, propStruct)
Colin Crossbb2e2b72016-12-08 17:23:53 -08001547 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001548 }
1549}
1550
Colin Cross0c66bc62021-07-20 09:47:41 -07001551// determineBuildOS stores the OS and architecture used for host targets used during the build into
Colin Cross528d67e2021-07-23 22:23:07 +00001552// config based on the runtime OS and architecture determined by Go and the product configuration.
Colin Cross0c66bc62021-07-20 09:47:41 -07001553func determineBuildOS(config *config) {
1554 config.BuildOS = func() OsType {
1555 switch runtime.GOOS {
1556 case "linux":
Colin Cross528d67e2021-07-23 22:23:07 +00001557 if Bool(config.productVariables.HostMusl) {
1558 return LinuxMusl
1559 }
Colin Cross0c66bc62021-07-20 09:47:41 -07001560 return Linux
1561 case "darwin":
1562 return Darwin
1563 default:
1564 panic(fmt.Sprintf("unsupported OS: %s", runtime.GOOS))
1565 }
1566 }()
1567
1568 config.BuildArch = func() ArchType {
1569 switch runtime.GOARCH {
1570 case "amd64":
1571 return X86_64
1572 default:
1573 panic(fmt.Sprintf("unsupported Arch: %s", runtime.GOARCH))
1574 }
1575 }()
1576
1577}
1578
Colin Crossa6845402020-11-16 15:08:19 -08001579// Convert the arch product variables into a list of targets for each OsType.
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001580func decodeTargetProductVariables(config *config) (map[OsType][]Target, error) {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001581 variables := config.productVariables
Dan Willemsen490fd492015-11-24 17:53:15 -08001582
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001583 targets := make(map[OsType][]Target)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001584 var targetErr error
1585
Liz Kammerb7f33662022-02-28 14:16:16 -05001586 type targetConfig struct {
1587 os OsType
1588 archName string
1589 archVariant *string
1590 cpuVariant *string
1591 abi []string
1592 nativeBridgeEnabled NativeBridgeSupport
1593 nativeBridgeHostArchName *string
1594 nativeBridgeRelativePath *string
1595 }
1596
1597 addTarget := func(target targetConfig) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001598 if targetErr != nil {
1599 return
Dan Willemsen490fd492015-11-24 17:53:15 -08001600 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001601
Liz Kammerb7f33662022-02-28 14:16:16 -05001602 arch, err := decodeArch(target.os, target.archName, target.archVariant, target.cpuVariant, target.abi)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001603 if err != nil {
1604 targetErr = err
1605 return
1606 }
Liz Kammerb7f33662022-02-28 14:16:16 -05001607 nativeBridgeRelativePathStr := String(target.nativeBridgeRelativePath)
1608 nativeBridgeHostArchNameStr := String(target.nativeBridgeHostArchName)
dimitry8d6dde82019-07-11 10:23:53 +02001609
1610 // Use guest arch as relative install path by default
Liz Kammerb7f33662022-02-28 14:16:16 -05001611 if target.nativeBridgeEnabled && nativeBridgeRelativePathStr == "" {
dimitry8d6dde82019-07-11 10:23:53 +02001612 nativeBridgeRelativePathStr = arch.ArchType.String()
1613 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001614
Jiyong Park1613e552020-09-14 19:43:17 +09001615 // A target is considered as HostCross if it's a host target which can't run natively on
1616 // the currently configured build machine (either because the OS is different or because of
1617 // the unsupported arch)
1618 hostCross := false
Liz Kammerb7f33662022-02-28 14:16:16 -05001619 if target.os.Class == Host {
Jiyong Park1613e552020-09-14 19:43:17 +09001620 var osSupported bool
Liz Kammerb7f33662022-02-28 14:16:16 -05001621 if target.os == config.BuildOS {
Jiyong Park1613e552020-09-14 19:43:17 +09001622 osSupported = true
Liz Kammerb7f33662022-02-28 14:16:16 -05001623 } else if config.BuildOS.Linux() && target.os.Linux() {
Jiyong Park1613e552020-09-14 19:43:17 +09001624 // LinuxBionic and Linux are compatible
1625 osSupported = true
1626 } else {
1627 osSupported = false
1628 }
1629
1630 var archSupported bool
1631 if arch.ArchType == Common {
1632 archSupported = true
1633 } else if arch.ArchType.Name == *variables.HostArch {
1634 archSupported = true
1635 } else if variables.HostSecondaryArch != nil && arch.ArchType.Name == *variables.HostSecondaryArch {
1636 archSupported = true
1637 } else {
1638 archSupported = false
1639 }
1640 if !osSupported || !archSupported {
1641 hostCross = true
1642 }
1643 }
1644
Liz Kammerb7f33662022-02-28 14:16:16 -05001645 targets[target.os] = append(targets[target.os],
Colin Crossa1ad8d12016-06-01 17:09:44 -07001646 Target{
Liz Kammerb7f33662022-02-28 14:16:16 -05001647 Os: target.os,
dimitry8d6dde82019-07-11 10:23:53 +02001648 Arch: arch,
Liz Kammerb7f33662022-02-28 14:16:16 -05001649 NativeBridge: target.nativeBridgeEnabled,
dimitry8d6dde82019-07-11 10:23:53 +02001650 NativeBridgeHostArchName: nativeBridgeHostArchNameStr,
1651 NativeBridgeRelativePath: nativeBridgeRelativePathStr,
Jiyong Park1613e552020-09-14 19:43:17 +09001652 HostCross: hostCross,
Colin Crossa1ad8d12016-06-01 17:09:44 -07001653 })
Dan Willemsen490fd492015-11-24 17:53:15 -08001654 }
1655
Colin Cross4225f652015-09-17 14:33:42 -07001656 if variables.HostArch == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001657 return nil, fmt.Errorf("No host primary architecture set")
Colin Cross4225f652015-09-17 14:33:42 -07001658 }
1659
Colin Crossa6845402020-11-16 15:08:19 -08001660 // The primary host target, which must always exist.
Liz Kammerb7f33662022-02-28 14:16:16 -05001661 addTarget(targetConfig{os: config.BuildOS, archName: *variables.HostArch, nativeBridgeEnabled: NativeBridgeDisabled})
Colin Cross4225f652015-09-17 14:33:42 -07001662
Colin Crossa6845402020-11-16 15:08:19 -08001663 // An optional secondary host target.
Colin Crosseeabb892015-11-20 13:07:51 -08001664 if variables.HostSecondaryArch != nil && *variables.HostSecondaryArch != "" {
Liz Kammerb7f33662022-02-28 14:16:16 -05001665 addTarget(targetConfig{os: config.BuildOS, archName: *variables.HostSecondaryArch, nativeBridgeEnabled: NativeBridgeDisabled})
Dan Willemsen490fd492015-11-24 17:53:15 -08001666 }
1667
Colin Crossa6845402020-11-16 15:08:19 -08001668 // Optional cross-compiled host targets, generally Windows.
Colin Crossff3ae9d2018-04-10 16:15:18 -07001669 if String(variables.CrossHost) != "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001670 crossHostOs := osByName(*variables.CrossHost)
1671 if crossHostOs == NoOsType {
1672 return nil, fmt.Errorf("Unknown cross host OS %q", *variables.CrossHost)
1673 }
1674
Colin Crossff3ae9d2018-04-10 16:15:18 -07001675 if String(variables.CrossHostArch) == "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001676 return nil, fmt.Errorf("No cross-host primary architecture set")
Dan Willemsen490fd492015-11-24 17:53:15 -08001677 }
1678
Colin Crossa6845402020-11-16 15:08:19 -08001679 // The primary cross-compiled host target.
Liz Kammerb7f33662022-02-28 14:16:16 -05001680 addTarget(targetConfig{os: crossHostOs, archName: *variables.CrossHostArch, nativeBridgeEnabled: NativeBridgeDisabled})
Dan Willemsen490fd492015-11-24 17:53:15 -08001681
Colin Crossa6845402020-11-16 15:08:19 -08001682 // An optional secondary cross-compiled host target.
Dan Willemsen490fd492015-11-24 17:53:15 -08001683 if variables.CrossHostSecondaryArch != nil && *variables.CrossHostSecondaryArch != "" {
Liz Kammerb7f33662022-02-28 14:16:16 -05001684 addTarget(targetConfig{os: crossHostOs, archName: *variables.CrossHostSecondaryArch, nativeBridgeEnabled: NativeBridgeDisabled})
Dan Willemsen490fd492015-11-24 17:53:15 -08001685 }
1686 }
1687
Colin Crossa6845402020-11-16 15:08:19 -08001688 // Optional device targets
Dan Willemsen3f32f032016-07-11 14:36:48 -07001689 if variables.DeviceArch != nil && *variables.DeviceArch != "" {
Colin Crossa6845402020-11-16 15:08:19 -08001690 // The primary device target.
Liz Kammerb7f33662022-02-28 14:16:16 -05001691 addTarget(targetConfig{
1692 os: Android,
1693 archName: *variables.DeviceArch,
1694 archVariant: variables.DeviceArchVariant,
1695 cpuVariant: variables.DeviceCpuVariant,
1696 abi: variables.DeviceAbi,
1697 nativeBridgeEnabled: NativeBridgeDisabled,
1698 })
Colin Cross4225f652015-09-17 14:33:42 -07001699
Colin Crossa6845402020-11-16 15:08:19 -08001700 // An optional secondary device target.
Dan Willemsen3f32f032016-07-11 14:36:48 -07001701 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" {
Liz Kammerb7f33662022-02-28 14:16:16 -05001702 addTarget(targetConfig{
1703 os: Android,
1704 archName: *variables.DeviceSecondaryArch,
1705 archVariant: variables.DeviceSecondaryArchVariant,
1706 cpuVariant: variables.DeviceSecondaryCpuVariant,
1707 abi: variables.DeviceSecondaryAbi,
1708 nativeBridgeEnabled: NativeBridgeDisabled,
1709 })
Colin Cross4225f652015-09-17 14:33:42 -07001710 }
dimitry1f33e402019-03-26 12:39:31 +01001711
Colin Crossa6845402020-11-16 15:08:19 -08001712 // An optional NativeBridge device target.
dimitry1f33e402019-03-26 12:39:31 +01001713 if variables.NativeBridgeArch != nil && *variables.NativeBridgeArch != "" {
Liz Kammerb7f33662022-02-28 14:16:16 -05001714 addTarget(targetConfig{
1715 os: Android,
1716 archName: *variables.NativeBridgeArch,
1717 archVariant: variables.NativeBridgeArchVariant,
1718 cpuVariant: variables.NativeBridgeCpuVariant,
1719 abi: variables.NativeBridgeAbi,
1720 nativeBridgeEnabled: NativeBridgeEnabled,
1721 nativeBridgeHostArchName: variables.DeviceArch,
1722 nativeBridgeRelativePath: variables.NativeBridgeRelativePath,
1723 })
dimitry1f33e402019-03-26 12:39:31 +01001724 }
1725
Colin Crossa6845402020-11-16 15:08:19 -08001726 // An optional secondary NativeBridge device target.
dimitry1f33e402019-03-26 12:39:31 +01001727 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" &&
1728 variables.NativeBridgeSecondaryArch != nil && *variables.NativeBridgeSecondaryArch != "" {
Liz Kammerb7f33662022-02-28 14:16:16 -05001729 addTarget(targetConfig{
1730 os: Android,
1731 archName: *variables.NativeBridgeSecondaryArch,
1732 archVariant: variables.NativeBridgeSecondaryArchVariant,
1733 cpuVariant: variables.NativeBridgeSecondaryCpuVariant,
1734 abi: variables.NativeBridgeSecondaryAbi,
1735 nativeBridgeEnabled: NativeBridgeEnabled,
1736 nativeBridgeHostArchName: variables.DeviceSecondaryArch,
1737 nativeBridgeRelativePath: variables.NativeBridgeSecondaryRelativePath,
1738 })
dimitry1f33e402019-03-26 12:39:31 +01001739 }
Colin Cross4225f652015-09-17 14:33:42 -07001740 }
1741
Colin Crossa1ad8d12016-06-01 17:09:44 -07001742 if targetErr != nil {
1743 return nil, targetErr
1744 }
1745
1746 return targets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001747}
1748
Colin Crossbb2e2b72016-12-08 17:23:53 -08001749// hasArmAbi returns true if arch has at least one arm ABI
1750func hasArmAbi(arch Arch) bool {
Jaewoong Jung3aff5782020-02-11 07:54:35 -08001751 return PrefixInList(arch.Abi, "arm")
Colin Crossbb2e2b72016-12-08 17:23:53 -08001752}
1753
Lev Rumyantsev34581212021-10-13 09:47:59 -07001754// hasArmAndroidArch returns true if targets has at least
1755// one arm Android arch (possibly native bridged)
Colin Cross4247f0d2017-04-13 16:56:14 -07001756func hasArmAndroidArch(targets []Target) bool {
1757 for _, target := range targets {
Lev Rumyantsev34581212021-10-13 09:47:59 -07001758 if target.Os == Android &&
1759 (target.Arch.ArchType == Arm || target.Arch.ArchType == Arm64) {
Victor Khimenko5eb8ec12018-03-21 20:30:54 +01001760 return true
1761 }
1762 }
1763 return false
1764}
1765
Colin Crossa6845402020-11-16 15:08:19 -08001766// archConfig describes a built-in configuration.
Dan Albert4098deb2016-10-19 14:04:41 -07001767type archConfig struct {
Liz Kammer992918d2022-11-11 10:37:54 -05001768 Arch string `json:"arch"`
1769 ArchVariant string `json:"arch_variant"`
1770 CpuVariant string `json:"cpu_variant"`
1771 Abi []string `json:"abis"`
Dan Albert4098deb2016-10-19 14:04:41 -07001772}
1773
Elliott Hughesc55b5862022-10-27 23:46:22 +00001774// getNdkAbisConfig returns the list of archConfigs that are used for building
1775// the API stubs and static libraries that are included in the NDK.
Dan Albert4098deb2016-10-19 14:04:41 -07001776func getNdkAbisConfig() []archConfig {
1777 return []archConfig{
Tamas Petzbca786d2021-01-20 18:56:33 +01001778 {"arm64", "armv8-a-branchprot", "", []string{"arm64-v8a"}},
Elliott Hughesc55b5862022-10-27 23:46:22 +00001779 {"arm", "armv7-a-neon", "", []string{"armeabi-v7a"}},
Elliott Hughesf7d31092023-03-14 23:11:57 +00001780 {"riscv64", "", "", []string{"riscv64"}},
Dan Albert4098deb2016-10-19 14:04:41 -07001781 {"x86_64", "", "", []string{"x86_64"}},
Inseob Kim5219c0e2021-06-17 00:33:00 +09001782 {"x86", "", "", []string{"x86"}},
Dan Albert4098deb2016-10-19 14:04:41 -07001783 }
1784}
1785
Colin Crossa6845402020-11-16 15:08:19 -08001786// getAmlAbisConfig returns a list of archConfigs for the ABIs supported by mainline modules.
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001787func getAmlAbisConfig() []archConfig {
1788 return []archConfig{
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001789 {"arm64", "armv8-a", "", []string{"arm64-v8a"}},
Inseob Kim5219c0e2021-06-17 00:33:00 +09001790 {"arm", "armv7-a-neon", "", []string{"armeabi-v7a"}},
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001791 {"x86_64", "", "", []string{"x86_64"}},
Inseob Kim5219c0e2021-06-17 00:33:00 +09001792 {"x86", "", "", []string{"x86"}},
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001793 }
1794}
1795
Colin Crossa6845402020-11-16 15:08:19 -08001796// decodeArchSettings converts a list of archConfigs into a list of Targets for the given OsType.
Liz Kammerb7f33662022-02-28 14:16:16 -05001797func decodeAndroidArchSettings(archConfigs []archConfig) ([]Target, error) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001798 var ret []Target
Dan Willemsen322acaf2016-01-12 23:07:05 -08001799
Dan Albert4098deb2016-10-19 14:04:41 -07001800 for _, config := range archConfigs {
Liz Kammer992918d2022-11-11 10:37:54 -05001801 arch, err := decodeArch(Android, config.Arch, &config.ArchVariant,
1802 &config.CpuVariant, config.Abi)
Dan Willemsen322acaf2016-01-12 23:07:05 -08001803 if err != nil {
1804 return nil, err
1805 }
Colin Cross3b19f5d2019-09-17 14:45:31 -07001806
Colin Crossa1ad8d12016-06-01 17:09:44 -07001807 ret = append(ret, Target{
1808 Os: Android,
1809 Arch: arch,
1810 })
Dan Willemsen322acaf2016-01-12 23:07:05 -08001811 }
1812
1813 return ret, nil
1814}
1815
Colin Crossa6845402020-11-16 15:08:19 -08001816// decodeArch converts a set of strings from product variables into an Arch struct.
Colin Crossa74ca042019-01-31 14:31:51 -08001817func decodeArch(os OsType, arch string, archVariant, cpuVariant *string, abi []string) (Arch, error) {
Colin Crossa6845402020-11-16 15:08:19 -08001818 // Verify the arch is valid
Colin Crosseeabb892015-11-20 13:07:51 -08001819 archType, ok := archTypeMap[arch]
1820 if !ok {
1821 return Arch{}, fmt.Errorf("unknown arch %q", arch)
1822 }
Colin Cross4225f652015-09-17 14:33:42 -07001823
Colin Crosseeabb892015-11-20 13:07:51 -08001824 a := Arch{
Colin Cross4225f652015-09-17 14:33:42 -07001825 ArchType: archType,
Colin Crossa6845402020-11-16 15:08:19 -08001826 ArchVariant: String(archVariant),
1827 CpuVariant: String(cpuVariant),
Colin Crossa74ca042019-01-31 14:31:51 -08001828 Abi: abi,
Colin Crosseeabb892015-11-20 13:07:51 -08001829 }
1830
Colin Crossa6845402020-11-16 15:08:19 -08001831 // Convert generic arch variants into the empty string.
Colin Crosseeabb892015-11-20 13:07:51 -08001832 if a.ArchVariant == a.ArchType.Name || a.ArchVariant == "generic" {
1833 a.ArchVariant = ""
1834 }
1835
Colin Crossa6845402020-11-16 15:08:19 -08001836 // Convert generic CPU variants into the empty string.
Colin Crosseeabb892015-11-20 13:07:51 -08001837 if a.CpuVariant == a.ArchType.Name || a.CpuVariant == "generic" {
1838 a.CpuVariant = ""
1839 }
1840
Liz Kammer2c2afe22022-02-11 11:35:03 -05001841 if a.ArchVariant != "" {
1842 if validArchVariants := archVariants[archType]; !InList(a.ArchVariant, validArchVariants) {
1843 return Arch{}, fmt.Errorf("[%q] unknown arch variant %q, support variants: %q", archType, a.ArchVariant, validArchVariants)
1844 }
1845 }
1846
1847 if a.CpuVariant != "" {
1848 if validCpuVariants := cpuVariants[archType]; !InList(a.CpuVariant, validCpuVariants) {
1849 return Arch{}, fmt.Errorf("[%q] unknown cpu variant %q, support variants: %q", archType, a.CpuVariant, validCpuVariants)
1850 }
1851 }
1852
Colin Crossa6845402020-11-16 15:08:19 -08001853 // Filter empty ABIs out of the list.
Colin Crosseeabb892015-11-20 13:07:51 -08001854 for i := 0; i < len(a.Abi); i++ {
1855 if a.Abi[i] == "" {
1856 a.Abi = append(a.Abi[:i], a.Abi[i+1:]...)
1857 i--
1858 }
1859 }
1860
Liz Kammere8303bd2022-02-16 09:02:48 -05001861 // Set ArchFeatures from the arch type. for Android OS, other os-es do not specify features
1862 if os == Android {
1863 if featureMap, ok := androidArchFeatureMap[archType]; ok {
Dan Willemsen01a3c252019-01-11 19:02:16 -08001864 a.ArchFeatures = featureMap[a.ArchVariant]
1865 }
Colin Crossc5c24ad2015-11-20 15:35:00 -08001866 }
1867
Colin Crosseeabb892015-11-20 13:07:51 -08001868 return a, nil
Colin Cross4225f652015-09-17 14:33:42 -07001869}
1870
Colin Crossa6845402020-11-16 15:08:19 -08001871// filterMultilibTargets takes a list of Targets and a multilib value and returns a new list of
1872// Targets containing only those that have the given multilib value.
Colin Cross69617d32016-09-06 10:39:07 -07001873func filterMultilibTargets(targets []Target, multilib string) []Target {
1874 var ret []Target
1875 for _, t := range targets {
1876 if t.Arch.ArchType.Multilib == multilib {
1877 ret = append(ret, t)
1878 }
1879 }
1880 return ret
1881}
1882
Colin Crossa6845402020-11-16 15:08:19 -08001883// getCommonTargets returns the set of Os specific common architecture targets for each Os in a list
1884// of targets.
Nan Zhangdb0b9a32017-02-27 10:12:13 -08001885func getCommonTargets(targets []Target) []Target {
1886 var ret []Target
1887 set := make(map[string]bool)
1888
1889 for _, t := range targets {
1890 if _, found := set[t.Os.String()]; !found {
1891 set[t.Os.String()] = true
Colin Cross39a18142022-06-24 18:43:40 -07001892 common := commonTargetMap[t.Os.String()]
1893 common.HostCross = t.HostCross
1894 ret = append(ret, common)
Nan Zhangdb0b9a32017-02-27 10:12:13 -08001895 }
1896 }
1897
1898 return ret
1899}
1900
Sam Delmericocc271e22022-06-01 15:45:02 +00001901// 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 -07001902// that contains zero or one Target for each OsType and HostCross, selecting the one that matches
1903// the earliest filter.
Sam Delmericocc271e22022-06-01 15:45:02 +00001904func FirstTarget(targets []Target, filters ...string) []Target {
Jiyong Park22101982020-09-17 19:09:58 +09001905 // find the first target from each OS
1906 var ret []Target
Colin Crossc0f0eb82022-07-19 14:41:11 -07001907 type osHostCross struct {
1908 os OsType
1909 hostCross bool
1910 }
1911 set := make(map[osHostCross]bool)
Jiyong Park22101982020-09-17 19:09:58 +09001912
Colin Cross6b4a32d2017-12-05 13:42:45 -08001913 for _, filter := range filters {
1914 buildTargets := filterMultilibTargets(targets, filter)
Jiyong Park22101982020-09-17 19:09:58 +09001915 for _, t := range buildTargets {
Colin Crossc0f0eb82022-07-19 14:41:11 -07001916 key := osHostCross{t.Os, t.HostCross}
1917 if _, found := set[key]; !found {
1918 set[key] = true
Jiyong Park22101982020-09-17 19:09:58 +09001919 ret = append(ret, t)
1920 }
Colin Cross6b4a32d2017-12-05 13:42:45 -08001921 }
1922 }
Jiyong Park22101982020-09-17 19:09:58 +09001923 return ret
Colin Cross6b4a32d2017-12-05 13:42:45 -08001924}
1925
Colin Crossa6845402020-11-16 15:08:19 -08001926// decodeMultilibTargets uses the module's multilib setting to select one or more targets from a
1927// list of Targets.
Colin Crossee0bc3b2018-10-02 22:01:37 -07001928func decodeMultilibTargets(multilib string, targets []Target, prefer32 bool) ([]Target, error) {
Colin Crossa6845402020-11-16 15:08:19 -08001929 var buildTargets []Target
Colin Cross6b4a32d2017-12-05 13:42:45 -08001930
Colin Cross4225f652015-09-17 14:33:42 -07001931 switch multilib {
1932 case "common":
Colin Cross6b4a32d2017-12-05 13:42:45 -08001933 buildTargets = getCommonTargets(targets)
1934 case "common_first":
1935 buildTargets = getCommonTargets(targets)
1936 if prefer32 {
Sam Delmericocc271e22022-06-01 15:45:02 +00001937 buildTargets = append(buildTargets, FirstTarget(targets, "lib32", "lib64")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001938 } else {
Sam Delmericocc271e22022-06-01 15:45:02 +00001939 buildTargets = append(buildTargets, FirstTarget(targets, "lib64", "lib32")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001940 }
Colin Cross4225f652015-09-17 14:33:42 -07001941 case "both":
Colin Cross8b74d172016-09-13 09:59:14 -07001942 if prefer32 {
1943 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1944 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1945 } else {
1946 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1947 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1948 }
Colin Cross4225f652015-09-17 14:33:42 -07001949 case "32":
Colin Cross69617d32016-09-06 10:39:07 -07001950 buildTargets = filterMultilibTargets(targets, "lib32")
Colin Cross4225f652015-09-17 14:33:42 -07001951 case "64":
Colin Cross69617d32016-09-06 10:39:07 -07001952 buildTargets = filterMultilibTargets(targets, "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001953 case "first":
1954 if prefer32 {
Sam Delmericocc271e22022-06-01 15:45:02 +00001955 buildTargets = FirstTarget(targets, "lib32", "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001956 } else {
Sam Delmericocc271e22022-06-01 15:45:02 +00001957 buildTargets = FirstTarget(targets, "lib64", "lib32")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001958 }
Victor Chang9448e8f2020-09-14 15:34:16 +01001959 case "first_prefer32":
Sam Delmericocc271e22022-06-01 15:45:02 +00001960 buildTargets = FirstTarget(targets, "lib32", "lib64")
Colin Cross69617d32016-09-06 10:39:07 -07001961 case "prefer32":
Colin Cross3dceee32018-09-06 10:19:57 -07001962 buildTargets = filterMultilibTargets(targets, "lib32")
1963 if len(buildTargets) == 0 {
1964 buildTargets = filterMultilibTargets(targets, "lib64")
1965 }
Dan Willemsen47450072021-10-19 20:24:49 -07001966 case "darwin_universal":
1967 buildTargets = filterMultilibTargets(targets, "lib64")
1968 // Reverse the targets so that the first architecture can depend on the second
1969 // architecture module in order to merge the outputs.
Colin Crossb5e3f7d2023-07-06 15:37:53 -07001970 ReverseSliceInPlace(buildTargets)
Dan Willemsen47450072021-10-19 20:24:49 -07001971 case "darwin_universal_common_first":
1972 archTargets := filterMultilibTargets(targets, "lib64")
Colin Crossb5e3f7d2023-07-06 15:37:53 -07001973 ReverseSliceInPlace(archTargets)
Dan Willemsen47450072021-10-19 20:24:49 -07001974 buildTargets = append(getCommonTargets(targets), archTargets...)
Colin Cross4225f652015-09-17 14:33:42 -07001975 default:
Victor Chang9448e8f2020-09-14 15:34:16 +01001976 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 -07001977 multilib)
Colin Cross4225f652015-09-17 14:33:42 -07001978 }
1979
Colin Crossa1ad8d12016-06-01 17:09:44 -07001980 return buildTargets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001981}
Jingwen Chen5d864492021-02-24 07:20:12 -05001982
Liz Kammerb6dbc872021-05-14 15:14:40 -04001983// ArchVariantContext defines the limited context necessary to retrieve arch_variant properties.
1984type ArchVariantContext interface {
1985 ModuleErrorf(fmt string, args ...interface{})
1986 PropertyErrorf(property, fmt string, args ...interface{})
1987}