@@ -4,127 +4,44 @@ pub fn build(b: *std.Build) !void {
44 const target = b .standardTargetOptions (.{});
55 const optimize = b .standardOptimizeOption (.{});
66
7- const options = .{
8- .clap = b .option (bool , "clap" , "Build CLAP target" ) orelse false ,
9- };
10-
11- const options_step = b .addOptions ();
12- inline for (std .meta .fields (@TypeOf (options ))) | field | {
13- options_step .addOption (field .type , field .name , @field (options , field .name ));
14- }
15- const options_module = options_step .createModule ();
16-
17- const zigplug = b .addModule ("zigplug" , .{
18- .root_source_file = b .path ("src/zigplug/zigplug.zig" ),
7+ const core_dep = b .dependency ("core" , .{
198 .target = target ,
209 .optimize = optimize ,
21- .imports = &.{
22- .{ .name = "zigplug_options" , .module = options_module },
23- },
2410 });
2511
12+ try b .modules .put ("zigplug" , core_dep .module ("zigplug" ));
13+
2614 const test_step = b .step ("test" , "Run unit tests" );
27- const tests = b .addTest (.{
28- .root_module = zigplug ,
29- });
30- const run_tests = b .addRunArtifact (tests );
31- test_step .dependOn (& run_tests .step );
3215
33- const docs_step = b .step ("docs" , "Build documentation" );
34- const check = b .step ("check" , "Check compile errors" );
16+ const core_tests = b .addSystemCommand (&.{ "zig" , "build" , "test" });
17+ core_tests .setCwd (b .path ("src/core" ));
18+ test_step .dependOn (& core_tests .step );
3519
36- const lib = b .addLibrary (.{
37- . name = "zigplug" ,
38- . root_module = zigplug ,
39- });
20+ const clap_tests = b .addSystemCommand (&.{ "zig" , "build" , "test" });
21+ clap_tests . setCwd ( b . path ( "src/clap" ));
22+ test_step . dependOn ( & clap_tests . step );
23+ }
4024
41- var docs_src = std .ArrayList (u8 ).empty ;
42- try docs_src .appendSlice (b .allocator ,
43- \\comptime {@import("std").testing.refAllDeclsRecursive(@This());}
44- \\
45- \\pub const zigplug = @import("zigplug");
46- \\
47- );
48- const docs_module = b .createModule (.{
25+ pub fn clapModule (b : * std.Build , target : std.Build.ResolvedTarget , optimize : std.builtin.OptimizeMode ) * std.Build.Module {
26+ const dep = b .dependencyFromBuildZig (@This (), .{
4927 .target = target ,
5028 .optimize = optimize ,
51- .imports = &.{.{ .name = "zigplug" , .module = zigplug }},
5229 });
5330
54- check .dependOn (& lib .step );
55-
56- if (options .clap ) {
57- const clap_module = b .addModule ("clap" , .{
58- .target = target ,
59- .optimize = optimize ,
60- .root_source_file = b .path ("src/clap/root.zig" ),
61- .imports = &.{
62- .{ .name = "zigplug" , .module = zigplug },
63- .{ .name = "zigplug_options" , .module = options_module },
64- },
65- });
66- clap_module .addImport ("clap" , clap_module );
67-
68- if (b .lazyDependency ("clap" , .{})) | clap_dep | {
69- const clap_c = b .addTranslateC (.{
70- .root_source_file = clap_dep .path ("include/clap/clap.h" ),
71- .target = target ,
72- .optimize = optimize ,
73- });
74- clap_module .addAnonymousImport ("clap_c" , .{
75- .root_source_file = clap_c .getOutput (),
76- });
77- }
78-
79- if (b .lazyDependency ("msgpack" , .{
80- .target = target ,
81- .optimize = optimize ,
82- })) | msgpack |
83- clap_module .addImport ("msgpack" , msgpack .module ("msgpack" ));
84-
85- const clap_tests = b .addTest (.{ .root_module = clap_module });
86- const run_clap_tests = b .addRunArtifact (clap_tests );
87- test_step .dependOn (& run_clap_tests .step );
88-
89- const clap_lib = b .addLibrary (.{
90- .name = "zigplug_clap" ,
91- .root_module = clap_module ,
92- });
93-
94- check .dependOn (& clap_lib .step );
95-
96- docs_module .addImport ("clap" , clap_module );
97- try docs_src .appendSlice (b .allocator ,
98- \\ pub const clap = @import("clap");
99- \\
100- );
101- }
102-
103- docs_module .root_source_file =
104- b .addWriteFile ("docs.zig" , try docs_src .toOwnedSlice (b .allocator ))
105- .getDirectory ()
106- .path (b , "docs.zig" );
107-
108- const docs = b .addLibrary (.{
109- .name = "zigplug-docs" ,
110- .root_module = docs_module ,
111- });
31+ if (dep .builder .lazyDependency ("clap" , .{
32+ .target = target ,
33+ .optimize = optimize ,
34+ })) | clap_dep |
35+ return clap_dep .module ("clap" );
11236
113- const install_docs = b .addInstallDirectory (.{
114- .source_dir = docs .getEmittedDocs (),
115- .install_dir = .prefix ,
116- .install_subdir = "docs" ,
117- });
118- docs_step .dependOn (& install_docs .step );
37+ unreachable ;
11938}
12039
12140pub const Options = struct {
12241 /// Doesn't have to match the display name in your plugin's descriptor
12342 name : []const u8 ,
12443 /// Module that contains `plugin()`
12544 root_module : * std.Build.Module ,
126- /// Must be the same dependency your plugin imports the "zigplug" module from
127- zigplug_dep : * std.Build.Dependency ,
12845 /// Whether to add the resulting compile step to your project's top level install step
12946 install : bool = true ,
13047};
@@ -135,18 +52,21 @@ pub fn addClap(b: *std.Build, options: Options) !*std.Build.Step.Compile {
13552 \\ export const clap_entry = @import("clap").clapEntry(@import("plugin_root"));
13653 );
13754
55+ const target = options .root_module .resolved_target .? ;
56+ const optimize = options .root_module .optimize .? ;
57+
13858 const lib = b .addLibrary (.{
13959 .name = try std .fmt .allocPrint (b .allocator , "{s}_clap" , .{options .name }),
14060 .linkage = .dynamic ,
14161 // printing to stdout/stderr segfaults without this, possibly a bug in zig's new x86 backend
14262 .use_llvm = true ,
14363 .root_module = b .createModule (.{
144- .target = options . root_module . resolved_target ,
145- .optimize = options . root_module . optimize ,
64+ .target = target ,
65+ .optimize = optimize ,
14666 .root_source_file = entry .getDirectory ().path (b , "entry.zig" ),
14767 .imports = &.{
14868 .{ .name = "plugin_root" , .module = options .root_module },
149- .{ .name = "clap" , .module = options . zigplug_dep . module ( "clap" ) },
69+ .{ .name = "clap" , .module = clapModule ( b , target , optimize ) },
15070 },
15171 }),
15272 });
0 commit comments