1

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.

2 Answers 2

0

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
2
  • Thank you for your answer. I had missed the basics. I will try it now.
    – Akinori
    Feb 13, 2021 at 2:57
  • After executing "hello.o" and "message.o", "hello" must be executed. It will result in a link error. Can I use join or other methods to synchronize them? If I have a large rakefile, synchronization can be complicated. Is there any good way to do this?
    – Akinori
    Feb 13, 2021 at 8:25
0

I found out that rake -m considers all tasks as multitasking. I defined individual tasks only for the tasks that I do not want to be executed in parallel. By specifying something like "rake -m -j 8", I confirmed that the build time was reduced.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.