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`.
*
* 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.
* 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
*/
@ -75,9 +75,6 @@
#define GIREPOSITORY_TYPELIB_FILENAME \
GIREPOSITORY_TYPELIB_NAME "-" GIREPOSITORY_TYPELIB_VERSION ".typelib"
static GIRepository *default_repository = NULL;
static GPtrArray *typelib_search_path = NULL;
typedef struct {
size_t n_interfaces;
GIBaseInfo *interfaces[];
@ -97,6 +94,9 @@ struct _GIRepository
{
GObject parent;
GPtrArray *typelib_search_path; /* (element-type filename) (owned) */
GPtrArray *library_paths; /* (element-type filename) (owned) */
GHashTable *typelibs; /* (string) namespace -> GITypelib */
GHashTable *lazy_typelibs; /* (string) namespace-version -> GITypelib */
GHashTable *info_by_gtype; /* GType -> GIBaseInfo */
@ -148,6 +148,40 @@ DllMain (HINSTANCE hinstDLL,
static void
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
= g_hash_table_new_full (g_str_hash, g_str_equal,
(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->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));
}
@ -198,54 +235,9 @@ gi_repository_class_init (GIRepositoryClass *class)
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:
* @repository: A #GIRepository
* @directory: (type filename): directory name to prepend to the typelib
* search path
*
@ -256,14 +248,17 @@ init_globals (void)
* Since: 2.80
*/
void
gi_repository_prepend_search_path (const char *directory)
gi_repository_prepend_search_path (GIRepository *repository,
const char *directory)
{
init_globals ();
g_ptr_array_insert (typelib_search_path, 0, g_strdup (directory));
g_return_if_fail (GI_IS_REPOSITORY (repository));
g_ptr_array_insert (repository->typelib_search_path, 0, g_strdup (directory));
}
/**
* gi_repository_get_search_path:
* @repository: A #GIRepository
* @n_paths_out: (optional) (out): The number of search paths returned.
*
* 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
*/
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};
@ -290,9 +289,77 @@ gi_repository_get_search_path (size_t *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 *
@ -321,17 +388,6 @@ get_typelib_dependencies (GITypelib *typelib)
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 *
check_version_conflict (GITypelib *typelib,
const char *namespace,
@ -372,7 +428,7 @@ get_registered_status (GIRepository *repository,
char **version_conflict)
{
GITypelib *typelib;
repository = get_repository (repository);
if (lazy_status)
*lazy_status = FALSE;
typelib = g_hash_table_lookup (repository->typelibs, namespace);
@ -489,8 +545,7 @@ register_internal (GIRepository *repository,
/**
* gi_repository_get_immediate_dependencies:
* @repository: (nullable): A #GIRepository, or `NULL` for the singleton
* process-global default #GIRepository
* @repository: A #GIRepository
* @namespace_: Namespace of interest
*
* Return an array of the immediate versioned dependencies for @namespace_.
@ -514,10 +569,9 @@ gi_repository_get_immediate_dependencies (GIRepository *repository,
GITypelib *typelib;
char **deps;
g_return_val_if_fail (GI_IS_REPOSITORY (repository), NULL);
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);
@ -571,8 +625,7 @@ get_typelib_dependencies_transitive (GIRepository *repository,
/**
* gi_repository_get_dependencies:
* @repository: (nullable): A #GIRepository, or `NULL` for the singleton
* process-global default #GIRepository
* @repository: A #GIRepository
* @namespace_: Namespace of interest
*
* Retrieves all (transitive) versioned dependencies for
@ -601,10 +654,9 @@ gi_repository_get_dependencies (GIRepository *repository,
char *dependency;
GPtrArray *out; /* owned utf8 elements */
g_return_val_if_fail (GI_IS_REPOSITORY (repository), NULL);
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);
@ -632,8 +684,7 @@ gi_repository_get_dependencies (GIRepository *repository,
/**
* gi_repository_load_typelib:
* @repository: (nullable): A #GIRepository, or `NULL` for the singleton
* process-global default #GIRepository
* @repository: A #GIRepository
* @typelib: the typelib to load
* @flags: flags affecting the loading operation
* @error: return location for a [type@GLib.Error], or `NULL`
@ -656,7 +707,7 @@ gi_repository_load_typelib (GIRepository *repository,
gboolean is_lazy;
char *version_conflict;
repository = get_repository (repository);
g_return_val_if_fail (GI_IS_REPOSITORY (repository), NULL);
header = (Header *) typelib->data;
namespace = gi_typelib_get_string (typelib, header->namespace);
@ -681,8 +732,7 @@ gi_repository_load_typelib (GIRepository *repository,
/**
* gi_repository_is_registered:
* @repository: (nullable): A #GIRepository, or `NULL` for the singleton
* process-global default #GIRepository
* @repository: A #GIRepository
* @namespace_: Namespace of interest
* @version: (nullable): Required version, may be `NULL` for latest
*
@ -703,41 +753,15 @@ gi_repository_is_registered (GIRepository *repository,
const char *namespace,
const char *version)
{
repository = get_repository (repository);
return get_registered (repository, namespace, version) != NULL;
}
g_return_val_if_fail (GI_IS_REPOSITORY (repository), FALSE);
/**
* 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);
return get_registered (repository, namespace, version) != NULL;
}
/**
* gi_repository_new:
*
* Create a new (non-singleton) [class@GIRepository.Repository].
*
* Most callers should use [func@GIRepository.Repository.get_default] instead,
* as a singleton repository is more useful in most situations.
* Create a new [class@GIRepository.Repository].
*
* Returns: (transfer full): a new [class@GIRepository.Repository]
* Since: 2.80
@ -750,8 +774,7 @@ gi_repository_new (void)
/**
* gi_repository_get_n_infos:
* @repository: (nullable): A #GIRepository, or `NULL` for the singleton
* process-global default #GIRepository
* @repository: A #GIRepository
* @namespace_: Namespace to inspect
*
* This function returns the number of metadata entries in
@ -769,10 +792,9 @@ gi_repository_get_n_infos (GIRepository *repository,
GITypelib *typelib;
unsigned int n_interfaces = 0;
g_return_val_if_fail (GI_IS_REPOSITORY (repository), -1);
g_return_val_if_fail (namespace != NULL, -1);
repository = get_repository (repository);
typelib = get_registered (repository, namespace, NULL);
g_return_val_if_fail (typelib != NULL, -1);
@ -784,8 +806,7 @@ gi_repository_get_n_infos (GIRepository *repository,
/**
* gi_repository_get_info:
* @repository: (nullable): A #GIRepository, or `NULL` for the singleton
* process-global default #GIRepository
* @repository: A #GIRepository
* @namespace_: Namespace to inspect
* @idx: 0-based offset into namespace metadata for entry
*
@ -808,11 +829,10 @@ gi_repository_get_info (GIRepository *repository,
GITypelib *typelib;
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 (idx < G_MAXUINT16, NULL);
repository = get_repository (repository);
typelib = get_registered (repository, namespace, 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:
* @repository: (nullable): A #GIRepository, or `NULL` for the singleton
* process-global default #GIRepository
* @repository: A #GIRepository
* @gtype: [type@GObject.Type] to search for
*
* Searches all loaded namespaces for a particular [type@GObject.Type].
@ -884,10 +903,9 @@ gi_repository_find_by_gtype (GIRepository *repository,
GIBaseInfo *cached;
DirEntry *entry;
g_return_val_if_fail (GI_IS_REPOSITORY (repository), NULL);
g_return_val_if_fail (gtype != G_TYPE_INVALID, NULL);
repository = get_repository (repository);
cached = g_hash_table_lookup (repository->info_by_gtype,
(gpointer)gtype);
@ -941,8 +959,7 @@ gi_repository_find_by_gtype (GIRepository *repository,
/**
* gi_repository_find_by_name:
* @repository: (nullable): A #GIRepository, or `NULL` for the singleton
* process-global default #GIRepository
* @repository: A #GIRepository
* @namespace_: Namespace which will be searched
* @name: Entry name to find
*
@ -964,9 +981,9 @@ gi_repository_find_by_name (GIRepository *repository,
GITypelib *typelib;
DirEntry *entry;
g_return_val_if_fail (GI_IS_REPOSITORY (repository), NULL);
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);
@ -1004,8 +1021,7 @@ find_by_error_domain_foreach (gpointer key,
/**
* gi_repository_find_by_error_domain:
* @repository: (nullable): A #GIRepository, or `NULL` for the singleton
* process-global default #GIRepository
* @repository: A #GIRepository
* @domain: a [type@GLib.Error] domain
*
* 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;
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,
GUINT_TO_POINTER (domain));
@ -1059,7 +1075,7 @@ gi_repository_find_by_error_domain (GIRepository *repository,
/**
* 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`
* @n_interfaces_out: (out): Number of interfaces
* @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;
g_return_if_fail (GI_IS_REPOSITORY (repository));
g_return_if_fail (g_type_fundamental (gtype) == G_TYPE_OBJECT);
repository = get_repository (repository);
cache = g_hash_table_lookup (repository->interfaces_for_gtype,
(void *) gtype);
if (cache == NULL)
@ -1150,8 +1165,7 @@ collect_namespaces (gpointer key,
/**
* gi_repository_get_loaded_namespaces:
* @repository: (nullable): A #GIRepository, or `NULL` for the singleton
* process-global default #GIRepository
* @repository: A #GIRepository
*
* Return the list of currently loaded namespaces.
*
@ -1166,7 +1180,7 @@ gi_repository_get_loaded_namespaces (GIRepository *repository)
char **names;
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->lazy_typelibs, collect_namespaces, &list);
@ -1182,8 +1196,7 @@ gi_repository_get_loaded_namespaces (GIRepository *repository)
/**
* gi_repository_get_version:
* @repository: (nullable): A #GIRepository, or `NULL` for the singleton
* process-global default #GIRepository
* @repository: A #GIRepository
* @namespace_: Namespace to inspect
*
* This function returns the loaded version associated with the given
@ -1203,10 +1216,9 @@ gi_repository_get_version (GIRepository *repository,
GITypelib *typelib;
Header *header;
g_return_val_if_fail (GI_IS_REPOSITORY (repository), NULL);
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);
@ -1217,8 +1229,7 @@ gi_repository_get_version (GIRepository *repository,
/**
* gi_repository_get_shared_libraries:
* @repository: (nullable): A #GIRepository, or `NULL` for the singleton
* process-global default #GIRepository
* @repository: A #GIRepository
* @namespace_: Namespace to inspect
* @out_n_elements: (out) (optional): Return location for the number of elements
* in the returned array
@ -1248,10 +1259,9 @@ gi_repository_get_shared_libraries (GIRepository *repository,
GITypelib *typelib;
Header *header;
g_return_val_if_fail (GI_IS_REPOSITORY (repository), NULL);
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);
@ -1284,8 +1294,7 @@ gi_repository_get_shared_libraries (GIRepository *repository,
/**
* gi_repository_get_c_prefix:
* @repository: (nullable): A #GIRepository, or `NULL` for the singleton
* process-global default #GIRepository
* @repository: A #GIRepository
* @namespace_: Namespace to inspect
*
* This function returns the C prefix, or the C level namespace
@ -1308,10 +1317,9 @@ gi_repository_get_c_prefix (GIRepository *repository,
GITypelib *typelib;
Header *header;
g_return_val_if_fail (GI_IS_REPOSITORY (repository), NULL);
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);
@ -1325,8 +1333,7 @@ gi_repository_get_c_prefix (GIRepository *repository,
/**
* gi_repository_get_typelib_path:
* @repository: (nullable): A #GIRepository, or `NULL` for the singleton
* process-global default #GIRepository
* @repository: A #GIRepository
* @namespace_: GI namespace to use, e.g. `Gtk`
*
* 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;
repository = get_repository (repository);
g_return_val_if_fail (GI_IS_REPOSITORY (repository), NULL);
if (!g_hash_table_lookup_extended (repository->typelibs, namespace,
&orig_key, &value))
@ -1626,8 +1633,7 @@ find_namespace_latest (const char *namespace,
/**
* gi_repository_enumerate_versions:
* @repository: (nullable): A #GIRepository, or `NULL` for the singleton
* process-global default #GIRepository
* @repository: A #GIRepository
* @namespace_: GI namespace, e.g. `Gtk`
* @n_versions_out: (optional) (out): The number of versions returned.
*
@ -1647,10 +1653,9 @@ gi_repository_enumerate_versions (GIRepository *repository,
const char *loaded_version;
char **ret;
init_globals ();
candidates = enumerate_namespace_versions (namespace_,
(const char * const *) typelib_search_path->pdata,
typelib_search_path->len);
(const char * const *) repository->typelib_search_path->pdata,
repository->typelib_search_path->len);
if (!candidates)
{
@ -1706,10 +1711,9 @@ require_internal (GIRepository *repository,
char *path = NULL;
char *tmp_version = NULL;
g_return_val_if_fail (GI_IS_REPOSITORY (repository), NULL);
g_return_val_if_fail (namespace != NULL, FALSE);
repository = get_repository (repository);
typelib = get_registered_status (repository, namespace, version, allow_lazy,
&is_lazy, &version_conflict);
if (typelib)
@ -1768,6 +1772,8 @@ require_internal (GIRepository *repository,
g_clear_error (&temp_error);
goto out;
}
typelib->library_paths = (repository->library_paths != NULL) ? g_ptr_array_ref (repository->library_paths) : NULL;
}
header = (Header *) typelib->data;
typelib_namespace = gi_typelib_get_string (typelib, header->namespace);
@ -1809,8 +1815,7 @@ require_internal (GIRepository *repository,
/**
* gi_repository_require:
* @repository: (nullable): A #GIRepository, or `NULL` for the singleton
* process-global default #GIRepository
* @repository: A #GIRepository
* @namespace_: GI namespace to use, e.g. `Gtk`
* @version: (nullable): Version of namespace, may be `NULL` for latest
* @flags: Set of [flags@GIRepository.RepositoryLoadFlags], may be 0
@ -1836,18 +1841,16 @@ gi_repository_require (GIRepository *repository,
{
GITypelib *typelib;
init_globals ();
typelib = require_internal (repository, namespace, version, flags,
(const char * const *) typelib_search_path->pdata,
typelib_search_path->len, error);
(const char * const *) repository->typelib_search_path->pdata,
repository->typelib_search_path->len, error);
return typelib;
}
/**
* gi_repository_require_private:
* @repository: (nullable): A #GIRepository, or `NULL` for the singleton
* process-global default #GIRepository
* @repository: A #GIRepository
* @typelib_dir: (type filename): Private directory where to find the requested
* typelib
* @namespace_: GI namespace to use, e.g. `Gtk`

View File

@ -76,20 +76,24 @@ typedef enum
/* Repository */
GI_AVAILABLE_IN_ALL
GIRepository *gi_repository_get_default (void);
GI_AVAILABLE_IN_ALL
GIRepository *gi_repository_new (void);
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
void gi_repository_prepend_library_path (const char *directory);
void gi_repository_prepend_library_path (GIRepository *repository,
const char *directory);
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
const char * gi_repository_load_typelib (GIRepository *repository,

View File

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

View File

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

View File

@ -2213,36 +2213,6 @@ gi_typelib_error_quark (void)
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:
* 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.
*/
static GModule *
load_one_shared_library (const char *shlib)
load_one_shared_library (GITypelib *typelib,
const char *shlib)
{
GSList *p;
GModule *m;
#ifdef __APPLE__
@ -2272,9 +2242,9 @@ load_one_shared_library (const char *shlib)
#endif
{
/* 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);
@ -2319,7 +2289,7 @@ gi_typelib_do_dlopen (GITypelib *typelib)
{
GModule *module;
module = load_one_shared_library (shlibs[i]);
module = load_one_shared_library (typelib, shlibs[i]);
if (module == NULL)
{
@ -2404,6 +2374,8 @@ gi_typelib_free (GITypelib *typelib)
{
g_clear_pointer (&typelib->bytes, g_bytes_unref);
g_clear_pointer (&typelib->library_paths, g_ptr_array_unref);
if (typelib->modules)
{
g_list_foreach (typelib->modules, (GFunc) (void *) g_module_close, NULL);

View File

@ -22,35 +22,16 @@
#include "glib.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
test_repository_search_paths_default (void)
{
const char * const *search_paths;
size_t n_search_paths;
GIRepository *repository = NULL;
search_paths = gi_repository_get_search_path (&n_search_paths);
g_assert_nonnull (search_paths);
repository = gi_repository_new ();
/* Init default paths */
g_assert_nonnull (gi_repository_get_default ());
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_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_clear_pointer (&expected_path, g_free);
#endif
g_clear_object (&repository);
}
static void
@ -68,9 +51,12 @@ test_repository_search_paths_prepend (void)
{
const char * const *search_paths;
size_t n_search_paths;
GIRepository *repository = NULL;
gi_repository_prepend_search_path (g_test_get_dir (G_TEST_BUILT));
search_paths = gi_repository_get_search_path (&n_search_paths);
repository = gi_repository_new ();
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_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);
#endif
gi_repository_prepend_search_path (g_test_get_dir (G_TEST_DIST));
search_paths = gi_repository_get_search_path (&n_search_paths);
gi_repository_prepend_search_path (repository, g_test_get_dir (G_TEST_DIST));
search_paths = gi_repository_get_search_path (repository, &n_search_paths);
g_assert_nonnull (search_paths);
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_clear_pointer (&expected_path, g_free);
#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
@ -109,9 +140,10 @@ main (int argc,
g_setenv ("GI_TYPELIB_PATH", g_get_tmp_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/prepend", test_repository_search_paths_prepend);
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/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 ();
}

View File

@ -59,7 +59,7 @@ test_repository_basic (RepositoryFixture *fx,
g_assert_cmpstrv (versions, ((char *[]){"2.0", NULL}));
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_cmpuint (g_strv_length ((char **) search_paths), >, 0);
g_assert_cmpstr (search_paths[0], ==, fx->gobject_typelib_dir);

View File

@ -43,13 +43,13 @@ repository_setup (RepositoryFixture *fx,
GError *local_error = NULL;
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 ();
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)
{
typelib = gi_repository_require (fx->repository, load_spec->name, load_spec->version, 0, &local_error);