From be2de283123358794a9492b75be6321a27a9d8d0 Mon Sep 17 00:00:00 2001 From: Vincent Torri Date: Sat, 13 May 2023 09:38:53 +0200 Subject: [PATCH] portability: fix usage of TVG_API Rule of thumb on Windows: * for a DLL: * if the library is built, set TVG_API to __declspec(dllexport) * if the library is used, set TVG_API to __declspec(dllimport) * for a static library, set TVG_API to nothing To set TVG_API for a static library, TVG_STATIC is defined when the stataic library is built. Otherwise, TVG_API is correctly set for a DLL. Also sun and intel compilers are handled @issue: https://github.com/thorvg/thorvg/issues/1446 --- inc/thorvg.h | 34 ++++++++++++++++++++------------- src/bin/meson.build | 4 +++- src/bin/svg2png/meson.build | 2 +- src/bin/svg2tvg/meson.build | 2 +- src/bindings/capi/thorvg_capi.h | 34 ++++++++++++++++++++------------- src/examples/meson.build | 4 +++- src/meson.build | 6 ++++-- test/meson.build | 4 +++- 8 files changed, 57 insertions(+), 33 deletions(-) diff --git a/inc/thorvg.h b/inc/thorvg.h index 104cd733..dd5c05fe 100644 --- a/inc/thorvg.h +++ b/inc/thorvg.h @@ -23,28 +23,36 @@ #undef TVG_API #endif -#if defined(_WIN32) && !defined(__clang__) - #if TVG_BUILD - #if TVG_EXPORT +#ifndef TVG_STATIC + #ifdef _WIN32 + #if TVG_BUILD #define TVG_API __declspec(dllexport) #else - #define TVG_API + #define TVG_API __declspec(dllimport) #endif + #elif (defined(__SUNPRO_C) || defined(__SUNPRO_CC)) + #define TVG_API __global #else - #define TVG_API __declspec(dllimport) - #endif - #define TVG_DEPRECATED __declspec(deprecated) -#else - #if TVG_BUILD - #if TVG_EXPORT - #define TVG_API __attribute__ ((visibility ("default"))) + #if (defined(__GNUC__) && __GNUC__ >= 4) || defined(__INTEL_COMPILER) + #define TVG_API __attribute__ ((visibility("default"))) #else #define TVG_API #endif - #else - #define TVG_API #endif +#else + #define TVG_API +#endif + +#ifdef TVG_DEPRECATED + #undef TVG_DEPRECATED +#endif + +#ifdef _WIN32 + #define TVG_DEPRECATED __declspec(deprecated) +#elif __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1) #define TVG_DEPRECATED __attribute__ ((__deprecated__)) +#else + #define TVG_DEPRECATED #endif #define _TVG_DECLARE_PRIVATE(A) \ diff --git a/src/bin/meson.build b/src/bin/meson.build index c4bf4c93..2e6cd012 100644 --- a/src/bin/meson.build +++ b/src/bin/meson.build @@ -1,4 +1,6 @@ -compiler_flags = ['-DTVG_BUILD'] +if (lib_type == 'static') + compiler_flags += ['-DTVG_STATIC'] +endif if all_tools or get_option('tools').contains('svg2png') == true subdir('svg2png') diff --git a/src/bin/svg2png/meson.build b/src/bin/svg2png/meson.build index ed21489e..3d8a6711 100644 --- a/src/bin/svg2png/meson.build +++ b/src/bin/svg2png/meson.build @@ -3,5 +3,5 @@ svg2png_src = files('svg2png.cpp', 'lodepng.cpp') executable('svg2png', svg2png_src, include_directories : headers, - cpp_args: compiler_flags, + cpp_args : compiler_flags, link_with : thorvg_lib) diff --git a/src/bin/svg2tvg/meson.build b/src/bin/svg2tvg/meson.build index a40111aa..8ab1512e 100644 --- a/src/bin/svg2tvg/meson.build +++ b/src/bin/svg2tvg/meson.build @@ -3,5 +3,5 @@ svg2tvg_src = files('svg2tvg.cpp') executable('svg2tvg', svg2tvg_src, include_directories : headers, - cpp_args: compiler_flags, + cpp_args : compiler_flags, link_with : thorvg_lib) diff --git a/src/bindings/capi/thorvg_capi.h b/src/bindings/capi/thorvg_capi.h index 170cc8a7..dc017487 100644 --- a/src/bindings/capi/thorvg_capi.h +++ b/src/bindings/capi/thorvg_capi.h @@ -25,28 +25,36 @@ #undef TVG_API #endif -#if defined(_WIN32) && !defined(__clang__) - #if TVG_BUILD - #if TVG_EXPORT +#ifndef TVG_STATIC + #ifdef _WIN32 + #if TVG_BUILD #define TVG_API __declspec(dllexport) #else - #define TVG_API + #define TVG_API __declspec(dllimport) #endif + #elif (defined(__SUNPRO_C) || defined(__SUNPRO_CC)) + #define TVG_API __global #else - #define TVG_API __declspec(dllimport) - #endif - #define TVG_DEPRECATED __declspec(deprecated) -#else - #if TVG_BUILD - #if TVG_EXPORT - #define TVG_API __attribute__ ((visibility ("default"))) + #if (defined(__GNUC__) && __GNUC__ >= 4) || defined(__INTEL_COMPILER) + #define TVG_API __attribute__ ((visibility("default"))) #else #define TVG_API #endif - #else - #define TVG_API #endif +#else + #define TVG_API +#endif + +#ifdef TVG_DEPRECATED + #undef TVG_DEPRECATED +#endif + +#ifdef _WIN32 + #define TVG_DEPRECATED __declspec(deprecated) +#elif __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1) #define TVG_DEPRECATED __attribute__ ((__deprecated__)) +#else + #define TVG_DEPRECATED #endif #ifdef __cplusplus diff --git a/src/examples/meson.build b/src/examples/meson.build index 46cceb7d..471f1f10 100644 --- a/src/examples/meson.build +++ b/src/examples/meson.build @@ -1,4 +1,6 @@ -compiler_flags = ['-DTVG_BUILD'] +if (lib_type == 'static') + compiler_flags += ['-DTVG_STATIC'] +endif examples_dep = dependency('elementary', required : true) diff --git a/src/meson.build b/src/meson.build index 12489f20..93541b1e 100644 --- a/src/meson.build +++ b/src/meson.build @@ -1,10 +1,12 @@ -compiler_flags = ['-DTVG_BUILD'] +compiler_flags = [] override_options = [] lib_type = get_option('default_library') if (lib_type == 'shared') - compiler_flags += ['-DTVG_EXPORT'] + compiler_flags += ['-DTVG_EXPORT', '-DTVG_BUILD'] +else + compiler_flags += ['-DTVG_STATIC'] endif cc = meson.get_compiler('cpp') diff --git a/test/meson.build b/test/meson.build index 780652b0..7e41e695 100644 --- a/test/meson.build +++ b/test/meson.build @@ -1,4 +1,6 @@ -compiler_flags = ['-DTVG_BUILD'] +if (lib_type == 'static') + compiler_flags += ['-DTVG_STATIC'] +endif test_file = [ 'testAccessor.cpp',