Richard W.M. Jones
2017-Feb-01 16:30 UTC
[Libguestfs] [PATCH] Really fix C++ support in the <nbdkit-plugin.h> header file (RHBZ#1418328).
Fixes commit 3647fe831a5c6d12832c4e48d1337489fd44fa59. --- .gitignore | 1 - include/nbdkit-plugin.h | 15 +++-- tests/Makefile.am | 39 ++++++++---- tests/test-cxx-plugin.cpp | 155 ++++++++++++++++++++++++++++++++++++++++++++++ tests/test-cxx.cpp | 44 ------------- tests/test-cxx.sh | 38 ++++++++++++ 6 files changed, 230 insertions(+), 62 deletions(-) create mode 100644 tests/test-cxx-plugin.cpp delete mode 100644 tests/test-cxx.cpp create mode 100755 tests/test-cxx.sh diff --git a/.gitignore b/.gitignore index 0f4e574..59e9510 100644 --- a/.gitignore +++ b/.gitignore @@ -43,7 +43,6 @@ Makefile.in /tests/disk.xz /tests/file-data /tests/test-connect -/tests/test-cxx /tests/test-file /tests/test-gzip /tests/test-newstyle diff --git a/include/nbdkit-plugin.h b/include/nbdkit-plugin.h index 9b3a5c9..568aaf5 100644 --- a/include/nbdkit-plugin.h +++ b/include/nbdkit-plugin.h @@ -36,13 +36,13 @@ #ifndef NBDKIT_PLUGIN_H #define NBDKIT_PLUGIN_H -#ifdef __cplusplus -extern "C" { -#endif - #include <stdarg.h> #include <stdint.h> +#ifdef __cplusplus +extern "C" { +#endif + #define NBDKIT_THREAD_MODEL_SERIALIZE_CONNECTIONS 0 #define NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS 1 #define NBDKIT_THREAD_MODEL_SERIALIZE_REQUESTS 2 @@ -108,7 +108,14 @@ extern void nbdkit_vdebug (const char *msg, va_list args); extern char *nbdkit_absolute_path (const char *path); extern int64_t nbdkit_parse_size (const char *str); +#ifdef __cplusplus +#define NBDKIT_CXX_LANG_C extern "C" +#else +#define NBDKIT_CXX_LANG_C /* nothing */ +#endif + #define NBDKIT_REGISTER_PLUGIN(plugin) \ + NBDKIT_CXX_LANG_C \ struct nbdkit_plugin * \ plugin_init (void) \ { \ diff --git a/tests/Makefile.am b/tests/Makefile.am index c2d473a..daf1d2a 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -71,6 +71,12 @@ check-valgrind: # Basic server command line and start-up tests. +check_PROGRAMS +check_DATA +check_SCRIPTS +check_LTLIBRARIES +noinst_LTLIBRARIES + TESTS = \ test-help.sh \ test-help-plugin.sh \ @@ -86,24 +92,34 @@ TESTS = \ test-ipv4.sh \ test-socket-activation -check_PROGRAMS = \ +check_PROGRAMS += \ test-socket-activation test_socket_activation_SOURCES = test-socket-activation.c test_socket_activation_CFLAGS = $(WARNINGS_CFLAGS) if HAVE_CXX -# Test we can include the <nbdkit-plugin.h> header file from C++. -# The test does nothing interesting, we're only interested if -# it builds. +# This builds a plugin using the C++ compiler. The plugin +# doesn't do anything interesting when run. TESTS += \ - test-cxx -check_PROGRAMS += \ - test-cxx + test-cxx.sh +# check_LTLIBRARIES won't build a shared library (see automake manual). +# So we have to do this and add a dependency. +noinst_LTLIBRARIES += \ + test-cxx-plugin.la +test-cxx.sh: test-cxx-plugin.la -test_cxx_SOURCES = test-cxx.cpp -test_cxx_CXXFLAGS = \ +test_cxx_plugin_la_SOURCES = \ + test-cxx-plugin.cpp \ + $(top_srcdir)/include/nbdkit-plugin.h +test_cxx_plugin_la_CPPFLAGS = \ + -I$(top_srcdir)/include +test_cxx_plugin_la_CXXFLAGS = \ $(WARNINGS_CFLAGS) +# For use of the -rpath option, see: +# https://lists.gnu.org/archive/html/libtool/2007-07/msg00067.html +test_cxx_plugin_la_LDFLAGS = \ + -module -avoid-version -shared -rpath /nowhere endif # In-depth tests need libguestfs, since that is a convenient way to @@ -120,13 +136,10 @@ TESTS_ENVIRONMENT = \ LD_LIBRARY_PATH=../plugins/ocaml/.libs # Common test library. -check_LTLIBRARIES = libtest.la +check_LTLIBRARIES += libtest.la libtest_la_SOURCES = test.c test.h libtest_la_CFLAGS = $(WARNINGS_CFLAGS) -check_DATA -check_SCRIPTS - # Basic connection test. check_PROGRAMS += test-connect TESTS += test-connect.sh diff --git a/tests/test-cxx-plugin.cpp b/tests/test-cxx-plugin.cpp new file mode 100644 index 0000000..43f441e --- /dev/null +++ b/tests/test-cxx-plugin.cpp @@ -0,0 +1,155 @@ +/* nbdkit + * Copyright (C) 2013-2017 Red Hat Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * 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. + * + * * 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. + * + * 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. + */ + +#ifndef __cplusplus +#error "this test should be compiled with a C++ compiler" +#endif + +#include <config.h> + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <nbdkit-plugin.h> + +/* This looks like a 100MB disk with one empty partition. */ +static unsigned char bootsector[512] = { + 0xfa, 0xb8, 0x00, 0x10, 0x8e, 0xd0, 0xbc, 0x00, + 0xb0, 0xb8, 0x00, 0x00, 0x8e, 0xd8, 0x8e, 0xc0, + 0xfb, 0xbe, 0x00, 0x7c, 0xbf, 0x00, 0x06, 0xb9, + 0x00, 0x02, 0xf3, 0xa4, 0xea, 0x21, 0x06, 0x00, + 0x00, 0xbe, 0xbe, 0x07, 0x38, 0x04, 0x75, 0x0b, + 0x83, 0xc6, 0x10, 0x81, 0xfe, 0xfe, 0x07, 0x75, + 0xf3, 0xeb, 0x16, 0xb4, 0x02, 0xb0, 0x01, 0xbb, + 0x00, 0x7c, 0xb2, 0x80, 0x8a, 0x74, 0x01, 0x8b, + 0x4c, 0x02, 0xcd, 0x13, 0xea, 0x00, 0x7c, 0x00, + 0x00, 0xeb, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xe1, 0x5b, 0x02, 0x00, 0x00, 0x00, 0x00, 0x02, + 0x03, 0x00, 0x83, 0xbc, 0x31, 0x0c, 0x80, 0x00, + 0x00, 0x00, 0x01, 0x1f, 0x03, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xaa +}; +static unsigned char data[104857600]; + +static void +cxx_load (void) +{ + memcpy (data, bootsector, sizeof bootsector); +} + +static void * +cxx_open (int readonly) +{ + return data; +} + +static int64_t +cxx_get_size (void *handle) +{ + return (int64_t) sizeof (data); +} + +#define THREAD_MODEL NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS + +static int +cxx_pread (void *handle, void *buf, uint32_t count, uint64_t offset) +{ + memcpy (buf, data+offset, count); + return 0; +} + +static struct nbdkit_plugin plugin = {}; +static void init_plugin_struct (void) __attribute__((constructor)); +static void +init_plugin_struct (void) +{ + plugin.name = "cxx"; + plugin.version = PACKAGE_VERSION; + plugin.load = cxx_load; + plugin.open = cxx_open; + plugin.get_size = cxx_get_size; + plugin.pread = cxx_pread; +} + +NBDKIT_REGISTER_PLUGIN(plugin) diff --git a/tests/test-cxx.cpp b/tests/test-cxx.cpp deleted file mode 100644 index a78c4a3..0000000 --- a/tests/test-cxx.cpp +++ /dev/null @@ -1,44 +0,0 @@ -/* nbdkit - * Copyright (C) 2017 Red Hat Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * 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. - * - * * 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. - * - * 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. - */ - -#ifndef __cplusplus -#error "this test should be compiled with a C++ compiler" -#endif - -#include "nbdkit-plugin.h" - -int -main (int argc, char *argv[]) -{ - return 0; -} diff --git a/tests/test-cxx.sh b/tests/test-cxx.sh new file mode 100755 index 0000000..1b86167 --- /dev/null +++ b/tests/test-cxx.sh @@ -0,0 +1,38 @@ +#!/bin/bash - +# nbdkit +# Copyright (C) 2017 Red Hat Inc. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# +# * 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. +# +# * 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. +# +# 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. + +set -e + +# We only really care that the plugin compiled, so we don't +# need to run it properly. +$VG ../src/nbdkit ./.libs/test-cxx-plugin.so --version -- 2.10.2
Apparently Analagous Threads
- [PATCH nbdkit v2] Really fix C++ support in the <nbdkit-plugin.h>
- [PATCH nbdkit v3] Really fix C++ support in the <nbdkit-plugin.h>
- Re: [PATCH nbdkit filters-v2 2/5] Introduce filters.
- [PATCH nbdkit 1/3] include: Function indirection for PE DLL
- nbdkit build broken