Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add build.zig #254

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
const std = @import("std");

const VERSION: std.SemanticVersion = .{
.major = 0,
.minor = 4,
.patch = 8,
};

pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

// On Windows, given there is no standard lib install dir etc., we rather
// by default build static lib.
const build_shared = b.option(
bool,
"md4c-shared",
"Build md4c as a shared library",
) orelse !target.result.isMinGW();

// build options
var with_utf8 = b.option(bool, "utf8", "Use UTF8") orelse false;
const with_utf16 = b.option(bool, "utf16", "Use UTF16") orelse false;
const with_ascii = b.option(bool, "ascii", "Use UTF8") orelse false;

// defaults to UTF8 if nothing else set
if (!with_utf8 and !with_utf16 and !with_ascii) {
with_utf8 = true;
}

const lib = if (build_shared)
b.addSharedLibrary(.{
.name = "md4c",
.target = target,
.optimize = optimize,
.version = VERSION,
})
else
b.addStaticLibrary(.{
.name = "md4c",
.target = target,
.optimize = optimize,
});

lib.addCSourceFiles(.{
.files = &md4c_sources,
.flags = &md4c_flags,
});

setDefines(lib, with_utf8, with_utf16, with_ascii);
lib.linkLibC();
lib.installHeader(b.path("src/md4c.h"), "md4c.h");
b.installArtifact(lib);

const exe = b.addExecutable(.{
.name = "md2html",
.target = target,
.optimize = optimize,
});

setDefines(exe, with_utf8, with_utf16, with_ascii);
exe.addCSourceFiles(.{
.files = &md2html_sources,
.flags = &md4c_flags,
});
exe.linkLibC();

const install_exe = b.addInstallArtifact(exe, .{});
const md2html_step = b.step("md2html", "Compile the md2html executable");
md2html_step.dependOn(&install_exe.step);
}

fn setDefines(
lib_or_exe: *std.Build.Step.Compile,
with_utf8: bool,
with_utf16: bool,
with_ascii: bool,
) void {
lib_or_exe.root_module.addCMacro(
"MD_VERSION_MAJOR",
std.fmt.comptimePrint("{d}", .{VERSION.major}),
);
lib_or_exe.root_module.addCMacro(
"MD_VERSION_MINOR",
std.fmt.comptimePrint("{d}", .{VERSION.minor}),
);
lib_or_exe.root_module.addCMacro(
"MD_VERSION_RELEASE",
std.fmt.comptimePrint("{d}", .{VERSION.patch}),
);

if (with_utf8) {
lib_or_exe.defineCMacro("MD4C_USE_UTF8", "1");
} else if (with_ascii) {
lib_or_exe.defineCMacro("MD4C_USE_ASCII", "1");
} else if (with_utf16) {
lib_or_exe.defineCMacro("MD4C_USE_UTF16", "1");
}
}

const md4c_flags = [_][]const u8{
"-Wall",
};

const md4c_sources = [_][]const u8{
"src/md4c.c",
};

const md2html_sources = [_][]const u8{
"src/md4c.c",
"src/md4c-html.c",
"src/entity.c",
"md2html/cmdline.c",
"md2html/md2html.c",
};
2 changes: 1 addition & 1 deletion src/md4c.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ typedef enum MD_TEXTTYPE {
MD_TEXT_SOFTBR, /* '\n' in source text where it is not semantically meaningful (soft break) */

/* Entity.
* (a) Named entity, e.g.  
* (a) Named entity, e.g.  
* (Note MD4C does not have a list of known entities.
* Anything matching the regexp /&[A-Za-z][A-Za-z0-9]{1,47};/ is
* treated as a named entity.)
Expand Down