From 484a26d9ccba361b3135162c677cd4e3aaed739d Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Wed, 10 Feb 2021 11:45:26 +0000 Subject: 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. Helps: #323, #455 Signed-off-by: Simon McVittie --- .flake8 | 2 +- girepository/girparser.c | 29 +++++++++++++++++++++++++---- giscanner/transformer.py | 4 +++- tests/warn/warningtester.py | 1 - tools/g-ir-tool-template.in | 6 ++++-- 5 files changed, 33 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 c483db15..f50ddb97 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 */ }; @@ -184,6 +185,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; } @@ -193,8 +198,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); @@ -206,8 +211,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); } @@ -308,6 +312,23 @@ 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); + g_debug ("Trying %s from GI_GIR_PATH", path); + 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 3b0bea8a..e99743ee 100644 --- a/giscanner/transformer.py +++ b/giscanner/transformer.py @@ -190,7 +190,9 @@ None.""" def _find_include(self, include): searchdirs = self._includepaths[:] - searchdirs.extend(GIRDIR) + from_env = os.getenv('GI_GIR_PATH', '') + if from_env: + searchdirs.extend(from_env.split(os.pathsep)) for path in self._get_gi_data_dirs(): searchdirs.append(os.path.join(path, 'gir-1.0')) 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 b8f67913..2edcdd0e 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, 'giscanner', 'gdump.c') if os.path.isfile(gdump_path): builtins.__dict__['GDUMP_PATH'] = gdump_path -- cgit v1.2.1