1nbdkit-cc-plugin(3) NBDKIT nbdkit-cc-plugin(3)
2
3
4
6 nbdkit-cc-plugin - write small nbdkit plugins in inline C (and other
7 languages)
8
10 nbdkit cc /path/to/plugin.c [CC=<CC>] [CFLAGS=<CFLAGS>]
11 [EXTRA_CFLAGS=<EXTRA_CFLAGS>]
12 nbdkit cc - <<'EOF'
13 ... C code ...
14 EOF
15
17 This plugin allows you to write small nbdkit(1) plugins in C (and some
18 other languages). When you use this plugin it compiles your source
19 code to a temporary plugin and then jumps into your compiled plugin.
20 It is somewhat similar to nbdkit-sh-plugin(3), except for C source
21 code. This can also be used to write plugins which are "C scripts".
22
23 Note this is not the way you normally write nbdkit plugins in C. To
24 understand how to write plugins in C normally, read nbdkit-plugin(3).
25
26 Simple plugin example
27 Simple plugins from the nbdkit source tree can be compiled and run
28 directly using commands such as:
29
30 $ nbdkit cc plugins/example1/example1.c EXTRA_CFLAGS="-I. -Iinclude"
31
32 You can also read the source from stdin using "-":
33
34 $ nbdkit cc - EXTRA_CFLAGS="-I. -Iinclude" \
35 < plugins/example1/example1.c
36
37 To replace the compiler flags:
38
39 $ nbdkit cc plugins/example1/example1.c \
40 CFLAGS="-O3 -mavx2 -fPIC -shared"
41
42 Compiler name and flags
43 The plugin parameters "CC", "CFLAGS" and "EXTRA_CFLAGS" (written in
44 uppercase) can be used to control which C compiler and C compiler flags
45 are used. If not set, the default compiler and flags from when nbdkit
46 was itself compiled from source are used. To see what those were you
47 can do:
48
49 $ nbdkit cc --dump-plugin
50 ...
51 CC=gcc
52 CFLAGS=-g -O2 -fPIC -shared
53
54 The "CFLAGS" parameter overrides the built-in flags completely. The
55 "EXTRA_CFLAGS" parameter adds extra flags to the built-in flags.
56
57 Plugin API version
58 Plugins compiled this way must use the same API version as the cc
59 plugin itself uses. Currently this is "NBDKIT_API_VERSION=2".
60
61 C plugin as a self-contained script
62 You can create a C plugin which is a self-contained script by adding
63 the following lines at the top and ensuring the C source is executable
64 ("chmod +x plugin.c"):
65
66 #if 0
67 exec nbdkit cc "$0" "$@"
68 #endif
69
70 The script can be run as a command with additional nbdkit flags and
71 plugin parameters, eg:
72
73 ./plugin.c -f -v
74 ./plugin.c -p 10000 --filter=cow
75 ./plugin.c param=1
76
77 Using this plugin with other programming languages
78 This plugin can be used with most ahead-of-time compiled programming
79 languages if they can create shared objects (.so files). The only
80 requirement is that the compiler ("CC") supports an -o option to write
81 a shared object. Some examples follow.
82
83 Using this plugin with C++
84
85 nbdkit cc CC=g++ source.cpp
86
87 Using this plugin with OCaml
88
89 nbdkit cc CC=ocamlopt \
90 CFLAGS="-output-obj -runtime-variant _pic NBDKit.cmx -cclib -lnbdkitocaml" \
91 source.ml
92
93 See nbdkit-ocaml-plugin(3).
94
96 The script name, or "-", must appear as the first parameter.
97
98 CC=CC
99 CFLAGS="CFLAGS"
100 EXTRA_CFLAGS="EXTRA_CFLAGS"
101 Override the compiler and flags. See "Compiler name and flags"
102 above.
103
104 All other parameters on the command line are passed to the plugin.
105
107 $ nbdkit cc - <<'EOF'
108 #include <stdint.h>
109 #include <string.h>
110
111 #define NBDKIT_API_VERSION 2
112 #include <nbdkit-plugin.h>
113
114 char data[10*1024*1024];
115
116 #define THREAD_MODEL NBDKIT_THREAD_MODEL_PARALLEL
117
118 static void *
119 my_open (int readonly)
120 {
121 return NBDKIT_HANDLE_NOT_NEEDED;
122 }
123
124 static int64_t
125 my_get_size (void *handle)
126 {
127 return (int64_t) sizeof (data);
128 }
129
130 static int
131 my_pread (void *handle, void *buf,
132 uint32_t count, uint64_t offset,
133 uint32_t flags)
134 {
135 memcpy (buf, data+offset, count);
136 return 0;
137 }
138
139 static int
140 my_pwrite (void *handle, const void *buf,
141 uint32_t count, uint64_t offset,
142 uint32_t flags)
143 {
144 memcpy (data+offset, buf, count);
145 return 0;
146 }
147
148 static struct nbdkit_plugin plugin = {
149 .name = "myplugin",
150 .open = my_open,
151 .get_size = my_get_size,
152 .pread = my_pread,
153 .pwrite = my_pwrite,
154 };
155
156 NBDKIT_REGISTER_PLUGIN(plugin)
157 EOF
158
160 $plugindir/nbdkit-cc-plugin.so
161 The plugin.
162
163 Use "nbdkit --dump-config" to find the location of $plugindir.
164
166 "nbdkit-cc-plugin" first appeared in nbdkit 1.22.
167
169 nbdkit(1), nbdkit-plugin(3), nbdkit-sh-plugin(3).
170
172 Richard W.M. Jones
173
175 Copyright (C) 2020 Red Hat Inc.
176
178 Redistribution and use in source and binary forms, with or without
179 modification, are permitted provided that the following conditions are
180 met:
181
182 • Redistributions of source code must retain the above copyright
183 notice, this list of conditions and the following disclaimer.
184
185 • Redistributions in binary form must reproduce the above copyright
186 notice, this list of conditions and the following disclaimer in the
187 documentation and/or other materials provided with the
188 distribution.
189
190 • Neither the name of Red Hat nor the names of its contributors may
191 be used to endorse or promote products derived from this software
192 without specific prior written permission.
193
194 THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND ANY
195 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
196 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
197 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR CONTRIBUTORS BE
198 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
199 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
200 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
201 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
202 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
203 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
204 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
205
206
207
208nbdkit-1.25.8 2021-05-25 nbdkit-cc-plugin(3)