Skip to content

Commit 413bd8a

Browse files
authored
Add zig build to CI (#622)
It's not ideal, but at least it's being tested. I have added support for pcre2test, so that we can run the unit tests against the library which Zig builds.
1 parent 0ed421c commit 413bd8a

File tree

3 files changed

+135
-21
lines changed

3 files changed

+135
-21
lines changed

.github/workflows/dev.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,31 @@ jobs:
274274
- name: Test
275275
run: cd build && ctest -j3 --output-on-failure
276276

277+
zebrilus:
278+
# Tests with: Zig compiler
279+
name: Zig
280+
runs-on: ubuntu-latest
281+
if: github.event_name != 'pull_request'
282+
steps:
283+
- name: Setup
284+
run: |
285+
sudo snap install zig --classic --beta
286+
287+
- name: Checkout
288+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
289+
with:
290+
submodules: true
291+
292+
- name: Build
293+
run: zig build
294+
295+
- name: Test
296+
run: |
297+
# Zig does something weird with the stack - it uses more space than the
298+
# equivalent plain C program.
299+
ulimit -S -s 16384
300+
srcdir=`pwd` pcre2test=`pwd`/zig-out/bin/pcre2test ./RunTest
301+
277302
bazel:
278303
# Tests with: Bazel build system
279304
name: Bazel

build.zig

Lines changed: 106 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,39 @@ pub fn build(b: *std.Build) !void {
1212
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);
1313
const codeUnitWidth = b.option(CodeUnitWidth, "code-unit-width", "Sets the code unit width") orelse .@"8";
1414

15-
const copyFiles = b.addWriteFiles();
16-
_ = copyFiles.addCopyFile(b.path("src/config.h.generic"), "config.h");
17-
_ = copyFiles.addCopyFile(b.path("src/pcre2.h.generic"), "pcre2.h");
15+
const pcre2_header_dir = b.addWriteFiles();
16+
const pcre2_header = pcre2_header_dir.addCopyFile(b.path("src/pcre2.h.generic"), "pcre2.h");
17+
18+
const config_header = b.addConfigHeader(
19+
.{
20+
.style = .{ .cmake = b.path("config-cmake.h.in") },
21+
.include_path = "config.h",
22+
},
23+
.{
24+
.HAVE_ASSERT_H = true,
25+
.HAVE_UNISTD_H = (target.result.os.tag != .windows),
26+
.HAVE_WINDOWS_H = (target.result.os.tag == .windows),
27+
28+
.HAVE_MEMMOVE = true,
29+
.HAVE_STRERROR = true,
30+
31+
.SUPPORT_PCRE2_8 = codeUnitWidth == CodeUnitWidth.@"8",
32+
.SUPPORT_PCRE2_16 = codeUnitWidth == CodeUnitWidth.@"16",
33+
.SUPPORT_PCRE2_32 = codeUnitWidth == CodeUnitWidth.@"32",
34+
.SUPPORT_UNICODE = true,
35+
36+
.PCRE2_EXPORT = null,
37+
.PCRE2_LINK_SIZE = 2,
38+
.PCRE2_HEAP_LIMIT = 20000000,
39+
.PCRE2_MATCH_LIMIT = 10000000,
40+
.PCRE2_MATCH_LIMIT_DEPTH = "MATCH_LIMIT",
41+
.PCRE2_MAX_VARLOOKBEHIND = 255,
42+
.NEWLINE_DEFAULT = 2,
43+
.PCRE2_PARENS_NEST_LIMIT = 250,
44+
},
45+
);
46+
47+
// pcre2-8/16/32.so
1848

1949
const lib = std.Build.Step.Compile.create(b, .{
2050
.name = b.fmt("pcre2-{s}", .{@tagName(codeUnitWidth)}),
@@ -27,22 +57,20 @@ pub fn build(b: *std.Build) !void {
2757
.linkage = linkage,
2858
});
2959

60+
lib.defineCMacro("HAVE_CONFIG_H", null);
61+
lib.defineCMacro("PCRE2_CODE_UNIT_WIDTH", @tagName(codeUnitWidth));
3062
if (linkage == .static) {
31-
try lib.root_module.c_macros.append(b.allocator, "-DPCRE2_STATIC");
63+
lib.defineCMacro("PCRE2_STATIC", null);
3264
}
3365

34-
lib.root_module.addCMacro("PCRE2_CODE_UNIT_WIDTH", @tagName(codeUnitWidth));
66+
lib.addConfigHeader(config_header);
67+
lib.addIncludePath(pcre2_header_dir.getDirectory());
68+
lib.addIncludePath(b.path("src"));
3569

3670
lib.addCSourceFile(.{
37-
.file = copyFiles.addCopyFile(b.path("src/pcre2_chartables.c.dist"), "pcre2_chartables.c"),
38-
.flags = &.{
39-
"-DHAVE_CONFIG_H",
40-
},
71+
.file = b.addWriteFiles().addCopyFile(b.path("src/pcre2_chartables.c.dist"), "pcre2_chartables.c"),
4172
});
4273

43-
lib.addIncludePath(b.path("src"));
44-
lib.addIncludePath(copyFiles.getDirectory());
45-
4674
lib.addCSourceFiles(.{
4775
.files = &.{
4876
"src/pcre2_auto_possess.c",
@@ -56,6 +84,7 @@ pub fn build(b: *std.Build) !void {
5684
"src/pcre2_error.c",
5785
"src/pcre2_extuni.c",
5886
"src/pcre2_find_bracket.c",
87+
"src/pcre2_jit_compile.c",
5988
"src/pcre2_maketables.c",
6089
"src/pcre2_match.c",
6190
"src/pcre2_match_data.c",
@@ -73,12 +102,72 @@ pub fn build(b: *std.Build) !void {
73102
"src/pcre2_valid_utf.c",
74103
"src/pcre2_xclass.c",
75104
},
76-
.flags = &.{
77-
"-DHAVE_CONFIG_H",
78-
"-DPCRE2_STATIC",
79-
},
80105
});
81106

82-
lib.installHeader(b.path("src/pcre2.h.generic"), "pcre2.h");
107+
lib.installHeader(pcre2_header, "pcre2.h");
83108
b.installArtifact(lib);
109+
110+
111+
// pcre2test
112+
113+
const pcre2test = b.addExecutable(.{
114+
.name = "pcre2test",
115+
.target = target,
116+
.optimize = optimize,
117+
});
118+
119+
120+
// pcre2-posix.so
121+
122+
if (codeUnitWidth == CodeUnitWidth.@"8") {
123+
const posixLib = std.Build.Step.Compile.create(b, .{
124+
.name = "pcre2-posix",
125+
.root_module = .{
126+
.target = target,
127+
.optimize = optimize,
128+
.link_libc = true,
129+
},
130+
.kind = .lib,
131+
.linkage = linkage,
132+
});
133+
134+
posixLib.defineCMacro("HAVE_CONFIG_H", null);
135+
posixLib.defineCMacro("PCRE2_CODE_UNIT_WIDTH", @tagName(codeUnitWidth));
136+
if (linkage == .static) {
137+
posixLib.defineCMacro("PCRE2_STATIC", null);
138+
}
139+
140+
posixLib.addConfigHeader(config_header);
141+
posixLib.addIncludePath(pcre2_header_dir.getDirectory());
142+
posixLib.addIncludePath(b.path("src"));
143+
144+
posixLib.addCSourceFiles(.{
145+
.files = &.{
146+
"src/pcre2posix.c",
147+
},
148+
});
149+
150+
posixLib.installHeader(b.path("src/pcre2posix.h"), "pcre2posix.h");
151+
b.installArtifact(posixLib);
152+
153+
pcre2test.linkLibrary(posixLib);
154+
}
155+
156+
157+
// pcre2test (again)
158+
159+
pcre2test.defineCMacro("HAVE_CONFIG_H", null);
160+
161+
pcre2test.addConfigHeader(config_header);
162+
pcre2test.addIncludePath(pcre2_header_dir.getDirectory());
163+
pcre2test.addIncludePath(b.path("src"));
164+
165+
pcre2test.addCSourceFile(.{
166+
.file = b.path("src/pcre2test.c"),
167+
});
168+
169+
pcre2test.linkLibC();
170+
pcre2test.linkLibrary(lib);
171+
172+
b.installArtifact(pcre2test);
84173
}

config-cmake.h.in

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@
4141
#cmakedefine HEAP_MATCH_RECURSE 1
4242
#cmakedefine NEVER_BACKSLASH_C 1
4343

44-
#define PCRE2_EXPORT @PCRE2_EXPORT@
45-
#define LINK_SIZE @PCRE2_LINK_SIZE@
44+
#define PCRE2_EXPORT @PCRE2_EXPORT@
45+
#define LINK_SIZE @PCRE2_LINK_SIZE@
4646
#define HEAP_LIMIT @PCRE2_HEAP_LIMIT@
47-
#define MATCH_LIMIT @PCRE2_MATCH_LIMIT@
48-
#define MATCH_LIMIT_DEPTH @PCRE2_MATCH_LIMIT_DEPTH@
47+
#define MATCH_LIMIT @PCRE2_MATCH_LIMIT@
48+
#define MATCH_LIMIT_DEPTH @PCRE2_MATCH_LIMIT_DEPTH@
4949
#define MAX_VARLOOKBEHIND @PCRE2_MAX_VARLOOKBEHIND@
5050
#define NEWLINE_DEFAULT @NEWLINE_DEFAULT@
5151
#define PARENS_NEST_LIMIT @PCRE2_PARENS_NEST_LIMIT@

0 commit comments

Comments
 (0)