Merge branch 'gi-repository-no-singleton' into 'main'

girepository: Drop gi_repository_get_default()

See merge request GNOME/glib!3856
This commit is contained in:
Philip Withnall 2024-01-26 13:04:40 +00:00
commit 494f8d4d87
8 changed files with 256 additions and 242 deletions

View File

@ -59,10 +59,10 @@
* standard Linux system this will end up being `/usr/lib/girepository-1.0`. * standard Linux system this will end up being `/usr/lib/girepository-1.0`.
* *
* It is possible to control the search paths programmatically, using * It is possible to control the search paths programmatically, using
* [func@GIRepository.Repository.prepend_search_path]. It is also possible to * [method@GIRepository.Repository.prepend_search_path]. It is also possible to
* modify the search paths by using the `GI_TYPELIB_PATH` environment variable. * modify the search paths by using the `GI_TYPELIB_PATH` environment variable.
* The environment variable takes precedence over the default search path * The environment variable takes precedence over the default search path
* and the [func@GIRepository.Repository.prepend_search_path] calls. * and the [method@GIRepository.Repository.prepend_search_path] calls.
* *
* Since: 2.80 * Since: 2.80
*/ */
@ -75,9 +75,6 @@
#define GIREPOSITORY_TYPELIB_FILENAME \ #define GIREPOSITORY_TYPELIB_FILENAME \
GIREPOSITORY_TYPELIB_NAME "-" GIREPOSITORY_TYPELIB_VERSION ".typelib" GIREPOSITORY_TYPELIB_NAME "-" GIREPOSITORY_TYPELIB_VERSION ".typelib"
static GIRepository *default_repository = NULL;
static GPtrArray *typelib_search_path = NULL;
typedef struct { typedef struct {
size_t n_interfaces; size_t n_interfaces;
GIBaseInfo *interfaces[]; GIBaseInfo *interfaces[];
@ -97,6 +94,9 @@ struct _GIRepository
{ {
GObject parent; GObject parent;
GPtrArray *typelib_search_path; /* (element-type filename) (owned) */
GPtrArray *library_paths; /* (element-type filename) (owned) */
GHashTable *typelibs; /* (string) namespace -> GITypelib */ GHashTable *typelibs; /* (string) namespace -> GITypelib */
GHashTable *lazy_typelibs; /* (string) namespace-version -> GITypelib */ GHashTable *lazy_typelibs; /* (string) namespace-version -> GITypelib */
GHashTable *info_by_gtype; /* GType -> GIBaseInfo */ GHashTable *info_by_gtype; /* GType -> GIBaseInfo */
@ -148,6 +148,40 @@ DllMain (HINSTANCE hinstDLL,
static void static void
gi_repository_init (GIRepository *repository) gi_repository_init (GIRepository *repository)
{ {
/* typelib search path */
{
const char *libdir;
char *typelib_dir;
const char *type_lib_path_env;
/* This variable is intended to take precedence over both:
* - the default search path;
* - all gi_repository_prepend_search_path() calls.
*/
type_lib_path_env = g_getenv ("GI_TYPELIB_PATH");
if (type_lib_path_env)
{
char **custom_dirs;
custom_dirs = g_strsplit (type_lib_path_env, G_SEARCHPATH_SEPARATOR_S, 0);
repository->typelib_search_path =
g_ptr_array_new_take_null_terminated ((gpointer) g_steal_pointer (&custom_dirs), g_free);
}
else
{
repository->typelib_search_path = g_ptr_array_new_null_terminated (1, g_free, TRUE);
}
libdir = GOBJECT_INTROSPECTION_LIBDIR;
typelib_dir = g_build_filename (libdir, "girepository-1.0", NULL);
g_ptr_array_add (repository->typelib_search_path, g_steal_pointer (&typelib_dir));
}
repository->library_paths = g_ptr_array_new_null_terminated (1, g_free, TRUE);
repository->typelibs repository->typelibs
= g_hash_table_new_full (g_str_hash, g_str_equal, = g_hash_table_new_full (g_str_hash, g_str_equal,
(GDestroyNotify) g_free, (GDestroyNotify) g_free,
@ -185,6 +219,9 @@ gi_repository_finalize (GObject *object)
g_clear_pointer (&repository->cached_shared_libraries, g_strfreev); g_clear_pointer (&repository->cached_shared_libraries, g_strfreev);
g_clear_pointer (&repository->library_paths, g_ptr_array_unref);
g_clear_pointer (&repository->typelib_search_path, g_ptr_array_unref);
(* G_OBJECT_CLASS (gi_repository_parent_class)->finalize) (G_OBJECT (repository)); (* G_OBJECT_CLASS (gi_repository_parent_class)->finalize) (G_OBJECT (repository));
} }
@ -198,54 +235,9 @@ gi_repository_class_init (GIRepositoryClass *class)
gobject_class->finalize = gi_repository_finalize; gobject_class->finalize = gi_repository_finalize;
} }
static void
init_globals (void)
{
static gsize initialized = 0;
if (!g_once_init_enter (&initialized))
return;
if (default_repository == NULL)
default_repository = gi_repository_new ();
if (typelib_search_path == NULL)
{
const char *libdir;
char *typelib_dir;
const char *type_lib_path_env;
/* This variable is intended to take precedence over both:
* - the default search path;
* - all gi_repository_prepend_search_path() calls.
*/
type_lib_path_env = g_getenv ("GI_TYPELIB_PATH");
if (type_lib_path_env)
{
char **custom_dirs;
custom_dirs = g_strsplit (type_lib_path_env, G_SEARCHPATH_SEPARATOR_S, 0);
typelib_search_path =
g_ptr_array_new_take_null_terminated ((gpointer) g_steal_pointer (&custom_dirs), g_free);
}
else
{
typelib_search_path = g_ptr_array_new_null_terminated (1, g_free, TRUE);
}
libdir = GOBJECT_INTROSPECTION_LIBDIR;
typelib_dir = g_build_filename (libdir, "girepository-1.0", NULL);
g_ptr_array_add (typelib_search_path, g_steal_pointer (&typelib_dir));
}
g_once_init_leave (&initialized, 1);
}
/** /**
* gi_repository_prepend_search_path: * gi_repository_prepend_search_path:
* @repository: A #GIRepository
* @directory: (type filename): directory name to prepend to the typelib * @directory: (type filename): directory name to prepend to the typelib
* search path * search path
* *
@ -256,14 +248,17 @@ init_globals (void)
* Since: 2.80 * Since: 2.80
*/ */
void void
gi_repository_prepend_search_path (const char *directory) gi_repository_prepend_search_path (GIRepository *repository,
const char *directory)
{ {
init_globals (); g_return_if_fail (GI_IS_REPOSITORY (repository));
g_ptr_array_insert (typelib_search_path, 0, g_strdup (directory));
g_ptr_array_insert (repository->typelib_search_path, 0, g_strdup (directory));
} }
/** /**
* gi_repository_get_search_path: * gi_repository_get_search_path:
* @repository: A #GIRepository
* @n_paths_out: (optional) (out): The number of search paths returned. * @n_paths_out: (optional) (out): The number of search paths returned.
* *
* Returns the current search path [class@GIRepository.Repository] will use when * Returns the current search path [class@GIRepository.Repository] will use when
@ -277,9 +272,13 @@ gi_repository_prepend_search_path (const char *directory)
* Since: 2.80 * Since: 2.80
*/ */
const char * const * const char * const *
gi_repository_get_search_path (size_t *n_paths_out) gi_repository_get_search_path (GIRepository *repository,
size_t *n_paths_out)
{ {
if G_UNLIKELY (!typelib_search_path || !typelib_search_path->pdata) g_return_val_if_fail (GI_IS_REPOSITORY (repository), NULL);
if G_UNLIKELY (!repository->typelib_search_path ||
!repository->typelib_search_path->pdata)
{ {
static const char * const empty_search_path[] = {NULL}; static const char * const empty_search_path[] = {NULL};
@ -290,9 +289,77 @@ gi_repository_get_search_path (size_t *n_paths_out)
} }
if (n_paths_out) if (n_paths_out)
*n_paths_out = typelib_search_path->len; *n_paths_out = repository->typelib_search_path->len;
return (const char * const *) typelib_search_path->pdata; return (const char * const *) repository->typelib_search_path->pdata;
}
/**
* gi_repository_prepend_library_path:
* @repository: A #GIRepository
* @directory: (type filename): a single directory to scan for shared libraries
*
* Prepends @directory to the search path that is used to
* search shared libraries referenced by imported namespaces.
*
* Multiple calls to this function all contribute to the final
* list of paths.
*
* The list of paths is unique to @repository. When a typelib is loaded by the
* repository, the list of paths from the @repository at that instant is used
* by the typelib for loading its modules.
*
* If the library is not found in the directories configured
* in this way, loading will fall back to the system library
* path (i.e. `LD_LIBRARY_PATH` and `DT_RPATH` in ELF systems).
* See the documentation of your dynamic linker for full details.
*
* Since: 2.80
*/
void
gi_repository_prepend_library_path (GIRepository *repository,
const char *directory)
{
g_return_if_fail (GI_IS_REPOSITORY (repository));
g_ptr_array_insert (repository->library_paths, 0, g_strdup (directory));
}
/**
* gi_repository_get_library_path:
* @repository: A #GIRepository
* @n_paths_out: (optional) (out): The number of library paths returned.
*
* Returns the current search path [class@GIRepository.Repository] will use when
* loading shared libraries referenced by imported namespaces.
*
* The list is internal to [class@GIRepository.Repository] and should not be
* freed, nor should its string elements.
*
* Returns: (element-type filename) (transfer none) (array length=n_paths_out): list of search paths, most
* important first
* Since: 2.80
*/
const char * const *
gi_repository_get_library_path (GIRepository *repository,
size_t *n_paths_out)
{
g_return_val_if_fail (GI_IS_REPOSITORY (repository), NULL);
if G_UNLIKELY (!repository->library_paths || !repository->library_paths->pdata)
{
static const char * const empty_search_path[] = {NULL};
if (n_paths_out)
*n_paths_out = 0;
return empty_search_path;
}
if (n_paths_out)
*n_paths_out = repository->library_paths->len;
return (const char * const *) repository->library_paths->pdata;
} }
static char * static char *
@ -321,17 +388,6 @@ get_typelib_dependencies (GITypelib *typelib)
return g_strsplit (dependencies_glob, "|", 0); return g_strsplit (dependencies_glob, "|", 0);
} }
static GIRepository *
get_repository (GIRepository *repository)
{
init_globals ();
if (repository != NULL)
return repository;
else
return default_repository;
}
static GITypelib * static GITypelib *
check_version_conflict (GITypelib *typelib, check_version_conflict (GITypelib *typelib,
const char *namespace, const char *namespace,
@ -372,7 +428,7 @@ get_registered_status (GIRepository *repository,
char **version_conflict) char **version_conflict)
{ {
GITypelib *typelib; GITypelib *typelib;
repository = get_repository (repository);
if (lazy_status) if (lazy_status)
*lazy_status = FALSE; *lazy_status = FALSE;
typelib = g_hash_table_lookup (repository->typelibs, namespace); typelib = g_hash_table_lookup (repository->typelibs, namespace);
@ -489,8 +545,7 @@ register_internal (GIRepository *repository,
/** /**
* gi_repository_get_immediate_dependencies: * gi_repository_get_immediate_dependencies:
* @repository: (nullable): A #GIRepository, or `NULL` for the singleton * @repository: A #GIRepository
* process-global default #GIRepository
* @namespace_: Namespace of interest * @namespace_: Namespace of interest
* *
* Return an array of the immediate versioned dependencies for @namespace_. * Return an array of the immediate versioned dependencies for @namespace_.
@ -514,10 +569,9 @@ gi_repository_get_immediate_dependencies (GIRepository *repository,
GITypelib *typelib; GITypelib *typelib;
char **deps; char **deps;
g_return_val_if_fail (GI_IS_REPOSITORY (repository), NULL);
g_return_val_if_fail (namespace != NULL, NULL); g_return_val_if_fail (namespace != NULL, NULL);
repository = get_repository (repository);
typelib = get_registered (repository, namespace, NULL); typelib = get_registered (repository, namespace, NULL);
g_return_val_if_fail (typelib != NULL, NULL); g_return_val_if_fail (typelib != NULL, NULL);
@ -571,8 +625,7 @@ get_typelib_dependencies_transitive (GIRepository *repository,
/** /**
* gi_repository_get_dependencies: * gi_repository_get_dependencies:
* @repository: (nullable): A #GIRepository, or `NULL` for the singleton * @repository: A #GIRepository
* process-global default #GIRepository
* @namespace_: Namespace of interest * @namespace_: Namespace of interest
* *
* Retrieves all (transitive) versioned dependencies for * Retrieves all (transitive) versioned dependencies for
@ -601,10 +654,9 @@ gi_repository_get_dependencies (GIRepository *repository,
char *dependency; char *dependency;
GPtrArray *out; /* owned utf8 elements */ GPtrArray *out; /* owned utf8 elements */
g_return_val_if_fail (GI_IS_REPOSITORY (repository), NULL);
g_return_val_if_fail (namespace != NULL, NULL); g_return_val_if_fail (namespace != NULL, NULL);
repository = get_repository (repository);
typelib = get_registered (repository, namespace, NULL); typelib = get_registered (repository, namespace, NULL);
g_return_val_if_fail (typelib != NULL, NULL); g_return_val_if_fail (typelib != NULL, NULL);
@ -632,8 +684,7 @@ gi_repository_get_dependencies (GIRepository *repository,
/** /**
* gi_repository_load_typelib: * gi_repository_load_typelib:
* @repository: (nullable): A #GIRepository, or `NULL` for the singleton * @repository: A #GIRepository
* process-global default #GIRepository
* @typelib: the typelib to load * @typelib: the typelib to load
* @flags: flags affecting the loading operation * @flags: flags affecting the loading operation
* @error: return location for a [type@GLib.Error], or `NULL` * @error: return location for a [type@GLib.Error], or `NULL`
@ -656,7 +707,7 @@ gi_repository_load_typelib (GIRepository *repository,
gboolean is_lazy; gboolean is_lazy;
char *version_conflict; char *version_conflict;
repository = get_repository (repository); g_return_val_if_fail (GI_IS_REPOSITORY (repository), NULL);
header = (Header *) typelib->data; header = (Header *) typelib->data;
namespace = gi_typelib_get_string (typelib, header->namespace); namespace = gi_typelib_get_string (typelib, header->namespace);
@ -681,8 +732,7 @@ gi_repository_load_typelib (GIRepository *repository,
/** /**
* gi_repository_is_registered: * gi_repository_is_registered:
* @repository: (nullable): A #GIRepository, or `NULL` for the singleton * @repository: A #GIRepository
* process-global default #GIRepository
* @namespace_: Namespace of interest * @namespace_: Namespace of interest
* @version: (nullable): Required version, may be `NULL` for latest * @version: (nullable): Required version, may be `NULL` for latest
* *
@ -703,41 +753,15 @@ gi_repository_is_registered (GIRepository *repository,
const char *namespace, const char *namespace,
const char *version) const char *version)
{ {
repository = get_repository (repository); g_return_val_if_fail (GI_IS_REPOSITORY (repository), FALSE);
return get_registered (repository, namespace, version) != NULL;
}
/** return get_registered (repository, namespace, version) != NULL;
* gi_repository_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: (transfer none): The global singleton [class@GIRepository.Repository]
* Since: 2.80
*/
GIRepository *
gi_repository_get_default (void)
{
return get_repository (NULL);
} }
/** /**
* gi_repository_new: * gi_repository_new:
* *
* Create a new (non-singleton) [class@GIRepository.Repository]. * Create a new [class@GIRepository.Repository].
*
* Most callers should use [func@GIRepository.Repository.get_default] instead,
* as a singleton repository is more useful in most situations.
* *
* Returns: (transfer full): a new [class@GIRepository.Repository] * Returns: (transfer full): a new [class@GIRepository.Repository]
* Since: 2.80 * Since: 2.80
@ -750,8 +774,7 @@ gi_repository_new (void)
/** /**
* gi_repository_get_n_infos: * gi_repository_get_n_infos:
* @repository: (nullable): A #GIRepository, or `NULL` for the singleton * @repository: A #GIRepository
* process-global default #GIRepository
* @namespace_: Namespace to inspect * @namespace_: Namespace to inspect
* *
* This function returns the number of metadata entries in * This function returns the number of metadata entries in
@ -769,10 +792,9 @@ gi_repository_get_n_infos (GIRepository *repository,
GITypelib *typelib; GITypelib *typelib;
unsigned int n_interfaces = 0; unsigned int n_interfaces = 0;
g_return_val_if_fail (GI_IS_REPOSITORY (repository), -1);
g_return_val_if_fail (namespace != NULL, -1); g_return_val_if_fail (namespace != NULL, -1);
repository = get_repository (repository);
typelib = get_registered (repository, namespace, NULL); typelib = get_registered (repository, namespace, NULL);
g_return_val_if_fail (typelib != NULL, -1); g_return_val_if_fail (typelib != NULL, -1);
@ -784,8 +806,7 @@ gi_repository_get_n_infos (GIRepository *repository,
/** /**
* gi_repository_get_info: * gi_repository_get_info:
* @repository: (nullable): A #GIRepository, or `NULL` for the singleton * @repository: A #GIRepository
* process-global default #GIRepository
* @namespace_: Namespace to inspect * @namespace_: Namespace to inspect
* @idx: 0-based offset into namespace metadata for entry * @idx: 0-based offset into namespace metadata for entry
* *
@ -808,11 +829,10 @@ gi_repository_get_info (GIRepository *repository,
GITypelib *typelib; GITypelib *typelib;
DirEntry *entry; DirEntry *entry;
g_return_val_if_fail (GI_IS_REPOSITORY (repository), NULL);
g_return_val_if_fail (namespace != NULL, NULL); g_return_val_if_fail (namespace != NULL, NULL);
g_return_val_if_fail (idx < G_MAXUINT16, NULL); g_return_val_if_fail (idx < G_MAXUINT16, NULL);
repository = get_repository (repository);
typelib = get_registered (repository, namespace, NULL); typelib = get_registered (repository, namespace, NULL);
g_return_val_if_fail (typelib != NULL, NULL); g_return_val_if_fail (typelib != NULL, NULL);
@ -860,8 +880,7 @@ find_by_gtype (GHashTable *table, FindByGTypeData *data, gboolean check_prefix)
/** /**
* gi_repository_find_by_gtype: * gi_repository_find_by_gtype:
* @repository: (nullable): A #GIRepository, or `NULL` for the singleton * @repository: A #GIRepository
* process-global default #GIRepository
* @gtype: [type@GObject.Type] to search for * @gtype: [type@GObject.Type] to search for
* *
* Searches all loaded namespaces for a particular [type@GObject.Type]. * Searches all loaded namespaces for a particular [type@GObject.Type].
@ -884,10 +903,9 @@ gi_repository_find_by_gtype (GIRepository *repository,
GIBaseInfo *cached; GIBaseInfo *cached;
DirEntry *entry; DirEntry *entry;
g_return_val_if_fail (GI_IS_REPOSITORY (repository), NULL);
g_return_val_if_fail (gtype != G_TYPE_INVALID, NULL); g_return_val_if_fail (gtype != G_TYPE_INVALID, NULL);
repository = get_repository (repository);
cached = g_hash_table_lookup (repository->info_by_gtype, cached = g_hash_table_lookup (repository->info_by_gtype,
(gpointer)gtype); (gpointer)gtype);
@ -941,8 +959,7 @@ gi_repository_find_by_gtype (GIRepository *repository,
/** /**
* gi_repository_find_by_name: * gi_repository_find_by_name:
* @repository: (nullable): A #GIRepository, or `NULL` for the singleton * @repository: A #GIRepository
* process-global default #GIRepository
* @namespace_: Namespace which will be searched * @namespace_: Namespace which will be searched
* @name: Entry name to find * @name: Entry name to find
* *
@ -964,9 +981,9 @@ gi_repository_find_by_name (GIRepository *repository,
GITypelib *typelib; GITypelib *typelib;
DirEntry *entry; DirEntry *entry;
g_return_val_if_fail (GI_IS_REPOSITORY (repository), NULL);
g_return_val_if_fail (namespace != NULL, NULL); g_return_val_if_fail (namespace != NULL, NULL);
repository = get_repository (repository);
typelib = get_registered (repository, namespace, NULL); typelib = get_registered (repository, namespace, NULL);
g_return_val_if_fail (typelib != NULL, NULL); g_return_val_if_fail (typelib != NULL, NULL);
@ -1004,8 +1021,7 @@ find_by_error_domain_foreach (gpointer key,
/** /**
* gi_repository_find_by_error_domain: * gi_repository_find_by_error_domain:
* @repository: (nullable): A #GIRepository, or `NULL` for the singleton * @repository: A #GIRepository
* process-global default #GIRepository
* @domain: a [type@GLib.Error] domain * @domain: a [type@GLib.Error] domain
* *
* Searches for the enum type corresponding to the given [type@GLib.Error] * Searches for the enum type corresponding to the given [type@GLib.Error]
@ -1026,7 +1042,7 @@ gi_repository_find_by_error_domain (GIRepository *repository,
FindByErrorDomainData data; FindByErrorDomainData data;
GIEnumInfo *cached; GIEnumInfo *cached;
repository = get_repository (repository); g_return_val_if_fail (GI_IS_REPOSITORY (repository), NULL);
cached = g_hash_table_lookup (repository->info_by_error_domain, cached = g_hash_table_lookup (repository->info_by_error_domain,
GUINT_TO_POINTER (domain)); GUINT_TO_POINTER (domain));
@ -1059,7 +1075,7 @@ gi_repository_find_by_error_domain (GIRepository *repository,
/** /**
* gi_repository_get_object_gtype_interfaces: * gi_repository_get_object_gtype_interfaces:
* @repository: (nullable): a #GIRepository, or `NULL` for the default repository * @repository: a #GIRepository
* @gtype: a [type@GObject.Type] whose fundamental type is `G_TYPE_OBJECT` * @gtype: a [type@GObject.Type] whose fundamental type is `G_TYPE_OBJECT`
* @n_interfaces_out: (out): Number of interfaces * @n_interfaces_out: (out): Number of interfaces
* @interfaces_out: (out) (transfer none) (array length=n_interfaces_out): Interfaces for @gtype * @interfaces_out: (out) (transfer none) (array length=n_interfaces_out): Interfaces for @gtype
@ -1088,10 +1104,9 @@ gi_repository_get_object_gtype_interfaces (GIRepository *repository,
{ {
GTypeInterfaceCache *cache; GTypeInterfaceCache *cache;
g_return_if_fail (GI_IS_REPOSITORY (repository));
g_return_if_fail (g_type_fundamental (gtype) == G_TYPE_OBJECT); g_return_if_fail (g_type_fundamental (gtype) == G_TYPE_OBJECT);
repository = get_repository (repository);
cache = g_hash_table_lookup (repository->interfaces_for_gtype, cache = g_hash_table_lookup (repository->interfaces_for_gtype,
(void *) gtype); (void *) gtype);
if (cache == NULL) if (cache == NULL)
@ -1150,8 +1165,7 @@ collect_namespaces (gpointer key,
/** /**
* gi_repository_get_loaded_namespaces: * gi_repository_get_loaded_namespaces:
* @repository: (nullable): A #GIRepository, or `NULL` for the singleton * @repository: A #GIRepository
* process-global default #GIRepository
* *
* Return the list of currently loaded namespaces. * Return the list of currently loaded namespaces.
* *
@ -1166,7 +1180,7 @@ gi_repository_get_loaded_namespaces (GIRepository *repository)
char **names; char **names;
size_t i; size_t i;
repository = get_repository (repository); g_return_val_if_fail (GI_IS_REPOSITORY (repository), NULL);
g_hash_table_foreach (repository->typelibs, collect_namespaces, &list); g_hash_table_foreach (repository->typelibs, collect_namespaces, &list);
g_hash_table_foreach (repository->lazy_typelibs, collect_namespaces, &list); g_hash_table_foreach (repository->lazy_typelibs, collect_namespaces, &list);
@ -1182,8 +1196,7 @@ gi_repository_get_loaded_namespaces (GIRepository *repository)
/** /**
* gi_repository_get_version: * gi_repository_get_version:
* @repository: (nullable): A #GIRepository, or `NULL` for the singleton * @repository: A #GIRepository
* process-global default #GIRepository
* @namespace_: Namespace to inspect * @namespace_: Namespace to inspect
* *
* This function returns the loaded version associated with the given * This function returns the loaded version associated with the given
@ -1203,10 +1216,9 @@ gi_repository_get_version (GIRepository *repository,
GITypelib *typelib; GITypelib *typelib;
Header *header; Header *header;
g_return_val_if_fail (GI_IS_REPOSITORY (repository), NULL);
g_return_val_if_fail (namespace != NULL, NULL); g_return_val_if_fail (namespace != NULL, NULL);
repository = get_repository (repository);
typelib = get_registered (repository, namespace, NULL); typelib = get_registered (repository, namespace, NULL);
g_return_val_if_fail (typelib != NULL, NULL); g_return_val_if_fail (typelib != NULL, NULL);
@ -1217,8 +1229,7 @@ gi_repository_get_version (GIRepository *repository,
/** /**
* gi_repository_get_shared_libraries: * gi_repository_get_shared_libraries:
* @repository: (nullable): A #GIRepository, or `NULL` for the singleton * @repository: A #GIRepository
* process-global default #GIRepository
* @namespace_: Namespace to inspect * @namespace_: Namespace to inspect
* @out_n_elements: (out) (optional): Return location for the number of elements * @out_n_elements: (out) (optional): Return location for the number of elements
* in the returned array * in the returned array
@ -1248,10 +1259,9 @@ gi_repository_get_shared_libraries (GIRepository *repository,
GITypelib *typelib; GITypelib *typelib;
Header *header; Header *header;
g_return_val_if_fail (GI_IS_REPOSITORY (repository), NULL);
g_return_val_if_fail (namespace != NULL, NULL); g_return_val_if_fail (namespace != NULL, NULL);
repository = get_repository (repository);
typelib = get_registered (repository, namespace, NULL); typelib = get_registered (repository, namespace, NULL);
g_return_val_if_fail (typelib != NULL, NULL); g_return_val_if_fail (typelib != NULL, NULL);
@ -1284,8 +1294,7 @@ gi_repository_get_shared_libraries (GIRepository *repository,
/** /**
* gi_repository_get_c_prefix: * gi_repository_get_c_prefix:
* @repository: (nullable): A #GIRepository, or `NULL` for the singleton * @repository: A #GIRepository
* process-global default #GIRepository
* @namespace_: Namespace to inspect * @namespace_: Namespace to inspect
* *
* This function returns the C prefix, or the C level namespace * This function returns the C prefix, or the C level namespace
@ -1308,10 +1317,9 @@ gi_repository_get_c_prefix (GIRepository *repository,
GITypelib *typelib; GITypelib *typelib;
Header *header; Header *header;
g_return_val_if_fail (GI_IS_REPOSITORY (repository), NULL);
g_return_val_if_fail (namespace_ != NULL, NULL); g_return_val_if_fail (namespace_ != NULL, NULL);
repository = get_repository (repository);
typelib = get_registered (repository, namespace_, NULL); typelib = get_registered (repository, namespace_, NULL);
g_return_val_if_fail (typelib != NULL, NULL); g_return_val_if_fail (typelib != NULL, NULL);
@ -1325,8 +1333,7 @@ gi_repository_get_c_prefix (GIRepository *repository,
/** /**
* gi_repository_get_typelib_path: * gi_repository_get_typelib_path:
* @repository: (nullable): A #GIRepository, or `NULL` for the singleton * @repository: A #GIRepository
* process-global default #GIRepository
* @namespace_: GI namespace to use, e.g. `Gtk` * @namespace_: GI namespace to use, e.g. `Gtk`
* *
* If namespace @namespace_ is loaded, return the full path to the * If namespace @namespace_ is loaded, return the full path to the
@ -1345,7 +1352,7 @@ gi_repository_get_typelib_path (GIRepository *repository,
{ {
gpointer orig_key, value; gpointer orig_key, value;
repository = get_repository (repository); g_return_val_if_fail (GI_IS_REPOSITORY (repository), NULL);
if (!g_hash_table_lookup_extended (repository->typelibs, namespace, if (!g_hash_table_lookup_extended (repository->typelibs, namespace,
&orig_key, &value)) &orig_key, &value))
@ -1626,8 +1633,7 @@ find_namespace_latest (const char *namespace,
/** /**
* gi_repository_enumerate_versions: * gi_repository_enumerate_versions:
* @repository: (nullable): A #GIRepository, or `NULL` for the singleton * @repository: A #GIRepository
* process-global default #GIRepository
* @namespace_: GI namespace, e.g. `Gtk` * @namespace_: GI namespace, e.g. `Gtk`
* @n_versions_out: (optional) (out): The number of versions returned. * @n_versions_out: (optional) (out): The number of versions returned.
* *
@ -1647,10 +1653,9 @@ gi_repository_enumerate_versions (GIRepository *repository,
const char *loaded_version; const char *loaded_version;
char **ret; char **ret;
init_globals ();
candidates = enumerate_namespace_versions (namespace_, candidates = enumerate_namespace_versions (namespace_,
(const char * const *) typelib_search_path->pdata, (const char * const *) repository->typelib_search_path->pdata,
typelib_search_path->len); repository->typelib_search_path->len);
if (!candidates) if (!candidates)
{ {
@ -1706,10 +1711,9 @@ require_internal (GIRepository *repository,
char *path = NULL; char *path = NULL;
char *tmp_version = NULL; char *tmp_version = NULL;
g_return_val_if_fail (GI_IS_REPOSITORY (repository), NULL);
g_return_val_if_fail (namespace != NULL, FALSE); g_return_val_if_fail (namespace != NULL, FALSE);
repository = get_repository (repository);
typelib = get_registered_status (repository, namespace, version, allow_lazy, typelib = get_registered_status (repository, namespace, version, allow_lazy,
&is_lazy, &version_conflict); &is_lazy, &version_conflict);
if (typelib) if (typelib)
@ -1768,6 +1772,8 @@ require_internal (GIRepository *repository,
g_clear_error (&temp_error); g_clear_error (&temp_error);
goto out; goto out;
} }
typelib->library_paths = (repository->library_paths != NULL) ? g_ptr_array_ref (repository->library_paths) : NULL;
} }
header = (Header *) typelib->data; header = (Header *) typelib->data;
typelib_namespace = gi_typelib_get_string (typelib, header->namespace); typelib_namespace = gi_typelib_get_string (typelib, header->namespace);
@ -1809,8 +1815,7 @@ require_internal (GIRepository *repository,
/** /**
* gi_repository_require: * gi_repository_require:
* @repository: (nullable): A #GIRepository, or `NULL` for the singleton * @repository: A #GIRepository
* process-global default #GIRepository
* @namespace_: GI namespace to use, e.g. `Gtk` * @namespace_: GI namespace to use, e.g. `Gtk`
* @version: (nullable): Version of namespace, may be `NULL` for latest * @version: (nullable): Version of namespace, may be `NULL` for latest
* @flags: Set of [flags@GIRepository.RepositoryLoadFlags], may be 0 * @flags: Set of [flags@GIRepository.RepositoryLoadFlags], may be 0
@ -1836,18 +1841,16 @@ gi_repository_require (GIRepository *repository,
{ {
GITypelib *typelib; GITypelib *typelib;
init_globals ();
typelib = require_internal (repository, namespace, version, flags, typelib = require_internal (repository, namespace, version, flags,
(const char * const *) typelib_search_path->pdata, (const char * const *) repository->typelib_search_path->pdata,
typelib_search_path->len, error); repository->typelib_search_path->len, error);
return typelib; return typelib;
} }
/** /**
* gi_repository_require_private: * gi_repository_require_private:
* @repository: (nullable): A #GIRepository, or `NULL` for the singleton * @repository: A #GIRepository
* process-global default #GIRepository
* @typelib_dir: (type filename): Private directory where to find the requested * @typelib_dir: (type filename): Private directory where to find the requested
* typelib * typelib
* @namespace_: GI namespace to use, e.g. `Gtk` * @namespace_: GI namespace to use, e.g. `Gtk`

View File

@ -76,20 +76,24 @@ typedef enum
/* Repository */ /* Repository */
GI_AVAILABLE_IN_ALL
GIRepository *gi_repository_get_default (void);
GI_AVAILABLE_IN_ALL GI_AVAILABLE_IN_ALL
GIRepository *gi_repository_new (void); GIRepository *gi_repository_new (void);
GI_AVAILABLE_IN_ALL GI_AVAILABLE_IN_ALL
void gi_repository_prepend_search_path (const char *directory); void gi_repository_prepend_search_path (GIRepository *repository,
const char *directory);
GI_AVAILABLE_IN_ALL GI_AVAILABLE_IN_ALL
void gi_repository_prepend_library_path (const char *directory); void gi_repository_prepend_library_path (GIRepository *repository,
const char *directory);
GI_AVAILABLE_IN_ALL GI_AVAILABLE_IN_ALL
const char * const * gi_repository_get_search_path (size_t *n_paths_out); const char * const * gi_repository_get_search_path (GIRepository *repository,
size_t *n_paths_out);
GI_AVAILABLE_IN_ALL
const char * const *gi_repository_get_library_path (GIRepository *repository,
size_t *n_paths_out);
GI_AVAILABLE_IN_ALL GI_AVAILABLE_IN_ALL
const char * gi_repository_load_typelib (GIRepository *repository, const char * gi_repository_load_typelib (GIRepository *repository,

View File

@ -1335,10 +1335,10 @@ gi_ir_writer_write (const char *filename,
FILE *ofile; FILE *ofile;
size_t i, j; size_t i, j;
char **dependencies; char **dependencies;
GIRepository *repository; GIRepository *repository = NULL;
Xml *xml; Xml *xml;
repository = gi_repository_get_default (); repository = gi_repository_new ();
if (filename == NULL) if (filename == NULL)
ofile = stdout; ofile = stdout;
@ -1462,4 +1462,6 @@ gi_ir_writer_write (const char *filename,
xml_end_element (xml, "repository"); xml_end_element (xml, "repository");
xml_free (xml); xml_free (xml);
g_clear_object (&repository);
} }

View File

@ -1318,6 +1318,7 @@ struct _GITypelib {
GBytes *bytes; /* (owned) */ GBytes *bytes; /* (owned) */
GList *modules; GList *modules;
gboolean open_attempted; gboolean open_attempted;
GPtrArray *library_paths; /* (element-type filename) (owned) (nullable) */
}; };
DirEntry *gi_typelib_get_dir_entry (GITypelib *typelib, DirEntry *gi_typelib_get_dir_entry (GITypelib *typelib,

View File

@ -2213,36 +2213,6 @@ gi_typelib_error_quark (void)
return quark; return quark;
} }
static GSList *library_paths;
/**
* gi_repository_prepend_library_path:
* @directory: (type filename): a single directory to scan for shared libraries
*
* Prepends @directory to the search path that is used to
* search shared libraries referenced by imported namespaces.
*
* Multiple calls to this function all contribute to the final
* list of paths.
*
* The list of paths is unique and shared for all
* [class@GIRepository.Repository] instances across the process, but it doesnt
* affect namespaces imported before the call.
*
* If the library is not found in the directories configured
* in this way, loading will fall back to the system library
* path (i.e. `LD_LIBRARY_PATH` and `DT_RPATH` in ELF systems).
* See the documentation of your dynamic linker for full details.
*
* Since: 2.80
*/
void
gi_repository_prepend_library_path (const char *directory)
{
library_paths = g_slist_prepend (library_paths,
g_strdup (directory));
}
/* Note on the GModule flags used by this function: /* Note on the GModule flags used by this function:
* Glade's autoconnect feature and OpenGL's extension mechanism * Glade's autoconnect feature and OpenGL's extension mechanism
@ -2254,9 +2224,9 @@ gi_repository_prepend_library_path (const char *directory)
* load modules globally for now. * load modules globally for now.
*/ */
static GModule * static GModule *
load_one_shared_library (const char *shlib) load_one_shared_library (GITypelib *typelib,
const char *shlib)
{ {
GSList *p;
GModule *m; GModule *m;
#ifdef __APPLE__ #ifdef __APPLE__
@ -2272,9 +2242,9 @@ load_one_shared_library (const char *shlib)
#endif #endif
{ {
/* First try in configured library paths */ /* First try in configured library paths */
for (p = library_paths; p; p = p->next) for (unsigned int i = 0; typelib->library_paths != NULL && i < typelib->library_paths->len; i++)
{ {
char *path = g_build_filename (p->data, shlib, NULL); char *path = g_build_filename (typelib->library_paths->pdata[i], shlib, NULL);
m = g_module_open (path, G_MODULE_BIND_LAZY); m = g_module_open (path, G_MODULE_BIND_LAZY);
@ -2319,7 +2289,7 @@ gi_typelib_do_dlopen (GITypelib *typelib)
{ {
GModule *module; GModule *module;
module = load_one_shared_library (shlibs[i]); module = load_one_shared_library (typelib, shlibs[i]);
if (module == NULL) if (module == NULL)
{ {
@ -2404,6 +2374,8 @@ gi_typelib_free (GITypelib *typelib)
{ {
g_clear_pointer (&typelib->bytes, g_bytes_unref); g_clear_pointer (&typelib->bytes, g_bytes_unref);
g_clear_pointer (&typelib->library_paths, g_ptr_array_unref);
if (typelib->modules) if (typelib->modules)
{ {
g_list_foreach (typelib->modules, (GFunc) (void *) g_module_close, NULL); g_list_foreach (typelib->modules, (GFunc) (void *) g_module_close, NULL);

View File

@ -22,35 +22,16 @@
#include "glib.h" #include "glib.h"
#include "girepository.h" #include "girepository.h"
/* Keep this test first, not to add search paths to other tests */
static void
test_repository_search_paths_unset (void)
{
const char * const *search_paths;
size_t n_search_paths;
search_paths = gi_repository_get_search_path (&n_search_paths);
g_assert_nonnull (search_paths);
g_assert_cmpstrv (search_paths, ((char *[]){NULL}));
g_assert_cmpuint (n_search_paths, ==, 0);
search_paths = gi_repository_get_search_path (NULL);
g_assert_cmpuint (g_strv_length ((char **) search_paths), ==, 0);
}
static void static void
test_repository_search_paths_default (void) test_repository_search_paths_default (void)
{ {
const char * const *search_paths; const char * const *search_paths;
size_t n_search_paths; size_t n_search_paths;
GIRepository *repository = NULL;
search_paths = gi_repository_get_search_path (&n_search_paths); repository = gi_repository_new ();
g_assert_nonnull (search_paths);
/* Init default paths */ search_paths = gi_repository_get_search_path (repository, &n_search_paths);
g_assert_nonnull (gi_repository_get_default ());
search_paths = gi_repository_get_search_path (&n_search_paths);
g_assert_nonnull (search_paths); g_assert_nonnull (search_paths);
g_assert_cmpuint (g_strv_length ((char **) search_paths), ==, 2); g_assert_cmpuint (g_strv_length ((char **) search_paths), ==, 2);
@ -61,6 +42,8 @@ test_repository_search_paths_default (void)
g_assert_cmpstr (search_paths[1], ==, expected_path); g_assert_cmpstr (search_paths[1], ==, expected_path);
g_clear_pointer (&expected_path, g_free); g_clear_pointer (&expected_path, g_free);
#endif #endif
g_clear_object (&repository);
} }
static void static void
@ -68,9 +51,12 @@ test_repository_search_paths_prepend (void)
{ {
const char * const *search_paths; const char * const *search_paths;
size_t n_search_paths; size_t n_search_paths;
GIRepository *repository = NULL;
gi_repository_prepend_search_path (g_test_get_dir (G_TEST_BUILT)); repository = gi_repository_new ();
search_paths = gi_repository_get_search_path (&n_search_paths);
gi_repository_prepend_search_path (repository, g_test_get_dir (G_TEST_BUILT));
search_paths = gi_repository_get_search_path (repository, &n_search_paths);
g_assert_nonnull (search_paths); g_assert_nonnull (search_paths);
g_assert_cmpuint (g_strv_length ((char **) search_paths), ==, 3); g_assert_cmpuint (g_strv_length ((char **) search_paths), ==, 3);
@ -83,8 +69,8 @@ test_repository_search_paths_prepend (void)
g_clear_pointer (&expected_path, g_free); g_clear_pointer (&expected_path, g_free);
#endif #endif
gi_repository_prepend_search_path (g_test_get_dir (G_TEST_DIST)); gi_repository_prepend_search_path (repository, g_test_get_dir (G_TEST_DIST));
search_paths = gi_repository_get_search_path (&n_search_paths); search_paths = gi_repository_get_search_path (repository, &n_search_paths);
g_assert_nonnull (search_paths); g_assert_nonnull (search_paths);
g_assert_cmpuint (g_strv_length ((char **) search_paths), ==, 4); g_assert_cmpuint (g_strv_length ((char **) search_paths), ==, 4);
@ -97,6 +83,51 @@ test_repository_search_paths_prepend (void)
g_assert_cmpstr (search_paths[3], ==, expected_path); g_assert_cmpstr (search_paths[3], ==, expected_path);
g_clear_pointer (&expected_path, g_free); g_clear_pointer (&expected_path, g_free);
#endif #endif
g_clear_object (&repository);
}
static void
test_repository_library_paths_default (void)
{
const char * const *library_paths;
size_t n_library_paths;
GIRepository *repository = NULL;
repository = gi_repository_new ();
library_paths = gi_repository_get_library_path (repository, &n_library_paths);
g_assert_nonnull (library_paths);
g_assert_cmpuint (g_strv_length ((char **) library_paths), ==, 0);
g_clear_object (&repository);
}
static void
test_repository_library_paths_prepend (void)
{
const char * const *library_paths;
size_t n_library_paths;
GIRepository *repository = NULL;
repository = gi_repository_new ();
gi_repository_prepend_library_path (repository, g_test_get_dir (G_TEST_BUILT));
library_paths = gi_repository_get_library_path (repository, &n_library_paths);
g_assert_nonnull (library_paths);
g_assert_cmpuint (g_strv_length ((char **) library_paths), ==, 1);
g_assert_cmpstr (library_paths[0], ==, g_test_get_dir (G_TEST_BUILT));
gi_repository_prepend_library_path (repository, g_test_get_dir (G_TEST_DIST));
library_paths = gi_repository_get_library_path (repository, &n_library_paths);
g_assert_nonnull (library_paths);
g_assert_cmpuint (g_strv_length ((char **) library_paths), ==, 2);
g_assert_cmpstr (library_paths[0], ==, g_test_get_dir (G_TEST_DIST));
g_assert_cmpstr (library_paths[1], ==, g_test_get_dir (G_TEST_BUILT));
g_clear_object (&repository);
} }
int int
@ -109,9 +140,10 @@ main (int argc,
g_setenv ("GI_TYPELIB_PATH", g_get_tmp_dir (), TRUE); g_setenv ("GI_TYPELIB_PATH", g_get_tmp_dir (), TRUE);
g_setenv ("GI_GIR_PATH", g_get_user_cache_dir (), TRUE); g_setenv ("GI_GIR_PATH", g_get_user_cache_dir (), TRUE);
g_test_add_func ("/repository-search-paths/unset", test_repository_search_paths_unset); g_test_add_func ("/repository/search-paths/default", test_repository_search_paths_default);
g_test_add_func ("/repository-search-paths/default", test_repository_search_paths_default); g_test_add_func ("/repository/search-paths/prepend", test_repository_search_paths_prepend);
g_test_add_func ("/repository-search-paths/prepend", test_repository_search_paths_prepend); g_test_add_func ("/repository/library-paths/default", test_repository_library_paths_default);
g_test_add_func ("/repository/library-paths/prepend", test_repository_library_paths_prepend);
return g_test_run (); return g_test_run ();
} }

View File

@ -59,7 +59,7 @@ test_repository_basic (RepositoryFixture *fx,
g_assert_cmpstrv (versions, ((char *[]){"2.0", NULL})); g_assert_cmpstrv (versions, ((char *[]){"2.0", NULL}));
g_clear_pointer (&versions, g_strfreev); g_clear_pointer (&versions, g_strfreev);
search_paths = gi_repository_get_search_path (NULL); search_paths = gi_repository_get_search_path (fx->repository, NULL);
g_assert_nonnull (search_paths); g_assert_nonnull (search_paths);
g_assert_cmpuint (g_strv_length ((char **) search_paths), >, 0); g_assert_cmpuint (g_strv_length ((char **) search_paths), >, 0);
g_assert_cmpstr (search_paths[0], ==, fx->gobject_typelib_dir); g_assert_cmpstr (search_paths[0], ==, fx->gobject_typelib_dir);

View File

@ -43,13 +43,13 @@ repository_setup (RepositoryFixture *fx,
GError *local_error = NULL; GError *local_error = NULL;
TypelibLoadSpec *load_spec = (TypelibLoadSpec *) data; TypelibLoadSpec *load_spec = (TypelibLoadSpec *) data;
fx->gobject_typelib_dir = g_test_build_filename (G_TEST_BUILT, "..", "..", "introspection", NULL);
g_test_message ("Using GI_TYPELIB_DIR = %s", fx->gobject_typelib_dir);
gi_repository_prepend_search_path (fx->gobject_typelib_dir);
fx->repository = gi_repository_new (); fx->repository = gi_repository_new ();
g_assert_nonnull (fx->repository); g_assert_nonnull (fx->repository);
fx->gobject_typelib_dir = g_test_build_filename (G_TEST_BUILT, "..", "..", "introspection", NULL);
g_test_message ("Using GI_TYPELIB_DIR = %s", fx->gobject_typelib_dir);
gi_repository_prepend_search_path (fx->repository, fx->gobject_typelib_dir);
if (load_spec) if (load_spec)
{ {
typelib = gi_repository_require (fx->repository, load_spec->name, load_spec->version, 0, &local_error); typelib = gi_repository_require (fx->repository, load_spec->name, load_spec->version, 0, &local_error);