blob: 086e945da644aa7a1459a7cc02d5049c94f5ae5e [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"
Cole Faustc843b992022-08-02 18:06:50 -070022 "sort"
Colin Cross3f40fa42015-01-30 17:27:36 -080023 "strings"
Colin Crossf6566ed2015-03-24 11:13:38 -070024
Colin Crosscb0ac952021-07-20 13:17:15 -070025 "android/soong/bazel"
Liz Kammere8303bd2022-02-16 09:02:48 -050026 "android/soong/starlark_fmt"
Colin Crosscb0ac952021-07-20 13:17:15 -070027
Colin Cross0f7d2ef2019-10-16 11:03:10 -070028 "github.com/google/blueprint"
Colin Cross617b88a2020-08-24 18:04:09 -070029 "github.com/google/blueprint/bootstrap"
Colin Crossf6566ed2015-03-24 11:13:38 -070030 "github.com/google/blueprint/proptools"
Colin Cross3f40fa42015-01-30 17:27:36 -080031)
32
Colin Cross3f40fa42015-01-30 17:27:36 -080033/*
34Example blueprints file containing all variant property groups, with comment listing what type
35of variants get properties in that group:
36
37module {
38 arch: {
39 arm: {
40 // Host or device variants with arm architecture
41 },
42 arm64: {
43 // Host or device variants with arm64 architecture
44 },
Colin Cross3f40fa42015-01-30 17:27:36 -080045 x86: {
46 // Host or device variants with x86 architecture
47 },
48 x86_64: {
49 // Host or device variants with x86_64 architecture
50 },
51 },
52 multilib: {
53 lib32: {
54 // Host or device variants for 32-bit architectures
55 },
56 lib64: {
57 // Host or device variants for 64-bit architectures
58 },
59 },
60 target: {
61 android: {
Martin Stjernholme284b482020-09-23 21:03:27 +010062 // Device variants (implies Bionic)
Colin Cross3f40fa42015-01-30 17:27:36 -080063 },
64 host: {
65 // Host variants
66 },
Martin Stjernholme284b482020-09-23 21:03:27 +010067 bionic: {
68 // Bionic (device and host) variants
69 },
70 linux_bionic: {
71 // Bionic host variants
72 },
73 linux: {
74 // Bionic (device and host) and Linux glibc variants
75 },
Dan Willemsen5746bd42017-10-02 19:42:01 -070076 linux_glibc: {
Martin Stjernholme284b482020-09-23 21:03:27 +010077 // Linux host variants (using non-Bionic libc)
Colin Cross3f40fa42015-01-30 17:27:36 -080078 },
79 darwin: {
80 // Darwin host variants
81 },
82 windows: {
83 // Windows host variants
84 },
85 not_windows: {
86 // Non-windows host variants
87 },
Martin Stjernholme284b482020-09-23 21:03:27 +010088 android_arm: {
89 // Any <os>_<arch> combination restricts to that os and arch
90 },
Colin Cross3f40fa42015-01-30 17:27:36 -080091 },
92}
93*/
Colin Cross7d5136f2015-05-11 13:39:40 -070094
Colin Cross3f40fa42015-01-30 17:27:36 -080095// An Arch indicates a single CPU architecture.
96type Arch struct {
Colin Crossa6845402020-11-16 15:08:19 -080097 // The type of the architecture (arm, arm64, x86, or x86_64).
98 ArchType ArchType
99
100 // The variant of the architecture, for example "armv7-a" or "armv7-a-neon" for arm.
101 ArchVariant string
102
103 // The variant of the CPU, for example "cortex-a53" for arm64.
104 CpuVariant string
105
106 // The list of Android app ABIs supported by the CPU architecture, for example "arm64-v8a".
107 Abi []string
108
109 // The list of arch-specific features supported by the CPU architecture, for example "neon".
Colin Crossc5c24ad2015-11-20 15:35:00 -0800110 ArchFeatures []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800111}
112
Colin Crossa6845402020-11-16 15:08:19 -0800113// String returns the Arch as a string. The value is used as the name of the variant created
114// by archMutator.
Colin Cross3f40fa42015-01-30 17:27:36 -0800115func (a Arch) String() string {
Colin Crossd3ba0392015-05-07 14:11:29 -0700116 s := a.ArchType.String()
Colin Cross3f40fa42015-01-30 17:27:36 -0800117 if a.ArchVariant != "" {
118 s += "_" + a.ArchVariant
119 }
120 if a.CpuVariant != "" {
121 s += "_" + a.CpuVariant
122 }
123 return s
124}
125
Colin Crossa6845402020-11-16 15:08:19 -0800126// ArchType is used to define the 4 supported architecture types (arm, arm64, x86, x86_64), as
127// well as the "common" architecture used for modules that support multiple architectures, for
128// example Java modules.
Colin Cross3f40fa42015-01-30 17:27:36 -0800129type ArchType struct {
Colin Crossa6845402020-11-16 15:08:19 -0800130 // Name is the name of the architecture type, "arm", "arm64", "x86", or "x86_64".
131 Name string
132
133 // Field is the name of the field used in properties that refer to the architecture, e.g. "Arm64".
134 Field string
135
136 // Multilib is either "lib32" or "lib64" for 32-bit or 64-bit architectures.
Colin Crossec193632015-07-06 17:49:43 -0700137 Multilib string
Colin Cross3f40fa42015-01-30 17:27:36 -0800138}
139
Colin Crossa6845402020-11-16 15:08:19 -0800140// String returns the name of the ArchType.
141func (a ArchType) String() string {
142 return a.Name
143}
144
145const COMMON_VARIANT = "common"
146
147var (
148 archTypeList []ArchType
149
Colin Crossf05b0d32022-07-14 18:10:34 -0700150 Arm = newArch("arm", "lib32")
151 Arm64 = newArch("arm64", "lib64")
152 Riscv64 = newArch("riscv64", "lib64")
153 X86 = newArch("x86", "lib32")
154 X86_64 = newArch("x86_64", "lib64")
Colin Crossa6845402020-11-16 15:08:19 -0800155
156 Common = ArchType{
157 Name: COMMON_VARIANT,
158 }
159)
160
161var archTypeMap = map[string]ArchType{}
162
Colin Crossec193632015-07-06 17:49:43 -0700163func newArch(name, multilib string) ArchType {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700164 archType := ArchType{
Colin Crossec193632015-07-06 17:49:43 -0700165 Name: name,
Dan Willemsenb1957a52016-06-23 23:44:54 -0700166 Field: proptools.FieldNameForProperty(name),
Colin Crossec193632015-07-06 17:49:43 -0700167 Multilib: multilib,
Colin Cross3f40fa42015-01-30 17:27:36 -0800168 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700169 archTypeList = append(archTypeList, archType)
Colin Crossa6845402020-11-16 15:08:19 -0800170 archTypeMap[name] = archType
Dan Willemsenb1957a52016-06-23 23:44:54 -0700171 return archType
Colin Cross3f40fa42015-01-30 17:27:36 -0800172}
173
Ustaeabf0f32021-12-06 15:17:23 -0500174// ArchTypeList returns a slice copy of the 4 supported ArchTypes for arm,
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000175// arm64, x86 and x86_64.
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -0700176func ArchTypeList() []ArchType {
177 return append([]ArchType(nil), archTypeList...)
178}
179
Colin Crossa6845402020-11-16 15:08:19 -0800180// MarshalText allows an ArchType to be serialized through any encoder that supports
181// encoding.TextMarshaler.
Colin Cross74ba9622019-02-11 15:11:14 -0800182func (a ArchType) MarshalText() ([]byte, error) {
Jeongik Chabec4d032021-04-15 08:55:38 +0900183 return []byte(a.String()), nil
Colin Cross74ba9622019-02-11 15:11:14 -0800184}
185
Colin Crossa6845402020-11-16 15:08:19 -0800186var _ encoding.TextMarshaler = ArchType{}
Colin Cross74ba9622019-02-11 15:11:14 -0800187
Colin Crossa6845402020-11-16 15:08:19 -0800188// UnmarshalText allows an ArchType to be deserialized through any decoder that supports
189// encoding.TextUnmarshaler.
Colin Cross74ba9622019-02-11 15:11:14 -0800190func (a *ArchType) UnmarshalText(text []byte) error {
191 if u, ok := archTypeMap[string(text)]; ok {
192 *a = u
193 return nil
194 }
195
196 return fmt.Errorf("unknown ArchType %q", text)
197}
198
Colin Crossa6845402020-11-16 15:08:19 -0800199var _ encoding.TextUnmarshaler = &ArchType{}
Colin Crossa1ad8d12016-06-01 17:09:44 -0700200
Colin Crossa6845402020-11-16 15:08:19 -0800201// OsClass is an enum that describes whether a variant of a module runs on the host, on the device,
202// or is generic.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700203type OsClass int
204
205const (
Colin Crossa6845402020-11-16 15:08:19 -0800206 // Generic is used for variants of modules that are not OS-specific.
Dan Willemsen0e2d97b2016-11-28 17:50:06 -0800207 Generic OsClass = iota
Colin Crossa6845402020-11-16 15:08:19 -0800208 // Device is used for variants of modules that run on the device.
Dan Willemsen0e2d97b2016-11-28 17:50:06 -0800209 Device
Colin Crossa6845402020-11-16 15:08:19 -0800210 // Host is used for variants of modules that run on the host.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700211 Host
Colin Crossa1ad8d12016-06-01 17:09:44 -0700212)
213
Colin Crossa6845402020-11-16 15:08:19 -0800214// String returns the OsClass as a string.
Colin Cross67a5c132017-05-09 13:45:28 -0700215func (class OsClass) String() string {
216 switch class {
217 case Generic:
218 return "generic"
219 case Device:
220 return "device"
221 case Host:
222 return "host"
Colin Cross67a5c132017-05-09 13:45:28 -0700223 default:
224 panic(fmt.Errorf("unknown class %d", class))
225 }
226}
227
Colin Crossa6845402020-11-16 15:08:19 -0800228// OsType describes an OS variant of a module.
229type OsType struct {
230 // Name is the name of the OS. It is also used as the name of the property in Android.bp
231 // files.
232 Name string
233
234 // Field is the name of the OS converted to an exported field name, i.e. with the first
235 // character capitalized.
236 Field string
237
238 // Class is the OsClass of the OS.
239 Class OsClass
240
241 // DefaultDisabled is set when the module variants for the OS should not be created unless
242 // the module explicitly requests them. This is used to limit Windows cross compilation to
243 // only modules that need it.
244 DefaultDisabled bool
245}
246
247// String returns the name of the OsType.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700248func (os OsType) String() string {
249 return os.Name
Colin Cross54c71122016-06-01 17:09:44 -0700250}
251
Colin Crossa6845402020-11-16 15:08:19 -0800252// Bionic returns true if the OS uses the Bionic libc runtime, i.e. if the OS is Android or
253// is Linux with Bionic.
Dan Willemsen866b5632017-09-22 12:28:24 -0700254func (os OsType) Bionic() bool {
255 return os == Android || os == LinuxBionic
256}
257
Colin Crossa6845402020-11-16 15:08:19 -0800258// Linux returns true if the OS uses the Linux kernel, i.e. if the OS is Android or is Linux
259// with or without the Bionic libc runtime.
Dan Willemsen866b5632017-09-22 12:28:24 -0700260func (os OsType) Linux() bool {
Colin Cross528d67e2021-07-23 22:23:07 +0000261 return os == Android || os == Linux || os == LinuxBionic || os == LinuxMusl
Dan Willemsen866b5632017-09-22 12:28:24 -0700262}
263
Colin Crossa6845402020-11-16 15:08:19 -0800264// newOsType constructs an OsType and adds it to the global lists.
265func newOsType(name string, class OsClass, defDisabled bool, archTypes ...ArchType) OsType {
266 checkCalledFromInit()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700267 os := OsType{
268 Name: name,
Colin Crossa6845402020-11-16 15:08:19 -0800269 Field: proptools.FieldNameForProperty(name),
Colin Crossa1ad8d12016-06-01 17:09:44 -0700270 Class: class,
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800271
272 DefaultDisabled: defDisabled,
Colin Cross54c71122016-06-01 17:09:44 -0700273 }
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000274 osTypeList = append(osTypeList, os)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800275
276 if _, found := commonTargetMap[name]; found {
277 panic(fmt.Errorf("Found Os type duplicate during OsType registration: %q", name))
278 } else {
Colin Crosse9fe2942020-11-10 18:12:15 -0800279 commonTargetMap[name] = Target{Os: os, Arch: CommonArch}
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800280 }
Colin Crossa6845402020-11-16 15:08:19 -0800281 osArchTypeMap[os] = archTypes
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800282
Colin Crossa1ad8d12016-06-01 17:09:44 -0700283 return os
284}
285
Colin Crossa6845402020-11-16 15:08:19 -0800286// osByName returns the OsType that has the given name, or NoOsType if none match.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700287func osByName(name string) OsType {
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000288 for _, os := range osTypeList {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700289 if os.Name == name {
290 return os
291 }
292 }
293
294 return NoOsType
Dan Willemsen490fd492015-11-24 17:53:15 -0800295}
296
Colin Crossa6845402020-11-16 15:08:19 -0800297var (
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000298 // osTypeList contains a list of all the supported OsTypes, including ones not supported
Colin Crossa6845402020-11-16 15:08:19 -0800299 // by the current build host or the target device.
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000300 osTypeList []OsType
Colin Crossa6845402020-11-16 15:08:19 -0800301 // commonTargetMap maps names of OsTypes to the corresponding common Target, i.e. the
302 // Target with the same OsType and the common ArchType.
303 commonTargetMap = make(map[string]Target)
304 // osArchTypeMap maps OsTypes to the list of supported ArchTypes for that OS.
305 osArchTypeMap = map[OsType][]ArchType{}
306
307 // NoOsType is a placeholder for when no OS is needed.
308 NoOsType OsType
309 // Linux is the OS for the Linux kernel plus the glibc runtime.
310 Linux = newOsType("linux_glibc", Host, false, X86, X86_64)
Colin Cross528d67e2021-07-23 22:23:07 +0000311 // LinuxMusl is the OS for the Linux kernel plus the musl runtime.
Colin Crossa9b2aac2022-06-15 17:25:51 -0700312 LinuxMusl = newOsType("linux_musl", Host, false, X86, X86_64, Arm64, Arm)
Colin Crossa6845402020-11-16 15:08:19 -0800313 // Darwin is the OS for MacOS/Darwin host machines.
Dan Willemsen8528f4e2021-10-19 00:22:06 -0700314 Darwin = newOsType("darwin", Host, false, Arm64, X86_64)
Colin Crossa6845402020-11-16 15:08:19 -0800315 // LinuxBionic is the OS for the Linux kernel plus the Bionic libc runtime, but without the
316 // rest of Android.
317 LinuxBionic = newOsType("linux_bionic", Host, false, Arm64, X86_64)
318 // Windows the OS for Windows host machines.
319 Windows = newOsType("windows", Host, true, X86, X86_64)
320 // Android is the OS for target devices that run all of Android, including the Linux kernel
321 // and the Bionic libc runtime.
Colin Crossf05b0d32022-07-14 18:10:34 -0700322 Android = newOsType("android", Device, false, Arm, Arm64, Riscv64, X86, X86_64)
Colin Crossa6845402020-11-16 15:08:19 -0800323
324 // CommonOS is a pseudo OSType for a common OS variant, which is OsType agnostic and which
325 // has dependencies on all the OS variants.
326 CommonOS = newOsType("common_os", Generic, false)
Colin Crosse9fe2942020-11-10 18:12:15 -0800327
328 // CommonArch is the Arch for all modules that are os-specific but not arch specific,
329 // for example most Java modules.
330 CommonArch = Arch{ArchType: Common}
dimitry1f33e402019-03-26 12:39:31 +0100331)
332
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000333// OsTypeList returns a slice copy of the supported OsTypes.
334func OsTypeList() []OsType {
335 return append([]OsType(nil), osTypeList...)
336}
337
Colin Crossa6845402020-11-16 15:08:19 -0800338// Target specifies the OS and architecture that a module is being compiled for.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700339type Target struct {
Colin Crossa6845402020-11-16 15:08:19 -0800340 // Os the OS that the module is being compiled for (e.g. "linux_glibc", "android").
341 Os OsType
342 // Arch is the architecture that the module is being compiled for.
343 Arch Arch
344 // NativeBridge is NativeBridgeEnabled if the architecture is supported using NativeBridge
345 // (i.e. arm on x86) for this device.
346 NativeBridge NativeBridgeSupport
347 // NativeBridgeHostArchName is the name of the real architecture that is used to implement
348 // the NativeBridge architecture. For example, for arm on x86 this would be "x86".
dimitry8d6dde82019-07-11 10:23:53 +0200349 NativeBridgeHostArchName string
Colin Crossa6845402020-11-16 15:08:19 -0800350 // NativeBridgeRelativePath is the name of the subdirectory that will contain NativeBridge
351 // libraries and binaries.
dimitry8d6dde82019-07-11 10:23:53 +0200352 NativeBridgeRelativePath string
Jiyong Park1613e552020-09-14 19:43:17 +0900353
354 // HostCross is true when the target cannot run natively on the current build host.
355 // For example, linux_glibc_x86 returns true on a regular x86/i686/Linux machines, but returns false
356 // on Mac (different OS), or on 64-bit only i686/Linux machines (unsupported arch).
357 HostCross bool
Colin Crossd3ba0392015-05-07 14:11:29 -0700358}
359
Colin Crossa6845402020-11-16 15:08:19 -0800360// NativeBridgeSupport is an enum that specifies if a Target supports NativeBridge.
361type NativeBridgeSupport bool
362
363const (
364 NativeBridgeDisabled NativeBridgeSupport = false
365 NativeBridgeEnabled NativeBridgeSupport = true
366)
367
368// String returns the OS and arch variations used for the Target.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700369func (target Target) String() string {
Colin Crossa195f912019-10-16 11:07:20 -0700370 return target.OsVariation() + "_" + target.ArchVariation()
371}
372
Colin Crossa6845402020-11-16 15:08:19 -0800373// OsVariation returns the name of the variation used by the osMutator for the Target.
Colin Crossa195f912019-10-16 11:07:20 -0700374func (target Target) OsVariation() string {
375 return target.Os.String()
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700376}
377
Colin Crossa6845402020-11-16 15:08:19 -0800378// ArchVariation returns the name of the variation used by the archMutator for the Target.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700379func (target Target) ArchVariation() string {
380 var variation string
dimitry1f33e402019-03-26 12:39:31 +0100381 if target.NativeBridge {
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700382 variation = "native_bridge_"
dimitry1f33e402019-03-26 12:39:31 +0100383 }
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700384 variation += target.Arch.String()
385
Colin Crossa195f912019-10-16 11:07:20 -0700386 return variation
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700387}
388
Colin Crossa6845402020-11-16 15:08:19 -0800389// Variations returns a list of blueprint.Variations for the osMutator and archMutator for the
390// Target.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700391func (target Target) Variations() []blueprint.Variation {
392 return []blueprint.Variation{
Colin Crossa195f912019-10-16 11:07:20 -0700393 {Mutator: "os", Variation: target.OsVariation()},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700394 {Mutator: "arch", Variation: target.ArchVariation()},
395 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800396}
397
Colin Crossa6845402020-11-16 15:08:19 -0800398// osMutator splits an arch-specific module into a variant for each OS that is enabled for the
399// module. It uses the HostOrDevice value passed to InitAndroidArchModule and the
400// device_supported and host_supported properties to determine which OsTypes are enabled for this
401// module, then searches through the Targets to determine which have enabled Targets for this
402// module.
Colin Cross617b88a2020-08-24 18:04:09 -0700403func osMutator(bpctx blueprint.BottomUpMutatorContext) {
Colin Crossa195f912019-10-16 11:07:20 -0700404 var module Module
405 var ok bool
Colin Cross617b88a2020-08-24 18:04:09 -0700406 if module, ok = bpctx.Module().(Module); !ok {
Colin Crossa6845402020-11-16 15:08:19 -0800407 // The module is not a Soong module, it is a Blueprint module.
Colin Cross617b88a2020-08-24 18:04:09 -0700408 if bootstrap.IsBootstrapModule(bpctx.Module()) {
409 // Bootstrap Go modules are always the build OS or linux bionic.
410 config := bpctx.Config().(Config)
411 osNames := []string{config.BuildOSTarget.OsVariation()}
412 for _, hostCrossTarget := range config.Targets[LinuxBionic] {
413 if hostCrossTarget.Arch.ArchType == config.BuildOSTarget.Arch.ArchType {
414 osNames = append(osNames, hostCrossTarget.OsVariation())
415 }
416 }
417 osNames = FirstUniqueStrings(osNames)
418 bpctx.CreateVariations(osNames...)
419 }
Colin Crossa195f912019-10-16 11:07:20 -0700420 return
421 }
422
Colin Cross617b88a2020-08-24 18:04:09 -0700423 // Bootstrap Go module support above requires this mutator to be a
424 // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext
425 // filters out non-Soong modules. Now that we've handled them, create a
426 // normal android.BottomUpMutatorContext.
Liz Kammer356f7d42021-01-26 09:18:53 -0500427 mctx := bottomUpMutatorContextFactory(bpctx, module, false, false)
Colin Cross617b88a2020-08-24 18:04:09 -0700428
Colin Crossa195f912019-10-16 11:07:20 -0700429 base := module.base()
430
Colin Crossa6845402020-11-16 15:08:19 -0800431 // Nothing to do for modules that are not architecture specific (e.g. a genrule).
Colin Crossa195f912019-10-16 11:07:20 -0700432 if !base.ArchSpecific() {
433 return
434 }
435
Colin Crossa6845402020-11-16 15:08:19 -0800436 // Collect a list of OSTypes supported by this module based on the HostOrDevice value
437 // passed to InitAndroidArchModule and the device_supported and host_supported properties.
Colin Crossa195f912019-10-16 11:07:20 -0700438 var moduleOSList []OsType
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000439 for _, os := range osTypeList {
Jiyong Park1613e552020-09-14 19:43:17 +0900440 for _, t := range mctx.Config().Targets[os] {
Colin Cross08d6f8f2020-11-19 02:33:19 +0000441 if base.supportsTarget(t) {
Jiyong Park1613e552020-09-14 19:43:17 +0900442 moduleOSList = append(moduleOSList, os)
443 break
Colin Crossa195f912019-10-16 11:07:20 -0700444 }
445 }
Colin Crossa195f912019-10-16 11:07:20 -0700446 }
447
Colin Crossa6845402020-11-16 15:08:19 -0800448 // If there are no supported OSes then disable the module.
Colin Crossa195f912019-10-16 11:07:20 -0700449 if len(moduleOSList) == 0 {
Inseob Kimeec88e12020-01-22 11:11:29 +0900450 base.Disable()
Colin Crossa195f912019-10-16 11:07:20 -0700451 return
452 }
453
Colin Crossa6845402020-11-16 15:08:19 -0800454 // Convert the list of supported OsTypes to the variation names.
Colin Crossa195f912019-10-16 11:07:20 -0700455 osNames := make([]string, len(moduleOSList))
Colin Crossa195f912019-10-16 11:07:20 -0700456 for i, os := range moduleOSList {
457 osNames[i] = os.String()
458 }
459
Paul Duffin1356d8c2020-02-25 19:26:33 +0000460 createCommonOSVariant := base.commonProperties.CreateCommonOSVariant
461 if createCommonOSVariant {
Colin Crossa6845402020-11-16 15:08:19 -0800462 // A CommonOS variant was requested so add it to the list of OS variants to
Paul Duffin1356d8c2020-02-25 19:26:33 +0000463 // create. It needs to be added to the end because it needs to depend on the
464 // the other variants in the list returned by CreateVariations(...) and inter
465 // variant dependencies can only be created from a later variant in that list to
466 // an earlier one. That is because variants are always processed in the order in
467 // which they are returned from CreateVariations(...).
468 osNames = append(osNames, CommonOS.Name)
469 moduleOSList = append(moduleOSList, CommonOS)
Colin Crossa195f912019-10-16 11:07:20 -0700470 }
471
Colin Crossa6845402020-11-16 15:08:19 -0800472 // Create the variations, annotate each one with which OS it was created for, and
473 // squash the appropriate OS-specific properties into the top level properties.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000474 modules := mctx.CreateVariations(osNames...)
475 for i, m := range modules {
476 m.base().commonProperties.CompileOS = moduleOSList[i]
477 m.base().setOSProperties(mctx)
478 }
479
480 if createCommonOSVariant {
481 // A CommonOS variant was requested so add dependencies from it (the last one in
482 // the list) to the OS type specific variants.
483 last := len(modules) - 1
484 commonOSVariant := modules[last]
485 commonOSVariant.base().commonProperties.CommonOSVariant = true
486 for _, module := range modules[0:last] {
487 // Ignore modules that are enabled. Note, this will only avoid adding
488 // dependencies on OsType variants that are explicitly disabled in their
489 // properties. The CommonOS variant will still depend on disabled variants
490 // if they are disabled afterwards, e.g. in archMutator if
491 if module.Enabled() {
492 mctx.AddInterVariantDependency(commonOsToOsSpecificVariantTag, commonOSVariant, module)
493 }
494 }
495 }
496}
497
Colin Crossc179ea62020-10-09 10:54:15 -0700498type archDepTag struct {
499 blueprint.BaseDependencyTag
500 name string
501}
Paul Duffin1356d8c2020-02-25 19:26:33 +0000502
Colin Crossc179ea62020-10-09 10:54:15 -0700503// Identifies the dependency from CommonOS variant to the os specific variants.
504var commonOsToOsSpecificVariantTag = archDepTag{name: "common os to os specific"}
505
Paul Duffin1356d8c2020-02-25 19:26:33 +0000506// Get the OsType specific variants for the current CommonOS variant.
507//
508// The returned list will only contain enabled OsType specific variants of the
509// module referenced in the supplied context. An empty list is returned if there
510// are no enabled variants or the supplied context is not for an CommonOS
511// variant.
512func GetOsSpecificVariantsOfCommonOSVariant(mctx BaseModuleContext) []Module {
513 var variants []Module
514 mctx.VisitDirectDeps(func(m Module) {
515 if mctx.OtherModuleDependencyTag(m) == commonOsToOsSpecificVariantTag {
516 if m.Enabled() {
517 variants = append(variants, m)
518 }
519 }
520 })
Paul Duffin1356d8c2020-02-25 19:26:33 +0000521 return variants
Colin Crossa195f912019-10-16 11:07:20 -0700522}
523
Dan Willemsen47450072021-10-19 20:24:49 -0700524var DarwinUniversalVariantTag = archDepTag{name: "darwin universal binary"}
525
Colin Crossee0bc3b2018-10-02 22:01:37 -0700526// archMutator splits a module into a variant for each Target requested by the module. Target selection
Colin Crossa6845402020-11-16 15:08:19 -0800527// for a module is in three levels, OsClass, multilib, and then Target.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700528// OsClass selection is determined by:
Colin Crossd079e0b2022-08-16 10:27:33 -0700529// - The HostOrDeviceSupported value passed in to InitAndroidArchModule by the module type factory, which selects
530// whether the module type can compile for host, device or both.
531// - The host_supported and device_supported properties on the module.
532//
Roland Levillainf5b635d2019-06-05 14:42:57 +0100533// 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 -0700534// for the module, the Device OsClass is selected.
535// Within each selected OsClass, the multilib selection is determined by:
Colin Crossd079e0b2022-08-16 10:27:33 -0700536// - The compile_multilib property if it set (which may be overridden by target.android.compile_multilib or
537// target.host.compile_multilib).
538// - The default multilib passed to InitAndroidArchModule if compile_multilib was not set.
539//
Colin Crossee0bc3b2018-10-02 22:01:37 -0700540// Valid multilib values include:
Colin Crossd079e0b2022-08-16 10:27:33 -0700541//
542// "both": compile for all Targets supported by the OsClass (generally x86_64 and x86, or arm64 and arm).
543// "first": compile for only a single preferred Target supported by the OsClass. This is generally x86_64 or arm64,
544// but may be arm for a 32-bit only build.
545// "32": compile for only a single 32-bit Target supported by the OsClass.
546// "64": compile for only a single 64-bit Target supported by the OsClass.
547// "common": compile a for a single Target that will work on all Targets supported by the OsClass (for example Java).
548// "common_first": compile a for a Target that will work on all Targets supported by the OsClass
549// (same as "common"), plus a second Target for the preferred Target supported by the OsClass
550// (same as "first"). This is used for java_binary that produces a common .jar and a wrapper
551// executable script.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700552//
553// Once the list of Targets is determined, the module is split into a variant for each Target.
554//
555// Modules can be initialized with InitAndroidMultiTargetsArchModule, in which case they will be split by OsClass,
556// but will have a common Target that is expected to handle all other selected Targets via ctx.MultiTargets().
Colin Cross617b88a2020-08-24 18:04:09 -0700557func archMutator(bpctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700558 var module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800559 var ok bool
Colin Cross617b88a2020-08-24 18:04:09 -0700560 if module, ok = bpctx.Module().(Module); !ok {
561 if bootstrap.IsBootstrapModule(bpctx.Module()) {
562 // Bootstrap Go modules are always the build architecture.
563 bpctx.CreateVariations(bpctx.Config().(Config).BuildOSTarget.ArchVariation())
564 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800565 return
566 }
567
Colin Cross617b88a2020-08-24 18:04:09 -0700568 // Bootstrap Go module support above requires this mutator to be a
569 // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext
570 // filters out non-Soong modules. Now that we've handled them, create a
571 // normal android.BottomUpMutatorContext.
Liz Kammer356f7d42021-01-26 09:18:53 -0500572 mctx := bottomUpMutatorContextFactory(bpctx, module, false, false)
Colin Cross617b88a2020-08-24 18:04:09 -0700573
Colin Cross5eca7cb2018-10-02 14:02:10 -0700574 base := module.base()
575
576 if !base.ArchSpecific() {
Colin Crossb9db4802016-06-03 01:50:47 +0000577 return
578 }
579
Colin Crossa195f912019-10-16 11:07:20 -0700580 os := base.commonProperties.CompileOS
Paul Duffin1356d8c2020-02-25 19:26:33 +0000581 if os == CommonOS {
582 // Make sure that the target related properties are initialized for the
583 // CommonOS variant.
584 addTargetProperties(module, commonTargetMap[os.Name], nil, true)
585
586 // Do not create arch specific variants for the CommonOS variant.
587 return
588 }
589
Colin Crossa195f912019-10-16 11:07:20 -0700590 osTargets := mctx.Config().Targets[os]
Colin Crossfb0c16e2019-11-20 17:12:35 -0800591 image := base.commonProperties.ImageVariation
Colin Crossa6845402020-11-16 15:08:19 -0800592 // Filter NativeBridge targets unless they are explicitly supported.
593 // Skip creating native bridge variants for non-core modules.
Paul Duffine3d1ae42021-09-03 17:47:17 +0100594 if os == Android && !(base.IsNativeBridgeSupported() && image == CoreVariation) {
Colin Cross83bead42019-12-18 10:45:46 -0800595
Colin Crossa195f912019-10-16 11:07:20 -0700596 var targets []Target
597 for _, t := range osTargets {
598 if !t.NativeBridge {
599 targets = append(targets, t)
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700600 }
601 }
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700602
Colin Crossa195f912019-10-16 11:07:20 -0700603 osTargets = targets
604 }
Colin Crossee0bc3b2018-10-02 22:01:37 -0700605
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700606 // only the primary arch in the ramdisk / vendor_ramdisk / recovery partition
Inseob Kim08758f02021-04-08 21:13:22 +0900607 if os == Android && (module.InstallInRecovery() || module.InstallInRamdisk() || module.InstallInVendorRamdisk() || module.InstallInDebugRamdisk()) {
Colin Crossa195f912019-10-16 11:07:20 -0700608 osTargets = []Target{osTargets[0]}
609 }
dimitry1f33e402019-03-26 12:39:31 +0100610
Jaewoong Jung003d8082021-02-24 17:39:54 -0800611 // Windows builds always prefer 32-bit
612 prefer32 := os == Windows
dimitry1f33e402019-03-26 12:39:31 +0100613
Colin Crossa6845402020-11-16 15:08:19 -0800614 // Determine the multilib selection for this module.
Christopher Ferris98f10222022-07-13 23:16:52 -0700615 ignorePrefer32OnDevice := mctx.Config().IgnorePrefer32OnDevice()
616 multilib, extraMultilib := decodeMultilib(base, os, ignorePrefer32OnDevice)
Colin Crossa6845402020-11-16 15:08:19 -0800617
618 // Convert the multilib selection into a list of Targets.
Colin Crossa195f912019-10-16 11:07:20 -0700619 targets, err := decodeMultilibTargets(multilib, osTargets, prefer32)
620 if err != nil {
621 mctx.ModuleErrorf("%s", err.Error())
622 }
Colin Cross5eca7cb2018-10-02 14:02:10 -0700623
Colin Crossc0f0eb82022-07-19 14:41:11 -0700624 // If there are no supported targets disable the module.
625 if len(targets) == 0 {
626 base.Disable()
627 return
628 }
629
Colin Crossa6845402020-11-16 15:08:19 -0800630 // If the module is using extraMultilib, decode the extraMultilib selection into
631 // a separate list of Targets.
Colin Crossa195f912019-10-16 11:07:20 -0700632 var multiTargets []Target
633 if extraMultilib != "" {
634 multiTargets, err = decodeMultilibTargets(extraMultilib, osTargets, prefer32)
Colin Crossa1ad8d12016-06-01 17:09:44 -0700635 if err != nil {
636 mctx.ModuleErrorf("%s", err.Error())
637 }
Colin Crossc0f0eb82022-07-19 14:41:11 -0700638 multiTargets = filterHostCross(multiTargets, targets[0].HostCross)
Colin Crossb9db4802016-06-03 01:50:47 +0000639 }
640
Colin Crossa6845402020-11-16 15:08:19 -0800641 // Recovery is always the primary architecture, filter out any other architectures.
Inseob Kim20fb5d42021-02-02 20:07:58 +0900642 // Common arch is also allowed
Colin Crossfb0c16e2019-11-20 17:12:35 -0800643 if image == RecoveryVariation {
644 primaryArch := mctx.Config().DevicePrimaryArchType()
Inseob Kim20fb5d42021-02-02 20:07:58 +0900645 targets = filterToArch(targets, primaryArch, Common)
646 multiTargets = filterToArch(multiTargets, primaryArch, Common)
Colin Crossfb0c16e2019-11-20 17:12:35 -0800647 }
648
Colin Crossa6845402020-11-16 15:08:19 -0800649 // If there are no supported targets disable the module.
Colin Crossa195f912019-10-16 11:07:20 -0700650 if len(targets) == 0 {
Inseob Kimeec88e12020-01-22 11:11:29 +0900651 base.Disable()
Dan Willemsen3f32f032016-07-11 14:36:48 -0700652 return
653 }
654
Colin Crossa6845402020-11-16 15:08:19 -0800655 // Convert the targets into a list of arch variation names.
Colin Crossa195f912019-10-16 11:07:20 -0700656 targetNames := make([]string, len(targets))
Colin Crossa195f912019-10-16 11:07:20 -0700657 for i, target := range targets {
658 targetNames[i] = target.ArchVariation()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700659 }
660
Colin Crossa6845402020-11-16 15:08:19 -0800661 // Create the variations, annotate each one with which Target it was created for, and
662 // squash the appropriate arch-specific properties into the top level properties.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700663 modules := mctx.CreateVariations(targetNames...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800664 for i, m := range modules {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000665 addTargetProperties(m, targets[i], multiTargets, i == 0)
Colin Cross617b88a2020-08-24 18:04:09 -0700666 m.base().setArchProperties(mctx)
Dan Willemsen8528f4e2021-10-19 00:22:06 -0700667
668 // Install support doesn't understand Darwin+Arm64
669 if os == Darwin && targets[i].HostCross {
670 m.base().commonProperties.SkipInstall = true
671 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800672 }
Dan Willemsen47450072021-10-19 20:24:49 -0700673
674 // Create a dependency for Darwin Universal binaries from the primary to secondary
675 // architecture. The module itself will be responsible for calling lipo to merge the outputs.
676 if os == Darwin {
677 if multilib == "darwin_universal" && len(modules) == 2 {
678 mctx.AddInterVariantDependency(DarwinUniversalVariantTag, modules[1], modules[0])
679 } else if multilib == "darwin_universal_common_first" && len(modules) == 3 {
680 mctx.AddInterVariantDependency(DarwinUniversalVariantTag, modules[2], modules[1])
681 }
682 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800683}
684
Colin Crossa6845402020-11-16 15:08:19 -0800685// addTargetProperties annotates a variant with the Target is is being compiled for, the list
686// of additional Targets it is supporting (if any), and whether it is the primary Target for
687// the module.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000688func addTargetProperties(m Module, target Target, multiTargets []Target, primaryTarget bool) {
689 m.base().commonProperties.CompileTarget = target
690 m.base().commonProperties.CompileMultiTargets = multiTargets
691 m.base().commonProperties.CompilePrimary = primaryTarget
692}
693
Colin Crossa6845402020-11-16 15:08:19 -0800694// decodeMultilib returns the appropriate compile_multilib property for the module, or the default
695// multilib from the factory's call to InitAndroidArchModule if none was set. For modules that
696// called InitAndroidMultiTargetsArchModule it always returns "common" for multilib, and returns
697// the actual multilib in extraMultilib.
Christopher Ferris98f10222022-07-13 23:16:52 -0700698func decodeMultilib(base *ModuleBase, os OsType, ignorePrefer32OnDevice bool) (multilib, extraMultilib string) {
Colin Crossa6845402020-11-16 15:08:19 -0800699 // First check the "android.compile_multilib" or "host.compile_multilib" properties.
Dan Willemsen47450072021-10-19 20:24:49 -0700700 switch os.Class {
Colin Crossee0bc3b2018-10-02 22:01:37 -0700701 case Device:
702 multilib = String(base.commonProperties.Target.Android.Compile_multilib)
Jiyong Park1613e552020-09-14 19:43:17 +0900703 case Host:
Colin Crossee0bc3b2018-10-02 22:01:37 -0700704 multilib = String(base.commonProperties.Target.Host.Compile_multilib)
705 }
Colin Crossa6845402020-11-16 15:08:19 -0800706
707 // If those aren't set, try the "compile_multilib" property.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700708 if multilib == "" {
709 multilib = String(base.commonProperties.Compile_multilib)
710 }
Colin Crossa6845402020-11-16 15:08:19 -0800711
712 // If that wasn't set, use the default multilib set by the factory.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700713 if multilib == "" {
714 multilib = base.commonProperties.Default_multilib
715 }
716
Christopher Ferris98f10222022-07-13 23:16:52 -0700717 // If a device is configured with multiple targets, this option
718 // force all device targets that prefer32 to be compiled only as
719 // the first target.
720 if ignorePrefer32OnDevice && os.Class == Device && (multilib == "prefer32" || multilib == "first_prefer32") {
721 multilib = "first"
722 }
723
Colin Crossee0bc3b2018-10-02 22:01:37 -0700724 if base.commonProperties.UseTargetVariants {
Dan Willemsen47450072021-10-19 20:24:49 -0700725 // Darwin has the concept of "universal binaries" which is implemented in Soong by
726 // building both x86_64 and arm64 variants, and having select module types know how to
727 // merge the outputs of their corresponding variants together into a final binary. Most
728 // module types don't need to understand this logic, as we only build a small portion
729 // of the tree for Darwin, and only module types writing macho files need to do the
730 // merging.
731 //
732 // This logic is not enabled for:
733 // "common", as it's not an arch-specific variant
734 // "32", as Darwin never has a 32-bit variant
735 // !UseTargetVariants, as the module has opted into handling the arch-specific logic on
736 // its own.
737 if os == Darwin && multilib != "common" && multilib != "32" {
738 if multilib == "common_first" {
739 multilib = "darwin_universal_common_first"
740 } else {
741 multilib = "darwin_universal"
742 }
743 }
744
Colin Crossee0bc3b2018-10-02 22:01:37 -0700745 return multilib, ""
746 } else {
747 // For app modules a single arch variant will be created per OS class which is expected to handle all the
748 // selected arches. Return the common-type as multilib and any Android.bp provided multilib as extraMultilib
749 if multilib == base.commonProperties.Default_multilib {
750 multilib = "first"
751 }
752 return base.commonProperties.Default_multilib, multilib
753 }
754}
755
Colin Crossa6845402020-11-16 15:08:19 -0800756// filterToArch takes a list of Targets and an ArchType, and returns a modified list that contains
Inseob Kim20fb5d42021-02-02 20:07:58 +0900757// only Targets that have the specified ArchTypes.
758func filterToArch(targets []Target, archs ...ArchType) []Target {
Colin Crossfb0c16e2019-11-20 17:12:35 -0800759 for i := 0; i < len(targets); i++ {
Inseob Kim20fb5d42021-02-02 20:07:58 +0900760 found := false
761 for _, arch := range archs {
762 if targets[i].Arch.ArchType == arch {
763 found = true
764 break
765 }
766 }
767 if !found {
Colin Crossfb0c16e2019-11-20 17:12:35 -0800768 targets = append(targets[:i], targets[i+1:]...)
769 i--
770 }
771 }
772 return targets
773}
774
Colin Crossc0f0eb82022-07-19 14:41:11 -0700775// filterHostCross takes a list of Targets and a hostCross value, and returns a modified list
776// that contains only Targets that have the specified HostCross.
777func filterHostCross(targets []Target, hostCross bool) []Target {
778 for i := 0; i < len(targets); i++ {
779 if targets[i].HostCross != hostCross {
780 targets = append(targets[:i], targets[i+1:]...)
781 i--
782 }
783 }
784 return targets
785}
786
Colin Crossa6845402020-11-16 15:08:19 -0800787// archPropRoot is a struct type used as the top level of the arch-specific properties. It
788// contains the "arch", "multilib", and "target" property structs. It is used to split up the
789// property structs to limit how much is allocated when a single arch-specific property group is
790// used. The types are interface{} because they will hold instances of runtime-created types.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800791type archPropRoot struct {
792 Arch, Multilib, Target interface{}
793}
794
Colin Crossa6845402020-11-16 15:08:19 -0800795// archPropTypeDesc holds the runtime-created types for the property structs to instantiate to
796// create an archPropRoot property struct.
797type archPropTypeDesc struct {
798 arch, multilib, target reflect.Type
799}
800
Colin Crosscbbd13f2020-01-17 14:08:22 -0800801// createArchPropTypeDesc takes a reflect.Type that is either a struct or a pointer to a struct, and
802// returns lists of reflect.Types that contains the arch-variant properties inside structs for each
803// arch, multilib and target property.
Colin Crossa6845402020-11-16 15:08:19 -0800804//
805// This is a relatively expensive operation, so the results are cached in the global
806// archPropTypeMap. It is constructed entirely based on compile-time data, so there is no need
807// to isolate the results between multiple tests running in parallel.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800808func createArchPropTypeDesc(props reflect.Type) []archPropTypeDesc {
Colin Crossb1d8c992020-01-21 11:43:29 -0800809 // Each property struct shard will be nested many times under the runtime generated arch struct,
810 // which can hit the limit of 64kB for the name of runtime generated structs. They are nested
811 // 97 times now, which may grow in the future, plus there is some overhead for the containing
812 // type. This number may need to be reduced if too many are added, but reducing it too far
813 // could cause problems if a single deeply nested property no longer fits in the name.
814 const maxArchTypeNameSize = 500
815
Colin Crossa6845402020-11-16 15:08:19 -0800816 // Convert the type to a new set of types that contains only the arch-specific properties
Usta Shrestha0b52d832022-02-04 21:37:39 -0500817 // (those that are tagged with `android:"arch_variant"`), and sharded into multiple types
Colin Crossa6845402020-11-16 15:08:19 -0800818 // to keep the runtime-generated names under the limit.
Colin Crossb1d8c992020-01-21 11:43:29 -0800819 propShards, _ := proptools.FilterPropertyStructSharded(props, maxArchTypeNameSize, filterArchStruct)
Colin Crossa6845402020-11-16 15:08:19 -0800820
821 // If the type has no arch-specific properties there is nothing to do.
Colin Crosscb988072019-01-24 14:58:11 -0800822 if len(propShards) == 0 {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700823 return nil
824 }
825
Colin Crosscbbd13f2020-01-17 14:08:22 -0800826 var ret []archPropTypeDesc
Colin Crossc17727d2018-10-24 12:42:09 -0700827 for _, props := range propShards {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700828
Colin Crossa6845402020-11-16 15:08:19 -0800829 // variantFields takes a list of variant property field names and returns a list the
830 // StructFields with the names and the type of the current shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700831 variantFields := func(names []string) []reflect.StructField {
832 ret := make([]reflect.StructField, len(names))
Dan Willemsenb1957a52016-06-23 23:44:54 -0700833
Colin Crossc17727d2018-10-24 12:42:09 -0700834 for i, name := range names {
835 ret[i].Name = name
836 ret[i].Type = props
Dan Willemsen866b5632017-09-22 12:28:24 -0700837 }
Colin Crossc17727d2018-10-24 12:42:09 -0700838
839 return ret
840 }
841
Colin Crossa6845402020-11-16 15:08:19 -0800842 // Create a type that contains the properties in this shard repeated for each
843 // architecture, architecture variant, and architecture feature.
Colin Crossc17727d2018-10-24 12:42:09 -0700844 archFields := make([]reflect.StructField, len(archTypeList))
845 for i, arch := range archTypeList {
Colin Crossa6845402020-11-16 15:08:19 -0800846 var variants []string
Colin Crossc17727d2018-10-24 12:42:09 -0700847
848 for _, archVariant := range archVariants[arch] {
849 archVariant := variantReplacer.Replace(archVariant)
850 variants = append(variants, proptools.FieldNameForProperty(archVariant))
851 }
Liz Kammer2c2afe22022-02-11 11:35:03 -0500852 for _, cpuVariant := range cpuVariants[arch] {
853 cpuVariant := variantReplacer.Replace(cpuVariant)
854 variants = append(variants, proptools.FieldNameForProperty(cpuVariant))
855 }
Colin Crossc17727d2018-10-24 12:42:09 -0700856 for _, feature := range archFeatures[arch] {
857 feature := variantReplacer.Replace(feature)
858 variants = append(variants, proptools.FieldNameForProperty(feature))
859 }
860
Colin Crossa6845402020-11-16 15:08:19 -0800861 // Create the StructFields for each architecture variant architecture feature
862 // (e.g. "arch.arm.cortex-a53" or "arch.arm.neon").
Colin Crossc17727d2018-10-24 12:42:09 -0700863 fields := variantFields(variants)
864
Colin Crossa6845402020-11-16 15:08:19 -0800865 // Create the StructField for the architecture itself (e.g. "arch.arm"). The special
866 // "BlueprintEmbed" name is used by Blueprint to put the properties in the
867 // parent struct.
Colin Crossc17727d2018-10-24 12:42:09 -0700868 fields = append([]reflect.StructField{{
869 Name: "BlueprintEmbed",
870 Type: props,
871 Anonymous: true,
872 }}, fields...)
873
874 archFields[i] = reflect.StructField{
875 Name: arch.Field,
876 Type: reflect.StructOf(fields),
877 }
878 }
Colin Crossa6845402020-11-16 15:08:19 -0800879
880 // Create the type of the "arch" property struct for this shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700881 archType := reflect.StructOf(archFields)
882
Colin Crossa6845402020-11-16 15:08:19 -0800883 // Create the type for the "multilib" property struct for this shard, containing the
884 // "multilib.lib32" and "multilib.lib64" property structs.
Colin Crossc17727d2018-10-24 12:42:09 -0700885 multilibType := reflect.StructOf(variantFields([]string{"Lib32", "Lib64"}))
886
Colin Crossa6845402020-11-16 15:08:19 -0800887 // Start with a list of the special targets
Colin Crossc17727d2018-10-24 12:42:09 -0700888 targets := []string{
889 "Host",
890 "Android64",
891 "Android32",
892 "Bionic",
Colin Cross528d67e2021-07-23 22:23:07 +0000893 "Glibc",
894 "Musl",
Colin Crossc17727d2018-10-24 12:42:09 -0700895 "Linux",
Colin Crossa98d36d2022-03-07 14:39:49 -0800896 "Host_linux",
Colin Crossc17727d2018-10-24 12:42:09 -0700897 "Not_windows",
898 "Arm_on_x86",
899 "Arm_on_x86_64",
Victor Khimenkoc26fcf42020-05-07 22:16:33 +0200900 "Native_bridge",
Colin Crossc17727d2018-10-24 12:42:09 -0700901 }
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000902 for _, os := range osTypeList {
Colin Crossa6845402020-11-16 15:08:19 -0800903 // Add all the OSes.
Colin Crossc17727d2018-10-24 12:42:09 -0700904 targets = append(targets, os.Field)
905
Colin Crossa6845402020-11-16 15:08:19 -0800906 // Add the OS/Arch combinations, e.g. "android_arm64".
Colin Crossc17727d2018-10-24 12:42:09 -0700907 for _, archType := range osArchTypeMap[os] {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400908 targets = append(targets, GetCompoundTargetField(os, archType))
Colin Crossc17727d2018-10-24 12:42:09 -0700909
Colin Cross1aa45b02022-02-10 10:33:10 -0800910 // Also add the special "linux_<arch>", "bionic_<arch>" , "glibc_<arch>", and
911 // "musl_<arch>" property structs.
Colin Crossc17727d2018-10-24 12:42:09 -0700912 if os.Linux() {
913 target := "Linux_" + archType.Name
914 if !InList(target, targets) {
915 targets = append(targets, target)
916 }
917 }
Colin Crossa98d36d2022-03-07 14:39:49 -0800918 if os.Linux() && os.Class == Host {
919 target := "Host_linux_" + archType.Name
920 if !InList(target, targets) {
921 targets = append(targets, target)
922 }
923 }
Colin Crossc17727d2018-10-24 12:42:09 -0700924 if os.Bionic() {
925 target := "Bionic_" + archType.Name
926 if !InList(target, targets) {
927 targets = append(targets, target)
928 }
Dan Willemsen866b5632017-09-22 12:28:24 -0700929 }
Colin Cross1aa45b02022-02-10 10:33:10 -0800930 if os == Linux {
931 target := "Glibc_" + archType.Name
932 if !InList(target, targets) {
933 targets = append(targets, target)
934 }
935 }
936 if os == LinuxMusl {
937 target := "Musl_" + archType.Name
938 if !InList(target, targets) {
939 targets = append(targets, target)
940 }
941 }
Dan Willemsen866b5632017-09-22 12:28:24 -0700942 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700943 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700944
Colin Crossa6845402020-11-16 15:08:19 -0800945 // Create the type for the "target" property struct for this shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700946 targetType := reflect.StructOf(variantFields(targets))
Colin Crosscbbd13f2020-01-17 14:08:22 -0800947
Colin Crossa6845402020-11-16 15:08:19 -0800948 // Return a descriptor of the 3 runtime-created types.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800949 ret = append(ret, archPropTypeDesc{
950 arch: reflect.PtrTo(archType),
951 multilib: reflect.PtrTo(multilibType),
952 target: reflect.PtrTo(targetType),
953 })
Colin Crossc17727d2018-10-24 12:42:09 -0700954 }
955 return ret
Dan Willemsenb1957a52016-06-23 23:44:54 -0700956}
957
Colin Crossa6845402020-11-16 15:08:19 -0800958// variantReplacer converts architecture variant or architecture feature names into names that
959// are valid for an Android.bp file.
960var variantReplacer = strings.NewReplacer("-", "_", ".", "_")
961
962// filterArchStruct returns true if the given field is an architecture specific property.
Colin Cross74449102019-09-25 11:26:40 -0700963func filterArchStruct(field reflect.StructField, prefix string) (bool, reflect.StructField) {
964 if proptools.HasTag(field, "android", "arch_variant") {
965 // The arch_variant field isn't necessary past this point
966 // Instead of wasting space, just remove it. Go also has a
967 // 16-bit limit on structure name length. The name is constructed
968 // based on the Go source representation of the structure, so
969 // the tag names count towards that length.
Colin Crossb4fecbf2020-01-21 11:38:47 -0800970
971 androidTag := field.Tag.Get("android")
972 values := strings.Split(androidTag, ",")
973
974 if string(field.Tag) != `android:"`+strings.Join(values, ",")+`"` {
975 panic(fmt.Errorf("unexpected tag format %q", field.Tag))
Colin Cross74449102019-09-25 11:26:40 -0700976 }
Colin Crossb4fecbf2020-01-21 11:38:47 -0800977 // these tags don't need to be present in the runtime generated struct type.
Liz Kammerff966b12022-07-29 10:49:16 -0400978 values = RemoveListFromList(values, []string{"arch_variant", "variant_prepend", "path"})
979 if len(values) > 0 {
Colin Crossb4fecbf2020-01-21 11:38:47 -0800980 panic(fmt.Errorf("unknown tags %q in field %q", values, prefix+field.Name))
981 }
982
Liz Kammerff966b12022-07-29 10:49:16 -0400983 field.Tag = ``
Colin Cross74449102019-09-25 11:26:40 -0700984 return true, field
985 }
986 return false, field
987}
988
Colin Crossa6845402020-11-16 15:08:19 -0800989// archPropTypeMap contains a cache of the results of createArchPropTypeDesc for each type. It is
990// shared across all Contexts, but is constructed based only on compile-time information so there
991// is no risk of contaminating one Context with data from another.
Dan Willemsenb1957a52016-06-23 23:44:54 -0700992var archPropTypeMap OncePer
993
Colin Crossa6845402020-11-16 15:08:19 -0800994// initArchModule adds the architecture-specific property structs to a Module.
995func initArchModule(m Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800996
997 base := m.base()
998
Ustaeabf0f32021-12-06 15:17:23 -0500999 if len(base.archProperties) != 0 {
1000 panic(fmt.Errorf("module %s already has archProperties", m.Name()))
1001 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001002
Ustaeabf0f32021-12-06 15:17:23 -05001003 getStructType := func(properties interface{}) reflect.Type {
Colin Cross3f40fa42015-01-30 17:27:36 -08001004 propertiesValue := reflect.ValueOf(properties)
Colin Cross62496a02016-08-08 15:49:17 -07001005 t := propertiesValue.Type()
Colin Cross3f40fa42015-01-30 17:27:36 -08001006 if propertiesValue.Kind() != reflect.Ptr {
Colin Crossca860ac2016-01-04 14:34:37 -08001007 panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
1008 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -08001009 }
1010
1011 propertiesValue = propertiesValue.Elem()
1012 if propertiesValue.Kind() != reflect.Struct {
Ustaeabf0f32021-12-06 15:17:23 -05001013 panic(fmt.Errorf("properties must be a pointer to a struct, got a pointer to %T",
Colin Crossca860ac2016-01-04 14:34:37 -08001014 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -08001015 }
Ustaeabf0f32021-12-06 15:17:23 -05001016 return t
1017 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001018
Usta851a3272022-01-05 23:42:33 -05001019 for _, properties := range m.GetProperties() {
Ustaeabf0f32021-12-06 15:17:23 -05001020 t := getStructType(properties)
Colin Crossa6845402020-11-16 15:08:19 -08001021 // Get or create the arch-specific property struct types for this property struct type.
Colin Cross571cccf2019-02-04 11:22:08 -08001022 archPropTypes := archPropTypeMap.Once(NewCustomOnceKey(t), func() interface{} {
Colin Crosscbbd13f2020-01-17 14:08:22 -08001023 return createArchPropTypeDesc(t)
1024 }).([]archPropTypeDesc)
Colin Cross3f40fa42015-01-30 17:27:36 -08001025
Colin Crossa6845402020-11-16 15:08:19 -08001026 // Instantiate one of each arch-specific property struct type and add it to the
1027 // properties for the Module.
Colin Crossc17727d2018-10-24 12:42:09 -07001028 var archProperties []interface{}
1029 for _, t := range archPropTypes {
Colin Crosscbbd13f2020-01-17 14:08:22 -08001030 archProperties = append(archProperties, &archPropRoot{
1031 Arch: reflect.Zero(t.arch).Interface(),
1032 Multilib: reflect.Zero(t.multilib).Interface(),
1033 Target: reflect.Zero(t.target).Interface(),
1034 })
Dan Willemsenb1957a52016-06-23 23:44:54 -07001035 }
Colin Crossc17727d2018-10-24 12:42:09 -07001036 base.archProperties = append(base.archProperties, archProperties)
1037 m.AddProperties(archProperties...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001038 }
1039
Colin Cross3f40fa42015-01-30 17:27:36 -08001040}
1041
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001042func maybeBlueprintEmbed(src reflect.Value) reflect.Value {
Colin Crossa6845402020-11-16 15:08:19 -08001043 // If the value of the field is a struct (as opposed to a pointer to a struct) then step
1044 // into the BlueprintEmbed field.
Dan Willemsenb1957a52016-06-23 23:44:54 -07001045 if src.Kind() == reflect.Struct {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001046 return src.FieldByName("BlueprintEmbed")
1047 } else {
1048 return src
Colin Cross06a931b2015-10-28 17:23:31 -07001049 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001050}
1051
1052// Merges the property struct in srcValue into dst.
Liz Kammerb6dbc872021-05-14 15:14:40 -04001053func mergePropertyStruct(ctx ArchVariantContext, dst interface{}, srcValue reflect.Value) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001054 src := maybeBlueprintEmbed(srcValue).Interface()
Colin Cross06a931b2015-10-28 17:23:31 -07001055
Colin Crossa6845402020-11-16 15:08:19 -08001056 // order checks the `android:"variant_prepend"` tag to handle properties where the
1057 // arch-specific value needs to come before the generic value, for example for lists of
1058 // include directories.
Colin Cross6ee75b62016-05-05 15:57:15 -07001059 order := func(property string,
1060 dstField, srcField reflect.StructField,
1061 dstValue, srcValue interface{}) (proptools.Order, error) {
1062 if proptools.HasTag(dstField, "android", "variant_prepend") {
1063 return proptools.Prepend, nil
1064 } else {
1065 return proptools.Append, nil
1066 }
1067 }
1068
Colin Crossa6845402020-11-16 15:08:19 -08001069 // Squash the located property struct into the destination property struct.
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001070 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src, nil, order)
Colin Cross06a931b2015-10-28 17:23:31 -07001071 if err != nil {
1072 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
1073 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
1074 } else {
1075 panic(err)
1076 }
1077 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001078}
Colin Cross85a88972015-11-23 13:29:51 -08001079
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001080// Returns the immediate child of the input property struct that corresponds to
1081// the sub-property "field".
Liz Kammerb6dbc872021-05-14 15:14:40 -04001082func getChildPropertyStruct(ctx ArchVariantContext,
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001083 src reflect.Value, field, userFriendlyField string) (reflect.Value, bool) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001084
1085 // Step into non-nil pointers to structs in the src value.
1086 if src.Kind() == reflect.Ptr {
1087 if src.IsNil() {
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001088 return reflect.Value{}, false
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001089 }
1090 src = src.Elem()
1091 }
1092
1093 // Find the requested field in the src struct.
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001094 child := src.FieldByName(proptools.FieldNameForProperty(field))
1095 if !child.IsValid() {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001096 ctx.ModuleErrorf("field %q does not exist", userFriendlyField)
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001097 return reflect.Value{}, false
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001098 }
1099
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001100 if child.IsZero() {
1101 return reflect.Value{}, false
1102 }
1103
1104 return child, true
Colin Cross06a931b2015-10-28 17:23:31 -07001105}
1106
Colin Crossa6845402020-11-16 15:08:19 -08001107// Squash the appropriate OS-specific property structs into the matching top level property structs
1108// based on the CompileOS value that was annotated on the variant.
Colin Crossa195f912019-10-16 11:07:20 -07001109func (m *ModuleBase) setOSProperties(ctx BottomUpMutatorContext) {
1110 os := m.commonProperties.CompileOS
1111
Ustadca02192021-12-20 12:56:46 -05001112 for i := range m.archProperties {
Usta851a3272022-01-05 23:42:33 -05001113 genProps := m.GetProperties()[i]
Colin Crossa195f912019-10-16 11:07:20 -07001114 if m.archProperties[i] == nil {
1115 continue
1116 }
1117 for _, archProperties := range m.archProperties[i] {
1118 archPropValues := reflect.ValueOf(archProperties).Elem()
1119
Colin Crosscbbd13f2020-01-17 14:08:22 -08001120 targetProp := archPropValues.FieldByName("Target").Elem()
Colin Crossa195f912019-10-16 11:07:20 -07001121
1122 // Handle host-specific properties in the form:
1123 // target: {
1124 // host: {
1125 // key: value,
1126 // },
1127 // },
Jiyong Park1613e552020-09-14 19:43:17 +09001128 if os.Class == Host {
Colin Crossa195f912019-10-16 11:07:20 -07001129 field := "Host"
1130 prefix := "target.host"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001131 if hostProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1132 mergePropertyStruct(ctx, genProps, hostProperties)
1133 }
Colin Crossa195f912019-10-16 11:07:20 -07001134 }
1135
1136 // Handle target OS generalities of the form:
1137 // target: {
1138 // bionic: {
1139 // key: value,
1140 // },
1141 // }
1142 if os.Linux() {
1143 field := "Linux"
1144 prefix := "target.linux"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001145 if linuxProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1146 mergePropertyStruct(ctx, genProps, linuxProperties)
1147 }
Colin Crossa195f912019-10-16 11:07:20 -07001148 }
1149
Colin Crossa98d36d2022-03-07 14:39:49 -08001150 if os.Linux() && os.Class == Host {
1151 field := "Host_linux"
1152 prefix := "target.host_linux"
1153 if linuxProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1154 mergePropertyStruct(ctx, genProps, linuxProperties)
1155 }
1156 }
1157
Colin Crossa195f912019-10-16 11:07:20 -07001158 if os.Bionic() {
1159 field := "Bionic"
1160 prefix := "target.bionic"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001161 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1162 mergePropertyStruct(ctx, genProps, bionicProperties)
1163 }
Colin Crossa195f912019-10-16 11:07:20 -07001164 }
1165
Colin Cross528d67e2021-07-23 22:23:07 +00001166 if os == Linux {
1167 field := "Glibc"
1168 prefix := "target.glibc"
1169 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1170 mergePropertyStruct(ctx, genProps, bionicProperties)
1171 }
1172 }
1173
1174 if os == LinuxMusl {
1175 field := "Musl"
1176 prefix := "target.musl"
1177 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1178 mergePropertyStruct(ctx, genProps, bionicProperties)
1179 }
Colin Cross528d67e2021-07-23 22:23:07 +00001180 }
1181
Colin Crossa195f912019-10-16 11:07:20 -07001182 // Handle target OS properties in the form:
1183 // target: {
1184 // linux_glibc: {
1185 // key: value,
1186 // },
1187 // not_windows: {
1188 // key: value,
1189 // },
1190 // android {
1191 // key: value,
1192 // },
1193 // },
1194 field := os.Field
1195 prefix := "target." + os.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001196 if osProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1197 mergePropertyStruct(ctx, genProps, osProperties)
1198 }
Colin Crossa195f912019-10-16 11:07:20 -07001199
Jiyong Park1613e552020-09-14 19:43:17 +09001200 if os.Class == Host && os != Windows {
Colin Crossa195f912019-10-16 11:07:20 -07001201 field := "Not_windows"
1202 prefix := "target.not_windows"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001203 if notWindowsProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1204 mergePropertyStruct(ctx, genProps, notWindowsProperties)
1205 }
Colin Crossa195f912019-10-16 11:07:20 -07001206 }
1207
1208 // Handle 64-bit device properties in the form:
1209 // target {
1210 // android64 {
1211 // key: value,
1212 // },
1213 // android32 {
1214 // key: value,
1215 // },
1216 // },
1217 // WARNING: this is probably not what you want to use in your blueprints file, it selects
1218 // options for all targets on a device that supports 64-bit binaries, not just the targets
1219 // that are being compiled for 64-bit. Its expected use case is binaries like linker and
1220 // debuggerd that need to know when they are a 32-bit process running on a 64-bit device
1221 if os.Class == Device {
1222 if ctx.Config().Android64() {
1223 field := "Android64"
1224 prefix := "target.android64"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001225 if android64Properties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1226 mergePropertyStruct(ctx, genProps, android64Properties)
1227 }
Colin Crossa195f912019-10-16 11:07:20 -07001228 } else {
1229 field := "Android32"
1230 prefix := "target.android32"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001231 if android32Properties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1232 mergePropertyStruct(ctx, genProps, android32Properties)
1233 }
Colin Crossa195f912019-10-16 11:07:20 -07001234 }
1235 }
1236 }
1237 }
1238}
1239
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001240// Returns the struct containing the properties specific to the given
1241// architecture type. These look like this in Blueprint files:
Colin Crossd079e0b2022-08-16 10:27:33 -07001242//
1243// arch: {
1244// arm64: {
1245// key: value,
1246// },
1247// },
1248//
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001249// This struct will also contain sub-structs containing to the architecture/CPU
1250// variants and features that themselves contain properties specific to those.
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001251func getArchTypeStruct(ctx ArchVariantContext, archProperties interface{}, archType ArchType) (reflect.Value, bool) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001252 archPropValues := reflect.ValueOf(archProperties).Elem()
1253 archProp := archPropValues.FieldByName("Arch").Elem()
1254 prefix := "arch." + archType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001255 return getChildPropertyStruct(ctx, archProp, archType.Name, prefix)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001256}
1257
1258// Returns the struct containing the properties specific to a given multilib
1259// value. These look like this in the Blueprint file:
Colin Crossd079e0b2022-08-16 10:27:33 -07001260//
1261// multilib: {
1262// lib32: {
1263// key: value,
1264// },
1265// },
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001266func getMultilibStruct(ctx ArchVariantContext, archProperties interface{}, archType ArchType) (reflect.Value, bool) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001267 archPropValues := reflect.ValueOf(archProperties).Elem()
1268 multilibProp := archPropValues.FieldByName("Multilib").Elem()
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001269 return getChildPropertyStruct(ctx, multilibProp, archType.Multilib, "multilib."+archType.Multilib)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001270}
1271
Liz Kammer9abd62d2021-05-21 08:37:59 -04001272func GetCompoundTargetField(os OsType, arch ArchType) string {
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04001273 return os.Field + "_" + arch.Name
1274}
1275
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001276// Returns the structs corresponding to the properties specific to the given
1277// architecture and OS in archProperties.
1278func getArchProperties(ctx BaseMutatorContext, archProperties interface{}, arch Arch, os OsType, nativeBridgeEnabled bool) []reflect.Value {
1279 result := make([]reflect.Value, 0)
1280 archPropValues := reflect.ValueOf(archProperties).Elem()
1281
1282 targetProp := archPropValues.FieldByName("Target").Elem()
1283
1284 archType := arch.ArchType
1285
1286 if arch.ArchType != Common {
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001287 archStruct, ok := getArchTypeStruct(ctx, archProperties, arch.ArchType)
1288 if ok {
1289 result = append(result, archStruct)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001290
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001291 // Handle arch-variant-specific properties in the form:
1292 // arch: {
1293 // arm: {
1294 // variant: {
1295 // key: value,
1296 // },
1297 // },
1298 // },
1299 v := variantReplacer.Replace(arch.ArchVariant)
1300 if v != "" {
1301 prefix := "arch." + archType.Name + "." + v
1302 if variantProperties, ok := getChildPropertyStruct(ctx, archStruct, v, prefix); ok {
1303 result = append(result, variantProperties)
1304 }
1305 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001306
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001307 // Handle cpu-variant-specific properties in the form:
1308 // arch: {
1309 // arm: {
1310 // variant: {
1311 // key: value,
1312 // },
1313 // },
1314 // },
1315 if arch.CpuVariant != arch.ArchVariant {
1316 c := variantReplacer.Replace(arch.CpuVariant)
1317 if c != "" {
1318 prefix := "arch." + archType.Name + "." + c
1319 if cpuVariantProperties, ok := getChildPropertyStruct(ctx, archStruct, c, prefix); ok {
1320 result = append(result, cpuVariantProperties)
1321 }
1322 }
1323 }
1324
1325 // Handle arch-feature-specific properties in the form:
1326 // arch: {
1327 // arm: {
1328 // feature: {
1329 // key: value,
1330 // },
1331 // },
1332 // },
1333 for _, feature := range arch.ArchFeatures {
1334 prefix := "arch." + archType.Name + "." + feature
1335 if featureProperties, ok := getChildPropertyStruct(ctx, archStruct, feature, prefix); ok {
1336 result = append(result, featureProperties)
1337 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001338 }
1339 }
1340
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001341 if multilibProperties, ok := getMultilibStruct(ctx, archProperties, archType); ok {
1342 result = append(result, multilibProperties)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001343 }
1344
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001345 // Handle combined OS-feature and arch specific properties in the form:
1346 // target: {
1347 // bionic_x86: {
1348 // key: value,
1349 // },
1350 // }
1351 if os.Linux() {
1352 field := "Linux_" + arch.ArchType.Name
1353 userFriendlyField := "target.linux_" + arch.ArchType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001354 if linuxProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1355 result = append(result, linuxProperties)
1356 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001357 }
1358
1359 if os.Bionic() {
1360 field := "Bionic_" + archType.Name
1361 userFriendlyField := "target.bionic_" + archType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001362 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1363 result = append(result, bionicProperties)
1364 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001365 }
1366
1367 // Handle combined OS and arch specific properties in the form:
1368 // target: {
1369 // linux_glibc_x86: {
1370 // key: value,
1371 // },
1372 // linux_glibc_arm: {
1373 // key: value,
1374 // },
1375 // android_arm {
1376 // key: value,
1377 // },
1378 // android_x86 {
1379 // key: value,
1380 // },
1381 // },
Liz Kammer9abd62d2021-05-21 08:37:59 -04001382 field := GetCompoundTargetField(os, archType)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001383 userFriendlyField := "target." + os.Name + "_" + archType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001384 if osArchProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1385 result = append(result, osArchProperties)
1386 }
Colin Cross528d67e2021-07-23 22:23:07 +00001387
Colin Cross1aa45b02022-02-10 10:33:10 -08001388 if os == Linux {
1389 field := "Glibc_" + archType.Name
1390 userFriendlyField := "target.glibc_" + "_" + archType.Name
1391 if osArchProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1392 result = append(result, osArchProperties)
1393 }
1394 }
1395
Colin Cross528d67e2021-07-23 22:23:07 +00001396 if os == LinuxMusl {
Colin Cross1aa45b02022-02-10 10:33:10 -08001397 field := "Musl_" + archType.Name
1398 userFriendlyField := "target.musl_" + "_" + archType.Name
1399 if osArchProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1400 result = append(result, osArchProperties)
1401 }
Colin Cross528d67e2021-07-23 22:23:07 +00001402 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001403 }
1404
1405 // Handle arm on x86 properties in the form:
1406 // target {
1407 // arm_on_x86 {
1408 // key: value,
1409 // },
1410 // arm_on_x86_64 {
1411 // key: value,
1412 // },
1413 // },
1414 if os.Class == Device {
1415 if arch.ArchType == X86 && (hasArmAbi(arch) ||
1416 hasArmAndroidArch(ctx.Config().Targets[Android])) {
1417 field := "Arm_on_x86"
1418 userFriendlyField := "target.arm_on_x86"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001419 if armOnX86Properties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1420 result = append(result, armOnX86Properties)
1421 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001422 }
1423 if arch.ArchType == X86_64 && (hasArmAbi(arch) ||
1424 hasArmAndroidArch(ctx.Config().Targets[Android])) {
1425 field := "Arm_on_x86_64"
1426 userFriendlyField := "target.arm_on_x86_64"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001427 if armOnX8664Properties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1428 result = append(result, armOnX8664Properties)
1429 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001430 }
1431 if os == Android && nativeBridgeEnabled {
1432 userFriendlyField := "Native_bridge"
1433 prefix := "target.native_bridge"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001434 if nativeBridgeProperties, ok := getChildPropertyStruct(ctx, targetProp, userFriendlyField, prefix); ok {
1435 result = append(result, nativeBridgeProperties)
1436 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001437 }
1438 }
1439
1440 return result
1441}
1442
Colin Crossa6845402020-11-16 15:08:19 -08001443// Squash the appropriate arch-specific property structs into the matching top level property
1444// structs based on the CompileTarget value that was annotated on the variant.
Colin Cross4157e882019-06-06 16:57:04 -07001445func (m *ModuleBase) setArchProperties(ctx BottomUpMutatorContext) {
1446 arch := m.Arch()
1447 os := m.Os()
Colin Crossd3ba0392015-05-07 14:11:29 -07001448
Ustadca02192021-12-20 12:56:46 -05001449 for i := range m.archProperties {
Usta851a3272022-01-05 23:42:33 -05001450 genProps := m.GetProperties()[i]
Colin Cross4157e882019-06-06 16:57:04 -07001451 if m.archProperties[i] == nil {
Dan Willemsenb1957a52016-06-23 23:44:54 -07001452 continue
1453 }
Dan Willemsenb1957a52016-06-23 23:44:54 -07001454
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001455 propStructs := make([]reflect.Value, 0)
1456 for _, archProperty := range m.archProperties[i] {
1457 propStructShard := getArchProperties(ctx, archProperty, arch, os, m.Target().NativeBridge == NativeBridgeEnabled)
1458 propStructs = append(propStructs, propStructShard...)
1459 }
Dan Willemsenb1957a52016-06-23 23:44:54 -07001460
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001461 for _, propStruct := range propStructs {
1462 mergePropertyStruct(ctx, genProps, propStruct)
Colin Crossbb2e2b72016-12-08 17:23:53 -08001463 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001464 }
1465}
1466
Colin Cross0c66bc62021-07-20 09:47:41 -07001467// determineBuildOS stores the OS and architecture used for host targets used during the build into
Colin Cross528d67e2021-07-23 22:23:07 +00001468// config based on the runtime OS and architecture determined by Go and the product configuration.
Colin Cross0c66bc62021-07-20 09:47:41 -07001469func determineBuildOS(config *config) {
1470 config.BuildOS = func() OsType {
1471 switch runtime.GOOS {
1472 case "linux":
Colin Cross528d67e2021-07-23 22:23:07 +00001473 if Bool(config.productVariables.HostMusl) {
1474 return LinuxMusl
1475 }
Colin Cross0c66bc62021-07-20 09:47:41 -07001476 return Linux
1477 case "darwin":
1478 return Darwin
1479 default:
1480 panic(fmt.Sprintf("unsupported OS: %s", runtime.GOOS))
1481 }
1482 }()
1483
1484 config.BuildArch = func() ArchType {
1485 switch runtime.GOARCH {
1486 case "amd64":
1487 return X86_64
1488 default:
1489 panic(fmt.Sprintf("unsupported Arch: %s", runtime.GOARCH))
1490 }
1491 }()
1492
1493}
1494
Colin Crossa6845402020-11-16 15:08:19 -08001495// Convert the arch product variables into a list of targets for each OsType.
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001496func decodeTargetProductVariables(config *config) (map[OsType][]Target, error) {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001497 variables := config.productVariables
Dan Willemsen490fd492015-11-24 17:53:15 -08001498
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001499 targets := make(map[OsType][]Target)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001500 var targetErr error
1501
Liz Kammerb7f33662022-02-28 14:16:16 -05001502 type targetConfig struct {
1503 os OsType
1504 archName string
1505 archVariant *string
1506 cpuVariant *string
1507 abi []string
1508 nativeBridgeEnabled NativeBridgeSupport
1509 nativeBridgeHostArchName *string
1510 nativeBridgeRelativePath *string
1511 }
1512
1513 addTarget := func(target targetConfig) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001514 if targetErr != nil {
1515 return
Dan Willemsen490fd492015-11-24 17:53:15 -08001516 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001517
Liz Kammerb7f33662022-02-28 14:16:16 -05001518 arch, err := decodeArch(target.os, target.archName, target.archVariant, target.cpuVariant, target.abi)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001519 if err != nil {
1520 targetErr = err
1521 return
1522 }
Liz Kammerb7f33662022-02-28 14:16:16 -05001523 nativeBridgeRelativePathStr := String(target.nativeBridgeRelativePath)
1524 nativeBridgeHostArchNameStr := String(target.nativeBridgeHostArchName)
dimitry8d6dde82019-07-11 10:23:53 +02001525
1526 // Use guest arch as relative install path by default
Liz Kammerb7f33662022-02-28 14:16:16 -05001527 if target.nativeBridgeEnabled && nativeBridgeRelativePathStr == "" {
dimitry8d6dde82019-07-11 10:23:53 +02001528 nativeBridgeRelativePathStr = arch.ArchType.String()
1529 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001530
Jiyong Park1613e552020-09-14 19:43:17 +09001531 // A target is considered as HostCross if it's a host target which can't run natively on
1532 // the currently configured build machine (either because the OS is different or because of
1533 // the unsupported arch)
1534 hostCross := false
Liz Kammerb7f33662022-02-28 14:16:16 -05001535 if target.os.Class == Host {
Jiyong Park1613e552020-09-14 19:43:17 +09001536 var osSupported bool
Liz Kammerb7f33662022-02-28 14:16:16 -05001537 if target.os == config.BuildOS {
Jiyong Park1613e552020-09-14 19:43:17 +09001538 osSupported = true
Liz Kammerb7f33662022-02-28 14:16:16 -05001539 } else if config.BuildOS.Linux() && target.os.Linux() {
Jiyong Park1613e552020-09-14 19:43:17 +09001540 // LinuxBionic and Linux are compatible
1541 osSupported = true
1542 } else {
1543 osSupported = false
1544 }
1545
1546 var archSupported bool
1547 if arch.ArchType == Common {
1548 archSupported = true
1549 } else if arch.ArchType.Name == *variables.HostArch {
1550 archSupported = true
1551 } else if variables.HostSecondaryArch != nil && arch.ArchType.Name == *variables.HostSecondaryArch {
1552 archSupported = true
1553 } else {
1554 archSupported = false
1555 }
1556 if !osSupported || !archSupported {
1557 hostCross = true
1558 }
1559 }
1560
Liz Kammerb7f33662022-02-28 14:16:16 -05001561 targets[target.os] = append(targets[target.os],
Colin Crossa1ad8d12016-06-01 17:09:44 -07001562 Target{
Liz Kammerb7f33662022-02-28 14:16:16 -05001563 Os: target.os,
dimitry8d6dde82019-07-11 10:23:53 +02001564 Arch: arch,
Liz Kammerb7f33662022-02-28 14:16:16 -05001565 NativeBridge: target.nativeBridgeEnabled,
dimitry8d6dde82019-07-11 10:23:53 +02001566 NativeBridgeHostArchName: nativeBridgeHostArchNameStr,
1567 NativeBridgeRelativePath: nativeBridgeRelativePathStr,
Jiyong Park1613e552020-09-14 19:43:17 +09001568 HostCross: hostCross,
Colin Crossa1ad8d12016-06-01 17:09:44 -07001569 })
Dan Willemsen490fd492015-11-24 17:53:15 -08001570 }
1571
Colin Cross4225f652015-09-17 14:33:42 -07001572 if variables.HostArch == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001573 return nil, fmt.Errorf("No host primary architecture set")
Colin Cross4225f652015-09-17 14:33:42 -07001574 }
1575
Colin Crossa6845402020-11-16 15:08:19 -08001576 // The primary host target, which must always exist.
Liz Kammerb7f33662022-02-28 14:16:16 -05001577 addTarget(targetConfig{os: config.BuildOS, archName: *variables.HostArch, nativeBridgeEnabled: NativeBridgeDisabled})
Colin Cross4225f652015-09-17 14:33:42 -07001578
Colin Crossa6845402020-11-16 15:08:19 -08001579 // An optional secondary host target.
Colin Crosseeabb892015-11-20 13:07:51 -08001580 if variables.HostSecondaryArch != nil && *variables.HostSecondaryArch != "" {
Liz Kammerb7f33662022-02-28 14:16:16 -05001581 addTarget(targetConfig{os: config.BuildOS, archName: *variables.HostSecondaryArch, nativeBridgeEnabled: NativeBridgeDisabled})
Dan Willemsen490fd492015-11-24 17:53:15 -08001582 }
1583
Colin Crossa6845402020-11-16 15:08:19 -08001584 // Optional cross-compiled host targets, generally Windows.
Colin Crossff3ae9d2018-04-10 16:15:18 -07001585 if String(variables.CrossHost) != "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001586 crossHostOs := osByName(*variables.CrossHost)
1587 if crossHostOs == NoOsType {
1588 return nil, fmt.Errorf("Unknown cross host OS %q", *variables.CrossHost)
1589 }
1590
Colin Crossff3ae9d2018-04-10 16:15:18 -07001591 if String(variables.CrossHostArch) == "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001592 return nil, fmt.Errorf("No cross-host primary architecture set")
Dan Willemsen490fd492015-11-24 17:53:15 -08001593 }
1594
Colin Crossa6845402020-11-16 15:08:19 -08001595 // The primary cross-compiled host target.
Liz Kammerb7f33662022-02-28 14:16:16 -05001596 addTarget(targetConfig{os: crossHostOs, archName: *variables.CrossHostArch, nativeBridgeEnabled: NativeBridgeDisabled})
Dan Willemsen490fd492015-11-24 17:53:15 -08001597
Colin Crossa6845402020-11-16 15:08:19 -08001598 // An optional secondary cross-compiled host target.
Dan Willemsen490fd492015-11-24 17:53:15 -08001599 if variables.CrossHostSecondaryArch != nil && *variables.CrossHostSecondaryArch != "" {
Liz Kammerb7f33662022-02-28 14:16:16 -05001600 addTarget(targetConfig{os: crossHostOs, archName: *variables.CrossHostSecondaryArch, nativeBridgeEnabled: NativeBridgeDisabled})
Dan Willemsen490fd492015-11-24 17:53:15 -08001601 }
1602 }
1603
Colin Crossa6845402020-11-16 15:08:19 -08001604 // Optional device targets
Dan Willemsen3f32f032016-07-11 14:36:48 -07001605 if variables.DeviceArch != nil && *variables.DeviceArch != "" {
Colin Crossa6845402020-11-16 15:08:19 -08001606 // The primary device target.
Liz Kammerb7f33662022-02-28 14:16:16 -05001607 addTarget(targetConfig{
1608 os: Android,
1609 archName: *variables.DeviceArch,
1610 archVariant: variables.DeviceArchVariant,
1611 cpuVariant: variables.DeviceCpuVariant,
1612 abi: variables.DeviceAbi,
1613 nativeBridgeEnabled: NativeBridgeDisabled,
1614 })
Colin Cross4225f652015-09-17 14:33:42 -07001615
Colin Crossa6845402020-11-16 15:08:19 -08001616 // An optional secondary device target.
Dan Willemsen3f32f032016-07-11 14:36:48 -07001617 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" {
Liz Kammerb7f33662022-02-28 14:16:16 -05001618 addTarget(targetConfig{
1619 os: Android,
1620 archName: *variables.DeviceSecondaryArch,
1621 archVariant: variables.DeviceSecondaryArchVariant,
1622 cpuVariant: variables.DeviceSecondaryCpuVariant,
1623 abi: variables.DeviceSecondaryAbi,
1624 nativeBridgeEnabled: NativeBridgeDisabled,
1625 })
Colin Cross4225f652015-09-17 14:33:42 -07001626 }
dimitry1f33e402019-03-26 12:39:31 +01001627
Colin Crossa6845402020-11-16 15:08:19 -08001628 // An optional NativeBridge device target.
dimitry1f33e402019-03-26 12:39:31 +01001629 if variables.NativeBridgeArch != nil && *variables.NativeBridgeArch != "" {
Liz Kammerb7f33662022-02-28 14:16:16 -05001630 addTarget(targetConfig{
1631 os: Android,
1632 archName: *variables.NativeBridgeArch,
1633 archVariant: variables.NativeBridgeArchVariant,
1634 cpuVariant: variables.NativeBridgeCpuVariant,
1635 abi: variables.NativeBridgeAbi,
1636 nativeBridgeEnabled: NativeBridgeEnabled,
1637 nativeBridgeHostArchName: variables.DeviceArch,
1638 nativeBridgeRelativePath: variables.NativeBridgeRelativePath,
1639 })
dimitry1f33e402019-03-26 12:39:31 +01001640 }
1641
Colin Crossa6845402020-11-16 15:08:19 -08001642 // An optional secondary NativeBridge device target.
dimitry1f33e402019-03-26 12:39:31 +01001643 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" &&
1644 variables.NativeBridgeSecondaryArch != nil && *variables.NativeBridgeSecondaryArch != "" {
Liz Kammerb7f33662022-02-28 14:16:16 -05001645 addTarget(targetConfig{
1646 os: Android,
1647 archName: *variables.NativeBridgeSecondaryArch,
1648 archVariant: variables.NativeBridgeSecondaryArchVariant,
1649 cpuVariant: variables.NativeBridgeSecondaryCpuVariant,
1650 abi: variables.NativeBridgeSecondaryAbi,
1651 nativeBridgeEnabled: NativeBridgeEnabled,
1652 nativeBridgeHostArchName: variables.DeviceSecondaryArch,
1653 nativeBridgeRelativePath: variables.NativeBridgeSecondaryRelativePath,
1654 })
dimitry1f33e402019-03-26 12:39:31 +01001655 }
Colin Cross4225f652015-09-17 14:33:42 -07001656 }
1657
Colin Crossa1ad8d12016-06-01 17:09:44 -07001658 if targetErr != nil {
1659 return nil, targetErr
1660 }
1661
1662 return targets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001663}
1664
Colin Crossbb2e2b72016-12-08 17:23:53 -08001665// hasArmAbi returns true if arch has at least one arm ABI
1666func hasArmAbi(arch Arch) bool {
Jaewoong Jung3aff5782020-02-11 07:54:35 -08001667 return PrefixInList(arch.Abi, "arm")
Colin Crossbb2e2b72016-12-08 17:23:53 -08001668}
1669
Lev Rumyantsev34581212021-10-13 09:47:59 -07001670// hasArmAndroidArch returns true if targets has at least
1671// one arm Android arch (possibly native bridged)
Colin Cross4247f0d2017-04-13 16:56:14 -07001672func hasArmAndroidArch(targets []Target) bool {
1673 for _, target := range targets {
Lev Rumyantsev34581212021-10-13 09:47:59 -07001674 if target.Os == Android &&
1675 (target.Arch.ArchType == Arm || target.Arch.ArchType == Arm64) {
Victor Khimenko5eb8ec12018-03-21 20:30:54 +01001676 return true
1677 }
1678 }
1679 return false
1680}
1681
Colin Crossa6845402020-11-16 15:08:19 -08001682// archConfig describes a built-in configuration.
Dan Albert4098deb2016-10-19 14:04:41 -07001683type archConfig struct {
1684 arch string
1685 archVariant string
1686 cpuVariant string
1687 abi []string
1688}
1689
Elliott Hughesc55b5862022-10-27 23:46:22 +00001690// getNdkAbisConfig returns the list of archConfigs that are used for building
1691// the API stubs and static libraries that are included in the NDK.
Dan Albert4098deb2016-10-19 14:04:41 -07001692func getNdkAbisConfig() []archConfig {
1693 return []archConfig{
Tamas Petzbca786d2021-01-20 18:56:33 +01001694 {"arm64", "armv8-a-branchprot", "", []string{"arm64-v8a"}},
Elliott Hughesc55b5862022-10-27 23:46:22 +00001695 {"arm", "armv7-a-neon", "", []string{"armeabi-v7a"}},
Dan Albert4098deb2016-10-19 14:04:41 -07001696 {"x86_64", "", "", []string{"x86_64"}},
Inseob Kim5219c0e2021-06-17 00:33:00 +09001697 {"x86", "", "", []string{"x86"}},
Dan Albert4098deb2016-10-19 14:04:41 -07001698 }
1699}
1700
Colin Crossa6845402020-11-16 15:08:19 -08001701// getAmlAbisConfig returns a list of archConfigs for the ABIs supported by mainline modules.
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001702func getAmlAbisConfig() []archConfig {
1703 return []archConfig{
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001704 {"arm64", "armv8-a", "", []string{"arm64-v8a"}},
Inseob Kim5219c0e2021-06-17 00:33:00 +09001705 {"arm", "armv7-a-neon", "", []string{"armeabi-v7a"}},
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001706 {"x86_64", "", "", []string{"x86_64"}},
Inseob Kim5219c0e2021-06-17 00:33:00 +09001707 {"x86", "", "", []string{"x86"}},
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001708 }
1709}
1710
Colin Crossa6845402020-11-16 15:08:19 -08001711// decodeArchSettings converts a list of archConfigs into a list of Targets for the given OsType.
Liz Kammerb7f33662022-02-28 14:16:16 -05001712func decodeAndroidArchSettings(archConfigs []archConfig) ([]Target, error) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001713 var ret []Target
Dan Willemsen322acaf2016-01-12 23:07:05 -08001714
Dan Albert4098deb2016-10-19 14:04:41 -07001715 for _, config := range archConfigs {
Liz Kammerb7f33662022-02-28 14:16:16 -05001716 arch, err := decodeArch(Android, config.arch, &config.archVariant,
Colin Crossa74ca042019-01-31 14:31:51 -08001717 &config.cpuVariant, config.abi)
Dan Willemsen322acaf2016-01-12 23:07:05 -08001718 if err != nil {
1719 return nil, err
1720 }
Colin Cross3b19f5d2019-09-17 14:45:31 -07001721
Colin Crossa1ad8d12016-06-01 17:09:44 -07001722 ret = append(ret, Target{
1723 Os: Android,
1724 Arch: arch,
1725 })
Dan Willemsen322acaf2016-01-12 23:07:05 -08001726 }
1727
1728 return ret, nil
1729}
1730
Colin Crossa6845402020-11-16 15:08:19 -08001731// decodeArch converts a set of strings from product variables into an Arch struct.
Colin Crossa74ca042019-01-31 14:31:51 -08001732func decodeArch(os OsType, arch string, archVariant, cpuVariant *string, abi []string) (Arch, error) {
Colin Crossa6845402020-11-16 15:08:19 -08001733 // Verify the arch is valid
Colin Crosseeabb892015-11-20 13:07:51 -08001734 archType, ok := archTypeMap[arch]
1735 if !ok {
1736 return Arch{}, fmt.Errorf("unknown arch %q", arch)
1737 }
Colin Cross4225f652015-09-17 14:33:42 -07001738
Colin Crosseeabb892015-11-20 13:07:51 -08001739 a := Arch{
Colin Cross4225f652015-09-17 14:33:42 -07001740 ArchType: archType,
Colin Crossa6845402020-11-16 15:08:19 -08001741 ArchVariant: String(archVariant),
1742 CpuVariant: String(cpuVariant),
Colin Crossa74ca042019-01-31 14:31:51 -08001743 Abi: abi,
Colin Crosseeabb892015-11-20 13:07:51 -08001744 }
1745
Colin Crossa6845402020-11-16 15:08:19 -08001746 // Convert generic arch variants into the empty string.
Colin Crosseeabb892015-11-20 13:07:51 -08001747 if a.ArchVariant == a.ArchType.Name || a.ArchVariant == "generic" {
1748 a.ArchVariant = ""
1749 }
1750
Colin Crossa6845402020-11-16 15:08:19 -08001751 // Convert generic CPU variants into the empty string.
Colin Crosseeabb892015-11-20 13:07:51 -08001752 if a.CpuVariant == a.ArchType.Name || a.CpuVariant == "generic" {
1753 a.CpuVariant = ""
1754 }
1755
Liz Kammer2c2afe22022-02-11 11:35:03 -05001756 if a.ArchVariant != "" {
1757 if validArchVariants := archVariants[archType]; !InList(a.ArchVariant, validArchVariants) {
1758 return Arch{}, fmt.Errorf("[%q] unknown arch variant %q, support variants: %q", archType, a.ArchVariant, validArchVariants)
1759 }
1760 }
1761
1762 if a.CpuVariant != "" {
1763 if validCpuVariants := cpuVariants[archType]; !InList(a.CpuVariant, validCpuVariants) {
1764 return Arch{}, fmt.Errorf("[%q] unknown cpu variant %q, support variants: %q", archType, a.CpuVariant, validCpuVariants)
1765 }
1766 }
1767
Colin Crossa6845402020-11-16 15:08:19 -08001768 // Filter empty ABIs out of the list.
Colin Crosseeabb892015-11-20 13:07:51 -08001769 for i := 0; i < len(a.Abi); i++ {
1770 if a.Abi[i] == "" {
1771 a.Abi = append(a.Abi[:i], a.Abi[i+1:]...)
1772 i--
1773 }
1774 }
1775
Liz Kammere8303bd2022-02-16 09:02:48 -05001776 // Set ArchFeatures from the arch type. for Android OS, other os-es do not specify features
1777 if os == Android {
1778 if featureMap, ok := androidArchFeatureMap[archType]; ok {
Dan Willemsen01a3c252019-01-11 19:02:16 -08001779 a.ArchFeatures = featureMap[a.ArchVariant]
1780 }
Colin Crossc5c24ad2015-11-20 15:35:00 -08001781 }
1782
Colin Crosseeabb892015-11-20 13:07:51 -08001783 return a, nil
Colin Cross4225f652015-09-17 14:33:42 -07001784}
1785
Colin Crossa6845402020-11-16 15:08:19 -08001786// filterMultilibTargets takes a list of Targets and a multilib value and returns a new list of
1787// Targets containing only those that have the given multilib value.
Colin Cross69617d32016-09-06 10:39:07 -07001788func filterMultilibTargets(targets []Target, multilib string) []Target {
1789 var ret []Target
1790 for _, t := range targets {
1791 if t.Arch.ArchType.Multilib == multilib {
1792 ret = append(ret, t)
1793 }
1794 }
1795 return ret
1796}
1797
Colin Crossa6845402020-11-16 15:08:19 -08001798// getCommonTargets returns the set of Os specific common architecture targets for each Os in a list
1799// of targets.
Nan Zhangdb0b9a32017-02-27 10:12:13 -08001800func getCommonTargets(targets []Target) []Target {
1801 var ret []Target
1802 set := make(map[string]bool)
1803
1804 for _, t := range targets {
1805 if _, found := set[t.Os.String()]; !found {
1806 set[t.Os.String()] = true
Colin Cross39a18142022-06-24 18:43:40 -07001807 common := commonTargetMap[t.Os.String()]
1808 common.HostCross = t.HostCross
1809 ret = append(ret, common)
Nan Zhangdb0b9a32017-02-27 10:12:13 -08001810 }
1811 }
1812
1813 return ret
1814}
1815
Sam Delmericocc271e22022-06-01 15:45:02 +00001816// FirstTarget takes a list of Targets and a list of multilib values and returns a list of Targets
Colin Crossc0f0eb82022-07-19 14:41:11 -07001817// that contains zero or one Target for each OsType and HostCross, selecting the one that matches
1818// the earliest filter.
Sam Delmericocc271e22022-06-01 15:45:02 +00001819func FirstTarget(targets []Target, filters ...string) []Target {
Jiyong Park22101982020-09-17 19:09:58 +09001820 // find the first target from each OS
1821 var ret []Target
Colin Crossc0f0eb82022-07-19 14:41:11 -07001822 type osHostCross struct {
1823 os OsType
1824 hostCross bool
1825 }
1826 set := make(map[osHostCross]bool)
Jiyong Park22101982020-09-17 19:09:58 +09001827
Colin Cross6b4a32d2017-12-05 13:42:45 -08001828 for _, filter := range filters {
1829 buildTargets := filterMultilibTargets(targets, filter)
Jiyong Park22101982020-09-17 19:09:58 +09001830 for _, t := range buildTargets {
Colin Crossc0f0eb82022-07-19 14:41:11 -07001831 key := osHostCross{t.Os, t.HostCross}
1832 if _, found := set[key]; !found {
1833 set[key] = true
Jiyong Park22101982020-09-17 19:09:58 +09001834 ret = append(ret, t)
1835 }
Colin Cross6b4a32d2017-12-05 13:42:45 -08001836 }
1837 }
Jiyong Park22101982020-09-17 19:09:58 +09001838 return ret
Colin Cross6b4a32d2017-12-05 13:42:45 -08001839}
1840
Colin Crossa6845402020-11-16 15:08:19 -08001841// decodeMultilibTargets uses the module's multilib setting to select one or more targets from a
1842// list of Targets.
Colin Crossee0bc3b2018-10-02 22:01:37 -07001843func decodeMultilibTargets(multilib string, targets []Target, prefer32 bool) ([]Target, error) {
Colin Crossa6845402020-11-16 15:08:19 -08001844 var buildTargets []Target
Colin Cross6b4a32d2017-12-05 13:42:45 -08001845
Colin Cross4225f652015-09-17 14:33:42 -07001846 switch multilib {
1847 case "common":
Colin Cross6b4a32d2017-12-05 13:42:45 -08001848 buildTargets = getCommonTargets(targets)
1849 case "common_first":
1850 buildTargets = getCommonTargets(targets)
1851 if prefer32 {
Sam Delmericocc271e22022-06-01 15:45:02 +00001852 buildTargets = append(buildTargets, FirstTarget(targets, "lib32", "lib64")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001853 } else {
Sam Delmericocc271e22022-06-01 15:45:02 +00001854 buildTargets = append(buildTargets, FirstTarget(targets, "lib64", "lib32")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001855 }
Colin Cross4225f652015-09-17 14:33:42 -07001856 case "both":
Colin Cross8b74d172016-09-13 09:59:14 -07001857 if prefer32 {
1858 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1859 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1860 } else {
1861 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1862 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1863 }
Colin Cross4225f652015-09-17 14:33:42 -07001864 case "32":
Colin Cross69617d32016-09-06 10:39:07 -07001865 buildTargets = filterMultilibTargets(targets, "lib32")
Colin Cross4225f652015-09-17 14:33:42 -07001866 case "64":
Colin Cross69617d32016-09-06 10:39:07 -07001867 buildTargets = filterMultilibTargets(targets, "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001868 case "first":
1869 if prefer32 {
Sam Delmericocc271e22022-06-01 15:45:02 +00001870 buildTargets = FirstTarget(targets, "lib32", "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001871 } else {
Sam Delmericocc271e22022-06-01 15:45:02 +00001872 buildTargets = FirstTarget(targets, "lib64", "lib32")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001873 }
Victor Chang9448e8f2020-09-14 15:34:16 +01001874 case "first_prefer32":
Sam Delmericocc271e22022-06-01 15:45:02 +00001875 buildTargets = FirstTarget(targets, "lib32", "lib64")
Colin Cross69617d32016-09-06 10:39:07 -07001876 case "prefer32":
Colin Cross3dceee32018-09-06 10:19:57 -07001877 buildTargets = filterMultilibTargets(targets, "lib32")
1878 if len(buildTargets) == 0 {
1879 buildTargets = filterMultilibTargets(targets, "lib64")
1880 }
Dan Willemsen47450072021-10-19 20:24:49 -07001881 case "darwin_universal":
1882 buildTargets = filterMultilibTargets(targets, "lib64")
1883 // Reverse the targets so that the first architecture can depend on the second
1884 // architecture module in order to merge the outputs.
1885 reverseSliceInPlace(buildTargets)
1886 case "darwin_universal_common_first":
1887 archTargets := filterMultilibTargets(targets, "lib64")
1888 reverseSliceInPlace(archTargets)
1889 buildTargets = append(getCommonTargets(targets), archTargets...)
Colin Cross4225f652015-09-17 14:33:42 -07001890 default:
Victor Chang9448e8f2020-09-14 15:34:16 +01001891 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 -07001892 multilib)
Colin Cross4225f652015-09-17 14:33:42 -07001893 }
1894
Colin Crossa1ad8d12016-06-01 17:09:44 -07001895 return buildTargets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001896}
Jingwen Chen5d864492021-02-24 07:20:12 -05001897
Chris Parsonsc424b762021-04-29 18:06:50 -04001898func (m *ModuleBase) getArchPropertySet(propertySet interface{}, archType ArchType) interface{} {
1899 archString := archType.Field
1900 for i := range m.archProperties {
1901 if m.archProperties[i] == nil {
1902 // Skip over nil properties
1903 continue
1904 }
1905
1906 // Not archProperties are usable; this function looks for properties of a very specific
1907 // form, and ignores the rest.
1908 for _, archProperty := range m.archProperties[i] {
1909 // archPropValue is a property struct, we are looking for the form:
1910 // `arch: { arm: { key: value, ... }}`
1911 archPropValue := reflect.ValueOf(archProperty).Elem()
1912
1913 // Unwrap src so that it should looks like a pointer to `arm: { key: value, ... }`
1914 src := archPropValue.FieldByName("Arch").Elem()
1915
1916 // Step into non-nil pointers to structs in the src value.
1917 if src.Kind() == reflect.Ptr {
1918 if src.IsNil() {
1919 continue
1920 }
1921 src = src.Elem()
1922 }
1923
1924 // Find the requested field (e.g. arm, x86) in the src struct.
1925 src = src.FieldByName(archString)
1926
1927 // We only care about structs.
1928 if !src.IsValid() || src.Kind() != reflect.Struct {
1929 continue
1930 }
1931
1932 // If the value of the field is a struct then step into the
1933 // BlueprintEmbed field. The special "BlueprintEmbed" name is
1934 // used by createArchPropTypeDesc to embed the arch properties
1935 // in the parent struct, so the src arch prop should be in this
1936 // field.
1937 //
1938 // See createArchPropTypeDesc for more details on how Arch-specific
1939 // module properties are processed from the nested props and written
1940 // into the module's archProperties.
1941 src = src.FieldByName("BlueprintEmbed")
1942
1943 // Clone the destination prop, since we want a unique prop struct per arch.
1944 propertySetClone := reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
1945
1946 // Copy the located property struct into the cloned destination property struct.
1947 err := proptools.ExtendMatchingProperties([]interface{}{propertySetClone}, src.Interface(), nil, proptools.OrderReplace)
1948 if err != nil {
1949 // This is fine, it just means the src struct doesn't match the type of propertySet.
1950 continue
1951 }
1952
1953 return propertySetClone
1954 }
1955 }
1956 // No property set was found specific to the given arch, so return an empty
1957 // property set.
1958 return reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
1959}
1960
1961// getMultilibPropertySet returns a property set struct matching the type of
1962// `propertySet`, containing multilib-specific module properties for the given architecture.
1963// If no multilib-specific properties exist for the given architecture, returns an empty property
1964// set matching `propertySet`'s type.
1965func (m *ModuleBase) getMultilibPropertySet(propertySet interface{}, archType ArchType) interface{} {
1966 // archType.Multilib is lowercase (for example, lib32) but property struct field is
1967 // capitalized, such as Lib32, so use strings.Title to capitalize it.
1968 multiLibString := strings.Title(archType.Multilib)
1969
1970 for i := range m.archProperties {
1971 if m.archProperties[i] == nil {
1972 // Skip over nil properties
1973 continue
1974 }
1975
1976 // Not archProperties are usable; this function looks for properties of a very specific
1977 // form, and ignores the rest.
1978 for _, archProperties := range m.archProperties[i] {
1979 // archPropValue is a property struct, we are looking for the form:
1980 // `multilib: { lib32: { key: value, ... }}`
1981 archPropValue := reflect.ValueOf(archProperties).Elem()
1982
1983 // Unwrap src so that it should looks like a pointer to `lib32: { key: value, ... }`
1984 src := archPropValue.FieldByName("Multilib").Elem()
1985
1986 // Step into non-nil pointers to structs in the src value.
1987 if src.Kind() == reflect.Ptr {
1988 if src.IsNil() {
1989 // Ignore nil pointers.
1990 continue
1991 }
1992 src = src.Elem()
1993 }
1994
1995 // Find the requested field (e.g. lib32) in the src struct.
1996 src = src.FieldByName(multiLibString)
1997
1998 // We only care about valid struct pointers.
1999 if !src.IsValid() || src.Kind() != reflect.Ptr || src.Elem().Kind() != reflect.Struct {
2000 continue
2001 }
2002
2003 // Get the zero value for the requested property set.
2004 propertySetClone := reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
2005
2006 // Copy the located property struct into the "zero" property set struct.
2007 err := proptools.ExtendMatchingProperties([]interface{}{propertySetClone}, src.Interface(), nil, proptools.OrderReplace)
2008
2009 if err != nil {
2010 // This is fine, it just means the src struct doesn't match.
2011 continue
2012 }
2013
2014 return propertySetClone
2015 }
2016 }
2017
2018 // There were no multilib properties specifically matching the given archtype.
2019 // Return zeroed value.
2020 return reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
2021}
2022
Liz Kammerb6dbc872021-05-14 15:14:40 -04002023// ArchVariantContext defines the limited context necessary to retrieve arch_variant properties.
2024type ArchVariantContext interface {
2025 ModuleErrorf(fmt string, args ...interface{})
2026 PropertyErrorf(property, fmt string, args ...interface{})
2027}
2028
Liz Kammer9abd62d2021-05-21 08:37:59 -04002029// ArchVariantProperties represents a map of arch-variant config strings to a property interface{}.
2030type ArchVariantProperties map[string]interface{}
2031
2032// ConfigurationAxisToArchVariantProperties represents a map of bazel.ConfigurationAxis to
2033// ArchVariantProperties, such that each independent arch-variant axis maps to the
2034// configs/properties for that axis.
2035type ConfigurationAxisToArchVariantProperties map[bazel.ConfigurationAxis]ArchVariantProperties
2036
2037// GetArchVariantProperties returns a ConfigurationAxisToArchVariantProperties where the
2038// arch-variant properties correspond to the values of the properties of the 'propertySet' struct
2039// that are specific to that axis/configuration. Each axis is independent, containing
2040// non-overlapping configs that correspond to the various "arch-variant" support, at this time:
Colin Crossd079e0b2022-08-16 10:27:33 -07002041//
2042// arches (including multilib)
2043// oses
2044// arch+os combinations
Jingwen Chen5d864492021-02-24 07:20:12 -05002045//
Liz Kammer9abd62d2021-05-21 08:37:59 -04002046// For example, passing a struct { Foo bool, Bar string } will return an interface{} that can be
2047// type asserted back into the same struct, containing the config-specific property value specified
2048// by the module if defined.
Chris Parsonsc424b762021-04-29 18:06:50 -04002049//
2050// Arch-specific properties may come from an arch stanza or a multilib stanza; properties
2051// in these stanzas are combined.
2052// For example: `arch: { x86: { Foo: ["bar"] } }, multilib: { lib32: {` Foo: ["baz"] } }`
2053// will result in `Foo: ["bar", "baz"]` being returned for architecture x86, if the given
2054// propertyset contains `Foo []string`.
Liz Kammer9abd62d2021-05-21 08:37:59 -04002055func (m *ModuleBase) GetArchVariantProperties(ctx ArchVariantContext, propertySet interface{}) ConfigurationAxisToArchVariantProperties {
Jingwen Chen5d864492021-02-24 07:20:12 -05002056 // Return value of the arch types to the prop values for that arch.
Liz Kammer9abd62d2021-05-21 08:37:59 -04002057 axisToProps := ConfigurationAxisToArchVariantProperties{}
Jingwen Chen5d864492021-02-24 07:20:12 -05002058
2059 // Nothing to do for non-arch-specific modules.
2060 if !m.ArchSpecific() {
Liz Kammer9abd62d2021-05-21 08:37:59 -04002061 return axisToProps
Jingwen Chen5d864492021-02-24 07:20:12 -05002062 }
2063
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002064 dstType := reflect.ValueOf(propertySet).Type()
2065 var archProperties []interface{}
2066
2067 // First find the property set in the module that corresponds to the requested
Usta851a3272022-01-05 23:42:33 -05002068 // one. m.archProperties[i] corresponds to m.GetProperties()[i].
2069 for i, generalProp := range m.GetProperties() {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002070 srcType := reflect.ValueOf(generalProp).Type()
2071 if srcType == dstType {
2072 archProperties = m.archProperties[i]
Liz Kammer135bf552021-08-11 10:46:06 -04002073 axisToProps[bazel.NoConfigAxis] = ArchVariantProperties{"": generalProp}
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002074 break
2075 }
2076 }
2077
2078 if archProperties == nil {
2079 // This module does not have the property set requested
Liz Kammer9abd62d2021-05-21 08:37:59 -04002080 return axisToProps
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002081 }
2082
Liz Kammer9abd62d2021-05-21 08:37:59 -04002083 archToProp := ArchVariantProperties{}
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002084 // For each arch type (x86, arm64, etc.)
Chris Parsonsc424b762021-04-29 18:06:50 -04002085 for _, arch := range ArchTypeList() {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002086 // Arch properties are sometimes sharded (see createArchPropTypeDesc() ).
Cole Faustc843b992022-08-02 18:06:50 -07002087 // Iterate over every shard and extract a struct with the same type as the
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002088 // input one that contains the data specific to that arch.
2089 propertyStructs := make([]reflect.Value, 0)
Cole Faustc843b992022-08-02 18:06:50 -07002090 archFeaturePropertyStructs := make(map[string][]reflect.Value, 0)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002091 for _, archProperty := range archProperties {
Lukacs T. Berki5f518392021-05-17 11:44:58 +02002092 archTypeStruct, ok := getArchTypeStruct(ctx, archProperty, arch)
2093 if ok {
2094 propertyStructs = append(propertyStructs, archTypeStruct)
Cole Faustc843b992022-08-02 18:06:50 -07002095
2096 // For each feature this arch supports (arm: neon, x86: ssse3, sse4, ...)
2097 for _, feature := range archFeatures[arch] {
2098 prefix := "arch." + arch.Name + "." + feature
2099 if featureProperties, ok := getChildPropertyStruct(ctx, archTypeStruct, feature, prefix); ok {
2100 archFeaturePropertyStructs[feature] = append(archFeaturePropertyStructs[feature], featureProperties)
2101 }
2102 }
Lukacs T. Berki5f518392021-05-17 11:44:58 +02002103 }
2104 multilibStruct, ok := getMultilibStruct(ctx, archProperty, arch)
2105 if ok {
2106 propertyStructs = append(propertyStructs, multilibStruct)
2107 }
Jingwen Chen5d864492021-02-24 07:20:12 -05002108 }
2109
Cole Faustc843b992022-08-02 18:06:50 -07002110 archToProp[arch.Name] = mergeStructs(ctx, propertyStructs, propertySet)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002111
Cole Faustc843b992022-08-02 18:06:50 -07002112 // In soong, if multiple features match the current configuration, they're
2113 // all used. In bazel, we have to have unambiguous select() statements, so
2114 // we can't have two features that are both active in the same select().
2115 // One alternative is to split out each feature into a separate select(),
2116 // but then it's difficult to support exclude_srcs, which may need to
2117 // exclude things from the regular arch select() statement if a certain
2118 // feature is active. Instead, keep the features in the same select
2119 // statement as the arches, but emit the power set of all possible
2120 // combinations of features, so that bazel can match the most precise one.
2121 allFeatures := make([]string, 0, len(archFeaturePropertyStructs))
2122 for feature := range archFeaturePropertyStructs {
2123 allFeatures = append(allFeatures, feature)
2124 }
2125 for _, features := range bazel.PowerSetWithoutEmptySet(allFeatures) {
2126 sort.Strings(features)
2127 propsForCurrentFeatureSet := make([]reflect.Value, 0)
2128 propsForCurrentFeatureSet = append(propsForCurrentFeatureSet, propertyStructs...)
2129 for _, feature := range features {
2130 propsForCurrentFeatureSet = append(propsForCurrentFeatureSet, archFeaturePropertyStructs[feature]...)
2131 }
2132 archToProp[arch.Name+"-"+strings.Join(features, "-")] =
2133 mergeStructs(ctx, propsForCurrentFeatureSet, propertySet)
2134 }
Jingwen Chen5d864492021-02-24 07:20:12 -05002135 }
Liz Kammer9abd62d2021-05-21 08:37:59 -04002136 axisToProps[bazel.ArchConfigurationAxis] = archToProp
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002137
Liz Kammer9abd62d2021-05-21 08:37:59 -04002138 osToProp := ArchVariantProperties{}
2139 archOsToProp := ArchVariantProperties{}
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002140
Liz Kammerfdd72e62021-10-11 15:41:03 -04002141 linuxStructs := getTargetStructs(ctx, archProperties, "Linux")
2142 bionicStructs := getTargetStructs(ctx, archProperties, "Bionic")
2143 hostStructs := getTargetStructs(ctx, archProperties, "Host")
Colin Crossa98d36d2022-03-07 14:39:49 -08002144 hostLinuxStructs := getTargetStructs(ctx, archProperties, "Host_linux")
Liz Kammerfdd72e62021-10-11 15:41:03 -04002145 hostNotWindowsStructs := getTargetStructs(ctx, archProperties, "Not_windows")
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002146
Liz Kammer9abd62d2021-05-21 08:37:59 -04002147 // For android, linux, ...
2148 for _, os := range osTypeList {
2149 if os == CommonOS {
2150 // It looks like this OS value is not used in Blueprint files
2151 continue
2152 }
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002153 osStructs := make([]reflect.Value, 0)
Liz Kammerfdd72e62021-10-11 15:41:03 -04002154
2155 osSpecificStructs := getTargetStructs(ctx, archProperties, os.Field)
2156 if os.Class == Host {
2157 osStructs = append(osStructs, hostStructs...)
Chris Parsonsa37e1952021-09-28 16:47:36 -04002158 }
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002159 if os.Linux() {
2160 osStructs = append(osStructs, linuxStructs...)
2161 }
2162 if os.Bionic() {
2163 osStructs = append(osStructs, bionicStructs...)
2164 }
Colin Crossa98d36d2022-03-07 14:39:49 -08002165 if os.Linux() && os.Class == Host {
2166 osStructs = append(osStructs, hostLinuxStructs...)
2167 }
Liz Kammerfdd72e62021-10-11 15:41:03 -04002168
2169 if os == LinuxMusl {
2170 osStructs = append(osStructs, getTargetStructs(ctx, archProperties, "Musl")...)
2171 }
2172 if os == Linux {
2173 osStructs = append(osStructs, getTargetStructs(ctx, archProperties, "Glibc")...)
2174 }
2175
2176 osStructs = append(osStructs, osSpecificStructs...)
2177
2178 if os.Class == Host && os != Windows {
2179 osStructs = append(osStructs, hostNotWindowsStructs...)
2180 }
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002181 osToProp[os.Name] = mergeStructs(ctx, osStructs, propertySet)
Chris Parsonsa37e1952021-09-28 16:47:36 -04002182
Liz Kammer9abd62d2021-05-21 08:37:59 -04002183 // For arm, x86, ...
2184 for _, arch := range osArchTypeMap[os] {
Chris Parsonsa37e1952021-09-28 16:47:36 -04002185 osArchStructs := make([]reflect.Value, 0)
2186
Chris Parsonsa37e1952021-09-28 16:47:36 -04002187 // Auto-combine with Linux_ and Bionic_ targets. This potentially results in
2188 // repetition and select() bloat, but use of Linux_* and Bionic_* targets is rare.
2189 // TODO(b/201423152): Look into cleanup.
2190 if os.Linux() {
2191 targetField := "Linux_" + arch.Name
Liz Kammerfdd72e62021-10-11 15:41:03 -04002192 targetStructs := getTargetStructs(ctx, archProperties, targetField)
2193 osArchStructs = append(osArchStructs, targetStructs...)
Chris Parsonsa37e1952021-09-28 16:47:36 -04002194 }
2195 if os.Bionic() {
2196 targetField := "Bionic_" + arch.Name
Liz Kammerfdd72e62021-10-11 15:41:03 -04002197 targetStructs := getTargetStructs(ctx, archProperties, targetField)
2198 osArchStructs = append(osArchStructs, targetStructs...)
Chris Parsonsa37e1952021-09-28 16:47:36 -04002199 }
Colin Cross2d295a22022-03-07 14:46:20 -08002200 if os == LinuxMusl {
2201 targetField := "Musl_" + arch.Name
2202 targetStructs := getTargetStructs(ctx, archProperties, targetField)
2203 osArchStructs = append(osArchStructs, targetStructs...)
2204 }
2205 if os == Linux {
2206 targetField := "Glibc_" + arch.Name
2207 targetStructs := getTargetStructs(ctx, archProperties, targetField)
2208 osArchStructs = append(osArchStructs, targetStructs...)
2209 }
Chris Parsonsa37e1952021-09-28 16:47:36 -04002210
Liz Kammerfdd72e62021-10-11 15:41:03 -04002211 targetField := GetCompoundTargetField(os, arch)
2212 targetName := fmt.Sprintf("%s_%s", os.Name, arch.Name)
2213 targetStructs := getTargetStructs(ctx, archProperties, targetField)
2214 osArchStructs = append(osArchStructs, targetStructs...)
2215
Chris Parsonsa37e1952021-09-28 16:47:36 -04002216 archOsToProp[targetName] = mergeStructs(ctx, osArchStructs, propertySet)
Liz Kammer9abd62d2021-05-21 08:37:59 -04002217 }
2218 }
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002219
Liz Kammer9abd62d2021-05-21 08:37:59 -04002220 axisToProps[bazel.OsConfigurationAxis] = osToProp
2221 axisToProps[bazel.OsArchConfigurationAxis] = archOsToProp
Liz Kammer9abd62d2021-05-21 08:37:59 -04002222 return axisToProps
Jingwen Chen5d864492021-02-24 07:20:12 -05002223}
Jingwen Chen91220d72021-03-24 02:18:33 -04002224
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04002225// Returns a struct matching the propertySet interface, containing properties specific to the targetName
2226// For example, given these arguments:
Colin Crossd079e0b2022-08-16 10:27:33 -07002227//
2228// propertySet = BaseCompilerProperties
2229// targetName = "android_arm"
2230//
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04002231// And given this Android.bp fragment:
Colin Crossd079e0b2022-08-16 10:27:33 -07002232//
2233// target:
2234// android_arm: {
2235// srcs: ["foo.c"],
2236// }
2237// android_arm64: {
2238// srcs: ["bar.c"],
2239// }
2240// }
2241//
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04002242// This would return a BaseCompilerProperties with BaseCompilerProperties.Srcs = ["foo.c"]
Liz Kammerfdd72e62021-10-11 15:41:03 -04002243func getTargetStructs(ctx ArchVariantContext, archProperties []interface{}, targetName string) []reflect.Value {
2244 var propertyStructs []reflect.Value
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04002245 for _, archProperty := range archProperties {
2246 archPropValues := reflect.ValueOf(archProperty).Elem()
2247 targetProp := archPropValues.FieldByName("Target").Elem()
2248 targetStruct, ok := getChildPropertyStruct(ctx, targetProp, targetName, targetName)
2249 if ok {
2250 propertyStructs = append(propertyStructs, targetStruct)
Chris Parsonsa37e1952021-09-28 16:47:36 -04002251 } else {
Liz Kammerfdd72e62021-10-11 15:41:03 -04002252 return []reflect.Value{}
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04002253 }
2254 }
2255
Liz Kammerfdd72e62021-10-11 15:41:03 -04002256 return propertyStructs
Chris Parsonsa37e1952021-09-28 16:47:36 -04002257}
2258
2259func mergeStructs(ctx ArchVariantContext, propertyStructs []reflect.Value, propertySet interface{}) interface{} {
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04002260 // Create a new instance of the requested property set
2261 value := reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
2262
2263 // Merge all the structs together
2264 for _, propertyStruct := range propertyStructs {
2265 mergePropertyStruct(ctx, value, propertyStruct)
2266 }
2267
2268 return value
2269}
Liz Kammere8303bd2022-02-16 09:02:48 -05002270
2271func printArchTypeStarlarkDict(dict map[ArchType][]string) string {
2272 valDict := make(map[string]string, len(dict))
2273 for k, v := range dict {
2274 valDict[k.String()] = starlark_fmt.PrintStringList(v, 1)
2275 }
2276 return starlark_fmt.PrintDict(valDict, 0)
2277}
2278
2279func printArchTypeNestedStarlarkDict(dict map[ArchType]map[string][]string) string {
2280 valDict := make(map[string]string, len(dict))
2281 for k, v := range dict {
2282 valDict[k.String()] = starlark_fmt.PrintStringListDict(v, 1)
2283 }
2284 return starlark_fmt.PrintDict(valDict, 0)
2285}
2286
2287func StarlarkArchConfigurations() string {
2288 return fmt.Sprintf(`
2289_arch_to_variants = %s
2290
2291_arch_to_cpu_variants = %s
2292
2293_arch_to_features = %s
2294
2295_android_arch_feature_for_arch_variant = %s
2296
2297arch_to_variants = _arch_to_variants
2298arch_to_cpu_variants = _arch_to_cpu_variants
2299arch_to_features = _arch_to_features
2300android_arch_feature_for_arch_variants = _android_arch_feature_for_arch_variant
2301`, printArchTypeStarlarkDict(archVariants),
2302 printArchTypeStarlarkDict(cpuVariants),
2303 printArchTypeStarlarkDict(archFeatures),
2304 printArchTypeNestedStarlarkDict(androidArchFeatureMap),
2305 )
2306}