# Rakefile help: rakefile calling a rakefile?

**URL:** <https://www.ruby-forum.com/t/rakefile-help-rakefile-calling-a-rakefile/70544>\
**Category:** Ruby\
**Created:** [September 29, 2006, 11:17pm UTC](https://www.ruby-forum.com/t/rakefile-help-rakefile-calling-a-rakefile/70544 "2006-09-29T23:17:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [September 29, 2006, 11:17pm UTC](https://www.ruby-forum.com/t/rakefile-help-rakefile-calling-a-rakefile/70544/1 "2006-09-29T23:17:45Z")

</div>

I’ve got a top-level Rakefile that builds some C files. So, I’ve got a  
rule in there for building .c files to .o files. This rakefile has its  
own global variables, like $CFLAGS for example.

rule ‘.o’ =\> ‘.c’ do |t|  
cmd = “cc #{$CFLAGS} #{$INCLUDES} -c -o #{t.name} #{t.source}”  
sh cmd  
end

I’ve also got a subdirectory with a C file in it that needs slightly  
different value of $CFLAGS to build. I’ve got a rule in that Rakefile  
as well, and it looks exactly like the one in the top-level rakefile.

The C file in the subdir needs to be built before the top-level  
Rakefile can do its jobs of building other things.

My guess is that just using:

```
require 'subdir/Rakefile'

```

would botch things up, since I’m using a global $CFLAGS.

I’d think I’d like to just do something this in the top-level Rakefile:

task :default =\> [:build\_other\_c\_file] do |t|  
sh “cd subdir; rake”  
end

Is that the way multi-rakefile projects usually do it?

Although it’s “considered harmful” for large projects, I don’t mind  
doing something like how make recursivly decends into subdirectories  
to make dependencies. How’s that usually done with rake?

I read one of Jim’s posts relating to this  
[http://rubyforge.org/pipermail/rake-devel/2005-December/000182.html](http://rubyforge.org/pipermail/rake-devel/2005-December/000182.html)  
but don’t know exactly how I’d go about building a “unified dependency  
graph” as he recommends.

Thanks,  
—John

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [December 5, 2006, 9:04pm UTC](https://www.ruby-forum.com/t/rakefile-help-rakefile-calling-a-rakefile/70544/2 "2006-12-05T21:04:43Z")

</div>

John G. wrote:

> I’d think I’d like to just do something this in the top-level Rakefile:
> 
> task :default =\> [:build\_other\_c\_file] do |t|  
> sh “cd subdir; rake”  
> end
> 
> Is that the way multi-rakefile projects usually do it?

That’s how I do it. May not be the best solution, but hey, it works.

> I read one of Jim’s posts relating to this  
> [http://rubyforge.org/pipermail/rake-devel/2005-December/000182.html](http://rubyforge.org/pipermail/rake-devel/2005-December/000182.html)  
> but don’t know exactly how I’d go about building a “unified dependency  
> graph” as he recommends.

I suppose you could load (instead of require) the nested Rakefile into a  
namespace and then use it as a dependency. For example:

namespace :nested  
load ‘subdir/Rakefile’  
end

task :default =\> :nested:default
