blob: 9e79e317530a28d6710d07d663296b858fb3f151 [file] [log] [blame]
Colin Cross3f40fa42015-01-30 17:27:36 -08001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Colin Cross635c3b02016-05-18 15:37:25 -070015package android
Colin Cross3f40fa42015-01-30 17:27:36 -080016
17import (
Colin Cross74ba9622019-02-11 15:11:14 -080018 "encoding"
Liz Kammer992918d2022-11-11 10:37:54 -050019 "encoding/json"
Colin Cross3f40fa42015-01-30 17:27:36 -080020 "fmt"
21 "reflect"
22 "runtime"
Cole Faustc843b992022-08-02 18:06:50 -070023 "sort"
Colin Cross3f40fa42015-01-30 17:27:36 -080024 "strings"
Colin Crossf6566ed2015-03-24 11:13:38 -070025
Colin Crosscb0ac952021-07-20 13:17:15 -070026 "android/soong/bazel"
Liz Kammere8303bd2022-02-16 09:02:48 -050027 "android/soong/starlark_fmt"
Colin Crosscb0ac952021-07-20 13:17:15 -070028
Colin Cross0f7d2ef2019-10-16 11:03:10 -070029 "github.com/google/blueprint"
Colin Cross617b88a2020-08-24 18:04:09 -070030 "github.com/google/blueprint/bootstrap"
Colin Crossf6566ed2015-03-24 11:13:38 -070031 "github.com/google/blueprint/proptools"
Colin Cross3f40fa42015-01-30 17:27:36 -080032)
33
Colin Cross3f40fa42015-01-30 17:27:36 -080034/*
35Example blueprints file containing all variant property groups, with comment listing what type
36of variants get properties in that group:
37
38module {
39 arch: {
40 arm: {
41 // Host or device variants with arm architecture
42 },
43 arm64: {
44 // Host or device variants with arm64 architecture
45 },
Colin Cross3f40fa42015-01-30 17:27:36 -080046 x86: {
47 // Host or device variants with x86 architecture
48 },
49 x86_64: {
50 // Host or device variants with x86_64 architecture
51 },
52 },
53 multilib: {
54 lib32: {
55 // Host or device variants for 32-bit architectures
56 },
57 lib64: {
58 // Host or device variants for 64-bit architectures
59 },
60 },
61 target: {
62 android: {
Martin Stjernholme284b482020-09-23 21:03:27 +010063 // Device variants (implies Bionic)
Colin Cross3f40fa42015-01-30 17:27:36 -080064 },
65 host: {
66 // Host variants
67 },
Martin Stjernholme284b482020-09-23 21:03:27 +010068 bionic: {
69 // Bionic (device and host) variants
70 },
71 linux_bionic: {
72 // Bionic host variants
73 },
74 linux: {
75 // Bionic (device and host) and Linux glibc variants
76 },
Dan Willemsen5746bd42017-10-02 19:42:01 -070077 linux_glibc: {
Martin Stjernholme284b482020-09-23 21:03:27 +010078 // Linux host variants (using non-Bionic libc)
Colin Cross3f40fa42015-01-30 17:27:36 -080079 },
80 darwin: {
81 // Darwin host variants
82 },
83 windows: {
84 // Windows host variants
85 },
86 not_windows: {
87 // Non-windows host variants
88 },
Martin Stjernholme284b482020-09-23 21:03:27 +010089 android_arm: {
90 // Any <os>_<arch> combination restricts to that os and arch
91 },
Colin Cross3f40fa42015-01-30 17:27:36 -080092 },
93}
94*/
Colin Cross7d5136f2015-05-11 13:39:40 -070095
Colin Cross3f40fa42015-01-30 17:27:36 -080096// An Arch indicates a single CPU architecture.
97type Arch struct {
Colin Crossa6845402020-11-16 15:08:19 -080098 // The type of the architecture (arm, arm64, x86, or x86_64).
99 ArchType ArchType
100
101 // The variant of the architecture, for example "armv7-a" or "armv7-a-neon" for arm.
102 ArchVariant string
103
104 // The variant of the CPU, for example "cortex-a53" for arm64.
105 CpuVariant string
106
107 // The list of Android app ABIs supported by the CPU architecture, for example "arm64-v8a".
108 Abi []string
109
110 // The list of arch-specific features supported by the CPU architecture, for example "neon".
Colin Crossc5c24ad2015-11-20 15:35:00 -0800111 ArchFeatures []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800112}
113
Colin Crossa6845402020-11-16 15:08:19 -0800114// String returns the Arch as a string. The value is used as the name of the variant created
115// by archMutator.
Colin Cross3f40fa42015-01-30 17:27:36 -0800116func (a Arch) String() string {
Colin Crossd3ba0392015-05-07 14:11:29 -0700117 s := a.ArchType.String()
Colin Cross3f40fa42015-01-30 17:27:36 -0800118 if a.ArchVariant != "" {
119 s += "_" + a.ArchVariant
120 }
121 if a.CpuVariant != "" {
122 s += "_" + a.CpuVariant
123 }
124 return s
125}
126
Colin Crossa6845402020-11-16 15:08:19 -0800127// ArchType is used to define the 4 supported architecture types (arm, arm64, x86, x86_64), as
128// well as the "common" architecture used for modules that support multiple architectures, for
129// example Java modules.
Colin Cross3f40fa42015-01-30 17:27:36 -0800130type ArchType struct {
Colin Crossa6845402020-11-16 15:08:19 -0800131 // Name is the name of the architecture type, "arm", "arm64", "x86", or "x86_64".
132 Name string
133
134 // Field is the name of the field used in properties that refer to the architecture, e.g. "Arm64".
135 Field string
136
137 // Multilib is either "lib32" or "lib64" for 32-bit or 64-bit architectures.
Colin Crossec193632015-07-06 17:49:43 -0700138 Multilib string
Colin Cross3f40fa42015-01-30 17:27:36 -0800139}
140
Colin Crossa6845402020-11-16 15:08:19 -0800141// String returns the name of the ArchType.
142func (a ArchType) String() string {
143 return a.Name
144}
145
146const COMMON_VARIANT = "common"
147
148var (
149 archTypeList []ArchType
150
Colin Crossf05b0d32022-07-14 18:10:34 -0700151 Arm = newArch("arm", "lib32")
152 Arm64 = newArch("arm64", "lib64")
153 Riscv64 = newArch("riscv64", "lib64")
154 X86 = newArch("x86", "lib32")
155 X86_64 = newArch("x86_64", "lib64")
Colin Crossa6845402020-11-16 15:08:19 -0800156
157 Common = ArchType{
158 Name: COMMON_VARIANT,
159 }
160)
161
162var archTypeMap = map[string]ArchType{}
163
Colin Crossec193632015-07-06 17:49:43 -0700164func newArch(name, multilib string) ArchType {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700165 archType := ArchType{
Colin Crossec193632015-07-06 17:49:43 -0700166 Name: name,
Dan Willemsenb1957a52016-06-23 23:44:54 -0700167 Field: proptools.FieldNameForProperty(name),
Colin Crossec193632015-07-06 17:49:43 -0700168 Multilib: multilib,
Colin Cross3f40fa42015-01-30 17:27:36 -0800169 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700170 archTypeList = append(archTypeList, archType)
Colin Crossa6845402020-11-16 15:08:19 -0800171 archTypeMap[name] = archType
Dan Willemsenb1957a52016-06-23 23:44:54 -0700172 return archType
Colin Cross3f40fa42015-01-30 17:27:36 -0800173}
174
Ustaeabf0f32021-12-06 15:17:23 -0500175// ArchTypeList returns a slice copy of the 4 supported ArchTypes for arm,
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000176// arm64, x86 and x86_64.
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -0700177func ArchTypeList() []ArchType {
178 return append([]ArchType(nil), archTypeList...)
179}
180
Colin Crossa6845402020-11-16 15:08:19 -0800181// MarshalText allows an ArchType to be serialized through any encoder that supports
182// encoding.TextMarshaler.
Colin Cross74ba9622019-02-11 15:11:14 -0800183func (a ArchType) MarshalText() ([]byte, error) {
Jeongik Chabec4d032021-04-15 08:55:38 +0900184 return []byte(a.String()), nil
Colin Cross74ba9622019-02-11 15:11:14 -0800185}
186
Colin Crossa6845402020-11-16 15:08:19 -0800187var _ encoding.TextMarshaler = ArchType{}
Colin Cross74ba9622019-02-11 15:11:14 -0800188
Colin Crossa6845402020-11-16 15:08:19 -0800189// UnmarshalText allows an ArchType to be deserialized through any decoder that supports
190// encoding.TextUnmarshaler.
Colin Cross74ba9622019-02-11 15:11:14 -0800191func (a *ArchType) UnmarshalText(text []byte) error {
192 if u, ok := archTypeMap[string(text)]; ok {
193 *a = u
194 return nil
195 }
196
197 return fmt.Errorf("unknown ArchType %q", text)
198}
199
Colin Crossa6845402020-11-16 15:08:19 -0800200var _ encoding.TextUnmarshaler = &ArchType{}
Colin Crossa1ad8d12016-06-01 17:09:44 -0700201
Colin Crossa6845402020-11-16 15:08:19 -0800202// OsClass is an enum that describes whether a variant of a module runs on the host, on the device,
203// or is generic.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700204type OsClass int
205
206const (
Colin Crossa6845402020-11-16 15:08:19 -0800207 // Generic is used for variants of modules that are not OS-specific.
Dan Willemsen0e2d97b2016-11-28 17:50:06 -0800208 Generic OsClass = iota
Colin Crossa6845402020-11-16 15:08:19 -0800209 // Device is used for variants of modules that run on the device.
Dan Willemsen0e2d97b2016-11-28 17:50:06 -0800210 Device
Colin Crossa6845402020-11-16 15:08:19 -0800211 // Host is used for variants of modules that run on the host.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700212 Host
Colin Crossa1ad8d12016-06-01 17:09:44 -0700213)
214
Colin Crossa6845402020-11-16 15:08:19 -0800215// String returns the OsClass as a string.
Colin Cross67a5c132017-05-09 13:45:28 -0700216func (class OsClass) String() string {
217 switch class {
218 case Generic:
219 return "generic"
220 case Device:
221 return "device"
222 case Host:
223 return "host"
Colin Cross67a5c132017-05-09 13:45:28 -0700224 default:
225 panic(fmt.Errorf("unknown class %d", class))
226 }
227}
228
Colin Crossa6845402020-11-16 15:08:19 -0800229// OsType describes an OS variant of a module.
230type OsType struct {
231 // Name is the name of the OS. It is also used as the name of the property in Android.bp
232 // files.
233 Name string
234
235 // Field is the name of the OS converted to an exported field name, i.e. with the first
236 // character capitalized.
237 Field string
238
239 // Class is the OsClass of the OS.
240 Class OsClass
241
242 // DefaultDisabled is set when the module variants for the OS should not be created unless
243 // the module explicitly requests them. This is used to limit Windows cross compilation to
244 // only modules that need it.
245 DefaultDisabled bool
246}
247
248// String returns the name of the OsType.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700249func (os OsType) String() string {
250 return os.Name
Colin Cross54c71122016-06-01 17:09:44 -0700251}
252
Colin Crossa6845402020-11-16 15:08:19 -0800253// Bionic returns true if the OS uses the Bionic libc runtime, i.e. if the OS is Android or
254// is Linux with Bionic.
Dan Willemsen866b5632017-09-22 12:28:24 -0700255func (os OsType) Bionic() bool {
256 return os == Android || os == LinuxBionic
257}
258
Colin Crossa6845402020-11-16 15:08:19 -0800259// Linux returns true if the OS uses the Linux kernel, i.e. if the OS is Android or is Linux
260// with or without the Bionic libc runtime.
Dan Willemsen866b5632017-09-22 12:28:24 -0700261func (os OsType) Linux() bool {
Colin Cross528d67e2021-07-23 22:23:07 +0000262 return os == Android || os == Linux || os == LinuxBionic || os == LinuxMusl
Dan Willemsen866b5632017-09-22 12:28:24 -0700263}
264
Colin Crossa6845402020-11-16 15:08:19 -0800265// newOsType constructs an OsType and adds it to the global lists.
266func newOsType(name string, class OsClass, defDisabled bool, archTypes ...ArchType) OsType {
267 checkCalledFromInit()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700268 os := OsType{
269 Name: name,
Colin Crossa6845402020-11-16 15:08:19 -0800270 Field: proptools.FieldNameForProperty(name),
Colin Crossa1ad8d12016-06-01 17:09:44 -0700271 Class: class,
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800272
273 DefaultDisabled: defDisabled,
Colin Cross54c71122016-06-01 17:09:44 -0700274 }
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000275 osTypeList = append(osTypeList, os)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800276
277 if _, found := commonTargetMap[name]; found {
278 panic(fmt.Errorf("Found Os type duplicate during OsType registration: %q", name))
279 } else {
Colin Crosse9fe2942020-11-10 18:12:15 -0800280 commonTargetMap[name] = Target{Os: os, Arch: CommonArch}
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800281 }
Colin Crossa6845402020-11-16 15:08:19 -0800282 osArchTypeMap[os] = archTypes
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800283
Colin Crossa1ad8d12016-06-01 17:09:44 -0700284 return os
285}
286
Colin Crossa6845402020-11-16 15:08:19 -0800287// osByName returns the OsType that has the given name, or NoOsType if none match.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700288func osByName(name string) OsType {
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000289 for _, os := range osTypeList {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700290 if os.Name == name {
291 return os
292 }
293 }
294
295 return NoOsType
Dan Willemsen490fd492015-11-24 17:53:15 -0800296}
297
Colin Crossa6845402020-11-16 15:08:19 -0800298var (
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000299 // osTypeList contains a list of all the supported OsTypes, including ones not supported
Colin Crossa6845402020-11-16 15:08:19 -0800300 // by the current build host or the target device.
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000301 osTypeList []OsType
Colin Crossa6845402020-11-16 15:08:19 -0800302 // commonTargetMap maps names of OsTypes to the corresponding common Target, i.e. the
303 // Target with the same OsType and the common ArchType.
304 commonTargetMap = make(map[string]Target)
305 // osArchTypeMap maps OsTypes to the list of supported ArchTypes for that OS.
306 osArchTypeMap = map[OsType][]ArchType{}
307
308 // NoOsType is a placeholder for when no OS is needed.
309 NoOsType OsType
310 // Linux is the OS for the Linux kernel plus the glibc runtime.
311 Linux = newOsType("linux_glibc", Host, false, X86, X86_64)
Colin Cross528d67e2021-07-23 22:23:07 +0000312 // LinuxMusl is the OS for the Linux kernel plus the musl runtime.
Colin Crossa9b2aac2022-06-15 17:25:51 -0700313 LinuxMusl = newOsType("linux_musl", Host, false, X86, X86_64, Arm64, Arm)
Colin Crossa6845402020-11-16 15:08:19 -0800314 // Darwin is the OS for MacOS/Darwin host machines.
Dan Willemsen8528f4e2021-10-19 00:22:06 -0700315 Darwin = newOsType("darwin", Host, false, Arm64, X86_64)
Colin Crossa6845402020-11-16 15:08:19 -0800316 // LinuxBionic is the OS for the Linux kernel plus the Bionic libc runtime, but without the
317 // rest of Android.
318 LinuxBionic = newOsType("linux_bionic", Host, false, Arm64, X86_64)
319 // Windows the OS for Windows host machines.
320 Windows = newOsType("windows", Host, true, X86, X86_64)
321 // Android is the OS for target devices that run all of Android, including the Linux kernel
322 // and the Bionic libc runtime.
Colin Crossf05b0d32022-07-14 18:10:34 -0700323 Android = newOsType("android", Device, false, Arm, Arm64, Riscv64, X86, X86_64)
Colin Crossa6845402020-11-16 15:08:19 -0800324
325 // CommonOS is a pseudo OSType for a common OS variant, which is OsType agnostic and which
326 // has dependencies on all the OS variants.
327 CommonOS = newOsType("common_os", Generic, false)
Colin Crosse9fe2942020-11-10 18:12:15 -0800328
329 // CommonArch is the Arch for all modules that are os-specific but not arch specific,
330 // for example most Java modules.
331 CommonArch = Arch{ArchType: Common}
dimitry1f33e402019-03-26 12:39:31 +0100332)
333
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000334// OsTypeList returns a slice copy of the supported OsTypes.
335func OsTypeList() []OsType {
336 return append([]OsType(nil), osTypeList...)
337}
338
Colin Crossa6845402020-11-16 15:08:19 -0800339// Target specifies the OS and architecture that a module is being compiled for.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700340type Target struct {
Colin Crossa6845402020-11-16 15:08:19 -0800341 // Os the OS that the module is being compiled for (e.g. "linux_glibc", "android").
342 Os OsType
343 // Arch is the architecture that the module is being compiled for.
344 Arch Arch
345 // NativeBridge is NativeBridgeEnabled if the architecture is supported using NativeBridge
346 // (i.e. arm on x86) for this device.
347 NativeBridge NativeBridgeSupport
348 // NativeBridgeHostArchName is the name of the real architecture that is used to implement
349 // the NativeBridge architecture. For example, for arm on x86 this would be "x86".
dimitry8d6dde82019-07-11 10:23:53 +0200350 NativeBridgeHostArchName string
Colin Crossa6845402020-11-16 15:08:19 -0800351 // NativeBridgeRelativePath is the name of the subdirectory that will contain NativeBridge
352 // libraries and binaries.
dimitry8d6dde82019-07-11 10:23:53 +0200353 NativeBridgeRelativePath string
Jiyong Park1613e552020-09-14 19:43:17 +0900354
355 // HostCross is true when the target cannot run natively on the current build host.
356 // For example, linux_glibc_x86 returns true on a regular x86/i686/Linux machines, but returns false
357 // on Mac (different OS), or on 64-bit only i686/Linux machines (unsupported arch).
358 HostCross bool
Colin Crossd3ba0392015-05-07 14:11:29 -0700359}
360
Colin Crossa6845402020-11-16 15:08:19 -0800361// NativeBridgeSupport is an enum that specifies if a Target supports NativeBridge.
362type NativeBridgeSupport bool
363
364const (
365 NativeBridgeDisabled NativeBridgeSupport = false
366 NativeBridgeEnabled NativeBridgeSupport = true
367)
368
369// String returns the OS and arch variations used for the Target.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700370func (target Target) String() string {
Colin Crossa195f912019-10-16 11:07:20 -0700371 return target.OsVariation() + "_" + target.ArchVariation()
372}
373
Colin Crossa6845402020-11-16 15:08:19 -0800374// OsVariation returns the name of the variation used by the osMutator for the Target.
Colin Crossa195f912019-10-16 11:07:20 -0700375func (target Target) OsVariation() string {
376 return target.Os.String()
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700377}
378
Colin Crossa6845402020-11-16 15:08:19 -0800379// ArchVariation returns the name of the variation used by the archMutator for the Target.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700380func (target Target) ArchVariation() string {
381 var variation string
dimitry1f33e402019-03-26 12:39:31 +0100382 if target.NativeBridge {
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700383 variation = "native_bridge_"
dimitry1f33e402019-03-26 12:39:31 +0100384 }
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700385 variation += target.Arch.String()
386
Colin Crossa195f912019-10-16 11:07:20 -0700387 return variation
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700388}
389
Colin Crossa6845402020-11-16 15:08:19 -0800390// Variations returns a list of blueprint.Variations for the osMutator and archMutator for the
391// Target.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700392func (target Target) Variations() []blueprint.Variation {
393 return []blueprint.Variation{
Colin Crossa195f912019-10-16 11:07:20 -0700394 {Mutator: "os", Variation: target.OsVariation()},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700395 {Mutator: "arch", Variation: target.ArchVariation()},
396 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800397}
398
Colin Crossa6845402020-11-16 15:08:19 -0800399// osMutator splits an arch-specific module into a variant for each OS that is enabled for the
400// module. It uses the HostOrDevice value passed to InitAndroidArchModule and the
401// device_supported and host_supported properties to determine which OsTypes are enabled for this
402// module, then searches through the Targets to determine which have enabled Targets for this
403// module.
Colin Cross617b88a2020-08-24 18:04:09 -0700404func osMutator(bpctx blueprint.BottomUpMutatorContext) {
Colin Crossa195f912019-10-16 11:07:20 -0700405 var module Module
406 var ok bool
Colin Cross617b88a2020-08-24 18:04:09 -0700407 if module, ok = bpctx.Module().(Module); !ok {
Colin Crossa6845402020-11-16 15:08:19 -0800408 // The module is not a Soong module, it is a Blueprint module.
Colin Cross617b88a2020-08-24 18:04:09 -0700409 if bootstrap.IsBootstrapModule(bpctx.Module()) {
410 // Bootstrap Go modules are always the build OS or linux bionic.
411 config := bpctx.Config().(Config)
412 osNames := []string{config.BuildOSTarget.OsVariation()}
413 for _, hostCrossTarget := range config.Targets[LinuxBionic] {
414 if hostCrossTarget.Arch.ArchType == config.BuildOSTarget.Arch.ArchType {
415 osNames = append(osNames, hostCrossTarget.OsVariation())
416 }
417 }
418 osNames = FirstUniqueStrings(osNames)
419 bpctx.CreateVariations(osNames...)
420 }
Colin Crossa195f912019-10-16 11:07:20 -0700421 return
422 }
423
Colin Cross617b88a2020-08-24 18:04:09 -0700424 // Bootstrap Go module support above requires this mutator to be a
425 // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext
426 // filters out non-Soong modules. Now that we've handled them, create a
427 // normal android.BottomUpMutatorContext.
Colin Crossb63d7b32023-12-07 16:54:51 -0800428 mctx := bottomUpMutatorContextFactory(bpctx, module, false)
Colin Cross984223f2024-02-01 17:10:23 -0800429 defer bottomUpMutatorContextPool.Put(mctx)
Colin Cross617b88a2020-08-24 18:04:09 -0700430
Colin Crossa195f912019-10-16 11:07:20 -0700431 base := module.base()
432
Colin Crossa6845402020-11-16 15:08:19 -0800433 // Nothing to do for modules that are not architecture specific (e.g. a genrule).
Colin Crossa195f912019-10-16 11:07:20 -0700434 if !base.ArchSpecific() {
435 return
436 }
437
Colin Crossa6845402020-11-16 15:08:19 -0800438 // Collect a list of OSTypes supported by this module based on the HostOrDevice value
439 // passed to InitAndroidArchModule and the device_supported and host_supported properties.
Colin Crossa195f912019-10-16 11:07:20 -0700440 var moduleOSList []OsType
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000441 for _, os := range osTypeList {
Jiyong Park1613e552020-09-14 19:43:17 +0900442 for _, t := range mctx.Config().Targets[os] {
Colin Cross08d6f8f2020-11-19 02:33:19 +0000443 if base.supportsTarget(t) {
Jiyong Park1613e552020-09-14 19:43:17 +0900444 moduleOSList = append(moduleOSList, os)
445 break
Colin Crossa195f912019-10-16 11:07:20 -0700446 }
447 }
Colin Crossa195f912019-10-16 11:07:20 -0700448 }
449
Cole Faust8fc38f32023-12-12 17:14:22 -0800450 createCommonOSVariant := base.commonProperties.CreateCommonOSVariant
451
Colin Crossa6845402020-11-16 15:08:19 -0800452 // If there are no supported OSes then disable the module.
Cole Faust8fc38f32023-12-12 17:14:22 -0800453 if len(moduleOSList) == 0 && !createCommonOSVariant {
Inseob Kimeec88e12020-01-22 11:11:29 +0900454 base.Disable()
Colin Crossa195f912019-10-16 11:07:20 -0700455 return
456 }
457
Colin Crossa6845402020-11-16 15:08:19 -0800458 // Convert the list of supported OsTypes to the variation names.
Colin Crossa195f912019-10-16 11:07:20 -0700459 osNames := make([]string, len(moduleOSList))
Colin Crossa195f912019-10-16 11:07:20 -0700460 for i, os := range moduleOSList {
461 osNames[i] = os.String()
462 }
463
Paul Duffin1356d8c2020-02-25 19:26:33 +0000464 if createCommonOSVariant {
Colin Crossa6845402020-11-16 15:08:19 -0800465 // A CommonOS variant was requested so add it to the list of OS variants to
Paul Duffin1356d8c2020-02-25 19:26:33 +0000466 // create. It needs to be added to the end because it needs to depend on the
467 // the other variants in the list returned by CreateVariations(...) and inter
468 // variant dependencies can only be created from a later variant in that list to
469 // an earlier one. That is because variants are always processed in the order in
470 // which they are returned from CreateVariations(...).
471 osNames = append(osNames, CommonOS.Name)
472 moduleOSList = append(moduleOSList, CommonOS)
Colin Crossa195f912019-10-16 11:07:20 -0700473 }
474
Colin Crossa6845402020-11-16 15:08:19 -0800475 // Create the variations, annotate each one with which OS it was created for, and
476 // squash the appropriate OS-specific properties into the top level properties.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000477 modules := mctx.CreateVariations(osNames...)
478 for i, m := range modules {
479 m.base().commonProperties.CompileOS = moduleOSList[i]
480 m.base().setOSProperties(mctx)
481 }
482
483 if createCommonOSVariant {
484 // A CommonOS variant was requested so add dependencies from it (the last one in
485 // the list) to the OS type specific variants.
486 last := len(modules) - 1
487 commonOSVariant := modules[last]
488 commonOSVariant.base().commonProperties.CommonOSVariant = true
489 for _, module := range modules[0:last] {
490 // Ignore modules that are enabled. Note, this will only avoid adding
491 // dependencies on OsType variants that are explicitly disabled in their
492 // properties. The CommonOS variant will still depend on disabled variants
493 // if they are disabled afterwards, e.g. in archMutator if
494 if module.Enabled() {
495 mctx.AddInterVariantDependency(commonOsToOsSpecificVariantTag, commonOSVariant, module)
496 }
497 }
498 }
499}
500
Colin Crossc179ea62020-10-09 10:54:15 -0700501type archDepTag struct {
502 blueprint.BaseDependencyTag
503 name string
504}
Paul Duffin1356d8c2020-02-25 19:26:33 +0000505
Colin Crossc179ea62020-10-09 10:54:15 -0700506// Identifies the dependency from CommonOS variant to the os specific variants.
507var commonOsToOsSpecificVariantTag = archDepTag{name: "common os to os specific"}
508
Paul Duffin1356d8c2020-02-25 19:26:33 +0000509// Get the OsType specific variants for the current CommonOS variant.
510//
511// The returned list will only contain enabled OsType specific variants of the
512// module referenced in the supplied context. An empty list is returned if there
513// are no enabled variants or the supplied context is not for an CommonOS
514// variant.
515func GetOsSpecificVariantsOfCommonOSVariant(mctx BaseModuleContext) []Module {
516 var variants []Module
517 mctx.VisitDirectDeps(func(m Module) {
518 if mctx.OtherModuleDependencyTag(m) == commonOsToOsSpecificVariantTag {
519 if m.Enabled() {
520 variants = append(variants, m)
521 }
522 }
523 })
Paul Duffin1356d8c2020-02-25 19:26:33 +0000524 return variants
Colin Crossa195f912019-10-16 11:07:20 -0700525}
526
Dan Willemsen47450072021-10-19 20:24:49 -0700527var DarwinUniversalVariantTag = archDepTag{name: "darwin universal binary"}
528
Colin Crossee0bc3b2018-10-02 22:01:37 -0700529// archMutator splits a module into a variant for each Target requested by the module. Target selection
Colin Crossa6845402020-11-16 15:08:19 -0800530// for a module is in three levels, OsClass, multilib, and then Target.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700531// OsClass selection is determined by:
Colin Crossd079e0b2022-08-16 10:27:33 -0700532// - The HostOrDeviceSupported value passed in to InitAndroidArchModule by the module type factory, which selects
533// whether the module type can compile for host, device or both.
534// - The host_supported and device_supported properties on the module.
535//
Roland Levillainf5b635d2019-06-05 14:42:57 +0100536// 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 -0700537// for the module, the Device OsClass is selected.
538// Within each selected OsClass, the multilib selection is determined by:
Colin Crossd079e0b2022-08-16 10:27:33 -0700539// - The compile_multilib property if it set (which may be overridden by target.android.compile_multilib or
540// target.host.compile_multilib).
541// - The default multilib passed to InitAndroidArchModule if compile_multilib was not set.
542//
Colin Crossee0bc3b2018-10-02 22:01:37 -0700543// Valid multilib values include:
Colin Crossd079e0b2022-08-16 10:27:33 -0700544//
545// "both": compile for all Targets supported by the OsClass (generally x86_64 and x86, or arm64 and arm).
546// "first": compile for only a single preferred Target supported by the OsClass. This is generally x86_64 or arm64,
547// but may be arm for a 32-bit only build.
548// "32": compile for only a single 32-bit Target supported by the OsClass.
549// "64": compile for only a single 64-bit Target supported by the OsClass.
550// "common": compile a for a single Target that will work on all Targets supported by the OsClass (for example Java).
551// "common_first": compile a for a Target that will work on all Targets supported by the OsClass
552// (same as "common"), plus a second Target for the preferred Target supported by the OsClass
553// (same as "first"). This is used for java_binary that produces a common .jar and a wrapper
554// executable script.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700555//
556// Once the list of Targets is determined, the module is split into a variant for each Target.
557//
558// Modules can be initialized with InitAndroidMultiTargetsArchModule, in which case they will be split by OsClass,
559// 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 -0700560func archMutator(bpctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700561 var module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800562 var ok bool
Colin Cross617b88a2020-08-24 18:04:09 -0700563 if module, ok = bpctx.Module().(Module); !ok {
564 if bootstrap.IsBootstrapModule(bpctx.Module()) {
565 // Bootstrap Go modules are always the build architecture.
566 bpctx.CreateVariations(bpctx.Config().(Config).BuildOSTarget.ArchVariation())
567 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800568 return
569 }
570
Colin Cross617b88a2020-08-24 18:04:09 -0700571 // Bootstrap Go module support above requires this mutator to be a
572 // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext
573 // filters out non-Soong modules. Now that we've handled them, create a
574 // normal android.BottomUpMutatorContext.
Colin Crossb63d7b32023-12-07 16:54:51 -0800575 mctx := bottomUpMutatorContextFactory(bpctx, module, false)
Colin Cross984223f2024-02-01 17:10:23 -0800576 defer bottomUpMutatorContextPool.Put(mctx)
Colin Cross617b88a2020-08-24 18:04:09 -0700577
Colin Cross5eca7cb2018-10-02 14:02:10 -0700578 base := module.base()
579
580 if !base.ArchSpecific() {
Colin Crossb9db4802016-06-03 01:50:47 +0000581 return
582 }
583
Colin Crossa195f912019-10-16 11:07:20 -0700584 os := base.commonProperties.CompileOS
Paul Duffin1356d8c2020-02-25 19:26:33 +0000585 if os == CommonOS {
586 // Make sure that the target related properties are initialized for the
587 // CommonOS variant.
588 addTargetProperties(module, commonTargetMap[os.Name], nil, true)
589
590 // Do not create arch specific variants for the CommonOS variant.
591 return
592 }
593
Colin Crossa195f912019-10-16 11:07:20 -0700594 osTargets := mctx.Config().Targets[os]
Colin Crossfb0c16e2019-11-20 17:12:35 -0800595 image := base.commonProperties.ImageVariation
Colin Crossa6845402020-11-16 15:08:19 -0800596 // Filter NativeBridge targets unless they are explicitly supported.
597 // Skip creating native bridge variants for non-core modules.
Paul Duffine3d1ae42021-09-03 17:47:17 +0100598 if os == Android && !(base.IsNativeBridgeSupported() && image == CoreVariation) {
Colin Cross83bead42019-12-18 10:45:46 -0800599
Colin Crossa195f912019-10-16 11:07:20 -0700600 var targets []Target
601 for _, t := range osTargets {
602 if !t.NativeBridge {
603 targets = append(targets, t)
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700604 }
605 }
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700606
Colin Crossa195f912019-10-16 11:07:20 -0700607 osTargets = targets
608 }
Colin Crossee0bc3b2018-10-02 22:01:37 -0700609
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700610 // only the primary arch in the ramdisk / vendor_ramdisk / recovery partition
Inseob Kim08758f02021-04-08 21:13:22 +0900611 if os == Android && (module.InstallInRecovery() || module.InstallInRamdisk() || module.InstallInVendorRamdisk() || module.InstallInDebugRamdisk()) {
Colin Crossa195f912019-10-16 11:07:20 -0700612 osTargets = []Target{osTargets[0]}
613 }
dimitry1f33e402019-03-26 12:39:31 +0100614
Jaewoong Jung003d8082021-02-24 17:39:54 -0800615 // Windows builds always prefer 32-bit
616 prefer32 := os == Windows
dimitry1f33e402019-03-26 12:39:31 +0100617
Colin Crossa6845402020-11-16 15:08:19 -0800618 // Determine the multilib selection for this module.
Christopher Ferris98f10222022-07-13 23:16:52 -0700619 ignorePrefer32OnDevice := mctx.Config().IgnorePrefer32OnDevice()
620 multilib, extraMultilib := decodeMultilib(base, os, ignorePrefer32OnDevice)
Colin Crossa6845402020-11-16 15:08:19 -0800621
622 // Convert the multilib selection into a list of Targets.
Colin Crossa195f912019-10-16 11:07:20 -0700623 targets, err := decodeMultilibTargets(multilib, osTargets, prefer32)
624 if err != nil {
625 mctx.ModuleErrorf("%s", err.Error())
626 }
Colin Cross5eca7cb2018-10-02 14:02:10 -0700627
Colin Crossc0f0eb82022-07-19 14:41:11 -0700628 // If there are no supported targets disable the module.
629 if len(targets) == 0 {
630 base.Disable()
631 return
632 }
633
Colin Crossa6845402020-11-16 15:08:19 -0800634 // If the module is using extraMultilib, decode the extraMultilib selection into
635 // a separate list of Targets.
Colin Crossa195f912019-10-16 11:07:20 -0700636 var multiTargets []Target
637 if extraMultilib != "" {
638 multiTargets, err = decodeMultilibTargets(extraMultilib, osTargets, prefer32)
Colin Crossa1ad8d12016-06-01 17:09:44 -0700639 if err != nil {
640 mctx.ModuleErrorf("%s", err.Error())
641 }
Colin Crossc0f0eb82022-07-19 14:41:11 -0700642 multiTargets = filterHostCross(multiTargets, targets[0].HostCross)
Colin Crossb9db4802016-06-03 01:50:47 +0000643 }
644
Colin Crossa6845402020-11-16 15:08:19 -0800645 // Recovery is always the primary architecture, filter out any other architectures.
Inseob Kim20fb5d42021-02-02 20:07:58 +0900646 // Common arch is also allowed
Colin Crossfb0c16e2019-11-20 17:12:35 -0800647 if image == RecoveryVariation {
648 primaryArch := mctx.Config().DevicePrimaryArchType()
Inseob Kim20fb5d42021-02-02 20:07:58 +0900649 targets = filterToArch(targets, primaryArch, Common)
650 multiTargets = filterToArch(multiTargets, primaryArch, Common)
Colin Crossfb0c16e2019-11-20 17:12:35 -0800651 }
652
Colin Crossa6845402020-11-16 15:08:19 -0800653 // If there are no supported targets disable the module.
Colin Crossa195f912019-10-16 11:07:20 -0700654 if len(targets) == 0 {
Inseob Kimeec88e12020-01-22 11:11:29 +0900655 base.Disable()
Dan Willemsen3f32f032016-07-11 14:36:48 -0700656 return
657 }
658
Colin Crossa6845402020-11-16 15:08:19 -0800659 // Convert the targets into a list of arch variation names.
Colin Crossa195f912019-10-16 11:07:20 -0700660 targetNames := make([]string, len(targets))
Colin Crossa195f912019-10-16 11:07:20 -0700661 for i, target := range targets {
662 targetNames[i] = target.ArchVariation()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700663 }
664
Colin Crossa6845402020-11-16 15:08:19 -0800665 // Create the variations, annotate each one with which Target it was created for, and
666 // squash the appropriate arch-specific properties into the top level properties.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700667 modules := mctx.CreateVariations(targetNames...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800668 for i, m := range modules {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000669 addTargetProperties(m, targets[i], multiTargets, i == 0)
Colin Cross617b88a2020-08-24 18:04:09 -0700670 m.base().setArchProperties(mctx)
Dan Willemsen8528f4e2021-10-19 00:22:06 -0700671
672 // Install support doesn't understand Darwin+Arm64
673 if os == Darwin && targets[i].HostCross {
674 m.base().commonProperties.SkipInstall = true
675 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800676 }
Dan Willemsen47450072021-10-19 20:24:49 -0700677
678 // Create a dependency for Darwin Universal binaries from the primary to secondary
679 // architecture. The module itself will be responsible for calling lipo to merge the outputs.
680 if os == Darwin {
681 if multilib == "darwin_universal" && len(modules) == 2 {
682 mctx.AddInterVariantDependency(DarwinUniversalVariantTag, modules[1], modules[0])
683 } else if multilib == "darwin_universal_common_first" && len(modules) == 3 {
684 mctx.AddInterVariantDependency(DarwinUniversalVariantTag, modules[2], modules[1])
685 }
686 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800687}
688
Colin Crossa6845402020-11-16 15:08:19 -0800689// addTargetProperties annotates a variant with the Target is is being compiled for, the list
690// of additional Targets it is supporting (if any), and whether it is the primary Target for
691// the module.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000692func addTargetProperties(m Module, target Target, multiTargets []Target, primaryTarget bool) {
693 m.base().commonProperties.CompileTarget = target
694 m.base().commonProperties.CompileMultiTargets = multiTargets
695 m.base().commonProperties.CompilePrimary = primaryTarget
Cole Faust0aa21cc2024-03-20 12:28:03 -0700696 m.base().commonProperties.ArchReady = true
Paul Duffin1356d8c2020-02-25 19:26:33 +0000697}
698
Colin Crossa6845402020-11-16 15:08:19 -0800699// decodeMultilib returns the appropriate compile_multilib property for the module, or the default
700// multilib from the factory's call to InitAndroidArchModule if none was set. For modules that
701// called InitAndroidMultiTargetsArchModule it always returns "common" for multilib, and returns
702// the actual multilib in extraMultilib.
Christopher Ferris98f10222022-07-13 23:16:52 -0700703func decodeMultilib(base *ModuleBase, os OsType, ignorePrefer32OnDevice bool) (multilib, extraMultilib string) {
Colin Crossa6845402020-11-16 15:08:19 -0800704 // First check the "android.compile_multilib" or "host.compile_multilib" properties.
Dan Willemsen47450072021-10-19 20:24:49 -0700705 switch os.Class {
Colin Crossee0bc3b2018-10-02 22:01:37 -0700706 case Device:
707 multilib = String(base.commonProperties.Target.Android.Compile_multilib)
Jiyong Park1613e552020-09-14 19:43:17 +0900708 case Host:
Colin Crossee0bc3b2018-10-02 22:01:37 -0700709 multilib = String(base.commonProperties.Target.Host.Compile_multilib)
710 }
Colin Crossa6845402020-11-16 15:08:19 -0800711
712 // If those aren't set, try the "compile_multilib" property.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700713 if multilib == "" {
714 multilib = String(base.commonProperties.Compile_multilib)
715 }
Colin Crossa6845402020-11-16 15:08:19 -0800716
717 // If that wasn't set, use the default multilib set by the factory.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700718 if multilib == "" {
719 multilib = base.commonProperties.Default_multilib
720 }
721
Christopher Ferris98f10222022-07-13 23:16:52 -0700722 // If a device is configured with multiple targets, this option
723 // force all device targets that prefer32 to be compiled only as
724 // the first target.
725 if ignorePrefer32OnDevice && os.Class == Device && (multilib == "prefer32" || multilib == "first_prefer32") {
726 multilib = "first"
727 }
728
Colin Crossee0bc3b2018-10-02 22:01:37 -0700729 if base.commonProperties.UseTargetVariants {
Dan Willemsen47450072021-10-19 20:24:49 -0700730 // Darwin has the concept of "universal binaries" which is implemented in Soong by
731 // building both x86_64 and arm64 variants, and having select module types know how to
732 // merge the outputs of their corresponding variants together into a final binary. Most
733 // module types don't need to understand this logic, as we only build a small portion
734 // of the tree for Darwin, and only module types writing macho files need to do the
735 // merging.
736 //
737 // This logic is not enabled for:
738 // "common", as it's not an arch-specific variant
739 // "32", as Darwin never has a 32-bit variant
740 // !UseTargetVariants, as the module has opted into handling the arch-specific logic on
741 // its own.
742 if os == Darwin && multilib != "common" && multilib != "32" {
743 if multilib == "common_first" {
744 multilib = "darwin_universal_common_first"
745 } else {
746 multilib = "darwin_universal"
747 }
748 }
749
Colin Crossee0bc3b2018-10-02 22:01:37 -0700750 return multilib, ""
751 } else {
752 // For app modules a single arch variant will be created per OS class which is expected to handle all the
753 // selected arches. Return the common-type as multilib and any Android.bp provided multilib as extraMultilib
754 if multilib == base.commonProperties.Default_multilib {
755 multilib = "first"
756 }
757 return base.commonProperties.Default_multilib, multilib
758 }
759}
760
Colin Crossa6845402020-11-16 15:08:19 -0800761// filterToArch takes a list of Targets and an ArchType, and returns a modified list that contains
Inseob Kim20fb5d42021-02-02 20:07:58 +0900762// only Targets that have the specified ArchTypes.
763func filterToArch(targets []Target, archs ...ArchType) []Target {
Colin Crossfb0c16e2019-11-20 17:12:35 -0800764 for i := 0; i < len(targets); i++ {
Inseob Kim20fb5d42021-02-02 20:07:58 +0900765 found := false
766 for _, arch := range archs {
767 if targets[i].Arch.ArchType == arch {
768 found = true
769 break
770 }
771 }
772 if !found {
Colin Crossfb0c16e2019-11-20 17:12:35 -0800773 targets = append(targets[:i], targets[i+1:]...)
774 i--
775 }
776 }
777 return targets
778}
779
Colin Crossc0f0eb82022-07-19 14:41:11 -0700780// filterHostCross takes a list of Targets and a hostCross value, and returns a modified list
781// that contains only Targets that have the specified HostCross.
782func filterHostCross(targets []Target, hostCross bool) []Target {
783 for i := 0; i < len(targets); i++ {
784 if targets[i].HostCross != hostCross {
785 targets = append(targets[:i], targets[i+1:]...)
786 i--
787 }
788 }
789 return targets
790}
791
Colin Crossa6845402020-11-16 15:08:19 -0800792// archPropRoot is a struct type used as the top level of the arch-specific properties. It
793// contains the "arch", "multilib", and "target" property structs. It is used to split up the
794// property structs to limit how much is allocated when a single arch-specific property group is
795// used. The types are interface{} because they will hold instances of runtime-created types.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800796type archPropRoot struct {
797 Arch, Multilib, Target interface{}
798}
799
Colin Crossa6845402020-11-16 15:08:19 -0800800// archPropTypeDesc holds the runtime-created types for the property structs to instantiate to
801// create an archPropRoot property struct.
802type archPropTypeDesc struct {
803 arch, multilib, target reflect.Type
804}
805
Colin Crosscbbd13f2020-01-17 14:08:22 -0800806// createArchPropTypeDesc takes a reflect.Type that is either a struct or a pointer to a struct, and
807// returns lists of reflect.Types that contains the arch-variant properties inside structs for each
808// arch, multilib and target property.
Colin Crossa6845402020-11-16 15:08:19 -0800809//
810// This is a relatively expensive operation, so the results are cached in the global
811// archPropTypeMap. It is constructed entirely based on compile-time data, so there is no need
812// to isolate the results between multiple tests running in parallel.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800813func createArchPropTypeDesc(props reflect.Type) []archPropTypeDesc {
Colin Crossb1d8c992020-01-21 11:43:29 -0800814 // Each property struct shard will be nested many times under the runtime generated arch struct,
815 // which can hit the limit of 64kB for the name of runtime generated structs. They are nested
816 // 97 times now, which may grow in the future, plus there is some overhead for the containing
817 // type. This number may need to be reduced if too many are added, but reducing it too far
818 // could cause problems if a single deeply nested property no longer fits in the name.
819 const maxArchTypeNameSize = 500
820
Colin Crossa6845402020-11-16 15:08:19 -0800821 // Convert the type to a new set of types that contains only the arch-specific properties
Usta Shrestha0b52d832022-02-04 21:37:39 -0500822 // (those that are tagged with `android:"arch_variant"`), and sharded into multiple types
Colin Crossa6845402020-11-16 15:08:19 -0800823 // to keep the runtime-generated names under the limit.
Colin Crossb1d8c992020-01-21 11:43:29 -0800824 propShards, _ := proptools.FilterPropertyStructSharded(props, maxArchTypeNameSize, filterArchStruct)
Colin Crossa6845402020-11-16 15:08:19 -0800825
826 // If the type has no arch-specific properties there is nothing to do.
Colin Crosscb988072019-01-24 14:58:11 -0800827 if len(propShards) == 0 {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700828 return nil
829 }
830
Colin Crosscbbd13f2020-01-17 14:08:22 -0800831 var ret []archPropTypeDesc
Colin Crossc17727d2018-10-24 12:42:09 -0700832 for _, props := range propShards {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700833
Colin Crossa6845402020-11-16 15:08:19 -0800834 // variantFields takes a list of variant property field names and returns a list the
835 // StructFields with the names and the type of the current shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700836 variantFields := func(names []string) []reflect.StructField {
837 ret := make([]reflect.StructField, len(names))
Dan Willemsenb1957a52016-06-23 23:44:54 -0700838
Colin Crossc17727d2018-10-24 12:42:09 -0700839 for i, name := range names {
840 ret[i].Name = name
841 ret[i].Type = props
Dan Willemsen866b5632017-09-22 12:28:24 -0700842 }
Colin Crossc17727d2018-10-24 12:42:09 -0700843
844 return ret
845 }
846
Colin Crossa6845402020-11-16 15:08:19 -0800847 // Create a type that contains the properties in this shard repeated for each
848 // architecture, architecture variant, and architecture feature.
Colin Crossc17727d2018-10-24 12:42:09 -0700849 archFields := make([]reflect.StructField, len(archTypeList))
850 for i, arch := range archTypeList {
Colin Crossa6845402020-11-16 15:08:19 -0800851 var variants []string
Colin Crossc17727d2018-10-24 12:42:09 -0700852
853 for _, archVariant := range archVariants[arch] {
854 archVariant := variantReplacer.Replace(archVariant)
855 variants = append(variants, proptools.FieldNameForProperty(archVariant))
856 }
Liz Kammer2c2afe22022-02-11 11:35:03 -0500857 for _, cpuVariant := range cpuVariants[arch] {
858 cpuVariant := variantReplacer.Replace(cpuVariant)
859 variants = append(variants, proptools.FieldNameForProperty(cpuVariant))
860 }
Colin Crossc17727d2018-10-24 12:42:09 -0700861 for _, feature := range archFeatures[arch] {
862 feature := variantReplacer.Replace(feature)
863 variants = append(variants, proptools.FieldNameForProperty(feature))
864 }
865
Colin Crossa6845402020-11-16 15:08:19 -0800866 // Create the StructFields for each architecture variant architecture feature
867 // (e.g. "arch.arm.cortex-a53" or "arch.arm.neon").
Colin Crossc17727d2018-10-24 12:42:09 -0700868 fields := variantFields(variants)
869
Colin Crossa6845402020-11-16 15:08:19 -0800870 // Create the StructField for the architecture itself (e.g. "arch.arm"). The special
871 // "BlueprintEmbed" name is used by Blueprint to put the properties in the
872 // parent struct.
Colin Crossc17727d2018-10-24 12:42:09 -0700873 fields = append([]reflect.StructField{{
874 Name: "BlueprintEmbed",
875 Type: props,
876 Anonymous: true,
877 }}, fields...)
878
879 archFields[i] = reflect.StructField{
880 Name: arch.Field,
881 Type: reflect.StructOf(fields),
882 }
883 }
Colin Crossa6845402020-11-16 15:08:19 -0800884
885 // Create the type of the "arch" property struct for this shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700886 archType := reflect.StructOf(archFields)
887
Colin Crossa6845402020-11-16 15:08:19 -0800888 // Create the type for the "multilib" property struct for this shard, containing the
889 // "multilib.lib32" and "multilib.lib64" property structs.
Colin Crossc17727d2018-10-24 12:42:09 -0700890 multilibType := reflect.StructOf(variantFields([]string{"Lib32", "Lib64"}))
891
Colin Crossa6845402020-11-16 15:08:19 -0800892 // Start with a list of the special targets
Colin Crossc17727d2018-10-24 12:42:09 -0700893 targets := []string{
894 "Host",
895 "Android64",
896 "Android32",
897 "Bionic",
Colin Cross528d67e2021-07-23 22:23:07 +0000898 "Glibc",
899 "Musl",
Colin Crossc17727d2018-10-24 12:42:09 -0700900 "Linux",
Colin Crossa98d36d2022-03-07 14:39:49 -0800901 "Host_linux",
Colin Crossc17727d2018-10-24 12:42:09 -0700902 "Not_windows",
903 "Arm_on_x86",
904 "Arm_on_x86_64",
Victor Khimenkoc26fcf42020-05-07 22:16:33 +0200905 "Native_bridge",
Colin Crossc17727d2018-10-24 12:42:09 -0700906 }
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000907 for _, os := range osTypeList {
Colin Crossa6845402020-11-16 15:08:19 -0800908 // Add all the OSes.
Colin Crossc17727d2018-10-24 12:42:09 -0700909 targets = append(targets, os.Field)
910
Colin Crossa6845402020-11-16 15:08:19 -0800911 // Add the OS/Arch combinations, e.g. "android_arm64".
Colin Crossc17727d2018-10-24 12:42:09 -0700912 for _, archType := range osArchTypeMap[os] {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400913 targets = append(targets, GetCompoundTargetField(os, archType))
Colin Crossc17727d2018-10-24 12:42:09 -0700914
Colin Cross1aa45b02022-02-10 10:33:10 -0800915 // Also add the special "linux_<arch>", "bionic_<arch>" , "glibc_<arch>", and
916 // "musl_<arch>" property structs.
Colin Crossc17727d2018-10-24 12:42:09 -0700917 if os.Linux() {
918 target := "Linux_" + archType.Name
919 if !InList(target, targets) {
920 targets = append(targets, target)
921 }
922 }
Colin Crossa98d36d2022-03-07 14:39:49 -0800923 if os.Linux() && os.Class == Host {
924 target := "Host_linux_" + archType.Name
925 if !InList(target, targets) {
926 targets = append(targets, target)
927 }
928 }
Colin Crossc17727d2018-10-24 12:42:09 -0700929 if os.Bionic() {
930 target := "Bionic_" + archType.Name
931 if !InList(target, targets) {
932 targets = append(targets, target)
933 }
Dan Willemsen866b5632017-09-22 12:28:24 -0700934 }
Colin Cross1aa45b02022-02-10 10:33:10 -0800935 if os == Linux {
936 target := "Glibc_" + archType.Name
937 if !InList(target, targets) {
938 targets = append(targets, target)
939 }
940 }
941 if os == LinuxMusl {
942 target := "Musl_" + archType.Name
943 if !InList(target, targets) {
944 targets = append(targets, target)
945 }
946 }
Dan Willemsen866b5632017-09-22 12:28:24 -0700947 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700948 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700949
Colin Crossa6845402020-11-16 15:08:19 -0800950 // Create the type for the "target" property struct for this shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700951 targetType := reflect.StructOf(variantFields(targets))
Colin Crosscbbd13f2020-01-17 14:08:22 -0800952
Colin Crossa6845402020-11-16 15:08:19 -0800953 // Return a descriptor of the 3 runtime-created types.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800954 ret = append(ret, archPropTypeDesc{
955 arch: reflect.PtrTo(archType),
956 multilib: reflect.PtrTo(multilibType),
957 target: reflect.PtrTo(targetType),
958 })
Colin Crossc17727d2018-10-24 12:42:09 -0700959 }
960 return ret
Dan Willemsenb1957a52016-06-23 23:44:54 -0700961}
962
Colin Crossa6845402020-11-16 15:08:19 -0800963// variantReplacer converts architecture variant or architecture feature names into names that
964// are valid for an Android.bp file.
965var variantReplacer = strings.NewReplacer("-", "_", ".", "_")
966
967// filterArchStruct returns true if the given field is an architecture specific property.
Colin Cross74449102019-09-25 11:26:40 -0700968func filterArchStruct(field reflect.StructField, prefix string) (bool, reflect.StructField) {
969 if proptools.HasTag(field, "android", "arch_variant") {
970 // The arch_variant field isn't necessary past this point
971 // Instead of wasting space, just remove it. Go also has a
972 // 16-bit limit on structure name length. The name is constructed
973 // based on the Go source representation of the structure, so
974 // the tag names count towards that length.
Colin Crossb4fecbf2020-01-21 11:38:47 -0800975
976 androidTag := field.Tag.Get("android")
977 values := strings.Split(androidTag, ",")
978
979 if string(field.Tag) != `android:"`+strings.Join(values, ",")+`"` {
980 panic(fmt.Errorf("unexpected tag format %q", field.Tag))
Colin Cross74449102019-09-25 11:26:40 -0700981 }
Colin Crossb4fecbf2020-01-21 11:38:47 -0800982 // these tags don't need to be present in the runtime generated struct type.
Liz Kammerff966b12022-07-29 10:49:16 -0400983 values = RemoveListFromList(values, []string{"arch_variant", "variant_prepend", "path"})
984 if len(values) > 0 {
Colin Crossb4fecbf2020-01-21 11:38:47 -0800985 panic(fmt.Errorf("unknown tags %q in field %q", values, prefix+field.Name))
986 }
987
Liz Kammerff966b12022-07-29 10:49:16 -0400988 field.Tag = ``
Colin Cross74449102019-09-25 11:26:40 -0700989 return true, field
990 }
991 return false, field
992}
993
Colin Crossa6845402020-11-16 15:08:19 -0800994// archPropTypeMap contains a cache of the results of createArchPropTypeDesc for each type. It is
995// shared across all Contexts, but is constructed based only on compile-time information so there
996// is no risk of contaminating one Context with data from another.
Dan Willemsenb1957a52016-06-23 23:44:54 -0700997var archPropTypeMap OncePer
998
Colin Crossa6845402020-11-16 15:08:19 -0800999// initArchModule adds the architecture-specific property structs to a Module.
1000func initArchModule(m Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001001
1002 base := m.base()
1003
Ustaeabf0f32021-12-06 15:17:23 -05001004 if len(base.archProperties) != 0 {
1005 panic(fmt.Errorf("module %s already has archProperties", m.Name()))
1006 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001007
Ustaeabf0f32021-12-06 15:17:23 -05001008 getStructType := func(properties interface{}) reflect.Type {
Colin Cross3f40fa42015-01-30 17:27:36 -08001009 propertiesValue := reflect.ValueOf(properties)
Colin Cross62496a02016-08-08 15:49:17 -07001010 t := propertiesValue.Type()
Colin Cross3f40fa42015-01-30 17:27:36 -08001011 if propertiesValue.Kind() != reflect.Ptr {
Colin Crossca860ac2016-01-04 14:34:37 -08001012 panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
1013 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -08001014 }
1015
1016 propertiesValue = propertiesValue.Elem()
1017 if propertiesValue.Kind() != reflect.Struct {
Ustaeabf0f32021-12-06 15:17:23 -05001018 panic(fmt.Errorf("properties must be a pointer to a struct, got a pointer to %T",
Colin Crossca860ac2016-01-04 14:34:37 -08001019 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -08001020 }
Ustaeabf0f32021-12-06 15:17:23 -05001021 return t
1022 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001023
Usta851a3272022-01-05 23:42:33 -05001024 for _, properties := range m.GetProperties() {
Ustaeabf0f32021-12-06 15:17:23 -05001025 t := getStructType(properties)
Colin Crossa6845402020-11-16 15:08:19 -08001026 // Get or create the arch-specific property struct types for this property struct type.
Colin Cross571cccf2019-02-04 11:22:08 -08001027 archPropTypes := archPropTypeMap.Once(NewCustomOnceKey(t), func() interface{} {
Colin Crosscbbd13f2020-01-17 14:08:22 -08001028 return createArchPropTypeDesc(t)
1029 }).([]archPropTypeDesc)
Colin Cross3f40fa42015-01-30 17:27:36 -08001030
Colin Crossa6845402020-11-16 15:08:19 -08001031 // Instantiate one of each arch-specific property struct type and add it to the
1032 // properties for the Module.
Colin Crossc17727d2018-10-24 12:42:09 -07001033 var archProperties []interface{}
1034 for _, t := range archPropTypes {
Colin Crosscbbd13f2020-01-17 14:08:22 -08001035 archProperties = append(archProperties, &archPropRoot{
1036 Arch: reflect.Zero(t.arch).Interface(),
1037 Multilib: reflect.Zero(t.multilib).Interface(),
1038 Target: reflect.Zero(t.target).Interface(),
1039 })
Dan Willemsenb1957a52016-06-23 23:44:54 -07001040 }
Colin Crossc17727d2018-10-24 12:42:09 -07001041 base.archProperties = append(base.archProperties, archProperties)
1042 m.AddProperties(archProperties...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001043 }
1044
Colin Cross3f40fa42015-01-30 17:27:36 -08001045}
1046
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001047func maybeBlueprintEmbed(src reflect.Value) reflect.Value {
Colin Crossa6845402020-11-16 15:08:19 -08001048 // If the value of the field is a struct (as opposed to a pointer to a struct) then step
1049 // into the BlueprintEmbed field.
Dan Willemsenb1957a52016-06-23 23:44:54 -07001050 if src.Kind() == reflect.Struct {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001051 return src.FieldByName("BlueprintEmbed")
1052 } else {
1053 return src
Colin Cross06a931b2015-10-28 17:23:31 -07001054 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001055}
1056
1057// Merges the property struct in srcValue into dst.
Liz Kammerb6dbc872021-05-14 15:14:40 -04001058func mergePropertyStruct(ctx ArchVariantContext, dst interface{}, srcValue reflect.Value) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001059 src := maybeBlueprintEmbed(srcValue).Interface()
Colin Cross06a931b2015-10-28 17:23:31 -07001060
Colin Crossa6845402020-11-16 15:08:19 -08001061 // order checks the `android:"variant_prepend"` tag to handle properties where the
1062 // arch-specific value needs to come before the generic value, for example for lists of
1063 // include directories.
Colin Cross1e7e0432024-02-02 10:59:50 -08001064 order := func(dstField, srcField reflect.StructField) (proptools.Order, error) {
Colin Cross6ee75b62016-05-05 15:57:15 -07001065 if proptools.HasTag(dstField, "android", "variant_prepend") {
1066 return proptools.Prepend, nil
1067 } else {
1068 return proptools.Append, nil
1069 }
1070 }
1071
Colin Crossa6845402020-11-16 15:08:19 -08001072 // Squash the located property struct into the destination property struct.
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001073 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src, nil, order)
Colin Cross06a931b2015-10-28 17:23:31 -07001074 if err != nil {
1075 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
1076 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
1077 } else {
1078 panic(err)
1079 }
1080 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001081}
Colin Cross85a88972015-11-23 13:29:51 -08001082
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001083// Returns the immediate child of the input property struct that corresponds to
1084// the sub-property "field".
Liz Kammerb6dbc872021-05-14 15:14:40 -04001085func getChildPropertyStruct(ctx ArchVariantContext,
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001086 src reflect.Value, field, userFriendlyField string) (reflect.Value, bool) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001087
1088 // Step into non-nil pointers to structs in the src value.
1089 if src.Kind() == reflect.Ptr {
1090 if src.IsNil() {
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001091 return reflect.Value{}, false
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001092 }
1093 src = src.Elem()
1094 }
1095
1096 // Find the requested field in the src struct.
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001097 child := src.FieldByName(proptools.FieldNameForProperty(field))
1098 if !child.IsValid() {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001099 ctx.ModuleErrorf("field %q does not exist", userFriendlyField)
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001100 return reflect.Value{}, false
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001101 }
1102
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001103 if child.IsZero() {
1104 return reflect.Value{}, false
1105 }
1106
1107 return child, true
Colin Cross06a931b2015-10-28 17:23:31 -07001108}
1109
Colin Crossa6845402020-11-16 15:08:19 -08001110// Squash the appropriate OS-specific property structs into the matching top level property structs
1111// based on the CompileOS value that was annotated on the variant.
Colin Crossa195f912019-10-16 11:07:20 -07001112func (m *ModuleBase) setOSProperties(ctx BottomUpMutatorContext) {
1113 os := m.commonProperties.CompileOS
1114
Ustadca02192021-12-20 12:56:46 -05001115 for i := range m.archProperties {
Usta851a3272022-01-05 23:42:33 -05001116 genProps := m.GetProperties()[i]
Colin Crossa195f912019-10-16 11:07:20 -07001117 if m.archProperties[i] == nil {
1118 continue
1119 }
1120 for _, archProperties := range m.archProperties[i] {
1121 archPropValues := reflect.ValueOf(archProperties).Elem()
1122
Colin Crosscbbd13f2020-01-17 14:08:22 -08001123 targetProp := archPropValues.FieldByName("Target").Elem()
Colin Crossa195f912019-10-16 11:07:20 -07001124
1125 // Handle host-specific properties in the form:
1126 // target: {
1127 // host: {
1128 // key: value,
1129 // },
1130 // },
Jiyong Park1613e552020-09-14 19:43:17 +09001131 if os.Class == Host {
Colin Crossa195f912019-10-16 11:07:20 -07001132 field := "Host"
1133 prefix := "target.host"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001134 if hostProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1135 mergePropertyStruct(ctx, genProps, hostProperties)
1136 }
Colin Crossa195f912019-10-16 11:07:20 -07001137 }
1138
1139 // Handle target OS generalities of the form:
1140 // target: {
1141 // bionic: {
1142 // key: value,
1143 // },
1144 // }
1145 if os.Linux() {
1146 field := "Linux"
1147 prefix := "target.linux"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001148 if linuxProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1149 mergePropertyStruct(ctx, genProps, linuxProperties)
1150 }
Colin Crossa195f912019-10-16 11:07:20 -07001151 }
1152
Colin Crossa98d36d2022-03-07 14:39:49 -08001153 if os.Linux() && os.Class == Host {
1154 field := "Host_linux"
1155 prefix := "target.host_linux"
1156 if linuxProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1157 mergePropertyStruct(ctx, genProps, linuxProperties)
1158 }
1159 }
1160
Colin Crossa195f912019-10-16 11:07:20 -07001161 if os.Bionic() {
1162 field := "Bionic"
1163 prefix := "target.bionic"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001164 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1165 mergePropertyStruct(ctx, genProps, bionicProperties)
1166 }
Colin Crossa195f912019-10-16 11:07:20 -07001167 }
1168
Colin Cross528d67e2021-07-23 22:23:07 +00001169 if os == Linux {
1170 field := "Glibc"
1171 prefix := "target.glibc"
1172 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1173 mergePropertyStruct(ctx, genProps, bionicProperties)
1174 }
1175 }
1176
1177 if os == LinuxMusl {
1178 field := "Musl"
1179 prefix := "target.musl"
1180 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1181 mergePropertyStruct(ctx, genProps, bionicProperties)
1182 }
Colin Cross528d67e2021-07-23 22:23:07 +00001183 }
1184
Colin Crossa195f912019-10-16 11:07:20 -07001185 // Handle target OS properties in the form:
1186 // target: {
1187 // linux_glibc: {
1188 // key: value,
1189 // },
1190 // not_windows: {
1191 // key: value,
1192 // },
1193 // android {
1194 // key: value,
1195 // },
1196 // },
1197 field := os.Field
1198 prefix := "target." + os.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001199 if osProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1200 mergePropertyStruct(ctx, genProps, osProperties)
1201 }
Colin Crossa195f912019-10-16 11:07:20 -07001202
Jiyong Park1613e552020-09-14 19:43:17 +09001203 if os.Class == Host && os != Windows {
Colin Crossa195f912019-10-16 11:07:20 -07001204 field := "Not_windows"
1205 prefix := "target.not_windows"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001206 if notWindowsProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1207 mergePropertyStruct(ctx, genProps, notWindowsProperties)
1208 }
Colin Crossa195f912019-10-16 11:07:20 -07001209 }
1210
1211 // Handle 64-bit device properties in the form:
1212 // target {
1213 // android64 {
1214 // key: value,
1215 // },
1216 // android32 {
1217 // key: value,
1218 // },
1219 // },
1220 // WARNING: this is probably not what you want to use in your blueprints file, it selects
1221 // options for all targets on a device that supports 64-bit binaries, not just the targets
1222 // that are being compiled for 64-bit. Its expected use case is binaries like linker and
1223 // debuggerd that need to know when they are a 32-bit process running on a 64-bit device
1224 if os.Class == Device {
1225 if ctx.Config().Android64() {
1226 field := "Android64"
1227 prefix := "target.android64"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001228 if android64Properties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1229 mergePropertyStruct(ctx, genProps, android64Properties)
1230 }
Colin Crossa195f912019-10-16 11:07:20 -07001231 } else {
1232 field := "Android32"
1233 prefix := "target.android32"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001234 if android32Properties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1235 mergePropertyStruct(ctx, genProps, android32Properties)
1236 }
Colin Crossa195f912019-10-16 11:07:20 -07001237 }
1238 }
1239 }
1240 }
1241}
1242
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001243// Returns the struct containing the properties specific to the given
1244// architecture type. These look like this in Blueprint files:
Colin Crossd079e0b2022-08-16 10:27:33 -07001245//
1246// arch: {
1247// arm64: {
1248// key: value,
1249// },
1250// },
1251//
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001252// This struct will also contain sub-structs containing to the architecture/CPU
1253// variants and features that themselves contain properties specific to those.
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001254func getArchTypeStruct(ctx ArchVariantContext, archProperties interface{}, archType ArchType) (reflect.Value, bool) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001255 archPropValues := reflect.ValueOf(archProperties).Elem()
1256 archProp := archPropValues.FieldByName("Arch").Elem()
1257 prefix := "arch." + archType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001258 return getChildPropertyStruct(ctx, archProp, archType.Name, prefix)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001259}
1260
1261// Returns the struct containing the properties specific to a given multilib
1262// value. These look like this in the Blueprint file:
Colin Crossd079e0b2022-08-16 10:27:33 -07001263//
1264// multilib: {
1265// lib32: {
1266// key: value,
1267// },
1268// },
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001269func getMultilibStruct(ctx ArchVariantContext, archProperties interface{}, archType ArchType) (reflect.Value, bool) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001270 archPropValues := reflect.ValueOf(archProperties).Elem()
1271 multilibProp := archPropValues.FieldByName("Multilib").Elem()
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001272 return getChildPropertyStruct(ctx, multilibProp, archType.Multilib, "multilib."+archType.Multilib)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001273}
1274
Liz Kammer9abd62d2021-05-21 08:37:59 -04001275func GetCompoundTargetField(os OsType, arch ArchType) string {
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04001276 return os.Field + "_" + arch.Name
1277}
1278
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001279// Returns the structs corresponding to the properties specific to the given
1280// architecture and OS in archProperties.
1281func getArchProperties(ctx BaseMutatorContext, archProperties interface{}, arch Arch, os OsType, nativeBridgeEnabled bool) []reflect.Value {
1282 result := make([]reflect.Value, 0)
1283 archPropValues := reflect.ValueOf(archProperties).Elem()
1284
1285 targetProp := archPropValues.FieldByName("Target").Elem()
1286
1287 archType := arch.ArchType
1288
1289 if arch.ArchType != Common {
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001290 archStruct, ok := getArchTypeStruct(ctx, archProperties, arch.ArchType)
1291 if ok {
1292 result = append(result, archStruct)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001293
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001294 // Handle arch-variant-specific properties in the form:
1295 // arch: {
1296 // arm: {
1297 // variant: {
1298 // key: value,
1299 // },
1300 // },
1301 // },
1302 v := variantReplacer.Replace(arch.ArchVariant)
1303 if v != "" {
1304 prefix := "arch." + archType.Name + "." + v
1305 if variantProperties, ok := getChildPropertyStruct(ctx, archStruct, v, prefix); ok {
1306 result = append(result, variantProperties)
1307 }
1308 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001309
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001310 // Handle cpu-variant-specific properties in the form:
1311 // arch: {
1312 // arm: {
1313 // variant: {
1314 // key: value,
1315 // },
1316 // },
1317 // },
1318 if arch.CpuVariant != arch.ArchVariant {
1319 c := variantReplacer.Replace(arch.CpuVariant)
1320 if c != "" {
1321 prefix := "arch." + archType.Name + "." + c
1322 if cpuVariantProperties, ok := getChildPropertyStruct(ctx, archStruct, c, prefix); ok {
1323 result = append(result, cpuVariantProperties)
1324 }
1325 }
1326 }
1327
1328 // Handle arch-feature-specific properties in the form:
1329 // arch: {
1330 // arm: {
1331 // feature: {
1332 // key: value,
1333 // },
1334 // },
1335 // },
1336 for _, feature := range arch.ArchFeatures {
1337 prefix := "arch." + archType.Name + "." + feature
1338 if featureProperties, ok := getChildPropertyStruct(ctx, archStruct, feature, prefix); ok {
1339 result = append(result, featureProperties)
1340 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001341 }
1342 }
1343
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001344 if multilibProperties, ok := getMultilibStruct(ctx, archProperties, archType); ok {
1345 result = append(result, multilibProperties)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001346 }
1347
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001348 // Handle combined OS-feature and arch specific properties in the form:
1349 // target: {
1350 // bionic_x86: {
1351 // key: value,
1352 // },
1353 // }
1354 if os.Linux() {
1355 field := "Linux_" + arch.ArchType.Name
1356 userFriendlyField := "target.linux_" + arch.ArchType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001357 if linuxProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1358 result = append(result, linuxProperties)
1359 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001360 }
1361
1362 if os.Bionic() {
1363 field := "Bionic_" + archType.Name
1364 userFriendlyField := "target.bionic_" + archType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001365 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1366 result = append(result, bionicProperties)
1367 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001368 }
1369
1370 // Handle combined OS and arch specific properties in the form:
1371 // target: {
1372 // linux_glibc_x86: {
1373 // key: value,
1374 // },
1375 // linux_glibc_arm: {
1376 // key: value,
1377 // },
1378 // android_arm {
1379 // key: value,
1380 // },
1381 // android_x86 {
1382 // key: value,
1383 // },
1384 // },
Liz Kammer9abd62d2021-05-21 08:37:59 -04001385 field := GetCompoundTargetField(os, archType)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001386 userFriendlyField := "target." + os.Name + "_" + archType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001387 if osArchProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1388 result = append(result, osArchProperties)
1389 }
Colin Cross528d67e2021-07-23 22:23:07 +00001390
Colin Cross1aa45b02022-02-10 10:33:10 -08001391 if os == Linux {
1392 field := "Glibc_" + archType.Name
1393 userFriendlyField := "target.glibc_" + "_" + archType.Name
1394 if osArchProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1395 result = append(result, osArchProperties)
1396 }
1397 }
1398
Colin Cross528d67e2021-07-23 22:23:07 +00001399 if os == LinuxMusl {
Colin Cross1aa45b02022-02-10 10:33:10 -08001400 field := "Musl_" + archType.Name
1401 userFriendlyField := "target.musl_" + "_" + archType.Name
1402 if osArchProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1403 result = append(result, osArchProperties)
1404 }
Colin Cross528d67e2021-07-23 22:23:07 +00001405 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001406 }
1407
1408 // Handle arm on x86 properties in the form:
1409 // target {
1410 // arm_on_x86 {
1411 // key: value,
1412 // },
1413 // arm_on_x86_64 {
1414 // key: value,
1415 // },
1416 // },
1417 if os.Class == Device {
1418 if arch.ArchType == X86 && (hasArmAbi(arch) ||
1419 hasArmAndroidArch(ctx.Config().Targets[Android])) {
1420 field := "Arm_on_x86"
1421 userFriendlyField := "target.arm_on_x86"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001422 if armOnX86Properties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1423 result = append(result, armOnX86Properties)
1424 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001425 }
1426 if arch.ArchType == X86_64 && (hasArmAbi(arch) ||
1427 hasArmAndroidArch(ctx.Config().Targets[Android])) {
1428 field := "Arm_on_x86_64"
1429 userFriendlyField := "target.arm_on_x86_64"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001430 if armOnX8664Properties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1431 result = append(result, armOnX8664Properties)
1432 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001433 }
1434 if os == Android && nativeBridgeEnabled {
1435 userFriendlyField := "Native_bridge"
1436 prefix := "target.native_bridge"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001437 if nativeBridgeProperties, ok := getChildPropertyStruct(ctx, targetProp, userFriendlyField, prefix); ok {
1438 result = append(result, nativeBridgeProperties)
1439 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001440 }
1441 }
1442
1443 return result
1444}
1445
Colin Crossa6845402020-11-16 15:08:19 -08001446// Squash the appropriate arch-specific property structs into the matching top level property
1447// structs based on the CompileTarget value that was annotated on the variant.
Colin Cross4157e882019-06-06 16:57:04 -07001448func (m *ModuleBase) setArchProperties(ctx BottomUpMutatorContext) {
1449 arch := m.Arch()
1450 os := m.Os()
Colin Crossd3ba0392015-05-07 14:11:29 -07001451
Ustadca02192021-12-20 12:56:46 -05001452 for i := range m.archProperties {
Usta851a3272022-01-05 23:42:33 -05001453 genProps := m.GetProperties()[i]
Colin Cross4157e882019-06-06 16:57:04 -07001454 if m.archProperties[i] == nil {
Dan Willemsenb1957a52016-06-23 23:44:54 -07001455 continue
1456 }
Dan Willemsenb1957a52016-06-23 23:44:54 -07001457
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001458 propStructs := make([]reflect.Value, 0)
1459 for _, archProperty := range m.archProperties[i] {
1460 propStructShard := getArchProperties(ctx, archProperty, arch, os, m.Target().NativeBridge == NativeBridgeEnabled)
1461 propStructs = append(propStructs, propStructShard...)
1462 }
Dan Willemsenb1957a52016-06-23 23:44:54 -07001463
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001464 for _, propStruct := range propStructs {
1465 mergePropertyStruct(ctx, genProps, propStruct)
Colin Crossbb2e2b72016-12-08 17:23:53 -08001466 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001467 }
1468}
1469
Colin Cross0c66bc62021-07-20 09:47:41 -07001470// determineBuildOS stores the OS and architecture used for host targets used during the build into
Colin Cross528d67e2021-07-23 22:23:07 +00001471// config based on the runtime OS and architecture determined by Go and the product configuration.
Colin Cross0c66bc62021-07-20 09:47:41 -07001472func determineBuildOS(config *config) {
1473 config.BuildOS = func() OsType {
1474 switch runtime.GOOS {
1475 case "linux":
Colin Cross528d67e2021-07-23 22:23:07 +00001476 if Bool(config.productVariables.HostMusl) {
1477 return LinuxMusl
1478 }
Colin Cross0c66bc62021-07-20 09:47:41 -07001479 return Linux
1480 case "darwin":
1481 return Darwin
1482 default:
1483 panic(fmt.Sprintf("unsupported OS: %s", runtime.GOOS))
1484 }
1485 }()
1486
1487 config.BuildArch = func() ArchType {
1488 switch runtime.GOARCH {
1489 case "amd64":
1490 return X86_64
1491 default:
1492 panic(fmt.Sprintf("unsupported Arch: %s", runtime.GOARCH))
1493 }
1494 }()
1495
1496}
1497
Colin Crossa6845402020-11-16 15:08:19 -08001498// Convert the arch product variables into a list of targets for each OsType.
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001499func decodeTargetProductVariables(config *config) (map[OsType][]Target, error) {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001500 variables := config.productVariables
Dan Willemsen490fd492015-11-24 17:53:15 -08001501
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001502 targets := make(map[OsType][]Target)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001503 var targetErr error
1504
Liz Kammerb7f33662022-02-28 14:16:16 -05001505 type targetConfig struct {
1506 os OsType
1507 archName string
1508 archVariant *string
1509 cpuVariant *string
1510 abi []string
1511 nativeBridgeEnabled NativeBridgeSupport
1512 nativeBridgeHostArchName *string
1513 nativeBridgeRelativePath *string
1514 }
1515
1516 addTarget := func(target targetConfig) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001517 if targetErr != nil {
1518 return
Dan Willemsen490fd492015-11-24 17:53:15 -08001519 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001520
Liz Kammerb7f33662022-02-28 14:16:16 -05001521 arch, err := decodeArch(target.os, target.archName, target.archVariant, target.cpuVariant, target.abi)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001522 if err != nil {
1523 targetErr = err
1524 return
1525 }
Liz Kammerb7f33662022-02-28 14:16:16 -05001526 nativeBridgeRelativePathStr := String(target.nativeBridgeRelativePath)
1527 nativeBridgeHostArchNameStr := String(target.nativeBridgeHostArchName)
dimitry8d6dde82019-07-11 10:23:53 +02001528
1529 // Use guest arch as relative install path by default
Liz Kammerb7f33662022-02-28 14:16:16 -05001530 if target.nativeBridgeEnabled && nativeBridgeRelativePathStr == "" {
dimitry8d6dde82019-07-11 10:23:53 +02001531 nativeBridgeRelativePathStr = arch.ArchType.String()
1532 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001533
Jiyong Park1613e552020-09-14 19:43:17 +09001534 // A target is considered as HostCross if it's a host target which can't run natively on
1535 // the currently configured build machine (either because the OS is different or because of
1536 // the unsupported arch)
1537 hostCross := false
Liz Kammerb7f33662022-02-28 14:16:16 -05001538 if target.os.Class == Host {
Jiyong Park1613e552020-09-14 19:43:17 +09001539 var osSupported bool
Liz Kammerb7f33662022-02-28 14:16:16 -05001540 if target.os == config.BuildOS {
Jiyong Park1613e552020-09-14 19:43:17 +09001541 osSupported = true
Liz Kammerb7f33662022-02-28 14:16:16 -05001542 } else if config.BuildOS.Linux() && target.os.Linux() {
Jiyong Park1613e552020-09-14 19:43:17 +09001543 // LinuxBionic and Linux are compatible
1544 osSupported = true
1545 } else {
1546 osSupported = false
1547 }
1548
1549 var archSupported bool
1550 if arch.ArchType == Common {
1551 archSupported = true
1552 } else if arch.ArchType.Name == *variables.HostArch {
1553 archSupported = true
1554 } else if variables.HostSecondaryArch != nil && arch.ArchType.Name == *variables.HostSecondaryArch {
1555 archSupported = true
1556 } else {
1557 archSupported = false
1558 }
1559 if !osSupported || !archSupported {
1560 hostCross = true
1561 }
1562 }
1563
Liz Kammerb7f33662022-02-28 14:16:16 -05001564 targets[target.os] = append(targets[target.os],
Colin Crossa1ad8d12016-06-01 17:09:44 -07001565 Target{
Liz Kammerb7f33662022-02-28 14:16:16 -05001566 Os: target.os,
dimitry8d6dde82019-07-11 10:23:53 +02001567 Arch: arch,
Liz Kammerb7f33662022-02-28 14:16:16 -05001568 NativeBridge: target.nativeBridgeEnabled,
dimitry8d6dde82019-07-11 10:23:53 +02001569 NativeBridgeHostArchName: nativeBridgeHostArchNameStr,
1570 NativeBridgeRelativePath: nativeBridgeRelativePathStr,
Jiyong Park1613e552020-09-14 19:43:17 +09001571 HostCross: hostCross,
Colin Crossa1ad8d12016-06-01 17:09:44 -07001572 })
Dan Willemsen490fd492015-11-24 17:53:15 -08001573 }
1574
Colin Cross4225f652015-09-17 14:33:42 -07001575 if variables.HostArch == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001576 return nil, fmt.Errorf("No host primary architecture set")
Colin Cross4225f652015-09-17 14:33:42 -07001577 }
1578
Colin Crossa6845402020-11-16 15:08:19 -08001579 // The primary host target, which must always exist.
Liz Kammerb7f33662022-02-28 14:16:16 -05001580 addTarget(targetConfig{os: config.BuildOS, archName: *variables.HostArch, nativeBridgeEnabled: NativeBridgeDisabled})
Colin Cross4225f652015-09-17 14:33:42 -07001581
Colin Crossa6845402020-11-16 15:08:19 -08001582 // An optional secondary host target.
Colin Crosseeabb892015-11-20 13:07:51 -08001583 if variables.HostSecondaryArch != nil && *variables.HostSecondaryArch != "" {
Liz Kammerb7f33662022-02-28 14:16:16 -05001584 addTarget(targetConfig{os: config.BuildOS, archName: *variables.HostSecondaryArch, nativeBridgeEnabled: NativeBridgeDisabled})
Dan Willemsen490fd492015-11-24 17:53:15 -08001585 }
1586
Colin Crossa6845402020-11-16 15:08:19 -08001587 // Optional cross-compiled host targets, generally Windows.
Colin Crossff3ae9d2018-04-10 16:15:18 -07001588 if String(variables.CrossHost) != "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001589 crossHostOs := osByName(*variables.CrossHost)
1590 if crossHostOs == NoOsType {
1591 return nil, fmt.Errorf("Unknown cross host OS %q", *variables.CrossHost)
1592 }
1593
Colin Crossff3ae9d2018-04-10 16:15:18 -07001594 if String(variables.CrossHostArch) == "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001595 return nil, fmt.Errorf("No cross-host primary architecture set")
Dan Willemsen490fd492015-11-24 17:53:15 -08001596 }
1597
Colin Crossa6845402020-11-16 15:08:19 -08001598 // The primary cross-compiled host target.
Liz Kammerb7f33662022-02-28 14:16:16 -05001599 addTarget(targetConfig{os: crossHostOs, archName: *variables.CrossHostArch, nativeBridgeEnabled: NativeBridgeDisabled})
Dan Willemsen490fd492015-11-24 17:53:15 -08001600
Colin Crossa6845402020-11-16 15:08:19 -08001601 // An optional secondary cross-compiled host target.
Dan Willemsen490fd492015-11-24 17:53:15 -08001602 if variables.CrossHostSecondaryArch != nil && *variables.CrossHostSecondaryArch != "" {
Liz Kammerb7f33662022-02-28 14:16:16 -05001603 addTarget(targetConfig{os: crossHostOs, archName: *variables.CrossHostSecondaryArch, nativeBridgeEnabled: NativeBridgeDisabled})
Dan Willemsen490fd492015-11-24 17:53:15 -08001604 }
1605 }
1606
Colin Crossa6845402020-11-16 15:08:19 -08001607 // Optional device targets
Dan Willemsen3f32f032016-07-11 14:36:48 -07001608 if variables.DeviceArch != nil && *variables.DeviceArch != "" {
Colin Crossa6845402020-11-16 15:08:19 -08001609 // The primary device target.
Liz Kammerb7f33662022-02-28 14:16:16 -05001610 addTarget(targetConfig{
1611 os: Android,
1612 archName: *variables.DeviceArch,
1613 archVariant: variables.DeviceArchVariant,
1614 cpuVariant: variables.DeviceCpuVariant,
1615 abi: variables.DeviceAbi,
1616 nativeBridgeEnabled: NativeBridgeDisabled,
1617 })
Colin Cross4225f652015-09-17 14:33:42 -07001618
Colin Crossa6845402020-11-16 15:08:19 -08001619 // An optional secondary device target.
Dan Willemsen3f32f032016-07-11 14:36:48 -07001620 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" {
Liz Kammerb7f33662022-02-28 14:16:16 -05001621 addTarget(targetConfig{
1622 os: Android,
1623 archName: *variables.DeviceSecondaryArch,
1624 archVariant: variables.DeviceSecondaryArchVariant,
1625 cpuVariant: variables.DeviceSecondaryCpuVariant,
1626 abi: variables.DeviceSecondaryAbi,
1627 nativeBridgeEnabled: NativeBridgeDisabled,
1628 })
Colin Cross4225f652015-09-17 14:33:42 -07001629 }
dimitry1f33e402019-03-26 12:39:31 +01001630
Colin Crossa6845402020-11-16 15:08:19 -08001631 // An optional NativeBridge device target.
dimitry1f33e402019-03-26 12:39:31 +01001632 if variables.NativeBridgeArch != nil && *variables.NativeBridgeArch != "" {
Liz Kammerb7f33662022-02-28 14:16:16 -05001633 addTarget(targetConfig{
1634 os: Android,
1635 archName: *variables.NativeBridgeArch,
1636 archVariant: variables.NativeBridgeArchVariant,
1637 cpuVariant: variables.NativeBridgeCpuVariant,
1638 abi: variables.NativeBridgeAbi,
1639 nativeBridgeEnabled: NativeBridgeEnabled,
1640 nativeBridgeHostArchName: variables.DeviceArch,
1641 nativeBridgeRelativePath: variables.NativeBridgeRelativePath,
1642 })
dimitry1f33e402019-03-26 12:39:31 +01001643 }
1644
Colin Crossa6845402020-11-16 15:08:19 -08001645 // An optional secondary NativeBridge device target.
dimitry1f33e402019-03-26 12:39:31 +01001646 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" &&
1647 variables.NativeBridgeSecondaryArch != nil && *variables.NativeBridgeSecondaryArch != "" {
Liz Kammerb7f33662022-02-28 14:16:16 -05001648 addTarget(targetConfig{
1649 os: Android,
1650 archName: *variables.NativeBridgeSecondaryArch,
1651 archVariant: variables.NativeBridgeSecondaryArchVariant,
1652 cpuVariant: variables.NativeBridgeSecondaryCpuVariant,
1653 abi: variables.NativeBridgeSecondaryAbi,
1654 nativeBridgeEnabled: NativeBridgeEnabled,
1655 nativeBridgeHostArchName: variables.DeviceSecondaryArch,
1656 nativeBridgeRelativePath: variables.NativeBridgeSecondaryRelativePath,
1657 })
dimitry1f33e402019-03-26 12:39:31 +01001658 }
Colin Cross4225f652015-09-17 14:33:42 -07001659 }
1660
Colin Crossa1ad8d12016-06-01 17:09:44 -07001661 if targetErr != nil {
1662 return nil, targetErr
1663 }
1664
1665 return targets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001666}
1667
Colin Crossbb2e2b72016-12-08 17:23:53 -08001668// hasArmAbi returns true if arch has at least one arm ABI
1669func hasArmAbi(arch Arch) bool {
Jaewoong Jung3aff5782020-02-11 07:54:35 -08001670 return PrefixInList(arch.Abi, "arm")
Colin Crossbb2e2b72016-12-08 17:23:53 -08001671}
1672
Lev Rumyantsev34581212021-10-13 09:47:59 -07001673// hasArmAndroidArch returns true if targets has at least
1674// one arm Android arch (possibly native bridged)
Colin Cross4247f0d2017-04-13 16:56:14 -07001675func hasArmAndroidArch(targets []Target) bool {
1676 for _, target := range targets {
Lev Rumyantsev34581212021-10-13 09:47:59 -07001677 if target.Os == Android &&
1678 (target.Arch.ArchType == Arm || target.Arch.ArchType == Arm64) {
Victor Khimenko5eb8ec12018-03-21 20:30:54 +01001679 return true
1680 }
1681 }
1682 return false
1683}
1684
Colin Crossa6845402020-11-16 15:08:19 -08001685// archConfig describes a built-in configuration.
Dan Albert4098deb2016-10-19 14:04:41 -07001686type archConfig struct {
Liz Kammer992918d2022-11-11 10:37:54 -05001687 Arch string `json:"arch"`
1688 ArchVariant string `json:"arch_variant"`
1689 CpuVariant string `json:"cpu_variant"`
1690 Abi []string `json:"abis"`
Dan Albert4098deb2016-10-19 14:04:41 -07001691}
1692
Elliott Hughesc55b5862022-10-27 23:46:22 +00001693// getNdkAbisConfig returns the list of archConfigs that are used for building
1694// the API stubs and static libraries that are included in the NDK.
Dan Albert4098deb2016-10-19 14:04:41 -07001695func getNdkAbisConfig() []archConfig {
1696 return []archConfig{
Tamas Petzbca786d2021-01-20 18:56:33 +01001697 {"arm64", "armv8-a-branchprot", "", []string{"arm64-v8a"}},
Elliott Hughesc55b5862022-10-27 23:46:22 +00001698 {"arm", "armv7-a-neon", "", []string{"armeabi-v7a"}},
Elliott Hughesf7d31092023-03-14 23:11:57 +00001699 {"riscv64", "", "", []string{"riscv64"}},
Dan Albert4098deb2016-10-19 14:04:41 -07001700 {"x86_64", "", "", []string{"x86_64"}},
Inseob Kim5219c0e2021-06-17 00:33:00 +09001701 {"x86", "", "", []string{"x86"}},
Dan Albert4098deb2016-10-19 14:04:41 -07001702 }
1703}
1704
Colin Crossa6845402020-11-16 15:08:19 -08001705// getAmlAbisConfig returns a list of archConfigs for the ABIs supported by mainline modules.
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001706func getAmlAbisConfig() []archConfig {
1707 return []archConfig{
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001708 {"arm64", "armv8-a", "", []string{"arm64-v8a"}},
Inseob Kim5219c0e2021-06-17 00:33:00 +09001709 {"arm", "armv7-a-neon", "", []string{"armeabi-v7a"}},
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001710 {"x86_64", "", "", []string{"x86_64"}},
Inseob Kim5219c0e2021-06-17 00:33:00 +09001711 {"x86", "", "", []string{"x86"}},
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001712 }
1713}
1714
Colin Crossa6845402020-11-16 15:08:19 -08001715// decodeArchSettings converts a list of archConfigs into a list of Targets for the given OsType.
Liz Kammerb7f33662022-02-28 14:16:16 -05001716func decodeAndroidArchSettings(archConfigs []archConfig) ([]Target, error) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001717 var ret []Target
Dan Willemsen322acaf2016-01-12 23:07:05 -08001718
Dan Albert4098deb2016-10-19 14:04:41 -07001719 for _, config := range archConfigs {
Liz Kammer992918d2022-11-11 10:37:54 -05001720 arch, err := decodeArch(Android, config.Arch, &config.ArchVariant,
1721 &config.CpuVariant, config.Abi)
Dan Willemsen322acaf2016-01-12 23:07:05 -08001722 if err != nil {
1723 return nil, err
1724 }
Colin Cross3b19f5d2019-09-17 14:45:31 -07001725
Colin Crossa1ad8d12016-06-01 17:09:44 -07001726 ret = append(ret, Target{
1727 Os: Android,
1728 Arch: arch,
1729 })
Dan Willemsen322acaf2016-01-12 23:07:05 -08001730 }
1731
1732 return ret, nil
1733}
1734
Colin Crossa6845402020-11-16 15:08:19 -08001735// decodeArch converts a set of strings from product variables into an Arch struct.
Colin Crossa74ca042019-01-31 14:31:51 -08001736func decodeArch(os OsType, arch string, archVariant, cpuVariant *string, abi []string) (Arch, error) {
Colin Crossa6845402020-11-16 15:08:19 -08001737 // Verify the arch is valid
Colin Crosseeabb892015-11-20 13:07:51 -08001738 archType, ok := archTypeMap[arch]
1739 if !ok {
1740 return Arch{}, fmt.Errorf("unknown arch %q", arch)
1741 }
Colin Cross4225f652015-09-17 14:33:42 -07001742
Colin Crosseeabb892015-11-20 13:07:51 -08001743 a := Arch{
Colin Cross4225f652015-09-17 14:33:42 -07001744 ArchType: archType,
Colin Crossa6845402020-11-16 15:08:19 -08001745 ArchVariant: String(archVariant),
1746 CpuVariant: String(cpuVariant),
Colin Crossa74ca042019-01-31 14:31:51 -08001747 Abi: abi,
Colin Crosseeabb892015-11-20 13:07:51 -08001748 }
1749
Colin Crossa6845402020-11-16 15:08:19 -08001750 // Convert generic arch variants into the empty string.
Colin Crosseeabb892015-11-20 13:07:51 -08001751 if a.ArchVariant == a.ArchType.Name || a.ArchVariant == "generic" {
1752 a.ArchVariant = ""
1753 }
1754
Colin Crossa6845402020-11-16 15:08:19 -08001755 // Convert generic CPU variants into the empty string.
Colin Crosseeabb892015-11-20 13:07:51 -08001756 if a.CpuVariant == a.ArchType.Name || a.CpuVariant == "generic" {
1757 a.CpuVariant = ""
1758 }
1759
Liz Kammer2c2afe22022-02-11 11:35:03 -05001760 if a.ArchVariant != "" {
1761 if validArchVariants := archVariants[archType]; !InList(a.ArchVariant, validArchVariants) {
1762 return Arch{}, fmt.Errorf("[%q] unknown arch variant %q, support variants: %q", archType, a.ArchVariant, validArchVariants)
1763 }
1764 }
1765
1766 if a.CpuVariant != "" {
1767 if validCpuVariants := cpuVariants[archType]; !InList(a.CpuVariant, validCpuVariants) {
1768 return Arch{}, fmt.Errorf("[%q] unknown cpu variant %q, support variants: %q", archType, a.CpuVariant, validCpuVariants)
1769 }
1770 }
1771
Colin Crossa6845402020-11-16 15:08:19 -08001772 // Filter empty ABIs out of the list.
Colin Crosseeabb892015-11-20 13:07:51 -08001773 for i := 0; i < len(a.Abi); i++ {
1774 if a.Abi[i] == "" {
1775 a.Abi = append(a.Abi[:i], a.Abi[i+1:]...)
1776 i--
1777 }
1778 }
1779
Liz Kammere8303bd2022-02-16 09:02:48 -05001780 // Set ArchFeatures from the arch type. for Android OS, other os-es do not specify features
1781 if os == Android {
1782 if featureMap, ok := androidArchFeatureMap[archType]; ok {
Dan Willemsen01a3c252019-01-11 19:02:16 -08001783 a.ArchFeatures = featureMap[a.ArchVariant]
1784 }
Colin Crossc5c24ad2015-11-20 15:35:00 -08001785 }
1786
Colin Crosseeabb892015-11-20 13:07:51 -08001787 return a, nil
Colin Cross4225f652015-09-17 14:33:42 -07001788}
1789
Colin Crossa6845402020-11-16 15:08:19 -08001790// filterMultilibTargets takes a list of Targets and a multilib value and returns a new list of
1791// Targets containing only those that have the given multilib value.
Colin Cross69617d32016-09-06 10:39:07 -07001792func filterMultilibTargets(targets []Target, multilib string) []Target {
1793 var ret []Target
1794 for _, t := range targets {
1795 if t.Arch.ArchType.Multilib == multilib {
1796 ret = append(ret, t)
1797 }
1798 }
1799 return ret
1800}
1801
Colin Crossa6845402020-11-16 15:08:19 -08001802// getCommonTargets returns the set of Os specific common architecture targets for each Os in a list
1803// of targets.
Nan Zhangdb0b9a32017-02-27 10:12:13 -08001804func getCommonTargets(targets []Target) []Target {
1805 var ret []Target
1806 set := make(map[string]bool)
1807
1808 for _, t := range targets {
1809 if _, found := set[t.Os.String()]; !found {
1810 set[t.Os.String()] = true
Colin Cross39a18142022-06-24 18:43:40 -07001811 common := commonTargetMap[t.Os.String()]
1812 common.HostCross = t.HostCross
1813 ret = append(ret, common)
Nan Zhangdb0b9a32017-02-27 10:12:13 -08001814 }
1815 }
1816
1817 return ret
1818}
1819
Sam Delmericocc271e22022-06-01 15:45:02 +00001820// 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 -07001821// that contains zero or one Target for each OsType and HostCross, selecting the one that matches
1822// the earliest filter.
Sam Delmericocc271e22022-06-01 15:45:02 +00001823func FirstTarget(targets []Target, filters ...string) []Target {
Jiyong Park22101982020-09-17 19:09:58 +09001824 // find the first target from each OS
1825 var ret []Target
Colin Crossc0f0eb82022-07-19 14:41:11 -07001826 type osHostCross struct {
1827 os OsType
1828 hostCross bool
1829 }
1830 set := make(map[osHostCross]bool)
Jiyong Park22101982020-09-17 19:09:58 +09001831
Colin Cross6b4a32d2017-12-05 13:42:45 -08001832 for _, filter := range filters {
1833 buildTargets := filterMultilibTargets(targets, filter)
Jiyong Park22101982020-09-17 19:09:58 +09001834 for _, t := range buildTargets {
Colin Crossc0f0eb82022-07-19 14:41:11 -07001835 key := osHostCross{t.Os, t.HostCross}
1836 if _, found := set[key]; !found {
1837 set[key] = true
Jiyong Park22101982020-09-17 19:09:58 +09001838 ret = append(ret, t)
1839 }
Colin Cross6b4a32d2017-12-05 13:42:45 -08001840 }
1841 }
Jiyong Park22101982020-09-17 19:09:58 +09001842 return ret
Colin Cross6b4a32d2017-12-05 13:42:45 -08001843}
1844
Colin Crossa6845402020-11-16 15:08:19 -08001845// decodeMultilibTargets uses the module's multilib setting to select one or more targets from a
1846// list of Targets.
Colin Crossee0bc3b2018-10-02 22:01:37 -07001847func decodeMultilibTargets(multilib string, targets []Target, prefer32 bool) ([]Target, error) {
Colin Crossa6845402020-11-16 15:08:19 -08001848 var buildTargets []Target
Colin Cross6b4a32d2017-12-05 13:42:45 -08001849
Colin Cross4225f652015-09-17 14:33:42 -07001850 switch multilib {
1851 case "common":
Colin Cross6b4a32d2017-12-05 13:42:45 -08001852 buildTargets = getCommonTargets(targets)
1853 case "common_first":
1854 buildTargets = getCommonTargets(targets)
1855 if prefer32 {
Sam Delmericocc271e22022-06-01 15:45:02 +00001856 buildTargets = append(buildTargets, FirstTarget(targets, "lib32", "lib64")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001857 } else {
Sam Delmericocc271e22022-06-01 15:45:02 +00001858 buildTargets = append(buildTargets, FirstTarget(targets, "lib64", "lib32")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001859 }
Colin Cross4225f652015-09-17 14:33:42 -07001860 case "both":
Colin Cross8b74d172016-09-13 09:59:14 -07001861 if prefer32 {
1862 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1863 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1864 } else {
1865 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1866 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1867 }
Colin Cross4225f652015-09-17 14:33:42 -07001868 case "32":
Colin Cross69617d32016-09-06 10:39:07 -07001869 buildTargets = filterMultilibTargets(targets, "lib32")
Colin Cross4225f652015-09-17 14:33:42 -07001870 case "64":
Colin Cross69617d32016-09-06 10:39:07 -07001871 buildTargets = filterMultilibTargets(targets, "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001872 case "first":
1873 if prefer32 {
Sam Delmericocc271e22022-06-01 15:45:02 +00001874 buildTargets = FirstTarget(targets, "lib32", "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001875 } else {
Sam Delmericocc271e22022-06-01 15:45:02 +00001876 buildTargets = FirstTarget(targets, "lib64", "lib32")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001877 }
Victor Chang9448e8f2020-09-14 15:34:16 +01001878 case "first_prefer32":
Sam Delmericocc271e22022-06-01 15:45:02 +00001879 buildTargets = FirstTarget(targets, "lib32", "lib64")
Colin Cross69617d32016-09-06 10:39:07 -07001880 case "prefer32":
Colin Cross3dceee32018-09-06 10:19:57 -07001881 buildTargets = filterMultilibTargets(targets, "lib32")
1882 if len(buildTargets) == 0 {
1883 buildTargets = filterMultilibTargets(targets, "lib64")
1884 }
Dan Willemsen47450072021-10-19 20:24:49 -07001885 case "darwin_universal":
1886 buildTargets = filterMultilibTargets(targets, "lib64")
1887 // Reverse the targets so that the first architecture can depend on the second
1888 // architecture module in order to merge the outputs.
Colin Crossb5e3f7d2023-07-06 15:37:53 -07001889 ReverseSliceInPlace(buildTargets)
Dan Willemsen47450072021-10-19 20:24:49 -07001890 case "darwin_universal_common_first":
1891 archTargets := filterMultilibTargets(targets, "lib64")
Colin Crossb5e3f7d2023-07-06 15:37:53 -07001892 ReverseSliceInPlace(archTargets)
Dan Willemsen47450072021-10-19 20:24:49 -07001893 buildTargets = append(getCommonTargets(targets), archTargets...)
Colin Cross4225f652015-09-17 14:33:42 -07001894 default:
Victor Chang9448e8f2020-09-14 15:34:16 +01001895 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 -07001896 multilib)
Colin Cross4225f652015-09-17 14:33:42 -07001897 }
1898
Colin Crossa1ad8d12016-06-01 17:09:44 -07001899 return buildTargets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001900}
Jingwen Chen5d864492021-02-24 07:20:12 -05001901
Chris Parsonsc424b762021-04-29 18:06:50 -04001902func (m *ModuleBase) getArchPropertySet(propertySet interface{}, archType ArchType) interface{} {
1903 archString := archType.Field
1904 for i := range m.archProperties {
1905 if m.archProperties[i] == nil {
1906 // Skip over nil properties
1907 continue
1908 }
1909
1910 // Not archProperties are usable; this function looks for properties of a very specific
1911 // form, and ignores the rest.
1912 for _, archProperty := range m.archProperties[i] {
1913 // archPropValue is a property struct, we are looking for the form:
1914 // `arch: { arm: { key: value, ... }}`
1915 archPropValue := reflect.ValueOf(archProperty).Elem()
1916
1917 // Unwrap src so that it should looks like a pointer to `arm: { key: value, ... }`
1918 src := archPropValue.FieldByName("Arch").Elem()
1919
1920 // Step into non-nil pointers to structs in the src value.
1921 if src.Kind() == reflect.Ptr {
1922 if src.IsNil() {
1923 continue
1924 }
1925 src = src.Elem()
1926 }
1927
1928 // Find the requested field (e.g. arm, x86) in the src struct.
1929 src = src.FieldByName(archString)
1930
1931 // We only care about structs.
1932 if !src.IsValid() || src.Kind() != reflect.Struct {
1933 continue
1934 }
1935
1936 // If the value of the field is a struct then step into the
1937 // BlueprintEmbed field. The special "BlueprintEmbed" name is
1938 // used by createArchPropTypeDesc to embed the arch properties
1939 // in the parent struct, so the src arch prop should be in this
1940 // field.
1941 //
1942 // See createArchPropTypeDesc for more details on how Arch-specific
1943 // module properties are processed from the nested props and written
1944 // into the module's archProperties.
1945 src = src.FieldByName("BlueprintEmbed")
1946
1947 // Clone the destination prop, since we want a unique prop struct per arch.
1948 propertySetClone := reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
1949
1950 // Copy the located property struct into the cloned destination property struct.
1951 err := proptools.ExtendMatchingProperties([]interface{}{propertySetClone}, src.Interface(), nil, proptools.OrderReplace)
1952 if err != nil {
1953 // This is fine, it just means the src struct doesn't match the type of propertySet.
1954 continue
1955 }
1956
1957 return propertySetClone
1958 }
1959 }
1960 // No property set was found specific to the given arch, so return an empty
1961 // property set.
1962 return reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
1963}
1964
1965// getMultilibPropertySet returns a property set struct matching the type of
1966// `propertySet`, containing multilib-specific module properties for the given architecture.
1967// If no multilib-specific properties exist for the given architecture, returns an empty property
1968// set matching `propertySet`'s type.
1969func (m *ModuleBase) getMultilibPropertySet(propertySet interface{}, archType ArchType) interface{} {
1970 // archType.Multilib is lowercase (for example, lib32) but property struct field is
1971 // capitalized, such as Lib32, so use strings.Title to capitalize it.
1972 multiLibString := strings.Title(archType.Multilib)
1973
1974 for i := range m.archProperties {
1975 if m.archProperties[i] == nil {
1976 // Skip over nil properties
1977 continue
1978 }
1979
1980 // Not archProperties are usable; this function looks for properties of a very specific
1981 // form, and ignores the rest.
1982 for _, archProperties := range m.archProperties[i] {
1983 // archPropValue is a property struct, we are looking for the form:
1984 // `multilib: { lib32: { key: value, ... }}`
1985 archPropValue := reflect.ValueOf(archProperties).Elem()
1986
1987 // Unwrap src so that it should looks like a pointer to `lib32: { key: value, ... }`
1988 src := archPropValue.FieldByName("Multilib").Elem()
1989
1990 // Step into non-nil pointers to structs in the src value.
1991 if src.Kind() == reflect.Ptr {
1992 if src.IsNil() {
1993 // Ignore nil pointers.
1994 continue
1995 }
1996 src = src.Elem()
1997 }
1998
1999 // Find the requested field (e.g. lib32) in the src struct.
2000 src = src.FieldByName(multiLibString)
2001
2002 // We only care about valid struct pointers.
2003 if !src.IsValid() || src.Kind() != reflect.Ptr || src.Elem().Kind() != reflect.Struct {
2004 continue
2005 }
2006
2007 // Get the zero value for the requested property set.
2008 propertySetClone := reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
2009
2010 // Copy the located property struct into the "zero" property set struct.
2011 err := proptools.ExtendMatchingProperties([]interface{}{propertySetClone}, src.Interface(), nil, proptools.OrderReplace)
2012
2013 if err != nil {
2014 // This is fine, it just means the src struct doesn't match.
2015 continue
2016 }
2017
2018 return propertySetClone
2019 }
2020 }
2021
2022 // There were no multilib properties specifically matching the given archtype.
2023 // Return zeroed value.
2024 return reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
2025}
2026
Liz Kammerb6dbc872021-05-14 15:14:40 -04002027// ArchVariantContext defines the limited context necessary to retrieve arch_variant properties.
2028type ArchVariantContext interface {
2029 ModuleErrorf(fmt string, args ...interface{})
2030 PropertyErrorf(property, fmt string, args ...interface{})
2031}
2032
Liz Kammer9abd62d2021-05-21 08:37:59 -04002033// ArchVariantProperties represents a map of arch-variant config strings to a property interface{}.
2034type ArchVariantProperties map[string]interface{}
2035
2036// ConfigurationAxisToArchVariantProperties represents a map of bazel.ConfigurationAxis to
2037// ArchVariantProperties, such that each independent arch-variant axis maps to the
2038// configs/properties for that axis.
2039type ConfigurationAxisToArchVariantProperties map[bazel.ConfigurationAxis]ArchVariantProperties
2040
2041// GetArchVariantProperties returns a ConfigurationAxisToArchVariantProperties where the
2042// arch-variant properties correspond to the values of the properties of the 'propertySet' struct
2043// that are specific to that axis/configuration. Each axis is independent, containing
2044// non-overlapping configs that correspond to the various "arch-variant" support, at this time:
Colin Crossd079e0b2022-08-16 10:27:33 -07002045//
2046// arches (including multilib)
2047// oses
2048// arch+os combinations
Jingwen Chen5d864492021-02-24 07:20:12 -05002049//
Liz Kammer9abd62d2021-05-21 08:37:59 -04002050// For example, passing a struct { Foo bool, Bar string } will return an interface{} that can be
2051// type asserted back into the same struct, containing the config-specific property value specified
2052// by the module if defined.
Chris Parsonsc424b762021-04-29 18:06:50 -04002053//
2054// Arch-specific properties may come from an arch stanza or a multilib stanza; properties
2055// in these stanzas are combined.
2056// For example: `arch: { x86: { Foo: ["bar"] } }, multilib: { lib32: {` Foo: ["baz"] } }`
2057// will result in `Foo: ["bar", "baz"]` being returned for architecture x86, if the given
2058// propertyset contains `Foo []string`.
Liz Kammer9abd62d2021-05-21 08:37:59 -04002059func (m *ModuleBase) GetArchVariantProperties(ctx ArchVariantContext, propertySet interface{}) ConfigurationAxisToArchVariantProperties {
Jingwen Chen5d864492021-02-24 07:20:12 -05002060 // Return value of the arch types to the prop values for that arch.
Liz Kammer9abd62d2021-05-21 08:37:59 -04002061 axisToProps := ConfigurationAxisToArchVariantProperties{}
Jingwen Chen5d864492021-02-24 07:20:12 -05002062
2063 // Nothing to do for non-arch-specific modules.
2064 if !m.ArchSpecific() {
Liz Kammer9abd62d2021-05-21 08:37:59 -04002065 return axisToProps
Jingwen Chen5d864492021-02-24 07:20:12 -05002066 }
2067
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002068 dstType := reflect.ValueOf(propertySet).Type()
2069 var archProperties []interface{}
2070
2071 // First find the property set in the module that corresponds to the requested
Usta851a3272022-01-05 23:42:33 -05002072 // one. m.archProperties[i] corresponds to m.GetProperties()[i].
2073 for i, generalProp := range m.GetProperties() {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002074 srcType := reflect.ValueOf(generalProp).Type()
2075 if srcType == dstType {
2076 archProperties = m.archProperties[i]
Liz Kammer135bf552021-08-11 10:46:06 -04002077 axisToProps[bazel.NoConfigAxis] = ArchVariantProperties{"": generalProp}
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002078 break
2079 }
2080 }
2081
2082 if archProperties == nil {
2083 // This module does not have the property set requested
Liz Kammer9abd62d2021-05-21 08:37:59 -04002084 return axisToProps
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002085 }
2086
Liz Kammer9abd62d2021-05-21 08:37:59 -04002087 archToProp := ArchVariantProperties{}
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002088 // For each arch type (x86, arm64, etc.)
Chris Parsonsc424b762021-04-29 18:06:50 -04002089 for _, arch := range ArchTypeList() {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002090 // Arch properties are sometimes sharded (see createArchPropTypeDesc() ).
Cole Faustc843b992022-08-02 18:06:50 -07002091 // Iterate over every shard and extract a struct with the same type as the
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002092 // input one that contains the data specific to that arch.
2093 propertyStructs := make([]reflect.Value, 0)
Cole Faustc843b992022-08-02 18:06:50 -07002094 archFeaturePropertyStructs := make(map[string][]reflect.Value, 0)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002095 for _, archProperty := range archProperties {
Lukacs T. Berki5f518392021-05-17 11:44:58 +02002096 archTypeStruct, ok := getArchTypeStruct(ctx, archProperty, arch)
2097 if ok {
2098 propertyStructs = append(propertyStructs, archTypeStruct)
Cole Faustc843b992022-08-02 18:06:50 -07002099
2100 // For each feature this arch supports (arm: neon, x86: ssse3, sse4, ...)
2101 for _, feature := range archFeatures[arch] {
2102 prefix := "arch." + arch.Name + "." + feature
2103 if featureProperties, ok := getChildPropertyStruct(ctx, archTypeStruct, feature, prefix); ok {
2104 archFeaturePropertyStructs[feature] = append(archFeaturePropertyStructs[feature], featureProperties)
2105 }
2106 }
Lukacs T. Berki5f518392021-05-17 11:44:58 +02002107 }
2108 multilibStruct, ok := getMultilibStruct(ctx, archProperty, arch)
2109 if ok {
2110 propertyStructs = append(propertyStructs, multilibStruct)
2111 }
Jingwen Chen5d864492021-02-24 07:20:12 -05002112 }
2113
Cole Faustc843b992022-08-02 18:06:50 -07002114 archToProp[arch.Name] = mergeStructs(ctx, propertyStructs, propertySet)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002115
Cole Faustc843b992022-08-02 18:06:50 -07002116 // In soong, if multiple features match the current configuration, they're
2117 // all used. In bazel, we have to have unambiguous select() statements, so
2118 // we can't have two features that are both active in the same select().
2119 // One alternative is to split out each feature into a separate select(),
2120 // but then it's difficult to support exclude_srcs, which may need to
2121 // exclude things from the regular arch select() statement if a certain
2122 // feature is active. Instead, keep the features in the same select
2123 // statement as the arches, but emit the power set of all possible
2124 // combinations of features, so that bazel can match the most precise one.
2125 allFeatures := make([]string, 0, len(archFeaturePropertyStructs))
2126 for feature := range archFeaturePropertyStructs {
2127 allFeatures = append(allFeatures, feature)
2128 }
2129 for _, features := range bazel.PowerSetWithoutEmptySet(allFeatures) {
2130 sort.Strings(features)
2131 propsForCurrentFeatureSet := make([]reflect.Value, 0)
2132 propsForCurrentFeatureSet = append(propsForCurrentFeatureSet, propertyStructs...)
2133 for _, feature := range features {
2134 propsForCurrentFeatureSet = append(propsForCurrentFeatureSet, archFeaturePropertyStructs[feature]...)
2135 }
2136 archToProp[arch.Name+"-"+strings.Join(features, "-")] =
2137 mergeStructs(ctx, propsForCurrentFeatureSet, propertySet)
2138 }
Jingwen Chen5d864492021-02-24 07:20:12 -05002139 }
Liz Kammer9abd62d2021-05-21 08:37:59 -04002140 axisToProps[bazel.ArchConfigurationAxis] = archToProp
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002141
Liz Kammer9abd62d2021-05-21 08:37:59 -04002142 osToProp := ArchVariantProperties{}
2143 archOsToProp := ArchVariantProperties{}
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002144
Liz Kammerfdd72e62021-10-11 15:41:03 -04002145 linuxStructs := getTargetStructs(ctx, archProperties, "Linux")
2146 bionicStructs := getTargetStructs(ctx, archProperties, "Bionic")
2147 hostStructs := getTargetStructs(ctx, archProperties, "Host")
Colin Crossa98d36d2022-03-07 14:39:49 -08002148 hostLinuxStructs := getTargetStructs(ctx, archProperties, "Host_linux")
Liz Kammerfdd72e62021-10-11 15:41:03 -04002149 hostNotWindowsStructs := getTargetStructs(ctx, archProperties, "Not_windows")
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002150
Liz Kammer9abd62d2021-05-21 08:37:59 -04002151 // For android, linux, ...
2152 for _, os := range osTypeList {
2153 if os == CommonOS {
2154 // It looks like this OS value is not used in Blueprint files
2155 continue
2156 }
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002157 osStructs := make([]reflect.Value, 0)
Liz Kammerfdd72e62021-10-11 15:41:03 -04002158
2159 osSpecificStructs := getTargetStructs(ctx, archProperties, os.Field)
2160 if os.Class == Host {
2161 osStructs = append(osStructs, hostStructs...)
Chris Parsonsa37e1952021-09-28 16:47:36 -04002162 }
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002163 if os.Linux() {
2164 osStructs = append(osStructs, linuxStructs...)
2165 }
2166 if os.Bionic() {
2167 osStructs = append(osStructs, bionicStructs...)
2168 }
Colin Crossa98d36d2022-03-07 14:39:49 -08002169 if os.Linux() && os.Class == Host {
2170 osStructs = append(osStructs, hostLinuxStructs...)
2171 }
Liz Kammerfdd72e62021-10-11 15:41:03 -04002172
2173 if os == LinuxMusl {
2174 osStructs = append(osStructs, getTargetStructs(ctx, archProperties, "Musl")...)
2175 }
2176 if os == Linux {
2177 osStructs = append(osStructs, getTargetStructs(ctx, archProperties, "Glibc")...)
2178 }
2179
2180 osStructs = append(osStructs, osSpecificStructs...)
2181
2182 if os.Class == Host && os != Windows {
2183 osStructs = append(osStructs, hostNotWindowsStructs...)
2184 }
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002185 osToProp[os.Name] = mergeStructs(ctx, osStructs, propertySet)
Chris Parsonsa37e1952021-09-28 16:47:36 -04002186
Liz Kammer9abd62d2021-05-21 08:37:59 -04002187 // For arm, x86, ...
2188 for _, arch := range osArchTypeMap[os] {
Chris Parsonsa37e1952021-09-28 16:47:36 -04002189 osArchStructs := make([]reflect.Value, 0)
2190
Chris Parsonsa37e1952021-09-28 16:47:36 -04002191 // Auto-combine with Linux_ and Bionic_ targets. This potentially results in
2192 // repetition and select() bloat, but use of Linux_* and Bionic_* targets is rare.
2193 // TODO(b/201423152): Look into cleanup.
2194 if os.Linux() {
2195 targetField := "Linux_" + arch.Name
Liz Kammerfdd72e62021-10-11 15:41:03 -04002196 targetStructs := getTargetStructs(ctx, archProperties, targetField)
2197 osArchStructs = append(osArchStructs, targetStructs...)
Chris Parsonsa37e1952021-09-28 16:47:36 -04002198 }
2199 if os.Bionic() {
2200 targetField := "Bionic_" + arch.Name
Liz Kammerfdd72e62021-10-11 15:41:03 -04002201 targetStructs := getTargetStructs(ctx, archProperties, targetField)
2202 osArchStructs = append(osArchStructs, targetStructs...)
Chris Parsonsa37e1952021-09-28 16:47:36 -04002203 }
Colin Cross2d295a22022-03-07 14:46:20 -08002204 if os == LinuxMusl {
2205 targetField := "Musl_" + arch.Name
2206 targetStructs := getTargetStructs(ctx, archProperties, targetField)
2207 osArchStructs = append(osArchStructs, targetStructs...)
2208 }
2209 if os == Linux {
2210 targetField := "Glibc_" + arch.Name
2211 targetStructs := getTargetStructs(ctx, archProperties, targetField)
2212 osArchStructs = append(osArchStructs, targetStructs...)
2213 }
Chris Parsonsa37e1952021-09-28 16:47:36 -04002214
Liz Kammerfdd72e62021-10-11 15:41:03 -04002215 targetField := GetCompoundTargetField(os, arch)
2216 targetName := fmt.Sprintf("%s_%s", os.Name, arch.Name)
2217 targetStructs := getTargetStructs(ctx, archProperties, targetField)
2218 osArchStructs = append(osArchStructs, targetStructs...)
2219
Chris Parsonsa37e1952021-09-28 16:47:36 -04002220 archOsToProp[targetName] = mergeStructs(ctx, osArchStructs, propertySet)
Liz Kammer9abd62d2021-05-21 08:37:59 -04002221 }
2222 }
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002223
Liz Kammer9abd62d2021-05-21 08:37:59 -04002224 axisToProps[bazel.OsConfigurationAxis] = osToProp
2225 axisToProps[bazel.OsArchConfigurationAxis] = archOsToProp
Liz Kammer9abd62d2021-05-21 08:37:59 -04002226 return axisToProps
Jingwen Chen5d864492021-02-24 07:20:12 -05002227}
Jingwen Chen91220d72021-03-24 02:18:33 -04002228
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04002229// Returns a struct matching the propertySet interface, containing properties specific to the targetName
2230// For example, given these arguments:
Colin Crossd079e0b2022-08-16 10:27:33 -07002231//
2232// propertySet = BaseCompilerProperties
2233// targetName = "android_arm"
2234//
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04002235// And given this Android.bp fragment:
Colin Crossd079e0b2022-08-16 10:27:33 -07002236//
2237// target:
2238// android_arm: {
2239// srcs: ["foo.c"],
2240// }
2241// android_arm64: {
2242// srcs: ["bar.c"],
2243// }
2244// }
2245//
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04002246// This would return a BaseCompilerProperties with BaseCompilerProperties.Srcs = ["foo.c"]
Liz Kammerfdd72e62021-10-11 15:41:03 -04002247func getTargetStructs(ctx ArchVariantContext, archProperties []interface{}, targetName string) []reflect.Value {
2248 var propertyStructs []reflect.Value
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04002249 for _, archProperty := range archProperties {
2250 archPropValues := reflect.ValueOf(archProperty).Elem()
2251 targetProp := archPropValues.FieldByName("Target").Elem()
2252 targetStruct, ok := getChildPropertyStruct(ctx, targetProp, targetName, targetName)
2253 if ok {
2254 propertyStructs = append(propertyStructs, targetStruct)
Chris Parsonsa37e1952021-09-28 16:47:36 -04002255 } else {
Liz Kammerfdd72e62021-10-11 15:41:03 -04002256 return []reflect.Value{}
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04002257 }
2258 }
2259
Liz Kammerfdd72e62021-10-11 15:41:03 -04002260 return propertyStructs
Chris Parsonsa37e1952021-09-28 16:47:36 -04002261}
2262
2263func mergeStructs(ctx ArchVariantContext, propertyStructs []reflect.Value, propertySet interface{}) interface{} {
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04002264 // Create a new instance of the requested property set
2265 value := reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
2266
2267 // Merge all the structs together
2268 for _, propertyStruct := range propertyStructs {
2269 mergePropertyStruct(ctx, value, propertyStruct)
2270 }
2271
2272 return value
2273}
Liz Kammere8303bd2022-02-16 09:02:48 -05002274
2275func printArchTypeStarlarkDict(dict map[ArchType][]string) string {
2276 valDict := make(map[string]string, len(dict))
2277 for k, v := range dict {
2278 valDict[k.String()] = starlark_fmt.PrintStringList(v, 1)
2279 }
2280 return starlark_fmt.PrintDict(valDict, 0)
2281}
2282
2283func printArchTypeNestedStarlarkDict(dict map[ArchType]map[string][]string) string {
2284 valDict := make(map[string]string, len(dict))
2285 for k, v := range dict {
2286 valDict[k.String()] = starlark_fmt.PrintStringListDict(v, 1)
2287 }
2288 return starlark_fmt.PrintDict(valDict, 0)
2289}
2290
Liz Kammer992918d2022-11-11 10:37:54 -05002291func printArchConfigList(arches []archConfig) string {
2292 jsonOut, err := json.MarshalIndent(arches, "", starlark_fmt.Indention(1))
2293 if err != nil {
2294 panic(fmt.Errorf("Error converting arch configs %#v to json: %q", arches, err))
2295 }
2296 return fmt.Sprintf("json.decode('''%s''')", string(jsonOut))
2297}
2298
Liz Kammere8303bd2022-02-16 09:02:48 -05002299func StarlarkArchConfigurations() string {
2300 return fmt.Sprintf(`
2301_arch_to_variants = %s
2302
2303_arch_to_cpu_variants = %s
2304
2305_arch_to_features = %s
2306
2307_android_arch_feature_for_arch_variant = %s
2308
Liz Kammer992918d2022-11-11 10:37:54 -05002309_aml_arches = %s
2310
2311_ndk_arches = %s
2312
Liz Kammere8303bd2022-02-16 09:02:48 -05002313arch_to_variants = _arch_to_variants
2314arch_to_cpu_variants = _arch_to_cpu_variants
2315arch_to_features = _arch_to_features
2316android_arch_feature_for_arch_variants = _android_arch_feature_for_arch_variant
Liz Kammer992918d2022-11-11 10:37:54 -05002317aml_arches = _aml_arches
2318ndk_arches = _ndk_arches
Liz Kammere8303bd2022-02-16 09:02:48 -05002319`, printArchTypeStarlarkDict(archVariants),
2320 printArchTypeStarlarkDict(cpuVariants),
2321 printArchTypeStarlarkDict(archFeatures),
2322 printArchTypeNestedStarlarkDict(androidArchFeatureMap),
Liz Kammer992918d2022-11-11 10:37:54 -05002323 printArchConfigList(getAmlAbisConfig()),
2324 printArchConfigList(getNdkAbisConfig()),
Liz Kammere8303bd2022-02-16 09:02:48 -05002325 )
2326}