Skip to content

Commit

Permalink
Update Zig, Adopt 0.11 Package Manager (#52)
Browse files Browse the repository at this point in the history
This updates Zig to nearly 0.11 (likely one or two days away). This also
enforces the Zig package manager as the one true way.
  • Loading branch information
mitchellh committed Aug 3, 2023
2 parents a3e5c9b + ab31ced commit 0937e81
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 45 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,34 @@ int main(void) {
</tr>
</table>

## Installation (Zig)

**These instructions are for Zig downstream users only.** If you are
using the C API to libxev, see the "Build" section.

This package works with Zig package manager introduces in Zig 0.11.
Create a `build.zig.zon` file like this:

```zig
.{
.name = "my-project",
.version = "0.0.0",
.dependencies = .{
.libxev = .{
.url = "https://github.com/mitchellh/libxev/archive/<git-ref-here>.tar.gz",
.hash = "12208070233b17de6be05e32af096a6760682b48598323234824def41789e993432c",
},
},
}
```

And in your `build.zig`:

```zig
const xev = b.dependency("libxev", .{ .target = target, .optimize = optimize });
exe.addModule("xev", xev.module("xev"));
```

## Documentation

🚧 Documentation is a work-in-progress. 🚧
Expand Down
84 changes: 42 additions & 42 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,12 @@ const std = @import("std");
const LibExeObjStep = std.build.LibExeObjStep;
const ScdocStep = @import("src/build/ScdocStep.zig");

/// DEPRECATED: This is the old std.build.Pkg for older versions of Zig.
/// I won't keep this around too long but there is no harm in exposing it
/// for now since our code works with older versions just fine (for now).
pub const pkg = std.build.Pkg{
.name = "xev",
.source = .{ .path = thisDir() ++ "/src/main.zig" },
};

/// Returns the module for libxev. The recommended approach is to depend
/// on libxev in your build.zig.zon file, then use
/// `b.dependency("libxev").module("xev")`. But if you're not using
/// a build.zig.zon yet this will work.
pub fn module(b: *std.Build) *std.Build.Module {
return b.createModule(.{
.source_file = .{ .path = (comptime thisDir()) ++ "/src/main.zig" },
});
}

pub fn build(b: *std.Build) !void {
// Modules available to downstream dependencies
_ = b.addModule("xev", .{
.source_file = .{ .path = (comptime thisDir()) ++ "/src/main.zig" },
});

const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

_ = b.addModule("xev", .{ .source_file = .{ .path = "src/main.zig" } });

const man_pages = b.option(
bool,
"man-pages",
Expand Down Expand Up @@ -111,8 +90,11 @@ pub fn build(b: *std.Build) !void {
.optimize = optimize,
});
static_binding_test.linkLibC();
static_binding_test.addIncludePath("include");
static_binding_test.addCSourceFile("examples/_basic.c", &[_][]const u8{ "-Wall", "-Wextra", "-pedantic", "-std=c99", "-D_POSIX_C_SOURCE=199309L" });
static_binding_test.addIncludePath(.{ .path = "include" });
static_binding_test.addCSourceFile(.{
.file = .{ .path = "examples/_basic.c" },
.flags = &[_][]const u8{ "-Wall", "-Wextra", "-pedantic", "-std=c99", "-D_POSIX_C_SOURCE=199309L" },
});
static_binding_test.linkLibrary(static_lib);
if (test_install) b.installArtifact(static_binding_test);

Expand Down Expand Up @@ -145,8 +127,11 @@ pub fn build(b: *std.Build) !void {
.optimize = optimize,
});
dynamic_binding_test.linkLibC();
dynamic_binding_test.addIncludePath("include");
dynamic_binding_test.addCSourceFile("examples/_basic.c", &[_][]const u8{ "-Wall", "-Wextra", "-pedantic", "-std=c99" });
dynamic_binding_test.addIncludePath(.{ .path = "include" });
dynamic_binding_test.addCSourceFile(.{
.file = .{ .path = "examples/_basic.c" },
.flags = &[_][]const u8{ "-Wall", "-Wextra", "-pedantic", "-std=c99" },
});
dynamic_binding_test.linkLibrary(dynamic_lib);
if (test_install) b.installArtifact(dynamic_binding_test);

Expand Down Expand Up @@ -239,9 +224,13 @@ fn benchTargets(
.target = target,
.optimize = .ReleaseFast, // benchmarks are always release fast
});
c_exe.addModule("xev", module(b));
c_exe.override_dest_dir = std.Build.InstallDir{ .custom = "bench" };
if (install) b.installArtifact(c_exe);
c_exe.addModule("xev", b.modules.get("xev").?);
if (install) {
const install_step = b.addInstallArtifact(c_exe, .{
.dest_dir = .{ .override = .{ .custom = "bench" } },
});
b.getInstallStep().dependOn(&install_step.step);
}

// Store the mapping
try map.put(try b.allocator.dupe(u8, name), c_exe);
Expand Down Expand Up @@ -293,9 +282,13 @@ fn exampleTargets(
.target = target,
.optimize = optimize,
});
c_exe.addModule("xev", module(b));
c_exe.override_dest_dir = std.Build.InstallDir{ .custom = "example" };
if (install) b.installArtifact(c_exe);
c_exe.addModule("xev", b.modules.get("xev").?);
if (install) {
const install_step = b.addInstallArtifact(c_exe, .{
.dest_dir = .{ .override = .{ .custom = "example" } },
});
b.getInstallStep().dependOn(&install_step.step);
}
} else {
const c_lib = c_lib_ orelse return error.UnsupportedPlatform;
const c_exe = b.addExecutable(.{
Expand All @@ -304,17 +297,24 @@ fn exampleTargets(
.optimize = optimize,
});
c_exe.linkLibC();
c_exe.addIncludePath("include");
c_exe.addCSourceFile(path, &[_][]const u8{
"-Wall",
"-Wextra",
"-pedantic",
"-std=c99",
"-D_POSIX_C_SOURCE=199309L",
c_exe.addIncludePath(.{ .path = "include" });
c_exe.addCSourceFile(.{
.file = .{ .path = path },
.flags = &[_][]const u8{
"-Wall",
"-Wextra",
"-pedantic",
"-std=c99",
"-D_POSIX_C_SOURCE=199309L",
},
});
c_exe.linkLibrary(c_lib);
c_exe.override_dest_dir = std.Build.InstallDir{ .custom = "example" };
if (install) b.installArtifact(c_exe);
if (install) {
const install_step = b.addInstallArtifact(c_exe, .{
.dest_dir = .{ .override = .{ .custom = "example" } },
});
b.getInstallStep().dependOn(&install_step.step);
}
}

// If we have specified a specific name, only install that one.
Expand Down
4 changes: 4 additions & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.{
.name = "libxev",
.version = "0.0.0",
}
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0937e81

Please sign in to comment.