Hi,

At Sat, 15 Mar 2003 12:56:35 +0900,
Jim Weirich wrote:
> Here is a file based dependency that will compile "hello.cc" to
> "hello.o".
> 
>    file "hello.cc"

This line looks verbose for me.

>    require 'rake/ctools'
>    Dir['*.c'].each do |fn|
>      c_source_file(fn)
>    end

Why not pattern rule?


require 'rbconfig.rb'
include Config

CC = CONFIG['CC']
COMPILE_C = "#{CC} #{CONFIG['COMPILE_C_FLAG']}"
OBJEXT = CONFIG['OBJEXT']

srcs = Dir["*.c"]
objs = srcs.map{|f|f.sub(/\.c$/, ".#{OBJEXT}")}
target = "foo"

rule /\.#{OBJEXT}$/, '.c' do |f|
  src = f.prerequisites.first
  sys("#{COMPILE_C} #{src}")
end

file target => objs do |f|
  sys("#{CC} -o #{f.name} #{f.prerequisites.join(' ')}")
end

task :default => target


--- bin/rake.rb-0.1.0 Sat Mar 15 11:01:00 2003 +++ bin/rake.rb Mon Mar 17 05:31:31 2003 @@ -4,6 +4,7 @@ class Task TASKS = Hash.new + RULES = Array.new attr_reader :prerequisites @@ -42,7 +43,15 @@ class << self def [](task_name) - TASKS[intern(task_name)] or fail "Don't know how to rake #{task_name}" + task = TASKS[intern(task_name)] and return task + task_name = task_name.to_s + RULES.each do |pat, deps, block| + pat.match(task_name) or next + deps = deps.collect {|dep| task_name.sub(pat, dep)} + return FileTask.new(task_name).enhance(deps, &block) + end + task = File.exist?(task_name) and return FileTask.new(task_name) + raise "Don't know how to rake #{task_name}" end def define_task(args, &block) @@ -66,7 +75,12 @@ end def intern(task_name) - (Symbol === task_name) ? task_name : task_name.intern + task_name.to_s + end + + def define_rule(pat, prereq) + String === pat and pat = /#{Regexp.quote(pat)}$/ + RULES << [pat, prereq, Proc.new] end end end @@ -92,12 +106,16 @@ FileTask.define_task(args, &block) end +def rule(pat, args, &block) + Task.define_rule(pat, args, &block) +end + def sys(cmd) puts cmd system(cmd) or fail "Command Failed: [#{cmd}]" end -def rake +def rake(*targets) begin here = Dir.pwd while ! File.exist?("Rakefile") @@ -107,8 +125,8 @@ end puts "(in #{Dir.pwd})" load "./Rakefile" - ARGV.push("default") if ARGV.size == 0 - ARGV.each { |task_name| Task[task_name].invoke } + targets = ["default"] if targets.empty? + targets.each { |task_name| Task[task_name].invoke } rescue Exception => ex puts "rake aborted!" puts ex.message @@ -117,5 +135,5 @@ end if __FILE__ == $0 then - rake + rake(*ARGV) end