# Rake: How do I Override Var's Used by a Rule?

**URL:** <https://www.ruby-forum.com/t/rake-how-do-i-override-vars-used-by-a-rule/151543>\
**Category:** Ruby\
**Created:** [October 22, 2008, 5:42pm UTC](https://www.ruby-forum.com/t/rake-how-do-i-override-vars-used-by-a-rule/151543 "2008-10-22T17:42:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rake\_newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2bfe46/32.png) [@rake\_newb](https://www.ruby-forum.com/u/rake_newb)\
**Post date:** [October 22, 2008, 5:42pm UTC](https://www.ruby-forum.com/t/rake-how-do-i-override-vars-used-by-a-rule/151543/1 "2008-10-22T17:42:32Z")

</div>

All:

For our rakefile system, we would like to have the ability to build  
either a “debug” version of our code, or a “release” version. My plan is  
to have 2 separate high-level tasks, where one of them invokes the other

- except with overridden variables. I wish I knew how to do this. 😉  
Please help.

Here are my tasks (edited for brevity):

=============================  
task :debug\_build =\> [:build\_depend, :build\_obj] do  
# handle the linking  
end

# task :release\_build do # Invoke the debug\_build task, but override the following var’s: # C\_OPTIMIZE\_LVL="-o2" # C\_DEBUG="" end

My .c -\> .obj rule looks like this:

# ============================= rule “.#{OBJ\_EXT}” =\> lambda{|objfile| find\_source(objfile)} do |t| sh “#{COMPILER} #{C\_OPTS} #{C\_DEBUG} #{C\_OPTIMIZE\_LVL} #{C\_OBJ\_DIR} #{C\_INC\_PATHS} #{C\_FLAGS} #{C\_TARGET} #{t.source}” end

So the only difference between the debug\_build and release\_build tasks  
is the compile line. NOTE: I currently read those variables (COMPILER,  
C\_OPTS, etc.) from another rakefile.

Any suggestions? Am I going about this the wrong way?

Thanks,  
Brock

---

<div class="post-metadata">

**Author:** ![rake\_newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2bfe46/32.png) [@rake\_newb](https://www.ruby-forum.com/u/rake_newb)\
**Post date:** [October 22, 2008, 5:52pm UTC](https://www.ruby-forum.com/t/rake-how-do-i-override-vars-used-by-a-rule/151543/2 "2008-10-22T17:52:45Z")

</div>

As usual, right after I finally decide to post something to a forum, I  
trip over an answer myself. I found that this seems to do the trick.

task :release\_build do  
C\_OPTIMIZE\_LVL="-o2"  
C\_DEBUG=""  
Rake::Task[:debug\_build].invoke  
end

Is this a good way to do this? Is there a better one? Should I just stop  
asking questions and move on?

Thanks,  
B

Brock Rycenga wrote:

> All:
> 
> For our rakefile system, we would like to have the ability to build  
> either a “debug” version of our code, or a “release” version. My plan is  
> to have 2 separate high-level tasks, where one of them invokes the other
> 
> - except with overridden variables. I wish I knew how to do this. 😉  
> Please help.
> 
> Here are my tasks (edited for brevity):
> 
> =============================  
> task :debug\_build =\> [:build\_depend, :build\_obj] do  
> # handle the linking  
> end
> 
> # task :release\_build do # Invoke the debug\_build task, but override the following var’s: # C\_OPTIMIZE\_LVL="-o2" # C\_DEBUG="" end
> 
> My .c -\> .obj rule looks like this:
> 
> # ============================= rule “.#{OBJ\_EXT}” =\> lambda{|objfile| find\_source(objfile)} do |t| sh “#{COMPILER} #{C\_OPTS} #{C\_DEBUG} #{C\_OPTIMIZE\_LVL} #{C\_OBJ\_DIR} #{C\_INC\_PATHS} #{C\_FLAGS} #{C\_TARGET} #{t.source}” end
> 
> So the only difference between the debug\_build and release\_build tasks  
> is the compile line. NOTE: I currently read those variables (COMPILER,  
> C\_OPTS, etc.) from another rakefile.
> 
> Any suggestions? Am I going about this the wrong way?
> 
> Thanks,  
> Brock

---

<div class="post-metadata">

**Author:** ![rake\_newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2bfe46/32.png) [@rake\_newb](https://www.ruby-forum.com/u/rake_newb)\
**Post date:** [October 22, 2008, 5:57pm UTC](https://www.ruby-forum.com/t/rake-how-do-i-override-vars-used-by-a-rule/151543/3 "2008-10-22T17:57:23Z")

</div>

This is really starting to feel like I am having a conversation with  
myself…

Even if this is the correct way to do this, it would be really nice to  
know how to override var’s from the command line. For example, with  
make, I could do the following:

make -f mymakefile OPT1=“new\_val” OPT2=“over\_new\_val” mytarget

This would override value for OPT1 and OPT2 in the makefile. Is there  
something equivalent in rake?

Thanks,  
B

Brock Rycenga wrote:

> As usual, right after I finally decide to post something to a forum, I  
> trip over an answer myself. I found that this seems to do the trick.
> 
> task :release\_build do  
> C\_OPTIMIZE\_LVL="-o2"  
> C\_DEBUG=""  
> Rake::Task[:debug\_build].invoke  
> end
> 
> Is this a good way to do this? Is there a better one? Should I just stop  
> asking questions and move on?
> 
> Thanks,  
> B

---

<div class="post-metadata">

**Author:** ![rake\_newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2bfe46/32.png) [@rake\_newb](https://www.ruby-forum.com/u/rake_newb)\
**Post date:** [October 22, 2008, 6:07pm UTC](https://www.ruby-forum.com/t/rake-how-do-i-override-vars-used-by-a-rule/151543/4 "2008-10-22T18:07:11Z")

</div>

On Wed, Oct 22, 2008 at 11:55 AM, Brock Rycenga  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This would override value for OPT1 and OPT2 in the makefile. Is there  
> something equivalent in rake?

OPT1 = ENV[‘OPT1’] || ‘default value’

On another note, top posting is discouraged on this list.

HTH,  
Michael G.

---

<div class="post-metadata">

**Author:** ![rake\_newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2bfe46/32.png) [@rake\_newb](https://www.ruby-forum.com/u/rake_newb)\
**Post date:** [October 22, 2008, 9:54pm UTC](https://www.ruby-forum.com/t/rake-how-do-i-override-vars-used-by-a-rule/151543/5 "2008-10-22T21:54:26Z")

</div>

Michael G. wrote:

> On Wed, Oct 22, 2008 at 11:55 AM, Brock Rycenga  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > This would override value for OPT1 and OPT2 in the makefile. Is there  
> > something equivalent in rake?
> 
> OPT1 = ENV[‘OPT1’] || ‘default value’
> 
> On another note, top posting is discouraged on this list.
> 
> HTH,  
> Michael G.

Thanks Michael. That does help, and that’s what I was looking for.

Brock

---

<div class="post-metadata">

**Author:** ![rake\_newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2bfe46/32.png) [@rake\_newb](https://www.ruby-forum.com/u/rake_newb)\
**Post date:** [October 22, 2008, 6:30pm UTC](https://www.ruby-forum.com/t/rake-how-do-i-override-vars-used-by-a-rule/151543/6 "2008-10-22T18:30:42Z")

</div>

On Thu, 23 Oct 2008, Brock Rycenga wrote:

> Is this a good way to do this? Is there a better one? Should I just stop  
> asking questions and move on?

I’d make them global variables, just to make them stand out, show they  
are shared between tasks, but I don’t use rake that much at the moment

As to the next question about command line options, then my reading of  
rake --help suggests you can’t set them from the command line, but you  
could set environment vars and read those inside the rakefile. Or read  
a config file with values in it.

> Thanks,  
> B

```
    Hugh
```
