Skip to content

Commit

Permalink
iguana support in build.zig (#12)
Browse files Browse the repository at this point in the history
* iguana support in build.zig

* additional build.zig changes

Co-authored-by: Jonathan Marler <[email protected]>
  • Loading branch information
g-w1 and marler8997 authored Feb 14, 2021
1 parent e408e41 commit 639d31a
Showing 1 changed file with 70 additions and 29 deletions.
99 changes: 70 additions & 29 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ const std = @import("std");
const Builder = std.build.Builder;
const Pkg = std.build.Pkg;

fn checkPackage(indexFile: []const u8, url: []const u8, ) void {
std.fs.cwd().access(indexFile, std.fs.File.OpenFlags { .read = true }) catch |err| {
std.debug.print("Error: library index file '{s}' does not exist\n", .{indexFile});
std.debug.print(" Run the following to clone it:\n", .{});
std.debug.print(" git clone {s} {s}\n", .{url, std.fs.path.dirname(indexFile)});
std.os.exit(1);
};
fn unwrapOptionalBool(optionalBool: ?bool) bool {
if (optionalBool) |b| return b;
return false;
}

pub fn build(b: *Builder) void {
const zigetRepo = "../ziget";
pub fn build(b: *Builder) !void {
const ssl_backend: enum { openssl, iguana } = init: {
const openssl = unwrapOptionalBool(b.option(bool, "openssl", "enable OpenSSL backend"));
const iguana = unwrapOptionalBool(b.option(bool, "iguana", "enable IguanaTLS backend"));
if (openssl and iguana) {
std.log.err("both '-Dopenssl' and '-Diguana' cannot be enabled at the same time", .{});
std.os.exit(1);
}
if (openssl) break :init .openssl;
if (iguana) break :init .iguana;

//
// TODO: figure out how to use ziget's build.zig file
//
const zigetIndexFile = zigetRepo ++ "/ziget.zig";
const sslIndexFile = zigetRepo ++ "/openssl/ssl.zig";
//const sslIndexFile = zigetRepo ++ "/nossl/ssl.zig";
checkPackage(zigetIndexFile, "https://github.com/marler8997/ziget");
std.log.err("please enable an ssl backend with either '-Dopenssl' or '-Diguana'", .{});
std.os.exit(1);
};

// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
Expand All @@ -32,23 +32,42 @@ pub fn build(b: *Builder) void {
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const mode = b.standardReleaseOptions();

const sslPkg = Pkg { .name = "ssl", .path = sslIndexFile };
const zigetPkg = Pkg {
.name = "ziget",
.path = zigetIndexFile,
.dependencies = &[_]Pkg {sslPkg},
};

const exe = b.addExecutable("zigup", "zigup.zig");
exe.setTarget(target);
exe.setBuildMode(mode);
exe.addPackage(zigetPkg);

// these libraries are required for openssl
exe.linkSystemLibrary("c");
exe.linkSystemLibrary("ssl");
exe.linkSystemLibrary("crypto");

//
// TODO: figure out how to use ziget's build.zig file
//
const ziget_repo = try getGitRepo(b.allocator, "https://github.com/marler8997/ziget");
const ssl_pkg = init: { switch (ssl_backend) {
.openssl => {
// these libraries are required for openssl
exe.linkSystemLibrary("c");
exe.linkSystemLibrary("ssl");
exe.linkSystemLibrary("crypto");
break :init Pkg {
.name = "ssl",
.path = try join(b, &[_][]const u8 {ziget_repo, "openssl", "ssl.zig"}),
};
},
.iguana => {
const iguana_repo = try getGitRepo(b.allocator, "https://github.com/alexnask/iguanaTLS");
const iguana_index_file = try join(b, &[_][]const u8 {iguana_repo, "src", "main.zig"});
break :init Pkg {
.name = "ssl",
.path = try join(b, &[_][]const u8 {ziget_repo, "iguana", "ssl.zig"}),
.dependencies = &[_]Pkg {
.{ .name = "iguana", .path = iguana_index_file },
},
};
}
}};
exe.addPackage(.{
.name = "ziget",
.path = try join(b, &[_][]const u8 {ziget_repo, "ziget.zig"}),
.dependencies = &[_]Pkg { ssl_pkg },
});
exe.install();

const run_cmd = exe.run();
Expand All @@ -58,3 +77,25 @@ pub fn build(b: *Builder) void {
run_step.dependOn(&run_cmd.step);
}

fn join(b: *Builder, parts: []const []const u8) ![]const u8 {
return try std.fs.path.join(b.allocator, parts);
}

fn getGitRepo(allocator: *std.mem.Allocator, url: []const u8) ![]const u8 {
const repo_path = init: {
const cwd = try std.process.getCwdAlloc(allocator);
defer allocator.free(cwd);
break :init try std.fs.path.join(allocator,
&[_][]const u8{ std.fs.path.dirname(cwd).?, std.fs.path.basename(url) }
);
};
errdefer allocator.free(repo_path);

std.fs.accessAbsolute(repo_path, std.fs.File.OpenFlags { .read = true }) catch |err| {
std.debug.print("Error: repository '{s}' does not exist\n", .{repo_path});
std.debug.print(" Run the following to clone it:\n", .{});
std.debug.print(" git clone {s} {s}\n", .{url, repo_path});
std.os.exit(1);
};
return repo_path;
}

0 comments on commit 639d31a

Please sign in to comment.