[Rake-devel] Heirarchical Builds

Brad Phelan bradphelan at xtargets.com
Fri Oct 5 06:22:39 EDT 2007

Hi Rakers,

Am new here to the list. Am joining because of a special requirement. I 
have been hacking the Rake source code without too much success to get 
the following requirements to work.

(Req 1) When a 'rakefile' is executed it should be processed under the 
condition that the current directory is set to the directory  that the 
rakefile is found in. This makes rakefiles agnostic as to where they are 
placed into  the project heirarchy and can refer to  local files using 
local paths.

    file "foo.o" => "foo.c" do |t|
       sh "#{gcc -c #{t.source} -o #{t}"

(Req 2) When executing build actions they should all execute from the 
project root directory. ie foo.c will
be compiled in a shell with the following command.

    gcc -c sub/foo.c -o sub/foo.o

As a first step I can create a simple wrapper called rake

def rake file
    Dir.chdir File.dirname(file) do
       load File.basename(file)

to change the current directory when executing sub rakes. This doesn't 
work because now the tasks/prerequisites are added to the build database 
without the full path to define them. Ie foo.o and foo.c are stored 
without the "sub" prefix. I can't for the life of me work out the exact 
place to patch this feature in to see if I can get it to work. One 
possible idea I have had is that we can extend the namespace concept to 
file based tasks. If the directory the task was defined in is 
automatically mapped to a namespace then perhaps the problem gets 
solved, though I haven't had a look at what it would require to do this.

I'm also working on trying to add construction environments to Rake ala 
SCons  so you can do something like

Env.open do |env|
    env[:CCFLAGS] = "-DFoo"
    env.file 'main.out' => [ 'main.o' ]

Env.open do |env|
    env[:CCFLAGS] = "-DBar"
    env.file 'main2.out' => [ 'main2.o' ]

task :default => ['main.out', 'main2.out']
rule( '.o' => '.c' ) do |t|
    path = t[:CCPATH].collect { |p| " -I#{p}" }
    sh "cc #{t[:CCFLAGS]} #{path} #{t.source} -c -o #{t.name}"

rule '.out' => '.o' do |t|
    sh "cc -o #{t[:CCFLAGS]} #{t.name} #{t.prerequisites.join(' ')}"

And each file will be built with different flags.

However I want to solve the hierarchical build problem first. Any 
pointers or suggestions would be appreciated. I would like to be able to 
convert over from using SCons in large hierarchical C/C++ projects but 
currently Rake lacks the above features.


Brad Phelan

More information about the Rake-devel mailing list