build: Add msvc_recommended_pragmas.h
authorChun-wei Fan <fanchunwei@src.gnome.org>
Mon, 19 Jun 2023 04:31:16 +0000 (12:31 +0800)
committerMatthias Clasen <mclasen@redhat.com>
Fri, 17 Nov 2023 19:53:54 +0000 (14:53 -0500)
We really always want to force-include msvc_recommended_pragmas.h to check for
things at compile time so that we can avoid stuff like missing includes or
attempting to return a value in a function that is supposed to have a
void-return-type.

The current problem is that, as indicated in the Visual Studio CI job, that we
couldn't locate msvc_recommended_pragmas.h during the build if GLib is built
as a subproject, and/or when msvc_recommended_pragmas.h is not in the paths
indicated by %INCLUDE%, meaning that the aforementioned issues would not be
caught by CI, which will then break builds on Visual Studio for people when
msvc_recommended_pragmas.h is found during their builds.

It would also be nice to be quiet from the warnings that we can really
disregard anyways.

So, add a copy of msvc_recommended_pragmas.h from GLib and update the build
files to look for it in build-aux/msvc, so that it can always be used during
the build, especially by the CI.

build-aux/msvc/msvc_recommended_pragmas.h [new file with mode: 0644]
meson.build

diff --git a/build-aux/msvc/msvc_recommended_pragmas.h b/build-aux/msvc/msvc_recommended_pragmas.h
new file mode 100644 (file)
index 0000000..051a02a
--- /dev/null
@@ -0,0 +1,41 @@
+#ifndef _MSC_VER
+#pragma error "This header is for Microsoft VC or clang-cl only."
+#endif /* _MSC_VER */
+
+/* Make MSVC more pedantic, this is a recommended pragma list
+ * from _Win32_Programming_ by Rector and Newcomer.
+ */
+#ifndef __clang__
+#pragma warning(error:4002) /* too many actual parameters for macro */
+#pragma warning(error:4003) /* not enough actual parameters for macro */
+#pragma warning(1:4010)     /* single-line comment contains line-continuation character */
+#pragma warning(error:4013) /* 'function' undefined; assuming extern returning int */
+#pragma warning(1:4016)     /* no function return type; using int as default */
+#pragma warning(error:4020) /* too many actual parameters */
+#pragma warning(error:4021) /* too few actual parameters */
+#pragma warning(error:4027) /* function declared without formal parameter list */
+#pragma warning(error:4029) /* declared formal parameter list different from definition */
+#pragma warning(error:4033) /* 'function' must return a value */
+#pragma warning(error:4035) /* 'function' : no return value */
+#pragma warning(error:4045) /* array bounds overflow */
+#pragma warning(error:4047) /* different levels of indirection */
+#pragma warning(error:4049) /* terminating line number emission */
+#pragma warning(error:4053) /* An expression of type void was used as an operand */
+#pragma warning(error:4071) /* no function prototype given */
+#pragma warning(disable:4101) /* unreferenced local variable */
+#pragma warning(error:4150)
+
+/* G_NORETURN */
+#pragma warning(error:4646) /* function declared with __declspec(noreturn) has non-void return type */
+#pragma warning(error:4715) /* 'function': not all control paths return a value */
+#pragma warning(error:4098) /* 'void' function returning a value */
+
+#pragma warning(disable:4244)  /* No possible loss of data warnings */
+#pragma warning(disable:4305)   /* No truncation from int to char warnings */
+
+#pragma warning(error:4819) /* The file contains a character that cannot be represented in the current code page */
+#endif                      /* __clang__ */
+
+/* work around Microsoft's premature attempt to deprecate the C-Library */
+#define _CRT_SECURE_NO_WARNINGS
+#define _CRT_NONSTDC_NO_WARNINGS
index c6653d3d002a95fab9d8383d74b364b6d824ebe9..f96487f96409635f5945e0a5ea107efb9921bccb 100644 (file)
@@ -248,10 +248,8 @@ endif
 
 # Compiler flags
 if cc.get_id() == 'msvc'
-  # Compiler options taken from msvc_recommended_pragmas.h
-  # in GLib, based on _Win32_Programming_ by Rector and Newcomer
   test_cflags = [
-    '-FImsvc_recommended_pragmas.h',
+    '-FI@0@/build-aux/msvc/msvc_recommended_pragmas.h'.format(meson.project_source_root()),
     '-D_USE_MATH_DEFINES',
     '-utf-8',
     '-Zc:preprocessor'