diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index ef48dbbd7e1f5f59bcfd2dee050203d356272f52..c807ea2aea398b7d9d28f2ace3e89040d8c93380 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -20,7 +20,7 @@ jobs: uses: mlugg/setup-zig@v2 with: mirror: "http://mirrors.nektro.net/s3cgi" - version: "0.16.0" + version: "0.17.0" - run: zig version - run: zig env diff --git a/.gitignore b/.gitignore index 4190bf347ddad046b7185bcf61b5bab5ee9b11f7..d210fd3d7ba71c1c95941ec7fa4f9a679ed623ed 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ zig-cache .zig-cache zig-out +zig-pkg .zigmod bin diff --git a/README.md b/README.md index 6f118ceea9f49ad1e2b4c7a317b06c29e2e07caa..a6c09218567c48486805cb6672d27dcc3e9ec180 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ A package manager for the Zig programming language. ## Built With -- Zig `0.16.0` +- Zig `0.17.0` - See [`zig.mod`](./zig.mod) and [`zigmod.lock`](./zigmod.lock) ### Build from Source diff --git a/build.zig b/build.zig index f3a940e0114d5223351881c5ba6ef311557c8570..7315cb89873814e60d091ed45266ef347e851d94 100644 --- a/build.zig +++ b/build.zig @@ -4,10 +4,10 @@ const builtin = @import("builtin"); const deps = @import("./deps.zig"); pub fn build(b: *std.Build) void { - b.reference_trace = 256; + // b.reference_trace = 255; // https://codeberg.org/ziglang/zig/issues/37136 const target = b.standardTargetOptions(.{}); - const mode = b.option(std.builtin.OptimizeMode, "mode", "") orelse .Debug; + const optimize = b.standardOptimizeOption(.{}); const use_full_name = b.option(bool, "use-full-name", "") orelse false; 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 "" }); @@ -16,7 +16,7 @@ pub fn build(b: *std.Build) void { .root_module = b.createModule(.{ .root_source_file = b.path("src/main.zig"), .target = target, - .optimize = mode, + .optimize = optimize, }), }); const tag = b.option(string, "tag", ""); @@ -37,9 +37,7 @@ pub fn build(b: *std.Build) void { const run_cmd = b.addRunArtifact(exe); run_cmd.step.dependOn(b.getInstallStep()); - if (b.args) |args| { - run_cmd.addArgs(args); - } + run_cmd.addPassthruArgs(); const run_step = b.step("run", "Run the app"); run_step.dependOn(&run_cmd.step); diff --git a/build.zig.zon b/build.zig.zon new file mode 100644 index 0000000000000000000000000000000000000000..0d674b451b544bee7b678175c532502bc2a8dea1 --- /dev/null +++ b/build.zig.zon @@ -0,0 +1,19 @@ +.{ + .fingerprint = 0xc11ddfd84b6c7d6b, + .name = .zigmod, + .version = "0.0.0", + .minimum_zig_version = "0.17.0", + + .paths = .{ + "build.zig", + "build.zig.zon", + "src", + }, + + .dependencies = .{ + .translate_c = .{ + .url = "git+https://codeberg.org/ziglang/translate-c?ref=2.0.2#c11030012dc325d446474d17b78c242fd1d622ec", + .hash = "translate_c-2.0.2-Q_BUWvBNBwBgPKQJPblip6M7AHNK_IPNPmyv4rSIQMQc", + }, + }, +} diff --git a/deps.zig b/deps.zig index 5e0ba97608b79552f15e3fa2b168ff822ad418de..bcf553fcc754682099a846a52c6bc4dc0c16259d 100644 --- a/deps.zig +++ b/deps.zig @@ -2,6 +2,10 @@ const std = @import("std"); const builtin = @import("builtin"); const string = []const u8; +const any_translate_c = true; +const Translator = @import("translate_c").Translator; // need to depend on ziglang/translate-c#2.0.2 + +pub var translate_c: ?*std.Build.Dependency = null; pub const GitExactStep = struct { step: *std.Build.Step, @@ -28,30 +32,30 @@ pub fn fetch(exe: *std.Build.Step.Compile) *std.Build.Step { const b = exe.step.owner; const step = b.step("fetch", ""); dependOnGit(GitExactStep.create(b, "https://github.com/marlersoft/zigwin32", "fb2645891520f3e99931db17a7eba04057e87e89"), step, &@field(package_data, "_o6ogpor87xc2")); - dependOnGit(GitExactStep.create(b, "https://github.com/nektro/arqv-ini", "d2465c64833590a04bd9b7f50c87363fd03e65bf"), step, &@field(package_data, "_u7sysdckdymi")); - dependOnGit(GitExactStep.create(b, "https://github.com/nektro/zig-ansi", "bebb39ae30d9848a1c212cee582d0b1c102d8b87"), step, &@field(package_data, "_s84v9o48ucb0")); - dependOnGit(GitExactStep.create(b, "https://github.com/nektro/zig-detect-license", "6de79b4ff8f7462e26f224f4bed3c81710c8893b"), step, &@field(package_data, "_2ovav391ivak")); - dependOnGit(GitExactStep.create(b, "https://github.com/nektro/zig-extras", "0c90ee9a82d8863e0e142211a587940ee2a09790"), step, &@field(package_data, "_f7dubzb7cyqe")); - dependOnGit(GitExactStep.create(b, "https://github.com/nektro/zig-git", "88b94a1960be841bb93a0bca65d556cb6385d1fd"), step, &@field(package_data, "_0k64oe2nuzvj")); - dependOnGit(GitExactStep.create(b, "https://github.com/nektro/zig-inquirer", "e745b018f87ddc4c8958370332e6cf61625403cc"), step, &@field(package_data, "_c1xirp1ota5p")); - dependOnGit(GitExactStep.create(b, "https://github.com/nektro/zig-intrusive-parser", "2409be829d989bd03b6d123159880660c6089820"), step, &@field(package_data, "_7l3oxw6nqqws")); - dependOnGit(GitExactStep.create(b, "https://github.com/nektro/zig-json", "d32454ad6d1a6e6e58a8c12a4f3ee1223be2e316"), step, &@field(package_data, "_ocmr9rtohgcc")); - dependOnGit(GitExactStep.create(b, "https://github.com/nektro/zig-leven", "014971d4b1e327a5c61322c3a6b9369fd1863864"), step, &@field(package_data, "_96h80ezrvj7i")); - dependOnGit(GitExactStep.create(b, "https://github.com/nektro/zig-licenses", "bc1cd51625c13d6c7df819fd0d85968e617f5c57"), step, &@field(package_data, "_0npcrzfdlrvk")); - dependOnGit(GitExactStep.create(b, "https://github.com/nektro/zig-licenses-text", "a9d067c3c4d6c226a1de5531b66785bc15d869ee"), step, &@field(package_data, "_pt88y5d80m25")); - dependOnGit(GitExactStep.create(b, "https://github.com/nektro/zig-nfs", "1a27fe574706f0b91c9f66b22fe7d3fa8f5129d5"), step, &@field(package_data, "_vph9l0hxpeze")); - dependOnGit(GitExactStep.create(b, "https://github.com/nektro/zig-nio", "17c4f044b15917efc1d3f018765881d53fa27e48"), step, &@field(package_data, "_kscsl0145t7x")); - dependOnGit(GitExactStep.create(b, "https://github.com/nektro/zig-sys-darwin", "b349aa32b55aae4cf2ae56740003f8aff0394920"), step, &@field(package_data, "_73bukkeci2u6")); - dependOnGit(GitExactStep.create(b, "https://github.com/nektro/zig-sys-freebsd", "f97e6c2de30af76818572490735c84ce1015958b"), step, &@field(package_data, "_qfev2g50alvp")); - dependOnGit(GitExactStep.create(b, "https://github.com/nektro/zig-sys-linux", "6dabe7cce239d882c8fd132ab5cda1d1f9ff3634"), step, &@field(package_data, "_h7tv7ayhffak")); - dependOnGit(GitExactStep.create(b, "https://github.com/nektro/zig-sys-netbsd", "5ad432ca421c59099be3630b9d0dda49e8bef0a7"), step, &@field(package_data, "_uvkgl6xf1azv")); - dependOnGit(GitExactStep.create(b, "https://github.com/nektro/zig-sys-openbsd", "164646a5e7c4129eb92ac524a60f1759605c99df"), step, &@field(package_data, "_4rmxgmcykiba")); - dependOnGit(GitExactStep.create(b, "https://github.com/nektro/zig-time", "1349fb2c8aaaa1503e8730facea6f87c5fe75dc0"), step, &@field(package_data, "_iecwp4b3bsfm")); - dependOnGit(GitExactStep.create(b, "https://github.com/nektro/zig-tracer", "4ad0602d52a3f40ef74f59f648602c61409f7ba7"), step, &@field(package_data, "_ede2wygpe1iy")); - dependOnGit(GitExactStep.create(b, "https://github.com/nektro/zig-yaml", "dff9fd43ebf1046c70090bdccfeb2e4f1c0d584b"), step, &@field(package_data, "_g982zq6e8wsv")); + dependOnGit(GitExactStep.create(b, "https://github.com/nektro/arqv-ini", "2951069265792192bd714980d44ccb4abee4a6e8"), step, &@field(package_data, "_u7sysdckdymi")); + dependOnGit(GitExactStep.create(b, "https://github.com/nektro/zig-ansi", "952d0751c359612fc1a7c493453cc6223bbaff5d"), step, &@field(package_data, "_s84v9o48ucb0")); + dependOnGit(GitExactStep.create(b, "https://github.com/nektro/zig-detect-license", "2ec231c2ae5274d926851f355f0896e0fcbbb5d0"), step, &@field(package_data, "_2ovav391ivak")); + dependOnGit(GitExactStep.create(b, "https://github.com/nektro/zig-extras", "fb3aa273de9116bbb7befd448835b854d906f2ee"), step, &@field(package_data, "_f7dubzb7cyqe")); + dependOnGit(GitExactStep.create(b, "https://github.com/nektro/zig-git", "f4bd29ad59967102db4532bda2d95d1512fbf3fb"), step, &@field(package_data, "_0k64oe2nuzvj")); + dependOnGit(GitExactStep.create(b, "https://github.com/nektro/zig-inquirer", "6feae6d21ac54cba8e5d4da91868c4db9f82343b"), step, &@field(package_data, "_c1xirp1ota5p")); + dependOnGit(GitExactStep.create(b, "https://github.com/nektro/zig-intrusive-parser", "6d074187f8c5aa2bdab11ee57c7180df768fca72"), step, &@field(package_data, "_7l3oxw6nqqws")); + dependOnGit(GitExactStep.create(b, "https://github.com/nektro/zig-json", "4f6aa411f1dc93cc86a052d8a69804c8ff762e59"), step, &@field(package_data, "_ocmr9rtohgcc")); + dependOnGit(GitExactStep.create(b, "https://github.com/nektro/zig-leven", "d990af4f7706edcb0d928d953d1a56c2830fa036"), step, &@field(package_data, "_96h80ezrvj7i")); + dependOnGit(GitExactStep.create(b, "https://github.com/nektro/zig-licenses", "1fd04fc1b7619ca2ddfd07354a93e6d72334f76f"), step, &@field(package_data, "_0npcrzfdlrvk")); + dependOnGit(GitExactStep.create(b, "https://github.com/nektro/zig-licenses-text", "a20d31da306967253b8a46c60d8368c7ac4b7229"), step, &@field(package_data, "_pt88y5d80m25")); + dependOnGit(GitExactStep.create(b, "https://github.com/nektro/zig-nfs", "b32bb30bbdc7e5ad9bb3adb5c477a98897c9cb10"), step, &@field(package_data, "_vph9l0hxpeze")); + dependOnGit(GitExactStep.create(b, "https://github.com/nektro/zig-nio", "b28c8efc86f3baec3737de86ffde5c4b3a5260f6"), step, &@field(package_data, "_kscsl0145t7x")); + dependOnGit(GitExactStep.create(b, "https://github.com/nektro/zig-sys-darwin", "312638dcf043c3163e260ec5b42610856fc9eeab"), step, &@field(package_data, "_73bukkeci2u6")); + dependOnGit(GitExactStep.create(b, "https://github.com/nektro/zig-sys-freebsd", "967ac568e385534825e6849bc82a9b51d45c600c"), step, &@field(package_data, "_qfev2g50alvp")); + dependOnGit(GitExactStep.create(b, "https://github.com/nektro/zig-sys-linux", "750d2aa874a9ddb473e7cd0af99f1e722aaab79d"), step, &@field(package_data, "_h7tv7ayhffak")); + dependOnGit(GitExactStep.create(b, "https://github.com/nektro/zig-sys-netbsd", "e16386268223744ac3f6036a028cc4359056731b"), step, &@field(package_data, "_uvkgl6xf1azv")); + dependOnGit(GitExactStep.create(b, "https://github.com/nektro/zig-sys-openbsd", "081e11079887e8f02d8480b6219a13a41afd8d69"), step, &@field(package_data, "_4rmxgmcykiba")); + dependOnGit(GitExactStep.create(b, "https://github.com/nektro/zig-time", "29f3f902cc8948faa36b853380c38ff587236661"), step, &@field(package_data, "_iecwp4b3bsfm")); + dependOnGit(GitExactStep.create(b, "https://github.com/nektro/zig-tracer", "ccf9a6b85a38cc343aba772f2d2aac4df49000e1"), step, &@field(package_data, "_ede2wygpe1iy")); + dependOnGit(GitExactStep.create(b, "https://github.com/nektro/zig-yaml", "e4e8dfe78a3a0e39d134382a18e8eb771418e850"), step, &@field(package_data, "_g982zq6e8wsv")); dependOnGit(GitExactStep.create(b, "https://github.com/madler/zlib", "da607da739fa6047df13e66a2af6b8bec7c2a498"), step, &@field(package_data, "_0e2d06bb494b")); dependOnGit(GitExactStep.create(b, "https://github.com/yaml/libyaml", "2c891fc7a770e8ba2fec34fc6b545c672beb37e6"), step, &@field(package_data, "_8mdbh0zuneb0")); - dependOnGit(GitExactStep.create(b, "https://github.com/ziglibs/known-folders", "207c34a16e4365edc20d92c7892f962b3bed46e8"), step, &@field(package_data, "_2ta738wrqbaq")); + dependOnGit(GitExactStep.create(b, "https://github.com/ziglibs/known-folders", "6da0d0c41b78b9ed2d34fa364fcb81b5ebece6c4"), step, &@field(package_data, "_2ta738wrqbaq")); return step; } @@ -76,7 +80,7 @@ pub fn addAllTo(exe: *std.Build.Step.Compile) void { exe.root_module.addImport(pkg.name, module); } // clear module memo cache so addAllTo can be called more than once in the same build.zig - inline for (comptime std.meta.declarations(package_data)) |decl| @field(package_data, decl.name).module_memo = null; + inline for (comptime std.meta.declarations(package_data)) |decl| @field(package_data, decl).module_memo = null; } pub fn dependOnGit(ge: GitExactStep, s: *std.Build.Step, p: *Package) void { @@ -96,6 +100,7 @@ pub const Package = struct { system_libs: []const string = &.{}, frameworks: []const string = &.{}, module_memo: ?*std.Build.Module = null, + translate_c_h: ?string = null, pub fn module(self: *Package, exe: *std.Build.Step.Compile, fetch_step: *std.Build.Step) *std.Build.Module { if (self.module_memo) |cached| { @@ -140,6 +145,7 @@ pub const Package = struct { for (self.c_include_dirs) |item| { result.addIncludePath(self.path.?.path(b, item)); dummy_library.root_module.addIncludePath(self.path.?.path(b, item)); + exe.root_module.addIncludePath(self.path.?.path(b, item)); link_lib_c = true; links += 1; } @@ -147,6 +153,20 @@ pub const Package = struct { dummy_library.root_module.addCSourceFile(.{ .file = self.path.?.path(b, item), .flags = self.c_source_flags }); links += 1; } + if (any_translate_c) if (self.translate_c_h) |h| { + if (translate_c == null) translate_c = b.dependency("translate_c", .{}); + const t: Translator = .init(translate_c.?, .{ + .c_source_file = b.addWriteFile("c.h", b.fmt("// {s}\n#include <{s}>\n", .{ self.name, h })).getDirectory().path(b, "c.h"), + .target = exe.root_module.resolved_target.?, + .optimize = exe.root_module.optimize.?, + }); + for (exe.root_module.include_dirs.items) |d| { + if (d != .path) continue; + t.addIncludePath(d.path); + } + result.addImport("c", t.mod); + link_lib_c = true; + }; for (self.system_libs) |item| { if (std.zig.target.isLibCLibName(&target, item)) continue; dummy_library.root_module.linkSystemLibrary(item, .{}); @@ -170,7 +190,7 @@ pub const Package = struct { }; fn checkMinZig(current: std.SemanticVersion, exe: *std.Build.Step.Compile) void { - const min = std.SemanticVersion.parse("0.16.0") catch return; + const min = std.SemanticVersion.parse("0.17.0") catch return; if (current.order(min).compare(.lt)) @panic(exe.step.owner.fmt("Your Zig version v{f} does not meet the minimum build requirement of v{f}", .{current, min})); } @@ -195,6 +215,7 @@ pub const package_data = struct { .name = "inquirer", .main = "src/lib.zig", .deps = &[_]*Package{ &_s84v9o48ucb0 }, + .translate_c_h = "termios.h", }; pub var _96h80ezrvj7i = Package{ .name = "leven", @@ -215,18 +236,22 @@ pub const package_data = struct { pub var _qfev2g50alvp = Package{ .name = "sys-freebsd", .main = "freebsd.zig", + .translate_c_h = "pthread.h", }; pub var _h7tv7ayhffak = Package{ .name = "sys-linux", .main = "mod.zig", + .translate_c_h = "pthread.h", }; pub var _uvkgl6xf1azv = Package{ .name = "sys-netbsd", .main = "netbsd.zig", + .translate_c_h = "pthread.h", }; pub var _4rmxgmcykiba = Package{ .name = "sys-openbsd", .main = "openbsd.zig", + .translate_c_h = "pthread.h", }; pub var _kscsl0145t7x = Package{ .name = "nio", @@ -262,6 +287,7 @@ pub const package_data = struct { .name = "git", .main = "git.zig", .deps = &[_]*Package{ &_iecwp4b3bsfm, &_f7dubzb7cyqe, &_ede2wygpe1iy, &_vph9l0hxpeze, &_kscsl0145t7x, &_0e2d06bb494b }, + .translate_c_h = "zlib.h", }; pub var _ocmr9rtohgcc = Package{ .name = "json", @@ -272,6 +298,7 @@ pub const package_data = struct { .name = "yaml", .main = "yaml.zig", .deps = &[_]*Package{ &_8mdbh0zuneb0 }, + .translate_c_h = "yaml.h", }; pub var _0e2d06bb494b = Package{ .c_include_dirs = &.{ "" }, diff --git a/docs/README.md b/docs/README.md index c6fe38c62e20fead5b27b40cc0b416ddbb81a3ef..2afec8cd3ab0546752c272ea6b3ff3caa7be37a8 100644 --- a/docs/README.md +++ b/docs/README.md @@ -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.16.0`. +The earliest Zig release this Zigmod was verified to work with is `0.17.0`. ## Download You may download a precompiled binary from https://github.com/nektro/zigmod/releases or build the project from source. diff --git a/docs/zig.mod.md b/docs/zig.mod.md index f8690cf10e6ae48ce171c92b8d5e2715bf17e3bb..6e6210f412afe8b253ba03f59492038599925343 100644 --- a/docs/zig.mod.md +++ b/docs/zig.mod.md @@ -42,6 +42,10 @@ This accepts a list of local directories to embed static assets. These files wil - Type: `[]string` This accepts a list of local directories to embed static assets. These files will be provided through a `files.zig` files in the package root. It generates a list of `@embedFile` calls. +### `translate_c_h` +- Type: `string` +A name a header file eg `foo.h` that will be passed as `#include ` to [`translate_c`](https://codeberg.org/ziglang/translate-c) and provided in an `@import("c")` module. + ### `dependencies` - Type: `[]Dep` This is a list of `Dep` objects. `Dep` objects are how you include the other people's code in your project. See the `Dep` documentation below to learn more about the attributes available here. diff --git a/src/cmd/fetch.0.16.zig b/src/cmd/fetch.0.16.zig new file mode 100644 index 0000000000000000000000000000000000000000..b93526b43ed12ccedee364fd04b5e462abf1fa06 --- /dev/null +++ b/src/cmd/fetch.0.16.zig @@ -0,0 +1,463 @@ +const std = @import("std"); +const string = []const u8; +const ansi = @import("ansi"); +const extras = @import("extras"); +const nfs = @import("nfs"); +const nio = @import("nio"); + +const zigmod = @import("../lib.zig"); +const u = @import("./../util/funcs.zig"); +const common = @import("./../common.zig"); +const license = @import("./license.zig"); + +// +// + +pub fn execute(self_name: []const u8, args: []const [:0]const u8) !void { + _ = self_name; + + const gpa = std.heap.c_allocator; + const cachepath = try u.find_cachepath(); + const dir = nfs.cwd(); + const should_update = !(args.len >= 1 and std.mem.eql(u8, args[0], "--no-update")); + + var options = common.CollectOptions{ + .log = should_update, + .update = should_update, + .alloc = gpa, + }; + const top_module = try common.collect_deps_deep(cachepath, dir, &options); + + var list = std.array_list.Managed(zigmod.Module).init(gpa); + try common.collect_pkgs(top_module, &list); + + try create_depszig(gpa, cachepath, dir, top_module, &list); + + try create_lockfile(gpa, &list, cachepath, dir); + + try diff_lockfile(gpa); + + options.update = false; + + var outfile = try dir.createFile("licenses.txt", .{}); + defer outfile.close(); + + try license.do(cachepath, dir, &options, outfile); +} + +pub fn create_depszig(alloc: std.mem.Allocator, cachepath: string, dir: nfs.Dir, top_module: zigmod.Module, list: *std.array_list.Managed(zigmod.Module)) !void { + const f = try dir.createFile("deps.zig", .{}); + defer f.close(); + + const w = f; + try w.writeAll("// zig fmt: off\n"); + 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("\n"); + try w.print("pub const cache = \"{}\";\n", .{u.altStringEscape(cachepath)}); + try w.writeAll("pub var skip_libc = false;\n"); + try w.writeAll("\n"); + try w.writeAll( + \\pub fn addAllTo(exe: *std.Build.Step.Compile) void { + \\ checkMinZig(builtin.zig_version, exe); + \\ @setEvalBranchQuota(1_000_000); + \\ for (packages) |pkg| { + \\ const module = pkg.module(exe); + \\ exe.root_module.addImport(pkg.import.?[0], module); + \\ } + \\ for (package_data._root.system_libs) |libname| { + \\ exe.root_module.linkSystemLibrary(libname, .{}); + \\ exe.root_module.link_libc = true; + \\ } + \\ // clear module memo cache so addAllTo can be called more than once in the same build.zig + \\ module_memo.clearAndFree(exe.step.owner.allocator); + \\} + \\ + \\var link_lib_c = false; + \\var module_memo: std.StringArrayHashMapUnmanaged(*std.Build.Module) = .empty; + \\pub const Package = struct { + \\ id: string, + \\ directory: string, + \\ import: ?struct { string, std.Build.LazyPath } = null, + \\ dependencies: []const *const Package, + \\ c_include_dirs: []const string = &.{}, + \\ c_source_files: []const string = &.{}, + \\ c_source_flags: []const string = &.{}, + \\ system_libs: []const string = &.{}, + \\ frameworks: []const string = &.{}, + \\ + \\ pub fn module(self: *const Package, exe: *std.Build.Step.Compile) *std.Build.Module { + \\ if (module_memo.get(self.id)) |cached| { + \\ return cached; + \\ } + \\ const b = exe.step.owner; + \\ const result = b.createModule(.{ + \\ .target = exe.root_module.resolved_target, + \\ }); + \\ const target = result.resolved_target.?.result; + \\ 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 => {}, + \\ .embed_path => {}, + \\ } + \\ } + \\ } + \\ for (self.c_include_dirs) |item| { + \\ result.addIncludePath(.{ .cwd_relative = (b.fmt("{s}/{s}", .{ self.directory, item })) }); + \\ exe.root_module.addIncludePath(.{ .cwd_relative = (b.fmt("{s}/{s}", .{ self.directory, item })) }); + \\ link_lib_c = true; + \\ } + \\ for (self.c_source_files) |item| { + \\ exe.root_module.addCSourceFile(.{ .file = .{ .cwd_relative = (b.fmt("{s}/{s}", .{ self.directory, item })) }, .flags = self.c_source_flags }); + \\ link_lib_c = true; + \\ } + \\ for (self.system_libs) |item| { + \\ if (skip_libc and std.zig.target.isLibCLibName(&target, item)) continue; + \\ result.linkSystemLibrary(item, .{}); + \\ exe.root_module.linkSystemLibrary(item, .{}); + \\ link_lib_c = true; + \\ } + \\ for (self.frameworks) |item| { + \\ result.linkFramework(item, .{}); + \\ exe.root_module.linkFramework(item, .{}); + \\ link_lib_c = true; + \\ } + \\ if (link_lib_c and !skip_libc) { + \\ result.link_libc = true; + \\ exe.root_module.link_libc = true; + \\ } + \\ module_memo.putNoClobber(b.allocator, self.id, result) catch @panic("OOM"); + \\ return result; + \\ } + \\}; + \\ + \\ + ); + + try w.print( + \\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{{f}} does not meet the minimum build requirement of v{{f}}", .{{current, min}})); + \\}} + \\ + \\ + , .{if (top_module.minZigVersion()) |sv| u.altSemanticVersion(sv) else null}); + + try w.writeAll("pub const dirs = struct {\n"); + try print_dirs(w, list.items, alloc); + try w.writeAll("};\n\n"); + + try w.writeAll("pub const package_data = struct {\n"); + var duped = std.array_list.Managed(zigmod.Module).init(alloc); + var done = std.array_list.Managed(zigmod.Module).init(alloc); + for (list.items) |mod| { + if (mod.type == .system_lib or mod.type == .framework) { + continue; + } + try duped.append(mod); + } + try print_pkg_data_to(w, &duped, &done); + try w.writeAll("};\n\n"); + + try w.writeAll("pub const packages = "); + try print_deps(w, top_module); + try w.writeAll(";\n\n"); + + try w.writeAll("pub const pkgs = "); + try print_pkgs(alloc, w, top_module); + try w.writeAll(";\n\n"); + + try w.writeAll("pub const imports = struct {\n"); + try print_imports(alloc, w, top_module, cachepath); + try w.writeAll("};\n"); +} + +fn create_lockfile(alloc: std.mem.Allocator, list: *std.array_list.Managed(zigmod.Module), path: string, dir: nfs.Dir) !void { + const fl = try dir.createFile("zigmod.lock", .{}); + defer fl.close(); + + std.mem.sort(zigmod.Module, list.items, {}, zigmod.Module.lessThan); + + const wl = fl; + try wl.writeAll("2\n"); + for (list.items) |m| { + if (m.dep) |md| { + if (md.type.isLocal()) continue; + const mpath = try std.fs.path.joinZ(alloc, &.{ path, m.clean_path }); + const version = try md.exact_version(alloc, mpath); + try wl.print("{s} {s} {s}\n", .{ @tagName(md.type), md.path, version }); + } + } +} + +const DiffChange = struct { + from: string, + to: string, +}; + +fn diff_lockfile(alloc: std.mem.Allocator) !void { + const max = std.math.maxInt(usize); + + if (try nfs.cwd().existsDir(".git")) { + const result = try u.run_cmd_raw(alloc, null, &.{ "git", "diff", "zigmod.lock" }); + var stdout = nio.FixedBufferStream([]const u8).init(result.stdout); + const r = &stdout; + while (try r.readUntilDelimiterOrEofAlloc(alloc, '\n', max)) |line| { + if (std.mem.startsWith(u8, line, "@@")) break; + } + + var rems = std.array_list.Managed(string).init(alloc); + var adds = std.array_list.Managed(string).init(alloc); + while (try r.readUntilDelimiterOrEofAlloc(alloc, '\n', max)) |line_full| { + const line = line_full[0 .. line_full.len - 1]; + if (line[0] == ' ') continue; + if (line[0] == '-') try rems.append(line[1..]); + if (line[0] == '+') if (line[1] == '2') break else try adds.append(line[1..]); + } + + var changes = std.StringHashMap(DiffChange).init(alloc); + + var didbreak = false; + var i: usize = 0; + while (i < rems.items.len) { + const it = rems.items[i]; + const sni = u.indexOfN(it, ' ', 2).?; + + var j: usize = 0; + while (j < adds.items.len) { + const jt = adds.items[j]; + const snj = u.indexOfN(jt, ' ', 2).?; + + if (std.mem.eql(u8, it[0..sni], jt[0..snj])) { + try changes.put(it[0..sni], .{ + .from = it[u.indexOfAfter(it, '-', sni).? + 1 .. it.len], + .to = jt[u.indexOfAfter(jt, '-', snj).? + 1 .. jt.len], + }); + _ = rems.orderedRemove(i); + _ = adds.orderedRemove(j); + didbreak = true; + break; + } + if (!didbreak) j += 1; + } + if (!didbreak) i += 1; + if (didbreak) didbreak = false; + } + + if (adds.items.len > 0) { + std.debug.print(comptime ansi.color.Faint("Newly added packages:\n"), .{}); + defer std.debug.print("\n", .{}); + + for (adds.items) |it| { + std.debug.print("- {s}\n", .{it}); + } + } + + if (rems.items.len > 0) { + std.debug.print(comptime ansi.color.Faint("Removed packages:\n"), .{}); + defer std.debug.print("\n", .{}); + + for (rems.items) |it| { + std.debug.print("- {s}\n", .{it}); + } + } + + if (changes.unmanaged.size > 0) std.debug.print(comptime ansi.color.Faint("Updated packages:\n"), .{}); + var iter = changes.iterator(); + while (iter.next()) |it| { + if (diff_printchange("git https://github.com", "- {s}/compare/{s}...{s}\n", it)) continue; + if (diff_printchange("git https://gitlab.com", "- {s}/-/compare/{s}...{s}\n", it)) continue; + if (diff_printchange("git https://gitea.com", "- {s}/compare/{s}...{s}\n", it)) continue; + + std.debug.print("- {s}\n", .{it.key_ptr.*}); + std.debug.print(" - {s} ... {s}\n", .{ it.value_ptr.from, it.value_ptr.to }); + } + } +} + +fn diff_printchange(comptime testt: string, comptime replacement: string, item: std.StringHashMap(DiffChange).Entry) bool { + if (std.mem.startsWith(u8, item.key_ptr.*, testt)) { + if (std.mem.eql(u8, item.value_ptr.from, item.value_ptr.to)) return true; + std.debug.print(replacement, .{ item.key_ptr.*[4..], item.value_ptr.from, item.value_ptr.to }); + return true; + } + return false; +} + +fn print_dirs(w: nfs.File, 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, &zigmod.Module.ROOT)) { + try w.writeAll(" pub const _root = \"\";\n"); + continue; + } + if (std.mem.eql(u8, mod.clean_path, "../..")) { + const cwd_realpath = try nfs.cwd().realpathAlloc(alloc, "."); + try w.print(" pub const _{s} = \"{}\";\n", .{ mod.short_id(), u.altStringEscape(cwd_realpath) }); + continue; + } + try w.print(" pub const _{s} = cache ++ \"/{}\";\n", .{ mod.short_id(), u.altStringEscape(mod.clean_path) }); + } +} + +fn print_deps(w: nfs.File, m: zigmod.Module) !void { + try w.writeAll("&[_]*const Package{\n"); + for (m.deps) |d| { + if (d.main.len == 0) { + continue; + } + if (d.for_build) { + continue; + } + try w.print(" &package_data._{s},\n", .{d.id[0..12]}); + } + try w.writeAll("}"); +} + +fn print_pkg_data_to(w: nfs.File, notdone: *std.array_list.Managed(zigmod.Module), done: *std.array_list.Managed(zigmod.Module)) !void { + var len: usize = notdone.items.len; + while (notdone.items.len > 0) { + for (notdone.items, 0..) |mod, i| { + if (contains_all(mod.deps, done.items)) { + try w.print( + \\ pub const _{s} = Package{{ + \\ .id = "{s}", + \\ .directory = dirs._{s}, + \\ + , .{ + mod.short_id(), + mod.short_id(), + mod.short_id(), + }); + if (mod.main.len > 0 and !std.mem.eql(u8, &mod.id, &zigmod.Module.ROOT)) { + try w.print( + \\ .import = .{{ "{s}", .{{ .cwd_relative = dirs._{s} ++ "/{s}" }} }}, + \\ + , .{ + mod.name, + mod.short_id(), + mod.main, + }); + } + { + try w.writeAll(" .dependencies ="); + try w.writeAll(" &.{"); + for (mod.deps, 0..) |moddep, j| { + if (moddep.type == .system_lib) continue; + if (moddep.type == .framework) continue; + 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 = &.{"); + for (mod.c_include_dirs, 0..) |item, j| { + try w.print(" \"{}\"", .{u.altStringEscape(item)}); + if (j != mod.c_include_dirs.len - 1) try w.writeAll(","); + } + try w.writeAll(" },\n"); + } + if (mod.c_source_files.len > 0) { + try w.writeAll(" .c_source_files = &.{"); + for (mod.c_source_files, 0..) |item, j| { + try w.print(" \"{}\"", .{u.altStringEscape(item)}); + if (j != mod.c_source_files.len - 1) try w.writeAll(","); + } + try w.writeAll(" },\n"); + } + if (mod.c_source_flags.len > 0) { + try w.writeAll(" .c_source_flags = &.{"); + for (mod.c_source_flags, 0..) |item, j| { + try w.print(" \"{}\"", .{u.altStringEscape(item)}); + if (j != mod.c_source_flags.len - 1) try w.writeAll(","); + } + try w.writeAll(" },\n"); + } + if (mod.has_syslib_deps()) { + try w.writeAll(" .system_libs = &.{"); + for (mod.deps, 0..) |item, j| { + if (!(item.type == .system_lib)) continue; + try w.print(" \"{}\"", .{u.altStringEscape(item.name)}); + if (j != mod.deps.len - 1) try w.writeAll(","); + } + try w.writeAll(" },\n"); + } + if (mod.has_framework_deps()) { + try w.writeAll(" .frameworks = &.{"); + for (mod.deps, 0..) |item, j| { + if (!(item.type == .system_lib)) continue; + try w.print(" \"{}\"", .{u.altStringEscape(item.name)}); + if (j != mod.deps.len - 1) try w.writeAll(","); + } + try w.writeAll(" },\n"); + } + try w.writeAll(" };\n"); + + try done.append(mod); + _ = notdone.orderedRemove(i); + break; + } + } + if (notdone.items.len == len) { + u.fail("notdone still has {d} items", .{len}); + } + len = notdone.items.len; + } +} + +/// returns if all of the zig modules in needles are in haystack +fn contains_all(needles: []zigmod.Module, haystack: []const zigmod.Module) bool { + for (needles) |item| { + if (item.main.len > 0 and !extras.containsAggregate(zigmod.Module, haystack, item)) { + return false; + } + } + return true; +} + +fn print_pkgs(alloc: std.mem.Allocator, w: nfs.File, m: zigmod.Module) !void { + try w.writeAll("struct {\n"); + for (m.deps) |d| { + if (d.main.len == 0) { + continue; + } + if (d.for_build) { + continue; + } + const ident = try zig_name_from_pkg_name(alloc, d.name); + try w.print(" pub const {s} = &package_data._{s};\n", .{ ident, d.id[0..12] }); + } + try w.writeAll("}"); +} + +fn print_imports(alloc: std.mem.Allocator, w: nfs.File, m: zigmod.Module, path: string) !void { + for (m.deps) |d| { + if (d.main.len == 0) { + continue; + } + if (!d.for_build) { + continue; + } + const ident = try zig_name_from_pkg_name(alloc, d.name); + try w.print(" pub const {s} = @import(\"{}/{}/{s}\");\n", .{ ident, u.altStringEscape(path), u.altStringEscape(d.clean_path), d.main }); + } +} + +fn zig_name_from_pkg_name(alloc: std.mem.Allocator, name: string) !string { + var legal = name; + legal = try std.mem.replaceOwned(u8, alloc, legal, "-", "_"); + legal = try std.mem.replaceOwned(u8, alloc, legal, "/", "_"); + legal = try std.mem.replaceOwned(u8, alloc, legal, ".", "_"); + return legal; +} diff --git a/src/cmd/fetch.zig b/src/cmd/fetch.zig index b93526b43ed12ccedee364fd04b5e462abf1fa06..6665984c64321809e8d9d2172dff45875195c4d4 100644 --- a/src/cmd/fetch.zig +++ b/src/cmd/fetch.zig @@ -54,9 +54,12 @@ pub fn create_depszig(alloc: std.mem.Allocator, cachepath: string, dir: nfs.Dir, 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.print("const any_translate_c = {};\n", .{top_module.any_translate_c()}); + try w.writeAll("const Translator = @import(\"translate_c\").Translator; // need to depend on ziglang/translate-c#2.0.2\n"); try w.writeAll("\n"); try w.print("pub const cache = \"{}\";\n", .{u.altStringEscape(cachepath)}); try w.writeAll("pub var skip_libc = false;\n"); + try w.writeAll("pub var translate_c: ?*std.Build.Dependency = null;\n"); try w.writeAll("\n"); try w.writeAll( \\pub fn addAllTo(exe: *std.Build.Step.Compile) void { @@ -86,6 +89,7 @@ pub fn create_depszig(alloc: std.mem.Allocator, cachepath: string, dir: nfs.Dir, \\ c_source_flags: []const string = &.{}, \\ system_libs: []const string = &.{}, \\ frameworks: []const string = &.{}, + \\ translate_c_h: ?string = null, \\ \\ pub fn module(self: *const Package, exe: *std.Build.Step.Compile) *std.Build.Module { \\ if (module_memo.get(self.id)) |cached| { @@ -121,6 +125,20 @@ pub fn create_depszig(alloc: std.mem.Allocator, cachepath: string, dir: nfs.Dir, \\ exe.root_module.addCSourceFile(.{ .file = .{ .cwd_relative = (b.fmt("{s}/{s}", .{ self.directory, item })) }, .flags = self.c_source_flags }); \\ link_lib_c = true; \\ } + \\ if (any_translate_c) if (self.translate_c_h) |h| { + \\ if (translate_c == null) translate_c = b.dependency("translate_c", .{}); + \\ const t: Translator = .init(translate_c.?, .{ + \\ .c_source_file = b.addWriteFile("c.h", b.fmt("// {s}\n#include <{s}>\n", .{ self.id, h })).getDirectory().path(b, "c.h"), + \\ .target = exe.root_module.resolved_target.?, + \\ .optimize = exe.root_module.optimize.?, + \\ }); + \\ for (exe.root_module.include_dirs.items) |d| { + \\ if (d != .path) continue; + \\ t.addIncludePath(d.path); + \\ } + \\ result.addImport("c", t.mod); + \\ link_lib_c = true; + \\ }; \\ for (self.system_libs) |item| { \\ if (skip_libc and std.zig.target.isLibCLibName(&target, item)) continue; \\ result.linkSystemLibrary(item, .{}); @@ -402,6 +420,9 @@ fn print_pkg_data_to(w: nfs.File, notdone: *std.array_list.Managed(zigmod.Module } try w.writeAll(" },\n"); } + if (mod.translate_c_h) |h| { + try w.print(" .translate_c_h = \"{s}\",\n", .{h}); + } try w.writeAll(" };\n"); try done.append(mod); diff --git a/src/cmd/generate.zig b/src/cmd/generate.zig index 78c7bd1ebe3acb0005d83f7b9cddb5cb02f8eb3c..d5ef87090ac423baf18c18a3ee3c14386aa38533 100644 --- a/src/cmd/generate.zig +++ b/src/cmd/generate.zig @@ -44,6 +44,10 @@ pub fn create_depszig(alloc: std.mem.Allocator, cachepath: [:0]const u8, dir: nf 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.print("const any_translate_c = {};\n", .{top_module.any_translate_c()}); + try w.writeAll("const Translator = @import(\"translate_c\").Translator; // need to depend on ziglang/translate-c#2.0.2\n"); + try w.writeAll("\n"); + try w.writeAll("pub var translate_c: ?*std.Build.Dependency = null;\n"); try w.writeAll("\n"); try w.writeAll( \\pub const GitExactStep = struct { @@ -107,7 +111,7 @@ pub fn create_depszig(alloc: std.mem.Allocator, cachepath: [:0]const u8, dir: nf \\ exe.root_module.addImport(pkg.name, module); \\ } \\ // clear module memo cache so addAllTo can be called more than once in the same build.zig - \\ inline for (comptime std.meta.declarations(package_data)) |decl| @field(package_data, decl.name).module_memo = null; + \\ inline for (comptime std.meta.declarations(package_data)) |decl| @field(package_data, decl).module_memo = null; \\} \\ \\pub fn dependOnGit(ge: GitExactStep, s: *std.Build.Step, p: *Package) void { @@ -127,6 +131,7 @@ pub fn create_depszig(alloc: std.mem.Allocator, cachepath: [:0]const u8, dir: nf \\ system_libs: []const string = &.{}, \\ frameworks: []const string = &.{}, \\ module_memo: ?*std.Build.Module = null, + \\ translate_c_h: ?string = null, \\ \\ pub fn module(self: *Package, exe: *std.Build.Step.Compile, fetch_step: *std.Build.Step) *std.Build.Module { \\ if (self.module_memo) |cached| { @@ -171,6 +176,7 @@ pub fn create_depszig(alloc: std.mem.Allocator, cachepath: [:0]const u8, dir: nf \\ for (self.c_include_dirs) |item| { \\ result.addIncludePath(self.path.?.path(b, item)); \\ dummy_library.root_module.addIncludePath(self.path.?.path(b, item)); + \\ exe.root_module.addIncludePath(self.path.?.path(b, item)); \\ link_lib_c = true; \\ links += 1; \\ } @@ -178,6 +184,20 @@ pub fn create_depszig(alloc: std.mem.Allocator, cachepath: [:0]const u8, dir: nf \\ dummy_library.root_module.addCSourceFile(.{ .file = self.path.?.path(b, item), .flags = self.c_source_flags }); \\ links += 1; \\ } + \\ if (any_translate_c) if (self.translate_c_h) |h| { + \\ if (translate_c == null) translate_c = b.dependency("translate_c", .{}); + \\ const t: Translator = .init(translate_c.?, .{ + \\ .c_source_file = b.addWriteFile("c.h", b.fmt("// {s}\n#include <{s}>\n", .{ self.name, h })).getDirectory().path(b, "c.h"), + \\ .target = exe.root_module.resolved_target.?, + \\ .optimize = exe.root_module.optimize.?, + \\ }); + \\ for (exe.root_module.include_dirs.items) |d| { + \\ if (d != .path) continue; + \\ t.addIncludePath(d.path); + \\ } + \\ result.addImport("c", t.mod); + \\ link_lib_c = true; + \\ }; \\ for (self.system_libs) |item| { \\ if (std.zig.target.isLibCLibName(&target, item)) continue; \\ dummy_library.root_module.linkSystemLibrary(item, .{}); @@ -329,6 +349,9 @@ fn print_pkg_data_to(w: nfs.File, notdone: *std.array_list.Managed(zigmod.Module } try w.writeAll(" },\n"); } + if (mod.translate_c_h) |h| { + try w.print(" .translate_c_h = \"{s}\",\n", .{h}); + } try w.writeAll(" };\n"); try done.append(mod); diff --git a/src/cmd/init.zig b/src/cmd/init.zig index a95784ef668eca48df0d8d440fe37bb3f655d628..7e7de3c8c50941e417bc8de3930ef566b2efdecd 100644 --- a/src/cmd/init.zig +++ b/src/cmd/init.zig @@ -44,7 +44,7 @@ pub fn execute(self_name: []const u8, args: []const [:0]const u8) !void { error.CantFindMain => try std.mem.concat(gpa, u8, &.{ flatname, ".zig" }), else => |ee| return ee, }) else null; - const entry = if (entry_bare) |ent| try gpa.dupeZ(u8, ent) else null; + const entry = if (entry_bare) |ent| try gpa.dupeSentinel(u8, ent, 0) else null; const maybe_license_text = try detectlicense.detectInDir(gpa, cwd); const license = try inquirer.forString(stdout, stdin, "license:", gpa, maybe_license_text); @@ -153,6 +153,7 @@ pub fn execute(self_name: []const u8, args: []const [:0]const u8) !void { } if (!exists) try w.writeAll(".zig-cache\n"); if (!exists) try w.writeAll("zig-out\n"); + if (!exists) try w.writeAll("zig-pkg\n"); try w.writeAll(".zigmod\n"); try w.writeAll("deps.zig\n"); try w.writeAll("files.zig\n"); @@ -194,7 +195,7 @@ pub fn execute(self_name: []const u8, args: []const [:0]const u8) !void { \\ \\pub fn build(b: *std.Build) void { \\ const target = b.standardTargetOptions(.{}); - \\ const mode = b.option(std.builtin.Mode, "mode", "") orelse .Debug; + \\ const optimize = b.standardOptimizeOption(.{}); \\ \\ const exe = b.addExecutable(.{ \\ @@ -203,7 +204,7 @@ pub fn execute(self_name: []const u8, args: []const [:0]const u8) !void { try w.writeAll( \\ .root_source_file = b.path("main.zig"), \\ .target = target, - \\ .optimize = mode, + \\ .optimize = optimize, \\ }); \\ deps.addAllTo(exe); \\ b.installArtifact(exe); @@ -224,12 +225,12 @@ pub fn execute(self_name: []const u8, args: []const [:0]const u8) !void { \\ \\pub fn build(b: *std.Build) void { \\ const target = b.standardTargetOptions(.{}); - \\ const mode = b.option(std.builtin.Mode, "mode", "") orelse .Debug; + \\ const optimize = b.standardOptimizeOption(.{}); \\ \\ const tests = b.addTest(.{ \\ .root_source_file = b.path("test.zig"), \\ .target = target, - \\ .optimize = mode, + \\ .optimize = optimize, \\ }); \\ deps.addAllTo(tests); \\ diff --git a/src/cmd/install.zig b/src/cmd/install.zig index 476d5ded3a682471400c5fe4fdafee036365a15d..fd784178cf5078ac65efbaf1898bd2e2744dfd1b 100644 --- a/src/cmd/install.zig +++ b/src/cmd/install.zig @@ -67,7 +67,7 @@ pub fn execute(self_name: []const u8, args: []const [:0]const u8) !void { const modfile = try zigmod.ModFile.from_dir(gpa, moddir, modpath); const zigversion_sv = modfile.min_zig_version orelse u.fail("zigmod manifest requires min_zig_version field", .{}); - const zigversion = try nio.fmt.allocPrint(gpa, "{s}", .{u.altSemanticVersion(zigversion_sv)}); + const zigversion = try nio.fmt.allocPrint(gpa, "{}", .{u.altSemanticVersion(zigversion_sv)}); const zigpath = try std.fs.path.joinZ(gpa, &.{ datapath, "zig", zigversion, "zig" }); // regen deps.zig with the repo's zig version @@ -89,7 +89,8 @@ pub fn execute(self_name: []const u8, args: []const [:0]const u8) !void { 0x0000_000d => try @import("./fetch.0.13.zig").create_depszig(gpa, cachepath, moddir, fetch_top_module, &fetch_list), 0x0000_000e => try @import("./fetch.0.14.zig").create_depszig(gpa, cachepath, moddir, fetch_top_module, &fetch_list), 0x0000_000f => try @import("./fetch.0.15.zig").create_depszig(gpa, cachepath, moddir, fetch_top_module, &fetch_list), - 0x0000_0010 => {}, // that's us, zigmod is already 0.16.0 + 0x0000_0010 => try @import("./fetch.0.16.zig").create_depszig(gpa, cachepath, moddir, fetch_top_module, &fetch_list), + 0x0000_0011 => {}, // that's us, zigmod is already 0.17.0 else => u.fail("zig {d}.{d} unimplemented", .{ version_sct.major, version_sct.minor }), } diff --git a/src/cmd/zpm.zig b/src/cmd/zpm.zig index 331e1e4ce22dc813135da963869bf955f2606ff2..a9345e69c60b2c20f0999f8919fd9c4623db000b 100644 --- a/src/cmd/zpm.zig +++ b/src/cmd/zpm.zig @@ -48,8 +48,8 @@ pub fn execute(self_name: []const u8, args: []const [:0]const u8) !void { } inline for (comptime std.meta.declarations(commands)) |decl| { - if (std.mem.eql(u8, args[0], decl.name)) { - const cmd = @field(commands, decl.name); + if (std.mem.eql(u8, args[0], decl)) { + const cmd = @field(commands, decl); try cmd.execute(self_name, args[1..]); return; } diff --git a/src/common.zig b/src/common.zig index 281ac856e190aa574757474657f53ea526b56acc..7f78fa48e0d885ff552e622690895477e27776fd 100644 --- a/src/common.zig +++ b/src/common.zig @@ -58,6 +58,7 @@ pub fn collect_deps_deep(cachepath: [:0]const u8, mdir: nfs.Dir, options: *Colle .yaml = m.yaml, .dep = null, .min_zig_version = m.min_zig_version, + .translate_c_h = m.translate_c_h, }; } @@ -88,6 +89,7 @@ pub fn collect_deps(cachepath: [:0]const u8, mdir: nfs.Dir, mdir_path: string, d .yaml = m.yaml, .dep = null, .min_zig_version = m.min_zig_version, + .translate_c_h = m.translate_c_h, }; } @@ -121,7 +123,7 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) ! switch (d.type) { .local => { if (!std.mem.endsWith(u8, d.main, ".zig")) { - return gpa.dupeZ(u8, d.main); + return gpa.dupeSentinel(u8, d.main, 0); } return d.path; }, @@ -206,7 +208,7 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) ! } const file_path_s = try std.fs.path.resolve(options.alloc, &.{ p, file_name }); defer gpa.free(file_path_s); - const file_path = try gpa.dupeZ(u8, file_path_s); + const file_path = try gpa.dupeSentinel(u8, file_path_s, 0); try d.type.pull(options.alloc, d.path, p); try nfs.cwd().deleteFile(file_path); return p; @@ -219,7 +221,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: [:0]const u8, options: *Co for (lock) |item| { if (std.mem.eql(u8, item[0], try d.clean_path(options.alloc))) { d.type = std.meta.stringToEnum(zigmod.Dep.Type, item[1]).?; - d.path = try gpa.dupeZ(u8, item[2]); + d.path = try gpa.dupeSentinel(u8, item[2], 0); d.version = item[3]; break; } @@ -247,6 +249,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: [:0]const u8, options: *Co .dep = d.*, .for_build = d.for_build, .min_zig_version = null, + .translate_c_h = null, }; }, else => { @@ -384,7 +387,7 @@ pub fn parse_lockfile(alloc: std.mem.Allocator, dir: nfs.Dir) ![]const [4]string var iter = std.mem.splitScalar(u8, line, ' '); const asdep = zigmod.Dep{ .type = std.meta.stringToEnum(zigmod.Dep.Type, iter.next().?).?, - .path = try gpa.dupeZ(u8, iter.next().?), + .path = try gpa.dupeSentinel(u8, iter.next().?, 0), .version = iter.next().?, .id = zigmod.Dep.EMPTY, .name = "", diff --git a/src/main.zig b/src/main.zig index 3de391d233d50944ca1da780ec8ac460689d40b2..b2274950472efe31dc4b0d0350a8d1e8f0c795fb 100644 --- a/src/main.zig +++ b/src/main.zig @@ -39,7 +39,7 @@ pub fn main(init: std.process.Init.Minimal) !void { std.debug.print("\n", .{}); std.debug.print("The commands available are:\n", .{}); inline for (comptime std.meta.declarations(zigmod.commands)) |decl| { - std.debug.print(" - {s}\n", .{decl.name}); + std.debug.print(" - {s}\n", .{decl}); } return; } @@ -58,8 +58,8 @@ pub fn main(init: std.process.Init.Minimal) !void { defer zigmod.deinit(); inline for (comptime std.meta.declarations(zigmod.commands)) |decl| { - if (std.mem.eql(u8, args[0], decl.name)) { - const cmd = @field(zigmod.commands, decl.name); + if (std.mem.eql(u8, args[0], decl)) { + const cmd = @field(zigmod.commands, decl); try cmd.execute(self_path, args[1..]); return; } diff --git a/src/util/dep.zig b/src/util/dep.zig index bba2e795daf811ea5dce11460f06a03d908b271b..d13b6e9d72cd9d70386247c26cb86251997d4c65 100644 --- a/src/util/dep.zig +++ b/src/util/dep.zig @@ -28,8 +28,9 @@ pub const Dep = struct { deps: []zigmod.Dep, keep: bool = false, for_build: bool = false, + translate_c_h: ?string = null, - pub const EMPTY = (" " ** 48).*; + pub const EMPTY: [48]u8 = @splat(' '); pub const Type = @import("./dep_type.zig").DepType; diff --git a/src/util/funcs.zig b/src/util/funcs.zig index c3a48dc0ccd404ae396de14659497bc8eb3d969f..6ebba2d7faa0f92ffe42a87bab043571b97f6236 100644 --- a/src/util/funcs.zig +++ b/src/util/funcs.zig @@ -56,7 +56,7 @@ pub fn file_list(alloc: std.mem.Allocator, dpath: [:0]const u8) ![]const [:0]con defer walker.deinit(); while (try walker.next()) |entry| { if (entry.type != .REG) continue; - try list.append(try alloc.dupeZ(u8, entry.path)); + try list.append(try alloc.dupeSentinel(u8, entry.path, 0)); } return list.toOwnedSlice(); } @@ -230,7 +230,7 @@ pub fn find_cachepath() ![:0]const u8 { const needle = "/.zigmod/deps"; if (std.mem.indexOf(u8, haystack, needle)) |index| { - return gpa.dupeZ(u8, haystack[0 .. index + needle.len]); + return gpa.dupeSentinel(u8, haystack[0 .. index + needle.len], 0); } return try std.fs.path.joinZ(gpa, &.{ haystack, ".zigmod", "deps" }); } diff --git a/src/util/modfile.zig b/src/util/modfile.zig index 745f1e36becd3ecd9cd34c88d9410498ec0a48ff..ff4f3e0080f0d398f1c1b14d0cccb34df25a9382 100644 --- a/src/util/modfile.zig +++ b/src/util/modfile.zig @@ -33,6 +33,7 @@ pub const ModFile = struct { rootdeps: []zigmod.Dep, builddeps: []zigmod.Dep, min_zig_version: ?std.SemanticVersion, + translate_c_h: ?string = null, pub fn openFile(dir: nfs.Dir, ops: nfs.Dir.OpenFileFlags) !struct { string, nfs.File } { return .{ "zig.mod", dir.openFile("zig.mod", ops) catch |err| switch (err) { @@ -91,6 +92,7 @@ pub const ModFile = struct { .rootdeps = try dep_list_by_name(alloc, mapping, &.{ "dev_dependencies", "root_dependencies" }, false), .builddeps = try dep_list_by_name(alloc, mapping, &.{ "dev_dependencies", "build_dependencies" }, true), .min_zig_version = std.SemanticVersion.parse(mapping.get_string("min_zig_version") orelse "") catch null, + .translate_c_h = mapping.get_string("translate_c_h"), }; } @@ -110,9 +112,9 @@ pub const ModFile = struct { if (item.mapping.get("src")) |val| { var src_iter = std.mem.tokenizeScalar(u8, val.string, ' '); dtype = src_iter.next().?; - path = try gpa.dupeZ(u8, src_iter.next().?); + path = try gpa.dupeSentinel(u8, src_iter.next().?, 0); if (src_iter.next()) |dver| { - version = try gpa.dupeZ(u8, dver); + version = try gpa.dupeSentinel(u8, dver, 0); } } else { dtype = item.mapping.get("type").?.string; @@ -151,6 +153,7 @@ pub const ModFile = struct { .deps = try dep_list_by_name(alloc, item.mapping, &.{"dependencies"}, for_build), .keep = std.mem.eql(u8, "true", item.mapping.get_string("keep") orelse ""), .for_build = for_build, + .translate_c_h = item.mapping.get_string("translate_c_h"), }); // const d = &dep_list.getLast(); } diff --git a/src/util/module.zig b/src/util/module.zig index 8ebf25fb2247308c201e00fe8e742062d40484b3..5bf8854e1e0865ee681d2d17fc1c94f37a18bc33 100644 --- a/src/util/module.zig +++ b/src/util/module.zig @@ -29,8 +29,9 @@ pub const Module = struct { dep: ?zigmod.Dep, for_build: bool = false, min_zig_version: ?std.SemanticVersion, + translate_c_h: ?string, - pub const ROOT: [48]u8 = ("root" ++ (" " ** 44)).*; + pub const ROOT: [48]u8 = ("root" ++ @as([44]u8, @splat(' '))).*; pub fn from(alloc: std.mem.Allocator, dep: zigmod.Dep, cachepath: [:0]const u8, options: *common.CollectOptions) !Module { var moddeps = std.array_list.Managed(Module).init(alloc); @@ -61,6 +62,7 @@ pub const Module = struct { .dep = dep, .for_build = dep.for_build, .min_zig_version = null, + .translate_c_h = dep.translate_c_h, }; } @@ -80,7 +82,7 @@ pub const Module = struct { try file_list_2.append(_b); } - std.mem.sort(string, file_list_2.items, void{}, struct { + std.mem.sort(string, file_list_2.items, {}, struct { pub fn lt(context: void, lhs: string, rhs: string) bool { _ = context; return std.mem.lessThan(u8, lhs, rhs); @@ -204,4 +206,10 @@ pub const Module = struct { }, }; } + + pub fn any_translate_c(self: *const Module) bool { + if (self.translate_c_h) |_| return true; + for (self.deps) |d| if (d.any_translate_c()) return true; + return false; + } }; diff --git a/zig.mod b/zig.mod index 18d742553906550f2dc749cdaab58019fa33e094..3e8c6529715b45178cc8d35cc2b9a718b343db4b 100644 --- a/zig.mod +++ b/zig.mod @@ -3,7 +3,7 @@ name: zigmod main: src/lib.zig license: MIT description: A package manager for the Zig programming language. -min_zig_version: 0.16.0 +min_zig_version: 0.17.0 min_zigmod_version: r96 dependencies: - src: git https://github.com/nektro/zig-yaml @@ -13,7 +13,7 @@ dependencies: name: known-folders main: known-folders.zig license: MIT - version: commit-207c34a16e4365edc20d92c7892f962b3bed46e8 + version: commit-6da0d0c41b78b9ed2d34fa364fcb81b5ebece6c4 - src: git https://github.com/nektro/zig-licenses - src: git https://github.com/nektro/zig-detect-license - src: git https://github.com/nektro/zig-inquirer diff --git a/zigmod.lock b/zigmod.lock index 8460295623f609c6f5bf36b8b8210aa4cd183c56..4c7683ddde8bc69e982f165686dce24076773a21 100644 --- a/zigmod.lock +++ b/zigmod.lock @@ -1,26 +1,26 @@ 2 git https://github.com/marlersoft/zigwin32 commit-fb2645891520f3e99931db17a7eba04057e87e89 -git https://github.com/nektro/arqv-ini commit-d2465c64833590a04bd9b7f50c87363fd03e65bf -git https://github.com/nektro/zig-ansi commit-bebb39ae30d9848a1c212cee582d0b1c102d8b87 -git https://github.com/nektro/zig-detect-license commit-6de79b4ff8f7462e26f224f4bed3c81710c8893b -git https://github.com/nektro/zig-extras commit-0c90ee9a82d8863e0e142211a587940ee2a09790 -git https://github.com/nektro/zig-git commit-88b94a1960be841bb93a0bca65d556cb6385d1fd -git https://github.com/nektro/zig-inquirer commit-e745b018f87ddc4c8958370332e6cf61625403cc -git https://github.com/nektro/zig-intrusive-parser commit-2409be829d989bd03b6d123159880660c6089820 -git https://github.com/nektro/zig-json commit-d32454ad6d1a6e6e58a8c12a4f3ee1223be2e316 -git https://github.com/nektro/zig-leven commit-014971d4b1e327a5c61322c3a6b9369fd1863864 -git https://github.com/nektro/zig-licenses commit-bc1cd51625c13d6c7df819fd0d85968e617f5c57 -git https://github.com/nektro/zig-licenses-text commit-a9d067c3c4d6c226a1de5531b66785bc15d869ee -git https://github.com/nektro/zig-nfs commit-1a27fe574706f0b91c9f66b22fe7d3fa8f5129d5 -git https://github.com/nektro/zig-nio commit-17c4f044b15917efc1d3f018765881d53fa27e48 -git https://github.com/nektro/zig-sys-darwin commit-b349aa32b55aae4cf2ae56740003f8aff0394920 -git https://github.com/nektro/zig-sys-freebsd commit-f97e6c2de30af76818572490735c84ce1015958b -git https://github.com/nektro/zig-sys-linux commit-6dabe7cce239d882c8fd132ab5cda1d1f9ff3634 -git https://github.com/nektro/zig-sys-netbsd commit-5ad432ca421c59099be3630b9d0dda49e8bef0a7 -git https://github.com/nektro/zig-sys-openbsd commit-164646a5e7c4129eb92ac524a60f1759605c99df -git https://github.com/nektro/zig-time commit-1349fb2c8aaaa1503e8730facea6f87c5fe75dc0 -git https://github.com/nektro/zig-tracer commit-4ad0602d52a3f40ef74f59f648602c61409f7ba7 -git https://github.com/nektro/zig-yaml commit-dff9fd43ebf1046c70090bdccfeb2e4f1c0d584b +git https://github.com/nektro/arqv-ini commit-2951069265792192bd714980d44ccb4abee4a6e8 +git https://github.com/nektro/zig-ansi commit-952d0751c359612fc1a7c493453cc6223bbaff5d +git https://github.com/nektro/zig-detect-license commit-2ec231c2ae5274d926851f355f0896e0fcbbb5d0 +git https://github.com/nektro/zig-extras commit-fb3aa273de9116bbb7befd448835b854d906f2ee +git https://github.com/nektro/zig-git commit-f4bd29ad59967102db4532bda2d95d1512fbf3fb +git https://github.com/nektro/zig-inquirer commit-6feae6d21ac54cba8e5d4da91868c4db9f82343b +git https://github.com/nektro/zig-intrusive-parser commit-6d074187f8c5aa2bdab11ee57c7180df768fca72 +git https://github.com/nektro/zig-json commit-4f6aa411f1dc93cc86a052d8a69804c8ff762e59 +git https://github.com/nektro/zig-leven commit-d990af4f7706edcb0d928d953d1a56c2830fa036 +git https://github.com/nektro/zig-licenses commit-1fd04fc1b7619ca2ddfd07354a93e6d72334f76f +git https://github.com/nektro/zig-licenses-text commit-a20d31da306967253b8a46c60d8368c7ac4b7229 +git https://github.com/nektro/zig-nfs commit-b32bb30bbdc7e5ad9bb3adb5c477a98897c9cb10 +git https://github.com/nektro/zig-nio commit-b28c8efc86f3baec3737de86ffde5c4b3a5260f6 +git https://github.com/nektro/zig-sys-darwin commit-312638dcf043c3163e260ec5b42610856fc9eeab +git https://github.com/nektro/zig-sys-freebsd commit-967ac568e385534825e6849bc82a9b51d45c600c +git https://github.com/nektro/zig-sys-linux commit-750d2aa874a9ddb473e7cd0af99f1e722aaab79d +git https://github.com/nektro/zig-sys-netbsd commit-e16386268223744ac3f6036a028cc4359056731b +git https://github.com/nektro/zig-sys-openbsd commit-081e11079887e8f02d8480b6219a13a41afd8d69 +git https://github.com/nektro/zig-time commit-29f3f902cc8948faa36b853380c38ff587236661 +git https://github.com/nektro/zig-tracer commit-ccf9a6b85a38cc343aba772f2d2aac4df49000e1 +git https://github.com/nektro/zig-yaml commit-e4e8dfe78a3a0e39d134382a18e8eb771418e850 git https://github.com/madler/zlib tag-v1.3.2 git https://github.com/yaml/libyaml tag-0.2.5 -git https://github.com/ziglibs/known-folders commit-207c34a16e4365edc20d92c7892f962b3bed46e8 +git https://github.com/ziglibs/known-folders commit-6da0d0c41b78b9ed2d34fa364fcb81b5ebece6c4