buildrakerakefile

How to execute Rake file tasks in parallel


I'm using Rake to build a C language build system. When compiling multiple files, I want to compile multiple files in parallel, using multiple cores of the CPU.

Can you give me some advice on how to write a Rake file? I would like to achieve the make -j in Rake.

As a restriction, I prefer not to install a new Gem.

For clarity, here is a simplified Rake file.

CC = "gcc"

task :default => "hello"

file "hello" => ["hello.o", "message.o"] do
  sh "#{CC} -o hello hello.o message.o"
end

file "hello.o" => "hello.c" do (1)
  sh "#{CC} -c hello.c"
end

file "message.o" => "message.c" do (2)
  sh "#{CC} -c message.c"
end

For tasks, I can use multitask. However, for file tasks, I don't know how to describe it. I would like to run the file tasks (1) and (2) concurrently.

my environment: ruby 2.6.4p104 (2019-08-28 revision 67798) [i386-cygwin]

I thank you in advance.


Solution

  • You can create threads and you can run your files in new thread.

    For example

    CC = "gcc"
    
    task :default => "hello"
    
    file "hello" => ["hello.o", "message.o"] do
      Thread.new do
        sh "#{CC} -o hello hello.o message.o"
      end
    end
    
    file "hello.o" => "hello.c" do
      Thread.new do
        sh "#{CC} -c hello.c"
      end
    end
    
    file "message.o" => "message.c" do
      Thread.new do
        sh "#{CC} -c message.c"
      end
    end