@@ -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}
0 commit comments