.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Podwrapper::Man 1.38.1 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "nbdkit-cc-plugin 3" .TH nbdkit-cc-plugin 3 2024-04-21 nbdkit-1.38.1 NBDKIT .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME nbdkit\-cc\-plugin \- write small nbdkit plugins in inline C (and other languages) .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 2 \& nbdkit cc /path/to/plugin.c [CC=] [CFLAGS=] \& [EXTRA_CFLAGS=] .Ve .PP .Vb 3 \& nbdkit cc \- <<\*(AqEOF\*(Aq \& ... C code ... \& EOF .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" This plugin allows you to write small \fBnbdkit\fR\|(1) plugins in C (and some other languages). When you use this plugin it compiles your source code to a temporary plugin and then jumps into your compiled plugin. It is somewhat similar to \fBnbdkit\-sh\-plugin\fR\|(3), except for C source code. This can also be used to write plugins which are "C scripts". .PP \&\fBNote\fR this is not the way you normally write nbdkit plugins in C. To understand how to write plugins in C normally, read \&\fBnbdkit\-plugin\fR\|(3). .SS "Simple plugin example" .IX Subsection "Simple plugin example" Simple plugins from the nbdkit source tree can be compiled and run directly using commands such as: .PP .Vb 1 \& $ nbdkit cc plugins/example1/example1.c EXTRA_CFLAGS="\-I. \-Iinclude" .Ve .PP You can also read the source from stdin using \f(CW\*(C`\-\*(C'\fR: .PP .Vb 2 \& $ nbdkit cc \- EXTRA_CFLAGS="\-I. \-Iinclude" \e \& < plugins/example1/example1.c .Ve .PP To replace the compiler flags: .PP .Vb 2 \& $ nbdkit cc plugins/example1/example1.c \e \& CFLAGS="\-O3 \-mavx2 \-fPIC \-shared" .Ve .SS "Compiler name and flags" .IX Subsection "Compiler name and flags" The plugin parameters \f(CW\*(C`CC\*(C'\fR, \f(CW\*(C`CFLAGS\*(C'\fR and \f(CW\*(C`EXTRA_CFLAGS\*(C'\fR (written in uppercase) can be used to control which C compiler and C compiler flags are used. If not set, the default compiler and flags from when nbdkit was itself compiled from source are used. To see what those were you can do: .PP .Vb 4 \& $ nbdkit cc \-\-dump\-plugin \& ... \& CC=gcc \& CFLAGS=\-g \-O2 \-fPIC \-shared .Ve .PP The \f(CW\*(C`CFLAGS\*(C'\fR parameter overrides the built-in flags completely. The \&\f(CW\*(C`EXTRA_CFLAGS\*(C'\fR parameter adds extra flags to the built-in flags. .SS "Plugin API version" .IX Subsection "Plugin API version" Plugins compiled this way must use the same API version as the cc plugin itself uses. Currently this is \f(CW\*(C`NBDKIT_API_VERSION=2\*(C'\fR. .SS "C plugin as a self-contained script" .IX Subsection "C plugin as a self-contained script" You can create a C plugin which is a self-contained script by adding the following lines at the top and ensuring the C source is executable (\f(CW\*(C`chmod\ +x\ plugin.c\*(C'\fR): .PP .Vb 3 \& #if 0 \& exec nbdkit cc "$0" "$@" \& #endif .Ve .PP The script can be run as a command with additional nbdkit flags and plugin parameters, eg: .PP .Vb 3 \& ./plugin.c \-f \-v \& ./plugin.c \-p 10000 \-\-filter=cow \& ./plugin.c param=1 .Ve .SS "Using this plugin with C++" .IX Subsection "Using this plugin with C++" .Vb 1 \& nbdkit cc CC=g++ source.cpp .Ve .PP C++ plugin scripts can be created similarly to C, but you must add \&\f(CW\*(C`CC=g++\*(C'\fR as a parameter to exec nbdkit. .SS "Using this plugin with OCaml" .IX Subsection "Using this plugin with OCaml" .Vb 3 \& nbdkit cc CC=ocamlopt \e \& CFLAGS="\-output\-obj \-runtime\-variant _pic NBDKit.cmx \-cclib \-lnbdkitocaml" \e \& source.ml .Ve .PP OCaml plugin scripts can be created using this trick: .PP .Vb 7 \& (*/.)>/dev/null 2>&1 \& exec nbdkit cc "$0" \e \& CC=ocamlopt \e \& CFLAGS="\-output\-obj \-runtime\-variant _pic NBDKit.cmx \-cclib \-lnbdkitocaml" \e \& "$@" \& *) \& (* followed by OCaml code for the plugin here *) .Ve .PP As with C plugin scripts, the file must be executable. See also \&\fBnbdkit\-ocaml\-plugin\fR\|(3). .SS "Using this plugin with other programming languages" .IX Subsection "Using this plugin with other programming languages" This plugin can be used with most ahead-of-time compiled programming languages if they can create shared objects (\fI.so\fR files). The only requirement is that the compiler (\f(CW\*(C`CC\*(C'\fR) supports an \fI\-o\fR option to write a shared object. .SH PARAMETERS .IX Header "PARAMETERS" The script name, or \f(CW\*(C`\-\*(C'\fR, must appear as the first parameter. .IP \fBCC=\fRCC 4 .IX Item "CC=CC" .PD 0 .IP "\fBCFLAGS=""\fRCFLAGS\fB""\fR" 4 .IX Item "CFLAGS=""CFLAGS""" .IP "\fBEXTRA_CFLAGS=""\fREXTRA_CFLAGS\fB""\fR" 4 .IX Item "EXTRA_CFLAGS=""EXTRA_CFLAGS""" .PD Override the compiler and flags. See "Compiler name and flags" above. .PP All other parameters on the command line are passed to the plugin. .SH EXAMPLE .IX Header "EXAMPLE" .Vb 3 \& $ nbdkit cc \- <<\*(AqEOF\*(Aq \& #include \& #include \& \& #define NBDKIT_API_VERSION 2 \& #include \& \& char data[10*1024*1024]; \& \& #define THREAD_MODEL NBDKIT_THREAD_MODEL_PARALLEL \& \& static void * \& my_open (int readonly) \& { \& return NBDKIT_HANDLE_NOT_NEEDED; \& } \& \& static int64_t \& my_get_size (void *handle) \& { \& return (int64_t) sizeof (data); \& } \& \& static int \& my_pread (void *handle, void *buf, \& uint32_t count, uint64_t offset, \& uint32_t flags) \& { \& memcpy (buf, data+offset, count); \& return 0; \& } \& \& static int \& my_pwrite (void *handle, const void *buf, \& uint32_t count, uint64_t offset, \& uint32_t flags) \& { \& memcpy (data+offset, buf, count); \& return 0; \& } \& \& static struct nbdkit_plugin plugin = { \& .name = "myplugin", \& .open = my_open, \& .get_size = my_get_size, \& .pread = my_pread, \& .pwrite = my_pwrite, \& }; \& \& NBDKIT_REGISTER_PLUGIN(plugin) \& EOF .Ve .SH FILES .IX Header "FILES" .ie n .IP \fR\fI$plugindir\fR\fI/nbdkit\-cc\-plugin.so\fR 4 .el .IP \fR\f(CI$plugindir\fR\fI/nbdkit\-cc\-plugin.so\fR 4 .IX Item "$plugindir/nbdkit-cc-plugin.so" The plugin. .Sp Use \f(CW\*(C`nbdkit \-\-dump\-config\*(C'\fR to find the location of \f(CW$plugindir\fR. .SH VERSION .IX Header "VERSION" \&\f(CW\*(C`nbdkit\-cc\-plugin\*(C'\fR first appeared in nbdkit 1.22. .SH "SEE ALSO" .IX Header "SEE ALSO" \&\fBnbdkit\fR\|(1), \&\fBnbdkit\-plugin\fR\|(3), \&\fBnbdkit\-eval\-plugin\fR\|(3). \&\fBnbdkit\-ocaml\-plugin\fR\|(3). \&\fBnbdkit\-sh\-plugin\fR\|(3). .SH AUTHORS .IX Header "AUTHORS" Richard W.M. Jones .SH COPYRIGHT .IX Header "COPYRIGHT" Copyright Red Hat .SH LICENSE .IX Header "LICENSE" Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: .IP \(bu 4 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. .IP \(bu 4 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. .IP \(bu 4 Neither the name of Red Hat nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. .PP THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.