blob: 6fb70c9f1f293c9bff5fe486547df39dc21e9175 [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 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 {
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)
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
Colin Crossa6845402020-11-16 15:08:19 -0800350// Target specifies the OS and architecture that a module is being compiled for.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700351type Target struct {
Colin Crossa6845402020-11-16 15:08:19 -0800352 // Os the OS that the module is being compiled for (e.g. "linux_glibc", "android").
353 Os OsType
354 // Arch is the architecture that the module is being compiled for.
355 Arch Arch
356 // NativeBridge is NativeBridgeEnabled if the architecture is supported using NativeBridge
357 // (i.e. arm on x86) for this device.
358 NativeBridge NativeBridgeSupport
359 // NativeBridgeHostArchName is the name of the real architecture that is used to implement
360 // the NativeBridge architecture. For example, for arm on x86 this would be "x86".
dimitry8d6dde82019-07-11 10:23:53 +0200361 NativeBridgeHostArchName string
Colin Crossa6845402020-11-16 15:08:19 -0800362 // NativeBridgeRelativePath is the name of the subdirectory that will contain NativeBridge
363 // libraries and binaries.
dimitry8d6dde82019-07-11 10:23:53 +0200364 NativeBridgeRelativePath string
Jiyong Park1613e552020-09-14 19:43:17 +0900365
366 // HostCross is true when the target cannot run natively on the current build host.
367 // For example, linux_glibc_x86 returns true on a regular x86/i686/Linux machines, but returns false
368 // on Mac (different OS), or on 64-bit only i686/Linux machines (unsupported arch).
369 HostCross bool
Colin Crossd3ba0392015-05-07 14:11:29 -0700370}
371
Colin Crossa6845402020-11-16 15:08:19 -0800372// NativeBridgeSupport is an enum that specifies if a Target supports NativeBridge.
373type NativeBridgeSupport bool
374
375const (
376 NativeBridgeDisabled NativeBridgeSupport = false
377 NativeBridgeEnabled NativeBridgeSupport = true
378)
379
380// String returns the OS and arch variations used for the Target.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700381func (target Target) String() string {
Colin Crossa195f912019-10-16 11:07:20 -0700382 return target.OsVariation() + "_" + target.ArchVariation()
383}
384
Colin Crossa6845402020-11-16 15:08:19 -0800385// OsVariation returns the name of the variation used by the osMutator for the Target.
Colin Crossa195f912019-10-16 11:07:20 -0700386func (target Target) OsVariation() string {
387 return target.Os.String()
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700388}
389
Colin Crossa6845402020-11-16 15:08:19 -0800390// ArchVariation returns the name of the variation used by the archMutator for the Target.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700391func (target Target) ArchVariation() string {
392 var variation string
dimitry1f33e402019-03-26 12:39:31 +0100393 if target.NativeBridge {
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700394 variation = "native_bridge_"
dimitry1f33e402019-03-26 12:39:31 +0100395 }
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700396 variation += target.Arch.String()
397
Colin Crossa195f912019-10-16 11:07:20 -0700398 return variation
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700399}
400
Colin Crossa6845402020-11-16 15:08:19 -0800401// Variations returns a list of blueprint.Variations for the osMutator and archMutator for the
402// Target.
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700403func (target Target) Variations() []blueprint.Variation {
404 return []blueprint.Variation{
Colin Crossa195f912019-10-16 11:07:20 -0700405 {Mutator: "os", Variation: target.OsVariation()},
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700406 {Mutator: "arch", Variation: target.ArchVariation()},
407 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800408}
409
Colin Crossa6845402020-11-16 15:08:19 -0800410// osMutator splits an arch-specific module into a variant for each OS that is enabled for the
411// module. It uses the HostOrDevice value passed to InitAndroidArchModule and the
412// device_supported and host_supported properties to determine which OsTypes are enabled for this
413// module, then searches through the Targets to determine which have enabled Targets for this
414// module.
Colin Cross617b88a2020-08-24 18:04:09 -0700415func osMutator(bpctx blueprint.BottomUpMutatorContext) {
Colin Crossa195f912019-10-16 11:07:20 -0700416 var module Module
417 var ok bool
Colin Cross617b88a2020-08-24 18:04:09 -0700418 if module, ok = bpctx.Module().(Module); !ok {
Colin Crossa6845402020-11-16 15:08:19 -0800419 // The module is not a Soong module, it is a Blueprint module.
Colin Cross617b88a2020-08-24 18:04:09 -0700420 if bootstrap.IsBootstrapModule(bpctx.Module()) {
421 // Bootstrap Go modules are always the build OS or linux bionic.
422 config := bpctx.Config().(Config)
423 osNames := []string{config.BuildOSTarget.OsVariation()}
424 for _, hostCrossTarget := range config.Targets[LinuxBionic] {
425 if hostCrossTarget.Arch.ArchType == config.BuildOSTarget.Arch.ArchType {
426 osNames = append(osNames, hostCrossTarget.OsVariation())
427 }
428 }
429 osNames = FirstUniqueStrings(osNames)
430 bpctx.CreateVariations(osNames...)
431 }
Colin Crossa195f912019-10-16 11:07:20 -0700432 return
433 }
434
Colin Cross617b88a2020-08-24 18:04:09 -0700435 // Bootstrap Go module support above requires this mutator to be a
436 // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext
437 // filters out non-Soong modules. Now that we've handled them, create a
438 // normal android.BottomUpMutatorContext.
Liz Kammer356f7d42021-01-26 09:18:53 -0500439 mctx := bottomUpMutatorContextFactory(bpctx, module, false, false)
Colin Cross617b88a2020-08-24 18:04:09 -0700440
Colin Crossa195f912019-10-16 11:07:20 -0700441 base := module.base()
442
Colin Crossa6845402020-11-16 15:08:19 -0800443 // Nothing to do for modules that are not architecture specific (e.g. a genrule).
Colin Crossa195f912019-10-16 11:07:20 -0700444 if !base.ArchSpecific() {
445 return
446 }
447
Colin Crossa6845402020-11-16 15:08:19 -0800448 // Collect a list of OSTypes supported by this module based on the HostOrDevice value
449 // passed to InitAndroidArchModule and the device_supported and host_supported properties.
Colin Crossa195f912019-10-16 11:07:20 -0700450 var moduleOSList []OsType
Paul Duffina04c1072020-03-02 10:16:35 +0000451 for _, os := range OsTypeList {
Jiyong Park1613e552020-09-14 19:43:17 +0900452 for _, t := range mctx.Config().Targets[os] {
Colin Cross08d6f8f2020-11-19 02:33:19 +0000453 if base.supportsTarget(t) {
Jiyong Park1613e552020-09-14 19:43:17 +0900454 moduleOSList = append(moduleOSList, os)
455 break
Colin Crossa195f912019-10-16 11:07:20 -0700456 }
457 }
Colin Crossa195f912019-10-16 11:07:20 -0700458 }
459
Colin Crossa6845402020-11-16 15:08:19 -0800460 // If there are no supported OSes then disable the module.
Colin Crossa195f912019-10-16 11:07:20 -0700461 if len(moduleOSList) == 0 {
Inseob Kimeec88e12020-01-22 11:11:29 +0900462 base.Disable()
Colin Crossa195f912019-10-16 11:07:20 -0700463 return
464 }
465
Colin Crossa6845402020-11-16 15:08:19 -0800466 // Convert the list of supported OsTypes to the variation names.
Colin Crossa195f912019-10-16 11:07:20 -0700467 osNames := make([]string, len(moduleOSList))
Colin Crossa195f912019-10-16 11:07:20 -0700468 for i, os := range moduleOSList {
469 osNames[i] = os.String()
470 }
471
Paul Duffin1356d8c2020-02-25 19:26:33 +0000472 createCommonOSVariant := base.commonProperties.CreateCommonOSVariant
473 if createCommonOSVariant {
Colin Crossa6845402020-11-16 15:08:19 -0800474 // A CommonOS variant was requested so add it to the list of OS variants to
Paul Duffin1356d8c2020-02-25 19:26:33 +0000475 // create. It needs to be added to the end because it needs to depend on the
476 // the other variants in the list returned by CreateVariations(...) and inter
477 // variant dependencies can only be created from a later variant in that list to
478 // an earlier one. That is because variants are always processed in the order in
479 // which they are returned from CreateVariations(...).
480 osNames = append(osNames, CommonOS.Name)
481 moduleOSList = append(moduleOSList, CommonOS)
Colin Crossa195f912019-10-16 11:07:20 -0700482 }
483
Colin Crossa6845402020-11-16 15:08:19 -0800484 // Create the variations, annotate each one with which OS it was created for, and
485 // squash the appropriate OS-specific properties into the top level properties.
Paul Duffin1356d8c2020-02-25 19:26:33 +0000486 modules := mctx.CreateVariations(osNames...)
487 for i, m := range modules {
488 m.base().commonProperties.CompileOS = moduleOSList[i]
489 m.base().setOSProperties(mctx)
490 }
491
492 if createCommonOSVariant {
493 // A CommonOS variant was requested so add dependencies from it (the last one in
494 // the list) to the OS type specific variants.
495 last := len(modules) - 1
496 commonOSVariant := modules[last]
497 commonOSVariant.base().commonProperties.CommonOSVariant = true
498 for _, module := range modules[0:last] {
499 // Ignore modules that are enabled. Note, this will only avoid adding
500 // dependencies on OsType variants that are explicitly disabled in their
501 // properties. The CommonOS variant will still depend on disabled variants
502 // if they are disabled afterwards, e.g. in archMutator if
503 if module.Enabled() {
504 mctx.AddInterVariantDependency(commonOsToOsSpecificVariantTag, commonOSVariant, module)
505 }
506 }
507 }
508}
509
Colin Crossc179ea62020-10-09 10:54:15 -0700510type archDepTag struct {
511 blueprint.BaseDependencyTag
512 name string
513}
Paul Duffin1356d8c2020-02-25 19:26:33 +0000514
Colin Crossc179ea62020-10-09 10:54:15 -0700515// Identifies the dependency from CommonOS variant to the os specific variants.
516var commonOsToOsSpecificVariantTag = archDepTag{name: "common os to os specific"}
517
Paul Duffin1356d8c2020-02-25 19:26:33 +0000518// Get the OsType specific variants for the current CommonOS variant.
519//
520// The returned list will only contain enabled OsType specific variants of the
521// module referenced in the supplied context. An empty list is returned if there
522// are no enabled variants or the supplied context is not for an CommonOS
523// variant.
524func GetOsSpecificVariantsOfCommonOSVariant(mctx BaseModuleContext) []Module {
525 var variants []Module
526 mctx.VisitDirectDeps(func(m Module) {
527 if mctx.OtherModuleDependencyTag(m) == commonOsToOsSpecificVariantTag {
528 if m.Enabled() {
529 variants = append(variants, m)
530 }
531 }
532 })
Paul Duffin1356d8c2020-02-25 19:26:33 +0000533 return variants
Colin Crossa195f912019-10-16 11:07:20 -0700534}
535
Colin Crossee0bc3b2018-10-02 22:01:37 -0700536// archMutator splits a module into a variant for each Target requested by the module. Target selection
Colin Crossa6845402020-11-16 15:08:19 -0800537// for a module is in three levels, OsClass, multilib, and then Target.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700538// OsClass selection is determined by:
539// - The HostOrDeviceSupported value passed in to InitAndroidArchModule by the module type factory, which selects
540// whether the module type can compile for host, device or both.
541// - The host_supported and device_supported properties on the module.
Roland Levillainf5b635d2019-06-05 14:42:57 +0100542// 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 -0700543// for the module, the Device OsClass is selected.
544// Within each selected OsClass, the multilib selection is determined by:
Jaewoong Jung02b2d4d2019-06-06 15:19:57 -0700545// - The compile_multilib property if it set (which may be overridden by target.android.compile_multilib or
Colin Crossee0bc3b2018-10-02 22:01:37 -0700546// target.host.compile_multilib).
547// - The default multilib passed to InitAndroidArchModule if compile_multilib was not set.
548// Valid multilib values include:
549// "both": compile for all Targets supported by the OsClass (generally x86_64 and x86, or arm64 and arm).
550// "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 -0700551// but may be arm for a 32-bit only build.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700552// "32": compile for only a single 32-bit Target supported by the OsClass.
553// "64": compile for only a single 64-bit Target supported by the OsClass.
Colin Crossa6845402020-11-16 15:08:19 -0800554// "common": compile a for a single Target that will work on all Targets supported by the OsClass (for example Java).
555// "common_first": compile a for a Target that will work on all Targets supported by the OsClass
556// (same as "common"), plus a second Target for the preferred Target supported by the OsClass
557// (same as "first"). This is used for java_binary that produces a common .jar and a wrapper
558// executable script.
Colin Crossee0bc3b2018-10-02 22:01:37 -0700559//
560// Once the list of Targets is determined, the module is split into a variant for each Target.
561//
562// Modules can be initialized with InitAndroidMultiTargetsArchModule, in which case they will be split by OsClass,
563// 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 -0700564func archMutator(bpctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700565 var module Module
Colin Cross3f40fa42015-01-30 17:27:36 -0800566 var ok bool
Colin Cross617b88a2020-08-24 18:04:09 -0700567 if module, ok = bpctx.Module().(Module); !ok {
568 if bootstrap.IsBootstrapModule(bpctx.Module()) {
569 // Bootstrap Go modules are always the build architecture.
570 bpctx.CreateVariations(bpctx.Config().(Config).BuildOSTarget.ArchVariation())
571 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800572 return
573 }
574
Colin Cross617b88a2020-08-24 18:04:09 -0700575 // Bootstrap Go module support above requires this mutator to be a
576 // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext
577 // filters out non-Soong modules. Now that we've handled them, create a
578 // normal android.BottomUpMutatorContext.
Liz Kammer356f7d42021-01-26 09:18:53 -0500579 mctx := bottomUpMutatorContextFactory(bpctx, module, false, false)
Colin Cross617b88a2020-08-24 18:04:09 -0700580
Colin Cross5eca7cb2018-10-02 14:02:10 -0700581 base := module.base()
582
583 if !base.ArchSpecific() {
Colin Crossb9db4802016-06-03 01:50:47 +0000584 return
585 }
586
Colin Crossa195f912019-10-16 11:07:20 -0700587 os := base.commonProperties.CompileOS
Paul Duffin1356d8c2020-02-25 19:26:33 +0000588 if os == CommonOS {
589 // Make sure that the target related properties are initialized for the
590 // CommonOS variant.
591 addTargetProperties(module, commonTargetMap[os.Name], nil, true)
592
593 // Do not create arch specific variants for the CommonOS variant.
594 return
595 }
596
Colin Crossa195f912019-10-16 11:07:20 -0700597 osTargets := mctx.Config().Targets[os]
Colin Crossfb0c16e2019-11-20 17:12:35 -0800598 image := base.commonProperties.ImageVariation
Colin Crossa6845402020-11-16 15:08:19 -0800599 // Filter NativeBridge targets unless they are explicitly supported.
600 // Skip creating native bridge variants for non-core modules.
Colin Cross83bead42019-12-18 10:45:46 -0800601 if os == Android &&
602 !(Bool(base.commonProperties.Native_bridge_supported) && image == CoreVariation) {
603
Colin Crossa195f912019-10-16 11:07:20 -0700604 var targets []Target
605 for _, t := range osTargets {
606 if !t.NativeBridge {
607 targets = append(targets, t)
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700608 }
609 }
Dan Willemsen0ef639b2018-10-10 17:02:29 -0700610
Colin Crossa195f912019-10-16 11:07:20 -0700611 osTargets = targets
612 }
Colin Crossee0bc3b2018-10-02 22:01:37 -0700613
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700614 // only the primary arch in the ramdisk / vendor_ramdisk / recovery partition
615 if os == Android && (module.InstallInRecovery() || module.InstallInRamdisk() || module.InstallInVendorRamdisk()) {
Colin Crossa195f912019-10-16 11:07:20 -0700616 osTargets = []Target{osTargets[0]}
617 }
dimitry1f33e402019-03-26 12:39:31 +0100618
Colin Crossa6845402020-11-16 15:08:19 -0800619 // Some modules want compile_multilib: "first" to mean 32-bit, not 64-bit.
Colin Cross08117de2021-01-20 10:26:00 -0800620 // This is used for HOST_PREFER_32_BIT=true support for Art modules.
Colin Crossa195f912019-10-16 11:07:20 -0700621 prefer32 := false
622 if base.prefer32 != nil {
Jiyong Park1613e552020-09-14 19:43:17 +0900623 prefer32 = base.prefer32(mctx, base, os)
Colin Crossa195f912019-10-16 11:07:20 -0700624 }
Colin Cross08117de2021-01-20 10:26:00 -0800625 if os == Windows {
626 // Windows builds always prefer 32-bit
627 prefer32 = true
628 }
dimitry1f33e402019-03-26 12:39:31 +0100629
Colin Crossa6845402020-11-16 15:08:19 -0800630 // Determine the multilib selection for this module.
Colin Crossa195f912019-10-16 11:07:20 -0700631 multilib, extraMultilib := decodeMultilib(base, os.Class)
Colin Crossa6845402020-11-16 15:08:19 -0800632
633 // Convert the multilib selection into a list of Targets.
Colin Crossa195f912019-10-16 11:07:20 -0700634 targets, err := decodeMultilibTargets(multilib, osTargets, prefer32)
635 if err != nil {
636 mctx.ModuleErrorf("%s", err.Error())
637 }
Colin Cross5eca7cb2018-10-02 14:02:10 -0700638
Colin Crossa6845402020-11-16 15:08:19 -0800639 // If the module is using extraMultilib, decode the extraMultilib selection into
640 // a separate list of Targets.
Colin Crossa195f912019-10-16 11:07:20 -0700641 var multiTargets []Target
642 if extraMultilib != "" {
643 multiTargets, err = decodeMultilibTargets(extraMultilib, osTargets, prefer32)
Colin Crossa1ad8d12016-06-01 17:09:44 -0700644 if err != nil {
645 mctx.ModuleErrorf("%s", err.Error())
646 }
Colin Crossb9db4802016-06-03 01:50:47 +0000647 }
648
Colin Crossa6845402020-11-16 15:08:19 -0800649 // Recovery is always the primary architecture, filter out any other architectures.
Inseob Kim20fb5d42021-02-02 20:07:58 +0900650 // Common arch is also allowed
Colin Crossfb0c16e2019-11-20 17:12:35 -0800651 if image == RecoveryVariation {
652 primaryArch := mctx.Config().DevicePrimaryArchType()
Inseob Kim20fb5d42021-02-02 20:07:58 +0900653 targets = filterToArch(targets, primaryArch, Common)
654 multiTargets = filterToArch(multiTargets, primaryArch, Common)
Colin Crossfb0c16e2019-11-20 17:12:35 -0800655 }
656
Colin Crossa6845402020-11-16 15:08:19 -0800657 // If there are no supported targets disable the module.
Colin Crossa195f912019-10-16 11:07:20 -0700658 if len(targets) == 0 {
Inseob Kimeec88e12020-01-22 11:11:29 +0900659 base.Disable()
Dan Willemsen3f32f032016-07-11 14:36:48 -0700660 return
661 }
662
Colin Crossa6845402020-11-16 15:08:19 -0800663 // Convert the targets into a list of arch variation names.
Colin Crossa195f912019-10-16 11:07:20 -0700664 targetNames := make([]string, len(targets))
Colin Crossa195f912019-10-16 11:07:20 -0700665 for i, target := range targets {
666 targetNames[i] = target.ArchVariation()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700667 }
668
Colin Crossa6845402020-11-16 15:08:19 -0800669 // Create the variations, annotate each one with which Target it was created for, and
670 // squash the appropriate arch-specific properties into the top level properties.
Colin Crossa1ad8d12016-06-01 17:09:44 -0700671 modules := mctx.CreateVariations(targetNames...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800672 for i, m := range modules {
Paul Duffin1356d8c2020-02-25 19:26:33 +0000673 addTargetProperties(m, targets[i], multiTargets, i == 0)
Colin Cross617b88a2020-08-24 18:04:09 -0700674 m.base().setArchProperties(mctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800675 }
676}
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
Inseob Kim20fb5d42021-02-02 20:07:58 +0900723// only Targets that have the specified ArchTypes.
724func filterToArch(targets []Target, archs ...ArchType) []Target {
Colin Crossfb0c16e2019-11-20 17:12:35 -0800725 for i := 0; i < len(targets); i++ {
Inseob Kim20fb5d42021-02-02 20:07:58 +0900726 found := false
727 for _, arch := range archs {
728 if targets[i].Arch.ArchType == arch {
729 found = true
730 break
731 }
732 }
733 if !found {
Colin Crossfb0c16e2019-11-20 17:12:35 -0800734 targets = append(targets[:i], targets[i+1:]...)
735 i--
736 }
737 }
738 return targets
739}
740
Colin Crossa6845402020-11-16 15:08:19 -0800741// archPropRoot is a struct type used as the top level of the arch-specific properties. It
742// contains the "arch", "multilib", and "target" property structs. It is used to split up the
743// property structs to limit how much is allocated when a single arch-specific property group is
744// used. The types are interface{} because they will hold instances of runtime-created types.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800745type archPropRoot struct {
746 Arch, Multilib, Target interface{}
747}
748
Colin Crossa6845402020-11-16 15:08:19 -0800749// archPropTypeDesc holds the runtime-created types for the property structs to instantiate to
750// create an archPropRoot property struct.
751type archPropTypeDesc struct {
752 arch, multilib, target reflect.Type
753}
754
Colin Crosscbbd13f2020-01-17 14:08:22 -0800755// createArchPropTypeDesc takes a reflect.Type that is either a struct or a pointer to a struct, and
756// returns lists of reflect.Types that contains the arch-variant properties inside structs for each
757// arch, multilib and target property.
Colin Crossa6845402020-11-16 15:08:19 -0800758//
759// This is a relatively expensive operation, so the results are cached in the global
760// archPropTypeMap. It is constructed entirely based on compile-time data, so there is no need
761// to isolate the results between multiple tests running in parallel.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800762func createArchPropTypeDesc(props reflect.Type) []archPropTypeDesc {
Colin Crossb1d8c992020-01-21 11:43:29 -0800763 // Each property struct shard will be nested many times under the runtime generated arch struct,
764 // which can hit the limit of 64kB for the name of runtime generated structs. They are nested
765 // 97 times now, which may grow in the future, plus there is some overhead for the containing
766 // type. This number may need to be reduced if too many are added, but reducing it too far
767 // could cause problems if a single deeply nested property no longer fits in the name.
768 const maxArchTypeNameSize = 500
769
Colin Crossa6845402020-11-16 15:08:19 -0800770 // Convert the type to a new set of types that contains only the arch-specific properties
771 // (those that are tagged with `android:"arch_specific"`), and sharded into multiple types
772 // to keep the runtime-generated names under the limit.
Colin Crossb1d8c992020-01-21 11:43:29 -0800773 propShards, _ := proptools.FilterPropertyStructSharded(props, maxArchTypeNameSize, filterArchStruct)
Colin Crossa6845402020-11-16 15:08:19 -0800774
775 // If the type has no arch-specific properties there is nothing to do.
Colin Crosscb988072019-01-24 14:58:11 -0800776 if len(propShards) == 0 {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700777 return nil
778 }
779
Colin Crosscbbd13f2020-01-17 14:08:22 -0800780 var ret []archPropTypeDesc
Colin Crossc17727d2018-10-24 12:42:09 -0700781 for _, props := range propShards {
Dan Willemsenb1957a52016-06-23 23:44:54 -0700782
Colin Crossa6845402020-11-16 15:08:19 -0800783 // variantFields takes a list of variant property field names and returns a list the
784 // StructFields with the names and the type of the current shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700785 variantFields := func(names []string) []reflect.StructField {
786 ret := make([]reflect.StructField, len(names))
Dan Willemsenb1957a52016-06-23 23:44:54 -0700787
Colin Crossc17727d2018-10-24 12:42:09 -0700788 for i, name := range names {
789 ret[i].Name = name
790 ret[i].Type = props
Dan Willemsen866b5632017-09-22 12:28:24 -0700791 }
Colin Crossc17727d2018-10-24 12:42:09 -0700792
793 return ret
794 }
795
Colin Crossa6845402020-11-16 15:08:19 -0800796 // Create a type that contains the properties in this shard repeated for each
797 // architecture, architecture variant, and architecture feature.
Colin Crossc17727d2018-10-24 12:42:09 -0700798 archFields := make([]reflect.StructField, len(archTypeList))
799 for i, arch := range archTypeList {
Colin Crossa6845402020-11-16 15:08:19 -0800800 var variants []string
Colin Crossc17727d2018-10-24 12:42:09 -0700801
802 for _, archVariant := range archVariants[arch] {
803 archVariant := variantReplacer.Replace(archVariant)
804 variants = append(variants, proptools.FieldNameForProperty(archVariant))
805 }
806 for _, feature := range archFeatures[arch] {
807 feature := variantReplacer.Replace(feature)
808 variants = append(variants, proptools.FieldNameForProperty(feature))
809 }
810
Colin Crossa6845402020-11-16 15:08:19 -0800811 // Create the StructFields for each architecture variant architecture feature
812 // (e.g. "arch.arm.cortex-a53" or "arch.arm.neon").
Colin Crossc17727d2018-10-24 12:42:09 -0700813 fields := variantFields(variants)
814
Colin Crossa6845402020-11-16 15:08:19 -0800815 // Create the StructField for the architecture itself (e.g. "arch.arm"). The special
816 // "BlueprintEmbed" name is used by Blueprint to put the properties in the
817 // parent struct.
Colin Crossc17727d2018-10-24 12:42:09 -0700818 fields = append([]reflect.StructField{{
819 Name: "BlueprintEmbed",
820 Type: props,
821 Anonymous: true,
822 }}, fields...)
823
824 archFields[i] = reflect.StructField{
825 Name: arch.Field,
826 Type: reflect.StructOf(fields),
827 }
828 }
Colin Crossa6845402020-11-16 15:08:19 -0800829
830 // Create the type of the "arch" property struct for this shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700831 archType := reflect.StructOf(archFields)
832
Colin Crossa6845402020-11-16 15:08:19 -0800833 // Create the type for the "multilib" property struct for this shard, containing the
834 // "multilib.lib32" and "multilib.lib64" property structs.
Colin Crossc17727d2018-10-24 12:42:09 -0700835 multilibType := reflect.StructOf(variantFields([]string{"Lib32", "Lib64"}))
836
Colin Crossa6845402020-11-16 15:08:19 -0800837 // Start with a list of the special targets
Colin Crossc17727d2018-10-24 12:42:09 -0700838 targets := []string{
839 "Host",
840 "Android64",
841 "Android32",
842 "Bionic",
843 "Linux",
844 "Not_windows",
845 "Arm_on_x86",
846 "Arm_on_x86_64",
Victor Khimenkoc26fcf42020-05-07 22:16:33 +0200847 "Native_bridge",
Colin Crossc17727d2018-10-24 12:42:09 -0700848 }
Paul Duffina04c1072020-03-02 10:16:35 +0000849 for _, os := range OsTypeList {
Colin Crossa6845402020-11-16 15:08:19 -0800850 // Add all the OSes.
Colin Crossc17727d2018-10-24 12:42:09 -0700851 targets = append(targets, os.Field)
852
Colin Crossa6845402020-11-16 15:08:19 -0800853 // Add the OS/Arch combinations, e.g. "android_arm64".
Colin Crossc17727d2018-10-24 12:42:09 -0700854 for _, archType := range osArchTypeMap[os] {
855 targets = append(targets, os.Field+"_"+archType.Name)
856
Colin Crossa6845402020-11-16 15:08:19 -0800857 // Also add the special "linux_<arch>" and "bionic_<arch>" property structs.
Colin Crossc17727d2018-10-24 12:42:09 -0700858 if os.Linux() {
859 target := "Linux_" + archType.Name
860 if !InList(target, targets) {
861 targets = append(targets, target)
862 }
863 }
864 if os.Bionic() {
865 target := "Bionic_" + archType.Name
866 if !InList(target, targets) {
867 targets = append(targets, target)
868 }
Dan Willemsen866b5632017-09-22 12:28:24 -0700869 }
870 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700871 }
Dan Willemsenb1957a52016-06-23 23:44:54 -0700872
Colin Crossa6845402020-11-16 15:08:19 -0800873 // Create the type for the "target" property struct for this shard.
Colin Crossc17727d2018-10-24 12:42:09 -0700874 targetType := reflect.StructOf(variantFields(targets))
Colin Crosscbbd13f2020-01-17 14:08:22 -0800875
Colin Crossa6845402020-11-16 15:08:19 -0800876 // Return a descriptor of the 3 runtime-created types.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800877 ret = append(ret, archPropTypeDesc{
878 arch: reflect.PtrTo(archType),
879 multilib: reflect.PtrTo(multilibType),
880 target: reflect.PtrTo(targetType),
881 })
Colin Crossc17727d2018-10-24 12:42:09 -0700882 }
883 return ret
Dan Willemsenb1957a52016-06-23 23:44:54 -0700884}
885
Colin Crossa6845402020-11-16 15:08:19 -0800886// variantReplacer converts architecture variant or architecture feature names into names that
887// are valid for an Android.bp file.
888var variantReplacer = strings.NewReplacer("-", "_", ".", "_")
889
890// filterArchStruct returns true if the given field is an architecture specific property.
Colin Cross74449102019-09-25 11:26:40 -0700891func filterArchStruct(field reflect.StructField, prefix string) (bool, reflect.StructField) {
892 if proptools.HasTag(field, "android", "arch_variant") {
893 // The arch_variant field isn't necessary past this point
894 // Instead of wasting space, just remove it. Go also has a
895 // 16-bit limit on structure name length. The name is constructed
896 // based on the Go source representation of the structure, so
897 // the tag names count towards that length.
Colin Crossb4fecbf2020-01-21 11:38:47 -0800898
899 androidTag := field.Tag.Get("android")
900 values := strings.Split(androidTag, ",")
901
902 if string(field.Tag) != `android:"`+strings.Join(values, ",")+`"` {
903 panic(fmt.Errorf("unexpected tag format %q", field.Tag))
Colin Cross74449102019-09-25 11:26:40 -0700904 }
Colin Crossb4fecbf2020-01-21 11:38:47 -0800905 // these tags don't need to be present in the runtime generated struct type.
906 values = RemoveListFromList(values, []string{"arch_variant", "variant_prepend", "path"})
907 if len(values) > 0 {
908 panic(fmt.Errorf("unknown tags %q in field %q", values, prefix+field.Name))
909 }
910
911 field.Tag = ""
Colin Cross74449102019-09-25 11:26:40 -0700912 return true, field
913 }
914 return false, field
915}
916
Colin Crossa6845402020-11-16 15:08:19 -0800917// archPropTypeMap contains a cache of the results of createArchPropTypeDesc for each type. It is
918// shared across all Contexts, but is constructed based only on compile-time information so there
919// is no risk of contaminating one Context with data from another.
Dan Willemsenb1957a52016-06-23 23:44:54 -0700920var archPropTypeMap OncePer
921
Colin Crossa6845402020-11-16 15:08:19 -0800922// initArchModule adds the architecture-specific property structs to a Module.
923func initArchModule(m Module) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800924
925 base := m.base()
926
Colin Crossa6845402020-11-16 15:08:19 -0800927 // Store the original list of top level property structs
Colin Cross36242852017-06-23 15:06:31 -0700928 base.generalProperties = m.GetProperties()
Colin Cross3f40fa42015-01-30 17:27:36 -0800929
930 for _, properties := range base.generalProperties {
931 propertiesValue := reflect.ValueOf(properties)
Colin Cross62496a02016-08-08 15:49:17 -0700932 t := propertiesValue.Type()
Colin Cross3f40fa42015-01-30 17:27:36 -0800933 if propertiesValue.Kind() != reflect.Ptr {
Colin Crossca860ac2016-01-04 14:34:37 -0800934 panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
935 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800936 }
937
938 propertiesValue = propertiesValue.Elem()
939 if propertiesValue.Kind() != reflect.Struct {
Colin Crossca860ac2016-01-04 14:34:37 -0800940 panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
941 propertiesValue.Interface()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800942 }
943
Colin Crossa6845402020-11-16 15:08:19 -0800944 // Get or create the arch-specific property struct types for this property struct type.
Colin Cross571cccf2019-02-04 11:22:08 -0800945 archPropTypes := archPropTypeMap.Once(NewCustomOnceKey(t), func() interface{} {
Colin Crosscbbd13f2020-01-17 14:08:22 -0800946 return createArchPropTypeDesc(t)
947 }).([]archPropTypeDesc)
Colin Cross3f40fa42015-01-30 17:27:36 -0800948
Colin Crossa6845402020-11-16 15:08:19 -0800949 // Instantiate one of each arch-specific property struct type and add it to the
950 // properties for the Module.
Colin Crossc17727d2018-10-24 12:42:09 -0700951 var archProperties []interface{}
952 for _, t := range archPropTypes {
Colin Crosscbbd13f2020-01-17 14:08:22 -0800953 archProperties = append(archProperties, &archPropRoot{
954 Arch: reflect.Zero(t.arch).Interface(),
955 Multilib: reflect.Zero(t.multilib).Interface(),
956 Target: reflect.Zero(t.target).Interface(),
957 })
Dan Willemsenb1957a52016-06-23 23:44:54 -0700958 }
Colin Crossc17727d2018-10-24 12:42:09 -0700959 base.archProperties = append(base.archProperties, archProperties)
960 m.AddProperties(archProperties...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800961 }
962
Colin Crossa6845402020-11-16 15:08:19 -0800963 // Update the list of properties that can be set by a defaults module or a call to
964 // AppendMatchingProperties or PrependMatchingProperties.
Colin Cross36242852017-06-23 15:06:31 -0700965 base.customizableProperties = m.GetProperties()
Colin Cross3f40fa42015-01-30 17:27:36 -0800966}
967
Colin Crossa6845402020-11-16 15:08:19 -0800968// appendProperties squashes properties from the given field of the given src property struct
969// into the dst property struct. Returns the reflect.Value of the field in the src property
970// struct to be used for further appendProperties calls on fields of that property struct.
Colin Cross4157e882019-06-06 16:57:04 -0700971func (m *ModuleBase) appendProperties(ctx BottomUpMutatorContext,
Dan Willemsenb1957a52016-06-23 23:44:54 -0700972 dst interface{}, src reflect.Value, field, srcPrefix string) reflect.Value {
Colin Cross06a931b2015-10-28 17:23:31 -0700973
Colin Crossa6845402020-11-16 15:08:19 -0800974 // Step into non-nil pointers to structs in the src value.
Colin Crosscbbd13f2020-01-17 14:08:22 -0800975 if src.Kind() == reflect.Ptr {
976 if src.IsNil() {
977 return src
978 }
979 src = src.Elem()
980 }
981
Colin Crossa6845402020-11-16 15:08:19 -0800982 // Find the requested field in the src struct.
Dan Willemsenb1957a52016-06-23 23:44:54 -0700983 src = src.FieldByName(field)
984 if !src.IsValid() {
Colin Crosseeabb892015-11-20 13:07:51 -0800985 ctx.ModuleErrorf("field %q does not exist", srcPrefix)
Dan Willemsenb1957a52016-06-23 23:44:54 -0700986 return src
Colin Cross85a88972015-11-23 13:29:51 -0800987 }
988
Colin Crossa6845402020-11-16 15:08:19 -0800989 // Save the value of the field in the src struct to return.
Dan Willemsenb1957a52016-06-23 23:44:54 -0700990 ret := src
Colin Cross85a88972015-11-23 13:29:51 -0800991
Colin Crossa6845402020-11-16 15:08:19 -0800992 // If the value of the field is a struct (as opposed to a pointer to a struct) then step
993 // into the BlueprintEmbed field.
Dan Willemsenb1957a52016-06-23 23:44:54 -0700994 if src.Kind() == reflect.Struct {
995 src = src.FieldByName("BlueprintEmbed")
Colin Cross06a931b2015-10-28 17:23:31 -0700996 }
997
Colin Crossa6845402020-11-16 15:08:19 -0800998 // order checks the `android:"variant_prepend"` tag to handle properties where the
999 // arch-specific value needs to come before the generic value, for example for lists of
1000 // include directories.
Colin Cross6ee75b62016-05-05 15:57:15 -07001001 order := func(property string,
1002 dstField, srcField reflect.StructField,
1003 dstValue, srcValue interface{}) (proptools.Order, error) {
1004 if proptools.HasTag(dstField, "android", "variant_prepend") {
1005 return proptools.Prepend, nil
1006 } else {
1007 return proptools.Append, nil
1008 }
1009 }
1010
Colin Crossa6845402020-11-16 15:08:19 -08001011 // Squash the located property struct into the destination property struct.
Dan Willemsenb1957a52016-06-23 23:44:54 -07001012 err := proptools.ExtendMatchingProperties([]interface{}{dst}, src.Interface(), nil, order)
Colin Cross06a931b2015-10-28 17:23:31 -07001013 if err != nil {
1014 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
1015 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
1016 } else {
1017 panic(err)
1018 }
1019 }
Colin Cross85a88972015-11-23 13:29:51 -08001020
Dan Willemsenb1957a52016-06-23 23:44:54 -07001021 return ret
Colin Cross06a931b2015-10-28 17:23:31 -07001022}
1023
Colin Crossa6845402020-11-16 15:08:19 -08001024// Squash the appropriate OS-specific property structs into the matching top level property structs
1025// based on the CompileOS value that was annotated on the variant.
Colin Crossa195f912019-10-16 11:07:20 -07001026func (m *ModuleBase) setOSProperties(ctx BottomUpMutatorContext) {
1027 os := m.commonProperties.CompileOS
1028
1029 for i := range m.generalProperties {
1030 genProps := m.generalProperties[i]
1031 if m.archProperties[i] == nil {
1032 continue
1033 }
1034 for _, archProperties := range m.archProperties[i] {
1035 archPropValues := reflect.ValueOf(archProperties).Elem()
1036
Colin Crosscbbd13f2020-01-17 14:08:22 -08001037 targetProp := archPropValues.FieldByName("Target").Elem()
Colin Crossa195f912019-10-16 11:07:20 -07001038
1039 // Handle host-specific properties in the form:
1040 // target: {
1041 // host: {
1042 // key: value,
1043 // },
1044 // },
Jiyong Park1613e552020-09-14 19:43:17 +09001045 if os.Class == Host {
Colin Crossa195f912019-10-16 11:07:20 -07001046 field := "Host"
1047 prefix := "target.host"
1048 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1049 }
1050
1051 // Handle target OS generalities of the form:
1052 // target: {
1053 // bionic: {
1054 // key: value,
1055 // },
1056 // }
1057 if os.Linux() {
1058 field := "Linux"
1059 prefix := "target.linux"
1060 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1061 }
1062
1063 if os.Bionic() {
1064 field := "Bionic"
1065 prefix := "target.bionic"
1066 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1067 }
1068
1069 // Handle target OS properties in the form:
1070 // target: {
1071 // linux_glibc: {
1072 // key: value,
1073 // },
1074 // not_windows: {
1075 // key: value,
1076 // },
1077 // android {
1078 // key: value,
1079 // },
1080 // },
1081 field := os.Field
1082 prefix := "target." + os.Name
1083 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1084
Jiyong Park1613e552020-09-14 19:43:17 +09001085 if os.Class == Host && os != Windows {
Colin Crossa195f912019-10-16 11:07:20 -07001086 field := "Not_windows"
1087 prefix := "target.not_windows"
1088 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1089 }
1090
1091 // Handle 64-bit device properties in the form:
1092 // target {
1093 // android64 {
1094 // key: value,
1095 // },
1096 // android32 {
1097 // key: value,
1098 // },
1099 // },
1100 // WARNING: this is probably not what you want to use in your blueprints file, it selects
1101 // options for all targets on a device that supports 64-bit binaries, not just the targets
1102 // that are being compiled for 64-bit. Its expected use case is binaries like linker and
1103 // debuggerd that need to know when they are a 32-bit process running on a 64-bit device
1104 if os.Class == Device {
1105 if ctx.Config().Android64() {
1106 field := "Android64"
1107 prefix := "target.android64"
1108 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1109 } else {
1110 field := "Android32"
1111 prefix := "target.android32"
1112 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1113 }
1114 }
1115 }
1116 }
1117}
1118
Colin Crossa6845402020-11-16 15:08:19 -08001119// Squash the appropriate arch-specific property structs into the matching top level property
1120// structs based on the CompileTarget value that was annotated on the variant.
Colin Cross4157e882019-06-06 16:57:04 -07001121func (m *ModuleBase) setArchProperties(ctx BottomUpMutatorContext) {
1122 arch := m.Arch()
1123 os := m.Os()
Colin Crossd3ba0392015-05-07 14:11:29 -07001124
Colin Cross4157e882019-06-06 16:57:04 -07001125 for i := range m.generalProperties {
1126 genProps := m.generalProperties[i]
1127 if m.archProperties[i] == nil {
Dan Willemsenb1957a52016-06-23 23:44:54 -07001128 continue
1129 }
Colin Cross4157e882019-06-06 16:57:04 -07001130 for _, archProperties := range m.archProperties[i] {
Colin Crossc17727d2018-10-24 12:42:09 -07001131 archPropValues := reflect.ValueOf(archProperties).Elem()
Dan Willemsenb1957a52016-06-23 23:44:54 -07001132
Colin Crosscbbd13f2020-01-17 14:08:22 -08001133 archProp := archPropValues.FieldByName("Arch").Elem()
1134 multilibProp := archPropValues.FieldByName("Multilib").Elem()
1135 targetProp := archPropValues.FieldByName("Target").Elem()
Dan Willemsenb1957a52016-06-23 23:44:54 -07001136
Colin Crossc17727d2018-10-24 12:42:09 -07001137 // Handle arch-specific properties in the form:
Colin Crossd5934c82017-10-02 13:55:26 -07001138 // arch: {
Colin Crossc17727d2018-10-24 12:42:09 -07001139 // arm64: {
Colin Crossd5934c82017-10-02 13:55:26 -07001140 // key: value,
1141 // },
1142 // },
Colin Crossc17727d2018-10-24 12:42:09 -07001143 t := arch.ArchType
1144
1145 if arch.ArchType != Common {
1146 field := proptools.FieldNameForProperty(t.Name)
1147 prefix := "arch." + t.Name
Colin Cross4157e882019-06-06 16:57:04 -07001148 archStruct := m.appendProperties(ctx, genProps, archProp, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001149
1150 // Handle arch-variant-specific properties in the form:
1151 // arch: {
1152 // variant: {
1153 // key: value,
1154 // },
1155 // },
1156 v := variantReplacer.Replace(arch.ArchVariant)
1157 if v != "" {
1158 field := proptools.FieldNameForProperty(v)
1159 prefix := "arch." + t.Name + "." + v
Colin Cross4157e882019-06-06 16:57:04 -07001160 m.appendProperties(ctx, genProps, archStruct, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001161 }
1162
1163 // Handle cpu-variant-specific properties in the form:
1164 // arch: {
1165 // variant: {
1166 // key: value,
1167 // },
1168 // },
1169 if arch.CpuVariant != arch.ArchVariant {
1170 c := variantReplacer.Replace(arch.CpuVariant)
1171 if c != "" {
1172 field := proptools.FieldNameForProperty(c)
1173 prefix := "arch." + t.Name + "." + c
Colin Cross4157e882019-06-06 16:57:04 -07001174 m.appendProperties(ctx, genProps, archStruct, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001175 }
1176 }
1177
1178 // Handle arch-feature-specific properties in the form:
1179 // arch: {
1180 // feature: {
1181 // key: value,
1182 // },
1183 // },
1184 for _, feature := range arch.ArchFeatures {
1185 field := proptools.FieldNameForProperty(feature)
1186 prefix := "arch." + t.Name + "." + feature
Colin Cross4157e882019-06-06 16:57:04 -07001187 m.appendProperties(ctx, genProps, archStruct, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001188 }
1189
1190 // Handle multilib-specific properties in the form:
1191 // multilib: {
1192 // lib32: {
1193 // key: value,
1194 // },
1195 // },
1196 field = proptools.FieldNameForProperty(t.Multilib)
1197 prefix = "multilib." + t.Multilib
Colin Cross4157e882019-06-06 16:57:04 -07001198 m.appendProperties(ctx, genProps, multilibProp, field, prefix)
Colin Cross08016332016-12-20 09:53:14 -08001199 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001200
Colin Crossa195f912019-10-16 11:07:20 -07001201 // Handle combined OS-feature and arch specific properties in the form:
Colin Crossc17727d2018-10-24 12:42:09 -07001202 // target: {
Colin Crossc17727d2018-10-24 12:42:09 -07001203 // bionic_x86: {
1204 // key: value,
1205 // },
1206 // }
Colin Crossa195f912019-10-16 11:07:20 -07001207 if os.Linux() && arch.ArchType != Common {
1208 field := "Linux_" + arch.ArchType.Name
1209 prefix := "target.linux_" + arch.ArchType.Name
Colin Cross4157e882019-06-06 16:57:04 -07001210 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossd5934c82017-10-02 13:55:26 -07001211 }
Colin Crossc5c24ad2015-11-20 15:35:00 -08001212
Colin Crossa195f912019-10-16 11:07:20 -07001213 if os.Bionic() && arch.ArchType != Common {
1214 field := "Bionic_" + t.Name
1215 prefix := "target.bionic_" + t.Name
Colin Cross4157e882019-06-06 16:57:04 -07001216 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001217 }
1218
Colin Crossa195f912019-10-16 11:07:20 -07001219 // Handle combined OS and arch specific properties in the form:
Colin Crossc17727d2018-10-24 12:42:09 -07001220 // target: {
Colin Crossc17727d2018-10-24 12:42:09 -07001221 // linux_glibc_x86: {
1222 // key: value,
1223 // },
1224 // linux_glibc_arm: {
1225 // key: value,
1226 // },
Colin Crossc17727d2018-10-24 12:42:09 -07001227 // android_arm {
1228 // key: value,
1229 // },
1230 // android_x86 {
Colin Crossd5934c82017-10-02 13:55:26 -07001231 // key: value,
1232 // },
1233 // },
Colin Crossc17727d2018-10-24 12:42:09 -07001234 if arch.ArchType != Common {
Colin Crossa195f912019-10-16 11:07:20 -07001235 field := os.Field + "_" + t.Name
1236 prefix := "target." + os.Name + "_" + t.Name
Colin Cross4157e882019-06-06 16:57:04 -07001237 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossd5934c82017-10-02 13:55:26 -07001238 }
1239
Colin Crossa195f912019-10-16 11:07:20 -07001240 // Handle arm on x86 properties in the form:
Colin Crossc17727d2018-10-24 12:42:09 -07001241 // target {
Colin Crossa195f912019-10-16 11:07:20 -07001242 // arm_on_x86 {
Colin Crossc17727d2018-10-24 12:42:09 -07001243 // key: value,
1244 // },
Colin Crossa195f912019-10-16 11:07:20 -07001245 // arm_on_x86_64 {
Colin Crossd5934c82017-10-02 13:55:26 -07001246 // key: value,
1247 // },
1248 // },
Colin Crossc17727d2018-10-24 12:42:09 -07001249 if os.Class == Device {
Victor Khimenko1a31f802020-09-17 03:07:31 +02001250 if arch.ArchType == X86 && (hasArmAbi(arch) ||
1251 hasArmAndroidArch(ctx.Config().Targets[Android])) {
Colin Crossc17727d2018-10-24 12:42:09 -07001252 field := "Arm_on_x86"
1253 prefix := "target.arm_on_x86"
Colin Cross4157e882019-06-06 16:57:04 -07001254 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001255 }
Victor Khimenko1a31f802020-09-17 03:07:31 +02001256 if arch.ArchType == X86_64 && (hasArmAbi(arch) ||
1257 hasArmAndroidArch(ctx.Config().Targets[Android])) {
Colin Crossc17727d2018-10-24 12:42:09 -07001258 field := "Arm_on_x86_64"
1259 prefix := "target.arm_on_x86_64"
Colin Cross4157e882019-06-06 16:57:04 -07001260 m.appendProperties(ctx, genProps, targetProp, field, prefix)
Colin Crossc17727d2018-10-24 12:42:09 -07001261 }
Victor Khimenkoc26fcf42020-05-07 22:16:33 +02001262 if os == Android && m.Target().NativeBridge == NativeBridgeEnabled {
1263 field := "Native_bridge"
1264 prefix := "target.native_bridge"
1265 m.appendProperties(ctx, genProps, targetProp, field, prefix)
1266 }
Colin Cross4247f0d2017-04-13 16:56:14 -07001267 }
Colin Crossbb2e2b72016-12-08 17:23:53 -08001268 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001269 }
1270}
1271
Colin Crossa6845402020-11-16 15:08:19 -08001272// Convert the arch product variables into a list of targets for each OsType.
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001273func decodeTargetProductVariables(config *config) (map[OsType][]Target, error) {
Dan Willemsen45133ac2018-03-09 21:22:06 -08001274 variables := config.productVariables
Dan Willemsen490fd492015-11-24 17:53:15 -08001275
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001276 targets := make(map[OsType][]Target)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001277 var targetErr error
1278
dimitry1f33e402019-03-26 12:39:31 +01001279 addTarget := func(os OsType, archName string, archVariant, cpuVariant *string, abi []string,
dimitry8d6dde82019-07-11 10:23:53 +02001280 nativeBridgeEnabled NativeBridgeSupport, nativeBridgeHostArchName *string,
1281 nativeBridgeRelativePath *string) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001282 if targetErr != nil {
1283 return
Dan Willemsen490fd492015-11-24 17:53:15 -08001284 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001285
Dan Willemsen01a3c252019-01-11 19:02:16 -08001286 arch, err := decodeArch(os, archName, archVariant, cpuVariant, abi)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001287 if err != nil {
1288 targetErr = err
1289 return
1290 }
dimitry8d6dde82019-07-11 10:23:53 +02001291 nativeBridgeRelativePathStr := String(nativeBridgeRelativePath)
1292 nativeBridgeHostArchNameStr := String(nativeBridgeHostArchName)
1293
1294 // Use guest arch as relative install path by default
1295 if nativeBridgeEnabled && nativeBridgeRelativePathStr == "" {
1296 nativeBridgeRelativePathStr = arch.ArchType.String()
1297 }
Colin Crossa1ad8d12016-06-01 17:09:44 -07001298
Jiyong Park1613e552020-09-14 19:43:17 +09001299 // A target is considered as HostCross if it's a host target which can't run natively on
1300 // the currently configured build machine (either because the OS is different or because of
1301 // the unsupported arch)
1302 hostCross := false
1303 if os.Class == Host {
1304 var osSupported bool
1305 if os == BuildOs {
1306 osSupported = true
1307 } else if BuildOs.Linux() && os.Linux() {
1308 // LinuxBionic and Linux are compatible
1309 osSupported = true
1310 } else {
1311 osSupported = false
1312 }
1313
1314 var archSupported bool
1315 if arch.ArchType == Common {
1316 archSupported = true
1317 } else if arch.ArchType.Name == *variables.HostArch {
1318 archSupported = true
1319 } else if variables.HostSecondaryArch != nil && arch.ArchType.Name == *variables.HostSecondaryArch {
1320 archSupported = true
1321 } else {
1322 archSupported = false
1323 }
1324 if !osSupported || !archSupported {
1325 hostCross = true
1326 }
1327 }
1328
Dan Willemsen0ef639b2018-10-10 17:02:29 -07001329 targets[os] = append(targets[os],
Colin Crossa1ad8d12016-06-01 17:09:44 -07001330 Target{
dimitry8d6dde82019-07-11 10:23:53 +02001331 Os: os,
1332 Arch: arch,
1333 NativeBridge: nativeBridgeEnabled,
1334 NativeBridgeHostArchName: nativeBridgeHostArchNameStr,
1335 NativeBridgeRelativePath: nativeBridgeRelativePathStr,
Jiyong Park1613e552020-09-14 19:43:17 +09001336 HostCross: hostCross,
Colin Crossa1ad8d12016-06-01 17:09:44 -07001337 })
Dan Willemsen490fd492015-11-24 17:53:15 -08001338 }
1339
Colin Cross4225f652015-09-17 14:33:42 -07001340 if variables.HostArch == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001341 return nil, fmt.Errorf("No host primary architecture set")
Colin Cross4225f652015-09-17 14:33:42 -07001342 }
1343
Colin Crossa6845402020-11-16 15:08:19 -08001344 // The primary host target, which must always exist.
dimitry8d6dde82019-07-11 10:23:53 +02001345 addTarget(BuildOs, *variables.HostArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -07001346
Colin Crossa6845402020-11-16 15:08:19 -08001347 // An optional secondary host target.
Colin Crosseeabb892015-11-20 13:07:51 -08001348 if variables.HostSecondaryArch != nil && *variables.HostSecondaryArch != "" {
dimitry8d6dde82019-07-11 10:23:53 +02001349 addTarget(BuildOs, *variables.HostSecondaryArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -08001350 }
1351
Colin Crossa6845402020-11-16 15:08:19 -08001352 // Optional cross-compiled host targets, generally Windows.
Colin Crossff3ae9d2018-04-10 16:15:18 -07001353 if String(variables.CrossHost) != "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001354 crossHostOs := osByName(*variables.CrossHost)
1355 if crossHostOs == NoOsType {
1356 return nil, fmt.Errorf("Unknown cross host OS %q", *variables.CrossHost)
1357 }
1358
Colin Crossff3ae9d2018-04-10 16:15:18 -07001359 if String(variables.CrossHostArch) == "" {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001360 return nil, fmt.Errorf("No cross-host primary architecture set")
Dan Willemsen490fd492015-11-24 17:53:15 -08001361 }
1362
Colin Crossa6845402020-11-16 15:08:19 -08001363 // The primary cross-compiled host target.
dimitry8d6dde82019-07-11 10:23:53 +02001364 addTarget(crossHostOs, *variables.CrossHostArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -08001365
Colin Crossa6845402020-11-16 15:08:19 -08001366 // An optional secondary cross-compiled host target.
Dan Willemsen490fd492015-11-24 17:53:15 -08001367 if variables.CrossHostSecondaryArch != nil && *variables.CrossHostSecondaryArch != "" {
dimitry8d6dde82019-07-11 10:23:53 +02001368 addTarget(crossHostOs, *variables.CrossHostSecondaryArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
Dan Willemsen490fd492015-11-24 17:53:15 -08001369 }
1370 }
1371
Colin Crossa6845402020-11-16 15:08:19 -08001372 // Optional device targets
Dan Willemsen3f32f032016-07-11 14:36:48 -07001373 if variables.DeviceArch != nil && *variables.DeviceArch != "" {
Doug Horn21b94272019-01-16 12:06:11 -08001374 var target = Android
1375 if Bool(variables.Fuchsia) {
1376 target = Fuchsia
1377 }
1378
Colin Crossa6845402020-11-16 15:08:19 -08001379 // The primary device target.
Doug Horn21b94272019-01-16 12:06:11 -08001380 addTarget(target, *variables.DeviceArch, variables.DeviceArchVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001381 variables.DeviceCpuVariant, variables.DeviceAbi, NativeBridgeDisabled, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -07001382
Colin Crossa6845402020-11-16 15:08:19 -08001383 // An optional secondary device target.
Dan Willemsen3f32f032016-07-11 14:36:48 -07001384 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" {
1385 addTarget(Android, *variables.DeviceSecondaryArch,
1386 variables.DeviceSecondaryArchVariant, variables.DeviceSecondaryCpuVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001387 variables.DeviceSecondaryAbi, NativeBridgeDisabled, nil, nil)
Colin Cross4225f652015-09-17 14:33:42 -07001388 }
dimitry1f33e402019-03-26 12:39:31 +01001389
Colin Crossa6845402020-11-16 15:08:19 -08001390 // An optional NativeBridge device target.
dimitry1f33e402019-03-26 12:39:31 +01001391 if variables.NativeBridgeArch != nil && *variables.NativeBridgeArch != "" {
1392 addTarget(Android, *variables.NativeBridgeArch,
1393 variables.NativeBridgeArchVariant, variables.NativeBridgeCpuVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001394 variables.NativeBridgeAbi, NativeBridgeEnabled, variables.DeviceArch,
1395 variables.NativeBridgeRelativePath)
dimitry1f33e402019-03-26 12:39:31 +01001396 }
1397
Colin Crossa6845402020-11-16 15:08:19 -08001398 // An optional secondary NativeBridge device target.
dimitry1f33e402019-03-26 12:39:31 +01001399 if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" &&
1400 variables.NativeBridgeSecondaryArch != nil && *variables.NativeBridgeSecondaryArch != "" {
1401 addTarget(Android, *variables.NativeBridgeSecondaryArch,
1402 variables.NativeBridgeSecondaryArchVariant,
1403 variables.NativeBridgeSecondaryCpuVariant,
dimitry8d6dde82019-07-11 10:23:53 +02001404 variables.NativeBridgeSecondaryAbi,
1405 NativeBridgeEnabled,
1406 variables.DeviceSecondaryArch,
1407 variables.NativeBridgeSecondaryRelativePath)
dimitry1f33e402019-03-26 12:39:31 +01001408 }
Colin Cross4225f652015-09-17 14:33:42 -07001409 }
1410
Colin Crossa1ad8d12016-06-01 17:09:44 -07001411 if targetErr != nil {
1412 return nil, targetErr
1413 }
1414
1415 return targets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001416}
1417
Colin Crossbb2e2b72016-12-08 17:23:53 -08001418// hasArmAbi returns true if arch has at least one arm ABI
1419func hasArmAbi(arch Arch) bool {
Jaewoong Jung3aff5782020-02-11 07:54:35 -08001420 return PrefixInList(arch.Abi, "arm")
Colin Crossbb2e2b72016-12-08 17:23:53 -08001421}
1422
dimitry628db6f2019-05-22 17:16:21 +02001423// hasArmArch returns true if targets has at least non-native_bridge arm Android arch
Colin Cross4247f0d2017-04-13 16:56:14 -07001424func hasArmAndroidArch(targets []Target) bool {
1425 for _, target := range targets {
Victor Khimenko1a31f802020-09-17 03:07:31 +02001426 if target.Os == Android && target.Arch.ArchType == Arm {
Victor Khimenko5eb8ec12018-03-21 20:30:54 +01001427 return true
1428 }
1429 }
1430 return false
1431}
1432
Colin Crossa6845402020-11-16 15:08:19 -08001433// archConfig describes a built-in configuration.
Dan Albert4098deb2016-10-19 14:04:41 -07001434type archConfig struct {
1435 arch string
1436 archVariant string
1437 cpuVariant string
1438 abi []string
1439}
1440
Colin Crossa6845402020-11-16 15:08:19 -08001441// getNdkAbisConfig returns a list of archConfigs for the ABIs supported by the NDK.
Dan Albert4098deb2016-10-19 14:04:41 -07001442func getNdkAbisConfig() []archConfig {
1443 return []archConfig{
Dan Albert6bba6442020-01-30 15:16:49 -08001444 {"arm", "armv7-a", "", []string{"armeabi-v7a"}},
Tamas Petzbca786d2021-01-20 18:56:33 +01001445 {"arm64", "armv8-a-branchprot", "", []string{"arm64-v8a"}},
Dan Albert4098deb2016-10-19 14:04:41 -07001446 {"x86", "", "", []string{"x86"}},
1447 {"x86_64", "", "", []string{"x86_64"}},
1448 }
1449}
1450
Colin Crossa6845402020-11-16 15:08:19 -08001451// getAmlAbisConfig returns a list of archConfigs for the ABIs supported by mainline modules.
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001452func getAmlAbisConfig() []archConfig {
1453 return []archConfig{
Martin Stjernholm93688342020-10-16 21:45:10 +01001454 {"arm", "armv7-a-neon", "", []string{"armeabi-v7a"}},
Martin Stjernholmc1ecc432019-11-15 15:00:31 +00001455 {"arm64", "armv8-a", "", []string{"arm64-v8a"}},
1456 {"x86", "", "", []string{"x86"}},
1457 {"x86_64", "", "", []string{"x86_64"}},
1458 }
1459}
1460
Colin Crossa6845402020-11-16 15:08:19 -08001461// decodeArchSettings converts a list of archConfigs into a list of Targets for the given OsType.
Dan Willemsen01a3c252019-01-11 19:02:16 -08001462func decodeArchSettings(os OsType, archConfigs []archConfig) ([]Target, error) {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001463 var ret []Target
Dan Willemsen322acaf2016-01-12 23:07:05 -08001464
Dan Albert4098deb2016-10-19 14:04:41 -07001465 for _, config := range archConfigs {
Dan Willemsen01a3c252019-01-11 19:02:16 -08001466 arch, err := decodeArch(os, config.arch, &config.archVariant,
Colin Crossa74ca042019-01-31 14:31:51 -08001467 &config.cpuVariant, config.abi)
Dan Willemsen322acaf2016-01-12 23:07:05 -08001468 if err != nil {
1469 return nil, err
1470 }
Colin Cross3b19f5d2019-09-17 14:45:31 -07001471
Colin Crossa1ad8d12016-06-01 17:09:44 -07001472 ret = append(ret, Target{
1473 Os: Android,
1474 Arch: arch,
1475 })
Dan Willemsen322acaf2016-01-12 23:07:05 -08001476 }
1477
1478 return ret, nil
1479}
1480
Colin Crossa6845402020-11-16 15:08:19 -08001481// decodeArch converts a set of strings from product variables into an Arch struct.
Colin Crossa74ca042019-01-31 14:31:51 -08001482func decodeArch(os OsType, arch string, archVariant, cpuVariant *string, abi []string) (Arch, error) {
Colin Crossa6845402020-11-16 15:08:19 -08001483 // Verify the arch is valid
Colin Crosseeabb892015-11-20 13:07:51 -08001484 archType, ok := archTypeMap[arch]
1485 if !ok {
1486 return Arch{}, fmt.Errorf("unknown arch %q", arch)
1487 }
Colin Cross4225f652015-09-17 14:33:42 -07001488
Colin Crosseeabb892015-11-20 13:07:51 -08001489 a := Arch{
Colin Cross4225f652015-09-17 14:33:42 -07001490 ArchType: archType,
Colin Crossa6845402020-11-16 15:08:19 -08001491 ArchVariant: String(archVariant),
1492 CpuVariant: String(cpuVariant),
Colin Crossa74ca042019-01-31 14:31:51 -08001493 Abi: abi,
Colin Crosseeabb892015-11-20 13:07:51 -08001494 }
1495
Colin Crossa6845402020-11-16 15:08:19 -08001496 // Convert generic arch variants into the empty string.
Colin Crosseeabb892015-11-20 13:07:51 -08001497 if a.ArchVariant == a.ArchType.Name || a.ArchVariant == "generic" {
1498 a.ArchVariant = ""
1499 }
1500
Colin Crossa6845402020-11-16 15:08:19 -08001501 // Convert generic CPU variants into the empty string.
Colin Crosseeabb892015-11-20 13:07:51 -08001502 if a.CpuVariant == a.ArchType.Name || a.CpuVariant == "generic" {
1503 a.CpuVariant = ""
1504 }
1505
Colin Crossa6845402020-11-16 15:08:19 -08001506 // Filter empty ABIs out of the list.
Colin Crosseeabb892015-11-20 13:07:51 -08001507 for i := 0; i < len(a.Abi); i++ {
1508 if a.Abi[i] == "" {
1509 a.Abi = append(a.Abi[:i], a.Abi[i+1:]...)
1510 i--
1511 }
1512 }
1513
Dan Willemsen01a3c252019-01-11 19:02:16 -08001514 if a.ArchVariant == "" {
Colin Crossa6845402020-11-16 15:08:19 -08001515 // Set ArchFeatures from the default arch features.
Dan Willemsen01a3c252019-01-11 19:02:16 -08001516 if featureMap, ok := defaultArchFeatureMap[os]; ok {
1517 a.ArchFeatures = featureMap[archType]
1518 }
1519 } else {
Colin Crossa6845402020-11-16 15:08:19 -08001520 // Set ArchFeatures from the arch type.
Dan Willemsen01a3c252019-01-11 19:02:16 -08001521 if featureMap, ok := archFeatureMap[archType]; ok {
1522 a.ArchFeatures = featureMap[a.ArchVariant]
1523 }
Colin Crossc5c24ad2015-11-20 15:35:00 -08001524 }
1525
Colin Crosseeabb892015-11-20 13:07:51 -08001526 return a, nil
Colin Cross4225f652015-09-17 14:33:42 -07001527}
1528
Colin Crossa6845402020-11-16 15:08:19 -08001529// filterMultilibTargets takes a list of Targets and a multilib value and returns a new list of
1530// Targets containing only those that have the given multilib value.
Colin Cross69617d32016-09-06 10:39:07 -07001531func filterMultilibTargets(targets []Target, multilib string) []Target {
1532 var ret []Target
1533 for _, t := range targets {
1534 if t.Arch.ArchType.Multilib == multilib {
1535 ret = append(ret, t)
1536 }
1537 }
1538 return ret
1539}
1540
Colin Crossa6845402020-11-16 15:08:19 -08001541// getCommonTargets returns the set of Os specific common architecture targets for each Os in a list
1542// of targets.
Nan Zhangdb0b9a32017-02-27 10:12:13 -08001543func getCommonTargets(targets []Target) []Target {
1544 var ret []Target
1545 set := make(map[string]bool)
1546
1547 for _, t := range targets {
1548 if _, found := set[t.Os.String()]; !found {
1549 set[t.Os.String()] = true
1550 ret = append(ret, commonTargetMap[t.Os.String()])
1551 }
1552 }
1553
1554 return ret
1555}
1556
Colin Crossa6845402020-11-16 15:08:19 -08001557// firstTarget takes a list of Targets and a list of multilib values and returns a list of Targets
1558// that contains zero or one Target for each OsType, selecting the one that matches the earliest
1559// filter.
Colin Cross3dceee32018-09-06 10:19:57 -07001560func firstTarget(targets []Target, filters ...string) []Target {
Jiyong Park22101982020-09-17 19:09:58 +09001561 // find the first target from each OS
1562 var ret []Target
1563 hasHost := false
1564 set := make(map[OsType]bool)
1565
Colin Cross6b4a32d2017-12-05 13:42:45 -08001566 for _, filter := range filters {
1567 buildTargets := filterMultilibTargets(targets, filter)
Jiyong Park22101982020-09-17 19:09:58 +09001568 for _, t := range buildTargets {
1569 if _, found := set[t.Os]; !found {
1570 hasHost = hasHost || (t.Os.Class == Host)
1571 set[t.Os] = true
1572 ret = append(ret, t)
1573 }
Colin Cross6b4a32d2017-12-05 13:42:45 -08001574 }
1575 }
Jiyong Park22101982020-09-17 19:09:58 +09001576 return ret
Colin Cross6b4a32d2017-12-05 13:42:45 -08001577}
1578
Colin Crossa6845402020-11-16 15:08:19 -08001579// decodeMultilibTargets uses the module's multilib setting to select one or more targets from a
1580// list of Targets.
Colin Crossee0bc3b2018-10-02 22:01:37 -07001581func decodeMultilibTargets(multilib string, targets []Target, prefer32 bool) ([]Target, error) {
Colin Crossa6845402020-11-16 15:08:19 -08001582 var buildTargets []Target
Colin Cross6b4a32d2017-12-05 13:42:45 -08001583
Colin Cross4225f652015-09-17 14:33:42 -07001584 switch multilib {
1585 case "common":
Colin Cross6b4a32d2017-12-05 13:42:45 -08001586 buildTargets = getCommonTargets(targets)
1587 case "common_first":
1588 buildTargets = getCommonTargets(targets)
1589 if prefer32 {
Colin Cross3dceee32018-09-06 10:19:57 -07001590 buildTargets = append(buildTargets, firstTarget(targets, "lib32", "lib64")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001591 } else {
Colin Cross3dceee32018-09-06 10:19:57 -07001592 buildTargets = append(buildTargets, firstTarget(targets, "lib64", "lib32")...)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001593 }
Colin Cross4225f652015-09-17 14:33:42 -07001594 case "both":
Colin Cross8b74d172016-09-13 09:59:14 -07001595 if prefer32 {
1596 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1597 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1598 } else {
1599 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
1600 buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
1601 }
Colin Cross4225f652015-09-17 14:33:42 -07001602 case "32":
Colin Cross69617d32016-09-06 10:39:07 -07001603 buildTargets = filterMultilibTargets(targets, "lib32")
Colin Cross4225f652015-09-17 14:33:42 -07001604 case "64":
Colin Cross69617d32016-09-06 10:39:07 -07001605 buildTargets = filterMultilibTargets(targets, "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001606 case "first":
1607 if prefer32 {
Colin Cross3dceee32018-09-06 10:19:57 -07001608 buildTargets = firstTarget(targets, "lib32", "lib64")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001609 } else {
Colin Cross3dceee32018-09-06 10:19:57 -07001610 buildTargets = firstTarget(targets, "lib64", "lib32")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001611 }
Victor Chang9448e8f2020-09-14 15:34:16 +01001612 case "first_prefer32":
1613 buildTargets = firstTarget(targets, "lib32", "lib64")
Colin Cross69617d32016-09-06 10:39:07 -07001614 case "prefer32":
Colin Cross3dceee32018-09-06 10:19:57 -07001615 buildTargets = filterMultilibTargets(targets, "lib32")
1616 if len(buildTargets) == 0 {
1617 buildTargets = filterMultilibTargets(targets, "lib64")
1618 }
Colin Cross4225f652015-09-17 14:33:42 -07001619 default:
Victor Chang9448e8f2020-09-14 15:34:16 +01001620 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 -07001621 multilib)
Colin Cross4225f652015-09-17 14:33:42 -07001622 }
1623
Colin Crossa1ad8d12016-06-01 17:09:44 -07001624 return buildTargets, nil
Colin Cross4225f652015-09-17 14:33:42 -07001625}