예제 #1
0
  public void load(Ruby runtime, boolean wrap) throws IOException {
    RubyModule timeout = runtime.defineModule("Timeout");
    RubyClass superclass = runtime.getRuntimeError();
    RubyClass timeoutError =
        runtime.defineClassUnder("Error", superclass, superclass.getAllocator(), timeout);
    runtime.defineClassUnder(
        "ExitException", runtime.getException(), runtime.getException().getAllocator(), timeout);

    // Here we create an "anonymous" exception type used for unrolling the stack.
    // MRI creates a new one for *every call* to timeout, which can be costly.
    // We opt to use a single exception type for all cases to avoid this overhead.
    RubyClass anonEx =
        runtime.defineClassUnder(
            "AnonymousException",
            runtime.getException(),
            runtime.getException().getAllocator(),
            timeout);
    anonEx.setBaseName(null); // clear basename so it's anonymous when raising

    // These are not really used by timeout, but exposed for compatibility
    timeout.defineConstant(
        "THIS_FILE", RubyRegexp.newRegexp(runtime, "timeout\\.rb", new RegexpOptions()));
    timeout.defineConstant("CALLER_OFFSET", RubyFixnum.newFixnum(runtime, 0));

    // Timeout module methods
    timeout.defineAnnotatedMethods(Timeout.class);

    // Toplevel defines
    runtime.getObject().defineConstant("TimeoutError", timeoutError);
    runtime.getObject().defineAnnotatedMethods(TimeoutToplevel.class);
  }
예제 #2
0
  @Override
  public void load(Ruby ruby, boolean wrap) throws IOException {
    RubyModule pg = ruby.defineModule("PG");
    ruby.defineClassUnder(
        "Error", ruby.getStandardError(), ruby.getStandardError().getAllocator(), pg);
    RubyModule pgConstants = ruby.defineModuleUnder("Constants", pg);

    // create the connection status constants
    for (ConnectionState status : ConnectionState.values())
      pgConstants.defineConstant(status.name(), ruby.newFixnum(status.ordinal()));

    for (TransactionStatus status : TransactionStatus.values())
      pgConstants.defineConstant(status.name(), ruby.newFixnum(status.ordinal()));

    for (ResultStatus status : ResultStatus.values())
      pgConstants.defineConstant(status.name(), ruby.newFixnum(status.ordinal()));

    // create the large object constants
    pgConstants.defineConstant("INV_READ", new RubyFixnum(ruby, LargeObjectAPI.READ));
    pgConstants.defineConstant("INV_WRITE", new RubyFixnum(ruby, LargeObjectAPI.WRITE));
    pgConstants.defineConstant("SEEK_SET", new RubyFixnum(ruby, LargeObjectAPI.SEEK_SET));
    pgConstants.defineConstant("SEEK_END", new RubyFixnum(ruby, LargeObjectAPI.SEEK_END));
    pgConstants.defineConstant("SEEK_CUR", new RubyFixnum(ruby, LargeObjectAPI.SEEK_CUR));

    // create error fields objects
    for (ErrorField field : ErrorResponse.ErrorField.values())
      pgConstants.defineConstant(field.name(), ruby.newFixnum(field.getCode()));

    pg.getSingletonClass().defineAnnotatedMethods(Postgresql.class);

    try {
      for (java.lang.reflect.Field field : Oid.class.getDeclaredFields()) {
        String name = field.getName();
        int value = field.getInt(null);
        pgConstants.defineConstant("OID_" + name, ruby.newFixnum(value));
      }
    } catch (Exception e) {
      ruby.newRuntimeError(e.getLocalizedMessage());
    }

    pgConstants.defineConstant("INVALID_OID", ruby.newFixnum(Oid.UNSPECIFIED));
    pg.includeModule(pgConstants);
    Connection.define(ruby, pg, pgConstants);
    Result.define(ruby, pg, pgConstants);
  }
예제 #3
0
  private static void setupMakefileConfig(RubyModule configModule, RubyHash mkmfHash) {
    Ruby ruby = configModule.getRuntime();

    RubyHash envHash = (RubyHash) ruby.getObject().fetchConstant("ENV".intern());
    String cc = getRubyEnv(envHash, "CC", "cc");
    String cpp = getRubyEnv(envHash, "CPP", "cc -E");
    String cxx = getRubyEnv(envHash, "CXX", "c++");

    String jflags = " -fno-omit-frame-pointer -fno-strict-aliasing ";
    // String oflags = " -O2  -DNDEBUG";
    // String wflags = " -W -Werror -Wall -Wno-unused -Wno-parentheses ";
    // String picflags = true ? "" : " -fPIC -pthread ";
    // String iflags = " -I\"$(JDK_HOME)/include\" -I\"$(JDK_HOME)/include/$(OS)\"
    // -I\"$(BUILD_DIR)\" ";
    // String soflags = true ? "" : " -shared -static-libgcc -mimpure-text -Wl,-O1 ";

    String cflags = jflags + " -fexceptions" /* + picflags */ + " $(cflags)";
    String cppflags = " $(DEFS) $(cppflags)";
    String cxxflags = cflags + " $(cxxflags)";
    String ldflags = ""; // + soflags;
    String dldflags = "";
    String ldsharedflags = " -shared ";

    String archflags = " -m" + (Platform.IS_64_BIT ? "64" : "32");

    String hdr_dir = new NormalizedFile(normalizedHome, "lib/native/include/").getPath();

    // A few platform specific values
    if (Platform.IS_WINDOWS) {
      ldflags +=
          " -L"
              + new NormalizedFile(
                      normalizedHome,
                      "lib/native/" + (Platform.IS_64_BIT ? "x86_64" : "i386") + "-Windows")
                  .getPath();
      ldflags += " -ljruby-cext";
      ldsharedflags += " $(if $(filter-out -g -g0,$(debugflags)),,-s)";
      dldflags = "-Wl,--enable-auto-image-base,--enable-auto-import $(DEFFILE)";
      archflags += " -march=native -mtune=native";
      setConfig(mkmfHash, "DLEXT", "dll");
    } else if (Platform.IS_MAC) {
      ldsharedflags = " -dynamic -bundle -undefined dynamic_lookup ";
      cflags = " -fPIC -DTARGET_RT_MAC_CFM=0 " + cflags;
      archflags = " -arch " + Platform.ARCH;
      cppflags = " -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE " + cppflags;
      setConfig(mkmfHash, "DLEXT", "bundle");
    } else {
      cflags = " -fPIC " + cflags;
      setConfig(mkmfHash, "DLEXT", "so");
    }

    String libext = "a";
    String objext = "o";

    setConfig(mkmfHash, "configure_args", "");
    setConfig(mkmfHash, "CFLAGS", cflags);
    setConfig(mkmfHash, "CPPFLAGS", cppflags);
    setConfig(mkmfHash, "CXXFLAGS", cxxflags);
    setConfig(mkmfHash, "ARCH_FLAG", archflags);
    setConfig(mkmfHash, "LDFLAGS", ldflags);
    setConfig(mkmfHash, "DLDFLAGS", dldflags);
    setConfig(mkmfHash, "DEFS", "");
    setConfig(mkmfHash, "LIBEXT", libext);
    setConfig(mkmfHash, "OBJEXT", objext);
    setConfig(mkmfHash, "LIBRUBYARG_STATIC", "");
    setConfig(mkmfHash, "LIBRUBYARG_SHARED", "");
    setConfig(mkmfHash, "LIBS", "");
    setConfig(mkmfHash, "DLDLIBS", "");
    setConfig(mkmfHash, "ENABLED_SHARED", "");
    setConfig(mkmfHash, "LIBRUBY", "");
    setConfig(mkmfHash, "LIBRUBY_A", "");
    setConfig(mkmfHash, "LIBRUBYARG", "");
    setConfig(mkmfHash, "prefix", " "); // This must not be empty for some extconf.rb's to work
    setConfig(mkmfHash, "ruby_install_name", jrubyScript());
    setConfig(mkmfHash, "LDSHARED", cc + ldsharedflags);
    setConfig(mkmfHash, "LDSHAREDXX", cxx + ldsharedflags);
    setConfig(mkmfHash, "RUBY_PLATFORM", getOSName());
    setConfig(mkmfHash, "CC", cc);
    setConfig(mkmfHash, "CPP", cpp);
    setConfig(mkmfHash, "CXX", cxx);
    setConfig(mkmfHash, "OUTFLAG", "-o ");
    setConfig(mkmfHash, "COMMON_HEADERS", "ruby.h");
    setConfig(mkmfHash, "PATH_SEPARATOR", ":");
    setConfig(mkmfHash, "INSTALL", "install -c ");
    setConfig(mkmfHash, "RM", "rm -f");
    setConfig(mkmfHash, "CP", "cp ");
    setConfig(mkmfHash, "MAKEDIRS", "mkdir -p ");
    setConfig(mkmfHash, "includedir", hdr_dir);
    setConfig(mkmfHash, "rubyhdrdir", hdr_dir);
    setConfig(mkmfHash, "archdir", hdr_dir);

    ruby.getObject().defineConstant("CROSS_COMPILING", ruby.getNil());

    configModule.defineConstant("MAKEFILE_CONFIG", mkmfHash);
  }
예제 #4
0
  /**
   * Just enough configuration settings (most don't make sense in Java) to run the rubytests unit
   * tests. The tests use <code>bindir</code>, <code>RUBY_INSTALL_NAME</code> and <code>EXEEXT
   * </code>.
   */
  public void load(Ruby runtime, boolean wrap) {
    RubyModule configModule;

    configModule = runtime.defineModule("RbConfig");
    RubyKernel.autoload(
        runtime.getObject(),
        runtime.newSymbol("Config"),
        runtime.newString("rbconfig/obsolete.rb"));

    configModule.defineAnnotatedMethods(RbConfigLibrary.class);

    RubyHash configHash = RubyHash.newHash(runtime);
    configModule.defineConstant("CONFIG", configHash);

    String[] versionParts;
    versionParts = Constants.RUBY_VERSION.split("\\.");

    setConfig(configHash, "MAJOR", versionParts[0]);
    setConfig(configHash, "MINOR", versionParts[1]);
    setConfig(configHash, "TEENY", versionParts[2]);
    setConfig(configHash, "ruby_version", versionParts[0] + '.' + versionParts[1] + ".0");
    // Rubygems is too specific on host cpu so until we have real need lets default to universal
    // setConfig(configHash, "arch", System.getProperty("os.arch") + "-java" +
    // System.getProperty("java.specification.version"));
    setConfig(
        configHash, "arch", "universal-java" + System.getProperty("java.specification.version"));

    normalizedHome = getNormalizedHome(runtime);

    // Use property for binDir if available, otherwise fall back to common bin default
    String binDir = SafePropertyAccessor.getProperty("jruby.bindir");
    if (binDir == null) {
      binDir = new NormalizedFile(normalizedHome, "bin").getPath();
    }
    setConfig(configHash, "bindir", binDir);

    setConfig(configHash, "RUBY_INSTALL_NAME", jrubyScript());
    setConfig(configHash, "RUBYW_INSTALL_NAME", IS_WINDOWS ? "jrubyw.exe" : jrubyScript());
    setConfig(configHash, "ruby_install_name", jrubyScript());
    setConfig(configHash, "rubyw_install_name", IS_WINDOWS ? "jrubyw.exe" : jrubyScript());
    setConfig(configHash, "SHELL", jrubyShell());
    setConfig(configHash, "prefix", normalizedHome);
    setConfig(configHash, "exec_prefix", normalizedHome);

    setConfig(configHash, "host_os", getOSName());
    setConfig(configHash, "host_vendor", System.getProperty("java.vendor"));
    setConfig(configHash, "host_cpu", getArchitecture());

    setConfig(configHash, "target_os", getOSName());

    setConfig(configHash, "target_cpu", getArchitecture());

    String jrubyJarFile = "jruby.jar";
    URL jrubyPropertiesUrl = Ruby.getClassLoader().getResource("/org/jruby/Ruby.class");
    if (jrubyPropertiesUrl != null) {
      Pattern jarFile =
          Pattern.compile("jar:file:.*?([a-zA-Z0-9.\\-]+\\.jar)!" + "/org/jruby/Ruby.class");
      Matcher jarMatcher = jarFile.matcher(jrubyPropertiesUrl.toString());
      jarMatcher.find();
      if (jarMatcher.matches()) {
        jrubyJarFile = jarMatcher.group(1);
      }
    }
    setConfig(configHash, "LIBRUBY", jrubyJarFile);
    setConfig(configHash, "LIBRUBY_SO", jrubyJarFile);
    setConfig(configHash, "LIBRUBY_SO", jrubyJarFile);
    setConfig(configHash, "LIBRUBY_ALIASES", jrubyJarFile);

    setConfig(configHash, "build", Constants.BUILD);
    setConfig(configHash, "target", Constants.TARGET);

    String shareDir = new NormalizedFile(normalizedHome, "share").getPath();
    String includeDir = new NormalizedFile(normalizedHome, "lib/native/" + getOSName()).getPath();

    String vendorDirGeneral = getVendorDirGeneral(runtime);
    String siteDirGeneral = getSiteDirGeneral(runtime);
    String rubySharedLibDir = getRubySharedLibDir(runtime);
    String rubyLibDir = getRubyLibDir(runtime);
    String archDir = getArchDir(runtime);
    String vendorDir = getVendorDir(runtime);
    String vendorLibDir = getVendorLibDir(runtime);
    String vendorArchDir = getVendorArchDir(runtime);
    String siteDir = getSiteDir(runtime);
    String siteLibDir = getSiteLibDir(runtime);
    String siteArchDir = getSiteArchDir(runtime);
    String sysConfDir = getSysConfDir(runtime);

    setConfig(configHash, "libdir", vendorDirGeneral);
    setConfig(configHash, "rubylibprefix", vendorDirGeneral + "/ruby");
    setConfig(configHash, "rubylibdir", rubyLibDir);
    setConfig(configHash, "rubysharedlibdir", rubySharedLibDir);
    if (!isSiteVendorSame(runtime)) {
      setConfig(configHash, "vendordir", vendorDir);
      setConfig(configHash, "vendorlibdir", vendorLibDir);
      setConfig(configHash, "vendorarchdir", vendorArchDir);
    }
    setConfig(configHash, "sitedir", siteDir);
    setConfig(configHash, "sitelibdir", siteLibDir);
    setConfig(configHash, "sitearchdir", siteArchDir);
    setConfig(configHash, "sitearch", "java");
    setConfig(configHash, "archdir", archDir);
    setConfig(configHash, "topdir", archDir);
    setConfig(configHash, "includedir", includeDir);
    setConfig(configHash, "configure_args", "");
    setConfig(configHash, "datadir", shareDir);
    setConfig(configHash, "mandir", new NormalizedFile(normalizedHome, "man").getPath());
    setConfig(configHash, "sysconfdir", sysConfDir);
    setConfig(configHash, "localstatedir", new NormalizedFile(normalizedHome, "var").getPath());
    setConfig(configHash, "DLEXT", "jar");
    if (getRubygemsDir(runtime) != null) {
      setConfig(configHash, "rubygemsdir", new NormalizedFile(getRubygemsDir(runtime)).getPath());
    }

    if (Platform.IS_WINDOWS) {
      setConfig(configHash, "EXEEXT", ".exe");
    } else {
      setConfig(configHash, "EXEEXT", "");
    }

    setConfig(configHash, "ridir", new NormalizedFile(shareDir, "ri").getPath());

    // These will be used as jruby defaults for rubygems if found
    String gemhome = SafePropertyAccessor.getProperty("jruby.gem.home");
    String gempath = SafePropertyAccessor.getProperty("jruby.gem.path");
    if (gemhome != null) setConfig(configHash, "default_gem_home", gemhome);
    if (gempath != null) setConfig(configHash, "default_gem_path", gempath);

    setConfig(configHash, "joda-time.version", Constants.JODA_TIME_VERSION);
    setConfig(configHash, "tzdata.version", Constants.TZDATA_VERSION);

    RubyHash mkmfHash = RubyHash.newHash(runtime);

    setConfig(mkmfHash, "libdir", vendorDirGeneral);
    setConfig(mkmfHash, "arch", "java");
    setConfig(mkmfHash, "rubylibdir", rubyLibDir);
    setConfig(mkmfHash, "rubysharedlibdir", rubySharedLibDir);
    if (!isSiteVendorSame(runtime)) {
      setConfig(mkmfHash, "vendordir", vendorDir);
      setConfig(mkmfHash, "vendorlibdir", vendorLibDir);
      setConfig(mkmfHash, "vendorarchdir", vendorArchDir);
    }
    setConfig(mkmfHash, "sitedir", siteDir);
    setConfig(mkmfHash, "sitelibdir", siteLibDir);
    setConfig(mkmfHash, "sitearchdir", siteArchDir);
    setConfig(mkmfHash, "sitearch", "java");
    setConfig(mkmfHash, "archdir", archDir);
    setConfig(mkmfHash, "topdir", archDir);
    setConfig(mkmfHash, "configure_args", "");
    setConfig(mkmfHash, "datadir", new NormalizedFile(normalizedHome, "share").getPath());
    setConfig(mkmfHash, "mandir", new NormalizedFile(normalizedHome, "man").getPath());
    setConfig(mkmfHash, "sysconfdir", sysConfDir);
    setConfig(mkmfHash, "localstatedir", new NormalizedFile(normalizedHome, "var").getPath());
    if (getRubygemsDir(runtime) != null) {
      setConfig(mkmfHash, "rubygemsdir", new NormalizedFile(getRubygemsDir(runtime)).getPath());
    }

    setupMakefileConfig(configModule, mkmfHash);

    runtime.getLoadService().load("jruby/kernel/rbconfig.rb", false);
  }