summaryrefslogtreecommitdiff
path: root/tool/make-snapshot
blob: ad5fd879f38f38c12d19b0139a9ee9f4516eb685 (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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
#!/usr/bin/ruby -s
# -*- coding: us-ascii -*-
require 'uri'
require 'digest/sha1'
require 'digest/sha2'
require 'fileutils'
require 'shellwords'
require 'tmpdir'
require 'pathname'
require File.expand_path("../lib/vcs", __FILE__)
require File.expand_path("../lib/colorize", __FILE__)
STDOUT.sync = true

$srcdir ||= nil
$exported = nil if ($exported ||= nil) == ""
$archname = nil if ($archname ||= nil) == ""
$keep_temp ||= nil
$patch_file ||= nil
$packages ||= nil
$digests ||= nil
$s3 ||= nil
$tooldir = File.expand_path("..", __FILE__)
$unicode_version = nil if ($unicode_version ||= nil) == ""
$colorize = Colorize.new

def usage
  <<USAGE
usage: #{File.basename $0} [option...] new-directory-to-save [version ...]
options:
  -srcdir=PATH          source directory path
  -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
  -packages=PKG[,...]   make PKG packages (#{PACKAGES.keys.join(", ")})
  -digests=ALG[,...]    show ALG digests (#{DIGESTS.join(", ")})
  -unicode_version=VER  Unicode version to generate encodings
  -svn[=URL]            make snapshot from SVN repository
                        (#{SVNURL})
  -git[=URL]            make snapshot from GIT repository
                        (#{GITURL})
  -s3=PATH              s3 bucket path(default=tmp)
  -help, --help         show this message
version:
  master, trunk, stable, branches/*, tags/*, X.Y, X.Y.Z, X.Y.Z-pL
each versions may be followed by optional @revision.
USAGE
end

DIGESTS = %w[SHA1 SHA256 SHA512]
PACKAGES = {
  "bzip" => %w".tar.bz2 bzip2 -c",
  "gzip" => %w".tar.gz  gzip -c",
  "xz"   => %w".tar.xz  xz -c",
  "zip"  => %w".zip     zip -qr",
}

if system("7z", out: IO::NULL)
  PACKAGES["gzip"] = %w".tar.gz 7z a dummy -tgzip -mx -so"
  PACKAGES["zip"]  = %w".zip    7z a -tzip  -mx" << {out: IO::NULL}
end
if gzip = ENV.delete("GZIP")
  PACKAGES["gzip"].concat(gzip.shellsplit)
end

if mflags = ENV["GNUMAKEFLAGS"] and /\A-(\S*)j\d*/ =~ mflags
  mflags = mflags.gsub(/(\A|\s)(-\S*)j\d*/, '\1\2')
  mflags.strip!
  ENV["GNUMAKEFLAGS"] = (mflags unless mflags.empty?)
end
ENV["LC_ALL"] = ENV["LANG"] = "C"
SVNURL = URI.parse("https://svn.ruby-lang.org/repos/ruby/")
# https git clone is disabled at git.ruby-lang.org/ruby.git.
GITURL = URI.parse("https://github.com/ruby/ruby.git")
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"
ENV["BUILTIN_TRANSOBJS"] ||= "newline.o"

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

$packages &&= $packages.split(/[, ]+/).tap {|pkg|
  if all = pkg.index("all")
    pkg[all, 1] = PACKAGES.keys - pkg
  end
  pkg -= PACKAGES.keys
  pkg.empty? or abort "#{File.basename $0}: unknown packages - #{pkg.join(", ")}"
}
$packages ||= PACKAGES.keys

$digests &&= $digests.split(/[, ]+/).tap {|dig|
  dig -= DIGESTS
  dig.empty? or abort "#{File.basename $0}: unknown digests - #{dig.join(", ")}"
}
$digests ||= DIGESTS

$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].shellsplit
  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|
  %x[#{ENV[var]} --disable-gem -e1 2>&1]
  if $?.success?
    ENV[var] += ' --disable-gem'
  end
end

if defined?($help) or defined?($_help)
  puts usage
  exit
end
unless destdir = ARGV.shift
  abort usage
end
revisions = ARGV.empty? ? [nil] : 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

def tar_create(tarball, dir)
  require 'rubygems'
  require 'rubygems/package'
  require 'rubygems/package/tar_writer'
  header = Gem::Package::TarHeader
  dir_type = "5"
  uname = gname = "ruby"
  File.open(tarball, "wb") do |f|
    w = Gem::Package::TarWriter.new(f)
    Dir.glob("#{dir}/**/*", File::FNM_DOTMATCH) do |path|
      next if File.basename(path) == "."
      s = File.stat(path)
      mode = 0644
      case
      when s.file?
        type = nil
        size = s.size
        mode |= 0111 if s.executable?
      when s.directory?
        path += "/"
        type = dir_type
        size = 0
        mode |= 0111
      else
        next
      end
      name, prefix = w.split_name(path)
      h = header.new(name: name, prefix: prefix, typeflag: type,
                     mode: mode, size: size, mtime: s.mtime,
                     uname: uname, gname: gname)
      f.write(h)
      if size > 0
        IO.copy_stream(path, f)
        f.write("\0" * (-size % 512))
      end
    end
  end
  true
rescue => e
  warn e.message
  false
end

def package(vcs, rev, destdir, tmp = nil)
  patchlevel = false
  prerelease = false
  if rev and revision = rev[/@(\d+)\z/, 1]
    rev = $`
  end
  case rev
  when nil
    url = nil
  when /\A(?:master|trunk)\z/
    url = vcs.trunk
  when /\Abranches\//
    url = vcs.branch($')
  when /\Atags\//
    url = vcs.tag($')
  when /\Astable\z/
    vcs.branch_list("ruby_[0-9]*") {|n| url = n[/\Aruby_\d+_\d+\z/]}
    url &&= vcs.branch(url)
  when /\A(.*)\.(.*)\.(.*)-(preview|rc)(\d+)/
    prerelease = true
    tag = "#{$4}#{$5}"
    url = vcs.tag("v#{$1}_#{$2}_#{$3}_#{$4}#{$5}")
  when /\A(.*)\.(.*)\.(.*)-p(\d+)/
    patchlevel = true
    tag = "p#{$4}"
    url = vcs.tag("v#{$1}_#{$2}_#{$3}_#{$4}")
  when /\A(\d+)\.(\d+)(?:\.(\d+))?\z/
    if $3 && ($1 > "2" || $1 == "2" && $2 >= "1")
      patchlevel = true
      tag = ""
      url = vcs.tag("v#{$1}_#{$2}_#{$3}")
    else
      url = vcs.branch("ruby_#{rev.tr('.', '_')}")
    end
  else
    warn "#{$0}: unknown version - #{rev}"
    return
  end
  if !revision and revision = vcs.get_revisions(url)
    url ||= vcs.branch(revision[3])
    revision = revision[0]
  end
  version = nil
  unless revision
    url = vcs.trunk
    vcs.grep(RUBY_VERSION_PATTERN, url, "version.h") {version = $1}
    unless rev == version
      warn "#{$0}: #{rev} not found"
      return
    end
    revision = vcs.get_revisions(url)[0]
  end
  v = nil
  if $exported
    if String === $exported
      v = $exported
    end
  else
    v = "ruby"
    puts "Exporting #{rev}@#{revision}"
    exported = tmp ? File.join(tmp, v) : v
    unless vcs.export(revision, url, exported, true) {|line| print line}
      warn("Export failed")
      return
    end
    if $srcdir
      Dir.glob($srcdir + "/{tool/config.{guess,sub},gems/*.gem,.downloaded-cache/*,enc/unicode/data/**/*.txt}") do |file|
        puts "copying #{file}"
        dest = exported + file[$srcdir.size..-1]
        FileUtils.mkpath(File.dirname(dest))
        begin
          FileUtils.ln(file, dest, force: true)
          next unless File.symlink?(dest)
          File.unlink(dest)
        rescue SystemCallError
        end
        begin
          FileUtils.cp_r(file, dest, preserve: true)
        rescue SystemCallError
        end
      end
    end
  end

  status = IO.read(File.dirname(__FILE__) + "/prereq.status")
  Dir.chdir(tmp) if tmp

  if !File.directory?(v)
    v = Dir.glob("ruby-*").select(&File.method(:directory?))
    v.size == 1 or abort "#{File.basename $0}: not exported"
    v = v[0]
  end

  open("#{v}/revision.h", "wb") {|f|
    short = vcs.short_revision(revision)
    f.puts "#define RUBY_REVISION #{short.inspect}"
    unless short == revision
      f.puts "#define RUBY_FULL_REVISION #{revision.inspect}"
    end
  }
  version ||= (versionhdr = IO.read("#{v}/version.h"))[RUBY_VERSION_PATTERN, 1]
  version ||=
    begin
      include_ruby_versionhdr = IO.read("#{v}/include/ruby/version.h")
      api_major_version = include_ruby_versionhdr[/^\#define\s+RUBY_API_VERSION_MAJOR\s+([\d.]+)/, 1]
      api_minor_version = include_ruby_versionhdr[/^\#define\s+RUBY_API_VERSION_MINOR\s+([\d.]+)/, 1]
      version_teeny = versionhdr[/^\#define\s+RUBY_VERSION_TEENY\s+(\d+)/, 1]
      [api_major_version, api_minor_version, version_teeny].join('.')
    end
  version or return
  if patchlevel
    unless tag.empty?
      versionhdr ||= IO.read("#{v}/version.h")
      patchlevel = versionhdr[/^\#define\s+RUBY_PATCHLEVEL\s+(\d+)/, 1]
      tag = (patchlevel ? "p#{patchlevel}" : vcs.revision_name(revision))
    end
  elsif prerelease
    versionhdr ||= IO.read("#{v}/version.h")
    versionhdr.sub!(/^\#define\s+RUBY_PATCHLEVEL_STR\s+"\K.+?(?=")/, tag)
    IO.write("#{v}/version.h", versionhdr)
  else
    tag ||= vcs.revision_name(revision)
  end
  unless v == $exported
    if $archname
      n = $archname
    elsif tag.empty?
      n = "ruby-#{version}"
    else
      n = "ruby-#{version}-#{tag}"
    end
    File.directory?(n) or File.rename v, n
    v = n
  end
  system(*%W"patch -d #{v} -p0 -i #{$patch_file}") if $patch_file
  if !$exported or $patch_file
    colors = %w[red yellow green cyan blue magenta]
    "take a breath, and go ahead".scan(/./) do |c|
      if c == ' '
        print c
      else
        colors.push(color = colors.shift)
        print $colorize.decorate(c, color)
      end
      sleep(c == "," ? 0.7 : 0.05)
    end
    puts
  end
  def (clean = []).add(n) push(n); n end
  Dir.chdir(v) do
    unless File.exist?("ChangeLog")
      vcs.export_changelog(url, nil, revision, "ChangeLog")
    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"
      f.puts "Object.__send__(:remove_const, :RUBY_PLATFORM)"
      f.puts "RUBY_PLATFORM='none'"
      f.puts "Object.__send__(:remove_const, :RUBY_VERSION)"
      f.puts "RUBY_VERSION='#{version}'"
    end
    unless File.exist?("configure")
      print "creating configure..."
      unless system([ENV["AUTOCONF"]]*2)
        puts $colorize.fail(" failed")
        return
      end
      puts $colorize.pass(" done")
    end
    clean.add("autom4te.cache")
    clean.add("enc/unicode/data")
    print "creating prerequisites..."
    if File.file?("common.mk") && /^prereq/ =~ commonmk = IO.read("common.mk")
      puts
      extout = clean.add('tmp')
      begin
        status = IO.read("tool/prereq.status")
      rescue Errno::ENOENT
        # use fallback file
      end
      File.open(clean.add("config.status"), "w") {|f|
        f.print status
      }
      FileUtils.mkpath(hdrdir = "#{extout}/include/ruby")
      File.open("#{hdrdir}/config.h", "w") {}
      FileUtils.mkpath(defaults = "#{extout}/rubygems/defaults")
      File.open("#{defaults}/operating_system.rb", "w") {}
      File.open("#{defaults}/ruby.rb", "w") {}
      miniruby = ENV['MINIRUBY'] + " -I. -I#{extout} -rcross"
      baseruby = ENV["BASERUBY"]
      mk = (IO.read("template/Makefile.in") rescue IO.read("Makefile.in")).
             gsub(/^@.*\n/, '')
      vars = {
        "EXTOUT"=>extout,
        "PATH_SEPARATOR"=>File::PATH_SEPARATOR,
        "MINIRUBY"=>miniruby,
        "RUBY"=>ENV["RUBY"],
        "BASERUBY"=>baseruby,
        "PWD"=>Dir.pwd,
        "ruby_version"=>version,
        "MAJOR"=>api_major_version,
        "MINOR"=>api_minor_version,
        "TEENY"=>version_teeny,
      }
      status.scan(/^s([%,])@([A-Za-z_][A-Za-z_0-9]*)@\1(.*?)\1g$/) do
        vars[$2] ||= $3
      end
      vars.delete("UNICODE_FILES") # for stable branches
      vars["UNICODE_VERSION"] = $unicode_version if $unicode_version
      args = vars.dup
      mk.gsub!(/@([A-Za-z_]\w*)@/) {args.delete($1); vars[$1] || ENV[$1]}
      mk << commonmk.gsub(/\{\$([^(){}]*)[^{}]*\}/, "")
      mk << <<-'APPEND'

prepare-package: touch-unicode-files
prepare-package: prereq update-download clean-cache $(CLEAN_CACHE)
clean-cache $(CLEAN_CACHE): after-update
update-download:: update-gems
after-update:: extract-gems
extract-gems: update-gems
update-gems:
$(UNICODE_SRC_DATA_DIR)/.unicode-tables.time:
touch-unicode-files:
      APPEND
      open(clean.add("Makefile"), "w") do |f|
        f.puts mk
      end
      ENV["CACHE_SAVE"] = "no"
      system(ENV["MAKE"] || ENV["make"] || "make", "prepare-package", *args.map {|arg| arg.join("=")})
      clean.push("rbconfig.rb", ".rbconfig.time", "enc.mk")
      print "prerequisites"
    else
      system(*%W"#{YACC} -o parse.c parse.y")
    end
    vcs.after_export(".") if exported
    FileUtils.rm_rf(clean) unless $keep_temp
    FileUtils.rm_rf(".downloaded-cache")
    if File.exist?("gems/bundled_gems")
      gems = Dir.glob("gems/*.gem")
      gems -= File.readlines("gems/bundled_gems").map {|line|
        name, version, _ = line.split(' ')
        "gems/#{name}-#{version}.gem"
      }
      FileUtils.rm_f(gems)
    else
      FileUtils.rm_rf("gems")
    end
    unless $?.success?
      puts $colorize.fail(" failed")
      return
    end
    puts $colorize.pass(" done")
  end

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

  tarball = nil
  return $packages.collect do |mesg|
    (ext, *cmd) = PACKAGES[mesg]
    File.directory?(destdir) or FileUtils.mkpath(destdir)
    file = File.join(destdir, "#{$archname||v}#{ext}")
    case ext
    when /\.tar/
      if tarball
        next if tarball.empty?
      else
        tarball = "#{$archname||v}.tar"
        print "creating tarball... #{tarball}"
        if tar_create(tarball, v)
          puts $colorize.pass(" done")
        else
          puts $colorize.fail(" failed")
          tarball = ""
          next
        end
      end
      print "creating #{mesg} tarball... #{file}"
      done = system(*cmd, tarball, out: file)
    else
      print "creating #{mesg} archive... #{file}"
      if Hash === cmd.last
        *cmd, opt = *cmd
        cmd << file << v << opt
      else
        (cmd = cmd.dup) << file << v
      end
      done = system(*cmd)
    end
    if done
      puts $colorize.pass(" done")
      file
    else
      puts $colorize.fail(" failed")
      nil
    end
  end.compact
ensure
  FileUtils.rm_rf(v) if v and !$exported and !$keep_temp
end

if [$srcdir, ($svn||=nil), ($git||=nil)].compact.size > 1
  abort "#{File.basename $0}: -srcdir, -svn, and -git are exclusive"
end
if $srcdir
  vcs = VCS.detect($srcdir)
elsif $svn
  vcs = VCS::SVN.new($svn == true ? SVNURL : URI.parse($svn))
elsif $git
  vcs = VCS::GIT.new($git == true ? GITURL : URI.parse($git))
else
  vcs = VCS::SVN.new(SVNURL)
end

success = true
revisions.collect {|rev| package(vcs, rev, destdir, tmp)}.flatten.each do |name|
  if !name
    success = false
    next
  end
  str = open(name, "rb") {|f| f.read}
  if $s3
    %x[aws s3 cp #{name} s3://ftp.r-l.o/pub/#{$s3}/#{Pathname(name).basename}]
    FileUtils.rm_f name
    name = "https://s3.amazonaws.com/ftp.r-l.o/pub/#{$s3}/#{Pathname(name).basename}"
  end
  puts "* #{$colorize.pass(name)}"
  puts "      SIZE:   #{str.bytesize} bytes"
  $digests.each do |alg|
    printf "      %-8s%s\n", "#{alg}:", Digest.const_get(alg).hexdigest(str)
  end
end

exit false if !success

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