From: Stefan Lang Date: 2005-04-28T09:30:08+09:00 Subject: Re: Rake help required On Thursday 28 April 2005 01:38, Mark Probert wrote: > Hi .. > > I am not sure how to create a set of Rake rules to do the following. Can > anyone prove assistance? > > Structure: > > Rakefile > foo > src_a/ > a_1.c > a_2.c > src_b/ > b_1.c > b_2.c > obj/ > a_1.o > a_2.o > b_1.o > b_2.o > libfoo.a ("ar cr libfoo.a obj/*.c; ranlib libfoo.a") > > I would like to have a single target > > $ rake build > > That would create the .o and the lib, then link them into the executable. > I can do this if everything is in the root directory but I am finding > defining the objects trickier. > > I'd rather avoid explicitly putting the directory name in the source list, > like > > SRC_A = ["src_a/a_1.c" ...] > > Regards, An Rantfile that does this job could look like: desc "Build foo." file :foo => "obj/libfoo.a" do |t| # adjust... sys "cc -o #{t.name} #{t.source}" end o_files = sys["**/*.c"].map { |f| "obj/" + File.basename(f).sub_ext("o") } file "obj/libfoo.a" => o_files do |t| sys "ar cr #{t.name} #{t.prerequisites.arglist}" sys "ranlib #{t.name}" end o_to_c = lambda { |t| t =~ /^obj\/([^_])_/ "src_#{$1}/#{File.basename(t)}".sub_ext "c" } gen Rule, :o => o_to_c do |t| sys "cc -c -o #{t.name} #{t.source}" end When invoking rant it will build foo, as its the first task: % rant Stefan