Skip to content

Commit

Permalink
♻️ simpler dependency management
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdelStark committed Oct 20, 2023
1 parent 35b0d79 commit 1c37256
Showing 1 changed file with 51 additions and 10 deletions.
61 changes: 51 additions & 10 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ const std = @import("std");
const package_name = "sig";
const package_path = "src/lib.zig";

// Represents a dependency on an external package.
const Dependency = struct {
name: []const u8,
module_name: []const u8,
};

// List of external dependencies that this package requires.
const external_dependencies = [_]Dependency{
.{ .name = "zig-cli", .module_name = "zig-cli" },
};

// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
// runner.
Expand All @@ -18,22 +29,31 @@ pub fn build(b: *std.Build) void {
// set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{});

// **************************************************************
// * HANDLE DEPENDENCY MODULES *
// **************************************************************
var dependency_modules = std.ArrayList(*std.build.Module).init(b.allocator);
defer _ = dependency_modules.deinit();

// Get dependency modules.
const dependencies_opts = .{ .target = target, .optimize = optimize };
const zig_cli_module = b.dependency("zig-cli", dependencies_opts).module("zig-cli");
_ = dependencies_opts;
// Populate dependency modules.
for (external_dependencies) |dep| {
const dependency_opts = .{ .target = target, .optimize = optimize };
const module = b.dependency(dep.name, dependency_opts).module(dep.module_name);
_ = dependency_modules.append(module) catch unreachable;
}
// This array can be passed to add the dependencies to lib, executable, tests, etc using `addModule` function.
const dep_array = toModuleDependencyArray(b.allocator, dependency_modules.items, &external_dependencies) catch unreachable;

// **************************************************************
// * CAIRO-ZIG AS A MODULE *
// **************************************************************
// expose cairo-zig as a module
_ = b.addModule(package_name, .{
.source_file = .{ .path = package_path },
.dependencies = &.{
.{
.name = "zig-cli",
.module = zig_cli_module,
},
},
.dependencies = dep_array,
});

// **************************************************************
Expand All @@ -48,7 +68,7 @@ pub fn build(b: *std.Build) void {
.optimize = optimize,
});
// Add dependency modules to the library.
lib.addModule("zig-cli", zig_cli_module);
for (dep_array) |mod| lib.addModule(mod.name, mod.module);
// This declares intent for the library to be installed into the standard
// location when the user invokes the "install" step (the default step when
// running `zig build`).
Expand All @@ -66,7 +86,7 @@ pub fn build(b: *std.Build) void {
.optimize = optimize,
});
// Add dependency modules to the executable.
exe.addModule("zig-cli", zig_cli_module);
for (dep_array) |mod| exe.addModule(mod.name, mod.module);
// This declares intent for the executable to be installed into the
// standard location when the user invokes the "install" step (the default
// step when running `zig build`).
Expand Down Expand Up @@ -103,7 +123,7 @@ pub fn build(b: *std.Build) void {
.optimize = optimize,
});
// Add dependency modules to the tests.
unit_tests.addModule("zig-cli", zig_cli_module);
for (dep_array) |mod| unit_tests.addModule(mod.name, mod.module);

const run_unit_tests = b.addRunArtifact(unit_tests);

Expand All @@ -114,3 +134,24 @@ pub fn build(b: *std.Build) void {
test_step.dependOn(&lib.step);
test_step.dependOn(&run_unit_tests.step);
}

/// Convert an array of Build.Module pointers to an array of Build.ModuleDependency.
/// # Arguments
/// * `allocator` - The allocator to use for the new array.
/// * `modules` - The array of Build.Module pointers to convert.
/// * `ext_deps` - The array of external dependencies.
/// # Returns
/// A new array of Build.ModuleDependency.
fn toModuleDependencyArray(allocator: std.mem.Allocator, modules: []const *std.Build.Module, ext_deps: []const Dependency) ![]std.Build.ModuleDependency {
var deps = std.ArrayList(std.Build.ModuleDependency).init(allocator);
defer deps.deinit();

for (modules, 0..) |module_ptr, i| {
try deps.append(.{
.name = ext_deps[i].name,
.module = module_ptr,
});
}

return deps.toOwnedSlice();
}

0 comments on commit 1c37256

Please sign in to comment.