Skip to content

Commit

Permalink
Change LIBDEFLATE_STATIC back to LIBDEFLATE_DLL
Browse files Browse the repository at this point in the history
Undo 8a26b31 ("Fix libdeflate.pc on Windows") which required
Windows users to define LIBDEFLATE_STATIC when linking to the static
library, instead of LIBDEFLATE_DLL when linking to the DLL.
__declspec(dllimport) is actually optional, so the cleanest solution to
this mess seems to be to use the original LIBDEFLATE_DLL approach, and
to omit it from libdeflate.pc since it is optional anyway.

Resolves ebiggers#259
  • Loading branch information
ebiggers committed Nov 26, 2022
1 parent bb3b38f commit 82e25be
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ if(LIBDEFLATE_BUILD_STATIC_LIB)
OUTPUT_NAME ${STATIC_LIB_NAME}
PUBLIC_HEADER libdeflate.h)
target_include_directories(libdeflate_static PUBLIC ${LIB_INCLUDE_DIRS})
target_compile_definitions(libdeflate_static PUBLIC LIBDEFLATE_STATIC)
target_compile_definitions(libdeflate_static PRIVATE ${LIB_COMPILE_DEFINITIONS})
target_compile_options(libdeflate_static PRIVATE ${LIB_COMPILE_OPTIONS})
list(APPEND LIB_TARGETS libdeflate_static)
Expand All @@ -209,6 +208,7 @@ if(LIBDEFLATE_BUILD_SHARED_LIB)
C_VISIBILITY_PRESET hidden
SOVERSION 0)
target_include_directories(libdeflate_shared PUBLIC ${LIB_INCLUDE_DIRS})
target_compile_definitions(libdeflate_shared PUBLIC LIBDEFLATE_DLL)
target_compile_definitions(libdeflate_shared PRIVATE ${LIB_COMPILE_DEFINITIONS})
target_compile_options(libdeflate_shared PRIVATE ${LIB_COMPILE_OPTIONS})
target_link_libraries(libdeflate_shared PRIVATE ${LIB_LINK_LIBRARIES})
Expand Down
29 changes: 17 additions & 12 deletions libdeflate.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,25 @@ extern "C" {
#include <stdint.h>

/*
* On Windows, you must define LIBDEFLATE_STATIC if you are linking to the
* static library version of libdeflate instead of the DLL. On other platforms,
* LIBDEFLATE_STATIC has no effect.
* Users of libdeflate.dll on Windows can define LIBDEFLATE_DLL to cause
* __declspec(dllimport) to be used. This should be done when it's easy to do.
* Otherwise it's fine to skip it, since it is a very minor performance
* optimization that is irrelevant for most use cases of libdeflate.
*/
#ifdef _WIN32
# if defined(LIBDEFLATE_STATIC)
# define LIBDEFLATEEXPORT
# elif defined(BUILDING_LIBDEFLATE)
# define LIBDEFLATEEXPORT __declspec(dllexport)
# else
# define LIBDEFLATEEXPORT __declspec(dllimport)
#undef LIBDEFLATEEXPORT
#ifdef LIBDEFLATE_DLL
# if defined(_WIN32) || defined(__CYGWIN__)
# ifdef BUILDING_LIBDEFLATE
# define LIBDEFLATEEXPORT __declspec(dllexport)
# else
# define LIBDEFLATEEXPORT __declspec(dllimport)
# endif
# elif defined(__GNUC__) && defined(BUILDING_LIBDEFLATE)
# define LIBDEFLATEEXPORT __attribute__((visibility("default")))
# endif
#else
# define LIBDEFLATEEXPORT __attribute__((visibility("default")))
#endif
#ifndef LIBDEFLATEEXPORT
# define LIBDEFLATEEXPORT
#endif

#if defined(BUILDING_LIBDEFLATE) && defined(__GNUC__) && defined(__i386__)
Expand Down
9 changes: 8 additions & 1 deletion libdeflate.pc.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,11 @@ Description: Fast implementation of DEFLATE, zlib, and gzip
Version: @PROJECT_VERSION@
Libs: -L${libdir} -ldeflate
Cflags: -I${includedir}
Cflags.private: -DLIBDEFLATE_STATIC

# Note: this library's public header allows LIBDEFLATE_DLL to be defined when
# linking to the DLL on Windows, to make __declspec(dllimport) be used.
# However, the only way to define a shared-library-only flag in a pkgconfig file
# is to use the weird workaround of unconditionally defining it in Cflags, then
# undefining it in Cflags.private. Just don't bother with this, since
# __declspec(dllimport) is optional anyway. It is a very minor performance
# optimization that is irrelevant for most use cases of libdeflate.

0 comments on commit 82e25be

Please sign in to comment.