Skip to content

Commit

Permalink
Improve Zig build & support latest Zig
Browse files Browse the repository at this point in the history
Adds static linking, better handling of copied files, and support latest
Zig compiler.
  • Loading branch information
RossComputerGuy committed Feb 17, 2024
1 parent 3864abd commit de6d449
Showing 1 changed file with 72 additions and 44 deletions.
116 changes: 72 additions & 44 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,56 +1,84 @@
const std = @import("std");

pub const CodeUnitWidth = enum {
@"8",
@"16",
@"32",
};

pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const linkage = b.option(std.Build.Step.Compile.Linkage, "linkage", "whether to statically or dynamically link the library") orelse .static;
const codeUnitWidth = b.option(CodeUnitWidth, "code-unit-width", "Sets the code unit width") orelse .@"8";

const copyFiles = b.addWriteFiles();
copyFiles.addCopyFileToSource(.{ .path = "src/config.h.generic" }, "src/config.h");
copyFiles.addCopyFileToSource(.{ .path = "src/pcre2.h.generic" }, "src/pcre2.h");
copyFiles.addCopyFileToSource(.{ .path = "src/pcre2_chartables.c.dist" }, "src/pcre2_chartables.c");

const lib = b.addStaticLibrary(.{
.name = "pcre2",
.target = target,
.optimize = optimize,
_ = copyFiles.addCopyFile(.{ .path = "src/config.h.generic" }, "config.h");
_ = copyFiles.addCopyFile(.{ .path = "src/pcre2.h.generic" }, "pcre2.h");

const lib = std.Build.Step.Compile.create(b, .{
.name = b.fmt("pcre2-{s}", .{@tagName(codeUnitWidth)}),
.root_module = .{
.target = target,
.optimize = optimize,
.link_libc = true,
},
.kind = .lib,
.linkage = linkage,
});
lib.addIncludePath(.{ .path = "src" });
lib.addCSourceFiles(&.{
"src/pcre2_auto_possess.c",
"src/pcre2_chkdint.c",
"src/pcre2_compile.c",
"src/pcre2_config.c",
"src/pcre2_context.c",
"src/pcre2_convert.c",
"src/pcre2_dfa_match.c",
"src/pcre2_error.c",
"src/pcre2_extuni.c",
"src/pcre2_find_bracket.c",
"src/pcre2_maketables.c",
"src/pcre2_match.c",
"src/pcre2_match_data.c",
"src/pcre2_newline.c",
"src/pcre2_ord2utf.c",
"src/pcre2_pattern_info.c",
"src/pcre2_script_run.c",
"src/pcre2_serialize.c",
"src/pcre2_string_utils.c",
"src/pcre2_study.c",
"src/pcre2_substitute.c",
"src/pcre2_substring.c",
"src/pcre2_tables.c",
"src/pcre2_ucd.c",
"src/pcre2_valid_utf.c",
"src/pcre2_xclass.c",
"src/pcre2_chartables.c",
}, &.{
"-std=c99",
"-DHAVE_CONFIG_H",
"-DPCRE2_CODE_UNIT_WIDTH=8",
"-DPCRE2_STATIC",

if (linkage == .static) {
try lib.root_module.c_macros.append(b.allocator, "-DPCRE2_STATIC");
}

lib.root_module.addCMacro("PCRE2_CODE_UNIT_WIDTH", @tagName(codeUnitWidth));

lib.addCSourceFile(.{
.file = copyFiles.addCopyFile(.{ .path = "src/pcre2_chartables.c.dist" }, "pcre2_chartables.c"),
.flags = &.{
"-DHAVE_CONFIG_H",
},
});
lib.step.dependOn(&copyFiles.step);

lib.addIncludePath(.{ .path = b.pathFromRoot("src") });
lib.addIncludePath(copyFiles.getDirectory());

lib.addCSourceFiles(.{
.files = &.{
b.pathFromRoot("src/pcre2_auto_possess.c"),
b.pathFromRoot("src/pcre2_chkdint.c"),
b.pathFromRoot("src/pcre2_compile.c"),
b.pathFromRoot("src/pcre2_config.c"),
b.pathFromRoot("src/pcre2_context.c"),
b.pathFromRoot("src/pcre2_convert.c"),
b.pathFromRoot("src/pcre2_dfa_match.c"),
b.pathFromRoot("src/pcre2_error.c"),
b.pathFromRoot("src/pcre2_extuni.c"),
b.pathFromRoot("src/pcre2_find_bracket.c"),
b.pathFromRoot("src/pcre2_maketables.c"),
b.pathFromRoot("src/pcre2_match.c"),
b.pathFromRoot("src/pcre2_match_data.c"),
b.pathFromRoot("src/pcre2_newline.c"),
b.pathFromRoot("src/pcre2_ord2utf.c"),
b.pathFromRoot("src/pcre2_pattern_info.c"),
b.pathFromRoot("src/pcre2_script_run.c"),
b.pathFromRoot("src/pcre2_serialize.c"),
b.pathFromRoot("src/pcre2_string_utils.c"),
b.pathFromRoot("src/pcre2_study.c"),
b.pathFromRoot("src/pcre2_substitute.c"),
b.pathFromRoot("src/pcre2_substring.c"),
b.pathFromRoot("src/pcre2_tables.c"),
b.pathFromRoot("src/pcre2_ucd.c"),
b.pathFromRoot("src/pcre2_valid_utf.c"),
b.pathFromRoot("src/pcre2_xclass.c"),
},
.flags = &.{
"-DHAVE_CONFIG_H",
"-DPCRE2_CODE_UNIT_WIDTH=8",
"-DPCRE2_STATIC",
},
});

lib.installHeader("src/pcre2.h.generic", "pcre2.h");
lib.linkLibC();
b.installArtifact(lib);
}

0 comments on commit de6d449

Please sign in to comment.