]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
Move package adding logic to CodeContext
authorLuca Bruno <lethalman88@gmail.com>
Mon, 23 Aug 2010 17:42:44 +0000 (19:42 +0200)
committerJürg Billeter <j@bitron.ch>
Sun, 3 Oct 2010 20:17:39 +0000 (22:17 +0200)
compiler/valacompiler.vala
vala/valacodecontext.vala
vapigen/valavapigen.vala

index 33cfc1949d68a2c3dfa006dd186d7176a632cfb6..09c5aac9be30ae82ca64c6191eb4ea3c2e2a4076 100644 (file)
@@ -151,56 +151,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, SourceFileType.PACKAGE, gir_path));
-
-               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, SourceFileType.PACKAGE, package_path));
-               
-               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);
@@ -247,6 +197,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;
@@ -284,9 +236,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;
@@ -307,27 +257,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");
                                }
@@ -365,11 +307,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;
index 0dbcf0b95319d0f0b0097f0741be707ceb8d7f94..8525fff5ab22f664ff5bb8c5b69d0e79c43efc4b 100644 (file)
@@ -103,6 +103,16 @@ public class Vala.CodeContext {
         */
        public string directory { get; set; }
 
+       /**
+        * 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.
         */
@@ -293,6 +303,72 @@ public class Vala.CodeContext {
                packages.add (pkg);
        }
 
+       /**
+        * 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, SourceFileType.PACKAGE, path));
+
+               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;
+               }
+
+               try {
+                       string contents;
+                       FileUtils.get_contents (filename, out contents);
+                       foreach (string package in contents.split ("\n")) {
+                               package = package.strip ();
+                               if (package != "") {
+                                       add_external_package (package);
+                               }
+                       }
+               } catch (FileError e) {
+                       Report.error (null, "Unable to read dependency file: %s".printf (e.message));
+                       return false;
+               }
+
+               return true;
+       }
+
        /**
         * Add the specified source file to the context. Only .vala, .vapi, .gs,
         * and .c extensions are supported.
@@ -335,6 +411,11 @@ public class Vala.CodeContext {
                        source_file.relative_filename = filename;
 
                        add_source_file (source_file);
+                       // look for a local .deps
+                       var deps_filename = "%s.deps".printf (filename.ndup (filename.length - ".vapi".length));
+                       if (!add_packages_from_file (deps_filename)) {
+                               return false;
+                       }
                } else if (filename.has_suffix (".c")) {
                        add_c_source_file (rpath);
                } else {
@@ -366,8 +447,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 */
@@ -380,8 +461,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) {
index 577d5192708bc5422a9a9355b1335688a00a33f6..32bbb25a6a36da053b662f0eedcee05ae2143dc7 100644 (file)
@@ -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, SourceFileType.PACKAGE, package_path));
-
-               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;
                }