]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
Load plugins with GModule
authorLuca Bruno <lucabru@src.gnome.org>
Tue, 4 Feb 2014 21:25:08 +0000 (22:25 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Mon, 11 Mar 2019 12:52:38 +0000 (13:52 +0100)
compiler/valacompiler.vala
vala/Makefile.am
vala/valacodecontext.vala
vala/valacodetransformer.vala
vapi/config.vapi

index a508e6a9ed2bd5cbc33082263321cd8a576cf6ef..e17a5290ea06d3c9fa958698cd3ad89f1091454d 100644 (file)
@@ -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;
@@ -105,6 +107,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" },
@@ -270,6 +273,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;
@@ -403,9 +407,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 ();
index 2c13f48d63d90d9339aab2d00d8f405ac2fd32d9..774c7bb12b29f355526cf8542425da04b4536eef 100644 (file)
@@ -8,6 +8,7 @@ AM_CPPFLAGS = \
        $(GLIB_CFLAGS) \
        $(GMODULE_CFLAGS) \
        -DPACKAGE_DATADIR=\"$(pkgdatadir)\" \
+       -DPACKAGE_LIBDIR=\"$(pkglibdir)\"       \
        $(NULL)
 
 BUILT_SOURCES = vala.vala.stamp $(srcdir)/valaversion.vala
index 34543a8d9e73018aeb054aa16aaab40085639590..b7fa6ed35f9dd29a57dd15819286247d5f753c23 100644 (file)
@@ -138,6 +138,11 @@ public class Vala.CodeContext {
         */
        public string[] metadata_directories { get; set; default = {}; }
 
+       /**
+        * List of directories where to find plugins.
+        */
+       public string[] plugin_directories;
+
        /**
         * Produce debug information.
         */
@@ -228,6 +233,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.
         */
@@ -505,6 +515,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) {
                if (is_defined (define)) {
                        Report.warning (null, "`%s' is already defined".printf (define));
@@ -813,3 +894,5 @@ public class Vala.CodeContext {
                return output;
        }
 }
+
+public delegate void Vala.RegisterPluginFunction (Vala.CodeContext context);
index 4ba2d25387adc40a2bb17d55ba6c597977bddc88..c2c84a5d10ef7d62dca0187e728304ef907512aa 100644 (file)
  * 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 {
+               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;
 
-       public CodeBuilder b;
-       public ArrayList<CodeBuilder> builder_stack = new ArrayList<CodeBuilder> ();
-       public 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 {
+               get {
+                       return _head._current_namespace;
+               }
+               set {
+                       _head._current_namespace = value;
+               }
+       }
+       Namespace _current_namespace;
 
-       public Namespace current_namespace = null;
+       public weak CodeTransformer head {
+               get {
+                       return _head;
+               }
+
+               set {
+                       _head = value;
+                       if (next != null) {
+                               next.head = value;
+                       }
+               }
+       }
+       private CodeTransformer _head;
 
-       public weak 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);
        }
index c8b5d58a1aea0387371c8baa1fe6f025810b863f..5ae26aab384d7d75606a38a5d24c12da8e7c69c5 100644 (file)
@@ -23,6 +23,7 @@
 [CCode (cprefix = "", lower_case_cprefix = "", cheader_filename = "config.h")]
 namespace Config {
        public const string PACKAGE_DATADIR;
+       public const string PACKAGE_LIBDIR;
        public const string PACKAGE_SUFFIX;
        public const string PACKAGE_VALADOC_LIBDIR;
        public const string PACKAGE_VALADOC_ICONDIR;