blob: 1403af4db90b0f29b279861ff1ab74740c505ffc [file] [log] [blame]
Colin Cross3f40fa42015-01-30 17:27:36 -08001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Colin Cross635c3b02016-05-18 15:37:25 -070015package android
Colin Cross3f40fa42015-01-30 17:27:36 -080016
17import (
Colin Cross74ba9622019-02-11 15:11:14 -080018 "encoding"
Colin Cross3f40fa42015-01-30 17:27:36 -080019 "fmt"
20 "reflect"
21 "runtime"
22 "strings"
Colin Crossf6566ed2015-03-24 11:13:38 -070023
Colin Cross0f7d2ef2019-10-16 11:03:10 -070024 "github.com/google/blueprint"
Colin Cross617b88a2020-08-24 18:04:09 -070025 "github.com/google/blueprint/bootstrap"
Colin Crossf6566ed2015-03-24 11:13:38 -070026 "github.com/google/blueprint/proptools"
Colin Cross3f40fa42015-01-30 17:27:36 -080027)
28
Colin Cross3f40fa42015-01-30 17:27:36 -080029/*
30Example blueprints file containing all variant property groups, with comment listing what type
31of variants get properties in that group:
32
33module {
34 arch: {
35 arm: {
36 // Host or device variants with arm architecture
37 },
38 arm64: {
39 // Host or device variants with arm64 architecture
40 },
Colin Cross3f40fa42015-01-30 17:27:36 -080041 x86: {
42 // Host or device variants with x86 architecture
43 },
44 x86_64: {
45 // Host or device variants with x86_64 architecture
46 },
47 },
48 multilib: {
49 lib32: {
50 // Host or device variants for 32-bit architectures
51 },
52 lib64: {
53 // Host or device variants for 64-bit architectures
54 },
55 },
56 target: {
57 android: {
Martin Stjernholme284b482020-09-23 21:03:27 +010058 // Device variants (implies Bionic)
Colin Cross3f40fa42015-01-30 17:27:36 -080059 },
60 host: {
61 // Host variants
62 },
Martin Stjernholme284b482020-09-23 21:03:27 +010063 bionic: {
64 // Bionic (device and host) variants
65 },
66 linux_bionic: {
67 // Bionic host variants
68 },
69 linux: {
70 // Bionic (device and host) and Linux glibc variants
71 },
Dan Willemsen5746bd42017-10-02 19:42:01 -070072 linux_glibc: {
Martin Stjernholme284b482020-09-23 21:03:27 +010073 // Linux host variants (using non-Bionic libc)
Colin Cross3f40fa42015-01-30 17:27:36 -080074 },
75 darwin: {
76 // Darwin host variants
77 },
78 windows: {
79 // Windows host variants
80 },
81 not_windows: {
82 // Non-windows host variants
83 },
Martin Stjernholme284b482020-09-23 21:03:27 +010084 android_arm: {
85 // Any <os>_<arch> combination restricts to that os and arch
86 },
Colin Cross3f40fa42015-01-30 17:27:36 -080087 },
88}
89*/
Colin Cross7d5136f2015-05-11 13:39:40 -070090
Colin Cross3f40fa42015-01-30 17:27:36 -080091// An Arch indicates a single CPU architecture.
92type Arch struct {
Colin Crossa6845402020-11-16 15:08:19 -080093 // The type of the architecture (arm, arm64, x86, or x86_64).
94 ArchType ArchType
95
96 // The variant of the architecture, for example "armv7-a" or "armv7-a-neon" for arm.
97 ArchVariant string
98
99 // The variant of the CPU, for example "cortex-a53" for arm64.
100 CpuVariant string
101
102 // The list of Android app ABIs supported by the CPU architecture, for example "arm64-v8a".
103 Abi []string
104
105 // The list of arch-specific features supported by the CPU architecture, for example "neon".
Colin Crossc5c24ad2015-11-20 15:35:00 -0800106 ArchFeatures []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800107}
108
Colin Crossa6845402020-11-16 15:08:19 -0800109// String returns the Arch as a string. The value is used as the name of the variant created
110// by archMutator.
Colin Cross3f40fa42015-01-30 17:27:36 -0800111func (a Arch) String() string {
Colin Crossd3ba0392015-05-07 14:11:29 -0700112 s := a.ArchType.String()
Colin Cross3f40fa42015-01-30 17:27:36 -0800113 if a.ArchVariant != "" {
114 s += "_" + a.ArchVariant
115 }
116 if a.CpuVariant != "" {
117 s += "_" + a.CpuVariant
118 }
119 return s
120}
121
Colin Crossa6845402020-11-16 15:08:19 -0800122// ArchType is used to define the 4 supported architecture types (arm, arm64, x86, x86_64), as
123// well as the "common" architecture used for modules that support multiple architectures, for
124// example Java modules.
Colin Cross3f40fa42015-01-30 17:27:36 -0800125type ArchType struct {
Colin Crossa6845402020-11-16 15:08:19 -0800126 // Name is the name of the architecture type, "arm", "arm64", "x86", or "x86_64".
127 Name string
128
129 // Field is the name of the field used in properties that refer to the architecture, e.g. "Arm64".
130 Field string
131
132 // Multilib is either "lib32" or "lib64" for 32-bit or 64-bit architectures.
Colin Crossec193632015-07-06 17:49:43 -0700133 Multilib string
Colin Cross3f40fa42015-01-30 17:27:36 -0800134}
135
Colin Crossa6845402020-11-16 15:08:19 -0800136// String returns the name of the ArchType.
137func (a ArchType) String() string {
138 return a.Name
139}
140
141const COMMON_VARIANT = "common"
142
143var (
144 archTypeList []ArchType
145
146 Arm = newArch("arm", "lib32")
147 Arm64 = newArch("arm64", "lib64")
148 X86 = newArch("x86", "lib32")
149 X86_64 = newArch("x86_64", "lib64")
150
151 Common = ArchType{
152 Name: COMMON_VARIANT,
153 }
154)
155
156var archTypeMap = map[string]ArchType{}
157
Colin Crossec193632015-07-06 17:49:43 -0700158func newArch(name, multilib string) ArchType {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700159 archType := ArchType{
Colin Crossec193632015-07-06 17:49:43 -0700160 Name: name,
Dan Willemsenb1957a52016-06-23 23:44:54 -0700161 Field: proptools.FieldNameForProperty(name),
Colin Crossec193632015-07-06 17:49:43 -0700162 Multilib: multilib,
Colin Cross3f40fa42015-01-30 17:27:36 -0800163 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700164 archTypeList = append(archTypeList, archType)
Colin Crossa6845402020-11-16 15:08:19 -0800165 archTypeMap[name] = archType
Dan Willemsenb1957a52016-06-23 23:44:54 -0700166 return archType
Colin Cross3f40fa42015-01-30 17:27:36 -0800167}
168
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000169// ArchTypeList returns the a slice copy of the 4 supported ArchTypes for arm,
170// arm64, x86 and x86_64.
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -0700171func ArchTypeList() []ArchType {
172 return append([]ArchType(nil), archTypeList...)
173}
174
Colin Crossa6845402020-11-16 15:08:19 -0800175// MarshalText allows an ArchType to be serialized through any encoder that supports
176// encoding.TextMarshaler.
Colin Cross74ba9622019-02-11 15:11:14 -0800177func (a ArchType) MarshalText() ([]byte, error) {
Jeongik Chabec4d032021-04-15 08:55:38 +0900178 return []byte(a.String()), nil
Colin Cross74ba9622019-02-11 15:11:14 -0800179}
180
Colin Crossa6845402020-11-16 15:08:19 -0800181var _ encoding.TextMarshaler = ArchType{}
Colin Cross74ba9622019-02-11 15:11:14 -0800182
Colin Crossa6845402020-11-16 15:08:19 -0800183// UnmarshalText allows an ArchType to be deserialized through any decoder that supports
184// encoding.TextUnmarshaler.
Colin Cross74ba9622019-02-11 15:11:14 -0800185func (a *ArchType) UnmarshalText(text []byte) error {
186 if u, ok := archTypeMap[string(text)]; ok {
187 *a = u
188 return nil
189 }
190
191 return fmt.Errorf("unknown ArchType %q", text)
192}
193
Colin Crossa6845402020-11-16 15:08:19 -0800194var _ encoding.TextUnmarshaler = &ArchType{}
Colin Crossa1ad8d12016-06-01 17:09:44 -0700195
Colin Crossa6845402020-11-16 15:08:19 -0800196// OsClass is an enum that describes whether a variant of a module runs on the host, on the device,
197// or is generic.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700198type OsClass int
199
200const (
Colin Crossa6845402020-11-16 15:08:19 -0800201 // Generic is used for variants of modules that are not OS-specific.
Dan Willemsen0e2d97b2016-11-28 17:50:06 -0800202 Generic OsClass = iota
Colin Crossa6845402020-11-16 15:08:19 -0800203 // Device is used for variants of modules that run on the device.
Dan Willemsen0e2d97b2016-11-28 17:50:06 -0800204 Device
Colin Crossa6845402020-11-16 15:08:19 -0800205 // Host is used for variants of modules that run on the host.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700206 Host
Colin Crossa1ad8d12016-06-01 17:09:44 -0700207)
208
Colin Crossa6845402020-11-16 15:08:19 -0800209// String returns the OsClass as a string.
Colin Cross67a5c132017-05-09 13:45:28 -0700210func (class OsClass) String() string {
211 switch class {
212 case Generic:
213 return "generic"
214 case Device:
215 return "device"
216 case Host:
217 return "host"
Colin Cross67a5c132017-05-09 13:45:28 -0700218 default:
219 panic(fmt.Errorf("unknown class %d", class))
220 }
221}
222
Colin Crossa6845402020-11-16 15:08:19 -0800223// OsType describes an OS variant of a module.
224type OsType struct {
225 // Name is the name of the OS. It is also used as the name of the property in Android.bp
226 // files.
227 Name string
228
229 // Field is the name of the OS converted to an exported field name, i.e. with the first
230 // character capitalized.
231 Field string
232
233 // Class is the OsClass of the OS.
234 Class OsClass
235
236 // DefaultDisabled is set when the module variants for the OS should not be created unless
237 // the module explicitly requests them. This is used to limit Windows cross compilation to
238 // only modules that need it.
239 DefaultDisabled bool
240}
241
242// String returns the name of the OsType.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700243func (os OsType) String() string {
244 return os.Name
Colin Cross54c71122016-06-01 17:09:44 -0700245}
246
Colin Crossa6845402020-11-16 15:08:19 -0800247// Bionic returns true if the OS uses the Bionic libc runtime, i.e. if the OS is Android or
248// is Linux with Bionic.
Dan Willemsen866b5632017-09-22 12:28:24 -0700249func (os OsType) Bionic() bool {
250 return os == Android || os == LinuxBionic
251}
252
Colin Crossa6845402020-11-16 15:08:19 -0800253// Linux returns true if the OS uses the Linux kernel, i.e. if the OS is Android or is Linux
254// with or without the Bionic libc runtime.
Dan Willemsen866b5632017-09-22 12:28:24 -0700255func (os OsType) Linux() bool {
256 return os == Android || os == Linux || os == LinuxBionic
257}
258
Colin Crossa6845402020-11-16 15:08:19 -0800259// newOsType constructs an OsType and adds it to the global lists.
260func newOsType(name string, class OsClass, defDisabled bool, archTypes ...ArchType) OsType {
261 checkCalledFromInit()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700262 os := OsType{
263 Name: name,
Colin Crossa6845402020-11-16 15:08:19 -0800264 Field: proptools.FieldNameForProperty(name),
Colin Crossa1ad8d12016-06-01 17:09:44 -0700265 Class: class,
Dan Willemsen0a37a2a2016-11-13 10:16:05 -0800266
267 DefaultDisabled: defDisabled,
Colin Cross54c71122016-06-01 17:09:44 -0700268 }
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000269 osTypeList = append(osTypeList, os)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800270
271 if _, found := commonTargetMap[name]; found {
272 panic(fmt.Errorf("Found Os type duplicate during OsType registration: %q", name))
273 } else {
Colin Crosse9fe2942020-11-10 18:12:15 -0800274 commonTargetMap[name] = Target{Os: os, Arch: CommonArch}
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800275 }
Colin Crossa6845402020-11-16 15:08:19 -0800276 osArchTypeMap[os] = archTypes
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800277
Colin Crossa1ad8d12016-06-01 17:09:44 -0700278 return os
279}
280
Colin Crossa6845402020-11-16 15:08:19 -0800281// osByName returns the OsType that has the given name, or NoOsType if none match.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700282func osByName(name string) OsType {
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000283 for _, os := range osTypeList {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700284 if os.Name == name {
285 return os
286 }
287 }
288
289 return NoOsType
Dan Willemsen490fd492015-11-24 17:53:15 -0800290}
291
Colin Crossa6845402020-11-16 15:08:19 -0800292// BuildOs returns the OsType for the OS that the build is running on.
293var BuildOs = func() OsType {
294 switch runtime.GOOS {
295 case "linux":
296 return Linux
297 case "darwin":
298 return Darwin
299 default:
300 panic(fmt.Sprintf("unsupported OS: %s", runtime.GOOS))
301 }
302}()
dimitry1f33e402019-03-26 12:39:31 +0100303
Colin Crossa6845402020-11-16 15:08:19 -0800304// BuildArch returns the ArchType for the CPU that the build is running on.
305var BuildArch = func() ArchType {
306 switch runtime.GOARCH {
307 case "amd64":
308 return X86_64
309 default:
310 panic(fmt.Sprintf("unsupported Arch: %s", runtime.GOARCH))
311 }
312}()
313
314var (
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000315 // osTypeList contains a list of all the supported OsTypes, including ones not supported
Colin Crossa6845402020-11-16 15:08:19 -0800316 // by the current build host or the target device.
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000317 osTypeList []OsType
Colin Crossa6845402020-11-16 15:08:19 -0800318 // commonTargetMap maps names of OsTypes to the corresponding common Target, i.e. the
319 // Target with the same OsType and the common ArchType.
320 commonTargetMap = make(map[string]Target)
321 // osArchTypeMap maps OsTypes to the list of supported ArchTypes for that OS.
322 osArchTypeMap = map[OsType][]ArchType{}
323
324 // NoOsType is a placeholder for when no OS is needed.
325 NoOsType OsType
326 // Linux is the OS for the Linux kernel plus the glibc runtime.
327 Linux = newOsType("linux_glibc", Host, false, X86, X86_64)
328 // Darwin is the OS for MacOS/Darwin host machines.
329 Darwin = newOsType("darwin", Host, false, X86_64)
330 // LinuxBionic is the OS for the Linux kernel plus the Bionic libc runtime, but without the
331 // rest of Android.
332 LinuxBionic = newOsType("linux_bionic", Host, false, Arm64, X86_64)
333 // Windows the OS for Windows host machines.
334 Windows = newOsType("windows", Host, true, X86, X86_64)
335 // Android is the OS for target devices that run all of Android, including the Linux kernel
336 // and the Bionic libc runtime.
337 Android = newOsType("android", Device, false, Arm, Arm64, X86, X86_64)
338 // Fuchsia is the OS for target devices that run Fuchsia.
339 Fuchsia = newOsType("fuchsia", Device, false, Arm64, X86_64)
340
341 // CommonOS is a pseudo OSType for a common OS variant, which is OsType agnostic and which
342 // has dependencies on all the OS variants.
343 CommonOS = newOsType("common_os", Generic, false)
Colin Crosse9fe2942020-11-10 18:12:15 -0800344
345 // CommonArch is the Arch for all modules that are os-specific but not arch specific,
346 // for example most Java modules.
347 CommonArch = Arch{ArchType: Common}
dimitry1f33e402019-03-26 12:39:31 +0100348)
349
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000350// OsTypeList returns a slice copy of the supported OsTypes.
351func OsTypeList() []OsType {
352 return append([]OsType(nil), osTypeList...)
353}
354
Colin Crossa6845402020-11-16 15:08:19 -0800355// Target specifies the OS and architecture that a module is being compiled for.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700356type Target struct {
Colin Crossa6845402020-11-16 15:08:19 -0800357 // Os the OS that the module is being compiled for (e.g. "linux_glibc", "android").
358 Os OsType
359 // Arch is the architecture that the module is being compiled for.
360 Arch Arch
361 // NativeBridge is NativeBridgeEnabled if the architecture is supported using NativeBridge
362 // (i.e. arm on x86) for this device.
363 NativeBridge NativeBridgeSupport
364 // NativeBridgeHostArchName is the name of the real architecture that is used to implement
365 // the NativeBridge architecture. For example, for arm on x86 this would be "x86".
dimitry8d6dde82019-07-11 10:23:53 +0200366 NativeBridgeHostArchName string
Colin Crossa6845402020-11-16 15:08:19 -0800367 // NativeBridgeRelativePath is the name of the subdirectory that will contain NativeBridge
368 // libraries and binaries.
dimitry8d6dde82019-07-11 10:23:53 +0200369 NativeBridgeRelativePath string
Jiyong Park1613e552020-09-14 19:43:17 +0900370
371 // HostCross is true when the target cannot run natively on the current build host.
372 // For example, linux_glibc_x86 returns true on a regular x86/i686/Linux machines, but returns false
373 // on Mac (different OS), or on 64-bit only i686/Linux machines (unsupported arch).
374 HostCross bool
Colin Crossd3ba0392015-05-07 14:11:29 -0700375}
376
Colin Crossa6845402020-11-16 15:08:19 -0800377// NativeBridgeSupport is an enum that specifies if a Target supports NativeBridge.
378type NativeBridgeSupport bool
379
380const (
381 NativeBridgeDisabled NativeBridgeSupport = false
382 NativeBridgeEnabled NativeBridgeSupport = true
383)
384
385// String returns the OS and arch variations used for the Target.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700386func (target Target) String() string {
Colin Crossa195f912019-10-16 11:07:20 -0700387 return target.OsVariation() + "_" + target.ArchVariation()
388}
389
Colin Crossa6845402020-11-16 15:08:19 -0800390// OsVariation returns the name of the variation used by the osMutator for the Target.
Colin Crossa195f912019-10-16 11:07:20 -0700391func (target Target) OsVariation() string {
392 return target.Os.String()
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700393}
394
Colin Crossa6845402020-11-16 15:08:19 -0800395// ArchVariation returns the name of the variation used by the archMutator for the Target.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700396func (target Target) ArchVariation() string {
397 var variation string
dimitry1f33e402019-03-26 12:39:31 +0100398 if target.NativeBridge {
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700399 variation = "native_bridge_"
dimitry1f33e402019-03-26 12:39:31 +0100400 }
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700401 variation += target.Arch.String()
402
Colin Crossa195f912019-10-16 11:07:20 -0700403 return variation
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700404}
405
Colin Crossa6845402020-11-16 15:08:19 -0800406// Variations returns a list of blueprint.Variations for the osMutator and archMutator for the
407// Target.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700408func (target Target) Variations() []blueprint.Variation {
409 return []blueprint.Variation{
Colin Crossa195f912019-10-16 11:07:20 -0700410 {Mutator: "os", Variation: target.OsVariation()},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700411 {Mutator: "arch", Variation: target.ArchVariation()},
412 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800413}
414
Liz Kammer4562a3b2021-04-21 18:15:34 -0400415func registerBp2buildArchPathDepsMutator(ctx RegisterMutatorsContext) {
416 ctx.BottomUp("bp2build-arch-pathdeps", bp2buildArchPathDepsMutator).Parallel()
417}
418
419// add dependencies for architecture specific properties tagged with `android:"path"`
420func bp2buildArchPathDepsMutator(ctx BottomUpMutatorContext) {
421 var module Module
422 module = ctx.Module()
423
424 m := module.base()
425 if !m.ArchSpecific() {
426 return
427 }
428
429 // addPathDepsForProps does not descend into sub structs, so we need to descend into the
430 // arch-specific properties ourselves
431 properties := []interface{}{}
432 for _, archProperties := range m.archProperties {
433 for _, archProps := range archProperties {
434 archPropValues := reflect.ValueOf(archProps).Elem()
435 // there are three "arch" variations, descend into each
436 for _, variant := range []string{"Arch", "Multilib", "Target"} {
437 // The properties are an interface, get the value (a pointer) that it points to
438 archProps := archPropValues.FieldByName(variant).Elem()
439 if archProps.IsNil() {
440 continue
441 }
442 // And then a pointer to a struct
443 archProps = archProps.Elem()
444 for i := 0; i < archProps.NumField(); i += 1 {
445 f := archProps.Field(i)
446 // If the value of the field is a struct (as opposed to a pointer to a struct) then step
447 // into the BlueprintEmbed field.
448 if f.Kind() == reflect.Struct {
449 f = f.FieldByName("BlueprintEmbed")
450 }
451 if f.IsZero() {
452 continue
453 }
454 props := f.Interface().(interface{})
455 properties = append(properties, props)
456 }
457 }
458 }
459 }
460 addPathDepsForProps(ctx, properties)
461}
462
Colin Crossa6845402020-11-16 15:08:19 -0800463// osMutator splits an arch-specific module into a variant for each OS that is enabled for the
464// module. It uses the HostOrDevice value passed to InitAndroidArchModule and the
465// device_supported and host_supported properties to determine which OsTypes are enabled for this
466// module, then searches through the Targets to determine which have enabled Targets for this
467// module.
Colin Cross617b88a2020-08-24 18:04:09 -0700468func osMutator(bpctx blueprint.BottomUpMutatorContext) {
Colin Crossa195f912019-10-16 11:07:20 -0700469 var module Module
470 var ok bool
Colin Cross617b88a2020-08-24 18:04:09 -0700471 if module, ok = bpctx.Module().(Module); !ok {
Colin Crossa6845402020-11-16 15:08:19 -0800472 // The module is not a Soong module, it is a Blueprint module.
Colin Cross617b88a2020-08-24 18:04:09 -0700473 if bootstrap.IsBootstrapModule(bpctx.Module()) {
474 // Bootstrap Go modules are always the build OS or linux bionic.
475 config := bpctx.Config().(Config)
476 osNames := []string{config.BuildOSTarget.OsVariation()}
477 for _, hostCrossTarget := range config.Targets[LinuxBionic] {
478 if hostCrossTarget.Arch.ArchType == config.BuildOSTarget.Arch.ArchType {
479 osNames = append(osNames, hostCrossTarget.OsVariation())
480 }
481 }
482 osNames = FirstUniqueStrings(osNames)
483 bpctx.CreateVariations(osNames...)
484 }
Colin Crossa195f912019-10-16 11:07:20 -0700485 return
486 }
487
Colin Cross617b88a2020-08-24 18:04:09 -0700488 // Bootstrap Go module support above requires this mutator to be a
489 // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext
490 // filters out non-Soong modules. Now that we've handled them, create a
491 // normal android.BottomUpMutatorContext.
Liz Kammer356f7d42021-01-26 09:18:53 -0500492 mctx := bottomUpMutatorContextFactory(bpctx, module, false, false)
Colin Cross617b88a2020-08-24 18:04:09 -0700493
Colin Crossa195f912019-10-16 11:07:20 -0700494 base := module.base()
495
Colin Crossa6845402020-11-16 15:08:19 -0800496 // Nothing to do for modules that are not architecture specific (e.g. a genrule).
Colin Crossa195f912019-10-16 11:07:20 -0700497 if !base.ArchSpecific() {
498 return
499 }
500
Colin Crossa6845402020-11-16 15:08:19 -0800501 // Collect a list of OSTypes supported by this module based on the HostOrDevice value
502 // passed to InitAndroidArchModule and the device_supported and host_supported properties.
Colin Crossa195f912019-10-16 11:07:20 -0700503 var moduleOSList []OsType
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000504 for _, os := range osTypeList {
Jiyong Park1613e552020-09-14 19:43:17 +0900505 for _, t := range mctx.Config().Targets[os] {
Colin Cross08d6f8f2020-11-19 02:33:19 +0000506 if base.supportsTarget(t) {
Jiyong Park1613e552020-09-14 19:43:17 +0900507 moduleOSList = append(moduleOSList, os)
508 break
Colin Crossa195f912019-10-16 11:07:20 -0700509 }
510 }
Colin Crossa195f912019-10-16 11:07:20 -0700511 }
512
Colin Crossa6845402020-11-16 15:08:19 -0800513 // If there are no supported OSes then disable the module.
Colin Crossa195f912019-10-16 11:07:20 -0700514 if len(moduleOSList) == 0 {
Inseob Kimeec88e12020-01-22 11:11:29 +0900515 base.Disable()
Colin Crossa195f912019-10-16 11:07:20 -0700516 return
517 }
518
Colin Crossa6845402020-11-16 15:08:19 -0800519 // Convert the list of supported OsTypes to the variation names.
Colin Crossa195f912019-10-16 11:07:20 -0700520 osNames := make([]string, len(moduleOSList))
Colin Crossa195f912019-10-16 11:07:20 -0700521 for i, os := range moduleOSList {
522 osNames[i] = os.String()
523 }
524
Paul Duffin1356d8c2020-02-25 19:26:33 +0000525 createCommonOSVariant := base.commonProperties.CreateCommonOSVariant
526 if createCommonOSVariant {
Colin Crossa6845402020-11-16 15:08:19 -0800527 // A CommonOS variant was requested so add it to the list of OS variants to
Paul Duffin1356d8c2020-02-25 19:26:33 +0000528 // create. It needs to be added to the end because it needs to depend on the
529 // the other variants in the list returned by CreateVariations(...) and inter
530 // variant dependencies can only be created from a later variant in that list to
531 // an earlier one. That is because variants are always processed in the order in
532 // which they are returned from CreateVariations(...).
533 osNames = append(osNames, CommonOS.Name)
534 moduleOSList = append(moduleOSList, CommonOS)
Colin Crossa195f912019-10-16 11:07:20 -0700535 }
536
Colin Crossa6845402020-11-16 15:08:19 -0800537 // Create the variations, annotate each one with which OS it was created for, and
538 // squash the appropriate OS-specific properties into the top level properties.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000539 modules := mctx.CreateVariations(osNames...)
540 for i, m := range modules {
541 m.base().commonProperties.CompileOS = moduleOSList[i]
542 m.base().setOSProperties(mctx)
543 }
544
545 if createCommonOSVariant {
546 // A CommonOS variant was requested so add dependencies from it (the last one in
547 // the list) to the OS type specific variants.
548 last := len(modules) - 1
549 commonOSVariant := modules[last]
550 commonOSVariant.base().commonProperties.CommonOSVariant = true
551 for _, module := range modules[0:last] {
552 // Ignore modules that are enabled. Note, this will only avoid adding
553 // dependencies on OsType variants that are explicitly disabled in their
554 // properties. The CommonOS variant will still depend on disabled variants
555 // if they are disabled afterwards, e.g. in archMutator if
556 if module.Enabled() {
557 mctx.AddInterVariantDependency(commonOsToOsSpecificVariantTag, commonOSVariant, module)
558 }
559 }
560 }
561}
562
Colin Crossc179ea62020-10-09 10:54:15 -0700563type archDepTag struct {
564 blueprint.BaseDependencyTag
565 name string
566}
Paul Duffin1356d8c2020-02-25 19:26:33 +0000567
Colin Crossc179ea62020-10-09 10:54:15 -0700568// Identifies the dependency from CommonOS variant to the os specific variants.
569var commonOsToOsSpecificVariantTag = archDepTag{name: "common os to os specific"}
570
Paul Duffin1356d8c2020-02-25 19:26:33 +0000571// Get the OsType specific variants for the current CommonOS variant.
572//
573// The returned list will only contain enabled OsType specific variants of the
574// module referenced in the supplied context. An empty list is returned if there
575// are no enabled variants or the supplied context is not for an CommonOS
576// variant.
577func GetOsSpecificVariantsOfCommonOSVariant(mctx BaseModuleContext) []Module {
578 var variants []Module
579 mctx.VisitDirectDeps(func(m Module) {
580 if mctx.OtherModuleDependencyTag(m) == commonOsToOsSpecificVariantTag {
581 if m.Enabled() {
582 variants = append(variants, m)
583 }
584 }
585 })
Paul Duffin1356d8c2020-02-25 19:26:33 +0000586 return variants
Colin Crossa195f912019-10-16 11:07:20 -0700587}
588
Colin Crossee0bc3b2018-10-02 22:01:37 -0700589// archMutator splits a module into a variant for each Target requested by the module. Target selection
Colin Crossa6845402020-11-16 15:08:19 -0800590// for a module is in three levels, OsClass, multilib, and then Target.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700591// OsClass selection is determined by:
592// - The HostOrDeviceSupported value passed in to InitAndroidArchModule by the module type factory, which selects
593// whether the module type can compile for host, device or both.
594// - The host_supported and device_supported properties on the module.
Roland Levillainf5b635d2019-06-05 14:42:57 +0100595// 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 -0700596// for the module, the Device OsClass is selected.
597// Within each selected OsClass, the multilib selection is determined by:
Jaewoong Jung02b2d4d2019-06-06 15:19:57 -0700598// - The compile_multilib property if it set (which may be overridden by target.android.compile_multilib or
Colin Crossee0bc3b2018-10-02 22:01:37 -0700599// target.host.compile_multilib).
600// - The default multilib passed to InitAndroidArchModule if compile_multilib was not set.
601// Valid multilib values include:
602// "both": compile for all Targets supported by the OsClass (generally x86_64 and x86, or arm64 and arm).
603// "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 -0700604// but may be arm for a 32-bit only build.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700605// "32": compile for only a single 32-bit Target supported by the OsClass.
606// "64": compile for only a single 64-bit Target supported by the OsClass.
Colin Crossa6845402020-11-16 15:08:19 -0800607// "common": compile a for a single Target that will work on all Targets supported by the OsClass (for example Java).
608// "common_first": compile a for a Target that will work on all Targets supported by the OsClass
609// (same as "common"), plus a second Target for the preferred Target supported by the OsClass
610// (same as "first"). This is used for java_binary that produces a common .jar and a wrapper
611// executable script.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700612//
613// Once the list of Targets is determined, the module is split into a variant for each Target.
614//
615// Modules can be initialized with InitAndroidMultiTargetsArchModule, in which case they will be split by OsClass,
616// 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 -0700617func archMutator(bpctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700618 var module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800619 var ok bool
Colin Cross617b88a2020-08-24 18:04:09 -0700620 if module, ok = bpctx.Module().(Module); !ok {
621 if bootstrap.IsBootstrapModule(bpctx.Module()) {
622 // Bootstrap Go modules are always the build architecture.
623 bpctx.CreateVariations(bpctx.Config().(Config).BuildOSTarget.ArchVariation())
624 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800625 return
626 }
627
Colin Cross617b88a2020-08-24 18:04:09 -0700628 // Bootstrap Go module support above requires this mutator to be a
629 // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext
630 // filters out non-Soong modules. Now that we've handled them, create a
631 // normal android.BottomUpMutatorContext.
Liz Kammer356f7d42021-01-26 09:18:53 -0500632 mctx := bottomUpMutatorContextFactory(bpctx, module, false, false)
Colin Cross617b88a2020-08-24 18:04:09 -0700633
Colin Cross5eca7cb2018-10-02 14:02:10 -0700634 base := module.base()
635
636 if !base.ArchSpecific() {
Colin Crossb9db4802016-06-03 01:50:47 +0000637 return
638 }
639
Colin Crossa195f912019-10-16 11:07:20 -0700640 os := base.commonProperties.CompileOS
Paul Duffin1356d8c2020-02-25 19:26:33 +0000641 if os == CommonOS {
642 // Make sure that the target related properties are initialized for the
643 // CommonOS variant.
644 addTargetProperties(module, commonTargetMap[os.Name], nil, true)
645
646 // Do not create arch specific variants for the CommonOS variant.
647 return
648 }
649
Colin Crossa195f912019-10-16 11:07:20 -0700650 osTargets := mctx.Config().Targets[os]
Colin Crossfb0c16e2019-11-20 17:12:35 -0800651 image := base.commonProperties.ImageVariation
Colin Crossa6845402020-11-16 15:08:19 -0800652 // Filter NativeBridge targets unless they are explicitly supported.
653 // Skip creating native bridge variants for non-core modules.
Colin Cross83bead42019-12-18 10:45:46 -0800654 if os == Android &&
655 !(Bool(base.commonProperties.Native_bridge_supported) && image == CoreVariation) {
656
Colin Crossa195f912019-10-16 11:07:20 -0700657 var targets []Target
658 for _, t := range osTargets {
659 if !t.NativeBridge {
660 targets = append(targets, t)
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700661 }
662 }
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700663
Colin Crossa195f912019-10-16 11:07:20 -0700664 osTargets = targets
665 }
Colin Crossee0bc3b2018-10-02 22:01:37 -0700666
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700667 // only the primary arch in the ramdisk / vendor_ramdisk / recovery partition
Inseob Kimaeb6bad2021-04-22 23:14:58 +0000668 if os == Android && (module.InstallInRecovery() || module.InstallInRamdisk() || module.InstallInVendorRamdisk()) {
Colin Crossa195f912019-10-16 11:07:20 -0700669 osTargets = []Target{osTargets[0]}
670 }
dimitry1f33e402019-03-26 12:39:31 +0100671
Jaewoong Jung003d8082021-02-24 17:39:54 -0800672 // Windows builds always prefer 32-bit
673 prefer32 := os == Windows
dimitry1f33e402019-03-26 12:39:31 +0100674
Colin Crossa6845402020-11-16 15:08:19 -0800675 // Determine the multilib selection for this module.
Colin Crossa195f912019-10-16 11:07:20 -0700676 multilib, extraMultilib := decodeMultilib(base, os.Class)
Colin Crossa6845402020-11-16 15:08:19 -0800677
678 // Convert the multilib selection into a list of Targets.
Colin Crossa195f912019-10-16 11:07:20 -0700679 targets, err := decodeMultilibTargets(multilib, osTargets, prefer32)
680 if err != nil {
681 mctx.ModuleErrorf("%s", err.Error())
682 }
Colin Cross5eca7cb2018-10-02 14:02:10 -0700683
Colin Crossa6845402020-11-16 15:08:19 -0800684 // If the module is using extraMultilib, decode the extraMultilib selection into
685 // a separate list of Targets.
Colin Crossa195f912019-10-16 11:07:20 -0700686 var multiTargets []Target
687 if extraMultilib != "" {
688 multiTargets, err = decodeMultilibTargets(extraMultilib, osTargets, prefer32)
Colin Crossa1ad8d12016-06-01 17:09:44 -0700689 if err != nil {
690 mctx.ModuleErrorf("%s", err.Error())
691 }
Colin Crossb9db4802016-06-03 01:50:47 +0000692 }
693
Colin Crossa6845402020-11-16 15:08:19 -0800694 // Recovery is always the primary architecture, filter out any other architectures.
Inseob Kim20fb5d42021-02-02 20:07:58 +0900695 // Common arch is also allowed
Colin Crossfb0c16e2019-11-20 17:12:35 -0800696 if image == RecoveryVariation {
697 primaryArch := mctx.Config().DevicePrimaryArchType()
Inseob Kim20fb5d42021-02-02 20:07:58 +0900698 targets = filterToArch(targets, primaryArch, Common)
699 multiTargets = filterToArch(multiTargets, primaryArch, Common)
Colin Crossfb0c16e2019-11-20 17:12:35 -0800700 }
701
Colin Crossa6845402020-11-16 15:08:19 -0800702 // If there are no supported targets disable the module.
Colin Crossa195f912019-10-16 11:07:20 -0700703 if len(targets) == 0 {
Inseob Kimeec88e12020-01-22 11:11:29 +0900704 base.Disable()
Dan Willemsen3f32f032016-07-11 14:36:48 -0700705 return
706 }
707
Colin Crossa6845402020-11-16 15:08:19 -0800708 // Convert the targets into a list of arch variation names.
Colin Crossa195f912019-10-16 11:07:20 -0700709 targetNames := make([]string, len(targets))
Colin Crossa195f912019-10-16 11:07:20 -0700710 for i, target := range targets {
711 targetNames[i] = target.ArchVariation()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700712 }
713
Colin Crossa6845402020-11-16 15:08:19 -0800714 // Create the variations, annotate each one with which Target it was created for, and
715 // squash the appropriate arch-specific properties into the top level properties.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700716 modules := mctx.CreateVariations(targetNames...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800717 for i, m := range modules {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000718 addTargetProperties(m, targets[i], multiTargets, i == 0)
Colin Cross617b88a2020-08-24 18:04:09 -0700719 m.base().setArchProperties(mctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800720 }
721}
722
Colin Crossa6845402020-11-16 15:08:19 -0800723// addTargetProperties annotates a variant with the Target is is being compiled for, the list
724// of additional Targets it is supporting (if any), and whether it is the primary Target for
725// the module.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000726func addTargetProperties(m Module, target Target, multiTargets []Target, primaryTarget bool) {
727 m.base().commonProperties.CompileTarget = target
728 m.base().commonProperties.CompileMultiTargets = multiTargets
729 m.base().commonProperties.CompilePrimary = primaryTarget
730}
731
Colin Crossa6845402020-11-16 15:08:19 -0800732// decodeMultilib returns the appropriate compile_multilib property for the module, or the default
733// multilib from the factory's call to InitAndroidArchModule if none was set. For modules that
734// called InitAndroidMultiTargetsArchModule it always returns "common" for multilib, and returns
735// the actual multilib in extraMultilib.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700736func decodeMultilib(base *ModuleBase, class OsClass) (multilib, extraMultilib string) {
Colin Crossa6845402020-11-16 15:08:19 -0800737 // First check the "android.compile_multilib" or "host.compile_multilib" properties.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700738 switch class {
739 case Device:
740 multilib = String(base.commonProperties.Target.Android.Compile_multilib)
Jiyong Park1613e552020-09-14 19:43:17 +0900741 case Host:
Colin Crossee0bc3b2018-10-02 22:01:37 -0700742 multilib = String(base.commonProperties.Target.Host.Compile_multilib)
743 }
Colin Crossa6845402020-11-16 15:08:19 -0800744
745 // If those aren't set, try the "compile_multilib" property.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700746 if multilib == "" {
747 multilib = String(base.commonProperties.Compile_multilib)
748 }
Colin Crossa6845402020-11-16 15:08:19 -0800749
750 // If that wasn't set, use the default multilib set by the factory.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700751 if multilib == "" {
752 multilib = base.commonProperties.Default_multilib
753 }
754
755 if base.commonProperties.UseTargetVariants {
756 return multilib, ""
757 } else {
758 // For app modules a single arch variant will be created per OS class which is expected to handle all the
759 // selected arches. Return the common-type as multilib and any Android.bp provided multilib as extraMultilib
760 if multilib == base.commonProperties.Default_multilib {
761 multilib = "first"
762 }
763 return base.commonProperties.Default_multilib, multilib
764 }
765}
766
Colin Crossa6845402020-11-16 15:08:19 -0800767// filterToArch takes a list of Targets and an ArchType, and returns a modified list that contains
Inseob Kim20fb5d42021-02-02 20:07:58 +0900768// only Targets that have the specified ArchTypes.
769func filterToArch(targets []Target, archs ...ArchType) []Target {
Colin Crossfb0c16e2019-11-20 17:12:35 -0800770 for i := 0; i < len(targets); i++ {
Inseob Kim20fb5d42021-02-02 20:07:58 +0900771 found := false
772 for _, arch := range archs {
773 if targets[i].Arch.ArchType == arch {
774 found = true
775 break
776 }
777 }
778 if !found {
Colin Crossfb0c16e2019-11-20 17:12:35 -0800779 targets = append(targets[:i], targets[i+1:]...)
780 i--
781 }
782 }
783 return targets
784}
785
Colin Crossa6845402020-11-16 15:08:19 -0800786// archPropRoot is a struct type used as the top level of the arch-specific properties. It
787// contains the "arch", "multilib", and "target" property structs. It is used to split up the
788// property structs to limit how much is allocated when a single arch-specific property group is
789// used. The types are interface{} because they will hold instances of runtime-created types.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800790type archPropRoot struct {
791 Arch, Multilib, Target interface{}
792}
793
Colin Crossa6845402020-11-16 15:08:19 -0800794// archPropTypeDesc holds the runtime-created types for the property structs to instantiate to
795// create an archPropRoot property struct.
796type archPropTypeDesc struct {
797 arch, multilib, target reflect.Type
798}
799
Colin Crosscbbd13f2020-01-17 14:08:22 -0800800// createArchPropTypeDesc takes a reflect.Type that is either a struct or a pointer to a struct, and
801// returns lists of reflect.Types that contains the arch-variant properties inside structs for each
802// arch, multilib and target property.
Colin Crossa6845402020-11-16 15:08:19 -0800803//
804// This is a relatively expensive operation, so the results are cached in the global
805// archPropTypeMap. It is constructed entirely based on compile-time data, so there is no need
806// to isolate the results between multiple tests running in parallel.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800807func createArchPropTypeDesc(props reflect.Type) []archPropTypeDesc {
Colin Crossb1d8c992020-01-21 11:43:29 -0800808 // Each property struct shard will be nested many times under the runtime generated arch struct,
809 // which can hit the limit of 64kB for the name of runtime generated structs. They are nested
810 // 97 times now, which may grow in the future, plus there is some overhead for the containing
811 // type. This number may need to be reduced if too many are added, but reducing it too far
812 // could cause problems if a single deeply nested property no longer fits in the name.
813 const maxArchTypeNameSize = 500
814
Colin Crossa6845402020-11-16 15:08:19 -0800815 // Convert the type to a new set of types that contains only the arch-specific properties
816 // (those that are tagged with `android:"arch_specific"`), and sharded into multiple types
817 // to keep the runtime-generated names under the limit.
Colin Crossb1d8c992020-01-21 11:43:29 -0800818 propShards, _ := proptools.FilterPropertyStructSharded(props, maxArchTypeNameSize, filterArchStruct)
Colin Crossa6845402020-11-16 15:08:19 -0800819
820 // If the type has no arch-specific properties there is nothing to do.
Colin Crosscb988072019-01-24 14:58:11 -0800821 if len(propShards) == 0 {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700822 return nil
823 }
824
Colin Crosscbbd13f2020-01-17 14:08:22 -0800825 var ret []archPropTypeDesc
Colin Crossc17727d2018-10-24 12:42:09 -0700826 for _, props := range propShards {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700827
Colin Crossa6845402020-11-16 15:08:19 -0800828 // variantFields takes a list of variant property field names and returns a list the
829 // StructFields with the names and the type of the current shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700830 variantFields := func(names []string) []reflect.StructField {
831 ret := make([]reflect.StructField, len(names))
Dan Willemsenb1957a52016-06-23 23:44:54 -0700832
Colin Crossc17727d2018-10-24 12:42:09 -0700833 for i, name := range names {
834 ret[i].Name = name
835 ret[i].Type = props
Dan Willemsen866b5632017-09-22 12:28:24 -0700836 }
Colin Crossc17727d2018-10-24 12:42:09 -0700837
838 return ret
839 }
840
Colin Crossa6845402020-11-16 15:08:19 -0800841 // Create a type that contains the properties in this shard repeated for each
842 // architecture, architecture variant, and architecture feature.
Colin Crossc17727d2018-10-24 12:42:09 -0700843 archFields := make([]reflect.StructField, len(archTypeList))
844 for i, arch := range archTypeList {
Colin Crossa6845402020-11-16 15:08:19 -0800845 var variants []string
Colin Crossc17727d2018-10-24 12:42:09 -0700846
847 for _, archVariant := range archVariants[arch] {
848 archVariant := variantReplacer.Replace(archVariant)
849 variants = append(variants, proptools.FieldNameForProperty(archVariant))
850 }
851 for _, feature := range archFeatures[arch] {
852 feature := variantReplacer.Replace(feature)
853 variants = append(variants, proptools.FieldNameForProperty(feature))
854 }
855
Colin Crossa6845402020-11-16 15:08:19 -0800856 // Create the StructFields for each architecture variant architecture feature
857 // (e.g. "arch.arm.cortex-a53" or "arch.arm.neon").
Colin Crossc17727d2018-10-24 12:42:09 -0700858 fields := variantFields(variants)
859
Colin Crossa6845402020-11-16 15:08:19 -0800860 // Create the StructField for the architecture itself (e.g. "arch.arm"). The special
861 // "BlueprintEmbed" name is used by Blueprint to put the properties in the
862 // parent struct.
Colin Crossc17727d2018-10-24 12:42:09 -0700863 fields = append([]reflect.StructField{{
864 Name: "BlueprintEmbed",
865 Type: props,
866 Anonymous: true,
867 }}, fields...)
868
869 archFields[i] = reflect.StructField{
870 Name: arch.Field,
871 Type: reflect.StructOf(fields),
872 }
873 }
Colin Crossa6845402020-11-16 15:08:19 -0800874
875 // Create the type of the "arch" property struct for this shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700876 archType := reflect.StructOf(archFields)
877
Colin Crossa6845402020-11-16 15:08:19 -0800878 // Create the type for the "multilib" property struct for this shard, containing the
879 // "multilib.lib32" and "multilib.lib64" property structs.
Colin Crossc17727d2018-10-24 12:42:09 -0700880 multilibType := reflect.StructOf(variantFields([]string{"Lib32", "Lib64"}))
881
Colin Crossa6845402020-11-16 15:08:19 -0800882 // Start with a list of the special targets
Colin Crossc17727d2018-10-24 12:42:09 -0700883 targets := []string{
884 "Host",
885 "Android64",
886 "Android32",
887 "Bionic",
888 "Linux",
889 "Not_windows",
890 "Arm_on_x86",
891 "Arm_on_x86_64",
Victor Khimenkoc26fcf42020-05-07 22:16:33 +0200892 "Native_bridge",
Colin Crossc17727d2018-10-24 12:42:09 -0700893 }
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000894 for _, os := range osTypeList {
Colin Crossa6845402020-11-16 15:08:19 -0800895 // Add all the OSes.
Colin Crossc17727d2018-10-24 12:42:09 -0700896 targets = append(targets, os.Field)
897
Colin Crossa6845402020-11-16 15:08:19 -0800898 // Add the OS/Arch combinations, e.g. "android_arm64".
Colin Crossc17727d2018-10-24 12:42:09 -0700899 for _, archType := range osArchTypeMap[os] {
900 targets = append(targets, os.Field+"_"+archType.Name)
901
Colin Crossa6845402020-11-16 15:08:19 -0800902 // Also add the special "linux_<arch>" and "bionic_<arch>" property structs.
Colin Crossc17727d2018-10-24 12:42:09 -0700903 if os.Linux() {
904 target := "Linux_" + archType.Name
905 if !InList(target, targets) {
906 targets = append(targets, target)
907 }
908 }
909 if os.Bionic() {
910 target := "Bionic_" + archType.Name
911 if !InList(target, targets) {
912 targets = append(targets, target)
913 }
Dan Willemsen866b5632017-09-22 12:28:24 -0700914 }
915 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700916 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700917
Colin Crossa6845402020-11-16 15:08:19 -0800918 // Create the type for the "target" property struct for this shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700919 targetType := reflect.StructOf(variantFields(targets))
Colin Crosscbbd13f2020-01-17 14:08:22 -0800920
Colin Crossa6845402020-11-16 15:08:19 -0800921 // Return a descriptor of the 3 runtime-created types.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800922 ret = append(ret, archPropTypeDesc{
923 arch: reflect.PtrTo(archType),
924 multilib: reflect.PtrTo(multilibType),
925 target: reflect.PtrTo(targetType),
926 })
Colin Crossc17727d2018-10-24 12:42:09 -0700927 }
928 return ret
Dan Willemsenb1957a52016-06-23 23:44:54 -0700929}
930
Colin Crossa6845402020-11-16 15:08:19 -0800931// variantReplacer converts architecture variant or architecture feature names into names that
932// are valid for an Android.bp file.
933var variantReplacer = strings.NewReplacer("-", "_", ".", "_")
934
935// filterArchStruct returns true if the given field is an architecture specific property.
Colin Cross74449102019-09-25 11:26:40 -0700936func filterArchStruct(field reflect.StructField, prefix string) (bool, reflect.StructField) {
937 if proptools.HasTag(field, "android", "arch_variant") {
938 // The arch_variant field isn't necessary past this point
939 // Instead of wasting space, just remove it. Go also has a
940 // 16-bit limit on structure name length. The name is constructed
941 // based on the Go source representation of the structure, so
942 // the tag names count towards that length.
Colin Crossb4fecbf2020-01-21 11:38:47 -0800943
944 androidTag := field.Tag.Get("android")
945 values := strings.Split(androidTag, ",")
946
947 if string(field.Tag) != `android:"`+strings.Join(values, ",")+`"` {
948 panic(fmt.Errorf("unexpected tag format %q", field.Tag))
Colin Cross74449102019-09-25 11:26:40 -0700949 }
Liz Kammer4562a3b2021-04-21 18:15:34 -0400950 // don't delete path tag as it is needed for bp2build
Colin Crossb4fecbf2020-01-21 11:38:47 -0800951 // these tags don't need to be present in the runtime generated struct type.
Liz Kammer4562a3b2021-04-21 18:15:34 -0400952 values = RemoveListFromList(values, []string{"arch_variant", "variant_prepend"})
953 if len(values) > 0 && values[0] != "path" {
Colin Crossb4fecbf2020-01-21 11:38:47 -0800954 panic(fmt.Errorf("unknown tags %q in field %q", values, prefix+field.Name))
Liz Kammer4562a3b2021-04-21 18:15:34 -0400955 } else if len(values) == 1 {
956 field.Tag = reflect.StructTag(`android:"` + strings.Join(values, ",") + `"`)
957 } else {
958 field.Tag = ``
Colin Crossb4fecbf2020-01-21 11:38:47 -0800959 }
960
Colin Cross74449102019-09-25 11:26:40 -0700961 return true, field
962 }
963 return false, field
964}
965
Colin Crossa6845402020-11-16 15:08:19 -0800966// archPropTypeMap contains a cache of the results of createArchPropTypeDesc for each type. It is
967// shared across all Contexts, but is constructed based only on compile-time information so there
968// is no risk of contaminating one Context with data from another.
Dan Willemsenb1957a52016-06-23 23:44:54 -0700969var archPropTypeMap OncePer
970
Colin Crossa6845402020-11-16 15:08:19 -0800971// initArchModule adds the architecture-specific property structs to a Module.
972func initArchModule(m Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800973
974 base := m.base()
975
Colin Crossa6845402020-11-16 15:08:19 -0800976 // Store the original list of top level property structs
Colin Cross36242852017-06-23 15:06:31 -0700977 base.generalProperties = m.GetProperties()
Colin Cross3f40fa42015-01-30 17:27:36 -0800978
979 for _, properties := range base.generalProperties {
980 propertiesValue := reflect.ValueOf(properties)
Colin Cross62496a02016-08-08 15:49:17 -0700981 t := propertiesValue.Type()
Colin Cross3f40fa42015-01-30 17:27:36 -0800982 if propertiesValue.Kind() != reflect.Ptr {
Colin Crossca860ac2016-01-04 14:34:37 -0800983 panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
984 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800985 }
986
987 propertiesValue = propertiesValue.Elem()
988 if propertiesValue.Kind() != reflect.Struct {
Colin Crossca860ac2016-01-04 14:34:37 -0800989 panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
990 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800991 }
992
Colin Crossa6845402020-11-16 15:08:19 -0800993 // Get or create the arch-specific property struct types for this property struct type.
Colin Cross571cccf2019-02-04 11:22:08 -0800994 archPropTypes := archPropTypeMap.Once(NewCustomOnceKey(t), func() interface{} {
Colin Crosscbbd13f2020-01-17 14:08:22 -0800995 return createArchPropTypeDesc(t)
996 }).([]archPropTypeDesc)
Colin Cross3f40fa42015-01-30 17:27:36 -0800997
Colin Crossa6845402020-11-16 15:08:19 -0800998 // Instantiate one of each arch-specific property struct type and add it to the
999 // properties for the Module.
Colin Crossc17727d2018-10-24 12:42:09 -07001000 var archProperties []interface{}
1001 for _, t := range archPropTypes {
Colin Crosscbbd13f2020-01-17 14:08:22 -08001002 archProperties = append(archProperties, &archPropRoot{
1003 Arch: reflect.Zero(t.arch).Interface(),
1004 Multilib: reflect.Zero(t.multilib).Interface(),
1005 Target: reflect.Zero(t.target).Interface(),
1006 })
Dan Willemsenb1957a52016-06-23 23:44:54 -07001007 }
Colin Crossc17727d2018-10-24 12:42:09 -07001008 base.archProperties = append(base.archProperties, archProperties)
1009 m.AddProperties(archProperties...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001010 }
1011
Colin Crossa6845402020-11-16 15:08:19 -08001012 // Update the list of properties that can be set by a defaults module or a call to
1013 // AppendMatchingProperties or PrependMatchingProperties.
Colin Cross36242852017-06-23 15:06:31 -07001014 base.customizableProperties = m.GetProperties()
Colin Cross3f40fa42015-01-30 17:27:36 -08001015}
1016
Colin Crossa6845402020-11-16 15:08:19 -08001017// appendProperties squashes properties from the given field of the given src property struct
1018// into the dst property struct. Returns the reflect.Value of the field in the src property
1019// struct to be used for further appendProperties calls on fields of that property struct.
Colin Cross4157e882019-06-06 16:57:04 -07001020func (m *ModuleBase) appendProperties(ctx BottomUpMutatorContext,
Dan Willemsenb1957a52016-06-23 23:44:54 -07001021 dst interface{}, src reflect.Value, field, srcPrefix string) reflect.Value {
Colin Cross06a931b2015-10-28 17:23:31 -07001022
Colin Crossa6845402020-11-16 15:08:19 -08001023 // Step into non-nil pointers to structs in the src value.
Colin Crosscbbd13f2020-01-17 14:08:22 -08001024 if src.Kind() == reflect.Ptr {
1025 if src.IsNil() {
1026 return src
1027 }
1028 src = src.Elem()
1029 }
1030
Colin Crossa6845402020-11-16 15:08:19 -08001031 // Find the requested field in the src struct.
Dan Willemsenb1957a52016-06-23 23:44:54 -07001032 src = src.FieldByName(field)
1033 if !src.IsValid() {
Colin Crosseeabb892015-11-20 13:07:51 -08001034 ctx.ModuleErrorf("field %q does not exist", srcPrefix)
Dan Willemsenb1957a52016-06-23 23:44:54 -07001035 return src
Colin Cross85a88972015-11-23 13:29:51 -08001036 }
1037
Colin Crossa6845402020-11-16 15:08:19 -08001038 // Save the value of the field in the src struct to return.
Dan Willemsenb1957a52016-06-23 23:44:54 -07001039 ret := src
Colin Cross85a88972015-11-23 13:29:51 -08001040
Colin Crossa6845402020-11-16 15:08:19 -08001041 // If the value of the field is a struct (as opposed to a pointer to a struct) then step
1042 // into the BlueprintEmbed field.
Dan Willemsenb1957a52016-06-23 23:44:54 -07001043 if src.Kind() == reflect.Struct {
1044 src = src.FieldByName("BlueprintEmbed")
Colin Cross06a931b2015-10-28 17:23:31 -07001045 }
1046
Colin Crossa6845402020-11-16 15:08:19 -08001047 // order checks the `android:"variant_prepend"` tag to handle properties where the
1048 // arch-specific value needs to come before the generic value, for example for lists of
1049 // include directories.
Colin Cross6ee75b62016-05-05 15:57:15 -07001050 order := func(property string,
1051 dstField, srcField reflect.StructField,
1052 dstValue, srcValue interface{}) (proptools.Order, error) {
1053 if proptools.HasTag(dstField, "android", "variant_prepend") {
1054 return proptools.Prepend, nil
1055 } else {
1056 return proptools.Append, nil
1057 }
1058 }
1059
Colin Crossa6845402020-11-16 15:08:19 -08001060 // Squash the located property struct into the destination property struct.
Dan Willemsenb1957a52016-06-23 23:44:54 -07001061 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src.Interface(), nil, order)
Colin Cross06a931b2015-10-28 17:23:31 -07001062 if err != nil {
1063 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
1064 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
1065 } else {
1066 panic(err)
1067 }
1068 }
Colin Cross85a88972015-11-23 13:29:51 -08001069
Dan Willemsenb1957a52016-06-23 23:44:54 -07001070 return ret
Colin Cross06a931b2015-10-28 17:23:31 -07001071}
1072
Colin Crossa6845402020-11-16 15:08:19 -08001073// Squash the appropriate OS-specific property structs into the matching top level property structs
1074// based on the CompileOS value that was annotated on the variant.
Colin Crossa195f912019-10-16 11:07:20 -07001075func (m *ModuleBase) setOSProperties(ctx BottomUpMutatorContext) {
1076 os := m.commonProperties.CompileOS
1077
1078 for i := range m.generalProperties {
1079 genProps := m.generalProperties[i]
1080 if m.archProperties[i] == nil {
1081 continue
1082 }
1083 for _, archProperties := range m.archProperties[i] {
1084 archPropValues := reflect.ValueOf(archProperties).Elem()
1085
Colin Crosscbbd13f2020-01-17 14:08:22 -08001086 targetProp := archPropValues.FieldByName("Target").Elem()
Colin Crossa195f912019-10-16 11:07:20 -07001087
1088 // Handle host-specific properties in the form:
1089 // target: {
1090 // host: {
1091 // key: value,
1092 // },
1093 // },
Jiyong Park1613e552020-09-14 19:43:17 +09001094 if os.Class == Host {
Colin Crossa195f912019-10-16 11:07:20 -07001095 field := "Host"
1096 prefix := "target.host"
1097 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1098 }
1099
1100 // Handle target OS generalities of the form:
1101 // target: {
1102 // bionic: {
1103 // key: value,
1104 // },
1105 // }
1106 if os.Linux() {
1107 field := "Linux"
1108 prefix := "target.linux"
1109 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1110 }
1111
1112 if os.Bionic() {
1113 field := "Bionic"
1114 prefix := "target.bionic"
1115 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1116 }
1117
1118 // Handle target OS properties in the form:
1119 // target: {
1120 // linux_glibc: {
1121 // key: value,
1122 // },
1123 // not_windows: {
1124 // key: value,
1125 // },
1126 // android {
1127 // key: value,
1128 // },
1129 // },
1130 field := os.Field
1131 prefix := "target." + os.Name
1132 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1133
Jiyong Park1613e552020-09-14 19:43:17 +09001134 if os.Class == Host && os != Windows {
Colin Crossa195f912019-10-16 11:07:20 -07001135 field := "Not_windows"
1136 prefix := "target.not_windows"
1137 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1138 }
1139
1140 // Handle 64-bit device properties in the form:
1141 // target {
1142 // android64 {
1143 // key: value,
1144 // },
1145 // android32 {
1146 // key: value,
1147 // },
1148 // },
1149 // WARNING: this is probably not what you want to use in your blueprints file, it selects
1150 // options for all targets on a device that supports 64-bit binaries, not just the targets
1151 // that are being compiled for 64-bit. Its expected use case is binaries like linker and
1152 // debuggerd that need to know when they are a 32-bit process running on a 64-bit device
1153 if os.Class == Device {
1154 if ctx.Config().Android64() {
1155 field := "Android64"
1156 prefix := "target.android64"
1157 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1158 } else {
1159 field := "Android32"
1160 prefix := "target.android32"
1161 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1162 }
1163 }
1164 }
1165 }
1166}
1167
Colin Crossa6845402020-11-16 15:08:19 -08001168// Squash the appropriate arch-specific property structs into the matching top level property
1169// structs based on the CompileTarget value that was annotated on the variant.
Colin Cross4157e882019-06-06 16:57:04 -07001170func (m *ModuleBase) setArchProperties(ctx BottomUpMutatorContext) {
1171 arch := m.Arch()
1172 os := m.Os()
Colin Crossd3ba0392015-05-07 14:11:29 -07001173
Colin Cross4157e882019-06-06 16:57:04 -07001174 for i := range m.generalProperties {
1175 genProps := m.generalProperties[i]
1176 if m.archProperties[i] == nil {
Dan Willemsenb1957a52016-06-23 23:44:54 -07001177 continue
1178 }
Colin Cross4157e882019-06-06 16:57:04 -07001179 for _, archProperties := range m.archProperties[i] {
Colin Crossc17727d2018-10-24 12:42:09 -07001180 archPropValues := reflect.ValueOf(archProperties).Elem()
Dan Willemsenb1957a52016-06-23 23:44:54 -07001181
Colin Crosscbbd13f2020-01-17 14:08:22 -08001182 archProp := archPropValues.FieldByName("Arch").Elem()
1183 multilibProp := archPropValues.FieldByName("Multilib").Elem()
1184 targetProp := archPropValues.FieldByName("Target").Elem()
Dan Willemsenb1957a52016-06-23 23:44:54 -07001185
Colin Crossc17727d2018-10-24 12:42:09 -07001186 // Handle arch-specific properties in the form:
Colin Crossd5934c82017-10-02 13:55:26 -07001187 // arch: {
Colin Crossc17727d2018-10-24 12:42:09 -07001188 // arm64: {
Colin Crossd5934c82017-10-02 13:55:26 -07001189 // key: value,
1190 // },
1191 // },
Colin Crossc17727d2018-10-24 12:42:09 -07001192 t := arch.ArchType
1193
1194 if arch.ArchType != Common {
1195 field := proptools.FieldNameForProperty(t.Name)
1196 prefix := "arch." + t.Name
Colin Cross4157e882019-06-06 16:57:04 -07001197 archStruct := m.appendProperties(ctx, genProps, archProp, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001198
1199 // Handle arch-variant-specific properties in the form:
1200 // arch: {
1201 // variant: {
1202 // key: value,
1203 // },
1204 // },
1205 v := variantReplacer.Replace(arch.ArchVariant)
1206 if v != "" {
1207 field := proptools.FieldNameForProperty(v)
1208 prefix := "arch." + t.Name + "." + v
Colin Cross4157e882019-06-06 16:57:04 -07001209 m.appendProperties(ctx, genProps, archStruct, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001210 }
1211
1212 // Handle cpu-variant-specific properties in the form:
1213 // arch: {
1214 // variant: {
1215 // key: value,
1216 // },
1217 // },
1218 if arch.CpuVariant != arch.ArchVariant {
1219 c := variantReplacer.Replace(arch.CpuVariant)
1220 if c != "" {
1221 field := proptools.FieldNameForProperty(c)
1222 prefix := "arch." + t.Name + "." + c
Colin Cross4157e882019-06-06 16:57:04 -07001223 m.appendProperties(ctx, genProps, archStruct, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001224 }
1225 }
1226
1227 // Handle arch-feature-specific properties in the form:
1228 // arch: {
1229 // feature: {
1230 // key: value,
1231 // },
1232 // },
1233 for _, feature := range arch.ArchFeatures {
1234 field := proptools.FieldNameForProperty(feature)
1235 prefix := "arch." + t.Name + "." + feature
Colin Cross4157e882019-06-06 16:57:04 -07001236 m.appendProperties(ctx, genProps, archStruct, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001237 }
1238
1239 // Handle multilib-specific properties in the form:
1240 // multilib: {
1241 // lib32: {
1242 // key: value,
1243 // },
1244 // },
1245 field = proptools.FieldNameForProperty(t.Multilib)
1246 prefix = "multilib." + t.Multilib
Colin Cross4157e882019-06-06 16:57:04 -07001247 m.appendProperties(ctx, genProps, multilibProp, field, prefix)
Colin Cross08016332016-12-20 09:53:14 -08001248 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001249
Colin Crossa195f912019-10-16 11:07:20 -07001250 // Handle combined OS-feature and arch specific properties in the form:
Colin Crossc17727d2018-10-24 12:42:09 -07001251 // target: {
Colin Crossc17727d2018-10-24 12:42:09 -07001252 // bionic_x86: {
1253 // key: value,
1254 // },
1255 // }
Colin Crossa195f912019-10-16 11:07:20 -07001256 if os.Linux() && arch.ArchType != Common {
1257 field := "Linux_" + arch.ArchType.Name
1258 prefix := "target.linux_" + arch.ArchType.Name
Colin Cross4157e882019-06-06 16:57:04 -07001259 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossd5934c82017-10-02 13:55:26 -07001260 }
Colin Crossc5c24ad2015-11-20 15:35:00 -08001261
Colin Crossa195f912019-10-16 11:07:20 -07001262 if os.Bionic() && arch.ArchType != Common {
1263 field := "Bionic_" + t.Name
1264 prefix := "target.bionic_" + t.Name
Colin Cross4157e882019-06-06 16:57:04 -07001265 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001266 }
1267
Colin Crossa195f912019-10-16 11:07:20 -07001268 // Handle combined OS and arch specific properties in the form:
Colin Crossc17727d2018-10-24 12:42:09 -07001269 // target: {
Colin Crossc17727d2018-10-24 12:42:09 -07001270 // linux_glibc_x86: {
1271 // key: value,
1272 // },
1273 // linux_glibc_arm: {
1274 // key: value,
1275 // },
Colin Crossc17727d2018-10-24 12:42:09 -07001276 // android_arm {
1277 // key: value,
1278 // },
1279 // android_x86 {
Colin Crossd5934c82017-10-02 13:55:26 -07001280 // key: value,
1281 // },
1282 // },
Colin Crossc17727d2018-10-24 12:42:09 -07001283 if arch.ArchType != Common {
Colin Crossa195f912019-10-16 11:07:20 -07001284 field := os.Field + "_" + t.Name
1285 prefix := "target." + os.Name + "_" + t.Name
Colin Cross4157e882019-06-06 16:57:04 -07001286 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossd5934c82017-10-02 13:55:26 -07001287 }
1288
Colin Crossa195f912019-10-16 11:07:20 -07001289 // Handle arm on x86 properties in the form:
Colin Crossc17727d2018-10-24 12:42:09 -07001290 // target {
Colin Crossa195f912019-10-16 11:07:20 -07001291 // arm_on_x86 {
Colin Crossc17727d2018-10-24 12:42:09 -07001292 // key: value,
1293 // },
Colin Crossa195f912019-10-16 11:07:20 -07001294 // arm_on_x86_64 {
Colin Crossd5934c82017-10-02 13:55:26 -07001295 // key: value,
1296 // },
1297 // },
Colin Crossc17727d2018-10-24 12:42:09 -07001298 if os.Class == Device {
Victor Khimenko1a31f802020-09-17 03:07:31 +02001299 if arch.ArchType == X86 && (hasArmAbi(arch) ||
1300 hasArmAndroidArch(ctx.Config().Targets[Android])) {
Colin Crossc17727d2018-10-24 12:42:09 -07001301 field := "Arm_on_x86"
1302 prefix := "target.arm_on_x86"
Colin Cross4157e882019-06-06 16:57:04 -07001303 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001304 }
Victor Khimenko1a31f802020-09-17 03:07:31 +02001305 if arch.ArchType == X86_64 && (hasArmAbi(arch) ||
1306 hasArmAndroidArch(ctx.Config().Targets[Android])) {
Colin Crossc17727d2018-10-24 12:42:09 -07001307 field := "Arm_on_x86_64"
1308 prefix := "target.arm_on_x86_64"
Colin Cross4157e882019-06-06 16:57:04 -07001309 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001310 }
Victor Khimenkoc26fcf42020-05-07 22:16:33 +02001311 if os == Android && m.Target().NativeBridge == NativeBridgeEnabled {
1312 field := "Native_bridge"
1313 prefix := "target.native_bridge"
1314 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1315 }
Colin Cross4247f0d2017-04-13 16:56:14 -07001316 }
Colin Crossbb2e2b72016-12-08 17:23:53 -08001317 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001318 }
1319}
1320
Colin Crossa6845402020-11-16 15:08:19 -08001321// Convert the arch product variables into a list of targets for each OsType.
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001322func decodeTargetProductVariables(config *config) (map[OsType][]Target, error) {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001323 variables := config.productVariables
Dan Willemsen490fd492015-11-24 17:53:15 -08001324
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001325 targets := make(map[OsType][]Target)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001326 var targetErr error
1327
dimitry1f33e402019-03-26 12:39:31 +01001328 addTarget := func(os OsType, archName string, archVariant, cpuVariant *string, abi []string,
dimitry8d6dde82019-07-11 10:23:53 +02001329 nativeBridgeEnabled NativeBridgeSupport, nativeBridgeHostArchName *string,
1330 nativeBridgeRelativePath *string) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001331 if targetErr != nil {
1332 return
Dan Willemsen490fd492015-11-24 17:53:15 -08001333 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001334
Dan Willemsen01a3c252019-01-11 19:02:16 -08001335 arch, err := decodeArch(os, archName, archVariant, cpuVariant, abi)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001336 if err != nil {
1337 targetErr = err
1338 return
1339 }
dimitry8d6dde82019-07-11 10:23:53 +02001340 nativeBridgeRelativePathStr := String(nativeBridgeRelativePath)
1341 nativeBridgeHostArchNameStr := String(nativeBridgeHostArchName)
1342
1343 // Use guest arch as relative install path by default
1344 if nativeBridgeEnabled && nativeBridgeRelativePathStr == "" {
1345 nativeBridgeRelativePathStr = arch.ArchType.String()
1346 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001347
Jiyong Park1613e552020-09-14 19:43:17 +09001348 // A target is considered as HostCross if it's a host target which can't run natively on
1349 // the currently configured build machine (either because the OS is different or because of
1350 // the unsupported arch)
1351 hostCross := false
1352 if os.Class == Host {
1353 var osSupported bool
1354 if os == BuildOs {
1355 osSupported = true
1356 } else if BuildOs.Linux() && os.Linux() {
1357 // LinuxBionic and Linux are compatible
1358 osSupported = true
1359 } else {
1360 osSupported = false
1361 }
1362
1363 var archSupported bool
1364 if arch.ArchType == Common {
1365 archSupported = true
1366 } else if arch.ArchType.Name == *variables.HostArch {
1367 archSupported = true
1368 } else if variables.HostSecondaryArch != nil && arch.ArchType.Name == *variables.HostSecondaryArch {
1369 archSupported = true
1370 } else {
1371 archSupported = false
1372 }
1373 if !osSupported || !archSupported {
1374 hostCross = true
1375 }
1376 }
1377
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001378 targets[os] = append(targets[os],
Colin Crossa1ad8d12016-06-01 17:09:44 -07001379 Target{
dimitry8d6dde82019-07-11 10:23:53 +02001380 Os: os,
1381 Arch: arch,
1382 NativeBridge: nativeBridgeEnabled,
1383 NativeBridgeHostArchName: nativeBridgeHostArchNameStr,
1384 NativeBridgeRelativePath: nativeBridgeRelativePathStr,
Jiyong Park1613e552020-09-14 19:43:17 +09001385 HostCross: hostCross,
Colin Crossa1ad8d12016-06-01 17:09:44 -07001386 })
Dan Willemsen490fd492015-11-24 17:53:15 -08001387 }
1388
Colin Cross4225f652015-09-17 14:33:42 -07001389 if variables.HostArch == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001390 return nil, fmt.Errorf("No host primary architecture set")
Colin Cross4225f652015-09-17 14:33:42 -07001391 }
1392
Colin Crossa6845402020-11-16 15:08:19 -08001393 // The primary host target, which must always exist.
dimitry8d6dde82019-07-11 10:23:53 +02001394 addTarget(BuildOs, *variables.HostArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -07001395
Colin Crossa6845402020-11-16 15:08:19 -08001396 // An optional secondary host target.
Colin Crosseeabb892015-11-20 13:07:51 -08001397 if variables.HostSecondaryArch != nil && *variables.HostSecondaryArch != "" {
dimitry8d6dde82019-07-11 10:23:53 +02001398 addTarget(BuildOs, *variables.HostSecondaryArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -08001399 }
1400
Colin Crossa6845402020-11-16 15:08:19 -08001401 // Optional cross-compiled host targets, generally Windows.
Colin Crossff3ae9d2018-04-10 16:15:18 -07001402 if String(variables.CrossHost) != "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001403 crossHostOs := osByName(*variables.CrossHost)
1404 if crossHostOs == NoOsType {
1405 return nil, fmt.Errorf("Unknown cross host OS %q", *variables.CrossHost)
1406 }
1407
Colin Crossff3ae9d2018-04-10 16:15:18 -07001408 if String(variables.CrossHostArch) == "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001409 return nil, fmt.Errorf("No cross-host primary architecture set")
Dan Willemsen490fd492015-11-24 17:53:15 -08001410 }
1411
Colin Crossa6845402020-11-16 15:08:19 -08001412 // The primary cross-compiled host target.
dimitry8d6dde82019-07-11 10:23:53 +02001413 addTarget(crossHostOs, *variables.CrossHostArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -08001414
Colin Crossa6845402020-11-16 15:08:19 -08001415 // An optional secondary cross-compiled host target.
Dan Willemsen490fd492015-11-24 17:53:15 -08001416 if variables.CrossHostSecondaryArch != nil && *variables.CrossHostSecondaryArch != "" {
dimitry8d6dde82019-07-11 10:23:53 +02001417 addTarget(crossHostOs, *variables.CrossHostSecondaryArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -08001418 }
1419 }
1420
Colin Crossa6845402020-11-16 15:08:19 -08001421 // Optional device targets
Dan Willemsen3f32f032016-07-11 14:36:48 -07001422 if variables.DeviceArch != nil && *variables.DeviceArch != "" {
Doug Horn21b94272019-01-16 12:06:11 -08001423 var target = Android
1424 if Bool(variables.Fuchsia) {
1425 target = Fuchsia
1426 }
1427
Colin Crossa6845402020-11-16 15:08:19 -08001428 // The primary device target.
Doug Horn21b94272019-01-16 12:06:11 -08001429 addTarget(target, *variables.DeviceArch, variables.DeviceArchVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001430 variables.DeviceCpuVariant, variables.DeviceAbi, NativeBridgeDisabled, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -07001431
Colin Crossa6845402020-11-16 15:08:19 -08001432 // An optional secondary device target.
Dan Willemsen3f32f032016-07-11 14:36:48 -07001433 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" {
1434 addTarget(Android, *variables.DeviceSecondaryArch,
1435 variables.DeviceSecondaryArchVariant, variables.DeviceSecondaryCpuVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001436 variables.DeviceSecondaryAbi, NativeBridgeDisabled, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -07001437 }
dimitry1f33e402019-03-26 12:39:31 +01001438
Colin Crossa6845402020-11-16 15:08:19 -08001439 // An optional NativeBridge device target.
dimitry1f33e402019-03-26 12:39:31 +01001440 if variables.NativeBridgeArch != nil && *variables.NativeBridgeArch != "" {
1441 addTarget(Android, *variables.NativeBridgeArch,
1442 variables.NativeBridgeArchVariant, variables.NativeBridgeCpuVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001443 variables.NativeBridgeAbi, NativeBridgeEnabled, variables.DeviceArch,
1444 variables.NativeBridgeRelativePath)
dimitry1f33e402019-03-26 12:39:31 +01001445 }
1446
Colin Crossa6845402020-11-16 15:08:19 -08001447 // An optional secondary NativeBridge device target.
dimitry1f33e402019-03-26 12:39:31 +01001448 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" &&
1449 variables.NativeBridgeSecondaryArch != nil && *variables.NativeBridgeSecondaryArch != "" {
1450 addTarget(Android, *variables.NativeBridgeSecondaryArch,
1451 variables.NativeBridgeSecondaryArchVariant,
1452 variables.NativeBridgeSecondaryCpuVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001453 variables.NativeBridgeSecondaryAbi,
1454 NativeBridgeEnabled,
1455 variables.DeviceSecondaryArch,
1456 variables.NativeBridgeSecondaryRelativePath)
dimitry1f33e402019-03-26 12:39:31 +01001457 }
Colin Cross4225f652015-09-17 14:33:42 -07001458 }
1459
Colin Crossa1ad8d12016-06-01 17:09:44 -07001460 if targetErr != nil {
1461 return nil, targetErr
1462 }
1463
1464 return targets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001465}
1466
Colin Crossbb2e2b72016-12-08 17:23:53 -08001467// hasArmAbi returns true if arch has at least one arm ABI
1468func hasArmAbi(arch Arch) bool {
Jaewoong Jung3aff5782020-02-11 07:54:35 -08001469 return PrefixInList(arch.Abi, "arm")
Colin Crossbb2e2b72016-12-08 17:23:53 -08001470}
1471
dimitry628db6f2019-05-22 17:16:21 +02001472// hasArmArch returns true if targets has at least non-native_bridge arm Android arch
Colin Cross4247f0d2017-04-13 16:56:14 -07001473func hasArmAndroidArch(targets []Target) bool {
1474 for _, target := range targets {
Victor Khimenko1a31f802020-09-17 03:07:31 +02001475 if target.Os == Android && target.Arch.ArchType == Arm {
Victor Khimenko5eb8ec12018-03-21 20:30:54 +01001476 return true
1477 }
1478 }
1479 return false
1480}
1481
Colin Crossa6845402020-11-16 15:08:19 -08001482// archConfig describes a built-in configuration.
Dan Albert4098deb2016-10-19 14:04:41 -07001483type archConfig struct {
1484 arch string
1485 archVariant string
1486 cpuVariant string
1487 abi []string
1488}
1489
Colin Crossa6845402020-11-16 15:08:19 -08001490// getNdkAbisConfig returns a list of archConfigs for the ABIs supported by the NDK.
Dan Albert4098deb2016-10-19 14:04:41 -07001491func getNdkAbisConfig() []archConfig {
1492 return []archConfig{
Dan Albert6bba6442020-01-30 15:16:49 -08001493 {"arm", "armv7-a", "", []string{"armeabi-v7a"}},
Tamas Petzbca786d2021-01-20 18:56:33 +01001494 {"arm64", "armv8-a-branchprot", "", []string{"arm64-v8a"}},
Dan Albert4098deb2016-10-19 14:04:41 -07001495 {"x86", "", "", []string{"x86"}},
1496 {"x86_64", "", "", []string{"x86_64"}},
1497 }
1498}
1499
Colin Crossa6845402020-11-16 15:08:19 -08001500// getAmlAbisConfig returns a list of archConfigs for the ABIs supported by mainline modules.
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001501func getAmlAbisConfig() []archConfig {
1502 return []archConfig{
Martin Stjernholm93688342020-10-16 21:45:10 +01001503 {"arm", "armv7-a-neon", "", []string{"armeabi-v7a"}},
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001504 {"arm64", "armv8-a", "", []string{"arm64-v8a"}},
1505 {"x86", "", "", []string{"x86"}},
1506 {"x86_64", "", "", []string{"x86_64"}},
1507 }
1508}
1509
Colin Crossa6845402020-11-16 15:08:19 -08001510// decodeArchSettings converts a list of archConfigs into a list of Targets for the given OsType.
Dan Willemsen01a3c252019-01-11 19:02:16 -08001511func decodeArchSettings(os OsType, archConfigs []archConfig) ([]Target, error) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001512 var ret []Target
Dan Willemsen322acaf2016-01-12 23:07:05 -08001513
Dan Albert4098deb2016-10-19 14:04:41 -07001514 for _, config := range archConfigs {
Dan Willemsen01a3c252019-01-11 19:02:16 -08001515 arch, err := decodeArch(os, config.arch, &config.archVariant,
Colin Crossa74ca042019-01-31 14:31:51 -08001516 &config.cpuVariant, config.abi)
Dan Willemsen322acaf2016-01-12 23:07:05 -08001517 if err != nil {
1518 return nil, err
1519 }
Colin Cross3b19f5d2019-09-17 14:45:31 -07001520
Colin Crossa1ad8d12016-06-01 17:09:44 -07001521 ret = append(ret, Target{
1522 Os: Android,
1523 Arch: arch,
1524 })
Dan Willemsen322acaf2016-01-12 23:07:05 -08001525 }
1526
1527 return ret, nil
1528}
1529
Colin Crossa6845402020-11-16 15:08:19 -08001530// decodeArch converts a set of strings from product variables into an Arch struct.
Colin Crossa74ca042019-01-31 14:31:51 -08001531func decodeArch(os OsType, arch string, archVariant, cpuVariant *string, abi []string) (Arch, error) {
Colin Crossa6845402020-11-16 15:08:19 -08001532 // Verify the arch is valid
Colin Crosseeabb892015-11-20 13:07:51 -08001533 archType, ok := archTypeMap[arch]
1534 if !ok {
1535 return Arch{}, fmt.Errorf("unknown arch %q", arch)
1536 }
Colin Cross4225f652015-09-17 14:33:42 -07001537
Colin Crosseeabb892015-11-20 13:07:51 -08001538 a := Arch{
Colin Cross4225f652015-09-17 14:33:42 -07001539 ArchType: archType,
Colin Crossa6845402020-11-16 15:08:19 -08001540 ArchVariant: String(archVariant),
1541 CpuVariant: String(cpuVariant),
Colin Crossa74ca042019-01-31 14:31:51 -08001542 Abi: abi,
Colin Crosseeabb892015-11-20 13:07:51 -08001543 }
1544
Colin Crossa6845402020-11-16 15:08:19 -08001545 // Convert generic arch variants into the empty string.
Colin Crosseeabb892015-11-20 13:07:51 -08001546 if a.ArchVariant == a.ArchType.Name || a.ArchVariant == "generic" {
1547 a.ArchVariant = ""
1548 }
1549
Colin Crossa6845402020-11-16 15:08:19 -08001550 // Convert generic CPU variants into the empty string.
Colin Crosseeabb892015-11-20 13:07:51 -08001551 if a.CpuVariant == a.ArchType.Name || a.CpuVariant == "generic" {
1552 a.CpuVariant = ""
1553 }
1554
Colin Crossa6845402020-11-16 15:08:19 -08001555 // Filter empty ABIs out of the list.
Colin Crosseeabb892015-11-20 13:07:51 -08001556 for i := 0; i < len(a.Abi); i++ {
1557 if a.Abi[i] == "" {
1558 a.Abi = append(a.Abi[:i], a.Abi[i+1:]...)
1559 i--
1560 }
1561 }
1562
Dan Willemsen01a3c252019-01-11 19:02:16 -08001563 if a.ArchVariant == "" {
Colin Crossa6845402020-11-16 15:08:19 -08001564 // Set ArchFeatures from the default arch features.
Dan Willemsen01a3c252019-01-11 19:02:16 -08001565 if featureMap, ok := defaultArchFeatureMap[os]; ok {
1566 a.ArchFeatures = featureMap[archType]
1567 }
1568 } else {
Colin Crossa6845402020-11-16 15:08:19 -08001569 // Set ArchFeatures from the arch type.
Dan Willemsen01a3c252019-01-11 19:02:16 -08001570 if featureMap, ok := archFeatureMap[archType]; ok {
1571 a.ArchFeatures = featureMap[a.ArchVariant]
1572 }
Colin Crossc5c24ad2015-11-20 15:35:00 -08001573 }
1574
Colin Crosseeabb892015-11-20 13:07:51 -08001575 return a, nil
Colin Cross4225f652015-09-17 14:33:42 -07001576}
1577
Colin Crossa6845402020-11-16 15:08:19 -08001578// filterMultilibTargets takes a list of Targets and a multilib value and returns a new list of
1579// Targets containing only those that have the given multilib value.
Colin Cross69617d32016-09-06 10:39:07 -07001580func filterMultilibTargets(targets []Target, multilib string) []Target {
1581 var ret []Target
1582 for _, t := range targets {
1583 if t.Arch.ArchType.Multilib == multilib {
1584 ret = append(ret, t)
1585 }
1586 }
1587 return ret
1588}
1589
Colin Crossa6845402020-11-16 15:08:19 -08001590// getCommonTargets returns the set of Os specific common architecture targets for each Os in a list
1591// of targets.
Nan Zhangdb0b9a32017-02-27 10:12:13 -08001592func getCommonTargets(targets []Target) []Target {
1593 var ret []Target
1594 set := make(map[string]bool)
1595
1596 for _, t := range targets {
1597 if _, found := set[t.Os.String()]; !found {
1598 set[t.Os.String()] = true
1599 ret = append(ret, commonTargetMap[t.Os.String()])
1600 }
1601 }
1602
1603 return ret
1604}
1605
Colin Crossa6845402020-11-16 15:08:19 -08001606// firstTarget takes a list of Targets and a list of multilib values and returns a list of Targets
1607// that contains zero or one Target for each OsType, selecting the one that matches the earliest
1608// filter.
Colin Cross3dceee32018-09-06 10:19:57 -07001609func firstTarget(targets []Target, filters ...string) []Target {
Jiyong Park22101982020-09-17 19:09:58 +09001610 // find the first target from each OS
1611 var ret []Target
1612 hasHost := false
1613 set := make(map[OsType]bool)
1614
Colin Cross6b4a32d2017-12-05 13:42:45 -08001615 for _, filter := range filters {
1616 buildTargets := filterMultilibTargets(targets, filter)
Jiyong Park22101982020-09-17 19:09:58 +09001617 for _, t := range buildTargets {
1618 if _, found := set[t.Os]; !found {
1619 hasHost = hasHost || (t.Os.Class == Host)
1620 set[t.Os] = true
1621 ret = append(ret, t)
1622 }
Colin Cross6b4a32d2017-12-05 13:42:45 -08001623 }
1624 }
Jiyong Park22101982020-09-17 19:09:58 +09001625 return ret
Colin Cross6b4a32d2017-12-05 13:42:45 -08001626}
1627
Colin Crossa6845402020-11-16 15:08:19 -08001628// decodeMultilibTargets uses the module's multilib setting to select one or more targets from a
1629// list of Targets.
Colin Crossee0bc3b2018-10-02 22:01:37 -07001630func decodeMultilibTargets(multilib string, targets []Target, prefer32 bool) ([]Target, error) {
Colin Crossa6845402020-11-16 15:08:19 -08001631 var buildTargets []Target
Colin Cross6b4a32d2017-12-05 13:42:45 -08001632
Colin Cross4225f652015-09-17 14:33:42 -07001633 switch multilib {
1634 case "common":
Colin Cross6b4a32d2017-12-05 13:42:45 -08001635 buildTargets = getCommonTargets(targets)
1636 case "common_first":
1637 buildTargets = getCommonTargets(targets)
1638 if prefer32 {
Colin Cross3dceee32018-09-06 10:19:57 -07001639 buildTargets = append(buildTargets, firstTarget(targets, "lib32", "lib64")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001640 } else {
Colin Cross3dceee32018-09-06 10:19:57 -07001641 buildTargets = append(buildTargets, firstTarget(targets, "lib64", "lib32")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001642 }
Colin Cross4225f652015-09-17 14:33:42 -07001643 case "both":
Colin Cross8b74d172016-09-13 09:59:14 -07001644 if prefer32 {
1645 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1646 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1647 } else {
1648 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1649 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1650 }
Colin Cross4225f652015-09-17 14:33:42 -07001651 case "32":
Colin Cross69617d32016-09-06 10:39:07 -07001652 buildTargets = filterMultilibTargets(targets, "lib32")
Colin Cross4225f652015-09-17 14:33:42 -07001653 case "64":
Colin Cross69617d32016-09-06 10:39:07 -07001654 buildTargets = filterMultilibTargets(targets, "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001655 case "first":
1656 if prefer32 {
Colin Cross3dceee32018-09-06 10:19:57 -07001657 buildTargets = firstTarget(targets, "lib32", "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001658 } else {
Colin Cross3dceee32018-09-06 10:19:57 -07001659 buildTargets = firstTarget(targets, "lib64", "lib32")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001660 }
Victor Chang9448e8f2020-09-14 15:34:16 +01001661 case "first_prefer32":
1662 buildTargets = firstTarget(targets, "lib32", "lib64")
Colin Cross69617d32016-09-06 10:39:07 -07001663 case "prefer32":
Colin Cross3dceee32018-09-06 10:19:57 -07001664 buildTargets = filterMultilibTargets(targets, "lib32")
1665 if len(buildTargets) == 0 {
1666 buildTargets = filterMultilibTargets(targets, "lib64")
1667 }
Colin Cross4225f652015-09-17 14:33:42 -07001668 default:
Victor Chang9448e8f2020-09-14 15:34:16 +01001669 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 -07001670 multilib)
Colin Cross4225f652015-09-17 14:33:42 -07001671 }
1672
Colin Crossa1ad8d12016-06-01 17:09:44 -07001673 return buildTargets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001674}
Jingwen Chen5d864492021-02-24 07:20:12 -05001675
Chris Parsonsc424b762021-04-29 18:06:50 -04001676func (m *ModuleBase) getArchPropertySet(propertySet interface{}, archType ArchType) interface{} {
1677 archString := archType.Field
1678 for i := range m.archProperties {
1679 if m.archProperties[i] == nil {
1680 // Skip over nil properties
1681 continue
1682 }
1683
1684 // Not archProperties are usable; this function looks for properties of a very specific
1685 // form, and ignores the rest.
1686 for _, archProperty := range m.archProperties[i] {
1687 // archPropValue is a property struct, we are looking for the form:
1688 // `arch: { arm: { key: value, ... }}`
1689 archPropValue := reflect.ValueOf(archProperty).Elem()
1690
1691 // Unwrap src so that it should looks like a pointer to `arm: { key: value, ... }`
1692 src := archPropValue.FieldByName("Arch").Elem()
1693
1694 // Step into non-nil pointers to structs in the src value.
1695 if src.Kind() == reflect.Ptr {
1696 if src.IsNil() {
1697 continue
1698 }
1699 src = src.Elem()
1700 }
1701
1702 // Find the requested field (e.g. arm, x86) in the src struct.
1703 src = src.FieldByName(archString)
1704
1705 // We only care about structs.
1706 if !src.IsValid() || src.Kind() != reflect.Struct {
1707 continue
1708 }
1709
1710 // If the value of the field is a struct then step into the
1711 // BlueprintEmbed field. The special "BlueprintEmbed" name is
1712 // used by createArchPropTypeDesc to embed the arch properties
1713 // in the parent struct, so the src arch prop should be in this
1714 // field.
1715 //
1716 // See createArchPropTypeDesc for more details on how Arch-specific
1717 // module properties are processed from the nested props and written
1718 // into the module's archProperties.
1719 src = src.FieldByName("BlueprintEmbed")
1720
1721 // Clone the destination prop, since we want a unique prop struct per arch.
1722 propertySetClone := reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
1723
1724 // Copy the located property struct into the cloned destination property struct.
1725 err := proptools.ExtendMatchingProperties([]interface{}{propertySetClone}, src.Interface(), nil, proptools.OrderReplace)
1726 if err != nil {
1727 // This is fine, it just means the src struct doesn't match the type of propertySet.
1728 continue
1729 }
1730
1731 return propertySetClone
1732 }
1733 }
1734 // No property set was found specific to the given arch, so return an empty
1735 // property set.
1736 return reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
1737}
1738
1739// getMultilibPropertySet returns a property set struct matching the type of
1740// `propertySet`, containing multilib-specific module properties for the given architecture.
1741// If no multilib-specific properties exist for the given architecture, returns an empty property
1742// set matching `propertySet`'s type.
1743func (m *ModuleBase) getMultilibPropertySet(propertySet interface{}, archType ArchType) interface{} {
1744 // archType.Multilib is lowercase (for example, lib32) but property struct field is
1745 // capitalized, such as Lib32, so use strings.Title to capitalize it.
1746 multiLibString := strings.Title(archType.Multilib)
1747
1748 for i := range m.archProperties {
1749 if m.archProperties[i] == nil {
1750 // Skip over nil properties
1751 continue
1752 }
1753
1754 // Not archProperties are usable; this function looks for properties of a very specific
1755 // form, and ignores the rest.
1756 for _, archProperties := range m.archProperties[i] {
1757 // archPropValue is a property struct, we are looking for the form:
1758 // `multilib: { lib32: { key: value, ... }}`
1759 archPropValue := reflect.ValueOf(archProperties).Elem()
1760
1761 // Unwrap src so that it should looks like a pointer to `lib32: { key: value, ... }`
1762 src := archPropValue.FieldByName("Multilib").Elem()
1763
1764 // Step into non-nil pointers to structs in the src value.
1765 if src.Kind() == reflect.Ptr {
1766 if src.IsNil() {
1767 // Ignore nil pointers.
1768 continue
1769 }
1770 src = src.Elem()
1771 }
1772
1773 // Find the requested field (e.g. lib32) in the src struct.
1774 src = src.FieldByName(multiLibString)
1775
1776 // We only care about valid struct pointers.
1777 if !src.IsValid() || src.Kind() != reflect.Ptr || src.Elem().Kind() != reflect.Struct {
1778 continue
1779 }
1780
1781 // Get the zero value for the requested property set.
1782 propertySetClone := reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
1783
1784 // Copy the located property struct into the "zero" property set struct.
1785 err := proptools.ExtendMatchingProperties([]interface{}{propertySetClone}, src.Interface(), nil, proptools.OrderReplace)
1786
1787 if err != nil {
1788 // This is fine, it just means the src struct doesn't match.
1789 continue
1790 }
1791
1792 return propertySetClone
1793 }
1794 }
1795
1796 // There were no multilib properties specifically matching the given archtype.
1797 // Return zeroed value.
1798 return reflect.New(reflect.ValueOf(propertySet).Elem().Type()).Interface()
1799}
1800
Jingwen Chen5d864492021-02-24 07:20:12 -05001801// GetArchProperties returns a map of architectures to the values of the
Chris Parsonsc424b762021-04-29 18:06:50 -04001802// properties of the 'propertySet' struct that are specific to that architecture.
Jingwen Chen5d864492021-02-24 07:20:12 -05001803//
1804// For example, passing a struct { Foo bool, Bar string } will return an
1805// interface{} that can be type asserted back into the same struct, containing
1806// the arch specific property value specified by the module if defined.
Chris Parsonsc424b762021-04-29 18:06:50 -04001807//
1808// Arch-specific properties may come from an arch stanza or a multilib stanza; properties
1809// in these stanzas are combined.
1810// For example: `arch: { x86: { Foo: ["bar"] } }, multilib: { lib32: {` Foo: ["baz"] } }`
1811// will result in `Foo: ["bar", "baz"]` being returned for architecture x86, if the given
1812// propertyset contains `Foo []string`.
1813func (m *ModuleBase) GetArchProperties(propertySet interface{}) map[ArchType]interface{} {
Jingwen Chen5d864492021-02-24 07:20:12 -05001814 // Return value of the arch types to the prop values for that arch.
1815 archToProp := map[ArchType]interface{}{}
1816
1817 // Nothing to do for non-arch-specific modules.
1818 if !m.ArchSpecific() {
1819 return archToProp
1820 }
1821
Chris Parsonsc424b762021-04-29 18:06:50 -04001822 // For each arch (x86, arm64, etc.),
1823 for _, arch := range ArchTypeList() {
1824 // Find arch-specific properties matching that property set type. For example, any
1825 // matching properties under `arch { x86 { ... } }`.
1826 archPropertySet := m.getArchPropertySet(propertySet, arch)
1827
1828 // Find multilib-specific properties matching that property set type. For example, any
1829 // matching properties under `multilib { lib32 { ... } }` for x86, as x86 is 32-bit.
1830 multilibPropertySet := m.getMultilibPropertySet(propertySet, arch)
1831
1832 // Append the multilibPropertySet to archPropertySet. This combines the
1833 // arch and multilib properties into a single property struct.
1834 err := proptools.ExtendMatchingProperties([]interface{}{archPropertySet}, multilibPropertySet, nil, proptools.OrderAppend)
1835 if err != nil {
1836 // archPropertySet and multilibPropertySet must be of the same type, or
1837 // something horrible went wrong.
1838 panic(err)
Jingwen Chen5d864492021-02-24 07:20:12 -05001839 }
1840
Chris Parsonsc424b762021-04-29 18:06:50 -04001841 archToProp[arch] = archPropertySet
Jingwen Chen5d864492021-02-24 07:20:12 -05001842 }
1843 return archToProp
1844}
Jingwen Chen91220d72021-03-24 02:18:33 -04001845
1846// GetTargetProperties returns a map of OS target (e.g. android, windows) to the
1847// values of the properties of the 'dst' struct that are specific to that OS
1848// target.
1849//
1850// For example, passing a struct { Foo bool, Bar string } will return an
1851// interface{} that can be type asserted back into the same struct, containing
1852// the os-specific property value specified by the module if defined.
1853//
1854// While this looks similar to GetArchProperties, the internal representation of
1855// the properties have a slightly different layout to warrant a standalone
1856// lookup function.
1857func (m *ModuleBase) GetTargetProperties(dst interface{}) map[OsType]interface{} {
1858 // Return value of the arch types to the prop values for that arch.
1859 osToProp := map[OsType]interface{}{}
1860
1861 // Nothing to do for non-OS/arch-specific modules.
1862 if !m.ArchSpecific() {
1863 return osToProp
1864 }
1865
1866 // archProperties has the type of [][]interface{}. Looks complicated, so
1867 // let's explain this step by step.
1868 //
1869 // Loop over the outer index, which determines the property struct that
1870 // contains a matching set of properties in dst that we're interested in.
1871 // For example, BaseCompilerProperties or BaseLinkerProperties.
1872 for i := range m.archProperties {
1873 if m.archProperties[i] == nil {
1874 continue
1875 }
1876
1877 // Iterate over the supported OS types
Jingwen Chen2f6a21e2021-04-05 07:33:05 +00001878 for _, os := range osTypeList {
Jingwen Chen91220d72021-03-24 02:18:33 -04001879 // e.g android, linux_bionic
1880 field := os.Field
1881
1882 // If it's not nil, loop over the inner index, which determines the arch variant
1883 // of the prop type. In an Android.bp file, this is like looping over:
1884 //
1885 // target: { android: { key: value, ... }, linux_bionic: { key: value, ... } }
1886 for _, archProperties := range m.archProperties[i] {
1887 archPropValues := reflect.ValueOf(archProperties).Elem()
1888
1889 // This is the archPropRoot struct. Traverse into the Targetnested struct.
1890 src := archPropValues.FieldByName("Target").Elem()
1891
1892 // Step into non-nil pointers to structs in the src value.
1893 if src.Kind() == reflect.Ptr {
1894 if src.IsNil() {
1895 continue
1896 }
1897 src = src.Elem()
1898 }
1899
1900 // Find the requested field (e.g. android, linux_bionic) in the src struct.
1901 src = src.FieldByName(field)
1902
1903 // Validation steps. We want valid non-nil pointers to structs.
1904 if !src.IsValid() || src.IsNil() {
1905 continue
1906 }
1907
1908 if src.Kind() != reflect.Ptr || src.Elem().Kind() != reflect.Struct {
1909 continue
1910 }
1911
1912 // Clone the destination prop, since we want a unique prop struct per arch.
1913 dstClone := reflect.New(reflect.ValueOf(dst).Elem().Type()).Interface()
1914
1915 // Copy the located property struct into the cloned destination property struct.
1916 err := proptools.ExtendMatchingProperties([]interface{}{dstClone}, src.Interface(), nil, proptools.OrderReplace)
1917 if err != nil {
1918 // This is fine, it just means the src struct doesn't match.
1919 continue
1920 }
1921
1922 // Found the prop for the os, you have.
1923 osToProp[os] = dstClone
1924
1925 // Go to the next prop.
1926 break
1927 }
1928 }
1929 }
1930 return osToProp
1931}