Structure main program properly
This commit is contained in:
parent
f89fb293c0
commit
e6c3c55179
1
.gitignore
vendored
1
.gitignore
vendored
@ -12,3 +12,4 @@ imgui.ini
|
|||||||
/.gdb_history
|
/.gdb_history
|
||||||
/.zig-cache/
|
/.zig-cache/
|
||||||
/zig-out/
|
/zig-out/
|
||||||
|
/bb-cache/
|
||||||
|
@ -274,7 +274,13 @@ fn debug_binary(b: *std.Build, exe: *std.Build.Step.Compile) *std.Build.Step.Run
|
|||||||
run_step.addArg("gdb");
|
run_step.addArg("gdb");
|
||||||
run_step.addArg("-ex");
|
run_step.addArg("-ex");
|
||||||
run_step.addArg("r");
|
run_step.addArg("r");
|
||||||
run_step.addArtifactArg(exe);
|
if (b.args) |args| {
|
||||||
|
run_step.addArg("--args");
|
||||||
|
run_step.addArtifactArg(exe);
|
||||||
|
run_step.addArgs(args);
|
||||||
|
} else {
|
||||||
|
run_step.addArtifactArg(exe);
|
||||||
|
}
|
||||||
|
|
||||||
return run_step;
|
return run_step;
|
||||||
}
|
}
|
||||||
|
4
src/compiler.bbb
Normal file
4
src/compiler.bbb
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
[export] main = fn [cc(c)] () s32
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
37
src/lib.zig
37
src/lib.zig
@ -279,6 +279,16 @@ pub const os = struct {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn make_directory(path: [*:0]const u8) void {
|
||||||
|
switch (builtin.os.tag) {
|
||||||
|
.windows => @compileError("TODO"),
|
||||||
|
else => {
|
||||||
|
const result = posix.mkdir(path, 0o755);
|
||||||
|
_ = result;
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const linux = struct {
|
const linux = struct {
|
||||||
pub const CPU_SETSIZE = 128;
|
pub const CPU_SETSIZE = 128;
|
||||||
pub const cpu_set_t = [CPU_SETSIZE / @sizeOf(usize)]usize;
|
pub const cpu_set_t = [CPU_SETSIZE / @sizeOf(usize)]usize;
|
||||||
@ -409,6 +419,7 @@ pub const os = struct {
|
|||||||
extern "c" fn read(fd: system.FileDescriptor, pointer: [*]u8, byte_count: usize) isize;
|
extern "c" fn read(fd: system.FileDescriptor, pointer: [*]u8, byte_count: usize) isize;
|
||||||
extern "c" fn write(fd: system.FileDescriptor, pointer: [*]const u8, byte_count: usize) isize;
|
extern "c" fn write(fd: system.FileDescriptor, pointer: [*]const u8, byte_count: usize) isize;
|
||||||
extern "c" fn sched_getaffinity(pid: c_int, size: usize, set: *cpu_set_t) c_int;
|
extern "c" fn sched_getaffinity(pid: c_int, size: usize, set: *cpu_set_t) c_int;
|
||||||
|
extern "c" fn mkdir(path: [*:0]const u8, mode: mode_t) c_int;
|
||||||
|
|
||||||
const mode_t = usize;
|
const mode_t = usize;
|
||||||
|
|
||||||
@ -522,6 +533,29 @@ pub const string = struct {
|
|||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn first_character(str: []const u8, ch: u8) ?usize {
|
||||||
|
for (str, 0..) |existing_ch, i| {
|
||||||
|
if (ch == existing_ch) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn last_character(str: []const u8, ch: u8) ?usize {
|
||||||
|
var i = str.len;
|
||||||
|
while (i > 0) {
|
||||||
|
i -= 1;
|
||||||
|
|
||||||
|
if (str[i] == ch) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const cstring = struct {
|
pub const cstring = struct {
|
||||||
@ -2474,6 +2508,7 @@ pub const GlobalState = struct {
|
|||||||
thread_count: usize,
|
thread_count: usize,
|
||||||
|
|
||||||
pub var initialized = false;
|
pub var initialized = false;
|
||||||
|
|
||||||
pub fn initialize() void {
|
pub fn initialize() void {
|
||||||
assert(!initialized);
|
assert(!initialized);
|
||||||
defer initialized = true;
|
defer initialized = true;
|
||||||
@ -2515,7 +2550,7 @@ pub fn print(format_string: [*:0]const u8, ...) callconv(.C) void {
|
|||||||
@cVaEnd(&args);
|
@cVaEnd(&args);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const print_string = print_string_stdout;
|
pub const print_string = print_string_stderr;
|
||||||
|
|
||||||
pub fn print_string_stdout(str: []const u8) void {
|
pub fn print_string_stdout(str: []const u8) void {
|
||||||
os.get_stdout().write(str);
|
os.get_stdout().write(str);
|
||||||
|
41
src/main.zig
41
src/main.zig
@ -1,4 +1,5 @@
|
|||||||
const lib = @import("lib.zig");
|
const lib = @import("lib.zig");
|
||||||
|
const os = lib.os;
|
||||||
const llvm = @import("LLVM.zig");
|
const llvm = @import("LLVM.zig");
|
||||||
const converter = @import("converter.zig");
|
const converter = @import("converter.zig");
|
||||||
const Arena = lib.Arena;
|
const Arena = lib.Arena;
|
||||||
@ -136,9 +137,45 @@ pub const panic = struct {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var global_persistent_arena: *Arena = undefined;
|
pub fn main(argc: c_int, argv: [*:null]const ?[*:0]const u8) callconv(.C) c_int {
|
||||||
|
if (argc != 2) {
|
||||||
|
lib.print_string("Failed to match argument count");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
const file_path_pointer = argv[1] orelse return 1;
|
||||||
|
const file_path = lib.cstring.to_slice(file_path_pointer);
|
||||||
|
if (file_path.len < 5) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
pub fn main() callconv(.C) c_int {
|
const extension_start = lib.string.last_character(file_path, '.') orelse return 1;
|
||||||
|
if (!lib.string.equal(file_path[extension_start..], ".bbb")) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
const separator_index = lib.string.last_character(file_path, '/') orelse 0;
|
||||||
|
const base_start = separator_index + @intFromBool(separator_index != 0 or file_path[separator_index] == '/');
|
||||||
|
const base_name = file_path[base_start..extension_start];
|
||||||
|
|
||||||
|
lib.GlobalState.initialize();
|
||||||
|
|
||||||
|
const arena = lib.global.arena;
|
||||||
|
|
||||||
|
const build_dir = "bb-cache";
|
||||||
|
os.make_directory(build_dir);
|
||||||
|
const output_path_base = arena.join_string(&.{ build_dir, "/", base_name });
|
||||||
|
const output_object_path = arena.join_string(&.{ output_path_base, ".o" });
|
||||||
|
const output_executable_path = output_path_base;
|
||||||
|
|
||||||
|
const file_content = lib.file.read(arena, file_path);
|
||||||
|
converter.convert(.{
|
||||||
|
.executable = output_executable_path,
|
||||||
|
.object = output_object_path,
|
||||||
|
.name = base_name,
|
||||||
|
.build_mode = .debug_none,
|
||||||
|
.content = file_content,
|
||||||
|
.path = file_path,
|
||||||
|
.has_debug_info = 1,
|
||||||
|
});
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user