Class: Buildr::Kotlin::Kotlinc

Inherits:
Compiler::Base show all
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

#options

Class Method Summary collapse

Instance Method Summary collapse

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, options) #:nodoc:
  super
  # use common options also for javac

  options[:javac] ||= Buildr::Compiler::Javac::OPTIONS.inject({}) do |hash, option|
    hash[option] = options[option]
    hash
  end
  
  options[:debug] = Buildr.options.debug || trace?(:kotlinc) if options[:debug].nil?
  options[:warnings] = verbose if options[:warnings].nil?
  options[:optimize] = false if options[:optimize].nil?
  options[:noStdlib] = true if options[:noStdlib].nil?
  @java = Javac.new(project, options[:javac])
end

Class Method Details

.applies_to?(project, task) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


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

.dependenciesObject



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.expand_path("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

Returns:

  • (Boolean)


81
82
83
# File 'lib/buildr/kotlin/compiler.rb', line 81

def installed?
  !kotlin_home.nil?
end

.kotlin_homeObject



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

Returns:

  • (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:
  check_options(options, OPTIONS)

  java_sources = java_sources(sources)

  unless Buildr.application.options.dryrun
    messageCollector = Java.org.apache.buildr.KotlinMessageCollector.new

    Java.load
    begin
      compiler = Java.org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.new
      compilerArguments = kotlinc_args
      compilerArguments.destination = File.expand_path(target)
      compilerArguments.classpath = dependencies.join(File::PATH_SEPARATOR)
      sources.each do |source|
        compilerArguments.freeArgs.add(File.expand_path(source))
      end
      services = Buildr::Util.java_platform? ? Java.org.jetbrains.kotlin.config.Services::EMPTY : Java.org.jetbrains.kotlin.config.Services.EMPTY
      compiler.exec(messageCollector, 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.expand_path(target) ]
      @java.compile(java_sources, target, deps)
    end
  end
end