Skip to content

Commit

Permalink
use an in-tree header for symbol export information
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
eli-schwartz committed Nov 6, 2022
1 parent b1d961c commit d384a5b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
15 changes: 6 additions & 9 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down Expand Up @@ -87,12 +86,15 @@ if (CMARK_SHARED)
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>)
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})
Expand All @@ -113,11 +115,6 @@ if (CMARK_STATIC)
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>)
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()

Expand Down
38 changes: 38 additions & 0 deletions src/cmark_export.h.in
Original file line number Diff line number Diff line change
@@ -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 */

0 comments on commit d384a5b

Please sign in to comment.