@@ -4,21 +4,27 @@ const builtin = @import("builtin");
4
4
pub fn build (b : * std.Build ) ! void {
5
5
const optimize = b .standardOptimizeOption (.{});
6
6
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" );
7
13
8
14
const mod = b .addModule ("libpcre" , .{
9
15
.root_source_file = b .path ("src/main.zig" ),
10
16
.optimize = optimize ,
11
17
.target = target ,
12
18
});
13
- try linkPcre (b , mod );
19
+ try linkPcre (b , mod , libpcre , use_system );
14
20
15
21
const lib = b .addStaticLibrary (.{
16
22
.name = "libpcre.zig" ,
17
23
.root_source_file = b .path ("src/main.zig" ),
18
24
.target = target ,
19
25
.optimize = optimize ,
20
26
});
21
- try linkPcre (b , & lib .root_module );
27
+ try linkPcre (b , & lib .root_module , libpcre , use_system );
22
28
b .installArtifact (lib );
23
29
24
30
const main_tests = b .addTest (.{
@@ -27,8 +33,7 @@ pub fn build(b: *std.Build) !void {
27
33
.optimize = optimize ,
28
34
.target = target ,
29
35
});
30
- main_tests .linkLibC ();
31
- try linkPcre (b , & main_tests .root_module );
36
+ try linkPcre (b , & main_tests .root_module , libpcre , use_system );
32
37
33
38
const main_tests_run = b .addRunArtifact (main_tests );
34
39
main_tests_run .step .dependOn (& main_tests .step );
@@ -37,18 +42,22 @@ pub fn build(b: *std.Build) !void {
37
42
test_step .dependOn (& main_tests_run .step );
38
43
}
39
44
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 {
49
58
mod .linkSystemLibrary ("pcre" , .{});
50
59
}
51
60
} else {
52
- mod .linkSystemLibrary ( "pcre" , .{} );
61
+ mod .linkLibrary ( libpcre );
53
62
}
54
63
}
0 commit comments