xref: /aosp_15_r20/external/pcre/build.zig (revision 22dc650d8ae982c6770746019a6f94af92b0f024)
1const std = @import("std");
2
3pub const CodeUnitWidth = enum {
4    @"8",
5    @"16",
6    @"32",
7};
8
9pub fn build(b: *std.Build) !void {
10    const target = b.standardTargetOptions(.{});
11    const optimize = b.standardOptimizeOption(.{});
12    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);
13    const codeUnitWidth = b.option(CodeUnitWidth, "code-unit-width", "Sets the code unit width") orelse .@"8";
14
15    const copyFiles = b.addWriteFiles();
16    _ = copyFiles.addCopyFile(.{ .path = "src/config.h.generic" }, "config.h");
17    _ = copyFiles.addCopyFile(.{ .path = "src/pcre2.h.generic" }, "pcre2.h");
18
19    const lib = std.Build.Step.Compile.create(b, .{
20        .name = b.fmt("pcre2-{s}", .{@tagName(codeUnitWidth)}),
21        .root_module = .{
22            .target = target,
23            .optimize = optimize,
24            .link_libc = true,
25        },
26        .kind = .lib,
27        .linkage = linkage,
28    });
29
30    if (linkage == .static) {
31        try lib.root_module.c_macros.append(b.allocator, "-DPCRE2_STATIC");
32    }
33
34    lib.root_module.addCMacro("PCRE2_CODE_UNIT_WIDTH", @tagName(codeUnitWidth));
35
36    lib.addCSourceFile(.{
37        .file = copyFiles.addCopyFile(.{ .path = "src/pcre2_chartables.c.dist" }, "pcre2_chartables.c"),
38        .flags = &.{
39            "-DHAVE_CONFIG_H",
40        },
41    });
42
43    lib.addIncludePath(.{ .path = b.pathFromRoot("src") });
44    lib.addIncludePath(copyFiles.getDirectory());
45
46    lib.addCSourceFiles(.{
47        .files = &.{
48            "src/pcre2_auto_possess.c",
49            "src/pcre2_chkdint.c",
50            "src/pcre2_compile.c",
51            "src/pcre2_config.c",
52            "src/pcre2_context.c",
53            "src/pcre2_convert.c",
54            "src/pcre2_dfa_match.c",
55            "src/pcre2_error.c",
56            "src/pcre2_extuni.c",
57            "src/pcre2_find_bracket.c",
58            "src/pcre2_maketables.c",
59            "src/pcre2_match.c",
60            "src/pcre2_match_data.c",
61            "src/pcre2_newline.c",
62            "src/pcre2_ord2utf.c",
63            "src/pcre2_pattern_info.c",
64            "src/pcre2_script_run.c",
65            "src/pcre2_serialize.c",
66            "src/pcre2_string_utils.c",
67            "src/pcre2_study.c",
68            "src/pcre2_substitute.c",
69            "src/pcre2_substring.c",
70            "src/pcre2_tables.c",
71            "src/pcre2_ucd.c",
72            "src/pcre2_valid_utf.c",
73            "src/pcre2_xclass.c",
74        },
75        .flags = &.{
76            "-DHAVE_CONFIG_H",
77            "-DPCRE2_STATIC",
78        },
79    });
80
81    lib.installHeader(.{ .path = "src/pcre2.h.generic" }, "pcre2.h");
82    b.installArtifact(lib);
83}
84