1nbdkit-cc-plugin(3)                 NBDKIT                 nbdkit-cc-plugin(3)
2
3
4

NAME

6       nbdkit-cc-plugin - write small nbdkit plugins in inline C (and other
7       languages)
8

SYNOPSIS

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

DESCRIPTION

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 C++
78        nbdkit cc CC=g++ source.cpp
79
80       C++ plugin scripts can be created similarly to C, but you must add
81       "CC=g++" as a parameter to exec nbdkit.
82
83   Using this plugin with OCaml
84        nbdkit cc CC=ocamlopt \
85                  CFLAGS="-output-obj -runtime-variant _pic unix.cmxa NBDKit.cmx -cclib -lnbdkitocaml" \
86                  source.ml
87
88       OCaml plugin scripts can be created using this trick:
89
90        (*/.)>/dev/null 2>&1
91        exec nbdkit cc "$0" \
92             CC=ocamlopt \
93             CFLAGS="-output-obj -runtime-variant _pic unix.cmxa NBDKit.cmx -cclib -lnbdkitocaml" \
94             "$@"
95        *)
96        (* followed by OCaml code for the plugin here *)
97
98       As with C plugin scripts, the file must be executable.  See also
99       nbdkit-ocaml-plugin(3).
100
101   Using this plugin with other programming languages
102       This plugin can be used with most ahead-of-time compiled programming
103       languages if they can create shared objects (.so files).  The only
104       requirement is that the compiler ("CC") supports an -o option to write
105       a shared object.
106

PARAMETERS

108       The script name, or "-", must appear as the first parameter.
109
110       CC=CC
111       CFLAGS="CFLAGS"
112       EXTRA_CFLAGS="EXTRA_CFLAGS"
113           Override the compiler and flags.  See "Compiler name and flags"
114           above.
115
116       All other parameters on the command line are passed to the plugin.
117

EXAMPLE

119        $ nbdkit cc - <<'EOF'
120        #include <stdint.h>
121        #include <string.h>
122
123        #define NBDKIT_API_VERSION 2
124        #include <nbdkit-plugin.h>
125
126        char data[10*1024*1024];
127
128        #define THREAD_MODEL NBDKIT_THREAD_MODEL_PARALLEL
129
130        static void *
131        my_open (int readonly)
132        {
133          return NBDKIT_HANDLE_NOT_NEEDED;
134        }
135
136        static int64_t
137        my_get_size (void *handle)
138        {
139          return (int64_t) sizeof (data);
140        }
141
142        static int
143        my_pread (void *handle, void *buf,
144                  uint32_t count, uint64_t offset,
145                  uint32_t flags)
146        {
147          memcpy (buf, data+offset, count);
148          return 0;
149        }
150
151        static int
152        my_pwrite (void *handle, const void *buf,
153                   uint32_t count, uint64_t offset,
154                   uint32_t flags)
155        {
156          memcpy (data+offset, buf, count);
157          return 0;
158        }
159
160        static struct nbdkit_plugin plugin = {
161          .name              = "myplugin",
162          .open              = my_open,
163          .get_size          = my_get_size,
164          .pread             = my_pread,
165          .pwrite            = my_pwrite,
166        };
167
168        NBDKIT_REGISTER_PLUGIN(plugin)
169        EOF
170

FILES

172       $plugindir/nbdkit-cc-plugin.so
173           The plugin.
174
175           Use "nbdkit --dump-config" to find the location of $plugindir.
176

VERSION

178       "nbdkit-cc-plugin" first appeared in nbdkit 1.22.
179

SEE ALSO

181       nbdkit(1), nbdkit-plugin(3), nbdkit-eval-plugin(3).
182       nbdkit-ocaml-plugin(3).  nbdkit-sh-plugin(3).
183

AUTHORS

185       Richard W.M. Jones
186
188       Copyright Red Hat
189

LICENSE

191       Redistribution and use in source and binary forms, with or without
192       modification, are permitted provided that the following conditions are
193       met:
194
195       •   Redistributions of source code must retain the above copyright
196           notice, this list of conditions and the following disclaimer.
197
198       •   Redistributions in binary form must reproduce the above copyright
199           notice, this list of conditions and the following disclaimer in the
200           documentation and/or other materials provided with the
201           distribution.
202
203       •   Neither the name of Red Hat nor the names of its contributors may
204           be used to endorse or promote products derived from this software
205           without specific prior written permission.
206
207       THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND ANY
208       EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
209       IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
210       PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR CONTRIBUTORS BE
211       LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
212       CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
213       SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
214       BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
215       WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
216       OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
217       ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
218
219
220
221nbdkit-1.34.4                     2023-09-26               nbdkit-cc-plugin(3)
Impressum