summaryrefslogtreecommitdiff
path: root/tool/make-snapshot
blob: 81f4cb06580e60ccbba5df462395a8d86d02b056 (plain)
1
2
3
4
5
6
7
8
9
10
11
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/ruby -s
# -*- coding: us-ascii -*-
require 'uri'
require 'digest/md5'
require 'digest/sha2'
require 'fileutils'
require 'tmpdir'
STDOUT.sync = true

$exported = nil if $exported == ""
$archname = nil if $archname == ""
$keep_temp ||= nil
$patch_file ||= nil
$tooldir = File.expand_path("..", __FILE__)

def usage
  <<USAGE
usage: #{File.basename $0} [option...] new-directory-to-save [version ...]
options:
  -exported=PATH        make snapshot from already exported working directory
  -archname=NAME        make the basename of snapshots NAME
  -keep_temp            keep temporary working directory
  -patch_file=PATCH     apply PATCH file after export
version:
  trunk, stable, branches/*, tags/*, X.Y.Z, X.Y.Z-pL
each versions may be followed by optional @revision.
USAGE
end

ENV["LC_ALL"] = ENV["LANG"] = "C"
SVNURL = URI.parse("http://svn.ruby-lang.org/repos/ruby/")
RUBY_VERSION_PATTERN = /^\#define\s+RUBY_VERSION\s+"([\d.]+)"/

ENV["VPATH"] ||= "include/ruby"
YACC = ENV["YACC"] ||= "bison"
ENV["BASERUBY"] ||= "ruby"
ENV["RUBY"] ||= "ruby"
ENV["MV"] ||= "mv"
ENV["RM"] ||= "rm -f"
ENV["MINIRUBY"] ||= "ruby"
ENV["PROGRAM"] ||= "ruby"
ENV["AUTOCONF"] ||= "autoconf"

class String
  # for older ruby
  alias bytesize size unless method_defined?(:bytesize)
end

class Dir
  def self.mktmpdir(path)
    path = File.join(tmpdir, path+"-#{$$}-#{rand(100000)}")
    begin
      mkdir(path)
    rescue Errno::EEXIST
      path.succ!
      retry
    end
    path
  end unless respond_to?(:mktmpdir)
end

$patch_file &&= File.expand_path($patch_file)
path = ENV["PATH"].split(File::PATH_SEPARATOR)
%w[YACC BASERUBY RUBY MV MINIRUBY].each do |var|
  cmd = ENV[var]
  unless path.any? {|dir|
      file = File.expand_path(cmd, dir)
      File.file?(file) and File.executable?(file)
    }
    abort "#{File.basename $0}: #{var} command not found - #{cmd}"
  end
end

%w[BASERUBY RUBY MINIRUBY].each do |var|
  `#{ENV[var]} --disable-gem -e1 2>&1`
  if $?.success?
    ENV[var] += ' --disable-gem'
  end
end

if $help or $_help
  puts usage
  exit
end
unless destdir = ARGV.shift
  abort usage
end
revisions = ARGV.empty? ? ["trunk"] : ARGV
unless tmp = $exported
  FileUtils.mkpath(destdir)
  destdir = File.expand_path(destdir)
  tmp = Dir.mktmpdir("ruby-snapshot")
  FileUtils.mkpath(tmp)
  at_exit {
    Dir.chdir "/"
    FileUtils.rm_rf(tmp)
  } unless $keep_temp
end
Dir.chdir tmp

def package(rev, destdir)
  patchlevel = false
  if revision = rev[/@(\d+)\z/, 1]
    rev = $`
  end
  case rev
  when /\Atrunk\z/, /\Abranches\//, /\Atags\//
    url = SVNURL + rev
  when /\Astable\z/
    url = SVNURL + "branches/"
    url = url + `svn ls #{url}`[/.*^(ruby_\d+_\d+)\//m, 1]
  when /\A(.*)\.(.*)\.(.*)-(preview|rc)(\d+)/
    tag = "#{$4}#{$5}"
    url = SVNURL + "tags/v#{$1}_#{$2}_#{$3}_#{$4}#{$5}"
  when /\A(.*)\.(.*)\.(.*)-p(\d+)/
    patchlevel = true
    tag = "p#{$4}"
    url = SVNURL + "tags/v#{$1}_#{$2}_#{$3}_#{$4}"
  when /\./
    url = SVNURL + "branches/ruby_#{rev.tr('.', '_')}"
  else
    warn "#{$0}: unknown version - #{rev}"
    return
  end
  revision ||= `svn info #{url} 2>&1`[/Last Changed Rev: (\d+)/, 1]
  version = nil
  unless revision
    url = SVNURL + "trunk"
    version = `svn cat #{url + "version.h"}`[RUBY_VERSION_PATTERN, 1]
    unless rev == version
      warn "#{$0}: #{rev} not found"
      return
    end
    revision = `svn info #{url}`[/Last Changed Rev: (\d+)/, 1]
  end
  v = nil
  if $exported
    if String === $exported
      v = $exported
    end
  else
    v = "ruby"
    puts "Exporting #{rev}@#{revision}"
    IO.popen("svn export -r #{revision} #{url} #{v}") do |pipe|
      pipe.each {|line| /^A/ =~ line or print line}
    end
    unless $?.success?
      warn("Export failed")
      return
    end
  end

  if !File.directory?(v)
    v = Dir.glob("ruby-*").select(&File.method(:directory?))
    v.size == 1 or abort "not exported"
    v = v[0]
  end
  open("#{v}/revision.h", "wb") {|f| f.puts "#define RUBY_REVISION #{revision}"}
  open("#{v}/.revision.time", "wb") {}
  version ||= (versionhdr = IO.read("#{v}/version.h"))[RUBY_VERSION_PATTERN, 1]
  version or return
  if patchlevel
    versionhdr ||= IO.read("#{v}/version.h")
    patchlevel = versionhdr[/^\#define\s+RUBY_PATCHLEVEL\s+(\d+)/, 1]
    tag = (patchlevel ? "p#{patchlevel}" : "r#{revision}")
  else
    tag ||= "r#{revision}"
  end
  unless v == $exported
    n = "ruby-#{version}-#{tag}"
    File.directory?(n) or File.rename v, n
    v = n
  end
  system("patch -d #{v} -p0 -i #{$patch_file}") if $patch_file
  "take a breath, and go ahead".scan(/./) {|c|print c; sleep(c == "," ? 0.7 : 0.05)}; puts
  def (clean = []).add(n) push(n); n end
  Dir.chdir(v) do
    %w[config.guess config.sub].each do |conf|
      next if File.exist?("tool/#{conf}")
      require File.expand_path("config_files", $tooldir)
      ConfigFiles.download(conf, "tool")
    end
    File.open(clean.add("cross.rb"), "w") do |f|
      f.puts "Object.__send__(:remove_const, :CROSS_COMPILING) if defined?(CROSS_COMPILING)"
      f.puts "CROSS_COMPILING=true"
    end
    unless File.exist?("configure")
      print "creating configure..."
      unless system(ENV["AUTOCONF"])
        puts " failed"
        return
      end
      puts " done"
    end
    clean.add("autom4te.cache")
    print "creating prerequisites..."
    if File.file?("common.mk") && /^prereq/ =~ commonmk = IO.read("common.mk")
      puts
      extout = clean.add('tmp')
      File.open(clean.add("config.status"), "w") {|f|
        f.puts "s,@configure_args@,|#_!!_#|,g"
        f.puts "s,@EXTOUT@,|#_!!_#|#{extout},g"
        f.puts "s,@bindir@,|#_!!_#|,g"
        f.puts "s,@ruby_install_name@,|#_!!_#|,g"
        f.puts "s,@ARCH_FLAG@,|#_!!_#|,g"
        f.puts "s,@CFLAGS@,|#_!!_#|,g"
        f.puts "s,@CPPFLAGS@,|#_!!_#|,g"
        f.puts "s,@LDFLAGS@,|#_!!_#|,g"
        f.puts "s,@DLDFLAGS@,|#_!!_#|,g"
        f.puts "s,@LIBEXT@,|#_!!_#|a,g"
        f.puts "s,@OBJEXT@,|#_!!_#|o,g"
        f.puts "s,@EXEEXT@,|#_!!_#|,g"
        f.puts "s,@LIBRUBY@,|#_!!_#|libruby.a,g"
        f.puts "s,@LIBRUBY_A@,|#_!!_#|libruby.a,g"
        f.puts "s,@RM@,|#_!!_#|rm -f,g"
        f.puts "s,@CP@,|#_!!_#|cp,g"
        f.puts "s,@rubyarchdir@,|#_!!_#|,g"
      }
      FileUtils.mkpath(hdrdir = "#{extout}/include/ruby")
      File.open("#{hdrdir}/config.h", "w") {}
      miniruby = ENV['MINIRUBY'] + " -r./cross"
      IO.popen("make -f - prereq"\
               " srcdir=. CHDIR=cd PATH_SEPARATOR='#{File::PATH_SEPARATOR}'"\
               " IFCHANGE=tool/ifchange MAKEDIRS='mkdir -p'"\
               " 'MINIRUBY=#{miniruby}' 'RUBY=#{ENV["RUBY"]}'", "w") do |f|
        f.puts(IO.read("Makefile.in").gsub(/^@.*\n/, '').gsub(/@([A-Za-z_]\w*)@/) {ENV[$1]})
        f.puts(commonmk.gsub(/\{[^{}]*\}/, ""))
      end
      clean.push("rbconfig.rb", ".rbconfig.time", "enc.mk")
      print "prerequisites"
    else
      system("#{YACC} -o parse.c parse.y")
    end
    FileUtils.rm_rf(clean)
    unless $?.success?
      puts " failed"
      return
    end
    puts " done"
  end

  if v == "."
    v = File.basename(Dir.pwd)
    Dir.chdir ".."
  else
    Dir.chdir(File.dirname(v))
    v = File.basename(v)
  end

  return [["bzip tarball", ".tar.bz2", %w"tar cjf"],
          ["gzip tarball", ".tar.gz", %w"tar czf"],
          ["zip archive", ".zip", %w"zip -qr"]
         ].collect do |mesg, ext, cmd|
    file = File.join(destdir, "#{v||$archname}#{ext}")
    print "creating #{mesg}... #{file}"
    if system(*(cmd + [file, v]))
      puts " done"
      file
    else
      puts " failed"
      nil
    end
  end.compact
ensure
  FileUtils.rm_rf(v) if v and !$exported and !$keep_temp
end

success = true
revisions.collect {|rev| package(rev, destdir)}.flatten.each do |name|
  if !name
    success = false
    next
  end
  str = open(name, "rb") {|f| f.read}
  md5 = Digest::MD5.hexdigest str
  sha = Digest::SHA256.hexdigest str
  puts "* #{name}"
  puts "  SIZE:   #{str.bytesize} bytes"
  puts "  MD5:    #{md5}"
  puts "  SHA256: #{sha}"
  puts
end

exit false if !success

# vim:fileencoding=US-ASCII sw=2 ts=4 noexpandtab ff=unix