diff options
author | Rico Tzschichholz <ricotz@ubuntu.com> | 2018-02-07 22:25:09 +0100 |
---|---|---|
committer | Rico Tzschichholz <ricotz@ubuntu.com> | 2018-02-08 07:44:37 +0100 |
commit | 7f0e90a5c34f437b7ab8f9197a9d42fc835b1a60 (patch) | |
tree | f65705f17cd1e3dcda3882cd8a6fb998747e3c1c | |
parent | cc629f3724d4a72e38726a4264ab69a925aa00c9 (diff) | |
download | vala-7f0e90a5c34f437b7ab8f9197a9d42fc835b1a60.tar.gz |
valac: Always use the given "pkg-config" and respect PKG_CONFIG envar
-rw-r--r-- | codegen/valaccodecompiler.vala | 47 | ||||
-rw-r--r-- | compiler/valacompiler.vala | 12 | ||||
-rw-r--r-- | vala/valacodecontext.vala | 62 | ||||
-rw-r--r-- | vala/valasourcefile.vala | 22 |
4 files changed, 77 insertions, 66 deletions
diff --git a/codegen/valaccodecompiler.vala b/codegen/valaccodecompiler.vala index 52161b422..05a71636d 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 8edb0749d..9d4d36b95 100644 --- a/compiler/valacompiler.vala +++ b/compiler/valacompiler.vala @@ -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); } } diff --git a/vala/valacodecontext.vala b/vala/valacodecontext.vala index 713ba7eb2..d3955df70 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"; } + + /** * Enable support for ABI stability. */ public bool abi_stability { get; set; } @@ -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; + } } diff --git a/vala/valasourcefile.vala b/vala/valasourcefile.vala index 621281dc5..1263bcd7d 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; |