Equivalent Makefile Behavior in Rakefile

So, I am studying rubyat the moment and discovered rake. I like to learn new tools, realizing what I already know with them, so I'm trying to convert Makefilewhat I have to rake.

Let's say it looks like this:

main: build/*.o
    clang -c $^ -o $@

build/%.o: src/%.c | build
    clang -c $< -o $@

build:
    mkdir build

What is especially important in this Makefile:

  • Match patterns with %
  • Order only dependency with | build

Is there a way to implement this logic with rakeor do I need to use ruby? For instance.

task :default => "main"

file "main" => "build/%.o" do
  sh "clang -o 'main' ??"
end

file 'build/%.o' => "src/%.c" do # order only dependency on `build`
  sh "clang -c ?? ??"
end
+4
source share
1 answer

This is that rake is not bad, and it is tragically underused:

task :default => "main"

# This assumes that your "main" is created by linking 
# all *.o files, each of which is the product of compiling a *.c file

# FileList[] creates a list of all *.c source files. The pathmap then
# changes the directory from /src/ to /out/ and the extension to .o

file "main" => FileList["src/**/*.c"].pathmap("%{^src,out}d/%n.o") do |t| 
   sh "ld #{t.sources.join(" ")} #{t.name}"
end

# This is the rule that says: if you need a 
# file out/bla.o, this will create it from /src/bla.c.    
rule /out\/.+.o/ => ->(target) { target.pathmap("%{^out,src}d/%n.c") } do |t|
  sh "cp #{t.source} #{t.name}"
end

Some notes:

  • . , glob ,
  • , , (, Make)
  • pathmap - Rake . , , .
+1

Source: https://habr.com/ru/post/1648091/


All Articles