blob: a7a123e84df844f531a3b25b9d9e8ac2c5f9a390 [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 (
Liz Kammer9abd62d2021-05-21 08:37:59 -040018 "android/soong/bazel"
Colin Cross74ba9622019-02-11 15:11:14 -080019 "encoding"
Colin Cross3f40fa42015-01-30 17:27:36 -080020 "fmt"
21 "reflect"
22 "runtime"
23 "strings"
Colin Crossf6566ed2015-03-24 11:13:38 -070024
Colin Cross0f7d2ef2019-10-16 11:03:10 -070025 "github.com/google/blueprint"
Colin Cross617b88a2020-08-24 18:04:09 -070026 "github.com/google/blueprint/bootstrap"
Colin Crossf6566ed2015-03-24 11:13:38 -070027 "github.com/google/blueprint/proptools"
Colin Cross3f40fa42015-01-30 17:27:36 -080028)
29
Colin Cross3f40fa42015-01-30 17:27:36 -080030/*
31Example blueprints file containing all variant property groups, with comment listing what type
32of variants get properties in that group:
33
34module {
35 arch: {
36 arm: {
37 // Host or device variants with arm architecture
38 },
39 arm64: {
40 // Host or device variants with arm64 architecture
41 },
Colin Cross3f40fa42015-01-30 17:27:36 -080042 x86: {
43 // Host or device variants with x86 architecture
44 },
45 x86_64: {
46 // Host or device variants with x86_64 architecture
47 },
48 },
49 multilib: {
50 lib32: {
51 // Host or device variants for 32-bit architectures
52 },
53 lib64: {
54 // Host or device variants for 64-bit architectures
55 },
56 },
57 target: {
58 android: {
Martin Stjernholme284b482020-09-23 21:03:27 +010059 // Device variants (implies Bionic)
Colin Cross3f40fa42015-01-30 17:27:36 -080060 },
61 host: {
62 // Host variants
63 },
Martin Stjernholme284b482020-09-23 21:03:27 +010064 bionic: {
65 // Bionic (device and host) variants
66 },
67 linux_bionic: {
68 // Bionic host variants
69 },
70 linux: {
71 // Bionic (device and host) and Linux glibc variants
72 },
Dan Willemsen5746bd42017-10-02 19:42:01 -070073 linux_glibc: {
Martin Stjernholme284b482020-09-23 21:03:27 +010074 // Linux host variants (using non-Bionic libc)
Colin Cross3f40fa42015-01-30 17:27:36 -080075 },
76 darwin: {
77 // Darwin host variants
78 },
79 windows: {
80 // Windows host variants
81 },
82 not_windows: {
83 // Non-windows host variants
84 },
Martin Stjernholme284b482020-09-23 21:03:27 +010085 android_arm: {
86 // Any <os>_<arch> combination restricts to that os and arch
87 },
Colin Cross3f40fa42015-01-30 17:27:36 -080088 },
89}
90*/
Colin Cross7d5136f2015-05-11 13:39:40 -070091
Colin Cross3f40fa42015-01-30 17:27:36 -080092// An Arch indicates a single CPU architecture.
93type Arch struct {
Colin Crossa6845402020-11-16 15:08:19 -080094 // The type of the architecture (arm, arm64, x86, or x86_64).
95 ArchType ArchType
96
97 // The variant of the architecture, for example "armv7-a" or "armv7-a-neon" for arm.
98 ArchVariant string
99
100 // The variant of the CPU, for example "cortex-a53" for arm64.
101 CpuVariant string
102
103 // The list of Android app ABIs supported by the CPU architecture, for example "arm64-v8a".
104 Abi []string
105
106 // The list of arch-specific features supported by the CPU architecture, for example "neon".
Colin Crossc5c24ad2015-11-20 15:35:00 -0800107 ArchFeatures []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800108}
109
Colin Crossa6845402020-11-16 15:08:19 -0800110// String returns the Arch as a string. The value is used as the name of the variant created
111// by archMutator.
Colin Cross3f40fa42015-01-30 17:27:36 -0800112func (a Arch) String() string {
Colin Crossd3ba0392015-05-07 14:11:29 -0700113 s := a.ArchType.String()
Colin Cross3f40fa42015-01-30 17:27:36 -0800114 if a.ArchVariant != "" {
115 s += "_" + a.ArchVariant
116 }
117 if a.CpuVariant != "" {
118 s += "_" + a.CpuVariant
119 }
120 return s
121}
122
Colin Crossa6845402020-11-16 15:08:19 -0800123// ArchType is used to define the 4 supported architecture types (arm, arm64, x86, x86_64), as
124// well as the "common" architecture used for modules that support multiple architectures, for
125// example Java modules.
Colin Cross3f40fa42015-01-30 17:27:36 -0800126type ArchType struct {
Colin Crossa6845402020-11-16 15:08:19 -0800127 // Name is the name of the architecture type, "arm", "arm64", "x86", or "x86_64".
128 Name string
129
130 // Field is the name of the field used in properties that refer to the architecture, e.g. "Arm64".
131 Field string
132
133 // Multilib is either "lib32" or "lib64" for 32-bit or 64-bit architectures.
Colin Crossec193632015-07-06 17:49:43 -0700134 Multilib string
Colin Cross3f40fa42015-01-30 17:27:36 -0800135}
136
Colin Crossa6845402020-11-16 15:08:19 -0800137// String returns the name of the ArchType.
138func (a ArchType) String() string {
139 return a.Name
140}
141
142const COMMON_VARIANT = "common"
143
144var (
145 archTypeList []ArchType
146
147 Arm = newArch("arm", "lib32")
148 Arm64 = newArch("arm64", "lib64")
149 X86 = newArch("x86", "lib32")
150 X86_64 = newArch("x86_64", "lib64")
151
152 Common = ArchType{
153 Name: COMMON_VARIANT,
154 }
155)
156
157var archTypeMap = map[string]ArchType{}
158
Colin Crossec193632015-07-06 17:49:43 -0700159func newArch(name, multilib string) ArchType {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700160 archType := ArchType{
Colin Crossec193632015-07-06 17:49:43 -0700161 Name: name,
Dan Willemsenb1957a52016-06-23 23:44:54 -0700162 Field: proptools.FieldNameForProperty(name),
Colin Crossec193632015-07-06 17:49:43 -0700163 Multilib: multilib,
Colin Cross3f40fa42015-01-30 17:27:36 -0800164 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700165 archTypeList = append(archTypeList, archType)
Colin Crossa6845402020-11-16 15:08:19 -0800166 archTypeMap[name] = archType
Dan Willemsenb1957a52016-06-23 23:44:54 -0700167 return archType
Colin Cross3f40fa42015-01-30 17:27:36 -0800168}
169
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000170// ArchTypeList returns the a slice copy of the 4 supported ArchTypes for arm,
171// arm64, x86 and x86_64.
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -0700172func ArchTypeList() []ArchType {
173 return append([]ArchType(nil), archTypeList...)
174}
175
Colin Crossa6845402020-11-16 15:08:19 -0800176// MarshalText allows an ArchType to be serialized through any encoder that supports
177// encoding.TextMarshaler.
Colin Cross74ba9622019-02-11 15:11:14 -0800178func (a ArchType) MarshalText() ([]byte, error) {
Jeongik Chabec4d032021-04-15 08:55:38 +0900179 return []byte(a.String()), nil
Colin Cross74ba9622019-02-11 15:11:14 -0800180}
181
Colin Crossa6845402020-11-16 15:08:19 -0800182var _ encoding.TextMarshaler = ArchType{}
Colin Cross74ba9622019-02-11 15:11:14 -0800183
Colin Crossa6845402020-11-16 15:08:19 -0800184// UnmarshalText allows an ArchType to be deserialized through any decoder that supports
185// encoding.TextUnmarshaler.
Colin Cross74ba9622019-02-11 15:11:14 -0800186func (a *ArchType) UnmarshalText(text []byte) error {
187 if u, ok := archTypeMap[string(text)]; ok {
188 *a = u
189 return nil
190 }
191
192 return fmt.Errorf("unknown ArchType %q", text)
193}
194
Colin Crossa6845402020-11-16 15:08:19 -0800195var _ encoding.TextUnmarshaler = &ArchType{}
Colin Crossa1ad8d12016-06-01 17:09:44 -0700196
Colin Crossa6845402020-11-16 15:08:19 -0800197// OsClass is an enum that describes whether a variant of a module runs on the host, on the device,
198// or is generic.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700199type OsClass int
200
201const (
Colin Crossa6845402020-11-16 15:08:19 -0800202 // Generic is used for variants of modules that are not OS-specific.
Dan Willemsen0e2d97b2016-11-28 17:50:06 -0800203 Generic OsClass = iota
Colin Crossa6845402020-11-16 15:08:19 -0800204 // Device is used for variants of modules that run on the device.
Dan Willemsen0e2d97b2016-11-28 17:50:06 -0800205 Device
Colin Crossa6845402020-11-16 15:08:19 -0800206 // Host is used for variants of modules that run on the host.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700207 Host
Colin Crossa1ad8d12016-06-01 17:09:44 -0700208)
209
Colin Crossa6845402020-11-16 15:08:19 -0800210// String returns the OsClass as a string.
Colin Cross67a5c132017-05-09 13:45:28 -0700211func (class OsClass) String() string {
212 switch class {
213 case Generic:
214 return "generic"
215 case Device:
216 return "device"
217 case Host:
218 return "host"
Colin Cross67a5c132017-05-09 13:45:28 -0700219 default:
220 panic(fmt.Errorf("unknown class %d", class))
221 }
222}
223
Colin Crossa6845402020-11-16 15:08:19 -0800224// OsType describes an OS variant of a module.
225type OsType struct {
226 // Name is the name of the OS. It is also used as the name of the property in Android.bp
227 // files.
228 Name string
229
230 // Field is the name of the OS converted to an exported field name, i.e. with the first
231 // character capitalized.
232 Field string
233
234 // Class is the OsClass of the OS.
235 Class OsClass
236
237 // DefaultDisabled is set when the module variants for the OS should not be created unless
238 // the module explicitly requests them. This is used to limit Windows cross compilation to
239 // only modules that need it.
240 DefaultDisabled bool
241}
242
243// String returns the name of the OsType.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700244func (os OsType) String() string {
245 return os.Name
Colin Cross54c71122016-06-01 17:09:44 -0700246}
247
Colin Crossa6845402020-11-16 15:08:19 -0800248// Bionic returns true if the OS uses the Bionic libc runtime, i.e. if the OS is Android or
249// is Linux with Bionic.
Dan Willemsen866b5632017-09-22 12:28:24 -0700250func (os OsType) Bionic() bool {
251 return os == Android || os == LinuxBionic
252}
253
Colin Crossa6845402020-11-16 15:08:19 -0800254// Linux returns true if the OS uses the Linux kernel, i.e. if the OS is Android or is Linux
255// with or without the Bionic libc runtime.
Dan Willemsen866b5632017-09-22 12:28:24 -0700256func (os OsType) Linux() bool {
257 return os == Android || os == Linux || os == LinuxBionic
258}
259
Colin Crossa6845402020-11-16 15:08:19 -0800260// newOsType constructs an OsType and adds it to the global lists.
261func newOsType(name string, class OsClass, defDisabled bool, archTypes ...ArchType) OsType {
262 checkCalledFromInit()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700263 os := OsType{
264 Name: name,
Colin Crossa6845402020-11-16 15:08:19 -0800265 Field: proptools.FieldNameForProperty(name),
Colin Crossa1ad8d12016-06-01 17:09:44 -0700266 Class: class,
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800267
268 DefaultDisabled: defDisabled,
Colin Cross54c71122016-06-01 17:09:44 -0700269 }
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000270 osTypeList = append(osTypeList, os)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800271
272 if _, found := commonTargetMap[name]; found {
273 panic(fmt.Errorf("Found Os type duplicate during OsType registration: %q", name))
274 } else {
Colin Crosse9fe2942020-11-10 18:12:15 -0800275 commonTargetMap[name] = Target{Os: os, Arch: CommonArch}
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800276 }
Colin Crossa6845402020-11-16 15:08:19 -0800277 osArchTypeMap[os] = archTypes
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800278
Colin Crossa1ad8d12016-06-01 17:09:44 -0700279 return os
280}
281
Colin Crossa6845402020-11-16 15:08:19 -0800282// osByName returns the OsType that has the given name, or NoOsType if none match.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700283func osByName(name string) OsType {
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000284 for _, os := range osTypeList {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700285 if os.Name == name {
286 return os
287 }
288 }
289
290 return NoOsType
Dan Willemsen490fd492015-11-24 17:53:15 -0800291}
292
Colin Crossa6845402020-11-16 15:08:19 -0800293var (
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000294 // osTypeList contains a list of all the supported OsTypes, including ones not supported
Colin Crossa6845402020-11-16 15:08:19 -0800295 // by the current build host or the target device.
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000296 osTypeList []OsType
Colin Crossa6845402020-11-16 15:08:19 -0800297 // commonTargetMap maps names of OsTypes to the corresponding common Target, i.e. the
298 // Target with the same OsType and the common ArchType.
299 commonTargetMap = make(map[string]Target)
300 // osArchTypeMap maps OsTypes to the list of supported ArchTypes for that OS.
301 osArchTypeMap = map[OsType][]ArchType{}
302
303 // NoOsType is a placeholder for when no OS is needed.
304 NoOsType OsType
305 // Linux is the OS for the Linux kernel plus the glibc runtime.
306 Linux = newOsType("linux_glibc", Host, false, X86, X86_64)
307 // Darwin is the OS for MacOS/Darwin host machines.
308 Darwin = newOsType("darwin", Host, false, X86_64)
309 // LinuxBionic is the OS for the Linux kernel plus the Bionic libc runtime, but without the
310 // rest of Android.
311 LinuxBionic = newOsType("linux_bionic", Host, false, Arm64, X86_64)
312 // Windows the OS for Windows host machines.
313 Windows = newOsType("windows", Host, true, X86, X86_64)
314 // Android is the OS for target devices that run all of Android, including the Linux kernel
315 // and the Bionic libc runtime.
316 Android = newOsType("android", Device, false, Arm, Arm64, X86, X86_64)
317 // Fuchsia is the OS for target devices that run Fuchsia.
318 Fuchsia = newOsType("fuchsia", Device, false, Arm64, X86_64)
319
320 // CommonOS is a pseudo OSType for a common OS variant, which is OsType agnostic and which
321 // has dependencies on all the OS variants.
322 CommonOS = newOsType("common_os", Generic, false)
Colin Crosse9fe2942020-11-10 18:12:15 -0800323
324 // CommonArch is the Arch for all modules that are os-specific but not arch specific,
325 // for example most Java modules.
326 CommonArch = Arch{ArchType: Common}
dimitry1f33e402019-03-26 12:39:31 +0100327)
328
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000329// OsTypeList returns a slice copy of the supported OsTypes.
330func OsTypeList() []OsType {
331 return append([]OsType(nil), osTypeList...)
332}
333
Colin Crossa6845402020-11-16 15:08:19 -0800334// Target specifies the OS and architecture that a module is being compiled for.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700335type Target struct {
Colin Crossa6845402020-11-16 15:08:19 -0800336 // Os the OS that the module is being compiled for (e.g. "linux_glibc", "android").
337 Os OsType
338 // Arch is the architecture that the module is being compiled for.
339 Arch Arch
340 // NativeBridge is NativeBridgeEnabled if the architecture is supported using NativeBridge
341 // (i.e. arm on x86) for this device.
342 NativeBridge NativeBridgeSupport
343 // NativeBridgeHostArchName is the name of the real architecture that is used to implement
344 // the NativeBridge architecture. For example, for arm on x86 this would be "x86".
dimitry8d6dde82019-07-11 10:23:53 +0200345 NativeBridgeHostArchName string
Colin Crossa6845402020-11-16 15:08:19 -0800346 // NativeBridgeRelativePath is the name of the subdirectory that will contain NativeBridge
347 // libraries and binaries.
dimitry8d6dde82019-07-11 10:23:53 +0200348 NativeBridgeRelativePath string
Jiyong Park1613e552020-09-14 19:43:17 +0900349
350 // HostCross is true when the target cannot run natively on the current build host.
351 // For example, linux_glibc_x86 returns true on a regular x86/i686/Linux machines, but returns false
352 // on Mac (different OS), or on 64-bit only i686/Linux machines (unsupported arch).
353 HostCross bool
Colin Crossd3ba0392015-05-07 14:11:29 -0700354}
355
Colin Crossa6845402020-11-16 15:08:19 -0800356// NativeBridgeSupport is an enum that specifies if a Target supports NativeBridge.
357type NativeBridgeSupport bool
358
359const (
360 NativeBridgeDisabled NativeBridgeSupport = false
361 NativeBridgeEnabled NativeBridgeSupport = true
362)
363
364// String returns the OS and arch variations used for the Target.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700365func (target Target) String() string {
Colin Crossa195f912019-10-16 11:07:20 -0700366 return target.OsVariation() + "_" + target.ArchVariation()
367}
368
Colin Crossa6845402020-11-16 15:08:19 -0800369// OsVariation returns the name of the variation used by the osMutator for the Target.
Colin Crossa195f912019-10-16 11:07:20 -0700370func (target Target) OsVariation() string {
371 return target.Os.String()
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700372}
373
Colin Crossa6845402020-11-16 15:08:19 -0800374// ArchVariation returns the name of the variation used by the archMutator for the Target.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700375func (target Target) ArchVariation() string {
376 var variation string
dimitry1f33e402019-03-26 12:39:31 +0100377 if target.NativeBridge {
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700378 variation = "native_bridge_"
dimitry1f33e402019-03-26 12:39:31 +0100379 }
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700380 variation += target.Arch.String()
381
Colin Crossa195f912019-10-16 11:07:20 -0700382 return variation
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700383}
384
Colin Crossa6845402020-11-16 15:08:19 -0800385// Variations returns a list of blueprint.Variations for the osMutator and archMutator for the
386// Target.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700387func (target Target) Variations() []blueprint.Variation {
388 return []blueprint.Variation{
Colin Crossa195f912019-10-16 11:07:20 -0700389 {Mutator: "os", Variation: target.OsVariation()},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700390 {Mutator: "arch", Variation: target.ArchVariation()},
391 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800392}
393
Liz Kammer4562a3b2021-04-21 18:15:34 -0400394func registerBp2buildArchPathDepsMutator(ctx RegisterMutatorsContext) {
395 ctx.BottomUp("bp2build-arch-pathdeps", bp2buildArchPathDepsMutator).Parallel()
396}
397
398// add dependencies for architecture specific properties tagged with `android:"path"`
399func bp2buildArchPathDepsMutator(ctx BottomUpMutatorContext) {
400 var module Module
401 module = ctx.Module()
402
403 m := module.base()
404 if !m.ArchSpecific() {
405 return
406 }
407
408 // addPathDepsForProps does not descend into sub structs, so we need to descend into the
409 // arch-specific properties ourselves
410 properties := []interface{}{}
411 for _, archProperties := range m.archProperties {
412 for _, archProps := range archProperties {
413 archPropValues := reflect.ValueOf(archProps).Elem()
414 // there are three "arch" variations, descend into each
415 for _, variant := range []string{"Arch", "Multilib", "Target"} {
416 // The properties are an interface, get the value (a pointer) that it points to
417 archProps := archPropValues.FieldByName(variant).Elem()
418 if archProps.IsNil() {
419 continue
420 }
421 // And then a pointer to a struct
422 archProps = archProps.Elem()
423 for i := 0; i < archProps.NumField(); i += 1 {
424 f := archProps.Field(i)
425 // If the value of the field is a struct (as opposed to a pointer to a struct) then step
426 // into the BlueprintEmbed field.
427 if f.Kind() == reflect.Struct {
428 f = f.FieldByName("BlueprintEmbed")
429 }
430 if f.IsZero() {
431 continue
432 }
433 props := f.Interface().(interface{})
434 properties = append(properties, props)
435 }
436 }
437 }
438 }
439 addPathDepsForProps(ctx, properties)
440}
441
Colin Crossa6845402020-11-16 15:08:19 -0800442// osMutator splits an arch-specific module into a variant for each OS that is enabled for the
443// module. It uses the HostOrDevice value passed to InitAndroidArchModule and the
444// device_supported and host_supported properties to determine which OsTypes are enabled for this
445// module, then searches through the Targets to determine which have enabled Targets for this
446// module.
Colin Cross617b88a2020-08-24 18:04:09 -0700447func osMutator(bpctx blueprint.BottomUpMutatorContext) {
Colin Crossa195f912019-10-16 11:07:20 -0700448 var module Module
449 var ok bool
Colin Cross617b88a2020-08-24 18:04:09 -0700450 if module, ok = bpctx.Module().(Module); !ok {
Colin Crossa6845402020-11-16 15:08:19 -0800451 // The module is not a Soong module, it is a Blueprint module.
Colin Cross617b88a2020-08-24 18:04:09 -0700452 if bootstrap.IsBootstrapModule(bpctx.Module()) {
453 // Bootstrap Go modules are always the build OS or linux bionic.
454 config := bpctx.Config().(Config)
455 osNames := []string{config.BuildOSTarget.OsVariation()}
456 for _, hostCrossTarget := range config.Targets[LinuxBionic] {
457 if hostCrossTarget.Arch.ArchType == config.BuildOSTarget.Arch.ArchType {
458 osNames = append(osNames, hostCrossTarget.OsVariation())
459 }
460 }
461 osNames = FirstUniqueStrings(osNames)
462 bpctx.CreateVariations(osNames...)
463 }
Colin Crossa195f912019-10-16 11:07:20 -0700464 return
465 }
466
Colin Cross617b88a2020-08-24 18:04:09 -0700467 // Bootstrap Go module support above requires this mutator to be a
468 // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext
469 // filters out non-Soong modules. Now that we've handled them, create a
470 // normal android.BottomUpMutatorContext.
Liz Kammer356f7d42021-01-26 09:18:53 -0500471 mctx := bottomUpMutatorContextFactory(bpctx, module, false, false)
Colin Cross617b88a2020-08-24 18:04:09 -0700472
Colin Crossa195f912019-10-16 11:07:20 -0700473 base := module.base()
474
Colin Crossa6845402020-11-16 15:08:19 -0800475 // Nothing to do for modules that are not architecture specific (e.g. a genrule).
Colin Crossa195f912019-10-16 11:07:20 -0700476 if !base.ArchSpecific() {
477 return
478 }
479
Colin Crossa6845402020-11-16 15:08:19 -0800480 // Collect a list of OSTypes supported by this module based on the HostOrDevice value
481 // passed to InitAndroidArchModule and the device_supported and host_supported properties.
Colin Crossa195f912019-10-16 11:07:20 -0700482 var moduleOSList []OsType
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000483 for _, os := range osTypeList {
Jiyong Park1613e552020-09-14 19:43:17 +0900484 for _, t := range mctx.Config().Targets[os] {
Colin Cross08d6f8f2020-11-19 02:33:19 +0000485 if base.supportsTarget(t) {
Jiyong Park1613e552020-09-14 19:43:17 +0900486 moduleOSList = append(moduleOSList, os)
487 break
Colin Crossa195f912019-10-16 11:07:20 -0700488 }
489 }
Colin Crossa195f912019-10-16 11:07:20 -0700490 }
491
Colin Crossa6845402020-11-16 15:08:19 -0800492 // If there are no supported OSes then disable the module.
Colin Crossa195f912019-10-16 11:07:20 -0700493 if len(moduleOSList) == 0 {
Inseob Kimeec88e12020-01-22 11:11:29 +0900494 base.Disable()
Colin Crossa195f912019-10-16 11:07:20 -0700495 return
496 }
497
Colin Crossa6845402020-11-16 15:08:19 -0800498 // Convert the list of supported OsTypes to the variation names.
Colin Crossa195f912019-10-16 11:07:20 -0700499 osNames := make([]string, len(moduleOSList))
Colin Crossa195f912019-10-16 11:07:20 -0700500 for i, os := range moduleOSList {
501 osNames[i] = os.String()
502 }
503
Paul Duffin1356d8c2020-02-25 19:26:33 +0000504 createCommonOSVariant := base.commonProperties.CreateCommonOSVariant
505 if createCommonOSVariant {
Colin Crossa6845402020-11-16 15:08:19 -0800506 // A CommonOS variant was requested so add it to the list of OS variants to
Paul Duffin1356d8c2020-02-25 19:26:33 +0000507 // create. It needs to be added to the end because it needs to depend on the
508 // the other variants in the list returned by CreateVariations(...) and inter
509 // variant dependencies can only be created from a later variant in that list to
510 // an earlier one. That is because variants are always processed in the order in
511 // which they are returned from CreateVariations(...).
512 osNames = append(osNames, CommonOS.Name)
513 moduleOSList = append(moduleOSList, CommonOS)
Colin Crossa195f912019-10-16 11:07:20 -0700514 }
515
Colin Crossa6845402020-11-16 15:08:19 -0800516 // Create the variations, annotate each one with which OS it was created for, and
517 // squash the appropriate OS-specific properties into the top level properties.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000518 modules := mctx.CreateVariations(osNames...)
519 for i, m := range modules {
520 m.base().commonProperties.CompileOS = moduleOSList[i]
521 m.base().setOSProperties(mctx)
522 }
523
524 if createCommonOSVariant {
525 // A CommonOS variant was requested so add dependencies from it (the last one in
526 // the list) to the OS type specific variants.
527 last := len(modules) - 1
528 commonOSVariant := modules[last]
529 commonOSVariant.base().commonProperties.CommonOSVariant = true
530 for _, module := range modules[0:last] {
531 // Ignore modules that are enabled. Note, this will only avoid adding
532 // dependencies on OsType variants that are explicitly disabled in their
533 // properties. The CommonOS variant will still depend on disabled variants
534 // if they are disabled afterwards, e.g. in archMutator if
535 if module.Enabled() {
536 mctx.AddInterVariantDependency(commonOsToOsSpecificVariantTag, commonOSVariant, module)
537 }
538 }
539 }
540}
541
Colin Crossc179ea62020-10-09 10:54:15 -0700542type archDepTag struct {
543 blueprint.BaseDependencyTag
544 name string
545}
Paul Duffin1356d8c2020-02-25 19:26:33 +0000546
Colin Crossc179ea62020-10-09 10:54:15 -0700547// Identifies the dependency from CommonOS variant to the os specific variants.
548var commonOsToOsSpecificVariantTag = archDepTag{name: "common os to os specific"}
549
Paul Duffin1356d8c2020-02-25 19:26:33 +0000550// Get the OsType specific variants for the current CommonOS variant.
551//
552// The returned list will only contain enabled OsType specific variants of the
553// module referenced in the supplied context. An empty list is returned if there
554// are no enabled variants or the supplied context is not for an CommonOS
555// variant.
556func GetOsSpecificVariantsOfCommonOSVariant(mctx BaseModuleContext) []Module {
557 var variants []Module
558 mctx.VisitDirectDeps(func(m Module) {
559 if mctx.OtherModuleDependencyTag(m) == commonOsToOsSpecificVariantTag {
560 if m.Enabled() {
561 variants = append(variants, m)
562 }
563 }
564 })
Paul Duffin1356d8c2020-02-25 19:26:33 +0000565 return variants
Colin Crossa195f912019-10-16 11:07:20 -0700566}
567
Colin Crossee0bc3b2018-10-02 22:01:37 -0700568// archMutator splits a module into a variant for each Target requested by the module. Target selection
Colin Crossa6845402020-11-16 15:08:19 -0800569// for a module is in three levels, OsClass, multilib, and then Target.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700570// OsClass selection is determined by:
571// - The HostOrDeviceSupported value passed in to InitAndroidArchModule by the module type factory, which selects
572// whether the module type can compile for host, device or both.
573// - The host_supported and device_supported properties on the module.
Roland Levillainf5b635d2019-06-05 14:42:57 +0100574// 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 -0700575// for the module, the Device OsClass is selected.
576// Within each selected OsClass, the multilib selection is determined by:
Jaewoong Jung02b2d4d2019-06-06 15:19:57 -0700577// - The compile_multilib property if it set (which may be overridden by target.android.compile_multilib or
Colin Crossee0bc3b2018-10-02 22:01:37 -0700578// target.host.compile_multilib).
579// - The default multilib passed to InitAndroidArchModule if compile_multilib was not set.
580// Valid multilib values include:
581// "both": compile for all Targets supported by the OsClass (generally x86_64 and x86, or arm64 and arm).
582// "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 -0700583// but may be arm for a 32-bit only build.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700584// "32": compile for only a single 32-bit Target supported by the OsClass.
585// "64": compile for only a single 64-bit Target supported by the OsClass.
Colin Crossa6845402020-11-16 15:08:19 -0800586// "common": compile a for a single Target that will work on all Targets supported by the OsClass (for example Java).
587// "common_first": compile a for a Target that will work on all Targets supported by the OsClass
588// (same as "common"), plus a second Target for the preferred Target supported by the OsClass
589// (same as "first"). This is used for java_binary that produces a common .jar and a wrapper
590// executable script.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700591//
592// Once the list of Targets is determined, the module is split into a variant for each Target.
593//
594// Modules can be initialized with InitAndroidMultiTargetsArchModule, in which case they will be split by OsClass,
595// 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 -0700596func archMutator(bpctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700597 var module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800598 var ok bool
Colin Cross617b88a2020-08-24 18:04:09 -0700599 if module, ok = bpctx.Module().(Module); !ok {
600 if bootstrap.IsBootstrapModule(bpctx.Module()) {
601 // Bootstrap Go modules are always the build architecture.
602 bpctx.CreateVariations(bpctx.Config().(Config).BuildOSTarget.ArchVariation())
603 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800604 return
605 }
606
Colin Cross617b88a2020-08-24 18:04:09 -0700607 // Bootstrap Go module support above requires this mutator to be a
608 // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext
609 // filters out non-Soong modules. Now that we've handled them, create a
610 // normal android.BottomUpMutatorContext.
Liz Kammer356f7d42021-01-26 09:18:53 -0500611 mctx := bottomUpMutatorContextFactory(bpctx, module, false, false)
Colin Cross617b88a2020-08-24 18:04:09 -0700612
Colin Cross5eca7cb2018-10-02 14:02:10 -0700613 base := module.base()
614
615 if !base.ArchSpecific() {
Colin Crossb9db4802016-06-03 01:50:47 +0000616 return
617 }
618
Colin Crossa195f912019-10-16 11:07:20 -0700619 os := base.commonProperties.CompileOS
Paul Duffin1356d8c2020-02-25 19:26:33 +0000620 if os == CommonOS {
621 // Make sure that the target related properties are initialized for the
622 // CommonOS variant.
623 addTargetProperties(module, commonTargetMap[os.Name], nil, true)
624
625 // Do not create arch specific variants for the CommonOS variant.
626 return
627 }
628
Colin Crossa195f912019-10-16 11:07:20 -0700629 osTargets := mctx.Config().Targets[os]
Colin Crossfb0c16e2019-11-20 17:12:35 -0800630 image := base.commonProperties.ImageVariation
Colin Crossa6845402020-11-16 15:08:19 -0800631 // Filter NativeBridge targets unless they are explicitly supported.
632 // Skip creating native bridge variants for non-core modules.
Colin Cross83bead42019-12-18 10:45:46 -0800633 if os == Android &&
634 !(Bool(base.commonProperties.Native_bridge_supported) && image == CoreVariation) {
635
Colin Crossa195f912019-10-16 11:07:20 -0700636 var targets []Target
637 for _, t := range osTargets {
638 if !t.NativeBridge {
639 targets = append(targets, t)
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700640 }
641 }
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700642
Colin Crossa195f912019-10-16 11:07:20 -0700643 osTargets = targets
644 }
Colin Crossee0bc3b2018-10-02 22:01:37 -0700645
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700646 // only the primary arch in the ramdisk / vendor_ramdisk / recovery partition
Inseob Kim08758f02021-04-08 21:13:22 +0900647 if os == Android && (module.InstallInRecovery() || module.InstallInRamdisk() || module.InstallInVendorRamdisk() || module.InstallInDebugRamdisk()) {
Colin Crossa195f912019-10-16 11:07:20 -0700648 osTargets = []Target{osTargets[0]}
649 }
dimitry1f33e402019-03-26 12:39:31 +0100650
Jaewoong Jung003d8082021-02-24 17:39:54 -0800651 // Windows builds always prefer 32-bit
652 prefer32 := os == Windows
dimitry1f33e402019-03-26 12:39:31 +0100653
Colin Crossa6845402020-11-16 15:08:19 -0800654 // Determine the multilib selection for this module.
Colin Crossa195f912019-10-16 11:07:20 -0700655 multilib, extraMultilib := decodeMultilib(base, os.Class)
Colin Crossa6845402020-11-16 15:08:19 -0800656
657 // Convert the multilib selection into a list of Targets.
Colin Crossa195f912019-10-16 11:07:20 -0700658 targets, err := decodeMultilibTargets(multilib, osTargets, prefer32)
659 if err != nil {
660 mctx.ModuleErrorf("%s", err.Error())
661 }
Colin Cross5eca7cb2018-10-02 14:02:10 -0700662
Colin Crossa6845402020-11-16 15:08:19 -0800663 // If the module is using extraMultilib, decode the extraMultilib selection into
664 // a separate list of Targets.
Colin Crossa195f912019-10-16 11:07:20 -0700665 var multiTargets []Target
666 if extraMultilib != "" {
667 multiTargets, err = decodeMultilibTargets(extraMultilib, osTargets, prefer32)
Colin Crossa1ad8d12016-06-01 17:09:44 -0700668 if err != nil {
669 mctx.ModuleErrorf("%s", err.Error())
670 }
Colin Crossb9db4802016-06-03 01:50:47 +0000671 }
672
Colin Crossa6845402020-11-16 15:08:19 -0800673 // Recovery is always the primary architecture, filter out any other architectures.
Inseob Kim20fb5d42021-02-02 20:07:58 +0900674 // Common arch is also allowed
Colin Crossfb0c16e2019-11-20 17:12:35 -0800675 if image == RecoveryVariation {
676 primaryArch := mctx.Config().DevicePrimaryArchType()
Inseob Kim20fb5d42021-02-02 20:07:58 +0900677 targets = filterToArch(targets, primaryArch, Common)
678 multiTargets = filterToArch(multiTargets, primaryArch, Common)
Colin Crossfb0c16e2019-11-20 17:12:35 -0800679 }
680
Colin Crossa6845402020-11-16 15:08:19 -0800681 // If there are no supported targets disable the module.
Colin Crossa195f912019-10-16 11:07:20 -0700682 if len(targets) == 0 {
Inseob Kimeec88e12020-01-22 11:11:29 +0900683 base.Disable()
Dan Willemsen3f32f032016-07-11 14:36:48 -0700684 return
685 }
686
Colin Crossa6845402020-11-16 15:08:19 -0800687 // Convert the targets into a list of arch variation names.
Colin Crossa195f912019-10-16 11:07:20 -0700688 targetNames := make([]string, len(targets))
Colin Crossa195f912019-10-16 11:07:20 -0700689 for i, target := range targets {
690 targetNames[i] = target.ArchVariation()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700691 }
692
Colin Crossa6845402020-11-16 15:08:19 -0800693 // Create the variations, annotate each one with which Target it was created for, and
694 // squash the appropriate arch-specific properties into the top level properties.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700695 modules := mctx.CreateVariations(targetNames...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800696 for i, m := range modules {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000697 addTargetProperties(m, targets[i], multiTargets, i == 0)
Colin Cross617b88a2020-08-24 18:04:09 -0700698 m.base().setArchProperties(mctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800699 }
700}
701
Colin Crossa6845402020-11-16 15:08:19 -0800702// addTargetProperties annotates a variant with the Target is is being compiled for, the list
703// of additional Targets it is supporting (if any), and whether it is the primary Target for
704// the module.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000705func addTargetProperties(m Module, target Target, multiTargets []Target, primaryTarget bool) {
706 m.base().commonProperties.CompileTarget = target
707 m.base().commonProperties.CompileMultiTargets = multiTargets
708 m.base().commonProperties.CompilePrimary = primaryTarget
709}
710
Colin Crossa6845402020-11-16 15:08:19 -0800711// decodeMultilib returns the appropriate compile_multilib property for the module, or the default
712// multilib from the factory's call to InitAndroidArchModule if none was set. For modules that
713// called InitAndroidMultiTargetsArchModule it always returns "common" for multilib, and returns
714// the actual multilib in extraMultilib.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700715func decodeMultilib(base *ModuleBase, class OsClass) (multilib, extraMultilib string) {
Colin Crossa6845402020-11-16 15:08:19 -0800716 // First check the "android.compile_multilib" or "host.compile_multilib" properties.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700717 switch class {
718 case Device:
719 multilib = String(base.commonProperties.Target.Android.Compile_multilib)
Jiyong Park1613e552020-09-14 19:43:17 +0900720 case Host:
Colin Crossee0bc3b2018-10-02 22:01:37 -0700721 multilib = String(base.commonProperties.Target.Host.Compile_multilib)
722 }
Colin Crossa6845402020-11-16 15:08:19 -0800723
724 // If those aren't set, try the "compile_multilib" property.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700725 if multilib == "" {
726 multilib = String(base.commonProperties.Compile_multilib)
727 }
Colin Crossa6845402020-11-16 15:08:19 -0800728
729 // If that wasn't set, use the default multilib set by the factory.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700730 if multilib == "" {
731 multilib = base.commonProperties.Default_multilib
732 }
733
734 if base.commonProperties.UseTargetVariants {
735 return multilib, ""
736 } else {
737 // For app modules a single arch variant will be created per OS class which is expected to handle all the
738 // selected arches. Return the common-type as multilib and any Android.bp provided multilib as extraMultilib
739 if multilib == base.commonProperties.Default_multilib {
740 multilib = "first"
741 }
742 return base.commonProperties.Default_multilib, multilib
743 }
744}
745
Colin Crossa6845402020-11-16 15:08:19 -0800746// filterToArch takes a list of Targets and an ArchType, and returns a modified list that contains
Inseob Kim20fb5d42021-02-02 20:07:58 +0900747// only Targets that have the specified ArchTypes.
748func filterToArch(targets []Target, archs ...ArchType) []Target {
Colin Crossfb0c16e2019-11-20 17:12:35 -0800749 for i := 0; i < len(targets); i++ {
Inseob Kim20fb5d42021-02-02 20:07:58 +0900750 found := false
751 for _, arch := range archs {
752 if targets[i].Arch.ArchType == arch {
753 found = true
754 break
755 }
756 }
757 if !found {
Colin Crossfb0c16e2019-11-20 17:12:35 -0800758 targets = append(targets[:i], targets[i+1:]...)
759 i--
760 }
761 }
762 return targets
763}
764
Colin Crossa6845402020-11-16 15:08:19 -0800765// archPropRoot is a struct type used as the top level of the arch-specific properties. It
766// contains the "arch", "multilib", and "target" property structs. It is used to split up the
767// property structs to limit how much is allocated when a single arch-specific property group is
768// used. The types are interface{} because they will hold instances of runtime-created types.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800769type archPropRoot struct {
770 Arch, Multilib, Target interface{}
771}
772
Colin Crossa6845402020-11-16 15:08:19 -0800773// archPropTypeDesc holds the runtime-created types for the property structs to instantiate to
774// create an archPropRoot property struct.
775type archPropTypeDesc struct {
776 arch, multilib, target reflect.Type
777}
778
Colin Crosscbbd13f2020-01-17 14:08:22 -0800779// createArchPropTypeDesc takes a reflect.Type that is either a struct or a pointer to a struct, and
780// returns lists of reflect.Types that contains the arch-variant properties inside structs for each
781// arch, multilib and target property.
Colin Crossa6845402020-11-16 15:08:19 -0800782//
783// This is a relatively expensive operation, so the results are cached in the global
784// archPropTypeMap. It is constructed entirely based on compile-time data, so there is no need
785// to isolate the results between multiple tests running in parallel.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800786func createArchPropTypeDesc(props reflect.Type) []archPropTypeDesc {
Colin Crossb1d8c992020-01-21 11:43:29 -0800787 // Each property struct shard will be nested many times under the runtime generated arch struct,
788 // which can hit the limit of 64kB for the name of runtime generated structs. They are nested
789 // 97 times now, which may grow in the future, plus there is some overhead for the containing
790 // type. This number may need to be reduced if too many are added, but reducing it too far
791 // could cause problems if a single deeply nested property no longer fits in the name.
792 const maxArchTypeNameSize = 500
793
Colin Crossa6845402020-11-16 15:08:19 -0800794 // Convert the type to a new set of types that contains only the arch-specific properties
795 // (those that are tagged with `android:"arch_specific"`), and sharded into multiple types
796 // to keep the runtime-generated names under the limit.
Colin Crossb1d8c992020-01-21 11:43:29 -0800797 propShards, _ := proptools.FilterPropertyStructSharded(props, maxArchTypeNameSize, filterArchStruct)
Colin Crossa6845402020-11-16 15:08:19 -0800798
799 // If the type has no arch-specific properties there is nothing to do.
Colin Crosscb988072019-01-24 14:58:11 -0800800 if len(propShards) == 0 {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700801 return nil
802 }
803
Colin Crosscbbd13f2020-01-17 14:08:22 -0800804 var ret []archPropTypeDesc
Colin Crossc17727d2018-10-24 12:42:09 -0700805 for _, props := range propShards {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700806
Colin Crossa6845402020-11-16 15:08:19 -0800807 // variantFields takes a list of variant property field names and returns a list the
808 // StructFields with the names and the type of the current shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700809 variantFields := func(names []string) []reflect.StructField {
810 ret := make([]reflect.StructField, len(names))
Dan Willemsenb1957a52016-06-23 23:44:54 -0700811
Colin Crossc17727d2018-10-24 12:42:09 -0700812 for i, name := range names {
813 ret[i].Name = name
814 ret[i].Type = props
Dan Willemsen866b5632017-09-22 12:28:24 -0700815 }
Colin Crossc17727d2018-10-24 12:42:09 -0700816
817 return ret
818 }
819
Colin Crossa6845402020-11-16 15:08:19 -0800820 // Create a type that contains the properties in this shard repeated for each
821 // architecture, architecture variant, and architecture feature.
Colin Crossc17727d2018-10-24 12:42:09 -0700822 archFields := make([]reflect.StructField, len(archTypeList))
823 for i, arch := range archTypeList {
Colin Crossa6845402020-11-16 15:08:19 -0800824 var variants []string
Colin Crossc17727d2018-10-24 12:42:09 -0700825
826 for _, archVariant := range archVariants[arch] {
827 archVariant := variantReplacer.Replace(archVariant)
828 variants = append(variants, proptools.FieldNameForProperty(archVariant))
829 }
830 for _, feature := range archFeatures[arch] {
831 feature := variantReplacer.Replace(feature)
832 variants = append(variants, proptools.FieldNameForProperty(feature))
833 }
834
Colin Crossa6845402020-11-16 15:08:19 -0800835 // Create the StructFields for each architecture variant architecture feature
836 // (e.g. "arch.arm.cortex-a53" or "arch.arm.neon").
Colin Crossc17727d2018-10-24 12:42:09 -0700837 fields := variantFields(variants)
838
Colin Crossa6845402020-11-16 15:08:19 -0800839 // Create the StructField for the architecture itself (e.g. "arch.arm"). The special
840 // "BlueprintEmbed" name is used by Blueprint to put the properties in the
841 // parent struct.
Colin Crossc17727d2018-10-24 12:42:09 -0700842 fields = append([]reflect.StructField{{
843 Name: "BlueprintEmbed",
844 Type: props,
845 Anonymous: true,
846 }}, fields...)
847
848 archFields[i] = reflect.StructField{
849 Name: arch.Field,
850 Type: reflect.StructOf(fields),
851 }
852 }
Colin Crossa6845402020-11-16 15:08:19 -0800853
854 // Create the type of the "arch" property struct for this shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700855 archType := reflect.StructOf(archFields)
856
Colin Crossa6845402020-11-16 15:08:19 -0800857 // Create the type for the "multilib" property struct for this shard, containing the
858 // "multilib.lib32" and "multilib.lib64" property structs.
Colin Crossc17727d2018-10-24 12:42:09 -0700859 multilibType := reflect.StructOf(variantFields([]string{"Lib32", "Lib64"}))
860
Colin Crossa6845402020-11-16 15:08:19 -0800861 // Start with a list of the special targets
Colin Crossc17727d2018-10-24 12:42:09 -0700862 targets := []string{
863 "Host",
864 "Android64",
865 "Android32",
866 "Bionic",
867 "Linux",
868 "Not_windows",
869 "Arm_on_x86",
870 "Arm_on_x86_64",
Victor Khimenkoc26fcf42020-05-07 22:16:33 +0200871 "Native_bridge",
Colin Crossc17727d2018-10-24 12:42:09 -0700872 }
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000873 for _, os := range osTypeList {
Colin Crossa6845402020-11-16 15:08:19 -0800874 // Add all the OSes.
Colin Crossc17727d2018-10-24 12:42:09 -0700875 targets = append(targets, os.Field)
876
Colin Crossa6845402020-11-16 15:08:19 -0800877 // Add the OS/Arch combinations, e.g. "android_arm64".
Colin Crossc17727d2018-10-24 12:42:09 -0700878 for _, archType := range osArchTypeMap[os] {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400879 targets = append(targets, GetCompoundTargetField(os, archType))
Colin Crossc17727d2018-10-24 12:42:09 -0700880
Colin Crossa6845402020-11-16 15:08:19 -0800881 // Also add the special "linux_<arch>" and "bionic_<arch>" property structs.
Colin Crossc17727d2018-10-24 12:42:09 -0700882 if os.Linux() {
883 target := "Linux_" + archType.Name
884 if !InList(target, targets) {
885 targets = append(targets, target)
886 }
887 }
888 if os.Bionic() {
889 target := "Bionic_" + archType.Name
890 if !InList(target, targets) {
891 targets = append(targets, target)
892 }
Dan Willemsen866b5632017-09-22 12:28:24 -0700893 }
894 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700895 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700896
Colin Crossa6845402020-11-16 15:08:19 -0800897 // Create the type for the "target" property struct for this shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700898 targetType := reflect.StructOf(variantFields(targets))
Colin Crosscbbd13f2020-01-17 14:08:22 -0800899
Colin Crossa6845402020-11-16 15:08:19 -0800900 // Return a descriptor of the 3 runtime-created types.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800901 ret = append(ret, archPropTypeDesc{
902 arch: reflect.PtrTo(archType),
903 multilib: reflect.PtrTo(multilibType),
904 target: reflect.PtrTo(targetType),
905 })
Colin Crossc17727d2018-10-24 12:42:09 -0700906 }
907 return ret
Dan Willemsenb1957a52016-06-23 23:44:54 -0700908}
909
Colin Crossa6845402020-11-16 15:08:19 -0800910// variantReplacer converts architecture variant or architecture feature names into names that
911// are valid for an Android.bp file.
912var variantReplacer = strings.NewReplacer("-", "_", ".", "_")
913
914// filterArchStruct returns true if the given field is an architecture specific property.
Colin Cross74449102019-09-25 11:26:40 -0700915func filterArchStruct(field reflect.StructField, prefix string) (bool, reflect.StructField) {
916 if proptools.HasTag(field, "android", "arch_variant") {
917 // The arch_variant field isn't necessary past this point
918 // Instead of wasting space, just remove it. Go also has a
919 // 16-bit limit on structure name length. The name is constructed
920 // based on the Go source representation of the structure, so
921 // the tag names count towards that length.
Colin Crossb4fecbf2020-01-21 11:38:47 -0800922
923 androidTag := field.Tag.Get("android")
924 values := strings.Split(androidTag, ",")
925
926 if string(field.Tag) != `android:"`+strings.Join(values, ",")+`"` {
927 panic(fmt.Errorf("unexpected tag format %q", field.Tag))
Colin Cross74449102019-09-25 11:26:40 -0700928 }
Liz Kammer4562a3b2021-04-21 18:15:34 -0400929 // don't delete path tag as it is needed for bp2build
Colin Crossb4fecbf2020-01-21 11:38:47 -0800930 // these tags don't need to be present in the runtime generated struct type.
Liz Kammer4562a3b2021-04-21 18:15:34 -0400931 values = RemoveListFromList(values, []string{"arch_variant", "variant_prepend"})
932 if len(values) > 0 && values[0] != "path" {
Colin Crossb4fecbf2020-01-21 11:38:47 -0800933 panic(fmt.Errorf("unknown tags %q in field %q", values, prefix+field.Name))
Liz Kammer4562a3b2021-04-21 18:15:34 -0400934 } else if len(values) == 1 {
935 field.Tag = reflect.StructTag(`android:"` + strings.Join(values, ",") + `"`)
936 } else {
937 field.Tag = ``
Colin Crossb4fecbf2020-01-21 11:38:47 -0800938 }
939
Colin Cross74449102019-09-25 11:26:40 -0700940 return true, field
941 }
942 return false, field
943}
944
Colin Crossa6845402020-11-16 15:08:19 -0800945// archPropTypeMap contains a cache of the results of createArchPropTypeDesc for each type. It is
946// shared across all Contexts, but is constructed based only on compile-time information so there
947// is no risk of contaminating one Context with data from another.
Dan Willemsenb1957a52016-06-23 23:44:54 -0700948var archPropTypeMap OncePer
949
Colin Crossa6845402020-11-16 15:08:19 -0800950// initArchModule adds the architecture-specific property structs to a Module.
951func initArchModule(m Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800952
953 base := m.base()
954
Colin Crossa6845402020-11-16 15:08:19 -0800955 // Store the original list of top level property structs
Colin Cross36242852017-06-23 15:06:31 -0700956 base.generalProperties = m.GetProperties()
Colin Cross3f40fa42015-01-30 17:27:36 -0800957
958 for _, properties := range base.generalProperties {
959 propertiesValue := reflect.ValueOf(properties)
Colin Cross62496a02016-08-08 15:49:17 -0700960 t := propertiesValue.Type()
Colin Cross3f40fa42015-01-30 17:27:36 -0800961 if propertiesValue.Kind() != reflect.Ptr {
Colin Crossca860ac2016-01-04 14:34:37 -0800962 panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
963 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800964 }
965
966 propertiesValue = propertiesValue.Elem()
967 if propertiesValue.Kind() != reflect.Struct {
Colin Crossca860ac2016-01-04 14:34:37 -0800968 panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
969 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800970 }
971
Colin Crossa6845402020-11-16 15:08:19 -0800972 // Get or create the arch-specific property struct types for this property struct type.
Colin Cross571cccf2019-02-04 11:22:08 -0800973 archPropTypes := archPropTypeMap.Once(NewCustomOnceKey(t), func() interface{} {
Colin Crosscbbd13f2020-01-17 14:08:22 -0800974 return createArchPropTypeDesc(t)
975 }).([]archPropTypeDesc)
Colin Cross3f40fa42015-01-30 17:27:36 -0800976
Colin Crossa6845402020-11-16 15:08:19 -0800977 // Instantiate one of each arch-specific property struct type and add it to the
978 // properties for the Module.
Colin Crossc17727d2018-10-24 12:42:09 -0700979 var archProperties []interface{}
980 for _, t := range archPropTypes {
Colin Crosscbbd13f2020-01-17 14:08:22 -0800981 archProperties = append(archProperties, &archPropRoot{
982 Arch: reflect.Zero(t.arch).Interface(),
983 Multilib: reflect.Zero(t.multilib).Interface(),
984 Target: reflect.Zero(t.target).Interface(),
985 })
Dan Willemsenb1957a52016-06-23 23:44:54 -0700986 }
Colin Crossc17727d2018-10-24 12:42:09 -0700987 base.archProperties = append(base.archProperties, archProperties)
988 m.AddProperties(archProperties...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800989 }
990
Colin Crossa6845402020-11-16 15:08:19 -0800991 // Update the list of properties that can be set by a defaults module or a call to
992 // AppendMatchingProperties or PrependMatchingProperties.
Colin Cross36242852017-06-23 15:06:31 -0700993 base.customizableProperties = m.GetProperties()
Colin Cross3f40fa42015-01-30 17:27:36 -0800994}
995
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200996func maybeBlueprintEmbed(src reflect.Value) reflect.Value {
Colin Crossa6845402020-11-16 15:08:19 -0800997 // If the value of the field is a struct (as opposed to a pointer to a struct) then step
998 // into the BlueprintEmbed field.
Dan Willemsenb1957a52016-06-23 23:44:54 -0700999 if src.Kind() == reflect.Struct {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001000 return src.FieldByName("BlueprintEmbed")
1001 } else {
1002 return src
Colin Cross06a931b2015-10-28 17:23:31 -07001003 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001004}
1005
1006// Merges the property struct in srcValue into dst.
Liz Kammerb6dbc872021-05-14 15:14:40 -04001007func mergePropertyStruct(ctx ArchVariantContext, dst interface{}, srcValue reflect.Value) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001008 src := maybeBlueprintEmbed(srcValue).Interface()
Colin Cross06a931b2015-10-28 17:23:31 -07001009
Colin Crossa6845402020-11-16 15:08:19 -08001010 // order checks the `android:"variant_prepend"` tag to handle properties where the
1011 // arch-specific value needs to come before the generic value, for example for lists of
1012 // include directories.
Colin Cross6ee75b62016-05-05 15:57:15 -07001013 order := func(property string,
1014 dstField, srcField reflect.StructField,
1015 dstValue, srcValue interface{}) (proptools.Order, error) {
1016 if proptools.HasTag(dstField, "android", "variant_prepend") {
1017 return proptools.Prepend, nil
1018 } else {
1019 return proptools.Append, nil
1020 }
1021 }
1022
Colin Crossa6845402020-11-16 15:08:19 -08001023 // Squash the located property struct into the destination property struct.
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001024 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src, nil, order)
Colin Cross06a931b2015-10-28 17:23:31 -07001025 if err != nil {
1026 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
1027 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
1028 } else {
1029 panic(err)
1030 }
1031 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001032}
Colin Cross85a88972015-11-23 13:29:51 -08001033
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001034// Returns the immediate child of the input property struct that corresponds to
1035// the sub-property "field".
Liz Kammerb6dbc872021-05-14 15:14:40 -04001036func getChildPropertyStruct(ctx ArchVariantContext,
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001037 src reflect.Value, field, userFriendlyField string) (reflect.Value, bool) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001038
1039 // Step into non-nil pointers to structs in the src value.
1040 if src.Kind() == reflect.Ptr {
1041 if src.IsNil() {
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001042 return reflect.Value{}, false
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001043 }
1044 src = src.Elem()
1045 }
1046
1047 // Find the requested field in the src struct.
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001048 child := src.FieldByName(proptools.FieldNameForProperty(field))
1049 if !child.IsValid() {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001050 ctx.ModuleErrorf("field %q does not exist", userFriendlyField)
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001051 return reflect.Value{}, false
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001052 }
1053
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001054 if child.IsZero() {
1055 return reflect.Value{}, false
1056 }
1057
1058 return child, true
Colin Cross06a931b2015-10-28 17:23:31 -07001059}
1060
Colin Crossa6845402020-11-16 15:08:19 -08001061// Squash the appropriate OS-specific property structs into the matching top level property structs
1062// based on the CompileOS value that was annotated on the variant.
Colin Crossa195f912019-10-16 11:07:20 -07001063func (m *ModuleBase) setOSProperties(ctx BottomUpMutatorContext) {
1064 os := m.commonProperties.CompileOS
1065
1066 for i := range m.generalProperties {
1067 genProps := m.generalProperties[i]
1068 if m.archProperties[i] == nil {
1069 continue
1070 }
1071 for _, archProperties := range m.archProperties[i] {
1072 archPropValues := reflect.ValueOf(archProperties).Elem()
1073
Colin Crosscbbd13f2020-01-17 14:08:22 -08001074 targetProp := archPropValues.FieldByName("Target").Elem()
Colin Crossa195f912019-10-16 11:07:20 -07001075
1076 // Handle host-specific properties in the form:
1077 // target: {
1078 // host: {
1079 // key: value,
1080 // },
1081 // },
Jiyong Park1613e552020-09-14 19:43:17 +09001082 if os.Class == Host {
Colin Crossa195f912019-10-16 11:07:20 -07001083 field := "Host"
1084 prefix := "target.host"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001085 if hostProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1086 mergePropertyStruct(ctx, genProps, hostProperties)
1087 }
Colin Crossa195f912019-10-16 11:07:20 -07001088 }
1089
1090 // Handle target OS generalities of the form:
1091 // target: {
1092 // bionic: {
1093 // key: value,
1094 // },
1095 // }
1096 if os.Linux() {
1097 field := "Linux"
1098 prefix := "target.linux"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001099 if linuxProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1100 mergePropertyStruct(ctx, genProps, linuxProperties)
1101 }
Colin Crossa195f912019-10-16 11:07:20 -07001102 }
1103
1104 if os.Bionic() {
1105 field := "Bionic"
1106 prefix := "target.bionic"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001107 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1108 mergePropertyStruct(ctx, genProps, bionicProperties)
1109 }
Colin Crossa195f912019-10-16 11:07:20 -07001110 }
1111
1112 // Handle target OS properties in the form:
1113 // target: {
1114 // linux_glibc: {
1115 // key: value,
1116 // },
1117 // not_windows: {
1118 // key: value,
1119 // },
1120 // android {
1121 // key: value,
1122 // },
1123 // },
1124 field := os.Field
1125 prefix := "target." + os.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001126 if osProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1127 mergePropertyStruct(ctx, genProps, osProperties)
1128 }
Colin Crossa195f912019-10-16 11:07:20 -07001129
Jiyong Park1613e552020-09-14 19:43:17 +09001130 if os.Class == Host && os != Windows {
Colin Crossa195f912019-10-16 11:07:20 -07001131 field := "Not_windows"
1132 prefix := "target.not_windows"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001133 if notWindowsProperties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1134 mergePropertyStruct(ctx, genProps, notWindowsProperties)
1135 }
Colin Crossa195f912019-10-16 11:07:20 -07001136 }
1137
1138 // Handle 64-bit device properties in the form:
1139 // target {
1140 // android64 {
1141 // key: value,
1142 // },
1143 // android32 {
1144 // key: value,
1145 // },
1146 // },
1147 // WARNING: this is probably not what you want to use in your blueprints file, it selects
1148 // options for all targets on a device that supports 64-bit binaries, not just the targets
1149 // that are being compiled for 64-bit. Its expected use case is binaries like linker and
1150 // debuggerd that need to know when they are a 32-bit process running on a 64-bit device
1151 if os.Class == Device {
1152 if ctx.Config().Android64() {
1153 field := "Android64"
1154 prefix := "target.android64"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001155 if android64Properties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1156 mergePropertyStruct(ctx, genProps, android64Properties)
1157 }
Colin Crossa195f912019-10-16 11:07:20 -07001158 } else {
1159 field := "Android32"
1160 prefix := "target.android32"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001161 if android32Properties, ok := getChildPropertyStruct(ctx, targetProp, field, prefix); ok {
1162 mergePropertyStruct(ctx, genProps, android32Properties)
1163 }
Colin Crossa195f912019-10-16 11:07:20 -07001164 }
1165 }
1166 }
1167 }
1168}
1169
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001170// Returns the struct containing the properties specific to the given
1171// architecture type. These look like this in Blueprint files:
1172// arch: {
1173// arm64: {
1174// key: value,
1175// },
1176// },
1177// This struct will also contain sub-structs containing to the architecture/CPU
1178// variants and features that themselves contain properties specific to those.
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001179func getArchTypeStruct(ctx ArchVariantContext, archProperties interface{}, archType ArchType) (reflect.Value, bool) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001180 archPropValues := reflect.ValueOf(archProperties).Elem()
1181 archProp := archPropValues.FieldByName("Arch").Elem()
1182 prefix := "arch." + archType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001183 return getChildPropertyStruct(ctx, archProp, archType.Name, prefix)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001184}
1185
1186// Returns the struct containing the properties specific to a given multilib
1187// value. These look like this in the Blueprint file:
1188// multilib: {
1189// lib32: {
1190// key: value,
1191// },
1192// },
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001193func getMultilibStruct(ctx ArchVariantContext, archProperties interface{}, archType ArchType) (reflect.Value, bool) {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001194 archPropValues := reflect.ValueOf(archProperties).Elem()
1195 multilibProp := archPropValues.FieldByName("Multilib").Elem()
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001196 return getChildPropertyStruct(ctx, multilibProp, archType.Multilib, "multilib."+archType.Multilib)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001197}
1198
Liz Kammer9abd62d2021-05-21 08:37:59 -04001199func GetCompoundTargetField(os OsType, arch ArchType) string {
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04001200 return os.Field + "_" + arch.Name
1201}
1202
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001203// Returns the structs corresponding to the properties specific to the given
1204// architecture and OS in archProperties.
1205func getArchProperties(ctx BaseMutatorContext, archProperties interface{}, arch Arch, os OsType, nativeBridgeEnabled bool) []reflect.Value {
1206 result := make([]reflect.Value, 0)
1207 archPropValues := reflect.ValueOf(archProperties).Elem()
1208
1209 targetProp := archPropValues.FieldByName("Target").Elem()
1210
1211 archType := arch.ArchType
1212
1213 if arch.ArchType != Common {
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001214 archStruct, ok := getArchTypeStruct(ctx, archProperties, arch.ArchType)
1215 if ok {
1216 result = append(result, archStruct)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001217
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001218 // Handle arch-variant-specific properties in the form:
1219 // arch: {
1220 // arm: {
1221 // variant: {
1222 // key: value,
1223 // },
1224 // },
1225 // },
1226 v := variantReplacer.Replace(arch.ArchVariant)
1227 if v != "" {
1228 prefix := "arch." + archType.Name + "." + v
1229 if variantProperties, ok := getChildPropertyStruct(ctx, archStruct, v, prefix); ok {
1230 result = append(result, variantProperties)
1231 }
1232 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001233
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001234 // Handle cpu-variant-specific properties in the form:
1235 // arch: {
1236 // arm: {
1237 // variant: {
1238 // key: value,
1239 // },
1240 // },
1241 // },
1242 if arch.CpuVariant != arch.ArchVariant {
1243 c := variantReplacer.Replace(arch.CpuVariant)
1244 if c != "" {
1245 prefix := "arch." + archType.Name + "." + c
1246 if cpuVariantProperties, ok := getChildPropertyStruct(ctx, archStruct, c, prefix); ok {
1247 result = append(result, cpuVariantProperties)
1248 }
1249 }
1250 }
1251
1252 // Handle arch-feature-specific properties in the form:
1253 // arch: {
1254 // arm: {
1255 // feature: {
1256 // key: value,
1257 // },
1258 // },
1259 // },
1260 for _, feature := range arch.ArchFeatures {
1261 prefix := "arch." + archType.Name + "." + feature
1262 if featureProperties, ok := getChildPropertyStruct(ctx, archStruct, feature, prefix); ok {
1263 result = append(result, featureProperties)
1264 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001265 }
1266 }
1267
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001268 if multilibProperties, ok := getMultilibStruct(ctx, archProperties, archType); ok {
1269 result = append(result, multilibProperties)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001270 }
1271
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001272 // Handle combined OS-feature and arch specific properties in the form:
1273 // target: {
1274 // bionic_x86: {
1275 // key: value,
1276 // },
1277 // }
1278 if os.Linux() {
1279 field := "Linux_" + arch.ArchType.Name
1280 userFriendlyField := "target.linux_" + arch.ArchType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001281 if linuxProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1282 result = append(result, linuxProperties)
1283 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001284 }
1285
1286 if os.Bionic() {
1287 field := "Bionic_" + archType.Name
1288 userFriendlyField := "target.bionic_" + archType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001289 if bionicProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1290 result = append(result, bionicProperties)
1291 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001292 }
1293
1294 // Handle combined OS and arch specific properties in the form:
1295 // target: {
1296 // linux_glibc_x86: {
1297 // key: value,
1298 // },
1299 // linux_glibc_arm: {
1300 // key: value,
1301 // },
1302 // android_arm {
1303 // key: value,
1304 // },
1305 // android_x86 {
1306 // key: value,
1307 // },
1308 // },
Liz Kammer9abd62d2021-05-21 08:37:59 -04001309 field := GetCompoundTargetField(os, archType)
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001310 userFriendlyField := "target." + os.Name + "_" + archType.Name
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001311 if osArchProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1312 result = append(result, osArchProperties)
1313 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001314 }
1315
1316 // Handle arm on x86 properties in the form:
1317 // target {
1318 // arm_on_x86 {
1319 // key: value,
1320 // },
1321 // arm_on_x86_64 {
1322 // key: value,
1323 // },
1324 // },
1325 if os.Class == Device {
1326 if arch.ArchType == X86 && (hasArmAbi(arch) ||
1327 hasArmAndroidArch(ctx.Config().Targets[Android])) {
1328 field := "Arm_on_x86"
1329 userFriendlyField := "target.arm_on_x86"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001330 if armOnX86Properties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1331 result = append(result, armOnX86Properties)
1332 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001333 }
1334 if arch.ArchType == X86_64 && (hasArmAbi(arch) ||
1335 hasArmAndroidArch(ctx.Config().Targets[Android])) {
1336 field := "Arm_on_x86_64"
1337 userFriendlyField := "target.arm_on_x86_64"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001338 if armOnX8664Properties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok {
1339 result = append(result, armOnX8664Properties)
1340 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001341 }
1342 if os == Android && nativeBridgeEnabled {
1343 userFriendlyField := "Native_bridge"
1344 prefix := "target.native_bridge"
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001345 if nativeBridgeProperties, ok := getChildPropertyStruct(ctx, targetProp, userFriendlyField, prefix); ok {
1346 result = append(result, nativeBridgeProperties)
1347 }
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001348 }
1349 }
1350
1351 return result
1352}
1353
Colin Crossa6845402020-11-16 15:08:19 -08001354// Squash the appropriate arch-specific property structs into the matching top level property
1355// structs based on the CompileTarget value that was annotated on the variant.
Colin Cross4157e882019-06-06 16:57:04 -07001356func (m *ModuleBase) setArchProperties(ctx BottomUpMutatorContext) {
1357 arch := m.Arch()
1358 os := m.Os()
Colin Crossd3ba0392015-05-07 14:11:29 -07001359
Colin Cross4157e882019-06-06 16:57:04 -07001360 for i := range m.generalProperties {
1361 genProps := m.generalProperties[i]
1362 if m.archProperties[i] == nil {
Dan Willemsenb1957a52016-06-23 23:44:54 -07001363 continue
1364 }
Dan Willemsenb1957a52016-06-23 23:44:54 -07001365
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001366 propStructs := make([]reflect.Value, 0)
1367 for _, archProperty := range m.archProperties[i] {
1368 propStructShard := getArchProperties(ctx, archProperty, arch, os, m.Target().NativeBridge == NativeBridgeEnabled)
1369 propStructs = append(propStructs, propStructShard...)
1370 }
Dan Willemsenb1957a52016-06-23 23:44:54 -07001371
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001372 for _, propStruct := range propStructs {
1373 mergePropertyStruct(ctx, genProps, propStruct)
Colin Crossbb2e2b72016-12-08 17:23:53 -08001374 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001375 }
1376}
1377
Colin Cross0c66bc62021-07-20 09:47:41 -07001378// determineBuildOS stores the OS and architecture used for host targets used during the build into
1379// config based on the runtime OS and architecture determined by Go.
1380func determineBuildOS(config *config) {
1381 config.BuildOS = func() OsType {
1382 switch runtime.GOOS {
1383 case "linux":
1384 return Linux
1385 case "darwin":
1386 return Darwin
1387 default:
1388 panic(fmt.Sprintf("unsupported OS: %s", runtime.GOOS))
1389 }
1390 }()
1391
1392 config.BuildArch = func() ArchType {
1393 switch runtime.GOARCH {
1394 case "amd64":
1395 return X86_64
1396 default:
1397 panic(fmt.Sprintf("unsupported Arch: %s", runtime.GOARCH))
1398 }
1399 }()
1400
1401}
1402
Colin Crossa6845402020-11-16 15:08:19 -08001403// Convert the arch product variables into a list of targets for each OsType.
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001404func decodeTargetProductVariables(config *config) (map[OsType][]Target, error) {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001405 variables := config.productVariables
Dan Willemsen490fd492015-11-24 17:53:15 -08001406
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001407 targets := make(map[OsType][]Target)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001408 var targetErr error
1409
dimitry1f33e402019-03-26 12:39:31 +01001410 addTarget := func(os OsType, archName string, archVariant, cpuVariant *string, abi []string,
dimitry8d6dde82019-07-11 10:23:53 +02001411 nativeBridgeEnabled NativeBridgeSupport, nativeBridgeHostArchName *string,
1412 nativeBridgeRelativePath *string) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001413 if targetErr != nil {
1414 return
Dan Willemsen490fd492015-11-24 17:53:15 -08001415 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001416
Dan Willemsen01a3c252019-01-11 19:02:16 -08001417 arch, err := decodeArch(os, archName, archVariant, cpuVariant, abi)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001418 if err != nil {
1419 targetErr = err
1420 return
1421 }
dimitry8d6dde82019-07-11 10:23:53 +02001422 nativeBridgeRelativePathStr := String(nativeBridgeRelativePath)
1423 nativeBridgeHostArchNameStr := String(nativeBridgeHostArchName)
1424
1425 // Use guest arch as relative install path by default
1426 if nativeBridgeEnabled && nativeBridgeRelativePathStr == "" {
1427 nativeBridgeRelativePathStr = arch.ArchType.String()
1428 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001429
Jiyong Park1613e552020-09-14 19:43:17 +09001430 // A target is considered as HostCross if it's a host target which can't run natively on
1431 // the currently configured build machine (either because the OS is different or because of
1432 // the unsupported arch)
1433 hostCross := false
1434 if os.Class == Host {
1435 var osSupported bool
Colin Cross0c66bc62021-07-20 09:47:41 -07001436 if os == config.BuildOS {
Jiyong Park1613e552020-09-14 19:43:17 +09001437 osSupported = true
Colin Cross0c66bc62021-07-20 09:47:41 -07001438 } else if config.BuildOS.Linux() && os.Linux() {
Jiyong Park1613e552020-09-14 19:43:17 +09001439 // LinuxBionic and Linux are compatible
1440 osSupported = true
1441 } else {
1442 osSupported = false
1443 }
1444
1445 var archSupported bool
1446 if arch.ArchType == Common {
1447 archSupported = true
1448 } else if arch.ArchType.Name == *variables.HostArch {
1449 archSupported = true
1450 } else if variables.HostSecondaryArch != nil && arch.ArchType.Name == *variables.HostSecondaryArch {
1451 archSupported = true
1452 } else {
1453 archSupported = false
1454 }
1455 if !osSupported || !archSupported {
1456 hostCross = true
1457 }
1458 }
1459
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001460 targets[os] = append(targets[os],
Colin Crossa1ad8d12016-06-01 17:09:44 -07001461 Target{
dimitry8d6dde82019-07-11 10:23:53 +02001462 Os: os,
1463 Arch: arch,
1464 NativeBridge: nativeBridgeEnabled,
1465 NativeBridgeHostArchName: nativeBridgeHostArchNameStr,
1466 NativeBridgeRelativePath: nativeBridgeRelativePathStr,
Jiyong Park1613e552020-09-14 19:43:17 +09001467 HostCross: hostCross,
Colin Crossa1ad8d12016-06-01 17:09:44 -07001468 })
Dan Willemsen490fd492015-11-24 17:53:15 -08001469 }
1470
Colin Cross4225f652015-09-17 14:33:42 -07001471 if variables.HostArch == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001472 return nil, fmt.Errorf("No host primary architecture set")
Colin Cross4225f652015-09-17 14:33:42 -07001473 }
1474
Colin Crossa6845402020-11-16 15:08:19 -08001475 // The primary host target, which must always exist.
Colin Cross0c66bc62021-07-20 09:47:41 -07001476 addTarget(config.BuildOS, *variables.HostArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -07001477
Colin Crossa6845402020-11-16 15:08:19 -08001478 // An optional secondary host target.
Colin Crosseeabb892015-11-20 13:07:51 -08001479 if variables.HostSecondaryArch != nil && *variables.HostSecondaryArch != "" {
Colin Cross0c66bc62021-07-20 09:47:41 -07001480 addTarget(config.BuildOS, *variables.HostSecondaryArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -08001481 }
1482
Colin Crossa6845402020-11-16 15:08:19 -08001483 // Optional cross-compiled host targets, generally Windows.
Colin Crossff3ae9d2018-04-10 16:15:18 -07001484 if String(variables.CrossHost) != "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001485 crossHostOs := osByName(*variables.CrossHost)
1486 if crossHostOs == NoOsType {
1487 return nil, fmt.Errorf("Unknown cross host OS %q", *variables.CrossHost)
1488 }
1489
Colin Crossff3ae9d2018-04-10 16:15:18 -07001490 if String(variables.CrossHostArch) == "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001491 return nil, fmt.Errorf("No cross-host primary architecture set")
Dan Willemsen490fd492015-11-24 17:53:15 -08001492 }
1493
Colin Crossa6845402020-11-16 15:08:19 -08001494 // The primary cross-compiled host target.
dimitry8d6dde82019-07-11 10:23:53 +02001495 addTarget(crossHostOs, *variables.CrossHostArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -08001496
Colin Crossa6845402020-11-16 15:08:19 -08001497 // An optional secondary cross-compiled host target.
Dan Willemsen490fd492015-11-24 17:53:15 -08001498 if variables.CrossHostSecondaryArch != nil && *variables.CrossHostSecondaryArch != "" {
dimitry8d6dde82019-07-11 10:23:53 +02001499 addTarget(crossHostOs, *variables.CrossHostSecondaryArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -08001500 }
1501 }
1502
Colin Crossa6845402020-11-16 15:08:19 -08001503 // Optional device targets
Dan Willemsen3f32f032016-07-11 14:36:48 -07001504 if variables.DeviceArch != nil && *variables.DeviceArch != "" {
Doug Horn21b94272019-01-16 12:06:11 -08001505 var target = Android
1506 if Bool(variables.Fuchsia) {
1507 target = Fuchsia
1508 }
1509
Colin Crossa6845402020-11-16 15:08:19 -08001510 // The primary device target.
Doug Horn21b94272019-01-16 12:06:11 -08001511 addTarget(target, *variables.DeviceArch, variables.DeviceArchVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001512 variables.DeviceCpuVariant, variables.DeviceAbi, NativeBridgeDisabled, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -07001513
Colin Crossa6845402020-11-16 15:08:19 -08001514 // An optional secondary device target.
Dan Willemsen3f32f032016-07-11 14:36:48 -07001515 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" {
1516 addTarget(Android, *variables.DeviceSecondaryArch,
1517 variables.DeviceSecondaryArchVariant, variables.DeviceSecondaryCpuVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001518 variables.DeviceSecondaryAbi, NativeBridgeDisabled, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -07001519 }
dimitry1f33e402019-03-26 12:39:31 +01001520
Colin Crossa6845402020-11-16 15:08:19 -08001521 // An optional NativeBridge device target.
dimitry1f33e402019-03-26 12:39:31 +01001522 if variables.NativeBridgeArch != nil && *variables.NativeBridgeArch != "" {
1523 addTarget(Android, *variables.NativeBridgeArch,
1524 variables.NativeBridgeArchVariant, variables.NativeBridgeCpuVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001525 variables.NativeBridgeAbi, NativeBridgeEnabled, variables.DeviceArch,
1526 variables.NativeBridgeRelativePath)
dimitry1f33e402019-03-26 12:39:31 +01001527 }
1528
Colin Crossa6845402020-11-16 15:08:19 -08001529 // An optional secondary NativeBridge device target.
dimitry1f33e402019-03-26 12:39:31 +01001530 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" &&
1531 variables.NativeBridgeSecondaryArch != nil && *variables.NativeBridgeSecondaryArch != "" {
1532 addTarget(Android, *variables.NativeBridgeSecondaryArch,
1533 variables.NativeBridgeSecondaryArchVariant,
1534 variables.NativeBridgeSecondaryCpuVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001535 variables.NativeBridgeSecondaryAbi,
1536 NativeBridgeEnabled,
1537 variables.DeviceSecondaryArch,
1538 variables.NativeBridgeSecondaryRelativePath)
dimitry1f33e402019-03-26 12:39:31 +01001539 }
Colin Cross4225f652015-09-17 14:33:42 -07001540 }
1541
Colin Crossa1ad8d12016-06-01 17:09:44 -07001542 if targetErr != nil {
1543 return nil, targetErr
1544 }
1545
1546 return targets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001547}
1548
Colin Crossbb2e2b72016-12-08 17:23:53 -08001549// hasArmAbi returns true if arch has at least one arm ABI
1550func hasArmAbi(arch Arch) bool {
Jaewoong Jung3aff5782020-02-11 07:54:35 -08001551 return PrefixInList(arch.Abi, "arm")
Colin Crossbb2e2b72016-12-08 17:23:53 -08001552}
1553
dimitry628db6f2019-05-22 17:16:21 +02001554// hasArmArch returns true if targets has at least non-native_bridge arm Android arch
Colin Cross4247f0d2017-04-13 16:56:14 -07001555func hasArmAndroidArch(targets []Target) bool {
1556 for _, target := range targets {
Victor Khimenko1a31f802020-09-17 03:07:31 +02001557 if target.Os == Android && target.Arch.ArchType == Arm {
Victor Khimenko5eb8ec12018-03-21 20:30:54 +01001558 return true
1559 }
1560 }
1561 return false
1562}
1563
Colin Crossa6845402020-11-16 15:08:19 -08001564// archConfig describes a built-in configuration.
Dan Albert4098deb2016-10-19 14:04:41 -07001565type archConfig struct {
1566 arch string
1567 archVariant string
1568 cpuVariant string
1569 abi []string
1570}
1571
Dan Albertf1d14c72020-07-30 14:32:55 -07001572// getNdkAbisConfig returns the list of archConfigs that are used for bulding
1573// the API stubs and static libraries that are included in the NDK. These are
1574// built *without Neon*, because non-Neon is still supported and building these
1575// with Neon will break those users.
Dan Albert4098deb2016-10-19 14:04:41 -07001576func getNdkAbisConfig() []archConfig {
1577 return []archConfig{
Tamas Petzbca786d2021-01-20 18:56:33 +01001578 {"arm64", "armv8-a-branchprot", "", []string{"arm64-v8a"}},
Inseob Kim5219c0e2021-06-17 00:33:00 +09001579 {"arm", "armv7-a", "", []string{"armeabi-v7a"}},
Dan Albert4098deb2016-10-19 14:04:41 -07001580 {"x86_64", "", "", []string{"x86_64"}},
Inseob Kim5219c0e2021-06-17 00:33:00 +09001581 {"x86", "", "", []string{"x86"}},
Dan Albert4098deb2016-10-19 14:04:41 -07001582 }
1583}
1584
Colin Crossa6845402020-11-16 15:08:19 -08001585// getAmlAbisConfig returns a list of archConfigs for the ABIs supported by mainline modules.
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001586func getAmlAbisConfig() []archConfig {
1587 return []archConfig{
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001588 {"arm64", "armv8-a", "", []string{"arm64-v8a"}},
Inseob Kim5219c0e2021-06-17 00:33:00 +09001589 {"arm", "armv7-a-neon", "", []string{"armeabi-v7a"}},
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001590 {"x86_64", "", "", []string{"x86_64"}},
Inseob Kim5219c0e2021-06-17 00:33:00 +09001591 {"x86", "", "", []string{"x86"}},
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001592 }
1593}
1594
Colin Crossa6845402020-11-16 15:08:19 -08001595// decodeArchSettings converts a list of archConfigs into a list of Targets for the given OsType.
Dan Willemsen01a3c252019-01-11 19:02:16 -08001596func decodeArchSettings(os OsType, archConfigs []archConfig) ([]Target, error) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001597 var ret []Target
Dan Willemsen322acaf2016-01-12 23:07:05 -08001598
Dan Albert4098deb2016-10-19 14:04:41 -07001599 for _, config := range archConfigs {
Dan Willemsen01a3c252019-01-11 19:02:16 -08001600 arch, err := decodeArch(os, config.arch, &config.archVariant,
Colin Crossa74ca042019-01-31 14:31:51 -08001601 &config.cpuVariant, config.abi)
Dan Willemsen322acaf2016-01-12 23:07:05 -08001602 if err != nil {
1603 return nil, err
1604 }
Colin Cross3b19f5d2019-09-17 14:45:31 -07001605
Colin Crossa1ad8d12016-06-01 17:09:44 -07001606 ret = append(ret, Target{
1607 Os: Android,
1608 Arch: arch,
1609 })
Dan Willemsen322acaf2016-01-12 23:07:05 -08001610 }
1611
1612 return ret, nil
1613}
1614
Colin Crossa6845402020-11-16 15:08:19 -08001615// decodeArch converts a set of strings from product variables into an Arch struct.
Colin Crossa74ca042019-01-31 14:31:51 -08001616func decodeArch(os OsType, arch string, archVariant, cpuVariant *string, abi []string) (Arch, error) {
Colin Crossa6845402020-11-16 15:08:19 -08001617 // Verify the arch is valid
Colin Crosseeabb892015-11-20 13:07:51 -08001618 archType, ok := archTypeMap[arch]
1619 if !ok {
1620 return Arch{}, fmt.Errorf("unknown arch %q", arch)
1621 }
Colin Cross4225f652015-09-17 14:33:42 -07001622
Colin Crosseeabb892015-11-20 13:07:51 -08001623 a := Arch{
Colin Cross4225f652015-09-17 14:33:42 -07001624 ArchType: archType,
Colin Crossa6845402020-11-16 15:08:19 -08001625 ArchVariant: String(archVariant),
1626 CpuVariant: String(cpuVariant),
Colin Crossa74ca042019-01-31 14:31:51 -08001627 Abi: abi,
Colin Crosseeabb892015-11-20 13:07:51 -08001628 }
1629
Colin Crossa6845402020-11-16 15:08:19 -08001630 // Convert generic arch variants into the empty string.
Colin Crosseeabb892015-11-20 13:07:51 -08001631 if a.ArchVariant == a.ArchType.Name || a.ArchVariant == "generic" {
1632 a.ArchVariant = ""
1633 }
1634
Colin Crossa6845402020-11-16 15:08:19 -08001635 // Convert generic CPU variants into the empty string.
Colin Crosseeabb892015-11-20 13:07:51 -08001636 if a.CpuVariant == a.ArchType.Name || a.CpuVariant == "generic" {
1637 a.CpuVariant = ""
1638 }
1639
Colin Crossa6845402020-11-16 15:08:19 -08001640 // Filter empty ABIs out of the list.
Colin Crosseeabb892015-11-20 13:07:51 -08001641 for i := 0; i < len(a.Abi); i++ {
1642 if a.Abi[i] == "" {
1643 a.Abi = append(a.Abi[:i], a.Abi[i+1:]...)
1644 i--
1645 }
1646 }
1647
Dan Willemsen01a3c252019-01-11 19:02:16 -08001648 if a.ArchVariant == "" {
Colin Crossa6845402020-11-16 15:08:19 -08001649 // Set ArchFeatures from the default arch features.
Dan Willemsen01a3c252019-01-11 19:02:16 -08001650 if featureMap, ok := defaultArchFeatureMap[os]; ok {
1651 a.ArchFeatures = featureMap[archType]
1652 }
1653 } else {
Colin Crossa6845402020-11-16 15:08:19 -08001654 // Set ArchFeatures from the arch type.
Dan Willemsen01a3c252019-01-11 19:02:16 -08001655 if featureMap, ok := archFeatureMap[archType]; ok {
1656 a.ArchFeatures = featureMap[a.ArchVariant]
1657 }
Colin Crossc5c24ad2015-11-20 15:35:00 -08001658 }
1659
Colin Crosseeabb892015-11-20 13:07:51 -08001660 return a, nil
Colin Cross4225f652015-09-17 14:33:42 -07001661}
1662
Colin Crossa6845402020-11-16 15:08:19 -08001663// filterMultilibTargets takes a list of Targets and a multilib value and returns a new list of
1664// Targets containing only those that have the given multilib value.
Colin Cross69617d32016-09-06 10:39:07 -07001665func filterMultilibTargets(targets []Target, multilib string) []Target {
1666 var ret []Target
1667 for _, t := range targets {
1668 if t.Arch.ArchType.Multilib == multilib {
1669 ret = append(ret, t)
1670 }
1671 }
1672 return ret
1673}
1674
Colin Crossa6845402020-11-16 15:08:19 -08001675// getCommonTargets returns the set of Os specific common architecture targets for each Os in a list
1676// of targets.
Nan Zhangdb0b9a32017-02-27 10:12:13 -08001677func getCommonTargets(targets []Target) []Target {
1678 var ret []Target
1679 set := make(map[string]bool)
1680
1681 for _, t := range targets {
1682 if _, found := set[t.Os.String()]; !found {
1683 set[t.Os.String()] = true
1684 ret = append(ret, commonTargetMap[t.Os.String()])
1685 }
1686 }
1687
1688 return ret
1689}
1690
Colin Crossa6845402020-11-16 15:08:19 -08001691// firstTarget takes a list of Targets and a list of multilib values and returns a list of Targets
1692// that contains zero or one Target for each OsType, selecting the one that matches the earliest
1693// filter.
Colin Cross3dceee32018-09-06 10:19:57 -07001694func firstTarget(targets []Target, filters ...string) []Target {
Jiyong Park22101982020-09-17 19:09:58 +09001695 // find the first target from each OS
1696 var ret []Target
1697 hasHost := false
1698 set := make(map[OsType]bool)
1699
Colin Cross6b4a32d2017-12-05 13:42:45 -08001700 for _, filter := range filters {
1701 buildTargets := filterMultilibTargets(targets, filter)
Jiyong Park22101982020-09-17 19:09:58 +09001702 for _, t := range buildTargets {
1703 if _, found := set[t.Os]; !found {
1704 hasHost = hasHost || (t.Os.Class == Host)
1705 set[t.Os] = true
1706 ret = append(ret, t)
1707 }
Colin Cross6b4a32d2017-12-05 13:42:45 -08001708 }
1709 }
Jiyong Park22101982020-09-17 19:09:58 +09001710 return ret
Colin Cross6b4a32d2017-12-05 13:42:45 -08001711}
1712
Colin Crossa6845402020-11-16 15:08:19 -08001713// decodeMultilibTargets uses the module's multilib setting to select one or more targets from a
1714// list of Targets.
Colin Crossee0bc3b2018-10-02 22:01:37 -07001715func decodeMultilibTargets(multilib string, targets []Target, prefer32 bool) ([]Target, error) {
Colin Crossa6845402020-11-16 15:08:19 -08001716 var buildTargets []Target
Colin Cross6b4a32d2017-12-05 13:42:45 -08001717
Colin Cross4225f652015-09-17 14:33:42 -07001718 switch multilib {
1719 case "common":
Colin Cross6b4a32d2017-12-05 13:42:45 -08001720 buildTargets = getCommonTargets(targets)
1721 case "common_first":
1722 buildTargets = getCommonTargets(targets)
1723 if prefer32 {
Colin Cross3dceee32018-09-06 10:19:57 -07001724 buildTargets = append(buildTargets, firstTarget(targets, "lib32", "lib64")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001725 } else {
Colin Cross3dceee32018-09-06 10:19:57 -07001726 buildTargets = append(buildTargets, firstTarget(targets, "lib64", "lib32")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001727 }
Colin Cross4225f652015-09-17 14:33:42 -07001728 case "both":
Colin Cross8b74d172016-09-13 09:59:14 -07001729 if prefer32 {
1730 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1731 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1732 } else {
1733 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1734 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1735 }
Colin Cross4225f652015-09-17 14:33:42 -07001736 case "32":
Colin Cross69617d32016-09-06 10:39:07 -07001737 buildTargets = filterMultilibTargets(targets, "lib32")
Colin Cross4225f652015-09-17 14:33:42 -07001738 case "64":
Colin Cross69617d32016-09-06 10:39:07 -07001739 buildTargets = filterMultilibTargets(targets, "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001740 case "first":
1741 if prefer32 {
Colin Cross3dceee32018-09-06 10:19:57 -07001742 buildTargets = firstTarget(targets, "lib32", "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001743 } else {
Colin Cross3dceee32018-09-06 10:19:57 -07001744 buildTargets = firstTarget(targets, "lib64", "lib32")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001745 }
Victor Chang9448e8f2020-09-14 15:34:16 +01001746 case "first_prefer32":
1747 buildTargets = firstTarget(targets, "lib32", "lib64")
Colin Cross69617d32016-09-06 10:39:07 -07001748 case "prefer32":
Colin Cross3dceee32018-09-06 10:19:57 -07001749 buildTargets = filterMultilibTargets(targets, "lib32")
1750 if len(buildTargets) == 0 {
1751 buildTargets = filterMultilibTargets(targets, "lib64")
1752 }
Colin Cross4225f652015-09-17 14:33:42 -07001753 default:
Victor Chang9448e8f2020-09-14 15:34:16 +01001754 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 -07001755 multilib)
Colin Cross4225f652015-09-17 14:33:42 -07001756 }
1757
Colin Crossa1ad8d12016-06-01 17:09:44 -07001758 return buildTargets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001759}
Jingwen Chen5d864492021-02-24 07:20:12 -05001760
Chris Parsonsc424b762021-04-29 18:06:50 -04001761func (m *ModuleBase) getArchPropertySet(propertySet interface{}, archType ArchType) interface{} {
1762 archString := archType.Field
1763 for i := range m.archProperties {
1764 if m.archProperties[i] == nil {
1765 // Skip over nil properties
1766 continue
1767 }
1768
1769 // Not archProperties are usable; this function looks for properties of a very specific
1770 // form, and ignores the rest.
1771 for _, archProperty := range m.archProperties[i] {
1772 // archPropValue is a property struct, we are looking for the form:
1773 // `arch: { arm: { key: value, ... }}`
1774 archPropValue := reflect.ValueOf(archProperty).Elem()
1775
1776 // Unwrap src so that it should looks like a pointer to `arm: { key: value, ... }`
1777 src := archPropValue.FieldByName("Arch").Elem()
1778
1779 // Step into non-nil pointers to structs in the src value.
1780 if src.Kind() == reflect.Ptr {
1781 if src.IsNil() {
1782 continue
1783 }
1784 src = src.Elem()
1785 }
1786
1787 // Find the requested field (e.g. arm, x86) in the src struct.
1788 src = src.FieldByName(archString)
1789
1790 // We only care about structs.
1791 if !src.IsValid() || src.Kind() != reflect.Struct {
1792 continue
1793 }
1794
1795 // If the value of the field is a struct then step into the
1796 // BlueprintEmbed field. The special "BlueprintEmbed" name is
1797 // used by createArchPropTypeDesc to embed the arch properties
1798 // in the parent struct, so the src arch prop should be in this
1799 // field.
1800 //
1801 // See createArchPropTypeDesc for more details on how Arch-specific
1802 // module properties are processed from the nested props and written
1803 // into the module's archProperties.
1804 src = src.FieldByName("BlueprintEmbed")
1805
1806 // Clone the destination prop, since we want a unique prop struct per arch.
1807 propertySetClone := reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
1808
1809 // Copy the located property struct into the cloned destination property struct.
1810 err := proptools.ExtendMatchingProperties([]interface{}{propertySetClone}, src.Interface(), nil, proptools.OrderReplace)
1811 if err != nil {
1812 // This is fine, it just means the src struct doesn't match the type of propertySet.
1813 continue
1814 }
1815
1816 return propertySetClone
1817 }
1818 }
1819 // No property set was found specific to the given arch, so return an empty
1820 // property set.
1821 return reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
1822}
1823
1824// getMultilibPropertySet returns a property set struct matching the type of
1825// `propertySet`, containing multilib-specific module properties for the given architecture.
1826// If no multilib-specific properties exist for the given architecture, returns an empty property
1827// set matching `propertySet`'s type.
1828func (m *ModuleBase) getMultilibPropertySet(propertySet interface{}, archType ArchType) interface{} {
1829 // archType.Multilib is lowercase (for example, lib32) but property struct field is
1830 // capitalized, such as Lib32, so use strings.Title to capitalize it.
1831 multiLibString := strings.Title(archType.Multilib)
1832
1833 for i := range m.archProperties {
1834 if m.archProperties[i] == nil {
1835 // Skip over nil properties
1836 continue
1837 }
1838
1839 // Not archProperties are usable; this function looks for properties of a very specific
1840 // form, and ignores the rest.
1841 for _, archProperties := range m.archProperties[i] {
1842 // archPropValue is a property struct, we are looking for the form:
1843 // `multilib: { lib32: { key: value, ... }}`
1844 archPropValue := reflect.ValueOf(archProperties).Elem()
1845
1846 // Unwrap src so that it should looks like a pointer to `lib32: { key: value, ... }`
1847 src := archPropValue.FieldByName("Multilib").Elem()
1848
1849 // Step into non-nil pointers to structs in the src value.
1850 if src.Kind() == reflect.Ptr {
1851 if src.IsNil() {
1852 // Ignore nil pointers.
1853 continue
1854 }
1855 src = src.Elem()
1856 }
1857
1858 // Find the requested field (e.g. lib32) in the src struct.
1859 src = src.FieldByName(multiLibString)
1860
1861 // We only care about valid struct pointers.
1862 if !src.IsValid() || src.Kind() != reflect.Ptr || src.Elem().Kind() != reflect.Struct {
1863 continue
1864 }
1865
1866 // Get the zero value for the requested property set.
1867 propertySetClone := reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
1868
1869 // Copy the located property struct into the "zero" property set struct.
1870 err := proptools.ExtendMatchingProperties([]interface{}{propertySetClone}, src.Interface(), nil, proptools.OrderReplace)
1871
1872 if err != nil {
1873 // This is fine, it just means the src struct doesn't match.
1874 continue
1875 }
1876
1877 return propertySetClone
1878 }
1879 }
1880
1881 // There were no multilib properties specifically matching the given archtype.
1882 // Return zeroed value.
1883 return reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
1884}
1885
Liz Kammerb6dbc872021-05-14 15:14:40 -04001886// ArchVariantContext defines the limited context necessary to retrieve arch_variant properties.
1887type ArchVariantContext interface {
1888 ModuleErrorf(fmt string, args ...interface{})
1889 PropertyErrorf(property, fmt string, args ...interface{})
1890}
1891
Liz Kammer9abd62d2021-05-21 08:37:59 -04001892// ArchVariantProperties represents a map of arch-variant config strings to a property interface{}.
1893type ArchVariantProperties map[string]interface{}
1894
1895// ConfigurationAxisToArchVariantProperties represents a map of bazel.ConfigurationAxis to
1896// ArchVariantProperties, such that each independent arch-variant axis maps to the
1897// configs/properties for that axis.
1898type ConfigurationAxisToArchVariantProperties map[bazel.ConfigurationAxis]ArchVariantProperties
1899
1900// GetArchVariantProperties returns a ConfigurationAxisToArchVariantProperties where the
1901// arch-variant properties correspond to the values of the properties of the 'propertySet' struct
1902// that are specific to that axis/configuration. Each axis is independent, containing
1903// non-overlapping configs that correspond to the various "arch-variant" support, at this time:
1904// arches (including multilib)
1905// oses
1906// arch+os combinations
Jingwen Chen5d864492021-02-24 07:20:12 -05001907//
Liz Kammer9abd62d2021-05-21 08:37:59 -04001908// For example, passing a struct { Foo bool, Bar string } will return an interface{} that can be
1909// type asserted back into the same struct, containing the config-specific property value specified
1910// by the module if defined.
Chris Parsonsc424b762021-04-29 18:06:50 -04001911//
1912// Arch-specific properties may come from an arch stanza or a multilib stanza; properties
1913// in these stanzas are combined.
1914// For example: `arch: { x86: { Foo: ["bar"] } }, multilib: { lib32: {` Foo: ["baz"] } }`
1915// will result in `Foo: ["bar", "baz"]` being returned for architecture x86, if the given
1916// propertyset contains `Foo []string`.
Liz Kammer9abd62d2021-05-21 08:37:59 -04001917func (m *ModuleBase) GetArchVariantProperties(ctx ArchVariantContext, propertySet interface{}) ConfigurationAxisToArchVariantProperties {
Jingwen Chen5d864492021-02-24 07:20:12 -05001918 // Return value of the arch types to the prop values for that arch.
Liz Kammer9abd62d2021-05-21 08:37:59 -04001919 axisToProps := ConfigurationAxisToArchVariantProperties{}
Jingwen Chen5d864492021-02-24 07:20:12 -05001920
1921 // Nothing to do for non-arch-specific modules.
1922 if !m.ArchSpecific() {
Liz Kammer9abd62d2021-05-21 08:37:59 -04001923 return axisToProps
Jingwen Chen5d864492021-02-24 07:20:12 -05001924 }
1925
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001926 dstType := reflect.ValueOf(propertySet).Type()
1927 var archProperties []interface{}
1928
1929 // First find the property set in the module that corresponds to the requested
1930 // one. m.archProperties[i] corresponds to m.generalProperties[i].
1931 for i, generalProp := range m.generalProperties {
1932 srcType := reflect.ValueOf(generalProp).Type()
1933 if srcType == dstType {
1934 archProperties = m.archProperties[i]
1935 break
1936 }
1937 }
1938
1939 if archProperties == nil {
1940 // This module does not have the property set requested
Liz Kammer9abd62d2021-05-21 08:37:59 -04001941 return axisToProps
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001942 }
1943
Liz Kammer9abd62d2021-05-21 08:37:59 -04001944 archToProp := ArchVariantProperties{}
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001945 // For each arch type (x86, arm64, etc.)
Chris Parsonsc424b762021-04-29 18:06:50 -04001946 for _, arch := range ArchTypeList() {
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001947 // Arch properties are sometimes sharded (see createArchPropTypeDesc() ).
1948 // Iterate over ever shard and extract a struct with the same type as the
1949 // input one that contains the data specific to that arch.
1950 propertyStructs := make([]reflect.Value, 0)
1951 for _, archProperty := range archProperties {
Lukacs T. Berki5f518392021-05-17 11:44:58 +02001952 archTypeStruct, ok := getArchTypeStruct(ctx, archProperty, arch)
1953 if ok {
1954 propertyStructs = append(propertyStructs, archTypeStruct)
1955 }
1956 multilibStruct, ok := getMultilibStruct(ctx, archProperty, arch)
1957 if ok {
1958 propertyStructs = append(propertyStructs, multilibStruct)
1959 }
Jingwen Chen5d864492021-02-24 07:20:12 -05001960 }
1961
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001962 // Create a new instance of the requested property set
1963 value := reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
1964
1965 // Merge all the structs together
1966 for _, propertyStruct := range propertyStructs {
1967 mergePropertyStruct(ctx, value, propertyStruct)
1968 }
1969
Liz Kammer9abd62d2021-05-21 08:37:59 -04001970 archToProp[arch.Name] = value
Jingwen Chen5d864492021-02-24 07:20:12 -05001971 }
Liz Kammer9abd62d2021-05-21 08:37:59 -04001972 axisToProps[bazel.ArchConfigurationAxis] = archToProp
Lukacs T. Berki598dd002021-05-05 09:00:01 +02001973
Liz Kammer9abd62d2021-05-21 08:37:59 -04001974 osToProp := ArchVariantProperties{}
1975 archOsToProp := ArchVariantProperties{}
1976 // For android, linux, ...
1977 for _, os := range osTypeList {
1978 if os == CommonOS {
1979 // It looks like this OS value is not used in Blueprint files
1980 continue
1981 }
1982 osToProp[os.Name] = getTargetStruct(ctx, propertySet, archProperties, os.Field)
1983 // For arm, x86, ...
1984 for _, arch := range osArchTypeMap[os] {
1985 targetField := GetCompoundTargetField(os, arch)
1986 targetName := fmt.Sprintf("%s_%s", os.Name, arch.Name)
1987 archOsToProp[targetName] = getTargetStruct(ctx, propertySet, archProperties, targetField)
1988 }
1989 }
1990 axisToProps[bazel.OsConfigurationAxis] = osToProp
1991 axisToProps[bazel.OsArchConfigurationAxis] = archOsToProp
1992
Liz Kammer01a16e82021-07-16 16:33:47 -04001993 axisToProps[bazel.BionicConfigurationAxis] = map[string]interface{}{
1994 "bionic": getTargetStruct(ctx, propertySet, archProperties, "Bionic"),
1995 }
1996
Liz Kammer9abd62d2021-05-21 08:37:59 -04001997 return axisToProps
Jingwen Chen5d864492021-02-24 07:20:12 -05001998}
Jingwen Chen91220d72021-03-24 02:18:33 -04001999
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04002000// Returns a struct matching the propertySet interface, containing properties specific to the targetName
2001// For example, given these arguments:
2002// propertySet = BaseCompilerProperties
2003// targetName = "android_arm"
2004// And given this Android.bp fragment:
2005// target:
2006// android_arm: {
2007// srcs: ["foo.c"],
2008// }
2009// android_arm64: {
2010// srcs: ["bar.c"],
2011// }
2012// }
2013// This would return a BaseCompilerProperties with BaseCompilerProperties.Srcs = ["foo.c"]
2014func getTargetStruct(ctx ArchVariantContext, propertySet interface{}, archProperties []interface{}, targetName string) interface{} {
2015 propertyStructs := make([]reflect.Value, 0)
2016 for _, archProperty := range archProperties {
2017 archPropValues := reflect.ValueOf(archProperty).Elem()
2018 targetProp := archPropValues.FieldByName("Target").Elem()
2019 targetStruct, ok := getChildPropertyStruct(ctx, targetProp, targetName, targetName)
2020 if ok {
2021 propertyStructs = append(propertyStructs, targetStruct)
2022 }
2023 }
2024
2025 // Create a new instance of the requested property set
2026 value := reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
2027
2028 // Merge all the structs together
2029 for _, propertyStruct := range propertyStructs {
2030 mergePropertyStruct(ctx, value, propertyStruct)
2031 }
2032
2033 return value
2034}