2017-01-20 15 views
0

多くのプラットフォームでC++ファイルを構築するために使用されるbuild.gradleファイルがあります。これは多くのプロジェクトで行われ、すべて同じプラットフォームを対象としています。このコードを別のプラグインに移動して、ビルドファイルのコードを最小限に抑えたいと思います。Gradle 3:カスタムプラグインの中からモデル要素を追加する方法は?

だから、私はbuild.gradleで、現時点では、次のいる:

model { 
    platforms { 
     "osx-x86_64" { 
      operatingSystem "osx" 
      architecture "x86_64" 
     } 

     "stm32f4xx-arm" { 
      architecture "arm" 
     } 

     "windows" { 
      operatingSystem "windows" 
      architecture "x86_64" 
     } 

     "linux" { 
      operatingSystem "linux" 
      architecture "x86_64" 
     } 

     "pi" { 
      operatingSystem "linux" 
      architecture "arm" 
     } 

    } 

    toolChains { 
     clang(Clang) 

     gcc(Gcc) { 

      target('stm32f4xx-arm') { 
       def prefix = "arm-none-eabi-" 
       cCompiler.executable   = prefix + cCompiler.executable 
       cppCompiler.executable  = prefix + cppCompiler.executable 
       assembler.executable   = prefix + assembler.executable 
       linker.executable   = prefix + linker.executable 
       staticLibArchiver.executable = prefix + staticLibArchiver.executable 
      } 

      target('windows') { 
       def prefix = "x86_64-w64-mingw32-" 
       cCompiler.executable   = prefix + cCompiler.executable 
       cppCompiler.executable  = prefix + cppCompiler.executable 
       assembler.executable   = prefix + assembler.executable 
       linker.executable   = prefix + linker.executable 
       staticLibArchiver.executable = prefix + staticLibArchiver.executable 
      } 

      target('linux') { 
       def prefix = "x86_64-linux-" 
       cCompiler.executable   = prefix + cCompiler.executable 
       cppCompiler.executable  = prefix + cppCompiler.executable 
       assembler.executable   = prefix + assembler.executable 
       linker.executable   = prefix + linker.executable 
       staticLibArchiver.executable = prefix + staticLibArchiver.executable 
      } 

      target('pi') { 
       def prefix = "arm-none-linux-gnueabi-" 
       cCompiler.executable   = prefix + cCompiler.executable 
       cppCompiler.executable  = prefix + cppCompiler.executable 
       assembler.executable   = prefix + assembler.executable 
       linker.executable   = prefix + linker.executable 
       staticLibArchiver.executable = prefix + staticLibArchiver.executable 
      } 

     } 

    } 
} 

私はプラグインにこれ​​を動かすにはどうすればよいですか?モデル空間ではなくプロジェクト空間を使用するので、Plugin.apply()メソッドの中からモデル要素にアクセスすることはできません。これをRuleSourceルールでどのように使用するのか分かりません。

答えて

0

回答はgradleフォーラムサイト(https://discuss.gradle.org/t/gradle-3-how-to-add-to-model-elements-from-inside-a-custom-plugin/21280)のユーザーjvff(Janito Vaqueiro Ferreira Filho)からのもので、他の誰かがこのページに掲載された場合にはここでコピーしています。

That would probably be implemented in a Groovy plugin like this: 

import org.gradle.model.Mutate 
import org.gradle.model.RuleSource 
import org.gradle.nativeplatform.toolchain.Clang 
import org.gradle.nativeplatform.toolchain.Gcc 
import org.gradle.nativeplatform.toolchain.NativeToolChainRegistry 
import org.gradle.platform.base.PlatformContainer 

public class MyPlugin extends RuleSource { 
    @Mutate 
    void addPlatforms(PlatformContainer platforms) { 
     platforms.create("osx-x86_64") { platform -> 
      platform.operatingSystem "osx" 
      platform.architecture "x86_64" 
     } 

     // ... 
    } 

    @Mutate 
    void addToolChains(NativeToolChainRegistry toolChains) { 
     toolChains.create("clang", Clang) 

     toolChains.create("gcc", Gcc) { gcc -> 
      gcc.target('stm32f4xx-arm') { 
       def prefix = "arm-none-eabi-" 
       cCompiler.executable = prefix + cCompiler.executable 
       // ... 
      } 

      // ... 
     } 
    } 
} 
Hope this helps =) 
関連する問題