]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Determine whether platform supports dll declspec
authorDon <don.j.olmstead@gmail.com>
Tue, 9 Jun 2020 16:19:06 +0000 (09:19 -0700)
committerHans Kristian Rosbach <hk-github@circlestorm.org>
Wed, 24 Jun 2020 09:48:53 +0000 (11:48 +0200)
Clang contains __has_declspec_attribute for MSVC compatibility. This can be
used to determine whether __declspec is available. Use this to determine if
dllimport/dllexport should be used when exporting the zlib API.

zconf-ng.h.in
zconf.h.in

index d2af3d59661b56527deb9c208296574ce3aa89e7..8ed84ed3b2fbfebb187b2d37fc3d5287b15f12f3 100644 (file)
 #  endif
 #endif
 
+/* Clang macro for detecting declspec support
+ * https://clang.llvm.org/docs/LanguageExtensions.html#has-declspec-attribute
+ */
+#ifndef __has_declspec_attribute
+#  define __has_declspec_attribute(x) 0
+#endif
+
 /* Maximum value for memLevel in deflateInit2 */
 #ifndef MAX_MEM_LEVEL
 #  define MAX_MEM_LEVEL 9
 /* Type declarations */
 
 
-#if defined(_WIN32)
-   /* If building or using zlib as a DLL, define ZLIB_DLL.
-    * This is not mandatory, but it offers a little performance increase.
-    */
-#  if defined(ZLIB_DLL)
-#    ifdef ZLIB_INTERNAL
-#      define ZEXTERN extern __declspec(dllexport)
-#    else
-#      define ZEXTERN extern __declspec(dllimport)
-#    endif
-#  endif  /* ZLIB_DLL */
-   /* If building or using zlib with the WINAPI/WINAPIV calling convention,
-    * define ZLIB_WINAPI.
-    * Caution: the standard ZLIB1.DLL is NOT compiled using ZLIB_WINAPI.
-    */
-#  ifdef ZLIB_WINAPI
-#    include <windows.h>
-     /* No need for _export, use ZLIB.DEF instead. */
-     /* For complete Windows compatibility, use WINAPI, not __stdcall. */
-#    define ZEXPORT WINAPI
-#    define ZEXPORTVA WINAPIV
+/* If building or using zlib as a DLL, define ZLIB_DLL.
+ * This is not mandatory, but it offers a little performance increase.
+ */
+#if defined(ZLIB_DLL) && (defined(_WIN32) || (__has_declspec_attribute(dllexport) && __has_declspec_attribute(dllimport)))
+#  ifdef ZLIB_INTERNAL
+#    define ZEXTERN extern __declspec(dllexport)
+#  else
+#    define ZEXTERN extern __declspec(dllimport)
 #  endif
 #endif
 
+/* If building or using zlib with the WINAPI/WINAPIV calling convention,
+ * define ZLIB_WINAPI.
+ * Caution: the standard ZLIB1.DLL is NOT compiled using ZLIB_WINAPI.
+ */
+#if defined(ZLIB_WINAPI) && defined(_WIN32)
+#  include <windows.h>
+   /* No need for _export, use ZLIB.DEF instead. */
+   /* For complete Windows compatibility, use WINAPI, not __stdcall. */
+#  define ZEXPORT WINAPI
+#  define ZEXPORTVA WINAPIV
+#endif
+
 #ifndef ZEXTERN
 #  define ZEXTERN extern
 #endif
index 2501330e2f3c4b108066b770f80f94ffbdc78088..8b38e89500febcf90eaeed22473b4a245309b873 100644 (file)
 #  endif
 #endif
 
+/* Clang macro for detecting declspec support
+ * https://clang.llvm.org/docs/LanguageExtensions.html#has-declspec-attribute
+ */
+#ifndef __has_declspec_attribute
+#  define __has_declspec_attribute(x) 0
+#endif
+
 /* Maximum value for memLevel in deflateInit2 */
 #ifndef MAX_MEM_LEVEL
 #  define MAX_MEM_LEVEL 9
 #  define OF(args)  args
 #endif
 
-#if defined(_WIN32)
-   /* If building or using zlib as a DLL, define ZLIB_DLL.
-    * This is not mandatory, but it offers a little performance increase.
-    */
-#  if defined(ZLIB_DLL)
-#    ifdef ZLIB_INTERNAL
-#      define ZEXTERN extern __declspec(dllexport)
-#    else
-#      define ZEXTERN extern __declspec(dllimport)
-#    endif
-#  endif  /* ZLIB_DLL */
-   /* If building or using zlib with the WINAPI/WINAPIV calling convention,
-    * define ZLIB_WINAPI.
-    * Caution: the standard ZLIB1.DLL is NOT compiled using ZLIB_WINAPI.
-    */
-#  ifdef ZLIB_WINAPI
-#    include <windows.h>
-     /* No need for _export, use ZLIB.DEF instead. */
-     /* For complete Windows compatibility, use WINAPI, not __stdcall. */
-#    define ZEXPORT WINAPI
-#    define ZEXPORTVA WINAPIV
+/* If building or using zlib as a DLL, define ZLIB_DLL.
+ * This is not mandatory, but it offers a little performance increase.
+ */
+#if defined(ZLIB_DLL) && (defined(_WIN32) || (__has_declspec_attribute(dllexport) && __has_declspec_attribute(dllimport)))
+#  ifdef ZLIB_INTERNAL
+#    define ZEXTERN extern __declspec(dllexport)
+#  else
+#    define ZEXTERN extern __declspec(dllimport)
 #  endif
 #endif
 
+/* If building or using zlib with the WINAPI/WINAPIV calling convention,
+ * define ZLIB_WINAPI.
+ * Caution: the standard ZLIB1.DLL is NOT compiled using ZLIB_WINAPI.
+ */
+#if defined(ZLIB_WINAPI) && defined(_WIN32)
+#  include <windows.h>
+   /* No need for _export, use ZLIB.DEF instead. */
+   /* For complete Windows compatibility, use WINAPI, not __stdcall. */
+#  define ZEXPORT WINAPI
+#  define ZEXPORTVA WINAPIV
+#endif
+
 #ifndef ZEXTERN
 #  define ZEXTERN extern
 #endif