blob: 7436660ac2702a0fa04c556a43f4811214fc1b6c [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"
Liz Kammer992918d2022-11-11 10:37:54 -050019 "encoding/json"
Colin Cross3f40fa42015-01-30 17:27:36 -080020 "fmt"
21 "reflect"
22 "runtime"
Cole Faustc843b992022-08-02 18:06:50 -070023 "sort"
Colin Cross3f40fa42015-01-30 17:27:36 -080024 "strings"
Colin Crossf6566ed2015-03-24 11:13:38 -070025
Colin Crosscb0ac952021-07-20 13:17:15 -070026 "android/soong/bazel"
Liz Kammere8303bd2022-02-16 09:02:48 -050027 "android/soong/starlark_fmt"
Colin Crosscb0ac952021-07-20 13:17:15 -070028
Colin Cross0f7d2ef2019-10-16 11:03:10 -070029 "github.com/google/blueprint"
Colin Cross617b88a2020-08-24 18:04:09 -070030 "github.com/google/blueprint/bootstrap"
Colin Crossf6566ed2015-03-24 11:13:38 -070031 "github.com/google/blueprint/proptools"
Colin Cross3f40fa42015-01-30 17:27:36 -080032)
33
Colin Cross3f40fa42015-01-30 17:27:36 -080034/*
35Example blueprints file containing all variant property groups, with comment listing what type
36of variants get properties in that group:
37
38module {
39 arch: {
40 arm: {
41 // Host or device variants with arm architecture
42 },
43 arm64: {
44 // Host or device variants with arm64 architecture
45 },
Colin Cross3f40fa42015-01-30 17:27:36 -080046 x86: {
47 // Host or device variants with x86 architecture
48 },
49 x86_64: {
50 // Host or device variants with x86_64 architecture
51 },
52 },
53 multilib: {
54 lib32: {
55 // Host or device variants for 32-bit architectures
56 },
57 lib64: {
58 // Host or device variants for 64-bit architectures
59 },
60 },
61 target: {
62 android: {
Martin Stjernholme284b482020-09-23 21:03:27 +010063 // Device variants (implies Bionic)
Colin Cross3f40fa42015-01-30 17:27:36 -080064 },
65 host: {
66 // Host variants
67 },
Martin Stjernholme284b482020-09-23 21:03:27 +010068 bionic: {
69 // Bionic (device and host) variants
70 },
71 linux_bionic: {
72 // Bionic host variants
73 },
74 linux: {
75 // Bionic (device and host) and Linux glibc variants
76 },
Dan Willemsen5746bd42017-10-02 19:42:01 -070077 linux_glibc: {
Martin Stjernholme284b482020-09-23 21:03:27 +010078 // Linux host variants (using non-Bionic libc)
Colin Cross3f40fa42015-01-30 17:27:36 -080079 },
80 darwin: {
81 // Darwin host variants
82 },
83 windows: {
84 // Windows host variants
85 },
86 not_windows: {
87 // Non-windows host variants
88 },
Martin Stjernholme284b482020-09-23 21:03:27 +010089 android_arm: {
90 // Any <os>_<arch> combination restricts to that os and arch
91 },
Colin Cross3f40fa42015-01-30 17:27:36 -080092 },
93}
94*/
Colin Cross7d5136f2015-05-11 13:39:40 -070095
Colin Cross3f40fa42015-01-30 17:27:36 -080096// An Arch indicates a single CPU architecture.
97type Arch struct {
Colin Crossa6845402020-11-16 15:08:19 -080098 // The type of the architecture (arm, arm64, x86, or x86_64).
99 ArchType ArchType
100
101 // The variant of the architecture, for example "armv7-a" or "armv7-a-neon" for arm.
102 ArchVariant string
103
104 // The variant of the CPU, for example "cortex-a53" for arm64.
105 CpuVariant string
106
107 // The list of Android app ABIs supported by the CPU architecture, for example "arm64-v8a".
108 Abi []string
109
110 // The list of arch-specific features supported by the CPU architecture, for example "neon".
Colin Crossc5c24ad2015-11-20 15:35:00 -0800111 ArchFeatures []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800112}
113
Colin Crossa6845402020-11-16 15:08:19 -0800114// String returns the Arch as a string. The value is used as the name of the variant created
115// by archMutator.
Colin Cross3f40fa42015-01-30 17:27:36 -0800116func (a Arch) String() string {
Colin Crossd3ba0392015-05-07 14:11:29 -0700117 s := a.ArchType.String()
Colin Cross3f40fa42015-01-30 17:27:36 -0800118 if a.ArchVariant != "" {
119 s += "_" + a.ArchVariant
120 }
121 if a.CpuVariant != "" {
122 s += "_" + a.CpuVariant
123 }
124 return s
125}
126
Colin Crossa6845402020-11-16 15:08:19 -0800127// ArchType is used to define the 4 supported architecture types (arm, arm64, x86, x86_64), as
128// well as the "common" architecture used for modules that support multiple architectures, for
129// example Java modules.
Colin Cross3f40fa42015-01-30 17:27:36 -0800130type ArchType struct {
Colin Crossa6845402020-11-16 15:08:19 -0800131 // Name is the name of the architecture type, "arm", "arm64", "x86", or "x86_64".
132 Name string
133
134 // Field is the name of the field used in properties that refer to the architecture, e.g. "Arm64".
135 Field string
136
137 // Multilib is either "lib32" or "lib64" for 32-bit or 64-bit architectures.
Colin Crossec193632015-07-06 17:49:43 -0700138 Multilib string
Colin Cross3f40fa42015-01-30 17:27:36 -0800139}
140
Colin Crossa6845402020-11-16 15:08:19 -0800141// String returns the name of the ArchType.
142func (a ArchType) String() string {
143 return a.Name
144}
145
146const COMMON_VARIANT = "common"
147
148var (
149 archTypeList []ArchType
150
Colin Crossf05b0d32022-07-14 18:10:34 -0700151 Arm = newArch("arm", "lib32")
152 Arm64 = newArch("arm64", "lib64")
153 Riscv64 = newArch("riscv64", "lib64")
154 X86 = newArch("x86", "lib32")
155 X86_64 = newArch("x86_64", "lib64")
Colin Crossa6845402020-11-16 15:08:19 -0800156
157 Common = ArchType{
158 Name: COMMON_VARIANT,
159 }
160)
161
162var archTypeMap = map[string]ArchType{}
163
Colin Crossec193632015-07-06 17:49:43 -0700164func newArch(name, multilib string) ArchType {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700165 archType := ArchType{
Colin Crossec193632015-07-06 17:49:43 -0700166 Name: name,
Dan Willemsenb1957a52016-06-23 23:44:54 -0700167 Field: proptools.FieldNameForProperty(name),
Colin Crossec193632015-07-06 17:49:43 -0700168 Multilib: multilib,
Colin Cross3f40fa42015-01-30 17:27:36 -0800169 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700170 archTypeList = append(archTypeList, archType)
Colin Crossa6845402020-11-16 15:08:19 -0800171 archTypeMap[name] = archType
Dan Willemsenb1957a52016-06-23 23:44:54 -0700172 return archType
Colin Cross3f40fa42015-01-30 17:27:36 -0800173}
174
Ustaeabf0f32021-12-06 15:17:23 -0500175// ArchTypeList returns a slice copy of the 4 supported ArchTypes for arm,
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000176// arm64, x86 and x86_64.
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -0700177func ArchTypeList() []ArchType {
178 return append([]ArchType(nil), archTypeList...)
179}
180
Colin Crossa6845402020-11-16 15:08:19 -0800181// MarshalText allows an ArchType to be serialized through any encoder that supports
182// encoding.TextMarshaler.
Colin Cross74ba9622019-02-11 15:11:14 -0800183func (a ArchType) MarshalText() ([]byte, error) {
Jeongik Chabec4d032021-04-15 08:55:38 +0900184 return []byte(a.String()), nil
Colin Cross74ba9622019-02-11 15:11:14 -0800185}
186
Colin Crossa6845402020-11-16 15:08:19 -0800187var _ encoding.TextMarshaler = ArchType{}
Colin Cross74ba9622019-02-11 15:11:14 -0800188
Colin Crossa6845402020-11-16 15:08:19 -0800189// UnmarshalText allows an ArchType to be deserialized through any decoder that supports
190// encoding.TextUnmarshaler.
Colin Cross74ba9622019-02-11 15:11:14 -0800191func (a *ArchType) UnmarshalText(text []byte) error {
192 if u, ok := archTypeMap[string(text)]; ok {
193 *a = u
194 return nil
195 }
196
197 return fmt.Errorf("unknown ArchType %q", text)
198}
199
Colin Crossa6845402020-11-16 15:08:19 -0800200var _ encoding.TextUnmarshaler = &ArchType{}
Colin Crossa1ad8d12016-06-01 17:09:44 -0700201
Colin Crossa6845402020-11-16 15:08:19 -0800202// OsClass is an enum that describes whether a variant of a module runs on the host, on the device,
203// or is generic.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700204type OsClass int
205
206const (
Colin Crossa6845402020-11-16 15:08:19 -0800207 // Generic is used for variants of modules that are not OS-specific.
Dan Willemsen0e2d97b2016-11-28 17:50:06 -0800208 Generic OsClass = iota
Colin Crossa6845402020-11-16 15:08:19 -0800209 // Device is used for variants of modules that run on the device.
Dan Willemsen0e2d97b2016-11-28 17:50:06 -0800210 Device
Colin Crossa6845402020-11-16 15:08:19 -0800211 // Host is used for variants of modules that run on the host.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700212 Host
Colin Crossa1ad8d12016-06-01 17:09:44 -0700213)
214
Colin Crossa6845402020-11-16 15:08:19 -0800215// String returns the OsClass as a string.
Colin Cross67a5c132017-05-09 13:45:28 -0700216func (class OsClass) String() string {
217 switch class {
218 case Generic:
219 return "generic"
220 case Device:
221 return "device"
222 case Host:
223 return "host"
Colin Cross67a5c132017-05-09 13:45:28 -0700224 default:
225 panic(fmt.Errorf("unknown class %d", class))
226 }
227}
228
Colin Crossa6845402020-11-16 15:08:19 -0800229// OsType describes an OS variant of a module.
230type OsType struct {
231 // Name is the name of the OS. It is also used as the name of the property in Android.bp
232 // files.
233 Name string
234
235 // Field is the name of the OS converted to an exported field name, i.e. with the first
236 // character capitalized.
237 Field string
238
239 // Class is the OsClass of the OS.
240 Class OsClass
241
242 // DefaultDisabled is set when the module variants for the OS should not be created unless
243 // the module explicitly requests them. This is used to limit Windows cross compilation to
244 // only modules that need it.
245 DefaultDisabled bool
246}
247
248// String returns the name of the OsType.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700249func (os OsType) String() string {
250 return os.Name
Colin Cross54c71122016-06-01 17:09:44 -0700251}
252
Colin Crossa6845402020-11-16 15:08:19 -0800253// Bionic returns true if the OS uses the Bionic libc runtime, i.e. if the OS is Android or
254// is Linux with Bionic.
Dan Willemsen866b5632017-09-22 12:28:24 -0700255func (os OsType) Bionic() bool {
256 return os == Android || os == LinuxBionic
257}
258
Colin Crossa6845402020-11-16 15:08:19 -0800259// Linux returns true if the OS uses the Linux kernel, i.e. if the OS is Android or is Linux
260// with or without the Bionic libc runtime.
Dan Willemsen866b5632017-09-22 12:28:24 -0700261func (os OsType) Linux() bool {
Colin Cross528d67e2021-07-23 22:23:07 +0000262 return os == Android || os == Linux || os == LinuxBionic || os == LinuxMusl
Dan Willemsen866b5632017-09-22 12:28:24 -0700263}
264
Colin Crossa6845402020-11-16 15:08:19 -0800265// newOsType constructs an OsType and adds it to the global lists.
266func newOsType(name string, class OsClass, defDisabled bool, archTypes ...ArchType) OsType {
267 checkCalledFromInit()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700268 os := OsType{
269 Name: name,
Colin Crossa6845402020-11-16 15:08:19 -0800270 Field: proptools.FieldNameForProperty(name),
Colin Crossa1ad8d12016-06-01 17:09:44 -0700271 Class: class,
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800272
273 DefaultDisabled: defDisabled,
Colin Cross54c71122016-06-01 17:09:44 -0700274 }
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000275 osTypeList = append(osTypeList, os)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800276
277 if _, found := commonTargetMap[name]; found {
278 panic(fmt.Errorf("Found Os type duplicate during OsType registration: %q", name))
279 } else {
Colin Crosse9fe2942020-11-10 18:12:15 -0800280 commonTargetMap[name] = Target{Os: os, Arch: CommonArch}
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800281 }
Colin Crossa6845402020-11-16 15:08:19 -0800282 osArchTypeMap[os] = archTypes
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800283
Colin Crossa1ad8d12016-06-01 17:09:44 -0700284 return os
285}
286
Colin Crossa6845402020-11-16 15:08:19 -0800287// osByName returns the OsType that has the given name, or NoOsType if none match.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700288func osByName(name string) OsType {
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000289 for _, os := range osTypeList {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700290 if os.Name == name {
291 return os
292 }
293 }
294
295 return NoOsType
Dan Willemsen490fd492015-11-24 17:53:15 -0800296}
297
Colin Crossa6845402020-11-16 15:08:19 -0800298var (
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000299 // osTypeList contains a list of all the supported OsTypes, including ones not supported
Colin Crossa6845402020-11-16 15:08:19 -0800300 // by the current build host or the target device.
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000301 osTypeList []OsType
Colin Crossa6845402020-11-16 15:08:19 -0800302 // commonTargetMap maps names of OsTypes to the corresponding common Target, i.e. the
303 // Target with the same OsType and the common ArchType.
304 commonTargetMap = make(map[string]Target)
305 // osArchTypeMap maps OsTypes to the list of supported ArchTypes for that OS.
306 osArchTypeMap = map[OsType][]ArchType{}
307
308 // NoOsType is a placeholder for when no OS is needed.
309 NoOsType OsType
310 // Linux is the OS for the Linux kernel plus the glibc runtime.
311 Linux = newOsType("linux_glibc", Host, false, X86, X86_64)
Colin Cross528d67e2021-07-23 22:23:07 +0000312 // LinuxMusl is the OS for the Linux kernel plus the musl runtime.
Colin Crossa9b2aac2022-06-15 17:25:51 -0700313 LinuxMusl = newOsType("linux_musl", Host, false, X86, X86_64, Arm64, Arm)
Colin Crossa6845402020-11-16 15:08:19 -0800314 // Darwin is the OS for MacOS/Darwin host machines.
Dan Willemsen8528f4e2021-10-19 00:22:06 -0700315 Darwin = newOsType("darwin", Host, false, Arm64, X86_64)
Colin Crossa6845402020-11-16 15:08:19 -0800316 // LinuxBionic is the OS for the Linux kernel plus the Bionic libc runtime, but without the
317 // rest of Android.
318 LinuxBionic = newOsType("linux_bionic", Host, false, Arm64, X86_64)
319 // Windows the OS for Windows host machines.
320 Windows = newOsType("windows", Host, true, X86, X86_64)
321 // Android is the OS for target devices that run all of Android, including the Linux kernel
322 // and the Bionic libc runtime.
Colin Crossf05b0d32022-07-14 18:10:34 -0700323 Android = newOsType("android", Device, false, Arm, Arm64, Riscv64, X86, X86_64)
Colin Crossa6845402020-11-16 15:08:19 -0800324
325 // CommonOS is a pseudo OSType for a common OS variant, which is OsType agnostic and which
326 // has dependencies on all the OS variants.
327 CommonOS = newOsType("common_os", Generic, false)
Colin Crosse9fe2942020-11-10 18:12:15 -0800328
329 // CommonArch is the Arch for all modules that are os-specific but not arch specific,
330 // for example most Java modules.
331 CommonArch = Arch{ArchType: Common}
dimitry1f33e402019-03-26 12:39:31 +0100332)
333
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000334// OsTypeList returns a slice copy of the supported OsTypes.
335func OsTypeList() []OsType {
336 return append([]OsType(nil), osTypeList...)
337}
338
Colin Crossa6845402020-11-16 15:08:19 -0800339// Target specifies the OS and architecture that a module is being compiled for.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700340type Target struct {
Colin Crossa6845402020-11-16 15:08:19 -0800341 // Os the OS that the module is being compiled for (e.g. "linux_glibc", "android").
342 Os OsType
343 // Arch is the architecture that the module is being compiled for.
344 Arch Arch
345 // NativeBridge is NativeBridgeEnabled if the architecture is supported using NativeBridge
346 // (i.e. arm on x86) for this device.
347 NativeBridge NativeBridgeSupport
348 // NativeBridgeHostArchName is the name of the real architecture that is used to implement
349 // the NativeBridge architecture. For example, for arm on x86 this would be "x86".
dimitry8d6dde82019-07-11 10:23:53 +0200350 NativeBridgeHostArchName string
Colin Crossa6845402020-11-16 15:08:19 -0800351 // NativeBridgeRelativePath is the name of the subdirectory that will contain NativeBridge
352 // libraries and binaries.
dimitry8d6dde82019-07-11 10:23:53 +0200353 NativeBridgeRelativePath string
Jiyong Park1613e552020-09-14 19:43:17 +0900354
355 // HostCross is true when the target cannot run natively on the current build host.
356 // For example, linux_glibc_x86 returns true on a regular x86/i686/Linux machines, but returns false
357 // on Mac (different OS), or on 64-bit only i686/Linux machines (unsupported arch).
358 HostCross bool
Colin Crossd3ba0392015-05-07 14:11:29 -0700359}
360
Colin Crossa6845402020-11-16 15:08:19 -0800361// NativeBridgeSupport is an enum that specifies if a Target supports NativeBridge.
362type NativeBridgeSupport bool
363
364const (
365 NativeBridgeDisabled NativeBridgeSupport = false
366 NativeBridgeEnabled NativeBridgeSupport = true
367)
368
369// String returns the OS and arch variations used for the Target.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700370func (target Target) String() string {
Colin Crossa195f912019-10-16 11:07:20 -0700371 return target.OsVariation() + "_" + target.ArchVariation()
372}
373
Colin Crossa6845402020-11-16 15:08:19 -0800374// OsVariation returns the name of the variation used by the osMutator for the Target.
Colin Crossa195f912019-10-16 11:07:20 -0700375func (target Target) OsVariation() string {
376 return target.Os.String()
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700377}
378
Colin Crossa6845402020-11-16 15:08:19 -0800379// ArchVariation returns the name of the variation used by the archMutator for the Target.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700380func (target Target) ArchVariation() string {
381 var variation string
dimitry1f33e402019-03-26 12:39:31 +0100382 if target.NativeBridge {
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700383 variation = "native_bridge_"
dimitry1f33e402019-03-26 12:39:31 +0100384 }
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700385 variation += target.Arch.String()
386
Colin Crossa195f912019-10-16 11:07:20 -0700387 return variation
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700388}
389
Colin Crossa6845402020-11-16 15:08:19 -0800390// Variations returns a list of blueprint.Variations for the osMutator and archMutator for the
391// Target.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700392func (target Target) Variations() []blueprint.Variation {
393 return []blueprint.Variation{
Colin Crossa195f912019-10-16 11:07:20 -0700394 {Mutator: "os", Variation: target.OsVariation()},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700395 {Mutator: "arch", Variation: target.ArchVariation()},
396 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800397}
398
Colin Crossa6845402020-11-16 15:08:19 -0800399// osMutator splits an arch-specific module into a variant for each OS that is enabled for the
400// module. It uses the HostOrDevice value passed to InitAndroidArchModule and the
401// device_supported and host_supported properties to determine which OsTypes are enabled for this
402// module, then searches through the Targets to determine which have enabled Targets for this
403// module.
Colin Cross617b88a2020-08-24 18:04:09 -0700404func osMutator(bpctx blueprint.BottomUpMutatorContext) {
Colin Crossa195f912019-10-16 11:07:20 -0700405 var module Module
406 var ok bool
Colin Cross617b88a2020-08-24 18:04:09 -0700407 if module, ok = bpctx.Module().(Module); !ok {
Colin Crossa6845402020-11-16 15:08:19 -0800408 // The module is not a Soong module, it is a Blueprint module.
Colin Cross617b88a2020-08-24 18:04:09 -0700409 if bootstrap.IsBootstrapModule(bpctx.Module()) {
410 // Bootstrap Go modules are always the build OS or linux bionic.
411 config := bpctx.Config().(Config)
412 osNames := []string{config.BuildOSTarget.OsVariation()}
413 for _, hostCrossTarget := range config.Targets[LinuxBionic] {
414 if hostCrossTarget.Arch.ArchType == config.BuildOSTarget.Arch.ArchType {
415 osNames = append(osNames, hostCrossTarget.OsVariation())
416 }
417 }
418 osNames = FirstUniqueStrings(osNames)
419 bpctx.CreateVariations(osNames...)
420 }
Colin Crossa195f912019-10-16 11:07:20 -0700421 return
422 }
423
Colin Cross617b88a2020-08-24 18:04:09 -0700424 // Bootstrap Go module support above requires this mutator to be a
425 // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext
426 // filters out non-Soong modules. Now that we've handled them, create a
427 // normal android.BottomUpMutatorContext.
Colin Crossb63d7b32023-12-07 16:54:51 -0800428 mctx := bottomUpMutatorContextFactory(bpctx, module, false)
Colin Cross617b88a2020-08-24 18:04:09 -0700429
Colin Crossa195f912019-10-16 11:07:20 -0700430 base := module.base()
431
Colin Crossa6845402020-11-16 15:08:19 -0800432 // Nothing to do for modules that are not architecture specific (e.g. a genrule).
Colin Crossa195f912019-10-16 11:07:20 -0700433 if !base.ArchSpecific() {
434 return
435 }
436
Colin Crossa6845402020-11-16 15:08:19 -0800437 // Collect a list of OSTypes supported by this module based on the HostOrDevice value
438 // passed to InitAndroidArchModule and the device_supported and host_supported properties.
Colin Crossa195f912019-10-16 11:07:20 -0700439 var moduleOSList []OsType
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000440 for _, os := range osTypeList {
Jiyong Park1613e552020-09-14 19:43:17 +0900441 for _, t := range mctx.Config().Targets[os] {
Colin Cross08d6f8f2020-11-19 02:33:19 +0000442 if base.supportsTarget(t) {
Jiyong Park1613e552020-09-14 19:43:17 +0900443 moduleOSList = append(moduleOSList, os)
444 break
Colin Crossa195f912019-10-16 11:07:20 -0700445 }
446 }
Colin Crossa195f912019-10-16 11:07:20 -0700447 }
448
Colin Crossa6845402020-11-16 15:08:19 -0800449 // If there are no supported OSes then disable the module.
Colin Crossa195f912019-10-16 11:07:20 -0700450 if len(moduleOSList) == 0 {
Inseob Kimeec88e12020-01-22 11:11:29 +0900451 base.Disable()
Colin Crossa195f912019-10-16 11:07:20 -0700452 return
453 }
454
Colin Crossa6845402020-11-16 15:08:19 -0800455 // Convert the list of supported OsTypes to the variation names.
Colin Crossa195f912019-10-16 11:07:20 -0700456 osNames := make([]string, len(moduleOSList))
Colin Crossa195f912019-10-16 11:07:20 -0700457 for i, os := range moduleOSList {
458 osNames[i] = os.String()
459 }
460
Paul Duffin1356d8c2020-02-25 19:26:33 +0000461 createCommonOSVariant := base.commonProperties.CreateCommonOSVariant
462 if createCommonOSVariant {
Colin Crossa6845402020-11-16 15:08:19 -0800463 // A CommonOS variant was requested so add it to the list of OS variants to
Paul Duffin1356d8c2020-02-25 19:26:33 +0000464 // create. It needs to be added to the end because it needs to depend on the
465 // the other variants in the list returned by CreateVariations(...) and inter
466 // variant dependencies can only be created from a later variant in that list to
467 // an earlier one. That is because variants are always processed in the order in
468 // which they are returned from CreateVariations(...).
469 osNames = append(osNames, CommonOS.Name)
470 moduleOSList = append(moduleOSList, CommonOS)
Colin Crossa195f912019-10-16 11:07:20 -0700471 }
472
Colin Crossa6845402020-11-16 15:08:19 -0800473 // Create the variations, annotate each one with which OS it was created for, and
474 // squash the appropriate OS-specific properties into the top level properties.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000475 modules := mctx.CreateVariations(osNames...)
476 for i, m := range modules {
477 m.base().commonProperties.CompileOS = moduleOSList[i]
478 m.base().setOSProperties(mctx)
479 }
480
481 if createCommonOSVariant {
482 // A CommonOS variant was requested so add dependencies from it (the last one in
483 // the list) to the OS type specific variants.
484 last := len(modules) - 1
485 commonOSVariant := modules[last]
486 commonOSVariant.base().commonProperties.CommonOSVariant = true
487 for _, module := range modules[0:last] {
488 // Ignore modules that are enabled. Note, this will only avoid adding
489 // dependencies on OsType variants that are explicitly disabled in their
490 // properties. The CommonOS variant will still depend on disabled variants
491 // if they are disabled afterwards, e.g. in archMutator if
492 if module.Enabled() {
493 mctx.AddInterVariantDependency(commonOsToOsSpecificVariantTag, commonOSVariant, module)
494 }
495 }
496 }
497}
498
Colin Crossc179ea62020-10-09 10:54:15 -0700499type archDepTag struct {
500 blueprint.BaseDependencyTag
501 name string
502}
Paul Duffin1356d8c2020-02-25 19:26:33 +0000503
Colin Crossc179ea62020-10-09 10:54:15 -0700504// Identifies the dependency from CommonOS variant to the os specific variants.
505var commonOsToOsSpecificVariantTag = archDepTag{name: "common os to os specific"}
506
Paul Duffin1356d8c2020-02-25 19:26:33 +0000507// Get the OsType specific variants for the current CommonOS variant.
508//
509// The returned list will only contain enabled OsType specific variants of the
510// module referenced in the supplied context. An empty list is returned if there
511// are no enabled variants or the supplied context is not for an CommonOS
512// variant.
513func GetOsSpecificVariantsOfCommonOSVariant(mctx BaseModuleContext) []Module {
514 var variants []Module
515 mctx.VisitDirectDeps(func(m Module) {
516 if mctx.OtherModuleDependencyTag(m) == commonOsToOsSpecificVariantTag {
517 if m.Enabled() {
518 variants = append(variants, m)
519 }
520 }
521 })
Paul Duffin1356d8c2020-02-25 19:26:33 +0000522 return variants
Colin Crossa195f912019-10-16 11:07:20 -0700523}
524
Dan Willemsen47450072021-10-19 20:24:49 -0700525var DarwinUniversalVariantTag = archDepTag{name: "darwin universal binary"}
526
Colin Crossee0bc3b2018-10-02 22:01:37 -0700527// archMutator splits a module into a variant for each Target requested by the module. Target selection
Colin Crossa6845402020-11-16 15:08:19 -0800528// for a module is in three levels, OsClass, multilib, and then Target.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700529// OsClass selection is determined by:
Colin Crossd079e0b2022-08-16 10:27:33 -0700530// - The HostOrDeviceSupported value passed in to InitAndroidArchModule by the module type factory, which selects
531// whether the module type can compile for host, device or both.
532// - The host_supported and device_supported properties on the module.
533//
Roland Levillainf5b635d2019-06-05 14:42:57 +0100534// 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 -0700535// for the module, the Device OsClass is selected.
536// Within each selected OsClass, the multilib selection is determined by:
Colin Crossd079e0b2022-08-16 10:27:33 -0700537// - The compile_multilib property if it set (which may be overridden by target.android.compile_multilib or
538// target.host.compile_multilib).
539// - The default multilib passed to InitAndroidArchModule if compile_multilib was not set.
540//
Colin Crossee0bc3b2018-10-02 22:01:37 -0700541// Valid multilib values include:
Colin Crossd079e0b2022-08-16 10:27:33 -0700542//
543// "both": compile for all Targets supported by the OsClass (generally x86_64 and x86, or arm64 and arm).
544// "first": compile for only a single preferred Target supported by the OsClass. This is generally x86_64 or arm64,
545// but may be arm for a 32-bit only build.
546// "32": compile for only a single 32-bit Target supported by the OsClass.
547// "64": compile for only a single 64-bit Target supported by the OsClass.
548// "common": compile a for a single Target that will work on all Targets supported by the OsClass (for example Java).
549// "common_first": compile a for a Target that will work on all Targets supported by the OsClass
550// (same as "common"), plus a second Target for the preferred Target supported by the OsClass
551// (same as "first"). This is used for java_binary that produces a common .jar and a wrapper
552// executable script.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700553//
554// Once the list of Targets is determined, the module is split into a variant for each Target.
555//
556// Modules can be initialized with InitAndroidMultiTargetsArchModule, in which case they will be split by OsClass,
557// 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 -0700558func archMutator(bpctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700559 var module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800560 var ok bool
Colin Cross617b88a2020-08-24 18:04:09 -0700561 if module, ok = bpctx.Module().(Module); !ok {
562 if bootstrap.IsBootstrapModule(bpctx.Module()) {
563 // Bootstrap Go modules are always the build architecture.
564 bpctx.CreateVariations(bpctx.Config().(Config).BuildOSTarget.ArchVariation())
565 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800566 return
567 }
568
Colin Cross617b88a2020-08-24 18:04:09 -0700569 // Bootstrap Go module support above requires this mutator to be a
570 // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext
571 // filters out non-Soong modules. Now that we've handled them, create a
572 // normal android.BottomUpMutatorContext.
Colin Crossb63d7b32023-12-07 16:54:51 -0800573 mctx := bottomUpMutatorContextFactory(bpctx, module, false)
Colin Cross617b88a2020-08-24 18:04:09 -0700574
Colin Cross5eca7cb2018-10-02 14:02:10 -0700575 base := module.base()
576
577 if !base.ArchSpecific() {
Colin Crossb9db4802016-06-03 01:50:47 +0000578 return
579 }
580
Colin Crossa195f912019-10-16 11:07:20 -0700581 os := base.commonProperties.CompileOS
Paul Duffin1356d8c2020-02-25 19:26:33 +0000582 if os == CommonOS {
583 // Make sure that the target related properties are initialized for the
584 // CommonOS variant.
585 addTargetProperties(module, commonTargetMap[os.Name], nil, true)
586
587 // Do not create arch specific variants for the CommonOS variant.
588 return
589 }
590
Colin Crossa195f912019-10-16 11:07:20 -0700591 osTargets := mctx.Config().Targets[os]
Colin Crossfb0c16e2019-11-20 17:12:35 -0800592 image := base.commonProperties.ImageVariation
Colin Crossa6845402020-11-16 15:08:19 -0800593 // Filter NativeBridge targets unless they are explicitly supported.
594 // Skip creating native bridge variants for non-core modules.
Paul Duffine3d1ae42021-09-03 17:47:17 +0100595 if os == Android && !(base.IsNativeBridgeSupported() && image == CoreVariation) {
Colin Cross83bead42019-12-18 10:45:46 -0800596
Colin Crossa195f912019-10-16 11:07:20 -0700597 var targets []Target
598 for _, t := range osTargets {
599 if !t.NativeBridge {
600 targets = append(targets, t)
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700601 }
602 }
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700603
Colin Crossa195f912019-10-16 11:07:20 -0700604 osTargets = targets
605 }
Colin Crossee0bc3b2018-10-02 22:01:37 -0700606
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700607 // only the primary arch in the ramdisk / vendor_ramdisk / recovery partition
Inseob Kim08758f02021-04-08 21:13:22 +0900608 if os == Android && (module.InstallInRecovery() || module.InstallInRamdisk() || module.InstallInVendorRamdisk() || module.InstallInDebugRamdisk()) {
Colin Crossa195f912019-10-16 11:07:20 -0700609 osTargets = []Target{osTargets[0]}
610 }
dimitry1f33e402019-03-26 12:39:31 +0100611
Jaewoong Jung003d8082021-02-24 17:39:54 -0800612 // Windows builds always prefer 32-bit
613 prefer32 := os == Windows
dimitry1f33e402019-03-26 12:39:31 +0100614
Colin Crossa6845402020-11-16 15:08:19 -0800615 // Determine the multilib selection for this module.
Christopher Ferris98f10222022-07-13 23:16:52 -0700616 ignorePrefer32OnDevice := mctx.Config().IgnorePrefer32OnDevice()
617 multilib, extraMultilib := decodeMultilib(base, os, ignorePrefer32OnDevice)
Colin Crossa6845402020-11-16 15:08:19 -0800618
619 // Convert the multilib selection into a list of Targets.
Colin Crossa195f912019-10-16 11:07:20 -0700620 targets, err := decodeMultilibTargets(multilib, osTargets, prefer32)
621 if err != nil {
622 mctx.ModuleErrorf("%s", err.Error())
623 }
Colin Cross5eca7cb2018-10-02 14:02:10 -0700624
Colin Crossc0f0eb82022-07-19 14:41:11 -0700625 // If there are no supported targets disable the module.
626 if len(targets) == 0 {
627 base.Disable()
628 return
629 }
630
Colin Crossa6845402020-11-16 15:08:19 -0800631 // If the module is using extraMultilib, decode the extraMultilib selection into
632 // a separate list of Targets.
Colin Crossa195f912019-10-16 11:07:20 -0700633 var multiTargets []Target
634 if extraMultilib != "" {
635 multiTargets, err = decodeMultilibTargets(extraMultilib, osTargets, prefer32)
Colin Crossa1ad8d12016-06-01 17:09:44 -0700636 if err != nil {
637 mctx.ModuleErrorf("%s", err.Error())
638 }
Colin Crossc0f0eb82022-07-19 14:41:11 -0700639 multiTargets = filterHostCross(multiTargets, targets[0].HostCross)
Colin Crossb9db4802016-06-03 01:50:47 +0000640 }
641
Colin Crossa6845402020-11-16 15:08:19 -0800642 // Recovery is always the primary architecture, filter out any other architectures.
Inseob Kim20fb5d42021-02-02 20:07:58 +0900643 // Common arch is also allowed
Colin Crossfb0c16e2019-11-20 17:12:35 -0800644 if image == RecoveryVariation {
645 primaryArch := mctx.Config().DevicePrimaryArchType()
Inseob Kim20fb5d42021-02-02 20:07:58 +0900646 targets = filterToArch(targets, primaryArch, Common)
647 multiTargets = filterToArch(multiTargets, primaryArch, Common)
Colin Crossfb0c16e2019-11-20 17:12:35 -0800648 }
649
Colin Crossa6845402020-11-16 15:08:19 -0800650 // If there are no supported targets disable the module.
Colin Crossa195f912019-10-16 11:07:20 -0700651 if len(targets) == 0 {
Inseob Kimeec88e12020-01-22 11:11:29 +0900652 base.Disable()
Dan Willemsen3f32f032016-07-11 14:36:48 -0700653 return
654 }
655
Colin Crossa6845402020-11-16 15:08:19 -0800656 // Convert the targets into a list of arch variation names.
Colin Crossa195f912019-10-16 11:07:20 -0700657 targetNames := make([]string, len(targets))
Colin Crossa195f912019-10-16 11:07:20 -0700658 for i, target := range targets {
659 targetNames[i] = target.ArchVariation()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700660 }
661
Colin Crossa6845402020-11-16 15:08:19 -0800662 // Create the variations, annotate each one with which Target it was created for, and
663 // squash the appropriate arch-specific properties into the top level properties.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700664 modules := mctx.CreateVariations(targetNames...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800665 for i, m := range modules {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000666 addTargetProperties(m, targets[i], multiTargets, i == 0)
Colin Cross617b88a2020-08-24 18:04:09 -0700667 m.base().setArchProperties(mctx)
Dan Willemsen8528f4e2021-10-19 00:22:06 -0700668
669 // Install support doesn't understand Darwin+Arm64
670 if os == Darwin && targets[i].HostCross {
671 m.base().commonProperties.SkipInstall = true
672 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800673 }
Dan Willemsen47450072021-10-19 20:24:49 -0700674
675 // Create a dependency for Darwin Universal binaries from the primary to secondary
676 // architecture. The module itself will be responsible for calling lipo to merge the outputs.
677 if os == Darwin {
678 if multilib == "darwin_universal" && len(modules) == 2 {
679 mctx.AddInterVariantDependency(DarwinUniversalVariantTag, modules[1], modules[0])
680 } else if multilib == "darwin_universal_common_first" && len(modules) == 3 {
681 mctx.AddInterVariantDependency(DarwinUniversalVariantTag, modules[2], modules[1])
682 }
683 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800684}
685
Colin Crossa6845402020-11-16 15:08:19 -0800686// addTargetProperties annotates a variant with the Target is is being compiled for, the list
687// of additional Targets it is supporting (if any), and whether it is the primary Target for
688// the module.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000689func addTargetProperties(m Module, target Target, multiTargets []Target, primaryTarget bool) {
690 m.base().commonProperties.CompileTarget = target
691 m.base().commonProperties.CompileMultiTargets = multiTargets
692 m.base().commonProperties.CompilePrimary = primaryTarget
693}
694
Colin Crossa6845402020-11-16 15:08:19 -0800695// decodeMultilib returns the appropriate compile_multilib property for the module, or the default
696// multilib from the factory's call to InitAndroidArchModule if none was set. For modules that
697// called InitAndroidMultiTargetsArchModule it always returns "common" for multilib, and returns
698// the actual multilib in extraMultilib.
Christopher Ferris98f10222022-07-13 23:16:52 -0700699func decodeMultilib(base *ModuleBase, os OsType, ignorePrefer32OnDevice bool) (multilib, extraMultilib string) {
Colin Crossa6845402020-11-16 15:08:19 -0800700 // First check the "android.compile_multilib" or "host.compile_multilib" properties.
Dan Willemsen47450072021-10-19 20:24:49 -0700701 switch os.Class {
Colin Crossee0bc3b2018-10-02 22:01:37 -0700702 case Device:
703 multilib = String(base.commonProperties.Target.Android.Compile_multilib)
Jiyong Park1613e552020-09-14 19:43:17 +0900704 case Host:
Colin Crossee0bc3b2018-10-02 22:01:37 -0700705 multilib = String(base.commonProperties.Target.Host.Compile_multilib)
706 }
Colin Crossa6845402020-11-16 15:08:19 -0800707
708 // If those aren't set, try the "compile_multilib" property.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700709 if multilib == "" {
710 multilib = String(base.commonProperties.Compile_multilib)
711 }
Colin Crossa6845402020-11-16 15:08:19 -0800712
713 // If that wasn't set, use the default multilib set by the factory.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700714 if multilib == "" {
715 multilib = base.commonProperties.Default_multilib
716 }
717
Christopher Ferris98f10222022-07-13 23:16:52 -0700718 // If a device is configured with multiple targets, this option
719 // force all device targets that prefer32 to be compiled only as
720 // the first target.
721 if ignorePrefer32OnDevice && os.Class == Device && (multilib == "prefer32" || multilib == "first_prefer32") {
722 multilib = "first"
723 }
724
Colin Crossee0bc3b2018-10-02 22:01:37 -0700725 if base.commonProperties.UseTargetVariants {
Dan Willemsen47450072021-10-19 20:24:49 -0700726 // Darwin has the concept of "universal binaries" which is implemented in Soong by
727 // building both x86_64 and arm64 variants, and having select module types know how to
728 // merge the outputs of their corresponding variants together into a final binary. Most
729 // module types don't need to understand this logic, as we only build a small portion
730 // of the tree for Darwin, and only module types writing macho files need to do the
731 // merging.
732 //
733 // This logic is not enabled for:
734 // "common", as it's not an arch-specific variant
735 // "32", as Darwin never has a 32-bit variant
736 // !UseTargetVariants, as the module has opted into handling the arch-specific logic on
737 // its own.
738 if os == Darwin && multilib != "common" && multilib != "32" {
739 if multilib == "common_first" {
740 multilib = "darwin_universal_common_first"
741 } else {
742 multilib = "darwin_universal"
743 }
744 }
745
Colin Crossee0bc3b2018-10-02 22:01:37 -0700746 return multilib, ""
747 } else {
748 // For app modules a single arch variant will be created per OS class which is expected to handle all the
749 // selected arches. Return the common-type as multilib and any Android.bp provided multilib as extraMultilib
750 if multilib == base.commonProperties.Default_multilib {
751 multilib = "first"
752 }
753 return base.commonProperties.Default_multilib, multilib
754 }
755}
756
Colin Crossa6845402020-11-16 15:08:19 -0800757// filterToArch takes a list of Targets and an ArchType, and returns a modified list that contains
Inseob Kim20fb5d42021-02-02 20:07:58 +0900758// only Targets that have the specified ArchTypes.
759func filterToArch(targets []Target, archs ...ArchType) []Target {
Colin Crossfb0c16e2019-11-20 17:12:35 -0800760 for i := 0; i < len(targets); i++ {
Inseob Kim20fb5d42021-02-02 20:07:58 +0900761 found := false
762 for _, arch := range archs {
763 if targets[i].Arch.ArchType == arch {
764 found = true
765 break
766 }
767 }
768 if !found {
Colin Crossfb0c16e2019-11-20 17:12:35 -0800769 targets = append(targets[:i], targets[i+1:]...)
770 i--
771 }
772 }
773 return targets
774}
775
Colin Crossc0f0eb82022-07-19 14:41:11 -0700776// filterHostCross takes a list of Targets and a hostCross value, and returns a modified list
777// that contains only Targets that have the specified HostCross.
778func filterHostCross(targets []Target, hostCross bool) []Target {
779 for i := 0; i < len(targets); i++ {
780 if targets[i].HostCross != hostCross {
781 targets = append(targets[:i], targets[i+1:]...)
782 i--
783 }
784 }
785 return targets
786}
787
Colin Crossa6845402020-11-16 15:08:19 -0800788// archPropRoot is a struct type used as the top level of the arch-specific properties. It
789// contains the "arch", "multilib", and "target" property structs. It is used to split up the
790// property structs to limit how much is allocated when a single arch-specific property group is
791// used. The types are interface{} because they will hold instances of runtime-created types.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800792type archPropRoot struct {
793 Arch, Multilib, Target interface{}
794}
795
Colin Crossa6845402020-11-16 15:08:19 -0800796// archPropTypeDesc holds the runtime-created types for the property structs to instantiate to
797// create an archPropRoot property struct.
798type archPropTypeDesc struct {
799 arch, multilib, target reflect.Type
800}
801
Colin Crosscbbd13f2020-01-17 14:08:22 -0800802// createArchPropTypeDesc takes a reflect.Type that is either a struct or a pointer to a struct, and
803// returns lists of reflect.Types that contains the arch-variant properties inside structs for each
804// arch, multilib and target property.
Colin Crossa6845402020-11-16 15:08:19 -0800805//
806// This is a relatively expensive operation, so the results are cached in the global
807// archPropTypeMap. It is constructed entirely based on compile-time data, so there is no need
808// to isolate the results between multiple tests running in parallel.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800809func createArchPropTypeDesc(props reflect.Type) []archPropTypeDesc {
Colin Crossb1d8c992020-01-21 11:43:29 -0800810 // Each property struct shard will be nested many times under the runtime generated arch struct,
811 // which can hit the limit of 64kB for the name of runtime generated structs. They are nested
812 // 97 times now, which may grow in the future, plus there is some overhead for the containing
813 // type. This number may need to be reduced if too many are added, but reducing it too far
814 // could cause problems if a single deeply nested property no longer fits in the name.
815 const maxArchTypeNameSize = 500
816
Colin Crossa6845402020-11-16 15:08:19 -0800817 // Convert the type to a new set of types that contains only the arch-specific properties
Usta Shrestha0b52d832022-02-04 21:37:39 -0500818 // (those that are tagged with `android:"arch_variant"`), and sharded into multiple types
Colin Crossa6845402020-11-16 15:08:19 -0800819 // to keep the runtime-generated names under the limit.
Colin Crossb1d8c992020-01-21 11:43:29 -0800820 propShards, _ := proptools.FilterPropertyStructSharded(props, maxArchTypeNameSize, filterArchStruct)
Colin Crossa6845402020-11-16 15:08:19 -0800821
822 // If the type has no arch-specific properties there is nothing to do.
Colin Crosscb988072019-01-24 14:58:11 -0800823 if len(propShards) == 0 {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700824 return nil
825 }
826
Colin Crosscbbd13f2020-01-17 14:08:22 -0800827 var ret []archPropTypeDesc
Colin Crossc17727d2018-10-24 12:42:09 -0700828 for _, props := range propShards {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700829
Colin Crossa6845402020-11-16 15:08:19 -0800830 // variantFields takes a list of variant property field names and returns a list the
831 // StructFields with the names and the type of the current shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700832 variantFields := func(names []string) []reflect.StructField {
833 ret := make([]reflect.StructField, len(names))
Dan Willemsenb1957a52016-06-23 23:44:54 -0700834
Colin Crossc17727d2018-10-24 12:42:09 -0700835 for i, name := range names {
836 ret[i].Name = name
837 ret[i].Type = props
Dan Willemsen866b5632017-09-22 12:28:24 -0700838 }
Colin Crossc17727d2018-10-24 12:42:09 -0700839
840 return ret
841 }
842
Colin Crossa6845402020-11-16 15:08:19 -0800843 // Create a type that contains the properties in this shard repeated for each
844 // architecture, architecture variant, and architecture feature.
Colin Crossc17727d2018-10-24 12:42:09 -0700845 archFields := make([]reflect.StructField, len(archTypeList))
846 for i, arch := range archTypeList {
Colin Crossa6845402020-11-16 15:08:19 -0800847 var variants []string
Colin Crossc17727d2018-10-24 12:42:09 -0700848
849 for _, archVariant := range archVariants[arch] {
850 archVariant := variantReplacer.Replace(archVariant)
851 variants = append(variants, proptools.FieldNameForProperty(archVariant))
852 }
Liz Kammer2c2afe22022-02-11 11:35:03 -0500853 for _, cpuVariant := range cpuVariants[arch] {
854 cpuVariant := variantReplacer.Replace(cpuVariant)
855 variants = append(variants, proptools.FieldNameForProperty(cpuVariant))
856 }
Colin Crossc17727d2018-10-24 12:42:09 -0700857 for _, feature := range archFeatures[arch] {
858 feature := variantReplacer.Replace(feature)
859 variants = append(variants, proptools.FieldNameForProperty(feature))
860 }
861
Colin Crossa6845402020-11-16 15:08:19 -0800862 // Create the StructFields for each architecture variant architecture feature
863 // (e.g. "arch.arm.cortex-a53" or "arch.arm.neon").
Colin Crossc17727d2018-10-24 12:42:09 -0700864 fields := variantFields(variants)
865
Colin Crossa6845402020-11-16 15:08:19 -0800866 // Create the StructField for the architecture itself (e.g. "arch.arm"). The special
867 // "BlueprintEmbed" name is used by Blueprint to put the properties in the
868 // parent struct.
Colin Crossc17727d2018-10-24 12:42:09 -0700869 fields = append([]reflect.StructField{{
870 Name: "BlueprintEmbed",
871 Type: props,
872 Anonymous: true,
873 }}, fields...)
874
875 archFields[i] = reflect.StructField{
876 Name: arch.Field,
877 Type: reflect.StructOf(fields),
878 }
879 }
Colin Crossa6845402020-11-16 15:08:19 -0800880
881 // Create the type of the "arch" property struct for this shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700882 archType := reflect.StructOf(archFields)
883
Colin Crossa6845402020-11-16 15:08:19 -0800884 // Create the type for the "multilib" property struct for this shard, containing the
885 // "multilib.lib32" and "multilib.lib64" property structs.
Colin Crossc17727d2018-10-24 12:42:09 -0700886 multilibType := reflect.StructOf(variantFields([]string{"Lib32", "Lib64"}))
887
Colin Crossa6845402020-11-16 15:08:19 -0800888 // Start with a list of the special targets
Colin Crossc17727d2018-10-24 12:42:09 -0700889 targets := []string{
890 "Host",
891 "Android64",
892 "Android32",
893 "Bionic",
Colin Cross528d67e2021-07-23 22:23:07 +0000894 "Glibc",
895 "Musl",
Colin Crossc17727d2018-10-24 12:42:09 -0700896 "Linux",
Colin Crossa98d36d2022-03-07 14:39:49 -0800897 "Host_linux",
Colin Crossc17727d2018-10-24 12:42:09 -0700898 "Not_windows",
899 "Arm_on_x86",
900 "Arm_on_x86_64",
Victor Khimenkoc26fcf42020-05-07 22:16:33 +0200901 "Native_bridge",
Colin Crossc17727d2018-10-24 12:42:09 -0700902 }
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000903 for _, os := range osTypeList {
Colin Crossa6845402020-11-16 15:08:19 -0800904 // Add all the OSes.
Colin Crossc17727d2018-10-24 12:42:09 -0700905 targets = append(targets, os.Field)
906
Colin Crossa6845402020-11-16 15:08:19 -0800907 // Add the OS/Arch combinations, e.g. "android_arm64".
Colin Crossc17727d2018-10-24 12:42:09 -0700908 for _, archType := range osArchTypeMap[os] {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400909 targets = append(targets, GetCompoundTargetField(os, archType))
Colin Crossc17727d2018-10-24 12:42:09 -0700910
Colin Cross1aa45b02022-02-10 10:33:10 -0800911 // Also add the special "linux_<arch>", "bionic_<arch>" , "glibc_<arch>", and
912 // "musl_<arch>" property structs.
Colin Crossc17727d2018-10-24 12:42:09 -0700913 if os.Linux() {
914 target := "Linux_" + archType.Name
915 if !InList(target, targets) {
916 targets = append(targets, target)
917 }
918 }
Colin Crossa98d36d2022-03-07 14:39:49 -0800919 if os.Linux() && os.Class == Host {
920 target := "Host_linux_" + archType.Name
921 if !InList(target, targets) {
922 targets = append(targets, target)
923 }
924 }
Colin Crossc17727d2018-10-24 12:42:09 -0700925 if os.Bionic() {
926 target := "Bionic_" + archType.Name
927 if !InList(target, targets) {
928 targets = append(targets, target)
929 }
Dan Willemsen866b5632017-09-22 12:28:24 -0700930 }
Colin Cross1aa45b02022-02-10 10:33:10 -0800931 if os == Linux {
932 target := "Glibc_" + archType.Name
933 if !InList(target, targets) {
934 targets = append(targets, target)
935 }
936 }
937 if os == LinuxMusl {
938 target := "Musl_" + archType.Name
939 if !InList(target, targets) {
940 targets = append(targets, target)
941 }
942 }
Dan Willemsen866b5632017-09-22 12:28:24 -0700943 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700944 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700945
Colin Crossa6845402020-11-16 15:08:19 -0800946 // Create the type for the "target" property struct for this shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700947 targetType := reflect.StructOf(variantFields(targets))
Colin Crosscbbd13f2020-01-17 14:08:22 -0800948
Colin Crossa6845402020-11-16 15:08:19 -0800949 // Return a descriptor of the 3 runtime-created types.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800950 ret = append(ret, archPropTypeDesc{
951 arch: reflect.PtrTo(archType),
952 multilib: reflect.PtrTo(multilibType),
953 target: reflect.PtrTo(targetType),
954 })
Colin Crossc17727d2018-10-24 12:42:09 -0700955 }
956 return ret
Dan Willemsenb1957a52016-06-23 23:44:54 -0700957}
958
Colin Crossa6845402020-11-16 15:08:19 -0800959// variantReplacer converts architecture variant or architecture feature names into names that
960// are valid for an Android.bp file.
961var variantReplacer = strings.NewReplacer("-", "_", ".", "_")
962
963// filterArchStruct returns true if the given field is an architecture specific property.
Colin Cross74449102019-09-25 11:26:40 -0700964func filterArchStruct(field reflect.StructField, prefix string) (bool, reflect.StructField) {
965 if proptools.HasTag(field, "android", "arch_variant") {
966 // The arch_variant field isn't necessary past this point
967 // Instead of wasting space, just remove it. Go also has a
968 // 16-bit limit on structure name length. The name is constructed
969 // based on the Go source representation of the structure, so
970 // the tag names count towards that length.
Colin Crossb4fecbf2020-01-21 11:38:47 -0800971
972 androidTag := field.Tag.Get("android")
973 values := strings.Split(androidTag, ",")
974
975 if string(field.Tag) != `android:"`+strings.Join(values, ",")+`"` {
976 panic(fmt.Errorf("unexpected tag format %q", field.Tag))
Colin Cross74449102019-09-25 11:26:40 -0700977 }
Colin Crossb4fecbf2020-01-21 11:38:47 -0800978 // these tags don't need to be present in the runtime generated struct type.
Liz Kammerff966b12022-07-29 10:49:16 -0400979 values = RemoveListFromList(values, []string{"arch_variant", "variant_prepend", "path"})
980 if len(values) > 0 {
Colin Crossb4fecbf2020-01-21 11:38:47 -0800981 panic(fmt.Errorf("unknown tags %q in field %q", values, prefix+field.Name))
982 }
983
Liz Kammerff966b12022-07-29 10:49:16 -0400984 field.Tag = ``
Colin Cross74449102019-09-25 11:26:40 -0700985 return true, field
986 }
987 return false, field
988}
989
Colin Crossa6845402020-11-16 15:08:19 -0800990// archPropTypeMap contains a cache of the results of createArchPropTypeDesc for each type. It is
991// shared across all Contexts, but is constructed based only on compile-time information so there
992// is no risk of contaminating one Context with data from another.
Dan Willemsenb1957a52016-06-23 23:44:54 -0700993var archPropTypeMap OncePer
994
Colin Crossa6845402020-11-16 15:08:19 -0800995// initArchModule adds the architecture-specific property structs to a Module.
996func initArchModule(m Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800997
998 base := m.base()
999
Ustaeabf0f32021-12-06 15:17:23 -05001000 if len(base.archProperties) != 0 {
1001 panic(fmt.Errorf("module %s already has archProperties", m.Name()))
1002 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001003
Ustaeabf0f32021-12-06 15:17:23 -05001004 getStructType := func(properties interface{}) reflect.Type {
Colin Cross3f40fa42015-01-30 17:27:36 -08001005 propertiesValue := reflect.ValueOf(properties)
Colin Cross62496a02016-08-08 15:49:17 -07001006 t := propertiesValue.Type()
Colin Cross3f40fa42015-01-30 17:27:36 -08001007 if propertiesValue.Kind() != reflect.Ptr {
Colin Crossca860ac2016-01-04 14:34:37 -08001008 panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
1009 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -08001010 }
1011
1012 propertiesValue = propertiesValue.Elem()
1013 if propertiesValue.Kind() != reflect.Struct {
Ustaeabf0f32021-12-06 15:17:23 -05001014 panic(fmt.Errorf("properties must be a pointer to a struct, got a pointer to %T",
Colin Crossca860ac2016-01-04 14:34:37 -08001015 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -08001016 }
Ustaeabf0f32021-12-06 15:17:23 -05001017 return t
1018 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001019
Usta851a3272022-01-05 23:42:33 -05001020 for _, properties := range m.GetProperties() {
Ustaeabf0f32021-12-06 15:17:23 -05001021 t := getStructType(properties)
Colin Crossa6845402020-11-16 15:08:19 -08001022 // Get or create the arch-specific property struct types for this property struct type.
Colin Cross571cccf2019-02-04 11:22:08 -08001023 archPropTypes := archPropTypeMap.Once(NewCustomOnceKey(t), func() interface{} {
Colin Crosscbbd13f2020-01-17 14:08:22 -08001024 return createArchPropTypeDesc(t)
1025 }).([]archPropTypeDesc)
Colin Cross3f40fa42015-01-30 17:27:36 -08001026
Colin Crossa6845402020-11-16 15:08:19 -08001027 // Instantiate one of each arch-specific property struct type and add it to the
1028 // properties for the Module.
Colin Crossc17727d2018-10-24 12:42:09 -07001029 var archProperties []interface{}
1030 for _, t := range archPropTypes {
Colin Crosscbbd13f2020-01-17 14:08:22 -08001031 archProperties = append(archProperties, &archPropRoot{
1032 Arch: reflect.Zero(t.arch).Interface(),
1033 Multilib: reflect.Zero(t.multilib).Interface(),
1034 Target: reflect.Zero(t.target).Interface(),
1035 })
Dan Willemsenb1957a52016-06-23 23:44:54 -07001036 }
Colin Crossc17727d2018-10-24 12:42:09 -07001037 base.archProperties = append(base.archProperties, archProperties)
1038 m.AddProperties(archProperties...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001039 }
1040
Colin Cross3f40fa42015-01-30 17:27:36 -08001041}
1042
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001043func maybeBlueprintEmbed(src reflect.Value) reflect.Value {
Colin Crossa6845402020-11-16 15:08:19 -08001044 // If the value of the field is a struct (as opposed to a pointer to a struct) then step
1045 // into the BlueprintEmbed field.
Dan Willemsenb1957a52016-06-23 23:44:54 -07001046 if src.Kind() == reflect.Struct {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001047 return src.FieldByName("BlueprintEmbed")
1048 } else {
1049 return src
Colin Cross06a931b2015-10-28 17:23:31 -07001050 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001051}
1052
1053// Merges the property struct in srcValue into dst.
Liz Kammerb6dbc872021-05-14 15:14:40 -04001054func mergePropertyStruct(ctx ArchVariantContext, dst interface{}, srcValue reflect.Value) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001055 src := maybeBlueprintEmbed(srcValue).Interface()
Colin Cross06a931b2015-10-28 17:23:31 -07001056
Colin Crossa6845402020-11-16 15:08:19 -08001057 // order checks the `android:"variant_prepend"` tag to handle properties where the
1058 // arch-specific value needs to come before the generic value, for example for lists of
1059 // include directories.
Colin Cross6ee75b62016-05-05 15:57:15 -07001060 order := func(property string,
1061 dstField, srcField reflect.StructField,
1062 dstValue, srcValue interface{}) (proptools.Order, error) {
1063 if proptools.HasTag(dstField, "android", "variant_prepend") {
1064 return proptools.Prepend, nil
1065 } else {
1066 return proptools.Append, nil
1067 }
1068 }
1069
Colin Crossa6845402020-11-16 15:08:19 -08001070 // Squash the located property struct into the destination property struct.
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001071 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src, nil, order)
Colin Cross06a931b2015-10-28 17:23:31 -07001072 if err != nil {
1073 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
1074 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
1075 } else {
1076 panic(err)
1077 }
1078 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001079}
Colin Cross85a88972015-11-23 13:29:51 -08001080
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001081// Returns the immediate child of the input property struct that corresponds to
1082// the sub-property "field".
Liz Kammerb6dbc872021-05-14 15:14:40 -04001083func getChildPropertyStruct(ctx ArchVariantContext,
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001084 src reflect.Value, field, userFriendlyField string) (reflect.Value, bool) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001085
1086 // Step into non-nil pointers to structs in the src value.
1087 if src.Kind() == reflect.Ptr {
1088 if src.IsNil() {
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001089 return reflect.Value{}, false
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001090 }
1091 src = src.Elem()
1092 }
1093
1094 // Find the requested field in the src struct.
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001095 child := src.FieldByName(proptools.FieldNameForProperty(field))
1096 if !child.IsValid() {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001097 ctx.ModuleErrorf("field %q does not exist", userFriendlyField)
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001098 return reflect.Value{}, false
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001099 }
1100
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001101 if child.IsZero() {
1102 return reflect.Value{}, false
1103 }
1104
1105 return child, true
Colin Cross06a931b2015-10-28 17:23:31 -07001106}
1107
Colin Crossa6845402020-11-16 15:08:19 -08001108// Squash the appropriate OS-specific property structs into the matching top level property structs
1109// based on the CompileOS value that was annotated on the variant.
Colin Crossa195f912019-10-16 11:07:20 -07001110func (m *ModuleBase) setOSProperties(ctx BottomUpMutatorContext) {
1111 os := m.commonProperties.CompileOS
1112
Ustadca02192021-12-20 12:56:46 -05001113 for i := range m.archProperties {
Usta851a3272022-01-05 23:42:33 -05001114 genProps := m.GetProperties()[i]
Colin Crossa195f912019-10-16 11:07:20 -07001115 if m.archProperties[i] == nil {
1116 continue
1117 }
1118 for _, archProperties := range m.archProperties[i] {
1119 archPropValues := reflect.ValueOf(archProperties).Elem()
1120
Colin Crosscbbd13f2020-01-17 14:08:22 -08001121 targetProp := archPropValues.FieldByName("Target").Elem()
Colin Crossa195f912019-10-16 11:07:20 -07001122
1123 // Handle host-specific properties in the form:
1124 // target: {
1125 // host: {
1126 // key: value,
1127 // },
1128 // },
Jiyong Park1613e552020-09-14 19:43:17 +09001129 if os.Class == Host {
Colin Crossa195f912019-10-16 11:07:20 -07001130 field := "Host"
1131 prefix := "target.host"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001132 if hostProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1133 mergePropertyStruct(ctx, genProps, hostProperties)
1134 }
Colin Crossa195f912019-10-16 11:07:20 -07001135 }
1136
1137 // Handle target OS generalities of the form:
1138 // target: {
1139 // bionic: {
1140 // key: value,
1141 // },
1142 // }
1143 if os.Linux() {
1144 field := "Linux"
1145 prefix := "target.linux"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001146 if linuxProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1147 mergePropertyStruct(ctx, genProps, linuxProperties)
1148 }
Colin Crossa195f912019-10-16 11:07:20 -07001149 }
1150
Colin Crossa98d36d2022-03-07 14:39:49 -08001151 if os.Linux() && os.Class == Host {
1152 field := "Host_linux"
1153 prefix := "target.host_linux"
1154 if linuxProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1155 mergePropertyStruct(ctx, genProps, linuxProperties)
1156 }
1157 }
1158
Colin Crossa195f912019-10-16 11:07:20 -07001159 if os.Bionic() {
1160 field := "Bionic"
1161 prefix := "target.bionic"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001162 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1163 mergePropertyStruct(ctx, genProps, bionicProperties)
1164 }
Colin Crossa195f912019-10-16 11:07:20 -07001165 }
1166
Colin Cross528d67e2021-07-23 22:23:07 +00001167 if os == Linux {
1168 field := "Glibc"
1169 prefix := "target.glibc"
1170 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1171 mergePropertyStruct(ctx, genProps, bionicProperties)
1172 }
1173 }
1174
1175 if os == LinuxMusl {
1176 field := "Musl"
1177 prefix := "target.musl"
1178 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1179 mergePropertyStruct(ctx, genProps, bionicProperties)
1180 }
Colin Cross528d67e2021-07-23 22:23:07 +00001181 }
1182
Colin Crossa195f912019-10-16 11:07:20 -07001183 // Handle target OS properties in the form:
1184 // target: {
1185 // linux_glibc: {
1186 // key: value,
1187 // },
1188 // not_windows: {
1189 // key: value,
1190 // },
1191 // android {
1192 // key: value,
1193 // },
1194 // },
1195 field := os.Field
1196 prefix := "target." + os.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001197 if osProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1198 mergePropertyStruct(ctx, genProps, osProperties)
1199 }
Colin Crossa195f912019-10-16 11:07:20 -07001200
Jiyong Park1613e552020-09-14 19:43:17 +09001201 if os.Class == Host && os != Windows {
Colin Crossa195f912019-10-16 11:07:20 -07001202 field := "Not_windows"
1203 prefix := "target.not_windows"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001204 if notWindowsProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1205 mergePropertyStruct(ctx, genProps, notWindowsProperties)
1206 }
Colin Crossa195f912019-10-16 11:07:20 -07001207 }
1208
1209 // Handle 64-bit device properties in the form:
1210 // target {
1211 // android64 {
1212 // key: value,
1213 // },
1214 // android32 {
1215 // key: value,
1216 // },
1217 // },
1218 // WARNING: this is probably not what you want to use in your blueprints file, it selects
1219 // options for all targets on a device that supports 64-bit binaries, not just the targets
1220 // that are being compiled for 64-bit. Its expected use case is binaries like linker and
1221 // debuggerd that need to know when they are a 32-bit process running on a 64-bit device
1222 if os.Class == Device {
1223 if ctx.Config().Android64() {
1224 field := "Android64"
1225 prefix := "target.android64"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001226 if android64Properties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1227 mergePropertyStruct(ctx, genProps, android64Properties)
1228 }
Colin Crossa195f912019-10-16 11:07:20 -07001229 } else {
1230 field := "Android32"
1231 prefix := "target.android32"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001232 if android32Properties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1233 mergePropertyStruct(ctx, genProps, android32Properties)
1234 }
Colin Crossa195f912019-10-16 11:07:20 -07001235 }
1236 }
1237 }
1238 }
1239}
1240
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001241// Returns the struct containing the properties specific to the given
1242// architecture type. These look like this in Blueprint files:
Colin Crossd079e0b2022-08-16 10:27:33 -07001243//
1244// arch: {
1245// arm64: {
1246// key: value,
1247// },
1248// },
1249//
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001250// This struct will also contain sub-structs containing to the architecture/CPU
1251// variants and features that themselves contain properties specific to those.
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001252func getArchTypeStruct(ctx ArchVariantContext, archProperties interface{}, archType ArchType) (reflect.Value, bool) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001253 archPropValues := reflect.ValueOf(archProperties).Elem()
1254 archProp := archPropValues.FieldByName("Arch").Elem()
1255 prefix := "arch." + archType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001256 return getChildPropertyStruct(ctx, archProp, archType.Name, prefix)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001257}
1258
1259// Returns the struct containing the properties specific to a given multilib
1260// value. These look like this in the Blueprint file:
Colin Crossd079e0b2022-08-16 10:27:33 -07001261//
1262// multilib: {
1263// lib32: {
1264// key: value,
1265// },
1266// },
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001267func getMultilibStruct(ctx ArchVariantContext, archProperties interface{}, archType ArchType) (reflect.Value, bool) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001268 archPropValues := reflect.ValueOf(archProperties).Elem()
1269 multilibProp := archPropValues.FieldByName("Multilib").Elem()
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001270 return getChildPropertyStruct(ctx, multilibProp, archType.Multilib, "multilib."+archType.Multilib)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001271}
1272
Liz Kammer9abd62d2021-05-21 08:37:59 -04001273func GetCompoundTargetField(os OsType, arch ArchType) string {
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04001274 return os.Field + "_" + arch.Name
1275}
1276
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001277// Returns the structs corresponding to the properties specific to the given
1278// architecture and OS in archProperties.
1279func getArchProperties(ctx BaseMutatorContext, archProperties interface{}, arch Arch, os OsType, nativeBridgeEnabled bool) []reflect.Value {
1280 result := make([]reflect.Value, 0)
1281 archPropValues := reflect.ValueOf(archProperties).Elem()
1282
1283 targetProp := archPropValues.FieldByName("Target").Elem()
1284
1285 archType := arch.ArchType
1286
1287 if arch.ArchType != Common {
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001288 archStruct, ok := getArchTypeStruct(ctx, archProperties, arch.ArchType)
1289 if ok {
1290 result = append(result, archStruct)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001291
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001292 // Handle arch-variant-specific properties in the form:
1293 // arch: {
1294 // arm: {
1295 // variant: {
1296 // key: value,
1297 // },
1298 // },
1299 // },
1300 v := variantReplacer.Replace(arch.ArchVariant)
1301 if v != "" {
1302 prefix := "arch." + archType.Name + "." + v
1303 if variantProperties, ok := getChildPropertyStruct(ctx, archStruct, v, prefix); ok {
1304 result = append(result, variantProperties)
1305 }
1306 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001307
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001308 // Handle cpu-variant-specific properties in the form:
1309 // arch: {
1310 // arm: {
1311 // variant: {
1312 // key: value,
1313 // },
1314 // },
1315 // },
1316 if arch.CpuVariant != arch.ArchVariant {
1317 c := variantReplacer.Replace(arch.CpuVariant)
1318 if c != "" {
1319 prefix := "arch." + archType.Name + "." + c
1320 if cpuVariantProperties, ok := getChildPropertyStruct(ctx, archStruct, c, prefix); ok {
1321 result = append(result, cpuVariantProperties)
1322 }
1323 }
1324 }
1325
1326 // Handle arch-feature-specific properties in the form:
1327 // arch: {
1328 // arm: {
1329 // feature: {
1330 // key: value,
1331 // },
1332 // },
1333 // },
1334 for _, feature := range arch.ArchFeatures {
1335 prefix := "arch." + archType.Name + "." + feature
1336 if featureProperties, ok := getChildPropertyStruct(ctx, archStruct, feature, prefix); ok {
1337 result = append(result, featureProperties)
1338 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001339 }
1340 }
1341
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001342 if multilibProperties, ok := getMultilibStruct(ctx, archProperties, archType); ok {
1343 result = append(result, multilibProperties)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001344 }
1345
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001346 // Handle combined OS-feature and arch specific properties in the form:
1347 // target: {
1348 // bionic_x86: {
1349 // key: value,
1350 // },
1351 // }
1352 if os.Linux() {
1353 field := "Linux_" + arch.ArchType.Name
1354 userFriendlyField := "target.linux_" + arch.ArchType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001355 if linuxProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1356 result = append(result, linuxProperties)
1357 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001358 }
1359
1360 if os.Bionic() {
1361 field := "Bionic_" + archType.Name
1362 userFriendlyField := "target.bionic_" + archType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001363 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1364 result = append(result, bionicProperties)
1365 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001366 }
1367
1368 // Handle combined OS and arch specific properties in the form:
1369 // target: {
1370 // linux_glibc_x86: {
1371 // key: value,
1372 // },
1373 // linux_glibc_arm: {
1374 // key: value,
1375 // },
1376 // android_arm {
1377 // key: value,
1378 // },
1379 // android_x86 {
1380 // key: value,
1381 // },
1382 // },
Liz Kammer9abd62d2021-05-21 08:37:59 -04001383 field := GetCompoundTargetField(os, archType)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001384 userFriendlyField := "target." + os.Name + "_" + archType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001385 if osArchProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1386 result = append(result, osArchProperties)
1387 }
Colin Cross528d67e2021-07-23 22:23:07 +00001388
Colin Cross1aa45b02022-02-10 10:33:10 -08001389 if os == Linux {
1390 field := "Glibc_" + archType.Name
1391 userFriendlyField := "target.glibc_" + "_" + archType.Name
1392 if osArchProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1393 result = append(result, osArchProperties)
1394 }
1395 }
1396
Colin Cross528d67e2021-07-23 22:23:07 +00001397 if os == LinuxMusl {
Colin Cross1aa45b02022-02-10 10:33:10 -08001398 field := "Musl_" + archType.Name
1399 userFriendlyField := "target.musl_" + "_" + archType.Name
1400 if osArchProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1401 result = append(result, osArchProperties)
1402 }
Colin Cross528d67e2021-07-23 22:23:07 +00001403 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001404 }
1405
1406 // Handle arm on x86 properties in the form:
1407 // target {
1408 // arm_on_x86 {
1409 // key: value,
1410 // },
1411 // arm_on_x86_64 {
1412 // key: value,
1413 // },
1414 // },
1415 if os.Class == Device {
1416 if arch.ArchType == X86 && (hasArmAbi(arch) ||
1417 hasArmAndroidArch(ctx.Config().Targets[Android])) {
1418 field := "Arm_on_x86"
1419 userFriendlyField := "target.arm_on_x86"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001420 if armOnX86Properties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1421 result = append(result, armOnX86Properties)
1422 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001423 }
1424 if arch.ArchType == X86_64 && (hasArmAbi(arch) ||
1425 hasArmAndroidArch(ctx.Config().Targets[Android])) {
1426 field := "Arm_on_x86_64"
1427 userFriendlyField := "target.arm_on_x86_64"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001428 if armOnX8664Properties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1429 result = append(result, armOnX8664Properties)
1430 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001431 }
1432 if os == Android && nativeBridgeEnabled {
1433 userFriendlyField := "Native_bridge"
1434 prefix := "target.native_bridge"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001435 if nativeBridgeProperties, ok := getChildPropertyStruct(ctx, targetProp, userFriendlyField, prefix); ok {
1436 result = append(result, nativeBridgeProperties)
1437 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001438 }
1439 }
1440
1441 return result
1442}
1443
Colin Crossa6845402020-11-16 15:08:19 -08001444// Squash the appropriate arch-specific property structs into the matching top level property
1445// structs based on the CompileTarget value that was annotated on the variant.
Colin Cross4157e882019-06-06 16:57:04 -07001446func (m *ModuleBase) setArchProperties(ctx BottomUpMutatorContext) {
1447 arch := m.Arch()
1448 os := m.Os()
Colin Crossd3ba0392015-05-07 14:11:29 -07001449
Ustadca02192021-12-20 12:56:46 -05001450 for i := range m.archProperties {
Usta851a3272022-01-05 23:42:33 -05001451 genProps := m.GetProperties()[i]
Colin Cross4157e882019-06-06 16:57:04 -07001452 if m.archProperties[i] == nil {
Dan Willemsenb1957a52016-06-23 23:44:54 -07001453 continue
1454 }
Dan Willemsenb1957a52016-06-23 23:44:54 -07001455
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001456 propStructs := make([]reflect.Value, 0)
1457 for _, archProperty := range m.archProperties[i] {
1458 propStructShard := getArchProperties(ctx, archProperty, arch, os, m.Target().NativeBridge == NativeBridgeEnabled)
1459 propStructs = append(propStructs, propStructShard...)
1460 }
Dan Willemsenb1957a52016-06-23 23:44:54 -07001461
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001462 for _, propStruct := range propStructs {
1463 mergePropertyStruct(ctx, genProps, propStruct)
Colin Crossbb2e2b72016-12-08 17:23:53 -08001464 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001465 }
1466}
1467
Colin Cross0c66bc62021-07-20 09:47:41 -07001468// determineBuildOS stores the OS and architecture used for host targets used during the build into
Colin Cross528d67e2021-07-23 22:23:07 +00001469// config based on the runtime OS and architecture determined by Go and the product configuration.
Colin Cross0c66bc62021-07-20 09:47:41 -07001470func determineBuildOS(config *config) {
1471 config.BuildOS = func() OsType {
1472 switch runtime.GOOS {
1473 case "linux":
Colin Cross528d67e2021-07-23 22:23:07 +00001474 if Bool(config.productVariables.HostMusl) {
1475 return LinuxMusl
1476 }
Colin Cross0c66bc62021-07-20 09:47:41 -07001477 return Linux
1478 case "darwin":
1479 return Darwin
1480 default:
1481 panic(fmt.Sprintf("unsupported OS: %s", runtime.GOOS))
1482 }
1483 }()
1484
1485 config.BuildArch = func() ArchType {
1486 switch runtime.GOARCH {
1487 case "amd64":
1488 return X86_64
1489 default:
1490 panic(fmt.Sprintf("unsupported Arch: %s", runtime.GOARCH))
1491 }
1492 }()
1493
1494}
1495
Colin Crossa6845402020-11-16 15:08:19 -08001496// Convert the arch product variables into a list of targets for each OsType.
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001497func decodeTargetProductVariables(config *config) (map[OsType][]Target, error) {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001498 variables := config.productVariables
Dan Willemsen490fd492015-11-24 17:53:15 -08001499
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001500 targets := make(map[OsType][]Target)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001501 var targetErr error
1502
Liz Kammerb7f33662022-02-28 14:16:16 -05001503 type targetConfig struct {
1504 os OsType
1505 archName string
1506 archVariant *string
1507 cpuVariant *string
1508 abi []string
1509 nativeBridgeEnabled NativeBridgeSupport
1510 nativeBridgeHostArchName *string
1511 nativeBridgeRelativePath *string
1512 }
1513
1514 addTarget := func(target targetConfig) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001515 if targetErr != nil {
1516 return
Dan Willemsen490fd492015-11-24 17:53:15 -08001517 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001518
Liz Kammerb7f33662022-02-28 14:16:16 -05001519 arch, err := decodeArch(target.os, target.archName, target.archVariant, target.cpuVariant, target.abi)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001520 if err != nil {
1521 targetErr = err
1522 return
1523 }
Liz Kammerb7f33662022-02-28 14:16:16 -05001524 nativeBridgeRelativePathStr := String(target.nativeBridgeRelativePath)
1525 nativeBridgeHostArchNameStr := String(target.nativeBridgeHostArchName)
dimitry8d6dde82019-07-11 10:23:53 +02001526
1527 // Use guest arch as relative install path by default
Liz Kammerb7f33662022-02-28 14:16:16 -05001528 if target.nativeBridgeEnabled && nativeBridgeRelativePathStr == "" {
dimitry8d6dde82019-07-11 10:23:53 +02001529 nativeBridgeRelativePathStr = arch.ArchType.String()
1530 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001531
Jiyong Park1613e552020-09-14 19:43:17 +09001532 // A target is considered as HostCross if it's a host target which can't run natively on
1533 // the currently configured build machine (either because the OS is different or because of
1534 // the unsupported arch)
1535 hostCross := false
Liz Kammerb7f33662022-02-28 14:16:16 -05001536 if target.os.Class == Host {
Jiyong Park1613e552020-09-14 19:43:17 +09001537 var osSupported bool
Liz Kammerb7f33662022-02-28 14:16:16 -05001538 if target.os == config.BuildOS {
Jiyong Park1613e552020-09-14 19:43:17 +09001539 osSupported = true
Liz Kammerb7f33662022-02-28 14:16:16 -05001540 } else if config.BuildOS.Linux() && target.os.Linux() {
Jiyong Park1613e552020-09-14 19:43:17 +09001541 // LinuxBionic and Linux are compatible
1542 osSupported = true
1543 } else {
1544 osSupported = false
1545 }
1546
1547 var archSupported bool
1548 if arch.ArchType == Common {
1549 archSupported = true
1550 } else if arch.ArchType.Name == *variables.HostArch {
1551 archSupported = true
1552 } else if variables.HostSecondaryArch != nil && arch.ArchType.Name == *variables.HostSecondaryArch {
1553 archSupported = true
1554 } else {
1555 archSupported = false
1556 }
1557 if !osSupported || !archSupported {
1558 hostCross = true
1559 }
1560 }
1561
Liz Kammerb7f33662022-02-28 14:16:16 -05001562 targets[target.os] = append(targets[target.os],
Colin Crossa1ad8d12016-06-01 17:09:44 -07001563 Target{
Liz Kammerb7f33662022-02-28 14:16:16 -05001564 Os: target.os,
dimitry8d6dde82019-07-11 10:23:53 +02001565 Arch: arch,
Liz Kammerb7f33662022-02-28 14:16:16 -05001566 NativeBridge: target.nativeBridgeEnabled,
dimitry8d6dde82019-07-11 10:23:53 +02001567 NativeBridgeHostArchName: nativeBridgeHostArchNameStr,
1568 NativeBridgeRelativePath: nativeBridgeRelativePathStr,
Jiyong Park1613e552020-09-14 19:43:17 +09001569 HostCross: hostCross,
Colin Crossa1ad8d12016-06-01 17:09:44 -07001570 })
Dan Willemsen490fd492015-11-24 17:53:15 -08001571 }
1572
Colin Cross4225f652015-09-17 14:33:42 -07001573 if variables.HostArch == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001574 return nil, fmt.Errorf("No host primary architecture set")
Colin Cross4225f652015-09-17 14:33:42 -07001575 }
1576
Colin Crossa6845402020-11-16 15:08:19 -08001577 // The primary host target, which must always exist.
Liz Kammerb7f33662022-02-28 14:16:16 -05001578 addTarget(targetConfig{os: config.BuildOS, archName: *variables.HostArch, nativeBridgeEnabled: NativeBridgeDisabled})
Colin Cross4225f652015-09-17 14:33:42 -07001579
Colin Crossa6845402020-11-16 15:08:19 -08001580 // An optional secondary host target.
Colin Crosseeabb892015-11-20 13:07:51 -08001581 if variables.HostSecondaryArch != nil && *variables.HostSecondaryArch != "" {
Liz Kammerb7f33662022-02-28 14:16:16 -05001582 addTarget(targetConfig{os: config.BuildOS, archName: *variables.HostSecondaryArch, nativeBridgeEnabled: NativeBridgeDisabled})
Dan Willemsen490fd492015-11-24 17:53:15 -08001583 }
1584
Colin Crossa6845402020-11-16 15:08:19 -08001585 // Optional cross-compiled host targets, generally Windows.
Colin Crossff3ae9d2018-04-10 16:15:18 -07001586 if String(variables.CrossHost) != "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001587 crossHostOs := osByName(*variables.CrossHost)
1588 if crossHostOs == NoOsType {
1589 return nil, fmt.Errorf("Unknown cross host OS %q", *variables.CrossHost)
1590 }
1591
Colin Crossff3ae9d2018-04-10 16:15:18 -07001592 if String(variables.CrossHostArch) == "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001593 return nil, fmt.Errorf("No cross-host primary architecture set")
Dan Willemsen490fd492015-11-24 17:53:15 -08001594 }
1595
Colin Crossa6845402020-11-16 15:08:19 -08001596 // The primary cross-compiled host target.
Liz Kammerb7f33662022-02-28 14:16:16 -05001597 addTarget(targetConfig{os: crossHostOs, archName: *variables.CrossHostArch, nativeBridgeEnabled: NativeBridgeDisabled})
Dan Willemsen490fd492015-11-24 17:53:15 -08001598
Colin Crossa6845402020-11-16 15:08:19 -08001599 // An optional secondary cross-compiled host target.
Dan Willemsen490fd492015-11-24 17:53:15 -08001600 if variables.CrossHostSecondaryArch != nil && *variables.CrossHostSecondaryArch != "" {
Liz Kammerb7f33662022-02-28 14:16:16 -05001601 addTarget(targetConfig{os: crossHostOs, archName: *variables.CrossHostSecondaryArch, nativeBridgeEnabled: NativeBridgeDisabled})
Dan Willemsen490fd492015-11-24 17:53:15 -08001602 }
1603 }
1604
Colin Crossa6845402020-11-16 15:08:19 -08001605 // Optional device targets
Dan Willemsen3f32f032016-07-11 14:36:48 -07001606 if variables.DeviceArch != nil && *variables.DeviceArch != "" {
Colin Crossa6845402020-11-16 15:08:19 -08001607 // The primary device target.
Liz Kammerb7f33662022-02-28 14:16:16 -05001608 addTarget(targetConfig{
1609 os: Android,
1610 archName: *variables.DeviceArch,
1611 archVariant: variables.DeviceArchVariant,
1612 cpuVariant: variables.DeviceCpuVariant,
1613 abi: variables.DeviceAbi,
1614 nativeBridgeEnabled: NativeBridgeDisabled,
1615 })
Colin Cross4225f652015-09-17 14:33:42 -07001616
Colin Crossa6845402020-11-16 15:08:19 -08001617 // An optional secondary device target.
Dan Willemsen3f32f032016-07-11 14:36:48 -07001618 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" {
Liz Kammerb7f33662022-02-28 14:16:16 -05001619 addTarget(targetConfig{
1620 os: Android,
1621 archName: *variables.DeviceSecondaryArch,
1622 archVariant: variables.DeviceSecondaryArchVariant,
1623 cpuVariant: variables.DeviceSecondaryCpuVariant,
1624 abi: variables.DeviceSecondaryAbi,
1625 nativeBridgeEnabled: NativeBridgeDisabled,
1626 })
Colin Cross4225f652015-09-17 14:33:42 -07001627 }
dimitry1f33e402019-03-26 12:39:31 +01001628
Colin Crossa6845402020-11-16 15:08:19 -08001629 // An optional NativeBridge device target.
dimitry1f33e402019-03-26 12:39:31 +01001630 if variables.NativeBridgeArch != nil && *variables.NativeBridgeArch != "" {
Liz Kammerb7f33662022-02-28 14:16:16 -05001631 addTarget(targetConfig{
1632 os: Android,
1633 archName: *variables.NativeBridgeArch,
1634 archVariant: variables.NativeBridgeArchVariant,
1635 cpuVariant: variables.NativeBridgeCpuVariant,
1636 abi: variables.NativeBridgeAbi,
1637 nativeBridgeEnabled: NativeBridgeEnabled,
1638 nativeBridgeHostArchName: variables.DeviceArch,
1639 nativeBridgeRelativePath: variables.NativeBridgeRelativePath,
1640 })
dimitry1f33e402019-03-26 12:39:31 +01001641 }
1642
Colin Crossa6845402020-11-16 15:08:19 -08001643 // An optional secondary NativeBridge device target.
dimitry1f33e402019-03-26 12:39:31 +01001644 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" &&
1645 variables.NativeBridgeSecondaryArch != nil && *variables.NativeBridgeSecondaryArch != "" {
Liz Kammerb7f33662022-02-28 14:16:16 -05001646 addTarget(targetConfig{
1647 os: Android,
1648 archName: *variables.NativeBridgeSecondaryArch,
1649 archVariant: variables.NativeBridgeSecondaryArchVariant,
1650 cpuVariant: variables.NativeBridgeSecondaryCpuVariant,
1651 abi: variables.NativeBridgeSecondaryAbi,
1652 nativeBridgeEnabled: NativeBridgeEnabled,
1653 nativeBridgeHostArchName: variables.DeviceSecondaryArch,
1654 nativeBridgeRelativePath: variables.NativeBridgeSecondaryRelativePath,
1655 })
dimitry1f33e402019-03-26 12:39:31 +01001656 }
Colin Cross4225f652015-09-17 14:33:42 -07001657 }
1658
Colin Crossa1ad8d12016-06-01 17:09:44 -07001659 if targetErr != nil {
1660 return nil, targetErr
1661 }
1662
1663 return targets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001664}
1665
Colin Crossbb2e2b72016-12-08 17:23:53 -08001666// hasArmAbi returns true if arch has at least one arm ABI
1667func hasArmAbi(arch Arch) bool {
Jaewoong Jung3aff5782020-02-11 07:54:35 -08001668 return PrefixInList(arch.Abi, "arm")
Colin Crossbb2e2b72016-12-08 17:23:53 -08001669}
1670
Lev Rumyantsev34581212021-10-13 09:47:59 -07001671// hasArmAndroidArch returns true if targets has at least
1672// one arm Android arch (possibly native bridged)
Colin Cross4247f0d2017-04-13 16:56:14 -07001673func hasArmAndroidArch(targets []Target) bool {
1674 for _, target := range targets {
Lev Rumyantsev34581212021-10-13 09:47:59 -07001675 if target.Os == Android &&
1676 (target.Arch.ArchType == Arm || target.Arch.ArchType == Arm64) {
Victor Khimenko5eb8ec12018-03-21 20:30:54 +01001677 return true
1678 }
1679 }
1680 return false
1681}
1682
Colin Crossa6845402020-11-16 15:08:19 -08001683// archConfig describes a built-in configuration.
Dan Albert4098deb2016-10-19 14:04:41 -07001684type archConfig struct {
Liz Kammer992918d2022-11-11 10:37:54 -05001685 Arch string `json:"arch"`
1686 ArchVariant string `json:"arch_variant"`
1687 CpuVariant string `json:"cpu_variant"`
1688 Abi []string `json:"abis"`
Dan Albert4098deb2016-10-19 14:04:41 -07001689}
1690
Elliott Hughesc55b5862022-10-27 23:46:22 +00001691// getNdkAbisConfig returns the list of archConfigs that are used for building
1692// the API stubs and static libraries that are included in the NDK.
Dan Albert4098deb2016-10-19 14:04:41 -07001693func getNdkAbisConfig() []archConfig {
1694 return []archConfig{
Tamas Petzbca786d2021-01-20 18:56:33 +01001695 {"arm64", "armv8-a-branchprot", "", []string{"arm64-v8a"}},
Elliott Hughesc55b5862022-10-27 23:46:22 +00001696 {"arm", "armv7-a-neon", "", []string{"armeabi-v7a"}},
Elliott Hughesf7d31092023-03-14 23:11:57 +00001697 {"riscv64", "", "", []string{"riscv64"}},
Dan Albert4098deb2016-10-19 14:04:41 -07001698 {"x86_64", "", "", []string{"x86_64"}},
Inseob Kim5219c0e2021-06-17 00:33:00 +09001699 {"x86", "", "", []string{"x86"}},
Dan Albert4098deb2016-10-19 14:04:41 -07001700 }
1701}
1702
Colin Crossa6845402020-11-16 15:08:19 -08001703// getAmlAbisConfig returns a list of archConfigs for the ABIs supported by mainline modules.
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001704func getAmlAbisConfig() []archConfig {
1705 return []archConfig{
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001706 {"arm64", "armv8-a", "", []string{"arm64-v8a"}},
Inseob Kim5219c0e2021-06-17 00:33:00 +09001707 {"arm", "armv7-a-neon", "", []string{"armeabi-v7a"}},
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001708 {"x86_64", "", "", []string{"x86_64"}},
Inseob Kim5219c0e2021-06-17 00:33:00 +09001709 {"x86", "", "", []string{"x86"}},
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001710 }
1711}
1712
Colin Crossa6845402020-11-16 15:08:19 -08001713// decodeArchSettings converts a list of archConfigs into a list of Targets for the given OsType.
Liz Kammerb7f33662022-02-28 14:16:16 -05001714func decodeAndroidArchSettings(archConfigs []archConfig) ([]Target, error) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001715 var ret []Target
Dan Willemsen322acaf2016-01-12 23:07:05 -08001716
Dan Albert4098deb2016-10-19 14:04:41 -07001717 for _, config := range archConfigs {
Liz Kammer992918d2022-11-11 10:37:54 -05001718 arch, err := decodeArch(Android, config.Arch, &config.ArchVariant,
1719 &config.CpuVariant, config.Abi)
Dan Willemsen322acaf2016-01-12 23:07:05 -08001720 if err != nil {
1721 return nil, err
1722 }
Colin Cross3b19f5d2019-09-17 14:45:31 -07001723
Colin Crossa1ad8d12016-06-01 17:09:44 -07001724 ret = append(ret, Target{
1725 Os: Android,
1726 Arch: arch,
1727 })
Dan Willemsen322acaf2016-01-12 23:07:05 -08001728 }
1729
1730 return ret, nil
1731}
1732
Colin Crossa6845402020-11-16 15:08:19 -08001733// decodeArch converts a set of strings from product variables into an Arch struct.
Colin Crossa74ca042019-01-31 14:31:51 -08001734func decodeArch(os OsType, arch string, archVariant, cpuVariant *string, abi []string) (Arch, error) {
Colin Crossa6845402020-11-16 15:08:19 -08001735 // Verify the arch is valid
Colin Crosseeabb892015-11-20 13:07:51 -08001736 archType, ok := archTypeMap[arch]
1737 if !ok {
1738 return Arch{}, fmt.Errorf("unknown arch %q", arch)
1739 }
Colin Cross4225f652015-09-17 14:33:42 -07001740
Colin Crosseeabb892015-11-20 13:07:51 -08001741 a := Arch{
Colin Cross4225f652015-09-17 14:33:42 -07001742 ArchType: archType,
Colin Crossa6845402020-11-16 15:08:19 -08001743 ArchVariant: String(archVariant),
1744 CpuVariant: String(cpuVariant),
Colin Crossa74ca042019-01-31 14:31:51 -08001745 Abi: abi,
Colin Crosseeabb892015-11-20 13:07:51 -08001746 }
1747
Colin Crossa6845402020-11-16 15:08:19 -08001748 // Convert generic arch variants into the empty string.
Colin Crosseeabb892015-11-20 13:07:51 -08001749 if a.ArchVariant == a.ArchType.Name || a.ArchVariant == "generic" {
1750 a.ArchVariant = ""
1751 }
1752
Colin Crossa6845402020-11-16 15:08:19 -08001753 // Convert generic CPU variants into the empty string.
Colin Crosseeabb892015-11-20 13:07:51 -08001754 if a.CpuVariant == a.ArchType.Name || a.CpuVariant == "generic" {
1755 a.CpuVariant = ""
1756 }
1757
Liz Kammer2c2afe22022-02-11 11:35:03 -05001758 if a.ArchVariant != "" {
1759 if validArchVariants := archVariants[archType]; !InList(a.ArchVariant, validArchVariants) {
1760 return Arch{}, fmt.Errorf("[%q] unknown arch variant %q, support variants: %q", archType, a.ArchVariant, validArchVariants)
1761 }
1762 }
1763
1764 if a.CpuVariant != "" {
1765 if validCpuVariants := cpuVariants[archType]; !InList(a.CpuVariant, validCpuVariants) {
1766 return Arch{}, fmt.Errorf("[%q] unknown cpu variant %q, support variants: %q", archType, a.CpuVariant, validCpuVariants)
1767 }
1768 }
1769
Colin Crossa6845402020-11-16 15:08:19 -08001770 // Filter empty ABIs out of the list.
Colin Crosseeabb892015-11-20 13:07:51 -08001771 for i := 0; i < len(a.Abi); i++ {
1772 if a.Abi[i] == "" {
1773 a.Abi = append(a.Abi[:i], a.Abi[i+1:]...)
1774 i--
1775 }
1776 }
1777
Liz Kammere8303bd2022-02-16 09:02:48 -05001778 // Set ArchFeatures from the arch type. for Android OS, other os-es do not specify features
1779 if os == Android {
1780 if featureMap, ok := androidArchFeatureMap[archType]; ok {
Dan Willemsen01a3c252019-01-11 19:02:16 -08001781 a.ArchFeatures = featureMap[a.ArchVariant]
1782 }
Colin Crossc5c24ad2015-11-20 15:35:00 -08001783 }
1784
Colin Crosseeabb892015-11-20 13:07:51 -08001785 return a, nil
Colin Cross4225f652015-09-17 14:33:42 -07001786}
1787
Colin Crossa6845402020-11-16 15:08:19 -08001788// filterMultilibTargets takes a list of Targets and a multilib value and returns a new list of
1789// Targets containing only those that have the given multilib value.
Colin Cross69617d32016-09-06 10:39:07 -07001790func filterMultilibTargets(targets []Target, multilib string) []Target {
1791 var ret []Target
1792 for _, t := range targets {
1793 if t.Arch.ArchType.Multilib == multilib {
1794 ret = append(ret, t)
1795 }
1796 }
1797 return ret
1798}
1799
Colin Crossa6845402020-11-16 15:08:19 -08001800// getCommonTargets returns the set of Os specific common architecture targets for each Os in a list
1801// of targets.
Nan Zhangdb0b9a32017-02-27 10:12:13 -08001802func getCommonTargets(targets []Target) []Target {
1803 var ret []Target
1804 set := make(map[string]bool)
1805
1806 for _, t := range targets {
1807 if _, found := set[t.Os.String()]; !found {
1808 set[t.Os.String()] = true
Colin Cross39a18142022-06-24 18:43:40 -07001809 common := commonTargetMap[t.Os.String()]
1810 common.HostCross = t.HostCross
1811 ret = append(ret, common)
Nan Zhangdb0b9a32017-02-27 10:12:13 -08001812 }
1813 }
1814
1815 return ret
1816}
1817
Sam Delmericocc271e22022-06-01 15:45:02 +00001818// 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 -07001819// that contains zero or one Target for each OsType and HostCross, selecting the one that matches
1820// the earliest filter.
Sam Delmericocc271e22022-06-01 15:45:02 +00001821func FirstTarget(targets []Target, filters ...string) []Target {
Jiyong Park22101982020-09-17 19:09:58 +09001822 // find the first target from each OS
1823 var ret []Target
Colin Crossc0f0eb82022-07-19 14:41:11 -07001824 type osHostCross struct {
1825 os OsType
1826 hostCross bool
1827 }
1828 set := make(map[osHostCross]bool)
Jiyong Park22101982020-09-17 19:09:58 +09001829
Colin Cross6b4a32d2017-12-05 13:42:45 -08001830 for _, filter := range filters {
1831 buildTargets := filterMultilibTargets(targets, filter)
Jiyong Park22101982020-09-17 19:09:58 +09001832 for _, t := range buildTargets {
Colin Crossc0f0eb82022-07-19 14:41:11 -07001833 key := osHostCross{t.Os, t.HostCross}
1834 if _, found := set[key]; !found {
1835 set[key] = true
Jiyong Park22101982020-09-17 19:09:58 +09001836 ret = append(ret, t)
1837 }
Colin Cross6b4a32d2017-12-05 13:42:45 -08001838 }
1839 }
Jiyong Park22101982020-09-17 19:09:58 +09001840 return ret
Colin Cross6b4a32d2017-12-05 13:42:45 -08001841}
1842
Colin Crossa6845402020-11-16 15:08:19 -08001843// decodeMultilibTargets uses the module's multilib setting to select one or more targets from a
1844// list of Targets.
Colin Crossee0bc3b2018-10-02 22:01:37 -07001845func decodeMultilibTargets(multilib string, targets []Target, prefer32 bool) ([]Target, error) {
Colin Crossa6845402020-11-16 15:08:19 -08001846 var buildTargets []Target
Colin Cross6b4a32d2017-12-05 13:42:45 -08001847
Colin Cross4225f652015-09-17 14:33:42 -07001848 switch multilib {
1849 case "common":
Colin Cross6b4a32d2017-12-05 13:42:45 -08001850 buildTargets = getCommonTargets(targets)
1851 case "common_first":
1852 buildTargets = getCommonTargets(targets)
1853 if prefer32 {
Sam Delmericocc271e22022-06-01 15:45:02 +00001854 buildTargets = append(buildTargets, FirstTarget(targets, "lib32", "lib64")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001855 } else {
Sam Delmericocc271e22022-06-01 15:45:02 +00001856 buildTargets = append(buildTargets, FirstTarget(targets, "lib64", "lib32")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001857 }
Colin Cross4225f652015-09-17 14:33:42 -07001858 case "both":
Colin Cross8b74d172016-09-13 09:59:14 -07001859 if prefer32 {
1860 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1861 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1862 } else {
1863 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1864 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1865 }
Colin Cross4225f652015-09-17 14:33:42 -07001866 case "32":
Colin Cross69617d32016-09-06 10:39:07 -07001867 buildTargets = filterMultilibTargets(targets, "lib32")
Colin Cross4225f652015-09-17 14:33:42 -07001868 case "64":
Colin Cross69617d32016-09-06 10:39:07 -07001869 buildTargets = filterMultilibTargets(targets, "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001870 case "first":
1871 if prefer32 {
Sam Delmericocc271e22022-06-01 15:45:02 +00001872 buildTargets = FirstTarget(targets, "lib32", "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001873 } else {
Sam Delmericocc271e22022-06-01 15:45:02 +00001874 buildTargets = FirstTarget(targets, "lib64", "lib32")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001875 }
Victor Chang9448e8f2020-09-14 15:34:16 +01001876 case "first_prefer32":
Sam Delmericocc271e22022-06-01 15:45:02 +00001877 buildTargets = FirstTarget(targets, "lib32", "lib64")
Colin Cross69617d32016-09-06 10:39:07 -07001878 case "prefer32":
Colin Cross3dceee32018-09-06 10:19:57 -07001879 buildTargets = filterMultilibTargets(targets, "lib32")
1880 if len(buildTargets) == 0 {
1881 buildTargets = filterMultilibTargets(targets, "lib64")
1882 }
Dan Willemsen47450072021-10-19 20:24:49 -07001883 case "darwin_universal":
1884 buildTargets = filterMultilibTargets(targets, "lib64")
1885 // Reverse the targets so that the first architecture can depend on the second
1886 // architecture module in order to merge the outputs.
Colin Crossb5e3f7d2023-07-06 15:37:53 -07001887 ReverseSliceInPlace(buildTargets)
Dan Willemsen47450072021-10-19 20:24:49 -07001888 case "darwin_universal_common_first":
1889 archTargets := filterMultilibTargets(targets, "lib64")
Colin Crossb5e3f7d2023-07-06 15:37:53 -07001890 ReverseSliceInPlace(archTargets)
Dan Willemsen47450072021-10-19 20:24:49 -07001891 buildTargets = append(getCommonTargets(targets), archTargets...)
Colin Cross4225f652015-09-17 14:33:42 -07001892 default:
Victor Chang9448e8f2020-09-14 15:34:16 +01001893 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 -07001894 multilib)
Colin Cross4225f652015-09-17 14:33:42 -07001895 }
1896
Colin Crossa1ad8d12016-06-01 17:09:44 -07001897 return buildTargets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001898}
Jingwen Chen5d864492021-02-24 07:20:12 -05001899
Chris Parsonsc424b762021-04-29 18:06:50 -04001900func (m *ModuleBase) getArchPropertySet(propertySet interface{}, archType ArchType) interface{} {
1901 archString := archType.Field
1902 for i := range m.archProperties {
1903 if m.archProperties[i] == nil {
1904 // Skip over nil properties
1905 continue
1906 }
1907
1908 // Not archProperties are usable; this function looks for properties of a very specific
1909 // form, and ignores the rest.
1910 for _, archProperty := range m.archProperties[i] {
1911 // archPropValue is a property struct, we are looking for the form:
1912 // `arch: { arm: { key: value, ... }}`
1913 archPropValue := reflect.ValueOf(archProperty).Elem()
1914
1915 // Unwrap src so that it should looks like a pointer to `arm: { key: value, ... }`
1916 src := archPropValue.FieldByName("Arch").Elem()
1917
1918 // Step into non-nil pointers to structs in the src value.
1919 if src.Kind() == reflect.Ptr {
1920 if src.IsNil() {
1921 continue
1922 }
1923 src = src.Elem()
1924 }
1925
1926 // Find the requested field (e.g. arm, x86) in the src struct.
1927 src = src.FieldByName(archString)
1928
1929 // We only care about structs.
1930 if !src.IsValid() || src.Kind() != reflect.Struct {
1931 continue
1932 }
1933
1934 // If the value of the field is a struct then step into the
1935 // BlueprintEmbed field. The special "BlueprintEmbed" name is
1936 // used by createArchPropTypeDesc to embed the arch properties
1937 // in the parent struct, so the src arch prop should be in this
1938 // field.
1939 //
1940 // See createArchPropTypeDesc for more details on how Arch-specific
1941 // module properties are processed from the nested props and written
1942 // into the module's archProperties.
1943 src = src.FieldByName("BlueprintEmbed")
1944
1945 // Clone the destination prop, since we want a unique prop struct per arch.
1946 propertySetClone := reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
1947
1948 // Copy the located property struct into the cloned destination property struct.
1949 err := proptools.ExtendMatchingProperties([]interface{}{propertySetClone}, src.Interface(), nil, proptools.OrderReplace)
1950 if err != nil {
1951 // This is fine, it just means the src struct doesn't match the type of propertySet.
1952 continue
1953 }
1954
1955 return propertySetClone
1956 }
1957 }
1958 // No property set was found specific to the given arch, so return an empty
1959 // property set.
1960 return reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
1961}
1962
1963// getMultilibPropertySet returns a property set struct matching the type of
1964// `propertySet`, containing multilib-specific module properties for the given architecture.
1965// If no multilib-specific properties exist for the given architecture, returns an empty property
1966// set matching `propertySet`'s type.
1967func (m *ModuleBase) getMultilibPropertySet(propertySet interface{}, archType ArchType) interface{} {
1968 // archType.Multilib is lowercase (for example, lib32) but property struct field is
1969 // capitalized, such as Lib32, so use strings.Title to capitalize it.
1970 multiLibString := strings.Title(archType.Multilib)
1971
1972 for i := range m.archProperties {
1973 if m.archProperties[i] == nil {
1974 // Skip over nil properties
1975 continue
1976 }
1977
1978 // Not archProperties are usable; this function looks for properties of a very specific
1979 // form, and ignores the rest.
1980 for _, archProperties := range m.archProperties[i] {
1981 // archPropValue is a property struct, we are looking for the form:
1982 // `multilib: { lib32: { key: value, ... }}`
1983 archPropValue := reflect.ValueOf(archProperties).Elem()
1984
1985 // Unwrap src so that it should looks like a pointer to `lib32: { key: value, ... }`
1986 src := archPropValue.FieldByName("Multilib").Elem()
1987
1988 // Step into non-nil pointers to structs in the src value.
1989 if src.Kind() == reflect.Ptr {
1990 if src.IsNil() {
1991 // Ignore nil pointers.
1992 continue
1993 }
1994 src = src.Elem()
1995 }
1996
1997 // Find the requested field (e.g. lib32) in the src struct.
1998 src = src.FieldByName(multiLibString)
1999
2000 // We only care about valid struct pointers.
2001 if !src.IsValid() || src.Kind() != reflect.Ptr || src.Elem().Kind() != reflect.Struct {
2002 continue
2003 }
2004
2005 // Get the zero value for the requested property set.
2006 propertySetClone := reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
2007
2008 // Copy the located property struct into the "zero" property set struct.
2009 err := proptools.ExtendMatchingProperties([]interface{}{propertySetClone}, src.Interface(), nil, proptools.OrderReplace)
2010
2011 if err != nil {
2012 // This is fine, it just means the src struct doesn't match.
2013 continue
2014 }
2015
2016 return propertySetClone
2017 }
2018 }
2019
2020 // There were no multilib properties specifically matching the given archtype.
2021 // Return zeroed value.
2022 return reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
2023}
2024
Liz Kammerb6dbc872021-05-14 15:14:40 -04002025// ArchVariantContext defines the limited context necessary to retrieve arch_variant properties.
2026type ArchVariantContext interface {
2027 ModuleErrorf(fmt string, args ...interface{})
2028 PropertyErrorf(property, fmt string, args ...interface{})
2029}
2030
Liz Kammer9abd62d2021-05-21 08:37:59 -04002031// ArchVariantProperties represents a map of arch-variant config strings to a property interface{}.
2032type ArchVariantProperties map[string]interface{}
2033
2034// ConfigurationAxisToArchVariantProperties represents a map of bazel.ConfigurationAxis to
2035// ArchVariantProperties, such that each independent arch-variant axis maps to the
2036// configs/properties for that axis.
2037type ConfigurationAxisToArchVariantProperties map[bazel.ConfigurationAxis]ArchVariantProperties
2038
2039// GetArchVariantProperties returns a ConfigurationAxisToArchVariantProperties where the
2040// arch-variant properties correspond to the values of the properties of the 'propertySet' struct
2041// that are specific to that axis/configuration. Each axis is independent, containing
2042// non-overlapping configs that correspond to the various "arch-variant" support, at this time:
Colin Crossd079e0b2022-08-16 10:27:33 -07002043//
2044// arches (including multilib)
2045// oses
2046// arch+os combinations
Jingwen Chen5d864492021-02-24 07:20:12 -05002047//
Liz Kammer9abd62d2021-05-21 08:37:59 -04002048// For example, passing a struct { Foo bool, Bar string } will return an interface{} that can be
2049// type asserted back into the same struct, containing the config-specific property value specified
2050// by the module if defined.
Chris Parsonsc424b762021-04-29 18:06:50 -04002051//
2052// Arch-specific properties may come from an arch stanza or a multilib stanza; properties
2053// in these stanzas are combined.
2054// For example: `arch: { x86: { Foo: ["bar"] } }, multilib: { lib32: {` Foo: ["baz"] } }`
2055// will result in `Foo: ["bar", "baz"]` being returned for architecture x86, if the given
2056// propertyset contains `Foo []string`.
Liz Kammer9abd62d2021-05-21 08:37:59 -04002057func (m *ModuleBase) GetArchVariantProperties(ctx ArchVariantContext, propertySet interface{}) ConfigurationAxisToArchVariantProperties {
Jingwen Chen5d864492021-02-24 07:20:12 -05002058 // Return value of the arch types to the prop values for that arch.
Liz Kammer9abd62d2021-05-21 08:37:59 -04002059 axisToProps := ConfigurationAxisToArchVariantProperties{}
Jingwen Chen5d864492021-02-24 07:20:12 -05002060
2061 // Nothing to do for non-arch-specific modules.
2062 if !m.ArchSpecific() {
Liz Kammer9abd62d2021-05-21 08:37:59 -04002063 return axisToProps
Jingwen Chen5d864492021-02-24 07:20:12 -05002064 }
2065
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002066 dstType := reflect.ValueOf(propertySet).Type()
2067 var archProperties []interface{}
2068
2069 // First find the property set in the module that corresponds to the requested
Usta851a3272022-01-05 23:42:33 -05002070 // one. m.archProperties[i] corresponds to m.GetProperties()[i].
2071 for i, generalProp := range m.GetProperties() {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002072 srcType := reflect.ValueOf(generalProp).Type()
2073 if srcType == dstType {
2074 archProperties = m.archProperties[i]
Liz Kammer135bf552021-08-11 10:46:06 -04002075 axisToProps[bazel.NoConfigAxis] = ArchVariantProperties{"": generalProp}
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002076 break
2077 }
2078 }
2079
2080 if archProperties == nil {
2081 // This module does not have the property set requested
Liz Kammer9abd62d2021-05-21 08:37:59 -04002082 return axisToProps
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002083 }
2084
Liz Kammer9abd62d2021-05-21 08:37:59 -04002085 archToProp := ArchVariantProperties{}
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002086 // For each arch type (x86, arm64, etc.)
Chris Parsonsc424b762021-04-29 18:06:50 -04002087 for _, arch := range ArchTypeList() {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002088 // Arch properties are sometimes sharded (see createArchPropTypeDesc() ).
Cole Faustc843b992022-08-02 18:06:50 -07002089 // Iterate over every shard and extract a struct with the same type as the
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002090 // input one that contains the data specific to that arch.
2091 propertyStructs := make([]reflect.Value, 0)
Cole Faustc843b992022-08-02 18:06:50 -07002092 archFeaturePropertyStructs := make(map[string][]reflect.Value, 0)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002093 for _, archProperty := range archProperties {
Lukacs T. Berki5f518392021-05-17 11:44:58 +02002094 archTypeStruct, ok := getArchTypeStruct(ctx, archProperty, arch)
2095 if ok {
2096 propertyStructs = append(propertyStructs, archTypeStruct)
Cole Faustc843b992022-08-02 18:06:50 -07002097
2098 // For each feature this arch supports (arm: neon, x86: ssse3, sse4, ...)
2099 for _, feature := range archFeatures[arch] {
2100 prefix := "arch." + arch.Name + "." + feature
2101 if featureProperties, ok := getChildPropertyStruct(ctx, archTypeStruct, feature, prefix); ok {
2102 archFeaturePropertyStructs[feature] = append(archFeaturePropertyStructs[feature], featureProperties)
2103 }
2104 }
Lukacs T. Berki5f518392021-05-17 11:44:58 +02002105 }
2106 multilibStruct, ok := getMultilibStruct(ctx, archProperty, arch)
2107 if ok {
2108 propertyStructs = append(propertyStructs, multilibStruct)
2109 }
Jingwen Chen5d864492021-02-24 07:20:12 -05002110 }
2111
Cole Faustc843b992022-08-02 18:06:50 -07002112 archToProp[arch.Name] = mergeStructs(ctx, propertyStructs, propertySet)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002113
Cole Faustc843b992022-08-02 18:06:50 -07002114 // In soong, if multiple features match the current configuration, they're
2115 // all used. In bazel, we have to have unambiguous select() statements, so
2116 // we can't have two features that are both active in the same select().
2117 // One alternative is to split out each feature into a separate select(),
2118 // but then it's difficult to support exclude_srcs, which may need to
2119 // exclude things from the regular arch select() statement if a certain
2120 // feature is active. Instead, keep the features in the same select
2121 // statement as the arches, but emit the power set of all possible
2122 // combinations of features, so that bazel can match the most precise one.
2123 allFeatures := make([]string, 0, len(archFeaturePropertyStructs))
2124 for feature := range archFeaturePropertyStructs {
2125 allFeatures = append(allFeatures, feature)
2126 }
2127 for _, features := range bazel.PowerSetWithoutEmptySet(allFeatures) {
2128 sort.Strings(features)
2129 propsForCurrentFeatureSet := make([]reflect.Value, 0)
2130 propsForCurrentFeatureSet = append(propsForCurrentFeatureSet, propertyStructs...)
2131 for _, feature := range features {
2132 propsForCurrentFeatureSet = append(propsForCurrentFeatureSet, archFeaturePropertyStructs[feature]...)
2133 }
2134 archToProp[arch.Name+"-"+strings.Join(features, "-")] =
2135 mergeStructs(ctx, propsForCurrentFeatureSet, propertySet)
2136 }
Jingwen Chen5d864492021-02-24 07:20:12 -05002137 }
Liz Kammer9abd62d2021-05-21 08:37:59 -04002138 axisToProps[bazel.ArchConfigurationAxis] = archToProp
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002139
Liz Kammer9abd62d2021-05-21 08:37:59 -04002140 osToProp := ArchVariantProperties{}
2141 archOsToProp := ArchVariantProperties{}
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002142
Liz Kammerfdd72e62021-10-11 15:41:03 -04002143 linuxStructs := getTargetStructs(ctx, archProperties, "Linux")
2144 bionicStructs := getTargetStructs(ctx, archProperties, "Bionic")
2145 hostStructs := getTargetStructs(ctx, archProperties, "Host")
Colin Crossa98d36d2022-03-07 14:39:49 -08002146 hostLinuxStructs := getTargetStructs(ctx, archProperties, "Host_linux")
Liz Kammerfdd72e62021-10-11 15:41:03 -04002147 hostNotWindowsStructs := getTargetStructs(ctx, archProperties, "Not_windows")
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002148
Liz Kammer9abd62d2021-05-21 08:37:59 -04002149 // For android, linux, ...
2150 for _, os := range osTypeList {
2151 if os == CommonOS {
2152 // It looks like this OS value is not used in Blueprint files
2153 continue
2154 }
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002155 osStructs := make([]reflect.Value, 0)
Liz Kammerfdd72e62021-10-11 15:41:03 -04002156
2157 osSpecificStructs := getTargetStructs(ctx, archProperties, os.Field)
2158 if os.Class == Host {
2159 osStructs = append(osStructs, hostStructs...)
Chris Parsonsa37e1952021-09-28 16:47:36 -04002160 }
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002161 if os.Linux() {
2162 osStructs = append(osStructs, linuxStructs...)
2163 }
2164 if os.Bionic() {
2165 osStructs = append(osStructs, bionicStructs...)
2166 }
Colin Crossa98d36d2022-03-07 14:39:49 -08002167 if os.Linux() && os.Class == Host {
2168 osStructs = append(osStructs, hostLinuxStructs...)
2169 }
Liz Kammerfdd72e62021-10-11 15:41:03 -04002170
2171 if os == LinuxMusl {
2172 osStructs = append(osStructs, getTargetStructs(ctx, archProperties, "Musl")...)
2173 }
2174 if os == Linux {
2175 osStructs = append(osStructs, getTargetStructs(ctx, archProperties, "Glibc")...)
2176 }
2177
2178 osStructs = append(osStructs, osSpecificStructs...)
2179
2180 if os.Class == Host && os != Windows {
2181 osStructs = append(osStructs, hostNotWindowsStructs...)
2182 }
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002183 osToProp[os.Name] = mergeStructs(ctx, osStructs, propertySet)
Chris Parsonsa37e1952021-09-28 16:47:36 -04002184
Liz Kammer9abd62d2021-05-21 08:37:59 -04002185 // For arm, x86, ...
2186 for _, arch := range osArchTypeMap[os] {
Chris Parsonsa37e1952021-09-28 16:47:36 -04002187 osArchStructs := make([]reflect.Value, 0)
2188
Chris Parsonsa37e1952021-09-28 16:47:36 -04002189 // Auto-combine with Linux_ and Bionic_ targets. This potentially results in
2190 // repetition and select() bloat, but use of Linux_* and Bionic_* targets is rare.
2191 // TODO(b/201423152): Look into cleanup.
2192 if os.Linux() {
2193 targetField := "Linux_" + arch.Name
Liz Kammerfdd72e62021-10-11 15:41:03 -04002194 targetStructs := getTargetStructs(ctx, archProperties, targetField)
2195 osArchStructs = append(osArchStructs, targetStructs...)
Chris Parsonsa37e1952021-09-28 16:47:36 -04002196 }
2197 if os.Bionic() {
2198 targetField := "Bionic_" + arch.Name
Liz Kammerfdd72e62021-10-11 15:41:03 -04002199 targetStructs := getTargetStructs(ctx, archProperties, targetField)
2200 osArchStructs = append(osArchStructs, targetStructs...)
Chris Parsonsa37e1952021-09-28 16:47:36 -04002201 }
Colin Cross2d295a22022-03-07 14:46:20 -08002202 if os == LinuxMusl {
2203 targetField := "Musl_" + arch.Name
2204 targetStructs := getTargetStructs(ctx, archProperties, targetField)
2205 osArchStructs = append(osArchStructs, targetStructs...)
2206 }
2207 if os == Linux {
2208 targetField := "Glibc_" + arch.Name
2209 targetStructs := getTargetStructs(ctx, archProperties, targetField)
2210 osArchStructs = append(osArchStructs, targetStructs...)
2211 }
Chris Parsonsa37e1952021-09-28 16:47:36 -04002212
Liz Kammerfdd72e62021-10-11 15:41:03 -04002213 targetField := GetCompoundTargetField(os, arch)
2214 targetName := fmt.Sprintf("%s_%s", os.Name, arch.Name)
2215 targetStructs := getTargetStructs(ctx, archProperties, targetField)
2216 osArchStructs = append(osArchStructs, targetStructs...)
2217
Chris Parsonsa37e1952021-09-28 16:47:36 -04002218 archOsToProp[targetName] = mergeStructs(ctx, osArchStructs, propertySet)
Liz Kammer9abd62d2021-05-21 08:37:59 -04002219 }
2220 }
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002221
Liz Kammer9abd62d2021-05-21 08:37:59 -04002222 axisToProps[bazel.OsConfigurationAxis] = osToProp
2223 axisToProps[bazel.OsArchConfigurationAxis] = archOsToProp
Liz Kammer9abd62d2021-05-21 08:37:59 -04002224 return axisToProps
Jingwen Chen5d864492021-02-24 07:20:12 -05002225}
Jingwen Chen91220d72021-03-24 02:18:33 -04002226
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04002227// Returns a struct matching the propertySet interface, containing properties specific to the targetName
2228// For example, given these arguments:
Colin Crossd079e0b2022-08-16 10:27:33 -07002229//
2230// propertySet = BaseCompilerProperties
2231// targetName = "android_arm"
2232//
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04002233// And given this Android.bp fragment:
Colin Crossd079e0b2022-08-16 10:27:33 -07002234//
2235// target:
2236// android_arm: {
2237// srcs: ["foo.c"],
2238// }
2239// android_arm64: {
2240// srcs: ["bar.c"],
2241// }
2242// }
2243//
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04002244// This would return a BaseCompilerProperties with BaseCompilerProperties.Srcs = ["foo.c"]
Liz Kammerfdd72e62021-10-11 15:41:03 -04002245func getTargetStructs(ctx ArchVariantContext, archProperties []interface{}, targetName string) []reflect.Value {
2246 var propertyStructs []reflect.Value
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04002247 for _, archProperty := range archProperties {
2248 archPropValues := reflect.ValueOf(archProperty).Elem()
2249 targetProp := archPropValues.FieldByName("Target").Elem()
2250 targetStruct, ok := getChildPropertyStruct(ctx, targetProp, targetName, targetName)
2251 if ok {
2252 propertyStructs = append(propertyStructs, targetStruct)
Chris Parsonsa37e1952021-09-28 16:47:36 -04002253 } else {
Liz Kammerfdd72e62021-10-11 15:41:03 -04002254 return []reflect.Value{}
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04002255 }
2256 }
2257
Liz Kammerfdd72e62021-10-11 15:41:03 -04002258 return propertyStructs
Chris Parsonsa37e1952021-09-28 16:47:36 -04002259}
2260
2261func mergeStructs(ctx ArchVariantContext, propertyStructs []reflect.Value, propertySet interface{}) interface{} {
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04002262 // Create a new instance of the requested property set
2263 value := reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
2264
2265 // Merge all the structs together
2266 for _, propertyStruct := range propertyStructs {
2267 mergePropertyStruct(ctx, value, propertyStruct)
2268 }
2269
2270 return value
2271}
Liz Kammere8303bd2022-02-16 09:02:48 -05002272
2273func printArchTypeStarlarkDict(dict map[ArchType][]string) string {
2274 valDict := make(map[string]string, len(dict))
2275 for k, v := range dict {
2276 valDict[k.String()] = starlark_fmt.PrintStringList(v, 1)
2277 }
2278 return starlark_fmt.PrintDict(valDict, 0)
2279}
2280
2281func printArchTypeNestedStarlarkDict(dict map[ArchType]map[string][]string) string {
2282 valDict := make(map[string]string, len(dict))
2283 for k, v := range dict {
2284 valDict[k.String()] = starlark_fmt.PrintStringListDict(v, 1)
2285 }
2286 return starlark_fmt.PrintDict(valDict, 0)
2287}
2288
Liz Kammer992918d2022-11-11 10:37:54 -05002289func printArchConfigList(arches []archConfig) string {
2290 jsonOut, err := json.MarshalIndent(arches, "", starlark_fmt.Indention(1))
2291 if err != nil {
2292 panic(fmt.Errorf("Error converting arch configs %#v to json: %q", arches, err))
2293 }
2294 return fmt.Sprintf("json.decode('''%s''')", string(jsonOut))
2295}
2296
Liz Kammere8303bd2022-02-16 09:02:48 -05002297func StarlarkArchConfigurations() string {
2298 return fmt.Sprintf(`
2299_arch_to_variants = %s
2300
2301_arch_to_cpu_variants = %s
2302
2303_arch_to_features = %s
2304
2305_android_arch_feature_for_arch_variant = %s
2306
Liz Kammer992918d2022-11-11 10:37:54 -05002307_aml_arches = %s
2308
2309_ndk_arches = %s
2310
Liz Kammere8303bd2022-02-16 09:02:48 -05002311arch_to_variants = _arch_to_variants
2312arch_to_cpu_variants = _arch_to_cpu_variants
2313arch_to_features = _arch_to_features
2314android_arch_feature_for_arch_variants = _android_arch_feature_for_arch_variant
Liz Kammer992918d2022-11-11 10:37:54 -05002315aml_arches = _aml_arches
2316ndk_arches = _ndk_arches
Liz Kammere8303bd2022-02-16 09:02:48 -05002317`, printArchTypeStarlarkDict(archVariants),
2318 printArchTypeStarlarkDict(cpuVariants),
2319 printArchTypeStarlarkDict(archFeatures),
2320 printArchTypeNestedStarlarkDict(androidArchFeatureMap),
Liz Kammer992918d2022-11-11 10:37:54 -05002321 printArchConfigList(getAmlAbisConfig()),
2322 printArchConfigList(getNdkAbisConfig()),
Liz Kammere8303bd2022-02-16 09:02:48 -05002323 )
2324}