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
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);
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);
}
}
*/
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.
*/
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;
+ }
}
_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;