[gobject-introspection/wip/smcv/search-paths: 2/6] Add GI_GIR_PATH environment variable




commit ee39338b00d8ad812ebb6ebda7da4b684124e87a
Author: Simon McVittie <smcv debian org>
Date:   Wed Feb 10 11:45:26 2021 +0000

    Add GI_GIR_PATH environment variable
    
    This is searched after any command-line includes paths, but before the
    XDG_DATA_DIRS or any built-in paths. For example, if
    libraries are installed in /opt/gnome and GObject-Introspection was
    configured with -Dgir_dir_prefix=lib64, you could set either
    GI_GIR_PATH=/opt/gnome/lib64/gir-1.0 or
    XDG_DATA_DIRS=/opt/gnome/lib64:/opt/gnome/share:${XDG_DATA_DIRS}.
    
    Use this to communicate the ../gir directory to giscanner instead
    of modifying Python's builtins.
    
    Signed-off-by: Simon McVittie <smcv debian org>
    
    Gbp-Pq: Name Add-GI_GIR_PATH-environment-variable.patch

 .flake8                     |  2 +-
 girepository/girparser.c    | 28 ++++++++++++++++++++++++----
 giscanner/transformer.py    |  4 +++-
 tests/warn/warningtester.py |  1 -
 tools/g-ir-tool-template.in |  6 ++++--
 5 files changed, 32 insertions(+), 9 deletions(-)
---
diff --git a/.flake8 b/.flake8
index 527cabcd..4ce80256 100644
--- a/.flake8
+++ b/.flake8
@@ -1,4 +1,4 @@
 [flake8]
 ignore=E127,E402,E501,E731,E128,W503,E741,W504
 exclude=misc,subprojects
-builtins=DATADIR,GIRDIR
+builtins=DATADIR
diff --git a/girepository/girparser.c b/girepository/girparser.c
index ad676e33..6747c3a5 100644
--- a/girepository/girparser.c
+++ b/girepository/girparser.c
@@ -58,6 +58,7 @@
 struct _GIrParser
 {
   gchar **includes;
+  gchar **gi_gir_path;
   GList *parsed_modules; /* All previously parsed modules */
 };
 
@@ -183,6 +184,10 @@ GIrParser *
 _g_ir_parser_new (void)
 {
   GIrParser *parser = g_slice_new0 (GIrParser);
+  const char *gi_gir_path = g_getenv ("GI_GIR_PATH");
+
+  if (gi_gir_path != NULL)
+    parser->gi_gir_path = g_strsplit (gi_gir_path, G_SEARCHPATH_SEPARATOR_S, 0);
 
   return parser;
 }
@@ -192,8 +197,8 @@ _g_ir_parser_free (GIrParser *parser)
 {
   GList *l;
 
-  if (parser->includes)
-    g_strfreev (parser->includes);
+  g_strfreev (parser->includes);
+  g_strfreev (parser->gi_gir_path);
 
   for (l = parser->parsed_modules; l; l = l->next)
     _g_ir_module_free (l->data);
@@ -205,8 +210,7 @@ void
 _g_ir_parser_set_includes (GIrParser          *parser,
                           const gchar *const *includes)
 {
-  if (parser->includes)
-    g_strfreev (parser->includes);
+  g_strfreev (parser->includes);
 
   parser->includes = g_strdupv ((char **)includes);
 }
@@ -296,6 +300,22 @@ locate_gir (GIrParser  *parser,
          path = NULL;
        }
     }
+
+  if (parser->gi_gir_path != NULL)
+    {
+      for (dir = (const gchar *const *) parser->gi_gir_path; *dir; dir++)
+        {
+          if (**dir == '\0')
+            continue;
+
+          path = g_build_filename (*dir, girname, NULL);
+          if (g_file_test (path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))
+            return path;
+          g_free (path);
+          path = NULL;
+        }
+    }
+
   for (dir = datadirs; *dir; dir++)
     {
       path = g_build_filename (*dir, GIR_SUFFIX, girname, NULL);
diff --git a/giscanner/transformer.py b/giscanner/transformer.py
index bcabdedc..58176a6f 100644
--- a/giscanner/transformer.py
+++ b/giscanner/transformer.py
@@ -190,12 +190,14 @@ None."""
 
     def _find_include(self, include):
         searchdirs = self._includepaths[:]
-        searchdirs.extend(GIRDIR)
+        searchdirs.extend(os.getenv('GI_GIR_PATH', '').split(os.pathsep))
         for path in self._get_gi_data_dirs():
             searchdirs.append(os.path.join(path, 'gir-1.0'))
 
         girname = '%s-%s.gir' % (include.name, include.version)
         for d in searchdirs:
+            if not d:
+                continue
             path = os.path.join(d, girname)
             if os.path.exists(path):
                 return path
diff --git a/tests/warn/warningtester.py b/tests/warn/warningtester.py
index 3bb9e4b2..2dc462c9 100644
--- a/tests/warn/warningtester.py
+++ b/tests/warn/warningtester.py
@@ -9,7 +9,6 @@ sys.path.insert(0, path)
 
 # Not correct, but enough to get the tests going uninstalled
 builtins.__dict__['DATADIR'] = path
-builtins.__dict__['GIRDIR'] = ''
 
 from giscanner.annotationparser import GtkDocCommentBlockParser
 from giscanner.ast import Include, Namespace
diff --git a/tools/g-ir-tool-template.in b/tools/g-ir-tool-template.in
index f8fd2d33..7fc4de5b 100755
--- a/tools/g-ir-tool-template.in
+++ b/tools/g-ir-tool-template.in
@@ -52,7 +52,6 @@ if not os.path.isdir(os.path.join(datadir, 'gir-1.0')):
     datadir = "@datarootdir@"
 
 builtins.__dict__['DATADIR'] = datadir
-builtins.__dict__['GIRDIR'] = []
 
 # Again, relative paths first so that the installation prefix is relocatable
 pylibdir = os.path.abspath(os.path.join(filedir, '..', 'lib', 'gobject-introspection'))
@@ -76,7 +75,10 @@ if not os.path.isfile(os.path.join(pylibdir, 'giscanner', '_giscanner' + py_mod_
         # We're running uninstalled inside meson
         builddir = os.path.abspath(os.path.join(filedir, '..'))
         pylibdir = builddir
-        builtins.__dict__['GIRDIR'].append(os.path.join(filedir, os.pardir, 'gir'))
+
+        if 'GI_GIR_PATH' not in os.environ:
+            os.environ['GI_GIR_PATH'] = os.path.join(filedir, os.pardir, 'gir')
+
         gdump_path = os.path.join(builddir, 'girepository', 'gdump.c')
         if os.path.isfile(gdump_path):
             builtins.__dict__['GDUMP_PATH'] = gdump_path


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