blob: ddc082b0096b3460d6a7da70bd5175d2a225886d [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"
25
Colin Cross0f7d2ef2019-10-16 11:03:10 -070026 "github.com/google/blueprint"
Colin Cross617b88a2020-08-24 18:04:09 -070027 "github.com/google/blueprint/bootstrap"
Colin Crossf6566ed2015-03-24 11:13:38 -070028 "github.com/google/blueprint/proptools"
Colin Cross3f40fa42015-01-30 17:27:36 -080029)
30
Colin Cross3f40fa42015-01-30 17:27:36 -080031/*
32Example blueprints file containing all variant property groups, with comment listing what type
33of variants get properties in that group:
34
35module {
36 arch: {
37 arm: {
38 // Host or device variants with arm architecture
39 },
40 arm64: {
41 // Host or device variants with arm64 architecture
42 },
Colin Cross3f40fa42015-01-30 17:27:36 -080043 x86: {
44 // Host or device variants with x86 architecture
45 },
46 x86_64: {
47 // Host or device variants with x86_64 architecture
48 },
49 },
50 multilib: {
51 lib32: {
52 // Host or device variants for 32-bit architectures
53 },
54 lib64: {
55 // Host or device variants for 64-bit architectures
56 },
57 },
58 target: {
59 android: {
Martin Stjernholme284b482020-09-23 21:03:27 +010060 // Device variants (implies Bionic)
Colin Cross3f40fa42015-01-30 17:27:36 -080061 },
62 host: {
63 // Host variants
64 },
Martin Stjernholme284b482020-09-23 21:03:27 +010065 bionic: {
66 // Bionic (device and host) variants
67 },
68 linux_bionic: {
69 // Bionic host variants
70 },
71 linux: {
72 // Bionic (device and host) and Linux glibc variants
73 },
Dan Willemsen5746bd42017-10-02 19:42:01 -070074 linux_glibc: {
Martin Stjernholme284b482020-09-23 21:03:27 +010075 // Linux host variants (using non-Bionic libc)
Colin Cross3f40fa42015-01-30 17:27:36 -080076 },
77 darwin: {
78 // Darwin host variants
79 },
80 windows: {
81 // Windows host variants
82 },
83 not_windows: {
84 // Non-windows host variants
85 },
Martin Stjernholme284b482020-09-23 21:03:27 +010086 android_arm: {
87 // Any <os>_<arch> combination restricts to that os and arch
88 },
Colin Cross3f40fa42015-01-30 17:27:36 -080089 },
90}
91*/
Colin Cross7d5136f2015-05-11 13:39:40 -070092
Colin Cross3f40fa42015-01-30 17:27:36 -080093// An Arch indicates a single CPU architecture.
94type Arch struct {
Colin Crossa6845402020-11-16 15:08:19 -080095 // The type of the architecture (arm, arm64, x86, or x86_64).
96 ArchType ArchType
97
98 // The variant of the architecture, for example "armv7-a" or "armv7-a-neon" for arm.
99 ArchVariant string
100
101 // The variant of the CPU, for example "cortex-a53" for arm64.
102 CpuVariant string
103
104 // The list of Android app ABIs supported by the CPU architecture, for example "arm64-v8a".
105 Abi []string
106
107 // The list of arch-specific features supported by the CPU architecture, for example "neon".
Colin Crossc5c24ad2015-11-20 15:35:00 -0800108 ArchFeatures []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800109}
110
Colin Crossa6845402020-11-16 15:08:19 -0800111// String returns the Arch as a string. The value is used as the name of the variant created
112// by archMutator.
Colin Cross3f40fa42015-01-30 17:27:36 -0800113func (a Arch) String() string {
Colin Crossd3ba0392015-05-07 14:11:29 -0700114 s := a.ArchType.String()
Colin Cross3f40fa42015-01-30 17:27:36 -0800115 if a.ArchVariant != "" {
116 s += "_" + a.ArchVariant
117 }
118 if a.CpuVariant != "" {
119 s += "_" + a.CpuVariant
120 }
121 return s
122}
123
Colin Crossa6845402020-11-16 15:08:19 -0800124// ArchType is used to define the 4 supported architecture types (arm, arm64, x86, x86_64), as
125// well as the "common" architecture used for modules that support multiple architectures, for
126// example Java modules.
Colin Cross3f40fa42015-01-30 17:27:36 -0800127type ArchType struct {
Colin Crossa6845402020-11-16 15:08:19 -0800128 // Name is the name of the architecture type, "arm", "arm64", "x86", or "x86_64".
129 Name string
130
131 // Field is the name of the field used in properties that refer to the architecture, e.g. "Arm64".
132 Field string
133
134 // Multilib is either "lib32" or "lib64" for 32-bit or 64-bit architectures.
Colin Crossec193632015-07-06 17:49:43 -0700135 Multilib string
Colin Cross3f40fa42015-01-30 17:27:36 -0800136}
137
Colin Crossa6845402020-11-16 15:08:19 -0800138// String returns the name of the ArchType.
139func (a ArchType) String() string {
140 return a.Name
141}
142
143const COMMON_VARIANT = "common"
144
145var (
146 archTypeList []ArchType
147
148 Arm = newArch("arm", "lib32")
149 Arm64 = newArch("arm64", "lib64")
150 X86 = newArch("x86", "lib32")
151 X86_64 = newArch("x86_64", "lib64")
152
153 Common = ArchType{
154 Name: COMMON_VARIANT,
155 }
156)
157
158var archTypeMap = map[string]ArchType{}
159
Colin Crossec193632015-07-06 17:49:43 -0700160func newArch(name, multilib string) ArchType {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700161 archType := ArchType{
Colin Crossec193632015-07-06 17:49:43 -0700162 Name: name,
Dan Willemsenb1957a52016-06-23 23:44:54 -0700163 Field: proptools.FieldNameForProperty(name),
Colin Crossec193632015-07-06 17:49:43 -0700164 Multilib: multilib,
Colin Cross3f40fa42015-01-30 17:27:36 -0800165 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700166 archTypeList = append(archTypeList, archType)
Colin Crossa6845402020-11-16 15:08:19 -0800167 archTypeMap[name] = archType
Dan Willemsenb1957a52016-06-23 23:44:54 -0700168 return archType
Colin Cross3f40fa42015-01-30 17:27:36 -0800169}
170
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000171// ArchTypeList returns the a slice copy of the 4 supported ArchTypes for arm,
172// arm64, x86 and x86_64.
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -0700173func ArchTypeList() []ArchType {
174 return append([]ArchType(nil), archTypeList...)
175}
176
Colin Crossa6845402020-11-16 15:08:19 -0800177// MarshalText allows an ArchType to be serialized through any encoder that supports
178// encoding.TextMarshaler.
Colin Cross74ba9622019-02-11 15:11:14 -0800179func (a ArchType) MarshalText() ([]byte, error) {
Jeongik Chabec4d032021-04-15 08:55:38 +0900180 return []byte(a.String()), nil
Colin Cross74ba9622019-02-11 15:11:14 -0800181}
182
Colin Crossa6845402020-11-16 15:08:19 -0800183var _ encoding.TextMarshaler = ArchType{}
Colin Cross74ba9622019-02-11 15:11:14 -0800184
Colin Crossa6845402020-11-16 15:08:19 -0800185// UnmarshalText allows an ArchType to be deserialized through any decoder that supports
186// encoding.TextUnmarshaler.
Colin Cross74ba9622019-02-11 15:11:14 -0800187func (a *ArchType) UnmarshalText(text []byte) error {
188 if u, ok := archTypeMap[string(text)]; ok {
189 *a = u
190 return nil
191 }
192
193 return fmt.Errorf("unknown ArchType %q", text)
194}
195
Colin Crossa6845402020-11-16 15:08:19 -0800196var _ encoding.TextUnmarshaler = &ArchType{}
Colin Crossa1ad8d12016-06-01 17:09:44 -0700197
Colin Crossa6845402020-11-16 15:08:19 -0800198// OsClass is an enum that describes whether a variant of a module runs on the host, on the device,
199// or is generic.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700200type OsClass int
201
202const (
Colin Crossa6845402020-11-16 15:08:19 -0800203 // Generic is used for variants of modules that are not OS-specific.
Dan Willemsen0e2d97b2016-11-28 17:50:06 -0800204 Generic OsClass = iota
Colin Crossa6845402020-11-16 15:08:19 -0800205 // Device is used for variants of modules that run on the device.
Dan Willemsen0e2d97b2016-11-28 17:50:06 -0800206 Device
Colin Crossa6845402020-11-16 15:08:19 -0800207 // Host is used for variants of modules that run on the host.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700208 Host
Colin Crossa1ad8d12016-06-01 17:09:44 -0700209)
210
Colin Crossa6845402020-11-16 15:08:19 -0800211// String returns the OsClass as a string.
Colin Cross67a5c132017-05-09 13:45:28 -0700212func (class OsClass) String() string {
213 switch class {
214 case Generic:
215 return "generic"
216 case Device:
217 return "device"
218 case Host:
219 return "host"
Colin Cross67a5c132017-05-09 13:45:28 -0700220 default:
221 panic(fmt.Errorf("unknown class %d", class))
222 }
223}
224
Colin Crossa6845402020-11-16 15:08:19 -0800225// OsType describes an OS variant of a module.
226type OsType struct {
227 // Name is the name of the OS. It is also used as the name of the property in Android.bp
228 // files.
229 Name string
230
231 // Field is the name of the OS converted to an exported field name, i.e. with the first
232 // character capitalized.
233 Field string
234
235 // Class is the OsClass of the OS.
236 Class OsClass
237
238 // DefaultDisabled is set when the module variants for the OS should not be created unless
239 // the module explicitly requests them. This is used to limit Windows cross compilation to
240 // only modules that need it.
241 DefaultDisabled bool
242}
243
244// String returns the name of the OsType.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700245func (os OsType) String() string {
246 return os.Name
Colin Cross54c71122016-06-01 17:09:44 -0700247}
248
Colin Crossa6845402020-11-16 15:08:19 -0800249// Bionic returns true if the OS uses the Bionic libc runtime, i.e. if the OS is Android or
250// is Linux with Bionic.
Dan Willemsen866b5632017-09-22 12:28:24 -0700251func (os OsType) Bionic() bool {
252 return os == Android || os == LinuxBionic
253}
254
Colin Crossa6845402020-11-16 15:08:19 -0800255// Linux returns true if the OS uses the Linux kernel, i.e. if the OS is Android or is Linux
256// with or without the Bionic libc runtime.
Dan Willemsen866b5632017-09-22 12:28:24 -0700257func (os OsType) Linux() bool {
Colin Cross528d67e2021-07-23 22:23:07 +0000258 return os == Android || os == Linux || os == LinuxBionic || os == LinuxMusl
Dan Willemsen866b5632017-09-22 12:28:24 -0700259}
260
Colin Crossa6845402020-11-16 15:08:19 -0800261// newOsType constructs an OsType and adds it to the global lists.
262func newOsType(name string, class OsClass, defDisabled bool, archTypes ...ArchType) OsType {
263 checkCalledFromInit()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700264 os := OsType{
265 Name: name,
Colin Crossa6845402020-11-16 15:08:19 -0800266 Field: proptools.FieldNameForProperty(name),
Colin Crossa1ad8d12016-06-01 17:09:44 -0700267 Class: class,
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800268
269 DefaultDisabled: defDisabled,
Colin Cross54c71122016-06-01 17:09:44 -0700270 }
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000271 osTypeList = append(osTypeList, os)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800272
273 if _, found := commonTargetMap[name]; found {
274 panic(fmt.Errorf("Found Os type duplicate during OsType registration: %q", name))
275 } else {
Colin Crosse9fe2942020-11-10 18:12:15 -0800276 commonTargetMap[name] = Target{Os: os, Arch: CommonArch}
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800277 }
Colin Crossa6845402020-11-16 15:08:19 -0800278 osArchTypeMap[os] = archTypes
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800279
Colin Crossa1ad8d12016-06-01 17:09:44 -0700280 return os
281}
282
Colin Crossa6845402020-11-16 15:08:19 -0800283// osByName returns the OsType that has the given name, or NoOsType if none match.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700284func osByName(name string) OsType {
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000285 for _, os := range osTypeList {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700286 if os.Name == name {
287 return os
288 }
289 }
290
291 return NoOsType
Dan Willemsen490fd492015-11-24 17:53:15 -0800292}
293
Colin Crossa6845402020-11-16 15:08:19 -0800294var (
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000295 // osTypeList contains a list of all the supported OsTypes, including ones not supported
Colin Crossa6845402020-11-16 15:08:19 -0800296 // by the current build host or the target device.
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000297 osTypeList []OsType
Colin Crossa6845402020-11-16 15:08:19 -0800298 // commonTargetMap maps names of OsTypes to the corresponding common Target, i.e. the
299 // Target with the same OsType and the common ArchType.
300 commonTargetMap = make(map[string]Target)
301 // osArchTypeMap maps OsTypes to the list of supported ArchTypes for that OS.
302 osArchTypeMap = map[OsType][]ArchType{}
303
304 // NoOsType is a placeholder for when no OS is needed.
305 NoOsType OsType
306 // Linux is the OS for the Linux kernel plus the glibc runtime.
307 Linux = newOsType("linux_glibc", Host, false, X86, X86_64)
Colin Cross528d67e2021-07-23 22:23:07 +0000308 // LinuxMusl is the OS for the Linux kernel plus the musl runtime.
309 LinuxMusl = newOsType("linux_musl", Host, false, X86, X86_64)
Colin Crossa6845402020-11-16 15:08:19 -0800310 // Darwin is the OS for MacOS/Darwin host machines.
311 Darwin = newOsType("darwin", Host, false, X86_64)
312 // LinuxBionic is the OS for the Linux kernel plus the Bionic libc runtime, but without the
313 // rest of Android.
314 LinuxBionic = newOsType("linux_bionic", Host, false, Arm64, X86_64)
315 // Windows the OS for Windows host machines.
316 Windows = newOsType("windows", Host, true, X86, X86_64)
317 // Android is the OS for target devices that run all of Android, including the Linux kernel
318 // and the Bionic libc runtime.
319 Android = newOsType("android", Device, false, Arm, Arm64, X86, X86_64)
Colin Crossa6845402020-11-16 15:08:19 -0800320
321 // CommonOS is a pseudo OSType for a common OS variant, which is OsType agnostic and which
322 // has dependencies on all the OS variants.
323 CommonOS = newOsType("common_os", Generic, false)
Colin Crosse9fe2942020-11-10 18:12:15 -0800324
325 // CommonArch is the Arch for all modules that are os-specific but not arch specific,
326 // for example most Java modules.
327 CommonArch = Arch{ArchType: Common}
dimitry1f33e402019-03-26 12:39:31 +0100328)
329
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000330// OsTypeList returns a slice copy of the supported OsTypes.
331func OsTypeList() []OsType {
332 return append([]OsType(nil), osTypeList...)
333}
334
Colin Crossa6845402020-11-16 15:08:19 -0800335// Target specifies the OS and architecture that a module is being compiled for.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700336type Target struct {
Colin Crossa6845402020-11-16 15:08:19 -0800337 // Os the OS that the module is being compiled for (e.g. "linux_glibc", "android").
338 Os OsType
339 // Arch is the architecture that the module is being compiled for.
340 Arch Arch
341 // NativeBridge is NativeBridgeEnabled if the architecture is supported using NativeBridge
342 // (i.e. arm on x86) for this device.
343 NativeBridge NativeBridgeSupport
344 // NativeBridgeHostArchName is the name of the real architecture that is used to implement
345 // the NativeBridge architecture. For example, for arm on x86 this would be "x86".
dimitry8d6dde82019-07-11 10:23:53 +0200346 NativeBridgeHostArchName string
Colin Crossa6845402020-11-16 15:08:19 -0800347 // NativeBridgeRelativePath is the name of the subdirectory that will contain NativeBridge
348 // libraries and binaries.
dimitry8d6dde82019-07-11 10:23:53 +0200349 NativeBridgeRelativePath string
Jiyong Park1613e552020-09-14 19:43:17 +0900350
351 // HostCross is true when the target cannot run natively on the current build host.
352 // For example, linux_glibc_x86 returns true on a regular x86/i686/Linux machines, but returns false
353 // on Mac (different OS), or on 64-bit only i686/Linux machines (unsupported arch).
354 HostCross bool
Colin Crossd3ba0392015-05-07 14:11:29 -0700355}
356
Colin Crossa6845402020-11-16 15:08:19 -0800357// NativeBridgeSupport is an enum that specifies if a Target supports NativeBridge.
358type NativeBridgeSupport bool
359
360const (
361 NativeBridgeDisabled NativeBridgeSupport = false
362 NativeBridgeEnabled NativeBridgeSupport = true
363)
364
365// String returns the OS and arch variations used for the Target.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700366func (target Target) String() string {
Colin Crossa195f912019-10-16 11:07:20 -0700367 return target.OsVariation() + "_" + target.ArchVariation()
368}
369
Colin Crossa6845402020-11-16 15:08:19 -0800370// OsVariation returns the name of the variation used by the osMutator for the Target.
Colin Crossa195f912019-10-16 11:07:20 -0700371func (target Target) OsVariation() string {
372 return target.Os.String()
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700373}
374
Colin Crossa6845402020-11-16 15:08:19 -0800375// ArchVariation returns the name of the variation used by the archMutator for the Target.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700376func (target Target) ArchVariation() string {
377 var variation string
dimitry1f33e402019-03-26 12:39:31 +0100378 if target.NativeBridge {
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700379 variation = "native_bridge_"
dimitry1f33e402019-03-26 12:39:31 +0100380 }
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700381 variation += target.Arch.String()
382
Colin Crossa195f912019-10-16 11:07:20 -0700383 return variation
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700384}
385
Colin Crossa6845402020-11-16 15:08:19 -0800386// Variations returns a list of blueprint.Variations for the osMutator and archMutator for the
387// Target.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700388func (target Target) Variations() []blueprint.Variation {
389 return []blueprint.Variation{
Colin Crossa195f912019-10-16 11:07:20 -0700390 {Mutator: "os", Variation: target.OsVariation()},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700391 {Mutator: "arch", Variation: target.ArchVariation()},
392 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800393}
394
Liz Kammer4562a3b2021-04-21 18:15:34 -0400395func registerBp2buildArchPathDepsMutator(ctx RegisterMutatorsContext) {
396 ctx.BottomUp("bp2build-arch-pathdeps", bp2buildArchPathDepsMutator).Parallel()
397}
398
399// add dependencies for architecture specific properties tagged with `android:"path"`
400func bp2buildArchPathDepsMutator(ctx BottomUpMutatorContext) {
401 var module Module
402 module = ctx.Module()
403
404 m := module.base()
405 if !m.ArchSpecific() {
406 return
407 }
408
409 // addPathDepsForProps does not descend into sub structs, so we need to descend into the
410 // arch-specific properties ourselves
411 properties := []interface{}{}
412 for _, archProperties := range m.archProperties {
413 for _, archProps := range archProperties {
414 archPropValues := reflect.ValueOf(archProps).Elem()
415 // there are three "arch" variations, descend into each
416 for _, variant := range []string{"Arch", "Multilib", "Target"} {
417 // The properties are an interface, get the value (a pointer) that it points to
418 archProps := archPropValues.FieldByName(variant).Elem()
419 if archProps.IsNil() {
420 continue
421 }
422 // And then a pointer to a struct
423 archProps = archProps.Elem()
424 for i := 0; i < archProps.NumField(); i += 1 {
425 f := archProps.Field(i)
426 // If the value of the field is a struct (as opposed to a pointer to a struct) then step
427 // into the BlueprintEmbed field.
428 if f.Kind() == reflect.Struct {
429 f = f.FieldByName("BlueprintEmbed")
430 }
431 if f.IsZero() {
432 continue
433 }
434 props := f.Interface().(interface{})
435 properties = append(properties, props)
436 }
437 }
438 }
439 }
440 addPathDepsForProps(ctx, properties)
441}
442
Colin Crossa6845402020-11-16 15:08:19 -0800443// osMutator splits an arch-specific module into a variant for each OS that is enabled for the
444// module. It uses the HostOrDevice value passed to InitAndroidArchModule and the
445// device_supported and host_supported properties to determine which OsTypes are enabled for this
446// module, then searches through the Targets to determine which have enabled Targets for this
447// module.
Colin Cross617b88a2020-08-24 18:04:09 -0700448func osMutator(bpctx blueprint.BottomUpMutatorContext) {
Colin Crossa195f912019-10-16 11:07:20 -0700449 var module Module
450 var ok bool
Colin Cross617b88a2020-08-24 18:04:09 -0700451 if module, ok = bpctx.Module().(Module); !ok {
Colin Crossa6845402020-11-16 15:08:19 -0800452 // The module is not a Soong module, it is a Blueprint module.
Colin Cross617b88a2020-08-24 18:04:09 -0700453 if bootstrap.IsBootstrapModule(bpctx.Module()) {
454 // Bootstrap Go modules are always the build OS or linux bionic.
455 config := bpctx.Config().(Config)
456 osNames := []string{config.BuildOSTarget.OsVariation()}
457 for _, hostCrossTarget := range config.Targets[LinuxBionic] {
458 if hostCrossTarget.Arch.ArchType == config.BuildOSTarget.Arch.ArchType {
459 osNames = append(osNames, hostCrossTarget.OsVariation())
460 }
461 }
462 osNames = FirstUniqueStrings(osNames)
463 bpctx.CreateVariations(osNames...)
464 }
Colin Crossa195f912019-10-16 11:07:20 -0700465 return
466 }
467
Colin Cross617b88a2020-08-24 18:04:09 -0700468 // Bootstrap Go module support above requires this mutator to be a
469 // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext
470 // filters out non-Soong modules. Now that we've handled them, create a
471 // normal android.BottomUpMutatorContext.
Liz Kammer356f7d42021-01-26 09:18:53 -0500472 mctx := bottomUpMutatorContextFactory(bpctx, module, false, false)
Colin Cross617b88a2020-08-24 18:04:09 -0700473
Colin Crossa195f912019-10-16 11:07:20 -0700474 base := module.base()
475
Colin Crossa6845402020-11-16 15:08:19 -0800476 // Nothing to do for modules that are not architecture specific (e.g. a genrule).
Colin Crossa195f912019-10-16 11:07:20 -0700477 if !base.ArchSpecific() {
478 return
479 }
480
Colin Crossa6845402020-11-16 15:08:19 -0800481 // Collect a list of OSTypes supported by this module based on the HostOrDevice value
482 // passed to InitAndroidArchModule and the device_supported and host_supported properties.
Colin Crossa195f912019-10-16 11:07:20 -0700483 var moduleOSList []OsType
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000484 for _, os := range osTypeList {
Jiyong Park1613e552020-09-14 19:43:17 +0900485 for _, t := range mctx.Config().Targets[os] {
Colin Cross08d6f8f2020-11-19 02:33:19 +0000486 if base.supportsTarget(t) {
Jiyong Park1613e552020-09-14 19:43:17 +0900487 moduleOSList = append(moduleOSList, os)
488 break
Colin Crossa195f912019-10-16 11:07:20 -0700489 }
490 }
Colin Crossa195f912019-10-16 11:07:20 -0700491 }
492
Colin Crossa6845402020-11-16 15:08:19 -0800493 // If there are no supported OSes then disable the module.
Colin Crossa195f912019-10-16 11:07:20 -0700494 if len(moduleOSList) == 0 {
Inseob Kimeec88e12020-01-22 11:11:29 +0900495 base.Disable()
Colin Crossa195f912019-10-16 11:07:20 -0700496 return
497 }
498
Colin Crossa6845402020-11-16 15:08:19 -0800499 // Convert the list of supported OsTypes to the variation names.
Colin Crossa195f912019-10-16 11:07:20 -0700500 osNames := make([]string, len(moduleOSList))
Colin Crossa195f912019-10-16 11:07:20 -0700501 for i, os := range moduleOSList {
502 osNames[i] = os.String()
503 }
504
Paul Duffin1356d8c2020-02-25 19:26:33 +0000505 createCommonOSVariant := base.commonProperties.CreateCommonOSVariant
506 if createCommonOSVariant {
Colin Crossa6845402020-11-16 15:08:19 -0800507 // A CommonOS variant was requested so add it to the list of OS variants to
Paul Duffin1356d8c2020-02-25 19:26:33 +0000508 // create. It needs to be added to the end because it needs to depend on the
509 // the other variants in the list returned by CreateVariations(...) and inter
510 // variant dependencies can only be created from a later variant in that list to
511 // an earlier one. That is because variants are always processed in the order in
512 // which they are returned from CreateVariations(...).
513 osNames = append(osNames, CommonOS.Name)
514 moduleOSList = append(moduleOSList, CommonOS)
Colin Crossa195f912019-10-16 11:07:20 -0700515 }
516
Colin Crossa6845402020-11-16 15:08:19 -0800517 // Create the variations, annotate each one with which OS it was created for, and
518 // squash the appropriate OS-specific properties into the top level properties.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000519 modules := mctx.CreateVariations(osNames...)
520 for i, m := range modules {
521 m.base().commonProperties.CompileOS = moduleOSList[i]
522 m.base().setOSProperties(mctx)
523 }
524
525 if createCommonOSVariant {
526 // A CommonOS variant was requested so add dependencies from it (the last one in
527 // the list) to the OS type specific variants.
528 last := len(modules) - 1
529 commonOSVariant := modules[last]
530 commonOSVariant.base().commonProperties.CommonOSVariant = true
531 for _, module := range modules[0:last] {
532 // Ignore modules that are enabled. Note, this will only avoid adding
533 // dependencies on OsType variants that are explicitly disabled in their
534 // properties. The CommonOS variant will still depend on disabled variants
535 // if they are disabled afterwards, e.g. in archMutator if
536 if module.Enabled() {
537 mctx.AddInterVariantDependency(commonOsToOsSpecificVariantTag, commonOSVariant, module)
538 }
539 }
540 }
541}
542
Colin Crossc179ea62020-10-09 10:54:15 -0700543type archDepTag struct {
544 blueprint.BaseDependencyTag
545 name string
546}
Paul Duffin1356d8c2020-02-25 19:26:33 +0000547
Colin Crossc179ea62020-10-09 10:54:15 -0700548// Identifies the dependency from CommonOS variant to the os specific variants.
549var commonOsToOsSpecificVariantTag = archDepTag{name: "common os to os specific"}
550
Paul Duffin1356d8c2020-02-25 19:26:33 +0000551// Get the OsType specific variants for the current CommonOS variant.
552//
553// The returned list will only contain enabled OsType specific variants of the
554// module referenced in the supplied context. An empty list is returned if there
555// are no enabled variants or the supplied context is not for an CommonOS
556// variant.
557func GetOsSpecificVariantsOfCommonOSVariant(mctx BaseModuleContext) []Module {
558 var variants []Module
559 mctx.VisitDirectDeps(func(m Module) {
560 if mctx.OtherModuleDependencyTag(m) == commonOsToOsSpecificVariantTag {
561 if m.Enabled() {
562 variants = append(variants, m)
563 }
564 }
565 })
Paul Duffin1356d8c2020-02-25 19:26:33 +0000566 return variants
Colin Crossa195f912019-10-16 11:07:20 -0700567}
568
Colin Crossee0bc3b2018-10-02 22:01:37 -0700569// archMutator splits a module into a variant for each Target requested by the module. Target selection
Colin Crossa6845402020-11-16 15:08:19 -0800570// for a module is in three levels, OsClass, multilib, and then Target.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700571// OsClass selection is determined by:
572// - The HostOrDeviceSupported value passed in to InitAndroidArchModule by the module type factory, which selects
573// whether the module type can compile for host, device or both.
574// - The host_supported and device_supported properties on the module.
Roland Levillainf5b635d2019-06-05 14:42:57 +0100575// 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 -0700576// for the module, the Device OsClass is selected.
577// Within each selected OsClass, the multilib selection is determined by:
Jaewoong Jung02b2d4d2019-06-06 15:19:57 -0700578// - The compile_multilib property if it set (which may be overridden by target.android.compile_multilib or
Colin Crossee0bc3b2018-10-02 22:01:37 -0700579// target.host.compile_multilib).
580// - The default multilib passed to InitAndroidArchModule if compile_multilib was not set.
581// Valid multilib values include:
582// "both": compile for all Targets supported by the OsClass (generally x86_64 and x86, or arm64 and arm).
583// "first": compile for only a single preferred Target supported by the OsClass. This is generally x86_64 or arm64,
Elliott Hughes79ae3412020-04-17 15:49:49 -0700584// but may be arm for a 32-bit only build.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700585// "32": compile for only a single 32-bit Target supported by the OsClass.
586// "64": compile for only a single 64-bit Target supported by the OsClass.
Colin Crossa6845402020-11-16 15:08:19 -0800587// "common": compile a for a single Target that will work on all Targets supported by the OsClass (for example Java).
588// "common_first": compile a for a Target that will work on all Targets supported by the OsClass
589// (same as "common"), plus a second Target for the preferred Target supported by the OsClass
590// (same as "first"). This is used for java_binary that produces a common .jar and a wrapper
591// executable script.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700592//
593// Once the list of Targets is determined, the module is split into a variant for each Target.
594//
595// Modules can be initialized with InitAndroidMultiTargetsArchModule, in which case they will be split by OsClass,
596// 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 -0700597func archMutator(bpctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700598 var module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800599 var ok bool
Colin Cross617b88a2020-08-24 18:04:09 -0700600 if module, ok = bpctx.Module().(Module); !ok {
601 if bootstrap.IsBootstrapModule(bpctx.Module()) {
602 // Bootstrap Go modules are always the build architecture.
603 bpctx.CreateVariations(bpctx.Config().(Config).BuildOSTarget.ArchVariation())
604 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800605 return
606 }
607
Colin Cross617b88a2020-08-24 18:04:09 -0700608 // Bootstrap Go module support above requires this mutator to be a
609 // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext
610 // filters out non-Soong modules. Now that we've handled them, create a
611 // normal android.BottomUpMutatorContext.
Liz Kammer356f7d42021-01-26 09:18:53 -0500612 mctx := bottomUpMutatorContextFactory(bpctx, module, false, false)
Colin Cross617b88a2020-08-24 18:04:09 -0700613
Colin Cross5eca7cb2018-10-02 14:02:10 -0700614 base := module.base()
615
616 if !base.ArchSpecific() {
Colin Crossb9db4802016-06-03 01:50:47 +0000617 return
618 }
619
Colin Crossa195f912019-10-16 11:07:20 -0700620 os := base.commonProperties.CompileOS
Paul Duffin1356d8c2020-02-25 19:26:33 +0000621 if os == CommonOS {
622 // Make sure that the target related properties are initialized for the
623 // CommonOS variant.
624 addTargetProperties(module, commonTargetMap[os.Name], nil, true)
625
626 // Do not create arch specific variants for the CommonOS variant.
627 return
628 }
629
Colin Crossa195f912019-10-16 11:07:20 -0700630 osTargets := mctx.Config().Targets[os]
Colin Crossfb0c16e2019-11-20 17:12:35 -0800631 image := base.commonProperties.ImageVariation
Colin Crossa6845402020-11-16 15:08:19 -0800632 // Filter NativeBridge targets unless they are explicitly supported.
633 // Skip creating native bridge variants for non-core modules.
Paul Duffine3d1ae42021-09-03 17:47:17 +0100634 if os == Android && !(base.IsNativeBridgeSupported() && image == CoreVariation) {
Colin Cross83bead42019-12-18 10:45:46 -0800635
Colin Crossa195f912019-10-16 11:07:20 -0700636 var targets []Target
637 for _, t := range osTargets {
638 if !t.NativeBridge {
639 targets = append(targets, t)
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700640 }
641 }
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700642
Colin Crossa195f912019-10-16 11:07:20 -0700643 osTargets = targets
644 }
Colin Crossee0bc3b2018-10-02 22:01:37 -0700645
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700646 // only the primary arch in the ramdisk / vendor_ramdisk / recovery partition
Inseob Kim08758f02021-04-08 21:13:22 +0900647 if os == Android && (module.InstallInRecovery() || module.InstallInRamdisk() || module.InstallInVendorRamdisk() || module.InstallInDebugRamdisk()) {
Colin Crossa195f912019-10-16 11:07:20 -0700648 osTargets = []Target{osTargets[0]}
649 }
dimitry1f33e402019-03-26 12:39:31 +0100650
Jaewoong Jung003d8082021-02-24 17:39:54 -0800651 // Windows builds always prefer 32-bit
652 prefer32 := os == Windows
dimitry1f33e402019-03-26 12:39:31 +0100653
Colin Crossa6845402020-11-16 15:08:19 -0800654 // Determine the multilib selection for this module.
Colin Crossa195f912019-10-16 11:07:20 -0700655 multilib, extraMultilib := decodeMultilib(base, os.Class)
Colin Crossa6845402020-11-16 15:08:19 -0800656
657 // Convert the multilib selection into a list of Targets.
Colin Crossa195f912019-10-16 11:07:20 -0700658 targets, err := decodeMultilibTargets(multilib, osTargets, prefer32)
659 if err != nil {
660 mctx.ModuleErrorf("%s", err.Error())
661 }
Colin Cross5eca7cb2018-10-02 14:02:10 -0700662
Colin Crossa6845402020-11-16 15:08:19 -0800663 // If the module is using extraMultilib, decode the extraMultilib selection into
664 // a separate list of Targets.
Colin Crossa195f912019-10-16 11:07:20 -0700665 var multiTargets []Target
666 if extraMultilib != "" {
667 multiTargets, err = decodeMultilibTargets(extraMultilib, osTargets, prefer32)
Colin Crossa1ad8d12016-06-01 17:09:44 -0700668 if err != nil {
669 mctx.ModuleErrorf("%s", err.Error())
670 }
Colin Crossb9db4802016-06-03 01:50:47 +0000671 }
672
Colin Crossa6845402020-11-16 15:08:19 -0800673 // Recovery is always the primary architecture, filter out any other architectures.
Inseob Kim20fb5d42021-02-02 20:07:58 +0900674 // Common arch is also allowed
Colin Crossfb0c16e2019-11-20 17:12:35 -0800675 if image == RecoveryVariation {
676 primaryArch := mctx.Config().DevicePrimaryArchType()
Inseob Kim20fb5d42021-02-02 20:07:58 +0900677 targets = filterToArch(targets, primaryArch, Common)
678 multiTargets = filterToArch(multiTargets, primaryArch, Common)
Colin Crossfb0c16e2019-11-20 17:12:35 -0800679 }
680
Colin Crossa6845402020-11-16 15:08:19 -0800681 // If there are no supported targets disable the module.
Colin Crossa195f912019-10-16 11:07:20 -0700682 if len(targets) == 0 {
Inseob Kimeec88e12020-01-22 11:11:29 +0900683 base.Disable()
Dan Willemsen3f32f032016-07-11 14:36:48 -0700684 return
685 }
686
Colin Crossa6845402020-11-16 15:08:19 -0800687 // Convert the targets into a list of arch variation names.
Colin Crossa195f912019-10-16 11:07:20 -0700688 targetNames := make([]string, len(targets))
Colin Crossa195f912019-10-16 11:07:20 -0700689 for i, target := range targets {
690 targetNames[i] = target.ArchVariation()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700691 }
692
Colin Crossa6845402020-11-16 15:08:19 -0800693 // Create the variations, annotate each one with which Target it was created for, and
694 // squash the appropriate arch-specific properties into the top level properties.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700695 modules := mctx.CreateVariations(targetNames...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800696 for i, m := range modules {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000697 addTargetProperties(m, targets[i], multiTargets, i == 0)
Colin Cross617b88a2020-08-24 18:04:09 -0700698 m.base().setArchProperties(mctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800699 }
700}
701
Colin Crossa6845402020-11-16 15:08:19 -0800702// addTargetProperties annotates a variant with the Target is is being compiled for, the list
703// of additional Targets it is supporting (if any), and whether it is the primary Target for
704// the module.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000705func addTargetProperties(m Module, target Target, multiTargets []Target, primaryTarget bool) {
706 m.base().commonProperties.CompileTarget = target
707 m.base().commonProperties.CompileMultiTargets = multiTargets
708 m.base().commonProperties.CompilePrimary = primaryTarget
709}
710
Colin Crossa6845402020-11-16 15:08:19 -0800711// decodeMultilib returns the appropriate compile_multilib property for the module, or the default
712// multilib from the factory's call to InitAndroidArchModule if none was set. For modules that
713// called InitAndroidMultiTargetsArchModule it always returns "common" for multilib, and returns
714// the actual multilib in extraMultilib.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700715func decodeMultilib(base *ModuleBase, class OsClass) (multilib, extraMultilib string) {
Colin Crossa6845402020-11-16 15:08:19 -0800716 // First check the "android.compile_multilib" or "host.compile_multilib" properties.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700717 switch class {
718 case Device:
719 multilib = String(base.commonProperties.Target.Android.Compile_multilib)
Jiyong Park1613e552020-09-14 19:43:17 +0900720 case Host:
Colin Crossee0bc3b2018-10-02 22:01:37 -0700721 multilib = String(base.commonProperties.Target.Host.Compile_multilib)
722 }
Colin Crossa6845402020-11-16 15:08:19 -0800723
724 // If those aren't set, try the "compile_multilib" property.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700725 if multilib == "" {
726 multilib = String(base.commonProperties.Compile_multilib)
727 }
Colin Crossa6845402020-11-16 15:08:19 -0800728
729 // If that wasn't set, use the default multilib set by the factory.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700730 if multilib == "" {
731 multilib = base.commonProperties.Default_multilib
732 }
733
734 if base.commonProperties.UseTargetVariants {
735 return multilib, ""
736 } else {
737 // For app modules a single arch variant will be created per OS class which is expected to handle all the
738 // selected arches. Return the common-type as multilib and any Android.bp provided multilib as extraMultilib
739 if multilib == base.commonProperties.Default_multilib {
740 multilib = "first"
741 }
742 return base.commonProperties.Default_multilib, multilib
743 }
744}
745
Colin Crossa6845402020-11-16 15:08:19 -0800746// filterToArch takes a list of Targets and an ArchType, and returns a modified list that contains
Inseob Kim20fb5d42021-02-02 20:07:58 +0900747// only Targets that have the specified ArchTypes.
748func filterToArch(targets []Target, archs ...ArchType) []Target {
Colin Crossfb0c16e2019-11-20 17:12:35 -0800749 for i := 0; i < len(targets); i++ {
Inseob Kim20fb5d42021-02-02 20:07:58 +0900750 found := false
751 for _, arch := range archs {
752 if targets[i].Arch.ArchType == arch {
753 found = true
754 break
755 }
756 }
757 if !found {
Colin Crossfb0c16e2019-11-20 17:12:35 -0800758 targets = append(targets[:i], targets[i+1:]...)
759 i--
760 }
761 }
762 return targets
763}
764
Colin Crossa6845402020-11-16 15:08:19 -0800765// archPropRoot is a struct type used as the top level of the arch-specific properties. It
766// contains the "arch", "multilib", and "target" property structs. It is used to split up the
767// property structs to limit how much is allocated when a single arch-specific property group is
768// used. The types are interface{} because they will hold instances of runtime-created types.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800769type archPropRoot struct {
770 Arch, Multilib, Target interface{}
771}
772
Colin Crossa6845402020-11-16 15:08:19 -0800773// archPropTypeDesc holds the runtime-created types for the property structs to instantiate to
774// create an archPropRoot property struct.
775type archPropTypeDesc struct {
776 arch, multilib, target reflect.Type
777}
778
Colin Crosscbbd13f2020-01-17 14:08:22 -0800779// createArchPropTypeDesc takes a reflect.Type that is either a struct or a pointer to a struct, and
780// returns lists of reflect.Types that contains the arch-variant properties inside structs for each
781// arch, multilib and target property.
Colin Crossa6845402020-11-16 15:08:19 -0800782//
783// This is a relatively expensive operation, so the results are cached in the global
784// archPropTypeMap. It is constructed entirely based on compile-time data, so there is no need
785// to isolate the results between multiple tests running in parallel.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800786func createArchPropTypeDesc(props reflect.Type) []archPropTypeDesc {
Colin Crossb1d8c992020-01-21 11:43:29 -0800787 // Each property struct shard will be nested many times under the runtime generated arch struct,
788 // which can hit the limit of 64kB for the name of runtime generated structs. They are nested
789 // 97 times now, which may grow in the future, plus there is some overhead for the containing
790 // type. This number may need to be reduced if too many are added, but reducing it too far
791 // could cause problems if a single deeply nested property no longer fits in the name.
792 const maxArchTypeNameSize = 500
793
Colin Crossa6845402020-11-16 15:08:19 -0800794 // Convert the type to a new set of types that contains only the arch-specific properties
795 // (those that are tagged with `android:"arch_specific"`), and sharded into multiple types
796 // to keep the runtime-generated names under the limit.
Colin Crossb1d8c992020-01-21 11:43:29 -0800797 propShards, _ := proptools.FilterPropertyStructSharded(props, maxArchTypeNameSize, filterArchStruct)
Colin Crossa6845402020-11-16 15:08:19 -0800798
799 // If the type has no arch-specific properties there is nothing to do.
Colin Crosscb988072019-01-24 14:58:11 -0800800 if len(propShards) == 0 {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700801 return nil
802 }
803
Colin Crosscbbd13f2020-01-17 14:08:22 -0800804 var ret []archPropTypeDesc
Colin Crossc17727d2018-10-24 12:42:09 -0700805 for _, props := range propShards {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700806
Colin Crossa6845402020-11-16 15:08:19 -0800807 // variantFields takes a list of variant property field names and returns a list the
808 // StructFields with the names and the type of the current shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700809 variantFields := func(names []string) []reflect.StructField {
810 ret := make([]reflect.StructField, len(names))
Dan Willemsenb1957a52016-06-23 23:44:54 -0700811
Colin Crossc17727d2018-10-24 12:42:09 -0700812 for i, name := range names {
813 ret[i].Name = name
814 ret[i].Type = props
Dan Willemsen866b5632017-09-22 12:28:24 -0700815 }
Colin Crossc17727d2018-10-24 12:42:09 -0700816
817 return ret
818 }
819
Colin Crossa6845402020-11-16 15:08:19 -0800820 // Create a type that contains the properties in this shard repeated for each
821 // architecture, architecture variant, and architecture feature.
Colin Crossc17727d2018-10-24 12:42:09 -0700822 archFields := make([]reflect.StructField, len(archTypeList))
823 for i, arch := range archTypeList {
Colin Crossa6845402020-11-16 15:08:19 -0800824 var variants []string
Colin Crossc17727d2018-10-24 12:42:09 -0700825
826 for _, archVariant := range archVariants[arch] {
827 archVariant := variantReplacer.Replace(archVariant)
828 variants = append(variants, proptools.FieldNameForProperty(archVariant))
829 }
830 for _, feature := range archFeatures[arch] {
831 feature := variantReplacer.Replace(feature)
832 variants = append(variants, proptools.FieldNameForProperty(feature))
833 }
834
Colin Crossa6845402020-11-16 15:08:19 -0800835 // Create the StructFields for each architecture variant architecture feature
836 // (e.g. "arch.arm.cortex-a53" or "arch.arm.neon").
Colin Crossc17727d2018-10-24 12:42:09 -0700837 fields := variantFields(variants)
838
Colin Crossa6845402020-11-16 15:08:19 -0800839 // Create the StructField for the architecture itself (e.g. "arch.arm"). The special
840 // "BlueprintEmbed" name is used by Blueprint to put the properties in the
841 // parent struct.
Colin Crossc17727d2018-10-24 12:42:09 -0700842 fields = append([]reflect.StructField{{
843 Name: "BlueprintEmbed",
844 Type: props,
845 Anonymous: true,
846 }}, fields...)
847
848 archFields[i] = reflect.StructField{
849 Name: arch.Field,
850 Type: reflect.StructOf(fields),
851 }
852 }
Colin Crossa6845402020-11-16 15:08:19 -0800853
854 // Create the type of the "arch" property struct for this shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700855 archType := reflect.StructOf(archFields)
856
Colin Crossa6845402020-11-16 15:08:19 -0800857 // Create the type for the "multilib" property struct for this shard, containing the
858 // "multilib.lib32" and "multilib.lib64" property structs.
Colin Crossc17727d2018-10-24 12:42:09 -0700859 multilibType := reflect.StructOf(variantFields([]string{"Lib32", "Lib64"}))
860
Colin Crossa6845402020-11-16 15:08:19 -0800861 // Start with a list of the special targets
Colin Crossc17727d2018-10-24 12:42:09 -0700862 targets := []string{
863 "Host",
864 "Android64",
865 "Android32",
866 "Bionic",
Colin Cross528d67e2021-07-23 22:23:07 +0000867 "Glibc",
868 "Musl",
Colin Crossc17727d2018-10-24 12:42:09 -0700869 "Linux",
870 "Not_windows",
871 "Arm_on_x86",
872 "Arm_on_x86_64",
Victor Khimenkoc26fcf42020-05-07 22:16:33 +0200873 "Native_bridge",
Colin Crossc17727d2018-10-24 12:42:09 -0700874 }
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000875 for _, os := range osTypeList {
Colin Crossa6845402020-11-16 15:08:19 -0800876 // Add all the OSes.
Colin Crossc17727d2018-10-24 12:42:09 -0700877 targets = append(targets, os.Field)
878
Colin Crossa6845402020-11-16 15:08:19 -0800879 // Add the OS/Arch combinations, e.g. "android_arm64".
Colin Crossc17727d2018-10-24 12:42:09 -0700880 for _, archType := range osArchTypeMap[os] {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400881 targets = append(targets, GetCompoundTargetField(os, archType))
Colin Crossc17727d2018-10-24 12:42:09 -0700882
Colin Crossa6845402020-11-16 15:08:19 -0800883 // Also add the special "linux_<arch>" and "bionic_<arch>" property structs.
Colin Crossc17727d2018-10-24 12:42:09 -0700884 if os.Linux() {
885 target := "Linux_" + archType.Name
886 if !InList(target, targets) {
887 targets = append(targets, target)
888 }
889 }
890 if os.Bionic() {
891 target := "Bionic_" + archType.Name
892 if !InList(target, targets) {
893 targets = append(targets, target)
894 }
Dan Willemsen866b5632017-09-22 12:28:24 -0700895 }
896 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700897 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700898
Colin Crossa6845402020-11-16 15:08:19 -0800899 // Create the type for the "target" property struct for this shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700900 targetType := reflect.StructOf(variantFields(targets))
Colin Crosscbbd13f2020-01-17 14:08:22 -0800901
Colin Crossa6845402020-11-16 15:08:19 -0800902 // Return a descriptor of the 3 runtime-created types.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800903 ret = append(ret, archPropTypeDesc{
904 arch: reflect.PtrTo(archType),
905 multilib: reflect.PtrTo(multilibType),
906 target: reflect.PtrTo(targetType),
907 })
Colin Crossc17727d2018-10-24 12:42:09 -0700908 }
909 return ret
Dan Willemsenb1957a52016-06-23 23:44:54 -0700910}
911
Colin Crossa6845402020-11-16 15:08:19 -0800912// variantReplacer converts architecture variant or architecture feature names into names that
913// are valid for an Android.bp file.
914var variantReplacer = strings.NewReplacer("-", "_", ".", "_")
915
916// filterArchStruct returns true if the given field is an architecture specific property.
Colin Cross74449102019-09-25 11:26:40 -0700917func filterArchStruct(field reflect.StructField, prefix string) (bool, reflect.StructField) {
918 if proptools.HasTag(field, "android", "arch_variant") {
919 // The arch_variant field isn't necessary past this point
920 // Instead of wasting space, just remove it. Go also has a
921 // 16-bit limit on structure name length. The name is constructed
922 // based on the Go source representation of the structure, so
923 // the tag names count towards that length.
Colin Crossb4fecbf2020-01-21 11:38:47 -0800924
925 androidTag := field.Tag.Get("android")
926 values := strings.Split(androidTag, ",")
927
928 if string(field.Tag) != `android:"`+strings.Join(values, ",")+`"` {
929 panic(fmt.Errorf("unexpected tag format %q", field.Tag))
Colin Cross74449102019-09-25 11:26:40 -0700930 }
Liz Kammer4562a3b2021-04-21 18:15:34 -0400931 // don't delete path tag as it is needed for bp2build
Colin Crossb4fecbf2020-01-21 11:38:47 -0800932 // these tags don't need to be present in the runtime generated struct type.
Liz Kammer4562a3b2021-04-21 18:15:34 -0400933 values = RemoveListFromList(values, []string{"arch_variant", "variant_prepend"})
934 if len(values) > 0 && values[0] != "path" {
Colin Crossb4fecbf2020-01-21 11:38:47 -0800935 panic(fmt.Errorf("unknown tags %q in field %q", values, prefix+field.Name))
Liz Kammer4562a3b2021-04-21 18:15:34 -0400936 } else if len(values) == 1 {
937 field.Tag = reflect.StructTag(`android:"` + strings.Join(values, ",") + `"`)
938 } else {
939 field.Tag = ``
Colin Crossb4fecbf2020-01-21 11:38:47 -0800940 }
941
Colin Cross74449102019-09-25 11:26:40 -0700942 return true, field
943 }
944 return false, field
945}
946
Colin Crossa6845402020-11-16 15:08:19 -0800947// archPropTypeMap contains a cache of the results of createArchPropTypeDesc for each type. It is
948// shared across all Contexts, but is constructed based only on compile-time information so there
949// is no risk of contaminating one Context with data from another.
Dan Willemsenb1957a52016-06-23 23:44:54 -0700950var archPropTypeMap OncePer
951
Colin Crossa6845402020-11-16 15:08:19 -0800952// initArchModule adds the architecture-specific property structs to a Module.
953func initArchModule(m Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800954
955 base := m.base()
956
Colin Crossa6845402020-11-16 15:08:19 -0800957 // Store the original list of top level property structs
Colin Cross36242852017-06-23 15:06:31 -0700958 base.generalProperties = m.GetProperties()
Colin Cross3f40fa42015-01-30 17:27:36 -0800959
960 for _, properties := range base.generalProperties {
961 propertiesValue := reflect.ValueOf(properties)
Colin Cross62496a02016-08-08 15:49:17 -0700962 t := propertiesValue.Type()
Colin Cross3f40fa42015-01-30 17:27:36 -0800963 if propertiesValue.Kind() != reflect.Ptr {
Colin Crossca860ac2016-01-04 14:34:37 -0800964 panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
965 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800966 }
967
968 propertiesValue = propertiesValue.Elem()
969 if propertiesValue.Kind() != reflect.Struct {
Colin Crossca860ac2016-01-04 14:34:37 -0800970 panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
971 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800972 }
973
Colin Crossa6845402020-11-16 15:08:19 -0800974 // Get or create the arch-specific property struct types for this property struct type.
Colin Cross571cccf2019-02-04 11:22:08 -0800975 archPropTypes := archPropTypeMap.Once(NewCustomOnceKey(t), func() interface{} {
Colin Crosscbbd13f2020-01-17 14:08:22 -0800976 return createArchPropTypeDesc(t)
977 }).([]archPropTypeDesc)
Colin Cross3f40fa42015-01-30 17:27:36 -0800978
Colin Crossa6845402020-11-16 15:08:19 -0800979 // Instantiate one of each arch-specific property struct type and add it to the
980 // properties for the Module.
Colin Crossc17727d2018-10-24 12:42:09 -0700981 var archProperties []interface{}
982 for _, t := range archPropTypes {
Colin Crosscbbd13f2020-01-17 14:08:22 -0800983 archProperties = append(archProperties, &archPropRoot{
984 Arch: reflect.Zero(t.arch).Interface(),
985 Multilib: reflect.Zero(t.multilib).Interface(),
986 Target: reflect.Zero(t.target).Interface(),
987 })
Dan Willemsenb1957a52016-06-23 23:44:54 -0700988 }
Colin Crossc17727d2018-10-24 12:42:09 -0700989 base.archProperties = append(base.archProperties, archProperties)
990 m.AddProperties(archProperties...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800991 }
992
Colin Crossa6845402020-11-16 15:08:19 -0800993 // Update the list of properties that can be set by a defaults module or a call to
994 // AppendMatchingProperties or PrependMatchingProperties.
Colin Cross36242852017-06-23 15:06:31 -0700995 base.customizableProperties = m.GetProperties()
Colin Cross3f40fa42015-01-30 17:27:36 -0800996}
997
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200998func maybeBlueprintEmbed(src reflect.Value) reflect.Value {
Colin Crossa6845402020-11-16 15:08:19 -0800999 // If the value of the field is a struct (as opposed to a pointer to a struct) then step
1000 // into the BlueprintEmbed field.
Dan Willemsenb1957a52016-06-23 23:44:54 -07001001 if src.Kind() == reflect.Struct {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001002 return src.FieldByName("BlueprintEmbed")
1003 } else {
1004 return src
Colin Cross06a931b2015-10-28 17:23:31 -07001005 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001006}
1007
1008// Merges the property struct in srcValue into dst.
Liz Kammerb6dbc872021-05-14 15:14:40 -04001009func mergePropertyStruct(ctx ArchVariantContext, dst interface{}, srcValue reflect.Value) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001010 src := maybeBlueprintEmbed(srcValue).Interface()
Colin Cross06a931b2015-10-28 17:23:31 -07001011
Colin Crossa6845402020-11-16 15:08:19 -08001012 // order checks the `android:"variant_prepend"` tag to handle properties where the
1013 // arch-specific value needs to come before the generic value, for example for lists of
1014 // include directories.
Colin Cross6ee75b62016-05-05 15:57:15 -07001015 order := func(property string,
1016 dstField, srcField reflect.StructField,
1017 dstValue, srcValue interface{}) (proptools.Order, error) {
1018 if proptools.HasTag(dstField, "android", "variant_prepend") {
1019 return proptools.Prepend, nil
1020 } else {
1021 return proptools.Append, nil
1022 }
1023 }
1024
Colin Crossa6845402020-11-16 15:08:19 -08001025 // Squash the located property struct into the destination property struct.
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001026 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src, nil, order)
Colin Cross06a931b2015-10-28 17:23:31 -07001027 if err != nil {
1028 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
1029 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
1030 } else {
1031 panic(err)
1032 }
1033 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001034}
Colin Cross85a88972015-11-23 13:29:51 -08001035
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001036// Returns the immediate child of the input property struct that corresponds to
1037// the sub-property "field".
Liz Kammerb6dbc872021-05-14 15:14:40 -04001038func getChildPropertyStruct(ctx ArchVariantContext,
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001039 src reflect.Value, field, userFriendlyField string) (reflect.Value, bool) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001040
1041 // Step into non-nil pointers to structs in the src value.
1042 if src.Kind() == reflect.Ptr {
1043 if src.IsNil() {
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001044 return reflect.Value{}, false
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001045 }
1046 src = src.Elem()
1047 }
1048
1049 // Find the requested field in the src struct.
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001050 child := src.FieldByName(proptools.FieldNameForProperty(field))
1051 if !child.IsValid() {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001052 ctx.ModuleErrorf("field %q does not exist", userFriendlyField)
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001053 return reflect.Value{}, false
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001054 }
1055
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001056 if child.IsZero() {
1057 return reflect.Value{}, false
1058 }
1059
1060 return child, true
Colin Cross06a931b2015-10-28 17:23:31 -07001061}
1062
Colin Crossa6845402020-11-16 15:08:19 -08001063// Squash the appropriate OS-specific property structs into the matching top level property structs
1064// based on the CompileOS value that was annotated on the variant.
Colin Crossa195f912019-10-16 11:07:20 -07001065func (m *ModuleBase) setOSProperties(ctx BottomUpMutatorContext) {
1066 os := m.commonProperties.CompileOS
1067
1068 for i := range m.generalProperties {
1069 genProps := m.generalProperties[i]
1070 if m.archProperties[i] == nil {
1071 continue
1072 }
1073 for _, archProperties := range m.archProperties[i] {
1074 archPropValues := reflect.ValueOf(archProperties).Elem()
1075
Colin Crosscbbd13f2020-01-17 14:08:22 -08001076 targetProp := archPropValues.FieldByName("Target").Elem()
Colin Crossa195f912019-10-16 11:07:20 -07001077
1078 // Handle host-specific properties in the form:
1079 // target: {
1080 // host: {
1081 // key: value,
1082 // },
1083 // },
Jiyong Park1613e552020-09-14 19:43:17 +09001084 if os.Class == Host {
Colin Crossa195f912019-10-16 11:07:20 -07001085 field := "Host"
1086 prefix := "target.host"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001087 if hostProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1088 mergePropertyStruct(ctx, genProps, hostProperties)
1089 }
Colin Crossa195f912019-10-16 11:07:20 -07001090 }
1091
1092 // Handle target OS generalities of the form:
1093 // target: {
1094 // bionic: {
1095 // key: value,
1096 // },
1097 // }
1098 if os.Linux() {
1099 field := "Linux"
1100 prefix := "target.linux"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001101 if linuxProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1102 mergePropertyStruct(ctx, genProps, linuxProperties)
1103 }
Colin Crossa195f912019-10-16 11:07:20 -07001104 }
1105
1106 if os.Bionic() {
1107 field := "Bionic"
1108 prefix := "target.bionic"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001109 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1110 mergePropertyStruct(ctx, genProps, bionicProperties)
1111 }
Colin Crossa195f912019-10-16 11:07:20 -07001112 }
1113
Colin Cross528d67e2021-07-23 22:23:07 +00001114 if os == Linux {
1115 field := "Glibc"
1116 prefix := "target.glibc"
1117 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1118 mergePropertyStruct(ctx, genProps, bionicProperties)
1119 }
1120 }
1121
1122 if os == LinuxMusl {
1123 field := "Musl"
1124 prefix := "target.musl"
1125 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1126 mergePropertyStruct(ctx, genProps, bionicProperties)
1127 }
1128
1129 // Special case: to ease the transition from glibc to musl, apply linux_glibc
1130 // properties (which has historically mean host linux) to musl variants.
1131 field = "Linux_glibc"
1132 prefix = "target.linux_glibc"
1133 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1134 mergePropertyStruct(ctx, genProps, bionicProperties)
1135 }
1136 }
1137
Colin Crossa195f912019-10-16 11:07:20 -07001138 // Handle target OS properties in the form:
1139 // target: {
1140 // linux_glibc: {
1141 // key: value,
1142 // },
1143 // not_windows: {
1144 // key: value,
1145 // },
1146 // android {
1147 // key: value,
1148 // },
1149 // },
1150 field := os.Field
1151 prefix := "target." + os.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001152 if osProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1153 mergePropertyStruct(ctx, genProps, osProperties)
1154 }
Colin Crossa195f912019-10-16 11:07:20 -07001155
Jiyong Park1613e552020-09-14 19:43:17 +09001156 if os.Class == Host && os != Windows {
Colin Crossa195f912019-10-16 11:07:20 -07001157 field := "Not_windows"
1158 prefix := "target.not_windows"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001159 if notWindowsProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1160 mergePropertyStruct(ctx, genProps, notWindowsProperties)
1161 }
Colin Crossa195f912019-10-16 11:07:20 -07001162 }
1163
1164 // Handle 64-bit device properties in the form:
1165 // target {
1166 // android64 {
1167 // key: value,
1168 // },
1169 // android32 {
1170 // key: value,
1171 // },
1172 // },
1173 // WARNING: this is probably not what you want to use in your blueprints file, it selects
1174 // options for all targets on a device that supports 64-bit binaries, not just the targets
1175 // that are being compiled for 64-bit. Its expected use case is binaries like linker and
1176 // debuggerd that need to know when they are a 32-bit process running on a 64-bit device
1177 if os.Class == Device {
1178 if ctx.Config().Android64() {
1179 field := "Android64"
1180 prefix := "target.android64"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001181 if android64Properties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1182 mergePropertyStruct(ctx, genProps, android64Properties)
1183 }
Colin Crossa195f912019-10-16 11:07:20 -07001184 } else {
1185 field := "Android32"
1186 prefix := "target.android32"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001187 if android32Properties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1188 mergePropertyStruct(ctx, genProps, android32Properties)
1189 }
Colin Crossa195f912019-10-16 11:07:20 -07001190 }
1191 }
1192 }
1193 }
1194}
1195
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001196// Returns the struct containing the properties specific to the given
1197// architecture type. These look like this in Blueprint files:
1198// arch: {
1199// arm64: {
1200// key: value,
1201// },
1202// },
1203// This struct will also contain sub-structs containing to the architecture/CPU
1204// variants and features that themselves contain properties specific to those.
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001205func getArchTypeStruct(ctx ArchVariantContext, archProperties interface{}, archType ArchType) (reflect.Value, bool) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001206 archPropValues := reflect.ValueOf(archProperties).Elem()
1207 archProp := archPropValues.FieldByName("Arch").Elem()
1208 prefix := "arch." + archType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001209 return getChildPropertyStruct(ctx, archProp, archType.Name, prefix)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001210}
1211
1212// Returns the struct containing the properties specific to a given multilib
1213// value. These look like this in the Blueprint file:
1214// multilib: {
1215// lib32: {
1216// key: value,
1217// },
1218// },
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001219func getMultilibStruct(ctx ArchVariantContext, archProperties interface{}, archType ArchType) (reflect.Value, bool) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001220 archPropValues := reflect.ValueOf(archProperties).Elem()
1221 multilibProp := archPropValues.FieldByName("Multilib").Elem()
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001222 return getChildPropertyStruct(ctx, multilibProp, archType.Multilib, "multilib."+archType.Multilib)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001223}
1224
Liz Kammer9abd62d2021-05-21 08:37:59 -04001225func GetCompoundTargetField(os OsType, arch ArchType) string {
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04001226 return os.Field + "_" + arch.Name
1227}
1228
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001229// Returns the structs corresponding to the properties specific to the given
1230// architecture and OS in archProperties.
1231func getArchProperties(ctx BaseMutatorContext, archProperties interface{}, arch Arch, os OsType, nativeBridgeEnabled bool) []reflect.Value {
1232 result := make([]reflect.Value, 0)
1233 archPropValues := reflect.ValueOf(archProperties).Elem()
1234
1235 targetProp := archPropValues.FieldByName("Target").Elem()
1236
1237 archType := arch.ArchType
1238
1239 if arch.ArchType != Common {
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001240 archStruct, ok := getArchTypeStruct(ctx, archProperties, arch.ArchType)
1241 if ok {
1242 result = append(result, archStruct)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001243
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001244 // Handle arch-variant-specific properties in the form:
1245 // arch: {
1246 // arm: {
1247 // variant: {
1248 // key: value,
1249 // },
1250 // },
1251 // },
1252 v := variantReplacer.Replace(arch.ArchVariant)
1253 if v != "" {
1254 prefix := "arch." + archType.Name + "." + v
1255 if variantProperties, ok := getChildPropertyStruct(ctx, archStruct, v, prefix); ok {
1256 result = append(result, variantProperties)
1257 }
1258 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001259
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001260 // Handle cpu-variant-specific properties in the form:
1261 // arch: {
1262 // arm: {
1263 // variant: {
1264 // key: value,
1265 // },
1266 // },
1267 // },
1268 if arch.CpuVariant != arch.ArchVariant {
1269 c := variantReplacer.Replace(arch.CpuVariant)
1270 if c != "" {
1271 prefix := "arch." + archType.Name + "." + c
1272 if cpuVariantProperties, ok := getChildPropertyStruct(ctx, archStruct, c, prefix); ok {
1273 result = append(result, cpuVariantProperties)
1274 }
1275 }
1276 }
1277
1278 // Handle arch-feature-specific properties in the form:
1279 // arch: {
1280 // arm: {
1281 // feature: {
1282 // key: value,
1283 // },
1284 // },
1285 // },
1286 for _, feature := range arch.ArchFeatures {
1287 prefix := "arch." + archType.Name + "." + feature
1288 if featureProperties, ok := getChildPropertyStruct(ctx, archStruct, feature, prefix); ok {
1289 result = append(result, featureProperties)
1290 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001291 }
1292 }
1293
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001294 if multilibProperties, ok := getMultilibStruct(ctx, archProperties, archType); ok {
1295 result = append(result, multilibProperties)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001296 }
1297
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001298 // Handle combined OS-feature and arch specific properties in the form:
1299 // target: {
1300 // bionic_x86: {
1301 // key: value,
1302 // },
1303 // }
1304 if os.Linux() {
1305 field := "Linux_" + arch.ArchType.Name
1306 userFriendlyField := "target.linux_" + arch.ArchType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001307 if linuxProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1308 result = append(result, linuxProperties)
1309 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001310 }
1311
1312 if os.Bionic() {
1313 field := "Bionic_" + archType.Name
1314 userFriendlyField := "target.bionic_" + archType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001315 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1316 result = append(result, bionicProperties)
1317 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001318 }
1319
1320 // Handle combined OS and arch specific properties in the form:
1321 // target: {
1322 // linux_glibc_x86: {
1323 // key: value,
1324 // },
1325 // linux_glibc_arm: {
1326 // key: value,
1327 // },
1328 // android_arm {
1329 // key: value,
1330 // },
1331 // android_x86 {
1332 // key: value,
1333 // },
1334 // },
Liz Kammer9abd62d2021-05-21 08:37:59 -04001335 field := GetCompoundTargetField(os, archType)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001336 userFriendlyField := "target." + os.Name + "_" + archType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001337 if osArchProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1338 result = append(result, osArchProperties)
1339 }
Colin Cross528d67e2021-07-23 22:23:07 +00001340
1341 if os == LinuxMusl {
1342 // Special case: to ease the transition from glibc to musl, apply linux_glibc
1343 // properties (which has historically mean host linux) to musl variants.
1344 field := "Linux_glibc_" + archType.Name
1345 userFriendlyField := "target.linux_glibc_" + archType.Name
1346 if osArchProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1347 result = append(result, osArchProperties)
1348 }
1349 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001350 }
1351
1352 // Handle arm on x86 properties in the form:
1353 // target {
1354 // arm_on_x86 {
1355 // key: value,
1356 // },
1357 // arm_on_x86_64 {
1358 // key: value,
1359 // },
1360 // },
1361 if os.Class == Device {
1362 if arch.ArchType == X86 && (hasArmAbi(arch) ||
1363 hasArmAndroidArch(ctx.Config().Targets[Android])) {
1364 field := "Arm_on_x86"
1365 userFriendlyField := "target.arm_on_x86"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001366 if armOnX86Properties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1367 result = append(result, armOnX86Properties)
1368 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001369 }
1370 if arch.ArchType == X86_64 && (hasArmAbi(arch) ||
1371 hasArmAndroidArch(ctx.Config().Targets[Android])) {
1372 field := "Arm_on_x86_64"
1373 userFriendlyField := "target.arm_on_x86_64"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001374 if armOnX8664Properties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1375 result = append(result, armOnX8664Properties)
1376 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001377 }
1378 if os == Android && nativeBridgeEnabled {
1379 userFriendlyField := "Native_bridge"
1380 prefix := "target.native_bridge"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001381 if nativeBridgeProperties, ok := getChildPropertyStruct(ctx, targetProp, userFriendlyField, prefix); ok {
1382 result = append(result, nativeBridgeProperties)
1383 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001384 }
1385 }
1386
1387 return result
1388}
1389
Colin Crossa6845402020-11-16 15:08:19 -08001390// Squash the appropriate arch-specific property structs into the matching top level property
1391// structs based on the CompileTarget value that was annotated on the variant.
Colin Cross4157e882019-06-06 16:57:04 -07001392func (m *ModuleBase) setArchProperties(ctx BottomUpMutatorContext) {
1393 arch := m.Arch()
1394 os := m.Os()
Colin Crossd3ba0392015-05-07 14:11:29 -07001395
Colin Cross4157e882019-06-06 16:57:04 -07001396 for i := range m.generalProperties {
1397 genProps := m.generalProperties[i]
1398 if m.archProperties[i] == nil {
Dan Willemsenb1957a52016-06-23 23:44:54 -07001399 continue
1400 }
Dan Willemsenb1957a52016-06-23 23:44:54 -07001401
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001402 propStructs := make([]reflect.Value, 0)
1403 for _, archProperty := range m.archProperties[i] {
1404 propStructShard := getArchProperties(ctx, archProperty, arch, os, m.Target().NativeBridge == NativeBridgeEnabled)
1405 propStructs = append(propStructs, propStructShard...)
1406 }
Dan Willemsenb1957a52016-06-23 23:44:54 -07001407
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001408 for _, propStruct := range propStructs {
1409 mergePropertyStruct(ctx, genProps, propStruct)
Colin Crossbb2e2b72016-12-08 17:23:53 -08001410 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001411 }
1412}
1413
Colin Cross0c66bc62021-07-20 09:47:41 -07001414// determineBuildOS stores the OS and architecture used for host targets used during the build into
Colin Cross528d67e2021-07-23 22:23:07 +00001415// config based on the runtime OS and architecture determined by Go and the product configuration.
Colin Cross0c66bc62021-07-20 09:47:41 -07001416func determineBuildOS(config *config) {
1417 config.BuildOS = func() OsType {
1418 switch runtime.GOOS {
1419 case "linux":
Colin Cross528d67e2021-07-23 22:23:07 +00001420 if Bool(config.productVariables.HostMusl) {
1421 return LinuxMusl
1422 }
Colin Cross0c66bc62021-07-20 09:47:41 -07001423 return Linux
1424 case "darwin":
1425 return Darwin
1426 default:
1427 panic(fmt.Sprintf("unsupported OS: %s", runtime.GOOS))
1428 }
1429 }()
1430
1431 config.BuildArch = func() ArchType {
1432 switch runtime.GOARCH {
1433 case "amd64":
1434 return X86_64
1435 default:
1436 panic(fmt.Sprintf("unsupported Arch: %s", runtime.GOARCH))
1437 }
1438 }()
1439
1440}
1441
Colin Crossa6845402020-11-16 15:08:19 -08001442// Convert the arch product variables into a list of targets for each OsType.
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001443func decodeTargetProductVariables(config *config) (map[OsType][]Target, error) {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001444 variables := config.productVariables
Dan Willemsen490fd492015-11-24 17:53:15 -08001445
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001446 targets := make(map[OsType][]Target)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001447 var targetErr error
1448
dimitry1f33e402019-03-26 12:39:31 +01001449 addTarget := func(os OsType, archName string, archVariant, cpuVariant *string, abi []string,
dimitry8d6dde82019-07-11 10:23:53 +02001450 nativeBridgeEnabled NativeBridgeSupport, nativeBridgeHostArchName *string,
1451 nativeBridgeRelativePath *string) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001452 if targetErr != nil {
1453 return
Dan Willemsen490fd492015-11-24 17:53:15 -08001454 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001455
Dan Willemsen01a3c252019-01-11 19:02:16 -08001456 arch, err := decodeArch(os, archName, archVariant, cpuVariant, abi)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001457 if err != nil {
1458 targetErr = err
1459 return
1460 }
dimitry8d6dde82019-07-11 10:23:53 +02001461 nativeBridgeRelativePathStr := String(nativeBridgeRelativePath)
1462 nativeBridgeHostArchNameStr := String(nativeBridgeHostArchName)
1463
1464 // Use guest arch as relative install path by default
1465 if nativeBridgeEnabled && nativeBridgeRelativePathStr == "" {
1466 nativeBridgeRelativePathStr = arch.ArchType.String()
1467 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001468
Jiyong Park1613e552020-09-14 19:43:17 +09001469 // A target is considered as HostCross if it's a host target which can't run natively on
1470 // the currently configured build machine (either because the OS is different or because of
1471 // the unsupported arch)
1472 hostCross := false
1473 if os.Class == Host {
1474 var osSupported bool
Colin Cross0c66bc62021-07-20 09:47:41 -07001475 if os == config.BuildOS {
Jiyong Park1613e552020-09-14 19:43:17 +09001476 osSupported = true
Colin Cross0c66bc62021-07-20 09:47:41 -07001477 } else if config.BuildOS.Linux() && os.Linux() {
Jiyong Park1613e552020-09-14 19:43:17 +09001478 // LinuxBionic and Linux are compatible
1479 osSupported = true
1480 } else {
1481 osSupported = false
1482 }
1483
1484 var archSupported bool
1485 if arch.ArchType == Common {
1486 archSupported = true
1487 } else if arch.ArchType.Name == *variables.HostArch {
1488 archSupported = true
1489 } else if variables.HostSecondaryArch != nil && arch.ArchType.Name == *variables.HostSecondaryArch {
1490 archSupported = true
1491 } else {
1492 archSupported = false
1493 }
1494 if !osSupported || !archSupported {
1495 hostCross = true
1496 }
1497 }
1498
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001499 targets[os] = append(targets[os],
Colin Crossa1ad8d12016-06-01 17:09:44 -07001500 Target{
dimitry8d6dde82019-07-11 10:23:53 +02001501 Os: os,
1502 Arch: arch,
1503 NativeBridge: nativeBridgeEnabled,
1504 NativeBridgeHostArchName: nativeBridgeHostArchNameStr,
1505 NativeBridgeRelativePath: nativeBridgeRelativePathStr,
Jiyong Park1613e552020-09-14 19:43:17 +09001506 HostCross: hostCross,
Colin Crossa1ad8d12016-06-01 17:09:44 -07001507 })
Dan Willemsen490fd492015-11-24 17:53:15 -08001508 }
1509
Colin Cross4225f652015-09-17 14:33:42 -07001510 if variables.HostArch == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001511 return nil, fmt.Errorf("No host primary architecture set")
Colin Cross4225f652015-09-17 14:33:42 -07001512 }
1513
Colin Crossa6845402020-11-16 15:08:19 -08001514 // The primary host target, which must always exist.
Colin Cross0c66bc62021-07-20 09:47:41 -07001515 addTarget(config.BuildOS, *variables.HostArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -07001516
Colin Crossa6845402020-11-16 15:08:19 -08001517 // An optional secondary host target.
Colin Crosseeabb892015-11-20 13:07:51 -08001518 if variables.HostSecondaryArch != nil && *variables.HostSecondaryArch != "" {
Colin Cross0c66bc62021-07-20 09:47:41 -07001519 addTarget(config.BuildOS, *variables.HostSecondaryArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -08001520 }
1521
Colin Crossa6845402020-11-16 15:08:19 -08001522 // Optional cross-compiled host targets, generally Windows.
Colin Crossff3ae9d2018-04-10 16:15:18 -07001523 if String(variables.CrossHost) != "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001524 crossHostOs := osByName(*variables.CrossHost)
1525 if crossHostOs == NoOsType {
1526 return nil, fmt.Errorf("Unknown cross host OS %q", *variables.CrossHost)
1527 }
1528
Colin Crossff3ae9d2018-04-10 16:15:18 -07001529 if String(variables.CrossHostArch) == "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001530 return nil, fmt.Errorf("No cross-host primary architecture set")
Dan Willemsen490fd492015-11-24 17:53:15 -08001531 }
1532
Colin Crossa6845402020-11-16 15:08:19 -08001533 // The primary cross-compiled host target.
dimitry8d6dde82019-07-11 10:23:53 +02001534 addTarget(crossHostOs, *variables.CrossHostArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -08001535
Colin Crossa6845402020-11-16 15:08:19 -08001536 // An optional secondary cross-compiled host target.
Dan Willemsen490fd492015-11-24 17:53:15 -08001537 if variables.CrossHostSecondaryArch != nil && *variables.CrossHostSecondaryArch != "" {
dimitry8d6dde82019-07-11 10:23:53 +02001538 addTarget(crossHostOs, *variables.CrossHostSecondaryArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -08001539 }
1540 }
1541
Colin Crossa6845402020-11-16 15:08:19 -08001542 // Optional device targets
Dan Willemsen3f32f032016-07-11 14:36:48 -07001543 if variables.DeviceArch != nil && *variables.DeviceArch != "" {
Colin Crossa6845402020-11-16 15:08:19 -08001544 // The primary device target.
Colin Crosscb0ac952021-07-20 13:17:15 -07001545 addTarget(Android, *variables.DeviceArch, variables.DeviceArchVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001546 variables.DeviceCpuVariant, variables.DeviceAbi, NativeBridgeDisabled, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -07001547
Colin Crossa6845402020-11-16 15:08:19 -08001548 // An optional secondary device target.
Dan Willemsen3f32f032016-07-11 14:36:48 -07001549 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" {
1550 addTarget(Android, *variables.DeviceSecondaryArch,
1551 variables.DeviceSecondaryArchVariant, variables.DeviceSecondaryCpuVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001552 variables.DeviceSecondaryAbi, NativeBridgeDisabled, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -07001553 }
dimitry1f33e402019-03-26 12:39:31 +01001554
Colin Crossa6845402020-11-16 15:08:19 -08001555 // An optional NativeBridge device target.
dimitry1f33e402019-03-26 12:39:31 +01001556 if variables.NativeBridgeArch != nil && *variables.NativeBridgeArch != "" {
1557 addTarget(Android, *variables.NativeBridgeArch,
1558 variables.NativeBridgeArchVariant, variables.NativeBridgeCpuVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001559 variables.NativeBridgeAbi, NativeBridgeEnabled, variables.DeviceArch,
1560 variables.NativeBridgeRelativePath)
dimitry1f33e402019-03-26 12:39:31 +01001561 }
1562
Colin Crossa6845402020-11-16 15:08:19 -08001563 // An optional secondary NativeBridge device target.
dimitry1f33e402019-03-26 12:39:31 +01001564 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" &&
1565 variables.NativeBridgeSecondaryArch != nil && *variables.NativeBridgeSecondaryArch != "" {
1566 addTarget(Android, *variables.NativeBridgeSecondaryArch,
1567 variables.NativeBridgeSecondaryArchVariant,
1568 variables.NativeBridgeSecondaryCpuVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001569 variables.NativeBridgeSecondaryAbi,
1570 NativeBridgeEnabled,
1571 variables.DeviceSecondaryArch,
1572 variables.NativeBridgeSecondaryRelativePath)
dimitry1f33e402019-03-26 12:39:31 +01001573 }
Colin Cross4225f652015-09-17 14:33:42 -07001574 }
1575
Colin Crossa1ad8d12016-06-01 17:09:44 -07001576 if targetErr != nil {
1577 return nil, targetErr
1578 }
1579
1580 return targets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001581}
1582
Colin Crossbb2e2b72016-12-08 17:23:53 -08001583// hasArmAbi returns true if arch has at least one arm ABI
1584func hasArmAbi(arch Arch) bool {
Jaewoong Jung3aff5782020-02-11 07:54:35 -08001585 return PrefixInList(arch.Abi, "arm")
Colin Crossbb2e2b72016-12-08 17:23:53 -08001586}
1587
dimitry628db6f2019-05-22 17:16:21 +02001588// hasArmArch returns true if targets has at least non-native_bridge arm Android arch
Colin Cross4247f0d2017-04-13 16:56:14 -07001589func hasArmAndroidArch(targets []Target) bool {
1590 for _, target := range targets {
Victor Khimenko1a31f802020-09-17 03:07:31 +02001591 if target.Os == Android && target.Arch.ArchType == Arm {
Victor Khimenko5eb8ec12018-03-21 20:30:54 +01001592 return true
1593 }
1594 }
1595 return false
1596}
1597
Colin Crossa6845402020-11-16 15:08:19 -08001598// archConfig describes a built-in configuration.
Dan Albert4098deb2016-10-19 14:04:41 -07001599type archConfig struct {
1600 arch string
1601 archVariant string
1602 cpuVariant string
1603 abi []string
1604}
1605
Dan Albertf1d14c72020-07-30 14:32:55 -07001606// getNdkAbisConfig returns the list of archConfigs that are used for bulding
1607// the API stubs and static libraries that are included in the NDK. These are
1608// built *without Neon*, because non-Neon is still supported and building these
1609// with Neon will break those users.
Dan Albert4098deb2016-10-19 14:04:41 -07001610func getNdkAbisConfig() []archConfig {
1611 return []archConfig{
Tamas Petzbca786d2021-01-20 18:56:33 +01001612 {"arm64", "armv8-a-branchprot", "", []string{"arm64-v8a"}},
Inseob Kim5219c0e2021-06-17 00:33:00 +09001613 {"arm", "armv7-a", "", []string{"armeabi-v7a"}},
Dan Albert4098deb2016-10-19 14:04:41 -07001614 {"x86_64", "", "", []string{"x86_64"}},
Inseob Kim5219c0e2021-06-17 00:33:00 +09001615 {"x86", "", "", []string{"x86"}},
Dan Albert4098deb2016-10-19 14:04:41 -07001616 }
1617}
1618
Colin Crossa6845402020-11-16 15:08:19 -08001619// getAmlAbisConfig returns a list of archConfigs for the ABIs supported by mainline modules.
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001620func getAmlAbisConfig() []archConfig {
1621 return []archConfig{
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001622 {"arm64", "armv8-a", "", []string{"arm64-v8a"}},
Inseob Kim5219c0e2021-06-17 00:33:00 +09001623 {"arm", "armv7-a-neon", "", []string{"armeabi-v7a"}},
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001624 {"x86_64", "", "", []string{"x86_64"}},
Inseob Kim5219c0e2021-06-17 00:33:00 +09001625 {"x86", "", "", []string{"x86"}},
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001626 }
1627}
1628
Colin Crossa6845402020-11-16 15:08:19 -08001629// decodeArchSettings converts a list of archConfigs into a list of Targets for the given OsType.
Dan Willemsen01a3c252019-01-11 19:02:16 -08001630func decodeArchSettings(os OsType, archConfigs []archConfig) ([]Target, error) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001631 var ret []Target
Dan Willemsen322acaf2016-01-12 23:07:05 -08001632
Dan Albert4098deb2016-10-19 14:04:41 -07001633 for _, config := range archConfigs {
Dan Willemsen01a3c252019-01-11 19:02:16 -08001634 arch, err := decodeArch(os, config.arch, &config.archVariant,
Colin Crossa74ca042019-01-31 14:31:51 -08001635 &config.cpuVariant, config.abi)
Dan Willemsen322acaf2016-01-12 23:07:05 -08001636 if err != nil {
1637 return nil, err
1638 }
Colin Cross3b19f5d2019-09-17 14:45:31 -07001639
Colin Crossa1ad8d12016-06-01 17:09:44 -07001640 ret = append(ret, Target{
1641 Os: Android,
1642 Arch: arch,
1643 })
Dan Willemsen322acaf2016-01-12 23:07:05 -08001644 }
1645
1646 return ret, nil
1647}
1648
Colin Crossa6845402020-11-16 15:08:19 -08001649// decodeArch converts a set of strings from product variables into an Arch struct.
Colin Crossa74ca042019-01-31 14:31:51 -08001650func decodeArch(os OsType, arch string, archVariant, cpuVariant *string, abi []string) (Arch, error) {
Colin Crossa6845402020-11-16 15:08:19 -08001651 // Verify the arch is valid
Colin Crosseeabb892015-11-20 13:07:51 -08001652 archType, ok := archTypeMap[arch]
1653 if !ok {
1654 return Arch{}, fmt.Errorf("unknown arch %q", arch)
1655 }
Colin Cross4225f652015-09-17 14:33:42 -07001656
Colin Crosseeabb892015-11-20 13:07:51 -08001657 a := Arch{
Colin Cross4225f652015-09-17 14:33:42 -07001658 ArchType: archType,
Colin Crossa6845402020-11-16 15:08:19 -08001659 ArchVariant: String(archVariant),
1660 CpuVariant: String(cpuVariant),
Colin Crossa74ca042019-01-31 14:31:51 -08001661 Abi: abi,
Colin Crosseeabb892015-11-20 13:07:51 -08001662 }
1663
Colin Crossa6845402020-11-16 15:08:19 -08001664 // Convert generic arch variants into the empty string.
Colin Crosseeabb892015-11-20 13:07:51 -08001665 if a.ArchVariant == a.ArchType.Name || a.ArchVariant == "generic" {
1666 a.ArchVariant = ""
1667 }
1668
Colin Crossa6845402020-11-16 15:08:19 -08001669 // Convert generic CPU variants into the empty string.
Colin Crosseeabb892015-11-20 13:07:51 -08001670 if a.CpuVariant == a.ArchType.Name || a.CpuVariant == "generic" {
1671 a.CpuVariant = ""
1672 }
1673
Colin Crossa6845402020-11-16 15:08:19 -08001674 // Filter empty ABIs out of the list.
Colin Crosseeabb892015-11-20 13:07:51 -08001675 for i := 0; i < len(a.Abi); i++ {
1676 if a.Abi[i] == "" {
1677 a.Abi = append(a.Abi[:i], a.Abi[i+1:]...)
1678 i--
1679 }
1680 }
1681
Dan Willemsen01a3c252019-01-11 19:02:16 -08001682 if a.ArchVariant == "" {
Colin Crossa6845402020-11-16 15:08:19 -08001683 // Set ArchFeatures from the default arch features.
Dan Willemsen01a3c252019-01-11 19:02:16 -08001684 if featureMap, ok := defaultArchFeatureMap[os]; ok {
1685 a.ArchFeatures = featureMap[archType]
1686 }
1687 } else {
Colin Crossa6845402020-11-16 15:08:19 -08001688 // Set ArchFeatures from the arch type.
Dan Willemsen01a3c252019-01-11 19:02:16 -08001689 if featureMap, ok := archFeatureMap[archType]; ok {
1690 a.ArchFeatures = featureMap[a.ArchVariant]
1691 }
Colin Crossc5c24ad2015-11-20 15:35:00 -08001692 }
1693
Colin Crosseeabb892015-11-20 13:07:51 -08001694 return a, nil
Colin Cross4225f652015-09-17 14:33:42 -07001695}
1696
Colin Crossa6845402020-11-16 15:08:19 -08001697// filterMultilibTargets takes a list of Targets and a multilib value and returns a new list of
1698// Targets containing only those that have the given multilib value.
Colin Cross69617d32016-09-06 10:39:07 -07001699func filterMultilibTargets(targets []Target, multilib string) []Target {
1700 var ret []Target
1701 for _, t := range targets {
1702 if t.Arch.ArchType.Multilib == multilib {
1703 ret = append(ret, t)
1704 }
1705 }
1706 return ret
1707}
1708
Colin Crossa6845402020-11-16 15:08:19 -08001709// getCommonTargets returns the set of Os specific common architecture targets for each Os in a list
1710// of targets.
Nan Zhangdb0b9a32017-02-27 10:12:13 -08001711func getCommonTargets(targets []Target) []Target {
1712 var ret []Target
1713 set := make(map[string]bool)
1714
1715 for _, t := range targets {
1716 if _, found := set[t.Os.String()]; !found {
1717 set[t.Os.String()] = true
1718 ret = append(ret, commonTargetMap[t.Os.String()])
1719 }
1720 }
1721
1722 return ret
1723}
1724
Colin Crossa6845402020-11-16 15:08:19 -08001725// firstTarget takes a list of Targets and a list of multilib values and returns a list of Targets
1726// that contains zero or one Target for each OsType, selecting the one that matches the earliest
1727// filter.
Colin Cross3dceee32018-09-06 10:19:57 -07001728func firstTarget(targets []Target, filters ...string) []Target {
Jiyong Park22101982020-09-17 19:09:58 +09001729 // find the first target from each OS
1730 var ret []Target
1731 hasHost := false
1732 set := make(map[OsType]bool)
1733
Colin Cross6b4a32d2017-12-05 13:42:45 -08001734 for _, filter := range filters {
1735 buildTargets := filterMultilibTargets(targets, filter)
Jiyong Park22101982020-09-17 19:09:58 +09001736 for _, t := range buildTargets {
1737 if _, found := set[t.Os]; !found {
1738 hasHost = hasHost || (t.Os.Class == Host)
1739 set[t.Os] = true
1740 ret = append(ret, t)
1741 }
Colin Cross6b4a32d2017-12-05 13:42:45 -08001742 }
1743 }
Jiyong Park22101982020-09-17 19:09:58 +09001744 return ret
Colin Cross6b4a32d2017-12-05 13:42:45 -08001745}
1746
Colin Crossa6845402020-11-16 15:08:19 -08001747// decodeMultilibTargets uses the module's multilib setting to select one or more targets from a
1748// list of Targets.
Colin Crossee0bc3b2018-10-02 22:01:37 -07001749func decodeMultilibTargets(multilib string, targets []Target, prefer32 bool) ([]Target, error) {
Colin Crossa6845402020-11-16 15:08:19 -08001750 var buildTargets []Target
Colin Cross6b4a32d2017-12-05 13:42:45 -08001751
Colin Cross4225f652015-09-17 14:33:42 -07001752 switch multilib {
1753 case "common":
Colin Cross6b4a32d2017-12-05 13:42:45 -08001754 buildTargets = getCommonTargets(targets)
1755 case "common_first":
1756 buildTargets = getCommonTargets(targets)
1757 if prefer32 {
Colin Cross3dceee32018-09-06 10:19:57 -07001758 buildTargets = append(buildTargets, firstTarget(targets, "lib32", "lib64")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001759 } else {
Colin Cross3dceee32018-09-06 10:19:57 -07001760 buildTargets = append(buildTargets, firstTarget(targets, "lib64", "lib32")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001761 }
Colin Cross4225f652015-09-17 14:33:42 -07001762 case "both":
Colin Cross8b74d172016-09-13 09:59:14 -07001763 if prefer32 {
1764 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1765 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1766 } else {
1767 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1768 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1769 }
Colin Cross4225f652015-09-17 14:33:42 -07001770 case "32":
Colin Cross69617d32016-09-06 10:39:07 -07001771 buildTargets = filterMultilibTargets(targets, "lib32")
Colin Cross4225f652015-09-17 14:33:42 -07001772 case "64":
Colin Cross69617d32016-09-06 10:39:07 -07001773 buildTargets = filterMultilibTargets(targets, "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001774 case "first":
1775 if prefer32 {
Colin Cross3dceee32018-09-06 10:19:57 -07001776 buildTargets = firstTarget(targets, "lib32", "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001777 } else {
Colin Cross3dceee32018-09-06 10:19:57 -07001778 buildTargets = firstTarget(targets, "lib64", "lib32")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001779 }
Victor Chang9448e8f2020-09-14 15:34:16 +01001780 case "first_prefer32":
1781 buildTargets = firstTarget(targets, "lib32", "lib64")
Colin Cross69617d32016-09-06 10:39:07 -07001782 case "prefer32":
Colin Cross3dceee32018-09-06 10:19:57 -07001783 buildTargets = filterMultilibTargets(targets, "lib32")
1784 if len(buildTargets) == 0 {
1785 buildTargets = filterMultilibTargets(targets, "lib64")
1786 }
Colin Cross4225f652015-09-17 14:33:42 -07001787 default:
Victor Chang9448e8f2020-09-14 15:34:16 +01001788 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 -07001789 multilib)
Colin Cross4225f652015-09-17 14:33:42 -07001790 }
1791
Colin Crossa1ad8d12016-06-01 17:09:44 -07001792 return buildTargets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001793}
Jingwen Chen5d864492021-02-24 07:20:12 -05001794
Chris Parsonsc424b762021-04-29 18:06:50 -04001795func (m *ModuleBase) getArchPropertySet(propertySet interface{}, archType ArchType) interface{} {
1796 archString := archType.Field
1797 for i := range m.archProperties {
1798 if m.archProperties[i] == nil {
1799 // Skip over nil properties
1800 continue
1801 }
1802
1803 // Not archProperties are usable; this function looks for properties of a very specific
1804 // form, and ignores the rest.
1805 for _, archProperty := range m.archProperties[i] {
1806 // archPropValue is a property struct, we are looking for the form:
1807 // `arch: { arm: { key: value, ... }}`
1808 archPropValue := reflect.ValueOf(archProperty).Elem()
1809
1810 // Unwrap src so that it should looks like a pointer to `arm: { key: value, ... }`
1811 src := archPropValue.FieldByName("Arch").Elem()
1812
1813 // Step into non-nil pointers to structs in the src value.
1814 if src.Kind() == reflect.Ptr {
1815 if src.IsNil() {
1816 continue
1817 }
1818 src = src.Elem()
1819 }
1820
1821 // Find the requested field (e.g. arm, x86) in the src struct.
1822 src = src.FieldByName(archString)
1823
1824 // We only care about structs.
1825 if !src.IsValid() || src.Kind() != reflect.Struct {
1826 continue
1827 }
1828
1829 // If the value of the field is a struct then step into the
1830 // BlueprintEmbed field. The special "BlueprintEmbed" name is
1831 // used by createArchPropTypeDesc to embed the arch properties
1832 // in the parent struct, so the src arch prop should be in this
1833 // field.
1834 //
1835 // See createArchPropTypeDesc for more details on how Arch-specific
1836 // module properties are processed from the nested props and written
1837 // into the module's archProperties.
1838 src = src.FieldByName("BlueprintEmbed")
1839
1840 // Clone the destination prop, since we want a unique prop struct per arch.
1841 propertySetClone := reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
1842
1843 // Copy the located property struct into the cloned destination property struct.
1844 err := proptools.ExtendMatchingProperties([]interface{}{propertySetClone}, src.Interface(), nil, proptools.OrderReplace)
1845 if err != nil {
1846 // This is fine, it just means the src struct doesn't match the type of propertySet.
1847 continue
1848 }
1849
1850 return propertySetClone
1851 }
1852 }
1853 // No property set was found specific to the given arch, so return an empty
1854 // property set.
1855 return reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
1856}
1857
1858// getMultilibPropertySet returns a property set struct matching the type of
1859// `propertySet`, containing multilib-specific module properties for the given architecture.
1860// If no multilib-specific properties exist for the given architecture, returns an empty property
1861// set matching `propertySet`'s type.
1862func (m *ModuleBase) getMultilibPropertySet(propertySet interface{}, archType ArchType) interface{} {
1863 // archType.Multilib is lowercase (for example, lib32) but property struct field is
1864 // capitalized, such as Lib32, so use strings.Title to capitalize it.
1865 multiLibString := strings.Title(archType.Multilib)
1866
1867 for i := range m.archProperties {
1868 if m.archProperties[i] == nil {
1869 // Skip over nil properties
1870 continue
1871 }
1872
1873 // Not archProperties are usable; this function looks for properties of a very specific
1874 // form, and ignores the rest.
1875 for _, archProperties := range m.archProperties[i] {
1876 // archPropValue is a property struct, we are looking for the form:
1877 // `multilib: { lib32: { key: value, ... }}`
1878 archPropValue := reflect.ValueOf(archProperties).Elem()
1879
1880 // Unwrap src so that it should looks like a pointer to `lib32: { key: value, ... }`
1881 src := archPropValue.FieldByName("Multilib").Elem()
1882
1883 // Step into non-nil pointers to structs in the src value.
1884 if src.Kind() == reflect.Ptr {
1885 if src.IsNil() {
1886 // Ignore nil pointers.
1887 continue
1888 }
1889 src = src.Elem()
1890 }
1891
1892 // Find the requested field (e.g. lib32) in the src struct.
1893 src = src.FieldByName(multiLibString)
1894
1895 // We only care about valid struct pointers.
1896 if !src.IsValid() || src.Kind() != reflect.Ptr || src.Elem().Kind() != reflect.Struct {
1897 continue
1898 }
1899
1900 // Get the zero value for the requested property set.
1901 propertySetClone := reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
1902
1903 // Copy the located property struct into the "zero" property set struct.
1904 err := proptools.ExtendMatchingProperties([]interface{}{propertySetClone}, src.Interface(), nil, proptools.OrderReplace)
1905
1906 if err != nil {
1907 // This is fine, it just means the src struct doesn't match.
1908 continue
1909 }
1910
1911 return propertySetClone
1912 }
1913 }
1914
1915 // There were no multilib properties specifically matching the given archtype.
1916 // Return zeroed value.
1917 return reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
1918}
1919
Liz Kammerb6dbc872021-05-14 15:14:40 -04001920// ArchVariantContext defines the limited context necessary to retrieve arch_variant properties.
1921type ArchVariantContext interface {
1922 ModuleErrorf(fmt string, args ...interface{})
1923 PropertyErrorf(property, fmt string, args ...interface{})
1924}
1925
Liz Kammer9abd62d2021-05-21 08:37:59 -04001926// ArchVariantProperties represents a map of arch-variant config strings to a property interface{}.
1927type ArchVariantProperties map[string]interface{}
1928
1929// ConfigurationAxisToArchVariantProperties represents a map of bazel.ConfigurationAxis to
1930// ArchVariantProperties, such that each independent arch-variant axis maps to the
1931// configs/properties for that axis.
1932type ConfigurationAxisToArchVariantProperties map[bazel.ConfigurationAxis]ArchVariantProperties
1933
1934// GetArchVariantProperties returns a ConfigurationAxisToArchVariantProperties where the
1935// arch-variant properties correspond to the values of the properties of the 'propertySet' struct
1936// that are specific to that axis/configuration. Each axis is independent, containing
1937// non-overlapping configs that correspond to the various "arch-variant" support, at this time:
1938// arches (including multilib)
1939// oses
1940// arch+os combinations
Jingwen Chen5d864492021-02-24 07:20:12 -05001941//
Liz Kammer9abd62d2021-05-21 08:37:59 -04001942// For example, passing a struct { Foo bool, Bar string } will return an interface{} that can be
1943// type asserted back into the same struct, containing the config-specific property value specified
1944// by the module if defined.
Chris Parsonsc424b762021-04-29 18:06:50 -04001945//
1946// Arch-specific properties may come from an arch stanza or a multilib stanza; properties
1947// in these stanzas are combined.
1948// For example: `arch: { x86: { Foo: ["bar"] } }, multilib: { lib32: {` Foo: ["baz"] } }`
1949// will result in `Foo: ["bar", "baz"]` being returned for architecture x86, if the given
1950// propertyset contains `Foo []string`.
Liz Kammer9abd62d2021-05-21 08:37:59 -04001951func (m *ModuleBase) GetArchVariantProperties(ctx ArchVariantContext, propertySet interface{}) ConfigurationAxisToArchVariantProperties {
Jingwen Chen5d864492021-02-24 07:20:12 -05001952 // Return value of the arch types to the prop values for that arch.
Liz Kammer9abd62d2021-05-21 08:37:59 -04001953 axisToProps := ConfigurationAxisToArchVariantProperties{}
Jingwen Chen5d864492021-02-24 07:20:12 -05001954
1955 // Nothing to do for non-arch-specific modules.
1956 if !m.ArchSpecific() {
Liz Kammer9abd62d2021-05-21 08:37:59 -04001957 return axisToProps
Jingwen Chen5d864492021-02-24 07:20:12 -05001958 }
1959
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001960 dstType := reflect.ValueOf(propertySet).Type()
1961 var archProperties []interface{}
1962
1963 // First find the property set in the module that corresponds to the requested
1964 // one. m.archProperties[i] corresponds to m.generalProperties[i].
1965 for i, generalProp := range m.generalProperties {
1966 srcType := reflect.ValueOf(generalProp).Type()
1967 if srcType == dstType {
1968 archProperties = m.archProperties[i]
Liz Kammer135bf552021-08-11 10:46:06 -04001969 axisToProps[bazel.NoConfigAxis] = ArchVariantProperties{"": generalProp}
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001970 break
1971 }
1972 }
1973
1974 if archProperties == nil {
1975 // This module does not have the property set requested
Liz Kammer9abd62d2021-05-21 08:37:59 -04001976 return axisToProps
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001977 }
1978
Liz Kammer9abd62d2021-05-21 08:37:59 -04001979 archToProp := ArchVariantProperties{}
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001980 // For each arch type (x86, arm64, etc.)
Chris Parsonsc424b762021-04-29 18:06:50 -04001981 for _, arch := range ArchTypeList() {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001982 // Arch properties are sometimes sharded (see createArchPropTypeDesc() ).
1983 // Iterate over ever shard and extract a struct with the same type as the
1984 // input one that contains the data specific to that arch.
1985 propertyStructs := make([]reflect.Value, 0)
1986 for _, archProperty := range archProperties {
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001987 archTypeStruct, ok := getArchTypeStruct(ctx, archProperty, arch)
1988 if ok {
1989 propertyStructs = append(propertyStructs, archTypeStruct)
1990 }
1991 multilibStruct, ok := getMultilibStruct(ctx, archProperty, arch)
1992 if ok {
1993 propertyStructs = append(propertyStructs, multilibStruct)
1994 }
Jingwen Chen5d864492021-02-24 07:20:12 -05001995 }
1996
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001997 // Create a new instance of the requested property set
1998 value := reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
1999
2000 // Merge all the structs together
2001 for _, propertyStruct := range propertyStructs {
2002 mergePropertyStruct(ctx, value, propertyStruct)
2003 }
2004
Liz Kammer9abd62d2021-05-21 08:37:59 -04002005 archToProp[arch.Name] = value
Jingwen Chen5d864492021-02-24 07:20:12 -05002006 }
Liz Kammer9abd62d2021-05-21 08:37:59 -04002007 axisToProps[bazel.ArchConfigurationAxis] = archToProp
Lukacs T. Berki598dd002021-05-05 09:00:01 +02002008
Liz Kammer9abd62d2021-05-21 08:37:59 -04002009 osToProp := ArchVariantProperties{}
2010 archOsToProp := ArchVariantProperties{}
2011 // For android, linux, ...
2012 for _, os := range osTypeList {
2013 if os == CommonOS {
2014 // It looks like this OS value is not used in Blueprint files
2015 continue
2016 }
2017 osToProp[os.Name] = getTargetStruct(ctx, propertySet, archProperties, os.Field)
2018 // For arm, x86, ...
2019 for _, arch := range osArchTypeMap[os] {
2020 targetField := GetCompoundTargetField(os, arch)
2021 targetName := fmt.Sprintf("%s_%s", os.Name, arch.Name)
2022 archOsToProp[targetName] = getTargetStruct(ctx, propertySet, archProperties, targetField)
2023 }
2024 }
2025 axisToProps[bazel.OsConfigurationAxis] = osToProp
2026 axisToProps[bazel.OsArchConfigurationAxis] = archOsToProp
2027
Liz Kammer01a16e82021-07-16 16:33:47 -04002028 axisToProps[bazel.BionicConfigurationAxis] = map[string]interface{}{
2029 "bionic": getTargetStruct(ctx, propertySet, archProperties, "Bionic"),
2030 }
2031
Liz Kammer9abd62d2021-05-21 08:37:59 -04002032 return axisToProps
Jingwen Chen5d864492021-02-24 07:20:12 -05002033}
Jingwen Chen91220d72021-03-24 02:18:33 -04002034
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04002035// Returns a struct matching the propertySet interface, containing properties specific to the targetName
2036// For example, given these arguments:
2037// propertySet = BaseCompilerProperties
2038// targetName = "android_arm"
2039// And given this Android.bp fragment:
2040// target:
2041// android_arm: {
2042// srcs: ["foo.c"],
2043// }
2044// android_arm64: {
2045// srcs: ["bar.c"],
2046// }
2047// }
2048// This would return a BaseCompilerProperties with BaseCompilerProperties.Srcs = ["foo.c"]
2049func getTargetStruct(ctx ArchVariantContext, propertySet interface{}, archProperties []interface{}, targetName string) interface{} {
2050 propertyStructs := make([]reflect.Value, 0)
2051 for _, archProperty := range archProperties {
2052 archPropValues := reflect.ValueOf(archProperty).Elem()
2053 targetProp := archPropValues.FieldByName("Target").Elem()
2054 targetStruct, ok := getChildPropertyStruct(ctx, targetProp, targetName, targetName)
2055 if ok {
2056 propertyStructs = append(propertyStructs, targetStruct)
2057 }
2058 }
2059
2060 // Create a new instance of the requested property set
2061 value := reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
2062
2063 // Merge all the structs together
2064 for _, propertyStruct := range propertyStructs {
2065 mergePropertyStruct(ctx, value, propertyStruct)
2066 }
2067
2068 return value
2069}