Skip to content

Commit 0f3e120

Browse files
authored
Merge pull request #20 from lun-4/static-linking
use a source build of libpcre
2 parents 9522ad9 + ee76c3c commit 0f3e120

File tree

4 files changed

+48
-27
lines changed

4 files changed

+48
-27
lines changed

.github/workflows/zig.yml

+11
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ jobs:
2828
- run: brew install pcre
2929
- run: zig build
3030
- run: zig build test
31+
test-windows:
32+
runs-on: windows-latest
33+
steps:
34+
- uses: actions/checkout@v2
35+
with:
36+
submodules: recursive
37+
- uses: goto-bus-stop/setup-zig@v2
38+
with:
39+
version: 0.12.0
40+
- run: zig build -Dtarget=x86_64-windows-msvc
41+
- run: zig build -Dtarget=x86_64-windows-msvc test
3142
lint:
3243
runs-on: ubuntu-latest
3344
steps:

README.md

+8-11
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,29 @@
22

33
![Build status](https://github.com/kivikakk/libpcre.zig/workflows/build/badge.svg)
44

5-
To use via the zig package manager:
5+
Use via the zig package manager (Zig v0.12+):
66

77
```sh
88
$ zig fetch --save https://github.com/kivikakk/libpcre.zig/archive/<commit hash>.tar.gz
99
```
1010

11-
Then add the following to `build.zig` (system `pcre` will be linked against automatically):
11+
Then add the following to `build.zig` (a source build of `pcre` will be linked against automatically):
1212

1313
```zig
1414
const pcre_pkg = b.dependency("libpcre.zig", .{ .optimize = optimize, .target = target });
1515
const pcre_mod = pcre_pkg.module("libpcre");
1616
exe.root_module.addImport("pcre", pcre_mod);
1717
```
1818

19-
To use as a vendored library, add the following to your `build.zig`:
20-
21-
```zig
22-
const linkPcre = @import("vendor/libpcre.zig/build.zig").linkPcre;
23-
try linkPcre(exe);
24-
exe.addPackagePath("libpcre", "vendor/libpcre.zig/src/main.zig");
25-
```
26-
27-
Supported operating systems:
19+
To link against the system `libpcre`, add the `system_library` build option like this:
2820

21+
Note, only the following systems support this mode:
2922
* Linux: `apt install pkg-config libpcre3-dev`
3023
* macOS: `brew install pkg-config pcre`
3124
* ~~Windows: install [vcpkg](https://github.com/microsoft/vcpkg#quick-start-windows), `vcpkg integrate install`, `vcpkg install pcre --triplet x64-windows-static`~~
3225

3326
Zig doesn't have vcpkg integration any more. Suggestions welcome!
27+
28+
```zig
29+
const pcre_pkg = b.dependency("libpcre.zig", .{ .optimize = optimize, .target = target, .system_library = "true" });
30+
```

build.zig

+23-14
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,27 @@ const builtin = @import("builtin");
44
pub fn build(b: *std.Build) !void {
55
const optimize = b.standardOptimizeOption(.{});
66
const target = b.standardTargetOptions(.{});
7+
const use_system = b.option(bool, "system_library", "link against libpcre from the system instead of source build") orelse false;
8+
const pcre_dep = b.dependency("pcre", .{
9+
.optimize = optimize,
10+
.target = target,
11+
});
12+
const libpcre = pcre_dep.artifact("pcre");
713

814
const mod = b.addModule("libpcre", .{
915
.root_source_file = b.path("src/main.zig"),
1016
.optimize = optimize,
1117
.target = target,
1218
});
13-
try linkPcre(b, mod);
19+
try linkPcre(b, mod, libpcre, use_system);
1420

1521
const lib = b.addStaticLibrary(.{
1622
.name = "libpcre.zig",
1723
.root_source_file = b.path("src/main.zig"),
1824
.target = target,
1925
.optimize = optimize,
2026
});
21-
try linkPcre(b, &lib.root_module);
27+
try linkPcre(b, &lib.root_module, libpcre, use_system);
2228
b.installArtifact(lib);
2329

2430
const main_tests = b.addTest(.{
@@ -27,8 +33,7 @@ pub fn build(b: *std.Build) !void {
2733
.optimize = optimize,
2834
.target = target,
2935
});
30-
main_tests.linkLibC();
31-
try linkPcre(b, &main_tests.root_module);
36+
try linkPcre(b, &main_tests.root_module, libpcre, use_system);
3237

3338
const main_tests_run = b.addRunArtifact(main_tests);
3439
main_tests_run.step.dependOn(&main_tests.step);
@@ -37,18 +42,22 @@ pub fn build(b: *std.Build) !void {
3742
test_step.dependOn(&main_tests_run.step);
3843
}
3944

40-
pub fn linkPcre(b: *std.Build, mod: *std.Build.Module) !void {
41-
if (builtin.os.tag == .macos) {
42-
// If `pkg-config libpcre` doesn't error, linkSystemLibrary("libpcre") will succeed.
43-
// If it errors, try "pcre", as either it will hit a .pc by that name, or the fallthru
44-
// `-lpcre` and standard includes will work. (Or it's not installed.)
45-
var code: u8 = undefined;
46-
if (b.runAllowFail(&[_][]const u8{ "pkg-config", "libpcre" }, &code, .Inherit)) |_| {
47-
mod.linkSystemLibrary("libpcre", .{});
48-
} else |_| {
45+
pub fn linkPcre(b: *std.Build, mod: *std.Build.Module, libpcre: *std.Build.Step.Compile, use_system: bool) !void {
46+
if (use_system) {
47+
if (builtin.os.tag == .macos) {
48+
// If `pkg-config libpcre` doesn't error, linkSystemLibrary("libpcre") will succeed.
49+
// If it errors, try "pcre", as either it will hit a .pc by that name, or the fallthru
50+
// `-lpcre` and standard includes will work. (Or it's not installed.)
51+
var code: u8 = undefined;
52+
if (b.runAllowFail(&[_][]const u8{ "pkg-config", "libpcre" }, &code, .Inherit)) |_| {
53+
mod.linkSystemLibrary("libpcre", .{});
54+
} else |_| {
55+
mod.linkSystemLibrary("pcre", .{});
56+
}
57+
} else {
4958
mod.linkSystemLibrary("pcre", .{});
5059
}
5160
} else {
52-
mod.linkSystemLibrary("pcre", .{});
61+
mod.linkLibrary(libpcre);
5362
}
5463
}

build.zig.zon

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
.{
22
.name = "libpcre.zig",
33
.version = "0.1.0",
4-
.dependencies = .{},
4+
.dependencies = .{
5+
.pcre = .{
6+
.url = "https://github.com/lun-4/pcre-8.45/archive/999bbb603e2d4b05acfbdd82df638eb6664a95c7.tar.gz",
7+
.hash = "1220e7e5cb6837d9b0bc9b11f99d7a0660ea89490c41adcfede311196d9dc7e14066",
8+
},
9+
},
510
.minimum_zig_version = "0.12.0",
611
.paths = .{
712
"build.zig",
@@ -11,4 +16,3 @@
1116
"README.md",
1217
},
1318
}
14-

0 commit comments

Comments
 (0)