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

OPTIONS =
[:warnings, :deprecation, :optimise, :target, :debug, :other]

Instance Attribute Summary

Attributes inherited from Compiler::Base

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Compiler::Base

applies_to?, #dependencies, #needed?, specify, to_sym

Constructor Details

#initialize(project, options) ⇒ Scalac

:nodoc:



56
57
58
59
60
61
62
# File 'lib/buildr/scala/compiler.rb', line 56

def initialize(project, options) #:nodoc:
  super
  options[:debug] = Buildr.options.debug if options[:debug].nil?
  options[:warnings] = verbose if options[:warnings].nil?
  options[:deprecation] ||= false
  options[:optimise] ||= false
end

Class Method Details

.dependenciesObject



42
43
44
# File 'lib/buildr/scala/compiler.rb', line 42

def dependencies
  [ 'scala-library.jar', 'scala-compiler.jar'].map { |jar| File.expand_path("lib/#{jar}", scala_home) }
end

.scala_homeObject



38
39
40
# File 'lib/buildr/scala/compiler.rb', line 38

def scala_home
  @home ||= ENV['SCALA_HOME']
end

.use_fscObject



46
47
48
# File 'lib/buildr/scala/compiler.rb', line 46

def use_fsc
  ENV["USE_FSC"] =~ /^(yes|on|true)$/i
end

Instance Method Details

#compile(sources, target, dependencies) ⇒ Object

:nodoc:



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/buildr/scala/compiler.rb', line 64

def compile(sources, target, dependencies) #:nodoc:
  check_options options, OPTIONS

  cmd_args = []
  cmd_args << '-classpath' << (dependencies + Scalac.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
  cmd_args += files_from_sources(sources)

  unless Buildr.application.options.dryrun
    Scalac.scala_home or fail 'Are we forgetting something? SCALA_HOME not set.'
    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
      Java.scala.tools.nsc.Main.process(cmd_args.to_java(Java.java.lang.String))
      fail 'Failed to compile, see errors above' if Java.scala.tools.nsc.Main.reporter.hasErrors
    end
  end
end