加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – Rakefile中的Makefile等效行为

发布时间:2020-12-17 02:09:20 所属栏目:百科 来源:网络整理
导读:所以我现在正在学习ruby并发现了耙子.我喜欢通过实现我已经知道的东西来学习新工具,所以我尝试转换我必须耙的Makefile. 让我们说它看起来像这样: main: build/*.o clang -c $^ -o $@build/%.o: src/%.c | build clang -c $ -o $@build: mkdir build 这个Mak
所以我现在正在学习ruby并发现了耙子.我喜欢通过实现我已经知道的东西来学习新工具,所以我尝试转换我必须耙的Makefile.

让我们说它看起来像这样:

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

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

build:
    mkdir build

这个Makefile有什么特别之处:

>模式匹配%
>使用|仅订购依赖项建立

有没有办法用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

解决方法

这是rake非常擅长的东西,而且可悲的是:

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)> pathmap和file任务是文件名的Rake扩展.其他实体(如数据库条目)没有类似内容,但您通常可以自己创建它们

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读