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

Discussion in 'Ruby' started by Brock Rycenga, Oct 22, 2008.

  1. 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
    --
    Posted via http://www.ruby-forum.com/.
     
    Brock Rycenga, Oct 22, 2008
    #1
    1. Advertising

  2. 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


    --
    Posted via http://www.ruby-forum.com/.
     
    Brock Rycenga, Oct 22, 2008
    #2
    1. Advertising

  3. 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


    --
    Posted via http://www.ruby-forum.com/.
     
    Brock Rycenga, Oct 22, 2008
    #3
  4. On Wed, Oct 22, 2008 at 11:55 AM, Brock Rycenga
    <> wrote:
    >
    > 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?
    >

    OPT1 = ENV['OPT1'] || 'default value'

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

    HTH,
    Michael Guterl
     
    Michael Guterl, Oct 22, 2008
    #4
  5. Brock Rycenga

    Hugh Sasse Guest

    On Thu, 23 Oct 2008, 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?


    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
     
    Hugh Sasse, Oct 22, 2008
    #5
  6. Michael Guterl wrote:
    > On Wed, Oct 22, 2008 at 11:55 AM, Brock Rycenga
    > <> 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 Guterl



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


    Brock

    --
    Posted via http://www.ruby-forum.com/.
     
    Brock Rycenga, Oct 22, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    1,367
  2. Martin Honermeyer

    Rake: multiple prerequisites in rule?

    Martin Honermeyer, Sep 2, 2005, in forum: Ruby
    Replies:
    6
    Views:
    171
    Martin Honermeyer
    Sep 6, 2005
  3. Joel VanderWerf
    Replies:
    1
    Views:
    222
    Joel VanderWerf
    Nov 10, 2006
  4. peppermonkey
    Replies:
    1
    Views:
    244
    Gregory Brown
    Feb 10, 2007
  5. Jim flip

    Rake Rule name problem

    Jim flip, Oct 20, 2008, in forum: Ruby
    Replies:
    7
    Views:
    106
    Jim Boooo
    Oct 23, 2008
Loading...

Share This Page