From d384a5b5024861bf81a7ee1c102a9b157606c5aa Mon Sep 17 00:00:00 2001 From: Eli Schwartz Date: Tue, 25 Oct 2022 23:22:29 -0400 Subject: [PATCH] use an in-tree header for symbol export information Relying on CMake's GenerateExportHeader produces a file that is longer than the one being added here, which for all that is just mostly defining macros not used here. And after all that, it only contains macros specific to a single compiler, while failing to consistently handle GNUC (that always supports symbol visibility even for static libraries). Replace this with a more targeted header that is easy to read or include into external build systems, and which is also more robust than the one that only exists inside CMake. --- src/CMakeLists.txt | 15 ++++++--------- src/cmark_export.h.in | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 9 deletions(-) create mode 100644 src/cmark_export.h.in diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 172d08370..81aa51fa3 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -50,7 +50,6 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmark_version.h.in ${CMAKE_CURRENT_BINARY_DIR}/cmark_version.h) include(GNUInstallDirs) -include (GenerateExportHeader) add_executable(${PROGRAM} ${PROGRAM_SOURCES}) cmark_add_compile_options(${PROGRAM}) @@ -87,12 +86,15 @@ if (CMARK_SHARED) $) add_library(cmark::cmark ALIAS ${LIBRARY}) - generate_export_header(${LIBRARY} - BASE_NAME ${PROJECT_NAME}) - list(APPEND CMARK_INSTALL ${LIBRARY}) + set(CMARK_STATIC_DEFINE 0) +else() + set(CMARK_STATIC_DEFINE 1) endif() +configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmark_export.h.in + ${CMAKE_CURRENT_BINARY_DIR}/cmark_export.h) + if (CMARK_STATIC) add_library(${STATICLIBRARY} STATIC ${LIBRARY_SOURCES}) cmark_add_compile_options(${STATICLIBRARY}) @@ -113,11 +115,6 @@ if (CMARK_STATIC) $) add_library(cmark::cmark_static ALIAS ${STATICLIBRARY}) - if (NOT CMARK_SHARED) - generate_export_header(${STATICLIBRARY} - BASE_NAME ${PROJECT_NAME}) - endif() - list(APPEND CMARK_INSTALL ${STATICLIBRARY}) endif() diff --git a/src/cmark_export.h.in b/src/cmark_export.h.in new file mode 100644 index 000000000..16eb9c668 --- /dev/null +++ b/src/cmark_export.h.in @@ -0,0 +1,38 @@ +#ifndef CMARK_EXPORT_H +#define CMARK_EXPORT_H + +/* Is this an exclusively static build */ +#if @CMARK_STATIC_DEFINE@ && ! defined CMARK_STATIC_DEFINE +# define CMARK_STATIC_DEFINE +#endif + +/* + * Here is the complicated part. Windows is special -- you cannot just define + * entry points unconditionally. + * */ +#if defined _WIN32 || defined __CYGWIN__ + /* When building static libraries, avoid marking public ones */ +# if defined CMARK_STATIC_DEFINE +# define CMARK_EXPORT + /* We are building this library */ +# elif defined libcmark_EXPORTS +# define CMARK_EXPORT __declspec(dllexport) + /* We are using this library */ +# else +# define CMARK_EXPORT __declspec(dllimport) +# endif + +/* On to the easy part. GCC and lookalikes such as clang just work */ +#elif defined __GNUC__ && __GNUC__ >= 4 +# define CMARK_EXPORT __attribute__((visibility("default"))) + +/* Older solaris support, why not */ +#elif defined __SUNPRO_C && __SUNPRO_C >= 0x550 +# define CMARK_EXPORT __global + +/* All else failed, and we don't know about this compiler. Be conservative. */ +#else +# define CMARK_EXPORT +#endif + +#endif /* CMARK_EXPORT_H */