Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Techatrix committed Dec 18, 2024
0 parents commit 57aa500
Show file tree
Hide file tree
Showing 7 changed files with 410 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.zig text eol=lf
*.zon text eol=lf
41 changes: 41 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: CI

on:
push:
branches:
- master
pull_request:
branches:
- master
workflow_dispatch:

jobs:
build:
strategy:
fail-fast: false
matrix:
zig-version: [master]
os: [ubuntu-latest, macos-latest, windows-latest]
include:
- zig-version: "0.12.1"
os: ubuntu-latest
- zig-version: "0.13.0"
os: ubuntu-latest
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Zig
uses: mlugg/setup-zig@v1
with:
version: ${{ matrix.zig-version }}

- name: Check Formatting
run: zig fmt --ast-check --check .

- name: Build
run: zig build --summary all

- name: Run Tests
run: zig build test --summary all
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.zig-cache
zig-cache
zig-out
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (Expat)

Copyright (c) contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[![CI](https://github.com/allyourcodebase/libexpat/actions/workflows/ci.yaml/badge.svg)](https://github.com/allyourcodebase/libexpat/actions)

# Expat

This is [Expat](https://github.com/libexpat/libexpat), packaged for [Zig](https://ziglang.org/).

## Installation

First, update your `build.zig.zon`:

```
# Initialize a `zig build` project if you haven't already
zig init
zig fetch --save git+https://github.com/allyourcodebase/libexpat.git
```

You can then import `expat` in your `build.zig` with:

```zig
const expat_dependency = b.dependency("expat", .{
.target = target,
.optimize = optimize,
});
your_exe.linkLibrary(expat_dependency.artifact("expat"));
```
301 changes: 301 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,301 @@
const std = @import("std");

const version: std.SemanticVersion = .{ .major = 2, .minor = 6, .patch = 4 };

pub fn build(b: *std.Build) void {
const upstream = b.dependency("libexpat", .{});
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

// Most of these config options have not been tested.

var context_bytes = b.option(i64, "context-bytes", "Define to specify how much context to retain around the current parse point, 0 to disable") orelse 1024;
if (context_bytes < 0) context_bytes = 0;

const CharType = enum {
char,
ushort,
wchar_t,
};

const char_type = b.option(CharType, "char-type", "Character type to use. (default=char)") orelse .char;
const dtd = b.option(bool, "dtd", "Define to make parameter entity parsing functionality available") orelse false;
const ge = b.option(bool, "ge", "Define to make general entity parsing functionality available") orelse false;
const ns = b.option(bool, "ns", "Define to make XML Namespaces functionality available") orelse false;
const attr_info = b.option(bool, "attr-info", "Define to allow retrieving the byte offsets for attribute names and values") orelse false;
const large_size = b.option(bool, "large-size", "Make XML_GetCurrent* functions return <(unsigned) long long> rather than <(unsigned) long>") orelse false;
const min_size = b.option(bool, "min-size", "Get a smaller (but slower) parser (in particular avoid multiple copies of the tokenizer)") orelse false;

if (dtd and !ge) {
std.log.err("Option `dtd` requires that `ge` is also enabled.", .{});
std.log.err("Please either enable option `ge` (recommended) or disable `dtd` also.\n", .{});
b.invalid_user_input = true;
return;
}

const need_short_char_arg = char_type == .wchar_t and target.result.os.tag != .windows;

const examples_dir: std.Build.Step.InstallArtifact.Options.Dir = .{ .override = .{ .custom = "examples" } };

b.getInstallStep().dependOn(&b.addInstallFileWithDir(upstream.path("expat/AUTHORS"), .prefix, "AUTHORS").step);
b.getInstallStep().dependOn(&b.addInstallFileWithDir(upstream.path("expat/Changes"), .prefix, "changelog").step);

const config_header = b.addConfigHeader(.{
.style = .{ .cmake = upstream.path("expat/expat_config.h.cmake") },
.include_path = "expat_config.h",
}, .{
.BYTEORDER = @as(i64, switch (target.result.cpu.arch.endian()) {
.little => 1234,
.big => 4321,
}),
.HAVE_ARC4RANDOM = null,
.HAVE_ARC4RANDOM_BUF = switch (target.result.os.tag) {
.dragonfly,
.netbsd,
.freebsd,
.solaris,
.openbsd,
.macos,
.ios,
.tvos,
.watchos,
// .visionos, // not available on Zig 0.12
=> true,
else => false,
},
.HAVE_DLFCN_H = true,
.HAVE_FCNTL_H = true,
.HAVE_GETPAGESIZE = true,
.HAVE_GETRANDOM = switch (target.result.os.tag) {
.linux, .freebsd => true,
else => false,
},
.HAVE_INTTYPES_H = true,
.HAVE_LIBBSD = null,
.HAVE_MEMORY_H = true,
.HAVE_MMAP = true,
.HAVE_STDINT_H = true,
.HAVE_STDLIB_H = true,
.HAVE_STRINGS_H = true,
.HAVE_STRING_H = true,
.HAVE_SYSCALL_GETRANDOM = target.result.os.tag == .linux,
.HAVE_SYS_STAT_H = true,
.HAVE_SYS_TYPES_H = true,
.HAVE_UNISTD_H = true,
.PACKAGE = "expat",
.PACKAGE_BUGREPORT = "https://github.com/libexpat/libexpat/issues",
.PACKAGE_NAME = "expat",
.PACKAGE_STRING = b.fmt("expat {}", .{version}),
.PACKAGE_TARNAME = "expat",
.PACKAGE_URL = "",
.PACKAGE_VERSION = b.fmt("{}", .{version}),
.STDC_HEADERS = true,
.WORDS_BIGENDIAN = null,
.XML_ATTR_INFO = attr_info,
.XML_CONTEXT_BYTES = context_bytes,
.XML_DEV_URANDOM = target.result.os.tag != .windows,
.XML_DTD = dtd,
.XML_GE = ge,
.XML_NS = ns,
.off_t = null,
});

const expat = b.addStaticLibrary(.{
.name = "expat",
.target = target,
.optimize = optimize,
.link_libc = true,
});
b.installArtifact(expat);
expat.linkLibC();
expat.addConfigHeader(config_header);
if (large_size) expat.root_module.addCMacro("XML_LARGE_SIZE", "1");
if (min_size) expat.root_module.addCMacro("XML_MIN_SIZE", "1");
if (char_type != .char) expat.root_module.addCMacro("XML_UNICODE", "1");
if (char_type == .wchar_t) expat.root_module.addCMacro("XML_UNICODE_WCHAR_T", "1");
expat.addIncludePath(upstream.path("expat/lib"));
expat.installHeader(upstream.path("expat/lib/expat.h"), "expat.h");
expat.installHeader(upstream.path("expat/lib/expat_external.h"), "expat_external.h");
expat.addCSourceFiles(.{
.files = sources,
.root = upstream.path("expat"),
.flags = if (need_short_char_arg) &.{ "-std=c99", "-fshort-wchar" } else &.{"-std=c99"},
});

const xmlwf = b.addExecutable(.{
.name = "xmlwf",
.target = target,
.optimize = optimize,
});
xmlwf.addConfigHeader(config_header);
xmlwf.addIncludePath(upstream.path("expat/lib"));
if (char_type != .char) xmlwf.root_module.addCMacro("XML_UNICODE", "1");
if (char_type == .wchar_t) xmlwf.root_module.addCMacro("XML_UNICODE_WCHAR_T", "1");
xmlwf.addCSourceFiles(.{
.files = xmlwf_sources,
.root = upstream.path("expat"),
.flags = if (need_short_char_arg) &.{"-fshort-wchar"} else &.{},
});
xmlwf.linkLibrary(expat);

switch (char_type) {
.char => {},
.ushort => @panic("The xmlwf tool can not be built with option -Dchar-type=ushort. Please pass -Dchar-type=(char|wchar_t)"),
.wchar_t => if (target.result.os.tag != .windows) {
@panic("The xmlwf tool can not be built with option -Dchar-type=wchar_t outside of Windows. Please pass -Dchar-type=char");
},
}

const run_xmlwf_cmd = b.addRunArtifact(xmlwf);

if (b.args) |args| {
run_xmlwf_cmd.addArgs(args);
}

const xmlwf_step = b.step("xmlwf", "run xmlwf");
xmlwf_step.dependOn(&run_xmlwf_cmd.step);

const examples_step = b.step("examples", "Build examples");

for (examples) |source| {
const exe = b.addExecutable(.{
.name = std.fs.path.stem(source),
.target = target,
.optimize = optimize,
});
if (char_type != .char) exe.root_module.addCMacro("XML_UNICODE", "1");
if (char_type == .wchar_t) exe.root_module.addCMacro("XML_UNICODE_WCHAR_T", "1");
exe.addCSourceFile(.{
.file = upstream.path(b.fmt("expat/{s}", .{source})),
.flags = if (need_short_char_arg) &.{"-fshort-wchar"} else &.{},
});
exe.linkLibrary(expat);

examples_step.dependOn(&b.addInstallArtifact(exe, .{ .dest_dir = examples_dir }).step);

if (char_type == .ushort) {
@panic("Examples can not be built with option -Dchar-type=ushort. Please pass -Dchar-type=(char|wchar_t)");
}
}

const test_step = b.step("test", "Run unit tests");

for (
[_][]const u8{ "runtests", "runtests_cxx" },
[_][]const []const u8{ runtests_sources, runtests_cxx_sources },
[_]bool{ false, true },
) |name, test_sources, link_cpp| {
var flags: std.ArrayListUnmanaged([]const u8) = .{};
if (link_cpp) flags.append(b.allocator, "-std=c++11") catch @panic("OOM");
if (need_short_char_arg) flags.append(b.allocator, "-fshort-wchar") catch @panic("OOM");

const test_exe = b.addExecutable(.{
.name = name,
.target = target,
.optimize = optimize,
});
test_exe.linkLibC();
if (link_cpp) test_exe.linkLibCpp();
test_exe.root_module.addCMacro("XML_TESTING", "1");
if (char_type != .char) test_exe.root_module.addCMacro("XML_UNICODE", "1");
if (char_type == .wchar_t) test_exe.root_module.addCMacro("XML_UNICODE_WCHAR_T", "1");
test_exe.addCSourceFiles(.{
.flags = flags.items,
.root = upstream.path("expat"),
.files = test_sources,
});
test_exe.addConfigHeader(config_header);
test_exe.addIncludePath(upstream.path("expat/lib"));
if (large_size) test_exe.root_module.addCMacro("XML_LARGE_SIZE", "1");
if (min_size) test_exe.root_module.addCMacro("XML_MIN_SIZE", "1");
test_exe.addCSourceFiles(.{
.files = sources,
.root = upstream.path("expat"),
.flags = if (need_short_char_arg) &.{ "-std=c99", "-fshort-wchar" } else &.{"-std=c99"},
});

if (char_type == .ushort) {
@panic("The testsuite can not be built with option -Dchar-type=ushort. Please pass -Dchar-type=(char|wchar_t)");
}

const run_test_cmd = b.addRunArtifact(test_exe);
if (b.args) |args| {
run_test_cmd.addArgs(args);
}
test_step.dependOn(&run_test_cmd.step);
}

const benchmark = b.addExecutable(.{
.name = "benchmark",
.target = target,
.optimize = optimize,
});
if (char_type != .char) benchmark.root_module.addCMacro("XML_UNICODE", "1");
if (char_type == .wchar_t) benchmark.root_module.addCMacro("XML_UNICODE_WCHAR_T", "1");
benchmark.addCSourceFile(.{
.file = upstream.path("expat/tests/benchmark/benchmark.c"),
.flags = if (need_short_char_arg) &.{"-fshort-wchar"} else &.{},
});
benchmark.linkLibrary(expat);

const run_benchmark_cmd = b.addRunArtifact(benchmark);
if (b.args) |args| {
run_benchmark_cmd.addArgs(args);
}

const benchmark_step = b.step("benchmark", "Run benchmark");
benchmark_step.dependOn(&run_benchmark_cmd.step);
}

const sources: []const []const u8 = &.{
"lib/xmlparse.c",
"lib/xmlrole.c",
"lib/xmltok.c",
};

const xmlwf_sources: []const []const u8 = &.{
"xmlwf/codepage.c",
"xmlwf/readfilemap.c",
"xmlwf/xmlfile.c",
"xmlwf/xmlwf.c",
};

const examples: []const []const u8 = &.{
"examples/element_declarations.c",
"examples/elements.c",
"examples/outline.c",
};

const runtests_sources: []const []const u8 = &.{
"tests/acc_tests.c",
"tests/alloc_tests.c",
"tests/basic_tests.c",
"tests/chardata.c",
"tests/common.c",
"tests/dummy.c",
"tests/handlers.c",
"tests/memcheck.c",
"tests/minicheck.c",
"tests/misc_tests.c",
"tests/ns_tests.c",
"tests/nsalloc_tests.c",
"tests/runtests.c",
"tests/structdata.c",
};

const runtests_cxx_sources: []const []const u8 = &.{
"tests/acc_tests_cxx.cpp",
"tests/alloc_tests_cxx.cpp",
"tests/basic_tests_cxx.cpp",
"tests/chardata_cxx.cpp",
"tests/common_cxx.cpp",
"tests/dummy_cxx.cpp",
"tests/handlers_cxx.cpp",
"tests/memcheck_cxx.cpp",
"tests/minicheck_cxx.cpp",
"tests/misc_tests_cxx.cpp",
"tests/ns_tests_cxx.cpp",
"tests/nsalloc_tests_cxx.cpp",
"tests/runtests_cxx.cpp",
"tests/structdata_cxx.cpp",
};
Loading

0 comments on commit 57aa500

Please sign in to comment.