From cbaa0c75d6e7f5039eaf9a7f762fe2da88516958 Mon Sep 17 00:00:00 2001 From: Bernard Assan Date: Fri, 20 Mar 2026 14:06:37 +0000 Subject: [PATCH 1/2] Update build.zig to compile on the latest stable Zig 0.15.2 Add a named LazyPath to pcre2_h so library uses can add pcre2_h to their projects include paths Add some more config_h options Correctly set PCRE2_EXPORT Signed-off-by: Bernard Assan --- build.zig | 157 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 87 insertions(+), 70 deletions(-) diff --git a/build.zig b/build.zig index 960137ab3..a025f4756 100644 --- a/build.zig +++ b/build.zig @@ -7,50 +7,74 @@ pub const CodeUnitWidth = enum { }; pub fn build(b: *std.Build) !void { - const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); - const linkage = b.option(std.builtin.LinkMode, "linkage", "whether to statically or dynamically link the library") orelse @as(std.builtin.LinkMode, if (target.result.isGnuLibC()) .dynamic else .static); + const target = b.standardTargetOptions(.{}); + const rt = target.result; + + const linkage = b.option(std.builtin.LinkMode, "linkage", "whether to statically or dynamically link the library") orelse @as(std.builtin.LinkMode, if (rt.isGnuLibC()) .dynamic else .static); const codeUnitWidth = b.option(CodeUnitWidth, "code-unit-width", "Sets the code unit width") orelse .@"8"; const jit = b.option(bool, "support_jit", "Enable/disable JIT compiler support") orelse false; - const pcre2_header_dir = b.addWriteFiles(); - const pcre2_header = pcre2_header_dir.addCopyFile(b.path("src/pcre2.h.generic"), "pcre2.h"); + const pcre2_h_dir = b.addWriteFiles(); + const pcre2_h = pcre2_h_dir.addCopyFile(b.path("src/pcre2.h.generic"), "pcre2.h"); + b.addNamedLazyPath("pcre2.h", pcre2_h); + + const is_unix = rt.os.tag != .windows; + const is_mingw = rt.isMinGW(); + const is_musl = rt.isMuslLibC(); + const is_glibc = rt.isGnuLibC(); + const is_freebsd = rt.isFreeBSDLibC(); - const config_header = b.addConfigHeader( + const config_h = b.addConfigHeader( .{ - .style = .{ .cmake = b.path("src/config-cmake.h.in") }, + .style = .{ + .cmake = b.path("src/config-cmake.h.in"), + }, .include_path = "config.h", }, .{ .HAVE_ASSERT_H = true, - .HAVE_UNISTD_H = (target.result.os.tag != .windows), - .HAVE_WINDOWS_H = (target.result.os.tag == .windows), - + .HAVE_DIRENT_H = is_unix or is_mingw, + .HAVE_SYS_STAT_H = true, + .HAVE_SYS_TYPES_H = true, + .HAVE_UNISTD_H = is_unix or is_mingw, + .HAVE_WINDOWS_H = rt.os.tag == .windows, + .HAVE_MKOSTEMP = is_unix, + .HAVE_MEMFD_CREATE = is_musl or is_glibc or is_freebsd, + .HAVE_SECURE_GETENV = is_musl or is_glibc or is_freebsd, + .HAVE_REALPATH = is_unix, + + // all compilation is using the Zig bundled c compiler .HAVE_ATTRIBUTE_UNINITIALIZED = true, + .HAVE_VISIBILITY = true, + .HAVE_BUILTIN_ASSUME = null, .HAVE_BUILTIN_MUL_OVERFLOW = true, .HAVE_BUILTIN_UNREACHABLE = true, + .INTEL_CET_ENABLED = null, - .SUPPORT_PCRE2_8 = codeUnitWidth == CodeUnitWidth.@"8", - .SUPPORT_PCRE2_16 = codeUnitWidth == CodeUnitWidth.@"16", - .SUPPORT_PCRE2_32 = codeUnitWidth == CodeUnitWidth.@"32", + .SUPPORT_PCRE2_8 = codeUnitWidth == .@"8", + .SUPPORT_PCRE2_16 = codeUnitWidth == .@"16", + .SUPPORT_PCRE2_32 = codeUnitWidth == .@"32", .SUPPORT_UNICODE = true, .SUPPORT_JIT = jit, - .PCRE2_EXPORT = null, + .PCRE2_EXPORT = switch (linkage) { + .dynamic => "__attribute__ ((visibility (\"default\")))", + else => null, + }, .PCRE2_LINK_SIZE = 2, + .PCRE2_PARENS_NEST_LIMIT = 250, .PCRE2_HEAP_LIMIT = 20000000, + .PCRE2_MAX_VARLOOKBEHIND = 255, .PCRE2_MATCH_LIMIT = 10000000, .PCRE2_MATCH_LIMIT_DEPTH = "MATCH_LIMIT", - .PCRE2_MAX_VARLOOKBEHIND = 255, - .NEWLINE_DEFAULT = 2, - .PCRE2_PARENS_NEST_LIMIT = 250, .PCRE2GREP_BUFSIZE = 20480, .PCRE2GREP_MAX_BUFSIZE = 1048576, + .NEWLINE_DEFAULT = 2, }, ); // pcre2-8/16/32 library - const lib_mod = b.createModule(.{ .target = target, .optimize = optimize, @@ -59,25 +83,18 @@ pub fn build(b: *std.Build) !void { lib_mod.addCMacro("HAVE_CONFIG_H", ""); lib_mod.addCMacro("PCRE2_CODE_UNIT_WIDTH", @tagName(codeUnitWidth)); - if (linkage == .static) { - lib_mod.addCMacro("PCRE2_STATIC", ""); + switch (linkage) { + .static => lib_mod.addCMacro("PCRE2_STATIC", ""), + .dynamic => {}, } + lib_mod.addConfigHeader(config_h); + lib_mod.addIncludePath(pcre2_h_dir.getDirectory()); + lib_mod.addIncludePath(b.path("src")); - const lib = b.addLibrary(.{ - .name = b.fmt("pcre2-{s}", .{@tagName(codeUnitWidth)}), - .root_module = lib_mod, - .linkage = linkage, - }); - - lib.addConfigHeader(config_header); - lib.addIncludePath(pcre2_header_dir.getDirectory()); - lib.addIncludePath(b.path("src")); - - lib.addCSourceFile(.{ + lib_mod.addCSourceFile(.{ .file = b.addWriteFiles().addCopyFile(b.path("src/pcre2_chartables.c.dist"), "pcre2_chartables.c"), }); - - lib.addCSourceFiles(.{ + lib_mod.addCSourceFiles(.{ .files = &.{ "src/pcre2_auto_possess.c", "src/pcre2_chkdint.c", @@ -112,11 +129,16 @@ pub fn build(b: *std.Build) !void { }, }); - lib.installHeader(pcre2_header, "pcre2.h"); + const lib = b.addLibrary(.{ + .name = b.fmt("pcre2-{t}", .{codeUnitWidth}), + .root_module = lib_mod, + .linkage = linkage, + }); + + lib.installHeader(pcre2_h, "pcre2.h"); b.installArtifact(lib); // pcre2test - const pcre2test_mod = b.createModule(.{ .target = target, .optimize = optimize, @@ -124,19 +146,28 @@ pub fn build(b: *std.Build) !void { }); pcre2test_mod.addCMacro("HAVE_CONFIG_H", ""); - if (linkage == .static) { - pcre2test_mod.addCMacro("PCRE2_STATIC", ""); - } else { - pcre2test_mod.addCMacro("PCRE2POSIX_SHARED", ""); + + switch (linkage) { + .static => pcre2test_mod.addCMacro("PCRE2_STATIC", ""), + .dynamic => pcre2test_mod.addCMacro("PCRE2POSIX_SHARED", ""), } const pcre2test = b.addExecutable(.{ .name = "pcre2test", .root_module = pcre2test_mod, }); + pcre2test_mod.addConfigHeader(config_h); + pcre2test_mod.addIncludePath(pcre2_h_dir.getDirectory()); + pcre2test_mod.addIncludePath(b.path("src")); - // pcre2-posix library + pcre2test_mod.addCSourceFile(.{ + .file = b.path("src/pcre2test.c"), + }); + + pcre2test_mod.linkLibrary(lib); + b.installArtifact(pcre2test); + // pcre2-posix library if (codeUnitWidth == CodeUnitWidth.@"8") { const posixLib_mod = b.createModule(.{ .target = target, @@ -146,47 +177,33 @@ pub fn build(b: *std.Build) !void { posixLib_mod.addCMacro("HAVE_CONFIG_H", ""); posixLib_mod.addCMacro("PCRE2_CODE_UNIT_WIDTH", @tagName(codeUnitWidth)); - if (linkage == .static) { - posixLib_mod.addCMacro("PCRE2_STATIC", ""); - } else { - posixLib_mod.addCMacro("PCRE2POSIX_SHARED", ""); - } - const posixLib = b.addLibrary(.{ - .name = "pcre2-posix", - .root_module = posixLib_mod, - .linkage = linkage, - }); + switch (linkage) { + .static => posixLib_mod.addCMacro("PCRE2_STATIC", ""), + .dynamic => posixLib_mod.addCMacro("PCRE2POSIX_SHARED", ""), + } - posixLib.addConfigHeader(config_header); - posixLib.addIncludePath(pcre2_header_dir.getDirectory()); - posixLib.addIncludePath(b.path("src")); + posixLib_mod.addConfigHeader(config_h); + posixLib_mod.addIncludePath(pcre2_h_dir.getDirectory()); + posixLib_mod.addIncludePath(b.path("src")); - posixLib.addCSourceFiles(.{ + posixLib_mod.addCSourceFiles(.{ .files = &.{ "src/pcre2posix.c", }, }); - posixLib.linkLibrary(lib); + posixLib_mod.linkLibrary(lib); + + const posixLib = b.addLibrary(.{ + .name = "pcre2-posix", + .root_module = posixLib_mod, + .linkage = linkage, + }); + + pcre2test_mod.linkLibrary(posixLib); posixLib.installHeader(b.path("src/pcre2posix.h"), "pcre2posix.h"); b.installArtifact(posixLib); - - pcre2test.linkLibrary(posixLib); } - - // pcre2test (again) - - pcre2test.addConfigHeader(config_header); - pcre2test.addIncludePath(pcre2_header_dir.getDirectory()); - pcre2test.addIncludePath(b.path("src")); - - pcre2test.addCSourceFile(.{ - .file = b.path("src/pcre2test.c"), - }); - - pcre2test.linkLibrary(lib); - - b.installArtifact(pcre2test); } From 161bed58493d213807b438cdfa6a7c59799ee031 Mon Sep 17 00:00:00 2001 From: Bernard Assan Date: Wed, 25 Mar 2026 01:09:05 +0000 Subject: [PATCH 2/2] Cleanup based on review from https://github.com/PCRE2Project/pcre2/pull/894 remove unused config options make visibility actually work (tested to be working with my project https://github.com/bernardassan/Glib/tree/glib_tests) even though I was resisting, expose "pcre2posix.h" as a named LazyPath for users who want to use this api instead Add sanitize_c option Signed-off-by: Bernard Assan --- build.zig | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/build.zig b/build.zig index a025f4756..526845550 100644 --- a/build.zig +++ b/build.zig @@ -12,6 +12,7 @@ pub fn build(b: *std.Build) !void { const rt = target.result; const linkage = b.option(std.builtin.LinkMode, "linkage", "whether to statically or dynamically link the library") orelse @as(std.builtin.LinkMode, if (rt.isGnuLibC()) .dynamic else .static); + const sanitize_c = b.option(std.zig.SanitizeC, "sanitize_c", "whether to build with undefined behaviour sanitizer enabled") orelse .off; const codeUnitWidth = b.option(CodeUnitWidth, "code-unit-width", "Sets the code unit width") orelse .@"8"; const jit = b.option(bool, "support_jit", "Enable/disable JIT compiler support") orelse false; @@ -39,18 +40,16 @@ pub fn build(b: *std.Build) !void { .HAVE_SYS_TYPES_H = true, .HAVE_UNISTD_H = is_unix or is_mingw, .HAVE_WINDOWS_H = rt.os.tag == .windows, - .HAVE_MKOSTEMP = is_unix, + .HAVE_MEMFD_CREATE = is_musl or is_glibc or is_freebsd, .HAVE_SECURE_GETENV = is_musl or is_glibc or is_freebsd, - .HAVE_REALPATH = is_unix, // all compilation is using the Zig bundled c compiler - .HAVE_ATTRIBUTE_UNINITIALIZED = true, - .HAVE_VISIBILITY = true, .HAVE_BUILTIN_ASSUME = null, .HAVE_BUILTIN_MUL_OVERFLOW = true, .HAVE_BUILTIN_UNREACHABLE = true, - .INTEL_CET_ENABLED = null, + .HAVE_ATTRIBUTE_UNINITIALIZED = true, + .HAVE_VISIBILITY = true, .SUPPORT_PCRE2_8 = codeUnitWidth == .@"8", .SUPPORT_PCRE2_16 = codeUnitWidth == .@"16", @@ -78,6 +77,7 @@ pub fn build(b: *std.Build) !void { const lib_mod = b.createModule(.{ .target = target, .optimize = optimize, + .sanitize_c = sanitize_c, .link_libc = true, }); @@ -127,6 +127,9 @@ pub fn build(b: *std.Build) !void { "src/pcre2_valid_utf.c", "src/pcre2_xclass.c", }, + .flags = &.{ + "-fvisibility=hidden", + }, }); const lib = b.addLibrary(.{ @@ -203,6 +206,7 @@ pub fn build(b: *std.Build) !void { pcre2test_mod.linkLibrary(posixLib); + b.addNamedLazyPath("pcre2posix.h", b.path("src/pcre2posix.h")); posixLib.installHeader(b.path("src/pcre2posix.h"), "pcre2posix.h"); b.installArtifact(posixLib); }