Skip to content

Commit e3312e6

Browse files
committed
rework clap entry point exporting
1 parent b70beff commit e3312e6

File tree

12 files changed

+194
-124
lines changed

12 files changed

+194
-124
lines changed

build.zig

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,39 +40,22 @@ pub fn clapModule(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std
4040
pub const Options = struct {
4141
/// Doesn't have to match the display name in your plugin's descriptor
4242
name: []const u8,
43-
/// Module that contains `plugin()`
43+
/// This module must export a CLAP entry point
4444
root_module: *std.Build.Module,
4545
/// Whether to add the resulting compile step to your project's top level install step
4646
install: bool = true,
4747
};
4848

4949
/// If `options.install` is true, the result will be installed to `lib/clap/{options.name}.clap`
5050
pub fn addClap(b: *std.Build, options: Options) !*std.Build.Step.Compile {
51-
const entry = b.addWriteFile("entry.zig",
52-
\\ export const clap_entry = @import("clap").clapEntry(@import("plugin_root"));
53-
);
54-
55-
const target = options.root_module.resolved_target.?;
56-
const optimize = options.root_module.optimize.?;
57-
5851
const lib = b.addLibrary(.{
5952
.name = try std.fmt.allocPrint(b.allocator, "{s}_clap", .{options.name}),
6053
.linkage = .dynamic,
6154
// printing to stdout/stderr segfaults without this, possibly a bug in zig's new x86 backend
6255
.use_llvm = true,
63-
.root_module = b.createModule(.{
64-
.target = target,
65-
.optimize = optimize,
66-
.root_source_file = entry.getDirectory().path(b, "entry.zig"),
67-
.imports = &.{
68-
.{ .name = "plugin_root", .module = options.root_module },
69-
.{ .name = "clap", .module = clapModule(b, target, optimize) },
70-
},
71-
}),
56+
.root_module = options.root_module,
7257
});
7358

74-
lib.step.dependOn(&entry.step);
75-
7659
if (options.install) {
7760
const install = b.addInstallArtifact(lib, .{
7861
.dest_sub_path = b.fmt("clap/{s}.clap", .{options.name}),

examples/clap-ext/build.zig

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,23 @@ pub fn build(b: *std.Build) !void {
1313
const plugin = b.createModule(.{
1414
.target = target,
1515
.optimize = optimize,
16-
.root_source_file = b.path("src/Plugin.zig"),
16+
.root_source_file = b.path("src/ClapExtExample.zig"),
1717
.imports = &.{
1818
.{ .name = "zigplug", .module = zigplug_dep.module("zigplug") },
19-
.{ .name = "zigplug_clap", .module = zigplug.clapModule(b, target, optimize) },
2019
},
2120
});
2221

2322
_ = try zigplug.addClap(b, .{
2423
.name = "clap-ext",
25-
.root_module = plugin,
24+
.root_module = b.createModule(.{
25+
.target = target,
26+
.optimize = optimize,
27+
.root_source_file = b.path("src/entry_clap.zig"),
28+
.imports = &.{
29+
.{ .name = "ClapExtExample", .module = plugin },
30+
31+
.{ .name = "zigplug_clap", .module = zigplug.clapModule(b, target, optimize) },
32+
},
33+
}),
2634
});
2735
}
Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ const ClapExtExample = @This();
33
const std = @import("std");
44

55
const zigplug = @import("zigplug");
6-
const clap = @import("zigplug_clap");
76

87
pub const meta: zigplug.Meta = .{
98
.name = "zigplug clap extension example",
@@ -26,52 +25,6 @@ pub const meta: zigplug.Meta = .{
2625
},
2726
};
2827

29-
fn getExtension(id: [:0]const u8) ?*const anyopaque {
30-
// let's implement the "note-name" extension, which is not supported in zigplug (yet?).
31-
// we can test this in reaper for example
32-
//
33-
// https://github.com/free-audio/clap/blob/main/include/clap/ext/note-name.h
34-
if (std.mem.eql(u8, id, &clap.c.CLAP_EXT_NOTE_NAME)) {
35-
const ext = struct {
36-
pub fn count(_: [*c]const clap.c.clap_plugin_t) callconv(.c) u32 {
37-
return 1;
38-
}
39-
40-
pub fn get(clap_plugin: [*c]const clap.c.clap_plugin_t, index: u32, note_name: [*c]clap.c.clap_note_name_t) callconv(.c) bool {
41-
// you can use this to access your plugin's state
42-
const self = clap.pluginFromClap(clap_plugin, ClapExtExample);
43-
_ = self;
44-
45-
if (index == 0) {
46-
// let's set the name of every note to some string
47-
note_name.* = .{
48-
// zig initializes c structs with `std.mem.zeroes` so we don't have to deal with an undefined
49-
// name value
50-
.port = -1,
51-
.key = -1,
52-
.channel = -1,
53-
};
54-
std.mem.copyForwards(u8, &note_name.*.name, "Hello world... i mean note");
55-
return true;
56-
}
57-
return false;
58-
}
59-
};
60-
return &clap.c.clap_plugin_note_name_t{
61-
.count = ext.count,
62-
.get = ext.get,
63-
};
64-
}
65-
return null;
66-
}
67-
68-
pub const clap_meta: clap.Meta = .{
69-
.id = "com.bandithedoge.zigplug_clap_ext_example",
70-
.features = &.{ .instrument, .synthesizer, .mono },
71-
.extra_features = &.{"custom-namespace:custom-feature"},
72-
.getExtension = getExtension,
73-
};
74-
7528
gpa: std.heap.GeneralPurposeAllocator(.{}) = .init,
7629

7730
phase: f32 = 0,
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const std = @import("std");
2+
const clap = @import("zigplug_clap");
3+
4+
const ClapExtExample = @import("ClapExtExample");
5+
6+
// let's implement the "note-name" extension, which is not supported in zigplug (yet?).
7+
// we can test this in reaper for example
8+
//
9+
// https://github.com/free-audio/clap/blob/main/include/clap/ext/note-name.h
10+
fn getExtension(id: [:0]const u8) ?*const anyopaque {
11+
if (std.mem.eql(u8, id, &clap.c.CLAP_EXT_NOTE_NAME)) {
12+
const ext = struct {
13+
pub fn count(_: [*c]const clap.c.clap_plugin_t) callconv(.c) u32 {
14+
return 1;
15+
}
16+
17+
pub fn get(clap_plugin: [*c]const clap.c.clap_plugin_t, index: u32, note_name: [*c]clap.c.clap_note_name_t) callconv(.c) bool {
18+
// you can use this to access your plugin's state
19+
const self = clap.pluginFromClap(clap_plugin, ClapExtExample);
20+
_ = self;
21+
22+
if (index == 0) {
23+
// let's set the name of every note to some string
24+
note_name.* = .{
25+
// zig initializes c structs with `std.mem.zeroes` so we don't have to deal with an undefined
26+
// name value
27+
.port = -1,
28+
.key = -1,
29+
.channel = -1,
30+
};
31+
std.mem.copyForwards(u8, &note_name.*.name, "Hello world... i mean note");
32+
return true;
33+
}
34+
return false;
35+
}
36+
};
37+
return &clap.c.clap_plugin_note_name_t{
38+
.count = ext.count,
39+
.get = ext.get,
40+
};
41+
}
42+
return null;
43+
}
44+
45+
comptime {
46+
clap.exportClap(ClapExtExample, .{
47+
.id = "com.bandithedoge.zigplug_clap_ext_example",
48+
.features = &.{ .instrument, .synthesizer, .mono },
49+
.extra_features = &.{"custom-namespace:custom-feature"},
50+
.getExtension = getExtension,
51+
});
52+
}

examples/gain/build.zig

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,23 @@ pub fn build(b: *std.Build) !void {
1313
const plugin = b.createModule(.{
1414
.target = target,
1515
.optimize = optimize,
16-
.root_source_file = b.path("src/Plugin.zig"),
16+
.root_source_file = b.path("src/GainExample.zig"),
1717
.imports = &.{
1818
.{ .name = "zigplug", .module = zigplug_dep.module("zigplug") },
19-
.{ .name = "zigplug_clap", .module = zigplug.clapModule(b, target, optimize) },
2019
},
2120
});
2221

2322
_ = try zigplug.addClap(b, .{
2423
.name = "gain",
25-
.root_module = plugin,
24+
.root_module = b.createModule(.{
25+
.target = target,
26+
.optimize = optimize,
27+
.root_source_file = b.path("src/entry_clap.zig"),
28+
.imports = &.{
29+
.{ .name = "GainExample", .module = plugin },
30+
31+
.{ .name = "zigplug_clap", .module = zigplug.clapModule(b, target, optimize) },
32+
},
33+
}),
2634
});
2735
}
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,6 @@ pub const meta: zigplug.Meta = .{
5454
.sample_accurate_automation = true,
5555
};
5656

57-
pub const clap_meta: @import("zigplug_clap").Meta = .{
58-
.id = "com.bandithedoge.zigplug_gain_example",
59-
.features = &.{ .audio_effect, .mono, .stereo, .utility },
60-
};
61-
6257
gpa: std.heap.GeneralPurposeAllocator(.{}) = .init,
6358

6459
pub fn init() !GainExample {

examples/gain/src/entry_clap.zig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const GainExample = @import("GainExample");
2+
3+
comptime {
4+
@import("zigplug_clap").exportClap(GainExample, .{
5+
.id = "com.bandithedoge.zigplug_gain_example",
6+
.features = &.{ .audio_effect, .mono, .stereo, .utility },
7+
});
8+
}

examples/sine/build.zig

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,23 @@ pub fn build(b: *std.Build) !void {
1313
const plugin = b.createModule(.{
1414
.target = target,
1515
.optimize = optimize,
16-
.root_source_file = b.path("src/Plugin.zig"),
16+
.root_source_file = b.path("src/SineExample.zig"),
1717
.imports = &.{
1818
.{ .name = "zigplug", .module = zigplug_dep.module("zigplug") },
19-
.{ .name = "zigplug_clap", .module = zigplug.clapModule(b, target, optimize) },
2019
},
2120
});
2221

2322
_ = try zigplug.addClap(b, .{
2423
.name = "sine",
25-
.root_module = plugin,
24+
.root_module = b.createModule(.{
25+
.target = target,
26+
.optimize = optimize,
27+
.root_source_file = b.path("src/entry_clap.zig"),
28+
.imports = &.{
29+
.{ .name = "SineExample", .module = plugin },
30+
31+
.{ .name = "zigplug_clap", .module = zigplug.clapModule(b, target, optimize) },
32+
},
33+
}),
2634
});
2735
}
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ pub const meta: zigplug.Meta = .{
2525
},
2626
};
2727

28-
pub const clap_meta: @import("zigplug_clap").Meta = .{
29-
.id = "com.bandithedoge.zigplug_sine_example",
30-
.features = &.{ .instrument, .synthesizer, .mono },
31-
};
32-
3328
gpa: std.heap.GeneralPurposeAllocator(.{}) = .init,
3429

3530
phase: f32 = 0,

examples/sine/src/entry_clap.zig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const SineExample = @import("SineExample");
2+
3+
comptime {
4+
@import("zigplug_clap").exportClap(SineExample, .{
5+
.id = "com.bandithedoge.zigplug_gain_example",
6+
.features = &.{ .audio_effect, .mono, .stereo, .utility },
7+
});
8+
}

0 commit comments

Comments
 (0)