This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH][LTO] Cleanup all LTRANS temporary files on error


This makes sure we cleanup all temporary files generated during the
LTRANS phase when one of the LTRANS invocation fails.

Bootstrapped and tested on x86_64-unknown-linux-gnu, ok for trunk?

Thanks,
Richard.

2010-05-25  Richard Guenther  <rguenther@suse.de>

	* lto-wrapper.c (nr, input_names, output_names, makefile): Globalize.
	(lto_wrapper_exit): Unlink all LTRANS temporary files on error.
	(run_gcc): Re-organize to make cleanup easier.

Index: lto-wrapper.c
===================================================================
*** lto-wrapper.c	(revision 159814)
--- lto-wrapper.c	(working copy)
*************** static enum lto_mode_d lto_mode = LTO_MO
*** 60,65 ****
--- 60,69 ----
  static char *ltrans_output_file;
  static char *flto_out;
  static char *args_name;
+ static unsigned int nr;
+ static char **input_names;
+ static char **output_names;
+ static char *makefile;
  
  static void maybe_unlink_file (const char *);
  
*************** lto_wrapper_exit (int status)
*** 71,76 ****
--- 75,82 ----
    static bool cleanup_done = false;
    if (!cleanup_done)
      {
+       unsigned int i;
+ 
        /* Setting cleanup_done prevents an infinite loop if one of the
           calls to maybe_unlink_file fails. */
        cleanup_done = true;
*************** lto_wrapper_exit (int status)
*** 81,86 ****
--- 87,100 ----
          maybe_unlink_file (flto_out);
        if (args_name)
          maybe_unlink_file (args_name);
+       if (makefile)
+ 	maybe_unlink_file (makefile);
+       for (i = 0; i < nr; ++i)
+ 	{
+ 	  maybe_unlink_file (input_names[i]);
+ 	  if (output_names[i])
+ 	    maybe_unlink_file (output_names[i]);
+ 	}
      }
    exit (status);
  }
*************** run_gcc (unsigned argc, char *argv[])
*** 428,454 ****
    else if (lto_mode == LTO_MODE_WHOPR)
      {
        FILE *stream = fopen (ltrans_output_file, "r");
-       unsigned int nr = 0;
-       char **input_names = NULL;
-       char **output_names = NULL;
-       char *makefile = NULL;
        FILE *mstream = NULL;
  
        if (!stream)
  	fatal_perror ("fopen: %s", ltrans_output_file);
  
!       argv_ptr[1] = "-fltrans";
! 
!       if (parallel)
! 	{
! 	  makefile = make_temp_file (".mk");
! 	  mstream = fopen (makefile, "w");
! 	}
! 
        for (;;)
  	{
  	  const unsigned piece = 32;
! 	  char *output_name;
  	  char *buf, *input_name = (char *)xmalloc (piece);
  	  size_t len;
  
--- 442,458 ----
    else if (lto_mode == LTO_MODE_WHOPR)
      {
        FILE *stream = fopen (ltrans_output_file, "r");
        FILE *mstream = NULL;
  
        if (!stream)
  	fatal_perror ("fopen: %s", ltrans_output_file);
  
!       /* Parse the list of LTRANS inputs from the WPA stage.  */
!       nr = 0;
        for (;;)
  	{
  	  const unsigned piece = 32;
! 	  char *output_name = NULL;
  	  char *buf, *input_name = (char *)xmalloc (piece);
  	  size_t len;
  
*************** cont:
*** 467,510 ****
  
  	  if (input_name[0] == '*')
  	    output_name = &input_name[1];
- 	  else
- 	    {
- 	      /* Otherwise, add FILES[I] to lto_execute_ltrans command line
- 		 and add the resulting file to LTRANS output list.  */
- 
- 	      /* Replace the .o suffix with a .ltrans.o suffix and write
- 		 the resulting name to the LTRANS output list.  */
- 	      obstack_init (&env_obstack);
- 	      obstack_grow (&env_obstack, input_name, strlen (input_name) - 2);
- 	      obstack_grow (&env_obstack, ".ltrans.o", sizeof (".ltrans.o"));
- 	      output_name = XOBFINISH (&env_obstack, char *);
- 
- 	      if (linker_output)
- 		{
- 		  char *dumpbase
- 		    = (char *) xmalloc (strlen (linker_output)
- 					+ sizeof(DUMPBASE_SUFFIX) + 1);
- 		  snprintf (dumpbase,
- 			    strlen (linker_output) + sizeof(DUMPBASE_SUFFIX),
- 			    "%s.ltrans%u", linker_output, nr);
- 		  argv_ptr[0] = dumpbase;
- 		}
- 
- 	      argv_ptr[2] = "-o";
- 	      argv_ptr[3] = output_name;
- 	      argv_ptr[4] = input_name;
- 	      argv_ptr[5] = NULL;
- 
- 	      if (parallel)
- 		{
- 		  fprintf (mstream, "%s:\n\t@%s ", output_name, new_argv[0]);
- 		  for (i = 1; new_argv[i] != NULL; ++i)
- 		    fprintf (mstream, " '%s'", new_argv[i]);
- 		  fprintf (mstream, "\n");
- 		}
- 	      else
- 		fork_execute (CONST_CAST (char **, new_argv));
- 	    }
  
  	  nr++;
  	  input_names = (char **)xrealloc (input_names, nr * sizeof (char *));
--- 471,476 ----
*************** cont:
*** 512,517 ****
--- 478,539 ----
  	  input_names[nr-1] = input_name;
  	  output_names[nr-1] = output_name;
  	}
+       fclose (stream);
+       maybe_unlink_file (ltrans_output_file);
+       ltrans_output_file = NULL;
+ 
+       if (parallel)
+ 	{
+ 	  makefile = make_temp_file (".mk");
+ 	  mstream = fopen (makefile, "w");
+ 	}
+ 
+       /* Execute the LTRANS stage for each input file (or prepare a
+ 	 makefile to invoke this in parallel).  */
+       for (i = 0; i < nr; ++i)
+ 	{
+ 	  char *output_name;
+ 	  char *input_name = input_names[i];
+ 	  /* If it's a pass-through file do nothing.  */
+ 	  if (output_names[i])
+ 	    continue;
+ 
+ 	  /* Replace the .o suffix with a .ltrans.o suffix and write
+ 	     the resulting name to the LTRANS output list.  */
+ 	  obstack_init (&env_obstack);
+ 	  obstack_grow (&env_obstack, input_name, strlen (input_name) - 2);
+ 	  obstack_grow (&env_obstack, ".ltrans.o", sizeof (".ltrans.o"));
+ 	  output_name = XOBFINISH (&env_obstack, char *);
+ 
+ 	  /* Adjust the dumpbase if the linker output file was seen.  */
+ 	  if (linker_output)
+ 	    {
+ 	      char *dumpbase
+ 		  = (char *) xmalloc (strlen (linker_output)
+ 				      + sizeof(DUMPBASE_SUFFIX) + 1);
+ 	      snprintf (dumpbase,
+ 			strlen (linker_output) + sizeof(DUMPBASE_SUFFIX),
+ 			"%s.ltrans%u", linker_output, nr);
+ 	      argv_ptr[0] = dumpbase;
+ 	    }
+ 
+ 	  argv_ptr[1] = "-fltrans";
+ 	  argv_ptr[2] = "-o";
+ 	  argv_ptr[3] = output_name;
+ 	  argv_ptr[4] = input_name;
+ 	  argv_ptr[5] = NULL;
+ 	  if (parallel)
+ 	    {
+ 	      fprintf (mstream, "%s:\n\t@%s ", output_name, new_argv[0]);
+ 	      for (j = 1; new_argv[j] != NULL; ++j)
+ 		fprintf (mstream, " '%s'", new_argv[j]);
+ 	      fprintf (mstream, "\n");
+ 	    }
+ 	  else
+ 	    fork_execute (CONST_CAST (char **, new_argv));
+ 
+ 	  output_names[i] = output_name;
+ 	}
        if (parallel)
  	{
  	  struct pex_obj *pex;
*************** cont:
*** 536,541 ****
--- 558,564 ----
  	  pex = collect_execute (CONST_CAST (char **, new_argv));
  	  collect_wait (new_argv[0], pex);
  	  maybe_unlink_file (makefile);
+ 	  makefile = NULL;
  	}
        for (i = 0; i < nr; ++i)
  	{
*************** cont:
*** 544,553 ****
  	  maybe_unlink_file (input_names[i]);
  	  free (input_names[i]);
  	}
        free (output_names);
        free (input_names);
-       fclose (stream);
-       maybe_unlink_file (ltrans_output_file);
        free (list_option_full);
      }
    else
--- 567,575 ----
  	  maybe_unlink_file (input_names[i]);
  	  free (input_names[i]);
  	}
+       nr = 0;
        free (output_names);
        free (input_names);
        free (list_option_full);
      }
    else


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]