Bug 552858: versioning
authorColin Walters <walters@src.gnome.org>
Sun, 12 Oct 2008 04:51:48 +0000 (04:51 +0000)
committerColin Walters <walters@src.gnome.org>
Sun, 12 Oct 2008 04:51:48 +0000 (04:51 +0000)
This is a big patch.  You should probably remove your installation
tree to be cleaner.

* docs/typelib-format.txt: Add nsversion entry which holds
version of namespace.
* girepository/girepository.h: Add 'version' parameter to
g_irepository_require.  This may be NULL.  Normally
bindings should pass an explicit version though.
* girepository/girepository.c: Lots of infrastructure to
support versioning.  Add some more documentation.  Disallow
some usage of NULL namespaces.
* girepository/girmodule.c: Add version parameter.
* girepository/gtypelib.c: Update header size.
* giscanner/ast.py: Add version to Namespace.
* giscanner/girparser.py: Parse version attribute from
XML, pass to Namespace.
* giscanner/girwriter.py: Write out version parameter.
* giscanner/transformer.py: Clean up include registration.
* tests/*: Add version attribute.
* tests/invoke/invoke.c: Don't try looking up test before
it's loaded in repository.
* tools/generate.c: Output version parameter.
* gir/Makefile.am: Add 2.0 version to .gir files.

svn path=/trunk/; revision=677

39 files changed:
ChangeLog
docs/typelib-format.txt
gir/Makefile.am
girepository/girepository.c
girepository/girepository.h
girepository/girmodule.c
girepository/girmodule.h
girepository/girparser.c
girepository/gtypelib.c
girepository/gtypelib.h
giscanner/ast.py
giscanner/girparser.py
giscanner/girwriter.py
giscanner/glibtransformer.py
giscanner/transformer.py
tests/array.gir
tests/boxed.gir
tests/constant.gir
tests/enum.gir
tests/errors.gir
tests/function.gir
tests/interface.gir
tests/invoke/Makefile.am
tests/invoke/invoke.c
tests/invoke/testfns.gir [deleted file]
tests/object.gir
tests/scanner/Makefile.am
tests/scanner/annotation-1.0-expected.gir [moved from tests/scanner/annotation-expected.gir with 97% similarity]
tests/scanner/drawable-1.0-expected.gir [moved from tests/scanner/drawable-expected.gir with 87% similarity]
tests/scanner/foo-1.0-expected.gir [moved from tests/scanner/foo-expected.gir with 99% similarity]
tests/scanner/utility-1.0-expected.gir [moved from tests/scanner/utility-expected.gir with 93% similarity]
tests/struct.gir
tests/types.gir
tests/types/Makefile.am
tests/union.gir
tests/xref1.gir
tests/xref2.gir
tools/g-ir-scanner
tools/generate.c

index 0d501db..b24c23d 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,33 @@
 2008-10-11  Colin Walters  <walters@verbum.org>
 
+       Bug 552858: versioning
+
+       This is a big patch.  You should probably remove your installation
+       tree to be cleaner.
+
+       * docs/typelib-format.txt: Add nsversion entry which holds
+       version of namespace.
+       * girepository/girepository.h: Add 'version' parameter to
+       g_irepository_require.  This may be NULL.  Normally
+       bindings should pass an explicit version though.
+       * girepository/girepository.c: Lots of infrastructure to
+       support versioning.  Add some more documentation.  Disallow
+       some usage of NULL namespaces.
+       * girepository/girmodule.c: Add version parameter.
+       * girepository/gtypelib.c: Update header size.
+       * giscanner/ast.py: Add version to Namespace.
+       * giscanner/girparser.py: Parse version attribute from
+       XML, pass to Namespace.
+       * giscanner/girwriter.py: Write out version parameter.
+       * giscanner/transformer.py: Clean up include registration.
+       * tests/*: Add version attribute.
+       * tests/invoke/invoke.c: Don't try looking up test before
+       it's loaded in repository.
+       * tools/generate.c: Output version parameter.
+       * gir/Makefile.am: Add 2.0 version to .gir files.
+
+2008-10-11  Colin Walters  <walters@verbum.org>
+
        * giscanner/scannerlexer.l (parse_gtkdoc): Don't lose
        if we have mismatched parens.
 
index 5279132..af7eb3c 100644 (file)
@@ -112,6 +112,7 @@ struct Header
 
   guint32   size;
   guint32   namespace;
+  guint32   nsversion;
   
   guint16   entry_blob_size;        /* 12 */
   guint16   function_blob_size;     /* 16 */
@@ -168,6 +169,8 @@ size:     The size of the typelib.
 
 namespace:
           Offset of the namespace string in the typelib. 
+nsversion:
+          Offset of the namespace version string in the typelib. 
 
 entry_blob_size:
 function_blob_size:
index a31cdff..dbe52a9 100644 (file)
@@ -14,9 +14,9 @@ else
 GLIB_LIBRARY=glib-2.0
 endif
 
-GLib.gir: $(G_IR_SCANNER) $(G_IR_SCANNER_FILES) Makefile
+GLib-2.0.gir: $(G_IR_SCANNER) $(G_IR_SCANNER_FILES) Makefile
        PYTHONPATH=$(top_builddir):$$PYTHONPATH $(G_IR_SCANNER) \
-           -v --namespace GLib \
+           -v --namespace GLib --nsversion=2.0 \
            --noclosure \
            --output $@ \
            --strip-prefix=g \
@@ -29,8 +29,8 @@ GLib.gir: $(G_IR_SCANNER) $(G_IR_SCANNER_FILES) Makefile
            $(GLIB_LIBDIR)/glib-2.0/include/glibconfig.h \
            $(GLIB_INCLUDEDIR)/glib/*.h
        PYTHONPATH=$(top_builddir):$$PYTHONPATH $(G_IR_SCANNER) \
-           --xpath-assertions=GLib-assertions.txt GLib.gir     
-BUILT_SOURCES += GLib.gir
+           --xpath-assertions=GLib-assertions.txt GLib-2.0.gir 
+BUILT_SOURCES += GLib-2.0.gir
 
 # gobject
 GOBJECT_INCLUDEDIR=`pkg-config --variable=includedir gobject-2.0`/glib-2.0
@@ -42,20 +42,20 @@ else
 GOBJECT_LIBRARY=gobject-2.0
 endif
 
-GObject.gir: GLib.gir $(G_IR_SCANNER) $(G_IR_SCANNER_FILES) Makefile
+GObject-2.0.gir: GLib-2.0.gir $(G_IR_SCANNER) $(G_IR_SCANNER_FILES) Makefile
        PYTHONPATH=$(top_builddir):$$PYTHONPATH $(G_IR_SCANNER) \
-           -v --namespace GObject \
+           -v --namespace GObject --nsversion=2.0 \
            --noclosure \
            --output $@ \
            --strip-prefix=g \
-            --include=$(top_builddir)/gir/GLib.gir \
+            --include=$(top_builddir)/gir/GLib-2.0.gir \
            --library=$(GOBJECT_LIBRARY) \
            -I$(GOBJECT_INCLUDEDIR) \
            -I$(GOBJECT_LIBDIR)/glib-2.0/include \
            -DGOBJECT_COMPILATION \
             --pkg glib-2.0 \
            $(GLIB_INCLUDEDIR)/gobject/*.h
-BUILT_SOURCES += GObject.gir
+BUILT_SOURCES += GObject-2.0.gir
 
 # gmodule
 GMODULE_INCLUDEDIR=`pkg-config --variable=includedir gmodule-2.0`/glib-2.0
@@ -67,19 +67,19 @@ else
 GMODULE_LIBRARY=gmodule-2.0
 endif
 
-GModule.gir: GLib.gir $(G_IR_SCANNER) $(G_IR_SCANNER_FILES)
+GModule-2.0.gir: GLib-2.0.gir $(G_IR_SCANNER) $(G_IR_SCANNER_FILES)
        PYTHONPATH=$(top_builddir):$$PYTHONPATH $(G_IR_SCANNER) \
-           -v --namespace GModule \
+           -v --namespace GModule --nsversion=2.0 \
            --noclosure \
            --output $@ \
            --strip-prefix=g \
-            --include=$(top_builddir)/gir/GLib.gir \
+            --include=$(top_builddir)/gir/GLib-2.0.gir \
            --library=$(GMODULE_LIBRARY) \
            -I$(GMODULE_INCLUDEDIR) \
            -I$(GMODULE_LIBDIR)/glib-2.0/include \
             --pkg glib-2.0 \
            $(GLIB_INCLUDEDIR)/gmodule.h
-BUILT_SOURCES += GModule.gir
+BUILT_SOURCES += GModule-2.0.gir
 
 # gio
 GIO_INCLUDEDIR=`pkg-config --variable=includedir gio-2.0`/glib-2.0
@@ -91,13 +91,13 @@ else
 GIO_LIBRARY=gio-2.0
 endif
 
-Gio.gir: GObject.gir $(G_IR_SCANNER) $(G_IR_SCANNER_FILES) Makefile
+Gio-2.0.gir: GObject-2.0.gir $(G_IR_SCANNER) $(G_IR_SCANNER_FILES) Makefile
        PYTHONPATH=$(top_builddir):$$PYTHONPATH $(G_IR_SCANNER) \
-           -v --namespace Gio \
+           -v --namespace Gio --nsversion=2.0 \
            --noclosure \
            --output $@ \
            --strip-prefix=g \
-            --include=$(top_builddir)/gir/GObject.gir \
+            --include=$(top_builddir)/gir/GObject-2.0.gir \
            --library=$(GIO_LIBRARY) \
            -I$(GIO_INCLUDEDIR) \
            -I$(GIO_LIBDIR)/glib-2.0/include \
@@ -105,7 +105,7 @@ Gio.gir: GObject.gir $(G_IR_SCANNER) $(G_IR_SCANNER_FILES) Makefile
             --pkg glib-2.0 \
             --pkg gobject-2.0 \
            $(GLIB_INCLUDEDIR)/gio/*.h
-BUILT_SOURCES += Gio.gir
+BUILT_SOURCES += Gio-2.0.gir
 CLEANFILES = $(BUILT_SOURCES)
 
 girdir=$(datadir)/gir
@@ -115,5 +115,5 @@ dist_gir_DATA = $(BUILT_SOURCES)
        $(DEBUG) $(top_builddir)/tools/g-ir-compiler$(EXEEXT) --includedir=. $(G_IR_COMPILER_OPTS) $< -o $@
 
 typelibsdir = $(datadir)/girepository
-typelibs_DATA = GLib.typelib GModule.typelib GObject.typelib Gio.typelib
+typelibs_DATA = GLib-2.0.typelib GModule-2.0.typelib GObject-2.0.typelib Gio-2.0.typelib
 CLEANFILES += $(typelibs_DATA)
index 7cb81f3..5bbad53 100644 (file)
@@ -2,6 +2,8 @@
 /* GObject introspection: Repository implementation
  *
  * Copyright (C) 2005 Matthias Clasen
+ * Copyright (C) 2008 Colin Walters <walters@verbum.org>
+ * Copyright (C) 2008 Red Hat, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -35,7 +37,7 @@ static GSList *search_path = NULL;
 struct _GIRepositoryPrivate 
 {
   GHashTable *typelibs; /* (string) namespace -> GTypelib */
-  GHashTable *lazy_typelibs; /* (string) namespace -> GTypelib */
+  GHashTable *lazy_typelibs; /* (string) namespace-version -> GTypelib */
 };
 
 G_DEFINE_TYPE (GIRepository, g_irepository, G_TYPE_OBJECT);
@@ -59,6 +61,7 @@ g_irepository_finalize (GObject *object)
   GIRepository *repository = G_IREPOSITORY (object);
 
   g_hash_table_destroy (repository->priv->typelibs);
+  g_hash_table_destroy (repository->priv->lazy_typelibs);
   
   (* G_OBJECT_CLASS (g_irepository_parent_class)->finalize) (G_OBJECT (repository));
 }
@@ -147,18 +150,51 @@ get_repository (GIRepository *repository)
 }
 
 static GTypelib *
+check_version_conflict (GTypelib *typelib, 
+                       const gchar *namespace,
+                       const gchar *expected_version,
+                       char       **version_conflict)
+{
+  Header *header;
+  const char *loaded_version;
+
+  if (expected_version == NULL)
+    {
+      if (version_conflict)
+       *version_conflict = NULL;
+      return typelib;
+    }
+  
+  header = (Header*)typelib->data;
+  loaded_version = g_typelib_get_string (typelib, header->nsversion);
+  g_assert (loaded_version != NULL);
+  
+  if (strcmp (expected_version, loaded_version) != 0)
+    {
+      if (version_conflict)
+       *version_conflict = (char*)loaded_version;
+      return NULL;
+    }
+  if (version_conflict)
+    *version_conflict = NULL;
+  return typelib;
+}
+
+static GTypelib *
 get_registered_status (GIRepository *repository,
                       const char   *namespace,
+                      const char   *version,
                       gboolean      allow_lazy,
-                      gboolean     *lazy_status)
+                      gboolean     *lazy_status,
+                      char        **version_conflict)
 {
   GTypelib *typelib;
   repository = get_repository (repository);
   if (lazy_status)
     *lazy_status = FALSE;
   typelib = g_hash_table_lookup (repository->priv->typelibs, namespace);
-  if (typelib)
-    return typelib;
+  if (typelib) 
+    return check_version_conflict (typelib, namespace, version, version_conflict);
   typelib = g_hash_table_lookup (repository->priv->lazy_typelibs, namespace);
   if (!typelib)
     return NULL;
@@ -166,14 +202,15 @@ get_registered_status (GIRepository *repository,
     *lazy_status = TRUE;
   if (!allow_lazy)
     return NULL;
-  return typelib;
+  return check_version_conflict (typelib, namespace, version, version_conflict);
 }
 
 static GTypelib *
 get_registered (GIRepository *repository,
-               const char   *namespace)
+               const char   *namespace,
+               const char   *version)
 {
-  return get_registered_status (repository, namespace, TRUE, NULL);
+  return get_registered_status (repository, namespace, version, TRUE, NULL, NULL);
 }
 
 static gboolean
@@ -192,19 +229,28 @@ load_dependencies_recurse (GIRepository *repository,
       for (i = 0; dependencies[i]; i++)
        {
          char *dependency = dependencies[i];
+         const char *last_dash;
+         char *dependency_namespace;
+         const char *dependency_version;
+
+         last_dash = strrchr (dependency, '-');
+         dependency_namespace = g_strndup (dependency, last_dash - dependency);
+         dependency_version = last_dash+1;
              
-         if (!g_irepository_require (repository, dependency
+         if (!g_irepository_require (repository, dependency_namespace, dependency_version,
                                      0, error))
            {
+             g_free (dependency_namespace);
              g_strfreev (dependencies);
              return FALSE;
            }
+         g_free (dependency_namespace);
        }
       g_strfreev (dependencies);
     }
   return TRUE;
 }
-                          
+
 static const char *
 register_internal (GIRepository *repository,
                   const char   *source,
@@ -214,6 +260,7 @@ register_internal (GIRepository *repository,
 {
   Header *header;
   const gchar *namespace;
+  const gchar *version;
   gboolean was_loaded;
   gboolean currently_lazy;
 
@@ -224,6 +271,7 @@ register_internal (GIRepository *repository,
   g_return_val_if_fail (header != NULL, FALSE);
 
   namespace = g_typelib_get_string (typelib, header->namespace);
+  version = g_typelib_get_string (typelib, header->nsversion);
 
   if (lazy)
     {
@@ -268,7 +316,7 @@ g_irepository_get_dependencies (GIRepository *repository,
 
   repository = get_repository (repository);
 
-  typelib = get_registered (repository, namespace);
+  typelib = get_registered (repository, namespace, NULL);
   g_return_val_if_fail (typelib != NULL, NULL);
 
   return get_typelib_dependencies (typelib);
@@ -282,28 +330,74 @@ g_irepository_load_typelib (GIRepository *repository,
 {
   Header *header;
   const char *namespace;
+  const char *nsversion;
   gboolean allow_lazy = flags & G_IREPOSITORY_LOAD_FLAG_LAZY;
   gboolean is_lazy;
+  char *version_conflict;
 
   repository = get_repository (repository);
 
   header = (Header *) typelib->data;
   namespace = g_typelib_get_string (typelib, header->namespace);
+  nsversion = g_typelib_get_string (typelib, header->nsversion);
 
-  if (get_registered_status (repository, namespace, allow_lazy, &is_lazy))
-    return namespace;
+  if (get_registered_status (repository, namespace, nsversion, allow_lazy, 
+                            &is_lazy, &version_conflict))
+    {
+      if (version_conflict != NULL)
+       {
+         g_set_error (error, G_IREPOSITORY_ERROR,
+                      G_IREPOSITORY_ERROR_NAMESPACE_VERSION_CONFLICT,
+                      "Attempting to load namespace '%s', version '%s', but '%s' is already loaded",
+                      namespace, nsversion, version_conflict);
+         return NULL;
+       }
+      return namespace;
+    }
   return register_internal (repository, "<builtin>", 
                            allow_lazy, typelib, error);
 }
 
+/**
+ * g_irepository_is_registered
+ * @repository: A #GIRepository, may be %NULL for the default
+ * @namespace: Namespace of interest
+ * @version: <allow-none>: Required version, may be %NULL for latest
+ *
+ * Check whether a particular namespace (and optionally, a specific
+ * version thereof) is currently loaded.  This function is likely to
+ * only be useful in unusual circumstances; in order to act upon
+ * metadata in the namespace, you should call #g_irepository_require
+ * instead which will ensure the namespace is loaded, and return as
+ * quickly as this function will if it has already been loaded.
+ * 
+ * Returns: %TRUE if namespace-version is loaded, %FALSE otherwise
+ */
 gboolean
 g_irepository_is_registered (GIRepository *repository, 
-                            const gchar *namespace)
+                            const gchar *namespace,
+                            const gchar *version)
 {
   repository = get_repository (repository);
-  return get_registered (repository, namespace) != NULL;
+  return get_registered (repository, namespace, version) != NULL;
 }
 
+/**
+ * g_irepository_get_default
+ *
+ * Returns the singleton process-global default #GIRepository.  It is
+ * not currently supported to have multiple repositories in a
+ * particular process, but this function is provided in the unlikely
+ * eventuality that it would become possible, and as a convenience for
+ * higher level language bindings to conform to the GObject method
+ * call conventions.
+
+ * All methods on #GIRepository also accept %NULL as an instance
+ * parameter to mean this default repository, which is usually more
+ * convenient for C.
+ * 
+ * Returns: The global singleton #GIRepository 
+ */
 GIRepository * 
 g_irepository_get_default (void)
 {
@@ -321,30 +415,33 @@ count_interfaces (gpointer key,
   *n_interfaces += ((Header *)typelib)->n_local_entries;
 }
 
+/**
+ * g_irepository_get_n_infos
+ * @repository: A #GIRepository, may be %NULL for the default
+ * @namespace: Namespace to inspect
+ *
+ * This function returns the number of metadata entries in
+ * given namespace @namespace.  The namespace must have
+ * already been loaded before calling this function.
+ *
+ * Returns: number of metadata entries
+ */
 gint                   
 g_irepository_get_n_infos (GIRepository *repository,
                           const gchar  *namespace)
 {
+  GTypelib *typelib;
   gint n_interfaces = 0;
 
+  g_return_val_if_fail (namespace != NULL, -1);
+
   repository = get_repository (repository);
   
-  if (namespace)
-    {
-      GTypelib *typelib;
+  typelib = get_registered (repository, namespace, NULL);
 
-      typelib = get_registered (repository, namespace);
+  g_return_val_if_fail (typelib != NULL, -1);
 
-      if (typelib)
-       n_interfaces = ((Header *)typelib->data)->n_local_entries;
-    }
-  else
-    {
-      g_hash_table_foreach (repository->priv->typelibs, 
-                           count_interfaces, &n_interfaces);
-      g_hash_table_foreach (repository->priv->lazy_typelibs, 
-                           count_interfaces, &n_interfaces);
-    }
+  n_interfaces = ((Header *)typelib->data)->n_local_entries;
 
   return n_interfaces;
 }
@@ -421,12 +518,27 @@ find_interface (gpointer key,
     }
 }
 
+/**
+ * g_irepository_get_info
+ * @repository: A #GIRepository, may be %NULL for the default
+ * @namespace: Namespace to inspect
+ * @index: Offset into namespace metadata for entry
+ *
+ * This function returns a particular metadata entry in the
+ * given namespace @namespace.  The namespace must have
+ * already been loaded before calling this function.
+ *
+ * Returns: #GIBaseInfo containing metadata
+ */
 GIBaseInfo * 
 g_irepository_get_info (GIRepository *repository,
                        const gchar  *namespace,
                        gint          index)
 {
   IfaceData data;
+  GTypelib *typelib;
+
+  g_return_val_if_fail (namespace != NULL, NULL);
 
   repository = get_repository (repository);
 
@@ -435,24 +547,29 @@ g_irepository_get_info (GIRepository *repository,
   data.index = index + 1;
   data.iface = NULL;
 
-  if (namespace)
-    {
-      GTypelib *typelib;
-      
-      typelib = get_registered (repository, namespace);
-      
-      if (typelib)
-       find_interface ((void *)namespace, typelib, &data);
-    }
-  else
-    {
-      g_hash_table_foreach (repository->priv->typelibs, find_interface, &data);
-      g_hash_table_foreach (repository->priv->lazy_typelibs, find_interface, &data);
-    }
+  typelib = get_registered (repository, namespace, NULL);
+  
+  g_return_val_if_fail (typelib != NULL, NULL);
+
+  find_interface ((void *)namespace, typelib, &data);
 
   return data.iface;  
 }
 
+/**
+ * g_irepository_find_by_gtype
+ * @repository: A #GIRepository, may be %NULL for the default
+ * @type: GType to search for
+ *
+ * Searches all loaded namespaces for a particular #GType.  Note that
+ * in order to locate the metadata, the namespace corresponding to
+ * the type must first have been loaded.  There is currently no
+ * mechanism for determining the namespace which corresponds to an
+ * arbitrary GType - thus, this function will function most reliably
+ * when you have expect the GType to be from a known namespace.
+ *
+ * Returns: #GIBaseInfo representing metadata about @type, or %NULL
+ */
 GIBaseInfo * 
 g_irepository_find_by_gtype (GIRepository *repository,
                             GType         type)
@@ -475,12 +592,14 @@ g_irepository_find_by_gtype (GIRepository *repository,
 /**
  * g_irepository_find_by_name
  * @repository: A #GIRepository, may be %NULL for the default
- * @namespace: Namespace to search in, may be %NULL for all
- * @name: Name to find
+ * @namespace: Namespace which will be searched
+ * @name: Entry name to find
+ *
+ * Searches for a particular entry in a namespace.  Before calling
+ * this function for a particular namespace, you must call
+ * #g_irepository_require once to load the namespace, or otherwise
+ * ensure the namespace has already been loaded.
  *
- * Searches for a particular name in one or all namespaces.
- * See #g_irepository_require to load metadata for namespaces.
-
  * Returns: #GIBaseInfo representing metadata about @name, or %NULL
  */
 GIBaseInfo * 
@@ -489,6 +608,9 @@ g_irepository_find_by_name (GIRepository *repository,
                            const gchar  *name)
 {
   IfaceData data;
+  GTypelib *typelib;
+
+  g_return_val_if_fail (namespace != NULL, NULL);
 
   repository = get_repository (repository);
 
@@ -497,20 +619,11 @@ g_irepository_find_by_name (GIRepository *repository,
   data.index = -1;
   data.iface = NULL;
 
-  if (namespace)
-    {
-      GTypelib *typelib;
-      
-      typelib = get_registered (repository, namespace);
-      
-      if (typelib)
-       find_interface ((void *)namespace, typelib, &data);
-    }
-  else
-    {
-      g_hash_table_foreach (repository->priv->typelibs, find_interface, &data);
-      g_hash_table_foreach (repository->priv->lazy_typelibs, find_interface, &data);
-    }
+  typelib = get_registered (repository, namespace, NULL);
+  
+  g_return_val_if_fail (typelib != NULL, NULL);
+
+  find_interface ((void *)namespace, typelib, &data);
 
   return data.iface;
 }
@@ -529,14 +642,12 @@ collect_namespaces (gpointer key,
  * g_irepository_get_namespaces
  * @repository: A #GIRepository, may be %NULL for the default
  *
- * Return the list of currently known namespaces.  Normally
- * if you want a particular namespace, you should call 
- * #g_irepository_require to load it in.
-
- * Returns: List of namespaces
+ * Return the list of currently loaded namespaces.
+ *
+ * Returns: <utf8,transfer>: List of namespaces
  */
 gchar ** 
-g_irepository_get_namespaces (GIRepository *repository)
+g_irepository_get_loaded_namespaces (GIRepository *repository)
 {
   GList *l, *list = NULL;
   gchar **names;
@@ -556,18 +667,68 @@ g_irepository_get_namespaces (GIRepository *repository)
   return names;
 }
 
+/**
+ * g_irepository_get_version
+ * @repository: A #GIRepository, may be %NULL for the default
+ * @namespace: Namespace to inspect
+ *
+ * This function returns the loaded version associated with the given
+ * namespace @namespace.
+ *
+ * Note: The namespace must have already been loaded using a function
+ * such as #g_irepository_require before calling this function.
+ *
+ * Returns: Loaded version
+ */
+const gchar *
+g_irepository_get_version (GIRepository *repository,
+                          const gchar  *namespace)
+{
+  GTypelib *typelib;
+  Header *header;
+
+  g_return_val_if_fail (namespace != NULL, NULL);
+
+  repository = get_repository (repository);
+
+  typelib = get_registered (repository, namespace, NULL);
+
+  g_return_val_if_fail (typelib != NULL, NULL);
+
+  header = (Header *) typelib->data;
+  return g_typelib_get_string (typelib, header->nsversion);
+}
+
+/**
+ * g_irepository_get_shared_library
+ * @repository: A #GIRepository, may be %NULL for the default
+ * @namespace: Namespace to inspect
+ *
+ * This function returns the full path to the shared C library
+ * associated with the given namespace @namespace. There may be no
+ * shared library path associated, in which case this function will
+ * return %NULL.
+ *
+ * Note: The namespace must have already been loaded using a function
+ * such as #g_irepository_require before calling this function.
+ *
+ * Returns: Full path to shared library, or %NULL if none associated
+ */
 const gchar *
 g_irepository_get_shared_library (GIRepository *repository,
-                                   const gchar  *namespace)
+                                 const gchar  *namespace)
 {
   GTypelib *typelib;
   Header *header;
 
+  g_return_val_if_fail (namespace != NULL, NULL);
+
   repository = get_repository (repository);
 
-  typelib = get_registered (repository, namespace);
-  if (!typelib)
-    return NULL;
+  typelib = get_registered (repository, namespace, NULL);
+
+  g_return_val_if_fail (typelib != NULL, NULL);
+
   header = (Header *) typelib->data;
   if (header->shared_library)
     return g_typelib_get_string (typelib, header->shared_library);
@@ -579,13 +740,14 @@ g_irepository_get_shared_library (GIRepository *repository,
  * g_irepository_get_typelib_path
  * @repository: Repository, may be %NULL for the default
  * @namespace: GI namespace to use, e.g. "Gtk"
+ * @version: <allow-none>: Version of namespace to use, e.g. "0.8", may be %NULL
  *
  * If namespace @namespace is loaded, return the full path to the
  * .typelib file it was loaded from.  If the typelib for 
  * namespace @namespace was included in a shared library, return
  * the special string "<builtin>".
  *
- * Returns: Filesystem path (or <builtin>) if successful, %NULL otherwise
+ * Returns: Filesystem path (or <builtin>) if successful, %NULL if namespace is not loaded
  */
 
 const gchar * 
@@ -607,94 +769,323 @@ g_irepository_get_typelib_path (GIRepository *repository,
   return ((char*)orig_key) + strlen ((char *) orig_key) + 1;
 }
 
+/* This simple search function looks for a specified namespace-version;
+   it's faster than the full directory listing required for latest version. */
+static GMappedFile *
+find_namespace_version (const gchar  *namespace,
+                       const gchar  *version,
+                       gchar       **path_ret)
+{
+  GSList *ldir;
+  GError *error = NULL;
+  GMappedFile *mfile = NULL;
+  char *fname;
+  fname = g_strdup_printf ("%s-%s.typelib", namespace, version);
+
+  for (ldir = search_path; ldir; ldir = ldir->next)
+    {
+      Header *header;
+      char *path = g_build_filename (ldir->data, fname, NULL);
+      
+      mfile = g_mapped_file_new (path, FALSE, &error);
+      if (error)
+       {
+         g_free (path);
+         g_clear_error (&error);
+         continue;
+       }
+      *path_ret = path;
+      break;
+    }
+  g_free (fname);
+  return mfile;
+}
+
+static gboolean
+parse_version (const char *version,
+              int *major,
+              int *minor)
+{
+  const char *dot;
+  const char *end;
+
+  *major = strtol (version, &end, 10);
+  dot = strchr (version, '.');
+  if (dot == NULL)
+    {
+      *minor = 0;
+      return TRUE;
+    }
+  if (dot != end)
+    return FALSE;
+  *minor = strtol (dot+1, &end, 10);
+  if (end != (version + strlen (version)))
+    return FALSE;
+  return TRUE;
+}
+
+static int
+compare_version (const char *v1,
+                const char *v2)
+{
+  gboolean err;
+  int v1_major, v1_minor;
+  int v2_major, v2_minor;
+  
+  err = parse_version (v1, &v1_major, &v1_minor);
+  g_assert (!err);
+
+  err = parse_version (v2, &v2_major, &v2_minor);
+  g_assert (!err);
+
+  if (v1_major > v2_major)
+    return 1;
+  else if (v2_major > v1_major)
+    return -1;
+  else if (v1_minor > v2_minor)
+    return 1;
+  else if (v2_minor > v1_minor)
+    return -1;
+  return 0;
+}
+
+struct NamespaceVersionCandidadate
+{
+  GMappedFile *mfile;
+  char *path;
+  char *version;
+};
+
+static int
+compare_candidate_reverse (struct NamespaceVersionCandidadate *c1,
+                          struct NamespaceVersionCandidadate *c2)
+{
+  int result = compare_version (c1->version, c2->version);
+  if (result > 0)
+    return -1;
+  else if (result < 0)
+    return 1;
+  else
+    return 0;
+}
+
+static void
+free_candidate (struct NamespaceVersionCandidadate *candidate)
+{
+  g_mapped_file_free (candidate->mfile);
+  g_free (candidate->path);
+  g_free (candidate->version);
+  g_free (candidate);
+}
+
+static GMappedFile *
+find_namespace_latest (const gchar  *namespace,
+                      gchar       **version_ret,
+                      gchar       **path_ret)
+{
+  GSList *ldir;
+  GError *error = NULL;
+  char *namespace_dash;
+  char *namespace_typelib;
+  GSList *candidates = NULL;
+  GMappedFile *result = NULL;
+
+  *version_ret = NULL;
+  *path_ret = NULL;
+
+  namespace_dash = g_strdup_printf ("%s-", namespace);
+  namespace_typelib = g_strdup_printf ("%s.typelib", namespace);
+  for (ldir = search_path; ldir; ldir = ldir->next)
+    {
+      GDir *dir;
+      const char *dirname;
+      const char *entry;
+
+      dirname = (const char*)ldir->data;
+      dir = g_dir_open (dirname, 0, NULL);
+      if (dir == NULL)
+       continue;
+      while ((entry = g_dir_read_name (dir)) != NULL) 
+       {
+         GMappedFile *mfile;
+         char *path, *version;
+         struct NamespaceVersionCandidadate *candidate;
+
+         if (!g_str_has_suffix (entry, ".typelib"))
+           continue;
+         
+         if (g_str_has_prefix (entry, namespace_dash))
+           {
+             const char *last_dash;
+             const char *name_end;
+             int major, minor;
+
+             name_end = strrchr (entry, '.');
+             last_dash = strrchr (entry, '-');
+             version = g_strndup (last_dash+1, name_end-(last_dash+1));
+             if (!parse_version (version, &major, &minor))
+               continue;
+           }
+         else
+           continue;
+
+         path = g_build_filename (dirname, entry, NULL);
+         mfile = g_mapped_file_new (path, FALSE, &error);
+         if (mfile == NULL)
+           {
+             g_free (path);
+             g_free (version);
+             g_clear_error (&error);
+             continue;
+           }
+         candidate = g_new0 (struct NamespaceVersionCandidadate, 1);
+         candidate->mfile = mfile;
+         candidate->path = path;
+         candidate->version = version;
+         candidates = g_slist_prepend (candidates, candidate);
+       }
+      g_dir_close (dir);
+    }
+
+  if (candidates != NULL)
+    {
+      struct NamespaceVersionCandidadate *elected;
+      candidates = g_slist_sort (candidates, (GCompareFunc) compare_candidate_reverse);
+      
+      elected = (struct NamespaceVersionCandidadate *) candidates->data;
+      /* Remove the elected one so we don't try to free it */
+      candidates = g_slist_delete_link (candidates, candidates);
+      
+      result = elected->mfile;
+      *path_ret = elected->path;
+      *version_ret = elected->version;
+      g_slist_foreach (candidates, (GFunc) free_candidate, NULL);
+      g_slist_free (candidates);
+    }  
+
+  g_free (namespace_dash);
+  g_free (namespace_typelib);
+  return result;
+}
+
 /**
  * g_irepository_require
- * @repository: Repository, may be %NULL for the default
+ * @repository: <allow-none>: Repository, may be %NULL for the default
  * @namespace: GI namespace to use, e.g. "Gtk"
+ * @version: <allow-none>: Version of namespace, may be %NULL for latest
  * @flags: Set of %GIRepositoryLoadFlags, may be %0
  * @error: a #GError.
  *
- * Force the namespace @namespace to be loaded if it isn't
- * already.  If @namespace is not loaded, this function will
- * search for a ".typelib" file using the repository search 
- * path.
+ * Force the namespace @namespace to be loaded if it isn't already.
+ * If @namespace is not loaded, this function will search for a
+ * ".typelib" file using the repository search path.  In addition, a
+ * version @version of namespace may be specified.  If @version is
+ * not specified, the latest will be used.
  *
  * Returns: %TRUE if successful, %NULL otherwise
  */
 gboolean
 g_irepository_require (GIRepository  *repository,
                       const gchar   *namespace,
+                      const gchar   *version,
                       GIRepositoryLoadFlags flags,
                       GError       **error)
 {
-  GSList *ldir;
   const char *dir;
-  gchar *fname, *full_path;
   GMappedFile *mfile;
+  gboolean ret = FALSE;
   GError *error1 = NULL;
+  Header *header;
   GTypelib *typelib = NULL;
-  const gchar *typelib_namespace, *shlib_fname;
+  const gchar *typelib_namespace, *typelib_version, *shlib_fname;
   GModule *module;
   guint32 shlib;
-  gboolean allow_lazy = flags & G_IREPOSITORY_LOAD_FLAG_LAZY;
+  gboolean allow_lazy = (flags & G_IREPOSITORY_LOAD_FLAG_LAZY) > 0;
   gboolean is_lazy;
+  char *version_conflict = NULL;
+  char *path = NULL;
+  char *tmp_version = NULL;
+
+  g_return_val_if_fail (namespace != NULL, FALSE);
 
   repository = get_repository (repository);
 
-  if (get_registered_status (repository, namespace, allow_lazy, &is_lazy))
+  if (get_registered_status (repository, namespace, version, allow_lazy, 
+                            &is_lazy, &version_conflict))
     return TRUE;
 
-  fname = g_strconcat (namespace, ".typelib", NULL);
-
-  for (ldir = search_path; ldir; ldir = ldir->next)
+  if (version_conflict != NULL)
     {
-      Header *header;
-      
-      full_path = g_build_filename (ldir->data, fname, NULL);
-      mfile = g_mapped_file_new (full_path, FALSE, &error1);
-      if (error1)
-       {
-         g_clear_error (&error1);
-         continue;
-       }
-
-      typelib = g_typelib_new_from_mapped_file (mfile);
-      header = (Header *) typelib->data;
-      typelib_namespace = g_typelib_get_string (typelib, header->namespace);
+      g_set_error (error, G_IREPOSITORY_ERROR,
+                  G_IREPOSITORY_ERROR_NAMESPACE_VERSION_CONFLICT,
+                  "Requiring namespace '%s' version '%s', but '%s' is already loaded",
+                  namespace, version, version_conflict);
+      return FALSE;
+    }
 
-      if (strcmp (typelib_namespace, namespace) != 0)
-       {
-         g_set_error (error, G_IREPOSITORY_ERROR,
-                      G_IREPOSITORY_ERROR_NAMESPACE_MISMATCH,
-                      "Typelib file %s for namespace '%s' contains "
-                      "namespace '%s' which doesn't match the file name",
-                      full_path, namespace, typelib_namespace);
-         g_free (full_path);
-         return FALSE; 
-       }
-      break;
-  }
+  if (version != NULL)
+    {
+      mfile = find_namespace_version (namespace, version, &path);
+      tmp_version = g_strdup (version);
+    }
+  else
+    {
+      mfile = find_namespace_latest (namespace, &tmp_version, &path);
+    }
+  
+  if (mfile == NULL)
+    {
+      const char *error_fmt;
+      if (version != NULL)
+       g_set_error (error, G_IREPOSITORY_ERROR,
+                    G_IREPOSITORY_ERROR_TYPELIB_NOT_FOUND,
+                    "Typelib file %s for namespace '%s', version '%s' not found",
+                    namespace, version);
+      else
+       g_set_error (error, G_IREPOSITORY_ERROR,
+                    G_IREPOSITORY_ERROR_TYPELIB_NOT_FOUND,
+                    "Typelib file for namespace '%s' (any version) not found",
+                    namespace);
+      goto out;
+    }
 
-  if (typelib == NULL)
+  typelib = g_typelib_new_from_mapped_file (mfile);
+  header = (Header *) typelib->data;
+  typelib_namespace = g_typelib_get_string (typelib, header->namespace);
+  typelib_version = g_typelib_get_string (typelib, header->nsversion);
+  
+  if (strcmp (typelib_namespace, namespace) != 0)
     {
       g_set_error (error, G_IREPOSITORY_ERROR,
-                  G_IREPOSITORY_ERROR_TYPELIB_NOT_FOUND,
-                  "Typelib file for namespace '%s' was not found in search"
-                  " path or could not be openened", namespace);
-      g_free (full_path);
-      return FALSE;
+                  G_IREPOSITORY_ERROR_NAMESPACE_MISMATCH,
+                  "Typelib file %s for namespace '%s' contains "
+                  "namespace '%s' which doesn't match the file name",
+                  path, namespace, typelib_namespace);
+      goto out;
+    }
+  if (version != NULL && strcmp (typelib_version, version) != 0)
+    {
+      g_set_error (error, G_IREPOSITORY_ERROR,
+                  G_IREPOSITORY_ERROR_NAMESPACE_MISMATCH,
+                  "Typelib file %s for namespace '%s' contains "
+                  "version '%s' which doesn't match the expected version '%s'",
+                  path, namespace, typelib_version, version);
+      goto out;
     }
 
-  g_free (fname);
-  if (!register_internal (repository, full_path, allow_lazy, 
+  if (!register_internal (repository, path, allow_lazy, 
                          typelib, error))
     {
       g_typelib_free (typelib);
-      g_free (full_path);
-      return FALSE;
+      goto out;
     }
-  g_free (full_path);
-  return TRUE; 
+  ret = TRUE;
+ out:
+  g_free (tmp_version);
+  g_free (path);
+  return ret; 
 }
 
 
index 28848d5..fb95a4f 100644 (file)
@@ -82,17 +82,19 @@ const char *  g_irepository_load_typelib  (GIRepository *repository,
                                           GIRepositoryLoadFlags flags,
                                           GError      **error);
 gboolean      g_irepository_is_registered (GIRepository *repository, 
-                                          const gchar  *namespace);
+                                          const gchar  *namespace,
+                                          const gchar  *version);
 GIBaseInfo *  g_irepository_find_by_name  (GIRepository *repository,
                                           const gchar  *namespace,
                                           const gchar  *name);
 gboolean      g_irepository_require       (GIRepository *repository,
-                                          const char   *namespace,
+                                          const gchar  *namespace,
+                                          const gchar  *version,           
                                           GIRepositoryLoadFlags flags,
                                           GError      **error);
 gchar      ** g_irepository_get_dependencies (GIRepository *repository,
-                                             const char *namespace);
-gchar      ** g_irepository_get_namespaces (GIRepository *repository);
+                                             const gchar  *namespace);
+gchar      ** g_irepository_get_loaded_namespaces (GIRepository *repository);
 GIBaseInfo *  g_irepository_find_by_gtype (GIRepository *repository,
                                           GType         gtype);
 gint          g_irepository_get_n_infos   (GIRepository *repository,
@@ -104,6 +106,9 @@ const gchar * g_irepository_get_typelib_path   (GIRepository *repository,
                                                const gchar  *namespace);
 const gchar * g_irepository_get_shared_library (GIRepository *repository,
                                                const gchar  *namespace);
+const gchar * g_irepository_get_version (GIRepository *repository,
+                                        const gchar  *namespace);
+
 /* Typelib */
 
 GTypelib *   g_typelib_new_from_memory       (guchar       *memory,
@@ -112,6 +117,7 @@ GTypelib *   g_typelib_new_from_const_memory (const guchar *memory,
                                                 gsize         len);
 GTypelib *   g_typelib_new_from_mapped_file  (GMappedFile  *mfile);
 void          g_typelib_free                  (GTypelib    *typelib);
+
 gboolean      g_typelib_symbol                (GTypelib    *typelib,
                                                const gchar *symbol_name,
                                                gpointer    *symbol);
@@ -121,6 +127,7 @@ typedef enum
 {
   G_IREPOSITORY_ERROR_TYPELIB_NOT_FOUND,
   G_IREPOSITORY_ERROR_NAMESPACE_MISMATCH,
+  G_IREPOSITORY_ERROR_NAMESPACE_VERSION_CONFLICT,
   G_IREPOSITORY_ERROR_LIBRARY_NOT_FOUND
 } GIRepositoryError;
 
index 65ee392..ad0ecf5 100644 (file)
 
 
 GIrModule *
-g_ir_module_new (const gchar *name, const gchar *shared_library)
+g_ir_module_new (const gchar *name, 
+                const gchar *version,
+                const gchar *shared_library)
 {
   GIrModule *module;
   
   module = g_new0 (GIrModule, 1);
 
   module->name = g_strdup (name);
+  module->version = g_strdup (version);
   if (shared_library)
       module->shared_library = g_strdup (shared_library);
   else
@@ -156,6 +159,7 @@ g_ir_module_build_typelib (GIrModule  *module,
     header->dependencies = 0;
   header->size = 0; /* filled in later */
   header->namespace = write_string (module->name, strings, data, &header_size);
+  header->nsversion = write_string (module->version, strings, data, &header_size);
   header->shared_library = (module->shared_library?
                              write_string (module->shared_library, strings, data, &header_size)
                              : 0);
index a4511e3..5b63c36 100644 (file)
@@ -32,12 +32,14 @@ typedef struct _GIrModule GIrModule;
 struct _GIrModule
 { 
   gchar *name;
+  gchar *version;
   gchar *shared_library;
   GList *dependencies;
   GList *entries;
 };
 
 GIrModule *g_ir_module_new            (const gchar *name,
+                                      const gchar *nsversion,
                                       const gchar *module_filename);
 void       g_ir_module_free           (GIrModule  *module);
 
index d4cc34a..5b66fa6 100644 (file)
@@ -2050,7 +2050,7 @@ parse_include (GMarkupParseContext *context,
       g_set_error (error,
                   G_MARKUP_ERROR,
                   G_MARKUP_ERROR_INVALID_CONTENT,
-                  "Could not find GIR file '%s'; check XDG_DATA_DIRS or use --includedir",
+                  "Could not find GIR file '%s.gir'; check XDG_DATA_DIRS or use --includedir",
                   name);
       return FALSE;
     }
@@ -2260,16 +2260,19 @@ start_element_handler (GMarkupParseContext *context,
     case 'n':
       if (strcmp (element_name, "namespace") == 0 && ctx->state == STATE_REPOSITORY)
        {
-         const gchar *name, *shared_library;
+         const gchar *name, *version, *shared_library;
          
          name = find_attribute ("name", attribute_names, attribute_values);
+         version = find_attribute ("version", attribute_names, attribute_values);
          shared_library = find_attribute ("shared-library", attribute_names, attribute_values);
 
          if (name == NULL)
            MISSING_ATTRIBUTE (context, error, element_name, "name");
+         else if (version == NULL)
+           MISSING_ATTRIBUTE (context, error, element_name, "version");
          else
            {
-             ctx->current_module = g_ir_module_new (name, shared_library);
+             ctx->current_module = g_ir_module_new (name, version, shared_library);
              ctx->modules = g_list_append (ctx->modules, ctx->current_module);
              ctx->current_module->dependencies = ctx->dependencies;
 
index e3f52aa..7a7a31f 100644 (file)
@@ -153,7 +153,7 @@ g_typelib_check_sanity (void)
       size_check_ok = FALSE; \
     }
   
-  CHECK_SIZE (Header, 104);
+  CHECK_SIZE (Header, 108);
   CHECK_SIZE (DirEntry, 12);
   CHECK_SIZE (SimpleTypeBlob, 4);
   CHECK_SIZE (ArgBlob, 12);
index 823e8a2..16182d5 100644 (file)
@@ -61,6 +61,7 @@ typedef struct
 
   guint32 size;
   guint32 namespace;
+  guint32 nsversion;
   guint32 shared_library;
 
   guint16 entry_blob_size;
index b5608ea..ec9c177 100644 (file)
@@ -135,13 +135,14 @@ class Node(object):
 
 class Namespace(Node):
 
-    def __init__(self, name):
+    def __init__(self, name, version):
         Node.__init__(self, name)
+        self.version = version
         self.nodes = []
 
     def __repr__(self):
-        return '%s(%r, %r)' % (self.__class__.__name__, self.name,
-                               self.nodes)
+        return '%s(%r, %r, %r)' % (self.__class__.__name__, self.name,
+                                   self.version, self.nodes)
 
 
 class Function(Node):
index 2725808..16d934c 100644 (file)
@@ -84,10 +84,10 @@ class GIRParser(object):
         assert root.tag == _corens('repository')
         for node in root.getchildren():
             if node.tag == _corens('include'):
-                self._includes.add(node.attrib['name'])
+                self._includes.add((node.attrib['name']))
         ns = root.find(_corens('namespace'))
         assert ns is not None
-        self._namespace = Namespace(ns.attrib['name'])
+        self._namespace = Namespace(ns.attrib['name'], ns.attrib['version'])
         self._shared_libraries.extend(ns.attrib['shared-library'].split(','))
         for child in ns.getchildren():
             self._parse_node(child)
index 25328d7..1ccbbef 100644 (file)
@@ -43,7 +43,7 @@ class GIRWriter(XMLWriter):
             ('xmlns:glib', 'http://www.gtk.org/introspection/glib/1.0'),
             ]
         with self.tagcontext('repository', attrs):
-            for include in includes:
+            for include in sorted(includes):
                 self._write_include(include)
             self._write_namespace(namespace, shlibs)
 
@@ -57,6 +57,7 @@ class GIRWriter(XMLWriter):
             libraries.append(os.path.basename(l))
 
         attrs = [('name', namespace.name),
+                 ('version', namespace.version),
                  ('shared-library', ','.join(libraries))]
         with self.tagcontext('namespace', attrs):
             for node in namespace.nodes:
index 1d9df84..319e3e7 100644 (file)
@@ -141,7 +141,7 @@ class GLibTransformer(object):
             self._validate(nodes)
 
         # Create a new namespace with what we found
-        namespace = Namespace(namespace.name)
+        namespace = Namespace(namespace.name, namespace.version)
         namespace.nodes = map(lambda x: x[1], self._names.aliases.itervalues())
         for (ns, x) in self._names.names.itervalues():
             namespace.nodes.append(x)
index be7f83f..ea89b33 100644 (file)
@@ -63,9 +63,9 @@ class Names(object):
 
 class Transformer(object):
 
-    def __init__(self, generator, namespace_name):
+    def __init__(self, generator, namespace_name, namespace_version):
         self.generator = generator
-        self._namespace = Namespace(namespace_name)
+        self._namespace = Namespace(namespace_name, namespace_version)
         self._names = Names()
         self._typedefs_ns = {}
         self._strip_prefix = ''
@@ -96,36 +96,37 @@ class Transformer(object):
         return self._namespace
 
     def register_include(self, filename):
-        (path, suffix) = os.path.splitext(filename)
-        name = os.path.basename(path)
+        (dirname, basename) = os.path.split(filename)
+        if dirname:
+            path = filename
+            (name, suffix) = os.path.splitext(basename)
+        else:
+            path = None
+            name = filename
+            if name.endswith('.gir'):
+                (name, suffix) = os.path.splitext(name)
         if name in self._includes:
             return
-        if suffix == '':
-            suffix = '.gir'
-            filename = path + suffix
-        if suffix == '.gir':
-            source = filename
-            if not os.path.exists(filename):
-                searchdirs = [os.path.join(d, 'gir') for d \
-                                  in _xdg_data_dirs]
-                searchdirs.extend(self._includepaths)
-                source = None
-                for d in searchdirs:
-                    source = os.path.join(d, filename)
-                    if os.path.exists(source):
-                        break
-                    source = None
-            if not source:
+        source = filename
+        if path is None:
+            girname = name + '.gir'
+            searchdirs = [os.path.join(d, 'gir') for d \
+                              in _xdg_data_dirs]
+            searchdirs.extend(self._includepaths)
+            for d in searchdirs:
+                path = os.path.join(d, girname)
+                if os.path.exists(path):
+                    break
+                path = None
+            if not path:
                 raise ValueError("Couldn't find include %r (search path: %r)"\
-                                     % (filename, searchdirs))
-            d = os.path.dirname(source)
-            if d not in self._includepaths:
-                self._includepaths.append(d)
-            self._includes.add(name)
-            from .girparser import GIRParser
-            parser = GIRParser(source)
-        else:
-            raise NotImplementedError(filename)
+                                     % (girname, searchdirs))
+        d = os.path.dirname(path)
+        if d not in self._includepaths:
+            self._includepaths.append(d)
+        self._includes.add(name)
+        from .girparser import GIRParser
+        parser = GIRParser(path)
         for include in parser.get_includes():
             self.register_include(include)
         nsname = parser.get_namespace().name
index 2c8db24..718dfd8 100644 (file)
@@ -3,7 +3,7 @@
             xmlns="http://www.gtk.org/introspection/core/1.0"
             xmlns:c="http://www.gtk.org/introspection/c/1.0"
             xmlns:glib="http://www.gtk.org/introspection/glib/1.0">
-  <namespace name="Foo">
+  <namespace name="Foo" version="1.0">
     <function name="test1" c:identifier="test1">
       <return-value>
         <type name="boolean" c:type="gboolean"/>
index 8b680ae..0de395b 100644 (file)
@@ -3,7 +3,7 @@
             xmlns="http://www.gtk.org/introspection/core/1.0"
             xmlns:c="http://www.gtk.org/introspection/c/1.0"
             xmlns:glib="http://www.gtk.org/introspection/glib/1.0">
-  <namespace name="Foo">
+  <namespace name="Foo" version="1.0">
     <glib:boxed glib:name="BoxedType1" glib:type-name="boxed1" glib:get-type="boxed1_get_type" deprecated="1">
       <field name="field1" readable="1" writable="1" offset="0">
         <type name="uint32"/>
index c64bd93..6fc502a 100644 (file)
@@ -3,7 +3,7 @@
             xmlns="http://www.gtk.org/introspection/core/1.0"
             xmlns:c="http://www.gtk.org/introspection/c/1.0"
             xmlns:glib="http://www.gtk.org/introspection/glib/1.0">
-  <namespace name="Foo">
+  <namespace name="Foo" version="1.0">
     <constant name="constant1" value="42">
       <type name="int"/>
     </constant>
index 5da381e..699bdec 100644 (file)
@@ -3,7 +3,7 @@
             xmlns="http://www.gtk.org/introspection/core/1.0"
             xmlns:c="http://www.gtk.org/introspection/c/1.0"
             xmlns:glib="http://www.gtk.org/introspection/glib/1.0">
-  <namespace name="Foo">
+  <namespace name="Foo" version="1.0">
     <enumeration name="Enum1" glib:type-name="FooEnum" glib:get-type="foo_enum_get_type">
       <member name="value1" value="0" />
       <member name="value2" value="1" />
index 206640f..1458807 100644 (file)
@@ -3,7 +3,7 @@
             xmlns="http://www.gtk.org/introspection/core/1.0"
             xmlns:c="http://www.gtk.org/introspection/c/1.0"
             xmlns:glib="http://www.gtk.org/introspection/glib/1.0">
-  <namespace name="Foo">
+  <namespace name="Foo" version="1.0">
     <enum name="ErrorCodes1" type-name="ErrorCodes1" get-type="foo_error_codes1_get_type">
       <member name="e1" value="0" />
       <member name="e2" value="1" deprecated="1" />
index cabeb2e..e60670a 100644 (file)
@@ -3,7 +3,7 @@
             xmlns="http://www.gtk.org/introspection/core/1.0"
             xmlns:c="http://www.gtk.org/introspection/c/1.0"
             xmlns:glib="http://www.gtk.org/introspection/glib/1.0">
-  <namespace name="Foo">
+  <namespace name="Foo" version="1.0">
     <boxed name="Boxed1" type-name="Boxed1" get-type="boxed1_get_type">
     </boxed>
     <function name="test1" symbol="test1" deprecated="1">
index a22cd72..4182a61 100644 (file)
@@ -3,7 +3,7 @@
             xmlns="http://www.gtk.org/introspection/core/1.0"
             xmlns:c="http://www.gtk.org/introspection/c/1.0"
             xmlns:glib="http://www.gtk.org/introspection/glib/1.0">
-  <namespace name="Foo">
+  <namespace name="Foo" version="1.0">
     <interface name="Iface1" glib:type-name="Iface1" glib:get-type="iface1_get_type">
       <requires>
         <interface name="Iface2" />
index 2b33d54..df72f25 100644 (file)
@@ -20,8 +20,8 @@ endif
 BUILT_SOURCES = testfns-metadata.c
 CLEANFILES = testfns-metadata.c 
 
-testfns-metadata.c: testfns.gir $(top_builddir)/tools/g-ir-compiler Makefile
-       $(CHECK_DEBUG) $(top_builddir)/tools/g-ir-compiler $(srcdir)/testfns.gir --code -o testfns-metadata.c
+testfns-metadata.c: testfns-1.0.gir $(top_builddir)/tools/g-ir-compiler Makefile
+       $(CHECK_DEBUG) $(top_builddir)/tools/g-ir-compiler $(srcdir)/testfns-1.0.gir --code -o testfns-metadata.c
 
 invoke_SOURCES = invoke.c
 invoke_CFLAGS = $(GIREPO_CFLAGS)  -I$(top_srcdir)/girepository
index 820c37a..27337e7 100644 (file)
@@ -25,10 +25,8 @@ main (int argc, char *argv[])
 
   rep = g_irepository_get_default ();
 
-  g_print ("before dlopening %s: %d infos in the repository\n", 
-          testfns,
-          g_irepository_get_n_infos (rep, "test"));
-
+  g_assert (!g_irepository_is_registered (NULL, "test", NULL));
+  
   handle = g_module_open (testfns, 0);
   if (!handle)
     {
@@ -36,6 +34,8 @@ main (int argc, char *argv[])
       return 1;
     }
 
+  g_assert (g_irepository_is_registered (NULL, "test", NULL));
+
   g_print ("after dlopening %s: %d infos in the repository\n", 
           testfns,
           g_irepository_get_n_infos (rep, "test"));
diff --git a/tests/invoke/testfns.gir b/tests/invoke/testfns.gir
deleted file mode 100755 (executable)
index 92e36a6..0000000
+++ /dev/null
@@ -1,98 +0,0 @@
-<?xml version="1.0"?>
-<repository version="1.0"
-            xmlns="http://www.gtk.org/introspection/core/1.0"
-            xmlns:c="http://www.gtk.org/introspection/c/1.0"
-            xmlns:glib="http://www.gtk.org/introspection/glib/1.0">
-  <namespace name="test">
-    <function name="test1" c:identifier="test1">
-      <return-value>
-        <type name="int" c:type="gint"/>
-      </return-value>
-      <parameters>
-        <parameter name="in" direction="in">
-          <type name="int" c:type="gint"/>
-        </parameter>
-      </parameters>
-    </function>
-
-    <function name="test2" c:identifier="test2">
-      <return-value>
-        <type name="none" c:type="void"/>
-      </return-value>
-      <parameters>
-        <parameter name="in" c:type="gint" direction="in">
-          <type name="int" c:type="gint"/>
-        </parameter>
-        <parameter name="out" c:type="gint" direction="out">
-          <type name="int" c:type="gint"/>
-        </parameter>
-      </parameters>
-    </function>
-
-    <function name="test3" c:identifier="test3">
-      <return-value>
-        <type name="none" c:type="void"/>
-      </return-value>
-      <parameters>
-        <parameter name="inout" c:type="gint" direction="inout">
-          <type name="int" c:type="gint"/>
-        </parameter>
-      </parameters>
-    </function>
-
-    <function name="test4" c:identifier="test4">
-      <return-value>
-        <type name="none" c:type="void"/>
-      </return-value>
-      <parameters>
-        <parameter name="blurb" direction="in">
-          <type name="utf8" c:type="gchar*"/>
-        </parameter>
-      </parameters>
-    </function>
-
-    <function name="test5" c:identifier="test5">
-      <return-value>
-        <type name="none" c:type="void"/>
-      </return-value>
-      <parameters>
-        <parameter name="blurb" direction="out" transfer="full">
-          <type name="utf8" c:type="gchar*"/>
-        </parameter>
-        <parameter name="len" direction="out">
-          <type name="int" c:type="gint"/>
-        </parameter>
-      </parameters>
-    </function>
-
-    <function name="test6" c:identifier="test6">
-      <return-value>
-        <type name="int" c:type="gint"/>
-      </return-value>
-      <parameters>
-        <parameter name="list" direction="in">
-          <type name="GLib.List<int>*" c:type="GList*"/>
-        </parameter>
-      </parameters>
-    </function>
-
-
-    <function name="test7" c:identifier="test7">
-      <return-value transfer="full">
-         <type name="utf8" c:type="gchar*"/>
-      </return-value>
-      <parameters>
-        <parameter name="list" direction="in">
-          <type name="GLib.List<utf8>*" c:type="GList*"/>
-        </parameter>
-      </parameters>
-    </function>
-
-    <function name="broken" c:identifier="broken">
-      <return-value>
-         <type name="none" c:type="void"/>
-      </return-value>
-    </function>
-
-  </namespace>
-</repository>
index 13c37c7..bca6e5e 100644 (file)
@@ -3,8 +3,8 @@
             xmlns="http://www.gtk.org/introspection/core/1.0"
             xmlns:c="http://www.gtk.org/introspection/c/1.0"
             xmlns:glib="http://www.gtk.org/introspection/glib/1.0">
-  <include name="GObject"/>
-  <namespace name="Foo">
+  <include name="GObject-2.0"/>
+  <namespace name="Foo" version="1.0">
     <interface name="IFace1" glib:type-name="IFace1" glib:get-type="iface1_get_type">
     </interface>
     <class name="Object1" parent="Object2" glib:type-name="Object1" glib:get-type="object1_get_type">
index fc6fd21..2bf78d1 100644 (file)
@@ -34,28 +34,52 @@ CLEANFILES = $(TYPELIBS) $(TXMLS) $(GIRS)
 BUILT_SOURCES = $(TYPELIBS) $(TXMLS) $(GIRS)
 EXTRA_DIST = $(EXPECTEDGIRS)
 
-%.gir: lib%.la %.c %.h utility.gir $(SCANNER) $(SCANNER_LIBS) Makefile
+annotation-1.0.gir: libannotation.la annotation.c annotation.h utility-1.0.gir $(SCANNER) $(SCANNER_LIBS) Makefile
        PYTHONPATH=$(top_builddir):$$PYTHONPATH $(CHECK_DEBUG) $(SCANNER) -v \
-       --include=$(top_srcdir)/gir/GObject.gir \
-       --include=$(top_builddir)/tests/scanner/utility.gir \
-       --library=$* \
-       --namespace=$* \
+       --include=$(top_srcdir)/gir/GObject-2.0.gir \
+       --include=$(top_builddir)/tests/scanner/utility-1.0.gir \
+       --library=annotation \
+       --namespace=annotation \
+       --nsversion=1.0 \
        --pkg gobject-2.0 \
-       $(srcdir)/$*.h $(srcdir)/$*.c \
+       $(srcdir)/annotation.h $(srcdir)/annotation.c \
         --output $@
-GIRS += annotation.gir
-GIRS += drawable.gir
-GIRS += foo.gir
+GIRS += annotation-1.0.gir
 
-utility.gir: libutility.la utility.h $(SCANNER) $(SCANNER_LIBS) Makefile
+drawable-1.0.gir: libdrawable.la drawable.c drawable.h utility-1.0.gir $(SCANNER) $(SCANNER_LIBS) Makefile
        PYTHONPATH=$(top_builddir):$$PYTHONPATH $(CHECK_DEBUG) $(SCANNER) -v \
-       --include=$(top_srcdir)/gir/GObject.gir \
+       --include=$(top_srcdir)/gir/GObject-2.0.gir \
+       --include=$(top_builddir)/tests/scanner/utility-1.0.gir \
+       --library=drawable \
+       --namespace=drawable \
+       --nsversion=1.0 \
+       --pkg gobject-2.0 \
+       $(srcdir)/drawable.h $(srcdir)/drawable.c \
+        --output $@
+GIRS += drawable-1.0.gir
+
+foo-1.0.gir: libfoo.la foo.c foo.h utility-1.0.gir $(SCANNER) $(SCANNER_LIBS) Makefile
+       PYTHONPATH=$(top_builddir):$$PYTHONPATH $(CHECK_DEBUG) $(SCANNER) -v \
+       --include=$(top_srcdir)/gir/GObject-2.0.gir \
+       --include=$(top_builddir)/tests/scanner/utility-1.0.gir \
+       --library=foo \
+       --namespace=foo \
+       --nsversion=1.0 \
+       --pkg gobject-2.0 \
+       $(srcdir)/foo.h $(srcdir)/foo.c \
+        --output $@
+GIRS += foo-1.0.gir
+
+utility-1.0.gir: libutility.la utility.h $(SCANNER) $(SCANNER_LIBS) Makefile
+       PYTHONPATH=$(top_builddir):$$PYTHONPATH $(CHECK_DEBUG) $(SCANNER) -v \
+       --include=$(top_srcdir)/gir/GObject-2.0.gir \
        --library=utility \
        --namespace=utility \
+       --nsversion=1.0 \
        --pkg gobject-2.0 \
        $(libutility_la_SOURCES) \
        --output $@
-GIRS += utility.gir
+GIRS += utility-1.0.gir
 
 pre-check:
        @if test "$(top_builddir)" != "$(top_srcdir)"; then \
similarity index 97%
rename from tests/scanner/annotation-expected.gir
rename to tests/scanner/annotation-1.0-expected.gir
index e8298b2..6a95e15 100644 (file)
@@ -3,10 +3,10 @@
             xmlns="http://www.gtk.org/introspection/core/1.0"
             xmlns:c="http://www.gtk.org/introspection/c/1.0"
             xmlns:glib="http://www.gtk.org/introspection/glib/1.0">
-  <include name="GLib"/>
-  <include name="GObject"/>
-  <include name="utility"/>
-  <namespace name="annotation" shared-library="annotation">
+  <include name="GLib-2.0"/>
+  <include name="GObject-2.0"/>
+  <include name="utility-1.0"/>
+  <namespace name="annotation" version="1.0" shared-library="annotation">
     <class name="Object"
            c:type="AnnotationObject"
            parent="GObject.Object"
similarity index 87%
rename from tests/scanner/drawable-expected.gir
rename to tests/scanner/drawable-1.0-expected.gir
index 37ff723..67893ca 100644 (file)
@@ -3,10 +3,10 @@
             xmlns="http://www.gtk.org/introspection/core/1.0"
             xmlns:c="http://www.gtk.org/introspection/c/1.0"
             xmlns:glib="http://www.gtk.org/introspection/glib/1.0">
-  <include name="GLib"/>
-  <include name="GObject"/>
-  <include name="utility"/>
-  <namespace name="drawable" shared-library="drawable">
+  <include name="GLib-2.0"/>
+  <include name="GObject-2.0"/>
+  <include name="utility-1.0"/>
+  <namespace name="drawable" version="1.0" shared-library="drawable">
     <class name="TestDrawable"
            c:type="TestDrawable"
            parent="GObject.Object"
similarity index 99%
rename from tests/scanner/foo-expected.gir
rename to tests/scanner/foo-1.0-expected.gir
index 1392f57..5a90f94 100644 (file)
@@ -3,10 +3,10 @@
             xmlns="http://www.gtk.org/introspection/core/1.0"
             xmlns:c="http://www.gtk.org/introspection/c/1.0"
             xmlns:glib="http://www.gtk.org/introspection/glib/1.0">
-  <include name="GLib"/>
-  <include name="GObject"/>
-  <include name="utility"/>
-  <namespace name="foo" shared-library="foo">
+  <include name="GLib-2.0"/>
+  <include name="GObject-2.0"/>
+  <include name="utility-1.0"/>
+  <namespace name="foo" version="1.0" shared-library="foo">
     <alias name="List" target="GLib.SList" c:type="FooList"/>
     <alias name="XEvent" target="none" c:type="FooXEvent"/>
     <alias name="ObjectCookie" target="any" c:type="FooObjectCookie"/>
similarity index 93%
rename from tests/scanner/utility-expected.gir
rename to tests/scanner/utility-1.0-expected.gir
index 5b8bbab..d9a10e8 100644 (file)
@@ -3,9 +3,9 @@
             xmlns="http://www.gtk.org/introspection/core/1.0"
             xmlns:c="http://www.gtk.org/introspection/c/1.0"
             xmlns:glib="http://www.gtk.org/introspection/glib/1.0">
-  <include name="GLib"/>
-  <include name="GObject"/>
-  <namespace name="utility" shared-library="utility">
+  <include name="GLib-2.0"/>
+  <include name="GObject-2.0"/>
+  <namespace name="utility" version="1.0" shared-library="utility">
     <class name="Object"
            c:type="UtilityObject"
            parent="GObject.Object"
index ff659cb..4420845 100644 (file)
@@ -3,7 +3,7 @@
             xmlns="http://www.gtk.org/introspection/core/1.0"
             xmlns:c="http://www.gtk.org/introspection/c/1.0"
             xmlns:glib="http://www.gtk.org/introspection/glib/1.0">
-  <namespace name="Foo">
+  <namespace name="Foo" version="1.0">
     <record name="FooStruct">
       <field name="foo_int" readable="1" writable="1" offset="0">
         <type name="int"/>
index 23890a8..36220ad 100644 (file)
@@ -3,7 +3,7 @@
             xmlns="http://www.gtk.org/introspection/core/1.0"
             xmlns:c="http://www.gtk.org/introspection/c/1.0"
             xmlns:glib="http://www.gtk.org/introspection/glib/1.0">
-  <namespace name="Foo">
+  <namespace name="Foo" version="1.0">
     <function name="lart" symbol="lart">
       <return-type type="gboolean" />
       <parameters>
index 38d0914..846789b 100644 (file)
@@ -28,15 +28,15 @@ TXMLS = $(GIRS:.gir=.gir.txml)
 CLEANFILES = $(TYPELIBS) $(TXMLS) $(GIRS)
 BUILT_SOURCES = $(TYPELIBS) $(TXMLS) $(GIRS)
 
-%.gir: lib%.la %.c %.h $(SCANNER) $(SCANNER_LIBS)
+gitesttypes-1.0.gir: libgitesttypes.la gitesttypes.c gitesttypes.h $(SCANNER) $(SCANNER_LIBS)
        PYTHONPATH=$(top_builddir):$$PYTHONPATH $(CHECK_DEBUG) $(SCANNER) -v \
-       --include=$(top_srcdir)/gir/GObject.gir \
-       --library=$* \
-       --namespace=$* \
+       --include=$(top_srcdir)/gir/GObject-2.0.gir \
+       --library=gitesttypes \
+       --namespace=giesttypes --nsversion=1.0 \
        --pkg gobject-2.0 \
-       $(srcdir)/$*.h $(srcdir)/$*.c \
+       $(srcdir)/gitesttypes.h $(srcdir)/gitesttypes.c \
        --output $@
-GIRS += gitesttypes.gir
+GIRS += gitesttypes-1.0.gir
 
 %.typelib: %.gir $(top_builddir)/tools/g-ir-compiler$(EXEEXT) Makefile
        $(top_builddir)/tools/g-ir-compiler --includedir=. --includedir=$(top_builddir)/gir $< -o $@
index 9c4ae61..3ace0e2 100644 (file)
@@ -3,7 +3,7 @@
             xmlns="http://www.gtk.org/introspection/core/1.0"
             xmlns:c="http://www.gtk.org/introspection/c/1.0"
             xmlns:glib="http://www.gtk.org/introspection/glib/1.0">
-  <namespace name="Foo">
+  <namespace name="Foo" version="1.0">
     <union name="union1" type-name="UnionType1" get-type="union1_get_type">
       <discriminator offset="-4" type="gint" />
       <field name="field1" readable="1" writable="1" offset="0" type="guint32" branch="0" />
index 275e30f..17b9239 100644 (file)
@@ -3,7 +3,7 @@
             xmlns="http://www.gtk.org/introspection/core/1.0"
             xmlns:c="http://www.gtk.org/introspection/c/1.0"
             xmlns:glib="http://www.gtk.org/introspection/glib/1.0">
-  <namespace name="Foo">
+  <namespace name="Foo" version="1.0">
     <glib:boxed glib:name="Boxed" glib:type-name="FooBoxed" glib:get-type="foo_boxed_get_type">
     </glib:boxed>
     <function name="test" symbol="foo_test">
index 06ec651..0ee4836 100644 (file)
@@ -3,7 +3,7 @@
             xmlns="http://www.gtk.org/introspection/core/1.0"
             xmlns:c="http://www.gtk.org/introspection/c/1.0"
             xmlns:glib="http://www.gtk.org/introspection/glib/1.0">
-  <namespace name="Bar">
+  <namespace name="Bar" version="1.0">
     <glib:boxed glib:name="Boxed" glib:type-name="BarBoxed" glib:get-type="bar_boxed_get_type">
     </glib:boxed>
     <function name="test" symbol="bar_test">
index 1d01bb7..7d3f02d 100755 (executable)
@@ -59,6 +59,9 @@ def _get_option_parser():
     parser.add_option("-n", "--namespace",
                       action="store", dest="namespace_name",
                       help="name of namespace for this unit")
+    parser.add_option("", "--nsversion",
+                      action="store", dest="namespace_version",
+                      help="version of namespace for this unit")
     parser.add_option("", "--strip-prefix",
                       action="store", dest="strip_prefix", default="",
                       help="prefix to strip from functions, like g_")
@@ -244,7 +247,7 @@ def main(args):
     ss.parse_macros(filenames)
 
     # Transform the C symbols into AST nodes
-    transformer = Transformer(ss, options.namespace_name)
+    transformer = Transformer(ss, options.namespace_name, options.namespace_version)
     transformer.set_strip_prefix(options.strip_prefix)
     for include in options.includes:
         transformer.register_include(include)
index 5f10d27..7e8d5a2 100644 (file)
@@ -1064,13 +1064,16 @@ write_repository (const char   *namespace,
     {
       const gchar *shared_library;
       const char *ns = namespace;
+      const char *version;
+
+      version = g_irepository_get_version (repository, ns);
 
       shared_library = g_irepository_get_shared_library (repository, ns);
       if (shared_library)
-        g_fprintf (file, "  <namespace name=\"%s\" shared-library=\"%s\">\n",
-                   ns, shared_library);
+        g_fprintf (file, "  <namespace name=\"%s\" version=\"%s\" shared-library=\"%s\">\n",
+                   ns, version, shared_library);
       else
-        g_fprintf (file, "  <namespace name=\"%s\">\n", ns);
+        g_fprintf (file, "  <namespace name=\"%s\" version=\"%s\">\n", ns, version);
       
       for (j = 0; j < g_irepository_get_n_infos (repository, ns); j++)
        {