[Rake-devel] Generating File Tasks

Jim Weirich jim at weirichhouse.org
Sat Dec 2 19:32:50 EST 2006

Jonathan Stott wrote:
> Hi
> I'm quite new to both make (and very recently) to rake, but I appear
> to have a problem that is beyond make and I cannot figure out how to
> do it in rake either.
> The situation is I have a series of drivers and a series of models,
> stored in the directories 'driver' and 'model', respectively. The
> driver code takes a model and performs some calculations with it and
> any model is valid for any driver.
> What I would like to be able to do is have some generic code whereby
> if I add a driver or model, I can type "rake driver_model" and compile
> the code.
> I have the code snippet:
> file "driver_model" => calc_dep("driver_model") do |t|
>         sh "#{CC} #{LDFLAGS} -o #{t.name} #{t.prerequisites.join(" ")} #{LIB}"
> end
> def calc_dep(file)
>         dep = file.split("_")
>         dep.collect! { |d| d + ".o" }
>         return dep
> end
> which will take driver_model, work out it depends on driver.o and
> model.o and proceed to build the code, which is just what I want.
> However, it does require me to have defined driver_model combinations
> for each executable I wish to create and since I have at last count 5
> drivers and 7 models, I would need to make 35 rules to cover each
> combination and more rules whenever I added another driver or model.
> So I would like to synthesise the possible driver/model combinations
> from the available source files at the time of running rake.
> I think I should be able to do this via :
> Task.new(task_name,app)
> and
> the .enhance(deps) method.
> within a nested look of the available models and drivers. However, I'm
> not sure how to find the correct application name, since
> DRIVERS.each do |d|
>         MODELS.each do |m|
>                 Task.new("#{d}_#{m}",Rake.application())
>         end
> end
> puts Task.tasks()

I haven't had a chance to look at this in detail, but I always use the 
standard task definition methods for my dynamically generated tasks.

Try this:

DRIVERS.each do |d|
   MODELS.each do |m|
     task "#{d}_#{m}"

-- Jim Weirich

More information about the Rake-devel mailing list