Index: lib/mkmf.rb
===================================================================
RCS file: /cvs/ruby/src/ruby/lib/mkmf.rb,v
retrieving revision 1.196
diff -U2 -p -d -r1.196 mkmf.rb
--- lib/mkmf.rb	17 Sep 2004 09:24:11 -0000	1.196
+++ lib/mkmf.rb	23 Sep 2004 09:55:26 -0000
@@ -883,4 +883,5 @@ target_prefix = #{target_prefix}
 LOCAL_LIBS = #{$LOCAL_LIBS}
 LIBS = #{$LIBRUBYARG} #{$libs} #{$LIBS}
+SRCS = #{srcs.collect(&File.method(:basename)).join(' ')}
 OBJS = #{$objs}
 TARGET = #{target}
@@ -915,5 +916,10 @@ all:		#{target ? $extout ? "install" : "
 static:		$(STATIC_LIB)
 
+PHONY:
 }
+  mfile.print %{
+.depend:        $(SRCS)
+		$(RUBY) -run -e mkdep -- srcdir='$(srcdir)' topdir='$(topdir)' hdrdir='$(hdrdir)' CFLAGS='$(CFLAGS)' CPPFLAGS='$(CPPFLAGS)' -- $?
+} unless $nmake == ?b
   mfile.print CLEANINGS
   dirs = []
@@ -997,5 +1003,7 @@ site-install-rb: install-rb
       while line = dfile.gets()
 	line.gsub!(/\.o\b/, ".#{$OBJEXT}")
-	line.gsub!(/(\s)([^\s\/]+\.[ch])/, '\1{$(srcdir)}\2') if $nmake
+        if $nmake and /^[^\t]/ =~ line
+          line.gsub!(/(\s)([^\s\/]+\.[ch])(?=\s|\z)/, '\1{.;$(VPATH)}\2')
+        end
 	line.gsub!(/\$\(hdrdir\)\/config.h/, $config_h) if $config_h
 	mfile.print line
Index: lib/un.rb
===================================================================
RCS file: /cvs/ruby/src/ruby/lib/un.rb,v
retrieving revision 1.5
diff -U2 -p -d -r1.5 un.rb
--- lib/un.rb	14 Feb 2004 14:27:43 -0000	1.5
+++ lib/un.rb	15 May 2004 04:27:30 -0000
@@ -31,16 +31,4 @@ end
 
 def setup(options = "")
-  ARGV.map! do |x|
-    case x
-    when /^-/
-      x.delete "^-#{options}v"
-    when /[*?\[{]/
-      Dir[x]
-    else
-      x
-    end
-  end
-  ARGV.flatten!
-  ARGV.delete_if{|x| x == "-"}
   opt_hash = {}
   OptionParser.new do |o|
@@ -53,4 +41,12 @@ def setup(options = "")
     o.parse!
   end
+  ARGV.map! do |x|
+    case x
+    when /[*?\[{]/
+      Dir[x]
+    else
+      x
+    end
+  end.flatten!
   yield ARGV, opt_hash
 end
@@ -211,4 +207,52 @@ def touch
   setup do |argv, options|
     FileUtils.touch argv, options
+  end
+end
+
+##
+# Make dependency file
+#
+#   ruby -run -e mkdep -- [var=value]... -- [INPUT]
+
+def mkdep
+  return if /mingw/ =~ RUBY_PLATFORM
+  setup do |argv,|
+    require 'mkmf'
+    require 'shellwords'
+    pat = []
+    options = []
+    while /\A(\w+)=(.*)/ =~ ARGV[0]
+      ARGV.shift
+      var, val = $1, $2
+      if /FLAGS$/ =~ var
+        options.concat(Shellwords.shellwords(val))
+      else
+        pat << [Regexp.new('(\A|\s)'+Regexp.quote(val)+"/"), "$(#{var})/"]
+      end
+    end
+    objext = Regexp.quote(CONFIG['OBJEXT'])
+    pat << [/\$\(srcdir\)\/(\S+\.#{objext})(?=$|\s*:)/, '']
+    pat << [/\$\(srcdir\)\//, '']
+    ARGV.shift if ARGV[0] == '--'
+    if /gcc/ =~ Config::CONFIG["CC"]
+      cmd = [Config::CONFIG["CC"], "-MM", ]
+      edit = true
+    elsif cmd = find_executable0("makedepend")
+      cmd = [cmd, "-f-", "--"]
+      edit = true
+    elsif cmd = find_executable0("mkdep")
+      cmd = [cmd, "-f-"]
+    else
+      return
+    end
+    cmd.concat(options).concat(ARGV)
+    if edit
+      str = `#{cmd.join(' ')}`
+      abort unless $?.success?
+      str = pat.inject(str) {|str, (re, s)| str.gsub(re) {"#{$1}#{s}"}}
+      open(".depend", "w") {|dep| dep.print str}
+    else
+      system(*cmd)
+    end
   end
 end
