[vala/wip/transform: 166/169] Load plugins with GModule



commit 0ea28902d35a772768b4fc9199758e85431bbb25
Author: Luca Bruno <lucabru src gnome org>
Date:   Tue Feb 4 22:25:08 2014 +0100

    Load plugins with GModule

 compiler/valacompiler.vala    |   11 ++++-
 configure.ac                  |    1 +
 vala/Makefile.am              |    1 +
 vala/valacodecontext.vala     |   83 ++++++++++++++++++++++++++++++++++++++++
 vala/valacodetransformer.vala |   85 +++++++++++++++++++++++++++++++++++-----
 vapi/config.vapi              |    1 +
 6 files changed, 168 insertions(+), 14 deletions(-)
---
diff --git a/compiler/valacompiler.vala b/compiler/valacompiler.vala
index f62b18d..1304318 100644
--- a/compiler/valacompiler.vala
+++ b/compiler/valacompiler.vala
@@ -38,6 +38,8 @@ class Vala.Compiler {
        static string[] gir_directories;
        [CCode (array_length = false, array_null_terminated = true)]
        static string[] metadata_directories;
+       [CCode (array_length = false, array_null_terminated = true)]
+       static string[] plugin_directories;
        static string vapi_filename;
        static string library;
        static string shared_library;
@@ -101,6 +103,7 @@ class Vala.Compiler {
                { "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 files in 
DIRECTORY", "DIRECTORY..." },
                { "metadatadir", 0, 0, OptionArg.FILENAME_ARRAY, ref metadata_directories, "Look for GIR 
.metadata files in DIRECTORY", "DIRECTORY..." },
+               { "plugindir", 0, 0, OptionArg.FILENAME_ARRAY, ref plugin_directories, "Look for Vala 
compiler plugins in DIRECTORY", "DIRECTORY..." },
                { "pkg", 0, 0, OptionArg.STRING_ARRAY, ref packages, "Include binding for PACKAGE", 
"PACKAGE..." },
                { "vapi", 0, 0, OptionArg.FILENAME, ref vapi_filename, "Output VAPI file name", "FILE" },
                { "library", 0, 0, OptionArg.STRING, ref library, "Library name", "NAME" },
@@ -241,6 +244,7 @@ class Vala.Compiler {
                context.vapi_comments = vapi_comments;
                context.gir_directories = gir_directories;
                context.metadata_directories = metadata_directories;
+               context.plugin_directories = plugin_directories;
                context.debug = debug;
                context.mem_profiler = mem_profiler;
                context.save_temps = save_temps;
@@ -370,9 +374,10 @@ class Vala.Compiler {
                        }
                }
 
-               var transformer = new GDBusServerTransformer ();
-               transformer.head = transformer;
-               transformer.transform (context);
+               context.register_transformer (new GDBusServerTransformer ());
+               context.load_plugins ();
+
+               context.transformer.transform (context);
 
                if (context.report.get_errors () > 0 || (fatal_warnings && context.report.get_warnings () > 
0)) {
                        return quit ();
diff --git a/configure.ac b/configure.ac
index 98a13d7..1eed5e2 100644
--- a/configure.ac
+++ b/configure.ac
@@ -22,6 +22,7 @@ AC_DEFINE_UNQUOTED(PACKAGE_SUFFIX, "$PACKAGE_SUFFIX", [Define to the suffix of t
 program_transform_name="s,\$\$,${PACKAGE_SUFFIX},"
 
 AC_SUBST(pkgdatadir, [${datadir}/vala${PACKAGE_SUFFIX}])
+AC_SUBST(pkglibdir, [${libdir}/vala${PACKAGE_SUFFIX}])
 
 # Checks for programs.
 AC_PROG_CC
diff --git a/vala/Makefile.am b/vala/Makefile.am
index 4d12e4a..71aad2d 100644
--- a/vala/Makefile.am
+++ b/vala/Makefile.am
@@ -8,6 +8,7 @@ AM_CPPFLAGS = \
        $(GLIB_CFLAGS) \
        $(GMODULE_CFLAGS) \
        -DPACKAGE_DATADIR=\"$(pkgdatadir)\" \
+       -DPACKAGE_LIBDIR=\"$(pkglibdir)\"       \
        $(NULL)
 
 BUILT_SOURCES = vala.vala.stamp
diff --git a/vala/valacodecontext.vala b/vala/valacodecontext.vala
index 4484c05..6a9c941 100644
--- a/vala/valacodecontext.vala
+++ b/vala/valacodecontext.vala
@@ -129,6 +129,11 @@ public class Vala.CodeContext {
        public string[] metadata_directories;
 
        /**
+        * List of directories where to find plugins.
+        */
+       public string[] plugin_directories;
+
+       /**
         * Produce debug information.
         */
        public bool debug { get; set; }
@@ -224,6 +229,11 @@ public class Vala.CodeContext {
        public FlowAnalyzer flow_analyzer { get; private set; }
 
        /**
+        * The head of the transformers chain.
+        */
+       public CodeTransformer transformer { get; set; }
+
+       /**
         * The selected code generator.
         */
        public CodeGenerator codegen { get; set; }
@@ -491,6 +501,77 @@ public class Vala.CodeContext {
                used_attr.check_unused (this);
        }
 
+       public void load_plugins () {
+               if (!Module.supported ()) {
+                       return;
+               }
+
+               if (plugin_directories != null) {
+                       foreach (string dir in plugin_directories) {
+                               load_plugins_in_directory (dir);
+                       }
+               }
+
+               load_plugins_in_directory (Config.PACKAGE_LIBDIR);
+       }
+
+       public void load_plugins_in_directory (string dirname) {
+               Dir dir;
+               try {
+                       dir = Dir.open (dirname, 0);
+               } catch (Error e) {
+                       return;
+               }
+               
+               string? name = null;
+
+               while ((name = dir.read_name ()) != null) {
+                       string path = Path.build_filename (dirname, name);
+                       if (FileUtils.test (path, FileTest.IS_DIR)) {
+                               continue;
+                       }
+                       if (!name.has_prefix ("valaplugin")) {
+                               continue;
+                       }
+
+                       var mod = Module.open (path, ModuleFlags.BIND_LOCAL);
+
+                       if (mod == null) {
+                               if (verbose_mode) {
+                                       stdout.printf ("Could not load module: %s\n", path);
+                               }
+                               continue;
+                       } else {
+                               if (verbose_mode) {
+                                       stdout.printf ("Loaded module: %s\n", path);
+                               }
+                       }
+
+                       void* function;
+                       if (!mod.symbol ("vala_plugin_register", out function) || function == null) {
+                               if (verbose_mode) {
+                                       stdout.printf ("Could not load entry point for module %s\n", path);
+                               }
+                               continue;
+                       }
+
+                       unowned RegisterPluginFunction register_plugin = (RegisterPluginFunction) function;
+                       register_plugin (this);
+
+                       mod.make_resident ();
+               }
+       }
+
+       /**
+        * Set the given transformer as the head transformer
+        */
+       public void register_transformer (CodeTransformer transformer) {
+               transformer.next = this.transformer;
+               // Setting .head, will recursively set the head for the next transformers
+               transformer.head = transformer;
+               this.transformer = transformer;
+       }
+       
        public void add_define (string define) {
                defines.add (define);
        }
@@ -664,3 +745,5 @@ public class Vala.CodeContext {
                return rpath;
        }
 }
+
+public delegate void Vala.RegisterPluginFunction (Vala.CodeContext context);
diff --git a/vala/valacodetransformer.vala b/vala/valacodetransformer.vala
index e09d559..853c9b4 100644
--- a/vala/valacodetransformer.vala
+++ b/vala/valacodetransformer.vala
@@ -24,17 +24,81 @@
  * Code visitor for transforming the code tree.
  */
 public class Vala.CodeTransformer : CodeVisitor {
-       public CodeContext context;
+       public CodeContext context {
+               get {
+                       return _head._context;
+               }
+               set {
+                       _head._context = value;
+               }
+       }
+       CodeContext _context;
 
-       public CodeBuilder b;
-       public ArrayList<CodeBuilder> builder_stack = new ArrayList<CodeBuilder> ();
-       public HashMap<string, CodeNode> wrapper_cache;
+       public CodeBuilder b {
+               get {
+                       return _head._b;
+               }
+               set {
+                       _head._b = value;
+               }
+       }
+       CodeBuilder _b;
+       
+       public ArrayList<CodeBuilder> builder_stack {
+               get {
+                       return _head._builder_stack;
+               }
+               set {
+                       _head._builder_stack = value;
+               }
+       }
+       ArrayList<CodeBuilder> _builder_stack;
+       
+       public HashMap<string, CodeNode> wrapper_cache {
+               get {
+                       return _head._wrapper_cache;
+               }
+               set {
+                       _head._wrapper_cache = value;
+               }
+       }
+       HashMap<string, CodeNode> _wrapper_cache;
+       
        /* Keep tracks of generated stuff to avoid cycles */
-       public HashSet<CodeNode> unit_generated = new HashSet<CodeNode> ();
+       public HashSet<CodeNode> unit_generated {
+               get {
+                       return _head._unit_generated;
+               }
+               set {
+                       _head._unit_generated = value;
+               }
+       }
+       HashSet<CodeNode> _unit_generated;
 
-       public Namespace current_namespace = null;
+       public Namespace current_namespace {
+               get {
+                       return _head._current_namespace;
+               }
+               set {
+                       _head._current_namespace = value;
+               }
+       }
+       Namespace _current_namespace;
 
-       public weak CodeTransformer head;
+       public weak CodeTransformer head {
+               get {
+                       return _head;
+               }
+               
+               set {
+                       _head = value;
+                       if (next != null) {
+                               next.head = value;
+                       }
+               }
+       }
+       private CodeTransformer _head;
+       
        public CodeTransformer next;
        
        public void push_builder (CodeBuilder builder) {
@@ -108,6 +172,9 @@ public class Vala.CodeTransformer : CodeVisitor {
         */
        public void transform (CodeContext context) {
                this.context = context;
+               builder_stack = new ArrayList<CodeBuilder> ();
+               unit_generated = new HashSet<CodeNode> ();
+               
                /* we're only interested in non-pkg source files */
                var source_files = context.get_source_files ();
                foreach (SourceFile file in source_files) {
@@ -329,10 +396,6 @@ public class Vala.CodeTransformer : CodeVisitor {
                next.visit_block (b);
        }
 
-       public override void visit_empty_statement (EmptyStatement stmt) {
-               next.visit_empty_statement (stmt);
-       }
-
        public override void visit_declaration_statement (DeclarationStatement stmt) {
                next.visit_declaration_statement (stmt);
        }
diff --git a/vapi/config.vapi b/vapi/config.vapi
index 2c6df9e..e4afb86 100644
--- a/vapi/config.vapi
+++ b/vapi/config.vapi
@@ -25,5 +25,6 @@ namespace Config {
        [CCode (cheader_filename = "version.h")]
        public const string BUILD_VERSION;
        public const string PACKAGE_DATADIR;
+       public const string PACKAGE_LIBDIR;
        public const string PACKAGE_SUFFIX;
 }


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