implement name for executables

This commit is contained in:
David Gonzalez Martin 2023-12-19 20:23:43 +01:00
parent 76332f5ad7
commit 070baf4599
8 changed files with 62 additions and 6 deletions

View File

@ -41,6 +41,7 @@ const installation_dir_name = "installation";
const ArgumentParsingError = error{
main_package_path_not_specified,
main_source_file_not_found,
};
fn reportUnterminatedArgumentError(string: []const u8) noreturn {
@ -57,6 +58,7 @@ fn parseArguments(compilation: *const Compilation) !Compilation.Module.Descripto
var should_transpile_to_c: ?bool = null;
var maybe_only_parse: ?bool = null;
var link_libc = false;
var maybe_executable_name: ?[]const u8 = null;
if (arguments.len == 0) {
// foo
@ -165,8 +167,26 @@ fn parseArguments(compilation: *const Compilation) !Compilation.Module.Descripto
} else {
reportUnterminatedArgumentError(current_argument);
}
} else if (equal(u8, current_argument, "-main_source_file")) {
if (i + 1 != arguments.len) {
i += 1;
const arg = arguments[i];
maybe_main_package_path = arg;
} else {
reportUnterminatedArgumentError(current_argument);
}
} else if (equal(u8, current_argument, "-name")) {
if (i + 1 != arguments.len) {
i += 1;
const arg = arguments[i];
maybe_executable_name = arg;
} else {
reportUnterminatedArgumentError(current_argument);
}
} else {
maybe_main_package_path = current_argument;
std.debug.panic("Unrecognized argument: {s}", .{current_argument});
}
}
}
@ -177,7 +197,12 @@ fn parseArguments(compilation: *const Compilation) !Compilation.Module.Descripto
const only_parse = maybe_only_parse orelse false;
var is_build = false;
const main_package_path = if (maybe_main_package_path) |path| path else blk: {
const main_package_path = if (maybe_main_package_path) |path| blk: {
const file = std.fs.cwd().openFile(path, .{}) catch return error.main_source_file_not_found;
file.close();
break :blk path;
} else blk: {
const build_file = "build.nat";
const file = std.fs.cwd().openFile(build_file, .{}) catch return error.main_package_path_not_specified;
file.close();
@ -187,7 +212,12 @@ fn parseArguments(compilation: *const Compilation) !Compilation.Module.Descripto
};
const executable_path = maybe_executable_path orelse blk: {
const executable_name = if (is_build) "build" else std.fs.path.basename(main_package_path[0 .. main_package_path.len - "/main.nat".len]);
const executable_name = if (is_build) b: {
assert(maybe_executable_name == null);
break :b "build";
} else b: {
break :b if (maybe_executable_name) |name| name else std.fs.path.basename(main_package_path[0 .. main_package_path.len - "/main.nat".len]);
};
assert(executable_name.len > 0);
const result = try std.mem.concat(allocator, u8, &.{ "nat/", executable_name });
break :blk result;

4
ci.sh
View File

@ -26,7 +26,7 @@ nat_compiler=$my_current_directory/zig-out/bin/nat
for standalone_test_case in $standalone_test_directory_files
do
STANDALONE_TEST_NAME=${standalone_test_case##*/}
$nat_compiler $standalone_test_case/main.nat
$nat_compiler -main_source_file $standalone_test_case/main.nat
if [[ "$?" == "0" ]]; then
passed_compilation_count=$(($passed_compilation_count + 1))
@ -66,7 +66,7 @@ do
if [[ "$?" == "0" ]]; then
passed_compilation_count=$(($passed_compilation_count + 1))
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
nat/src
nat/$MY_TESTNAME
if [[ "$?" == "0" ]]; then
passed_test_count=$(($passed_test_count + 1))

View File

@ -10,6 +10,7 @@ const main = fn () s32 {
.abi = .gnu,
},
.main_source_path = "src/main.nat",
.name = "exe",
};
if (executable.compile()) {

View File

@ -7,6 +7,7 @@ const Executable = struct{
target: Target,
main_source_path: [:0]const u8,
link_libc: bool = false,
name: []const u8,
const compile = fn(executable: Executable) bool {
const argument_count = std.start.argument_count;
@ -21,7 +22,7 @@ const Executable = struct{
const compile_with_compiler_path = fn(executable: Executable, compiler_path: [&:0]const u8) bool {
if (std.os.duplicate_process()) |pid| {
if (pid == 0) {
const argv = [_:null] ?[&:0]const u8{ compiler_path, #cast(executable.main_source_path.ptr), "-link_libc", if (executable.link_libc) "true" else "false"};
const argv = [_:null] ?[&:0]const u8{ compiler_path, "-main_source_file", #cast(executable.main_source_path.ptr), "-link_libc", if (executable.link_libc) "true" else "false", "-name", #cast(executable.name.ptr) };
std.os.execute(path = compiler_path, argv = argv.&, env = std.start.environment_values);
return true;
} else {

View File

@ -352,6 +352,23 @@ const waitpid = fn(pid: Process.Id, flags: u32) ?u32 {
}
const memfd_create = fn(name: [&:0]const u8, flags: u32) ?FileDescriptor{
switch (current) {
.linux => {
if (linux.unwrapSyscall(syscall_result = linux.memfd_create(path, flags))) |raw_result| {
const file_descriptor = FileDescriptor{
.handle = #cast(raw_result),
};
return file_descriptor;
} else {
return null;
}
},
else => #error("OS not supported"),
}
}
const IoChannelBehavior = enum{
pipe,
close,

View File

@ -485,6 +485,11 @@ const poll = fn(file_descriptors: [&]PollFileDescriptor, file_descriptor_count:
return result;
}
const memfd_create = fn(name: [&:0]const u8, flags: u32) usize {
const result = #syscall(#cast(Syscall.memfd_create), flags);
return result;
}
const unwrapSyscall = fn(syscall_result: usize) ?usize {
const signed_syscall_result: ssize = #cast(syscall_result);
if (signed_syscall_result >= 0) {

View File

@ -10,6 +10,7 @@ const main = fn () s32 {
.abi = .gnu,
},
.main_source_path = "src/main.nat",
.name = "first",
};
if (executable.compile()) {

View File

@ -11,6 +11,7 @@ const main = fn () s32 {
},
.main_source_path = "src/main.nat",
.link_libc = true,
.name = "link_libc",
};
if (executable.compile()) {