]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
valac: Always use the given "pkg-config" and respect PKG_CONFIG envar 7f0e90a5c34f437b7ab8f9197a9d42fc835b1a60
authorRico Tzschichholz <ricotz@ubuntu.com>
Wed, 7 Feb 2018 21:25:09 +0000 (22:25 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Thu, 8 Feb 2018 06:44:37 +0000 (07:44 +0100)
codegen/valaccodecompiler.vala
compiler/valacompiler.vala
vala/valacodecontext.vala
vala/valasourcefile.vala

index 52161b4229a427755e2e0812479407bf0999db5b..05a71636d34bfd01a0af7877c2272ea226068c78 100644 (file)
@@ -29,57 +29,22 @@ public class Vala.CCodeCompiler {
        public CCodeCompiler () {
        }
 
-       static bool package_exists(string package_name, string? pkg_config_command = "pkg-config") {
-               string pc = pkg_config_command + " --exists " + package_name;
-               int exit_status;
-
-               try {
-                       Process.spawn_command_line_sync (pc, null, null, out exit_status);
-                       return (0 == exit_status);
-               } catch (SpawnError e) {
-                       Report.error (null, e.message);
-                       return false;
-               }
-       }
-
        /**
         * Compile generated C code to object code and optionally link object
         * files.
         *
         * @param context a code context
         */
-       public void compile (CodeContext context, string? cc_command, string[] cc_options, string? pkg_config_command = null) {
-               bool use_pkgconfig = false;
-
-               if (pkg_config_command == null) {
-                       pkg_config_command = "pkg-config";
-               }
-
-               string pc = pkg_config_command + " --cflags";
-               if (!context.compile_only) {
-                       pc += " --libs";
-               }
-               use_pkgconfig = true;
-               pc += " gobject-2.0";
+       public void compile (CodeContext context, string? cc_command, string[] cc_options) {
+               string pc = " gobject-2.0";
                foreach (string pkg in context.get_packages ()) {
-                       if (package_exists (pkg, pkg_config_command)) {
-                               use_pkgconfig = true;
+                       if (context.pkg_config_exists (pkg)) {
                                pc += " " + pkg;
                        }
                }
-               string pkgflags = "";
-               if (use_pkgconfig) {
-                       try {
-                               int exit_status;
-                               Process.spawn_command_line_sync (pc, out pkgflags, null, out exit_status);
-                               if (exit_status != 0) {
-                                       Report.error (null, "pkg-config exited with status %d".printf (exit_status));
-                                       return;
-                               }
-                       } catch (SpawnError e) {
-                               Report.error (null, e.message);
-                               return;
-                       }
+               string? pkgflags = context.pkg_config_compile_flags (pc);
+               if (pkgflags == null) {
+                       return;
                }
 
                // TODO compile the C code files in parallel
index 8edb0749d5f157e1db9936f17cf45a0bbff86992..9d4d36b95e0835d20c36f01c47324a3c7bdb0f8e 100644 (file)
@@ -286,6 +286,11 @@ class Vala.Compiler {
 
                context.run_output = run_output;
 
+               if (pkg_config_command == null) {
+                       pkg_config_command = Environment.get_variable ("PKG_CONFIG") ?? "pkg-config";
+               }
+               context.pkg_config_command = pkg_config_command;
+
                if (defines != null) {
                        foreach (string define in defines) {
                                context.add_define (define);
@@ -495,13 +500,10 @@ class Vala.Compiler {
                        if (cc_command == null && Environment.get_variable ("CC") != null) {
                                cc_command = Environment.get_variable ("CC");
                        }
-                       if (pkg_config_command == null && Environment.get_variable ("PKG_CONFIG") != null) {
-                               pkg_config_command = Environment.get_variable ("PKG_CONFIG");
-                       }
                        if (cc_options == null) {
-                               ccompiler.compile (context, cc_command, new string[] { }, pkg_config_command);
+                               ccompiler.compile (context, cc_command, new string[] { });
                        } else {
-                               ccompiler.compile (context, cc_command, cc_options, pkg_config_command);
+                               ccompiler.compile (context, cc_command, cc_options);
                        }
                }
 
index 713ba7eb21f246c910bac92ca7ccc677d392017b..d3955df70a6bdd35a6b5b44acb2328010ddb5772 100644 (file)
@@ -71,6 +71,11 @@ public class Vala.CodeContext {
         */
        public bool ccode_only { get; set; }
 
+       /**
+        * Command to run pkg-config.
+        */
+       public string pkg_config_command { get; set; default = "pkg-config"; }
+
        /**
         * Enable support for ABI stability.
         */
@@ -684,4 +689,61 @@ public class Vala.CodeContext {
 
                return rpath;
        }
+
+       public bool pkg_config_exists (string package_name) {
+               string pc = pkg_config_command + " --exists " + package_name;
+               int exit_status;
+
+               try {
+                       Process.spawn_command_line_sync (pc, null, null, out exit_status);
+                       return (0 == exit_status);
+               } catch (SpawnError e) {
+                       Report.error (null, e.message);
+                       return false;
+               }
+       }
+
+       public string? pkg_config_modversion (string package_name) {
+               string pc = pkg_config_command + " --silence-errors --modversion " + package_name;
+               string? output = null;
+               int exit_status;
+
+               try {
+                       Process.spawn_command_line_sync (pc, out output, null, out exit_status);
+                       if (exit_status != 0) {
+                               output = output[0:-1];
+                               if (output == "") {
+                                       output = null;
+                               }
+                       }
+               } catch (SpawnError e) {
+                       output = null;
+               }
+
+               return output;
+       }
+
+       public string? pkg_config_compile_flags (string package_name) {
+               string pc = pkg_config_command + " --cflags";
+               if (!compile_only) {
+                       pc += " --libs";
+               }
+               pc += package_name;
+
+               string? output = null;
+               int exit_status;
+
+               try {
+                       Process.spawn_command_line_sync (pc, out output, null, out exit_status);
+                       if (exit_status != 0) {
+                               Report.error (null, "%s exited with status %d".printf (pkg_config_command, exit_status));
+                               return null;
+                       }
+               } catch (SpawnError e) {
+                       Report.error (null, e.message);
+                       output = null;
+               }
+
+               return output;
+       }
 }
index 621281dc560ee2c8d0f75643ea7a9acfdd1cfb2b..1263bcd7d790d249a679ca24a25bf684cd22643c 100644 (file)
@@ -70,26 +70,8 @@ public class Vala.SourceFile {
 
                        _version_requested = true;
 
-                       string pkg_config_name = package_name;
-                       if (pkg_config_name == null) {
-                               return null;
-                       }
-
-                       string? standard_output;
-                       int exit_status;
-
-                       try {
-                               Process.spawn_command_line_sync ("pkg-config --silence-errors --modversion %s".printf (pkg_config_name), out standard_output, null, out exit_status);
-                               if (exit_status != 0) {
-                                       return null;
-                               }
-                       } catch (GLib.SpawnError err) {
-                               return null;
-                       }
-
-                       standard_output = standard_output[0:-1];
-                       if (standard_output != "") {
-                               _installed_version = standard_output;
+                       if (_package_name != null) {
+                               _installed_version = context.pkg_config_modversion (package_name);
                        }
 
                        return _installed_version;