blob: 583793efecaa4c49771e63d14199680f8ee47d7d [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 {
258 return os == Android || os == Linux || os == LinuxBionic
259}
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)
308 // Darwin is the OS for MacOS/Darwin host machines.
309 Darwin = newOsType("darwin", Host, false, X86_64)
310 // LinuxBionic is the OS for the Linux kernel plus the Bionic libc runtime, but without the
311 // rest of Android.
312 LinuxBionic = newOsType("linux_bionic", Host, false, Arm64, X86_64)
313 // Windows the OS for Windows host machines.
314 Windows = newOsType("windows", Host, true, X86, X86_64)
315 // Android is the OS for target devices that run all of Android, including the Linux kernel
316 // and the Bionic libc runtime.
317 Android = newOsType("android", Device, false, Arm, Arm64, X86, X86_64)
Colin Crossa6845402020-11-16 15:08:19 -0800318
319 // CommonOS is a pseudo OSType for a common OS variant, which is OsType agnostic and which
320 // has dependencies on all the OS variants.
321 CommonOS = newOsType("common_os", Generic, false)
Colin Crosse9fe2942020-11-10 18:12:15 -0800322
323 // CommonArch is the Arch for all modules that are os-specific but not arch specific,
324 // for example most Java modules.
325 CommonArch = Arch{ArchType: Common}
dimitry1f33e402019-03-26 12:39:31 +0100326)
327
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000328// OsTypeList returns a slice copy of the supported OsTypes.
329func OsTypeList() []OsType {
330 return append([]OsType(nil), osTypeList...)
331}
332
Colin Crossa6845402020-11-16 15:08:19 -0800333// Target specifies the OS and architecture that a module is being compiled for.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700334type Target struct {
Colin Crossa6845402020-11-16 15:08:19 -0800335 // Os the OS that the module is being compiled for (e.g. "linux_glibc", "android").
336 Os OsType
337 // Arch is the architecture that the module is being compiled for.
338 Arch Arch
339 // NativeBridge is NativeBridgeEnabled if the architecture is supported using NativeBridge
340 // (i.e. arm on x86) for this device.
341 NativeBridge NativeBridgeSupport
342 // NativeBridgeHostArchName is the name of the real architecture that is used to implement
343 // the NativeBridge architecture. For example, for arm on x86 this would be "x86".
dimitry8d6dde82019-07-11 10:23:53 +0200344 NativeBridgeHostArchName string
Colin Crossa6845402020-11-16 15:08:19 -0800345 // NativeBridgeRelativePath is the name of the subdirectory that will contain NativeBridge
346 // libraries and binaries.
dimitry8d6dde82019-07-11 10:23:53 +0200347 NativeBridgeRelativePath string
Jiyong Park1613e552020-09-14 19:43:17 +0900348
349 // HostCross is true when the target cannot run natively on the current build host.
350 // For example, linux_glibc_x86 returns true on a regular x86/i686/Linux machines, but returns false
351 // on Mac (different OS), or on 64-bit only i686/Linux machines (unsupported arch).
352 HostCross bool
Colin Crossd3ba0392015-05-07 14:11:29 -0700353}
354
Colin Crossa6845402020-11-16 15:08:19 -0800355// NativeBridgeSupport is an enum that specifies if a Target supports NativeBridge.
356type NativeBridgeSupport bool
357
358const (
359 NativeBridgeDisabled NativeBridgeSupport = false
360 NativeBridgeEnabled NativeBridgeSupport = true
361)
362
363// String returns the OS and arch variations used for the Target.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700364func (target Target) String() string {
Colin Crossa195f912019-10-16 11:07:20 -0700365 return target.OsVariation() + "_" + target.ArchVariation()
366}
367
Colin Crossa6845402020-11-16 15:08:19 -0800368// OsVariation returns the name of the variation used by the osMutator for the Target.
Colin Crossa195f912019-10-16 11:07:20 -0700369func (target Target) OsVariation() string {
370 return target.Os.String()
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700371}
372
Colin Crossa6845402020-11-16 15:08:19 -0800373// ArchVariation returns the name of the variation used by the archMutator for the Target.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700374func (target Target) ArchVariation() string {
375 var variation string
dimitry1f33e402019-03-26 12:39:31 +0100376 if target.NativeBridge {
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700377 variation = "native_bridge_"
dimitry1f33e402019-03-26 12:39:31 +0100378 }
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700379 variation += target.Arch.String()
380
Colin Crossa195f912019-10-16 11:07:20 -0700381 return variation
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700382}
383
Colin Crossa6845402020-11-16 15:08:19 -0800384// Variations returns a list of blueprint.Variations for the osMutator and archMutator for the
385// Target.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700386func (target Target) Variations() []blueprint.Variation {
387 return []blueprint.Variation{
Colin Crossa195f912019-10-16 11:07:20 -0700388 {Mutator: "os", Variation: target.OsVariation()},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700389 {Mutator: "arch", Variation: target.ArchVariation()},
390 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800391}
392
Liz Kammer4562a3b2021-04-21 18:15:34 -0400393func registerBp2buildArchPathDepsMutator(ctx RegisterMutatorsContext) {
394 ctx.BottomUp("bp2build-arch-pathdeps", bp2buildArchPathDepsMutator).Parallel()
395}
396
397// add dependencies for architecture specific properties tagged with `android:"path"`
398func bp2buildArchPathDepsMutator(ctx BottomUpMutatorContext) {
399 var module Module
400 module = ctx.Module()
401
402 m := module.base()
403 if !m.ArchSpecific() {
404 return
405 }
406
407 // addPathDepsForProps does not descend into sub structs, so we need to descend into the
408 // arch-specific properties ourselves
409 properties := []interface{}{}
410 for _, archProperties := range m.archProperties {
411 for _, archProps := range archProperties {
412 archPropValues := reflect.ValueOf(archProps).Elem()
413 // there are three "arch" variations, descend into each
414 for _, variant := range []string{"Arch", "Multilib", "Target"} {
415 // The properties are an interface, get the value (a pointer) that it points to
416 archProps := archPropValues.FieldByName(variant).Elem()
417 if archProps.IsNil() {
418 continue
419 }
420 // And then a pointer to a struct
421 archProps = archProps.Elem()
422 for i := 0; i < archProps.NumField(); i += 1 {
423 f := archProps.Field(i)
424 // If the value of the field is a struct (as opposed to a pointer to a struct) then step
425 // into the BlueprintEmbed field.
426 if f.Kind() == reflect.Struct {
427 f = f.FieldByName("BlueprintEmbed")
428 }
429 if f.IsZero() {
430 continue
431 }
432 props := f.Interface().(interface{})
433 properties = append(properties, props)
434 }
435 }
436 }
437 }
438 addPathDepsForProps(ctx, properties)
439}
440
Colin Crossa6845402020-11-16 15:08:19 -0800441// osMutator splits an arch-specific module into a variant for each OS that is enabled for the
442// module. It uses the HostOrDevice value passed to InitAndroidArchModule and the
443// device_supported and host_supported properties to determine which OsTypes are enabled for this
444// module, then searches through the Targets to determine which have enabled Targets for this
445// module.
Colin Cross617b88a2020-08-24 18:04:09 -0700446func osMutator(bpctx blueprint.BottomUpMutatorContext) {
Colin Crossa195f912019-10-16 11:07:20 -0700447 var module Module
448 var ok bool
Colin Cross617b88a2020-08-24 18:04:09 -0700449 if module, ok = bpctx.Module().(Module); !ok {
Colin Crossa6845402020-11-16 15:08:19 -0800450 // The module is not a Soong module, it is a Blueprint module.
Colin Cross617b88a2020-08-24 18:04:09 -0700451 if bootstrap.IsBootstrapModule(bpctx.Module()) {
452 // Bootstrap Go modules are always the build OS or linux bionic.
453 config := bpctx.Config().(Config)
454 osNames := []string{config.BuildOSTarget.OsVariation()}
455 for _, hostCrossTarget := range config.Targets[LinuxBionic] {
456 if hostCrossTarget.Arch.ArchType == config.BuildOSTarget.Arch.ArchType {
457 osNames = append(osNames, hostCrossTarget.OsVariation())
458 }
459 }
460 osNames = FirstUniqueStrings(osNames)
461 bpctx.CreateVariations(osNames...)
462 }
Colin Crossa195f912019-10-16 11:07:20 -0700463 return
464 }
465
Colin Cross617b88a2020-08-24 18:04:09 -0700466 // Bootstrap Go module support above requires this mutator to be a
467 // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext
468 // filters out non-Soong modules. Now that we've handled them, create a
469 // normal android.BottomUpMutatorContext.
Liz Kammer356f7d42021-01-26 09:18:53 -0500470 mctx := bottomUpMutatorContextFactory(bpctx, module, false, false)
Colin Cross617b88a2020-08-24 18:04:09 -0700471
Colin Crossa195f912019-10-16 11:07:20 -0700472 base := module.base()
473
Colin Crossa6845402020-11-16 15:08:19 -0800474 // Nothing to do for modules that are not architecture specific (e.g. a genrule).
Colin Crossa195f912019-10-16 11:07:20 -0700475 if !base.ArchSpecific() {
476 return
477 }
478
Colin Crossa6845402020-11-16 15:08:19 -0800479 // Collect a list of OSTypes supported by this module based on the HostOrDevice value
480 // passed to InitAndroidArchModule and the device_supported and host_supported properties.
Colin Crossa195f912019-10-16 11:07:20 -0700481 var moduleOSList []OsType
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000482 for _, os := range osTypeList {
Jiyong Park1613e552020-09-14 19:43:17 +0900483 for _, t := range mctx.Config().Targets[os] {
Colin Cross08d6f8f2020-11-19 02:33:19 +0000484 if base.supportsTarget(t) {
Jiyong Park1613e552020-09-14 19:43:17 +0900485 moduleOSList = append(moduleOSList, os)
486 break
Colin Crossa195f912019-10-16 11:07:20 -0700487 }
488 }
Colin Crossa195f912019-10-16 11:07:20 -0700489 }
490
Colin Crossa6845402020-11-16 15:08:19 -0800491 // If there are no supported OSes then disable the module.
Colin Crossa195f912019-10-16 11:07:20 -0700492 if len(moduleOSList) == 0 {
Inseob Kimeec88e12020-01-22 11:11:29 +0900493 base.Disable()
Colin Crossa195f912019-10-16 11:07:20 -0700494 return
495 }
496
Colin Crossa6845402020-11-16 15:08:19 -0800497 // Convert the list of supported OsTypes to the variation names.
Colin Crossa195f912019-10-16 11:07:20 -0700498 osNames := make([]string, len(moduleOSList))
Colin Crossa195f912019-10-16 11:07:20 -0700499 for i, os := range moduleOSList {
500 osNames[i] = os.String()
501 }
502
Paul Duffin1356d8c2020-02-25 19:26:33 +0000503 createCommonOSVariant := base.commonProperties.CreateCommonOSVariant
504 if createCommonOSVariant {
Colin Crossa6845402020-11-16 15:08:19 -0800505 // A CommonOS variant was requested so add it to the list of OS variants to
Paul Duffin1356d8c2020-02-25 19:26:33 +0000506 // create. It needs to be added to the end because it needs to depend on the
507 // the other variants in the list returned by CreateVariations(...) and inter
508 // variant dependencies can only be created from a later variant in that list to
509 // an earlier one. That is because variants are always processed in the order in
510 // which they are returned from CreateVariations(...).
511 osNames = append(osNames, CommonOS.Name)
512 moduleOSList = append(moduleOSList, CommonOS)
Colin Crossa195f912019-10-16 11:07:20 -0700513 }
514
Colin Crossa6845402020-11-16 15:08:19 -0800515 // Create the variations, annotate each one with which OS it was created for, and
516 // squash the appropriate OS-specific properties into the top level properties.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000517 modules := mctx.CreateVariations(osNames...)
518 for i, m := range modules {
519 m.base().commonProperties.CompileOS = moduleOSList[i]
520 m.base().setOSProperties(mctx)
521 }
522
523 if createCommonOSVariant {
524 // A CommonOS variant was requested so add dependencies from it (the last one in
525 // the list) to the OS type specific variants.
526 last := len(modules) - 1
527 commonOSVariant := modules[last]
528 commonOSVariant.base().commonProperties.CommonOSVariant = true
529 for _, module := range modules[0:last] {
530 // Ignore modules that are enabled. Note, this will only avoid adding
531 // dependencies on OsType variants that are explicitly disabled in their
532 // properties. The CommonOS variant will still depend on disabled variants
533 // if they are disabled afterwards, e.g. in archMutator if
534 if module.Enabled() {
535 mctx.AddInterVariantDependency(commonOsToOsSpecificVariantTag, commonOSVariant, module)
536 }
537 }
538 }
539}
540
Colin Crossc179ea62020-10-09 10:54:15 -0700541type archDepTag struct {
542 blueprint.BaseDependencyTag
543 name string
544}
Paul Duffin1356d8c2020-02-25 19:26:33 +0000545
Colin Crossc179ea62020-10-09 10:54:15 -0700546// Identifies the dependency from CommonOS variant to the os specific variants.
547var commonOsToOsSpecificVariantTag = archDepTag{name: "common os to os specific"}
548
Paul Duffin1356d8c2020-02-25 19:26:33 +0000549// Get the OsType specific variants for the current CommonOS variant.
550//
551// The returned list will only contain enabled OsType specific variants of the
552// module referenced in the supplied context. An empty list is returned if there
553// are no enabled variants or the supplied context is not for an CommonOS
554// variant.
555func GetOsSpecificVariantsOfCommonOSVariant(mctx BaseModuleContext) []Module {
556 var variants []Module
557 mctx.VisitDirectDeps(func(m Module) {
558 if mctx.OtherModuleDependencyTag(m) == commonOsToOsSpecificVariantTag {
559 if m.Enabled() {
560 variants = append(variants, m)
561 }
562 }
563 })
Paul Duffin1356d8c2020-02-25 19:26:33 +0000564 return variants
Colin Crossa195f912019-10-16 11:07:20 -0700565}
566
Colin Crossee0bc3b2018-10-02 22:01:37 -0700567// archMutator splits a module into a variant for each Target requested by the module. Target selection
Colin Crossa6845402020-11-16 15:08:19 -0800568// for a module is in three levels, OsClass, multilib, and then Target.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700569// OsClass selection is determined by:
570// - The HostOrDeviceSupported value passed in to InitAndroidArchModule by the module type factory, which selects
571// whether the module type can compile for host, device or both.
572// - The host_supported and device_supported properties on the module.
Roland Levillainf5b635d2019-06-05 14:42:57 +0100573// 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 -0700574// for the module, the Device OsClass is selected.
575// Within each selected OsClass, the multilib selection is determined by:
Jaewoong Jung02b2d4d2019-06-06 15:19:57 -0700576// - The compile_multilib property if it set (which may be overridden by target.android.compile_multilib or
Colin Crossee0bc3b2018-10-02 22:01:37 -0700577// target.host.compile_multilib).
578// - The default multilib passed to InitAndroidArchModule if compile_multilib was not set.
579// Valid multilib values include:
580// "both": compile for all Targets supported by the OsClass (generally x86_64 and x86, or arm64 and arm).
581// "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 -0700582// but may be arm for a 32-bit only build.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700583// "32": compile for only a single 32-bit Target supported by the OsClass.
584// "64": compile for only a single 64-bit Target supported by the OsClass.
Colin Crossa6845402020-11-16 15:08:19 -0800585// "common": compile a for a single Target that will work on all Targets supported by the OsClass (for example Java).
586// "common_first": compile a for a Target that will work on all Targets supported by the OsClass
587// (same as "common"), plus a second Target for the preferred Target supported by the OsClass
588// (same as "first"). This is used for java_binary that produces a common .jar and a wrapper
589// executable script.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700590//
591// Once the list of Targets is determined, the module is split into a variant for each Target.
592//
593// Modules can be initialized with InitAndroidMultiTargetsArchModule, in which case they will be split by OsClass,
594// 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 -0700595func archMutator(bpctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700596 var module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800597 var ok bool
Colin Cross617b88a2020-08-24 18:04:09 -0700598 if module, ok = bpctx.Module().(Module); !ok {
599 if bootstrap.IsBootstrapModule(bpctx.Module()) {
600 // Bootstrap Go modules are always the build architecture.
601 bpctx.CreateVariations(bpctx.Config().(Config).BuildOSTarget.ArchVariation())
602 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800603 return
604 }
605
Colin Cross617b88a2020-08-24 18:04:09 -0700606 // Bootstrap Go module support above requires this mutator to be a
607 // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext
608 // filters out non-Soong modules. Now that we've handled them, create a
609 // normal android.BottomUpMutatorContext.
Liz Kammer356f7d42021-01-26 09:18:53 -0500610 mctx := bottomUpMutatorContextFactory(bpctx, module, false, false)
Colin Cross617b88a2020-08-24 18:04:09 -0700611
Colin Cross5eca7cb2018-10-02 14:02:10 -0700612 base := module.base()
613
614 if !base.ArchSpecific() {
Colin Crossb9db4802016-06-03 01:50:47 +0000615 return
616 }
617
Colin Crossa195f912019-10-16 11:07:20 -0700618 os := base.commonProperties.CompileOS
Paul Duffin1356d8c2020-02-25 19:26:33 +0000619 if os == CommonOS {
620 // Make sure that the target related properties are initialized for the
621 // CommonOS variant.
622 addTargetProperties(module, commonTargetMap[os.Name], nil, true)
623
624 // Do not create arch specific variants for the CommonOS variant.
625 return
626 }
627
Colin Crossa195f912019-10-16 11:07:20 -0700628 osTargets := mctx.Config().Targets[os]
Colin Crossfb0c16e2019-11-20 17:12:35 -0800629 image := base.commonProperties.ImageVariation
Colin Crossa6845402020-11-16 15:08:19 -0800630 // Filter NativeBridge targets unless they are explicitly supported.
631 // Skip creating native bridge variants for non-core modules.
Colin Cross83bead42019-12-18 10:45:46 -0800632 if os == Android &&
633 !(Bool(base.commonProperties.Native_bridge_supported) && image == CoreVariation) {
634
Colin Crossa195f912019-10-16 11:07:20 -0700635 var targets []Target
636 for _, t := range osTargets {
637 if !t.NativeBridge {
638 targets = append(targets, t)
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700639 }
640 }
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700641
Colin Crossa195f912019-10-16 11:07:20 -0700642 osTargets = targets
643 }
Colin Crossee0bc3b2018-10-02 22:01:37 -0700644
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700645 // only the primary arch in the ramdisk / vendor_ramdisk / recovery partition
Inseob Kim08758f02021-04-08 21:13:22 +0900646 if os == Android && (module.InstallInRecovery() || module.InstallInRamdisk() || module.InstallInVendorRamdisk() || module.InstallInDebugRamdisk()) {
Colin Crossa195f912019-10-16 11:07:20 -0700647 osTargets = []Target{osTargets[0]}
648 }
dimitry1f33e402019-03-26 12:39:31 +0100649
Jaewoong Jung003d8082021-02-24 17:39:54 -0800650 // Windows builds always prefer 32-bit
651 prefer32 := os == Windows
dimitry1f33e402019-03-26 12:39:31 +0100652
Colin Crossa6845402020-11-16 15:08:19 -0800653 // Determine the multilib selection for this module.
Colin Crossa195f912019-10-16 11:07:20 -0700654 multilib, extraMultilib := decodeMultilib(base, os.Class)
Colin Crossa6845402020-11-16 15:08:19 -0800655
656 // Convert the multilib selection into a list of Targets.
Colin Crossa195f912019-10-16 11:07:20 -0700657 targets, err := decodeMultilibTargets(multilib, osTargets, prefer32)
658 if err != nil {
659 mctx.ModuleErrorf("%s", err.Error())
660 }
Colin Cross5eca7cb2018-10-02 14:02:10 -0700661
Colin Crossa6845402020-11-16 15:08:19 -0800662 // If the module is using extraMultilib, decode the extraMultilib selection into
663 // a separate list of Targets.
Colin Crossa195f912019-10-16 11:07:20 -0700664 var multiTargets []Target
665 if extraMultilib != "" {
666 multiTargets, err = decodeMultilibTargets(extraMultilib, osTargets, prefer32)
Colin Crossa1ad8d12016-06-01 17:09:44 -0700667 if err != nil {
668 mctx.ModuleErrorf("%s", err.Error())
669 }
Colin Crossb9db4802016-06-03 01:50:47 +0000670 }
671
Colin Crossa6845402020-11-16 15:08:19 -0800672 // Recovery is always the primary architecture, filter out any other architectures.
Inseob Kim20fb5d42021-02-02 20:07:58 +0900673 // Common arch is also allowed
Colin Crossfb0c16e2019-11-20 17:12:35 -0800674 if image == RecoveryVariation {
675 primaryArch := mctx.Config().DevicePrimaryArchType()
Inseob Kim20fb5d42021-02-02 20:07:58 +0900676 targets = filterToArch(targets, primaryArch, Common)
677 multiTargets = filterToArch(multiTargets, primaryArch, Common)
Colin Crossfb0c16e2019-11-20 17:12:35 -0800678 }
679
Colin Crossa6845402020-11-16 15:08:19 -0800680 // If there are no supported targets disable the module.
Colin Crossa195f912019-10-16 11:07:20 -0700681 if len(targets) == 0 {
Inseob Kimeec88e12020-01-22 11:11:29 +0900682 base.Disable()
Dan Willemsen3f32f032016-07-11 14:36:48 -0700683 return
684 }
685
Colin Crossa6845402020-11-16 15:08:19 -0800686 // Convert the targets into a list of arch variation names.
Colin Crossa195f912019-10-16 11:07:20 -0700687 targetNames := make([]string, len(targets))
Colin Crossa195f912019-10-16 11:07:20 -0700688 for i, target := range targets {
689 targetNames[i] = target.ArchVariation()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700690 }
691
Colin Crossa6845402020-11-16 15:08:19 -0800692 // Create the variations, annotate each one with which Target it was created for, and
693 // squash the appropriate arch-specific properties into the top level properties.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700694 modules := mctx.CreateVariations(targetNames...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800695 for i, m := range modules {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000696 addTargetProperties(m, targets[i], multiTargets, i == 0)
Colin Cross617b88a2020-08-24 18:04:09 -0700697 m.base().setArchProperties(mctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800698 }
699}
700
Colin Crossa6845402020-11-16 15:08:19 -0800701// addTargetProperties annotates a variant with the Target is is being compiled for, the list
702// of additional Targets it is supporting (if any), and whether it is the primary Target for
703// the module.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000704func addTargetProperties(m Module, target Target, multiTargets []Target, primaryTarget bool) {
705 m.base().commonProperties.CompileTarget = target
706 m.base().commonProperties.CompileMultiTargets = multiTargets
707 m.base().commonProperties.CompilePrimary = primaryTarget
708}
709
Colin Crossa6845402020-11-16 15:08:19 -0800710// decodeMultilib returns the appropriate compile_multilib property for the module, or the default
711// multilib from the factory's call to InitAndroidArchModule if none was set. For modules that
712// called InitAndroidMultiTargetsArchModule it always returns "common" for multilib, and returns
713// the actual multilib in extraMultilib.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700714func decodeMultilib(base *ModuleBase, class OsClass) (multilib, extraMultilib string) {
Colin Crossa6845402020-11-16 15:08:19 -0800715 // First check the "android.compile_multilib" or "host.compile_multilib" properties.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700716 switch class {
717 case Device:
718 multilib = String(base.commonProperties.Target.Android.Compile_multilib)
Jiyong Park1613e552020-09-14 19:43:17 +0900719 case Host:
Colin Crossee0bc3b2018-10-02 22:01:37 -0700720 multilib = String(base.commonProperties.Target.Host.Compile_multilib)
721 }
Colin Crossa6845402020-11-16 15:08:19 -0800722
723 // If those aren't set, try the "compile_multilib" property.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700724 if multilib == "" {
725 multilib = String(base.commonProperties.Compile_multilib)
726 }
Colin Crossa6845402020-11-16 15:08:19 -0800727
728 // If that wasn't set, use the default multilib set by the factory.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700729 if multilib == "" {
730 multilib = base.commonProperties.Default_multilib
731 }
732
733 if base.commonProperties.UseTargetVariants {
734 return multilib, ""
735 } else {
736 // For app modules a single arch variant will be created per OS class which is expected to handle all the
737 // selected arches. Return the common-type as multilib and any Android.bp provided multilib as extraMultilib
738 if multilib == base.commonProperties.Default_multilib {
739 multilib = "first"
740 }
741 return base.commonProperties.Default_multilib, multilib
742 }
743}
744
Colin Crossa6845402020-11-16 15:08:19 -0800745// filterToArch takes a list of Targets and an ArchType, and returns a modified list that contains
Inseob Kim20fb5d42021-02-02 20:07:58 +0900746// only Targets that have the specified ArchTypes.
747func filterToArch(targets []Target, archs ...ArchType) []Target {
Colin Crossfb0c16e2019-11-20 17:12:35 -0800748 for i := 0; i < len(targets); i++ {
Inseob Kim20fb5d42021-02-02 20:07:58 +0900749 found := false
750 for _, arch := range archs {
751 if targets[i].Arch.ArchType == arch {
752 found = true
753 break
754 }
755 }
756 if !found {
Colin Crossfb0c16e2019-11-20 17:12:35 -0800757 targets = append(targets[:i], targets[i+1:]...)
758 i--
759 }
760 }
761 return targets
762}
763
Colin Crossa6845402020-11-16 15:08:19 -0800764// archPropRoot is a struct type used as the top level of the arch-specific properties. It
765// contains the "arch", "multilib", and "target" property structs. It is used to split up the
766// property structs to limit how much is allocated when a single arch-specific property group is
767// used. The types are interface{} because they will hold instances of runtime-created types.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800768type archPropRoot struct {
769 Arch, Multilib, Target interface{}
770}
771
Colin Crossa6845402020-11-16 15:08:19 -0800772// archPropTypeDesc holds the runtime-created types for the property structs to instantiate to
773// create an archPropRoot property struct.
774type archPropTypeDesc struct {
775 arch, multilib, target reflect.Type
776}
777
Colin Crosscbbd13f2020-01-17 14:08:22 -0800778// createArchPropTypeDesc takes a reflect.Type that is either a struct or a pointer to a struct, and
779// returns lists of reflect.Types that contains the arch-variant properties inside structs for each
780// arch, multilib and target property.
Colin Crossa6845402020-11-16 15:08:19 -0800781//
782// This is a relatively expensive operation, so the results are cached in the global
783// archPropTypeMap. It is constructed entirely based on compile-time data, so there is no need
784// to isolate the results between multiple tests running in parallel.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800785func createArchPropTypeDesc(props reflect.Type) []archPropTypeDesc {
Colin Crossb1d8c992020-01-21 11:43:29 -0800786 // Each property struct shard will be nested many times under the runtime generated arch struct,
787 // which can hit the limit of 64kB for the name of runtime generated structs. They are nested
788 // 97 times now, which may grow in the future, plus there is some overhead for the containing
789 // type. This number may need to be reduced if too many are added, but reducing it too far
790 // could cause problems if a single deeply nested property no longer fits in the name.
791 const maxArchTypeNameSize = 500
792
Colin Crossa6845402020-11-16 15:08:19 -0800793 // Convert the type to a new set of types that contains only the arch-specific properties
794 // (those that are tagged with `android:"arch_specific"`), and sharded into multiple types
795 // to keep the runtime-generated names under the limit.
Colin Crossb1d8c992020-01-21 11:43:29 -0800796 propShards, _ := proptools.FilterPropertyStructSharded(props, maxArchTypeNameSize, filterArchStruct)
Colin Crossa6845402020-11-16 15:08:19 -0800797
798 // If the type has no arch-specific properties there is nothing to do.
Colin Crosscb988072019-01-24 14:58:11 -0800799 if len(propShards) == 0 {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700800 return nil
801 }
802
Colin Crosscbbd13f2020-01-17 14:08:22 -0800803 var ret []archPropTypeDesc
Colin Crossc17727d2018-10-24 12:42:09 -0700804 for _, props := range propShards {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700805
Colin Crossa6845402020-11-16 15:08:19 -0800806 // variantFields takes a list of variant property field names and returns a list the
807 // StructFields with the names and the type of the current shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700808 variantFields := func(names []string) []reflect.StructField {
809 ret := make([]reflect.StructField, len(names))
Dan Willemsenb1957a52016-06-23 23:44:54 -0700810
Colin Crossc17727d2018-10-24 12:42:09 -0700811 for i, name := range names {
812 ret[i].Name = name
813 ret[i].Type = props
Dan Willemsen866b5632017-09-22 12:28:24 -0700814 }
Colin Crossc17727d2018-10-24 12:42:09 -0700815
816 return ret
817 }
818
Colin Crossa6845402020-11-16 15:08:19 -0800819 // Create a type that contains the properties in this shard repeated for each
820 // architecture, architecture variant, and architecture feature.
Colin Crossc17727d2018-10-24 12:42:09 -0700821 archFields := make([]reflect.StructField, len(archTypeList))
822 for i, arch := range archTypeList {
Colin Crossa6845402020-11-16 15:08:19 -0800823 var variants []string
Colin Crossc17727d2018-10-24 12:42:09 -0700824
825 for _, archVariant := range archVariants[arch] {
826 archVariant := variantReplacer.Replace(archVariant)
827 variants = append(variants, proptools.FieldNameForProperty(archVariant))
828 }
829 for _, feature := range archFeatures[arch] {
830 feature := variantReplacer.Replace(feature)
831 variants = append(variants, proptools.FieldNameForProperty(feature))
832 }
833
Colin Crossa6845402020-11-16 15:08:19 -0800834 // Create the StructFields for each architecture variant architecture feature
835 // (e.g. "arch.arm.cortex-a53" or "arch.arm.neon").
Colin Crossc17727d2018-10-24 12:42:09 -0700836 fields := variantFields(variants)
837
Colin Crossa6845402020-11-16 15:08:19 -0800838 // Create the StructField for the architecture itself (e.g. "arch.arm"). The special
839 // "BlueprintEmbed" name is used by Blueprint to put the properties in the
840 // parent struct.
Colin Crossc17727d2018-10-24 12:42:09 -0700841 fields = append([]reflect.StructField{{
842 Name: "BlueprintEmbed",
843 Type: props,
844 Anonymous: true,
845 }}, fields...)
846
847 archFields[i] = reflect.StructField{
848 Name: arch.Field,
849 Type: reflect.StructOf(fields),
850 }
851 }
Colin Crossa6845402020-11-16 15:08:19 -0800852
853 // Create the type of the "arch" property struct for this shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700854 archType := reflect.StructOf(archFields)
855
Colin Crossa6845402020-11-16 15:08:19 -0800856 // Create the type for the "multilib" property struct for this shard, containing the
857 // "multilib.lib32" and "multilib.lib64" property structs.
Colin Crossc17727d2018-10-24 12:42:09 -0700858 multilibType := reflect.StructOf(variantFields([]string{"Lib32", "Lib64"}))
859
Colin Crossa6845402020-11-16 15:08:19 -0800860 // Start with a list of the special targets
Colin Crossc17727d2018-10-24 12:42:09 -0700861 targets := []string{
862 "Host",
863 "Android64",
864 "Android32",
865 "Bionic",
866 "Linux",
867 "Not_windows",
868 "Arm_on_x86",
869 "Arm_on_x86_64",
Victor Khimenkoc26fcf42020-05-07 22:16:33 +0200870 "Native_bridge",
Colin Crossc17727d2018-10-24 12:42:09 -0700871 }
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000872 for _, os := range osTypeList {
Colin Crossa6845402020-11-16 15:08:19 -0800873 // Add all the OSes.
Colin Crossc17727d2018-10-24 12:42:09 -0700874 targets = append(targets, os.Field)
875
Colin Crossa6845402020-11-16 15:08:19 -0800876 // Add the OS/Arch combinations, e.g. "android_arm64".
Colin Crossc17727d2018-10-24 12:42:09 -0700877 for _, archType := range osArchTypeMap[os] {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400878 targets = append(targets, GetCompoundTargetField(os, archType))
Colin Crossc17727d2018-10-24 12:42:09 -0700879
Colin Crossa6845402020-11-16 15:08:19 -0800880 // Also add the special "linux_<arch>" and "bionic_<arch>" property structs.
Colin Crossc17727d2018-10-24 12:42:09 -0700881 if os.Linux() {
882 target := "Linux_" + archType.Name
883 if !InList(target, targets) {
884 targets = append(targets, target)
885 }
886 }
887 if os.Bionic() {
888 target := "Bionic_" + archType.Name
889 if !InList(target, targets) {
890 targets = append(targets, target)
891 }
Dan Willemsen866b5632017-09-22 12:28:24 -0700892 }
893 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700894 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700895
Colin Crossa6845402020-11-16 15:08:19 -0800896 // Create the type for the "target" property struct for this shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700897 targetType := reflect.StructOf(variantFields(targets))
Colin Crosscbbd13f2020-01-17 14:08:22 -0800898
Colin Crossa6845402020-11-16 15:08:19 -0800899 // Return a descriptor of the 3 runtime-created types.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800900 ret = append(ret, archPropTypeDesc{
901 arch: reflect.PtrTo(archType),
902 multilib: reflect.PtrTo(multilibType),
903 target: reflect.PtrTo(targetType),
904 })
Colin Crossc17727d2018-10-24 12:42:09 -0700905 }
906 return ret
Dan Willemsenb1957a52016-06-23 23:44:54 -0700907}
908
Colin Crossa6845402020-11-16 15:08:19 -0800909// variantReplacer converts architecture variant or architecture feature names into names that
910// are valid for an Android.bp file.
911var variantReplacer = strings.NewReplacer("-", "_", ".", "_")
912
913// filterArchStruct returns true if the given field is an architecture specific property.
Colin Cross74449102019-09-25 11:26:40 -0700914func filterArchStruct(field reflect.StructField, prefix string) (bool, reflect.StructField) {
915 if proptools.HasTag(field, "android", "arch_variant") {
916 // The arch_variant field isn't necessary past this point
917 // Instead of wasting space, just remove it. Go also has a
918 // 16-bit limit on structure name length. The name is constructed
919 // based on the Go source representation of the structure, so
920 // the tag names count towards that length.
Colin Crossb4fecbf2020-01-21 11:38:47 -0800921
922 androidTag := field.Tag.Get("android")
923 values := strings.Split(androidTag, ",")
924
925 if string(field.Tag) != `android:"`+strings.Join(values, ",")+`"` {
926 panic(fmt.Errorf("unexpected tag format %q", field.Tag))
Colin Cross74449102019-09-25 11:26:40 -0700927 }
Liz Kammer4562a3b2021-04-21 18:15:34 -0400928 // don't delete path tag as it is needed for bp2build
Colin Crossb4fecbf2020-01-21 11:38:47 -0800929 // these tags don't need to be present in the runtime generated struct type.
Liz Kammer4562a3b2021-04-21 18:15:34 -0400930 values = RemoveListFromList(values, []string{"arch_variant", "variant_prepend"})
931 if len(values) > 0 && values[0] != "path" {
Colin Crossb4fecbf2020-01-21 11:38:47 -0800932 panic(fmt.Errorf("unknown tags %q in field %q", values, prefix+field.Name))
Liz Kammer4562a3b2021-04-21 18:15:34 -0400933 } else if len(values) == 1 {
934 field.Tag = reflect.StructTag(`android:"` + strings.Join(values, ",") + `"`)
935 } else {
936 field.Tag = ``
Colin Crossb4fecbf2020-01-21 11:38:47 -0800937 }
938
Colin Cross74449102019-09-25 11:26:40 -0700939 return true, field
940 }
941 return false, field
942}
943
Colin Crossa6845402020-11-16 15:08:19 -0800944// archPropTypeMap contains a cache of the results of createArchPropTypeDesc for each type. It is
945// shared across all Contexts, but is constructed based only on compile-time information so there
946// is no risk of contaminating one Context with data from another.
Dan Willemsenb1957a52016-06-23 23:44:54 -0700947var archPropTypeMap OncePer
948
Colin Crossa6845402020-11-16 15:08:19 -0800949// initArchModule adds the architecture-specific property structs to a Module.
950func initArchModule(m Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800951
952 base := m.base()
953
Colin Crossa6845402020-11-16 15:08:19 -0800954 // Store the original list of top level property structs
Colin Cross36242852017-06-23 15:06:31 -0700955 base.generalProperties = m.GetProperties()
Colin Cross3f40fa42015-01-30 17:27:36 -0800956
957 for _, properties := range base.generalProperties {
958 propertiesValue := reflect.ValueOf(properties)
Colin Cross62496a02016-08-08 15:49:17 -0700959 t := propertiesValue.Type()
Colin Cross3f40fa42015-01-30 17:27:36 -0800960 if propertiesValue.Kind() != reflect.Ptr {
Colin Crossca860ac2016-01-04 14:34:37 -0800961 panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
962 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800963 }
964
965 propertiesValue = propertiesValue.Elem()
966 if propertiesValue.Kind() != reflect.Struct {
Colin Crossca860ac2016-01-04 14:34:37 -0800967 panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
968 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800969 }
970
Colin Crossa6845402020-11-16 15:08:19 -0800971 // Get or create the arch-specific property struct types for this property struct type.
Colin Cross571cccf2019-02-04 11:22:08 -0800972 archPropTypes := archPropTypeMap.Once(NewCustomOnceKey(t), func() interface{} {
Colin Crosscbbd13f2020-01-17 14:08:22 -0800973 return createArchPropTypeDesc(t)
974 }).([]archPropTypeDesc)
Colin Cross3f40fa42015-01-30 17:27:36 -0800975
Colin Crossa6845402020-11-16 15:08:19 -0800976 // Instantiate one of each arch-specific property struct type and add it to the
977 // properties for the Module.
Colin Crossc17727d2018-10-24 12:42:09 -0700978 var archProperties []interface{}
979 for _, t := range archPropTypes {
Colin Crosscbbd13f2020-01-17 14:08:22 -0800980 archProperties = append(archProperties, &archPropRoot{
981 Arch: reflect.Zero(t.arch).Interface(),
982 Multilib: reflect.Zero(t.multilib).Interface(),
983 Target: reflect.Zero(t.target).Interface(),
984 })
Dan Willemsenb1957a52016-06-23 23:44:54 -0700985 }
Colin Crossc17727d2018-10-24 12:42:09 -0700986 base.archProperties = append(base.archProperties, archProperties)
987 m.AddProperties(archProperties...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800988 }
989
Colin Crossa6845402020-11-16 15:08:19 -0800990 // Update the list of properties that can be set by a defaults module or a call to
991 // AppendMatchingProperties or PrependMatchingProperties.
Colin Cross36242852017-06-23 15:06:31 -0700992 base.customizableProperties = m.GetProperties()
Colin Cross3f40fa42015-01-30 17:27:36 -0800993}
994
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200995func maybeBlueprintEmbed(src reflect.Value) reflect.Value {
Colin Crossa6845402020-11-16 15:08:19 -0800996 // If the value of the field is a struct (as opposed to a pointer to a struct) then step
997 // into the BlueprintEmbed field.
Dan Willemsenb1957a52016-06-23 23:44:54 -0700998 if src.Kind() == reflect.Struct {
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200999 return src.FieldByName("BlueprintEmbed")
1000 } else {
1001 return src
Colin Cross06a931b2015-10-28 17:23:31 -07001002 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001003}
1004
1005// Merges the property struct in srcValue into dst.
Liz Kammerb6dbc872021-05-14 15:14:40 -04001006func mergePropertyStruct(ctx ArchVariantContext, dst interface{}, srcValue reflect.Value) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001007 src := maybeBlueprintEmbed(srcValue).Interface()
Colin Cross06a931b2015-10-28 17:23:31 -07001008
Colin Crossa6845402020-11-16 15:08:19 -08001009 // order checks the `android:"variant_prepend"` tag to handle properties where the
1010 // arch-specific value needs to come before the generic value, for example for lists of
1011 // include directories.
Colin Cross6ee75b62016-05-05 15:57:15 -07001012 order := func(property string,
1013 dstField, srcField reflect.StructField,
1014 dstValue, srcValue interface{}) (proptools.Order, error) {
1015 if proptools.HasTag(dstField, "android", "variant_prepend") {
1016 return proptools.Prepend, nil
1017 } else {
1018 return proptools.Append, nil
1019 }
1020 }
1021
Colin Crossa6845402020-11-16 15:08:19 -08001022 // Squash the located property struct into the destination property struct.
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001023 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src, nil, order)
Colin Cross06a931b2015-10-28 17:23:31 -07001024 if err != nil {
1025 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
1026 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
1027 } else {
1028 panic(err)
1029 }
1030 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001031}
Colin Cross85a88972015-11-23 13:29:51 -08001032
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001033// Returns the immediate child of the input property struct that corresponds to
1034// the sub-property "field".
Liz Kammerb6dbc872021-05-14 15:14:40 -04001035func getChildPropertyStruct(ctx ArchVariantContext,
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001036 src reflect.Value, field, userFriendlyField string) (reflect.Value, bool) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001037
1038 // Step into non-nil pointers to structs in the src value.
1039 if src.Kind() == reflect.Ptr {
1040 if src.IsNil() {
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001041 return reflect.Value{}, false
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001042 }
1043 src = src.Elem()
1044 }
1045
1046 // Find the requested field in the src struct.
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001047 child := src.FieldByName(proptools.FieldNameForProperty(field))
1048 if !child.IsValid() {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001049 ctx.ModuleErrorf("field %q does not exist", userFriendlyField)
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001050 return reflect.Value{}, false
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001051 }
1052
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001053 if child.IsZero() {
1054 return reflect.Value{}, false
1055 }
1056
1057 return child, true
Colin Cross06a931b2015-10-28 17:23:31 -07001058}
1059
Colin Crossa6845402020-11-16 15:08:19 -08001060// Squash the appropriate OS-specific property structs into the matching top level property structs
1061// based on the CompileOS value that was annotated on the variant.
Colin Crossa195f912019-10-16 11:07:20 -07001062func (m *ModuleBase) setOSProperties(ctx BottomUpMutatorContext) {
1063 os := m.commonProperties.CompileOS
1064
1065 for i := range m.generalProperties {
1066 genProps := m.generalProperties[i]
1067 if m.archProperties[i] == nil {
1068 continue
1069 }
1070 for _, archProperties := range m.archProperties[i] {
1071 archPropValues := reflect.ValueOf(archProperties).Elem()
1072
Colin Crosscbbd13f2020-01-17 14:08:22 -08001073 targetProp := archPropValues.FieldByName("Target").Elem()
Colin Crossa195f912019-10-16 11:07:20 -07001074
1075 // Handle host-specific properties in the form:
1076 // target: {
1077 // host: {
1078 // key: value,
1079 // },
1080 // },
Jiyong Park1613e552020-09-14 19:43:17 +09001081 if os.Class == Host {
Colin Crossa195f912019-10-16 11:07:20 -07001082 field := "Host"
1083 prefix := "target.host"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001084 if hostProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1085 mergePropertyStruct(ctx, genProps, hostProperties)
1086 }
Colin Crossa195f912019-10-16 11:07:20 -07001087 }
1088
1089 // Handle target OS generalities of the form:
1090 // target: {
1091 // bionic: {
1092 // key: value,
1093 // },
1094 // }
1095 if os.Linux() {
1096 field := "Linux"
1097 prefix := "target.linux"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001098 if linuxProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1099 mergePropertyStruct(ctx, genProps, linuxProperties)
1100 }
Colin Crossa195f912019-10-16 11:07:20 -07001101 }
1102
1103 if os.Bionic() {
1104 field := "Bionic"
1105 prefix := "target.bionic"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001106 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1107 mergePropertyStruct(ctx, genProps, bionicProperties)
1108 }
Colin Crossa195f912019-10-16 11:07:20 -07001109 }
1110
1111 // Handle target OS properties in the form:
1112 // target: {
1113 // linux_glibc: {
1114 // key: value,
1115 // },
1116 // not_windows: {
1117 // key: value,
1118 // },
1119 // android {
1120 // key: value,
1121 // },
1122 // },
1123 field := os.Field
1124 prefix := "target." + os.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001125 if osProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1126 mergePropertyStruct(ctx, genProps, osProperties)
1127 }
Colin Crossa195f912019-10-16 11:07:20 -07001128
Jiyong Park1613e552020-09-14 19:43:17 +09001129 if os.Class == Host && os != Windows {
Colin Crossa195f912019-10-16 11:07:20 -07001130 field := "Not_windows"
1131 prefix := "target.not_windows"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001132 if notWindowsProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1133 mergePropertyStruct(ctx, genProps, notWindowsProperties)
1134 }
Colin Crossa195f912019-10-16 11:07:20 -07001135 }
1136
1137 // Handle 64-bit device properties in the form:
1138 // target {
1139 // android64 {
1140 // key: value,
1141 // },
1142 // android32 {
1143 // key: value,
1144 // },
1145 // },
1146 // WARNING: this is probably not what you want to use in your blueprints file, it selects
1147 // options for all targets on a device that supports 64-bit binaries, not just the targets
1148 // that are being compiled for 64-bit. Its expected use case is binaries like linker and
1149 // debuggerd that need to know when they are a 32-bit process running on a 64-bit device
1150 if os.Class == Device {
1151 if ctx.Config().Android64() {
1152 field := "Android64"
1153 prefix := "target.android64"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001154 if android64Properties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1155 mergePropertyStruct(ctx, genProps, android64Properties)
1156 }
Colin Crossa195f912019-10-16 11:07:20 -07001157 } else {
1158 field := "Android32"
1159 prefix := "target.android32"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001160 if android32Properties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1161 mergePropertyStruct(ctx, genProps, android32Properties)
1162 }
Colin Crossa195f912019-10-16 11:07:20 -07001163 }
1164 }
1165 }
1166 }
1167}
1168
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001169// Returns the struct containing the properties specific to the given
1170// architecture type. These look like this in Blueprint files:
1171// arch: {
1172// arm64: {
1173// key: value,
1174// },
1175// },
1176// This struct will also contain sub-structs containing to the architecture/CPU
1177// variants and features that themselves contain properties specific to those.
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001178func getArchTypeStruct(ctx ArchVariantContext, archProperties interface{}, archType ArchType) (reflect.Value, bool) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001179 archPropValues := reflect.ValueOf(archProperties).Elem()
1180 archProp := archPropValues.FieldByName("Arch").Elem()
1181 prefix := "arch." + archType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001182 return getChildPropertyStruct(ctx, archProp, archType.Name, prefix)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001183}
1184
1185// Returns the struct containing the properties specific to a given multilib
1186// value. These look like this in the Blueprint file:
1187// multilib: {
1188// lib32: {
1189// key: value,
1190// },
1191// },
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001192func getMultilibStruct(ctx ArchVariantContext, archProperties interface{}, archType ArchType) (reflect.Value, bool) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001193 archPropValues := reflect.ValueOf(archProperties).Elem()
1194 multilibProp := archPropValues.FieldByName("Multilib").Elem()
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001195 return getChildPropertyStruct(ctx, multilibProp, archType.Multilib, "multilib."+archType.Multilib)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001196}
1197
Liz Kammer9abd62d2021-05-21 08:37:59 -04001198func GetCompoundTargetField(os OsType, arch ArchType) string {
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04001199 return os.Field + "_" + arch.Name
1200}
1201
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001202// Returns the structs corresponding to the properties specific to the given
1203// architecture and OS in archProperties.
1204func getArchProperties(ctx BaseMutatorContext, archProperties interface{}, arch Arch, os OsType, nativeBridgeEnabled bool) []reflect.Value {
1205 result := make([]reflect.Value, 0)
1206 archPropValues := reflect.ValueOf(archProperties).Elem()
1207
1208 targetProp := archPropValues.FieldByName("Target").Elem()
1209
1210 archType := arch.ArchType
1211
1212 if arch.ArchType != Common {
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001213 archStruct, ok := getArchTypeStruct(ctx, archProperties, arch.ArchType)
1214 if ok {
1215 result = append(result, archStruct)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001216
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001217 // Handle arch-variant-specific properties in the form:
1218 // arch: {
1219 // arm: {
1220 // variant: {
1221 // key: value,
1222 // },
1223 // },
1224 // },
1225 v := variantReplacer.Replace(arch.ArchVariant)
1226 if v != "" {
1227 prefix := "arch." + archType.Name + "." + v
1228 if variantProperties, ok := getChildPropertyStruct(ctx, archStruct, v, prefix); ok {
1229 result = append(result, variantProperties)
1230 }
1231 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001232
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001233 // Handle cpu-variant-specific properties in the form:
1234 // arch: {
1235 // arm: {
1236 // variant: {
1237 // key: value,
1238 // },
1239 // },
1240 // },
1241 if arch.CpuVariant != arch.ArchVariant {
1242 c := variantReplacer.Replace(arch.CpuVariant)
1243 if c != "" {
1244 prefix := "arch." + archType.Name + "." + c
1245 if cpuVariantProperties, ok := getChildPropertyStruct(ctx, archStruct, c, prefix); ok {
1246 result = append(result, cpuVariantProperties)
1247 }
1248 }
1249 }
1250
1251 // Handle arch-feature-specific properties in the form:
1252 // arch: {
1253 // arm: {
1254 // feature: {
1255 // key: value,
1256 // },
1257 // },
1258 // },
1259 for _, feature := range arch.ArchFeatures {
1260 prefix := "arch." + archType.Name + "." + feature
1261 if featureProperties, ok := getChildPropertyStruct(ctx, archStruct, feature, prefix); ok {
1262 result = append(result, featureProperties)
1263 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001264 }
1265 }
1266
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001267 if multilibProperties, ok := getMultilibStruct(ctx, archProperties, archType); ok {
1268 result = append(result, multilibProperties)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001269 }
1270
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001271 // Handle combined OS-feature and arch specific properties in the form:
1272 // target: {
1273 // bionic_x86: {
1274 // key: value,
1275 // },
1276 // }
1277 if os.Linux() {
1278 field := "Linux_" + arch.ArchType.Name
1279 userFriendlyField := "target.linux_" + arch.ArchType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001280 if linuxProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1281 result = append(result, linuxProperties)
1282 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001283 }
1284
1285 if os.Bionic() {
1286 field := "Bionic_" + archType.Name
1287 userFriendlyField := "target.bionic_" + archType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001288 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1289 result = append(result, bionicProperties)
1290 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001291 }
1292
1293 // Handle combined OS and arch specific properties in the form:
1294 // target: {
1295 // linux_glibc_x86: {
1296 // key: value,
1297 // },
1298 // linux_glibc_arm: {
1299 // key: value,
1300 // },
1301 // android_arm {
1302 // key: value,
1303 // },
1304 // android_x86 {
1305 // key: value,
1306 // },
1307 // },
Liz Kammer9abd62d2021-05-21 08:37:59 -04001308 field := GetCompoundTargetField(os, archType)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001309 userFriendlyField := "target." + os.Name + "_" + archType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001310 if osArchProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1311 result = append(result, osArchProperties)
1312 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001313 }
1314
1315 // Handle arm on x86 properties in the form:
1316 // target {
1317 // arm_on_x86 {
1318 // key: value,
1319 // },
1320 // arm_on_x86_64 {
1321 // key: value,
1322 // },
1323 // },
1324 if os.Class == Device {
1325 if arch.ArchType == X86 && (hasArmAbi(arch) ||
1326 hasArmAndroidArch(ctx.Config().Targets[Android])) {
1327 field := "Arm_on_x86"
1328 userFriendlyField := "target.arm_on_x86"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001329 if armOnX86Properties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1330 result = append(result, armOnX86Properties)
1331 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001332 }
1333 if arch.ArchType == X86_64 && (hasArmAbi(arch) ||
1334 hasArmAndroidArch(ctx.Config().Targets[Android])) {
1335 field := "Arm_on_x86_64"
1336 userFriendlyField := "target.arm_on_x86_64"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001337 if armOnX8664Properties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1338 result = append(result, armOnX8664Properties)
1339 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001340 }
1341 if os == Android && nativeBridgeEnabled {
1342 userFriendlyField := "Native_bridge"
1343 prefix := "target.native_bridge"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001344 if nativeBridgeProperties, ok := getChildPropertyStruct(ctx, targetProp, userFriendlyField, prefix); ok {
1345 result = append(result, nativeBridgeProperties)
1346 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001347 }
1348 }
1349
1350 return result
1351}
1352
Colin Crossa6845402020-11-16 15:08:19 -08001353// Squash the appropriate arch-specific property structs into the matching top level property
1354// structs based on the CompileTarget value that was annotated on the variant.
Colin Cross4157e882019-06-06 16:57:04 -07001355func (m *ModuleBase) setArchProperties(ctx BottomUpMutatorContext) {
1356 arch := m.Arch()
1357 os := m.Os()
Colin Crossd3ba0392015-05-07 14:11:29 -07001358
Colin Cross4157e882019-06-06 16:57:04 -07001359 for i := range m.generalProperties {
1360 genProps := m.generalProperties[i]
1361 if m.archProperties[i] == nil {
Dan Willemsenb1957a52016-06-23 23:44:54 -07001362 continue
1363 }
Dan Willemsenb1957a52016-06-23 23:44:54 -07001364
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001365 propStructs := make([]reflect.Value, 0)
1366 for _, archProperty := range m.archProperties[i] {
1367 propStructShard := getArchProperties(ctx, archProperty, arch, os, m.Target().NativeBridge == NativeBridgeEnabled)
1368 propStructs = append(propStructs, propStructShard...)
1369 }
Dan Willemsenb1957a52016-06-23 23:44:54 -07001370
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001371 for _, propStruct := range propStructs {
1372 mergePropertyStruct(ctx, genProps, propStruct)
Colin Crossbb2e2b72016-12-08 17:23:53 -08001373 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001374 }
1375}
1376
Colin Cross0c66bc62021-07-20 09:47:41 -07001377// determineBuildOS stores the OS and architecture used for host targets used during the build into
1378// config based on the runtime OS and architecture determined by Go.
1379func determineBuildOS(config *config) {
1380 config.BuildOS = func() OsType {
1381 switch runtime.GOOS {
1382 case "linux":
1383 return Linux
1384 case "darwin":
1385 return Darwin
1386 default:
1387 panic(fmt.Sprintf("unsupported OS: %s", runtime.GOOS))
1388 }
1389 }()
1390
1391 config.BuildArch = func() ArchType {
1392 switch runtime.GOARCH {
1393 case "amd64":
1394 return X86_64
1395 default:
1396 panic(fmt.Sprintf("unsupported Arch: %s", runtime.GOARCH))
1397 }
1398 }()
1399
1400}
1401
Colin Crossa6845402020-11-16 15:08:19 -08001402// Convert the arch product variables into a list of targets for each OsType.
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001403func decodeTargetProductVariables(config *config) (map[OsType][]Target, error) {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001404 variables := config.productVariables
Dan Willemsen490fd492015-11-24 17:53:15 -08001405
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001406 targets := make(map[OsType][]Target)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001407 var targetErr error
1408
dimitry1f33e402019-03-26 12:39:31 +01001409 addTarget := func(os OsType, archName string, archVariant, cpuVariant *string, abi []string,
dimitry8d6dde82019-07-11 10:23:53 +02001410 nativeBridgeEnabled NativeBridgeSupport, nativeBridgeHostArchName *string,
1411 nativeBridgeRelativePath *string) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001412 if targetErr != nil {
1413 return
Dan Willemsen490fd492015-11-24 17:53:15 -08001414 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001415
Dan Willemsen01a3c252019-01-11 19:02:16 -08001416 arch, err := decodeArch(os, archName, archVariant, cpuVariant, abi)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001417 if err != nil {
1418 targetErr = err
1419 return
1420 }
dimitry8d6dde82019-07-11 10:23:53 +02001421 nativeBridgeRelativePathStr := String(nativeBridgeRelativePath)
1422 nativeBridgeHostArchNameStr := String(nativeBridgeHostArchName)
1423
1424 // Use guest arch as relative install path by default
1425 if nativeBridgeEnabled && nativeBridgeRelativePathStr == "" {
1426 nativeBridgeRelativePathStr = arch.ArchType.String()
1427 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001428
Jiyong Park1613e552020-09-14 19:43:17 +09001429 // A target is considered as HostCross if it's a host target which can't run natively on
1430 // the currently configured build machine (either because the OS is different or because of
1431 // the unsupported arch)
1432 hostCross := false
1433 if os.Class == Host {
1434 var osSupported bool
Colin Cross0c66bc62021-07-20 09:47:41 -07001435 if os == config.BuildOS {
Jiyong Park1613e552020-09-14 19:43:17 +09001436 osSupported = true
Colin Cross0c66bc62021-07-20 09:47:41 -07001437 } else if config.BuildOS.Linux() && os.Linux() {
Jiyong Park1613e552020-09-14 19:43:17 +09001438 // LinuxBionic and Linux are compatible
1439 osSupported = true
1440 } else {
1441 osSupported = false
1442 }
1443
1444 var archSupported bool
1445 if arch.ArchType == Common {
1446 archSupported = true
1447 } else if arch.ArchType.Name == *variables.HostArch {
1448 archSupported = true
1449 } else if variables.HostSecondaryArch != nil && arch.ArchType.Name == *variables.HostSecondaryArch {
1450 archSupported = true
1451 } else {
1452 archSupported = false
1453 }
1454 if !osSupported || !archSupported {
1455 hostCross = true
1456 }
1457 }
1458
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001459 targets[os] = append(targets[os],
Colin Crossa1ad8d12016-06-01 17:09:44 -07001460 Target{
dimitry8d6dde82019-07-11 10:23:53 +02001461 Os: os,
1462 Arch: arch,
1463 NativeBridge: nativeBridgeEnabled,
1464 NativeBridgeHostArchName: nativeBridgeHostArchNameStr,
1465 NativeBridgeRelativePath: nativeBridgeRelativePathStr,
Jiyong Park1613e552020-09-14 19:43:17 +09001466 HostCross: hostCross,
Colin Crossa1ad8d12016-06-01 17:09:44 -07001467 })
Dan Willemsen490fd492015-11-24 17:53:15 -08001468 }
1469
Colin Cross4225f652015-09-17 14:33:42 -07001470 if variables.HostArch == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001471 return nil, fmt.Errorf("No host primary architecture set")
Colin Cross4225f652015-09-17 14:33:42 -07001472 }
1473
Colin Crossa6845402020-11-16 15:08:19 -08001474 // The primary host target, which must always exist.
Colin Cross0c66bc62021-07-20 09:47:41 -07001475 addTarget(config.BuildOS, *variables.HostArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -07001476
Colin Crossa6845402020-11-16 15:08:19 -08001477 // An optional secondary host target.
Colin Crosseeabb892015-11-20 13:07:51 -08001478 if variables.HostSecondaryArch != nil && *variables.HostSecondaryArch != "" {
Colin Cross0c66bc62021-07-20 09:47:41 -07001479 addTarget(config.BuildOS, *variables.HostSecondaryArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -08001480 }
1481
Colin Crossa6845402020-11-16 15:08:19 -08001482 // Optional cross-compiled host targets, generally Windows.
Colin Crossff3ae9d2018-04-10 16:15:18 -07001483 if String(variables.CrossHost) != "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001484 crossHostOs := osByName(*variables.CrossHost)
1485 if crossHostOs == NoOsType {
1486 return nil, fmt.Errorf("Unknown cross host OS %q", *variables.CrossHost)
1487 }
1488
Colin Crossff3ae9d2018-04-10 16:15:18 -07001489 if String(variables.CrossHostArch) == "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001490 return nil, fmt.Errorf("No cross-host primary architecture set")
Dan Willemsen490fd492015-11-24 17:53:15 -08001491 }
1492
Colin Crossa6845402020-11-16 15:08:19 -08001493 // The primary cross-compiled host target.
dimitry8d6dde82019-07-11 10:23:53 +02001494 addTarget(crossHostOs, *variables.CrossHostArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -08001495
Colin Crossa6845402020-11-16 15:08:19 -08001496 // An optional secondary cross-compiled host target.
Dan Willemsen490fd492015-11-24 17:53:15 -08001497 if variables.CrossHostSecondaryArch != nil && *variables.CrossHostSecondaryArch != "" {
dimitry8d6dde82019-07-11 10:23:53 +02001498 addTarget(crossHostOs, *variables.CrossHostSecondaryArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -08001499 }
1500 }
1501
Colin Crossa6845402020-11-16 15:08:19 -08001502 // Optional device targets
Dan Willemsen3f32f032016-07-11 14:36:48 -07001503 if variables.DeviceArch != nil && *variables.DeviceArch != "" {
Colin Crossa6845402020-11-16 15:08:19 -08001504 // The primary device target.
Colin Crosscb0ac952021-07-20 13:17:15 -07001505 addTarget(Android, *variables.DeviceArch, variables.DeviceArchVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001506 variables.DeviceCpuVariant, variables.DeviceAbi, NativeBridgeDisabled, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -07001507
Colin Crossa6845402020-11-16 15:08:19 -08001508 // An optional secondary device target.
Dan Willemsen3f32f032016-07-11 14:36:48 -07001509 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" {
1510 addTarget(Android, *variables.DeviceSecondaryArch,
1511 variables.DeviceSecondaryArchVariant, variables.DeviceSecondaryCpuVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001512 variables.DeviceSecondaryAbi, NativeBridgeDisabled, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -07001513 }
dimitry1f33e402019-03-26 12:39:31 +01001514
Colin Crossa6845402020-11-16 15:08:19 -08001515 // An optional NativeBridge device target.
dimitry1f33e402019-03-26 12:39:31 +01001516 if variables.NativeBridgeArch != nil && *variables.NativeBridgeArch != "" {
1517 addTarget(Android, *variables.NativeBridgeArch,
1518 variables.NativeBridgeArchVariant, variables.NativeBridgeCpuVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001519 variables.NativeBridgeAbi, NativeBridgeEnabled, variables.DeviceArch,
1520 variables.NativeBridgeRelativePath)
dimitry1f33e402019-03-26 12:39:31 +01001521 }
1522
Colin Crossa6845402020-11-16 15:08:19 -08001523 // An optional secondary NativeBridge device target.
dimitry1f33e402019-03-26 12:39:31 +01001524 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" &&
1525 variables.NativeBridgeSecondaryArch != nil && *variables.NativeBridgeSecondaryArch != "" {
1526 addTarget(Android, *variables.NativeBridgeSecondaryArch,
1527 variables.NativeBridgeSecondaryArchVariant,
1528 variables.NativeBridgeSecondaryCpuVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001529 variables.NativeBridgeSecondaryAbi,
1530 NativeBridgeEnabled,
1531 variables.DeviceSecondaryArch,
1532 variables.NativeBridgeSecondaryRelativePath)
dimitry1f33e402019-03-26 12:39:31 +01001533 }
Colin Cross4225f652015-09-17 14:33:42 -07001534 }
1535
Colin Crossa1ad8d12016-06-01 17:09:44 -07001536 if targetErr != nil {
1537 return nil, targetErr
1538 }
1539
1540 return targets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001541}
1542
Colin Crossbb2e2b72016-12-08 17:23:53 -08001543// hasArmAbi returns true if arch has at least one arm ABI
1544func hasArmAbi(arch Arch) bool {
Jaewoong Jung3aff5782020-02-11 07:54:35 -08001545 return PrefixInList(arch.Abi, "arm")
Colin Crossbb2e2b72016-12-08 17:23:53 -08001546}
1547
dimitry628db6f2019-05-22 17:16:21 +02001548// hasArmArch returns true if targets has at least non-native_bridge arm Android arch
Colin Cross4247f0d2017-04-13 16:56:14 -07001549func hasArmAndroidArch(targets []Target) bool {
1550 for _, target := range targets {
Victor Khimenko1a31f802020-09-17 03:07:31 +02001551 if target.Os == Android && target.Arch.ArchType == Arm {
Victor Khimenko5eb8ec12018-03-21 20:30:54 +01001552 return true
1553 }
1554 }
1555 return false
1556}
1557
Colin Crossa6845402020-11-16 15:08:19 -08001558// archConfig describes a built-in configuration.
Dan Albert4098deb2016-10-19 14:04:41 -07001559type archConfig struct {
1560 arch string
1561 archVariant string
1562 cpuVariant string
1563 abi []string
1564}
1565
Dan Albertf1d14c72020-07-30 14:32:55 -07001566// getNdkAbisConfig returns the list of archConfigs that are used for bulding
1567// the API stubs and static libraries that are included in the NDK. These are
1568// built *without Neon*, because non-Neon is still supported and building these
1569// with Neon will break those users.
Dan Albert4098deb2016-10-19 14:04:41 -07001570func getNdkAbisConfig() []archConfig {
1571 return []archConfig{
Tamas Petzbca786d2021-01-20 18:56:33 +01001572 {"arm64", "armv8-a-branchprot", "", []string{"arm64-v8a"}},
Inseob Kim5219c0e2021-06-17 00:33:00 +09001573 {"arm", "armv7-a", "", []string{"armeabi-v7a"}},
Dan Albert4098deb2016-10-19 14:04:41 -07001574 {"x86_64", "", "", []string{"x86_64"}},
Inseob Kim5219c0e2021-06-17 00:33:00 +09001575 {"x86", "", "", []string{"x86"}},
Dan Albert4098deb2016-10-19 14:04:41 -07001576 }
1577}
1578
Colin Crossa6845402020-11-16 15:08:19 -08001579// getAmlAbisConfig returns a list of archConfigs for the ABIs supported by mainline modules.
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001580func getAmlAbisConfig() []archConfig {
1581 return []archConfig{
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001582 {"arm64", "armv8-a", "", []string{"arm64-v8a"}},
Inseob Kim5219c0e2021-06-17 00:33:00 +09001583 {"arm", "armv7-a-neon", "", []string{"armeabi-v7a"}},
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001584 {"x86_64", "", "", []string{"x86_64"}},
Inseob Kim5219c0e2021-06-17 00:33:00 +09001585 {"x86", "", "", []string{"x86"}},
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001586 }
1587}
1588
Colin Crossa6845402020-11-16 15:08:19 -08001589// decodeArchSettings converts a list of archConfigs into a list of Targets for the given OsType.
Dan Willemsen01a3c252019-01-11 19:02:16 -08001590func decodeArchSettings(os OsType, archConfigs []archConfig) ([]Target, error) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001591 var ret []Target
Dan Willemsen322acaf2016-01-12 23:07:05 -08001592
Dan Albert4098deb2016-10-19 14:04:41 -07001593 for _, config := range archConfigs {
Dan Willemsen01a3c252019-01-11 19:02:16 -08001594 arch, err := decodeArch(os, config.arch, &config.archVariant,
Colin Crossa74ca042019-01-31 14:31:51 -08001595 &config.cpuVariant, config.abi)
Dan Willemsen322acaf2016-01-12 23:07:05 -08001596 if err != nil {
1597 return nil, err
1598 }
Colin Cross3b19f5d2019-09-17 14:45:31 -07001599
Colin Crossa1ad8d12016-06-01 17:09:44 -07001600 ret = append(ret, Target{
1601 Os: Android,
1602 Arch: arch,
1603 })
Dan Willemsen322acaf2016-01-12 23:07:05 -08001604 }
1605
1606 return ret, nil
1607}
1608
Colin Crossa6845402020-11-16 15:08:19 -08001609// decodeArch converts a set of strings from product variables into an Arch struct.
Colin Crossa74ca042019-01-31 14:31:51 -08001610func decodeArch(os OsType, arch string, archVariant, cpuVariant *string, abi []string) (Arch, error) {
Colin Crossa6845402020-11-16 15:08:19 -08001611 // Verify the arch is valid
Colin Crosseeabb892015-11-20 13:07:51 -08001612 archType, ok := archTypeMap[arch]
1613 if !ok {
1614 return Arch{}, fmt.Errorf("unknown arch %q", arch)
1615 }
Colin Cross4225f652015-09-17 14:33:42 -07001616
Colin Crosseeabb892015-11-20 13:07:51 -08001617 a := Arch{
Colin Cross4225f652015-09-17 14:33:42 -07001618 ArchType: archType,
Colin Crossa6845402020-11-16 15:08:19 -08001619 ArchVariant: String(archVariant),
1620 CpuVariant: String(cpuVariant),
Colin Crossa74ca042019-01-31 14:31:51 -08001621 Abi: abi,
Colin Crosseeabb892015-11-20 13:07:51 -08001622 }
1623
Colin Crossa6845402020-11-16 15:08:19 -08001624 // Convert generic arch variants into the empty string.
Colin Crosseeabb892015-11-20 13:07:51 -08001625 if a.ArchVariant == a.ArchType.Name || a.ArchVariant == "generic" {
1626 a.ArchVariant = ""
1627 }
1628
Colin Crossa6845402020-11-16 15:08:19 -08001629 // Convert generic CPU variants into the empty string.
Colin Crosseeabb892015-11-20 13:07:51 -08001630 if a.CpuVariant == a.ArchType.Name || a.CpuVariant == "generic" {
1631 a.CpuVariant = ""
1632 }
1633
Colin Crossa6845402020-11-16 15:08:19 -08001634 // Filter empty ABIs out of the list.
Colin Crosseeabb892015-11-20 13:07:51 -08001635 for i := 0; i < len(a.Abi); i++ {
1636 if a.Abi[i] == "" {
1637 a.Abi = append(a.Abi[:i], a.Abi[i+1:]...)
1638 i--
1639 }
1640 }
1641
Dan Willemsen01a3c252019-01-11 19:02:16 -08001642 if a.ArchVariant == "" {
Colin Crossa6845402020-11-16 15:08:19 -08001643 // Set ArchFeatures from the default arch features.
Dan Willemsen01a3c252019-01-11 19:02:16 -08001644 if featureMap, ok := defaultArchFeatureMap[os]; ok {
1645 a.ArchFeatures = featureMap[archType]
1646 }
1647 } else {
Colin Crossa6845402020-11-16 15:08:19 -08001648 // Set ArchFeatures from the arch type.
Dan Willemsen01a3c252019-01-11 19:02:16 -08001649 if featureMap, ok := archFeatureMap[archType]; ok {
1650 a.ArchFeatures = featureMap[a.ArchVariant]
1651 }
Colin Crossc5c24ad2015-11-20 15:35:00 -08001652 }
1653
Colin Crosseeabb892015-11-20 13:07:51 -08001654 return a, nil
Colin Cross4225f652015-09-17 14:33:42 -07001655}
1656
Colin Crossa6845402020-11-16 15:08:19 -08001657// filterMultilibTargets takes a list of Targets and a multilib value and returns a new list of
1658// Targets containing only those that have the given multilib value.
Colin Cross69617d32016-09-06 10:39:07 -07001659func filterMultilibTargets(targets []Target, multilib string) []Target {
1660 var ret []Target
1661 for _, t := range targets {
1662 if t.Arch.ArchType.Multilib == multilib {
1663 ret = append(ret, t)
1664 }
1665 }
1666 return ret
1667}
1668
Colin Crossa6845402020-11-16 15:08:19 -08001669// getCommonTargets returns the set of Os specific common architecture targets for each Os in a list
1670// of targets.
Nan Zhangdb0b9a32017-02-27 10:12:13 -08001671func getCommonTargets(targets []Target) []Target {
1672 var ret []Target
1673 set := make(map[string]bool)
1674
1675 for _, t := range targets {
1676 if _, found := set[t.Os.String()]; !found {
1677 set[t.Os.String()] = true
1678 ret = append(ret, commonTargetMap[t.Os.String()])
1679 }
1680 }
1681
1682 return ret
1683}
1684
Colin Crossa6845402020-11-16 15:08:19 -08001685// firstTarget takes a list of Targets and a list of multilib values and returns a list of Targets
1686// that contains zero or one Target for each OsType, selecting the one that matches the earliest
1687// filter.
Colin Cross3dceee32018-09-06 10:19:57 -07001688func firstTarget(targets []Target, filters ...string) []Target {
Jiyong Park22101982020-09-17 19:09:58 +09001689 // find the first target from each OS
1690 var ret []Target
1691 hasHost := false
1692 set := make(map[OsType]bool)
1693
Colin Cross6b4a32d2017-12-05 13:42:45 -08001694 for _, filter := range filters {
1695 buildTargets := filterMultilibTargets(targets, filter)
Jiyong Park22101982020-09-17 19:09:58 +09001696 for _, t := range buildTargets {
1697 if _, found := set[t.Os]; !found {
1698 hasHost = hasHost || (t.Os.Class == Host)
1699 set[t.Os] = true
1700 ret = append(ret, t)
1701 }
Colin Cross6b4a32d2017-12-05 13:42:45 -08001702 }
1703 }
Jiyong Park22101982020-09-17 19:09:58 +09001704 return ret
Colin Cross6b4a32d2017-12-05 13:42:45 -08001705}
1706
Colin Crossa6845402020-11-16 15:08:19 -08001707// decodeMultilibTargets uses the module's multilib setting to select one or more targets from a
1708// list of Targets.
Colin Crossee0bc3b2018-10-02 22:01:37 -07001709func decodeMultilibTargets(multilib string, targets []Target, prefer32 bool) ([]Target, error) {
Colin Crossa6845402020-11-16 15:08:19 -08001710 var buildTargets []Target
Colin Cross6b4a32d2017-12-05 13:42:45 -08001711
Colin Cross4225f652015-09-17 14:33:42 -07001712 switch multilib {
1713 case "common":
Colin Cross6b4a32d2017-12-05 13:42:45 -08001714 buildTargets = getCommonTargets(targets)
1715 case "common_first":
1716 buildTargets = getCommonTargets(targets)
1717 if prefer32 {
Colin Cross3dceee32018-09-06 10:19:57 -07001718 buildTargets = append(buildTargets, firstTarget(targets, "lib32", "lib64")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001719 } else {
Colin Cross3dceee32018-09-06 10:19:57 -07001720 buildTargets = append(buildTargets, firstTarget(targets, "lib64", "lib32")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001721 }
Colin Cross4225f652015-09-17 14:33:42 -07001722 case "both":
Colin Cross8b74d172016-09-13 09:59:14 -07001723 if prefer32 {
1724 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1725 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1726 } else {
1727 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1728 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1729 }
Colin Cross4225f652015-09-17 14:33:42 -07001730 case "32":
Colin Cross69617d32016-09-06 10:39:07 -07001731 buildTargets = filterMultilibTargets(targets, "lib32")
Colin Cross4225f652015-09-17 14:33:42 -07001732 case "64":
Colin Cross69617d32016-09-06 10:39:07 -07001733 buildTargets = filterMultilibTargets(targets, "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001734 case "first":
1735 if prefer32 {
Colin Cross3dceee32018-09-06 10:19:57 -07001736 buildTargets = firstTarget(targets, "lib32", "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001737 } else {
Colin Cross3dceee32018-09-06 10:19:57 -07001738 buildTargets = firstTarget(targets, "lib64", "lib32")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001739 }
Victor Chang9448e8f2020-09-14 15:34:16 +01001740 case "first_prefer32":
1741 buildTargets = firstTarget(targets, "lib32", "lib64")
Colin Cross69617d32016-09-06 10:39:07 -07001742 case "prefer32":
Colin Cross3dceee32018-09-06 10:19:57 -07001743 buildTargets = filterMultilibTargets(targets, "lib32")
1744 if len(buildTargets) == 0 {
1745 buildTargets = filterMultilibTargets(targets, "lib64")
1746 }
Colin Cross4225f652015-09-17 14:33:42 -07001747 default:
Victor Chang9448e8f2020-09-14 15:34:16 +01001748 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 -07001749 multilib)
Colin Cross4225f652015-09-17 14:33:42 -07001750 }
1751
Colin Crossa1ad8d12016-06-01 17:09:44 -07001752 return buildTargets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001753}
Jingwen Chen5d864492021-02-24 07:20:12 -05001754
Chris Parsonsc424b762021-04-29 18:06:50 -04001755func (m *ModuleBase) getArchPropertySet(propertySet interface{}, archType ArchType) interface{} {
1756 archString := archType.Field
1757 for i := range m.archProperties {
1758 if m.archProperties[i] == nil {
1759 // Skip over nil properties
1760 continue
1761 }
1762
1763 // Not archProperties are usable; this function looks for properties of a very specific
1764 // form, and ignores the rest.
1765 for _, archProperty := range m.archProperties[i] {
1766 // archPropValue is a property struct, we are looking for the form:
1767 // `arch: { arm: { key: value, ... }}`
1768 archPropValue := reflect.ValueOf(archProperty).Elem()
1769
1770 // Unwrap src so that it should looks like a pointer to `arm: { key: value, ... }`
1771 src := archPropValue.FieldByName("Arch").Elem()
1772
1773 // Step into non-nil pointers to structs in the src value.
1774 if src.Kind() == reflect.Ptr {
1775 if src.IsNil() {
1776 continue
1777 }
1778 src = src.Elem()
1779 }
1780
1781 // Find the requested field (e.g. arm, x86) in the src struct.
1782 src = src.FieldByName(archString)
1783
1784 // We only care about structs.
1785 if !src.IsValid() || src.Kind() != reflect.Struct {
1786 continue
1787 }
1788
1789 // If the value of the field is a struct then step into the
1790 // BlueprintEmbed field. The special "BlueprintEmbed" name is
1791 // used by createArchPropTypeDesc to embed the arch properties
1792 // in the parent struct, so the src arch prop should be in this
1793 // field.
1794 //
1795 // See createArchPropTypeDesc for more details on how Arch-specific
1796 // module properties are processed from the nested props and written
1797 // into the module's archProperties.
1798 src = src.FieldByName("BlueprintEmbed")
1799
1800 // Clone the destination prop, since we want a unique prop struct per arch.
1801 propertySetClone := reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
1802
1803 // Copy the located property struct into the cloned destination property struct.
1804 err := proptools.ExtendMatchingProperties([]interface{}{propertySetClone}, src.Interface(), nil, proptools.OrderReplace)
1805 if err != nil {
1806 // This is fine, it just means the src struct doesn't match the type of propertySet.
1807 continue
1808 }
1809
1810 return propertySetClone
1811 }
1812 }
1813 // No property set was found specific to the given arch, so return an empty
1814 // property set.
1815 return reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
1816}
1817
1818// getMultilibPropertySet returns a property set struct matching the type of
1819// `propertySet`, containing multilib-specific module properties for the given architecture.
1820// If no multilib-specific properties exist for the given architecture, returns an empty property
1821// set matching `propertySet`'s type.
1822func (m *ModuleBase) getMultilibPropertySet(propertySet interface{}, archType ArchType) interface{} {
1823 // archType.Multilib is lowercase (for example, lib32) but property struct field is
1824 // capitalized, such as Lib32, so use strings.Title to capitalize it.
1825 multiLibString := strings.Title(archType.Multilib)
1826
1827 for i := range m.archProperties {
1828 if m.archProperties[i] == nil {
1829 // Skip over nil properties
1830 continue
1831 }
1832
1833 // Not archProperties are usable; this function looks for properties of a very specific
1834 // form, and ignores the rest.
1835 for _, archProperties := range m.archProperties[i] {
1836 // archPropValue is a property struct, we are looking for the form:
1837 // `multilib: { lib32: { key: value, ... }}`
1838 archPropValue := reflect.ValueOf(archProperties).Elem()
1839
1840 // Unwrap src so that it should looks like a pointer to `lib32: { key: value, ... }`
1841 src := archPropValue.FieldByName("Multilib").Elem()
1842
1843 // Step into non-nil pointers to structs in the src value.
1844 if src.Kind() == reflect.Ptr {
1845 if src.IsNil() {
1846 // Ignore nil pointers.
1847 continue
1848 }
1849 src = src.Elem()
1850 }
1851
1852 // Find the requested field (e.g. lib32) in the src struct.
1853 src = src.FieldByName(multiLibString)
1854
1855 // We only care about valid struct pointers.
1856 if !src.IsValid() || src.Kind() != reflect.Ptr || src.Elem().Kind() != reflect.Struct {
1857 continue
1858 }
1859
1860 // Get the zero value for the requested property set.
1861 propertySetClone := reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
1862
1863 // Copy the located property struct into the "zero" property set struct.
1864 err := proptools.ExtendMatchingProperties([]interface{}{propertySetClone}, src.Interface(), nil, proptools.OrderReplace)
1865
1866 if err != nil {
1867 // This is fine, it just means the src struct doesn't match.
1868 continue
1869 }
1870
1871 return propertySetClone
1872 }
1873 }
1874
1875 // There were no multilib properties specifically matching the given archtype.
1876 // Return zeroed value.
1877 return reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
1878}
1879
Liz Kammerb6dbc872021-05-14 15:14:40 -04001880// ArchVariantContext defines the limited context necessary to retrieve arch_variant properties.
1881type ArchVariantContext interface {
1882 ModuleErrorf(fmt string, args ...interface{})
1883 PropertyErrorf(property, fmt string, args ...interface{})
1884}
1885
Liz Kammer9abd62d2021-05-21 08:37:59 -04001886// ArchVariantProperties represents a map of arch-variant config strings to a property interface{}.
1887type ArchVariantProperties map[string]interface{}
1888
1889// ConfigurationAxisToArchVariantProperties represents a map of bazel.ConfigurationAxis to
1890// ArchVariantProperties, such that each independent arch-variant axis maps to the
1891// configs/properties for that axis.
1892type ConfigurationAxisToArchVariantProperties map[bazel.ConfigurationAxis]ArchVariantProperties
1893
1894// GetArchVariantProperties returns a ConfigurationAxisToArchVariantProperties where the
1895// arch-variant properties correspond to the values of the properties of the 'propertySet' struct
1896// that are specific to that axis/configuration. Each axis is independent, containing
1897// non-overlapping configs that correspond to the various "arch-variant" support, at this time:
1898// arches (including multilib)
1899// oses
1900// arch+os combinations
Jingwen Chen5d864492021-02-24 07:20:12 -05001901//
Liz Kammer9abd62d2021-05-21 08:37:59 -04001902// For example, passing a struct { Foo bool, Bar string } will return an interface{} that can be
1903// type asserted back into the same struct, containing the config-specific property value specified
1904// by the module if defined.
Chris Parsonsc424b762021-04-29 18:06:50 -04001905//
1906// Arch-specific properties may come from an arch stanza or a multilib stanza; properties
1907// in these stanzas are combined.
1908// For example: `arch: { x86: { Foo: ["bar"] } }, multilib: { lib32: {` Foo: ["baz"] } }`
1909// will result in `Foo: ["bar", "baz"]` being returned for architecture x86, if the given
1910// propertyset contains `Foo []string`.
Liz Kammer9abd62d2021-05-21 08:37:59 -04001911func (m *ModuleBase) GetArchVariantProperties(ctx ArchVariantContext, propertySet interface{}) ConfigurationAxisToArchVariantProperties {
Jingwen Chen5d864492021-02-24 07:20:12 -05001912 // Return value of the arch types to the prop values for that arch.
Liz Kammer9abd62d2021-05-21 08:37:59 -04001913 axisToProps := ConfigurationAxisToArchVariantProperties{}
Jingwen Chen5d864492021-02-24 07:20:12 -05001914
1915 // Nothing to do for non-arch-specific modules.
1916 if !m.ArchSpecific() {
Liz Kammer9abd62d2021-05-21 08:37:59 -04001917 return axisToProps
Jingwen Chen5d864492021-02-24 07:20:12 -05001918 }
1919
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001920 dstType := reflect.ValueOf(propertySet).Type()
1921 var archProperties []interface{}
1922
1923 // First find the property set in the module that corresponds to the requested
1924 // one. m.archProperties[i] corresponds to m.generalProperties[i].
1925 for i, generalProp := range m.generalProperties {
1926 srcType := reflect.ValueOf(generalProp).Type()
1927 if srcType == dstType {
1928 archProperties = m.archProperties[i]
1929 break
1930 }
1931 }
1932
1933 if archProperties == nil {
1934 // This module does not have the property set requested
Liz Kammer9abd62d2021-05-21 08:37:59 -04001935 return axisToProps
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001936 }
1937
Liz Kammer9abd62d2021-05-21 08:37:59 -04001938 archToProp := ArchVariantProperties{}
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001939 // For each arch type (x86, arm64, etc.)
Chris Parsonsc424b762021-04-29 18:06:50 -04001940 for _, arch := range ArchTypeList() {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001941 // Arch properties are sometimes sharded (see createArchPropTypeDesc() ).
1942 // Iterate over ever shard and extract a struct with the same type as the
1943 // input one that contains the data specific to that arch.
1944 propertyStructs := make([]reflect.Value, 0)
1945 for _, archProperty := range archProperties {
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001946 archTypeStruct, ok := getArchTypeStruct(ctx, archProperty, arch)
1947 if ok {
1948 propertyStructs = append(propertyStructs, archTypeStruct)
1949 }
1950 multilibStruct, ok := getMultilibStruct(ctx, archProperty, arch)
1951 if ok {
1952 propertyStructs = append(propertyStructs, multilibStruct)
1953 }
Jingwen Chen5d864492021-02-24 07:20:12 -05001954 }
1955
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001956 // Create a new instance of the requested property set
1957 value := reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
1958
1959 // Merge all the structs together
1960 for _, propertyStruct := range propertyStructs {
1961 mergePropertyStruct(ctx, value, propertyStruct)
1962 }
1963
Liz Kammer9abd62d2021-05-21 08:37:59 -04001964 archToProp[arch.Name] = value
Jingwen Chen5d864492021-02-24 07:20:12 -05001965 }
Liz Kammer9abd62d2021-05-21 08:37:59 -04001966 axisToProps[bazel.ArchConfigurationAxis] = archToProp
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001967
Liz Kammer9abd62d2021-05-21 08:37:59 -04001968 osToProp := ArchVariantProperties{}
1969 archOsToProp := ArchVariantProperties{}
1970 // For android, linux, ...
1971 for _, os := range osTypeList {
1972 if os == CommonOS {
1973 // It looks like this OS value is not used in Blueprint files
1974 continue
1975 }
1976 osToProp[os.Name] = getTargetStruct(ctx, propertySet, archProperties, os.Field)
1977 // For arm, x86, ...
1978 for _, arch := range osArchTypeMap[os] {
1979 targetField := GetCompoundTargetField(os, arch)
1980 targetName := fmt.Sprintf("%s_%s", os.Name, arch.Name)
1981 archOsToProp[targetName] = getTargetStruct(ctx, propertySet, archProperties, targetField)
1982 }
1983 }
1984 axisToProps[bazel.OsConfigurationAxis] = osToProp
1985 axisToProps[bazel.OsArchConfigurationAxis] = archOsToProp
1986
Liz Kammer01a16e82021-07-16 16:33:47 -04001987 axisToProps[bazel.BionicConfigurationAxis] = map[string]interface{}{
1988 "bionic": getTargetStruct(ctx, propertySet, archProperties, "Bionic"),
1989 }
1990
Liz Kammer9abd62d2021-05-21 08:37:59 -04001991 return axisToProps
Jingwen Chen5d864492021-02-24 07:20:12 -05001992}
Jingwen Chen91220d72021-03-24 02:18:33 -04001993
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04001994// Returns a struct matching the propertySet interface, containing properties specific to the targetName
1995// For example, given these arguments:
1996// propertySet = BaseCompilerProperties
1997// targetName = "android_arm"
1998// And given this Android.bp fragment:
1999// target:
2000// android_arm: {
2001// srcs: ["foo.c"],
2002// }
2003// android_arm64: {
2004// srcs: ["bar.c"],
2005// }
2006// }
2007// This would return a BaseCompilerProperties with BaseCompilerProperties.Srcs = ["foo.c"]
2008func getTargetStruct(ctx ArchVariantContext, propertySet interface{}, archProperties []interface{}, targetName string) interface{} {
2009 propertyStructs := make([]reflect.Value, 0)
2010 for _, archProperty := range archProperties {
2011 archPropValues := reflect.ValueOf(archProperty).Elem()
2012 targetProp := archPropValues.FieldByName("Target").Elem()
2013 targetStruct, ok := getChildPropertyStruct(ctx, targetProp, targetName, targetName)
2014 if ok {
2015 propertyStructs = append(propertyStructs, targetStruct)
2016 }
2017 }
2018
2019 // Create a new instance of the requested property set
2020 value := reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
2021
2022 // Merge all the structs together
2023 for _, propertyStruct := range propertyStructs {
2024 mergePropertyStruct(ctx, value, propertyStruct)
2025 }
2026
2027 return value
2028}