2016-07-16 3 views
4

今はrubyを覚えており、rakeを発見しました。私はすでに知っていることを実装して新しいツールを学ぶのが好きなので、私はMakefileに変換しようとしますrakeです。Rakefileのメークファイルと同等の動作

のは、それは次のようになりましょう:

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

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

build: 
    mkdir build 

これは何のMakefileについて特別だことは次のとおりです。| build%

  • 注文のみ依存とのマッチング

    1. パターン

      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拡張です。データベースエントリなど、他のエンティティと似たものはありませんが、通常は自分で作成できます。