LCOV - code coverage report
Current view: top level - glib/girepository/compiler - compiler.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 0 102 0.0 %
Date: 2024-05-07 05:15:23 Functions: 0 3 0.0 %
Branches: 0 42 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
       2                 :            : 
       3                 :            : /* GObject introspection: Typelib compiler
       4                 :            :  *
       5                 :            :  *
       6                 :            :  * SPDX-License-Identifier: LGPL-2.1-or-later
       7                 :            : 
       8                 :            :  * Copyright (C) 2005 Matthias Clasen
       9                 :            :  * Copyright (C) 2024 GNOME Foundation
      10                 :            :  *
      11                 :            :  * This library is free software; you can redistribute it and/or
      12                 :            :  * modify it under the terms of the GNU Lesser General Public
      13                 :            :  * License as published by the Free Software Foundation; either
      14                 :            :  * version 2.1 of the License, or (at your option) any later version.
      15                 :            :  *
      16                 :            :  * This library is distributed in the hope that it will be useful,
      17                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      18                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      19                 :            :  * Lesser General Public License for more details.
      20                 :            :  *
      21                 :            :  * You should have received a copy of the GNU Lesser General
      22                 :            :  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
      23                 :            :  */
      24                 :            : 
      25                 :            : #include <errno.h>
      26                 :            : #include <locale.h>
      27                 :            : #include <string.h>
      28                 :            : 
      29                 :            : #include <gio/gio.h>
      30                 :            : #include <girepository.h>
      31                 :            : #include <glib.h>
      32                 :            : #include <glib/gi18n.h>
      33                 :            : #include <glib/gstdio.h>
      34                 :            : 
      35                 :            : #ifdef G_OS_WIN32
      36                 :            : #include <fcntl.h>
      37                 :            : #include <io.h>
      38                 :            : #endif
      39                 :            : 
      40                 :            : #include "girmodule-private.h"
      41                 :            : #include "girnode-private.h"
      42                 :            : #include "girparser-private.h"
      43                 :            : 
      44                 :            : static gchar **includedirs = NULL;
      45                 :            : static gchar **input = NULL;
      46                 :            : static gchar *output = NULL;
      47                 :            : static gchar **shlibs = NULL;
      48                 :            : static gboolean debug = FALSE;
      49                 :            : static gboolean verbose = FALSE;
      50                 :            : static gboolean show_version = FALSE;
      51                 :            : 
      52                 :            : static gboolean
      53                 :          0 : write_out_typelib (gchar     *prefix,
      54                 :            :                    GITypelib *typelib)
      55                 :            : {
      56                 :            :   FILE *file;
      57                 :            :   gsize written;
      58                 :            :   GFile *file_obj;
      59                 :            :   gchar *filename;
      60                 :            :   GFile *tmp_file_obj;
      61                 :            :   gchar *tmp_filename;
      62                 :          0 :   GError *error = NULL;
      63                 :          0 :   gboolean success = FALSE;
      64                 :            : 
      65         [ #  # ]:          0 :   if (output == NULL)
      66                 :            :     {
      67                 :          0 :       file = stdout;
      68                 :          0 :       file_obj = NULL;
      69                 :          0 :       filename = NULL;
      70                 :          0 :       tmp_filename = NULL;
      71                 :          0 :       tmp_file_obj = NULL;
      72                 :            : #ifdef G_OS_WIN32
      73                 :            :       setmode (fileno (file), _O_BINARY);
      74                 :            : #endif
      75                 :            :     }
      76                 :            :   else
      77                 :            :     {
      78         [ #  # ]:          0 :       if (prefix)
      79                 :            :         {
      80                 :          0 :           filename = g_strdup_printf ("%s-%s", prefix, output);
      81                 :            :         }
      82                 :            :       else
      83                 :            :         {
      84                 :          0 :           filename = g_strdup (output);
      85                 :            :         }
      86                 :          0 :       file_obj = g_file_new_for_path (filename);
      87                 :          0 :       tmp_filename = g_strdup_printf ("%s.tmp", filename);
      88                 :          0 :       tmp_file_obj = g_file_new_for_path (tmp_filename);
      89                 :          0 :       file = g_fopen (tmp_filename, "wb");
      90                 :            : 
      91         [ #  # ]:          0 :       if (file == NULL)
      92                 :            :         {
      93                 :          0 :           char *message = g_strdup_printf (_("Failed to open ‘%s’: %s"), tmp_filename, g_strerror (errno));
      94                 :          0 :           g_fprintf (stderr, "%s\n", message);
      95                 :          0 :           g_free (message);
      96                 :          0 :           goto out;
      97                 :            :         }
      98                 :            :     }
      99                 :            : 
     100                 :          0 :   written = fwrite (typelib->data, 1, typelib->len, file);
     101         [ #  # ]:          0 :   if (written < typelib->len)
     102                 :            :     {
     103                 :          0 :       char *message = g_strdup_printf (_("Error: Could not write the whole output: %s"), g_strerror (errno));
     104                 :          0 :       g_fprintf (stderr, "%s\n", message);
     105                 :          0 :       g_free (message);
     106                 :          0 :       goto out;
     107                 :            :     }
     108                 :            : 
     109         [ #  # ]:          0 :   if (output != NULL)
     110                 :          0 :     fclose (file);
     111         [ #  # ]:          0 :   if (tmp_filename != NULL)
     112                 :            :     {
     113         [ #  # ]:          0 :       if (!g_file_move (tmp_file_obj, file_obj, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &error))
     114                 :            :         {
     115                 :          0 :           char *message = g_strdup_printf (_("Error: Failed to rename ‘%s’ to ‘%s’: %s"),
     116                 :            :                                            tmp_filename, filename,
     117                 :          0 :                                            error->message);
     118                 :          0 :           g_fprintf (stderr, "%s\n", message);
     119                 :          0 :           g_free (message);
     120                 :          0 :           g_clear_error (&error);
     121                 :          0 :           goto out;
     122                 :            :         }
     123                 :            :     }
     124                 :          0 :   success = TRUE;
     125                 :          0 : out:
     126                 :          0 :   g_clear_object (&file_obj);
     127                 :          0 :   g_clear_object (&tmp_file_obj);
     128                 :          0 :   g_free (filename);
     129                 :          0 :   g_free (tmp_filename);
     130                 :            : 
     131                 :          0 :   return success;
     132                 :            : }
     133                 :            : 
     134                 :            : static GLogLevelFlags logged_levels;
     135                 :            : 
     136                 :            : static void
     137                 :          0 : log_handler (const gchar   *log_domain,
     138                 :            :              GLogLevelFlags log_level,
     139                 :            :              const gchar   *message,
     140                 :            :              gpointer       user_data)
     141                 :            : {
     142         [ #  # ]:          0 :   if (log_level & logged_levels)
     143                 :          0 :     g_log_default_handler (log_domain, log_level, message, user_data);
     144                 :          0 : }
     145                 :            : 
     146                 :            : static GOptionEntry options[] = {
     147                 :            :   { "includedir", 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &includedirs, N_("Include directories in GIR search path"), N_("DIRECTORY") },
     148                 :            :   { "output", 'o', 0, G_OPTION_ARG_FILENAME, &output, N_("Output file"), N_("FILE") },
     149                 :            :   { "shared-library", 'l', 0, G_OPTION_ARG_FILENAME_ARRAY, &shlibs, N_("Shared library"), N_("FILE") },
     150                 :            :   { "debug", 0, 0, G_OPTION_ARG_NONE, &debug, N_("Show debug messages"), NULL },
     151                 :            :   { "verbose", 0, 0, G_OPTION_ARG_NONE, &verbose, N_("Show verbose messages"), NULL },
     152                 :            :   { "version", 0, 0, G_OPTION_ARG_NONE, &show_version, N_("Show program’s version number and exit"), NULL },
     153                 :            :   { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &input, NULL, NULL },
     154                 :            :   G_OPTION_ENTRY_NULL
     155                 :            : };
     156                 :            : 
     157                 :            : int
     158                 :          0 : main (int argc, char **argv)
     159                 :            : {
     160                 :            :   GOptionContext *context;
     161                 :          0 :   GError *error = NULL;
     162                 :            :   GIIrParser *parser;
     163                 :            :   GIIrModule *module;
     164                 :            : 
     165                 :          0 :   setlocale (LC_ALL, "");
     166                 :            : 
     167                 :            :   /* Translators: commandline placeholder */
     168                 :          0 :   context = g_option_context_new (_("FILE"));
     169                 :          0 :   g_option_context_add_main_entries (context, options, NULL);
     170                 :          0 :   g_option_context_parse (context, &argc, &argv, &error);
     171                 :          0 :   g_option_context_free (context);
     172                 :            : 
     173         [ #  # ]:          0 :   if (error)
     174                 :            :     {
     175                 :          0 :       char *message = g_strdup_printf (_("Error parsing arguments: %s"), error->message);
     176                 :          0 :       g_fprintf (stderr, "%s\n", message);
     177                 :          0 :       g_free (message);
     178                 :            : 
     179                 :          0 :       g_error_free (error);
     180                 :            : 
     181                 :          0 :       return 1;
     182                 :            :     }
     183                 :            : 
     184                 :          0 :   logged_levels = G_LOG_LEVEL_MASK & ~(G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_DEBUG);
     185         [ #  # ]:          0 :   if (debug)
     186                 :          0 :     logged_levels = logged_levels | G_LOG_LEVEL_DEBUG;
     187         [ #  # ]:          0 :   if (verbose)
     188                 :          0 :     logged_levels = logged_levels | G_LOG_LEVEL_MESSAGE;
     189                 :          0 :   g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL);
     190                 :            : 
     191                 :          0 :   g_log_set_default_handler (log_handler, NULL);
     192                 :            : 
     193         [ #  # ]:          0 :   if (show_version)
     194                 :            :     {
     195                 :          0 :       g_printf ("gi-compile-repository %u.%u.%u\n",
     196                 :            :                 GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
     197                 :          0 :       return 0;
     198                 :            :     }
     199                 :            : 
     200   [ #  #  #  # ]:          0 :   if (!input || g_strv_length (input) != 1)
     201                 :            :     {
     202                 :          0 :       g_fprintf (stderr, "%s\n", _("Please specify exactly one input file"));
     203                 :            : 
     204                 :          0 :       return 1;
     205                 :            :     }
     206                 :            : 
     207         [ #  # ]:          0 :   g_debug ("[parsing] start, %d includes",
     208                 :            :            includedirs ? g_strv_length (includedirs) : 0);
     209                 :            : 
     210                 :          0 :   parser = gi_ir_parser_new ();
     211                 :          0 :   gi_ir_parser_set_debug (parser, logged_levels);
     212                 :            : 
     213                 :          0 :   gi_ir_parser_set_includes (parser, (const char *const *) includedirs);
     214                 :            : 
     215                 :          0 :   module = gi_ir_parser_parse_file (parser, input[0], &error);
     216         [ #  # ]:          0 :   if (module == NULL)
     217                 :            :     {
     218                 :          0 :       char *message = g_strdup_printf (_("Error parsing file ‘%s’: %s"), input[0], error->message);
     219                 :          0 :       g_fprintf (stderr, "%s\n", message);
     220                 :          0 :       g_free (message);
     221                 :            : 
     222                 :          0 :       return 1;
     223                 :            :     }
     224                 :            : 
     225                 :          0 :   g_debug ("[parsing] done");
     226                 :            : 
     227                 :          0 :   g_debug ("[building] start");
     228                 :            : 
     229                 :            :   {
     230                 :          0 :     GITypelib *typelib = NULL;
     231                 :            : 
     232         [ #  # ]:          0 :     if (shlibs)
     233                 :            :       {
     234         [ #  # ]:          0 :         if (module->shared_library)
     235                 :          0 :           g_free (module->shared_library);
     236                 :          0 :         module->shared_library = g_strjoinv (",", shlibs);
     237                 :            :       }
     238                 :            : 
     239                 :          0 :     g_debug ("[building] module %s", module->name);
     240                 :            : 
     241                 :          0 :     typelib = gi_ir_module_build_typelib (module);
     242         [ #  # ]:          0 :     if (typelib == NULL)
     243                 :          0 :       g_error (_("Failed to build typelib for module ‘%s’"), module->name);
     244         [ #  # ]:          0 :     if (!gi_typelib_validate (typelib, &error))
     245                 :          0 :       g_error (_("Invalid typelib for module ‘%s’: %s"),
     246                 :            :                module->name, error->message);
     247                 :            : 
     248         [ #  # ]:          0 :     if (!write_out_typelib (NULL, typelib))
     249                 :          0 :       return 1;
     250                 :            : 
     251                 :          0 :     g_clear_pointer (&typelib, gi_typelib_unref);
     252                 :            :   }
     253                 :            : 
     254                 :          0 :   g_debug ("[building] done");
     255                 :            : 
     256                 :            : #if 0
     257                 :            :   /* No point */
     258                 :            :   gi_ir_parser_free (parser);
     259                 :            : #endif
     260                 :            : 
     261                 :          0 :   return 0;
     262                 :            : }

Generated by: LCOV version 1.14