blob: 9bc9d89243e0fb8b2754ecb0a96b209c1fb95a1a [file] [log] [blame]
Colin Cross3f40fa42015-01-30 17:27:36 -08001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Colin Cross635c3b02016-05-18 15:37:25 -070015package android
Colin Cross3f40fa42015-01-30 17:27:36 -080016
17import (
Colin Cross74ba9622019-02-11 15:11:14 -080018 "encoding"
Colin Cross3f40fa42015-01-30 17:27:36 -080019 "fmt"
20 "reflect"
21 "runtime"
Cole Faustc843b992022-08-02 18:06:50 -070022 "sort"
Colin Cross3f40fa42015-01-30 17:27:36 -080023 "strings"
Colin Crossf6566ed2015-03-24 11:13:38 -070024
Colin Crosscb0ac952021-07-20 13:17:15 -070025 "android/soong/bazel"
Liz Kammere8303bd2022-02-16 09:02:48 -050026 "android/soong/starlark_fmt"
Colin Crosscb0ac952021-07-20 13:17:15 -070027
Colin Cross0f7d2ef2019-10-16 11:03:10 -070028 "github.com/google/blueprint"
Colin Cross617b88a2020-08-24 18:04:09 -070029 "github.com/google/blueprint/bootstrap"
Colin Crossf6566ed2015-03-24 11:13:38 -070030 "github.com/google/blueprint/proptools"
Colin Cross3f40fa42015-01-30 17:27:36 -080031)
32
Colin Cross3f40fa42015-01-30 17:27:36 -080033/*
34Example blueprints file containing all variant property groups, with comment listing what type
35of variants get properties in that group:
36
37module {
38 arch: {
39 arm: {
40 // Host or device variants with arm architecture
41 },
42 arm64: {
43 // Host or device variants with arm64 architecture
44 },
Colin Cross3f40fa42015-01-30 17:27:36 -080045 x86: {
46 // Host or device variants with x86 architecture
47 },
48 x86_64: {
49 // Host or device variants with x86_64 architecture
50 },
51 },
52 multilib: {
53 lib32: {
54 // Host or device variants for 32-bit architectures
55 },
56 lib64: {
57 // Host or device variants for 64-bit architectures
58 },
59 },
60 target: {
61 android: {
Martin Stjernholme284b482020-09-23 21:03:27 +010062 // Device variants (implies Bionic)
Colin Cross3f40fa42015-01-30 17:27:36 -080063 },
64 host: {
65 // Host variants
66 },
Martin Stjernholme284b482020-09-23 21:03:27 +010067 bionic: {
68 // Bionic (device and host) variants
69 },
70 linux_bionic: {
71 // Bionic host variants
72 },
73 linux: {
74 // Bionic (device and host) and Linux glibc variants
75 },
Dan Willemsen5746bd42017-10-02 19:42:01 -070076 linux_glibc: {
Martin Stjernholme284b482020-09-23 21:03:27 +010077 // Linux host variants (using non-Bionic libc)
Colin Cross3f40fa42015-01-30 17:27:36 -080078 },
79 darwin: {
80 // Darwin host variants
81 },
82 windows: {
83 // Windows host variants
84 },
85 not_windows: {
86 // Non-windows host variants
87 },
Martin Stjernholme284b482020-09-23 21:03:27 +010088 android_arm: {
89 // Any <os>_<arch> combination restricts to that os and arch
90 },
Colin Cross3f40fa42015-01-30 17:27:36 -080091 },
92}
93*/
Colin Cross7d5136f2015-05-11 13:39:40 -070094
Colin Cross3f40fa42015-01-30 17:27:36 -080095// An Arch indicates a single CPU architecture.
96type Arch struct {
Colin Crossa6845402020-11-16 15:08:19 -080097 // The type of the architecture (arm, arm64, x86, or x86_64).
98 ArchType ArchType
99
100 // The variant of the architecture, for example "armv7-a" or "armv7-a-neon" for arm.
101 ArchVariant string
102
103 // The variant of the CPU, for example "cortex-a53" for arm64.
104 CpuVariant string
105
106 // The list of Android app ABIs supported by the CPU architecture, for example "arm64-v8a".
107 Abi []string
108
109 // The list of arch-specific features supported by the CPU architecture, for example "neon".
Colin Crossc5c24ad2015-11-20 15:35:00 -0800110 ArchFeatures []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800111}
112
Colin Crossa6845402020-11-16 15:08:19 -0800113// String returns the Arch as a string. The value is used as the name of the variant created
114// by archMutator.
Colin Cross3f40fa42015-01-30 17:27:36 -0800115func (a Arch) String() string {
Colin Crossd3ba0392015-05-07 14:11:29 -0700116 s := a.ArchType.String()
Colin Cross3f40fa42015-01-30 17:27:36 -0800117 if a.ArchVariant != "" {
118 s += "_" + a.ArchVariant
119 }
120 if a.CpuVariant != "" {
121 s += "_" + a.CpuVariant
122 }
123 return s
124}
125
Colin Crossa6845402020-11-16 15:08:19 -0800126// ArchType is used to define the 4 supported architecture types (arm, arm64, x86, x86_64), as
127// well as the "common" architecture used for modules that support multiple architectures, for
128// example Java modules.
Colin Cross3f40fa42015-01-30 17:27:36 -0800129type ArchType struct {
Colin Crossa6845402020-11-16 15:08:19 -0800130 // Name is the name of the architecture type, "arm", "arm64", "x86", or "x86_64".
131 Name string
132
133 // Field is the name of the field used in properties that refer to the architecture, e.g. "Arm64".
134 Field string
135
136 // Multilib is either "lib32" or "lib64" for 32-bit or 64-bit architectures.
Colin Crossec193632015-07-06 17:49:43 -0700137 Multilib string
Colin Cross3f40fa42015-01-30 17:27:36 -0800138}
139
Colin Crossa6845402020-11-16 15:08:19 -0800140// String returns the name of the ArchType.
141func (a ArchType) String() string {
142 return a.Name
143}
144
145const COMMON_VARIANT = "common"
146
147var (
148 archTypeList []ArchType
149
150 Arm = newArch("arm", "lib32")
151 Arm64 = newArch("arm64", "lib64")
152 X86 = newArch("x86", "lib32")
153 X86_64 = newArch("x86_64", "lib64")
154
155 Common = ArchType{
156 Name: COMMON_VARIANT,
157 }
158)
159
160var archTypeMap = map[string]ArchType{}
161
Colin Crossec193632015-07-06 17:49:43 -0700162func newArch(name, multilib string) ArchType {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700163 archType := ArchType{
Colin Crossec193632015-07-06 17:49:43 -0700164 Name: name,
Dan Willemsenb1957a52016-06-23 23:44:54 -0700165 Field: proptools.FieldNameForProperty(name),
Colin Crossec193632015-07-06 17:49:43 -0700166 Multilib: multilib,
Colin Cross3f40fa42015-01-30 17:27:36 -0800167 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700168 archTypeList = append(archTypeList, archType)
Colin Crossa6845402020-11-16 15:08:19 -0800169 archTypeMap[name] = archType
Dan Willemsenb1957a52016-06-23 23:44:54 -0700170 return archType
Colin Cross3f40fa42015-01-30 17:27:36 -0800171}
172
Ustaeabf0f32021-12-06 15:17:23 -0500173// ArchTypeList returns a slice copy of the 4 supported ArchTypes for arm,
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000174// arm64, x86 and x86_64.
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -0700175func ArchTypeList() []ArchType {
176 return append([]ArchType(nil), archTypeList...)
177}
178
Colin Crossa6845402020-11-16 15:08:19 -0800179// MarshalText allows an ArchType to be serialized through any encoder that supports
180// encoding.TextMarshaler.
Colin Cross74ba9622019-02-11 15:11:14 -0800181func (a ArchType) MarshalText() ([]byte, error) {
Jeongik Chabec4d032021-04-15 08:55:38 +0900182 return []byte(a.String()), nil
Colin Cross74ba9622019-02-11 15:11:14 -0800183}
184
Colin Crossa6845402020-11-16 15:08:19 -0800185var _ encoding.TextMarshaler = ArchType{}
Colin Cross74ba9622019-02-11 15:11:14 -0800186
Colin Crossa6845402020-11-16 15:08:19 -0800187// UnmarshalText allows an ArchType to be deserialized through any decoder that supports
188// encoding.TextUnmarshaler.
Colin Cross74ba9622019-02-11 15:11:14 -0800189func (a *ArchType) UnmarshalText(text []byte) error {
190 if u, ok := archTypeMap[string(text)]; ok {
191 *a = u
192 return nil
193 }
194
195 return fmt.Errorf("unknown ArchType %q", text)
196}
197
Colin Crossa6845402020-11-16 15:08:19 -0800198var _ encoding.TextUnmarshaler = &ArchType{}
Colin Crossa1ad8d12016-06-01 17:09:44 -0700199
Colin Crossa6845402020-11-16 15:08:19 -0800200// OsClass is an enum that describes whether a variant of a module runs on the host, on the device,
201// or is generic.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700202type OsClass int
203
204const (
Colin Crossa6845402020-11-16 15:08:19 -0800205 // Generic is used for variants of modules that are not OS-specific.
Dan Willemsen0e2d97b2016-11-28 17:50:06 -0800206 Generic OsClass = iota
Colin Crossa6845402020-11-16 15:08:19 -0800207 // Device is used for variants of modules that run on the device.
Dan Willemsen0e2d97b2016-11-28 17:50:06 -0800208 Device
Colin Crossa6845402020-11-16 15:08:19 -0800209 // Host is used for variants of modules that run on the host.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700210 Host
Colin Crossa1ad8d12016-06-01 17:09:44 -0700211)
212
Colin Crossa6845402020-11-16 15:08:19 -0800213// String returns the OsClass as a string.
Colin Cross67a5c132017-05-09 13:45:28 -0700214func (class OsClass) String() string {
215 switch class {
216 case Generic:
217 return "generic"
218 case Device:
219 return "device"
220 case Host:
221 return "host"
Colin Cross67a5c132017-05-09 13:45:28 -0700222 default:
223 panic(fmt.Errorf("unknown class %d", class))
224 }
225}
226
Colin Crossa6845402020-11-16 15:08:19 -0800227// OsType describes an OS variant of a module.
228type OsType struct {
229 // Name is the name of the OS. It is also used as the name of the property in Android.bp
230 // files.
231 Name string
232
233 // Field is the name of the OS converted to an exported field name, i.e. with the first
234 // character capitalized.
235 Field string
236
237 // Class is the OsClass of the OS.
238 Class OsClass
239
240 // DefaultDisabled is set when the module variants for the OS should not be created unless
241 // the module explicitly requests them. This is used to limit Windows cross compilation to
242 // only modules that need it.
243 DefaultDisabled bool
244}
245
246// String returns the name of the OsType.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700247func (os OsType) String() string {
248 return os.Name
Colin Cross54c71122016-06-01 17:09:44 -0700249}
250
Colin Crossa6845402020-11-16 15:08:19 -0800251// Bionic returns true if the OS uses the Bionic libc runtime, i.e. if the OS is Android or
252// is Linux with Bionic.
Dan Willemsen866b5632017-09-22 12:28:24 -0700253func (os OsType) Bionic() bool {
254 return os == Android || os == LinuxBionic
255}
256
Colin Crossa6845402020-11-16 15:08:19 -0800257// Linux returns true if the OS uses the Linux kernel, i.e. if the OS is Android or is Linux
258// with or without the Bionic libc runtime.
Dan Willemsen866b5632017-09-22 12:28:24 -0700259func (os OsType) Linux() bool {
Colin Cross528d67e2021-07-23 22:23:07 +0000260 return os == Android || os == Linux || os == LinuxBionic || os == LinuxMusl
Dan Willemsen866b5632017-09-22 12:28:24 -0700261}
262
Colin Crossa6845402020-11-16 15:08:19 -0800263// newOsType constructs an OsType and adds it to the global lists.
264func newOsType(name string, class OsClass, defDisabled bool, archTypes ...ArchType) OsType {
265 checkCalledFromInit()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700266 os := OsType{
267 Name: name,
Colin Crossa6845402020-11-16 15:08:19 -0800268 Field: proptools.FieldNameForProperty(name),
Colin Crossa1ad8d12016-06-01 17:09:44 -0700269 Class: class,
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800270
271 DefaultDisabled: defDisabled,
Colin Cross54c71122016-06-01 17:09:44 -0700272 }
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000273 osTypeList = append(osTypeList, os)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800274
275 if _, found := commonTargetMap[name]; found {
276 panic(fmt.Errorf("Found Os type duplicate during OsType registration: %q", name))
277 } else {
Colin Crosse9fe2942020-11-10 18:12:15 -0800278 commonTargetMap[name] = Target{Os: os, Arch: CommonArch}
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800279 }
Colin Crossa6845402020-11-16 15:08:19 -0800280 osArchTypeMap[os] = archTypes
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800281
Colin Crossa1ad8d12016-06-01 17:09:44 -0700282 return os
283}
284
Colin Crossa6845402020-11-16 15:08:19 -0800285// osByName returns the OsType that has the given name, or NoOsType if none match.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700286func osByName(name string) OsType {
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000287 for _, os := range osTypeList {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700288 if os.Name == name {
289 return os
290 }
291 }
292
293 return NoOsType
Dan Willemsen490fd492015-11-24 17:53:15 -0800294}
295
Colin Crossa6845402020-11-16 15:08:19 -0800296var (
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000297 // osTypeList contains a list of all the supported OsTypes, including ones not supported
Colin Crossa6845402020-11-16 15:08:19 -0800298 // by the current build host or the target device.
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000299 osTypeList []OsType
Colin Crossa6845402020-11-16 15:08:19 -0800300 // commonTargetMap maps names of OsTypes to the corresponding common Target, i.e. the
301 // Target with the same OsType and the common ArchType.
302 commonTargetMap = make(map[string]Target)
303 // osArchTypeMap maps OsTypes to the list of supported ArchTypes for that OS.
304 osArchTypeMap = map[OsType][]ArchType{}
305
306 // NoOsType is a placeholder for when no OS is needed.
307 NoOsType OsType
308 // Linux is the OS for the Linux kernel plus the glibc runtime.
309 Linux = newOsType("linux_glibc", Host, false, X86, X86_64)
Colin Cross528d67e2021-07-23 22:23:07 +0000310 // LinuxMusl is the OS for the Linux kernel plus the musl runtime.
Colin Crossa9b2aac2022-06-15 17:25:51 -0700311 LinuxMusl = newOsType("linux_musl", Host, false, X86, X86_64, Arm64, Arm)
Colin Crossa6845402020-11-16 15:08:19 -0800312 // Darwin is the OS for MacOS/Darwin host machines.
Dan Willemsen8528f4e2021-10-19 00:22:06 -0700313 Darwin = newOsType("darwin", Host, false, Arm64, X86_64)
Colin Crossa6845402020-11-16 15:08:19 -0800314 // LinuxBionic is the OS for the Linux kernel plus the Bionic libc runtime, but without the
315 // rest of Android.
316 LinuxBionic = newOsType("linux_bionic", Host, false, Arm64, X86_64)
317 // Windows the OS for Windows host machines.
318 Windows = newOsType("windows", Host, true, X86, X86_64)
319 // Android is the OS for target devices that run all of Android, including the Linux kernel
320 // and the Bionic libc runtime.
321 Android = newOsType("android", Device, false, Arm, Arm64, X86, X86_64)
Colin Crossa6845402020-11-16 15:08:19 -0800322
323 // CommonOS is a pseudo OSType for a common OS variant, which is OsType agnostic and which
324 // has dependencies on all the OS variants.
325 CommonOS = newOsType("common_os", Generic, false)
Colin Crosse9fe2942020-11-10 18:12:15 -0800326
327 // CommonArch is the Arch for all modules that are os-specific but not arch specific,
328 // for example most Java modules.
329 CommonArch = Arch{ArchType: Common}
dimitry1f33e402019-03-26 12:39:31 +0100330)
331
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000332// OsTypeList returns a slice copy of the supported OsTypes.
333func OsTypeList() []OsType {
334 return append([]OsType(nil), osTypeList...)
335}
336
Colin Crossa6845402020-11-16 15:08:19 -0800337// Target specifies the OS and architecture that a module is being compiled for.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700338type Target struct {
Colin Crossa6845402020-11-16 15:08:19 -0800339 // Os the OS that the module is being compiled for (e.g. "linux_glibc", "android").
340 Os OsType
341 // Arch is the architecture that the module is being compiled for.
342 Arch Arch
343 // NativeBridge is NativeBridgeEnabled if the architecture is supported using NativeBridge
344 // (i.e. arm on x86) for this device.
345 NativeBridge NativeBridgeSupport
346 // NativeBridgeHostArchName is the name of the real architecture that is used to implement
347 // the NativeBridge architecture. For example, for arm on x86 this would be "x86".
dimitry8d6dde82019-07-11 10:23:53 +0200348 NativeBridgeHostArchName string
Colin Crossa6845402020-11-16 15:08:19 -0800349 // NativeBridgeRelativePath is the name of the subdirectory that will contain NativeBridge
350 // libraries and binaries.
dimitry8d6dde82019-07-11 10:23:53 +0200351 NativeBridgeRelativePath string
Jiyong Park1613e552020-09-14 19:43:17 +0900352
353 // HostCross is true when the target cannot run natively on the current build host.
354 // For example, linux_glibc_x86 returns true on a regular x86/i686/Linux machines, but returns false
355 // on Mac (different OS), or on 64-bit only i686/Linux machines (unsupported arch).
356 HostCross bool
Colin Crossd3ba0392015-05-07 14:11:29 -0700357}
358
Colin Crossa6845402020-11-16 15:08:19 -0800359// NativeBridgeSupport is an enum that specifies if a Target supports NativeBridge.
360type NativeBridgeSupport bool
361
362const (
363 NativeBridgeDisabled NativeBridgeSupport = false
364 NativeBridgeEnabled NativeBridgeSupport = true
365)
366
367// String returns the OS and arch variations used for the Target.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700368func (target Target) String() string {
Colin Crossa195f912019-10-16 11:07:20 -0700369 return target.OsVariation() + "_" + target.ArchVariation()
370}
371
Colin Crossa6845402020-11-16 15:08:19 -0800372// OsVariation returns the name of the variation used by the osMutator for the Target.
Colin Crossa195f912019-10-16 11:07:20 -0700373func (target Target) OsVariation() string {
374 return target.Os.String()
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700375}
376
Colin Crossa6845402020-11-16 15:08:19 -0800377// ArchVariation returns the name of the variation used by the archMutator for the Target.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700378func (target Target) ArchVariation() string {
379 var variation string
dimitry1f33e402019-03-26 12:39:31 +0100380 if target.NativeBridge {
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700381 variation = "native_bridge_"
dimitry1f33e402019-03-26 12:39:31 +0100382 }
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700383 variation += target.Arch.String()
384
Colin Crossa195f912019-10-16 11:07:20 -0700385 return variation
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700386}
387
Colin Crossa6845402020-11-16 15:08:19 -0800388// Variations returns a list of blueprint.Variations for the osMutator and archMutator for the
389// Target.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700390func (target Target) Variations() []blueprint.Variation {
391 return []blueprint.Variation{
Colin Crossa195f912019-10-16 11:07:20 -0700392 {Mutator: "os", Variation: target.OsVariation()},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700393 {Mutator: "arch", Variation: target.ArchVariation()},
394 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800395}
396
Colin Crossa6845402020-11-16 15:08:19 -0800397// osMutator splits an arch-specific module into a variant for each OS that is enabled for the
398// module. It uses the HostOrDevice value passed to InitAndroidArchModule and the
399// device_supported and host_supported properties to determine which OsTypes are enabled for this
400// module, then searches through the Targets to determine which have enabled Targets for this
401// module.
Colin Cross617b88a2020-08-24 18:04:09 -0700402func osMutator(bpctx blueprint.BottomUpMutatorContext) {
Colin Crossa195f912019-10-16 11:07:20 -0700403 var module Module
404 var ok bool
Colin Cross617b88a2020-08-24 18:04:09 -0700405 if module, ok = bpctx.Module().(Module); !ok {
Colin Crossa6845402020-11-16 15:08:19 -0800406 // The module is not a Soong module, it is a Blueprint module.
Colin Cross617b88a2020-08-24 18:04:09 -0700407 if bootstrap.IsBootstrapModule(bpctx.Module()) {
408 // Bootstrap Go modules are always the build OS or linux bionic.
409 config := bpctx.Config().(Config)
410 osNames := []string{config.BuildOSTarget.OsVariation()}
411 for _, hostCrossTarget := range config.Targets[LinuxBionic] {
412 if hostCrossTarget.Arch.ArchType == config.BuildOSTarget.Arch.ArchType {
413 osNames = append(osNames, hostCrossTarget.OsVariation())
414 }
415 }
416 osNames = FirstUniqueStrings(osNames)
417 bpctx.CreateVariations(osNames...)
418 }
Colin Crossa195f912019-10-16 11:07:20 -0700419 return
420 }
421
Colin Cross617b88a2020-08-24 18:04:09 -0700422 // Bootstrap Go module support above requires this mutator to be a
423 // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext
424 // filters out non-Soong modules. Now that we've handled them, create a
425 // normal android.BottomUpMutatorContext.
Liz Kammer356f7d42021-01-26 09:18:53 -0500426 mctx := bottomUpMutatorContextFactory(bpctx, module, false, false)
Colin Cross617b88a2020-08-24 18:04:09 -0700427
Colin Crossa195f912019-10-16 11:07:20 -0700428 base := module.base()
429
Colin Crossa6845402020-11-16 15:08:19 -0800430 // Nothing to do for modules that are not architecture specific (e.g. a genrule).
Colin Crossa195f912019-10-16 11:07:20 -0700431 if !base.ArchSpecific() {
432 return
433 }
434
Colin Crossa6845402020-11-16 15:08:19 -0800435 // Collect a list of OSTypes supported by this module based on the HostOrDevice value
436 // passed to InitAndroidArchModule and the device_supported and host_supported properties.
Colin Crossa195f912019-10-16 11:07:20 -0700437 var moduleOSList []OsType
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000438 for _, os := range osTypeList {
Jiyong Park1613e552020-09-14 19:43:17 +0900439 for _, t := range mctx.Config().Targets[os] {
Colin Cross08d6f8f2020-11-19 02:33:19 +0000440 if base.supportsTarget(t) {
Jiyong Park1613e552020-09-14 19:43:17 +0900441 moduleOSList = append(moduleOSList, os)
442 break
Colin Crossa195f912019-10-16 11:07:20 -0700443 }
444 }
Colin Crossa195f912019-10-16 11:07:20 -0700445 }
446
Colin Crossa6845402020-11-16 15:08:19 -0800447 // If there are no supported OSes then disable the module.
Colin Crossa195f912019-10-16 11:07:20 -0700448 if len(moduleOSList) == 0 {
Inseob Kimeec88e12020-01-22 11:11:29 +0900449 base.Disable()
Colin Crossa195f912019-10-16 11:07:20 -0700450 return
451 }
452
Colin Crossa6845402020-11-16 15:08:19 -0800453 // Convert the list of supported OsTypes to the variation names.
Colin Crossa195f912019-10-16 11:07:20 -0700454 osNames := make([]string, len(moduleOSList))
Colin Crossa195f912019-10-16 11:07:20 -0700455 for i, os := range moduleOSList {
456 osNames[i] = os.String()
457 }
458
Paul Duffin1356d8c2020-02-25 19:26:33 +0000459 createCommonOSVariant := base.commonProperties.CreateCommonOSVariant
460 if createCommonOSVariant {
Colin Crossa6845402020-11-16 15:08:19 -0800461 // A CommonOS variant was requested so add it to the list of OS variants to
Paul Duffin1356d8c2020-02-25 19:26:33 +0000462 // create. It needs to be added to the end because it needs to depend on the
463 // the other variants in the list returned by CreateVariations(...) and inter
464 // variant dependencies can only be created from a later variant in that list to
465 // an earlier one. That is because variants are always processed in the order in
466 // which they are returned from CreateVariations(...).
467 osNames = append(osNames, CommonOS.Name)
468 moduleOSList = append(moduleOSList, CommonOS)
Colin Crossa195f912019-10-16 11:07:20 -0700469 }
470
Colin Crossa6845402020-11-16 15:08:19 -0800471 // Create the variations, annotate each one with which OS it was created for, and
472 // squash the appropriate OS-specific properties into the top level properties.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000473 modules := mctx.CreateVariations(osNames...)
474 for i, m := range modules {
475 m.base().commonProperties.CompileOS = moduleOSList[i]
476 m.base().setOSProperties(mctx)
477 }
478
479 if createCommonOSVariant {
480 // A CommonOS variant was requested so add dependencies from it (the last one in
481 // the list) to the OS type specific variants.
482 last := len(modules) - 1
483 commonOSVariant := modules[last]
484 commonOSVariant.base().commonProperties.CommonOSVariant = true
485 for _, module := range modules[0:last] {
486 // Ignore modules that are enabled. Note, this will only avoid adding
487 // dependencies on OsType variants that are explicitly disabled in their
488 // properties. The CommonOS variant will still depend on disabled variants
489 // if they are disabled afterwards, e.g. in archMutator if
490 if module.Enabled() {
491 mctx.AddInterVariantDependency(commonOsToOsSpecificVariantTag, commonOSVariant, module)
492 }
493 }
494 }
495}
496
Colin Crossc179ea62020-10-09 10:54:15 -0700497type archDepTag struct {
498 blueprint.BaseDependencyTag
499 name string
500}
Paul Duffin1356d8c2020-02-25 19:26:33 +0000501
Colin Crossc179ea62020-10-09 10:54:15 -0700502// Identifies the dependency from CommonOS variant to the os specific variants.
503var commonOsToOsSpecificVariantTag = archDepTag{name: "common os to os specific"}
504
Paul Duffin1356d8c2020-02-25 19:26:33 +0000505// Get the OsType specific variants for the current CommonOS variant.
506//
507// The returned list will only contain enabled OsType specific variants of the
508// module referenced in the supplied context. An empty list is returned if there
509// are no enabled variants or the supplied context is not for an CommonOS
510// variant.
511func GetOsSpecificVariantsOfCommonOSVariant(mctx BaseModuleContext) []Module {
512 var variants []Module
513 mctx.VisitDirectDeps(func(m Module) {
514 if mctx.OtherModuleDependencyTag(m) == commonOsToOsSpecificVariantTag {
515 if m.Enabled() {
516 variants = append(variants, m)
517 }
518 }
519 })
Paul Duffin1356d8c2020-02-25 19:26:33 +0000520 return variants
Colin Crossa195f912019-10-16 11:07:20 -0700521}
522
Dan Willemsen47450072021-10-19 20:24:49 -0700523var DarwinUniversalVariantTag = archDepTag{name: "darwin universal binary"}
524
Colin Crossee0bc3b2018-10-02 22:01:37 -0700525// archMutator splits a module into a variant for each Target requested by the module. Target selection
Colin Crossa6845402020-11-16 15:08:19 -0800526// for a module is in three levels, OsClass, multilib, and then Target.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700527// OsClass selection is determined by:
Colin Crossd079e0b2022-08-16 10:27:33 -0700528// - The HostOrDeviceSupported value passed in to InitAndroidArchModule by the module type factory, which selects
529// whether the module type can compile for host, device or both.
530// - The host_supported and device_supported properties on the module.
531//
Roland Levillainf5b635d2019-06-05 14:42:57 +0100532// 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 -0700533// for the module, the Device OsClass is selected.
534// Within each selected OsClass, the multilib selection is determined by:
Colin Crossd079e0b2022-08-16 10:27:33 -0700535// - The compile_multilib property if it set (which may be overridden by target.android.compile_multilib or
536// target.host.compile_multilib).
537// - The default multilib passed to InitAndroidArchModule if compile_multilib was not set.
538//
Colin Crossee0bc3b2018-10-02 22:01:37 -0700539// Valid multilib values include:
Colin Crossd079e0b2022-08-16 10:27:33 -0700540//
541// "both": compile for all Targets supported by the OsClass (generally x86_64 and x86, or arm64 and arm).
542// "first": compile for only a single preferred Target supported by the OsClass. This is generally x86_64 or arm64,
543// but may be arm for a 32-bit only build.
544// "32": compile for only a single 32-bit Target supported by the OsClass.
545// "64": compile for only a single 64-bit Target supported by the OsClass.
546// "common": compile a for a single Target that will work on all Targets supported by the OsClass (for example Java).
547// "common_first": compile a for a Target that will work on all Targets supported by the OsClass
548// (same as "common"), plus a second Target for the preferred Target supported by the OsClass
549// (same as "first"). This is used for java_binary that produces a common .jar and a wrapper
550// executable script.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700551//
552// Once the list of Targets is determined, the module is split into a variant for each Target.
553//
554// Modules can be initialized with InitAndroidMultiTargetsArchModule, in which case they will be split by OsClass,
555// 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 -0700556func archMutator(bpctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700557 var module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800558 var ok bool
Colin Cross617b88a2020-08-24 18:04:09 -0700559 if module, ok = bpctx.Module().(Module); !ok {
560 if bootstrap.IsBootstrapModule(bpctx.Module()) {
561 // Bootstrap Go modules are always the build architecture.
562 bpctx.CreateVariations(bpctx.Config().(Config).BuildOSTarget.ArchVariation())
563 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800564 return
565 }
566
Colin Cross617b88a2020-08-24 18:04:09 -0700567 // Bootstrap Go module support above requires this mutator to be a
568 // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext
569 // filters out non-Soong modules. Now that we've handled them, create a
570 // normal android.BottomUpMutatorContext.
Liz Kammer356f7d42021-01-26 09:18:53 -0500571 mctx := bottomUpMutatorContextFactory(bpctx, module, false, false)
Colin Cross617b88a2020-08-24 18:04:09 -0700572
Colin Cross5eca7cb2018-10-02 14:02:10 -0700573 base := module.base()
574
575 if !base.ArchSpecific() {
Colin Crossb9db4802016-06-03 01:50:47 +0000576 return
577 }
578
Colin Crossa195f912019-10-16 11:07:20 -0700579 os := base.commonProperties.CompileOS
Paul Duffin1356d8c2020-02-25 19:26:33 +0000580 if os == CommonOS {
581 // Make sure that the target related properties are initialized for the
582 // CommonOS variant.
583 addTargetProperties(module, commonTargetMap[os.Name], nil, true)
584
585 // Do not create arch specific variants for the CommonOS variant.
586 return
587 }
588
Colin Crossa195f912019-10-16 11:07:20 -0700589 osTargets := mctx.Config().Targets[os]
Colin Crossfb0c16e2019-11-20 17:12:35 -0800590 image := base.commonProperties.ImageVariation
Colin Crossa6845402020-11-16 15:08:19 -0800591 // Filter NativeBridge targets unless they are explicitly supported.
592 // Skip creating native bridge variants for non-core modules.
Paul Duffine3d1ae42021-09-03 17:47:17 +0100593 if os == Android && !(base.IsNativeBridgeSupported() && image == CoreVariation) {
Colin Cross83bead42019-12-18 10:45:46 -0800594
Colin Crossa195f912019-10-16 11:07:20 -0700595 var targets []Target
596 for _, t := range osTargets {
597 if !t.NativeBridge {
598 targets = append(targets, t)
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700599 }
600 }
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700601
Colin Crossa195f912019-10-16 11:07:20 -0700602 osTargets = targets
603 }
Colin Crossee0bc3b2018-10-02 22:01:37 -0700604
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700605 // only the primary arch in the ramdisk / vendor_ramdisk / recovery partition
Inseob Kim08758f02021-04-08 21:13:22 +0900606 if os == Android && (module.InstallInRecovery() || module.InstallInRamdisk() || module.InstallInVendorRamdisk() || module.InstallInDebugRamdisk()) {
Colin Crossa195f912019-10-16 11:07:20 -0700607 osTargets = []Target{osTargets[0]}
608 }
dimitry1f33e402019-03-26 12:39:31 +0100609
Jaewoong Jung003d8082021-02-24 17:39:54 -0800610 // Windows builds always prefer 32-bit
611 prefer32 := os == Windows
dimitry1f33e402019-03-26 12:39:31 +0100612
Colin Crossa6845402020-11-16 15:08:19 -0800613 // Determine the multilib selection for this module.
Christopher Ferris98f10222022-07-13 23:16:52 -0700614 ignorePrefer32OnDevice := mctx.Config().IgnorePrefer32OnDevice()
615 multilib, extraMultilib := decodeMultilib(base, os, ignorePrefer32OnDevice)
Colin Crossa6845402020-11-16 15:08:19 -0800616
617 // Convert the multilib selection into a list of Targets.
Colin Crossa195f912019-10-16 11:07:20 -0700618 targets, err := decodeMultilibTargets(multilib, osTargets, prefer32)
619 if err != nil {
620 mctx.ModuleErrorf("%s", err.Error())
621 }
Colin Cross5eca7cb2018-10-02 14:02:10 -0700622
Colin Crossc0f0eb82022-07-19 14:41:11 -0700623 // If there are no supported targets disable the module.
624 if len(targets) == 0 {
625 base.Disable()
626 return
627 }
628
Colin Crossa6845402020-11-16 15:08:19 -0800629 // If the module is using extraMultilib, decode the extraMultilib selection into
630 // a separate list of Targets.
Colin Crossa195f912019-10-16 11:07:20 -0700631 var multiTargets []Target
632 if extraMultilib != "" {
633 multiTargets, err = decodeMultilibTargets(extraMultilib, osTargets, prefer32)
Colin Crossa1ad8d12016-06-01 17:09:44 -0700634 if err != nil {
635 mctx.ModuleErrorf("%s", err.Error())
636 }
Colin Crossc0f0eb82022-07-19 14:41:11 -0700637 multiTargets = filterHostCross(multiTargets, targets[0].HostCross)
Colin Crossb9db4802016-06-03 01:50:47 +0000638 }
639
Colin Crossa6845402020-11-16 15:08:19 -0800640 // Recovery is always the primary architecture, filter out any other architectures.
Inseob Kim20fb5d42021-02-02 20:07:58 +0900641 // Common arch is also allowed
Colin Crossfb0c16e2019-11-20 17:12:35 -0800642 if image == RecoveryVariation {
643 primaryArch := mctx.Config().DevicePrimaryArchType()
Inseob Kim20fb5d42021-02-02 20:07:58 +0900644 targets = filterToArch(targets, primaryArch, Common)
645 multiTargets = filterToArch(multiTargets, primaryArch, Common)
Colin Crossfb0c16e2019-11-20 17:12:35 -0800646 }
647
Colin Crossa6845402020-11-16 15:08:19 -0800648 // If there are no supported targets disable the module.
Colin Crossa195f912019-10-16 11:07:20 -0700649 if len(targets) == 0 {
Inseob Kimeec88e12020-01-22 11:11:29 +0900650 base.Disable()
Dan Willemsen3f32f032016-07-11 14:36:48 -0700651 return
652 }
653
Colin Crossa6845402020-11-16 15:08:19 -0800654 // Convert the targets into a list of arch variation names.
Colin Crossa195f912019-10-16 11:07:20 -0700655 targetNames := make([]string, len(targets))
Colin Crossa195f912019-10-16 11:07:20 -0700656 for i, target := range targets {
657 targetNames[i] = target.ArchVariation()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700658 }
659
Colin Crossa6845402020-11-16 15:08:19 -0800660 // Create the variations, annotate each one with which Target it was created for, and
661 // squash the appropriate arch-specific properties into the top level properties.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700662 modules := mctx.CreateVariations(targetNames...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800663 for i, m := range modules {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000664 addTargetProperties(m, targets[i], multiTargets, i == 0)
Colin Cross617b88a2020-08-24 18:04:09 -0700665 m.base().setArchProperties(mctx)
Dan Willemsen8528f4e2021-10-19 00:22:06 -0700666
667 // Install support doesn't understand Darwin+Arm64
668 if os == Darwin && targets[i].HostCross {
669 m.base().commonProperties.SkipInstall = true
670 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800671 }
Dan Willemsen47450072021-10-19 20:24:49 -0700672
673 // Create a dependency for Darwin Universal binaries from the primary to secondary
674 // architecture. The module itself will be responsible for calling lipo to merge the outputs.
675 if os == Darwin {
676 if multilib == "darwin_universal" && len(modules) == 2 {
677 mctx.AddInterVariantDependency(DarwinUniversalVariantTag, modules[1], modules[0])
678 } else if multilib == "darwin_universal_common_first" && len(modules) == 3 {
679 mctx.AddInterVariantDependency(DarwinUniversalVariantTag, modules[2], modules[1])
680 }
681 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800682}
683
Colin Crossa6845402020-11-16 15:08:19 -0800684// addTargetProperties annotates a variant with the Target is is being compiled for, the list
685// of additional Targets it is supporting (if any), and whether it is the primary Target for
686// the module.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000687func addTargetProperties(m Module, target Target, multiTargets []Target, primaryTarget bool) {
688 m.base().commonProperties.CompileTarget = target
689 m.base().commonProperties.CompileMultiTargets = multiTargets
690 m.base().commonProperties.CompilePrimary = primaryTarget
691}
692
Colin Crossa6845402020-11-16 15:08:19 -0800693// decodeMultilib returns the appropriate compile_multilib property for the module, or the default
694// multilib from the factory's call to InitAndroidArchModule if none was set. For modules that
695// called InitAndroidMultiTargetsArchModule it always returns "common" for multilib, and returns
696// the actual multilib in extraMultilib.
Christopher Ferris98f10222022-07-13 23:16:52 -0700697func decodeMultilib(base *ModuleBase, os OsType, ignorePrefer32OnDevice bool) (multilib, extraMultilib string) {
Colin Crossa6845402020-11-16 15:08:19 -0800698 // First check the "android.compile_multilib" or "host.compile_multilib" properties.
Dan Willemsen47450072021-10-19 20:24:49 -0700699 switch os.Class {
Colin Crossee0bc3b2018-10-02 22:01:37 -0700700 case Device:
701 multilib = String(base.commonProperties.Target.Android.Compile_multilib)
Jiyong Park1613e552020-09-14 19:43:17 +0900702 case Host:
Colin Crossee0bc3b2018-10-02 22:01:37 -0700703 multilib = String(base.commonProperties.Target.Host.Compile_multilib)
704 }
Colin Crossa6845402020-11-16 15:08:19 -0800705
706 // If those aren't set, try the "compile_multilib" property.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700707 if multilib == "" {
708 multilib = String(base.commonProperties.Compile_multilib)
709 }
Colin Crossa6845402020-11-16 15:08:19 -0800710
711 // If that wasn't set, use the default multilib set by the factory.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700712 if multilib == "" {
713 multilib = base.commonProperties.Default_multilib
714 }
715
Christopher Ferris98f10222022-07-13 23:16:52 -0700716 // If a device is configured with multiple targets, this option
717 // force all device targets that prefer32 to be compiled only as
718 // the first target.
719 if ignorePrefer32OnDevice && os.Class == Device && (multilib == "prefer32" || multilib == "first_prefer32") {
720 multilib = "first"
721 }
722
Colin Crossee0bc3b2018-10-02 22:01:37 -0700723 if base.commonProperties.UseTargetVariants {
Dan Willemsen47450072021-10-19 20:24:49 -0700724 // Darwin has the concept of "universal binaries" which is implemented in Soong by
725 // building both x86_64 and arm64 variants, and having select module types know how to
726 // merge the outputs of their corresponding variants together into a final binary. Most
727 // module types don't need to understand this logic, as we only build a small portion
728 // of the tree for Darwin, and only module types writing macho files need to do the
729 // merging.
730 //
731 // This logic is not enabled for:
732 // "common", as it's not an arch-specific variant
733 // "32", as Darwin never has a 32-bit variant
734 // !UseTargetVariants, as the module has opted into handling the arch-specific logic on
735 // its own.
736 if os == Darwin && multilib != "common" && multilib != "32" {
737 if multilib == "common_first" {
738 multilib = "darwin_universal_common_first"
739 } else {
740 multilib = "darwin_universal"
741 }
742 }
743
Colin Crossee0bc3b2018-10-02 22:01:37 -0700744 return multilib, ""
745 } else {
746 // For app modules a single arch variant will be created per OS class which is expected to handle all the
747 // selected arches. Return the common-type as multilib and any Android.bp provided multilib as extraMultilib
748 if multilib == base.commonProperties.Default_multilib {
749 multilib = "first"
750 }
751 return base.commonProperties.Default_multilib, multilib
752 }
753}
754
Colin Crossa6845402020-11-16 15:08:19 -0800755// filterToArch takes a list of Targets and an ArchType, and returns a modified list that contains
Inseob Kim20fb5d42021-02-02 20:07:58 +0900756// only Targets that have the specified ArchTypes.
757func filterToArch(targets []Target, archs ...ArchType) []Target {
Colin Crossfb0c16e2019-11-20 17:12:35 -0800758 for i := 0; i < len(targets); i++ {
Inseob Kim20fb5d42021-02-02 20:07:58 +0900759 found := false
760 for _, arch := range archs {
761 if targets[i].Arch.ArchType == arch {
762 found = true
763 break
764 }
765 }
766 if !found {
Colin Crossfb0c16e2019-11-20 17:12:35 -0800767 targets = append(targets[:i], targets[i+1:]...)
768 i--
769 }
770 }
771 return targets
772}
773
Colin Crossc0f0eb82022-07-19 14:41:11 -0700774// filterHostCross takes a list of Targets and a hostCross value, and returns a modified list
775// that contains only Targets that have the specified HostCross.
776func filterHostCross(targets []Target, hostCross bool) []Target {
777 for i := 0; i < len(targets); i++ {
778 if targets[i].HostCross != hostCross {
779 targets = append(targets[:i], targets[i+1:]...)
780 i--
781 }
782 }
783 return targets
784}
785
Colin Crossa6845402020-11-16 15:08:19 -0800786// archPropRoot is a struct type used as the top level of the arch-specific properties. It
787// contains the "arch", "multilib", and "target" property structs. It is used to split up the
788// property structs to limit how much is allocated when a single arch-specific property group is
789// used. The types are interface{} because they will hold instances of runtime-created types.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800790type archPropRoot struct {
791 Arch, Multilib, Target interface{}
792}
793
Colin Crossa6845402020-11-16 15:08:19 -0800794// archPropTypeDesc holds the runtime-created types for the property structs to instantiate to
795// create an archPropRoot property struct.
796type archPropTypeDesc struct {
797 arch, multilib, target reflect.Type
798}
799
Colin Crosscbbd13f2020-01-17 14:08:22 -0800800// createArchPropTypeDesc takes a reflect.Type that is either a struct or a pointer to a struct, and
801// returns lists of reflect.Types that contains the arch-variant properties inside structs for each
802// arch, multilib and target property.
Colin Crossa6845402020-11-16 15:08:19 -0800803//
804// This is a relatively expensive operation, so the results are cached in the global
805// archPropTypeMap. It is constructed entirely based on compile-time data, so there is no need
806// to isolate the results between multiple tests running in parallel.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800807func createArchPropTypeDesc(props reflect.Type) []archPropTypeDesc {
Colin Crossb1d8c992020-01-21 11:43:29 -0800808 // Each property struct shard will be nested many times under the runtime generated arch struct,
809 // which can hit the limit of 64kB for the name of runtime generated structs. They are nested
810 // 97 times now, which may grow in the future, plus there is some overhead for the containing
811 // type. This number may need to be reduced if too many are added, but reducing it too far
812 // could cause problems if a single deeply nested property no longer fits in the name.
813 const maxArchTypeNameSize = 500
814
Colin Crossa6845402020-11-16 15:08:19 -0800815 // Convert the type to a new set of types that contains only the arch-specific properties
Usta Shrestha0b52d832022-02-04 21:37:39 -0500816 // (those that are tagged with `android:"arch_variant"`), and sharded into multiple types
Colin Crossa6845402020-11-16 15:08:19 -0800817 // to keep the runtime-generated names under the limit.
Colin Crossb1d8c992020-01-21 11:43:29 -0800818 propShards, _ := proptools.FilterPropertyStructSharded(props, maxArchTypeNameSize, filterArchStruct)
Colin Crossa6845402020-11-16 15:08:19 -0800819
820 // If the type has no arch-specific properties there is nothing to do.
Colin Crosscb988072019-01-24 14:58:11 -0800821 if len(propShards) == 0 {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700822 return nil
823 }
824
Colin Crosscbbd13f2020-01-17 14:08:22 -0800825 var ret []archPropTypeDesc
Colin Crossc17727d2018-10-24 12:42:09 -0700826 for _, props := range propShards {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700827
Colin Crossa6845402020-11-16 15:08:19 -0800828 // variantFields takes a list of variant property field names and returns a list the
829 // StructFields with the names and the type of the current shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700830 variantFields := func(names []string) []reflect.StructField {
831 ret := make([]reflect.StructField, len(names))
Dan Willemsenb1957a52016-06-23 23:44:54 -0700832
Colin Crossc17727d2018-10-24 12:42:09 -0700833 for i, name := range names {
834 ret[i].Name = name
835 ret[i].Type = props
Dan Willemsen866b5632017-09-22 12:28:24 -0700836 }
Colin Crossc17727d2018-10-24 12:42:09 -0700837
838 return ret
839 }
840
Colin Crossa6845402020-11-16 15:08:19 -0800841 // Create a type that contains the properties in this shard repeated for each
842 // architecture, architecture variant, and architecture feature.
Colin Crossc17727d2018-10-24 12:42:09 -0700843 archFields := make([]reflect.StructField, len(archTypeList))
844 for i, arch := range archTypeList {
Colin Crossa6845402020-11-16 15:08:19 -0800845 var variants []string
Colin Crossc17727d2018-10-24 12:42:09 -0700846
847 for _, archVariant := range archVariants[arch] {
848 archVariant := variantReplacer.Replace(archVariant)
849 variants = append(variants, proptools.FieldNameForProperty(archVariant))
850 }
Liz Kammer2c2afe22022-02-11 11:35:03 -0500851 for _, cpuVariant := range cpuVariants[arch] {
852 cpuVariant := variantReplacer.Replace(cpuVariant)
853 variants = append(variants, proptools.FieldNameForProperty(cpuVariant))
854 }
Colin Crossc17727d2018-10-24 12:42:09 -0700855 for _, feature := range archFeatures[arch] {
856 feature := variantReplacer.Replace(feature)
857 variants = append(variants, proptools.FieldNameForProperty(feature))
858 }
859
Colin Crossa6845402020-11-16 15:08:19 -0800860 // Create the StructFields for each architecture variant architecture feature
861 // (e.g. "arch.arm.cortex-a53" or "arch.arm.neon").
Colin Crossc17727d2018-10-24 12:42:09 -0700862 fields := variantFields(variants)
863
Colin Crossa6845402020-11-16 15:08:19 -0800864 // Create the StructField for the architecture itself (e.g. "arch.arm"). The special
865 // "BlueprintEmbed" name is used by Blueprint to put the properties in the
866 // parent struct.
Colin Crossc17727d2018-10-24 12:42:09 -0700867 fields = append([]reflect.StructField{{
868 Name: "BlueprintEmbed",
869 Type: props,
870 Anonymous: true,
871 }}, fields...)
872
873 archFields[i] = reflect.StructField{
874 Name: arch.Field,
875 Type: reflect.StructOf(fields),
876 }
877 }
Colin Crossa6845402020-11-16 15:08:19 -0800878
879 // Create the type of the "arch" property struct for this shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700880 archType := reflect.StructOf(archFields)
881
Colin Crossa6845402020-11-16 15:08:19 -0800882 // Create the type for the "multilib" property struct for this shard, containing the
883 // "multilib.lib32" and "multilib.lib64" property structs.
Colin Crossc17727d2018-10-24 12:42:09 -0700884 multilibType := reflect.StructOf(variantFields([]string{"Lib32", "Lib64"}))
885
Colin Crossa6845402020-11-16 15:08:19 -0800886 // Start with a list of the special targets
Colin Crossc17727d2018-10-24 12:42:09 -0700887 targets := []string{
888 "Host",
889 "Android64",
890 "Android32",
891 "Bionic",
Colin Cross528d67e2021-07-23 22:23:07 +0000892 "Glibc",
893 "Musl",
Colin Crossc17727d2018-10-24 12:42:09 -0700894 "Linux",
Colin Crossa98d36d2022-03-07 14:39:49 -0800895 "Host_linux",
Colin Crossc17727d2018-10-24 12:42:09 -0700896 "Not_windows",
897 "Arm_on_x86",
898 "Arm_on_x86_64",
Victor Khimenkoc26fcf42020-05-07 22:16:33 +0200899 "Native_bridge",
Colin Crossc17727d2018-10-24 12:42:09 -0700900 }
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000901 for _, os := range osTypeList {
Colin Crossa6845402020-11-16 15:08:19 -0800902 // Add all the OSes.
Colin Crossc17727d2018-10-24 12:42:09 -0700903 targets = append(targets, os.Field)
904
Colin Crossa6845402020-11-16 15:08:19 -0800905 // Add the OS/Arch combinations, e.g. "android_arm64".
Colin Crossc17727d2018-10-24 12:42:09 -0700906 for _, archType := range osArchTypeMap[os] {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400907 targets = append(targets, GetCompoundTargetField(os, archType))
Colin Crossc17727d2018-10-24 12:42:09 -0700908
Colin Cross1aa45b02022-02-10 10:33:10 -0800909 // Also add the special "linux_<arch>", "bionic_<arch>" , "glibc_<arch>", and
910 // "musl_<arch>" property structs.
Colin Crossc17727d2018-10-24 12:42:09 -0700911 if os.Linux() {
912 target := "Linux_" + archType.Name
913 if !InList(target, targets) {
914 targets = append(targets, target)
915 }
916 }
Colin Crossa98d36d2022-03-07 14:39:49 -0800917 if os.Linux() && os.Class == Host {
918 target := "Host_linux_" + archType.Name
919 if !InList(target, targets) {
920 targets = append(targets, target)
921 }
922 }
Colin Crossc17727d2018-10-24 12:42:09 -0700923 if os.Bionic() {
924 target := "Bionic_" + archType.Name
925 if !InList(target, targets) {
926 targets = append(targets, target)
927 }
Dan Willemsen866b5632017-09-22 12:28:24 -0700928 }
Colin Cross1aa45b02022-02-10 10:33:10 -0800929 if os == Linux {
930 target := "Glibc_" + archType.Name
931 if !InList(target, targets) {
932 targets = append(targets, target)
933 }
934 }
935 if os == LinuxMusl {
936 target := "Musl_" + archType.Name
937 if !InList(target, targets) {
938 targets = append(targets, target)
939 }
940 }
Dan Willemsen866b5632017-09-22 12:28:24 -0700941 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700942 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700943
Colin Crossa6845402020-11-16 15:08:19 -0800944 // Create the type for the "target" property struct for this shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700945 targetType := reflect.StructOf(variantFields(targets))
Colin Crosscbbd13f2020-01-17 14:08:22 -0800946
Colin Crossa6845402020-11-16 15:08:19 -0800947 // Return a descriptor of the 3 runtime-created types.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800948 ret = append(ret, archPropTypeDesc{
949 arch: reflect.PtrTo(archType),
950 multilib: reflect.PtrTo(multilibType),
951 target: reflect.PtrTo(targetType),
952 })
Colin Crossc17727d2018-10-24 12:42:09 -0700953 }
954 return ret
Dan Willemsenb1957a52016-06-23 23:44:54 -0700955}
956
Colin Crossa6845402020-11-16 15:08:19 -0800957// variantReplacer converts architecture variant or architecture feature names into names that
958// are valid for an Android.bp file.
959var variantReplacer = strings.NewReplacer("-", "_", ".", "_")
960
961// filterArchStruct returns true if the given field is an architecture specific property.
Colin Cross74449102019-09-25 11:26:40 -0700962func filterArchStruct(field reflect.StructField, prefix string) (bool, reflect.StructField) {
963 if proptools.HasTag(field, "android", "arch_variant") {
964 // The arch_variant field isn't necessary past this point
965 // Instead of wasting space, just remove it. Go also has a
966 // 16-bit limit on structure name length. The name is constructed
967 // based on the Go source representation of the structure, so
968 // the tag names count towards that length.
Colin Crossb4fecbf2020-01-21 11:38:47 -0800969
970 androidTag := field.Tag.Get("android")
971 values := strings.Split(androidTag, ",")
972
973 if string(field.Tag) != `android:"`+strings.Join(values, ",")+`"` {
974 panic(fmt.Errorf("unexpected tag format %q", field.Tag))
Colin Cross74449102019-09-25 11:26:40 -0700975 }
Colin Crossb4fecbf2020-01-21 11:38:47 -0800976 // these tags don't need to be present in the runtime generated struct type.
Liz Kammerff966b12022-07-29 10:49:16 -0400977 values = RemoveListFromList(values, []string{"arch_variant", "variant_prepend", "path"})
978 if len(values) > 0 {
Colin Crossb4fecbf2020-01-21 11:38:47 -0800979 panic(fmt.Errorf("unknown tags %q in field %q", values, prefix+field.Name))
980 }
981
Liz Kammerff966b12022-07-29 10:49:16 -0400982 field.Tag = ``
Colin Cross74449102019-09-25 11:26:40 -0700983 return true, field
984 }
985 return false, field
986}
987
Colin Crossa6845402020-11-16 15:08:19 -0800988// archPropTypeMap contains a cache of the results of createArchPropTypeDesc for each type. It is
989// shared across all Contexts, but is constructed based only on compile-time information so there
990// is no risk of contaminating one Context with data from another.
Dan Willemsenb1957a52016-06-23 23:44:54 -0700991var archPropTypeMap OncePer
992
Colin Crossa6845402020-11-16 15:08:19 -0800993// initArchModule adds the architecture-specific property structs to a Module.
994func initArchModule(m Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800995
996 base := m.base()
997
Ustaeabf0f32021-12-06 15:17:23 -0500998 if len(base.archProperties) != 0 {
999 panic(fmt.Errorf("module %s already has archProperties", m.Name()))
1000 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001001
Ustaeabf0f32021-12-06 15:17:23 -05001002 getStructType := func(properties interface{}) reflect.Type {
Colin Cross3f40fa42015-01-30 17:27:36 -08001003 propertiesValue := reflect.ValueOf(properties)
Colin Cross62496a02016-08-08 15:49:17 -07001004 t := propertiesValue.Type()
Colin Cross3f40fa42015-01-30 17:27:36 -08001005 if propertiesValue.Kind() != reflect.Ptr {
Colin Crossca860ac2016-01-04 14:34:37 -08001006 panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
1007 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -08001008 }
1009
1010 propertiesValue = propertiesValue.Elem()
1011 if propertiesValue.Kind() != reflect.Struct {
Ustaeabf0f32021-12-06 15:17:23 -05001012 panic(fmt.Errorf("properties must be a pointer to a struct, got a pointer to %T",
Colin Crossca860ac2016-01-04 14:34:37 -08001013 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -08001014 }
Ustaeabf0f32021-12-06 15:17:23 -05001015 return t
1016 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001017
Usta851a3272022-01-05 23:42:33 -05001018 for _, properties := range m.GetProperties() {
Ustaeabf0f32021-12-06 15:17:23 -05001019 t := getStructType(properties)
Colin Crossa6845402020-11-16 15:08:19 -08001020 // Get or create the arch-specific property struct types for this property struct type.
Colin Cross571cccf2019-02-04 11:22:08 -08001021 archPropTypes := archPropTypeMap.Once(NewCustomOnceKey(t), func() interface{} {
Colin Crosscbbd13f2020-01-17 14:08:22 -08001022 return createArchPropTypeDesc(t)
1023 }).([]archPropTypeDesc)
Colin Cross3f40fa42015-01-30 17:27:36 -08001024
Colin Crossa6845402020-11-16 15:08:19 -08001025 // Instantiate one of each arch-specific property struct type and add it to the
1026 // properties for the Module.
Colin Crossc17727d2018-10-24 12:42:09 -07001027 var archProperties []interface{}
1028 for _, t := range archPropTypes {
Colin Crosscbbd13f2020-01-17 14:08:22 -08001029 archProperties = append(archProperties, &archPropRoot{
1030 Arch: reflect.Zero(t.arch).Interface(),
1031 Multilib: reflect.Zero(t.multilib).Interface(),
1032 Target: reflect.Zero(t.target).Interface(),
1033 })
Dan Willemsenb1957a52016-06-23 23:44:54 -07001034 }
Colin Crossc17727d2018-10-24 12:42:09 -07001035 base.archProperties = append(base.archProperties, archProperties)
1036 m.AddProperties(archProperties...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001037 }
1038
Colin Cross3f40fa42015-01-30 17:27:36 -08001039}
1040
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001041func maybeBlueprintEmbed(src reflect.Value) reflect.Value {
Colin Crossa6845402020-11-16 15:08:19 -08001042 // If the value of the field is a struct (as opposed to a pointer to a struct) then step
1043 // into the BlueprintEmbed field.
Dan Willemsenb1957a52016-06-23 23:44:54 -07001044 if src.Kind() == reflect.Struct {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001045 return src.FieldByName("BlueprintEmbed")
1046 } else {
1047 return src
Colin Cross06a931b2015-10-28 17:23:31 -07001048 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001049}
1050
1051// Merges the property struct in srcValue into dst.
Liz Kammerb6dbc872021-05-14 15:14:40 -04001052func mergePropertyStruct(ctx ArchVariantContext, dst interface{}, srcValue reflect.Value) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001053 src := maybeBlueprintEmbed(srcValue).Interface()
Colin Cross06a931b2015-10-28 17:23:31 -07001054
Colin Crossa6845402020-11-16 15:08:19 -08001055 // order checks the `android:"variant_prepend"` tag to handle properties where the
1056 // arch-specific value needs to come before the generic value, for example for lists of
1057 // include directories.
Colin Cross6ee75b62016-05-05 15:57:15 -07001058 order := func(property string,
1059 dstField, srcField reflect.StructField,
1060 dstValue, srcValue interface{}) (proptools.Order, error) {
1061 if proptools.HasTag(dstField, "android", "variant_prepend") {
1062 return proptools.Prepend, nil
1063 } else {
1064 return proptools.Append, nil
1065 }
1066 }
1067
Colin Crossa6845402020-11-16 15:08:19 -08001068 // Squash the located property struct into the destination property struct.
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001069 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src, nil, order)
Colin Cross06a931b2015-10-28 17:23:31 -07001070 if err != nil {
1071 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
1072 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
1073 } else {
1074 panic(err)
1075 }
1076 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001077}
Colin Cross85a88972015-11-23 13:29:51 -08001078
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001079// Returns the immediate child of the input property struct that corresponds to
1080// the sub-property "field".
Liz Kammerb6dbc872021-05-14 15:14:40 -04001081func getChildPropertyStruct(ctx ArchVariantContext,
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001082 src reflect.Value, field, userFriendlyField string) (reflect.Value, bool) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001083
1084 // Step into non-nil pointers to structs in the src value.
1085 if src.Kind() == reflect.Ptr {
1086 if src.IsNil() {
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001087 return reflect.Value{}, false
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001088 }
1089 src = src.Elem()
1090 }
1091
1092 // Find the requested field in the src struct.
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001093 child := src.FieldByName(proptools.FieldNameForProperty(field))
1094 if !child.IsValid() {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001095 ctx.ModuleErrorf("field %q does not exist", userFriendlyField)
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001096 return reflect.Value{}, false
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001097 }
1098
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001099 if child.IsZero() {
1100 return reflect.Value{}, false
1101 }
1102
1103 return child, true
Colin Cross06a931b2015-10-28 17:23:31 -07001104}
1105
Colin Crossa6845402020-11-16 15:08:19 -08001106// Squash the appropriate OS-specific property structs into the matching top level property structs
1107// based on the CompileOS value that was annotated on the variant.
Colin Crossa195f912019-10-16 11:07:20 -07001108func (m *ModuleBase) setOSProperties(ctx BottomUpMutatorContext) {
1109 os := m.commonProperties.CompileOS
1110
Ustadca02192021-12-20 12:56:46 -05001111 for i := range m.archProperties {
Usta851a3272022-01-05 23:42:33 -05001112 genProps := m.GetProperties()[i]
Colin Crossa195f912019-10-16 11:07:20 -07001113 if m.archProperties[i] == nil {
1114 continue
1115 }
1116 for _, archProperties := range m.archProperties[i] {
1117 archPropValues := reflect.ValueOf(archProperties).Elem()
1118
Colin Crosscbbd13f2020-01-17 14:08:22 -08001119 targetProp := archPropValues.FieldByName("Target").Elem()
Colin Crossa195f912019-10-16 11:07:20 -07001120
1121 // Handle host-specific properties in the form:
1122 // target: {
1123 // host: {
1124 // key: value,
1125 // },
1126 // },
Jiyong Park1613e552020-09-14 19:43:17 +09001127 if os.Class == Host {
Colin Crossa195f912019-10-16 11:07:20 -07001128 field := "Host"
1129 prefix := "target.host"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001130 if hostProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1131 mergePropertyStruct(ctx, genProps, hostProperties)
1132 }
Colin Crossa195f912019-10-16 11:07:20 -07001133 }
1134
1135 // Handle target OS generalities of the form:
1136 // target: {
1137 // bionic: {
1138 // key: value,
1139 // },
1140 // }
1141 if os.Linux() {
1142 field := "Linux"
1143 prefix := "target.linux"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001144 if linuxProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1145 mergePropertyStruct(ctx, genProps, linuxProperties)
1146 }
Colin Crossa195f912019-10-16 11:07:20 -07001147 }
1148
Colin Crossa98d36d2022-03-07 14:39:49 -08001149 if os.Linux() && os.Class == Host {
1150 field := "Host_linux"
1151 prefix := "target.host_linux"
1152 if linuxProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1153 mergePropertyStruct(ctx, genProps, linuxProperties)
1154 }
1155 }
1156
Colin Crossa195f912019-10-16 11:07:20 -07001157 if os.Bionic() {
1158 field := "Bionic"
1159 prefix := "target.bionic"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001160 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1161 mergePropertyStruct(ctx, genProps, bionicProperties)
1162 }
Colin Crossa195f912019-10-16 11:07:20 -07001163 }
1164
Colin Cross528d67e2021-07-23 22:23:07 +00001165 if os == Linux {
1166 field := "Glibc"
1167 prefix := "target.glibc"
1168 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1169 mergePropertyStruct(ctx, genProps, bionicProperties)
1170 }
1171 }
1172
1173 if os == LinuxMusl {
1174 field := "Musl"
1175 prefix := "target.musl"
1176 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1177 mergePropertyStruct(ctx, genProps, bionicProperties)
1178 }
Colin Cross528d67e2021-07-23 22:23:07 +00001179 }
1180
Colin Crossa195f912019-10-16 11:07:20 -07001181 // Handle target OS properties in the form:
1182 // target: {
1183 // linux_glibc: {
1184 // key: value,
1185 // },
1186 // not_windows: {
1187 // key: value,
1188 // },
1189 // android {
1190 // key: value,
1191 // },
1192 // },
1193 field := os.Field
1194 prefix := "target." + os.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001195 if osProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1196 mergePropertyStruct(ctx, genProps, osProperties)
1197 }
Colin Crossa195f912019-10-16 11:07:20 -07001198
Jiyong Park1613e552020-09-14 19:43:17 +09001199 if os.Class == Host && os != Windows {
Colin Crossa195f912019-10-16 11:07:20 -07001200 field := "Not_windows"
1201 prefix := "target.not_windows"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001202 if notWindowsProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1203 mergePropertyStruct(ctx, genProps, notWindowsProperties)
1204 }
Colin Crossa195f912019-10-16 11:07:20 -07001205 }
1206
1207 // Handle 64-bit device properties in the form:
1208 // target {
1209 // android64 {
1210 // key: value,
1211 // },
1212 // android32 {
1213 // key: value,
1214 // },
1215 // },
1216 // WARNING: this is probably not what you want to use in your blueprints file, it selects
1217 // options for all targets on a device that supports 64-bit binaries, not just the targets
1218 // that are being compiled for 64-bit. Its expected use case is binaries like linker and
1219 // debuggerd that need to know when they are a 32-bit process running on a 64-bit device
1220 if os.Class == Device {
1221 if ctx.Config().Android64() {
1222 field := "Android64"
1223 prefix := "target.android64"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001224 if android64Properties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1225 mergePropertyStruct(ctx, genProps, android64Properties)
1226 }
Colin Crossa195f912019-10-16 11:07:20 -07001227 } else {
1228 field := "Android32"
1229 prefix := "target.android32"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001230 if android32Properties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1231 mergePropertyStruct(ctx, genProps, android32Properties)
1232 }
Colin Crossa195f912019-10-16 11:07:20 -07001233 }
1234 }
1235 }
1236 }
1237}
1238
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001239// Returns the struct containing the properties specific to the given
1240// architecture type. These look like this in Blueprint files:
Colin Crossd079e0b2022-08-16 10:27:33 -07001241//
1242// arch: {
1243// arm64: {
1244// key: value,
1245// },
1246// },
1247//
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001248// This struct will also contain sub-structs containing to the architecture/CPU
1249// variants and features that themselves contain properties specific to those.
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001250func getArchTypeStruct(ctx ArchVariantContext, archProperties interface{}, archType ArchType) (reflect.Value, bool) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001251 archPropValues := reflect.ValueOf(archProperties).Elem()
1252 archProp := archPropValues.FieldByName("Arch").Elem()
1253 prefix := "arch." + archType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001254 return getChildPropertyStruct(ctx, archProp, archType.Name, prefix)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001255}
1256
1257// Returns the struct containing the properties specific to a given multilib
1258// value. These look like this in the Blueprint file:
Colin Crossd079e0b2022-08-16 10:27:33 -07001259//
1260// multilib: {
1261// lib32: {
1262// key: value,
1263// },
1264// },
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001265func getMultilibStruct(ctx ArchVariantContext, archProperties interface{}, archType ArchType) (reflect.Value, bool) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001266 archPropValues := reflect.ValueOf(archProperties).Elem()
1267 multilibProp := archPropValues.FieldByName("Multilib").Elem()
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001268 return getChildPropertyStruct(ctx, multilibProp, archType.Multilib, "multilib."+archType.Multilib)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001269}
1270
Liz Kammer9abd62d2021-05-21 08:37:59 -04001271func GetCompoundTargetField(os OsType, arch ArchType) string {
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04001272 return os.Field + "_" + arch.Name
1273}
1274
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001275// Returns the structs corresponding to the properties specific to the given
1276// architecture and OS in archProperties.
1277func getArchProperties(ctx BaseMutatorContext, archProperties interface{}, arch Arch, os OsType, nativeBridgeEnabled bool) []reflect.Value {
1278 result := make([]reflect.Value, 0)
1279 archPropValues := reflect.ValueOf(archProperties).Elem()
1280
1281 targetProp := archPropValues.FieldByName("Target").Elem()
1282
1283 archType := arch.ArchType
1284
1285 if arch.ArchType != Common {
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001286 archStruct, ok := getArchTypeStruct(ctx, archProperties, arch.ArchType)
1287 if ok {
1288 result = append(result, archStruct)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001289
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001290 // Handle arch-variant-specific properties in the form:
1291 // arch: {
1292 // arm: {
1293 // variant: {
1294 // key: value,
1295 // },
1296 // },
1297 // },
1298 v := variantReplacer.Replace(arch.ArchVariant)
1299 if v != "" {
1300 prefix := "arch." + archType.Name + "." + v
1301 if variantProperties, ok := getChildPropertyStruct(ctx, archStruct, v, prefix); ok {
1302 result = append(result, variantProperties)
1303 }
1304 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001305
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001306 // Handle cpu-variant-specific properties in the form:
1307 // arch: {
1308 // arm: {
1309 // variant: {
1310 // key: value,
1311 // },
1312 // },
1313 // },
1314 if arch.CpuVariant != arch.ArchVariant {
1315 c := variantReplacer.Replace(arch.CpuVariant)
1316 if c != "" {
1317 prefix := "arch." + archType.Name + "." + c
1318 if cpuVariantProperties, ok := getChildPropertyStruct(ctx, archStruct, c, prefix); ok {
1319 result = append(result, cpuVariantProperties)
1320 }
1321 }
1322 }
1323
1324 // Handle arch-feature-specific properties in the form:
1325 // arch: {
1326 // arm: {
1327 // feature: {
1328 // key: value,
1329 // },
1330 // },
1331 // },
1332 for _, feature := range arch.ArchFeatures {
1333 prefix := "arch." + archType.Name + "." + feature
1334 if featureProperties, ok := getChildPropertyStruct(ctx, archStruct, feature, prefix); ok {
1335 result = append(result, featureProperties)
1336 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001337 }
1338 }
1339
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001340 if multilibProperties, ok := getMultilibStruct(ctx, archProperties, archType); ok {
1341 result = append(result, multilibProperties)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001342 }
1343
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001344 // Handle combined OS-feature and arch specific properties in the form:
1345 // target: {
1346 // bionic_x86: {
1347 // key: value,
1348 // },
1349 // }
1350 if os.Linux() {
1351 field := "Linux_" + arch.ArchType.Name
1352 userFriendlyField := "target.linux_" + arch.ArchType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001353 if linuxProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1354 result = append(result, linuxProperties)
1355 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001356 }
1357
1358 if os.Bionic() {
1359 field := "Bionic_" + archType.Name
1360 userFriendlyField := "target.bionic_" + archType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001361 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1362 result = append(result, bionicProperties)
1363 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001364 }
1365
1366 // Handle combined OS and arch specific properties in the form:
1367 // target: {
1368 // linux_glibc_x86: {
1369 // key: value,
1370 // },
1371 // linux_glibc_arm: {
1372 // key: value,
1373 // },
1374 // android_arm {
1375 // key: value,
1376 // },
1377 // android_x86 {
1378 // key: value,
1379 // },
1380 // },
Liz Kammer9abd62d2021-05-21 08:37:59 -04001381 field := GetCompoundTargetField(os, archType)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001382 userFriendlyField := "target." + os.Name + "_" + archType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001383 if osArchProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1384 result = append(result, osArchProperties)
1385 }
Colin Cross528d67e2021-07-23 22:23:07 +00001386
Colin Cross1aa45b02022-02-10 10:33:10 -08001387 if os == Linux {
1388 field := "Glibc_" + archType.Name
1389 userFriendlyField := "target.glibc_" + "_" + archType.Name
1390 if osArchProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1391 result = append(result, osArchProperties)
1392 }
1393 }
1394
Colin Cross528d67e2021-07-23 22:23:07 +00001395 if os == LinuxMusl {
Colin Cross1aa45b02022-02-10 10:33:10 -08001396 field := "Musl_" + archType.Name
1397 userFriendlyField := "target.musl_" + "_" + archType.Name
1398 if osArchProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1399 result = append(result, osArchProperties)
1400 }
Colin Cross528d67e2021-07-23 22:23:07 +00001401 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001402 }
1403
1404 // Handle arm on x86 properties in the form:
1405 // target {
1406 // arm_on_x86 {
1407 // key: value,
1408 // },
1409 // arm_on_x86_64 {
1410 // key: value,
1411 // },
1412 // },
1413 if os.Class == Device {
1414 if arch.ArchType == X86 && (hasArmAbi(arch) ||
1415 hasArmAndroidArch(ctx.Config().Targets[Android])) {
1416 field := "Arm_on_x86"
1417 userFriendlyField := "target.arm_on_x86"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001418 if armOnX86Properties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1419 result = append(result, armOnX86Properties)
1420 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001421 }
1422 if arch.ArchType == X86_64 && (hasArmAbi(arch) ||
1423 hasArmAndroidArch(ctx.Config().Targets[Android])) {
1424 field := "Arm_on_x86_64"
1425 userFriendlyField := "target.arm_on_x86_64"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001426 if armOnX8664Properties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1427 result = append(result, armOnX8664Properties)
1428 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001429 }
1430 if os == Android && nativeBridgeEnabled {
1431 userFriendlyField := "Native_bridge"
1432 prefix := "target.native_bridge"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001433 if nativeBridgeProperties, ok := getChildPropertyStruct(ctx, targetProp, userFriendlyField, prefix); ok {
1434 result = append(result, nativeBridgeProperties)
1435 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001436 }
1437 }
1438
1439 return result
1440}
1441
Colin Crossa6845402020-11-16 15:08:19 -08001442// Squash the appropriate arch-specific property structs into the matching top level property
1443// structs based on the CompileTarget value that was annotated on the variant.
Colin Cross4157e882019-06-06 16:57:04 -07001444func (m *ModuleBase) setArchProperties(ctx BottomUpMutatorContext) {
1445 arch := m.Arch()
1446 os := m.Os()
Colin Crossd3ba0392015-05-07 14:11:29 -07001447
Ustadca02192021-12-20 12:56:46 -05001448 for i := range m.archProperties {
Usta851a3272022-01-05 23:42:33 -05001449 genProps := m.GetProperties()[i]
Colin Cross4157e882019-06-06 16:57:04 -07001450 if m.archProperties[i] == nil {
Dan Willemsenb1957a52016-06-23 23:44:54 -07001451 continue
1452 }
Dan Willemsenb1957a52016-06-23 23:44:54 -07001453
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001454 propStructs := make([]reflect.Value, 0)
1455 for _, archProperty := range m.archProperties[i] {
1456 propStructShard := getArchProperties(ctx, archProperty, arch, os, m.Target().NativeBridge == NativeBridgeEnabled)
1457 propStructs = append(propStructs, propStructShard...)
1458 }
Dan Willemsenb1957a52016-06-23 23:44:54 -07001459
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001460 for _, propStruct := range propStructs {
1461 mergePropertyStruct(ctx, genProps, propStruct)
Colin Crossbb2e2b72016-12-08 17:23:53 -08001462 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001463 }
1464}
1465
Colin Cross0c66bc62021-07-20 09:47:41 -07001466// determineBuildOS stores the OS and architecture used for host targets used during the build into
Colin Cross528d67e2021-07-23 22:23:07 +00001467// config based on the runtime OS and architecture determined by Go and the product configuration.
Colin Cross0c66bc62021-07-20 09:47:41 -07001468func determineBuildOS(config *config) {
1469 config.BuildOS = func() OsType {
1470 switch runtime.GOOS {
1471 case "linux":
Colin Cross528d67e2021-07-23 22:23:07 +00001472 if Bool(config.productVariables.HostMusl) {
1473 return LinuxMusl
1474 }
Colin Cross0c66bc62021-07-20 09:47:41 -07001475 return Linux
1476 case "darwin":
1477 return Darwin
1478 default:
1479 panic(fmt.Sprintf("unsupported OS: %s", runtime.GOOS))
1480 }
1481 }()
1482
1483 config.BuildArch = func() ArchType {
1484 switch runtime.GOARCH {
1485 case "amd64":
1486 return X86_64
1487 default:
1488 panic(fmt.Sprintf("unsupported Arch: %s", runtime.GOARCH))
1489 }
1490 }()
1491
1492}
1493
Colin Crossa6845402020-11-16 15:08:19 -08001494// Convert the arch product variables into a list of targets for each OsType.
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001495func decodeTargetProductVariables(config *config) (map[OsType][]Target, error) {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001496 variables := config.productVariables
Dan Willemsen490fd492015-11-24 17:53:15 -08001497
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001498 targets := make(map[OsType][]Target)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001499 var targetErr error
1500
Liz Kammerb7f33662022-02-28 14:16:16 -05001501 type targetConfig struct {
1502 os OsType
1503 archName string
1504 archVariant *string
1505 cpuVariant *string
1506 abi []string
1507 nativeBridgeEnabled NativeBridgeSupport
1508 nativeBridgeHostArchName *string
1509 nativeBridgeRelativePath *string
1510 }
1511
1512 addTarget := func(target targetConfig) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001513 if targetErr != nil {
1514 return
Dan Willemsen490fd492015-11-24 17:53:15 -08001515 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001516
Liz Kammerb7f33662022-02-28 14:16:16 -05001517 arch, err := decodeArch(target.os, target.archName, target.archVariant, target.cpuVariant, target.abi)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001518 if err != nil {
1519 targetErr = err
1520 return
1521 }
Liz Kammerb7f33662022-02-28 14:16:16 -05001522 nativeBridgeRelativePathStr := String(target.nativeBridgeRelativePath)
1523 nativeBridgeHostArchNameStr := String(target.nativeBridgeHostArchName)
dimitry8d6dde82019-07-11 10:23:53 +02001524
1525 // Use guest arch as relative install path by default
Liz Kammerb7f33662022-02-28 14:16:16 -05001526 if target.nativeBridgeEnabled && nativeBridgeRelativePathStr == "" {
dimitry8d6dde82019-07-11 10:23:53 +02001527 nativeBridgeRelativePathStr = arch.ArchType.String()
1528 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001529
Jiyong Park1613e552020-09-14 19:43:17 +09001530 // A target is considered as HostCross if it's a host target which can't run natively on
1531 // the currently configured build machine (either because the OS is different or because of
1532 // the unsupported arch)
1533 hostCross := false
Liz Kammerb7f33662022-02-28 14:16:16 -05001534 if target.os.Class == Host {
Jiyong Park1613e552020-09-14 19:43:17 +09001535 var osSupported bool
Liz Kammerb7f33662022-02-28 14:16:16 -05001536 if target.os == config.BuildOS {
Jiyong Park1613e552020-09-14 19:43:17 +09001537 osSupported = true
Liz Kammerb7f33662022-02-28 14:16:16 -05001538 } else if config.BuildOS.Linux() && target.os.Linux() {
Jiyong Park1613e552020-09-14 19:43:17 +09001539 // LinuxBionic and Linux are compatible
1540 osSupported = true
1541 } else {
1542 osSupported = false
1543 }
1544
1545 var archSupported bool
1546 if arch.ArchType == Common {
1547 archSupported = true
1548 } else if arch.ArchType.Name == *variables.HostArch {
1549 archSupported = true
1550 } else if variables.HostSecondaryArch != nil && arch.ArchType.Name == *variables.HostSecondaryArch {
1551 archSupported = true
1552 } else {
1553 archSupported = false
1554 }
1555 if !osSupported || !archSupported {
1556 hostCross = true
1557 }
1558 }
1559
Liz Kammerb7f33662022-02-28 14:16:16 -05001560 targets[target.os] = append(targets[target.os],
Colin Crossa1ad8d12016-06-01 17:09:44 -07001561 Target{
Liz Kammerb7f33662022-02-28 14:16:16 -05001562 Os: target.os,
dimitry8d6dde82019-07-11 10:23:53 +02001563 Arch: arch,
Liz Kammerb7f33662022-02-28 14:16:16 -05001564 NativeBridge: target.nativeBridgeEnabled,
dimitry8d6dde82019-07-11 10:23:53 +02001565 NativeBridgeHostArchName: nativeBridgeHostArchNameStr,
1566 NativeBridgeRelativePath: nativeBridgeRelativePathStr,
Jiyong Park1613e552020-09-14 19:43:17 +09001567 HostCross: hostCross,
Colin Crossa1ad8d12016-06-01 17:09:44 -07001568 })
Dan Willemsen490fd492015-11-24 17:53:15 -08001569 }
1570
Colin Cross4225f652015-09-17 14:33:42 -07001571 if variables.HostArch == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001572 return nil, fmt.Errorf("No host primary architecture set")
Colin Cross4225f652015-09-17 14:33:42 -07001573 }
1574
Colin Crossa6845402020-11-16 15:08:19 -08001575 // The primary host target, which must always exist.
Liz Kammerb7f33662022-02-28 14:16:16 -05001576 addTarget(targetConfig{os: config.BuildOS, archName: *variables.HostArch, nativeBridgeEnabled: NativeBridgeDisabled})
Colin Cross4225f652015-09-17 14:33:42 -07001577
Colin Crossa6845402020-11-16 15:08:19 -08001578 // An optional secondary host target.
Colin Crosseeabb892015-11-20 13:07:51 -08001579 if variables.HostSecondaryArch != nil && *variables.HostSecondaryArch != "" {
Liz Kammerb7f33662022-02-28 14:16:16 -05001580 addTarget(targetConfig{os: config.BuildOS, archName: *variables.HostSecondaryArch, nativeBridgeEnabled: NativeBridgeDisabled})
Dan Willemsen490fd492015-11-24 17:53:15 -08001581 }
1582
Colin Crossa6845402020-11-16 15:08:19 -08001583 // Optional cross-compiled host targets, generally Windows.
Colin Crossff3ae9d2018-04-10 16:15:18 -07001584 if String(variables.CrossHost) != "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001585 crossHostOs := osByName(*variables.CrossHost)
1586 if crossHostOs == NoOsType {
1587 return nil, fmt.Errorf("Unknown cross host OS %q", *variables.CrossHost)
1588 }
1589
Colin Crossff3ae9d2018-04-10 16:15:18 -07001590 if String(variables.CrossHostArch) == "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001591 return nil, fmt.Errorf("No cross-host primary architecture set")
Dan Willemsen490fd492015-11-24 17:53:15 -08001592 }
1593
Colin Crossa6845402020-11-16 15:08:19 -08001594 // The primary cross-compiled host target.
Liz Kammerb7f33662022-02-28 14:16:16 -05001595 addTarget(targetConfig{os: crossHostOs, archName: *variables.CrossHostArch, nativeBridgeEnabled: NativeBridgeDisabled})
Dan Willemsen490fd492015-11-24 17:53:15 -08001596
Colin Crossa6845402020-11-16 15:08:19 -08001597 // An optional secondary cross-compiled host target.
Dan Willemsen490fd492015-11-24 17:53:15 -08001598 if variables.CrossHostSecondaryArch != nil && *variables.CrossHostSecondaryArch != "" {
Liz Kammerb7f33662022-02-28 14:16:16 -05001599 addTarget(targetConfig{os: crossHostOs, archName: *variables.CrossHostSecondaryArch, nativeBridgeEnabled: NativeBridgeDisabled})
Dan Willemsen490fd492015-11-24 17:53:15 -08001600 }
1601 }
1602
Colin Crossa6845402020-11-16 15:08:19 -08001603 // Optional device targets
Dan Willemsen3f32f032016-07-11 14:36:48 -07001604 if variables.DeviceArch != nil && *variables.DeviceArch != "" {
Colin Crossa6845402020-11-16 15:08:19 -08001605 // The primary device target.
Liz Kammerb7f33662022-02-28 14:16:16 -05001606 addTarget(targetConfig{
1607 os: Android,
1608 archName: *variables.DeviceArch,
1609 archVariant: variables.DeviceArchVariant,
1610 cpuVariant: variables.DeviceCpuVariant,
1611 abi: variables.DeviceAbi,
1612 nativeBridgeEnabled: NativeBridgeDisabled,
1613 })
Colin Cross4225f652015-09-17 14:33:42 -07001614
Colin Crossa6845402020-11-16 15:08:19 -08001615 // An optional secondary device target.
Dan Willemsen3f32f032016-07-11 14:36:48 -07001616 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" {
Liz Kammerb7f33662022-02-28 14:16:16 -05001617 addTarget(targetConfig{
1618 os: Android,
1619 archName: *variables.DeviceSecondaryArch,
1620 archVariant: variables.DeviceSecondaryArchVariant,
1621 cpuVariant: variables.DeviceSecondaryCpuVariant,
1622 abi: variables.DeviceSecondaryAbi,
1623 nativeBridgeEnabled: NativeBridgeDisabled,
1624 })
Colin Cross4225f652015-09-17 14:33:42 -07001625 }
dimitry1f33e402019-03-26 12:39:31 +01001626
Colin Crossa6845402020-11-16 15:08:19 -08001627 // An optional NativeBridge device target.
dimitry1f33e402019-03-26 12:39:31 +01001628 if variables.NativeBridgeArch != nil && *variables.NativeBridgeArch != "" {
Liz Kammerb7f33662022-02-28 14:16:16 -05001629 addTarget(targetConfig{
1630 os: Android,
1631 archName: *variables.NativeBridgeArch,
1632 archVariant: variables.NativeBridgeArchVariant,
1633 cpuVariant: variables.NativeBridgeCpuVariant,
1634 abi: variables.NativeBridgeAbi,
1635 nativeBridgeEnabled: NativeBridgeEnabled,
1636 nativeBridgeHostArchName: variables.DeviceArch,
1637 nativeBridgeRelativePath: variables.NativeBridgeRelativePath,
1638 })
dimitry1f33e402019-03-26 12:39:31 +01001639 }
1640
Colin Crossa6845402020-11-16 15:08:19 -08001641 // An optional secondary NativeBridge device target.
dimitry1f33e402019-03-26 12:39:31 +01001642 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" &&
1643 variables.NativeBridgeSecondaryArch != nil && *variables.NativeBridgeSecondaryArch != "" {
Liz Kammerb7f33662022-02-28 14:16:16 -05001644 addTarget(targetConfig{
1645 os: Android,
1646 archName: *variables.NativeBridgeSecondaryArch,
1647 archVariant: variables.NativeBridgeSecondaryArchVariant,
1648 cpuVariant: variables.NativeBridgeSecondaryCpuVariant,
1649 abi: variables.NativeBridgeSecondaryAbi,
1650 nativeBridgeEnabled: NativeBridgeEnabled,
1651 nativeBridgeHostArchName: variables.DeviceSecondaryArch,
1652 nativeBridgeRelativePath: variables.NativeBridgeSecondaryRelativePath,
1653 })
dimitry1f33e402019-03-26 12:39:31 +01001654 }
Colin Cross4225f652015-09-17 14:33:42 -07001655 }
1656
Colin Crossa1ad8d12016-06-01 17:09:44 -07001657 if targetErr != nil {
1658 return nil, targetErr
1659 }
1660
1661 return targets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001662}
1663
Colin Crossbb2e2b72016-12-08 17:23:53 -08001664// hasArmAbi returns true if arch has at least one arm ABI
1665func hasArmAbi(arch Arch) bool {
Jaewoong Jung3aff5782020-02-11 07:54:35 -08001666 return PrefixInList(arch.Abi, "arm")
Colin Crossbb2e2b72016-12-08 17:23:53 -08001667}
1668
Lev Rumyantsev34581212021-10-13 09:47:59 -07001669// hasArmAndroidArch returns true if targets has at least
1670// one arm Android arch (possibly native bridged)
Colin Cross4247f0d2017-04-13 16:56:14 -07001671func hasArmAndroidArch(targets []Target) bool {
1672 for _, target := range targets {
Lev Rumyantsev34581212021-10-13 09:47:59 -07001673 if target.Os == Android &&
1674 (target.Arch.ArchType == Arm || target.Arch.ArchType == Arm64) {
Victor Khimenko5eb8ec12018-03-21 20:30:54 +01001675 return true
1676 }
1677 }
1678 return false
1679}
1680
Colin Crossa6845402020-11-16 15:08:19 -08001681// archConfig describes a built-in configuration.
Dan Albert4098deb2016-10-19 14:04:41 -07001682type archConfig struct {
1683 arch string
1684 archVariant string
1685 cpuVariant string
1686 abi []string
1687}
1688
Dan Albertf1d14c72020-07-30 14:32:55 -07001689// getNdkAbisConfig returns the list of archConfigs that are used for bulding
1690// the API stubs and static libraries that are included in the NDK. These are
1691// built *without Neon*, because non-Neon is still supported and building these
1692// with Neon will break those users.
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"}},
Inseob Kim5219c0e2021-06-17 00:33:00 +09001696 {"arm", "armv7-a", "", []string{"armeabi-v7a"}},
Dan Albert4098deb2016-10-19 14:04:41 -07001697 {"x86_64", "", "", []string{"x86_64"}},
Inseob Kim5219c0e2021-06-17 00:33:00 +09001698 {"x86", "", "", []string{"x86"}},
Dan Albert4098deb2016-10-19 14:04:41 -07001699 }
1700}
1701
Colin Crossa6845402020-11-16 15:08:19 -08001702// getAmlAbisConfig returns a list of archConfigs for the ABIs supported by mainline modules.
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001703func getAmlAbisConfig() []archConfig {
1704 return []archConfig{
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001705 {"arm64", "armv8-a", "", []string{"arm64-v8a"}},
Inseob Kim5219c0e2021-06-17 00:33:00 +09001706 {"arm", "armv7-a-neon", "", []string{"armeabi-v7a"}},
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001707 {"x86_64", "", "", []string{"x86_64"}},
Inseob Kim5219c0e2021-06-17 00:33:00 +09001708 {"x86", "", "", []string{"x86"}},
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001709 }
1710}
1711
Colin Crossa6845402020-11-16 15:08:19 -08001712// decodeArchSettings converts a list of archConfigs into a list of Targets for the given OsType.
Liz Kammerb7f33662022-02-28 14:16:16 -05001713func decodeAndroidArchSettings(archConfigs []archConfig) ([]Target, error) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001714 var ret []Target
Dan Willemsen322acaf2016-01-12 23:07:05 -08001715
Dan Albert4098deb2016-10-19 14:04:41 -07001716 for _, config := range archConfigs {
Liz Kammerb7f33662022-02-28 14:16:16 -05001717 arch, err := decodeArch(Android, config.arch, &config.archVariant,
Colin Crossa74ca042019-01-31 14:31:51 -08001718 &config.cpuVariant, config.abi)
Dan Willemsen322acaf2016-01-12 23:07:05 -08001719 if err != nil {
1720 return nil, err
1721 }
Colin Cross3b19f5d2019-09-17 14:45:31 -07001722
Colin Crossa1ad8d12016-06-01 17:09:44 -07001723 ret = append(ret, Target{
1724 Os: Android,
1725 Arch: arch,
1726 })
Dan Willemsen322acaf2016-01-12 23:07:05 -08001727 }
1728
1729 return ret, nil
1730}
1731
Colin Crossa6845402020-11-16 15:08:19 -08001732// decodeArch converts a set of strings from product variables into an Arch struct.
Colin Crossa74ca042019-01-31 14:31:51 -08001733func decodeArch(os OsType, arch string, archVariant, cpuVariant *string, abi []string) (Arch, error) {
Colin Crossa6845402020-11-16 15:08:19 -08001734 // Verify the arch is valid
Colin Crosseeabb892015-11-20 13:07:51 -08001735 archType, ok := archTypeMap[arch]
1736 if !ok {
1737 return Arch{}, fmt.Errorf("unknown arch %q", arch)
1738 }
Colin Cross4225f652015-09-17 14:33:42 -07001739
Colin Crosseeabb892015-11-20 13:07:51 -08001740 a := Arch{
Colin Cross4225f652015-09-17 14:33:42 -07001741 ArchType: archType,
Colin Crossa6845402020-11-16 15:08:19 -08001742 ArchVariant: String(archVariant),
1743 CpuVariant: String(cpuVariant),
Colin Crossa74ca042019-01-31 14:31:51 -08001744 Abi: abi,
Colin Crosseeabb892015-11-20 13:07:51 -08001745 }
1746
Colin Crossa6845402020-11-16 15:08:19 -08001747 // Convert generic arch variants into the empty string.
Colin Crosseeabb892015-11-20 13:07:51 -08001748 if a.ArchVariant == a.ArchType.Name || a.ArchVariant == "generic" {
1749 a.ArchVariant = ""
1750 }
1751
Colin Crossa6845402020-11-16 15:08:19 -08001752 // Convert generic CPU variants into the empty string.
Colin Crosseeabb892015-11-20 13:07:51 -08001753 if a.CpuVariant == a.ArchType.Name || a.CpuVariant == "generic" {
1754 a.CpuVariant = ""
1755 }
1756
Liz Kammer2c2afe22022-02-11 11:35:03 -05001757 if a.ArchVariant != "" {
1758 if validArchVariants := archVariants[archType]; !InList(a.ArchVariant, validArchVariants) {
1759 return Arch{}, fmt.Errorf("[%q] unknown arch variant %q, support variants: %q", archType, a.ArchVariant, validArchVariants)
1760 }
1761 }
1762
1763 if a.CpuVariant != "" {
1764 if validCpuVariants := cpuVariants[archType]; !InList(a.CpuVariant, validCpuVariants) {
1765 return Arch{}, fmt.Errorf("[%q] unknown cpu variant %q, support variants: %q", archType, a.CpuVariant, validCpuVariants)
1766 }
1767 }
1768
Colin Crossa6845402020-11-16 15:08:19 -08001769 // Filter empty ABIs out of the list.
Colin Crosseeabb892015-11-20 13:07:51 -08001770 for i := 0; i < len(a.Abi); i++ {
1771 if a.Abi[i] == "" {
1772 a.Abi = append(a.Abi[:i], a.Abi[i+1:]...)
1773 i--
1774 }
1775 }
1776
Liz Kammere8303bd2022-02-16 09:02:48 -05001777 // Set ArchFeatures from the arch type. for Android OS, other os-es do not specify features
1778 if os == Android {
1779 if featureMap, ok := androidArchFeatureMap[archType]; ok {
Dan Willemsen01a3c252019-01-11 19:02:16 -08001780 a.ArchFeatures = featureMap[a.ArchVariant]
1781 }
Colin Crossc5c24ad2015-11-20 15:35:00 -08001782 }
1783
Colin Crosseeabb892015-11-20 13:07:51 -08001784 return a, nil
Colin Cross4225f652015-09-17 14:33:42 -07001785}
1786
Colin Crossa6845402020-11-16 15:08:19 -08001787// filterMultilibTargets takes a list of Targets and a multilib value and returns a new list of
1788// Targets containing only those that have the given multilib value.
Colin Cross69617d32016-09-06 10:39:07 -07001789func filterMultilibTargets(targets []Target, multilib string) []Target {
1790 var ret []Target
1791 for _, t := range targets {
1792 if t.Arch.ArchType.Multilib == multilib {
1793 ret = append(ret, t)
1794 }
1795 }
1796 return ret
1797}
1798
Colin Crossa6845402020-11-16 15:08:19 -08001799// getCommonTargets returns the set of Os specific common architecture targets for each Os in a list
1800// of targets.
Nan Zhangdb0b9a32017-02-27 10:12:13 -08001801func getCommonTargets(targets []Target) []Target {
1802 var ret []Target
1803 set := make(map[string]bool)
1804
1805 for _, t := range targets {
1806 if _, found := set[t.Os.String()]; !found {
1807 set[t.Os.String()] = true
Colin Cross39a18142022-06-24 18:43:40 -07001808 common := commonTargetMap[t.Os.String()]
1809 common.HostCross = t.HostCross
1810 ret = append(ret, common)
Nan Zhangdb0b9a32017-02-27 10:12:13 -08001811 }
1812 }
1813
1814 return ret
1815}
1816
Sam Delmericocc271e22022-06-01 15:45:02 +00001817// 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 -07001818// that contains zero or one Target for each OsType and HostCross, selecting the one that matches
1819// the earliest filter.
Sam Delmericocc271e22022-06-01 15:45:02 +00001820func FirstTarget(targets []Target, filters ...string) []Target {
Jiyong Park22101982020-09-17 19:09:58 +09001821 // find the first target from each OS
1822 var ret []Target
Colin Crossc0f0eb82022-07-19 14:41:11 -07001823 type osHostCross struct {
1824 os OsType
1825 hostCross bool
1826 }
1827 set := make(map[osHostCross]bool)
Jiyong Park22101982020-09-17 19:09:58 +09001828
Colin Cross6b4a32d2017-12-05 13:42:45 -08001829 for _, filter := range filters {
1830 buildTargets := filterMultilibTargets(targets, filter)
Jiyong Park22101982020-09-17 19:09:58 +09001831 for _, t := range buildTargets {
Colin Crossc0f0eb82022-07-19 14:41:11 -07001832 key := osHostCross{t.Os, t.HostCross}
1833 if _, found := set[key]; !found {
1834 set[key] = true
Jiyong Park22101982020-09-17 19:09:58 +09001835 ret = append(ret, t)
1836 }
Colin Cross6b4a32d2017-12-05 13:42:45 -08001837 }
1838 }
Jiyong Park22101982020-09-17 19:09:58 +09001839 return ret
Colin Cross6b4a32d2017-12-05 13:42:45 -08001840}
1841
Colin Crossa6845402020-11-16 15:08:19 -08001842// decodeMultilibTargets uses the module's multilib setting to select one or more targets from a
1843// list of Targets.
Colin Crossee0bc3b2018-10-02 22:01:37 -07001844func decodeMultilibTargets(multilib string, targets []Target, prefer32 bool) ([]Target, error) {
Colin Crossa6845402020-11-16 15:08:19 -08001845 var buildTargets []Target
Colin Cross6b4a32d2017-12-05 13:42:45 -08001846
Colin Cross4225f652015-09-17 14:33:42 -07001847 switch multilib {
1848 case "common":
Colin Cross6b4a32d2017-12-05 13:42:45 -08001849 buildTargets = getCommonTargets(targets)
1850 case "common_first":
1851 buildTargets = getCommonTargets(targets)
1852 if prefer32 {
Sam Delmericocc271e22022-06-01 15:45:02 +00001853 buildTargets = append(buildTargets, FirstTarget(targets, "lib32", "lib64")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001854 } else {
Sam Delmericocc271e22022-06-01 15:45:02 +00001855 buildTargets = append(buildTargets, FirstTarget(targets, "lib64", "lib32")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001856 }
Colin Cross4225f652015-09-17 14:33:42 -07001857 case "both":
Colin Cross8b74d172016-09-13 09:59:14 -07001858 if prefer32 {
1859 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1860 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1861 } else {
1862 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1863 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1864 }
Colin Cross4225f652015-09-17 14:33:42 -07001865 case "32":
Colin Cross69617d32016-09-06 10:39:07 -07001866 buildTargets = filterMultilibTargets(targets, "lib32")
Colin Cross4225f652015-09-17 14:33:42 -07001867 case "64":
Colin Cross69617d32016-09-06 10:39:07 -07001868 buildTargets = filterMultilibTargets(targets, "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001869 case "first":
1870 if prefer32 {
Sam Delmericocc271e22022-06-01 15:45:02 +00001871 buildTargets = FirstTarget(targets, "lib32", "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001872 } else {
Sam Delmericocc271e22022-06-01 15:45:02 +00001873 buildTargets = FirstTarget(targets, "lib64", "lib32")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001874 }
Victor Chang9448e8f2020-09-14 15:34:16 +01001875 case "first_prefer32":
Sam Delmericocc271e22022-06-01 15:45:02 +00001876 buildTargets = FirstTarget(targets, "lib32", "lib64")
Colin Cross69617d32016-09-06 10:39:07 -07001877 case "prefer32":
Colin Cross3dceee32018-09-06 10:19:57 -07001878 buildTargets = filterMultilibTargets(targets, "lib32")
1879 if len(buildTargets) == 0 {
1880 buildTargets = filterMultilibTargets(targets, "lib64")
1881 }
Dan Willemsen47450072021-10-19 20:24:49 -07001882 case "darwin_universal":
1883 buildTargets = filterMultilibTargets(targets, "lib64")
1884 // Reverse the targets so that the first architecture can depend on the second
1885 // architecture module in order to merge the outputs.
1886 reverseSliceInPlace(buildTargets)
1887 case "darwin_universal_common_first":
1888 archTargets := filterMultilibTargets(targets, "lib64")
1889 reverseSliceInPlace(archTargets)
1890 buildTargets = append(getCommonTargets(targets), archTargets...)
Colin Cross4225f652015-09-17 14:33:42 -07001891 default:
Victor Chang9448e8f2020-09-14 15:34:16 +01001892 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 -07001893 multilib)
Colin Cross4225f652015-09-17 14:33:42 -07001894 }
1895
Colin Crossa1ad8d12016-06-01 17:09:44 -07001896 return buildTargets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001897}
Jingwen Chen5d864492021-02-24 07:20:12 -05001898
Chris Parsonsc424b762021-04-29 18:06:50 -04001899func (m *ModuleBase) getArchPropertySet(propertySet interface{}, archType ArchType) interface{} {
1900 archString := archType.Field
1901 for i := range m.archProperties {
1902 if m.archProperties[i] == nil {
1903 // Skip over nil properties
1904 continue
1905 }
1906
1907 // Not archProperties are usable; this function looks for properties of a very specific
1908 // form, and ignores the rest.
1909 for _, archProperty := range m.archProperties[i] {
1910 // archPropValue is a property struct, we are looking for the form:
1911 // `arch: { arm: { key: value, ... }}`
1912 archPropValue := reflect.ValueOf(archProperty).Elem()
1913
1914 // Unwrap src so that it should looks like a pointer to `arm: { key: value, ... }`
1915 src := archPropValue.FieldByName("Arch").Elem()
1916
1917 // Step into non-nil pointers to structs in the src value.
1918 if src.Kind() == reflect.Ptr {
1919 if src.IsNil() {
1920 continue
1921 }
1922 src = src.Elem()
1923 }
1924
1925 // Find the requested field (e.g. arm, x86) in the src struct.
1926 src = src.FieldByName(archString)
1927
1928 // We only care about structs.
1929 if !src.IsValid() || src.Kind() != reflect.Struct {
1930 continue
1931 }
1932
1933 // If the value of the field is a struct then step into the
1934 // BlueprintEmbed field. The special "BlueprintEmbed" name is
1935 // used by createArchPropTypeDesc to embed the arch properties
1936 // in the parent struct, so the src arch prop should be in this
1937 // field.
1938 //
1939 // See createArchPropTypeDesc for more details on how Arch-specific
1940 // module properties are processed from the nested props and written
1941 // into the module's archProperties.
1942 src = src.FieldByName("BlueprintEmbed")
1943
1944 // Clone the destination prop, since we want a unique prop struct per arch.
1945 propertySetClone := reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
1946
1947 // Copy the located property struct into the cloned destination property struct.
1948 err := proptools.ExtendMatchingProperties([]interface{}{propertySetClone}, src.Interface(), nil, proptools.OrderReplace)
1949 if err != nil {
1950 // This is fine, it just means the src struct doesn't match the type of propertySet.
1951 continue
1952 }
1953
1954 return propertySetClone
1955 }
1956 }
1957 // No property set was found specific to the given arch, so return an empty
1958 // property set.
1959 return reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
1960}
1961
1962// getMultilibPropertySet returns a property set struct matching the type of
1963// `propertySet`, containing multilib-specific module properties for the given architecture.
1964// If no multilib-specific properties exist for the given architecture, returns an empty property
1965// set matching `propertySet`'s type.
1966func (m *ModuleBase) getMultilibPropertySet(propertySet interface{}, archType ArchType) interface{} {
1967 // archType.Multilib is lowercase (for example, lib32) but property struct field is
1968 // capitalized, such as Lib32, so use strings.Title to capitalize it.
1969 multiLibString := strings.Title(archType.Multilib)
1970
1971 for i := range m.archProperties {
1972 if m.archProperties[i] == nil {
1973 // Skip over nil properties
1974 continue
1975 }
1976
1977 // Not archProperties are usable; this function looks for properties of a very specific
1978 // form, and ignores the rest.
1979 for _, archProperties := range m.archProperties[i] {
1980 // archPropValue is a property struct, we are looking for the form:
1981 // `multilib: { lib32: { key: value, ... }}`
1982 archPropValue := reflect.ValueOf(archProperties).Elem()
1983
1984 // Unwrap src so that it should looks like a pointer to `lib32: { key: value, ... }`
1985 src := archPropValue.FieldByName("Multilib").Elem()
1986
1987 // Step into non-nil pointers to structs in the src value.
1988 if src.Kind() == reflect.Ptr {
1989 if src.IsNil() {
1990 // Ignore nil pointers.
1991 continue
1992 }
1993 src = src.Elem()
1994 }
1995
1996 // Find the requested field (e.g. lib32) in the src struct.
1997 src = src.FieldByName(multiLibString)
1998
1999 // We only care about valid struct pointers.
2000 if !src.IsValid() || src.Kind() != reflect.Ptr || src.Elem().Kind() != reflect.Struct {
2001 continue
2002 }
2003
2004 // Get the zero value for the requested property set.
2005 propertySetClone := reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
2006
2007 // Copy the located property struct into the "zero" property set struct.
2008 err := proptools.ExtendMatchingProperties([]interface{}{propertySetClone}, src.Interface(), nil, proptools.OrderReplace)
2009
2010 if err != nil {
2011 // This is fine, it just means the src struct doesn't match.
2012 continue
2013 }
2014
2015 return propertySetClone
2016 }
2017 }
2018
2019 // There were no multilib properties specifically matching the given archtype.
2020 // Return zeroed value.
2021 return reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
2022}
2023
Liz Kammerb6dbc872021-05-14 15:14:40 -04002024// ArchVariantContext defines the limited context necessary to retrieve arch_variant properties.
2025type ArchVariantContext interface {
2026 ModuleErrorf(fmt string, args ...interface{})
2027 PropertyErrorf(property, fmt string, args ...interface{})
2028}
2029
Liz Kammer9abd62d2021-05-21 08:37:59 -04002030// ArchVariantProperties represents a map of arch-variant config strings to a property interface{}.
2031type ArchVariantProperties map[string]interface{}
2032
2033// ConfigurationAxisToArchVariantProperties represents a map of bazel.ConfigurationAxis to
2034// ArchVariantProperties, such that each independent arch-variant axis maps to the
2035// configs/properties for that axis.
2036type ConfigurationAxisToArchVariantProperties map[bazel.ConfigurationAxis]ArchVariantProperties
2037
2038// GetArchVariantProperties returns a ConfigurationAxisToArchVariantProperties where the
2039// arch-variant properties correspond to the values of the properties of the 'propertySet' struct
2040// that are specific to that axis/configuration. Each axis is independent, containing
2041// non-overlapping configs that correspond to the various "arch-variant" support, at this time:
Colin Crossd079e0b2022-08-16 10:27:33 -07002042//
2043// arches (including multilib)
2044// oses
2045// arch+os combinations
Jingwen Chen5d864492021-02-24 07:20:12 -05002046//
Liz Kammer9abd62d2021-05-21 08:37:59 -04002047// For example, passing a struct { Foo bool, Bar string } will return an interface{} that can be
2048// type asserted back into the same struct, containing the config-specific property value specified
2049// by the module if defined.
Chris Parsonsc424b762021-04-29 18:06:50 -04002050//
2051// Arch-specific properties may come from an arch stanza or a multilib stanza; properties
2052// in these stanzas are combined.
2053// For example: `arch: { x86: { Foo: ["bar"] } }, multilib: { lib32: {` Foo: ["baz"] } }`
2054// will result in `Foo: ["bar", "baz"]` being returned for architecture x86, if the given
2055// propertyset contains `Foo []string`.
Liz Kammer9abd62d2021-05-21 08:37:59 -04002056func (m *ModuleBase) GetArchVariantProperties(ctx ArchVariantContext, propertySet interface{}) ConfigurationAxisToArchVariantProperties {
Jingwen Chen5d864492021-02-24 07:20:12 -05002057 // Return value of the arch types to the prop values for that arch.
Liz Kammer9abd62d2021-05-21 08:37:59 -04002058 axisToProps := ConfigurationAxisToArchVariantProperties{}
Jingwen Chen5d864492021-02-24 07:20:12 -05002059
2060 // Nothing to do for non-arch-specific modules.
2061 if !m.ArchSpecific() {
Liz Kammer9abd62d2021-05-21 08:37:59 -04002062 return axisToProps
Jingwen Chen5d864492021-02-24 07:20:12 -05002063 }
2064
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002065 dstType := reflect.ValueOf(propertySet).Type()
2066 var archProperties []interface{}
2067
2068 // First find the property set in the module that corresponds to the requested
Usta851a3272022-01-05 23:42:33 -05002069 // one. m.archProperties[i] corresponds to m.GetProperties()[i].
2070 for i, generalProp := range m.GetProperties() {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002071 srcType := reflect.ValueOf(generalProp).Type()
2072 if srcType == dstType {
2073 archProperties = m.archProperties[i]
Liz Kammer135bf552021-08-11 10:46:06 -04002074 axisToProps[bazel.NoConfigAxis] = ArchVariantProperties{"": generalProp}
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002075 break
2076 }
2077 }
2078
2079 if archProperties == nil {
2080 // This module does not have the property set requested
Liz Kammer9abd62d2021-05-21 08:37:59 -04002081 return axisToProps
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002082 }
2083
Liz Kammer9abd62d2021-05-21 08:37:59 -04002084 archToProp := ArchVariantProperties{}
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002085 // For each arch type (x86, arm64, etc.)
Chris Parsonsc424b762021-04-29 18:06:50 -04002086 for _, arch := range ArchTypeList() {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002087 // Arch properties are sometimes sharded (see createArchPropTypeDesc() ).
Cole Faustc843b992022-08-02 18:06:50 -07002088 // Iterate over every shard and extract a struct with the same type as the
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002089 // input one that contains the data specific to that arch.
2090 propertyStructs := make([]reflect.Value, 0)
Cole Faustc843b992022-08-02 18:06:50 -07002091 archFeaturePropertyStructs := make(map[string][]reflect.Value, 0)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002092 for _, archProperty := range archProperties {
Lukacs T. Berki5f518392021-05-17 11:44:58 +02002093 archTypeStruct, ok := getArchTypeStruct(ctx, archProperty, arch)
2094 if ok {
2095 propertyStructs = append(propertyStructs, archTypeStruct)
Cole Faustc843b992022-08-02 18:06:50 -07002096
2097 // For each feature this arch supports (arm: neon, x86: ssse3, sse4, ...)
2098 for _, feature := range archFeatures[arch] {
2099 prefix := "arch." + arch.Name + "." + feature
2100 if featureProperties, ok := getChildPropertyStruct(ctx, archTypeStruct, feature, prefix); ok {
2101 archFeaturePropertyStructs[feature] = append(archFeaturePropertyStructs[feature], featureProperties)
2102 }
2103 }
Lukacs T. Berki5f518392021-05-17 11:44:58 +02002104 }
2105 multilibStruct, ok := getMultilibStruct(ctx, archProperty, arch)
2106 if ok {
2107 propertyStructs = append(propertyStructs, multilibStruct)
2108 }
Jingwen Chen5d864492021-02-24 07:20:12 -05002109 }
2110
Cole Faustc843b992022-08-02 18:06:50 -07002111 archToProp[arch.Name] = mergeStructs(ctx, propertyStructs, propertySet)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002112
Cole Faustc843b992022-08-02 18:06:50 -07002113 // In soong, if multiple features match the current configuration, they're
2114 // all used. In bazel, we have to have unambiguous select() statements, so
2115 // we can't have two features that are both active in the same select().
2116 // One alternative is to split out each feature into a separate select(),
2117 // but then it's difficult to support exclude_srcs, which may need to
2118 // exclude things from the regular arch select() statement if a certain
2119 // feature is active. Instead, keep the features in the same select
2120 // statement as the arches, but emit the power set of all possible
2121 // combinations of features, so that bazel can match the most precise one.
2122 allFeatures := make([]string, 0, len(archFeaturePropertyStructs))
2123 for feature := range archFeaturePropertyStructs {
2124 allFeatures = append(allFeatures, feature)
2125 }
2126 for _, features := range bazel.PowerSetWithoutEmptySet(allFeatures) {
2127 sort.Strings(features)
2128 propsForCurrentFeatureSet := make([]reflect.Value, 0)
2129 propsForCurrentFeatureSet = append(propsForCurrentFeatureSet, propertyStructs...)
2130 for _, feature := range features {
2131 propsForCurrentFeatureSet = append(propsForCurrentFeatureSet, archFeaturePropertyStructs[feature]...)
2132 }
2133 archToProp[arch.Name+"-"+strings.Join(features, "-")] =
2134 mergeStructs(ctx, propsForCurrentFeatureSet, propertySet)
2135 }
Jingwen Chen5d864492021-02-24 07:20:12 -05002136 }
Liz Kammer9abd62d2021-05-21 08:37:59 -04002137 axisToProps[bazel.ArchConfigurationAxis] = archToProp
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002138
Liz Kammer9abd62d2021-05-21 08:37:59 -04002139 osToProp := ArchVariantProperties{}
2140 archOsToProp := ArchVariantProperties{}
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002141
Liz Kammerfdd72e62021-10-11 15:41:03 -04002142 linuxStructs := getTargetStructs(ctx, archProperties, "Linux")
2143 bionicStructs := getTargetStructs(ctx, archProperties, "Bionic")
2144 hostStructs := getTargetStructs(ctx, archProperties, "Host")
Colin Crossa98d36d2022-03-07 14:39:49 -08002145 hostLinuxStructs := getTargetStructs(ctx, archProperties, "Host_linux")
Liz Kammerfdd72e62021-10-11 15:41:03 -04002146 hostNotWindowsStructs := getTargetStructs(ctx, archProperties, "Not_windows")
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002147
Liz Kammer9abd62d2021-05-21 08:37:59 -04002148 // For android, linux, ...
2149 for _, os := range osTypeList {
2150 if os == CommonOS {
2151 // It looks like this OS value is not used in Blueprint files
2152 continue
2153 }
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002154 osStructs := make([]reflect.Value, 0)
Liz Kammerfdd72e62021-10-11 15:41:03 -04002155
2156 osSpecificStructs := getTargetStructs(ctx, archProperties, os.Field)
2157 if os.Class == Host {
2158 osStructs = append(osStructs, hostStructs...)
Chris Parsonsa37e1952021-09-28 16:47:36 -04002159 }
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002160 if os.Linux() {
2161 osStructs = append(osStructs, linuxStructs...)
2162 }
2163 if os.Bionic() {
2164 osStructs = append(osStructs, bionicStructs...)
2165 }
Colin Crossa98d36d2022-03-07 14:39:49 -08002166 if os.Linux() && os.Class == Host {
2167 osStructs = append(osStructs, hostLinuxStructs...)
2168 }
Liz Kammerfdd72e62021-10-11 15:41:03 -04002169
2170 if os == LinuxMusl {
2171 osStructs = append(osStructs, getTargetStructs(ctx, archProperties, "Musl")...)
2172 }
2173 if os == Linux {
2174 osStructs = append(osStructs, getTargetStructs(ctx, archProperties, "Glibc")...)
2175 }
2176
2177 osStructs = append(osStructs, osSpecificStructs...)
2178
2179 if os.Class == Host && os != Windows {
2180 osStructs = append(osStructs, hostNotWindowsStructs...)
2181 }
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002182 osToProp[os.Name] = mergeStructs(ctx, osStructs, propertySet)
Chris Parsonsa37e1952021-09-28 16:47:36 -04002183
Liz Kammer9abd62d2021-05-21 08:37:59 -04002184 // For arm, x86, ...
2185 for _, arch := range osArchTypeMap[os] {
Chris Parsonsa37e1952021-09-28 16:47:36 -04002186 osArchStructs := make([]reflect.Value, 0)
2187
Chris Parsonsa37e1952021-09-28 16:47:36 -04002188 // Auto-combine with Linux_ and Bionic_ targets. This potentially results in
2189 // repetition and select() bloat, but use of Linux_* and Bionic_* targets is rare.
2190 // TODO(b/201423152): Look into cleanup.
2191 if os.Linux() {
2192 targetField := "Linux_" + arch.Name
Liz Kammerfdd72e62021-10-11 15:41:03 -04002193 targetStructs := getTargetStructs(ctx, archProperties, targetField)
2194 osArchStructs = append(osArchStructs, targetStructs...)
Chris Parsonsa37e1952021-09-28 16:47:36 -04002195 }
2196 if os.Bionic() {
2197 targetField := "Bionic_" + arch.Name
Liz Kammerfdd72e62021-10-11 15:41:03 -04002198 targetStructs := getTargetStructs(ctx, archProperties, targetField)
2199 osArchStructs = append(osArchStructs, targetStructs...)
Chris Parsonsa37e1952021-09-28 16:47:36 -04002200 }
Colin Cross2d295a22022-03-07 14:46:20 -08002201 if os == LinuxMusl {
2202 targetField := "Musl_" + arch.Name
2203 targetStructs := getTargetStructs(ctx, archProperties, targetField)
2204 osArchStructs = append(osArchStructs, targetStructs...)
2205 }
2206 if os == Linux {
2207 targetField := "Glibc_" + arch.Name
2208 targetStructs := getTargetStructs(ctx, archProperties, targetField)
2209 osArchStructs = append(osArchStructs, targetStructs...)
2210 }
Chris Parsonsa37e1952021-09-28 16:47:36 -04002211
Liz Kammerfdd72e62021-10-11 15:41:03 -04002212 targetField := GetCompoundTargetField(os, arch)
2213 targetName := fmt.Sprintf("%s_%s", os.Name, arch.Name)
2214 targetStructs := getTargetStructs(ctx, archProperties, targetField)
2215 osArchStructs = append(osArchStructs, targetStructs...)
2216
Chris Parsonsa37e1952021-09-28 16:47:36 -04002217 archOsToProp[targetName] = mergeStructs(ctx, osArchStructs, propertySet)
Liz Kammer9abd62d2021-05-21 08:37:59 -04002218 }
2219 }
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002220
Liz Kammer9abd62d2021-05-21 08:37:59 -04002221 axisToProps[bazel.OsConfigurationAxis] = osToProp
2222 axisToProps[bazel.OsArchConfigurationAxis] = archOsToProp
Liz Kammer9abd62d2021-05-21 08:37:59 -04002223 return axisToProps
Jingwen Chen5d864492021-02-24 07:20:12 -05002224}
Jingwen Chen91220d72021-03-24 02:18:33 -04002225
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04002226// Returns a struct matching the propertySet interface, containing properties specific to the targetName
2227// For example, given these arguments:
Colin Crossd079e0b2022-08-16 10:27:33 -07002228//
2229// propertySet = BaseCompilerProperties
2230// targetName = "android_arm"
2231//
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04002232// And given this Android.bp fragment:
Colin Crossd079e0b2022-08-16 10:27:33 -07002233//
2234// target:
2235// android_arm: {
2236// srcs: ["foo.c"],
2237// }
2238// android_arm64: {
2239// srcs: ["bar.c"],
2240// }
2241// }
2242//
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04002243// This would return a BaseCompilerProperties with BaseCompilerProperties.Srcs = ["foo.c"]
Liz Kammerfdd72e62021-10-11 15:41:03 -04002244func getTargetStructs(ctx ArchVariantContext, archProperties []interface{}, targetName string) []reflect.Value {
2245 var propertyStructs []reflect.Value
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04002246 for _, archProperty := range archProperties {
2247 archPropValues := reflect.ValueOf(archProperty).Elem()
2248 targetProp := archPropValues.FieldByName("Target").Elem()
2249 targetStruct, ok := getChildPropertyStruct(ctx, targetProp, targetName, targetName)
2250 if ok {
2251 propertyStructs = append(propertyStructs, targetStruct)
Chris Parsonsa37e1952021-09-28 16:47:36 -04002252 } else {
Liz Kammerfdd72e62021-10-11 15:41:03 -04002253 return []reflect.Value{}
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04002254 }
2255 }
2256
Liz Kammerfdd72e62021-10-11 15:41:03 -04002257 return propertyStructs
Chris Parsonsa37e1952021-09-28 16:47:36 -04002258}
2259
2260func mergeStructs(ctx ArchVariantContext, propertyStructs []reflect.Value, propertySet interface{}) interface{} {
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04002261 // Create a new instance of the requested property set
2262 value := reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
2263
2264 // Merge all the structs together
2265 for _, propertyStruct := range propertyStructs {
2266 mergePropertyStruct(ctx, value, propertyStruct)
2267 }
2268
2269 return value
2270}
Liz Kammere8303bd2022-02-16 09:02:48 -05002271
2272func printArchTypeStarlarkDict(dict map[ArchType][]string) string {
2273 valDict := make(map[string]string, len(dict))
2274 for k, v := range dict {
2275 valDict[k.String()] = starlark_fmt.PrintStringList(v, 1)
2276 }
2277 return starlark_fmt.PrintDict(valDict, 0)
2278}
2279
2280func printArchTypeNestedStarlarkDict(dict map[ArchType]map[string][]string) string {
2281 valDict := make(map[string]string, len(dict))
2282 for k, v := range dict {
2283 valDict[k.String()] = starlark_fmt.PrintStringListDict(v, 1)
2284 }
2285 return starlark_fmt.PrintDict(valDict, 0)
2286}
2287
2288func StarlarkArchConfigurations() string {
2289 return fmt.Sprintf(`
2290_arch_to_variants = %s
2291
2292_arch_to_cpu_variants = %s
2293
2294_arch_to_features = %s
2295
2296_android_arch_feature_for_arch_variant = %s
2297
2298arch_to_variants = _arch_to_variants
2299arch_to_cpu_variants = _arch_to_cpu_variants
2300arch_to_features = _arch_to_features
2301android_arch_feature_for_arch_variants = _android_arch_feature_for_arch_variant
2302`, printArchTypeStarlarkDict(archVariants),
2303 printArchTypeStarlarkDict(cpuVariants),
2304 printArchTypeStarlarkDict(archFeatures),
2305 printArchTypeNestedStarlarkDict(androidArchFeatureMap),
2306 )
2307}