zig/lib/compiler/aro/aro/Driver/Multilib.zig
Andrew Kelley 240d0b68f6 make aro-based translate-c lazily built from source
Part of #19063.

Primarily, this moves Aro from deps/ to lib/compiler/ so that it can be
lazily compiled from source. src/aro_translate_c.zig is moved to
lib/compiler/aro_translate_c.zig and some of Zig CLI logic moved to a
main() function there.

aro_translate_c.zig becomes the "common" import for clang-based
translate-c.

Not all of the compiler was able to be detangled from Aro, however, so
it still, for now, remains being compiled with the main compiler
sources due to the clang-based translate-c depending on it. Once
aro-based translate-c achieves feature parity with the clang-based
translate-c implementation, the clang-based one can be removed from Zig.

Aro made it unnecessarily difficult to depend on with these .def files
and all these Zig module requirements. I looked at the .def files and
made these observations:

- The canonical source is llvm .def files.
- Therefore there is an update process to sync with llvm that involves
  regenerating the .def files in Aro.
- Therefore you might as well just regenerate the .zig files directly
  and check those into Aro.
- Also with a small amount of tinkering, the file size on disk of these
  generated .zig files can be made many times smaller, without
  compromising type safety in the usage of the data.

This would make things much easier on Zig as downstream project,
particularly we could remove those pesky stubs when bootstrapping.

I have gone ahead with these changes since they unblock me and I will
have a chat with Vexu to see what he thinks.
2024-02-28 13:21:05 -07:00

71 lines
2.3 KiB
Zig

const std = @import("std");
const Filesystem = @import("Filesystem.zig").Filesystem;
pub const Flags = std.BoundedArray([]const u8, 6);
/// Large enough for GCCDetector for Linux; may need to be increased to support other toolchains.
const max_multilibs = 4;
const MultilibArray = std.BoundedArray(Multilib, max_multilibs);
pub const Detected = struct {
multilibs: MultilibArray = .{},
selected: Multilib = .{},
biarch_sibling: ?Multilib = null,
pub fn filter(self: *Detected, multilib_filter: Filter, fs: Filesystem) void {
var found_count: usize = 0;
for (self.multilibs.constSlice()) |multilib| {
if (multilib_filter.exists(multilib, fs)) {
self.multilibs.set(found_count, multilib);
found_count += 1;
}
}
self.multilibs.resize(found_count) catch unreachable;
}
pub fn select(self: *Detected, flags: Flags) !bool {
var filtered: MultilibArray = .{};
for (self.multilibs.constSlice()) |multilib| {
for (multilib.flags.constSlice()) |multilib_flag| {
const matched = for (flags.constSlice()) |arg_flag| {
if (std.mem.eql(u8, arg_flag[1..], multilib_flag[1..])) break arg_flag;
} else multilib_flag;
if (matched[0] != multilib_flag[0]) break;
} else {
filtered.appendAssumeCapacity(multilib);
}
}
if (filtered.len == 0) return false;
if (filtered.len == 1) {
self.selected = filtered.get(0);
return true;
}
return error.TooManyMultilibs;
}
};
pub const Filter = struct {
base: [2][]const u8,
file: []const u8,
pub fn exists(self: Filter, m: Multilib, fs: Filesystem) bool {
return fs.joinedExists(&.{ self.base[0], self.base[1], m.gcc_suffix, self.file });
}
};
const Multilib = @This();
gcc_suffix: []const u8 = "",
os_suffix: []const u8 = "",
include_suffix: []const u8 = "",
flags: Flags = .{},
priority: u32 = 0,
pub fn init(gcc_suffix: []const u8, os_suffix: []const u8, flags: []const []const u8) Multilib {
var self: Multilib = .{
.gcc_suffix = gcc_suffix,
.os_suffix = os_suffix,
};
self.flags.appendSliceAssumeCapacity(flags);
return self;
}