Skip to content

Commit

Permalink
upgrade to Zig 0.12.0
Browse files Browse the repository at this point in the history
  • Loading branch information
nektro committed Jun 2, 2024
1 parent 5313096 commit 21683f9
Show file tree
Hide file tree
Showing 18 changed files with 286 additions and 308 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
- name: Setup Zig
uses: goto-bus-stop/setup-zig@v2
with:
version: "0.11.0"
version: "0.12.0"

- run: zig version
- run: zig env
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Setup Zig
uses: goto-bus-stop/setup-zig@v2
with:
version: "0.11.0"
version: "0.12.0"

- run: zig version
- run: zig env
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ A package manager for the Zig programming language.
- https://github.com/nektro/zigmod/releases

## Built With
- Zig master (at least `0.11.0`)
- Zig master (at least `0.12.0`)
- See [`zig.mod`](./zig.mod) and [`zigmod.lock`](./zigmod.lock)

### Build from Source
Expand Down
15 changes: 10 additions & 5 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,27 @@ const string = []const u8;
const builtin = @import("builtin");
const deps = @import("./deps.zig");

pub fn build(b: *std.build.Builder) void {
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const mode = b.option(std.builtin.Mode, "mode", "") orelse .Debug;
const use_full_name = b.option(bool, "use-full-name", "") orelse false;
const with_arch_os = b.fmt("-{s}-{s}", .{ @tagName(target.cpu_arch orelse builtin.cpu.arch), @tagName(target.os_tag orelse builtin.os.tag) });
const with_arch_os = b.fmt("-{s}-{s}", .{ @tagName(target.result.cpu.arch), @tagName(target.result.os.tag) });
const exe_name = b.fmt("{s}{s}", .{ "zigmod", if (use_full_name) with_arch_os else "" });
const exe = b.addExecutable(.{ .name = exe_name, .root_source_file = .{ .path = "src/main.zig" }, .target = target, .optimize = mode });
const exe = b.addExecutable(.{
.name = exe_name,
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = mode,
});
const tag = b.option(string, "tag", "") orelse "dev";
const strip = b.option(bool, "strip", "Build without debug info.") orelse false;

const exe_options = b.addOptions();
exe.addOptions("build_options", exe_options);
exe.root_module.addImport("build_options", exe_options.createModule());
exe_options.addOption(string, "version", tag);

deps.addAllTo(exe);
exe.strip = strip;
exe.root_module.strip = strip;
b.installArtifact(exe);

const run_cmd = b.addRunArtifact(exe);
Expand Down
211 changes: 106 additions & 105 deletions deps.zig

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The rest of this documentation will assume you already have Zig installed.

As Zig is still in development itself, if you plan to contribute to Zigmod you will need a master download of Zig. Those can be obtained from https://ziglang.org/download/#release-master.

The earliest Zig release this Zigmod was verified to work with is `0.11.0`.
The earliest Zig release this Zigmod was verified to work with is `0.12.0`.

## Download
You may download a precompiled binary from https://github.com/nektro/zigmod/releases or build the project from source.
Expand Down
7 changes: 1 addition & 6 deletions docs/deps.zig.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ A helper function to automatically pull in dependencies, purely from the `zig bu
- Type: `pub fn (exe: *std.build.LibExeObjStep) void`
A helper function to add all of the packages, C files, and system libraries to the passed exectuable. It will also automatically link libC in the event that any C files are found in the dependency tree.

### `addAllLibrariesTo`
- Type: `pub fn (exe: *std.build.LibExeObjStep) void`
A helper function called by `addAllTo` that adds the C files and libraries.

### `Package`
```zig
pub const Package = struct {
Expand All @@ -29,7 +25,6 @@ pub const Package = struct {
c_source_flags: []const string = &.{},
system_libs: []const string = &.{},
frameworks: []const string = &.{},
vcpkg: bool = false,
};
```

Expand All @@ -43,7 +38,7 @@ This is a an array of all of the items in `package_data`, but only contains the

### `pkgs`
- Type: `struct<NAME, Package>`
This is a struct that associates the package name to the relavant `Package`. The only packages listed are the dependencies of the root project.
This is a struct that associates the package name to the relavant `Package`. The only packages listed are the dependencies of the root project.

### `imports`
- Type: `struct<NAME, @import(main)>`
Expand Down
10 changes: 0 additions & 10 deletions docs/zig.mod.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@ Similar to `dependencies` but will only get added to the project if the current
- Type: `string`
Parsed as a `std.SemanticVersion`, this attribute refers to the minimum compatible Zig version for this package/application and will cause `zig build` to panic if violated.

#### `vcpkg`
- Type: `bool`
- Example: `true`|any
This attribute is a flag to call `try exe.addVcpkgPaths(.static);` when on Windows. Likely used in conjunction with adding system libraries/C code. `true` is the only value that will enable this flag.

----

### Dep Object
Expand Down Expand Up @@ -124,10 +119,5 @@ This attribute specifies a way to filter when the dependency will be generated i
- Example: `true`|any
This attribute is a manual override for having an external repo that contains no Zig or C code but other files be managed through Zigmod and `deps.zig`. `true` is the only value that will enable this flag.

#### Dep `vcpkg`
- Type: `string`
- Example: `true`|any
This attribute is a flag to call `try exe.addVcpkgPaths(.static);` when on Windows. Likely used in conjunction with adding system libraries/C code. `true` is the only value that will enable this flag.

#### Dep Overrides
There are a number of fields you can add to a `Dep` object that will override it's top-level value. This is most useful in the case where a project you want to use does not have a `zigmod.yml` manifest. You can then use overrides to define the values for them. The only top-level value you can not override is `dependencies`.
150 changes: 73 additions & 77 deletions src/cmd/fetch.zig
Original file line number Diff line number Diff line change
Expand Up @@ -52,94 +52,89 @@ pub fn create_depszig(alloc: std.mem.Allocator, cachepath: string, dir: std.fs.D
try w.writeAll("const std = @import(\"std\");\n");
try w.writeAll("const builtin = @import(\"builtin\");\n");
try w.writeAll("const string = []const u8;\n");
try w.writeAll("const ModuleDependency = std.build.ModuleDependency;\n");
try w.writeAll("\n");
try w.print("pub const cache = \"{}\";\n", .{std.zig.fmtEscapes(cachepath)});
try w.writeAll("\n");
try w.writeAll(
\\pub fn addAllTo(exe: *std.build.LibExeObjStep) void {
\\pub fn addAllTo(exe: *std.Build.Step.Compile) void {
\\ checkMinZig(builtin.zig_version, exe);
\\ const b = exe.step.owner;
\\ @setEvalBranchQuota(1_000_000);
\\ for (packages) |pkg| {
\\ const moddep = pkg.zp(b);
\\ exe.addModule(moddep.name, moddep.module);
\\ const module = pkg.module(exe);
\\ exe.root_module.addImport(pkg.import.?[0], module);
\\ }
\\ addAllLibrariesTo(exe);
\\}
\\
\\pub fn addAllLibrariesTo(exe: *std.build.LibExeObjStep) void {
\\ const b = exe.step.owner;
\\ var llc = false;
\\ var vcpkg = false;
\\ inline for (comptime std.meta.declarations(package_data)) |decl| {
\\ const pkg = @as(Package, @field(package_data, decl.name));
\\ for (pkg.system_libs) |item| {
\\ exe.linkSystemLibrary(item);
\\ llc = true;
\\ }
\\ for (pkg.frameworks) |item| {
\\ if (!builtin.target.isDarwin()) @panic(b.fmt("a dependency is attempting to link to the framework {s}, which is only possible under Darwin", .{item}));
\\ exe.linkFramework(item);
\\ llc = true;
\\ }
\\ for (pkg.c_include_dirs) |item| {
\\ exe.addIncludePath(.{.path = b.fmt("{s}/{s}", .{ @field(dirs, decl.name), item })});
\\ llc = true;
\\ }
\\ for (pkg.c_source_files) |item| {
\\ exe.addCSourceFile(.{ .file = .{ .path = b.fmt("{s}/{s}", .{ @field(dirs, decl.name), item }) }, .flags = pkg.c_source_flags });
\\ llc = true;
\\ }
\\ vcpkg = vcpkg or pkg.vcpkg;
\\ }
\\ if (llc) exe.linkLibC();
\\ if (builtin.os.tag == .windows and vcpkg) exe.addVcpkgPaths(.static) catch |err| @panic(@errorName(err));
\\}
\\
\\var link_lib_c = false;
\\pub const Package = struct {
\\ directory: string,
\\ pkg: ?Pkg = null,
\\ import: ?struct { string, std.Build.LazyPath } = null,
\\ dependencies: []const *Package,
\\ c_include_dirs: []const string = &.{},
\\ c_source_files: []const string = &.{},
\\ c_source_flags: []const string = &.{},
\\ system_libs: []const string = &.{},
\\ frameworks: []const string = &.{},
\\ vcpkg: bool = false,
\\ module: ?ModuleDependency = null,
\\ module_memo: ?*std.Build.Module = null,
\\
\\ pub fn zp(self: *Package, b: *std.build.Builder) ModuleDependency {
\\ var temp: [100]ModuleDependency = undefined;
\\ const pkg = self.pkg.?;
\\ for (pkg.dependencies, 0..) |item, i| {
\\ temp[i] = item.zp(b);
\\ pub fn module(self: *Package, exe: *std.Build.Step.Compile) *std.Build.Module {
\\ if (self.module_memo) |cached| {
\\ return cached;
\\ }
\\ const b = exe.step.owner;
\\ const result = b.createModule(.{});
\\ const dummy_library = b.addStaticLibrary(.{
\\ .name = "dummy",
\\ .target = exe.root_module.resolved_target orelse b.host,
\\ .optimize = exe.root_module.optimize.?,
\\ });
\\ if (self.import) |capture| {
\\ result.root_source_file = capture[1];
\\ }
\\ for (self.dependencies) |item| {
\\ const module_dep = item.module(exe);
\\ if (module_dep.root_source_file != null) {
\\ result.addImport(item.import.?[0], module_dep);
\\ }
\\ for (module_dep.include_dirs.items) |jtem| {
\\ switch (jtem) {
\\ .path => result.addIncludePath(jtem.path),
\\ .path_system, .path_after, .framework_path, .framework_path_system, .other_step, .config_header_step => {},
\\ }
\\ }
\\ }
\\ for (self.c_include_dirs) |item| {
\\ result.addIncludePath(b.path(b.fmt("{s}/{s}", .{ self.directory, item })));
\\ dummy_library.addIncludePath(b.path(b.fmt("{s}/{s}", .{ self.directory, item })));
\\ link_lib_c = true;
\\ }
\\ for (self.c_source_files) |item| {
\\ dummy_library.addCSourceFile(.{ .file = b.path(b.fmt("{s}/{s}", .{ self.directory, item })), .flags = self.c_source_flags });
\\ }
\\ if (self.module) |mod| {
\\ return mod;
\\ for (self.system_libs) |item| {
\\ dummy_library.linkSystemLibrary(item);
\\ }
\\ const result = ModuleDependency{
\\ .name = pkg.name,
\\ .module = b.createModule(.{
\\ .source_file = pkg.source,
\\ .dependencies = b.allocator.dupe(ModuleDependency, temp[0..pkg.dependencies.len]) catch @panic("oom"),
\\ }),
\\ };
\\ self.module = result;
\\ for (self.frameworks) |item| {
\\ dummy_library.linkFramework(item);
\\ }
\\ if (self.c_source_files.len > 0 or self.system_libs.len > 0 or self.frameworks.len > 0) {
\\ dummy_library.linkLibC();
\\ exe.root_module.linkLibrary(dummy_library);
\\ link_lib_c = true;
\\ }
\\ if (link_lib_c) {
\\ result.link_libc = true;
\\ }
\\ self.module_memo = result;
\\ return result;
\\ }
\\};
\\
\\pub const Pkg = struct {
\\ name: string,
\\ source: std.build.FileSource,
\\ dependencies: []const *Package,
\\};
\\
\\
);

try w.print(
\\fn checkMinZig(current: std.SemanticVersion, exe: *std.build.LibExeObjStep) void {{
\\fn checkMinZig(current: std.SemanticVersion, exe: *std.Build.Step.Compile) void {{
\\ const min = std.SemanticVersion.parse("{?}") catch return;
\\ if (current.order(min).compare(.lt)) @panic(exe.step.owner.fmt("Your Zig version v{{}} does not meet the minimum build requirement of v{{}}", .{{current, min}}));
\\}}
Expand All @@ -148,7 +143,7 @@ pub fn create_depszig(alloc: std.mem.Allocator, cachepath: string, dir: std.fs.D
, .{top_module.minZigVersion()});

try w.writeAll("pub const dirs = struct {\n");
try print_dirs(w, list.items);
try print_dirs(w, list.items, alloc);
try w.writeAll("};\n\n");

try w.writeAll("pub const package_data = struct {\n");
Expand Down Expand Up @@ -215,7 +210,7 @@ fn diff_lockfile(alloc: std.mem.Allocator) !void {
while (try r.readUntilDelimiterOrEofAlloc(alloc, '\n', max)) |line| {
if (line[0] == ' ') continue;
if (line[0] == '-') try rems.append(line[1..]);
if (line[0] == '+') if (line[1] == '2') continue else try adds.append(line[1..]);
if (line[0] == '+') if (line[1] == '2') break else try adds.append(line[1..]);
}

var changes = std.StringHashMap(DiffChange).init(alloc);
Expand Down Expand Up @@ -287,13 +282,18 @@ fn diff_printchange(comptime testt: string, comptime replacement: string, item:
return false;
}

fn print_dirs(w: std.fs.File.Writer, list: []const zigmod.Module) !void {
fn print_dirs(w: std.fs.File.Writer, list: []const zigmod.Module, alloc: std.mem.Allocator) !void {
for (list) |mod| {
if (mod.type == .system_lib or mod.type == .framework) continue;
if (std.mem.eql(u8, mod.id, "root")) {
try w.writeAll(" pub const _root = \"\";\n");
continue;
}
if (std.mem.eql(u8, mod.clean_path, "../..")) {
const cwd_realpath = try std.fs.cwd().realpathAlloc(alloc, ".");
try w.print(" pub const _{s} = \"{}\";\n", .{ mod.short_id(), std.zig.fmtEscapes(cwd_realpath) });
continue;
}
try w.print(" pub const _{s} = cache ++ \"/{}\";\n", .{ mod.short_id(), std.zig.fmtEscapes(mod.clean_path) });
}
}
Expand Down Expand Up @@ -327,23 +327,22 @@ fn print_pkg_data_to(w: std.fs.File.Writer, notdone: *std.ArrayList(zigmod.Modul
});
if (mod.main.len > 0 and !std.mem.eql(u8, mod.id, "root")) {
try w.print(
\\ .pkg = Pkg{{ .name = "{s}", .source = .{{ .path = dirs._{s} ++ "/{s}" }}, .dependencies =
\\ .import = .{{ "{s}", .{{ .path = dirs._{s} ++ "/{s}" }} }},
\\
, .{
mod.name,
mod.short_id(),
mod.main,
});
if (mod.has_no_zig_deps()) {
try w.writeAll(" &.{} },\n");
} else {
try w.writeAll(" &.{");
for (mod.deps, 0..) |moddep, j| {
if (moddep.main.len == 0) continue;
try w.print(" &_{s}", .{moddep.id[0..12]});
if (j != mod.deps.len - 1) try w.writeAll(",");
}
try w.writeAll(" } },\n");
}
{
try w.writeAll(" .dependencies =");
try w.writeAll(" &.{");
for (mod.deps, 0..) |moddep, j| {
try w.print(" &_{s}", .{moddep.id[0..12]});
if (j != mod.deps.len - 1) try w.writeAll(",");
}
try w.writeAll(" },\n");
}
if (mod.c_include_dirs.len > 0) {
try w.writeAll(" .c_include_dirs = &.{");
Expand Down Expand Up @@ -387,9 +386,6 @@ fn print_pkg_data_to(w: std.fs.File.Writer, notdone: *std.ArrayList(zigmod.Modul
}
try w.writeAll(" },\n");
}
if (mod.vcpkg) {
try w.writeAll(" .vcpkg = true,\n");
}
try w.writeAll(" };\n");

try done.append(mod);
Expand Down
Loading

0 comments on commit 21683f9

Please sign in to comment.