(linenum→info "unix/slp.c:2238")

ruby/1.9.0/instruby.rb

    1: #!./miniruby
    2: 
    3: load "./rbconfig.rb"
    4: include RbConfig
    5: 
    6: srcdir = File.dirname(__FILE__)
    7: $:.unshift File.expand_path("lib", srcdir)
    8: require 'fileutils'
    9: require 'shellwords'
   10: require 'optparse'
   11: require 'optparse/shellwords'
   12: require 'tempfile'
   13: 
   14: STDOUT.sync = true
   15: File.umask(0)
   16: 
   17: def parse_args()
   18:   $mantype = 'doc'
   19:   $destdir = nil
   20:   $extout = nil
   21:   $make = 'make'
   22:   $mflags = []
   23:   $install = []
   24:   $installed_list = nil
   25:   $dryrun = false
   26:   $rdocdir = nil
   27:   opt = OptionParser.new
   28:   opt.on('-n') {$dryrun = true}
   29:   opt.on('--dest-dir=DIR') {|dir| $destdir = dir}
   30:   opt.on('--extout=DIR') {|dir| $extout = (dir unless dir.empty?)}
   31:   opt.on('--make=COMMAND') {|make| $make = make}
   32:   opt.on('--mantype=MAN') {|man| $mantype = man}
   33:   opt.on('--make-flags=FLAGS', '--mflags', Shellwords) do |v|
   34:     if arg = v.first
   35:       arg.insert(0, '-') if /\A[^-][^=]*\Z/ =~ arg
   36:     end
   37:     $mflags.concat(v)
   38:   end
   39:   opt.on('-i', '--install=TYPE',
   40:          [:local, :bin, :lib, :man, :ext, :"ext-arch", :"ext-comm", :rdoc]) do |ins|
   41:     $install << ins
   42:   end
   43:   opt.on('--installed-list [FILENAME]') {|name| $installed_list = name}
   44:   opt.on('--rdoc-output [DIR]') {|dir| $rdocdir = dir}
   45: 
   46:   opt.parse! rescue abort [$!.message, opt].join("\n")
   47: 
   48:   $make, *rest = Shellwords.shellwords($make)
   49:   $mflags.unshift(*rest) unless rest.empty?
   50: 
   51:   def $mflags.set?(flag)
   52:     grep(/\A-(?!-).*#{'%s' % flag}/i) { return true }
   53:     false
   54:   end
   55:   def $mflags.defined?(var)
   56:     grep(/\A#{var}=(.*)/) {return $1}
   57:     false
   58:   end
   59: 
   60:   if $mflags.set?(?n)
   61:     $dryrun = true
   62:   else
   63:     $mflags << '-n' if $dryrun
   64:   end
   65: 
   66:   $destdir ||= $mflags.defined?("DESTDIR")
   67:   if $extout ||= $mflags.defined?("EXTOUT")
   68:     Config.expand($extout)
   69:   end
   70: 
   71:   $continue = $mflags.set?(?k)
   72: 
   73:   if $installed_list ||= $mflags.defined?('INSTALLED_LIST')
   74:     Config.expand($installed_list, Config::CONFIG)
   75:     $installed_list = open($installed_list, "ab")
   76:     $installed_list.sync = true
   77:   end
   78: 
   79:   $rdocdir ||= $mflags.defined?('RDOCOUT')
   80: end
   81: 
   82: parse_args()
   83: 
   84: include FileUtils
   85: include FileUtils::NoWrite if $dryrun
   86: @fileutils_output = STDOUT
   87: @fileutils_label = ''
   88: 
   89: $install_procs = Hash.new {[]}
   90: def install?(*types, &block)
   91:   $install_procs[:all] <<= block
   92:   types.each do |type|
   93:     $install_procs[type] <<= block
   94:   end
   95: end
   96: 
   97: def install(src, dest, options = {})
   98:   options[:preserve] = true
   99:   super(src, with_destdir(dest), options)
  100:   if $installed_list
  101:     dest = File.join(dest, File.basename(src)) if $made_dirs[dest]
  102:     $installed_list.puts dest
  103:   end
  104: end
  105: 
  106: def ln_sf(src, dest)
  107:   super(src, with_destdir(dest))
  108:   $installed_list.puts dest if $installed_list
  109: end
  110: 
  111: $made_dirs = {}
  112: def makedirs(dirs)
  113:   dirs = fu_list(dirs)
  114:   dirs.collect! do |dir|
  115:     realdir = with_destdir(dir)
  116:     realdir unless $made_dirs.fetch(dir) do
  117:       $made_dirs[dir] = true
  118:       $installed_list.puts(File.join(dir, "")) if $installed_list
  119:       File.directory?(realdir)
  120:     end
  121:   end.compact!
  122:   super(dirs, :mode => 0755) unless dirs.empty?
  123: end
  124: 
  125: def install_recursive(srcdir, dest, options = {})
  126:   noinst = options[:no_install]
  127:   glob = options[:glob] || "*"
  128:   subpath = srcdir.size..-1
  129:   Dir.glob("#{srcdir}/**/#{glob}") do |src|
  130:     case base = File.basename(src)
  131:     when /\A\#.*\#\z/, /~\z/
  132:       next
  133:     end
  134:     if noinst
  135:       if Array === noinst
  136:         next if noinst.any? {|n| File.fnmatch?(n, base)}
  137:       else
  138:         next if File.fnmatch?(noinst, base)
  139:       end
  140:     end
  141:     d = dest + src[subpath]
  142:     if File.directory?(src)
  143:       makedirs(d)
  144:     else
  145:       makedirs(File.dirname(d))
  146:       install src, d
  147:     end
  148:   end
  149: end
  150: 
  151: def open_for_install(path, mode, &block)
  152:   unless $dryrun
  153:     open(with_destdir(path), mode, &block)
  154:   end
  155:   $installed_list.puts path if /^w/ =~ mode and $installed_list
  156: end
  157: 
  158: def with_destdir(dir)
  159:   return dir if !$destdir or $destdir.empty?
  160:   dir = dir.sub(/\A\w:/, '') if File::PATH_SEPARATOR == ';'
  161:   $destdir + dir
  162: end
  163: 
  164: exeext = CONFIG["EXEEXT"]
  165: 
  166: ruby_install_name = CONFIG["ruby_install_name"]
  167: rubyw_install_name = CONFIG["rubyw_install_name"]
  168: 
  169: version = CONFIG["ruby_version"]
  170: bindir = CONFIG["bindir"]
  171: libdir = CONFIG["libdir"]
  172: archhdrdir = rubyhdrdir = CONFIG["rubyhdrdir"]
  173: archhdrdir += "/" + CONFIG["arch"]
  174: rubylibdir = CONFIG["rubylibdir"]
  175: archlibdir = CONFIG["archdir"]
  176: sitelibdir = CONFIG["sitelibdir"]
  177: sitearchlibdir = CONFIG["sitearchdir"]
  178: vendorlibdir = CONFIG["vendorlibdir"]
  179: vendorarchlibdir = CONFIG["vendorarchdir"]
  180: mandir = File.join(CONFIG["mandir"], "man")
  181: configure_args = Shellwords.shellwords(CONFIG["configure_args"])
  182: enable_shared = CONFIG["ENABLE_SHARED"] == 'yes'
  183: dll = CONFIG["LIBRUBY_SO"]
  184: lib = CONFIG["LIBRUBY"]
  185: arc = CONFIG["LIBRUBY_A"]
  186: 
  187: install?(:local, :arch, :bin) do
  188:   puts "installing binary commands"
  189: 
  190:   makedirs [bindir, libdir, archlibdir]
  191: 
  192:   install ruby_install_name+exeext, bindir, :mode => 0755
  193:   if rubyw_install_name and !rubyw_install_name.empty?
  194:     install rubyw_install_name+exeext, bindir, :mode => 0755
  195:   end
  196:   if enable_shared and dll != lib
  197:     install dll, bindir, :mode => 0755
  198:   end
  199:   install lib, libdir, :mode => 0755 unless lib == arc
  200:   install arc, libdir, :mode => 0644
  201:   install "rbconfig.rb", archlibdir, :mode => 0644
  202:   if CONFIG["ARCHFILE"]
  203:     for file in CONFIG["ARCHFILE"].split
  204:       install file, archlibdir, :mode => 0644
  205:     end
  206:   end
  207: 
  208:   if dll == lib and dll != arc
  209:     for link in CONFIG["LIBRUBY_ALIASES"].split
  210:       ln_sf(dll, File.join(libdir, link))
  211:     end
  212:   end
  213: end
  214: 
  215: if $extout
  216:   extout = "#$extout"
  217:   install?(:ext, :arch, :'ext-arch') do
  218:     puts "installing extension objects"
  219:     makedirs [archlibdir, sitearchlibdir, vendorarchlibdir, archhdrdir]
  220:     if noinst = CONFIG["no_install_files"] and noinst.empty?
  221:       noinst = nil
  222:     end
  223:     install_recursive("#{extout}/#{CONFIG['arch']}", archlibdir, :no_install => noinst)
  224:     install_recursive("#{extout}/include/#{CONFIG['arch']}", archhdrdir, :glob => "*.h")
  225:   end
  226:   install?(:ext, :comm, :'ext-comm') do
  227:     puts "installing extension scripts"
  228:     hdrdir = rubyhdrdir + "/ruby"
  229:     makedirs [rubylibdir, sitelibdir, vendorlibdir, hdrdir]
  230:     install_recursive("#{extout}/common", rubylibdir)
  231:     install_recursive("#{extout}/include/ruby", hdrdir, :glob => "*.h")
  232:   end
  233: end
  234: 
  235: install?(:rdoc) do
  236:   if $rdocdir
  237:     puts "installing rdoc"
  238: 
  239:     ridatadir = File.join(CONFIG['datadir'], 'ri/$(MAJOR).$(MINOR).$(TEENY)/system')
  240:     Config.expand(ridatadir)
  241:     makedirs [ridatadir]
  242:     install_recursive($rdocdir, ridatadir)
  243:   end
  244: end
  245: 
  246: install?(:local, :comm, :bin) do
  247:   puts "installing command scripts"
  248: 
  249:   Dir.chdir srcdir
  250:   makedirs [bindir, rubylibdir]
  251: 
  252:   ruby_shebang = File.join(bindir, ruby_install_name)
  253:   if File::ALT_SEPARATOR
  254:     ruby_bin_dosish = ruby_shebang.tr(File::SEPARATOR, File::ALT_SEPARATOR)
  255:   end
  256:   for src in Dir["bin/*"]
  257:     next unless File.file?(src)
  258:     next if /\/[.#]|(\.(old|bak|orig|rej|diff|patch|core)|~|\/core)$/i =~ src
  259: 
  260:     name = ruby_install_name.sub(/ruby/, File.basename(src))
  261:     dest = File.join(bindir, name)
  262: 
  263:     install src, dest, :mode => 0755
  264: 
  265:     next if $dryrun
  266: 
  267:     shebang = ''
  268:     body = ''
  269:     open_for_install(dest, "r+") { |f|
  270:       shebang = f.gets
  271:       body = f.read
  272: 
  273:       if shebang.sub!(/^\#!.*?ruby\b/) {"#!" + ruby_shebang}
  274:         f.rewind
  275:         f.print shebang, body
  276:         f.truncate(f.pos)
  277:       end
  278:     }
  279: 
  280:     if ruby_bin_dosish
  281:       batfile = File.join(bindir, name + ".bat")
  282:       open_for_install(batfile, "wb") {|b|
  283:         b.print((<<EOH+shebang+body+<<EOF).gsub(/\r?\n/, "\r\n"))
  284: @echo off
  285: @if not "%~d0" == "~d0" goto WinNT
  286: #{ruby_bin_dosish} -x "#{batfile}" %1 %2 %3 %4 %5 %6 %7 %8 %9
  287: @goto endofruby
  288: :WinNT
  289: "%~dp0#{ruby_install_name}" -x "%~f0" %*
  290: @goto endofruby
  291: EOH
  292: __END__
  293: :endofruby
  294: EOF
  295:       }
  296:     end
  297:   end
  298: end
  299: 
  300: install?(:local, :comm, :lib) do
  301:   puts "installing library scripts"
  302: 
  303:   Dir.chdir srcdir
  304:   makedirs [rubylibdir]
  305: 
  306:   for f in Dir["lib/**/*{.rb,help-message}"]
  307:     dir = File.dirname(f).sub!(/\Alib/, rubylibdir) || rubylibdir
  308:     makedirs dir
  309:     install f, dir, :mode => 0644
  310:   end
  311: end
  312: 
  313: install?(:local, :arch, :lib) do
  314:   puts "installing headers"
  315: 
  316:   Dir.chdir(srcdir)
  317:   makedirs [rubyhdrdir]
  318:   noinst = []
  319:   unless RUBY_PLATFORM =~ /mswin32|mingw|bccwin32/
  320:     noinst << "win32.h"
  321:   end
  322:   noinst = nil if noinst.empty?
  323:   install_recursive("include", rubyhdrdir, :no_install => noinst, :glob => "*.h")
  324: end
  325: 
  326: install?(:local, :comm, :man) do
  327:   puts "installing manpages"
  328: 
  329:   Dir.chdir(srcdir)
  330:   for mdoc in Dir["*.[1-9]"]
  331:     next unless File.file?(mdoc) and open(mdoc){|fh| fh.read(1) == '.'}
  332: 
  333:     destdir = mandir + mdoc[/(\d+)$/]
  334:     destfile = File.join(destdir, mdoc.sub(/ruby/, ruby_install_name))
  335: 
  336:     makedirs destdir
  337: 
  338:     if $mantype == "doc"
  339:       install mdoc, destfile, :mode => 0644
  340:     else
  341:       require 'mdoc2man.rb'
  342: 
  343:       w = Tempfile.open(mdoc)
  344: 
  345:       open(mdoc) { |r|
  346:         Mdoc2Man.mdoc2man(r, w)
  347:       }
  348: 
  349:       w.close
  350: 
  351:       install w.path, destfile, :mode => 0644
  352:     end
  353:   end
  354: end
  355: 
  356: $install.concat ARGV.collect {|n| n.intern}
  357: $install << :local << :ext if $install.empty?
  358: $install.each do |inst|
  359:   $install_procs[inst].each do |block|
  360:     dir = Dir.pwd
  361:     begin
  362:       block.call
  363:     ensure
  364:       Dir.chdir(dir)
  365:     end
  366:   end
  367: end
  368: 
  369: # vi:set sw=2:
Syntax (Markdown)