-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathbuild.zig
More file actions
169 lines (148 loc) · 5.21 KB
/
Copy pathbuild.zig
File metadata and controls
169 lines (148 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const std = @import("std");
const Build = std.Build;
const Step = Build.Step;
const Module = Build.Module;
const Allocator = std.mem.Allocator;
const SanitizeC = std.zig.SanitizeC;
const MODULE_NAME = "curl";
pub fn build(b: *Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const link_vendor = b.option(bool, "link_vendor", "Whether link to vendored libcurl (default: true)") orelse true;
const sanitize_c = b.option(SanitizeC, "sanitize_c", "Enable compiler sanitizers (default: null)");
const mbedtls_pthreads = b.option(bool, "mbedtls_pthreads", "Enable mbedtls pthread support (default: false)") orelse false;
const module = b.addModule(MODULE_NAME, .{
.root_source_file = b.path("src/root.zig"),
.link_libc = true,
.target = target,
.optimize = optimize,
});
const manifest = try parseManifest(b);
defer manifest.deinit(b.allocator);
const opt = b.addOptions();
opt.addOption([]const u8, "version", manifest.version);
module.addImport("build_info", opt.createModule());
var libcurl: ?*Step.Compile = null;
if (link_vendor) {
if (buildLibcurl(b, target, optimize, sanitize_c, mbedtls_pthreads)) |v| {
libcurl = v;
module.linkLibrary(v);
} else {
return;
}
}
inline for (.{ "basic", "post", "upload", "advanced", "multi", "header" }) |name| {
try addExample(b, name, module, libcurl, target, optimize);
}
const main_tests = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = b.path("src/root.zig"),
.link_libc = true,
.target = target,
.optimize = optimize,
}),
});
if (libcurl) |lib| {
main_tests.root_module.linkLibrary(lib);
} else {
main_tests.root_module.linkSystemLibrary("curl", .{});
}
const run_main_tests = b.addRunArtifact(main_tests);
const test_step = b.step("test", "Run library tests");
test_step.dependOn(&run_main_tests.step);
const doc_obj = b.addObject(.{
.name = "docs",
.root_module = module,
});
const install_docs = b.addInstallDirectory(.{
.source_dir = doc_obj.getEmittedDocs(),
.install_dir = .prefix,
.install_subdir = "docs",
});
const docs_step = b.step("docs", "Generate documentation");
docs_step.dependOn(&install_docs.step);
const check_step = b.step("check", "Used for checking the library");
inline for (.{ "basic", "advanced", "multi" }) |name| {
const check_exe = b.addExecutable(.{
.name = "check-" ++ name,
.root_module = b.createModule(.{
.root_source_file = b.path("examples/" ++ name ++ ".zig"),
.link_libc = true,
.target = target,
.optimize = optimize,
}),
});
check_exe.root_module.addImport(MODULE_NAME, module);
check_step.dependOn(&check_exe.step);
}
}
fn buildLibcurl(
b: *Build,
target: Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
sanitize_c: ?std.zig.SanitizeC,
mbedtls_pthreads: bool,
) ?*Step.Compile {
const curl = @import("libs/curl.zig").create(b, target, optimize, sanitize_c, mbedtls_pthreads);
const tls = @import("libs/mbedtls.zig").create(b, target, optimize, sanitize_c, mbedtls_pthreads);
const zlib = @import("libs/zlib.zig").create(b, target, optimize, sanitize_c);
if (curl == null or tls == null or zlib == null) {
return null;
}
const libcurl = curl.?;
libcurl.root_module.linkLibrary(tls.?);
libcurl.root_module.linkLibrary(zlib.?);
return libcurl;
}
fn addExample(
b: *Build,
comptime name: []const u8,
curl_module: *Module,
libcurl: ?*Step.Compile,
target: Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
) !void {
const exe = b.addExecutable(.{
.name = name,
.root_module = b.createModule(.{
.root_source_file = b.path("examples/" ++ name ++ ".zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
}),
});
b.installArtifact(exe);
exe.root_module.addImport(MODULE_NAME, curl_module);
if (libcurl) |lib| {
exe.root_module.linkLibrary(lib);
} else {
exe.root_module.linkSystemLibrary("curl", .{});
}
const run_step = b.step(
"run-" ++ name,
std.fmt.comptimePrint("Run {s} example", .{name}),
);
run_step.dependOn(&b.addRunArtifact(exe).step);
}
const Manifest = struct {
version: []const u8,
fn deinit(self: Manifest, allocator: Allocator) void {
allocator.free(self.version);
}
};
fn parseManifest(b: *Build) !Manifest {
const input = @embedFile("build.zig.zon");
var diagnostics: std.zon.parse.Diagnostics = .{};
defer diagnostics.deinit(b.allocator);
const parsed = std.zon.parse.fromSliceAlloc(
Manifest,
b.allocator,
input,
&diagnostics,
.{ .free_on_error = true, .ignore_unknown_fields = true },
) catch |err| {
std.debug.print("Parse diagnostics:\n{f}\n", .{diagnostics});
return err;
};
return parsed;
}