Make FirstTarget treat HostCross separately from Host
Use Target.Os and Target.HostCross as the key in FirstTarget so that
it returns a separate target for host and host cross architectures.
This is useful when host and host cross are both linux_musl, but
host cross is an independenct architecture like arm64.
Also filter the targets returned by ctx.MultiTargets() to match
the HostCross value of ctx.Target() to prevent the newly created
HostCross variants from colliding with Host variants in JNI or
test data attached to Java targets using a common arch.
This relands If75790001afe9d0f9d4d8166f207847851812297 with the
addition of the ctx.MultiTargets() filtering.
Bug: 236052820
Test: TestArchMutator
Change-Id: Ia6fe1185915d174d0ad6b401c227e0e57bee5c24
diff --git a/android/arch.go b/android/arch.go
index b5bd2f0..809b2c6 100644
--- a/android/arch.go
+++ b/android/arch.go
@@ -616,6 +616,12 @@
mctx.ModuleErrorf("%s", err.Error())
}
+ // If there are no supported targets disable the module.
+ if len(targets) == 0 {
+ base.Disable()
+ return
+ }
+
// If the module is using extraMultilib, decode the extraMultilib selection into
// a separate list of Targets.
var multiTargets []Target
@@ -624,6 +630,7 @@
if err != nil {
mctx.ModuleErrorf("%s", err.Error())
}
+ multiTargets = filterHostCross(multiTargets, targets[0].HostCross)
}
// Recovery is always the primary architecture, filter out any other architectures.
@@ -760,6 +767,18 @@
return targets
}
+// filterHostCross takes a list of Targets and a hostCross value, and returns a modified list
+// that contains only Targets that have the specified HostCross.
+func filterHostCross(targets []Target, hostCross bool) []Target {
+ for i := 0; i < len(targets); i++ {
+ if targets[i].HostCross != hostCross {
+ targets = append(targets[:i], targets[i+1:]...)
+ i--
+ }
+ }
+ return targets
+}
+
// archPropRoot is a struct type used as the top level of the arch-specific properties. It
// contains the "arch", "multilib", and "target" property structs. It is used to split up the
// property structs to limit how much is allocated when a single arch-specific property group is
@@ -1789,20 +1808,23 @@
}
// FirstTarget takes a list of Targets and a list of multilib values and returns a list of Targets
-// that contains zero or one Target for each OsType, selecting the one that matches the earliest
-// filter.
+// that contains zero or one Target for each OsType and HostCross, selecting the one that matches
+// the earliest filter.
func FirstTarget(targets []Target, filters ...string) []Target {
// find the first target from each OS
var ret []Target
- hasHost := false
- set := make(map[OsType]bool)
+ type osHostCross struct {
+ os OsType
+ hostCross bool
+ }
+ set := make(map[osHostCross]bool)
for _, filter := range filters {
buildTargets := filterMultilibTargets(targets, filter)
for _, t := range buildTargets {
- if _, found := set[t.Os]; !found {
- hasHost = hasHost || (t.Os.Class == Host)
- set[t.Os] = true
+ key := osHostCross{t.Os, t.HostCross}
+ if _, found := set[key]; !found {
+ set[key] = true
ret = append(ret, t)
}
}