resources: Support C23 #embed

Make it possible for glib-compile-resources to generate a source
file that uses C23 #embed to include the resource data.
This commit is contained in:
Matthias Clasen 2024-10-21 23:32:36 -04:00
parent 984224263d
commit 46d32bc141
2 changed files with 25 additions and 11 deletions

View File

@ -133,6 +133,13 @@ OPTIONS
as a string literal. When ``--external-data`` is given, the data is only
declared in the generated C file, and the data has to be linked externally.
``--embed-data=FILENAME``
By default, code generated by ``--generate-source`` embeds the resource data
as a string literal. When ``--embed-data`` is given, the data is included in
the generated C file via a C23 #embed directive. ``FILENAME`` is the name
of the file that contains the resource data.
``--dependency-file <FILE>``
Write dependencies in the same style as ``gcc -M -MF`` to the given file. If
@ -178,4 +185,4 @@ ENVIRONMENT
The full path to the ``json-glib-format`` executable. This is used to
preprocess resources with the ``json-stripblanks`` preprocessing option. If
this environment variable is not set, ``json-glib-format`` is searched for in
the ``PATH``.
the ``PATH``.

View File

@ -808,6 +808,7 @@ main (int argc, char **argv)
gboolean manual_register = FALSE;
gboolean internal = FALSE;
gboolean external_data = FALSE;
char *embed_data = NULL;
gboolean generate_dependencies = FALSE;
gboolean generate_phony_targets = FALSE;
char *dependency_file = NULL;
@ -830,6 +831,7 @@ main (int argc, char **argv)
{ "manual-register", 0, 0, G_OPTION_ARG_NONE, &manual_register, N_("Dont automatically create and register resource"), NULL },
{ "internal", 0, 0, G_OPTION_ARG_NONE, &internal, N_("Dont export functions; declare them G_GNUC_INTERNAL"), NULL },
{ "external-data", 0, 0, G_OPTION_ARG_NONE, &external_data, N_("Dont embed resource data in the C file; assume it's linked externally instead"), NULL },
{ "embed-data", 0, 0, G_OPTION_ARG_FILENAME, &embed_data, N_("Don't embed resource data in the C fil; use C23 #embed instead"), N_("FILENAME") },
{ "c-name", 0, 0, G_OPTION_ARG_STRING, &c_name, N_("C identifier name used for the generated source code"), N_("IDENTIFIER") },
{ "compiler", 'C', 0, G_OPTION_ARG_STRING, &compiler, N_("The target C compiler (default: the CC environment variable)"), N_("COMMAND") },
G_OPTION_ENTRY_NULL
@ -1070,16 +1072,16 @@ main (int argc, char **argv)
{
if (generate_source)
{
int fd = g_file_open_tmp (NULL, &binary_target, NULL);
if (fd == -1)
{
g_printerr ("Can't open temp file\n");
g_free (c_name);
int fd = g_file_open_tmp (NULL, &binary_target, NULL);
if (fd == -1)
{
g_printerr ("Can't open temp file\n");
g_free (c_name);
g_hash_table_unref (files);
return 1;
}
close (fd);
}
return 1;
}
close (fd);
}
if (c_name == NULL)
{
@ -1210,6 +1212,11 @@ main (int argc, char **argv)
"\n",
export, data_size, c_name);
}
else if (embed_data)
{
g_fprintf (file,
"static const union { const double alignment; void * const ptr; const guint8 data[%" G_GSIZE_FORMAT "];} %s_resource_data = { .data = {\n#embed \"%s\"\n}};\n", data_size, c_name, embed_data);
}
else
{
if (compiler_type == COMPILER_MSVC || compiler_type == COMPILER_UNKNOWN)
@ -1259,7 +1266,7 @@ main (int argc, char **argv)
"{\n"
" return g_static_resource_get_resource (&static_resource);\n"
"}\n",
c_name, c_name, (external_data ? "" : " - 1 /* nul terminator */"),
c_name, c_name, ((external_data || embed_data) ? "" : " - 1 /* nul terminator */"),
export, c_name, c_name);