Class: Buildr::Scala::Scalac

Inherits:
Compiler::Base show all
Defined in:
lib/buildr/scala/compiler.rb

Overview

Scalac compiler:

compile.using(:scalac)

Used by default if .scala files are found in the src/main/scala directory (or src/test/scala) and sets the target directory to target/classes (or target/test/classes).

Accepts the following options:

  • :warnings – Generate warnings if true (opposite of -nowarn).

  • :deprecation – Output source locations where deprecated APIs are used.

  • :optimise – Generates faster bytecode by applying optimisations to the program.

  • :target – Class file compatibility with specified release.

  • :debug – Generate debugging info.

  • :other – Array of options to pass to the Scalac compiler as is, e.g. -Xprint-types

Constant Summary collapse

DEFAULT_ZINC_VERSION =
'0.3.12'
DEFAULT_SBT_VERSION =
'0.13.12'
DEFAULT_JLINE_VERSION =
'2.14.2'
DEFAULT_SCALAMAIN_VERSION =
'1.0.3'
REQUIRES =

The scalac 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::Scalac'].library = '2.7.5'

namespace before this file is required. This is of course, only if SCALA_HOME is not set or invalid.

ArtifactNamespace.for(self) do |ns|
  version = Buildr.settings.build['scala.version'] || DEFAULT_VERSION
  ns.library!      'org.scala-lang:scala-library:jar:>=' + version
  ns.compiler!     'org.scala-lang:scala-compiler:jar:>=' + version
  unless Buildr::Scala.version?(2.7, 2.8, 2.9)
    # added in Scala 2.10
    ns.reflect!      'org.scala-lang:scala-reflect:jar:>=' + version
    ns.actors!       'org.scala-lang:scala-actors:jar:>=' + version
    ns.xml!          'org.scala-lang.modules:scala-xml_2.11:jar:1.0.5'
    ns.parser_combinators! 'org.scala-lang.modules:scala-parser-combinators_2.11:jar:1.0.4'

  end
end
ZINC_REQUIRES =
ArtifactNamespace.for(self) do |ns|
  zinc_version  = Buildr.settings.build['zinc.version']  || DEFAULT_ZINC_VERSION
  sbt_version   = Buildr.settings.build['sbt.version']   || DEFAULT_SBT_VERSION
  jline_version = Buildr.settings.build['jline.version'] || DEFAULT_JLINE_VERSION
  scalamain_version = Buildr.settings.build['scalamain.version'] || DEFAULT_SCALAMAIN_VERSION
  ns.zinc!          "com.typesafe.zinc:zinc:jar:>=#{zinc_version}"
  ns.sbt_interface! "com.typesafe.sbt:sbt-interface:jar:>=#{sbt_version}"
  ns.incremental!   "com.typesafe.sbt:incremental-compiler:jar:>=#{sbt_version}"
  ns.compiler_interface_sources! "com.typesafe.sbt:compiler-interface:jar:sources:>=#{sbt_version}"
  ns.jline!        "jline:jline:jar:>=#{jline_version}"
  ns.scalamain!    "io.tmio:scalamain:jar:>=#{scalamain_version}"
end
Javac =
Buildr::Compiler::Javac
OPTIONS =
[:warnings, :deprecation, :optimise, :target, :debug, :other, :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) ⇒ Scalac

:nodoc:



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/buildr/scala/compiler.rb', line 187

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 if options[:debug].nil?
  options[:warnings] = verbose if options[:warnings].nil?
  options[:deprecation] ||= false
  options[:optimise] ||= false
  options[:make] ||= :transitivenocp if Scala.version? 2.8
  @java = Javac.new(project, options[:javac])
end

Class Method Details

.applies_to?(project, task) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


134
135
136
137
138
139
140
# File 'lib/buildr/scala/compiler.rb', line 134

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 .scala files
  paths.any? { |path| !Dir["#{path}/**/*.scala"].empty? }
end

.dependenciesObject



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/buildr/scala/compiler.rb', line 118

def dependencies
  scala_dependencies = if use_installed?
    %w(scala-library scala-compiler).map { |s| File.expand_path("lib/#{s}.jar", scala_home) }
  else
    REQUIRES.artifacts.map(&:to_s)
  end

  zinc_dependencies = ZINC_REQUIRES.artifacts.map(&:to_s)

  (scala_dependencies + zinc_dependencies).compact
end

.installed?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/buildr/scala/compiler.rb', line 106

def installed?
  !scala_home.nil?
end

.scala_homeObject



96
97
98
99
100
101
102
103
104
# File 'lib/buildr/scala/compiler.rb', line 96

def scala_home
  env_home = ENV['SCALA_HOME']

  @home ||= (if !env_home.nil? && File.exists?(env_home + '/lib/scala-library.jar') && File.exists?(env_home + '/lib/scala-compiler.jar')
    env_home
  else
    nil
  end)
end

.use_fscObject



130
131
132
# File 'lib/buildr/scala/compiler.rb', line 130

def use_fsc
  use_installed? && ENV['USE_FSC'] =~ /^(yes|on|true)$/i
end

.use_installed?Boolean

Returns:

  • (Boolean)


110
111
112
113
114
115
116
# File 'lib/buildr/scala/compiler.rb', line 110

def use_installed?
  if installed? && Buildr.settings.build['scala.version']
    Buildr.settings.build['scala.version'] == Scala.installed_version
  else
    Buildr.settings.build['scala.version'].nil? && installed?
  end
end

Instance Method Details

#compile(sources, target, dependencies) ⇒ Object

:nodoc:



203
204
205
206
207
208
209
# File 'lib/buildr/scala/compiler.rb', line 203

def compile(sources, target, dependencies) #:nodoc:
  if zinc?
    compile_with_zinc(sources, target, dependencies)
  else
    compile_with_scalac(sources, target, dependencies)
  end
end

#compile_with_scalac(sources, target, dependencies) ⇒ Object

:nodoc:



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/buildr/scala/compiler.rb', line 211

def compile_with_scalac(sources, target, dependencies) #:nodoc:
  check_options(options, OPTIONS + (Scala.version?(2.8) ? [:make] : []))

  java_sources = java_sources(sources)
  enable_dep_tracing = Scala.version?(2.8) && java_sources.empty?

  dependencies.unshift target if enable_dep_tracing

  cmd_args = []
  cmd_args << '-classpath' << dependencies.join(File::PATH_SEPARATOR)
  source_paths = sources.select { |source| File.directory?(source) }
  cmd_args << '-sourcepath' << source_paths.join(File::PATH_SEPARATOR) unless source_paths.empty?
  cmd_args << '-d' << File.expand_path(target)
  cmd_args += scalac_args

  if enable_dep_tracing
    dep_dir = File.expand_path(target)
    Dir.mkdir dep_dir unless File.exists? dep_dir

    cmd_args << '-make:' + options[:make].to_s
    cmd_args << '-dependencyfile'
    cmd_args << File.expand_path('.scala-deps', dep_dir)
  end

  cmd_args += files_from_sources(sources)

  unless Buildr.application.options.dryrun
    trace((['scalac'] + cmd_args).join(' '))

    if Scalac.use_fsc
      system(([File.expand_path('bin/fsc', Scalac.scala_home)] + cmd_args).join(' ')) or
        fail 'Failed to compile, see errors above'
    else
      Java.load
      begin
        Java.scala.tools.nsc.Main.process(cmd_args.to_java(Java.java.lang.String))
      rescue => e
        fail "Scala compiler crashed:\n#{e.inspect}"
      end
      fail 'Failed to compile, see errors above' if Java.scala.tools.nsc.Main.reporter.hasErrors
    end

    unless java_sources.empty?
      trace 'Compiling mixed Java/Scala sources'

      # TODO  includes scala-compiler.jar
      deps = dependencies + Scalac.dependencies + [ File.expand_path(target) ]
      @java.compile(java_sources, target, deps)
    end
  end
end

#compile_with_zinc(sources, target, dependencies) ⇒ Object

:nodoc:



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/buildr/scala/compiler.rb', line 263

def compile_with_zinc(sources, target, dependencies) #:nodoc:

  dependencies.unshift target

  cmd_args = []
  cmd_args << '-sbt-interface' << REQUIRES.sbt_interface.artifact
  cmd_args << '-compiler-interface' << REQUIRES.compiler_interface_sources.artifact
  cmd_args << '-scala-library' << dependencies.find { |d| d =~ /scala-library/ }
  cmd_args << '-scala-compiler' << dependencies.find { |d| d =~ /scala-compiler/ }
  cmd_args << '-scala-extra' << dependencies.find { |d| d =~ /scala-reflect/ }
  cmd_args << '-classpath' << (dependencies + [ File.join(File.dirname(__FILE__)) ]).join(File::PATH_SEPARATOR)
  source_paths = sources.select { |source| File.directory?(source) }
  cmd_args << '-Ssourcepath' << ("-S" + source_paths.join(File::PATH_SEPARATOR)) unless source_paths.empty?
  cmd_args << '-d' << File.expand_path(target)
  cmd_args += scalac_args
  cmd_args << '-debug' if trace?(:scalac)

  cmd_args.map!(&:to_s)

  cmd_args += files_from_sources(sources)

  unless Buildr.application.options.dryrun
    trace((%w(io.tmio.scalamain.Main com.typesafe.zinc.Main main) + cmd_args).join(' '))
    begin
      Java::Commands.java 'io.tmio.scalamain.Main', *(%w(com.typesafe.zinc.Main main) + cmd_args + [{:classpath => Scalac.dependencies + [File.join(File.dirname(__FILE__)) ]}])
    rescue => e
      fail "Zinc compiler crashed:\n#{e.inspect}\n#{e.backtrace.join("\n")}"
    end
  end
end