pub fn addTargetQuery(wip: *Wip, q: *const std.Target.Query) !TargetQuery.OptionalIndex {
if (q.isNative()) return .none;
const gpa = wip.gpa;
const cpu_name: ?String = switch (q.cpu_model) {
.native, .baseline, .determined_by_arch_os => null,
.explicit => |model| try wip.addString(model.name),
};
const os_version_min: TargetQuery.OsVersion = if (q.os_version_min) |ver| switch (ver) {
.none => .none,
.semver => |sem_ver| .{ .semver = try wip.addSemVer(sem_ver) },
.windows => |win_ver| .{ .windows = win_ver },
} else .default;
const os_version_max: TargetQuery.OsVersion = if (q.os_version_max) |ver| switch (ver) {
.none => .none,
.semver => |sem_ver| .{ .semver = try wip.addSemVer(sem_ver) },
.windows => |win_ver| .{ .windows = win_ver },
} else .default;
const glibc_version: ?String = if (q.glibc_version) |sem_ver| try wip.addSemVer(sem_ver) else null;
const dynamic_linker: ?String = if (q.dynamic_linker) |*dl|
if (dl.get()) |s| try wip.addString(s) else .empty
else
null;
const cpu_features_add_empty = q.cpu_features_add.isEmpty();
const cpu_features_sub_empty = q.cpu_features_sub.isEmpty();
const result_index: TargetQuery.Index = try wip.addExtra(TargetQuery, .{
.flags = .{
.cpu_arch = .init(q.cpu_arch),
.cpu_model = .init(q.cpu_model),
.cpu_features_add = !cpu_features_add_empty,
.cpu_features_sub = !cpu_features_sub_empty,
.os_tag = .init(q.os_tag),
.abi = .init(q.abi),
.object_format = .init(q.ofmt),
.os_version_min = os_version_min,
.os_version_max = os_version_max,
.glibc_version = glibc_version != null,
.android_api_level = q.android_api_level != null,
.dynamic_linker = dynamic_linker != null,
},
.cpu_features_add = .{ .value = if (cpu_features_add_empty) null else q.cpu_features_add },
.cpu_features_sub = .{ .value = if (cpu_features_sub_empty) null else q.cpu_features_sub },
.glibc_version = .{ .value = glibc_version },
.android_api_level = .{ .value = q.android_api_level },
.dynamic_linker = .{ .value = dynamic_linker },
.cpu_name = .{ .value = cpu_name },
.os_version_min = .{ .u = os_version_min },
.os_version_max = .{ .u = os_version_max },
});
// Deduplicate.
const gop = try wip.targets_table.getOrPutContext(gpa, result_index, @as(TargetsTableContext, .{
.extra = wip.extra.items,
}));
if (gop.found_existing) {
wip.extra.items.len = @backingInt(result_index);
return .init(gop.key_ptr.*);
} else {
return .init(result_index);
}
}