[vala/switch-to-gir: 2/34] Move adding packages logic to the CodeContext
- From: Luca Bruno <lucabru src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [vala/switch-to-gir: 2/34] Move adding packages logic to the CodeContext
- Date: Sun, 29 Aug 2010 16:19:53 +0000 (UTC)
commit a55d00709947c701aa9ed4e32cd1f3be7fbf30e8
Author: Luca Bruno <lethalman88 gmail com>
Date: Mon Aug 23 19:42:44 2010 +0200
Move adding packages logic to the CodeContext
compiler/valacompiler.vala | 81 +++++-------------------------------
vala/valacodecontext.vala | 85 ++++++++++++++++++++++++++++++++++++--
vapigen/valavapigen.vala | 97 +++++++++-----------------------------------
3 files changed, 112 insertions(+), 151 deletions(-)
---
diff --git a/compiler/valacompiler.vala b/compiler/valacompiler.vala
index 45339ec..f3a445e 100644
--- a/compiler/valacompiler.vala
+++ b/compiler/valacompiler.vala
@@ -144,56 +144,6 @@ class Vala.Compiler {
}
}
- private bool add_gir (CodeContext context, string gir) {
- var gir_path = context.get_gir_path (gir, gir_directories);
-
- if (gir_path == null) {
- return false;
- }
-
- context.add_source_file (new SourceFile (context, gir_path, true));
-
- return true;
- }
-
- private bool add_package (CodeContext context, string pkg) {
- if (context.has_package (pkg)) {
- // ignore multiple occurences of the same package
- return true;
- }
-
- var package_path = context.get_package_path (pkg, vapi_directories);
-
- if (package_path == null) {
- return false;
- }
-
- context.add_package (pkg);
-
- context.add_source_file (new SourceFile (context, package_path, true));
-
- var deps_filename = Path.build_filename (Path.get_dirname (package_path), "%s.deps".printf (pkg));
- if (FileUtils.test (deps_filename, FileTest.EXISTS)) {
- try {
- string deps_content;
- size_t deps_len;
- FileUtils.get_contents (deps_filename, out deps_content, out deps_len);
- foreach (string dep in deps_content.split ("\n")) {
- dep = dep.strip ();
- if (dep != "") {
- if (!add_package (context, dep)) {
- Report.error (null, "%s, dependency of %s, not found in specified Vala API directories".printf (dep, pkg));
- }
- }
- }
- } catch (FileError e) {
- Report.error (null, "Unable to read dependency file: %s".printf (e.message));
- }
- }
-
- return true;
- }
-
private int run () {
context = new CodeContext ();
CodeContext.push (context);
@@ -240,6 +190,8 @@ class Vala.Compiler {
} else {
context.directory = context.basedir;
}
+ context.vapi_directories = vapi_directories;
+ context.gir_directories = gir_directories;
context.debug = debug;
context.thread = thread;
context.mem_profiler = mem_profiler;
@@ -276,9 +228,7 @@ class Vala.Compiler {
if (context.profile == Profile.POSIX) {
if (!nostdpkg) {
/* default package */
- if (!add_package (context, "posix")) {
- Report.error (null, "posix not found in specified Vala API directories");
- }
+ context.add_external_package ("posix");
}
} else if (context.profile == Profile.GOBJECT) {
int glib_major = 2;
@@ -299,27 +249,19 @@ class Vala.Compiler {
if (!nostdpkg) {
/* default packages */
- if (!add_package (context, "glib-2.0")) {
- Report.error (null, "glib-2.0 not found in specified Vala API directories");
- }
- if (!add_package (context, "gobject-2.0")) {
- Report.error (null, "gobject-2.0 not found in specified Vala API directories");
- }
+ context.add_external_package ("glib-2.0");
+ context.add_external_package ("gobject-2.0");
}
} else if (context.profile == Profile.DOVA) {
if (!nostdpkg) {
/* default package */
- if (!add_package (context, "dova-core-0.1")) {
- Report.error (null, "dova-core-0.1 not found in specified Vala API directories");
- }
+ context.add_external_package ("dova-core-0.1");
}
}
if (packages != null) {
foreach (string package in packages) {
- if (!add_package (context, package) && !add_gir (context, package)) {
- Report.error (null, "%s not found in specified Vala API directories or GObject-Introspection GIR directories".printf (package));
- }
+ context.add_external_package (package);
if (context.profile == Profile.GOBJECT && package == "dbus-glib-1") {
context.add_define ("DBUS_GLIB");
}
@@ -349,11 +291,10 @@ class Vala.Compiler {
bool has_c_files = false;
foreach (string source in sources) {
- if (!context.add_source_filename (source, run_output)) {
- break;
- }
- if (source.has_suffix (".c")) {
- has_c_files = true;
+ if (context.add_source_filename (source, run_output)) {
+ if (source.has_suffix (".c")) {
+ has_c_files = true;
+ }
}
}
sources = null;
diff --git a/vala/valacodecontext.vala b/vala/valacodecontext.vala
index 637f404..9210f28 100644
--- a/vala/valacodecontext.vala
+++ b/vala/valacodecontext.vala
@@ -112,6 +112,16 @@ public class Vala.CodeContext {
}
/**
+ * List of directories where to find .vapi files.
+ */
+ public string[] vapi_directories;
+
+ /**
+ * List of directories where to find .gir files.
+ */
+ public string[] gir_directories;
+
+ /**
* Produce debug information.
*/
public bool debug { get; set; }
@@ -302,6 +312,73 @@ public class Vala.CodeContext {
}
/**
+ * Pull the specified package into the context.
+ * The method is tolerant if the package has been already loaded.
+ *
+ * @param pkg a package name
+ * @return false if the package could not be loaded
+ *
+ */
+ public bool add_external_package (string pkg) {
+ if (has_package (pkg)) {
+ // ignore multiple occurences of the same package
+ return true;
+ }
+
+ // first try .vapi
+ var path = get_vapi_path (pkg);
+ if (path == null) {
+ // try with .gir
+ path = get_gir_path (pkg);
+ }
+ if (path == null) {
+ Report.error (null, "Package `%s' not found in specified Vala API directories or GObject-Introspection GIR directories".printf (pkg));
+ return false;
+ }
+
+ add_package (pkg);
+
+ add_source_file (new SourceFile (this, path, true));
+ var deps_filename = Path.build_filename (Path.get_dirname (path), "%s.deps".printf (pkg));
+ if (!add_packages_from_file (deps_filename)) {
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * Read the given filename and pull in packages.
+ * The method is tolerant if the file does not exist.
+ *
+ * @param filename a filanem
+ * @return false if an error occurs while reading the file or if a package could not be added
+ */
+ public bool add_packages_from_file (string filename) {
+ if (!FileUtils.test (filename, FileTest.EXISTS)) {
+ return true;
+ }
+
+ string[] packages;
+ try {
+ string contents;
+ FileUtils.get_contents (filename, out contents);
+ packages = contents.strip ().split ("\n");
+ } catch (FileError e) {
+ Report.error (null, "Unable to read packages file: %s".printf (e.message));
+ return false;
+ }
+
+ foreach (string package in packages) {
+ if (!add_external_package (package)) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ /**
* Add the specified source file to the context. Only .vala, .vapi, .gs and .c extensions are supported.
*
* @param filename a filename
@@ -373,8 +450,8 @@ public class Vala.CodeContext {
return (define in defines);
}
- public string? get_package_path (string pkg, string[] directories) {
- var path = get_file_path (pkg + ".vapi", "vala" + Config.PACKAGE_SUFFIX + "/vapi", "vala/vapi", directories);
+ public string? get_vapi_path (string pkg) {
+ var path = get_file_path (pkg + ".vapi", "vala" + Config.PACKAGE_SUFFIX + "/vapi", "vala/vapi", vapi_directories);
if (path == null) {
/* last chance: try the package compiled-in vapi dir */
@@ -387,8 +464,8 @@ public class Vala.CodeContext {
return path;
}
- public string? get_gir_path (string gir, string[] directories) {
- return get_file_path (gir + ".gir", "gir-1.0", null, directories);
+ public string? get_gir_path (string gir) {
+ return get_file_path (gir + ".gir", "gir-1.0", null, gir_directories);
}
string? get_file_path (string basename, string versioned_data_dir, string? data_dir, string[] directories) {
diff --git a/vapigen/valavapigen.vala b/vapigen/valavapigen.vala
index 403fa89..0a2c993 100644
--- a/vapigen/valavapigen.vala
+++ b/vapigen/valavapigen.vala
@@ -30,6 +30,8 @@ class Vala.VAPIGen : Object {
static string[] sources;
[CCode (array_length = false, array_null_terminated = true)]
static string[] vapi_directories;
+ [CCode (array_length = false, array_null_terminated = true)]
+ static string[] gir_directories;
static string library;
[CCode (array_length = false, array_null_terminated = true)]
static string[] packages;
@@ -38,6 +40,7 @@ class Vala.VAPIGen : Object {
const OptionEntry[] options = {
{ "vapidir", 0, 0, OptionArg.FILENAME_ARRAY, ref vapi_directories, "Look for package bindings in DIRECTORY", "DIRECTORY..." },
+ { "girdir", 0, 0, OptionArg.FILENAME_ARRAY, ref gir_directories, "Look for GIR bindings in DIRECTORY", "DIRECTORY..." },
{ "pkg", 0, 0, OptionArg.STRING_ARRAY, ref packages, "Include binding for PACKAGE", "PACKAGE..." },
{ "library", 0, 0, OptionArg.STRING, ref library, "Library name", "NAME" },
{ "metadata", 0, 0, OptionArg.FILENAME, ref metadata_filename, "Metadata filename", "FILE" },
@@ -61,68 +64,20 @@ class Vala.VAPIGen : Object {
return 1;
}
}
-
- /* TODO: this is duplicated between here and the compiler. its should go somewhere on its own */
- private bool add_package (string pkg) {
- if (context.has_package (pkg)) {
- // ignore multiple occurences of the same package
- return true;
- }
-
- var package_path = context.get_package_path (pkg, vapi_directories);
-
- if (package_path == null) {
- return false;
- }
-
- context.add_package (pkg);
-
- context.add_source_file (new SourceFile (context, package_path, true));
-
- var deps_filename = Path.build_filename (Path.get_dirname (package_path), "%s.deps".printf (pkg));
- if (FileUtils.test (deps_filename, FileTest.EXISTS)) {
- try {
- string deps_content;
- ulong deps_len;
- FileUtils.get_contents (deps_filename, out deps_content, out deps_len);
- foreach (string dep in deps_content.split ("\n")) {
- dep = dep.strip ();
- if (dep != "") {
- if (!add_package (dep)) {
- Report.error (null, "%s, dependency of %s, not found in specified Vala API directories".printf (dep, pkg));
- }
- }
- }
- } catch (FileError e) {
- Report.error (null, "Unable to read dependency file: %s".printf (e.message));
- }
- }
-
- return true;
- }
- private static string[]? get_packages_from_depsfile (string depsfile) {
- try {
- string contents;
- FileUtils.get_contents (depsfile, out contents);
- return contents.strip ().split ("\n");
- } catch (FileError e) {
- // deps files are optional
- return null;
- }
- }
-
private int run () {
context = new CodeContext ();
context.profile = Profile.GOBJECT;
+ context.vapi_directories = vapi_directories;
+ context.gir_directories = gir_directories;
CodeContext.push (context);
/* default package */
- if (!add_package ("glib-2.0")) {
- Report.error (null, "glib-2.0 not found in specified Vala API directories");
- }
- if (!add_package ("gobject-2.0")) {
- Report.error (null, "gobject-2.0 not found in specified Vala API directories");
+ context.add_external_package ("glib-2.0");
+ context.add_external_package ("gobject-2.0");
+
+ if (context.report.get_errors () > 0) {
+ return quit ();
}
/* load packages from .deps file */
@@ -132,40 +87,28 @@ class Vala.VAPIGen : Object {
}
var depsfile = source.substring (0, source.length - "gi".length) + "deps";
+ context.add_packages_from_file (depsfile);
+ }
- if (!FileUtils.test (depsfile, FileTest.EXISTS)) continue;
-
- string[] deps = get_packages_from_depsfile (depsfile);
-
- foreach (string dep in deps) {
- if (!add_package (dep)) {
- Report.error (null, "%s not found in specified Vala API directories".printf (dep));
- }
- }
+ if (context.report.get_errors () > 0) {
+ return quit ();
}
// depsfile for gir case
if (library != null) {
var depsfile = library + ".deps";
- if (FileUtils.test (depsfile, FileTest.EXISTS)) {
-
- string[] deps = get_packages_from_depsfile (depsfile);
-
- foreach (string dep in deps) {
- if (!add_package (dep)) {
- Report.error (null, "%s not found in specified Vala API directories".printf (dep));
- }
- }
- }
+ context.add_packages_from_file (depsfile);
} else {
Report.error (null, "--library option must be specified");
}
+ if (context.report.get_errors () > 0) {
+ return quit ();
+ }
+
if (packages != null) {
foreach (string package in packages) {
- if (!add_package (package)) {
- Report.error (null, "%s not found in specified Vala API directories".printf (package));
- }
+ context.add_external_package (package);
}
packages = null;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]