From 349a7b766333f831790374fe06ca21d64d8b26ee Mon Sep 17 00:00:00 2001 From: Kleis Auke Wolthuizen Date: Tue, 17 Jan 2023 19:16:30 +0100 Subject: [PATCH] Build a dynamically loadable module for resvg --- libvips/foreign/foreign.c | 2 +- libvips/foreign/meson.build | 9 ++++- libvips/meson.build | 15 ++++++++ libvips/module/resvg.c | 70 +++++++++++++++++++++++++++++++++++++ meson.build | 21 ++++++----- meson_options.txt | 5 +++ 6 files changed, 112 insertions(+), 10 deletions(-) create mode 100644 libvips/module/resvg.c diff --git a/libvips/foreign/foreign.c b/libvips/foreign/foreign.c index 3da372e52e..a5066413d1 100644 --- a/libvips/foreign/foreign.c +++ b/libvips/foreign/foreign.c @@ -3006,7 +3006,7 @@ vips_foreign_operation_init( void ) vips_foreign_load_pdf_source_get_type(); #endif /*HAVE_PDFIUM*/ -#if defined(HAVE_RSVG) || defined(HAVE_RESVG) +#if (defined(HAVE_RSVG) || defined(HAVE_RESVG)) && !defined(RESVG_MODULE) vips_foreign_load_svg_file_get_type(); vips_foreign_load_svg_buffer_get_type(); #ifdef HAVE_RSVG diff --git a/libvips/foreign/meson.build b/libvips/foreign/meson.build index a72f9c7867..253faa3d11 100644 --- a/libvips/foreign/meson.build +++ b/libvips/foreign/meson.build @@ -44,7 +44,6 @@ foreign_sources = files( 'rawsave.c', 'spngload.c', 'spngsave.c', - 'svgload.c', 'tiff2vips.c', 'tiff.c', 'tiffload.c', @@ -123,6 +122,14 @@ endif libvips_sources += foreign_sources +resvg_module_sources = files( + 'svgload.c', +) + +if not resvg_module + foreign_sources += resvg_module_sources +endif + foreign_lib = static_library('foreign', foreign_sources, foreign_headers, diff --git a/libvips/meson.build b/libvips/meson.build index 4fe672b1f1..29e0a27631 100644 --- a/libvips/meson.build +++ b/libvips/meson.build @@ -165,3 +165,18 @@ if openslide_module install_dir: module_dir ) endif + +if resvg_module + shared_module('vips-resvg', + 'module/resvg.c', + resvg_module_sources, + c_args: module_c_args, + link_args: module_link_args, + name_prefix: '', + name_suffix: module_suffix, + dependencies: [gmodule_dep, libvips_dep, resvg_dep], + gnu_symbol_visibility: 'hidden', + install: true, + install_dir: module_dir + ) +endif diff --git a/libvips/module/resvg.c b/libvips/module/resvg.c new file mode 100644 index 0000000000..de53db8960 --- /dev/null +++ b/libvips/module/resvg.c @@ -0,0 +1,70 @@ +/* resvg as a dynamically loadable module + * + * 17/1/23 kleisauke + * - initial + */ + +/* + + This file is part of VIPS. + + VIPS is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA + + */ + +/* + + These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk + + */ + +/* +#define DEBUG + */ + +#ifdef HAVE_CONFIG_H +#include +#endif /*HAVE_CONFIG_H*/ +#include + +#include +#include + +#include +#include +#include + +#if defined(HAVE_RESVG) && defined(RESVG_MODULE) + +/* This is called on module load. + */ +G_MODULE_EXPORT const gchar * +g_module_check_init( GModule *module ) +{ +#ifdef DEBUG + printf( "vips_resvg: module init\n" ); +#endif /*DEBUG*/ + + extern GType vips_foreign_load_svg_file_get_type( void ); + extern GType vips_foreign_load_svg_buffer_get_type( void ); + + vips_foreign_load_svg_file_get_type(); + vips_foreign_load_svg_buffer_get_type(); + + return( NULL ); +} + +#endif /*defined(HAVE_RESVG) && defined(RESVG_MODULE)*/ diff --git a/meson.build b/meson.build index b2ed5d876c..ff0d21d3e3 100644 --- a/meson.build +++ b/meson.build @@ -371,13 +371,18 @@ if librsvg_found cfg_var.set('HAVE_RSVG', '1') endif -# libresvg doesn't ship pkg-config files, so we can't use dependency(). -libresvg_dep = cc.find_library('resvg', has_headers: ['resvg.h'], required: get_option('resvg')) -libresvg_found = libresvg_dep.found() -if libresvg_found - # It's annoying that I have to spell out the `include_directories` here, but without it Meson can build - # but `compile_commands.json` doesn't have the -I$PREFIX/include, making code analysis tools & VSCode fail. - libvips_deps += declare_dependency(dependencies: [libresvg_dep], include_directories: [include_directories(get_option('prefix') / get_option('includedir'))], link_args: ['-ldl']) +# only if librsvg not found +resvg_dep = disabler() +resvg_module = false +if not librsvg_found + # resvg doesn't ship pkg-config files, so we can't use dependency() + resvg_dep = cc.find_library('resvg', has_headers: ['resvg.h'], required: get_option('resvg')) + resvg_module = modules_enabled and not get_option('resvg-module').disabled() + if resvg_module + cfg_var.set('RESVG_MODULE', '1') + else + libvips_deps += resvg_dep + endif cfg_var.set('HAVE_RESVG', '1') endif @@ -644,7 +649,7 @@ build_summary = { 'PDF load with PDFium': [pdfium_dep.found()], 'PDF load with poppler-glib': [libpoppler_found, ' (dynamic module: ', libpoppler_module, ')'], 'SVG load with librsvg': [librsvg_found], - 'SVG load with libresvg': [libresvg_found], + 'SVG load with libresvg': [resvg_dep.found(), ' (dynamic module: ', resvg_module, ')'], 'EXR load with OpenEXR': [openexr_dep.found()], 'OpenSlide load': [openslide_dep.found(), ' (dynamic module: ', openslide_module, ')'], 'Matlab load with libmatio': [matio_dep.found()], diff --git a/meson_options.txt b/meson_options.txt index 0fbe593df7..022bcb2952 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -193,6 +193,11 @@ option('resvg', value: 'auto', description: 'Build with resvg') +option('resvg-module', + type: 'feature', + value: 'auto', + description: 'Build resvg as module') + option('spng', type: 'feature', value: 'auto',