From 1e0090db515e8485a37219078620a77441dbc5a6 Mon Sep 17 00:00:00 2001 From: Nicholas Guriev Date: Wed, 16 Oct 2019 21:16:12 +0300 Subject: [PATCH] Relocatable module of image loader --- CMakeLists.txt | 5 ++++- cmake/config.h.in | 2 ++ meson.build | 19 +++++++++++++++++++ meson_options.txt | 4 ++++ src/vector/stb/CMakeLists.txt | 13 +++++++++++-- src/vector/stb/meson.build | 2 ++ src/vector/vimageloader.cpp | 34 +++++++++++++--------------------- 7 files changed, 55 insertions(+), 24 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 82c1a3f..ead190e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,7 +36,10 @@ option(LOTTIE_TEST "Build LOTTIE AUTOTESTS" OFF) option(LOTTIE_CCACHE "Enable LOTTIE ccache SUPPORT" OFF) option(LOTTIE_ASAN "Compile with asan" OFF) -CONFIGURE_FILE(${CMAKE_CURRENT_LIST_DIR}/cmake/config.h.in config.h) +set(LOTTIE_MODULE_PATH "${CMAKE_SHARED_LIBRARY_PREFIX}rlottie-image-loader${CMAKE_SHARED_LIBRARY_SUFFIX}" + CACHE STRING "Absolute or relative path to dynamic loader plugin.") + +configure_file(${CMAKE_CURRENT_LIST_DIR}/cmake/config.h.in config.h) target_include_directories(rlottie PRIVATE diff --git a/cmake/config.h.in b/cmake/config.h.in index e120bba..ca42a9e 100644 --- a/cmake/config.h.in +++ b/cmake/config.h.in @@ -4,6 +4,8 @@ #define LOTTIE_IMAGE_MODULE_SUPPORT #endif +#define LOTTIE_IMAGE_MODULE_PLUGIN "@LOTTIE_MODULE_PATH@" + #cmakedefine LOTTIE_THREAD #ifdef LOTTIE_THREAD diff --git a/meson.build b/meson.build index e4d1669..f8374e0 100644 --- a/meson.build +++ b/meson.build @@ -18,6 +18,25 @@ endif if get_option('module') == true config_h.set10('LOTTIE_IMAGE_MODULE_SUPPORT', true) + + if meson.get_compiler('cpp').get_id() != 'msvc' + lib_prefix = 'lib' + else + lib_prefix = '' + endif + if host_machine.system() == 'darwin' + lib_suffix = '.dylib' + elif host_machine.system() == 'windows' + lib_suffix = '.dll' + else + lib_suffix = '.so' + endif + if get_option('moduledir') != '' + config_h.set_quoted('LOTTIE_IMAGE_MODULE_PLUGIN', + get_option('prefix') / get_option('moduledir') / lib_prefix + 'rlottie-image-loader' + lib_suffix) + else + config_h.set_quoted('LOTTIE_IMAGE_MODULE_PLUGIN', lib_prefix + 'rlottie-image-loader' + lib_suffix) + endif endif if get_option('cache') == true diff --git a/meson_options.txt b/meson_options.txt index deafc02..c81dfbc 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -13,6 +13,10 @@ option('module', value: true, description: 'Enable module support in rlottie') +option('moduledir', + type: 'string', + description: 'Dynamic plugins directory') + option('log', type: 'boolean', value: false, diff --git a/src/vector/stb/CMakeLists.txt b/src/vector/stb/CMakeLists.txt index dd96ec5..54b6bc1 100644 --- a/src/vector/stb/CMakeLists.txt +++ b/src/vector/stb/CMakeLists.txt @@ -5,12 +5,21 @@ if(LOTTIE_MODULE) target_compile_options(rlottie-image-loader PRIVATE -fvisibility=hidden ) + + get_filename_component(LOTTIE_MODULE_FILENAME ${LOTTIE_MODULE_PATH} NAME) + get_filename_component(LOTTIE_MODULE_DIR ${LOTTIE_MODULE_PATH} DIRECTORY) + if (NOT LOTTIE_MODULE_DIR) + set(LOTTIE_MODULE_DIR ${LIB_INSTALL_DIR}) + endif() + set_target_properties(rlottie-image-loader PROPERTIES DEFINE_SYMBOL LOT_BUILD + PREFIX "" + SUFFIX "" + OUTPUT_NAME ${LOTTIE_MODULE_FILENAME} ) install(TARGETS rlottie-image-loader - LIBRARY DESTINATION ${LIB_INSTALL_DIR} - ARCHIVE DESTINATION ${LIB_INSTALL_DIR} + LIBRARY DESTINATION ${LOTTIE_MODULE_DIR} ) else() target_sources(rlottie diff --git a/src/vector/stb/meson.build b/src/vector/stb/meson.build index e04bde7..49be076 100644 --- a/src/vector/stb/meson.build +++ b/src/vector/stb/meson.build @@ -2,10 +2,12 @@ source_file = ['stb_image.cpp'] if get_option('module') == true + rlottie_image_loader_dir = get_option('moduledir') != '' ? get_option('moduledir') : get_option('libdir') rlottie_image_loader_lib = shared_library('rlottie-image-loader', source_file, include_directories : [include_directories('.'), config_dir], install : true, + install_dir : rlottie_image_loader_dir, cpp_args : compiler_flags, gnu_symbol_visibility : 'hidden', ) diff --git a/src/vector/vimageloader.cpp b/src/vector/vimageloader.cpp index 03c269a..c2446be 100644 --- a/src/vector/vimageloader.cpp +++ b/src/vector/vimageloader.cpp @@ -1,13 +1,14 @@ #include "vimageloader.h" #include "config.h" #include "vdebug.h" -#ifndef _WIN32 -#include -#else -#include -#endif #include +#ifdef _WIN32 +# include +#else +# include +#endif // _WIN32 + using lottie_image_load_f = unsigned char *(*)(const char *filename, int *x, int *y, int *comp, int req_comp); using lottie_image_load_data_f = unsigned char *(*)(const char *data, int len, @@ -36,11 +37,11 @@ struct VImageLoader::Impl { lottie_image_load_data_f imageFromData{nullptr}; #ifdef LOTTIE_IMAGE_MODULE_SUPPORT -#ifdef _WIN32 +# ifdef _WIN32 HMODULE dl_handle{nullptr}; bool moduleLoad() { - dl_handle = LoadLibraryA("librlottie-image-loader.dll"); + dl_handle = LoadLibraryA(LOTTIE_IMAGE_MODULE_PLUGIN); return (dl_handle == nullptr); } void moduleFree() @@ -56,7 +57,7 @@ struct VImageLoader::Impl { imageFromData = reinterpret_cast( GetProcAddress(dl_handle, "lottie_image_load_from_data")); } -#else +# else // _WIN32 void *dl_handle{nullptr}; void init() { @@ -72,22 +73,13 @@ struct VImageLoader::Impl { { if (dl_handle) dlclose(dl_handle); } -#ifdef __APPLE__ bool moduleLoad() { - dl_handle = dlopen("librlottie-image-loader.dylib", RTLD_LAZY); + dl_handle = dlopen(LOTTIE_IMAGE_MODULE_PLUGIN, RTLD_LAZY); return (dl_handle == nullptr); } -#else - bool moduleLoad() - { - dl_handle = dlopen("librlottie-image-loader.so", RTLD_LAZY); - return (dl_handle == nullptr); - } -#endif -#endif -#else - void *dl_handle{nullptr}; +# endif // _WIN32 +#else // LOTTIE_IMAGE_MODULE_SUPPORT void init() { imageLoad = lottie_image_load; @@ -96,7 +88,7 @@ struct VImageLoader::Impl { } void moduleFree() {} bool moduleLoad() { return false; } -#endif +#endif // LOTTIE_IMAGE_MODULE_SUPPORT Impl() { -- 2.7.4