robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

bfredlMerge pull request #38546 from dchinmay2/push-pozrrzptmvnve684a3dd18

master
5.8 KiB160 linesraw
1const std = @import("std");
2const gen = @import("gen/gen_steps.zig");
3
4pub fn lintSources(
5    b: *std.Build,
6    nvim_sources: std.ArrayList(gen.SourceItem),
7    nvim_headers: std.ArrayList([]u8),
8) !std.ArrayList([]const u8) {
9    var lint_sources: std.ArrayList([]const u8) = try .initCapacity(b.allocator, 0);
10    for (nvim_sources.items) |source| {
11        try lint_sources.append(b.allocator, b.fmt("src/nvim/{s}", .{source.name}));
12    }
13    for (nvim_headers.items) |header| {
14        try lint_sources.append(b.allocator, b.fmt("src/nvim/{s}", .{header}));
15    }
16    try lint_sources.append(b.allocator, "src/tee/tee.c");
17    try lint_sources.append(b.allocator, "src/xxd/xxd.c");
18    return lint_sources;
19}
20
21pub fn addSteps(
22    b: *std.Build,
23    target: std.Build.ResolvedTarget,
24    cdb_step: *std.Build.Step,
25    nvim_exe_install: *std.Build.Step.InstallArtifact,
26    nvim_sources: std.ArrayList(gen.SourceItem),
27    nvim_headers: std.ArrayList([]u8),
28    ci_build: bool,
29) !void {
30    const lintc = b.step("lintc", "Lint C source");
31    var lint_sources = try lintSources(b, nvim_sources, nvim_headers);
32    defer lint_sources.deinit(b.allocator);
33    const lintc_uncrustify = addLintcUncrustifyStep(b, lint_sources);
34    lintc.dependOn(lintc_uncrustify);
35    const lintc_clint = addLintcClintStep(b, nvim_exe_install, lint_sources, ci_build);
36    lintc.dependOn(lintc_clint);
37    const lintc_clang_tidy = try addLintcClangtidyStep(b, cdb_step, nvim_exe_install, target, lint_sources);
38    lintc.dependOn(lintc_clang_tidy);
39    try addFormatcStep(b, lint_sources);
40}
41
42fn addLintcUncrustifyStep(
43    b: *std.Build,
44    lint_sources: std.ArrayList([]const u8),
45) *std.Build.Step {
46    const lintc_uncrustify = b.step("lintc-uncrustify", "Check formatting of C source with uncrustify");
47    const uncrustify = b.dependency("uncrustify", .{
48        .target = b.graph.host,
49        .optimize = .ReleaseFast,
50    });
51    const uncrustify_exe = uncrustify.artifact("uncrustify");
52    for (lint_sources.items) |file| {
53        const run = b.addRunArtifact(uncrustify_exe);
54        run.addArgs(&.{ "-c", "src/uncrustify.cfg", "-q", "--check" });
55        run.addFileArg(b.path(file));
56        run.expectExitCode(0);
57        lintc_uncrustify.dependOn(&run.step);
58    }
59    return lintc_uncrustify;
60}
61
62fn addLintcClangtidyStep(
63    b: *std.Build,
64    cdb_step: *std.Build.Step,
65    nvim_exe_install: *std.Build.Step.InstallArtifact,
66    target: std.Build.ResolvedTarget,
67    lint_sources: std.ArrayList([]const u8),
68) !*std.Build.Step {
69    const lintc_clang_tidy = b.step("lintc-clang-tidy", "Lint C source with clang-tidy");
70    var exclusions: std.ArrayList([]const u8) = try .initCapacity(b.allocator, 0);
71    defer exclusions.deinit(b.allocator);
72    try exclusions.appendSlice(b.allocator, &.{
73        "src/nvim/eval/typval_encode.c.h",
74        "src/nvim/api/ui_events.in.h",
75        "src/xxd/xxd.c",
76    });
77    if (target.result.os.tag == .windows) {
78        try exclusions.appendSlice(b.allocator, &.{
79            "src/nvim/os/pty_proc_unix.h",
80            "src/nvim/os/unix_defs.h",
81        });
82    } else {
83        try exclusions.appendSlice(b.allocator, &.{
84            "src/nvim/os/win_defs.h",
85            "src/nvim/os/pty_proc_win.h",
86            "src/nvim/os/pty_conpty_win.h",
87            "src/nvim/os/os_win_console.h",
88        });
89    }
90
91    outer: for (lint_sources.items) |file| {
92        for (exclusions.items) |exclusion| {
93            if (std.mem.eql(u8, exclusion, file)) continue :outer;
94        }
95        const run = b.addSystemCommand(&.{"clang-tidy"});
96        run.step.dependOn(cdb_step);
97        run.step.dependOn(&nvim_exe_install.step);
98        run.addFileArg(b.path(file));
99        run.addArg("--quiet");
100        run.expectStdOutEqual("");
101        lintc_clang_tidy.dependOn(&run.step);
102    }
103    return lintc_clang_tidy;
104}
105
106fn addLintcClintStep(
107    b: *std.Build,
108    // use InstallArtifact instead of nvim_exe so that cache is not busted
109    // for every file to be linted
110    nvim_exe_install: *std.Build.Step.InstallArtifact,
111    lint_sources: std.ArrayList([]const u8),
112    ci_build: bool,
113) *std.Build.Step {
114    const lintc_clint = b.step("lintc-clint", "Lint C source with clint");
115    const exclusions = [_][]const u8{ "src/nvim/tui/terminfo_defs.h", "src/xxd/xxd.c" };
116    const nvim_path = b.getInstallPath(.bin, nvim_exe_install.artifact.out_filename);
117
118    outer: for (lint_sources.items) |file| {
119        for (exclusions) |exclusion| {
120            if (std.mem.endsWith(u8, file, exclusion)) continue :outer;
121        }
122        const run = b.addSystemCommand(&.{nvim_path});
123        run.step.dependOn(&nvim_exe_install.step);
124        run.addArgs(&.{ "-u", "NONE", "-l" });
125        run.addFileArg(b.path("src/clint.lua"));
126        if (ci_build) {
127            run.addArg("--output=gh_action");
128        } else {
129            run.addArg("--output=vs7");
130        }
131        run.addFileArg(b.path(file));
132        run.expectStdOutEqual("");
133        lintc_clint.dependOn(&run.step);
134    }
135    return lintc_clint;
136}
137
138fn addFormatcStep(
139    b: *std.Build,
140    lint_sources: std.ArrayList([]const u8),
141) !void {
142    const formatc = b.step("formatc", "Format C source with uncrustify");
143    const uncrustify = b.dependency("uncrustify", .{
144        .target = b.graph.host,
145        .optimize = .ReleaseFast,
146    });
147    const uncrustify_exe = uncrustify.artifact("uncrustify");
148
149    const update = b.addUpdateSourceFiles();
150    for (lint_sources.items) |file| {
151        const run = b.addRunArtifact(uncrustify_exe);
152        run.addArgs(&.{ "-c", "src/uncrustify.cfg", "-f" });
153        run.addFileArg(b.path(file));
154        run.addArg("-o");
155        const output_name = try std.mem.replaceOwned(u8, b.allocator, file, "/", "-");
156        const output = run.addOutputFileArg(output_name);
157        update.addCopyFileToSource(output, file);
158    }
159    formatc.dependOn(&update.step);
160}