blob: eb651e6188f44487952e6b51bfb113a88cb9c169 [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"
Colin Cross74ba9622019-02-11 15:11:14 -080022 "strconv"
Colin Cross3f40fa42015-01-30 17:27:36 -080023 "strings"
Colin Crossf6566ed2015-03-24 11:13:38 -070024
Colin Cross0f7d2ef2019-10-16 11:03:10 -070025 "github.com/google/blueprint"
Colin Cross617b88a2020-08-24 18:04:09 -070026 "github.com/google/blueprint/bootstrap"
Colin Crossf6566ed2015-03-24 11:13:38 -070027 "github.com/google/blueprint/proptools"
Colin Cross3f40fa42015-01-30 17:27:36 -080028)
29
Colin Cross3f40fa42015-01-30 17:27:36 -080030/*
31Example blueprints file containing all variant property groups, with comment listing what type
32of variants get properties in that group:
33
34module {
35 arch: {
36 arm: {
37 // Host or device variants with arm architecture
38 },
39 arm64: {
40 // Host or device variants with arm64 architecture
41 },
Colin Cross3f40fa42015-01-30 17:27:36 -080042 x86: {
43 // Host or device variants with x86 architecture
44 },
45 x86_64: {
46 // Host or device variants with x86_64 architecture
47 },
48 },
49 multilib: {
50 lib32: {
51 // Host or device variants for 32-bit architectures
52 },
53 lib64: {
54 // Host or device variants for 64-bit architectures
55 },
56 },
57 target: {
58 android: {
Martin Stjernholme284b482020-09-23 21:03:27 +010059 // Device variants (implies Bionic)
Colin Cross3f40fa42015-01-30 17:27:36 -080060 },
61 host: {
62 // Host variants
63 },
Martin Stjernholme284b482020-09-23 21:03:27 +010064 bionic: {
65 // Bionic (device and host) variants
66 },
67 linux_bionic: {
68 // Bionic host variants
69 },
70 linux: {
71 // Bionic (device and host) and Linux glibc variants
72 },
Dan Willemsen5746bd42017-10-02 19:42:01 -070073 linux_glibc: {
Martin Stjernholme284b482020-09-23 21:03:27 +010074 // Linux host variants (using non-Bionic libc)
Colin Cross3f40fa42015-01-30 17:27:36 -080075 },
76 darwin: {
77 // Darwin host variants
78 },
79 windows: {
80 // Windows host variants
81 },
82 not_windows: {
83 // Non-windows host variants
84 },
Martin Stjernholme284b482020-09-23 21:03:27 +010085 android_arm: {
86 // Any <os>_<arch> combination restricts to that os and arch
87 },
Colin Cross3f40fa42015-01-30 17:27:36 -080088 },
89}
90*/
Colin Cross7d5136f2015-05-11 13:39:40 -070091
Colin Cross3f40fa42015-01-30 17:27:36 -080092// An Arch indicates a single CPU architecture.
93type Arch struct {
Colin Crossa6845402020-11-16 15:08:19 -080094 // The type of the architecture (arm, arm64, x86, or x86_64).
95 ArchType ArchType
96
97 // The variant of the architecture, for example "armv7-a" or "armv7-a-neon" for arm.
98 ArchVariant string
99
100 // The variant of the CPU, for example "cortex-a53" for arm64.
101 CpuVariant string
102
103 // The list of Android app ABIs supported by the CPU architecture, for example "arm64-v8a".
104 Abi []string
105
106 // The list of arch-specific features supported by the CPU architecture, for example "neon".
Colin Crossc5c24ad2015-11-20 15:35:00 -0800107 ArchFeatures []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800108}
109
Colin Crossa6845402020-11-16 15:08:19 -0800110// String returns the Arch as a string. The value is used as the name of the variant created
111// by archMutator.
Colin Cross3f40fa42015-01-30 17:27:36 -0800112func (a Arch) String() string {
Colin Crossd3ba0392015-05-07 14:11:29 -0700113 s := a.ArchType.String()
Colin Cross3f40fa42015-01-30 17:27:36 -0800114 if a.ArchVariant != "" {
115 s += "_" + a.ArchVariant
116 }
117 if a.CpuVariant != "" {
118 s += "_" + a.CpuVariant
119 }
120 return s
121}
122
Colin Crossa6845402020-11-16 15:08:19 -0800123// ArchType is used to define the 4 supported architecture types (arm, arm64, x86, x86_64), as
124// well as the "common" architecture used for modules that support multiple architectures, for
125// example Java modules.
Colin Cross3f40fa42015-01-30 17:27:36 -0800126type ArchType struct {
Colin Crossa6845402020-11-16 15:08:19 -0800127 // Name is the name of the architecture type, "arm", "arm64", "x86", or "x86_64".
128 Name string
129
130 // Field is the name of the field used in properties that refer to the architecture, e.g. "Arm64".
131 Field string
132
133 // Multilib is either "lib32" or "lib64" for 32-bit or 64-bit architectures.
Colin Crossec193632015-07-06 17:49:43 -0700134 Multilib string
Colin Cross3f40fa42015-01-30 17:27:36 -0800135}
136
Colin Crossa6845402020-11-16 15:08:19 -0800137// String returns the name of the ArchType.
138func (a ArchType) String() string {
139 return a.Name
140}
141
142const COMMON_VARIANT = "common"
143
144var (
145 archTypeList []ArchType
146
147 Arm = newArch("arm", "lib32")
148 Arm64 = newArch("arm64", "lib64")
149 X86 = newArch("x86", "lib32")
150 X86_64 = newArch("x86_64", "lib64")
151
152 Common = ArchType{
153 Name: COMMON_VARIANT,
154 }
155)
156
157var archTypeMap = map[string]ArchType{}
158
Colin Crossec193632015-07-06 17:49:43 -0700159func newArch(name, multilib string) ArchType {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700160 archType := ArchType{
Colin Crossec193632015-07-06 17:49:43 -0700161 Name: name,
Dan Willemsenb1957a52016-06-23 23:44:54 -0700162 Field: proptools.FieldNameForProperty(name),
Colin Crossec193632015-07-06 17:49:43 -0700163 Multilib: multilib,
Colin Cross3f40fa42015-01-30 17:27:36 -0800164 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700165 archTypeList = append(archTypeList, archType)
Colin Crossa6845402020-11-16 15:08:19 -0800166 archTypeMap[name] = archType
Dan Willemsenb1957a52016-06-23 23:44:54 -0700167 return archType
Colin Cross3f40fa42015-01-30 17:27:36 -0800168}
169
Colin Crossa6845402020-11-16 15:08:19 -0800170// ArchTypeList returns the 4 supported ArchTypes for arm, 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) {
178 return []byte(strconv.Quote(a.String())), nil
179}
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 }
Paul Duffina04c1072020-03-02 10:16:35 +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 Crossb5ae1932020-11-17 06:32:06 +0000274 commonTargetMap[name] = Target{Os: os, Arch: Arch{ArchType: Common}}
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 {
Paul Duffina04c1072020-03-02 10:16:35 +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 (
315 // OsTypeList contains a list of all the supported OsTypes, including ones not supported
316 // by the current build host or the target device.
317 OsTypeList []OsType
318 // 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)
dimitry1f33e402019-03-26 12:39:31 +0100344)
345
Colin Crossa6845402020-11-16 15:08:19 -0800346// Target specifies the OS and architecture that a module is being compiled for.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700347type Target struct {
Colin Crossa6845402020-11-16 15:08:19 -0800348 // Os the OS that the module is being compiled for (e.g. "linux_glibc", "android").
349 Os OsType
350 // Arch is the architecture that the module is being compiled for.
351 Arch Arch
352 // NativeBridge is NativeBridgeEnabled if the architecture is supported using NativeBridge
353 // (i.e. arm on x86) for this device.
354 NativeBridge NativeBridgeSupport
355 // NativeBridgeHostArchName is the name of the real architecture that is used to implement
356 // the NativeBridge architecture. For example, for arm on x86 this would be "x86".
dimitry8d6dde82019-07-11 10:23:53 +0200357 NativeBridgeHostArchName string
Colin Crossa6845402020-11-16 15:08:19 -0800358 // NativeBridgeRelativePath is the name of the subdirectory that will contain NativeBridge
359 // libraries and binaries.
dimitry8d6dde82019-07-11 10:23:53 +0200360 NativeBridgeRelativePath string
Jiyong Park1613e552020-09-14 19:43:17 +0900361
362 // HostCross is true when the target cannot run natively on the current build host.
363 // For example, linux_glibc_x86 returns true on a regular x86/i686/Linux machines, but returns false
364 // on Mac (different OS), or on 64-bit only i686/Linux machines (unsupported arch).
365 HostCross bool
Colin Crossd3ba0392015-05-07 14:11:29 -0700366}
367
Colin Crossa6845402020-11-16 15:08:19 -0800368// NativeBridgeSupport is an enum that specifies if a Target supports NativeBridge.
369type NativeBridgeSupport bool
370
371const (
372 NativeBridgeDisabled NativeBridgeSupport = false
373 NativeBridgeEnabled NativeBridgeSupport = true
374)
375
376// String returns the OS and arch variations used for the Target.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700377func (target Target) String() string {
Colin Crossa195f912019-10-16 11:07:20 -0700378 return target.OsVariation() + "_" + target.ArchVariation()
379}
380
Colin Crossa6845402020-11-16 15:08:19 -0800381// OsVariation returns the name of the variation used by the osMutator for the Target.
Colin Crossa195f912019-10-16 11:07:20 -0700382func (target Target) OsVariation() string {
383 return target.Os.String()
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700384}
385
Colin Crossa6845402020-11-16 15:08:19 -0800386// ArchVariation returns the name of the variation used by the archMutator for the Target.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700387func (target Target) ArchVariation() string {
388 var variation string
dimitry1f33e402019-03-26 12:39:31 +0100389 if target.NativeBridge {
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700390 variation = "native_bridge_"
dimitry1f33e402019-03-26 12:39:31 +0100391 }
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700392 variation += target.Arch.String()
393
Colin Crossa195f912019-10-16 11:07:20 -0700394 return variation
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700395}
396
Colin Crossa6845402020-11-16 15:08:19 -0800397// Variations returns a list of blueprint.Variations for the osMutator and archMutator for the
398// Target.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700399func (target Target) Variations() []blueprint.Variation {
400 return []blueprint.Variation{
Colin Crossa195f912019-10-16 11:07:20 -0700401 {Mutator: "os", Variation: target.OsVariation()},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700402 {Mutator: "arch", Variation: target.ArchVariation()},
403 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800404}
405
Colin Crossa6845402020-11-16 15:08:19 -0800406// osMutator splits an arch-specific module into a variant for each OS that is enabled for the
407// module. It uses the HostOrDevice value passed to InitAndroidArchModule and the
408// device_supported and host_supported properties to determine which OsTypes are enabled for this
409// module, then searches through the Targets to determine which have enabled Targets for this
410// module.
Colin Cross617b88a2020-08-24 18:04:09 -0700411func osMutator(bpctx blueprint.BottomUpMutatorContext) {
Colin Crossa195f912019-10-16 11:07:20 -0700412 var module Module
413 var ok bool
Colin Cross617b88a2020-08-24 18:04:09 -0700414 if module, ok = bpctx.Module().(Module); !ok {
Colin Crossa6845402020-11-16 15:08:19 -0800415 // The module is not a Soong module, it is a Blueprint module.
Colin Cross617b88a2020-08-24 18:04:09 -0700416 if bootstrap.IsBootstrapModule(bpctx.Module()) {
417 // Bootstrap Go modules are always the build OS or linux bionic.
418 config := bpctx.Config().(Config)
419 osNames := []string{config.BuildOSTarget.OsVariation()}
420 for _, hostCrossTarget := range config.Targets[LinuxBionic] {
421 if hostCrossTarget.Arch.ArchType == config.BuildOSTarget.Arch.ArchType {
422 osNames = append(osNames, hostCrossTarget.OsVariation())
423 }
424 }
425 osNames = FirstUniqueStrings(osNames)
426 bpctx.CreateVariations(osNames...)
427 }
Colin Crossa195f912019-10-16 11:07:20 -0700428 return
429 }
430
Colin Cross617b88a2020-08-24 18:04:09 -0700431 // Bootstrap Go module support above requires this mutator to be a
432 // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext
433 // filters out non-Soong modules. Now that we've handled them, create a
434 // normal android.BottomUpMutatorContext.
435 mctx := bottomUpMutatorContextFactory(bpctx, module, false)
436
Colin Crossa195f912019-10-16 11:07:20 -0700437 base := module.base()
438
Colin Crossa6845402020-11-16 15:08:19 -0800439 // Nothing to do for modules that are not architecture specific (e.g. a genrule).
Colin Crossa195f912019-10-16 11:07:20 -0700440 if !base.ArchSpecific() {
441 return
442 }
443
Colin Crossa6845402020-11-16 15:08:19 -0800444 // Collect a list of OSTypes supported by this module based on the HostOrDevice value
445 // passed to InitAndroidArchModule and the device_supported and host_supported properties.
Colin Crossa195f912019-10-16 11:07:20 -0700446 var moduleOSList []OsType
Paul Duffina04c1072020-03-02 10:16:35 +0000447 for _, os := range OsTypeList {
Jiyong Park1613e552020-09-14 19:43:17 +0900448 for _, t := range mctx.Config().Targets[os] {
Colin Cross08d6f8f2020-11-19 02:33:19 +0000449 if base.supportsTarget(t) {
Jiyong Park1613e552020-09-14 19:43:17 +0900450 moduleOSList = append(moduleOSList, os)
451 break
Colin Crossa195f912019-10-16 11:07:20 -0700452 }
453 }
Colin Crossa195f912019-10-16 11:07:20 -0700454 }
455
Colin Crossa6845402020-11-16 15:08:19 -0800456 // If there are no supported OSes then disable the module.
Colin Crossa195f912019-10-16 11:07:20 -0700457 if len(moduleOSList) == 0 {
Inseob Kimeec88e12020-01-22 11:11:29 +0900458 base.Disable()
Colin Crossa195f912019-10-16 11:07:20 -0700459 return
460 }
461
Colin Crossa6845402020-11-16 15:08:19 -0800462 // Convert the list of supported OsTypes to the variation names.
Colin Crossa195f912019-10-16 11:07:20 -0700463 osNames := make([]string, len(moduleOSList))
Colin Crossa195f912019-10-16 11:07:20 -0700464 for i, os := range moduleOSList {
465 osNames[i] = os.String()
466 }
467
Paul Duffin1356d8c2020-02-25 19:26:33 +0000468 createCommonOSVariant := base.commonProperties.CreateCommonOSVariant
469 if createCommonOSVariant {
Colin Crossa6845402020-11-16 15:08:19 -0800470 // A CommonOS variant was requested so add it to the list of OS variants to
Paul Duffin1356d8c2020-02-25 19:26:33 +0000471 // create. It needs to be added to the end because it needs to depend on the
472 // the other variants in the list returned by CreateVariations(...) and inter
473 // variant dependencies can only be created from a later variant in that list to
474 // an earlier one. That is because variants are always processed in the order in
475 // which they are returned from CreateVariations(...).
476 osNames = append(osNames, CommonOS.Name)
477 moduleOSList = append(moduleOSList, CommonOS)
Colin Crossa195f912019-10-16 11:07:20 -0700478 }
479
Colin Crossa6845402020-11-16 15:08:19 -0800480 // Create the variations, annotate each one with which OS it was created for, and
481 // squash the appropriate OS-specific properties into the top level properties.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000482 modules := mctx.CreateVariations(osNames...)
483 for i, m := range modules {
484 m.base().commonProperties.CompileOS = moduleOSList[i]
485 m.base().setOSProperties(mctx)
486 }
487
488 if createCommonOSVariant {
489 // A CommonOS variant was requested so add dependencies from it (the last one in
490 // the list) to the OS type specific variants.
491 last := len(modules) - 1
492 commonOSVariant := modules[last]
493 commonOSVariant.base().commonProperties.CommonOSVariant = true
494 for _, module := range modules[0:last] {
495 // Ignore modules that are enabled. Note, this will only avoid adding
496 // dependencies on OsType variants that are explicitly disabled in their
497 // properties. The CommonOS variant will still depend on disabled variants
498 // if they are disabled afterwards, e.g. in archMutator if
499 if module.Enabled() {
500 mctx.AddInterVariantDependency(commonOsToOsSpecificVariantTag, commonOSVariant, module)
501 }
502 }
503 }
504}
505
Colin Crossc179ea62020-10-09 10:54:15 -0700506type archDepTag struct {
507 blueprint.BaseDependencyTag
508 name string
509}
Paul Duffin1356d8c2020-02-25 19:26:33 +0000510
Colin Crossc179ea62020-10-09 10:54:15 -0700511// Identifies the dependency from CommonOS variant to the os specific variants.
512var commonOsToOsSpecificVariantTag = archDepTag{name: "common os to os specific"}
513
Colin Crossb5ae1932020-11-17 06:32:06 +0000514// Identifies the dependency from arch variant to the common variant for a "common_first" multilib.
515var firstArchToCommonArchDepTag = archDepTag{name: "first arch to common arch"}
516
Paul Duffin1356d8c2020-02-25 19:26:33 +0000517// Get the OsType specific variants for the current CommonOS variant.
518//
519// The returned list will only contain enabled OsType specific variants of the
520// module referenced in the supplied context. An empty list is returned if there
521// are no enabled variants or the supplied context is not for an CommonOS
522// variant.
523func GetOsSpecificVariantsOfCommonOSVariant(mctx BaseModuleContext) []Module {
524 var variants []Module
525 mctx.VisitDirectDeps(func(m Module) {
526 if mctx.OtherModuleDependencyTag(m) == commonOsToOsSpecificVariantTag {
527 if m.Enabled() {
528 variants = append(variants, m)
529 }
530 }
531 })
Paul Duffin1356d8c2020-02-25 19:26:33 +0000532 return variants
Colin Crossa195f912019-10-16 11:07:20 -0700533}
534
Colin Crossee0bc3b2018-10-02 22:01:37 -0700535// archMutator splits a module into a variant for each Target requested by the module. Target selection
Colin Crossa6845402020-11-16 15:08:19 -0800536// for a module is in three levels, OsClass, multilib, and then Target.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700537// OsClass selection is determined by:
538// - The HostOrDeviceSupported value passed in to InitAndroidArchModule by the module type factory, which selects
539// whether the module type can compile for host, device or both.
540// - The host_supported and device_supported properties on the module.
Roland Levillainf5b635d2019-06-05 14:42:57 +0100541// 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 -0700542// for the module, the Device OsClass is selected.
543// Within each selected OsClass, the multilib selection is determined by:
Jaewoong Jung02b2d4d2019-06-06 15:19:57 -0700544// - The compile_multilib property if it set (which may be overridden by target.android.compile_multilib or
Colin Crossee0bc3b2018-10-02 22:01:37 -0700545// target.host.compile_multilib).
546// - The default multilib passed to InitAndroidArchModule if compile_multilib was not set.
547// Valid multilib values include:
548// "both": compile for all Targets supported by the OsClass (generally x86_64 and x86, or arm64 and arm).
549// "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 -0700550// but may be arm for a 32-bit only build.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700551// "32": compile for only a single 32-bit Target supported by the OsClass.
552// "64": compile for only a single 64-bit Target supported by the OsClass.
Colin Crossa6845402020-11-16 15:08:19 -0800553// "common": compile a for a single Target that will work on all Targets supported by the OsClass (for example Java).
554// "common_first": compile a for a Target that will work on all Targets supported by the OsClass
555// (same as "common"), plus a second Target for the preferred Target supported by the OsClass
556// (same as "first"). This is used for java_binary that produces a common .jar and a wrapper
557// executable script.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700558//
559// Once the list of Targets is determined, the module is split into a variant for each Target.
560//
561// Modules can be initialized with InitAndroidMultiTargetsArchModule, in which case they will be split by OsClass,
562// 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 -0700563func archMutator(bpctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700564 var module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800565 var ok bool
Colin Cross617b88a2020-08-24 18:04:09 -0700566 if module, ok = bpctx.Module().(Module); !ok {
567 if bootstrap.IsBootstrapModule(bpctx.Module()) {
568 // Bootstrap Go modules are always the build architecture.
569 bpctx.CreateVariations(bpctx.Config().(Config).BuildOSTarget.ArchVariation())
570 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800571 return
572 }
573
Colin Cross617b88a2020-08-24 18:04:09 -0700574 // Bootstrap Go module support above requires this mutator to be a
575 // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext
576 // filters out non-Soong modules. Now that we've handled them, create a
577 // normal android.BottomUpMutatorContext.
578 mctx := bottomUpMutatorContextFactory(bpctx, module, false)
579
Colin Cross5eca7cb2018-10-02 14:02:10 -0700580 base := module.base()
581
582 if !base.ArchSpecific() {
Colin Crossb9db4802016-06-03 01:50:47 +0000583 return
584 }
585
Colin Crossa195f912019-10-16 11:07:20 -0700586 os := base.commonProperties.CompileOS
Paul Duffin1356d8c2020-02-25 19:26:33 +0000587 if os == CommonOS {
588 // Make sure that the target related properties are initialized for the
589 // CommonOS variant.
590 addTargetProperties(module, commonTargetMap[os.Name], nil, true)
591
592 // Do not create arch specific variants for the CommonOS variant.
593 return
594 }
595
Colin Crossa195f912019-10-16 11:07:20 -0700596 osTargets := mctx.Config().Targets[os]
Colin Crossfb0c16e2019-11-20 17:12:35 -0800597 image := base.commonProperties.ImageVariation
Colin Crossa6845402020-11-16 15:08:19 -0800598 // Filter NativeBridge targets unless they are explicitly supported.
599 // Skip creating native bridge variants for non-core modules.
Colin Cross83bead42019-12-18 10:45:46 -0800600 if os == Android &&
601 !(Bool(base.commonProperties.Native_bridge_supported) && image == CoreVariation) {
602
Colin Crossa195f912019-10-16 11:07:20 -0700603 var targets []Target
604 for _, t := range osTargets {
605 if !t.NativeBridge {
606 targets = append(targets, t)
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700607 }
608 }
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700609
Colin Crossa195f912019-10-16 11:07:20 -0700610 osTargets = targets
611 }
Colin Crossee0bc3b2018-10-02 22:01:37 -0700612
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700613 // only the primary arch in the ramdisk / vendor_ramdisk / recovery partition
614 if os == Android && (module.InstallInRecovery() || module.InstallInRamdisk() || module.InstallInVendorRamdisk()) {
Colin Crossa195f912019-10-16 11:07:20 -0700615 osTargets = []Target{osTargets[0]}
616 }
dimitry1f33e402019-03-26 12:39:31 +0100617
Colin Crossa6845402020-11-16 15:08:19 -0800618 // Some modules want compile_multilib: "first" to mean 32-bit, not 64-bit.
619 // This is used for Windows support and for HOST_PREFER_32_BIT=true support for Art modules.
Colin Crossa195f912019-10-16 11:07:20 -0700620 prefer32 := false
621 if base.prefer32 != nil {
Jiyong Park1613e552020-09-14 19:43:17 +0900622 prefer32 = base.prefer32(mctx, base, os)
Colin Crossa195f912019-10-16 11:07:20 -0700623 }
dimitry1f33e402019-03-26 12:39:31 +0100624
Colin Crossa6845402020-11-16 15:08:19 -0800625 // Determine the multilib selection for this module.
Colin Crossa195f912019-10-16 11:07:20 -0700626 multilib, extraMultilib := decodeMultilib(base, os.Class)
Colin Crossa6845402020-11-16 15:08:19 -0800627
628 // Convert the multilib selection into a list of Targets.
Colin Crossa195f912019-10-16 11:07:20 -0700629 targets, err := decodeMultilibTargets(multilib, osTargets, prefer32)
630 if err != nil {
631 mctx.ModuleErrorf("%s", err.Error())
632 }
Colin Cross5eca7cb2018-10-02 14:02:10 -0700633
Colin Crossa6845402020-11-16 15:08:19 -0800634 // If the module is using extraMultilib, decode the extraMultilib selection into
635 // a separate list of Targets.
Colin Crossa195f912019-10-16 11:07:20 -0700636 var multiTargets []Target
637 if extraMultilib != "" {
638 multiTargets, err = decodeMultilibTargets(extraMultilib, osTargets, prefer32)
Colin Crossa1ad8d12016-06-01 17:09:44 -0700639 if err != nil {
640 mctx.ModuleErrorf("%s", err.Error())
641 }
Colin Crossb9db4802016-06-03 01:50:47 +0000642 }
643
Colin Crossa6845402020-11-16 15:08:19 -0800644 // Recovery is always the primary architecture, filter out any other architectures.
Colin Crossfb0c16e2019-11-20 17:12:35 -0800645 if image == RecoveryVariation {
646 primaryArch := mctx.Config().DevicePrimaryArchType()
647 targets = filterToArch(targets, primaryArch)
648 multiTargets = filterToArch(multiTargets, primaryArch)
649 }
650
Colin Crossa6845402020-11-16 15:08:19 -0800651 // If there are no supported targets disable the module.
Colin Crossa195f912019-10-16 11:07:20 -0700652 if len(targets) == 0 {
Inseob Kimeec88e12020-01-22 11:11:29 +0900653 base.Disable()
Dan Willemsen3f32f032016-07-11 14:36:48 -0700654 return
655 }
656
Colin Crossa6845402020-11-16 15:08:19 -0800657 // Convert the targets into a list of arch variation names.
Colin Crossa195f912019-10-16 11:07:20 -0700658 targetNames := make([]string, len(targets))
Colin Crossa195f912019-10-16 11:07:20 -0700659 for i, target := range targets {
660 targetNames[i] = target.ArchVariation()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700661 }
662
Colin Crossa6845402020-11-16 15:08:19 -0800663 // Create the variations, annotate each one with which Target it was created for, and
664 // squash the appropriate arch-specific properties into the top level properties.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700665 modules := mctx.CreateVariations(targetNames...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800666 for i, m := range modules {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000667 addTargetProperties(m, targets[i], multiTargets, i == 0)
Colin Cross617b88a2020-08-24 18:04:09 -0700668 m.base().setArchProperties(mctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800669 }
Colin Crossb5ae1932020-11-17 06:32:06 +0000670
671 if multilib == "common_first" && len(modules) >= 2 {
672 for i := range modules[1:] {
673 mctx.AddInterVariantDependency(firstArchToCommonArchDepTag, modules[i+1], modules[0])
674 }
675 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800676}
677
Colin Crossa6845402020-11-16 15:08:19 -0800678// addTargetProperties annotates a variant with the Target is is being compiled for, the list
679// of additional Targets it is supporting (if any), and whether it is the primary Target for
680// the module.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000681func addTargetProperties(m Module, target Target, multiTargets []Target, primaryTarget bool) {
682 m.base().commonProperties.CompileTarget = target
683 m.base().commonProperties.CompileMultiTargets = multiTargets
684 m.base().commonProperties.CompilePrimary = primaryTarget
685}
686
Colin Crossa6845402020-11-16 15:08:19 -0800687// decodeMultilib returns the appropriate compile_multilib property for the module, or the default
688// multilib from the factory's call to InitAndroidArchModule if none was set. For modules that
689// called InitAndroidMultiTargetsArchModule it always returns "common" for multilib, and returns
690// the actual multilib in extraMultilib.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700691func decodeMultilib(base *ModuleBase, class OsClass) (multilib, extraMultilib string) {
Colin Crossa6845402020-11-16 15:08:19 -0800692 // First check the "android.compile_multilib" or "host.compile_multilib" properties.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700693 switch class {
694 case Device:
695 multilib = String(base.commonProperties.Target.Android.Compile_multilib)
Jiyong Park1613e552020-09-14 19:43:17 +0900696 case Host:
Colin Crossee0bc3b2018-10-02 22:01:37 -0700697 multilib = String(base.commonProperties.Target.Host.Compile_multilib)
698 }
Colin Crossa6845402020-11-16 15:08:19 -0800699
700 // If those aren't set, try the "compile_multilib" property.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700701 if multilib == "" {
702 multilib = String(base.commonProperties.Compile_multilib)
703 }
Colin Crossa6845402020-11-16 15:08:19 -0800704
705 // If that wasn't set, use the default multilib set by the factory.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700706 if multilib == "" {
707 multilib = base.commonProperties.Default_multilib
708 }
709
710 if base.commonProperties.UseTargetVariants {
711 return multilib, ""
712 } else {
713 // For app modules a single arch variant will be created per OS class which is expected to handle all the
714 // selected arches. Return the common-type as multilib and any Android.bp provided multilib as extraMultilib
715 if multilib == base.commonProperties.Default_multilib {
716 multilib = "first"
717 }
718 return base.commonProperties.Default_multilib, multilib
719 }
720}
721
Colin Crossa6845402020-11-16 15:08:19 -0800722// filterToArch takes a list of Targets and an ArchType, and returns a modified list that contains
723// only Targets that have the specified ArchType.
Colin Crossfb0c16e2019-11-20 17:12:35 -0800724func filterToArch(targets []Target, arch ArchType) []Target {
725 for i := 0; i < len(targets); i++ {
726 if targets[i].Arch.ArchType != arch {
727 targets = append(targets[:i], targets[i+1:]...)
728 i--
729 }
730 }
731 return targets
732}
733
Colin Crossa6845402020-11-16 15:08:19 -0800734// archPropRoot is a struct type used as the top level of the arch-specific properties. It
735// contains the "arch", "multilib", and "target" property structs. It is used to split up the
736// property structs to limit how much is allocated when a single arch-specific property group is
737// used. The types are interface{} because they will hold instances of runtime-created types.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800738type archPropRoot struct {
739 Arch, Multilib, Target interface{}
740}
741
Colin Crossa6845402020-11-16 15:08:19 -0800742// archPropTypeDesc holds the runtime-created types for the property structs to instantiate to
743// create an archPropRoot property struct.
744type archPropTypeDesc struct {
745 arch, multilib, target reflect.Type
746}
747
Colin Crosscbbd13f2020-01-17 14:08:22 -0800748// createArchPropTypeDesc takes a reflect.Type that is either a struct or a pointer to a struct, and
749// returns lists of reflect.Types that contains the arch-variant properties inside structs for each
750// arch, multilib and target property.
Colin Crossa6845402020-11-16 15:08:19 -0800751//
752// This is a relatively expensive operation, so the results are cached in the global
753// archPropTypeMap. It is constructed entirely based on compile-time data, so there is no need
754// to isolate the results between multiple tests running in parallel.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800755func createArchPropTypeDesc(props reflect.Type) []archPropTypeDesc {
Colin Crossb1d8c992020-01-21 11:43:29 -0800756 // Each property struct shard will be nested many times under the runtime generated arch struct,
757 // which can hit the limit of 64kB for the name of runtime generated structs. They are nested
758 // 97 times now, which may grow in the future, plus there is some overhead for the containing
759 // type. This number may need to be reduced if too many are added, but reducing it too far
760 // could cause problems if a single deeply nested property no longer fits in the name.
761 const maxArchTypeNameSize = 500
762
Colin Crossa6845402020-11-16 15:08:19 -0800763 // Convert the type to a new set of types that contains only the arch-specific properties
764 // (those that are tagged with `android:"arch_specific"`), and sharded into multiple types
765 // to keep the runtime-generated names under the limit.
Colin Crossb1d8c992020-01-21 11:43:29 -0800766 propShards, _ := proptools.FilterPropertyStructSharded(props, maxArchTypeNameSize, filterArchStruct)
Colin Crossa6845402020-11-16 15:08:19 -0800767
768 // If the type has no arch-specific properties there is nothing to do.
Colin Crosscb988072019-01-24 14:58:11 -0800769 if len(propShards) == 0 {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700770 return nil
771 }
772
Colin Crosscbbd13f2020-01-17 14:08:22 -0800773 var ret []archPropTypeDesc
Colin Crossc17727d2018-10-24 12:42:09 -0700774 for _, props := range propShards {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700775
Colin Crossa6845402020-11-16 15:08:19 -0800776 // variantFields takes a list of variant property field names and returns a list the
777 // StructFields with the names and the type of the current shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700778 variantFields := func(names []string) []reflect.StructField {
779 ret := make([]reflect.StructField, len(names))
Dan Willemsenb1957a52016-06-23 23:44:54 -0700780
Colin Crossc17727d2018-10-24 12:42:09 -0700781 for i, name := range names {
782 ret[i].Name = name
783 ret[i].Type = props
Dan Willemsen866b5632017-09-22 12:28:24 -0700784 }
Colin Crossc17727d2018-10-24 12:42:09 -0700785
786 return ret
787 }
788
Colin Crossa6845402020-11-16 15:08:19 -0800789 // Create a type that contains the properties in this shard repeated for each
790 // architecture, architecture variant, and architecture feature.
Colin Crossc17727d2018-10-24 12:42:09 -0700791 archFields := make([]reflect.StructField, len(archTypeList))
792 for i, arch := range archTypeList {
Colin Crossa6845402020-11-16 15:08:19 -0800793 var variants []string
Colin Crossc17727d2018-10-24 12:42:09 -0700794
795 for _, archVariant := range archVariants[arch] {
796 archVariant := variantReplacer.Replace(archVariant)
797 variants = append(variants, proptools.FieldNameForProperty(archVariant))
798 }
799 for _, feature := range archFeatures[arch] {
800 feature := variantReplacer.Replace(feature)
801 variants = append(variants, proptools.FieldNameForProperty(feature))
802 }
803
Colin Crossa6845402020-11-16 15:08:19 -0800804 // Create the StructFields for each architecture variant architecture feature
805 // (e.g. "arch.arm.cortex-a53" or "arch.arm.neon").
Colin Crossc17727d2018-10-24 12:42:09 -0700806 fields := variantFields(variants)
807
Colin Crossa6845402020-11-16 15:08:19 -0800808 // Create the StructField for the architecture itself (e.g. "arch.arm"). The special
809 // "BlueprintEmbed" name is used by Blueprint to put the properties in the
810 // parent struct.
Colin Crossc17727d2018-10-24 12:42:09 -0700811 fields = append([]reflect.StructField{{
812 Name: "BlueprintEmbed",
813 Type: props,
814 Anonymous: true,
815 }}, fields...)
816
817 archFields[i] = reflect.StructField{
818 Name: arch.Field,
819 Type: reflect.StructOf(fields),
820 }
821 }
Colin Crossa6845402020-11-16 15:08:19 -0800822
823 // Create the type of the "arch" property struct for this shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700824 archType := reflect.StructOf(archFields)
825
Colin Crossa6845402020-11-16 15:08:19 -0800826 // Create the type for the "multilib" property struct for this shard, containing the
827 // "multilib.lib32" and "multilib.lib64" property structs.
Colin Crossc17727d2018-10-24 12:42:09 -0700828 multilibType := reflect.StructOf(variantFields([]string{"Lib32", "Lib64"}))
829
Colin Crossa6845402020-11-16 15:08:19 -0800830 // Start with a list of the special targets
Colin Crossc17727d2018-10-24 12:42:09 -0700831 targets := []string{
832 "Host",
833 "Android64",
834 "Android32",
835 "Bionic",
836 "Linux",
837 "Not_windows",
838 "Arm_on_x86",
839 "Arm_on_x86_64",
Victor Khimenkoc26fcf42020-05-07 22:16:33 +0200840 "Native_bridge",
Colin Crossc17727d2018-10-24 12:42:09 -0700841 }
Paul Duffina04c1072020-03-02 10:16:35 +0000842 for _, os := range OsTypeList {
Colin Crossa6845402020-11-16 15:08:19 -0800843 // Add all the OSes.
Colin Crossc17727d2018-10-24 12:42:09 -0700844 targets = append(targets, os.Field)
845
Colin Crossa6845402020-11-16 15:08:19 -0800846 // Add the OS/Arch combinations, e.g. "android_arm64".
Colin Crossc17727d2018-10-24 12:42:09 -0700847 for _, archType := range osArchTypeMap[os] {
848 targets = append(targets, os.Field+"_"+archType.Name)
849
Colin Crossa6845402020-11-16 15:08:19 -0800850 // Also add the special "linux_<arch>" and "bionic_<arch>" property structs.
Colin Crossc17727d2018-10-24 12:42:09 -0700851 if os.Linux() {
852 target := "Linux_" + archType.Name
853 if !InList(target, targets) {
854 targets = append(targets, target)
855 }
856 }
857 if os.Bionic() {
858 target := "Bionic_" + archType.Name
859 if !InList(target, targets) {
860 targets = append(targets, target)
861 }
Dan Willemsen866b5632017-09-22 12:28:24 -0700862 }
863 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700864 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700865
Colin Crossa6845402020-11-16 15:08:19 -0800866 // Create the type for the "target" property struct for this shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700867 targetType := reflect.StructOf(variantFields(targets))
Colin Crosscbbd13f2020-01-17 14:08:22 -0800868
Colin Crossa6845402020-11-16 15:08:19 -0800869 // Return a descriptor of the 3 runtime-created types.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800870 ret = append(ret, archPropTypeDesc{
871 arch: reflect.PtrTo(archType),
872 multilib: reflect.PtrTo(multilibType),
873 target: reflect.PtrTo(targetType),
874 })
Colin Crossc17727d2018-10-24 12:42:09 -0700875 }
876 return ret
Dan Willemsenb1957a52016-06-23 23:44:54 -0700877}
878
Colin Crossa6845402020-11-16 15:08:19 -0800879// variantReplacer converts architecture variant or architecture feature names into names that
880// are valid for an Android.bp file.
881var variantReplacer = strings.NewReplacer("-", "_", ".", "_")
882
883// filterArchStruct returns true if the given field is an architecture specific property.
Colin Cross74449102019-09-25 11:26:40 -0700884func filterArchStruct(field reflect.StructField, prefix string) (bool, reflect.StructField) {
885 if proptools.HasTag(field, "android", "arch_variant") {
886 // The arch_variant field isn't necessary past this point
887 // Instead of wasting space, just remove it. Go also has a
888 // 16-bit limit on structure name length. The name is constructed
889 // based on the Go source representation of the structure, so
890 // the tag names count towards that length.
Colin Crossb4fecbf2020-01-21 11:38:47 -0800891
892 androidTag := field.Tag.Get("android")
893 values := strings.Split(androidTag, ",")
894
895 if string(field.Tag) != `android:"`+strings.Join(values, ",")+`"` {
896 panic(fmt.Errorf("unexpected tag format %q", field.Tag))
Colin Cross74449102019-09-25 11:26:40 -0700897 }
Colin Crossb4fecbf2020-01-21 11:38:47 -0800898 // these tags don't need to be present in the runtime generated struct type.
899 values = RemoveListFromList(values, []string{"arch_variant", "variant_prepend", "path"})
900 if len(values) > 0 {
901 panic(fmt.Errorf("unknown tags %q in field %q", values, prefix+field.Name))
902 }
903
904 field.Tag = ""
Colin Cross74449102019-09-25 11:26:40 -0700905 return true, field
906 }
907 return false, field
908}
909
Colin Crossa6845402020-11-16 15:08:19 -0800910// archPropTypeMap contains a cache of the results of createArchPropTypeDesc for each type. It is
911// shared across all Contexts, but is constructed based only on compile-time information so there
912// is no risk of contaminating one Context with data from another.
Dan Willemsenb1957a52016-06-23 23:44:54 -0700913var archPropTypeMap OncePer
914
Colin Crossa6845402020-11-16 15:08:19 -0800915// initArchModule adds the architecture-specific property structs to a Module.
916func initArchModule(m Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800917
918 base := m.base()
919
Colin Crossa6845402020-11-16 15:08:19 -0800920 // Store the original list of top level property structs
Colin Cross36242852017-06-23 15:06:31 -0700921 base.generalProperties = m.GetProperties()
Colin Cross3f40fa42015-01-30 17:27:36 -0800922
923 for _, properties := range base.generalProperties {
924 propertiesValue := reflect.ValueOf(properties)
Colin Cross62496a02016-08-08 15:49:17 -0700925 t := propertiesValue.Type()
Colin Cross3f40fa42015-01-30 17:27:36 -0800926 if propertiesValue.Kind() != reflect.Ptr {
Colin Crossca860ac2016-01-04 14:34:37 -0800927 panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
928 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800929 }
930
931 propertiesValue = propertiesValue.Elem()
932 if propertiesValue.Kind() != reflect.Struct {
Colin Crossca860ac2016-01-04 14:34:37 -0800933 panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
934 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800935 }
936
Colin Crossa6845402020-11-16 15:08:19 -0800937 // Get or create the arch-specific property struct types for this property struct type.
Colin Cross571cccf2019-02-04 11:22:08 -0800938 archPropTypes := archPropTypeMap.Once(NewCustomOnceKey(t), func() interface{} {
Colin Crosscbbd13f2020-01-17 14:08:22 -0800939 return createArchPropTypeDesc(t)
940 }).([]archPropTypeDesc)
Colin Cross3f40fa42015-01-30 17:27:36 -0800941
Colin Crossa6845402020-11-16 15:08:19 -0800942 // Instantiate one of each arch-specific property struct type and add it to the
943 // properties for the Module.
Colin Crossc17727d2018-10-24 12:42:09 -0700944 var archProperties []interface{}
945 for _, t := range archPropTypes {
Colin Crosscbbd13f2020-01-17 14:08:22 -0800946 archProperties = append(archProperties, &archPropRoot{
947 Arch: reflect.Zero(t.arch).Interface(),
948 Multilib: reflect.Zero(t.multilib).Interface(),
949 Target: reflect.Zero(t.target).Interface(),
950 })
Dan Willemsenb1957a52016-06-23 23:44:54 -0700951 }
Colin Crossc17727d2018-10-24 12:42:09 -0700952 base.archProperties = append(base.archProperties, archProperties)
953 m.AddProperties(archProperties...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800954 }
955
Colin Crossa6845402020-11-16 15:08:19 -0800956 // Update the list of properties that can be set by a defaults module or a call to
957 // AppendMatchingProperties or PrependMatchingProperties.
Colin Cross36242852017-06-23 15:06:31 -0700958 base.customizableProperties = m.GetProperties()
Colin Cross3f40fa42015-01-30 17:27:36 -0800959}
960
Colin Crossa6845402020-11-16 15:08:19 -0800961// appendProperties squashes properties from the given field of the given src property struct
962// into the dst property struct. Returns the reflect.Value of the field in the src property
963// struct to be used for further appendProperties calls on fields of that property struct.
Colin Cross4157e882019-06-06 16:57:04 -0700964func (m *ModuleBase) appendProperties(ctx BottomUpMutatorContext,
Dan Willemsenb1957a52016-06-23 23:44:54 -0700965 dst interface{}, src reflect.Value, field, srcPrefix string) reflect.Value {
Colin Cross06a931b2015-10-28 17:23:31 -0700966
Colin Crossa6845402020-11-16 15:08:19 -0800967 // Step into non-nil pointers to structs in the src value.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800968 if src.Kind() == reflect.Ptr {
969 if src.IsNil() {
970 return src
971 }
972 src = src.Elem()
973 }
974
Colin Crossa6845402020-11-16 15:08:19 -0800975 // Find the requested field in the src struct.
Dan Willemsenb1957a52016-06-23 23:44:54 -0700976 src = src.FieldByName(field)
977 if !src.IsValid() {
Colin Crosseeabb892015-11-20 13:07:51 -0800978 ctx.ModuleErrorf("field %q does not exist", srcPrefix)
Dan Willemsenb1957a52016-06-23 23:44:54 -0700979 return src
Colin Cross85a88972015-11-23 13:29:51 -0800980 }
981
Colin Crossa6845402020-11-16 15:08:19 -0800982 // Save the value of the field in the src struct to return.
Dan Willemsenb1957a52016-06-23 23:44:54 -0700983 ret := src
Colin Cross85a88972015-11-23 13:29:51 -0800984
Colin Crossa6845402020-11-16 15:08:19 -0800985 // If the value of the field is a struct (as opposed to a pointer to a struct) then step
986 // into the BlueprintEmbed field.
Dan Willemsenb1957a52016-06-23 23:44:54 -0700987 if src.Kind() == reflect.Struct {
988 src = src.FieldByName("BlueprintEmbed")
Colin Cross06a931b2015-10-28 17:23:31 -0700989 }
990
Colin Crossa6845402020-11-16 15:08:19 -0800991 // order checks the `android:"variant_prepend"` tag to handle properties where the
992 // arch-specific value needs to come before the generic value, for example for lists of
993 // include directories.
Colin Cross6ee75b62016-05-05 15:57:15 -0700994 order := func(property string,
995 dstField, srcField reflect.StructField,
996 dstValue, srcValue interface{}) (proptools.Order, error) {
997 if proptools.HasTag(dstField, "android", "variant_prepend") {
998 return proptools.Prepend, nil
999 } else {
1000 return proptools.Append, nil
1001 }
1002 }
1003
Colin Crossa6845402020-11-16 15:08:19 -08001004 // Squash the located property struct into the destination property struct.
Dan Willemsenb1957a52016-06-23 23:44:54 -07001005 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src.Interface(), nil, order)
Colin Cross06a931b2015-10-28 17:23:31 -07001006 if err != nil {
1007 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
1008 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
1009 } else {
1010 panic(err)
1011 }
1012 }
Colin Cross85a88972015-11-23 13:29:51 -08001013
Dan Willemsenb1957a52016-06-23 23:44:54 -07001014 return ret
Colin Cross06a931b2015-10-28 17:23:31 -07001015}
1016
Colin Crossa6845402020-11-16 15:08:19 -08001017// Squash the appropriate OS-specific property structs into the matching top level property structs
1018// based on the CompileOS value that was annotated on the variant.
Colin Crossa195f912019-10-16 11:07:20 -07001019func (m *ModuleBase) setOSProperties(ctx BottomUpMutatorContext) {
1020 os := m.commonProperties.CompileOS
1021
1022 for i := range m.generalProperties {
1023 genProps := m.generalProperties[i]
1024 if m.archProperties[i] == nil {
1025 continue
1026 }
1027 for _, archProperties := range m.archProperties[i] {
1028 archPropValues := reflect.ValueOf(archProperties).Elem()
1029
Colin Crosscbbd13f2020-01-17 14:08:22 -08001030 targetProp := archPropValues.FieldByName("Target").Elem()
Colin Crossa195f912019-10-16 11:07:20 -07001031
1032 // Handle host-specific properties in the form:
1033 // target: {
1034 // host: {
1035 // key: value,
1036 // },
1037 // },
Jiyong Park1613e552020-09-14 19:43:17 +09001038 if os.Class == Host {
Colin Crossa195f912019-10-16 11:07:20 -07001039 field := "Host"
1040 prefix := "target.host"
1041 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1042 }
1043
1044 // Handle target OS generalities of the form:
1045 // target: {
1046 // bionic: {
1047 // key: value,
1048 // },
1049 // }
1050 if os.Linux() {
1051 field := "Linux"
1052 prefix := "target.linux"
1053 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1054 }
1055
1056 if os.Bionic() {
1057 field := "Bionic"
1058 prefix := "target.bionic"
1059 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1060 }
1061
1062 // Handle target OS properties in the form:
1063 // target: {
1064 // linux_glibc: {
1065 // key: value,
1066 // },
1067 // not_windows: {
1068 // key: value,
1069 // },
1070 // android {
1071 // key: value,
1072 // },
1073 // },
1074 field := os.Field
1075 prefix := "target." + os.Name
1076 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1077
Jiyong Park1613e552020-09-14 19:43:17 +09001078 if os.Class == Host && os != Windows {
Colin Crossa195f912019-10-16 11:07:20 -07001079 field := "Not_windows"
1080 prefix := "target.not_windows"
1081 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1082 }
1083
1084 // Handle 64-bit device properties in the form:
1085 // target {
1086 // android64 {
1087 // key: value,
1088 // },
1089 // android32 {
1090 // key: value,
1091 // },
1092 // },
1093 // WARNING: this is probably not what you want to use in your blueprints file, it selects
1094 // options for all targets on a device that supports 64-bit binaries, not just the targets
1095 // that are being compiled for 64-bit. Its expected use case is binaries like linker and
1096 // debuggerd that need to know when they are a 32-bit process running on a 64-bit device
1097 if os.Class == Device {
1098 if ctx.Config().Android64() {
1099 field := "Android64"
1100 prefix := "target.android64"
1101 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1102 } else {
1103 field := "Android32"
1104 prefix := "target.android32"
1105 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1106 }
1107 }
1108 }
1109 }
1110}
1111
Colin Crossa6845402020-11-16 15:08:19 -08001112// Squash the appropriate arch-specific property structs into the matching top level property
1113// structs based on the CompileTarget value that was annotated on the variant.
Colin Cross4157e882019-06-06 16:57:04 -07001114func (m *ModuleBase) setArchProperties(ctx BottomUpMutatorContext) {
1115 arch := m.Arch()
1116 os := m.Os()
Colin Crossd3ba0392015-05-07 14:11:29 -07001117
Colin Cross4157e882019-06-06 16:57:04 -07001118 for i := range m.generalProperties {
1119 genProps := m.generalProperties[i]
1120 if m.archProperties[i] == nil {
Dan Willemsenb1957a52016-06-23 23:44:54 -07001121 continue
1122 }
Colin Cross4157e882019-06-06 16:57:04 -07001123 for _, archProperties := range m.archProperties[i] {
Colin Crossc17727d2018-10-24 12:42:09 -07001124 archPropValues := reflect.ValueOf(archProperties).Elem()
Dan Willemsenb1957a52016-06-23 23:44:54 -07001125
Colin Crosscbbd13f2020-01-17 14:08:22 -08001126 archProp := archPropValues.FieldByName("Arch").Elem()
1127 multilibProp := archPropValues.FieldByName("Multilib").Elem()
1128 targetProp := archPropValues.FieldByName("Target").Elem()
Dan Willemsenb1957a52016-06-23 23:44:54 -07001129
Colin Crossc17727d2018-10-24 12:42:09 -07001130 // Handle arch-specific properties in the form:
Colin Crossd5934c82017-10-02 13:55:26 -07001131 // arch: {
Colin Crossc17727d2018-10-24 12:42:09 -07001132 // arm64: {
Colin Crossd5934c82017-10-02 13:55:26 -07001133 // key: value,
1134 // },
1135 // },
Colin Crossc17727d2018-10-24 12:42:09 -07001136 t := arch.ArchType
1137
1138 if arch.ArchType != Common {
1139 field := proptools.FieldNameForProperty(t.Name)
1140 prefix := "arch." + t.Name
Colin Cross4157e882019-06-06 16:57:04 -07001141 archStruct := m.appendProperties(ctx, genProps, archProp, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001142
1143 // Handle arch-variant-specific properties in the form:
1144 // arch: {
1145 // variant: {
1146 // key: value,
1147 // },
1148 // },
1149 v := variantReplacer.Replace(arch.ArchVariant)
1150 if v != "" {
1151 field := proptools.FieldNameForProperty(v)
1152 prefix := "arch." + t.Name + "." + v
Colin Cross4157e882019-06-06 16:57:04 -07001153 m.appendProperties(ctx, genProps, archStruct, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001154 }
1155
1156 // Handle cpu-variant-specific properties in the form:
1157 // arch: {
1158 // variant: {
1159 // key: value,
1160 // },
1161 // },
1162 if arch.CpuVariant != arch.ArchVariant {
1163 c := variantReplacer.Replace(arch.CpuVariant)
1164 if c != "" {
1165 field := proptools.FieldNameForProperty(c)
1166 prefix := "arch." + t.Name + "." + c
Colin Cross4157e882019-06-06 16:57:04 -07001167 m.appendProperties(ctx, genProps, archStruct, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001168 }
1169 }
1170
1171 // Handle arch-feature-specific properties in the form:
1172 // arch: {
1173 // feature: {
1174 // key: value,
1175 // },
1176 // },
1177 for _, feature := range arch.ArchFeatures {
1178 field := proptools.FieldNameForProperty(feature)
1179 prefix := "arch." + t.Name + "." + feature
Colin Cross4157e882019-06-06 16:57:04 -07001180 m.appendProperties(ctx, genProps, archStruct, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001181 }
1182
1183 // Handle multilib-specific properties in the form:
1184 // multilib: {
1185 // lib32: {
1186 // key: value,
1187 // },
1188 // },
1189 field = proptools.FieldNameForProperty(t.Multilib)
1190 prefix = "multilib." + t.Multilib
Colin Cross4157e882019-06-06 16:57:04 -07001191 m.appendProperties(ctx, genProps, multilibProp, field, prefix)
Colin Cross08016332016-12-20 09:53:14 -08001192 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001193
Colin Crossa195f912019-10-16 11:07:20 -07001194 // Handle combined OS-feature and arch specific properties in the form:
Colin Crossc17727d2018-10-24 12:42:09 -07001195 // target: {
Colin Crossc17727d2018-10-24 12:42:09 -07001196 // bionic_x86: {
1197 // key: value,
1198 // },
1199 // }
Colin Crossa195f912019-10-16 11:07:20 -07001200 if os.Linux() && arch.ArchType != Common {
1201 field := "Linux_" + arch.ArchType.Name
1202 prefix := "target.linux_" + arch.ArchType.Name
Colin Cross4157e882019-06-06 16:57:04 -07001203 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossd5934c82017-10-02 13:55:26 -07001204 }
Colin Crossc5c24ad2015-11-20 15:35:00 -08001205
Colin Crossa195f912019-10-16 11:07:20 -07001206 if os.Bionic() && arch.ArchType != Common {
1207 field := "Bionic_" + t.Name
1208 prefix := "target.bionic_" + t.Name
Colin Cross4157e882019-06-06 16:57:04 -07001209 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001210 }
1211
Colin Crossa195f912019-10-16 11:07:20 -07001212 // Handle combined OS and arch specific properties in the form:
Colin Crossc17727d2018-10-24 12:42:09 -07001213 // target: {
Colin Crossc17727d2018-10-24 12:42:09 -07001214 // linux_glibc_x86: {
1215 // key: value,
1216 // },
1217 // linux_glibc_arm: {
1218 // key: value,
1219 // },
Colin Crossc17727d2018-10-24 12:42:09 -07001220 // android_arm {
1221 // key: value,
1222 // },
1223 // android_x86 {
Colin Crossd5934c82017-10-02 13:55:26 -07001224 // key: value,
1225 // },
1226 // },
Colin Crossc17727d2018-10-24 12:42:09 -07001227 if arch.ArchType != Common {
Colin Crossa195f912019-10-16 11:07:20 -07001228 field := os.Field + "_" + t.Name
1229 prefix := "target." + os.Name + "_" + t.Name
Colin Cross4157e882019-06-06 16:57:04 -07001230 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossd5934c82017-10-02 13:55:26 -07001231 }
1232
Colin Crossa195f912019-10-16 11:07:20 -07001233 // Handle arm on x86 properties in the form:
Colin Crossc17727d2018-10-24 12:42:09 -07001234 // target {
Colin Crossa195f912019-10-16 11:07:20 -07001235 // arm_on_x86 {
Colin Crossc17727d2018-10-24 12:42:09 -07001236 // key: value,
1237 // },
Colin Crossa195f912019-10-16 11:07:20 -07001238 // arm_on_x86_64 {
Colin Crossd5934c82017-10-02 13:55:26 -07001239 // key: value,
1240 // },
1241 // },
Colin Crossc17727d2018-10-24 12:42:09 -07001242 if os.Class == Device {
Victor Khimenko1a31f802020-09-17 03:07:31 +02001243 if arch.ArchType == X86 && (hasArmAbi(arch) ||
1244 hasArmAndroidArch(ctx.Config().Targets[Android])) {
Colin Crossc17727d2018-10-24 12:42:09 -07001245 field := "Arm_on_x86"
1246 prefix := "target.arm_on_x86"
Colin Cross4157e882019-06-06 16:57:04 -07001247 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001248 }
Victor Khimenko1a31f802020-09-17 03:07:31 +02001249 if arch.ArchType == X86_64 && (hasArmAbi(arch) ||
1250 hasArmAndroidArch(ctx.Config().Targets[Android])) {
Colin Crossc17727d2018-10-24 12:42:09 -07001251 field := "Arm_on_x86_64"
1252 prefix := "target.arm_on_x86_64"
Colin Cross4157e882019-06-06 16:57:04 -07001253 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001254 }
Victor Khimenkoc26fcf42020-05-07 22:16:33 +02001255 if os == Android && m.Target().NativeBridge == NativeBridgeEnabled {
1256 field := "Native_bridge"
1257 prefix := "target.native_bridge"
1258 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1259 }
Colin Cross4247f0d2017-04-13 16:56:14 -07001260 }
Colin Crossbb2e2b72016-12-08 17:23:53 -08001261 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001262 }
1263}
1264
Colin Crossa6845402020-11-16 15:08:19 -08001265// Convert the arch product variables into a list of targets for each OsType.
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001266func decodeTargetProductVariables(config *config) (map[OsType][]Target, error) {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001267 variables := config.productVariables
Dan Willemsen490fd492015-11-24 17:53:15 -08001268
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001269 targets := make(map[OsType][]Target)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001270 var targetErr error
1271
dimitry1f33e402019-03-26 12:39:31 +01001272 addTarget := func(os OsType, archName string, archVariant, cpuVariant *string, abi []string,
dimitry8d6dde82019-07-11 10:23:53 +02001273 nativeBridgeEnabled NativeBridgeSupport, nativeBridgeHostArchName *string,
1274 nativeBridgeRelativePath *string) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001275 if targetErr != nil {
1276 return
Dan Willemsen490fd492015-11-24 17:53:15 -08001277 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001278
Dan Willemsen01a3c252019-01-11 19:02:16 -08001279 arch, err := decodeArch(os, archName, archVariant, cpuVariant, abi)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001280 if err != nil {
1281 targetErr = err
1282 return
1283 }
dimitry8d6dde82019-07-11 10:23:53 +02001284 nativeBridgeRelativePathStr := String(nativeBridgeRelativePath)
1285 nativeBridgeHostArchNameStr := String(nativeBridgeHostArchName)
1286
1287 // Use guest arch as relative install path by default
1288 if nativeBridgeEnabled && nativeBridgeRelativePathStr == "" {
1289 nativeBridgeRelativePathStr = arch.ArchType.String()
1290 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001291
Jiyong Park1613e552020-09-14 19:43:17 +09001292 // A target is considered as HostCross if it's a host target which can't run natively on
1293 // the currently configured build machine (either because the OS is different or because of
1294 // the unsupported arch)
1295 hostCross := false
1296 if os.Class == Host {
1297 var osSupported bool
1298 if os == BuildOs {
1299 osSupported = true
1300 } else if BuildOs.Linux() && os.Linux() {
1301 // LinuxBionic and Linux are compatible
1302 osSupported = true
1303 } else {
1304 osSupported = false
1305 }
1306
1307 var archSupported bool
1308 if arch.ArchType == Common {
1309 archSupported = true
1310 } else if arch.ArchType.Name == *variables.HostArch {
1311 archSupported = true
1312 } else if variables.HostSecondaryArch != nil && arch.ArchType.Name == *variables.HostSecondaryArch {
1313 archSupported = true
1314 } else {
1315 archSupported = false
1316 }
1317 if !osSupported || !archSupported {
1318 hostCross = true
1319 }
1320 }
1321
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001322 targets[os] = append(targets[os],
Colin Crossa1ad8d12016-06-01 17:09:44 -07001323 Target{
dimitry8d6dde82019-07-11 10:23:53 +02001324 Os: os,
1325 Arch: arch,
1326 NativeBridge: nativeBridgeEnabled,
1327 NativeBridgeHostArchName: nativeBridgeHostArchNameStr,
1328 NativeBridgeRelativePath: nativeBridgeRelativePathStr,
Jiyong Park1613e552020-09-14 19:43:17 +09001329 HostCross: hostCross,
Colin Crossa1ad8d12016-06-01 17:09:44 -07001330 })
Dan Willemsen490fd492015-11-24 17:53:15 -08001331 }
1332
Colin Cross4225f652015-09-17 14:33:42 -07001333 if variables.HostArch == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001334 return nil, fmt.Errorf("No host primary architecture set")
Colin Cross4225f652015-09-17 14:33:42 -07001335 }
1336
Colin Crossa6845402020-11-16 15:08:19 -08001337 // The primary host target, which must always exist.
dimitry8d6dde82019-07-11 10:23:53 +02001338 addTarget(BuildOs, *variables.HostArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -07001339
Colin Crossa6845402020-11-16 15:08:19 -08001340 // An optional secondary host target.
Colin Crosseeabb892015-11-20 13:07:51 -08001341 if variables.HostSecondaryArch != nil && *variables.HostSecondaryArch != "" {
dimitry8d6dde82019-07-11 10:23:53 +02001342 addTarget(BuildOs, *variables.HostSecondaryArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -08001343 }
1344
Colin Crossa6845402020-11-16 15:08:19 -08001345 // An optional host target that uses the Bionic glibc runtime.
Colin Crossff3ae9d2018-04-10 16:15:18 -07001346 if Bool(config.Host_bionic) {
dimitry8d6dde82019-07-11 10:23:53 +02001347 addTarget(LinuxBionic, "x86_64", nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen01a405a2016-06-13 17:19:03 -07001348 }
Colin Crossa6845402020-11-16 15:08:19 -08001349
1350 // An optional cross-compiled host target that uses the Bionic glibc runtime on an arm64
1351 // architecture.
Jiyong Park22101982020-09-17 19:09:58 +09001352 if Bool(config.Host_bionic_arm64) {
1353 addTarget(LinuxBionic, "arm64", nil, nil, nil, NativeBridgeDisabled, nil, nil)
1354 }
Dan Willemsen01a405a2016-06-13 17:19:03 -07001355
Colin Crossa6845402020-11-16 15:08:19 -08001356 // Optional cross-compiled host targets, generally Windows.
Colin Crossff3ae9d2018-04-10 16:15:18 -07001357 if String(variables.CrossHost) != "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001358 crossHostOs := osByName(*variables.CrossHost)
1359 if crossHostOs == NoOsType {
1360 return nil, fmt.Errorf("Unknown cross host OS %q", *variables.CrossHost)
1361 }
1362
Colin Crossff3ae9d2018-04-10 16:15:18 -07001363 if String(variables.CrossHostArch) == "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001364 return nil, fmt.Errorf("No cross-host primary architecture set")
Dan Willemsen490fd492015-11-24 17:53:15 -08001365 }
1366
Colin Crossa6845402020-11-16 15:08:19 -08001367 // The primary cross-compiled host target.
dimitry8d6dde82019-07-11 10:23:53 +02001368 addTarget(crossHostOs, *variables.CrossHostArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -08001369
Colin Crossa6845402020-11-16 15:08:19 -08001370 // An optional secondary cross-compiled host target.
Dan Willemsen490fd492015-11-24 17:53:15 -08001371 if variables.CrossHostSecondaryArch != nil && *variables.CrossHostSecondaryArch != "" {
dimitry8d6dde82019-07-11 10:23:53 +02001372 addTarget(crossHostOs, *variables.CrossHostSecondaryArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -08001373 }
1374 }
1375
Colin Crossa6845402020-11-16 15:08:19 -08001376 // Optional device targets
Dan Willemsen3f32f032016-07-11 14:36:48 -07001377 if variables.DeviceArch != nil && *variables.DeviceArch != "" {
Doug Horn21b94272019-01-16 12:06:11 -08001378 var target = Android
1379 if Bool(variables.Fuchsia) {
1380 target = Fuchsia
1381 }
1382
Colin Crossa6845402020-11-16 15:08:19 -08001383 // The primary device target.
Doug Horn21b94272019-01-16 12:06:11 -08001384 addTarget(target, *variables.DeviceArch, variables.DeviceArchVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001385 variables.DeviceCpuVariant, variables.DeviceAbi, NativeBridgeDisabled, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -07001386
Colin Crossa6845402020-11-16 15:08:19 -08001387 // An optional secondary device target.
Dan Willemsen3f32f032016-07-11 14:36:48 -07001388 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" {
1389 addTarget(Android, *variables.DeviceSecondaryArch,
1390 variables.DeviceSecondaryArchVariant, variables.DeviceSecondaryCpuVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001391 variables.DeviceSecondaryAbi, NativeBridgeDisabled, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -07001392 }
dimitry1f33e402019-03-26 12:39:31 +01001393
Colin Crossa6845402020-11-16 15:08:19 -08001394 // An optional NativeBridge device target.
dimitry1f33e402019-03-26 12:39:31 +01001395 if variables.NativeBridgeArch != nil && *variables.NativeBridgeArch != "" {
1396 addTarget(Android, *variables.NativeBridgeArch,
1397 variables.NativeBridgeArchVariant, variables.NativeBridgeCpuVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001398 variables.NativeBridgeAbi, NativeBridgeEnabled, variables.DeviceArch,
1399 variables.NativeBridgeRelativePath)
dimitry1f33e402019-03-26 12:39:31 +01001400 }
1401
Colin Crossa6845402020-11-16 15:08:19 -08001402 // An optional secondary NativeBridge device target.
dimitry1f33e402019-03-26 12:39:31 +01001403 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" &&
1404 variables.NativeBridgeSecondaryArch != nil && *variables.NativeBridgeSecondaryArch != "" {
1405 addTarget(Android, *variables.NativeBridgeSecondaryArch,
1406 variables.NativeBridgeSecondaryArchVariant,
1407 variables.NativeBridgeSecondaryCpuVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001408 variables.NativeBridgeSecondaryAbi,
1409 NativeBridgeEnabled,
1410 variables.DeviceSecondaryArch,
1411 variables.NativeBridgeSecondaryRelativePath)
dimitry1f33e402019-03-26 12:39:31 +01001412 }
Colin Cross4225f652015-09-17 14:33:42 -07001413 }
1414
Colin Crossa1ad8d12016-06-01 17:09:44 -07001415 if targetErr != nil {
1416 return nil, targetErr
1417 }
1418
1419 return targets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001420}
1421
Colin Crossbb2e2b72016-12-08 17:23:53 -08001422// hasArmAbi returns true if arch has at least one arm ABI
1423func hasArmAbi(arch Arch) bool {
Jaewoong Jung3aff5782020-02-11 07:54:35 -08001424 return PrefixInList(arch.Abi, "arm")
Colin Crossbb2e2b72016-12-08 17:23:53 -08001425}
1426
dimitry628db6f2019-05-22 17:16:21 +02001427// hasArmArch returns true if targets has at least non-native_bridge arm Android arch
Colin Cross4247f0d2017-04-13 16:56:14 -07001428func hasArmAndroidArch(targets []Target) bool {
1429 for _, target := range targets {
Victor Khimenko1a31f802020-09-17 03:07:31 +02001430 if target.Os == Android && target.Arch.ArchType == Arm {
Victor Khimenko5eb8ec12018-03-21 20:30:54 +01001431 return true
1432 }
1433 }
1434 return false
1435}
1436
Colin Crossa6845402020-11-16 15:08:19 -08001437// archConfig describes a built-in configuration.
Dan Albert4098deb2016-10-19 14:04:41 -07001438type archConfig struct {
1439 arch string
1440 archVariant string
1441 cpuVariant string
1442 abi []string
1443}
1444
Colin Crossa6845402020-11-16 15:08:19 -08001445// getMegaDeviceConfig returns a list of archConfigs for every architecture simultaneously.
Dan Albert4098deb2016-10-19 14:04:41 -07001446func getMegaDeviceConfig() []archConfig {
1447 return []archConfig{
Dan Albert8818f492019-02-19 13:53:01 -08001448 {"arm", "armv7-a", "generic", []string{"armeabi-v7a"}},
Dan Willemsen110a89d2016-01-14 15:17:19 -08001449 {"arm", "armv7-a-neon", "generic", []string{"armeabi-v7a"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001450 {"arm", "armv7-a-neon", "cortex-a7", []string{"armeabi-v7a"}},
1451 {"arm", "armv7-a-neon", "cortex-a8", []string{"armeabi-v7a"}},
Dan Willemsen110a89d2016-01-14 15:17:19 -08001452 {"arm", "armv7-a-neon", "cortex-a9", []string{"armeabi-v7a"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001453 {"arm", "armv7-a-neon", "cortex-a15", []string{"armeabi-v7a"}},
1454 {"arm", "armv7-a-neon", "cortex-a53", []string{"armeabi-v7a"}},
1455 {"arm", "armv7-a-neon", "cortex-a53.a57", []string{"armeabi-v7a"}},
Richard Fungeb37ed32018-09-24 16:33:45 -07001456 {"arm", "armv7-a-neon", "cortex-a72", []string{"armeabi-v7a"}},
Jake Weinstein6600a442017-05-15 18:27:12 -04001457 {"arm", "armv7-a-neon", "cortex-a73", []string{"armeabi-v7a"}},
Christopher Ferrisba14a8f2018-04-23 18:15:25 -07001458 {"arm", "armv7-a-neon", "cortex-a75", []string{"armeabi-v7a"}},
Haibo Huanga31e2bd2018-10-09 14:27:28 -07001459 {"arm", "armv7-a-neon", "cortex-a76", []string{"armeabi-v7a"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001460 {"arm", "armv7-a-neon", "krait", []string{"armeabi-v7a"}},
Alex Naidisae4fc182016-08-20 00:14:56 +02001461 {"arm", "armv7-a-neon", "kryo", []string{"armeabi-v7a"}},
Artem Serovd3072b02018-11-15 15:21:51 +00001462 {"arm", "armv7-a-neon", "kryo385", []string{"armeabi-v7a"}},
Junmo Park8ea49592017-07-24 07:14:55 +09001463 {"arm", "armv7-a-neon", "exynos-m1", []string{"armeabi-v7a"}},
Junmo Parkd86c9022017-07-21 09:07:47 +09001464 {"arm", "armv7-a-neon", "exynos-m2", []string{"armeabi-v7a"}},
Dan Willemsen110a89d2016-01-14 15:17:19 -08001465 {"arm64", "armv8-a", "cortex-a53", []string{"arm64-v8a"}},
Richard Fungeb37ed32018-09-24 16:33:45 -07001466 {"arm64", "armv8-a", "cortex-a72", []string{"arm64-v8a"}},
Jake Weinstein6600a442017-05-15 18:27:12 -04001467 {"arm64", "armv8-a", "cortex-a73", []string{"arm64-v8a"}},
Alex Naidisac01ff52016-08-30 15:56:33 +02001468 {"arm64", "armv8-a", "kryo", []string{"arm64-v8a"}},
Junmo Park8ea49592017-07-24 07:14:55 +09001469 {"arm64", "armv8-a", "exynos-m1", []string{"arm64-v8a"}},
Junmo Parkd86c9022017-07-21 09:07:47 +09001470 {"arm64", "armv8-a", "exynos-m2", []string{"arm64-v8a"}},
Artem Serovd3072b02018-11-15 15:21:51 +00001471 {"arm64", "armv8-2a", "kryo385", []string{"arm64-v8a"}},
Raphael Gault70b96b02020-06-18 09:56:53 +00001472 {"arm64", "armv8-2a-dotprod", "cortex-a55", []string{"arm64-v8a"}},
1473 {"arm64", "armv8-2a-dotprod", "cortex-a75", []string{"arm64-v8a"}},
1474 {"arm64", "armv8-2a-dotprod", "cortex-a76", []string{"arm64-v8a"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001475 {"x86", "", "", []string{"x86"}},
1476 {"x86", "atom", "", []string{"x86"}},
1477 {"x86", "haswell", "", []string{"x86"}},
1478 {"x86", "ivybridge", "", []string{"x86"}},
1479 {"x86", "sandybridge", "", []string{"x86"}},
1480 {"x86", "silvermont", "", []string{"x86"}},
Benjamin Gordon87e7f2f2019-02-14 10:59:48 -07001481 {"x86", "stoneyridge", "", []string{"x86"}},
Dan Willemsen8a354052016-05-10 14:30:51 -07001482 {"x86", "x86_64", "", []string{"x86"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001483 {"x86_64", "", "", []string{"x86_64"}},
1484 {"x86_64", "haswell", "", []string{"x86_64"}},
1485 {"x86_64", "ivybridge", "", []string{"x86_64"}},
1486 {"x86_64", "sandybridge", "", []string{"x86_64"}},
1487 {"x86_64", "silvermont", "", []string{"x86_64"}},
Benjamin Gordon87e7f2f2019-02-14 10:59:48 -07001488 {"x86_64", "stoneyridge", "", []string{"x86_64"}},
Dan Willemsen322acaf2016-01-12 23:07:05 -08001489 }
Dan Albert4098deb2016-10-19 14:04:41 -07001490}
Dan Willemsen322acaf2016-01-12 23:07:05 -08001491
Colin Crossa6845402020-11-16 15:08:19 -08001492// getNdkAbisConfig returns a list of archConfigs for the ABIs supported by the NDK.
Dan Albert4098deb2016-10-19 14:04:41 -07001493func getNdkAbisConfig() []archConfig {
1494 return []archConfig{
Dan Albert6bba6442020-01-30 15:16:49 -08001495 {"arm", "armv7-a", "", []string{"armeabi-v7a"}},
Dan Albert4098deb2016-10-19 14:04:41 -07001496 {"arm64", "armv8-a", "", []string{"arm64-v8a"}},
Dan Albert4098deb2016-10-19 14:04:41 -07001497 {"x86", "", "", []string{"x86"}},
1498 {"x86_64", "", "", []string{"x86_64"}},
1499 }
1500}
1501
Colin Crossa6845402020-11-16 15:08:19 -08001502// getAmlAbisConfig returns a list of archConfigs for the ABIs supported by mainline modules.
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001503func getAmlAbisConfig() []archConfig {
1504 return []archConfig{
Martin Stjernholm93688342020-10-16 21:45:10 +01001505 {"arm", "armv7-a-neon", "", []string{"armeabi-v7a"}},
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001506 {"arm64", "armv8-a", "", []string{"arm64-v8a"}},
1507 {"x86", "", "", []string{"x86"}},
1508 {"x86_64", "", "", []string{"x86_64"}},
1509 }
1510}
1511
Colin Crossa6845402020-11-16 15:08:19 -08001512// decodeArchSettings converts a list of archConfigs into a list of Targets for the given OsType.
Dan Willemsen01a3c252019-01-11 19:02:16 -08001513func decodeArchSettings(os OsType, archConfigs []archConfig) ([]Target, error) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001514 var ret []Target
Dan Willemsen322acaf2016-01-12 23:07:05 -08001515
Dan Albert4098deb2016-10-19 14:04:41 -07001516 for _, config := range archConfigs {
Dan Willemsen01a3c252019-01-11 19:02:16 -08001517 arch, err := decodeArch(os, config.arch, &config.archVariant,
Colin Crossa74ca042019-01-31 14:31:51 -08001518 &config.cpuVariant, config.abi)
Dan Willemsen322acaf2016-01-12 23:07:05 -08001519 if err != nil {
1520 return nil, err
1521 }
Colin Cross3b19f5d2019-09-17 14:45:31 -07001522
Colin Crossa1ad8d12016-06-01 17:09:44 -07001523 ret = append(ret, Target{
1524 Os: Android,
1525 Arch: arch,
1526 })
Dan Willemsen322acaf2016-01-12 23:07:05 -08001527 }
1528
1529 return ret, nil
1530}
1531
Colin Crossa6845402020-11-16 15:08:19 -08001532// decodeArch converts a set of strings from product variables into an Arch struct.
Colin Crossa74ca042019-01-31 14:31:51 -08001533func decodeArch(os OsType, arch string, archVariant, cpuVariant *string, abi []string) (Arch, error) {
Colin Crossa6845402020-11-16 15:08:19 -08001534 // Verify the arch is valid
Colin Crosseeabb892015-11-20 13:07:51 -08001535 archType, ok := archTypeMap[arch]
1536 if !ok {
1537 return Arch{}, fmt.Errorf("unknown arch %q", arch)
1538 }
Colin Cross4225f652015-09-17 14:33:42 -07001539
Colin Crosseeabb892015-11-20 13:07:51 -08001540 a := Arch{
Colin Cross4225f652015-09-17 14:33:42 -07001541 ArchType: archType,
Colin Crossa6845402020-11-16 15:08:19 -08001542 ArchVariant: String(archVariant),
1543 CpuVariant: String(cpuVariant),
Colin Crossa74ca042019-01-31 14:31:51 -08001544 Abi: abi,
Colin Crosseeabb892015-11-20 13:07:51 -08001545 }
1546
Colin Crossa6845402020-11-16 15:08:19 -08001547 // Convert generic arch variants into the empty string.
Colin Crosseeabb892015-11-20 13:07:51 -08001548 if a.ArchVariant == a.ArchType.Name || a.ArchVariant == "generic" {
1549 a.ArchVariant = ""
1550 }
1551
Colin Crossa6845402020-11-16 15:08:19 -08001552 // Convert generic CPU variants into the empty string.
Colin Crosseeabb892015-11-20 13:07:51 -08001553 if a.CpuVariant == a.ArchType.Name || a.CpuVariant == "generic" {
1554 a.CpuVariant = ""
1555 }
1556
Colin Crossa6845402020-11-16 15:08:19 -08001557 // Filter empty ABIs out of the list.
Colin Crosseeabb892015-11-20 13:07:51 -08001558 for i := 0; i < len(a.Abi); i++ {
1559 if a.Abi[i] == "" {
1560 a.Abi = append(a.Abi[:i], a.Abi[i+1:]...)
1561 i--
1562 }
1563 }
1564
Dan Willemsen01a3c252019-01-11 19:02:16 -08001565 if a.ArchVariant == "" {
Colin Crossa6845402020-11-16 15:08:19 -08001566 // Set ArchFeatures from the default arch features.
Dan Willemsen01a3c252019-01-11 19:02:16 -08001567 if featureMap, ok := defaultArchFeatureMap[os]; ok {
1568 a.ArchFeatures = featureMap[archType]
1569 }
1570 } else {
Colin Crossa6845402020-11-16 15:08:19 -08001571 // Set ArchFeatures from the arch type.
Dan Willemsen01a3c252019-01-11 19:02:16 -08001572 if featureMap, ok := archFeatureMap[archType]; ok {
1573 a.ArchFeatures = featureMap[a.ArchVariant]
1574 }
Colin Crossc5c24ad2015-11-20 15:35:00 -08001575 }
1576
Colin Crosseeabb892015-11-20 13:07:51 -08001577 return a, nil
Colin Cross4225f652015-09-17 14:33:42 -07001578}
1579
Colin Crossa6845402020-11-16 15:08:19 -08001580// filterMultilibTargets takes a list of Targets and a multilib value and returns a new list of
1581// Targets containing only those that have the given multilib value.
Colin Cross69617d32016-09-06 10:39:07 -07001582func filterMultilibTargets(targets []Target, multilib string) []Target {
1583 var ret []Target
1584 for _, t := range targets {
1585 if t.Arch.ArchType.Multilib == multilib {
1586 ret = append(ret, t)
1587 }
1588 }
1589 return ret
1590}
1591
Colin Crossa6845402020-11-16 15:08:19 -08001592// getCommonTargets returns the set of Os specific common architecture targets for each Os in a list
1593// of targets.
Nan Zhangdb0b9a32017-02-27 10:12:13 -08001594func getCommonTargets(targets []Target) []Target {
1595 var ret []Target
1596 set := make(map[string]bool)
1597
1598 for _, t := range targets {
1599 if _, found := set[t.Os.String()]; !found {
1600 set[t.Os.String()] = true
1601 ret = append(ret, commonTargetMap[t.Os.String()])
1602 }
1603 }
1604
1605 return ret
1606}
1607
Colin Crossa6845402020-11-16 15:08:19 -08001608// firstTarget takes a list of Targets and a list of multilib values and returns a list of Targets
1609// that contains zero or one Target for each OsType, selecting the one that matches the earliest
1610// filter.
Colin Cross3dceee32018-09-06 10:19:57 -07001611func firstTarget(targets []Target, filters ...string) []Target {
Jiyong Park22101982020-09-17 19:09:58 +09001612 // find the first target from each OS
1613 var ret []Target
1614 hasHost := false
1615 set := make(map[OsType]bool)
1616
Colin Cross6b4a32d2017-12-05 13:42:45 -08001617 for _, filter := range filters {
1618 buildTargets := filterMultilibTargets(targets, filter)
Jiyong Park22101982020-09-17 19:09:58 +09001619 for _, t := range buildTargets {
1620 if _, found := set[t.Os]; !found {
1621 hasHost = hasHost || (t.Os.Class == Host)
1622 set[t.Os] = true
1623 ret = append(ret, t)
1624 }
Colin Cross6b4a32d2017-12-05 13:42:45 -08001625 }
1626 }
Jiyong Park22101982020-09-17 19:09:58 +09001627 return ret
Colin Cross6b4a32d2017-12-05 13:42:45 -08001628}
1629
Colin Crossa6845402020-11-16 15:08:19 -08001630// decodeMultilibTargets uses the module's multilib setting to select one or more targets from a
1631// list of Targets.
Colin Crossee0bc3b2018-10-02 22:01:37 -07001632func decodeMultilibTargets(multilib string, targets []Target, prefer32 bool) ([]Target, error) {
Colin Crossa6845402020-11-16 15:08:19 -08001633 var buildTargets []Target
Colin Cross6b4a32d2017-12-05 13:42:45 -08001634
Colin Cross4225f652015-09-17 14:33:42 -07001635 switch multilib {
1636 case "common":
Colin Cross6b4a32d2017-12-05 13:42:45 -08001637 buildTargets = getCommonTargets(targets)
1638 case "common_first":
1639 buildTargets = getCommonTargets(targets)
1640 if prefer32 {
Colin Cross3dceee32018-09-06 10:19:57 -07001641 buildTargets = append(buildTargets, firstTarget(targets, "lib32", "lib64")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001642 } else {
Colin Cross3dceee32018-09-06 10:19:57 -07001643 buildTargets = append(buildTargets, firstTarget(targets, "lib64", "lib32")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001644 }
Colin Cross4225f652015-09-17 14:33:42 -07001645 case "both":
Colin Cross8b74d172016-09-13 09:59:14 -07001646 if prefer32 {
1647 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1648 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1649 } else {
1650 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1651 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1652 }
Colin Cross4225f652015-09-17 14:33:42 -07001653 case "32":
Colin Cross69617d32016-09-06 10:39:07 -07001654 buildTargets = filterMultilibTargets(targets, "lib32")
Colin Cross4225f652015-09-17 14:33:42 -07001655 case "64":
Colin Cross69617d32016-09-06 10:39:07 -07001656 buildTargets = filterMultilibTargets(targets, "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001657 case "first":
1658 if prefer32 {
Colin Cross3dceee32018-09-06 10:19:57 -07001659 buildTargets = firstTarget(targets, "lib32", "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001660 } else {
Colin Cross3dceee32018-09-06 10:19:57 -07001661 buildTargets = firstTarget(targets, "lib64", "lib32")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001662 }
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:
Colin Cross69617d32016-09-06 10:39:07 -07001669 return nil, fmt.Errorf(`compile_multilib must be "both", "first", "32", "64", or "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}