示例#1
0
static void
do_compile (const char **current_argv, int current_argc)
{
  char *errmsg_fmt, *errmsg_arg;
  int index = 0;

  int dash_o_index = current_argc;
  int of_index = current_argc + 1;
  int argc_count = current_argc + 2;

  while (index < num_arches)
    {
      int additional_arch_options = 0;

      current_argv[0] = get_driver_name (get_arch_name (arches[index]));

      /* setup output file.  */
      out_files[num_outfiles] = make_temp_file (".out");
      current_argv[dash_o_index] = "-o";
      current_argv[of_index] = out_files [num_outfiles];
      num_outfiles++;

      /* Add arch option as the last option. Do not add any other option
	 before removing this option.  */
      additional_arch_options = add_arch_options (index, current_argv, argc_count);
      argc_count += additional_arch_options;

      commands[index].prog = current_argv[0];
      commands[index].argv = current_argv;

      current_argv[argc_count] = NULL;

#ifdef DEBUG
      debug_command_line (current_argv, argc_count);
#endif
      commands[index].pid = pexecute (current_argv[0],
				      (char *const *)current_argv,
				      progname, NULL,
				      &errmsg_fmt,
				      &errmsg_arg,
				      PEXECUTE_SEARCH | PEXECUTE_ONE);

      if (commands[index].pid == -1)
	pfatal_pexecute (errmsg_fmt, errmsg_arg);

      do_wait (commands[index].pid, commands[index].prog);
      fflush (stdout);

      /* Remove the last arch option added in the current_argv list.  */
      if (additional_arch_options)
	argc_count -= remove_arch_options (current_argv, argc_count);
      index++;
    }
}
static void
do_lipo (int start_outfile_index, const char *out_file)
{
  int i, j, pid;
  char *errmsg_fmt, *errmsg_arg;

  /* Populate lipo arguments.  */
  lipo_argv[0] = "lipo";
  lipo_argv[1] = "-create";
  lipo_argv[2] = "-o";
  lipo_argv[3] = out_file;

  /* Already 4 lipo arguments are set.  Now add all lipo inputs.  */
  j = 4;
  for (i = 0; i < num_arches; i++)
    lipo_argv[j++] = out_files[start_outfile_index + i];

  /* Add the NULL at the end.  */
  lipo_argv[j++] = NULL;

#ifdef DEBUG
  debug_command_line (lipo_argv, j);
#endif

  if (verbose_flag)
    {
      for (i = 0; lipo_argv[i]; i++)
	fprintf (stderr, "%s ", lipo_argv[i]);
      fprintf (stderr, "\n");
    }
  pid = pexecute (lipo_argv[0], (char *const *)lipo_argv, progname, NULL, &errmsg_fmt,
		  &errmsg_arg, PEXECUTE_SEARCH | PEXECUTE_ONE);

  if (pid == -1)
    pfatal_pexecute (errmsg_fmt, errmsg_arg);

  do_wait (pid, lipo_argv[0]);
}
/* Main entry point. This is gcc driver driver!
   Interpret -arch flag from the list of input arguments. Invoke appropriate
   compiler driver. 'lipo' the results if more than one -arch is supplied.  */
int
main (int argc, const char **argv)
{
  size_t i;
  int l, pid, argv_0_len, prog_len;
  char *errmsg_fmt, *errmsg_arg;
  char *override_option_str = NULL;
  char path_buffer[2*PATH_MAX+1];
  int linklen;

  total_argc = argc;
  prog_len = 0;

  argv_0_len = strlen (argv[0]);

  /* Get the progname, required by pexecute () and program location.  */
  prog_len = get_prog_name_len (argv[0]);

  /* If argv[0] is all program name (no slashes), search the PATH environment
     variable to get the fully resolved path to the executable. */
  if (prog_len == argv_0_len)
    {
#ifdef DEBUG
      progname = argv[0] + argv_0_len - prog_len;
      fprintf (stderr,"%s: before PATH resolution, full progname = %s\n",
               argv[0]+argv_0_len-prog_len, argv[0]);
#endif
      argv[0] = resolve_path_to_executable (argv[0]);
      prog_len = get_prog_name_len (argv[0]);
      argv_0_len = strlen(argv[0]);
    }

  /* If argv[0] is a symbolic link, use the directory of the pointed-to file
     to find compiler components. */

  if ((linklen = readlink (argv[0], path_buffer, PATH_MAX)) != -1)
    {
      /* readlink succeeds if argv[0] is a symlink.  path_buffer now contains
	 the file referenced. */
      path_buffer[linklen] = '\0';
#ifdef DEBUG
      progname = argv[0] + argv_0_len - prog_len;
      fprintf (stderr, "%s: before symlink, full prog = %s target = %s\n",
	       progname, argv[0], path_buffer);
#endif
      argv[0] = resolve_symlink(argv[0], path_buffer, argv_0_len, prog_len);
      argv_0_len = strlen(argv[0]);

      /* Get the progname, required by pexecute () and program location.  */
      prog_len = get_prog_name_len (argv[0]);

#ifdef DEBUG
      progname = argv[0] + argv_0_len - prog_len;
      printf("%s: ARGV[0] after symlink = %s\n", progname, argv[0]);
#endif
    }

  progname = argv[0] + argv_0_len - prog_len;

  /* Setup driver prefix.  */
  prefix_len = argv_0_len - prog_len;
  curr_dir = (char *) malloc (sizeof (char) * (prefix_len + 1));
  strncpy (curr_dir, argv[0], prefix_len);
  curr_dir[prefix_len] = '\0';
  driver_exec_prefix = (argv[0], "/usr/bin", curr_dir);

#ifdef DEBUG
  fprintf (stderr,"%s: full progname = %s\n", progname, argv[0]);
  fprintf (stderr,"%s: progname = %s\n", progname, progname);
  fprintf (stderr,"%s: driver_exec_prefix = %s\n", progname, driver_exec_prefix);
#endif

  /* Before we get too far, rewrite the command line with any requested overrides */
  if ((override_option_str = getenv ("QA_OVERRIDE_GCC3_OPTIONS")) != NULL)
    rewrite_command_line(override_option_str, &argc, (char***)&argv);



  initialize ();

  /* Process arguments. Take appropriate actions when
     -arch, -c, -S, -E, -o is encountered. Find input file name.  */
  for (i = 1; i < argc; i++)
    {
      if (!strcmp (argv[i], "-arch"))
	{
	  if (i + 1 >= argc)
	    abort ();

	  add_arch (argv[i+1]);
	  i++;
	}
      else if (!strcmp (argv[i], "-c"))
	{
	  new_argv[new_argc++] = argv[i];
	  compile_only_request = 1;
	}
      else if (!strcmp (argv[i], "-S"))
	{
	  new_argv[new_argc++] = argv[i];
	  asm_output_request = 1;
	}
      else if (!strcmp (argv[i], "-E"))
	{
	  new_argv[new_argc++] = argv[i];
	  preprocessed_output_request = 1;
	}
      else if (!strcmp (argv[i], "-MD") || !strcmp (argv[i], "-MMD"))
	{
	  new_argv[new_argc++] = argv[i];
	  dash_capital_m_seen = 1;
	}
      else if (!strcmp (argv[i], "-m32"))
	{
	  new_argv[new_argc++] = argv[i];
	  dash_m32_seen = 1;
	}
      else if (!strcmp (argv[i], "-m64"))
	{
	  new_argv[new_argc++] = argv[i];
	  dash_m64_seen = 1;
	}
      else if (!strcmp (argv[i], "-dynamiclib"))
	{
	  new_argv[new_argc++] = argv[i];
	  dash_dynamiclib_seen = 1;
        }
      else if (!strcmp (argv[i], "-v"))
	{
	  new_argv[new_argc++] = argv[i];
	  verbose_flag = 1;
	}
      else if (!strcmp (argv[i], "-o"))
	{
	  if (i + 1 >= argc)
	    fatal ("argument to '-o' is missing");

	  output_filename = argv[i+1];
	  i++;
	}
      else if ((! strcmp (argv[i], "-pass-exit-codes"))
	       || (! strcmp (argv[i], "-print-search-dirs"))
	       || (! strcmp (argv[i], "-print-libgcc-file-name"))
	       || (! strncmp (argv[i], "-print-file-name=", 17))
	       || (! strncmp (argv[i], "-print-prog-name=", 17))
	       || (! strcmp (argv[i], "-print-multi-lib"))
	       || (! strcmp (argv[i], "-print-multi-directory"))
	       || (! strcmp (argv[i], "-print-multi-os-directory"))
	       || (! strcmp (argv[i], "-ftarget-help"))
	       || (! strcmp (argv[i], "-fhelp"))
	       || (! strcmp (argv[i], "+e"))
	       || (! strncmp (argv[i], "-Wa,",4))
	       || (! strncmp (argv[i], "-Wp,",4))
	       || (! strncmp (argv[i], "-Wl,",4))
	       || (! strncmp (argv[i], "-l", 2))
	       || (! strncmp (argv[i], "-weak-l", 7))
	       || (! strncmp (argv[i], "-specs=", 7))
	       || (! strcmp (argv[i], "-ObjC"))
	       || (! strcmp (argv[i], "-fobjC"))
	       || (! strcmp (argv[i], "-ObjC++"))
	       || (! strcmp (argv[i], "-time"))
	       || (! strcmp (argv[i], "-###"))
	       || (! strcmp (argv[i], "-fconstant-cfstrings"))
	       || (! strcmp (argv[i], "-fno-constant-cfstrings"))
	       || (! strcmp (argv[i], "-static-libgcc"))
	       || (! strcmp (argv[i], "-shared-libgcc"))
	       || (! strcmp (argv[i], "-pipe"))
	       )
	{
	  new_argv[new_argc++] = argv[i];
	}
      else if (! strcmp (argv[i], "-save-temps")
	       || ! strcmp (argv[i], "--save-temps"))
	{
	  new_argv[new_argc++] = argv[i];
	  save_temps_seen = 1;
	}
      else if ((! strcmp (argv[i], "-Xlinker"))
	       || (! strcmp (argv[i], "-Xassembler"))
	       || (! strcmp (argv[i], "-Xpreprocessor"))
	       || (! strcmp (argv[i], "-l"))
	       || (! strcmp (argv[i], "-weak_library"))
	       || (! strcmp (argv[i], "-weak_framework"))
	       || (! strcmp (argv[i], "-specs"))
	       || (! strcmp (argv[i], "-framework"))
	       )
	{
	  new_argv[new_argc++] = argv[i];
	  i++;
	  new_argv[new_argc++] = argv[i];
	}
      else if (! strncmp (argv[i], "-Xarch_", 7))
	{
	  arch_conditional[new_argc] = get_arch_name (argv[i] + 7);
	  i++;
	  new_argv[new_argc++] = argv[i];
	}
      else if (argv[i][0] == '-' && argv[i][1] != 0)
	{
	  const char *p = &argv[i][1];
	  int c = *p;

	  /* First copy this flag itself.  */
	  new_argv[new_argc++] = argv[i];

	  if (argv[i][1] == 'M')
	    dash_capital_m_seen = 1;

	  /* Now copy this flag's arguments, if any, appropriately.  */
	  if ((SWITCH_TAKES_ARG (c) > (p[1] != 0))
	      || WORD_SWITCH_TAKES_ARG (p))
	    {
	      int j = 0;
	      int n_args = WORD_SWITCH_TAKES_ARG (p);
	      if (n_args == 0)
		{
		  /* Count only the option arguments in separate argv elements.  */
		  n_args = SWITCH_TAKES_ARG (c) - (p[1] != 0);
		}
	      if (i + n_args >= argc)
		fatal ("argument to `-%s' is missing", p);


	      while ( j < n_args)
		{
		  i++;
		  new_argv[new_argc++] = argv[i];
		  j++;
		}
	    }

	}
      else
	{
	  struct input_filename *ifn;
	  new_argv[new_argc++] = argv[i];
	  ifn = (struct input_filename *) malloc (sizeof (struct input_filename));
	  ifn->name = argv[i];
	  ifn->index = i;
	  ifn->next = NULL;
	  num_infiles++;

	  if (last_infile)
	      last_infile->next = ifn;
	  else
	    in_files = ifn;

	  last_infile = ifn;
	}
    }

#if 0
  if (num_infiles == 0)
    fatal ("no input files");
#endif

  if (num_arches == 0)
    add_arch(get_arch_name(NULL));

  if (num_arches > 1)
    {
      if (preprocessed_output_request
	  || save_temps_seen
	  || asm_output_request
	  || dash_capital_m_seen)
	fatal ("-E, -S, -save-temps and -M options are not allowed with multiple -arch flags");
    }
  /* If -arch is not present OR Only one -arch <blah> is specified.
     Invoke appropriate compiler driver.  FAT build is not required in this
     case.  */

  if (num_arches == 1)
    {
      int arch_specific_argc;
      const char **arch_specific_argv;

      /* Find compiler driver based on -arch <foo> and add approriate
	 -m* argument.  */
      new_argv[0] = get_driver_name (get_arch_name (arches[0]));
      new_argc = new_argc + add_arch_options (0, new_argv, new_argc);

#ifdef DEBUG
      printf ("%s: invoking single driver name = %s\n", progname, new_argv[0]);
#endif

      /* Re insert output file name.  */
      if (output_filename)
	{
	  new_argv[new_argc++] = "-o";
	  new_argv[new_argc++] = output_filename;
	}

      /* Add the NULL.  */
      new_argv[new_argc] = NULL;

      arch_specific_argv =
	(const char **) malloc ((new_argc + 1) * sizeof (const char *));
      arch_specific_argc = filter_args_for_arch (new_argv,
						 new_argc,
						 arch_specific_argv,
						 get_arch_name (arches[0]));

#ifdef DEBUG
      debug_command_line (arch_specific_argv, arch_specific_argc);
#endif

      pid = pexecute (arch_specific_argv[0], (char *const *)arch_specific_argv,
		      progname, NULL, &errmsg_fmt, &errmsg_arg,
		      PEXECUTE_SEARCH | PEXECUTE_ONE);

      if (pid == -1)
	pfatal_pexecute (errmsg_fmt, errmsg_arg);

      do_wait (pid, arch_specific_argv[0]);
    }
  else
    {
      /* Handle multiple -arch <blah>.  */

      /* If more than one input files are supplied but only one output filename
	 is present then IMA will be used.  */
      if (num_infiles > 1 && !compile_only_request)
	ima_is_used = 1;

      /* Linker wants to know this in case of multiple -arch.  */
      if (!compile_only_request && !dash_dynamiclib_seen)
	new_argv[new_argc++] = "-Wl,-arch_multiple";


      /* If only one input file is specified OR IMA is used then expected output
	 is one FAT binary.  */
      if (num_infiles == 1 || ima_is_used)
	{
	  const char *out_file;

	     /* Create output file name based on
	     input filename, if required.  */
	  if (compile_only_request && !output_filename && num_infiles == 1)
	    out_file = strip_path_and_suffix (in_files->name, ".o");
	  else
	    out_file = (output_filename ? output_filename : final_output);


	  /* Linker wants to know name of output file using one extra arg.  */
	  if (!compile_only_request)
	    {
	      char *oname = (char *)(output_filename ? output_filename : final_output);
	      char *n =  malloc (sizeof (char) * (strlen (oname) + 5));
	      strcpy (n, "-Wl,");
	      strcat (n, oname);
	      new_argv[new_argc++] = "-Wl,-final_output";
	      new_argv[new_argc++] = n;
	    }

	  /* Compile file(s) for each arch and lipo 'em together.  */
	  do_compile (new_argv, new_argc);

	  /* Make FAT binary by combining individual output files for each
	     architecture, using 'lipo'.  */
	  do_lipo (0, out_file);
	}
      else
	{
	  /* Multiple input files are present and IMA is not used.
	     Which means need to generate multiple FAT files.  */
	  do_compile_separately ();
	  do_lipo_separately ();
	}
    }

  final_cleanup ();
  free (curr_dir);
  return greatest_status;
}