Skip to content

Commit c74babb

Browse files
committed
properly detect and define HAVE_GETENTROPY
1 parent 1fab240 commit c74babb

1 file changed

Lines changed: 22 additions & 1 deletion

File tree

‎build.zig‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,24 @@ pub fn build(b: *std.Build) void {
5656
.netbsd => target.result.os.isAtLeast(.netbsd, .{ .major = 10, .minor = 0, .patch = 0 }) orelse false,
5757
else => false,
5858
};
59+
const has_getentropy: bool = switch (target.result.os.tag) {
60+
.linux => if (target.result.isMuslLibC())
61+
true
62+
else if (target.result.isGnuLibC())
63+
target.result.os.version_range.linux.glibc.order(.{ .major = 2, .minor = 25, .patch = 0 }) != .lt
64+
else
65+
unreachable,
66+
.macos,
67+
.ios,
68+
.tvos,
69+
.watchos,
70+
.visionos,
71+
=> true,
72+
.freebsd => target.result.os.isAtLeast(.freebsd, .{ .major = 12, .minor = 0, .patch = 0 }) orelse false,
73+
.netbsd => target.result.os.isAtLeast(.netbsd, .{ .major = 10, .minor = 0, .patch = 0 }) orelse false,
74+
.openbsd => target.result.os.isAtLeast(.freebsd, .{ .major = 5, .minor = 6, .patch = 0 }) orelse false,
75+
else => false,
76+
};
5977

6078
const has_arc4random_buf = switch (target.result.os.tag) {
6179
.dragonfly,
@@ -123,7 +141,7 @@ pub fn build(b: *std.Build) void {
123141
// if (has_arc4random) expat_sources.append(b.allocator, "lib/random_arc4random.c") catch @panic("OOM"); // HAVE_ARC4RANDOM
124142
if (has_arc4random_buf) expat_sources.append(b.allocator, "lib/random_arc4random_buf.c") catch @panic("OOM");
125143
if (xml_dev_urandom) expat_sources.append(b.allocator, "lib/random_dev_urandom.c") catch @panic("OOM");
126-
expat_sources.append(b.allocator, "lib/random_getentropy.c") catch @panic("OOM"); // HAVE_GETENTROPY
144+
if (has_getentropy) expat_sources.append(b.allocator, "lib/random_getentropy.c") catch @panic("OOM");
127145
if (has_getrandom) expat_sources.append(b.allocator, "lib/random_getrandom.c") catch @panic("OOM");
128146
if (target.result.os.tag == .windows) expat_sources.append(b.allocator, "lib/random_rand_s.c") catch @panic("OOM");
129147

@@ -144,6 +162,7 @@ pub fn build(b: *std.Build) void {
144162
if (min_size) expat.root_module.addCMacro("XML_MIN_SIZE", "1");
145163
if (char_type != .char) expat.root_module.addCMacro("XML_UNICODE", "1");
146164
if (char_type == .wchar_t) expat.root_module.addCMacro("XML_UNICODE_WCHAR_T", "1");
165+
if (has_getentropy) expat.root_module.addCMacro("HAVE_GETENTROPY", "1");
147166
expat.root_module.addIncludePath(upstream.path("expat/lib"));
148167
expat.installHeader(upstream.path("expat/lib/expat.h"), "expat.h");
149168
expat.installHeader(upstream.path("expat/lib/expat_external.h"), "expat_external.h");
@@ -167,6 +186,7 @@ pub fn build(b: *std.Build) void {
167186
xmlwf.root_module.addIncludePath(upstream.path("expat/lib"));
168187
if (char_type != .char) xmlwf.root_module.addCMacro("XML_UNICODE", "1");
169188
if (char_type == .wchar_t) xmlwf.root_module.addCMacro("XML_UNICODE_WCHAR_T", "1");
189+
if (has_getentropy) xmlwf.root_module.addCMacro("HAVE_GETENTROPY", "1");
170190
xmlwf.root_module.addCSourceFiles(.{
171191
.files = xmlwf_sources,
172192
.root = upstream.path("expat"),
@@ -209,6 +229,7 @@ pub fn build(b: *std.Build) void {
209229
});
210230
if (char_type != .char) exe.root_module.addCMacro("XML_UNICODE", "1");
211231
if (char_type == .wchar_t) exe.root_module.addCMacro("XML_UNICODE_WCHAR_T", "1");
232+
if (has_getentropy) exe.root_module.addCMacro("HAVE_GETENTROPY", "1");
212233
exe.root_module.addCSourceFile(.{
213234
.file = upstream.path(b.fmt("expat/{s}", .{source})),
214235
.flags = if (need_short_char_arg) &.{"-fshort-wchar"} else &.{},

0 commit comments

Comments
 (0)