]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
valaccompiler.vala: Check for MSVC-like compiler meson-msvc
authorChun-wei Fan <fanchunwei@src.gnome.org>
Fri, 28 Jan 2022 04:50:38 +0000 (12:50 +0800)
committerChun-wei Fan <fanchunwei@src.gnome.org>
Fri, 28 Jan 2022 08:48:52 +0000 (16:48 +0800)
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
vala/valacodecontext.vala

index cc635abfd49946b4823244716a39df1e1905dc30..ee7458e2f782f13c5a1392c1c663de126523fdd0 100644 (file)
@@ -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);
index 9fc211f7a3a85b104c3833bbc420cd5de17ae46d..7acf7edddf10306b2a36817aaeb73e7c054bccd9 100644 (file)
@@ -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";
                }