[vala/meson-msvc: 18/18] valaccompiler.vala: Check for MSVC-like compiler




commit a1e98b8f4549d34d5cee80ed3f011d2b78cc95c4
Author: Chun-wei Fan <fanchunwei src gnome org>
Date:   Fri Jan 28 12:50:38 2022 +0800

    valaccompiler.vala: Check for MSVC-like compiler
    
    If we are using an enviroment where it is set up to be like a Visual Studio
    cmd.exe prompt, and if CC is not set, we assume that we are using Visual Studio
    
    Thus, we need to call pkg-config with the --msvc-syntax flag.  This also apples
    if we choose to use clang-cl, so that we can use the correct C compiler/linker
    flags when we compile the generated C-code.

 codegen/valaccodecompiler.vala | 32 +++++++++++++++++++++++++-------
 vala/valacodecontext.vala      |  5 ++++-
 2 files changed, 29 insertions(+), 8 deletions(-)
---
diff --git a/codegen/valaccodecompiler.vala b/codegen/valaccodecompiler.vala
index cc635abfd..ee7458e2f 100644
--- a/codegen/valaccodecompiler.vala
+++ b/codegen/valaccodecompiler.vala
@@ -37,6 +37,9 @@ public class Vala.CCodeCompiler {
         */
        public void compile (CodeContext context, string? cc_command, string[] cc_options) {
                string pc = "";
+               bool is_msvc_like = false;
+               string debug_cflag = " -g";
+               string obj_out_cflag = " -o ";
                if (context.profile == Profile.GOBJECT) {
                        pc += " gobject-2.0";
                }
@@ -45,9 +48,23 @@ public class Vala.CCodeCompiler {
                                pc += " " + pkg;
                        }
                }
+
+               // TODO compile the C code files in parallel
+
+               if (cc_command == null) {
+                       if (Environment.get_variable ("WindowsSdkDir") != null &&
+                           Environment.get_variable ("VSInstallDir") != null) {
+                               cc_command = "cl";
+                       } else {
+                               cc_command = "cc";
+                       }
+               }
+               if (cc_command == "cl" || cc_command == "clang-cl")
+                       is_msvc_like = true;
+
                string? pkgflags;
                if (pc.length > 0) {
-                       pkgflags = context.pkg_config_compile_flags (pc);
+                       pkgflags = context.pkg_config_compile_flags (pc, is_msvc_like);
                        if (pkgflags == null) {
                                return;
                        }
@@ -55,14 +72,13 @@ public class Vala.CCodeCompiler {
                        pkgflags = "";
                }
 
-               // TODO compile the C code files in parallel
-
-               if (cc_command == null) {
-                       cc_command = "cc";
+               if (is_msvc_like) {
+                       debug_cflag = " -Zi";
+                       obj_out_cflag = " -Fo";
                }
                string cmdline = cc_command;
                if (context.debug) {
-                       cmdline += " -g";
+                       cmdline += obj_out_cflag;
                }
                if (context.compile_only) {
                        cmdline += " -c";
@@ -71,7 +87,7 @@ public class Vala.CCodeCompiler {
                        if (context.directory != null && context.directory != "" && !Path.is_absolute 
(context.output)) {
                                output = "%s%c%s".printf (context.directory, Path.DIR_SEPARATOR, 
context.output);
                        }
-                       cmdline += " -o " + Shell.quote (output);
+                       cmdline += obj_out_cflag + Shell.quote (output);
                }
 
                /* we're only interested in non-pkg source files */
@@ -88,6 +104,8 @@ public class Vala.CCodeCompiler {
 
                // add libraries after source files to fix linking
                // with --as-needed and on Windows
+               if (is_msvc_like)
+                       cmdline += " -link ";
                cmdline += " " + pkgflags.strip ();
                foreach (string cc_option in cc_options) {
                        cmdline += " " + Shell.quote (cc_option);
diff --git a/vala/valacodecontext.vala b/vala/valacodecontext.vala
index 9fc211f7a..7acf7eddd 100644
--- a/vala/valacodecontext.vala
+++ b/vala/valacodecontext.vala
@@ -905,8 +905,11 @@ public class Vala.CodeContext {
                return output;
        }
 
-       public string? pkg_config_compile_flags (string package_name) {
+       public string? pkg_config_compile_flags (string package_name, bool is_msvc_like) {
                string pc = pkg_config_command + " --cflags";
+               if (is_msvc_like) {
+                       pc += " --msvc-syntax";
+               }
                if (!compile_only) {
                        pc += " --libs";
                }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]