Class: Buildr::Kotlin::Kotlinc
- Inherits:
-
Compiler::Base
- Object
- Compiler::Base
- Buildr::Kotlin::Kotlinc
- Defined in:
- lib/buildr/kotlin/compiler.rb
Overview
Kotlin compiler:
compile.using(:kotlin)
Used by default if .kt files are found in the src/main/kotlin directory (or src/test/kotlin) and sets the target directory to target/classes (or target/test/classes). Accepts the following options:
-
:warnings – Issue warnings when compiling. True when running in verbose mode.
-
:debug – Generates bytecode with debugging information. Set from the debug
environment variable/global option.
-
:optimize – Optimize the byte code generation. False by default.
-
:target – Bytecode compatibility.
-
:noStdlib – Include the Kotlin runtime. False by default.
-
:javac – Arguments for javac compiler.
Constant Summary collapse
- REQUIRES =
The kotlin compiler jars are added to classpath at load time, if you want to customize artifact versions, you must set them on the
artifact_ns['Buildr::Compiler::Kotlinc'].library = '1.1.3-2'
namespace before this file is required. This is of course, only if KOTLIN_HOME is not set or invalid.
ArtifactNamespace.for(self) do |ns| version = Buildr.settings.build['kotlin.version'] || DEFAULT_VERSION ns.compiler! 'org.jetbrains.kotlin:kotlin-compiler:jar:>=' + version end
- Javac =
Buildr::Compiler::Javac
- OPTIONS =
[:warnings, :optimize, :target, :debug, :noStdlib, :javac]
Instance Attribute Summary
Attributes inherited from Compiler::Base
Class Method Summary collapse
-
.applies_to?(project, task) ⇒ Boolean
:nodoc:.
- .dependencies ⇒ Object
- .installed? ⇒ Boolean
- .kotlin_home ⇒ Object
- .use_installed? ⇒ Boolean
Instance Method Summary collapse
-
#compile(sources, target, dependencies) ⇒ Object
:nodoc:.
-
#initialize(project, options) ⇒ Kotlinc
constructor
:nodoc:.
Methods inherited from Compiler::Base
#dependencies, #needed?, specify, to_sym
Constructor Details
#initialize(project, options) ⇒ Kotlinc
:nodoc:
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/buildr/kotlin/compiler.rb', line 135 def initialize(project, ) #:nodoc: super # use common options also for javac [:javac] ||= Buildr::Compiler::Javac::OPTIONS.inject({}) do |hash, option| hash[option] = [option] hash end [:debug] = Buildr..debug || trace?(:kotlinc) if [:debug].nil? [:warnings] = verbose if [:warnings].nil? [:optimize] = false if [:optimize].nil? [:noStdlib] = true if [:noStdlib].nil? @java = Javac.new(project, [:javac]) end |
Class Method Details
.applies_to?(project, task) ⇒ Boolean
:nodoc:
104 105 106 107 108 109 110 |
# File 'lib/buildr/kotlin/compiler.rb', line 104 def applies_to?(project, task) #:nodoc: paths = task.sources + [sources].flatten.map { |src| Array(project.path_to(:source, task.usage, src.to_sym)) } paths.flatten! # Just select if we find .kt files paths.any? { |path| !Dir["#{path}/**/*.kt"].empty? } end |
.dependencies ⇒ Object
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/buildr/kotlin/compiler.rb', line 93 def dependencies kotlin_dependencies = if use_installed? %w(kotlin-stdlib kotlin-compiler).map { |s| File.("lib/#{s}.jar", kotlin_home) } else REQUIRES.artifacts.map(&:to_s) end # Add Java utilities (eg KotlinMessageCollector) kotlin_dependencies |= [ File.join(File.dirname(__FILE__)) ] (kotlin_dependencies).compact end |
.installed? ⇒ Boolean
81 82 83 |
# File 'lib/buildr/kotlin/compiler.rb', line 81 def installed? !kotlin_home.nil? end |
.kotlin_home ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/buildr/kotlin/compiler.rb', line 71 def kotlin_home env_home = ENV['KOTLIN_HOME'] @home ||= if !env_home.nil? && File.exists?(env_home + '/lib/kotlin-compiler.jar') env_home else nil end end |
.use_installed? ⇒ Boolean
85 86 87 88 89 90 91 |
# File 'lib/buildr/kotlin/compiler.rb', line 85 def use_installed? if installed? && Buildr.settings.build['kotlin.version'] Buildr.settings.build['kotlin.version'] == Kotlin.installed_version else Buildr.settings.build['kotlin.version'].nil? && installed? end end |
Instance Method Details
#compile(sources, target, dependencies) ⇒ Object
:nodoc:
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/buildr/kotlin/compiler.rb', line 153 def compile(sources, target, dependencies) #:nodoc: (, OPTIONS) java_sources = java_sources(sources) unless Buildr.application..dryrun = Java.org.apache.buildr.KotlinMessageCollector.new Java.load begin compiler = Java.org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.new compilerArguments = kotlinc_args compilerArguments.destination = File.(target) compilerArguments.classpath = dependencies.join(File::PATH_SEPARATOR) sources.each do |source| compilerArguments.freeArgs.add(File.(source)) end services = Buildr::Util.java_platform? ? Java.org.jetbrains.kotlin.config.Services::EMPTY : Java.org.jetbrains.kotlin.config.Services.EMPTY compiler.exec(, services, compilerArguments) rescue => e fail "Kotlin compiler crashed:\n#{e.inspect}" end unless java_sources.empty? trace 'Compiling mixed Java/Kotlin sources' deps = dependencies + Kotlinc.dependencies + [ File.(target) ] @java.compile(java_sources, target, deps) end end end |