Skip to content

Commit

Permalink
Relocatable module of image loader
Browse files Browse the repository at this point in the history
  • Loading branch information
mymedia2 authored and smohantty committed Oct 17, 2019
1 parent 1c19e28 commit 7e948b3
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 24 deletions.
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions cmake/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#define LOTTIE_IMAGE_MODULE_SUPPORT
#endif

#define LOTTIE_IMAGE_MODULE_PLUGIN "@LOTTIE_MODULE_PATH@"

#cmakedefine LOTTIE_THREAD

#ifdef LOTTIE_THREAD
Expand Down
19 changes: 19 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions meson_options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 11 additions & 2 deletions src/vector/stb/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/vector/stb/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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',
)
Expand Down
34 changes: 13 additions & 21 deletions src/vector/vimageloader.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#include "vimageloader.h"
#include "config.h"
#include "vdebug.h"
#ifndef _WIN32
#include <dlfcn.h>
#else
#include <windows.h>
#endif
#include <cstring>

#ifdef _WIN32
# include <windows.h>
#else
# include <dlfcn.h>
#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,
Expand Down Expand Up @@ -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()
Expand All @@ -56,7 +57,7 @@ struct VImageLoader::Impl {
imageFromData = reinterpret_cast<lottie_image_load_data_f>(
GetProcAddress(dl_handle, "lottie_image_load_from_data"));
}
#else
# else // _WIN32
void *dl_handle{nullptr};
void init()
{
Expand All @@ -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;
Expand All @@ -96,7 +88,7 @@ struct VImageLoader::Impl {
}
void moduleFree() {}
bool moduleLoad() { return false; }
#endif
#endif // LOTTIE_IMAGE_MODULE_SUPPORT

Impl()
{
Expand Down

0 comments on commit 7e948b3

Please sign in to comment.