blob: 9f937522f651127330628f54e8d40bf48fdef709 [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
Colin Crossa6845402020-11-16 15:08:19 -0800415// osMutator splits an arch-specific module into a variant for each OS that is enabled for the
416// module. It uses the HostOrDevice value passed to InitAndroidArchModule and the
417// device_supported and host_supported properties to determine which OsTypes are enabled for this
418// module, then searches through the Targets to determine which have enabled Targets for this
419// module.
Colin Cross617b88a2020-08-24 18:04:09 -0700420func osMutator(bpctx blueprint.BottomUpMutatorContext) {
Colin Crossa195f912019-10-16 11:07:20 -0700421 var module Module
422 var ok bool
Colin Cross617b88a2020-08-24 18:04:09 -0700423 if module, ok = bpctx.Module().(Module); !ok {
Colin Crossa6845402020-11-16 15:08:19 -0800424 // The module is not a Soong module, it is a Blueprint module.
Colin Cross617b88a2020-08-24 18:04:09 -0700425 if bootstrap.IsBootstrapModule(bpctx.Module()) {
426 // Bootstrap Go modules are always the build OS or linux bionic.
427 config := bpctx.Config().(Config)
428 osNames := []string{config.BuildOSTarget.OsVariation()}
429 for _, hostCrossTarget := range config.Targets[LinuxBionic] {
430 if hostCrossTarget.Arch.ArchType == config.BuildOSTarget.Arch.ArchType {
431 osNames = append(osNames, hostCrossTarget.OsVariation())
432 }
433 }
434 osNames = FirstUniqueStrings(osNames)
435 bpctx.CreateVariations(osNames...)
436 }
Colin Crossa195f912019-10-16 11:07:20 -0700437 return
438 }
439
Colin Cross617b88a2020-08-24 18:04:09 -0700440 // Bootstrap Go module support above requires this mutator to be a
441 // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext
442 // filters out non-Soong modules. Now that we've handled them, create a
443 // normal android.BottomUpMutatorContext.
Liz Kammer356f7d42021-01-26 09:18:53 -0500444 mctx := bottomUpMutatorContextFactory(bpctx, module, false, false)
Colin Cross617b88a2020-08-24 18:04:09 -0700445
Colin Crossa195f912019-10-16 11:07:20 -0700446 base := module.base()
447
Colin Crossa6845402020-11-16 15:08:19 -0800448 // Nothing to do for modules that are not architecture specific (e.g. a genrule).
Colin Crossa195f912019-10-16 11:07:20 -0700449 if !base.ArchSpecific() {
450 return
451 }
452
Colin Crossa6845402020-11-16 15:08:19 -0800453 // Collect a list of OSTypes supported by this module based on the HostOrDevice value
454 // passed to InitAndroidArchModule and the device_supported and host_supported properties.
Colin Crossa195f912019-10-16 11:07:20 -0700455 var moduleOSList []OsType
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000456 for _, os := range osTypeList {
Jiyong Park1613e552020-09-14 19:43:17 +0900457 for _, t := range mctx.Config().Targets[os] {
Colin Cross08d6f8f2020-11-19 02:33:19 +0000458 if base.supportsTarget(t) {
Jiyong Park1613e552020-09-14 19:43:17 +0900459 moduleOSList = append(moduleOSList, os)
460 break
Colin Crossa195f912019-10-16 11:07:20 -0700461 }
462 }
Colin Crossa195f912019-10-16 11:07:20 -0700463 }
464
Colin Crossa6845402020-11-16 15:08:19 -0800465 // If there are no supported OSes then disable the module.
Colin Crossa195f912019-10-16 11:07:20 -0700466 if len(moduleOSList) == 0 {
Inseob Kimeec88e12020-01-22 11:11:29 +0900467 base.Disable()
Colin Crossa195f912019-10-16 11:07:20 -0700468 return
469 }
470
Colin Crossa6845402020-11-16 15:08:19 -0800471 // Convert the list of supported OsTypes to the variation names.
Colin Crossa195f912019-10-16 11:07:20 -0700472 osNames := make([]string, len(moduleOSList))
Colin Crossa195f912019-10-16 11:07:20 -0700473 for i, os := range moduleOSList {
474 osNames[i] = os.String()
475 }
476
Paul Duffin1356d8c2020-02-25 19:26:33 +0000477 createCommonOSVariant := base.commonProperties.CreateCommonOSVariant
478 if createCommonOSVariant {
Colin Crossa6845402020-11-16 15:08:19 -0800479 // A CommonOS variant was requested so add it to the list of OS variants to
Paul Duffin1356d8c2020-02-25 19:26:33 +0000480 // create. It needs to be added to the end because it needs to depend on the
481 // the other variants in the list returned by CreateVariations(...) and inter
482 // variant dependencies can only be created from a later variant in that list to
483 // an earlier one. That is because variants are always processed in the order in
484 // which they are returned from CreateVariations(...).
485 osNames = append(osNames, CommonOS.Name)
486 moduleOSList = append(moduleOSList, CommonOS)
Colin Crossa195f912019-10-16 11:07:20 -0700487 }
488
Colin Crossa6845402020-11-16 15:08:19 -0800489 // Create the variations, annotate each one with which OS it was created for, and
490 // squash the appropriate OS-specific properties into the top level properties.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000491 modules := mctx.CreateVariations(osNames...)
492 for i, m := range modules {
493 m.base().commonProperties.CompileOS = moduleOSList[i]
494 m.base().setOSProperties(mctx)
495 }
496
497 if createCommonOSVariant {
498 // A CommonOS variant was requested so add dependencies from it (the last one in
499 // the list) to the OS type specific variants.
500 last := len(modules) - 1
501 commonOSVariant := modules[last]
502 commonOSVariant.base().commonProperties.CommonOSVariant = true
503 for _, module := range modules[0:last] {
504 // Ignore modules that are enabled. Note, this will only avoid adding
505 // dependencies on OsType variants that are explicitly disabled in their
506 // properties. The CommonOS variant will still depend on disabled variants
507 // if they are disabled afterwards, e.g. in archMutator if
508 if module.Enabled() {
509 mctx.AddInterVariantDependency(commonOsToOsSpecificVariantTag, commonOSVariant, module)
510 }
511 }
512 }
513}
514
Colin Crossc179ea62020-10-09 10:54:15 -0700515type archDepTag struct {
516 blueprint.BaseDependencyTag
517 name string
518}
Paul Duffin1356d8c2020-02-25 19:26:33 +0000519
Colin Crossc179ea62020-10-09 10:54:15 -0700520// Identifies the dependency from CommonOS variant to the os specific variants.
521var commonOsToOsSpecificVariantTag = archDepTag{name: "common os to os specific"}
522
Paul Duffin1356d8c2020-02-25 19:26:33 +0000523// Get the OsType specific variants for the current CommonOS variant.
524//
525// The returned list will only contain enabled OsType specific variants of the
526// module referenced in the supplied context. An empty list is returned if there
527// are no enabled variants or the supplied context is not for an CommonOS
528// variant.
529func GetOsSpecificVariantsOfCommonOSVariant(mctx BaseModuleContext) []Module {
530 var variants []Module
531 mctx.VisitDirectDeps(func(m Module) {
532 if mctx.OtherModuleDependencyTag(m) == commonOsToOsSpecificVariantTag {
533 if m.Enabled() {
534 variants = append(variants, m)
535 }
536 }
537 })
Paul Duffin1356d8c2020-02-25 19:26:33 +0000538 return variants
Colin Crossa195f912019-10-16 11:07:20 -0700539}
540
Colin Crossee0bc3b2018-10-02 22:01:37 -0700541// archMutator splits a module into a variant for each Target requested by the module. Target selection
Colin Crossa6845402020-11-16 15:08:19 -0800542// for a module is in three levels, OsClass, multilib, and then Target.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700543// OsClass selection is determined by:
544// - The HostOrDeviceSupported value passed in to InitAndroidArchModule by the module type factory, which selects
545// whether the module type can compile for host, device or both.
546// - The host_supported and device_supported properties on the module.
Roland Levillainf5b635d2019-06-05 14:42:57 +0100547// 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 -0700548// for the module, the Device OsClass is selected.
549// Within each selected OsClass, the multilib selection is determined by:
Jaewoong Jung02b2d4d2019-06-06 15:19:57 -0700550// - The compile_multilib property if it set (which may be overridden by target.android.compile_multilib or
Colin Crossee0bc3b2018-10-02 22:01:37 -0700551// target.host.compile_multilib).
552// - The default multilib passed to InitAndroidArchModule if compile_multilib was not set.
553// Valid multilib values include:
554// "both": compile for all Targets supported by the OsClass (generally x86_64 and x86, or arm64 and arm).
555// "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 -0700556// but may be arm for a 32-bit only build.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700557// "32": compile for only a single 32-bit Target supported by the OsClass.
558// "64": compile for only a single 64-bit Target supported by the OsClass.
Colin Crossa6845402020-11-16 15:08:19 -0800559// "common": compile a for a single Target that will work on all Targets supported by the OsClass (for example Java).
560// "common_first": compile a for a Target that will work on all Targets supported by the OsClass
561// (same as "common"), plus a second Target for the preferred Target supported by the OsClass
562// (same as "first"). This is used for java_binary that produces a common .jar and a wrapper
563// executable script.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700564//
565// Once the list of Targets is determined, the module is split into a variant for each Target.
566//
567// Modules can be initialized with InitAndroidMultiTargetsArchModule, in which case they will be split by OsClass,
568// 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 -0700569func archMutator(bpctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700570 var module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800571 var ok bool
Colin Cross617b88a2020-08-24 18:04:09 -0700572 if module, ok = bpctx.Module().(Module); !ok {
573 if bootstrap.IsBootstrapModule(bpctx.Module()) {
574 // Bootstrap Go modules are always the build architecture.
575 bpctx.CreateVariations(bpctx.Config().(Config).BuildOSTarget.ArchVariation())
576 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800577 return
578 }
579
Colin Cross617b88a2020-08-24 18:04:09 -0700580 // Bootstrap Go module support above requires this mutator to be a
581 // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext
582 // filters out non-Soong modules. Now that we've handled them, create a
583 // normal android.BottomUpMutatorContext.
Liz Kammer356f7d42021-01-26 09:18:53 -0500584 mctx := bottomUpMutatorContextFactory(bpctx, module, false, false)
Colin Cross617b88a2020-08-24 18:04:09 -0700585
Colin Cross5eca7cb2018-10-02 14:02:10 -0700586 base := module.base()
587
588 if !base.ArchSpecific() {
Colin Crossb9db4802016-06-03 01:50:47 +0000589 return
590 }
591
Colin Crossa195f912019-10-16 11:07:20 -0700592 os := base.commonProperties.CompileOS
Paul Duffin1356d8c2020-02-25 19:26:33 +0000593 if os == CommonOS {
594 // Make sure that the target related properties are initialized for the
595 // CommonOS variant.
596 addTargetProperties(module, commonTargetMap[os.Name], nil, true)
597
598 // Do not create arch specific variants for the CommonOS variant.
599 return
600 }
601
Colin Crossa195f912019-10-16 11:07:20 -0700602 osTargets := mctx.Config().Targets[os]
Colin Crossfb0c16e2019-11-20 17:12:35 -0800603 image := base.commonProperties.ImageVariation
Colin Crossa6845402020-11-16 15:08:19 -0800604 // Filter NativeBridge targets unless they are explicitly supported.
605 // Skip creating native bridge variants for non-core modules.
Colin Cross83bead42019-12-18 10:45:46 -0800606 if os == Android &&
607 !(Bool(base.commonProperties.Native_bridge_supported) && image == CoreVariation) {
608
Colin Crossa195f912019-10-16 11:07:20 -0700609 var targets []Target
610 for _, t := range osTargets {
611 if !t.NativeBridge {
612 targets = append(targets, t)
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700613 }
614 }
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700615
Colin Crossa195f912019-10-16 11:07:20 -0700616 osTargets = targets
617 }
Colin Crossee0bc3b2018-10-02 22:01:37 -0700618
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700619 // only the primary arch in the ramdisk / vendor_ramdisk / recovery partition
Inseob Kimf84e9c02021-04-08 21:13:22 +0900620 if os == Android && (module.InstallInRecovery() || module.InstallInRamdisk() || module.InstallInVendorRamdisk() || module.InstallInDebugRamdisk()) {
Colin Crossa195f912019-10-16 11:07:20 -0700621 osTargets = []Target{osTargets[0]}
622 }
dimitry1f33e402019-03-26 12:39:31 +0100623
Jaewoong Jung003d8082021-02-24 17:39:54 -0800624 // Windows builds always prefer 32-bit
625 prefer32 := os == Windows
dimitry1f33e402019-03-26 12:39:31 +0100626
Colin Crossa6845402020-11-16 15:08:19 -0800627 // Determine the multilib selection for this module.
Colin Crossa195f912019-10-16 11:07:20 -0700628 multilib, extraMultilib := decodeMultilib(base, os.Class)
Colin Crossa6845402020-11-16 15:08:19 -0800629
630 // Convert the multilib selection into a list of Targets.
Colin Crossa195f912019-10-16 11:07:20 -0700631 targets, err := decodeMultilibTargets(multilib, osTargets, prefer32)
632 if err != nil {
633 mctx.ModuleErrorf("%s", err.Error())
634 }
Colin Cross5eca7cb2018-10-02 14:02:10 -0700635
Colin Crossa6845402020-11-16 15:08:19 -0800636 // If the module is using extraMultilib, decode the extraMultilib selection into
637 // a separate list of Targets.
Colin Crossa195f912019-10-16 11:07:20 -0700638 var multiTargets []Target
639 if extraMultilib != "" {
640 multiTargets, err = decodeMultilibTargets(extraMultilib, osTargets, prefer32)
Colin Crossa1ad8d12016-06-01 17:09:44 -0700641 if err != nil {
642 mctx.ModuleErrorf("%s", err.Error())
643 }
Colin Crossb9db4802016-06-03 01:50:47 +0000644 }
645
Colin Crossa6845402020-11-16 15:08:19 -0800646 // Recovery is always the primary architecture, filter out any other architectures.
Inseob Kim20fb5d42021-02-02 20:07:58 +0900647 // Common arch is also allowed
Colin Crossfb0c16e2019-11-20 17:12:35 -0800648 if image == RecoveryVariation {
649 primaryArch := mctx.Config().DevicePrimaryArchType()
Inseob Kim20fb5d42021-02-02 20:07:58 +0900650 targets = filterToArch(targets, primaryArch, Common)
651 multiTargets = filterToArch(multiTargets, primaryArch, Common)
Colin Crossfb0c16e2019-11-20 17:12:35 -0800652 }
653
Colin Crossa6845402020-11-16 15:08:19 -0800654 // If there are no supported targets disable the module.
Colin Crossa195f912019-10-16 11:07:20 -0700655 if len(targets) == 0 {
Inseob Kimeec88e12020-01-22 11:11:29 +0900656 base.Disable()
Dan Willemsen3f32f032016-07-11 14:36:48 -0700657 return
658 }
659
Colin Crossa6845402020-11-16 15:08:19 -0800660 // Convert the targets into a list of arch variation names.
Colin Crossa195f912019-10-16 11:07:20 -0700661 targetNames := make([]string, len(targets))
Colin Crossa195f912019-10-16 11:07:20 -0700662 for i, target := range targets {
663 targetNames[i] = target.ArchVariation()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700664 }
665
Colin Crossa6845402020-11-16 15:08:19 -0800666 // Create the variations, annotate each one with which Target it was created for, and
667 // squash the appropriate arch-specific properties into the top level properties.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700668 modules := mctx.CreateVariations(targetNames...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800669 for i, m := range modules {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000670 addTargetProperties(m, targets[i], multiTargets, i == 0)
Colin Cross617b88a2020-08-24 18:04:09 -0700671 m.base().setArchProperties(mctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800672 }
673}
674
Colin Crossa6845402020-11-16 15:08:19 -0800675// addTargetProperties annotates a variant with the Target is is being compiled for, the list
676// of additional Targets it is supporting (if any), and whether it is the primary Target for
677// the module.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000678func addTargetProperties(m Module, target Target, multiTargets []Target, primaryTarget bool) {
679 m.base().commonProperties.CompileTarget = target
680 m.base().commonProperties.CompileMultiTargets = multiTargets
681 m.base().commonProperties.CompilePrimary = primaryTarget
682}
683
Colin Crossa6845402020-11-16 15:08:19 -0800684// decodeMultilib returns the appropriate compile_multilib property for the module, or the default
685// multilib from the factory's call to InitAndroidArchModule if none was set. For modules that
686// called InitAndroidMultiTargetsArchModule it always returns "common" for multilib, and returns
687// the actual multilib in extraMultilib.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700688func decodeMultilib(base *ModuleBase, class OsClass) (multilib, extraMultilib string) {
Colin Crossa6845402020-11-16 15:08:19 -0800689 // First check the "android.compile_multilib" or "host.compile_multilib" properties.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700690 switch class {
691 case Device:
692 multilib = String(base.commonProperties.Target.Android.Compile_multilib)
Jiyong Park1613e552020-09-14 19:43:17 +0900693 case Host:
Colin Crossee0bc3b2018-10-02 22:01:37 -0700694 multilib = String(base.commonProperties.Target.Host.Compile_multilib)
695 }
Colin Crossa6845402020-11-16 15:08:19 -0800696
697 // If those aren't set, try the "compile_multilib" property.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700698 if multilib == "" {
699 multilib = String(base.commonProperties.Compile_multilib)
700 }
Colin Crossa6845402020-11-16 15:08:19 -0800701
702 // If that wasn't set, use the default multilib set by the factory.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700703 if multilib == "" {
704 multilib = base.commonProperties.Default_multilib
705 }
706
707 if base.commonProperties.UseTargetVariants {
708 return multilib, ""
709 } else {
710 // For app modules a single arch variant will be created per OS class which is expected to handle all the
711 // selected arches. Return the common-type as multilib and any Android.bp provided multilib as extraMultilib
712 if multilib == base.commonProperties.Default_multilib {
713 multilib = "first"
714 }
715 return base.commonProperties.Default_multilib, multilib
716 }
717}
718
Colin Crossa6845402020-11-16 15:08:19 -0800719// filterToArch takes a list of Targets and an ArchType, and returns a modified list that contains
Inseob Kim20fb5d42021-02-02 20:07:58 +0900720// only Targets that have the specified ArchTypes.
721func filterToArch(targets []Target, archs ...ArchType) []Target {
Colin Crossfb0c16e2019-11-20 17:12:35 -0800722 for i := 0; i < len(targets); i++ {
Inseob Kim20fb5d42021-02-02 20:07:58 +0900723 found := false
724 for _, arch := range archs {
725 if targets[i].Arch.ArchType == arch {
726 found = true
727 break
728 }
729 }
730 if !found {
Colin Crossfb0c16e2019-11-20 17:12:35 -0800731 targets = append(targets[:i], targets[i+1:]...)
732 i--
733 }
734 }
735 return targets
736}
737
Colin Crossa6845402020-11-16 15:08:19 -0800738// archPropRoot is a struct type used as the top level of the arch-specific properties. It
739// contains the "arch", "multilib", and "target" property structs. It is used to split up the
740// property structs to limit how much is allocated when a single arch-specific property group is
741// used. The types are interface{} because they will hold instances of runtime-created types.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800742type archPropRoot struct {
743 Arch, Multilib, Target interface{}
744}
745
Colin Crossa6845402020-11-16 15:08:19 -0800746// archPropTypeDesc holds the runtime-created types for the property structs to instantiate to
747// create an archPropRoot property struct.
748type archPropTypeDesc struct {
749 arch, multilib, target reflect.Type
750}
751
Colin Crosscbbd13f2020-01-17 14:08:22 -0800752// createArchPropTypeDesc takes a reflect.Type that is either a struct or a pointer to a struct, and
753// returns lists of reflect.Types that contains the arch-variant properties inside structs for each
754// arch, multilib and target property.
Colin Crossa6845402020-11-16 15:08:19 -0800755//
756// This is a relatively expensive operation, so the results are cached in the global
757// archPropTypeMap. It is constructed entirely based on compile-time data, so there is no need
758// to isolate the results between multiple tests running in parallel.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800759func createArchPropTypeDesc(props reflect.Type) []archPropTypeDesc {
Colin Crossb1d8c992020-01-21 11:43:29 -0800760 // Each property struct shard will be nested many times under the runtime generated arch struct,
761 // which can hit the limit of 64kB for the name of runtime generated structs. They are nested
762 // 97 times now, which may grow in the future, plus there is some overhead for the containing
763 // type. This number may need to be reduced if too many are added, but reducing it too far
764 // could cause problems if a single deeply nested property no longer fits in the name.
765 const maxArchTypeNameSize = 500
766
Colin Crossa6845402020-11-16 15:08:19 -0800767 // Convert the type to a new set of types that contains only the arch-specific properties
768 // (those that are tagged with `android:"arch_specific"`), and sharded into multiple types
769 // to keep the runtime-generated names under the limit.
Colin Crossb1d8c992020-01-21 11:43:29 -0800770 propShards, _ := proptools.FilterPropertyStructSharded(props, maxArchTypeNameSize, filterArchStruct)
Colin Crossa6845402020-11-16 15:08:19 -0800771
772 // If the type has no arch-specific properties there is nothing to do.
Colin Crosscb988072019-01-24 14:58:11 -0800773 if len(propShards) == 0 {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700774 return nil
775 }
776
Colin Crosscbbd13f2020-01-17 14:08:22 -0800777 var ret []archPropTypeDesc
Colin Crossc17727d2018-10-24 12:42:09 -0700778 for _, props := range propShards {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700779
Colin Crossa6845402020-11-16 15:08:19 -0800780 // variantFields takes a list of variant property field names and returns a list the
781 // StructFields with the names and the type of the current shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700782 variantFields := func(names []string) []reflect.StructField {
783 ret := make([]reflect.StructField, len(names))
Dan Willemsenb1957a52016-06-23 23:44:54 -0700784
Colin Crossc17727d2018-10-24 12:42:09 -0700785 for i, name := range names {
786 ret[i].Name = name
787 ret[i].Type = props
Dan Willemsen866b5632017-09-22 12:28:24 -0700788 }
Colin Crossc17727d2018-10-24 12:42:09 -0700789
790 return ret
791 }
792
Colin Crossa6845402020-11-16 15:08:19 -0800793 // Create a type that contains the properties in this shard repeated for each
794 // architecture, architecture variant, and architecture feature.
Colin Crossc17727d2018-10-24 12:42:09 -0700795 archFields := make([]reflect.StructField, len(archTypeList))
796 for i, arch := range archTypeList {
Colin Crossa6845402020-11-16 15:08:19 -0800797 var variants []string
Colin Crossc17727d2018-10-24 12:42:09 -0700798
799 for _, archVariant := range archVariants[arch] {
800 archVariant := variantReplacer.Replace(archVariant)
801 variants = append(variants, proptools.FieldNameForProperty(archVariant))
802 }
803 for _, feature := range archFeatures[arch] {
804 feature := variantReplacer.Replace(feature)
805 variants = append(variants, proptools.FieldNameForProperty(feature))
806 }
807
Colin Crossa6845402020-11-16 15:08:19 -0800808 // Create the StructFields for each architecture variant architecture feature
809 // (e.g. "arch.arm.cortex-a53" or "arch.arm.neon").
Colin Crossc17727d2018-10-24 12:42:09 -0700810 fields := variantFields(variants)
811
Colin Crossa6845402020-11-16 15:08:19 -0800812 // Create the StructField for the architecture itself (e.g. "arch.arm"). The special
813 // "BlueprintEmbed" name is used by Blueprint to put the properties in the
814 // parent struct.
Colin Crossc17727d2018-10-24 12:42:09 -0700815 fields = append([]reflect.StructField{{
816 Name: "BlueprintEmbed",
817 Type: props,
818 Anonymous: true,
819 }}, fields...)
820
821 archFields[i] = reflect.StructField{
822 Name: arch.Field,
823 Type: reflect.StructOf(fields),
824 }
825 }
Colin Crossa6845402020-11-16 15:08:19 -0800826
827 // Create the type of the "arch" property struct for this shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700828 archType := reflect.StructOf(archFields)
829
Colin Crossa6845402020-11-16 15:08:19 -0800830 // Create the type for the "multilib" property struct for this shard, containing the
831 // "multilib.lib32" and "multilib.lib64" property structs.
Colin Crossc17727d2018-10-24 12:42:09 -0700832 multilibType := reflect.StructOf(variantFields([]string{"Lib32", "Lib64"}))
833
Colin Crossa6845402020-11-16 15:08:19 -0800834 // Start with a list of the special targets
Colin Crossc17727d2018-10-24 12:42:09 -0700835 targets := []string{
836 "Host",
837 "Android64",
838 "Android32",
839 "Bionic",
840 "Linux",
841 "Not_windows",
842 "Arm_on_x86",
843 "Arm_on_x86_64",
Victor Khimenkoc26fcf42020-05-07 22:16:33 +0200844 "Native_bridge",
Colin Crossc17727d2018-10-24 12:42:09 -0700845 }
Jingwen Chen2f6a21e2021-04-05 07:33:05 +0000846 for _, os := range osTypeList {
Colin Crossa6845402020-11-16 15:08:19 -0800847 // Add all the OSes.
Colin Crossc17727d2018-10-24 12:42:09 -0700848 targets = append(targets, os.Field)
849
Colin Crossa6845402020-11-16 15:08:19 -0800850 // Add the OS/Arch combinations, e.g. "android_arm64".
Colin Crossc17727d2018-10-24 12:42:09 -0700851 for _, archType := range osArchTypeMap[os] {
852 targets = append(targets, os.Field+"_"+archType.Name)
853
Colin Crossa6845402020-11-16 15:08:19 -0800854 // Also add the special "linux_<arch>" and "bionic_<arch>" property structs.
Colin Crossc17727d2018-10-24 12:42:09 -0700855 if os.Linux() {
856 target := "Linux_" + archType.Name
857 if !InList(target, targets) {
858 targets = append(targets, target)
859 }
860 }
861 if os.Bionic() {
862 target := "Bionic_" + archType.Name
863 if !InList(target, targets) {
864 targets = append(targets, target)
865 }
Dan Willemsen866b5632017-09-22 12:28:24 -0700866 }
867 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700868 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700869
Colin Crossa6845402020-11-16 15:08:19 -0800870 // Create the type for the "target" property struct for this shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700871 targetType := reflect.StructOf(variantFields(targets))
Colin Crosscbbd13f2020-01-17 14:08:22 -0800872
Colin Crossa6845402020-11-16 15:08:19 -0800873 // Return a descriptor of the 3 runtime-created types.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800874 ret = append(ret, archPropTypeDesc{
875 arch: reflect.PtrTo(archType),
876 multilib: reflect.PtrTo(multilibType),
877 target: reflect.PtrTo(targetType),
878 })
Colin Crossc17727d2018-10-24 12:42:09 -0700879 }
880 return ret
Dan Willemsenb1957a52016-06-23 23:44:54 -0700881}
882
Colin Crossa6845402020-11-16 15:08:19 -0800883// variantReplacer converts architecture variant or architecture feature names into names that
884// are valid for an Android.bp file.
885var variantReplacer = strings.NewReplacer("-", "_", ".", "_")
886
887// filterArchStruct returns true if the given field is an architecture specific property.
Colin Cross74449102019-09-25 11:26:40 -0700888func filterArchStruct(field reflect.StructField, prefix string) (bool, reflect.StructField) {
889 if proptools.HasTag(field, "android", "arch_variant") {
890 // The arch_variant field isn't necessary past this point
891 // Instead of wasting space, just remove it. Go also has a
892 // 16-bit limit on structure name length. The name is constructed
893 // based on the Go source representation of the structure, so
894 // the tag names count towards that length.
Colin Crossb4fecbf2020-01-21 11:38:47 -0800895
896 androidTag := field.Tag.Get("android")
897 values := strings.Split(androidTag, ",")
898
899 if string(field.Tag) != `android:"`+strings.Join(values, ",")+`"` {
900 panic(fmt.Errorf("unexpected tag format %q", field.Tag))
Colin Cross74449102019-09-25 11:26:40 -0700901 }
Colin Crossb4fecbf2020-01-21 11:38:47 -0800902 // these tags don't need to be present in the runtime generated struct type.
903 values = RemoveListFromList(values, []string{"arch_variant", "variant_prepend", "path"})
904 if len(values) > 0 {
905 panic(fmt.Errorf("unknown tags %q in field %q", values, prefix+field.Name))
906 }
907
908 field.Tag = ""
Colin Cross74449102019-09-25 11:26:40 -0700909 return true, field
910 }
911 return false, field
912}
913
Colin Crossa6845402020-11-16 15:08:19 -0800914// archPropTypeMap contains a cache of the results of createArchPropTypeDesc for each type. It is
915// shared across all Contexts, but is constructed based only on compile-time information so there
916// is no risk of contaminating one Context with data from another.
Dan Willemsenb1957a52016-06-23 23:44:54 -0700917var archPropTypeMap OncePer
918
Colin Crossa6845402020-11-16 15:08:19 -0800919// initArchModule adds the architecture-specific property structs to a Module.
920func initArchModule(m Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800921
922 base := m.base()
923
Colin Crossa6845402020-11-16 15:08:19 -0800924 // Store the original list of top level property structs
Colin Cross36242852017-06-23 15:06:31 -0700925 base.generalProperties = m.GetProperties()
Colin Cross3f40fa42015-01-30 17:27:36 -0800926
927 for _, properties := range base.generalProperties {
928 propertiesValue := reflect.ValueOf(properties)
Colin Cross62496a02016-08-08 15:49:17 -0700929 t := propertiesValue.Type()
Colin Cross3f40fa42015-01-30 17:27:36 -0800930 if propertiesValue.Kind() != reflect.Ptr {
Colin Crossca860ac2016-01-04 14:34:37 -0800931 panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
932 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800933 }
934
935 propertiesValue = propertiesValue.Elem()
936 if propertiesValue.Kind() != reflect.Struct {
Colin Crossca860ac2016-01-04 14:34:37 -0800937 panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
938 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800939 }
940
Colin Crossa6845402020-11-16 15:08:19 -0800941 // Get or create the arch-specific property struct types for this property struct type.
Colin Cross571cccf2019-02-04 11:22:08 -0800942 archPropTypes := archPropTypeMap.Once(NewCustomOnceKey(t), func() interface{} {
Colin Crosscbbd13f2020-01-17 14:08:22 -0800943 return createArchPropTypeDesc(t)
944 }).([]archPropTypeDesc)
Colin Cross3f40fa42015-01-30 17:27:36 -0800945
Colin Crossa6845402020-11-16 15:08:19 -0800946 // Instantiate one of each arch-specific property struct type and add it to the
947 // properties for the Module.
Colin Crossc17727d2018-10-24 12:42:09 -0700948 var archProperties []interface{}
949 for _, t := range archPropTypes {
Colin Crosscbbd13f2020-01-17 14:08:22 -0800950 archProperties = append(archProperties, &archPropRoot{
951 Arch: reflect.Zero(t.arch).Interface(),
952 Multilib: reflect.Zero(t.multilib).Interface(),
953 Target: reflect.Zero(t.target).Interface(),
954 })
Dan Willemsenb1957a52016-06-23 23:44:54 -0700955 }
Colin Crossc17727d2018-10-24 12:42:09 -0700956 base.archProperties = append(base.archProperties, archProperties)
957 m.AddProperties(archProperties...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800958 }
959
Colin Crossa6845402020-11-16 15:08:19 -0800960 // Update the list of properties that can be set by a defaults module or a call to
961 // AppendMatchingProperties or PrependMatchingProperties.
Colin Cross36242852017-06-23 15:06:31 -0700962 base.customizableProperties = m.GetProperties()
Colin Cross3f40fa42015-01-30 17:27:36 -0800963}
964
Colin Crossa6845402020-11-16 15:08:19 -0800965// appendProperties squashes properties from the given field of the given src property struct
966// into the dst property struct. Returns the reflect.Value of the field in the src property
967// struct to be used for further appendProperties calls on fields of that property struct.
Colin Cross4157e882019-06-06 16:57:04 -0700968func (m *ModuleBase) appendProperties(ctx BottomUpMutatorContext,
Dan Willemsenb1957a52016-06-23 23:44:54 -0700969 dst interface{}, src reflect.Value, field, srcPrefix string) reflect.Value {
Colin Cross06a931b2015-10-28 17:23:31 -0700970
Colin Crossa6845402020-11-16 15:08:19 -0800971 // Step into non-nil pointers to structs in the src value.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800972 if src.Kind() == reflect.Ptr {
973 if src.IsNil() {
974 return src
975 }
976 src = src.Elem()
977 }
978
Colin Crossa6845402020-11-16 15:08:19 -0800979 // Find the requested field in the src struct.
Dan Willemsenb1957a52016-06-23 23:44:54 -0700980 src = src.FieldByName(field)
981 if !src.IsValid() {
Colin Crosseeabb892015-11-20 13:07:51 -0800982 ctx.ModuleErrorf("field %q does not exist", srcPrefix)
Dan Willemsenb1957a52016-06-23 23:44:54 -0700983 return src
Colin Cross85a88972015-11-23 13:29:51 -0800984 }
985
Colin Crossa6845402020-11-16 15:08:19 -0800986 // Save the value of the field in the src struct to return.
Dan Willemsenb1957a52016-06-23 23:44:54 -0700987 ret := src
Colin Cross85a88972015-11-23 13:29:51 -0800988
Colin Crossa6845402020-11-16 15:08:19 -0800989 // If the value of the field is a struct (as opposed to a pointer to a struct) then step
990 // into the BlueprintEmbed field.
Dan Willemsenb1957a52016-06-23 23:44:54 -0700991 if src.Kind() == reflect.Struct {
992 src = src.FieldByName("BlueprintEmbed")
Colin Cross06a931b2015-10-28 17:23:31 -0700993 }
994
Colin Crossa6845402020-11-16 15:08:19 -0800995 // order checks the `android:"variant_prepend"` tag to handle properties where the
996 // arch-specific value needs to come before the generic value, for example for lists of
997 // include directories.
Colin Cross6ee75b62016-05-05 15:57:15 -0700998 order := func(property string,
999 dstField, srcField reflect.StructField,
1000 dstValue, srcValue interface{}) (proptools.Order, error) {
1001 if proptools.HasTag(dstField, "android", "variant_prepend") {
1002 return proptools.Prepend, nil
1003 } else {
1004 return proptools.Append, nil
1005 }
1006 }
1007
Colin Crossa6845402020-11-16 15:08:19 -08001008 // Squash the located property struct into the destination property struct.
Dan Willemsenb1957a52016-06-23 23:44:54 -07001009 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src.Interface(), nil, order)
Colin Cross06a931b2015-10-28 17:23:31 -07001010 if err != nil {
1011 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
1012 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
1013 } else {
1014 panic(err)
1015 }
1016 }
Colin Cross85a88972015-11-23 13:29:51 -08001017
Dan Willemsenb1957a52016-06-23 23:44:54 -07001018 return ret
Colin Cross06a931b2015-10-28 17:23:31 -07001019}
1020
Colin Crossa6845402020-11-16 15:08:19 -08001021// Squash the appropriate OS-specific property structs into the matching top level property structs
1022// based on the CompileOS value that was annotated on the variant.
Colin Crossa195f912019-10-16 11:07:20 -07001023func (m *ModuleBase) setOSProperties(ctx BottomUpMutatorContext) {
1024 os := m.commonProperties.CompileOS
1025
1026 for i := range m.generalProperties {
1027 genProps := m.generalProperties[i]
1028 if m.archProperties[i] == nil {
1029 continue
1030 }
1031 for _, archProperties := range m.archProperties[i] {
1032 archPropValues := reflect.ValueOf(archProperties).Elem()
1033
Colin Crosscbbd13f2020-01-17 14:08:22 -08001034 targetProp := archPropValues.FieldByName("Target").Elem()
Colin Crossa195f912019-10-16 11:07:20 -07001035
1036 // Handle host-specific properties in the form:
1037 // target: {
1038 // host: {
1039 // key: value,
1040 // },
1041 // },
Jiyong Park1613e552020-09-14 19:43:17 +09001042 if os.Class == Host {
Colin Crossa195f912019-10-16 11:07:20 -07001043 field := "Host"
1044 prefix := "target.host"
1045 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1046 }
1047
1048 // Handle target OS generalities of the form:
1049 // target: {
1050 // bionic: {
1051 // key: value,
1052 // },
1053 // }
1054 if os.Linux() {
1055 field := "Linux"
1056 prefix := "target.linux"
1057 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1058 }
1059
1060 if os.Bionic() {
1061 field := "Bionic"
1062 prefix := "target.bionic"
1063 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1064 }
1065
1066 // Handle target OS properties in the form:
1067 // target: {
1068 // linux_glibc: {
1069 // key: value,
1070 // },
1071 // not_windows: {
1072 // key: value,
1073 // },
1074 // android {
1075 // key: value,
1076 // },
1077 // },
1078 field := os.Field
1079 prefix := "target." + os.Name
1080 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1081
Jiyong Park1613e552020-09-14 19:43:17 +09001082 if os.Class == Host && os != Windows {
Colin Crossa195f912019-10-16 11:07:20 -07001083 field := "Not_windows"
1084 prefix := "target.not_windows"
1085 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1086 }
1087
1088 // Handle 64-bit device properties in the form:
1089 // target {
1090 // android64 {
1091 // key: value,
1092 // },
1093 // android32 {
1094 // key: value,
1095 // },
1096 // },
1097 // WARNING: this is probably not what you want to use in your blueprints file, it selects
1098 // options for all targets on a device that supports 64-bit binaries, not just the targets
1099 // that are being compiled for 64-bit. Its expected use case is binaries like linker and
1100 // debuggerd that need to know when they are a 32-bit process running on a 64-bit device
1101 if os.Class == Device {
1102 if ctx.Config().Android64() {
1103 field := "Android64"
1104 prefix := "target.android64"
1105 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1106 } else {
1107 field := "Android32"
1108 prefix := "target.android32"
1109 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1110 }
1111 }
1112 }
1113 }
1114}
1115
Colin Crossa6845402020-11-16 15:08:19 -08001116// Squash the appropriate arch-specific property structs into the matching top level property
1117// structs based on the CompileTarget value that was annotated on the variant.
Colin Cross4157e882019-06-06 16:57:04 -07001118func (m *ModuleBase) setArchProperties(ctx BottomUpMutatorContext) {
1119 arch := m.Arch()
1120 os := m.Os()
Colin Crossd3ba0392015-05-07 14:11:29 -07001121
Colin Cross4157e882019-06-06 16:57:04 -07001122 for i := range m.generalProperties {
1123 genProps := m.generalProperties[i]
1124 if m.archProperties[i] == nil {
Dan Willemsenb1957a52016-06-23 23:44:54 -07001125 continue
1126 }
Colin Cross4157e882019-06-06 16:57:04 -07001127 for _, archProperties := range m.archProperties[i] {
Colin Crossc17727d2018-10-24 12:42:09 -07001128 archPropValues := reflect.ValueOf(archProperties).Elem()
Dan Willemsenb1957a52016-06-23 23:44:54 -07001129
Colin Crosscbbd13f2020-01-17 14:08:22 -08001130 archProp := archPropValues.FieldByName("Arch").Elem()
1131 multilibProp := archPropValues.FieldByName("Multilib").Elem()
1132 targetProp := archPropValues.FieldByName("Target").Elem()
Dan Willemsenb1957a52016-06-23 23:44:54 -07001133
Colin Crossc17727d2018-10-24 12:42:09 -07001134 // Handle arch-specific properties in the form:
Colin Crossd5934c82017-10-02 13:55:26 -07001135 // arch: {
Colin Crossc17727d2018-10-24 12:42:09 -07001136 // arm64: {
Colin Crossd5934c82017-10-02 13:55:26 -07001137 // key: value,
1138 // },
1139 // },
Colin Crossc17727d2018-10-24 12:42:09 -07001140 t := arch.ArchType
1141
1142 if arch.ArchType != Common {
1143 field := proptools.FieldNameForProperty(t.Name)
1144 prefix := "arch." + t.Name
Colin Cross4157e882019-06-06 16:57:04 -07001145 archStruct := m.appendProperties(ctx, genProps, archProp, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001146
1147 // Handle arch-variant-specific properties in the form:
1148 // arch: {
1149 // variant: {
1150 // key: value,
1151 // },
1152 // },
1153 v := variantReplacer.Replace(arch.ArchVariant)
1154 if v != "" {
1155 field := proptools.FieldNameForProperty(v)
1156 prefix := "arch." + t.Name + "." + v
Colin Cross4157e882019-06-06 16:57:04 -07001157 m.appendProperties(ctx, genProps, archStruct, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001158 }
1159
1160 // Handle cpu-variant-specific properties in the form:
1161 // arch: {
1162 // variant: {
1163 // key: value,
1164 // },
1165 // },
1166 if arch.CpuVariant != arch.ArchVariant {
1167 c := variantReplacer.Replace(arch.CpuVariant)
1168 if c != "" {
1169 field := proptools.FieldNameForProperty(c)
1170 prefix := "arch." + t.Name + "." + c
Colin Cross4157e882019-06-06 16:57:04 -07001171 m.appendProperties(ctx, genProps, archStruct, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001172 }
1173 }
1174
1175 // Handle arch-feature-specific properties in the form:
1176 // arch: {
1177 // feature: {
1178 // key: value,
1179 // },
1180 // },
1181 for _, feature := range arch.ArchFeatures {
1182 field := proptools.FieldNameForProperty(feature)
1183 prefix := "arch." + t.Name + "." + feature
Colin Cross4157e882019-06-06 16:57:04 -07001184 m.appendProperties(ctx, genProps, archStruct, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001185 }
1186
1187 // Handle multilib-specific properties in the form:
1188 // multilib: {
1189 // lib32: {
1190 // key: value,
1191 // },
1192 // },
1193 field = proptools.FieldNameForProperty(t.Multilib)
1194 prefix = "multilib." + t.Multilib
Colin Cross4157e882019-06-06 16:57:04 -07001195 m.appendProperties(ctx, genProps, multilibProp, field, prefix)
Colin Cross08016332016-12-20 09:53:14 -08001196 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001197
Colin Crossa195f912019-10-16 11:07:20 -07001198 // Handle combined OS-feature and arch specific properties in the form:
Colin Crossc17727d2018-10-24 12:42:09 -07001199 // target: {
Colin Crossc17727d2018-10-24 12:42:09 -07001200 // bionic_x86: {
1201 // key: value,
1202 // },
1203 // }
Colin Crossa195f912019-10-16 11:07:20 -07001204 if os.Linux() && arch.ArchType != Common {
1205 field := "Linux_" + arch.ArchType.Name
1206 prefix := "target.linux_" + arch.ArchType.Name
Colin Cross4157e882019-06-06 16:57:04 -07001207 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossd5934c82017-10-02 13:55:26 -07001208 }
Colin Crossc5c24ad2015-11-20 15:35:00 -08001209
Colin Crossa195f912019-10-16 11:07:20 -07001210 if os.Bionic() && arch.ArchType != Common {
1211 field := "Bionic_" + t.Name
1212 prefix := "target.bionic_" + t.Name
Colin Cross4157e882019-06-06 16:57:04 -07001213 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001214 }
1215
Colin Crossa195f912019-10-16 11:07:20 -07001216 // Handle combined OS and arch specific properties in the form:
Colin Crossc17727d2018-10-24 12:42:09 -07001217 // target: {
Colin Crossc17727d2018-10-24 12:42:09 -07001218 // linux_glibc_x86: {
1219 // key: value,
1220 // },
1221 // linux_glibc_arm: {
1222 // key: value,
1223 // },
Colin Crossc17727d2018-10-24 12:42:09 -07001224 // android_arm {
1225 // key: value,
1226 // },
1227 // android_x86 {
Colin Crossd5934c82017-10-02 13:55:26 -07001228 // key: value,
1229 // },
1230 // },
Colin Crossc17727d2018-10-24 12:42:09 -07001231 if arch.ArchType != Common {
Colin Crossa195f912019-10-16 11:07:20 -07001232 field := os.Field + "_" + t.Name
1233 prefix := "target." + os.Name + "_" + t.Name
Colin Cross4157e882019-06-06 16:57:04 -07001234 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossd5934c82017-10-02 13:55:26 -07001235 }
1236
Colin Crossa195f912019-10-16 11:07:20 -07001237 // Handle arm on x86 properties in the form:
Colin Crossc17727d2018-10-24 12:42:09 -07001238 // target {
Colin Crossa195f912019-10-16 11:07:20 -07001239 // arm_on_x86 {
Colin Crossc17727d2018-10-24 12:42:09 -07001240 // key: value,
1241 // },
Colin Crossa195f912019-10-16 11:07:20 -07001242 // arm_on_x86_64 {
Colin Crossd5934c82017-10-02 13:55:26 -07001243 // key: value,
1244 // },
1245 // },
Colin Crossc17727d2018-10-24 12:42:09 -07001246 if os.Class == Device {
Victor Khimenko1a31f802020-09-17 03:07:31 +02001247 if arch.ArchType == X86 && (hasArmAbi(arch) ||
1248 hasArmAndroidArch(ctx.Config().Targets[Android])) {
Colin Crossc17727d2018-10-24 12:42:09 -07001249 field := "Arm_on_x86"
1250 prefix := "target.arm_on_x86"
Colin Cross4157e882019-06-06 16:57:04 -07001251 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001252 }
Victor Khimenko1a31f802020-09-17 03:07:31 +02001253 if arch.ArchType == X86_64 && (hasArmAbi(arch) ||
1254 hasArmAndroidArch(ctx.Config().Targets[Android])) {
Colin Crossc17727d2018-10-24 12:42:09 -07001255 field := "Arm_on_x86_64"
1256 prefix := "target.arm_on_x86_64"
Colin Cross4157e882019-06-06 16:57:04 -07001257 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001258 }
Victor Khimenkoc26fcf42020-05-07 22:16:33 +02001259 if os == Android && m.Target().NativeBridge == NativeBridgeEnabled {
1260 field := "Native_bridge"
1261 prefix := "target.native_bridge"
1262 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1263 }
Colin Cross4247f0d2017-04-13 16:56:14 -07001264 }
Colin Crossbb2e2b72016-12-08 17:23:53 -08001265 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001266 }
1267}
1268
Colin Crossa6845402020-11-16 15:08:19 -08001269// Convert the arch product variables into a list of targets for each OsType.
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001270func decodeTargetProductVariables(config *config) (map[OsType][]Target, error) {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001271 variables := config.productVariables
Dan Willemsen490fd492015-11-24 17:53:15 -08001272
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001273 targets := make(map[OsType][]Target)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001274 var targetErr error
1275
dimitry1f33e402019-03-26 12:39:31 +01001276 addTarget := func(os OsType, archName string, archVariant, cpuVariant *string, abi []string,
dimitry8d6dde82019-07-11 10:23:53 +02001277 nativeBridgeEnabled NativeBridgeSupport, nativeBridgeHostArchName *string,
1278 nativeBridgeRelativePath *string) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001279 if targetErr != nil {
1280 return
Dan Willemsen490fd492015-11-24 17:53:15 -08001281 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001282
Dan Willemsen01a3c252019-01-11 19:02:16 -08001283 arch, err := decodeArch(os, archName, archVariant, cpuVariant, abi)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001284 if err != nil {
1285 targetErr = err
1286 return
1287 }
dimitry8d6dde82019-07-11 10:23:53 +02001288 nativeBridgeRelativePathStr := String(nativeBridgeRelativePath)
1289 nativeBridgeHostArchNameStr := String(nativeBridgeHostArchName)
1290
1291 // Use guest arch as relative install path by default
1292 if nativeBridgeEnabled && nativeBridgeRelativePathStr == "" {
1293 nativeBridgeRelativePathStr = arch.ArchType.String()
1294 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001295
Jiyong Park1613e552020-09-14 19:43:17 +09001296 // A target is considered as HostCross if it's a host target which can't run natively on
1297 // the currently configured build machine (either because the OS is different or because of
1298 // the unsupported arch)
1299 hostCross := false
1300 if os.Class == Host {
1301 var osSupported bool
1302 if os == BuildOs {
1303 osSupported = true
1304 } else if BuildOs.Linux() && os.Linux() {
1305 // LinuxBionic and Linux are compatible
1306 osSupported = true
1307 } else {
1308 osSupported = false
1309 }
1310
1311 var archSupported bool
1312 if arch.ArchType == Common {
1313 archSupported = true
1314 } else if arch.ArchType.Name == *variables.HostArch {
1315 archSupported = true
1316 } else if variables.HostSecondaryArch != nil && arch.ArchType.Name == *variables.HostSecondaryArch {
1317 archSupported = true
1318 } else {
1319 archSupported = false
1320 }
1321 if !osSupported || !archSupported {
1322 hostCross = true
1323 }
1324 }
1325
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001326 targets[os] = append(targets[os],
Colin Crossa1ad8d12016-06-01 17:09:44 -07001327 Target{
dimitry8d6dde82019-07-11 10:23:53 +02001328 Os: os,
1329 Arch: arch,
1330 NativeBridge: nativeBridgeEnabled,
1331 NativeBridgeHostArchName: nativeBridgeHostArchNameStr,
1332 NativeBridgeRelativePath: nativeBridgeRelativePathStr,
Jiyong Park1613e552020-09-14 19:43:17 +09001333 HostCross: hostCross,
Colin Crossa1ad8d12016-06-01 17:09:44 -07001334 })
Dan Willemsen490fd492015-11-24 17:53:15 -08001335 }
1336
Colin Cross4225f652015-09-17 14:33:42 -07001337 if variables.HostArch == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001338 return nil, fmt.Errorf("No host primary architecture set")
Colin Cross4225f652015-09-17 14:33:42 -07001339 }
1340
Colin Crossa6845402020-11-16 15:08:19 -08001341 // The primary host target, which must always exist.
dimitry8d6dde82019-07-11 10:23:53 +02001342 addTarget(BuildOs, *variables.HostArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -07001343
Colin Crossa6845402020-11-16 15:08:19 -08001344 // An optional secondary host target.
Colin Crosseeabb892015-11-20 13:07:51 -08001345 if variables.HostSecondaryArch != nil && *variables.HostSecondaryArch != "" {
dimitry8d6dde82019-07-11 10:23:53 +02001346 addTarget(BuildOs, *variables.HostSecondaryArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -08001347 }
1348
Colin Crossa6845402020-11-16 15:08:19 -08001349 // Optional cross-compiled host targets, generally Windows.
Colin Crossff3ae9d2018-04-10 16:15:18 -07001350 if String(variables.CrossHost) != "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001351 crossHostOs := osByName(*variables.CrossHost)
1352 if crossHostOs == NoOsType {
1353 return nil, fmt.Errorf("Unknown cross host OS %q", *variables.CrossHost)
1354 }
1355
Colin Crossff3ae9d2018-04-10 16:15:18 -07001356 if String(variables.CrossHostArch) == "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001357 return nil, fmt.Errorf("No cross-host primary architecture set")
Dan Willemsen490fd492015-11-24 17:53:15 -08001358 }
1359
Colin Crossa6845402020-11-16 15:08:19 -08001360 // The primary cross-compiled host target.
dimitry8d6dde82019-07-11 10:23:53 +02001361 addTarget(crossHostOs, *variables.CrossHostArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -08001362
Colin Crossa6845402020-11-16 15:08:19 -08001363 // An optional secondary cross-compiled host target.
Dan Willemsen490fd492015-11-24 17:53:15 -08001364 if variables.CrossHostSecondaryArch != nil && *variables.CrossHostSecondaryArch != "" {
dimitry8d6dde82019-07-11 10:23:53 +02001365 addTarget(crossHostOs, *variables.CrossHostSecondaryArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -08001366 }
1367 }
1368
Colin Crossa6845402020-11-16 15:08:19 -08001369 // Optional device targets
Dan Willemsen3f32f032016-07-11 14:36:48 -07001370 if variables.DeviceArch != nil && *variables.DeviceArch != "" {
Doug Horn21b94272019-01-16 12:06:11 -08001371 var target = Android
1372 if Bool(variables.Fuchsia) {
1373 target = Fuchsia
1374 }
1375
Colin Crossa6845402020-11-16 15:08:19 -08001376 // The primary device target.
Doug Horn21b94272019-01-16 12:06:11 -08001377 addTarget(target, *variables.DeviceArch, variables.DeviceArchVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001378 variables.DeviceCpuVariant, variables.DeviceAbi, NativeBridgeDisabled, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -07001379
Colin Crossa6845402020-11-16 15:08:19 -08001380 // An optional secondary device target.
Dan Willemsen3f32f032016-07-11 14:36:48 -07001381 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" {
1382 addTarget(Android, *variables.DeviceSecondaryArch,
1383 variables.DeviceSecondaryArchVariant, variables.DeviceSecondaryCpuVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001384 variables.DeviceSecondaryAbi, NativeBridgeDisabled, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -07001385 }
dimitry1f33e402019-03-26 12:39:31 +01001386
Colin Crossa6845402020-11-16 15:08:19 -08001387 // An optional NativeBridge device target.
dimitry1f33e402019-03-26 12:39:31 +01001388 if variables.NativeBridgeArch != nil && *variables.NativeBridgeArch != "" {
1389 addTarget(Android, *variables.NativeBridgeArch,
1390 variables.NativeBridgeArchVariant, variables.NativeBridgeCpuVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001391 variables.NativeBridgeAbi, NativeBridgeEnabled, variables.DeviceArch,
1392 variables.NativeBridgeRelativePath)
dimitry1f33e402019-03-26 12:39:31 +01001393 }
1394
Colin Crossa6845402020-11-16 15:08:19 -08001395 // An optional secondary NativeBridge device target.
dimitry1f33e402019-03-26 12:39:31 +01001396 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" &&
1397 variables.NativeBridgeSecondaryArch != nil && *variables.NativeBridgeSecondaryArch != "" {
1398 addTarget(Android, *variables.NativeBridgeSecondaryArch,
1399 variables.NativeBridgeSecondaryArchVariant,
1400 variables.NativeBridgeSecondaryCpuVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001401 variables.NativeBridgeSecondaryAbi,
1402 NativeBridgeEnabled,
1403 variables.DeviceSecondaryArch,
1404 variables.NativeBridgeSecondaryRelativePath)
dimitry1f33e402019-03-26 12:39:31 +01001405 }
Colin Cross4225f652015-09-17 14:33:42 -07001406 }
1407
Colin Crossa1ad8d12016-06-01 17:09:44 -07001408 if targetErr != nil {
1409 return nil, targetErr
1410 }
1411
1412 return targets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001413}
1414
Colin Crossbb2e2b72016-12-08 17:23:53 -08001415// hasArmAbi returns true if arch has at least one arm ABI
1416func hasArmAbi(arch Arch) bool {
Jaewoong Jung3aff5782020-02-11 07:54:35 -08001417 return PrefixInList(arch.Abi, "arm")
Colin Crossbb2e2b72016-12-08 17:23:53 -08001418}
1419
dimitry628db6f2019-05-22 17:16:21 +02001420// hasArmArch returns true if targets has at least non-native_bridge arm Android arch
Colin Cross4247f0d2017-04-13 16:56:14 -07001421func hasArmAndroidArch(targets []Target) bool {
1422 for _, target := range targets {
Victor Khimenko1a31f802020-09-17 03:07:31 +02001423 if target.Os == Android && target.Arch.ArchType == Arm {
Victor Khimenko5eb8ec12018-03-21 20:30:54 +01001424 return true
1425 }
1426 }
1427 return false
1428}
1429
Colin Crossa6845402020-11-16 15:08:19 -08001430// archConfig describes a built-in configuration.
Dan Albert4098deb2016-10-19 14:04:41 -07001431type archConfig struct {
1432 arch string
1433 archVariant string
1434 cpuVariant string
1435 abi []string
1436}
1437
Colin Crossa6845402020-11-16 15:08:19 -08001438// getNdkAbisConfig returns a list of archConfigs for the ABIs supported by the NDK.
Dan Albert4098deb2016-10-19 14:04:41 -07001439func getNdkAbisConfig() []archConfig {
1440 return []archConfig{
Dan Albert6bba6442020-01-30 15:16:49 -08001441 {"arm", "armv7-a", "", []string{"armeabi-v7a"}},
Tamas Petzbca786d2021-01-20 18:56:33 +01001442 {"arm64", "armv8-a-branchprot", "", []string{"arm64-v8a"}},
Dan Albert4098deb2016-10-19 14:04:41 -07001443 {"x86", "", "", []string{"x86"}},
1444 {"x86_64", "", "", []string{"x86_64"}},
1445 }
1446}
1447
Colin Crossa6845402020-11-16 15:08:19 -08001448// getAmlAbisConfig returns a list of archConfigs for the ABIs supported by mainline modules.
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001449func getAmlAbisConfig() []archConfig {
1450 return []archConfig{
Martin Stjernholm93688342020-10-16 21:45:10 +01001451 {"arm", "armv7-a-neon", "", []string{"armeabi-v7a"}},
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001452 {"arm64", "armv8-a", "", []string{"arm64-v8a"}},
1453 {"x86", "", "", []string{"x86"}},
1454 {"x86_64", "", "", []string{"x86_64"}},
1455 }
1456}
1457
Colin Crossa6845402020-11-16 15:08:19 -08001458// decodeArchSettings converts a list of archConfigs into a list of Targets for the given OsType.
Dan Willemsen01a3c252019-01-11 19:02:16 -08001459func decodeArchSettings(os OsType, archConfigs []archConfig) ([]Target, error) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001460 var ret []Target
Dan Willemsen322acaf2016-01-12 23:07:05 -08001461
Dan Albert4098deb2016-10-19 14:04:41 -07001462 for _, config := range archConfigs {
Dan Willemsen01a3c252019-01-11 19:02:16 -08001463 arch, err := decodeArch(os, config.arch, &config.archVariant,
Colin Crossa74ca042019-01-31 14:31:51 -08001464 &config.cpuVariant, config.abi)
Dan Willemsen322acaf2016-01-12 23:07:05 -08001465 if err != nil {
1466 return nil, err
1467 }
Colin Cross3b19f5d2019-09-17 14:45:31 -07001468
Colin Crossa1ad8d12016-06-01 17:09:44 -07001469 ret = append(ret, Target{
1470 Os: Android,
1471 Arch: arch,
1472 })
Dan Willemsen322acaf2016-01-12 23:07:05 -08001473 }
1474
1475 return ret, nil
1476}
1477
Colin Crossa6845402020-11-16 15:08:19 -08001478// decodeArch converts a set of strings from product variables into an Arch struct.
Colin Crossa74ca042019-01-31 14:31:51 -08001479func decodeArch(os OsType, arch string, archVariant, cpuVariant *string, abi []string) (Arch, error) {
Colin Crossa6845402020-11-16 15:08:19 -08001480 // Verify the arch is valid
Colin Crosseeabb892015-11-20 13:07:51 -08001481 archType, ok := archTypeMap[arch]
1482 if !ok {
1483 return Arch{}, fmt.Errorf("unknown arch %q", arch)
1484 }
Colin Cross4225f652015-09-17 14:33:42 -07001485
Colin Crosseeabb892015-11-20 13:07:51 -08001486 a := Arch{
Colin Cross4225f652015-09-17 14:33:42 -07001487 ArchType: archType,
Colin Crossa6845402020-11-16 15:08:19 -08001488 ArchVariant: String(archVariant),
1489 CpuVariant: String(cpuVariant),
Colin Crossa74ca042019-01-31 14:31:51 -08001490 Abi: abi,
Colin Crosseeabb892015-11-20 13:07:51 -08001491 }
1492
Colin Crossa6845402020-11-16 15:08:19 -08001493 // Convert generic arch variants into the empty string.
Colin Crosseeabb892015-11-20 13:07:51 -08001494 if a.ArchVariant == a.ArchType.Name || a.ArchVariant == "generic" {
1495 a.ArchVariant = ""
1496 }
1497
Colin Crossa6845402020-11-16 15:08:19 -08001498 // Convert generic CPU variants into the empty string.
Colin Crosseeabb892015-11-20 13:07:51 -08001499 if a.CpuVariant == a.ArchType.Name || a.CpuVariant == "generic" {
1500 a.CpuVariant = ""
1501 }
1502
Colin Crossa6845402020-11-16 15:08:19 -08001503 // Filter empty ABIs out of the list.
Colin Crosseeabb892015-11-20 13:07:51 -08001504 for i := 0; i < len(a.Abi); i++ {
1505 if a.Abi[i] == "" {
1506 a.Abi = append(a.Abi[:i], a.Abi[i+1:]...)
1507 i--
1508 }
1509 }
1510
Dan Willemsen01a3c252019-01-11 19:02:16 -08001511 if a.ArchVariant == "" {
Colin Crossa6845402020-11-16 15:08:19 -08001512 // Set ArchFeatures from the default arch features.
Dan Willemsen01a3c252019-01-11 19:02:16 -08001513 if featureMap, ok := defaultArchFeatureMap[os]; ok {
1514 a.ArchFeatures = featureMap[archType]
1515 }
1516 } else {
Colin Crossa6845402020-11-16 15:08:19 -08001517 // Set ArchFeatures from the arch type.
Dan Willemsen01a3c252019-01-11 19:02:16 -08001518 if featureMap, ok := archFeatureMap[archType]; ok {
1519 a.ArchFeatures = featureMap[a.ArchVariant]
1520 }
Colin Crossc5c24ad2015-11-20 15:35:00 -08001521 }
1522
Colin Crosseeabb892015-11-20 13:07:51 -08001523 return a, nil
Colin Cross4225f652015-09-17 14:33:42 -07001524}
1525
Colin Crossa6845402020-11-16 15:08:19 -08001526// filterMultilibTargets takes a list of Targets and a multilib value and returns a new list of
1527// Targets containing only those that have the given multilib value.
Colin Cross69617d32016-09-06 10:39:07 -07001528func filterMultilibTargets(targets []Target, multilib string) []Target {
1529 var ret []Target
1530 for _, t := range targets {
1531 if t.Arch.ArchType.Multilib == multilib {
1532 ret = append(ret, t)
1533 }
1534 }
1535 return ret
1536}
1537
Colin Crossa6845402020-11-16 15:08:19 -08001538// getCommonTargets returns the set of Os specific common architecture targets for each Os in a list
1539// of targets.
Nan Zhangdb0b9a32017-02-27 10:12:13 -08001540func getCommonTargets(targets []Target) []Target {
1541 var ret []Target
1542 set := make(map[string]bool)
1543
1544 for _, t := range targets {
1545 if _, found := set[t.Os.String()]; !found {
1546 set[t.Os.String()] = true
1547 ret = append(ret, commonTargetMap[t.Os.String()])
1548 }
1549 }
1550
1551 return ret
1552}
1553
Colin Crossa6845402020-11-16 15:08:19 -08001554// firstTarget takes a list of Targets and a list of multilib values and returns a list of Targets
1555// that contains zero or one Target for each OsType, selecting the one that matches the earliest
1556// filter.
Colin Cross3dceee32018-09-06 10:19:57 -07001557func firstTarget(targets []Target, filters ...string) []Target {
Jiyong Park22101982020-09-17 19:09:58 +09001558 // find the first target from each OS
1559 var ret []Target
1560 hasHost := false
1561 set := make(map[OsType]bool)
1562
Colin Cross6b4a32d2017-12-05 13:42:45 -08001563 for _, filter := range filters {
1564 buildTargets := filterMultilibTargets(targets, filter)
Jiyong Park22101982020-09-17 19:09:58 +09001565 for _, t := range buildTargets {
1566 if _, found := set[t.Os]; !found {
1567 hasHost = hasHost || (t.Os.Class == Host)
1568 set[t.Os] = true
1569 ret = append(ret, t)
1570 }
Colin Cross6b4a32d2017-12-05 13:42:45 -08001571 }
1572 }
Jiyong Park22101982020-09-17 19:09:58 +09001573 return ret
Colin Cross6b4a32d2017-12-05 13:42:45 -08001574}
1575
Colin Crossa6845402020-11-16 15:08:19 -08001576// decodeMultilibTargets uses the module's multilib setting to select one or more targets from a
1577// list of Targets.
Colin Crossee0bc3b2018-10-02 22:01:37 -07001578func decodeMultilibTargets(multilib string, targets []Target, prefer32 bool) ([]Target, error) {
Colin Crossa6845402020-11-16 15:08:19 -08001579 var buildTargets []Target
Colin Cross6b4a32d2017-12-05 13:42:45 -08001580
Colin Cross4225f652015-09-17 14:33:42 -07001581 switch multilib {
1582 case "common":
Colin Cross6b4a32d2017-12-05 13:42:45 -08001583 buildTargets = getCommonTargets(targets)
1584 case "common_first":
1585 buildTargets = getCommonTargets(targets)
1586 if prefer32 {
Colin Cross3dceee32018-09-06 10:19:57 -07001587 buildTargets = append(buildTargets, firstTarget(targets, "lib32", "lib64")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001588 } else {
Colin Cross3dceee32018-09-06 10:19:57 -07001589 buildTargets = append(buildTargets, firstTarget(targets, "lib64", "lib32")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001590 }
Colin Cross4225f652015-09-17 14:33:42 -07001591 case "both":
Colin Cross8b74d172016-09-13 09:59:14 -07001592 if prefer32 {
1593 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1594 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1595 } else {
1596 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1597 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1598 }
Colin Cross4225f652015-09-17 14:33:42 -07001599 case "32":
Colin Cross69617d32016-09-06 10:39:07 -07001600 buildTargets = filterMultilibTargets(targets, "lib32")
Colin Cross4225f652015-09-17 14:33:42 -07001601 case "64":
Colin Cross69617d32016-09-06 10:39:07 -07001602 buildTargets = filterMultilibTargets(targets, "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001603 case "first":
1604 if prefer32 {
Colin Cross3dceee32018-09-06 10:19:57 -07001605 buildTargets = firstTarget(targets, "lib32", "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001606 } else {
Colin Cross3dceee32018-09-06 10:19:57 -07001607 buildTargets = firstTarget(targets, "lib64", "lib32")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001608 }
Victor Chang9448e8f2020-09-14 15:34:16 +01001609 case "first_prefer32":
1610 buildTargets = firstTarget(targets, "lib32", "lib64")
Colin Cross69617d32016-09-06 10:39:07 -07001611 case "prefer32":
Colin Cross3dceee32018-09-06 10:19:57 -07001612 buildTargets = filterMultilibTargets(targets, "lib32")
1613 if len(buildTargets) == 0 {
1614 buildTargets = filterMultilibTargets(targets, "lib64")
1615 }
Colin Cross4225f652015-09-17 14:33:42 -07001616 default:
Victor Chang9448e8f2020-09-14 15:34:16 +01001617 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 -07001618 multilib)
Colin Cross4225f652015-09-17 14:33:42 -07001619 }
1620
Colin Crossa1ad8d12016-06-01 17:09:44 -07001621 return buildTargets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001622}
Jingwen Chen5d864492021-02-24 07:20:12 -05001623
1624// GetArchProperties returns a map of architectures to the values of the
1625// properties of the 'dst' struct that are specific to that architecture.
1626//
1627// For example, passing a struct { Foo bool, Bar string } will return an
1628// interface{} that can be type asserted back into the same struct, containing
1629// the arch specific property value specified by the module if defined.
1630func (m *ModuleBase) GetArchProperties(dst interface{}) map[ArchType]interface{} {
1631 // Return value of the arch types to the prop values for that arch.
1632 archToProp := map[ArchType]interface{}{}
1633
1634 // Nothing to do for non-arch-specific modules.
1635 if !m.ArchSpecific() {
1636 return archToProp
1637 }
1638
1639 // archProperties has the type of [][]interface{}. Looks complicated, so let's
1640 // explain this step by step.
1641 //
1642 // Loop over the outer index, which determines the property struct that
1643 // contains a matching set of properties in dst that we're interested in.
1644 // For example, BaseCompilerProperties or BaseLinkerProperties.
1645 for i := range m.archProperties {
1646 if m.archProperties[i] == nil {
1647 // Skip over nil arch props
1648 continue
1649 }
1650
1651 // Non-nil arch prop, let's see if the props match up.
1652 for _, arch := range ArchTypeList() {
1653 // e.g X86, Arm
1654 field := arch.Field
1655
1656 // If it's not nil, loop over the inner index, which determines the arch variant
1657 // of the prop type. In an Android.bp file, this is like looping over:
1658 //
1659 // arch: { arm: { key: value, ... }, x86: { key: value, ... } }
1660 for _, archProperties := range m.archProperties[i] {
1661 archPropValues := reflect.ValueOf(archProperties).Elem()
1662
1663 // This is the archPropRoot struct. Traverse into the Arch nested struct.
1664 src := archPropValues.FieldByName("Arch").Elem()
1665
1666 // Step into non-nil pointers to structs in the src value.
1667 if src.Kind() == reflect.Ptr {
1668 if src.IsNil() {
1669 // Ignore nil pointers.
1670 continue
1671 }
1672 src = src.Elem()
1673 }
1674
1675 // Find the requested field (e.g. x86, x86_64) in the src struct.
1676 src = src.FieldByName(field)
1677 if !src.IsValid() {
1678 continue
1679 }
1680
1681 // We only care about structs. These are not the droids you are looking for.
1682 if src.Kind() != reflect.Struct {
1683 continue
1684 }
1685
1686 // If the value of the field is a struct then step into the
1687 // BlueprintEmbed field. The special "BlueprintEmbed" name is
1688 // used by createArchPropTypeDesc to embed the arch properties
1689 // in the parent struct, so the src arch prop should be in this
1690 // field.
1691 //
1692 // See createArchPropTypeDesc for more details on how Arch-specific
1693 // module properties are processed from the nested props and written
1694 // into the module's archProperties.
1695 src = src.FieldByName("BlueprintEmbed")
1696
1697 // Clone the destination prop, since we want a unique prop struct per arch.
1698 dstClone := reflect.New(reflect.ValueOf(dst).Elem().Type()).Interface()
1699
1700 // Copy the located property struct into the cloned destination property struct.
1701 err := proptools.ExtendMatchingProperties([]interface{}{dstClone}, src.Interface(), nil, proptools.OrderReplace)
1702 if err != nil {
1703 // This is fine, it just means the src struct doesn't match.
1704 continue
1705 }
1706
1707 // Found the prop for the arch, you have.
1708 archToProp[arch] = dstClone
1709
1710 // Go to the next prop.
1711 break
1712 }
1713 }
1714 }
1715 return archToProp
1716}
Jingwen Chen91220d72021-03-24 02:18:33 -04001717
1718// GetTargetProperties returns a map of OS target (e.g. android, windows) to the
1719// values of the properties of the 'dst' struct that are specific to that OS
1720// target.
1721//
1722// For example, passing a struct { Foo bool, Bar string } will return an
1723// interface{} that can be type asserted back into the same struct, containing
1724// the os-specific property value specified by the module if defined.
1725//
1726// While this looks similar to GetArchProperties, the internal representation of
1727// the properties have a slightly different layout to warrant a standalone
1728// lookup function.
1729func (m *ModuleBase) GetTargetProperties(dst interface{}) map[OsType]interface{} {
1730 // Return value of the arch types to the prop values for that arch.
1731 osToProp := map[OsType]interface{}{}
1732
1733 // Nothing to do for non-OS/arch-specific modules.
1734 if !m.ArchSpecific() {
1735 return osToProp
1736 }
1737
1738 // archProperties has the type of [][]interface{}. Looks complicated, so
1739 // let's explain this step by step.
1740 //
1741 // Loop over the outer index, which determines the property struct that
1742 // contains a matching set of properties in dst that we're interested in.
1743 // For example, BaseCompilerProperties or BaseLinkerProperties.
1744 for i := range m.archProperties {
1745 if m.archProperties[i] == nil {
1746 continue
1747 }
1748
1749 // Iterate over the supported OS types
Jingwen Chen2f6a21e2021-04-05 07:33:05 +00001750 for _, os := range osTypeList {
Jingwen Chen91220d72021-03-24 02:18:33 -04001751 // e.g android, linux_bionic
1752 field := os.Field
1753
1754 // If it's not nil, loop over the inner index, which determines the arch variant
1755 // of the prop type. In an Android.bp file, this is like looping over:
1756 //
1757 // target: { android: { key: value, ... }, linux_bionic: { key: value, ... } }
1758 for _, archProperties := range m.archProperties[i] {
1759 archPropValues := reflect.ValueOf(archProperties).Elem()
1760
1761 // This is the archPropRoot struct. Traverse into the Targetnested struct.
1762 src := archPropValues.FieldByName("Target").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 continue
1768 }
1769 src = src.Elem()
1770 }
1771
1772 // Find the requested field (e.g. android, linux_bionic) in the src struct.
1773 src = src.FieldByName(field)
1774
1775 // Validation steps. We want valid non-nil pointers to structs.
1776 if !src.IsValid() || src.IsNil() {
1777 continue
1778 }
1779
1780 if src.Kind() != reflect.Ptr || src.Elem().Kind() != reflect.Struct {
1781 continue
1782 }
1783
1784 // Clone the destination prop, since we want a unique prop struct per arch.
1785 dstClone := reflect.New(reflect.ValueOf(dst).Elem().Type()).Interface()
1786
1787 // Copy the located property struct into the cloned destination property struct.
1788 err := proptools.ExtendMatchingProperties([]interface{}{dstClone}, src.Interface(), nil, proptools.OrderReplace)
1789 if err != nil {
1790 // This is fine, it just means the src struct doesn't match.
1791 continue
1792 }
1793
1794 // Found the prop for the os, you have.
1795 osToProp[os] = dstClone
1796
1797 // Go to the next prop.
1798 break
1799 }
1800 }
1801 }
1802 return osToProp
1803}