[vala/0.36: 180/212] valac: Always use the given "pkg-config" and respect PKG_CONFIG envar



commit 9fb685e9327720fbeb938b7023342db075cd056b
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Wed Feb 7 22:25:09 2018 +0100

    valac: Always use the given "pkg-config" and respect PKG_CONFIG envar

 codegen/valaccodecompiler.vala |   47 ++++--------------------------
 compiler/valacompiler.vala     |   12 ++++---
 vala/valacodecontext.vala      |   62 ++++++++++++++++++++++++++++++++++++++++
 vala/valasourcefile.vala       |   22 +-------------
 4 files changed, 77 insertions(+), 66 deletions(-)
---
diff --git a/codegen/valaccodecompiler.vala b/codegen/valaccodecompiler.vala
index 52161b4..05a7163 100644
--- a/codegen/valaccodecompiler.vala
+++ b/codegen/valaccodecompiler.vala
@@ -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
diff --git a/compiler/valacompiler.vala b/compiler/valacompiler.vala
index 1b8315c..3cabae8 100644
--- a/compiler/valacompiler.vala
+++ b/compiler/valacompiler.vala
@@ -264,6 +264,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);
@@ -481,13 +486,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);
                        }
                }
 
diff --git a/vala/valacodecontext.vala b/vala/valacodecontext.vala
index ba369a1..2f233c6 100644
--- a/vala/valacodecontext.vala
+++ b/vala/valacodecontext.vala
@@ -72,6 +72,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"; }
+
+       /**
         * Output C header file.
         */
        public string? header_filename { get; set; }
@@ -673,4 +678,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;
+       }
 }
diff --git a/vala/valasourcefile.vala b/vala/valasourcefile.vala
index 621281d..1263bcd 100644
--- a/vala/valasourcefile.vala
+++ b/vala/valasourcefile.vala
@@ -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;


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