srctree

Gregory Mullen parent 90487085 9443f341
make curl a build opt

build.zig added: 30, removed: 5, total 25
@@ -4,6 +4,11 @@ pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
 
// build options
const enable_libcurl = b.option(bool, "libcurl", "enable linking with libcurl") orelse false;
const options = b.addOptions();
options.addOption(bool, "libcurl", enable_libcurl);
 
var binaries = std.ArrayList(*std.Build.Step.Compile).init(b.allocator);
defer binaries.clearAndFree();
 
@@ -35,9 +40,12 @@ pub fn build(b: *std.Build) void {
binaries.append(unit_tests) catch unreachable;
 
for (binaries.items) |ex| {
ex.linkSystemLibrary2("curl", .{ .preferred_link_mode = .Static });
//exe.linkLibC();
addSrcTemplates(ex);
ex.addOptions("config", options);
if (enable_libcurl) {
ex.linkSystemLibrary2("curl", .{ .preferred_link_mode = .Static });
ex.linkLibC();
}
}
 
const run_unit_tests = b.addRunArtifact(unit_tests);
 
src/curl.zig added: 30, removed: 5, total 25
@@ -1,4 +1,5 @@
const std = @import("std");
const bconfig = @import("config");
 
const cURL = @cImport({
@cInclude("curl/curl.h");
@@ -12,7 +13,16 @@ pub const CURLResult = struct {
body: ?[]u8,
};
 
pub fn curlRequest(a: Allocator, uri: []const u8) !CURLResult {
pub const curlRequest: *const fn (Allocator, []const u8) anyerror!CURLResult = if (bconfig.libcurl)
_curlRequest
else
fakeCurl;
 
fn fakeCurl(_: Allocator, _: []const u8) !CURLResult {
return error.CURLNotAvailable;
}
 
fn _curlRequest(a: Allocator, uri: []const u8) !CURLResult {
if (cURL.curl_global_init(cURL.CURL_GLOBAL_ALL) != cURL.CURLE_OK)
return error.CURLGlobalInitFailed;
defer cURL.curl_global_cleanup();
@@ -57,3 +67,10 @@ fn curlWriteCB(data: *anyopaque, size: c_uint, nmemb: c_uint, user_data: *anyopa
buffer.appendSlice(typed_data[0 .. nmemb * size]) catch return 0;
return nmemb * size;
}
 
test "bconfig" {
if (!bconfig.libcurl) {
const err = curlRequest(std.testing.allocator, "https://gr.ht/");
try std.testing.expectError(error.CURLNotAvailable, err);
} else {}
}