2016-07-16 71 views
4

所以我现在在学习ruby并发现rake。我喜欢通过实施我已经知道的事情来学习新工具,所以我尝试将Makefile转换为rake耙文件中的Makefile等效行为

比方说,它看起来像这样:

main: build/*.o 
    clang -c $^ -o [email protected] 

build/%.o: src/%.c | build 
    clang -c $< -o [email protected] 

build: 
    mkdir build 

什么特殊关于这个Makefile是:

  1. 模式与%
  2. 订单仅依赖匹配与| build

是否有某种方法可以使用rake来实现此逻辑,还是必须使用ruby本身?例如。

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 

回答

1

这是耙是相当不错的东西,那是可悲的使用不足:

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 

一些注意事项:

  • 规则名称可以是正则表达式。我相信全局样式也是可能的,但是找到更容易总是使用正则表达式
  • 如果一个规则的目标已经存在,并且比其所有源更新,它将不会再次执行(如Make)
  • 路径图和文件任务是文件名的Rake扩展。对于其他实体(如数据库条目)没有任何相似之处,但您通常可以自己创建它们