Skip to content

Commit

Permalink
organize build without boilerplate imports (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
StringNick authored Sep 11, 2024
1 parent 2db59d8 commit 60a6a9f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 74 deletions.
104 changes: 35 additions & 69 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
const std = @import("std");
const build_helpers = @import("build_helpers.zig");
const package_name = "coconut";
const package_path = "src/lib.zig";
const build_helpers = @import("build_helpers.zig");

// List of external dependencies that this package requires.
const external_dependencies = [_]build_helpers.Dependency{
.{
.name = "httpz",
.module_name = "httpz",
},
.{
.name = "zul",
.module_name = "zul",
},
.{
.name = "bitcoin-primitives",
.module_name = "bitcoin-primitives",
},
.{
.name = "zig-cli",
.module_name = "zig-cli",
Expand All @@ -23,26 +35,18 @@ pub fn build(b: *std.Build) !void {
// set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{});

// Channel module
const channel_m = b.addModule("channels", .{
.target = target,
.optimize = optimize,
.root_source_file = b.path("src/channels/channels.zig"),
});

// **************************************************************
// * HANDLE DEPENDENCY MODULES *
// **************************************************************
const dependencies_opts = .{
.target = target,
.optimize = optimize,
};

// This array can be passed to add the dependencies to lib, executable, tests, etc using `addModule` function.
const deps = build_helpers.generateModuleDependencies(
b,
&external_dependencies,
dependencies_opts,
.{
.optimize = optimize,
.target = target,
},
) catch unreachable;

// **************************************************************
Expand All @@ -54,30 +58,6 @@ pub fn build(b: *std.Build) !void {
.imports = deps,
});

// httpz dependency
const httpz_module = b.dependency("httpz", .{ .target = target, .optimize = optimize }).module("httpz");

// postgresql dependency
const pg = b.dependency("pg", .{
.target = target,
.optimize = optimize,
});

const zul = b.dependency("zul", .{
.target = target,
.optimize = optimize,
}).module("zul");

const bitcoin_primitives = b.dependency("bitcoin-primitives", .{
.target = target,
.optimize = optimize,
});

const base58_module = b.dependency("base58-zig", .{
.target = target,
.optimize = optimize,
}).module("base58-zig");

// **************************************************************
// * COCONUT AS A LIBRARY *
// **************************************************************
Expand All @@ -87,11 +67,13 @@ pub fn build(b: *std.Build) !void {
.target = target,
.optimize = optimize,
});

// Add dependency modules to the library.
for (deps) |mod| lib.root_module.addImport(
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 @@ -111,14 +93,8 @@ pub fn build(b: *std.Build) !void {
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("zul", zul);
exe.root_module.addImport("httpz", httpz_module);
exe.root_module.addImport("bitcoin-primitives", bitcoin_primitives.module("bitcoin-primitives"));
exe.root_module.addImport("base58", base58_module);

exe.root_module.addImport("channels", channel_m);

// Add dependency modules to the executable.
// Add dependency modules to the library.
for (deps) |mod| exe.root_module.addImport(
mod.name,
mod.module,
Expand All @@ -134,11 +110,8 @@ pub fn build(b: *std.Build) !void {
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("httpz", httpz_module);
exe.root_module.addImport("pg", pg.module("pg"));
exe.root_module.addImport("zul", zul);

// Add dependency modules to the executable.
// Add dependency modules to the library.
for (deps) |mod| exe.root_module.addImport(
mod.name,
mod.module,
Expand All @@ -155,12 +128,12 @@ pub fn build(b: *std.Build) !void {
.optimize = optimize,
.single_threaded = false,
});
lib_unit_tests.root_module.addImport("zul", zul);
lib_unit_tests.root_module.addImport("httpz", httpz_module);
lib_unit_tests.root_module.addImport("bitcoin-primitives", bitcoin_primitives.module("bitcoin-primitives"));
lib_unit_tests.root_module.addImport("base58", base58_module);

lib_unit_tests.root_module.addImport("channels", channel_m);
// Add dependency modules to the library.
for (deps) |mod| lib_unit_tests.root_module.addImport(
mod.name,
mod.module,
);

const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);

Expand All @@ -178,13 +151,8 @@ pub fn build(b: *std.Build) !void {
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("httpz", httpz_module);
exe.root_module.addImport("channels", channel_m);
exe.root_module.addImport("zul", zul);
exe.root_module.addImport("pg", pg.module("pg"));
exe.root_module.addImport("bitcoin-primitives", bitcoin_primitives.module("bitcoin-primitives"));

// Add dependency modules to the executable.
// Add dependency modules to the library.
for (deps) |mod| exe.root_module.addImport(
mod.name,
mod.module,
Expand Down Expand Up @@ -213,7 +181,8 @@ pub fn build(b: *std.Build) !void {
.target = target,
.optimize = optimize,
});
// Add dependency modules to the executable.

// Add dependency modules to the library.
for (deps) |mod| exe.root_module.addImport(
mod.name,
mod.module,
Expand All @@ -238,12 +207,12 @@ pub fn build(b: *std.Build) !void {
.optimize = optimize,
.single_threaded = false,
});
lib_unit_tests.root_module.addImport("zul", zul);
lib_unit_tests.root_module.addImport("httpz", httpz_module);
lib_unit_tests.root_module.addImport("bitcoin-primitives", bitcoin_primitives.module("bitcoin-primitives"));
lib_unit_tests.root_module.addImport("base58", base58_module);

lib_unit_tests.root_module.addImport("channels", channel_m);
// Add dependency modules to the library.
for (deps) |mod| lib_unit_tests.root_module.addImport(
mod.name,
mod.module,
);

const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);

Expand All @@ -258,15 +227,12 @@ pub fn build(b: *std.Build) !void {
.optimize = .ReleaseFast,
});

// Add dependency modules to the executable.
// Add dependency modules to the library.
for (deps) |mod| bench.root_module.addImport(
mod.name,
mod.module,
);

bench.root_module.addImport("zul", zul);
bench.root_module.addImport("bitcoin-primitives", bitcoin_primitives.module("bitcoin-primitives"));

const run_bench = b.addRunArtifact(bench);

// Add option for report generation
Expand Down
4 changes: 0 additions & 4 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@
.url = "https://github.com/karlseguin/pg.zig/archive/1491270ac43c7eba91992bb06b3758254c36e39a.zip",
.hash = "1220bcc68967188de7ad5d520a4629c0d1e169c111d87e6978a3c128de5ec2b6bdd0",
},
.@"base58-zig" = .{
.url = "https://github.com/ultd/base58-zig/archive/e1001fbe8b41eed36d81e37931ada66b784e51dc.zip",
.hash = "12206e5050a03cd9dcb896781de0cf541081488006532675371653f61d00c1f27433",
},
.@"bitcoin-primitives" = .{
.url = "git+https://github.com/zig-bitcoin/bitcoin-primitives#b51ffa5b67e376a102bde0250e4235bc66e32c2e",
.hash = "1220ae99270542861a0f2cc5d9b0b2df2f11c6bd2ce431c9067c5c958cd36f66c948",
Expand Down
2 changes: 1 addition & 1 deletion src/fake_wallet/fake_wallet.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const MeltQuoteBolt11Request = core.nuts.nut05.MeltQuoteBolt11Request;
const Settings = core.lightning.Settings;
const MintMeltSettings = core.lightning.MintMeltSettings;
const FeeReserve = core.mint.FeeReserve;
const Channel = @import("channels").Channel;
const Channel = @import("../channels/channels.zig").Channel;
const MintQuoteState = core.nuts.nut04.QuoteState;

// TODO: wait any invoices, here we need create a new listener, that will receive
Expand Down

0 comments on commit 60a6a9f

Please sign in to comment.