Skip to content

Commit

Permalink
[progress #146] fix run
Browse files Browse the repository at this point in the history
  • Loading branch information
tiawl committed Apr 7, 2024
1 parent 6af2072 commit fdcde59
Show file tree
Hide file tree
Showing 21 changed files with 421 additions and 353 deletions.
41 changes: 23 additions & 18 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const shaders = @import ("build/shaders.zig");

const utils = @import ("build/utils.zig");
const Options = utils.Options;
const Package = utils.Package;
const Profile = utils.Profile;
const zon = utils.zon;

Expand Down Expand Up @@ -132,7 +133,7 @@ fn parse_options (builder: *std.Build) !Profile
return profile;
}

fn link (builder: *std.Build, profile: *const Profile) !*std.Build.Module
fn link (builder: *std.Build, profile: *const Profile) !*Package
{
const glfw_lib = glfw.lib (builder, profile);

Expand All @@ -142,19 +143,22 @@ fn link (builder: *std.Build, profile: *const Profile) !*std.Build.Module
});
const cimgui = imgui_dep.artifact ("cimgui");

const c = builder.createModule (.{
.root_source_file = .{ .path = try builder.build_root.join (
builder.allocator, &.{ "src", "binding", "raw.zig", }), },
.target = profile.target,
.optimize = profile.optimize,
});
c.linkLibrary (glfw_lib);
c.linkLibrary (cimgui);
c.addIncludePath (imgui_dep.path ("imgui"));
const c = try Package.init (builder, profile, "c", try builder.build_root.join (
builder.allocator, &.{ "src", "binding", "raw.zig", }));
c.link (glfw_lib);
c.link (cimgui);
c.include (imgui_dep.path ("imgui"));

return c;
}

fn manage_deps (glfw_pkg: *Package, vk_pkg: *Package) !void
{
try glfw_pkg.get ("window").put (vk_pkg.get ("instance"), .{});
try glfw_pkg.get ("window").put (vk_pkg.get ("khr").get ("surface"), .{});
try vk_pkg.put (glfw_pkg.get ("vk"), .{ .pkg_name = "glfw", });
}

fn import (builder: *std.Build, exe: *std.Build.Step.Compile,
profile: *const Profile) !void
{
Expand All @@ -166,10 +170,11 @@ fn import (builder: *std.Build, exe: *std.Build.Step.Compile,

const shaders_module = try shaders.import (builder, exe, profile);
const c = try link (builder, profile);
const glfw_module = try glfw.import (builder, profile, c);
const vk_module = try vk.import (builder, profile, c);
const imgui_module =
try imgui.import (builder, profile, c, glfw_module, vk_module);
const glfw_pkg = try glfw.import (builder, profile, c);
const vk_pkg = try vk.import (builder, profile, c);
const imgui_pkg = try imgui.import (builder, profile, c, glfw_pkg, vk_pkg);

try manage_deps (glfw_pkg, vk_pkg);

const build_options = profile.variables.createModule ();
const logger = builder.createModule (.{
Expand All @@ -189,14 +194,14 @@ fn import (builder: *std.Build, exe: *std.Build.Step.Compile,
.optimize = profile.optimize,
});
instance.addImport ("logger", logger);
instance.addImport ("vk", vk_module);
instance.addImport ("vk", vk_pkg.module);

for ([_] struct { name: [] const u8, ptr: *std.Build.Module, } {
.{ .name = "datetime", .ptr = datetime, },
.{ .name = "shader", .ptr = shaders_module, },
.{ .name = "glfw", .ptr = glfw_module, },
.{ .name = "vk", .ptr = vk_module, },
.{ .name = "imgui", .ptr = imgui_module, },
.{ .name = "glfw", .ptr = glfw_pkg.module, },
.{ .name = "vk", .ptr = vk_pkg.module, },
.{ .name = "imgui", .ptr = imgui_pkg.module, },
.{ .name = "logger", .ptr = logger, },
.{ .name = "instance", .ptr = instance, },
}) |module| exe.root_module.addImport (module.name, module.ptr);
Expand Down
48 changes: 15 additions & 33 deletions build/glfw.zig
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
const std = @import ("std");

const Profile = @import ("utils.zig").Profile;
const utils = @import ("utils.zig");
const Package = utils.Package;
const Profile = utils.Profile;

pub fn import (builder: *std.Build, profile: *const Profile,
c: *std.Build.Module) !*std.Build.Module
c: *Package) !*Package
{
const path = try builder.build_root.join (builder.allocator,
&.{ "src", "binding", "glfw", });

var modules = std.ArrayList (*std.Build.Module).init (builder.allocator);
var glfw = try Package.init (builder, profile, "glfw",
try std.fs.path.join (builder.allocator, &.{ path, "glfw.zig", }));
try glfw.put (c, .{});

var sub: *Package = undefined;

var dir = try builder.build_root.handle.openDir (path, .{ .iterate = true, });
defer dir.close ();
Expand All @@ -21,42 +27,18 @@ pub fn import (builder: *std.Build, profile: *const Profile,
.file => {
if (!std.mem.eql (u8, entry.name, "glfw.zig"))
{
try modules.append (builder.createModule (.{
.root_source_file = .{ .path = try std.fs.path.join (
builder.allocator, &.{ path, entry.name, }), },
.target = profile.target,
.optimize = profile.optimize,
}));
modules.items [modules.items.len - 1].addImport ("c", c);
sub = try Package.init (builder, profile,
builder.dupe (std.fs.path.stem (entry.name)),
try std.fs.path.join (builder.allocator, &.{ path, entry.name, }));
try sub.put (c, .{});
try glfw.put (sub, .{});
try sub.put (glfw, .{});
}
},
else => {},
}
}

const glfw = builder.createModule (.{
.root_source_file = .{ .path = try std.fs.path.join (builder.allocator,
&.{ path, "glfw.zig", }), },
.target = profile.target,
.optimize = profile.optimize,
});
glfw.addImport ("c", c);

for (modules.items) |module|
{
const name = std.fs.path.stem (
std.fs.path.basename (module.root_source_file.?.getPath (builder)));
glfw.addImport (name, module);
module.addImport ("glfw", glfw);
for (modules.items) |other|
{
const other_name = std.fs.path.stem (
std.fs.path.basename (other.root_source_file.?.getPath (builder)));
if (std.mem.eql (u8, name, other_name)) continue;
module.addImport (other_name, other);
}
}

return glfw;
}

Expand Down
56 changes: 20 additions & 36 deletions build/imgui.zig
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
const std = @import ("std");

const Profile = @import ("utils.zig").Profile;
const utils = @import ("utils.zig");
const Package = utils.Package;
const Profile = utils.Profile;

pub fn import (builder: *std.Build, profile: *const Profile,
c: *std.Build.Module, glfw: *std.Build.Module,
vk: *std.Build.Module) !*std.Build.Module
c: *Package, glfw: *Package, vk: *Package) !*Package
{
const path = try builder.build_root.join (builder.allocator,
&.{ "src", "binding", "imgui", });

var modules = std.ArrayList (*std.Build.Module).init (builder.allocator);
var imgui = try Package.init (builder, profile, "imgui",
try std.fs.path.join (builder.allocator, &.{ path, "imgui.zig", }));
try imgui.put (c, .{});
try imgui.put (vk, .{});
try imgui.put (glfw, .{});

var sub: *Package = undefined;

var dir = try builder.build_root.handle.openDir (path, .{ .iterate = true, });
defer dir.close ();
Expand All @@ -22,43 +29,20 @@ pub fn import (builder: *std.Build, profile: *const Profile,
.file => {
if (!std.mem.eql (u8, entry.name, "imgui.zig"))
{
try modules.append (builder.createModule (.{
.root_source_file = .{ .path = try std.fs.path.join (
builder.allocator, &.{ path, entry.name, }), },
.target = profile.target,
.optimize = profile.optimize,
}));
modules.items [modules.items.len - 1].addImport ("c", c);
modules.items [modules.items.len - 1].addImport ("glfw", glfw);
modules.items [modules.items.len - 1].addImport ("vk", vk);
sub = try Package.init (builder, profile,
builder.dupe (std.fs.path.stem (entry.name)),
try std.fs.path.join (builder.allocator,
&.{ path, entry.name, }));
try sub.put (c, .{});
try sub.put (vk, .{});
try sub.put (glfw, .{});
try imgui.put (sub, .{});
try sub.put (imgui, .{});
}
},
else => {},
}
}

const imgui = builder.createModule (.{
.root_source_file = .{ .path = try std.fs.path.join (builder.allocator,
&.{ path, "imgui.zig", }), },
.target = profile.target,
.optimize = profile.optimize,
});
imgui.addImport ("c", c);

for (modules.items) |module|
{
const name = std.fs.path.stem (
std.fs.path.basename (module.root_source_file.?.getPath (builder)));
imgui.addImport (name, module);
module.addImport ("imgui", imgui);
for (modules.items) |other|
{
const other_name = std.fs.path.stem (
std.fs.path.basename (other.root_source_file.?.getPath (builder)));
if (std.mem.eql (u8, name, other_name)) continue;
module.addImport (other_name, other);
}
}

return imgui;
}
2 changes: 1 addition & 1 deletion build/shaders.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn add (builder: *std.Build, exe: *std.Build.Step.Compile, profile: *const Profi
depth.* += 1;
if (std.mem.eql (u8, entry.basename, dupe))
{
const path = try builder.allocator.dupe (u8, entry.path);
const path = builder.dupe (entry.path);
const source = try glsl.readFileAlloc (builder.allocator, path, std.math.maxInt (usize));
try ptr.nodes.append (.{
.name = std.fs.path.extension (dupe) [1 ..],
Expand Down
46 changes: 46 additions & 0 deletions build/utils.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,52 @@ pub const Profile = struct
command: [] const [] const u8,
};

pub const Package = struct
{
name: [] const u8,
module: *std.Build.Module,
subs: std.StringHashMap (*@This ()),

pub fn init (builder: *std.Build, profile: *const Profile,
name: [] const u8, path: [] const u8) !*@This ()
{
const self = try builder.allocator.create (@This ());
self.name = name;
self.module = builder.createModule (.{
.root_source_file = .{ .path = path, },
.target = profile.target,
.optimize = profile.optimize,
});
self.subs = std.StringHashMap (*@This ()).init (builder.allocator);
return self;
}

pub fn get (self: @This (), key: [] const u8) *@This ()
{
return self.subs.get (key) orelse
std.debug.panic ("\"{s}\" module does not exist into \"{s}\" package",
.{ key, self.name, });
}

pub fn put (self: *@This (), pkg: *@This (),
optional: struct { pkg_name: ?[] const u8 = null, }) !void
{
const name = if (optional.pkg_name) |n| n else pkg.name;
self.module.addImport (name, pkg.module);
try self.subs.put (name, pkg);
}

pub fn link (self: @This (), lib: *std.Build.Step.Compile) void
{
self.module.linkLibrary (lib);
}

pub fn include (self: @This (), path: std.Build.LazyPath) void
{
self.module.addIncludePath (path);
}
};

// Create a hash from a shader's source contents.
pub fn digest (profile: ?*const Profile, source: [] u8) [64] u8
{
Expand Down
Loading

0 comments on commit fdcde59

Please sign in to comment.