Пример #1
0
bool
execute_java_class (const char *class_name,
		    const char * const *classpaths,
		    unsigned int classpaths_count,
		    bool use_minimal_classpath,
		    const char *exe_dir,
		    const char * const *args,
		    bool verbose, bool quiet,
		    execute_fn *executer, void *private_data)
{
  bool err = false;
  unsigned int nargs;
  char *old_JAVA_HOME;

  /* Count args.  */
  {
    const char * const *arg;

    for (nargs = 0, arg = args; *arg != NULL; nargs++, arg++)
     ;
  }

  /* First, try a class compiled to a native code executable.  */
  if (exe_dir != NULL)
    {
      char *exe_pathname = concatenated_filename (exe_dir, class_name, EXEEXT);
      char *old_classpath;
      char **argv = (char **) xmalloca ((1 + nargs + 1) * sizeof (char *));
      unsigned int i;

      /* Set CLASSPATH.  */
      old_classpath =
	set_classpath (classpaths, classpaths_count, use_minimal_classpath,
		       verbose);

      argv[0] = exe_pathname;
      for (i = 0; i <= nargs; i++)
	argv[1 + i] = (char *) args[i];

      if (verbose)
	{
	  char *command = shell_quote_argv (argv);
	  printf ("%s\n", command);
	  free (command);
	}

      err = executer (class_name, exe_pathname, argv, private_data);

      /* Reset CLASSPATH.  */
      reset_classpath (old_classpath);

      freea (argv);

      goto done1;
    }

  {
    const char *java = getenv ("JAVA");
    if (java != NULL && java[0] != '\0')
      {
	/* Because $JAVA may consist of a command and options, we use the
	   shell.  Because $JAVA has been set by the user, we leave all
	   all environment variables in place, including JAVA_HOME, and
	   we don't erase the user's CLASSPATH.  */
	char *old_classpath;
	unsigned int command_length;
	char *command;
	char *argv[4];
	const char * const *arg;
	char *p;

	/* Set CLASSPATH.  */
	old_classpath =
	  set_classpath (classpaths, classpaths_count, false,
			 verbose);

	command_length = strlen (java);
	command_length += 1 + shell_quote_length (class_name);
	for (arg = args; *arg != NULL; arg++)
	  command_length += 1 + shell_quote_length (*arg);
	command_length += 1;

	command = (char *) xmalloca (command_length);
	p = command;
	/* Don't shell_quote $JAVA, because it may consist of a command
	   and options.  */
	memcpy (p, java, strlen (java));
	p += strlen (java);
	*p++ = ' ';
	p = shell_quote_copy (p, class_name);
	for (arg = args; *arg != NULL; arg++)
	  {
	    *p++ = ' ';
	    p = shell_quote_copy (p, *arg);
	  }
	*p++ = '\0';
	/* Ensure command_length was correctly calculated.  */
	if (p - command > command_length)
	  abort ();

	if (verbose)
	  printf ("%s\n", command);

	argv[0] = "/bin/sh";
	argv[1] = "-c";
	argv[2] = command;
	argv[3] = NULL;
	err = executer (java, "/bin/sh", argv, private_data);

	freea (command);

	/* Reset CLASSPATH.  */
	reset_classpath (old_classpath);

	goto done1;
      }
  }

  /* Unset the JAVA_HOME environment variable.  */
  old_JAVA_HOME = getenv ("JAVA_HOME");
  if (old_JAVA_HOME != NULL)
    {
      old_JAVA_HOME = xstrdup (old_JAVA_HOME);
      unsetenv ("JAVA_HOME");
    }

  {
    static bool gij_tested;
    static bool gij_present;

    if (!gij_tested)
      {
	/* Test for presence of gij: "gij --version > /dev/null"  */
	char *argv[3];
	int exitstatus;

	argv[0] = "gij";
	argv[1] = "--version";
	argv[2] = NULL;
	exitstatus = execute ("gij", "gij", argv, false, false, true, true,
			      true, false);
	gij_present = (exitstatus == 0);
	gij_tested = true;
      }

    if (gij_present)
      {
	char *old_classpath;
	char **argv = (char **) xmalloca ((2 + nargs + 1) * sizeof (char *));
	unsigned int i;

	/* Set CLASSPATH.  */
	old_classpath =
	  set_classpath (classpaths, classpaths_count, use_minimal_classpath,
			 verbose);

	argv[0] = "gij";
	argv[1] = (char *) class_name;
	for (i = 0; i <= nargs; i++)
	  argv[2 + i] = (char *) args[i];

	if (verbose)
	  {
	    char *command = shell_quote_argv (argv);
	    printf ("%s\n", command);
	    free (command);
	  }

	err = executer ("gij", "gij", argv, private_data);

	/* Reset CLASSPATH.  */
	reset_classpath (old_classpath);

	freea (argv);

	goto done2;
      }
  }

  {
    static bool java_tested;
    static bool java_present;

    if (!java_tested)
      {
	/* Test for presence of java: "java -version 2> /dev/null"  */
	char *argv[3];
	int exitstatus;

	argv[0] = "java";
	argv[1] = "-version";
	argv[2] = NULL;
	exitstatus = execute ("java", "java", argv, false, false, true, true,
			      true, false);
	java_present = (exitstatus == 0);
	java_tested = true;
      }

    if (java_present)
      {
	char *old_classpath;
	char **argv = (char **) xmalloca ((2 + nargs + 1) * sizeof (char *));
	unsigned int i;

	/* Set CLASSPATH.  We don't use the "-classpath ..." option because
	   in JDK 1.1.x its argument should also contain the JDK's classes.zip,
	   but we don't know its location.  (In JDK 1.3.0 it would work.)  */
	old_classpath =
	  set_classpath (classpaths, classpaths_count, use_minimal_classpath,
			 verbose);

	argv[0] = "java";
	argv[1] = (char *) class_name;
	for (i = 0; i <= nargs; i++)
	  argv[2 + i] = (char *) args[i];

	if (verbose)
	  {
	    char *command = shell_quote_argv (argv);
	    printf ("%s\n", command);
	    free (command);
	  }

	err = executer ("java", "java", argv, private_data);

	/* Reset CLASSPATH.  */
	reset_classpath (old_classpath);

	freea (argv);

	goto done2;
      }
  }

  {
    static bool jre_tested;
    static bool jre_present;

    if (!jre_tested)
      {
	/* Test for presence of jre: "jre 2> /dev/null ; test $? = 1"  */
	char *argv[2];
	int exitstatus;

	argv[0] = "jre";
	argv[1] = NULL;
	exitstatus = execute ("jre", "jre", argv, false, false, true, true,
			      true, false);
	jre_present = (exitstatus == 0 || exitstatus == 1);
	jre_tested = true;
      }

    if (jre_present)
      {
	char *old_classpath;
	char **argv = (char **) xmalloca ((2 + nargs + 1) * sizeof (char *));
	unsigned int i;

	/* Set CLASSPATH.  We don't use the "-classpath ..." option because
	   in JDK 1.1.x its argument should also contain the JDK's classes.zip,
	   but we don't know its location.  */
	old_classpath =
	  set_classpath (classpaths, classpaths_count, use_minimal_classpath,
			 verbose);

	argv[0] = "jre";
	argv[1] = (char *) class_name;
	for (i = 0; i <= nargs; i++)
	  argv[2 + i] = (char *) args[i];

	if (verbose)
	  {
	    char *command = shell_quote_argv (argv);
	    printf ("%s\n", command);
	    free (command);
	  }

	err = executer ("jre", "jre", argv, private_data);

	/* Reset CLASSPATH.  */
	reset_classpath (old_classpath);

	freea (argv);

	goto done2;
      }
  }

#if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__
  /* Win32, Cygwin */
  {
    static bool jview_tested;
    static bool jview_present;

    if (!jview_tested)
      {
	/* Test for presence of jview: "jview -? >nul ; test $? = 1"  */
	char *argv[3];
	int exitstatus;

	argv[0] = "jview";
	argv[1] = "-?";
	argv[2] = NULL;
	exitstatus = execute ("jview", "jview", argv, false, false, true, true,
			      true, false);
	jview_present = (exitstatus == 0 || exitstatus == 1);
	jview_tested = true;
      }

    if (jview_present)
      {
	char *old_classpath;
	char **argv = (char **) xmalloca ((2 + nargs + 1) * sizeof (char *));
	unsigned int i;

	/* Set CLASSPATH.  */
	old_classpath =
	  set_classpath (classpaths, classpaths_count, use_minimal_classpath,
			 verbose);

	argv[0] = "jview";
	argv[1] = (char *) class_name;
	for (i = 0; i <= nargs; i++)
	  argv[2 + i] = (char *) args[i];

	if (verbose)
	  {
	    char *command = shell_quote_argv (argv);
	    printf ("%s\n", command);
	    free (command);
	  }

	err = executer ("jview", "jview", argv, private_data);

	/* Reset CLASSPATH.  */
	reset_classpath (old_classpath);

	freea (argv);

	goto done2;
      }
  }
#endif

  if (!quiet)
    error (0, 0, _("Java virtual machine not found, try installing gij or set $JAVA"));
  err = true;

 done2:
  if (old_JAVA_HOME != NULL)
    {
      xsetenv ("JAVA_HOME", old_JAVA_HOME, 1);
      free (old_JAVA_HOME);
    }

 done1:
  return err;
}
Пример #2
0
bool configt::set(const cmdlinet &cmdline)
{
  // defaults -- we match the architecture we have ourselves
  
  ansi_c.single_precision_constant=false;
  ansi_c.for_has_scope=false; // ealier than C99
  ansi_c.cpp11=false;
  ansi_c.use_fixed_for_float=false;
  ansi_c.endianness=ansi_ct::NO_ENDIANNESS;
  ansi_c.os=ansi_ct::NO_OS;
  ansi_c.arch=ansi_ct::NO_ARCH;
  ansi_c.lib=configt::ansi_ct::LIB_NONE;
  ansi_c.NULL_is_zero=(size_t)((void*)0)==0;
  
  // Default is ROUND_TO_EVEN, justified by C99:
  // 1 At program startup the floating-point environment is initialized as
  // prescribed by IEC 60559:
  // - All floating-point exception status flags are cleared.
  // - The rounding direction mode is rounding to nearest.
  ansi_c.rounding_mode=ieee_floatt::ROUND_TO_EVEN;

  if(cmdline.isset("function"))
    main=cmdline.get_value("function");
    
  if(cmdline.isset('D'))
    ansi_c.defines=cmdline.get_values('D');

  if(cmdline.isset('I'))
    ansi_c.include_paths=cmdline.get_values('I');

  if(cmdline.isset("classpath"))
  {
    // Specifying -classpath or -cp overrides any setting of the
    // CLASSPATH environment variable.
    set_classpath(cmdline.get_value("classpath"));
  }
  else if(cmdline.isset("cp"))
  {
    // Specifying -classpath or -cp overrides any setting of the
    // CLASSPATH environment variable.
    set_classpath(cmdline.get_value("cp"));
  }
  else
  {
    // environment variable set?
    const char *CLASSPATH=getenv("CLASSPATH");
    if(CLASSPATH!=NULL)
      set_classpath(CLASSPATH);
    else
      set_classpath("."); // default
  }

  if(cmdline.isset("include"))
    ansi_c.include_files=cmdline.get_values("include");

  if(cmdline.isset("floatbv"))
    ansi_c.use_fixed_for_float=false;

  if(cmdline.isset("fixedbv"))
    ansi_c.use_fixed_for_float=true;

  // the default architecture is the one we run on
  irep_idt this_arch=this_architecture();
  irep_idt arch=this_arch;

  // let's pick an OS now
  // the default is the one we run on  
  irep_idt this_os=this_operating_system();
  irep_idt os=this_os;

  if(cmdline.isset("i386-linux"))
  {
    os="linux";
    arch="i386";
  }
  else if(cmdline.isset("i386-win32") ||
          cmdline.isset("win32"))
  {
    os="windows";
    arch="i386";
  }
  else if(cmdline.isset("winx64"))
  {
    os="windows";
    arch="x86_64";
  }
  else if(cmdline.isset("i386-macos"))
  {
    os="macos";
    arch="i386";
  }
  else if(cmdline.isset("ppc-macos"))
  {
    arch="powerpc";
    os="macos";
  }

  if(os=="windows")
  {
    // Cygwin uses GCC throughout, use i386-linux
    // MinGW needs --win32 --gcc
    ansi_c.lib=configt::ansi_ct::LIB_FULL;
    ansi_c.os=configt::ansi_ct::OS_WIN;

    if(cmdline.isset("gcc"))
    {
      // There are gcc versions that target Windows (MinGW for example),
      // and we support that.
      ansi_c.preprocessor=ansi_ct::PP_GCC;
      ansi_c.mode=ansi_ct::MODE_GCC_C;

      // enable Cygwin
      #ifdef _WIN32
      ansi_c.defines.push_back("__CYGWIN__");
      #endif
    }
    else
    {
      // On Windows, our default is Visual Studio.
      // On FreeBSD, it's clang.
      // On anything else, it's GCC as the preprocessor,
      // but we recognize the Visual Studio language,
      // which is somewhat inconsistent.
      #ifdef _WIN32
      ansi_c.preprocessor=ansi_ct::PP_VISUAL_STUDIO;
      ansi_c.mode=ansi_ct::MODE_VISUAL_STUDIO_C_CPP;
      #elif __FreeBSD__
      ansi_c.preprocessor=ansi_ct::PP_CLANG;
      ansi_c.mode=ansi_ct::MODE_VISUAL_STUDIO_C_CPP;
      #else
      ansi_c.preprocessor=ansi_ct::PP_GCC;
      ansi_c.mode=ansi_ct::MODE_VISUAL_STUDIO_C_CPP;
      #endif
    }
  }
  else if(os=="macos")
  {
    ansi_c.lib=configt::ansi_ct::LIB_FULL;
    ansi_c.os=configt::ansi_ct::OS_MACOS;
    ansi_c.mode=ansi_ct::MODE_GCC_C;
    ansi_c.preprocessor=ansi_ct::PP_GCC;
  }
  else if(os=="linux" || os=="solaris")
  {
    ansi_c.lib=configt::ansi_ct::LIB_FULL;
    ansi_c.os=configt::ansi_ct::OS_LINUX;
    ansi_c.mode=ansi_ct::MODE_GCC_C;
    ansi_c.preprocessor=ansi_ct::PP_GCC;
  }
  else if(os=="freebsd")
  {
    ansi_c.lib=configt::ansi_ct::LIB_FULL;
    ansi_c.os=configt::ansi_ct::OS_LINUX;
    ansi_c.mode=ansi_ct::MODE_GCC_C;
    ansi_c.preprocessor=ansi_ct::PP_CLANG;
  }
  else
  {
    // give up, but use reasonable defaults
    ansi_c.lib=configt::ansi_ct::LIB_FULL;
    ansi_c.os=configt::ansi_ct::OS_LINUX;
    ansi_c.mode=ansi_ct::MODE_GCC_C;
    ansi_c.preprocessor=ansi_ct::PP_GCC;
  }
  
  if(arch=="none")
  {
    // the architecture for people who can't commit
    ansi_c.arch=configt::ansi_ct::NO_ARCH;
    ansi_c.endianness=configt::ansi_ct::NO_ENDIANNESS;
    ansi_c.lib=configt::ansi_ct::LIB_NONE;
    ansi_c.NULL_is_zero=false;

    if(sizeof(long int)==8)
      ansi_c.set_64();
    else
      ansi_c.set_32();
  }
  else if(arch=="alpha")
    ansi_c.set_arch_spec_alpha();
  else if(arch=="arm64" ||
          arch=="armel" ||
          arch=="armhf" ||
          arch=="arm")
    ansi_c.set_arch_spec_arm(arch);
  else if(arch=="mips64el" ||
          arch=="mipsn32el" ||
          arch=="mipsel" ||
          arch=="mips64" ||
          arch=="mipsn32" ||
          arch=="mips")
    ansi_c.set_arch_spec_mips(arch);
  else if(arch=="powerpc" ||
          arch=="ppc64" ||
          arch=="ppc64le")
    ansi_c.set_arch_spec_power(arch);
  else if(arch=="sparc" ||
          arch=="sparc64")
    ansi_c.set_arch_spec_sparc(arch);
  else if(arch=="ia64")
    ansi_c.set_arch_spec_ia64();
  else if(arch=="s390x")
    ansi_c.set_arch_spec_s390x();
  else if(arch=="s390")
    ansi_c.set_arch_spec_s390();
  else if(arch=="x32")
    ansi_c.set_arch_spec_x32();
  else if(arch=="x86_64")
    ansi_c.set_arch_spec_x86_64();
  else if(arch=="i386")
    ansi_c.set_arch_spec_i386();
  else
  {
    // We run on something new and unknown.
    // We verify for i386 instead.
    ansi_c.set_arch_spec_i386();
    arch="i386";
  }

  if(os=="windows")
  {
    // note that sizeof(void *)==8, but sizeof(long)==4!
    if(arch=="x86_64")
      ansi_c.set_LLP64();
    
    // On Windows, wchar_t is unsigned 16 bit, regardless
    // of the compiler used.
    ansi_c.wchar_t_width=2*8;
    ansi_c.wchar_t_is_unsigned=true;

    // long double is the same as double in Visual Studio,
    // but it's 16 bytes with GCC with the 64-bit target.
    if(arch=="x64_64" && cmdline.isset("gcc"))
      ansi_c.long_double_width=16*8;
    else
      ansi_c.long_double_width=8*8;
  }

  // Let's check some of the type widths in case we run
  // the same architecture and OS that we are verifying for.
  if(arch==this_arch && os==this_os)
  {
    assert(ansi_c.int_width==sizeof(int)*8);
    assert(ansi_c.long_int_width==sizeof(long)*8);
    assert(ansi_c.bool_width==sizeof(bool)*8);
    assert(ansi_c.char_width==sizeof(char)*8);
    assert(ansi_c.short_int_width==sizeof(short)*8);
    assert(ansi_c.long_long_int_width==sizeof(long long)*8);
    assert(ansi_c.pointer_width==sizeof(void *)*8);
    assert(ansi_c.single_width==sizeof(float)*8);
    assert(ansi_c.double_width==sizeof(double)*8);
    assert(ansi_c.char_is_unsigned==(char(255)==255));

    #ifndef _WIN32
    // On Windows, long double width varies by compiler
    assert(ansi_c.long_double_width==sizeof(long double)*8);
    #endif
  }  
  
  // the following allows overriding the defaults
  
  if(cmdline.isset("16"))
    ansi_c.set_16();

  if(cmdline.isset("32"))
    ansi_c.set_32();
    
  if(cmdline.isset("64"))
    ansi_c.set_64();

  if(cmdline.isset("LP64"))
    ansi_c.set_LP64();  // int=32, long=64, pointer=64

  if(cmdline.isset("ILP64"))
    ansi_c.set_ILP64(); // int=64, long=64, pointer=64

  if(cmdline.isset("LLP64"))
    ansi_c.set_LLP64(); // int=32, long=32, pointer=64

  if(cmdline.isset("ILP32"))
    ansi_c.set_ILP32(); // int=32, long=32, pointer=32

  if(cmdline.isset("LP32"))
    ansi_c.set_LP32();  // int=16, long=32, pointer=32
    
  if(cmdline.isset("string-abstraction"))
    ansi_c.string_abstraction=true;
  else
    ansi_c.string_abstraction=false;  
  
  if(cmdline.isset("no-library"))
    ansi_c.lib=configt::ansi_ct::LIB_NONE;
  
  if(cmdline.isset("little-endian"))
    ansi_c.endianness=configt::ansi_ct::IS_LITTLE_ENDIAN;

  if(cmdline.isset("big-endian"))
    ansi_c.endianness=configt::ansi_ct::IS_BIG_ENDIAN;

  if(cmdline.isset("little-endian") &&
     cmdline.isset("big-endian"))
    return true;

  if(cmdline.isset("unsigned-char"))
    ansi_c.char_is_unsigned=true;

  if(cmdline.isset("round-to-even") ||
     cmdline.isset("round-to-nearest"))
    ansi_c.rounding_mode=ieee_floatt::ROUND_TO_EVEN;

  if(cmdline.isset("round-to-plus-inf"))
    ansi_c.rounding_mode=ieee_floatt::ROUND_TO_PLUS_INF;

  if(cmdline.isset("round-to-minus-inf"))
    ansi_c.rounding_mode=ieee_floatt::ROUND_TO_MINUS_INF;

  if(cmdline.isset("round-to-zero"))
    ansi_c.rounding_mode=ieee_floatt::ROUND_TO_ZERO;

  return false;
}