@@ -50,6 +50,11 @@ const BuiltRule = struct {
5050 }
5151};
5252
53+ const BuildOptions = struct {
54+ target : std.Build.ResolvedTarget ,
55+ optimize : std.builtin.OptimizeMode ,
56+ };
57+
5358pub const BuilderOptions = struct {
5459 /// You should never need to set this. Defaults to native host.
5560 target : ? std.Build.ResolvedTarget = null ,
@@ -69,27 +74,29 @@ pub const BuilderOptions = struct {
6974 optimize : std.builtin.OptimizeMode = .Debug ,
7075};
7176
72- /// Creater a step builder for zlinter
77+ /// Create a step builder for zlinter
7378pub fn builder (b : * std.Build , options : BuilderOptions ) StepBuilder {
7479 return .{
7580 .rules = .empty ,
76- .include_paths = .empty ,
77- .exclude_paths = .empty ,
78- .sources = .empty ,
81+ .exclude = .empty ,
82+ .include = .empty ,
83+ .options = .{
84+ .optimize = options .optimize ,
85+ .target = options .target orelse b .graph .host ,
86+ },
7987 .b = b ,
80- .optimize = options .optimize ,
81- .target = options .target orelse b .graph .host ,
8288 };
8389}
8490
85- /// Represents a source that can be linted.
86- pub const LintSource = union (enum ) {
91+ /// Represents something that should be linted.
92+ const LintIncludeSource = union (enum ) {
8793 /// e.g., library or executable.
8894 compiled_unit : struct {
8995 compile_step : * std.Build.Step.Compile ,
9096 },
97+ path : std.Build.LazyPath ,
9198
92- pub fn compiled (compile : * std.Build.Step.Compile ) LintSource {
99+ pub fn compiled (compile : * std.Build.Step.Compile ) LintIncludeSource {
93100 return .{
94101 .compiled_unit = .{
95102 .compile_step = compile ,
@@ -98,15 +105,17 @@ pub const LintSource = union(enum) {
98105 }
99106};
100107
108+ /// Represents something that should be excluded from linting.
109+ const LintExcludeSource = union (enum ) {
110+ path : std.Build.LazyPath ,
111+ };
112+
101113const StepBuilder = struct {
102114 rules : shims .ArrayList (BuiltRule ),
103- // TODO: Collapse paths and sources into one array for union `LintSource`.
104- include_paths : shims .ArrayList (std .Build .LazyPath ),
105- exclude_paths : shims .ArrayList (std .Build .LazyPath ),
106- sources : shims .ArrayList (LintSource ),
115+ include : shims .ArrayList (LintIncludeSource ),
116+ exclude : shims .ArrayList (LintExcludeSource ),
117+ options : BuildOptions ,
107118 b : * std.Build ,
108- target : std.Build.ResolvedTarget ,
109- optimize : std.builtin.OptimizeMode ,
110119
111120 pub fn addRule (
112121 self : * StepBuilder ,
@@ -121,23 +130,22 @@ const StepBuilder = struct {
121130 self .b ,
122131 source ,
123132 .{
124- .optimize = self .optimize ,
125- .target = self .target ,
133+ .optimize = self .options . optimize ,
134+ .target = self .options . target ,
126135 },
127136 config ,
128137 ),
129138 ) catch @panic ("OOM" );
130139 }
131140
132- /// Adds a source to be linted (e.g., library or executable ). Only inputs
133- /// resolved to this source within the projects path will be linted.
141+ /// Adds a source to be linted (e.g., library, executable or path ). Only
142+ /// inputs resolved to this source within the projects path will be linted.
134143 ///
135- /// If a source is not set then it falls back to linting the include paths.
136- /// If no include paths are given then it falls back to linting all source
137- /// files under the current working directory.
138- pub fn addSource (self : * StepBuilder , source : LintSource ) void {
144+ /// If no paths are given or resolved then it falls back to linting all
145+ /// zig source files under the current working directory.
146+ pub fn addSource (self : * StepBuilder , source : LintIncludeSource ) void {
139147 const arena = self .b .allocator ;
140- self .sources .append (arena , source ) catch @panic ("OOM" );
148+ self .include .append (arena , source ) catch @panic ("OOM" );
141149 }
142150
143151 /// Set the paths to include or exclude when running the linter.
@@ -160,9 +168,9 @@ const StepBuilder = struct {
160168 const arena = self .b .allocator ;
161169
162170 if (paths .include ) | includes |
163- for (includes ) | path | self .include_paths .append (arena , path ) catch @panic ("OOM" );
171+ for (includes ) | path | self .include .append (arena , .{ . path = path } ) catch @panic ("OOM" );
164172 if (paths .exclude ) | excludes |
165- for (excludes ) | path | self .exclude_paths .append (arena , path ) catch @panic ("OOM" );
173+ for (excludes ) | path | self .exclude .append (arena , .{ . path = path } ) catch @panic ("OOM" );
166174 }
167175
168176 pub fn build (self : * StepBuilder ) * std.Build.Step {
@@ -172,18 +180,14 @@ const StepBuilder = struct {
172180 b ,
173181 self .rules .items ,
174182 .{
175- .target = self .target ,
176- .optimize = self .optimize ,
177- .zlinter = .{
178- .dependency = b .dependencyFromBuildZig (
179- @"build.zig" ,
180- .{},
181- ),
182- },
183- .include_paths = self .include_paths .items ,
184- .exclude_paths = self .exclude_paths .items ,
185- .sources = self .sources .items ,
183+ .dependency = b .dependencyFromBuildZig (
184+ @"build.zig" ,
185+ .{},
186+ ),
186187 },
188+ self .include .items ,
189+ self .exclude .items ,
190+ self .options ,
187191 );
188192 }
189193};
@@ -364,19 +368,18 @@ pub fn build(b: *std.Build) void {
364368
365369 const lint_cmd = b .step ("lint" , "Lint the linters own source code." );
366370 lint_cmd .dependOn (step : {
367- var sources = shims .ArrayList (LintSource ).empty ;
368- sources .append (b .allocator , .compiled (b .addLibrary (.{
371+ var include = shims .ArrayList (LintIncludeSource ).empty ;
372+ include .append (b .allocator , .compiled (b .addLibrary (.{
369373 .name = "zlinter" ,
370374 .root_module = zlinter_lib_module ,
371375 }))) catch @panic ("OOM" );
372376
373- var include_paths = shims .ArrayList (std .Build .LazyPath ).empty ;
374- var exclude_paths = shims .ArrayList (std .Build .LazyPath ).empty ;
377+ var exclude = shims .ArrayList (LintExcludeSource ).empty ;
375378
376379 // Also lint all files within project, not just those resolved to our compiled source.
377- include_paths .append (b .allocator , b .path ("./" )) catch @panic ("OOM" );
378- exclude_paths .append (b .allocator , b .path ("integration_tests/test_cases" )) catch @panic ("OOM" );
379- exclude_paths .append (b .allocator , b .path ("integration_tests/src/test_case_references.zig" )) catch @panic ("OOM" );
380+ include .append (b .allocator , .{ . path = b .path ("./" ) } ) catch @panic ("OOM" );
381+ exclude .append (b .allocator , .{ . path = b .path ("integration_tests/test_cases" ) } ) catch @panic ("OOM" );
382+ exclude .append (b .allocator , .{ . path = b .path ("integration_tests/src/test_case_references.zig" ) } ) catch @panic ("OOM" );
380383
381384 break :step buildStep (
382385 b ,
@@ -430,13 +433,12 @@ pub fn build(b: *std.Build) void {
430433 },
431434 ),
432435 },
436+ .{ .module = zlinter_lib_module },
437+ include .items ,
438+ exclude .items ,
433439 .{
434- .sources = sources .items ,
435440 .target = target ,
436441 .optimize = optimize ,
437- .include_paths = include_paths .items ,
438- .exclude_paths = exclude_paths .items ,
439- .zlinter = .{ .module = zlinter_lib_module },
440442 },
441443 );
442444 });
@@ -492,19 +494,15 @@ fn toZonString(val: anytype, allocator: std.mem.Allocator) []const u8 {
492494fn buildStep (
493495 b : * std.Build ,
494496 rules : []const BuiltRule ,
495- options : struct {
496- target : std.Build.ResolvedTarget ,
497- optimize : std.builtin.OptimizeMode ,
498- zlinter : union (enum ) {
499- dependency : * std.Build.Dependency ,
500- module : * std.Build.Module ,
501- },
502- include_paths : []const std.Build.LazyPath ,
503- exclude_paths : []const std.Build.LazyPath ,
504- sources : []const LintSource ,
497+ zlinter : union (enum ) {
498+ dependency : * std.Build.Dependency ,
499+ module : * std.Build.Module ,
505500 },
501+ include : []const LintIncludeSource ,
502+ exclude : []const LintExcludeSource ,
503+ options : BuildOptions ,
506504) * std.Build.Step {
507- const zlinter_lib_module : * std.Build.Module , const exe_file : std.Build.LazyPath , const build_rules_exe_file : std.Build.LazyPath = switch (options . zlinter ) {
505+ const zlinter_lib_module : * std.Build.Module , const exe_file : std.Build.LazyPath , const build_rules_exe_file : std.Build.LazyPath = switch (zlinter ) {
508506 .dependency = > | d | .{ d .module ("zlinter" ), d .path ("src/exe/run_linter.zig" ), d .path ("build_rules.zig" ) },
509507 .module = > | m | .{ m , b .path ("src/exe/run_linter.zig" ), b .path ("build_rules.zig" ) },
510508 };
@@ -552,9 +550,8 @@ fn buildStep(
552550 const zlinter_run = ZlinterRun .create (
553551 b ,
554552 zlinter_exe ,
555- options .include_paths ,
556- options .exclude_paths ,
557- options .sources ,
553+ include ,
554+ exclude ,
558555 );
559556
560557 return & zlinter_run .step ;
@@ -755,14 +752,11 @@ const ZlinterRun = struct {
755752 /// CLI arguments to be passed to zlinter when executed
756753 argv : shims .ArrayList (Arg ),
757754
758- /// Include paths configured within the build file.
759- include_paths : []const std.Build.LazyPath ,
760-
761755 /// Exclude paths confiured within the build file.
762- exclude_paths : []const std.Build.LazyPath ,
756+ exclude : []const LintExcludeSource ,
763757
764758 /// The sources to lint (e.g., an executable or library).
765- sources : []const LintSource ,
759+ include : []const LintIncludeSource ,
766760
767761 const Arg = union (enum ) {
768762 artifact : * std.Build.Step.Compile ,
@@ -772,9 +766,8 @@ const ZlinterRun = struct {
772766 pub fn create (
773767 owner : * std.Build ,
774768 exe : * std.Build.Step.Compile ,
775- include_paths : []const std.Build.LazyPath ,
776- exclude_paths : []const std.Build.LazyPath ,
777- sources : []const LintSource ,
769+ include : []const LintIncludeSource ,
770+ exclude : []const LintExcludeSource ,
778771 ) * ZlinterRun {
779772 const arena = owner .allocator ;
780773
@@ -787,15 +780,10 @@ const ZlinterRun = struct {
787780 .makeFn = make ,
788781 }),
789782 .argv = .empty ,
790- .exclude_paths = exclude_paths ,
791- .include_paths = include_paths ,
792- .sources = sources ,
783+ .exclude = exclude ,
784+ .include = include ,
793785 };
794786
795- for (include_paths ) | path | {
796- addWatchInput (owner , & self .step , path , .lintable_file ) catch @panic ("OOM" );
797- }
798-
799787 self .argv .append (arena , .{ .artifact = exe }) catch @panic ("OOM" );
800788
801789 if (owner .args ) | args | self .addArgs (args );
@@ -811,11 +799,17 @@ const ZlinterRun = struct {
811799 const bin_file = exe .getEmittedBin ();
812800 bin_file .addStepDependencies (& self .step );
813801
814- for (sources ) | s | {
802+ for (include ) | s | {
815803 switch (s ) {
816- .compiled_unit = > | info | {
817- self .step .dependOn (& info .compile_step .step );
818- },
804+ .compiled_unit = > | info | self .step .dependOn (
805+ & info .compile_step .step ,
806+ ),
807+ .path = > | path | addWatchInput (
808+ owner ,
809+ & self .step ,
810+ path ,
811+ .lintable_file ,
812+ ) catch @panic ("OOM" ),
819813 }
820814 }
821815
@@ -861,13 +855,11 @@ const ZlinterRun = struct {
861855
862856 var includes : shims .ArrayList (std .Build .LazyPath ) = try .initCapacity (
863857 b .allocator ,
864- @max (1 , run .include_paths .len ),
858+ @max (1 , run .include .len ),
865859 );
866860 defer includes .deinit (b .allocator );
867861
868- includes .appendSliceAssumeCapacity (run .include_paths );
869-
870- for (run .sources ) | source | {
862+ for (run .include ) | source | {
871863 switch (source ) {
872864 .compiled_unit = > | info | {
873865 var exe = info .compile_step ;
@@ -906,15 +898,27 @@ const ZlinterRun = struct {
906898 }
907899 }
908900 },
901+ .path = > | path | try includes .append (
902+ b .allocator ,
903+ path ,
904+ ),
909905 }
910906 }
911907 if (includes .items .len == 0 ) {
912908 includes .appendAssumeCapacity (b .path ("./" ));
913909 }
914910
911+ var excludes : shims .ArrayList (std .Build .LazyPath ) = try .initCapacity (b .allocator , run .exclude .len );
912+ defer excludes .deinit (b .allocator );
913+ for (run .exclude ) | exclude | {
914+ switch (exclude ) {
915+ .path = > | path | excludes .appendAssumeCapacity (path ),
916+ }
917+ }
918+
915919 const build_info_zon_bytes : []const u8 = toZonString (BuildInfo {
916920 .include_paths = try subPaths (& run .step , includes .items ),
917- .exclude_paths = try subPaths (& run .step , run . exclude_paths ),
921+ .exclude_paths = try subPaths (& run .step , excludes . items ),
918922 }, b .allocator );
919923
920924 const env_map = arena .create (std .process .EnvMap ) catch @panic ("OOM" );
0 commit comments