blob: a0895edf8050ea1a09f0e512b2b0263eccaff611 [file] [log] [blame]
Colin Cross3f40fa42015-01-30 17:27:36 -08001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Colin Cross635c3b02016-05-18 15:37:25 -070015package android
Colin Cross3f40fa42015-01-30 17:27:36 -080016
17import (
Colin Cross74ba9622019-02-11 15:11:14 -080018 "encoding"
Colin Cross3f40fa42015-01-30 17:27:36 -080019 "fmt"
20 "reflect"
21 "runtime"
22 "strings"
Colin Crossf6566ed2015-03-24 11:13:38 -070023
Colin Crosscb0ac952021-07-20 13:17:15 -070024 "android/soong/bazel"
Liz Kammere8303bd2022-02-16 09:02:48 -050025 "android/soong/starlark_fmt"
Colin Crosscb0ac952021-07-20 13:17:15 -070026
Colin Cross0f7d2ef2019-10-16 11:03:10 -070027 "github.com/google/blueprint"
Colin Cross617b88a2020-08-24 18:04:09 -070028 "github.com/google/blueprint/bootstrap"
Colin Crossf6566ed2015-03-24 11:13:38 -070029 "github.com/google/blueprint/proptools"
Colin Cross3f40fa42015-01-30 17:27:36 -080030)
31
Colin Cross3f40fa42015-01-30 17:27:36 -080032/*
33Example blueprints file containing all variant property groups, with comment listing what type
34of variants get properties in that group:
35
36module {
37 arch: {
38 arm: {
39 // Host or device variants with arm architecture
40 },
41 arm64: {
42 // Host or device variants with arm64 architecture
43 },
Colin Cross3f40fa42015-01-30 17:27:36 -080044 x86: {
45 // Host or device variants with x86 architecture
46 },
47 x86_64: {
48 // Host or device variants with x86_64 architecture
49 },
50 },
51 multilib: {
52 lib32: {
53 // Host or device variants for 32-bit architectures
54 },
55 lib64: {
56 // Host or device variants for 64-bit architectures
57 },
58 },
59 target: {
60 android: {
Martin Stjernholme284b482020-09-23 21:03:27 +010061 // Device variants (implies Bionic)
Colin Cross3f40fa42015-01-30 17:27:36 -080062 },
63 host: {
64 // Host variants
65 },
Martin Stjernholme284b482020-09-23 21:03:27 +010066 bionic: {
67 // Bionic (device and host) variants
68 },
69 linux_bionic: {
70 // Bionic host variants
71 },
72 linux: {
73 // Bionic (device and host) and Linux glibc variants
74 },
Dan Willemsen5746bd42017-10-02 19:42:01 -070075 linux_glibc: {
Martin Stjernholme284b482020-09-23 21:03:27 +010076 // Linux host variants (using non-Bionic libc)
Colin Cross3f40fa42015-01-30 17:27:36 -080077 },
78 darwin: {
79 // Darwin host variants
80 },
81 windows: {
82 // Windows host variants
83 },
84 not_windows: {
85 // Non-windows host variants
86 },
Martin Stjernholme284b482020-09-23 21:03:27 +010087 android_arm: {
88 // Any <os>_<arch> combination restricts to that os and arch
89 },
Colin Cross3f40fa42015-01-30 17:27:36 -080090 },
91}
92*/
Colin Cross7d5136f2015-05-11 13:39:40 -070093
Colin Cross3f40fa42015-01-30 17:27:36 -080094// An Arch indicates a single CPU architecture.
95type Arch struct {
Colin Crossa6845402020-11-16 15:08:19 -080096 // The type of the architecture (arm, arm64, x86, or x86_64).
97 ArchType ArchType
98
99 // The variant of the architecture, for example "armv7-a" or "armv7-a-neon" for arm.
100 ArchVariant string
101
102 // The variant of the CPU, for example "cortex-a53" for arm64.
103 CpuVariant string
104
105 // The list of Android app ABIs supported by the CPU architecture, for example "arm64-v8a".
106 Abi []string
107
108 // The list of arch-specific features supported by the CPU architecture, for example "neon".
Colin Crossc5c24ad2015-11-20 15:35:00 -0800109 ArchFeatures []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800110}
111
Colin Crossa6845402020-11-16 15:08:19 -0800112// String returns the Arch as a string. The value is used as the name of the variant created
113// by archMutator.
Colin Cross3f40fa42015-01-30 17:27:36 -0800114func (a Arch) String() string {
Colin Crossd3ba0392015-05-07 14:11:29 -0700115 s := a.ArchType.String()
Colin Cross3f40fa42015-01-30 17:27:36 -0800116 if a.ArchVariant != "" {
117 s += "_" + a.ArchVariant
118 }
119 if a.CpuVariant != "" {
120 s += "_" + a.CpuVariant
121 }
122 return s
123}
124
Colin Crossa6845402020-11-16 15:08:19 -0800125// ArchType is used to define the 4 supported architecture types (arm, arm64, x86, x86_64), as
126// well as the "common" architecture used for modules that support multiple architectures, for
127// example Java modules.
Colin Cross3f40fa42015-01-30 17:27:36 -0800128type ArchType struct {
Colin Crossa6845402020-11-16 15:08:19 -0800129 // Name is the name of the architecture type, "arm", "arm64", "x86", or "x86_64".
130 Name string
131
132 // Field is the name of the field used in properties that refer to the architecture, e.g. "Arm64".
133 Field string
134
135 // Multilib is either "lib32" or "lib64" for 32-bit or 64-bit architectures.
Colin Crossec193632015-07-06 17:49:43 -0700136 Multilib string
Colin Cross3f40fa42015-01-30 17:27:36 -0800137}
138
Colin Crossa6845402020-11-16 15:08:19 -0800139// String returns the name of the ArchType.
140func (a ArchType) String() string {
141 return a.Name
142}
143
144const COMMON_VARIANT = "common"
145
146var (
147 archTypeList []ArchType
148
149 Arm = newArch("arm", "lib32")
150 Arm64 = newArch("arm64", "lib64")
151 X86 = newArch("x86", "lib32")
152 X86_64 = newArch("x86_64", "lib64")
153
154 Common = ArchType{
155 Name: COMMON_VARIANT,
156 }
157)
158
159var archTypeMap = map[string]ArchType{}
160
Colin Crossec193632015-07-06 17:49:43 -0700161func newArch(name, multilib string) ArchType {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700162 archType := ArchType{
Colin Crossec193632015-07-06 17:49:43 -0700163 Name: name,
Dan Willemsenb1957a52016-06-23 23:44:54 -0700164 Field: proptools.FieldNameForProperty(name),
Colin Crossec193632015-07-06 17:49:43 -0700165 Multilib: multilib,
Colin Cross3f40fa42015-01-30 17:27:36 -0800166 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700167 archTypeList = append(archTypeList, archType)
Colin Crossa6845402020-11-16 15:08:19 -0800168 archTypeMap[name] = archType
Dan Willemsenb1957a52016-06-23 23:44:54 -0700169 return archType
Colin Cross3f40fa42015-01-30 17:27:36 -0800170}
171
Ustaeabf0f32021-12-06 15:17:23 -0500172// ArchTypeList returns a slice copy of the 4 supported ArchTypes for arm,
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000173// arm64, x86 and x86_64.
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -0700174func ArchTypeList() []ArchType {
175 return append([]ArchType(nil), archTypeList...)
176}
177
Colin Crossa6845402020-11-16 15:08:19 -0800178// MarshalText allows an ArchType to be serialized through any encoder that supports
179// encoding.TextMarshaler.
Colin Cross74ba9622019-02-11 15:11:14 -0800180func (a ArchType) MarshalText() ([]byte, error) {
Jeongik Chabec4d032021-04-15 08:55:38 +0900181 return []byte(a.String()), nil
Colin Cross74ba9622019-02-11 15:11:14 -0800182}
183
Colin Crossa6845402020-11-16 15:08:19 -0800184var _ encoding.TextMarshaler = ArchType{}
Colin Cross74ba9622019-02-11 15:11:14 -0800185
Colin Crossa6845402020-11-16 15:08:19 -0800186// UnmarshalText allows an ArchType to be deserialized through any decoder that supports
187// encoding.TextUnmarshaler.
Colin Cross74ba9622019-02-11 15:11:14 -0800188func (a *ArchType) UnmarshalText(text []byte) error {
189 if u, ok := archTypeMap[string(text)]; ok {
190 *a = u
191 return nil
192 }
193
194 return fmt.Errorf("unknown ArchType %q", text)
195}
196
Colin Crossa6845402020-11-16 15:08:19 -0800197var _ encoding.TextUnmarshaler = &ArchType{}
Colin Crossa1ad8d12016-06-01 17:09:44 -0700198
Colin Crossa6845402020-11-16 15:08:19 -0800199// OsClass is an enum that describes whether a variant of a module runs on the host, on the device,
200// or is generic.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700201type OsClass int
202
203const (
Colin Crossa6845402020-11-16 15:08:19 -0800204 // Generic is used for variants of modules that are not OS-specific.
Dan Willemsen0e2d97b2016-11-28 17:50:06 -0800205 Generic OsClass = iota
Colin Crossa6845402020-11-16 15:08:19 -0800206 // Device is used for variants of modules that run on the device.
Dan Willemsen0e2d97b2016-11-28 17:50:06 -0800207 Device
Colin Crossa6845402020-11-16 15:08:19 -0800208 // Host is used for variants of modules that run on the host.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700209 Host
Colin Crossa1ad8d12016-06-01 17:09:44 -0700210)
211
Colin Crossa6845402020-11-16 15:08:19 -0800212// String returns the OsClass as a string.
Colin Cross67a5c132017-05-09 13:45:28 -0700213func (class OsClass) String() string {
214 switch class {
215 case Generic:
216 return "generic"
217 case Device:
218 return "device"
219 case Host:
220 return "host"
Colin Cross67a5c132017-05-09 13:45:28 -0700221 default:
222 panic(fmt.Errorf("unknown class %d", class))
223 }
224}
225
Colin Crossa6845402020-11-16 15:08:19 -0800226// OsType describes an OS variant of a module.
227type OsType struct {
228 // Name is the name of the OS. It is also used as the name of the property in Android.bp
229 // files.
230 Name string
231
232 // Field is the name of the OS converted to an exported field name, i.e. with the first
233 // character capitalized.
234 Field string
235
236 // Class is the OsClass of the OS.
237 Class OsClass
238
239 // DefaultDisabled is set when the module variants for the OS should not be created unless
240 // the module explicitly requests them. This is used to limit Windows cross compilation to
241 // only modules that need it.
242 DefaultDisabled bool
243}
244
245// String returns the name of the OsType.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700246func (os OsType) String() string {
247 return os.Name
Colin Cross54c71122016-06-01 17:09:44 -0700248}
249
Colin Crossa6845402020-11-16 15:08:19 -0800250// Bionic returns true if the OS uses the Bionic libc runtime, i.e. if the OS is Android or
251// is Linux with Bionic.
Dan Willemsen866b5632017-09-22 12:28:24 -0700252func (os OsType) Bionic() bool {
253 return os == Android || os == LinuxBionic
254}
255
Colin Crossa6845402020-11-16 15:08:19 -0800256// Linux returns true if the OS uses the Linux kernel, i.e. if the OS is Android or is Linux
257// with or without the Bionic libc runtime.
Dan Willemsen866b5632017-09-22 12:28:24 -0700258func (os OsType) Linux() bool {
Colin Cross528d67e2021-07-23 22:23:07 +0000259 return os == Android || os == Linux || os == LinuxBionic || os == LinuxMusl
Dan Willemsen866b5632017-09-22 12:28:24 -0700260}
261
Colin Crossa6845402020-11-16 15:08:19 -0800262// newOsType constructs an OsType and adds it to the global lists.
263func newOsType(name string, class OsClass, defDisabled bool, archTypes ...ArchType) OsType {
264 checkCalledFromInit()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700265 os := OsType{
266 Name: name,
Colin Crossa6845402020-11-16 15:08:19 -0800267 Field: proptools.FieldNameForProperty(name),
Colin Crossa1ad8d12016-06-01 17:09:44 -0700268 Class: class,
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800269
270 DefaultDisabled: defDisabled,
Colin Cross54c71122016-06-01 17:09:44 -0700271 }
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000272 osTypeList = append(osTypeList, os)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800273
274 if _, found := commonTargetMap[name]; found {
275 panic(fmt.Errorf("Found Os type duplicate during OsType registration: %q", name))
276 } else {
Colin Crosse9fe2942020-11-10 18:12:15 -0800277 commonTargetMap[name] = Target{Os: os, Arch: CommonArch}
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800278 }
Colin Crossa6845402020-11-16 15:08:19 -0800279 osArchTypeMap[os] = archTypes
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800280
Colin Crossa1ad8d12016-06-01 17:09:44 -0700281 return os
282}
283
Colin Crossa6845402020-11-16 15:08:19 -0800284// osByName returns the OsType that has the given name, or NoOsType if none match.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700285func osByName(name string) OsType {
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000286 for _, os := range osTypeList {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700287 if os.Name == name {
288 return os
289 }
290 }
291
292 return NoOsType
Dan Willemsen490fd492015-11-24 17:53:15 -0800293}
294
Colin Crossa6845402020-11-16 15:08:19 -0800295var (
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000296 // osTypeList contains a list of all the supported OsTypes, including ones not supported
Colin Crossa6845402020-11-16 15:08:19 -0800297 // by the current build host or the target device.
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000298 osTypeList []OsType
Colin Crossa6845402020-11-16 15:08:19 -0800299 // commonTargetMap maps names of OsTypes to the corresponding common Target, i.e. the
300 // Target with the same OsType and the common ArchType.
301 commonTargetMap = make(map[string]Target)
302 // osArchTypeMap maps OsTypes to the list of supported ArchTypes for that OS.
303 osArchTypeMap = map[OsType][]ArchType{}
304
305 // NoOsType is a placeholder for when no OS is needed.
306 NoOsType OsType
307 // Linux is the OS for the Linux kernel plus the glibc runtime.
308 Linux = newOsType("linux_glibc", Host, false, X86, X86_64)
Colin Cross528d67e2021-07-23 22:23:07 +0000309 // LinuxMusl is the OS for the Linux kernel plus the musl runtime.
Colin Crossa9b2aac2022-06-15 17:25:51 -0700310 LinuxMusl = newOsType("linux_musl", Host, false, X86, X86_64, Arm64, Arm)
Colin Crossa6845402020-11-16 15:08:19 -0800311 // Darwin is the OS for MacOS/Darwin host machines.
Dan Willemsen8528f4e2021-10-19 00:22:06 -0700312 Darwin = newOsType("darwin", Host, false, Arm64, X86_64)
Colin Crossa6845402020-11-16 15:08:19 -0800313 // LinuxBionic is the OS for the Linux kernel plus the Bionic libc runtime, but without the
314 // rest of Android.
315 LinuxBionic = newOsType("linux_bionic", Host, false, Arm64, X86_64)
316 // Windows the OS for Windows host machines.
317 Windows = newOsType("windows", Host, true, X86, X86_64)
318 // Android is the OS for target devices that run all of Android, including the Linux kernel
319 // and the Bionic libc runtime.
320 Android = newOsType("android", Device, false, Arm, Arm64, X86, X86_64)
Colin Crossa6845402020-11-16 15:08:19 -0800321
322 // CommonOS is a pseudo OSType for a common OS variant, which is OsType agnostic and which
323 // has dependencies on all the OS variants.
324 CommonOS = newOsType("common_os", Generic, false)
Colin Crosse9fe2942020-11-10 18:12:15 -0800325
326 // CommonArch is the Arch for all modules that are os-specific but not arch specific,
327 // for example most Java modules.
328 CommonArch = Arch{ArchType: Common}
dimitry1f33e402019-03-26 12:39:31 +0100329)
330
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000331// OsTypeList returns a slice copy of the supported OsTypes.
332func OsTypeList() []OsType {
333 return append([]OsType(nil), osTypeList...)
334}
335
Colin Crossa6845402020-11-16 15:08:19 -0800336// Target specifies the OS and architecture that a module is being compiled for.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700337type Target struct {
Colin Crossa6845402020-11-16 15:08:19 -0800338 // Os the OS that the module is being compiled for (e.g. "linux_glibc", "android").
339 Os OsType
340 // Arch is the architecture that the module is being compiled for.
341 Arch Arch
342 // NativeBridge is NativeBridgeEnabled if the architecture is supported using NativeBridge
343 // (i.e. arm on x86) for this device.
344 NativeBridge NativeBridgeSupport
345 // NativeBridgeHostArchName is the name of the real architecture that is used to implement
346 // the NativeBridge architecture. For example, for arm on x86 this would be "x86".
dimitry8d6dde82019-07-11 10:23:53 +0200347 NativeBridgeHostArchName string
Colin Crossa6845402020-11-16 15:08:19 -0800348 // NativeBridgeRelativePath is the name of the subdirectory that will contain NativeBridge
349 // libraries and binaries.
dimitry8d6dde82019-07-11 10:23:53 +0200350 NativeBridgeRelativePath string
Jiyong Park1613e552020-09-14 19:43:17 +0900351
352 // HostCross is true when the target cannot run natively on the current build host.
353 // For example, linux_glibc_x86 returns true on a regular x86/i686/Linux machines, but returns false
354 // on Mac (different OS), or on 64-bit only i686/Linux machines (unsupported arch).
355 HostCross bool
Colin Crossd3ba0392015-05-07 14:11:29 -0700356}
357
Colin Crossa6845402020-11-16 15:08:19 -0800358// NativeBridgeSupport is an enum that specifies if a Target supports NativeBridge.
359type NativeBridgeSupport bool
360
361const (
362 NativeBridgeDisabled NativeBridgeSupport = false
363 NativeBridgeEnabled NativeBridgeSupport = true
364)
365
366// String returns the OS and arch variations used for the Target.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700367func (target Target) String() string {
Colin Crossa195f912019-10-16 11:07:20 -0700368 return target.OsVariation() + "_" + target.ArchVariation()
369}
370
Colin Crossa6845402020-11-16 15:08:19 -0800371// OsVariation returns the name of the variation used by the osMutator for the Target.
Colin Crossa195f912019-10-16 11:07:20 -0700372func (target Target) OsVariation() string {
373 return target.Os.String()
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700374}
375
Colin Crossa6845402020-11-16 15:08:19 -0800376// ArchVariation returns the name of the variation used by the archMutator for the Target.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700377func (target Target) ArchVariation() string {
378 var variation string
dimitry1f33e402019-03-26 12:39:31 +0100379 if target.NativeBridge {
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700380 variation = "native_bridge_"
dimitry1f33e402019-03-26 12:39:31 +0100381 }
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700382 variation += target.Arch.String()
383
Colin Crossa195f912019-10-16 11:07:20 -0700384 return variation
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700385}
386
Colin Crossa6845402020-11-16 15:08:19 -0800387// Variations returns a list of blueprint.Variations for the osMutator and archMutator for the
388// Target.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700389func (target Target) Variations() []blueprint.Variation {
390 return []blueprint.Variation{
Colin Crossa195f912019-10-16 11:07:20 -0700391 {Mutator: "os", Variation: target.OsVariation()},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700392 {Mutator: "arch", Variation: target.ArchVariation()},
393 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800394}
395
Colin Crossa6845402020-11-16 15:08:19 -0800396// osMutator splits an arch-specific module into a variant for each OS that is enabled for the
397// module. It uses the HostOrDevice value passed to InitAndroidArchModule and the
398// device_supported and host_supported properties to determine which OsTypes are enabled for this
399// module, then searches through the Targets to determine which have enabled Targets for this
400// module.
Colin Cross617b88a2020-08-24 18:04:09 -0700401func osMutator(bpctx blueprint.BottomUpMutatorContext) {
Colin Crossa195f912019-10-16 11:07:20 -0700402 var module Module
403 var ok bool
Colin Cross617b88a2020-08-24 18:04:09 -0700404 if module, ok = bpctx.Module().(Module); !ok {
Colin Crossa6845402020-11-16 15:08:19 -0800405 // The module is not a Soong module, it is a Blueprint module.
Colin Cross617b88a2020-08-24 18:04:09 -0700406 if bootstrap.IsBootstrapModule(bpctx.Module()) {
407 // Bootstrap Go modules are always the build OS or linux bionic.
408 config := bpctx.Config().(Config)
409 osNames := []string{config.BuildOSTarget.OsVariation()}
410 for _, hostCrossTarget := range config.Targets[LinuxBionic] {
411 if hostCrossTarget.Arch.ArchType == config.BuildOSTarget.Arch.ArchType {
412 osNames = append(osNames, hostCrossTarget.OsVariation())
413 }
414 }
415 osNames = FirstUniqueStrings(osNames)
416 bpctx.CreateVariations(osNames...)
417 }
Colin Crossa195f912019-10-16 11:07:20 -0700418 return
419 }
420
Colin Cross617b88a2020-08-24 18:04:09 -0700421 // Bootstrap Go module support above requires this mutator to be a
422 // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext
423 // filters out non-Soong modules. Now that we've handled them, create a
424 // normal android.BottomUpMutatorContext.
Liz Kammer356f7d42021-01-26 09:18:53 -0500425 mctx := bottomUpMutatorContextFactory(bpctx, module, false, false)
Colin Cross617b88a2020-08-24 18:04:09 -0700426
Colin Crossa195f912019-10-16 11:07:20 -0700427 base := module.base()
428
Colin Crossa6845402020-11-16 15:08:19 -0800429 // Nothing to do for modules that are not architecture specific (e.g. a genrule).
Colin Crossa195f912019-10-16 11:07:20 -0700430 if !base.ArchSpecific() {
431 return
432 }
433
Colin Crossa6845402020-11-16 15:08:19 -0800434 // Collect a list of OSTypes supported by this module based on the HostOrDevice value
435 // passed to InitAndroidArchModule and the device_supported and host_supported properties.
Colin Crossa195f912019-10-16 11:07:20 -0700436 var moduleOSList []OsType
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000437 for _, os := range osTypeList {
Jiyong Park1613e552020-09-14 19:43:17 +0900438 for _, t := range mctx.Config().Targets[os] {
Colin Cross08d6f8f2020-11-19 02:33:19 +0000439 if base.supportsTarget(t) {
Jiyong Park1613e552020-09-14 19:43:17 +0900440 moduleOSList = append(moduleOSList, os)
441 break
Colin Crossa195f912019-10-16 11:07:20 -0700442 }
443 }
Colin Crossa195f912019-10-16 11:07:20 -0700444 }
445
Colin Crossa6845402020-11-16 15:08:19 -0800446 // If there are no supported OSes then disable the module.
Colin Crossa195f912019-10-16 11:07:20 -0700447 if len(moduleOSList) == 0 {
Inseob Kimeec88e12020-01-22 11:11:29 +0900448 base.Disable()
Colin Crossa195f912019-10-16 11:07:20 -0700449 return
450 }
451
Colin Crossa6845402020-11-16 15:08:19 -0800452 // Convert the list of supported OsTypes to the variation names.
Colin Crossa195f912019-10-16 11:07:20 -0700453 osNames := make([]string, len(moduleOSList))
Colin Crossa195f912019-10-16 11:07:20 -0700454 for i, os := range moduleOSList {
455 osNames[i] = os.String()
456 }
457
Paul Duffin1356d8c2020-02-25 19:26:33 +0000458 createCommonOSVariant := base.commonProperties.CreateCommonOSVariant
459 if createCommonOSVariant {
Colin Crossa6845402020-11-16 15:08:19 -0800460 // A CommonOS variant was requested so add it to the list of OS variants to
Paul Duffin1356d8c2020-02-25 19:26:33 +0000461 // create. It needs to be added to the end because it needs to depend on the
462 // the other variants in the list returned by CreateVariations(...) and inter
463 // variant dependencies can only be created from a later variant in that list to
464 // an earlier one. That is because variants are always processed in the order in
465 // which they are returned from CreateVariations(...).
466 osNames = append(osNames, CommonOS.Name)
467 moduleOSList = append(moduleOSList, CommonOS)
Colin Crossa195f912019-10-16 11:07:20 -0700468 }
469
Colin Crossa6845402020-11-16 15:08:19 -0800470 // Create the variations, annotate each one with which OS it was created for, and
471 // squash the appropriate OS-specific properties into the top level properties.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000472 modules := mctx.CreateVariations(osNames...)
473 for i, m := range modules {
474 m.base().commonProperties.CompileOS = moduleOSList[i]
475 m.base().setOSProperties(mctx)
476 }
477
478 if createCommonOSVariant {
479 // A CommonOS variant was requested so add dependencies from it (the last one in
480 // the list) to the OS type specific variants.
481 last := len(modules) - 1
482 commonOSVariant := modules[last]
483 commonOSVariant.base().commonProperties.CommonOSVariant = true
484 for _, module := range modules[0:last] {
485 // Ignore modules that are enabled. Note, this will only avoid adding
486 // dependencies on OsType variants that are explicitly disabled in their
487 // properties. The CommonOS variant will still depend on disabled variants
488 // if they are disabled afterwards, e.g. in archMutator if
489 if module.Enabled() {
490 mctx.AddInterVariantDependency(commonOsToOsSpecificVariantTag, commonOSVariant, module)
491 }
492 }
493 }
494}
495
Colin Crossc179ea62020-10-09 10:54:15 -0700496type archDepTag struct {
497 blueprint.BaseDependencyTag
498 name string
499}
Paul Duffin1356d8c2020-02-25 19:26:33 +0000500
Colin Crossc179ea62020-10-09 10:54:15 -0700501// Identifies the dependency from CommonOS variant to the os specific variants.
502var commonOsToOsSpecificVariantTag = archDepTag{name: "common os to os specific"}
503
Paul Duffin1356d8c2020-02-25 19:26:33 +0000504// Get the OsType specific variants for the current CommonOS variant.
505//
506// The returned list will only contain enabled OsType specific variants of the
507// module referenced in the supplied context. An empty list is returned if there
508// are no enabled variants or the supplied context is not for an CommonOS
509// variant.
510func GetOsSpecificVariantsOfCommonOSVariant(mctx BaseModuleContext) []Module {
511 var variants []Module
512 mctx.VisitDirectDeps(func(m Module) {
513 if mctx.OtherModuleDependencyTag(m) == commonOsToOsSpecificVariantTag {
514 if m.Enabled() {
515 variants = append(variants, m)
516 }
517 }
518 })
Paul Duffin1356d8c2020-02-25 19:26:33 +0000519 return variants
Colin Crossa195f912019-10-16 11:07:20 -0700520}
521
Dan Willemsen47450072021-10-19 20:24:49 -0700522var DarwinUniversalVariantTag = archDepTag{name: "darwin universal binary"}
523
Colin Crossee0bc3b2018-10-02 22:01:37 -0700524// archMutator splits a module into a variant for each Target requested by the module. Target selection
Colin Crossa6845402020-11-16 15:08:19 -0800525// for a module is in three levels, OsClass, multilib, and then Target.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700526// OsClass selection is determined by:
Colin Crossd079e0b2022-08-16 10:27:33 -0700527// - The HostOrDeviceSupported value passed in to InitAndroidArchModule by the module type factory, which selects
528// whether the module type can compile for host, device or both.
529// - The host_supported and device_supported properties on the module.
530//
Roland Levillainf5b635d2019-06-05 14:42:57 +0100531// 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 -0700532// for the module, the Device OsClass is selected.
533// Within each selected OsClass, the multilib selection is determined by:
Colin Crossd079e0b2022-08-16 10:27:33 -0700534// - The compile_multilib property if it set (which may be overridden by target.android.compile_multilib or
535// target.host.compile_multilib).
536// - The default multilib passed to InitAndroidArchModule if compile_multilib was not set.
537//
Colin Crossee0bc3b2018-10-02 22:01:37 -0700538// Valid multilib values include:
Colin Crossd079e0b2022-08-16 10:27:33 -0700539//
540// "both": compile for all Targets supported by the OsClass (generally x86_64 and x86, or arm64 and arm).
541// "first": compile for only a single preferred Target supported by the OsClass. This is generally x86_64 or arm64,
542// but may be arm for a 32-bit only build.
543// "32": compile for only a single 32-bit Target supported by the OsClass.
544// "64": compile for only a single 64-bit Target supported by the OsClass.
545// "common": compile a for a single Target that will work on all Targets supported by the OsClass (for example Java).
546// "common_first": compile a for a Target that will work on all Targets supported by the OsClass
547// (same as "common"), plus a second Target for the preferred Target supported by the OsClass
548// (same as "first"). This is used for java_binary that produces a common .jar and a wrapper
549// executable script.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700550//
551// Once the list of Targets is determined, the module is split into a variant for each Target.
552//
553// Modules can be initialized with InitAndroidMultiTargetsArchModule, in which case they will be split by OsClass,
554// 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 -0700555func archMutator(bpctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700556 var module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800557 var ok bool
Colin Cross617b88a2020-08-24 18:04:09 -0700558 if module, ok = bpctx.Module().(Module); !ok {
559 if bootstrap.IsBootstrapModule(bpctx.Module()) {
560 // Bootstrap Go modules are always the build architecture.
561 bpctx.CreateVariations(bpctx.Config().(Config).BuildOSTarget.ArchVariation())
562 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800563 return
564 }
565
Colin Cross617b88a2020-08-24 18:04:09 -0700566 // Bootstrap Go module support above requires this mutator to be a
567 // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext
568 // filters out non-Soong modules. Now that we've handled them, create a
569 // normal android.BottomUpMutatorContext.
Liz Kammer356f7d42021-01-26 09:18:53 -0500570 mctx := bottomUpMutatorContextFactory(bpctx, module, false, false)
Colin Cross617b88a2020-08-24 18:04:09 -0700571
Colin Cross5eca7cb2018-10-02 14:02:10 -0700572 base := module.base()
573
574 if !base.ArchSpecific() {
Colin Crossb9db4802016-06-03 01:50:47 +0000575 return
576 }
577
Colin Crossa195f912019-10-16 11:07:20 -0700578 os := base.commonProperties.CompileOS
Paul Duffin1356d8c2020-02-25 19:26:33 +0000579 if os == CommonOS {
580 // Make sure that the target related properties are initialized for the
581 // CommonOS variant.
582 addTargetProperties(module, commonTargetMap[os.Name], nil, true)
583
584 // Do not create arch specific variants for the CommonOS variant.
585 return
586 }
587
Colin Crossa195f912019-10-16 11:07:20 -0700588 osTargets := mctx.Config().Targets[os]
Colin Crossfb0c16e2019-11-20 17:12:35 -0800589 image := base.commonProperties.ImageVariation
Colin Crossa6845402020-11-16 15:08:19 -0800590 // Filter NativeBridge targets unless they are explicitly supported.
591 // Skip creating native bridge variants for non-core modules.
Paul Duffine3d1ae42021-09-03 17:47:17 +0100592 if os == Android && !(base.IsNativeBridgeSupported() && image == CoreVariation) {
Colin Cross83bead42019-12-18 10:45:46 -0800593
Colin Crossa195f912019-10-16 11:07:20 -0700594 var targets []Target
595 for _, t := range osTargets {
596 if !t.NativeBridge {
597 targets = append(targets, t)
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700598 }
599 }
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700600
Colin Crossa195f912019-10-16 11:07:20 -0700601 osTargets = targets
602 }
Colin Crossee0bc3b2018-10-02 22:01:37 -0700603
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700604 // only the primary arch in the ramdisk / vendor_ramdisk / recovery partition
Inseob Kim08758f02021-04-08 21:13:22 +0900605 if os == Android && (module.InstallInRecovery() || module.InstallInRamdisk() || module.InstallInVendorRamdisk() || module.InstallInDebugRamdisk()) {
Colin Crossa195f912019-10-16 11:07:20 -0700606 osTargets = []Target{osTargets[0]}
607 }
dimitry1f33e402019-03-26 12:39:31 +0100608
Jaewoong Jung003d8082021-02-24 17:39:54 -0800609 // Windows builds always prefer 32-bit
610 prefer32 := os == Windows
dimitry1f33e402019-03-26 12:39:31 +0100611
Colin Crossa6845402020-11-16 15:08:19 -0800612 // Determine the multilib selection for this module.
Christopher Ferris98f10222022-07-13 23:16:52 -0700613 ignorePrefer32OnDevice := mctx.Config().IgnorePrefer32OnDevice()
614 multilib, extraMultilib := decodeMultilib(base, os, ignorePrefer32OnDevice)
Colin Crossa6845402020-11-16 15:08:19 -0800615
616 // Convert the multilib selection into a list of Targets.
Colin Crossa195f912019-10-16 11:07:20 -0700617 targets, err := decodeMultilibTargets(multilib, osTargets, prefer32)
618 if err != nil {
619 mctx.ModuleErrorf("%s", err.Error())
620 }
Colin Cross5eca7cb2018-10-02 14:02:10 -0700621
Colin Crossa6845402020-11-16 15:08:19 -0800622 // If the module is using extraMultilib, decode the extraMultilib selection into
623 // a separate list of Targets.
Colin Crossa195f912019-10-16 11:07:20 -0700624 var multiTargets []Target
625 if extraMultilib != "" {
626 multiTargets, err = decodeMultilibTargets(extraMultilib, osTargets, prefer32)
Colin Crossa1ad8d12016-06-01 17:09:44 -0700627 if err != nil {
628 mctx.ModuleErrorf("%s", err.Error())
629 }
Colin Crossb9db4802016-06-03 01:50:47 +0000630 }
631
Colin Crossa6845402020-11-16 15:08:19 -0800632 // Recovery is always the primary architecture, filter out any other architectures.
Inseob Kim20fb5d42021-02-02 20:07:58 +0900633 // Common arch is also allowed
Colin Crossfb0c16e2019-11-20 17:12:35 -0800634 if image == RecoveryVariation {
635 primaryArch := mctx.Config().DevicePrimaryArchType()
Inseob Kim20fb5d42021-02-02 20:07:58 +0900636 targets = filterToArch(targets, primaryArch, Common)
637 multiTargets = filterToArch(multiTargets, primaryArch, Common)
Colin Crossfb0c16e2019-11-20 17:12:35 -0800638 }
639
Colin Crossa6845402020-11-16 15:08:19 -0800640 // If there are no supported targets disable the module.
Colin Crossa195f912019-10-16 11:07:20 -0700641 if len(targets) == 0 {
Inseob Kimeec88e12020-01-22 11:11:29 +0900642 base.Disable()
Dan Willemsen3f32f032016-07-11 14:36:48 -0700643 return
644 }
645
Colin Crossa6845402020-11-16 15:08:19 -0800646 // Convert the targets into a list of arch variation names.
Colin Crossa195f912019-10-16 11:07:20 -0700647 targetNames := make([]string, len(targets))
Colin Crossa195f912019-10-16 11:07:20 -0700648 for i, target := range targets {
649 targetNames[i] = target.ArchVariation()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700650 }
651
Colin Crossa6845402020-11-16 15:08:19 -0800652 // Create the variations, annotate each one with which Target it was created for, and
653 // squash the appropriate arch-specific properties into the top level properties.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700654 modules := mctx.CreateVariations(targetNames...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800655 for i, m := range modules {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000656 addTargetProperties(m, targets[i], multiTargets, i == 0)
Colin Cross617b88a2020-08-24 18:04:09 -0700657 m.base().setArchProperties(mctx)
Dan Willemsen8528f4e2021-10-19 00:22:06 -0700658
659 // Install support doesn't understand Darwin+Arm64
660 if os == Darwin && targets[i].HostCross {
661 m.base().commonProperties.SkipInstall = true
662 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800663 }
Dan Willemsen47450072021-10-19 20:24:49 -0700664
665 // Create a dependency for Darwin Universal binaries from the primary to secondary
666 // architecture. The module itself will be responsible for calling lipo to merge the outputs.
667 if os == Darwin {
668 if multilib == "darwin_universal" && len(modules) == 2 {
669 mctx.AddInterVariantDependency(DarwinUniversalVariantTag, modules[1], modules[0])
670 } else if multilib == "darwin_universal_common_first" && len(modules) == 3 {
671 mctx.AddInterVariantDependency(DarwinUniversalVariantTag, modules[2], modules[1])
672 }
673 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800674}
675
Colin Crossa6845402020-11-16 15:08:19 -0800676// addTargetProperties annotates a variant with the Target is is being compiled for, the list
677// of additional Targets it is supporting (if any), and whether it is the primary Target for
678// the module.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000679func addTargetProperties(m Module, target Target, multiTargets []Target, primaryTarget bool) {
680 m.base().commonProperties.CompileTarget = target
681 m.base().commonProperties.CompileMultiTargets = multiTargets
682 m.base().commonProperties.CompilePrimary = primaryTarget
683}
684
Colin Crossa6845402020-11-16 15:08:19 -0800685// decodeMultilib returns the appropriate compile_multilib property for the module, or the default
686// multilib from the factory's call to InitAndroidArchModule if none was set. For modules that
687// called InitAndroidMultiTargetsArchModule it always returns "common" for multilib, and returns
688// the actual multilib in extraMultilib.
Christopher Ferris98f10222022-07-13 23:16:52 -0700689func decodeMultilib(base *ModuleBase, os OsType, ignorePrefer32OnDevice bool) (multilib, extraMultilib string) {
Colin Crossa6845402020-11-16 15:08:19 -0800690 // First check the "android.compile_multilib" or "host.compile_multilib" properties.
Dan Willemsen47450072021-10-19 20:24:49 -0700691 switch os.Class {
Colin Crossee0bc3b2018-10-02 22:01:37 -0700692 case Device:
693 multilib = String(base.commonProperties.Target.Android.Compile_multilib)
Jiyong Park1613e552020-09-14 19:43:17 +0900694 case Host:
Colin Crossee0bc3b2018-10-02 22:01:37 -0700695 multilib = String(base.commonProperties.Target.Host.Compile_multilib)
696 }
Colin Crossa6845402020-11-16 15:08:19 -0800697
698 // If those aren't set, try the "compile_multilib" property.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700699 if multilib == "" {
700 multilib = String(base.commonProperties.Compile_multilib)
701 }
Colin Crossa6845402020-11-16 15:08:19 -0800702
703 // If that wasn't set, use the default multilib set by the factory.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700704 if multilib == "" {
705 multilib = base.commonProperties.Default_multilib
706 }
707
Christopher Ferris98f10222022-07-13 23:16:52 -0700708 // If a device is configured with multiple targets, this option
709 // force all device targets that prefer32 to be compiled only as
710 // the first target.
711 if ignorePrefer32OnDevice && os.Class == Device && (multilib == "prefer32" || multilib == "first_prefer32") {
712 multilib = "first"
713 }
714
Colin Crossee0bc3b2018-10-02 22:01:37 -0700715 if base.commonProperties.UseTargetVariants {
Dan Willemsen47450072021-10-19 20:24:49 -0700716 // Darwin has the concept of "universal binaries" which is implemented in Soong by
717 // building both x86_64 and arm64 variants, and having select module types know how to
718 // merge the outputs of their corresponding variants together into a final binary. Most
719 // module types don't need to understand this logic, as we only build a small portion
720 // of the tree for Darwin, and only module types writing macho files need to do the
721 // merging.
722 //
723 // This logic is not enabled for:
724 // "common", as it's not an arch-specific variant
725 // "32", as Darwin never has a 32-bit variant
726 // !UseTargetVariants, as the module has opted into handling the arch-specific logic on
727 // its own.
728 if os == Darwin && multilib != "common" && multilib != "32" {
729 if multilib == "common_first" {
730 multilib = "darwin_universal_common_first"
731 } else {
732 multilib = "darwin_universal"
733 }
734 }
735
Colin Crossee0bc3b2018-10-02 22:01:37 -0700736 return multilib, ""
737 } else {
738 // For app modules a single arch variant will be created per OS class which is expected to handle all the
739 // selected arches. Return the common-type as multilib and any Android.bp provided multilib as extraMultilib
740 if multilib == base.commonProperties.Default_multilib {
741 multilib = "first"
742 }
743 return base.commonProperties.Default_multilib, multilib
744 }
745}
746
Colin Crossa6845402020-11-16 15:08:19 -0800747// filterToArch takes a list of Targets and an ArchType, and returns a modified list that contains
Inseob Kim20fb5d42021-02-02 20:07:58 +0900748// only Targets that have the specified ArchTypes.
749func filterToArch(targets []Target, archs ...ArchType) []Target {
Colin Crossfb0c16e2019-11-20 17:12:35 -0800750 for i := 0; i < len(targets); i++ {
Inseob Kim20fb5d42021-02-02 20:07:58 +0900751 found := false
752 for _, arch := range archs {
753 if targets[i].Arch.ArchType == arch {
754 found = true
755 break
756 }
757 }
758 if !found {
Colin Crossfb0c16e2019-11-20 17:12:35 -0800759 targets = append(targets[:i], targets[i+1:]...)
760 i--
761 }
762 }
763 return targets
764}
765
Colin Crossa6845402020-11-16 15:08:19 -0800766// archPropRoot is a struct type used as the top level of the arch-specific properties. It
767// contains the "arch", "multilib", and "target" property structs. It is used to split up the
768// property structs to limit how much is allocated when a single arch-specific property group is
769// used. The types are interface{} because they will hold instances of runtime-created types.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800770type archPropRoot struct {
771 Arch, Multilib, Target interface{}
772}
773
Colin Crossa6845402020-11-16 15:08:19 -0800774// archPropTypeDesc holds the runtime-created types for the property structs to instantiate to
775// create an archPropRoot property struct.
776type archPropTypeDesc struct {
777 arch, multilib, target reflect.Type
778}
779
Colin Crosscbbd13f2020-01-17 14:08:22 -0800780// createArchPropTypeDesc takes a reflect.Type that is either a struct or a pointer to a struct, and
781// returns lists of reflect.Types that contains the arch-variant properties inside structs for each
782// arch, multilib and target property.
Colin Crossa6845402020-11-16 15:08:19 -0800783//
784// This is a relatively expensive operation, so the results are cached in the global
785// archPropTypeMap. It is constructed entirely based on compile-time data, so there is no need
786// to isolate the results between multiple tests running in parallel.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800787func createArchPropTypeDesc(props reflect.Type) []archPropTypeDesc {
Colin Crossb1d8c992020-01-21 11:43:29 -0800788 // Each property struct shard will be nested many times under the runtime generated arch struct,
789 // which can hit the limit of 64kB for the name of runtime generated structs. They are nested
790 // 97 times now, which may grow in the future, plus there is some overhead for the containing
791 // type. This number may need to be reduced if too many are added, but reducing it too far
792 // could cause problems if a single deeply nested property no longer fits in the name.
793 const maxArchTypeNameSize = 500
794
Colin Crossa6845402020-11-16 15:08:19 -0800795 // Convert the type to a new set of types that contains only the arch-specific properties
Usta Shrestha0b52d832022-02-04 21:37:39 -0500796 // (those that are tagged with `android:"arch_variant"`), and sharded into multiple types
Colin Crossa6845402020-11-16 15:08:19 -0800797 // to keep the runtime-generated names under the limit.
Colin Crossb1d8c992020-01-21 11:43:29 -0800798 propShards, _ := proptools.FilterPropertyStructSharded(props, maxArchTypeNameSize, filterArchStruct)
Colin Crossa6845402020-11-16 15:08:19 -0800799
800 // If the type has no arch-specific properties there is nothing to do.
Colin Crosscb988072019-01-24 14:58:11 -0800801 if len(propShards) == 0 {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700802 return nil
803 }
804
Colin Crosscbbd13f2020-01-17 14:08:22 -0800805 var ret []archPropTypeDesc
Colin Crossc17727d2018-10-24 12:42:09 -0700806 for _, props := range propShards {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700807
Colin Crossa6845402020-11-16 15:08:19 -0800808 // variantFields takes a list of variant property field names and returns a list the
809 // StructFields with the names and the type of the current shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700810 variantFields := func(names []string) []reflect.StructField {
811 ret := make([]reflect.StructField, len(names))
Dan Willemsenb1957a52016-06-23 23:44:54 -0700812
Colin Crossc17727d2018-10-24 12:42:09 -0700813 for i, name := range names {
814 ret[i].Name = name
815 ret[i].Type = props
Dan Willemsen866b5632017-09-22 12:28:24 -0700816 }
Colin Crossc17727d2018-10-24 12:42:09 -0700817
818 return ret
819 }
820
Colin Crossa6845402020-11-16 15:08:19 -0800821 // Create a type that contains the properties in this shard repeated for each
822 // architecture, architecture variant, and architecture feature.
Colin Crossc17727d2018-10-24 12:42:09 -0700823 archFields := make([]reflect.StructField, len(archTypeList))
824 for i, arch := range archTypeList {
Colin Crossa6845402020-11-16 15:08:19 -0800825 var variants []string
Colin Crossc17727d2018-10-24 12:42:09 -0700826
827 for _, archVariant := range archVariants[arch] {
828 archVariant := variantReplacer.Replace(archVariant)
829 variants = append(variants, proptools.FieldNameForProperty(archVariant))
830 }
Liz Kammer2c2afe22022-02-11 11:35:03 -0500831 for _, cpuVariant := range cpuVariants[arch] {
832 cpuVariant := variantReplacer.Replace(cpuVariant)
833 variants = append(variants, proptools.FieldNameForProperty(cpuVariant))
834 }
Colin Crossc17727d2018-10-24 12:42:09 -0700835 for _, feature := range archFeatures[arch] {
836 feature := variantReplacer.Replace(feature)
837 variants = append(variants, proptools.FieldNameForProperty(feature))
838 }
839
Colin Crossa6845402020-11-16 15:08:19 -0800840 // Create the StructFields for each architecture variant architecture feature
841 // (e.g. "arch.arm.cortex-a53" or "arch.arm.neon").
Colin Crossc17727d2018-10-24 12:42:09 -0700842 fields := variantFields(variants)
843
Colin Crossa6845402020-11-16 15:08:19 -0800844 // Create the StructField for the architecture itself (e.g. "arch.arm"). The special
845 // "BlueprintEmbed" name is used by Blueprint to put the properties in the
846 // parent struct.
Colin Crossc17727d2018-10-24 12:42:09 -0700847 fields = append([]reflect.StructField{{
848 Name: "BlueprintEmbed",
849 Type: props,
850 Anonymous: true,
851 }}, fields...)
852
853 archFields[i] = reflect.StructField{
854 Name: arch.Field,
855 Type: reflect.StructOf(fields),
856 }
857 }
Colin Crossa6845402020-11-16 15:08:19 -0800858
859 // Create the type of the "arch" property struct for this shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700860 archType := reflect.StructOf(archFields)
861
Colin Crossa6845402020-11-16 15:08:19 -0800862 // Create the type for the "multilib" property struct for this shard, containing the
863 // "multilib.lib32" and "multilib.lib64" property structs.
Colin Crossc17727d2018-10-24 12:42:09 -0700864 multilibType := reflect.StructOf(variantFields([]string{"Lib32", "Lib64"}))
865
Colin Crossa6845402020-11-16 15:08:19 -0800866 // Start with a list of the special targets
Colin Crossc17727d2018-10-24 12:42:09 -0700867 targets := []string{
868 "Host",
869 "Android64",
870 "Android32",
871 "Bionic",
Colin Cross528d67e2021-07-23 22:23:07 +0000872 "Glibc",
873 "Musl",
Colin Crossc17727d2018-10-24 12:42:09 -0700874 "Linux",
Colin Crossa98d36d2022-03-07 14:39:49 -0800875 "Host_linux",
Colin Crossc17727d2018-10-24 12:42:09 -0700876 "Not_windows",
877 "Arm_on_x86",
878 "Arm_on_x86_64",
Victor Khimenkoc26fcf42020-05-07 22:16:33 +0200879 "Native_bridge",
Colin Crossc17727d2018-10-24 12:42:09 -0700880 }
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000881 for _, os := range osTypeList {
Colin Crossa6845402020-11-16 15:08:19 -0800882 // Add all the OSes.
Colin Crossc17727d2018-10-24 12:42:09 -0700883 targets = append(targets, os.Field)
884
Colin Crossa6845402020-11-16 15:08:19 -0800885 // Add the OS/Arch combinations, e.g. "android_arm64".
Colin Crossc17727d2018-10-24 12:42:09 -0700886 for _, archType := range osArchTypeMap[os] {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400887 targets = append(targets, GetCompoundTargetField(os, archType))
Colin Crossc17727d2018-10-24 12:42:09 -0700888
Colin Cross1aa45b02022-02-10 10:33:10 -0800889 // Also add the special "linux_<arch>", "bionic_<arch>" , "glibc_<arch>", and
890 // "musl_<arch>" property structs.
Colin Crossc17727d2018-10-24 12:42:09 -0700891 if os.Linux() {
892 target := "Linux_" + archType.Name
893 if !InList(target, targets) {
894 targets = append(targets, target)
895 }
896 }
Colin Crossa98d36d2022-03-07 14:39:49 -0800897 if os.Linux() && os.Class == Host {
898 target := "Host_linux_" + archType.Name
899 if !InList(target, targets) {
900 targets = append(targets, target)
901 }
902 }
Colin Crossc17727d2018-10-24 12:42:09 -0700903 if os.Bionic() {
904 target := "Bionic_" + archType.Name
905 if !InList(target, targets) {
906 targets = append(targets, target)
907 }
Dan Willemsen866b5632017-09-22 12:28:24 -0700908 }
Colin Cross1aa45b02022-02-10 10:33:10 -0800909 if os == Linux {
910 target := "Glibc_" + archType.Name
911 if !InList(target, targets) {
912 targets = append(targets, target)
913 }
914 }
915 if os == LinuxMusl {
916 target := "Musl_" + archType.Name
917 if !InList(target, targets) {
918 targets = append(targets, target)
919 }
920 }
Dan Willemsen866b5632017-09-22 12:28:24 -0700921 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700922 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700923
Colin Crossa6845402020-11-16 15:08:19 -0800924 // Create the type for the "target" property struct for this shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700925 targetType := reflect.StructOf(variantFields(targets))
Colin Crosscbbd13f2020-01-17 14:08:22 -0800926
Colin Crossa6845402020-11-16 15:08:19 -0800927 // Return a descriptor of the 3 runtime-created types.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800928 ret = append(ret, archPropTypeDesc{
929 arch: reflect.PtrTo(archType),
930 multilib: reflect.PtrTo(multilibType),
931 target: reflect.PtrTo(targetType),
932 })
Colin Crossc17727d2018-10-24 12:42:09 -0700933 }
934 return ret
Dan Willemsenb1957a52016-06-23 23:44:54 -0700935}
936
Colin Crossa6845402020-11-16 15:08:19 -0800937// variantReplacer converts architecture variant or architecture feature names into names that
938// are valid for an Android.bp file.
939var variantReplacer = strings.NewReplacer("-", "_", ".", "_")
940
941// filterArchStruct returns true if the given field is an architecture specific property.
Colin Cross74449102019-09-25 11:26:40 -0700942func filterArchStruct(field reflect.StructField, prefix string) (bool, reflect.StructField) {
943 if proptools.HasTag(field, "android", "arch_variant") {
944 // The arch_variant field isn't necessary past this point
945 // Instead of wasting space, just remove it. Go also has a
946 // 16-bit limit on structure name length. The name is constructed
947 // based on the Go source representation of the structure, so
948 // the tag names count towards that length.
Colin Crossb4fecbf2020-01-21 11:38:47 -0800949
950 androidTag := field.Tag.Get("android")
951 values := strings.Split(androidTag, ",")
952
953 if string(field.Tag) != `android:"`+strings.Join(values, ",")+`"` {
954 panic(fmt.Errorf("unexpected tag format %q", field.Tag))
Colin Cross74449102019-09-25 11:26:40 -0700955 }
Colin Crossb4fecbf2020-01-21 11:38:47 -0800956 // these tags don't need to be present in the runtime generated struct type.
Liz Kammerff966b12022-07-29 10:49:16 -0400957 values = RemoveListFromList(values, []string{"arch_variant", "variant_prepend", "path"})
958 if len(values) > 0 {
Colin Crossb4fecbf2020-01-21 11:38:47 -0800959 panic(fmt.Errorf("unknown tags %q in field %q", values, prefix+field.Name))
960 }
961
Liz Kammerff966b12022-07-29 10:49:16 -0400962 field.Tag = ``
Colin Cross74449102019-09-25 11:26:40 -0700963 return true, field
964 }
965 return false, field
966}
967
Colin Crossa6845402020-11-16 15:08:19 -0800968// archPropTypeMap contains a cache of the results of createArchPropTypeDesc for each type. It is
969// shared across all Contexts, but is constructed based only on compile-time information so there
970// is no risk of contaminating one Context with data from another.
Dan Willemsenb1957a52016-06-23 23:44:54 -0700971var archPropTypeMap OncePer
972
Colin Crossa6845402020-11-16 15:08:19 -0800973// initArchModule adds the architecture-specific property structs to a Module.
974func initArchModule(m Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800975
976 base := m.base()
977
Ustaeabf0f32021-12-06 15:17:23 -0500978 if len(base.archProperties) != 0 {
979 panic(fmt.Errorf("module %s already has archProperties", m.Name()))
980 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800981
Ustaeabf0f32021-12-06 15:17:23 -0500982 getStructType := func(properties interface{}) reflect.Type {
Colin Cross3f40fa42015-01-30 17:27:36 -0800983 propertiesValue := reflect.ValueOf(properties)
Colin Cross62496a02016-08-08 15:49:17 -0700984 t := propertiesValue.Type()
Colin Cross3f40fa42015-01-30 17:27:36 -0800985 if propertiesValue.Kind() != reflect.Ptr {
Colin Crossca860ac2016-01-04 14:34:37 -0800986 panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
987 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800988 }
989
990 propertiesValue = propertiesValue.Elem()
991 if propertiesValue.Kind() != reflect.Struct {
Ustaeabf0f32021-12-06 15:17:23 -0500992 panic(fmt.Errorf("properties must be a pointer to a struct, got a pointer to %T",
Colin Crossca860ac2016-01-04 14:34:37 -0800993 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800994 }
Ustaeabf0f32021-12-06 15:17:23 -0500995 return t
996 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800997
Usta851a3272022-01-05 23:42:33 -0500998 for _, properties := range m.GetProperties() {
Ustaeabf0f32021-12-06 15:17:23 -0500999 t := getStructType(properties)
Colin Crossa6845402020-11-16 15:08:19 -08001000 // Get or create the arch-specific property struct types for this property struct type.
Colin Cross571cccf2019-02-04 11:22:08 -08001001 archPropTypes := archPropTypeMap.Once(NewCustomOnceKey(t), func() interface{} {
Colin Crosscbbd13f2020-01-17 14:08:22 -08001002 return createArchPropTypeDesc(t)
1003 }).([]archPropTypeDesc)
Colin Cross3f40fa42015-01-30 17:27:36 -08001004
Colin Crossa6845402020-11-16 15:08:19 -08001005 // Instantiate one of each arch-specific property struct type and add it to the
1006 // properties for the Module.
Colin Crossc17727d2018-10-24 12:42:09 -07001007 var archProperties []interface{}
1008 for _, t := range archPropTypes {
Colin Crosscbbd13f2020-01-17 14:08:22 -08001009 archProperties = append(archProperties, &archPropRoot{
1010 Arch: reflect.Zero(t.arch).Interface(),
1011 Multilib: reflect.Zero(t.multilib).Interface(),
1012 Target: reflect.Zero(t.target).Interface(),
1013 })
Dan Willemsenb1957a52016-06-23 23:44:54 -07001014 }
Colin Crossc17727d2018-10-24 12:42:09 -07001015 base.archProperties = append(base.archProperties, archProperties)
1016 m.AddProperties(archProperties...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001017 }
1018
Colin Cross3f40fa42015-01-30 17:27:36 -08001019}
1020
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001021func maybeBlueprintEmbed(src reflect.Value) reflect.Value {
Colin Crossa6845402020-11-16 15:08:19 -08001022 // If the value of the field is a struct (as opposed to a pointer to a struct) then step
1023 // into the BlueprintEmbed field.
Dan Willemsenb1957a52016-06-23 23:44:54 -07001024 if src.Kind() == reflect.Struct {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001025 return src.FieldByName("BlueprintEmbed")
1026 } else {
1027 return src
Colin Cross06a931b2015-10-28 17:23:31 -07001028 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001029}
1030
1031// Merges the property struct in srcValue into dst.
Liz Kammerb6dbc872021-05-14 15:14:40 -04001032func mergePropertyStruct(ctx ArchVariantContext, dst interface{}, srcValue reflect.Value) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001033 src := maybeBlueprintEmbed(srcValue).Interface()
Colin Cross06a931b2015-10-28 17:23:31 -07001034
Colin Crossa6845402020-11-16 15:08:19 -08001035 // order checks the `android:"variant_prepend"` tag to handle properties where the
1036 // arch-specific value needs to come before the generic value, for example for lists of
1037 // include directories.
Colin Cross6ee75b62016-05-05 15:57:15 -07001038 order := func(property string,
1039 dstField, srcField reflect.StructField,
1040 dstValue, srcValue interface{}) (proptools.Order, error) {
1041 if proptools.HasTag(dstField, "android", "variant_prepend") {
1042 return proptools.Prepend, nil
1043 } else {
1044 return proptools.Append, nil
1045 }
1046 }
1047
Colin Crossa6845402020-11-16 15:08:19 -08001048 // Squash the located property struct into the destination property struct.
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001049 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src, nil, order)
Colin Cross06a931b2015-10-28 17:23:31 -07001050 if err != nil {
1051 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
1052 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
1053 } else {
1054 panic(err)
1055 }
1056 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001057}
Colin Cross85a88972015-11-23 13:29:51 -08001058
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001059// Returns the immediate child of the input property struct that corresponds to
1060// the sub-property "field".
Liz Kammerb6dbc872021-05-14 15:14:40 -04001061func getChildPropertyStruct(ctx ArchVariantContext,
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001062 src reflect.Value, field, userFriendlyField string) (reflect.Value, bool) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001063
1064 // Step into non-nil pointers to structs in the src value.
1065 if src.Kind() == reflect.Ptr {
1066 if src.IsNil() {
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001067 return reflect.Value{}, false
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001068 }
1069 src = src.Elem()
1070 }
1071
1072 // Find the requested field in the src struct.
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001073 child := src.FieldByName(proptools.FieldNameForProperty(field))
1074 if !child.IsValid() {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001075 ctx.ModuleErrorf("field %q does not exist", userFriendlyField)
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001076 return reflect.Value{}, false
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001077 }
1078
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001079 if child.IsZero() {
1080 return reflect.Value{}, false
1081 }
1082
1083 return child, true
Colin Cross06a931b2015-10-28 17:23:31 -07001084}
1085
Colin Crossa6845402020-11-16 15:08:19 -08001086// Squash the appropriate OS-specific property structs into the matching top level property structs
1087// based on the CompileOS value that was annotated on the variant.
Colin Crossa195f912019-10-16 11:07:20 -07001088func (m *ModuleBase) setOSProperties(ctx BottomUpMutatorContext) {
1089 os := m.commonProperties.CompileOS
1090
Ustadca02192021-12-20 12:56:46 -05001091 for i := range m.archProperties {
Usta851a3272022-01-05 23:42:33 -05001092 genProps := m.GetProperties()[i]
Colin Crossa195f912019-10-16 11:07:20 -07001093 if m.archProperties[i] == nil {
1094 continue
1095 }
1096 for _, archProperties := range m.archProperties[i] {
1097 archPropValues := reflect.ValueOf(archProperties).Elem()
1098
Colin Crosscbbd13f2020-01-17 14:08:22 -08001099 targetProp := archPropValues.FieldByName("Target").Elem()
Colin Crossa195f912019-10-16 11:07:20 -07001100
1101 // Handle host-specific properties in the form:
1102 // target: {
1103 // host: {
1104 // key: value,
1105 // },
1106 // },
Jiyong Park1613e552020-09-14 19:43:17 +09001107 if os.Class == Host {
Colin Crossa195f912019-10-16 11:07:20 -07001108 field := "Host"
1109 prefix := "target.host"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001110 if hostProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1111 mergePropertyStruct(ctx, genProps, hostProperties)
1112 }
Colin Crossa195f912019-10-16 11:07:20 -07001113 }
1114
1115 // Handle target OS generalities of the form:
1116 // target: {
1117 // bionic: {
1118 // key: value,
1119 // },
1120 // }
1121 if os.Linux() {
1122 field := "Linux"
1123 prefix := "target.linux"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001124 if linuxProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1125 mergePropertyStruct(ctx, genProps, linuxProperties)
1126 }
Colin Crossa195f912019-10-16 11:07:20 -07001127 }
1128
Colin Crossa98d36d2022-03-07 14:39:49 -08001129 if os.Linux() && os.Class == Host {
1130 field := "Host_linux"
1131 prefix := "target.host_linux"
1132 if linuxProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1133 mergePropertyStruct(ctx, genProps, linuxProperties)
1134 }
1135 }
1136
Colin Crossa195f912019-10-16 11:07:20 -07001137 if os.Bionic() {
1138 field := "Bionic"
1139 prefix := "target.bionic"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001140 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1141 mergePropertyStruct(ctx, genProps, bionicProperties)
1142 }
Colin Crossa195f912019-10-16 11:07:20 -07001143 }
1144
Colin Cross528d67e2021-07-23 22:23:07 +00001145 if os == Linux {
1146 field := "Glibc"
1147 prefix := "target.glibc"
1148 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1149 mergePropertyStruct(ctx, genProps, bionicProperties)
1150 }
1151 }
1152
1153 if os == LinuxMusl {
1154 field := "Musl"
1155 prefix := "target.musl"
1156 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1157 mergePropertyStruct(ctx, genProps, bionicProperties)
1158 }
Colin Cross528d67e2021-07-23 22:23:07 +00001159 }
1160
Colin Crossa195f912019-10-16 11:07:20 -07001161 // Handle target OS properties in the form:
1162 // target: {
1163 // linux_glibc: {
1164 // key: value,
1165 // },
1166 // not_windows: {
1167 // key: value,
1168 // },
1169 // android {
1170 // key: value,
1171 // },
1172 // },
1173 field := os.Field
1174 prefix := "target." + os.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001175 if osProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1176 mergePropertyStruct(ctx, genProps, osProperties)
1177 }
Colin Crossa195f912019-10-16 11:07:20 -07001178
Jiyong Park1613e552020-09-14 19:43:17 +09001179 if os.Class == Host && os != Windows {
Colin Crossa195f912019-10-16 11:07:20 -07001180 field := "Not_windows"
1181 prefix := "target.not_windows"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001182 if notWindowsProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1183 mergePropertyStruct(ctx, genProps, notWindowsProperties)
1184 }
Colin Crossa195f912019-10-16 11:07:20 -07001185 }
1186
1187 // Handle 64-bit device properties in the form:
1188 // target {
1189 // android64 {
1190 // key: value,
1191 // },
1192 // android32 {
1193 // key: value,
1194 // },
1195 // },
1196 // WARNING: this is probably not what you want to use in your blueprints file, it selects
1197 // options for all targets on a device that supports 64-bit binaries, not just the targets
1198 // that are being compiled for 64-bit. Its expected use case is binaries like linker and
1199 // debuggerd that need to know when they are a 32-bit process running on a 64-bit device
1200 if os.Class == Device {
1201 if ctx.Config().Android64() {
1202 field := "Android64"
1203 prefix := "target.android64"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001204 if android64Properties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1205 mergePropertyStruct(ctx, genProps, android64Properties)
1206 }
Colin Crossa195f912019-10-16 11:07:20 -07001207 } else {
1208 field := "Android32"
1209 prefix := "target.android32"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001210 if android32Properties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1211 mergePropertyStruct(ctx, genProps, android32Properties)
1212 }
Colin Crossa195f912019-10-16 11:07:20 -07001213 }
1214 }
1215 }
1216 }
1217}
1218
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001219// Returns the struct containing the properties specific to the given
1220// architecture type. These look like this in Blueprint files:
Colin Crossd079e0b2022-08-16 10:27:33 -07001221//
1222// arch: {
1223// arm64: {
1224// key: value,
1225// },
1226// },
1227//
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001228// This struct will also contain sub-structs containing to the architecture/CPU
1229// variants and features that themselves contain properties specific to those.
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001230func getArchTypeStruct(ctx ArchVariantContext, archProperties interface{}, archType ArchType) (reflect.Value, bool) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001231 archPropValues := reflect.ValueOf(archProperties).Elem()
1232 archProp := archPropValues.FieldByName("Arch").Elem()
1233 prefix := "arch." + archType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001234 return getChildPropertyStruct(ctx, archProp, archType.Name, prefix)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001235}
1236
1237// Returns the struct containing the properties specific to a given multilib
1238// value. These look like this in the Blueprint file:
Colin Crossd079e0b2022-08-16 10:27:33 -07001239//
1240// multilib: {
1241// lib32: {
1242// key: value,
1243// },
1244// },
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001245func getMultilibStruct(ctx ArchVariantContext, archProperties interface{}, archType ArchType) (reflect.Value, bool) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001246 archPropValues := reflect.ValueOf(archProperties).Elem()
1247 multilibProp := archPropValues.FieldByName("Multilib").Elem()
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001248 return getChildPropertyStruct(ctx, multilibProp, archType.Multilib, "multilib."+archType.Multilib)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001249}
1250
Liz Kammer9abd62d2021-05-21 08:37:59 -04001251func GetCompoundTargetField(os OsType, arch ArchType) string {
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04001252 return os.Field + "_" + arch.Name
1253}
1254
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001255// Returns the structs corresponding to the properties specific to the given
1256// architecture and OS in archProperties.
1257func getArchProperties(ctx BaseMutatorContext, archProperties interface{}, arch Arch, os OsType, nativeBridgeEnabled bool) []reflect.Value {
1258 result := make([]reflect.Value, 0)
1259 archPropValues := reflect.ValueOf(archProperties).Elem()
1260
1261 targetProp := archPropValues.FieldByName("Target").Elem()
1262
1263 archType := arch.ArchType
1264
1265 if arch.ArchType != Common {
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001266 archStruct, ok := getArchTypeStruct(ctx, archProperties, arch.ArchType)
1267 if ok {
1268 result = append(result, archStruct)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001269
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001270 // Handle arch-variant-specific properties in the form:
1271 // arch: {
1272 // arm: {
1273 // variant: {
1274 // key: value,
1275 // },
1276 // },
1277 // },
1278 v := variantReplacer.Replace(arch.ArchVariant)
1279 if v != "" {
1280 prefix := "arch." + archType.Name + "." + v
1281 if variantProperties, ok := getChildPropertyStruct(ctx, archStruct, v, prefix); ok {
1282 result = append(result, variantProperties)
1283 }
1284 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001285
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001286 // Handle cpu-variant-specific properties in the form:
1287 // arch: {
1288 // arm: {
1289 // variant: {
1290 // key: value,
1291 // },
1292 // },
1293 // },
1294 if arch.CpuVariant != arch.ArchVariant {
1295 c := variantReplacer.Replace(arch.CpuVariant)
1296 if c != "" {
1297 prefix := "arch." + archType.Name + "." + c
1298 if cpuVariantProperties, ok := getChildPropertyStruct(ctx, archStruct, c, prefix); ok {
1299 result = append(result, cpuVariantProperties)
1300 }
1301 }
1302 }
1303
1304 // Handle arch-feature-specific properties in the form:
1305 // arch: {
1306 // arm: {
1307 // feature: {
1308 // key: value,
1309 // },
1310 // },
1311 // },
1312 for _, feature := range arch.ArchFeatures {
1313 prefix := "arch." + archType.Name + "." + feature
1314 if featureProperties, ok := getChildPropertyStruct(ctx, archStruct, feature, prefix); ok {
1315 result = append(result, featureProperties)
1316 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001317 }
1318 }
1319
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001320 if multilibProperties, ok := getMultilibStruct(ctx, archProperties, archType); ok {
1321 result = append(result, multilibProperties)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001322 }
1323
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001324 // Handle combined OS-feature and arch specific properties in the form:
1325 // target: {
1326 // bionic_x86: {
1327 // key: value,
1328 // },
1329 // }
1330 if os.Linux() {
1331 field := "Linux_" + arch.ArchType.Name
1332 userFriendlyField := "target.linux_" + arch.ArchType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001333 if linuxProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1334 result = append(result, linuxProperties)
1335 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001336 }
1337
1338 if os.Bionic() {
1339 field := "Bionic_" + archType.Name
1340 userFriendlyField := "target.bionic_" + archType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001341 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1342 result = append(result, bionicProperties)
1343 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001344 }
1345
1346 // Handle combined OS and arch specific properties in the form:
1347 // target: {
1348 // linux_glibc_x86: {
1349 // key: value,
1350 // },
1351 // linux_glibc_arm: {
1352 // key: value,
1353 // },
1354 // android_arm {
1355 // key: value,
1356 // },
1357 // android_x86 {
1358 // key: value,
1359 // },
1360 // },
Liz Kammer9abd62d2021-05-21 08:37:59 -04001361 field := GetCompoundTargetField(os, archType)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001362 userFriendlyField := "target." + os.Name + "_" + archType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001363 if osArchProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1364 result = append(result, osArchProperties)
1365 }
Colin Cross528d67e2021-07-23 22:23:07 +00001366
Colin Cross1aa45b02022-02-10 10:33:10 -08001367 if os == Linux {
1368 field := "Glibc_" + archType.Name
1369 userFriendlyField := "target.glibc_" + "_" + archType.Name
1370 if osArchProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1371 result = append(result, osArchProperties)
1372 }
1373 }
1374
Colin Cross528d67e2021-07-23 22:23:07 +00001375 if os == LinuxMusl {
Colin Cross1aa45b02022-02-10 10:33:10 -08001376 field := "Musl_" + archType.Name
1377 userFriendlyField := "target.musl_" + "_" + archType.Name
1378 if osArchProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1379 result = append(result, osArchProperties)
1380 }
Colin Cross528d67e2021-07-23 22:23:07 +00001381 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001382 }
1383
1384 // Handle arm on x86 properties in the form:
1385 // target {
1386 // arm_on_x86 {
1387 // key: value,
1388 // },
1389 // arm_on_x86_64 {
1390 // key: value,
1391 // },
1392 // },
1393 if os.Class == Device {
1394 if arch.ArchType == X86 && (hasArmAbi(arch) ||
1395 hasArmAndroidArch(ctx.Config().Targets[Android])) {
1396 field := "Arm_on_x86"
1397 userFriendlyField := "target.arm_on_x86"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001398 if armOnX86Properties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1399 result = append(result, armOnX86Properties)
1400 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001401 }
1402 if arch.ArchType == X86_64 && (hasArmAbi(arch) ||
1403 hasArmAndroidArch(ctx.Config().Targets[Android])) {
1404 field := "Arm_on_x86_64"
1405 userFriendlyField := "target.arm_on_x86_64"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001406 if armOnX8664Properties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1407 result = append(result, armOnX8664Properties)
1408 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001409 }
1410 if os == Android && nativeBridgeEnabled {
1411 userFriendlyField := "Native_bridge"
1412 prefix := "target.native_bridge"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001413 if nativeBridgeProperties, ok := getChildPropertyStruct(ctx, targetProp, userFriendlyField, prefix); ok {
1414 result = append(result, nativeBridgeProperties)
1415 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001416 }
1417 }
1418
1419 return result
1420}
1421
Colin Crossa6845402020-11-16 15:08:19 -08001422// Squash the appropriate arch-specific property structs into the matching top level property
1423// structs based on the CompileTarget value that was annotated on the variant.
Colin Cross4157e882019-06-06 16:57:04 -07001424func (m *ModuleBase) setArchProperties(ctx BottomUpMutatorContext) {
1425 arch := m.Arch()
1426 os := m.Os()
Colin Crossd3ba0392015-05-07 14:11:29 -07001427
Ustadca02192021-12-20 12:56:46 -05001428 for i := range m.archProperties {
Usta851a3272022-01-05 23:42:33 -05001429 genProps := m.GetProperties()[i]
Colin Cross4157e882019-06-06 16:57:04 -07001430 if m.archProperties[i] == nil {
Dan Willemsenb1957a52016-06-23 23:44:54 -07001431 continue
1432 }
Dan Willemsenb1957a52016-06-23 23:44:54 -07001433
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001434 propStructs := make([]reflect.Value, 0)
1435 for _, archProperty := range m.archProperties[i] {
1436 propStructShard := getArchProperties(ctx, archProperty, arch, os, m.Target().NativeBridge == NativeBridgeEnabled)
1437 propStructs = append(propStructs, propStructShard...)
1438 }
Dan Willemsenb1957a52016-06-23 23:44:54 -07001439
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001440 for _, propStruct := range propStructs {
1441 mergePropertyStruct(ctx, genProps, propStruct)
Colin Crossbb2e2b72016-12-08 17:23:53 -08001442 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001443 }
1444}
1445
Colin Cross0c66bc62021-07-20 09:47:41 -07001446// determineBuildOS stores the OS and architecture used for host targets used during the build into
Colin Cross528d67e2021-07-23 22:23:07 +00001447// config based on the runtime OS and architecture determined by Go and the product configuration.
Colin Cross0c66bc62021-07-20 09:47:41 -07001448func determineBuildOS(config *config) {
1449 config.BuildOS = func() OsType {
1450 switch runtime.GOOS {
1451 case "linux":
Colin Cross528d67e2021-07-23 22:23:07 +00001452 if Bool(config.productVariables.HostMusl) {
1453 return LinuxMusl
1454 }
Colin Cross0c66bc62021-07-20 09:47:41 -07001455 return Linux
1456 case "darwin":
1457 return Darwin
1458 default:
1459 panic(fmt.Sprintf("unsupported OS: %s", runtime.GOOS))
1460 }
1461 }()
1462
1463 config.BuildArch = func() ArchType {
1464 switch runtime.GOARCH {
1465 case "amd64":
1466 return X86_64
1467 default:
1468 panic(fmt.Sprintf("unsupported Arch: %s", runtime.GOARCH))
1469 }
1470 }()
1471
1472}
1473
Colin Crossa6845402020-11-16 15:08:19 -08001474// Convert the arch product variables into a list of targets for each OsType.
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001475func decodeTargetProductVariables(config *config) (map[OsType][]Target, error) {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001476 variables := config.productVariables
Dan Willemsen490fd492015-11-24 17:53:15 -08001477
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001478 targets := make(map[OsType][]Target)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001479 var targetErr error
1480
Liz Kammerb7f33662022-02-28 14:16:16 -05001481 type targetConfig struct {
1482 os OsType
1483 archName string
1484 archVariant *string
1485 cpuVariant *string
1486 abi []string
1487 nativeBridgeEnabled NativeBridgeSupport
1488 nativeBridgeHostArchName *string
1489 nativeBridgeRelativePath *string
1490 }
1491
1492 addTarget := func(target targetConfig) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001493 if targetErr != nil {
1494 return
Dan Willemsen490fd492015-11-24 17:53:15 -08001495 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001496
Liz Kammerb7f33662022-02-28 14:16:16 -05001497 arch, err := decodeArch(target.os, target.archName, target.archVariant, target.cpuVariant, target.abi)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001498 if err != nil {
1499 targetErr = err
1500 return
1501 }
Liz Kammerb7f33662022-02-28 14:16:16 -05001502 nativeBridgeRelativePathStr := String(target.nativeBridgeRelativePath)
1503 nativeBridgeHostArchNameStr := String(target.nativeBridgeHostArchName)
dimitry8d6dde82019-07-11 10:23:53 +02001504
1505 // Use guest arch as relative install path by default
Liz Kammerb7f33662022-02-28 14:16:16 -05001506 if target.nativeBridgeEnabled && nativeBridgeRelativePathStr == "" {
dimitry8d6dde82019-07-11 10:23:53 +02001507 nativeBridgeRelativePathStr = arch.ArchType.String()
1508 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001509
Jiyong Park1613e552020-09-14 19:43:17 +09001510 // A target is considered as HostCross if it's a host target which can't run natively on
1511 // the currently configured build machine (either because the OS is different or because of
1512 // the unsupported arch)
1513 hostCross := false
Liz Kammerb7f33662022-02-28 14:16:16 -05001514 if target.os.Class == Host {
Jiyong Park1613e552020-09-14 19:43:17 +09001515 var osSupported bool
Liz Kammerb7f33662022-02-28 14:16:16 -05001516 if target.os == config.BuildOS {
Jiyong Park1613e552020-09-14 19:43:17 +09001517 osSupported = true
Liz Kammerb7f33662022-02-28 14:16:16 -05001518 } else if config.BuildOS.Linux() && target.os.Linux() {
Jiyong Park1613e552020-09-14 19:43:17 +09001519 // LinuxBionic and Linux are compatible
1520 osSupported = true
1521 } else {
1522 osSupported = false
1523 }
1524
1525 var archSupported bool
1526 if arch.ArchType == Common {
1527 archSupported = true
1528 } else if arch.ArchType.Name == *variables.HostArch {
1529 archSupported = true
1530 } else if variables.HostSecondaryArch != nil && arch.ArchType.Name == *variables.HostSecondaryArch {
1531 archSupported = true
1532 } else {
1533 archSupported = false
1534 }
1535 if !osSupported || !archSupported {
1536 hostCross = true
1537 }
1538 }
1539
Liz Kammerb7f33662022-02-28 14:16:16 -05001540 targets[target.os] = append(targets[target.os],
Colin Crossa1ad8d12016-06-01 17:09:44 -07001541 Target{
Liz Kammerb7f33662022-02-28 14:16:16 -05001542 Os: target.os,
dimitry8d6dde82019-07-11 10:23:53 +02001543 Arch: arch,
Liz Kammerb7f33662022-02-28 14:16:16 -05001544 NativeBridge: target.nativeBridgeEnabled,
dimitry8d6dde82019-07-11 10:23:53 +02001545 NativeBridgeHostArchName: nativeBridgeHostArchNameStr,
1546 NativeBridgeRelativePath: nativeBridgeRelativePathStr,
Jiyong Park1613e552020-09-14 19:43:17 +09001547 HostCross: hostCross,
Colin Crossa1ad8d12016-06-01 17:09:44 -07001548 })
Dan Willemsen490fd492015-11-24 17:53:15 -08001549 }
1550
Colin Cross4225f652015-09-17 14:33:42 -07001551 if variables.HostArch == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001552 return nil, fmt.Errorf("No host primary architecture set")
Colin Cross4225f652015-09-17 14:33:42 -07001553 }
1554
Colin Crossa6845402020-11-16 15:08:19 -08001555 // The primary host target, which must always exist.
Liz Kammerb7f33662022-02-28 14:16:16 -05001556 addTarget(targetConfig{os: config.BuildOS, archName: *variables.HostArch, nativeBridgeEnabled: NativeBridgeDisabled})
Colin Cross4225f652015-09-17 14:33:42 -07001557
Colin Crossa6845402020-11-16 15:08:19 -08001558 // An optional secondary host target.
Colin Crosseeabb892015-11-20 13:07:51 -08001559 if variables.HostSecondaryArch != nil && *variables.HostSecondaryArch != "" {
Liz Kammerb7f33662022-02-28 14:16:16 -05001560 addTarget(targetConfig{os: config.BuildOS, archName: *variables.HostSecondaryArch, nativeBridgeEnabled: NativeBridgeDisabled})
Dan Willemsen490fd492015-11-24 17:53:15 -08001561 }
1562
Colin Crossa6845402020-11-16 15:08:19 -08001563 // Optional cross-compiled host targets, generally Windows.
Colin Crossff3ae9d2018-04-10 16:15:18 -07001564 if String(variables.CrossHost) != "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001565 crossHostOs := osByName(*variables.CrossHost)
1566 if crossHostOs == NoOsType {
1567 return nil, fmt.Errorf("Unknown cross host OS %q", *variables.CrossHost)
1568 }
1569
Colin Crossff3ae9d2018-04-10 16:15:18 -07001570 if String(variables.CrossHostArch) == "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001571 return nil, fmt.Errorf("No cross-host primary architecture set")
Dan Willemsen490fd492015-11-24 17:53:15 -08001572 }
1573
Colin Crossa6845402020-11-16 15:08:19 -08001574 // The primary cross-compiled host target.
Liz Kammerb7f33662022-02-28 14:16:16 -05001575 addTarget(targetConfig{os: crossHostOs, archName: *variables.CrossHostArch, nativeBridgeEnabled: NativeBridgeDisabled})
Dan Willemsen490fd492015-11-24 17:53:15 -08001576
Colin Crossa6845402020-11-16 15:08:19 -08001577 // An optional secondary cross-compiled host target.
Dan Willemsen490fd492015-11-24 17:53:15 -08001578 if variables.CrossHostSecondaryArch != nil && *variables.CrossHostSecondaryArch != "" {
Liz Kammerb7f33662022-02-28 14:16:16 -05001579 addTarget(targetConfig{os: crossHostOs, archName: *variables.CrossHostSecondaryArch, nativeBridgeEnabled: NativeBridgeDisabled})
Dan Willemsen490fd492015-11-24 17:53:15 -08001580 }
1581 }
1582
Colin Crossa6845402020-11-16 15:08:19 -08001583 // Optional device targets
Dan Willemsen3f32f032016-07-11 14:36:48 -07001584 if variables.DeviceArch != nil && *variables.DeviceArch != "" {
Colin Crossa6845402020-11-16 15:08:19 -08001585 // The primary device target.
Liz Kammerb7f33662022-02-28 14:16:16 -05001586 addTarget(targetConfig{
1587 os: Android,
1588 archName: *variables.DeviceArch,
1589 archVariant: variables.DeviceArchVariant,
1590 cpuVariant: variables.DeviceCpuVariant,
1591 abi: variables.DeviceAbi,
1592 nativeBridgeEnabled: NativeBridgeDisabled,
1593 })
Colin Cross4225f652015-09-17 14:33:42 -07001594
Colin Crossa6845402020-11-16 15:08:19 -08001595 // An optional secondary device target.
Dan Willemsen3f32f032016-07-11 14:36:48 -07001596 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" {
Liz Kammerb7f33662022-02-28 14:16:16 -05001597 addTarget(targetConfig{
1598 os: Android,
1599 archName: *variables.DeviceSecondaryArch,
1600 archVariant: variables.DeviceSecondaryArchVariant,
1601 cpuVariant: variables.DeviceSecondaryCpuVariant,
1602 abi: variables.DeviceSecondaryAbi,
1603 nativeBridgeEnabled: NativeBridgeDisabled,
1604 })
Colin Cross4225f652015-09-17 14:33:42 -07001605 }
dimitry1f33e402019-03-26 12:39:31 +01001606
Colin Crossa6845402020-11-16 15:08:19 -08001607 // An optional NativeBridge device target.
dimitry1f33e402019-03-26 12:39:31 +01001608 if variables.NativeBridgeArch != nil && *variables.NativeBridgeArch != "" {
Liz Kammerb7f33662022-02-28 14:16:16 -05001609 addTarget(targetConfig{
1610 os: Android,
1611 archName: *variables.NativeBridgeArch,
1612 archVariant: variables.NativeBridgeArchVariant,
1613 cpuVariant: variables.NativeBridgeCpuVariant,
1614 abi: variables.NativeBridgeAbi,
1615 nativeBridgeEnabled: NativeBridgeEnabled,
1616 nativeBridgeHostArchName: variables.DeviceArch,
1617 nativeBridgeRelativePath: variables.NativeBridgeRelativePath,
1618 })
dimitry1f33e402019-03-26 12:39:31 +01001619 }
1620
Colin Crossa6845402020-11-16 15:08:19 -08001621 // An optional secondary NativeBridge device target.
dimitry1f33e402019-03-26 12:39:31 +01001622 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" &&
1623 variables.NativeBridgeSecondaryArch != nil && *variables.NativeBridgeSecondaryArch != "" {
Liz Kammerb7f33662022-02-28 14:16:16 -05001624 addTarget(targetConfig{
1625 os: Android,
1626 archName: *variables.NativeBridgeSecondaryArch,
1627 archVariant: variables.NativeBridgeSecondaryArchVariant,
1628 cpuVariant: variables.NativeBridgeSecondaryCpuVariant,
1629 abi: variables.NativeBridgeSecondaryAbi,
1630 nativeBridgeEnabled: NativeBridgeEnabled,
1631 nativeBridgeHostArchName: variables.DeviceSecondaryArch,
1632 nativeBridgeRelativePath: variables.NativeBridgeSecondaryRelativePath,
1633 })
dimitry1f33e402019-03-26 12:39:31 +01001634 }
Colin Cross4225f652015-09-17 14:33:42 -07001635 }
1636
Colin Crossa1ad8d12016-06-01 17:09:44 -07001637 if targetErr != nil {
1638 return nil, targetErr
1639 }
1640
1641 return targets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001642}
1643
Colin Crossbb2e2b72016-12-08 17:23:53 -08001644// hasArmAbi returns true if arch has at least one arm ABI
1645func hasArmAbi(arch Arch) bool {
Jaewoong Jung3aff5782020-02-11 07:54:35 -08001646 return PrefixInList(arch.Abi, "arm")
Colin Crossbb2e2b72016-12-08 17:23:53 -08001647}
1648
Lev Rumyantsev34581212021-10-13 09:47:59 -07001649// hasArmAndroidArch returns true if targets has at least
1650// one arm Android arch (possibly native bridged)
Colin Cross4247f0d2017-04-13 16:56:14 -07001651func hasArmAndroidArch(targets []Target) bool {
1652 for _, target := range targets {
Lev Rumyantsev34581212021-10-13 09:47:59 -07001653 if target.Os == Android &&
1654 (target.Arch.ArchType == Arm || target.Arch.ArchType == Arm64) {
Victor Khimenko5eb8ec12018-03-21 20:30:54 +01001655 return true
1656 }
1657 }
1658 return false
1659}
1660
Colin Crossa6845402020-11-16 15:08:19 -08001661// archConfig describes a built-in configuration.
Dan Albert4098deb2016-10-19 14:04:41 -07001662type archConfig struct {
1663 arch string
1664 archVariant string
1665 cpuVariant string
1666 abi []string
1667}
1668
Dan Albertf1d14c72020-07-30 14:32:55 -07001669// getNdkAbisConfig returns the list of archConfigs that are used for bulding
1670// the API stubs and static libraries that are included in the NDK. These are
1671// built *without Neon*, because non-Neon is still supported and building these
1672// with Neon will break those users.
Dan Albert4098deb2016-10-19 14:04:41 -07001673func getNdkAbisConfig() []archConfig {
1674 return []archConfig{
Tamas Petzbca786d2021-01-20 18:56:33 +01001675 {"arm64", "armv8-a-branchprot", "", []string{"arm64-v8a"}},
Inseob Kim5219c0e2021-06-17 00:33:00 +09001676 {"arm", "armv7-a", "", []string{"armeabi-v7a"}},
Dan Albert4098deb2016-10-19 14:04:41 -07001677 {"x86_64", "", "", []string{"x86_64"}},
Inseob Kim5219c0e2021-06-17 00:33:00 +09001678 {"x86", "", "", []string{"x86"}},
Dan Albert4098deb2016-10-19 14:04:41 -07001679 }
1680}
1681
Colin Crossa6845402020-11-16 15:08:19 -08001682// getAmlAbisConfig returns a list of archConfigs for the ABIs supported by mainline modules.
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001683func getAmlAbisConfig() []archConfig {
1684 return []archConfig{
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001685 {"arm64", "armv8-a", "", []string{"arm64-v8a"}},
Inseob Kim5219c0e2021-06-17 00:33:00 +09001686 {"arm", "armv7-a-neon", "", []string{"armeabi-v7a"}},
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001687 {"x86_64", "", "", []string{"x86_64"}},
Inseob Kim5219c0e2021-06-17 00:33:00 +09001688 {"x86", "", "", []string{"x86"}},
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001689 }
1690}
1691
Colin Crossa6845402020-11-16 15:08:19 -08001692// decodeArchSettings converts a list of archConfigs into a list of Targets for the given OsType.
Liz Kammerb7f33662022-02-28 14:16:16 -05001693func decodeAndroidArchSettings(archConfigs []archConfig) ([]Target, error) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001694 var ret []Target
Dan Willemsen322acaf2016-01-12 23:07:05 -08001695
Dan Albert4098deb2016-10-19 14:04:41 -07001696 for _, config := range archConfigs {
Liz Kammerb7f33662022-02-28 14:16:16 -05001697 arch, err := decodeArch(Android, config.arch, &config.archVariant,
Colin Crossa74ca042019-01-31 14:31:51 -08001698 &config.cpuVariant, config.abi)
Dan Willemsen322acaf2016-01-12 23:07:05 -08001699 if err != nil {
1700 return nil, err
1701 }
Colin Cross3b19f5d2019-09-17 14:45:31 -07001702
Colin Crossa1ad8d12016-06-01 17:09:44 -07001703 ret = append(ret, Target{
1704 Os: Android,
1705 Arch: arch,
1706 })
Dan Willemsen322acaf2016-01-12 23:07:05 -08001707 }
1708
1709 return ret, nil
1710}
1711
Colin Crossa6845402020-11-16 15:08:19 -08001712// decodeArch converts a set of strings from product variables into an Arch struct.
Colin Crossa74ca042019-01-31 14:31:51 -08001713func decodeArch(os OsType, arch string, archVariant, cpuVariant *string, abi []string) (Arch, error) {
Colin Crossa6845402020-11-16 15:08:19 -08001714 // Verify the arch is valid
Colin Crosseeabb892015-11-20 13:07:51 -08001715 archType, ok := archTypeMap[arch]
1716 if !ok {
1717 return Arch{}, fmt.Errorf("unknown arch %q", arch)
1718 }
Colin Cross4225f652015-09-17 14:33:42 -07001719
Colin Crosseeabb892015-11-20 13:07:51 -08001720 a := Arch{
Colin Cross4225f652015-09-17 14:33:42 -07001721 ArchType: archType,
Colin Crossa6845402020-11-16 15:08:19 -08001722 ArchVariant: String(archVariant),
1723 CpuVariant: String(cpuVariant),
Colin Crossa74ca042019-01-31 14:31:51 -08001724 Abi: abi,
Colin Crosseeabb892015-11-20 13:07:51 -08001725 }
1726
Colin Crossa6845402020-11-16 15:08:19 -08001727 // Convert generic arch variants into the empty string.
Colin Crosseeabb892015-11-20 13:07:51 -08001728 if a.ArchVariant == a.ArchType.Name || a.ArchVariant == "generic" {
1729 a.ArchVariant = ""
1730 }
1731
Colin Crossa6845402020-11-16 15:08:19 -08001732 // Convert generic CPU variants into the empty string.
Colin Crosseeabb892015-11-20 13:07:51 -08001733 if a.CpuVariant == a.ArchType.Name || a.CpuVariant == "generic" {
1734 a.CpuVariant = ""
1735 }
1736
Liz Kammer2c2afe22022-02-11 11:35:03 -05001737 if a.ArchVariant != "" {
1738 if validArchVariants := archVariants[archType]; !InList(a.ArchVariant, validArchVariants) {
1739 return Arch{}, fmt.Errorf("[%q] unknown arch variant %q, support variants: %q", archType, a.ArchVariant, validArchVariants)
1740 }
1741 }
1742
1743 if a.CpuVariant != "" {
1744 if validCpuVariants := cpuVariants[archType]; !InList(a.CpuVariant, validCpuVariants) {
1745 return Arch{}, fmt.Errorf("[%q] unknown cpu variant %q, support variants: %q", archType, a.CpuVariant, validCpuVariants)
1746 }
1747 }
1748
Colin Crossa6845402020-11-16 15:08:19 -08001749 // Filter empty ABIs out of the list.
Colin Crosseeabb892015-11-20 13:07:51 -08001750 for i := 0; i < len(a.Abi); i++ {
1751 if a.Abi[i] == "" {
1752 a.Abi = append(a.Abi[:i], a.Abi[i+1:]...)
1753 i--
1754 }
1755 }
1756
Liz Kammere8303bd2022-02-16 09:02:48 -05001757 // Set ArchFeatures from the arch type. for Android OS, other os-es do not specify features
1758 if os == Android {
1759 if featureMap, ok := androidArchFeatureMap[archType]; ok {
Dan Willemsen01a3c252019-01-11 19:02:16 -08001760 a.ArchFeatures = featureMap[a.ArchVariant]
1761 }
Colin Crossc5c24ad2015-11-20 15:35:00 -08001762 }
1763
Colin Crosseeabb892015-11-20 13:07:51 -08001764 return a, nil
Colin Cross4225f652015-09-17 14:33:42 -07001765}
1766
Colin Crossa6845402020-11-16 15:08:19 -08001767// filterMultilibTargets takes a list of Targets and a multilib value and returns a new list of
1768// Targets containing only those that have the given multilib value.
Colin Cross69617d32016-09-06 10:39:07 -07001769func filterMultilibTargets(targets []Target, multilib string) []Target {
1770 var ret []Target
1771 for _, t := range targets {
1772 if t.Arch.ArchType.Multilib == multilib {
1773 ret = append(ret, t)
1774 }
1775 }
1776 return ret
1777}
1778
Colin Crossa6845402020-11-16 15:08:19 -08001779// getCommonTargets returns the set of Os specific common architecture targets for each Os in a list
1780// of targets.
Nan Zhangdb0b9a32017-02-27 10:12:13 -08001781func getCommonTargets(targets []Target) []Target {
1782 var ret []Target
1783 set := make(map[string]bool)
1784
1785 for _, t := range targets {
1786 if _, found := set[t.Os.String()]; !found {
1787 set[t.Os.String()] = true
Colin Cross39a18142022-06-24 18:43:40 -07001788 common := commonTargetMap[t.Os.String()]
1789 common.HostCross = t.HostCross
1790 ret = append(ret, common)
Nan Zhangdb0b9a32017-02-27 10:12:13 -08001791 }
1792 }
1793
1794 return ret
1795}
1796
Sam Delmericocc271e22022-06-01 15:45:02 +00001797// FirstTarget takes a list of Targets and a list of multilib values and returns a list of Targets
Colin Cross3b56c922022-07-20 17:37:37 +00001798// that contains zero or one Target for each OsType, selecting the one that matches the earliest
1799// filter.
Sam Delmericocc271e22022-06-01 15:45:02 +00001800func FirstTarget(targets []Target, filters ...string) []Target {
Jiyong Park22101982020-09-17 19:09:58 +09001801 // find the first target from each OS
1802 var ret []Target
Colin Cross3b56c922022-07-20 17:37:37 +00001803 hasHost := false
1804 set := make(map[OsType]bool)
Jiyong Park22101982020-09-17 19:09:58 +09001805
Colin Cross6b4a32d2017-12-05 13:42:45 -08001806 for _, filter := range filters {
1807 buildTargets := filterMultilibTargets(targets, filter)
Jiyong Park22101982020-09-17 19:09:58 +09001808 for _, t := range buildTargets {
Colin Cross3b56c922022-07-20 17:37:37 +00001809 if _, found := set[t.Os]; !found {
1810 hasHost = hasHost || (t.Os.Class == Host)
1811 set[t.Os] = true
Jiyong Park22101982020-09-17 19:09:58 +09001812 ret = append(ret, t)
1813 }
Colin Cross6b4a32d2017-12-05 13:42:45 -08001814 }
1815 }
Jiyong Park22101982020-09-17 19:09:58 +09001816 return ret
Colin Cross6b4a32d2017-12-05 13:42:45 -08001817}
1818
Colin Crossa6845402020-11-16 15:08:19 -08001819// decodeMultilibTargets uses the module's multilib setting to select one or more targets from a
1820// list of Targets.
Colin Crossee0bc3b2018-10-02 22:01:37 -07001821func decodeMultilibTargets(multilib string, targets []Target, prefer32 bool) ([]Target, error) {
Colin Crossa6845402020-11-16 15:08:19 -08001822 var buildTargets []Target
Colin Cross6b4a32d2017-12-05 13:42:45 -08001823
Colin Cross4225f652015-09-17 14:33:42 -07001824 switch multilib {
1825 case "common":
Colin Cross6b4a32d2017-12-05 13:42:45 -08001826 buildTargets = getCommonTargets(targets)
1827 case "common_first":
1828 buildTargets = getCommonTargets(targets)
1829 if prefer32 {
Sam Delmericocc271e22022-06-01 15:45:02 +00001830 buildTargets = append(buildTargets, FirstTarget(targets, "lib32", "lib64")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001831 } else {
Sam Delmericocc271e22022-06-01 15:45:02 +00001832 buildTargets = append(buildTargets, FirstTarget(targets, "lib64", "lib32")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001833 }
Colin Cross4225f652015-09-17 14:33:42 -07001834 case "both":
Colin Cross8b74d172016-09-13 09:59:14 -07001835 if prefer32 {
1836 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1837 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1838 } else {
1839 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1840 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1841 }
Colin Cross4225f652015-09-17 14:33:42 -07001842 case "32":
Colin Cross69617d32016-09-06 10:39:07 -07001843 buildTargets = filterMultilibTargets(targets, "lib32")
Colin Cross4225f652015-09-17 14:33:42 -07001844 case "64":
Colin Cross69617d32016-09-06 10:39:07 -07001845 buildTargets = filterMultilibTargets(targets, "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001846 case "first":
1847 if prefer32 {
Sam Delmericocc271e22022-06-01 15:45:02 +00001848 buildTargets = FirstTarget(targets, "lib32", "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001849 } else {
Sam Delmericocc271e22022-06-01 15:45:02 +00001850 buildTargets = FirstTarget(targets, "lib64", "lib32")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001851 }
Victor Chang9448e8f2020-09-14 15:34:16 +01001852 case "first_prefer32":
Sam Delmericocc271e22022-06-01 15:45:02 +00001853 buildTargets = FirstTarget(targets, "lib32", "lib64")
Colin Cross69617d32016-09-06 10:39:07 -07001854 case "prefer32":
Colin Cross3dceee32018-09-06 10:19:57 -07001855 buildTargets = filterMultilibTargets(targets, "lib32")
1856 if len(buildTargets) == 0 {
1857 buildTargets = filterMultilibTargets(targets, "lib64")
1858 }
Dan Willemsen47450072021-10-19 20:24:49 -07001859 case "darwin_universal":
1860 buildTargets = filterMultilibTargets(targets, "lib64")
1861 // Reverse the targets so that the first architecture can depend on the second
1862 // architecture module in order to merge the outputs.
1863 reverseSliceInPlace(buildTargets)
1864 case "darwin_universal_common_first":
1865 archTargets := filterMultilibTargets(targets, "lib64")
1866 reverseSliceInPlace(archTargets)
1867 buildTargets = append(getCommonTargets(targets), archTargets...)
Colin Cross4225f652015-09-17 14:33:42 -07001868 default:
Victor Chang9448e8f2020-09-14 15:34:16 +01001869 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 -07001870 multilib)
Colin Cross4225f652015-09-17 14:33:42 -07001871 }
1872
Colin Crossa1ad8d12016-06-01 17:09:44 -07001873 return buildTargets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001874}
Jingwen Chen5d864492021-02-24 07:20:12 -05001875
Chris Parsonsc424b762021-04-29 18:06:50 -04001876func (m *ModuleBase) getArchPropertySet(propertySet interface{}, archType ArchType) interface{} {
1877 archString := archType.Field
1878 for i := range m.archProperties {
1879 if m.archProperties[i] == nil {
1880 // Skip over nil properties
1881 continue
1882 }
1883
1884 // Not archProperties are usable; this function looks for properties of a very specific
1885 // form, and ignores the rest.
1886 for _, archProperty := range m.archProperties[i] {
1887 // archPropValue is a property struct, we are looking for the form:
1888 // `arch: { arm: { key: value, ... }}`
1889 archPropValue := reflect.ValueOf(archProperty).Elem()
1890
1891 // Unwrap src so that it should looks like a pointer to `arm: { key: value, ... }`
1892 src := archPropValue.FieldByName("Arch").Elem()
1893
1894 // Step into non-nil pointers to structs in the src value.
1895 if src.Kind() == reflect.Ptr {
1896 if src.IsNil() {
1897 continue
1898 }
1899 src = src.Elem()
1900 }
1901
1902 // Find the requested field (e.g. arm, x86) in the src struct.
1903 src = src.FieldByName(archString)
1904
1905 // We only care about structs.
1906 if !src.IsValid() || src.Kind() != reflect.Struct {
1907 continue
1908 }
1909
1910 // If the value of the field is a struct then step into the
1911 // BlueprintEmbed field. The special "BlueprintEmbed" name is
1912 // used by createArchPropTypeDesc to embed the arch properties
1913 // in the parent struct, so the src arch prop should be in this
1914 // field.
1915 //
1916 // See createArchPropTypeDesc for more details on how Arch-specific
1917 // module properties are processed from the nested props and written
1918 // into the module's archProperties.
1919 src = src.FieldByName("BlueprintEmbed")
1920
1921 // Clone the destination prop, since we want a unique prop struct per arch.
1922 propertySetClone := reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
1923
1924 // Copy the located property struct into the cloned destination property struct.
1925 err := proptools.ExtendMatchingProperties([]interface{}{propertySetClone}, src.Interface(), nil, proptools.OrderReplace)
1926 if err != nil {
1927 // This is fine, it just means the src struct doesn't match the type of propertySet.
1928 continue
1929 }
1930
1931 return propertySetClone
1932 }
1933 }
1934 // No property set was found specific to the given arch, so return an empty
1935 // property set.
1936 return reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
1937}
1938
1939// getMultilibPropertySet returns a property set struct matching the type of
1940// `propertySet`, containing multilib-specific module properties for the given architecture.
1941// If no multilib-specific properties exist for the given architecture, returns an empty property
1942// set matching `propertySet`'s type.
1943func (m *ModuleBase) getMultilibPropertySet(propertySet interface{}, archType ArchType) interface{} {
1944 // archType.Multilib is lowercase (for example, lib32) but property struct field is
1945 // capitalized, such as Lib32, so use strings.Title to capitalize it.
1946 multiLibString := strings.Title(archType.Multilib)
1947
1948 for i := range m.archProperties {
1949 if m.archProperties[i] == nil {
1950 // Skip over nil properties
1951 continue
1952 }
1953
1954 // Not archProperties are usable; this function looks for properties of a very specific
1955 // form, and ignores the rest.
1956 for _, archProperties := range m.archProperties[i] {
1957 // archPropValue is a property struct, we are looking for the form:
1958 // `multilib: { lib32: { key: value, ... }}`
1959 archPropValue := reflect.ValueOf(archProperties).Elem()
1960
1961 // Unwrap src so that it should looks like a pointer to `lib32: { key: value, ... }`
1962 src := archPropValue.FieldByName("Multilib").Elem()
1963
1964 // Step into non-nil pointers to structs in the src value.
1965 if src.Kind() == reflect.Ptr {
1966 if src.IsNil() {
1967 // Ignore nil pointers.
1968 continue
1969 }
1970 src = src.Elem()
1971 }
1972
1973 // Find the requested field (e.g. lib32) in the src struct.
1974 src = src.FieldByName(multiLibString)
1975
1976 // We only care about valid struct pointers.
1977 if !src.IsValid() || src.Kind() != reflect.Ptr || src.Elem().Kind() != reflect.Struct {
1978 continue
1979 }
1980
1981 // Get the zero value for the requested property set.
1982 propertySetClone := reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
1983
1984 // Copy the located property struct into the "zero" property set struct.
1985 err := proptools.ExtendMatchingProperties([]interface{}{propertySetClone}, src.Interface(), nil, proptools.OrderReplace)
1986
1987 if err != nil {
1988 // This is fine, it just means the src struct doesn't match.
1989 continue
1990 }
1991
1992 return propertySetClone
1993 }
1994 }
1995
1996 // There were no multilib properties specifically matching the given archtype.
1997 // Return zeroed value.
1998 return reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
1999}
2000
Liz Kammerb6dbc872021-05-14 15:14:40 -04002001// ArchVariantContext defines the limited context necessary to retrieve arch_variant properties.
2002type ArchVariantContext interface {
2003 ModuleErrorf(fmt string, args ...interface{})
2004 PropertyErrorf(property, fmt string, args ...interface{})
2005}
2006
Liz Kammer9abd62d2021-05-21 08:37:59 -04002007// ArchVariantProperties represents a map of arch-variant config strings to a property interface{}.
2008type ArchVariantProperties map[string]interface{}
2009
2010// ConfigurationAxisToArchVariantProperties represents a map of bazel.ConfigurationAxis to
2011// ArchVariantProperties, such that each independent arch-variant axis maps to the
2012// configs/properties for that axis.
2013type ConfigurationAxisToArchVariantProperties map[bazel.ConfigurationAxis]ArchVariantProperties
2014
2015// GetArchVariantProperties returns a ConfigurationAxisToArchVariantProperties where the
2016// arch-variant properties correspond to the values of the properties of the 'propertySet' struct
2017// that are specific to that axis/configuration. Each axis is independent, containing
2018// non-overlapping configs that correspond to the various "arch-variant" support, at this time:
Colin Crossd079e0b2022-08-16 10:27:33 -07002019//
2020// arches (including multilib)
2021// oses
2022// arch+os combinations
Jingwen Chen5d864492021-02-24 07:20:12 -05002023//
Liz Kammer9abd62d2021-05-21 08:37:59 -04002024// For example, passing a struct { Foo bool, Bar string } will return an interface{} that can be
2025// type asserted back into the same struct, containing the config-specific property value specified
2026// by the module if defined.
Chris Parsonsc424b762021-04-29 18:06:50 -04002027//
2028// Arch-specific properties may come from an arch stanza or a multilib stanza; properties
2029// in these stanzas are combined.
2030// For example: `arch: { x86: { Foo: ["bar"] } }, multilib: { lib32: {` Foo: ["baz"] } }`
2031// will result in `Foo: ["bar", "baz"]` being returned for architecture x86, if the given
2032// propertyset contains `Foo []string`.
Liz Kammer9abd62d2021-05-21 08:37:59 -04002033func (m *ModuleBase) GetArchVariantProperties(ctx ArchVariantContext, propertySet interface{}) ConfigurationAxisToArchVariantProperties {
Jingwen Chen5d864492021-02-24 07:20:12 -05002034 // Return value of the arch types to the prop values for that arch.
Liz Kammer9abd62d2021-05-21 08:37:59 -04002035 axisToProps := ConfigurationAxisToArchVariantProperties{}
Jingwen Chen5d864492021-02-24 07:20:12 -05002036
2037 // Nothing to do for non-arch-specific modules.
2038 if !m.ArchSpecific() {
Liz Kammer9abd62d2021-05-21 08:37:59 -04002039 return axisToProps
Jingwen Chen5d864492021-02-24 07:20:12 -05002040 }
2041
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002042 dstType := reflect.ValueOf(propertySet).Type()
2043 var archProperties []interface{}
2044
2045 // First find the property set in the module that corresponds to the requested
Usta851a3272022-01-05 23:42:33 -05002046 // one. m.archProperties[i] corresponds to m.GetProperties()[i].
2047 for i, generalProp := range m.GetProperties() {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002048 srcType := reflect.ValueOf(generalProp).Type()
2049 if srcType == dstType {
2050 archProperties = m.archProperties[i]
Liz Kammer135bf552021-08-11 10:46:06 -04002051 axisToProps[bazel.NoConfigAxis] = ArchVariantProperties{"": generalProp}
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002052 break
2053 }
2054 }
2055
2056 if archProperties == nil {
2057 // This module does not have the property set requested
Liz Kammer9abd62d2021-05-21 08:37:59 -04002058 return axisToProps
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002059 }
2060
Liz Kammer9abd62d2021-05-21 08:37:59 -04002061 archToProp := ArchVariantProperties{}
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002062 // For each arch type (x86, arm64, etc.)
Chris Parsonsc424b762021-04-29 18:06:50 -04002063 for _, arch := range ArchTypeList() {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002064 // Arch properties are sometimes sharded (see createArchPropTypeDesc() ).
2065 // Iterate over ever shard and extract a struct with the same type as the
2066 // input one that contains the data specific to that arch.
2067 propertyStructs := make([]reflect.Value, 0)
2068 for _, archProperty := range archProperties {
Lukacs T. Berki5f518392021-05-17 11:44:58 +02002069 archTypeStruct, ok := getArchTypeStruct(ctx, archProperty, arch)
2070 if ok {
2071 propertyStructs = append(propertyStructs, archTypeStruct)
2072 }
2073 multilibStruct, ok := getMultilibStruct(ctx, archProperty, arch)
2074 if ok {
2075 propertyStructs = append(propertyStructs, multilibStruct)
2076 }
Jingwen Chen5d864492021-02-24 07:20:12 -05002077 }
2078
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002079 // Create a new instance of the requested property set
2080 value := reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
2081
Chris Parsonsa37e1952021-09-28 16:47:36 -04002082 archToProp[arch.Name] = mergeStructs(ctx, propertyStructs, value)
Jingwen Chen5d864492021-02-24 07:20:12 -05002083 }
Liz Kammer9abd62d2021-05-21 08:37:59 -04002084 axisToProps[bazel.ArchConfigurationAxis] = archToProp
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002085
Liz Kammer9abd62d2021-05-21 08:37:59 -04002086 osToProp := ArchVariantProperties{}
2087 archOsToProp := ArchVariantProperties{}
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002088
Liz Kammerfdd72e62021-10-11 15:41:03 -04002089 linuxStructs := getTargetStructs(ctx, archProperties, "Linux")
2090 bionicStructs := getTargetStructs(ctx, archProperties, "Bionic")
2091 hostStructs := getTargetStructs(ctx, archProperties, "Host")
Colin Crossa98d36d2022-03-07 14:39:49 -08002092 hostLinuxStructs := getTargetStructs(ctx, archProperties, "Host_linux")
Liz Kammerfdd72e62021-10-11 15:41:03 -04002093 hostNotWindowsStructs := getTargetStructs(ctx, archProperties, "Not_windows")
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002094
Liz Kammer9abd62d2021-05-21 08:37:59 -04002095 // For android, linux, ...
2096 for _, os := range osTypeList {
2097 if os == CommonOS {
2098 // It looks like this OS value is not used in Blueprint files
2099 continue
2100 }
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002101 osStructs := make([]reflect.Value, 0)
Liz Kammerfdd72e62021-10-11 15:41:03 -04002102
2103 osSpecificStructs := getTargetStructs(ctx, archProperties, os.Field)
2104 if os.Class == Host {
2105 osStructs = append(osStructs, hostStructs...)
Chris Parsonsa37e1952021-09-28 16:47:36 -04002106 }
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002107 if os.Linux() {
2108 osStructs = append(osStructs, linuxStructs...)
2109 }
2110 if os.Bionic() {
2111 osStructs = append(osStructs, bionicStructs...)
2112 }
Colin Crossa98d36d2022-03-07 14:39:49 -08002113 if os.Linux() && os.Class == Host {
2114 osStructs = append(osStructs, hostLinuxStructs...)
2115 }
Liz Kammerfdd72e62021-10-11 15:41:03 -04002116
2117 if os == LinuxMusl {
2118 osStructs = append(osStructs, getTargetStructs(ctx, archProperties, "Musl")...)
2119 }
2120 if os == Linux {
2121 osStructs = append(osStructs, getTargetStructs(ctx, archProperties, "Glibc")...)
2122 }
2123
2124 osStructs = append(osStructs, osSpecificStructs...)
2125
2126 if os.Class == Host && os != Windows {
2127 osStructs = append(osStructs, hostNotWindowsStructs...)
2128 }
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002129 osToProp[os.Name] = mergeStructs(ctx, osStructs, propertySet)
Chris Parsonsa37e1952021-09-28 16:47:36 -04002130
Liz Kammer9abd62d2021-05-21 08:37:59 -04002131 // For arm, x86, ...
2132 for _, arch := range osArchTypeMap[os] {
Chris Parsonsa37e1952021-09-28 16:47:36 -04002133 osArchStructs := make([]reflect.Value, 0)
2134
Chris Parsonsa37e1952021-09-28 16:47:36 -04002135 // Auto-combine with Linux_ and Bionic_ targets. This potentially results in
2136 // repetition and select() bloat, but use of Linux_* and Bionic_* targets is rare.
2137 // TODO(b/201423152): Look into cleanup.
2138 if os.Linux() {
2139 targetField := "Linux_" + arch.Name
Liz Kammerfdd72e62021-10-11 15:41:03 -04002140 targetStructs := getTargetStructs(ctx, archProperties, targetField)
2141 osArchStructs = append(osArchStructs, targetStructs...)
Chris Parsonsa37e1952021-09-28 16:47:36 -04002142 }
2143 if os.Bionic() {
2144 targetField := "Bionic_" + arch.Name
Liz Kammerfdd72e62021-10-11 15:41:03 -04002145 targetStructs := getTargetStructs(ctx, archProperties, targetField)
2146 osArchStructs = append(osArchStructs, targetStructs...)
Chris Parsonsa37e1952021-09-28 16:47:36 -04002147 }
Colin Cross2d295a22022-03-07 14:46:20 -08002148 if os == LinuxMusl {
2149 targetField := "Musl_" + arch.Name
2150 targetStructs := getTargetStructs(ctx, archProperties, targetField)
2151 osArchStructs = append(osArchStructs, targetStructs...)
2152 }
2153 if os == Linux {
2154 targetField := "Glibc_" + arch.Name
2155 targetStructs := getTargetStructs(ctx, archProperties, targetField)
2156 osArchStructs = append(osArchStructs, targetStructs...)
2157 }
Chris Parsonsa37e1952021-09-28 16:47:36 -04002158
Liz Kammerfdd72e62021-10-11 15:41:03 -04002159 targetField := GetCompoundTargetField(os, arch)
2160 targetName := fmt.Sprintf("%s_%s", os.Name, arch.Name)
2161 targetStructs := getTargetStructs(ctx, archProperties, targetField)
2162 osArchStructs = append(osArchStructs, targetStructs...)
2163
Chris Parsonsa37e1952021-09-28 16:47:36 -04002164 archOsToProp[targetName] = mergeStructs(ctx, osArchStructs, propertySet)
Liz Kammer9abd62d2021-05-21 08:37:59 -04002165 }
2166 }
Chris Parsons2dde0cb2021-10-01 14:45:30 -04002167
Liz Kammer9abd62d2021-05-21 08:37:59 -04002168 axisToProps[bazel.OsConfigurationAxis] = osToProp
2169 axisToProps[bazel.OsArchConfigurationAxis] = archOsToProp
Liz Kammer9abd62d2021-05-21 08:37:59 -04002170 return axisToProps
Jingwen Chen5d864492021-02-24 07:20:12 -05002171}
Jingwen Chen91220d72021-03-24 02:18:33 -04002172
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04002173// Returns a struct matching the propertySet interface, containing properties specific to the targetName
2174// For example, given these arguments:
Colin Crossd079e0b2022-08-16 10:27:33 -07002175//
2176// propertySet = BaseCompilerProperties
2177// targetName = "android_arm"
2178//
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04002179// And given this Android.bp fragment:
Colin Crossd079e0b2022-08-16 10:27:33 -07002180//
2181// target:
2182// android_arm: {
2183// srcs: ["foo.c"],
2184// }
2185// android_arm64: {
2186// srcs: ["bar.c"],
2187// }
2188// }
2189//
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04002190// This would return a BaseCompilerProperties with BaseCompilerProperties.Srcs = ["foo.c"]
Liz Kammerfdd72e62021-10-11 15:41:03 -04002191func getTargetStructs(ctx ArchVariantContext, archProperties []interface{}, targetName string) []reflect.Value {
2192 var propertyStructs []reflect.Value
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04002193 for _, archProperty := range archProperties {
2194 archPropValues := reflect.ValueOf(archProperty).Elem()
2195 targetProp := archPropValues.FieldByName("Target").Elem()
2196 targetStruct, ok := getChildPropertyStruct(ctx, targetProp, targetName, targetName)
2197 if ok {
2198 propertyStructs = append(propertyStructs, targetStruct)
Chris Parsonsa37e1952021-09-28 16:47:36 -04002199 } else {
Liz Kammerfdd72e62021-10-11 15:41:03 -04002200 return []reflect.Value{}
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04002201 }
2202 }
2203
Liz Kammerfdd72e62021-10-11 15:41:03 -04002204 return propertyStructs
Chris Parsonsa37e1952021-09-28 16:47:36 -04002205}
2206
2207func mergeStructs(ctx ArchVariantContext, propertyStructs []reflect.Value, propertySet interface{}) interface{} {
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04002208 // Create a new instance of the requested property set
2209 value := reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
2210
2211 // Merge all the structs together
2212 for _, propertyStruct := range propertyStructs {
2213 mergePropertyStruct(ctx, value, propertyStruct)
2214 }
2215
2216 return value
2217}
Liz Kammere8303bd2022-02-16 09:02:48 -05002218
2219func printArchTypeStarlarkDict(dict map[ArchType][]string) string {
2220 valDict := make(map[string]string, len(dict))
2221 for k, v := range dict {
2222 valDict[k.String()] = starlark_fmt.PrintStringList(v, 1)
2223 }
2224 return starlark_fmt.PrintDict(valDict, 0)
2225}
2226
2227func printArchTypeNestedStarlarkDict(dict map[ArchType]map[string][]string) string {
2228 valDict := make(map[string]string, len(dict))
2229 for k, v := range dict {
2230 valDict[k.String()] = starlark_fmt.PrintStringListDict(v, 1)
2231 }
2232 return starlark_fmt.PrintDict(valDict, 0)
2233}
2234
2235func StarlarkArchConfigurations() string {
2236 return fmt.Sprintf(`
2237_arch_to_variants = %s
2238
2239_arch_to_cpu_variants = %s
2240
2241_arch_to_features = %s
2242
2243_android_arch_feature_for_arch_variant = %s
2244
2245arch_to_variants = _arch_to_variants
2246arch_to_cpu_variants = _arch_to_cpu_variants
2247arch_to_features = _arch_to_features
2248android_arch_feature_for_arch_variants = _android_arch_feature_for_arch_variant
2249`, printArchTypeStarlarkDict(archVariants),
2250 printArchTypeStarlarkDict(cpuVariants),
2251 printArchTypeStarlarkDict(archFeatures),
2252 printArchTypeNestedStarlarkDict(androidArchFeatureMap),
2253 )
2254}