blob: 8aa8d4043d32506d67d523b6d7891a8e95786192 [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"
22 "strings"
Colin Crossf6566ed2015-03-24 11:13:38 -070023
Colin Crosscb0ac952021-07-20 13:17:15 -070024 "android/soong/bazel"
Liz Kammere8303bd2022-02-16 09:02:48 -050025 "android/soong/starlark_fmt"
Colin Crosscb0ac952021-07-20 13:17:15 -070026
Colin Cross0f7d2ef2019-10-16 11:03:10 -070027 "github.com/google/blueprint"
Colin Cross617b88a2020-08-24 18:04:09 -070028 "github.com/google/blueprint/bootstrap"
Colin Crossf6566ed2015-03-24 11:13:38 -070029 "github.com/google/blueprint/proptools"
Colin Cross3f40fa42015-01-30 17:27:36 -080030)
31
Colin Cross3f40fa42015-01-30 17:27:36 -080032/*
33Example blueprints file containing all variant property groups, with comment listing what type
34of variants get properties in that group:
35
36module {
37 arch: {
38 arm: {
39 // Host or device variants with arm architecture
40 },
41 arm64: {
42 // Host or device variants with arm64 architecture
43 },
Colin Cross3f40fa42015-01-30 17:27:36 -080044 x86: {
45 // Host or device variants with x86 architecture
46 },
47 x86_64: {
48 // Host or device variants with x86_64 architecture
49 },
50 },
51 multilib: {
52 lib32: {
53 // Host or device variants for 32-bit architectures
54 },
55 lib64: {
56 // Host or device variants for 64-bit architectures
57 },
58 },
59 target: {
60 android: {
Martin Stjernholme284b482020-09-23 21:03:27 +010061 // Device variants (implies Bionic)
Colin Cross3f40fa42015-01-30 17:27:36 -080062 },
63 host: {
64 // Host variants
65 },
Martin Stjernholme284b482020-09-23 21:03:27 +010066 bionic: {
67 // Bionic (device and host) variants
68 },
69 linux_bionic: {
70 // Bionic host variants
71 },
72 linux: {
73 // Bionic (device and host) and Linux glibc variants
74 },
Dan Willemsen5746bd42017-10-02 19:42:01 -070075 linux_glibc: {
Martin Stjernholme284b482020-09-23 21:03:27 +010076 // Linux host variants (using non-Bionic libc)
Colin Cross3f40fa42015-01-30 17:27:36 -080077 },
78 darwin: {
79 // Darwin host variants
80 },
81 windows: {
82 // Windows host variants
83 },
84 not_windows: {
85 // Non-windows host variants
86 },
Martin Stjernholme284b482020-09-23 21:03:27 +010087 android_arm: {
88 // Any <os>_<arch> combination restricts to that os and arch
89 },
Colin Cross3f40fa42015-01-30 17:27:36 -080090 },
91}
92*/
Colin Cross7d5136f2015-05-11 13:39:40 -070093
Colin Cross3f40fa42015-01-30 17:27:36 -080094// An Arch indicates a single CPU architecture.
95type Arch struct {
Colin Crossa6845402020-11-16 15:08:19 -080096 // The type of the architecture (arm, arm64, x86, or x86_64).
97 ArchType ArchType
98
99 // The variant of the architecture, for example "armv7-a" or "armv7-a-neon" for arm.
100 ArchVariant string
101
102 // The variant of the CPU, for example "cortex-a53" for arm64.
103 CpuVariant string
104
105 // The list of Android app ABIs supported by the CPU architecture, for example "arm64-v8a".
106 Abi []string
107
108 // The list of arch-specific features supported by the CPU architecture, for example "neon".
Colin Crossc5c24ad2015-11-20 15:35:00 -0800109 ArchFeatures []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800110}
111
Colin Crossa6845402020-11-16 15:08:19 -0800112// String returns the Arch as a string. The value is used as the name of the variant created
113// by archMutator.
Colin Cross3f40fa42015-01-30 17:27:36 -0800114func (a Arch) String() string {
Colin Crossd3ba0392015-05-07 14:11:29 -0700115 s := a.ArchType.String()
Colin Cross3f40fa42015-01-30 17:27:36 -0800116 if a.ArchVariant != "" {
117 s += "_" + a.ArchVariant
118 }
119 if a.CpuVariant != "" {
120 s += "_" + a.CpuVariant
121 }
122 return s
123}
124
Colin Crossa6845402020-11-16 15:08:19 -0800125// ArchType is used to define the 4 supported architecture types (arm, arm64, x86, x86_64), as
126// well as the "common" architecture used for modules that support multiple architectures, for
127// example Java modules.
Colin Cross3f40fa42015-01-30 17:27:36 -0800128type ArchType struct {
Colin Crossa6845402020-11-16 15:08:19 -0800129 // Name is the name of the architecture type, "arm", "arm64", "x86", or "x86_64".
130 Name string
131
132 // Field is the name of the field used in properties that refer to the architecture, e.g. "Arm64".
133 Field string
134
135 // Multilib is either "lib32" or "lib64" for 32-bit or 64-bit architectures.
Colin Crossec193632015-07-06 17:49:43 -0700136 Multilib string
Colin Cross3f40fa42015-01-30 17:27:36 -0800137}
138
Colin Crossa6845402020-11-16 15:08:19 -0800139// String returns the name of the ArchType.
140func (a ArchType) String() string {
141 return a.Name
142}
143
144const COMMON_VARIANT = "common"
145
146var (
147 archTypeList []ArchType
148
149 Arm = newArch("arm", "lib32")
150 Arm64 = newArch("arm64", "lib64")
151 X86 = newArch("x86", "lib32")
152 X86_64 = newArch("x86_64", "lib64")
153
154 Common = ArchType{
155 Name: COMMON_VARIANT,
156 }
157)
158
159var archTypeMap = map[string]ArchType{}
160
Colin Crossec193632015-07-06 17:49:43 -0700161func newArch(name, multilib string) ArchType {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700162 archType := ArchType{
Colin Crossec193632015-07-06 17:49:43 -0700163 Name: name,
Dan Willemsenb1957a52016-06-23 23:44:54 -0700164 Field: proptools.FieldNameForProperty(name),
Colin Crossec193632015-07-06 17:49:43 -0700165 Multilib: multilib,
Colin Cross3f40fa42015-01-30 17:27:36 -0800166 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700167 archTypeList = append(archTypeList, archType)
Colin Crossa6845402020-11-16 15:08:19 -0800168 archTypeMap[name] = archType
Dan Willemsenb1957a52016-06-23 23:44:54 -0700169 return archType
Colin Cross3f40fa42015-01-30 17:27:36 -0800170}
171
Ustaeabf0f32021-12-06 15:17:23 -0500172// ArchTypeList returns a slice copy of the 4 supported ArchTypes for arm,
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000173// arm64, x86 and x86_64.
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -0700174func ArchTypeList() []ArchType {
175 return append([]ArchType(nil), archTypeList...)
176}
177
Colin Crossa6845402020-11-16 15:08:19 -0800178// MarshalText allows an ArchType to be serialized through any encoder that supports
179// encoding.TextMarshaler.
Colin Cross74ba9622019-02-11 15:11:14 -0800180func (a ArchType) MarshalText() ([]byte, error) {
Jeongik Chabec4d032021-04-15 08:55:38 +0900181 return []byte(a.String()), nil
Colin Cross74ba9622019-02-11 15:11:14 -0800182}
183
Colin Crossa6845402020-11-16 15:08:19 -0800184var _ encoding.TextMarshaler = ArchType{}
Colin Cross74ba9622019-02-11 15:11:14 -0800185
Colin Crossa6845402020-11-16 15:08:19 -0800186// UnmarshalText allows an ArchType to be deserialized through any decoder that supports
187// encoding.TextUnmarshaler.
Colin Cross74ba9622019-02-11 15:11:14 -0800188func (a *ArchType) UnmarshalText(text []byte) error {
189 if u, ok := archTypeMap[string(text)]; ok {
190 *a = u
191 return nil
192 }
193
194 return fmt.Errorf("unknown ArchType %q", text)
195}
196
Colin Crossa6845402020-11-16 15:08:19 -0800197var _ encoding.TextUnmarshaler = &ArchType{}
Colin Crossa1ad8d12016-06-01 17:09:44 -0700198
Colin Crossa6845402020-11-16 15:08:19 -0800199// OsClass is an enum that describes whether a variant of a module runs on the host, on the device,
200// or is generic.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700201type OsClass int
202
203const (
Colin Crossa6845402020-11-16 15:08:19 -0800204 // Generic is used for variants of modules that are not OS-specific.
Dan Willemsen0e2d97b2016-11-28 17:50:06 -0800205 Generic OsClass = iota
Colin Crossa6845402020-11-16 15:08:19 -0800206 // Device is used for variants of modules that run on the device.
Dan Willemsen0e2d97b2016-11-28 17:50:06 -0800207 Device
Colin Crossa6845402020-11-16 15:08:19 -0800208 // Host is used for variants of modules that run on the host.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700209 Host
Colin Crossa1ad8d12016-06-01 17:09:44 -0700210)
211
Colin Crossa6845402020-11-16 15:08:19 -0800212// String returns the OsClass as a string.
Colin Cross67a5c132017-05-09 13:45:28 -0700213func (class OsClass) String() string {
214 switch class {
215 case Generic:
216 return "generic"
217 case Device:
218 return "device"
219 case Host:
220 return "host"
Colin Cross67a5c132017-05-09 13:45:28 -0700221 default:
222 panic(fmt.Errorf("unknown class %d", class))
223 }
224}
225
Colin Crossa6845402020-11-16 15:08:19 -0800226// OsType describes an OS variant of a module.
227type OsType struct {
228 // Name is the name of the OS. It is also used as the name of the property in Android.bp
229 // files.
230 Name string
231
232 // Field is the name of the OS converted to an exported field name, i.e. with the first
233 // character capitalized.
234 Field string
235
236 // Class is the OsClass of the OS.
237 Class OsClass
238
239 // DefaultDisabled is set when the module variants for the OS should not be created unless
240 // the module explicitly requests them. This is used to limit Windows cross compilation to
241 // only modules that need it.
242 DefaultDisabled bool
243}
244
245// String returns the name of the OsType.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700246func (os OsType) String() string {
247 return os.Name
Colin Cross54c71122016-06-01 17:09:44 -0700248}
249
Colin Crossa6845402020-11-16 15:08:19 -0800250// Bionic returns true if the OS uses the Bionic libc runtime, i.e. if the OS is Android or
251// is Linux with Bionic.
Dan Willemsen866b5632017-09-22 12:28:24 -0700252func (os OsType) Bionic() bool {
253 return os == Android || os == LinuxBionic
254}
255
Colin Crossa6845402020-11-16 15:08:19 -0800256// Linux returns true if the OS uses the Linux kernel, i.e. if the OS is Android or is Linux
257// with or without the Bionic libc runtime.
Dan Willemsen866b5632017-09-22 12:28:24 -0700258func (os OsType) Linux() bool {
Colin Cross528d67e2021-07-23 22:23:07 +0000259 return os == Android || os == Linux || os == LinuxBionic || os == LinuxMusl
Dan Willemsen866b5632017-09-22 12:28:24 -0700260}
261
Colin Crossa6845402020-11-16 15:08:19 -0800262// newOsType constructs an OsType and adds it to the global lists.
263func newOsType(name string, class OsClass, defDisabled bool, archTypes ...ArchType) OsType {
264 checkCalledFromInit()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700265 os := OsType{
266 Name: name,
Colin Crossa6845402020-11-16 15:08:19 -0800267 Field: proptools.FieldNameForProperty(name),
Colin Crossa1ad8d12016-06-01 17:09:44 -0700268 Class: class,
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800269
270 DefaultDisabled: defDisabled,
Colin Cross54c71122016-06-01 17:09:44 -0700271 }
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000272 osTypeList = append(osTypeList, os)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800273
274 if _, found := commonTargetMap[name]; found {
275 panic(fmt.Errorf("Found Os type duplicate during OsType registration: %q", name))
276 } else {
Colin Crosse9fe2942020-11-10 18:12:15 -0800277 commonTargetMap[name] = Target{Os: os, Arch: CommonArch}
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800278 }
Colin Crossa6845402020-11-16 15:08:19 -0800279 osArchTypeMap[os] = archTypes
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800280
Colin Crossa1ad8d12016-06-01 17:09:44 -0700281 return os
282}
283
Colin Crossa6845402020-11-16 15:08:19 -0800284// osByName returns the OsType that has the given name, or NoOsType if none match.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700285func osByName(name string) OsType {
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000286 for _, os := range osTypeList {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700287 if os.Name == name {
288 return os
289 }
290 }
291
292 return NoOsType
Dan Willemsen490fd492015-11-24 17:53:15 -0800293}
294
Colin Crossa6845402020-11-16 15:08:19 -0800295var (
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000296 // osTypeList contains a list of all the supported OsTypes, including ones not supported
Colin Crossa6845402020-11-16 15:08:19 -0800297 // by the current build host or the target device.
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000298 osTypeList []OsType
Colin Crossa6845402020-11-16 15:08:19 -0800299 // commonTargetMap maps names of OsTypes to the corresponding common Target, i.e. the
300 // Target with the same OsType and the common ArchType.
301 commonTargetMap = make(map[string]Target)
302 // osArchTypeMap maps OsTypes to the list of supported ArchTypes for that OS.
303 osArchTypeMap = map[OsType][]ArchType{}
304
305 // NoOsType is a placeholder for when no OS is needed.
306 NoOsType OsType
307 // Linux is the OS for the Linux kernel plus the glibc runtime.
308 Linux = newOsType("linux_glibc", Host, false, X86, X86_64)
Colin Cross528d67e2021-07-23 22:23:07 +0000309 // LinuxMusl is the OS for the Linux kernel plus the musl runtime.
310 LinuxMusl = newOsType("linux_musl", Host, false, X86, X86_64)
Colin Crossa6845402020-11-16 15:08:19 -0800311 // Darwin is the OS for MacOS/Darwin host machines.
Dan Willemsen8528f4e2021-10-19 00:22:06 -0700312 Darwin = newOsType("darwin", Host, false, Arm64, X86_64)
Colin Crossa6845402020-11-16 15:08:19 -0800313 // LinuxBionic is the OS for the Linux kernel plus the Bionic libc runtime, but without the
314 // rest of Android.
315 LinuxBionic = newOsType("linux_bionic", Host, false, Arm64, X86_64)
316 // Windows the OS for Windows host machines.
317 Windows = newOsType("windows", Host, true, X86, X86_64)
318 // Android is the OS for target devices that run all of Android, including the Linux kernel
319 // and the Bionic libc runtime.
320 Android = newOsType("android", Device, false, Arm, Arm64, X86, X86_64)
Colin Crossa6845402020-11-16 15:08:19 -0800321
322 // CommonOS is a pseudo OSType for a common OS variant, which is OsType agnostic and which
323 // has dependencies on all the OS variants.
324 CommonOS = newOsType("common_os", Generic, false)
Colin Crosse9fe2942020-11-10 18:12:15 -0800325
326 // CommonArch is the Arch for all modules that are os-specific but not arch specific,
327 // for example most Java modules.
328 CommonArch = Arch{ArchType: Common}
dimitry1f33e402019-03-26 12:39:31 +0100329)
330
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000331// OsTypeList returns a slice copy of the supported OsTypes.
332func OsTypeList() []OsType {
333 return append([]OsType(nil), osTypeList...)
334}
335
Colin Crossa6845402020-11-16 15:08:19 -0800336// Target specifies the OS and architecture that a module is being compiled for.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700337type Target struct {
Colin Crossa6845402020-11-16 15:08:19 -0800338 // Os the OS that the module is being compiled for (e.g. "linux_glibc", "android").
339 Os OsType
340 // Arch is the architecture that the module is being compiled for.
341 Arch Arch
342 // NativeBridge is NativeBridgeEnabled if the architecture is supported using NativeBridge
343 // (i.e. arm on x86) for this device.
344 NativeBridge NativeBridgeSupport
345 // NativeBridgeHostArchName is the name of the real architecture that is used to implement
346 // the NativeBridge architecture. For example, for arm on x86 this would be "x86".
dimitry8d6dde82019-07-11 10:23:53 +0200347 NativeBridgeHostArchName string
Colin Crossa6845402020-11-16 15:08:19 -0800348 // NativeBridgeRelativePath is the name of the subdirectory that will contain NativeBridge
349 // libraries and binaries.
dimitry8d6dde82019-07-11 10:23:53 +0200350 NativeBridgeRelativePath string
Jiyong Park1613e552020-09-14 19:43:17 +0900351
352 // HostCross is true when the target cannot run natively on the current build host.
353 // For example, linux_glibc_x86 returns true on a regular x86/i686/Linux machines, but returns false
354 // on Mac (different OS), or on 64-bit only i686/Linux machines (unsupported arch).
355 HostCross bool
Colin Crossd3ba0392015-05-07 14:11:29 -0700356}
357
Colin Crossa6845402020-11-16 15:08:19 -0800358// NativeBridgeSupport is an enum that specifies if a Target supports NativeBridge.
359type NativeBridgeSupport bool
360
361const (
362 NativeBridgeDisabled NativeBridgeSupport = false
363 NativeBridgeEnabled NativeBridgeSupport = true
364)
365
366// String returns the OS and arch variations used for the Target.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700367func (target Target) String() string {
Colin Crossa195f912019-10-16 11:07:20 -0700368 return target.OsVariation() + "_" + target.ArchVariation()
369}
370
Colin Crossa6845402020-11-16 15:08:19 -0800371// OsVariation returns the name of the variation used by the osMutator for the Target.
Colin Crossa195f912019-10-16 11:07:20 -0700372func (target Target) OsVariation() string {
373 return target.Os.String()
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700374}
375
Colin Crossa6845402020-11-16 15:08:19 -0800376// ArchVariation returns the name of the variation used by the archMutator for the Target.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700377func (target Target) ArchVariation() string {
378 var variation string
dimitry1f33e402019-03-26 12:39:31 +0100379 if target.NativeBridge {
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700380 variation = "native_bridge_"
dimitry1f33e402019-03-26 12:39:31 +0100381 }
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700382 variation += target.Arch.String()
383
Colin Crossa195f912019-10-16 11:07:20 -0700384 return variation
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700385}
386
Colin Crossa6845402020-11-16 15:08:19 -0800387// Variations returns a list of blueprint.Variations for the osMutator and archMutator for the
388// Target.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700389func (target Target) Variations() []blueprint.Variation {
390 return []blueprint.Variation{
Colin Crossa195f912019-10-16 11:07:20 -0700391 {Mutator: "os", Variation: target.OsVariation()},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700392 {Mutator: "arch", Variation: target.ArchVariation()},
393 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800394}
395
Liz Kammer4562a3b2021-04-21 18:15:34 -0400396func registerBp2buildArchPathDepsMutator(ctx RegisterMutatorsContext) {
397 ctx.BottomUp("bp2build-arch-pathdeps", bp2buildArchPathDepsMutator).Parallel()
398}
399
400// add dependencies for architecture specific properties tagged with `android:"path"`
401func bp2buildArchPathDepsMutator(ctx BottomUpMutatorContext) {
402 var module Module
403 module = ctx.Module()
404
405 m := module.base()
406 if !m.ArchSpecific() {
407 return
408 }
409
410 // addPathDepsForProps does not descend into sub structs, so we need to descend into the
411 // arch-specific properties ourselves
Ustaeabf0f32021-12-06 15:17:23 -0500412 var properties []interface{}
Liz Kammer4562a3b2021-04-21 18:15:34 -0400413 for _, archProperties := range m.archProperties {
414 for _, archProps := range archProperties {
415 archPropValues := reflect.ValueOf(archProps).Elem()
416 // there are three "arch" variations, descend into each
417 for _, variant := range []string{"Arch", "Multilib", "Target"} {
418 // The properties are an interface, get the value (a pointer) that it points to
419 archProps := archPropValues.FieldByName(variant).Elem()
420 if archProps.IsNil() {
421 continue
422 }
423 // And then a pointer to a struct
424 archProps = archProps.Elem()
425 for i := 0; i < archProps.NumField(); i += 1 {
426 f := archProps.Field(i)
427 // If the value of the field is a struct (as opposed to a pointer to a struct) then step
428 // into the BlueprintEmbed field.
429 if f.Kind() == reflect.Struct {
430 f = f.FieldByName("BlueprintEmbed")
431 }
432 if f.IsZero() {
433 continue
434 }
435 props := f.Interface().(interface{})
436 properties = append(properties, props)
437 }
438 }
439 }
440 }
441 addPathDepsForProps(ctx, properties)
442}
443
Colin Crossa6845402020-11-16 15:08:19 -0800444// osMutator splits an arch-specific module into a variant for each OS that is enabled for the
445// module. It uses the HostOrDevice value passed to InitAndroidArchModule and the
446// device_supported and host_supported properties to determine which OsTypes are enabled for this
447// module, then searches through the Targets to determine which have enabled Targets for this
448// module.
Colin Cross617b88a2020-08-24 18:04:09 -0700449func osMutator(bpctx blueprint.BottomUpMutatorContext) {
Colin Crossa195f912019-10-16 11:07:20 -0700450 var module Module
451 var ok bool
Colin Cross617b88a2020-08-24 18:04:09 -0700452 if module, ok = bpctx.Module().(Module); !ok {
Colin Crossa6845402020-11-16 15:08:19 -0800453 // The module is not a Soong module, it is a Blueprint module.
Colin Cross617b88a2020-08-24 18:04:09 -0700454 if bootstrap.IsBootstrapModule(bpctx.Module()) {
455 // Bootstrap Go modules are always the build OS or linux bionic.
456 config := bpctx.Config().(Config)
457 osNames := []string{config.BuildOSTarget.OsVariation()}
458 for _, hostCrossTarget := range config.Targets[LinuxBionic] {
459 if hostCrossTarget.Arch.ArchType == config.BuildOSTarget.Arch.ArchType {
460 osNames = append(osNames, hostCrossTarget.OsVariation())
461 }
462 }
463 osNames = FirstUniqueStrings(osNames)
464 bpctx.CreateVariations(osNames...)
465 }
Colin Crossa195f912019-10-16 11:07:20 -0700466 return
467 }
468
Colin Cross617b88a2020-08-24 18:04:09 -0700469 // Bootstrap Go module support above requires this mutator to be a
470 // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext
471 // filters out non-Soong modules. Now that we've handled them, create a
472 // normal android.BottomUpMutatorContext.
Liz Kammer356f7d42021-01-26 09:18:53 -0500473 mctx := bottomUpMutatorContextFactory(bpctx, module, false, false)
Colin Cross617b88a2020-08-24 18:04:09 -0700474
Colin Crossa195f912019-10-16 11:07:20 -0700475 base := module.base()
476
Colin Crossa6845402020-11-16 15:08:19 -0800477 // Nothing to do for modules that are not architecture specific (e.g. a genrule).
Colin Crossa195f912019-10-16 11:07:20 -0700478 if !base.ArchSpecific() {
479 return
480 }
481
Colin Crossa6845402020-11-16 15:08:19 -0800482 // Collect a list of OSTypes supported by this module based on the HostOrDevice value
483 // passed to InitAndroidArchModule and the device_supported and host_supported properties.
Colin Crossa195f912019-10-16 11:07:20 -0700484 var moduleOSList []OsType
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000485 for _, os := range osTypeList {
Jiyong Park1613e552020-09-14 19:43:17 +0900486 for _, t := range mctx.Config().Targets[os] {
Colin Cross08d6f8f2020-11-19 02:33:19 +0000487 if base.supportsTarget(t) {
Jiyong Park1613e552020-09-14 19:43:17 +0900488 moduleOSList = append(moduleOSList, os)
489 break
Colin Crossa195f912019-10-16 11:07:20 -0700490 }
491 }
Colin Crossa195f912019-10-16 11:07:20 -0700492 }
493
Colin Crossa6845402020-11-16 15:08:19 -0800494 // If there are no supported OSes then disable the module.
Colin Crossa195f912019-10-16 11:07:20 -0700495 if len(moduleOSList) == 0 {
Inseob Kimeec88e12020-01-22 11:11:29 +0900496 base.Disable()
Colin Crossa195f912019-10-16 11:07:20 -0700497 return
498 }
499
Colin Crossa6845402020-11-16 15:08:19 -0800500 // Convert the list of supported OsTypes to the variation names.
Colin Crossa195f912019-10-16 11:07:20 -0700501 osNames := make([]string, len(moduleOSList))
Colin Crossa195f912019-10-16 11:07:20 -0700502 for i, os := range moduleOSList {
503 osNames[i] = os.String()
504 }
505
Paul Duffin1356d8c2020-02-25 19:26:33 +0000506 createCommonOSVariant := base.commonProperties.CreateCommonOSVariant
507 if createCommonOSVariant {
Colin Crossa6845402020-11-16 15:08:19 -0800508 // A CommonOS variant was requested so add it to the list of OS variants to
Paul Duffin1356d8c2020-02-25 19:26:33 +0000509 // create. It needs to be added to the end because it needs to depend on the
510 // the other variants in the list returned by CreateVariations(...) and inter
511 // variant dependencies can only be created from a later variant in that list to
512 // an earlier one. That is because variants are always processed in the order in
513 // which they are returned from CreateVariations(...).
514 osNames = append(osNames, CommonOS.Name)
515 moduleOSList = append(moduleOSList, CommonOS)
Colin Crossa195f912019-10-16 11:07:20 -0700516 }
517
Colin Crossa6845402020-11-16 15:08:19 -0800518 // Create the variations, annotate each one with which OS it was created for, and
519 // squash the appropriate OS-specific properties into the top level properties.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000520 modules := mctx.CreateVariations(osNames...)
521 for i, m := range modules {
522 m.base().commonProperties.CompileOS = moduleOSList[i]
523 m.base().setOSProperties(mctx)
524 }
525
526 if createCommonOSVariant {
527 // A CommonOS variant was requested so add dependencies from it (the last one in
528 // the list) to the OS type specific variants.
529 last := len(modules) - 1
530 commonOSVariant := modules[last]
531 commonOSVariant.base().commonProperties.CommonOSVariant = true
532 for _, module := range modules[0:last] {
533 // Ignore modules that are enabled. Note, this will only avoid adding
534 // dependencies on OsType variants that are explicitly disabled in their
535 // properties. The CommonOS variant will still depend on disabled variants
536 // if they are disabled afterwards, e.g. in archMutator if
537 if module.Enabled() {
538 mctx.AddInterVariantDependency(commonOsToOsSpecificVariantTag, commonOSVariant, module)
539 }
540 }
541 }
542}
543
Colin Crossc179ea62020-10-09 10:54:15 -0700544type archDepTag struct {
545 blueprint.BaseDependencyTag
546 name string
547}
Paul Duffin1356d8c2020-02-25 19:26:33 +0000548
Colin Crossc179ea62020-10-09 10:54:15 -0700549// Identifies the dependency from CommonOS variant to the os specific variants.
550var commonOsToOsSpecificVariantTag = archDepTag{name: "common os to os specific"}
551
Paul Duffin1356d8c2020-02-25 19:26:33 +0000552// Get the OsType specific variants for the current CommonOS variant.
553//
554// The returned list will only contain enabled OsType specific variants of the
555// module referenced in the supplied context. An empty list is returned if there
556// are no enabled variants or the supplied context is not for an CommonOS
557// variant.
558func GetOsSpecificVariantsOfCommonOSVariant(mctx BaseModuleContext) []Module {
559 var variants []Module
560 mctx.VisitDirectDeps(func(m Module) {
561 if mctx.OtherModuleDependencyTag(m) == commonOsToOsSpecificVariantTag {
562 if m.Enabled() {
563 variants = append(variants, m)
564 }
565 }
566 })
Paul Duffin1356d8c2020-02-25 19:26:33 +0000567 return variants
Colin Crossa195f912019-10-16 11:07:20 -0700568}
569
Dan Willemsen47450072021-10-19 20:24:49 -0700570var DarwinUniversalVariantTag = archDepTag{name: "darwin universal binary"}
571
Colin Crossee0bc3b2018-10-02 22:01:37 -0700572// archMutator splits a module into a variant for each Target requested by the module. Target selection
Colin Crossa6845402020-11-16 15:08:19 -0800573// for a module is in three levels, OsClass, multilib, and then Target.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700574// OsClass selection is determined by:
575// - The HostOrDeviceSupported value passed in to InitAndroidArchModule by the module type factory, which selects
576// whether the module type can compile for host, device or both.
577// - The host_supported and device_supported properties on the module.
Roland Levillainf5b635d2019-06-05 14:42:57 +0100578// 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 -0700579// for the module, the Device OsClass is selected.
580// Within each selected OsClass, the multilib selection is determined by:
Jaewoong Jung02b2d4d2019-06-06 15:19:57 -0700581// - The compile_multilib property if it set (which may be overridden by target.android.compile_multilib or
Colin Crossee0bc3b2018-10-02 22:01:37 -0700582// target.host.compile_multilib).
583// - The default multilib passed to InitAndroidArchModule if compile_multilib was not set.
584// Valid multilib values include:
585// "both": compile for all Targets supported by the OsClass (generally x86_64 and x86, or arm64 and arm).
586// "first": compile for only a single preferred Target supported by the OsClass. This is generally x86_64 or arm64,
Elliott Hughes79ae3412020-04-17 15:49:49 -0700587// but may be arm for a 32-bit only build.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700588// "32": compile for only a single 32-bit Target supported by the OsClass.
589// "64": compile for only a single 64-bit Target supported by the OsClass.
Colin Crossa6845402020-11-16 15:08:19 -0800590// "common": compile a for a single Target that will work on all Targets supported by the OsClass (for example Java).
591// "common_first": compile a for a Target that will work on all Targets supported by the OsClass
592// (same as "common"), plus a second Target for the preferred Target supported by the OsClass
593// (same as "first"). This is used for java_binary that produces a common .jar and a wrapper
594// executable script.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700595//
596// Once the list of Targets is determined, the module is split into a variant for each Target.
597//
598// Modules can be initialized with InitAndroidMultiTargetsArchModule, in which case they will be split by OsClass,
599// but will have a common Target that is expected to handle all other selected Targets via ctx.MultiTargets().
Colin Cross617b88a2020-08-24 18:04:09 -0700600func archMutator(bpctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700601 var module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800602 var ok bool
Colin Cross617b88a2020-08-24 18:04:09 -0700603 if module, ok = bpctx.Module().(Module); !ok {
604 if bootstrap.IsBootstrapModule(bpctx.Module()) {
605 // Bootstrap Go modules are always the build architecture.
606 bpctx.CreateVariations(bpctx.Config().(Config).BuildOSTarget.ArchVariation())
607 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800608 return
609 }
610
Colin Cross617b88a2020-08-24 18:04:09 -0700611 // Bootstrap Go module support above requires this mutator to be a
612 // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext
613 // filters out non-Soong modules. Now that we've handled them, create a
614 // normal android.BottomUpMutatorContext.
Liz Kammer356f7d42021-01-26 09:18:53 -0500615 mctx := bottomUpMutatorContextFactory(bpctx, module, false, false)
Colin Cross617b88a2020-08-24 18:04:09 -0700616
Colin Cross5eca7cb2018-10-02 14:02:10 -0700617 base := module.base()
618
619 if !base.ArchSpecific() {
Colin Crossb9db4802016-06-03 01:50:47 +0000620 return
621 }
622
Colin Crossa195f912019-10-16 11:07:20 -0700623 os := base.commonProperties.CompileOS
Paul Duffin1356d8c2020-02-25 19:26:33 +0000624 if os == CommonOS {
625 // Make sure that the target related properties are initialized for the
626 // CommonOS variant.
627 addTargetProperties(module, commonTargetMap[os.Name], nil, true)
628
629 // Do not create arch specific variants for the CommonOS variant.
630 return
631 }
632
Colin Crossa195f912019-10-16 11:07:20 -0700633 osTargets := mctx.Config().Targets[os]
Colin Crossfb0c16e2019-11-20 17:12:35 -0800634 image := base.commonProperties.ImageVariation
Colin Crossa6845402020-11-16 15:08:19 -0800635 // Filter NativeBridge targets unless they are explicitly supported.
636 // Skip creating native bridge variants for non-core modules.
Paul Duffine3d1ae42021-09-03 17:47:17 +0100637 if os == Android && !(base.IsNativeBridgeSupported() && image == CoreVariation) {
Colin Cross83bead42019-12-18 10:45:46 -0800638
Colin Crossa195f912019-10-16 11:07:20 -0700639 var targets []Target
640 for _, t := range osTargets {
641 if !t.NativeBridge {
642 targets = append(targets, t)
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700643 }
644 }
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700645
Colin Crossa195f912019-10-16 11:07:20 -0700646 osTargets = targets
647 }
Colin Crossee0bc3b2018-10-02 22:01:37 -0700648
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700649 // only the primary arch in the ramdisk / vendor_ramdisk / recovery partition
Inseob Kim08758f02021-04-08 21:13:22 +0900650 if os == Android && (module.InstallInRecovery() || module.InstallInRamdisk() || module.InstallInVendorRamdisk() || module.InstallInDebugRamdisk()) {
Colin Crossa195f912019-10-16 11:07:20 -0700651 osTargets = []Target{osTargets[0]}
652 }
dimitry1f33e402019-03-26 12:39:31 +0100653
Jaewoong Jung003d8082021-02-24 17:39:54 -0800654 // Windows builds always prefer 32-bit
655 prefer32 := os == Windows
dimitry1f33e402019-03-26 12:39:31 +0100656
Colin Crossa6845402020-11-16 15:08:19 -0800657 // Determine the multilib selection for this module.
Dan Willemsen47450072021-10-19 20:24:49 -0700658 multilib, extraMultilib := decodeMultilib(base, os)
Colin Crossa6845402020-11-16 15:08:19 -0800659
660 // Convert the multilib selection into a list of Targets.
Colin Crossa195f912019-10-16 11:07:20 -0700661 targets, err := decodeMultilibTargets(multilib, osTargets, prefer32)
662 if err != nil {
663 mctx.ModuleErrorf("%s", err.Error())
664 }
Colin Cross5eca7cb2018-10-02 14:02:10 -0700665
Colin Crossa6845402020-11-16 15:08:19 -0800666 // If the module is using extraMultilib, decode the extraMultilib selection into
667 // a separate list of Targets.
Colin Crossa195f912019-10-16 11:07:20 -0700668 var multiTargets []Target
669 if extraMultilib != "" {
670 multiTargets, err = decodeMultilibTargets(extraMultilib, osTargets, prefer32)
Colin Crossa1ad8d12016-06-01 17:09:44 -0700671 if err != nil {
672 mctx.ModuleErrorf("%s", err.Error())
673 }
Colin Crossb9db4802016-06-03 01:50:47 +0000674 }
675
Colin Crossa6845402020-11-16 15:08:19 -0800676 // Recovery is always the primary architecture, filter out any other architectures.
Inseob Kim20fb5d42021-02-02 20:07:58 +0900677 // Common arch is also allowed
Colin Crossfb0c16e2019-11-20 17:12:35 -0800678 if image == RecoveryVariation {
679 primaryArch := mctx.Config().DevicePrimaryArchType()
Inseob Kim20fb5d42021-02-02 20:07:58 +0900680 targets = filterToArch(targets, primaryArch, Common)
681 multiTargets = filterToArch(multiTargets, primaryArch, Common)
Colin Crossfb0c16e2019-11-20 17:12:35 -0800682 }
683
Colin Crossa6845402020-11-16 15:08:19 -0800684 // If there are no supported targets disable the module.
Colin Crossa195f912019-10-16 11:07:20 -0700685 if len(targets) == 0 {
Inseob Kimeec88e12020-01-22 11:11:29 +0900686 base.Disable()
Dan Willemsen3f32f032016-07-11 14:36:48 -0700687 return
688 }
689
Colin Crossa6845402020-11-16 15:08:19 -0800690 // Convert the targets into a list of arch variation names.
Colin Crossa195f912019-10-16 11:07:20 -0700691 targetNames := make([]string, len(targets))
Colin Crossa195f912019-10-16 11:07:20 -0700692 for i, target := range targets {
693 targetNames[i] = target.ArchVariation()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700694 }
695
Colin Crossa6845402020-11-16 15:08:19 -0800696 // Create the variations, annotate each one with which Target it was created for, and
697 // squash the appropriate arch-specific properties into the top level properties.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700698 modules := mctx.CreateVariations(targetNames...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800699 for i, m := range modules {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000700 addTargetProperties(m, targets[i], multiTargets, i == 0)
Colin Cross617b88a2020-08-24 18:04:09 -0700701 m.base().setArchProperties(mctx)
Dan Willemsen8528f4e2021-10-19 00:22:06 -0700702
703 // Install support doesn't understand Darwin+Arm64
704 if os == Darwin && targets[i].HostCross {
705 m.base().commonProperties.SkipInstall = true
706 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800707 }
Dan Willemsen47450072021-10-19 20:24:49 -0700708
709 // Create a dependency for Darwin Universal binaries from the primary to secondary
710 // architecture. The module itself will be responsible for calling lipo to merge the outputs.
711 if os == Darwin {
712 if multilib == "darwin_universal" && len(modules) == 2 {
713 mctx.AddInterVariantDependency(DarwinUniversalVariantTag, modules[1], modules[0])
714 } else if multilib == "darwin_universal_common_first" && len(modules) == 3 {
715 mctx.AddInterVariantDependency(DarwinUniversalVariantTag, modules[2], modules[1])
716 }
717 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800718}
719
Colin Crossa6845402020-11-16 15:08:19 -0800720// addTargetProperties annotates a variant with the Target is is being compiled for, the list
721// of additional Targets it is supporting (if any), and whether it is the primary Target for
722// the module.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000723func addTargetProperties(m Module, target Target, multiTargets []Target, primaryTarget bool) {
724 m.base().commonProperties.CompileTarget = target
725 m.base().commonProperties.CompileMultiTargets = multiTargets
726 m.base().commonProperties.CompilePrimary = primaryTarget
727}
728
Colin Crossa6845402020-11-16 15:08:19 -0800729// decodeMultilib returns the appropriate compile_multilib property for the module, or the default
730// multilib from the factory's call to InitAndroidArchModule if none was set. For modules that
731// called InitAndroidMultiTargetsArchModule it always returns "common" for multilib, and returns
732// the actual multilib in extraMultilib.
Dan Willemsen47450072021-10-19 20:24:49 -0700733func decodeMultilib(base *ModuleBase, os OsType) (multilib, extraMultilib string) {
Colin Crossa6845402020-11-16 15:08:19 -0800734 // First check the "android.compile_multilib" or "host.compile_multilib" properties.
Dan Willemsen47450072021-10-19 20:24:49 -0700735 switch os.Class {
Colin Crossee0bc3b2018-10-02 22:01:37 -0700736 case Device:
737 multilib = String(base.commonProperties.Target.Android.Compile_multilib)
Jiyong Park1613e552020-09-14 19:43:17 +0900738 case Host:
Colin Crossee0bc3b2018-10-02 22:01:37 -0700739 multilib = String(base.commonProperties.Target.Host.Compile_multilib)
740 }
Colin Crossa6845402020-11-16 15:08:19 -0800741
742 // If those aren't set, try the "compile_multilib" property.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700743 if multilib == "" {
744 multilib = String(base.commonProperties.Compile_multilib)
745 }
Colin Crossa6845402020-11-16 15:08:19 -0800746
747 // If that wasn't set, use the default multilib set by the factory.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700748 if multilib == "" {
749 multilib = base.commonProperties.Default_multilib
750 }
751
752 if base.commonProperties.UseTargetVariants {
Dan Willemsen47450072021-10-19 20:24:49 -0700753 // Darwin has the concept of "universal binaries" which is implemented in Soong by
754 // building both x86_64 and arm64 variants, and having select module types know how to
755 // merge the outputs of their corresponding variants together into a final binary. Most
756 // module types don't need to understand this logic, as we only build a small portion
757 // of the tree for Darwin, and only module types writing macho files need to do the
758 // merging.
759 //
760 // This logic is not enabled for:
761 // "common", as it's not an arch-specific variant
762 // "32", as Darwin never has a 32-bit variant
763 // !UseTargetVariants, as the module has opted into handling the arch-specific logic on
764 // its own.
765 if os == Darwin && multilib != "common" && multilib != "32" {
766 if multilib == "common_first" {
767 multilib = "darwin_universal_common_first"
768 } else {
769 multilib = "darwin_universal"
770 }
771 }
772
Colin Crossee0bc3b2018-10-02 22:01:37 -0700773 return multilib, ""
774 } else {
775 // For app modules a single arch variant will be created per OS class which is expected to handle all the
776 // selected arches. Return the common-type as multilib and any Android.bp provided multilib as extraMultilib
777 if multilib == base.commonProperties.Default_multilib {
778 multilib = "first"
779 }
780 return base.commonProperties.Default_multilib, multilib
781 }
782}
783
Colin Crossa6845402020-11-16 15:08:19 -0800784// filterToArch takes a list of Targets and an ArchType, and returns a modified list that contains
Inseob Kim20fb5d42021-02-02 20:07:58 +0900785// only Targets that have the specified ArchTypes.
786func filterToArch(targets []Target, archs ...ArchType) []Target {
Colin Crossfb0c16e2019-11-20 17:12:35 -0800787 for i := 0; i < len(targets); i++ {
Inseob Kim20fb5d42021-02-02 20:07:58 +0900788 found := false
789 for _, arch := range archs {
790 if targets[i].Arch.ArchType == arch {
791 found = true
792 break
793 }
794 }
795 if !found {
Colin Crossfb0c16e2019-11-20 17:12:35 -0800796 targets = append(targets[:i], targets[i+1:]...)
797 i--
798 }
799 }
800 return targets
801}
802
Colin Crossa6845402020-11-16 15:08:19 -0800803// archPropRoot is a struct type used as the top level of the arch-specific properties. It
804// contains the "arch", "multilib", and "target" property structs. It is used to split up the
805// property structs to limit how much is allocated when a single arch-specific property group is
806// used. The types are interface{} because they will hold instances of runtime-created types.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800807type archPropRoot struct {
808 Arch, Multilib, Target interface{}
809}
810
Colin Crossa6845402020-11-16 15:08:19 -0800811// archPropTypeDesc holds the runtime-created types for the property structs to instantiate to
812// create an archPropRoot property struct.
813type archPropTypeDesc struct {
814 arch, multilib, target reflect.Type
815}
816
Colin Crosscbbd13f2020-01-17 14:08:22 -0800817// createArchPropTypeDesc takes a reflect.Type that is either a struct or a pointer to a struct, and
818// returns lists of reflect.Types that contains the arch-variant properties inside structs for each
819// arch, multilib and target property.
Colin Crossa6845402020-11-16 15:08:19 -0800820//
821// This is a relatively expensive operation, so the results are cached in the global
822// archPropTypeMap. It is constructed entirely based on compile-time data, so there is no need
823// to isolate the results between multiple tests running in parallel.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800824func createArchPropTypeDesc(props reflect.Type) []archPropTypeDesc {
Colin Crossb1d8c992020-01-21 11:43:29 -0800825 // Each property struct shard will be nested many times under the runtime generated arch struct,
826 // which can hit the limit of 64kB for the name of runtime generated structs. They are nested
827 // 97 times now, which may grow in the future, plus there is some overhead for the containing
828 // type. This number may need to be reduced if too many are added, but reducing it too far
829 // could cause problems if a single deeply nested property no longer fits in the name.
830 const maxArchTypeNameSize = 500
831
Colin Crossa6845402020-11-16 15:08:19 -0800832 // Convert the type to a new set of types that contains only the arch-specific properties
Usta Shrestha0b52d832022-02-04 21:37:39 -0500833 // (those that are tagged with `android:"arch_variant"`), and sharded into multiple types
Colin Crossa6845402020-11-16 15:08:19 -0800834 // to keep the runtime-generated names under the limit.
Colin Crossb1d8c992020-01-21 11:43:29 -0800835 propShards, _ := proptools.FilterPropertyStructSharded(props, maxArchTypeNameSize, filterArchStruct)
Colin Crossa6845402020-11-16 15:08:19 -0800836
837 // If the type has no arch-specific properties there is nothing to do.
Colin Crosscb988072019-01-24 14:58:11 -0800838 if len(propShards) == 0 {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700839 return nil
840 }
841
Colin Crosscbbd13f2020-01-17 14:08:22 -0800842 var ret []archPropTypeDesc
Colin Crossc17727d2018-10-24 12:42:09 -0700843 for _, props := range propShards {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700844
Colin Crossa6845402020-11-16 15:08:19 -0800845 // variantFields takes a list of variant property field names and returns a list the
846 // StructFields with the names and the type of the current shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700847 variantFields := func(names []string) []reflect.StructField {
848 ret := make([]reflect.StructField, len(names))
Dan Willemsenb1957a52016-06-23 23:44:54 -0700849
Colin Crossc17727d2018-10-24 12:42:09 -0700850 for i, name := range names {
851 ret[i].Name = name
852 ret[i].Type = props
Dan Willemsen866b5632017-09-22 12:28:24 -0700853 }
Colin Crossc17727d2018-10-24 12:42:09 -0700854
855 return ret
856 }
857
Colin Crossa6845402020-11-16 15:08:19 -0800858 // Create a type that contains the properties in this shard repeated for each
859 // architecture, architecture variant, and architecture feature.
Colin Crossc17727d2018-10-24 12:42:09 -0700860 archFields := make([]reflect.StructField, len(archTypeList))
861 for i, arch := range archTypeList {
Colin Crossa6845402020-11-16 15:08:19 -0800862 var variants []string
Colin Crossc17727d2018-10-24 12:42:09 -0700863
864 for _, archVariant := range archVariants[arch] {
865 archVariant := variantReplacer.Replace(archVariant)
866 variants = append(variants, proptools.FieldNameForProperty(archVariant))
867 }
Liz Kammer2c2afe22022-02-11 11:35:03 -0500868 for _, cpuVariant := range cpuVariants[arch] {
869 cpuVariant := variantReplacer.Replace(cpuVariant)
870 variants = append(variants, proptools.FieldNameForProperty(cpuVariant))
871 }
Colin Crossc17727d2018-10-24 12:42:09 -0700872 for _, feature := range archFeatures[arch] {
873 feature := variantReplacer.Replace(feature)
874 variants = append(variants, proptools.FieldNameForProperty(feature))
875 }
876
Colin Crossa6845402020-11-16 15:08:19 -0800877 // Create the StructFields for each architecture variant architecture feature
878 // (e.g. "arch.arm.cortex-a53" or "arch.arm.neon").
Colin Crossc17727d2018-10-24 12:42:09 -0700879 fields := variantFields(variants)
880
Colin Crossa6845402020-11-16 15:08:19 -0800881 // Create the StructField for the architecture itself (e.g. "arch.arm"). The special
882 // "BlueprintEmbed" name is used by Blueprint to put the properties in the
883 // parent struct.
Colin Crossc17727d2018-10-24 12:42:09 -0700884 fields = append([]reflect.StructField{{
885 Name: "BlueprintEmbed",
886 Type: props,
887 Anonymous: true,
888 }}, fields...)
889
890 archFields[i] = reflect.StructField{
891 Name: arch.Field,
892 Type: reflect.StructOf(fields),
893 }
894 }
Colin Crossa6845402020-11-16 15:08:19 -0800895
896 // Create the type of the "arch" property struct for this shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700897 archType := reflect.StructOf(archFields)
898
Colin Crossa6845402020-11-16 15:08:19 -0800899 // Create the type for the "multilib" property struct for this shard, containing the
900 // "multilib.lib32" and "multilib.lib64" property structs.
Colin Crossc17727d2018-10-24 12:42:09 -0700901 multilibType := reflect.StructOf(variantFields([]string{"Lib32", "Lib64"}))
902
Colin Crossa6845402020-11-16 15:08:19 -0800903 // Start with a list of the special targets
Colin Crossc17727d2018-10-24 12:42:09 -0700904 targets := []string{
905 "Host",
906 "Android64",
907 "Android32",
908 "Bionic",
Colin Cross528d67e2021-07-23 22:23:07 +0000909 "Glibc",
910 "Musl",
Colin Crossc17727d2018-10-24 12:42:09 -0700911 "Linux",
912 "Not_windows",
913 "Arm_on_x86",
914 "Arm_on_x86_64",
Victor Khimenkoc26fcf42020-05-07 22:16:33 +0200915 "Native_bridge",
Colin Crossc17727d2018-10-24 12:42:09 -0700916 }
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000917 for _, os := range osTypeList {
Colin Crossa6845402020-11-16 15:08:19 -0800918 // Add all the OSes.
Colin Crossc17727d2018-10-24 12:42:09 -0700919 targets = append(targets, os.Field)
920
Colin Crossa6845402020-11-16 15:08:19 -0800921 // Add the OS/Arch combinations, e.g. "android_arm64".
Colin Crossc17727d2018-10-24 12:42:09 -0700922 for _, archType := range osArchTypeMap[os] {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400923 targets = append(targets, GetCompoundTargetField(os, archType))
Colin Crossc17727d2018-10-24 12:42:09 -0700924
Colin Cross1aa45b02022-02-10 10:33:10 -0800925 // Also add the special "linux_<arch>", "bionic_<arch>" , "glibc_<arch>", and
926 // "musl_<arch>" property structs.
Colin Crossc17727d2018-10-24 12:42:09 -0700927 if os.Linux() {
928 target := "Linux_" + archType.Name
929 if !InList(target, targets) {
930 targets = append(targets, target)
931 }
932 }
933 if os.Bionic() {
934 target := "Bionic_" + archType.Name
935 if !InList(target, targets) {
936 targets = append(targets, target)
937 }
Dan Willemsen866b5632017-09-22 12:28:24 -0700938 }
Colin Cross1aa45b02022-02-10 10:33:10 -0800939 if os == Linux {
940 target := "Glibc_" + archType.Name
941 if !InList(target, targets) {
942 targets = append(targets, target)
943 }
944 }
945 if os == LinuxMusl {
946 target := "Musl_" + archType.Name
947 if !InList(target, targets) {
948 targets = append(targets, target)
949 }
950 }
Dan Willemsen866b5632017-09-22 12:28:24 -0700951 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700952 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700953
Colin Crossa6845402020-11-16 15:08:19 -0800954 // Create the type for the "target" property struct for this shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700955 targetType := reflect.StructOf(variantFields(targets))
Colin Crosscbbd13f2020-01-17 14:08:22 -0800956
Colin Crossa6845402020-11-16 15:08:19 -0800957 // Return a descriptor of the 3 runtime-created types.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800958 ret = append(ret, archPropTypeDesc{
959 arch: reflect.PtrTo(archType),
960 multilib: reflect.PtrTo(multilibType),
961 target: reflect.PtrTo(targetType),
962 })
Colin Crossc17727d2018-10-24 12:42:09 -0700963 }
964 return ret
Dan Willemsenb1957a52016-06-23 23:44:54 -0700965}
966
Colin Crossa6845402020-11-16 15:08:19 -0800967// variantReplacer converts architecture variant or architecture feature names into names that
968// are valid for an Android.bp file.
969var variantReplacer = strings.NewReplacer("-", "_", ".", "_")
970
971// filterArchStruct returns true if the given field is an architecture specific property.
Colin Cross74449102019-09-25 11:26:40 -0700972func filterArchStruct(field reflect.StructField, prefix string) (bool, reflect.StructField) {
973 if proptools.HasTag(field, "android", "arch_variant") {
974 // The arch_variant field isn't necessary past this point
975 // Instead of wasting space, just remove it. Go also has a
976 // 16-bit limit on structure name length. The name is constructed
977 // based on the Go source representation of the structure, so
978 // the tag names count towards that length.
Colin Crossb4fecbf2020-01-21 11:38:47 -0800979
980 androidTag := field.Tag.Get("android")
981 values := strings.Split(androidTag, ",")
982
983 if string(field.Tag) != `android:"`+strings.Join(values, ",")+`"` {
984 panic(fmt.Errorf("unexpected tag format %q", field.Tag))
Colin Cross74449102019-09-25 11:26:40 -0700985 }
Liz Kammer4562a3b2021-04-21 18:15:34 -0400986 // don't delete path tag as it is needed for bp2build
Colin Crossb4fecbf2020-01-21 11:38:47 -0800987 // these tags don't need to be present in the runtime generated struct type.
Liz Kammer4562a3b2021-04-21 18:15:34 -0400988 values = RemoveListFromList(values, []string{"arch_variant", "variant_prepend"})
989 if len(values) > 0 && values[0] != "path" {
Colin Crossb4fecbf2020-01-21 11:38:47 -0800990 panic(fmt.Errorf("unknown tags %q in field %q", values, prefix+field.Name))
Liz Kammer4562a3b2021-04-21 18:15:34 -0400991 } else if len(values) == 1 {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxfd0c03c2021-09-21 15:17:48 +0000992 // FIXME(b/200678898): This assumes that the only tag type when there's
993 // `android:"arch_variant"` is `android` itself and thus clobbers others
Liz Kammer4562a3b2021-04-21 18:15:34 -0400994 field.Tag = reflect.StructTag(`android:"` + strings.Join(values, ",") + `"`)
995 } else {
996 field.Tag = ``
Colin Crossb4fecbf2020-01-21 11:38:47 -0800997 }
998
Colin Cross74449102019-09-25 11:26:40 -0700999 return true, field
1000 }
1001 return false, field
1002}
1003
Colin Crossa6845402020-11-16 15:08:19 -08001004// archPropTypeMap contains a cache of the results of createArchPropTypeDesc for each type. It is
1005// shared across all Contexts, but is constructed based only on compile-time information so there
1006// is no risk of contaminating one Context with data from another.
Dan Willemsenb1957a52016-06-23 23:44:54 -07001007var archPropTypeMap OncePer
1008
Colin Crossa6845402020-11-16 15:08:19 -08001009// initArchModule adds the architecture-specific property structs to a Module.
1010func initArchModule(m Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001011
1012 base := m.base()
1013
Ustaeabf0f32021-12-06 15:17:23 -05001014 if len(base.archProperties) != 0 {
1015 panic(fmt.Errorf("module %s already has archProperties", m.Name()))
1016 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001017
Ustaeabf0f32021-12-06 15:17:23 -05001018 getStructType := func(properties interface{}) reflect.Type {
Colin Cross3f40fa42015-01-30 17:27:36 -08001019 propertiesValue := reflect.ValueOf(properties)
Colin Cross62496a02016-08-08 15:49:17 -07001020 t := propertiesValue.Type()
Colin Cross3f40fa42015-01-30 17:27:36 -08001021 if propertiesValue.Kind() != reflect.Ptr {
Colin Crossca860ac2016-01-04 14:34:37 -08001022 panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
1023 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -08001024 }
1025
1026 propertiesValue = propertiesValue.Elem()
1027 if propertiesValue.Kind() != reflect.Struct {
Ustaeabf0f32021-12-06 15:17:23 -05001028 panic(fmt.Errorf("properties must be a pointer to a struct, got a pointer to %T",
Colin Crossca860ac2016-01-04 14:34:37 -08001029 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -08001030 }
Ustaeabf0f32021-12-06 15:17:23 -05001031 return t
1032 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001033
Usta851a3272022-01-05 23:42:33 -05001034 for _, properties := range m.GetProperties() {
Ustaeabf0f32021-12-06 15:17:23 -05001035 t := getStructType(properties)
Colin Crossa6845402020-11-16 15:08:19 -08001036 // Get or create the arch-specific property struct types for this property struct type.
Colin Cross571cccf2019-02-04 11:22:08 -08001037 archPropTypes := archPropTypeMap.Once(NewCustomOnceKey(t), func() interface{} {
Colin Crosscbbd13f2020-01-17 14:08:22 -08001038 return createArchPropTypeDesc(t)
1039 }).([]archPropTypeDesc)
Colin Cross3f40fa42015-01-30 17:27:36 -08001040
Colin Crossa6845402020-11-16 15:08:19 -08001041 // Instantiate one of each arch-specific property struct type and add it to the
1042 // properties for the Module.
Colin Crossc17727d2018-10-24 12:42:09 -07001043 var archProperties []interface{}
1044 for _, t := range archPropTypes {
Colin Crosscbbd13f2020-01-17 14:08:22 -08001045 archProperties = append(archProperties, &archPropRoot{
1046 Arch: reflect.Zero(t.arch).Interface(),
1047 Multilib: reflect.Zero(t.multilib).Interface(),
1048 Target: reflect.Zero(t.target).Interface(),
1049 })
Dan Willemsenb1957a52016-06-23 23:44:54 -07001050 }
Colin Crossc17727d2018-10-24 12:42:09 -07001051 base.archProperties = append(base.archProperties, archProperties)
1052 m.AddProperties(archProperties...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001053 }
1054
Colin Cross3f40fa42015-01-30 17:27:36 -08001055}
1056
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001057func maybeBlueprintEmbed(src reflect.Value) reflect.Value {
Colin Crossa6845402020-11-16 15:08:19 -08001058 // If the value of the field is a struct (as opposed to a pointer to a struct) then step
1059 // into the BlueprintEmbed field.
Dan Willemsenb1957a52016-06-23 23:44:54 -07001060 if src.Kind() == reflect.Struct {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001061 return src.FieldByName("BlueprintEmbed")
1062 } else {
1063 return src
Colin Cross06a931b2015-10-28 17:23:31 -07001064 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001065}
1066
1067// Merges the property struct in srcValue into dst.
Liz Kammerb6dbc872021-05-14 15:14:40 -04001068func mergePropertyStruct(ctx ArchVariantContext, dst interface{}, srcValue reflect.Value) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001069 src := maybeBlueprintEmbed(srcValue).Interface()
Colin Cross06a931b2015-10-28 17:23:31 -07001070
Colin Crossa6845402020-11-16 15:08:19 -08001071 // order checks the `android:"variant_prepend"` tag to handle properties where the
1072 // arch-specific value needs to come before the generic value, for example for lists of
1073 // include directories.
Colin Cross6ee75b62016-05-05 15:57:15 -07001074 order := func(property string,
1075 dstField, srcField reflect.StructField,
1076 dstValue, srcValue interface{}) (proptools.Order, error) {
1077 if proptools.HasTag(dstField, "android", "variant_prepend") {
1078 return proptools.Prepend, nil
1079 } else {
1080 return proptools.Append, nil
1081 }
1082 }
1083
Colin Crossa6845402020-11-16 15:08:19 -08001084 // Squash the located property struct into the destination property struct.
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001085 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src, nil, order)
Colin Cross06a931b2015-10-28 17:23:31 -07001086 if err != nil {
1087 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
1088 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
1089 } else {
1090 panic(err)
1091 }
1092 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001093}
Colin Cross85a88972015-11-23 13:29:51 -08001094
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001095// Returns the immediate child of the input property struct that corresponds to
1096// the sub-property "field".
Liz Kammerb6dbc872021-05-14 15:14:40 -04001097func getChildPropertyStruct(ctx ArchVariantContext,
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001098 src reflect.Value, field, userFriendlyField string) (reflect.Value, bool) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001099
1100 // Step into non-nil pointers to structs in the src value.
1101 if src.Kind() == reflect.Ptr {
1102 if src.IsNil() {
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001103 return reflect.Value{}, false
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001104 }
1105 src = src.Elem()
1106 }
1107
1108 // Find the requested field in the src struct.
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001109 child := src.FieldByName(proptools.FieldNameForProperty(field))
1110 if !child.IsValid() {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001111 ctx.ModuleErrorf("field %q does not exist", userFriendlyField)
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001112 return reflect.Value{}, false
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001113 }
1114
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001115 if child.IsZero() {
1116 return reflect.Value{}, false
1117 }
1118
1119 return child, true
Colin Cross06a931b2015-10-28 17:23:31 -07001120}
1121
Colin Crossa6845402020-11-16 15:08:19 -08001122// Squash the appropriate OS-specific property structs into the matching top level property structs
1123// based on the CompileOS value that was annotated on the variant.
Colin Crossa195f912019-10-16 11:07:20 -07001124func (m *ModuleBase) setOSProperties(ctx BottomUpMutatorContext) {
1125 os := m.commonProperties.CompileOS
1126
Ustadca02192021-12-20 12:56:46 -05001127 for i := range m.archProperties {
Usta851a3272022-01-05 23:42:33 -05001128 genProps := m.GetProperties()[i]
Colin Crossa195f912019-10-16 11:07:20 -07001129 if m.archProperties[i] == nil {
1130 continue
1131 }
1132 for _, archProperties := range m.archProperties[i] {
1133 archPropValues := reflect.ValueOf(archProperties).Elem()
1134
Colin Crosscbbd13f2020-01-17 14:08:22 -08001135 targetProp := archPropValues.FieldByName("Target").Elem()
Colin Crossa195f912019-10-16 11:07:20 -07001136
1137 // Handle host-specific properties in the form:
1138 // target: {
1139 // host: {
1140 // key: value,
1141 // },
1142 // },
Jiyong Park1613e552020-09-14 19:43:17 +09001143 if os.Class == Host {
Colin Crossa195f912019-10-16 11:07:20 -07001144 field := "Host"
1145 prefix := "target.host"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001146 if hostProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1147 mergePropertyStruct(ctx, genProps, hostProperties)
1148 }
Colin Crossa195f912019-10-16 11:07:20 -07001149 }
1150
1151 // Handle target OS generalities of the form:
1152 // target: {
1153 // bionic: {
1154 // key: value,
1155 // },
1156 // }
1157 if os.Linux() {
1158 field := "Linux"
1159 prefix := "target.linux"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001160 if linuxProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1161 mergePropertyStruct(ctx, genProps, linuxProperties)
1162 }
Colin Crossa195f912019-10-16 11:07:20 -07001163 }
1164
1165 if os.Bionic() {
1166 field := "Bionic"
1167 prefix := "target.bionic"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001168 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1169 mergePropertyStruct(ctx, genProps, bionicProperties)
1170 }
Colin Crossa195f912019-10-16 11:07:20 -07001171 }
1172
Colin Cross528d67e2021-07-23 22:23:07 +00001173 if os == Linux {
1174 field := "Glibc"
1175 prefix := "target.glibc"
1176 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1177 mergePropertyStruct(ctx, genProps, bionicProperties)
1178 }
1179 }
1180
1181 if os == LinuxMusl {
1182 field := "Musl"
1183 prefix := "target.musl"
1184 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1185 mergePropertyStruct(ctx, genProps, bionicProperties)
1186 }
1187
1188 // Special case: to ease the transition from glibc to musl, apply linux_glibc
1189 // properties (which has historically mean host linux) to musl variants.
1190 field = "Linux_glibc"
1191 prefix = "target.linux_glibc"
1192 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1193 mergePropertyStruct(ctx, genProps, bionicProperties)
1194 }
1195 }
1196
Colin Crossa195f912019-10-16 11:07:20 -07001197 // Handle target OS properties in the form:
1198 // target: {
1199 // linux_glibc: {
1200 // key: value,
1201 // },
1202 // not_windows: {
1203 // key: value,
1204 // },
1205 // android {
1206 // key: value,
1207 // },
1208 // },
1209 field := os.Field
1210 prefix := "target." + os.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001211 if osProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1212 mergePropertyStruct(ctx, genProps, osProperties)
1213 }
Colin Crossa195f912019-10-16 11:07:20 -07001214
Jiyong Park1613e552020-09-14 19:43:17 +09001215 if os.Class == Host && os != Windows {
Colin Crossa195f912019-10-16 11:07:20 -07001216 field := "Not_windows"
1217 prefix := "target.not_windows"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001218 if notWindowsProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1219 mergePropertyStruct(ctx, genProps, notWindowsProperties)
1220 }
Colin Crossa195f912019-10-16 11:07:20 -07001221 }
1222
1223 // Handle 64-bit device properties in the form:
1224 // target {
1225 // android64 {
1226 // key: value,
1227 // },
1228 // android32 {
1229 // key: value,
1230 // },
1231 // },
1232 // WARNING: this is probably not what you want to use in your blueprints file, it selects
1233 // options for all targets on a device that supports 64-bit binaries, not just the targets
1234 // that are being compiled for 64-bit. Its expected use case is binaries like linker and
1235 // debuggerd that need to know when they are a 32-bit process running on a 64-bit device
1236 if os.Class == Device {
1237 if ctx.Config().Android64() {
1238 field := "Android64"
1239 prefix := "target.android64"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001240 if android64Properties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1241 mergePropertyStruct(ctx, genProps, android64Properties)
1242 }
Colin Crossa195f912019-10-16 11:07:20 -07001243 } else {
1244 field := "Android32"
1245 prefix := "target.android32"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001246 if android32Properties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1247 mergePropertyStruct(ctx, genProps, android32Properties)
1248 }
Colin Crossa195f912019-10-16 11:07:20 -07001249 }
1250 }
1251 }
1252 }
1253}
1254
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001255// Returns the struct containing the properties specific to the given
1256// architecture type. These look like this in Blueprint files:
1257// arch: {
1258// arm64: {
1259// key: value,
1260// },
1261// },
1262// This struct will also contain sub-structs containing to the architecture/CPU
1263// variants and features that themselves contain properties specific to those.
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001264func getArchTypeStruct(ctx ArchVariantContext, archProperties interface{}, archType ArchType) (reflect.Value, bool) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001265 archPropValues := reflect.ValueOf(archProperties).Elem()
1266 archProp := archPropValues.FieldByName("Arch").Elem()
1267 prefix := "arch." + archType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001268 return getChildPropertyStruct(ctx, archProp, archType.Name, prefix)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001269}
1270
1271// Returns the struct containing the properties specific to a given multilib
1272// value. These look like this in the Blueprint file:
1273// multilib: {
1274// lib32: {
1275// key: value,
1276// },
1277// },
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001278func getMultilibStruct(ctx ArchVariantContext, archProperties interface{}, archType ArchType) (reflect.Value, bool) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001279 archPropValues := reflect.ValueOf(archProperties).Elem()
1280 multilibProp := archPropValues.FieldByName("Multilib").Elem()
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001281 return getChildPropertyStruct(ctx, multilibProp, archType.Multilib, "multilib."+archType.Multilib)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001282}
1283
Liz Kammer9abd62d2021-05-21 08:37:59 -04001284func GetCompoundTargetField(os OsType, arch ArchType) string {
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04001285 return os.Field + "_" + arch.Name
1286}
1287
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001288// Returns the structs corresponding to the properties specific to the given
1289// architecture and OS in archProperties.
1290func getArchProperties(ctx BaseMutatorContext, archProperties interface{}, arch Arch, os OsType, nativeBridgeEnabled bool) []reflect.Value {
1291 result := make([]reflect.Value, 0)
1292 archPropValues := reflect.ValueOf(archProperties).Elem()
1293
1294 targetProp := archPropValues.FieldByName("Target").Elem()
1295
1296 archType := arch.ArchType
1297
1298 if arch.ArchType != Common {
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001299 archStruct, ok := getArchTypeStruct(ctx, archProperties, arch.ArchType)
1300 if ok {
1301 result = append(result, archStruct)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001302
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001303 // Handle arch-variant-specific properties in the form:
1304 // arch: {
1305 // arm: {
1306 // variant: {
1307 // key: value,
1308 // },
1309 // },
1310 // },
1311 v := variantReplacer.Replace(arch.ArchVariant)
1312 if v != "" {
1313 prefix := "arch." + archType.Name + "." + v
1314 if variantProperties, ok := getChildPropertyStruct(ctx, archStruct, v, prefix); ok {
1315 result = append(result, variantProperties)
1316 }
1317 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001318
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001319 // Handle cpu-variant-specific properties in the form:
1320 // arch: {
1321 // arm: {
1322 // variant: {
1323 // key: value,
1324 // },
1325 // },
1326 // },
1327 if arch.CpuVariant != arch.ArchVariant {
1328 c := variantReplacer.Replace(arch.CpuVariant)
1329 if c != "" {
1330 prefix := "arch." + archType.Name + "." + c
1331 if cpuVariantProperties, ok := getChildPropertyStruct(ctx, archStruct, c, prefix); ok {
1332 result = append(result, cpuVariantProperties)
1333 }
1334 }
1335 }
1336
1337 // Handle arch-feature-specific properties in the form:
1338 // arch: {
1339 // arm: {
1340 // feature: {
1341 // key: value,
1342 // },
1343 // },
1344 // },
1345 for _, feature := range arch.ArchFeatures {
1346 prefix := "arch." + archType.Name + "." + feature
1347 if featureProperties, ok := getChildPropertyStruct(ctx, archStruct, feature, prefix); ok {
1348 result = append(result, featureProperties)
1349 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001350 }
1351 }
1352
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001353 if multilibProperties, ok := getMultilibStruct(ctx, archProperties, archType); ok {
1354 result = append(result, multilibProperties)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001355 }
1356
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001357 // Handle combined OS-feature and arch specific properties in the form:
1358 // target: {
1359 // bionic_x86: {
1360 // key: value,
1361 // },
1362 // }
1363 if os.Linux() {
1364 field := "Linux_" + arch.ArchType.Name
1365 userFriendlyField := "target.linux_" + arch.ArchType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001366 if linuxProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1367 result = append(result, linuxProperties)
1368 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001369 }
1370
1371 if os.Bionic() {
1372 field := "Bionic_" + archType.Name
1373 userFriendlyField := "target.bionic_" + archType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001374 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1375 result = append(result, bionicProperties)
1376 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001377 }
1378
1379 // Handle combined OS and arch specific properties in the form:
1380 // target: {
1381 // linux_glibc_x86: {
1382 // key: value,
1383 // },
1384 // linux_glibc_arm: {
1385 // key: value,
1386 // },
1387 // android_arm {
1388 // key: value,
1389 // },
1390 // android_x86 {
1391 // key: value,
1392 // },
1393 // },
Liz Kammer9abd62d2021-05-21 08:37:59 -04001394 field := GetCompoundTargetField(os, archType)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001395 userFriendlyField := "target." + os.Name + "_" + archType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001396 if osArchProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1397 result = append(result, osArchProperties)
1398 }
Colin Cross528d67e2021-07-23 22:23:07 +00001399
Colin Cross1aa45b02022-02-10 10:33:10 -08001400 if os == Linux {
1401 field := "Glibc_" + archType.Name
1402 userFriendlyField := "target.glibc_" + "_" + archType.Name
1403 if osArchProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1404 result = append(result, osArchProperties)
1405 }
1406 }
1407
Colin Cross528d67e2021-07-23 22:23:07 +00001408 if os == LinuxMusl {
Colin Cross1aa45b02022-02-10 10:33:10 -08001409 field := "Musl_" + archType.Name
1410 userFriendlyField := "target.musl_" + "_" + archType.Name
1411 if osArchProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1412 result = append(result, osArchProperties)
1413 }
1414
Colin Cross528d67e2021-07-23 22:23:07 +00001415 // Special case: to ease the transition from glibc to musl, apply linux_glibc
1416 // properties (which has historically mean host linux) to musl variants.
Colin Cross1aa45b02022-02-10 10:33:10 -08001417 field = "Linux_glibc_" + archType.Name
1418 userFriendlyField = "target.linux_glibc_" + archType.Name
Colin Cross528d67e2021-07-23 22:23:07 +00001419 if osArchProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1420 result = append(result, osArchProperties)
1421 }
1422 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001423 }
1424
1425 // Handle arm on x86 properties in the form:
1426 // target {
1427 // arm_on_x86 {
1428 // key: value,
1429 // },
1430 // arm_on_x86_64 {
1431 // key: value,
1432 // },
1433 // },
1434 if os.Class == Device {
1435 if arch.ArchType == X86 && (hasArmAbi(arch) ||
1436 hasArmAndroidArch(ctx.Config().Targets[Android])) {
1437 field := "Arm_on_x86"
1438 userFriendlyField := "target.arm_on_x86"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001439 if armOnX86Properties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1440 result = append(result, armOnX86Properties)
1441 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001442 }
1443 if arch.ArchType == X86_64 && (hasArmAbi(arch) ||
1444 hasArmAndroidArch(ctx.Config().Targets[Android])) {
1445 field := "Arm_on_x86_64"
1446 userFriendlyField := "target.arm_on_x86_64"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001447 if armOnX8664Properties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1448 result = append(result, armOnX8664Properties)
1449 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001450 }
1451 if os == Android && nativeBridgeEnabled {
1452 userFriendlyField := "Native_bridge"
1453 prefix := "target.native_bridge"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001454 if nativeBridgeProperties, ok := getChildPropertyStruct(ctx, targetProp, userFriendlyField, prefix); ok {
1455 result = append(result, nativeBridgeProperties)
1456 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001457 }
1458 }
1459
1460 return result
1461}
1462
Colin Crossa6845402020-11-16 15:08:19 -08001463// Squash the appropriate arch-specific property structs into the matching top level property
1464// structs based on the CompileTarget value that was annotated on the variant.
Colin Cross4157e882019-06-06 16:57:04 -07001465func (m *ModuleBase) setArchProperties(ctx BottomUpMutatorContext) {
1466 arch := m.Arch()
1467 os := m.Os()
Colin Crossd3ba0392015-05-07 14:11:29 -07001468
Ustadca02192021-12-20 12:56:46 -05001469 for i := range m.archProperties {
Usta851a3272022-01-05 23:42:33 -05001470 genProps := m.GetProperties()[i]
Colin Cross4157e882019-06-06 16:57:04 -07001471 if m.archProperties[i] == nil {
Dan Willemsenb1957a52016-06-23 23:44:54 -07001472 continue
1473 }
Dan Willemsenb1957a52016-06-23 23:44:54 -07001474
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001475 propStructs := make([]reflect.Value, 0)
1476 for _, archProperty := range m.archProperties[i] {
1477 propStructShard := getArchProperties(ctx, archProperty, arch, os, m.Target().NativeBridge == NativeBridgeEnabled)
1478 propStructs = append(propStructs, propStructShard...)
1479 }
Dan Willemsenb1957a52016-06-23 23:44:54 -07001480
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001481 for _, propStruct := range propStructs {
1482 mergePropertyStruct(ctx, genProps, propStruct)
Colin Crossbb2e2b72016-12-08 17:23:53 -08001483 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001484 }
1485}
1486
Colin Cross0c66bc62021-07-20 09:47:41 -07001487// determineBuildOS stores the OS and architecture used for host targets used during the build into
Colin Cross528d67e2021-07-23 22:23:07 +00001488// config based on the runtime OS and architecture determined by Go and the product configuration.
Colin Cross0c66bc62021-07-20 09:47:41 -07001489func determineBuildOS(config *config) {
1490 config.BuildOS = func() OsType {
1491 switch runtime.GOOS {
1492 case "linux":
Colin Cross528d67e2021-07-23 22:23:07 +00001493 if Bool(config.productVariables.HostMusl) {
1494 return LinuxMusl
1495 }
Colin Cross0c66bc62021-07-20 09:47:41 -07001496 return Linux
1497 case "darwin":
1498 return Darwin
1499 default:
1500 panic(fmt.Sprintf("unsupported OS: %s", runtime.GOOS))
1501 }
1502 }()
1503
1504 config.BuildArch = func() ArchType {
1505 switch runtime.GOARCH {
1506 case "amd64":
1507 return X86_64
1508 default:
1509 panic(fmt.Sprintf("unsupported Arch: %s", runtime.GOARCH))
1510 }
1511 }()
1512
1513}
1514
Colin Crossa6845402020-11-16 15:08:19 -08001515// Convert the arch product variables into a list of targets for each OsType.
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001516func decodeTargetProductVariables(config *config) (map[OsType][]Target, error) {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001517 variables := config.productVariables
Dan Willemsen490fd492015-11-24 17:53:15 -08001518
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001519 targets := make(map[OsType][]Target)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001520 var targetErr error
1521
Liz Kammerb7f33662022-02-28 14:16:16 -05001522 type targetConfig struct {
1523 os OsType
1524 archName string
1525 archVariant *string
1526 cpuVariant *string
1527 abi []string
1528 nativeBridgeEnabled NativeBridgeSupport
1529 nativeBridgeHostArchName *string
1530 nativeBridgeRelativePath *string
1531 }
1532
1533 addTarget := func(target targetConfig) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001534 if targetErr != nil {
1535 return
Dan Willemsen490fd492015-11-24 17:53:15 -08001536 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001537
Liz Kammerb7f33662022-02-28 14:16:16 -05001538 arch, err := decodeArch(target.os, target.archName, target.archVariant, target.cpuVariant, target.abi)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001539 if err != nil {
1540 targetErr = err
1541 return
1542 }
Liz Kammerb7f33662022-02-28 14:16:16 -05001543 nativeBridgeRelativePathStr := String(target.nativeBridgeRelativePath)
1544 nativeBridgeHostArchNameStr := String(target.nativeBridgeHostArchName)
dimitry8d6dde82019-07-11 10:23:53 +02001545
1546 // Use guest arch as relative install path by default
Liz Kammerb7f33662022-02-28 14:16:16 -05001547 if target.nativeBridgeEnabled && nativeBridgeRelativePathStr == "" {
dimitry8d6dde82019-07-11 10:23:53 +02001548 nativeBridgeRelativePathStr = arch.ArchType.String()
1549 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001550
Jiyong Park1613e552020-09-14 19:43:17 +09001551 // A target is considered as HostCross if it's a host target which can't run natively on
1552 // the currently configured build machine (either because the OS is different or because of
1553 // the unsupported arch)
1554 hostCross := false
Liz Kammerb7f33662022-02-28 14:16:16 -05001555 if target.os.Class == Host {
Jiyong Park1613e552020-09-14 19:43:17 +09001556 var osSupported bool
Liz Kammerb7f33662022-02-28 14:16:16 -05001557 if target.os == config.BuildOS {
Jiyong Park1613e552020-09-14 19:43:17 +09001558 osSupported = true
Liz Kammerb7f33662022-02-28 14:16:16 -05001559 } else if config.BuildOS.Linux() && target.os.Linux() {
Jiyong Park1613e552020-09-14 19:43:17 +09001560 // LinuxBionic and Linux are compatible
1561 osSupported = true
1562 } else {
1563 osSupported = false
1564 }
1565
1566 var archSupported bool
1567 if arch.ArchType == Common {
1568 archSupported = true
1569 } else if arch.ArchType.Name == *variables.HostArch {
1570 archSupported = true
1571 } else if variables.HostSecondaryArch != nil && arch.ArchType.Name == *variables.HostSecondaryArch {
1572 archSupported = true
1573 } else {
1574 archSupported = false
1575 }
1576 if !osSupported || !archSupported {
1577 hostCross = true
1578 }
1579 }
1580
Liz Kammerb7f33662022-02-28 14:16:16 -05001581 targets[target.os] = append(targets[target.os],
Colin Crossa1ad8d12016-06-01 17:09:44 -07001582 Target{
Liz Kammerb7f33662022-02-28 14:16:16 -05001583 Os: target.os,
dimitry8d6dde82019-07-11 10:23:53 +02001584 Arch: arch,
Liz Kammerb7f33662022-02-28 14:16:16 -05001585 NativeBridge: target.nativeBridgeEnabled,
dimitry8d6dde82019-07-11 10:23:53 +02001586 NativeBridgeHostArchName: nativeBridgeHostArchNameStr,
1587 NativeBridgeRelativePath: nativeBridgeRelativePathStr,
Jiyong Park1613e552020-09-14 19:43:17 +09001588 HostCross: hostCross,
Colin Crossa1ad8d12016-06-01 17:09:44 -07001589 })
Dan Willemsen490fd492015-11-24 17:53:15 -08001590 }
1591
Colin Cross4225f652015-09-17 14:33:42 -07001592 if variables.HostArch == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001593 return nil, fmt.Errorf("No host primary architecture set")
Colin Cross4225f652015-09-17 14:33:42 -07001594 }
1595
Colin Crossa6845402020-11-16 15:08:19 -08001596 // The primary host target, which must always exist.
Liz Kammerb7f33662022-02-28 14:16:16 -05001597 addTarget(targetConfig{os: config.BuildOS, archName: *variables.HostArch, nativeBridgeEnabled: NativeBridgeDisabled})
Colin Cross4225f652015-09-17 14:33:42 -07001598
Colin Crossa6845402020-11-16 15:08:19 -08001599 // An optional secondary host target.
Colin Crosseeabb892015-11-20 13:07:51 -08001600 if variables.HostSecondaryArch != nil && *variables.HostSecondaryArch != "" {
Liz Kammerb7f33662022-02-28 14:16:16 -05001601 addTarget(targetConfig{os: config.BuildOS, archName: *variables.HostSecondaryArch, nativeBridgeEnabled: NativeBridgeDisabled})
Dan Willemsen490fd492015-11-24 17:53:15 -08001602 }
1603
Colin Crossa6845402020-11-16 15:08:19 -08001604 // Optional cross-compiled host targets, generally Windows.
Colin Crossff3ae9d2018-04-10 16:15:18 -07001605 if String(variables.CrossHost) != "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001606 crossHostOs := osByName(*variables.CrossHost)
1607 if crossHostOs == NoOsType {
1608 return nil, fmt.Errorf("Unknown cross host OS %q", *variables.CrossHost)
1609 }
1610
Colin Crossff3ae9d2018-04-10 16:15:18 -07001611 if String(variables.CrossHostArch) == "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001612 return nil, fmt.Errorf("No cross-host primary architecture set")
Dan Willemsen490fd492015-11-24 17:53:15 -08001613 }
1614
Colin Crossa6845402020-11-16 15:08:19 -08001615 // The primary cross-compiled host target.
Liz Kammerb7f33662022-02-28 14:16:16 -05001616 addTarget(targetConfig{os: crossHostOs, archName: *variables.CrossHostArch, nativeBridgeEnabled: NativeBridgeDisabled})
Dan Willemsen490fd492015-11-24 17:53:15 -08001617
Colin Crossa6845402020-11-16 15:08:19 -08001618 // An optional secondary cross-compiled host target.
Dan Willemsen490fd492015-11-24 17:53:15 -08001619 if variables.CrossHostSecondaryArch != nil && *variables.CrossHostSecondaryArch != "" {
Liz Kammerb7f33662022-02-28 14:16:16 -05001620 addTarget(targetConfig{os: crossHostOs, archName: *variables.CrossHostSecondaryArch, nativeBridgeEnabled: NativeBridgeDisabled})
Dan Willemsen490fd492015-11-24 17:53:15 -08001621 }
1622 }
1623
Colin Crossa6845402020-11-16 15:08:19 -08001624 // Optional device targets
Dan Willemsen3f32f032016-07-11 14:36:48 -07001625 if variables.DeviceArch != nil && *variables.DeviceArch != "" {
Colin Crossa6845402020-11-16 15:08:19 -08001626 // The primary device target.
Liz Kammerb7f33662022-02-28 14:16:16 -05001627 addTarget(targetConfig{
1628 os: Android,
1629 archName: *variables.DeviceArch,
1630 archVariant: variables.DeviceArchVariant,
1631 cpuVariant: variables.DeviceCpuVariant,
1632 abi: variables.DeviceAbi,
1633 nativeBridgeEnabled: NativeBridgeDisabled,
1634 })
Colin Cross4225f652015-09-17 14:33:42 -07001635
Colin Crossa6845402020-11-16 15:08:19 -08001636 // An optional secondary device target.
Dan Willemsen3f32f032016-07-11 14:36:48 -07001637 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" {
Liz Kammerb7f33662022-02-28 14:16:16 -05001638 addTarget(targetConfig{
1639 os: Android,
1640 archName: *variables.DeviceSecondaryArch,
1641 archVariant: variables.DeviceSecondaryArchVariant,
1642 cpuVariant: variables.DeviceSecondaryCpuVariant,
1643 abi: variables.DeviceSecondaryAbi,
1644 nativeBridgeEnabled: NativeBridgeDisabled,
1645 })
Colin Cross4225f652015-09-17 14:33:42 -07001646 }
dimitry1f33e402019-03-26 12:39:31 +01001647
Colin Crossa6845402020-11-16 15:08:19 -08001648 // An optional NativeBridge device target.
dimitry1f33e402019-03-26 12:39:31 +01001649 if variables.NativeBridgeArch != nil && *variables.NativeBridgeArch != "" {
Liz Kammerb7f33662022-02-28 14:16:16 -05001650 addTarget(targetConfig{
1651 os: Android,
1652 archName: *variables.NativeBridgeArch,
1653 archVariant: variables.NativeBridgeArchVariant,
1654 cpuVariant: variables.NativeBridgeCpuVariant,
1655 abi: variables.NativeBridgeAbi,
1656 nativeBridgeEnabled: NativeBridgeEnabled,
1657 nativeBridgeHostArchName: variables.DeviceArch,
1658 nativeBridgeRelativePath: variables.NativeBridgeRelativePath,
1659 })
dimitry1f33e402019-03-26 12:39:31 +01001660 }
1661
Colin Crossa6845402020-11-16 15:08:19 -08001662 // An optional secondary NativeBridge device target.
dimitry1f33e402019-03-26 12:39:31 +01001663 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" &&
1664 variables.NativeBridgeSecondaryArch != nil && *variables.NativeBridgeSecondaryArch != "" {
Liz Kammerb7f33662022-02-28 14:16:16 -05001665 addTarget(targetConfig{
1666 os: Android,
1667 archName: *variables.NativeBridgeSecondaryArch,
1668 archVariant: variables.NativeBridgeSecondaryArchVariant,
1669 cpuVariant: variables.NativeBridgeSecondaryCpuVariant,
1670 abi: variables.NativeBridgeSecondaryAbi,
1671 nativeBridgeEnabled: NativeBridgeEnabled,
1672 nativeBridgeHostArchName: variables.DeviceSecondaryArch,
1673 nativeBridgeRelativePath: variables.NativeBridgeSecondaryRelativePath,
1674 })
dimitry1f33e402019-03-26 12:39:31 +01001675 }
Colin Cross4225f652015-09-17 14:33:42 -07001676 }
1677
Colin Crossa1ad8d12016-06-01 17:09:44 -07001678 if targetErr != nil {
1679 return nil, targetErr
1680 }
1681
1682 return targets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001683}
1684
Colin Crossbb2e2b72016-12-08 17:23:53 -08001685// hasArmAbi returns true if arch has at least one arm ABI
1686func hasArmAbi(arch Arch) bool {
Jaewoong Jung3aff5782020-02-11 07:54:35 -08001687 return PrefixInList(arch.Abi, "arm")
Colin Crossbb2e2b72016-12-08 17:23:53 -08001688}
1689
Lev Rumyantsev34581212021-10-13 09:47:59 -07001690// hasArmAndroidArch returns true if targets has at least
1691// one arm Android arch (possibly native bridged)
Colin Cross4247f0d2017-04-13 16:56:14 -07001692func hasArmAndroidArch(targets []Target) bool {
1693 for _, target := range targets {
Lev Rumyantsev34581212021-10-13 09:47:59 -07001694 if target.Os == Android &&
1695 (target.Arch.ArchType == Arm || target.Arch.ArchType == Arm64) {
Victor Khimenko5eb8ec12018-03-21 20:30:54 +01001696 return true
1697 }
1698 }
1699 return false
1700}
1701
Colin Crossa6845402020-11-16 15:08:19 -08001702// archConfig describes a built-in configuration.
Dan Albert4098deb2016-10-19 14:04:41 -07001703type archConfig struct {
1704 arch string
1705 archVariant string
1706 cpuVariant string
1707 abi []string
1708}
1709
Dan Albertf1d14c72020-07-30 14:32:55 -07001710// getNdkAbisConfig returns the list of archConfigs that are used for bulding
1711// the API stubs and static libraries that are included in the NDK. These are
1712// built *without Neon*, because non-Neon is still supported and building these
1713// with Neon will break those users.
Dan Albert4098deb2016-10-19 14:04:41 -07001714func getNdkAbisConfig() []archConfig {
1715 return []archConfig{
Tamas Petzbca786d2021-01-20 18:56:33 +01001716 {"arm64", "armv8-a-branchprot", "", []string{"arm64-v8a"}},
Inseob Kim5219c0e2021-06-17 00:33:00 +09001717 {"arm", "armv7-a", "", []string{"armeabi-v7a"}},
Dan Albert4098deb2016-10-19 14:04:41 -07001718 {"x86_64", "", "", []string{"x86_64"}},
Inseob Kim5219c0e2021-06-17 00:33:00 +09001719 {"x86", "", "", []string{"x86"}},
Dan Albert4098deb2016-10-19 14:04:41 -07001720 }
1721}
1722
Colin Crossa6845402020-11-16 15:08:19 -08001723// getAmlAbisConfig returns a list of archConfigs for the ABIs supported by mainline modules.
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001724func getAmlAbisConfig() []archConfig {
1725 return []archConfig{
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001726 {"arm64", "armv8-a", "", []string{"arm64-v8a"}},
Inseob Kim5219c0e2021-06-17 00:33:00 +09001727 {"arm", "armv7-a-neon", "", []string{"armeabi-v7a"}},
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001728 {"x86_64", "", "", []string{"x86_64"}},
Inseob Kim5219c0e2021-06-17 00:33:00 +09001729 {"x86", "", "", []string{"x86"}},
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001730 }
1731}
1732
Colin Crossa6845402020-11-16 15:08:19 -08001733// decodeArchSettings converts a list of archConfigs into a list of Targets for the given OsType.
Liz Kammerb7f33662022-02-28 14:16:16 -05001734func decodeAndroidArchSettings(archConfigs []archConfig) ([]Target, error) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001735 var ret []Target
Dan Willemsen322acaf2016-01-12 23:07:05 -08001736
Dan Albert4098deb2016-10-19 14:04:41 -07001737 for _, config := range archConfigs {
Liz Kammerb7f33662022-02-28 14:16:16 -05001738 arch, err := decodeArch(Android, config.arch, &config.archVariant,
Colin Crossa74ca042019-01-31 14:31:51 -08001739 &config.cpuVariant, config.abi)
Dan Willemsen322acaf2016-01-12 23:07:05 -08001740 if err != nil {
1741 return nil, err
1742 }
Colin Cross3b19f5d2019-09-17 14:45:31 -07001743
Colin Crossa1ad8d12016-06-01 17:09:44 -07001744 ret = append(ret, Target{
1745 Os: Android,
1746 Arch: arch,
1747 })
Dan Willemsen322acaf2016-01-12 23:07:05 -08001748 }
1749
1750 return ret, nil
1751}
1752
Colin Crossa6845402020-11-16 15:08:19 -08001753// decodeArch converts a set of strings from product variables into an Arch struct.
Colin Crossa74ca042019-01-31 14:31:51 -08001754func decodeArch(os OsType, arch string, archVariant, cpuVariant *string, abi []string) (Arch, error) {
Colin Crossa6845402020-11-16 15:08:19 -08001755 // Verify the arch is valid
Colin Crosseeabb892015-11-20 13:07:51 -08001756 archType, ok := archTypeMap[arch]
1757 if !ok {
1758 return Arch{}, fmt.Errorf("unknown arch %q", arch)
1759 }
Colin Cross4225f652015-09-17 14:33:42 -07001760
Colin Crosseeabb892015-11-20 13:07:51 -08001761 a := Arch{
Colin Cross4225f652015-09-17 14:33:42 -07001762 ArchType: archType,
Colin Crossa6845402020-11-16 15:08:19 -08001763 ArchVariant: String(archVariant),
1764 CpuVariant: String(cpuVariant),
Colin Crossa74ca042019-01-31 14:31:51 -08001765 Abi: abi,
Colin Crosseeabb892015-11-20 13:07:51 -08001766 }
1767
Colin Crossa6845402020-11-16 15:08:19 -08001768 // Convert generic arch variants into the empty string.
Colin Crosseeabb892015-11-20 13:07:51 -08001769 if a.ArchVariant == a.ArchType.Name || a.ArchVariant == "generic" {
1770 a.ArchVariant = ""
1771 }
1772
Colin Crossa6845402020-11-16 15:08:19 -08001773 // Convert generic CPU variants into the empty string.
Colin Crosseeabb892015-11-20 13:07:51 -08001774 if a.CpuVariant == a.ArchType.Name || a.CpuVariant == "generic" {
1775 a.CpuVariant = ""
1776 }
1777
Liz Kammer2c2afe22022-02-11 11:35:03 -05001778 if a.ArchVariant != "" {
1779 if validArchVariants := archVariants[archType]; !InList(a.ArchVariant, validArchVariants) {
1780 return Arch{}, fmt.Errorf("[%q] unknown arch variant %q, support variants: %q", archType, a.ArchVariant, validArchVariants)
1781 }
1782 }
1783
1784 if a.CpuVariant != "" {
1785 if validCpuVariants := cpuVariants[archType]; !InList(a.CpuVariant, validCpuVariants) {
1786 return Arch{}, fmt.Errorf("[%q] unknown cpu variant %q, support variants: %q", archType, a.CpuVariant, validCpuVariants)
1787 }
1788 }
1789
Colin Crossa6845402020-11-16 15:08:19 -08001790 // Filter empty ABIs out of the list.
Colin Crosseeabb892015-11-20 13:07:51 -08001791 for i := 0; i < len(a.Abi); i++ {
1792 if a.Abi[i] == "" {
1793 a.Abi = append(a.Abi[:i], a.Abi[i+1:]...)
1794 i--
1795 }
1796 }
1797
Liz Kammere8303bd2022-02-16 09:02:48 -05001798 // Set ArchFeatures from the arch type. for Android OS, other os-es do not specify features
1799 if os == Android {
1800 if featureMap, ok := androidArchFeatureMap[archType]; ok {
Dan Willemsen01a3c252019-01-11 19:02:16 -08001801 a.ArchFeatures = featureMap[a.ArchVariant]
1802 }
Colin Crossc5c24ad2015-11-20 15:35:00 -08001803 }
1804
Colin Crosseeabb892015-11-20 13:07:51 -08001805 return a, nil
Colin Cross4225f652015-09-17 14:33:42 -07001806}
1807
Colin Crossa6845402020-11-16 15:08:19 -08001808// filterMultilibTargets takes a list of Targets and a multilib value and returns a new list of
1809// Targets containing only those that have the given multilib value.
Colin Cross69617d32016-09-06 10:39:07 -07001810func filterMultilibTargets(targets []Target, multilib string) []Target {
1811 var ret []Target
1812 for _, t := range targets {
1813 if t.Arch.ArchType.Multilib == multilib {
1814 ret = append(ret, t)
1815 }
1816 }
1817 return ret
1818}
1819
Colin Crossa6845402020-11-16 15:08:19 -08001820// getCommonTargets returns the set of Os specific common architecture targets for each Os in a list
1821// of targets.
Nan Zhangdb0b9a32017-02-27 10:12:13 -08001822func getCommonTargets(targets []Target) []Target {
1823 var ret []Target
1824 set := make(map[string]bool)
1825
1826 for _, t := range targets {
1827 if _, found := set[t.Os.String()]; !found {
1828 set[t.Os.String()] = true
1829 ret = append(ret, commonTargetMap[t.Os.String()])
1830 }
1831 }
1832
1833 return ret
1834}
1835
Colin Crossa6845402020-11-16 15:08:19 -08001836// firstTarget takes a list of Targets and a list of multilib values and returns a list of Targets
1837// that contains zero or one Target for each OsType, selecting the one that matches the earliest
1838// filter.
Colin Cross3dceee32018-09-06 10:19:57 -07001839func firstTarget(targets []Target, filters ...string) []Target {
Jiyong Park22101982020-09-17 19:09:58 +09001840 // find the first target from each OS
1841 var ret []Target
1842 hasHost := false
1843 set := make(map[OsType]bool)
1844
Colin Cross6b4a32d2017-12-05 13:42:45 -08001845 for _, filter := range filters {
1846 buildTargets := filterMultilibTargets(targets, filter)
Jiyong Park22101982020-09-17 19:09:58 +09001847 for _, t := range buildTargets {
1848 if _, found := set[t.Os]; !found {
1849 hasHost = hasHost || (t.Os.Class == Host)
1850 set[t.Os] = true
1851 ret = append(ret, t)
1852 }
Colin Cross6b4a32d2017-12-05 13:42:45 -08001853 }
1854 }
Jiyong Park22101982020-09-17 19:09:58 +09001855 return ret
Colin Cross6b4a32d2017-12-05 13:42:45 -08001856}
1857
Colin Crossa6845402020-11-16 15:08:19 -08001858// decodeMultilibTargets uses the module's multilib setting to select one or more targets from a
1859// list of Targets.
Colin Crossee0bc3b2018-10-02 22:01:37 -07001860func decodeMultilibTargets(multilib string, targets []Target, prefer32 bool) ([]Target, error) {
Colin Crossa6845402020-11-16 15:08:19 -08001861 var buildTargets []Target
Colin Cross6b4a32d2017-12-05 13:42:45 -08001862
Colin Cross4225f652015-09-17 14:33:42 -07001863 switch multilib {
1864 case "common":
Colin Cross6b4a32d2017-12-05 13:42:45 -08001865 buildTargets = getCommonTargets(targets)
1866 case "common_first":
1867 buildTargets = getCommonTargets(targets)
1868 if prefer32 {
Colin Cross3dceee32018-09-06 10:19:57 -07001869 buildTargets = append(buildTargets, firstTarget(targets, "lib32", "lib64")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001870 } else {
Colin Cross3dceee32018-09-06 10:19:57 -07001871 buildTargets = append(buildTargets, firstTarget(targets, "lib64", "lib32")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001872 }
Colin Cross4225f652015-09-17 14:33:42 -07001873 case "both":
Colin Cross8b74d172016-09-13 09:59:14 -07001874 if prefer32 {
1875 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1876 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1877 } else {
1878 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1879 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1880 }
Colin Cross4225f652015-09-17 14:33:42 -07001881 case "32":
Colin Cross69617d32016-09-06 10:39:07 -07001882 buildTargets = filterMultilibTargets(targets, "lib32")
Colin Cross4225f652015-09-17 14:33:42 -07001883 case "64":
Colin Cross69617d32016-09-06 10:39:07 -07001884 buildTargets = filterMultilibTargets(targets, "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001885 case "first":
1886 if prefer32 {
Colin Cross3dceee32018-09-06 10:19:57 -07001887 buildTargets = firstTarget(targets, "lib32", "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001888 } else {
Colin Cross3dceee32018-09-06 10:19:57 -07001889 buildTargets = firstTarget(targets, "lib64", "lib32")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001890 }
Victor Chang9448e8f2020-09-14 15:34:16 +01001891 case "first_prefer32":
1892 buildTargets = firstTarget(targets, "lib32", "lib64")
Colin Cross69617d32016-09-06 10:39:07 -07001893 case "prefer32":
Colin Cross3dceee32018-09-06 10:19:57 -07001894 buildTargets = filterMultilibTargets(targets, "lib32")
1895 if len(buildTargets) == 0 {
1896 buildTargets = filterMultilibTargets(targets, "lib64")
1897 }
Dan Willemsen47450072021-10-19 20:24:49 -07001898 case "darwin_universal":
1899 buildTargets = filterMultilibTargets(targets, "lib64")
1900 // Reverse the targets so that the first architecture can depend on the second
1901 // architecture module in order to merge the outputs.
1902 reverseSliceInPlace(buildTargets)
1903 case "darwin_universal_common_first":
1904 archTargets := filterMultilibTargets(targets, "lib64")
1905 reverseSliceInPlace(archTargets)
1906 buildTargets = append(getCommonTargets(targets), archTargets...)
Colin Cross4225f652015-09-17 14:33:42 -07001907 default:
Victor Chang9448e8f2020-09-14 15:34:16 +01001908 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 -07001909 multilib)
Colin Cross4225f652015-09-17 14:33:42 -07001910 }
1911
Colin Crossa1ad8d12016-06-01 17:09:44 -07001912 return buildTargets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001913}
Jingwen Chen5d864492021-02-24 07:20:12 -05001914
Chris Parsonsc424b762021-04-29 18:06:50 -04001915func (m *ModuleBase) getArchPropertySet(propertySet interface{}, archType ArchType) interface{} {
1916 archString := archType.Field
1917 for i := range m.archProperties {
1918 if m.archProperties[i] == nil {
1919 // Skip over nil properties
1920 continue
1921 }
1922
1923 // Not archProperties are usable; this function looks for properties of a very specific
1924 // form, and ignores the rest.
1925 for _, archProperty := range m.archProperties[i] {
1926 // archPropValue is a property struct, we are looking for the form:
1927 // `arch: { arm: { key: value, ... }}`
1928 archPropValue := reflect.ValueOf(archProperty).Elem()
1929
1930 // Unwrap src so that it should looks like a pointer to `arm: { key: value, ... }`
1931 src := archPropValue.FieldByName("Arch").Elem()
1932
1933 // Step into non-nil pointers to structs in the src value.
1934 if src.Kind() == reflect.Ptr {
1935 if src.IsNil() {
1936 continue
1937 }
1938 src = src.Elem()
1939 }
1940
1941 // Find the requested field (e.g. arm, x86) in the src struct.
1942 src = src.FieldByName(archString)
1943
1944 // We only care about structs.
1945 if !src.IsValid() || src.Kind() != reflect.Struct {
1946 continue
1947 }
1948
1949 // If the value of the field is a struct then step into the
1950 // BlueprintEmbed field. The special "BlueprintEmbed" name is
1951 // used by createArchPropTypeDesc to embed the arch properties
1952 // in the parent struct, so the src arch prop should be in this
1953 // field.
1954 //
1955 // See createArchPropTypeDesc for more details on how Arch-specific
1956 // module properties are processed from the nested props and written
1957 // into the module's archProperties.
1958 src = src.FieldByName("BlueprintEmbed")
1959
1960 // Clone the destination prop, since we want a unique prop struct per arch.
1961 propertySetClone := reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
1962
1963 // Copy the located property struct into the cloned destination property struct.
1964 err := proptools.ExtendMatchingProperties([]interface{}{propertySetClone}, src.Interface(), nil, proptools.OrderReplace)
1965 if err != nil {
1966 // This is fine, it just means the src struct doesn't match the type of propertySet.
1967 continue
1968 }
1969
1970 return propertySetClone
1971 }
1972 }
1973 // No property set was found specific to the given arch, so return an empty
1974 // property set.
1975 return reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
1976}
1977
1978// getMultilibPropertySet returns a property set struct matching the type of
1979// `propertySet`, containing multilib-specific module properties for the given architecture.
1980// If no multilib-specific properties exist for the given architecture, returns an empty property
1981// set matching `propertySet`'s type.
1982func (m *ModuleBase) getMultilibPropertySet(propertySet interface{}, archType ArchType) interface{} {
1983 // archType.Multilib is lowercase (for example, lib32) but property struct field is
1984 // capitalized, such as Lib32, so use strings.Title to capitalize it.
1985 multiLibString := strings.Title(archType.Multilib)
1986
1987 for i := range m.archProperties {
1988 if m.archProperties[i] == nil {
1989 // Skip over nil properties
1990 continue
1991 }
1992
1993 // Not archProperties are usable; this function looks for properties of a very specific
1994 // form, and ignores the rest.
1995 for _, archProperties := range m.archProperties[i] {
1996 // archPropValue is a property struct, we are looking for the form:
1997 // `multilib: { lib32: { key: value, ... }}`
1998 archPropValue := reflect.ValueOf(archProperties).Elem()
1999
2000 // Unwrap src so that it should looks like a pointer to `lib32: { key: value, ... }`
2001 src := archPropValue.FieldByName("Multilib").Elem()
2002
2003 // Step into non-nil pointers to structs in the src value.
2004 if src.Kind() == reflect.Ptr {
2005 if src.IsNil() {
2006 // Ignore nil pointers.
2007 continue
2008 }
2009 src = src.Elem()
2010 }
2011
2012 // Find the requested field (e.g. lib32) in the src struct.
2013 src = src.FieldByName(multiLibString)
2014
2015 // We only care about valid struct pointers.
2016 if !src.IsValid() || src.Kind() != reflect.Ptr || src.Elem().Kind() != reflect.Struct {
2017 continue
2018 }
2019
2020 // Get the zero value for the requested property set.
2021 propertySetClone := reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
2022
2023 // Copy the located property struct into the "zero" property set struct.
2024 err := proptools.ExtendMatchingProperties([]interface{}{propertySetClone}, src.Interface(), nil, proptools.OrderReplace)
2025
2026 if err != nil {
2027 // This is fine, it just means the src struct doesn't match.
2028 continue
2029 }
2030
2031 return propertySetClone
2032 }
2033 }
2034
2035 // There were no multilib properties specifically matching the given archtype.
2036 // Return zeroed value.
2037 return reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
2038}
2039
Liz Kammerb6dbc872021-05-14 15:14:40 -04002040// ArchVariantContext defines the limited context necessary to retrieve arch_variant properties.
2041type ArchVariantContext interface {
2042 ModuleErrorf(fmt string, args ...interface{})
2043 PropertyErrorf(property, fmt string, args ...interface{})
2044}
2045
Liz Kammer9abd62d2021-05-21 08:37:59 -04002046// ArchVariantProperties represents a map of arch-variant config strings to a property interface{}.
2047type ArchVariantProperties map[string]interface{}
2048
2049// ConfigurationAxisToArchVariantProperties represents a map of bazel.ConfigurationAxis to
2050// ArchVariantProperties, such that each independent arch-variant axis maps to the
2051// configs/properties for that axis.
2052type ConfigurationAxisToArchVariantProperties map[bazel.ConfigurationAxis]ArchVariantProperties
2053
2054// GetArchVariantProperties returns a ConfigurationAxisToArchVariantProperties where the
2055// arch-variant properties correspond to the values of the properties of the 'propertySet' struct
2056// that are specific to that axis/configuration. Each axis is independent, containing
2057// non-overlapping configs that correspond to the various "arch-variant" support, at this time:
2058// arches (including multilib)
2059// oses
2060// arch+os combinations
Jingwen Chen5d864492021-02-24 07:20:12 -05002061//
Liz Kammer9abd62d2021-05-21 08:37:59 -04002062// For example, passing a struct { Foo bool, Bar string } will return an interface{} that can be
2063// type asserted back into the same struct, containing the config-specific property value specified
2064// by the module if defined.
Chris Parsonsc424b762021-04-29 18:06:50 -04002065//
2066// Arch-specific properties may come from an arch stanza or a multilib stanza; properties
2067// in these stanzas are combined.
2068// For example: `arch: { x86: { Foo: ["bar"] } }, multilib: { lib32: {` Foo: ["baz"] } }`
2069// will result in `Foo: ["bar", "baz"]` being returned for architecture x86, if the given
2070// propertyset contains `Foo []string`.
Liz Kammer9abd62d2021-05-21 08:37:59 -04002071func (m *ModuleBase) GetArchVariantProperties(ctx ArchVariantContext, propertySet interface{}) ConfigurationAxisToArchVariantProperties {
Jingwen Chen5d864492021-02-24 07:20:12 -05002072 // Return value of the arch types to the prop values for that arch.
Liz Kammer9abd62d2021-05-21 08:37:59 -04002073 axisToProps := ConfigurationAxisToArchVariantProperties{}
Jingwen Chen5d864492021-02-24 07:20:12 -05002074
2075 // Nothing to do for non-arch-specific modules.
2076 if !m.ArchSpecific() {
Liz Kammer9abd62d2021-05-21 08:37:59 -04002077 return axisToProps
Jingwen Chen5d864492021-02-24 07:20:12 -05002078 }
2079
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002080 dstType := reflect.ValueOf(propertySet).Type()
2081 var archProperties []interface{}
2082
2083 // First find the property set in the module that corresponds to the requested
Usta851a3272022-01-05 23:42:33 -05002084 // one. m.archProperties[i] corresponds to m.GetProperties()[i].
2085 for i, generalProp := range m.GetProperties() {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002086 srcType := reflect.ValueOf(generalProp).Type()
2087 if srcType == dstType {
2088 archProperties = m.archProperties[i]
Liz Kammer135bf552021-08-11 10:46:06 -04002089 axisToProps[bazel.NoConfigAxis] = ArchVariantProperties{"": generalProp}
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002090 break
2091 }
2092 }
2093
2094 if archProperties == nil {
2095 // This module does not have the property set requested
Liz Kammer9abd62d2021-05-21 08:37:59 -04002096 return axisToProps
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002097 }
2098
Liz Kammer9abd62d2021-05-21 08:37:59 -04002099 archToProp := ArchVariantProperties{}
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002100 // For each arch type (x86, arm64, etc.)
Chris Parsonsc424b762021-04-29 18:06:50 -04002101 for _, arch := range ArchTypeList() {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002102 // Arch properties are sometimes sharded (see createArchPropTypeDesc() ).
2103 // Iterate over ever shard and extract a struct with the same type as the
2104 // input one that contains the data specific to that arch.
2105 propertyStructs := make([]reflect.Value, 0)
2106 for _, archProperty := range archProperties {
Lukacs T. Berki5f518392021-05-17 11:44:58 +02002107 archTypeStruct, ok := getArchTypeStruct(ctx, archProperty, arch)
2108 if ok {
2109 propertyStructs = append(propertyStructs, archTypeStruct)
2110 }
2111 multilibStruct, ok := getMultilibStruct(ctx, archProperty, arch)
2112 if ok {
2113 propertyStructs = append(propertyStructs, multilibStruct)
2114 }
Jingwen Chen5d864492021-02-24 07:20:12 -05002115 }
2116
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002117 // Create a new instance of the requested property set
2118 value := reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
2119
Chris Parsonsa37e1952021-09-28 16:47:36 -04002120 archToProp[arch.Name] = mergeStructs(ctx, propertyStructs, value)
Jingwen Chen5d864492021-02-24 07:20:12 -05002121 }
Liz Kammer9abd62d2021-05-21 08:37:59 -04002122 axisToProps[bazel.ArchConfigurationAxis] = archToProp
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002123
Liz Kammer9abd62d2021-05-21 08:37:59 -04002124 osToProp := ArchVariantProperties{}
2125 archOsToProp := ArchVariantProperties{}
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002126
Liz Kammerfdd72e62021-10-11 15:41:03 -04002127 linuxStructs := getTargetStructs(ctx, archProperties, "Linux")
2128 bionicStructs := getTargetStructs(ctx, archProperties, "Bionic")
2129 hostStructs := getTargetStructs(ctx, archProperties, "Host")
2130 hostNotWindowsStructs := getTargetStructs(ctx, archProperties, "Not_windows")
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002131
Liz Kammer9abd62d2021-05-21 08:37:59 -04002132 // For android, linux, ...
2133 for _, os := range osTypeList {
2134 if os == CommonOS {
2135 // It looks like this OS value is not used in Blueprint files
2136 continue
2137 }
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002138 osStructs := make([]reflect.Value, 0)
Liz Kammerfdd72e62021-10-11 15:41:03 -04002139
2140 osSpecificStructs := getTargetStructs(ctx, archProperties, os.Field)
2141 if os.Class == Host {
2142 osStructs = append(osStructs, hostStructs...)
Chris Parsonsa37e1952021-09-28 16:47:36 -04002143 }
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002144 if os.Linux() {
2145 osStructs = append(osStructs, linuxStructs...)
2146 }
2147 if os.Bionic() {
2148 osStructs = append(osStructs, bionicStructs...)
2149 }
Liz Kammerfdd72e62021-10-11 15:41:03 -04002150
2151 if os == LinuxMusl {
2152 osStructs = append(osStructs, getTargetStructs(ctx, archProperties, "Musl")...)
2153 }
2154 if os == Linux {
2155 osStructs = append(osStructs, getTargetStructs(ctx, archProperties, "Glibc")...)
2156 }
2157
2158 osStructs = append(osStructs, osSpecificStructs...)
2159
2160 if os.Class == Host && os != Windows {
2161 osStructs = append(osStructs, hostNotWindowsStructs...)
2162 }
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002163 osToProp[os.Name] = mergeStructs(ctx, osStructs, propertySet)
Chris Parsonsa37e1952021-09-28 16:47:36 -04002164
Liz Kammer9abd62d2021-05-21 08:37:59 -04002165 // For arm, x86, ...
2166 for _, arch := range osArchTypeMap[os] {
Chris Parsonsa37e1952021-09-28 16:47:36 -04002167 osArchStructs := make([]reflect.Value, 0)
2168
Chris Parsonsa37e1952021-09-28 16:47:36 -04002169 // Auto-combine with Linux_ and Bionic_ targets. This potentially results in
2170 // repetition and select() bloat, but use of Linux_* and Bionic_* targets is rare.
2171 // TODO(b/201423152): Look into cleanup.
2172 if os.Linux() {
2173 targetField := "Linux_" + arch.Name
Liz Kammerfdd72e62021-10-11 15:41:03 -04002174 targetStructs := getTargetStructs(ctx, archProperties, targetField)
2175 osArchStructs = append(osArchStructs, targetStructs...)
Chris Parsonsa37e1952021-09-28 16:47:36 -04002176 }
2177 if os.Bionic() {
2178 targetField := "Bionic_" + arch.Name
Liz Kammerfdd72e62021-10-11 15:41:03 -04002179 targetStructs := getTargetStructs(ctx, archProperties, targetField)
2180 osArchStructs = append(osArchStructs, targetStructs...)
Chris Parsonsa37e1952021-09-28 16:47:36 -04002181 }
2182
Liz Kammerfdd72e62021-10-11 15:41:03 -04002183 targetField := GetCompoundTargetField(os, arch)
2184 targetName := fmt.Sprintf("%s_%s", os.Name, arch.Name)
2185 targetStructs := getTargetStructs(ctx, archProperties, targetField)
2186 osArchStructs = append(osArchStructs, targetStructs...)
2187
Chris Parsonsa37e1952021-09-28 16:47:36 -04002188 archOsToProp[targetName] = mergeStructs(ctx, osArchStructs, propertySet)
Liz Kammer9abd62d2021-05-21 08:37:59 -04002189 }
2190 }
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002191
Liz Kammer9abd62d2021-05-21 08:37:59 -04002192 axisToProps[bazel.OsConfigurationAxis] = osToProp
2193 axisToProps[bazel.OsArchConfigurationAxis] = archOsToProp
Liz Kammer9abd62d2021-05-21 08:37:59 -04002194 return axisToProps
Jingwen Chen5d864492021-02-24 07:20:12 -05002195}
Jingwen Chen91220d72021-03-24 02:18:33 -04002196
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04002197// Returns a struct matching the propertySet interface, containing properties specific to the targetName
2198// For example, given these arguments:
2199// propertySet = BaseCompilerProperties
2200// targetName = "android_arm"
2201// And given this Android.bp fragment:
2202// target:
2203// android_arm: {
2204// srcs: ["foo.c"],
2205// }
2206// android_arm64: {
2207// srcs: ["bar.c"],
2208// }
2209// }
2210// This would return a BaseCompilerProperties with BaseCompilerProperties.Srcs = ["foo.c"]
Liz Kammerfdd72e62021-10-11 15:41:03 -04002211func getTargetStructs(ctx ArchVariantContext, archProperties []interface{}, targetName string) []reflect.Value {
2212 var propertyStructs []reflect.Value
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04002213 for _, archProperty := range archProperties {
2214 archPropValues := reflect.ValueOf(archProperty).Elem()
2215 targetProp := archPropValues.FieldByName("Target").Elem()
2216 targetStruct, ok := getChildPropertyStruct(ctx, targetProp, targetName, targetName)
2217 if ok {
2218 propertyStructs = append(propertyStructs, targetStruct)
Chris Parsonsa37e1952021-09-28 16:47:36 -04002219 } else {
Liz Kammerfdd72e62021-10-11 15:41:03 -04002220 return []reflect.Value{}
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04002221 }
2222 }
2223
Liz Kammerfdd72e62021-10-11 15:41:03 -04002224 return propertyStructs
Chris Parsonsa37e1952021-09-28 16:47:36 -04002225}
2226
2227func mergeStructs(ctx ArchVariantContext, propertyStructs []reflect.Value, propertySet interface{}) interface{} {
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04002228 // Create a new instance of the requested property set
2229 value := reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
2230
2231 // Merge all the structs together
2232 for _, propertyStruct := range propertyStructs {
2233 mergePropertyStruct(ctx, value, propertyStruct)
2234 }
2235
2236 return value
2237}
Liz Kammere8303bd2022-02-16 09:02:48 -05002238
2239func printArchTypeStarlarkDict(dict map[ArchType][]string) string {
2240 valDict := make(map[string]string, len(dict))
2241 for k, v := range dict {
2242 valDict[k.String()] = starlark_fmt.PrintStringList(v, 1)
2243 }
2244 return starlark_fmt.PrintDict(valDict, 0)
2245}
2246
2247func printArchTypeNestedStarlarkDict(dict map[ArchType]map[string][]string) string {
2248 valDict := make(map[string]string, len(dict))
2249 for k, v := range dict {
2250 valDict[k.String()] = starlark_fmt.PrintStringListDict(v, 1)
2251 }
2252 return starlark_fmt.PrintDict(valDict, 0)
2253}
2254
2255func StarlarkArchConfigurations() string {
2256 return fmt.Sprintf(`
2257_arch_to_variants = %s
2258
2259_arch_to_cpu_variants = %s
2260
2261_arch_to_features = %s
2262
2263_android_arch_feature_for_arch_variant = %s
2264
2265arch_to_variants = _arch_to_variants
2266arch_to_cpu_variants = _arch_to_cpu_variants
2267arch_to_features = _arch_to_features
2268android_arch_feature_for_arch_variants = _android_arch_feature_for_arch_variant
2269`, printArchTypeStarlarkDict(archVariants),
2270 printArchTypeStarlarkDict(cpuVariants),
2271 printArchTypeStarlarkDict(archFeatures),
2272 printArchTypeNestedStarlarkDict(androidArchFeatureMap),
2273 )
2274}