This commit is contained in:
David Gonzalez Martin 2024-03-27 23:46:47 -06:00
parent 12fe8d9fd8
commit c1ec772654
6 changed files with 41 additions and 53 deletions

View File

@ -176,7 +176,7 @@ const CSourceKind = enum {
cpp,
};
fn compileMusl(context: *const Context) MuslContext{
fn compileMusl(context: *const Context) MuslContext {
const musl = try MuslContext.init(context);
var exists = true;
var dir = std.fs.cwd().openDir(musl.global_cache_dir, .{}) catch b: {
@ -272,13 +272,13 @@ pub fn compileCSourceFile(context: *const Context, arguments: []const []const u8
_ = kind; // autofix
var argument_index: usize = 0;
_ = &argument_index;
const Mode = enum{
const Mode = enum {
object,
link,
};
var out_path: ?[]const u8 = null;
var out_mode: ?Mode = null;
const Extension = enum{
const Extension = enum {
c,
cpp,
assembly,
@ -286,15 +286,15 @@ pub fn compileCSourceFile(context: *const Context, arguments: []const []const u8
static_library,
shared_library,
};
const CSourceFile = struct{
const CSourceFile = struct {
path: []const u8,
extension: Extension,
};
const DebugInfo = enum{
const DebugInfo = enum {
yes,
no,
};
const LinkArch = enum{
const LinkArch = enum {
arm64,
};
var debug_info: ?DebugInfo = null;
@ -314,17 +314,10 @@ pub fn compileCSourceFile(context: *const Context, arguments: []const []const u8
if (data_structures.last_byte(argument, '.')) |dot_index| {
const extension_string = argument[dot_index..];
const extension: Extension =
if (byte_equal(extension_string, ".c")) .c
else if (byte_equal(extension_string, ".cpp") or byte_equal(extension_string, ".cxx") or byte_equal(extension_string, ".cc")) .cpp
else if (byte_equal(extension_string, ".S")) .assembly
else if (byte_equal(extension_string, ".o")) .object
else if (byte_equal(extension_string, ".a")) .static_library
else if (byte_equal(extension_string, ".so") or
if (byte_equal(extension_string, ".c")) .c else if (byte_equal(extension_string, ".cpp") or byte_equal(extension_string, ".cxx") or byte_equal(extension_string, ".cc")) .cpp else if (byte_equal(extension_string, ".S")) .assembly else if (byte_equal(extension_string, ".o")) .object else if (byte_equal(extension_string, ".a")) .static_library else if (byte_equal(extension_string, ".so") or
byte_equal(extension_string, ".dll") or
byte_equal(extension_string, ".dylib") or
byte_equal(extension_string, ".tbd")
) .shared_library
else {
byte_equal(extension_string, ".tbd")) .shared_library else {
try write(.panic, argument);
try write(.panic, "\n");
@panic("Unable to recognize extension for the file above");
@ -464,7 +457,7 @@ pub fn compileCSourceFile(context: *const Context, arguments: []const []const u8
const wl_arg = argument["-Wl,".len..];
if (data_structures.first_byte(wl_arg, ',')) |comma_index| {
const key = wl_arg[0..comma_index];
const value = wl_arg[comma_index + 1..];
const value = wl_arg[comma_index + 1 ..];
try ld_argv.append(context.my_allocator, key);
try ld_argv.append(context.my_allocator, value);
} else {
@ -486,7 +479,7 @@ pub fn compileCSourceFile(context: *const Context, arguments: []const []const u8
try list.append_slice(context.my_allocator, argument);
try list.append(context.my_allocator, '\n');
try std.fs.cwd().writeFile(try std.fmt.allocPrint(context.allocator, "{s}/dev/nativity/nat/unhandled_arg_{}", .{home_dir, std.time.milliTimestamp()}), list.slice());
try std.fs.cwd().writeFile(try std.fmt.allocPrint(context.allocator, "{s}/dev/nativity/nat/unhandled_arg_{}", .{ home_dir, std.time.milliTimestamp() }), list.slice());
}
try write(.panic, "unhandled argument: '");
try write(.panic, argument);
@ -533,15 +526,13 @@ pub fn compileCSourceFile(context: *const Context, arguments: []const []const u8
try argv.append(context.my_allocator, "-fno-builtin");
}
if (link_libcpp) {
// include paths
}
const link_libc = c_source_file.extension == .c;
if (link_libc) {
}
if (link_libc) {}
const link_libunwind = false;
if (link_libunwind) {
@ -592,11 +583,11 @@ pub fn compileCSourceFile(context: *const Context, arguments: []const []const u8
break :blk target_triple_buffer.slice();
};
try argv.append_slice(context.my_allocator, &.{"-target", target_triple});
try argv.append_slice(context.my_allocator, &.{ "-target", target_triple });
const object_path = switch (mode) {
.object => out_path.?,
.link => try std.mem.concat(context.allocator, u8, &.{if (out_path) |op| op else "a.o", ".o"}),
.link => try std.mem.concat(context.allocator, u8, &.{ if (out_path) |op| op else "a.o", ".o" }),
};
try link_objects.append(context.my_allocator, .{
@ -623,12 +614,12 @@ pub fn compileCSourceFile(context: *const Context, arguments: []const []const u8
else => &.{},
};
for (libc_framework_dirs) |framework_dir| {
try argv.append_slice(context.my_allocator, &.{"-iframework", framework_dir});
try argv.append_slice(context.my_allocator, &.{ "-iframework", framework_dir });
}
const framework_dirs = &[_][]const u8{};
for (framework_dirs) |framework_dir| {
try argv.append_slice(context.my_allocator, &.{"-F", framework_dir});
try argv.append_slice(context.my_allocator, &.{ "-F", framework_dir });
}
// TODO: c headers dir
@ -653,7 +644,7 @@ pub fn compileCSourceFile(context: *const Context, arguments: []const []const u8
};
for (libc_include_dirs) |include_dir| {
try argv.append_slice(context.my_allocator, &.{"-isystem", include_dir});
try argv.append_slice(context.my_allocator, &.{ "-isystem", include_dir });
}
// TODO: cpu model
@ -727,7 +718,7 @@ pub fn compileCSourceFile(context: *const Context, arguments: []const []const u8
// TODO: extra flags
// TODO: cache exempt flags
try argv.append_slice(context.my_allocator, &.{"-c", "-o", object_path});
try argv.append_slice(context.my_allocator, &.{ "-c", "-o", object_path });
// TODO: emit ASM/LLVM IR
const debug_clang_args = false;
@ -15669,7 +15660,7 @@ pub const Unit = struct {
o_file,
});
var arguments = [_][]const u8{ "-c", c_source_file, "-o", object_path, "-g", "-fno-stack-protector"};
var arguments = [_][]const u8{ "-c", c_source_file, "-o", object_path, "-g", "-fno-stack-protector" };
try compileCSourceFile(context, &arguments, .c);
object_files.append_with_capacity(.{
.path = object_path,

View File

@ -736,7 +736,7 @@ pub fn starts_with_slice(bytes: []const u8, slice: []const u8) bool {
pub fn ends_with_slice(bytes: []const u8, slice: []const u8) bool {
if (slice.len <= bytes.len) {
if (byte_equal(bytes[bytes.len - slice.len..], slice)) {
if (byte_equal(bytes[bytes.len - slice.len ..], slice)) {
return true;
}
}

View File

@ -1,7 +1,7 @@
const Compilation = @import("../Compilation.zig");
const Context = Compilation.Context;
const lld = @import("lld.zig");
pub const Options = struct{
pub const Options = struct {
backend: Backend = .lld,
output_file_path: []const u8,
objects: []const Object,
@ -11,15 +11,15 @@ pub const Options = struct{
link_libcpp: bool,
};
const Backend = enum{
const Backend = enum {
lld,
};
pub const Object = struct{
pub const Object = struct {
path: []const u8,
};
pub const Library = struct{
pub const Library = struct {
path: []const u8,
};

View File

@ -59,7 +59,7 @@ pub fn link(context: *const Compilation.Context, options: linker.Options) !void
.linux => {
if (options.link_libcpp) {
assert(options.link_libc);
try argv.append(context.my_allocator, "/usr/lib/libstdc++.so" );
try argv.append(context.my_allocator, "/usr/lib/libstdc++.so");
}
if (options.link_libc) {
@ -80,9 +80,8 @@ pub fn link(context: *const Compilation.Context, options: linker.Options) !void
else => @compileError("OS not supported"),
}
for (options.libraries) |lib| {
try argv.append(context.my_allocator, try std.mem.concat(context.allocator, u8, &.{"-l", lib.path}));
try argv.append(context.my_allocator, try std.mem.concat(context.allocator, u8, &.{ "-l", lib.path }));
}
const argv_zero_terminated = try Compilation.argsCopyZ(context.allocator, argv.slice());
@ -92,8 +91,8 @@ pub fn link(context: *const Compilation.Context, options: linker.Options) !void
var stderr_ptr: [*]const u8 = undefined;
var stderr_len: usize = 0;
const result = switch (@import("builtin").os.tag) {
.linux => NativityLLDLinkELF (argv_zero_terminated.ptr, argv_zero_terminated.len, &stdout_ptr, &stdout_len, &stderr_ptr, &stderr_len),
.macos => NativityLLDLinkMachO (argv_zero_terminated.ptr, argv_zero_terminated.len, &stdout_ptr, &stdout_len, &stderr_ptr, &stderr_len),
.linux => NativityLLDLinkELF(argv_zero_terminated.ptr, argv_zero_terminated.len, &stdout_ptr, &stdout_len, &stderr_ptr, &stderr_len),
.macos => NativityLLDLinkMachO(argv_zero_terminated.ptr, argv_zero_terminated.len, &stdout_ptr, &stdout_len, &stderr_ptr, &stderr_len),
.windows => NativityLLDLinkCOFF(argv_zero_terminated.ptr, argv_zero_terminated.len, &stdout_ptr, &stdout_len, &stderr_ptr, &stderr_len),
else => @compileError("OS not supported"),
};

View File

@ -67,7 +67,7 @@ pub fn main() !void {
argument_list.append(' ') catch {};
}
argument_list.append('\n') catch {};
std.fs.cwd().writeFile(std.fmt.allocPrint(std.heap.page_allocator, "{s}/dev/nativity/nat/invocation_log_{}", .{home_dir, timestamp}) catch unreachable, argument_list.items) catch {};
std.fs.cwd().writeFile(std.fmt.allocPrint(std.heap.page_allocator, "{s}/dev/nativity/nat/invocation_log_{}", .{ home_dir, timestamp }) catch unreachable, argument_list.items) catch {};
}
if (arguments.len <= 1) {

View File

@ -413,9 +413,7 @@ fn runCmakeTests(allocator: Allocator, dir_path: []const u8) !void {
const ninja = try std.ChildProcess.run(.{
.allocator = allocator,
// TODO: delete -main_source_file?
.argv = &.{
"ninja"
},
.argv = &.{"ninja"},
.max_output_bytes = std.math.maxInt(u64),
});
const ninja_result: TestError!bool = switch (ninja.term) {
@ -446,7 +444,7 @@ fn runCmakeTests(allocator: Allocator, dir_path: []const u8) !void {
.allocator = allocator,
// TODO: delete -main_source_file?
.argv = &.{
try std.mem.concat(allocator, u8, &.{ "./", cc_entry.name}),
try std.mem.concat(allocator, u8, &.{ "./", cc_entry.name }),
},
.max_output_bytes = std.math.maxInt(u64),
});
@ -475,7 +473,7 @@ fn runCmakeTests(allocator: Allocator, dir_path: []const u8) !void {
std.debug.print("[TEST {s}]\n", .{if (success) "\x1b[32mOK\x1b[0m" else "\x1b[31mFAILED\x1b[0m"});
},
else => std.debug.panic("Entry {s} is a {s}", .{cc_entry.name, @tagName(cc_entry.kind)}),
else => std.debug.panic("Entry {s} is a {s}", .{ cc_entry.name, @tagName(cc_entry.kind) }),
}
try std.posix.chdir(cc_dir_path);