Browse Source

GenerateExportHeader: Fix Clang support on Windows

When using Clang with Visual Studio, the GenerateExportHeader function
generates the DEPRECATED definition "__attribute__ ((__deprecated__))",
because Clang can compile such instruction. However, if a user wants to compile
the library with this generated header with MSVC, the declaration is not valid.

We suggest to always use the DEPRECATED definition "__declspec(deprecated)" on
Windows, which is valid for both Clang and MSVC.
Corentin Damman 2 years ago
parent
commit
1e19936320

+ 1 - 1
Modules/GenerateExportHeader.cmake

@@ -293,7 +293,7 @@ macro(_DO_SET_MACRO_VALUES TARGET_LIBRARY)
   set(DEFINE_IMPORT)
   set(DEFINE_NO_EXPORT)
 
-  if (COMPILER_HAS_DEPRECATED_ATTR)
+  if (COMPILER_HAS_DEPRECATED_ATTR AND NOT WIN32)
     set(DEFINE_DEPRECATED "__attribute__ ((__deprecated__))")
   elseif(COMPILER_HAS_DEPRECATED)
     set(DEFINE_DEPRECATED "__declspec(deprecated)")

+ 3 - 1
Tests/RunCMake/GenerateExportHeader/GEH.cmake

@@ -100,7 +100,9 @@ if (WIN32 OR CYGWIN)
     set(_platform Win32-Clang)
   elseif(MSVC AND COMPILER_HAS_DEPRECATED)
     set(_platform Win32)
-  elseif((MINGW OR CYGWIN) AND COMPILER_HAS_DEPRECATED)
+  elseif(CYGWIN AND COMPILER_HAS_DEPRECATED)
+    set(_platform Cygwin)
+  elseif(MINGW AND COMPILER_HAS_DEPRECATED)
     set(_platform MinGW)
   else()
     set(_platform WinEmpty)

+ 42 - 0
Tests/RunCMake/GenerateExportHeader/reference/Cygwin/libshared_export.h

@@ -0,0 +1,42 @@
+
+#ifndef LIBSHARED_EXPORT_H
+#define LIBSHARED_EXPORT_H
+
+#ifdef LIBSHARED_STATIC_DEFINE
+#  define LIBSHARED_EXPORT
+#  define LIBSHARED_NO_EXPORT
+#else
+#  ifndef LIBSHARED_EXPORT
+#    ifdef libshared_EXPORTS
+        /* We are building this library */
+#      define LIBSHARED_EXPORT __declspec(dllexport)
+#    else
+        /* We are using this library */
+#      define LIBSHARED_EXPORT __declspec(dllimport)
+#    endif
+#  endif
+
+#  ifndef LIBSHARED_NO_EXPORT
+#    define LIBSHARED_NO_EXPORT
+#  endif
+#endif
+
+#ifndef LIBSHARED_DEPRECATED
+#  define LIBSHARED_DEPRECATED __attribute__ ((__deprecated__))
+#endif
+
+#ifndef LIBSHARED_DEPRECATED_EXPORT
+#  define LIBSHARED_DEPRECATED_EXPORT LIBSHARED_EXPORT LIBSHARED_DEPRECATED
+#endif
+
+#ifndef LIBSHARED_DEPRECATED_NO_EXPORT
+#  define LIBSHARED_DEPRECATED_NO_EXPORT LIBSHARED_NO_EXPORT LIBSHARED_DEPRECATED
+#endif
+
+#if 0 /* DEFINE_NO_DEPRECATED */
+#  ifndef LIBSHARED_NO_DEPRECATED
+#    define LIBSHARED_NO_DEPRECATED
+#  endif
+#endif
+
+#endif /* LIBSHARED_EXPORT_H */

+ 42 - 0
Tests/RunCMake/GenerateExportHeader/reference/Cygwin/libstatic_export.h

@@ -0,0 +1,42 @@
+
+#ifndef LIBSTATIC_EXPORT_H
+#define LIBSTATIC_EXPORT_H
+
+#ifdef LIBSTATIC_STATIC_DEFINE
+#  define LIBSTATIC_EXPORT
+#  define LIBSTATIC_NO_EXPORT
+#else
+#  ifndef LIBSTATIC_EXPORT
+#    ifdef libstatic_EXPORTS
+        /* We are building this library */
+#      define LIBSTATIC_EXPORT
+#    else
+        /* We are using this library */
+#      define LIBSTATIC_EXPORT
+#    endif
+#  endif
+
+#  ifndef LIBSTATIC_NO_EXPORT
+#    define LIBSTATIC_NO_EXPORT
+#  endif
+#endif
+
+#ifndef LIBSTATIC_DEPRECATED
+#  define LIBSTATIC_DEPRECATED __attribute__ ((__deprecated__))
+#endif
+
+#ifndef LIBSTATIC_DEPRECATED_EXPORT
+#  define LIBSTATIC_DEPRECATED_EXPORT LIBSTATIC_EXPORT LIBSTATIC_DEPRECATED
+#endif
+
+#ifndef LIBSTATIC_DEPRECATED_NO_EXPORT
+#  define LIBSTATIC_DEPRECATED_NO_EXPORT LIBSTATIC_NO_EXPORT LIBSTATIC_DEPRECATED
+#endif
+
+#if 0 /* DEFINE_NO_DEPRECATED */
+#  ifndef LIBSTATIC_NO_DEPRECATED
+#    define LIBSTATIC_NO_DEPRECATED
+#  endif
+#endif
+
+#endif /* LIBSTATIC_EXPORT_H */

+ 1 - 1
Tests/RunCMake/GenerateExportHeader/reference/MinGW/libshared_export.h

@@ -22,7 +22,7 @@
 #endif
 
 #ifndef LIBSHARED_DEPRECATED
-#  define LIBSHARED_DEPRECATED __attribute__ ((__deprecated__))
+#  define LIBSHARED_DEPRECATED __declspec(deprecated)
 #endif
 
 #ifndef LIBSHARED_DEPRECATED_EXPORT

+ 1 - 1
Tests/RunCMake/GenerateExportHeader/reference/MinGW/libstatic_export.h

@@ -22,7 +22,7 @@
 #endif
 
 #ifndef LIBSTATIC_DEPRECATED
-#  define LIBSTATIC_DEPRECATED __attribute__ ((__deprecated__))
+#  define LIBSTATIC_DEPRECATED __declspec(deprecated)
 #endif
 
 #ifndef LIBSTATIC_DEPRECATED_EXPORT

+ 1 - 1
Tests/RunCMake/GenerateExportHeader/reference/Win32-Clang/libshared_export.h

@@ -22,7 +22,7 @@
 #endif
 
 #ifndef LIBSHARED_DEPRECATED
-#  define LIBSHARED_DEPRECATED __attribute__ ((__deprecated__))
+#  define LIBSHARED_DEPRECATED __declspec(deprecated)
 #endif
 
 #ifndef LIBSHARED_DEPRECATED_EXPORT

+ 1 - 1
Tests/RunCMake/GenerateExportHeader/reference/Win32-Clang/libstatic_export.h

@@ -22,7 +22,7 @@
 #endif
 
 #ifndef LIBSTATIC_DEPRECATED
-#  define LIBSTATIC_DEPRECATED __attribute__ ((__deprecated__))
+#  define LIBSTATIC_DEPRECATED __declspec(deprecated)
 #endif
 
 #ifndef LIBSTATIC_DEPRECATED_EXPORT