Monday, June 14, 2010

Quick & Painless C/C++ header installations using rake

In wrapping up and refactoring my new rake driven build system, I've written numerous utility functions. Most stuff specific to compiling crap is in a custom "Builder" module that provides a templated interface to the current OS/Compiler kit, via environment variables and methods like make_object, make_shared_library. My top level rakefile also has a neato inference rule rigged together for handling my triple-tree and quad tree build patterns.

The real fun of the day however, is this baby:


#
# A magic function that generates file tasks that copy all headers from
# directory ind # to directory outd.  Any missing directories are created as
# needed.
#
# The return value is a list suitable for the right hand side of a task, e.g.
#
#   task :headers => header_magic(src, dest)
#
def header_magic(ind, outd)
  dirs = []
  hdrs = []

  find_files(ind, /.*\.h[hxp]*$/) do |p|
    outdir = File.dirname p.sub(ind, outd)
    outfile = File.join(outd, File.basename(p))

    directory outdir
    file outfile => p do
      cp_r p, outdir
    end
    dirs.push outdir
    hdrs.push outfile
    CLEAN.include outfile, outdir
  end

  dirs+hdrs
end

find_files is another custom function, it simply takes a block and yields and file names matching the specified pattern. (In this case, more liberal than the usual header extensions but sufficient for my needs)


I call it header_magic, because it after all the other stuff I played with, it's the closest to Clarke's third law. Which is exactly what I want!



footnote: I just have my top level clean task nuke the entire build tree; so the CLEAN part hasn't been tested.

No comments:

Post a Comment