Module: Ruby_core_source

Defined in:
lib/ruby_core_source.rb

Class Method Summary collapse

Class Method Details

.create_makefile_with_core(hdrs, name) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ruby_core_source.rb', line 12

def create_makefile_with_core(hdrs, name)

  #
  # First, see if the gem already has the needed headers
  #
  if hdrs.call
    create_makefile(name)
    return true
  end

  ruby_dir = ""
  svn_version = nil
  if RUBY_PATCHLEVEL < 0 # -1 means from trunk
    Tempfile.open("preview-revision") { |temp|
      uri = URI.parse("http://cloud.github.com/downloads/mark-moseley/ruby_core_source/preview_revision.yml")
      uri.download(temp)
      revision_map = YAML::load(File.open(temp.path))
      ruby_dir = revision_map[RUBY_REVISION]
      if ruby_dir.nil?
        if RUBY_DESCRIPTION =~ /dev.*trunk (\d+)/
          #svn_version = 'svn-r' + $1 # TODO rdp
          svn_version = $1
        else
          return false
        end
      end
    }
  else
    ruby_dir = "ruby-" + RUBY_VERSION.to_s + "-p" + RUBY_PATCHLEVEL.to_s
  end

  #
  # Check if core headers were already downloaded; if so, use them
  #
  dest_dir = Config::CONFIG["rubyhdrdir"] + "/" + ( ruby_dir || svn_version )
  puts dest_dir
  with_cppflags("-I" + dest_dir) {
    if hdrs.call
      create_makefile(name)
      return true
    end
  }


  #
  # Download the headers
  #
  Tempfile.open("ruby-src") { |temp|

    if !svn_version
      # download it
      uri_path = "http://ftp.ruby-lang.org/pub/ruby/1.9/" + ruby_dir + ".tar.gz"
      temp.binmode
      uri = URI.parse(uri_path)
      uri.download(temp) # download it to temp
      tgz = Zlib::GzipReader.new(File.open(temp, "rb"))
    else
      ruby_dir = svn_version
    end

    FileUtils.mkdir_p(dest_dir)
    Dir.mktmpdir { |dir|
      inc_dir = dir + "/" + ruby_dir + "/*.inc"
      hdr_dir = dir + "/" + ruby_dir + "/*.h"
      if svn_version
        Dir.rmdir dir
        # recreate it
        svn_dir = dir + '/' + svn_version
        system("svn co -r#{svn_version} http://svn.ruby-lang.org/repos/ruby/trunk #{svn_dir}")
        Dir.chdir(svn_dir) do
          # create id.h (needed and must be created for some reason)
          system("sh -c 'autoconf'")
          system("sh -c './configure --disable-doc'")
          system("sh -c 'make'") # todo only build version.h id.h
        end
      else
        Archive::Tar::Minitar.unpack(tgz, dir)
      end
      FileUtils.cp(Dir.glob([ inc_dir, hdr_dir ]), dest_dir)        
    }
  }

  with_cppflags("-I" + dest_dir) {
    if hdrs.call
      create_makefile(name)
      return true
    end
  }
  return false
end