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, cpp,
}; };
fn compileMusl(context: *const Context) MuslContext{ fn compileMusl(context: *const Context) MuslContext {
const musl = try MuslContext.init(context); const musl = try MuslContext.init(context);
var exists = true; var exists = true;
var dir = std.fs.cwd().openDir(musl.global_cache_dir, .{}) catch b: { 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 _ = kind; // autofix
var argument_index: usize = 0; var argument_index: usize = 0;
_ = &argument_index; _ = &argument_index;
const Mode = enum{ const Mode = enum {
object, object,
link, link,
}; };
var out_path: ?[]const u8 = null; var out_path: ?[]const u8 = null;
var out_mode: ?Mode = null; var out_mode: ?Mode = null;
const Extension = enum{ const Extension = enum {
c, c,
cpp, cpp,
assembly, assembly,
@ -286,15 +286,15 @@ pub fn compileCSourceFile(context: *const Context, arguments: []const []const u8
static_library, static_library,
shared_library, shared_library,
}; };
const CSourceFile = struct{ const CSourceFile = struct {
path: []const u8, path: []const u8,
extension: Extension, extension: Extension,
}; };
const DebugInfo = enum{ const DebugInfo = enum {
yes, yes,
no, no,
}; };
const LinkArch = enum{ const LinkArch = enum {
arm64, arm64,
}; };
var debug_info: ?DebugInfo = null; var debug_info: ?DebugInfo = null;
@ -314,21 +314,14 @@ pub fn compileCSourceFile(context: *const Context, arguments: []const []const u8
if (data_structures.last_byte(argument, '.')) |dot_index| { if (data_structures.last_byte(argument, '.')) |dot_index| {
const extension_string = argument[dot_index..]; const extension_string = argument[dot_index..];
const extension: Extension = const extension: Extension =
if (byte_equal(extension_string, ".c")) .c 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
else if (byte_equal(extension_string, ".cpp") or byte_equal(extension_string, ".cxx") or byte_equal(extension_string, ".cc")) .cpp byte_equal(extension_string, ".dll") or
else if (byte_equal(extension_string, ".S")) .assembly byte_equal(extension_string, ".dylib") or
else if (byte_equal(extension_string, ".o")) .object byte_equal(extension_string, ".tbd")) .shared_library else {
else if (byte_equal(extension_string, ".a")) .static_library try write(.panic, argument);
else if (byte_equal(extension_string, ".so") or try write(.panic, "\n");
byte_equal(extension_string, ".dll") or @panic("Unable to recognize extension for the file above");
byte_equal(extension_string, ".dylib") or };
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");
};
switch (extension) { switch (extension) {
.c, .cpp, .assembly => { .c, .cpp, .assembly => {
try c_source_files.append(context.my_allocator, .{ try c_source_files.append(context.my_allocator, .{
@ -464,7 +457,7 @@ pub fn compileCSourceFile(context: *const Context, arguments: []const []const u8
const wl_arg = argument["-Wl,".len..]; const wl_arg = argument["-Wl,".len..];
if (data_structures.first_byte(wl_arg, ',')) |comma_index| { if (data_structures.first_byte(wl_arg, ',')) |comma_index| {
const key = wl_arg[0..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, key);
try ld_argv.append(context.my_allocator, value); try ld_argv.append(context.my_allocator, value);
} else { } 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_slice(context.my_allocator, argument);
try list.append(context.my_allocator, '\n'); 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, "unhandled argument: '");
try write(.panic, 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"); try argv.append(context.my_allocator, "-fno-builtin");
} }
if (link_libcpp) { if (link_libcpp) {
// include paths // include paths
} }
const link_libc = c_source_file.extension == .c; const link_libc = c_source_file.extension == .c;
if (link_libc) { if (link_libc) {}
}
const link_libunwind = false; const link_libunwind = false;
if (link_libunwind) { if (link_libunwind) {
@ -592,11 +583,11 @@ pub fn compileCSourceFile(context: *const Context, arguments: []const []const u8
break :blk target_triple_buffer.slice(); 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) { const object_path = switch (mode) {
.object => out_path.?, .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, .{ try link_objects.append(context.my_allocator, .{
@ -623,12 +614,12 @@ pub fn compileCSourceFile(context: *const Context, arguments: []const []const u8
else => &.{}, else => &.{},
}; };
for (libc_framework_dirs) |framework_dir| { 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{}; const framework_dirs = &[_][]const u8{};
for (framework_dirs) |framework_dir| { 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 // TODO: c headers dir
@ -653,7 +644,7 @@ pub fn compileCSourceFile(context: *const Context, arguments: []const []const u8
}; };
for (libc_include_dirs) |include_dir| { 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 // TODO: cpu model
@ -727,7 +718,7 @@ pub fn compileCSourceFile(context: *const Context, arguments: []const []const u8
// TODO: extra flags // TODO: extra flags
// TODO: cache exempt 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 // TODO: emit ASM/LLVM IR
const debug_clang_args = false; const debug_clang_args = false;
@ -15669,7 +15660,7 @@ pub const Unit = struct {
o_file, 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); try compileCSourceFile(context, &arguments, .c);
object_files.append_with_capacity(.{ object_files.append_with_capacity(.{
.path = object_path, .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 { pub fn ends_with_slice(bytes: []const u8, slice: []const u8) bool {
if (slice.len <= bytes.len) { 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; return true;
} }
} }
@ -749,7 +749,7 @@ pub fn first_byte(bytes: []const u8, byte: u8) ?usize {
if (b == byte) { if (b == byte) {
return i; return i;
} }
} }
return null; return null;
} }

View File

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

View File

@ -59,7 +59,7 @@ pub fn link(context: *const Compilation.Context, options: linker.Options) !void
.linux => { .linux => {
if (options.link_libcpp) { if (options.link_libcpp) {
assert(options.link_libc); 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) { if (options.link_libc) {
@ -80,9 +80,8 @@ pub fn link(context: *const Compilation.Context, options: linker.Options) !void
else => @compileError("OS not supported"), else => @compileError("OS not supported"),
} }
for (options.libraries) |lib| { 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()); 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_ptr: [*]const u8 = undefined;
var stderr_len: usize = 0; var stderr_len: usize = 0;
const result = switch (@import("builtin").os.tag) { 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), .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), .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), .windows => NativityLLDLinkCOFF(argv_zero_terminated.ptr, argv_zero_terminated.len, &stdout_ptr, &stdout_len, &stderr_ptr, &stderr_len),
else => @compileError("OS not supported"), else => @compileError("OS not supported"),
}; };

View File

@ -66,8 +66,8 @@ pub fn main() !void {
argument_list.appendSlice(arg) catch {}; argument_list.appendSlice(arg) catch {};
argument_list.append(' ') catch {}; argument_list.append(' ') catch {};
} }
argument_list.append('\n') 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) { if (arguments.len <= 1) {

View File

@ -136,7 +136,7 @@ fn runStandaloneTests(allocator: Allocator) !void {
.allocator = allocator, .allocator = allocator,
// TODO: delete -main_source_file? // TODO: delete -main_source_file?
.argv = &.{ "zig-out/bin/nat", "exe", "-main_source_file", source_file_path }, .argv = &.{ "zig-out/bin/nat", "exe", "-main_source_file", source_file_path },
.max_output_bytes = std.math.maxInt(u64), .max_output_bytes = std.math.maxInt(u64),
}); });
ran_compilation_count += 1; ran_compilation_count += 1;
@ -225,7 +225,7 @@ fn runBuildTests(allocator: Allocator) !void {
.allocator = allocator, .allocator = allocator,
// TODO: delete -main_source_file? // TODO: delete -main_source_file?
.argv = &.{ compiler_realpath, "build" }, .argv = &.{ compiler_realpath, "build" },
.max_output_bytes = std.math.maxInt(u64), .max_output_bytes = std.math.maxInt(u64),
}); });
ran_compilation_count += 1; ran_compilation_count += 1;
@ -413,9 +413,7 @@ fn runCmakeTests(allocator: Allocator, dir_path: []const u8) !void {
const ninja = try std.ChildProcess.run(.{ const ninja = try std.ChildProcess.run(.{
.allocator = allocator, .allocator = allocator,
// TODO: delete -main_source_file? // TODO: delete -main_source_file?
.argv = &.{ .argv = &.{"ninja"},
"ninja"
},
.max_output_bytes = std.math.maxInt(u64), .max_output_bytes = std.math.maxInt(u64),
}); });
const ninja_result: TestError!bool = switch (ninja.term) { const ninja_result: TestError!bool = switch (ninja.term) {
@ -446,7 +444,7 @@ fn runCmakeTests(allocator: Allocator, dir_path: []const u8) !void {
.allocator = allocator, .allocator = allocator,
// TODO: delete -main_source_file? // TODO: delete -main_source_file?
.argv = &.{ .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), .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"}); 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); try std.posix.chdir(cc_dir_path);