const std = @import("std"); const zlinter = @import("zlinter"); pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); // Dependencies are pinned to exact commits in build.zig.zon. // M1-PKG-004 zarko, M1-PKG-005 zig-regex, M1-PKG-003 zig-build-libyaml. const zarko_dep = b.dependency("zarko", .{ .target = target, .optimize = optimize }); const regex_dep = b.dependency("regex", .{ .target = target, .optimize = optimize }); const libyaml_dep = b.dependency("libyaml", .{ .target = target, .optimize = optimize }); const mod = b.addModule("csv2journal", .{ .root_source_file = b.path("src/root.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "zarko", .module = zarko_dep.module("zarko") }, .{ .name = "regex", .module = regex_dep.module("regex") }, .{ .name = "yaml", .module = libyaml_dep.module("yaml") }, }, }); // The shipped examples are part of the documented contract, so the schema // tests validate the real files rather than a copy that can drift. mod.addAnonymousImport("example_project_yaml", .{ .root_source_file = b.path("docs/yaml/project.yaml"), }); mod.addAnonymousImport("example_headerless_yaml", .{ .root_source_file = b.path("docs/yaml/project-headerless.yaml"), }); mod.addAnonymousImport("example_rules_yaml", .{ .root_source_file = b.path("docs/yaml/rules.yaml"), }); const exe = b.addExecutable(.{ .name = "csv2journal", .root_module = b.createModule(.{ .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "csv2journal", .module = mod }}, }), }); b.installArtifact(exe); const run_cmd = b.addRunArtifact(exe); run_cmd.step.dependOn(b.getInstallStep()); run_cmd.addPassthruArgs(); const run_step = b.step("run", "Run the app"); run_step.dependOn(&run_cmd.step); // M1-JRN-008: writes the compatibility corpus for hledger to parse. Wired // only into its own step, so it is never installed beside the application // -- M1-PKG-007 keeps hledger out of the runtime, and this with it. const corpus = b.addExecutable(.{ .name = "compat-corpus", .root_module = b.createModule(.{ .root_source_file = b.path("src/compat/dump.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "csv2journal", .module = mod }}, }), }); const corpus_run = b.addRunArtifact(corpus); corpus_run.addPassthruArgs(); const corpus_step = b.step("compat-corpus", "Write the hledger compatibility corpus to a directory"); corpus_step.dependOn(&corpus_run.step); // Times the steps of one evaluation over a generated project. Like the // corpus, it has its own step and is never installed. const bench = b.addExecutable(.{ .name = "bench", .root_module = b.createModule(.{ .root_source_file = b.path("src/bench/main.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "csv2journal", .module = mod }}, }), }); const bench_step = b.step("bench", "Time the steps of evaluating a generated project"); bench_step.dependOn(&b.addRunArtifact(bench).step); // The custom lint rule is tested like the rest of the code. Its module is // shaped the way zlinter shapes a rule -- the rule source with `zlinter` // imported -- so the tests run against what the linter actually loads. const lint_rule_tests = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("build/lint_rules/no_comptime_interface_tests.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "zlinter", .module = b.dependency("zlinter", .{}).module("zlinter"), }}, }), }); const mod_tests = b.addTest(.{ .root_module = mod }); const exe_tests = b.addTest(.{ .root_module = exe.root_module }); const bench_tests = b.addTest(.{ .root_module = bench.root_module }); const test_step = b.step("test", "Run tests"); test_step.dependOn(&b.addRunArtifact(mod_tests).step); test_step.dependOn(&b.addRunArtifact(exe_tests).step); test_step.dependOn(&b.addRunArtifact(bench_tests).step); // Compiled but not run: the test binary never analyses its main(). test_step.dependOn(&bench.step); test_step.dependOn(&b.addRunArtifact(lint_rule_tests).step); const lint_cmd = b.step("lint", "Lint source code with zlinter."); lint_cmd.dependOn(makeLintStep(b)); // The interface's JavaScript and CSS, which zlinter does not read. Both // tools take their configuration from src/ui, beside the code it governs, // and come from Nix rather than from a package manager (D1). const lint_ui = b.step("lint-ui", "Lint the interface's JavaScript and CSS."); lint_ui.dependOn(&uiCommand(b, &.{ "deno", "lint" }).step); lint_ui.dependOn(&uiCommand(b, &.{ "biome", "lint", "--error-on-warnings", "." }).step); // The interface's JavaScript tests, the `*_test.js` beside each module. const test_ui = b.step("test-ui", "Run the interface's JavaScript tests."); test_ui.dependOn(&uiCommand(b, &.{ "deno", "test" }).step); } /// A tool run in src/ui, where the interface's code and its configuration /// live. fn uiCommand(b: *std.Build, argv: []const []const u8) *std.Build.Step.Run { const run = b.addSystemCommand(argv); run.setCwd(b.path("src/ui/web")); return run; } fn makeLintStep(b: *std.Build) *std.Build.Step { var builder = zlinter.builder(b, .{}); builder.addPaths(.{ .include_dirs = &.{ b.path("src/"), b.path("build/") }, .include_files = &.{ b.path("build.zig"), b.path("build.zig.zon") }, }); // We use all builtin rules except for those: // * .require_doc_comment // * .import_ordering // * .require_errdefer_dealloc builder.addRule(.{ .builtin = .field_naming }, .{ .error_field_min_len = .{ .@"error" = .{ .len = 3 } }, .enum_field_min_len = .{ .@"error" = .{ .len = 3 } }, .enum_field_exclude_len = &.{ "ne", "ge", "eq", "lf", "gt", "eq", "lt", "le", "op" }, .struct_field_min_len = .{ .@"error" = .{ .len = 3 } }, .union_field_min_len = .{ .@"error" = .{ .len = 3 } }, .union_field_exclude_len = &.{ "eq", "ge", "gt", "le", "lt", "ne" }, }); builder.addRule(.{ .builtin = .field_ordering }, .{ .union_field_order = .{ .@"error" = .alphabetical_ascending }, .struct_field_order = .{ .@"error" = .alphabetical_ascending }, .enum_field_order = .{ .@"error" = .alphabetical_ascending }, }); builder.addRule(.{ .builtin = .declaration_naming }, .{ .decl_name_min_len = .{ .@"error" = .{ .len = 3 } }, // zlinter's default short-name allowlist, plus `ui` for the // `pub const ui` namespace in root.zig (the parallel of `io`). .decl_name_exclude_len = &.{ "x", "y", "z", "i", "b", "it", "ip", "c", "io", "ui" }, }); builder.addRule(.{ .builtin = .function_naming }, .{}); builder.addRule(.{ .builtin = .file_naming }, .{}); builder.addRule(.{ .builtin = .no_unused }, .{ .container_declaration = .@"error" }); builder.addRule(.{ .builtin = .no_deprecated }, .{ .severity = .@"error" }); builder.addRule(.{ .builtin = .no_empty_block }, .{}); builder.addRule(.{ .builtin = .no_inferred_error_unions }, .{ .severity = .@"error" }); builder.addRule(.{ .builtin = .no_orelse_unreachable }, .{}); builder.addRule(.{ .builtin = .require_labeled_continue }, .{}); builder.addRule(.{ .builtin = .no_unsafe_undefined }, .{ .return_value = .@"error", .break_value = .@"error", .const_value = .@"error", .optional_value = .@"error", .pointer_value = .@"error", .enum_or_union_value = .@"error", .primitive_scalar_value = .@"error", }); builder.addRule(.{ .builtin = .no_literal_only_bool_expression }, .{}); builder.addRule(.{ .builtin = .no_hidden_allocations }, .{}); builder.addRule(.{ .builtin = .switch_case_ordering }, .{}); builder.addRule(.{ .builtin = .max_positional_args }, .{ .severity = .@"error" }); builder.addRule(.{ .builtin = .no_comment_out_code }, .{ .severity = .@"error" }); builder.addRule(.{ .builtin = .no_todo }, .{}); builder.addRule(.{ .builtin = .no_literal_args }, .{}); builder.addRule(.{ .builtin = .no_swallow_error }, .{ .detect_catch_unreachable = .@"error", .detect_empty_catch = .@"error", .detect_empty_else = .@"error", .detect_else_unreachable = .@"error", }); builder.addRule(.{ .builtin = .no_panic }, .{}); builder.addRule(.{ .builtin = .require_exhaustive_enum_switch }, .{ .severity = .@"error" }); builder.addRule(.{ .builtin = .require_braces }, .{}); builder.addRule(.{ .builtin = .require_fmt }, .{}); builder.addRule(.{ .builtin = .no_global_vars }, .{}); builder.addRule(.{ .builtin = .no_redundant_comptime }, .{ .severity = .@"error" }); builder.addRule(.{ .builtin = .build_zig_zon }, .{ .require_license_path = .@"error" }); builder.addRule(.{ .custom = .{ .name = "no_comptime_interface_tests", .path = "build/lint_rules/no_comptime_interface_tests.zig", } }, .{ .has_decl = .@"error", .has_field = .@"error" }); return builder.build(); }